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"
29 #define CS427x_SYSCLK_MCLK 0
34 /* Default DAI format without Master and Slave flag */
35 #define DAI_FMT_BASE (SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF)
38 * struct codec_priv - CODEC private data
39 * @mclk_freq: Clock rate of MCLK
40 * @mclk_id: MCLK (or main clock) id for set_sysclk()
41 * @fll_id: FLL (or secordary clock) id for set_sysclk()
42 * @pll_id: PLL id for set_pll()
45 unsigned long mclk_freq;
52 * struct cpu_priv - CPU private data
53 * @sysclk_freq: SYSCLK rates for set_sysclk()
54 * @sysclk_dir: SYSCLK directions for set_sysclk()
55 * @sysclk_id: SYSCLK ids for set_sysclk()
56 * @slot_width: Slot width of each frame
58 * Note: [1] for tx and [0] for rx
61 unsigned long sysclk_freq[2];
68 * struct fsl_asoc_card_priv - Freescale Generic ASOC card private data
69 * @dai_link: DAI link structure including normal one and DPCM link
70 * @hp_jack: Headphone Jack structure
71 * @mic_jack: Microphone Jack structure
72 * @pdev: platform device pointer
73 * @codec_priv: CODEC private data
74 * @cpu_priv: CPU private data
75 * @card: ASoC card structure
76 * @streams: Mask of current active streams
77 * @sample_rate: Current sample rate
78 * @sample_format: Current sample format
79 * @asrc_rate: ASRC sample rate used by Back-Ends
80 * @asrc_format: ASRC sample format used by Back-Ends
81 * @dai_fmt: DAI format between CPU and CODEC
85 struct fsl_asoc_card_priv {
86 struct snd_soc_dai_link dai_link[3];
87 struct asoc_simple_jack hp_jack;
88 struct asoc_simple_jack mic_jack;
89 struct platform_device *pdev;
90 struct codec_priv codec_priv;
91 struct cpu_priv cpu_priv;
92 struct snd_soc_card card;
95 snd_pcm_format_t sample_format;
97 snd_pcm_format_t asrc_format;
103 * This dapm route map exists for DPCM link only.
104 * The other routes shall go through Device Tree.
106 * Note: keep all ASRC routes in the second half
107 * to drop them easily for non-ASRC cases.
109 static const struct snd_soc_dapm_route audio_map[] = {
110 /* 1st half -- Normal DAPM routes */
111 {"Playback", NULL, "CPU-Playback"},
112 {"CPU-Capture", NULL, "Capture"},
113 /* 2nd half -- ASRC DAPM routes */
114 {"CPU-Playback", NULL, "ASRC-Playback"},
115 {"ASRC-Capture", NULL, "CPU-Capture"},
118 static const struct snd_soc_dapm_route audio_map_ac97[] = {
119 /* 1st half -- Normal DAPM routes */
120 {"Playback", NULL, "AC97 Playback"},
121 {"AC97 Capture", NULL, "Capture"},
122 /* 2nd half -- ASRC DAPM routes */
123 {"AC97 Playback", NULL, "ASRC-Playback"},
124 {"ASRC-Capture", NULL, "AC97 Capture"},
127 static const struct snd_soc_dapm_route audio_map_tx[] = {
128 /* 1st half -- Normal DAPM routes */
129 {"Playback", NULL, "CPU-Playback"},
130 /* 2nd half -- ASRC DAPM routes */
131 {"CPU-Playback", NULL, "ASRC-Playback"},
134 static const struct snd_soc_dapm_route audio_map_rx[] = {
135 /* 1st half -- Normal DAPM routes */
136 {"CPU-Capture", NULL, "Capture"},
137 /* 2nd half -- ASRC DAPM routes */
138 {"ASRC-Capture", NULL, "CPU-Capture"},
141 /* Add all possible widgets into here without being redundant */
142 static const struct snd_soc_dapm_widget fsl_asoc_card_dapm_widgets[] = {
143 SND_SOC_DAPM_LINE("Line Out Jack", NULL),
144 SND_SOC_DAPM_LINE("Line In Jack", NULL),
145 SND_SOC_DAPM_HP("Headphone Jack", NULL),
146 SND_SOC_DAPM_SPK("Ext Spk", NULL),
147 SND_SOC_DAPM_MIC("Mic Jack", NULL),
148 SND_SOC_DAPM_MIC("AMIC", NULL),
149 SND_SOC_DAPM_MIC("DMIC", NULL),
152 static bool fsl_asoc_card_is_ac97(struct fsl_asoc_card_priv *priv)
154 return priv->dai_fmt == SND_SOC_DAIFMT_AC97;
157 static int fsl_asoc_card_hw_params(struct snd_pcm_substream *substream,
158 struct snd_pcm_hw_params *params)
160 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
161 struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
162 bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
163 struct codec_priv *codec_priv = &priv->codec_priv;
164 struct cpu_priv *cpu_priv = &priv->cpu_priv;
165 struct device *dev = rtd->card->dev;
166 unsigned int pll_out;
169 priv->sample_rate = params_rate(params);
170 priv->sample_format = params_format(params);
171 priv->streams |= BIT(substream->stream);
173 if (fsl_asoc_card_is_ac97(priv))
176 /* Specific configurations of DAIs starts from here */
177 ret = snd_soc_dai_set_sysclk(asoc_rtd_to_cpu(rtd, 0), cpu_priv->sysclk_id[tx],
178 cpu_priv->sysclk_freq[tx],
179 cpu_priv->sysclk_dir[tx]);
180 if (ret && ret != -ENOTSUPP) {
181 dev_err(dev, "failed to set sysclk for cpu dai\n");
185 if (cpu_priv->slot_width) {
186 ret = snd_soc_dai_set_tdm_slot(asoc_rtd_to_cpu(rtd, 0), 0x3, 0x3, 2,
187 cpu_priv->slot_width);
188 if (ret && ret != -ENOTSUPP) {
189 dev_err(dev, "failed to set TDM slot for cpu dai\n");
194 /* Specific configuration for PLL */
195 if (codec_priv->pll_id && codec_priv->fll_id) {
196 if (priv->sample_format == SNDRV_PCM_FORMAT_S24_LE)
197 pll_out = priv->sample_rate * 384;
199 pll_out = priv->sample_rate * 256;
201 ret = snd_soc_dai_set_pll(asoc_rtd_to_codec(rtd, 0),
204 codec_priv->mclk_freq, pll_out);
206 dev_err(dev, "failed to start FLL: %d\n", ret);
210 ret = snd_soc_dai_set_sysclk(asoc_rtd_to_codec(rtd, 0),
212 pll_out, SND_SOC_CLOCK_IN);
214 if (ret && ret != -ENOTSUPP) {
215 dev_err(dev, "failed to set SYSCLK: %d\n", ret);
223 priv->streams &= ~BIT(substream->stream);
227 static int fsl_asoc_card_hw_free(struct snd_pcm_substream *substream)
229 struct snd_soc_pcm_runtime *rtd = substream->private_data;
230 struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
231 struct codec_priv *codec_priv = &priv->codec_priv;
232 struct device *dev = rtd->card->dev;
235 priv->streams &= ~BIT(substream->stream);
237 if (!priv->streams && codec_priv->pll_id && codec_priv->fll_id) {
238 /* Force freq to be 0 to avoid error message in codec */
239 ret = snd_soc_dai_set_sysclk(asoc_rtd_to_codec(rtd, 0),
244 dev_err(dev, "failed to switch away from FLL: %d\n", ret);
248 ret = snd_soc_dai_set_pll(asoc_rtd_to_codec(rtd, 0),
249 codec_priv->pll_id, 0, 0, 0);
250 if (ret && ret != -ENOTSUPP) {
251 dev_err(dev, "failed to stop FLL: %d\n", ret);
259 static const struct snd_soc_ops fsl_asoc_card_ops = {
260 .hw_params = fsl_asoc_card_hw_params,
261 .hw_free = fsl_asoc_card_hw_free,
264 static int be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
265 struct snd_pcm_hw_params *params)
267 struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
268 struct snd_interval *rate;
269 struct snd_mask *mask;
271 rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
272 rate->max = rate->min = priv->asrc_rate;
274 mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
276 snd_mask_set_format(mask, priv->asrc_format);
281 SND_SOC_DAILINK_DEFS(hifi,
282 DAILINK_COMP_ARRAY(COMP_EMPTY()),
283 DAILINK_COMP_ARRAY(COMP_EMPTY()),
284 DAILINK_COMP_ARRAY(COMP_EMPTY()));
286 SND_SOC_DAILINK_DEFS(hifi_fe,
287 DAILINK_COMP_ARRAY(COMP_EMPTY()),
288 DAILINK_COMP_ARRAY(COMP_DUMMY()),
289 DAILINK_COMP_ARRAY(COMP_EMPTY()));
291 SND_SOC_DAILINK_DEFS(hifi_be,
292 DAILINK_COMP_ARRAY(COMP_EMPTY()),
293 DAILINK_COMP_ARRAY(COMP_EMPTY()),
294 DAILINK_COMP_ARRAY(COMP_DUMMY()));
296 static struct snd_soc_dai_link fsl_asoc_card_dai[] = {
297 /* Default ASoC DAI Link*/
300 .stream_name = "HiFi",
301 .ops = &fsl_asoc_card_ops,
302 SND_SOC_DAILINK_REG(hifi),
304 /* DPCM Link between Front-End and Back-End (Optional) */
306 .name = "HiFi-ASRC-FE",
307 .stream_name = "HiFi-ASRC-FE",
311 SND_SOC_DAILINK_REG(hifi_fe),
314 .name = "HiFi-ASRC-BE",
315 .stream_name = "HiFi-ASRC-BE",
316 .be_hw_params_fixup = be_hw_params_fixup,
317 .ops = &fsl_asoc_card_ops,
321 SND_SOC_DAILINK_REG(hifi_be),
325 static int fsl_asoc_card_audmux_init(struct device_node *np,
326 struct fsl_asoc_card_priv *priv)
328 struct device *dev = &priv->pdev->dev;
329 u32 int_ptcr = 0, ext_ptcr = 0;
330 int int_port, ext_port;
333 ret = of_property_read_u32(np, "mux-int-port", &int_port);
335 dev_err(dev, "mux-int-port missing or invalid\n");
338 ret = of_property_read_u32(np, "mux-ext-port", &ext_port);
340 dev_err(dev, "mux-ext-port missing or invalid\n");
345 * The port numbering in the hardware manual starts at 1, while
346 * the AUDMUX API expects it starts at 0.
352 * Use asynchronous mode (6 wires) for all cases except AC97.
353 * If only 4 wires are needed, just set SSI into
354 * synchronous mode and enable 4 PADs in IOMUX.
356 switch (priv->dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
357 case SND_SOC_DAIFMT_CBM_CFM:
358 int_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | ext_port) |
359 IMX_AUDMUX_V2_PTCR_RCSEL(8 | ext_port) |
360 IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) |
361 IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
362 IMX_AUDMUX_V2_PTCR_RFSDIR |
363 IMX_AUDMUX_V2_PTCR_RCLKDIR |
364 IMX_AUDMUX_V2_PTCR_TFSDIR |
365 IMX_AUDMUX_V2_PTCR_TCLKDIR;
367 case SND_SOC_DAIFMT_CBM_CFS:
368 int_ptcr = IMX_AUDMUX_V2_PTCR_RCSEL(8 | ext_port) |
369 IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
370 IMX_AUDMUX_V2_PTCR_RCLKDIR |
371 IMX_AUDMUX_V2_PTCR_TCLKDIR;
372 ext_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | int_port) |
373 IMX_AUDMUX_V2_PTCR_TFSEL(int_port) |
374 IMX_AUDMUX_V2_PTCR_RFSDIR |
375 IMX_AUDMUX_V2_PTCR_TFSDIR;
377 case SND_SOC_DAIFMT_CBS_CFM:
378 int_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | ext_port) |
379 IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) |
380 IMX_AUDMUX_V2_PTCR_RFSDIR |
381 IMX_AUDMUX_V2_PTCR_TFSDIR;
382 ext_ptcr = IMX_AUDMUX_V2_PTCR_RCSEL(8 | int_port) |
383 IMX_AUDMUX_V2_PTCR_TCSEL(int_port) |
384 IMX_AUDMUX_V2_PTCR_RCLKDIR |
385 IMX_AUDMUX_V2_PTCR_TCLKDIR;
387 case SND_SOC_DAIFMT_CBS_CFS:
388 ext_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | int_port) |
389 IMX_AUDMUX_V2_PTCR_RCSEL(8 | int_port) |
390 IMX_AUDMUX_V2_PTCR_TFSEL(int_port) |
391 IMX_AUDMUX_V2_PTCR_TCSEL(int_port) |
392 IMX_AUDMUX_V2_PTCR_RFSDIR |
393 IMX_AUDMUX_V2_PTCR_RCLKDIR |
394 IMX_AUDMUX_V2_PTCR_TFSDIR |
395 IMX_AUDMUX_V2_PTCR_TCLKDIR;
398 if (!fsl_asoc_card_is_ac97(priv))
402 if (fsl_asoc_card_is_ac97(priv)) {
403 int_ptcr = IMX_AUDMUX_V2_PTCR_SYN |
404 IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
405 IMX_AUDMUX_V2_PTCR_TCLKDIR;
406 ext_ptcr = IMX_AUDMUX_V2_PTCR_SYN |
407 IMX_AUDMUX_V2_PTCR_TFSEL(int_port) |
408 IMX_AUDMUX_V2_PTCR_TFSDIR;
411 /* Asynchronous mode can not be set along with RCLKDIR */
412 if (!fsl_asoc_card_is_ac97(priv)) {
414 IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port);
416 ret = imx_audmux_v2_configure_port(int_port, 0,
419 dev_err(dev, "audmux internal port setup failed\n");
424 ret = imx_audmux_v2_configure_port(int_port, int_ptcr,
425 IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port));
427 dev_err(dev, "audmux internal port setup failed\n");
431 if (!fsl_asoc_card_is_ac97(priv)) {
433 IMX_AUDMUX_V2_PDCR_RXDSEL(int_port);
435 ret = imx_audmux_v2_configure_port(ext_port, 0,
438 dev_err(dev, "audmux external port setup failed\n");
443 ret = imx_audmux_v2_configure_port(ext_port, ext_ptcr,
444 IMX_AUDMUX_V2_PDCR_RXDSEL(int_port));
446 dev_err(dev, "audmux external port setup failed\n");
453 static int hp_jack_event(struct notifier_block *nb, unsigned long event,
456 struct snd_soc_jack *jack = (struct snd_soc_jack *)data;
457 struct snd_soc_dapm_context *dapm = &jack->card->dapm;
459 if (event & SND_JACK_HEADPHONE)
460 /* Disable speaker if headphone is plugged in */
461 snd_soc_dapm_disable_pin(dapm, "Ext Spk");
463 snd_soc_dapm_enable_pin(dapm, "Ext Spk");
468 static struct notifier_block hp_jack_nb = {
469 .notifier_call = hp_jack_event,
472 static int mic_jack_event(struct notifier_block *nb, unsigned long event,
475 struct snd_soc_jack *jack = (struct snd_soc_jack *)data;
476 struct snd_soc_dapm_context *dapm = &jack->card->dapm;
478 if (event & SND_JACK_MICROPHONE)
479 /* Disable dmic if microphone is plugged in */
480 snd_soc_dapm_disable_pin(dapm, "DMIC");
482 snd_soc_dapm_enable_pin(dapm, "DMIC");
487 static struct notifier_block mic_jack_nb = {
488 .notifier_call = mic_jack_event,
491 static int fsl_asoc_card_late_probe(struct snd_soc_card *card)
493 struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(card);
494 struct snd_soc_pcm_runtime *rtd = list_first_entry(
495 &card->rtd_list, struct snd_soc_pcm_runtime, list);
496 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
497 struct codec_priv *codec_priv = &priv->codec_priv;
498 struct device *dev = card->dev;
501 if (fsl_asoc_card_is_ac97(priv)) {
502 #if IS_ENABLED(CONFIG_SND_AC97_CODEC)
503 struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
504 struct snd_ac97 *ac97 = snd_soc_component_get_drvdata(component);
507 * Use slots 3/4 for S/PDIF so SSI won't try to enable
508 * other slots and send some samples there
509 * due to SLOTREQ bits for S/PDIF received from codec
511 snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS,
512 AC97_EA_SPSA_SLOT_MASK, AC97_EA_SPSA_3_4);
518 ret = snd_soc_dai_set_sysclk(codec_dai, codec_priv->mclk_id,
519 codec_priv->mclk_freq, SND_SOC_CLOCK_IN);
520 if (ret && ret != -ENOTSUPP) {
521 dev_err(dev, "failed to set sysclk in %s\n", __func__);
528 static int fsl_asoc_card_probe(struct platform_device *pdev)
530 struct device_node *cpu_np, *codec_np, *asrc_np;
531 struct device_node *np = pdev->dev.of_node;
532 struct platform_device *asrc_pdev = NULL;
533 struct device_node *bitclkmaster = NULL;
534 struct device_node *framemaster = NULL;
535 struct platform_device *cpu_pdev;
536 struct fsl_asoc_card_priv *priv;
537 struct device *codec_dev = NULL;
538 const char *codec_dai_name;
539 const char *codec_dev_name;
544 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
548 cpu_np = of_parse_phandle(np, "audio-cpu", 0);
549 /* Give a chance to old DT binding */
551 cpu_np = of_parse_phandle(np, "ssi-controller", 0);
553 dev_err(&pdev->dev, "CPU phandle missing or invalid\n");
558 cpu_pdev = of_find_device_by_node(cpu_np);
560 dev_err(&pdev->dev, "failed to find CPU DAI device\n");
565 codec_np = of_parse_phandle(np, "audio-codec", 0);
567 struct platform_device *codec_pdev;
568 struct i2c_client *codec_i2c;
570 codec_i2c = of_find_i2c_device_by_node(codec_np);
572 codec_dev = &codec_i2c->dev;
573 codec_dev_name = codec_i2c->name;
576 codec_pdev = of_find_device_by_node(codec_np);
578 codec_dev = &codec_pdev->dev;
579 codec_dev_name = codec_pdev->name;
584 asrc_np = of_parse_phandle(np, "audio-asrc", 0);
586 asrc_pdev = of_find_device_by_node(asrc_np);
588 /* Get the MCLK rate only, and leave it controlled by CODEC drivers */
590 struct clk *codec_clk = clk_get(codec_dev, NULL);
592 if (!IS_ERR(codec_clk)) {
593 priv->codec_priv.mclk_freq = clk_get_rate(codec_clk);
598 /* Default sample rate and format, will be updated in hw_params() */
599 priv->sample_rate = 44100;
600 priv->sample_format = SNDRV_PCM_FORMAT_S16_LE;
602 /* Assign a default DAI format, and allow each card to overwrite it */
603 priv->dai_fmt = DAI_FMT_BASE;
605 memcpy(priv->dai_link, fsl_asoc_card_dai,
606 sizeof(struct snd_soc_dai_link) * ARRAY_SIZE(priv->dai_link));
608 priv->card.dapm_routes = audio_map;
609 priv->card.num_dapm_routes = ARRAY_SIZE(audio_map);
610 /* Diversify the card configurations */
611 if (of_device_is_compatible(np, "fsl,imx-audio-cs42888")) {
612 codec_dai_name = "cs42888";
613 priv->cpu_priv.sysclk_freq[TX] = priv->codec_priv.mclk_freq;
614 priv->cpu_priv.sysclk_freq[RX] = priv->codec_priv.mclk_freq;
615 priv->cpu_priv.sysclk_dir[TX] = SND_SOC_CLOCK_OUT;
616 priv->cpu_priv.sysclk_dir[RX] = SND_SOC_CLOCK_OUT;
617 priv->cpu_priv.slot_width = 32;
618 priv->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
619 } else if (of_device_is_compatible(np, "fsl,imx-audio-cs427x")) {
620 codec_dai_name = "cs4271-hifi";
621 priv->codec_priv.mclk_id = CS427x_SYSCLK_MCLK;
622 priv->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
623 } else if (of_device_is_compatible(np, "fsl,imx-audio-sgtl5000")) {
624 codec_dai_name = "sgtl5000";
625 priv->codec_priv.mclk_id = SGTL5000_SYSCLK;
626 priv->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
627 } else if (of_device_is_compatible(np, "fsl,imx-audio-tlv320aic32x4")) {
628 codec_dai_name = "tlv320aic32x4-hifi";
629 priv->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
630 } else if (of_device_is_compatible(np, "fsl,imx-audio-wm8962")) {
631 codec_dai_name = "wm8962";
632 priv->codec_priv.mclk_id = WM8962_SYSCLK_MCLK;
633 priv->codec_priv.fll_id = WM8962_SYSCLK_FLL;
634 priv->codec_priv.pll_id = WM8962_FLL;
635 priv->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
636 } else if (of_device_is_compatible(np, "fsl,imx-audio-wm8960")) {
637 codec_dai_name = "wm8960-hifi";
638 priv->codec_priv.fll_id = WM8960_SYSCLK_AUTO;
639 priv->codec_priv.pll_id = WM8960_SYSCLK_AUTO;
640 priv->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
641 } else if (of_device_is_compatible(np, "fsl,imx-audio-ac97")) {
642 codec_dai_name = "ac97-hifi";
643 priv->dai_fmt = SND_SOC_DAIFMT_AC97;
644 priv->card.dapm_routes = audio_map_ac97;
645 priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_ac97);
646 } else if (of_device_is_compatible(np, "fsl,imx-audio-mqs")) {
647 codec_dai_name = "fsl-mqs-dai";
648 priv->dai_fmt = SND_SOC_DAIFMT_LEFT_J |
649 SND_SOC_DAIFMT_CBS_CFS |
650 SND_SOC_DAIFMT_NB_NF;
651 priv->dai_link[1].dpcm_capture = 0;
652 priv->dai_link[2].dpcm_capture = 0;
653 priv->card.dapm_routes = audio_map_tx;
654 priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_tx);
655 } else if (of_device_is_compatible(np, "fsl,imx-audio-wm8524")) {
656 codec_dai_name = "wm8524-hifi";
657 priv->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
658 priv->dai_link[1].dpcm_capture = 0;
659 priv->dai_link[2].dpcm_capture = 0;
660 priv->cpu_priv.slot_width = 32;
661 priv->card.dapm_routes = audio_map_tx;
662 priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_tx);
663 } else if (of_device_is_compatible(np, "fsl,imx-audio-si476x")) {
664 codec_dai_name = "si476x-codec";
665 priv->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
666 priv->card.dapm_routes = audio_map_rx;
667 priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_rx);
669 dev_err(&pdev->dev, "unknown Device Tree compatible\n");
674 /* Format info from DT is optional. */
675 daifmt = snd_soc_of_parse_daifmt(np, NULL,
676 &bitclkmaster, &framemaster);
677 daifmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
678 if (bitclkmaster || framemaster) {
679 if (codec_np == bitclkmaster)
680 daifmt |= (codec_np == framemaster) ?
681 SND_SOC_DAIFMT_CBM_CFM : SND_SOC_DAIFMT_CBM_CFS;
683 daifmt |= (codec_np == framemaster) ?
684 SND_SOC_DAIFMT_CBS_CFM : SND_SOC_DAIFMT_CBS_CFS;
686 /* Override dai_fmt with value from DT */
687 priv->dai_fmt = daifmt;
690 /* Change direction according to format */
691 if (priv->dai_fmt & SND_SOC_DAIFMT_CBM_CFM) {
692 priv->cpu_priv.sysclk_dir[TX] = SND_SOC_CLOCK_IN;
693 priv->cpu_priv.sysclk_dir[RX] = SND_SOC_CLOCK_IN;
696 of_node_put(bitclkmaster);
697 of_node_put(framemaster);
699 if (!fsl_asoc_card_is_ac97(priv) && !codec_dev) {
700 dev_err(&pdev->dev, "failed to find codec device\n");
705 /* Common settings for corresponding Freescale CPU DAI driver */
706 if (of_node_name_eq(cpu_np, "ssi")) {
707 /* Only SSI needs to configure AUDMUX */
708 ret = fsl_asoc_card_audmux_init(np, priv);
710 dev_err(&pdev->dev, "failed to init audmux\n");
713 } else if (of_node_name_eq(cpu_np, "esai")) {
714 struct clk *esai_clk = clk_get(&cpu_pdev->dev, "extal");
716 if (!IS_ERR(esai_clk)) {
717 priv->cpu_priv.sysclk_freq[TX] = clk_get_rate(esai_clk);
718 priv->cpu_priv.sysclk_freq[RX] = clk_get_rate(esai_clk);
720 } else if (PTR_ERR(esai_clk) == -EPROBE_DEFER) {
725 priv->cpu_priv.sysclk_id[1] = ESAI_HCKT_EXTAL;
726 priv->cpu_priv.sysclk_id[0] = ESAI_HCKR_EXTAL;
727 } else if (of_node_name_eq(cpu_np, "sai")) {
728 priv->cpu_priv.sysclk_id[1] = FSL_SAI_CLK_MAST1;
729 priv->cpu_priv.sysclk_id[0] = FSL_SAI_CLK_MAST1;
732 /* Initialize sound card */
734 priv->card.dev = &pdev->dev;
735 ret = snd_soc_of_parse_card_name(&priv->card, "model");
737 snprintf(priv->name, sizeof(priv->name), "%s-audio",
738 fsl_asoc_card_is_ac97(priv) ? "ac97" : codec_dev_name);
739 priv->card.name = priv->name;
741 priv->card.dai_link = priv->dai_link;
742 priv->card.late_probe = fsl_asoc_card_late_probe;
743 priv->card.dapm_widgets = fsl_asoc_card_dapm_widgets;
744 priv->card.num_dapm_widgets = ARRAY_SIZE(fsl_asoc_card_dapm_widgets);
746 /* Drop the second half of DAPM routes -- ASRC */
748 priv->card.num_dapm_routes /= 2;
750 if (of_property_read_bool(np, "audio-routing")) {
751 ret = snd_soc_of_parse_audio_routing(&priv->card, "audio-routing");
753 dev_err(&pdev->dev, "failed to parse audio-routing: %d\n", ret);
758 /* Normal DAI Link */
759 priv->dai_link[0].cpus->of_node = cpu_np;
760 priv->dai_link[0].codecs->dai_name = codec_dai_name;
762 if (!fsl_asoc_card_is_ac97(priv))
763 priv->dai_link[0].codecs->of_node = codec_np;
767 ret = of_property_read_u32(cpu_np, "cell-index", &idx);
770 "cannot get CPU index property\n");
774 priv->dai_link[0].codecs->name =
775 devm_kasprintf(&pdev->dev, GFP_KERNEL,
778 if (!priv->dai_link[0].codecs->name) {
784 priv->dai_link[0].platforms->of_node = cpu_np;
785 priv->dai_link[0].dai_fmt = priv->dai_fmt;
786 priv->card.num_links = 1;
789 /* DPCM DAI Links only if ASRC exsits */
790 priv->dai_link[1].cpus->of_node = asrc_np;
791 priv->dai_link[1].platforms->of_node = asrc_np;
792 priv->dai_link[2].codecs->dai_name = codec_dai_name;
793 priv->dai_link[2].codecs->of_node = codec_np;
794 priv->dai_link[2].codecs->name =
795 priv->dai_link[0].codecs->name;
796 priv->dai_link[2].cpus->of_node = cpu_np;
797 priv->dai_link[2].dai_fmt = priv->dai_fmt;
798 priv->card.num_links = 3;
800 ret = of_property_read_u32(asrc_np, "fsl,asrc-rate",
803 dev_err(&pdev->dev, "failed to get output rate\n");
808 ret = of_property_read_u32(asrc_np, "fsl,asrc-format",
811 /* Fallback to old binding; translate to asrc_format */
812 ret = of_property_read_u32(asrc_np, "fsl,asrc-width",
816 "failed to decide output format\n");
821 priv->asrc_format = SNDRV_PCM_FORMAT_S24_LE;
823 priv->asrc_format = SNDRV_PCM_FORMAT_S16_LE;
827 /* Finish card registering */
828 platform_set_drvdata(pdev, priv);
829 snd_soc_card_set_drvdata(&priv->card, priv);
831 ret = devm_snd_soc_register_card(&pdev->dev, &priv->card);
833 if (ret != -EPROBE_DEFER)
834 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret);
839 * Properties "hp-det-gpio" and "mic-det-gpio" are optional, and
840 * asoc_simple_init_jack uses these properties for creating
841 * Headphone Jack and Microphone Jack.
843 * The notifier is initialized in snd_soc_card_jack_new(), then
844 * snd_soc_jack_notifier_register can be called.
846 if (of_property_read_bool(np, "hp-det-gpio")) {
847 ret = asoc_simple_init_jack(&priv->card, &priv->hp_jack,
848 1, NULL, "Headphone Jack");
852 snd_soc_jack_notifier_register(&priv->hp_jack.jack, &hp_jack_nb);
855 if (of_property_read_bool(np, "mic-det-gpio")) {
856 ret = asoc_simple_init_jack(&priv->card, &priv->mic_jack,
857 0, NULL, "Mic Jack");
861 snd_soc_jack_notifier_register(&priv->mic_jack.jack, &mic_jack_nb);
865 of_node_put(asrc_np);
866 of_node_put(codec_np);
867 put_device(&cpu_pdev->dev);
874 static const struct of_device_id fsl_asoc_card_dt_ids[] = {
875 { .compatible = "fsl,imx-audio-ac97", },
876 { .compatible = "fsl,imx-audio-cs42888", },
877 { .compatible = "fsl,imx-audio-cs427x", },
878 { .compatible = "fsl,imx-audio-tlv320aic32x4", },
879 { .compatible = "fsl,imx-audio-sgtl5000", },
880 { .compatible = "fsl,imx-audio-wm8962", },
881 { .compatible = "fsl,imx-audio-wm8960", },
882 { .compatible = "fsl,imx-audio-mqs", },
883 { .compatible = "fsl,imx-audio-wm8524", },
884 { .compatible = "fsl,imx-audio-si476x", },
887 MODULE_DEVICE_TABLE(of, fsl_asoc_card_dt_ids);
889 static struct platform_driver fsl_asoc_card_driver = {
890 .probe = fsl_asoc_card_probe,
892 .name = "fsl-asoc-card",
893 .pm = &snd_soc_pm_ops,
894 .of_match_table = fsl_asoc_card_dt_ids,
897 module_platform_driver(fsl_asoc_card_driver);
899 MODULE_DESCRIPTION("Freescale Generic ASoC Sound Card driver with ASRC");
900 MODULE_AUTHOR("Nicolin Chen <nicoleotsuka@gmail.com>");
901 MODULE_ALIAS("platform:fsl-asoc-card");
902 MODULE_LICENSE("GPL");