Linux 6.13
[linux.git] / sound / soc / fsl / fsl-asoc-card.c
blob02e1594e8223c920f9d9250a75932cbb14914258
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>
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>
21 #include "fsl_esai.h"
22 #include "fsl_sai.h"
23 #include "imx-audmux.h"
25 #include "../codecs/sgtl5000.h"
26 #include "../codecs/wm8962.h"
27 #include "../codecs/wm8960.h"
28 #include "../codecs/wm8994.h"
29 #include "../codecs/tlv320aic31xx.h"
30 #include "../codecs/nau8822.h"
31 #include "../codecs/wm8904.h"
33 #define DRIVER_NAME "fsl-asoc-card"
35 #define CS427x_SYSCLK_MCLK 0
37 #define RX 0
38 #define TX 1
40 /* Default DAI format without Master and Slave flag */
41 #define DAI_FMT_BASE (SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF)
43 /**
44 * struct codec_priv - CODEC private data
45 * @mclk: Main clock of the CODEC
46 * @mclk_freq: Clock rate of MCLK
47 * @free_freq: Clock rate of MCLK for hw_free()
48 * @mclk_id: MCLK (or main clock) id for set_sysclk()
49 * @fll_id: FLL (or secordary clock) id for set_sysclk()
50 * @pll_id: PLL id for set_pll()
52 struct codec_priv {
53 struct clk *mclk;
54 unsigned long mclk_freq;
55 unsigned long free_freq;
56 u32 mclk_id;
57 int fll_id;
58 int pll_id;
61 /**
62 * struct cpu_priv - CPU private data
63 * @sysclk_freq: SYSCLK rates for set_sysclk()
64 * @sysclk_dir: SYSCLK directions for set_sysclk()
65 * @sysclk_id: SYSCLK ids for set_sysclk()
66 * @slot_width: Slot width of each frame
67 * @slot_num: Number of slots of each frame
69 * Note: [1] for tx and [0] for rx
71 struct cpu_priv {
72 unsigned long sysclk_freq[2];
73 u32 sysclk_dir[2];
74 u32 sysclk_id[2];
75 u32 slot_width;
76 u32 slot_num;
79 /**
80 * struct fsl_asoc_card_priv - Freescale Generic ASOC card private data
81 * @dai_link: DAI link structure including normal one and DPCM link
82 * @hp_jack: Headphone Jack structure
83 * @mic_jack: Microphone Jack structure
84 * @pdev: platform device pointer
85 * @codec_priv: CODEC private data
86 * @cpu_priv: CPU private data
87 * @card: ASoC card structure
88 * @streams: Mask of current active streams
89 * @sample_rate: Current sample rate
90 * @sample_format: Current sample format
91 * @asrc_rate: ASRC sample rate used by Back-Ends
92 * @asrc_format: ASRC sample format used by Back-Ends
93 * @dai_fmt: DAI format between CPU and CODEC
94 * @name: Card name
97 struct fsl_asoc_card_priv {
98 struct snd_soc_dai_link dai_link[3];
99 struct simple_util_jack hp_jack;
100 struct simple_util_jack mic_jack;
101 struct platform_device *pdev;
102 struct codec_priv codec_priv[2];
103 struct cpu_priv cpu_priv;
104 struct snd_soc_card card;
105 u8 streams;
106 u32 sample_rate;
107 snd_pcm_format_t sample_format;
108 u32 asrc_rate;
109 snd_pcm_format_t asrc_format;
110 u32 dai_fmt;
111 char name[32];
115 * This dapm route map exists for DPCM link only.
116 * The other routes shall go through Device Tree.
118 * Note: keep all ASRC routes in the second half
119 * to drop them easily for non-ASRC cases.
121 static const struct snd_soc_dapm_route audio_map[] = {
122 /* 1st half -- Normal DAPM routes */
123 {"Playback", NULL, "CPU-Playback"},
124 {"CPU-Capture", NULL, "Capture"},
125 /* 2nd half -- ASRC DAPM routes */
126 {"CPU-Playback", NULL, "ASRC-Playback"},
127 {"ASRC-Capture", NULL, "CPU-Capture"},
130 static const struct snd_soc_dapm_route audio_map_ac97[] = {
131 /* 1st half -- Normal DAPM routes */
132 {"AC97 Playback", NULL, "CPU AC97 Playback"},
133 {"CPU AC97 Capture", NULL, "AC97 Capture"},
134 /* 2nd half -- ASRC DAPM routes */
135 {"CPU AC97 Playback", NULL, "ASRC-Playback"},
136 {"ASRC-Capture", NULL, "CPU AC97 Capture"},
139 static const struct snd_soc_dapm_route audio_map_tx[] = {
140 /* 1st half -- Normal DAPM routes */
141 {"Playback", NULL, "CPU-Playback"},
142 /* 2nd half -- ASRC DAPM routes */
143 {"CPU-Playback", NULL, "ASRC-Playback"},
146 static const struct snd_soc_dapm_route audio_map_rx[] = {
147 /* 1st half -- Normal DAPM routes */
148 {"CPU-Capture", NULL, "Capture"},
149 /* 2nd half -- ASRC DAPM routes */
150 {"ASRC-Capture", NULL, "CPU-Capture"},
153 /* Add all possible widgets into here without being redundant */
154 static const struct snd_soc_dapm_widget fsl_asoc_card_dapm_widgets[] = {
155 SND_SOC_DAPM_LINE("Line Out Jack", NULL),
156 SND_SOC_DAPM_LINE("Line In Jack", NULL),
157 SND_SOC_DAPM_HP("Headphone Jack", NULL),
158 SND_SOC_DAPM_SPK("Ext Spk", NULL),
159 SND_SOC_DAPM_MIC("Mic Jack", NULL),
160 SND_SOC_DAPM_MIC("AMIC", NULL),
161 SND_SOC_DAPM_MIC("DMIC", NULL),
164 static bool fsl_asoc_card_is_ac97(struct fsl_asoc_card_priv *priv)
166 return priv->dai_fmt == SND_SOC_DAIFMT_AC97;
169 static int fsl_asoc_card_hw_params(struct snd_pcm_substream *substream,
170 struct snd_pcm_hw_params *params)
172 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
173 struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
174 bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
175 struct codec_priv *codec_priv;
176 struct snd_soc_dai *codec_dai;
177 struct cpu_priv *cpu_priv = &priv->cpu_priv;
178 struct device *dev = rtd->card->dev;
179 unsigned int pll_out;
180 int codec_idx;
181 int ret;
183 priv->sample_rate = params_rate(params);
184 priv->sample_format = params_format(params);
185 priv->streams |= BIT(substream->stream);
187 if (fsl_asoc_card_is_ac97(priv))
188 return 0;
190 /* Specific configurations of DAIs starts from here */
191 ret = snd_soc_dai_set_sysclk(snd_soc_rtd_to_cpu(rtd, 0), cpu_priv->sysclk_id[tx],
192 cpu_priv->sysclk_freq[tx],
193 cpu_priv->sysclk_dir[tx]);
194 if (ret && ret != -ENOTSUPP) {
195 dev_err(dev, "failed to set sysclk for cpu dai\n");
196 goto fail;
199 if (cpu_priv->slot_width) {
200 if (!cpu_priv->slot_num)
201 cpu_priv->slot_num = 2;
203 ret = snd_soc_dai_set_tdm_slot(snd_soc_rtd_to_cpu(rtd, 0), 0x3, 0x3,
204 cpu_priv->slot_num,
205 cpu_priv->slot_width);
206 if (ret && ret != -ENOTSUPP) {
207 dev_err(dev, "failed to set TDM slot for cpu dai\n");
208 goto fail;
212 /* Specific configuration for PLL */
213 for_each_rtd_codec_dais(rtd, codec_idx, codec_dai) {
214 codec_priv = &priv->codec_priv[codec_idx];
216 if (codec_priv->pll_id >= 0 && codec_priv->fll_id >= 0) {
217 if (priv->sample_format == SNDRV_PCM_FORMAT_S24_LE)
218 pll_out = priv->sample_rate * 384;
219 else
220 pll_out = priv->sample_rate * 256;
222 ret = snd_soc_dai_set_pll(codec_dai,
223 codec_priv->pll_id,
224 codec_priv->mclk_id,
225 codec_priv->mclk_freq, pll_out);
226 if (ret) {
227 dev_err(dev, "failed to start FLL: %d\n", ret);
228 goto fail;
231 ret = snd_soc_dai_set_sysclk(codec_dai,
232 codec_priv->fll_id,
233 pll_out, SND_SOC_CLOCK_IN);
235 if (ret && ret != -ENOTSUPP) {
236 dev_err(dev, "failed to set SYSCLK: %d\n", ret);
237 goto fail;
242 return 0;
244 fail:
245 priv->streams &= ~BIT(substream->stream);
246 return ret;
249 static int fsl_asoc_card_hw_free(struct snd_pcm_substream *substream)
251 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
252 struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
253 struct codec_priv *codec_priv;
254 struct snd_soc_dai *codec_dai;
255 struct device *dev = rtd->card->dev;
256 int codec_idx;
257 int ret;
259 priv->streams &= ~BIT(substream->stream);
261 for_each_rtd_codec_dais(rtd, codec_idx, codec_dai) {
262 codec_priv = &priv->codec_priv[codec_idx];
264 if (!priv->streams && codec_priv->pll_id >= 0 && codec_priv->fll_id >= 0) {
265 /* Force freq to be free_freq to avoid error message in codec */
266 ret = snd_soc_dai_set_sysclk(codec_dai,
267 codec_priv->mclk_id,
268 codec_priv->free_freq,
269 SND_SOC_CLOCK_IN);
270 if (ret) {
271 dev_err(dev, "failed to switch away from FLL: %d\n", ret);
272 return ret;
275 ret = snd_soc_dai_set_pll(codec_dai,
276 codec_priv->pll_id, 0, 0, 0);
277 if (ret && ret != -ENOTSUPP) {
278 dev_err(dev, "failed to stop FLL: %d\n", ret);
279 return ret;
284 return 0;
287 static const struct snd_soc_ops fsl_asoc_card_ops = {
288 .hw_params = fsl_asoc_card_hw_params,
289 .hw_free = fsl_asoc_card_hw_free,
292 static int be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
293 struct snd_pcm_hw_params *params)
295 struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
296 struct snd_interval *rate;
297 struct snd_mask *mask;
299 rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
300 rate->max = rate->min = priv->asrc_rate;
302 mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
303 snd_mask_none(mask);
304 snd_mask_set_format(mask, priv->asrc_format);
306 return 0;
309 static const struct snd_soc_dai_link fsl_asoc_card_dai[] = {
310 /* Default ASoC DAI Link*/
312 .name = "HiFi",
313 .stream_name = "HiFi",
314 .ops = &fsl_asoc_card_ops,
316 /* DPCM Link between Front-End and Back-End (Optional) */
318 .name = "HiFi-ASRC-FE",
319 .stream_name = "HiFi-ASRC-FE",
320 .dynamic = 1,
323 .name = "HiFi-ASRC-BE",
324 .stream_name = "HiFi-ASRC-BE",
325 .be_hw_params_fixup = be_hw_params_fixup,
326 .ops = &fsl_asoc_card_ops,
327 .no_pcm = 1,
331 static int fsl_asoc_card_audmux_init(struct device_node *np,
332 struct fsl_asoc_card_priv *priv)
334 struct device *dev = &priv->pdev->dev;
335 u32 int_ptcr = 0, ext_ptcr = 0;
336 int int_port, ext_port;
337 int ret;
339 ret = of_property_read_u32(np, "mux-int-port", &int_port);
340 if (ret) {
341 dev_err(dev, "mux-int-port missing or invalid\n");
342 return ret;
344 ret = of_property_read_u32(np, "mux-ext-port", &ext_port);
345 if (ret) {
346 dev_err(dev, "mux-ext-port missing or invalid\n");
347 return ret;
351 * The port numbering in the hardware manual starts at 1, while
352 * the AUDMUX API expects it starts at 0.
354 int_port--;
355 ext_port--;
358 * Use asynchronous mode (6 wires) for all cases except AC97.
359 * If only 4 wires are needed, just set SSI into
360 * synchronous mode and enable 4 PADs in IOMUX.
362 switch (priv->dai_fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
363 case SND_SOC_DAIFMT_CBP_CFP:
364 int_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | ext_port) |
365 IMX_AUDMUX_V2_PTCR_RCSEL(8 | ext_port) |
366 IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) |
367 IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
368 IMX_AUDMUX_V2_PTCR_RFSDIR |
369 IMX_AUDMUX_V2_PTCR_RCLKDIR |
370 IMX_AUDMUX_V2_PTCR_TFSDIR |
371 IMX_AUDMUX_V2_PTCR_TCLKDIR;
372 break;
373 case SND_SOC_DAIFMT_CBP_CFC:
374 int_ptcr = IMX_AUDMUX_V2_PTCR_RCSEL(8 | ext_port) |
375 IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
376 IMX_AUDMUX_V2_PTCR_RCLKDIR |
377 IMX_AUDMUX_V2_PTCR_TCLKDIR;
378 ext_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | int_port) |
379 IMX_AUDMUX_V2_PTCR_TFSEL(int_port) |
380 IMX_AUDMUX_V2_PTCR_RFSDIR |
381 IMX_AUDMUX_V2_PTCR_TFSDIR;
382 break;
383 case SND_SOC_DAIFMT_CBC_CFP:
384 int_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | ext_port) |
385 IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) |
386 IMX_AUDMUX_V2_PTCR_RFSDIR |
387 IMX_AUDMUX_V2_PTCR_TFSDIR;
388 ext_ptcr = IMX_AUDMUX_V2_PTCR_RCSEL(8 | int_port) |
389 IMX_AUDMUX_V2_PTCR_TCSEL(int_port) |
390 IMX_AUDMUX_V2_PTCR_RCLKDIR |
391 IMX_AUDMUX_V2_PTCR_TCLKDIR;
392 break;
393 case SND_SOC_DAIFMT_CBC_CFC:
394 ext_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | int_port) |
395 IMX_AUDMUX_V2_PTCR_RCSEL(8 | int_port) |
396 IMX_AUDMUX_V2_PTCR_TFSEL(int_port) |
397 IMX_AUDMUX_V2_PTCR_TCSEL(int_port) |
398 IMX_AUDMUX_V2_PTCR_RFSDIR |
399 IMX_AUDMUX_V2_PTCR_RCLKDIR |
400 IMX_AUDMUX_V2_PTCR_TFSDIR |
401 IMX_AUDMUX_V2_PTCR_TCLKDIR;
402 break;
403 default:
404 if (!fsl_asoc_card_is_ac97(priv))
405 return -EINVAL;
408 if (fsl_asoc_card_is_ac97(priv)) {
409 int_ptcr = IMX_AUDMUX_V2_PTCR_SYN |
410 IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
411 IMX_AUDMUX_V2_PTCR_TCLKDIR;
412 ext_ptcr = IMX_AUDMUX_V2_PTCR_SYN |
413 IMX_AUDMUX_V2_PTCR_TFSEL(int_port) |
414 IMX_AUDMUX_V2_PTCR_TFSDIR;
417 /* Asynchronous mode can not be set along with RCLKDIR */
418 if (!fsl_asoc_card_is_ac97(priv)) {
419 unsigned int pdcr =
420 IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port);
422 ret = imx_audmux_v2_configure_port(int_port, 0,
423 pdcr);
424 if (ret) {
425 dev_err(dev, "audmux internal port setup failed\n");
426 return ret;
430 ret = imx_audmux_v2_configure_port(int_port, int_ptcr,
431 IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port));
432 if (ret) {
433 dev_err(dev, "audmux internal port setup failed\n");
434 return ret;
437 if (!fsl_asoc_card_is_ac97(priv)) {
438 unsigned int pdcr =
439 IMX_AUDMUX_V2_PDCR_RXDSEL(int_port);
441 ret = imx_audmux_v2_configure_port(ext_port, 0,
442 pdcr);
443 if (ret) {
444 dev_err(dev, "audmux external port setup failed\n");
445 return ret;
449 ret = imx_audmux_v2_configure_port(ext_port, ext_ptcr,
450 IMX_AUDMUX_V2_PDCR_RXDSEL(int_port));
451 if (ret) {
452 dev_err(dev, "audmux external port setup failed\n");
453 return ret;
456 return 0;
459 static int fsl_asoc_card_spdif_init(struct device_node *codec_np[],
460 struct device_node *cpu_np,
461 const char *codec_dai_name[],
462 struct fsl_asoc_card_priv *priv)
464 struct device *dev = &priv->pdev->dev;
465 struct device_node *np = dev->of_node;
467 if (!of_node_name_eq(cpu_np, "spdif")) {
468 dev_err(dev, "CPU phandle invalid, should be an SPDIF device\n");
469 return -EINVAL;
472 priv->dai_link[0].playback_only = true;
473 priv->dai_link[0].capture_only = true;
475 for (int i = 0; i < 2; i++) {
476 if (!codec_np[i])
477 break;
479 if (of_device_is_compatible(codec_np[i], "linux,spdif-dit")) {
480 priv->dai_link[0].capture_only = false;
481 codec_dai_name[i] = "dit-hifi";
482 } else if (of_device_is_compatible(codec_np[i], "linux,spdif-dir")) {
483 priv->dai_link[0].playback_only = false;
484 codec_dai_name[i] = "dir-hifi";
488 // Old SPDIF DT binding
489 if (!codec_np[0]) {
490 codec_dai_name[0] = snd_soc_dummy_dlc.dai_name;
491 if (of_property_read_bool(np, "spdif-out"))
492 priv->dai_link[0].capture_only = false;
493 if (of_property_read_bool(np, "spdif-in"))
494 priv->dai_link[0].playback_only = false;
497 if (priv->dai_link[0].playback_only && priv->dai_link[0].capture_only) {
498 dev_err(dev, "no enabled S/PDIF DAI link\n");
499 return -EINVAL;
502 if (priv->dai_link[0].playback_only) {
503 priv->dai_link[1].playback_only = true;
504 priv->dai_link[2].playback_only = true;
505 priv->card.dapm_routes = audio_map_tx;
506 priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_tx);
507 } else if (priv->dai_link[0].capture_only) {
508 priv->dai_link[1].capture_only = true;
509 priv->dai_link[2].capture_only = true;
510 priv->card.dapm_routes = audio_map_rx;
511 priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_rx);
514 // No DAPM routes with old bindings and dummy codec
515 if (!codec_np[0]) {
516 priv->card.dapm_routes = NULL;
517 priv->card.num_dapm_routes = 0;
520 if (codec_np[0] && codec_np[1]) {
521 priv->dai_link[0].num_codecs = 2;
522 priv->dai_link[2].num_codecs = 2;
525 return 0;
528 static int hp_jack_event(struct notifier_block *nb, unsigned long event,
529 void *data)
531 struct snd_soc_jack *jack = (struct snd_soc_jack *)data;
532 struct snd_soc_dapm_context *dapm = &jack->card->dapm;
534 if (event & SND_JACK_HEADPHONE)
535 /* Disable speaker if headphone is plugged in */
536 return snd_soc_dapm_disable_pin(dapm, "Ext Spk");
537 else
538 return snd_soc_dapm_enable_pin(dapm, "Ext Spk");
541 static struct notifier_block hp_jack_nb = {
542 .notifier_call = hp_jack_event,
545 static int mic_jack_event(struct notifier_block *nb, unsigned long event,
546 void *data)
548 struct snd_soc_jack *jack = (struct snd_soc_jack *)data;
549 struct snd_soc_dapm_context *dapm = &jack->card->dapm;
551 if (event & SND_JACK_MICROPHONE)
552 /* Disable dmic if microphone is plugged in */
553 return snd_soc_dapm_disable_pin(dapm, "DMIC");
554 else
555 return snd_soc_dapm_enable_pin(dapm, "DMIC");
558 static struct notifier_block mic_jack_nb = {
559 .notifier_call = mic_jack_event,
562 static int fsl_asoc_card_late_probe(struct snd_soc_card *card)
564 struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(card);
565 struct snd_soc_pcm_runtime *rtd = list_first_entry(
566 &card->rtd_list, struct snd_soc_pcm_runtime, list);
567 struct snd_soc_dai *codec_dai;
568 struct codec_priv *codec_priv;
569 struct device *dev = card->dev;
570 int codec_idx;
571 int ret;
573 if (fsl_asoc_card_is_ac97(priv)) {
574 #if IS_ENABLED(CONFIG_SND_AC97_CODEC)
575 struct snd_soc_component *component = snd_soc_rtd_to_codec(rtd, 0)->component;
576 struct snd_ac97 *ac97 = snd_soc_component_get_drvdata(component);
579 * Use slots 3/4 for S/PDIF so SSI won't try to enable
580 * other slots and send some samples there
581 * due to SLOTREQ bits for S/PDIF received from codec
583 snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS,
584 AC97_EA_SPSA_SLOT_MASK, AC97_EA_SPSA_3_4);
585 #endif
587 return 0;
590 for_each_rtd_codec_dais(rtd, codec_idx, codec_dai) {
591 codec_priv = &priv->codec_priv[codec_idx];
593 ret = snd_soc_dai_set_sysclk(codec_dai, codec_priv->mclk_id,
594 codec_priv->mclk_freq, SND_SOC_CLOCK_IN);
595 if (ret && ret != -ENOTSUPP) {
596 dev_err(dev, "failed to set sysclk in %s\n", __func__);
597 return ret;
600 if (!IS_ERR_OR_NULL(codec_priv->mclk))
601 clk_prepare_enable(codec_priv->mclk);
604 return 0;
607 static int fsl_asoc_card_probe(struct platform_device *pdev)
609 struct device_node *cpu_np, *asrc_np;
610 struct snd_soc_dai_link_component *codec_comp;
611 struct device_node *codec_np[2];
612 struct device_node *np = pdev->dev.of_node;
613 struct platform_device *asrc_pdev = NULL;
614 struct device_node *bitclkprovider = NULL;
615 struct device_node *frameprovider = NULL;
616 struct platform_device *cpu_pdev;
617 struct fsl_asoc_card_priv *priv;
618 struct device *codec_dev[2] = { NULL, NULL };
619 struct snd_soc_dai_link_component *dlc;
620 const char *codec_dai_name[2];
621 const char *codec_dev_name[2];
622 u32 asrc_fmt = 0;
623 int codec_idx;
624 u32 width;
625 int ret;
627 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
628 if (!priv)
629 return -ENOMEM;
631 priv->pdev = pdev;
633 cpu_np = of_parse_phandle(np, "audio-cpu", 0);
634 /* Give a chance to old DT bindings */
635 if (!cpu_np)
636 cpu_np = of_parse_phandle(np, "ssi-controller", 0);
637 if (!cpu_np)
638 cpu_np = of_parse_phandle(np, "spdif-controller", 0);
639 if (!cpu_np) {
640 dev_err(&pdev->dev, "CPU phandle missing or invalid\n");
641 ret = -EINVAL;
642 goto fail;
645 cpu_pdev = of_find_device_by_node(cpu_np);
646 if (!cpu_pdev) {
647 dev_err(&pdev->dev, "failed to find CPU DAI device\n");
648 ret = -EINVAL;
649 goto fail;
652 codec_np[0] = of_parse_phandle(np, "audio-codec", 0);
653 codec_np[1] = of_parse_phandle(np, "audio-codec", 1);
655 for (codec_idx = 0; codec_idx < 2; codec_idx++) {
656 if (codec_np[codec_idx]) {
657 struct platform_device *codec_pdev;
658 struct i2c_client *codec_i2c;
660 codec_i2c = of_find_i2c_device_by_node(codec_np[codec_idx]);
661 if (codec_i2c) {
662 codec_dev[codec_idx] = &codec_i2c->dev;
663 codec_dev_name[codec_idx] = codec_i2c->name;
665 if (!codec_dev[codec_idx]) {
666 codec_pdev = of_find_device_by_node(codec_np[codec_idx]);
667 if (codec_pdev) {
668 codec_dev[codec_idx] = &codec_pdev->dev;
669 codec_dev_name[codec_idx] = codec_pdev->name;
675 asrc_np = of_parse_phandle(np, "audio-asrc", 0);
676 if (asrc_np)
677 asrc_pdev = of_find_device_by_node(asrc_np);
679 /* Get the MCLK rate only, and leave it controlled by CODEC drivers */
680 for (codec_idx = 0; codec_idx < 2; codec_idx++) {
681 if (codec_dev[codec_idx]) {
682 struct clk *codec_clk = clk_get(codec_dev[codec_idx], NULL);
684 if (!IS_ERR(codec_clk)) {
685 priv->codec_priv[codec_idx].mclk_freq = clk_get_rate(codec_clk);
686 clk_put(codec_clk);
691 /* Default sample rate and format, will be updated in hw_params() */
692 priv->sample_rate = 44100;
693 priv->sample_format = SNDRV_PCM_FORMAT_S16_LE;
695 /* Assign a default DAI format, and allow each card to overwrite it */
696 priv->dai_fmt = DAI_FMT_BASE;
698 memcpy(priv->dai_link, fsl_asoc_card_dai,
699 sizeof(struct snd_soc_dai_link) * ARRAY_SIZE(priv->dai_link));
701 * "Default ASoC DAI Link": 1 cpus, 2 codecs, 1 platforms
702 * "DPCM Link Front-End": 1 cpus, 1 codecs (dummy), 1 platforms
703 * "DPCM Link Back-End": 1 cpus, 2 codecs
704 * totally 10 components
706 dlc = devm_kcalloc(&pdev->dev, 10, sizeof(*dlc), GFP_KERNEL);
707 if (!dlc) {
708 ret = -ENOMEM;
709 goto asrc_fail;
712 priv->dai_link[0].cpus = &dlc[0];
713 priv->dai_link[0].num_cpus = 1;
714 priv->dai_link[0].codecs = &dlc[1];
715 priv->dai_link[0].num_codecs = 1;
716 priv->dai_link[0].platforms = &dlc[3];
717 priv->dai_link[0].num_platforms = 1;
719 priv->dai_link[1].cpus = &dlc[4];
720 priv->dai_link[1].num_cpus = 1;
721 priv->dai_link[1].codecs = &dlc[5];
722 priv->dai_link[1].num_codecs = 0; /* dummy */
723 priv->dai_link[1].platforms = &dlc[6];
724 priv->dai_link[1].num_platforms = 1;
726 priv->dai_link[2].cpus = &dlc[7];
727 priv->dai_link[2].num_cpus = 1;
728 priv->dai_link[2].codecs = &dlc[8];
729 priv->dai_link[2].num_codecs = 1;
731 priv->card.dapm_routes = audio_map;
732 priv->card.num_dapm_routes = ARRAY_SIZE(audio_map);
733 priv->card.driver_name = DRIVER_NAME;
735 for (codec_idx = 0; codec_idx < 2; codec_idx++) {
736 priv->codec_priv[codec_idx].fll_id = -1;
737 priv->codec_priv[codec_idx].pll_id = -1;
740 /* Diversify the card configurations */
741 if (of_device_is_compatible(np, "fsl,imx-audio-cs42888")) {
742 codec_dai_name[0] = "cs42888";
743 priv->cpu_priv.sysclk_freq[TX] = priv->codec_priv[0].mclk_freq;
744 priv->cpu_priv.sysclk_freq[RX] = priv->codec_priv[0].mclk_freq;
745 priv->cpu_priv.sysclk_dir[TX] = SND_SOC_CLOCK_OUT;
746 priv->cpu_priv.sysclk_dir[RX] = SND_SOC_CLOCK_OUT;
747 priv->cpu_priv.slot_width = 32;
748 priv->dai_fmt |= SND_SOC_DAIFMT_CBC_CFC;
749 } else if (of_device_is_compatible(np, "fsl,imx-audio-cs427x")) {
750 codec_dai_name[0] = "cs4271-hifi";
751 priv->codec_priv[0].mclk_id = CS427x_SYSCLK_MCLK;
752 priv->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
753 } else if (of_device_is_compatible(np, "fsl,imx-audio-sgtl5000")) {
754 codec_dai_name[0] = "sgtl5000";
755 priv->codec_priv[0].mclk_id = SGTL5000_SYSCLK;
756 priv->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
757 } else if (of_device_is_compatible(np, "fsl,imx-audio-tlv320aic32x4")) {
758 codec_dai_name[0] = "tlv320aic32x4-hifi";
759 priv->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
760 } else if (of_device_is_compatible(np, "fsl,imx-audio-tlv320aic31xx")) {
761 codec_dai_name[0] = "tlv320dac31xx-hifi";
762 priv->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
763 priv->dai_link[1].playback_only = 1;
764 priv->dai_link[2].playback_only = 1;
765 priv->cpu_priv.sysclk_dir[TX] = SND_SOC_CLOCK_OUT;
766 priv->cpu_priv.sysclk_dir[RX] = SND_SOC_CLOCK_OUT;
767 priv->card.dapm_routes = audio_map_tx;
768 priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_tx);
769 } else if (of_device_is_compatible(np, "fsl,imx-audio-wm8962")) {
770 codec_dai_name[0] = "wm8962";
771 priv->codec_priv[0].mclk_id = WM8962_SYSCLK_MCLK;
772 priv->codec_priv[0].fll_id = WM8962_SYSCLK_FLL;
773 priv->codec_priv[0].pll_id = WM8962_FLL;
774 priv->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
775 } else if (of_device_is_compatible(np, "fsl,imx-audio-wm8960")) {
776 codec_dai_name[0] = "wm8960-hifi";
777 priv->codec_priv[0].fll_id = WM8960_SYSCLK_AUTO;
778 priv->codec_priv[0].pll_id = WM8960_SYSCLK_AUTO;
779 priv->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
780 } else if (of_device_is_compatible(np, "fsl,imx-audio-ac97")) {
781 codec_dai_name[0] = "ac97-hifi";
782 priv->dai_fmt = SND_SOC_DAIFMT_AC97;
783 priv->card.dapm_routes = audio_map_ac97;
784 priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_ac97);
785 } else if (of_device_is_compatible(np, "fsl,imx-audio-mqs")) {
786 codec_dai_name[0] = "fsl-mqs-dai";
787 priv->dai_fmt = SND_SOC_DAIFMT_LEFT_J |
788 SND_SOC_DAIFMT_CBC_CFC |
789 SND_SOC_DAIFMT_NB_NF;
790 priv->dai_link[1].playback_only = 1;
791 priv->dai_link[2].playback_only = 1;
792 priv->card.dapm_routes = audio_map_tx;
793 priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_tx);
794 } else if (of_device_is_compatible(np, "fsl,imx-audio-wm8524")) {
795 codec_dai_name[0] = "wm8524-hifi";
796 priv->dai_fmt |= SND_SOC_DAIFMT_CBC_CFC;
797 priv->dai_link[1].playback_only = 1;
798 priv->dai_link[2].playback_only = 1;
799 priv->cpu_priv.slot_width = 32;
800 priv->card.dapm_routes = audio_map_tx;
801 priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_tx);
802 } else if (of_device_is_compatible(np, "fsl,imx-audio-si476x")) {
803 codec_dai_name[0] = "si476x-codec";
804 priv->dai_fmt |= SND_SOC_DAIFMT_CBC_CFC;
805 priv->card.dapm_routes = audio_map_rx;
806 priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_rx);
807 } else if (of_device_is_compatible(np, "fsl,imx-audio-wm8958")) {
808 codec_dai_name[0] = "wm8994-aif1";
809 priv->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
810 priv->codec_priv[0].mclk_id = WM8994_FLL_SRC_MCLK1;
811 priv->codec_priv[0].fll_id = WM8994_SYSCLK_FLL1;
812 priv->codec_priv[0].pll_id = WM8994_FLL1;
813 priv->codec_priv[0].free_freq = priv->codec_priv[0].mclk_freq;
814 priv->card.dapm_routes = NULL;
815 priv->card.num_dapm_routes = 0;
816 } else if (of_device_is_compatible(np, "fsl,imx-audio-nau8822")) {
817 codec_dai_name[0] = "nau8822-hifi";
818 priv->codec_priv[0].mclk_id = NAU8822_CLK_MCLK;
819 priv->codec_priv[0].fll_id = NAU8822_CLK_PLL;
820 priv->codec_priv[0].pll_id = NAU8822_CLK_PLL;
821 priv->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
822 if (codec_dev[0])
823 priv->codec_priv[0].mclk = devm_clk_get(codec_dev[0], NULL);
824 } else if (of_device_is_compatible(np, "fsl,imx-audio-wm8904")) {
825 codec_dai_name[0] = "wm8904-hifi";
826 priv->codec_priv[0].mclk_id = WM8904_FLL_MCLK;
827 priv->codec_priv[0].fll_id = WM8904_CLK_FLL;
828 priv->codec_priv[0].pll_id = WM8904_FLL_MCLK;
829 priv->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
830 } else if (of_device_is_compatible(np, "fsl,imx-audio-spdif")) {
831 ret = fsl_asoc_card_spdif_init(codec_np, cpu_np, codec_dai_name, priv);
832 if (ret)
833 goto asrc_fail;
834 } else {
835 dev_err(&pdev->dev, "unknown Device Tree compatible\n");
836 ret = -EINVAL;
837 goto asrc_fail;
841 * Allow setting mclk-id from the device-tree node. Otherwise, the
842 * default value for each card configuration is used.
844 for_each_link_codecs((&(priv->dai_link[0])), codec_idx, codec_comp) {
845 of_property_read_u32_index(np, "mclk-id", codec_idx,
846 &priv->codec_priv[codec_idx].mclk_id);
849 /* Format info from DT is optional. */
850 snd_soc_daifmt_parse_clock_provider_as_phandle(np, NULL, &bitclkprovider, &frameprovider);
851 if (bitclkprovider || frameprovider) {
852 unsigned int daifmt = snd_soc_daifmt_parse_format(np, NULL);
853 bool codec_bitclkprovider = false;
854 bool codec_frameprovider = false;
856 for_each_link_codecs((&(priv->dai_link[0])), codec_idx, codec_comp) {
857 if (bitclkprovider && codec_np[codec_idx] == bitclkprovider)
858 codec_bitclkprovider = true;
859 if (frameprovider && codec_np[codec_idx] == frameprovider)
860 codec_frameprovider = true;
863 if (codec_bitclkprovider)
864 daifmt |= (codec_frameprovider) ?
865 SND_SOC_DAIFMT_CBP_CFP : SND_SOC_DAIFMT_CBP_CFC;
866 else
867 daifmt |= (codec_frameprovider) ?
868 SND_SOC_DAIFMT_CBC_CFP : SND_SOC_DAIFMT_CBC_CFC;
870 /* Override dai_fmt with value from DT */
871 priv->dai_fmt = daifmt;
874 /* Change direction according to format */
875 if (priv->dai_fmt & SND_SOC_DAIFMT_CBP_CFP) {
876 priv->cpu_priv.sysclk_dir[TX] = SND_SOC_CLOCK_IN;
877 priv->cpu_priv.sysclk_dir[RX] = SND_SOC_CLOCK_IN;
880 of_node_put(bitclkprovider);
881 of_node_put(frameprovider);
883 if (!fsl_asoc_card_is_ac97(priv) && !codec_dev[0]
884 && codec_dai_name[0] != snd_soc_dummy_dlc.dai_name) {
885 dev_dbg(&pdev->dev, "failed to find codec device\n");
886 ret = -EPROBE_DEFER;
887 goto asrc_fail;
890 /* Common settings for corresponding Freescale CPU DAI driver */
891 if (of_node_name_eq(cpu_np, "ssi")) {
892 /* Only SSI needs to configure AUDMUX */
893 ret = fsl_asoc_card_audmux_init(np, priv);
894 if (ret) {
895 dev_err(&pdev->dev, "failed to init audmux\n");
896 goto asrc_fail;
898 } else if (of_node_name_eq(cpu_np, "esai")) {
899 struct clk *esai_clk = clk_get(&cpu_pdev->dev, "extal");
901 if (!IS_ERR(esai_clk)) {
902 priv->cpu_priv.sysclk_freq[TX] = clk_get_rate(esai_clk);
903 priv->cpu_priv.sysclk_freq[RX] = clk_get_rate(esai_clk);
904 clk_put(esai_clk);
905 } else if (PTR_ERR(esai_clk) == -EPROBE_DEFER) {
906 ret = -EPROBE_DEFER;
907 goto asrc_fail;
910 priv->cpu_priv.sysclk_id[1] = ESAI_HCKT_EXTAL;
911 priv->cpu_priv.sysclk_id[0] = ESAI_HCKR_EXTAL;
912 } else if (of_node_name_eq(cpu_np, "sai")) {
913 priv->cpu_priv.sysclk_id[1] = FSL_SAI_CLK_MAST1;
914 priv->cpu_priv.sysclk_id[0] = FSL_SAI_CLK_MAST1;
917 /* Initialize sound card */
918 priv->card.dev = &pdev->dev;
919 priv->card.owner = THIS_MODULE;
920 ret = snd_soc_of_parse_card_name(&priv->card, "model");
921 if (ret) {
922 snprintf(priv->name, sizeof(priv->name), "%s-audio",
923 fsl_asoc_card_is_ac97(priv) ? "ac97" : codec_dev_name[0]);
924 priv->card.name = priv->name;
926 priv->card.dai_link = priv->dai_link;
927 priv->card.late_probe = fsl_asoc_card_late_probe;
928 priv->card.dapm_widgets = fsl_asoc_card_dapm_widgets;
929 priv->card.num_dapm_widgets = ARRAY_SIZE(fsl_asoc_card_dapm_widgets);
931 /* Drop the second half of DAPM routes -- ASRC */
932 if (!asrc_pdev)
933 priv->card.num_dapm_routes /= 2;
935 if (of_property_read_bool(np, "audio-routing")) {
936 ret = snd_soc_of_parse_audio_routing(&priv->card, "audio-routing");
937 if (ret) {
938 dev_err(&pdev->dev, "failed to parse audio-routing: %d\n", ret);
939 goto asrc_fail;
943 /* Normal DAI Link */
944 priv->dai_link[0].cpus->of_node = cpu_np;
945 for_each_link_codecs((&(priv->dai_link[0])), codec_idx, codec_comp) {
946 codec_comp->dai_name = codec_dai_name[codec_idx];
949 // Old SPDIF DT binding support
950 if (codec_dai_name[0] == snd_soc_dummy_dlc.dai_name)
951 priv->dai_link[0].codecs[0].name = snd_soc_dummy_dlc.name;
953 if (!fsl_asoc_card_is_ac97(priv)) {
954 for_each_link_codecs((&(priv->dai_link[0])), codec_idx, codec_comp) {
955 codec_comp->of_node = codec_np[codec_idx];
957 } else {
958 u32 idx;
960 ret = of_property_read_u32(cpu_np, "cell-index", &idx);
961 if (ret) {
962 dev_err(&pdev->dev,
963 "cannot get CPU index property\n");
964 goto asrc_fail;
967 priv->dai_link[0].codecs[0].name =
968 devm_kasprintf(&pdev->dev, GFP_KERNEL,
969 "ac97-codec.%u",
970 (unsigned int)idx);
971 if (!priv->dai_link[0].codecs[0].name) {
972 ret = -ENOMEM;
973 goto asrc_fail;
977 priv->dai_link[0].platforms->of_node = cpu_np;
978 priv->dai_link[0].dai_fmt = priv->dai_fmt;
979 priv->card.num_links = 1;
981 if (asrc_pdev) {
982 /* DPCM DAI Links only if ASRC exists */
983 priv->dai_link[1].cpus->of_node = asrc_np;
984 priv->dai_link[1].platforms->of_node = asrc_np;
985 for_each_link_codecs((&(priv->dai_link[2])), codec_idx, codec_comp) {
986 codec_comp->dai_name = priv->dai_link[0].codecs[codec_idx].dai_name;
987 codec_comp->of_node = priv->dai_link[0].codecs[codec_idx].of_node;
988 codec_comp->name = priv->dai_link[0].codecs[codec_idx].name;
990 priv->dai_link[2].cpus->of_node = cpu_np;
991 priv->dai_link[2].dai_fmt = priv->dai_fmt;
992 priv->card.num_links = 3;
994 ret = of_property_read_u32(asrc_np, "fsl,asrc-rate",
995 &priv->asrc_rate);
996 if (ret) {
997 dev_err(&pdev->dev, "failed to get output rate\n");
998 ret = -EINVAL;
999 goto asrc_fail;
1002 ret = of_property_read_u32(asrc_np, "fsl,asrc-format", &asrc_fmt);
1003 priv->asrc_format = (__force snd_pcm_format_t)asrc_fmt;
1004 if (ret) {
1005 /* Fallback to old binding; translate to asrc_format */
1006 ret = of_property_read_u32(asrc_np, "fsl,asrc-width",
1007 &width);
1008 if (ret) {
1009 dev_err(&pdev->dev,
1010 "failed to decide output format\n");
1011 goto asrc_fail;
1014 if (width == 24)
1015 priv->asrc_format = SNDRV_PCM_FORMAT_S24_LE;
1016 else
1017 priv->asrc_format = SNDRV_PCM_FORMAT_S16_LE;
1021 /* Finish card registering */
1022 platform_set_drvdata(pdev, priv);
1023 snd_soc_card_set_drvdata(&priv->card, priv);
1025 ret = devm_snd_soc_register_card(&pdev->dev, &priv->card);
1026 if (ret) {
1027 dev_err_probe(&pdev->dev, ret, "snd_soc_register_card failed\n");
1028 goto asrc_fail;
1032 * Properties "hp-det-gpios" and "mic-det-gpios" are optional, and
1033 * simple_util_init_jack() uses these properties for creating
1034 * Headphone Jack and Microphone Jack.
1036 * The notifier is initialized in snd_soc_card_jack_new(), then
1037 * snd_soc_jack_notifier_register can be called.
1039 if (of_property_read_bool(np, "hp-det-gpios") ||
1040 of_property_read_bool(np, "hp-det-gpio") /* deprecated */) {
1041 ret = simple_util_init_jack(&priv->card, &priv->hp_jack,
1042 1, NULL, "Headphone Jack");
1043 if (ret)
1044 goto asrc_fail;
1046 snd_soc_jack_notifier_register(&priv->hp_jack.jack, &hp_jack_nb);
1049 if (of_property_read_bool(np, "mic-det-gpios") ||
1050 of_property_read_bool(np, "mic-det-gpio") /* deprecated */) {
1051 ret = simple_util_init_jack(&priv->card, &priv->mic_jack,
1052 0, NULL, "Mic Jack");
1053 if (ret)
1054 goto asrc_fail;
1056 snd_soc_jack_notifier_register(&priv->mic_jack.jack, &mic_jack_nb);
1059 asrc_fail:
1060 of_node_put(asrc_np);
1061 of_node_put(codec_np[0]);
1062 of_node_put(codec_np[1]);
1063 put_device(&cpu_pdev->dev);
1064 fail:
1065 of_node_put(cpu_np);
1067 return ret;
1070 static const struct of_device_id fsl_asoc_card_dt_ids[] = {
1071 { .compatible = "fsl,imx-audio-ac97", },
1072 { .compatible = "fsl,imx-audio-cs42888", },
1073 { .compatible = "fsl,imx-audio-cs427x", },
1074 { .compatible = "fsl,imx-audio-tlv320aic32x4", },
1075 { .compatible = "fsl,imx-audio-tlv320aic31xx", },
1076 { .compatible = "fsl,imx-audio-sgtl5000", },
1077 { .compatible = "fsl,imx-audio-wm8962", },
1078 { .compatible = "fsl,imx-audio-wm8960", },
1079 { .compatible = "fsl,imx-audio-mqs", },
1080 { .compatible = "fsl,imx-audio-wm8524", },
1081 { .compatible = "fsl,imx-audio-si476x", },
1082 { .compatible = "fsl,imx-audio-wm8958", },
1083 { .compatible = "fsl,imx-audio-nau8822", },
1084 { .compatible = "fsl,imx-audio-wm8904", },
1085 { .compatible = "fsl,imx-audio-spdif", },
1088 MODULE_DEVICE_TABLE(of, fsl_asoc_card_dt_ids);
1090 static struct platform_driver fsl_asoc_card_driver = {
1091 .probe = fsl_asoc_card_probe,
1092 .driver = {
1093 .name = DRIVER_NAME,
1094 .pm = &snd_soc_pm_ops,
1095 .of_match_table = fsl_asoc_card_dt_ids,
1098 module_platform_driver(fsl_asoc_card_driver);
1100 MODULE_DESCRIPTION("Freescale Generic ASoC Sound Card driver with ASRC");
1101 MODULE_AUTHOR("Nicolin Chen <nicoleotsuka@gmail.com>");
1102 MODULE_ALIAS("platform:" DRIVER_NAME);
1103 MODULE_LICENSE("GPL");