2 * linux/arch/arm/mach-omap2/mux.c
4 * OMAP2 and OMAP3 pin multiplexing configurations
6 * Copyright (C) 2004 - 2008 Texas Instruments Inc.
7 * Copyright (C) 2003 - 2008 Nokia Corporation
9 * Written by Tony Lindgren
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <linux/module.h>
27 #include <linux/init.h>
29 #include <linux/slab.h>
30 #include <linux/spinlock.h>
31 #include <linux/list.h>
32 #include <linux/ctype.h>
33 #include <linux/debugfs.h>
34 #include <linux/seq_file.h>
35 #include <linux/uaccess.h>
37 #include <asm/system.h>
39 #include <plat/control.h>
43 #define OMAP_MUX_BASE_OFFSET 0x30 /* Offset from CTRL_BASE */
44 #define OMAP_MUX_BASE_SZ 0x5ca
45 #define MUXABLE_GPIO_MODE3 BIT(0)
47 struct omap_mux_entry {
49 struct list_head node;
52 static unsigned long mux_phys;
53 static void __iomem *mux_base;
54 static u8 omap_mux_flags;
56 u16 omap_mux_read(u16 reg)
58 if (cpu_is_omap24xx())
59 return __raw_readb(mux_base + reg);
61 return __raw_readw(mux_base + reg);
64 void omap_mux_write(u16 val, u16 reg)
66 if (cpu_is_omap24xx())
67 __raw_writeb(val, mux_base + reg);
69 __raw_writew(val, mux_base + reg);
72 void omap_mux_write_array(struct omap_board_mux *board_mux)
74 while (board_mux->reg_offset != OMAP_MUX_TERMINATOR) {
75 omap_mux_write(board_mux->value, board_mux->reg_offset);
80 static LIST_HEAD(muxmodes);
81 static DEFINE_MUTEX(muxmode_mutex);
83 #ifdef CONFIG_OMAP_MUX
85 static char *omap_mux_options;
87 int __init omap_mux_init_gpio(int gpio, int val)
89 struct omap_mux_entry *e;
95 list_for_each_entry(e, &muxmodes, node) {
96 struct omap_mux *m = &e->mux;
97 if (gpio == m->gpio) {
101 old_mode = omap_mux_read(m->reg_offset);
102 mux_mode = val & ~(OMAP_MUX_NR_MODES - 1);
103 if (omap_mux_flags & MUXABLE_GPIO_MODE3)
104 mux_mode |= OMAP_MUX_MODE3;
106 mux_mode |= OMAP_MUX_MODE4;
107 printk(KERN_DEBUG "mux: Setting signal "
108 "%s.gpio%i 0x%04x -> 0x%04x\n",
109 m->muxnames[0], gpio, old_mode, mux_mode);
110 omap_mux_write(mux_mode, m->reg_offset);
119 printk(KERN_ERR "mux: Multiple gpio paths for gpio%i\n", gpio);
123 printk(KERN_ERR "mux: Could not set gpio%i\n", gpio);
128 int __init omap_mux_init_signal(char *muxname, int val)
130 struct omap_mux_entry *e;
131 char *m0_name = NULL, *mode_name = NULL;
134 mode_name = strchr(muxname, '.');
143 list_for_each_entry(e, &muxmodes, node) {
144 struct omap_mux *m = &e->mux;
145 char *m0_entry = m->muxnames[0];
148 if (m0_name && strcmp(m0_name, m0_entry))
151 for (i = 0; i < OMAP_MUX_NR_MODES; i++) {
152 char *mode_cur = m->muxnames[i];
157 if (!strcmp(mode_name, mode_cur)) {
161 old_mode = omap_mux_read(m->reg_offset);
163 printk(KERN_DEBUG "mux: Setting signal "
164 "%s.%s 0x%04x -> 0x%04x\n",
165 m0_entry, muxname, old_mode, mux_mode);
166 omap_mux_write(mux_mode, m->reg_offset);
176 printk(KERN_ERR "mux: Multiple signal paths (%i) for %s\n",
181 printk(KERN_ERR "mux: Could not set signal %s\n", muxname);
186 #ifdef CONFIG_DEBUG_FS
188 #define OMAP_MUX_MAX_NR_FLAGS 10
189 #define OMAP_MUX_TEST_FLAG(val, mask) \
190 if (((val) & (mask)) == (mask)) { \
195 /* REVISIT: Add checking for non-optimal mux settings */
196 static inline void omap_mux_decode(struct seq_file *s, u16 val)
198 char *flags[OMAP_MUX_MAX_NR_FLAGS];
199 char mode[sizeof("OMAP_MUX_MODE") + 1];
202 sprintf(mode, "OMAP_MUX_MODE%d", val & 0x7);
206 OMAP_MUX_TEST_FLAG(val, OMAP_PIN_OFF_WAKEUPENABLE);
207 if (val & OMAP_OFF_EN) {
208 if (!(val & OMAP_OFFOUT_EN)) {
209 if (!(val & OMAP_OFF_PULL_UP)) {
210 OMAP_MUX_TEST_FLAG(val,
211 OMAP_PIN_OFF_INPUT_PULLDOWN);
213 OMAP_MUX_TEST_FLAG(val,
214 OMAP_PIN_OFF_INPUT_PULLUP);
217 if (!(val & OMAP_OFFOUT_VAL)) {
218 OMAP_MUX_TEST_FLAG(val,
219 OMAP_PIN_OFF_OUTPUT_LOW);
221 OMAP_MUX_TEST_FLAG(val,
222 OMAP_PIN_OFF_OUTPUT_HIGH);
227 if (val & OMAP_INPUT_EN) {
228 if (val & OMAP_PULL_ENA) {
229 if (!(val & OMAP_PULL_UP)) {
230 OMAP_MUX_TEST_FLAG(val,
231 OMAP_PIN_INPUT_PULLDOWN);
233 OMAP_MUX_TEST_FLAG(val, OMAP_PIN_INPUT_PULLUP);
236 OMAP_MUX_TEST_FLAG(val, OMAP_PIN_INPUT);
240 flags[i] = "OMAP_PIN_OUTPUT";
244 seq_printf(s, "%s", flags[i]);
246 seq_printf(s, " | ");
250 #define OMAP_MUX_DEFNAME_LEN 16
252 static int omap_mux_dbg_board_show(struct seq_file *s, void *unused)
254 struct omap_mux_entry *e;
256 list_for_each_entry(e, &muxmodes, node) {
257 struct omap_mux *m = &e->mux;
258 char m0_def[OMAP_MUX_DEFNAME_LEN];
259 char *m0_name = m->muxnames[0];
266 /* REVISIT: Needs to be updated if mode0 names get longer */
267 for (i = 0; i < OMAP_MUX_DEFNAME_LEN; i++) {
268 if (m0_name[i] == '\0') {
269 m0_def[i] = m0_name[i];
272 m0_def[i] = toupper(m0_name[i]);
274 val = omap_mux_read(m->reg_offset);
275 mode = val & OMAP_MUX_MODE7;
277 seq_printf(s, "OMAP%i_MUX(%s, ",
278 cpu_is_omap34xx() ? 3 : 0, m0_def);
279 omap_mux_decode(s, val);
280 seq_printf(s, "),\n");
286 static int omap_mux_dbg_board_open(struct inode *inode, struct file *file)
288 return single_open(file, omap_mux_dbg_board_show, &inode->i_private);
291 static const struct file_operations omap_mux_dbg_board_fops = {
292 .open = omap_mux_dbg_board_open,
295 .release = single_release,
298 static int omap_mux_dbg_signal_show(struct seq_file *s, void *unused)
300 struct omap_mux *m = s->private;
301 const char *none = "NA";
305 val = omap_mux_read(m->reg_offset);
306 mode = val & OMAP_MUX_MODE7;
308 seq_printf(s, "name: %s.%s (0x%08lx/0x%03x = 0x%04x), b %s, t %s\n",
309 m->muxnames[0], m->muxnames[mode],
310 mux_phys + m->reg_offset, m->reg_offset, val,
311 m->balls[0] ? m->balls[0] : none,
312 m->balls[1] ? m->balls[1] : none);
313 seq_printf(s, "mode: ");
314 omap_mux_decode(s, val);
316 seq_printf(s, "signals: %s | %s | %s | %s | %s | %s | %s | %s\n",
317 m->muxnames[0] ? m->muxnames[0] : none,
318 m->muxnames[1] ? m->muxnames[1] : none,
319 m->muxnames[2] ? m->muxnames[2] : none,
320 m->muxnames[3] ? m->muxnames[3] : none,
321 m->muxnames[4] ? m->muxnames[4] : none,
322 m->muxnames[5] ? m->muxnames[5] : none,
323 m->muxnames[6] ? m->muxnames[6] : none,
324 m->muxnames[7] ? m->muxnames[7] : none);
329 #define OMAP_MUX_MAX_ARG_CHAR 7
331 static ssize_t omap_mux_dbg_signal_write(struct file *file,
332 const char __user *user_buf,
333 size_t count, loff_t *ppos)
335 char buf[OMAP_MUX_MAX_ARG_CHAR];
336 struct seq_file *seqf;
341 if (count > OMAP_MUX_MAX_ARG_CHAR)
344 memset(buf, 0, sizeof(buf));
345 buf_size = min(count, sizeof(buf) - 1);
347 if (copy_from_user(buf, user_buf, buf_size))
350 ret = strict_strtoul(buf, 0x10, &val);
357 seqf = file->private_data;
360 omap_mux_write((u16)val, m->reg_offset);
366 static int omap_mux_dbg_signal_open(struct inode *inode, struct file *file)
368 return single_open(file, omap_mux_dbg_signal_show, inode->i_private);
371 static const struct file_operations omap_mux_dbg_signal_fops = {
372 .open = omap_mux_dbg_signal_open,
374 .write = omap_mux_dbg_signal_write,
376 .release = single_release,
379 static struct dentry *mux_dbg_dir;
381 static void __init omap_mux_dbg_init(void)
383 struct omap_mux_entry *e;
385 mux_dbg_dir = debugfs_create_dir("omap_mux", NULL);
389 (void)debugfs_create_file("board", S_IRUGO, mux_dbg_dir,
390 NULL, &omap_mux_dbg_board_fops);
392 list_for_each_entry(e, &muxmodes, node) {
393 struct omap_mux *m = &e->mux;
395 (void)debugfs_create_file(m->muxnames[0], S_IWUGO, mux_dbg_dir,
396 m, &omap_mux_dbg_signal_fops);
401 static inline void omap_mux_dbg_init(void)
404 #endif /* CONFIG_DEBUG_FS */
406 static void __init omap_mux_free_names(struct omap_mux *m)
410 for (i = 0; i < OMAP_MUX_NR_MODES; i++)
411 kfree(m->muxnames[i]);
413 #ifdef CONFIG_DEBUG_FS
414 for (i = 0; i < OMAP_MUX_NR_SIDES; i++)
420 /* Free all data except for GPIO pins unless CONFIG_DEBUG_FS is set */
421 static int __init omap_mux_late_init(void)
423 struct omap_mux_entry *e, *tmp;
425 list_for_each_entry_safe(e, tmp, &muxmodes, node) {
426 struct omap_mux *m = &e->mux;
427 u16 mode = omap_mux_read(m->reg_offset);
429 if (OMAP_MODE_GPIO(mode))
432 #ifndef CONFIG_DEBUG_FS
433 mutex_lock(&muxmode_mutex);
435 mutex_unlock(&muxmode_mutex);
436 omap_mux_free_names(m);
446 late_initcall(omap_mux_late_init);
448 static void __init omap_mux_package_fixup(struct omap_mux *p,
449 struct omap_mux *superset)
451 while (p->reg_offset != OMAP_MUX_TERMINATOR) {
452 struct omap_mux *s = superset;
455 while (s->reg_offset != OMAP_MUX_TERMINATOR) {
456 if (s->reg_offset == p->reg_offset) {
464 printk(KERN_ERR "mux: Unknown entry offset 0x%x\n",
470 #ifdef CONFIG_DEBUG_FS
472 static void __init omap_mux_package_init_balls(struct omap_ball *b,
473 struct omap_mux *superset)
475 while (b->reg_offset != OMAP_MUX_TERMINATOR) {
476 struct omap_mux *s = superset;
479 while (s->reg_offset != OMAP_MUX_TERMINATOR) {
480 if (s->reg_offset == b->reg_offset) {
481 s->balls[0] = b->balls[0];
482 s->balls[1] = b->balls[1];
489 printk(KERN_ERR "mux: Unknown ball offset 0x%x\n",
495 #else /* CONFIG_DEBUG_FS */
497 static inline void omap_mux_package_init_balls(struct omap_ball *b,
498 struct omap_mux *superset)
502 #endif /* CONFIG_DEBUG_FS */
504 static int __init omap_mux_setup(char *options)
509 omap_mux_options = options;
513 __setup("omap_mux=", omap_mux_setup);
516 * Note that the omap_mux=some.signal1=0x1234,some.signal2=0x1234
517 * cmdline options only override the bootloader values.
518 * During development, please enable CONFIG_DEBUG_FS, and use the
519 * signal specific entries under debugfs.
521 static void __init omap_mux_set_cmdline_signals(void)
523 char *options, *next_opt, *token;
525 if (!omap_mux_options)
528 options = kmalloc(strlen(omap_mux_options) + 1, GFP_KERNEL);
532 strcpy(options, omap_mux_options);
535 while ((token = strsep(&next_opt, ",")) != NULL) {
540 name = strsep(&keyval, "=");
544 res = strict_strtoul(keyval, 0x10, &val);
548 omap_mux_init_signal(name, (u16)val);
555 static int __init omap_mux_copy_names(struct omap_mux *src,
556 struct omap_mux *dst)
560 for (i = 0; i < OMAP_MUX_NR_MODES; i++) {
561 if (src->muxnames[i]) {
563 kmalloc(strlen(src->muxnames[i]) + 1,
565 if (!dst->muxnames[i])
567 strcpy(dst->muxnames[i], src->muxnames[i]);
571 #ifdef CONFIG_DEBUG_FS
572 for (i = 0; i < OMAP_MUX_NR_SIDES; i++) {
575 kmalloc(strlen(src->balls[i]) + 1,
579 strcpy(dst->balls[i], src->balls[i]);
587 omap_mux_free_names(dst);
592 #endif /* CONFIG_OMAP_MUX */
594 static u16 omap_mux_get_by_gpio(int gpio)
596 struct omap_mux_entry *e;
597 u16 offset = OMAP_MUX_TERMINATOR;
599 list_for_each_entry(e, &muxmodes, node) {
600 struct omap_mux *m = &e->mux;
601 if (m->gpio == gpio) {
602 offset = m->reg_offset;
610 /* Needed for dynamic muxing of GPIO pins for off-idle */
611 u16 omap_mux_get_gpio(int gpio)
615 offset = omap_mux_get_by_gpio(gpio);
616 if (offset == OMAP_MUX_TERMINATOR) {
617 printk(KERN_ERR "mux: Could not get gpio%i\n", gpio);
621 return omap_mux_read(offset);
624 /* Needed for dynamic muxing of GPIO pins for off-idle */
625 void omap_mux_set_gpio(u16 val, int gpio)
629 offset = omap_mux_get_by_gpio(gpio);
630 if (offset == OMAP_MUX_TERMINATOR) {
631 printk(KERN_ERR "mux: Could not set gpio%i\n", gpio);
635 omap_mux_write(val, offset);
638 static struct omap_mux * __init omap_mux_list_add(struct omap_mux *src)
640 struct omap_mux_entry *entry;
643 entry = kzalloc(sizeof(struct omap_mux_entry), GFP_KERNEL);
648 memcpy(m, src, sizeof(struct omap_mux_entry));
650 #ifdef CONFIG_OMAP_MUX
651 if (omap_mux_copy_names(src, m)) {
657 mutex_lock(&muxmode_mutex);
658 list_add_tail(&entry->node, &muxmodes);
659 mutex_unlock(&muxmode_mutex);
665 * Note if CONFIG_OMAP_MUX is not selected, we will only initialize
666 * the GPIO to mux offset mapping that is needed for dynamic muxing
667 * of GPIO pins for off-idle.
669 static void __init omap_mux_init_list(struct omap_mux *superset)
671 while (superset->reg_offset != OMAP_MUX_TERMINATOR) {
672 struct omap_mux *entry;
674 #ifdef CONFIG_OMAP_MUX
675 if (!superset->muxnames || !superset->muxnames[0]) {
680 /* Skip pins that are not muxed as GPIO by bootloader */
681 if (!OMAP_MODE_GPIO(omap_mux_read(superset->reg_offset))) {
687 entry = omap_mux_list_add(superset);
689 printk(KERN_ERR "mux: Could not add entry\n");
696 #ifdef CONFIG_OMAP_MUX
698 static void omap_mux_init_package(struct omap_mux *superset,
699 struct omap_mux *package_subset,
700 struct omap_ball *package_balls)
703 omap_mux_package_fixup(package_subset, superset);
705 omap_mux_package_init_balls(package_balls, superset);
708 static void omap_mux_init_signals(struct omap_board_mux *board_mux)
710 omap_mux_set_cmdline_signals();
711 omap_mux_write_array(board_mux);
716 static void omap_mux_init_package(struct omap_mux *superset,
717 struct omap_mux *package_subset,
718 struct omap_ball *package_balls)
722 static void omap_mux_init_signals(struct omap_board_mux *board_mux)
728 int __init omap_mux_init(u32 mux_pbase, u32 mux_size,
729 struct omap_mux *superset,
730 struct omap_mux *package_subset,
731 struct omap_board_mux *board_mux,
732 struct omap_ball *package_balls)
737 mux_phys = mux_pbase;
738 mux_base = ioremap(mux_pbase, mux_size);
740 printk(KERN_ERR "mux: Could not ioremap\n");
744 if (cpu_is_omap24xx())
745 omap_mux_flags = MUXABLE_GPIO_MODE3;
747 omap_mux_init_package(superset, package_subset, package_balls);
748 omap_mux_init_list(superset);
749 omap_mux_init_signals(board_mux);