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> 29a23dc694SShawn Guo 30a23dc694SShawn Guo #include "imx-audmux.h" 31a23dc694SShawn Guo 32a23dc694SShawn Guo #define DRIVER_NAME "imx-audmux" 33a23dc694SShawn Guo 34a23dc694SShawn Guo static struct clk *audmux_clk; 35a23dc694SShawn Guo static void __iomem *audmux_base; 36a23dc694SShawn Guo 37a23dc694SShawn Guo #define IMX_AUDMUX_V2_PTCR(x) ((x) * 8) 38a23dc694SShawn Guo #define IMX_AUDMUX_V2_PDCR(x) ((x) * 8 + 4) 39a23dc694SShawn Guo 40a23dc694SShawn Guo #ifdef CONFIG_DEBUG_FS 41a23dc694SShawn Guo static struct dentry *audmux_debugfs_root; 42a23dc694SShawn Guo 43a23dc694SShawn Guo /* There is an annoying discontinuity in the SSI numbering with regard 44a23dc694SShawn Guo * to the Linux number of the devices */ 45a23dc694SShawn Guo static const char *audmux_port_string(int port) 46a23dc694SShawn Guo { 47a23dc694SShawn Guo switch (port) { 48a23dc694SShawn Guo case MX31_AUDMUX_PORT1_SSI0: 49a23dc694SShawn Guo return "imx-ssi.0"; 50a23dc694SShawn Guo case MX31_AUDMUX_PORT2_SSI1: 51a23dc694SShawn Guo return "imx-ssi.1"; 52a23dc694SShawn Guo case MX31_AUDMUX_PORT3_SSI_PINS_3: 53a23dc694SShawn Guo return "SSI3"; 54a23dc694SShawn Guo case MX31_AUDMUX_PORT4_SSI_PINS_4: 55a23dc694SShawn Guo return "SSI4"; 56a23dc694SShawn Guo case MX31_AUDMUX_PORT5_SSI_PINS_5: 57a23dc694SShawn Guo return "SSI5"; 58a23dc694SShawn Guo case MX31_AUDMUX_PORT6_SSI_PINS_6: 59a23dc694SShawn Guo return "SSI6"; 60a23dc694SShawn Guo default: 61a23dc694SShawn Guo return "UNKNOWN"; 62a23dc694SShawn Guo } 63a23dc694SShawn Guo } 64a23dc694SShawn Guo 65a23dc694SShawn Guo static ssize_t audmux_read_file(struct file *file, char __user *user_buf, 66a23dc694SShawn Guo size_t count, loff_t *ppos) 67a23dc694SShawn Guo { 68a23dc694SShawn Guo ssize_t ret; 691b3ed70aSFelipe Pena char *buf; 70e5f89768SMark Brown uintptr_t port = (uintptr_t)file->private_data; 71a23dc694SShawn Guo u32 pdcr, ptcr; 72a23dc694SShawn Guo 7372192366SFabio Estevam if (audmux_clk) { 7472192366SFabio Estevam ret = clk_prepare_enable(audmux_clk); 7572192366SFabio Estevam if (ret) 7672192366SFabio Estevam return ret; 7772192366SFabio Estevam } 78a23dc694SShawn Guo 79a23dc694SShawn Guo ptcr = readl(audmux_base + IMX_AUDMUX_V2_PTCR(port)); 80a23dc694SShawn Guo pdcr = readl(audmux_base + IMX_AUDMUX_V2_PDCR(port)); 81a23dc694SShawn Guo 82a23dc694SShawn Guo if (audmux_clk) 83a23dc694SShawn Guo clk_disable_unprepare(audmux_clk); 84a23dc694SShawn Guo 851b3ed70aSFelipe Pena buf = kmalloc(PAGE_SIZE, GFP_KERNEL); 861b3ed70aSFelipe Pena if (!buf) 871b3ed70aSFelipe Pena return -ENOMEM; 881b3ed70aSFelipe Pena 89*c407cd00SSilvio Cesare ret = scnprintf(buf, PAGE_SIZE, "PDCR: %08x\nPTCR: %08x\n", 90a23dc694SShawn Guo pdcr, ptcr); 91a23dc694SShawn Guo 92a23dc694SShawn Guo if (ptcr & IMX_AUDMUX_V2_PTCR_TFSDIR) 93*c407cd00SSilvio Cesare ret += scnprintf(buf + ret, PAGE_SIZE - ret, 94a23dc694SShawn Guo "TxFS output from %s, ", 95a23dc694SShawn Guo audmux_port_string((ptcr >> 27) & 0x7)); 96a23dc694SShawn Guo else 97*c407cd00SSilvio Cesare ret += scnprintf(buf + ret, PAGE_SIZE - ret, 98a23dc694SShawn Guo "TxFS input, "); 99a23dc694SShawn Guo 100a23dc694SShawn Guo if (ptcr & IMX_AUDMUX_V2_PTCR_TCLKDIR) 101*c407cd00SSilvio Cesare ret += scnprintf(buf + ret, PAGE_SIZE - ret, 102a23dc694SShawn Guo "TxClk output from %s", 103a23dc694SShawn Guo audmux_port_string((ptcr >> 22) & 0x7)); 104a23dc694SShawn Guo else 105*c407cd00SSilvio Cesare ret += scnprintf(buf + ret, PAGE_SIZE - ret, 106a23dc694SShawn Guo "TxClk input"); 107a23dc694SShawn Guo 108*c407cd00SSilvio Cesare ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n"); 109a23dc694SShawn Guo 110a23dc694SShawn Guo if (ptcr & IMX_AUDMUX_V2_PTCR_SYN) { 111*c407cd00SSilvio Cesare ret += scnprintf(buf + ret, PAGE_SIZE - ret, 112a23dc694SShawn Guo "Port is symmetric"); 113a23dc694SShawn Guo } else { 114a23dc694SShawn Guo if (ptcr & IMX_AUDMUX_V2_PTCR_RFSDIR) 115*c407cd00SSilvio Cesare ret += scnprintf(buf + ret, PAGE_SIZE - ret, 116a23dc694SShawn Guo "RxFS output from %s, ", 117a23dc694SShawn Guo audmux_port_string((ptcr >> 17) & 0x7)); 118a23dc694SShawn Guo else 119*c407cd00SSilvio Cesare ret += scnprintf(buf + ret, PAGE_SIZE - ret, 120a23dc694SShawn Guo "RxFS input, "); 121a23dc694SShawn Guo 122a23dc694SShawn Guo if (ptcr & IMX_AUDMUX_V2_PTCR_RCLKDIR) 123*c407cd00SSilvio Cesare ret += scnprintf(buf + ret, PAGE_SIZE - ret, 124a23dc694SShawn Guo "RxClk output from %s", 125a23dc694SShawn Guo audmux_port_string((ptcr >> 12) & 0x7)); 126a23dc694SShawn Guo else 127*c407cd00SSilvio Cesare ret += scnprintf(buf + ret, PAGE_SIZE - ret, 128a23dc694SShawn Guo "RxClk input"); 129a23dc694SShawn Guo } 130a23dc694SShawn Guo 131*c407cd00SSilvio Cesare ret += scnprintf(buf + ret, PAGE_SIZE - ret, 132a23dc694SShawn Guo "\nData received from %s\n", 133a23dc694SShawn Guo audmux_port_string((pdcr >> 13) & 0x7)); 134a23dc694SShawn Guo 135a23dc694SShawn Guo ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); 136a23dc694SShawn Guo 137a23dc694SShawn Guo kfree(buf); 138a23dc694SShawn Guo 139a23dc694SShawn Guo return ret; 140a23dc694SShawn Guo } 141a23dc694SShawn Guo 142a23dc694SShawn Guo static const struct file_operations audmux_debugfs_fops = { 1431eecb828SMark Brown .open = simple_open, 144a23dc694SShawn Guo .read = audmux_read_file, 145a23dc694SShawn Guo .llseek = default_llseek, 146a23dc694SShawn Guo }; 147a23dc694SShawn Guo 148b8909783SLars-Peter Clausen static void audmux_debugfs_init(void) 149a23dc694SShawn Guo { 150e5f89768SMark Brown uintptr_t i; 151a23dc694SShawn Guo char buf[20]; 152a23dc694SShawn Guo 153a23dc694SShawn Guo audmux_debugfs_root = debugfs_create_dir("audmux", NULL); 154a23dc694SShawn Guo if (!audmux_debugfs_root) { 155a23dc694SShawn Guo pr_warning("Failed to create AUDMUX debugfs root\n"); 156a23dc694SShawn Guo return; 157a23dc694SShawn Guo } 158a23dc694SShawn Guo 159409b78ccSTorben Hohn for (i = 0; i < MX31_AUDMUX_PORT7_SSI_PINS_7 + 1; i++) { 160e5f89768SMark Brown snprintf(buf, sizeof(buf), "ssi%lu", i); 161a23dc694SShawn Guo if (!debugfs_create_file(buf, 0444, audmux_debugfs_root, 162a23dc694SShawn Guo (void *)i, &audmux_debugfs_fops)) 163e5f89768SMark Brown pr_warning("Failed to create AUDMUX port %lu debugfs file\n", 164a23dc694SShawn Guo i); 165a23dc694SShawn Guo } 166a23dc694SShawn Guo } 167a23dc694SShawn Guo 168a0a3d518SBill Pemberton static void audmux_debugfs_remove(void) 169a23dc694SShawn Guo { 170a23dc694SShawn Guo debugfs_remove_recursive(audmux_debugfs_root); 171a23dc694SShawn Guo } 172a23dc694SShawn Guo #else 173a23dc694SShawn Guo static inline void audmux_debugfs_init(void) 174a23dc694SShawn Guo { 175a23dc694SShawn Guo } 176a23dc694SShawn Guo 177a23dc694SShawn Guo static inline void audmux_debugfs_remove(void) 178a23dc694SShawn Guo { 179a23dc694SShawn Guo } 180a23dc694SShawn Guo #endif 181a23dc694SShawn Guo 182a10807aeSFabio Estevam static enum imx_audmux_type { 183a23dc694SShawn Guo IMX21_AUDMUX, 184a23dc694SShawn Guo IMX31_AUDMUX, 185a23dc694SShawn Guo } audmux_type; 186a23dc694SShawn Guo 187e51cebf7SKrzysztof Kozlowski static const struct platform_device_id imx_audmux_ids[] = { 188a23dc694SShawn Guo { 189a23dc694SShawn Guo .name = "imx21-audmux", 190a23dc694SShawn Guo .driver_data = IMX21_AUDMUX, 191a23dc694SShawn Guo }, { 192a23dc694SShawn Guo .name = "imx31-audmux", 193a23dc694SShawn Guo .driver_data = IMX31_AUDMUX, 194a23dc694SShawn Guo }, { 195a23dc694SShawn Guo /* sentinel */ 196a23dc694SShawn Guo } 197a23dc694SShawn Guo }; 198a23dc694SShawn Guo MODULE_DEVICE_TABLE(platform, imx_audmux_ids); 199a23dc694SShawn Guo 200a23dc694SShawn Guo static const struct of_device_id imx_audmux_dt_ids[] = { 201a23dc694SShawn Guo { .compatible = "fsl,imx21-audmux", .data = &imx_audmux_ids[0], }, 202a23dc694SShawn Guo { .compatible = "fsl,imx31-audmux", .data = &imx_audmux_ids[1], }, 203a23dc694SShawn Guo { /* sentinel */ } 204a23dc694SShawn Guo }; 205a23dc694SShawn Guo MODULE_DEVICE_TABLE(of, imx_audmux_dt_ids); 206a23dc694SShawn Guo 207a23dc694SShawn Guo static const uint8_t port_mapping[] = { 208a23dc694SShawn Guo 0x0, 0x4, 0x8, 0x10, 0x14, 0x1c, 209a23dc694SShawn Guo }; 210a23dc694SShawn Guo 211a23dc694SShawn Guo int imx_audmux_v1_configure_port(unsigned int port, unsigned int pcr) 212a23dc694SShawn Guo { 213a23dc694SShawn Guo if (audmux_type != IMX21_AUDMUX) 214a23dc694SShawn Guo return -EINVAL; 215a23dc694SShawn Guo 216a23dc694SShawn Guo if (!audmux_base) 217a23dc694SShawn Guo return -ENOSYS; 218a23dc694SShawn Guo 219a23dc694SShawn Guo if (port >= ARRAY_SIZE(port_mapping)) 220a23dc694SShawn Guo return -EINVAL; 221a23dc694SShawn Guo 222a23dc694SShawn Guo writel(pcr, audmux_base + port_mapping[port]); 223a23dc694SShawn Guo 224a23dc694SShawn Guo return 0; 225a23dc694SShawn Guo } 226a23dc694SShawn Guo EXPORT_SYMBOL_GPL(imx_audmux_v1_configure_port); 227a23dc694SShawn Guo 228a23dc694SShawn Guo int imx_audmux_v2_configure_port(unsigned int port, unsigned int ptcr, 229a23dc694SShawn Guo unsigned int pdcr) 230a23dc694SShawn Guo { 23172192366SFabio Estevam int ret; 23272192366SFabio Estevam 233a23dc694SShawn Guo if (audmux_type != IMX31_AUDMUX) 234a23dc694SShawn Guo return -EINVAL; 235a23dc694SShawn Guo 236a23dc694SShawn Guo if (!audmux_base) 237a23dc694SShawn Guo return -ENOSYS; 238a23dc694SShawn Guo 23972192366SFabio Estevam if (audmux_clk) { 24072192366SFabio Estevam ret = clk_prepare_enable(audmux_clk); 24172192366SFabio Estevam if (ret) 24272192366SFabio Estevam return ret; 24372192366SFabio Estevam } 244a23dc694SShawn Guo 245a23dc694SShawn Guo writel(ptcr, audmux_base + IMX_AUDMUX_V2_PTCR(port)); 246a23dc694SShawn Guo writel(pdcr, audmux_base + IMX_AUDMUX_V2_PDCR(port)); 247a23dc694SShawn Guo 248a23dc694SShawn Guo if (audmux_clk) 249a23dc694SShawn Guo clk_disable_unprepare(audmux_clk); 250a23dc694SShawn Guo 251a23dc694SShawn Guo return 0; 252a23dc694SShawn Guo } 253a23dc694SShawn Guo EXPORT_SYMBOL_GPL(imx_audmux_v2_configure_port); 254a23dc694SShawn Guo 2558548a464SMarkus Pargmann static int imx_audmux_parse_dt_defaults(struct platform_device *pdev, 2568548a464SMarkus Pargmann struct device_node *of_node) 2578548a464SMarkus Pargmann { 2588548a464SMarkus Pargmann struct device_node *child; 2598548a464SMarkus Pargmann 2608548a464SMarkus Pargmann for_each_available_child_of_node(of_node, child) { 2618548a464SMarkus Pargmann unsigned int port; 2628548a464SMarkus Pargmann unsigned int ptcr = 0; 2638548a464SMarkus Pargmann unsigned int pdcr = 0; 2648548a464SMarkus Pargmann unsigned int pcr = 0; 2658548a464SMarkus Pargmann unsigned int val; 2668548a464SMarkus Pargmann int ret; 2678548a464SMarkus Pargmann int i = 0; 2688548a464SMarkus Pargmann 2698548a464SMarkus Pargmann ret = of_property_read_u32(child, "fsl,audmux-port", &port); 2708548a464SMarkus Pargmann if (ret) { 27106d15a2eSRob Herring dev_warn(&pdev->dev, "Failed to get fsl,audmux-port of child node \"%pOF\"\n", 27206d15a2eSRob Herring child); 2738548a464SMarkus Pargmann continue; 2748548a464SMarkus Pargmann } 2758548a464SMarkus Pargmann if (!of_property_read_bool(child, "fsl,port-config")) { 27606d15a2eSRob Herring dev_warn(&pdev->dev, "child node \"%pOF\" does not have property fsl,port-config\n", 27706d15a2eSRob Herring child); 2788548a464SMarkus Pargmann continue; 2798548a464SMarkus Pargmann } 2808548a464SMarkus Pargmann 2818548a464SMarkus Pargmann for (i = 0; (ret = of_property_read_u32_index(child, 2829c0aeaa3SMarkus Pargmann "fsl,port-config", i, &val)) == 0; 2838548a464SMarkus Pargmann ++i) { 2848548a464SMarkus Pargmann if (audmux_type == IMX31_AUDMUX) { 2858548a464SMarkus Pargmann if (i % 2) 2868548a464SMarkus Pargmann pdcr |= val; 2878548a464SMarkus Pargmann else 2888548a464SMarkus Pargmann ptcr |= val; 2898548a464SMarkus Pargmann } else { 2908548a464SMarkus Pargmann pcr |= val; 2918548a464SMarkus Pargmann } 2928548a464SMarkus Pargmann } 2938548a464SMarkus Pargmann 2949c0aeaa3SMarkus Pargmann if (ret != -EOVERFLOW) { 29506d15a2eSRob Herring dev_err(&pdev->dev, "Failed to read u32 at index %d of child %pOF\n", 29606d15a2eSRob Herring i, child); 2978548a464SMarkus Pargmann continue; 2988548a464SMarkus Pargmann } 2998548a464SMarkus Pargmann 3008548a464SMarkus Pargmann if (audmux_type == IMX31_AUDMUX) { 3018548a464SMarkus Pargmann if (i % 2) { 30206d15a2eSRob Herring dev_err(&pdev->dev, "One pdcr value is missing in child node %pOF\n", 30306d15a2eSRob Herring child); 3048548a464SMarkus Pargmann continue; 3058548a464SMarkus Pargmann } 3068548a464SMarkus Pargmann imx_audmux_v2_configure_port(port, ptcr, pdcr); 3078548a464SMarkus Pargmann } else { 3088548a464SMarkus Pargmann imx_audmux_v1_configure_port(port, pcr); 3098548a464SMarkus Pargmann } 3108548a464SMarkus Pargmann } 3118548a464SMarkus Pargmann 3128548a464SMarkus Pargmann return 0; 3138548a464SMarkus Pargmann } 3148548a464SMarkus Pargmann 315a0a3d518SBill Pemberton static int imx_audmux_probe(struct platform_device *pdev) 316a23dc694SShawn Guo { 317a23dc694SShawn Guo struct resource *res; 318a23dc694SShawn Guo const struct of_device_id *of_id = 319a23dc694SShawn Guo of_match_device(imx_audmux_dt_ids, &pdev->dev); 320a23dc694SShawn Guo 321a23dc694SShawn Guo res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 322b25b5aa0SThierry Reding audmux_base = devm_ioremap_resource(&pdev->dev, res); 323b25b5aa0SThierry Reding if (IS_ERR(audmux_base)) 324b25b5aa0SThierry Reding return PTR_ERR(audmux_base); 325a23dc694SShawn Guo 326127c5cadSFabio Estevam audmux_clk = devm_clk_get(&pdev->dev, "audmux"); 327a23dc694SShawn Guo if (IS_ERR(audmux_clk)) { 328a23dc694SShawn Guo dev_dbg(&pdev->dev, "cannot get clock: %ld\n", 329a23dc694SShawn Guo PTR_ERR(audmux_clk)); 330a23dc694SShawn Guo audmux_clk = NULL; 331a23dc694SShawn Guo } 332a23dc694SShawn Guo 333a23dc694SShawn Guo if (of_id) 334a23dc694SShawn Guo pdev->id_entry = of_id->data; 335a23dc694SShawn Guo audmux_type = pdev->id_entry->driver_data; 336a23dc694SShawn Guo if (audmux_type == IMX31_AUDMUX) 337a23dc694SShawn Guo audmux_debugfs_init(); 338a23dc694SShawn Guo 339bae35992SFabio Estevam if (of_id) 3408548a464SMarkus Pargmann imx_audmux_parse_dt_defaults(pdev, pdev->dev.of_node); 3418548a464SMarkus Pargmann 342a23dc694SShawn Guo return 0; 343a23dc694SShawn Guo } 344a23dc694SShawn Guo 345a0a3d518SBill Pemberton static int imx_audmux_remove(struct platform_device *pdev) 346a23dc694SShawn Guo { 347a23dc694SShawn Guo if (audmux_type == IMX31_AUDMUX) 348a23dc694SShawn Guo audmux_debugfs_remove(); 349a23dc694SShawn Guo 350a23dc694SShawn Guo return 0; 351a23dc694SShawn Guo } 352a23dc694SShawn Guo 353a23dc694SShawn Guo static struct platform_driver imx_audmux_driver = { 354a23dc694SShawn Guo .probe = imx_audmux_probe, 355a0a3d518SBill Pemberton .remove = imx_audmux_remove, 356a23dc694SShawn Guo .id_table = imx_audmux_ids, 357a23dc694SShawn Guo .driver = { 358a23dc694SShawn Guo .name = DRIVER_NAME, 359a23dc694SShawn Guo .of_match_table = imx_audmux_dt_ids, 360a23dc694SShawn Guo } 361a23dc694SShawn Guo }; 362a23dc694SShawn Guo 363a23dc694SShawn Guo static int __init imx_audmux_init(void) 364a23dc694SShawn Guo { 365a23dc694SShawn Guo return platform_driver_register(&imx_audmux_driver); 366a23dc694SShawn Guo } 367a23dc694SShawn Guo subsys_initcall(imx_audmux_init); 368a23dc694SShawn Guo 369a23dc694SShawn Guo static void __exit imx_audmux_exit(void) 370a23dc694SShawn Guo { 371a23dc694SShawn Guo platform_driver_unregister(&imx_audmux_driver); 372a23dc694SShawn Guo } 373a23dc694SShawn Guo module_exit(imx_audmux_exit); 374a23dc694SShawn Guo 375a23dc694SShawn Guo MODULE_DESCRIPTION("Freescale i.MX AUDMUX driver"); 376a23dc694SShawn Guo MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>"); 377a23dc694SShawn Guo MODULE_LICENSE("GPL v2"); 378a23dc694SShawn Guo MODULE_ALIAS("platform:" DRIVER_NAME); 379