2 * Copyright 2012 Freescale Semiconductor, Inc.
3 * Copyright 2012 Linaro Ltd.
4 * Copyright 2009 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de>
6 * Initial development of this code was funded by
7 * Phytec Messtechnik GmbH, http://www.phytec.de
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.
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.
20 #include <linux/clk.h>
21 #include <linux/debugfs.h>
22 #include <linux/err.h>
24 #include <linux/module.h>
26 #include <linux/of_device.h>
27 #include <linux/platform_device.h>
28 #include <linux/slab.h>
30 #include "imx-audmux.h"
32 #define DRIVER_NAME "imx-audmux"
34 static struct clk *audmux_clk;
35 static void __iomem *audmux_base;
37 #define IMX_AUDMUX_V2_PTCR(x) ((x) * 8)
38 #define IMX_AUDMUX_V2_PDCR(x) ((x) * 8 + 4)
40 #ifdef CONFIG_DEBUG_FS
41 static struct dentry *audmux_debugfs_root;
43 /* There is an annoying discontinuity in the SSI numbering with regard
44 * to the Linux number of the devices */
45 static const char *audmux_port_string(int port)
48 case MX31_AUDMUX_PORT1_SSI0:
50 case MX31_AUDMUX_PORT2_SSI1:
52 case MX31_AUDMUX_PORT3_SSI_PINS_3:
54 case MX31_AUDMUX_PORT4_SSI_PINS_4:
56 case MX31_AUDMUX_PORT5_SSI_PINS_5:
58 case MX31_AUDMUX_PORT6_SSI_PINS_6:
65 static ssize_t audmux_read_file(struct file *file, char __user *user_buf,
66 size_t count, loff_t *ppos)
69 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
70 int port = (int)file->private_data;
77 clk_prepare_enable(audmux_clk);
79 ptcr = readl(audmux_base + IMX_AUDMUX_V2_PTCR(port));
80 pdcr = readl(audmux_base + IMX_AUDMUX_V2_PDCR(port));
83 clk_disable_unprepare(audmux_clk);
85 ret = snprintf(buf, PAGE_SIZE, "PDCR: %08x\nPTCR: %08x\n",
88 if (ptcr & IMX_AUDMUX_V2_PTCR_TFSDIR)
89 ret += snprintf(buf + ret, PAGE_SIZE - ret,
90 "TxFS output from %s, ",
91 audmux_port_string((ptcr >> 27) & 0x7));
93 ret += snprintf(buf + ret, PAGE_SIZE - ret,
96 if (ptcr & IMX_AUDMUX_V2_PTCR_TCLKDIR)
97 ret += snprintf(buf + ret, PAGE_SIZE - ret,
98 "TxClk output from %s",
99 audmux_port_string((ptcr >> 22) & 0x7));
101 ret += snprintf(buf + ret, PAGE_SIZE - ret,
104 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
106 if (ptcr & IMX_AUDMUX_V2_PTCR_SYN) {
107 ret += snprintf(buf + ret, PAGE_SIZE - ret,
108 "Port is symmetric");
110 if (ptcr & IMX_AUDMUX_V2_PTCR_RFSDIR)
111 ret += snprintf(buf + ret, PAGE_SIZE - ret,
112 "RxFS output from %s, ",
113 audmux_port_string((ptcr >> 17) & 0x7));
115 ret += snprintf(buf + ret, PAGE_SIZE - ret,
118 if (ptcr & IMX_AUDMUX_V2_PTCR_RCLKDIR)
119 ret += snprintf(buf + ret, PAGE_SIZE - ret,
120 "RxClk output from %s",
121 audmux_port_string((ptcr >> 12) & 0x7));
123 ret += snprintf(buf + ret, PAGE_SIZE - ret,
127 ret += snprintf(buf + ret, PAGE_SIZE - ret,
128 "\nData received from %s\n",
129 audmux_port_string((pdcr >> 13) & 0x7));
131 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
138 static const struct file_operations audmux_debugfs_fops = {
140 .read = audmux_read_file,
141 .llseek = default_llseek,
144 static void __init audmux_debugfs_init(void)
149 audmux_debugfs_root = debugfs_create_dir("audmux", NULL);
150 if (!audmux_debugfs_root) {
151 pr_warning("Failed to create AUDMUX debugfs root\n");
155 for (i = 1; i < 8; i++) {
156 snprintf(buf, sizeof(buf), "ssi%d", i);
157 if (!debugfs_create_file(buf, 0444, audmux_debugfs_root,
158 (void *)i, &audmux_debugfs_fops))
159 pr_warning("Failed to create AUDMUX port %d debugfs file\n",
164 static void __devexit audmux_debugfs_remove(void)
166 debugfs_remove_recursive(audmux_debugfs_root);
169 static inline void audmux_debugfs_init(void)
173 static inline void audmux_debugfs_remove(void)
178 enum imx_audmux_type {
183 static struct platform_device_id imx_audmux_ids[] = {
185 .name = "imx21-audmux",
186 .driver_data = IMX21_AUDMUX,
188 .name = "imx31-audmux",
189 .driver_data = IMX31_AUDMUX,
194 MODULE_DEVICE_TABLE(platform, imx_audmux_ids);
196 static const struct of_device_id imx_audmux_dt_ids[] = {
197 { .compatible = "fsl,imx21-audmux", .data = &imx_audmux_ids[0], },
198 { .compatible = "fsl,imx31-audmux", .data = &imx_audmux_ids[1], },
201 MODULE_DEVICE_TABLE(of, imx_audmux_dt_ids);
203 static const uint8_t port_mapping[] = {
204 0x0, 0x4, 0x8, 0x10, 0x14, 0x1c,
207 int imx_audmux_v1_configure_port(unsigned int port, unsigned int pcr)
209 if (audmux_type != IMX21_AUDMUX)
215 if (port >= ARRAY_SIZE(port_mapping))
218 writel(pcr, audmux_base + port_mapping[port]);
222 EXPORT_SYMBOL_GPL(imx_audmux_v1_configure_port);
224 int imx_audmux_v2_configure_port(unsigned int port, unsigned int ptcr,
227 if (audmux_type != IMX31_AUDMUX)
234 clk_prepare_enable(audmux_clk);
236 writel(ptcr, audmux_base + IMX_AUDMUX_V2_PTCR(port));
237 writel(pdcr, audmux_base + IMX_AUDMUX_V2_PDCR(port));
240 clk_disable_unprepare(audmux_clk);
244 EXPORT_SYMBOL_GPL(imx_audmux_v2_configure_port);
246 static int __devinit imx_audmux_probe(struct platform_device *pdev)
248 struct resource *res;
249 const struct of_device_id *of_id =
250 of_match_device(imx_audmux_dt_ids, &pdev->dev);
252 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
253 audmux_base = devm_request_and_ioremap(&pdev->dev, res);
255 return -EADDRNOTAVAIL;
257 audmux_clk = clk_get(&pdev->dev, "audmux");
258 if (IS_ERR(audmux_clk)) {
259 dev_dbg(&pdev->dev, "cannot get clock: %ld\n",
260 PTR_ERR(audmux_clk));
265 pdev->id_entry = of_id->data;
266 audmux_type = pdev->id_entry->driver_data;
267 if (audmux_type == IMX31_AUDMUX)
268 audmux_debugfs_init();
273 static int __devexit imx_audmux_remove(struct platform_device *pdev)
275 if (audmux_type == IMX31_AUDMUX)
276 audmux_debugfs_remove();
282 static struct platform_driver imx_audmux_driver = {
283 .probe = imx_audmux_probe,
284 .remove = __devexit_p(imx_audmux_remove),
285 .id_table = imx_audmux_ids,
288 .owner = THIS_MODULE,
289 .of_match_table = imx_audmux_dt_ids,
293 static int __init imx_audmux_init(void)
295 return platform_driver_register(&imx_audmux_driver);
297 subsys_initcall(imx_audmux_init);
299 static void __exit imx_audmux_exit(void)
301 platform_driver_unregister(&imx_audmux_driver);
303 module_exit(imx_audmux_exit);
305 MODULE_DESCRIPTION("Freescale i.MX AUDMUX driver");
306 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
307 MODULE_LICENSE("GPL v2");
308 MODULE_ALIAS("platform:" DRIVER_NAME);