Sascha Hauer | 9eedbdf | 2009-10-29 17:12:39 +0100 | [diff] [blame] | 1 | /* |
Richard Zhao | 3bc34a6 | 2012-03-05 22:30:52 +0800 | [diff] [blame] | 2 | * Copyright 2012 Freescale Semiconductor, Inc. |
| 3 | * Copyright 2012 Linaro Ltd. |
Sascha Hauer | 9eedbdf | 2009-10-29 17:12:39 +0100 | [diff] [blame] | 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. |
Sascha Hauer | 9eedbdf | 2009-10-29 17:12:39 +0100 | [diff] [blame] | 18 | */ |
| 19 | |
Sascha Hauer | 9eedbdf | 2009-10-29 17:12:39 +0100 | [diff] [blame] | 20 | #include <linux/clk.h> |
Mark Brown | 7b4e08a | 2010-01-11 16:33:18 +0000 | [diff] [blame] | 21 | #include <linux/debugfs.h> |
Richard Zhao | 3bc34a6 | 2012-03-05 22:30:52 +0800 | [diff] [blame] | 22 | #include <linux/err.h> |
| 23 | #include <linux/io.h> |
| 24 | #include <linux/module.h> |
Richard Zhao | 9d5ef26 | 2012-03-05 22:31:04 +0800 | [diff] [blame] | 25 | #include <linux/of.h> |
| 26 | #include <linux/of_device.h> |
Richard Zhao | 3bc34a6 | 2012-03-05 22:30:52 +0800 | [diff] [blame] | 27 | #include <linux/platform_device.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 28 | #include <linux/slab.h> |
Shawn Guo | 3c77c29c | 2012-03-05 22:30:53 +0800 | [diff] [blame] | 29 | |
| 30 | #include "imx-audmux.h" |
Richard Zhao | 3bc34a6 | 2012-03-05 22:30:52 +0800 | [diff] [blame] | 31 | |
| 32 | #define DRIVER_NAME "imx-audmux" |
Sascha Hauer | 9eedbdf | 2009-10-29 17:12:39 +0100 | [diff] [blame] | 33 | |
| 34 | static struct clk *audmux_clk; |
| 35 | static void __iomem *audmux_base; |
| 36 | |
Shawn Guo | af4872f | 2012-03-05 22:30:54 +0800 | [diff] [blame] | 37 | #define IMX_AUDMUX_V2_PTCR(x) ((x) * 8) |
| 38 | #define IMX_AUDMUX_V2_PDCR(x) ((x) * 8 + 4) |
Sascha Hauer | 9eedbdf | 2009-10-29 17:12:39 +0100 | [diff] [blame] | 39 | |
Mark Brown | 7b4e08a | 2010-01-11 16:33:18 +0000 | [diff] [blame] | 40 | #ifdef CONFIG_DEBUG_FS |
| 41 | static struct dentry *audmux_debugfs_root; |
| 42 | |
| 43 | static 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 */ |
| 51 | static 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 | |
| 71 | static 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 | |
| 82 | if (audmux_clk) |
| 83 | clk_enable(audmux_clk); |
| 84 | |
Shawn Guo | af4872f | 2012-03-05 22:30:54 +0800 | [diff] [blame] | 85 | ptcr = readl(audmux_base + IMX_AUDMUX_V2_PTCR(port)); |
| 86 | pdcr = readl(audmux_base + IMX_AUDMUX_V2_PDCR(port)); |
Mark Brown | 7b4e08a | 2010-01-11 16:33:18 +0000 | [diff] [blame] | 87 | |
| 88 | if (audmux_clk) |
| 89 | clk_disable(audmux_clk); |
| 90 | |
| 91 | ret = snprintf(buf, PAGE_SIZE, "PDCR: %08x\nPTCR: %08x\n", |
| 92 | pdcr, ptcr); |
| 93 | |
Shawn Guo | af4872f | 2012-03-05 22:30:54 +0800 | [diff] [blame] | 94 | if (ptcr & IMX_AUDMUX_V2_PTCR_TFSDIR) |
Mark Brown | 7b4e08a | 2010-01-11 16:33:18 +0000 | [diff] [blame] | 95 | ret += snprintf(buf + ret, PAGE_SIZE - ret, |
| 96 | "TxFS output from %s, ", |
| 97 | audmux_port_string((ptcr >> 27) & 0x7)); |
| 98 | else |
| 99 | ret += snprintf(buf + ret, PAGE_SIZE - ret, |
| 100 | "TxFS input, "); |
| 101 | |
Shawn Guo | af4872f | 2012-03-05 22:30:54 +0800 | [diff] [blame] | 102 | if (ptcr & IMX_AUDMUX_V2_PTCR_TCLKDIR) |
Mark Brown | 7b4e08a | 2010-01-11 16:33:18 +0000 | [diff] [blame] | 103 | ret += snprintf(buf + ret, PAGE_SIZE - ret, |
| 104 | "TxClk output from %s", |
| 105 | audmux_port_string((ptcr >> 22) & 0x7)); |
| 106 | else |
| 107 | ret += snprintf(buf + ret, PAGE_SIZE - ret, |
| 108 | "TxClk input"); |
| 109 | |
| 110 | ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n"); |
| 111 | |
Shawn Guo | af4872f | 2012-03-05 22:30:54 +0800 | [diff] [blame] | 112 | if (ptcr & IMX_AUDMUX_V2_PTCR_SYN) { |
Mark Brown | 7b4e08a | 2010-01-11 16:33:18 +0000 | [diff] [blame] | 113 | ret += snprintf(buf + ret, PAGE_SIZE - ret, |
| 114 | "Port is symmetric"); |
| 115 | } else { |
Shawn Guo | af4872f | 2012-03-05 22:30:54 +0800 | [diff] [blame] | 116 | if (ptcr & IMX_AUDMUX_V2_PTCR_RFSDIR) |
Mark Brown | 7b4e08a | 2010-01-11 16:33:18 +0000 | [diff] [blame] | 117 | ret += snprintf(buf + ret, PAGE_SIZE - ret, |
| 118 | "RxFS output from %s, ", |
| 119 | audmux_port_string((ptcr >> 17) & 0x7)); |
| 120 | else |
| 121 | ret += snprintf(buf + ret, PAGE_SIZE - ret, |
| 122 | "RxFS input, "); |
| 123 | |
Shawn Guo | af4872f | 2012-03-05 22:30:54 +0800 | [diff] [blame] | 124 | if (ptcr & IMX_AUDMUX_V2_PTCR_RCLKDIR) |
Mark Brown | 7b4e08a | 2010-01-11 16:33:18 +0000 | [diff] [blame] | 125 | ret += snprintf(buf + ret, PAGE_SIZE - ret, |
| 126 | "RxClk output from %s", |
| 127 | audmux_port_string((ptcr >> 12) & 0x7)); |
| 128 | else |
| 129 | ret += snprintf(buf + ret, PAGE_SIZE - ret, |
| 130 | "RxClk input"); |
| 131 | } |
| 132 | |
| 133 | ret += snprintf(buf + ret, PAGE_SIZE - ret, |
| 134 | "\nData received from %s\n", |
| 135 | audmux_port_string((pdcr >> 13) & 0x7)); |
| 136 | |
| 137 | ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); |
| 138 | |
| 139 | kfree(buf); |
| 140 | |
| 141 | return ret; |
| 142 | } |
| 143 | |
| 144 | static const struct file_operations audmux_debugfs_fops = { |
| 145 | .open = audmux_open_file, |
| 146 | .read = audmux_read_file, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 147 | .llseek = default_llseek, |
Mark Brown | 7b4e08a | 2010-01-11 16:33:18 +0000 | [diff] [blame] | 148 | }; |
| 149 | |
Richard Zhao | 3bc34a6 | 2012-03-05 22:30:52 +0800 | [diff] [blame] | 150 | static void __init audmux_debugfs_init(void) |
Mark Brown | 7b4e08a | 2010-01-11 16:33:18 +0000 | [diff] [blame] | 151 | { |
| 152 | int i; |
| 153 | char buf[20]; |
| 154 | |
| 155 | audmux_debugfs_root = debugfs_create_dir("audmux", NULL); |
| 156 | if (!audmux_debugfs_root) { |
| 157 | pr_warning("Failed to create AUDMUX debugfs root\n"); |
| 158 | return; |
| 159 | } |
| 160 | |
| 161 | for (i = 1; i < 8; i++) { |
| 162 | snprintf(buf, sizeof(buf), "ssi%d", i); |
| 163 | if (!debugfs_create_file(buf, 0444, audmux_debugfs_root, |
| 164 | (void *)i, &audmux_debugfs_fops)) |
| 165 | pr_warning("Failed to create AUDMUX port %d debugfs file\n", |
| 166 | i); |
| 167 | } |
| 168 | } |
Richard Zhao | 3bc34a6 | 2012-03-05 22:30:52 +0800 | [diff] [blame] | 169 | |
| 170 | static void __exit audmux_debugfs_remove(void) |
| 171 | { |
| 172 | debugfs_remove_recursive(audmux_debugfs_root); |
| 173 | } |
Mark Brown | 7b4e08a | 2010-01-11 16:33:18 +0000 | [diff] [blame] | 174 | #else |
| 175 | static inline void audmux_debugfs_init(void) |
| 176 | { |
| 177 | } |
Richard Zhao | 3bc34a6 | 2012-03-05 22:30:52 +0800 | [diff] [blame] | 178 | |
| 179 | static inline void audmux_debugfs_remove(void) |
| 180 | { |
| 181 | } |
Mark Brown | 7b4e08a | 2010-01-11 16:33:18 +0000 | [diff] [blame] | 182 | #endif |
| 183 | |
Richard Zhao | 3bc34a6 | 2012-03-05 22:30:52 +0800 | [diff] [blame] | 184 | enum imx_audmux_type { |
| 185 | IMX21_AUDMUX, |
| 186 | IMX31_AUDMUX, |
| 187 | } audmux_type; |
| 188 | |
| 189 | static struct platform_device_id imx_audmux_ids[] = { |
| 190 | { |
| 191 | .name = "imx21-audmux", |
| 192 | .driver_data = IMX21_AUDMUX, |
| 193 | }, { |
| 194 | .name = "imx31-audmux", |
| 195 | .driver_data = IMX31_AUDMUX, |
| 196 | }, { |
| 197 | /* sentinel */ |
| 198 | } |
| 199 | }; |
| 200 | MODULE_DEVICE_TABLE(platform, imx_audmux_ids); |
| 201 | |
Richard Zhao | 9d5ef26 | 2012-03-05 22:31:04 +0800 | [diff] [blame] | 202 | static const struct of_device_id imx_audmux_dt_ids[] = { |
| 203 | { .compatible = "fsl,imx21-audmux", .data = &imx_audmux_ids[0], }, |
| 204 | { .compatible = "fsl,imx31-audmux", .data = &imx_audmux_ids[1], }, |
| 205 | { /* sentinel */ } |
| 206 | }; |
| 207 | MODULE_DEVICE_TABLE(of, imx_audmux_dt_ids); |
| 208 | |
Shawn Guo | 2405fc9 | 2012-03-05 22:30:51 +0800 | [diff] [blame] | 209 | static const uint8_t port_mapping[] = { |
| 210 | 0x0, 0x4, 0x8, 0x10, 0x14, 0x1c, |
| 211 | }; |
| 212 | |
Shawn Guo | af4872f | 2012-03-05 22:30:54 +0800 | [diff] [blame] | 213 | int imx_audmux_v1_configure_port(unsigned int port, unsigned int pcr) |
Shawn Guo | 2405fc9 | 2012-03-05 22:30:51 +0800 | [diff] [blame] | 214 | { |
Richard Zhao | 3bc34a6 | 2012-03-05 22:30:52 +0800 | [diff] [blame] | 215 | if (audmux_type != IMX21_AUDMUX) |
| 216 | return -EINVAL; |
| 217 | |
Shawn Guo | 2405fc9 | 2012-03-05 22:30:51 +0800 | [diff] [blame] | 218 | if (!audmux_base) |
| 219 | return -ENOSYS; |
| 220 | |
| 221 | if (port >= ARRAY_SIZE(port_mapping)) |
| 222 | return -EINVAL; |
| 223 | |
| 224 | writel(pcr, audmux_base + port_mapping[port]); |
| 225 | |
| 226 | return 0; |
| 227 | } |
Shawn Guo | af4872f | 2012-03-05 22:30:54 +0800 | [diff] [blame] | 228 | EXPORT_SYMBOL_GPL(imx_audmux_v1_configure_port); |
Shawn Guo | 2405fc9 | 2012-03-05 22:30:51 +0800 | [diff] [blame] | 229 | |
Shawn Guo | af4872f | 2012-03-05 22:30:54 +0800 | [diff] [blame] | 230 | int imx_audmux_v2_configure_port(unsigned int port, unsigned int ptcr, |
Sascha Hauer | 9eedbdf | 2009-10-29 17:12:39 +0100 | [diff] [blame] | 231 | unsigned int pdcr) |
| 232 | { |
Richard Zhao | 3bc34a6 | 2012-03-05 22:30:52 +0800 | [diff] [blame] | 233 | if (audmux_type != IMX31_AUDMUX) |
| 234 | return -EINVAL; |
| 235 | |
Sascha Hauer | 9eedbdf | 2009-10-29 17:12:39 +0100 | [diff] [blame] | 236 | if (!audmux_base) |
| 237 | return -ENOSYS; |
| 238 | |
| 239 | if (audmux_clk) |
| 240 | clk_enable(audmux_clk); |
| 241 | |
Shawn Guo | af4872f | 2012-03-05 22:30:54 +0800 | [diff] [blame] | 242 | writel(ptcr, audmux_base + IMX_AUDMUX_V2_PTCR(port)); |
| 243 | writel(pdcr, audmux_base + IMX_AUDMUX_V2_PDCR(port)); |
Sascha Hauer | 9eedbdf | 2009-10-29 17:12:39 +0100 | [diff] [blame] | 244 | |
| 245 | if (audmux_clk) |
| 246 | clk_disable(audmux_clk); |
| 247 | |
| 248 | return 0; |
| 249 | } |
Shawn Guo | af4872f | 2012-03-05 22:30:54 +0800 | [diff] [blame] | 250 | EXPORT_SYMBOL_GPL(imx_audmux_v2_configure_port); |
Sascha Hauer | 9eedbdf | 2009-10-29 17:12:39 +0100 | [diff] [blame] | 251 | |
Richard Zhao | 3bc34a6 | 2012-03-05 22:30:52 +0800 | [diff] [blame] | 252 | static int __init imx_audmux_probe(struct platform_device *pdev) |
Sascha Hauer | 9eedbdf | 2009-10-29 17:12:39 +0100 | [diff] [blame] | 253 | { |
Richard Zhao | 3bc34a6 | 2012-03-05 22:30:52 +0800 | [diff] [blame] | 254 | struct resource *res; |
Richard Zhao | 9d5ef26 | 2012-03-05 22:31:04 +0800 | [diff] [blame] | 255 | const struct of_device_id *of_id = |
| 256 | of_match_device(imx_audmux_dt_ids, &pdev->dev); |
Richard Zhao | 3bc34a6 | 2012-03-05 22:30:52 +0800 | [diff] [blame] | 257 | |
| 258 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 259 | audmux_base = devm_request_and_ioremap(&pdev->dev, res); |
| 260 | if (!audmux_base) |
| 261 | return -EADDRNOTAVAIL; |
| 262 | |
| 263 | audmux_clk = clk_get(&pdev->dev, "audmux"); |
| 264 | if (IS_ERR(audmux_clk)) { |
| 265 | dev_dbg(&pdev->dev, "cannot get clock: %ld\n", |
| 266 | PTR_ERR(audmux_clk)); |
| 267 | audmux_clk = NULL; |
Eric BĂ©nard | 8402ed3 | 2010-06-08 11:03:00 +0200 | [diff] [blame] | 268 | } |
Sascha Hauer | 56c6b87 | 2011-08-23 21:24:57 +0200 | [diff] [blame] | 269 | |
Richard Zhao | 9d5ef26 | 2012-03-05 22:31:04 +0800 | [diff] [blame] | 270 | if (of_id) |
| 271 | pdev->id_entry = of_id->data; |
Richard Zhao | 3bc34a6 | 2012-03-05 22:30:52 +0800 | [diff] [blame] | 272 | audmux_type = pdev->id_entry->driver_data; |
| 273 | if (audmux_type == IMX31_AUDMUX) |
Shawn Guo | 2405fc9 | 2012-03-05 22:30:51 +0800 | [diff] [blame] | 274 | audmux_debugfs_init(); |
Mark Brown | 7b4e08a | 2010-01-11 16:33:18 +0000 | [diff] [blame] | 275 | |
Sascha Hauer | 9eedbdf | 2009-10-29 17:12:39 +0100 | [diff] [blame] | 276 | return 0; |
| 277 | } |
| 278 | |
Richard Zhao | 3bc34a6 | 2012-03-05 22:30:52 +0800 | [diff] [blame] | 279 | static int __exit imx_audmux_remove(struct platform_device *pdev) |
| 280 | { |
| 281 | if (audmux_type == IMX31_AUDMUX) |
| 282 | audmux_debugfs_remove(); |
| 283 | clk_put(audmux_clk); |
| 284 | |
| 285 | return 0; |
| 286 | } |
| 287 | |
| 288 | static struct platform_driver imx_audmux_driver = { |
| 289 | .probe = imx_audmux_probe, |
| 290 | .remove = __exit_p(imx_audmux_remove), |
| 291 | .id_table = imx_audmux_ids, |
| 292 | .driver = { |
| 293 | .name = DRIVER_NAME, |
| 294 | .owner = THIS_MODULE, |
Richard Zhao | 9d5ef26 | 2012-03-05 22:31:04 +0800 | [diff] [blame] | 295 | .of_match_table = imx_audmux_dt_ids, |
Richard Zhao | 3bc34a6 | 2012-03-05 22:30:52 +0800 | [diff] [blame] | 296 | } |
| 297 | }; |
| 298 | |
| 299 | static int __init imx_audmux_init(void) |
| 300 | { |
| 301 | return platform_driver_register(&imx_audmux_driver); |
| 302 | } |
| 303 | subsys_initcall(imx_audmux_init); |
| 304 | |
| 305 | static void __exit imx_audmux_exit(void) |
| 306 | { |
| 307 | platform_driver_unregister(&imx_audmux_driver); |
| 308 | } |
| 309 | module_exit(imx_audmux_exit); |
| 310 | |
| 311 | MODULE_DESCRIPTION("Freescale i.MX AUDMUX driver"); |
| 312 | MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>"); |
| 313 | MODULE_LICENSE("GPL v2"); |
| 314 | MODULE_ALIAS("platform:" DRIVER_NAME); |