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 "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. pr_err("%s: Could not find signal %s\n", __func__, muxname);
  179. return -ENODEV;
  180. }
  181. int __init omap_mux_get_by_name(const char *muxname,
  182. struct omap_mux_partition **found_partition,
  183. struct omap_mux **found_mux)
  184. {
  185. struct omap_mux_partition *partition;
  186. list_for_each_entry(partition, &mux_partitions, node) {
  187. struct omap_mux *mux = NULL;
  188. int mux_mode = _omap_mux_get_by_name(partition, muxname, &mux);
  189. if (mux_mode < 0)
  190. continue;
  191. *found_partition = partition;
  192. *found_mux = mux;
  193. return mux_mode;
  194. }
  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, mux_dbg_dir,
  611. m, &omap_mux_dbg_signal_fops);
  612. }
  613. }
  614. static void __init omap_mux_dbg_init(void)
  615. {
  616. struct omap_mux_partition *partition;
  617. static struct dentry *mux_dbg_board_dir;
  618. mux_dbg_dir = debugfs_create_dir("omap_mux", NULL);
  619. if (!mux_dbg_dir)
  620. return;
  621. mux_dbg_board_dir = debugfs_create_dir("board", mux_dbg_dir);
  622. if (!mux_dbg_board_dir)
  623. return;
  624. list_for_each_entry(partition, &mux_partitions, node) {
  625. omap_mux_dbg_create_entry(partition, mux_dbg_dir);
  626. (void)debugfs_create_file(partition->name, S_IRUGO,
  627. mux_dbg_board_dir, partition,
  628. &omap_mux_dbg_board_fops);
  629. }
  630. }
  631. #else
  632. static inline void omap_mux_dbg_init(void)
  633. {
  634. }
  635. #endif /* CONFIG_DEBUG_FS */
  636. static void __init omap_mux_free_names(struct omap_mux *m)
  637. {
  638. int i;
  639. for (i = 0; i < OMAP_MUX_NR_MODES; i++)
  640. kfree(m->muxnames[i]);
  641. #ifdef CONFIG_DEBUG_FS
  642. for (i = 0; i < OMAP_MUX_NR_SIDES; i++)
  643. kfree(m->balls[i]);
  644. #endif
  645. }
  646. /* Free all data except for GPIO pins unless CONFIG_DEBUG_FS is set */
  647. int __init omap_mux_late_init(void)
  648. {
  649. struct omap_mux_partition *partition;
  650. int ret;
  651. list_for_each_entry(partition, &mux_partitions, node) {
  652. struct omap_mux_entry *e, *tmp;
  653. list_for_each_entry_safe(e, tmp, &partition->muxmodes, node) {
  654. struct omap_mux *m = &e->mux;
  655. u16 mode = omap_mux_read(partition, m->reg_offset);
  656. if (OMAP_MODE_GPIO(partition, mode))
  657. continue;
  658. #ifndef CONFIG_DEBUG_FS
  659. mutex_lock(&muxmode_mutex);
  660. list_del(&e->node);
  661. mutex_unlock(&muxmode_mutex);
  662. omap_mux_free_names(m);
  663. kfree(m);
  664. #endif
  665. }
  666. }
  667. ret = request_irq(omap_prcm_event_to_irq("io"),
  668. omap_hwmod_mux_handle_irq, IRQF_SHARED | IRQF_NO_SUSPEND,
  669. "hwmod_io", omap_mux_late_init);
  670. if (ret)
  671. pr_warning("mux: Failed to setup hwmod io irq %d\n", ret);
  672. omap_mux_dbg_init();
  673. return 0;
  674. }
  675. static void __init omap_mux_package_fixup(struct omap_mux *p,
  676. struct omap_mux *superset)
  677. {
  678. while (p->reg_offset != OMAP_MUX_TERMINATOR) {
  679. struct omap_mux *s = superset;
  680. int found = 0;
  681. while (s->reg_offset != OMAP_MUX_TERMINATOR) {
  682. if (s->reg_offset == p->reg_offset) {
  683. *s = *p;
  684. found++;
  685. break;
  686. }
  687. s++;
  688. }
  689. if (!found)
  690. pr_err("%s: Unknown entry offset 0x%x\n", __func__,
  691. p->reg_offset);
  692. p++;
  693. }
  694. }
  695. #ifdef CONFIG_DEBUG_FS
  696. static void __init omap_mux_package_init_balls(struct omap_ball *b,
  697. struct omap_mux *superset)
  698. {
  699. while (b->reg_offset != OMAP_MUX_TERMINATOR) {
  700. struct omap_mux *s = superset;
  701. int found = 0;
  702. while (s->reg_offset != OMAP_MUX_TERMINATOR) {
  703. if (s->reg_offset == b->reg_offset) {
  704. s->balls[0] = b->balls[0];
  705. s->balls[1] = b->balls[1];
  706. found++;
  707. break;
  708. }
  709. s++;
  710. }
  711. if (!found)
  712. pr_err("%s: Unknown ball offset 0x%x\n", __func__,
  713. b->reg_offset);
  714. b++;
  715. }
  716. }
  717. #else /* CONFIG_DEBUG_FS */
  718. static inline void omap_mux_package_init_balls(struct omap_ball *b,
  719. struct omap_mux *superset)
  720. {
  721. }
  722. #endif /* CONFIG_DEBUG_FS */
  723. static int __init omap_mux_setup(char *options)
  724. {
  725. if (!options)
  726. return 0;
  727. omap_mux_options = options;
  728. return 1;
  729. }
  730. __setup("omap_mux=", omap_mux_setup);
  731. /*
  732. * Note that the omap_mux=some.signal1=0x1234,some.signal2=0x1234
  733. * cmdline options only override the bootloader values.
  734. * During development, please enable CONFIG_DEBUG_FS, and use the
  735. * signal specific entries under debugfs.
  736. */
  737. static void __init omap_mux_set_cmdline_signals(void)
  738. {
  739. char *options, *next_opt, *token;
  740. if (!omap_mux_options)
  741. return;
  742. options = kstrdup(omap_mux_options, GFP_KERNEL);
  743. if (!options)
  744. return;
  745. next_opt = options;
  746. while ((token = strsep(&next_opt, ",")) != NULL) {
  747. char *keyval, *name;
  748. unsigned long val;
  749. keyval = token;
  750. name = strsep(&keyval, "=");
  751. if (name) {
  752. int res;
  753. res = strict_strtoul(keyval, 0x10, &val);
  754. if (res < 0)
  755. continue;
  756. omap_mux_init_signal(name, (u16)val);
  757. }
  758. }
  759. kfree(options);
  760. }
  761. static int __init omap_mux_copy_names(struct omap_mux *src,
  762. struct omap_mux *dst)
  763. {
  764. int i;
  765. for (i = 0; i < OMAP_MUX_NR_MODES; i++) {
  766. if (src->muxnames[i]) {
  767. dst->muxnames[i] = kstrdup(src->muxnames[i],
  768. GFP_KERNEL);
  769. if (!dst->muxnames[i])
  770. goto free;
  771. }
  772. }
  773. #ifdef CONFIG_DEBUG_FS
  774. for (i = 0; i < OMAP_MUX_NR_SIDES; i++) {
  775. if (src->balls[i]) {
  776. dst->balls[i] = kstrdup(src->balls[i], GFP_KERNEL);
  777. if (!dst->balls[i])
  778. goto free;
  779. }
  780. }
  781. #endif
  782. return 0;
  783. free:
  784. omap_mux_free_names(dst);
  785. return -ENOMEM;
  786. }
  787. #endif /* CONFIG_OMAP_MUX */
  788. static struct omap_mux *omap_mux_get_by_gpio(
  789. struct omap_mux_partition *partition,
  790. int gpio)
  791. {
  792. struct omap_mux_entry *e;
  793. struct omap_mux *ret = NULL;
  794. list_for_each_entry(e, &partition->muxmodes, node) {
  795. struct omap_mux *m = &e->mux;
  796. if (m->gpio == gpio) {
  797. ret = m;
  798. break;
  799. }
  800. }
  801. return ret;
  802. }
  803. /* Needed for dynamic muxing of GPIO pins for off-idle */
  804. u16 omap_mux_get_gpio(int gpio)
  805. {
  806. struct omap_mux_partition *partition;
  807. struct omap_mux *m = NULL;
  808. list_for_each_entry(partition, &mux_partitions, node) {
  809. m = omap_mux_get_by_gpio(partition, gpio);
  810. if (m)
  811. return omap_mux_read(partition, m->reg_offset);
  812. }
  813. if (!m || m->reg_offset == OMAP_MUX_TERMINATOR)
  814. pr_err("%s: Could not get gpio%i\n", __func__, gpio);
  815. return OMAP_MUX_TERMINATOR;
  816. }
  817. /* Needed for dynamic muxing of GPIO pins for off-idle */
  818. void omap_mux_set_gpio(u16 val, int gpio)
  819. {
  820. struct omap_mux_partition *partition;
  821. struct omap_mux *m = NULL;
  822. list_for_each_entry(partition, &mux_partitions, node) {
  823. m = omap_mux_get_by_gpio(partition, gpio);
  824. if (m) {
  825. omap_mux_write(partition, val, m->reg_offset);
  826. return;
  827. }
  828. }
  829. if (!m || m->reg_offset == OMAP_MUX_TERMINATOR)
  830. pr_err("%s: Could not set gpio%i\n", __func__, gpio);
  831. }
  832. static struct omap_mux * __init omap_mux_list_add(
  833. struct omap_mux_partition *partition,
  834. struct omap_mux *src)
  835. {
  836. struct omap_mux_entry *entry;
  837. struct omap_mux *m;
  838. entry = kzalloc(sizeof(struct omap_mux_entry), GFP_KERNEL);
  839. if (!entry)
  840. return NULL;
  841. m = &entry->mux;
  842. entry->mux = *src;
  843. #ifdef CONFIG_OMAP_MUX
  844. if (omap_mux_copy_names(src, m)) {
  845. kfree(entry);
  846. return NULL;
  847. }
  848. #endif
  849. mutex_lock(&muxmode_mutex);
  850. list_add_tail(&entry->node, &partition->muxmodes);
  851. mutex_unlock(&muxmode_mutex);
  852. return m;
  853. }
  854. /*
  855. * Note if CONFIG_OMAP_MUX is not selected, we will only initialize
  856. * the GPIO to mux offset mapping that is needed for dynamic muxing
  857. * of GPIO pins for off-idle.
  858. */
  859. static void __init omap_mux_init_list(struct omap_mux_partition *partition,
  860. struct omap_mux *superset)
  861. {
  862. while (superset->reg_offset != OMAP_MUX_TERMINATOR) {
  863. struct omap_mux *entry;
  864. #ifdef CONFIG_OMAP_MUX
  865. if (!superset->muxnames || !superset->muxnames[0]) {
  866. superset++;
  867. continue;
  868. }
  869. #else
  870. /* Skip pins that are not muxed as GPIO by bootloader */
  871. if (!OMAP_MODE_GPIO(partition, omap_mux_read(partition,
  872. superset->reg_offset))) {
  873. superset++;
  874. continue;
  875. }
  876. #endif
  877. entry = omap_mux_list_add(partition, superset);
  878. if (!entry) {
  879. pr_err("%s: Could not add entry\n", __func__);
  880. return;
  881. }
  882. superset++;
  883. }
  884. }
  885. #ifdef CONFIG_OMAP_MUX
  886. static void omap_mux_init_package(struct omap_mux *superset,
  887. struct omap_mux *package_subset,
  888. struct omap_ball *package_balls)
  889. {
  890. if (package_subset)
  891. omap_mux_package_fixup(package_subset, superset);
  892. if (package_balls)
  893. omap_mux_package_init_balls(package_balls, superset);
  894. }
  895. static void __init omap_mux_init_signals(struct omap_mux_partition *partition,
  896. struct omap_board_mux *board_mux)
  897. {
  898. omap_mux_set_cmdline_signals();
  899. omap_mux_write_array(partition, board_mux);
  900. }
  901. #else
  902. static void omap_mux_init_package(struct omap_mux *superset,
  903. struct omap_mux *package_subset,
  904. struct omap_ball *package_balls)
  905. {
  906. }
  907. static void __init omap_mux_init_signals(struct omap_mux_partition *partition,
  908. struct omap_board_mux *board_mux)
  909. {
  910. }
  911. #endif
  912. static u32 mux_partitions_cnt;
  913. int __init omap_mux_init(const char *name, u32 flags,
  914. u32 mux_pbase, u32 mux_size,
  915. struct omap_mux *superset,
  916. struct omap_mux *package_subset,
  917. struct omap_board_mux *board_mux,
  918. struct omap_ball *package_balls)
  919. {
  920. struct omap_mux_partition *partition;
  921. partition = kzalloc(sizeof(struct omap_mux_partition), GFP_KERNEL);
  922. if (!partition)
  923. return -ENOMEM;
  924. partition->name = name;
  925. partition->flags = flags;
  926. partition->gpio = flags & OMAP_MUX_MODE7;
  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. }