mux.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  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 <asm/system.h>
  36. #include <plat/omap_hwmod.h>
  37. #include "control.h"
  38. #include "mux.h"
  39. #define OMAP_MUX_BASE_OFFSET 0x30 /* Offset from CTRL_BASE */
  40. #define OMAP_MUX_BASE_SZ 0x5ca
  41. struct omap_mux_entry {
  42. struct omap_mux mux;
  43. struct list_head node;
  44. };
  45. static LIST_HEAD(mux_partitions);
  46. static DEFINE_MUTEX(muxmode_mutex);
  47. struct omap_mux_partition *omap_mux_get(const char *name)
  48. {
  49. struct omap_mux_partition *partition;
  50. list_for_each_entry(partition, &mux_partitions, node) {
  51. if (!strcmp(name, partition->name))
  52. return partition;
  53. }
  54. return NULL;
  55. }
  56. u16 omap_mux_read(struct omap_mux_partition *partition, u16 reg)
  57. {
  58. if (partition->flags & OMAP_MUX_REG_8BIT)
  59. return __raw_readb(partition->base + reg);
  60. else
  61. return __raw_readw(partition->base + reg);
  62. }
  63. void omap_mux_write(struct omap_mux_partition *partition, u16 val,
  64. u16 reg)
  65. {
  66. if (partition->flags & OMAP_MUX_REG_8BIT)
  67. __raw_writeb(val, partition->base + reg);
  68. else
  69. __raw_writew(val, partition->base + reg);
  70. }
  71. void omap_mux_write_array(struct omap_mux_partition *partition,
  72. struct omap_board_mux *board_mux)
  73. {
  74. if (!board_mux)
  75. return;
  76. while (board_mux->reg_offset != OMAP_MUX_TERMINATOR) {
  77. omap_mux_write(partition, board_mux->value,
  78. board_mux->reg_offset);
  79. board_mux++;
  80. }
  81. }
  82. #ifdef CONFIG_OMAP_MUX
  83. static char *omap_mux_options;
  84. static int __init _omap_mux_init_gpio(struct omap_mux_partition *partition,
  85. int gpio, int val)
  86. {
  87. struct omap_mux_entry *e;
  88. struct omap_mux *gpio_mux = NULL;
  89. u16 old_mode;
  90. u16 mux_mode;
  91. int found = 0;
  92. struct list_head *muxmodes = &partition->muxmodes;
  93. if (!gpio)
  94. return -EINVAL;
  95. list_for_each_entry(e, muxmodes, node) {
  96. struct omap_mux *m = &e->mux;
  97. if (gpio == m->gpio) {
  98. gpio_mux = m;
  99. found++;
  100. }
  101. }
  102. if (found == 0) {
  103. pr_err("%s: Could not set gpio%i\n", __func__, gpio);
  104. return -ENODEV;
  105. }
  106. if (found > 1) {
  107. pr_info("%s: Multiple gpio paths (%d) for gpio%i\n", __func__,
  108. found, gpio);
  109. return -EINVAL;
  110. }
  111. old_mode = omap_mux_read(partition, gpio_mux->reg_offset);
  112. mux_mode = val & ~(OMAP_MUX_NR_MODES - 1);
  113. if (partition->flags & OMAP_MUX_GPIO_IN_MODE3)
  114. mux_mode |= OMAP_MUX_MODE3;
  115. else
  116. mux_mode |= OMAP_MUX_MODE4;
  117. pr_debug("%s: Setting signal %s.gpio%i 0x%04x -> 0x%04x\n", __func__,
  118. gpio_mux->muxnames[0], gpio, old_mode, mux_mode);
  119. omap_mux_write(partition, mux_mode, gpio_mux->reg_offset);
  120. return 0;
  121. }
  122. int __init omap_mux_init_gpio(int gpio, int val)
  123. {
  124. struct omap_mux_partition *partition;
  125. int ret;
  126. list_for_each_entry(partition, &mux_partitions, node) {
  127. ret = _omap_mux_init_gpio(partition, gpio, val);
  128. if (!ret)
  129. return ret;
  130. }
  131. return -ENODEV;
  132. }
  133. static int __init _omap_mux_get_by_name(struct omap_mux_partition *partition,
  134. const char *muxname,
  135. struct omap_mux **found_mux)
  136. {
  137. struct omap_mux *mux = NULL;
  138. struct omap_mux_entry *e;
  139. const char *mode_name;
  140. int found = 0, found_mode = 0, mode0_len = 0;
  141. struct list_head *muxmodes = &partition->muxmodes;
  142. mode_name = strchr(muxname, '.');
  143. if (mode_name) {
  144. mode0_len = strlen(muxname) - strlen(mode_name);
  145. mode_name++;
  146. } else {
  147. mode_name = muxname;
  148. }
  149. list_for_each_entry(e, muxmodes, node) {
  150. char *m0_entry;
  151. int i;
  152. mux = &e->mux;
  153. m0_entry = mux->muxnames[0];
  154. /* First check for full name in mode0.muxmode format */
  155. if (mode0_len && strncmp(muxname, m0_entry, mode0_len))
  156. continue;
  157. /* Then check for muxmode only */
  158. for (i = 0; i < OMAP_MUX_NR_MODES; i++) {
  159. char *mode_cur = mux->muxnames[i];
  160. if (!mode_cur)
  161. continue;
  162. if (!strcmp(mode_name, mode_cur)) {
  163. *found_mux = mux;
  164. found++;
  165. found_mode = i;
  166. }
  167. }
  168. }
  169. if (found == 1) {
  170. return found_mode;
  171. }
  172. if (found > 1) {
  173. pr_err("%s: Multiple signal paths (%i) for %s\n", __func__,
  174. found, muxname);
  175. return -EINVAL;
  176. }
  177. pr_err("%s: Could not find signal %s\n", __func__, muxname);
  178. return -ENODEV;
  179. }
  180. static int __init
  181. omap_mux_get_by_name(const char *muxname,
  182. struct omap_mux_partition **found_partition,
  183. struct omap_mux **found_mux)
  184. {
  185. struct omap_mux_partition *partition;
  186. list_for_each_entry(partition, &mux_partitions, node) {
  187. struct omap_mux *mux = NULL;
  188. int mux_mode = _omap_mux_get_by_name(partition, muxname, &mux);
  189. if (mux_mode < 0)
  190. continue;
  191. *found_partition = partition;
  192. *found_mux = mux;
  193. return mux_mode;
  194. }
  195. return -ENODEV;
  196. }
  197. int __init omap_mux_init_signal(const char *muxname, int val)
  198. {
  199. struct omap_mux_partition *partition = NULL;
  200. struct omap_mux *mux = NULL;
  201. u16 old_mode;
  202. int mux_mode;
  203. mux_mode = omap_mux_get_by_name(muxname, &partition, &mux);
  204. if (mux_mode < 0)
  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. /* Assumes the calling function takes care of locking */
  290. void omap_hwmod_mux(struct omap_hwmod_mux_info *hmux, u8 state)
  291. {
  292. int i;
  293. /* Runtime idling of dynamic pads */
  294. if (state == _HWMOD_STATE_IDLE && hmux->enabled) {
  295. for (i = 0; i < hmux->nr_pads_dynamic; i++) {
  296. struct omap_device_pad *pad = hmux->pads_dynamic[i];
  297. int val = -EINVAL;
  298. val = pad->idle;
  299. omap_mux_write(pad->partition, val,
  300. pad->mux->reg_offset);
  301. }
  302. return;
  303. }
  304. /* Runtime enabling of dynamic pads */
  305. if ((state == _HWMOD_STATE_ENABLED) && hmux->pads_dynamic
  306. && hmux->enabled) {
  307. for (i = 0; i < hmux->nr_pads_dynamic; i++) {
  308. struct omap_device_pad *pad = hmux->pads_dynamic[i];
  309. int val = -EINVAL;
  310. val = pad->enable;
  311. omap_mux_write(pad->partition, val,
  312. pad->mux->reg_offset);
  313. }
  314. return;
  315. }
  316. /* Enabling or disabling of all pads */
  317. for (i = 0; i < hmux->nr_pads; i++) {
  318. struct omap_device_pad *pad = &hmux->pads[i];
  319. int flags, val = -EINVAL;
  320. flags = pad->flags;
  321. switch (state) {
  322. case _HWMOD_STATE_ENABLED:
  323. val = pad->enable;
  324. pr_debug("%s: Enabling %s %x\n", __func__,
  325. pad->name, val);
  326. break;
  327. case _HWMOD_STATE_DISABLED:
  328. /* Use safe mode unless OMAP_DEVICE_PAD_REMUX */
  329. if (flags & OMAP_DEVICE_PAD_REMUX)
  330. val = pad->off;
  331. else
  332. val = OMAP_MUX_MODE7;
  333. pr_debug("%s: Disabling %s %x\n", __func__,
  334. pad->name, val);
  335. break;
  336. default:
  337. /* Nothing to be done */
  338. break;
  339. };
  340. if (val >= 0) {
  341. omap_mux_write(pad->partition, val,
  342. pad->mux->reg_offset);
  343. pad->flags = flags;
  344. }
  345. }
  346. if (state == _HWMOD_STATE_ENABLED)
  347. hmux->enabled = true;
  348. else
  349. hmux->enabled = false;
  350. }
  351. #ifdef CONFIG_DEBUG_FS
  352. #define OMAP_MUX_MAX_NR_FLAGS 10
  353. #define OMAP_MUX_TEST_FLAG(val, mask) \
  354. if (((val) & (mask)) == (mask)) { \
  355. i++; \
  356. flags[i] = #mask; \
  357. }
  358. /* REVISIT: Add checking for non-optimal mux settings */
  359. static inline void omap_mux_decode(struct seq_file *s, u16 val)
  360. {
  361. char *flags[OMAP_MUX_MAX_NR_FLAGS];
  362. char mode[sizeof("OMAP_MUX_MODE") + 1];
  363. int i = -1;
  364. sprintf(mode, "OMAP_MUX_MODE%d", val & 0x7);
  365. i++;
  366. flags[i] = mode;
  367. OMAP_MUX_TEST_FLAG(val, OMAP_PIN_OFF_WAKEUPENABLE);
  368. if (val & OMAP_OFF_EN) {
  369. if (!(val & OMAP_OFFOUT_EN)) {
  370. if (!(val & OMAP_OFF_PULL_UP)) {
  371. OMAP_MUX_TEST_FLAG(val,
  372. OMAP_PIN_OFF_INPUT_PULLDOWN);
  373. } else {
  374. OMAP_MUX_TEST_FLAG(val,
  375. OMAP_PIN_OFF_INPUT_PULLUP);
  376. }
  377. } else {
  378. if (!(val & OMAP_OFFOUT_VAL)) {
  379. OMAP_MUX_TEST_FLAG(val,
  380. OMAP_PIN_OFF_OUTPUT_LOW);
  381. } else {
  382. OMAP_MUX_TEST_FLAG(val,
  383. OMAP_PIN_OFF_OUTPUT_HIGH);
  384. }
  385. }
  386. }
  387. if (val & OMAP_INPUT_EN) {
  388. if (val & OMAP_PULL_ENA) {
  389. if (!(val & OMAP_PULL_UP)) {
  390. OMAP_MUX_TEST_FLAG(val,
  391. OMAP_PIN_INPUT_PULLDOWN);
  392. } else {
  393. OMAP_MUX_TEST_FLAG(val, OMAP_PIN_INPUT_PULLUP);
  394. }
  395. } else {
  396. OMAP_MUX_TEST_FLAG(val, OMAP_PIN_INPUT);
  397. }
  398. } else {
  399. i++;
  400. flags[i] = "OMAP_PIN_OUTPUT";
  401. }
  402. do {
  403. seq_printf(s, "%s", flags[i]);
  404. if (i > 0)
  405. seq_printf(s, " | ");
  406. } while (i-- > 0);
  407. }
  408. #define OMAP_MUX_DEFNAME_LEN 32
  409. static int omap_mux_dbg_board_show(struct seq_file *s, void *unused)
  410. {
  411. struct omap_mux_partition *partition = s->private;
  412. struct omap_mux_entry *e;
  413. u8 omap_gen = omap_rev() >> 28;
  414. list_for_each_entry(e, &partition->muxmodes, node) {
  415. struct omap_mux *m = &e->mux;
  416. char m0_def[OMAP_MUX_DEFNAME_LEN];
  417. char *m0_name = m->muxnames[0];
  418. u16 val;
  419. int i, mode;
  420. if (!m0_name)
  421. continue;
  422. /* REVISIT: Needs to be updated if mode0 names get longer */
  423. for (i = 0; i < OMAP_MUX_DEFNAME_LEN; i++) {
  424. if (m0_name[i] == '\0') {
  425. m0_def[i] = m0_name[i];
  426. break;
  427. }
  428. m0_def[i] = toupper(m0_name[i]);
  429. }
  430. val = omap_mux_read(partition, m->reg_offset);
  431. mode = val & OMAP_MUX_MODE7;
  432. if (mode != 0)
  433. seq_printf(s, "/* %s */\n", m->muxnames[mode]);
  434. /*
  435. * XXX: Might be revisited to support differences across
  436. * same OMAP generation.
  437. */
  438. seq_printf(s, "OMAP%d_MUX(%s, ", omap_gen, m0_def);
  439. omap_mux_decode(s, val);
  440. seq_printf(s, "),\n");
  441. }
  442. return 0;
  443. }
  444. static int omap_mux_dbg_board_open(struct inode *inode, struct file *file)
  445. {
  446. return single_open(file, omap_mux_dbg_board_show, inode->i_private);
  447. }
  448. static const struct file_operations omap_mux_dbg_board_fops = {
  449. .open = omap_mux_dbg_board_open,
  450. .read = seq_read,
  451. .llseek = seq_lseek,
  452. .release = single_release,
  453. };
  454. static struct omap_mux_partition *omap_mux_get_partition(struct omap_mux *mux)
  455. {
  456. struct omap_mux_partition *partition;
  457. list_for_each_entry(partition, &mux_partitions, node) {
  458. struct list_head *muxmodes = &partition->muxmodes;
  459. struct omap_mux_entry *e;
  460. list_for_each_entry(e, muxmodes, node) {
  461. struct omap_mux *m = &e->mux;
  462. if (m == mux)
  463. return partition;
  464. }
  465. }
  466. return NULL;
  467. }
  468. static int omap_mux_dbg_signal_show(struct seq_file *s, void *unused)
  469. {
  470. struct omap_mux *m = s->private;
  471. struct omap_mux_partition *partition;
  472. const char *none = "NA";
  473. u16 val;
  474. int mode;
  475. partition = omap_mux_get_partition(m);
  476. if (!partition)
  477. return 0;
  478. val = omap_mux_read(partition, m->reg_offset);
  479. mode = val & OMAP_MUX_MODE7;
  480. seq_printf(s, "name: %s.%s (0x%08x/0x%03x = 0x%04x), b %s, t %s\n",
  481. m->muxnames[0], m->muxnames[mode],
  482. partition->phys + m->reg_offset, m->reg_offset, val,
  483. m->balls[0] ? m->balls[0] : none,
  484. m->balls[1] ? m->balls[1] : none);
  485. seq_printf(s, "mode: ");
  486. omap_mux_decode(s, val);
  487. seq_printf(s, "\n");
  488. seq_printf(s, "signals: %s | %s | %s | %s | %s | %s | %s | %s\n",
  489. m->muxnames[0] ? m->muxnames[0] : none,
  490. m->muxnames[1] ? m->muxnames[1] : none,
  491. m->muxnames[2] ? m->muxnames[2] : none,
  492. m->muxnames[3] ? m->muxnames[3] : none,
  493. m->muxnames[4] ? m->muxnames[4] : none,
  494. m->muxnames[5] ? m->muxnames[5] : none,
  495. m->muxnames[6] ? m->muxnames[6] : none,
  496. m->muxnames[7] ? m->muxnames[7] : none);
  497. return 0;
  498. }
  499. #define OMAP_MUX_MAX_ARG_CHAR 7
  500. static ssize_t omap_mux_dbg_signal_write(struct file *file,
  501. const char __user *user_buf,
  502. size_t count, loff_t *ppos)
  503. {
  504. char buf[OMAP_MUX_MAX_ARG_CHAR];
  505. struct seq_file *seqf;
  506. struct omap_mux *m;
  507. unsigned long val;
  508. int buf_size, ret;
  509. struct omap_mux_partition *partition;
  510. if (count > OMAP_MUX_MAX_ARG_CHAR)
  511. return -EINVAL;
  512. memset(buf, 0, sizeof(buf));
  513. buf_size = min(count, sizeof(buf) - 1);
  514. if (copy_from_user(buf, user_buf, buf_size))
  515. return -EFAULT;
  516. ret = strict_strtoul(buf, 0x10, &val);
  517. if (ret < 0)
  518. return ret;
  519. if (val > 0xffff)
  520. return -EINVAL;
  521. seqf = file->private_data;
  522. m = seqf->private;
  523. partition = omap_mux_get_partition(m);
  524. if (!partition)
  525. return -ENODEV;
  526. omap_mux_write(partition, (u16)val, m->reg_offset);
  527. *ppos += count;
  528. return count;
  529. }
  530. static int omap_mux_dbg_signal_open(struct inode *inode, struct file *file)
  531. {
  532. return single_open(file, omap_mux_dbg_signal_show, inode->i_private);
  533. }
  534. static const struct file_operations omap_mux_dbg_signal_fops = {
  535. .open = omap_mux_dbg_signal_open,
  536. .read = seq_read,
  537. .write = omap_mux_dbg_signal_write,
  538. .llseek = seq_lseek,
  539. .release = single_release,
  540. };
  541. static struct dentry *mux_dbg_dir;
  542. static void __init omap_mux_dbg_create_entry(
  543. struct omap_mux_partition *partition,
  544. struct dentry *mux_dbg_dir)
  545. {
  546. struct omap_mux_entry *e;
  547. list_for_each_entry(e, &partition->muxmodes, node) {
  548. struct omap_mux *m = &e->mux;
  549. (void)debugfs_create_file(m->muxnames[0], S_IWUSR, mux_dbg_dir,
  550. m, &omap_mux_dbg_signal_fops);
  551. }
  552. }
  553. static void __init omap_mux_dbg_init(void)
  554. {
  555. struct omap_mux_partition *partition;
  556. static struct dentry *mux_dbg_board_dir;
  557. mux_dbg_dir = debugfs_create_dir("omap_mux", NULL);
  558. if (!mux_dbg_dir)
  559. return;
  560. mux_dbg_board_dir = debugfs_create_dir("board", mux_dbg_dir);
  561. if (!mux_dbg_board_dir)
  562. return;
  563. list_for_each_entry(partition, &mux_partitions, node) {
  564. omap_mux_dbg_create_entry(partition, mux_dbg_dir);
  565. (void)debugfs_create_file(partition->name, S_IRUGO,
  566. mux_dbg_board_dir, partition,
  567. &omap_mux_dbg_board_fops);
  568. }
  569. }
  570. #else
  571. static inline void omap_mux_dbg_init(void)
  572. {
  573. }
  574. #endif /* CONFIG_DEBUG_FS */
  575. static void __init omap_mux_free_names(struct omap_mux *m)
  576. {
  577. int i;
  578. for (i = 0; i < OMAP_MUX_NR_MODES; i++)
  579. kfree(m->muxnames[i]);
  580. #ifdef CONFIG_DEBUG_FS
  581. for (i = 0; i < OMAP_MUX_NR_SIDES; i++)
  582. kfree(m->balls[i]);
  583. #endif
  584. }
  585. /* Free all data except for GPIO pins unless CONFIG_DEBUG_FS is set */
  586. static int __init omap_mux_late_init(void)
  587. {
  588. struct omap_mux_partition *partition;
  589. list_for_each_entry(partition, &mux_partitions, node) {
  590. struct omap_mux_entry *e, *tmp;
  591. list_for_each_entry_safe(e, tmp, &partition->muxmodes, node) {
  592. struct omap_mux *m = &e->mux;
  593. u16 mode = omap_mux_read(partition, m->reg_offset);
  594. if (OMAP_MODE_GPIO(mode))
  595. continue;
  596. #ifndef CONFIG_DEBUG_FS
  597. mutex_lock(&muxmode_mutex);
  598. list_del(&e->node);
  599. mutex_unlock(&muxmode_mutex);
  600. omap_mux_free_names(m);
  601. kfree(m);
  602. #endif
  603. }
  604. }
  605. omap_mux_dbg_init();
  606. return 0;
  607. }
  608. late_initcall(omap_mux_late_init);
  609. static void __init omap_mux_package_fixup(struct omap_mux *p,
  610. struct omap_mux *superset)
  611. {
  612. while (p->reg_offset != OMAP_MUX_TERMINATOR) {
  613. struct omap_mux *s = superset;
  614. int found = 0;
  615. while (s->reg_offset != OMAP_MUX_TERMINATOR) {
  616. if (s->reg_offset == p->reg_offset) {
  617. *s = *p;
  618. found++;
  619. break;
  620. }
  621. s++;
  622. }
  623. if (!found)
  624. pr_err("%s: Unknown entry offset 0x%x\n", __func__,
  625. p->reg_offset);
  626. p++;
  627. }
  628. }
  629. #ifdef CONFIG_DEBUG_FS
  630. static void __init omap_mux_package_init_balls(struct omap_ball *b,
  631. struct omap_mux *superset)
  632. {
  633. while (b->reg_offset != OMAP_MUX_TERMINATOR) {
  634. struct omap_mux *s = superset;
  635. int found = 0;
  636. while (s->reg_offset != OMAP_MUX_TERMINATOR) {
  637. if (s->reg_offset == b->reg_offset) {
  638. s->balls[0] = b->balls[0];
  639. s->balls[1] = b->balls[1];
  640. found++;
  641. break;
  642. }
  643. s++;
  644. }
  645. if (!found)
  646. pr_err("%s: Unknown ball offset 0x%x\n", __func__,
  647. b->reg_offset);
  648. b++;
  649. }
  650. }
  651. #else /* CONFIG_DEBUG_FS */
  652. static inline void omap_mux_package_init_balls(struct omap_ball *b,
  653. struct omap_mux *superset)
  654. {
  655. }
  656. #endif /* CONFIG_DEBUG_FS */
  657. static int __init omap_mux_setup(char *options)
  658. {
  659. if (!options)
  660. return 0;
  661. omap_mux_options = options;
  662. return 1;
  663. }
  664. __setup("omap_mux=", omap_mux_setup);
  665. /*
  666. * Note that the omap_mux=some.signal1=0x1234,some.signal2=0x1234
  667. * cmdline options only override the bootloader values.
  668. * During development, please enable CONFIG_DEBUG_FS, and use the
  669. * signal specific entries under debugfs.
  670. */
  671. static void __init omap_mux_set_cmdline_signals(void)
  672. {
  673. char *options, *next_opt, *token;
  674. if (!omap_mux_options)
  675. return;
  676. options = kstrdup(omap_mux_options, GFP_KERNEL);
  677. if (!options)
  678. return;
  679. next_opt = options;
  680. while ((token = strsep(&next_opt, ",")) != NULL) {
  681. char *keyval, *name;
  682. unsigned long val;
  683. keyval = token;
  684. name = strsep(&keyval, "=");
  685. if (name) {
  686. int res;
  687. res = strict_strtoul(keyval, 0x10, &val);
  688. if (res < 0)
  689. continue;
  690. omap_mux_init_signal(name, (u16)val);
  691. }
  692. }
  693. kfree(options);
  694. }
  695. static int __init omap_mux_copy_names(struct omap_mux *src,
  696. struct omap_mux *dst)
  697. {
  698. int i;
  699. for (i = 0; i < OMAP_MUX_NR_MODES; i++) {
  700. if (src->muxnames[i]) {
  701. dst->muxnames[i] = kstrdup(src->muxnames[i],
  702. GFP_KERNEL);
  703. if (!dst->muxnames[i])
  704. goto free;
  705. }
  706. }
  707. #ifdef CONFIG_DEBUG_FS
  708. for (i = 0; i < OMAP_MUX_NR_SIDES; i++) {
  709. if (src->balls[i]) {
  710. dst->balls[i] = kstrdup(src->balls[i], GFP_KERNEL);
  711. if (!dst->balls[i])
  712. goto free;
  713. }
  714. }
  715. #endif
  716. return 0;
  717. free:
  718. omap_mux_free_names(dst);
  719. return -ENOMEM;
  720. }
  721. #endif /* CONFIG_OMAP_MUX */
  722. static struct omap_mux *omap_mux_get_by_gpio(
  723. struct omap_mux_partition *partition,
  724. int gpio)
  725. {
  726. struct omap_mux_entry *e;
  727. struct omap_mux *ret = NULL;
  728. list_for_each_entry(e, &partition->muxmodes, node) {
  729. struct omap_mux *m = &e->mux;
  730. if (m->gpio == gpio) {
  731. ret = m;
  732. break;
  733. }
  734. }
  735. return ret;
  736. }
  737. /* Needed for dynamic muxing of GPIO pins for off-idle */
  738. u16 omap_mux_get_gpio(int gpio)
  739. {
  740. struct omap_mux_partition *partition;
  741. struct omap_mux *m = NULL;
  742. list_for_each_entry(partition, &mux_partitions, node) {
  743. m = omap_mux_get_by_gpio(partition, gpio);
  744. if (m)
  745. return omap_mux_read(partition, m->reg_offset);
  746. }
  747. if (!m || m->reg_offset == OMAP_MUX_TERMINATOR)
  748. pr_err("%s: Could not get gpio%i\n", __func__, gpio);
  749. return OMAP_MUX_TERMINATOR;
  750. }
  751. /* Needed for dynamic muxing of GPIO pins for off-idle */
  752. void omap_mux_set_gpio(u16 val, int gpio)
  753. {
  754. struct omap_mux_partition *partition;
  755. struct omap_mux *m = NULL;
  756. list_for_each_entry(partition, &mux_partitions, node) {
  757. m = omap_mux_get_by_gpio(partition, gpio);
  758. if (m) {
  759. omap_mux_write(partition, val, m->reg_offset);
  760. return;
  761. }
  762. }
  763. if (!m || m->reg_offset == OMAP_MUX_TERMINATOR)
  764. pr_err("%s: Could not set gpio%i\n", __func__, gpio);
  765. }
  766. static struct omap_mux * __init omap_mux_list_add(
  767. struct omap_mux_partition *partition,
  768. struct omap_mux *src)
  769. {
  770. struct omap_mux_entry *entry;
  771. struct omap_mux *m;
  772. entry = kzalloc(sizeof(struct omap_mux_entry), GFP_KERNEL);
  773. if (!entry)
  774. return NULL;
  775. m = &entry->mux;
  776. entry->mux = *src;
  777. #ifdef CONFIG_OMAP_MUX
  778. if (omap_mux_copy_names(src, m)) {
  779. kfree(entry);
  780. return NULL;
  781. }
  782. #endif
  783. mutex_lock(&muxmode_mutex);
  784. list_add_tail(&entry->node, &partition->muxmodes);
  785. mutex_unlock(&muxmode_mutex);
  786. return m;
  787. }
  788. /*
  789. * Note if CONFIG_OMAP_MUX is not selected, we will only initialize
  790. * the GPIO to mux offset mapping that is needed for dynamic muxing
  791. * of GPIO pins for off-idle.
  792. */
  793. static void __init omap_mux_init_list(struct omap_mux_partition *partition,
  794. struct omap_mux *superset)
  795. {
  796. while (superset->reg_offset != OMAP_MUX_TERMINATOR) {
  797. struct omap_mux *entry;
  798. #ifdef CONFIG_OMAP_MUX
  799. if (!superset->muxnames || !superset->muxnames[0]) {
  800. superset++;
  801. continue;
  802. }
  803. #else
  804. /* Skip pins that are not muxed as GPIO by bootloader */
  805. if (!OMAP_MODE_GPIO(omap_mux_read(partition,
  806. superset->reg_offset))) {
  807. superset++;
  808. continue;
  809. }
  810. #endif
  811. entry = omap_mux_list_add(partition, superset);
  812. if (!entry) {
  813. pr_err("%s: Could not add entry\n", __func__);
  814. return;
  815. }
  816. superset++;
  817. }
  818. }
  819. #ifdef CONFIG_OMAP_MUX
  820. static void omap_mux_init_package(struct omap_mux *superset,
  821. struct omap_mux *package_subset,
  822. struct omap_ball *package_balls)
  823. {
  824. if (package_subset)
  825. omap_mux_package_fixup(package_subset, superset);
  826. if (package_balls)
  827. omap_mux_package_init_balls(package_balls, superset);
  828. }
  829. static void omap_mux_init_signals(struct omap_mux_partition *partition,
  830. struct omap_board_mux *board_mux)
  831. {
  832. omap_mux_set_cmdline_signals();
  833. omap_mux_write_array(partition, board_mux);
  834. }
  835. #else
  836. static void omap_mux_init_package(struct omap_mux *superset,
  837. struct omap_mux *package_subset,
  838. struct omap_ball *package_balls)
  839. {
  840. }
  841. static void omap_mux_init_signals(struct omap_mux_partition *partition,
  842. struct omap_board_mux *board_mux)
  843. {
  844. }
  845. #endif
  846. static u32 mux_partitions_cnt;
  847. int __init omap_mux_init(const char *name, u32 flags,
  848. u32 mux_pbase, u32 mux_size,
  849. struct omap_mux *superset,
  850. struct omap_mux *package_subset,
  851. struct omap_board_mux *board_mux,
  852. struct omap_ball *package_balls)
  853. {
  854. struct omap_mux_partition *partition;
  855. partition = kzalloc(sizeof(struct omap_mux_partition), GFP_KERNEL);
  856. if (!partition)
  857. return -ENOMEM;
  858. partition->name = name;
  859. partition->flags = flags;
  860. partition->size = mux_size;
  861. partition->phys = mux_pbase;
  862. partition->base = ioremap(mux_pbase, mux_size);
  863. if (!partition->base) {
  864. pr_err("%s: Could not ioremap mux partition at 0x%08x\n",
  865. __func__, partition->phys);
  866. kfree(partition);
  867. return -ENODEV;
  868. }
  869. INIT_LIST_HEAD(&partition->muxmodes);
  870. list_add_tail(&partition->node, &mux_partitions);
  871. mux_partitions_cnt++;
  872. pr_info("%s: Add partition: #%d: %s, flags: %x\n", __func__,
  873. mux_partitions_cnt, partition->name, partition->flags);
  874. omap_mux_init_package(superset, package_subset, package_balls);
  875. omap_mux_init_list(partition, superset);
  876. omap_mux_init_signals(partition, board_mux);
  877. return 0;
  878. }