mux.c 26 KB

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