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