Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Atmel SSC driver
0004  *
0005  * Copyright (C) 2007 Atmel Corporation
0006  */
0007 
0008 #include <linux/platform_device.h>
0009 #include <linux/list.h>
0010 #include <linux/clk.h>
0011 #include <linux/err.h>
0012 #include <linux/io.h>
0013 #include <linux/mutex.h>
0014 #include <linux/atmel-ssc.h>
0015 #include <linux/slab.h>
0016 #include <linux/module.h>
0017 
0018 #include <linux/of.h>
0019 
0020 #include "../../sound/soc/atmel/atmel_ssc_dai.h"
0021 
0022 /* Serialize access to ssc_list and user count */
0023 static DEFINE_MUTEX(user_lock);
0024 static LIST_HEAD(ssc_list);
0025 
0026 struct ssc_device *ssc_request(unsigned int ssc_num)
0027 {
0028     int ssc_valid = 0;
0029     struct ssc_device *ssc;
0030 
0031     mutex_lock(&user_lock);
0032     list_for_each_entry(ssc, &ssc_list, list) {
0033         if (ssc->pdev->dev.of_node) {
0034             if (of_alias_get_id(ssc->pdev->dev.of_node, "ssc")
0035                 == ssc_num) {
0036                 ssc->pdev->id = ssc_num;
0037                 ssc_valid = 1;
0038                 break;
0039             }
0040         } else if (ssc->pdev->id == ssc_num) {
0041             ssc_valid = 1;
0042             break;
0043         }
0044     }
0045 
0046     if (!ssc_valid) {
0047         mutex_unlock(&user_lock);
0048         pr_err("ssc: ssc%d platform device is missing\n", ssc_num);
0049         return ERR_PTR(-ENODEV);
0050     }
0051 
0052     if (ssc->user) {
0053         mutex_unlock(&user_lock);
0054         dev_dbg(&ssc->pdev->dev, "module busy\n");
0055         return ERR_PTR(-EBUSY);
0056     }
0057     ssc->user++;
0058     mutex_unlock(&user_lock);
0059 
0060     clk_prepare(ssc->clk);
0061 
0062     return ssc;
0063 }
0064 EXPORT_SYMBOL(ssc_request);
0065 
0066 void ssc_free(struct ssc_device *ssc)
0067 {
0068     bool disable_clk = true;
0069 
0070     mutex_lock(&user_lock);
0071     if (ssc->user)
0072         ssc->user--;
0073     else {
0074         disable_clk = false;
0075         dev_dbg(&ssc->pdev->dev, "device already free\n");
0076     }
0077     mutex_unlock(&user_lock);
0078 
0079     if (disable_clk)
0080         clk_unprepare(ssc->clk);
0081 }
0082 EXPORT_SYMBOL(ssc_free);
0083 
0084 static struct atmel_ssc_platform_data at91rm9200_config = {
0085     .use_dma = 0,
0086     .has_fslen_ext = 0,
0087 };
0088 
0089 static struct atmel_ssc_platform_data at91sam9rl_config = {
0090     .use_dma = 0,
0091     .has_fslen_ext = 1,
0092 };
0093 
0094 static struct atmel_ssc_platform_data at91sam9g45_config = {
0095     .use_dma = 1,
0096     .has_fslen_ext = 1,
0097 };
0098 
0099 static const struct platform_device_id atmel_ssc_devtypes[] = {
0100     {
0101         .name = "at91rm9200_ssc",
0102         .driver_data = (unsigned long) &at91rm9200_config,
0103     }, {
0104         .name = "at91sam9rl_ssc",
0105         .driver_data = (unsigned long) &at91sam9rl_config,
0106     }, {
0107         .name = "at91sam9g45_ssc",
0108         .driver_data = (unsigned long) &at91sam9g45_config,
0109     }, {
0110         /* sentinel */
0111     }
0112 };
0113 
0114 #ifdef CONFIG_OF
0115 static const struct of_device_id atmel_ssc_dt_ids[] = {
0116     {
0117         .compatible = "atmel,at91rm9200-ssc",
0118         .data = &at91rm9200_config,
0119     }, {
0120         .compatible = "atmel,at91sam9rl-ssc",
0121         .data = &at91sam9rl_config,
0122     }, {
0123         .compatible = "atmel,at91sam9g45-ssc",
0124         .data = &at91sam9g45_config,
0125     }, {
0126         /* sentinel */
0127     }
0128 };
0129 MODULE_DEVICE_TABLE(of, atmel_ssc_dt_ids);
0130 #endif
0131 
0132 static inline const struct atmel_ssc_platform_data *
0133     atmel_ssc_get_driver_data(struct platform_device *pdev)
0134 {
0135     if (pdev->dev.of_node) {
0136         const struct of_device_id *match;
0137         match = of_match_node(atmel_ssc_dt_ids, pdev->dev.of_node);
0138         if (match == NULL)
0139             return NULL;
0140         return match->data;
0141     }
0142 
0143     return (struct atmel_ssc_platform_data *)
0144         platform_get_device_id(pdev)->driver_data;
0145 }
0146 
0147 #ifdef CONFIG_SND_ATMEL_SOC_SSC
0148 static int ssc_sound_dai_probe(struct ssc_device *ssc)
0149 {
0150     struct device_node *np = ssc->pdev->dev.of_node;
0151     int ret;
0152     int id;
0153 
0154     ssc->sound_dai = false;
0155 
0156     if (!of_property_read_bool(np, "#sound-dai-cells"))
0157         return 0;
0158 
0159     id = of_alias_get_id(np, "ssc");
0160     if (id < 0)
0161         return id;
0162 
0163     ret = atmel_ssc_set_audio(id);
0164     ssc->sound_dai = !ret;
0165 
0166     return ret;
0167 }
0168 
0169 static void ssc_sound_dai_remove(struct ssc_device *ssc)
0170 {
0171     if (!ssc->sound_dai)
0172         return;
0173 
0174     atmel_ssc_put_audio(of_alias_get_id(ssc->pdev->dev.of_node, "ssc"));
0175 }
0176 #else
0177 static inline int ssc_sound_dai_probe(struct ssc_device *ssc)
0178 {
0179     if (of_property_read_bool(ssc->pdev->dev.of_node, "#sound-dai-cells"))
0180         return -ENOTSUPP;
0181 
0182     return 0;
0183 }
0184 
0185 static inline void ssc_sound_dai_remove(struct ssc_device *ssc)
0186 {
0187 }
0188 #endif
0189 
0190 static int ssc_probe(struct platform_device *pdev)
0191 {
0192     struct resource *regs;
0193     struct ssc_device *ssc;
0194     const struct atmel_ssc_platform_data *plat_dat;
0195 
0196     ssc = devm_kzalloc(&pdev->dev, sizeof(struct ssc_device), GFP_KERNEL);
0197     if (!ssc) {
0198         dev_dbg(&pdev->dev, "out of memory\n");
0199         return -ENOMEM;
0200     }
0201 
0202     ssc->pdev = pdev;
0203 
0204     plat_dat = atmel_ssc_get_driver_data(pdev);
0205     if (!plat_dat)
0206         return -ENODEV;
0207     ssc->pdata = (struct atmel_ssc_platform_data *)plat_dat;
0208 
0209     if (pdev->dev.of_node) {
0210         struct device_node *np = pdev->dev.of_node;
0211         ssc->clk_from_rk_pin =
0212             of_property_read_bool(np, "atmel,clk-from-rk-pin");
0213     }
0214 
0215     regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
0216     ssc->regs = devm_ioremap_resource(&pdev->dev, regs);
0217     if (IS_ERR(ssc->regs))
0218         return PTR_ERR(ssc->regs);
0219 
0220     ssc->phybase = regs->start;
0221 
0222     ssc->clk = devm_clk_get(&pdev->dev, "pclk");
0223     if (IS_ERR(ssc->clk)) {
0224         dev_dbg(&pdev->dev, "no pclk clock defined\n");
0225         return -ENXIO;
0226     }
0227 
0228     /* disable all interrupts */
0229     clk_prepare_enable(ssc->clk);
0230     ssc_writel(ssc->regs, IDR, -1);
0231     ssc_readl(ssc->regs, SR);
0232     clk_disable_unprepare(ssc->clk);
0233 
0234     ssc->irq = platform_get_irq(pdev, 0);
0235     if (ssc->irq < 0) {
0236         dev_dbg(&pdev->dev, "could not get irq\n");
0237         return ssc->irq;
0238     }
0239 
0240     mutex_lock(&user_lock);
0241     list_add_tail(&ssc->list, &ssc_list);
0242     mutex_unlock(&user_lock);
0243 
0244     platform_set_drvdata(pdev, ssc);
0245 
0246     dev_info(&pdev->dev, "Atmel SSC device at 0x%p (irq %d)\n",
0247             ssc->regs, ssc->irq);
0248 
0249     if (ssc_sound_dai_probe(ssc))
0250         dev_err(&pdev->dev, "failed to auto-setup ssc for audio\n");
0251 
0252     return 0;
0253 }
0254 
0255 static int ssc_remove(struct platform_device *pdev)
0256 {
0257     struct ssc_device *ssc = platform_get_drvdata(pdev);
0258 
0259     ssc_sound_dai_remove(ssc);
0260 
0261     mutex_lock(&user_lock);
0262     list_del(&ssc->list);
0263     mutex_unlock(&user_lock);
0264 
0265     return 0;
0266 }
0267 
0268 static struct platform_driver ssc_driver = {
0269     .driver     = {
0270         .name       = "ssc",
0271         .of_match_table = of_match_ptr(atmel_ssc_dt_ids),
0272     },
0273     .id_table   = atmel_ssc_devtypes,
0274     .probe      = ssc_probe,
0275     .remove     = ssc_remove,
0276 };
0277 module_platform_driver(ssc_driver);
0278 
0279 MODULE_AUTHOR("Hans-Christian Noren Egtvedt <egtvedt@samfundet.no>");
0280 MODULE_DESCRIPTION("SSC driver for Atmel AT91");
0281 MODULE_LICENSE("GPL");
0282 MODULE_ALIAS("platform:ssc");