3 * arch/arm/mach-u300/padmux.c
6 * Copyright (C) 2009 ST-Ericsson AB
7 * License terms: GNU General Public License (GPL) version 2
8 * U300 PADMUX functions
9 * Author: Martin Persson <martin.persson@stericsson.com>
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/device.h>
15 #include <linux/err.h>
16 #include <linux/errno.h>
18 #include <linux/mutex.h>
19 #include <linux/string.h>
20 #include <linux/bug.h>
21 #include <linux/debugfs.h>
22 #include <linux/seq_file.h>
23 #include <mach/u300-regs.h>
24 #include <mach/syscon.h>
27 static DEFINE_MUTEX(pmx_mutex
);
29 const u32 pmx_registers
[] = {
30 (U300_SYSCON_VBASE
+ U300_SYSCON_PMC1LR
),
31 (U300_SYSCON_VBASE
+ U300_SYSCON_PMC1HR
),
32 (U300_SYSCON_VBASE
+ U300_SYSCON_PMC2R
),
33 (U300_SYSCON_VBASE
+ U300_SYSCON_PMC3R
),
34 (U300_SYSCON_VBASE
+ U300_SYSCON_PMC4R
)
37 /* High level functionality */
41 * {"PMC1LR" mask, "PMC1LR" value},
42 * {"PMC1HR" mask, "PMC1HR" value},
43 * {"PMC2R" mask, "PMC2R" value},
44 * {"PMC3R" mask, "PMC3R" value},
45 * {"PMC4R" mask, "PMC4R" value}
48 static struct pmx mmc_setting
= {
49 .setting
= U300_APP_PMX_MMC_SETTING
,
54 {U300_SYSCON_PMC1LR_MMCSD_MASK
,
55 U300_SYSCON_PMC1LR_MMCSD_MMCSD
},
59 {U300_SYSCON_PMC4R_APP_MISC_12_MASK
,
60 U300_SYSCON_PMC4R_APP_MISC_12_APP_GPIO
}
64 static struct pmx spi_setting
= {
65 .setting
= U300_APP_PMX_SPI_SETTING
,
70 {U300_SYSCON_PMC1HR_APP_SPI_2_MASK
|
71 U300_SYSCON_PMC1HR_APP_SPI_CS_1_MASK
|
72 U300_SYSCON_PMC1HR_APP_SPI_CS_2_MASK
,
73 U300_SYSCON_PMC1HR_APP_SPI_2_SPI
|
74 U300_SYSCON_PMC1HR_APP_SPI_CS_1_SPI
|
75 U300_SYSCON_PMC1HR_APP_SPI_CS_2_SPI
},
82 /* Available padmux settings */
83 static struct pmx
*pmx_settings
[] = {
88 static void update_registers(struct pmx
*pmx
, bool activate
)
90 u16 regval
, val
, mask
;
93 for (i
= 0; i
< ARRAY_SIZE(pmx_registers
); i
++) {
95 val
= pmx
->onmask
[i
].val
;
99 mask
= pmx
->onmask
[i
].mask
;
101 regval
= readw(pmx_registers
[i
]);
104 writew(regval
, pmx_registers
[i
]);
109 struct pmx
*pmx_get(struct device
*dev
, enum pmx_settings setting
)
112 struct pmx
*pmx
= ERR_PTR(-ENOENT
);
115 return ERR_PTR(-EINVAL
);
117 mutex_lock(&pmx_mutex
);
118 for (i
= 0; i
< ARRAY_SIZE(pmx_settings
); i
++) {
120 if (setting
== pmx_settings
[i
]->setting
) {
122 if (pmx_settings
[i
]->dev
!= NULL
) {
123 WARN(1, "padmux: required setting "
124 "in use by another consumer\n");
126 pmx
= pmx_settings
[i
];
128 dev_dbg(dev
, "padmux: setting nr %d is now "
129 "bound to %s and ready to use\n",
130 setting
, dev_name(dev
));
135 mutex_unlock(&pmx_mutex
);
139 EXPORT_SYMBOL(pmx_get
);
141 int pmx_put(struct device
*dev
, struct pmx
*pmx
)
146 if (pmx
== NULL
|| dev
== NULL
)
149 mutex_lock(&pmx_mutex
);
150 for (i
= 0; i
< ARRAY_SIZE(pmx_settings
); i
++) {
152 if (pmx
->setting
== pmx_settings
[i
]->setting
) {
154 if (dev
!= pmx
->dev
) {
155 WARN(1, "padmux: cannot release handle as "
156 "it is bound to another consumer\n");
160 pmx_settings
[i
]->dev
= NULL
;
166 mutex_unlock(&pmx_mutex
);
170 EXPORT_SYMBOL(pmx_put
);
172 int pmx_activate(struct device
*dev
, struct pmx
*pmx
)
177 if (pmx
== NULL
|| dev
== NULL
)
180 mutex_lock(&pmx_mutex
);
182 /* Make sure the required bits are not used */
183 for (i
= 0; i
< ARRAY_SIZE(pmx_settings
); i
++) {
185 if (pmx_settings
[i
]->dev
== NULL
|| pmx_settings
[i
] == pmx
)
188 for (j
= 0; j
< ARRAY_SIZE(pmx_registers
); j
++) {
190 if (pmx_settings
[i
]->onmask
[j
].mask
& pmx
->
192 /* More than one entry on the same bits */
193 WARN(1, "padmux: cannot activate "
194 "setting. Bit conflict with "
195 "an active setting\n");
202 update_registers(pmx
, true);
203 pmx
->activated
= true;
204 dev_dbg(dev
, "padmux: setting nr %d is activated\n",
208 mutex_unlock(&pmx_mutex
);
211 EXPORT_SYMBOL(pmx_activate
);
213 int pmx_deactivate(struct device
*dev
, struct pmx
*pmx
)
218 if (pmx
== NULL
|| dev
== NULL
)
221 mutex_lock(&pmx_mutex
);
222 for (i
= 0; i
< ARRAY_SIZE(pmx_settings
); i
++) {
224 if (pmx_settings
[i
]->dev
== NULL
)
227 if (pmx
->setting
== pmx_settings
[i
]->setting
) {
229 if (dev
!= pmx
->dev
) {
230 WARN(1, "padmux: cannot deactivate "
231 "pmx setting as it was activated "
232 "by another consumer\n");
237 update_registers(pmx
, false);
238 pmx_settings
[i
]->dev
= NULL
;
239 pmx
->activated
= false;
241 dev_dbg(dev
, "padmux: setting nr %d is deactivated",
247 mutex_unlock(&pmx_mutex
);
251 EXPORT_SYMBOL(pmx_deactivate
);
254 * For internal use only. If it is to be exported,
255 * it should be reentrant. Notice that pmx_activate
256 * (i.e. runtime settings) always override default settings.
258 static int pmx_set_default(void)
260 /* Used to identify several entries on the same bits */
261 u16 modbits
[ARRAY_SIZE(pmx_registers
)];
265 memset(modbits
, 0, ARRAY_SIZE(pmx_registers
) * sizeof(u16
));
267 for (i
= 0; i
< ARRAY_SIZE(pmx_settings
); i
++) {
269 if (!pmx_settings
[i
]->default_on
)
272 for (j
= 0; j
< ARRAY_SIZE(pmx_registers
); j
++) {
274 /* Make sure there is only one entry on the same bits */
275 if (modbits
[j
] & pmx_settings
[i
]->onmask
[j
].mask
) {
279 modbits
[j
] |= pmx_settings
[i
]->onmask
[j
].mask
;
281 update_registers(pmx_settings
[i
], true);
286 #if (defined(CONFIG_DEBUG_FS) && defined(CONFIG_U300_DEBUG))
287 static int pmx_show(struct seq_file
*s
, void *data
)
290 seq_printf(s
, "-------------------------------------------------\n");
291 seq_printf(s
, "SETTING BOUND TO DEVICE STATE\n");
292 seq_printf(s
, "-------------------------------------------------\n");
293 mutex_lock(&pmx_mutex
);
294 for (i
= 0; i
< ARRAY_SIZE(pmx_settings
); i
++) {
295 /* Format pmx and device name nicely */
299 chars
= snprintf(&cdp
[0], 17, "%s", pmx_settings
[i
]->name
);
304 chars
= snprintf(&cdp
[16], 17, "%s", pmx_settings
[i
]->dev
?
305 dev_name(pmx_settings
[i
]->dev
) : "N/A");
315 pmx_settings
[i
]->activated
?
316 "ACTIVATED" : "DEACTIVATED"
320 mutex_unlock(&pmx_mutex
);
324 static int pmx_open(struct inode
*inode
, struct file
*file
)
326 return single_open(file
, pmx_show
, NULL
);
329 static const struct file_operations pmx_operations
= {
330 .owner
= THIS_MODULE
,
334 .release
= single_release
,
337 static int __init
init_pmx_read_debugfs(void)
339 /* Expose a simple debugfs interface to view pmx settings */
340 (void) debugfs_create_file("padmux", S_IFREG
| S_IRUGO
,
347 * This needs to come in after the core_initcall(),
348 * because debugfs is not available until
349 * the subsystems come up.
351 module_init(init_pmx_read_debugfs
);
354 static int __init
pmx_init(void)
358 ret
= pmx_set_default();
360 if (IS_ERR_VALUE(ret
))
361 pr_crit("padmux: default settings could not be set\n");
366 /* Should be initialized before consumers */
367 core_initcall(pmx_init
);