147bbcbfeSAndra Danciu // SPDX-License-Identifier: GPL-2.0+ 247bbcbfeSAndra Danciu // 347bbcbfeSAndra Danciu // Copyright 2012 Freescale Semiconductor, Inc. 447bbcbfeSAndra Danciu // Copyright 2012 Linaro Ltd. 547bbcbfeSAndra Danciu // Copyright 2009 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de> 647bbcbfeSAndra Danciu // 747bbcbfeSAndra Danciu // Initial development of this code was funded by 847bbcbfeSAndra Danciu // Phytec Messtechnik GmbH, http://www.phytec.de 9a23dc694SShawn Guo 10a23dc694SShawn Guo #include <linux/clk.h> 11a23dc694SShawn Guo #include <linux/debugfs.h> 12a23dc694SShawn Guo #include <linux/err.h> 13a23dc694SShawn Guo #include <linux/io.h> 14a23dc694SShawn Guo #include <linux/module.h> 15a23dc694SShawn Guo #include <linux/of.h> 16a23dc694SShawn Guo #include <linux/of_device.h> 17a23dc694SShawn Guo #include <linux/platform_device.h> 18a23dc694SShawn Guo #include <linux/slab.h> 19a23dc694SShawn Guo 20a23dc694SShawn Guo #include "imx-audmux.h" 21a23dc694SShawn Guo 22a23dc694SShawn Guo #define DRIVER_NAME "imx-audmux" 23a23dc694SShawn Guo 24a23dc694SShawn Guo static struct clk *audmux_clk; 25a23dc694SShawn Guo static void __iomem *audmux_base; 26a23dc694SShawn Guo 27a23dc694SShawn Guo #define IMX_AUDMUX_V2_PTCR(x) ((x) * 8) 28a23dc694SShawn Guo #define IMX_AUDMUX_V2_PDCR(x) ((x) * 8 + 4) 29a23dc694SShawn Guo 30a23dc694SShawn Guo #ifdef CONFIG_DEBUG_FS 31a23dc694SShawn Guo static struct dentry *audmux_debugfs_root; 32a23dc694SShawn Guo 33a23dc694SShawn Guo /* There is an annoying discontinuity in the SSI numbering with regard 34a23dc694SShawn Guo * to the Linux number of the devices */ 35a23dc694SShawn Guo static const char *audmux_port_string(int port) 36a23dc694SShawn Guo { 37a23dc694SShawn Guo switch (port) { 38a23dc694SShawn Guo case MX31_AUDMUX_PORT1_SSI0: 39a23dc694SShawn Guo return "imx-ssi.0"; 40a23dc694SShawn Guo case MX31_AUDMUX_PORT2_SSI1: 41a23dc694SShawn Guo return "imx-ssi.1"; 42a23dc694SShawn Guo case MX31_AUDMUX_PORT3_SSI_PINS_3: 43a23dc694SShawn Guo return "SSI3"; 44a23dc694SShawn Guo case MX31_AUDMUX_PORT4_SSI_PINS_4: 45a23dc694SShawn Guo return "SSI4"; 46a23dc694SShawn Guo case MX31_AUDMUX_PORT5_SSI_PINS_5: 47a23dc694SShawn Guo return "SSI5"; 48a23dc694SShawn Guo case MX31_AUDMUX_PORT6_SSI_PINS_6: 49a23dc694SShawn Guo return "SSI6"; 50a23dc694SShawn Guo default: 51a23dc694SShawn Guo return "UNKNOWN"; 52a23dc694SShawn Guo } 53a23dc694SShawn Guo } 54a23dc694SShawn Guo 55a23dc694SShawn Guo static ssize_t audmux_read_file(struct file *file, char __user *user_buf, 56a23dc694SShawn Guo size_t count, loff_t *ppos) 57a23dc694SShawn Guo { 58a23dc694SShawn Guo ssize_t ret; 591b3ed70aSFelipe Pena char *buf; 60e5f89768SMark Brown uintptr_t port = (uintptr_t)file->private_data; 61a23dc694SShawn Guo u32 pdcr, ptcr; 62a23dc694SShawn Guo 6372192366SFabio Estevam if (audmux_clk) { 6472192366SFabio Estevam ret = clk_prepare_enable(audmux_clk); 6572192366SFabio Estevam if (ret) 6672192366SFabio Estevam return ret; 6772192366SFabio Estevam } 68a23dc694SShawn Guo 69a23dc694SShawn Guo ptcr = readl(audmux_base + IMX_AUDMUX_V2_PTCR(port)); 70a23dc694SShawn Guo pdcr = readl(audmux_base + IMX_AUDMUX_V2_PDCR(port)); 71a23dc694SShawn Guo 72a23dc694SShawn Guo if (audmux_clk) 73a23dc694SShawn Guo clk_disable_unprepare(audmux_clk); 74a23dc694SShawn Guo 751b3ed70aSFelipe Pena buf = kmalloc(PAGE_SIZE, GFP_KERNEL); 761b3ed70aSFelipe Pena if (!buf) 771b3ed70aSFelipe Pena return -ENOMEM; 781b3ed70aSFelipe Pena 79c407cd00SSilvio Cesare ret = scnprintf(buf, PAGE_SIZE, "PDCR: %08x\nPTCR: %08x\n", 80a23dc694SShawn Guo pdcr, ptcr); 81a23dc694SShawn Guo 82a23dc694SShawn Guo if (ptcr & IMX_AUDMUX_V2_PTCR_TFSDIR) 83c407cd00SSilvio Cesare ret += scnprintf(buf + ret, PAGE_SIZE - ret, 84a23dc694SShawn Guo "TxFS output from %s, ", 85a23dc694SShawn Guo audmux_port_string((ptcr >> 27) & 0x7)); 86a23dc694SShawn Guo else 87c407cd00SSilvio Cesare ret += scnprintf(buf + ret, PAGE_SIZE - ret, 88a23dc694SShawn Guo "TxFS input, "); 89a23dc694SShawn Guo 90a23dc694SShawn Guo if (ptcr & IMX_AUDMUX_V2_PTCR_TCLKDIR) 91c407cd00SSilvio Cesare ret += scnprintf(buf + ret, PAGE_SIZE - ret, 92a23dc694SShawn Guo "TxClk output from %s", 93a23dc694SShawn Guo audmux_port_string((ptcr >> 22) & 0x7)); 94a23dc694SShawn Guo else 95c407cd00SSilvio Cesare ret += scnprintf(buf + ret, PAGE_SIZE - ret, 96a23dc694SShawn Guo "TxClk input"); 97a23dc694SShawn Guo 98c407cd00SSilvio Cesare ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n"); 99a23dc694SShawn Guo 100a23dc694SShawn Guo if (ptcr & IMX_AUDMUX_V2_PTCR_SYN) { 101c407cd00SSilvio Cesare ret += scnprintf(buf + ret, PAGE_SIZE - ret, 102a23dc694SShawn Guo "Port is symmetric"); 103a23dc694SShawn Guo } else { 104a23dc694SShawn Guo if (ptcr & IMX_AUDMUX_V2_PTCR_RFSDIR) 105c407cd00SSilvio Cesare ret += scnprintf(buf + ret, PAGE_SIZE - ret, 106a23dc694SShawn Guo "RxFS output from %s, ", 107a23dc694SShawn Guo audmux_port_string((ptcr >> 17) & 0x7)); 108a23dc694SShawn Guo else 109c407cd00SSilvio Cesare ret += scnprintf(buf + ret, PAGE_SIZE - ret, 110a23dc694SShawn Guo "RxFS input, "); 111a23dc694SShawn Guo 112a23dc694SShawn Guo if (ptcr & IMX_AUDMUX_V2_PTCR_RCLKDIR) 113c407cd00SSilvio Cesare ret += scnprintf(buf + ret, PAGE_SIZE - ret, 114a23dc694SShawn Guo "RxClk output from %s", 115a23dc694SShawn Guo audmux_port_string((ptcr >> 12) & 0x7)); 116a23dc694SShawn Guo else 117c407cd00SSilvio Cesare ret += scnprintf(buf + ret, PAGE_SIZE - ret, 118a23dc694SShawn Guo "RxClk input"); 119a23dc694SShawn Guo } 120a23dc694SShawn Guo 121c407cd00SSilvio Cesare ret += scnprintf(buf + ret, PAGE_SIZE - ret, 122a23dc694SShawn Guo "\nData received from %s\n", 123a23dc694SShawn Guo audmux_port_string((pdcr >> 13) & 0x7)); 124a23dc694SShawn Guo 125a23dc694SShawn Guo ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); 126a23dc694SShawn Guo 127a23dc694SShawn Guo kfree(buf); 128a23dc694SShawn Guo 129a23dc694SShawn Guo return ret; 130a23dc694SShawn Guo } 131a23dc694SShawn Guo 132a23dc694SShawn Guo static const struct file_operations audmux_debugfs_fops = { 1331eecb828SMark Brown .open = simple_open, 134a23dc694SShawn Guo .read = audmux_read_file, 135a23dc694SShawn Guo .llseek = default_llseek, 136a23dc694SShawn Guo }; 137a23dc694SShawn Guo 138b8909783SLars-Peter Clausen static void audmux_debugfs_init(void) 139a23dc694SShawn Guo { 140e5f89768SMark Brown uintptr_t i; 141a23dc694SShawn Guo char buf[20]; 142a23dc694SShawn Guo 143a23dc694SShawn Guo audmux_debugfs_root = debugfs_create_dir("audmux", NULL); 144a23dc694SShawn Guo 145409b78ccSTorben Hohn for (i = 0; i < MX31_AUDMUX_PORT7_SSI_PINS_7 + 1; i++) { 146e5f89768SMark Brown snprintf(buf, sizeof(buf), "ssi%lu", i); 147*227ab8baSGreg Kroah-Hartman debugfs_create_file(buf, 0444, audmux_debugfs_root, 148*227ab8baSGreg Kroah-Hartman (void *)i, &audmux_debugfs_fops); 149a23dc694SShawn Guo } 150a23dc694SShawn Guo } 151a23dc694SShawn Guo 152a0a3d518SBill Pemberton static void audmux_debugfs_remove(void) 153a23dc694SShawn Guo { 154a23dc694SShawn Guo debugfs_remove_recursive(audmux_debugfs_root); 155a23dc694SShawn Guo } 156a23dc694SShawn Guo #else 157a23dc694SShawn Guo static inline void audmux_debugfs_init(void) 158a23dc694SShawn Guo { 159a23dc694SShawn Guo } 160a23dc694SShawn Guo 161a23dc694SShawn Guo static inline void audmux_debugfs_remove(void) 162a23dc694SShawn Guo { 163a23dc694SShawn Guo } 164a23dc694SShawn Guo #endif 165a23dc694SShawn Guo 166a10807aeSFabio Estevam static enum imx_audmux_type { 167a23dc694SShawn Guo IMX21_AUDMUX, 168a23dc694SShawn Guo IMX31_AUDMUX, 169a23dc694SShawn Guo } audmux_type; 170a23dc694SShawn Guo 171e51cebf7SKrzysztof Kozlowski static const struct platform_device_id imx_audmux_ids[] = { 172a23dc694SShawn Guo { 173a23dc694SShawn Guo .name = "imx21-audmux", 174a23dc694SShawn Guo .driver_data = IMX21_AUDMUX, 175a23dc694SShawn Guo }, { 176a23dc694SShawn Guo .name = "imx31-audmux", 177a23dc694SShawn Guo .driver_data = IMX31_AUDMUX, 178a23dc694SShawn Guo }, { 179a23dc694SShawn Guo /* sentinel */ 180a23dc694SShawn Guo } 181a23dc694SShawn Guo }; 182a23dc694SShawn Guo MODULE_DEVICE_TABLE(platform, imx_audmux_ids); 183a23dc694SShawn Guo 184a23dc694SShawn Guo static const struct of_device_id imx_audmux_dt_ids[] = { 185a23dc694SShawn Guo { .compatible = "fsl,imx21-audmux", .data = &imx_audmux_ids[0], }, 186a23dc694SShawn Guo { .compatible = "fsl,imx31-audmux", .data = &imx_audmux_ids[1], }, 187a23dc694SShawn Guo { /* sentinel */ } 188a23dc694SShawn Guo }; 189a23dc694SShawn Guo MODULE_DEVICE_TABLE(of, imx_audmux_dt_ids); 190a23dc694SShawn Guo 191a23dc694SShawn Guo static const uint8_t port_mapping[] = { 192a23dc694SShawn Guo 0x0, 0x4, 0x8, 0x10, 0x14, 0x1c, 193a23dc694SShawn Guo }; 194a23dc694SShawn Guo 195a23dc694SShawn Guo int imx_audmux_v1_configure_port(unsigned int port, unsigned int pcr) 196a23dc694SShawn Guo { 197a23dc694SShawn Guo if (audmux_type != IMX21_AUDMUX) 198a23dc694SShawn Guo return -EINVAL; 199a23dc694SShawn Guo 200a23dc694SShawn Guo if (!audmux_base) 201a23dc694SShawn Guo return -ENOSYS; 202a23dc694SShawn Guo 203a23dc694SShawn Guo if (port >= ARRAY_SIZE(port_mapping)) 204a23dc694SShawn Guo return -EINVAL; 205a23dc694SShawn Guo 206a23dc694SShawn Guo writel(pcr, audmux_base + port_mapping[port]); 207a23dc694SShawn Guo 208a23dc694SShawn Guo return 0; 209a23dc694SShawn Guo } 210a23dc694SShawn Guo EXPORT_SYMBOL_GPL(imx_audmux_v1_configure_port); 211a23dc694SShawn Guo 212a23dc694SShawn Guo int imx_audmux_v2_configure_port(unsigned int port, unsigned int ptcr, 213a23dc694SShawn Guo unsigned int pdcr) 214a23dc694SShawn Guo { 21572192366SFabio Estevam int ret; 21672192366SFabio Estevam 217a23dc694SShawn Guo if (audmux_type != IMX31_AUDMUX) 218a23dc694SShawn Guo return -EINVAL; 219a23dc694SShawn Guo 220a23dc694SShawn Guo if (!audmux_base) 221a23dc694SShawn Guo return -ENOSYS; 222a23dc694SShawn Guo 22372192366SFabio Estevam if (audmux_clk) { 22472192366SFabio Estevam ret = clk_prepare_enable(audmux_clk); 22572192366SFabio Estevam if (ret) 22672192366SFabio Estevam return ret; 22772192366SFabio Estevam } 228a23dc694SShawn Guo 229a23dc694SShawn Guo writel(ptcr, audmux_base + IMX_AUDMUX_V2_PTCR(port)); 230a23dc694SShawn Guo writel(pdcr, audmux_base + IMX_AUDMUX_V2_PDCR(port)); 231a23dc694SShawn Guo 232a23dc694SShawn Guo if (audmux_clk) 233a23dc694SShawn Guo clk_disable_unprepare(audmux_clk); 234a23dc694SShawn Guo 235a23dc694SShawn Guo return 0; 236a23dc694SShawn Guo } 237a23dc694SShawn Guo EXPORT_SYMBOL_GPL(imx_audmux_v2_configure_port); 238a23dc694SShawn Guo 2398548a464SMarkus Pargmann static int imx_audmux_parse_dt_defaults(struct platform_device *pdev, 2408548a464SMarkus Pargmann struct device_node *of_node) 2418548a464SMarkus Pargmann { 2428548a464SMarkus Pargmann struct device_node *child; 2438548a464SMarkus Pargmann 2448548a464SMarkus Pargmann for_each_available_child_of_node(of_node, child) { 2458548a464SMarkus Pargmann unsigned int port; 2468548a464SMarkus Pargmann unsigned int ptcr = 0; 2478548a464SMarkus Pargmann unsigned int pdcr = 0; 2488548a464SMarkus Pargmann unsigned int pcr = 0; 2498548a464SMarkus Pargmann unsigned int val; 2508548a464SMarkus Pargmann int ret; 2518548a464SMarkus Pargmann int i = 0; 2528548a464SMarkus Pargmann 2538548a464SMarkus Pargmann ret = of_property_read_u32(child, "fsl,audmux-port", &port); 2548548a464SMarkus Pargmann if (ret) { 25506d15a2eSRob Herring dev_warn(&pdev->dev, "Failed to get fsl,audmux-port of child node \"%pOF\"\n", 25606d15a2eSRob Herring child); 2578548a464SMarkus Pargmann continue; 2588548a464SMarkus Pargmann } 2598548a464SMarkus Pargmann if (!of_property_read_bool(child, "fsl,port-config")) { 26006d15a2eSRob Herring dev_warn(&pdev->dev, "child node \"%pOF\" does not have property fsl,port-config\n", 26106d15a2eSRob Herring child); 2628548a464SMarkus Pargmann continue; 2638548a464SMarkus Pargmann } 2648548a464SMarkus Pargmann 2658548a464SMarkus Pargmann for (i = 0; (ret = of_property_read_u32_index(child, 2669c0aeaa3SMarkus Pargmann "fsl,port-config", i, &val)) == 0; 2678548a464SMarkus Pargmann ++i) { 2688548a464SMarkus Pargmann if (audmux_type == IMX31_AUDMUX) { 2698548a464SMarkus Pargmann if (i % 2) 2708548a464SMarkus Pargmann pdcr |= val; 2718548a464SMarkus Pargmann else 2728548a464SMarkus Pargmann ptcr |= val; 2738548a464SMarkus Pargmann } else { 2748548a464SMarkus Pargmann pcr |= val; 2758548a464SMarkus Pargmann } 2768548a464SMarkus Pargmann } 2778548a464SMarkus Pargmann 2789c0aeaa3SMarkus Pargmann if (ret != -EOVERFLOW) { 27906d15a2eSRob Herring dev_err(&pdev->dev, "Failed to read u32 at index %d of child %pOF\n", 28006d15a2eSRob Herring i, child); 2818548a464SMarkus Pargmann continue; 2828548a464SMarkus Pargmann } 2838548a464SMarkus Pargmann 2848548a464SMarkus Pargmann if (audmux_type == IMX31_AUDMUX) { 2858548a464SMarkus Pargmann if (i % 2) { 28606d15a2eSRob Herring dev_err(&pdev->dev, "One pdcr value is missing in child node %pOF\n", 28706d15a2eSRob Herring child); 2888548a464SMarkus Pargmann continue; 2898548a464SMarkus Pargmann } 2908548a464SMarkus Pargmann imx_audmux_v2_configure_port(port, ptcr, pdcr); 2918548a464SMarkus Pargmann } else { 2928548a464SMarkus Pargmann imx_audmux_v1_configure_port(port, pcr); 2938548a464SMarkus Pargmann } 2948548a464SMarkus Pargmann } 2958548a464SMarkus Pargmann 2968548a464SMarkus Pargmann return 0; 2978548a464SMarkus Pargmann } 2988548a464SMarkus Pargmann 299a0a3d518SBill Pemberton static int imx_audmux_probe(struct platform_device *pdev) 300a23dc694SShawn Guo { 301a23dc694SShawn Guo struct resource *res; 302a23dc694SShawn Guo const struct of_device_id *of_id = 303a23dc694SShawn Guo of_match_device(imx_audmux_dt_ids, &pdev->dev); 304a23dc694SShawn Guo 305a23dc694SShawn Guo res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 306b25b5aa0SThierry Reding audmux_base = devm_ioremap_resource(&pdev->dev, res); 307b25b5aa0SThierry Reding if (IS_ERR(audmux_base)) 308b25b5aa0SThierry Reding return PTR_ERR(audmux_base); 309a23dc694SShawn Guo 310127c5cadSFabio Estevam audmux_clk = devm_clk_get(&pdev->dev, "audmux"); 311a23dc694SShawn Guo if (IS_ERR(audmux_clk)) { 312a23dc694SShawn Guo dev_dbg(&pdev->dev, "cannot get clock: %ld\n", 313a23dc694SShawn Guo PTR_ERR(audmux_clk)); 314a23dc694SShawn Guo audmux_clk = NULL; 315a23dc694SShawn Guo } 316a23dc694SShawn Guo 317a23dc694SShawn Guo if (of_id) 318a23dc694SShawn Guo pdev->id_entry = of_id->data; 319a23dc694SShawn Guo audmux_type = pdev->id_entry->driver_data; 320a23dc694SShawn Guo if (audmux_type == IMX31_AUDMUX) 321a23dc694SShawn Guo audmux_debugfs_init(); 322a23dc694SShawn Guo 323bae35992SFabio Estevam if (of_id) 3248548a464SMarkus Pargmann imx_audmux_parse_dt_defaults(pdev, pdev->dev.of_node); 3258548a464SMarkus Pargmann 326a23dc694SShawn Guo return 0; 327a23dc694SShawn Guo } 328a23dc694SShawn Guo 329a0a3d518SBill Pemberton static int imx_audmux_remove(struct platform_device *pdev) 330a23dc694SShawn Guo { 331a23dc694SShawn Guo if (audmux_type == IMX31_AUDMUX) 332a23dc694SShawn Guo audmux_debugfs_remove(); 333a23dc694SShawn Guo 334a23dc694SShawn Guo return 0; 335a23dc694SShawn Guo } 336a23dc694SShawn Guo 337a23dc694SShawn Guo static struct platform_driver imx_audmux_driver = { 338a23dc694SShawn Guo .probe = imx_audmux_probe, 339a0a3d518SBill Pemberton .remove = imx_audmux_remove, 340a23dc694SShawn Guo .id_table = imx_audmux_ids, 341a23dc694SShawn Guo .driver = { 342a23dc694SShawn Guo .name = DRIVER_NAME, 343a23dc694SShawn Guo .of_match_table = imx_audmux_dt_ids, 344a23dc694SShawn Guo } 345a23dc694SShawn Guo }; 346a23dc694SShawn Guo 347a23dc694SShawn Guo static int __init imx_audmux_init(void) 348a23dc694SShawn Guo { 349a23dc694SShawn Guo return platform_driver_register(&imx_audmux_driver); 350a23dc694SShawn Guo } 351a23dc694SShawn Guo subsys_initcall(imx_audmux_init); 352a23dc694SShawn Guo 353a23dc694SShawn Guo static void __exit imx_audmux_exit(void) 354a23dc694SShawn Guo { 355a23dc694SShawn Guo platform_driver_unregister(&imx_audmux_driver); 356a23dc694SShawn Guo } 357a23dc694SShawn Guo module_exit(imx_audmux_exit); 358a23dc694SShawn Guo 359a23dc694SShawn Guo MODULE_DESCRIPTION("Freescale i.MX AUDMUX driver"); 360a23dc694SShawn Guo MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>"); 361a23dc694SShawn Guo MODULE_LICENSE("GPL v2"); 362a23dc694SShawn Guo MODULE_ALIAS("platform:" DRIVER_NAME); 363