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