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/platform_device.h>
17 #include <linux/slab.h>
19 #include "imx-audmux.h"
21 #define DRIVER_NAME "imx-audmux"
23 static struct clk *audmux_clk;
24 static void __iomem *audmux_base;
28 #define IMX_AUDMUX_V2_PTCR(x) ((x) * 8)
29 #define IMX_AUDMUX_V2_PDCR(x) ((x) * 8 + 4)
31 #ifdef CONFIG_DEBUG_FS
32 static struct dentry *audmux_debugfs_root;
34 /* There is an annoying discontinuity in the SSI numbering with regard
35 * to the Linux number of the devices */
36 static const char *audmux_port_string(int port)
39 case MX31_AUDMUX_PORT1_SSI0:
41 case MX31_AUDMUX_PORT2_SSI1:
43 case MX31_AUDMUX_PORT3_SSI_PINS_3:
45 case MX31_AUDMUX_PORT4_SSI_PINS_4:
47 case MX31_AUDMUX_PORT5_SSI_PINS_5:
49 case MX31_AUDMUX_PORT6_SSI_PINS_6:
56 static ssize_t audmux_read_file(struct file *file, char __user *user_buf,
57 size_t count, loff_t *ppos)
61 uintptr_t port = (uintptr_t)file->private_data;
64 ret = clk_prepare_enable(audmux_clk);
68 ptcr = readl(audmux_base + IMX_AUDMUX_V2_PTCR(port));
69 pdcr = readl(audmux_base + IMX_AUDMUX_V2_PDCR(port));
71 clk_disable_unprepare(audmux_clk);
73 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
77 ret = sysfs_emit(buf, "PDCR: %08x\nPTCR: %08x\n", pdcr, ptcr);
79 if (ptcr & IMX_AUDMUX_V2_PTCR_TFSDIR)
80 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
81 "TxFS output from %s, ",
82 audmux_port_string((ptcr >> 27) & 0x7));
84 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
87 if (ptcr & IMX_AUDMUX_V2_PTCR_TCLKDIR)
88 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
89 "TxClk output from %s",
90 audmux_port_string((ptcr >> 22) & 0x7));
92 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
95 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
97 if (ptcr & IMX_AUDMUX_V2_PTCR_SYN) {
98 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
101 if (ptcr & IMX_AUDMUX_V2_PTCR_RFSDIR)
102 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
103 "RxFS output from %s, ",
104 audmux_port_string((ptcr >> 17) & 0x7));
106 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
109 if (ptcr & IMX_AUDMUX_V2_PTCR_RCLKDIR)
110 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
111 "RxClk output from %s",
112 audmux_port_string((ptcr >> 12) & 0x7));
114 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
118 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
119 "\nData received from %s\n",
120 audmux_port_string((pdcr >> 13) & 0x7));
122 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
129 static const struct file_operations audmux_debugfs_fops = {
131 .read = audmux_read_file,
132 .llseek = default_llseek,
135 static void audmux_debugfs_init(void)
140 audmux_debugfs_root = debugfs_create_dir("audmux", NULL);
142 for (i = 0; i < MX31_AUDMUX_PORT7_SSI_PINS_7 + 1; i++) {
143 snprintf(buf, sizeof(buf), "ssi%lu", i);
144 debugfs_create_file(buf, 0444, audmux_debugfs_root,
145 (void *)i, &audmux_debugfs_fops);
149 static void audmux_debugfs_remove(void)
151 debugfs_remove_recursive(audmux_debugfs_root);
154 static inline void audmux_debugfs_init(void)
158 static inline void audmux_debugfs_remove(void)
163 static enum imx_audmux_type {
168 static const struct of_device_id imx_audmux_dt_ids[] = {
169 { .compatible = "fsl,imx21-audmux", .data = (void *)IMX21_AUDMUX, },
170 { .compatible = "fsl,imx31-audmux", .data = (void *)IMX31_AUDMUX, },
173 MODULE_DEVICE_TABLE(of, imx_audmux_dt_ids);
175 static const uint8_t port_mapping[] = {
176 0x0, 0x4, 0x8, 0x10, 0x14, 0x1c,
179 int imx_audmux_v1_configure_port(unsigned int port, unsigned int pcr)
181 if (audmux_type != IMX21_AUDMUX)
187 if (port >= ARRAY_SIZE(port_mapping))
190 writel(pcr, audmux_base + port_mapping[port]);
194 EXPORT_SYMBOL_GPL(imx_audmux_v1_configure_port);
196 int imx_audmux_v2_configure_port(unsigned int port, unsigned int ptcr,
201 if (audmux_type != IMX31_AUDMUX)
207 ret = clk_prepare_enable(audmux_clk);
211 writel(ptcr, audmux_base + IMX_AUDMUX_V2_PTCR(port));
212 writel(pdcr, audmux_base + IMX_AUDMUX_V2_PDCR(port));
214 clk_disable_unprepare(audmux_clk);
218 EXPORT_SYMBOL_GPL(imx_audmux_v2_configure_port);
220 static int imx_audmux_parse_dt_defaults(struct platform_device *pdev,
221 struct device_node *of_node)
223 struct device_node *child;
225 for_each_available_child_of_node(of_node, child) {
227 unsigned int ptcr = 0;
228 unsigned int pdcr = 0;
229 unsigned int pcr = 0;
234 ret = of_property_read_u32(child, "fsl,audmux-port", &port);
236 dev_warn(&pdev->dev, "Failed to get fsl,audmux-port of child node \"%pOF\"\n",
240 if (!of_property_read_bool(child, "fsl,port-config")) {
241 dev_warn(&pdev->dev, "child node \"%pOF\" does not have property fsl,port-config\n",
246 for (i = 0; (ret = of_property_read_u32_index(child,
247 "fsl,port-config", i, &val)) == 0;
249 if (audmux_type == IMX31_AUDMUX) {
259 if (ret != -EOVERFLOW) {
260 dev_err(&pdev->dev, "Failed to read u32 at index %d of child %pOF\n",
265 if (audmux_type == IMX31_AUDMUX) {
267 dev_err(&pdev->dev, "One pdcr value is missing in child node %pOF\n",
271 imx_audmux_v2_configure_port(port, ptcr, pdcr);
273 imx_audmux_v1_configure_port(port, pcr);
280 static int imx_audmux_probe(struct platform_device *pdev)
282 audmux_base = devm_platform_ioremap_resource(pdev, 0);
283 if (IS_ERR(audmux_base))
284 return PTR_ERR(audmux_base);
286 audmux_clk = devm_clk_get(&pdev->dev, "audmux");
287 if (IS_ERR(audmux_clk)) {
288 dev_dbg(&pdev->dev, "cannot get clock: %ld\n",
289 PTR_ERR(audmux_clk));
293 audmux_type = (uintptr_t)of_device_get_match_data(&pdev->dev);
295 switch (audmux_type) {
297 audmux_debugfs_init();
304 dev_err(&pdev->dev, "unsupported version!\n");
308 regcache = devm_kzalloc(&pdev->dev, sizeof(u32) * reg_max, GFP_KERNEL);
312 imx_audmux_parse_dt_defaults(pdev, pdev->dev.of_node);
317 static void imx_audmux_remove(struct platform_device *pdev)
319 if (audmux_type == IMX31_AUDMUX)
320 audmux_debugfs_remove();
323 #ifdef CONFIG_PM_SLEEP
324 static int imx_audmux_suspend(struct device *dev)
328 clk_prepare_enable(audmux_clk);
330 for (i = 0; i < reg_max; i++)
331 regcache[i] = readl(audmux_base + i * 4);
333 clk_disable_unprepare(audmux_clk);
338 static int imx_audmux_resume(struct device *dev)
342 clk_prepare_enable(audmux_clk);
344 for (i = 0; i < reg_max; i++)
345 writel(regcache[i], audmux_base + i * 4);
347 clk_disable_unprepare(audmux_clk);
351 #endif /* CONFIG_PM_SLEEP */
353 static const struct dev_pm_ops imx_audmux_pm = {
354 SET_SYSTEM_SLEEP_PM_OPS(imx_audmux_suspend, imx_audmux_resume)
357 static struct platform_driver imx_audmux_driver = {
358 .probe = imx_audmux_probe,
359 .remove_new = imx_audmux_remove,
362 .pm = &imx_audmux_pm,
363 .of_match_table = imx_audmux_dt_ids,
367 static int __init imx_audmux_init(void)
369 return platform_driver_register(&imx_audmux_driver);
371 subsys_initcall(imx_audmux_init);
373 static void __exit imx_audmux_exit(void)
375 platform_driver_unregister(&imx_audmux_driver);
377 module_exit(imx_audmux_exit);
379 MODULE_DESCRIPTION("Freescale i.MX AUDMUX driver");
380 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
381 MODULE_LICENSE("GPL v2");
382 MODULE_ALIAS("platform:" DRIVER_NAME);