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