1 // SPDX-License-Identifier: GPL-2.0+
3 // Copyright 2012 Freescale Semiconductor, Inc.
4 // Copyright 2012 Linaro Ltd.
5 // Copyright 2009 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de>
7 // Initial development of this code was funded by
8 // Phytec Messtechnik GmbH, http://www.phytec.de
10 #include <linux/clk.h>
11 #include <linux/debugfs.h>
12 #include <linux/err.h>
14 #include <linux/module.h>
16 #include <linux/of_device.h>
17 #include <linux/platform_device.h>
18 #include <linux/slab.h>
20 #include "imx-audmux.h"
22 #define DRIVER_NAME "imx-audmux"
24 static struct clk *audmux_clk;
25 static void __iomem *audmux_base;
27 #define IMX_AUDMUX_V2_PTCR(x) ((x) * 8)
28 #define IMX_AUDMUX_V2_PDCR(x) ((x) * 8 + 4)
30 #ifdef CONFIG_DEBUG_FS
31 static struct dentry *audmux_debugfs_root;
33 /* There is an annoying discontinuity in the SSI numbering with regard
34 * to the Linux number of the devices */
35 static const char *audmux_port_string(int port)
38 case MX31_AUDMUX_PORT1_SSI0:
40 case MX31_AUDMUX_PORT2_SSI1:
42 case MX31_AUDMUX_PORT3_SSI_PINS_3:
44 case MX31_AUDMUX_PORT4_SSI_PINS_4:
46 case MX31_AUDMUX_PORT5_SSI_PINS_5:
48 case MX31_AUDMUX_PORT6_SSI_PINS_6:
55 static ssize_t audmux_read_file(struct file *file, char __user *user_buf,
56 size_t count, loff_t *ppos)
60 uintptr_t port = (uintptr_t)file->private_data;
64 ret = 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));
73 clk_disable_unprepare(audmux_clk);
75 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
79 ret = scnprintf(buf, PAGE_SIZE, "PDCR: %08x\nPTCR: %08x\n",
82 if (ptcr & IMX_AUDMUX_V2_PTCR_TFSDIR)
83 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
84 "TxFS output from %s, ",
85 audmux_port_string((ptcr >> 27) & 0x7));
87 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
90 if (ptcr & IMX_AUDMUX_V2_PTCR_TCLKDIR)
91 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
92 "TxClk output from %s",
93 audmux_port_string((ptcr >> 22) & 0x7));
95 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
98 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
100 if (ptcr & IMX_AUDMUX_V2_PTCR_SYN) {
101 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
102 "Port is symmetric");
104 if (ptcr & IMX_AUDMUX_V2_PTCR_RFSDIR)
105 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
106 "RxFS output from %s, ",
107 audmux_port_string((ptcr >> 17) & 0x7));
109 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
112 if (ptcr & IMX_AUDMUX_V2_PTCR_RCLKDIR)
113 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
114 "RxClk output from %s",
115 audmux_port_string((ptcr >> 12) & 0x7));
117 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
121 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
122 "\nData received from %s\n",
123 audmux_port_string((pdcr >> 13) & 0x7));
125 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
132 static const struct file_operations audmux_debugfs_fops = {
134 .read = audmux_read_file,
135 .llseek = default_llseek,
138 static void audmux_debugfs_init(void)
143 audmux_debugfs_root = debugfs_create_dir("audmux", NULL);
144 if (!audmux_debugfs_root) {
145 pr_warning("Failed to create AUDMUX debugfs root\n");
149 for (i = 0; i < MX31_AUDMUX_PORT7_SSI_PINS_7 + 1; i++) {
150 snprintf(buf, sizeof(buf), "ssi%lu", i);
151 if (!debugfs_create_file(buf, 0444, audmux_debugfs_root,
152 (void *)i, &audmux_debugfs_fops))
153 pr_warning("Failed to create AUDMUX port %lu debugfs file\n",
158 static void audmux_debugfs_remove(void)
160 debugfs_remove_recursive(audmux_debugfs_root);
163 static inline void audmux_debugfs_init(void)
167 static inline void audmux_debugfs_remove(void)
172 static enum imx_audmux_type {
177 static const struct platform_device_id imx_audmux_ids[] = {
179 .name = "imx21-audmux",
180 .driver_data = IMX21_AUDMUX,
182 .name = "imx31-audmux",
183 .driver_data = IMX31_AUDMUX,
188 MODULE_DEVICE_TABLE(platform, imx_audmux_ids);
190 static const struct of_device_id imx_audmux_dt_ids[] = {
191 { .compatible = "fsl,imx21-audmux", .data = &imx_audmux_ids[0], },
192 { .compatible = "fsl,imx31-audmux", .data = &imx_audmux_ids[1], },
195 MODULE_DEVICE_TABLE(of, imx_audmux_dt_ids);
197 static const uint8_t port_mapping[] = {
198 0x0, 0x4, 0x8, 0x10, 0x14, 0x1c,
201 int imx_audmux_v1_configure_port(unsigned int port, unsigned int pcr)
203 if (audmux_type != IMX21_AUDMUX)
209 if (port >= ARRAY_SIZE(port_mapping))
212 writel(pcr, audmux_base + port_mapping[port]);
216 EXPORT_SYMBOL_GPL(imx_audmux_v1_configure_port);
218 int imx_audmux_v2_configure_port(unsigned int port, unsigned int ptcr,
223 if (audmux_type != IMX31_AUDMUX)
230 ret = clk_prepare_enable(audmux_clk);
235 writel(ptcr, audmux_base + IMX_AUDMUX_V2_PTCR(port));
236 writel(pdcr, audmux_base + IMX_AUDMUX_V2_PDCR(port));
239 clk_disable_unprepare(audmux_clk);
243 EXPORT_SYMBOL_GPL(imx_audmux_v2_configure_port);
245 static int imx_audmux_parse_dt_defaults(struct platform_device *pdev,
246 struct device_node *of_node)
248 struct device_node *child;
250 for_each_available_child_of_node(of_node, child) {
252 unsigned int ptcr = 0;
253 unsigned int pdcr = 0;
254 unsigned int pcr = 0;
259 ret = of_property_read_u32(child, "fsl,audmux-port", &port);
261 dev_warn(&pdev->dev, "Failed to get fsl,audmux-port of child node \"%pOF\"\n",
265 if (!of_property_read_bool(child, "fsl,port-config")) {
266 dev_warn(&pdev->dev, "child node \"%pOF\" does not have property fsl,port-config\n",
271 for (i = 0; (ret = of_property_read_u32_index(child,
272 "fsl,port-config", i, &val)) == 0;
274 if (audmux_type == IMX31_AUDMUX) {
284 if (ret != -EOVERFLOW) {
285 dev_err(&pdev->dev, "Failed to read u32 at index %d of child %pOF\n",
290 if (audmux_type == IMX31_AUDMUX) {
292 dev_err(&pdev->dev, "One pdcr value is missing in child node %pOF\n",
296 imx_audmux_v2_configure_port(port, ptcr, pdcr);
298 imx_audmux_v1_configure_port(port, pcr);
305 static int imx_audmux_probe(struct platform_device *pdev)
307 struct resource *res;
308 const struct of_device_id *of_id =
309 of_match_device(imx_audmux_dt_ids, &pdev->dev);
311 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
312 audmux_base = devm_ioremap_resource(&pdev->dev, res);
313 if (IS_ERR(audmux_base))
314 return PTR_ERR(audmux_base);
316 audmux_clk = devm_clk_get(&pdev->dev, "audmux");
317 if (IS_ERR(audmux_clk)) {
318 dev_dbg(&pdev->dev, "cannot get clock: %ld\n",
319 PTR_ERR(audmux_clk));
324 pdev->id_entry = of_id->data;
325 audmux_type = pdev->id_entry->driver_data;
326 if (audmux_type == IMX31_AUDMUX)
327 audmux_debugfs_init();
330 imx_audmux_parse_dt_defaults(pdev, pdev->dev.of_node);
335 static int imx_audmux_remove(struct platform_device *pdev)
337 if (audmux_type == IMX31_AUDMUX)
338 audmux_debugfs_remove();
343 static struct platform_driver imx_audmux_driver = {
344 .probe = imx_audmux_probe,
345 .remove = imx_audmux_remove,
346 .id_table = imx_audmux_ids,
349 .of_match_table = imx_audmux_dt_ids,
353 static int __init imx_audmux_init(void)
355 return platform_driver_register(&imx_audmux_driver);
357 subsys_initcall(imx_audmux_init);
359 static void __exit imx_audmux_exit(void)
361 platform_driver_unregister(&imx_audmux_driver);
363 module_exit(imx_audmux_exit);
365 MODULE_DESCRIPTION("Freescale i.MX AUDMUX driver");
366 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
367 MODULE_LICENSE("GPL v2");
368 MODULE_ALIAS("platform:" DRIVER_NAME);