mux.c 26 KB

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