imx-audmux.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. * Copyright 2012 Freescale Semiconductor, Inc.
  3. * Copyright 2012 Linaro Ltd.
  4. * Copyright 2009 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de>
  5. *
  6. * Initial development of this code was funded by
  7. * Phytec Messtechnik GmbH, http://www.phytec.de
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. */
  19. #include <linux/clk.h>
  20. #include <linux/debugfs.h>
  21. #include <linux/err.h>
  22. #include <linux/io.h>
  23. #include <linux/module.h>
  24. #include <linux/of.h>
  25. #include <linux/of_device.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/slab.h>
  28. #include "imx-audmux.h"
  29. #define DRIVER_NAME "imx-audmux"
  30. static struct clk *audmux_clk;
  31. static void __iomem *audmux_base;
  32. #define IMX_AUDMUX_V2_PTCR(x) ((x) * 8)
  33. #define IMX_AUDMUX_V2_PDCR(x) ((x) * 8 + 4)
  34. #ifdef CONFIG_DEBUG_FS
  35. static struct dentry *audmux_debugfs_root;
  36. static int audmux_open_file(struct inode *inode, struct file *file)
  37. {
  38. file->private_data = inode->i_private;
  39. return 0;
  40. }
  41. /* There is an annoying discontinuity in the SSI numbering with regard
  42. * to the Linux number of the devices */
  43. static const char *audmux_port_string(int port)
  44. {
  45. switch (port) {
  46. case MX31_AUDMUX_PORT1_SSI0:
  47. return "imx-ssi.0";
  48. case MX31_AUDMUX_PORT2_SSI1:
  49. return "imx-ssi.1";
  50. case MX31_AUDMUX_PORT3_SSI_PINS_3:
  51. return "SSI3";
  52. case MX31_AUDMUX_PORT4_SSI_PINS_4:
  53. return "SSI4";
  54. case MX31_AUDMUX_PORT5_SSI_PINS_5:
  55. return "SSI5";
  56. case MX31_AUDMUX_PORT6_SSI_PINS_6:
  57. return "SSI6";
  58. default:
  59. return "UNKNOWN";
  60. }
  61. }
  62. static ssize_t audmux_read_file(struct file *file, char __user *user_buf,
  63. size_t count, loff_t *ppos)
  64. {
  65. ssize_t ret;
  66. char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  67. int port = (int)file->private_data;
  68. u32 pdcr, ptcr;
  69. if (!buf)
  70. return -ENOMEM;
  71. if (audmux_clk)
  72. clk_enable(audmux_clk);
  73. ptcr = readl(audmux_base + IMX_AUDMUX_V2_PTCR(port));
  74. pdcr = readl(audmux_base + IMX_AUDMUX_V2_PDCR(port));
  75. if (audmux_clk)
  76. clk_disable(audmux_clk);
  77. ret = snprintf(buf, PAGE_SIZE, "PDCR: %08x\nPTCR: %08x\n",
  78. pdcr, ptcr);
  79. if (ptcr & IMX_AUDMUX_V2_PTCR_TFSDIR)
  80. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  81. "TxFS output from %s, ",
  82. audmux_port_string((ptcr >> 27) & 0x7));
  83. else
  84. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  85. "TxFS input, ");
  86. if (ptcr & IMX_AUDMUX_V2_PTCR_TCLKDIR)
  87. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  88. "TxClk output from %s",
  89. audmux_port_string((ptcr >> 22) & 0x7));
  90. else
  91. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  92. "TxClk input");
  93. ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
  94. if (ptcr & IMX_AUDMUX_V2_PTCR_SYN) {
  95. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  96. "Port is symmetric");
  97. } else {
  98. if (ptcr & IMX_AUDMUX_V2_PTCR_RFSDIR)
  99. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  100. "RxFS output from %s, ",
  101. audmux_port_string((ptcr >> 17) & 0x7));
  102. else
  103. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  104. "RxFS input, ");
  105. if (ptcr & IMX_AUDMUX_V2_PTCR_RCLKDIR)
  106. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  107. "RxClk output from %s",
  108. audmux_port_string((ptcr >> 12) & 0x7));
  109. else
  110. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  111. "RxClk input");
  112. }
  113. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  114. "\nData received from %s\n",
  115. audmux_port_string((pdcr >> 13) & 0x7));
  116. ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
  117. kfree(buf);
  118. return ret;
  119. }
  120. static const struct file_operations audmux_debugfs_fops = {
  121. .open = audmux_open_file,
  122. .read = audmux_read_file,
  123. .llseek = default_llseek,
  124. };
  125. static void __init audmux_debugfs_init(void)
  126. {
  127. int i;
  128. char buf[20];
  129. audmux_debugfs_root = debugfs_create_dir("audmux", NULL);
  130. if (!audmux_debugfs_root) {
  131. pr_warning("Failed to create AUDMUX debugfs root\n");
  132. return;
  133. }
  134. for (i = 1; i < 8; i++) {
  135. snprintf(buf, sizeof(buf), "ssi%d", i);
  136. if (!debugfs_create_file(buf, 0444, audmux_debugfs_root,
  137. (void *)i, &audmux_debugfs_fops))
  138. pr_warning("Failed to create AUDMUX port %d debugfs file\n",
  139. i);
  140. }
  141. }
  142. static void __devexit audmux_debugfs_remove(void)
  143. {
  144. debugfs_remove_recursive(audmux_debugfs_root);
  145. }
  146. #else
  147. static inline void audmux_debugfs_init(void)
  148. {
  149. }
  150. static inline void audmux_debugfs_remove(void)
  151. {
  152. }
  153. #endif
  154. enum imx_audmux_type {
  155. IMX21_AUDMUX,
  156. IMX31_AUDMUX,
  157. } audmux_type;
  158. static struct platform_device_id imx_audmux_ids[] = {
  159. {
  160. .name = "imx21-audmux",
  161. .driver_data = IMX21_AUDMUX,
  162. }, {
  163. .name = "imx31-audmux",
  164. .driver_data = IMX31_AUDMUX,
  165. }, {
  166. /* sentinel */
  167. }
  168. };
  169. MODULE_DEVICE_TABLE(platform, imx_audmux_ids);
  170. static const struct of_device_id imx_audmux_dt_ids[] = {
  171. { .compatible = "fsl,imx21-audmux", .data = &imx_audmux_ids[0], },
  172. { .compatible = "fsl,imx31-audmux", .data = &imx_audmux_ids[1], },
  173. { /* sentinel */ }
  174. };
  175. MODULE_DEVICE_TABLE(of, imx_audmux_dt_ids);
  176. static const uint8_t port_mapping[] = {
  177. 0x0, 0x4, 0x8, 0x10, 0x14, 0x1c,
  178. };
  179. int imx_audmux_v1_configure_port(unsigned int port, unsigned int pcr)
  180. {
  181. if (audmux_type != IMX21_AUDMUX)
  182. return -EINVAL;
  183. if (!audmux_base)
  184. return -ENOSYS;
  185. if (port >= ARRAY_SIZE(port_mapping))
  186. return -EINVAL;
  187. writel(pcr, audmux_base + port_mapping[port]);
  188. return 0;
  189. }
  190. EXPORT_SYMBOL_GPL(imx_audmux_v1_configure_port);
  191. int imx_audmux_v2_configure_port(unsigned int port, unsigned int ptcr,
  192. unsigned int pdcr)
  193. {
  194. if (audmux_type != IMX31_AUDMUX)
  195. return -EINVAL;
  196. if (!audmux_base)
  197. return -ENOSYS;
  198. if (audmux_clk)
  199. clk_enable(audmux_clk);
  200. writel(ptcr, audmux_base + IMX_AUDMUX_V2_PTCR(port));
  201. writel(pdcr, audmux_base + IMX_AUDMUX_V2_PDCR(port));
  202. if (audmux_clk)
  203. clk_disable(audmux_clk);
  204. return 0;
  205. }
  206. EXPORT_SYMBOL_GPL(imx_audmux_v2_configure_port);
  207. static int __devinit imx_audmux_probe(struct platform_device *pdev)
  208. {
  209. struct resource *res;
  210. const struct of_device_id *of_id =
  211. of_match_device(imx_audmux_dt_ids, &pdev->dev);
  212. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  213. audmux_base = devm_request_and_ioremap(&pdev->dev, res);
  214. if (!audmux_base)
  215. return -EADDRNOTAVAIL;
  216. audmux_clk = clk_get(&pdev->dev, "audmux");
  217. if (IS_ERR(audmux_clk)) {
  218. dev_dbg(&pdev->dev, "cannot get clock: %ld\n",
  219. PTR_ERR(audmux_clk));
  220. audmux_clk = NULL;
  221. }
  222. if (of_id)
  223. pdev->id_entry = of_id->data;
  224. audmux_type = pdev->id_entry->driver_data;
  225. if (audmux_type == IMX31_AUDMUX)
  226. audmux_debugfs_init();
  227. return 0;
  228. }
  229. static int __devexit imx_audmux_remove(struct platform_device *pdev)
  230. {
  231. if (audmux_type == IMX31_AUDMUX)
  232. audmux_debugfs_remove();
  233. clk_put(audmux_clk);
  234. return 0;
  235. }
  236. static struct platform_driver imx_audmux_driver = {
  237. .probe = imx_audmux_probe,
  238. .remove = __devexit_p(imx_audmux_remove),
  239. .id_table = imx_audmux_ids,
  240. .driver = {
  241. .name = DRIVER_NAME,
  242. .owner = THIS_MODULE,
  243. .of_match_table = imx_audmux_dt_ids,
  244. }
  245. };
  246. static int __init imx_audmux_init(void)
  247. {
  248. return platform_driver_register(&imx_audmux_driver);
  249. }
  250. subsys_initcall(imx_audmux_init);
  251. static void __exit imx_audmux_exit(void)
  252. {
  253. platform_driver_unregister(&imx_audmux_driver);
  254. }
  255. module_exit(imx_audmux_exit);
  256. MODULE_DESCRIPTION("Freescale i.MX AUDMUX driver");
  257. MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
  258. MODULE_LICENSE("GPL v2");
  259. MODULE_ALIAS("platform:" DRIVER_NAME);