2 * linux/arch/arm/mach-omap2/mux.c
4 * OMAP2, OMAP3 and OMAP4 pin multiplexing configurations
6 * Copyright (C) 2004 - 2010 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/kernel.h>
27 #include <linux/init.h>
29 #include <linux/list.h>
30 #include <linux/slab.h>
31 #include <linux/ctype.h>
32 #include <linux/debugfs.h>
33 #include <linux/seq_file.h>
34 #include <linux/uaccess.h>
36 #include <asm/system.h>
41 #define OMAP_MUX_BASE_OFFSET 0x30 /* Offset from CTRL_BASE */
42 #define OMAP_MUX_BASE_SZ 0x5ca
44 struct omap_mux_entry {
46 struct list_head node;
49 static LIST_HEAD(mux_partitions);
50 static DEFINE_MUTEX(muxmode_mutex);
52 struct omap_mux_partition *omap_mux_get(const char *name)
54 struct omap_mux_partition *partition;
56 list_for_each_entry(partition, &mux_partitions, node) {
57 if (!strcmp(name, partition->name))
64 u16 omap_mux_read(struct omap_mux_partition *partition, u16 reg)
66 if (partition->flags & OMAP_MUX_REG_8BIT)
67 return __raw_readb(partition->base + reg);
69 return __raw_readw(partition->base + reg);
72 void omap_mux_write(struct omap_mux_partition *partition, u16 val,
75 if (partition->flags & OMAP_MUX_REG_8BIT)
76 __raw_writeb(val, partition->base + reg);
78 __raw_writew(val, partition->base + reg);
81 void omap_mux_write_array(struct omap_mux_partition *partition,
82 struct omap_board_mux *board_mux)
84 while (board_mux->reg_offset != OMAP_MUX_TERMINATOR) {
85 omap_mux_write(partition, board_mux->value,
86 board_mux->reg_offset);
91 #ifdef CONFIG_OMAP_MUX
93 static char *omap_mux_options;
95 static int __init _omap_mux_init_gpio(struct omap_mux_partition *partition,
98 struct omap_mux_entry *e;
99 struct omap_mux *gpio_mux = NULL;
103 struct list_head *muxmodes = &partition->muxmodes;
108 list_for_each_entry(e, muxmodes, node) {
109 struct omap_mux *m = &e->mux;
110 if (gpio == m->gpio) {
117 pr_err("%s: Could not set gpio%i\n", __func__, gpio);
122 pr_info("%s: Multiple gpio paths (%d) for gpio%i\n", __func__,
127 old_mode = omap_mux_read(partition, gpio_mux->reg_offset);
128 mux_mode = val & ~(OMAP_MUX_NR_MODES - 1);
129 if (partition->flags & OMAP_MUX_GPIO_IN_MODE3)
130 mux_mode |= OMAP_MUX_MODE3;
132 mux_mode |= OMAP_MUX_MODE4;
133 pr_debug("%s: Setting signal %s.gpio%i 0x%04x -> 0x%04x\n", __func__,
134 gpio_mux->muxnames[0], gpio, old_mode, mux_mode);
135 omap_mux_write(partition, mux_mode, gpio_mux->reg_offset);
140 int __init omap_mux_init_gpio(int gpio, int val)
142 struct omap_mux_partition *partition;
145 list_for_each_entry(partition, &mux_partitions, node) {
146 ret = _omap_mux_init_gpio(partition, gpio, val);
154 static int __init _omap_mux_get_by_name(struct omap_mux_partition *partition,
156 struct omap_mux **found_mux)
158 struct omap_mux *mux = NULL;
159 struct omap_mux_entry *e;
160 const char *mode_name;
161 int found = 0, found_mode, mode0_len = 0;
162 struct list_head *muxmodes = &partition->muxmodes;
164 mode_name = strchr(muxname, '.');
166 mode0_len = strlen(muxname) - strlen(mode_name);
172 list_for_each_entry(e, muxmodes, node) {
177 m0_entry = mux->muxnames[0];
179 /* First check for full name in mode0.muxmode format */
180 if (mode0_len && strncmp(muxname, m0_entry, mode0_len))
183 /* Then check for muxmode only */
184 for (i = 0; i < OMAP_MUX_NR_MODES; i++) {
185 char *mode_cur = mux->muxnames[i];
190 if (!strcmp(mode_name, mode_cur)) {
203 pr_err("%s: Multiple signal paths (%i) for %s\n", __func__,
208 pr_err("%s: Could not find signal %s\n", __func__, muxname);
214 omap_mux_get_by_name(const char *muxname,
215 struct omap_mux_partition **found_partition,
216 struct omap_mux **found_mux)
218 struct omap_mux_partition *partition;
220 list_for_each_entry(partition, &mux_partitions, node) {
221 struct omap_mux *mux = NULL;
222 int mux_mode = _omap_mux_get_by_name(partition, muxname, &mux);
226 *found_partition = partition;
235 int __init omap_mux_init_signal(const char *muxname, int val)
237 struct omap_mux_partition *partition = NULL;
238 struct omap_mux *mux = NULL;
242 mux_mode = omap_mux_get_by_name(muxname, &partition, &mux);
246 old_mode = omap_mux_read(partition, mux->reg_offset);
248 pr_debug("%s: Setting signal %s 0x%04x -> 0x%04x\n",
249 __func__, muxname, old_mode, mux_mode);
250 omap_mux_write(partition, mux_mode, mux->reg_offset);
255 #ifdef CONFIG_DEBUG_FS
257 #define OMAP_MUX_MAX_NR_FLAGS 10
258 #define OMAP_MUX_TEST_FLAG(val, mask) \
259 if (((val) & (mask)) == (mask)) { \
264 /* REVISIT: Add checking for non-optimal mux settings */
265 static inline void omap_mux_decode(struct seq_file *s, u16 val)
267 char *flags[OMAP_MUX_MAX_NR_FLAGS];
268 char mode[sizeof("OMAP_MUX_MODE") + 1];
271 sprintf(mode, "OMAP_MUX_MODE%d", val & 0x7);
275 OMAP_MUX_TEST_FLAG(val, OMAP_PIN_OFF_WAKEUPENABLE);
276 if (val & OMAP_OFF_EN) {
277 if (!(val & OMAP_OFFOUT_EN)) {
278 if (!(val & OMAP_OFF_PULL_UP)) {
279 OMAP_MUX_TEST_FLAG(val,
280 OMAP_PIN_OFF_INPUT_PULLDOWN);
282 OMAP_MUX_TEST_FLAG(val,
283 OMAP_PIN_OFF_INPUT_PULLUP);
286 if (!(val & OMAP_OFFOUT_VAL)) {
287 OMAP_MUX_TEST_FLAG(val,
288 OMAP_PIN_OFF_OUTPUT_LOW);
290 OMAP_MUX_TEST_FLAG(val,
291 OMAP_PIN_OFF_OUTPUT_HIGH);
296 if (val & OMAP_INPUT_EN) {
297 if (val & OMAP_PULL_ENA) {
298 if (!(val & OMAP_PULL_UP)) {
299 OMAP_MUX_TEST_FLAG(val,
300 OMAP_PIN_INPUT_PULLDOWN);
302 OMAP_MUX_TEST_FLAG(val, OMAP_PIN_INPUT_PULLUP);
305 OMAP_MUX_TEST_FLAG(val, OMAP_PIN_INPUT);
309 flags[i] = "OMAP_PIN_OUTPUT";
313 seq_printf(s, "%s", flags[i]);
315 seq_printf(s, " | ");
319 #define OMAP_MUX_DEFNAME_LEN 32
321 static int omap_mux_dbg_board_show(struct seq_file *s, void *unused)
323 struct omap_mux_partition *partition = s->private;
324 struct omap_mux_entry *e;
325 u8 omap_gen = omap_rev() >> 28;
327 list_for_each_entry(e, &partition->muxmodes, node) {
328 struct omap_mux *m = &e->mux;
329 char m0_def[OMAP_MUX_DEFNAME_LEN];
330 char *m0_name = m->muxnames[0];
337 /* REVISIT: Needs to be updated if mode0 names get longer */
338 for (i = 0; i < OMAP_MUX_DEFNAME_LEN; i++) {
339 if (m0_name[i] == '\0') {
340 m0_def[i] = m0_name[i];
343 m0_def[i] = toupper(m0_name[i]);
345 val = omap_mux_read(partition, m->reg_offset);
346 mode = val & OMAP_MUX_MODE7;
348 seq_printf(s, "/* %s */\n", m->muxnames[mode]);
351 * XXX: Might be revisited to support differences accross
352 * same OMAP generation.
354 seq_printf(s, "OMAP%d_MUX(%s, ", omap_gen, m0_def);
355 omap_mux_decode(s, val);
356 seq_printf(s, "),\n");
362 static int omap_mux_dbg_board_open(struct inode *inode, struct file *file)
364 return single_open(file, omap_mux_dbg_board_show, inode->i_private);
367 static const struct file_operations omap_mux_dbg_board_fops = {
368 .open = omap_mux_dbg_board_open,
371 .release = single_release,
374 static struct omap_mux_partition *omap_mux_get_partition(struct omap_mux *mux)
376 struct omap_mux_partition *partition;
378 list_for_each_entry(partition, &mux_partitions, node) {
379 struct list_head *muxmodes = &partition->muxmodes;
380 struct omap_mux_entry *e;
382 list_for_each_entry(e, muxmodes, node) {
383 struct omap_mux *m = &e->mux;
393 static int omap_mux_dbg_signal_show(struct seq_file *s, void *unused)
395 struct omap_mux *m = s->private;
396 struct omap_mux_partition *partition;
397 const char *none = "NA";
401 partition = omap_mux_get_partition(m);
405 val = omap_mux_read(partition, m->reg_offset);
406 mode = val & OMAP_MUX_MODE7;
408 seq_printf(s, "name: %s.%s (0x%08x/0x%03x = 0x%04x), b %s, t %s\n",
409 m->muxnames[0], m->muxnames[mode],
410 partition->phys + m->reg_offset, m->reg_offset, val,
411 m->balls[0] ? m->balls[0] : none,
412 m->balls[1] ? m->balls[1] : none);
413 seq_printf(s, "mode: ");
414 omap_mux_decode(s, val);
416 seq_printf(s, "signals: %s | %s | %s | %s | %s | %s | %s | %s\n",
417 m->muxnames[0] ? m->muxnames[0] : none,
418 m->muxnames[1] ? m->muxnames[1] : none,
419 m->muxnames[2] ? m->muxnames[2] : none,
420 m->muxnames[3] ? m->muxnames[3] : none,
421 m->muxnames[4] ? m->muxnames[4] : none,
422 m->muxnames[5] ? m->muxnames[5] : none,
423 m->muxnames[6] ? m->muxnames[6] : none,
424 m->muxnames[7] ? m->muxnames[7] : none);
429 #define OMAP_MUX_MAX_ARG_CHAR 7
431 static ssize_t omap_mux_dbg_signal_write(struct file *file,
432 const char __user *user_buf,
433 size_t count, loff_t *ppos)
435 char buf[OMAP_MUX_MAX_ARG_CHAR];
436 struct seq_file *seqf;
440 struct omap_mux_partition *partition;
442 if (count > OMAP_MUX_MAX_ARG_CHAR)
445 memset(buf, 0, sizeof(buf));
446 buf_size = min(count, sizeof(buf) - 1);
448 if (copy_from_user(buf, user_buf, buf_size))
451 ret = strict_strtoul(buf, 0x10, &val);
458 seqf = file->private_data;
461 partition = omap_mux_get_partition(m);
465 omap_mux_write(partition, (u16)val, m->reg_offset);
471 static int omap_mux_dbg_signal_open(struct inode *inode, struct file *file)
473 return single_open(file, omap_mux_dbg_signal_show, inode->i_private);
476 static const struct file_operations omap_mux_dbg_signal_fops = {
477 .open = omap_mux_dbg_signal_open,
479 .write = omap_mux_dbg_signal_write,
481 .release = single_release,
484 static struct dentry *mux_dbg_dir;
486 static void __init omap_mux_dbg_create_entry(
487 struct omap_mux_partition *partition,
488 struct dentry *mux_dbg_dir)
490 struct omap_mux_entry *e;
492 list_for_each_entry(e, &partition->muxmodes, node) {
493 struct omap_mux *m = &e->mux;
495 (void)debugfs_create_file(m->muxnames[0], S_IWUGO, mux_dbg_dir,
496 m, &omap_mux_dbg_signal_fops);
500 static void __init omap_mux_dbg_init(void)
502 struct omap_mux_partition *partition;
503 static struct dentry *mux_dbg_board_dir;
505 mux_dbg_dir = debugfs_create_dir("omap_mux", NULL);
509 mux_dbg_board_dir = debugfs_create_dir("board", mux_dbg_dir);
510 if (!mux_dbg_board_dir)
513 list_for_each_entry(partition, &mux_partitions, node) {
514 omap_mux_dbg_create_entry(partition, mux_dbg_dir);
515 (void)debugfs_create_file(partition->name, S_IRUGO,
516 mux_dbg_board_dir, partition,
517 &omap_mux_dbg_board_fops);
522 static inline void omap_mux_dbg_init(void)
525 #endif /* CONFIG_DEBUG_FS */
527 static void __init omap_mux_free_names(struct omap_mux *m)
531 for (i = 0; i < OMAP_MUX_NR_MODES; i++)
532 kfree(m->muxnames[i]);
534 #ifdef CONFIG_DEBUG_FS
535 for (i = 0; i < OMAP_MUX_NR_SIDES; i++)
541 /* Free all data except for GPIO pins unless CONFIG_DEBUG_FS is set */
542 static int __init omap_mux_late_init(void)
544 struct omap_mux_partition *partition;
546 list_for_each_entry(partition, &mux_partitions, node) {
547 struct omap_mux_entry *e, *tmp;
548 list_for_each_entry_safe(e, tmp, &partition->muxmodes, node) {
549 struct omap_mux *m = &e->mux;
550 u16 mode = omap_mux_read(partition, m->reg_offset);
552 if (OMAP_MODE_GPIO(mode))
555 #ifndef CONFIG_DEBUG_FS
556 mutex_lock(&muxmode_mutex);
558 mutex_unlock(&muxmode_mutex);
559 omap_mux_free_names(m);
569 late_initcall(omap_mux_late_init);
571 static void __init omap_mux_package_fixup(struct omap_mux *p,
572 struct omap_mux *superset)
574 while (p->reg_offset != OMAP_MUX_TERMINATOR) {
575 struct omap_mux *s = superset;
578 while (s->reg_offset != OMAP_MUX_TERMINATOR) {
579 if (s->reg_offset == p->reg_offset) {
587 pr_err("%s: Unknown entry offset 0x%x\n", __func__,
593 #ifdef CONFIG_DEBUG_FS
595 static void __init omap_mux_package_init_balls(struct omap_ball *b,
596 struct omap_mux *superset)
598 while (b->reg_offset != OMAP_MUX_TERMINATOR) {
599 struct omap_mux *s = superset;
602 while (s->reg_offset != OMAP_MUX_TERMINATOR) {
603 if (s->reg_offset == b->reg_offset) {
604 s->balls[0] = b->balls[0];
605 s->balls[1] = b->balls[1];
612 pr_err("%s: Unknown ball offset 0x%x\n", __func__,
618 #else /* CONFIG_DEBUG_FS */
620 static inline void omap_mux_package_init_balls(struct omap_ball *b,
621 struct omap_mux *superset)
625 #endif /* CONFIG_DEBUG_FS */
627 static int __init omap_mux_setup(char *options)
632 omap_mux_options = options;
636 __setup("omap_mux=", omap_mux_setup);
639 * Note that the omap_mux=some.signal1=0x1234,some.signal2=0x1234
640 * cmdline options only override the bootloader values.
641 * During development, please enable CONFIG_DEBUG_FS, and use the
642 * signal specific entries under debugfs.
644 static void __init omap_mux_set_cmdline_signals(void)
646 char *options, *next_opt, *token;
648 if (!omap_mux_options)
651 options = kmalloc(strlen(omap_mux_options) + 1, GFP_KERNEL);
655 strcpy(options, omap_mux_options);
658 while ((token = strsep(&next_opt, ",")) != NULL) {
663 name = strsep(&keyval, "=");
667 res = strict_strtoul(keyval, 0x10, &val);
671 omap_mux_init_signal(name, (u16)val);
678 static int __init omap_mux_copy_names(struct omap_mux *src,
679 struct omap_mux *dst)
683 for (i = 0; i < OMAP_MUX_NR_MODES; i++) {
684 if (src->muxnames[i]) {
686 kmalloc(strlen(src->muxnames[i]) + 1,
688 if (!dst->muxnames[i])
690 strcpy(dst->muxnames[i], src->muxnames[i]);
694 #ifdef CONFIG_DEBUG_FS
695 for (i = 0; i < OMAP_MUX_NR_SIDES; i++) {
698 kmalloc(strlen(src->balls[i]) + 1,
702 strcpy(dst->balls[i], src->balls[i]);
710 omap_mux_free_names(dst);
715 #endif /* CONFIG_OMAP_MUX */
717 static struct omap_mux *omap_mux_get_by_gpio(
718 struct omap_mux_partition *partition,
721 struct omap_mux_entry *e;
722 struct omap_mux *ret = NULL;
724 list_for_each_entry(e, &partition->muxmodes, node) {
725 struct omap_mux *m = &e->mux;
726 if (m->gpio == gpio) {
735 /* Needed for dynamic muxing of GPIO pins for off-idle */
736 u16 omap_mux_get_gpio(int gpio)
738 struct omap_mux_partition *partition;
741 list_for_each_entry(partition, &mux_partitions, node) {
742 m = omap_mux_get_by_gpio(partition, gpio);
744 return omap_mux_read(partition, m->reg_offset);
747 if (!m || m->reg_offset == OMAP_MUX_TERMINATOR)
748 pr_err("%s: Could not get gpio%i\n", __func__, gpio);
750 return OMAP_MUX_TERMINATOR;
753 /* Needed for dynamic muxing of GPIO pins for off-idle */
754 void omap_mux_set_gpio(u16 val, int gpio)
756 struct omap_mux_partition *partition;
757 struct omap_mux *m = NULL;
759 list_for_each_entry(partition, &mux_partitions, node) {
760 m = omap_mux_get_by_gpio(partition, gpio);
762 omap_mux_write(partition, val, m->reg_offset);
767 if (!m || m->reg_offset == OMAP_MUX_TERMINATOR)
768 pr_err("%s: Could not set gpio%i\n", __func__, gpio);
771 static struct omap_mux * __init omap_mux_list_add(
772 struct omap_mux_partition *partition,
773 struct omap_mux *src)
775 struct omap_mux_entry *entry;
778 entry = kzalloc(sizeof(struct omap_mux_entry), GFP_KERNEL);
783 memcpy(m, src, sizeof(struct omap_mux_entry));
785 #ifdef CONFIG_OMAP_MUX
786 if (omap_mux_copy_names(src, m)) {
792 mutex_lock(&muxmode_mutex);
793 list_add_tail(&entry->node, &partition->muxmodes);
794 mutex_unlock(&muxmode_mutex);
800 * Note if CONFIG_OMAP_MUX is not selected, we will only initialize
801 * the GPIO to mux offset mapping that is needed for dynamic muxing
802 * of GPIO pins for off-idle.
804 static void __init omap_mux_init_list(struct omap_mux_partition *partition,
805 struct omap_mux *superset)
807 while (superset->reg_offset != OMAP_MUX_TERMINATOR) {
808 struct omap_mux *entry;
810 #ifdef CONFIG_OMAP_MUX
811 if (!superset->muxnames || !superset->muxnames[0]) {
816 /* Skip pins that are not muxed as GPIO by bootloader */
817 if (!OMAP_MODE_GPIO(omap_mux_read(partition,
818 superset->reg_offset))) {
824 entry = omap_mux_list_add(partition, superset);
826 pr_err("%s: Could not add entry\n", __func__);
833 #ifdef CONFIG_OMAP_MUX
835 static void omap_mux_init_package(struct omap_mux *superset,
836 struct omap_mux *package_subset,
837 struct omap_ball *package_balls)
840 omap_mux_package_fixup(package_subset, superset);
842 omap_mux_package_init_balls(package_balls, superset);
845 static void omap_mux_init_signals(struct omap_mux_partition *partition,
846 struct omap_board_mux *board_mux)
848 omap_mux_set_cmdline_signals();
849 omap_mux_write_array(partition, board_mux);
854 static void omap_mux_init_package(struct omap_mux *superset,
855 struct omap_mux *package_subset,
856 struct omap_ball *package_balls)
860 static void omap_mux_init_signals(struct omap_mux_partition *partition,
861 struct omap_board_mux *board_mux)
867 static u32 mux_partitions_cnt;
869 int __init omap_mux_init(const char *name, u32 flags,
870 u32 mux_pbase, u32 mux_size,
871 struct omap_mux *superset,
872 struct omap_mux *package_subset,
873 struct omap_board_mux *board_mux,
874 struct omap_ball *package_balls)
876 struct omap_mux_partition *partition;
878 partition = kzalloc(sizeof(struct omap_mux_partition), GFP_KERNEL);
882 partition->name = name;
883 partition->flags = flags;
884 partition->size = mux_size;
885 partition->phys = mux_pbase;
886 partition->base = ioremap(mux_pbase, mux_size);
887 if (!partition->base) {
888 pr_err("%s: Could not ioremap mux partition at 0x%08x\n",
889 __func__, partition->phys);
893 INIT_LIST_HEAD(&partition->muxmodes);
895 list_add_tail(&partition->node, &mux_partitions);
896 mux_partitions_cnt++;
897 pr_info("%s: Add partition: #%d: %s, flags: %x\n", __func__,
898 mux_partitions_cnt, partition->name, partition->flags);
900 omap_mux_init_package(superset, package_subset, package_balls);
901 omap_mux_init_list(partition, superset);
902 omap_mux_init_signals(partition, board_mux);