Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 //
0003 // Freescale MPC8610HPCD ALSA SoC Machine driver
0004 //
0005 // Author: Timur Tabi <timur@freescale.com>
0006 //
0007 // Copyright 2007-2010 Freescale Semiconductor, Inc.
0008 
0009 #include <linux/module.h>
0010 #include <linux/interrupt.h>
0011 #include <linux/fsl/guts.h>
0012 #include <linux/of_address.h>
0013 #include <linux/of_device.h>
0014 #include <linux/slab.h>
0015 #include <sound/soc.h>
0016 
0017 #include "fsl_dma.h"
0018 #include "fsl_ssi.h"
0019 #include "fsl_utils.h"
0020 
0021 /* There's only one global utilities register */
0022 static phys_addr_t guts_phys;
0023 
0024 /**
0025  * mpc8610_hpcd_data: machine-specific ASoC device data
0026  *
0027  * This structure contains data for a single sound platform device on an
0028  * MPC8610 HPCD.  Some of the data is taken from the device tree.
0029  */
0030 struct mpc8610_hpcd_data {
0031     struct snd_soc_dai_link dai[2];
0032     struct snd_soc_card card;
0033     unsigned int dai_format;
0034     unsigned int codec_clk_direction;
0035     unsigned int cpu_clk_direction;
0036     unsigned int clk_frequency;
0037     unsigned int ssi_id;        /* 0 = SSI1, 1 = SSI2, etc */
0038     unsigned int dma_id[2];     /* 0 = DMA1, 1 = DMA2, etc */
0039     unsigned int dma_channel_id[2]; /* 0 = ch 0, 1 = ch 1, etc*/
0040     char codec_dai_name[DAI_NAME_SIZE];
0041     char platform_name[2][DAI_NAME_SIZE]; /* One for each DMA channel */
0042 };
0043 
0044 /**
0045  * mpc8610_hpcd_machine_probe: initialize the board
0046  *
0047  * This function is used to initialize the board-specific hardware.
0048  *
0049  * Here we program the DMACR and PMUXCR registers.
0050  */
0051 static int mpc8610_hpcd_machine_probe(struct snd_soc_card *card)
0052 {
0053     struct mpc8610_hpcd_data *machine_data =
0054         container_of(card, struct mpc8610_hpcd_data, card);
0055     struct ccsr_guts __iomem *guts;
0056 
0057     guts = ioremap(guts_phys, sizeof(struct ccsr_guts));
0058     if (!guts) {
0059         dev_err(card->dev, "could not map global utilities\n");
0060         return -ENOMEM;
0061     }
0062 
0063     /* Program the signal routing between the SSI and the DMA */
0064     guts_set_dmacr(guts, machine_data->dma_id[0],
0065                machine_data->dma_channel_id[0],
0066                CCSR_GUTS_DMACR_DEV_SSI);
0067     guts_set_dmacr(guts, machine_data->dma_id[1],
0068                machine_data->dma_channel_id[1],
0069                CCSR_GUTS_DMACR_DEV_SSI);
0070 
0071     guts_set_pmuxcr_dma(guts, machine_data->dma_id[0],
0072                 machine_data->dma_channel_id[0], 0);
0073     guts_set_pmuxcr_dma(guts, machine_data->dma_id[1],
0074                 machine_data->dma_channel_id[1], 0);
0075 
0076     switch (machine_data->ssi_id) {
0077     case 0:
0078         clrsetbits_be32(&guts->pmuxcr,
0079             CCSR_GUTS_PMUXCR_SSI1_MASK, CCSR_GUTS_PMUXCR_SSI1_SSI);
0080         break;
0081     case 1:
0082         clrsetbits_be32(&guts->pmuxcr,
0083             CCSR_GUTS_PMUXCR_SSI2_MASK, CCSR_GUTS_PMUXCR_SSI2_SSI);
0084         break;
0085     }
0086 
0087     iounmap(guts);
0088 
0089     return 0;
0090 }
0091 
0092 /**
0093  * mpc8610_hpcd_startup: program the board with various hardware parameters
0094  *
0095  * This function takes board-specific information, like clock frequencies
0096  * and serial data formats, and passes that information to the codec and
0097  * transport drivers.
0098  */
0099 static int mpc8610_hpcd_startup(struct snd_pcm_substream *substream)
0100 {
0101     struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0102     struct mpc8610_hpcd_data *machine_data =
0103         container_of(rtd->card, struct mpc8610_hpcd_data, card);
0104     struct device *dev = rtd->card->dev;
0105     int ret = 0;
0106 
0107     /* Tell the codec driver what the serial protocol is. */
0108     ret = snd_soc_dai_set_fmt(asoc_rtd_to_codec(rtd, 0), machine_data->dai_format);
0109     if (ret < 0) {
0110         dev_err(dev, "could not set codec driver audio format\n");
0111         return ret;
0112     }
0113 
0114     /*
0115      * Tell the codec driver what the MCLK frequency is, and whether it's
0116      * a slave or master.
0117      */
0118     ret = snd_soc_dai_set_sysclk(asoc_rtd_to_codec(rtd, 0), 0,
0119                      machine_data->clk_frequency,
0120                      machine_data->codec_clk_direction);
0121     if (ret < 0) {
0122         dev_err(dev, "could not set codec driver clock params\n");
0123         return ret;
0124     }
0125 
0126     return 0;
0127 }
0128 
0129 /**
0130  * mpc8610_hpcd_machine_remove: Remove the sound device
0131  *
0132  * This function is called to remove the sound device for one SSI.  We
0133  * de-program the DMACR and PMUXCR register.
0134  */
0135 static int mpc8610_hpcd_machine_remove(struct snd_soc_card *card)
0136 {
0137     struct mpc8610_hpcd_data *machine_data =
0138         container_of(card, struct mpc8610_hpcd_data, card);
0139     struct ccsr_guts __iomem *guts;
0140 
0141     guts = ioremap(guts_phys, sizeof(struct ccsr_guts));
0142     if (!guts) {
0143         dev_err(card->dev, "could not map global utilities\n");
0144         return -ENOMEM;
0145     }
0146 
0147     /* Restore the signal routing */
0148 
0149     guts_set_dmacr(guts, machine_data->dma_id[0],
0150                machine_data->dma_channel_id[0], 0);
0151     guts_set_dmacr(guts, machine_data->dma_id[1],
0152                machine_data->dma_channel_id[1], 0);
0153 
0154     switch (machine_data->ssi_id) {
0155     case 0:
0156         clrsetbits_be32(&guts->pmuxcr,
0157             CCSR_GUTS_PMUXCR_SSI1_MASK, CCSR_GUTS_PMUXCR_SSI1_LA);
0158         break;
0159     case 1:
0160         clrsetbits_be32(&guts->pmuxcr,
0161             CCSR_GUTS_PMUXCR_SSI2_MASK, CCSR_GUTS_PMUXCR_SSI2_LA);
0162         break;
0163     }
0164 
0165     iounmap(guts);
0166 
0167     return 0;
0168 }
0169 
0170 /**
0171  * mpc8610_hpcd_ops: ASoC machine driver operations
0172  */
0173 static const struct snd_soc_ops mpc8610_hpcd_ops = {
0174     .startup = mpc8610_hpcd_startup,
0175 };
0176 
0177 /**
0178  * mpc8610_hpcd_probe: platform probe function for the machine driver
0179  *
0180  * Although this is a machine driver, the SSI node is the "master" node with
0181  * respect to audio hardware connections.  Therefore, we create a new ASoC
0182  * device for each new SSI node that has a codec attached.
0183  */
0184 static int mpc8610_hpcd_probe(struct platform_device *pdev)
0185 {
0186     struct device *dev = pdev->dev.parent;
0187     /* ssi_pdev is the platform device for the SSI node that probed us */
0188     struct platform_device *ssi_pdev = to_platform_device(dev);
0189     struct device_node *np = ssi_pdev->dev.of_node;
0190     struct device_node *codec_np = NULL;
0191     struct mpc8610_hpcd_data *machine_data;
0192     struct snd_soc_dai_link_component *comp;
0193     int ret;
0194     const char *sprop;
0195     const u32 *iprop;
0196 
0197     /* Find the codec node for this SSI. */
0198     codec_np = of_parse_phandle(np, "codec-handle", 0);
0199     if (!codec_np) {
0200         dev_err(dev, "invalid codec node\n");
0201         return -EINVAL;
0202     }
0203 
0204     machine_data = kzalloc(sizeof(struct mpc8610_hpcd_data), GFP_KERNEL);
0205     if (!machine_data) {
0206         ret = -ENOMEM;
0207         goto error_alloc;
0208     }
0209 
0210     comp = devm_kzalloc(&pdev->dev, 6 * sizeof(*comp), GFP_KERNEL);
0211     if (!comp) {
0212         ret = -ENOMEM;
0213         goto error_alloc;
0214     }
0215 
0216     machine_data->dai[0].cpus   = &comp[0];
0217     machine_data->dai[0].codecs = &comp[1];
0218     machine_data->dai[0].platforms  = &comp[2];
0219 
0220     machine_data->dai[0].num_cpus       = 1;
0221     machine_data->dai[0].num_codecs     = 1;
0222     machine_data->dai[0].num_platforms  = 1;
0223 
0224     machine_data->dai[1].cpus   = &comp[3];
0225     machine_data->dai[1].codecs = &comp[4];
0226     machine_data->dai[1].platforms  = &comp[5];
0227 
0228     machine_data->dai[1].num_cpus       = 1;
0229     machine_data->dai[1].num_codecs     = 1;
0230     machine_data->dai[1].num_platforms  = 1;
0231 
0232     machine_data->dai[0].cpus->dai_name = dev_name(&ssi_pdev->dev);
0233     machine_data->dai[0].ops = &mpc8610_hpcd_ops;
0234 
0235     /* ASoC core can match codec with device node */
0236     machine_data->dai[0].codecs->of_node = codec_np;
0237 
0238     /* The DAI name from the codec (snd_soc_dai_driver.name) */
0239     machine_data->dai[0].codecs->dai_name = "cs4270-hifi";
0240 
0241     /* We register two DAIs per SSI, one for playback and the other for
0242      * capture.  Currently, we only support codecs that have one DAI for
0243      * both playback and capture.
0244      */
0245     memcpy(&machine_data->dai[1], &machine_data->dai[0],
0246            sizeof(struct snd_soc_dai_link));
0247 
0248     /* Get the device ID */
0249     iprop = of_get_property(np, "cell-index", NULL);
0250     if (!iprop) {
0251         dev_err(&pdev->dev, "cell-index property not found\n");
0252         ret = -EINVAL;
0253         goto error;
0254     }
0255     machine_data->ssi_id = be32_to_cpup(iprop);
0256 
0257     /* Get the serial format and clock direction. */
0258     sprop = of_get_property(np, "fsl,mode", NULL);
0259     if (!sprop) {
0260         dev_err(&pdev->dev, "fsl,mode property not found\n");
0261         ret = -EINVAL;
0262         goto error;
0263     }
0264 
0265     if (strcasecmp(sprop, "i2s-slave") == 0) {
0266         machine_data->dai_format =
0267             SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBP_CFP;
0268         machine_data->codec_clk_direction = SND_SOC_CLOCK_OUT;
0269         machine_data->cpu_clk_direction = SND_SOC_CLOCK_IN;
0270 
0271         /* In i2s-slave mode, the codec has its own clock source, so we
0272          * need to get the frequency from the device tree and pass it to
0273          * the codec driver.
0274          */
0275         iprop = of_get_property(codec_np, "clock-frequency", NULL);
0276         if (!iprop || !*iprop) {
0277             dev_err(&pdev->dev, "codec bus-frequency "
0278                 "property is missing or invalid\n");
0279             ret = -EINVAL;
0280             goto error;
0281         }
0282         machine_data->clk_frequency = be32_to_cpup(iprop);
0283     } else if (strcasecmp(sprop, "i2s-master") == 0) {
0284         machine_data->dai_format =
0285             SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBC_CFC;
0286         machine_data->codec_clk_direction = SND_SOC_CLOCK_IN;
0287         machine_data->cpu_clk_direction = SND_SOC_CLOCK_OUT;
0288     } else if (strcasecmp(sprop, "lj-slave") == 0) {
0289         machine_data->dai_format =
0290             SND_SOC_DAIFMT_LEFT_J | SND_SOC_DAIFMT_CBP_CFP;
0291         machine_data->codec_clk_direction = SND_SOC_CLOCK_OUT;
0292         machine_data->cpu_clk_direction = SND_SOC_CLOCK_IN;
0293     } else if (strcasecmp(sprop, "lj-master") == 0) {
0294         machine_data->dai_format =
0295             SND_SOC_DAIFMT_LEFT_J | SND_SOC_DAIFMT_CBC_CFC;
0296         machine_data->codec_clk_direction = SND_SOC_CLOCK_IN;
0297         machine_data->cpu_clk_direction = SND_SOC_CLOCK_OUT;
0298     } else if (strcasecmp(sprop, "rj-slave") == 0) {
0299         machine_data->dai_format =
0300             SND_SOC_DAIFMT_RIGHT_J | SND_SOC_DAIFMT_CBP_CFP;
0301         machine_data->codec_clk_direction = SND_SOC_CLOCK_OUT;
0302         machine_data->cpu_clk_direction = SND_SOC_CLOCK_IN;
0303     } else if (strcasecmp(sprop, "rj-master") == 0) {
0304         machine_data->dai_format =
0305             SND_SOC_DAIFMT_RIGHT_J | SND_SOC_DAIFMT_CBC_CFC;
0306         machine_data->codec_clk_direction = SND_SOC_CLOCK_IN;
0307         machine_data->cpu_clk_direction = SND_SOC_CLOCK_OUT;
0308     } else if (strcasecmp(sprop, "ac97-slave") == 0) {
0309         machine_data->dai_format =
0310             SND_SOC_DAIFMT_AC97 | SND_SOC_DAIFMT_CBP_CFP;
0311         machine_data->codec_clk_direction = SND_SOC_CLOCK_OUT;
0312         machine_data->cpu_clk_direction = SND_SOC_CLOCK_IN;
0313     } else if (strcasecmp(sprop, "ac97-master") == 0) {
0314         machine_data->dai_format =
0315             SND_SOC_DAIFMT_AC97 | SND_SOC_DAIFMT_CBC_CFC;
0316         machine_data->codec_clk_direction = SND_SOC_CLOCK_IN;
0317         machine_data->cpu_clk_direction = SND_SOC_CLOCK_OUT;
0318     } else {
0319         dev_err(&pdev->dev,
0320             "unrecognized fsl,mode property '%s'\n", sprop);
0321         ret = -EINVAL;
0322         goto error;
0323     }
0324 
0325     if (!machine_data->clk_frequency) {
0326         dev_err(&pdev->dev, "unknown clock frequency\n");
0327         ret = -EINVAL;
0328         goto error;
0329     }
0330 
0331     /* Find the playback DMA channel to use. */
0332     machine_data->dai[0].platforms->name = machine_data->platform_name[0];
0333     ret = fsl_asoc_get_dma_channel(np, "fsl,playback-dma",
0334                        &machine_data->dai[0],
0335                        &machine_data->dma_channel_id[0],
0336                        &machine_data->dma_id[0]);
0337     if (ret) {
0338         dev_err(&pdev->dev, "missing/invalid playback DMA phandle\n");
0339         goto error;
0340     }
0341 
0342     /* Find the capture DMA channel to use. */
0343     machine_data->dai[1].platforms->name = machine_data->platform_name[1];
0344     ret = fsl_asoc_get_dma_channel(np, "fsl,capture-dma",
0345                        &machine_data->dai[1],
0346                        &machine_data->dma_channel_id[1],
0347                        &machine_data->dma_id[1]);
0348     if (ret) {
0349         dev_err(&pdev->dev, "missing/invalid capture DMA phandle\n");
0350         goto error;
0351     }
0352 
0353     /* Initialize our DAI data structure.  */
0354     machine_data->dai[0].stream_name = "playback";
0355     machine_data->dai[1].stream_name = "capture";
0356     machine_data->dai[0].name = machine_data->dai[0].stream_name;
0357     machine_data->dai[1].name = machine_data->dai[1].stream_name;
0358 
0359     machine_data->card.probe = mpc8610_hpcd_machine_probe;
0360     machine_data->card.remove = mpc8610_hpcd_machine_remove;
0361     machine_data->card.name = pdev->name; /* The platform driver name */
0362     machine_data->card.owner = THIS_MODULE;
0363     machine_data->card.dev = &pdev->dev;
0364     machine_data->card.num_links = 2;
0365     machine_data->card.dai_link = machine_data->dai;
0366 
0367     /* Register with ASoC */
0368     ret = snd_soc_register_card(&machine_data->card);
0369     if (ret) {
0370         dev_err(&pdev->dev, "could not register card\n");
0371         goto error;
0372     }
0373 
0374     of_node_put(codec_np);
0375 
0376     return 0;
0377 
0378 error:
0379     kfree(machine_data);
0380 error_alloc:
0381     of_node_put(codec_np);
0382     return ret;
0383 }
0384 
0385 /**
0386  * mpc8610_hpcd_remove: remove the platform device
0387  *
0388  * This function is called when the platform device is removed.
0389  */
0390 static int mpc8610_hpcd_remove(struct platform_device *pdev)
0391 {
0392     struct snd_soc_card *card = platform_get_drvdata(pdev);
0393     struct mpc8610_hpcd_data *machine_data =
0394         container_of(card, struct mpc8610_hpcd_data, card);
0395 
0396     snd_soc_unregister_card(card);
0397     kfree(machine_data);
0398 
0399     return 0;
0400 }
0401 
0402 static struct platform_driver mpc8610_hpcd_driver = {
0403     .probe = mpc8610_hpcd_probe,
0404     .remove = mpc8610_hpcd_remove,
0405     .driver = {
0406         /* The name must match 'compatible' property in the device tree,
0407          * in lowercase letters.
0408          */
0409         .name = "snd-soc-mpc8610hpcd",
0410     },
0411 };
0412 
0413 /**
0414  * mpc8610_hpcd_init: machine driver initialization.
0415  *
0416  * This function is called when this module is loaded.
0417  */
0418 static int __init mpc8610_hpcd_init(void)
0419 {
0420     struct device_node *guts_np;
0421     struct resource res;
0422 
0423     pr_info("Freescale MPC8610 HPCD ALSA SoC machine driver\n");
0424 
0425     /* Get the physical address of the global utilities registers */
0426     guts_np = of_find_compatible_node(NULL, NULL, "fsl,mpc8610-guts");
0427     if (of_address_to_resource(guts_np, 0, &res)) {
0428         pr_err("mpc8610-hpcd: missing/invalid global utilities node\n");
0429         of_node_put(guts_np);
0430         return -EINVAL;
0431     }
0432     guts_phys = res.start;
0433     of_node_put(guts_np);
0434 
0435     return platform_driver_register(&mpc8610_hpcd_driver);
0436 }
0437 
0438 /**
0439  * mpc8610_hpcd_exit: machine driver exit
0440  *
0441  * This function is called when this driver is unloaded.
0442  */
0443 static void __exit mpc8610_hpcd_exit(void)
0444 {
0445     platform_driver_unregister(&mpc8610_hpcd_driver);
0446 }
0447 
0448 module_init(mpc8610_hpcd_init);
0449 module_exit(mpc8610_hpcd_exit);
0450 
0451 MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
0452 MODULE_DESCRIPTION("Freescale MPC8610 HPCD ALSA SoC machine driver");
0453 MODULE_LICENSE("GPL v2");