Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 // Copyright 2017-2021 NXP
0003 
0004 #include <linux/module.h>
0005 #include <linux/init.h>
0006 #include <linux/slab.h>
0007 #include <linux/gpio/consumer.h>
0008 #include <linux/of_device.h>
0009 #include <linux/i2c.h>
0010 #include <linux/of_gpio.h>
0011 #include <linux/clk.h>
0012 #include <sound/soc.h>
0013 #include <sound/pcm_params.h>
0014 #include <sound/pcm.h>
0015 #include <sound/soc-dapm.h>
0016 #include <sound/simple_card_utils.h>
0017 
0018 #include "fsl_sai.h"
0019 
0020 #define IMX_CARD_MCLK_22P5792MHZ  22579200
0021 #define IMX_CARD_MCLK_24P576MHZ   24576000
0022 
0023 enum codec_type {
0024     CODEC_DUMMY = 0,
0025     CODEC_AK5558 = 1,
0026     CODEC_AK4458,
0027     CODEC_AK4497,
0028     CODEC_AK5552,
0029 };
0030 
0031 /*
0032  * Mapping LRCK fs and frame width, table 3 & 4 in datasheet
0033  * @rmin: min rate
0034  * @rmax: max rate
0035  * @wmin: min frame ratio
0036  * @wmax: max frame ratio
0037  */
0038 struct imx_akcodec_fs_mul {
0039     unsigned int rmin;
0040     unsigned int rmax;
0041     unsigned int wmin;
0042     unsigned int wmax;
0043 };
0044 
0045 /*
0046  * Mapping TDM mode and frame width
0047  */
0048 struct imx_akcodec_tdm_fs_mul {
0049     unsigned int min;
0050     unsigned int max;
0051     unsigned int mul;
0052 };
0053 
0054 /*
0055  * struct imx_card_plat_data - specific info for codecs
0056  *
0057  * @fs_mul: ratio of mclk/fs for normal mode
0058  * @tdm_fs_mul: ratio of mclk/fs for tdm mode
0059  * @support_rates: supported sample rate
0060  * @support_tdm_rates: supported sample rate for tdm mode
0061  * @support_channels: supported channels
0062  * @support_tdm_channels: supported channels for tdm mode
0063  * @num_fs_mul: ARRAY_SIZE of fs_mul
0064  * @num_tdm_fs_mul: ARRAY_SIZE of tdm_fs_mul
0065  * @num_rates: ARRAY_SIZE of support_rates
0066  * @num_tdm_rates: ARRAY_SIZE of support_tdm_rates
0067  * @num_channels: ARRAY_SIZE of support_channels
0068  * @num_tdm_channels: ARRAY_SIZE of support_tdm_channels
0069  * @type: codec type
0070  */
0071 struct imx_card_plat_data {
0072     struct imx_akcodec_fs_mul  *fs_mul;
0073     struct imx_akcodec_tdm_fs_mul  *tdm_fs_mul;
0074     const u32 *support_rates;
0075     const u32 *support_tdm_rates;
0076     const u32 *support_channels;
0077     const u32 *support_tdm_channels;
0078     unsigned int num_fs_mul;
0079     unsigned int num_tdm_fs_mul;
0080     unsigned int num_rates;
0081     unsigned int num_tdm_rates;
0082     unsigned int num_channels;
0083     unsigned int num_tdm_channels;
0084     unsigned int num_codecs;
0085     enum codec_type type;
0086 };
0087 
0088 /*
0089  * struct dai_link_data - specific info for dai link
0090  *
0091  * @slots: slot number
0092  * @slot_width: slot width value
0093  * @cpu_sysclk_id: sysclk id for cpu dai
0094  * @one2one_ratio: true if mclk equal to bclk
0095  */
0096 struct dai_link_data {
0097     unsigned int slots;
0098     unsigned int slot_width;
0099     unsigned int cpu_sysclk_id;
0100     bool one2one_ratio;
0101 };
0102 
0103 /*
0104  * struct imx_card_data - platform device data
0105  *
0106  * @plat_data: pointer of imx_card_plat_data
0107  * @dapm_routes: pointer of dapm_routes
0108  * @link_data: private data for dai link
0109  * @card: card instance
0110  * @num_dapm_routes: number of dapm_routes
0111  * @asrc_rate: asrc rates
0112  * @asrc_format: asrc format
0113  */
0114 struct imx_card_data {
0115     struct imx_card_plat_data *plat_data;
0116     struct snd_soc_dapm_route *dapm_routes;
0117     struct dai_link_data *link_data;
0118     struct snd_soc_card card;
0119     int num_dapm_routes;
0120     u32 asrc_rate;
0121     snd_pcm_format_t asrc_format;
0122 };
0123 
0124 static struct imx_akcodec_fs_mul ak4458_fs_mul[] = {
0125     /* Normal, < 32kHz */
0126     { .rmin = 8000,   .rmax = 24000,  .wmin = 256,  .wmax = 1024, },
0127     /* Normal, 32kHz */
0128     { .rmin = 32000,  .rmax = 32000,  .wmin = 256,  .wmax = 1024, },
0129     /* Normal */
0130     { .rmin = 44100,  .rmax = 48000,  .wmin = 256,  .wmax = 768,  },
0131     /* Double */
0132     { .rmin = 88200,  .rmax = 96000,  .wmin = 256,  .wmax = 512,  },
0133     /* Quad */
0134     { .rmin = 176400, .rmax = 192000, .wmin = 128,  .wmax = 256,  },
0135     /* Oct */
0136     { .rmin = 352800, .rmax = 384000, .wmin = 32,   .wmax = 128,  },
0137     /* Hex */
0138     { .rmin = 705600, .rmax = 768000, .wmin = 16,   .wmax = 64,   },
0139 };
0140 
0141 static struct imx_akcodec_tdm_fs_mul ak4458_tdm_fs_mul[] = {
0142     /*
0143      * Table 13 - Audio Interface Format
0144      * For TDM mode, MCLK should is set to
0145      * obtained from 2 * slots * slot_width
0146      */
0147     { .min = 128,   .max = 128, .mul = 256  }, /* TDM128 */
0148     { .min = 256,   .max = 256, .mul = 512  }, /* TDM256 */
0149     { .min = 512,   .max = 512, .mul = 1024  }, /* TDM512 */
0150 };
0151 
0152 static struct imx_akcodec_fs_mul ak4497_fs_mul[] = {
0153     /**
0154      * Table 7      - mapping multiplier and speed mode
0155      * Tables 8 & 9 - mapping speed mode and LRCK fs
0156      */
0157     { .rmin = 8000,   .rmax = 32000,  .wmin = 256,  .wmax = 1024, }, /* Normal, <= 32kHz */
0158     { .rmin = 44100,  .rmax = 48000,  .wmin = 256,  .wmax = 512, }, /* Normal */
0159     { .rmin = 88200,  .rmax = 96000,  .wmin = 256,  .wmax = 256, }, /* Double */
0160     { .rmin = 176400, .rmax = 192000, .wmin = 128,  .wmax = 128, }, /* Quad */
0161     { .rmin = 352800, .rmax = 384000, .wmin = 128,  .wmax = 128, }, /* Oct */
0162     { .rmin = 705600, .rmax = 768000, .wmin = 64,   .wmax = 64, }, /* Hex */
0163 };
0164 
0165 /*
0166  * Auto MCLK selection based on LRCK for Normal Mode
0167  * (Table 4 from datasheet)
0168  */
0169 static struct imx_akcodec_fs_mul ak5558_fs_mul[] = {
0170     { .rmin = 8000,   .rmax = 32000,  .wmin = 512,  .wmax = 1024, },
0171     { .rmin = 44100,  .rmax = 48000,  .wmin = 512,  .wmax = 512, },
0172     { .rmin = 88200,  .rmax = 96000,  .wmin = 256,  .wmax = 256, },
0173     { .rmin = 176400, .rmax = 192000, .wmin = 128,  .wmax = 128, },
0174     { .rmin = 352800, .rmax = 384000, .wmin = 64,   .wmax = 64, },
0175     { .rmin = 705600, .rmax = 768000, .wmin = 32,   .wmax = 32, },
0176 };
0177 
0178 /*
0179  * MCLK and BCLK selection based on TDM mode
0180  * because of SAI we also add the restriction: MCLK >= 2 * BCLK
0181  * (Table 9 from datasheet)
0182  */
0183 static struct imx_akcodec_tdm_fs_mul ak5558_tdm_fs_mul[] = {
0184     { .min = 128,   .max = 128, .mul = 256 },
0185     { .min = 256,   .max = 256, .mul = 512 },
0186     { .min = 512,   .max = 512, .mul = 1024 },
0187 };
0188 
0189 static const u32 akcodec_rates[] = {
0190     8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200,
0191     96000, 176400, 192000, 352800, 384000, 705600, 768000,
0192 };
0193 
0194 static const u32 akcodec_tdm_rates[] = {
0195     8000, 16000, 32000, 48000, 96000,
0196 };
0197 
0198 static const u32 ak4458_channels[] = {
0199     1, 2, 4, 6, 8, 10, 12, 14, 16,
0200 };
0201 
0202 static const u32 ak4458_tdm_channels[] = {
0203     1, 2, 3, 4, 5, 6, 7, 8, 16,
0204 };
0205 
0206 static const u32 ak5558_channels[] = {
0207     1, 2, 4, 6, 8,
0208 };
0209 
0210 static const u32 ak5558_tdm_channels[] = {
0211     1, 2, 3, 4, 5, 6, 7, 8,
0212 };
0213 
0214 static bool format_is_dsd(struct snd_pcm_hw_params *params)
0215 {
0216     snd_pcm_format_t format = params_format(params);
0217 
0218     switch (format) {
0219     case SNDRV_PCM_FORMAT_DSD_U8:
0220     case SNDRV_PCM_FORMAT_DSD_U16_LE:
0221     case SNDRV_PCM_FORMAT_DSD_U16_BE:
0222     case SNDRV_PCM_FORMAT_DSD_U32_LE:
0223     case SNDRV_PCM_FORMAT_DSD_U32_BE:
0224         return true;
0225     default:
0226         return false;
0227     }
0228 }
0229 
0230 static bool format_is_tdm(struct dai_link_data *link_data)
0231 {
0232     if (link_data->slots > 2)
0233         return true;
0234     else
0235         return false;
0236 }
0237 
0238 static bool codec_is_akcodec(unsigned int type)
0239 {
0240     switch (type) {
0241     case CODEC_AK4458:
0242     case CODEC_AK4497:
0243     case CODEC_AK5558:
0244     case CODEC_AK5552:
0245         return true;
0246     default:
0247         break;
0248     }
0249     return false;
0250 }
0251 
0252 static unsigned long akcodec_get_mclk_rate(struct snd_pcm_substream *substream,
0253                        struct snd_pcm_hw_params *params,
0254                        int slots, int slot_width)
0255 {
0256     struct snd_soc_pcm_runtime *rtd = substream->private_data;
0257     struct imx_card_data *data = snd_soc_card_get_drvdata(rtd->card);
0258     const struct imx_card_plat_data *plat_data = data->plat_data;
0259     struct dai_link_data *link_data = &data->link_data[rtd->num];
0260     unsigned int width = slots * slot_width;
0261     unsigned int rate = params_rate(params);
0262     int i;
0263 
0264     if (format_is_tdm(link_data)) {
0265         for (i = 0; i < plat_data->num_tdm_fs_mul; i++) {
0266             /* min = max = slots * slots_width */
0267             if (width != plat_data->tdm_fs_mul[i].min)
0268                 continue;
0269             return rate * plat_data->tdm_fs_mul[i].mul;
0270         }
0271     } else {
0272         for (i = 0; i < plat_data->num_fs_mul; i++) {
0273             if (rate >= plat_data->fs_mul[i].rmin &&
0274                 rate <= plat_data->fs_mul[i].rmax) {
0275                 width = max(width, plat_data->fs_mul[i].wmin);
0276                 width = min(width, plat_data->fs_mul[i].wmax);
0277 
0278                 /* Adjust SAI bclk:mclk ratio */
0279                 width *= link_data->one2one_ratio ? 1 : 2;
0280 
0281                 return rate * width;
0282             }
0283         }
0284     }
0285 
0286     /* Let DAI manage clk frequency by default */
0287     return 0;
0288 }
0289 
0290 static int imx_aif_hw_params(struct snd_pcm_substream *substream,
0291                  struct snd_pcm_hw_params *params)
0292 {
0293     struct snd_soc_pcm_runtime *rtd = substream->private_data;
0294     struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
0295     struct snd_soc_card *card = rtd->card;
0296     struct imx_card_data *data = snd_soc_card_get_drvdata(card);
0297     struct dai_link_data *link_data = &data->link_data[rtd->num];
0298     struct imx_card_plat_data *plat_data = data->plat_data;
0299     struct device *dev = card->dev;
0300     struct snd_soc_dai *codec_dai;
0301     unsigned long mclk_freq;
0302     unsigned int fmt = rtd->dai_link->dai_fmt;
0303     unsigned int slots, slot_width;
0304     int ret, i;
0305 
0306     slots = link_data->slots;
0307     slot_width = link_data->slot_width;
0308 
0309     if (!format_is_tdm(link_data)) {
0310         if (format_is_dsd(params)) {
0311             slots = 1;
0312             slot_width = params_width(params);
0313             fmt = (rtd->dai_link->dai_fmt & ~SND_SOC_DAIFMT_FORMAT_MASK) |
0314                   SND_SOC_DAIFMT_PDM;
0315         } else {
0316             slots = 2;
0317             slot_width = params_physical_width(params);
0318             fmt = (rtd->dai_link->dai_fmt & ~SND_SOC_DAIFMT_FORMAT_MASK) |
0319                   SND_SOC_DAIFMT_I2S;
0320         }
0321     }
0322 
0323     ret = snd_soc_dai_set_fmt(cpu_dai, snd_soc_daifmt_clock_provider_flipped(fmt));
0324     if (ret && ret != -ENOTSUPP) {
0325         dev_err(dev, "failed to set cpu dai fmt: %d\n", ret);
0326         return ret;
0327     }
0328     ret = snd_soc_dai_set_tdm_slot(cpu_dai,
0329                        BIT(slots) - 1,
0330                        BIT(slots) - 1,
0331                        slots, slot_width);
0332     if (ret && ret != -ENOTSUPP) {
0333         dev_err(dev, "failed to set cpu dai tdm slot: %d\n", ret);
0334         return ret;
0335     }
0336 
0337     for_each_rtd_codec_dais(rtd, i, codec_dai) {
0338         ret = snd_soc_dai_set_fmt(codec_dai, fmt);
0339         if (ret && ret != -ENOTSUPP) {
0340             dev_err(dev, "failed to set codec dai[%d] fmt: %d\n", i, ret);
0341             return ret;
0342         }
0343 
0344         ret = snd_soc_dai_set_tdm_slot(codec_dai,
0345                            BIT(slots) - 1,
0346                            BIT(slots) - 1,
0347                            slots, slot_width);
0348         if (ret && ret != -ENOTSUPP) {
0349             dev_err(dev, "failed to set codec dai[%d] tdm slot: %d\n", i, ret);
0350             return ret;
0351         }
0352     }
0353 
0354     /* Set MCLK freq */
0355     if (codec_is_akcodec(plat_data->type))
0356         mclk_freq = akcodec_get_mclk_rate(substream, params, slots, slot_width);
0357     else
0358         mclk_freq = params_rate(params) * slots * slot_width;
0359 
0360     if (format_is_dsd(params)) {
0361         /* Use the maximum freq from DSD512 (512*44100 = 22579200) */
0362         if (!(params_rate(params) % 11025))
0363             mclk_freq = IMX_CARD_MCLK_22P5792MHZ;
0364         else
0365             mclk_freq = IMX_CARD_MCLK_24P576MHZ;
0366     }
0367 
0368     ret = snd_soc_dai_set_sysclk(cpu_dai, link_data->cpu_sysclk_id, mclk_freq,
0369                      SND_SOC_CLOCK_OUT);
0370     if (ret && ret != -ENOTSUPP) {
0371         dev_err(dev, "failed to set cpui dai mclk1 rate (%lu): %d\n", mclk_freq, ret);
0372         return ret;
0373     }
0374 
0375     return 0;
0376 }
0377 
0378 static int ak5558_hw_rule_rate(struct snd_pcm_hw_params *p, struct snd_pcm_hw_rule *r)
0379 {
0380     struct dai_link_data *link_data = r->private;
0381     struct snd_interval t = { .min = 8000, .max = 8000, };
0382     unsigned long mclk_freq;
0383     unsigned int fs;
0384     int i;
0385 
0386     fs = hw_param_interval(p, SNDRV_PCM_HW_PARAM_SAMPLE_BITS)->min;
0387     fs *= link_data->slots;
0388 
0389     /* Identify maximum supported rate */
0390     for (i = 0; i < ARRAY_SIZE(akcodec_rates); i++) {
0391         mclk_freq = fs * akcodec_rates[i];
0392         /* Adjust SAI bclk:mclk ratio */
0393         mclk_freq *= link_data->one2one_ratio ? 1 : 2;
0394 
0395         /* Skip rates for which MCLK is beyond supported value */
0396         if (mclk_freq > 36864000)
0397             continue;
0398 
0399         if (t.max < akcodec_rates[i])
0400             t.max = akcodec_rates[i];
0401     }
0402 
0403     return snd_interval_refine(hw_param_interval(p, r->var), &t);
0404 }
0405 
0406 static int imx_aif_startup(struct snd_pcm_substream *substream)
0407 {
0408     struct snd_pcm_runtime *runtime = substream->runtime;
0409     struct snd_soc_pcm_runtime *rtd = substream->private_data;
0410     struct snd_soc_card *card = rtd->card;
0411     struct imx_card_data *data = snd_soc_card_get_drvdata(card);
0412     struct dai_link_data *link_data = &data->link_data[rtd->num];
0413     static struct snd_pcm_hw_constraint_list constraint_rates;
0414     static struct snd_pcm_hw_constraint_list constraint_channels;
0415     int ret = 0;
0416 
0417     if (format_is_tdm(link_data)) {
0418         constraint_channels.list = data->plat_data->support_tdm_channels;
0419         constraint_channels.count = data->plat_data->num_tdm_channels;
0420         constraint_rates.list = data->plat_data->support_tdm_rates;
0421         constraint_rates.count = data->plat_data->num_tdm_rates;
0422     } else {
0423         constraint_channels.list = data->plat_data->support_channels;
0424         constraint_channels.count = data->plat_data->num_channels;
0425         constraint_rates.list = data->plat_data->support_rates;
0426         constraint_rates.count = data->plat_data->num_rates;
0427     }
0428 
0429     if (constraint_channels.count) {
0430         ret = snd_pcm_hw_constraint_list(runtime, 0,
0431                          SNDRV_PCM_HW_PARAM_CHANNELS,
0432                          &constraint_channels);
0433         if (ret)
0434             return ret;
0435     }
0436 
0437     if (constraint_rates.count) {
0438         ret = snd_pcm_hw_constraint_list(runtime, 0,
0439                          SNDRV_PCM_HW_PARAM_RATE,
0440                          &constraint_rates);
0441         if (ret)
0442             return ret;
0443     }
0444 
0445     if (data->plat_data->type == CODEC_AK5558)
0446         ret = snd_pcm_hw_rule_add(substream->runtime, 0,
0447                       SNDRV_PCM_HW_PARAM_RATE,
0448                       ak5558_hw_rule_rate, link_data,
0449                       SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
0450 
0451     return ret;
0452 }
0453 
0454 static const struct snd_soc_ops imx_aif_ops = {
0455     .hw_params = imx_aif_hw_params,
0456     .startup = imx_aif_startup,
0457 };
0458 
0459 static const struct snd_soc_ops imx_aif_ops_be = {
0460     .hw_params = imx_aif_hw_params,
0461 };
0462 
0463 static int be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
0464                   struct snd_pcm_hw_params *params)
0465 {
0466     struct snd_soc_card *card = rtd->card;
0467     struct imx_card_data *data = snd_soc_card_get_drvdata(card);
0468     struct snd_interval *rate;
0469     struct snd_mask *mask;
0470 
0471     rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
0472     rate->max = data->asrc_rate;
0473     rate->min = data->asrc_rate;
0474 
0475     mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
0476     snd_mask_none(mask);
0477     snd_mask_set(mask, (__force unsigned int)data->asrc_format);
0478 
0479     return 0;
0480 }
0481 
0482 static int imx_card_parse_of(struct imx_card_data *data)
0483 {
0484     struct imx_card_plat_data *plat_data = data->plat_data;
0485     struct snd_soc_card *card = &data->card;
0486     struct snd_soc_dai_link_component *dlc;
0487     struct device_node *platform = NULL;
0488     struct device_node *codec = NULL;
0489     struct device_node *cpu = NULL;
0490     struct device_node *np;
0491     struct device *dev = card->dev;
0492     struct snd_soc_dai_link *link;
0493     struct dai_link_data *link_data;
0494     struct of_phandle_args args;
0495     int ret, num_links;
0496     u32 asrc_fmt = 0;
0497     u32 width;
0498 
0499     ret = snd_soc_of_parse_card_name(card, "model");
0500     if (ret) {
0501         dev_err(dev, "Error parsing card name: %d\n", ret);
0502         return ret;
0503     }
0504 
0505     /* DAPM routes */
0506     if (of_property_read_bool(dev->of_node, "audio-routing")) {
0507         ret = snd_soc_of_parse_audio_routing(card, "audio-routing");
0508         if (ret)
0509             return ret;
0510     }
0511 
0512     /* Populate links */
0513     num_links = of_get_child_count(dev->of_node);
0514 
0515     /* Allocate the DAI link array */
0516     card->dai_link = devm_kcalloc(dev, num_links, sizeof(*link), GFP_KERNEL);
0517     if (!card->dai_link)
0518         return -ENOMEM;
0519 
0520     data->link_data = devm_kcalloc(dev, num_links, sizeof(*link), GFP_KERNEL);
0521     if (!data->link_data)
0522         return -ENOMEM;
0523 
0524     card->num_links = num_links;
0525     link = card->dai_link;
0526     link_data = data->link_data;
0527 
0528     for_each_child_of_node(dev->of_node, np) {
0529         dlc = devm_kzalloc(dev, 2 * sizeof(*dlc), GFP_KERNEL);
0530         if (!dlc) {
0531             ret = -ENOMEM;
0532             goto err_put_np;
0533         }
0534 
0535         link->cpus  = &dlc[0];
0536         link->platforms = &dlc[1];
0537 
0538         link->num_cpus      = 1;
0539         link->num_platforms = 1;
0540 
0541         ret = of_property_read_string(np, "link-name", &link->name);
0542         if (ret) {
0543             dev_err(card->dev, "error getting codec dai_link name\n");
0544             goto err_put_np;
0545         }
0546 
0547         cpu = of_get_child_by_name(np, "cpu");
0548         if (!cpu) {
0549             dev_err(dev, "%s: Can't find cpu DT node\n", link->name);
0550             ret = -EINVAL;
0551             goto err;
0552         }
0553 
0554         ret = of_parse_phandle_with_args(cpu, "sound-dai",
0555                          "#sound-dai-cells", 0, &args);
0556         if (ret) {
0557             dev_err(card->dev, "%s: error getting cpu phandle\n", link->name);
0558             goto err;
0559         }
0560 
0561         if (of_node_name_eq(args.np, "sai")) {
0562             /* sai sysclk id */
0563             link_data->cpu_sysclk_id = FSL_SAI_CLK_MAST1;
0564 
0565             /* sai may support mclk/bclk = 1 */
0566             if (of_find_property(np, "fsl,mclk-equal-bclk", NULL)) {
0567                 link_data->one2one_ratio = true;
0568             } else {
0569                 int i;
0570 
0571                 /*
0572                  * i.MX8MQ don't support one2one ratio, then
0573                  * with ak4497 only 16bit case is supported.
0574                  */
0575                 for (i = 0; i < ARRAY_SIZE(ak4497_fs_mul); i++) {
0576                     if (ak4497_fs_mul[i].rmin == 705600 &&
0577                         ak4497_fs_mul[i].rmax == 768000) {
0578                         ak4497_fs_mul[i].wmin = 32;
0579                         ak4497_fs_mul[i].wmax = 32;
0580                     }
0581                 }
0582             }
0583         }
0584 
0585         link->cpus->of_node = args.np;
0586         link->platforms->of_node = link->cpus->of_node;
0587         link->id = args.args[0];
0588 
0589         ret = snd_soc_of_get_dai_name(cpu, &link->cpus->dai_name);
0590         if (ret) {
0591             dev_err_probe(card->dev, ret,
0592                       "%s: error getting cpu dai name\n", link->name);
0593             goto err;
0594         }
0595 
0596         codec = of_get_child_by_name(np, "codec");
0597         if (codec) {
0598             ret = snd_soc_of_get_dai_link_codecs(dev, codec, link);
0599             if (ret < 0) {
0600                 dev_err_probe(dev, ret, "%s: codec dai not found\n",
0601                         link->name);
0602                 goto err;
0603             }
0604 
0605             plat_data->num_codecs = link->num_codecs;
0606 
0607             /* Check the akcodec type */
0608             if (!strcmp(link->codecs->dai_name, "ak4458-aif"))
0609                 plat_data->type = CODEC_AK4458;
0610             else if (!strcmp(link->codecs->dai_name, "ak4497-aif"))
0611                 plat_data->type = CODEC_AK4497;
0612             else if (!strcmp(link->codecs->dai_name, "ak5558-aif"))
0613                 plat_data->type = CODEC_AK5558;
0614             else if (!strcmp(link->codecs->dai_name, "ak5552-aif"))
0615                 plat_data->type = CODEC_AK5552;
0616 
0617         } else {
0618             dlc = devm_kzalloc(dev, sizeof(*dlc), GFP_KERNEL);
0619             if (!dlc) {
0620                 ret = -ENOMEM;
0621                 goto err;
0622             }
0623 
0624             link->codecs     = dlc;
0625             link->num_codecs = 1;
0626 
0627             link->codecs->dai_name = "snd-soc-dummy-dai";
0628             link->codecs->name = "snd-soc-dummy";
0629         }
0630 
0631         if (!strncmp(link->name, "HiFi-ASRC-FE", 12)) {
0632             /* DPCM frontend */
0633             link->dynamic = 1;
0634             link->dpcm_merged_chan = 1;
0635 
0636             ret = of_property_read_u32(args.np, "fsl,asrc-rate", &data->asrc_rate);
0637             if (ret) {
0638                 dev_err(dev, "failed to get output rate\n");
0639                 ret = -EINVAL;
0640                 goto err;
0641             }
0642 
0643             ret = of_property_read_u32(args.np, "fsl,asrc-format", &asrc_fmt);
0644             data->asrc_format = (__force snd_pcm_format_t)asrc_fmt;
0645             if (ret) {
0646                 /* Fallback to old binding; translate to asrc_format */
0647                 ret = of_property_read_u32(args.np, "fsl,asrc-width", &width);
0648                 if (ret) {
0649                     dev_err(dev,
0650                         "failed to decide output format\n");
0651                     goto err;
0652                 }
0653 
0654                 if (width == 24)
0655                     data->asrc_format = SNDRV_PCM_FORMAT_S24_LE;
0656                 else
0657                     data->asrc_format = SNDRV_PCM_FORMAT_S16_LE;
0658             }
0659         } else if (!strncmp(link->name, "HiFi-ASRC-BE", 12)) {
0660             /* DPCM backend */
0661             link->no_pcm = 1;
0662             link->platforms->of_node = NULL;
0663             link->platforms->name = "snd-soc-dummy";
0664 
0665             link->be_hw_params_fixup = be_hw_params_fixup;
0666             link->ops = &imx_aif_ops_be;
0667         } else {
0668             link->ops = &imx_aif_ops;
0669         }
0670 
0671         if (link->no_pcm || link->dynamic)
0672             snd_soc_dai_link_set_capabilities(link);
0673 
0674         /* Get dai fmt */
0675         ret = asoc_simple_parse_daifmt(dev, np, codec,
0676                            NULL, &link->dai_fmt);
0677         if (ret)
0678             link->dai_fmt = SND_SOC_DAIFMT_NB_NF |
0679                     SND_SOC_DAIFMT_CBC_CFC |
0680                     SND_SOC_DAIFMT_I2S;
0681 
0682         /* Get tdm slot */
0683         snd_soc_of_parse_tdm_slot(np, NULL, NULL,
0684                       &link_data->slots,
0685                       &link_data->slot_width);
0686         /* default value */
0687         if (!link_data->slots)
0688             link_data->slots = 2;
0689 
0690         if (!link_data->slot_width)
0691             link_data->slot_width = 32;
0692 
0693         link->ignore_pmdown_time = 1;
0694         link->stream_name = link->name;
0695         link++;
0696         link_data++;
0697 
0698         of_node_put(cpu);
0699         of_node_put(codec);
0700         of_node_put(platform);
0701 
0702         cpu = NULL;
0703         codec = NULL;
0704         platform = NULL;
0705     }
0706 
0707     return 0;
0708 err:
0709     of_node_put(cpu);
0710     of_node_put(codec);
0711     of_node_put(platform);
0712 err_put_np:
0713     of_node_put(np);
0714     return ret;
0715 }
0716 
0717 static int imx_card_probe(struct platform_device *pdev)
0718 {
0719     struct snd_soc_dai_link *link_be = NULL, *link;
0720     struct imx_card_plat_data *plat_data;
0721     struct imx_card_data *data;
0722     int ret, i;
0723 
0724     data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
0725     if (!data)
0726         return -ENOMEM;
0727 
0728     plat_data = devm_kzalloc(&pdev->dev, sizeof(*plat_data), GFP_KERNEL);
0729     if (!plat_data)
0730         return -ENOMEM;
0731 
0732     data->plat_data = plat_data;
0733     data->card.dev = &pdev->dev;
0734 
0735     dev_set_drvdata(&pdev->dev, &data->card);
0736     snd_soc_card_set_drvdata(&data->card, data);
0737     ret = imx_card_parse_of(data);
0738     if (ret)
0739         return ret;
0740 
0741     data->num_dapm_routes = plat_data->num_codecs + 1;
0742     data->dapm_routes = devm_kcalloc(&pdev->dev, data->num_dapm_routes,
0743                      sizeof(struct snd_soc_dapm_route),
0744                      GFP_KERNEL);
0745     if (!data->dapm_routes)
0746         return -ENOMEM;
0747 
0748     /* configure the dapm routes */
0749     switch (plat_data->type) {
0750     case CODEC_AK4458:
0751     case CODEC_AK4497:
0752         if (plat_data->num_codecs == 1) {
0753             data->dapm_routes[0].sink = "Playback";
0754             data->dapm_routes[0].source = "CPU-Playback";
0755             i = 1;
0756         } else {
0757             for (i = 0; i < plat_data->num_codecs; i++) {
0758                 data->dapm_routes[i].sink =
0759                     devm_kasprintf(&pdev->dev, GFP_KERNEL, "%d %s",
0760                                i + 1, "Playback");
0761                 data->dapm_routes[i].source = "CPU-Playback";
0762             }
0763         }
0764         data->dapm_routes[i].sink = "CPU-Playback";
0765         data->dapm_routes[i].source = "ASRC-Playback";
0766         break;
0767     case CODEC_AK5558:
0768     case CODEC_AK5552:
0769         if (plat_data->num_codecs == 1) {
0770             data->dapm_routes[0].sink = "CPU-Capture";
0771             data->dapm_routes[0].source = "Capture";
0772             i = 1;
0773         } else {
0774             for (i = 0; i < plat_data->num_codecs; i++) {
0775                 data->dapm_routes[i].source =
0776                     devm_kasprintf(&pdev->dev, GFP_KERNEL, "%d %s",
0777                                i + 1, "Capture");
0778                 data->dapm_routes[i].sink = "CPU-Capture";
0779             }
0780         }
0781         data->dapm_routes[i].sink = "ASRC-Capture";
0782         data->dapm_routes[i].source = "CPU-Capture";
0783         break;
0784     default:
0785         break;
0786     }
0787 
0788     /* default platform data for akcodecs */
0789     if (codec_is_akcodec(plat_data->type)) {
0790         plat_data->support_rates = akcodec_rates;
0791         plat_data->num_rates = ARRAY_SIZE(akcodec_rates);
0792         plat_data->support_tdm_rates = akcodec_tdm_rates;
0793         plat_data->num_tdm_rates = ARRAY_SIZE(akcodec_tdm_rates);
0794 
0795         switch (plat_data->type) {
0796         case CODEC_AK4458:
0797             plat_data->fs_mul = ak4458_fs_mul;
0798             plat_data->num_fs_mul = ARRAY_SIZE(ak4458_fs_mul);
0799             plat_data->tdm_fs_mul = ak4458_tdm_fs_mul;
0800             plat_data->num_tdm_fs_mul = ARRAY_SIZE(ak4458_tdm_fs_mul);
0801             plat_data->support_channels = ak4458_channels;
0802             plat_data->num_channels = ARRAY_SIZE(ak4458_channels);
0803             plat_data->support_tdm_channels = ak4458_tdm_channels;
0804             plat_data->num_tdm_channels = ARRAY_SIZE(ak4458_tdm_channels);
0805             break;
0806         case CODEC_AK4497:
0807             plat_data->fs_mul = ak4497_fs_mul;
0808             plat_data->num_fs_mul = ARRAY_SIZE(ak4497_fs_mul);
0809             plat_data->support_channels = ak4458_channels;
0810             plat_data->num_channels = ARRAY_SIZE(ak4458_channels);
0811             break;
0812         case CODEC_AK5558:
0813         case CODEC_AK5552:
0814             plat_data->fs_mul = ak5558_fs_mul;
0815             plat_data->num_fs_mul = ARRAY_SIZE(ak5558_fs_mul);
0816             plat_data->tdm_fs_mul = ak5558_tdm_fs_mul;
0817             plat_data->num_tdm_fs_mul = ARRAY_SIZE(ak5558_tdm_fs_mul);
0818             plat_data->support_channels = ak5558_channels;
0819             plat_data->num_channels = ARRAY_SIZE(ak5558_channels);
0820             plat_data->support_tdm_channels = ak5558_tdm_channels;
0821             plat_data->num_tdm_channels = ARRAY_SIZE(ak5558_tdm_channels);
0822             break;
0823         default:
0824             break;
0825         }
0826     }
0827 
0828     /* with asrc as front end */
0829     if (data->card.num_links == 3) {
0830         data->card.dapm_routes = data->dapm_routes;
0831         data->card.num_dapm_routes = data->num_dapm_routes;
0832         for_each_card_prelinks(&data->card, i, link) {
0833             if (link->no_pcm == 1)
0834                 link_be = link;
0835         }
0836         for_each_card_prelinks(&data->card, i, link) {
0837             if (link->dynamic == 1 && link_be) {
0838                 link->dpcm_playback = link_be->dpcm_playback;
0839                 link->dpcm_capture = link_be->dpcm_capture;
0840             }
0841         }
0842     }
0843 
0844     ret = devm_snd_soc_register_card(&pdev->dev, &data->card);
0845     if (ret)
0846         return dev_err_probe(&pdev->dev, ret, "snd_soc_register_card failed\n");
0847 
0848     return 0;
0849 }
0850 
0851 static const struct of_device_id imx_card_dt_ids[] = {
0852     { .compatible = "fsl,imx-audio-card", },
0853     { },
0854 };
0855 MODULE_DEVICE_TABLE(of, imx_card_dt_ids);
0856 
0857 static struct platform_driver imx_card_driver = {
0858     .driver = {
0859         .name = "imx-card",
0860         .pm = &snd_soc_pm_ops,
0861         .of_match_table = imx_card_dt_ids,
0862     },
0863     .probe = imx_card_probe,
0864 };
0865 module_platform_driver(imx_card_driver);
0866 
0867 MODULE_DESCRIPTION("Freescale i.MX ASoC Machine Driver");
0868 MODULE_LICENSE("GPL v2");
0869 MODULE_ALIAS("platform:imx-card");