ASoC: fsl-asoc-card: use different route map for AC'97 mode
[linux-2.6-block.git] / sound / soc / fsl / fsl-asoc-card.c
1 /*
2  * Freescale Generic ASoC Sound Card driver with ASRC
3  *
4  * Copyright (C) 2014 Freescale Semiconductor, Inc.
5  *
6  * Author: Nicolin Chen <nicoleotsuka@gmail.com>
7  *
8  * This file is licensed under the terms of the GNU General Public License
9  * version 2. This program is licensed "as is" without any warranty of any
10  * kind, whether express or implied.
11  */
12
13 #include <linux/clk.h>
14 #include <linux/i2c.h>
15 #include <linux/module.h>
16 #include <linux/of_platform.h>
17 #if IS_ENABLED(CONFIG_SND_AC97_CODEC)
18 #include <sound/ac97_codec.h>
19 #endif
20 #include <sound/pcm_params.h>
21 #include <sound/soc.h>
22
23 #include "fsl_esai.h"
24 #include "fsl_sai.h"
25 #include "imx-audmux.h"
26
27 #include "../codecs/sgtl5000.h"
28 #include "../codecs/wm8962.h"
29 #include "../codecs/wm8960.h"
30
31 #define RX 0
32 #define TX 1
33
34 /* Default DAI format without Master and Slave flag */
35 #define DAI_FMT_BASE (SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF)
36
37 /**
38  * CODEC private data
39  *
40  * @mclk_freq: Clock rate of MCLK
41  * @mclk_id: MCLK (or main clock) id for set_sysclk()
42  * @fll_id: FLL (or secordary clock) id for set_sysclk()
43  * @pll_id: PLL id for set_pll()
44  */
45 struct codec_priv {
46         unsigned long mclk_freq;
47         u32 mclk_id;
48         u32 fll_id;
49         u32 pll_id;
50 };
51
52 /**
53  * CPU private data
54  *
55  * @sysclk_freq[2]: SYSCLK rates for set_sysclk()
56  * @sysclk_dir[2]: SYSCLK directions for set_sysclk()
57  * @sysclk_id[2]: SYSCLK ids for set_sysclk()
58  * @slot_width: Slot width of each frame
59  *
60  * Note: [1] for tx and [0] for rx
61  */
62 struct cpu_priv {
63         unsigned long sysclk_freq[2];
64         u32 sysclk_dir[2];
65         u32 sysclk_id[2];
66         u32 slot_width;
67 };
68
69 /**
70  * Freescale Generic ASOC card private data
71  *
72  * @dai_link[3]: DAI link structure including normal one and DPCM link
73  * @pdev: platform device pointer
74  * @codec_priv: CODEC private data
75  * @cpu_priv: CPU private data
76  * @card: ASoC card structure
77  * @sample_rate: Current sample rate
78  * @sample_format: Current sample format
79  * @asrc_rate: ASRC sample rate used by Back-Ends
80  * @asrc_format: ASRC sample format used by Back-Ends
81  * @dai_fmt: DAI format between CPU and CODEC
82  * @name: Card name
83  */
84
85 struct fsl_asoc_card_priv {
86         struct snd_soc_dai_link dai_link[3];
87         struct platform_device *pdev;
88         struct codec_priv codec_priv;
89         struct cpu_priv cpu_priv;
90         struct snd_soc_card card;
91         u32 sample_rate;
92         u32 sample_format;
93         u32 asrc_rate;
94         u32 asrc_format;
95         u32 dai_fmt;
96         char name[32];
97 };
98
99 /**
100  * This dapm route map exsits for DPCM link only.
101  * The other routes shall go through Device Tree.
102  */
103 static const struct snd_soc_dapm_route audio_map[] = {
104         {"CPU-Playback",  NULL, "ASRC-Playback"},
105         {"Playback",  NULL, "CPU-Playback"},
106         {"ASRC-Capture",  NULL, "CPU-Capture"},
107         {"CPU-Capture",  NULL, "Capture"},
108 };
109
110 static const struct snd_soc_dapm_route audio_map_ac97[] = {
111         {"AC97 Playback",  NULL, "ASRC-Playback"},
112         {"Playback",  NULL, "AC97 Playback"},
113         {"ASRC-Capture",  NULL, "AC97 Capture"},
114         {"AC97 Capture",  NULL, "Capture"},
115 };
116
117 /* Add all possible widgets into here without being redundant */
118 static const struct snd_soc_dapm_widget fsl_asoc_card_dapm_widgets[] = {
119         SND_SOC_DAPM_LINE("Line Out Jack", NULL),
120         SND_SOC_DAPM_LINE("Line In Jack", NULL),
121         SND_SOC_DAPM_HP("Headphone Jack", NULL),
122         SND_SOC_DAPM_SPK("Ext Spk", NULL),
123         SND_SOC_DAPM_MIC("Mic Jack", NULL),
124         SND_SOC_DAPM_MIC("AMIC", NULL),
125         SND_SOC_DAPM_MIC("DMIC", NULL),
126 };
127
128 static bool fsl_asoc_card_is_ac97(struct fsl_asoc_card_priv *priv)
129 {
130         return priv->dai_fmt == SND_SOC_DAIFMT_AC97;
131 }
132
133 static int fsl_asoc_card_hw_params(struct snd_pcm_substream *substream,
134                                    struct snd_pcm_hw_params *params)
135 {
136         struct snd_soc_pcm_runtime *rtd = substream->private_data;
137         struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
138         bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
139         struct cpu_priv *cpu_priv = &priv->cpu_priv;
140         struct device *dev = rtd->card->dev;
141         int ret;
142
143         priv->sample_rate = params_rate(params);
144         priv->sample_format = params_format(params);
145
146         /*
147          * If codec-dai is DAI Master and all configurations are already in the
148          * set_bias_level(), bypass the remaining settings in hw_params().
149          * Note: (dai_fmt & CBM_CFM) includes CBM_CFM and CBM_CFS.
150          */
151         if ((priv->card.set_bias_level &&
152              priv->dai_fmt & SND_SOC_DAIFMT_CBM_CFM) ||
153             fsl_asoc_card_is_ac97(priv))
154                 return 0;
155
156         /* Specific configurations of DAIs starts from here */
157         ret = snd_soc_dai_set_sysclk(rtd->cpu_dai, cpu_priv->sysclk_id[tx],
158                                      cpu_priv->sysclk_freq[tx],
159                                      cpu_priv->sysclk_dir[tx]);
160         if (ret) {
161                 dev_err(dev, "failed to set sysclk for cpu dai\n");
162                 return ret;
163         }
164
165         if (cpu_priv->slot_width) {
166                 ret = snd_soc_dai_set_tdm_slot(rtd->cpu_dai, 0x3, 0x3, 2,
167                                                cpu_priv->slot_width);
168                 if (ret) {
169                         dev_err(dev, "failed to set TDM slot for cpu dai\n");
170                         return ret;
171                 }
172         }
173
174         return 0;
175 }
176
177 static struct snd_soc_ops fsl_asoc_card_ops = {
178         .hw_params = fsl_asoc_card_hw_params,
179 };
180
181 static int be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
182                               struct snd_pcm_hw_params *params)
183 {
184         struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
185         struct snd_interval *rate;
186         struct snd_mask *mask;
187
188         rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
189         rate->max = rate->min = priv->asrc_rate;
190
191         mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
192         snd_mask_none(mask);
193         snd_mask_set(mask, priv->asrc_format);
194
195         return 0;
196 }
197
198 static struct snd_soc_dai_link fsl_asoc_card_dai[] = {
199         /* Default ASoC DAI Link*/
200         {
201                 .name = "HiFi",
202                 .stream_name = "HiFi",
203                 .ops = &fsl_asoc_card_ops,
204         },
205         /* DPCM Link between Front-End and Back-End (Optional) */
206         {
207                 .name = "HiFi-ASRC-FE",
208                 .stream_name = "HiFi-ASRC-FE",
209                 .codec_name = "snd-soc-dummy",
210                 .codec_dai_name = "snd-soc-dummy-dai",
211                 .dpcm_playback = 1,
212                 .dpcm_capture = 1,
213                 .dynamic = 1,
214         },
215         {
216                 .name = "HiFi-ASRC-BE",
217                 .stream_name = "HiFi-ASRC-BE",
218                 .platform_name = "snd-soc-dummy",
219                 .be_hw_params_fixup = be_hw_params_fixup,
220                 .ops = &fsl_asoc_card_ops,
221                 .dpcm_playback = 1,
222                 .dpcm_capture = 1,
223                 .no_pcm = 1,
224         },
225 };
226
227 static int fsl_asoc_card_set_bias_level(struct snd_soc_card *card,
228                                         struct snd_soc_dapm_context *dapm,
229                                         enum snd_soc_bias_level level)
230 {
231         struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(card);
232         struct snd_soc_dai *codec_dai = card->rtd[0].codec_dai;
233         struct codec_priv *codec_priv = &priv->codec_priv;
234         struct device *dev = card->dev;
235         unsigned int pll_out;
236         int ret;
237
238         if (dapm->dev != codec_dai->dev)
239                 return 0;
240
241         switch (level) {
242         case SND_SOC_BIAS_PREPARE:
243                 if (dapm->bias_level != SND_SOC_BIAS_STANDBY)
244                         break;
245
246                 if (priv->sample_format == SNDRV_PCM_FORMAT_S24_LE)
247                         pll_out = priv->sample_rate * 384;
248                 else
249                         pll_out = priv->sample_rate * 256;
250
251                 ret = snd_soc_dai_set_pll(codec_dai, codec_priv->pll_id,
252                                           codec_priv->mclk_id,
253                                           codec_priv->mclk_freq, pll_out);
254                 if (ret) {
255                         dev_err(dev, "failed to start FLL: %d\n", ret);
256                         return ret;
257                 }
258
259                 ret = snd_soc_dai_set_sysclk(codec_dai, codec_priv->fll_id,
260                                              pll_out, SND_SOC_CLOCK_IN);
261                 if (ret) {
262                         dev_err(dev, "failed to set SYSCLK: %d\n", ret);
263                         return ret;
264                 }
265                 break;
266
267         case SND_SOC_BIAS_STANDBY:
268                 if (dapm->bias_level != SND_SOC_BIAS_PREPARE)
269                         break;
270
271                 ret = snd_soc_dai_set_sysclk(codec_dai, codec_priv->mclk_id,
272                                              codec_priv->mclk_freq,
273                                              SND_SOC_CLOCK_IN);
274                 if (ret) {
275                         dev_err(dev, "failed to switch away from FLL: %d\n", ret);
276                         return ret;
277                 }
278
279                 ret = snd_soc_dai_set_pll(codec_dai, codec_priv->pll_id, 0, 0, 0);
280                 if (ret) {
281                         dev_err(dev, "failed to stop FLL: %d\n", ret);
282                         return ret;
283                 }
284                 break;
285
286         default:
287                 break;
288         }
289
290         return 0;
291 }
292
293 static int fsl_asoc_card_audmux_init(struct device_node *np,
294                                      struct fsl_asoc_card_priv *priv)
295 {
296         struct device *dev = &priv->pdev->dev;
297         u32 int_ptcr = 0, ext_ptcr = 0;
298         int int_port, ext_port;
299         int ret;
300
301         ret = of_property_read_u32(np, "mux-int-port", &int_port);
302         if (ret) {
303                 dev_err(dev, "mux-int-port missing or invalid\n");
304                 return ret;
305         }
306         ret = of_property_read_u32(np, "mux-ext-port", &ext_port);
307         if (ret) {
308                 dev_err(dev, "mux-ext-port missing or invalid\n");
309                 return ret;
310         }
311
312         /*
313          * The port numbering in the hardware manual starts at 1, while
314          * the AUDMUX API expects it starts at 0.
315          */
316         int_port--;
317         ext_port--;
318
319         /*
320          * Use asynchronous mode (6 wires) for all cases except AC97.
321          * If only 4 wires are needed, just set SSI into
322          * synchronous mode and enable 4 PADs in IOMUX.
323          */
324         switch (priv->dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
325         case SND_SOC_DAIFMT_CBM_CFM:
326                 int_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | ext_port) |
327                            IMX_AUDMUX_V2_PTCR_RCSEL(8 | ext_port) |
328                            IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) |
329                            IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
330                            IMX_AUDMUX_V2_PTCR_RFSDIR |
331                            IMX_AUDMUX_V2_PTCR_RCLKDIR |
332                            IMX_AUDMUX_V2_PTCR_TFSDIR |
333                            IMX_AUDMUX_V2_PTCR_TCLKDIR;
334                 break;
335         case SND_SOC_DAIFMT_CBM_CFS:
336                 int_ptcr = IMX_AUDMUX_V2_PTCR_RCSEL(8 | ext_port) |
337                            IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
338                            IMX_AUDMUX_V2_PTCR_RCLKDIR |
339                            IMX_AUDMUX_V2_PTCR_TCLKDIR;
340                 ext_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | int_port) |
341                            IMX_AUDMUX_V2_PTCR_TFSEL(int_port) |
342                            IMX_AUDMUX_V2_PTCR_RFSDIR |
343                            IMX_AUDMUX_V2_PTCR_TFSDIR;
344                 break;
345         case SND_SOC_DAIFMT_CBS_CFM:
346                 int_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | ext_port) |
347                            IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) |
348                            IMX_AUDMUX_V2_PTCR_RFSDIR |
349                            IMX_AUDMUX_V2_PTCR_TFSDIR;
350                 ext_ptcr = IMX_AUDMUX_V2_PTCR_RCSEL(8 | int_port) |
351                            IMX_AUDMUX_V2_PTCR_TCSEL(int_port) |
352                            IMX_AUDMUX_V2_PTCR_RCLKDIR |
353                            IMX_AUDMUX_V2_PTCR_TCLKDIR;
354                 break;
355         case SND_SOC_DAIFMT_CBS_CFS:
356                 ext_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | int_port) |
357                            IMX_AUDMUX_V2_PTCR_RCSEL(8 | int_port) |
358                            IMX_AUDMUX_V2_PTCR_TFSEL(int_port) |
359                            IMX_AUDMUX_V2_PTCR_TCSEL(int_port) |
360                            IMX_AUDMUX_V2_PTCR_RFSDIR |
361                            IMX_AUDMUX_V2_PTCR_RCLKDIR |
362                            IMX_AUDMUX_V2_PTCR_TFSDIR |
363                            IMX_AUDMUX_V2_PTCR_TCLKDIR;
364                 break;
365         default:
366                 if (!fsl_asoc_card_is_ac97(priv))
367                         return -EINVAL;
368         }
369
370         if (fsl_asoc_card_is_ac97(priv)) {
371                 int_ptcr = IMX_AUDMUX_V2_PTCR_SYN |
372                            IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
373                            IMX_AUDMUX_V2_PTCR_TCLKDIR;
374                 ext_ptcr = IMX_AUDMUX_V2_PTCR_SYN |
375                            IMX_AUDMUX_V2_PTCR_TFSEL(int_port) |
376                            IMX_AUDMUX_V2_PTCR_TFSDIR;
377         }
378
379         /* Asynchronous mode can not be set along with RCLKDIR */
380         if (!fsl_asoc_card_is_ac97(priv)) {
381                 unsigned int pdcr =
382                                 IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port);
383
384                 ret = imx_audmux_v2_configure_port(int_port, 0,
385                                                    pdcr);
386                 if (ret) {
387                         dev_err(dev, "audmux internal port setup failed\n");
388                         return ret;
389                 }
390         }
391
392         ret = imx_audmux_v2_configure_port(int_port, int_ptcr,
393                                            IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port));
394         if (ret) {
395                 dev_err(dev, "audmux internal port setup failed\n");
396                 return ret;
397         }
398
399         if (!fsl_asoc_card_is_ac97(priv)) {
400                 unsigned int pdcr =
401                                 IMX_AUDMUX_V2_PDCR_RXDSEL(int_port);
402
403                 ret = imx_audmux_v2_configure_port(ext_port, 0,
404                                                    pdcr);
405                 if (ret) {
406                         dev_err(dev, "audmux external port setup failed\n");
407                         return ret;
408                 }
409         }
410
411         ret = imx_audmux_v2_configure_port(ext_port, ext_ptcr,
412                                            IMX_AUDMUX_V2_PDCR_RXDSEL(int_port));
413         if (ret) {
414                 dev_err(dev, "audmux external port setup failed\n");
415                 return ret;
416         }
417
418         return 0;
419 }
420
421 static int fsl_asoc_card_late_probe(struct snd_soc_card *card)
422 {
423         struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(card);
424         struct snd_soc_dai *codec_dai = card->rtd[0].codec_dai;
425         struct codec_priv *codec_priv = &priv->codec_priv;
426         struct device *dev = card->dev;
427         int ret;
428
429         if (fsl_asoc_card_is_ac97(priv)) {
430 #if IS_ENABLED(CONFIG_SND_AC97_CODEC)
431                 struct snd_soc_codec *codec = card->rtd[0].codec;
432                 struct snd_ac97 *ac97 = snd_soc_codec_get_drvdata(codec);
433
434                 /*
435                  * Use slots 3/4 for S/PDIF so SSI won't try to enable
436                  * other slots and send some samples there
437                  * due to SLOTREQ bits for S/PDIF received from codec
438                  */
439                 snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS,
440                                      AC97_EA_SPSA_SLOT_MASK, AC97_EA_SPSA_3_4);
441 #endif
442
443                 return 0;
444         }
445
446         ret = snd_soc_dai_set_sysclk(codec_dai, codec_priv->mclk_id,
447                                      codec_priv->mclk_freq, SND_SOC_CLOCK_IN);
448         if (ret) {
449                 dev_err(dev, "failed to set sysclk in %s\n", __func__);
450                 return ret;
451         }
452
453         return 0;
454 }
455
456 static int fsl_asoc_card_probe(struct platform_device *pdev)
457 {
458         struct device_node *cpu_np, *codec_np, *asrc_np;
459         struct device_node *np = pdev->dev.of_node;
460         struct platform_device *asrc_pdev = NULL;
461         struct platform_device *cpu_pdev;
462         struct fsl_asoc_card_priv *priv;
463         struct i2c_client *codec_dev;
464         const char *codec_dai_name;
465         u32 width;
466         int ret;
467
468         priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
469         if (!priv)
470                 return -ENOMEM;
471
472         cpu_np = of_parse_phandle(np, "audio-cpu", 0);
473         /* Give a chance to old DT binding */
474         if (!cpu_np)
475                 cpu_np = of_parse_phandle(np, "ssi-controller", 0);
476         if (!cpu_np) {
477                 dev_err(&pdev->dev, "CPU phandle missing or invalid\n");
478                 ret = -EINVAL;
479                 goto fail;
480         }
481
482         cpu_pdev = of_find_device_by_node(cpu_np);
483         if (!cpu_pdev) {
484                 dev_err(&pdev->dev, "failed to find CPU DAI device\n");
485                 ret = -EINVAL;
486                 goto fail;
487         }
488
489         codec_np = of_parse_phandle(np, "audio-codec", 0);
490         if (codec_np)
491                 codec_dev = of_find_i2c_device_by_node(codec_np);
492         else
493                 codec_dev = NULL;
494
495         asrc_np = of_parse_phandle(np, "audio-asrc", 0);
496         if (asrc_np)
497                 asrc_pdev = of_find_device_by_node(asrc_np);
498
499         /* Get the MCLK rate only, and leave it controlled by CODEC drivers */
500         if (codec_dev) {
501                 struct clk *codec_clk = clk_get(&codec_dev->dev, NULL);
502
503                 if (!IS_ERR(codec_clk)) {
504                         priv->codec_priv.mclk_freq = clk_get_rate(codec_clk);
505                         clk_put(codec_clk);
506                 }
507         }
508
509         /* Default sample rate and format, will be updated in hw_params() */
510         priv->sample_rate = 44100;
511         priv->sample_format = SNDRV_PCM_FORMAT_S16_LE;
512
513         /* Assign a default DAI format, and allow each card to overwrite it */
514         priv->dai_fmt = DAI_FMT_BASE;
515
516         /* Diversify the card configurations */
517         if (of_device_is_compatible(np, "fsl,imx-audio-cs42888")) {
518                 codec_dai_name = "cs42888";
519                 priv->card.set_bias_level = NULL;
520                 priv->cpu_priv.sysclk_freq[TX] = priv->codec_priv.mclk_freq;
521                 priv->cpu_priv.sysclk_freq[RX] = priv->codec_priv.mclk_freq;
522                 priv->cpu_priv.sysclk_dir[TX] = SND_SOC_CLOCK_OUT;
523                 priv->cpu_priv.sysclk_dir[RX] = SND_SOC_CLOCK_OUT;
524                 priv->cpu_priv.slot_width = 32;
525                 priv->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
526         } else if (of_device_is_compatible(np, "fsl,imx-audio-sgtl5000")) {
527                 codec_dai_name = "sgtl5000";
528                 priv->codec_priv.mclk_id = SGTL5000_SYSCLK;
529                 priv->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
530         } else if (of_device_is_compatible(np, "fsl,imx-audio-wm8962")) {
531                 codec_dai_name = "wm8962";
532                 priv->card.set_bias_level = fsl_asoc_card_set_bias_level;
533                 priv->codec_priv.mclk_id = WM8962_SYSCLK_MCLK;
534                 priv->codec_priv.fll_id = WM8962_SYSCLK_FLL;
535                 priv->codec_priv.pll_id = WM8962_FLL;
536                 priv->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
537         } else if (of_device_is_compatible(np, "fsl,imx-audio-wm8960")) {
538                 codec_dai_name = "wm8960-hifi";
539                 priv->card.set_bias_level = fsl_asoc_card_set_bias_level;
540                 priv->codec_priv.fll_id = WM8960_SYSCLK_AUTO;
541                 priv->codec_priv.pll_id = WM8960_SYSCLK_AUTO;
542                 priv->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
543         } else if (of_device_is_compatible(np, "fsl,imx-audio-ac97")) {
544                 codec_dai_name = "ac97-hifi";
545                 priv->card.set_bias_level = NULL;
546                 priv->dai_fmt = SND_SOC_DAIFMT_AC97;
547         } else {
548                 dev_err(&pdev->dev, "unknown Device Tree compatible\n");
549                 ret = -EINVAL;
550                 goto asrc_fail;
551         }
552
553         if (!fsl_asoc_card_is_ac97(priv) && !codec_dev) {
554                 dev_err(&pdev->dev, "failed to find codec device\n");
555                 ret = -EINVAL;
556                 goto asrc_fail;
557         }
558
559         /* Common settings for corresponding Freescale CPU DAI driver */
560         if (strstr(cpu_np->name, "ssi")) {
561                 /* Only SSI needs to configure AUDMUX */
562                 ret = fsl_asoc_card_audmux_init(np, priv);
563                 if (ret) {
564                         dev_err(&pdev->dev, "failed to init audmux\n");
565                         goto asrc_fail;
566                 }
567         } else if (strstr(cpu_np->name, "esai")) {
568                 priv->cpu_priv.sysclk_id[1] = ESAI_HCKT_EXTAL;
569                 priv->cpu_priv.sysclk_id[0] = ESAI_HCKR_EXTAL;
570         } else if (strstr(cpu_np->name, "sai")) {
571                 priv->cpu_priv.sysclk_id[1] = FSL_SAI_CLK_MAST1;
572                 priv->cpu_priv.sysclk_id[0] = FSL_SAI_CLK_MAST1;
573         }
574
575         snprintf(priv->name, sizeof(priv->name), "%s-audio",
576                  fsl_asoc_card_is_ac97(priv) ? "ac97" :
577                  codec_dev->name);
578
579         /* Initialize sound card */
580         priv->pdev = pdev;
581         priv->card.dev = &pdev->dev;
582         priv->card.name = priv->name;
583         priv->card.dai_link = priv->dai_link;
584         priv->card.dapm_routes = fsl_asoc_card_is_ac97(priv) ?
585                                  audio_map_ac97 : audio_map;
586         priv->card.late_probe = fsl_asoc_card_late_probe;
587         priv->card.num_dapm_routes = ARRAY_SIZE(audio_map);
588         priv->card.dapm_widgets = fsl_asoc_card_dapm_widgets;
589         priv->card.num_dapm_widgets = ARRAY_SIZE(fsl_asoc_card_dapm_widgets);
590
591         memcpy(priv->dai_link, fsl_asoc_card_dai,
592                sizeof(struct snd_soc_dai_link) * ARRAY_SIZE(priv->dai_link));
593
594         ret = snd_soc_of_parse_audio_routing(&priv->card, "audio-routing");
595         if (ret) {
596                 dev_err(&pdev->dev, "failed to parse audio-routing: %d\n", ret);
597                 goto asrc_fail;
598         }
599
600         /* Normal DAI Link */
601         priv->dai_link[0].cpu_of_node = cpu_np;
602         priv->dai_link[0].codec_dai_name = codec_dai_name;
603
604         if (!fsl_asoc_card_is_ac97(priv))
605                 priv->dai_link[0].codec_of_node = codec_np;
606         else {
607                 u32 idx;
608
609                 ret = of_property_read_u32(cpu_np, "cell-index", &idx);
610                 if (ret) {
611                         dev_err(&pdev->dev,
612                                 "cannot get CPU index property\n");
613                         goto asrc_fail;
614                 }
615
616                 priv->dai_link[0].codec_name =
617                                 devm_kasprintf(&pdev->dev, GFP_KERNEL,
618                                                "ac97-codec.%u",
619                                                (unsigned int)idx);
620         }
621
622         priv->dai_link[0].platform_of_node = cpu_np;
623         priv->dai_link[0].dai_fmt = priv->dai_fmt;
624         priv->card.num_links = 1;
625
626         if (asrc_pdev) {
627                 /* DPCM DAI Links only if ASRC exsits */
628                 priv->dai_link[1].cpu_of_node = asrc_np;
629                 priv->dai_link[1].platform_of_node = asrc_np;
630                 priv->dai_link[2].codec_dai_name = codec_dai_name;
631                 priv->dai_link[2].codec_of_node = codec_np;
632                 priv->dai_link[2].codec_name =
633                                 priv->dai_link[0].codec_name;
634                 priv->dai_link[2].cpu_of_node = cpu_np;
635                 priv->dai_link[2].dai_fmt = priv->dai_fmt;
636                 priv->card.num_links = 3;
637
638                 ret = of_property_read_u32(asrc_np, "fsl,asrc-rate",
639                                            &priv->asrc_rate);
640                 if (ret) {
641                         dev_err(&pdev->dev, "failed to get output rate\n");
642                         ret = -EINVAL;
643                         goto asrc_fail;
644                 }
645
646                 ret = of_property_read_u32(asrc_np, "fsl,asrc-width", &width);
647                 if (ret) {
648                         dev_err(&pdev->dev, "failed to get output rate\n");
649                         ret = -EINVAL;
650                         goto asrc_fail;
651                 }
652
653                 if (width == 24)
654                         priv->asrc_format = SNDRV_PCM_FORMAT_S24_LE;
655                 else
656                         priv->asrc_format = SNDRV_PCM_FORMAT_S16_LE;
657         }
658
659         /* Finish card registering */
660         platform_set_drvdata(pdev, priv);
661         snd_soc_card_set_drvdata(&priv->card, priv);
662
663         ret = devm_snd_soc_register_card(&pdev->dev, &priv->card);
664         if (ret)
665                 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret);
666
667 asrc_fail:
668         of_node_put(asrc_np);
669         of_node_put(codec_np);
670 fail:
671         of_node_put(cpu_np);
672
673         return ret;
674 }
675
676 static const struct of_device_id fsl_asoc_card_dt_ids[] = {
677         { .compatible = "fsl,imx-audio-ac97", },
678         { .compatible = "fsl,imx-audio-cs42888", },
679         { .compatible = "fsl,imx-audio-sgtl5000", },
680         { .compatible = "fsl,imx-audio-wm8962", },
681         { .compatible = "fsl,imx-audio-wm8960", },
682         {}
683 };
684 MODULE_DEVICE_TABLE(of, fsl_asoc_card_dt_ids);
685
686 static struct platform_driver fsl_asoc_card_driver = {
687         .probe = fsl_asoc_card_probe,
688         .driver = {
689                 .name = "fsl-asoc-card",
690                 .pm = &snd_soc_pm_ops,
691                 .of_match_table = fsl_asoc_card_dt_ids,
692         },
693 };
694 module_platform_driver(fsl_asoc_card_driver);
695
696 MODULE_DESCRIPTION("Freescale Generic ASoC Sound Card driver with ASRC");
697 MODULE_AUTHOR("Nicolin Chen <nicoleotsuka@gmail.com>");
698 MODULE_ALIAS("platform:fsl-asoc-card");
699 MODULE_LICENSE("GPL");