mux.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  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 readb_relaxed(partition->base + reg);
  64. else
  65. return readw_relaxed(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. writeb_relaxed(val, partition->base + reg);
  72. else
  73. writew_relaxed(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)
  157. if (strncmp(muxname, m0_entry, mode0_len) ||
  158. (strlen(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. return -ENODEV;
  181. }
  182. int __init omap_mux_get_by_name(const char *muxname,
  183. struct omap_mux_partition **found_partition,
  184. struct omap_mux **found_mux)
  185. {
  186. struct omap_mux_partition *partition;
  187. list_for_each_entry(partition, &mux_partitions, node) {
  188. struct omap_mux *mux = NULL;
  189. int mux_mode = _omap_mux_get_by_name(partition, muxname, &mux);
  190. if (mux_mode < 0)
  191. continue;
  192. *found_partition = partition;
  193. *found_mux = mux;
  194. return mux_mode;
  195. }
  196. pr_err("%s: Could not find signal %s\n", __func__, muxname);
  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. struct seq_file *seqf;
  568. struct omap_mux *m;
  569. u16 val;
  570. int ret;
  571. struct omap_mux_partition *partition;
  572. if (count > OMAP_MUX_MAX_ARG_CHAR)
  573. return -EINVAL;
  574. ret = kstrtou16_from_user(user_buf, count, 0x10, &val);
  575. if (ret < 0)
  576. return ret;
  577. seqf = file->private_data;
  578. m = seqf->private;
  579. partition = omap_mux_get_partition(m);
  580. if (!partition)
  581. return -ENODEV;
  582. omap_mux_write(partition, val, m->reg_offset);
  583. *ppos += count;
  584. return count;
  585. }
  586. static int omap_mux_dbg_signal_open(struct inode *inode, struct file *file)
  587. {
  588. return single_open(file, omap_mux_dbg_signal_show, inode->i_private);
  589. }
  590. static const struct file_operations omap_mux_dbg_signal_fops = {
  591. .open = omap_mux_dbg_signal_open,
  592. .read = seq_read,
  593. .write = omap_mux_dbg_signal_write,
  594. .llseek = seq_lseek,
  595. .release = single_release,
  596. };
  597. static struct dentry *mux_dbg_dir;
  598. static void __init omap_mux_dbg_create_entry(
  599. struct omap_mux_partition *partition,
  600. struct dentry *mux_dbg_dir)
  601. {
  602. struct omap_mux_entry *e;
  603. list_for_each_entry(e, &partition->muxmodes, node) {
  604. struct omap_mux *m = &e->mux;
  605. (void)debugfs_create_file(m->muxnames[0], S_IWUSR | S_IRUGO,
  606. mux_dbg_dir, m,
  607. &omap_mux_dbg_signal_fops);
  608. }
  609. }
  610. static void __init omap_mux_dbg_init(void)
  611. {
  612. struct omap_mux_partition *partition;
  613. static struct dentry *mux_dbg_board_dir;
  614. mux_dbg_dir = debugfs_create_dir("omap_mux", NULL);
  615. if (!mux_dbg_dir)
  616. return;
  617. mux_dbg_board_dir = debugfs_create_dir("board", mux_dbg_dir);
  618. if (!mux_dbg_board_dir)
  619. return;
  620. list_for_each_entry(partition, &mux_partitions, node) {
  621. omap_mux_dbg_create_entry(partition, mux_dbg_dir);
  622. (void)debugfs_create_file(partition->name, S_IRUGO,
  623. mux_dbg_board_dir, partition,
  624. &omap_mux_dbg_board_fops);
  625. }
  626. }
  627. #else
  628. static inline void omap_mux_dbg_init(void)
  629. {
  630. }
  631. #endif /* CONFIG_DEBUG_FS */
  632. static void __init omap_mux_free_names(struct omap_mux *m)
  633. {
  634. int i;
  635. for (i = 0; i < OMAP_MUX_NR_MODES; i++)
  636. kfree(m->muxnames[i]);
  637. #ifdef CONFIG_DEBUG_FS
  638. for (i = 0; i < OMAP_MUX_NR_SIDES; i++)
  639. kfree(m->balls[i]);
  640. #endif
  641. }
  642. /* Free all data except for GPIO pins unless CONFIG_DEBUG_FS is set */
  643. int __init omap_mux_late_init(void)
  644. {
  645. struct omap_mux_partition *partition;
  646. int ret;
  647. list_for_each_entry(partition, &mux_partitions, node) {
  648. struct omap_mux_entry *e, *tmp;
  649. list_for_each_entry_safe(e, tmp, &partition->muxmodes, node) {
  650. struct omap_mux *m = &e->mux;
  651. u16 mode = omap_mux_read(partition, m->reg_offset);
  652. if (OMAP_MODE_GPIO(partition, mode))
  653. continue;
  654. #ifndef CONFIG_DEBUG_FS
  655. mutex_lock(&muxmode_mutex);
  656. list_del(&e->node);
  657. mutex_unlock(&muxmode_mutex);
  658. omap_mux_free_names(m);
  659. kfree(m);
  660. #endif
  661. }
  662. }
  663. omap_mux_dbg_init();
  664. /* see pinctrl-single-omap for the wake-up interrupt handling */
  665. if (of_have_populated_dt())
  666. return 0;
  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_warn("mux: Failed to setup hwmod io irq %d\n", ret);
  672. return 0;
  673. }
  674. static void __init omap_mux_package_fixup(struct omap_mux *p,
  675. struct omap_mux *superset)
  676. {
  677. while (p->reg_offset != OMAP_MUX_TERMINATOR) {
  678. struct omap_mux *s = superset;
  679. int found = 0;
  680. while (s->reg_offset != OMAP_MUX_TERMINATOR) {
  681. if (s->reg_offset == p->reg_offset) {
  682. *s = *p;
  683. found++;
  684. break;
  685. }
  686. s++;
  687. }
  688. if (!found)
  689. pr_err("%s: Unknown entry offset 0x%x\n", __func__,
  690. p->reg_offset);
  691. p++;
  692. }
  693. }
  694. #ifdef CONFIG_DEBUG_FS
  695. static void __init omap_mux_package_init_balls(struct omap_ball *b,
  696. struct omap_mux *superset)
  697. {
  698. while (b->reg_offset != OMAP_MUX_TERMINATOR) {
  699. struct omap_mux *s = superset;
  700. int found = 0;
  701. while (s->reg_offset != OMAP_MUX_TERMINATOR) {
  702. if (s->reg_offset == b->reg_offset) {
  703. s->balls[0] = b->balls[0];
  704. s->balls[1] = b->balls[1];
  705. found++;
  706. break;
  707. }
  708. s++;
  709. }
  710. if (!found)
  711. pr_err("%s: Unknown ball offset 0x%x\n", __func__,
  712. b->reg_offset);
  713. b++;
  714. }
  715. }
  716. #else /* CONFIG_DEBUG_FS */
  717. static inline void omap_mux_package_init_balls(struct omap_ball *b,
  718. struct omap_mux *superset)
  719. {
  720. }
  721. #endif /* CONFIG_DEBUG_FS */
  722. static int __init omap_mux_setup(char *options)
  723. {
  724. if (!options)
  725. return 0;
  726. omap_mux_options = options;
  727. return 1;
  728. }
  729. __setup("omap_mux=", omap_mux_setup);
  730. /*
  731. * Note that the omap_mux=some.signal1=0x1234,some.signal2=0x1234
  732. * cmdline options only override the bootloader values.
  733. * During development, please enable CONFIG_DEBUG_FS, and use the
  734. * signal specific entries under debugfs.
  735. */
  736. static void __init omap_mux_set_cmdline_signals(void)
  737. {
  738. char *options, *next_opt, *token;
  739. if (!omap_mux_options)
  740. return;
  741. options = kstrdup(omap_mux_options, GFP_KERNEL);
  742. if (!options)
  743. return;
  744. next_opt = options;
  745. while ((token = strsep(&next_opt, ",")) != NULL) {
  746. char *keyval, *name;
  747. u16 val;
  748. keyval = token;
  749. name = strsep(&keyval, "=");
  750. if (name) {
  751. int res;
  752. res = kstrtou16(keyval, 0x10, &val);
  753. if (res < 0)
  754. continue;
  755. omap_mux_init_signal(name, (u16)val);
  756. }
  757. }
  758. kfree(options);
  759. }
  760. static int __init omap_mux_copy_names(struct omap_mux *src,
  761. struct omap_mux *dst)
  762. {
  763. int i;
  764. for (i = 0; i < OMAP_MUX_NR_MODES; i++) {
  765. if (src->muxnames[i]) {
  766. dst->muxnames[i] = kstrdup(src->muxnames[i],
  767. GFP_KERNEL);
  768. if (!dst->muxnames[i])
  769. goto free;
  770. }
  771. }
  772. #ifdef CONFIG_DEBUG_FS
  773. for (i = 0; i < OMAP_MUX_NR_SIDES; i++) {
  774. if (src->balls[i]) {
  775. dst->balls[i] = kstrdup(src->balls[i], GFP_KERNEL);
  776. if (!dst->balls[i])
  777. goto free;
  778. }
  779. }
  780. #endif
  781. return 0;
  782. free:
  783. omap_mux_free_names(dst);
  784. return -ENOMEM;
  785. }
  786. #endif /* CONFIG_OMAP_MUX */
  787. static struct omap_mux *omap_mux_get_by_gpio(
  788. struct omap_mux_partition *partition,
  789. int gpio)
  790. {
  791. struct omap_mux_entry *e;
  792. struct omap_mux *ret = NULL;
  793. list_for_each_entry(e, &partition->muxmodes, node) {
  794. struct omap_mux *m = &e->mux;
  795. if (m->gpio == gpio) {
  796. ret = m;
  797. break;
  798. }
  799. }
  800. return ret;
  801. }
  802. /* Needed for dynamic muxing of GPIO pins for off-idle */
  803. u16 omap_mux_get_gpio(int gpio)
  804. {
  805. struct omap_mux_partition *partition;
  806. struct omap_mux *m = NULL;
  807. list_for_each_entry(partition, &mux_partitions, node) {
  808. m = omap_mux_get_by_gpio(partition, gpio);
  809. if (m)
  810. return omap_mux_read(partition, m->reg_offset);
  811. }
  812. if (!m || m->reg_offset == OMAP_MUX_TERMINATOR)
  813. pr_err("%s: Could not get gpio%i\n", __func__, gpio);
  814. return OMAP_MUX_TERMINATOR;
  815. }
  816. /* Needed for dynamic muxing of GPIO pins for off-idle */
  817. void omap_mux_set_gpio(u16 val, int gpio)
  818. {
  819. struct omap_mux_partition *partition;
  820. struct omap_mux *m = NULL;
  821. list_for_each_entry(partition, &mux_partitions, node) {
  822. m = omap_mux_get_by_gpio(partition, gpio);
  823. if (m) {
  824. omap_mux_write(partition, val, m->reg_offset);
  825. return;
  826. }
  827. }
  828. if (!m || m->reg_offset == OMAP_MUX_TERMINATOR)
  829. pr_err("%s: Could not set gpio%i\n", __func__, gpio);
  830. }
  831. static struct omap_mux * __init omap_mux_list_add(
  832. struct omap_mux_partition *partition,
  833. struct omap_mux *src)
  834. {
  835. struct omap_mux_entry *entry;
  836. struct omap_mux *m;
  837. entry = kzalloc(sizeof(struct omap_mux_entry), GFP_KERNEL);
  838. if (!entry)
  839. return NULL;
  840. m = &entry->mux;
  841. entry->mux = *src;
  842. #ifdef CONFIG_OMAP_MUX
  843. if (omap_mux_copy_names(src, m)) {
  844. kfree(entry);
  845. return NULL;
  846. }
  847. #endif
  848. mutex_lock(&muxmode_mutex);
  849. list_add_tail(&entry->node, &partition->muxmodes);
  850. mutex_unlock(&muxmode_mutex);
  851. return m;
  852. }
  853. /*
  854. * Note if CONFIG_OMAP_MUX is not selected, we will only initialize
  855. * the GPIO to mux offset mapping that is needed for dynamic muxing
  856. * of GPIO pins for off-idle.
  857. */
  858. static void __init omap_mux_init_list(struct omap_mux_partition *partition,
  859. struct omap_mux *superset)
  860. {
  861. while (superset->reg_offset != OMAP_MUX_TERMINATOR) {
  862. struct omap_mux *entry;
  863. #ifdef CONFIG_OMAP_MUX
  864. if (!superset->muxnames[0]) {
  865. superset++;
  866. continue;
  867. }
  868. #else
  869. /* Skip pins that are not muxed as GPIO by bootloader */
  870. if (!OMAP_MODE_GPIO(partition, omap_mux_read(partition,
  871. superset->reg_offset))) {
  872. superset++;
  873. continue;
  874. }
  875. #endif
  876. entry = omap_mux_list_add(partition, superset);
  877. if (!entry) {
  878. pr_err("%s: Could not add entry\n", __func__);
  879. return;
  880. }
  881. superset++;
  882. }
  883. }
  884. #ifdef CONFIG_OMAP_MUX
  885. static void omap_mux_init_package(struct omap_mux *superset,
  886. struct omap_mux *package_subset,
  887. struct omap_ball *package_balls)
  888. {
  889. if (package_subset)
  890. omap_mux_package_fixup(package_subset, superset);
  891. if (package_balls)
  892. omap_mux_package_init_balls(package_balls, superset);
  893. }
  894. static void __init omap_mux_init_signals(struct omap_mux_partition *partition,
  895. struct omap_board_mux *board_mux)
  896. {
  897. omap_mux_set_cmdline_signals();
  898. omap_mux_write_array(partition, board_mux);
  899. }
  900. #else
  901. static void omap_mux_init_package(struct omap_mux *superset,
  902. struct omap_mux *package_subset,
  903. struct omap_ball *package_balls)
  904. {
  905. }
  906. static void __init omap_mux_init_signals(struct omap_mux_partition *partition,
  907. struct omap_board_mux *board_mux)
  908. {
  909. }
  910. #endif
  911. static u32 mux_partitions_cnt;
  912. int __init omap_mux_init(const char *name, u32 flags,
  913. u32 mux_pbase, u32 mux_size,
  914. struct omap_mux *superset,
  915. struct omap_mux *package_subset,
  916. struct omap_board_mux *board_mux,
  917. struct omap_ball *package_balls)
  918. {
  919. struct omap_mux_partition *partition;
  920. partition = kzalloc(sizeof(struct omap_mux_partition), GFP_KERNEL);
  921. if (!partition)
  922. return -ENOMEM;
  923. partition->name = name;
  924. partition->flags = flags;
  925. partition->gpio = flags & OMAP_MUX_MODE7;
  926. partition->size = mux_size;
  927. partition->phys = mux_pbase;
  928. partition->base = ioremap(mux_pbase, mux_size);
  929. if (!partition->base) {
  930. pr_err("%s: Could not ioremap mux partition at 0x%08x\n",
  931. __func__, partition->phys);
  932. kfree(partition);
  933. return -ENODEV;
  934. }
  935. INIT_LIST_HEAD(&partition->muxmodes);
  936. list_add_tail(&partition->node, &mux_partitions);
  937. mux_partitions_cnt++;
  938. pr_info("%s: Add partition: #%d: %s, flags: %x\n", __func__,
  939. mux_partitions_cnt, partition->name, partition->flags);
  940. omap_mux_init_package(superset, package_subset, package_balls);
  941. omap_mux_init_list(partition, superset);
  942. omap_mux_init_signals(partition, board_mux);
  943. return 0;
  944. }