blob: 0fa3d74125bc25c5f48ee5c2507ca1c52cf19077 [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
Tony Lindgren8419fdb2010-12-22 18:42:35 -0800154static int __init _omap_mux_get_by_name(struct omap_mux_partition *partition,
155 const char *muxname,
156 struct omap_mux **found_mux)
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800157{
Tony Lindgren8419fdb2010-12-22 18:42:35 -0800158 struct omap_mux *mux = NULL;
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800159 struct omap_mux_entry *e;
Tony Lindgren5a3b2f72010-10-01 16:35:24 -0700160 const char *mode_name;
Tony Lindgren8419fdb2010-12-22 18:42:35 -0800161 int found = 0, found_mode, mode0_len = 0;
Benoit Cousson112485e2010-08-16 10:55:35 +0200162 struct list_head *muxmodes = &partition->muxmodes;
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800163
164 mode_name = strchr(muxname, '.');
165 if (mode_name) {
Tony Lindgren5a3b2f72010-10-01 16:35:24 -0700166 mode0_len = strlen(muxname) - strlen(mode_name);
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800167 mode_name++;
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800168 } else {
169 mode_name = muxname;
170 }
171
Benoit Cousson112485e2010-08-16 10:55:35 +0200172 list_for_each_entry(e, muxmodes, node) {
Tony Lindgren8419fdb2010-12-22 18:42:35 -0800173 char *m0_entry;
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800174 int i;
175
Tony Lindgren8419fdb2010-12-22 18:42:35 -0800176 mux = &e->mux;
177 m0_entry = mux->muxnames[0];
178
Tony Lindgren5a3b2f72010-10-01 16:35:24 -0700179 /* First check for full name in mode0.muxmode format */
180 if (mode0_len && strncmp(muxname, m0_entry, mode0_len))
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800181 continue;
182
Tony Lindgren5a3b2f72010-10-01 16:35:24 -0700183 /* Then check for muxmode only */
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800184 for (i = 0; i < OMAP_MUX_NR_MODES; i++) {
Tony Lindgren8419fdb2010-12-22 18:42:35 -0800185 char *mode_cur = mux->muxnames[i];
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800186
187 if (!mode_cur)
188 continue;
189
190 if (!strcmp(mode_name, mode_cur)) {
Tony Lindgren8419fdb2010-12-22 18:42:35 -0800191 *found_mux = mux;
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800192 found++;
Tony Lindgren8419fdb2010-12-22 18:42:35 -0800193 found_mode = i;
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800194 }
195 }
196 }
197
Tony Lindgren8419fdb2010-12-22 18:42:35 -0800198 if (found == 1) {
199 return found_mode;
200 }
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800201
202 if (found > 1) {
Dan Murphy032a6422010-11-15 09:50:50 -0600203 pr_err("%s: Multiple signal paths (%i) for %s\n", __func__,
Benoit Cousson1cbb3a92010-08-10 17:33:01 +0200204 found, muxname);
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800205 return -EINVAL;
206 }
207
Tony Lindgren8419fdb2010-12-22 18:42:35 -0800208 pr_err("%s: Could not find signal %s\n", __func__, muxname);
209
210 return -ENODEV;
211}
212
213static int __init
214omap_mux_get_by_name(const char *muxname,
215 struct omap_mux_partition **found_partition,
216 struct omap_mux **found_mux)
217{
218 struct omap_mux_partition *partition;
219
220 list_for_each_entry(partition, &mux_partitions, node) {
221 struct omap_mux *mux = NULL;
222 int mux_mode = _omap_mux_get_by_name(partition, muxname, &mux);
223 if (mux_mode < 0)
224 continue;
225
226 *found_partition = partition;
227 *found_mux = mux;
228
229 return mux_mode;
230 }
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800231
232 return -ENODEV;
233}
234
Benoit Cousson112485e2010-08-16 10:55:35 +0200235int __init omap_mux_init_signal(const char *muxname, int val)
236{
Tony Lindgren8419fdb2010-12-22 18:42:35 -0800237 struct omap_mux_partition *partition = NULL;
238 struct omap_mux *mux = NULL;
239 u16 old_mode;
240 int mux_mode;
Benoit Cousson112485e2010-08-16 10:55:35 +0200241
Tony Lindgren8419fdb2010-12-22 18:42:35 -0800242 mux_mode = omap_mux_get_by_name(muxname, &partition, &mux);
243 if (mux_mode < 0)
244 return mux_mode;
Benoit Cousson112485e2010-08-16 10:55:35 +0200245
Tony Lindgren8419fdb2010-12-22 18:42:35 -0800246 old_mode = omap_mux_read(partition, mux->reg_offset);
247 mux_mode |= val;
248 pr_debug("%s: Setting signal %s 0x%04x -> 0x%04x\n",
249 __func__, muxname, old_mode, mux_mode);
250 omap_mux_write(partition, mux_mode, mux->reg_offset);
Benoit Cousson112485e2010-08-16 10:55:35 +0200251
Tony Lindgren8419fdb2010-12-22 18:42:35 -0800252 return 0;
Benoit Cousson112485e2010-08-16 10:55:35 +0200253}
254
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800255#ifdef CONFIG_DEBUG_FS
256
257#define OMAP_MUX_MAX_NR_FLAGS 10
258#define OMAP_MUX_TEST_FLAG(val, mask) \
259 if (((val) & (mask)) == (mask)) { \
260 i++; \
261 flags[i] = #mask; \
262 }
263
264/* REVISIT: Add checking for non-optimal mux settings */
265static inline void omap_mux_decode(struct seq_file *s, u16 val)
266{
267 char *flags[OMAP_MUX_MAX_NR_FLAGS];
Tony Lindgren78737ae2010-02-01 13:03:42 -0800268 char mode[sizeof("OMAP_MUX_MODE") + 1];
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800269 int i = -1;
270
271 sprintf(mode, "OMAP_MUX_MODE%d", val & 0x7);
272 i++;
273 flags[i] = mode;
274
275 OMAP_MUX_TEST_FLAG(val, OMAP_PIN_OFF_WAKEUPENABLE);
276 if (val & OMAP_OFF_EN) {
277 if (!(val & OMAP_OFFOUT_EN)) {
278 if (!(val & OMAP_OFF_PULL_UP)) {
279 OMAP_MUX_TEST_FLAG(val,
280 OMAP_PIN_OFF_INPUT_PULLDOWN);
281 } else {
282 OMAP_MUX_TEST_FLAG(val,
283 OMAP_PIN_OFF_INPUT_PULLUP);
284 }
285 } else {
286 if (!(val & OMAP_OFFOUT_VAL)) {
287 OMAP_MUX_TEST_FLAG(val,
288 OMAP_PIN_OFF_OUTPUT_LOW);
289 } else {
290 OMAP_MUX_TEST_FLAG(val,
291 OMAP_PIN_OFF_OUTPUT_HIGH);
292 }
293 }
294 }
295
296 if (val & OMAP_INPUT_EN) {
297 if (val & OMAP_PULL_ENA) {
298 if (!(val & OMAP_PULL_UP)) {
299 OMAP_MUX_TEST_FLAG(val,
300 OMAP_PIN_INPUT_PULLDOWN);
301 } else {
302 OMAP_MUX_TEST_FLAG(val, OMAP_PIN_INPUT_PULLUP);
303 }
304 } else {
305 OMAP_MUX_TEST_FLAG(val, OMAP_PIN_INPUT);
306 }
307 } else {
308 i++;
309 flags[i] = "OMAP_PIN_OUTPUT";
310 }
311
312 do {
313 seq_printf(s, "%s", flags[i]);
314 if (i > 0)
315 seq_printf(s, " | ");
316 } while (i-- > 0);
317}
318
Benoit Cousson112485e2010-08-16 10:55:35 +0200319#define OMAP_MUX_DEFNAME_LEN 32
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800320
321static int omap_mux_dbg_board_show(struct seq_file *s, void *unused)
322{
Benoit Cousson112485e2010-08-16 10:55:35 +0200323 struct omap_mux_partition *partition = s->private;
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800324 struct omap_mux_entry *e;
Benoit Cousson112485e2010-08-16 10:55:35 +0200325 u8 omap_gen = omap_rev() >> 28;
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800326
Benoit Cousson112485e2010-08-16 10:55:35 +0200327 list_for_each_entry(e, &partition->muxmodes, node) {
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800328 struct omap_mux *m = &e->mux;
329 char m0_def[OMAP_MUX_DEFNAME_LEN];
330 char *m0_name = m->muxnames[0];
331 u16 val;
332 int i, mode;
333
334 if (!m0_name)
335 continue;
336
Tony Lindgren78737ae2010-02-01 13:03:42 -0800337 /* REVISIT: Needs to be updated if mode0 names get longer */
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800338 for (i = 0; i < OMAP_MUX_DEFNAME_LEN; i++) {
339 if (m0_name[i] == '\0') {
340 m0_def[i] = m0_name[i];
341 break;
342 }
343 m0_def[i] = toupper(m0_name[i]);
344 }
Benoit Cousson112485e2010-08-16 10:55:35 +0200345 val = omap_mux_read(partition, m->reg_offset);
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800346 mode = val & OMAP_MUX_MODE7;
Benoit Cousson112485e2010-08-16 10:55:35 +0200347 if (mode != 0)
348 seq_printf(s, "/* %s */\n", m->muxnames[mode]);
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800349
Benoit Cousson112485e2010-08-16 10:55:35 +0200350 /*
351 * XXX: Might be revisited to support differences accross
352 * same OMAP generation.
353 */
354 seq_printf(s, "OMAP%d_MUX(%s, ", omap_gen, m0_def);
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800355 omap_mux_decode(s, val);
356 seq_printf(s, "),\n");
357 }
358
359 return 0;
360}
361
362static int omap_mux_dbg_board_open(struct inode *inode, struct file *file)
363{
Benoit Cousson112485e2010-08-16 10:55:35 +0200364 return single_open(file, omap_mux_dbg_board_show, inode->i_private);
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800365}
366
367static const struct file_operations omap_mux_dbg_board_fops = {
368 .open = omap_mux_dbg_board_open,
369 .read = seq_read,
370 .llseek = seq_lseek,
371 .release = single_release,
372};
373
Benoit Cousson112485e2010-08-16 10:55:35 +0200374static struct omap_mux_partition *omap_mux_get_partition(struct omap_mux *mux)
375{
376 struct omap_mux_partition *partition;
377
378 list_for_each_entry(partition, &mux_partitions, node) {
379 struct list_head *muxmodes = &partition->muxmodes;
380 struct omap_mux_entry *e;
381
382 list_for_each_entry(e, muxmodes, node) {
383 struct omap_mux *m = &e->mux;
384
385 if (m == mux)
386 return partition;
387 }
388 }
389
390 return NULL;
391}
392
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800393static int omap_mux_dbg_signal_show(struct seq_file *s, void *unused)
394{
395 struct omap_mux *m = s->private;
Benoit Cousson112485e2010-08-16 10:55:35 +0200396 struct omap_mux_partition *partition;
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800397 const char *none = "NA";
398 u16 val;
399 int mode;
400
Benoit Cousson112485e2010-08-16 10:55:35 +0200401 partition = omap_mux_get_partition(m);
402 if (!partition)
403 return 0;
404
405 val = omap_mux_read(partition, m->reg_offset);
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800406 mode = val & OMAP_MUX_MODE7;
407
Benoit Cousson112485e2010-08-16 10:55:35 +0200408 seq_printf(s, "name: %s.%s (0x%08x/0x%03x = 0x%04x), b %s, t %s\n",
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800409 m->muxnames[0], m->muxnames[mode],
Benoit Cousson112485e2010-08-16 10:55:35 +0200410 partition->phys + m->reg_offset, m->reg_offset, val,
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800411 m->balls[0] ? m->balls[0] : none,
412 m->balls[1] ? m->balls[1] : none);
413 seq_printf(s, "mode: ");
414 omap_mux_decode(s, val);
415 seq_printf(s, "\n");
416 seq_printf(s, "signals: %s | %s | %s | %s | %s | %s | %s | %s\n",
417 m->muxnames[0] ? m->muxnames[0] : none,
418 m->muxnames[1] ? m->muxnames[1] : none,
419 m->muxnames[2] ? m->muxnames[2] : none,
420 m->muxnames[3] ? m->muxnames[3] : none,
421 m->muxnames[4] ? m->muxnames[4] : none,
422 m->muxnames[5] ? m->muxnames[5] : none,
423 m->muxnames[6] ? m->muxnames[6] : none,
424 m->muxnames[7] ? m->muxnames[7] : none);
425
426 return 0;
427}
428
429#define OMAP_MUX_MAX_ARG_CHAR 7
430
431static ssize_t omap_mux_dbg_signal_write(struct file *file,
Benoit Cousson112485e2010-08-16 10:55:35 +0200432 const char __user *user_buf,
433 size_t count, loff_t *ppos)
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800434{
435 char buf[OMAP_MUX_MAX_ARG_CHAR];
436 struct seq_file *seqf;
437 struct omap_mux *m;
438 unsigned long val;
439 int buf_size, ret;
Benoit Cousson112485e2010-08-16 10:55:35 +0200440 struct omap_mux_partition *partition;
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800441
442 if (count > OMAP_MUX_MAX_ARG_CHAR)
443 return -EINVAL;
444
445 memset(buf, 0, sizeof(buf));
446 buf_size = min(count, sizeof(buf) - 1);
447
448 if (copy_from_user(buf, user_buf, buf_size))
449 return -EFAULT;
450
451 ret = strict_strtoul(buf, 0x10, &val);
452 if (ret < 0)
453 return ret;
454
455 if (val > 0xffff)
456 return -EINVAL;
457
458 seqf = file->private_data;
459 m = seqf->private;
460
Benoit Cousson112485e2010-08-16 10:55:35 +0200461 partition = omap_mux_get_partition(m);
462 if (!partition)
463 return -ENODEV;
464
465 omap_mux_write(partition, (u16)val, m->reg_offset);
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800466 *ppos += count;
467
468 return count;
469}
470
471static int omap_mux_dbg_signal_open(struct inode *inode, struct file *file)
472{
473 return single_open(file, omap_mux_dbg_signal_show, inode->i_private);
474}
475
476static const struct file_operations omap_mux_dbg_signal_fops = {
477 .open = omap_mux_dbg_signal_open,
478 .read = seq_read,
479 .write = omap_mux_dbg_signal_write,
480 .llseek = seq_lseek,
481 .release = single_release,
482};
483
484static struct dentry *mux_dbg_dir;
485
Benoit Cousson112485e2010-08-16 10:55:35 +0200486static void __init omap_mux_dbg_create_entry(
487 struct omap_mux_partition *partition,
488 struct dentry *mux_dbg_dir)
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800489{
490 struct omap_mux_entry *e;
491
Benoit Cousson112485e2010-08-16 10:55:35 +0200492 list_for_each_entry(e, &partition->muxmodes, node) {
493 struct omap_mux *m = &e->mux;
494
495 (void)debugfs_create_file(m->muxnames[0], S_IWUGO, mux_dbg_dir,
496 m, &omap_mux_dbg_signal_fops);
497 }
498}
499
500static void __init omap_mux_dbg_init(void)
501{
502 struct omap_mux_partition *partition;
503 static struct dentry *mux_dbg_board_dir;
504
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800505 mux_dbg_dir = debugfs_create_dir("omap_mux", NULL);
506 if (!mux_dbg_dir)
507 return;
508
Benoit Cousson112485e2010-08-16 10:55:35 +0200509 mux_dbg_board_dir = debugfs_create_dir("board", mux_dbg_dir);
510 if (!mux_dbg_board_dir)
511 return;
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800512
Benoit Cousson112485e2010-08-16 10:55:35 +0200513 list_for_each_entry(partition, &mux_partitions, node) {
514 omap_mux_dbg_create_entry(partition, mux_dbg_dir);
515 (void)debugfs_create_file(partition->name, S_IRUGO,
516 mux_dbg_board_dir, partition,
517 &omap_mux_dbg_board_fops);
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800518 }
519}
520
521#else
522static inline void omap_mux_dbg_init(void)
523{
524}
525#endif /* CONFIG_DEBUG_FS */
526
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800527static void __init omap_mux_free_names(struct omap_mux *m)
528{
529 int i;
530
531 for (i = 0; i < OMAP_MUX_NR_MODES; i++)
532 kfree(m->muxnames[i]);
533
534#ifdef CONFIG_DEBUG_FS
535 for (i = 0; i < OMAP_MUX_NR_SIDES; i++)
536 kfree(m->balls[i]);
Tony Lindgren1dbae812005-11-10 14:26:51 +0000537#endif
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800538
539}
540
541/* Free all data except for GPIO pins unless CONFIG_DEBUG_FS is set */
542static int __init omap_mux_late_init(void)
543{
Benoit Cousson112485e2010-08-16 10:55:35 +0200544 struct omap_mux_partition *partition;
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800545
Benoit Cousson112485e2010-08-16 10:55:35 +0200546 list_for_each_entry(partition, &mux_partitions, node) {
547 struct omap_mux_entry *e, *tmp;
548 list_for_each_entry_safe(e, tmp, &partition->muxmodes, node) {
549 struct omap_mux *m = &e->mux;
550 u16 mode = omap_mux_read(partition, m->reg_offset);
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800551
Benoit Cousson112485e2010-08-16 10:55:35 +0200552 if (OMAP_MODE_GPIO(mode))
553 continue;
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800554
555#ifndef CONFIG_DEBUG_FS
Benoit Cousson112485e2010-08-16 10:55:35 +0200556 mutex_lock(&muxmode_mutex);
557 list_del(&e->node);
558 mutex_unlock(&muxmode_mutex);
559 omap_mux_free_names(m);
560 kfree(m);
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800561#endif
Benoit Cousson112485e2010-08-16 10:55:35 +0200562 }
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800563 }
564
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800565 omap_mux_dbg_init();
566
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800567 return 0;
568}
569late_initcall(omap_mux_late_init);
570
571static void __init omap_mux_package_fixup(struct omap_mux *p,
572 struct omap_mux *superset)
573{
574 while (p->reg_offset != OMAP_MUX_TERMINATOR) {
575 struct omap_mux *s = superset;
576 int found = 0;
577
578 while (s->reg_offset != OMAP_MUX_TERMINATOR) {
579 if (s->reg_offset == p->reg_offset) {
580 *s = *p;
581 found++;
582 break;
583 }
584 s++;
585 }
586 if (!found)
Dan Murphy032a6422010-11-15 09:50:50 -0600587 pr_err("%s: Unknown entry offset 0x%x\n", __func__,
Benoit Cousson1cbb3a92010-08-10 17:33:01 +0200588 p->reg_offset);
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800589 p++;
590 }
591}
592
593#ifdef CONFIG_DEBUG_FS
594
595static void __init omap_mux_package_init_balls(struct omap_ball *b,
596 struct omap_mux *superset)
597{
598 while (b->reg_offset != OMAP_MUX_TERMINATOR) {
599 struct omap_mux *s = superset;
600 int found = 0;
601
602 while (s->reg_offset != OMAP_MUX_TERMINATOR) {
603 if (s->reg_offset == b->reg_offset) {
604 s->balls[0] = b->balls[0];
605 s->balls[1] = b->balls[1];
606 found++;
607 break;
608 }
609 s++;
610 }
611 if (!found)
Dan Murphy032a6422010-11-15 09:50:50 -0600612 pr_err("%s: Unknown ball offset 0x%x\n", __func__,
Benoit Cousson1cbb3a92010-08-10 17:33:01 +0200613 b->reg_offset);
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800614 b++;
615 }
616}
617
618#else /* CONFIG_DEBUG_FS */
619
620static inline void omap_mux_package_init_balls(struct omap_ball *b,
621 struct omap_mux *superset)
622{
623}
624
625#endif /* CONFIG_DEBUG_FS */
626
627static int __init omap_mux_setup(char *options)
628{
629 if (!options)
630 return 0;
631
632 omap_mux_options = options;
633
634 return 1;
635}
636__setup("omap_mux=", omap_mux_setup);
637
638/*
639 * Note that the omap_mux=some.signal1=0x1234,some.signal2=0x1234
640 * cmdline options only override the bootloader values.
641 * During development, please enable CONFIG_DEBUG_FS, and use the
642 * signal specific entries under debugfs.
643 */
644static void __init omap_mux_set_cmdline_signals(void)
645{
646 char *options, *next_opt, *token;
647
648 if (!omap_mux_options)
649 return;
650
651 options = kmalloc(strlen(omap_mux_options) + 1, GFP_KERNEL);
652 if (!options)
653 return;
654
655 strcpy(options, omap_mux_options);
656 next_opt = options;
657
658 while ((token = strsep(&next_opt, ",")) != NULL) {
659 char *keyval, *name;
660 unsigned long val;
661
662 keyval = token;
663 name = strsep(&keyval, "=");
664 if (name) {
665 int res;
666
667 res = strict_strtoul(keyval, 0x10, &val);
668 if (res < 0)
669 continue;
670
671 omap_mux_init_signal(name, (u16)val);
672 }
673 }
674
675 kfree(options);
676}
677
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800678static int __init omap_mux_copy_names(struct omap_mux *src,
Benoit Cousson112485e2010-08-16 10:55:35 +0200679 struct omap_mux *dst)
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800680{
681 int i;
682
683 for (i = 0; i < OMAP_MUX_NR_MODES; i++) {
684 if (src->muxnames[i]) {
685 dst->muxnames[i] =
686 kmalloc(strlen(src->muxnames[i]) + 1,
687 GFP_KERNEL);
688 if (!dst->muxnames[i])
689 goto free;
690 strcpy(dst->muxnames[i], src->muxnames[i]);
691 }
692 }
693
694#ifdef CONFIG_DEBUG_FS
695 for (i = 0; i < OMAP_MUX_NR_SIDES; i++) {
696 if (src->balls[i]) {
697 dst->balls[i] =
698 kmalloc(strlen(src->balls[i]) + 1,
699 GFP_KERNEL);
700 if (!dst->balls[i])
701 goto free;
702 strcpy(dst->balls[i], src->balls[i]);
703 }
704 }
705#endif
706
707 return 0;
708
709free:
710 omap_mux_free_names(dst);
711 return -ENOMEM;
712
713}
714
715#endif /* CONFIG_OMAP_MUX */
716
Benoit Cousson112485e2010-08-16 10:55:35 +0200717static struct omap_mux *omap_mux_get_by_gpio(
718 struct omap_mux_partition *partition,
719 int gpio)
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800720{
721 struct omap_mux_entry *e;
Benoit Cousson112485e2010-08-16 10:55:35 +0200722 struct omap_mux *ret = NULL;
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800723
Benoit Cousson112485e2010-08-16 10:55:35 +0200724 list_for_each_entry(e, &partition->muxmodes, node) {
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800725 struct omap_mux *m = &e->mux;
726 if (m->gpio == gpio) {
Benoit Cousson112485e2010-08-16 10:55:35 +0200727 ret = m;
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800728 break;
729 }
730 }
731
Benoit Cousson112485e2010-08-16 10:55:35 +0200732 return ret;
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800733}
734
735/* Needed for dynamic muxing of GPIO pins for off-idle */
736u16 omap_mux_get_gpio(int gpio)
737{
Benoit Cousson112485e2010-08-16 10:55:35 +0200738 struct omap_mux_partition *partition;
739 struct omap_mux *m;
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800740
Benoit Cousson112485e2010-08-16 10:55:35 +0200741 list_for_each_entry(partition, &mux_partitions, node) {
742 m = omap_mux_get_by_gpio(partition, gpio);
743 if (m)
744 return omap_mux_read(partition, m->reg_offset);
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800745 }
746
Benoit Cousson112485e2010-08-16 10:55:35 +0200747 if (!m || m->reg_offset == OMAP_MUX_TERMINATOR)
Dan Murphy032a6422010-11-15 09:50:50 -0600748 pr_err("%s: Could not get gpio%i\n", __func__, gpio);
Benoit Cousson112485e2010-08-16 10:55:35 +0200749
750 return OMAP_MUX_TERMINATOR;
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800751}
752
753/* Needed for dynamic muxing of GPIO pins for off-idle */
754void omap_mux_set_gpio(u16 val, int gpio)
755{
Benoit Cousson112485e2010-08-16 10:55:35 +0200756 struct omap_mux_partition *partition;
757 struct omap_mux *m = NULL;
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800758
Benoit Cousson112485e2010-08-16 10:55:35 +0200759 list_for_each_entry(partition, &mux_partitions, node) {
760 m = omap_mux_get_by_gpio(partition, gpio);
761 if (m) {
762 omap_mux_write(partition, val, m->reg_offset);
763 return;
764 }
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800765 }
766
Benoit Cousson112485e2010-08-16 10:55:35 +0200767 if (!m || m->reg_offset == OMAP_MUX_TERMINATOR)
Dan Murphy032a6422010-11-15 09:50:50 -0600768 pr_err("%s: Could not set gpio%i\n", __func__, gpio);
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800769}
770
Benoit Cousson112485e2010-08-16 10:55:35 +0200771static struct omap_mux * __init omap_mux_list_add(
772 struct omap_mux_partition *partition,
773 struct omap_mux *src)
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800774{
775 struct omap_mux_entry *entry;
776 struct omap_mux *m;
777
778 entry = kzalloc(sizeof(struct omap_mux_entry), GFP_KERNEL);
779 if (!entry)
780 return NULL;
781
782 m = &entry->mux;
783 memcpy(m, src, sizeof(struct omap_mux_entry));
784
785#ifdef CONFIG_OMAP_MUX
786 if (omap_mux_copy_names(src, m)) {
787 kfree(entry);
788 return NULL;
789 }
790#endif
791
792 mutex_lock(&muxmode_mutex);
Benoit Cousson112485e2010-08-16 10:55:35 +0200793 list_add_tail(&entry->node, &partition->muxmodes);
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800794 mutex_unlock(&muxmode_mutex);
795
796 return m;
797}
798
799/*
800 * Note if CONFIG_OMAP_MUX is not selected, we will only initialize
801 * the GPIO to mux offset mapping that is needed for dynamic muxing
802 * of GPIO pins for off-idle.
803 */
Benoit Cousson112485e2010-08-16 10:55:35 +0200804static void __init omap_mux_init_list(struct omap_mux_partition *partition,
805 struct omap_mux *superset)
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800806{
807 while (superset->reg_offset != OMAP_MUX_TERMINATOR) {
808 struct omap_mux *entry;
809
Ranjith Lohithakshanb72c7d52010-02-17 17:17:01 +0000810#ifdef CONFIG_OMAP_MUX
811 if (!superset->muxnames || !superset->muxnames[0]) {
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800812 superset++;
813 continue;
814 }
Ranjith Lohithakshanb72c7d52010-02-17 17:17:01 +0000815#else
816 /* Skip pins that are not muxed as GPIO by bootloader */
Benoit Cousson112485e2010-08-16 10:55:35 +0200817 if (!OMAP_MODE_GPIO(omap_mux_read(partition,
818 superset->reg_offset))) {
Tony Lindgren9ecef432010-02-01 11:22:54 -0800819 superset++;
820 continue;
821 }
822#endif
823
Benoit Cousson112485e2010-08-16 10:55:35 +0200824 entry = omap_mux_list_add(partition, superset);
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800825 if (!entry) {
Dan Murphy032a6422010-11-15 09:50:50 -0600826 pr_err("%s: Could not add entry\n", __func__);
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800827 return;
828 }
829 superset++;
830 }
831}
832
Tony Lindgren321cfc82010-02-15 10:03:35 -0800833#ifdef CONFIG_OMAP_MUX
834
835static void omap_mux_init_package(struct omap_mux *superset,
836 struct omap_mux *package_subset,
837 struct omap_ball *package_balls)
838{
839 if (package_subset)
840 omap_mux_package_fixup(package_subset, superset);
841 if (package_balls)
842 omap_mux_package_init_balls(package_balls, superset);
843}
844
Benoit Cousson112485e2010-08-16 10:55:35 +0200845static void omap_mux_init_signals(struct omap_mux_partition *partition,
846 struct omap_board_mux *board_mux)
Tony Lindgren321cfc82010-02-15 10:03:35 -0800847{
848 omap_mux_set_cmdline_signals();
Benoit Cousson112485e2010-08-16 10:55:35 +0200849 omap_mux_write_array(partition, board_mux);
Tony Lindgren321cfc82010-02-15 10:03:35 -0800850}
851
852#else
853
854static void omap_mux_init_package(struct omap_mux *superset,
855 struct omap_mux *package_subset,
856 struct omap_ball *package_balls)
857{
858}
859
Benoit Cousson112485e2010-08-16 10:55:35 +0200860static void omap_mux_init_signals(struct omap_mux_partition *partition,
861 struct omap_board_mux *board_mux)
Tony Lindgren321cfc82010-02-15 10:03:35 -0800862{
863}
864
865#endif
866
Benoit Cousson112485e2010-08-16 10:55:35 +0200867static u32 mux_partitions_cnt;
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800868
Benoit Cousson112485e2010-08-16 10:55:35 +0200869int __init omap_mux_init(const char *name, u32 flags,
870 u32 mux_pbase, u32 mux_size,
871 struct omap_mux *superset,
872 struct omap_mux *package_subset,
873 struct omap_board_mux *board_mux,
874 struct omap_ball *package_balls)
875{
876 struct omap_mux_partition *partition;
877
878 partition = kzalloc(sizeof(struct omap_mux_partition), GFP_KERNEL);
879 if (!partition)
880 return -ENOMEM;
881
882 partition->name = name;
883 partition->flags = flags;
884 partition->size = mux_size;
885 partition->phys = mux_pbase;
886 partition->base = ioremap(mux_pbase, mux_size);
887 if (!partition->base) {
Dan Murphy032a6422010-11-15 09:50:50 -0600888 pr_err("%s: Could not ioremap mux partition at 0x%08x\n",
889 __func__, partition->phys);
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800890 return -ENODEV;
891 }
892
Benoit Cousson112485e2010-08-16 10:55:35 +0200893 INIT_LIST_HEAD(&partition->muxmodes);
894
895 list_add_tail(&partition->node, &mux_partitions);
896 mux_partitions_cnt++;
Dan Murphy032a6422010-11-15 09:50:50 -0600897 pr_info("%s: Add partition: #%d: %s, flags: %x\n", __func__,
Benoit Cousson112485e2010-08-16 10:55:35 +0200898 mux_partitions_cnt, partition->name, partition->flags);
Tony Lindgrend5425be2010-07-05 16:31:35 +0300899
Tony Lindgren321cfc82010-02-15 10:03:35 -0800900 omap_mux_init_package(superset, package_subset, package_balls);
Benoit Cousson112485e2010-08-16 10:55:35 +0200901 omap_mux_init_list(partition, superset);
902 omap_mux_init_signals(partition, board_mux);
Tony Lindgren2cb0c542010-01-19 18:17:07 -0800903
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800904 return 0;
905}
906