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_init_signal(struct omap_mux_partition *partition,
155 const char *muxname, int val)
157 struct omap_mux_entry *e;
158 const char *mode_name;
159 int found = 0, mode0_len = 0;
160 struct list_head *muxmodes = &partition->muxmodes;
162 mode_name = strchr(muxname, '.');
164 mode0_len = strlen(muxname) - strlen(mode_name);
170 list_for_each_entry(e, muxmodes, node) {
171 struct omap_mux *m = &e->mux;
172 char *m0_entry = m->muxnames[0];
175 /* First check for full name in mode0.muxmode format */
176 if (mode0_len && strncmp(muxname, m0_entry, mode0_len))
179 /* Then check for muxmode only */
180 for (i = 0; i < OMAP_MUX_NR_MODES; i++) {
181 char *mode_cur = m->muxnames[i];
186 if (!strcmp(mode_name, mode_cur)) {
190 old_mode = omap_mux_read(partition,
193 pr_debug("%s: Setting signal "
194 "%s.%s 0x%04x -> 0x%04x\n", __func__,
195 m0_entry, muxname, old_mode, mux_mode);
196 omap_mux_write(partition, mux_mode,
207 pr_err("%s: Multiple signal paths (%i) for %s\n", __func__,
212 pr_err("%s: Could not set signal %s\n", __func__, muxname);
217 int __init omap_mux_init_signal(const char *muxname, int val)
219 struct omap_mux_partition *partition;
222 list_for_each_entry(partition, &mux_partitions, node) {
223 ret = _omap_mux_init_signal(partition, muxname, val);
232 #ifdef CONFIG_DEBUG_FS
234 #define OMAP_MUX_MAX_NR_FLAGS 10
235 #define OMAP_MUX_TEST_FLAG(val, mask) \
236 if (((val) & (mask)) == (mask)) { \
241 /* REVISIT: Add checking for non-optimal mux settings */
242 static inline void omap_mux_decode(struct seq_file *s, u16 val)
244 char *flags[OMAP_MUX_MAX_NR_FLAGS];
245 char mode[sizeof("OMAP_MUX_MODE") + 1];
248 sprintf(mode, "OMAP_MUX_MODE%d", val & 0x7);
252 OMAP_MUX_TEST_FLAG(val, OMAP_PIN_OFF_WAKEUPENABLE);
253 if (val & OMAP_OFF_EN) {
254 if (!(val & OMAP_OFFOUT_EN)) {
255 if (!(val & OMAP_OFF_PULL_UP)) {
256 OMAP_MUX_TEST_FLAG(val,
257 OMAP_PIN_OFF_INPUT_PULLDOWN);
259 OMAP_MUX_TEST_FLAG(val,
260 OMAP_PIN_OFF_INPUT_PULLUP);
263 if (!(val & OMAP_OFFOUT_VAL)) {
264 OMAP_MUX_TEST_FLAG(val,
265 OMAP_PIN_OFF_OUTPUT_LOW);
267 OMAP_MUX_TEST_FLAG(val,
268 OMAP_PIN_OFF_OUTPUT_HIGH);
273 if (val & OMAP_INPUT_EN) {
274 if (val & OMAP_PULL_ENA) {
275 if (!(val & OMAP_PULL_UP)) {
276 OMAP_MUX_TEST_FLAG(val,
277 OMAP_PIN_INPUT_PULLDOWN);
279 OMAP_MUX_TEST_FLAG(val, OMAP_PIN_INPUT_PULLUP);
282 OMAP_MUX_TEST_FLAG(val, OMAP_PIN_INPUT);
286 flags[i] = "OMAP_PIN_OUTPUT";
290 seq_printf(s, "%s", flags[i]);
292 seq_printf(s, " | ");
296 #define OMAP_MUX_DEFNAME_LEN 32
298 static int omap_mux_dbg_board_show(struct seq_file *s, void *unused)
300 struct omap_mux_partition *partition = s->private;
301 struct omap_mux_entry *e;
302 u8 omap_gen = omap_rev() >> 28;
304 list_for_each_entry(e, &partition->muxmodes, node) {
305 struct omap_mux *m = &e->mux;
306 char m0_def[OMAP_MUX_DEFNAME_LEN];
307 char *m0_name = m->muxnames[0];
314 /* REVISIT: Needs to be updated if mode0 names get longer */
315 for (i = 0; i < OMAP_MUX_DEFNAME_LEN; i++) {
316 if (m0_name[i] == '\0') {
317 m0_def[i] = m0_name[i];
320 m0_def[i] = toupper(m0_name[i]);
322 val = omap_mux_read(partition, m->reg_offset);
323 mode = val & OMAP_MUX_MODE7;
325 seq_printf(s, "/* %s */\n", m->muxnames[mode]);
328 * XXX: Might be revisited to support differences accross
329 * same OMAP generation.
331 seq_printf(s, "OMAP%d_MUX(%s, ", omap_gen, m0_def);
332 omap_mux_decode(s, val);
333 seq_printf(s, "),\n");
339 static int omap_mux_dbg_board_open(struct inode *inode, struct file *file)
341 return single_open(file, omap_mux_dbg_board_show, inode->i_private);
344 static const struct file_operations omap_mux_dbg_board_fops = {
345 .open = omap_mux_dbg_board_open,
348 .release = single_release,
351 static struct omap_mux_partition *omap_mux_get_partition(struct omap_mux *mux)
353 struct omap_mux_partition *partition;
355 list_for_each_entry(partition, &mux_partitions, node) {
356 struct list_head *muxmodes = &partition->muxmodes;
357 struct omap_mux_entry *e;
359 list_for_each_entry(e, muxmodes, node) {
360 struct omap_mux *m = &e->mux;
370 static int omap_mux_dbg_signal_show(struct seq_file *s, void *unused)
372 struct omap_mux *m = s->private;
373 struct omap_mux_partition *partition;
374 const char *none = "NA";
378 partition = omap_mux_get_partition(m);
382 val = omap_mux_read(partition, m->reg_offset);
383 mode = val & OMAP_MUX_MODE7;
385 seq_printf(s, "name: %s.%s (0x%08x/0x%03x = 0x%04x), b %s, t %s\n",
386 m->muxnames[0], m->muxnames[mode],
387 partition->phys + m->reg_offset, m->reg_offset, val,
388 m->balls[0] ? m->balls[0] : none,
389 m->balls[1] ? m->balls[1] : none);
390 seq_printf(s, "mode: ");
391 omap_mux_decode(s, val);
393 seq_printf(s, "signals: %s | %s | %s | %s | %s | %s | %s | %s\n",
394 m->muxnames[0] ? m->muxnames[0] : none,
395 m->muxnames[1] ? m->muxnames[1] : none,
396 m->muxnames[2] ? m->muxnames[2] : none,
397 m->muxnames[3] ? m->muxnames[3] : none,
398 m->muxnames[4] ? m->muxnames[4] : none,
399 m->muxnames[5] ? m->muxnames[5] : none,
400 m->muxnames[6] ? m->muxnames[6] : none,
401 m->muxnames[7] ? m->muxnames[7] : none);
406 #define OMAP_MUX_MAX_ARG_CHAR 7
408 static ssize_t omap_mux_dbg_signal_write(struct file *file,
409 const char __user *user_buf,
410 size_t count, loff_t *ppos)
412 char buf[OMAP_MUX_MAX_ARG_CHAR];
413 struct seq_file *seqf;
417 struct omap_mux_partition *partition;
419 if (count > OMAP_MUX_MAX_ARG_CHAR)
422 memset(buf, 0, sizeof(buf));
423 buf_size = min(count, sizeof(buf) - 1);
425 if (copy_from_user(buf, user_buf, buf_size))
428 ret = strict_strtoul(buf, 0x10, &val);
435 seqf = file->private_data;
438 partition = omap_mux_get_partition(m);
442 omap_mux_write(partition, (u16)val, m->reg_offset);
448 static int omap_mux_dbg_signal_open(struct inode *inode, struct file *file)
450 return single_open(file, omap_mux_dbg_signal_show, inode->i_private);
453 static const struct file_operations omap_mux_dbg_signal_fops = {
454 .open = omap_mux_dbg_signal_open,
456 .write = omap_mux_dbg_signal_write,
458 .release = single_release,
461 static struct dentry *mux_dbg_dir;
463 static void __init omap_mux_dbg_create_entry(
464 struct omap_mux_partition *partition,
465 struct dentry *mux_dbg_dir)
467 struct omap_mux_entry *e;
469 list_for_each_entry(e, &partition->muxmodes, node) {
470 struct omap_mux *m = &e->mux;
472 (void)debugfs_create_file(m->muxnames[0], S_IWUGO, mux_dbg_dir,
473 m, &omap_mux_dbg_signal_fops);
477 static void __init omap_mux_dbg_init(void)
479 struct omap_mux_partition *partition;
480 static struct dentry *mux_dbg_board_dir;
482 mux_dbg_dir = debugfs_create_dir("omap_mux", NULL);
486 mux_dbg_board_dir = debugfs_create_dir("board", mux_dbg_dir);
487 if (!mux_dbg_board_dir)
490 list_for_each_entry(partition, &mux_partitions, node) {
491 omap_mux_dbg_create_entry(partition, mux_dbg_dir);
492 (void)debugfs_create_file(partition->name, S_IRUGO,
493 mux_dbg_board_dir, partition,
494 &omap_mux_dbg_board_fops);
499 static inline void omap_mux_dbg_init(void)
502 #endif /* CONFIG_DEBUG_FS */
504 static void __init omap_mux_free_names(struct omap_mux *m)
508 for (i = 0; i < OMAP_MUX_NR_MODES; i++)
509 kfree(m->muxnames[i]);
511 #ifdef CONFIG_DEBUG_FS
512 for (i = 0; i < OMAP_MUX_NR_SIDES; i++)
518 /* Free all data except for GPIO pins unless CONFIG_DEBUG_FS is set */
519 static int __init omap_mux_late_init(void)
521 struct omap_mux_partition *partition;
523 list_for_each_entry(partition, &mux_partitions, node) {
524 struct omap_mux_entry *e, *tmp;
525 list_for_each_entry_safe(e, tmp, &partition->muxmodes, node) {
526 struct omap_mux *m = &e->mux;
527 u16 mode = omap_mux_read(partition, m->reg_offset);
529 if (OMAP_MODE_GPIO(mode))
532 #ifndef CONFIG_DEBUG_FS
533 mutex_lock(&muxmode_mutex);
535 mutex_unlock(&muxmode_mutex);
536 omap_mux_free_names(m);
546 late_initcall(omap_mux_late_init);
548 static void __init omap_mux_package_fixup(struct omap_mux *p,
549 struct omap_mux *superset)
551 while (p->reg_offset != OMAP_MUX_TERMINATOR) {
552 struct omap_mux *s = superset;
555 while (s->reg_offset != OMAP_MUX_TERMINATOR) {
556 if (s->reg_offset == p->reg_offset) {
564 pr_err("%s: Unknown entry offset 0x%x\n", __func__,
570 #ifdef CONFIG_DEBUG_FS
572 static void __init omap_mux_package_init_balls(struct omap_ball *b,
573 struct omap_mux *superset)
575 while (b->reg_offset != OMAP_MUX_TERMINATOR) {
576 struct omap_mux *s = superset;
579 while (s->reg_offset != OMAP_MUX_TERMINATOR) {
580 if (s->reg_offset == b->reg_offset) {
581 s->balls[0] = b->balls[0];
582 s->balls[1] = b->balls[1];
589 pr_err("%s: Unknown ball offset 0x%x\n", __func__,
595 #else /* CONFIG_DEBUG_FS */
597 static inline void omap_mux_package_init_balls(struct omap_ball *b,
598 struct omap_mux *superset)
602 #endif /* CONFIG_DEBUG_FS */
604 static int __init omap_mux_setup(char *options)
609 omap_mux_options = options;
613 __setup("omap_mux=", omap_mux_setup);
616 * Note that the omap_mux=some.signal1=0x1234,some.signal2=0x1234
617 * cmdline options only override the bootloader values.
618 * During development, please enable CONFIG_DEBUG_FS, and use the
619 * signal specific entries under debugfs.
621 static void __init omap_mux_set_cmdline_signals(void)
623 char *options, *next_opt, *token;
625 if (!omap_mux_options)
628 options = kmalloc(strlen(omap_mux_options) + 1, GFP_KERNEL);
632 strcpy(options, omap_mux_options);
635 while ((token = strsep(&next_opt, ",")) != NULL) {
640 name = strsep(&keyval, "=");
644 res = strict_strtoul(keyval, 0x10, &val);
648 omap_mux_init_signal(name, (u16)val);
655 static int __init omap_mux_copy_names(struct omap_mux *src,
656 struct omap_mux *dst)
660 for (i = 0; i < OMAP_MUX_NR_MODES; i++) {
661 if (src->muxnames[i]) {
663 kmalloc(strlen(src->muxnames[i]) + 1,
665 if (!dst->muxnames[i])
667 strcpy(dst->muxnames[i], src->muxnames[i]);
671 #ifdef CONFIG_DEBUG_FS
672 for (i = 0; i < OMAP_MUX_NR_SIDES; i++) {
675 kmalloc(strlen(src->balls[i]) + 1,
679 strcpy(dst->balls[i], src->balls[i]);
687 omap_mux_free_names(dst);
692 #endif /* CONFIG_OMAP_MUX */
694 static struct omap_mux *omap_mux_get_by_gpio(
695 struct omap_mux_partition *partition,
698 struct omap_mux_entry *e;
699 struct omap_mux *ret = NULL;
701 list_for_each_entry(e, &partition->muxmodes, node) {
702 struct omap_mux *m = &e->mux;
703 if (m->gpio == gpio) {
712 /* Needed for dynamic muxing of GPIO pins for off-idle */
713 u16 omap_mux_get_gpio(int gpio)
715 struct omap_mux_partition *partition;
718 list_for_each_entry(partition, &mux_partitions, node) {
719 m = omap_mux_get_by_gpio(partition, gpio);
721 return omap_mux_read(partition, m->reg_offset);
724 if (!m || m->reg_offset == OMAP_MUX_TERMINATOR)
725 pr_err("%s: Could not get gpio%i\n", __func__, gpio);
727 return OMAP_MUX_TERMINATOR;
730 /* Needed for dynamic muxing of GPIO pins for off-idle */
731 void omap_mux_set_gpio(u16 val, int gpio)
733 struct omap_mux_partition *partition;
734 struct omap_mux *m = NULL;
736 list_for_each_entry(partition, &mux_partitions, node) {
737 m = omap_mux_get_by_gpio(partition, gpio);
739 omap_mux_write(partition, val, m->reg_offset);
744 if (!m || m->reg_offset == OMAP_MUX_TERMINATOR)
745 pr_err("%s: Could not set gpio%i\n", __func__, gpio);
748 static struct omap_mux * __init omap_mux_list_add(
749 struct omap_mux_partition *partition,
750 struct omap_mux *src)
752 struct omap_mux_entry *entry;
755 entry = kzalloc(sizeof(struct omap_mux_entry), GFP_KERNEL);
760 memcpy(m, src, sizeof(struct omap_mux_entry));
762 #ifdef CONFIG_OMAP_MUX
763 if (omap_mux_copy_names(src, m)) {
769 mutex_lock(&muxmode_mutex);
770 list_add_tail(&entry->node, &partition->muxmodes);
771 mutex_unlock(&muxmode_mutex);
777 * Note if CONFIG_OMAP_MUX is not selected, we will only initialize
778 * the GPIO to mux offset mapping that is needed for dynamic muxing
779 * of GPIO pins for off-idle.
781 static void __init omap_mux_init_list(struct omap_mux_partition *partition,
782 struct omap_mux *superset)
784 while (superset->reg_offset != OMAP_MUX_TERMINATOR) {
785 struct omap_mux *entry;
787 #ifdef CONFIG_OMAP_MUX
788 if (!superset->muxnames || !superset->muxnames[0]) {
793 /* Skip pins that are not muxed as GPIO by bootloader */
794 if (!OMAP_MODE_GPIO(omap_mux_read(partition,
795 superset->reg_offset))) {
801 entry = omap_mux_list_add(partition, superset);
803 pr_err("%s: Could not add entry\n", __func__);
810 #ifdef CONFIG_OMAP_MUX
812 static void omap_mux_init_package(struct omap_mux *superset,
813 struct omap_mux *package_subset,
814 struct omap_ball *package_balls)
817 omap_mux_package_fixup(package_subset, superset);
819 omap_mux_package_init_balls(package_balls, superset);
822 static void omap_mux_init_signals(struct omap_mux_partition *partition,
823 struct omap_board_mux *board_mux)
825 omap_mux_set_cmdline_signals();
826 omap_mux_write_array(partition, board_mux);
831 static void omap_mux_init_package(struct omap_mux *superset,
832 struct omap_mux *package_subset,
833 struct omap_ball *package_balls)
837 static void omap_mux_init_signals(struct omap_mux_partition *partition,
838 struct omap_board_mux *board_mux)
844 static u32 mux_partitions_cnt;
846 int __init omap_mux_init(const char *name, u32 flags,
847 u32 mux_pbase, u32 mux_size,
848 struct omap_mux *superset,
849 struct omap_mux *package_subset,
850 struct omap_board_mux *board_mux,
851 struct omap_ball *package_balls)
853 struct omap_mux_partition *partition;
855 partition = kzalloc(sizeof(struct omap_mux_partition), GFP_KERNEL);
859 partition->name = name;
860 partition->flags = flags;
861 partition->size = mux_size;
862 partition->phys = mux_pbase;
863 partition->base = ioremap(mux_pbase, mux_size);
864 if (!partition->base) {
865 pr_err("%s: Could not ioremap mux partition at 0x%08x\n",
866 __func__, partition->phys);
870 INIT_LIST_HEAD(&partition->muxmodes);
872 list_add_tail(&partition->node, &mux_partitions);
873 mux_partitions_cnt++;
874 pr_info("%s: Add partition: #%d: %s, flags: %x\n", __func__,
875 mux_partitions_cnt, partition->name, partition->flags);
877 omap_mux_init_package(superset, package_subset, package_balls);
878 omap_mux_init_list(partition, superset);
879 omap_mux_init_signals(partition, board_mux);