Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 //
0003 // Freescale P1022RDK ALSA SoC Machine driver
0004 //
0005 // Author: Timur Tabi <timur@freescale.com>
0006 //
0007 // Copyright 2012 Freescale Semiconductor, Inc.
0008 //
0009 // Note: in order for audio to work correctly, the output controls need
0010 // to be enabled, because they control the clock.  So for playback, for
0011 // example:
0012 //
0013 //      amixer sset 'Left Output Mixer PCM' on
0014 //      amixer sset 'Right Output Mixer PCM' on
0015 
0016 #include <linux/module.h>
0017 #include <linux/fsl/guts.h>
0018 #include <linux/interrupt.h>
0019 #include <linux/of_address.h>
0020 #include <linux/of_device.h>
0021 #include <linux/slab.h>
0022 #include <sound/soc.h>
0023 
0024 #include "fsl_dma.h"
0025 #include "fsl_ssi.h"
0026 #include "fsl_utils.h"
0027 
0028 /* P1022-specific PMUXCR and DMUXCR bit definitions */
0029 
0030 #define CCSR_GUTS_PMUXCR_UART0_I2C1_MASK    0x0001c000
0031 #define CCSR_GUTS_PMUXCR_UART0_I2C1_UART0_SSI   0x00010000
0032 #define CCSR_GUTS_PMUXCR_UART0_I2C1_SSI     0x00018000
0033 
0034 #define CCSR_GUTS_PMUXCR_SSI_DMA_TDM_MASK   0x00000c00
0035 #define CCSR_GUTS_PMUXCR_SSI_DMA_TDM_SSI    0x00000000
0036 
0037 #define CCSR_GUTS_DMUXCR_PAD    1   /* DMA controller/channel set to pad */
0038 #define CCSR_GUTS_DMUXCR_SSI    2   /* DMA controller/channel set to SSI */
0039 
0040 /*
0041  * Set the DMACR register in the GUTS
0042  *
0043  * The DMACR register determines the source of initiated transfers for each
0044  * channel on each DMA controller.  Rather than have a bunch of repetitive
0045  * macros for the bit patterns, we just have a function that calculates
0046  * them.
0047  *
0048  * guts: Pointer to GUTS structure
0049  * co: The DMA controller (0 or 1)
0050  * ch: The channel on the DMA controller (0, 1, 2, or 3)
0051  * device: The device to set as the target (CCSR_GUTS_DMUXCR_xxx)
0052  */
0053 static inline void guts_set_dmuxcr(struct ccsr_guts __iomem *guts,
0054     unsigned int co, unsigned int ch, unsigned int device)
0055 {
0056     unsigned int shift = 16 + (8 * (1 - co) + 2 * (3 - ch));
0057 
0058     clrsetbits_be32(&guts->dmuxcr, 3 << shift, device << shift);
0059 }
0060 
0061 /* There's only one global utilities register */
0062 static phys_addr_t guts_phys;
0063 
0064 /**
0065  * machine_data: machine-specific ASoC device data
0066  *
0067  * This structure contains data for a single sound platform device on an
0068  * P1022 RDK.  Some of the data is taken from the device tree.
0069  */
0070 struct machine_data {
0071     struct snd_soc_dai_link dai[2];
0072     struct snd_soc_card card;
0073     unsigned int dai_format;
0074     unsigned int codec_clk_direction;
0075     unsigned int cpu_clk_direction;
0076     unsigned int clk_frequency;
0077     unsigned int dma_id[2];     /* 0 = DMA1, 1 = DMA2, etc */
0078     unsigned int dma_channel_id[2]; /* 0 = ch 0, 1 = ch 1, etc*/
0079     char platform_name[2][DAI_NAME_SIZE]; /* One for each DMA channel */
0080 };
0081 
0082 /**
0083  * p1022_rdk_machine_probe: initialize the board
0084  *
0085  * This function is used to initialize the board-specific hardware.
0086  *
0087  * Here we program the DMACR and PMUXCR registers.
0088  */
0089 static int p1022_rdk_machine_probe(struct snd_soc_card *card)
0090 {
0091     struct machine_data *mdata =
0092         container_of(card, struct machine_data, card);
0093     struct ccsr_guts __iomem *guts;
0094 
0095     guts = ioremap(guts_phys, sizeof(struct ccsr_guts));
0096     if (!guts) {
0097         dev_err(card->dev, "could not map global utilities\n");
0098         return -ENOMEM;
0099     }
0100 
0101     /* Enable SSI Tx signal */
0102     clrsetbits_be32(&guts->pmuxcr, CCSR_GUTS_PMUXCR_UART0_I2C1_MASK,
0103             CCSR_GUTS_PMUXCR_UART0_I2C1_UART0_SSI);
0104 
0105     /* Enable SSI Rx signal */
0106     clrsetbits_be32(&guts->pmuxcr, CCSR_GUTS_PMUXCR_SSI_DMA_TDM_MASK,
0107             CCSR_GUTS_PMUXCR_SSI_DMA_TDM_SSI);
0108 
0109     /* Enable DMA Channel for SSI */
0110     guts_set_dmuxcr(guts, mdata->dma_id[0], mdata->dma_channel_id[0],
0111             CCSR_GUTS_DMUXCR_SSI);
0112 
0113     guts_set_dmuxcr(guts, mdata->dma_id[1], mdata->dma_channel_id[1],
0114             CCSR_GUTS_DMUXCR_SSI);
0115 
0116     iounmap(guts);
0117 
0118     return 0;
0119 }
0120 
0121 /**
0122  * p1022_rdk_startup: program the board with various hardware parameters
0123  *
0124  * This function takes board-specific information, like clock frequencies
0125  * and serial data formats, and passes that information to the codec and
0126  * transport drivers.
0127  */
0128 static int p1022_rdk_startup(struct snd_pcm_substream *substream)
0129 {
0130     struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0131     struct machine_data *mdata =
0132         container_of(rtd->card, struct machine_data, card);
0133     struct device *dev = rtd->card->dev;
0134     int ret = 0;
0135 
0136     /* Tell the codec driver what the serial protocol is. */
0137     ret = snd_soc_dai_set_fmt(asoc_rtd_to_codec(rtd, 0), mdata->dai_format);
0138     if (ret < 0) {
0139         dev_err(dev, "could not set codec driver audio format (ret=%i)\n",
0140             ret);
0141         return ret;
0142     }
0143 
0144     ret = snd_soc_dai_set_pll(asoc_rtd_to_codec(rtd, 0), 0, 0, mdata->clk_frequency,
0145         mdata->clk_frequency);
0146     if (ret < 0) {
0147         dev_err(dev, "could not set codec PLL frequency (ret=%i)\n",
0148             ret);
0149         return ret;
0150     }
0151 
0152     return 0;
0153 }
0154 
0155 /**
0156  * p1022_rdk_machine_remove: Remove the sound device
0157  *
0158  * This function is called to remove the sound device for one SSI.  We
0159  * de-program the DMACR and PMUXCR register.
0160  */
0161 static int p1022_rdk_machine_remove(struct snd_soc_card *card)
0162 {
0163     struct machine_data *mdata =
0164         container_of(card, struct machine_data, card);
0165     struct ccsr_guts __iomem *guts;
0166 
0167     guts = ioremap(guts_phys, sizeof(struct ccsr_guts));
0168     if (!guts) {
0169         dev_err(card->dev, "could not map global utilities\n");
0170         return -ENOMEM;
0171     }
0172 
0173     /* Restore the signal routing */
0174     clrbits32(&guts->pmuxcr, CCSR_GUTS_PMUXCR_UART0_I2C1_MASK);
0175     clrbits32(&guts->pmuxcr, CCSR_GUTS_PMUXCR_SSI_DMA_TDM_MASK);
0176     guts_set_dmuxcr(guts, mdata->dma_id[0], mdata->dma_channel_id[0], 0);
0177     guts_set_dmuxcr(guts, mdata->dma_id[1], mdata->dma_channel_id[1], 0);
0178 
0179     iounmap(guts);
0180 
0181     return 0;
0182 }
0183 
0184 /**
0185  * p1022_rdk_ops: ASoC machine driver operations
0186  */
0187 static const struct snd_soc_ops p1022_rdk_ops = {
0188     .startup = p1022_rdk_startup,
0189 };
0190 
0191 /**
0192  * p1022_rdk_probe: platform probe function for the machine driver
0193  *
0194  * Although this is a machine driver, the SSI node is the "master" node with
0195  * respect to audio hardware connections.  Therefore, we create a new ASoC
0196  * device for each new SSI node that has a codec attached.
0197  */
0198 static int p1022_rdk_probe(struct platform_device *pdev)
0199 {
0200     struct device *dev = pdev->dev.parent;
0201     /* ssi_pdev is the platform device for the SSI node that probed us */
0202     struct platform_device *ssi_pdev = to_platform_device(dev);
0203     struct device_node *np = ssi_pdev->dev.of_node;
0204     struct device_node *codec_np = NULL;
0205     struct machine_data *mdata;
0206     struct snd_soc_dai_link_component *comp;
0207     const u32 *iprop;
0208     int ret;
0209 
0210     /* Find the codec node for this SSI. */
0211     codec_np = of_parse_phandle(np, "codec-handle", 0);
0212     if (!codec_np) {
0213         dev_err(dev, "could not find codec node\n");
0214         return -EINVAL;
0215     }
0216 
0217     mdata = kzalloc(sizeof(struct machine_data), GFP_KERNEL);
0218     if (!mdata) {
0219         ret = -ENOMEM;
0220         goto error_put;
0221     }
0222 
0223     comp = devm_kzalloc(&pdev->dev, 6 * sizeof(*comp), GFP_KERNEL);
0224     if (!comp) {
0225         ret = -ENOMEM;
0226         goto error_put;
0227     }
0228 
0229     mdata->dai[0].cpus  = &comp[0];
0230     mdata->dai[0].codecs    = &comp[1];
0231     mdata->dai[0].platforms = &comp[2];
0232 
0233     mdata->dai[0].num_cpus      = 1;
0234     mdata->dai[0].num_codecs    = 1;
0235     mdata->dai[0].num_platforms = 1;
0236 
0237     mdata->dai[1].cpus  = &comp[3];
0238     mdata->dai[1].codecs    = &comp[4];
0239     mdata->dai[1].platforms = &comp[5];
0240 
0241     mdata->dai[1].num_cpus      = 1;
0242     mdata->dai[1].num_codecs    = 1;
0243     mdata->dai[1].num_platforms = 1;
0244 
0245     mdata->dai[0].cpus->dai_name = dev_name(&ssi_pdev->dev);
0246     mdata->dai[0].ops = &p1022_rdk_ops;
0247 
0248     /* ASoC core can match codec with device node */
0249     mdata->dai[0].codecs->of_node = codec_np;
0250 
0251     /*
0252      * We register two DAIs per SSI, one for playback and the other for
0253      * capture.  We support codecs that have separate DAIs for both playback
0254      * and capture.
0255      */
0256     memcpy(&mdata->dai[1], &mdata->dai[0], sizeof(struct snd_soc_dai_link));
0257 
0258     /* The DAI names from the codec (snd_soc_dai_driver.name) */
0259     mdata->dai[0].codecs->dai_name = "wm8960-hifi";
0260     mdata->dai[1].codecs->dai_name = mdata->dai[0].codecs->dai_name;
0261 
0262     /*
0263      * Configure the SSI for I2S slave mode.  Older device trees have
0264      * an fsl,mode property, but we ignore that since there's really
0265      * only one way to configure the SSI.
0266      */
0267     mdata->dai_format = SND_SOC_DAIFMT_NB_NF |
0268         SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBP_CFP;
0269     mdata->codec_clk_direction = SND_SOC_CLOCK_OUT;
0270     mdata->cpu_clk_direction = SND_SOC_CLOCK_IN;
0271 
0272     /*
0273      * In i2s-slave mode, the codec has its own clock source, so we
0274      * need to get the frequency from the device tree and pass it to
0275      * the codec driver.
0276      */
0277     iprop = of_get_property(codec_np, "clock-frequency", NULL);
0278     if (!iprop || !*iprop) {
0279         dev_err(&pdev->dev, "codec bus-frequency property is missing or invalid\n");
0280         ret = -EINVAL;
0281         goto error;
0282     }
0283     mdata->clk_frequency = be32_to_cpup(iprop);
0284 
0285     if (!mdata->clk_frequency) {
0286         dev_err(&pdev->dev, "unknown clock frequency\n");
0287         ret = -EINVAL;
0288         goto error;
0289     }
0290 
0291     /* Find the playback DMA channel to use. */
0292     mdata->dai[0].platforms->name = mdata->platform_name[0];
0293     ret = fsl_asoc_get_dma_channel(np, "fsl,playback-dma", &mdata->dai[0],
0294                        &mdata->dma_channel_id[0],
0295                        &mdata->dma_id[0]);
0296     if (ret) {
0297         dev_err(&pdev->dev, "missing/invalid playback DMA phandle (ret=%i)\n",
0298             ret);
0299         goto error;
0300     }
0301 
0302     /* Find the capture DMA channel to use. */
0303     mdata->dai[1].platforms->name = mdata->platform_name[1];
0304     ret = fsl_asoc_get_dma_channel(np, "fsl,capture-dma", &mdata->dai[1],
0305                        &mdata->dma_channel_id[1],
0306                        &mdata->dma_id[1]);
0307     if (ret) {
0308         dev_err(&pdev->dev, "missing/invalid capture DMA phandle (ret=%i)\n",
0309             ret);
0310         goto error;
0311     }
0312 
0313     /* Initialize our DAI data structure.  */
0314     mdata->dai[0].stream_name = "playback";
0315     mdata->dai[1].stream_name = "capture";
0316     mdata->dai[0].name = mdata->dai[0].stream_name;
0317     mdata->dai[1].name = mdata->dai[1].stream_name;
0318 
0319     mdata->card.probe = p1022_rdk_machine_probe;
0320     mdata->card.remove = p1022_rdk_machine_remove;
0321     mdata->card.name = pdev->name; /* The platform driver name */
0322     mdata->card.owner = THIS_MODULE;
0323     mdata->card.dev = &pdev->dev;
0324     mdata->card.num_links = 2;
0325     mdata->card.dai_link = mdata->dai;
0326 
0327     /* Register with ASoC */
0328     ret = snd_soc_register_card(&mdata->card);
0329     if (ret) {
0330         dev_err(&pdev->dev, "could not register card (ret=%i)\n", ret);
0331         goto error;
0332     }
0333 
0334     return 0;
0335 
0336 error:
0337     kfree(mdata);
0338 error_put:
0339     of_node_put(codec_np);
0340     return ret;
0341 }
0342 
0343 /**
0344  * p1022_rdk_remove: remove the platform device
0345  *
0346  * This function is called when the platform device is removed.
0347  */
0348 static int p1022_rdk_remove(struct platform_device *pdev)
0349 {
0350     struct snd_soc_card *card = platform_get_drvdata(pdev);
0351     struct machine_data *mdata =
0352         container_of(card, struct machine_data, card);
0353 
0354     snd_soc_unregister_card(card);
0355     kfree(mdata);
0356 
0357     return 0;
0358 }
0359 
0360 static struct platform_driver p1022_rdk_driver = {
0361     .probe = p1022_rdk_probe,
0362     .remove = p1022_rdk_remove,
0363     .driver = {
0364         /*
0365          * The name must match 'compatible' property in the device tree,
0366          * in lowercase letters.
0367          */
0368         .name = "snd-soc-p1022rdk",
0369     },
0370 };
0371 
0372 /**
0373  * p1022_rdk_init: machine driver initialization.
0374  *
0375  * This function is called when this module is loaded.
0376  */
0377 static int __init p1022_rdk_init(void)
0378 {
0379     struct device_node *guts_np;
0380     struct resource res;
0381 
0382     /* Get the physical address of the global utilities registers */
0383     guts_np = of_find_compatible_node(NULL, NULL, "fsl,p1022-guts");
0384     if (of_address_to_resource(guts_np, 0, &res)) {
0385         pr_err("snd-soc-p1022rdk: missing/invalid global utils node\n");
0386         of_node_put(guts_np);
0387         return -EINVAL;
0388     }
0389     guts_phys = res.start;
0390     of_node_put(guts_np);
0391 
0392     return platform_driver_register(&p1022_rdk_driver);
0393 }
0394 
0395 /**
0396  * p1022_rdk_exit: machine driver exit
0397  *
0398  * This function is called when this driver is unloaded.
0399  */
0400 static void __exit p1022_rdk_exit(void)
0401 {
0402     platform_driver_unregister(&p1022_rdk_driver);
0403 }
0404 
0405 late_initcall(p1022_rdk_init);
0406 module_exit(p1022_rdk_exit);
0407 
0408 MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
0409 MODULE_DESCRIPTION("Freescale / iVeia P1022 RDK ALSA SoC machine driver");
0410 MODULE_LICENSE("GPL v2");