imx-audmux.c 8.6 KB

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