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
;
90 struct omap_mux
*gpio_mux
;
98 list_for_each_entry(e
, &muxmodes
, node
) {
99 struct omap_mux
*m
= &e
->mux
;
100 if (gpio
== m
->gpio
) {
107 printk(KERN_ERR
"mux: Could not set gpio%i\n", gpio
);
112 printk(KERN_INFO
"mux: Multiple gpio paths (%d) for gpio%i\n",
117 old_mode
= omap_mux_read(gpio_mux
->reg_offset
);
118 mux_mode
= val
& ~(OMAP_MUX_NR_MODES
- 1);
119 if (omap_mux_flags
& MUXABLE_GPIO_MODE3
)
120 mux_mode
|= OMAP_MUX_MODE3
;
122 mux_mode
|= OMAP_MUX_MODE4
;
123 printk(KERN_DEBUG
"mux: Setting signal %s.gpio%i 0x%04x -> 0x%04x\n",
124 gpio_mux
->muxnames
[0], gpio
, old_mode
, mux_mode
);
125 omap_mux_write(mux_mode
, gpio_mux
->reg_offset
);
130 int __init
omap_mux_init_signal(char *muxname
, int val
)
132 struct omap_mux_entry
*e
;
133 char *m0_name
= NULL
, *mode_name
= NULL
;
136 mode_name
= strchr(muxname
, '.');
145 list_for_each_entry(e
, &muxmodes
, node
) {
146 struct omap_mux
*m
= &e
->mux
;
147 char *m0_entry
= m
->muxnames
[0];
150 if (m0_name
&& strcmp(m0_name
, m0_entry
))
153 for (i
= 0; i
< OMAP_MUX_NR_MODES
; i
++) {
154 char *mode_cur
= m
->muxnames
[i
];
159 if (!strcmp(mode_name
, mode_cur
)) {
163 old_mode
= omap_mux_read(m
->reg_offset
);
165 printk(KERN_DEBUG
"mux: Setting signal "
166 "%s.%s 0x%04x -> 0x%04x\n",
167 m0_entry
, muxname
, old_mode
, mux_mode
);
168 omap_mux_write(mux_mode
, m
->reg_offset
);
178 printk(KERN_ERR
"mux: Multiple signal paths (%i) for %s\n",
183 printk(KERN_ERR
"mux: Could not set signal %s\n", muxname
);
188 #ifdef CONFIG_DEBUG_FS
190 #define OMAP_MUX_MAX_NR_FLAGS 10
191 #define OMAP_MUX_TEST_FLAG(val, mask) \
192 if (((val) & (mask)) == (mask)) { \
197 /* REVISIT: Add checking for non-optimal mux settings */
198 static inline void omap_mux_decode(struct seq_file
*s
, u16 val
)
200 char *flags
[OMAP_MUX_MAX_NR_FLAGS
];
201 char mode
[sizeof("OMAP_MUX_MODE") + 1];
204 sprintf(mode
, "OMAP_MUX_MODE%d", val
& 0x7);
208 OMAP_MUX_TEST_FLAG(val
, OMAP_PIN_OFF_WAKEUPENABLE
);
209 if (val
& OMAP_OFF_EN
) {
210 if (!(val
& OMAP_OFFOUT_EN
)) {
211 if (!(val
& OMAP_OFF_PULL_UP
)) {
212 OMAP_MUX_TEST_FLAG(val
,
213 OMAP_PIN_OFF_INPUT_PULLDOWN
);
215 OMAP_MUX_TEST_FLAG(val
,
216 OMAP_PIN_OFF_INPUT_PULLUP
);
219 if (!(val
& OMAP_OFFOUT_VAL
)) {
220 OMAP_MUX_TEST_FLAG(val
,
221 OMAP_PIN_OFF_OUTPUT_LOW
);
223 OMAP_MUX_TEST_FLAG(val
,
224 OMAP_PIN_OFF_OUTPUT_HIGH
);
229 if (val
& OMAP_INPUT_EN
) {
230 if (val
& OMAP_PULL_ENA
) {
231 if (!(val
& OMAP_PULL_UP
)) {
232 OMAP_MUX_TEST_FLAG(val
,
233 OMAP_PIN_INPUT_PULLDOWN
);
235 OMAP_MUX_TEST_FLAG(val
, OMAP_PIN_INPUT_PULLUP
);
238 OMAP_MUX_TEST_FLAG(val
, OMAP_PIN_INPUT
);
242 flags
[i
] = "OMAP_PIN_OUTPUT";
246 seq_printf(s
, "%s", flags
[i
]);
248 seq_printf(s
, " | ");
252 #define OMAP_MUX_DEFNAME_LEN 16
254 static int omap_mux_dbg_board_show(struct seq_file
*s
, void *unused
)
256 struct omap_mux_entry
*e
;
258 list_for_each_entry(e
, &muxmodes
, node
) {
259 struct omap_mux
*m
= &e
->mux
;
260 char m0_def
[OMAP_MUX_DEFNAME_LEN
];
261 char *m0_name
= m
->muxnames
[0];
268 /* REVISIT: Needs to be updated if mode0 names get longer */
269 for (i
= 0; i
< OMAP_MUX_DEFNAME_LEN
; i
++) {
270 if (m0_name
[i
] == '\0') {
271 m0_def
[i
] = m0_name
[i
];
274 m0_def
[i
] = toupper(m0_name
[i
]);
276 val
= omap_mux_read(m
->reg_offset
);
277 mode
= val
& OMAP_MUX_MODE7
;
279 seq_printf(s
, "OMAP%i_MUX(%s, ",
280 cpu_is_omap34xx() ? 3 : 0, m0_def
);
281 omap_mux_decode(s
, val
);
282 seq_printf(s
, "),\n");
288 static int omap_mux_dbg_board_open(struct inode
*inode
, struct file
*file
)
290 return single_open(file
, omap_mux_dbg_board_show
, &inode
->i_private
);
293 static const struct file_operations omap_mux_dbg_board_fops
= {
294 .open
= omap_mux_dbg_board_open
,
297 .release
= single_release
,
300 static int omap_mux_dbg_signal_show(struct seq_file
*s
, void *unused
)
302 struct omap_mux
*m
= s
->private;
303 const char *none
= "NA";
307 val
= omap_mux_read(m
->reg_offset
);
308 mode
= val
& OMAP_MUX_MODE7
;
310 seq_printf(s
, "name: %s.%s (0x%08lx/0x%03x = 0x%04x), b %s, t %s\n",
311 m
->muxnames
[0], m
->muxnames
[mode
],
312 mux_phys
+ m
->reg_offset
, m
->reg_offset
, val
,
313 m
->balls
[0] ? m
->balls
[0] : none
,
314 m
->balls
[1] ? m
->balls
[1] : none
);
315 seq_printf(s
, "mode: ");
316 omap_mux_decode(s
, val
);
318 seq_printf(s
, "signals: %s | %s | %s | %s | %s | %s | %s | %s\n",
319 m
->muxnames
[0] ? m
->muxnames
[0] : none
,
320 m
->muxnames
[1] ? m
->muxnames
[1] : none
,
321 m
->muxnames
[2] ? m
->muxnames
[2] : none
,
322 m
->muxnames
[3] ? m
->muxnames
[3] : none
,
323 m
->muxnames
[4] ? m
->muxnames
[4] : none
,
324 m
->muxnames
[5] ? m
->muxnames
[5] : none
,
325 m
->muxnames
[6] ? m
->muxnames
[6] : none
,
326 m
->muxnames
[7] ? m
->muxnames
[7] : none
);
331 #define OMAP_MUX_MAX_ARG_CHAR 7
333 static ssize_t
omap_mux_dbg_signal_write(struct file
*file
,
334 const char __user
*user_buf
,
335 size_t count
, loff_t
*ppos
)
337 char buf
[OMAP_MUX_MAX_ARG_CHAR
];
338 struct seq_file
*seqf
;
343 if (count
> OMAP_MUX_MAX_ARG_CHAR
)
346 memset(buf
, 0, sizeof(buf
));
347 buf_size
= min(count
, sizeof(buf
) - 1);
349 if (copy_from_user(buf
, user_buf
, buf_size
))
352 ret
= strict_strtoul(buf
, 0x10, &val
);
359 seqf
= file
->private_data
;
362 omap_mux_write((u16
)val
, m
->reg_offset
);
368 static int omap_mux_dbg_signal_open(struct inode
*inode
, struct file
*file
)
370 return single_open(file
, omap_mux_dbg_signal_show
, inode
->i_private
);
373 static const struct file_operations omap_mux_dbg_signal_fops
= {
374 .open
= omap_mux_dbg_signal_open
,
376 .write
= omap_mux_dbg_signal_write
,
378 .release
= single_release
,
381 static struct dentry
*mux_dbg_dir
;
383 static void __init
omap_mux_dbg_init(void)
385 struct omap_mux_entry
*e
;
387 mux_dbg_dir
= debugfs_create_dir("omap_mux", NULL
);
391 (void)debugfs_create_file("board", S_IRUGO
, mux_dbg_dir
,
392 NULL
, &omap_mux_dbg_board_fops
);
394 list_for_each_entry(e
, &muxmodes
, node
) {
395 struct omap_mux
*m
= &e
->mux
;
397 (void)debugfs_create_file(m
->muxnames
[0], S_IWUGO
, mux_dbg_dir
,
398 m
, &omap_mux_dbg_signal_fops
);
403 static inline void omap_mux_dbg_init(void)
406 #endif /* CONFIG_DEBUG_FS */
408 static void __init
omap_mux_free_names(struct omap_mux
*m
)
412 for (i
= 0; i
< OMAP_MUX_NR_MODES
; i
++)
413 kfree(m
->muxnames
[i
]);
415 #ifdef CONFIG_DEBUG_FS
416 for (i
= 0; i
< OMAP_MUX_NR_SIDES
; i
++)
422 /* Free all data except for GPIO pins unless CONFIG_DEBUG_FS is set */
423 static int __init
omap_mux_late_init(void)
425 struct omap_mux_entry
*e
, *tmp
;
427 list_for_each_entry_safe(e
, tmp
, &muxmodes
, node
) {
428 struct omap_mux
*m
= &e
->mux
;
429 u16 mode
= omap_mux_read(m
->reg_offset
);
431 if (OMAP_MODE_GPIO(mode
))
434 #ifndef CONFIG_DEBUG_FS
435 mutex_lock(&muxmode_mutex
);
437 mutex_unlock(&muxmode_mutex
);
438 omap_mux_free_names(m
);
448 late_initcall(omap_mux_late_init
);
450 static void __init
omap_mux_package_fixup(struct omap_mux
*p
,
451 struct omap_mux
*superset
)
453 while (p
->reg_offset
!= OMAP_MUX_TERMINATOR
) {
454 struct omap_mux
*s
= superset
;
457 while (s
->reg_offset
!= OMAP_MUX_TERMINATOR
) {
458 if (s
->reg_offset
== p
->reg_offset
) {
466 printk(KERN_ERR
"mux: Unknown entry offset 0x%x\n",
472 #ifdef CONFIG_DEBUG_FS
474 static void __init
omap_mux_package_init_balls(struct omap_ball
*b
,
475 struct omap_mux
*superset
)
477 while (b
->reg_offset
!= OMAP_MUX_TERMINATOR
) {
478 struct omap_mux
*s
= superset
;
481 while (s
->reg_offset
!= OMAP_MUX_TERMINATOR
) {
482 if (s
->reg_offset
== b
->reg_offset
) {
483 s
->balls
[0] = b
->balls
[0];
484 s
->balls
[1] = b
->balls
[1];
491 printk(KERN_ERR
"mux: Unknown ball offset 0x%x\n",
497 #else /* CONFIG_DEBUG_FS */
499 static inline void omap_mux_package_init_balls(struct omap_ball
*b
,
500 struct omap_mux
*superset
)
504 #endif /* CONFIG_DEBUG_FS */
506 static int __init
omap_mux_setup(char *options
)
511 omap_mux_options
= options
;
515 __setup("omap_mux=", omap_mux_setup
);
518 * Note that the omap_mux=some.signal1=0x1234,some.signal2=0x1234
519 * cmdline options only override the bootloader values.
520 * During development, please enable CONFIG_DEBUG_FS, and use the
521 * signal specific entries under debugfs.
523 static void __init
omap_mux_set_cmdline_signals(void)
525 char *options
, *next_opt
, *token
;
527 if (!omap_mux_options
)
530 options
= kmalloc(strlen(omap_mux_options
) + 1, GFP_KERNEL
);
534 strcpy(options
, omap_mux_options
);
537 while ((token
= strsep(&next_opt
, ",")) != NULL
) {
542 name
= strsep(&keyval
, "=");
546 res
= strict_strtoul(keyval
, 0x10, &val
);
550 omap_mux_init_signal(name
, (u16
)val
);
557 static int __init
omap_mux_copy_names(struct omap_mux
*src
,
558 struct omap_mux
*dst
)
562 for (i
= 0; i
< OMAP_MUX_NR_MODES
; i
++) {
563 if (src
->muxnames
[i
]) {
565 kmalloc(strlen(src
->muxnames
[i
]) + 1,
567 if (!dst
->muxnames
[i
])
569 strcpy(dst
->muxnames
[i
], src
->muxnames
[i
]);
573 #ifdef CONFIG_DEBUG_FS
574 for (i
= 0; i
< OMAP_MUX_NR_SIDES
; i
++) {
577 kmalloc(strlen(src
->balls
[i
]) + 1,
581 strcpy(dst
->balls
[i
], src
->balls
[i
]);
589 omap_mux_free_names(dst
);
594 #endif /* CONFIG_OMAP_MUX */
596 static u16
omap_mux_get_by_gpio(int gpio
)
598 struct omap_mux_entry
*e
;
599 u16 offset
= OMAP_MUX_TERMINATOR
;
601 list_for_each_entry(e
, &muxmodes
, node
) {
602 struct omap_mux
*m
= &e
->mux
;
603 if (m
->gpio
== gpio
) {
604 offset
= m
->reg_offset
;
612 /* Needed for dynamic muxing of GPIO pins for off-idle */
613 u16
omap_mux_get_gpio(int gpio
)
617 offset
= omap_mux_get_by_gpio(gpio
);
618 if (offset
== OMAP_MUX_TERMINATOR
) {
619 printk(KERN_ERR
"mux: Could not get gpio%i\n", gpio
);
623 return omap_mux_read(offset
);
626 /* Needed for dynamic muxing of GPIO pins for off-idle */
627 void omap_mux_set_gpio(u16 val
, int gpio
)
631 offset
= omap_mux_get_by_gpio(gpio
);
632 if (offset
== OMAP_MUX_TERMINATOR
) {
633 printk(KERN_ERR
"mux: Could not set gpio%i\n", gpio
);
637 omap_mux_write(val
, offset
);
640 static struct omap_mux
* __init
omap_mux_list_add(struct omap_mux
*src
)
642 struct omap_mux_entry
*entry
;
645 entry
= kzalloc(sizeof(struct omap_mux_entry
), GFP_KERNEL
);
650 memcpy(m
, src
, sizeof(struct omap_mux_entry
));
652 #ifdef CONFIG_OMAP_MUX
653 if (omap_mux_copy_names(src
, m
)) {
659 mutex_lock(&muxmode_mutex
);
660 list_add_tail(&entry
->node
, &muxmodes
);
661 mutex_unlock(&muxmode_mutex
);
667 * Note if CONFIG_OMAP_MUX is not selected, we will only initialize
668 * the GPIO to mux offset mapping that is needed for dynamic muxing
669 * of GPIO pins for off-idle.
671 static void __init
omap_mux_init_list(struct omap_mux
*superset
)
673 while (superset
->reg_offset
!= OMAP_MUX_TERMINATOR
) {
674 struct omap_mux
*entry
;
676 #ifdef CONFIG_OMAP_MUX
677 if (!superset
->muxnames
|| !superset
->muxnames
[0]) {
682 /* Skip pins that are not muxed as GPIO by bootloader */
683 if (!OMAP_MODE_GPIO(omap_mux_read(superset
->reg_offset
))) {
689 entry
= omap_mux_list_add(superset
);
691 printk(KERN_ERR
"mux: Could not add entry\n");
698 #ifdef CONFIG_OMAP_MUX
700 static void omap_mux_init_package(struct omap_mux
*superset
,
701 struct omap_mux
*package_subset
,
702 struct omap_ball
*package_balls
)
705 omap_mux_package_fixup(package_subset
, superset
);
707 omap_mux_package_init_balls(package_balls
, superset
);
710 static void omap_mux_init_signals(struct omap_board_mux
*board_mux
)
712 omap_mux_set_cmdline_signals();
713 omap_mux_write_array(board_mux
);
718 static void omap_mux_init_package(struct omap_mux
*superset
,
719 struct omap_mux
*package_subset
,
720 struct omap_ball
*package_balls
)
724 static void omap_mux_init_signals(struct omap_board_mux
*board_mux
)
730 int __init
omap_mux_init(u32 mux_pbase
, u32 mux_size
,
731 struct omap_mux
*superset
,
732 struct omap_mux
*package_subset
,
733 struct omap_board_mux
*board_mux
,
734 struct omap_ball
*package_balls
)
739 mux_phys
= mux_pbase
;
740 mux_base
= ioremap(mux_pbase
, mux_size
);
742 printk(KERN_ERR
"mux: Could not ioremap\n");
746 if (cpu_is_omap24xx())
747 omap_mux_flags
= MUXABLE_GPIO_MODE3
;
749 omap_mux_init_package(superset
, package_subset
, package_balls
);
750 omap_mux_init_list(superset
);
751 omap_mux_init_signals(board_mux
);