mux.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  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 <asm/system.h>
  38. #include <plat/omap_hwmod.h>
  39. #include "control.h"
  40. #include "mux.h"
  41. #include "prm.h"
  42. #define OMAP_MUX_BASE_OFFSET 0x30 /* Offset from CTRL_BASE */
  43. #define OMAP_MUX_BASE_SZ 0x5ca
  44. struct omap_mux_entry {
  45. struct omap_mux mux;
  46. struct list_head node;
  47. };
  48. static LIST_HEAD(mux_partitions);
  49. static DEFINE_MUTEX(muxmode_mutex);
  50. struct omap_mux_partition *omap_mux_get(const char *name)
  51. {
  52. struct omap_mux_partition *partition;
  53. list_for_each_entry(partition, &mux_partitions, node) {
  54. if (!strcmp(name, partition->name))
  55. return partition;
  56. }
  57. return NULL;
  58. }
  59. u16 omap_mux_read(struct omap_mux_partition *partition, u16 reg)
  60. {
  61. if (partition->flags & OMAP_MUX_REG_8BIT)
  62. return __raw_readb(partition->base + reg);
  63. else
  64. return __raw_readw(partition->base + reg);
  65. }
  66. void omap_mux_write(struct omap_mux_partition *partition, u16 val,
  67. u16 reg)
  68. {
  69. if (partition->flags & OMAP_MUX_REG_8BIT)
  70. __raw_writeb(val, partition->base + reg);
  71. else
  72. __raw_writew(val, partition->base + reg);
  73. }
  74. void omap_mux_write_array(struct omap_mux_partition *partition,
  75. struct omap_board_mux *board_mux)
  76. {
  77. if (!board_mux)
  78. return;
  79. while (board_mux->reg_offset != OMAP_MUX_TERMINATOR) {
  80. omap_mux_write(partition, board_mux->value,
  81. board_mux->reg_offset);
  82. board_mux++;
  83. }
  84. }
  85. #ifdef CONFIG_OMAP_MUX
  86. static char *omap_mux_options;
  87. static int _omap_mux_init_gpio(struct omap_mux_partition *partition,
  88. int gpio, int val)
  89. {
  90. struct omap_mux_entry *e;
  91. struct omap_mux *gpio_mux = NULL;
  92. u16 old_mode;
  93. u16 mux_mode;
  94. int found = 0;
  95. struct list_head *muxmodes = &partition->muxmodes;
  96. if (!gpio)
  97. return -EINVAL;
  98. list_for_each_entry(e, muxmodes, node) {
  99. struct omap_mux *m = &e->mux;
  100. if (gpio == m->gpio) {
  101. gpio_mux = m;
  102. found++;
  103. }
  104. }
  105. if (found == 0) {
  106. pr_err("%s: Could not set gpio%i\n", __func__, gpio);
  107. return -ENODEV;
  108. }
  109. if (found > 1) {
  110. pr_info("%s: Multiple gpio paths (%d) for gpio%i\n", __func__,
  111. found, gpio);
  112. return -EINVAL;
  113. }
  114. old_mode = omap_mux_read(partition, gpio_mux->reg_offset);
  115. mux_mode = val & ~(OMAP_MUX_NR_MODES - 1);
  116. if (partition->flags & OMAP_MUX_GPIO_IN_MODE3)
  117. mux_mode |= OMAP_MUX_MODE3;
  118. else
  119. mux_mode |= OMAP_MUX_MODE4;
  120. pr_debug("%s: Setting signal %s.gpio%i 0x%04x -> 0x%04x\n", __func__,
  121. gpio_mux->muxnames[0], gpio, old_mode, mux_mode);
  122. omap_mux_write(partition, mux_mode, gpio_mux->reg_offset);
  123. return 0;
  124. }
  125. int omap_mux_init_gpio(int gpio, int val)
  126. {
  127. struct omap_mux_partition *partition;
  128. int ret;
  129. list_for_each_entry(partition, &mux_partitions, node) {
  130. ret = _omap_mux_init_gpio(partition, gpio, val);
  131. if (!ret)
  132. return ret;
  133. }
  134. return -ENODEV;
  135. }
  136. static int _omap_mux_get_by_name(struct omap_mux_partition *partition,
  137. const char *muxname,
  138. struct omap_mux **found_mux)
  139. {
  140. struct omap_mux *mux = NULL;
  141. struct omap_mux_entry *e;
  142. const char *mode_name;
  143. int found = 0, found_mode = 0, mode0_len = 0;
  144. struct list_head *muxmodes = &partition->muxmodes;
  145. mode_name = strchr(muxname, '.');
  146. if (mode_name) {
  147. mode0_len = strlen(muxname) - strlen(mode_name);
  148. mode_name++;
  149. } else {
  150. mode_name = muxname;
  151. }
  152. list_for_each_entry(e, muxmodes, node) {
  153. char *m0_entry;
  154. int i;
  155. mux = &e->mux;
  156. m0_entry = mux->muxnames[0];
  157. /* First check for full name in mode0.muxmode format */
  158. if (mode0_len && strncmp(muxname, m0_entry, mode0_len))
  159. continue;
  160. /* Then check for muxmode only */
  161. for (i = 0; i < OMAP_MUX_NR_MODES; i++) {
  162. char *mode_cur = mux->muxnames[i];
  163. if (!mode_cur)
  164. continue;
  165. if (!strcmp(mode_name, mode_cur)) {
  166. *found_mux = mux;
  167. found++;
  168. found_mode = i;
  169. }
  170. }
  171. }
  172. if (found == 1) {
  173. return found_mode;
  174. }
  175. if (found > 1) {
  176. pr_err("%s: Multiple signal paths (%i) for %s\n", __func__,
  177. found, muxname);
  178. return -EINVAL;
  179. }
  180. pr_err("%s: Could not find signal %s\n", __func__, muxname);
  181. return -ENODEV;
  182. }
  183. static int
  184. omap_mux_get_by_name(const char *muxname,
  185. struct omap_mux_partition **found_partition,
  186. struct omap_mux **found_mux)
  187. {
  188. struct omap_mux_partition *partition;
  189. list_for_each_entry(partition, &mux_partitions, node) {
  190. struct omap_mux *mux = NULL;
  191. int mux_mode = _omap_mux_get_by_name(partition, muxname, &mux);
  192. if (mux_mode < 0)
  193. continue;
  194. *found_partition = partition;
  195. *found_mux = mux;
  196. return mux_mode;
  197. }
  198. return -ENODEV;
  199. }
  200. int omap_mux_init_signal(const char *muxname, int val)
  201. {
  202. struct omap_mux_partition *partition = NULL;
  203. struct omap_mux *mux = NULL;
  204. u16 old_mode;
  205. int mux_mode;
  206. mux_mode = omap_mux_get_by_name(muxname, &partition, &mux);
  207. if (mux_mode < 0)
  208. return mux_mode;
  209. old_mode = omap_mux_read(partition, mux->reg_offset);
  210. mux_mode |= val;
  211. pr_debug("%s: Setting signal %s 0x%04x -> 0x%04x\n",
  212. __func__, muxname, old_mode, mux_mode);
  213. omap_mux_write(partition, mux_mode, mux->reg_offset);
  214. return 0;
  215. }
  216. struct omap_hwmod_mux_info * __init
  217. omap_hwmod_mux_init(struct omap_device_pad *bpads, int nr_pads)
  218. {
  219. struct omap_hwmod_mux_info *hmux;
  220. int i, nr_pads_dynamic = 0;
  221. if (!bpads || nr_pads < 1)
  222. return NULL;
  223. hmux = kzalloc(sizeof(struct omap_hwmod_mux_info), GFP_KERNEL);
  224. if (!hmux)
  225. goto err1;
  226. hmux->nr_pads = nr_pads;
  227. hmux->pads = kzalloc(sizeof(struct omap_device_pad) *
  228. nr_pads, GFP_KERNEL);
  229. if (!hmux->pads)
  230. goto err2;
  231. for (i = 0; i < hmux->nr_pads; i++) {
  232. struct omap_mux_partition *partition;
  233. struct omap_device_pad *bpad = &bpads[i], *pad = &hmux->pads[i];
  234. struct omap_mux *mux;
  235. int mux_mode;
  236. mux_mode = omap_mux_get_by_name(bpad->name, &partition, &mux);
  237. if (mux_mode < 0)
  238. goto err3;
  239. if (!pad->partition)
  240. pad->partition = partition;
  241. if (!pad->mux)
  242. pad->mux = mux;
  243. pad->name = kzalloc(strlen(bpad->name) + 1, GFP_KERNEL);
  244. if (!pad->name) {
  245. int j;
  246. for (j = i - 1; j >= 0; j--)
  247. kfree(hmux->pads[j].name);
  248. goto err3;
  249. }
  250. strcpy(pad->name, bpad->name);
  251. pad->flags = bpad->flags;
  252. pad->enable = bpad->enable;
  253. pad->idle = bpad->idle;
  254. pad->off = bpad->off;
  255. if (pad->flags &
  256. (OMAP_DEVICE_PAD_REMUX | OMAP_DEVICE_PAD_WAKEUP))
  257. nr_pads_dynamic++;
  258. pr_debug("%s: Initialized %s\n", __func__, pad->name);
  259. }
  260. if (!nr_pads_dynamic)
  261. return hmux;
  262. /*
  263. * Add pads that need dynamic muxing into a separate list
  264. */
  265. hmux->nr_pads_dynamic = nr_pads_dynamic;
  266. hmux->pads_dynamic = kzalloc(sizeof(struct omap_device_pad *) *
  267. nr_pads_dynamic, GFP_KERNEL);
  268. if (!hmux->pads_dynamic) {
  269. pr_err("%s: Could not allocate dynamic pads\n", __func__);
  270. return hmux;
  271. }
  272. nr_pads_dynamic = 0;
  273. for (i = 0; i < hmux->nr_pads; i++) {
  274. struct omap_device_pad *pad = &hmux->pads[i];
  275. if (pad->flags &
  276. (OMAP_DEVICE_PAD_REMUX | OMAP_DEVICE_PAD_WAKEUP)) {
  277. pr_debug("%s: pad %s tagged dynamic\n",
  278. __func__, pad->name);
  279. hmux->pads_dynamic[nr_pads_dynamic] = pad;
  280. nr_pads_dynamic++;
  281. }
  282. }
  283. return hmux;
  284. err3:
  285. kfree(hmux->pads);
  286. err2:
  287. kfree(hmux);
  288. err1:
  289. pr_err("%s: Could not allocate device mux entry\n", __func__);
  290. return NULL;
  291. }
  292. /**
  293. * omap_hwmod_mux_scan_wakeups - omap hwmod scan wakeup pads
  294. * @hmux: Pads for a hwmod
  295. * @mpu_irqs: MPU irq array for a hwmod
  296. *
  297. * Scans the wakeup status of pads for a single hwmod. If an irq
  298. * array is defined for this mux, the parser will call the registered
  299. * ISRs for corresponding pads, otherwise the parser will stop at the
  300. * first wakeup active pad and return. Returns true if there is a
  301. * pending and non-served wakeup event for the mux, otherwise false.
  302. */
  303. static bool omap_hwmod_mux_scan_wakeups(struct omap_hwmod_mux_info *hmux,
  304. struct omap_hwmod_irq_info *mpu_irqs)
  305. {
  306. int i, irq;
  307. unsigned int val;
  308. u32 handled_irqs = 0;
  309. for (i = 0; i < hmux->nr_pads_dynamic; i++) {
  310. struct omap_device_pad *pad = hmux->pads_dynamic[i];
  311. if (!(pad->flags & OMAP_DEVICE_PAD_WAKEUP) ||
  312. !(pad->idle & OMAP_WAKEUP_EN))
  313. continue;
  314. val = omap_mux_read(pad->partition, pad->mux->reg_offset);
  315. if (!(val & OMAP_WAKEUP_EVENT))
  316. continue;
  317. if (!hmux->irqs)
  318. return true;
  319. irq = hmux->irqs[i];
  320. /* make sure we only handle each irq once */
  321. if (handled_irqs & 1 << irq)
  322. continue;
  323. handled_irqs |= 1 << irq;
  324. generic_handle_irq(mpu_irqs[irq].irq);
  325. }
  326. return false;
  327. }
  328. /**
  329. * _omap_hwmod_mux_handle_irq - Process wakeup events for a single hwmod
  330. *
  331. * Checks a single hwmod for every wakeup capable pad to see if there is an
  332. * active wakeup event. If this is the case, call the corresponding ISR.
  333. */
  334. static int _omap_hwmod_mux_handle_irq(struct omap_hwmod *oh, void *data)
  335. {
  336. if (!oh->mux || !oh->mux->enabled)
  337. return 0;
  338. if (omap_hwmod_mux_scan_wakeups(oh->mux, oh->mpu_irqs))
  339. generic_handle_irq(oh->mpu_irqs[0].irq);
  340. return 0;
  341. }
  342. /**
  343. * omap_hwmod_mux_handle_irq - Process pad wakeup irqs.
  344. *
  345. * Calls a function for each registered omap_hwmod to check
  346. * pad wakeup statuses.
  347. */
  348. static irqreturn_t omap_hwmod_mux_handle_irq(int irq, void *unused)
  349. {
  350. omap_hwmod_for_each(_omap_hwmod_mux_handle_irq, NULL);
  351. return IRQ_HANDLED;
  352. }
  353. /* Assumes the calling function takes care of locking */
  354. void omap_hwmod_mux(struct omap_hwmod_mux_info *hmux, u8 state)
  355. {
  356. int i;
  357. /* Runtime idling of dynamic pads */
  358. if (state == _HWMOD_STATE_IDLE && hmux->enabled) {
  359. for (i = 0; i < hmux->nr_pads_dynamic; i++) {
  360. struct omap_device_pad *pad = hmux->pads_dynamic[i];
  361. int val = -EINVAL;
  362. val = pad->idle;
  363. omap_mux_write(pad->partition, val,
  364. pad->mux->reg_offset);
  365. }
  366. return;
  367. }
  368. /* Runtime enabling of dynamic pads */
  369. if ((state == _HWMOD_STATE_ENABLED) && hmux->pads_dynamic
  370. && hmux->enabled) {
  371. for (i = 0; i < hmux->nr_pads_dynamic; i++) {
  372. struct omap_device_pad *pad = hmux->pads_dynamic[i];
  373. int val = -EINVAL;
  374. val = pad->enable;
  375. omap_mux_write(pad->partition, val,
  376. pad->mux->reg_offset);
  377. }
  378. return;
  379. }
  380. /* Enabling or disabling of all pads */
  381. for (i = 0; i < hmux->nr_pads; i++) {
  382. struct omap_device_pad *pad = &hmux->pads[i];
  383. int flags, val = -EINVAL;
  384. flags = pad->flags;
  385. switch (state) {
  386. case _HWMOD_STATE_ENABLED:
  387. val = pad->enable;
  388. pr_debug("%s: Enabling %s %x\n", __func__,
  389. pad->name, val);
  390. break;
  391. case _HWMOD_STATE_DISABLED:
  392. /* Use safe mode unless OMAP_DEVICE_PAD_REMUX */
  393. if (flags & OMAP_DEVICE_PAD_REMUX)
  394. val = pad->off;
  395. else
  396. val = OMAP_MUX_MODE7;
  397. pr_debug("%s: Disabling %s %x\n", __func__,
  398. pad->name, val);
  399. break;
  400. default:
  401. /* Nothing to be done */
  402. break;
  403. };
  404. if (val >= 0) {
  405. omap_mux_write(pad->partition, val,
  406. pad->mux->reg_offset);
  407. pad->flags = flags;
  408. }
  409. }
  410. if (state == _HWMOD_STATE_ENABLED)
  411. hmux->enabled = true;
  412. else
  413. hmux->enabled = false;
  414. }
  415. #ifdef CONFIG_DEBUG_FS
  416. #define OMAP_MUX_MAX_NR_FLAGS 10
  417. #define OMAP_MUX_TEST_FLAG(val, mask) \
  418. if (((val) & (mask)) == (mask)) { \
  419. i++; \
  420. flags[i] = #mask; \
  421. }
  422. /* REVISIT: Add checking for non-optimal mux settings */
  423. static inline void omap_mux_decode(struct seq_file *s, u16 val)
  424. {
  425. char *flags[OMAP_MUX_MAX_NR_FLAGS];
  426. char mode[sizeof("OMAP_MUX_MODE") + 1];
  427. int i = -1;
  428. sprintf(mode, "OMAP_MUX_MODE%d", val & 0x7);
  429. i++;
  430. flags[i] = mode;
  431. OMAP_MUX_TEST_FLAG(val, OMAP_PIN_OFF_WAKEUPENABLE);
  432. if (val & OMAP_OFF_EN) {
  433. if (!(val & OMAP_OFFOUT_EN)) {
  434. if (!(val & OMAP_OFF_PULL_UP)) {
  435. OMAP_MUX_TEST_FLAG(val,
  436. OMAP_PIN_OFF_INPUT_PULLDOWN);
  437. } else {
  438. OMAP_MUX_TEST_FLAG(val,
  439. OMAP_PIN_OFF_INPUT_PULLUP);
  440. }
  441. } else {
  442. if (!(val & OMAP_OFFOUT_VAL)) {
  443. OMAP_MUX_TEST_FLAG(val,
  444. OMAP_PIN_OFF_OUTPUT_LOW);
  445. } else {
  446. OMAP_MUX_TEST_FLAG(val,
  447. OMAP_PIN_OFF_OUTPUT_HIGH);
  448. }
  449. }
  450. }
  451. if (val & OMAP_INPUT_EN) {
  452. if (val & OMAP_PULL_ENA) {
  453. if (!(val & OMAP_PULL_UP)) {
  454. OMAP_MUX_TEST_FLAG(val,
  455. OMAP_PIN_INPUT_PULLDOWN);
  456. } else {
  457. OMAP_MUX_TEST_FLAG(val, OMAP_PIN_INPUT_PULLUP);
  458. }
  459. } else {
  460. OMAP_MUX_TEST_FLAG(val, OMAP_PIN_INPUT);
  461. }
  462. } else {
  463. i++;
  464. flags[i] = "OMAP_PIN_OUTPUT";
  465. }
  466. do {
  467. seq_printf(s, "%s", flags[i]);
  468. if (i > 0)
  469. seq_printf(s, " | ");
  470. } while (i-- > 0);
  471. }
  472. #define OMAP_MUX_DEFNAME_LEN 32
  473. static int omap_mux_dbg_board_show(struct seq_file *s, void *unused)
  474. {
  475. struct omap_mux_partition *partition = s->private;
  476. struct omap_mux_entry *e;
  477. u8 omap_gen = omap_rev() >> 28;
  478. list_for_each_entry(e, &partition->muxmodes, node) {
  479. struct omap_mux *m = &e->mux;
  480. char m0_def[OMAP_MUX_DEFNAME_LEN];
  481. char *m0_name = m->muxnames[0];
  482. u16 val;
  483. int i, mode;
  484. if (!m0_name)
  485. continue;
  486. /* REVISIT: Needs to be updated if mode0 names get longer */
  487. for (i = 0; i < OMAP_MUX_DEFNAME_LEN; i++) {
  488. if (m0_name[i] == '\0') {
  489. m0_def[i] = m0_name[i];
  490. break;
  491. }
  492. m0_def[i] = toupper(m0_name[i]);
  493. }
  494. val = omap_mux_read(partition, m->reg_offset);
  495. mode = val & OMAP_MUX_MODE7;
  496. if (mode != 0)
  497. seq_printf(s, "/* %s */\n", m->muxnames[mode]);
  498. /*
  499. * XXX: Might be revisited to support differences across
  500. * same OMAP generation.
  501. */
  502. seq_printf(s, "OMAP%d_MUX(%s, ", omap_gen, m0_def);
  503. omap_mux_decode(s, val);
  504. seq_printf(s, "),\n");
  505. }
  506. return 0;
  507. }
  508. static int omap_mux_dbg_board_open(struct inode *inode, struct file *file)
  509. {
  510. return single_open(file, omap_mux_dbg_board_show, inode->i_private);
  511. }
  512. static const struct file_operations omap_mux_dbg_board_fops = {
  513. .open = omap_mux_dbg_board_open,
  514. .read = seq_read,
  515. .llseek = seq_lseek,
  516. .release = single_release,
  517. };
  518. static struct omap_mux_partition *omap_mux_get_partition(struct omap_mux *mux)
  519. {
  520. struct omap_mux_partition *partition;
  521. list_for_each_entry(partition, &mux_partitions, node) {
  522. struct list_head *muxmodes = &partition->muxmodes;
  523. struct omap_mux_entry *e;
  524. list_for_each_entry(e, muxmodes, node) {
  525. struct omap_mux *m = &e->mux;
  526. if (m == mux)
  527. return partition;
  528. }
  529. }
  530. return NULL;
  531. }
  532. static int omap_mux_dbg_signal_show(struct seq_file *s, void *unused)
  533. {
  534. struct omap_mux *m = s->private;
  535. struct omap_mux_partition *partition;
  536. const char *none = "NA";
  537. u16 val;
  538. int mode;
  539. partition = omap_mux_get_partition(m);
  540. if (!partition)
  541. return 0;
  542. val = omap_mux_read(partition, m->reg_offset);
  543. mode = val & OMAP_MUX_MODE7;
  544. seq_printf(s, "name: %s.%s (0x%08x/0x%03x = 0x%04x), b %s, t %s\n",
  545. m->muxnames[0], m->muxnames[mode],
  546. partition->phys + m->reg_offset, m->reg_offset, val,
  547. m->balls[0] ? m->balls[0] : none,
  548. m->balls[1] ? m->balls[1] : none);
  549. seq_printf(s, "mode: ");
  550. omap_mux_decode(s, val);
  551. seq_printf(s, "\n");
  552. seq_printf(s, "signals: %s | %s | %s | %s | %s | %s | %s | %s\n",
  553. m->muxnames[0] ? m->muxnames[0] : none,
  554. m->muxnames[1] ? m->muxnames[1] : none,
  555. m->muxnames[2] ? m->muxnames[2] : none,
  556. m->muxnames[3] ? m->muxnames[3] : none,
  557. m->muxnames[4] ? m->muxnames[4] : none,
  558. m->muxnames[5] ? m->muxnames[5] : none,
  559. m->muxnames[6] ? m->muxnames[6] : none,
  560. m->muxnames[7] ? m->muxnames[7] : none);
  561. return 0;
  562. }
  563. #define OMAP_MUX_MAX_ARG_CHAR 7
  564. static ssize_t omap_mux_dbg_signal_write(struct file *file,
  565. const char __user *user_buf,
  566. size_t count, loff_t *ppos)
  567. {
  568. char buf[OMAP_MUX_MAX_ARG_CHAR];
  569. struct seq_file *seqf;
  570. struct omap_mux *m;
  571. unsigned long val;
  572. int buf_size, ret;
  573. struct omap_mux_partition *partition;
  574. if (count > OMAP_MUX_MAX_ARG_CHAR)
  575. return -EINVAL;
  576. memset(buf, 0, sizeof(buf));
  577. buf_size = min(count, sizeof(buf) - 1);
  578. if (copy_from_user(buf, user_buf, buf_size))
  579. return -EFAULT;
  580. ret = strict_strtoul(buf, 0x10, &val);
  581. if (ret < 0)
  582. return ret;
  583. if (val > 0xffff)
  584. return -EINVAL;
  585. seqf = file->private_data;
  586. m = seqf->private;
  587. partition = omap_mux_get_partition(m);
  588. if (!partition)
  589. return -ENODEV;
  590. omap_mux_write(partition, (u16)val, m->reg_offset);
  591. *ppos += count;
  592. return count;
  593. }
  594. static int omap_mux_dbg_signal_open(struct inode *inode, struct file *file)
  595. {
  596. return single_open(file, omap_mux_dbg_signal_show, inode->i_private);
  597. }
  598. static const struct file_operations omap_mux_dbg_signal_fops = {
  599. .open = omap_mux_dbg_signal_open,
  600. .read = seq_read,
  601. .write = omap_mux_dbg_signal_write,
  602. .llseek = seq_lseek,
  603. .release = single_release,
  604. };
  605. static struct dentry *mux_dbg_dir;
  606. static void __init omap_mux_dbg_create_entry(
  607. struct omap_mux_partition *partition,
  608. struct dentry *mux_dbg_dir)
  609. {
  610. struct omap_mux_entry *e;
  611. list_for_each_entry(e, &partition->muxmodes, node) {
  612. struct omap_mux *m = &e->mux;
  613. (void)debugfs_create_file(m->muxnames[0], S_IWUSR, mux_dbg_dir,
  614. m, &omap_mux_dbg_signal_fops);
  615. }
  616. }
  617. static void __init omap_mux_dbg_init(void)
  618. {
  619. struct omap_mux_partition *partition;
  620. static struct dentry *mux_dbg_board_dir;
  621. mux_dbg_dir = debugfs_create_dir("omap_mux", NULL);
  622. if (!mux_dbg_dir)
  623. return;
  624. mux_dbg_board_dir = debugfs_create_dir("board", mux_dbg_dir);
  625. if (!mux_dbg_board_dir)
  626. return;
  627. list_for_each_entry(partition, &mux_partitions, node) {
  628. omap_mux_dbg_create_entry(partition, mux_dbg_dir);
  629. (void)debugfs_create_file(partition->name, S_IRUGO,
  630. mux_dbg_board_dir, partition,
  631. &omap_mux_dbg_board_fops);
  632. }
  633. }
  634. #else
  635. static inline void omap_mux_dbg_init(void)
  636. {
  637. }
  638. #endif /* CONFIG_DEBUG_FS */
  639. static void __init omap_mux_free_names(struct omap_mux *m)
  640. {
  641. int i;
  642. for (i = 0; i < OMAP_MUX_NR_MODES; i++)
  643. kfree(m->muxnames[i]);
  644. #ifdef CONFIG_DEBUG_FS
  645. for (i = 0; i < OMAP_MUX_NR_SIDES; i++)
  646. kfree(m->balls[i]);
  647. #endif
  648. }
  649. /* Free all data except for GPIO pins unless CONFIG_DEBUG_FS is set */
  650. static int __init omap_mux_late_init(void)
  651. {
  652. struct omap_mux_partition *partition;
  653. int ret;
  654. list_for_each_entry(partition, &mux_partitions, node) {
  655. struct omap_mux_entry *e, *tmp;
  656. list_for_each_entry_safe(e, tmp, &partition->muxmodes, node) {
  657. struct omap_mux *m = &e->mux;
  658. u16 mode = omap_mux_read(partition, m->reg_offset);
  659. if (OMAP_MODE_GPIO(mode))
  660. continue;
  661. #ifndef CONFIG_DEBUG_FS
  662. mutex_lock(&muxmode_mutex);
  663. list_del(&e->node);
  664. mutex_unlock(&muxmode_mutex);
  665. omap_mux_free_names(m);
  666. kfree(m);
  667. #endif
  668. }
  669. }
  670. ret = request_irq(omap_prcm_event_to_irq("io"),
  671. omap_hwmod_mux_handle_irq, IRQF_SHARED | IRQF_NO_SUSPEND,
  672. "hwmod_io", omap_mux_late_init);
  673. if (ret)
  674. pr_warning("mux: Failed to setup hwmod io irq %d\n", ret);
  675. omap_mux_dbg_init();
  676. return 0;
  677. }
  678. late_initcall(omap_mux_late_init);
  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(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->size = mux_size;
  931. partition->phys = mux_pbase;
  932. partition->base = ioremap(mux_pbase, mux_size);
  933. if (!partition->base) {
  934. pr_err("%s: Could not ioremap mux partition at 0x%08x\n",
  935. __func__, partition->phys);
  936. kfree(partition);
  937. return -ENODEV;
  938. }
  939. INIT_LIST_HEAD(&partition->muxmodes);
  940. list_add_tail(&partition->node, &mux_partitions);
  941. mux_partitions_cnt++;
  942. pr_info("%s: Add partition: #%d: %s, flags: %x\n", __func__,
  943. mux_partitions_cnt, partition->name, partition->flags);
  944. omap_mux_init_package(superset, package_subset, package_balls);
  945. omap_mux_init_list(partition, superset);
  946. omap_mux_init_signals(partition, board_mux);
  947. return 0;
  948. }