1 // SPDX-License-Identifier: GPL-2.0
3 // Freescale Generic ASoC Sound Card driver with ASRC
5 // Copyright (C) 2014 Freescale Semiconductor, Inc.
7 // Author: Nicolin Chen <nicoleotsuka@gmail.com>
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>
16 #include <sound/pcm_params.h>
17 #include <sound/soc.h>
18 #include <sound/jack.h>
19 #include <sound/simple_card_utils.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"
32 #define DRIVER_NAME "fsl-asoc-card"
34 #define CS427x_SYSCLK_MCLK 0
39 /* Default DAI format without Master and Slave flag */
40 #define DAI_FMT_BASE (SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF)
43 * struct codec_priv - CODEC private data
44 * @mclk_freq: Clock rate of MCLK
45 * @free_freq: Clock rate of MCLK for hw_free()
46 * @mclk_id: MCLK (or main clock) id for set_sysclk()
47 * @fll_id: FLL (or secordary clock) id for set_sysclk()
48 * @pll_id: PLL id for set_pll()
52 unsigned long mclk_freq;
53 unsigned long free_freq;
60 * struct cpu_priv - CPU private data
61 * @sysclk_freq: SYSCLK rates for set_sysclk()
62 * @sysclk_dir: SYSCLK directions for set_sysclk()
63 * @sysclk_id: SYSCLK ids for set_sysclk()
64 * @slot_width: Slot width of each frame
65 * @slot_num: Number of slots of each frame
67 * Note: [1] for tx and [0] for rx
70 unsigned long sysclk_freq[2];
78 * struct fsl_asoc_card_priv - Freescale Generic ASOC card private data
79 * @dai_link: DAI link structure including normal one and DPCM link
80 * @hp_jack: Headphone Jack structure
81 * @mic_jack: Microphone Jack structure
82 * @pdev: platform device pointer
83 * @codec_priv: CODEC private data
84 * @cpu_priv: CPU private data
85 * @card: ASoC card structure
86 * @streams: Mask of current active streams
87 * @sample_rate: Current sample rate
88 * @sample_format: Current sample format
89 * @asrc_rate: ASRC sample rate used by Back-Ends
90 * @asrc_format: ASRC sample format used by Back-Ends
91 * @dai_fmt: DAI format between CPU and CODEC
95 struct fsl_asoc_card_priv {
96 struct snd_soc_dai_link dai_link[3];
97 struct asoc_simple_jack hp_jack;
98 struct asoc_simple_jack mic_jack;
99 struct platform_device *pdev;
100 struct codec_priv codec_priv;
101 struct cpu_priv cpu_priv;
102 struct snd_soc_card card;
105 snd_pcm_format_t sample_format;
107 snd_pcm_format_t asrc_format;
113 * This dapm route map exists for DPCM link only.
114 * The other routes shall go through Device Tree.
116 * Note: keep all ASRC routes in the second half
117 * to drop them easily for non-ASRC cases.
119 static const struct snd_soc_dapm_route audio_map[] = {
120 /* 1st half -- Normal DAPM routes */
121 {"Playback", NULL, "CPU-Playback"},
122 {"CPU-Capture", NULL, "Capture"},
123 /* 2nd half -- ASRC DAPM routes */
124 {"CPU-Playback", NULL, "ASRC-Playback"},
125 {"ASRC-Capture", NULL, "CPU-Capture"},
128 static const struct snd_soc_dapm_route audio_map_ac97[] = {
129 /* 1st half -- Normal DAPM routes */
130 {"AC97 Playback", NULL, "CPU AC97 Playback"},
131 {"CPU AC97 Capture", NULL, "AC97 Capture"},
132 /* 2nd half -- ASRC DAPM routes */
133 {"CPU AC97 Playback", NULL, "ASRC-Playback"},
134 {"ASRC-Capture", NULL, "CPU AC97 Capture"},
137 static const struct snd_soc_dapm_route audio_map_tx[] = {
138 /* 1st half -- Normal DAPM routes */
139 {"Playback", NULL, "CPU-Playback"},
140 /* 2nd half -- ASRC DAPM routes */
141 {"CPU-Playback", NULL, "ASRC-Playback"},
144 static const struct snd_soc_dapm_route audio_map_rx[] = {
145 /* 1st half -- Normal DAPM routes */
146 {"CPU-Capture", NULL, "Capture"},
147 /* 2nd half -- ASRC DAPM routes */
148 {"ASRC-Capture", NULL, "CPU-Capture"},
151 /* Add all possible widgets into here without being redundant */
152 static const struct snd_soc_dapm_widget fsl_asoc_card_dapm_widgets[] = {
153 SND_SOC_DAPM_LINE("Line Out Jack", NULL),
154 SND_SOC_DAPM_LINE("Line In Jack", NULL),
155 SND_SOC_DAPM_HP("Headphone Jack", NULL),
156 SND_SOC_DAPM_SPK("Ext Spk", NULL),
157 SND_SOC_DAPM_MIC("Mic Jack", NULL),
158 SND_SOC_DAPM_MIC("AMIC", NULL),
159 SND_SOC_DAPM_MIC("DMIC", NULL),
162 static bool fsl_asoc_card_is_ac97(struct fsl_asoc_card_priv *priv)
164 return priv->dai_fmt == SND_SOC_DAIFMT_AC97;
167 static int fsl_asoc_card_hw_params(struct snd_pcm_substream *substream,
168 struct snd_pcm_hw_params *params)
170 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
171 struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
172 bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
173 struct codec_priv *codec_priv = &priv->codec_priv;
174 struct cpu_priv *cpu_priv = &priv->cpu_priv;
175 struct device *dev = rtd->card->dev;
176 unsigned int pll_out;
179 priv->sample_rate = params_rate(params);
180 priv->sample_format = params_format(params);
181 priv->streams |= BIT(substream->stream);
183 if (fsl_asoc_card_is_ac97(priv))
186 /* Specific configurations of DAIs starts from here */
187 ret = snd_soc_dai_set_sysclk(asoc_rtd_to_cpu(rtd, 0), cpu_priv->sysclk_id[tx],
188 cpu_priv->sysclk_freq[tx],
189 cpu_priv->sysclk_dir[tx]);
190 if (ret && ret != -ENOTSUPP) {
191 dev_err(dev, "failed to set sysclk for cpu dai\n");
195 if (cpu_priv->slot_width) {
196 if (!cpu_priv->slot_num)
197 cpu_priv->slot_num = 2;
199 ret = snd_soc_dai_set_tdm_slot(asoc_rtd_to_cpu(rtd, 0), 0x3, 0x3,
201 cpu_priv->slot_width);
202 if (ret && ret != -ENOTSUPP) {
203 dev_err(dev, "failed to set TDM slot for cpu dai\n");
208 /* Specific configuration for PLL */
209 if (codec_priv->pll_id && codec_priv->fll_id) {
210 if (priv->sample_format == SNDRV_PCM_FORMAT_S24_LE)
211 pll_out = priv->sample_rate * 384;
213 pll_out = priv->sample_rate * 256;
215 ret = snd_soc_dai_set_pll(asoc_rtd_to_codec(rtd, 0),
218 codec_priv->mclk_freq, pll_out);
220 dev_err(dev, "failed to start FLL: %d\n", ret);
224 ret = snd_soc_dai_set_sysclk(asoc_rtd_to_codec(rtd, 0),
226 pll_out, SND_SOC_CLOCK_IN);
228 if (ret && ret != -ENOTSUPP) {
229 dev_err(dev, "failed to set SYSCLK: %d\n", ret);
237 priv->streams &= ~BIT(substream->stream);
241 static int fsl_asoc_card_hw_free(struct snd_pcm_substream *substream)
243 struct snd_soc_pcm_runtime *rtd = substream->private_data;
244 struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
245 struct codec_priv *codec_priv = &priv->codec_priv;
246 struct device *dev = rtd->card->dev;
249 priv->streams &= ~BIT(substream->stream);
251 if (!priv->streams && codec_priv->pll_id && codec_priv->fll_id) {
252 /* Force freq to be free_freq to avoid error message in codec */
253 ret = snd_soc_dai_set_sysclk(asoc_rtd_to_codec(rtd, 0),
255 codec_priv->free_freq,
258 dev_err(dev, "failed to switch away from FLL: %d\n", ret);
262 ret = snd_soc_dai_set_pll(asoc_rtd_to_codec(rtd, 0),
263 codec_priv->pll_id, 0, 0, 0);
264 if (ret && ret != -ENOTSUPP) {
265 dev_err(dev, "failed to stop FLL: %d\n", ret);
273 static const struct snd_soc_ops fsl_asoc_card_ops = {
274 .hw_params = fsl_asoc_card_hw_params,
275 .hw_free = fsl_asoc_card_hw_free,
278 static int be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
279 struct snd_pcm_hw_params *params)
281 struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
282 struct snd_interval *rate;
283 struct snd_mask *mask;
285 rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
286 rate->max = rate->min = priv->asrc_rate;
288 mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
290 snd_mask_set_format(mask, priv->asrc_format);
295 SND_SOC_DAILINK_DEFS(hifi,
296 DAILINK_COMP_ARRAY(COMP_EMPTY()),
297 DAILINK_COMP_ARRAY(COMP_EMPTY()),
298 DAILINK_COMP_ARRAY(COMP_EMPTY()));
300 SND_SOC_DAILINK_DEFS(hifi_fe,
301 DAILINK_COMP_ARRAY(COMP_EMPTY()),
302 DAILINK_COMP_ARRAY(COMP_DUMMY()),
303 DAILINK_COMP_ARRAY(COMP_EMPTY()));
305 SND_SOC_DAILINK_DEFS(hifi_be,
306 DAILINK_COMP_ARRAY(COMP_EMPTY()),
307 DAILINK_COMP_ARRAY(COMP_EMPTY()),
308 DAILINK_COMP_ARRAY(COMP_DUMMY()));
310 static const struct snd_soc_dai_link fsl_asoc_card_dai[] = {
311 /* Default ASoC DAI Link*/
314 .stream_name = "HiFi",
315 .ops = &fsl_asoc_card_ops,
316 SND_SOC_DAILINK_REG(hifi),
318 /* DPCM Link between Front-End and Back-End (Optional) */
320 .name = "HiFi-ASRC-FE",
321 .stream_name = "HiFi-ASRC-FE",
325 SND_SOC_DAILINK_REG(hifi_fe),
328 .name = "HiFi-ASRC-BE",
329 .stream_name = "HiFi-ASRC-BE",
330 .be_hw_params_fixup = be_hw_params_fixup,
331 .ops = &fsl_asoc_card_ops,
335 SND_SOC_DAILINK_REG(hifi_be),
339 static int fsl_asoc_card_audmux_init(struct device_node *np,
340 struct fsl_asoc_card_priv *priv)
342 struct device *dev = &priv->pdev->dev;
343 u32 int_ptcr = 0, ext_ptcr = 0;
344 int int_port, ext_port;
347 ret = of_property_read_u32(np, "mux-int-port", &int_port);
349 dev_err(dev, "mux-int-port missing or invalid\n");
352 ret = of_property_read_u32(np, "mux-ext-port", &ext_port);
354 dev_err(dev, "mux-ext-port missing or invalid\n");
359 * The port numbering in the hardware manual starts at 1, while
360 * the AUDMUX API expects it starts at 0.
366 * Use asynchronous mode (6 wires) for all cases except AC97.
367 * If only 4 wires are needed, just set SSI into
368 * synchronous mode and enable 4 PADs in IOMUX.
370 switch (priv->dai_fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
371 case SND_SOC_DAIFMT_CBP_CFP:
372 int_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | ext_port) |
373 IMX_AUDMUX_V2_PTCR_RCSEL(8 | ext_port) |
374 IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) |
375 IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
376 IMX_AUDMUX_V2_PTCR_RFSDIR |
377 IMX_AUDMUX_V2_PTCR_RCLKDIR |
378 IMX_AUDMUX_V2_PTCR_TFSDIR |
379 IMX_AUDMUX_V2_PTCR_TCLKDIR;
381 case SND_SOC_DAIFMT_CBP_CFC:
382 int_ptcr = IMX_AUDMUX_V2_PTCR_RCSEL(8 | ext_port) |
383 IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
384 IMX_AUDMUX_V2_PTCR_RCLKDIR |
385 IMX_AUDMUX_V2_PTCR_TCLKDIR;
386 ext_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | int_port) |
387 IMX_AUDMUX_V2_PTCR_TFSEL(int_port) |
388 IMX_AUDMUX_V2_PTCR_RFSDIR |
389 IMX_AUDMUX_V2_PTCR_TFSDIR;
391 case SND_SOC_DAIFMT_CBC_CFP:
392 int_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | ext_port) |
393 IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) |
394 IMX_AUDMUX_V2_PTCR_RFSDIR |
395 IMX_AUDMUX_V2_PTCR_TFSDIR;
396 ext_ptcr = IMX_AUDMUX_V2_PTCR_RCSEL(8 | int_port) |
397 IMX_AUDMUX_V2_PTCR_TCSEL(int_port) |
398 IMX_AUDMUX_V2_PTCR_RCLKDIR |
399 IMX_AUDMUX_V2_PTCR_TCLKDIR;
401 case SND_SOC_DAIFMT_CBC_CFC:
402 ext_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | int_port) |
403 IMX_AUDMUX_V2_PTCR_RCSEL(8 | int_port) |
404 IMX_AUDMUX_V2_PTCR_TFSEL(int_port) |
405 IMX_AUDMUX_V2_PTCR_TCSEL(int_port) |
406 IMX_AUDMUX_V2_PTCR_RFSDIR |
407 IMX_AUDMUX_V2_PTCR_RCLKDIR |
408 IMX_AUDMUX_V2_PTCR_TFSDIR |
409 IMX_AUDMUX_V2_PTCR_TCLKDIR;
412 if (!fsl_asoc_card_is_ac97(priv))
416 if (fsl_asoc_card_is_ac97(priv)) {
417 int_ptcr = IMX_AUDMUX_V2_PTCR_SYN |
418 IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
419 IMX_AUDMUX_V2_PTCR_TCLKDIR;
420 ext_ptcr = IMX_AUDMUX_V2_PTCR_SYN |
421 IMX_AUDMUX_V2_PTCR_TFSEL(int_port) |
422 IMX_AUDMUX_V2_PTCR_TFSDIR;
425 /* Asynchronous mode can not be set along with RCLKDIR */
426 if (!fsl_asoc_card_is_ac97(priv)) {
428 IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port);
430 ret = imx_audmux_v2_configure_port(int_port, 0,
433 dev_err(dev, "audmux internal port setup failed\n");
438 ret = imx_audmux_v2_configure_port(int_port, int_ptcr,
439 IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port));
441 dev_err(dev, "audmux internal port setup failed\n");
445 if (!fsl_asoc_card_is_ac97(priv)) {
447 IMX_AUDMUX_V2_PDCR_RXDSEL(int_port);
449 ret = imx_audmux_v2_configure_port(ext_port, 0,
452 dev_err(dev, "audmux external port setup failed\n");
457 ret = imx_audmux_v2_configure_port(ext_port, ext_ptcr,
458 IMX_AUDMUX_V2_PDCR_RXDSEL(int_port));
460 dev_err(dev, "audmux external port setup failed\n");
467 static int hp_jack_event(struct notifier_block *nb, unsigned long event,
470 struct snd_soc_jack *jack = (struct snd_soc_jack *)data;
471 struct snd_soc_dapm_context *dapm = &jack->card->dapm;
473 if (event & SND_JACK_HEADPHONE)
474 /* Disable speaker if headphone is plugged in */
475 return snd_soc_dapm_disable_pin(dapm, "Ext Spk");
477 return snd_soc_dapm_enable_pin(dapm, "Ext Spk");
480 static struct notifier_block hp_jack_nb = {
481 .notifier_call = hp_jack_event,
484 static int mic_jack_event(struct notifier_block *nb, unsigned long event,
487 struct snd_soc_jack *jack = (struct snd_soc_jack *)data;
488 struct snd_soc_dapm_context *dapm = &jack->card->dapm;
490 if (event & SND_JACK_MICROPHONE)
491 /* Disable dmic if microphone is plugged in */
492 return snd_soc_dapm_disable_pin(dapm, "DMIC");
494 return snd_soc_dapm_enable_pin(dapm, "DMIC");
497 static struct notifier_block mic_jack_nb = {
498 .notifier_call = mic_jack_event,
501 static int fsl_asoc_card_late_probe(struct snd_soc_card *card)
503 struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(card);
504 struct snd_soc_pcm_runtime *rtd = list_first_entry(
505 &card->rtd_list, struct snd_soc_pcm_runtime, list);
506 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
507 struct codec_priv *codec_priv = &priv->codec_priv;
508 struct device *dev = card->dev;
511 if (fsl_asoc_card_is_ac97(priv)) {
512 #if IS_ENABLED(CONFIG_SND_AC97_CODEC)
513 struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
514 struct snd_ac97 *ac97 = snd_soc_component_get_drvdata(component);
517 * Use slots 3/4 for S/PDIF so SSI won't try to enable
518 * other slots and send some samples there
519 * due to SLOTREQ bits for S/PDIF received from codec
521 snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS,
522 AC97_EA_SPSA_SLOT_MASK, AC97_EA_SPSA_3_4);
528 ret = snd_soc_dai_set_sysclk(codec_dai, codec_priv->mclk_id,
529 codec_priv->mclk_freq, SND_SOC_CLOCK_IN);
530 if (ret && ret != -ENOTSUPP) {
531 dev_err(dev, "failed to set sysclk in %s\n", __func__);
535 if (!IS_ERR_OR_NULL(codec_priv->mclk))
536 clk_prepare_enable(codec_priv->mclk);
541 static int fsl_asoc_card_probe(struct platform_device *pdev)
543 struct device_node *cpu_np, *codec_np, *asrc_np;
544 struct device_node *np = pdev->dev.of_node;
545 struct platform_device *asrc_pdev = NULL;
546 struct device_node *bitclkprovider = NULL;
547 struct device_node *frameprovider = NULL;
548 struct platform_device *cpu_pdev;
549 struct fsl_asoc_card_priv *priv;
550 struct device *codec_dev = NULL;
551 const char *codec_dai_name;
552 const char *codec_dev_name;
557 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
561 cpu_np = of_parse_phandle(np, "audio-cpu", 0);
562 /* Give a chance to old DT binding */
564 cpu_np = of_parse_phandle(np, "ssi-controller", 0);
566 dev_err(&pdev->dev, "CPU phandle missing or invalid\n");
571 cpu_pdev = of_find_device_by_node(cpu_np);
573 dev_err(&pdev->dev, "failed to find CPU DAI device\n");
578 codec_np = of_parse_phandle(np, "audio-codec", 0);
580 struct platform_device *codec_pdev;
581 struct i2c_client *codec_i2c;
583 codec_i2c = of_find_i2c_device_by_node(codec_np);
585 codec_dev = &codec_i2c->dev;
586 codec_dev_name = codec_i2c->name;
589 codec_pdev = of_find_device_by_node(codec_np);
591 codec_dev = &codec_pdev->dev;
592 codec_dev_name = codec_pdev->name;
597 asrc_np = of_parse_phandle(np, "audio-asrc", 0);
599 asrc_pdev = of_find_device_by_node(asrc_np);
601 /* Get the MCLK rate only, and leave it controlled by CODEC drivers */
603 struct clk *codec_clk = clk_get(codec_dev, NULL);
605 if (!IS_ERR(codec_clk)) {
606 priv->codec_priv.mclk_freq = clk_get_rate(codec_clk);
611 /* Default sample rate and format, will be updated in hw_params() */
612 priv->sample_rate = 44100;
613 priv->sample_format = SNDRV_PCM_FORMAT_S16_LE;
615 /* Assign a default DAI format, and allow each card to overwrite it */
616 priv->dai_fmt = DAI_FMT_BASE;
618 memcpy(priv->dai_link, fsl_asoc_card_dai,
619 sizeof(struct snd_soc_dai_link) * ARRAY_SIZE(priv->dai_link));
621 priv->card.dapm_routes = audio_map;
622 priv->card.num_dapm_routes = ARRAY_SIZE(audio_map);
623 priv->card.driver_name = DRIVER_NAME;
624 /* Diversify the card configurations */
625 if (of_device_is_compatible(np, "fsl,imx-audio-cs42888")) {
626 codec_dai_name = "cs42888";
627 priv->cpu_priv.sysclk_freq[TX] = priv->codec_priv.mclk_freq;
628 priv->cpu_priv.sysclk_freq[RX] = priv->codec_priv.mclk_freq;
629 priv->cpu_priv.sysclk_dir[TX] = SND_SOC_CLOCK_OUT;
630 priv->cpu_priv.sysclk_dir[RX] = SND_SOC_CLOCK_OUT;
631 priv->cpu_priv.slot_width = 32;
632 priv->dai_fmt |= SND_SOC_DAIFMT_CBC_CFC;
633 } else if (of_device_is_compatible(np, "fsl,imx-audio-cs427x")) {
634 codec_dai_name = "cs4271-hifi";
635 priv->codec_priv.mclk_id = CS427x_SYSCLK_MCLK;
636 priv->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
637 } else if (of_device_is_compatible(np, "fsl,imx-audio-sgtl5000")) {
638 codec_dai_name = "sgtl5000";
639 priv->codec_priv.mclk_id = SGTL5000_SYSCLK;
640 priv->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
641 } else if (of_device_is_compatible(np, "fsl,imx-audio-tlv320aic32x4")) {
642 codec_dai_name = "tlv320aic32x4-hifi";
643 priv->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
644 } else if (of_device_is_compatible(np, "fsl,imx-audio-tlv320aic31xx")) {
645 codec_dai_name = "tlv320dac31xx-hifi";
646 priv->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
647 priv->dai_link[1].dpcm_capture = 0;
648 priv->dai_link[2].dpcm_capture = 0;
649 priv->cpu_priv.sysclk_dir[TX] = SND_SOC_CLOCK_OUT;
650 priv->cpu_priv.sysclk_dir[RX] = SND_SOC_CLOCK_OUT;
651 priv->card.dapm_routes = audio_map_tx;
652 priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_tx);
653 } else if (of_device_is_compatible(np, "fsl,imx-audio-wm8962")) {
654 codec_dai_name = "wm8962";
655 priv->codec_priv.mclk_id = WM8962_SYSCLK_MCLK;
656 priv->codec_priv.fll_id = WM8962_SYSCLK_FLL;
657 priv->codec_priv.pll_id = WM8962_FLL;
658 priv->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
659 } else if (of_device_is_compatible(np, "fsl,imx-audio-wm8960")) {
660 codec_dai_name = "wm8960-hifi";
661 priv->codec_priv.fll_id = WM8960_SYSCLK_AUTO;
662 priv->codec_priv.pll_id = WM8960_SYSCLK_AUTO;
663 priv->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
664 } else if (of_device_is_compatible(np, "fsl,imx-audio-ac97")) {
665 codec_dai_name = "ac97-hifi";
666 priv->dai_fmt = SND_SOC_DAIFMT_AC97;
667 priv->card.dapm_routes = audio_map_ac97;
668 priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_ac97);
669 } else if (of_device_is_compatible(np, "fsl,imx-audio-mqs")) {
670 codec_dai_name = "fsl-mqs-dai";
671 priv->dai_fmt = SND_SOC_DAIFMT_LEFT_J |
672 SND_SOC_DAIFMT_CBC_CFC |
673 SND_SOC_DAIFMT_NB_NF;
674 priv->dai_link[1].dpcm_capture = 0;
675 priv->dai_link[2].dpcm_capture = 0;
676 priv->card.dapm_routes = audio_map_tx;
677 priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_tx);
678 } else if (of_device_is_compatible(np, "fsl,imx-audio-wm8524")) {
679 codec_dai_name = "wm8524-hifi";
680 priv->dai_fmt |= SND_SOC_DAIFMT_CBC_CFC;
681 priv->dai_link[1].dpcm_capture = 0;
682 priv->dai_link[2].dpcm_capture = 0;
683 priv->cpu_priv.slot_width = 32;
684 priv->card.dapm_routes = audio_map_tx;
685 priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_tx);
686 } else if (of_device_is_compatible(np, "fsl,imx-audio-si476x")) {
687 codec_dai_name = "si476x-codec";
688 priv->dai_fmt |= SND_SOC_DAIFMT_CBC_CFC;
689 priv->card.dapm_routes = audio_map_rx;
690 priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_rx);
691 } else if (of_device_is_compatible(np, "fsl,imx-audio-wm8958")) {
692 codec_dai_name = "wm8994-aif1";
693 priv->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
694 priv->codec_priv.mclk_id = WM8994_FLL_SRC_MCLK1;
695 priv->codec_priv.fll_id = WM8994_SYSCLK_FLL1;
696 priv->codec_priv.pll_id = WM8994_FLL1;
697 priv->codec_priv.free_freq = priv->codec_priv.mclk_freq;
698 priv->card.dapm_routes = NULL;
699 priv->card.num_dapm_routes = 0;
700 } else if (of_device_is_compatible(np, "fsl,imx-audio-nau8822")) {
701 codec_dai_name = "nau8822-hifi";
702 priv->codec_priv.mclk_id = NAU8822_CLK_MCLK;
703 priv->codec_priv.fll_id = NAU8822_CLK_PLL;
704 priv->codec_priv.pll_id = NAU8822_CLK_PLL;
705 priv->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
707 priv->codec_priv.mclk = devm_clk_get(codec_dev, NULL);
709 dev_err(&pdev->dev, "unknown Device Tree compatible\n");
715 * Allow setting mclk-id from the device-tree node. Otherwise, the
716 * default value for each card configuration is used.
718 of_property_read_u32(np, "mclk-id", &priv->codec_priv.mclk_id);
720 /* Format info from DT is optional. */
721 snd_soc_daifmt_parse_clock_provider_as_phandle(np, NULL, &bitclkprovider, &frameprovider);
722 if (bitclkprovider || frameprovider) {
723 unsigned int daifmt = snd_soc_daifmt_parse_format(np, NULL);
725 if (codec_np == bitclkprovider)
726 daifmt |= (codec_np == frameprovider) ?
727 SND_SOC_DAIFMT_CBP_CFP : SND_SOC_DAIFMT_CBP_CFC;
729 daifmt |= (codec_np == frameprovider) ?
730 SND_SOC_DAIFMT_CBC_CFP : SND_SOC_DAIFMT_CBC_CFC;
732 /* Override dai_fmt with value from DT */
733 priv->dai_fmt = daifmt;
736 /* Change direction according to format */
737 if (priv->dai_fmt & SND_SOC_DAIFMT_CBP_CFP) {
738 priv->cpu_priv.sysclk_dir[TX] = SND_SOC_CLOCK_IN;
739 priv->cpu_priv.sysclk_dir[RX] = SND_SOC_CLOCK_IN;
742 of_node_put(bitclkprovider);
743 of_node_put(frameprovider);
745 if (!fsl_asoc_card_is_ac97(priv) && !codec_dev) {
746 dev_dbg(&pdev->dev, "failed to find codec device\n");
751 /* Common settings for corresponding Freescale CPU DAI driver */
752 if (of_node_name_eq(cpu_np, "ssi")) {
753 /* Only SSI needs to configure AUDMUX */
754 ret = fsl_asoc_card_audmux_init(np, priv);
756 dev_err(&pdev->dev, "failed to init audmux\n");
759 } else if (of_node_name_eq(cpu_np, "esai")) {
760 struct clk *esai_clk = clk_get(&cpu_pdev->dev, "extal");
762 if (!IS_ERR(esai_clk)) {
763 priv->cpu_priv.sysclk_freq[TX] = clk_get_rate(esai_clk);
764 priv->cpu_priv.sysclk_freq[RX] = clk_get_rate(esai_clk);
766 } else if (PTR_ERR(esai_clk) == -EPROBE_DEFER) {
771 priv->cpu_priv.sysclk_id[1] = ESAI_HCKT_EXTAL;
772 priv->cpu_priv.sysclk_id[0] = ESAI_HCKR_EXTAL;
773 } else if (of_node_name_eq(cpu_np, "sai")) {
774 priv->cpu_priv.sysclk_id[1] = FSL_SAI_CLK_MAST1;
775 priv->cpu_priv.sysclk_id[0] = FSL_SAI_CLK_MAST1;
778 /* Initialize sound card */
780 priv->card.dev = &pdev->dev;
781 priv->card.owner = THIS_MODULE;
782 ret = snd_soc_of_parse_card_name(&priv->card, "model");
784 snprintf(priv->name, sizeof(priv->name), "%s-audio",
785 fsl_asoc_card_is_ac97(priv) ? "ac97" : codec_dev_name);
786 priv->card.name = priv->name;
788 priv->card.dai_link = priv->dai_link;
789 priv->card.late_probe = fsl_asoc_card_late_probe;
790 priv->card.dapm_widgets = fsl_asoc_card_dapm_widgets;
791 priv->card.num_dapm_widgets = ARRAY_SIZE(fsl_asoc_card_dapm_widgets);
793 /* Drop the second half of DAPM routes -- ASRC */
795 priv->card.num_dapm_routes /= 2;
797 if (of_property_read_bool(np, "audio-routing")) {
798 ret = snd_soc_of_parse_audio_routing(&priv->card, "audio-routing");
800 dev_err(&pdev->dev, "failed to parse audio-routing: %d\n", ret);
805 /* Normal DAI Link */
806 priv->dai_link[0].cpus->of_node = cpu_np;
807 priv->dai_link[0].codecs->dai_name = codec_dai_name;
809 if (!fsl_asoc_card_is_ac97(priv))
810 priv->dai_link[0].codecs->of_node = codec_np;
814 ret = of_property_read_u32(cpu_np, "cell-index", &idx);
817 "cannot get CPU index property\n");
821 priv->dai_link[0].codecs->name =
822 devm_kasprintf(&pdev->dev, GFP_KERNEL,
825 if (!priv->dai_link[0].codecs->name) {
831 priv->dai_link[0].platforms->of_node = cpu_np;
832 priv->dai_link[0].dai_fmt = priv->dai_fmt;
833 priv->card.num_links = 1;
836 /* DPCM DAI Links only if ASRC exists */
837 priv->dai_link[1].cpus->of_node = asrc_np;
838 priv->dai_link[1].platforms->of_node = asrc_np;
839 priv->dai_link[2].codecs->dai_name = codec_dai_name;
840 priv->dai_link[2].codecs->of_node = codec_np;
841 priv->dai_link[2].codecs->name =
842 priv->dai_link[0].codecs->name;
843 priv->dai_link[2].cpus->of_node = cpu_np;
844 priv->dai_link[2].dai_fmt = priv->dai_fmt;
845 priv->card.num_links = 3;
847 ret = of_property_read_u32(asrc_np, "fsl,asrc-rate",
850 dev_err(&pdev->dev, "failed to get output rate\n");
855 ret = of_property_read_u32(asrc_np, "fsl,asrc-format", &asrc_fmt);
856 priv->asrc_format = (__force snd_pcm_format_t)asrc_fmt;
858 /* Fallback to old binding; translate to asrc_format */
859 ret = of_property_read_u32(asrc_np, "fsl,asrc-width",
863 "failed to decide output format\n");
868 priv->asrc_format = SNDRV_PCM_FORMAT_S24_LE;
870 priv->asrc_format = SNDRV_PCM_FORMAT_S16_LE;
874 /* Finish card registering */
875 platform_set_drvdata(pdev, priv);
876 snd_soc_card_set_drvdata(&priv->card, priv);
878 ret = devm_snd_soc_register_card(&pdev->dev, &priv->card);
880 dev_err_probe(&pdev->dev, ret, "snd_soc_register_card failed\n");
885 * Properties "hp-det-gpio" and "mic-det-gpio" are optional, and
886 * asoc_simple_init_jack uses these properties for creating
887 * Headphone Jack and Microphone Jack.
889 * The notifier is initialized in snd_soc_card_jack_new(), then
890 * snd_soc_jack_notifier_register can be called.
892 if (of_property_read_bool(np, "hp-det-gpio")) {
893 ret = asoc_simple_init_jack(&priv->card, &priv->hp_jack,
894 1, NULL, "Headphone Jack");
898 snd_soc_jack_notifier_register(&priv->hp_jack.jack, &hp_jack_nb);
901 if (of_property_read_bool(np, "mic-det-gpio")) {
902 ret = asoc_simple_init_jack(&priv->card, &priv->mic_jack,
903 0, NULL, "Mic Jack");
907 snd_soc_jack_notifier_register(&priv->mic_jack.jack, &mic_jack_nb);
911 of_node_put(asrc_np);
912 of_node_put(codec_np);
913 put_device(&cpu_pdev->dev);
920 static const struct of_device_id fsl_asoc_card_dt_ids[] = {
921 { .compatible = "fsl,imx-audio-ac97", },
922 { .compatible = "fsl,imx-audio-cs42888", },
923 { .compatible = "fsl,imx-audio-cs427x", },
924 { .compatible = "fsl,imx-audio-tlv320aic32x4", },
925 { .compatible = "fsl,imx-audio-tlv320aic31xx", },
926 { .compatible = "fsl,imx-audio-sgtl5000", },
927 { .compatible = "fsl,imx-audio-wm8962", },
928 { .compatible = "fsl,imx-audio-wm8960", },
929 { .compatible = "fsl,imx-audio-mqs", },
930 { .compatible = "fsl,imx-audio-wm8524", },
931 { .compatible = "fsl,imx-audio-si476x", },
932 { .compatible = "fsl,imx-audio-wm8958", },
933 { .compatible = "fsl,imx-audio-nau8822", },
936 MODULE_DEVICE_TABLE(of, fsl_asoc_card_dt_ids);
938 static struct platform_driver fsl_asoc_card_driver = {
939 .probe = fsl_asoc_card_probe,
942 .pm = &snd_soc_pm_ops,
943 .of_match_table = fsl_asoc_card_dt_ids,
946 module_platform_driver(fsl_asoc_card_driver);
948 MODULE_DESCRIPTION("Freescale Generic ASoC Sound Card driver with ASRC");
949 MODULE_AUTHOR("Nicolin Chen <nicoleotsuka@gmail.com>");
950 MODULE_ALIAS("platform:" DRIVER_NAME);
951 MODULE_LICENSE("GPL");