imx-audmux.c 9.0 KB

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