mux.c 23 KB

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