OMAP: mux: Add __func__ macro to pr_xxx macros
[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_init_signal(struct omap_mux_partition *partition,
155                                         const char *muxname, int val)
156 {
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;
161
162         mode_name = strchr(muxname, '.');
163         if (mode_name) {
164                 mode0_len = strlen(muxname) - strlen(mode_name);
165                 mode_name++;
166         } else {
167                 mode_name = muxname;
168         }
169
170         list_for_each_entry(e, muxmodes, node) {
171                 struct omap_mux *m = &e->mux;
172                 char *m0_entry = m->muxnames[0];
173                 int i;
174
175                 /* First check for full name in mode0.muxmode format */
176                 if (mode0_len && strncmp(muxname, m0_entry, mode0_len))
177                         continue;
178
179                 /* Then check for muxmode only */
180                 for (i = 0; i < OMAP_MUX_NR_MODES; i++) {
181                         char *mode_cur = m->muxnames[i];
182
183                         if (!mode_cur)
184                                 continue;
185
186                         if (!strcmp(mode_name, mode_cur)) {
187                                 u16 old_mode;
188                                 u16 mux_mode;
189
190                                 old_mode = omap_mux_read(partition,
191                                                          m->reg_offset);
192                                 mux_mode = val | i;
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,
197                                                m->reg_offset);
198                                 found++;
199                         }
200                 }
201         }
202
203         if (found == 1)
204                 return 0;
205
206         if (found > 1) {
207                 pr_err("%s: Multiple signal paths (%i) for %s\n", __func__,
208                        found, muxname);
209                 return -EINVAL;
210         }
211
212         pr_err("%s: Could not set signal %s\n", __func__, muxname);
213
214         return -ENODEV;
215 }
216
217 int __init omap_mux_init_signal(const char *muxname, int val)
218 {
219         struct omap_mux_partition *partition;
220         int ret;
221
222         list_for_each_entry(partition, &mux_partitions, node) {
223                 ret = _omap_mux_init_signal(partition, muxname, val);
224                 if (!ret)
225                         return ret;
226         }
227
228         return -ENODEV;
229
230 }
231
232 #ifdef CONFIG_DEBUG_FS
233
234 #define OMAP_MUX_MAX_NR_FLAGS   10
235 #define OMAP_MUX_TEST_FLAG(val, mask)                           \
236         if (((val) & (mask)) == (mask)) {                       \
237                 i++;                                            \
238                 flags[i] =  #mask;                              \
239         }
240
241 /* REVISIT: Add checking for non-optimal mux settings */
242 static inline void omap_mux_decode(struct seq_file *s, u16 val)
243 {
244         char *flags[OMAP_MUX_MAX_NR_FLAGS];
245         char mode[sizeof("OMAP_MUX_MODE") + 1];
246         int i = -1;
247
248         sprintf(mode, "OMAP_MUX_MODE%d", val & 0x7);
249         i++;
250         flags[i] = mode;
251
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);
258                         } else {
259                                 OMAP_MUX_TEST_FLAG(val,
260                                         OMAP_PIN_OFF_INPUT_PULLUP);
261                         }
262                 } else {
263                         if (!(val & OMAP_OFFOUT_VAL)) {
264                                 OMAP_MUX_TEST_FLAG(val,
265                                         OMAP_PIN_OFF_OUTPUT_LOW);
266                         } else {
267                                 OMAP_MUX_TEST_FLAG(val,
268                                         OMAP_PIN_OFF_OUTPUT_HIGH);
269                         }
270                 }
271         }
272
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);
278                         } else {
279                                 OMAP_MUX_TEST_FLAG(val, OMAP_PIN_INPUT_PULLUP);
280                         }
281                 } else {
282                         OMAP_MUX_TEST_FLAG(val, OMAP_PIN_INPUT);
283                 }
284         } else {
285                 i++;
286                 flags[i] = "OMAP_PIN_OUTPUT";
287         }
288
289         do {
290                 seq_printf(s, "%s", flags[i]);
291                 if (i > 0)
292                         seq_printf(s, " | ");
293         } while (i-- > 0);
294 }
295
296 #define OMAP_MUX_DEFNAME_LEN    32
297
298 static int omap_mux_dbg_board_show(struct seq_file *s, void *unused)
299 {
300         struct omap_mux_partition *partition = s->private;
301         struct omap_mux_entry *e;
302         u8 omap_gen = omap_rev() >> 28;
303
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];
308                 u16 val;
309                 int i, mode;
310
311                 if (!m0_name)
312                         continue;
313
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];
318                                 break;
319                         }
320                         m0_def[i] = toupper(m0_name[i]);
321                 }
322                 val = omap_mux_read(partition, m->reg_offset);
323                 mode = val & OMAP_MUX_MODE7;
324                 if (mode != 0)
325                         seq_printf(s, "/* %s */\n", m->muxnames[mode]);
326
327                 /*
328                  * XXX: Might be revisited to support differences accross
329                  * same OMAP generation.
330                  */
331                 seq_printf(s, "OMAP%d_MUX(%s, ", omap_gen, m0_def);
332                 omap_mux_decode(s, val);
333                 seq_printf(s, "),\n");
334         }
335
336         return 0;
337 }
338
339 static int omap_mux_dbg_board_open(struct inode *inode, struct file *file)
340 {
341         return single_open(file, omap_mux_dbg_board_show, inode->i_private);
342 }
343
344 static const struct file_operations omap_mux_dbg_board_fops = {
345         .open           = omap_mux_dbg_board_open,
346         .read           = seq_read,
347         .llseek         = seq_lseek,
348         .release        = single_release,
349 };
350
351 static struct omap_mux_partition *omap_mux_get_partition(struct omap_mux *mux)
352 {
353         struct omap_mux_partition *partition;
354
355         list_for_each_entry(partition, &mux_partitions, node) {
356                 struct list_head *muxmodes = &partition->muxmodes;
357                 struct omap_mux_entry *e;
358
359                 list_for_each_entry(e, muxmodes, node) {
360                         struct omap_mux *m = &e->mux;
361
362                         if (m == mux)
363                                 return partition;
364                 }
365         }
366
367         return NULL;
368 }
369
370 static int omap_mux_dbg_signal_show(struct seq_file *s, void *unused)
371 {
372         struct omap_mux *m = s->private;
373         struct omap_mux_partition *partition;
374         const char *none = "NA";
375         u16 val;
376         int mode;
377
378         partition = omap_mux_get_partition(m);
379         if (!partition)
380                 return 0;
381
382         val = omap_mux_read(partition, m->reg_offset);
383         mode = val & OMAP_MUX_MODE7;
384
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);
392         seq_printf(s, "\n");
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);
402
403         return 0;
404 }
405
406 #define OMAP_MUX_MAX_ARG_CHAR  7
407
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)
411 {
412         char buf[OMAP_MUX_MAX_ARG_CHAR];
413         struct seq_file *seqf;
414         struct omap_mux *m;
415         unsigned long val;
416         int buf_size, ret;
417         struct omap_mux_partition *partition;
418
419         if (count > OMAP_MUX_MAX_ARG_CHAR)
420                 return -EINVAL;
421
422         memset(buf, 0, sizeof(buf));
423         buf_size = min(count, sizeof(buf) - 1);
424
425         if (copy_from_user(buf, user_buf, buf_size))
426                 return -EFAULT;
427
428         ret = strict_strtoul(buf, 0x10, &val);
429         if (ret < 0)
430                 return ret;
431
432         if (val > 0xffff)
433                 return -EINVAL;
434
435         seqf = file->private_data;
436         m = seqf->private;
437
438         partition = omap_mux_get_partition(m);
439         if (!partition)
440                 return -ENODEV;
441
442         omap_mux_write(partition, (u16)val, m->reg_offset);
443         *ppos += count;
444
445         return count;
446 }
447
448 static int omap_mux_dbg_signal_open(struct inode *inode, struct file *file)
449 {
450         return single_open(file, omap_mux_dbg_signal_show, inode->i_private);
451 }
452
453 static const struct file_operations omap_mux_dbg_signal_fops = {
454         .open           = omap_mux_dbg_signal_open,
455         .read           = seq_read,
456         .write          = omap_mux_dbg_signal_write,
457         .llseek         = seq_lseek,
458         .release        = single_release,
459 };
460
461 static struct dentry *mux_dbg_dir;
462
463 static void __init omap_mux_dbg_create_entry(
464                                 struct omap_mux_partition *partition,
465                                 struct dentry *mux_dbg_dir)
466 {
467         struct omap_mux_entry *e;
468
469         list_for_each_entry(e, &partition->muxmodes, node) {
470                 struct omap_mux *m = &e->mux;
471
472                 (void)debugfs_create_file(m->muxnames[0], S_IWUGO, mux_dbg_dir,
473                                           m, &omap_mux_dbg_signal_fops);
474         }
475 }
476
477 static void __init omap_mux_dbg_init(void)
478 {
479         struct omap_mux_partition *partition;
480         static struct dentry *mux_dbg_board_dir;
481
482         mux_dbg_dir = debugfs_create_dir("omap_mux", NULL);
483         if (!mux_dbg_dir)
484                 return;
485
486         mux_dbg_board_dir = debugfs_create_dir("board", mux_dbg_dir);
487         if (!mux_dbg_board_dir)
488                 return;
489
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);
495         }
496 }
497
498 #else
499 static inline void omap_mux_dbg_init(void)
500 {
501 }
502 #endif  /* CONFIG_DEBUG_FS */
503
504 static void __init omap_mux_free_names(struct omap_mux *m)
505 {
506         int i;
507
508         for (i = 0; i < OMAP_MUX_NR_MODES; i++)
509                 kfree(m->muxnames[i]);
510
511 #ifdef CONFIG_DEBUG_FS
512         for (i = 0; i < OMAP_MUX_NR_SIDES; i++)
513                 kfree(m->balls[i]);
514 #endif
515
516 }
517
518 /* Free all data except for GPIO pins unless CONFIG_DEBUG_FS is set */
519 static int __init omap_mux_late_init(void)
520 {
521         struct omap_mux_partition *partition;
522
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);
528
529                         if (OMAP_MODE_GPIO(mode))
530                                 continue;
531
532 #ifndef CONFIG_DEBUG_FS
533                         mutex_lock(&muxmode_mutex);
534                         list_del(&e->node);
535                         mutex_unlock(&muxmode_mutex);
536                         omap_mux_free_names(m);
537                         kfree(m);
538 #endif
539                 }
540         }
541
542         omap_mux_dbg_init();
543
544         return 0;
545 }
546 late_initcall(omap_mux_late_init);
547
548 static void __init omap_mux_package_fixup(struct omap_mux *p,
549                                         struct omap_mux *superset)
550 {
551         while (p->reg_offset !=  OMAP_MUX_TERMINATOR) {
552                 struct omap_mux *s = superset;
553                 int found = 0;
554
555                 while (s->reg_offset != OMAP_MUX_TERMINATOR) {
556                         if (s->reg_offset == p->reg_offset) {
557                                 *s = *p;
558                                 found++;
559                                 break;
560                         }
561                         s++;
562                 }
563                 if (!found)
564                         pr_err("%s: Unknown entry offset 0x%x\n", __func__,
565                                p->reg_offset);
566                 p++;
567         }
568 }
569
570 #ifdef CONFIG_DEBUG_FS
571
572 static void __init omap_mux_package_init_balls(struct omap_ball *b,
573                                 struct omap_mux *superset)
574 {
575         while (b->reg_offset != OMAP_MUX_TERMINATOR) {
576                 struct omap_mux *s = superset;
577                 int found = 0;
578
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];
583                                 found++;
584                                 break;
585                         }
586                         s++;
587                 }
588                 if (!found)
589                         pr_err("%s: Unknown ball offset 0x%x\n", __func__,
590                                b->reg_offset);
591                 b++;
592         }
593 }
594
595 #else   /* CONFIG_DEBUG_FS */
596
597 static inline void omap_mux_package_init_balls(struct omap_ball *b,
598                                         struct omap_mux *superset)
599 {
600 }
601
602 #endif  /* CONFIG_DEBUG_FS */
603
604 static int __init omap_mux_setup(char *options)
605 {
606         if (!options)
607                 return 0;
608
609         omap_mux_options = options;
610
611         return 1;
612 }
613 __setup("omap_mux=", omap_mux_setup);
614
615 /*
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.
620  */
621 static void __init omap_mux_set_cmdline_signals(void)
622 {
623         char *options, *next_opt, *token;
624
625         if (!omap_mux_options)
626                 return;
627
628         options = kmalloc(strlen(omap_mux_options) + 1, GFP_KERNEL);
629         if (!options)
630                 return;
631
632         strcpy(options, omap_mux_options);
633         next_opt = options;
634
635         while ((token = strsep(&next_opt, ",")) != NULL) {
636                 char *keyval, *name;
637                 unsigned long val;
638
639                 keyval = token;
640                 name = strsep(&keyval, "=");
641                 if (name) {
642                         int res;
643
644                         res = strict_strtoul(keyval, 0x10, &val);
645                         if (res < 0)
646                                 continue;
647
648                         omap_mux_init_signal(name, (u16)val);
649                 }
650         }
651
652         kfree(options);
653 }
654
655 static int __init omap_mux_copy_names(struct omap_mux *src,
656                                       struct omap_mux *dst)
657 {
658         int i;
659
660         for (i = 0; i < OMAP_MUX_NR_MODES; i++) {
661                 if (src->muxnames[i]) {
662                         dst->muxnames[i] =
663                                 kmalloc(strlen(src->muxnames[i]) + 1,
664                                         GFP_KERNEL);
665                         if (!dst->muxnames[i])
666                                 goto free;
667                         strcpy(dst->muxnames[i], src->muxnames[i]);
668                 }
669         }
670
671 #ifdef CONFIG_DEBUG_FS
672         for (i = 0; i < OMAP_MUX_NR_SIDES; i++) {
673                 if (src->balls[i]) {
674                         dst->balls[i] =
675                                 kmalloc(strlen(src->balls[i]) + 1,
676                                         GFP_KERNEL);
677                         if (!dst->balls[i])
678                                 goto free;
679                         strcpy(dst->balls[i], src->balls[i]);
680                 }
681         }
682 #endif
683
684         return 0;
685
686 free:
687         omap_mux_free_names(dst);
688         return -ENOMEM;
689
690 }
691
692 #endif  /* CONFIG_OMAP_MUX */
693
694 static struct omap_mux *omap_mux_get_by_gpio(
695                                 struct omap_mux_partition *partition,
696                                 int gpio)
697 {
698         struct omap_mux_entry *e;
699         struct omap_mux *ret = NULL;
700
701         list_for_each_entry(e, &partition->muxmodes, node) {
702                 struct omap_mux *m = &e->mux;
703                 if (m->gpio == gpio) {
704                         ret = m;
705                         break;
706                 }
707         }
708
709         return ret;
710 }
711
712 /* Needed for dynamic muxing of GPIO pins for off-idle */
713 u16 omap_mux_get_gpio(int gpio)
714 {
715         struct omap_mux_partition *partition;
716         struct omap_mux *m;
717
718         list_for_each_entry(partition, &mux_partitions, node) {
719                 m = omap_mux_get_by_gpio(partition, gpio);
720                 if (m)
721                         return omap_mux_read(partition, m->reg_offset);
722         }
723
724         if (!m || m->reg_offset == OMAP_MUX_TERMINATOR)
725                 pr_err("%s: Could not get gpio%i\n", __func__, gpio);
726
727         return OMAP_MUX_TERMINATOR;
728 }
729
730 /* Needed for dynamic muxing of GPIO pins for off-idle */
731 void omap_mux_set_gpio(u16 val, int gpio)
732 {
733         struct omap_mux_partition *partition;
734         struct omap_mux *m = NULL;
735
736         list_for_each_entry(partition, &mux_partitions, node) {
737                 m = omap_mux_get_by_gpio(partition, gpio);
738                 if (m) {
739                         omap_mux_write(partition, val, m->reg_offset);
740                         return;
741                 }
742         }
743
744         if (!m || m->reg_offset == OMAP_MUX_TERMINATOR)
745                 pr_err("%s: Could not set gpio%i\n", __func__, gpio);
746 }
747
748 static struct omap_mux * __init omap_mux_list_add(
749                                         struct omap_mux_partition *partition,
750                                         struct omap_mux *src)
751 {
752         struct omap_mux_entry *entry;
753         struct omap_mux *m;
754
755         entry = kzalloc(sizeof(struct omap_mux_entry), GFP_KERNEL);
756         if (!entry)
757                 return NULL;
758
759         m = &entry->mux;
760         memcpy(m, src, sizeof(struct omap_mux_entry));
761
762 #ifdef CONFIG_OMAP_MUX
763         if (omap_mux_copy_names(src, m)) {
764                 kfree(entry);
765                 return NULL;
766         }
767 #endif
768
769         mutex_lock(&muxmode_mutex);
770         list_add_tail(&entry->node, &partition->muxmodes);
771         mutex_unlock(&muxmode_mutex);
772
773         return m;
774 }
775
776 /*
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.
780  */
781 static void __init omap_mux_init_list(struct omap_mux_partition *partition,
782                                       struct omap_mux *superset)
783 {
784         while (superset->reg_offset !=  OMAP_MUX_TERMINATOR) {
785                 struct omap_mux *entry;
786
787 #ifdef CONFIG_OMAP_MUX
788                 if (!superset->muxnames || !superset->muxnames[0]) {
789                         superset++;
790                         continue;
791                 }
792 #else
793                 /* Skip pins that are not muxed as GPIO by bootloader */
794                 if (!OMAP_MODE_GPIO(omap_mux_read(partition,
795                                     superset->reg_offset))) {
796                         superset++;
797                         continue;
798                 }
799 #endif
800
801                 entry = omap_mux_list_add(partition, superset);
802                 if (!entry) {
803                         pr_err("%s: Could not add entry\n", __func__);
804                         return;
805                 }
806                 superset++;
807         }
808 }
809
810 #ifdef CONFIG_OMAP_MUX
811
812 static void omap_mux_init_package(struct omap_mux *superset,
813                                   struct omap_mux *package_subset,
814                                   struct omap_ball *package_balls)
815 {
816         if (package_subset)
817                 omap_mux_package_fixup(package_subset, superset);
818         if (package_balls)
819                 omap_mux_package_init_balls(package_balls, superset);
820 }
821
822 static void omap_mux_init_signals(struct omap_mux_partition *partition,
823                                   struct omap_board_mux *board_mux)
824 {
825         omap_mux_set_cmdline_signals();
826         omap_mux_write_array(partition, board_mux);
827 }
828
829 #else
830
831 static void omap_mux_init_package(struct omap_mux *superset,
832                                   struct omap_mux *package_subset,
833                                   struct omap_ball *package_balls)
834 {
835 }
836
837 static void omap_mux_init_signals(struct omap_mux_partition *partition,
838                                   struct omap_board_mux *board_mux)
839 {
840 }
841
842 #endif
843
844 static u32 mux_partitions_cnt;
845
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)
852 {
853         struct omap_mux_partition *partition;
854
855         partition = kzalloc(sizeof(struct omap_mux_partition), GFP_KERNEL);
856         if (!partition)
857                 return -ENOMEM;
858
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);
867                 return -ENODEV;
868         }
869
870         INIT_LIST_HEAD(&partition->muxmodes);
871
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);
876
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);
880
881         return 0;
882 }
883