1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright(c) 2019-2020 Intel Corporation.
5 * Intel SOF Machine Driver with Realtek rt5682 Codec
6 * and speaker codec MAX98357A or RT1015.
9 #include <linux/input.h>
10 #include <linux/module.h>
11 #include <linux/platform_device.h>
12 #include <linux/clk.h>
13 #include <linux/dmi.h>
14 #include <sound/core.h>
15 #include <sound/jack.h>
16 #include <sound/pcm.h>
17 #include <sound/pcm_params.h>
18 #include <sound/soc.h>
19 #include <sound/sof.h>
20 #include <sound/rt5682.h>
21 #include <sound/rt5682s.h>
22 #include <sound/soc-acpi.h>
23 #include "../../codecs/rt5682.h"
24 #include "../../codecs/rt5682s.h"
25 #include "../../codecs/hdac_hdmi.h"
26 #include "../common/soc-intel-quirks.h"
27 #include "hda_dsp_common.h"
28 #include "sof_maxim_common.h"
29 #include "sof_realtek_common.h"
33 #define SOF_RT5682_SSP_CODEC(quirk) ((quirk) & GENMASK(2, 0))
34 #define SOF_RT5682_SSP_CODEC_MASK (GENMASK(2, 0))
35 #define SOF_RT5682_MCLK_EN BIT(3)
36 #define SOF_RT5682_MCLK_24MHZ BIT(4)
37 #define SOF_SPEAKER_AMP_PRESENT BIT(5)
38 #define SOF_RT5682_SSP_AMP_SHIFT 6
39 #define SOF_RT5682_SSP_AMP_MASK (GENMASK(8, 6))
40 #define SOF_RT5682_SSP_AMP(quirk) \
41 (((quirk) << SOF_RT5682_SSP_AMP_SHIFT) & SOF_RT5682_SSP_AMP_MASK)
42 #define SOF_RT5682_MCLK_BYTCHT_EN BIT(9)
43 #define SOF_RT5682_NUM_HDMIDEV_SHIFT 10
44 #define SOF_RT5682_NUM_HDMIDEV_MASK (GENMASK(12, 10))
45 #define SOF_RT5682_NUM_HDMIDEV(quirk) \
46 ((quirk << SOF_RT5682_NUM_HDMIDEV_SHIFT) & SOF_RT5682_NUM_HDMIDEV_MASK)
47 #define SOF_RT1011_SPEAKER_AMP_PRESENT BIT(13)
48 #define SOF_RT1015_SPEAKER_AMP_PRESENT BIT(14)
49 #define SOF_RT1015P_SPEAKER_AMP_PRESENT BIT(16)
50 #define SOF_MAX98373_SPEAKER_AMP_PRESENT BIT(17)
51 #define SOF_MAX98360A_SPEAKER_AMP_PRESENT BIT(18)
53 /* BT audio offload: reserve 3 bits for future */
54 #define SOF_BT_OFFLOAD_SSP_SHIFT 19
55 #define SOF_BT_OFFLOAD_SSP_MASK (GENMASK(21, 19))
56 #define SOF_BT_OFFLOAD_SSP(quirk) \
57 (((quirk) << SOF_BT_OFFLOAD_SSP_SHIFT) & SOF_BT_OFFLOAD_SSP_MASK)
58 #define SOF_SSP_BT_OFFLOAD_PRESENT BIT(22)
59 #define SOF_RT5682S_HEADPHONE_CODEC_PRESENT BIT(23)
60 #define SOF_MAX98390_SPEAKER_AMP_PRESENT BIT(24)
61 #define SOF_MAX98390_TWEETER_SPEAKER_PRESENT BIT(25)
62 #define SOF_RT1019_SPEAKER_AMP_PRESENT BIT(26)
65 /* Default: MCLK on, MCLK 19.2M, SSP0 */
66 static unsigned long sof_rt5682_quirk = SOF_RT5682_MCLK_EN |
67 SOF_RT5682_SSP_CODEC(0);
69 static int is_legacy_cpu;
72 struct list_head head;
73 struct snd_soc_dai *codec_dai;
74 struct snd_soc_jack hdmi_jack;
78 struct sof_card_private {
80 struct snd_soc_jack sof_headset;
81 struct list_head hdmi_pcm_list;
82 bool common_hdmi_codec_drv;
86 static int sof_rt5682_quirk_cb(const struct dmi_system_id *id)
88 sof_rt5682_quirk = (unsigned long)id->driver_data;
92 static const struct dmi_system_id sof_rt5682_quirk_table[] = {
94 .callback = sof_rt5682_quirk_cb,
96 DMI_MATCH(DMI_SYS_VENDOR, "Circuitco"),
97 DMI_MATCH(DMI_PRODUCT_NAME, "Minnowboard Max"),
99 .driver_data = (void *)(SOF_RT5682_SSP_CODEC(2)),
102 .callback = sof_rt5682_quirk_cb,
104 DMI_MATCH(DMI_SYS_VENDOR, "AAEON"),
105 DMI_MATCH(DMI_PRODUCT_NAME, "UP-CHT01"),
107 .driver_data = (void *)(SOF_RT5682_SSP_CODEC(2)),
110 .callback = sof_rt5682_quirk_cb,
112 DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
113 DMI_MATCH(DMI_PRODUCT_NAME, "WhiskeyLake Client"),
115 .driver_data = (void *)(SOF_RT5682_MCLK_EN |
116 SOF_RT5682_MCLK_24MHZ |
117 SOF_RT5682_SSP_CODEC(1)),
121 * Dooly is hatch family but using rt1015 amp so it
122 * requires a quirk before "Google_Hatch".
124 .callback = sof_rt5682_quirk_cb,
126 DMI_MATCH(DMI_SYS_VENDOR, "HP"),
127 DMI_MATCH(DMI_PRODUCT_NAME, "Dooly"),
129 .driver_data = (void *)(SOF_RT5682_MCLK_EN |
130 SOF_RT5682_MCLK_24MHZ |
131 SOF_RT5682_SSP_CODEC(0) |
132 SOF_SPEAKER_AMP_PRESENT |
133 SOF_RT1015_SPEAKER_AMP_PRESENT |
134 SOF_RT5682_SSP_AMP(1)),
137 .callback = sof_rt5682_quirk_cb,
139 DMI_MATCH(DMI_PRODUCT_FAMILY, "Google_Hatch"),
141 .driver_data = (void *)(SOF_RT5682_MCLK_EN |
142 SOF_RT5682_MCLK_24MHZ |
143 SOF_RT5682_SSP_CODEC(0) |
144 SOF_SPEAKER_AMP_PRESENT |
145 SOF_RT5682_SSP_AMP(1)),
148 .callback = sof_rt5682_quirk_cb,
150 DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
151 DMI_MATCH(DMI_PRODUCT_NAME, "Ice Lake Client"),
153 .driver_data = (void *)(SOF_RT5682_MCLK_EN |
154 SOF_RT5682_SSP_CODEC(0)),
157 .callback = sof_rt5682_quirk_cb,
159 DMI_MATCH(DMI_PRODUCT_FAMILY, "Google_Volteer"),
160 DMI_MATCH(DMI_OEM_STRING, "AUDIO-MAX98373_ALC5682I_I2S_UP4"),
162 .driver_data = (void *)(SOF_RT5682_MCLK_EN |
163 SOF_RT5682_SSP_CODEC(0) |
164 SOF_SPEAKER_AMP_PRESENT |
165 SOF_MAX98373_SPEAKER_AMP_PRESENT |
166 SOF_RT5682_SSP_AMP(2) |
167 SOF_RT5682_NUM_HDMIDEV(4)),
170 .callback = sof_rt5682_quirk_cb,
172 DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
173 DMI_MATCH(DMI_PRODUCT_NAME, "Alder Lake Client Platform"),
174 DMI_MATCH(DMI_OEM_STRING, "AUDIO-ADL_MAX98373_ALC5682I_I2S"),
176 .driver_data = (void *)(SOF_RT5682_MCLK_EN |
177 SOF_RT5682_SSP_CODEC(0) |
178 SOF_SPEAKER_AMP_PRESENT |
179 SOF_MAX98373_SPEAKER_AMP_PRESENT |
180 SOF_RT5682_SSP_AMP(2) |
181 SOF_RT5682_NUM_HDMIDEV(4)),
184 .callback = sof_rt5682_quirk_cb,
186 DMI_MATCH(DMI_PRODUCT_FAMILY, "Google_Brya"),
187 DMI_MATCH(DMI_OEM_STRING, "AUDIO-MAX98390_ALC5682I_I2S"),
189 .driver_data = (void *)(SOF_RT5682_MCLK_EN |
190 SOF_RT5682_SSP_CODEC(0) |
191 SOF_SPEAKER_AMP_PRESENT |
192 SOF_MAX98390_SPEAKER_AMP_PRESENT |
193 SOF_RT5682_SSP_AMP(2) |
194 SOF_RT5682_NUM_HDMIDEV(4)),
197 .callback = sof_rt5682_quirk_cb,
199 DMI_MATCH(DMI_PRODUCT_FAMILY, "Google_Brya"),
200 DMI_MATCH(DMI_OEM_STRING, "AUDIO-MAX98390_ALC5682I_I2S_4SPK"),
202 .driver_data = (void *)(SOF_RT5682_MCLK_EN |
203 SOF_RT5682_SSP_CODEC(0) |
204 SOF_SPEAKER_AMP_PRESENT |
205 SOF_MAX98390_SPEAKER_AMP_PRESENT |
206 SOF_MAX98390_TWEETER_SPEAKER_PRESENT |
207 SOF_RT5682_SSP_AMP(1) |
208 SOF_RT5682_NUM_HDMIDEV(4) |
209 SOF_BT_OFFLOAD_SSP(2) |
210 SOF_SSP_BT_OFFLOAD_PRESENT),
214 .callback = sof_rt5682_quirk_cb,
216 DMI_MATCH(DMI_PRODUCT_FAMILY, "Google_Brya"),
217 DMI_MATCH(DMI_OEM_STRING, "AUDIO-MAX98360_ALC5682I_I2S_AMP_SSP2"),
219 .driver_data = (void *)(SOF_RT5682_MCLK_EN |
220 SOF_RT5682_SSP_CODEC(0) |
221 SOF_SPEAKER_AMP_PRESENT |
222 SOF_MAX98360A_SPEAKER_AMP_PRESENT |
223 SOF_RT5682_SSP_AMP(2) |
224 SOF_RT5682_NUM_HDMIDEV(4)),
227 .callback = sof_rt5682_quirk_cb,
229 DMI_MATCH(DMI_PRODUCT_FAMILY, "Google_Rex"),
230 DMI_MATCH(DMI_OEM_STRING, "AUDIO-MAX98360_ALC5682I_I2S"),
232 .driver_data = (void *)(SOF_RT5682_MCLK_EN |
233 SOF_RT5682_SSP_CODEC(2) |
234 SOF_SPEAKER_AMP_PRESENT |
235 SOF_MAX98360A_SPEAKER_AMP_PRESENT |
236 SOF_RT5682_SSP_AMP(0) |
237 SOF_RT5682_NUM_HDMIDEV(4) |
238 SOF_BT_OFFLOAD_SSP(1) |
239 SOF_SSP_BT_OFFLOAD_PRESENT
243 .callback = sof_rt5682_quirk_cb,
245 DMI_MATCH(DMI_PRODUCT_FAMILY, "Google_Rex"),
246 DMI_MATCH(DMI_OEM_STRING, "AUDIO-ALC1019_ALC5682I_I2S"),
248 .driver_data = (void *)(SOF_RT5682_MCLK_EN |
249 SOF_RT5682_SSP_CODEC(2) |
250 SOF_SPEAKER_AMP_PRESENT |
251 SOF_RT1019_SPEAKER_AMP_PRESENT |
252 SOF_RT5682_SSP_AMP(0) |
253 SOF_RT5682_NUM_HDMIDEV(3)
257 .callback = sof_rt5682_quirk_cb,
259 DMI_MATCH(DMI_PRODUCT_FAMILY, "Google_Rex"),
261 .driver_data = (void *)(SOF_RT5682_MCLK_EN |
262 SOF_RT5682_SSP_CODEC(2) |
263 SOF_SPEAKER_AMP_PRESENT |
264 SOF_RT5682_SSP_AMP(0) |
265 SOF_RT5682_NUM_HDMIDEV(4) |
266 SOF_BT_OFFLOAD_SSP(1) |
267 SOF_SSP_BT_OFFLOAD_PRESENT
273 static int sof_hdmi_init(struct snd_soc_pcm_runtime *rtd)
275 struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
276 struct snd_soc_dai *dai = asoc_rtd_to_codec(rtd, 0);
277 struct sof_hdmi_pcm *pcm;
279 pcm = devm_kzalloc(rtd->card->dev, sizeof(*pcm), GFP_KERNEL);
283 /* dai_link id is 1:1 mapped to the PCM device */
284 pcm->device = rtd->dai_link->id;
285 pcm->codec_dai = dai;
287 list_add_tail(&pcm->head, &ctx->hdmi_pcm_list);
292 static struct snd_soc_jack_pin jack_pins[] = {
294 .pin = "Headphone Jack",
295 .mask = SND_JACK_HEADPHONE,
298 .pin = "Headset Mic",
299 .mask = SND_JACK_MICROPHONE,
303 static int sof_rt5682_codec_init(struct snd_soc_pcm_runtime *rtd)
305 struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
306 struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
307 struct snd_soc_jack *jack;
310 /* need to enable ASRC function for 24MHz mclk rate */
311 if ((sof_rt5682_quirk & SOF_RT5682_MCLK_EN) &&
312 (sof_rt5682_quirk & SOF_RT5682_MCLK_24MHZ)) {
313 if (sof_rt5682_quirk & SOF_RT5682S_HEADPHONE_CODEC_PRESENT)
314 rt5682s_sel_asrc_clk_src(component,
315 RT5682S_DA_STEREO1_FILTER |
316 RT5682S_AD_STEREO1_FILTER,
317 RT5682S_CLK_SEL_I2S1_ASRC);
319 rt5682_sel_asrc_clk_src(component,
320 RT5682_DA_STEREO1_FILTER |
321 RT5682_AD_STEREO1_FILTER,
322 RT5682_CLK_SEL_I2S1_ASRC);
325 if (sof_rt5682_quirk & SOF_RT5682_MCLK_BYTCHT_EN) {
327 * The firmware might enable the clock at
328 * boot (this information may or may not
329 * be reflected in the enable clock register).
330 * To change the rate we must disable the clock
331 * first to cover these cases. Due to common
332 * clock framework restrictions that do not allow
333 * to disable a clock that has not been enabled,
334 * we need to enable the clock first.
336 ret = clk_prepare_enable(ctx->mclk);
338 clk_disable_unprepare(ctx->mclk);
340 ret = clk_set_rate(ctx->mclk, 19200000);
343 dev_err(rtd->dev, "unable to set MCLK rate\n");
347 * Headset buttons map to the google Reference headset.
348 * These can be configured by userspace.
350 ret = snd_soc_card_jack_new_pins(rtd->card, "Headset Jack",
351 SND_JACK_HEADSET | SND_JACK_BTN_0 |
352 SND_JACK_BTN_1 | SND_JACK_BTN_2 |
356 ARRAY_SIZE(jack_pins));
358 dev_err(rtd->dev, "Headset Jack creation failed: %d\n", ret);
362 jack = &ctx->sof_headset;
364 snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
365 snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
366 snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
367 snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
368 ret = snd_soc_component_set_jack(component, jack, NULL);
371 dev_err(rtd->dev, "Headset Jack call-back failed: %d\n", ret);
378 static void sof_rt5682_codec_exit(struct snd_soc_pcm_runtime *rtd)
380 struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
382 snd_soc_component_set_jack(component, NULL, NULL);
385 static int sof_rt5682_hw_params(struct snd_pcm_substream *substream,
386 struct snd_pcm_hw_params *params)
388 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
389 struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
390 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
391 int pll_id, pll_source, pll_in, pll_out, clk_id, ret;
393 if (sof_rt5682_quirk & SOF_RT5682_MCLK_EN) {
394 if (sof_rt5682_quirk & SOF_RT5682_MCLK_BYTCHT_EN) {
395 ret = clk_prepare_enable(ctx->mclk);
398 "could not configure MCLK state");
403 if (sof_rt5682_quirk & SOF_RT5682S_HEADPHONE_CODEC_PRESENT)
404 pll_source = RT5682S_PLL_S_MCLK;
406 pll_source = RT5682_PLL1_S_MCLK;
408 /* get the tplg configured mclk. */
409 pll_in = sof_dai_get_mclk(rtd);
411 /* mclk from the quirk is the first choice */
412 if (sof_rt5682_quirk & SOF_RT5682_MCLK_24MHZ) {
413 if (pll_in != 24000000)
414 dev_warn(rtd->dev, "configure wrong mclk in tplg, please use 24MHz.\n");
416 } else if (pll_in == 0) {
417 /* use default mclk if not specified correct in topology */
419 } else if (pll_in < 0) {
423 if (sof_rt5682_quirk & SOF_RT5682S_HEADPHONE_CODEC_PRESENT)
424 pll_source = RT5682S_PLL_S_BCLK1;
426 pll_source = RT5682_PLL1_S_BCLK1;
428 pll_in = params_rate(params) * 50;
431 if (sof_rt5682_quirk & SOF_RT5682S_HEADPHONE_CODEC_PRESENT) {
432 pll_id = RT5682S_PLL2;
433 clk_id = RT5682S_SCLK_S_PLL2;
435 pll_id = RT5682_PLL1;
436 clk_id = RT5682_SCLK_S_PLL1;
439 pll_out = params_rate(params) * 512;
441 /* when MCLK is 512FS, no need to set PLL configuration additionally. */
442 if (pll_in == pll_out)
443 clk_id = RT5682S_SCLK_S_MCLK;
445 /* Configure pll for codec */
446 ret = snd_soc_dai_set_pll(codec_dai, pll_id, pll_source, pll_in,
449 dev_err(rtd->dev, "snd_soc_dai_set_pll err = %d\n", ret);
452 /* Configure sysclk for codec */
453 ret = snd_soc_dai_set_sysclk(codec_dai, clk_id,
454 pll_out, SND_SOC_CLOCK_IN);
456 dev_err(rtd->dev, "snd_soc_dai_set_sysclk err = %d\n", ret);
459 * slot_width should equal or large than data length, set them
462 ret = snd_soc_dai_set_tdm_slot(codec_dai, 0x0, 0x0, 2,
463 params_width(params));
465 dev_err(rtd->dev, "set TDM slot err:%d\n", ret);
472 static struct snd_soc_ops sof_rt5682_ops = {
473 .hw_params = sof_rt5682_hw_params,
476 static struct snd_soc_dai_link_component platform_component[] = {
478 /* name might be overridden during probe */
479 .name = "0000:00:1f.3"
483 static int sof_card_late_probe(struct snd_soc_card *card)
485 struct sof_card_private *ctx = snd_soc_card_get_drvdata(card);
486 struct snd_soc_component *component = NULL;
487 struct snd_soc_dapm_context *dapm = &card->dapm;
488 char jack_name[NAME_SIZE];
489 struct sof_hdmi_pcm *pcm;
492 if (sof_rt5682_quirk & SOF_MAX98373_SPEAKER_AMP_PRESENT) {
493 /* Disable Left and Right Spk pin after boot */
494 snd_soc_dapm_disable_pin(dapm, "Left Spk");
495 snd_soc_dapm_disable_pin(dapm, "Right Spk");
496 err = snd_soc_dapm_sync(dapm);
501 /* HDMI is not supported by SOF on Baytrail/CherryTrail */
502 if (is_legacy_cpu || !ctx->idisp_codec)
505 if (list_empty(&ctx->hdmi_pcm_list))
508 if (ctx->common_hdmi_codec_drv) {
509 pcm = list_first_entry(&ctx->hdmi_pcm_list, struct sof_hdmi_pcm,
511 component = pcm->codec_dai->component;
512 return hda_dsp_hdmi_build_controls(card, component);
515 list_for_each_entry(pcm, &ctx->hdmi_pcm_list, head) {
516 component = pcm->codec_dai->component;
517 snprintf(jack_name, sizeof(jack_name),
518 "HDMI/DP, pcm=%d Jack", pcm->device);
519 err = snd_soc_card_jack_new(card, jack_name,
520 SND_JACK_AVOUT, &pcm->hdmi_jack);
525 err = hdac_hdmi_jack_init(pcm->codec_dai, pcm->device,
531 return hdac_hdmi_jack_port_init(component, &card->dapm);
534 static const struct snd_kcontrol_new sof_controls[] = {
535 SOC_DAPM_PIN_SWITCH("Headphone Jack"),
536 SOC_DAPM_PIN_SWITCH("Headset Mic"),
537 SOC_DAPM_PIN_SWITCH("Left Spk"),
538 SOC_DAPM_PIN_SWITCH("Right Spk"),
542 static const struct snd_soc_dapm_widget sof_widgets[] = {
543 SND_SOC_DAPM_HP("Headphone Jack", NULL),
544 SND_SOC_DAPM_MIC("Headset Mic", NULL),
545 SND_SOC_DAPM_SPK("Left Spk", NULL),
546 SND_SOC_DAPM_SPK("Right Spk", NULL),
549 static const struct snd_soc_dapm_widget dmic_widgets[] = {
550 SND_SOC_DAPM_MIC("SoC DMIC", NULL),
553 static const struct snd_soc_dapm_route sof_map[] = {
554 /* HP jack connectors - unknown if we have jack detection */
555 { "Headphone Jack", NULL, "HPOL" },
556 { "Headphone Jack", NULL, "HPOR" },
559 { "IN1P", NULL, "Headset Mic" },
562 static const struct snd_soc_dapm_route dmic_map[] = {
564 {"DMic", NULL, "SoC DMIC"},
567 static int dmic_init(struct snd_soc_pcm_runtime *rtd)
569 struct snd_soc_card *card = rtd->card;
572 ret = snd_soc_dapm_new_controls(&card->dapm, dmic_widgets,
573 ARRAY_SIZE(dmic_widgets));
575 dev_err(card->dev, "DMic widget addition failed: %d\n", ret);
576 /* Don't need to add routes if widget addition failed */
580 ret = snd_soc_dapm_add_routes(&card->dapm, dmic_map,
581 ARRAY_SIZE(dmic_map));
584 dev_err(card->dev, "DMic map addition failed: %d\n", ret);
589 /* sof audio machine driver for rt5682 codec */
590 static struct snd_soc_card sof_audio_card_rt5682 = {
591 .name = "rt5682", /* the sof- prefix is added by the core */
592 .owner = THIS_MODULE,
593 .controls = sof_controls,
594 .num_controls = ARRAY_SIZE(sof_controls),
595 .dapm_widgets = sof_widgets,
596 .num_dapm_widgets = ARRAY_SIZE(sof_widgets),
597 .dapm_routes = sof_map,
598 .num_dapm_routes = ARRAY_SIZE(sof_map),
599 .fully_routed = true,
600 .late_probe = sof_card_late_probe,
603 static struct snd_soc_dai_link_component rt5682_component[] = {
605 .name = "i2c-10EC5682:00",
606 .dai_name = "rt5682-aif1",
610 static struct snd_soc_dai_link_component rt5682s_component[] = {
612 .name = "i2c-RTL5682:00",
613 .dai_name = "rt5682s-aif1",
617 static struct snd_soc_dai_link_component dmic_component[] = {
619 .name = "dmic-codec",
620 .dai_name = "dmic-hifi",
624 #define IDISP_CODEC_MASK 0x4
626 static struct snd_soc_dai_link *sof_card_dai_links_create(struct device *dev,
633 struct snd_soc_dai_link_component *idisp_components;
634 struct snd_soc_dai_link_component *cpus;
635 struct snd_soc_dai_link *links;
638 links = devm_kcalloc(dev, sof_audio_card_rt5682.num_links,
639 sizeof(struct snd_soc_dai_link), GFP_KERNEL);
640 cpus = devm_kcalloc(dev, sof_audio_card_rt5682.num_links,
641 sizeof(struct snd_soc_dai_link_component), GFP_KERNEL);
646 links[id].name = devm_kasprintf(dev, GFP_KERNEL,
647 "SSP%d-Codec", ssp_codec);
652 if (sof_rt5682_quirk & SOF_RT5682S_HEADPHONE_CODEC_PRESENT) {
653 links[id].codecs = rt5682s_component;
654 links[id].num_codecs = ARRAY_SIZE(rt5682s_component);
656 links[id].codecs = rt5682_component;
657 links[id].num_codecs = ARRAY_SIZE(rt5682_component);
659 links[id].platforms = platform_component;
660 links[id].num_platforms = ARRAY_SIZE(platform_component);
661 links[id].init = sof_rt5682_codec_init;
662 links[id].exit = sof_rt5682_codec_exit;
663 links[id].ops = &sof_rt5682_ops;
664 links[id].dpcm_playback = 1;
665 links[id].dpcm_capture = 1;
666 links[id].no_pcm = 1;
667 links[id].cpus = &cpus[id];
668 links[id].num_cpus = 1;
670 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
673 if (!links[id].cpus->dai_name)
677 * Currently, On SKL+ platforms MCLK will be turned off in sof
678 * runtime suspended, and it will go into runtime suspended
679 * right after playback is stop. However, rt5682 will output
680 * static noise if sysclk turns off during playback. Set
681 * ignore_pmdown_time to power down rt5682 immediately and
683 * It can be removed once we can control MCLK by driver.
685 links[id].ignore_pmdown_time = 1;
686 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
689 if (!links[id].cpus->dai_name)
695 if (dmic_be_num > 0) {
696 /* at least we have dmic01 */
697 links[id].name = "dmic01";
698 links[id].cpus = &cpus[id];
699 links[id].cpus->dai_name = "DMIC01 Pin";
700 links[id].init = dmic_init;
701 if (dmic_be_num > 1) {
702 /* set up 2 BE links at most */
703 links[id + 1].name = "dmic16k";
704 links[id + 1].cpus = &cpus[id + 1];
705 links[id + 1].cpus->dai_name = "DMIC16k Pin";
710 for (i = 0; i < dmic_be_num; i++) {
712 links[id].num_cpus = 1;
713 links[id].codecs = dmic_component;
714 links[id].num_codecs = ARRAY_SIZE(dmic_component);
715 links[id].platforms = platform_component;
716 links[id].num_platforms = ARRAY_SIZE(platform_component);
717 links[id].ignore_suspend = 1;
718 links[id].dpcm_capture = 1;
719 links[id].no_pcm = 1;
725 idisp_components = devm_kcalloc(dev,
727 sizeof(struct snd_soc_dai_link_component),
729 if (!idisp_components)
732 for (i = 1; i <= hdmi_num; i++) {
733 links[id].name = devm_kasprintf(dev, GFP_KERNEL,
739 links[id].cpus = &cpus[id];
740 links[id].num_cpus = 1;
741 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
743 if (!links[id].cpus->dai_name)
747 idisp_components[i - 1].name = "ehdaudio0D2";
748 idisp_components[i - 1].dai_name = devm_kasprintf(dev,
752 if (!idisp_components[i - 1].dai_name)
755 idisp_components[i - 1] = asoc_dummy_dlc;
758 links[id].codecs = &idisp_components[i - 1];
759 links[id].num_codecs = 1;
760 links[id].platforms = platform_component;
761 links[id].num_platforms = ARRAY_SIZE(platform_component);
762 links[id].init = sof_hdmi_init;
763 links[id].dpcm_playback = 1;
764 links[id].no_pcm = 1;
769 if (sof_rt5682_quirk & SOF_SPEAKER_AMP_PRESENT) {
770 links[id].name = devm_kasprintf(dev, GFP_KERNEL,
771 "SSP%d-Codec", ssp_amp);
776 if (sof_rt5682_quirk & SOF_RT1015_SPEAKER_AMP_PRESENT) {
777 sof_rt1015_dai_link(&links[id]);
778 } else if (sof_rt5682_quirk & SOF_RT1015P_SPEAKER_AMP_PRESENT) {
779 sof_rt1015p_dai_link(&links[id]);
780 } else if (sof_rt5682_quirk & SOF_RT1019_SPEAKER_AMP_PRESENT) {
781 sof_rt1019p_dai_link(&links[id]);
782 } else if (sof_rt5682_quirk &
783 SOF_MAX98373_SPEAKER_AMP_PRESENT) {
784 links[id].codecs = max_98373_components;
785 links[id].num_codecs = ARRAY_SIZE(max_98373_components);
786 links[id].init = max_98373_spk_codec_init;
787 links[id].ops = &max_98373_ops;
788 } else if (sof_rt5682_quirk &
789 SOF_MAX98360A_SPEAKER_AMP_PRESENT) {
790 max_98360a_dai_link(&links[id]);
791 } else if (sof_rt5682_quirk &
792 SOF_RT1011_SPEAKER_AMP_PRESENT) {
793 sof_rt1011_dai_link(&links[id]);
794 } else if (sof_rt5682_quirk &
795 SOF_MAX98390_SPEAKER_AMP_PRESENT) {
796 if (sof_rt5682_quirk &
797 SOF_MAX98390_TWEETER_SPEAKER_PRESENT) {
798 links[id].codecs = max_98390_4spk_components;
799 links[id].num_codecs = ARRAY_SIZE(max_98390_4spk_components);
801 links[id].codecs = max_98390_components;
802 links[id].num_codecs = ARRAY_SIZE(max_98390_components);
804 links[id].init = max_98390_spk_codec_init;
805 links[id].ops = &max_98390_ops;
808 max_98357a_dai_link(&links[id]);
810 links[id].platforms = platform_component;
811 links[id].num_platforms = ARRAY_SIZE(platform_component);
812 links[id].dpcm_playback = 1;
813 /* feedback stream or firmware-generated echo reference */
814 links[id].dpcm_capture = 1;
816 links[id].no_pcm = 1;
817 links[id].cpus = &cpus[id];
818 links[id].num_cpus = 1;
820 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
823 if (!links[id].cpus->dai_name)
827 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
830 if (!links[id].cpus->dai_name)
836 /* BT audio offload */
837 if (sof_rt5682_quirk & SOF_SSP_BT_OFFLOAD_PRESENT) {
838 int port = (sof_rt5682_quirk & SOF_BT_OFFLOAD_SSP_MASK) >>
839 SOF_BT_OFFLOAD_SSP_SHIFT;
842 links[id].cpus = &cpus[id];
843 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
845 if (!links[id].cpus->dai_name)
847 links[id].name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-BT", port);
850 links[id].codecs = &asoc_dummy_dlc;
851 links[id].num_codecs = 1;
852 links[id].platforms = platform_component;
853 links[id].num_platforms = ARRAY_SIZE(platform_component);
854 links[id].dpcm_playback = 1;
855 links[id].dpcm_capture = 1;
856 links[id].no_pcm = 1;
857 links[id].num_cpus = 1;
865 static int sof_audio_probe(struct platform_device *pdev)
867 struct snd_soc_dai_link *dai_links;
868 struct snd_soc_acpi_mach *mach;
869 struct sof_card_private *ctx;
870 int dmic_be_num, hdmi_num;
871 int ret, ssp_amp, ssp_codec;
873 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
877 if (pdev->id_entry && pdev->id_entry->driver_data)
878 sof_rt5682_quirk = (unsigned long)pdev->id_entry->driver_data;
880 dmi_check_system(sof_rt5682_quirk_table);
882 mach = pdev->dev.platform_data;
884 /* A speaker amp might not be present when the quirk claims one is.
885 * Detect this via whether the machine driver match includes quirk_data.
887 if ((sof_rt5682_quirk & SOF_SPEAKER_AMP_PRESENT) && !mach->quirk_data)
888 sof_rt5682_quirk &= ~SOF_SPEAKER_AMP_PRESENT;
890 /* Detect the headset codec variant */
891 if (acpi_dev_present("RTL5682", NULL, -1))
892 sof_rt5682_quirk |= SOF_RT5682S_HEADPHONE_CODEC_PRESENT;
894 if (soc_intel_is_byt() || soc_intel_is_cht()) {
898 /* default quirk for legacy cpu */
899 sof_rt5682_quirk = SOF_RT5682_MCLK_EN |
900 SOF_RT5682_MCLK_BYTCHT_EN |
901 SOF_RT5682_SSP_CODEC(2);
904 hdmi_num = (sof_rt5682_quirk & SOF_RT5682_NUM_HDMIDEV_MASK) >>
905 SOF_RT5682_NUM_HDMIDEV_SHIFT;
906 /* default number of HDMI DAI's */
910 if (mach->mach_params.codec_mask & IDISP_CODEC_MASK)
911 ctx->idisp_codec = true;
914 /* need to get main clock from pmc */
915 if (sof_rt5682_quirk & SOF_RT5682_MCLK_BYTCHT_EN) {
916 ctx->mclk = devm_clk_get(&pdev->dev, "pmc_plt_clk_3");
917 if (IS_ERR(ctx->mclk)) {
918 ret = PTR_ERR(ctx->mclk);
921 "Failed to get MCLK from pmc_plt_clk_3: %d\n",
926 ret = clk_prepare_enable(ctx->mclk);
929 "could not configure MCLK state");
934 dev_dbg(&pdev->dev, "sof_rt5682_quirk = %lx\n", sof_rt5682_quirk);
936 ssp_amp = (sof_rt5682_quirk & SOF_RT5682_SSP_AMP_MASK) >>
937 SOF_RT5682_SSP_AMP_SHIFT;
939 ssp_codec = sof_rt5682_quirk & SOF_RT5682_SSP_CODEC_MASK;
941 /* compute number of dai links */
942 sof_audio_card_rt5682.num_links = 1 + dmic_be_num + hdmi_num;
944 if (sof_rt5682_quirk & SOF_SPEAKER_AMP_PRESENT)
945 sof_audio_card_rt5682.num_links++;
947 if (sof_rt5682_quirk & SOF_MAX98373_SPEAKER_AMP_PRESENT)
948 max_98373_set_codec_conf(&sof_audio_card_rt5682);
949 else if (sof_rt5682_quirk & SOF_RT1011_SPEAKER_AMP_PRESENT)
950 sof_rt1011_codec_conf(&sof_audio_card_rt5682);
951 else if (sof_rt5682_quirk & SOF_RT1015P_SPEAKER_AMP_PRESENT)
952 sof_rt1015p_codec_conf(&sof_audio_card_rt5682);
953 else if (sof_rt5682_quirk & SOF_MAX98390_SPEAKER_AMP_PRESENT) {
954 if (sof_rt5682_quirk & SOF_MAX98390_TWEETER_SPEAKER_PRESENT)
955 max_98390_set_codec_conf(&sof_audio_card_rt5682,
956 ARRAY_SIZE(max_98390_4spk_components));
958 max_98390_set_codec_conf(&sof_audio_card_rt5682,
959 ARRAY_SIZE(max_98390_components));
962 if (sof_rt5682_quirk & SOF_SSP_BT_OFFLOAD_PRESENT)
963 sof_audio_card_rt5682.num_links++;
965 dai_links = sof_card_dai_links_create(&pdev->dev, ssp_codec, ssp_amp,
966 dmic_be_num, hdmi_num, ctx->idisp_codec);
970 sof_audio_card_rt5682.dai_link = dai_links;
972 if (sof_rt5682_quirk & SOF_RT1015_SPEAKER_AMP_PRESENT)
973 sof_rt1015_codec_conf(&sof_audio_card_rt5682);
975 INIT_LIST_HEAD(&ctx->hdmi_pcm_list);
977 sof_audio_card_rt5682.dev = &pdev->dev;
979 /* set platform name for each dailink */
980 ret = snd_soc_fixup_dai_links_platform_name(&sof_audio_card_rt5682,
981 mach->mach_params.platform);
985 ctx->common_hdmi_codec_drv = mach->mach_params.common_hdmi_codec_drv;
987 snd_soc_card_set_drvdata(&sof_audio_card_rt5682, ctx);
989 return devm_snd_soc_register_card(&pdev->dev,
990 &sof_audio_card_rt5682);
993 static const struct platform_device_id board_ids[] = {
995 .name = "sof_rt5682",
998 .name = "cml_rt1015_rt5682",
999 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1000 SOF_RT5682_MCLK_24MHZ |
1001 SOF_RT5682_SSP_CODEC(0) |
1002 SOF_SPEAKER_AMP_PRESENT |
1003 SOF_RT1015_SPEAKER_AMP_PRESENT |
1004 SOF_RT5682_SSP_AMP(1)),
1007 .name = "jsl_rt5682_rt1015",
1008 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1009 SOF_RT5682_MCLK_24MHZ |
1010 SOF_RT5682_SSP_CODEC(0) |
1011 SOF_SPEAKER_AMP_PRESENT |
1012 SOF_RT1015_SPEAKER_AMP_PRESENT |
1013 SOF_RT5682_SSP_AMP(1)),
1016 .name = "jsl_rt5682_mx98360",
1017 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1018 SOF_RT5682_MCLK_24MHZ |
1019 SOF_RT5682_SSP_CODEC(0) |
1020 SOF_SPEAKER_AMP_PRESENT |
1021 SOF_MAX98360A_SPEAKER_AMP_PRESENT |
1022 SOF_RT5682_SSP_AMP(1)),
1025 .name = "jsl_rt5682_rt1015p",
1026 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1027 SOF_RT5682_MCLK_24MHZ |
1028 SOF_RT5682_SSP_CODEC(0) |
1029 SOF_SPEAKER_AMP_PRESENT |
1030 SOF_RT1015P_SPEAKER_AMP_PRESENT |
1031 SOF_RT5682_SSP_AMP(1)),
1034 .name = "jsl_rt5682",
1035 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1036 SOF_RT5682_MCLK_24MHZ |
1037 SOF_RT5682_SSP_CODEC(0)),
1040 .name = "tgl_mx98357_rt5682",
1041 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1042 SOF_RT5682_SSP_CODEC(0) |
1043 SOF_SPEAKER_AMP_PRESENT |
1044 SOF_RT5682_SSP_AMP(1) |
1045 SOF_RT5682_NUM_HDMIDEV(4) |
1046 SOF_BT_OFFLOAD_SSP(2) |
1047 SOF_SSP_BT_OFFLOAD_PRESENT),
1050 .name = "tgl_rt1011_rt5682",
1051 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1052 SOF_RT5682_SSP_CODEC(0) |
1053 SOF_SPEAKER_AMP_PRESENT |
1054 SOF_RT1011_SPEAKER_AMP_PRESENT |
1055 SOF_RT5682_SSP_AMP(1) |
1056 SOF_RT5682_NUM_HDMIDEV(4) |
1057 SOF_BT_OFFLOAD_SSP(2) |
1058 SOF_SSP_BT_OFFLOAD_PRESENT),
1061 .name = "tgl_mx98373_rt5682",
1062 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1063 SOF_RT5682_SSP_CODEC(0) |
1064 SOF_SPEAKER_AMP_PRESENT |
1065 SOF_MAX98373_SPEAKER_AMP_PRESENT |
1066 SOF_RT5682_SSP_AMP(1) |
1067 SOF_RT5682_NUM_HDMIDEV(4) |
1068 SOF_BT_OFFLOAD_SSP(2) |
1069 SOF_SSP_BT_OFFLOAD_PRESENT),
1072 .name = "adl_mx98373_rt5682",
1073 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1074 SOF_RT5682_SSP_CODEC(0) |
1075 SOF_SPEAKER_AMP_PRESENT |
1076 SOF_MAX98373_SPEAKER_AMP_PRESENT |
1077 SOF_RT5682_SSP_AMP(1) |
1078 SOF_RT5682_NUM_HDMIDEV(4) |
1079 SOF_BT_OFFLOAD_SSP(2) |
1080 SOF_SSP_BT_OFFLOAD_PRESENT),
1083 .name = "adl_mx98357_rt5682",
1084 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1085 SOF_RT5682_SSP_CODEC(0) |
1086 SOF_SPEAKER_AMP_PRESENT |
1087 SOF_RT5682_SSP_AMP(2) |
1088 SOF_RT5682_NUM_HDMIDEV(4)),
1091 .name = "adl_max98390_rt5682",
1092 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1093 SOF_RT5682_SSP_CODEC(0) |
1094 SOF_SPEAKER_AMP_PRESENT |
1095 SOF_MAX98390_SPEAKER_AMP_PRESENT |
1096 SOF_RT5682_SSP_AMP(1) |
1097 SOF_RT5682_NUM_HDMIDEV(4) |
1098 SOF_BT_OFFLOAD_SSP(2) |
1099 SOF_SSP_BT_OFFLOAD_PRESENT),
1102 .name = "adl_mx98360_rt5682",
1103 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1104 SOF_RT5682_SSP_CODEC(0) |
1105 SOF_SPEAKER_AMP_PRESENT |
1106 SOF_MAX98360A_SPEAKER_AMP_PRESENT |
1107 SOF_RT5682_SSP_AMP(1) |
1108 SOF_RT5682_NUM_HDMIDEV(4) |
1109 SOF_BT_OFFLOAD_SSP(2) |
1110 SOF_SSP_BT_OFFLOAD_PRESENT),
1113 .name = "adl_rt5682",
1114 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1115 SOF_RT5682_SSP_CODEC(0) |
1116 SOF_RT5682_NUM_HDMIDEV(4) |
1117 SOF_BT_OFFLOAD_SSP(2) |
1118 SOF_SSP_BT_OFFLOAD_PRESENT),
1121 .name = "adl_rt1019_rt5682",
1122 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1123 SOF_RT5682_SSP_CODEC(0) |
1124 SOF_SPEAKER_AMP_PRESENT |
1125 SOF_RT1019_SPEAKER_AMP_PRESENT |
1126 SOF_RT5682_SSP_AMP(1) |
1127 SOF_RT5682_NUM_HDMIDEV(4) |
1128 SOF_BT_OFFLOAD_SSP(2) |
1129 SOF_SSP_BT_OFFLOAD_PRESENT),
1132 .name = "rpl_mx98360_rt5682",
1133 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1134 SOF_RT5682_SSP_CODEC(0) |
1135 SOF_SPEAKER_AMP_PRESENT |
1136 SOF_MAX98360A_SPEAKER_AMP_PRESENT |
1137 SOF_RT5682_SSP_AMP(1) |
1138 SOF_RT5682_NUM_HDMIDEV(4) |
1139 SOF_BT_OFFLOAD_SSP(2) |
1140 SOF_SSP_BT_OFFLOAD_PRESENT),
1143 .name = "rpl_rt1019_rt5682",
1144 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1145 SOF_RT5682_SSP_CODEC(0) |
1146 SOF_SPEAKER_AMP_PRESENT |
1147 SOF_RT1019_SPEAKER_AMP_PRESENT |
1148 SOF_RT5682_SSP_AMP(1) |
1149 SOF_RT5682_NUM_HDMIDEV(4) |
1150 SOF_BT_OFFLOAD_SSP(2) |
1151 SOF_SSP_BT_OFFLOAD_PRESENT),
1154 .name = "mtl_mx98357_rt5682",
1155 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1156 SOF_RT5682_SSP_CODEC(0) |
1157 SOF_SPEAKER_AMP_PRESENT |
1158 SOF_RT5682_SSP_AMP(1) |
1159 SOF_RT5682_NUM_HDMIDEV(4) |
1160 SOF_BT_OFFLOAD_SSP(2) |
1161 SOF_SSP_BT_OFFLOAD_PRESENT),
1164 .name = "mtl_mx98360_rt5682",
1165 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1166 SOF_RT5682_SSP_CODEC(0) |
1167 SOF_SPEAKER_AMP_PRESENT |
1168 SOF_MAX98360A_SPEAKER_AMP_PRESENT |
1169 SOF_RT5682_SSP_AMP(1) |
1170 SOF_RT5682_NUM_HDMIDEV(4)),
1173 .name = "mtl_rt1019_rt5682",
1174 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1175 SOF_RT5682_SSP_CODEC(2) |
1176 SOF_SPEAKER_AMP_PRESENT |
1177 SOF_RT1019_SPEAKER_AMP_PRESENT |
1178 SOF_RT5682_SSP_AMP(0) |
1179 SOF_RT5682_NUM_HDMIDEV(3)),
1183 MODULE_DEVICE_TABLE(platform, board_ids);
1185 static struct platform_driver sof_audio = {
1186 .probe = sof_audio_probe,
1188 .name = "sof_rt5682",
1189 .pm = &snd_soc_pm_ops,
1191 .id_table = board_ids,
1193 module_platform_driver(sof_audio)
1195 /* Module information */
1196 MODULE_DESCRIPTION("SOF Audio Machine driver");
1197 MODULE_AUTHOR("Bard Liao <bard.liao@intel.com>");
1198 MODULE_AUTHOR("Sathya Prakash M R <sathya.prakash.m.r@intel.com>");
1199 MODULE_AUTHOR("Brent Lu <brent.lu@intel.com>");
1200 MODULE_AUTHOR("Mac Chiang <mac.chiang@intel.com>");
1201 MODULE_LICENSE("GPL v2");
1202 MODULE_IMPORT_NS(SND_SOC_INTEL_HDA_DSP_COMMON);
1203 MODULE_IMPORT_NS(SND_SOC_INTEL_SOF_MAXIM_COMMON);
1204 MODULE_IMPORT_NS(SND_SOC_INTEL_SOF_REALTEK_COMMON);