imx-audmux.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. /* There is an annoying discontinuity in the SSI numbering with regard
  37. * to the Linux number of the devices */
  38. static const char *audmux_port_string(int port)
  39. {
  40. switch (port) {
  41. case MX31_AUDMUX_PORT1_SSI0:
  42. return "imx-ssi.0";
  43. case MX31_AUDMUX_PORT2_SSI1:
  44. return "imx-ssi.1";
  45. case MX31_AUDMUX_PORT3_SSI_PINS_3:
  46. return "SSI3";
  47. case MX31_AUDMUX_PORT4_SSI_PINS_4:
  48. return "SSI4";
  49. case MX31_AUDMUX_PORT5_SSI_PINS_5:
  50. return "SSI5";
  51. case MX31_AUDMUX_PORT6_SSI_PINS_6:
  52. return "SSI6";
  53. default:
  54. return "UNKNOWN";
  55. }
  56. }
  57. static ssize_t audmux_read_file(struct file *file, char __user *user_buf,
  58. size_t count, loff_t *ppos)
  59. {
  60. ssize_t ret;
  61. char *buf;
  62. uintptr_t port = (uintptr_t)file->private_data;
  63. u32 pdcr, ptcr;
  64. if (audmux_clk) {
  65. ret = clk_prepare_enable(audmux_clk);
  66. if (ret)
  67. return ret;
  68. }
  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. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  74. if (!buf)
  75. return -ENOMEM;
  76. ret = snprintf(buf, PAGE_SIZE, "PDCR: %08x\nPTCR: %08x\n",
  77. pdcr, ptcr);
  78. if (ptcr & IMX_AUDMUX_V2_PTCR_TFSDIR)
  79. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  80. "TxFS output from %s, ",
  81. audmux_port_string((ptcr >> 27) & 0x7));
  82. else
  83. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  84. "TxFS input, ");
  85. if (ptcr & IMX_AUDMUX_V2_PTCR_TCLKDIR)
  86. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  87. "TxClk output from %s",
  88. audmux_port_string((ptcr >> 22) & 0x7));
  89. else
  90. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  91. "TxClk input");
  92. ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
  93. if (ptcr & IMX_AUDMUX_V2_PTCR_SYN) {
  94. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  95. "Port is symmetric");
  96. } else {
  97. if (ptcr & IMX_AUDMUX_V2_PTCR_RFSDIR)
  98. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  99. "RxFS output from %s, ",
  100. audmux_port_string((ptcr >> 17) & 0x7));
  101. else
  102. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  103. "RxFS input, ");
  104. if (ptcr & IMX_AUDMUX_V2_PTCR_RCLKDIR)
  105. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  106. "RxClk output from %s",
  107. audmux_port_string((ptcr >> 12) & 0x7));
  108. else
  109. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  110. "RxClk input");
  111. }
  112. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  113. "\nData received from %s\n",
  114. audmux_port_string((pdcr >> 13) & 0x7));
  115. ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
  116. kfree(buf);
  117. return ret;
  118. }
  119. static const struct file_operations audmux_debugfs_fops = {
  120. .open = simple_open,
  121. .read = audmux_read_file,
  122. .llseek = default_llseek,
  123. };
  124. static void audmux_debugfs_init(void)
  125. {
  126. uintptr_t i;
  127. char buf[20];
  128. audmux_debugfs_root = debugfs_create_dir("audmux", NULL);
  129. if (!audmux_debugfs_root) {
  130. pr_warning("Failed to create AUDMUX debugfs root\n");
  131. return;
  132. }
  133. for (i = 0; i < MX31_AUDMUX_PORT7_SSI_PINS_7 + 1; i++) {
  134. snprintf(buf, sizeof(buf), "ssi%lu", i);
  135. if (!debugfs_create_file(buf, 0444, audmux_debugfs_root,
  136. (void *)i, &audmux_debugfs_fops))
  137. pr_warning("Failed to create AUDMUX port %lu debugfs file\n",
  138. i);
  139. }
  140. }
  141. static void audmux_debugfs_remove(void)
  142. {
  143. debugfs_remove_recursive(audmux_debugfs_root);
  144. }
  145. #else
  146. static inline void audmux_debugfs_init(void)
  147. {
  148. }
  149. static inline void audmux_debugfs_remove(void)
  150. {
  151. }
  152. #endif
  153. static enum imx_audmux_type {
  154. IMX21_AUDMUX,
  155. IMX31_AUDMUX,
  156. } audmux_type;
  157. static const struct platform_device_id imx_audmux_ids[] = {
  158. {
  159. .name = "imx21-audmux",
  160. .driver_data = IMX21_AUDMUX,
  161. }, {
  162. .name = "imx31-audmux",
  163. .driver_data = IMX31_AUDMUX,
  164. }, {
  165. /* sentinel */
  166. }
  167. };
  168. MODULE_DEVICE_TABLE(platform, imx_audmux_ids);
  169. static const struct of_device_id imx_audmux_dt_ids[] = {
  170. { .compatible = "fsl,imx21-audmux", .data = &imx_audmux_ids[0], },
  171. { .compatible = "fsl,imx31-audmux", .data = &imx_audmux_ids[1], },
  172. { /* sentinel */ }
  173. };
  174. MODULE_DEVICE_TABLE(of, imx_audmux_dt_ids);
  175. static const uint8_t port_mapping[] = {
  176. 0x0, 0x4, 0x8, 0x10, 0x14, 0x1c,
  177. };
  178. int imx_audmux_v1_configure_port(unsigned int port, unsigned int pcr)
  179. {
  180. if (audmux_type != IMX21_AUDMUX)
  181. return -EINVAL;
  182. if (!audmux_base)
  183. return -ENOSYS;
  184. if (port >= ARRAY_SIZE(port_mapping))
  185. return -EINVAL;
  186. writel(pcr, audmux_base + port_mapping[port]);
  187. return 0;
  188. }
  189. EXPORT_SYMBOL_GPL(imx_audmux_v1_configure_port);
  190. int imx_audmux_v2_configure_port(unsigned int port, unsigned int ptcr,
  191. unsigned int pdcr)
  192. {
  193. int ret;
  194. if (audmux_type != IMX31_AUDMUX)
  195. return -EINVAL;
  196. if (!audmux_base)
  197. return -ENOSYS;
  198. if (audmux_clk) {
  199. ret = clk_prepare_enable(audmux_clk);
  200. if (ret)
  201. return ret;
  202. }
  203. writel(ptcr, audmux_base + IMX_AUDMUX_V2_PTCR(port));
  204. writel(pdcr, audmux_base + IMX_AUDMUX_V2_PDCR(port));
  205. if (audmux_clk)
  206. clk_disable_unprepare(audmux_clk);
  207. return 0;
  208. }
  209. EXPORT_SYMBOL_GPL(imx_audmux_v2_configure_port);
  210. static int imx_audmux_parse_dt_defaults(struct platform_device *pdev,
  211. struct device_node *of_node)
  212. {
  213. struct device_node *child;
  214. for_each_available_child_of_node(of_node, child) {
  215. unsigned int port;
  216. unsigned int ptcr = 0;
  217. unsigned int pdcr = 0;
  218. unsigned int pcr = 0;
  219. unsigned int val;
  220. int ret;
  221. int i = 0;
  222. ret = of_property_read_u32(child, "fsl,audmux-port", &port);
  223. if (ret) {
  224. dev_warn(&pdev->dev, "Failed to get fsl,audmux-port of child node \"%s\"\n",
  225. child->full_name);
  226. continue;
  227. }
  228. if (!of_property_read_bool(child, "fsl,port-config")) {
  229. dev_warn(&pdev->dev, "child node \"%s\" does not have property fsl,port-config\n",
  230. child->full_name);
  231. continue;
  232. }
  233. for (i = 0; (ret = of_property_read_u32_index(child,
  234. "fsl,port-config", i, &val)) == 0;
  235. ++i) {
  236. if (audmux_type == IMX31_AUDMUX) {
  237. if (i % 2)
  238. pdcr |= val;
  239. else
  240. ptcr |= val;
  241. } else {
  242. pcr |= val;
  243. }
  244. }
  245. if (ret != -EOVERFLOW) {
  246. dev_err(&pdev->dev, "Failed to read u32 at index %d of child %s\n",
  247. i, child->full_name);
  248. continue;
  249. }
  250. if (audmux_type == IMX31_AUDMUX) {
  251. if (i % 2) {
  252. dev_err(&pdev->dev, "One pdcr value is missing in child node %s\n",
  253. child->full_name);
  254. continue;
  255. }
  256. imx_audmux_v2_configure_port(port, ptcr, pdcr);
  257. } else {
  258. imx_audmux_v1_configure_port(port, pcr);
  259. }
  260. }
  261. return 0;
  262. }
  263. static int imx_audmux_probe(struct platform_device *pdev)
  264. {
  265. struct resource *res;
  266. const struct of_device_id *of_id =
  267. of_match_device(imx_audmux_dt_ids, &pdev->dev);
  268. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  269. audmux_base = devm_ioremap_resource(&pdev->dev, res);
  270. if (IS_ERR(audmux_base))
  271. return PTR_ERR(audmux_base);
  272. audmux_clk = devm_clk_get(&pdev->dev, "audmux");
  273. if (IS_ERR(audmux_clk)) {
  274. dev_dbg(&pdev->dev, "cannot get clock: %ld\n",
  275. PTR_ERR(audmux_clk));
  276. audmux_clk = NULL;
  277. }
  278. if (of_id)
  279. pdev->id_entry = of_id->data;
  280. audmux_type = pdev->id_entry->driver_data;
  281. if (audmux_type == IMX31_AUDMUX)
  282. audmux_debugfs_init();
  283. if (of_id)
  284. imx_audmux_parse_dt_defaults(pdev, pdev->dev.of_node);
  285. return 0;
  286. }
  287. static int imx_audmux_remove(struct platform_device *pdev)
  288. {
  289. if (audmux_type == IMX31_AUDMUX)
  290. audmux_debugfs_remove();
  291. return 0;
  292. }
  293. static struct platform_driver imx_audmux_driver = {
  294. .probe = imx_audmux_probe,
  295. .remove = imx_audmux_remove,
  296. .id_table = imx_audmux_ids,
  297. .driver = {
  298. .name = DRIVER_NAME,
  299. .of_match_table = imx_audmux_dt_ids,
  300. }
  301. };
  302. static int __init imx_audmux_init(void)
  303. {
  304. return platform_driver_register(&imx_audmux_driver);
  305. }
  306. subsys_initcall(imx_audmux_init);
  307. static void __exit imx_audmux_exit(void)
  308. {
  309. platform_driver_unregister(&imx_audmux_driver);
  310. }
  311. module_exit(imx_audmux_exit);
  312. MODULE_DESCRIPTION("Freescale i.MX AUDMUX driver");
  313. MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
  314. MODULE_LICENSE("GPL v2");
  315. MODULE_ALIAS("platform:" DRIVER_NAME);