1a23dc694SShawn Guo /* 2a23dc694SShawn Guo * Copyright 2012 Freescale Semiconductor, Inc. 3a23dc694SShawn Guo * Copyright 2012 Linaro Ltd. 4a23dc694SShawn Guo * Copyright 2009 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de> 5a23dc694SShawn Guo * 6a23dc694SShawn Guo * Initial development of this code was funded by 7a23dc694SShawn Guo * Phytec Messtechnik GmbH, http://www.phytec.de 8a23dc694SShawn Guo * 9a23dc694SShawn Guo * This program is free software; you can redistribute it and/or modify 10a23dc694SShawn Guo * it under the terms of the GNU General Public License as published by 11a23dc694SShawn Guo * the Free Software Foundation; either version 2 of the License, or 12a23dc694SShawn Guo * (at your option) any later version. 13a23dc694SShawn Guo * 14a23dc694SShawn Guo * This program is distributed in the hope that it will be useful, 15a23dc694SShawn Guo * but WITHOUT ANY WARRANTY; without even the implied warranty of 16a23dc694SShawn Guo * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17a23dc694SShawn Guo * GNU General Public License for more details. 18a23dc694SShawn Guo */ 19a23dc694SShawn Guo 20a23dc694SShawn Guo #include <linux/clk.h> 21a23dc694SShawn Guo #include <linux/debugfs.h> 22a23dc694SShawn Guo #include <linux/err.h> 23a23dc694SShawn Guo #include <linux/io.h> 24a23dc694SShawn Guo #include <linux/module.h> 25a23dc694SShawn Guo #include <linux/of.h> 26a23dc694SShawn Guo #include <linux/of_device.h> 27a23dc694SShawn Guo #include <linux/platform_device.h> 28a23dc694SShawn Guo #include <linux/slab.h> 29a1c85ec0SRichard Zhao #include <linux/pinctrl/consumer.h> 30a23dc694SShawn Guo 31a23dc694SShawn Guo #include "imx-audmux.h" 32a23dc694SShawn Guo 33a23dc694SShawn Guo #define DRIVER_NAME "imx-audmux" 34a23dc694SShawn Guo 35a23dc694SShawn Guo static struct clk *audmux_clk; 36a23dc694SShawn Guo static void __iomem *audmux_base; 37a23dc694SShawn Guo 38a23dc694SShawn Guo #define IMX_AUDMUX_V2_PTCR(x) ((x) * 8) 39a23dc694SShawn Guo #define IMX_AUDMUX_V2_PDCR(x) ((x) * 8 + 4) 40a23dc694SShawn Guo 41a23dc694SShawn Guo #ifdef CONFIG_DEBUG_FS 42a23dc694SShawn Guo static struct dentry *audmux_debugfs_root; 43a23dc694SShawn Guo 44a23dc694SShawn Guo /* There is an annoying discontinuity in the SSI numbering with regard 45a23dc694SShawn Guo * to the Linux number of the devices */ 46a23dc694SShawn Guo static const char *audmux_port_string(int port) 47a23dc694SShawn Guo { 48a23dc694SShawn Guo switch (port) { 49a23dc694SShawn Guo case MX31_AUDMUX_PORT1_SSI0: 50a23dc694SShawn Guo return "imx-ssi.0"; 51a23dc694SShawn Guo case MX31_AUDMUX_PORT2_SSI1: 52a23dc694SShawn Guo return "imx-ssi.1"; 53a23dc694SShawn Guo case MX31_AUDMUX_PORT3_SSI_PINS_3: 54a23dc694SShawn Guo return "SSI3"; 55a23dc694SShawn Guo case MX31_AUDMUX_PORT4_SSI_PINS_4: 56a23dc694SShawn Guo return "SSI4"; 57a23dc694SShawn Guo case MX31_AUDMUX_PORT5_SSI_PINS_5: 58a23dc694SShawn Guo return "SSI5"; 59a23dc694SShawn Guo case MX31_AUDMUX_PORT6_SSI_PINS_6: 60a23dc694SShawn Guo return "SSI6"; 61a23dc694SShawn Guo default: 62a23dc694SShawn Guo return "UNKNOWN"; 63a23dc694SShawn Guo } 64a23dc694SShawn Guo } 65a23dc694SShawn Guo 66a23dc694SShawn Guo static ssize_t audmux_read_file(struct file *file, char __user *user_buf, 67a23dc694SShawn Guo size_t count, loff_t *ppos) 68a23dc694SShawn Guo { 69a23dc694SShawn Guo ssize_t ret; 70a23dc694SShawn Guo char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL); 71a23dc694SShawn Guo int port = (int)file->private_data; 72a23dc694SShawn Guo u32 pdcr, ptcr; 73a23dc694SShawn Guo 74a23dc694SShawn Guo if (!buf) 75a23dc694SShawn Guo return -ENOMEM; 76a23dc694SShawn Guo 77a23dc694SShawn Guo if (audmux_clk) 78a23dc694SShawn Guo clk_prepare_enable(audmux_clk); 79a23dc694SShawn Guo 80a23dc694SShawn Guo ptcr = readl(audmux_base + IMX_AUDMUX_V2_PTCR(port)); 81a23dc694SShawn Guo pdcr = readl(audmux_base + IMX_AUDMUX_V2_PDCR(port)); 82a23dc694SShawn Guo 83a23dc694SShawn Guo if (audmux_clk) 84a23dc694SShawn Guo clk_disable_unprepare(audmux_clk); 85a23dc694SShawn Guo 86a23dc694SShawn Guo ret = snprintf(buf, PAGE_SIZE, "PDCR: %08x\nPTCR: %08x\n", 87a23dc694SShawn Guo pdcr, ptcr); 88a23dc694SShawn Guo 89a23dc694SShawn Guo if (ptcr & IMX_AUDMUX_V2_PTCR_TFSDIR) 90a23dc694SShawn Guo ret += snprintf(buf + ret, PAGE_SIZE - ret, 91a23dc694SShawn Guo "TxFS output from %s, ", 92a23dc694SShawn Guo audmux_port_string((ptcr >> 27) & 0x7)); 93a23dc694SShawn Guo else 94a23dc694SShawn Guo ret += snprintf(buf + ret, PAGE_SIZE - ret, 95a23dc694SShawn Guo "TxFS input, "); 96a23dc694SShawn Guo 97a23dc694SShawn Guo if (ptcr & IMX_AUDMUX_V2_PTCR_TCLKDIR) 98a23dc694SShawn Guo ret += snprintf(buf + ret, PAGE_SIZE - ret, 99a23dc694SShawn Guo "TxClk output from %s", 100a23dc694SShawn Guo audmux_port_string((ptcr >> 22) & 0x7)); 101a23dc694SShawn Guo else 102a23dc694SShawn Guo ret += snprintf(buf + ret, PAGE_SIZE - ret, 103a23dc694SShawn Guo "TxClk input"); 104a23dc694SShawn Guo 105a23dc694SShawn Guo ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n"); 106a23dc694SShawn Guo 107a23dc694SShawn Guo if (ptcr & IMX_AUDMUX_V2_PTCR_SYN) { 108a23dc694SShawn Guo ret += snprintf(buf + ret, PAGE_SIZE - ret, 109a23dc694SShawn Guo "Port is symmetric"); 110a23dc694SShawn Guo } else { 111a23dc694SShawn Guo if (ptcr & IMX_AUDMUX_V2_PTCR_RFSDIR) 112a23dc694SShawn Guo ret += snprintf(buf + ret, PAGE_SIZE - ret, 113a23dc694SShawn Guo "RxFS output from %s, ", 114a23dc694SShawn Guo audmux_port_string((ptcr >> 17) & 0x7)); 115a23dc694SShawn Guo else 116a23dc694SShawn Guo ret += snprintf(buf + ret, PAGE_SIZE - ret, 117a23dc694SShawn Guo "RxFS input, "); 118a23dc694SShawn Guo 119a23dc694SShawn Guo if (ptcr & IMX_AUDMUX_V2_PTCR_RCLKDIR) 120a23dc694SShawn Guo ret += snprintf(buf + ret, PAGE_SIZE - ret, 121a23dc694SShawn Guo "RxClk output from %s", 122a23dc694SShawn Guo audmux_port_string((ptcr >> 12) & 0x7)); 123a23dc694SShawn Guo else 124a23dc694SShawn Guo ret += snprintf(buf + ret, PAGE_SIZE - ret, 125a23dc694SShawn Guo "RxClk input"); 126a23dc694SShawn Guo } 127a23dc694SShawn Guo 128a23dc694SShawn Guo ret += snprintf(buf + ret, PAGE_SIZE - ret, 129a23dc694SShawn Guo "\nData received from %s\n", 130a23dc694SShawn Guo audmux_port_string((pdcr >> 13) & 0x7)); 131a23dc694SShawn Guo 132a23dc694SShawn Guo ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); 133a23dc694SShawn Guo 134a23dc694SShawn Guo kfree(buf); 135a23dc694SShawn Guo 136a23dc694SShawn Guo return ret; 137a23dc694SShawn Guo } 138a23dc694SShawn Guo 139a23dc694SShawn Guo static const struct file_operations audmux_debugfs_fops = { 1401eecb828SMark Brown .open = simple_open, 141a23dc694SShawn Guo .read = audmux_read_file, 142a23dc694SShawn Guo .llseek = default_llseek, 143a23dc694SShawn Guo }; 144a23dc694SShawn Guo 145a23dc694SShawn Guo static void __init audmux_debugfs_init(void) 146a23dc694SShawn Guo { 147a23dc694SShawn Guo int i; 148a23dc694SShawn Guo char buf[20]; 149a23dc694SShawn Guo 150a23dc694SShawn Guo audmux_debugfs_root = debugfs_create_dir("audmux", NULL); 151a23dc694SShawn Guo if (!audmux_debugfs_root) { 152a23dc694SShawn Guo pr_warning("Failed to create AUDMUX debugfs root\n"); 153a23dc694SShawn Guo return; 154a23dc694SShawn Guo } 155a23dc694SShawn Guo 156409b78ccSTorben Hohn for (i = 0; i < MX31_AUDMUX_PORT7_SSI_PINS_7 + 1; i++) { 157a23dc694SShawn Guo snprintf(buf, sizeof(buf), "ssi%d", i); 158a23dc694SShawn Guo if (!debugfs_create_file(buf, 0444, audmux_debugfs_root, 159a23dc694SShawn Guo (void *)i, &audmux_debugfs_fops)) 160a23dc694SShawn Guo pr_warning("Failed to create AUDMUX port %d debugfs file\n", 161a23dc694SShawn Guo i); 162a23dc694SShawn Guo } 163a23dc694SShawn Guo } 164a23dc694SShawn Guo 165*a0a3d518SBill Pemberton static void audmux_debugfs_remove(void) 166a23dc694SShawn Guo { 167a23dc694SShawn Guo debugfs_remove_recursive(audmux_debugfs_root); 168a23dc694SShawn Guo } 169a23dc694SShawn Guo #else 170a23dc694SShawn Guo static inline void audmux_debugfs_init(void) 171a23dc694SShawn Guo { 172a23dc694SShawn Guo } 173a23dc694SShawn Guo 174a23dc694SShawn Guo static inline void audmux_debugfs_remove(void) 175a23dc694SShawn Guo { 176a23dc694SShawn Guo } 177a23dc694SShawn Guo #endif 178a23dc694SShawn Guo 179a23dc694SShawn Guo enum imx_audmux_type { 180a23dc694SShawn Guo IMX21_AUDMUX, 181a23dc694SShawn Guo IMX31_AUDMUX, 182a23dc694SShawn Guo } audmux_type; 183a23dc694SShawn Guo 184a23dc694SShawn Guo static struct platform_device_id imx_audmux_ids[] = { 185a23dc694SShawn Guo { 186a23dc694SShawn Guo .name = "imx21-audmux", 187a23dc694SShawn Guo .driver_data = IMX21_AUDMUX, 188a23dc694SShawn Guo }, { 189a23dc694SShawn Guo .name = "imx31-audmux", 190a23dc694SShawn Guo .driver_data = IMX31_AUDMUX, 191a23dc694SShawn Guo }, { 192a23dc694SShawn Guo /* sentinel */ 193a23dc694SShawn Guo } 194a23dc694SShawn Guo }; 195a23dc694SShawn Guo MODULE_DEVICE_TABLE(platform, imx_audmux_ids); 196a23dc694SShawn Guo 197a23dc694SShawn Guo static const struct of_device_id imx_audmux_dt_ids[] = { 198a23dc694SShawn Guo { .compatible = "fsl,imx21-audmux", .data = &imx_audmux_ids[0], }, 199a23dc694SShawn Guo { .compatible = "fsl,imx31-audmux", .data = &imx_audmux_ids[1], }, 200a23dc694SShawn Guo { /* sentinel */ } 201a23dc694SShawn Guo }; 202a23dc694SShawn Guo MODULE_DEVICE_TABLE(of, imx_audmux_dt_ids); 203a23dc694SShawn Guo 204a23dc694SShawn Guo static const uint8_t port_mapping[] = { 205a23dc694SShawn Guo 0x0, 0x4, 0x8, 0x10, 0x14, 0x1c, 206a23dc694SShawn Guo }; 207a23dc694SShawn Guo 208a23dc694SShawn Guo int imx_audmux_v1_configure_port(unsigned int port, unsigned int pcr) 209a23dc694SShawn Guo { 210a23dc694SShawn Guo if (audmux_type != IMX21_AUDMUX) 211a23dc694SShawn Guo return -EINVAL; 212a23dc694SShawn Guo 213a23dc694SShawn Guo if (!audmux_base) 214a23dc694SShawn Guo return -ENOSYS; 215a23dc694SShawn Guo 216a23dc694SShawn Guo if (port >= ARRAY_SIZE(port_mapping)) 217a23dc694SShawn Guo return -EINVAL; 218a23dc694SShawn Guo 219a23dc694SShawn Guo writel(pcr, audmux_base + port_mapping[port]); 220a23dc694SShawn Guo 221a23dc694SShawn Guo return 0; 222a23dc694SShawn Guo } 223a23dc694SShawn Guo EXPORT_SYMBOL_GPL(imx_audmux_v1_configure_port); 224a23dc694SShawn Guo 225a23dc694SShawn Guo int imx_audmux_v2_configure_port(unsigned int port, unsigned int ptcr, 226a23dc694SShawn Guo unsigned int pdcr) 227a23dc694SShawn Guo { 228a23dc694SShawn Guo if (audmux_type != IMX31_AUDMUX) 229a23dc694SShawn Guo return -EINVAL; 230a23dc694SShawn Guo 231a23dc694SShawn Guo if (!audmux_base) 232a23dc694SShawn Guo return -ENOSYS; 233a23dc694SShawn Guo 234a23dc694SShawn Guo if (audmux_clk) 235a23dc694SShawn Guo clk_prepare_enable(audmux_clk); 236a23dc694SShawn Guo 237a23dc694SShawn Guo writel(ptcr, audmux_base + IMX_AUDMUX_V2_PTCR(port)); 238a23dc694SShawn Guo writel(pdcr, audmux_base + IMX_AUDMUX_V2_PDCR(port)); 239a23dc694SShawn Guo 240a23dc694SShawn Guo if (audmux_clk) 241a23dc694SShawn Guo clk_disable_unprepare(audmux_clk); 242a23dc694SShawn Guo 243a23dc694SShawn Guo return 0; 244a23dc694SShawn Guo } 245a23dc694SShawn Guo EXPORT_SYMBOL_GPL(imx_audmux_v2_configure_port); 246a23dc694SShawn Guo 247*a0a3d518SBill Pemberton static int imx_audmux_probe(struct platform_device *pdev) 248a23dc694SShawn Guo { 249a23dc694SShawn Guo struct resource *res; 250a1c85ec0SRichard Zhao struct pinctrl *pinctrl; 251a23dc694SShawn Guo const struct of_device_id *of_id = 252a23dc694SShawn Guo of_match_device(imx_audmux_dt_ids, &pdev->dev); 253a23dc694SShawn Guo 254a23dc694SShawn Guo res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 255a23dc694SShawn Guo audmux_base = devm_request_and_ioremap(&pdev->dev, res); 256a23dc694SShawn Guo if (!audmux_base) 257a23dc694SShawn Guo return -EADDRNOTAVAIL; 258a23dc694SShawn Guo 259a1c85ec0SRichard Zhao pinctrl = devm_pinctrl_get_select_default(&pdev->dev); 260a1c85ec0SRichard Zhao if (IS_ERR(pinctrl)) { 261a1c85ec0SRichard Zhao dev_err(&pdev->dev, "setup pinctrl failed!"); 262a1c85ec0SRichard Zhao return PTR_ERR(pinctrl); 263a1c85ec0SRichard Zhao } 264a1c85ec0SRichard Zhao 265a23dc694SShawn Guo audmux_clk = clk_get(&pdev->dev, "audmux"); 266a23dc694SShawn Guo if (IS_ERR(audmux_clk)) { 267a23dc694SShawn Guo dev_dbg(&pdev->dev, "cannot get clock: %ld\n", 268a23dc694SShawn Guo PTR_ERR(audmux_clk)); 269a23dc694SShawn Guo audmux_clk = NULL; 270a23dc694SShawn Guo } 271a23dc694SShawn Guo 272a23dc694SShawn Guo if (of_id) 273a23dc694SShawn Guo pdev->id_entry = of_id->data; 274a23dc694SShawn Guo audmux_type = pdev->id_entry->driver_data; 275a23dc694SShawn Guo if (audmux_type == IMX31_AUDMUX) 276a23dc694SShawn Guo audmux_debugfs_init(); 277a23dc694SShawn Guo 278a23dc694SShawn Guo return 0; 279a23dc694SShawn Guo } 280a23dc694SShawn Guo 281*a0a3d518SBill Pemberton static int imx_audmux_remove(struct platform_device *pdev) 282a23dc694SShawn Guo { 283a23dc694SShawn Guo if (audmux_type == IMX31_AUDMUX) 284a23dc694SShawn Guo audmux_debugfs_remove(); 285a23dc694SShawn Guo clk_put(audmux_clk); 286a23dc694SShawn Guo 287a23dc694SShawn Guo return 0; 288a23dc694SShawn Guo } 289a23dc694SShawn Guo 290a23dc694SShawn Guo static struct platform_driver imx_audmux_driver = { 291a23dc694SShawn Guo .probe = imx_audmux_probe, 292*a0a3d518SBill Pemberton .remove = imx_audmux_remove, 293a23dc694SShawn Guo .id_table = imx_audmux_ids, 294a23dc694SShawn Guo .driver = { 295a23dc694SShawn Guo .name = DRIVER_NAME, 296a23dc694SShawn Guo .owner = THIS_MODULE, 297a23dc694SShawn Guo .of_match_table = imx_audmux_dt_ids, 298a23dc694SShawn Guo } 299a23dc694SShawn Guo }; 300a23dc694SShawn Guo 301a23dc694SShawn Guo static int __init imx_audmux_init(void) 302a23dc694SShawn Guo { 303a23dc694SShawn Guo return platform_driver_register(&imx_audmux_driver); 304a23dc694SShawn Guo } 305a23dc694SShawn Guo subsys_initcall(imx_audmux_init); 306a23dc694SShawn Guo 307a23dc694SShawn Guo static void __exit imx_audmux_exit(void) 308a23dc694SShawn Guo { 309a23dc694SShawn Guo platform_driver_unregister(&imx_audmux_driver); 310a23dc694SShawn Guo } 311a23dc694SShawn Guo module_exit(imx_audmux_exit); 312a23dc694SShawn Guo 313a23dc694SShawn Guo MODULE_DESCRIPTION("Freescale i.MX AUDMUX driver"); 314a23dc694SShawn Guo MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>"); 315a23dc694SShawn Guo MODULE_LICENSE("GPL v2"); 316a23dc694SShawn Guo MODULE_ALIAS("platform:" DRIVER_NAME); 317