blob: 27eb51a224cb315b4302fbe08cbb43447954c8d4 [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
Tony Lindgren9796b322010-12-22 18:42:35 -080038#include <plat/omap_hwmod.h>
39
Paul Walmsley4814ced2010-10-08 11:40:20 -060040#include "control.h"
Tony Lindgren15ac7af2009-12-11 16:16:32 -080041#include "mux.h"
Tony Lindgren1dbae812005-11-10 14:26:51 +000042
Mike Rapoport92c9f502009-12-11 16:16:31 -080043#define OMAP_MUX_BASE_OFFSET 0x30 /* Offset from CTRL_BASE */
44#define OMAP_MUX_BASE_SZ 0x5ca
45
Tony Lindgren15ac7af2009-12-11 16:16:32 -080046struct omap_mux_entry {
47 struct omap_mux mux;
48 struct list_head node;
49};
50
Benoit Cousson112485e2010-08-16 10:55:35 +020051static LIST_HEAD(mux_partitions);
52static DEFINE_MUTEX(muxmode_mutex);
Mike Rapoport92c9f502009-12-11 16:16:31 -080053
Benoit Cousson112485e2010-08-16 10:55:35 +020054struct omap_mux_partition *omap_mux_get(const char *name)
Mike Rapoport92c9f502009-12-11 16:16:31 -080055{
Benoit Cousson112485e2010-08-16 10:55:35 +020056 struct omap_mux_partition *partition;
57
58 list_for_each_entry(partition, &mux_partitions, node) {
59 if (!strcmp(name, partition->name))
60 return partition;
61 }
62
63 return NULL;
Mike Rapoport92c9f502009-12-11 16:16:31 -080064}
65
Benoit Cousson112485e2010-08-16 10:55:35 +020066u16 omap_mux_read(struct omap_mux_partition *partition, u16 reg)
Mike Rapoport92c9f502009-12-11 16:16:31 -080067{
Benoit Cousson112485e2010-08-16 10:55:35 +020068 if (partition->flags & OMAP_MUX_REG_8BIT)
69 return __raw_readb(partition->base + reg);
Mike Rapoport92c9f502009-12-11 16:16:31 -080070 else
Benoit Cousson112485e2010-08-16 10:55:35 +020071 return __raw_readw(partition->base + reg);
Mike Rapoport92c9f502009-12-11 16:16:31 -080072}
Tony Lindgren7d7f6652008-01-25 00:42:48 -080073
Benoit Cousson112485e2010-08-16 10:55:35 +020074void omap_mux_write(struct omap_mux_partition *partition, u16 val,
75 u16 reg)
Tony Lindgrend4bb72e2010-01-19 15:15:24 -080076{
Benoit Cousson112485e2010-08-16 10:55:35 +020077 if (partition->flags & OMAP_MUX_REG_8BIT)
78 __raw_writeb(val, partition->base + reg);
79 else
80 __raw_writew(val, partition->base + reg);
81}
82
83void omap_mux_write_array(struct omap_mux_partition *partition,
84 struct omap_board_mux *board_mux)
85{
86 while (board_mux->reg_offset != OMAP_MUX_TERMINATOR) {
87 omap_mux_write(partition, board_mux->value,
88 board_mux->reg_offset);
Tony Lindgrend4bb72e2010-01-19 15:15:24 -080089 board_mux++;
90 }
91}
92
Tony Lindgren15ac7af2009-12-11 16:16:32 -080093#ifdef CONFIG_OMAP_MUX
94
95static char *omap_mux_options;
96
Benoit Cousson112485e2010-08-16 10:55:35 +020097static int __init _omap_mux_init_gpio(struct omap_mux_partition *partition,
98 int gpio, int val)
Tony Lindgren15ac7af2009-12-11 16:16:32 -080099{
100 struct omap_mux_entry *e;
Sanjeev Premica828762010-09-23 18:27:18 -0700101 struct omap_mux *gpio_mux = NULL;
Grazvydas Ignotas8a6f7e12010-08-02 13:18:28 +0300102 u16 old_mode;
103 u16 mux_mode;
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800104 int found = 0;
Benoit Cousson112485e2010-08-16 10:55:35 +0200105 struct list_head *muxmodes = &partition->muxmodes;
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800106
107 if (!gpio)
108 return -EINVAL;
109
Benoit Cousson112485e2010-08-16 10:55:35 +0200110 list_for_each_entry(e, muxmodes, node) {
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800111 struct omap_mux *m = &e->mux;
112 if (gpio == m->gpio) {
Grazvydas Ignotas8a6f7e12010-08-02 13:18:28 +0300113 gpio_mux = m;
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800114 found++;
115 }
116 }
117
Grazvydas Ignotas8a6f7e12010-08-02 13:18:28 +0300118 if (found == 0) {
Dan Murphy032a6422010-11-15 09:50:50 -0600119 pr_err("%s: Could not set gpio%i\n", __func__, gpio);
Grazvydas Ignotas8a6f7e12010-08-02 13:18:28 +0300120 return -ENODEV;
121 }
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800122
123 if (found > 1) {
Dan Murphy032a6422010-11-15 09:50:50 -0600124 pr_info("%s: Multiple gpio paths (%d) for gpio%i\n", __func__,
Benoit Cousson1cbb3a92010-08-10 17:33:01 +0200125 found, gpio);
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800126 return -EINVAL;
127 }
128
Benoit Cousson112485e2010-08-16 10:55:35 +0200129 old_mode = omap_mux_read(partition, gpio_mux->reg_offset);
Grazvydas Ignotas8a6f7e12010-08-02 13:18:28 +0300130 mux_mode = val & ~(OMAP_MUX_NR_MODES - 1);
Benoit Cousson112485e2010-08-16 10:55:35 +0200131 if (partition->flags & OMAP_MUX_GPIO_IN_MODE3)
Grazvydas Ignotas8a6f7e12010-08-02 13:18:28 +0300132 mux_mode |= OMAP_MUX_MODE3;
133 else
134 mux_mode |= OMAP_MUX_MODE4;
Dan Murphy032a6422010-11-15 09:50:50 -0600135 pr_debug("%s: Setting signal %s.gpio%i 0x%04x -> 0x%04x\n", __func__,
Benoit Cousson1cbb3a92010-08-10 17:33:01 +0200136 gpio_mux->muxnames[0], gpio, old_mode, mux_mode);
Benoit Cousson112485e2010-08-16 10:55:35 +0200137 omap_mux_write(partition, mux_mode, gpio_mux->reg_offset);
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800138
Grazvydas Ignotas8a6f7e12010-08-02 13:18:28 +0300139 return 0;
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800140}
141
Benoit Cousson112485e2010-08-16 10:55:35 +0200142int __init omap_mux_init_gpio(int gpio, int val)
143{
144 struct omap_mux_partition *partition;
145 int ret;
146
147 list_for_each_entry(partition, &mux_partitions, node) {
148 ret = _omap_mux_init_gpio(partition, gpio, val);
149 if (!ret)
150 return ret;
151 }
152
153 return -ENODEV;
154}
155
Tony Lindgren8419fdb2010-12-22 18:42:35 -0800156static int __init _omap_mux_get_by_name(struct omap_mux_partition *partition,
157 const char *muxname,
158 struct omap_mux **found_mux)
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800159{
Tony Lindgren8419fdb2010-12-22 18:42:35 -0800160 struct omap_mux *mux = NULL;
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800161 struct omap_mux_entry *e;
Tony Lindgren5a3b2f72010-10-01 16:35:24 -0700162 const char *mode_name;
Tony Lindgren8419fdb2010-12-22 18:42:35 -0800163 int found = 0, found_mode, mode0_len = 0;
Benoit Cousson112485e2010-08-16 10:55:35 +0200164 struct list_head *muxmodes = &partition->muxmodes;
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800165
166 mode_name = strchr(muxname, '.');
167 if (mode_name) {
Tony Lindgren5a3b2f72010-10-01 16:35:24 -0700168 mode0_len = strlen(muxname) - strlen(mode_name);
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800169 mode_name++;
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800170 } else {
171 mode_name = muxname;
172 }
173
Benoit Cousson112485e2010-08-16 10:55:35 +0200174 list_for_each_entry(e, muxmodes, node) {
Tony Lindgren8419fdb2010-12-22 18:42:35 -0800175 char *m0_entry;
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800176 int i;
177
Tony Lindgren8419fdb2010-12-22 18:42:35 -0800178 mux = &e->mux;
179 m0_entry = mux->muxnames[0];
180
Tony Lindgren5a3b2f72010-10-01 16:35:24 -0700181 /* First check for full name in mode0.muxmode format */
182 if (mode0_len && strncmp(muxname, m0_entry, mode0_len))
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800183 continue;
184
Tony Lindgren5a3b2f72010-10-01 16:35:24 -0700185 /* Then check for muxmode only */
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800186 for (i = 0; i < OMAP_MUX_NR_MODES; i++) {
Tony Lindgren8419fdb2010-12-22 18:42:35 -0800187 char *mode_cur = mux->muxnames[i];
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800188
189 if (!mode_cur)
190 continue;
191
192 if (!strcmp(mode_name, mode_cur)) {
Tony Lindgren8419fdb2010-12-22 18:42:35 -0800193 *found_mux = mux;
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800194 found++;
Tony Lindgren8419fdb2010-12-22 18:42:35 -0800195 found_mode = i;
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800196 }
197 }
198 }
199
Tony Lindgren8419fdb2010-12-22 18:42:35 -0800200 if (found == 1) {
201 return found_mode;
202 }
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800203
204 if (found > 1) {
Dan Murphy032a6422010-11-15 09:50:50 -0600205 pr_err("%s: Multiple signal paths (%i) for %s\n", __func__,
Benoit Cousson1cbb3a92010-08-10 17:33:01 +0200206 found, muxname);
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800207 return -EINVAL;
208 }
209
Tony Lindgren8419fdb2010-12-22 18:42:35 -0800210 pr_err("%s: Could not find signal %s\n", __func__, muxname);
211
212 return -ENODEV;
213}
214
215static int __init
216omap_mux_get_by_name(const char *muxname,
217 struct omap_mux_partition **found_partition,
218 struct omap_mux **found_mux)
219{
220 struct omap_mux_partition *partition;
221
222 list_for_each_entry(partition, &mux_partitions, node) {
223 struct omap_mux *mux = NULL;
224 int mux_mode = _omap_mux_get_by_name(partition, muxname, &mux);
225 if (mux_mode < 0)
226 continue;
227
228 *found_partition = partition;
229 *found_mux = mux;
230
231 return mux_mode;
232 }
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800233
234 return -ENODEV;
235}
236
Benoit Cousson112485e2010-08-16 10:55:35 +0200237int __init omap_mux_init_signal(const char *muxname, int val)
238{
Tony Lindgren8419fdb2010-12-22 18:42:35 -0800239 struct omap_mux_partition *partition = NULL;
240 struct omap_mux *mux = NULL;
241 u16 old_mode;
242 int mux_mode;
Benoit Cousson112485e2010-08-16 10:55:35 +0200243
Tony Lindgren8419fdb2010-12-22 18:42:35 -0800244 mux_mode = omap_mux_get_by_name(muxname, &partition, &mux);
245 if (mux_mode < 0)
246 return mux_mode;
Benoit Cousson112485e2010-08-16 10:55:35 +0200247
Tony Lindgren8419fdb2010-12-22 18:42:35 -0800248 old_mode = omap_mux_read(partition, mux->reg_offset);
249 mux_mode |= val;
250 pr_debug("%s: Setting signal %s 0x%04x -> 0x%04x\n",
251 __func__, muxname, old_mode, mux_mode);
252 omap_mux_write(partition, mux_mode, mux->reg_offset);
Benoit Cousson112485e2010-08-16 10:55:35 +0200253
Tony Lindgren8419fdb2010-12-22 18:42:35 -0800254 return 0;
Benoit Cousson112485e2010-08-16 10:55:35 +0200255}
256
Tony Lindgren9796b322010-12-22 18:42:35 -0800257struct omap_hwmod_mux_info * __init
258omap_hwmod_mux_init(struct omap_device_pad *bpads, int nr_pads)
259{
260 struct omap_hwmod_mux_info *hmux;
261 int i;
262
263 if (!bpads || nr_pads < 1)
264 return NULL;
265
266 hmux = kzalloc(sizeof(struct omap_hwmod_mux_info), GFP_KERNEL);
267 if (!hmux)
268 goto err1;
269
270 hmux->nr_pads = nr_pads;
271
272 hmux->pads = kzalloc(sizeof(struct omap_device_pad) *
273 nr_pads, GFP_KERNEL);
274 if (!hmux->pads)
275 goto err2;
276
277 for (i = 0; i < hmux->nr_pads; i++) {
278 struct omap_mux_partition *partition;
279 struct omap_device_pad *bpad = &bpads[i], *pad = &hmux->pads[i];
280 struct omap_mux *mux;
281 int mux_mode;
282
283 mux_mode = omap_mux_get_by_name(bpad->name, &partition, &mux);
284 if (mux_mode < 0)
285 goto err3;
286 if (!pad->partition)
287 pad->partition = partition;
288 if (!pad->mux)
289 pad->mux = mux;
290
291 pad->name = kzalloc(strlen(bpad->name) + 1, GFP_KERNEL);
292 if (!pad->name) {
293 int j;
294
295 for (j = i - 1; j >= 0; j--)
296 kfree(hmux->pads[j].name);
297 goto err3;
298 }
299 strcpy(pad->name, bpad->name);
300
301 pad->flags = bpad->flags;
302 pad->enable = bpad->enable;
303 pad->idle = bpad->idle;
304 pad->off = bpad->off;
305 pr_debug("%s: Initialized %s\n", __func__, pad->name);
306 }
307
308 return hmux;
309
310err3:
311 kfree(hmux->pads);
312err2:
313 kfree(hmux);
314err1:
315 pr_err("%s: Could not allocate device mux entry\n", __func__);
316
317 return NULL;
318}
319
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800320#ifdef CONFIG_DEBUG_FS
321
322#define OMAP_MUX_MAX_NR_FLAGS 10
323#define OMAP_MUX_TEST_FLAG(val, mask) \
324 if (((val) & (mask)) == (mask)) { \
325 i++; \
326 flags[i] = #mask; \
327 }
328
329/* REVISIT: Add checking for non-optimal mux settings */
330static inline void omap_mux_decode(struct seq_file *s, u16 val)
331{
332 char *flags[OMAP_MUX_MAX_NR_FLAGS];
Tony Lindgren78737ae2010-02-01 13:03:42 -0800333 char mode[sizeof("OMAP_MUX_MODE") + 1];
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800334 int i = -1;
335
336 sprintf(mode, "OMAP_MUX_MODE%d", val & 0x7);
337 i++;
338 flags[i] = mode;
339
340 OMAP_MUX_TEST_FLAG(val, OMAP_PIN_OFF_WAKEUPENABLE);
341 if (val & OMAP_OFF_EN) {
342 if (!(val & OMAP_OFFOUT_EN)) {
343 if (!(val & OMAP_OFF_PULL_UP)) {
344 OMAP_MUX_TEST_FLAG(val,
345 OMAP_PIN_OFF_INPUT_PULLDOWN);
346 } else {
347 OMAP_MUX_TEST_FLAG(val,
348 OMAP_PIN_OFF_INPUT_PULLUP);
349 }
350 } else {
351 if (!(val & OMAP_OFFOUT_VAL)) {
352 OMAP_MUX_TEST_FLAG(val,
353 OMAP_PIN_OFF_OUTPUT_LOW);
354 } else {
355 OMAP_MUX_TEST_FLAG(val,
356 OMAP_PIN_OFF_OUTPUT_HIGH);
357 }
358 }
359 }
360
361 if (val & OMAP_INPUT_EN) {
362 if (val & OMAP_PULL_ENA) {
363 if (!(val & OMAP_PULL_UP)) {
364 OMAP_MUX_TEST_FLAG(val,
365 OMAP_PIN_INPUT_PULLDOWN);
366 } else {
367 OMAP_MUX_TEST_FLAG(val, OMAP_PIN_INPUT_PULLUP);
368 }
369 } else {
370 OMAP_MUX_TEST_FLAG(val, OMAP_PIN_INPUT);
371 }
372 } else {
373 i++;
374 flags[i] = "OMAP_PIN_OUTPUT";
375 }
376
377 do {
378 seq_printf(s, "%s", flags[i]);
379 if (i > 0)
380 seq_printf(s, " | ");
381 } while (i-- > 0);
382}
383
Benoit Cousson112485e2010-08-16 10:55:35 +0200384#define OMAP_MUX_DEFNAME_LEN 32
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800385
386static int omap_mux_dbg_board_show(struct seq_file *s, void *unused)
387{
Benoit Cousson112485e2010-08-16 10:55:35 +0200388 struct omap_mux_partition *partition = s->private;
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800389 struct omap_mux_entry *e;
Benoit Cousson112485e2010-08-16 10:55:35 +0200390 u8 omap_gen = omap_rev() >> 28;
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800391
Benoit Cousson112485e2010-08-16 10:55:35 +0200392 list_for_each_entry(e, &partition->muxmodes, node) {
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800393 struct omap_mux *m = &e->mux;
394 char m0_def[OMAP_MUX_DEFNAME_LEN];
395 char *m0_name = m->muxnames[0];
396 u16 val;
397 int i, mode;
398
399 if (!m0_name)
400 continue;
401
Tony Lindgren78737ae2010-02-01 13:03:42 -0800402 /* REVISIT: Needs to be updated if mode0 names get longer */
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800403 for (i = 0; i < OMAP_MUX_DEFNAME_LEN; i++) {
404 if (m0_name[i] == '\0') {
405 m0_def[i] = m0_name[i];
406 break;
407 }
408 m0_def[i] = toupper(m0_name[i]);
409 }
Benoit Cousson112485e2010-08-16 10:55:35 +0200410 val = omap_mux_read(partition, m->reg_offset);
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800411 mode = val & OMAP_MUX_MODE7;
Benoit Cousson112485e2010-08-16 10:55:35 +0200412 if (mode != 0)
413 seq_printf(s, "/* %s */\n", m->muxnames[mode]);
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800414
Benoit Cousson112485e2010-08-16 10:55:35 +0200415 /*
416 * XXX: Might be revisited to support differences accross
417 * same OMAP generation.
418 */
419 seq_printf(s, "OMAP%d_MUX(%s, ", omap_gen, m0_def);
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800420 omap_mux_decode(s, val);
421 seq_printf(s, "),\n");
422 }
423
424 return 0;
425}
426
427static int omap_mux_dbg_board_open(struct inode *inode, struct file *file)
428{
Benoit Cousson112485e2010-08-16 10:55:35 +0200429 return single_open(file, omap_mux_dbg_board_show, inode->i_private);
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800430}
431
432static const struct file_operations omap_mux_dbg_board_fops = {
433 .open = omap_mux_dbg_board_open,
434 .read = seq_read,
435 .llseek = seq_lseek,
436 .release = single_release,
437};
438
Benoit Cousson112485e2010-08-16 10:55:35 +0200439static struct omap_mux_partition *omap_mux_get_partition(struct omap_mux *mux)
440{
441 struct omap_mux_partition *partition;
442
443 list_for_each_entry(partition, &mux_partitions, node) {
444 struct list_head *muxmodes = &partition->muxmodes;
445 struct omap_mux_entry *e;
446
447 list_for_each_entry(e, muxmodes, node) {
448 struct omap_mux *m = &e->mux;
449
450 if (m == mux)
451 return partition;
452 }
453 }
454
455 return NULL;
456}
457
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800458static int omap_mux_dbg_signal_show(struct seq_file *s, void *unused)
459{
460 struct omap_mux *m = s->private;
Benoit Cousson112485e2010-08-16 10:55:35 +0200461 struct omap_mux_partition *partition;
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800462 const char *none = "NA";
463 u16 val;
464 int mode;
465
Benoit Cousson112485e2010-08-16 10:55:35 +0200466 partition = omap_mux_get_partition(m);
467 if (!partition)
468 return 0;
469
470 val = omap_mux_read(partition, m->reg_offset);
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800471 mode = val & OMAP_MUX_MODE7;
472
Benoit Cousson112485e2010-08-16 10:55:35 +0200473 seq_printf(s, "name: %s.%s (0x%08x/0x%03x = 0x%04x), b %s, t %s\n",
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800474 m->muxnames[0], m->muxnames[mode],
Benoit Cousson112485e2010-08-16 10:55:35 +0200475 partition->phys + m->reg_offset, m->reg_offset, val,
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800476 m->balls[0] ? m->balls[0] : none,
477 m->balls[1] ? m->balls[1] : none);
478 seq_printf(s, "mode: ");
479 omap_mux_decode(s, val);
480 seq_printf(s, "\n");
481 seq_printf(s, "signals: %s | %s | %s | %s | %s | %s | %s | %s\n",
482 m->muxnames[0] ? m->muxnames[0] : none,
483 m->muxnames[1] ? m->muxnames[1] : none,
484 m->muxnames[2] ? m->muxnames[2] : none,
485 m->muxnames[3] ? m->muxnames[3] : none,
486 m->muxnames[4] ? m->muxnames[4] : none,
487 m->muxnames[5] ? m->muxnames[5] : none,
488 m->muxnames[6] ? m->muxnames[6] : none,
489 m->muxnames[7] ? m->muxnames[7] : none);
490
491 return 0;
492}
493
494#define OMAP_MUX_MAX_ARG_CHAR 7
495
496static ssize_t omap_mux_dbg_signal_write(struct file *file,
Benoit Cousson112485e2010-08-16 10:55:35 +0200497 const char __user *user_buf,
498 size_t count, loff_t *ppos)
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800499{
500 char buf[OMAP_MUX_MAX_ARG_CHAR];
501 struct seq_file *seqf;
502 struct omap_mux *m;
503 unsigned long val;
504 int buf_size, ret;
Benoit Cousson112485e2010-08-16 10:55:35 +0200505 struct omap_mux_partition *partition;
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800506
507 if (count > OMAP_MUX_MAX_ARG_CHAR)
508 return -EINVAL;
509
510 memset(buf, 0, sizeof(buf));
511 buf_size = min(count, sizeof(buf) - 1);
512
513 if (copy_from_user(buf, user_buf, buf_size))
514 return -EFAULT;
515
516 ret = strict_strtoul(buf, 0x10, &val);
517 if (ret < 0)
518 return ret;
519
520 if (val > 0xffff)
521 return -EINVAL;
522
523 seqf = file->private_data;
524 m = seqf->private;
525
Benoit Cousson112485e2010-08-16 10:55:35 +0200526 partition = omap_mux_get_partition(m);
527 if (!partition)
528 return -ENODEV;
529
530 omap_mux_write(partition, (u16)val, m->reg_offset);
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800531 *ppos += count;
532
533 return count;
534}
535
536static int omap_mux_dbg_signal_open(struct inode *inode, struct file *file)
537{
538 return single_open(file, omap_mux_dbg_signal_show, inode->i_private);
539}
540
541static const struct file_operations omap_mux_dbg_signal_fops = {
542 .open = omap_mux_dbg_signal_open,
543 .read = seq_read,
544 .write = omap_mux_dbg_signal_write,
545 .llseek = seq_lseek,
546 .release = single_release,
547};
548
549static struct dentry *mux_dbg_dir;
550
Benoit Cousson112485e2010-08-16 10:55:35 +0200551static void __init omap_mux_dbg_create_entry(
552 struct omap_mux_partition *partition,
553 struct dentry *mux_dbg_dir)
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800554{
555 struct omap_mux_entry *e;
556
Benoit Cousson112485e2010-08-16 10:55:35 +0200557 list_for_each_entry(e, &partition->muxmodes, node) {
558 struct omap_mux *m = &e->mux;
559
560 (void)debugfs_create_file(m->muxnames[0], S_IWUGO, mux_dbg_dir,
561 m, &omap_mux_dbg_signal_fops);
562 }
563}
564
565static void __init omap_mux_dbg_init(void)
566{
567 struct omap_mux_partition *partition;
568 static struct dentry *mux_dbg_board_dir;
569
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800570 mux_dbg_dir = debugfs_create_dir("omap_mux", NULL);
571 if (!mux_dbg_dir)
572 return;
573
Benoit Cousson112485e2010-08-16 10:55:35 +0200574 mux_dbg_board_dir = debugfs_create_dir("board", mux_dbg_dir);
575 if (!mux_dbg_board_dir)
576 return;
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800577
Benoit Cousson112485e2010-08-16 10:55:35 +0200578 list_for_each_entry(partition, &mux_partitions, node) {
579 omap_mux_dbg_create_entry(partition, mux_dbg_dir);
580 (void)debugfs_create_file(partition->name, S_IRUGO,
581 mux_dbg_board_dir, partition,
582 &omap_mux_dbg_board_fops);
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800583 }
584}
585
586#else
587static inline void omap_mux_dbg_init(void)
588{
589}
590#endif /* CONFIG_DEBUG_FS */
591
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800592static void __init omap_mux_free_names(struct omap_mux *m)
593{
594 int i;
595
596 for (i = 0; i < OMAP_MUX_NR_MODES; i++)
597 kfree(m->muxnames[i]);
598
599#ifdef CONFIG_DEBUG_FS
600 for (i = 0; i < OMAP_MUX_NR_SIDES; i++)
601 kfree(m->balls[i]);
Tony Lindgren1dbae812005-11-10 14:26:51 +0000602#endif
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800603
604}
605
606/* Free all data except for GPIO pins unless CONFIG_DEBUG_FS is set */
607static int __init omap_mux_late_init(void)
608{
Benoit Cousson112485e2010-08-16 10:55:35 +0200609 struct omap_mux_partition *partition;
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800610
Benoit Cousson112485e2010-08-16 10:55:35 +0200611 list_for_each_entry(partition, &mux_partitions, node) {
612 struct omap_mux_entry *e, *tmp;
613 list_for_each_entry_safe(e, tmp, &partition->muxmodes, node) {
614 struct omap_mux *m = &e->mux;
615 u16 mode = omap_mux_read(partition, m->reg_offset);
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800616
Benoit Cousson112485e2010-08-16 10:55:35 +0200617 if (OMAP_MODE_GPIO(mode))
618 continue;
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800619
620#ifndef CONFIG_DEBUG_FS
Benoit Cousson112485e2010-08-16 10:55:35 +0200621 mutex_lock(&muxmode_mutex);
622 list_del(&e->node);
623 mutex_unlock(&muxmode_mutex);
624 omap_mux_free_names(m);
625 kfree(m);
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800626#endif
Benoit Cousson112485e2010-08-16 10:55:35 +0200627 }
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800628 }
629
Tony Lindgren4b715ef2009-12-11 16:16:32 -0800630 omap_mux_dbg_init();
631
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800632 return 0;
633}
634late_initcall(omap_mux_late_init);
635
636static void __init omap_mux_package_fixup(struct omap_mux *p,
637 struct omap_mux *superset)
638{
639 while (p->reg_offset != OMAP_MUX_TERMINATOR) {
640 struct omap_mux *s = superset;
641 int found = 0;
642
643 while (s->reg_offset != OMAP_MUX_TERMINATOR) {
644 if (s->reg_offset == p->reg_offset) {
645 *s = *p;
646 found++;
647 break;
648 }
649 s++;
650 }
651 if (!found)
Dan Murphy032a6422010-11-15 09:50:50 -0600652 pr_err("%s: Unknown entry offset 0x%x\n", __func__,
Benoit Cousson1cbb3a92010-08-10 17:33:01 +0200653 p->reg_offset);
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800654 p++;
655 }
656}
657
658#ifdef CONFIG_DEBUG_FS
659
660static void __init omap_mux_package_init_balls(struct omap_ball *b,
661 struct omap_mux *superset)
662{
663 while (b->reg_offset != OMAP_MUX_TERMINATOR) {
664 struct omap_mux *s = superset;
665 int found = 0;
666
667 while (s->reg_offset != OMAP_MUX_TERMINATOR) {
668 if (s->reg_offset == b->reg_offset) {
669 s->balls[0] = b->balls[0];
670 s->balls[1] = b->balls[1];
671 found++;
672 break;
673 }
674 s++;
675 }
676 if (!found)
Dan Murphy032a6422010-11-15 09:50:50 -0600677 pr_err("%s: Unknown ball offset 0x%x\n", __func__,
Benoit Cousson1cbb3a92010-08-10 17:33:01 +0200678 b->reg_offset);
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800679 b++;
680 }
681}
682
683#else /* CONFIG_DEBUG_FS */
684
685static inline void omap_mux_package_init_balls(struct omap_ball *b,
686 struct omap_mux *superset)
687{
688}
689
690#endif /* CONFIG_DEBUG_FS */
691
692static int __init omap_mux_setup(char *options)
693{
694 if (!options)
695 return 0;
696
697 omap_mux_options = options;
698
699 return 1;
700}
701__setup("omap_mux=", omap_mux_setup);
702
703/*
704 * Note that the omap_mux=some.signal1=0x1234,some.signal2=0x1234
705 * cmdline options only override the bootloader values.
706 * During development, please enable CONFIG_DEBUG_FS, and use the
707 * signal specific entries under debugfs.
708 */
709static void __init omap_mux_set_cmdline_signals(void)
710{
711 char *options, *next_opt, *token;
712
713 if (!omap_mux_options)
714 return;
715
716 options = kmalloc(strlen(omap_mux_options) + 1, GFP_KERNEL);
717 if (!options)
718 return;
719
720 strcpy(options, omap_mux_options);
721 next_opt = options;
722
723 while ((token = strsep(&next_opt, ",")) != NULL) {
724 char *keyval, *name;
725 unsigned long val;
726
727 keyval = token;
728 name = strsep(&keyval, "=");
729 if (name) {
730 int res;
731
732 res = strict_strtoul(keyval, 0x10, &val);
733 if (res < 0)
734 continue;
735
736 omap_mux_init_signal(name, (u16)val);
737 }
738 }
739
740 kfree(options);
741}
742
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800743static int __init omap_mux_copy_names(struct omap_mux *src,
Benoit Cousson112485e2010-08-16 10:55:35 +0200744 struct omap_mux *dst)
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800745{
746 int i;
747
748 for (i = 0; i < OMAP_MUX_NR_MODES; i++) {
749 if (src->muxnames[i]) {
750 dst->muxnames[i] =
751 kmalloc(strlen(src->muxnames[i]) + 1,
752 GFP_KERNEL);
753 if (!dst->muxnames[i])
754 goto free;
755 strcpy(dst->muxnames[i], src->muxnames[i]);
756 }
757 }
758
759#ifdef CONFIG_DEBUG_FS
760 for (i = 0; i < OMAP_MUX_NR_SIDES; i++) {
761 if (src->balls[i]) {
762 dst->balls[i] =
763 kmalloc(strlen(src->balls[i]) + 1,
764 GFP_KERNEL);
765 if (!dst->balls[i])
766 goto free;
767 strcpy(dst->balls[i], src->balls[i]);
768 }
769 }
770#endif
771
772 return 0;
773
774free:
775 omap_mux_free_names(dst);
776 return -ENOMEM;
777
778}
779
780#endif /* CONFIG_OMAP_MUX */
781
Benoit Cousson112485e2010-08-16 10:55:35 +0200782static struct omap_mux *omap_mux_get_by_gpio(
783 struct omap_mux_partition *partition,
784 int gpio)
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800785{
786 struct omap_mux_entry *e;
Benoit Cousson112485e2010-08-16 10:55:35 +0200787 struct omap_mux *ret = NULL;
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800788
Benoit Cousson112485e2010-08-16 10:55:35 +0200789 list_for_each_entry(e, &partition->muxmodes, node) {
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800790 struct omap_mux *m = &e->mux;
791 if (m->gpio == gpio) {
Benoit Cousson112485e2010-08-16 10:55:35 +0200792 ret = m;
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800793 break;
794 }
795 }
796
Benoit Cousson112485e2010-08-16 10:55:35 +0200797 return ret;
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800798}
799
800/* Needed for dynamic muxing of GPIO pins for off-idle */
801u16 omap_mux_get_gpio(int gpio)
802{
Benoit Cousson112485e2010-08-16 10:55:35 +0200803 struct omap_mux_partition *partition;
804 struct omap_mux *m;
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800805
Benoit Cousson112485e2010-08-16 10:55:35 +0200806 list_for_each_entry(partition, &mux_partitions, node) {
807 m = omap_mux_get_by_gpio(partition, gpio);
808 if (m)
809 return omap_mux_read(partition, m->reg_offset);
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800810 }
811
Benoit Cousson112485e2010-08-16 10:55:35 +0200812 if (!m || m->reg_offset == OMAP_MUX_TERMINATOR)
Dan Murphy032a6422010-11-15 09:50:50 -0600813 pr_err("%s: Could not get gpio%i\n", __func__, gpio);
Benoit Cousson112485e2010-08-16 10:55:35 +0200814
815 return OMAP_MUX_TERMINATOR;
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800816}
817
818/* Needed for dynamic muxing of GPIO pins for off-idle */
819void omap_mux_set_gpio(u16 val, int gpio)
820{
Benoit Cousson112485e2010-08-16 10:55:35 +0200821 struct omap_mux_partition *partition;
822 struct omap_mux *m = NULL;
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800823
Benoit Cousson112485e2010-08-16 10:55:35 +0200824 list_for_each_entry(partition, &mux_partitions, node) {
825 m = omap_mux_get_by_gpio(partition, gpio);
826 if (m) {
827 omap_mux_write(partition, val, m->reg_offset);
828 return;
829 }
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800830 }
831
Benoit Cousson112485e2010-08-16 10:55:35 +0200832 if (!m || m->reg_offset == OMAP_MUX_TERMINATOR)
Dan Murphy032a6422010-11-15 09:50:50 -0600833 pr_err("%s: Could not set gpio%i\n", __func__, gpio);
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800834}
835
Benoit Cousson112485e2010-08-16 10:55:35 +0200836static struct omap_mux * __init omap_mux_list_add(
837 struct omap_mux_partition *partition,
838 struct omap_mux *src)
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800839{
840 struct omap_mux_entry *entry;
841 struct omap_mux *m;
842
843 entry = kzalloc(sizeof(struct omap_mux_entry), GFP_KERNEL);
844 if (!entry)
845 return NULL;
846
847 m = &entry->mux;
848 memcpy(m, src, sizeof(struct omap_mux_entry));
849
850#ifdef CONFIG_OMAP_MUX
851 if (omap_mux_copy_names(src, m)) {
852 kfree(entry);
853 return NULL;
854 }
855#endif
856
857 mutex_lock(&muxmode_mutex);
Benoit Cousson112485e2010-08-16 10:55:35 +0200858 list_add_tail(&entry->node, &partition->muxmodes);
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800859 mutex_unlock(&muxmode_mutex);
860
861 return m;
862}
863
864/*
865 * Note if CONFIG_OMAP_MUX is not selected, we will only initialize
866 * the GPIO to mux offset mapping that is needed for dynamic muxing
867 * of GPIO pins for off-idle.
868 */
Benoit Cousson112485e2010-08-16 10:55:35 +0200869static void __init omap_mux_init_list(struct omap_mux_partition *partition,
870 struct omap_mux *superset)
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800871{
872 while (superset->reg_offset != OMAP_MUX_TERMINATOR) {
873 struct omap_mux *entry;
874
Ranjith Lohithakshanb72c7d52010-02-17 17:17:01 +0000875#ifdef CONFIG_OMAP_MUX
876 if (!superset->muxnames || !superset->muxnames[0]) {
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800877 superset++;
878 continue;
879 }
Ranjith Lohithakshanb72c7d52010-02-17 17:17:01 +0000880#else
881 /* Skip pins that are not muxed as GPIO by bootloader */
Benoit Cousson112485e2010-08-16 10:55:35 +0200882 if (!OMAP_MODE_GPIO(omap_mux_read(partition,
883 superset->reg_offset))) {
Tony Lindgren9ecef432010-02-01 11:22:54 -0800884 superset++;
885 continue;
886 }
887#endif
888
Benoit Cousson112485e2010-08-16 10:55:35 +0200889 entry = omap_mux_list_add(partition, superset);
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800890 if (!entry) {
Dan Murphy032a6422010-11-15 09:50:50 -0600891 pr_err("%s: Could not add entry\n", __func__);
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800892 return;
893 }
894 superset++;
895 }
896}
897
Tony Lindgren321cfc82010-02-15 10:03:35 -0800898#ifdef CONFIG_OMAP_MUX
899
900static void omap_mux_init_package(struct omap_mux *superset,
901 struct omap_mux *package_subset,
902 struct omap_ball *package_balls)
903{
904 if (package_subset)
905 omap_mux_package_fixup(package_subset, superset);
906 if (package_balls)
907 omap_mux_package_init_balls(package_balls, superset);
908}
909
Benoit Cousson112485e2010-08-16 10:55:35 +0200910static void omap_mux_init_signals(struct omap_mux_partition *partition,
911 struct omap_board_mux *board_mux)
Tony Lindgren321cfc82010-02-15 10:03:35 -0800912{
913 omap_mux_set_cmdline_signals();
Benoit Cousson112485e2010-08-16 10:55:35 +0200914 omap_mux_write_array(partition, board_mux);
Tony Lindgren321cfc82010-02-15 10:03:35 -0800915}
916
917#else
918
919static void omap_mux_init_package(struct omap_mux *superset,
920 struct omap_mux *package_subset,
921 struct omap_ball *package_balls)
922{
923}
924
Benoit Cousson112485e2010-08-16 10:55:35 +0200925static void omap_mux_init_signals(struct omap_mux_partition *partition,
926 struct omap_board_mux *board_mux)
Tony Lindgren321cfc82010-02-15 10:03:35 -0800927{
928}
929
930#endif
931
Benoit Cousson112485e2010-08-16 10:55:35 +0200932static u32 mux_partitions_cnt;
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800933
Benoit Cousson112485e2010-08-16 10:55:35 +0200934int __init omap_mux_init(const char *name, u32 flags,
935 u32 mux_pbase, u32 mux_size,
936 struct omap_mux *superset,
937 struct omap_mux *package_subset,
938 struct omap_board_mux *board_mux,
939 struct omap_ball *package_balls)
940{
941 struct omap_mux_partition *partition;
942
943 partition = kzalloc(sizeof(struct omap_mux_partition), GFP_KERNEL);
944 if (!partition)
945 return -ENOMEM;
946
947 partition->name = name;
948 partition->flags = flags;
949 partition->size = mux_size;
950 partition->phys = mux_pbase;
951 partition->base = ioremap(mux_pbase, mux_size);
952 if (!partition->base) {
Dan Murphy032a6422010-11-15 09:50:50 -0600953 pr_err("%s: Could not ioremap mux partition at 0x%08x\n",
954 __func__, partition->phys);
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800955 return -ENODEV;
956 }
957
Benoit Cousson112485e2010-08-16 10:55:35 +0200958 INIT_LIST_HEAD(&partition->muxmodes);
959
960 list_add_tail(&partition->node, &mux_partitions);
961 mux_partitions_cnt++;
Dan Murphy032a6422010-11-15 09:50:50 -0600962 pr_info("%s: Add partition: #%d: %s, flags: %x\n", __func__,
Benoit Cousson112485e2010-08-16 10:55:35 +0200963 mux_partitions_cnt, partition->name, partition->flags);
Tony Lindgrend5425be2010-07-05 16:31:35 +0300964
Tony Lindgren321cfc82010-02-15 10:03:35 -0800965 omap_mux_init_package(superset, package_subset, package_balls);
Benoit Cousson112485e2010-08-16 10:55:35 +0200966 omap_mux_init_list(partition, superset);
967 omap_mux_init_signals(partition, board_mux);
Tony Lindgren2cb0c542010-01-19 18:17:07 -0800968
Tony Lindgren15ac7af2009-12-11 16:16:32 -0800969 return 0;
970}
971