ARM: cpu topology: Add debugfs interface for cpu_power
[cmplus.git] / arch / arm / mach-omap2 / mux.c
blobc7d0b21c7f5f7df61f12d4dd58f42c70ed7d9a58
1 /*
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>
28 #include <linux/io.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>
38 #include <plat/omap_hwmod.h>
40 #include "control.h"
41 #include "mux.h"
43 #define OMAP_MUX_BASE_OFFSET 0x30 /* Offset from CTRL_BASE */
44 #define OMAP_MUX_BASE_SZ 0x5ca
46 struct omap_mux_entry {
47 struct omap_mux mux;
48 struct list_head node;
51 static LIST_HEAD(mux_partitions);
52 static DEFINE_MUTEX(muxmode_mutex);
54 struct omap_mux_partition *omap_mux_get(const char *name)
56 struct omap_mux_partition *partition;
58 list_for_each_entry(partition, &mux_partitions, node) {
59 if (!strcmp(name, partition->name))
60 return partition;
63 return NULL;
66 u16 omap_mux_read(struct omap_mux_partition *partition, u16 reg)
68 if (partition->flags & OMAP_MUX_REG_8BIT)
69 return __raw_readb(partition->base + reg);
70 else
71 return __raw_readw(partition->base + reg);
74 void omap_mux_write(struct omap_mux_partition *partition, u16 val,
75 u16 reg)
77 if (partition->flags & OMAP_MUX_REG_8BIT)
78 __raw_writeb(val, partition->base + reg);
79 else
80 __raw_writew(val, partition->base + reg);
83 void omap_mux_write_array(struct omap_mux_partition *partition,
84 struct omap_board_mux *board_mux)
86 if (!board_mux)
87 return;
89 while (board_mux->reg_offset != OMAP_MUX_TERMINATOR) {
90 omap_mux_write(partition, board_mux->value,
91 board_mux->reg_offset);
92 board_mux++;
96 #ifdef CONFIG_OMAP_MUX
98 static char *omap_mux_options;
100 static int __init _omap_mux_init_gpio(struct omap_mux_partition *partition,
101 int gpio, int val)
103 struct omap_mux_entry *e;
104 struct omap_mux *gpio_mux = NULL;
105 u16 old_mode;
106 u16 mux_mode;
107 int found = 0;
108 struct list_head *muxmodes = &partition->muxmodes;
110 if (!gpio)
111 return -EINVAL;
113 list_for_each_entry(e, muxmodes, node) {
114 struct omap_mux *m = &e->mux;
115 if (gpio == m->gpio) {
116 gpio_mux = m;
117 found++;
121 if (found == 0)
122 return -ENODEV;
124 if (found > 1) {
125 pr_info("%s: Multiple gpio paths (%d) for gpio%i\n", __func__,
126 found, gpio);
127 return -EINVAL;
130 old_mode = omap_mux_read(partition, gpio_mux->reg_offset);
131 mux_mode = val & ~(OMAP_MUX_NR_MODES - 1);
132 if (partition->flags & OMAP_MUX_GPIO_IN_MODE3)
133 mux_mode |= OMAP_MUX_MODE3;
134 else
135 mux_mode |= OMAP_MUX_MODE4;
136 pr_debug("%s: Setting signal %s.gpio%i 0x%04x -> 0x%04x\n", __func__,
137 gpio_mux->muxnames[0], gpio, old_mode, mux_mode);
138 omap_mux_write(partition, mux_mode, gpio_mux->reg_offset);
140 return 0;
143 int __init omap_mux_init_gpio(int gpio, int val)
145 struct omap_mux_partition *partition;
146 int ret;
148 list_for_each_entry(partition, &mux_partitions, node) {
149 ret = _omap_mux_init_gpio(partition, gpio, val);
150 if (!ret)
151 return ret;
154 pr_err("%s: Could not set gpio%i\n", __func__, gpio);
156 return -ENODEV;
159 static int __init _omap_mux_get_by_name(struct omap_mux_partition *partition,
160 const char *muxname,
161 struct omap_mux **found_mux)
163 struct omap_mux *mux = NULL;
164 struct omap_mux_entry *e;
165 const char *mode_name;
166 int found = 0, found_mode = 0, mode0_len = 0;
167 struct list_head *muxmodes = &partition->muxmodes;
169 mode_name = strchr(muxname, '.');
170 if (mode_name) {
171 mode0_len = strlen(muxname) - strlen(mode_name);
172 mode_name++;
173 } else {
174 mode_name = muxname;
177 list_for_each_entry(e, muxmodes, node) {
178 char *m0_entry;
179 int i;
181 mux = &e->mux;
182 m0_entry = mux->muxnames[0];
184 /* First check for full name in mode0.muxmode format */
185 if (mode0_len && strncmp(muxname, m0_entry, mode0_len))
186 continue;
188 /* Then check for muxmode only */
189 for (i = 0; i < OMAP_MUX_NR_MODES; i++) {
190 char *mode_cur = mux->muxnames[i];
192 if (!mode_cur)
193 continue;
195 if (!strcmp(mode_name, mode_cur)) {
196 *found_mux = mux;
197 found++;
198 found_mode = i;
203 if (found == 1) {
204 return found_mode;
207 if (found > 1) {
208 pr_err("%s: Multiple signal paths (%i) for %s\n", __func__,
209 found, muxname);
210 return -EINVAL;
213 return -ENODEV;
216 static int __init
217 omap_mux_get_by_name(const char *muxname,
218 struct omap_mux_partition **found_partition,
219 struct omap_mux **found_mux)
221 struct omap_mux_partition *partition;
223 list_for_each_entry(partition, &mux_partitions, node) {
224 struct omap_mux *mux = NULL;
225 int mux_mode = _omap_mux_get_by_name(partition, muxname, &mux);
226 if (mux_mode < 0)
227 continue;
229 *found_partition = partition;
230 *found_mux = mux;
232 return mux_mode;
235 pr_err("%s: Could not find signal %s\n", __func__, muxname);
237 return -ENODEV;
240 int __init omap_mux_init_signal(const char *muxname, int val)
242 struct omap_mux_partition *partition = NULL;
243 struct omap_mux *mux = NULL;
244 u16 old_mode;
245 int mux_mode;
247 mux_mode = omap_mux_get_by_name(muxname, &partition, &mux);
248 if (mux_mode < 0)
249 return mux_mode;
251 old_mode = omap_mux_read(partition, mux->reg_offset);
252 mux_mode |= val;
253 pr_debug("%s: Setting signal %s 0x%04x -> 0x%04x\n",
254 __func__, muxname, old_mode, mux_mode);
255 omap_mux_write(partition, mux_mode, mux->reg_offset);
257 return 0;
260 struct omap_hwmod_mux_info * __init
261 omap_hwmod_mux_init(struct omap_device_pad *bpads, int nr_pads)
263 struct omap_hwmod_mux_info *hmux;
264 int i, nr_pads_dynamic = 0;
266 if (!bpads || nr_pads < 1)
267 return NULL;
269 hmux = kzalloc(sizeof(struct omap_hwmod_mux_info), GFP_KERNEL);
270 if (!hmux)
271 goto err1;
273 hmux->nr_pads = nr_pads;
275 hmux->pads = kzalloc(sizeof(struct omap_device_pad) *
276 nr_pads, GFP_KERNEL);
277 if (!hmux->pads)
278 goto err2;
280 for (i = 0; i < hmux->nr_pads; i++) {
281 struct omap_mux_partition *partition;
282 struct omap_device_pad *bpad = &bpads[i], *pad = &hmux->pads[i];
283 struct omap_mux *mux;
284 int mux_mode;
286 mux_mode = omap_mux_get_by_name(bpad->name, &partition, &mux);
287 if (mux_mode < 0)
288 goto err3;
289 if (!pad->partition)
290 pad->partition = partition;
291 if (!pad->mux)
292 pad->mux = mux;
294 pad->name = kzalloc(strlen(bpad->name) + 1, GFP_KERNEL);
295 if (!pad->name) {
296 int j;
298 for (j = i - 1; j >= 0; j--)
299 kfree(hmux->pads[j].name);
300 goto err3;
302 strcpy(pad->name, bpad->name);
304 pad->flags = bpad->flags;
305 pad->enable = bpad->enable;
306 pad->idle = bpad->idle;
307 pad->off = bpad->off;
309 if (pad->flags & OMAP_DEVICE_PAD_REMUX)
310 nr_pads_dynamic++;
312 pr_debug("%s: Initialized %s\n", __func__, pad->name);
315 if (!nr_pads_dynamic)
316 return hmux;
319 * Add pads that need dynamic muxing into a separate list
322 hmux->nr_pads_dynamic = nr_pads_dynamic;
323 hmux->pads_dynamic = kzalloc(sizeof(struct omap_device_pad *) *
324 nr_pads_dynamic, GFP_KERNEL);
325 if (!hmux->pads_dynamic) {
326 pr_err("%s: Could not allocate dynamic pads\n", __func__);
327 return hmux;
330 nr_pads_dynamic = 0;
331 for (i = 0; i < hmux->nr_pads; i++) {
332 struct omap_device_pad *pad = &hmux->pads[i];
334 if (pad->flags & OMAP_DEVICE_PAD_REMUX) {
335 pr_debug("%s: pad %s tagged dynamic\n",
336 __func__, pad->name);
337 hmux->pads_dynamic[nr_pads_dynamic] = pad;
338 nr_pads_dynamic++;
342 return hmux;
344 err3:
345 kfree(hmux->pads);
346 err2:
347 kfree(hmux);
348 err1:
349 pr_err("%s: Could not allocate device mux entry\n", __func__);
351 return NULL;
355 * omap_hwmod_mux_get_wake_status - omap hwmod check pad wakeup
356 * @hmux: Pads for a hwmod
358 * Gets the wakeup status of given pad from omap-hwmod.
359 * Returns true if wakeup event is set for pad else false
360 * if wakeup is not occured or pads are not avialable.
362 int omap_hwmod_mux_get_wake_status(struct omap_hwmod_mux_info *hmux)
364 int i;
365 unsigned int val;
366 u8 ret = false;
368 for (i = 0; i < hmux->nr_pads; i++) {
369 struct omap_device_pad *pad = &hmux->pads[i];
371 if (pad->flags & OMAP_DEVICE_PAD_WAKEUP) {
372 val = omap_mux_read(pad->partition,
373 pad->mux->reg_offset);
374 if (val & OMAP_WAKEUP_EVENT) {
375 ret = true;
376 break;
381 return ret;
384 /* Assumes the calling function takes care of locking */
385 void omap_hwmod_mux(struct omap_hwmod_mux_info *hmux, u8 state)
387 int i;
389 /* Runtime idling of dynamic pads */
390 if (state == _HWMOD_STATE_IDLE && hmux->enabled) {
391 for (i = 0; i < hmux->nr_pads_dynamic; i++) {
392 struct omap_device_pad *pad = hmux->pads_dynamic[i];
393 int val = -EINVAL;
395 val = pad->idle;
396 omap_mux_write(pad->partition, val,
397 pad->mux->reg_offset);
400 return;
403 /* Runtime enabling of dynamic pads */
404 if ((state == _HWMOD_STATE_ENABLED) && hmux->pads_dynamic
405 && hmux->enabled) {
406 for (i = 0; i < hmux->nr_pads_dynamic; i++) {
407 struct omap_device_pad *pad = hmux->pads_dynamic[i];
408 int val = -EINVAL;
410 val = pad->enable;
411 omap_mux_write(pad->partition, val,
412 pad->mux->reg_offset);
415 return;
418 /* Enabling or disabling of all pads */
419 for (i = 0; i < hmux->nr_pads; i++) {
420 struct omap_device_pad *pad = &hmux->pads[i];
421 int flags, val = -EINVAL;
423 flags = pad->flags;
425 switch (state) {
426 case _HWMOD_STATE_ENABLED:
427 val = pad->enable;
428 pr_debug("%s: Enabling %s %x\n", __func__,
429 pad->name, val);
430 break;
431 case _HWMOD_STATE_DISABLED:
432 /* Use safe mode unless OMAP_DEVICE_PAD_REMUX */
433 if (flags & OMAP_DEVICE_PAD_REMUX)
434 val = pad->off;
435 else
436 val = OMAP_MUX_MODE7;
437 pr_debug("%s: Disabling %s %x\n", __func__,
438 pad->name, val);
439 break;
440 default:
441 /* Nothing to be done */
442 break;
445 if (val >= 0) {
446 omap_mux_write(pad->partition, val,
447 pad->mux->reg_offset);
448 pad->flags = flags;
452 if (state == _HWMOD_STATE_ENABLED)
453 hmux->enabled = true;
454 else
455 hmux->enabled = false;
458 #ifdef CONFIG_DEBUG_FS
460 #define OMAP_MUX_MAX_NR_FLAGS 10
461 #define OMAP_MUX_TEST_FLAG(val, mask) \
462 if (((val) & (mask)) == (mask)) { \
463 i++; \
464 flags[i] = #mask; \
467 /* REVISIT: Add checking for non-optimal mux settings */
468 static inline void omap_mux_decode(struct seq_file *s, u16 val)
470 char *flags[OMAP_MUX_MAX_NR_FLAGS];
471 char mode[sizeof("OMAP_MUX_MODE") + 1];
472 int i = -1;
474 sprintf(mode, "OMAP_MUX_MODE%d", val & 0x7);
475 i++;
476 flags[i] = mode;
478 OMAP_MUX_TEST_FLAG(val, OMAP_PIN_OFF_WAKEUPENABLE);
479 if (val & OMAP_OFF_EN) {
480 if (!(val & OMAP_OFFOUT_EN)) {
481 if (!(val & OMAP_OFF_PULL_UP)) {
482 OMAP_MUX_TEST_FLAG(val,
483 OMAP_PIN_OFF_INPUT_PULLDOWN);
484 } else {
485 OMAP_MUX_TEST_FLAG(val,
486 OMAP_PIN_OFF_INPUT_PULLUP);
488 } else {
489 if (!(val & OMAP_OFFOUT_VAL)) {
490 OMAP_MUX_TEST_FLAG(val,
491 OMAP_PIN_OFF_OUTPUT_LOW);
492 } else {
493 OMAP_MUX_TEST_FLAG(val,
494 OMAP_PIN_OFF_OUTPUT_HIGH);
499 if (val & OMAP_INPUT_EN) {
500 if (val & OMAP_PULL_ENA) {
501 if (!(val & OMAP_PULL_UP)) {
502 OMAP_MUX_TEST_FLAG(val,
503 OMAP_PIN_INPUT_PULLDOWN);
504 } else {
505 OMAP_MUX_TEST_FLAG(val, OMAP_PIN_INPUT_PULLUP);
507 } else {
508 OMAP_MUX_TEST_FLAG(val, OMAP_PIN_INPUT);
510 } else {
511 i++;
512 flags[i] = "OMAP_PIN_OUTPUT";
515 do {
516 seq_printf(s, "%s", flags[i]);
517 if (i > 0)
518 seq_printf(s, " | ");
519 } while (i-- > 0);
522 #define OMAP_MUX_DEFNAME_LEN 32
524 static int omap_mux_dbg_board_show(struct seq_file *s, void *unused)
526 struct omap_mux_partition *partition = s->private;
527 struct omap_mux_entry *e;
528 u8 omap_gen = omap_rev() >> 28;
530 list_for_each_entry(e, &partition->muxmodes, node) {
531 struct omap_mux *m = &e->mux;
532 char m0_def[OMAP_MUX_DEFNAME_LEN];
533 char *m0_name = m->muxnames[0];
534 u16 val;
535 int i, mode;
537 if (!m0_name)
538 continue;
540 /* REVISIT: Needs to be updated if mode0 names get longer */
541 for (i = 0; i < OMAP_MUX_DEFNAME_LEN; i++) {
542 if (m0_name[i] == '\0') {
543 m0_def[i] = m0_name[i];
544 break;
546 m0_def[i] = toupper(m0_name[i]);
548 val = omap_mux_read(partition, m->reg_offset);
549 mode = val & OMAP_MUX_MODE7;
550 if (mode != 0)
551 seq_printf(s, "/* %s */\n", m->muxnames[mode]);
554 * XXX: Might be revisited to support differences across
555 * same OMAP generation.
557 seq_printf(s, "OMAP%d_MUX(%s, ", omap_gen, m0_def);
558 omap_mux_decode(s, val);
559 seq_printf(s, "),\n");
562 return 0;
565 static int omap_mux_dbg_board_open(struct inode *inode, struct file *file)
567 return single_open(file, omap_mux_dbg_board_show, inode->i_private);
570 static const struct file_operations omap_mux_dbg_board_fops = {
571 .open = omap_mux_dbg_board_open,
572 .read = seq_read,
573 .llseek = seq_lseek,
574 .release = single_release,
577 static struct omap_mux_partition *omap_mux_get_partition(struct omap_mux *mux)
579 struct omap_mux_partition *partition;
581 list_for_each_entry(partition, &mux_partitions, node) {
582 struct list_head *muxmodes = &partition->muxmodes;
583 struct omap_mux_entry *e;
585 list_for_each_entry(e, muxmodes, node) {
586 struct omap_mux *m = &e->mux;
588 if (m == mux)
589 return partition;
593 return NULL;
596 static int omap_mux_dbg_signal_show(struct seq_file *s, void *unused)
598 struct omap_mux *m = s->private;
599 struct omap_mux_partition *partition;
600 const char *none = "NA";
601 u16 val;
602 int mode;
604 partition = omap_mux_get_partition(m);
605 if (!partition)
606 return 0;
608 val = omap_mux_read(partition, m->reg_offset);
609 mode = val & OMAP_MUX_MODE7;
611 seq_printf(s, "name: %s.%s (0x%08x/0x%03x = 0x%04x), b %s, t %s\n",
612 m->muxnames[0], m->muxnames[mode],
613 partition->phys + m->reg_offset, m->reg_offset, val,
614 m->balls[0] ? m->balls[0] : none,
615 m->balls[1] ? m->balls[1] : none);
616 seq_printf(s, "mode: ");
617 omap_mux_decode(s, val);
618 seq_printf(s, "\n");
619 seq_printf(s, "signals: %s | %s | %s | %s | %s | %s | %s | %s\n",
620 m->muxnames[0] ? m->muxnames[0] : none,
621 m->muxnames[1] ? m->muxnames[1] : none,
622 m->muxnames[2] ? m->muxnames[2] : none,
623 m->muxnames[3] ? m->muxnames[3] : none,
624 m->muxnames[4] ? m->muxnames[4] : none,
625 m->muxnames[5] ? m->muxnames[5] : none,
626 m->muxnames[6] ? m->muxnames[6] : none,
627 m->muxnames[7] ? m->muxnames[7] : none);
629 return 0;
632 #define OMAP_MUX_MAX_ARG_CHAR 7
634 static ssize_t omap_mux_dbg_signal_write(struct file *file,
635 const char __user *user_buf,
636 size_t count, loff_t *ppos)
638 char buf[OMAP_MUX_MAX_ARG_CHAR];
639 struct seq_file *seqf;
640 struct omap_mux *m;
641 unsigned long val;
642 int buf_size, ret;
643 struct omap_mux_partition *partition;
645 if (count > OMAP_MUX_MAX_ARG_CHAR)
646 return -EINVAL;
648 memset(buf, 0, sizeof(buf));
649 buf_size = min(count, sizeof(buf) - 1);
651 if (copy_from_user(buf, user_buf, buf_size))
652 return -EFAULT;
654 ret = strict_strtoul(buf, 0x10, &val);
655 if (ret < 0)
656 return ret;
658 if (val > 0xffff)
659 return -EINVAL;
661 seqf = file->private_data;
662 m = seqf->private;
664 partition = omap_mux_get_partition(m);
665 if (!partition)
666 return -ENODEV;
668 omap_mux_write(partition, (u16)val, m->reg_offset);
669 *ppos += count;
671 return count;
674 static int omap_mux_dbg_signal_open(struct inode *inode, struct file *file)
676 return single_open(file, omap_mux_dbg_signal_show, inode->i_private);
679 static const struct file_operations omap_mux_dbg_signal_fops = {
680 .open = omap_mux_dbg_signal_open,
681 .read = seq_read,
682 .write = omap_mux_dbg_signal_write,
683 .llseek = seq_lseek,
684 .release = single_release,
687 static struct dentry *mux_dbg_dir;
689 static void __init omap_mux_dbg_create_entry(
690 struct omap_mux_partition *partition,
691 struct dentry *mux_dbg_dir)
693 struct omap_mux_entry *e;
695 list_for_each_entry(e, &partition->muxmodes, node) {
696 struct omap_mux *m = &e->mux;
698 (void)debugfs_create_file(m->muxnames[0], S_IWUSR, mux_dbg_dir,
699 m, &omap_mux_dbg_signal_fops);
703 static void __init omap_mux_dbg_init(void)
705 struct omap_mux_partition *partition;
706 static struct dentry *mux_dbg_board_dir;
708 mux_dbg_dir = debugfs_create_dir("omap_mux", NULL);
709 if (!mux_dbg_dir)
710 return;
712 mux_dbg_board_dir = debugfs_create_dir("board", mux_dbg_dir);
713 if (!mux_dbg_board_dir)
714 return;
716 list_for_each_entry(partition, &mux_partitions, node) {
717 omap_mux_dbg_create_entry(partition, mux_dbg_dir);
718 (void)debugfs_create_file(partition->name, S_IRUGO,
719 mux_dbg_board_dir, partition,
720 &omap_mux_dbg_board_fops);
724 #else
725 static inline void omap_mux_dbg_init(void)
728 #endif /* CONFIG_DEBUG_FS */
730 static void __init omap_mux_free_names(struct omap_mux *m)
732 int i;
734 for (i = 0; i < OMAP_MUX_NR_MODES; i++)
735 kfree(m->muxnames[i]);
737 #ifdef CONFIG_DEBUG_FS
738 for (i = 0; i < OMAP_MUX_NR_SIDES; i++)
739 kfree(m->balls[i]);
740 #endif
744 /* Free all data except for GPIO pins unless CONFIG_DEBUG_FS is set */
745 static int __init omap_mux_late_init(void)
747 struct omap_mux_partition *partition;
749 list_for_each_entry(partition, &mux_partitions, node) {
750 struct omap_mux_entry *e, *tmp;
751 list_for_each_entry_safe(e, tmp, &partition->muxmodes, node) {
752 struct omap_mux *m = &e->mux;
753 u16 mode = omap_mux_read(partition, m->reg_offset);
755 if (OMAP_MODE_GPIO(mode))
756 continue;
758 #ifndef CONFIG_DEBUG_FS
759 mutex_lock(&muxmode_mutex);
760 list_del(&e->node);
761 mutex_unlock(&muxmode_mutex);
762 omap_mux_free_names(m);
763 kfree(m);
764 #endif
768 omap_mux_dbg_init();
770 return 0;
772 late_initcall(omap_mux_late_init);
774 static void __init omap_mux_package_fixup(struct omap_mux *p,
775 struct omap_mux *superset)
777 while (p->reg_offset != OMAP_MUX_TERMINATOR) {
778 struct omap_mux *s = superset;
779 int found = 0;
781 while (s->reg_offset != OMAP_MUX_TERMINATOR) {
782 if (s->reg_offset == p->reg_offset) {
783 *s = *p;
784 found++;
785 break;
787 s++;
789 if (!found)
790 pr_err("%s: Unknown entry offset 0x%x\n", __func__,
791 p->reg_offset);
792 p++;
796 #ifdef CONFIG_DEBUG_FS
798 static void __init omap_mux_package_init_balls(struct omap_ball *b,
799 struct omap_mux *superset)
801 while (b->reg_offset != OMAP_MUX_TERMINATOR) {
802 struct omap_mux *s = superset;
803 int found = 0;
805 while (s->reg_offset != OMAP_MUX_TERMINATOR) {
806 if (s->reg_offset == b->reg_offset) {
807 s->balls[0] = b->balls[0];
808 s->balls[1] = b->balls[1];
809 found++;
810 break;
812 s++;
814 if (!found)
815 pr_err("%s: Unknown ball offset 0x%x\n", __func__,
816 b->reg_offset);
817 b++;
821 #else /* CONFIG_DEBUG_FS */
823 static inline void omap_mux_package_init_balls(struct omap_ball *b,
824 struct omap_mux *superset)
828 #endif /* CONFIG_DEBUG_FS */
830 static int __init omap_mux_setup(char *options)
832 if (!options)
833 return 0;
835 omap_mux_options = options;
837 return 1;
839 __setup("omap_mux=", omap_mux_setup);
842 * Note that the omap_mux=some.signal1=0x1234,some.signal2=0x1234
843 * cmdline options only override the bootloader values.
844 * During development, please enable CONFIG_DEBUG_FS, and use the
845 * signal specific entries under debugfs.
847 static void __init omap_mux_set_cmdline_signals(void)
849 char *options, *next_opt, *token;
851 if (!omap_mux_options)
852 return;
854 options = kmalloc(strlen(omap_mux_options) + 1, GFP_KERNEL);
855 if (!options)
856 return;
858 strcpy(options, omap_mux_options);
859 next_opt = options;
861 while ((token = strsep(&next_opt, ",")) != NULL) {
862 char *keyval, *name;
863 unsigned long val;
865 keyval = token;
866 name = strsep(&keyval, "=");
867 if (name) {
868 int res;
870 res = strict_strtoul(keyval, 0x10, &val);
871 if (res < 0)
872 continue;
874 omap_mux_init_signal(name, (u16)val);
878 kfree(options);
881 static int __init omap_mux_copy_names(struct omap_mux *src,
882 struct omap_mux *dst)
884 int i;
886 for (i = 0; i < OMAP_MUX_NR_MODES; i++) {
887 if (src->muxnames[i]) {
888 dst->muxnames[i] =
889 kmalloc(strlen(src->muxnames[i]) + 1,
890 GFP_KERNEL);
891 if (!dst->muxnames[i])
892 goto free;
893 strcpy(dst->muxnames[i], src->muxnames[i]);
897 #ifdef CONFIG_DEBUG_FS
898 for (i = 0; i < OMAP_MUX_NR_SIDES; i++) {
899 if (src->balls[i]) {
900 dst->balls[i] =
901 kmalloc(strlen(src->balls[i]) + 1,
902 GFP_KERNEL);
903 if (!dst->balls[i])
904 goto free;
905 strcpy(dst->balls[i], src->balls[i]);
908 #endif
910 return 0;
912 free:
913 omap_mux_free_names(dst);
914 return -ENOMEM;
918 #endif /* CONFIG_OMAP_MUX */
920 static struct omap_mux *omap_mux_get_by_gpio(
921 struct omap_mux_partition *partition,
922 int gpio)
924 struct omap_mux_entry *e;
925 struct omap_mux *ret = NULL;
927 list_for_each_entry(e, &partition->muxmodes, node) {
928 struct omap_mux *m = &e->mux;
929 if (m->gpio == gpio) {
930 ret = m;
931 break;
935 return ret;
938 /* Needed for dynamic muxing of GPIO pins for off-idle */
939 struct omap_mux *omap_mux_get_gpio(int gpio)
941 struct omap_mux_partition *partition;
942 struct omap_mux *m = NULL;
944 list_for_each_entry(partition, &mux_partitions, node) {
945 m = omap_mux_get_by_gpio(partition, gpio);
946 if (m)
947 return m;
950 return NULL;
953 /* Needed for dynamic muxing of GPIO pins for off-idle */
954 void omap_mux_set_gpio(u16 val, int gpio)
956 struct omap_mux_partition *partition;
957 struct omap_mux *m = NULL;
959 list_for_each_entry(partition, &mux_partitions, node) {
960 m = omap_mux_get_by_gpio(partition, gpio);
961 if (m) {
962 omap_mux_write(partition, val, m->reg_offset);
963 return;
967 if (!m || m->reg_offset == OMAP_MUX_TERMINATOR)
968 pr_err("%s: Could not set gpio%i\n", __func__, gpio);
971 bool omap_mux_get_wakeupevent(struct omap_mux *m)
973 u16 val;
974 if (IS_ERR_OR_NULL(m) || !cpu_is_omap44xx())
975 return false;
977 val = omap_mux_read(m->partition, m->reg_offset);
978 return val & OMAP_WAKEUP_EVENT;
981 /* Has no locking, don't use on a pad that is remuxed (by hwmod or otherwise) */
982 bool omap_mux_get_wakeupenable(struct omap_mux *m)
984 u16 val;
985 if (IS_ERR_OR_NULL(m))
986 return false;
988 val = omap_mux_read(m->partition, m->reg_offset);
989 return val & OMAP_PIN_OFF_WAKEUPENABLE;
992 /* Has no locking, don't use on a pad that is remuxed (by hwmod or otherwise) */
993 int omap_mux_set_wakeupenable(struct omap_mux *m)
995 u16 val;
996 if (IS_ERR_OR_NULL(m))
997 return -EINVAL;
999 val = omap_mux_read(m->partition, m->reg_offset);
1000 val |= OMAP_PIN_OFF_WAKEUPENABLE;
1001 omap_mux_write(m->partition, val, m->reg_offset);
1003 return 0;
1006 /* Has no locking, don't use on a pad that is remuxed (by hwmod or otherwise) */
1007 int omap_mux_clear_wakeupenable(struct omap_mux *m)
1009 u16 val;
1010 if (IS_ERR_OR_NULL(m))
1011 return -EINVAL;
1013 val = omap_mux_read(m->partition, m->reg_offset);
1014 val &= ~OMAP_PIN_OFF_WAKEUPENABLE;
1015 omap_mux_write(m->partition, val, m->reg_offset);
1017 return 0;
1020 static struct omap_mux * __init omap_mux_list_add(
1021 struct omap_mux_partition *partition,
1022 struct omap_mux *src)
1024 struct omap_mux_entry *entry;
1025 struct omap_mux *m;
1027 entry = kzalloc(sizeof(struct omap_mux_entry), GFP_KERNEL);
1028 if (!entry)
1029 return NULL;
1031 m = &entry->mux;
1032 entry->mux = *src;
1033 m->partition = partition;
1035 #ifdef CONFIG_OMAP_MUX
1036 if (omap_mux_copy_names(src, m)) {
1037 kfree(entry);
1038 return NULL;
1040 #endif
1042 mutex_lock(&muxmode_mutex);
1043 list_add_tail(&entry->node, &partition->muxmodes);
1044 mutex_unlock(&muxmode_mutex);
1046 return m;
1050 * Note if CONFIG_OMAP_MUX is not selected, we will only initialize
1051 * the GPIO to mux offset mapping that is needed for dynamic muxing
1052 * of GPIO pins for off-idle.
1054 static void __init omap_mux_init_list(struct omap_mux_partition *partition,
1055 struct omap_mux *superset)
1057 while (superset->reg_offset != OMAP_MUX_TERMINATOR) {
1058 struct omap_mux *entry;
1060 #ifdef CONFIG_OMAP_MUX
1061 if (!superset->muxnames || !superset->muxnames[0]) {
1062 superset++;
1063 continue;
1065 #else
1066 /* Skip pins that are not muxed as GPIO by bootloader */
1067 if (!OMAP_MODE_GPIO(omap_mux_read(partition,
1068 superset->reg_offset))) {
1069 superset++;
1070 continue;
1072 #endif
1074 entry = omap_mux_list_add(partition, superset);
1075 if (!entry) {
1076 pr_err("%s: Could not add entry\n", __func__);
1077 return;
1079 superset++;
1083 #ifdef CONFIG_OMAP_MUX
1085 static void omap_mux_init_package(struct omap_mux *superset,
1086 struct omap_mux *package_subset,
1087 struct omap_ball *package_balls)
1089 if (package_subset)
1090 omap_mux_package_fixup(package_subset, superset);
1091 if (package_balls)
1092 omap_mux_package_init_balls(package_balls, superset);
1095 static void omap_mux_init_signals(struct omap_mux_partition *partition,
1096 struct omap_board_mux *board_mux)
1098 omap_mux_set_cmdline_signals();
1099 omap_mux_write_array(partition, board_mux);
1102 #else
1104 static void omap_mux_init_package(struct omap_mux *superset,
1105 struct omap_mux *package_subset,
1106 struct omap_ball *package_balls)
1110 static void omap_mux_init_signals(struct omap_mux_partition *partition,
1111 struct omap_board_mux *board_mux)
1115 #endif
1117 static u32 mux_partitions_cnt;
1119 int __init omap_mux_init(const char *name, u32 flags,
1120 u32 mux_pbase, u32 mux_size,
1121 struct omap_mux *superset,
1122 struct omap_mux *package_subset,
1123 struct omap_board_mux *board_mux,
1124 struct omap_ball *package_balls)
1126 struct omap_mux_partition *partition;
1128 partition = kzalloc(sizeof(struct omap_mux_partition), GFP_KERNEL);
1129 if (!partition)
1130 return -ENOMEM;
1132 partition->name = name;
1133 partition->flags = flags;
1134 partition->size = mux_size;
1135 partition->phys = mux_pbase;
1136 partition->base = ioremap(mux_pbase, mux_size);
1137 if (!partition->base) {
1138 pr_err("%s: Could not ioremap mux partition at 0x%08x\n",
1139 __func__, partition->phys);
1140 kfree(partition);
1141 return -ENODEV;
1144 INIT_LIST_HEAD(&partition->muxmodes);
1146 list_add_tail(&partition->node, &mux_partitions);
1147 mux_partitions_cnt++;
1148 pr_info("%s: Add partition: #%d: %s, flags: %x\n", __func__,
1149 mux_partitions_cnt, partition->name, partition->flags);
1151 omap_mux_init_package(superset, package_subset, package_balls);
1152 omap_mux_init_list(partition, superset);
1153 omap_mux_init_signals(partition, board_mux);
1155 return 0;