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"
30 #define CS427x_SYSCLK_MCLK 0
35 /* Default DAI format without Master and Slave flag */
36 #define DAI_FMT_BASE (SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF)
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()
47 unsigned long mclk_freq;
48 unsigned long free_freq;
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
61 * Note: [1] for tx and [0] for rx
64 unsigned long sysclk_freq[2];
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
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;
98 snd_pcm_format_t sample_format;
100 snd_pcm_format_t asrc_format;
106 * This dapm route map exists for DPCM link only.
107 * The other routes shall go through Device Tree.
109 * Note: keep all ASRC routes in the second half
110 * to drop them easily for non-ASRC cases.
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"},
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"},
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"},
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"},
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),
155 static bool fsl_asoc_card_is_ac97(struct fsl_asoc_card_priv *priv)
157 return priv->dai_fmt == SND_SOC_DAIFMT_AC97;
160 static int fsl_asoc_card_hw_params(struct snd_pcm_substream *substream,
161 struct snd_pcm_hw_params *params)
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;
172 priv->sample_rate = params_rate(params);
173 priv->sample_format = params_format(params);
174 priv->streams |= BIT(substream->stream);
176 if (fsl_asoc_card_is_ac97(priv))
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");
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");
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;
202 pll_out = priv->sample_rate * 256;
204 ret = snd_soc_dai_set_pll(asoc_rtd_to_codec(rtd, 0),
207 codec_priv->mclk_freq, pll_out);
209 dev_err(dev, "failed to start FLL: %d\n", ret);
213 ret = snd_soc_dai_set_sysclk(asoc_rtd_to_codec(rtd, 0),
215 pll_out, SND_SOC_CLOCK_IN);
217 if (ret && ret != -ENOTSUPP) {
218 dev_err(dev, "failed to set SYSCLK: %d\n", ret);
226 priv->streams &= ~BIT(substream->stream);
230 static int fsl_asoc_card_hw_free(struct snd_pcm_substream *substream)
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;
238 priv->streams &= ~BIT(substream->stream);
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),
244 codec_priv->free_freq,
247 dev_err(dev, "failed to switch away from FLL: %d\n", ret);
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);
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,
267 static int be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
268 struct snd_pcm_hw_params *params)
270 struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
271 struct snd_interval *rate;
272 struct snd_mask *mask;
274 rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
275 rate->max = rate->min = priv->asrc_rate;
277 mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
279 snd_mask_set_format(mask, priv->asrc_format);
284 SND_SOC_DAILINK_DEFS(hifi,
285 DAILINK_COMP_ARRAY(COMP_EMPTY()),
286 DAILINK_COMP_ARRAY(COMP_EMPTY()),
287 DAILINK_COMP_ARRAY(COMP_EMPTY()));
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()));
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()));
299 static struct snd_soc_dai_link fsl_asoc_card_dai[] = {
300 /* Default ASoC DAI Link*/
303 .stream_name = "HiFi",
304 .ops = &fsl_asoc_card_ops,
305 SND_SOC_DAILINK_REG(hifi),
307 /* DPCM Link between Front-End and Back-End (Optional) */
309 .name = "HiFi-ASRC-FE",
310 .stream_name = "HiFi-ASRC-FE",
314 SND_SOC_DAILINK_REG(hifi_fe),
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,
324 SND_SOC_DAILINK_REG(hifi_be),
328 static int fsl_asoc_card_audmux_init(struct device_node *np,
329 struct fsl_asoc_card_priv *priv)
331 struct device *dev = &priv->pdev->dev;
332 u32 int_ptcr = 0, ext_ptcr = 0;
333 int int_port, ext_port;
336 ret = of_property_read_u32(np, "mux-int-port", &int_port);
338 dev_err(dev, "mux-int-port missing or invalid\n");
341 ret = of_property_read_u32(np, "mux-ext-port", &ext_port);
343 dev_err(dev, "mux-ext-port missing or invalid\n");
348 * The port numbering in the hardware manual starts at 1, while
349 * the AUDMUX API expects it starts at 0.
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.
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;
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;
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;
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;
401 if (!fsl_asoc_card_is_ac97(priv))
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;
414 /* Asynchronous mode can not be set along with RCLKDIR */
415 if (!fsl_asoc_card_is_ac97(priv)) {
417 IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port);
419 ret = imx_audmux_v2_configure_port(int_port, 0,
422 dev_err(dev, "audmux internal port setup failed\n");
427 ret = imx_audmux_v2_configure_port(int_port, int_ptcr,
428 IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port));
430 dev_err(dev, "audmux internal port setup failed\n");
434 if (!fsl_asoc_card_is_ac97(priv)) {
436 IMX_AUDMUX_V2_PDCR_RXDSEL(int_port);
438 ret = imx_audmux_v2_configure_port(ext_port, 0,
441 dev_err(dev, "audmux external port setup failed\n");
446 ret = imx_audmux_v2_configure_port(ext_port, ext_ptcr,
447 IMX_AUDMUX_V2_PDCR_RXDSEL(int_port));
449 dev_err(dev, "audmux external port setup failed\n");
456 static int hp_jack_event(struct notifier_block *nb, unsigned long event,
459 struct snd_soc_jack *jack = (struct snd_soc_jack *)data;
460 struct snd_soc_dapm_context *dapm = &jack->card->dapm;
462 if (event & SND_JACK_HEADPHONE)
463 /* Disable speaker if headphone is plugged in */
464 snd_soc_dapm_disable_pin(dapm, "Ext Spk");
466 snd_soc_dapm_enable_pin(dapm, "Ext Spk");
471 static struct notifier_block hp_jack_nb = {
472 .notifier_call = hp_jack_event,
475 static int mic_jack_event(struct notifier_block *nb, unsigned long event,
478 struct snd_soc_jack *jack = (struct snd_soc_jack *)data;
479 struct snd_soc_dapm_context *dapm = &jack->card->dapm;
481 if (event & SND_JACK_MICROPHONE)
482 /* Disable dmic if microphone is plugged in */
483 snd_soc_dapm_disable_pin(dapm, "DMIC");
485 snd_soc_dapm_enable_pin(dapm, "DMIC");
490 static struct notifier_block mic_jack_nb = {
491 .notifier_call = mic_jack_event,
494 static int fsl_asoc_card_late_probe(struct snd_soc_card *card)
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;
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);
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
514 snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS,
515 AC97_EA_SPSA_SLOT_MASK, AC97_EA_SPSA_3_4);
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__);
531 static int fsl_asoc_card_probe(struct platform_device *pdev)
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;
547 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
551 cpu_np = of_parse_phandle(np, "audio-cpu", 0);
552 /* Give a chance to old DT binding */
554 cpu_np = of_parse_phandle(np, "ssi-controller", 0);
556 dev_err(&pdev->dev, "CPU phandle missing or invalid\n");
561 cpu_pdev = of_find_device_by_node(cpu_np);
563 dev_err(&pdev->dev, "failed to find CPU DAI device\n");
568 codec_np = of_parse_phandle(np, "audio-codec", 0);
570 struct platform_device *codec_pdev;
571 struct i2c_client *codec_i2c;
573 codec_i2c = of_find_i2c_device_by_node(codec_np);
575 codec_dev = &codec_i2c->dev;
576 codec_dev_name = codec_i2c->name;
579 codec_pdev = of_find_device_by_node(codec_np);
581 codec_dev = &codec_pdev->dev;
582 codec_dev_name = codec_pdev->name;
587 asrc_np = of_parse_phandle(np, "audio-asrc", 0);
589 asrc_pdev = of_find_device_by_node(asrc_np);
591 /* Get the MCLK rate only, and leave it controlled by CODEC drivers */
593 struct clk *codec_clk = clk_get(codec_dev, NULL);
595 if (!IS_ERR(codec_clk)) {
596 priv->codec_priv.mclk_freq = clk_get_rate(codec_clk);
601 /* Default sample rate and format, will be updated in hw_params() */
602 priv->sample_rate = 44100;
603 priv->sample_format = SNDRV_PCM_FORMAT_S16_LE;
605 /* Assign a default DAI format, and allow each card to overwrite it */
606 priv->dai_fmt = DAI_FMT_BASE;
608 memcpy(priv->dai_link, fsl_asoc_card_dai,
609 sizeof(struct snd_soc_dai_link) * ARRAY_SIZE(priv->dai_link));
611 priv->card.dapm_routes = audio_map;
612 priv->card.num_dapm_routes = ARRAY_SIZE(audio_map);
613 /* Diversify the card configurations */
614 if (of_device_is_compatible(np, "fsl,imx-audio-cs42888")) {
615 codec_dai_name = "cs42888";
616 priv->cpu_priv.sysclk_freq[TX] = priv->codec_priv.mclk_freq;
617 priv->cpu_priv.sysclk_freq[RX] = priv->codec_priv.mclk_freq;
618 priv->cpu_priv.sysclk_dir[TX] = SND_SOC_CLOCK_OUT;
619 priv->cpu_priv.sysclk_dir[RX] = SND_SOC_CLOCK_OUT;
620 priv->cpu_priv.slot_width = 32;
621 priv->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
622 } else if (of_device_is_compatible(np, "fsl,imx-audio-cs427x")) {
623 codec_dai_name = "cs4271-hifi";
624 priv->codec_priv.mclk_id = CS427x_SYSCLK_MCLK;
625 priv->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
626 } else if (of_device_is_compatible(np, "fsl,imx-audio-sgtl5000")) {
627 codec_dai_name = "sgtl5000";
628 priv->codec_priv.mclk_id = SGTL5000_SYSCLK;
629 priv->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
630 } else if (of_device_is_compatible(np, "fsl,imx-audio-tlv320aic32x4")) {
631 codec_dai_name = "tlv320aic32x4-hifi";
632 priv->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
633 } else if (of_device_is_compatible(np, "fsl,imx-audio-wm8962")) {
634 codec_dai_name = "wm8962";
635 priv->codec_priv.mclk_id = WM8962_SYSCLK_MCLK;
636 priv->codec_priv.fll_id = WM8962_SYSCLK_FLL;
637 priv->codec_priv.pll_id = WM8962_FLL;
638 priv->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
639 } else if (of_device_is_compatible(np, "fsl,imx-audio-wm8960")) {
640 codec_dai_name = "wm8960-hifi";
641 priv->codec_priv.fll_id = WM8960_SYSCLK_AUTO;
642 priv->codec_priv.pll_id = WM8960_SYSCLK_AUTO;
643 priv->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
644 } else if (of_device_is_compatible(np, "fsl,imx-audio-ac97")) {
645 codec_dai_name = "ac97-hifi";
646 priv->dai_fmt = SND_SOC_DAIFMT_AC97;
647 priv->card.dapm_routes = audio_map_ac97;
648 priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_ac97);
649 } else if (of_device_is_compatible(np, "fsl,imx-audio-mqs")) {
650 codec_dai_name = "fsl-mqs-dai";
651 priv->dai_fmt = SND_SOC_DAIFMT_LEFT_J |
652 SND_SOC_DAIFMT_CBS_CFS |
653 SND_SOC_DAIFMT_NB_NF;
654 priv->dai_link[1].dpcm_capture = 0;
655 priv->dai_link[2].dpcm_capture = 0;
656 priv->card.dapm_routes = audio_map_tx;
657 priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_tx);
658 } else if (of_device_is_compatible(np, "fsl,imx-audio-wm8524")) {
659 codec_dai_name = "wm8524-hifi";
660 priv->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
661 priv->dai_link[1].dpcm_capture = 0;
662 priv->dai_link[2].dpcm_capture = 0;
663 priv->cpu_priv.slot_width = 32;
664 priv->card.dapm_routes = audio_map_tx;
665 priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_tx);
666 } else if (of_device_is_compatible(np, "fsl,imx-audio-si476x")) {
667 codec_dai_name = "si476x-codec";
668 priv->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
669 priv->card.dapm_routes = audio_map_rx;
670 priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_rx);
671 } else if (of_device_is_compatible(np, "fsl,imx-audio-wm8958")) {
672 codec_dai_name = "wm8994-aif1";
673 priv->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
674 priv->codec_priv.mclk_id = WM8994_FLL_SRC_MCLK1;
675 priv->codec_priv.fll_id = WM8994_SYSCLK_FLL1;
676 priv->codec_priv.pll_id = WM8994_FLL1;
677 priv->codec_priv.free_freq = priv->codec_priv.mclk_freq;
678 priv->card.dapm_routes = NULL;
679 priv->card.num_dapm_routes = 0;
681 dev_err(&pdev->dev, "unknown Device Tree compatible\n");
686 /* Format info from DT is optional. */
687 daifmt = snd_soc_of_parse_daifmt(np, NULL,
688 &bitclkmaster, &framemaster);
689 daifmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
690 if (bitclkmaster || framemaster) {
691 if (codec_np == bitclkmaster)
692 daifmt |= (codec_np == framemaster) ?
693 SND_SOC_DAIFMT_CBM_CFM : SND_SOC_DAIFMT_CBM_CFS;
695 daifmt |= (codec_np == framemaster) ?
696 SND_SOC_DAIFMT_CBS_CFM : SND_SOC_DAIFMT_CBS_CFS;
698 /* Override dai_fmt with value from DT */
699 priv->dai_fmt = daifmt;
702 /* Change direction according to format */
703 if (priv->dai_fmt & SND_SOC_DAIFMT_CBM_CFM) {
704 priv->cpu_priv.sysclk_dir[TX] = SND_SOC_CLOCK_IN;
705 priv->cpu_priv.sysclk_dir[RX] = SND_SOC_CLOCK_IN;
708 of_node_put(bitclkmaster);
709 of_node_put(framemaster);
711 if (!fsl_asoc_card_is_ac97(priv) && !codec_dev) {
712 dev_err(&pdev->dev, "failed to find codec device\n");
717 /* Common settings for corresponding Freescale CPU DAI driver */
718 if (of_node_name_eq(cpu_np, "ssi")) {
719 /* Only SSI needs to configure AUDMUX */
720 ret = fsl_asoc_card_audmux_init(np, priv);
722 dev_err(&pdev->dev, "failed to init audmux\n");
725 } else if (of_node_name_eq(cpu_np, "esai")) {
726 struct clk *esai_clk = clk_get(&cpu_pdev->dev, "extal");
728 if (!IS_ERR(esai_clk)) {
729 priv->cpu_priv.sysclk_freq[TX] = clk_get_rate(esai_clk);
730 priv->cpu_priv.sysclk_freq[RX] = clk_get_rate(esai_clk);
732 } else if (PTR_ERR(esai_clk) == -EPROBE_DEFER) {
737 priv->cpu_priv.sysclk_id[1] = ESAI_HCKT_EXTAL;
738 priv->cpu_priv.sysclk_id[0] = ESAI_HCKR_EXTAL;
739 } else if (of_node_name_eq(cpu_np, "sai")) {
740 priv->cpu_priv.sysclk_id[1] = FSL_SAI_CLK_MAST1;
741 priv->cpu_priv.sysclk_id[0] = FSL_SAI_CLK_MAST1;
744 /* Initialize sound card */
746 priv->card.dev = &pdev->dev;
747 priv->card.owner = THIS_MODULE;
748 ret = snd_soc_of_parse_card_name(&priv->card, "model");
750 snprintf(priv->name, sizeof(priv->name), "%s-audio",
751 fsl_asoc_card_is_ac97(priv) ? "ac97" : codec_dev_name);
752 priv->card.name = priv->name;
754 priv->card.dai_link = priv->dai_link;
755 priv->card.late_probe = fsl_asoc_card_late_probe;
756 priv->card.dapm_widgets = fsl_asoc_card_dapm_widgets;
757 priv->card.num_dapm_widgets = ARRAY_SIZE(fsl_asoc_card_dapm_widgets);
759 /* Drop the second half of DAPM routes -- ASRC */
761 priv->card.num_dapm_routes /= 2;
763 if (of_property_read_bool(np, "audio-routing")) {
764 ret = snd_soc_of_parse_audio_routing(&priv->card, "audio-routing");
766 dev_err(&pdev->dev, "failed to parse audio-routing: %d\n", ret);
771 /* Normal DAI Link */
772 priv->dai_link[0].cpus->of_node = cpu_np;
773 priv->dai_link[0].codecs->dai_name = codec_dai_name;
775 if (!fsl_asoc_card_is_ac97(priv))
776 priv->dai_link[0].codecs->of_node = codec_np;
780 ret = of_property_read_u32(cpu_np, "cell-index", &idx);
783 "cannot get CPU index property\n");
787 priv->dai_link[0].codecs->name =
788 devm_kasprintf(&pdev->dev, GFP_KERNEL,
791 if (!priv->dai_link[0].codecs->name) {
797 priv->dai_link[0].platforms->of_node = cpu_np;
798 priv->dai_link[0].dai_fmt = priv->dai_fmt;
799 priv->card.num_links = 1;
802 /* DPCM DAI Links only if ASRC exsits */
803 priv->dai_link[1].cpus->of_node = asrc_np;
804 priv->dai_link[1].platforms->of_node = asrc_np;
805 priv->dai_link[2].codecs->dai_name = codec_dai_name;
806 priv->dai_link[2].codecs->of_node = codec_np;
807 priv->dai_link[2].codecs->name =
808 priv->dai_link[0].codecs->name;
809 priv->dai_link[2].cpus->of_node = cpu_np;
810 priv->dai_link[2].dai_fmt = priv->dai_fmt;
811 priv->card.num_links = 3;
813 ret = of_property_read_u32(asrc_np, "fsl,asrc-rate",
816 dev_err(&pdev->dev, "failed to get output rate\n");
821 ret = of_property_read_u32(asrc_np, "fsl,asrc-format",
824 /* Fallback to old binding; translate to asrc_format */
825 ret = of_property_read_u32(asrc_np, "fsl,asrc-width",
829 "failed to decide output format\n");
834 priv->asrc_format = SNDRV_PCM_FORMAT_S24_LE;
836 priv->asrc_format = SNDRV_PCM_FORMAT_S16_LE;
840 /* Finish card registering */
841 platform_set_drvdata(pdev, priv);
842 snd_soc_card_set_drvdata(&priv->card, priv);
844 ret = devm_snd_soc_register_card(&pdev->dev, &priv->card);
846 if (ret != -EPROBE_DEFER)
847 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret);
852 * Properties "hp-det-gpio" and "mic-det-gpio" are optional, and
853 * asoc_simple_init_jack uses these properties for creating
854 * Headphone Jack and Microphone Jack.
856 * The notifier is initialized in snd_soc_card_jack_new(), then
857 * snd_soc_jack_notifier_register can be called.
859 if (of_property_read_bool(np, "hp-det-gpio")) {
860 ret = asoc_simple_init_jack(&priv->card, &priv->hp_jack,
861 1, NULL, "Headphone Jack");
865 snd_soc_jack_notifier_register(&priv->hp_jack.jack, &hp_jack_nb);
868 if (of_property_read_bool(np, "mic-det-gpio")) {
869 ret = asoc_simple_init_jack(&priv->card, &priv->mic_jack,
870 0, NULL, "Mic Jack");
874 snd_soc_jack_notifier_register(&priv->mic_jack.jack, &mic_jack_nb);
878 of_node_put(asrc_np);
879 of_node_put(codec_np);
880 put_device(&cpu_pdev->dev);
887 static const struct of_device_id fsl_asoc_card_dt_ids[] = {
888 { .compatible = "fsl,imx-audio-ac97", },
889 { .compatible = "fsl,imx-audio-cs42888", },
890 { .compatible = "fsl,imx-audio-cs427x", },
891 { .compatible = "fsl,imx-audio-tlv320aic32x4", },
892 { .compatible = "fsl,imx-audio-sgtl5000", },
893 { .compatible = "fsl,imx-audio-wm8962", },
894 { .compatible = "fsl,imx-audio-wm8960", },
895 { .compatible = "fsl,imx-audio-mqs", },
896 { .compatible = "fsl,imx-audio-wm8524", },
897 { .compatible = "fsl,imx-audio-si476x", },
898 { .compatible = "fsl,imx-audio-wm8958", },
901 MODULE_DEVICE_TABLE(of, fsl_asoc_card_dt_ids);
903 static struct platform_driver fsl_asoc_card_driver = {
904 .probe = fsl_asoc_card_probe,
906 .name = "fsl-asoc-card",
907 .pm = &snd_soc_pm_ops,
908 .of_match_table = fsl_asoc_card_dt_ids,
911 module_platform_driver(fsl_asoc_card_driver);
913 MODULE_DESCRIPTION("Freescale Generic ASoC Sound Card driver with ASRC");
914 MODULE_AUTHOR("Nicolin Chen <nicoleotsuka@gmail.com>");
915 MODULE_ALIAS("platform:fsl-asoc-card");
916 MODULE_LICENSE("GPL");