mux.c 24 KB

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