Merge tag 'nfs-for-5.9-1' of git://git.linux-nfs.org/projects/trondmy/linux-nfs
[linux-2.6-microblaze.git] / sound / soc / fsl / fsl-asoc-card.c
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Freescale Generic ASoC Sound Card driver with ASRC
4 //
5 // Copyright (C) 2014 Freescale Semiconductor, Inc.
6 //
7 // Author: Nicolin Chen <nicoleotsuka@gmail.com>
8
9 #include <linux/clk.h>
10 #include <linux/i2c.h>
11 #include <linux/module.h>
12 #include <linux/of_platform.h>
13 #if IS_ENABLED(CONFIG_SND_AC97_CODEC)
14 #include <sound/ac97_codec.h>
15 #endif
16 #include <sound/pcm_params.h>
17 #include <sound/soc.h>
18 #include <sound/jack.h>
19 #include <sound/simple_card_utils.h>
20
21 #include "fsl_esai.h"
22 #include "fsl_sai.h"
23 #include "imx-audmux.h"
24
25 #include "../codecs/sgtl5000.h"
26 #include "../codecs/wm8962.h"
27 #include "../codecs/wm8960.h"
28
29 #define CS427x_SYSCLK_MCLK 0
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  * struct codec_priv - CODEC private data
39  * @mclk_freq: Clock rate of MCLK
40  * @mclk_id: MCLK (or main clock) id for set_sysclk()
41  * @fll_id: FLL (or secordary clock) id for set_sysclk()
42  * @pll_id: PLL id for set_pll()
43  */
44 struct codec_priv {
45         unsigned long mclk_freq;
46         u32 mclk_id;
47         u32 fll_id;
48         u32 pll_id;
49 };
50
51 /**
52  * struct cpu_priv - CPU private data
53  * @sysclk_freq: SYSCLK rates for set_sysclk()
54  * @sysclk_dir: SYSCLK directions for set_sysclk()
55  * @sysclk_id: SYSCLK ids for set_sysclk()
56  * @slot_width: Slot width of each frame
57  *
58  * Note: [1] for tx and [0] for rx
59  */
60 struct cpu_priv {
61         unsigned long sysclk_freq[2];
62         u32 sysclk_dir[2];
63         u32 sysclk_id[2];
64         u32 slot_width;
65 };
66
67 /**
68  * struct fsl_asoc_card_priv - Freescale Generic ASOC card private data
69  * @dai_link: DAI link structure including normal one and DPCM link
70  * @hp_jack: Headphone Jack structure
71  * @mic_jack: Microphone Jack structure
72  * @pdev: platform device pointer
73  * @codec_priv: CODEC private data
74  * @cpu_priv: CPU private data
75  * @card: ASoC card structure
76  * @sample_rate: Current sample rate
77  * @sample_format: Current sample format
78  * @asrc_rate: ASRC sample rate used by Back-Ends
79  * @asrc_format: ASRC sample format used by Back-Ends
80  * @dai_fmt: DAI format between CPU and CODEC
81  * @name: Card name
82  */
83
84 struct fsl_asoc_card_priv {
85         struct snd_soc_dai_link dai_link[3];
86         struct asoc_simple_jack hp_jack;
87         struct asoc_simple_jack mic_jack;
88         struct platform_device *pdev;
89         struct codec_priv codec_priv;
90         struct cpu_priv cpu_priv;
91         struct snd_soc_card card;
92         u32 sample_rate;
93         snd_pcm_format_t sample_format;
94         u32 asrc_rate;
95         snd_pcm_format_t asrc_format;
96         u32 dai_fmt;
97         char name[32];
98 };
99
100 /*
101  * This dapm route map exists for DPCM link only.
102  * The other routes shall go through Device Tree.
103  *
104  * Note: keep all ASRC routes in the second half
105  *       to drop them easily for non-ASRC cases.
106  */
107 static const struct snd_soc_dapm_route audio_map[] = {
108         /* 1st half -- Normal DAPM routes */
109         {"Playback",  NULL, "CPU-Playback"},
110         {"CPU-Capture",  NULL, "Capture"},
111         /* 2nd half -- ASRC DAPM routes */
112         {"CPU-Playback",  NULL, "ASRC-Playback"},
113         {"ASRC-Capture",  NULL, "CPU-Capture"},
114 };
115
116 static const struct snd_soc_dapm_route audio_map_ac97[] = {
117         /* 1st half -- Normal DAPM routes */
118         {"Playback",  NULL, "AC97 Playback"},
119         {"AC97 Capture",  NULL, "Capture"},
120         /* 2nd half -- ASRC DAPM routes */
121         {"AC97 Playback",  NULL, "ASRC-Playback"},
122         {"ASRC-Capture",  NULL, "AC97 Capture"},
123 };
124
125 static const struct snd_soc_dapm_route audio_map_tx[] = {
126         /* 1st half -- Normal DAPM routes */
127         {"Playback",  NULL, "CPU-Playback"},
128         /* 2nd half -- ASRC DAPM routes */
129         {"CPU-Playback",  NULL, "ASRC-Playback"},
130 };
131
132 /* Add all possible widgets into here without being redundant */
133 static const struct snd_soc_dapm_widget fsl_asoc_card_dapm_widgets[] = {
134         SND_SOC_DAPM_LINE("Line Out Jack", NULL),
135         SND_SOC_DAPM_LINE("Line In Jack", NULL),
136         SND_SOC_DAPM_HP("Headphone Jack", NULL),
137         SND_SOC_DAPM_SPK("Ext Spk", NULL),
138         SND_SOC_DAPM_MIC("Mic Jack", NULL),
139         SND_SOC_DAPM_MIC("AMIC", NULL),
140         SND_SOC_DAPM_MIC("DMIC", NULL),
141 };
142
143 static bool fsl_asoc_card_is_ac97(struct fsl_asoc_card_priv *priv)
144 {
145         return priv->dai_fmt == SND_SOC_DAIFMT_AC97;
146 }
147
148 static int fsl_asoc_card_hw_params(struct snd_pcm_substream *substream,
149                                    struct snd_pcm_hw_params *params)
150 {
151         struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
152         struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
153         bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
154         struct cpu_priv *cpu_priv = &priv->cpu_priv;
155         struct device *dev = rtd->card->dev;
156         int ret;
157
158         priv->sample_rate = params_rate(params);
159         priv->sample_format = params_format(params);
160
161         /*
162          * If codec-dai is DAI Master and all configurations are already in the
163          * set_bias_level(), bypass the remaining settings in hw_params().
164          * Note: (dai_fmt & CBM_CFM) includes CBM_CFM and CBM_CFS.
165          */
166         if ((priv->card.set_bias_level &&
167              priv->dai_fmt & SND_SOC_DAIFMT_CBM_CFM) ||
168             fsl_asoc_card_is_ac97(priv))
169                 return 0;
170
171         /* Specific configurations of DAIs starts from here */
172         ret = snd_soc_dai_set_sysclk(asoc_rtd_to_cpu(rtd, 0), cpu_priv->sysclk_id[tx],
173                                      cpu_priv->sysclk_freq[tx],
174                                      cpu_priv->sysclk_dir[tx]);
175         if (ret && ret != -ENOTSUPP) {
176                 dev_err(dev, "failed to set sysclk for cpu dai\n");
177                 return ret;
178         }
179
180         if (cpu_priv->slot_width) {
181                 ret = snd_soc_dai_set_tdm_slot(asoc_rtd_to_cpu(rtd, 0), 0x3, 0x3, 2,
182                                                cpu_priv->slot_width);
183                 if (ret && ret != -ENOTSUPP) {
184                         dev_err(dev, "failed to set TDM slot for cpu dai\n");
185                         return ret;
186                 }
187         }
188
189         return 0;
190 }
191
192 static const struct snd_soc_ops fsl_asoc_card_ops = {
193         .hw_params = fsl_asoc_card_hw_params,
194 };
195
196 static int be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
197                               struct snd_pcm_hw_params *params)
198 {
199         struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
200         struct snd_interval *rate;
201         struct snd_mask *mask;
202
203         rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
204         rate->max = rate->min = priv->asrc_rate;
205
206         mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
207         snd_mask_none(mask);
208         snd_mask_set_format(mask, priv->asrc_format);
209
210         return 0;
211 }
212
213 SND_SOC_DAILINK_DEFS(hifi,
214         DAILINK_COMP_ARRAY(COMP_EMPTY()),
215         DAILINK_COMP_ARRAY(COMP_EMPTY()),
216         DAILINK_COMP_ARRAY(COMP_EMPTY()));
217
218 SND_SOC_DAILINK_DEFS(hifi_fe,
219         DAILINK_COMP_ARRAY(COMP_EMPTY()),
220         DAILINK_COMP_ARRAY(COMP_DUMMY()),
221         DAILINK_COMP_ARRAY(COMP_EMPTY()));
222
223 SND_SOC_DAILINK_DEFS(hifi_be,
224         DAILINK_COMP_ARRAY(COMP_EMPTY()),
225         DAILINK_COMP_ARRAY(COMP_EMPTY()),
226         DAILINK_COMP_ARRAY(COMP_DUMMY()));
227
228 static struct snd_soc_dai_link fsl_asoc_card_dai[] = {
229         /* Default ASoC DAI Link*/
230         {
231                 .name = "HiFi",
232                 .stream_name = "HiFi",
233                 .ops = &fsl_asoc_card_ops,
234                 SND_SOC_DAILINK_REG(hifi),
235         },
236         /* DPCM Link between Front-End and Back-End (Optional) */
237         {
238                 .name = "HiFi-ASRC-FE",
239                 .stream_name = "HiFi-ASRC-FE",
240                 .dpcm_playback = 1,
241                 .dpcm_capture = 1,
242                 .dynamic = 1,
243                 SND_SOC_DAILINK_REG(hifi_fe),
244         },
245         {
246                 .name = "HiFi-ASRC-BE",
247                 .stream_name = "HiFi-ASRC-BE",
248                 .be_hw_params_fixup = be_hw_params_fixup,
249                 .ops = &fsl_asoc_card_ops,
250                 .dpcm_playback = 1,
251                 .dpcm_capture = 1,
252                 .no_pcm = 1,
253                 SND_SOC_DAILINK_REG(hifi_be),
254         },
255 };
256
257 static int fsl_asoc_card_set_bias_level(struct snd_soc_card *card,
258                                         struct snd_soc_dapm_context *dapm,
259                                         enum snd_soc_bias_level level)
260 {
261         struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(card);
262         struct snd_soc_pcm_runtime *rtd;
263         struct snd_soc_dai *codec_dai;
264         struct codec_priv *codec_priv = &priv->codec_priv;
265         struct device *dev = card->dev;
266         unsigned int pll_out;
267         int ret;
268
269         rtd = snd_soc_get_pcm_runtime(card, &card->dai_link[0]);
270         codec_dai = asoc_rtd_to_codec(rtd, 0);
271         if (dapm->dev != codec_dai->dev)
272                 return 0;
273
274         switch (level) {
275         case SND_SOC_BIAS_PREPARE:
276                 if (dapm->bias_level != SND_SOC_BIAS_STANDBY)
277                         break;
278
279                 if (priv->sample_format == SNDRV_PCM_FORMAT_S24_LE)
280                         pll_out = priv->sample_rate * 384;
281                 else
282                         pll_out = priv->sample_rate * 256;
283
284                 ret = snd_soc_dai_set_pll(codec_dai, codec_priv->pll_id,
285                                           codec_priv->mclk_id,
286                                           codec_priv->mclk_freq, pll_out);
287                 if (ret) {
288                         dev_err(dev, "failed to start FLL: %d\n", ret);
289                         return ret;
290                 }
291
292                 ret = snd_soc_dai_set_sysclk(codec_dai, codec_priv->fll_id,
293                                              pll_out, SND_SOC_CLOCK_IN);
294                 if (ret && ret != -ENOTSUPP) {
295                         dev_err(dev, "failed to set SYSCLK: %d\n", ret);
296                         return ret;
297                 }
298                 break;
299
300         case SND_SOC_BIAS_STANDBY:
301                 if (dapm->bias_level != SND_SOC_BIAS_PREPARE)
302                         break;
303
304                 ret = snd_soc_dai_set_sysclk(codec_dai, codec_priv->mclk_id,
305                                              codec_priv->mclk_freq,
306                                              SND_SOC_CLOCK_IN);
307                 if (ret && ret != -ENOTSUPP) {
308                         dev_err(dev, "failed to switch away from FLL: %d\n", ret);
309                         return ret;
310                 }
311
312                 ret = snd_soc_dai_set_pll(codec_dai, codec_priv->pll_id, 0, 0, 0);
313                 if (ret) {
314                         dev_err(dev, "failed to stop FLL: %d\n", ret);
315                         return ret;
316                 }
317                 break;
318
319         default:
320                 break;
321         }
322
323         return 0;
324 }
325
326 static int fsl_asoc_card_audmux_init(struct device_node *np,
327                                      struct fsl_asoc_card_priv *priv)
328 {
329         struct device *dev = &priv->pdev->dev;
330         u32 int_ptcr = 0, ext_ptcr = 0;
331         int int_port, ext_port;
332         int ret;
333
334         ret = of_property_read_u32(np, "mux-int-port", &int_port);
335         if (ret) {
336                 dev_err(dev, "mux-int-port missing or invalid\n");
337                 return ret;
338         }
339         ret = of_property_read_u32(np, "mux-ext-port", &ext_port);
340         if (ret) {
341                 dev_err(dev, "mux-ext-port missing or invalid\n");
342                 return ret;
343         }
344
345         /*
346          * The port numbering in the hardware manual starts at 1, while
347          * the AUDMUX API expects it starts at 0.
348          */
349         int_port--;
350         ext_port--;
351
352         /*
353          * Use asynchronous mode (6 wires) for all cases except AC97.
354          * If only 4 wires are needed, just set SSI into
355          * synchronous mode and enable 4 PADs in IOMUX.
356          */
357         switch (priv->dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
358         case SND_SOC_DAIFMT_CBM_CFM:
359                 int_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | ext_port) |
360                            IMX_AUDMUX_V2_PTCR_RCSEL(8 | ext_port) |
361                            IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) |
362                            IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
363                            IMX_AUDMUX_V2_PTCR_RFSDIR |
364                            IMX_AUDMUX_V2_PTCR_RCLKDIR |
365                            IMX_AUDMUX_V2_PTCR_TFSDIR |
366                            IMX_AUDMUX_V2_PTCR_TCLKDIR;
367                 break;
368         case SND_SOC_DAIFMT_CBM_CFS:
369                 int_ptcr = IMX_AUDMUX_V2_PTCR_RCSEL(8 | ext_port) |
370                            IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
371                            IMX_AUDMUX_V2_PTCR_RCLKDIR |
372                            IMX_AUDMUX_V2_PTCR_TCLKDIR;
373                 ext_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | int_port) |
374                            IMX_AUDMUX_V2_PTCR_TFSEL(int_port) |
375                            IMX_AUDMUX_V2_PTCR_RFSDIR |
376                            IMX_AUDMUX_V2_PTCR_TFSDIR;
377                 break;
378         case SND_SOC_DAIFMT_CBS_CFM:
379                 int_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | ext_port) |
380                            IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) |
381                            IMX_AUDMUX_V2_PTCR_RFSDIR |
382                            IMX_AUDMUX_V2_PTCR_TFSDIR;
383                 ext_ptcr = IMX_AUDMUX_V2_PTCR_RCSEL(8 | int_port) |
384                            IMX_AUDMUX_V2_PTCR_TCSEL(int_port) |
385                            IMX_AUDMUX_V2_PTCR_RCLKDIR |
386                            IMX_AUDMUX_V2_PTCR_TCLKDIR;
387                 break;
388         case SND_SOC_DAIFMT_CBS_CFS:
389                 ext_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | int_port) |
390                            IMX_AUDMUX_V2_PTCR_RCSEL(8 | int_port) |
391                            IMX_AUDMUX_V2_PTCR_TFSEL(int_port) |
392                            IMX_AUDMUX_V2_PTCR_TCSEL(int_port) |
393                            IMX_AUDMUX_V2_PTCR_RFSDIR |
394                            IMX_AUDMUX_V2_PTCR_RCLKDIR |
395                            IMX_AUDMUX_V2_PTCR_TFSDIR |
396                            IMX_AUDMUX_V2_PTCR_TCLKDIR;
397                 break;
398         default:
399                 if (!fsl_asoc_card_is_ac97(priv))
400                         return -EINVAL;
401         }
402
403         if (fsl_asoc_card_is_ac97(priv)) {
404                 int_ptcr = IMX_AUDMUX_V2_PTCR_SYN |
405                            IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
406                            IMX_AUDMUX_V2_PTCR_TCLKDIR;
407                 ext_ptcr = IMX_AUDMUX_V2_PTCR_SYN |
408                            IMX_AUDMUX_V2_PTCR_TFSEL(int_port) |
409                            IMX_AUDMUX_V2_PTCR_TFSDIR;
410         }
411
412         /* Asynchronous mode can not be set along with RCLKDIR */
413         if (!fsl_asoc_card_is_ac97(priv)) {
414                 unsigned int pdcr =
415                                 IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port);
416
417                 ret = imx_audmux_v2_configure_port(int_port, 0,
418                                                    pdcr);
419                 if (ret) {
420                         dev_err(dev, "audmux internal port setup failed\n");
421                         return ret;
422                 }
423         }
424
425         ret = imx_audmux_v2_configure_port(int_port, int_ptcr,
426                                            IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port));
427         if (ret) {
428                 dev_err(dev, "audmux internal port setup failed\n");
429                 return ret;
430         }
431
432         if (!fsl_asoc_card_is_ac97(priv)) {
433                 unsigned int pdcr =
434                                 IMX_AUDMUX_V2_PDCR_RXDSEL(int_port);
435
436                 ret = imx_audmux_v2_configure_port(ext_port, 0,
437                                                    pdcr);
438                 if (ret) {
439                         dev_err(dev, "audmux external port setup failed\n");
440                         return ret;
441                 }
442         }
443
444         ret = imx_audmux_v2_configure_port(ext_port, ext_ptcr,
445                                            IMX_AUDMUX_V2_PDCR_RXDSEL(int_port));
446         if (ret) {
447                 dev_err(dev, "audmux external port setup failed\n");
448                 return ret;
449         }
450
451         return 0;
452 }
453
454 static int hp_jack_event(struct notifier_block *nb, unsigned long event,
455                          void *data)
456 {
457         struct snd_soc_jack *jack = (struct snd_soc_jack *)data;
458         struct snd_soc_dapm_context *dapm = &jack->card->dapm;
459
460         if (event & SND_JACK_HEADPHONE)
461                 /* Disable speaker if headphone is plugged in */
462                 snd_soc_dapm_disable_pin(dapm, "Ext Spk");
463         else
464                 snd_soc_dapm_enable_pin(dapm, "Ext Spk");
465
466         return 0;
467 }
468
469 static struct notifier_block hp_jack_nb = {
470         .notifier_call = hp_jack_event,
471 };
472
473 static int mic_jack_event(struct notifier_block *nb, unsigned long event,
474                           void *data)
475 {
476         struct snd_soc_jack *jack = (struct snd_soc_jack *)data;
477         struct snd_soc_dapm_context *dapm = &jack->card->dapm;
478
479         if (event & SND_JACK_MICROPHONE)
480                 /* Disable dmic if microphone is plugged in */
481                 snd_soc_dapm_disable_pin(dapm, "DMIC");
482         else
483                 snd_soc_dapm_enable_pin(dapm, "DMIC");
484
485         return 0;
486 }
487
488 static struct notifier_block mic_jack_nb = {
489         .notifier_call = mic_jack_event,
490 };
491
492 static int fsl_asoc_card_late_probe(struct snd_soc_card *card)
493 {
494         struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(card);
495         struct snd_soc_pcm_runtime *rtd = list_first_entry(
496                         &card->rtd_list, struct snd_soc_pcm_runtime, list);
497         struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
498         struct codec_priv *codec_priv = &priv->codec_priv;
499         struct device *dev = card->dev;
500         int ret;
501
502         if (fsl_asoc_card_is_ac97(priv)) {
503 #if IS_ENABLED(CONFIG_SND_AC97_CODEC)
504                 struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
505                 struct snd_ac97 *ac97 = snd_soc_component_get_drvdata(component);
506
507                 /*
508                  * Use slots 3/4 for S/PDIF so SSI won't try to enable
509                  * other slots and send some samples there
510                  * due to SLOTREQ bits for S/PDIF received from codec
511                  */
512                 snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS,
513                                      AC97_EA_SPSA_SLOT_MASK, AC97_EA_SPSA_3_4);
514 #endif
515
516                 return 0;
517         }
518
519         ret = snd_soc_dai_set_sysclk(codec_dai, codec_priv->mclk_id,
520                                      codec_priv->mclk_freq, SND_SOC_CLOCK_IN);
521         if (ret && ret != -ENOTSUPP) {
522                 dev_err(dev, "failed to set sysclk in %s\n", __func__);
523                 return ret;
524         }
525
526         return 0;
527 }
528
529 static int fsl_asoc_card_probe(struct platform_device *pdev)
530 {
531         struct device_node *cpu_np, *codec_np, *asrc_np;
532         struct device_node *np = pdev->dev.of_node;
533         struct platform_device *asrc_pdev = NULL;
534         struct device_node *bitclkmaster = NULL;
535         struct device_node *framemaster = NULL;
536         struct platform_device *cpu_pdev;
537         struct fsl_asoc_card_priv *priv;
538         struct device *codec_dev = NULL;
539         const char *codec_dai_name;
540         const char *codec_dev_name;
541         unsigned int daifmt;
542         u32 width;
543         int ret;
544
545         priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
546         if (!priv)
547                 return -ENOMEM;
548
549         cpu_np = of_parse_phandle(np, "audio-cpu", 0);
550         /* Give a chance to old DT binding */
551         if (!cpu_np)
552                 cpu_np = of_parse_phandle(np, "ssi-controller", 0);
553         if (!cpu_np) {
554                 dev_err(&pdev->dev, "CPU phandle missing or invalid\n");
555                 ret = -EINVAL;
556                 goto fail;
557         }
558
559         cpu_pdev = of_find_device_by_node(cpu_np);
560         if (!cpu_pdev) {
561                 dev_err(&pdev->dev, "failed to find CPU DAI device\n");
562                 ret = -EINVAL;
563                 goto fail;
564         }
565
566         codec_np = of_parse_phandle(np, "audio-codec", 0);
567         if (codec_np) {
568                 struct platform_device *codec_pdev;
569                 struct i2c_client *codec_i2c;
570
571                 codec_i2c = of_find_i2c_device_by_node(codec_np);
572                 if (codec_i2c) {
573                         codec_dev = &codec_i2c->dev;
574                         codec_dev_name = codec_i2c->name;
575                 }
576                 if (!codec_dev) {
577                         codec_pdev = of_find_device_by_node(codec_np);
578                         if (codec_pdev) {
579                                 codec_dev = &codec_pdev->dev;
580                                 codec_dev_name = codec_pdev->name;
581                         }
582                 }
583         }
584
585         asrc_np = of_parse_phandle(np, "audio-asrc", 0);
586         if (asrc_np)
587                 asrc_pdev = of_find_device_by_node(asrc_np);
588
589         /* Get the MCLK rate only, and leave it controlled by CODEC drivers */
590         if (codec_dev) {
591                 struct clk *codec_clk = clk_get(codec_dev, NULL);
592
593                 if (!IS_ERR(codec_clk)) {
594                         priv->codec_priv.mclk_freq = clk_get_rate(codec_clk);
595                         clk_put(codec_clk);
596                 }
597         }
598
599         /* Default sample rate and format, will be updated in hw_params() */
600         priv->sample_rate = 44100;
601         priv->sample_format = SNDRV_PCM_FORMAT_S16_LE;
602
603         /* Assign a default DAI format, and allow each card to overwrite it */
604         priv->dai_fmt = DAI_FMT_BASE;
605
606         memcpy(priv->dai_link, fsl_asoc_card_dai,
607                sizeof(struct snd_soc_dai_link) * ARRAY_SIZE(priv->dai_link));
608
609         priv->card.dapm_routes = audio_map;
610         priv->card.num_dapm_routes = ARRAY_SIZE(audio_map);
611         /* Diversify the card configurations */
612         if (of_device_is_compatible(np, "fsl,imx-audio-cs42888")) {
613                 codec_dai_name = "cs42888";
614                 priv->card.set_bias_level = NULL;
615                 priv->cpu_priv.sysclk_freq[TX] = priv->codec_priv.mclk_freq;
616                 priv->cpu_priv.sysclk_freq[RX] = priv->codec_priv.mclk_freq;
617                 priv->cpu_priv.sysclk_dir[TX] = SND_SOC_CLOCK_OUT;
618                 priv->cpu_priv.sysclk_dir[RX] = SND_SOC_CLOCK_OUT;
619                 priv->cpu_priv.slot_width = 32;
620                 priv->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
621         } else if (of_device_is_compatible(np, "fsl,imx-audio-cs427x")) {
622                 codec_dai_name = "cs4271-hifi";
623                 priv->codec_priv.mclk_id = CS427x_SYSCLK_MCLK;
624                 priv->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
625         } else if (of_device_is_compatible(np, "fsl,imx-audio-sgtl5000")) {
626                 codec_dai_name = "sgtl5000";
627                 priv->codec_priv.mclk_id = SGTL5000_SYSCLK;
628                 priv->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
629         } else if (of_device_is_compatible(np, "fsl,imx-audio-wm8962")) {
630                 codec_dai_name = "wm8962";
631                 priv->card.set_bias_level = fsl_asoc_card_set_bias_level;
632                 priv->codec_priv.mclk_id = WM8962_SYSCLK_MCLK;
633                 priv->codec_priv.fll_id = WM8962_SYSCLK_FLL;
634                 priv->codec_priv.pll_id = WM8962_FLL;
635                 priv->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
636         } else if (of_device_is_compatible(np, "fsl,imx-audio-wm8960")) {
637                 codec_dai_name = "wm8960-hifi";
638                 priv->card.set_bias_level = fsl_asoc_card_set_bias_level;
639                 priv->codec_priv.fll_id = WM8960_SYSCLK_AUTO;
640                 priv->codec_priv.pll_id = WM8960_SYSCLK_AUTO;
641                 priv->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
642         } else if (of_device_is_compatible(np, "fsl,imx-audio-ac97")) {
643                 codec_dai_name = "ac97-hifi";
644                 priv->card.set_bias_level = NULL;
645                 priv->dai_fmt = SND_SOC_DAIFMT_AC97;
646                 priv->card.dapm_routes = audio_map_ac97;
647                 priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_ac97);
648         } else if (of_device_is_compatible(np, "fsl,imx-audio-mqs")) {
649                 codec_dai_name = "fsl-mqs-dai";
650                 priv->card.set_bias_level = NULL;
651                 priv->dai_fmt = SND_SOC_DAIFMT_LEFT_J |
652                                 SND_SOC_DAIFMT_CBS_CFS |
653                                 SND_SOC_DAIFMT_NB_NF;
654                 priv->dai_link[1].dpcm_capture = 0;
655                 priv->dai_link[2].dpcm_capture = 0;
656                 priv->card.dapm_routes = audio_map_tx;
657                 priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_tx);
658         } else if (of_device_is_compatible(np, "fsl,imx-audio-wm8524")) {
659                 codec_dai_name = "wm8524-hifi";
660                 priv->card.set_bias_level = NULL;
661                 priv->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
662                 priv->dai_link[1].dpcm_capture = 0;
663                 priv->dai_link[2].dpcm_capture = 0;
664                 priv->cpu_priv.slot_width = 32;
665                 priv->card.dapm_routes = audio_map_tx;
666                 priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_tx);
667         } else {
668                 dev_err(&pdev->dev, "unknown Device Tree compatible\n");
669                 ret = -EINVAL;
670                 goto asrc_fail;
671         }
672
673         /* Format info from DT is optional. */
674         daifmt = snd_soc_of_parse_daifmt(np, NULL,
675                                          &bitclkmaster, &framemaster);
676         daifmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
677         if (bitclkmaster || framemaster) {
678                 if (codec_np == bitclkmaster)
679                         daifmt |= (codec_np == framemaster) ?
680                                 SND_SOC_DAIFMT_CBM_CFM : SND_SOC_DAIFMT_CBM_CFS;
681                 else
682                         daifmt |= (codec_np == framemaster) ?
683                                 SND_SOC_DAIFMT_CBS_CFM : SND_SOC_DAIFMT_CBS_CFS;
684
685                 /* Override dai_fmt with value from DT */
686                 priv->dai_fmt = daifmt;
687         }
688
689         /* Change direction according to format */
690         if (priv->dai_fmt & SND_SOC_DAIFMT_CBM_CFM) {
691                 priv->cpu_priv.sysclk_dir[TX] = SND_SOC_CLOCK_IN;
692                 priv->cpu_priv.sysclk_dir[RX] = SND_SOC_CLOCK_IN;
693         }
694
695         of_node_put(bitclkmaster);
696         of_node_put(framemaster);
697
698         if (!fsl_asoc_card_is_ac97(priv) && !codec_dev) {
699                 dev_err(&pdev->dev, "failed to find codec device\n");
700                 ret = -EPROBE_DEFER;
701                 goto asrc_fail;
702         }
703
704         /* Common settings for corresponding Freescale CPU DAI driver */
705         if (of_node_name_eq(cpu_np, "ssi")) {
706                 /* Only SSI needs to configure AUDMUX */
707                 ret = fsl_asoc_card_audmux_init(np, priv);
708                 if (ret) {
709                         dev_err(&pdev->dev, "failed to init audmux\n");
710                         goto asrc_fail;
711                 }
712         } else if (of_node_name_eq(cpu_np, "esai")) {
713                 priv->cpu_priv.sysclk_id[1] = ESAI_HCKT_EXTAL;
714                 priv->cpu_priv.sysclk_id[0] = ESAI_HCKR_EXTAL;
715         } else if (of_node_name_eq(cpu_np, "sai")) {
716                 priv->cpu_priv.sysclk_id[1] = FSL_SAI_CLK_MAST1;
717                 priv->cpu_priv.sysclk_id[0] = FSL_SAI_CLK_MAST1;
718         }
719
720         /* Initialize sound card */
721         priv->pdev = pdev;
722         priv->card.dev = &pdev->dev;
723         ret = snd_soc_of_parse_card_name(&priv->card, "model");
724         if (ret) {
725                 snprintf(priv->name, sizeof(priv->name), "%s-audio",
726                          fsl_asoc_card_is_ac97(priv) ? "ac97" : codec_dev_name);
727                 priv->card.name = priv->name;
728         }
729         priv->card.dai_link = priv->dai_link;
730         priv->card.late_probe = fsl_asoc_card_late_probe;
731         priv->card.dapm_widgets = fsl_asoc_card_dapm_widgets;
732         priv->card.num_dapm_widgets = ARRAY_SIZE(fsl_asoc_card_dapm_widgets);
733
734         /* Drop the second half of DAPM routes -- ASRC */
735         if (!asrc_pdev)
736                 priv->card.num_dapm_routes /= 2;
737
738         if (of_property_read_bool(np, "audio-routing")) {
739                 ret = snd_soc_of_parse_audio_routing(&priv->card, "audio-routing");
740                 if (ret) {
741                         dev_err(&pdev->dev, "failed to parse audio-routing: %d\n", ret);
742                         goto asrc_fail;
743                 }
744         }
745
746         /* Normal DAI Link */
747         priv->dai_link[0].cpus->of_node = cpu_np;
748         priv->dai_link[0].codecs->dai_name = codec_dai_name;
749
750         if (!fsl_asoc_card_is_ac97(priv))
751                 priv->dai_link[0].codecs->of_node = codec_np;
752         else {
753                 u32 idx;
754
755                 ret = of_property_read_u32(cpu_np, "cell-index", &idx);
756                 if (ret) {
757                         dev_err(&pdev->dev,
758                                 "cannot get CPU index property\n");
759                         goto asrc_fail;
760                 }
761
762                 priv->dai_link[0].codecs->name =
763                                 devm_kasprintf(&pdev->dev, GFP_KERNEL,
764                                                "ac97-codec.%u",
765                                                (unsigned int)idx);
766                 if (!priv->dai_link[0].codecs->name) {
767                         ret = -ENOMEM;
768                         goto asrc_fail;
769                 }
770         }
771
772         priv->dai_link[0].platforms->of_node = cpu_np;
773         priv->dai_link[0].dai_fmt = priv->dai_fmt;
774         priv->card.num_links = 1;
775
776         if (asrc_pdev) {
777                 /* DPCM DAI Links only if ASRC exsits */
778                 priv->dai_link[1].cpus->of_node = asrc_np;
779                 priv->dai_link[1].platforms->of_node = asrc_np;
780                 priv->dai_link[2].codecs->dai_name = codec_dai_name;
781                 priv->dai_link[2].codecs->of_node = codec_np;
782                 priv->dai_link[2].codecs->name =
783                                 priv->dai_link[0].codecs->name;
784                 priv->dai_link[2].cpus->of_node = cpu_np;
785                 priv->dai_link[2].dai_fmt = priv->dai_fmt;
786                 priv->card.num_links = 3;
787
788                 ret = of_property_read_u32(asrc_np, "fsl,asrc-rate",
789                                            &priv->asrc_rate);
790                 if (ret) {
791                         dev_err(&pdev->dev, "failed to get output rate\n");
792                         ret = -EINVAL;
793                         goto asrc_fail;
794                 }
795
796                 ret = of_property_read_u32(asrc_np, "fsl,asrc-format",
797                                            &priv->asrc_format);
798                 if (ret) {
799                         /* Fallback to old binding; translate to asrc_format */
800                         ret = of_property_read_u32(asrc_np, "fsl,asrc-width",
801                                                    &width);
802                         if (ret) {
803                                 dev_err(&pdev->dev,
804                                         "failed to decide output format\n");
805                                 goto asrc_fail;
806                         }
807
808                         if (width == 24)
809                                 priv->asrc_format = SNDRV_PCM_FORMAT_S24_LE;
810                         else
811                                 priv->asrc_format = SNDRV_PCM_FORMAT_S16_LE;
812                 }
813         }
814
815         /* Finish card registering */
816         platform_set_drvdata(pdev, priv);
817         snd_soc_card_set_drvdata(&priv->card, priv);
818
819         ret = devm_snd_soc_register_card(&pdev->dev, &priv->card);
820         if (ret) {
821                 if (ret != -EPROBE_DEFER)
822                         dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret);
823                 goto asrc_fail;
824         }
825
826         /*
827          * Properties "hp-det-gpio" and "mic-det-gpio" are optional, and
828          * asoc_simple_init_jack uses these properties for creating
829          * Headphone Jack and Microphone Jack.
830          *
831          * The notifier is initialized in snd_soc_card_jack_new(), then
832          * snd_soc_jack_notifier_register can be called.
833          */
834         if (of_property_read_bool(np, "hp-det-gpio")) {
835                 ret = asoc_simple_init_jack(&priv->card, &priv->hp_jack,
836                                             1, NULL, "Headphone Jack");
837                 if (ret)
838                         goto asrc_fail;
839
840                 snd_soc_jack_notifier_register(&priv->hp_jack.jack, &hp_jack_nb);
841         }
842
843         if (of_property_read_bool(np, "mic-det-gpio")) {
844                 ret = asoc_simple_init_jack(&priv->card, &priv->mic_jack,
845                                             0, NULL, "Mic Jack");
846                 if (ret)
847                         goto asrc_fail;
848
849                 snd_soc_jack_notifier_register(&priv->mic_jack.jack, &mic_jack_nb);
850         }
851
852 asrc_fail:
853         of_node_put(asrc_np);
854         of_node_put(codec_np);
855         put_device(&cpu_pdev->dev);
856 fail:
857         of_node_put(cpu_np);
858
859         return ret;
860 }
861
862 static const struct of_device_id fsl_asoc_card_dt_ids[] = {
863         { .compatible = "fsl,imx-audio-ac97", },
864         { .compatible = "fsl,imx-audio-cs42888", },
865         { .compatible = "fsl,imx-audio-cs427x", },
866         { .compatible = "fsl,imx-audio-sgtl5000", },
867         { .compatible = "fsl,imx-audio-wm8962", },
868         { .compatible = "fsl,imx-audio-wm8960", },
869         { .compatible = "fsl,imx-audio-mqs", },
870         { .compatible = "fsl,imx-audio-wm8524", },
871         {}
872 };
873 MODULE_DEVICE_TABLE(of, fsl_asoc_card_dt_ids);
874
875 static struct platform_driver fsl_asoc_card_driver = {
876         .probe = fsl_asoc_card_probe,
877         .driver = {
878                 .name = "fsl-asoc-card",
879                 .pm = &snd_soc_pm_ops,
880                 .of_match_table = fsl_asoc_card_dt_ids,
881         },
882 };
883 module_platform_driver(fsl_asoc_card_driver);
884
885 MODULE_DESCRIPTION("Freescale Generic ASoC Sound Card driver with ASRC");
886 MODULE_AUTHOR("Nicolin Chen <nicoleotsuka@gmail.com>");
887 MODULE_ALIAS("platform:fsl-asoc-card");
888 MODULE_LICENSE("GPL");