]> Pileus Git - ~andy/linux/blob - arch/arm/mach-omap2/mux.c
Merge branch 'common/pfc' into sh-latest
[~andy/linux] / 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 #include <linux/irq.h>
36 #include <linux/interrupt.h>
37
38
39 #include <plat/omap_hwmod.h>
40
41 #include "control.h"
42 #include "mux.h"
43 #include "prm.h"
44
45 #define OMAP_MUX_BASE_OFFSET            0x30    /* Offset from CTRL_BASE */
46 #define OMAP_MUX_BASE_SZ                0x5ca
47
48 struct omap_mux_entry {
49         struct omap_mux         mux;
50         struct list_head        node;
51 };
52
53 static LIST_HEAD(mux_partitions);
54 static DEFINE_MUTEX(muxmode_mutex);
55
56 struct omap_mux_partition *omap_mux_get(const char *name)
57 {
58         struct omap_mux_partition *partition;
59
60         list_for_each_entry(partition, &mux_partitions, node) {
61                 if (!strcmp(name, partition->name))
62                         return partition;
63         }
64
65         return NULL;
66 }
67
68 u16 omap_mux_read(struct omap_mux_partition *partition, u16 reg)
69 {
70         if (partition->flags & OMAP_MUX_REG_8BIT)
71                 return __raw_readb(partition->base + reg);
72         else
73                 return __raw_readw(partition->base + reg);
74 }
75
76 void omap_mux_write(struct omap_mux_partition *partition, u16 val,
77                            u16 reg)
78 {
79         if (partition->flags & OMAP_MUX_REG_8BIT)
80                 __raw_writeb(val, partition->base + reg);
81         else
82                 __raw_writew(val, partition->base + reg);
83 }
84
85 void omap_mux_write_array(struct omap_mux_partition *partition,
86                                  struct omap_board_mux *board_mux)
87 {
88         if (!board_mux)
89                 return;
90
91         while (board_mux->reg_offset != OMAP_MUX_TERMINATOR) {
92                 omap_mux_write(partition, board_mux->value,
93                                board_mux->reg_offset);
94                 board_mux++;
95         }
96 }
97
98 #ifdef CONFIG_OMAP_MUX
99
100 static char *omap_mux_options;
101
102 static int __init _omap_mux_init_gpio(struct omap_mux_partition *partition,
103                                       int gpio, int val)
104 {
105         struct omap_mux_entry *e;
106         struct omap_mux *gpio_mux = NULL;
107         u16 old_mode;
108         u16 mux_mode;
109         int found = 0;
110         struct list_head *muxmodes = &partition->muxmodes;
111
112         if (!gpio)
113                 return -EINVAL;
114
115         list_for_each_entry(e, muxmodes, node) {
116                 struct omap_mux *m = &e->mux;
117                 if (gpio == m->gpio) {
118                         gpio_mux = m;
119                         found++;
120                 }
121         }
122
123         if (found == 0) {
124                 pr_err("%s: Could not set gpio%i\n", __func__, gpio);
125                 return -ENODEV;
126         }
127
128         if (found > 1) {
129                 pr_info("%s: Multiple gpio paths (%d) for gpio%i\n", __func__,
130                         found, gpio);
131                 return -EINVAL;
132         }
133
134         old_mode = omap_mux_read(partition, gpio_mux->reg_offset);
135         mux_mode = val & ~(OMAP_MUX_NR_MODES - 1);
136         if (partition->flags & OMAP_MUX_GPIO_IN_MODE3)
137                 mux_mode |= OMAP_MUX_MODE3;
138         else
139                 mux_mode |= OMAP_MUX_MODE4;
140         pr_debug("%s: Setting signal %s.gpio%i 0x%04x -> 0x%04x\n", __func__,
141                  gpio_mux->muxnames[0], gpio, old_mode, mux_mode);
142         omap_mux_write(partition, mux_mode, gpio_mux->reg_offset);
143
144         return 0;
145 }
146
147 int __init omap_mux_init_gpio(int gpio, int val)
148 {
149         struct omap_mux_partition *partition;
150         int ret;
151
152         list_for_each_entry(partition, &mux_partitions, node) {
153                 ret = _omap_mux_init_gpio(partition, gpio, val);
154                 if (!ret)
155                         return ret;
156         }
157
158         return -ENODEV;
159 }
160
161 static int __init _omap_mux_get_by_name(struct omap_mux_partition *partition,
162                                         const char *muxname,
163                                         struct omap_mux **found_mux)
164 {
165         struct omap_mux *mux = NULL;
166         struct omap_mux_entry *e;
167         const char *mode_name;
168         int found = 0, found_mode = 0, mode0_len = 0;
169         struct list_head *muxmodes = &partition->muxmodes;
170
171         mode_name = strchr(muxname, '.');
172         if (mode_name) {
173                 mode0_len = strlen(muxname) - strlen(mode_name);
174                 mode_name++;
175         } else {
176                 mode_name = muxname;
177         }
178
179         list_for_each_entry(e, muxmodes, node) {
180                 char *m0_entry;
181                 int i;
182
183                 mux = &e->mux;
184                 m0_entry = mux->muxnames[0];
185
186                 /* First check for full name in mode0.muxmode format */
187                 if (mode0_len && strncmp(muxname, m0_entry, mode0_len))
188                         continue;
189
190                 /* Then check for muxmode only */
191                 for (i = 0; i < OMAP_MUX_NR_MODES; i++) {
192                         char *mode_cur = mux->muxnames[i];
193
194                         if (!mode_cur)
195                                 continue;
196
197                         if (!strcmp(mode_name, mode_cur)) {
198                                 *found_mux = mux;
199                                 found++;
200                                 found_mode = i;
201                         }
202                 }
203         }
204
205         if (found == 1) {
206                 return found_mode;
207         }
208
209         if (found > 1) {
210                 pr_err("%s: Multiple signal paths (%i) for %s\n", __func__,
211                        found, muxname);
212                 return -EINVAL;
213         }
214
215         pr_err("%s: Could not find signal %s\n", __func__, muxname);
216
217         return -ENODEV;
218 }
219
220 int __init omap_mux_get_by_name(const char *muxname,
221                         struct omap_mux_partition **found_partition,
222                         struct omap_mux **found_mux)
223 {
224         struct omap_mux_partition *partition;
225
226         list_for_each_entry(partition, &mux_partitions, node) {
227                 struct omap_mux *mux = NULL;
228                 int mux_mode = _omap_mux_get_by_name(partition, muxname, &mux);
229                 if (mux_mode < 0)
230                         continue;
231
232                 *found_partition = partition;
233                 *found_mux = mux;
234
235                 return mux_mode;
236         }
237
238         return -ENODEV;
239 }
240
241 int __init omap_mux_init_signal(const char *muxname, int val)
242 {
243         struct omap_mux_partition *partition = NULL;
244         struct omap_mux *mux = NULL;
245         u16 old_mode;
246         int mux_mode;
247
248         mux_mode = omap_mux_get_by_name(muxname, &partition, &mux);
249         if (mux_mode < 0 || !mux)
250                 return mux_mode;
251
252         old_mode = omap_mux_read(partition, mux->reg_offset);
253         mux_mode |= val;
254         pr_debug("%s: Setting signal %s 0x%04x -> 0x%04x\n",
255                          __func__, muxname, old_mode, mux_mode);
256         omap_mux_write(partition, mux_mode, mux->reg_offset);
257
258         return 0;
259 }
260
261 struct omap_hwmod_mux_info * __init
262 omap_hwmod_mux_init(struct omap_device_pad *bpads, int nr_pads)
263 {
264         struct omap_hwmod_mux_info *hmux;
265         int i, nr_pads_dynamic = 0;
266
267         if (!bpads || nr_pads < 1)
268                 return NULL;
269
270         hmux = kzalloc(sizeof(struct omap_hwmod_mux_info), GFP_KERNEL);
271         if (!hmux)
272                 goto err1;
273
274         hmux->nr_pads = nr_pads;
275
276         hmux->pads = kzalloc(sizeof(struct omap_device_pad) *
277                                 nr_pads, GFP_KERNEL);
278         if (!hmux->pads)
279                 goto err2;
280
281         for (i = 0; i < hmux->nr_pads; i++) {
282                 struct omap_mux_partition *partition;
283                 struct omap_device_pad *bpad = &bpads[i], *pad = &hmux->pads[i];
284                 struct omap_mux *mux;
285                 int mux_mode;
286
287                 mux_mode = omap_mux_get_by_name(bpad->name, &partition, &mux);
288                 if (mux_mode < 0)
289                         goto err3;
290                 if (!pad->partition)
291                         pad->partition = partition;
292                 if (!pad->mux)
293                         pad->mux = mux;
294
295                 pad->name = kzalloc(strlen(bpad->name) + 1, GFP_KERNEL);
296                 if (!pad->name) {
297                         int j;
298
299                         for (j = i - 1; j >= 0; j--)
300                                 kfree(hmux->pads[j].name);
301                         goto err3;
302                 }
303                 strcpy(pad->name, bpad->name);
304
305                 pad->flags = bpad->flags;
306                 pad->enable = bpad->enable;
307                 pad->idle = bpad->idle;
308                 pad->off = bpad->off;
309
310                 if (pad->flags &
311                     (OMAP_DEVICE_PAD_REMUX | OMAP_DEVICE_PAD_WAKEUP))
312                         nr_pads_dynamic++;
313
314                 pr_debug("%s: Initialized %s\n", __func__, pad->name);
315         }
316
317         if (!nr_pads_dynamic)
318                 return hmux;
319
320         /*
321          * Add pads that need dynamic muxing into a separate list
322          */
323
324         hmux->nr_pads_dynamic = nr_pads_dynamic;
325         hmux->pads_dynamic = kzalloc(sizeof(struct omap_device_pad *) *
326                                         nr_pads_dynamic, GFP_KERNEL);
327         if (!hmux->pads_dynamic) {
328                 pr_err("%s: Could not allocate dynamic pads\n", __func__);
329                 return hmux;
330         }
331
332         nr_pads_dynamic = 0;
333         for (i = 0; i < hmux->nr_pads; i++) {
334                 struct omap_device_pad *pad = &hmux->pads[i];
335
336                 if (pad->flags &
337                     (OMAP_DEVICE_PAD_REMUX | OMAP_DEVICE_PAD_WAKEUP)) {
338                         pr_debug("%s: pad %s tagged dynamic\n",
339                                         __func__, pad->name);
340                         hmux->pads_dynamic[nr_pads_dynamic] = pad;
341                         nr_pads_dynamic++;
342                 }
343         }
344
345         return hmux;
346
347 err3:
348         kfree(hmux->pads);
349 err2:
350         kfree(hmux);
351 err1:
352         pr_err("%s: Could not allocate device mux entry\n", __func__);
353
354         return NULL;
355 }
356
357 /**
358  * omap_hwmod_mux_scan_wakeups - omap hwmod scan wakeup pads
359  * @hmux: Pads for a hwmod
360  * @mpu_irqs: MPU irq array for a hwmod
361  *
362  * Scans the wakeup status of pads for a single hwmod.  If an irq
363  * array is defined for this mux, the parser will call the registered
364  * ISRs for corresponding pads, otherwise the parser will stop at the
365  * first wakeup active pad and return.  Returns true if there is a
366  * pending and non-served wakeup event for the mux, otherwise false.
367  */
368 static bool omap_hwmod_mux_scan_wakeups(struct omap_hwmod_mux_info *hmux,
369                 struct omap_hwmod_irq_info *mpu_irqs)
370 {
371         int i, irq;
372         unsigned int val;
373         u32 handled_irqs = 0;
374
375         for (i = 0; i < hmux->nr_pads_dynamic; i++) {
376                 struct omap_device_pad *pad = hmux->pads_dynamic[i];
377
378                 if (!(pad->flags & OMAP_DEVICE_PAD_WAKEUP) ||
379                     !(pad->idle & OMAP_WAKEUP_EN))
380                         continue;
381
382                 val = omap_mux_read(pad->partition, pad->mux->reg_offset);
383                 if (!(val & OMAP_WAKEUP_EVENT))
384                         continue;
385
386                 if (!hmux->irqs)
387                         return true;
388
389                 irq = hmux->irqs[i];
390                 /* make sure we only handle each irq once */
391                 if (handled_irqs & 1 << irq)
392                         continue;
393
394                 handled_irqs |= 1 << irq;
395
396                 generic_handle_irq(mpu_irqs[irq].irq);
397         }
398
399         return false;
400 }
401
402 /**
403  * _omap_hwmod_mux_handle_irq - Process wakeup events for a single hwmod
404  *
405  * Checks a single hwmod for every wakeup capable pad to see if there is an
406  * active wakeup event. If this is the case, call the corresponding ISR.
407  */
408 static int _omap_hwmod_mux_handle_irq(struct omap_hwmod *oh, void *data)
409 {
410         if (!oh->mux || !oh->mux->enabled)
411                 return 0;
412         if (omap_hwmod_mux_scan_wakeups(oh->mux, oh->mpu_irqs))
413                 generic_handle_irq(oh->mpu_irqs[0].irq);
414         return 0;
415 }
416
417 /**
418  * omap_hwmod_mux_handle_irq - Process pad wakeup irqs.
419  *
420  * Calls a function for each registered omap_hwmod to check
421  * pad wakeup statuses.
422  */
423 static irqreturn_t omap_hwmod_mux_handle_irq(int irq, void *unused)
424 {
425         omap_hwmod_for_each(_omap_hwmod_mux_handle_irq, NULL);
426         return IRQ_HANDLED;
427 }
428
429 /* Assumes the calling function takes care of locking */
430 void omap_hwmod_mux(struct omap_hwmod_mux_info *hmux, u8 state)
431 {
432         int i;
433
434         /* Runtime idling of dynamic pads */
435         if (state == _HWMOD_STATE_IDLE && hmux->enabled) {
436                 for (i = 0; i < hmux->nr_pads_dynamic; i++) {
437                         struct omap_device_pad *pad = hmux->pads_dynamic[i];
438                         int val = -EINVAL;
439
440                         val = pad->idle;
441                         omap_mux_write(pad->partition, val,
442                                         pad->mux->reg_offset);
443                 }
444
445                 return;
446         }
447
448         /* Runtime enabling of dynamic pads */
449         if ((state == _HWMOD_STATE_ENABLED) && hmux->pads_dynamic
450                                         && hmux->enabled) {
451                 for (i = 0; i < hmux->nr_pads_dynamic; i++) {
452                         struct omap_device_pad *pad = hmux->pads_dynamic[i];
453                         int val = -EINVAL;
454
455                         val = pad->enable;
456                         omap_mux_write(pad->partition, val,
457                                         pad->mux->reg_offset);
458                 }
459
460                 return;
461         }
462
463         /* Enabling or disabling of all pads */
464         for (i = 0; i < hmux->nr_pads; i++) {
465                 struct omap_device_pad *pad = &hmux->pads[i];
466                 int flags, val = -EINVAL;
467
468                 flags = pad->flags;
469
470                 switch (state) {
471                 case _HWMOD_STATE_ENABLED:
472                         val = pad->enable;
473                         pr_debug("%s: Enabling %s %x\n", __func__,
474                                         pad->name, val);
475                         break;
476                 case _HWMOD_STATE_DISABLED:
477                         /* Use safe mode unless OMAP_DEVICE_PAD_REMUX */
478                         if (flags & OMAP_DEVICE_PAD_REMUX)
479                                 val = pad->off;
480                         else
481                                 val = OMAP_MUX_MODE7;
482                         pr_debug("%s: Disabling %s %x\n", __func__,
483                                         pad->name, val);
484                         break;
485                 default:
486                         /* Nothing to be done */
487                         break;
488                 };
489
490                 if (val >= 0) {
491                         omap_mux_write(pad->partition, val,
492                                         pad->mux->reg_offset);
493                         pad->flags = flags;
494                 }
495         }
496
497         if (state == _HWMOD_STATE_ENABLED)
498                 hmux->enabled = true;
499         else
500                 hmux->enabled = false;
501 }
502
503 #ifdef CONFIG_DEBUG_FS
504
505 #define OMAP_MUX_MAX_NR_FLAGS   10
506 #define OMAP_MUX_TEST_FLAG(val, mask)                           \
507         if (((val) & (mask)) == (mask)) {                       \
508                 i++;                                            \
509                 flags[i] =  #mask;                              \
510         }
511
512 /* REVISIT: Add checking for non-optimal mux settings */
513 static inline void omap_mux_decode(struct seq_file *s, u16 val)
514 {
515         char *flags[OMAP_MUX_MAX_NR_FLAGS];
516         char mode[sizeof("OMAP_MUX_MODE") + 1];
517         int i = -1;
518
519         sprintf(mode, "OMAP_MUX_MODE%d", val & 0x7);
520         i++;
521         flags[i] = mode;
522
523         OMAP_MUX_TEST_FLAG(val, OMAP_PIN_OFF_WAKEUPENABLE);
524         if (val & OMAP_OFF_EN) {
525                 if (!(val & OMAP_OFFOUT_EN)) {
526                         if (!(val & OMAP_OFF_PULL_UP)) {
527                                 OMAP_MUX_TEST_FLAG(val,
528                                         OMAP_PIN_OFF_INPUT_PULLDOWN);
529                         } else {
530                                 OMAP_MUX_TEST_FLAG(val,
531                                         OMAP_PIN_OFF_INPUT_PULLUP);
532                         }
533                 } else {
534                         if (!(val & OMAP_OFFOUT_VAL)) {
535                                 OMAP_MUX_TEST_FLAG(val,
536                                         OMAP_PIN_OFF_OUTPUT_LOW);
537                         } else {
538                                 OMAP_MUX_TEST_FLAG(val,
539                                         OMAP_PIN_OFF_OUTPUT_HIGH);
540                         }
541                 }
542         }
543
544         if (val & OMAP_INPUT_EN) {
545                 if (val & OMAP_PULL_ENA) {
546                         if (!(val & OMAP_PULL_UP)) {
547                                 OMAP_MUX_TEST_FLAG(val,
548                                         OMAP_PIN_INPUT_PULLDOWN);
549                         } else {
550                                 OMAP_MUX_TEST_FLAG(val, OMAP_PIN_INPUT_PULLUP);
551                         }
552                 } else {
553                         OMAP_MUX_TEST_FLAG(val, OMAP_PIN_INPUT);
554                 }
555         } else {
556                 i++;
557                 flags[i] = "OMAP_PIN_OUTPUT";
558         }
559
560         do {
561                 seq_printf(s, "%s", flags[i]);
562                 if (i > 0)
563                         seq_printf(s, " | ");
564         } while (i-- > 0);
565 }
566
567 #define OMAP_MUX_DEFNAME_LEN    32
568
569 static int omap_mux_dbg_board_show(struct seq_file *s, void *unused)
570 {
571         struct omap_mux_partition *partition = s->private;
572         struct omap_mux_entry *e;
573         u8 omap_gen = omap_rev() >> 28;
574
575         list_for_each_entry(e, &partition->muxmodes, node) {
576                 struct omap_mux *m = &e->mux;
577                 char m0_def[OMAP_MUX_DEFNAME_LEN];
578                 char *m0_name = m->muxnames[0];
579                 u16 val;
580                 int i, mode;
581
582                 if (!m0_name)
583                         continue;
584
585                 /* REVISIT: Needs to be updated if mode0 names get longer */
586                 for (i = 0; i < OMAP_MUX_DEFNAME_LEN; i++) {
587                         if (m0_name[i] == '\0') {
588                                 m0_def[i] = m0_name[i];
589                                 break;
590                         }
591                         m0_def[i] = toupper(m0_name[i]);
592                 }
593                 val = omap_mux_read(partition, m->reg_offset);
594                 mode = val & OMAP_MUX_MODE7;
595                 if (mode != 0)
596                         seq_printf(s, "/* %s */\n", m->muxnames[mode]);
597
598                 /*
599                  * XXX: Might be revisited to support differences across
600                  * same OMAP generation.
601                  */
602                 seq_printf(s, "OMAP%d_MUX(%s, ", omap_gen, m0_def);
603                 omap_mux_decode(s, val);
604                 seq_printf(s, "),\n");
605         }
606
607         return 0;
608 }
609
610 static int omap_mux_dbg_board_open(struct inode *inode, struct file *file)
611 {
612         return single_open(file, omap_mux_dbg_board_show, inode->i_private);
613 }
614
615 static const struct file_operations omap_mux_dbg_board_fops = {
616         .open           = omap_mux_dbg_board_open,
617         .read           = seq_read,
618         .llseek         = seq_lseek,
619         .release        = single_release,
620 };
621
622 static struct omap_mux_partition *omap_mux_get_partition(struct omap_mux *mux)
623 {
624         struct omap_mux_partition *partition;
625
626         list_for_each_entry(partition, &mux_partitions, node) {
627                 struct list_head *muxmodes = &partition->muxmodes;
628                 struct omap_mux_entry *e;
629
630                 list_for_each_entry(e, muxmodes, node) {
631                         struct omap_mux *m = &e->mux;
632
633                         if (m == mux)
634                                 return partition;
635                 }
636         }
637
638         return NULL;
639 }
640
641 static int omap_mux_dbg_signal_show(struct seq_file *s, void *unused)
642 {
643         struct omap_mux *m = s->private;
644         struct omap_mux_partition *partition;
645         const char *none = "NA";
646         u16 val;
647         int mode;
648
649         partition = omap_mux_get_partition(m);
650         if (!partition)
651                 return 0;
652
653         val = omap_mux_read(partition, m->reg_offset);
654         mode = val & OMAP_MUX_MODE7;
655
656         seq_printf(s, "name: %s.%s (0x%08x/0x%03x = 0x%04x), b %s, t %s\n",
657                         m->muxnames[0], m->muxnames[mode],
658                         partition->phys + m->reg_offset, m->reg_offset, val,
659                         m->balls[0] ? m->balls[0] : none,
660                         m->balls[1] ? m->balls[1] : none);
661         seq_printf(s, "mode: ");
662         omap_mux_decode(s, val);
663         seq_printf(s, "\n");
664         seq_printf(s, "signals: %s | %s | %s | %s | %s | %s | %s | %s\n",
665                         m->muxnames[0] ? m->muxnames[0] : none,
666                         m->muxnames[1] ? m->muxnames[1] : none,
667                         m->muxnames[2] ? m->muxnames[2] : none,
668                         m->muxnames[3] ? m->muxnames[3] : none,
669                         m->muxnames[4] ? m->muxnames[4] : none,
670                         m->muxnames[5] ? m->muxnames[5] : none,
671                         m->muxnames[6] ? m->muxnames[6] : none,
672                         m->muxnames[7] ? m->muxnames[7] : none);
673
674         return 0;
675 }
676
677 #define OMAP_MUX_MAX_ARG_CHAR  7
678
679 static ssize_t omap_mux_dbg_signal_write(struct file *file,
680                                          const char __user *user_buf,
681                                          size_t count, loff_t *ppos)
682 {
683         char buf[OMAP_MUX_MAX_ARG_CHAR];
684         struct seq_file *seqf;
685         struct omap_mux *m;
686         unsigned long val;
687         int buf_size, ret;
688         struct omap_mux_partition *partition;
689
690         if (count > OMAP_MUX_MAX_ARG_CHAR)
691                 return -EINVAL;
692
693         memset(buf, 0, sizeof(buf));
694         buf_size = min(count, sizeof(buf) - 1);
695
696         if (copy_from_user(buf, user_buf, buf_size))
697                 return -EFAULT;
698
699         ret = strict_strtoul(buf, 0x10, &val);
700         if (ret < 0)
701                 return ret;
702
703         if (val > 0xffff)
704                 return -EINVAL;
705
706         seqf = file->private_data;
707         m = seqf->private;
708
709         partition = omap_mux_get_partition(m);
710         if (!partition)
711                 return -ENODEV;
712
713         omap_mux_write(partition, (u16)val, m->reg_offset);
714         *ppos += count;
715
716         return count;
717 }
718
719 static int omap_mux_dbg_signal_open(struct inode *inode, struct file *file)
720 {
721         return single_open(file, omap_mux_dbg_signal_show, inode->i_private);
722 }
723
724 static const struct file_operations omap_mux_dbg_signal_fops = {
725         .open           = omap_mux_dbg_signal_open,
726         .read           = seq_read,
727         .write          = omap_mux_dbg_signal_write,
728         .llseek         = seq_lseek,
729         .release        = single_release,
730 };
731
732 static struct dentry *mux_dbg_dir;
733
734 static void __init omap_mux_dbg_create_entry(
735                                 struct omap_mux_partition *partition,
736                                 struct dentry *mux_dbg_dir)
737 {
738         struct omap_mux_entry *e;
739
740         list_for_each_entry(e, &partition->muxmodes, node) {
741                 struct omap_mux *m = &e->mux;
742
743                 (void)debugfs_create_file(m->muxnames[0], S_IWUSR, mux_dbg_dir,
744                                           m, &omap_mux_dbg_signal_fops);
745         }
746 }
747
748 static void __init omap_mux_dbg_init(void)
749 {
750         struct omap_mux_partition *partition;
751         static struct dentry *mux_dbg_board_dir;
752
753         mux_dbg_dir = debugfs_create_dir("omap_mux", NULL);
754         if (!mux_dbg_dir)
755                 return;
756
757         mux_dbg_board_dir = debugfs_create_dir("board", mux_dbg_dir);
758         if (!mux_dbg_board_dir)
759                 return;
760
761         list_for_each_entry(partition, &mux_partitions, node) {
762                 omap_mux_dbg_create_entry(partition, mux_dbg_dir);
763                 (void)debugfs_create_file(partition->name, S_IRUGO,
764                                           mux_dbg_board_dir, partition,
765                                           &omap_mux_dbg_board_fops);
766         }
767 }
768
769 #else
770 static inline void omap_mux_dbg_init(void)
771 {
772 }
773 #endif  /* CONFIG_DEBUG_FS */
774
775 static void __init omap_mux_free_names(struct omap_mux *m)
776 {
777         int i;
778
779         for (i = 0; i < OMAP_MUX_NR_MODES; i++)
780                 kfree(m->muxnames[i]);
781
782 #ifdef CONFIG_DEBUG_FS
783         for (i = 0; i < OMAP_MUX_NR_SIDES; i++)
784                 kfree(m->balls[i]);
785 #endif
786
787 }
788
789 /* Free all data except for GPIO pins unless CONFIG_DEBUG_FS is set */
790 int __init omap_mux_late_init(void)
791 {
792         struct omap_mux_partition *partition;
793         int ret;
794
795         list_for_each_entry(partition, &mux_partitions, node) {
796                 struct omap_mux_entry *e, *tmp;
797                 list_for_each_entry_safe(e, tmp, &partition->muxmodes, node) {
798                         struct omap_mux *m = &e->mux;
799                         u16 mode = omap_mux_read(partition, m->reg_offset);
800
801                         if (OMAP_MODE_GPIO(mode))
802                                 continue;
803
804 #ifndef CONFIG_DEBUG_FS
805                         mutex_lock(&muxmode_mutex);
806                         list_del(&e->node);
807                         mutex_unlock(&muxmode_mutex);
808                         omap_mux_free_names(m);
809                         kfree(m);
810 #endif
811                 }
812         }
813
814         ret = request_irq(omap_prcm_event_to_irq("io"),
815                 omap_hwmod_mux_handle_irq, IRQF_SHARED | IRQF_NO_SUSPEND,
816                         "hwmod_io", omap_mux_late_init);
817
818         if (ret)
819                 pr_warning("mux: Failed to setup hwmod io irq %d\n", ret);
820
821         omap_mux_dbg_init();
822
823         return 0;
824 }
825
826 static void __init omap_mux_package_fixup(struct omap_mux *p,
827                                         struct omap_mux *superset)
828 {
829         while (p->reg_offset !=  OMAP_MUX_TERMINATOR) {
830                 struct omap_mux *s = superset;
831                 int found = 0;
832
833                 while (s->reg_offset != OMAP_MUX_TERMINATOR) {
834                         if (s->reg_offset == p->reg_offset) {
835                                 *s = *p;
836                                 found++;
837                                 break;
838                         }
839                         s++;
840                 }
841                 if (!found)
842                         pr_err("%s: Unknown entry offset 0x%x\n", __func__,
843                                p->reg_offset);
844                 p++;
845         }
846 }
847
848 #ifdef CONFIG_DEBUG_FS
849
850 static void __init omap_mux_package_init_balls(struct omap_ball *b,
851                                 struct omap_mux *superset)
852 {
853         while (b->reg_offset != OMAP_MUX_TERMINATOR) {
854                 struct omap_mux *s = superset;
855                 int found = 0;
856
857                 while (s->reg_offset != OMAP_MUX_TERMINATOR) {
858                         if (s->reg_offset == b->reg_offset) {
859                                 s->balls[0] = b->balls[0];
860                                 s->balls[1] = b->balls[1];
861                                 found++;
862                                 break;
863                         }
864                         s++;
865                 }
866                 if (!found)
867                         pr_err("%s: Unknown ball offset 0x%x\n", __func__,
868                                b->reg_offset);
869                 b++;
870         }
871 }
872
873 #else   /* CONFIG_DEBUG_FS */
874
875 static inline void omap_mux_package_init_balls(struct omap_ball *b,
876                                         struct omap_mux *superset)
877 {
878 }
879
880 #endif  /* CONFIG_DEBUG_FS */
881
882 static int __init omap_mux_setup(char *options)
883 {
884         if (!options)
885                 return 0;
886
887         omap_mux_options = options;
888
889         return 1;
890 }
891 __setup("omap_mux=", omap_mux_setup);
892
893 /*
894  * Note that the omap_mux=some.signal1=0x1234,some.signal2=0x1234
895  * cmdline options only override the bootloader values.
896  * During development, please enable CONFIG_DEBUG_FS, and use the
897  * signal specific entries under debugfs.
898  */
899 static void __init omap_mux_set_cmdline_signals(void)
900 {
901         char *options, *next_opt, *token;
902
903         if (!omap_mux_options)
904                 return;
905
906         options = kstrdup(omap_mux_options, GFP_KERNEL);
907         if (!options)
908                 return;
909
910         next_opt = options;
911
912         while ((token = strsep(&next_opt, ",")) != NULL) {
913                 char *keyval, *name;
914                 unsigned long val;
915
916                 keyval = token;
917                 name = strsep(&keyval, "=");
918                 if (name) {
919                         int res;
920
921                         res = strict_strtoul(keyval, 0x10, &val);
922                         if (res < 0)
923                                 continue;
924
925                         omap_mux_init_signal(name, (u16)val);
926                 }
927         }
928
929         kfree(options);
930 }
931
932 static int __init omap_mux_copy_names(struct omap_mux *src,
933                                       struct omap_mux *dst)
934 {
935         int i;
936
937         for (i = 0; i < OMAP_MUX_NR_MODES; i++) {
938                 if (src->muxnames[i]) {
939                         dst->muxnames[i] = kstrdup(src->muxnames[i],
940                                                    GFP_KERNEL);
941                         if (!dst->muxnames[i])
942                                 goto free;
943                 }
944         }
945
946 #ifdef CONFIG_DEBUG_FS
947         for (i = 0; i < OMAP_MUX_NR_SIDES; i++) {
948                 if (src->balls[i]) {
949                         dst->balls[i] = kstrdup(src->balls[i], GFP_KERNEL);
950                         if (!dst->balls[i])
951                                 goto free;
952                 }
953         }
954 #endif
955
956         return 0;
957
958 free:
959         omap_mux_free_names(dst);
960         return -ENOMEM;
961
962 }
963
964 #endif  /* CONFIG_OMAP_MUX */
965
966 static struct omap_mux *omap_mux_get_by_gpio(
967                                 struct omap_mux_partition *partition,
968                                 int gpio)
969 {
970         struct omap_mux_entry *e;
971         struct omap_mux *ret = NULL;
972
973         list_for_each_entry(e, &partition->muxmodes, node) {
974                 struct omap_mux *m = &e->mux;
975                 if (m->gpio == gpio) {
976                         ret = m;
977                         break;
978                 }
979         }
980
981         return ret;
982 }
983
984 /* Needed for dynamic muxing of GPIO pins for off-idle */
985 u16 omap_mux_get_gpio(int gpio)
986 {
987         struct omap_mux_partition *partition;
988         struct omap_mux *m = NULL;
989
990         list_for_each_entry(partition, &mux_partitions, node) {
991                 m = omap_mux_get_by_gpio(partition, gpio);
992                 if (m)
993                         return omap_mux_read(partition, m->reg_offset);
994         }
995
996         if (!m || m->reg_offset == OMAP_MUX_TERMINATOR)
997                 pr_err("%s: Could not get gpio%i\n", __func__, gpio);
998
999         return OMAP_MUX_TERMINATOR;
1000 }
1001
1002 /* Needed for dynamic muxing of GPIO pins for off-idle */
1003 void omap_mux_set_gpio(u16 val, int gpio)
1004 {
1005         struct omap_mux_partition *partition;
1006         struct omap_mux *m = NULL;
1007
1008         list_for_each_entry(partition, &mux_partitions, node) {
1009                 m = omap_mux_get_by_gpio(partition, gpio);
1010                 if (m) {
1011                         omap_mux_write(partition, val, m->reg_offset);
1012                         return;
1013                 }
1014         }
1015
1016         if (!m || m->reg_offset == OMAP_MUX_TERMINATOR)
1017                 pr_err("%s: Could not set gpio%i\n", __func__, gpio);
1018 }
1019
1020 static struct omap_mux * __init omap_mux_list_add(
1021                                         struct omap_mux_partition *partition,
1022                                         struct omap_mux *src)
1023 {
1024         struct omap_mux_entry *entry;
1025         struct omap_mux *m;
1026
1027         entry = kzalloc(sizeof(struct omap_mux_entry), GFP_KERNEL);
1028         if (!entry)
1029                 return NULL;
1030
1031         m = &entry->mux;
1032         entry->mux = *src;
1033
1034 #ifdef CONFIG_OMAP_MUX
1035         if (omap_mux_copy_names(src, m)) {
1036                 kfree(entry);
1037                 return NULL;
1038         }
1039 #endif
1040
1041         mutex_lock(&muxmode_mutex);
1042         list_add_tail(&entry->node, &partition->muxmodes);
1043         mutex_unlock(&muxmode_mutex);
1044
1045         return m;
1046 }
1047
1048 /*
1049  * Note if CONFIG_OMAP_MUX is not selected, we will only initialize
1050  * the GPIO to mux offset mapping that is needed for dynamic muxing
1051  * of GPIO pins for off-idle.
1052  */
1053 static void __init omap_mux_init_list(struct omap_mux_partition *partition,
1054                                       struct omap_mux *superset)
1055 {
1056         while (superset->reg_offset !=  OMAP_MUX_TERMINATOR) {
1057                 struct omap_mux *entry;
1058
1059 #ifdef CONFIG_OMAP_MUX
1060                 if (!superset->muxnames || !superset->muxnames[0]) {
1061                         superset++;
1062                         continue;
1063                 }
1064 #else
1065                 /* Skip pins that are not muxed as GPIO by bootloader */
1066                 if (!OMAP_MODE_GPIO(omap_mux_read(partition,
1067                                     superset->reg_offset))) {
1068                         superset++;
1069                         continue;
1070                 }
1071 #endif
1072
1073                 entry = omap_mux_list_add(partition, superset);
1074                 if (!entry) {
1075                         pr_err("%s: Could not add entry\n", __func__);
1076                         return;
1077                 }
1078                 superset++;
1079         }
1080 }
1081
1082 #ifdef CONFIG_OMAP_MUX
1083
1084 static void omap_mux_init_package(struct omap_mux *superset,
1085                                   struct omap_mux *package_subset,
1086                                   struct omap_ball *package_balls)
1087 {
1088         if (package_subset)
1089                 omap_mux_package_fixup(package_subset, superset);
1090         if (package_balls)
1091                 omap_mux_package_init_balls(package_balls, superset);
1092 }
1093
1094 static void __init omap_mux_init_signals(struct omap_mux_partition *partition,
1095                                          struct omap_board_mux *board_mux)
1096 {
1097         omap_mux_set_cmdline_signals();
1098         omap_mux_write_array(partition, board_mux);
1099 }
1100
1101 #else
1102
1103 static void omap_mux_init_package(struct omap_mux *superset,
1104                                   struct omap_mux *package_subset,
1105                                   struct omap_ball *package_balls)
1106 {
1107 }
1108
1109 static void __init omap_mux_init_signals(struct omap_mux_partition *partition,
1110                                          struct omap_board_mux *board_mux)
1111 {
1112 }
1113
1114 #endif
1115
1116 static u32 mux_partitions_cnt;
1117
1118 int __init omap_mux_init(const char *name, u32 flags,
1119                          u32 mux_pbase, u32 mux_size,
1120                          struct omap_mux *superset,
1121                          struct omap_mux *package_subset,
1122                          struct omap_board_mux *board_mux,
1123                          struct omap_ball *package_balls)
1124 {
1125         struct omap_mux_partition *partition;
1126
1127         partition = kzalloc(sizeof(struct omap_mux_partition), GFP_KERNEL);
1128         if (!partition)
1129                 return -ENOMEM;
1130
1131         partition->name = name;
1132         partition->flags = flags;
1133         partition->size = mux_size;
1134         partition->phys = mux_pbase;
1135         partition->base = ioremap(mux_pbase, mux_size);
1136         if (!partition->base) {
1137                 pr_err("%s: Could not ioremap mux partition at 0x%08x\n",
1138                         __func__, partition->phys);
1139                 kfree(partition);
1140                 return -ENODEV;
1141         }
1142
1143         INIT_LIST_HEAD(&partition->muxmodes);
1144
1145         list_add_tail(&partition->node, &mux_partitions);
1146         mux_partitions_cnt++;
1147         pr_info("%s: Add partition: #%d: %s, flags: %x\n", __func__,
1148                 mux_partitions_cnt, partition->name, partition->flags);
1149
1150         omap_mux_init_package(superset, package_subset, package_balls);
1151         omap_mux_init_list(partition, superset);
1152         omap_mux_init_signals(partition, board_mux);
1153
1154         return 0;
1155 }
1156