PageRenderTime 66ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/arch/arm/mach-omap2/mux.c

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