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