blob: 3d71d93caab2999fc18ed2311dc1cafa172b4397 [file] [log] [blame]
Tony Lindgren1dbae812005-11-10 14:26:51 +00001/*
2 * linux/arch/arm/mach-omap2/mux.c
3 *
Benoit Cousson112485e2010-08-16 10:55:35 +02004 * OMAP2, OMAP3 and OMAP4 pin multiplexing configurations
Tony Lindgren1dbae812005-11-10 14:26:51 +00005 *
Benoit Cousson112485e2010-08-16 10:55:35 +02006 * Copyright (C) 2004 - 2010 Texas Instruments Inc.
Tony Lindgren93308992008-01-24 17:24:15 -08007 * Copyright (C) 2003 - 2008 Nokia Corporation
Tony Lindgren1dbae812005-11-10 14:26:51 +00008 *
Tony Lindgren93308992008-01-24 17:24:15 -08009 * Written by Tony Lindgren
Tony Lindgren1dbae812005-11-10 14:26:51 +000010 *
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 */
Paul Walmsley4814ced2010-10-08 11:40:20 -060026#include <linux/kernel.h>
Tony Lindgren1dbae812005-11-10 14:26:51 +000027#include <linux/init.h>
Russell Kingfced80c2008-09-06 12:10:45 +010028#include <linux/io.h>
Tony Lindgren15ac7af2009-12-11 16:16:32 -080029#include <linux/list.h>
Paul Walmsley4814ced2010-10-08 11:40:20 -060030#include <linux/slab.h>
Tony Lindgren4b715ef2009-12-11 16:16:32 -080031#include <linux/ctype.h>
32#include <linux/debugfs.h>
33#include <linux/seq_file.h>
34#include <linux/uaccess.h>
Tony Lindgren1dbae812005-11-10 14:26:51 +000035
Russell Kingfced80c2008-09-06 12:10:45 +010036#include <asm/system.h>
37
Paul Walmsley4814ced2010-10-08 11:40:20 -060038#include "control.h"
Tony Lindgren15ac7af2009-12-11 16:16:32 -080039#include "mux.h"
Tony Lindgren1dbae812005-11-10 14:26:51 +000040
Mike Rapoport92c9f502009-12-11 16:16:31 -080041#define OMAP_MUX_BASE_OFFSET 0x30 /* Offset from CTRL_BASE */
42#define OMAP_MUX_BASE_SZ 0x5ca
43
Tony Lindgren15ac7af2009-12-11 16:16:32 -080044struct omap_mux_entry {
45 struct omap_mux mux;
46 struct list_head node;
47};
48
Benoit Cousson112485e2010-08-16 10:55:35 +020049static LIST_HEAD(mux_partitions);
50static DEFINE_MUTEX(muxmode_mutex);
Mike Rapoport92c9f502009-12-11 16:16:31 -080051
Benoit Cousson112485e2010-08-16 10:55:35 +020052struct omap_mux_partition *omap_mux_get(const char *name)
Mike Rapoport92c9f502009-12-11 16:16:31 -080053{
Benoit Cousson112485e2010-08-16 10:55:35 +020054 struct omap_mux_partition *partition;
55
56 list_for_each_entry(partition, &mux_partitions, node) {
57 if (!strcmp(name, partition->name))
58 return partition;
59 }
60
61 return NULL;
Mike Rapoport92c9f502009-12-11 16:16:31 -080062}
63
Benoit Cousson112485e2010-08-16 10:55:35 +020064u16 omap_mux_read(struct omap_mux_partition *partition, u16 reg)
Mike Rapoport92c9f502009-12-11 16:16:31 -080065{
Benoit Cousson112485e2010-08-16 10:55:35 +020066 if (partition->flags & OMAP_MUX_REG_8BIT)
67 return __raw_readb(partition->base + reg);
Mike Rapoport92c9f502009-12-11 16:16:31 -080068 else
Benoit Cousson112485e2010-08-16 10:55:35 +020069 return __raw_readw(partition->base + reg);
Mike Rapoport92c9f502009-12-11 16:16:31 -080070}
Tony Lindgren7d7f6652008-01-25 00:42:48 -080071
Benoit Cousson112485e2010-08-16 10:55:35 +020072void omap_mux_write(struct omap_mux_partition *partition, u16 val,
73 u16 reg)
Tony Lindgrend4bb72e2010-01-19 15:15:24 -080074{
Benoit Cousson112485e2010-08-16 10:55:35 +020075 if (partition->flags & OMAP_MUX_REG_8BIT)
76 __raw_writeb(val, partition->base + reg);
77 else
78 __raw_writew(val, partition->base + reg);
79}
80
81void omap_mux_write_array(struct omap_mux_partition *partition,
82 struct omap_board_mux *board_mux)
83{
84 while (board_mux->reg_offset != OMAP_MUX_TERMINATOR) {
85 omap_mux_write(partition, board_mux->value,
86 board_mux->reg_offset);
Tony Lindgrend4bb72e2010-01-19 15:15:24 -080087 board_mux++;
88 }
89}
90
Tony Lindgren15ac7af2009-12-11 16:16:32 -080091#ifdef CONFIG_OMAP_MUX
92
93static char *omap_mux_options;
94
Benoit Cousson112485e2010-08-16 10:55:35 +020095static int __init _omap_mux_init_gpio(struct omap_mux_partition *partition,
96 int gpio, int val)
Tony Lindgren15ac7af2009-12-11 16:16:32 -080097{
98 struct omap_mux_entry *e;
Sanjeev Premica828762010-09-23 18:27:18 -070099 struct omap_mux *gpio_mux = NULL;
Grazvydas Ignotas8a6f7e12010-08-02 13:18:28 +0300100 u16 old_mode;
101 u16 mux_mode;
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800102 int found = 0;
Benoit Cousson112485e2010-08-16 10:55:35 +0200103 struct list_head *muxmodes = &partition->muxmodes;
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800104
105 if (!gpio)
106 return -EINVAL;
107
Benoit Cousson112485e2010-08-16 10:55:35 +0200108 list_for_each_entry(e, muxmodes, node) {
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800109 struct omap_mux *m = &e->mux;
110 if (gpio == m->gpio) {
Grazvydas Ignotas8a6f7e12010-08-02 13:18:28 +0300111 gpio_mux = m;
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800112 found++;
113 }
114 }
115
Grazvydas Ignotas8a6f7e12010-08-02 13:18:28 +0300116 if (found == 0) {
Dan Murphy032a6422010-11-15 09:50:50 -0600117 pr_err("%s: Could not set gpio%i\n", __func__, gpio);
Grazvydas Ignotas8a6f7e12010-08-02 13:18:28 +0300118 return -ENODEV;
119 }
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800120
121 if (found > 1) {
Dan Murphy032a6422010-11-15 09:50:50 -0600122 pr_info("%s: Multiple gpio paths (%d) for gpio%i\n", __func__,
Benoit Cousson1cbb3a92010-08-10 17:33:01 +0200123 found, gpio);
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800124 return -EINVAL;
125 }
126
Benoit Cousson112485e2010-08-16 10:55:35 +0200127 old_mode = omap_mux_read(partition, gpio_mux->reg_offset);
Grazvydas Ignotas8a6f7e12010-08-02 13:18:28 +0300128 mux_mode = val & ~(OMAP_MUX_NR_MODES - 1);
Benoit Cousson112485e2010-08-16 10:55:35 +0200129 if (partition->flags & OMAP_MUX_GPIO_IN_MODE3)
Grazvydas Ignotas8a6f7e12010-08-02 13:18:28 +0300130 mux_mode |= OMAP_MUX_MODE3;
131 else
132 mux_mode |= OMAP_MUX_MODE4;
Dan Murphy032a6422010-11-15 09:50:50 -0600133 pr_debug("%s: Setting signal %s.gpio%i 0x%04x -> 0x%04x\n", __func__,
Benoit Cousson1cbb3a92010-08-10 17:33:01 +0200134 gpio_mux->muxnames[0], gpio, old_mode, mux_mode);
Benoit Cousson112485e2010-08-16 10:55:35 +0200135 omap_mux_write(partition, mux_mode, gpio_mux->reg_offset);
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800136
Grazvydas Ignotas8a6f7e12010-08-02 13:18:28 +0300137 return 0;
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800138}
139
Benoit Cousson112485e2010-08-16 10:55:35 +0200140int __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
154static int __init _omap_mux_init_signal(struct omap_mux_partition *partition,
155 const char *muxname, int val)
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800156{
157 struct omap_mux_entry *e;
Tony Lindgren5a3b2f72010-10-01 16:35:24 -0700158 const char *mode_name;
159 int found = 0, mode0_len = 0;
Benoit Cousson112485e2010-08-16 10:55:35 +0200160 struct list_head *muxmodes = &partition->muxmodes;
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800161
162 mode_name = strchr(muxname, '.');
163 if (mode_name) {
Tony Lindgren5a3b2f72010-10-01 16:35:24 -0700164 mode0_len = strlen(muxname) - strlen(mode_name);
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800165 mode_name++;
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800166 } else {
167 mode_name = muxname;
168 }
169
Benoit Cousson112485e2010-08-16 10:55:35 +0200170 list_for_each_entry(e, muxmodes, node) {
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800171 struct omap_mux *m = &e->mux;
172 char *m0_entry = m->muxnames[0];
173 int i;
174
Tony Lindgren5a3b2f72010-10-01 16:35:24 -0700175 /* First check for full name in mode0.muxmode format */
176 if (mode0_len && strncmp(muxname, m0_entry, mode0_len))
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800177 continue;
178
Tony Lindgren5a3b2f72010-10-01 16:35:24 -0700179 /* Then check for muxmode only */
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800180 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
Benoit Cousson112485e2010-08-16 10:55:35 +0200190 old_mode = omap_mux_read(partition,
191 m->reg_offset);
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800192 mux_mode = val | i;
Dan Murphy032a6422010-11-15 09:50:50 -0600193 pr_debug("%s: Setting signal "
194 "%s.%s 0x%04x -> 0x%04x\n", __func__,
Benoit Cousson1cbb3a92010-08-10 17:33:01 +0200195 m0_entry, muxname, old_mode, mux_mode);
Benoit Cousson112485e2010-08-16 10:55:35 +0200196 omap_mux_write(partition, mux_mode,
197 m->reg_offset);
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800198 found++;
199 }
200 }
201 }
202
203 if (found == 1)
204 return 0;
205
206 if (found > 1) {
Dan Murphy032a6422010-11-15 09:50:50 -0600207 pr_err("%s: Multiple signal paths (%i) for %s\n", __func__,
Benoit Cousson1cbb3a92010-08-10 17:33:01 +0200208 found, muxname);
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800209 return -EINVAL;
210 }
211
Dan Murphy032a6422010-11-15 09:50:50 -0600212 pr_err("%s: Could not set signal %s\n", __func__, muxname);
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800213
214 return -ENODEV;
215}
216
Benoit Cousson112485e2010-08-16 10:55:35 +0200217int __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
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800232#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 */
242static inline void omap_mux_decode(struct seq_file *s, u16 val)
243{
244 char *flags[OMAP_MUX_MAX_NR_FLAGS];
Tony Lindgren78737ae2010-02-01 13:03:42 -0800245 char mode[sizeof("OMAP_MUX_MODE") + 1];
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800246 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
Benoit Cousson112485e2010-08-16 10:55:35 +0200296#define OMAP_MUX_DEFNAME_LEN 32
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800297
298static int omap_mux_dbg_board_show(struct seq_file *s, void *unused)
299{
Benoit Cousson112485e2010-08-16 10:55:35 +0200300 struct omap_mux_partition *partition = s->private;
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800301 struct omap_mux_entry *e;
Benoit Cousson112485e2010-08-16 10:55:35 +0200302 u8 omap_gen = omap_rev() >> 28;
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800303
Benoit Cousson112485e2010-08-16 10:55:35 +0200304 list_for_each_entry(e, &partition->muxmodes, node) {
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800305 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
Tony Lindgren78737ae2010-02-01 13:03:42 -0800314 /* REVISIT: Needs to be updated if mode0 names get longer */
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800315 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 }
Benoit Cousson112485e2010-08-16 10:55:35 +0200322 val = omap_mux_read(partition, m->reg_offset);
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800323 mode = val & OMAP_MUX_MODE7;
Benoit Cousson112485e2010-08-16 10:55:35 +0200324 if (mode != 0)
325 seq_printf(s, "/* %s */\n", m->muxnames[mode]);
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800326
Benoit Cousson112485e2010-08-16 10:55:35 +0200327 /*
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);
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800332 omap_mux_decode(s, val);
333 seq_printf(s, "),\n");
334 }
335
336 return 0;
337}
338
339static int omap_mux_dbg_board_open(struct inode *inode, struct file *file)
340{
Benoit Cousson112485e2010-08-16 10:55:35 +0200341 return single_open(file, omap_mux_dbg_board_show, inode->i_private);
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800342}
343
344static 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
Benoit Cousson112485e2010-08-16 10:55:35 +0200351static 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
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800370static int omap_mux_dbg_signal_show(struct seq_file *s, void *unused)
371{
372 struct omap_mux *m = s->private;
Benoit Cousson112485e2010-08-16 10:55:35 +0200373 struct omap_mux_partition *partition;
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800374 const char *none = "NA";
375 u16 val;
376 int mode;
377
Benoit Cousson112485e2010-08-16 10:55:35 +0200378 partition = omap_mux_get_partition(m);
379 if (!partition)
380 return 0;
381
382 val = omap_mux_read(partition, m->reg_offset);
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800383 mode = val & OMAP_MUX_MODE7;
384
Benoit Cousson112485e2010-08-16 10:55:35 +0200385 seq_printf(s, "name: %s.%s (0x%08x/0x%03x = 0x%04x), b %s, t %s\n",
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800386 m->muxnames[0], m->muxnames[mode],
Benoit Cousson112485e2010-08-16 10:55:35 +0200387 partition->phys + m->reg_offset, m->reg_offset, val,
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800388 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
408static ssize_t omap_mux_dbg_signal_write(struct file *file,
Benoit Cousson112485e2010-08-16 10:55:35 +0200409 const char __user *user_buf,
410 size_t count, loff_t *ppos)
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800411{
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;
Benoit Cousson112485e2010-08-16 10:55:35 +0200417 struct omap_mux_partition *partition;
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800418
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
Benoit Cousson112485e2010-08-16 10:55:35 +0200438 partition = omap_mux_get_partition(m);
439 if (!partition)
440 return -ENODEV;
441
442 omap_mux_write(partition, (u16)val, m->reg_offset);
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800443 *ppos += count;
444
445 return count;
446}
447
448static 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
453static 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
461static struct dentry *mux_dbg_dir;
462
Benoit Cousson112485e2010-08-16 10:55:35 +0200463static void __init omap_mux_dbg_create_entry(
464 struct omap_mux_partition *partition,
465 struct dentry *mux_dbg_dir)
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800466{
467 struct omap_mux_entry *e;
468
Benoit Cousson112485e2010-08-16 10:55:35 +0200469 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
477static void __init omap_mux_dbg_init(void)
478{
479 struct omap_mux_partition *partition;
480 static struct dentry *mux_dbg_board_dir;
481
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800482 mux_dbg_dir = debugfs_create_dir("omap_mux", NULL);
483 if (!mux_dbg_dir)
484 return;
485
Benoit Cousson112485e2010-08-16 10:55:35 +0200486 mux_dbg_board_dir = debugfs_create_dir("board", mux_dbg_dir);
487 if (!mux_dbg_board_dir)
488 return;
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800489
Benoit Cousson112485e2010-08-16 10:55:35 +0200490 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);
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800495 }
496}
497
498#else
499static inline void omap_mux_dbg_init(void)
500{
501}
502#endif /* CONFIG_DEBUG_FS */
503
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800504static 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]);
Tony Lindgren1dbae812005-11-10 14:26:51 +0000514#endif
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800515
516}
517
518/* Free all data except for GPIO pins unless CONFIG_DEBUG_FS is set */
519static int __init omap_mux_late_init(void)
520{
Benoit Cousson112485e2010-08-16 10:55:35 +0200521 struct omap_mux_partition *partition;
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800522
Benoit Cousson112485e2010-08-16 10:55:35 +0200523 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);
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800528
Benoit Cousson112485e2010-08-16 10:55:35 +0200529 if (OMAP_MODE_GPIO(mode))
530 continue;
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800531
532#ifndef CONFIG_DEBUG_FS
Benoit Cousson112485e2010-08-16 10:55:35 +0200533 mutex_lock(&muxmode_mutex);
534 list_del(&e->node);
535 mutex_unlock(&muxmode_mutex);
536 omap_mux_free_names(m);
537 kfree(m);
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800538#endif
Benoit Cousson112485e2010-08-16 10:55:35 +0200539 }
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800540 }
541
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800542 omap_mux_dbg_init();
543
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800544 return 0;
545}
546late_initcall(omap_mux_late_init);
547
548static 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)
Dan Murphy032a6422010-11-15 09:50:50 -0600564 pr_err("%s: Unknown entry offset 0x%x\n", __func__,
Benoit Cousson1cbb3a92010-08-10 17:33:01 +0200565 p->reg_offset);
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800566 p++;
567 }
568}
569
570#ifdef CONFIG_DEBUG_FS
571
572static 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)
Dan Murphy032a6422010-11-15 09:50:50 -0600589 pr_err("%s: Unknown ball offset 0x%x\n", __func__,
Benoit Cousson1cbb3a92010-08-10 17:33:01 +0200590 b->reg_offset);
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800591 b++;
592 }
593}
594
595#else /* CONFIG_DEBUG_FS */
596
597static 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
604static 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 */
621static 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
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800655static int __init omap_mux_copy_names(struct omap_mux *src,
Benoit Cousson112485e2010-08-16 10:55:35 +0200656 struct omap_mux *dst)
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800657{
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
686free:
687 omap_mux_free_names(dst);
688 return -ENOMEM;
689
690}
691
692#endif /* CONFIG_OMAP_MUX */
693
Benoit Cousson112485e2010-08-16 10:55:35 +0200694static struct omap_mux *omap_mux_get_by_gpio(
695 struct omap_mux_partition *partition,
696 int gpio)
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800697{
698 struct omap_mux_entry *e;
Benoit Cousson112485e2010-08-16 10:55:35 +0200699 struct omap_mux *ret = NULL;
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800700
Benoit Cousson112485e2010-08-16 10:55:35 +0200701 list_for_each_entry(e, &partition->muxmodes, node) {
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800702 struct omap_mux *m = &e->mux;
703 if (m->gpio == gpio) {
Benoit Cousson112485e2010-08-16 10:55:35 +0200704 ret = m;
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800705 break;
706 }
707 }
708
Benoit Cousson112485e2010-08-16 10:55:35 +0200709 return ret;
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800710}
711
712/* Needed for dynamic muxing of GPIO pins for off-idle */
713u16 omap_mux_get_gpio(int gpio)
714{
Benoit Cousson112485e2010-08-16 10:55:35 +0200715 struct omap_mux_partition *partition;
716 struct omap_mux *m;
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800717
Benoit Cousson112485e2010-08-16 10:55:35 +0200718 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);
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800722 }
723
Benoit Cousson112485e2010-08-16 10:55:35 +0200724 if (!m || m->reg_offset == OMAP_MUX_TERMINATOR)
Dan Murphy032a6422010-11-15 09:50:50 -0600725 pr_err("%s: Could not get gpio%i\n", __func__, gpio);
Benoit Cousson112485e2010-08-16 10:55:35 +0200726
727 return OMAP_MUX_TERMINATOR;
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800728}
729
730/* Needed for dynamic muxing of GPIO pins for off-idle */
731void omap_mux_set_gpio(u16 val, int gpio)
732{
Benoit Cousson112485e2010-08-16 10:55:35 +0200733 struct omap_mux_partition *partition;
734 struct omap_mux *m = NULL;
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800735
Benoit Cousson112485e2010-08-16 10:55:35 +0200736 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 }
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800742 }
743
Benoit Cousson112485e2010-08-16 10:55:35 +0200744 if (!m || m->reg_offset == OMAP_MUX_TERMINATOR)
Dan Murphy032a6422010-11-15 09:50:50 -0600745 pr_err("%s: Could not set gpio%i\n", __func__, gpio);
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800746}
747
Benoit Cousson112485e2010-08-16 10:55:35 +0200748static struct omap_mux * __init omap_mux_list_add(
749 struct omap_mux_partition *partition,
750 struct omap_mux *src)
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800751{
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);
Benoit Cousson112485e2010-08-16 10:55:35 +0200770 list_add_tail(&entry->node, &partition->muxmodes);
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800771 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 */
Benoit Cousson112485e2010-08-16 10:55:35 +0200781static void __init omap_mux_init_list(struct omap_mux_partition *partition,
782 struct omap_mux *superset)
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800783{
784 while (superset->reg_offset != OMAP_MUX_TERMINATOR) {
785 struct omap_mux *entry;
786
Ranjith Lohithakshanb72c7d52010-02-17 17:17:01 +0000787#ifdef CONFIG_OMAP_MUX
788 if (!superset->muxnames || !superset->muxnames[0]) {
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800789 superset++;
790 continue;
791 }
Ranjith Lohithakshanb72c7d52010-02-17 17:17:01 +0000792#else
793 /* Skip pins that are not muxed as GPIO by bootloader */
Benoit Cousson112485e2010-08-16 10:55:35 +0200794 if (!OMAP_MODE_GPIO(omap_mux_read(partition,
795 superset->reg_offset))) {
Tony Lindgren9ecef432010-02-01 11:22:54 -0800796 superset++;
797 continue;
798 }
799#endif
800
Benoit Cousson112485e2010-08-16 10:55:35 +0200801 entry = omap_mux_list_add(partition, superset);
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800802 if (!entry) {
Dan Murphy032a6422010-11-15 09:50:50 -0600803 pr_err("%s: Could not add entry\n", __func__);
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800804 return;
805 }
806 superset++;
807 }
808}
809
Tony Lindgren321cfc82010-02-15 10:03:35 -0800810#ifdef CONFIG_OMAP_MUX
811
812static 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
Benoit Cousson112485e2010-08-16 10:55:35 +0200822static void omap_mux_init_signals(struct omap_mux_partition *partition,
823 struct omap_board_mux *board_mux)
Tony Lindgren321cfc82010-02-15 10:03:35 -0800824{
825 omap_mux_set_cmdline_signals();
Benoit Cousson112485e2010-08-16 10:55:35 +0200826 omap_mux_write_array(partition, board_mux);
Tony Lindgren321cfc82010-02-15 10:03:35 -0800827}
828
829#else
830
831static void omap_mux_init_package(struct omap_mux *superset,
832 struct omap_mux *package_subset,
833 struct omap_ball *package_balls)
834{
835}
836
Benoit Cousson112485e2010-08-16 10:55:35 +0200837static void omap_mux_init_signals(struct omap_mux_partition *partition,
838 struct omap_board_mux *board_mux)
Tony Lindgren321cfc82010-02-15 10:03:35 -0800839{
840}
841
842#endif
843
Benoit Cousson112485e2010-08-16 10:55:35 +0200844static u32 mux_partitions_cnt;
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800845
Benoit Cousson112485e2010-08-16 10:55:35 +0200846int __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) {
Dan Murphy032a6422010-11-15 09:50:50 -0600865 pr_err("%s: Could not ioremap mux partition at 0x%08x\n",
866 __func__, partition->phys);
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800867 return -ENODEV;
868 }
869
Benoit Cousson112485e2010-08-16 10:55:35 +0200870 INIT_LIST_HEAD(&partition->muxmodes);
871
872 list_add_tail(&partition->node, &mux_partitions);
873 mux_partitions_cnt++;
Dan Murphy032a6422010-11-15 09:50:50 -0600874 pr_info("%s: Add partition: #%d: %s, flags: %x\n", __func__,
Benoit Cousson112485e2010-08-16 10:55:35 +0200875 mux_partitions_cnt, partition->name, partition->flags);
Tony Lindgrend5425be2010-07-05 16:31:35 +0300876
Tony Lindgren321cfc82010-02-15 10:03:35 -0800877 omap_mux_init_package(superset, package_subset, package_balls);
Benoit Cousson112485e2010-08-16 10:55:35 +0200878 omap_mux_init_list(partition, superset);
879 omap_mux_init_signals(partition, board_mux);
Tony Lindgren2cb0c542010-01-19 18:17:07 -0800880
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800881 return 0;
882}
883