Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright 2017 NXP
0004  *
0005  * The code contained herein is licensed under the GNU General Public
0006  * License. You may obtain a copy of the GNU General Public License
0007  * Version 2 or later at the following locations:
0008  *
0009  * https://www.opensource.org/licenses/gpl-license.html
0010  * https://www.gnu.org/copyleft/gpl.html
0011  */
0012 
0013 #include <linux/module.h>
0014 #include <linux/of_platform.h>
0015 #include <linux/clk.h>
0016 #include <sound/soc.h>
0017 #include <sound/soc-dapm.h>
0018 #include <linux/pm_runtime.h>
0019 #include "fsl_sai.h"
0020 #include "fsl_audmix.h"
0021 
0022 struct imx_audmix {
0023     struct platform_device *pdev;
0024     struct snd_soc_card card;
0025     struct platform_device *audmix_pdev;
0026     struct platform_device *out_pdev;
0027     struct clk *cpu_mclk;
0028     int num_dai;
0029     struct snd_soc_dai_link *dai;
0030     int num_dai_conf;
0031     struct snd_soc_codec_conf *dai_conf;
0032     int num_dapm_routes;
0033     struct snd_soc_dapm_route *dapm_routes;
0034 };
0035 
0036 static const u32 imx_audmix_rates[] = {
0037     8000, 12000, 16000, 24000, 32000, 48000, 64000, 96000,
0038 };
0039 
0040 static const struct snd_pcm_hw_constraint_list imx_audmix_rate_constraints = {
0041     .count = ARRAY_SIZE(imx_audmix_rates),
0042     .list = imx_audmix_rates,
0043 };
0044 
0045 static int imx_audmix_fe_startup(struct snd_pcm_substream *substream)
0046 {
0047     struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0048     struct imx_audmix *priv = snd_soc_card_get_drvdata(rtd->card);
0049     struct snd_pcm_runtime *runtime = substream->runtime;
0050     struct device *dev = rtd->card->dev;
0051     unsigned long clk_rate = clk_get_rate(priv->cpu_mclk);
0052     int ret;
0053 
0054     if (clk_rate % 24576000 == 0) {
0055         ret = snd_pcm_hw_constraint_list(runtime, 0,
0056                          SNDRV_PCM_HW_PARAM_RATE,
0057                          &imx_audmix_rate_constraints);
0058         if (ret < 0)
0059             return ret;
0060     } else {
0061         dev_warn(dev, "mclk may be not supported %lu\n", clk_rate);
0062     }
0063 
0064     ret = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
0065                        1, 8);
0066     if (ret < 0)
0067         return ret;
0068 
0069     return snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT,
0070                         FSL_AUDMIX_FORMATS);
0071 }
0072 
0073 static int imx_audmix_fe_hw_params(struct snd_pcm_substream *substream,
0074                    struct snd_pcm_hw_params *params)
0075 {
0076     struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0077     struct device *dev = rtd->card->dev;
0078     bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
0079     unsigned int fmt = SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_NB_NF;
0080     u32 channels = params_channels(params);
0081     int ret, dir;
0082 
0083     /* For playback the AUDMIX is consumer, and for record is provider */
0084     fmt |= tx ? SND_SOC_DAIFMT_BP_FP : SND_SOC_DAIFMT_BC_FC;
0085     dir  = tx ? SND_SOC_CLOCK_OUT : SND_SOC_CLOCK_IN;
0086 
0087     /* set DAI configuration */
0088     ret = snd_soc_dai_set_fmt(asoc_rtd_to_cpu(rtd, 0), fmt);
0089     if (ret) {
0090         dev_err(dev, "failed to set cpu dai fmt: %d\n", ret);
0091         return ret;
0092     }
0093 
0094     ret = snd_soc_dai_set_sysclk(asoc_rtd_to_cpu(rtd, 0), FSL_SAI_CLK_MAST1, 0, dir);
0095     if (ret) {
0096         dev_err(dev, "failed to set cpu sysclk: %d\n", ret);
0097         return ret;
0098     }
0099 
0100     /*
0101      * Per datasheet, AUDMIX expects 8 slots and 32 bits
0102      * for every slot in TDM mode.
0103      */
0104     ret = snd_soc_dai_set_tdm_slot(asoc_rtd_to_cpu(rtd, 0), BIT(channels) - 1,
0105                        BIT(channels) - 1, 8, 32);
0106     if (ret)
0107         dev_err(dev, "failed to set cpu dai tdm slot: %d\n", ret);
0108 
0109     return ret;
0110 }
0111 
0112 static int imx_audmix_be_hw_params(struct snd_pcm_substream *substream,
0113                    struct snd_pcm_hw_params *params)
0114 {
0115     struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0116     struct device *dev = rtd->card->dev;
0117     bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
0118     unsigned int fmt = SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_NB_NF;
0119     int ret;
0120 
0121     if (!tx)
0122         return 0;
0123 
0124     /* For playback the AUDMIX is consumer */
0125     fmt |= SND_SOC_DAIFMT_BC_FC;
0126 
0127     /* set AUDMIX DAI configuration */
0128     ret = snd_soc_dai_set_fmt(asoc_rtd_to_cpu(rtd, 0), fmt);
0129     if (ret)
0130         dev_err(dev, "failed to set AUDMIX DAI fmt: %d\n", ret);
0131 
0132     return ret;
0133 }
0134 
0135 static const struct snd_soc_ops imx_audmix_fe_ops = {
0136     .startup = imx_audmix_fe_startup,
0137     .hw_params = imx_audmix_fe_hw_params,
0138 };
0139 
0140 static const struct snd_soc_ops imx_audmix_be_ops = {
0141     .hw_params = imx_audmix_be_hw_params,
0142 };
0143 
0144 static int imx_audmix_probe(struct platform_device *pdev)
0145 {
0146     struct device_node *np = pdev->dev.of_node;
0147     struct device_node *audmix_np = NULL, *out_cpu_np = NULL;
0148     struct platform_device *audmix_pdev = NULL;
0149     struct platform_device *cpu_pdev;
0150     struct of_phandle_args args;
0151     struct imx_audmix *priv;
0152     int i, num_dai, ret;
0153     const char *fe_name_pref = "HiFi-AUDMIX-FE-";
0154     char *be_name, *be_pb, *be_cp, *dai_name, *capture_dai_name;
0155 
0156     if (pdev->dev.parent) {
0157         audmix_np = pdev->dev.parent->of_node;
0158     } else {
0159         dev_err(&pdev->dev, "Missing parent device.\n");
0160         return -EINVAL;
0161     }
0162 
0163     if (!audmix_np) {
0164         dev_err(&pdev->dev, "Missing DT node for parent device.\n");
0165         return -EINVAL;
0166     }
0167 
0168     audmix_pdev = of_find_device_by_node(audmix_np);
0169     if (!audmix_pdev) {
0170         dev_err(&pdev->dev, "Missing AUDMIX platform device for %s\n",
0171             np->full_name);
0172         return -EINVAL;
0173     }
0174     put_device(&audmix_pdev->dev);
0175 
0176     num_dai = of_count_phandle_with_args(audmix_np, "dais", NULL);
0177     if (num_dai != FSL_AUDMIX_MAX_DAIS) {
0178         dev_err(&pdev->dev, "Need 2 dais to be provided for %s\n",
0179             audmix_np->full_name);
0180         return -EINVAL;
0181     }
0182 
0183     priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
0184     if (!priv)
0185         return -ENOMEM;
0186 
0187     priv->num_dai = 2 * num_dai;
0188     priv->dai = devm_kcalloc(&pdev->dev, priv->num_dai,
0189                  sizeof(struct snd_soc_dai_link), GFP_KERNEL);
0190     if (!priv->dai)
0191         return -ENOMEM;
0192 
0193     priv->num_dai_conf = num_dai;
0194     priv->dai_conf = devm_kcalloc(&pdev->dev, priv->num_dai_conf,
0195                       sizeof(struct snd_soc_codec_conf),
0196                       GFP_KERNEL);
0197     if (!priv->dai_conf)
0198         return -ENOMEM;
0199 
0200     priv->num_dapm_routes = 3 * num_dai;
0201     priv->dapm_routes = devm_kcalloc(&pdev->dev, priv->num_dapm_routes,
0202                      sizeof(struct snd_soc_dapm_route),
0203                      GFP_KERNEL);
0204     if (!priv->dapm_routes)
0205         return -ENOMEM;
0206 
0207     for (i = 0; i < num_dai; i++) {
0208         struct snd_soc_dai_link_component *dlc;
0209 
0210         /* for CPU/Codec/Platform x 2 */
0211         dlc = devm_kcalloc(&pdev->dev, 6, sizeof(*dlc), GFP_KERNEL);
0212         if (!dlc)
0213             return -ENOMEM;
0214 
0215         ret = of_parse_phandle_with_args(audmix_np, "dais", NULL, i,
0216                          &args);
0217         if (ret < 0) {
0218             dev_err(&pdev->dev, "of_parse_phandle_with_args failed\n");
0219             return ret;
0220         }
0221 
0222         cpu_pdev = of_find_device_by_node(args.np);
0223         if (!cpu_pdev) {
0224             dev_err(&pdev->dev, "failed to find SAI platform device\n");
0225             return -EINVAL;
0226         }
0227         put_device(&cpu_pdev->dev);
0228 
0229         dai_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s%s",
0230                       fe_name_pref, args.np->full_name + 1);
0231 
0232         dev_info(pdev->dev.parent, "DAI FE name:%s\n", dai_name);
0233 
0234         if (i == 0) {
0235             out_cpu_np = args.np;
0236             capture_dai_name =
0237                 devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s %s",
0238                            dai_name, "CPU-Capture");
0239         }
0240 
0241         priv->dai[i].cpus = &dlc[0];
0242         priv->dai[i].codecs = &dlc[1];
0243         priv->dai[i].platforms = &dlc[2];
0244 
0245         priv->dai[i].num_cpus = 1;
0246         priv->dai[i].num_codecs = 1;
0247         priv->dai[i].num_platforms = 1;
0248 
0249         priv->dai[i].name = dai_name;
0250         priv->dai[i].stream_name = "HiFi-AUDMIX-FE";
0251         priv->dai[i].codecs->dai_name = "snd-soc-dummy-dai";
0252         priv->dai[i].codecs->name = "snd-soc-dummy";
0253         priv->dai[i].cpus->of_node = args.np;
0254         priv->dai[i].cpus->dai_name = dev_name(&cpu_pdev->dev);
0255         priv->dai[i].platforms->of_node = args.np;
0256         priv->dai[i].dynamic = 1;
0257         priv->dai[i].dpcm_playback = 1;
0258         priv->dai[i].dpcm_capture = (i == 0 ? 1 : 0);
0259         priv->dai[i].ignore_pmdown_time = 1;
0260         priv->dai[i].ops = &imx_audmix_fe_ops;
0261 
0262         /* Add AUDMIX Backend */
0263         be_name = devm_kasprintf(&pdev->dev, GFP_KERNEL,
0264                      "audmix-%d", i);
0265         be_pb = devm_kasprintf(&pdev->dev, GFP_KERNEL,
0266                        "AUDMIX-Playback-%d", i);
0267         be_cp = devm_kasprintf(&pdev->dev, GFP_KERNEL,
0268                        "AUDMIX-Capture-%d", i);
0269 
0270         priv->dai[num_dai + i].cpus = &dlc[3];
0271         priv->dai[num_dai + i].codecs = &dlc[4];
0272         priv->dai[num_dai + i].platforms = &dlc[5];
0273 
0274         priv->dai[num_dai + i].num_cpus = 1;
0275         priv->dai[num_dai + i].num_codecs = 1;
0276         priv->dai[num_dai + i].num_platforms = 1;
0277 
0278         priv->dai[num_dai + i].name = be_name;
0279         priv->dai[num_dai + i].codecs->dai_name = "snd-soc-dummy-dai";
0280         priv->dai[num_dai + i].codecs->name = "snd-soc-dummy";
0281         priv->dai[num_dai + i].cpus->of_node = audmix_np;
0282         priv->dai[num_dai + i].cpus->dai_name = be_name;
0283         priv->dai[num_dai + i].platforms->name = "snd-soc-dummy";
0284         priv->dai[num_dai + i].no_pcm = 1;
0285         priv->dai[num_dai + i].dpcm_playback = 1;
0286         priv->dai[num_dai + i].dpcm_capture  = 1;
0287         priv->dai[num_dai + i].ignore_pmdown_time = 1;
0288         priv->dai[num_dai + i].ops = &imx_audmix_be_ops;
0289 
0290         priv->dai_conf[i].dlc.of_node = args.np;
0291         priv->dai_conf[i].name_prefix = dai_name;
0292 
0293         priv->dapm_routes[i].source =
0294             devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s %s",
0295                        dai_name, "CPU-Playback");
0296         priv->dapm_routes[i].sink = be_pb;
0297         priv->dapm_routes[num_dai + i].source   = be_pb;
0298         priv->dapm_routes[num_dai + i].sink     = be_cp;
0299         priv->dapm_routes[2 * num_dai + i].source = be_cp;
0300         priv->dapm_routes[2 * num_dai + i].sink   = capture_dai_name;
0301     }
0302 
0303     cpu_pdev = of_find_device_by_node(out_cpu_np);
0304     if (!cpu_pdev) {
0305         dev_err(&pdev->dev, "failed to find SAI platform device\n");
0306         return -EINVAL;
0307     }
0308     put_device(&cpu_pdev->dev);
0309 
0310     priv->cpu_mclk = devm_clk_get(&cpu_pdev->dev, "mclk1");
0311     if (IS_ERR(priv->cpu_mclk)) {
0312         ret = PTR_ERR(priv->cpu_mclk);
0313         dev_err(&cpu_pdev->dev, "failed to get DAI mclk1: %d\n", ret);
0314         return -EINVAL;
0315     }
0316 
0317     priv->audmix_pdev = audmix_pdev;
0318     priv->out_pdev  = cpu_pdev;
0319 
0320     priv->card.dai_link = priv->dai;
0321     priv->card.num_links = priv->num_dai;
0322     priv->card.codec_conf = priv->dai_conf;
0323     priv->card.num_configs = priv->num_dai_conf;
0324     priv->card.dapm_routes = priv->dapm_routes;
0325     priv->card.num_dapm_routes = priv->num_dapm_routes;
0326     priv->card.dev = &pdev->dev;
0327     priv->card.owner = THIS_MODULE;
0328     priv->card.name = "imx-audmix";
0329 
0330     platform_set_drvdata(pdev, &priv->card);
0331     snd_soc_card_set_drvdata(&priv->card, priv);
0332 
0333     ret = devm_snd_soc_register_card(&pdev->dev, &priv->card);
0334     if (ret) {
0335         dev_err(&pdev->dev, "snd_soc_register_card failed\n");
0336         return ret;
0337     }
0338 
0339     return ret;
0340 }
0341 
0342 static struct platform_driver imx_audmix_driver = {
0343     .probe = imx_audmix_probe,
0344     .driver = {
0345         .name = "imx-audmix",
0346         .pm = &snd_soc_pm_ops,
0347     },
0348 };
0349 module_platform_driver(imx_audmix_driver);
0350 
0351 MODULE_DESCRIPTION("NXP AUDMIX ASoC machine driver");
0352 MODULE_AUTHOR("Viorel Suman <viorel.suman@nxp.com>");
0353 MODULE_ALIAS("platform:imx-audmix");
0354 MODULE_LICENSE("GPL v2");