mux.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158
  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 <linux/irq.h>
  36. #include <linux/interrupt.h>
  37. #include <plat/omap_hwmod.h>
  38. #include "control.h"
  39. #include "mux.h"
  40. #include "prm.h"
  41. #define OMAP_MUX_BASE_OFFSET 0x30 /* Offset from CTRL_BASE */
  42. #define OMAP_MUX_BASE_SZ 0x5ca
  43. struct omap_mux_entry {
  44. struct omap_mux mux;
  45. struct list_head node;
  46. };
  47. static LIST_HEAD(mux_partitions);
  48. static DEFINE_MUTEX(muxmode_mutex);
  49. struct omap_mux_partition *omap_mux_get(const char *name)
  50. {
  51. struct omap_mux_partition *partition;
  52. list_for_each_entry(partition, &mux_partitions, node) {
  53. if (!strcmp(name, partition->name))
  54. return partition;
  55. }
  56. return NULL;
  57. }
  58. u16 omap_mux_read(struct omap_mux_partition *partition, u16 reg)
  59. {
  60. if (partition->flags & OMAP_MUX_REG_8BIT)
  61. return __raw_readb(partition->base + reg);
  62. else
  63. return __raw_readw(partition->base + reg);
  64. }
  65. void omap_mux_write(struct omap_mux_partition *partition, u16 val,
  66. u16 reg)
  67. {
  68. if (partition->flags & OMAP_MUX_REG_8BIT)
  69. __raw_writeb(val, partition->base + reg);
  70. else
  71. __raw_writew(val, partition->base + reg);
  72. }
  73. void omap_mux_write_array(struct omap_mux_partition *partition,
  74. struct omap_board_mux *board_mux)
  75. {
  76. if (!board_mux)
  77. return;
  78. while (board_mux->reg_offset != OMAP_MUX_TERMINATOR) {
  79. omap_mux_write(partition, board_mux->value,
  80. board_mux->reg_offset);
  81. board_mux++;
  82. }
  83. }
  84. #ifdef CONFIG_OMAP_MUX
  85. static char *omap_mux_options;
  86. static int __init _omap_mux_init_gpio(struct omap_mux_partition *partition,
  87. int gpio, int val)
  88. {
  89. struct omap_mux_entry *e;
  90. struct omap_mux *gpio_mux = NULL;
  91. u16 old_mode;
  92. u16 mux_mode;
  93. int found = 0;
  94. struct list_head *muxmodes = &partition->muxmodes;
  95. if (!gpio)
  96. return -EINVAL;
  97. list_for_each_entry(e, muxmodes, node) {
  98. struct omap_mux *m = &e->mux;
  99. if (gpio == m->gpio) {
  100. gpio_mux = m;
  101. found++;
  102. }
  103. }
  104. if (found == 0) {
  105. pr_err("%s: Could not set gpio%i\n", __func__, gpio);
  106. return -ENODEV;
  107. }
  108. if (found > 1) {
  109. pr_info("%s: Multiple gpio paths (%d) for gpio%i\n", __func__,
  110. found, gpio);
  111. return -EINVAL;
  112. }
  113. old_mode = omap_mux_read(partition, gpio_mux->reg_offset);
  114. mux_mode = val & ~(OMAP_MUX_NR_MODES - 1);
  115. if (partition->flags & OMAP_MUX_GPIO_IN_MODE3)
  116. mux_mode |= OMAP_MUX_MODE3;
  117. else
  118. mux_mode |= OMAP_MUX_MODE4;
  119. pr_debug("%s: Setting signal %s.gpio%i 0x%04x -> 0x%04x\n", __func__,
  120. gpio_mux->muxnames[0], gpio, old_mode, mux_mode);
  121. omap_mux_write(partition, mux_mode, gpio_mux->reg_offset);
  122. return 0;
  123. }
  124. int __init omap_mux_init_gpio(int gpio, int val)
  125. {
  126. struct omap_mux_partition *partition;
  127. int ret;
  128. list_for_each_entry(partition, &mux_partitions, node) {
  129. ret = _omap_mux_init_gpio(partition, gpio, val);
  130. if (!ret)
  131. return ret;
  132. }
  133. return -ENODEV;
  134. }
  135. static int __init _omap_mux_get_by_name(struct omap_mux_partition *partition,
  136. const char *muxname,
  137. struct omap_mux **found_mux)
  138. {
  139. struct omap_mux *mux = NULL;
  140. struct omap_mux_entry *e;
  141. const char *mode_name;
  142. int found = 0, found_mode = 0, mode0_len = 0;
  143. struct list_head *muxmodes = &partition->muxmodes;
  144. mode_name = strchr(muxname, '.');
  145. if (mode_name) {
  146. mode0_len = strlen(muxname) - strlen(mode_name);
  147. mode_name++;
  148. } else {
  149. mode_name = muxname;
  150. }
  151. list_for_each_entry(e, muxmodes, node) {
  152. char *m0_entry;
  153. int i;
  154. mux = &e->mux;
  155. m0_entry = mux->muxnames[0];
  156. /* First check for full name in mode0.muxmode format */
  157. if (mode0_len && strncmp(muxname, m0_entry, mode0_len))
  158. continue;
  159. /* Then check for muxmode only */
  160. for (i = 0; i < OMAP_MUX_NR_MODES; i++) {
  161. char *mode_cur = mux->muxnames[i];
  162. if (!mode_cur)
  163. continue;
  164. if (!strcmp(mode_name, mode_cur)) {
  165. *found_mux = mux;
  166. found++;
  167. found_mode = i;
  168. }
  169. }
  170. }
  171. if (found == 1) {
  172. return found_mode;
  173. }
  174. if (found > 1) {
  175. pr_err("%s: Multiple signal paths (%i) for %s\n", __func__,
  176. found, muxname);
  177. return -EINVAL;
  178. }
  179. pr_err("%s: Could not find signal %s\n", __func__, muxname);
  180. return -ENODEV;
  181. }
  182. static int __init
  183. omap_mux_get_by_name(const char *muxname,
  184. struct omap_mux_partition **found_partition,
  185. struct omap_mux **found_mux)
  186. {
  187. struct omap_mux_partition *partition;
  188. list_for_each_entry(partition, &mux_partitions, node) {
  189. struct omap_mux *mux = NULL;
  190. int mux_mode = _omap_mux_get_by_name(partition, muxname, &mux);
  191. if (mux_mode < 0)
  192. continue;
  193. *found_partition = partition;
  194. *found_mux = mux;
  195. return mux_mode;
  196. }
  197. return -ENODEV;
  198. }
  199. int __init omap_mux_init_signal(const char *muxname, int val)
  200. {
  201. struct omap_mux_partition *partition = NULL;
  202. struct omap_mux *mux = NULL;
  203. u16 old_mode;
  204. int mux_mode;
  205. mux_mode = omap_mux_get_by_name(muxname, &partition, &mux);
  206. if (mux_mode < 0)
  207. return mux_mode;
  208. old_mode = omap_mux_read(partition, mux->reg_offset);
  209. mux_mode |= val;
  210. pr_debug("%s: Setting signal %s 0x%04x -> 0x%04x\n",
  211. __func__, muxname, old_mode, mux_mode);
  212. omap_mux_write(partition, mux_mode, mux->reg_offset);
  213. return 0;
  214. }
  215. struct omap_hwmod_mux_info * __init
  216. omap_hwmod_mux_init(struct omap_device_pad *bpads, int nr_pads)
  217. {
  218. struct omap_hwmod_mux_info *hmux;
  219. int i, nr_pads_dynamic = 0;
  220. if (!bpads || nr_pads < 1)
  221. return NULL;
  222. hmux = kzalloc(sizeof(struct omap_hwmod_mux_info), GFP_KERNEL);
  223. if (!hmux)
  224. goto err1;
  225. hmux->nr_pads = nr_pads;
  226. hmux->pads = kzalloc(sizeof(struct omap_device_pad) *
  227. nr_pads, GFP_KERNEL);
  228. if (!hmux->pads)
  229. goto err2;
  230. for (i = 0; i < hmux->nr_pads; i++) {
  231. struct omap_mux_partition *partition;
  232. struct omap_device_pad *bpad = &bpads[i], *pad = &hmux->pads[i];
  233. struct omap_mux *mux;
  234. int mux_mode;
  235. mux_mode = omap_mux_get_by_name(bpad->name, &partition, &mux);
  236. if (mux_mode < 0)
  237. goto err3;
  238. if (!pad->partition)
  239. pad->partition = partition;
  240. if (!pad->mux)
  241. pad->mux = mux;
  242. pad->name = kzalloc(strlen(bpad->name) + 1, GFP_KERNEL);
  243. if (!pad->name) {
  244. int j;
  245. for (j = i - 1; j >= 0; j--)
  246. kfree(hmux->pads[j].name);
  247. goto err3;
  248. }
  249. strcpy(pad->name, bpad->name);
  250. pad->flags = bpad->flags;
  251. pad->enable = bpad->enable;
  252. pad->idle = bpad->idle;
  253. pad->off = bpad->off;
  254. if (pad->flags &
  255. (OMAP_DEVICE_PAD_REMUX | OMAP_DEVICE_PAD_WAKEUP))
  256. nr_pads_dynamic++;
  257. pr_debug("%s: Initialized %s\n", __func__, pad->name);
  258. }
  259. if (!nr_pads_dynamic)
  260. return hmux;
  261. /*
  262. * Add pads that need dynamic muxing into a separate list
  263. */
  264. hmux->nr_pads_dynamic = nr_pads_dynamic;
  265. hmux->pads_dynamic = kzalloc(sizeof(struct omap_device_pad *) *
  266. nr_pads_dynamic, GFP_KERNEL);
  267. if (!hmux->pads_dynamic) {
  268. pr_err("%s: Could not allocate dynamic pads\n", __func__);
  269. return hmux;
  270. }
  271. nr_pads_dynamic = 0;
  272. for (i = 0; i < hmux->nr_pads; i++) {
  273. struct omap_device_pad *pad = &hmux->pads[i];
  274. if (pad->flags &
  275. (OMAP_DEVICE_PAD_REMUX | OMAP_DEVICE_PAD_WAKEUP)) {
  276. pr_debug("%s: pad %s tagged dynamic\n",
  277. __func__, pad->name);
  278. hmux->pads_dynamic[nr_pads_dynamic] = pad;
  279. nr_pads_dynamic++;
  280. }
  281. }
  282. return hmux;
  283. err3:
  284. kfree(hmux->pads);
  285. err2:
  286. kfree(hmux);
  287. err1:
  288. pr_err("%s: Could not allocate device mux entry\n", __func__);
  289. return NULL;
  290. }
  291. /**
  292. * omap_hwmod_mux_scan_wakeups - omap hwmod scan wakeup pads
  293. * @hmux: Pads for a hwmod
  294. * @mpu_irqs: MPU irq array for a hwmod
  295. *
  296. * Scans the wakeup status of pads for a single hwmod. If an irq
  297. * array is defined for this mux, the parser will call the registered
  298. * ISRs for corresponding pads, otherwise the parser will stop at the
  299. * first wakeup active pad and return. Returns true if there is a
  300. * pending and non-served wakeup event for the mux, otherwise false.
  301. */
  302. static bool omap_hwmod_mux_scan_wakeups(struct omap_hwmod_mux_info *hmux,
  303. struct omap_hwmod_irq_info *mpu_irqs)
  304. {
  305. int i, irq;
  306. unsigned int val;
  307. u32 handled_irqs = 0;
  308. for (i = 0; i < hmux->nr_pads_dynamic; i++) {
  309. struct omap_device_pad *pad = hmux->pads_dynamic[i];
  310. if (!(pad->flags & OMAP_DEVICE_PAD_WAKEUP) ||
  311. !(pad->idle & OMAP_WAKEUP_EN))
  312. continue;
  313. val = omap_mux_read(pad->partition, pad->mux->reg_offset);
  314. if (!(val & OMAP_WAKEUP_EVENT))
  315. continue;
  316. if (!hmux->irqs)
  317. return true;
  318. irq = hmux->irqs[i];
  319. /* make sure we only handle each irq once */
  320. if (handled_irqs & 1 << irq)
  321. continue;
  322. handled_irqs |= 1 << irq;
  323. generic_handle_irq(mpu_irqs[irq].irq);
  324. }
  325. return false;
  326. }
  327. /**
  328. * _omap_hwmod_mux_handle_irq - Process wakeup events for a single hwmod
  329. *
  330. * Checks a single hwmod for every wakeup capable pad to see if there is an
  331. * active wakeup event. If this is the case, call the corresponding ISR.
  332. */
  333. static int _omap_hwmod_mux_handle_irq(struct omap_hwmod *oh, void *data)
  334. {
  335. if (!oh->mux || !oh->mux->enabled)
  336. return 0;
  337. if (omap_hwmod_mux_scan_wakeups(oh->mux, oh->mpu_irqs))
  338. generic_handle_irq(oh->mpu_irqs[0].irq);
  339. return 0;
  340. }
  341. /**
  342. * omap_hwmod_mux_handle_irq - Process pad wakeup irqs.
  343. *
  344. * Calls a function for each registered omap_hwmod to check
  345. * pad wakeup statuses.
  346. */
  347. static irqreturn_t omap_hwmod_mux_handle_irq(int irq, void *unused)
  348. {
  349. omap_hwmod_for_each(_omap_hwmod_mux_handle_irq, NULL);
  350. return IRQ_HANDLED;
  351. }
  352. /* Assumes the calling function takes care of locking */
  353. void omap_hwmod_mux(struct omap_hwmod_mux_info *hmux, u8 state)
  354. {
  355. int i;
  356. /* Runtime idling of dynamic pads */
  357. if (state == _HWMOD_STATE_IDLE && hmux->enabled) {
  358. for (i = 0; i < hmux->nr_pads_dynamic; i++) {
  359. struct omap_device_pad *pad = hmux->pads_dynamic[i];
  360. int val = -EINVAL;
  361. val = pad->idle;
  362. omap_mux_write(pad->partition, val,
  363. pad->mux->reg_offset);
  364. }
  365. return;
  366. }
  367. /* Runtime enabling of dynamic pads */
  368. if ((state == _HWMOD_STATE_ENABLED) && hmux->pads_dynamic
  369. && hmux->enabled) {
  370. for (i = 0; i < hmux->nr_pads_dynamic; i++) {
  371. struct omap_device_pad *pad = hmux->pads_dynamic[i];
  372. int val = -EINVAL;
  373. val = pad->enable;
  374. omap_mux_write(pad->partition, val,
  375. pad->mux->reg_offset);
  376. }
  377. return;
  378. }
  379. /* Enabling or disabling of all pads */
  380. for (i = 0; i < hmux->nr_pads; i++) {
  381. struct omap_device_pad *pad = &hmux->pads[i];
  382. int flags, val = -EINVAL;
  383. flags = pad->flags;
  384. switch (state) {
  385. case _HWMOD_STATE_ENABLED:
  386. val = pad->enable;
  387. pr_debug("%s: Enabling %s %x\n", __func__,
  388. pad->name, val);
  389. break;
  390. case _HWMOD_STATE_DISABLED:
  391. /* Use safe mode unless OMAP_DEVICE_PAD_REMUX */
  392. if (flags & OMAP_DEVICE_PAD_REMUX)
  393. val = pad->off;
  394. else
  395. val = OMAP_MUX_MODE7;
  396. pr_debug("%s: Disabling %s %x\n", __func__,
  397. pad->name, val);
  398. break;
  399. default:
  400. /* Nothing to be done */
  401. break;
  402. };
  403. if (val >= 0) {
  404. omap_mux_write(pad->partition, val,
  405. pad->mux->reg_offset);
  406. pad->flags = flags;
  407. }
  408. }
  409. if (state == _HWMOD_STATE_ENABLED)
  410. hmux->enabled = true;
  411. else
  412. hmux->enabled = false;
  413. }
  414. #ifdef CONFIG_DEBUG_FS
  415. #define OMAP_MUX_MAX_NR_FLAGS 10
  416. #define OMAP_MUX_TEST_FLAG(val, mask) \
  417. if (((val) & (mask)) == (mask)) { \
  418. i++; \
  419. flags[i] = #mask; \
  420. }
  421. /* REVISIT: Add checking for non-optimal mux settings */
  422. static inline void omap_mux_decode(struct seq_file *s, u16 val)
  423. {
  424. char *flags[OMAP_MUX_MAX_NR_FLAGS];
  425. char mode[sizeof("OMAP_MUX_MODE") + 1];
  426. int i = -1;
  427. sprintf(mode, "OMAP_MUX_MODE%d", val & 0x7);
  428. i++;
  429. flags[i] = mode;
  430. OMAP_MUX_TEST_FLAG(val, OMAP_PIN_OFF_WAKEUPENABLE);
  431. if (val & OMAP_OFF_EN) {
  432. if (!(val & OMAP_OFFOUT_EN)) {
  433. if (!(val & OMAP_OFF_PULL_UP)) {
  434. OMAP_MUX_TEST_FLAG(val,
  435. OMAP_PIN_OFF_INPUT_PULLDOWN);
  436. } else {
  437. OMAP_MUX_TEST_FLAG(val,
  438. OMAP_PIN_OFF_INPUT_PULLUP);
  439. }
  440. } else {
  441. if (!(val & OMAP_OFFOUT_VAL)) {
  442. OMAP_MUX_TEST_FLAG(val,
  443. OMAP_PIN_OFF_OUTPUT_LOW);
  444. } else {
  445. OMAP_MUX_TEST_FLAG(val,
  446. OMAP_PIN_OFF_OUTPUT_HIGH);
  447. }
  448. }
  449. }
  450. if (val & OMAP_INPUT_EN) {
  451. if (val & OMAP_PULL_ENA) {
  452. if (!(val & OMAP_PULL_UP)) {
  453. OMAP_MUX_TEST_FLAG(val,
  454. OMAP_PIN_INPUT_PULLDOWN);
  455. } else {
  456. OMAP_MUX_TEST_FLAG(val, OMAP_PIN_INPUT_PULLUP);
  457. }
  458. } else {
  459. OMAP_MUX_TEST_FLAG(val, OMAP_PIN_INPUT);
  460. }
  461. } else {
  462. i++;
  463. flags[i] = "OMAP_PIN_OUTPUT";
  464. }
  465. do {
  466. seq_printf(s, "%s", flags[i]);
  467. if (i > 0)
  468. seq_printf(s, " | ");
  469. } while (i-- > 0);
  470. }
  471. #define OMAP_MUX_DEFNAME_LEN 32
  472. static int omap_mux_dbg_board_show(struct seq_file *s, void *unused)
  473. {
  474. struct omap_mux_partition *partition = s->private;
  475. struct omap_mux_entry *e;
  476. u8 omap_gen = omap_rev() >> 28;
  477. list_for_each_entry(e, &partition->muxmodes, node) {
  478. struct omap_mux *m = &e->mux;
  479. char m0_def[OMAP_MUX_DEFNAME_LEN];
  480. char *m0_name = m->muxnames[0];
  481. u16 val;
  482. int i, mode;
  483. if (!m0_name)
  484. continue;
  485. /* REVISIT: Needs to be updated if mode0 names get longer */
  486. for (i = 0; i < OMAP_MUX_DEFNAME_LEN; i++) {
  487. if (m0_name[i] == '\0') {
  488. m0_def[i] = m0_name[i];
  489. break;
  490. }
  491. m0_def[i] = toupper(m0_name[i]);
  492. }
  493. val = omap_mux_read(partition, m->reg_offset);
  494. mode = val & OMAP_MUX_MODE7;
  495. if (mode != 0)
  496. seq_printf(s, "/* %s */\n", m->muxnames[mode]);
  497. /*
  498. * XXX: Might be revisited to support differences across
  499. * same OMAP generation.
  500. */
  501. seq_printf(s, "OMAP%d_MUX(%s, ", omap_gen, m0_def);
  502. omap_mux_decode(s, val);
  503. seq_printf(s, "),\n");
  504. }
  505. return 0;
  506. }
  507. static int omap_mux_dbg_board_open(struct inode *inode, struct file *file)
  508. {
  509. return single_open(file, omap_mux_dbg_board_show, inode->i_private);
  510. }
  511. static const struct file_operations omap_mux_dbg_board_fops = {
  512. .open = omap_mux_dbg_board_open,
  513. .read = seq_read,
  514. .llseek = seq_lseek,
  515. .release = single_release,
  516. };
  517. static struct omap_mux_partition *omap_mux_get_partition(struct omap_mux *mux)
  518. {
  519. struct omap_mux_partition *partition;
  520. list_for_each_entry(partition, &mux_partitions, node) {
  521. struct list_head *muxmodes = &partition->muxmodes;
  522. struct omap_mux_entry *e;
  523. list_for_each_entry(e, muxmodes, node) {
  524. struct omap_mux *m = &e->mux;
  525. if (m == mux)
  526. return partition;
  527. }
  528. }
  529. return NULL;
  530. }
  531. static int omap_mux_dbg_signal_show(struct seq_file *s, void *unused)
  532. {
  533. struct omap_mux *m = s->private;
  534. struct omap_mux_partition *partition;
  535. const char *none = "NA";
  536. u16 val;
  537. int mode;
  538. partition = omap_mux_get_partition(m);
  539. if (!partition)
  540. return 0;
  541. val = omap_mux_read(partition, m->reg_offset);
  542. mode = val & OMAP_MUX_MODE7;
  543. seq_printf(s, "name: %s.%s (0x%08x/0x%03x = 0x%04x), b %s, t %s\n",
  544. m->muxnames[0], m->muxnames[mode],
  545. partition->phys + m->reg_offset, m->reg_offset, val,
  546. m->balls[0] ? m->balls[0] : none,
  547. m->balls[1] ? m->balls[1] : none);
  548. seq_printf(s, "mode: ");
  549. omap_mux_decode(s, val);
  550. seq_printf(s, "\n");
  551. seq_printf(s, "signals: %s | %s | %s | %s | %s | %s | %s | %s\n",
  552. m->muxnames[0] ? m->muxnames[0] : none,
  553. m->muxnames[1] ? m->muxnames[1] : none,
  554. m->muxnames[2] ? m->muxnames[2] : none,
  555. m->muxnames[3] ? m->muxnames[3] : none,
  556. m->muxnames[4] ? m->muxnames[4] : none,
  557. m->muxnames[5] ? m->muxnames[5] : none,
  558. m->muxnames[6] ? m->muxnames[6] : none,
  559. m->muxnames[7] ? m->muxnames[7] : none);
  560. return 0;
  561. }
  562. #define OMAP_MUX_MAX_ARG_CHAR 7
  563. static ssize_t omap_mux_dbg_signal_write(struct file *file,
  564. const char __user *user_buf,
  565. size_t count, loff_t *ppos)
  566. {
  567. char buf[OMAP_MUX_MAX_ARG_CHAR];
  568. struct seq_file *seqf;
  569. struct omap_mux *m;
  570. unsigned long val;
  571. int buf_size, ret;
  572. struct omap_mux_partition *partition;
  573. if (count > OMAP_MUX_MAX_ARG_CHAR)
  574. return -EINVAL;
  575. memset(buf, 0, sizeof(buf));
  576. buf_size = min(count, sizeof(buf) - 1);
  577. if (copy_from_user(buf, user_buf, buf_size))
  578. return -EFAULT;
  579. ret = strict_strtoul(buf, 0x10, &val);
  580. if (ret < 0)
  581. return ret;
  582. if (val > 0xffff)
  583. return -EINVAL;
  584. seqf = file->private_data;
  585. m = seqf->private;
  586. partition = omap_mux_get_partition(m);
  587. if (!partition)
  588. return -ENODEV;
  589. omap_mux_write(partition, (u16)val, m->reg_offset);
  590. *ppos += count;
  591. return count;
  592. }
  593. static int omap_mux_dbg_signal_open(struct inode *inode, struct file *file)
  594. {
  595. return single_open(file, omap_mux_dbg_signal_show, inode->i_private);
  596. }
  597. static const struct file_operations omap_mux_dbg_signal_fops = {
  598. .open = omap_mux_dbg_signal_open,
  599. .read = seq_read,
  600. .write = omap_mux_dbg_signal_write,
  601. .llseek = seq_lseek,
  602. .release = single_release,
  603. };
  604. static struct dentry *mux_dbg_dir;
  605. static void __init omap_mux_dbg_create_entry(
  606. struct omap_mux_partition *partition,
  607. struct dentry *mux_dbg_dir)
  608. {
  609. struct omap_mux_entry *e;
  610. list_for_each_entry(e, &partition->muxmodes, node) {
  611. struct omap_mux *m = &e->mux;
  612. (void)debugfs_create_file(m->muxnames[0], S_IWUSR, mux_dbg_dir,
  613. m, &omap_mux_dbg_signal_fops);
  614. }
  615. }
  616. static void __init omap_mux_dbg_init(void)
  617. {
  618. struct omap_mux_partition *partition;
  619. static struct dentry *mux_dbg_board_dir;
  620. mux_dbg_dir = debugfs_create_dir("omap_mux", NULL);
  621. if (!mux_dbg_dir)
  622. return;
  623. mux_dbg_board_dir = debugfs_create_dir("board", mux_dbg_dir);
  624. if (!mux_dbg_board_dir)
  625. return;
  626. list_for_each_entry(partition, &mux_partitions, node) {
  627. omap_mux_dbg_create_entry(partition, mux_dbg_dir);
  628. (void)debugfs_create_file(partition->name, S_IRUGO,
  629. mux_dbg_board_dir, partition,
  630. &omap_mux_dbg_board_fops);
  631. }
  632. }
  633. #else
  634. static inline void omap_mux_dbg_init(void)
  635. {
  636. }
  637. #endif /* CONFIG_DEBUG_FS */
  638. static void __init omap_mux_free_names(struct omap_mux *m)
  639. {
  640. int i;
  641. for (i = 0; i < OMAP_MUX_NR_MODES; i++)
  642. kfree(m->muxnames[i]);
  643. #ifdef CONFIG_DEBUG_FS
  644. for (i = 0; i < OMAP_MUX_NR_SIDES; i++)
  645. kfree(m->balls[i]);
  646. #endif
  647. }
  648. /* Free all data except for GPIO pins unless CONFIG_DEBUG_FS is set */
  649. static int __init omap_mux_late_init(void)
  650. {
  651. struct omap_mux_partition *partition;
  652. int ret;
  653. list_for_each_entry(partition, &mux_partitions, node) {
  654. struct omap_mux_entry *e, *tmp;
  655. list_for_each_entry_safe(e, tmp, &partition->muxmodes, node) {
  656. struct omap_mux *m = &e->mux;
  657. u16 mode = omap_mux_read(partition, m->reg_offset);
  658. if (OMAP_MODE_GPIO(mode))
  659. continue;
  660. #ifndef CONFIG_DEBUG_FS
  661. mutex_lock(&muxmode_mutex);
  662. list_del(&e->node);
  663. mutex_unlock(&muxmode_mutex);
  664. omap_mux_free_names(m);
  665. kfree(m);
  666. #endif
  667. }
  668. }
  669. ret = request_irq(omap_prcm_event_to_irq("io"),
  670. omap_hwmod_mux_handle_irq, IRQF_SHARED | IRQF_NO_SUSPEND,
  671. "hwmod_io", omap_mux_late_init);
  672. if (ret)
  673. pr_warning("mux: Failed to setup hwmod io irq %d\n", ret);
  674. omap_mux_dbg_init();
  675. return 0;
  676. }
  677. late_initcall(omap_mux_late_init);
  678. static void __init omap_mux_package_fixup(struct omap_mux *p,
  679. struct omap_mux *superset)
  680. {
  681. while (p->reg_offset != OMAP_MUX_TERMINATOR) {
  682. struct omap_mux *s = superset;
  683. int found = 0;
  684. while (s->reg_offset != OMAP_MUX_TERMINATOR) {
  685. if (s->reg_offset == p->reg_offset) {
  686. *s = *p;
  687. found++;
  688. break;
  689. }
  690. s++;
  691. }
  692. if (!found)
  693. pr_err("%s: Unknown entry offset 0x%x\n", __func__,
  694. p->reg_offset);
  695. p++;
  696. }
  697. }
  698. #ifdef CONFIG_DEBUG_FS
  699. static void __init omap_mux_package_init_balls(struct omap_ball *b,
  700. struct omap_mux *superset)
  701. {
  702. while (b->reg_offset != OMAP_MUX_TERMINATOR) {
  703. struct omap_mux *s = superset;
  704. int found = 0;
  705. while (s->reg_offset != OMAP_MUX_TERMINATOR) {
  706. if (s->reg_offset == b->reg_offset) {
  707. s->balls[0] = b->balls[0];
  708. s->balls[1] = b->balls[1];
  709. found++;
  710. break;
  711. }
  712. s++;
  713. }
  714. if (!found)
  715. pr_err("%s: Unknown ball offset 0x%x\n", __func__,
  716. b->reg_offset);
  717. b++;
  718. }
  719. }
  720. #else /* CONFIG_DEBUG_FS */
  721. static inline void omap_mux_package_init_balls(struct omap_ball *b,
  722. struct omap_mux *superset)
  723. {
  724. }
  725. #endif /* CONFIG_DEBUG_FS */
  726. static int __init omap_mux_setup(char *options)
  727. {
  728. if (!options)
  729. return 0;
  730. omap_mux_options = options;
  731. return 1;
  732. }
  733. __setup("omap_mux=", omap_mux_setup);
  734. /*
  735. * Note that the omap_mux=some.signal1=0x1234,some.signal2=0x1234
  736. * cmdline options only override the bootloader values.
  737. * During development, please enable CONFIG_DEBUG_FS, and use the
  738. * signal specific entries under debugfs.
  739. */
  740. static void __init omap_mux_set_cmdline_signals(void)
  741. {
  742. char *options, *next_opt, *token;
  743. if (!omap_mux_options)
  744. return;
  745. options = kstrdup(omap_mux_options, GFP_KERNEL);
  746. if (!options)
  747. return;
  748. next_opt = options;
  749. while ((token = strsep(&next_opt, ",")) != NULL) {
  750. char *keyval, *name;
  751. unsigned long val;
  752. keyval = token;
  753. name = strsep(&keyval, "=");
  754. if (name) {
  755. int res;
  756. res = strict_strtoul(keyval, 0x10, &val);
  757. if (res < 0)
  758. continue;
  759. omap_mux_init_signal(name, (u16)val);
  760. }
  761. }
  762. kfree(options);
  763. }
  764. static int __init omap_mux_copy_names(struct omap_mux *src,
  765. struct omap_mux *dst)
  766. {
  767. int i;
  768. for (i = 0; i < OMAP_MUX_NR_MODES; i++) {
  769. if (src->muxnames[i]) {
  770. dst->muxnames[i] = kstrdup(src->muxnames[i],
  771. GFP_KERNEL);
  772. if (!dst->muxnames[i])
  773. goto free;
  774. }
  775. }
  776. #ifdef CONFIG_DEBUG_FS
  777. for (i = 0; i < OMAP_MUX_NR_SIDES; i++) {
  778. if (src->balls[i]) {
  779. dst->balls[i] = kstrdup(src->balls[i], GFP_KERNEL);
  780. if (!dst->balls[i])
  781. goto free;
  782. }
  783. }
  784. #endif
  785. return 0;
  786. free:
  787. omap_mux_free_names(dst);
  788. return -ENOMEM;
  789. }
  790. #endif /* CONFIG_OMAP_MUX */
  791. static struct omap_mux *omap_mux_get_by_gpio(
  792. struct omap_mux_partition *partition,
  793. int gpio)
  794. {
  795. struct omap_mux_entry *e;
  796. struct omap_mux *ret = NULL;
  797. list_for_each_entry(e, &partition->muxmodes, node) {
  798. struct omap_mux *m = &e->mux;
  799. if (m->gpio == gpio) {
  800. ret = m;
  801. break;
  802. }
  803. }
  804. return ret;
  805. }
  806. /* Needed for dynamic muxing of GPIO pins for off-idle */
  807. u16 omap_mux_get_gpio(int gpio)
  808. {
  809. struct omap_mux_partition *partition;
  810. struct omap_mux *m = NULL;
  811. list_for_each_entry(partition, &mux_partitions, node) {
  812. m = omap_mux_get_by_gpio(partition, gpio);
  813. if (m)
  814. return omap_mux_read(partition, m->reg_offset);
  815. }
  816. if (!m || m->reg_offset == OMAP_MUX_TERMINATOR)
  817. pr_err("%s: Could not get gpio%i\n", __func__, gpio);
  818. return OMAP_MUX_TERMINATOR;
  819. }
  820. /* Needed for dynamic muxing of GPIO pins for off-idle */
  821. void omap_mux_set_gpio(u16 val, int gpio)
  822. {
  823. struct omap_mux_partition *partition;
  824. struct omap_mux *m = NULL;
  825. list_for_each_entry(partition, &mux_partitions, node) {
  826. m = omap_mux_get_by_gpio(partition, gpio);
  827. if (m) {
  828. omap_mux_write(partition, val, m->reg_offset);
  829. return;
  830. }
  831. }
  832. if (!m || m->reg_offset == OMAP_MUX_TERMINATOR)
  833. pr_err("%s: Could not set gpio%i\n", __func__, gpio);
  834. }
  835. static struct omap_mux * __init omap_mux_list_add(
  836. struct omap_mux_partition *partition,
  837. struct omap_mux *src)
  838. {
  839. struct omap_mux_entry *entry;
  840. struct omap_mux *m;
  841. entry = kzalloc(sizeof(struct omap_mux_entry), GFP_KERNEL);
  842. if (!entry)
  843. return NULL;
  844. m = &entry->mux;
  845. entry->mux = *src;
  846. #ifdef CONFIG_OMAP_MUX
  847. if (omap_mux_copy_names(src, m)) {
  848. kfree(entry);
  849. return NULL;
  850. }
  851. #endif
  852. mutex_lock(&muxmode_mutex);
  853. list_add_tail(&entry->node, &partition->muxmodes);
  854. mutex_unlock(&muxmode_mutex);
  855. return m;
  856. }
  857. /*
  858. * Note if CONFIG_OMAP_MUX is not selected, we will only initialize
  859. * the GPIO to mux offset mapping that is needed for dynamic muxing
  860. * of GPIO pins for off-idle.
  861. */
  862. static void __init omap_mux_init_list(struct omap_mux_partition *partition,
  863. struct omap_mux *superset)
  864. {
  865. while (superset->reg_offset != OMAP_MUX_TERMINATOR) {
  866. struct omap_mux *entry;
  867. #ifdef CONFIG_OMAP_MUX
  868. if (!superset->muxnames || !superset->muxnames[0]) {
  869. superset++;
  870. continue;
  871. }
  872. #else
  873. /* Skip pins that are not muxed as GPIO by bootloader */
  874. if (!OMAP_MODE_GPIO(omap_mux_read(partition,
  875. superset->reg_offset))) {
  876. superset++;
  877. continue;
  878. }
  879. #endif
  880. entry = omap_mux_list_add(partition, superset);
  881. if (!entry) {
  882. pr_err("%s: Could not add entry\n", __func__);
  883. return;
  884. }
  885. superset++;
  886. }
  887. }
  888. #ifdef CONFIG_OMAP_MUX
  889. static void omap_mux_init_package(struct omap_mux *superset,
  890. struct omap_mux *package_subset,
  891. struct omap_ball *package_balls)
  892. {
  893. if (package_subset)
  894. omap_mux_package_fixup(package_subset, superset);
  895. if (package_balls)
  896. omap_mux_package_init_balls(package_balls, superset);
  897. }
  898. static void __init omap_mux_init_signals(struct omap_mux_partition *partition,
  899. struct omap_board_mux *board_mux)
  900. {
  901. omap_mux_set_cmdline_signals();
  902. omap_mux_write_array(partition, board_mux);
  903. }
  904. #else
  905. static void omap_mux_init_package(struct omap_mux *superset,
  906. struct omap_mux *package_subset,
  907. struct omap_ball *package_balls)
  908. {
  909. }
  910. static void __init omap_mux_init_signals(struct omap_mux_partition *partition,
  911. struct omap_board_mux *board_mux)
  912. {
  913. }
  914. #endif
  915. static u32 mux_partitions_cnt;
  916. int __init omap_mux_init(const char *name, u32 flags,
  917. u32 mux_pbase, u32 mux_size,
  918. struct omap_mux *superset,
  919. struct omap_mux *package_subset,
  920. struct omap_board_mux *board_mux,
  921. struct omap_ball *package_balls)
  922. {
  923. struct omap_mux_partition *partition;
  924. partition = kzalloc(sizeof(struct omap_mux_partition), GFP_KERNEL);
  925. if (!partition)
  926. return -ENOMEM;
  927. partition->name = name;
  928. partition->flags = flags;
  929. partition->size = mux_size;
  930. partition->phys = mux_pbase;
  931. partition->base = ioremap(mux_pbase, mux_size);
  932. if (!partition->base) {
  933. pr_err("%s: Could not ioremap mux partition at 0x%08x\n",
  934. __func__, partition->phys);
  935. kfree(partition);
  936. return -ENODEV;
  937. }
  938. INIT_LIST_HEAD(&partition->muxmodes);
  939. list_add_tail(&partition->node, &mux_partitions);
  940. mux_partitions_cnt++;
  941. pr_info("%s: Add partition: #%d: %s, flags: %x\n", __func__,
  942. mux_partitions_cnt, partition->name, partition->flags);
  943. omap_mux_init_package(superset, package_subset, package_balls);
  944. omap_mux_init_list(partition, superset);
  945. omap_mux_init_signals(partition, board_mux);
  946. return 0;
  947. }