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 ret
= clk_prepare_enable(audmux_clk
);
82 ptcr
= readl(audmux_base
+ IMX_AUDMUX_V2_PTCR(port
));
83 pdcr
= readl(audmux_base
+ IMX_AUDMUX_V2_PDCR(port
));
86 clk_disable_unprepare(audmux_clk
);
88 ret
= snprintf(buf
, PAGE_SIZE
, "PDCR: %08x\nPTCR: %08x\n",
91 if (ptcr
& IMX_AUDMUX_V2_PTCR_TFSDIR
)
92 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
93 "TxFS output from %s, ",
94 audmux_port_string((ptcr
>> 27) & 0x7));
96 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
99 if (ptcr
& IMX_AUDMUX_V2_PTCR_TCLKDIR
)
100 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
101 "TxClk output from %s",
102 audmux_port_string((ptcr
>> 22) & 0x7));
104 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
107 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, "\n");
109 if (ptcr
& IMX_AUDMUX_V2_PTCR_SYN
) {
110 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
111 "Port is symmetric");
113 if (ptcr
& IMX_AUDMUX_V2_PTCR_RFSDIR
)
114 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
115 "RxFS output from %s, ",
116 audmux_port_string((ptcr
>> 17) & 0x7));
118 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
121 if (ptcr
& IMX_AUDMUX_V2_PTCR_RCLKDIR
)
122 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
123 "RxClk output from %s",
124 audmux_port_string((ptcr
>> 12) & 0x7));
126 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
130 ret
+= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
131 "\nData received from %s\n",
132 audmux_port_string((pdcr
>> 13) & 0x7));
134 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
141 static const struct file_operations audmux_debugfs_fops
= {
143 .read
= audmux_read_file
,
144 .llseek
= default_llseek
,
147 static void __init
audmux_debugfs_init(void)
152 audmux_debugfs_root
= debugfs_create_dir("audmux", NULL
);
153 if (!audmux_debugfs_root
) {
154 pr_warning("Failed to create AUDMUX debugfs root\n");
158 for (i
= 0; i
< MX31_AUDMUX_PORT7_SSI_PINS_7
+ 1; i
++) {
159 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",
167 static void audmux_debugfs_remove(void)
169 debugfs_remove_recursive(audmux_debugfs_root
);
172 static inline void audmux_debugfs_init(void)
176 static inline void audmux_debugfs_remove(void)
181 static enum imx_audmux_type
{
186 static struct platform_device_id imx_audmux_ids
[] = {
188 .name
= "imx21-audmux",
189 .driver_data
= IMX21_AUDMUX
,
191 .name
= "imx31-audmux",
192 .driver_data
= IMX31_AUDMUX
,
197 MODULE_DEVICE_TABLE(platform
, imx_audmux_ids
);
199 static 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], },
204 MODULE_DEVICE_TABLE(of
, imx_audmux_dt_ids
);
206 static const uint8_t port_mapping
[] = {
207 0x0, 0x4, 0x8, 0x10, 0x14, 0x1c,
210 int imx_audmux_v1_configure_port(unsigned int port
, unsigned int pcr
)
212 if (audmux_type
!= IMX21_AUDMUX
)
218 if (port
>= ARRAY_SIZE(port_mapping
))
221 writel(pcr
, audmux_base
+ port_mapping
[port
]);
225 EXPORT_SYMBOL_GPL(imx_audmux_v1_configure_port
);
227 int imx_audmux_v2_configure_port(unsigned int port
, unsigned int ptcr
,
232 if (audmux_type
!= IMX31_AUDMUX
)
239 ret
= clk_prepare_enable(audmux_clk
);
244 writel(ptcr
, audmux_base
+ IMX_AUDMUX_V2_PTCR(port
));
245 writel(pdcr
, audmux_base
+ IMX_AUDMUX_V2_PDCR(port
));
248 clk_disable_unprepare(audmux_clk
);
252 EXPORT_SYMBOL_GPL(imx_audmux_v2_configure_port
);
254 static int imx_audmux_parse_dt_defaults(struct platform_device
*pdev
,
255 struct device_node
*of_node
)
257 struct device_node
*child
;
259 for_each_available_child_of_node(of_node
, child
) {
261 unsigned int ptcr
= 0;
262 unsigned int pdcr
= 0;
263 unsigned int pcr
= 0;
268 ret
= of_property_read_u32(child
, "fsl,audmux-port", &port
);
270 dev_warn(&pdev
->dev
, "Failed to get fsl,audmux-port of child node \"%s\"\n",
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",
280 for (i
= 0; (ret
= of_property_read_u32_index(child
,
281 "fsl,port-config", i
, &val
)) == 0;
283 if (audmux_type
== IMX31_AUDMUX
) {
293 if (ret
!= -EOVERFLOW
) {
294 dev_err(&pdev
->dev
, "Failed to read u32 at index %d of child %s\n",
295 i
, child
->full_name
);
299 if (audmux_type
== IMX31_AUDMUX
) {
301 dev_err(&pdev
->dev
, "One pdcr value is missing in child node %s\n",
305 imx_audmux_v2_configure_port(port
, ptcr
, pdcr
);
307 imx_audmux_v1_configure_port(port
, pcr
);
314 static int imx_audmux_probe(struct platform_device
*pdev
)
316 struct resource
*res
;
317 const struct of_device_id
*of_id
=
318 of_match_device(imx_audmux_dt_ids
, &pdev
->dev
);
320 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
321 audmux_base
= devm_ioremap_resource(&pdev
->dev
, res
);
322 if (IS_ERR(audmux_base
))
323 return PTR_ERR(audmux_base
);
325 audmux_clk
= devm_clk_get(&pdev
->dev
, "audmux");
326 if (IS_ERR(audmux_clk
)) {
327 dev_dbg(&pdev
->dev
, "cannot get clock: %ld\n",
328 PTR_ERR(audmux_clk
));
333 pdev
->id_entry
= of_id
->data
;
334 audmux_type
= pdev
->id_entry
->driver_data
;
335 if (audmux_type
== IMX31_AUDMUX
)
336 audmux_debugfs_init();
339 imx_audmux_parse_dt_defaults(pdev
, pdev
->dev
.of_node
);
344 static int imx_audmux_remove(struct platform_device
*pdev
)
346 if (audmux_type
== IMX31_AUDMUX
)
347 audmux_debugfs_remove();
352 static struct platform_driver imx_audmux_driver
= {
353 .probe
= imx_audmux_probe
,
354 .remove
= imx_audmux_remove
,
355 .id_table
= imx_audmux_ids
,
358 .owner
= THIS_MODULE
,
359 .of_match_table
= imx_audmux_dt_ids
,
363 static int __init
imx_audmux_init(void)
365 return platform_driver_register(&imx_audmux_driver
);
367 subsys_initcall(imx_audmux_init
);
369 static void __exit
imx_audmux_exit(void)
371 platform_driver_unregister(&imx_audmux_driver
);
373 module_exit(imx_audmux_exit
);
375 MODULE_DESCRIPTION("Freescale i.MX AUDMUX driver");
376 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
377 MODULE_LICENSE("GPL v2");
378 MODULE_ALIAS("platform:" DRIVER_NAME
);