blob: ab17381cc9812a7e0538f30ea44496bfdd944c6d [file] [log] [blame]
Sascha Hauer9eedbdf2009-10-29 17:12:39 +01001/*
Richard Zhao3bc34a62012-03-05 22:30:52 +08002 * Copyright 2012 Freescale Semiconductor, Inc.
3 * Copyright 2012 Linaro Ltd.
Sascha Hauer9eedbdf2009-10-29 17:12:39 +01004 * 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.
Sascha Hauer9eedbdf2009-10-29 17:12:39 +010018 */
19
Sascha Hauer9eedbdf2009-10-29 17:12:39 +010020#include <linux/clk.h>
Mark Brown7b4e08a2010-01-11 16:33:18 +000021#include <linux/debugfs.h>
Richard Zhao3bc34a62012-03-05 22:30:52 +080022#include <linux/err.h>
23#include <linux/io.h>
24#include <linux/module.h>
Richard Zhao9d5ef262012-03-05 22:31:04 +080025#include <linux/of.h>
26#include <linux/of_device.h>
Richard Zhao3bc34a62012-03-05 22:30:52 +080027#include <linux/platform_device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Shawn Guo3c77c292012-03-05 22:30:53 +080029
30#include "imx-audmux.h"
Richard Zhao3bc34a62012-03-05 22:30:52 +080031
32#define DRIVER_NAME "imx-audmux"
Sascha Hauer9eedbdf2009-10-29 17:12:39 +010033
34static struct clk *audmux_clk;
35static void __iomem *audmux_base;
36
Shawn Guoaf4872f2012-03-05 22:30:54 +080037#define IMX_AUDMUX_V2_PTCR(x) ((x) * 8)
38#define IMX_AUDMUX_V2_PDCR(x) ((x) * 8 + 4)
Sascha Hauer9eedbdf2009-10-29 17:12:39 +010039
Mark Brown7b4e08a2010-01-11 16:33:18 +000040#ifdef CONFIG_DEBUG_FS
41static struct dentry *audmux_debugfs_root;
42
Mark Brown7b4e08a2010-01-11 16:33:18 +000043/* There is an annoying discontinuity in the SSI numbering with regard
44 * to the Linux number of the devices */
45static const char *audmux_port_string(int port)
46{
47 switch (port) {
48 case MX31_AUDMUX_PORT1_SSI0:
49 return "imx-ssi.0";
50 case MX31_AUDMUX_PORT2_SSI1:
51 return "imx-ssi.1";
52 case MX31_AUDMUX_PORT3_SSI_PINS_3:
53 return "SSI3";
54 case MX31_AUDMUX_PORT4_SSI_PINS_4:
55 return "SSI4";
56 case MX31_AUDMUX_PORT5_SSI_PINS_5:
57 return "SSI5";
58 case MX31_AUDMUX_PORT6_SSI_PINS_6:
59 return "SSI6";
60 default:
61 return "UNKNOWN";
62 }
63}
64
65static ssize_t audmux_read_file(struct file *file, char __user *user_buf,
66 size_t count, loff_t *ppos)
67{
68 ssize_t ret;
69 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
70 int port = (int)file->private_data;
71 u32 pdcr, ptcr;
72
73 if (!buf)
74 return -ENOMEM;
75
Fabio Estevam72192362013-07-21 13:39:08 -030076 if (audmux_clk) {
77 ret = clk_prepare_enable(audmux_clk);
78 if (ret)
79 return ret;
80 }
Mark Brown7b4e08a2010-01-11 16:33:18 +000081
Shawn Guoaf4872f2012-03-05 22:30:54 +080082 ptcr = readl(audmux_base + IMX_AUDMUX_V2_PTCR(port));
83 pdcr = readl(audmux_base + IMX_AUDMUX_V2_PDCR(port));
Mark Brown7b4e08a2010-01-11 16:33:18 +000084
85 if (audmux_clk)
Linus Torvalds34800592012-03-27 16:41:24 -070086 clk_disable_unprepare(audmux_clk);
Mark Brown7b4e08a2010-01-11 16:33:18 +000087
88 ret = snprintf(buf, PAGE_SIZE, "PDCR: %08x\nPTCR: %08x\n",
89 pdcr, ptcr);
90
Shawn Guoaf4872f2012-03-05 22:30:54 +080091 if (ptcr & IMX_AUDMUX_V2_PTCR_TFSDIR)
Mark Brown7b4e08a2010-01-11 16:33:18 +000092 ret += snprintf(buf + ret, PAGE_SIZE - ret,
93 "TxFS output from %s, ",
94 audmux_port_string((ptcr >> 27) & 0x7));
95 else
96 ret += snprintf(buf + ret, PAGE_SIZE - ret,
97 "TxFS input, ");
98
Shawn Guoaf4872f2012-03-05 22:30:54 +080099 if (ptcr & IMX_AUDMUX_V2_PTCR_TCLKDIR)
Mark Brown7b4e08a2010-01-11 16:33:18 +0000100 ret += snprintf(buf + ret, PAGE_SIZE - ret,
101 "TxClk output from %s",
102 audmux_port_string((ptcr >> 22) & 0x7));
103 else
104 ret += snprintf(buf + ret, PAGE_SIZE - ret,
105 "TxClk input");
106
107 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
108
Shawn Guoaf4872f2012-03-05 22:30:54 +0800109 if (ptcr & IMX_AUDMUX_V2_PTCR_SYN) {
Mark Brown7b4e08a2010-01-11 16:33:18 +0000110 ret += snprintf(buf + ret, PAGE_SIZE - ret,
111 "Port is symmetric");
112 } else {
Shawn Guoaf4872f2012-03-05 22:30:54 +0800113 if (ptcr & IMX_AUDMUX_V2_PTCR_RFSDIR)
Mark Brown7b4e08a2010-01-11 16:33:18 +0000114 ret += snprintf(buf + ret, PAGE_SIZE - ret,
115 "RxFS output from %s, ",
116 audmux_port_string((ptcr >> 17) & 0x7));
117 else
118 ret += snprintf(buf + ret, PAGE_SIZE - ret,
119 "RxFS input, ");
120
Shawn Guoaf4872f2012-03-05 22:30:54 +0800121 if (ptcr & IMX_AUDMUX_V2_PTCR_RCLKDIR)
Mark Brown7b4e08a2010-01-11 16:33:18 +0000122 ret += snprintf(buf + ret, PAGE_SIZE - ret,
123 "RxClk output from %s",
124 audmux_port_string((ptcr >> 12) & 0x7));
125 else
126 ret += snprintf(buf + ret, PAGE_SIZE - ret,
127 "RxClk input");
128 }
129
130 ret += snprintf(buf + ret, PAGE_SIZE - ret,
131 "\nData received from %s\n",
132 audmux_port_string((pdcr >> 13) & 0x7));
133
134 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
135
136 kfree(buf);
137
138 return ret;
139}
140
141static const struct file_operations audmux_debugfs_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -0700142 .open = simple_open,
Mark Brown7b4e08a2010-01-11 16:33:18 +0000143 .read = audmux_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200144 .llseek = default_llseek,
Mark Brown7b4e08a2010-01-11 16:33:18 +0000145};
146
Richard Zhao3bc34a62012-03-05 22:30:52 +0800147static void __init audmux_debugfs_init(void)
Mark Brown7b4e08a2010-01-11 16:33:18 +0000148{
149 int i;
150 char buf[20];
151
152 audmux_debugfs_root = debugfs_create_dir("audmux", NULL);
153 if (!audmux_debugfs_root) {
154 pr_warning("Failed to create AUDMUX debugfs root\n");
155 return;
156 }
157
Torben Hohn409b78c2012-07-18 15:01:17 +0200158 for (i = 0; i < MX31_AUDMUX_PORT7_SSI_PINS_7 + 1; i++) {
Mark Brown7b4e08a2010-01-11 16:33:18 +0000159 snprintf(buf, sizeof(buf), "ssi%d", i);
160 if (!debugfs_create_file(buf, 0444, audmux_debugfs_root,
161 (void *)i, &audmux_debugfs_fops))
162 pr_warning("Failed to create AUDMUX port %d debugfs file\n",
163 i);
164 }
165}
Richard Zhao3bc34a62012-03-05 22:30:52 +0800166
Bill Pembertona0a3d512012-12-07 09:26:16 -0500167static void audmux_debugfs_remove(void)
Richard Zhao3bc34a62012-03-05 22:30:52 +0800168{
169 debugfs_remove_recursive(audmux_debugfs_root);
170}
Mark Brown7b4e08a2010-01-11 16:33:18 +0000171#else
172static inline void audmux_debugfs_init(void)
173{
174}
Richard Zhao3bc34a62012-03-05 22:30:52 +0800175
176static inline void audmux_debugfs_remove(void)
177{
178}
Mark Brown7b4e08a2010-01-11 16:33:18 +0000179#endif
180
Fabio Estevama10807a2013-02-11 13:39:08 -0200181static enum imx_audmux_type {
Richard Zhao3bc34a62012-03-05 22:30:52 +0800182 IMX21_AUDMUX,
183 IMX31_AUDMUX,
184} audmux_type;
185
186static struct platform_device_id imx_audmux_ids[] = {
187 {
188 .name = "imx21-audmux",
189 .driver_data = IMX21_AUDMUX,
190 }, {
191 .name = "imx31-audmux",
192 .driver_data = IMX31_AUDMUX,
193 }, {
194 /* sentinel */
195 }
196};
197MODULE_DEVICE_TABLE(platform, imx_audmux_ids);
198
Richard Zhao9d5ef262012-03-05 22:31:04 +0800199static const struct of_device_id imx_audmux_dt_ids[] = {
200 { .compatible = "fsl,imx21-audmux", .data = &imx_audmux_ids[0], },
201 { .compatible = "fsl,imx31-audmux", .data = &imx_audmux_ids[1], },
202 { /* sentinel */ }
203};
204MODULE_DEVICE_TABLE(of, imx_audmux_dt_ids);
205
Shawn Guo2405fc92012-03-05 22:30:51 +0800206static const uint8_t port_mapping[] = {
207 0x0, 0x4, 0x8, 0x10, 0x14, 0x1c,
208};
209
Shawn Guoaf4872f2012-03-05 22:30:54 +0800210int imx_audmux_v1_configure_port(unsigned int port, unsigned int pcr)
Shawn Guo2405fc92012-03-05 22:30:51 +0800211{
Richard Zhao3bc34a62012-03-05 22:30:52 +0800212 if (audmux_type != IMX21_AUDMUX)
213 return -EINVAL;
214
Shawn Guo2405fc92012-03-05 22:30:51 +0800215 if (!audmux_base)
216 return -ENOSYS;
217
218 if (port >= ARRAY_SIZE(port_mapping))
219 return -EINVAL;
220
221 writel(pcr, audmux_base + port_mapping[port]);
222
223 return 0;
224}
Shawn Guoaf4872f2012-03-05 22:30:54 +0800225EXPORT_SYMBOL_GPL(imx_audmux_v1_configure_port);
Shawn Guo2405fc92012-03-05 22:30:51 +0800226
Shawn Guoaf4872f2012-03-05 22:30:54 +0800227int imx_audmux_v2_configure_port(unsigned int port, unsigned int ptcr,
Sascha Hauer9eedbdf2009-10-29 17:12:39 +0100228 unsigned int pdcr)
229{
Fabio Estevam72192362013-07-21 13:39:08 -0300230 int ret;
231
Richard Zhao3bc34a62012-03-05 22:30:52 +0800232 if (audmux_type != IMX31_AUDMUX)
233 return -EINVAL;
234
Sascha Hauer9eedbdf2009-10-29 17:12:39 +0100235 if (!audmux_base)
236 return -ENOSYS;
237
Fabio Estevam72192362013-07-21 13:39:08 -0300238 if (audmux_clk) {
239 ret = clk_prepare_enable(audmux_clk);
240 if (ret)
241 return ret;
242 }
Sascha Hauer9eedbdf2009-10-29 17:12:39 +0100243
Shawn Guoaf4872f2012-03-05 22:30:54 +0800244 writel(ptcr, audmux_base + IMX_AUDMUX_V2_PTCR(port));
245 writel(pdcr, audmux_base + IMX_AUDMUX_V2_PDCR(port));
Sascha Hauer9eedbdf2009-10-29 17:12:39 +0100246
247 if (audmux_clk)
Linus Torvalds34800592012-03-27 16:41:24 -0700248 clk_disable_unprepare(audmux_clk);
Sascha Hauer9eedbdf2009-10-29 17:12:39 +0100249
250 return 0;
251}
Shawn Guoaf4872f2012-03-05 22:30:54 +0800252EXPORT_SYMBOL_GPL(imx_audmux_v2_configure_port);
Sascha Hauer9eedbdf2009-10-29 17:12:39 +0100253
Markus Pargmann8548a462013-07-27 13:31:52 +0200254static int imx_audmux_parse_dt_defaults(struct platform_device *pdev,
255 struct device_node *of_node)
256{
257 struct device_node *child;
258
259 for_each_available_child_of_node(of_node, child) {
260 unsigned int port;
261 unsigned int ptcr = 0;
262 unsigned int pdcr = 0;
263 unsigned int pcr = 0;
264 unsigned int val;
265 int ret;
266 int i = 0;
267
268 ret = of_property_read_u32(child, "fsl,audmux-port", &port);
269 if (ret) {
270 dev_warn(&pdev->dev, "Failed to get fsl,audmux-port of child node \"%s\"\n",
271 child->full_name);
272 continue;
273 }
274 if (!of_property_read_bool(child, "fsl,port-config")) {
275 dev_warn(&pdev->dev, "child node \"%s\" does not have property fsl,port-config\n",
276 child->full_name);
277 continue;
278 }
279
280 for (i = 0; (ret = of_property_read_u32_index(child,
Markus Pargmann9c0aeaa2013-08-10 14:55:57 +0200281 "fsl,port-config", i, &val)) == 0;
Markus Pargmann8548a462013-07-27 13:31:52 +0200282 ++i) {
283 if (audmux_type == IMX31_AUDMUX) {
284 if (i % 2)
285 pdcr |= val;
286 else
287 ptcr |= val;
288 } else {
289 pcr |= val;
290 }
291 }
292
Markus Pargmann9c0aeaa2013-08-10 14:55:57 +0200293 if (ret != -EOVERFLOW) {
Markus Pargmann8548a462013-07-27 13:31:52 +0200294 dev_err(&pdev->dev, "Failed to read u32 at index %d of child %s\n",
295 i, child->full_name);
296 continue;
297 }
298
299 if (audmux_type == IMX31_AUDMUX) {
300 if (i % 2) {
301 dev_err(&pdev->dev, "One pdcr value is missing in child node %s\n",
302 child->full_name);
303 continue;
304 }
305 imx_audmux_v2_configure_port(port, ptcr, pdcr);
306 } else {
307 imx_audmux_v1_configure_port(port, pcr);
308 }
309 }
310
311 return 0;
312}
313
Bill Pembertona0a3d512012-12-07 09:26:16 -0500314static int imx_audmux_probe(struct platform_device *pdev)
Sascha Hauer9eedbdf2009-10-29 17:12:39 +0100315{
Richard Zhao3bc34a62012-03-05 22:30:52 +0800316 struct resource *res;
Richard Zhao9d5ef262012-03-05 22:31:04 +0800317 const struct of_device_id *of_id =
318 of_match_device(imx_audmux_dt_ids, &pdev->dev);
Richard Zhao3bc34a62012-03-05 22:30:52 +0800319
320 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Redingb25b5aa2013-01-21 11:09:26 +0100321 audmux_base = devm_ioremap_resource(&pdev->dev, res);
322 if (IS_ERR(audmux_base))
323 return PTR_ERR(audmux_base);
Richard Zhao3bc34a62012-03-05 22:30:52 +0800324
Fabio Estevam127c5ca2013-03-12 20:51:28 -0300325 audmux_clk = devm_clk_get(&pdev->dev, "audmux");
Richard Zhao3bc34a62012-03-05 22:30:52 +0800326 if (IS_ERR(audmux_clk)) {
327 dev_dbg(&pdev->dev, "cannot get clock: %ld\n",
328 PTR_ERR(audmux_clk));
329 audmux_clk = NULL;
Eric BĂ©nard8402ed32010-06-08 11:03:00 +0200330 }
Sascha Hauer56c6b872011-08-23 21:24:57 +0200331
Richard Zhao9d5ef262012-03-05 22:31:04 +0800332 if (of_id)
333 pdev->id_entry = of_id->data;
Richard Zhao3bc34a62012-03-05 22:30:52 +0800334 audmux_type = pdev->id_entry->driver_data;
335 if (audmux_type == IMX31_AUDMUX)
Shawn Guo2405fc92012-03-05 22:30:51 +0800336 audmux_debugfs_init();
Mark Brown7b4e08a2010-01-11 16:33:18 +0000337
Markus Pargmann8548a462013-07-27 13:31:52 +0200338 imx_audmux_parse_dt_defaults(pdev, pdev->dev.of_node);
339
Sascha Hauer9eedbdf2009-10-29 17:12:39 +0100340 return 0;
341}
342
Bill Pembertona0a3d512012-12-07 09:26:16 -0500343static int imx_audmux_remove(struct platform_device *pdev)
Richard Zhao3bc34a62012-03-05 22:30:52 +0800344{
345 if (audmux_type == IMX31_AUDMUX)
346 audmux_debugfs_remove();
Richard Zhao3bc34a62012-03-05 22:30:52 +0800347
348 return 0;
349}
350
351static struct platform_driver imx_audmux_driver = {
352 .probe = imx_audmux_probe,
Bill Pembertona0a3d512012-12-07 09:26:16 -0500353 .remove = imx_audmux_remove,
Richard Zhao3bc34a62012-03-05 22:30:52 +0800354 .id_table = imx_audmux_ids,
355 .driver = {
356 .name = DRIVER_NAME,
357 .owner = THIS_MODULE,
Richard Zhao9d5ef262012-03-05 22:31:04 +0800358 .of_match_table = imx_audmux_dt_ids,
Richard Zhao3bc34a62012-03-05 22:30:52 +0800359 }
360};
361
362static int __init imx_audmux_init(void)
363{
364 return platform_driver_register(&imx_audmux_driver);
365}
366subsys_initcall(imx_audmux_init);
367
368static void __exit imx_audmux_exit(void)
369{
370 platform_driver_unregister(&imx_audmux_driver);
371}
372module_exit(imx_audmux_exit);
373
374MODULE_DESCRIPTION("Freescale i.MX AUDMUX driver");
375MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
376MODULE_LICENSE("GPL v2");
377MODULE_ALIAS("platform:" DRIVER_NAME);