1 // SPDX-License-Identifier: GPL-2.0-only
3 // Copyright(c) 2020 Intel Corporation
5 #include <linux/device.h>
6 #include <linux/kernel.h>
8 #include <sound/pcm_params.h>
10 #include <sound/soc-acpi.h>
11 #include <sound/soc-dai.h>
12 #include <sound/soc-dapm.h>
13 #include <sound/sof.h>
14 #include <uapi/sound/asound.h>
15 #include "../../codecs/rt1011.h"
16 #include "../../codecs/rt1015.h"
17 #include "../../codecs/rt1308.h"
18 #include "../common/soc-intel-quirks.h"
19 #include "sof_realtek_common.h"
22 * Common structures and functions
24 static const struct snd_kcontrol_new realtek_2spk_kcontrols
[] = {
25 SOC_DAPM_PIN_SWITCH("Left Spk"),
26 SOC_DAPM_PIN_SWITCH("Right Spk"),
30 static const struct snd_soc_dapm_widget realtek_2spk_widgets
[] = {
31 SND_SOC_DAPM_SPK("Left Spk", NULL
),
32 SND_SOC_DAPM_SPK("Right Spk", NULL
),
35 static const struct snd_kcontrol_new realtek_4spk_kcontrols
[] = {
36 SOC_DAPM_PIN_SWITCH("WL Ext Spk"),
37 SOC_DAPM_PIN_SWITCH("WR Ext Spk"),
38 SOC_DAPM_PIN_SWITCH("TL Ext Spk"),
39 SOC_DAPM_PIN_SWITCH("TR Ext Spk"),
42 static const struct snd_soc_dapm_widget realtek_4spk_widgets
[] = {
43 SND_SOC_DAPM_SPK("WL Ext Spk", NULL
),
44 SND_SOC_DAPM_SPK("WR Ext Spk", NULL
),
45 SND_SOC_DAPM_SPK("TL Ext Spk", NULL
),
46 SND_SOC_DAPM_SPK("TR Ext Spk", NULL
),
49 /* helper function to get the number of specific codec */
50 static unsigned int get_num_codecs(const char *hid
)
52 struct acpi_device
*adev
;
53 unsigned int dev_num
= 0;
55 for_each_acpi_dev_match(adev
, hid
, NULL
, -1)
64 static const struct snd_soc_dapm_route speaker_map_lr
[] = {
66 { "Left Spk", NULL
, "Left SPO" },
67 { "Right Spk", NULL
, "Right SPO" },
70 static const struct snd_soc_dapm_route rt1011_4spk_routes
[] = {
71 {"WL Ext Spk", NULL
, "WL SPO" },
72 {"WR Ext Spk", NULL
, "WR SPO" },
73 {"TL Ext Spk", NULL
, "TL SPO" },
74 {"TR Ext Spk", NULL
, "TR SPO" },
77 static struct snd_soc_codec_conf rt1011_2spk_codec_confs
[] = {
79 .dlc
= COMP_CODEC_CONF(RT1011_DEV0_NAME
),
80 .name_prefix
= "Left",
83 .dlc
= COMP_CODEC_CONF(RT1011_DEV1_NAME
),
84 .name_prefix
= "Right",
88 static struct snd_soc_codec_conf rt1011_4spk_codec_confs
[] = {
90 .dlc
= COMP_CODEC_CONF(RT1011_DEV0_NAME
),
94 .dlc
= COMP_CODEC_CONF(RT1011_DEV1_NAME
),
98 .dlc
= COMP_CODEC_CONF(RT1011_DEV2_NAME
),
102 .dlc
= COMP_CODEC_CONF(RT1011_DEV3_NAME
),
107 static struct snd_soc_dai_link_component rt1011_dai_link_components
[] = {
109 .name
= RT1011_DEV0_NAME
,
110 .dai_name
= RT1011_CODEC_DAI
,
113 .name
= RT1011_DEV1_NAME
,
114 .dai_name
= RT1011_CODEC_DAI
,
117 .name
= RT1011_DEV2_NAME
,
118 .dai_name
= RT1011_CODEC_DAI
,
121 .name
= RT1011_DEV3_NAME
,
122 .dai_name
= RT1011_CODEC_DAI
,
126 static const struct {
129 } rt1011_tdm_mask
[] = {
130 {.tx
= 0x4, .rx
= 0x1},
131 {.tx
= 0x8, .rx
= 0x2},
132 {.tx
= 0x1, .rx
= 0x1},
133 {.tx
= 0x2, .rx
= 0x2},
136 static int rt1011_hw_params(struct snd_pcm_substream
*substream
,
137 struct snd_pcm_hw_params
*params
)
139 struct snd_soc_pcm_runtime
*rtd
= snd_soc_substream_to_rtd(substream
);
140 struct snd_soc_dai
*codec_dai
;
141 int srate
, i
, ret
= 0;
143 srate
= params_rate(params
);
145 for_each_rtd_codec_dais(rtd
, i
, codec_dai
) {
146 /* 100 Fs to drive 24 bit data */
147 ret
= snd_soc_dai_set_pll(codec_dai
, 0, RT1011_PLL1_S_BCLK
,
148 100 * srate
, 256 * srate
);
150 dev_err(codec_dai
->dev
, "fail to set pll, ret %d\n",
155 ret
= snd_soc_dai_set_sysclk(codec_dai
, RT1011_FS_SYS_PRE_S_PLL1
,
156 256 * srate
, SND_SOC_CLOCK_IN
);
158 dev_err(codec_dai
->dev
, "fail to set sysclk, ret %d\n",
163 if (i
>= ARRAY_SIZE(rt1011_tdm_mask
)) {
164 dev_err(codec_dai
->dev
, "invalid codec index %d\n",
169 ret
= snd_soc_dai_set_tdm_slot(codec_dai
, rt1011_tdm_mask
[i
].tx
,
170 rt1011_tdm_mask
[i
].rx
, 4,
171 params_width(params
));
173 dev_err(codec_dai
->dev
, "fail to set tdm slot, ret %d\n",
182 static const struct snd_soc_ops rt1011_ops
= {
183 .hw_params
= rt1011_hw_params
,
186 static int rt1011_init(struct snd_soc_pcm_runtime
*rtd
)
188 struct snd_soc_card
*card
= rtd
->card
;
189 unsigned int num_codecs
= get_num_codecs(RT1011_ACPI_HID
);
192 switch (num_codecs
) {
194 if (!soc_intel_is_cml()) {
195 ret
= snd_soc_dapm_new_controls(&card
->dapm
, realtek_2spk_widgets
,
196 ARRAY_SIZE(realtek_2spk_widgets
));
198 dev_err(rtd
->dev
, "fail to add rt1011 widgets, ret %d\n",
203 ret
= snd_soc_add_card_controls(card
, realtek_2spk_kcontrols
,
204 ARRAY_SIZE(realtek_2spk_kcontrols
));
206 dev_err(rtd
->dev
, "fail to add rt1011 kcontrols, ret %d\n",
211 ret
= snd_soc_dapm_add_routes(&card
->dapm
, speaker_map_lr
,
212 ARRAY_SIZE(speaker_map_lr
));
214 dev_err(rtd
->dev
, "fail to add rt1011 routes, ret %d\n",
223 * register speaker widgets "WL Ext Spk" and "WR Ext Spk" to
224 * keep backward compatible with cml devices
228 ret
= snd_soc_dapm_new_controls(&card
->dapm
, realtek_4spk_widgets
,
231 dev_err(rtd
->dev
, "fail to add rt1011 widgets, ret %d\n",
236 ret
= snd_soc_add_card_controls(card
, realtek_4spk_kcontrols
,
239 dev_err(rtd
->dev
, "fail to add rt1011 controls, ret %d\n",
244 ret
= snd_soc_dapm_add_routes(&card
->dapm
, rt1011_4spk_routes
,
247 dev_err(rtd
->dev
, "fail to add rt1011 routes, ret %d\n",
253 dev_err(rtd
->dev
, "rt1011: invalid num_codecs %d\n", num_codecs
);
260 void sof_rt1011_dai_link(struct device
*dev
, struct snd_soc_dai_link
*link
)
262 unsigned int num_codecs
= get_num_codecs(RT1011_ACPI_HID
);
264 link
->codecs
= rt1011_dai_link_components
;
266 switch (num_codecs
) {
269 link
->num_codecs
= num_codecs
;
272 dev_err(dev
, "rt1011: invalid num_codecs %d\n", num_codecs
);
276 link
->init
= rt1011_init
;
277 link
->ops
= &rt1011_ops
;
279 EXPORT_SYMBOL_NS(sof_rt1011_dai_link
, "SND_SOC_INTEL_SOF_REALTEK_COMMON");
281 void sof_rt1011_codec_conf(struct device
*dev
, struct snd_soc_card
*card
)
283 unsigned int num_codecs
= get_num_codecs(RT1011_ACPI_HID
);
285 switch (num_codecs
) {
287 if (soc_intel_is_cml()) {
289 * use name prefix 'WL' and 'WR' for speaker widgets to
290 * keep backward compatible with cml devices
292 card
->codec_conf
= rt1011_4spk_codec_confs
;
294 card
->codec_conf
= rt1011_2spk_codec_confs
;
297 card
->num_configs
= num_codecs
;
300 card
->codec_conf
= rt1011_4spk_codec_confs
;
301 card
->num_configs
= ARRAY_SIZE(rt1011_4spk_codec_confs
);
304 dev_err(dev
, "rt1011: invalid num_codecs %d\n", num_codecs
);
309 EXPORT_SYMBOL_NS(sof_rt1011_codec_conf
, "SND_SOC_INTEL_SOF_REALTEK_COMMON");
312 * rt1015: i2c mode driver for ALC1015 and ALC1015Q
313 * rt1015p: auto-mode driver for ALC1015, ALC1015Q, and ALC1015Q-VB
315 * For stereo output, there are always two amplifiers on the board.
316 * However, the ACPI implements only one device instance (UID=0) if they
317 * are sharing the same enable pin. This is the case of rt1015p.
319 static const struct snd_soc_dapm_route rt1015p_dapm_routes
[] = {
321 { "Left Spk", NULL
, "Speaker" },
322 { "Right Spk", NULL
, "Speaker" },
325 static struct snd_soc_dai_link_component rt1015p_dai_link_components
[] = {
327 .name
= RT1015P_DEV0_NAME
,
328 .dai_name
= RT1015P_CODEC_DAI
,
332 static int rt1015p_hw_params(struct snd_pcm_substream
*substream
,
333 struct snd_pcm_hw_params
*params
)
335 /* reserved for debugging purpose */
340 static const struct snd_soc_ops rt1015p_ops
= {
341 .hw_params
= rt1015p_hw_params
,
344 static int rt1015p_init(struct snd_soc_pcm_runtime
*rtd
)
346 struct snd_soc_card
*card
= rtd
->card
;
349 ret
= snd_soc_dapm_new_controls(&card
->dapm
, realtek_2spk_widgets
,
350 ARRAY_SIZE(realtek_2spk_widgets
));
352 dev_err(rtd
->dev
, "fail to add rt1015p widgets, ret %d\n", ret
);
356 ret
= snd_soc_add_card_controls(card
, realtek_2spk_kcontrols
,
357 ARRAY_SIZE(realtek_2spk_kcontrols
));
359 dev_err(rtd
->dev
, "fail to add rt1015p kcontrols, ret %d\n", ret
);
363 ret
= snd_soc_dapm_add_routes(&card
->dapm
, rt1015p_dapm_routes
,
364 ARRAY_SIZE(rt1015p_dapm_routes
));
366 dev_err(rtd
->dev
, "Speaker map addition failed: %d\n", ret
);
370 void sof_rt1015p_dai_link(struct snd_soc_dai_link
*link
)
372 link
->codecs
= rt1015p_dai_link_components
;
373 link
->num_codecs
= ARRAY_SIZE(rt1015p_dai_link_components
);
374 link
->init
= rt1015p_init
;
375 link
->ops
= &rt1015p_ops
;
377 EXPORT_SYMBOL_NS(sof_rt1015p_dai_link
, "SND_SOC_INTEL_SOF_REALTEK_COMMON");
379 void sof_rt1015p_codec_conf(struct snd_soc_card
*card
)
382 EXPORT_SYMBOL_NS(sof_rt1015p_codec_conf
, "SND_SOC_INTEL_SOF_REALTEK_COMMON");
385 * RT1015 audio amplifier
388 static const struct {
391 } rt1015_tdm_mask
[] = {
392 {.tx
= 0x0, .rx
= 0x1},
393 {.tx
= 0x0, .rx
= 0x2},
396 static int rt1015_hw_params(struct snd_pcm_substream
*substream
,
397 struct snd_pcm_hw_params
*params
)
399 struct snd_soc_pcm_runtime
*rtd
= snd_soc_substream_to_rtd(substream
);
400 struct snd_soc_dai_link
*dai_link
= rtd
->dai_link
;
401 struct snd_soc_dai
*codec_dai
;
405 clk_freq
= sof_dai_get_bclk(rtd
);
408 dev_err(rtd
->dev
, "fail to get bclk freq, ret %d\n", clk_freq
);
412 for_each_rtd_codec_dais(rtd
, i
, codec_dai
) {
413 ret
= snd_soc_dai_set_pll(codec_dai
, 0, RT1015_PLL_S_BCLK
,
415 params_rate(params
) * 256);
417 dev_err(codec_dai
->dev
, "fail to set pll, ret %d\n",
422 ret
= snd_soc_dai_set_sysclk(codec_dai
, RT1015_SCLK_S_PLL
,
423 params_rate(params
) * 256,
426 dev_err(codec_dai
->dev
, "fail to set sysclk, ret %d\n",
431 switch (dai_link
->dai_fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
432 case SND_SOC_DAIFMT_DSP_A
:
433 case SND_SOC_DAIFMT_DSP_B
:
435 ret
= snd_soc_dai_set_tdm_slot(codec_dai
,
436 rt1015_tdm_mask
[i
].tx
,
437 rt1015_tdm_mask
[i
].rx
,
439 params_width(params
));
441 dev_err(codec_dai
->dev
, "fail to set tdm slot, ret %d\n",
447 dev_dbg(codec_dai
->dev
, "codec is in I2S mode\n");
455 static const struct snd_soc_ops rt1015_ops
= {
456 .hw_params
= rt1015_hw_params
,
459 static struct snd_soc_codec_conf rt1015_amp_conf
[] = {
461 .dlc
= COMP_CODEC_CONF(RT1015_DEV0_NAME
),
462 .name_prefix
= "Left",
465 .dlc
= COMP_CODEC_CONF(RT1015_DEV1_NAME
),
466 .name_prefix
= "Right",
470 static struct snd_soc_dai_link_component rt1015_components
[] = {
472 .name
= RT1015_DEV0_NAME
,
473 .dai_name
= RT1015_CODEC_DAI
,
476 .name
= RT1015_DEV1_NAME
,
477 .dai_name
= RT1015_CODEC_DAI
,
481 static int speaker_codec_init_lr(struct snd_soc_pcm_runtime
*rtd
)
483 struct snd_soc_card
*card
= rtd
->card
;
484 unsigned int num_codecs
= get_num_codecs(RT1015_ACPI_HID
);
487 switch (num_codecs
) {
489 ret
= snd_soc_dapm_new_controls(&card
->dapm
, realtek_2spk_widgets
,
490 ARRAY_SIZE(realtek_2spk_widgets
));
492 dev_err(rtd
->dev
, "fail to add rt1015 widgets, ret %d\n",
497 ret
= snd_soc_add_card_controls(card
, realtek_2spk_kcontrols
,
498 ARRAY_SIZE(realtek_2spk_kcontrols
));
500 dev_err(rtd
->dev
, "fail to add rt1015 kcontrols, ret %d\n",
505 ret
= snd_soc_dapm_add_routes(&rtd
->card
->dapm
, speaker_map_lr
,
506 ARRAY_SIZE(speaker_map_lr
));
508 dev_err(rtd
->dev
, "fail to add rt1015 routes, ret %d\n",
514 dev_err(rtd
->dev
, "rt1015: invalid num_codecs %d\n", num_codecs
);
521 void sof_rt1015_codec_conf(struct snd_soc_card
*card
)
523 card
->codec_conf
= rt1015_amp_conf
;
524 card
->num_configs
= ARRAY_SIZE(rt1015_amp_conf
);
526 EXPORT_SYMBOL_NS(sof_rt1015_codec_conf
, "SND_SOC_INTEL_SOF_REALTEK_COMMON");
528 void sof_rt1015_dai_link(struct snd_soc_dai_link
*link
)
530 link
->codecs
= rt1015_components
;
531 link
->num_codecs
= ARRAY_SIZE(rt1015_components
);
532 link
->init
= speaker_codec_init_lr
;
533 link
->ops
= &rt1015_ops
;
535 EXPORT_SYMBOL_NS(sof_rt1015_dai_link
, "SND_SOC_INTEL_SOF_REALTEK_COMMON");
538 * RT1308 audio amplifier
540 static const struct snd_kcontrol_new rt1308_kcontrols
[] = {
541 SOC_DAPM_PIN_SWITCH("Speakers"),
544 static const struct snd_soc_dapm_widget rt1308_dapm_widgets
[] = {
545 SND_SOC_DAPM_SPK("Speakers", NULL
),
548 static const struct snd_soc_dapm_route rt1308_dapm_routes
[] = {
550 {"Speakers", NULL
, "SPOL"},
551 {"Speakers", NULL
, "SPOR"},
554 static struct snd_soc_dai_link_component rt1308_components
[] = {
556 .name
= RT1308_DEV0_NAME
,
557 .dai_name
= RT1308_CODEC_DAI
,
561 static int rt1308_init(struct snd_soc_pcm_runtime
*rtd
)
563 struct snd_soc_card
*card
= rtd
->card
;
566 ret
= snd_soc_dapm_new_controls(&card
->dapm
, rt1308_dapm_widgets
,
567 ARRAY_SIZE(rt1308_dapm_widgets
));
569 dev_err(rtd
->dev
, "fail to add dapm controls, ret %d\n", ret
);
573 ret
= snd_soc_add_card_controls(card
, rt1308_kcontrols
,
574 ARRAY_SIZE(rt1308_kcontrols
));
576 dev_err(rtd
->dev
, "fail to add card controls, ret %d\n", ret
);
580 ret
= snd_soc_dapm_add_routes(&card
->dapm
, rt1308_dapm_routes
,
581 ARRAY_SIZE(rt1308_dapm_routes
));
584 dev_err(rtd
->dev
, "fail to add dapm routes, ret %d\n", ret
);
589 static int rt1308_hw_params(struct snd_pcm_substream
*substream
,
590 struct snd_pcm_hw_params
*params
)
592 struct snd_soc_pcm_runtime
*rtd
= snd_soc_substream_to_rtd(substream
);
593 struct snd_soc_card
*card
= rtd
->card
;
594 struct snd_soc_dai
*codec_dai
= snd_soc_rtd_to_codec(rtd
, 0);
595 int clk_id
, clk_freq
, pll_out
;
598 clk_id
= RT1308_PLL_S_MCLK
;
599 /* get the tplg configured mclk. */
600 clk_freq
= sof_dai_get_mclk(rtd
);
602 pll_out
= params_rate(params
) * 512;
605 ret
= snd_soc_dai_set_pll(codec_dai
, 0, clk_id
, clk_freq
, pll_out
);
607 dev_err(card
->dev
, "Failed to set RT1308 PLL: %d\n", ret
);
611 /* Set rt1308 sysclk */
612 ret
= snd_soc_dai_set_sysclk(codec_dai
, RT1308_FS_SYS_S_PLL
, pll_out
,
615 dev_err(card
->dev
, "Failed to set RT1308 SYSCLK: %d\n", ret
);
620 static const struct snd_soc_ops rt1308_ops
= {
621 .hw_params
= rt1308_hw_params
,
624 void sof_rt1308_dai_link(struct snd_soc_dai_link
*link
)
626 link
->codecs
= rt1308_components
;
627 link
->num_codecs
= ARRAY_SIZE(rt1308_components
);
628 link
->init
= rt1308_init
;
629 link
->ops
= &rt1308_ops
;
631 EXPORT_SYMBOL_NS(sof_rt1308_dai_link
, "SND_SOC_INTEL_SOF_REALTEK_COMMON");
634 * 2-amp Configuration for RT1019
637 static const struct snd_soc_dapm_route rt1019p_dapm_routes
[] = {
639 { "Left Spk", NULL
, "Speaker" },
640 { "Right Spk", NULL
, "Speaker" },
643 static struct snd_soc_dai_link_component rt1019p_components
[] = {
645 .name
= RT1019P_DEV0_NAME
,
646 .dai_name
= RT1019P_CODEC_DAI
,
650 static int rt1019p_init(struct snd_soc_pcm_runtime
*rtd
)
652 struct snd_soc_card
*card
= rtd
->card
;
655 ret
= snd_soc_dapm_new_controls(&card
->dapm
, realtek_2spk_widgets
,
656 ARRAY_SIZE(realtek_2spk_widgets
));
658 dev_err(rtd
->dev
, "fail to add rt1019p widgets, ret %d\n", ret
);
662 ret
= snd_soc_add_card_controls(card
, realtek_2spk_kcontrols
,
663 ARRAY_SIZE(realtek_2spk_kcontrols
));
665 dev_err(rtd
->dev
, "fail to add rt1019p kcontrols, ret %d\n", ret
);
669 ret
= snd_soc_dapm_add_routes(&card
->dapm
, rt1019p_dapm_routes
,
670 ARRAY_SIZE(rt1019p_dapm_routes
));
672 dev_err(rtd
->dev
, "Speaker map addition failed: %d\n", ret
);
678 void sof_rt1019p_dai_link(struct snd_soc_dai_link
*link
)
680 link
->codecs
= rt1019p_components
;
681 link
->num_codecs
= ARRAY_SIZE(rt1019p_components
);
682 link
->init
= rt1019p_init
;
684 EXPORT_SYMBOL_NS(sof_rt1019p_dai_link
, "SND_SOC_INTEL_SOF_REALTEK_COMMON");
686 MODULE_DESCRIPTION("ASoC Intel SOF Realtek helpers");
687 MODULE_LICENSE("GPL");