omap2+: Add omap_mux_get_by_name
[pandora-kernel.git] / arch / arm / mach-omap2 / mux.c
1 /*
2  * linux/arch/arm/mach-omap2/mux.c
3  *
4  * OMAP2, OMAP3 and OMAP4 pin multiplexing configurations
5  *
6  * Copyright (C) 2004 - 2010 Texas Instruments Inc.
7  * Copyright (C) 2003 - 2008 Nokia Corporation
8  *
9  * Written by Tony Lindgren
10  *
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.
15  *
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.
20  *
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
24  *
25  */
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>
35
36 #include <asm/system.h>
37
38 #include "control.h"
39 #include "mux.h"
40
41 #define OMAP_MUX_BASE_OFFSET            0x30    /* Offset from CTRL_BASE */
42 #define OMAP_MUX_BASE_SZ                0x5ca
43
44 struct omap_mux_entry {
45         struct omap_mux         mux;
46         struct list_head        node;
47 };
48
49 static LIST_HEAD(mux_partitions);
50 static DEFINE_MUTEX(muxmode_mutex);
51
52 struct omap_mux_partition *omap_mux_get(const char *name)
53 {
54         struct omap_mux_partition *partition;
55
56         list_for_each_entry(partition, &mux_partitions, node) {
57                 if (!strcmp(name, partition->name))
58                         return partition;
59         }
60
61         return NULL;
62 }
63
64 u16 omap_mux_read(struct omap_mux_partition *partition, u16 reg)
65 {
66         if (partition->flags & OMAP_MUX_REG_8BIT)
67                 return __raw_readb(partition->base + reg);
68         else
69                 return __raw_readw(partition->base + reg);
70 }
71
72 void omap_mux_write(struct omap_mux_partition *partition, u16 val,
73                            u16 reg)
74 {
75         if (partition->flags & OMAP_MUX_REG_8BIT)
76                 __raw_writeb(val, partition->base + reg);
77         else
78                 __raw_writew(val, partition->base + reg);
79 }
80
81 void omap_mux_write_array(struct omap_mux_partition *partition,
82                                  struct omap_board_mux *board_mux)
83 {
84         while (board_mux->reg_offset != OMAP_MUX_TERMINATOR) {
85                 omap_mux_write(partition, board_mux->value,
86                                board_mux->reg_offset);
87                 board_mux++;
88         }
89 }
90
91 #ifdef CONFIG_OMAP_MUX
92
93 static char *omap_mux_options;
94
95 static int __init _omap_mux_init_gpio(struct omap_mux_partition *partition,
96                                       int gpio, int val)
97 {
98         struct omap_mux_entry *e;
99         struct omap_mux *gpio_mux = NULL;
100         u16 old_mode;
101         u16 mux_mode;
102         int found = 0;
103         struct list_head *muxmodes = &partition->muxmodes;
104
105         if (!gpio)
106                 return -EINVAL;
107
108         list_for_each_entry(e, muxmodes, node) {
109                 struct omap_mux *m = &e->mux;
110                 if (gpio == m->gpio) {
111                         gpio_mux = m;
112                         found++;
113                 }
114         }
115
116         if (found == 0) {
117                 pr_err("%s: Could not set gpio%i\n", __func__, gpio);
118                 return -ENODEV;
119         }
120
121         if (found > 1) {
122                 pr_info("%s: Multiple gpio paths (%d) for gpio%i\n", __func__,
123                         found, gpio);
124                 return -EINVAL;
125         }
126
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;
131         else
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);
136
137         return 0;
138 }
139
140 int __init omap_mux_init_gpio(int gpio, int val)
141 {
142         struct omap_mux_partition *partition;
143         int ret;
144
145         list_for_each_entry(partition, &mux_partitions, node) {
146                 ret = _omap_mux_init_gpio(partition, gpio, val);
147                 if (!ret)
148                         return ret;
149         }
150
151         return -ENODEV;
152 }
153
154 static int __init _omap_mux_get_by_name(struct omap_mux_partition *partition,
155                                         const char *muxname,
156                                         struct omap_mux **found_mux)
157 {
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;
163
164         mode_name = strchr(muxname, '.');
165         if (mode_name) {
166                 mode0_len = strlen(muxname) - strlen(mode_name);
167                 mode_name++;
168         } else {
169                 mode_name = muxname;
170         }
171
172         list_for_each_entry(e, muxmodes, node) {
173                 char *m0_entry;
174                 int i;
175
176                 mux = &e->mux;
177                 m0_entry = mux->muxnames[0];
178
179                 /* First check for full name in mode0.muxmode format */
180                 if (mode0_len && strncmp(muxname, m0_entry, mode0_len))
181                         continue;
182
183                 /* Then check for muxmode only */
184                 for (i = 0; i < OMAP_MUX_NR_MODES; i++) {
185                         char *mode_cur = mux->muxnames[i];
186
187                         if (!mode_cur)
188                                 continue;
189
190                         if (!strcmp(mode_name, mode_cur)) {
191                                 *found_mux = mux;
192                                 found++;
193                                 found_mode = i;
194                         }
195                 }
196         }
197
198         if (found == 1) {
199                 return found_mode;
200         }
201
202         if (found > 1) {
203                 pr_err("%s: Multiple signal paths (%i) for %s\n", __func__,
204                        found, muxname);
205                 return -EINVAL;
206         }
207
208         pr_err("%s: Could not find signal %s\n", __func__, muxname);
209
210         return -ENODEV;
211 }
212
213 static int __init
214 omap_mux_get_by_name(const char *muxname,
215                         struct omap_mux_partition **found_partition,
216                         struct omap_mux **found_mux)
217 {
218         struct omap_mux_partition *partition;
219
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);
223                 if (mux_mode < 0)
224                         continue;
225
226                 *found_partition = partition;
227                 *found_mux = mux;
228
229                 return mux_mode;
230         }
231
232         return -ENODEV;
233 }
234
235 int __init omap_mux_init_signal(const char *muxname, int val)
236 {
237         struct omap_mux_partition *partition = NULL;
238         struct omap_mux *mux = NULL;
239         u16 old_mode;
240         int mux_mode;
241
242         mux_mode = omap_mux_get_by_name(muxname, &partition, &mux);
243         if (mux_mode < 0)
244                 return mux_mode;
245
246         old_mode = omap_mux_read(partition, mux->reg_offset);
247         mux_mode |= val;
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);
251
252         return 0;
253 }
254
255 #ifdef CONFIG_DEBUG_FS
256
257 #define OMAP_MUX_MAX_NR_FLAGS   10
258 #define OMAP_MUX_TEST_FLAG(val, mask)                           \
259         if (((val) & (mask)) == (mask)) {                       \
260                 i++;                                            \
261                 flags[i] =  #mask;                              \
262         }
263
264 /* REVISIT: Add checking for non-optimal mux settings */
265 static inline void omap_mux_decode(struct seq_file *s, u16 val)
266 {
267         char *flags[OMAP_MUX_MAX_NR_FLAGS];
268         char mode[sizeof("OMAP_MUX_MODE") + 1];
269         int i = -1;
270
271         sprintf(mode, "OMAP_MUX_MODE%d", val & 0x7);
272         i++;
273         flags[i] = mode;
274
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);
281                         } else {
282                                 OMAP_MUX_TEST_FLAG(val,
283                                         OMAP_PIN_OFF_INPUT_PULLUP);
284                         }
285                 } else {
286                         if (!(val & OMAP_OFFOUT_VAL)) {
287                                 OMAP_MUX_TEST_FLAG(val,
288                                         OMAP_PIN_OFF_OUTPUT_LOW);
289                         } else {
290                                 OMAP_MUX_TEST_FLAG(val,
291                                         OMAP_PIN_OFF_OUTPUT_HIGH);
292                         }
293                 }
294         }
295
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);
301                         } else {
302                                 OMAP_MUX_TEST_FLAG(val, OMAP_PIN_INPUT_PULLUP);
303                         }
304                 } else {
305                         OMAP_MUX_TEST_FLAG(val, OMAP_PIN_INPUT);
306                 }
307         } else {
308                 i++;
309                 flags[i] = "OMAP_PIN_OUTPUT";
310         }
311
312         do {
313                 seq_printf(s, "%s", flags[i]);
314                 if (i > 0)
315                         seq_printf(s, " | ");
316         } while (i-- > 0);
317 }
318
319 #define OMAP_MUX_DEFNAME_LEN    32
320
321 static int omap_mux_dbg_board_show(struct seq_file *s, void *unused)
322 {
323         struct omap_mux_partition *partition = s->private;
324         struct omap_mux_entry *e;
325         u8 omap_gen = omap_rev() >> 28;
326
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];
331                 u16 val;
332                 int i, mode;
333
334                 if (!m0_name)
335                         continue;
336
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];
341                                 break;
342                         }
343                         m0_def[i] = toupper(m0_name[i]);
344                 }
345                 val = omap_mux_read(partition, m->reg_offset);
346                 mode = val & OMAP_MUX_MODE7;
347                 if (mode != 0)
348                         seq_printf(s, "/* %s */\n", m->muxnames[mode]);
349
350                 /*
351                  * XXX: Might be revisited to support differences accross
352                  * same OMAP generation.
353                  */
354                 seq_printf(s, "OMAP%d_MUX(%s, ", omap_gen, m0_def);
355                 omap_mux_decode(s, val);
356                 seq_printf(s, "),\n");
357         }
358
359         return 0;
360 }
361
362 static int omap_mux_dbg_board_open(struct inode *inode, struct file *file)
363 {
364         return single_open(file, omap_mux_dbg_board_show, inode->i_private);
365 }
366
367 static const struct file_operations omap_mux_dbg_board_fops = {
368         .open           = omap_mux_dbg_board_open,
369         .read           = seq_read,
370         .llseek         = seq_lseek,
371         .release        = single_release,
372 };
373
374 static struct omap_mux_partition *omap_mux_get_partition(struct omap_mux *mux)
375 {
376         struct omap_mux_partition *partition;
377
378         list_for_each_entry(partition, &mux_partitions, node) {
379                 struct list_head *muxmodes = &partition->muxmodes;
380                 struct omap_mux_entry *e;
381
382                 list_for_each_entry(e, muxmodes, node) {
383                         struct omap_mux *m = &e->mux;
384
385                         if (m == mux)
386                                 return partition;
387                 }
388         }
389
390         return NULL;
391 }
392
393 static int omap_mux_dbg_signal_show(struct seq_file *s, void *unused)
394 {
395         struct omap_mux *m = s->private;
396         struct omap_mux_partition *partition;
397         const char *none = "NA";
398         u16 val;
399         int mode;
400
401         partition = omap_mux_get_partition(m);
402         if (!partition)
403                 return 0;
404
405         val = omap_mux_read(partition, m->reg_offset);
406         mode = val & OMAP_MUX_MODE7;
407
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);
415         seq_printf(s, "\n");
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);
425
426         return 0;
427 }
428
429 #define OMAP_MUX_MAX_ARG_CHAR  7
430
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)
434 {
435         char buf[OMAP_MUX_MAX_ARG_CHAR];
436         struct seq_file *seqf;
437         struct omap_mux *m;
438         unsigned long val;
439         int buf_size, ret;
440         struct omap_mux_partition *partition;
441
442         if (count > OMAP_MUX_MAX_ARG_CHAR)
443                 return -EINVAL;
444
445         memset(buf, 0, sizeof(buf));
446         buf_size = min(count, sizeof(buf) - 1);
447
448         if (copy_from_user(buf, user_buf, buf_size))
449                 return -EFAULT;
450
451         ret = strict_strtoul(buf, 0x10, &val);
452         if (ret < 0)
453                 return ret;
454
455         if (val > 0xffff)
456                 return -EINVAL;
457
458         seqf = file->private_data;
459         m = seqf->private;
460
461         partition = omap_mux_get_partition(m);
462         if (!partition)
463                 return -ENODEV;
464
465         omap_mux_write(partition, (u16)val, m->reg_offset);
466         *ppos += count;
467
468         return count;
469 }
470
471 static int omap_mux_dbg_signal_open(struct inode *inode, struct file *file)
472 {
473         return single_open(file, omap_mux_dbg_signal_show, inode->i_private);
474 }
475
476 static const struct file_operations omap_mux_dbg_signal_fops = {
477         .open           = omap_mux_dbg_signal_open,
478         .read           = seq_read,
479         .write          = omap_mux_dbg_signal_write,
480         .llseek         = seq_lseek,
481         .release        = single_release,
482 };
483
484 static struct dentry *mux_dbg_dir;
485
486 static void __init omap_mux_dbg_create_entry(
487                                 struct omap_mux_partition *partition,
488                                 struct dentry *mux_dbg_dir)
489 {
490         struct omap_mux_entry *e;
491
492         list_for_each_entry(e, &partition->muxmodes, node) {
493                 struct omap_mux *m = &e->mux;
494
495                 (void)debugfs_create_file(m->muxnames[0], S_IWUGO, mux_dbg_dir,
496                                           m, &omap_mux_dbg_signal_fops);
497         }
498 }
499
500 static void __init omap_mux_dbg_init(void)
501 {
502         struct omap_mux_partition *partition;
503         static struct dentry *mux_dbg_board_dir;
504
505         mux_dbg_dir = debugfs_create_dir("omap_mux", NULL);
506         if (!mux_dbg_dir)
507                 return;
508
509         mux_dbg_board_dir = debugfs_create_dir("board", mux_dbg_dir);
510         if (!mux_dbg_board_dir)
511                 return;
512
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);
518         }
519 }
520
521 #else
522 static inline void omap_mux_dbg_init(void)
523 {
524 }
525 #endif  /* CONFIG_DEBUG_FS */
526
527 static void __init omap_mux_free_names(struct omap_mux *m)
528 {
529         int i;
530
531         for (i = 0; i < OMAP_MUX_NR_MODES; i++)
532                 kfree(m->muxnames[i]);
533
534 #ifdef CONFIG_DEBUG_FS
535         for (i = 0; i < OMAP_MUX_NR_SIDES; i++)
536                 kfree(m->balls[i]);
537 #endif
538
539 }
540
541 /* Free all data except for GPIO pins unless CONFIG_DEBUG_FS is set */
542 static int __init omap_mux_late_init(void)
543 {
544         struct omap_mux_partition *partition;
545
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);
551
552                         if (OMAP_MODE_GPIO(mode))
553                                 continue;
554
555 #ifndef CONFIG_DEBUG_FS
556                         mutex_lock(&muxmode_mutex);
557                         list_del(&e->node);
558                         mutex_unlock(&muxmode_mutex);
559                         omap_mux_free_names(m);
560                         kfree(m);
561 #endif
562                 }
563         }
564
565         omap_mux_dbg_init();
566
567         return 0;
568 }
569 late_initcall(omap_mux_late_init);
570
571 static void __init omap_mux_package_fixup(struct omap_mux *p,
572                                         struct omap_mux *superset)
573 {
574         while (p->reg_offset !=  OMAP_MUX_TERMINATOR) {
575                 struct omap_mux *s = superset;
576                 int found = 0;
577
578                 while (s->reg_offset != OMAP_MUX_TERMINATOR) {
579                         if (s->reg_offset == p->reg_offset) {
580                                 *s = *p;
581                                 found++;
582                                 break;
583                         }
584                         s++;
585                 }
586                 if (!found)
587                         pr_err("%s: Unknown entry offset 0x%x\n", __func__,
588                                p->reg_offset);
589                 p++;
590         }
591 }
592
593 #ifdef CONFIG_DEBUG_FS
594
595 static void __init omap_mux_package_init_balls(struct omap_ball *b,
596                                 struct omap_mux *superset)
597 {
598         while (b->reg_offset != OMAP_MUX_TERMINATOR) {
599                 struct omap_mux *s = superset;
600                 int found = 0;
601
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];
606                                 found++;
607                                 break;
608                         }
609                         s++;
610                 }
611                 if (!found)
612                         pr_err("%s: Unknown ball offset 0x%x\n", __func__,
613                                b->reg_offset);
614                 b++;
615         }
616 }
617
618 #else   /* CONFIG_DEBUG_FS */
619
620 static inline void omap_mux_package_init_balls(struct omap_ball *b,
621                                         struct omap_mux *superset)
622 {
623 }
624
625 #endif  /* CONFIG_DEBUG_FS */
626
627 static int __init omap_mux_setup(char *options)
628 {
629         if (!options)
630                 return 0;
631
632         omap_mux_options = options;
633
634         return 1;
635 }
636 __setup("omap_mux=", omap_mux_setup);
637
638 /*
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.
643  */
644 static void __init omap_mux_set_cmdline_signals(void)
645 {
646         char *options, *next_opt, *token;
647
648         if (!omap_mux_options)
649                 return;
650
651         options = kmalloc(strlen(omap_mux_options) + 1, GFP_KERNEL);
652         if (!options)
653                 return;
654
655         strcpy(options, omap_mux_options);
656         next_opt = options;
657
658         while ((token = strsep(&next_opt, ",")) != NULL) {
659                 char *keyval, *name;
660                 unsigned long val;
661
662                 keyval = token;
663                 name = strsep(&keyval, "=");
664                 if (name) {
665                         int res;
666
667                         res = strict_strtoul(keyval, 0x10, &val);
668                         if (res < 0)
669                                 continue;
670
671                         omap_mux_init_signal(name, (u16)val);
672                 }
673         }
674
675         kfree(options);
676 }
677
678 static int __init omap_mux_copy_names(struct omap_mux *src,
679                                       struct omap_mux *dst)
680 {
681         int i;
682
683         for (i = 0; i < OMAP_MUX_NR_MODES; i++) {
684                 if (src->muxnames[i]) {
685                         dst->muxnames[i] =
686                                 kmalloc(strlen(src->muxnames[i]) + 1,
687                                         GFP_KERNEL);
688                         if (!dst->muxnames[i])
689                                 goto free;
690                         strcpy(dst->muxnames[i], src->muxnames[i]);
691                 }
692         }
693
694 #ifdef CONFIG_DEBUG_FS
695         for (i = 0; i < OMAP_MUX_NR_SIDES; i++) {
696                 if (src->balls[i]) {
697                         dst->balls[i] =
698                                 kmalloc(strlen(src->balls[i]) + 1,
699                                         GFP_KERNEL);
700                         if (!dst->balls[i])
701                                 goto free;
702                         strcpy(dst->balls[i], src->balls[i]);
703                 }
704         }
705 #endif
706
707         return 0;
708
709 free:
710         omap_mux_free_names(dst);
711         return -ENOMEM;
712
713 }
714
715 #endif  /* CONFIG_OMAP_MUX */
716
717 static struct omap_mux *omap_mux_get_by_gpio(
718                                 struct omap_mux_partition *partition,
719                                 int gpio)
720 {
721         struct omap_mux_entry *e;
722         struct omap_mux *ret = NULL;
723
724         list_for_each_entry(e, &partition->muxmodes, node) {
725                 struct omap_mux *m = &e->mux;
726                 if (m->gpio == gpio) {
727                         ret = m;
728                         break;
729                 }
730         }
731
732         return ret;
733 }
734
735 /* Needed for dynamic muxing of GPIO pins for off-idle */
736 u16 omap_mux_get_gpio(int gpio)
737 {
738         struct omap_mux_partition *partition;
739         struct omap_mux *m;
740
741         list_for_each_entry(partition, &mux_partitions, node) {
742                 m = omap_mux_get_by_gpio(partition, gpio);
743                 if (m)
744                         return omap_mux_read(partition, m->reg_offset);
745         }
746
747         if (!m || m->reg_offset == OMAP_MUX_TERMINATOR)
748                 pr_err("%s: Could not get gpio%i\n", __func__, gpio);
749
750         return OMAP_MUX_TERMINATOR;
751 }
752
753 /* Needed for dynamic muxing of GPIO pins for off-idle */
754 void omap_mux_set_gpio(u16 val, int gpio)
755 {
756         struct omap_mux_partition *partition;
757         struct omap_mux *m = NULL;
758
759         list_for_each_entry(partition, &mux_partitions, node) {
760                 m = omap_mux_get_by_gpio(partition, gpio);
761                 if (m) {
762                         omap_mux_write(partition, val, m->reg_offset);
763                         return;
764                 }
765         }
766
767         if (!m || m->reg_offset == OMAP_MUX_TERMINATOR)
768                 pr_err("%s: Could not set gpio%i\n", __func__, gpio);
769 }
770
771 static struct omap_mux * __init omap_mux_list_add(
772                                         struct omap_mux_partition *partition,
773                                         struct omap_mux *src)
774 {
775         struct omap_mux_entry *entry;
776         struct omap_mux *m;
777
778         entry = kzalloc(sizeof(struct omap_mux_entry), GFP_KERNEL);
779         if (!entry)
780                 return NULL;
781
782         m = &entry->mux;
783         memcpy(m, src, sizeof(struct omap_mux_entry));
784
785 #ifdef CONFIG_OMAP_MUX
786         if (omap_mux_copy_names(src, m)) {
787                 kfree(entry);
788                 return NULL;
789         }
790 #endif
791
792         mutex_lock(&muxmode_mutex);
793         list_add_tail(&entry->node, &partition->muxmodes);
794         mutex_unlock(&muxmode_mutex);
795
796         return m;
797 }
798
799 /*
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.
803  */
804 static void __init omap_mux_init_list(struct omap_mux_partition *partition,
805                                       struct omap_mux *superset)
806 {
807         while (superset->reg_offset !=  OMAP_MUX_TERMINATOR) {
808                 struct omap_mux *entry;
809
810 #ifdef CONFIG_OMAP_MUX
811                 if (!superset->muxnames || !superset->muxnames[0]) {
812                         superset++;
813                         continue;
814                 }
815 #else
816                 /* Skip pins that are not muxed as GPIO by bootloader */
817                 if (!OMAP_MODE_GPIO(omap_mux_read(partition,
818                                     superset->reg_offset))) {
819                         superset++;
820                         continue;
821                 }
822 #endif
823
824                 entry = omap_mux_list_add(partition, superset);
825                 if (!entry) {
826                         pr_err("%s: Could not add entry\n", __func__);
827                         return;
828                 }
829                 superset++;
830         }
831 }
832
833 #ifdef CONFIG_OMAP_MUX
834
835 static void omap_mux_init_package(struct omap_mux *superset,
836                                   struct omap_mux *package_subset,
837                                   struct omap_ball *package_balls)
838 {
839         if (package_subset)
840                 omap_mux_package_fixup(package_subset, superset);
841         if (package_balls)
842                 omap_mux_package_init_balls(package_balls, superset);
843 }
844
845 static void omap_mux_init_signals(struct omap_mux_partition *partition,
846                                   struct omap_board_mux *board_mux)
847 {
848         omap_mux_set_cmdline_signals();
849         omap_mux_write_array(partition, board_mux);
850 }
851
852 #else
853
854 static void omap_mux_init_package(struct omap_mux *superset,
855                                   struct omap_mux *package_subset,
856                                   struct omap_ball *package_balls)
857 {
858 }
859
860 static void omap_mux_init_signals(struct omap_mux_partition *partition,
861                                   struct omap_board_mux *board_mux)
862 {
863 }
864
865 #endif
866
867 static u32 mux_partitions_cnt;
868
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)
875 {
876         struct omap_mux_partition *partition;
877
878         partition = kzalloc(sizeof(struct omap_mux_partition), GFP_KERNEL);
879         if (!partition)
880                 return -ENOMEM;
881
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);
890                 return -ENODEV;
891         }
892
893         INIT_LIST_HEAD(&partition->muxmodes);
894
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);
899
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);
903
904         return 0;
905 }
906