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