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"
31 #include "../codecs/wm8904.h"
33 #define DRIVER_NAME "fsl-asoc-card"
35 #define CS427x_SYSCLK_MCLK 0
40 /* Default DAI format without Master and Slave flag */
41 #define DAI_FMT_BASE (SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF)
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()
54 unsigned long mclk_freq
;
55 unsigned long free_freq
;
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
72 unsigned long sysclk_freq
[2];
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
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
;
107 snd_pcm_format_t sample_format
;
109 snd_pcm_format_t asrc_format
;
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
;
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
))
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");
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,
205 cpu_priv
->slot_width
);
206 if (ret
&& ret
!= -ENOTSUPP
) {
207 dev_err(dev
, "failed to set TDM slot for cpu dai\n");
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;
220 pll_out
= priv
->sample_rate
* 256;
222 ret
= snd_soc_dai_set_pll(codec_dai
,
225 codec_priv
->mclk_freq
, pll_out
);
227 dev_err(dev
, "failed to start FLL: %d\n", ret
);
231 ret
= snd_soc_dai_set_sysclk(codec_dai
,
233 pll_out
, SND_SOC_CLOCK_IN
);
235 if (ret
&& ret
!= -ENOTSUPP
) {
236 dev_err(dev
, "failed to set SYSCLK: %d\n", ret
);
245 priv
->streams
&= ~BIT(substream
->stream
);
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
;
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
,
268 codec_priv
->free_freq
,
271 dev_err(dev
, "failed to switch away from FLL: %d\n", 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
);
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
);
304 snd_mask_set_format(mask
, priv
->asrc_format
);
309 static const struct snd_soc_dai_link fsl_asoc_card_dai
[] = {
310 /* Default ASoC DAI Link*/
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",
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
,
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
;
339 ret
= of_property_read_u32(np
, "mux-int-port", &int_port
);
341 dev_err(dev
, "mux-int-port missing or invalid\n");
344 ret
= of_property_read_u32(np
, "mux-ext-port", &ext_port
);
346 dev_err(dev
, "mux-ext-port missing or invalid\n");
351 * The port numbering in the hardware manual starts at 1, while
352 * the AUDMUX API expects it starts at 0.
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
;
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
;
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
;
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
;
404 if (!fsl_asoc_card_is_ac97(priv
))
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
)) {
420 IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port
);
422 ret
= imx_audmux_v2_configure_port(int_port
, 0,
425 dev_err(dev
, "audmux internal port setup failed\n");
430 ret
= imx_audmux_v2_configure_port(int_port
, int_ptcr
,
431 IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port
));
433 dev_err(dev
, "audmux internal port setup failed\n");
437 if (!fsl_asoc_card_is_ac97(priv
)) {
439 IMX_AUDMUX_V2_PDCR_RXDSEL(int_port
);
441 ret
= imx_audmux_v2_configure_port(ext_port
, 0,
444 dev_err(dev
, "audmux external port setup failed\n");
449 ret
= imx_audmux_v2_configure_port(ext_port
, ext_ptcr
,
450 IMX_AUDMUX_V2_PDCR_RXDSEL(int_port
));
452 dev_err(dev
, "audmux external port setup failed\n");
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");
472 priv
->dai_link
[0].playback_only
= true;
473 priv
->dai_link
[0].capture_only
= true;
475 for (int i
= 0; i
< 2; i
++) {
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
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");
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
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;
528 static int hp_jack_event(struct notifier_block
*nb
, unsigned long event
,
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");
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
,
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");
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
;
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
);
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__
);
600 if (!IS_ERR_OR_NULL(codec_priv
->mclk
))
601 clk_prepare_enable(codec_priv
->mclk
);
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];
627 priv
= devm_kzalloc(&pdev
->dev
, sizeof(*priv
), GFP_KERNEL
);
633 cpu_np
= of_parse_phandle(np
, "audio-cpu", 0);
634 /* Give a chance to old DT bindings */
636 cpu_np
= of_parse_phandle(np
, "ssi-controller", 0);
638 cpu_np
= of_parse_phandle(np
, "spdif-controller", 0);
640 dev_err(&pdev
->dev
, "CPU phandle missing or invalid\n");
645 cpu_pdev
= of_find_device_by_node(cpu_np
);
647 dev_err(&pdev
->dev
, "failed to find CPU DAI device\n");
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
]);
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
]);
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);
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
);
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
);
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
;
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
);
835 dev_err(&pdev
->dev
, "unknown Device Tree compatible\n");
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
;
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");
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
);
895 dev_err(&pdev
->dev
, "failed to init audmux\n");
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
);
905 } else if (PTR_ERR(esai_clk
) == -EPROBE_DEFER
) {
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");
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 */
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");
938 dev_err(&pdev
->dev
, "failed to parse audio-routing: %d\n", ret
);
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
];
960 ret
= of_property_read_u32(cpu_np
, "cell-index", &idx
);
963 "cannot get CPU index property\n");
967 priv
->dai_link
[0].codecs
[0].name
=
968 devm_kasprintf(&pdev
->dev
, GFP_KERNEL
,
971 if (!priv
->dai_link
[0].codecs
[0].name
) {
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;
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",
997 dev_err(&pdev
->dev
, "failed to get output rate\n");
1002 ret
= of_property_read_u32(asrc_np
, "fsl,asrc-format", &asrc_fmt
);
1003 priv
->asrc_format
= (__force snd_pcm_format_t
)asrc_fmt
;
1005 /* Fallback to old binding; translate to asrc_format */
1006 ret
= of_property_read_u32(asrc_np
, "fsl,asrc-width",
1010 "failed to decide output format\n");
1015 priv
->asrc_format
= SNDRV_PCM_FORMAT_S24_LE
;
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
);
1027 dev_err_probe(&pdev
->dev
, ret
, "snd_soc_register_card failed\n");
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");
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");
1056 snd_soc_jack_notifier_register(&priv
->mic_jack
.jack
, &mic_jack_nb
);
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
);
1065 of_node_put(cpu_np
);
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
,
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");