mux.c 26 KB

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