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, https://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;
29 #define IMX_AUDMUX_V2_PTCR(x) ((x) * 8)
30 #define IMX_AUDMUX_V2_PDCR(x) ((x) * 8 + 4)
32 #ifdef CONFIG_DEBUG_FS
33 static struct dentry *audmux_debugfs_root;
35 /* There is an annoying discontinuity in the SSI numbering with regard
36 * to the Linux number of the devices */
37 static const char *audmux_port_string(int port)
40 case MX31_AUDMUX_PORT1_SSI0:
42 case MX31_AUDMUX_PORT2_SSI1:
44 case MX31_AUDMUX_PORT3_SSI_PINS_3:
46 case MX31_AUDMUX_PORT4_SSI_PINS_4:
48 case MX31_AUDMUX_PORT5_SSI_PINS_5:
50 case MX31_AUDMUX_PORT6_SSI_PINS_6:
57 static ssize_t audmux_read_file(struct file *file, char __user *user_buf,
58 size_t count, loff_t *ppos)
62 uintptr_t port = (uintptr_t)file->private_data;
66 ret = clk_prepare_enable(audmux_clk);
71 ptcr = readl(audmux_base + IMX_AUDMUX_V2_PTCR(port));
72 pdcr = readl(audmux_base + IMX_AUDMUX_V2_PDCR(port));
75 clk_disable_unprepare(audmux_clk);
77 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
81 ret = scnprintf(buf, PAGE_SIZE, "PDCR: %08x\nPTCR: %08x\n",
84 if (ptcr & IMX_AUDMUX_V2_PTCR_TFSDIR)
85 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
86 "TxFS output from %s, ",
87 audmux_port_string((ptcr >> 27) & 0x7));
89 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
92 if (ptcr & IMX_AUDMUX_V2_PTCR_TCLKDIR)
93 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
94 "TxClk output from %s",
95 audmux_port_string((ptcr >> 22) & 0x7));
97 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
100 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
102 if (ptcr & IMX_AUDMUX_V2_PTCR_SYN) {
103 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
104 "Port is symmetric");
106 if (ptcr & IMX_AUDMUX_V2_PTCR_RFSDIR)
107 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
108 "RxFS output from %s, ",
109 audmux_port_string((ptcr >> 17) & 0x7));
111 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
114 if (ptcr & IMX_AUDMUX_V2_PTCR_RCLKDIR)
115 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
116 "RxClk output from %s",
117 audmux_port_string((ptcr >> 12) & 0x7));
119 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
123 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
124 "\nData received from %s\n",
125 audmux_port_string((pdcr >> 13) & 0x7));
127 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
134 static const struct file_operations audmux_debugfs_fops = {
136 .read = audmux_read_file,
137 .llseek = default_llseek,
140 static void audmux_debugfs_init(void)
145 audmux_debugfs_root = debugfs_create_dir("audmux", NULL);
147 for (i = 0; i < MX31_AUDMUX_PORT7_SSI_PINS_7 + 1; i++) {
148 snprintf(buf, sizeof(buf), "ssi%lu", i);
149 debugfs_create_file(buf, 0444, audmux_debugfs_root,
150 (void *)i, &audmux_debugfs_fops);
154 static void audmux_debugfs_remove(void)
156 debugfs_remove_recursive(audmux_debugfs_root);
159 static inline void audmux_debugfs_init(void)
163 static inline void audmux_debugfs_remove(void)
168 static enum imx_audmux_type {
173 static const struct of_device_id imx_audmux_dt_ids[] = {
174 { .compatible = "fsl,imx21-audmux", .data = (void *)IMX21_AUDMUX, },
175 { .compatible = "fsl,imx31-audmux", .data = (void *)IMX31_AUDMUX, },
178 MODULE_DEVICE_TABLE(of, imx_audmux_dt_ids);
180 static const uint8_t port_mapping[] = {
181 0x0, 0x4, 0x8, 0x10, 0x14, 0x1c,
184 int imx_audmux_v1_configure_port(unsigned int port, unsigned int pcr)
186 if (audmux_type != IMX21_AUDMUX)
192 if (port >= ARRAY_SIZE(port_mapping))
195 writel(pcr, audmux_base + port_mapping[port]);
199 EXPORT_SYMBOL_GPL(imx_audmux_v1_configure_port);
201 int imx_audmux_v2_configure_port(unsigned int port, unsigned int ptcr,
206 if (audmux_type != IMX31_AUDMUX)
213 ret = clk_prepare_enable(audmux_clk);
218 writel(ptcr, audmux_base + IMX_AUDMUX_V2_PTCR(port));
219 writel(pdcr, audmux_base + IMX_AUDMUX_V2_PDCR(port));
222 clk_disable_unprepare(audmux_clk);
226 EXPORT_SYMBOL_GPL(imx_audmux_v2_configure_port);
228 static int imx_audmux_parse_dt_defaults(struct platform_device *pdev,
229 struct device_node *of_node)
231 struct device_node *child;
233 for_each_available_child_of_node(of_node, child) {
235 unsigned int ptcr = 0;
236 unsigned int pdcr = 0;
237 unsigned int pcr = 0;
242 ret = of_property_read_u32(child, "fsl,audmux-port", &port);
244 dev_warn(&pdev->dev, "Failed to get fsl,audmux-port of child node \"%pOF\"\n",
248 if (!of_property_read_bool(child, "fsl,port-config")) {
249 dev_warn(&pdev->dev, "child node \"%pOF\" does not have property fsl,port-config\n",
254 for (i = 0; (ret = of_property_read_u32_index(child,
255 "fsl,port-config", i, &val)) == 0;
257 if (audmux_type == IMX31_AUDMUX) {
267 if (ret != -EOVERFLOW) {
268 dev_err(&pdev->dev, "Failed to read u32 at index %d of child %pOF\n",
273 if (audmux_type == IMX31_AUDMUX) {
275 dev_err(&pdev->dev, "One pdcr value is missing in child node %pOF\n",
279 imx_audmux_v2_configure_port(port, ptcr, pdcr);
281 imx_audmux_v1_configure_port(port, pcr);
288 static int imx_audmux_probe(struct platform_device *pdev)
290 audmux_base = devm_platform_ioremap_resource(pdev, 0);
291 if (IS_ERR(audmux_base))
292 return PTR_ERR(audmux_base);
294 audmux_clk = devm_clk_get(&pdev->dev, "audmux");
295 if (IS_ERR(audmux_clk)) {
296 dev_dbg(&pdev->dev, "cannot get clock: %ld\n",
297 PTR_ERR(audmux_clk));
301 audmux_type = (enum imx_audmux_type)of_device_get_match_data(&pdev->dev);
303 switch (audmux_type) {
305 audmux_debugfs_init();
312 dev_err(&pdev->dev, "unsupported version!\n");
316 regcache = devm_kzalloc(&pdev->dev, sizeof(u32) * reg_max, GFP_KERNEL);
320 imx_audmux_parse_dt_defaults(pdev, pdev->dev.of_node);
325 static int imx_audmux_remove(struct platform_device *pdev)
327 if (audmux_type == IMX31_AUDMUX)
328 audmux_debugfs_remove();
333 #ifdef CONFIG_PM_SLEEP
334 static int imx_audmux_suspend(struct device *dev)
338 clk_prepare_enable(audmux_clk);
340 for (i = 0; i < reg_max; i++)
341 regcache[i] = readl(audmux_base + i * 4);
343 clk_disable_unprepare(audmux_clk);
348 static int imx_audmux_resume(struct device *dev)
352 clk_prepare_enable(audmux_clk);
354 for (i = 0; i < reg_max; i++)
355 writel(regcache[i], audmux_base + i * 4);
357 clk_disable_unprepare(audmux_clk);
361 #endif /* CONFIG_PM_SLEEP */
363 static const struct dev_pm_ops imx_audmux_pm = {
364 SET_SYSTEM_SLEEP_PM_OPS(imx_audmux_suspend, imx_audmux_resume)
367 static struct platform_driver imx_audmux_driver = {
368 .probe = imx_audmux_probe,
369 .remove = imx_audmux_remove,
372 .pm = &imx_audmux_pm,
373 .of_match_table = imx_audmux_dt_ids,
377 static int __init imx_audmux_init(void)
379 return platform_driver_register(&imx_audmux_driver);
381 subsys_initcall(imx_audmux_init);
383 static void __exit imx_audmux_exit(void)
385 platform_driver_unregister(&imx_audmux_driver);
387 module_exit(imx_audmux_exit);
389 MODULE_DESCRIPTION("Freescale i.MX AUDMUX driver");
390 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
391 MODULE_LICENSE("GPL v2");
392 MODULE_ALIAS("platform:" DRIVER_NAME);