Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 //
0003 // Copyright 2012 Freescale Semiconductor, Inc.
0004 // Copyright 2012 Linaro Ltd.
0005 // Copyright 2009 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de>
0006 //
0007 // Initial development of this code was funded by
0008 // Phytec Messtechnik GmbH, https://www.phytec.de
0009 
0010 #include <linux/clk.h>
0011 #include <linux/debugfs.h>
0012 #include <linux/err.h>
0013 #include <linux/io.h>
0014 #include <linux/module.h>
0015 #include <linux/of.h>
0016 #include <linux/of_device.h>
0017 #include <linux/platform_device.h>
0018 #include <linux/slab.h>
0019 
0020 #include "imx-audmux.h"
0021 
0022 #define DRIVER_NAME "imx-audmux"
0023 
0024 static struct clk *audmux_clk;
0025 static void __iomem *audmux_base;
0026 static u32 *regcache;
0027 static u32 reg_max;
0028 
0029 #define IMX_AUDMUX_V2_PTCR(x)       ((x) * 8)
0030 #define IMX_AUDMUX_V2_PDCR(x)       ((x) * 8 + 4)
0031 
0032 #ifdef CONFIG_DEBUG_FS
0033 static struct dentry *audmux_debugfs_root;
0034 
0035 /* There is an annoying discontinuity in the SSI numbering with regard
0036  * to the Linux number of the devices */
0037 static const char *audmux_port_string(int port)
0038 {
0039     switch (port) {
0040     case MX31_AUDMUX_PORT1_SSI0:
0041         return "imx-ssi.0";
0042     case MX31_AUDMUX_PORT2_SSI1:
0043         return "imx-ssi.1";
0044     case MX31_AUDMUX_PORT3_SSI_PINS_3:
0045         return "SSI3";
0046     case MX31_AUDMUX_PORT4_SSI_PINS_4:
0047         return "SSI4";
0048     case MX31_AUDMUX_PORT5_SSI_PINS_5:
0049         return "SSI5";
0050     case MX31_AUDMUX_PORT6_SSI_PINS_6:
0051         return "SSI6";
0052     default:
0053         return "UNKNOWN";
0054     }
0055 }
0056 
0057 static ssize_t audmux_read_file(struct file *file, char __user *user_buf,
0058                 size_t count, loff_t *ppos)
0059 {
0060     ssize_t ret;
0061     char *buf;
0062     uintptr_t port = (uintptr_t)file->private_data;
0063     u32 pdcr, ptcr;
0064 
0065     ret = clk_prepare_enable(audmux_clk);
0066     if (ret)
0067         return ret;
0068 
0069     ptcr = readl(audmux_base + IMX_AUDMUX_V2_PTCR(port));
0070     pdcr = readl(audmux_base + IMX_AUDMUX_V2_PDCR(port));
0071 
0072     clk_disable_unprepare(audmux_clk);
0073 
0074     buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
0075     if (!buf)
0076         return -ENOMEM;
0077 
0078     ret = scnprintf(buf, PAGE_SIZE, "PDCR: %08x\nPTCR: %08x\n",
0079                pdcr, ptcr);
0080 
0081     if (ptcr & IMX_AUDMUX_V2_PTCR_TFSDIR)
0082         ret += scnprintf(buf + ret, PAGE_SIZE - ret,
0083                 "TxFS output from %s, ",
0084                 audmux_port_string((ptcr >> 27) & 0x7));
0085     else
0086         ret += scnprintf(buf + ret, PAGE_SIZE - ret,
0087                 "TxFS input, ");
0088 
0089     if (ptcr & IMX_AUDMUX_V2_PTCR_TCLKDIR)
0090         ret += scnprintf(buf + ret, PAGE_SIZE - ret,
0091                 "TxClk output from %s",
0092                 audmux_port_string((ptcr >> 22) & 0x7));
0093     else
0094         ret += scnprintf(buf + ret, PAGE_SIZE - ret,
0095                 "TxClk input");
0096 
0097     ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
0098 
0099     if (ptcr & IMX_AUDMUX_V2_PTCR_SYN) {
0100         ret += scnprintf(buf + ret, PAGE_SIZE - ret,
0101                 "Port is symmetric");
0102     } else {
0103         if (ptcr & IMX_AUDMUX_V2_PTCR_RFSDIR)
0104             ret += scnprintf(buf + ret, PAGE_SIZE - ret,
0105                     "RxFS output from %s, ",
0106                     audmux_port_string((ptcr >> 17) & 0x7));
0107         else
0108             ret += scnprintf(buf + ret, PAGE_SIZE - ret,
0109                     "RxFS input, ");
0110 
0111         if (ptcr & IMX_AUDMUX_V2_PTCR_RCLKDIR)
0112             ret += scnprintf(buf + ret, PAGE_SIZE - ret,
0113                     "RxClk output from %s",
0114                     audmux_port_string((ptcr >> 12) & 0x7));
0115         else
0116             ret += scnprintf(buf + ret, PAGE_SIZE - ret,
0117                     "RxClk input");
0118     }
0119 
0120     ret += scnprintf(buf + ret, PAGE_SIZE - ret,
0121             "\nData received from %s\n",
0122             audmux_port_string((pdcr >> 13) & 0x7));
0123 
0124     ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
0125 
0126     kfree(buf);
0127 
0128     return ret;
0129 }
0130 
0131 static const struct file_operations audmux_debugfs_fops = {
0132     .open = simple_open,
0133     .read = audmux_read_file,
0134     .llseek = default_llseek,
0135 };
0136 
0137 static void audmux_debugfs_init(void)
0138 {
0139     uintptr_t i;
0140     char buf[20];
0141 
0142     audmux_debugfs_root = debugfs_create_dir("audmux", NULL);
0143 
0144     for (i = 0; i < MX31_AUDMUX_PORT7_SSI_PINS_7 + 1; i++) {
0145         snprintf(buf, sizeof(buf), "ssi%lu", i);
0146         debugfs_create_file(buf, 0444, audmux_debugfs_root,
0147                     (void *)i, &audmux_debugfs_fops);
0148     }
0149 }
0150 
0151 static void audmux_debugfs_remove(void)
0152 {
0153     debugfs_remove_recursive(audmux_debugfs_root);
0154 }
0155 #else
0156 static inline void audmux_debugfs_init(void)
0157 {
0158 }
0159 
0160 static inline void audmux_debugfs_remove(void)
0161 {
0162 }
0163 #endif
0164 
0165 static enum imx_audmux_type {
0166     IMX21_AUDMUX,
0167     IMX31_AUDMUX,
0168 } audmux_type;
0169 
0170 static const struct of_device_id imx_audmux_dt_ids[] = {
0171     { .compatible = "fsl,imx21-audmux", .data = (void *)IMX21_AUDMUX, },
0172     { .compatible = "fsl,imx31-audmux", .data = (void *)IMX31_AUDMUX, },
0173     { /* sentinel */ }
0174 };
0175 MODULE_DEVICE_TABLE(of, imx_audmux_dt_ids);
0176 
0177 static const uint8_t port_mapping[] = {
0178     0x0, 0x4, 0x8, 0x10, 0x14, 0x1c,
0179 };
0180 
0181 int imx_audmux_v1_configure_port(unsigned int port, unsigned int pcr)
0182 {
0183     if (audmux_type != IMX21_AUDMUX)
0184         return -EINVAL;
0185 
0186     if (!audmux_base)
0187         return -ENOSYS;
0188 
0189     if (port >= ARRAY_SIZE(port_mapping))
0190         return -EINVAL;
0191 
0192     writel(pcr, audmux_base + port_mapping[port]);
0193 
0194     return 0;
0195 }
0196 EXPORT_SYMBOL_GPL(imx_audmux_v1_configure_port);
0197 
0198 int imx_audmux_v2_configure_port(unsigned int port, unsigned int ptcr,
0199         unsigned int pdcr)
0200 {
0201     int ret;
0202 
0203     if (audmux_type != IMX31_AUDMUX)
0204         return -EINVAL;
0205 
0206     if (!audmux_base)
0207         return -ENOSYS;
0208 
0209     ret = clk_prepare_enable(audmux_clk);
0210     if (ret)
0211         return ret;
0212 
0213     writel(ptcr, audmux_base + IMX_AUDMUX_V2_PTCR(port));
0214     writel(pdcr, audmux_base + IMX_AUDMUX_V2_PDCR(port));
0215 
0216     clk_disable_unprepare(audmux_clk);
0217 
0218     return 0;
0219 }
0220 EXPORT_SYMBOL_GPL(imx_audmux_v2_configure_port);
0221 
0222 static int imx_audmux_parse_dt_defaults(struct platform_device *pdev,
0223         struct device_node *of_node)
0224 {
0225     struct device_node *child;
0226 
0227     for_each_available_child_of_node(of_node, child) {
0228         unsigned int port;
0229         unsigned int ptcr = 0;
0230         unsigned int pdcr = 0;
0231         unsigned int pcr = 0;
0232         unsigned int val;
0233         int ret;
0234         int i = 0;
0235 
0236         ret = of_property_read_u32(child, "fsl,audmux-port", &port);
0237         if (ret) {
0238             dev_warn(&pdev->dev, "Failed to get fsl,audmux-port of child node \"%pOF\"\n",
0239                     child);
0240             continue;
0241         }
0242         if (!of_property_read_bool(child, "fsl,port-config")) {
0243             dev_warn(&pdev->dev, "child node \"%pOF\" does not have property fsl,port-config\n",
0244                     child);
0245             continue;
0246         }
0247 
0248         for (i = 0; (ret = of_property_read_u32_index(child,
0249                     "fsl,port-config", i, &val)) == 0;
0250                 ++i) {
0251             if (audmux_type == IMX31_AUDMUX) {
0252                 if (i % 2)
0253                     pdcr |= val;
0254                 else
0255                     ptcr |= val;
0256             } else {
0257                 pcr |= val;
0258             }
0259         }
0260 
0261         if (ret != -EOVERFLOW) {
0262             dev_err(&pdev->dev, "Failed to read u32 at index %d of child %pOF\n",
0263                     i, child);
0264             continue;
0265         }
0266 
0267         if (audmux_type == IMX31_AUDMUX) {
0268             if (i % 2) {
0269                 dev_err(&pdev->dev, "One pdcr value is missing in child node %pOF\n",
0270                         child);
0271                 continue;
0272             }
0273             imx_audmux_v2_configure_port(port, ptcr, pdcr);
0274         } else {
0275             imx_audmux_v1_configure_port(port, pcr);
0276         }
0277     }
0278 
0279     return 0;
0280 }
0281 
0282 static int imx_audmux_probe(struct platform_device *pdev)
0283 {
0284     audmux_base = devm_platform_ioremap_resource(pdev, 0);
0285     if (IS_ERR(audmux_base))
0286         return PTR_ERR(audmux_base);
0287 
0288     audmux_clk = devm_clk_get(&pdev->dev, "audmux");
0289     if (IS_ERR(audmux_clk)) {
0290         dev_dbg(&pdev->dev, "cannot get clock: %ld\n",
0291                 PTR_ERR(audmux_clk));
0292         audmux_clk = NULL;
0293     }
0294 
0295     audmux_type = (uintptr_t)of_device_get_match_data(&pdev->dev);
0296 
0297     switch (audmux_type) {
0298     case IMX31_AUDMUX:
0299         audmux_debugfs_init();
0300         reg_max = 14;
0301         break;
0302     case IMX21_AUDMUX:
0303         reg_max = 6;
0304         break;
0305     default:
0306         dev_err(&pdev->dev, "unsupported version!\n");
0307         return -EINVAL;
0308     }
0309 
0310     regcache = devm_kzalloc(&pdev->dev, sizeof(u32) * reg_max, GFP_KERNEL);
0311     if (!regcache)
0312         return -ENOMEM;
0313 
0314     imx_audmux_parse_dt_defaults(pdev, pdev->dev.of_node);
0315 
0316     return 0;
0317 }
0318 
0319 static int imx_audmux_remove(struct platform_device *pdev)
0320 {
0321     if (audmux_type == IMX31_AUDMUX)
0322         audmux_debugfs_remove();
0323 
0324     return 0;
0325 }
0326 
0327 #ifdef CONFIG_PM_SLEEP
0328 static int imx_audmux_suspend(struct device *dev)
0329 {
0330     int i;
0331 
0332     clk_prepare_enable(audmux_clk);
0333 
0334     for (i = 0; i < reg_max; i++)
0335         regcache[i] = readl(audmux_base + i * 4);
0336 
0337     clk_disable_unprepare(audmux_clk);
0338 
0339     return 0;
0340 }
0341 
0342 static int imx_audmux_resume(struct device *dev)
0343 {
0344     int i;
0345 
0346     clk_prepare_enable(audmux_clk);
0347 
0348     for (i = 0; i < reg_max; i++)
0349         writel(regcache[i], audmux_base + i * 4);
0350 
0351     clk_disable_unprepare(audmux_clk);
0352 
0353     return 0;
0354 }
0355 #endif /* CONFIG_PM_SLEEP */
0356 
0357 static const struct dev_pm_ops imx_audmux_pm = {
0358     SET_SYSTEM_SLEEP_PM_OPS(imx_audmux_suspend, imx_audmux_resume)
0359 };
0360 
0361 static struct platform_driver imx_audmux_driver = {
0362     .probe      = imx_audmux_probe,
0363     .remove     = imx_audmux_remove,
0364     .driver = {
0365         .name   = DRIVER_NAME,
0366         .pm = &imx_audmux_pm,
0367         .of_match_table = imx_audmux_dt_ids,
0368     }
0369 };
0370 
0371 static int __init imx_audmux_init(void)
0372 {
0373     return platform_driver_register(&imx_audmux_driver);
0374 }
0375 subsys_initcall(imx_audmux_init);
0376 
0377 static void __exit imx_audmux_exit(void)
0378 {
0379     platform_driver_unregister(&imx_audmux_driver);
0380 }
0381 module_exit(imx_audmux_exit);
0382 
0383 MODULE_DESCRIPTION("Freescale i.MX AUDMUX driver");
0384 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
0385 MODULE_LICENSE("GPL v2");
0386 MODULE_ALIAS("platform:" DRIVER_NAME);