imx-audmux.c 7.0 KB

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