mux.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077
  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, nr_pads_dynamic = 0;
  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. if (pad->flags & OMAP_DEVICE_PAD_REMUX)
  251. nr_pads_dynamic++;
  252. pr_debug("%s: Initialized %s\n", __func__, pad->name);
  253. }
  254. if (!nr_pads_dynamic)
  255. return hmux;
  256. /*
  257. * Add pads that need dynamic muxing into a separate list
  258. */
  259. hmux->nr_pads_dynamic = nr_pads_dynamic;
  260. hmux->pads_dynamic = kzalloc(sizeof(struct omap_device_pad *) *
  261. nr_pads_dynamic, GFP_KERNEL);
  262. if (!hmux->pads_dynamic) {
  263. pr_err("%s: Could not allocate dynamic pads\n", __func__);
  264. return hmux;
  265. }
  266. nr_pads_dynamic = 0;
  267. for (i = 0; i < hmux->nr_pads; i++) {
  268. struct omap_device_pad *pad = &hmux->pads[i];
  269. if (pad->flags & OMAP_DEVICE_PAD_REMUX) {
  270. pr_debug("%s: pad %s tagged dynamic\n",
  271. __func__, pad->name);
  272. hmux->pads_dynamic[nr_pads_dynamic] = pad;
  273. nr_pads_dynamic++;
  274. }
  275. }
  276. return hmux;
  277. err3:
  278. kfree(hmux->pads);
  279. err2:
  280. kfree(hmux);
  281. err1:
  282. pr_err("%s: Could not allocate device mux entry\n", __func__);
  283. return NULL;
  284. }
  285. /* Assumes the calling function takes care of locking */
  286. void omap_hwmod_mux(struct omap_hwmod_mux_info *hmux, u8 state)
  287. {
  288. int i;
  289. /* Runtime idling of dynamic pads */
  290. if (state == _HWMOD_STATE_IDLE && hmux->enabled) {
  291. for (i = 0; i < hmux->nr_pads_dynamic; i++) {
  292. struct omap_device_pad *pad = hmux->pads_dynamic[i];
  293. int val = -EINVAL;
  294. val = pad->idle;
  295. omap_mux_write(pad->partition, val,
  296. pad->mux->reg_offset);
  297. }
  298. return;
  299. }
  300. /* Runtime enabling of dynamic pads */
  301. if ((state == _HWMOD_STATE_ENABLED) && hmux->pads_dynamic
  302. && hmux->enabled) {
  303. for (i = 0; i < hmux->nr_pads_dynamic; i++) {
  304. struct omap_device_pad *pad = hmux->pads_dynamic[i];
  305. int val = -EINVAL;
  306. val = pad->enable;
  307. omap_mux_write(pad->partition, val,
  308. pad->mux->reg_offset);
  309. }
  310. return;
  311. }
  312. /* Enabling or disabling of all pads */
  313. for (i = 0; i < hmux->nr_pads; i++) {
  314. struct omap_device_pad *pad = &hmux->pads[i];
  315. int flags, val = -EINVAL;
  316. flags = pad->flags;
  317. switch (state) {
  318. case _HWMOD_STATE_ENABLED:
  319. val = pad->enable;
  320. pr_debug("%s: Enabling %s %x\n", __func__,
  321. pad->name, val);
  322. break;
  323. case _HWMOD_STATE_DISABLED:
  324. /* Use safe mode unless OMAP_DEVICE_PAD_REMUX */
  325. if (flags & OMAP_DEVICE_PAD_REMUX)
  326. val = pad->off;
  327. else
  328. val = OMAP_MUX_MODE7;
  329. pr_debug("%s: Disabling %s %x\n", __func__,
  330. pad->name, val);
  331. break;
  332. default:
  333. /* Nothing to be done */
  334. break;
  335. };
  336. if (val >= 0) {
  337. omap_mux_write(pad->partition, val,
  338. pad->mux->reg_offset);
  339. pad->flags = flags;
  340. }
  341. }
  342. if (state == _HWMOD_STATE_ENABLED)
  343. hmux->enabled = true;
  344. else
  345. hmux->enabled = false;
  346. }
  347. #ifdef CONFIG_DEBUG_FS
  348. #define OMAP_MUX_MAX_NR_FLAGS 10
  349. #define OMAP_MUX_TEST_FLAG(val, mask) \
  350. if (((val) & (mask)) == (mask)) { \
  351. i++; \
  352. flags[i] = #mask; \
  353. }
  354. /* REVISIT: Add checking for non-optimal mux settings */
  355. static inline void omap_mux_decode(struct seq_file *s, u16 val)
  356. {
  357. char *flags[OMAP_MUX_MAX_NR_FLAGS];
  358. char mode[sizeof("OMAP_MUX_MODE") + 1];
  359. int i = -1;
  360. sprintf(mode, "OMAP_MUX_MODE%d", val & 0x7);
  361. i++;
  362. flags[i] = mode;
  363. OMAP_MUX_TEST_FLAG(val, OMAP_PIN_OFF_WAKEUPENABLE);
  364. if (val & OMAP_OFF_EN) {
  365. if (!(val & OMAP_OFFOUT_EN)) {
  366. if (!(val & OMAP_OFF_PULL_UP)) {
  367. OMAP_MUX_TEST_FLAG(val,
  368. OMAP_PIN_OFF_INPUT_PULLDOWN);
  369. } else {
  370. OMAP_MUX_TEST_FLAG(val,
  371. OMAP_PIN_OFF_INPUT_PULLUP);
  372. }
  373. } else {
  374. if (!(val & OMAP_OFFOUT_VAL)) {
  375. OMAP_MUX_TEST_FLAG(val,
  376. OMAP_PIN_OFF_OUTPUT_LOW);
  377. } else {
  378. OMAP_MUX_TEST_FLAG(val,
  379. OMAP_PIN_OFF_OUTPUT_HIGH);
  380. }
  381. }
  382. }
  383. if (val & OMAP_INPUT_EN) {
  384. if (val & OMAP_PULL_ENA) {
  385. if (!(val & OMAP_PULL_UP)) {
  386. OMAP_MUX_TEST_FLAG(val,
  387. OMAP_PIN_INPUT_PULLDOWN);
  388. } else {
  389. OMAP_MUX_TEST_FLAG(val, OMAP_PIN_INPUT_PULLUP);
  390. }
  391. } else {
  392. OMAP_MUX_TEST_FLAG(val, OMAP_PIN_INPUT);
  393. }
  394. } else {
  395. i++;
  396. flags[i] = "OMAP_PIN_OUTPUT";
  397. }
  398. do {
  399. seq_printf(s, "%s", flags[i]);
  400. if (i > 0)
  401. seq_printf(s, " | ");
  402. } while (i-- > 0);
  403. }
  404. #define OMAP_MUX_DEFNAME_LEN 32
  405. static int omap_mux_dbg_board_show(struct seq_file *s, void *unused)
  406. {
  407. struct omap_mux_partition *partition = s->private;
  408. struct omap_mux_entry *e;
  409. u8 omap_gen = omap_rev() >> 28;
  410. list_for_each_entry(e, &partition->muxmodes, node) {
  411. struct omap_mux *m = &e->mux;
  412. char m0_def[OMAP_MUX_DEFNAME_LEN];
  413. char *m0_name = m->muxnames[0];
  414. u16 val;
  415. int i, mode;
  416. if (!m0_name)
  417. continue;
  418. /* REVISIT: Needs to be updated if mode0 names get longer */
  419. for (i = 0; i < OMAP_MUX_DEFNAME_LEN; i++) {
  420. if (m0_name[i] == '\0') {
  421. m0_def[i] = m0_name[i];
  422. break;
  423. }
  424. m0_def[i] = toupper(m0_name[i]);
  425. }
  426. val = omap_mux_read(partition, m->reg_offset);
  427. mode = val & OMAP_MUX_MODE7;
  428. if (mode != 0)
  429. seq_printf(s, "/* %s */\n", m->muxnames[mode]);
  430. /*
  431. * XXX: Might be revisited to support differences accross
  432. * same OMAP generation.
  433. */
  434. seq_printf(s, "OMAP%d_MUX(%s, ", omap_gen, m0_def);
  435. omap_mux_decode(s, val);
  436. seq_printf(s, "),\n");
  437. }
  438. return 0;
  439. }
  440. static int omap_mux_dbg_board_open(struct inode *inode, struct file *file)
  441. {
  442. return single_open(file, omap_mux_dbg_board_show, inode->i_private);
  443. }
  444. static const struct file_operations omap_mux_dbg_board_fops = {
  445. .open = omap_mux_dbg_board_open,
  446. .read = seq_read,
  447. .llseek = seq_lseek,
  448. .release = single_release,
  449. };
  450. static struct omap_mux_partition *omap_mux_get_partition(struct omap_mux *mux)
  451. {
  452. struct omap_mux_partition *partition;
  453. list_for_each_entry(partition, &mux_partitions, node) {
  454. struct list_head *muxmodes = &partition->muxmodes;
  455. struct omap_mux_entry *e;
  456. list_for_each_entry(e, muxmodes, node) {
  457. struct omap_mux *m = &e->mux;
  458. if (m == mux)
  459. return partition;
  460. }
  461. }
  462. return NULL;
  463. }
  464. static int omap_mux_dbg_signal_show(struct seq_file *s, void *unused)
  465. {
  466. struct omap_mux *m = s->private;
  467. struct omap_mux_partition *partition;
  468. const char *none = "NA";
  469. u16 val;
  470. int mode;
  471. partition = omap_mux_get_partition(m);
  472. if (!partition)
  473. return 0;
  474. val = omap_mux_read(partition, m->reg_offset);
  475. mode = val & OMAP_MUX_MODE7;
  476. seq_printf(s, "name: %s.%s (0x%08x/0x%03x = 0x%04x), b %s, t %s\n",
  477. m->muxnames[0], m->muxnames[mode],
  478. partition->phys + m->reg_offset, m->reg_offset, val,
  479. m->balls[0] ? m->balls[0] : none,
  480. m->balls[1] ? m->balls[1] : none);
  481. seq_printf(s, "mode: ");
  482. omap_mux_decode(s, val);
  483. seq_printf(s, "\n");
  484. seq_printf(s, "signals: %s | %s | %s | %s | %s | %s | %s | %s\n",
  485. m->muxnames[0] ? m->muxnames[0] : none,
  486. m->muxnames[1] ? m->muxnames[1] : none,
  487. m->muxnames[2] ? m->muxnames[2] : none,
  488. m->muxnames[3] ? m->muxnames[3] : none,
  489. m->muxnames[4] ? m->muxnames[4] : none,
  490. m->muxnames[5] ? m->muxnames[5] : none,
  491. m->muxnames[6] ? m->muxnames[6] : none,
  492. m->muxnames[7] ? m->muxnames[7] : none);
  493. return 0;
  494. }
  495. #define OMAP_MUX_MAX_ARG_CHAR 7
  496. static ssize_t omap_mux_dbg_signal_write(struct file *file,
  497. const char __user *user_buf,
  498. size_t count, loff_t *ppos)
  499. {
  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;
  505. struct omap_mux_partition *partition;
  506. if (count > OMAP_MUX_MAX_ARG_CHAR)
  507. return -EINVAL;
  508. memset(buf, 0, sizeof(buf));
  509. buf_size = min(count, sizeof(buf) - 1);
  510. if (copy_from_user(buf, user_buf, buf_size))
  511. return -EFAULT;
  512. ret = strict_strtoul(buf, 0x10, &val);
  513. if (ret < 0)
  514. return ret;
  515. if (val > 0xffff)
  516. return -EINVAL;
  517. seqf = file->private_data;
  518. m = seqf->private;
  519. partition = omap_mux_get_partition(m);
  520. if (!partition)
  521. return -ENODEV;
  522. omap_mux_write(partition, (u16)val, m->reg_offset);
  523. *ppos += count;
  524. return count;
  525. }
  526. static int omap_mux_dbg_signal_open(struct inode *inode, struct file *file)
  527. {
  528. return single_open(file, omap_mux_dbg_signal_show, inode->i_private);
  529. }
  530. static const struct file_operations omap_mux_dbg_signal_fops = {
  531. .open = omap_mux_dbg_signal_open,
  532. .read = seq_read,
  533. .write = omap_mux_dbg_signal_write,
  534. .llseek = seq_lseek,
  535. .release = single_release,
  536. };
  537. static struct dentry *mux_dbg_dir;
  538. static void __init omap_mux_dbg_create_entry(
  539. struct omap_mux_partition *partition,
  540. struct dentry *mux_dbg_dir)
  541. {
  542. struct omap_mux_entry *e;
  543. list_for_each_entry(e, &partition->muxmodes, node) {
  544. struct omap_mux *m = &e->mux;
  545. (void)debugfs_create_file(m->muxnames[0], S_IWUSR, mux_dbg_dir,
  546. m, &omap_mux_dbg_signal_fops);
  547. }
  548. }
  549. static void __init omap_mux_dbg_init(void)
  550. {
  551. struct omap_mux_partition *partition;
  552. static struct dentry *mux_dbg_board_dir;
  553. mux_dbg_dir = debugfs_create_dir("omap_mux", NULL);
  554. if (!mux_dbg_dir)
  555. return;
  556. mux_dbg_board_dir = debugfs_create_dir("board", mux_dbg_dir);
  557. if (!mux_dbg_board_dir)
  558. return;
  559. list_for_each_entry(partition, &mux_partitions, node) {
  560. omap_mux_dbg_create_entry(partition, mux_dbg_dir);
  561. (void)debugfs_create_file(partition->name, S_IRUGO,
  562. mux_dbg_board_dir, partition,
  563. &omap_mux_dbg_board_fops);
  564. }
  565. }
  566. #else
  567. static inline void omap_mux_dbg_init(void)
  568. {
  569. }
  570. #endif /* CONFIG_DEBUG_FS */
  571. static void __init omap_mux_free_names(struct omap_mux *m)
  572. {
  573. int i;
  574. for (i = 0; i < OMAP_MUX_NR_MODES; i++)
  575. kfree(m->muxnames[i]);
  576. #ifdef CONFIG_DEBUG_FS
  577. for (i = 0; i < OMAP_MUX_NR_SIDES; i++)
  578. kfree(m->balls[i]);
  579. #endif
  580. }
  581. /* Free all data except for GPIO pins unless CONFIG_DEBUG_FS is set */
  582. static int __init omap_mux_late_init(void)
  583. {
  584. struct omap_mux_partition *partition;
  585. list_for_each_entry(partition, &mux_partitions, node) {
  586. struct omap_mux_entry *e, *tmp;
  587. list_for_each_entry_safe(e, tmp, &partition->muxmodes, node) {
  588. struct omap_mux *m = &e->mux;
  589. u16 mode = omap_mux_read(partition, m->reg_offset);
  590. if (OMAP_MODE_GPIO(mode))
  591. continue;
  592. #ifndef CONFIG_DEBUG_FS
  593. mutex_lock(&muxmode_mutex);
  594. list_del(&e->node);
  595. mutex_unlock(&muxmode_mutex);
  596. omap_mux_free_names(m);
  597. kfree(m);
  598. #endif
  599. }
  600. }
  601. omap_mux_dbg_init();
  602. return 0;
  603. }
  604. late_initcall(omap_mux_late_init);
  605. static void __init omap_mux_package_fixup(struct omap_mux *p,
  606. struct omap_mux *superset)
  607. {
  608. while (p->reg_offset != OMAP_MUX_TERMINATOR) {
  609. struct omap_mux *s = superset;
  610. int found = 0;
  611. while (s->reg_offset != OMAP_MUX_TERMINATOR) {
  612. if (s->reg_offset == p->reg_offset) {
  613. *s = *p;
  614. found++;
  615. break;
  616. }
  617. s++;
  618. }
  619. if (!found)
  620. pr_err("%s: Unknown entry offset 0x%x\n", __func__,
  621. p->reg_offset);
  622. p++;
  623. }
  624. }
  625. #ifdef CONFIG_DEBUG_FS
  626. static void __init omap_mux_package_init_balls(struct omap_ball *b,
  627. struct omap_mux *superset)
  628. {
  629. while (b->reg_offset != OMAP_MUX_TERMINATOR) {
  630. struct omap_mux *s = superset;
  631. int found = 0;
  632. while (s->reg_offset != OMAP_MUX_TERMINATOR) {
  633. if (s->reg_offset == b->reg_offset) {
  634. s->balls[0] = b->balls[0];
  635. s->balls[1] = b->balls[1];
  636. found++;
  637. break;
  638. }
  639. s++;
  640. }
  641. if (!found)
  642. pr_err("%s: Unknown ball offset 0x%x\n", __func__,
  643. b->reg_offset);
  644. b++;
  645. }
  646. }
  647. #else /* CONFIG_DEBUG_FS */
  648. static inline void omap_mux_package_init_balls(struct omap_ball *b,
  649. struct omap_mux *superset)
  650. {
  651. }
  652. #endif /* CONFIG_DEBUG_FS */
  653. static int __init omap_mux_setup(char *options)
  654. {
  655. if (!options)
  656. return 0;
  657. omap_mux_options = options;
  658. return 1;
  659. }
  660. __setup("omap_mux=", omap_mux_setup);
  661. /*
  662. * Note that the omap_mux=some.signal1=0x1234,some.signal2=0x1234
  663. * cmdline options only override the bootloader values.
  664. * During development, please enable CONFIG_DEBUG_FS, and use the
  665. * signal specific entries under debugfs.
  666. */
  667. static void __init omap_mux_set_cmdline_signals(void)
  668. {
  669. char *options, *next_opt, *token;
  670. if (!omap_mux_options)
  671. return;
  672. options = kmalloc(strlen(omap_mux_options) + 1, GFP_KERNEL);
  673. if (!options)
  674. return;
  675. strcpy(options, omap_mux_options);
  676. next_opt = options;
  677. while ((token = strsep(&next_opt, ",")) != NULL) {
  678. char *keyval, *name;
  679. unsigned long val;
  680. keyval = token;
  681. name = strsep(&keyval, "=");
  682. if (name) {
  683. int res;
  684. res = strict_strtoul(keyval, 0x10, &val);
  685. if (res < 0)
  686. continue;
  687. omap_mux_init_signal(name, (u16)val);
  688. }
  689. }
  690. kfree(options);
  691. }
  692. static int __init omap_mux_copy_names(struct omap_mux *src,
  693. struct omap_mux *dst)
  694. {
  695. int i;
  696. for (i = 0; i < OMAP_MUX_NR_MODES; i++) {
  697. if (src->muxnames[i]) {
  698. dst->muxnames[i] =
  699. kmalloc(strlen(src->muxnames[i]) + 1,
  700. GFP_KERNEL);
  701. if (!dst->muxnames[i])
  702. goto free;
  703. strcpy(dst->muxnames[i], src->muxnames[i]);
  704. }
  705. }
  706. #ifdef CONFIG_DEBUG_FS
  707. for (i = 0; i < OMAP_MUX_NR_SIDES; i++) {
  708. if (src->balls[i]) {
  709. dst->balls[i] =
  710. kmalloc(strlen(src->balls[i]) + 1,
  711. GFP_KERNEL);
  712. if (!dst->balls[i])
  713. goto free;
  714. strcpy(dst->balls[i], src->balls[i]);
  715. }
  716. }
  717. #endif
  718. return 0;
  719. free:
  720. omap_mux_free_names(dst);
  721. return -ENOMEM;
  722. }
  723. #endif /* CONFIG_OMAP_MUX */
  724. static struct omap_mux *omap_mux_get_by_gpio(
  725. struct omap_mux_partition *partition,
  726. int gpio)
  727. {
  728. struct omap_mux_entry *e;
  729. struct omap_mux *ret = NULL;
  730. list_for_each_entry(e, &partition->muxmodes, node) {
  731. struct omap_mux *m = &e->mux;
  732. if (m->gpio == gpio) {
  733. ret = m;
  734. break;
  735. }
  736. }
  737. return ret;
  738. }
  739. /* Needed for dynamic muxing of GPIO pins for off-idle */
  740. u16 omap_mux_get_gpio(int gpio)
  741. {
  742. struct omap_mux_partition *partition;
  743. struct omap_mux *m;
  744. list_for_each_entry(partition, &mux_partitions, node) {
  745. m = omap_mux_get_by_gpio(partition, gpio);
  746. if (m)
  747. return omap_mux_read(partition, m->reg_offset);
  748. }
  749. if (!m || m->reg_offset == OMAP_MUX_TERMINATOR)
  750. pr_err("%s: Could not get gpio%i\n", __func__, gpio);
  751. return OMAP_MUX_TERMINATOR;
  752. }
  753. /* Needed for dynamic muxing of GPIO pins for off-idle */
  754. void omap_mux_set_gpio(u16 val, int gpio)
  755. {
  756. struct omap_mux_partition *partition;
  757. struct omap_mux *m = NULL;
  758. list_for_each_entry(partition, &mux_partitions, node) {
  759. m = omap_mux_get_by_gpio(partition, gpio);
  760. if (m) {
  761. omap_mux_write(partition, val, m->reg_offset);
  762. return;
  763. }
  764. }
  765. if (!m || m->reg_offset == OMAP_MUX_TERMINATOR)
  766. pr_err("%s: Could not set gpio%i\n", __func__, gpio);
  767. }
  768. static struct omap_mux * __init omap_mux_list_add(
  769. struct omap_mux_partition *partition,
  770. struct omap_mux *src)
  771. {
  772. struct omap_mux_entry *entry;
  773. struct omap_mux *m;
  774. entry = kzalloc(sizeof(struct omap_mux_entry), GFP_KERNEL);
  775. if (!entry)
  776. return NULL;
  777. m = &entry->mux;
  778. entry->mux = *src;
  779. #ifdef CONFIG_OMAP_MUX
  780. if (omap_mux_copy_names(src, m)) {
  781. kfree(entry);
  782. return NULL;
  783. }
  784. #endif
  785. mutex_lock(&muxmode_mutex);
  786. list_add_tail(&entry->node, &partition->muxmodes);
  787. mutex_unlock(&muxmode_mutex);
  788. return m;
  789. }
  790. /*
  791. * Note if CONFIG_OMAP_MUX is not selected, we will only initialize
  792. * the GPIO to mux offset mapping that is needed for dynamic muxing
  793. * of GPIO pins for off-idle.
  794. */
  795. static void __init omap_mux_init_list(struct omap_mux_partition *partition,
  796. struct omap_mux *superset)
  797. {
  798. while (superset->reg_offset != OMAP_MUX_TERMINATOR) {
  799. struct omap_mux *entry;
  800. #ifdef CONFIG_OMAP_MUX
  801. if (!superset->muxnames || !superset->muxnames[0]) {
  802. superset++;
  803. continue;
  804. }
  805. #else
  806. /* Skip pins that are not muxed as GPIO by bootloader */
  807. if (!OMAP_MODE_GPIO(omap_mux_read(partition,
  808. superset->reg_offset))) {
  809. superset++;
  810. continue;
  811. }
  812. #endif
  813. entry = omap_mux_list_add(partition, superset);
  814. if (!entry) {
  815. pr_err("%s: Could not add entry\n", __func__);
  816. return;
  817. }
  818. superset++;
  819. }
  820. }
  821. #ifdef CONFIG_OMAP_MUX
  822. static void omap_mux_init_package(struct omap_mux *superset,
  823. struct omap_mux *package_subset,
  824. struct omap_ball *package_balls)
  825. {
  826. if (package_subset)
  827. omap_mux_package_fixup(package_subset, superset);
  828. if (package_balls)
  829. omap_mux_package_init_balls(package_balls, superset);
  830. }
  831. static void omap_mux_init_signals(struct omap_mux_partition *partition,
  832. struct omap_board_mux *board_mux)
  833. {
  834. omap_mux_set_cmdline_signals();
  835. omap_mux_write_array(partition, board_mux);
  836. }
  837. #else
  838. static void omap_mux_init_package(struct omap_mux *superset,
  839. struct omap_mux *package_subset,
  840. struct omap_ball *package_balls)
  841. {
  842. }
  843. static void omap_mux_init_signals(struct omap_mux_partition *partition,
  844. struct omap_board_mux *board_mux)
  845. {
  846. }
  847. #endif
  848. static u32 mux_partitions_cnt;
  849. int __init omap_mux_init(const char *name, u32 flags,
  850. u32 mux_pbase, u32 mux_size,
  851. struct omap_mux *superset,
  852. struct omap_mux *package_subset,
  853. struct omap_board_mux *board_mux,
  854. struct omap_ball *package_balls)
  855. {
  856. struct omap_mux_partition *partition;
  857. partition = kzalloc(sizeof(struct omap_mux_partition), GFP_KERNEL);
  858. if (!partition)
  859. return -ENOMEM;
  860. partition->name = name;
  861. partition->flags = flags;
  862. partition->size = mux_size;
  863. partition->phys = mux_pbase;
  864. partition->base = ioremap(mux_pbase, mux_size);
  865. if (!partition->base) {
  866. pr_err("%s: Could not ioremap mux partition at 0x%08x\n",
  867. __func__, partition->phys);
  868. kfree(partition);
  869. return -ENODEV;
  870. }
  871. INIT_LIST_HEAD(&partition->muxmodes);
  872. list_add_tail(&partition->node, &mux_partitions);
  873. mux_partitions_cnt++;
  874. pr_info("%s: Add partition: #%d: %s, flags: %x\n", __func__,
  875. mux_partitions_cnt, partition->name, partition->flags);
  876. omap_mux_init_package(superset, package_subset, package_balls);
  877. omap_mux_init_list(partition, superset);
  878. omap_mux_init_signals(partition, board_mux);
  879. return 0;
  880. }