blob: 1765a197acb0ab14d0a54cda8751dd45fa7b71fb [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
76 if (audmux_clk)
Linus Torvalds34800592012-03-27 16:41:24 -070077 clk_prepare_enable(audmux_clk);
Mark Brown7b4e08a2010-01-11 16:33:18 +000078
Shawn Guoaf4872f2012-03-05 22:30:54 +080079 ptcr = readl(audmux_base + IMX_AUDMUX_V2_PTCR(port));
80 pdcr = readl(audmux_base + IMX_AUDMUX_V2_PDCR(port));
Mark Brown7b4e08a2010-01-11 16:33:18 +000081
82 if (audmux_clk)
Linus Torvalds34800592012-03-27 16:41:24 -070083 clk_disable_unprepare(audmux_clk);
Mark Brown7b4e08a2010-01-11 16:33:18 +000084
85 ret = snprintf(buf, PAGE_SIZE, "PDCR: %08x\nPTCR: %08x\n",
86 pdcr, ptcr);
87
Shawn Guoaf4872f2012-03-05 22:30:54 +080088 if (ptcr & IMX_AUDMUX_V2_PTCR_TFSDIR)
Mark Brown7b4e08a2010-01-11 16:33:18 +000089 ret += snprintf(buf + ret, PAGE_SIZE - ret,
90 "TxFS output from %s, ",
91 audmux_port_string((ptcr >> 27) & 0x7));
92 else
93 ret += snprintf(buf + ret, PAGE_SIZE - ret,
94 "TxFS input, ");
95
Shawn Guoaf4872f2012-03-05 22:30:54 +080096 if (ptcr & IMX_AUDMUX_V2_PTCR_TCLKDIR)
Mark Brown7b4e08a2010-01-11 16:33:18 +000097 ret += snprintf(buf + ret, PAGE_SIZE - ret,
98 "TxClk output from %s",
99 audmux_port_string((ptcr >> 22) & 0x7));
100 else
101 ret += snprintf(buf + ret, PAGE_SIZE - ret,
102 "TxClk input");
103
104 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
105
Shawn Guoaf4872f2012-03-05 22:30:54 +0800106 if (ptcr & IMX_AUDMUX_V2_PTCR_SYN) {
Mark Brown7b4e08a2010-01-11 16:33:18 +0000107 ret += snprintf(buf + ret, PAGE_SIZE - ret,
108 "Port is symmetric");
109 } else {
Shawn Guoaf4872f2012-03-05 22:30:54 +0800110 if (ptcr & IMX_AUDMUX_V2_PTCR_RFSDIR)
Mark Brown7b4e08a2010-01-11 16:33:18 +0000111 ret += snprintf(buf + ret, PAGE_SIZE - ret,
112 "RxFS output from %s, ",
113 audmux_port_string((ptcr >> 17) & 0x7));
114 else
115 ret += snprintf(buf + ret, PAGE_SIZE - ret,
116 "RxFS input, ");
117
Shawn Guoaf4872f2012-03-05 22:30:54 +0800118 if (ptcr & IMX_AUDMUX_V2_PTCR_RCLKDIR)
Mark Brown7b4e08a2010-01-11 16:33:18 +0000119 ret += snprintf(buf + ret, PAGE_SIZE - ret,
120 "RxClk output from %s",
121 audmux_port_string((ptcr >> 12) & 0x7));
122 else
123 ret += snprintf(buf + ret, PAGE_SIZE - ret,
124 "RxClk input");
125 }
126
127 ret += snprintf(buf + ret, PAGE_SIZE - ret,
128 "\nData received from %s\n",
129 audmux_port_string((pdcr >> 13) & 0x7));
130
131 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
132
133 kfree(buf);
134
135 return ret;
136}
137
138static const struct file_operations audmux_debugfs_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -0700139 .open = simple_open,
Mark Brown7b4e08a2010-01-11 16:33:18 +0000140 .read = audmux_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200141 .llseek = default_llseek,
Mark Brown7b4e08a2010-01-11 16:33:18 +0000142};
143
Richard Zhao3bc34a62012-03-05 22:30:52 +0800144static void __init audmux_debugfs_init(void)
Mark Brown7b4e08a2010-01-11 16:33:18 +0000145{
146 int i;
147 char buf[20];
148
149 audmux_debugfs_root = debugfs_create_dir("audmux", NULL);
150 if (!audmux_debugfs_root) {
151 pr_warning("Failed to create AUDMUX debugfs root\n");
152 return;
153 }
154
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",
160 i);
161 }
162}
Richard Zhao3bc34a62012-03-05 22:30:52 +0800163
Fabio Estevamfb97624a2012-03-07 15:19:57 -0300164static void __devexit audmux_debugfs_remove(void)
Richard Zhao3bc34a62012-03-05 22:30:52 +0800165{
166 debugfs_remove_recursive(audmux_debugfs_root);
167}
Mark Brown7b4e08a2010-01-11 16:33:18 +0000168#else
169static inline void audmux_debugfs_init(void)
170{
171}
Richard Zhao3bc34a62012-03-05 22:30:52 +0800172
173static inline void audmux_debugfs_remove(void)
174{
175}
Mark Brown7b4e08a2010-01-11 16:33:18 +0000176#endif
177
Richard Zhao3bc34a62012-03-05 22:30:52 +0800178enum imx_audmux_type {
179 IMX21_AUDMUX,
180 IMX31_AUDMUX,
181} audmux_type;
182
183static struct platform_device_id imx_audmux_ids[] = {
184 {
185 .name = "imx21-audmux",
186 .driver_data = IMX21_AUDMUX,
187 }, {
188 .name = "imx31-audmux",
189 .driver_data = IMX31_AUDMUX,
190 }, {
191 /* sentinel */
192 }
193};
194MODULE_DEVICE_TABLE(platform, imx_audmux_ids);
195
Richard Zhao9d5ef262012-03-05 22:31:04 +0800196static 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], },
199 { /* sentinel */ }
200};
201MODULE_DEVICE_TABLE(of, imx_audmux_dt_ids);
202
Shawn Guo2405fc92012-03-05 22:30:51 +0800203static const uint8_t port_mapping[] = {
204 0x0, 0x4, 0x8, 0x10, 0x14, 0x1c,
205};
206
Shawn Guoaf4872f2012-03-05 22:30:54 +0800207int imx_audmux_v1_configure_port(unsigned int port, unsigned int pcr)
Shawn Guo2405fc92012-03-05 22:30:51 +0800208{
Richard Zhao3bc34a62012-03-05 22:30:52 +0800209 if (audmux_type != IMX21_AUDMUX)
210 return -EINVAL;
211
Shawn Guo2405fc92012-03-05 22:30:51 +0800212 if (!audmux_base)
213 return -ENOSYS;
214
215 if (port >= ARRAY_SIZE(port_mapping))
216 return -EINVAL;
217
218 writel(pcr, audmux_base + port_mapping[port]);
219
220 return 0;
221}
Shawn Guoaf4872f2012-03-05 22:30:54 +0800222EXPORT_SYMBOL_GPL(imx_audmux_v1_configure_port);
Shawn Guo2405fc92012-03-05 22:30:51 +0800223
Shawn Guoaf4872f2012-03-05 22:30:54 +0800224int imx_audmux_v2_configure_port(unsigned int port, unsigned int ptcr,
Sascha Hauer9eedbdf2009-10-29 17:12:39 +0100225 unsigned int pdcr)
226{
Richard Zhao3bc34a62012-03-05 22:30:52 +0800227 if (audmux_type != IMX31_AUDMUX)
228 return -EINVAL;
229
Sascha Hauer9eedbdf2009-10-29 17:12:39 +0100230 if (!audmux_base)
231 return -ENOSYS;
232
233 if (audmux_clk)
Linus Torvalds34800592012-03-27 16:41:24 -0700234 clk_prepare_enable(audmux_clk);
Sascha Hauer9eedbdf2009-10-29 17:12:39 +0100235
Shawn Guoaf4872f2012-03-05 22:30:54 +0800236 writel(ptcr, audmux_base + IMX_AUDMUX_V2_PTCR(port));
237 writel(pdcr, audmux_base + IMX_AUDMUX_V2_PDCR(port));
Sascha Hauer9eedbdf2009-10-29 17:12:39 +0100238
239 if (audmux_clk)
Linus Torvalds34800592012-03-27 16:41:24 -0700240 clk_disable_unprepare(audmux_clk);
Sascha Hauer9eedbdf2009-10-29 17:12:39 +0100241
242 return 0;
243}
Shawn Guoaf4872f2012-03-05 22:30:54 +0800244EXPORT_SYMBOL_GPL(imx_audmux_v2_configure_port);
Sascha Hauer9eedbdf2009-10-29 17:12:39 +0100245
Fabio Estevamfb97624a2012-03-07 15:19:57 -0300246static int __devinit imx_audmux_probe(struct platform_device *pdev)
Sascha Hauer9eedbdf2009-10-29 17:12:39 +0100247{
Richard Zhao3bc34a62012-03-05 22:30:52 +0800248 struct resource *res;
Richard Zhao9d5ef262012-03-05 22:31:04 +0800249 const struct of_device_id *of_id =
250 of_match_device(imx_audmux_dt_ids, &pdev->dev);
Richard Zhao3bc34a62012-03-05 22:30:52 +0800251
252 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
253 audmux_base = devm_request_and_ioremap(&pdev->dev, res);
254 if (!audmux_base)
255 return -EADDRNOTAVAIL;
256
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));
261 audmux_clk = NULL;
Eric BĂ©nard8402ed32010-06-08 11:03:00 +0200262 }
Sascha Hauer56c6b872011-08-23 21:24:57 +0200263
Richard Zhao9d5ef262012-03-05 22:31:04 +0800264 if (of_id)
265 pdev->id_entry = of_id->data;
Richard Zhao3bc34a62012-03-05 22:30:52 +0800266 audmux_type = pdev->id_entry->driver_data;
267 if (audmux_type == IMX31_AUDMUX)
Shawn Guo2405fc92012-03-05 22:30:51 +0800268 audmux_debugfs_init();
Mark Brown7b4e08a2010-01-11 16:33:18 +0000269
Sascha Hauer9eedbdf2009-10-29 17:12:39 +0100270 return 0;
271}
272
Fabio Estevamfb97624a2012-03-07 15:19:57 -0300273static int __devexit imx_audmux_remove(struct platform_device *pdev)
Richard Zhao3bc34a62012-03-05 22:30:52 +0800274{
275 if (audmux_type == IMX31_AUDMUX)
276 audmux_debugfs_remove();
277 clk_put(audmux_clk);
278
279 return 0;
280}
281
282static struct platform_driver imx_audmux_driver = {
283 .probe = imx_audmux_probe,
Fabio Estevamfb97624a2012-03-07 15:19:57 -0300284 .remove = __devexit_p(imx_audmux_remove),
Richard Zhao3bc34a62012-03-05 22:30:52 +0800285 .id_table = imx_audmux_ids,
286 .driver = {
287 .name = DRIVER_NAME,
288 .owner = THIS_MODULE,
Richard Zhao9d5ef262012-03-05 22:31:04 +0800289 .of_match_table = imx_audmux_dt_ids,
Richard Zhao3bc34a62012-03-05 22:30:52 +0800290 }
291};
292
293static int __init imx_audmux_init(void)
294{
295 return platform_driver_register(&imx_audmux_driver);
296}
297subsys_initcall(imx_audmux_init);
298
299static void __exit imx_audmux_exit(void)
300{
301 platform_driver_unregister(&imx_audmux_driver);
302}
303module_exit(imx_audmux_exit);
304
305MODULE_DESCRIPTION("Freescale i.MX AUDMUX driver");
306MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
307MODULE_LICENSE("GPL v2");
308MODULE_ALIAS("platform:" DRIVER_NAME);