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