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