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 | 3c77c29 | 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 | |
Mark Brown | 7b4e08a | 2010-01-11 16:33:18 +0000 | [diff] [blame] | 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) |
| 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 | |
| 65 | static 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 Estevam | 66bb2a7 | 2012-04-05 10:57:51 -0300 | [diff] [blame] | 76 | if (!audmux_base) |
| 77 | return -ENOSYS; |
| 78 | |
Mark Brown | 7b4e08a | 2010-01-11 16:33:18 +0000 | [diff] [blame] | 79 | if (audmux_clk) |
Linus Torvalds | 3480059 | 2012-03-27 16:41:24 -0700 | [diff] [blame] | 80 | clk_prepare_enable(audmux_clk); |
Mark Brown | 7b4e08a | 2010-01-11 16:33:18 +0000 | [diff] [blame] | 81 | |
Shawn Guo | af4872f | 2012-03-05 22:30:54 +0800 | [diff] [blame] | 82 | ptcr = readl(audmux_base + IMX_AUDMUX_V2_PTCR(port)); |
| 83 | pdcr = readl(audmux_base + IMX_AUDMUX_V2_PDCR(port)); |
Mark Brown | 7b4e08a | 2010-01-11 16:33:18 +0000 | [diff] [blame] | 84 | |
| 85 | if (audmux_clk) |
Linus Torvalds | 3480059 | 2012-03-27 16:41:24 -0700 | [diff] [blame] | 86 | clk_disable_unprepare(audmux_clk); |
Mark Brown | 7b4e08a | 2010-01-11 16:33:18 +0000 | [diff] [blame] | 87 | |
| 88 | ret = snprintf(buf, PAGE_SIZE, "PDCR: %08x\nPTCR: %08x\n", |
| 89 | pdcr, ptcr); |
| 90 | |
Shawn Guo | af4872f | 2012-03-05 22:30:54 +0800 | [diff] [blame] | 91 | if (ptcr & IMX_AUDMUX_V2_PTCR_TFSDIR) |
Mark Brown | 7b4e08a | 2010-01-11 16:33:18 +0000 | [diff] [blame] | 92 | 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 Guo | af4872f | 2012-03-05 22:30:54 +0800 | [diff] [blame] | 99 | if (ptcr & IMX_AUDMUX_V2_PTCR_TCLKDIR) |
Mark Brown | 7b4e08a | 2010-01-11 16:33:18 +0000 | [diff] [blame] | 100 | 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 Guo | af4872f | 2012-03-05 22:30:54 +0800 | [diff] [blame] | 109 | if (ptcr & IMX_AUDMUX_V2_PTCR_SYN) { |
Mark Brown | 7b4e08a | 2010-01-11 16:33:18 +0000 | [diff] [blame] | 110 | ret += snprintf(buf + ret, PAGE_SIZE - ret, |
| 111 | "Port is symmetric"); |
| 112 | } else { |
Shawn Guo | af4872f | 2012-03-05 22:30:54 +0800 | [diff] [blame] | 113 | if (ptcr & IMX_AUDMUX_V2_PTCR_RFSDIR) |
Mark Brown | 7b4e08a | 2010-01-11 16:33:18 +0000 | [diff] [blame] | 114 | 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 Guo | af4872f | 2012-03-05 22:30:54 +0800 | [diff] [blame] | 121 | if (ptcr & IMX_AUDMUX_V2_PTCR_RCLKDIR) |
Mark Brown | 7b4e08a | 2010-01-11 16:33:18 +0000 | [diff] [blame] | 122 | 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 | |
| 141 | static const struct file_operations audmux_debugfs_fops = { |
Stephen Boyd | 234e340 | 2012-04-05 14:25:11 -0700 | [diff] [blame] | 142 | .open = simple_open, |
Mark Brown | 7b4e08a | 2010-01-11 16:33:18 +0000 | [diff] [blame] | 143 | .read = audmux_read_file, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 144 | .llseek = default_llseek, |
Mark Brown | 7b4e08a | 2010-01-11 16:33:18 +0000 | [diff] [blame] | 145 | }; |
| 146 | |
Richard Zhao | 3bc34a6 | 2012-03-05 22:30:52 +0800 | [diff] [blame] | 147 | static void __init audmux_debugfs_init(void) |
Mark Brown | 7b4e08a | 2010-01-11 16:33:18 +0000 | [diff] [blame] | 148 | { |
| 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 | |
Fabio Estevam | 00792ac | 2012-04-05 09:45:51 -0300 | [diff] [blame] | 158 | for (i = 0; i < MX31_AUDMUX_PORT6_SSI_PINS_6 + 1; i++) { |
Mark Brown | 7b4e08a | 2010-01-11 16:33:18 +0000 | [diff] [blame] | 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", |
| 163 | i); |
| 164 | } |
| 165 | } |
Richard Zhao | 3bc34a6 | 2012-03-05 22:30:52 +0800 | [diff] [blame] | 166 | |
Fabio Estevam | fb97624a | 2012-03-07 15:19:57 -0300 | [diff] [blame] | 167 | static void __devexit audmux_debugfs_remove(void) |
Richard Zhao | 3bc34a6 | 2012-03-05 22:30:52 +0800 | [diff] [blame] | 168 | { |
| 169 | debugfs_remove_recursive(audmux_debugfs_root); |
| 170 | } |
Mark Brown | 7b4e08a | 2010-01-11 16:33:18 +0000 | [diff] [blame] | 171 | #else |
| 172 | static inline void audmux_debugfs_init(void) |
| 173 | { |
| 174 | } |
Richard Zhao | 3bc34a6 | 2012-03-05 22:30:52 +0800 | [diff] [blame] | 175 | |
| 176 | static inline void audmux_debugfs_remove(void) |
| 177 | { |
| 178 | } |
Mark Brown | 7b4e08a | 2010-01-11 16:33:18 +0000 | [diff] [blame] | 179 | #endif |
| 180 | |
Richard Zhao | 3bc34a6 | 2012-03-05 22:30:52 +0800 | [diff] [blame] | 181 | enum imx_audmux_type { |
| 182 | IMX21_AUDMUX, |
| 183 | IMX31_AUDMUX, |
| 184 | } audmux_type; |
| 185 | |
| 186 | static 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 | }; |
| 197 | MODULE_DEVICE_TABLE(platform, imx_audmux_ids); |
| 198 | |
Richard Zhao | 9d5ef26 | 2012-03-05 22:31:04 +0800 | [diff] [blame] | 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], }, |
| 202 | { /* sentinel */ } |
| 203 | }; |
| 204 | MODULE_DEVICE_TABLE(of, imx_audmux_dt_ids); |
| 205 | |
Shawn Guo | 2405fc9 | 2012-03-05 22:30:51 +0800 | [diff] [blame] | 206 | static const uint8_t port_mapping[] = { |
| 207 | 0x0, 0x4, 0x8, 0x10, 0x14, 0x1c, |
| 208 | }; |
| 209 | |
Shawn Guo | af4872f | 2012-03-05 22:30:54 +0800 | [diff] [blame] | 210 | int imx_audmux_v1_configure_port(unsigned int port, unsigned int pcr) |
Shawn Guo | 2405fc9 | 2012-03-05 22:30:51 +0800 | [diff] [blame] | 211 | { |
Richard Zhao | 3bc34a6 | 2012-03-05 22:30:52 +0800 | [diff] [blame] | 212 | if (audmux_type != IMX21_AUDMUX) |
| 213 | return -EINVAL; |
| 214 | |
Shawn Guo | 2405fc9 | 2012-03-05 22:30:51 +0800 | [diff] [blame] | 215 | 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 Guo | af4872f | 2012-03-05 22:30:54 +0800 | [diff] [blame] | 225 | EXPORT_SYMBOL_GPL(imx_audmux_v1_configure_port); |
Shawn Guo | 2405fc9 | 2012-03-05 22:30:51 +0800 | [diff] [blame] | 226 | |
Shawn Guo | af4872f | 2012-03-05 22:30:54 +0800 | [diff] [blame] | 227 | int imx_audmux_v2_configure_port(unsigned int port, unsigned int ptcr, |
Sascha Hauer | 9eedbdf | 2009-10-29 17:12:39 +0100 | [diff] [blame] | 228 | unsigned int pdcr) |
| 229 | { |
Richard Zhao | 3bc34a6 | 2012-03-05 22:30:52 +0800 | [diff] [blame] | 230 | if (audmux_type != IMX31_AUDMUX) |
| 231 | return -EINVAL; |
| 232 | |
Sascha Hauer | 9eedbdf | 2009-10-29 17:12:39 +0100 | [diff] [blame] | 233 | if (!audmux_base) |
| 234 | return -ENOSYS; |
| 235 | |
| 236 | if (audmux_clk) |
Linus Torvalds | 3480059 | 2012-03-27 16:41:24 -0700 | [diff] [blame] | 237 | clk_prepare_enable(audmux_clk); |
Sascha Hauer | 9eedbdf | 2009-10-29 17:12:39 +0100 | [diff] [blame] | 238 | |
Shawn Guo | af4872f | 2012-03-05 22:30:54 +0800 | [diff] [blame] | 239 | writel(ptcr, audmux_base + IMX_AUDMUX_V2_PTCR(port)); |
| 240 | writel(pdcr, audmux_base + IMX_AUDMUX_V2_PDCR(port)); |
Sascha Hauer | 9eedbdf | 2009-10-29 17:12:39 +0100 | [diff] [blame] | 241 | |
| 242 | if (audmux_clk) |
Linus Torvalds | 3480059 | 2012-03-27 16:41:24 -0700 | [diff] [blame] | 243 | clk_disable_unprepare(audmux_clk); |
Sascha Hauer | 9eedbdf | 2009-10-29 17:12:39 +0100 | [diff] [blame] | 244 | |
| 245 | return 0; |
| 246 | } |
Shawn Guo | af4872f | 2012-03-05 22:30:54 +0800 | [diff] [blame] | 247 | EXPORT_SYMBOL_GPL(imx_audmux_v2_configure_port); |
Sascha Hauer | 9eedbdf | 2009-10-29 17:12:39 +0100 | [diff] [blame] | 248 | |
Fabio Estevam | fb97624a | 2012-03-07 15:19:57 -0300 | [diff] [blame] | 249 | static int __devinit imx_audmux_probe(struct platform_device *pdev) |
Sascha Hauer | 9eedbdf | 2009-10-29 17:12:39 +0100 | [diff] [blame] | 250 | { |
Richard Zhao | 3bc34a6 | 2012-03-05 22:30:52 +0800 | [diff] [blame] | 251 | struct resource *res; |
Richard Zhao | 9d5ef26 | 2012-03-05 22:31:04 +0800 | [diff] [blame] | 252 | const struct of_device_id *of_id = |
| 253 | of_match_device(imx_audmux_dt_ids, &pdev->dev); |
Richard Zhao | 3bc34a6 | 2012-03-05 22:30:52 +0800 | [diff] [blame] | 254 | |
| 255 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 256 | audmux_base = devm_request_and_ioremap(&pdev->dev, res); |
| 257 | if (!audmux_base) |
| 258 | return -EADDRNOTAVAIL; |
| 259 | |
| 260 | audmux_clk = clk_get(&pdev->dev, "audmux"); |
| 261 | if (IS_ERR(audmux_clk)) { |
| 262 | dev_dbg(&pdev->dev, "cannot get clock: %ld\n", |
| 263 | PTR_ERR(audmux_clk)); |
| 264 | audmux_clk = NULL; |
Eric BĂ©nard | 8402ed3 | 2010-06-08 11:03:00 +0200 | [diff] [blame] | 265 | } |
Sascha Hauer | 56c6b87 | 2011-08-23 21:24:57 +0200 | [diff] [blame] | 266 | |
Richard Zhao | 9d5ef26 | 2012-03-05 22:31:04 +0800 | [diff] [blame] | 267 | if (of_id) |
| 268 | pdev->id_entry = of_id->data; |
Richard Zhao | 3bc34a6 | 2012-03-05 22:30:52 +0800 | [diff] [blame] | 269 | audmux_type = pdev->id_entry->driver_data; |
| 270 | if (audmux_type == IMX31_AUDMUX) |
Shawn Guo | 2405fc9 | 2012-03-05 22:30:51 +0800 | [diff] [blame] | 271 | audmux_debugfs_init(); |
Mark Brown | 7b4e08a | 2010-01-11 16:33:18 +0000 | [diff] [blame] | 272 | |
Sascha Hauer | 9eedbdf | 2009-10-29 17:12:39 +0100 | [diff] [blame] | 273 | return 0; |
| 274 | } |
| 275 | |
Fabio Estevam | fb97624a | 2012-03-07 15:19:57 -0300 | [diff] [blame] | 276 | static int __devexit imx_audmux_remove(struct platform_device *pdev) |
Richard Zhao | 3bc34a6 | 2012-03-05 22:30:52 +0800 | [diff] [blame] | 277 | { |
| 278 | if (audmux_type == IMX31_AUDMUX) |
| 279 | audmux_debugfs_remove(); |
| 280 | clk_put(audmux_clk); |
| 281 | |
| 282 | return 0; |
| 283 | } |
| 284 | |
| 285 | static struct platform_driver imx_audmux_driver = { |
| 286 | .probe = imx_audmux_probe, |
Fabio Estevam | fb97624a | 2012-03-07 15:19:57 -0300 | [diff] [blame] | 287 | .remove = __devexit_p(imx_audmux_remove), |
Richard Zhao | 3bc34a6 | 2012-03-05 22:30:52 +0800 | [diff] [blame] | 288 | .id_table = imx_audmux_ids, |
| 289 | .driver = { |
| 290 | .name = DRIVER_NAME, |
| 291 | .owner = THIS_MODULE, |
Richard Zhao | 9d5ef26 | 2012-03-05 22:31:04 +0800 | [diff] [blame] | 292 | .of_match_table = imx_audmux_dt_ids, |
Richard Zhao | 3bc34a6 | 2012-03-05 22:30:52 +0800 | [diff] [blame] | 293 | } |
| 294 | }; |
| 295 | |
| 296 | static int __init imx_audmux_init(void) |
| 297 | { |
| 298 | return platform_driver_register(&imx_audmux_driver); |
| 299 | } |
| 300 | subsys_initcall(imx_audmux_init); |
| 301 | |
| 302 | static void __exit imx_audmux_exit(void) |
| 303 | { |
| 304 | platform_driver_unregister(&imx_audmux_driver); |
| 305 | } |
| 306 | module_exit(imx_audmux_exit); |
| 307 | |
| 308 | MODULE_DESCRIPTION("Freescale i.MX AUDMUX driver"); |
| 309 | MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>"); |
| 310 | MODULE_LICENSE("GPL v2"); |
| 311 | MODULE_ALIAS("platform:" DRIVER_NAME); |