imx-audmux.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 <linux/pinctrl/consumer.h>
  29. #include "imx-audmux.h"
  30. #define DRIVER_NAME "imx-audmux"
  31. static struct clk *audmux_clk;
  32. static void __iomem *audmux_base;
  33. #define IMX_AUDMUX_V2_PTCR(x) ((x) * 8)
  34. #define IMX_AUDMUX_V2_PDCR(x) ((x) * 8 + 4)
  35. #ifdef CONFIG_DEBUG_FS
  36. static struct dentry *audmux_debugfs_root;
  37. /* There is an annoying discontinuity in the SSI numbering with regard
  38. * to the Linux number of the devices */
  39. static const char *audmux_port_string(int port)
  40. {
  41. switch (port) {
  42. case MX31_AUDMUX_PORT1_SSI0:
  43. return "imx-ssi.0";
  44. case MX31_AUDMUX_PORT2_SSI1:
  45. return "imx-ssi.1";
  46. case MX31_AUDMUX_PORT3_SSI_PINS_3:
  47. return "SSI3";
  48. case MX31_AUDMUX_PORT4_SSI_PINS_4:
  49. return "SSI4";
  50. case MX31_AUDMUX_PORT5_SSI_PINS_5:
  51. return "SSI5";
  52. case MX31_AUDMUX_PORT6_SSI_PINS_6:
  53. return "SSI6";
  54. default:
  55. return "UNKNOWN";
  56. }
  57. }
  58. static ssize_t audmux_read_file(struct file *file, char __user *user_buf,
  59. size_t count, loff_t *ppos)
  60. {
  61. ssize_t ret;
  62. char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  63. int port = (int)file->private_data;
  64. u32 pdcr, ptcr;
  65. if (!buf)
  66. return -ENOMEM;
  67. if (audmux_clk)
  68. clk_prepare_enable(audmux_clk);
  69. ptcr = readl(audmux_base + IMX_AUDMUX_V2_PTCR(port));
  70. pdcr = readl(audmux_base + IMX_AUDMUX_V2_PDCR(port));
  71. if (audmux_clk)
  72. clk_disable_unprepare(audmux_clk);
  73. ret = snprintf(buf, PAGE_SIZE, "PDCR: %08x\nPTCR: %08x\n",
  74. pdcr, ptcr);
  75. if (ptcr & IMX_AUDMUX_V2_PTCR_TFSDIR)
  76. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  77. "TxFS output from %s, ",
  78. audmux_port_string((ptcr >> 27) & 0x7));
  79. else
  80. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  81. "TxFS input, ");
  82. if (ptcr & IMX_AUDMUX_V2_PTCR_TCLKDIR)
  83. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  84. "TxClk output from %s",
  85. audmux_port_string((ptcr >> 22) & 0x7));
  86. else
  87. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  88. "TxClk input");
  89. ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
  90. if (ptcr & IMX_AUDMUX_V2_PTCR_SYN) {
  91. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  92. "Port is symmetric");
  93. } else {
  94. if (ptcr & IMX_AUDMUX_V2_PTCR_RFSDIR)
  95. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  96. "RxFS output from %s, ",
  97. audmux_port_string((ptcr >> 17) & 0x7));
  98. else
  99. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  100. "RxFS input, ");
  101. if (ptcr & IMX_AUDMUX_V2_PTCR_RCLKDIR)
  102. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  103. "RxClk output from %s",
  104. audmux_port_string((ptcr >> 12) & 0x7));
  105. else
  106. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  107. "RxClk input");
  108. }
  109. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  110. "\nData received from %s\n",
  111. audmux_port_string((pdcr >> 13) & 0x7));
  112. ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
  113. kfree(buf);
  114. return ret;
  115. }
  116. static const struct file_operations audmux_debugfs_fops = {
  117. .open = simple_open,
  118. .read = audmux_read_file,
  119. .llseek = default_llseek,
  120. };
  121. static void __init audmux_debugfs_init(void)
  122. {
  123. int i;
  124. char buf[20];
  125. audmux_debugfs_root = debugfs_create_dir("audmux", NULL);
  126. if (!audmux_debugfs_root) {
  127. pr_warning("Failed to create AUDMUX debugfs root\n");
  128. return;
  129. }
  130. for (i = 0; i < MX31_AUDMUX_PORT7_SSI_PINS_7 + 1; i++) {
  131. snprintf(buf, sizeof(buf), "ssi%d", i);
  132. if (!debugfs_create_file(buf, 0444, audmux_debugfs_root,
  133. (void *)i, &audmux_debugfs_fops))
  134. pr_warning("Failed to create AUDMUX port %d debugfs file\n",
  135. i);
  136. }
  137. }
  138. static void audmux_debugfs_remove(void)
  139. {
  140. debugfs_remove_recursive(audmux_debugfs_root);
  141. }
  142. #else
  143. static inline void audmux_debugfs_init(void)
  144. {
  145. }
  146. static inline void audmux_debugfs_remove(void)
  147. {
  148. }
  149. #endif
  150. static enum imx_audmux_type {
  151. IMX21_AUDMUX,
  152. IMX31_AUDMUX,
  153. } audmux_type;
  154. static struct platform_device_id imx_audmux_ids[] = {
  155. {
  156. .name = "imx21-audmux",
  157. .driver_data = IMX21_AUDMUX,
  158. }, {
  159. .name = "imx31-audmux",
  160. .driver_data = IMX31_AUDMUX,
  161. }, {
  162. /* sentinel */
  163. }
  164. };
  165. MODULE_DEVICE_TABLE(platform, imx_audmux_ids);
  166. static const struct of_device_id imx_audmux_dt_ids[] = {
  167. { .compatible = "fsl,imx21-audmux", .data = &imx_audmux_ids[0], },
  168. { .compatible = "fsl,imx31-audmux", .data = &imx_audmux_ids[1], },
  169. { /* sentinel */ }
  170. };
  171. MODULE_DEVICE_TABLE(of, imx_audmux_dt_ids);
  172. static const uint8_t port_mapping[] = {
  173. 0x0, 0x4, 0x8, 0x10, 0x14, 0x1c,
  174. };
  175. int imx_audmux_v1_configure_port(unsigned int port, unsigned int pcr)
  176. {
  177. if (audmux_type != IMX21_AUDMUX)
  178. return -EINVAL;
  179. if (!audmux_base)
  180. return -ENOSYS;
  181. if (port >= ARRAY_SIZE(port_mapping))
  182. return -EINVAL;
  183. writel(pcr, audmux_base + port_mapping[port]);
  184. return 0;
  185. }
  186. EXPORT_SYMBOL_GPL(imx_audmux_v1_configure_port);
  187. int imx_audmux_v2_configure_port(unsigned int port, unsigned int ptcr,
  188. unsigned int pdcr)
  189. {
  190. if (audmux_type != IMX31_AUDMUX)
  191. return -EINVAL;
  192. if (!audmux_base)
  193. return -ENOSYS;
  194. if (audmux_clk)
  195. clk_prepare_enable(audmux_clk);
  196. writel(ptcr, audmux_base + IMX_AUDMUX_V2_PTCR(port));
  197. writel(pdcr, audmux_base + IMX_AUDMUX_V2_PDCR(port));
  198. if (audmux_clk)
  199. clk_disable_unprepare(audmux_clk);
  200. return 0;
  201. }
  202. EXPORT_SYMBOL_GPL(imx_audmux_v2_configure_port);
  203. static int imx_audmux_probe(struct platform_device *pdev)
  204. {
  205. struct resource *res;
  206. struct pinctrl *pinctrl;
  207. const struct of_device_id *of_id =
  208. of_match_device(imx_audmux_dt_ids, &pdev->dev);
  209. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  210. audmux_base = devm_ioremap_resource(&pdev->dev, res);
  211. if (IS_ERR(audmux_base))
  212. return PTR_ERR(audmux_base);
  213. pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
  214. if (IS_ERR(pinctrl)) {
  215. dev_err(&pdev->dev, "setup pinctrl failed!");
  216. return PTR_ERR(pinctrl);
  217. }
  218. audmux_clk = clk_get(&pdev->dev, "audmux");
  219. if (IS_ERR(audmux_clk)) {
  220. dev_dbg(&pdev->dev, "cannot get clock: %ld\n",
  221. PTR_ERR(audmux_clk));
  222. audmux_clk = NULL;
  223. }
  224. if (of_id)
  225. pdev->id_entry = of_id->data;
  226. audmux_type = pdev->id_entry->driver_data;
  227. if (audmux_type == IMX31_AUDMUX)
  228. audmux_debugfs_init();
  229. return 0;
  230. }
  231. static int imx_audmux_remove(struct platform_device *pdev)
  232. {
  233. if (audmux_type == IMX31_AUDMUX)
  234. audmux_debugfs_remove();
  235. clk_put(audmux_clk);
  236. return 0;
  237. }
  238. static struct platform_driver imx_audmux_driver = {
  239. .probe = imx_audmux_probe,
  240. .remove = imx_audmux_remove,
  241. .id_table = imx_audmux_ids,
  242. .driver = {
  243. .name = DRIVER_NAME,
  244. .owner = THIS_MODULE,
  245. .of_match_table = imx_audmux_dt_ids,
  246. }
  247. };
  248. static int __init imx_audmux_init(void)
  249. {
  250. return platform_driver_register(&imx_audmux_driver);
  251. }
  252. subsys_initcall(imx_audmux_init);
  253. static void __exit imx_audmux_exit(void)
  254. {
  255. platform_driver_unregister(&imx_audmux_driver);
  256. }
  257. module_exit(imx_audmux_exit);
  258. MODULE_DESCRIPTION("Freescale i.MX AUDMUX driver");
  259. MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
  260. MODULE_LICENSE("GPL v2");
  261. MODULE_ALIAS("platform:" DRIVER_NAME);