blob: 0fe66c3dde1225fcabb51ef58d227bda21b76bc2 [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
43static int audmux_open_file(struct inode *inode, struct file *file)
44{
45 file->private_data = inode->i_private;
46 return 0;
47}
48
49/* There is an annoying discontinuity in the SSI numbering with regard
50 * to the Linux number of the devices */
51static const char *audmux_port_string(int port)
52{
53 switch (port) {
54 case MX31_AUDMUX_PORT1_SSI0:
55 return "imx-ssi.0";
56 case MX31_AUDMUX_PORT2_SSI1:
57 return "imx-ssi.1";
58 case MX31_AUDMUX_PORT3_SSI_PINS_3:
59 return "SSI3";
60 case MX31_AUDMUX_PORT4_SSI_PINS_4:
61 return "SSI4";
62 case MX31_AUDMUX_PORT5_SSI_PINS_5:
63 return "SSI5";
64 case MX31_AUDMUX_PORT6_SSI_PINS_6:
65 return "SSI6";
66 default:
67 return "UNKNOWN";
68 }
69}
70
71static ssize_t audmux_read_file(struct file *file, char __user *user_buf,
72 size_t count, loff_t *ppos)
73{
74 ssize_t ret;
75 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
76 int port = (int)file->private_data;
77 u32 pdcr, ptcr;
78
79 if (!buf)
80 return -ENOMEM;
81
Fabio Estevam66bb2a72012-04-05 10:57:51 -030082 if (!audmux_base)
83 return -ENOSYS;
84
Mark Brown7b4e08a2010-01-11 16:33:18 +000085 if (audmux_clk)
Linus Torvalds34800592012-03-27 16:41:24 -070086 clk_prepare_enable(audmux_clk);
Mark Brown7b4e08a2010-01-11 16:33:18 +000087
Shawn Guoaf4872f2012-03-05 22:30:54 +080088 ptcr = readl(audmux_base + IMX_AUDMUX_V2_PTCR(port));
89 pdcr = readl(audmux_base + IMX_AUDMUX_V2_PDCR(port));
Mark Brown7b4e08a2010-01-11 16:33:18 +000090
91 if (audmux_clk)
Linus Torvalds34800592012-03-27 16:41:24 -070092 clk_disable_unprepare(audmux_clk);
Mark Brown7b4e08a2010-01-11 16:33:18 +000093
94 ret = snprintf(buf, PAGE_SIZE, "PDCR: %08x\nPTCR: %08x\n",
95 pdcr, ptcr);
96
Shawn Guoaf4872f2012-03-05 22:30:54 +080097 if (ptcr & IMX_AUDMUX_V2_PTCR_TFSDIR)
Mark Brown7b4e08a2010-01-11 16:33:18 +000098 ret += snprintf(buf + ret, PAGE_SIZE - ret,
99 "TxFS output from %s, ",
100 audmux_port_string((ptcr >> 27) & 0x7));
101 else
102 ret += snprintf(buf + ret, PAGE_SIZE - ret,
103 "TxFS input, ");
104
Shawn Guoaf4872f2012-03-05 22:30:54 +0800105 if (ptcr & IMX_AUDMUX_V2_PTCR_TCLKDIR)
Mark Brown7b4e08a2010-01-11 16:33:18 +0000106 ret += snprintf(buf + ret, PAGE_SIZE - ret,
107 "TxClk output from %s",
108 audmux_port_string((ptcr >> 22) & 0x7));
109 else
110 ret += snprintf(buf + ret, PAGE_SIZE - ret,
111 "TxClk input");
112
113 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
114
Shawn Guoaf4872f2012-03-05 22:30:54 +0800115 if (ptcr & IMX_AUDMUX_V2_PTCR_SYN) {
Mark Brown7b4e08a2010-01-11 16:33:18 +0000116 ret += snprintf(buf + ret, PAGE_SIZE - ret,
117 "Port is symmetric");
118 } else {
Shawn Guoaf4872f2012-03-05 22:30:54 +0800119 if (ptcr & IMX_AUDMUX_V2_PTCR_RFSDIR)
Mark Brown7b4e08a2010-01-11 16:33:18 +0000120 ret += snprintf(buf + ret, PAGE_SIZE - ret,
121 "RxFS output from %s, ",
122 audmux_port_string((ptcr >> 17) & 0x7));
123 else
124 ret += snprintf(buf + ret, PAGE_SIZE - ret,
125 "RxFS input, ");
126
Shawn Guoaf4872f2012-03-05 22:30:54 +0800127 if (ptcr & IMX_AUDMUX_V2_PTCR_RCLKDIR)
Mark Brown7b4e08a2010-01-11 16:33:18 +0000128 ret += snprintf(buf + ret, PAGE_SIZE - ret,
129 "RxClk output from %s",
130 audmux_port_string((ptcr >> 12) & 0x7));
131 else
132 ret += snprintf(buf + ret, PAGE_SIZE - ret,
133 "RxClk input");
134 }
135
136 ret += snprintf(buf + ret, PAGE_SIZE - ret,
137 "\nData received from %s\n",
138 audmux_port_string((pdcr >> 13) & 0x7));
139
140 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
141
142 kfree(buf);
143
144 return ret;
145}
146
147static const struct file_operations audmux_debugfs_fops = {
148 .open = audmux_open_file,
149 .read = audmux_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200150 .llseek = default_llseek,
Mark Brown7b4e08a2010-01-11 16:33:18 +0000151};
152
Richard Zhao3bc34a62012-03-05 22:30:52 +0800153static void __init audmux_debugfs_init(void)
Mark Brown7b4e08a2010-01-11 16:33:18 +0000154{
155 int i;
156 char buf[20];
157
158 audmux_debugfs_root = debugfs_create_dir("audmux", NULL);
159 if (!audmux_debugfs_root) {
160 pr_warning("Failed to create AUDMUX debugfs root\n");
161 return;
162 }
163
Fabio Estevam00792ac2012-04-05 09:45:51 -0300164 for (i = 0; i < MX31_AUDMUX_PORT6_SSI_PINS_6 + 1; i++) {
Mark Brown7b4e08a2010-01-11 16:33:18 +0000165 snprintf(buf, sizeof(buf), "ssi%d", i);
166 if (!debugfs_create_file(buf, 0444, audmux_debugfs_root,
167 (void *)i, &audmux_debugfs_fops))
168 pr_warning("Failed to create AUDMUX port %d debugfs file\n",
169 i);
170 }
171}
Richard Zhao3bc34a62012-03-05 22:30:52 +0800172
Fabio Estevamfb97624a2012-03-07 15:19:57 -0300173static void __devexit audmux_debugfs_remove(void)
Richard Zhao3bc34a62012-03-05 22:30:52 +0800174{
175 debugfs_remove_recursive(audmux_debugfs_root);
176}
Mark Brown7b4e08a2010-01-11 16:33:18 +0000177#else
178static inline void audmux_debugfs_init(void)
179{
180}
Richard Zhao3bc34a62012-03-05 22:30:52 +0800181
182static inline void audmux_debugfs_remove(void)
183{
184}
Mark Brown7b4e08a2010-01-11 16:33:18 +0000185#endif
186
Richard Zhao3bc34a62012-03-05 22:30:52 +0800187enum imx_audmux_type {
188 IMX21_AUDMUX,
189 IMX31_AUDMUX,
190} audmux_type;
191
192static struct platform_device_id imx_audmux_ids[] = {
193 {
194 .name = "imx21-audmux",
195 .driver_data = IMX21_AUDMUX,
196 }, {
197 .name = "imx31-audmux",
198 .driver_data = IMX31_AUDMUX,
199 }, {
200 /* sentinel */
201 }
202};
203MODULE_DEVICE_TABLE(platform, imx_audmux_ids);
204
Richard Zhao9d5ef262012-03-05 22:31:04 +0800205static const struct of_device_id imx_audmux_dt_ids[] = {
206 { .compatible = "fsl,imx21-audmux", .data = &imx_audmux_ids[0], },
207 { .compatible = "fsl,imx31-audmux", .data = &imx_audmux_ids[1], },
208 { /* sentinel */ }
209};
210MODULE_DEVICE_TABLE(of, imx_audmux_dt_ids);
211
Shawn Guo2405fc92012-03-05 22:30:51 +0800212static const uint8_t port_mapping[] = {
213 0x0, 0x4, 0x8, 0x10, 0x14, 0x1c,
214};
215
Shawn Guoaf4872f2012-03-05 22:30:54 +0800216int imx_audmux_v1_configure_port(unsigned int port, unsigned int pcr)
Shawn Guo2405fc92012-03-05 22:30:51 +0800217{
Richard Zhao3bc34a62012-03-05 22:30:52 +0800218 if (audmux_type != IMX21_AUDMUX)
219 return -EINVAL;
220
Shawn Guo2405fc92012-03-05 22:30:51 +0800221 if (!audmux_base)
222 return -ENOSYS;
223
224 if (port >= ARRAY_SIZE(port_mapping))
225 return -EINVAL;
226
227 writel(pcr, audmux_base + port_mapping[port]);
228
229 return 0;
230}
Shawn Guoaf4872f2012-03-05 22:30:54 +0800231EXPORT_SYMBOL_GPL(imx_audmux_v1_configure_port);
Shawn Guo2405fc92012-03-05 22:30:51 +0800232
Shawn Guoaf4872f2012-03-05 22:30:54 +0800233int imx_audmux_v2_configure_port(unsigned int port, unsigned int ptcr,
Sascha Hauer9eedbdf2009-10-29 17:12:39 +0100234 unsigned int pdcr)
235{
Richard Zhao3bc34a62012-03-05 22:30:52 +0800236 if (audmux_type != IMX31_AUDMUX)
237 return -EINVAL;
238
Sascha Hauer9eedbdf2009-10-29 17:12:39 +0100239 if (!audmux_base)
240 return -ENOSYS;
241
242 if (audmux_clk)
Linus Torvalds34800592012-03-27 16:41:24 -0700243 clk_prepare_enable(audmux_clk);
Sascha Hauer9eedbdf2009-10-29 17:12:39 +0100244
Shawn Guoaf4872f2012-03-05 22:30:54 +0800245 writel(ptcr, audmux_base + IMX_AUDMUX_V2_PTCR(port));
246 writel(pdcr, audmux_base + IMX_AUDMUX_V2_PDCR(port));
Sascha Hauer9eedbdf2009-10-29 17:12:39 +0100247
248 if (audmux_clk)
Linus Torvalds34800592012-03-27 16:41:24 -0700249 clk_disable_unprepare(audmux_clk);
Sascha Hauer9eedbdf2009-10-29 17:12:39 +0100250
251 return 0;
252}
Shawn Guoaf4872f2012-03-05 22:30:54 +0800253EXPORT_SYMBOL_GPL(imx_audmux_v2_configure_port);
Sascha Hauer9eedbdf2009-10-29 17:12:39 +0100254
Fabio Estevamfb97624a2012-03-07 15:19:57 -0300255static int __devinit imx_audmux_probe(struct platform_device *pdev)
Sascha Hauer9eedbdf2009-10-29 17:12:39 +0100256{
Richard Zhao3bc34a62012-03-05 22:30:52 +0800257 struct resource *res;
Richard Zhao9d5ef262012-03-05 22:31:04 +0800258 const struct of_device_id *of_id =
259 of_match_device(imx_audmux_dt_ids, &pdev->dev);
Richard Zhao3bc34a62012-03-05 22:30:52 +0800260
261 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
262 audmux_base = devm_request_and_ioremap(&pdev->dev, res);
263 if (!audmux_base)
264 return -EADDRNOTAVAIL;
265
266 audmux_clk = clk_get(&pdev->dev, "audmux");
267 if (IS_ERR(audmux_clk)) {
268 dev_dbg(&pdev->dev, "cannot get clock: %ld\n",
269 PTR_ERR(audmux_clk));
270 audmux_clk = NULL;
Eric Bénard8402ed32010-06-08 11:03:00 +0200271 }
Sascha Hauer56c6b872011-08-23 21:24:57 +0200272
Richard Zhao9d5ef262012-03-05 22:31:04 +0800273 if (of_id)
274 pdev->id_entry = of_id->data;
Richard Zhao3bc34a62012-03-05 22:30:52 +0800275 audmux_type = pdev->id_entry->driver_data;
276 if (audmux_type == IMX31_AUDMUX)
Shawn Guo2405fc92012-03-05 22:30:51 +0800277 audmux_debugfs_init();
Mark Brown7b4e08a2010-01-11 16:33:18 +0000278
Sascha Hauer9eedbdf2009-10-29 17:12:39 +0100279 return 0;
280}
281
Fabio Estevamfb97624a2012-03-07 15:19:57 -0300282static int __devexit imx_audmux_remove(struct platform_device *pdev)
Richard Zhao3bc34a62012-03-05 22:30:52 +0800283{
284 if (audmux_type == IMX31_AUDMUX)
285 audmux_debugfs_remove();
286 clk_put(audmux_clk);
287
288 return 0;
289}
290
291static struct platform_driver imx_audmux_driver = {
292 .probe = imx_audmux_probe,
Fabio Estevamfb97624a2012-03-07 15:19:57 -0300293 .remove = __devexit_p(imx_audmux_remove),
Richard Zhao3bc34a62012-03-05 22:30:52 +0800294 .id_table = imx_audmux_ids,
295 .driver = {
296 .name = DRIVER_NAME,
297 .owner = THIS_MODULE,
Richard Zhao9d5ef262012-03-05 22:31:04 +0800298 .of_match_table = imx_audmux_dt_ids,
Richard Zhao3bc34a62012-03-05 22:30:52 +0800299 }
300};
301
302static int __init imx_audmux_init(void)
303{
304 return platform_driver_register(&imx_audmux_driver);
305}
306subsys_initcall(imx_audmux_init);
307
308static void __exit imx_audmux_exit(void)
309{
310 platform_driver_unregister(&imx_audmux_driver);
311}
312module_exit(imx_audmux_exit);
313
314MODULE_DESCRIPTION("Freescale i.MX AUDMUX driver");
315MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
316MODULE_LICENSE("GPL v2");
317MODULE_ALIAS("platform:" DRIVER_NAME);