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"),
247 .driver_data = (void *)(SOF_RT5682_MCLK_EN |
248 SOF_RT5682_SSP_CODEC(2) |
249 SOF_SPEAKER_AMP_PRESENT |
250 SOF_RT5682_SSP_AMP(0) |
251 SOF_RT5682_NUM_HDMIDEV(4) |
252 SOF_BT_OFFLOAD_SSP(1) |
253 SOF_SSP_BT_OFFLOAD_PRESENT
259 static int sof_hdmi_init(struct snd_soc_pcm_runtime *rtd)
261 struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
262 struct snd_soc_dai *dai = asoc_rtd_to_codec(rtd, 0);
263 struct sof_hdmi_pcm *pcm;
265 pcm = devm_kzalloc(rtd->card->dev, sizeof(*pcm), GFP_KERNEL);
269 /* dai_link id is 1:1 mapped to the PCM device */
270 pcm->device = rtd->dai_link->id;
271 pcm->codec_dai = dai;
273 list_add_tail(&pcm->head, &ctx->hdmi_pcm_list);
278 static struct snd_soc_jack_pin jack_pins[] = {
280 .pin = "Headphone Jack",
281 .mask = SND_JACK_HEADPHONE,
284 .pin = "Headset Mic",
285 .mask = SND_JACK_MICROPHONE,
289 static int sof_rt5682_codec_init(struct snd_soc_pcm_runtime *rtd)
291 struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
292 struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
293 struct snd_soc_jack *jack;
296 /* need to enable ASRC function for 24MHz mclk rate */
297 if ((sof_rt5682_quirk & SOF_RT5682_MCLK_EN) &&
298 (sof_rt5682_quirk & SOF_RT5682_MCLK_24MHZ)) {
299 if (sof_rt5682_quirk & SOF_RT5682S_HEADPHONE_CODEC_PRESENT)
300 rt5682s_sel_asrc_clk_src(component,
301 RT5682S_DA_STEREO1_FILTER |
302 RT5682S_AD_STEREO1_FILTER,
303 RT5682S_CLK_SEL_I2S1_ASRC);
305 rt5682_sel_asrc_clk_src(component,
306 RT5682_DA_STEREO1_FILTER |
307 RT5682_AD_STEREO1_FILTER,
308 RT5682_CLK_SEL_I2S1_ASRC);
311 if (sof_rt5682_quirk & SOF_RT5682_MCLK_BYTCHT_EN) {
313 * The firmware might enable the clock at
314 * boot (this information may or may not
315 * be reflected in the enable clock register).
316 * To change the rate we must disable the clock
317 * first to cover these cases. Due to common
318 * clock framework restrictions that do not allow
319 * to disable a clock that has not been enabled,
320 * we need to enable the clock first.
322 ret = clk_prepare_enable(ctx->mclk);
324 clk_disable_unprepare(ctx->mclk);
326 ret = clk_set_rate(ctx->mclk, 19200000);
329 dev_err(rtd->dev, "unable to set MCLK rate\n");
333 * Headset buttons map to the google Reference headset.
334 * These can be configured by userspace.
336 ret = snd_soc_card_jack_new_pins(rtd->card, "Headset Jack",
337 SND_JACK_HEADSET | SND_JACK_BTN_0 |
338 SND_JACK_BTN_1 | SND_JACK_BTN_2 |
342 ARRAY_SIZE(jack_pins));
344 dev_err(rtd->dev, "Headset Jack creation failed: %d\n", ret);
348 jack = &ctx->sof_headset;
350 snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
351 snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
352 snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
353 snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
354 ret = snd_soc_component_set_jack(component, jack, NULL);
357 dev_err(rtd->dev, "Headset Jack call-back failed: %d\n", ret);
364 static void sof_rt5682_codec_exit(struct snd_soc_pcm_runtime *rtd)
366 struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
368 snd_soc_component_set_jack(component, NULL, NULL);
371 static int sof_rt5682_hw_params(struct snd_pcm_substream *substream,
372 struct snd_pcm_hw_params *params)
374 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
375 struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
376 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
377 int pll_id, pll_source, pll_in, pll_out, clk_id, ret;
379 if (sof_rt5682_quirk & SOF_RT5682_MCLK_EN) {
380 if (sof_rt5682_quirk & SOF_RT5682_MCLK_BYTCHT_EN) {
381 ret = clk_prepare_enable(ctx->mclk);
384 "could not configure MCLK state");
389 if (sof_rt5682_quirk & SOF_RT5682S_HEADPHONE_CODEC_PRESENT)
390 pll_source = RT5682S_PLL_S_MCLK;
392 pll_source = RT5682_PLL1_S_MCLK;
394 /* get the tplg configured mclk. */
395 pll_in = sof_dai_get_mclk(rtd);
397 /* mclk from the quirk is the first choice */
398 if (sof_rt5682_quirk & SOF_RT5682_MCLK_24MHZ) {
399 if (pll_in != 24000000)
400 dev_warn(rtd->dev, "configure wrong mclk in tplg, please use 24MHz.\n");
402 } else if (pll_in == 0) {
403 /* use default mclk if not specified correct in topology */
405 } else if (pll_in < 0) {
409 if (sof_rt5682_quirk & SOF_RT5682S_HEADPHONE_CODEC_PRESENT)
410 pll_source = RT5682S_PLL_S_BCLK1;
412 pll_source = RT5682_PLL1_S_BCLK1;
414 pll_in = params_rate(params) * 50;
417 if (sof_rt5682_quirk & SOF_RT5682S_HEADPHONE_CODEC_PRESENT) {
418 pll_id = RT5682S_PLL2;
419 clk_id = RT5682S_SCLK_S_PLL2;
421 pll_id = RT5682_PLL1;
422 clk_id = RT5682_SCLK_S_PLL1;
425 pll_out = params_rate(params) * 512;
427 /* when MCLK is 512FS, no need to set PLL configuration additionally. */
428 if (pll_in == pll_out)
429 clk_id = RT5682S_SCLK_S_MCLK;
431 /* Configure pll for codec */
432 ret = snd_soc_dai_set_pll(codec_dai, pll_id, pll_source, pll_in,
435 dev_err(rtd->dev, "snd_soc_dai_set_pll err = %d\n", ret);
438 /* Configure sysclk for codec */
439 ret = snd_soc_dai_set_sysclk(codec_dai, clk_id,
440 pll_out, SND_SOC_CLOCK_IN);
442 dev_err(rtd->dev, "snd_soc_dai_set_sysclk err = %d\n", ret);
445 * slot_width should equal or large than data length, set them
448 ret = snd_soc_dai_set_tdm_slot(codec_dai, 0x0, 0x0, 2,
449 params_width(params));
451 dev_err(rtd->dev, "set TDM slot err:%d\n", ret);
458 static struct snd_soc_ops sof_rt5682_ops = {
459 .hw_params = sof_rt5682_hw_params,
462 static struct snd_soc_dai_link_component platform_component[] = {
464 /* name might be overridden during probe */
465 .name = "0000:00:1f.3"
469 static int sof_card_late_probe(struct snd_soc_card *card)
471 struct sof_card_private *ctx = snd_soc_card_get_drvdata(card);
472 struct snd_soc_component *component = NULL;
473 struct snd_soc_dapm_context *dapm = &card->dapm;
474 char jack_name[NAME_SIZE];
475 struct sof_hdmi_pcm *pcm;
478 if (sof_rt5682_quirk & SOF_MAX98373_SPEAKER_AMP_PRESENT) {
479 /* Disable Left and Right Spk pin after boot */
480 snd_soc_dapm_disable_pin(dapm, "Left Spk");
481 snd_soc_dapm_disable_pin(dapm, "Right Spk");
482 err = snd_soc_dapm_sync(dapm);
487 /* HDMI is not supported by SOF on Baytrail/CherryTrail */
488 if (is_legacy_cpu || !ctx->idisp_codec)
491 if (list_empty(&ctx->hdmi_pcm_list))
494 if (ctx->common_hdmi_codec_drv) {
495 pcm = list_first_entry(&ctx->hdmi_pcm_list, struct sof_hdmi_pcm,
497 component = pcm->codec_dai->component;
498 return hda_dsp_hdmi_build_controls(card, component);
501 list_for_each_entry(pcm, &ctx->hdmi_pcm_list, head) {
502 component = pcm->codec_dai->component;
503 snprintf(jack_name, sizeof(jack_name),
504 "HDMI/DP, pcm=%d Jack", pcm->device);
505 err = snd_soc_card_jack_new(card, jack_name,
506 SND_JACK_AVOUT, &pcm->hdmi_jack);
511 err = hdac_hdmi_jack_init(pcm->codec_dai, pcm->device,
517 return hdac_hdmi_jack_port_init(component, &card->dapm);
520 static const struct snd_kcontrol_new sof_controls[] = {
521 SOC_DAPM_PIN_SWITCH("Headphone Jack"),
522 SOC_DAPM_PIN_SWITCH("Headset Mic"),
523 SOC_DAPM_PIN_SWITCH("Left Spk"),
524 SOC_DAPM_PIN_SWITCH("Right Spk"),
528 static const struct snd_soc_dapm_widget sof_widgets[] = {
529 SND_SOC_DAPM_HP("Headphone Jack", NULL),
530 SND_SOC_DAPM_MIC("Headset Mic", NULL),
531 SND_SOC_DAPM_SPK("Left Spk", NULL),
532 SND_SOC_DAPM_SPK("Right Spk", NULL),
535 static const struct snd_soc_dapm_widget dmic_widgets[] = {
536 SND_SOC_DAPM_MIC("SoC DMIC", NULL),
539 static const struct snd_soc_dapm_route sof_map[] = {
540 /* HP jack connectors - unknown if we have jack detection */
541 { "Headphone Jack", NULL, "HPOL" },
542 { "Headphone Jack", NULL, "HPOR" },
545 { "IN1P", NULL, "Headset Mic" },
548 static const struct snd_soc_dapm_route dmic_map[] = {
550 {"DMic", NULL, "SoC DMIC"},
553 static int dmic_init(struct snd_soc_pcm_runtime *rtd)
555 struct snd_soc_card *card = rtd->card;
558 ret = snd_soc_dapm_new_controls(&card->dapm, dmic_widgets,
559 ARRAY_SIZE(dmic_widgets));
561 dev_err(card->dev, "DMic widget addition failed: %d\n", ret);
562 /* Don't need to add routes if widget addition failed */
566 ret = snd_soc_dapm_add_routes(&card->dapm, dmic_map,
567 ARRAY_SIZE(dmic_map));
570 dev_err(card->dev, "DMic map addition failed: %d\n", ret);
575 /* sof audio machine driver for rt5682 codec */
576 static struct snd_soc_card sof_audio_card_rt5682 = {
577 .name = "rt5682", /* the sof- prefix is added by the core */
578 .owner = THIS_MODULE,
579 .controls = sof_controls,
580 .num_controls = ARRAY_SIZE(sof_controls),
581 .dapm_widgets = sof_widgets,
582 .num_dapm_widgets = ARRAY_SIZE(sof_widgets),
583 .dapm_routes = sof_map,
584 .num_dapm_routes = ARRAY_SIZE(sof_map),
585 .fully_routed = true,
586 .late_probe = sof_card_late_probe,
589 static struct snd_soc_dai_link_component rt5682_component[] = {
591 .name = "i2c-10EC5682:00",
592 .dai_name = "rt5682-aif1",
596 static struct snd_soc_dai_link_component rt5682s_component[] = {
598 .name = "i2c-RTL5682:00",
599 .dai_name = "rt5682s-aif1",
603 static struct snd_soc_dai_link_component dmic_component[] = {
605 .name = "dmic-codec",
606 .dai_name = "dmic-hifi",
610 static struct snd_soc_dai_link_component dummy_component[] = {
612 .name = "snd-soc-dummy",
613 .dai_name = "snd-soc-dummy-dai",
617 #define IDISP_CODEC_MASK 0x4
619 static struct snd_soc_dai_link *sof_card_dai_links_create(struct device *dev,
626 struct snd_soc_dai_link_component *idisp_components;
627 struct snd_soc_dai_link_component *cpus;
628 struct snd_soc_dai_link *links;
631 links = devm_kcalloc(dev, sof_audio_card_rt5682.num_links,
632 sizeof(struct snd_soc_dai_link), GFP_KERNEL);
633 cpus = devm_kcalloc(dev, sof_audio_card_rt5682.num_links,
634 sizeof(struct snd_soc_dai_link_component), GFP_KERNEL);
639 links[id].name = devm_kasprintf(dev, GFP_KERNEL,
640 "SSP%d-Codec", ssp_codec);
645 if (sof_rt5682_quirk & SOF_RT5682S_HEADPHONE_CODEC_PRESENT) {
646 links[id].codecs = rt5682s_component;
647 links[id].num_codecs = ARRAY_SIZE(rt5682s_component);
649 links[id].codecs = rt5682_component;
650 links[id].num_codecs = ARRAY_SIZE(rt5682_component);
652 links[id].platforms = platform_component;
653 links[id].num_platforms = ARRAY_SIZE(platform_component);
654 links[id].init = sof_rt5682_codec_init;
655 links[id].exit = sof_rt5682_codec_exit;
656 links[id].ops = &sof_rt5682_ops;
657 links[id].dpcm_playback = 1;
658 links[id].dpcm_capture = 1;
659 links[id].no_pcm = 1;
660 links[id].cpus = &cpus[id];
661 links[id].num_cpus = 1;
663 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
666 if (!links[id].cpus->dai_name)
670 * Currently, On SKL+ platforms MCLK will be turned off in sof
671 * runtime suspended, and it will go into runtime suspended
672 * right after playback is stop. However, rt5682 will output
673 * static noise if sysclk turns off during playback. Set
674 * ignore_pmdown_time to power down rt5682 immediately and
676 * It can be removed once we can control MCLK by driver.
678 links[id].ignore_pmdown_time = 1;
679 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
682 if (!links[id].cpus->dai_name)
688 if (dmic_be_num > 0) {
689 /* at least we have dmic01 */
690 links[id].name = "dmic01";
691 links[id].cpus = &cpus[id];
692 links[id].cpus->dai_name = "DMIC01 Pin";
693 links[id].init = dmic_init;
694 if (dmic_be_num > 1) {
695 /* set up 2 BE links at most */
696 links[id + 1].name = "dmic16k";
697 links[id + 1].cpus = &cpus[id + 1];
698 links[id + 1].cpus->dai_name = "DMIC16k Pin";
703 for (i = 0; i < dmic_be_num; i++) {
705 links[id].num_cpus = 1;
706 links[id].codecs = dmic_component;
707 links[id].num_codecs = ARRAY_SIZE(dmic_component);
708 links[id].platforms = platform_component;
709 links[id].num_platforms = ARRAY_SIZE(platform_component);
710 links[id].ignore_suspend = 1;
711 links[id].dpcm_capture = 1;
712 links[id].no_pcm = 1;
718 idisp_components = devm_kcalloc(dev,
720 sizeof(struct snd_soc_dai_link_component),
722 if (!idisp_components)
725 for (i = 1; i <= hdmi_num; i++) {
726 links[id].name = devm_kasprintf(dev, GFP_KERNEL,
732 links[id].cpus = &cpus[id];
733 links[id].num_cpus = 1;
734 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
736 if (!links[id].cpus->dai_name)
740 idisp_components[i - 1].name = "ehdaudio0D2";
741 idisp_components[i - 1].dai_name = devm_kasprintf(dev,
745 if (!idisp_components[i - 1].dai_name)
748 idisp_components[i - 1].name = "snd-soc-dummy";
749 idisp_components[i - 1].dai_name = "snd-soc-dummy-dai";
752 links[id].codecs = &idisp_components[i - 1];
753 links[id].num_codecs = 1;
754 links[id].platforms = platform_component;
755 links[id].num_platforms = ARRAY_SIZE(platform_component);
756 links[id].init = sof_hdmi_init;
757 links[id].dpcm_playback = 1;
758 links[id].no_pcm = 1;
763 if (sof_rt5682_quirk & SOF_SPEAKER_AMP_PRESENT) {
764 links[id].name = devm_kasprintf(dev, GFP_KERNEL,
765 "SSP%d-Codec", ssp_amp);
770 if (sof_rt5682_quirk & SOF_RT1015_SPEAKER_AMP_PRESENT) {
771 sof_rt1015_dai_link(&links[id]);
772 } else if (sof_rt5682_quirk & SOF_RT1015P_SPEAKER_AMP_PRESENT) {
773 sof_rt1015p_dai_link(&links[id]);
774 } else if (sof_rt5682_quirk & SOF_RT1019_SPEAKER_AMP_PRESENT) {
775 sof_rt1019p_dai_link(&links[id]);
776 } else if (sof_rt5682_quirk &
777 SOF_MAX98373_SPEAKER_AMP_PRESENT) {
778 links[id].codecs = max_98373_components;
779 links[id].num_codecs = ARRAY_SIZE(max_98373_components);
780 links[id].init = max_98373_spk_codec_init;
781 links[id].ops = &max_98373_ops;
782 } else if (sof_rt5682_quirk &
783 SOF_MAX98360A_SPEAKER_AMP_PRESENT) {
784 max_98360a_dai_link(&links[id]);
785 } else if (sof_rt5682_quirk &
786 SOF_RT1011_SPEAKER_AMP_PRESENT) {
787 sof_rt1011_dai_link(&links[id]);
788 } else if (sof_rt5682_quirk &
789 SOF_MAX98390_SPEAKER_AMP_PRESENT) {
790 if (sof_rt5682_quirk &
791 SOF_MAX98390_TWEETER_SPEAKER_PRESENT) {
792 links[id].codecs = max_98390_4spk_components;
793 links[id].num_codecs = ARRAY_SIZE(max_98390_4spk_components);
795 links[id].codecs = max_98390_components;
796 links[id].num_codecs = ARRAY_SIZE(max_98390_components);
798 links[id].init = max_98390_spk_codec_init;
799 links[id].ops = &max_98390_ops;
802 max_98357a_dai_link(&links[id]);
804 links[id].platforms = platform_component;
805 links[id].num_platforms = ARRAY_SIZE(platform_component);
806 links[id].dpcm_playback = 1;
807 /* feedback stream or firmware-generated echo reference */
808 links[id].dpcm_capture = 1;
810 links[id].no_pcm = 1;
811 links[id].cpus = &cpus[id];
812 links[id].num_cpus = 1;
814 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
817 if (!links[id].cpus->dai_name)
821 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
824 if (!links[id].cpus->dai_name)
830 /* BT audio offload */
831 if (sof_rt5682_quirk & SOF_SSP_BT_OFFLOAD_PRESENT) {
832 int port = (sof_rt5682_quirk & SOF_BT_OFFLOAD_SSP_MASK) >>
833 SOF_BT_OFFLOAD_SSP_SHIFT;
836 links[id].cpus = &cpus[id];
837 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
839 if (!links[id].cpus->dai_name)
841 links[id].name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-BT", port);
844 links[id].codecs = dummy_component;
845 links[id].num_codecs = ARRAY_SIZE(dummy_component);
846 links[id].platforms = platform_component;
847 links[id].num_platforms = ARRAY_SIZE(platform_component);
848 links[id].dpcm_playback = 1;
849 links[id].dpcm_capture = 1;
850 links[id].no_pcm = 1;
851 links[id].num_cpus = 1;
859 static int sof_audio_probe(struct platform_device *pdev)
861 struct snd_soc_dai_link *dai_links;
862 struct snd_soc_acpi_mach *mach;
863 struct sof_card_private *ctx;
864 int dmic_be_num, hdmi_num;
865 int ret, ssp_amp, ssp_codec;
867 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
871 if (pdev->id_entry && pdev->id_entry->driver_data)
872 sof_rt5682_quirk = (unsigned long)pdev->id_entry->driver_data;
874 dmi_check_system(sof_rt5682_quirk_table);
876 mach = pdev->dev.platform_data;
878 /* A speaker amp might not be present when the quirk claims one is.
879 * Detect this via whether the machine driver match includes quirk_data.
881 if ((sof_rt5682_quirk & SOF_SPEAKER_AMP_PRESENT) && !mach->quirk_data)
882 sof_rt5682_quirk &= ~SOF_SPEAKER_AMP_PRESENT;
884 /* Detect the headset codec variant */
885 if (acpi_dev_present("RTL5682", NULL, -1))
886 sof_rt5682_quirk |= SOF_RT5682S_HEADPHONE_CODEC_PRESENT;
888 if (soc_intel_is_byt() || soc_intel_is_cht()) {
892 /* default quirk for legacy cpu */
893 sof_rt5682_quirk = SOF_RT5682_MCLK_EN |
894 SOF_RT5682_MCLK_BYTCHT_EN |
895 SOF_RT5682_SSP_CODEC(2);
898 hdmi_num = (sof_rt5682_quirk & SOF_RT5682_NUM_HDMIDEV_MASK) >>
899 SOF_RT5682_NUM_HDMIDEV_SHIFT;
900 /* default number of HDMI DAI's */
904 if (mach->mach_params.codec_mask & IDISP_CODEC_MASK)
905 ctx->idisp_codec = true;
908 /* need to get main clock from pmc */
909 if (sof_rt5682_quirk & SOF_RT5682_MCLK_BYTCHT_EN) {
910 ctx->mclk = devm_clk_get(&pdev->dev, "pmc_plt_clk_3");
911 if (IS_ERR(ctx->mclk)) {
912 ret = PTR_ERR(ctx->mclk);
915 "Failed to get MCLK from pmc_plt_clk_3: %d\n",
920 ret = clk_prepare_enable(ctx->mclk);
923 "could not configure MCLK state");
928 dev_dbg(&pdev->dev, "sof_rt5682_quirk = %lx\n", sof_rt5682_quirk);
930 ssp_amp = (sof_rt5682_quirk & SOF_RT5682_SSP_AMP_MASK) >>
931 SOF_RT5682_SSP_AMP_SHIFT;
933 ssp_codec = sof_rt5682_quirk & SOF_RT5682_SSP_CODEC_MASK;
935 /* compute number of dai links */
936 sof_audio_card_rt5682.num_links = 1 + dmic_be_num + hdmi_num;
938 if (sof_rt5682_quirk & SOF_SPEAKER_AMP_PRESENT)
939 sof_audio_card_rt5682.num_links++;
941 if (sof_rt5682_quirk & SOF_MAX98373_SPEAKER_AMP_PRESENT)
942 max_98373_set_codec_conf(&sof_audio_card_rt5682);
943 else if (sof_rt5682_quirk & SOF_RT1011_SPEAKER_AMP_PRESENT)
944 sof_rt1011_codec_conf(&sof_audio_card_rt5682);
945 else if (sof_rt5682_quirk & SOF_RT1015P_SPEAKER_AMP_PRESENT)
946 sof_rt1015p_codec_conf(&sof_audio_card_rt5682);
947 else if (sof_rt5682_quirk & SOF_MAX98390_SPEAKER_AMP_PRESENT) {
948 if (sof_rt5682_quirk & SOF_MAX98390_TWEETER_SPEAKER_PRESENT)
949 max_98390_set_codec_conf(&sof_audio_card_rt5682,
950 ARRAY_SIZE(max_98390_4spk_components));
952 max_98390_set_codec_conf(&sof_audio_card_rt5682,
953 ARRAY_SIZE(max_98390_components));
956 if (sof_rt5682_quirk & SOF_SSP_BT_OFFLOAD_PRESENT)
957 sof_audio_card_rt5682.num_links++;
959 dai_links = sof_card_dai_links_create(&pdev->dev, ssp_codec, ssp_amp,
960 dmic_be_num, hdmi_num, ctx->idisp_codec);
964 sof_audio_card_rt5682.dai_link = dai_links;
966 if (sof_rt5682_quirk & SOF_RT1015_SPEAKER_AMP_PRESENT)
967 sof_rt1015_codec_conf(&sof_audio_card_rt5682);
969 INIT_LIST_HEAD(&ctx->hdmi_pcm_list);
971 sof_audio_card_rt5682.dev = &pdev->dev;
973 /* set platform name for each dailink */
974 ret = snd_soc_fixup_dai_links_platform_name(&sof_audio_card_rt5682,
975 mach->mach_params.platform);
979 ctx->common_hdmi_codec_drv = mach->mach_params.common_hdmi_codec_drv;
981 snd_soc_card_set_drvdata(&sof_audio_card_rt5682, ctx);
983 return devm_snd_soc_register_card(&pdev->dev,
984 &sof_audio_card_rt5682);
987 static const struct platform_device_id board_ids[] = {
989 .name = "sof_rt5682",
992 .name = "tgl_mx98357_rt5682",
993 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
994 SOF_RT5682_SSP_CODEC(0) |
995 SOF_SPEAKER_AMP_PRESENT |
996 SOF_RT5682_SSP_AMP(1) |
997 SOF_RT5682_NUM_HDMIDEV(4) |
998 SOF_BT_OFFLOAD_SSP(2) |
999 SOF_SSP_BT_OFFLOAD_PRESENT),
1002 .name = "jsl_rt5682_rt1015",
1003 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1004 SOF_RT5682_MCLK_24MHZ |
1005 SOF_RT5682_SSP_CODEC(0) |
1006 SOF_SPEAKER_AMP_PRESENT |
1007 SOF_RT1015_SPEAKER_AMP_PRESENT |
1008 SOF_RT5682_SSP_AMP(1)),
1011 .name = "tgl_mx98373_rt5682",
1012 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1013 SOF_RT5682_SSP_CODEC(0) |
1014 SOF_SPEAKER_AMP_PRESENT |
1015 SOF_MAX98373_SPEAKER_AMP_PRESENT |
1016 SOF_RT5682_SSP_AMP(1) |
1017 SOF_RT5682_NUM_HDMIDEV(4) |
1018 SOF_BT_OFFLOAD_SSP(2) |
1019 SOF_SSP_BT_OFFLOAD_PRESENT),
1022 .name = "jsl_rt5682_mx98360",
1023 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1024 SOF_RT5682_MCLK_24MHZ |
1025 SOF_RT5682_SSP_CODEC(0) |
1026 SOF_SPEAKER_AMP_PRESENT |
1027 SOF_MAX98360A_SPEAKER_AMP_PRESENT |
1028 SOF_RT5682_SSP_AMP(1)),
1031 .name = "cml_rt1015_rt5682",
1032 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1033 SOF_RT5682_MCLK_24MHZ |
1034 SOF_RT5682_SSP_CODEC(0) |
1035 SOF_SPEAKER_AMP_PRESENT |
1036 SOF_RT1015_SPEAKER_AMP_PRESENT |
1037 SOF_RT5682_SSP_AMP(1)),
1040 .name = "tgl_rt1011_rt5682",
1041 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1042 SOF_RT5682_SSP_CODEC(0) |
1043 SOF_SPEAKER_AMP_PRESENT |
1044 SOF_RT1011_SPEAKER_AMP_PRESENT |
1045 SOF_RT5682_SSP_AMP(1) |
1046 SOF_RT5682_NUM_HDMIDEV(4) |
1047 SOF_BT_OFFLOAD_SSP(2) |
1048 SOF_SSP_BT_OFFLOAD_PRESENT),
1051 .name = "jsl_rt5682_rt1015p",
1052 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1053 SOF_RT5682_MCLK_24MHZ |
1054 SOF_RT5682_SSP_CODEC(0) |
1055 SOF_SPEAKER_AMP_PRESENT |
1056 SOF_RT1015P_SPEAKER_AMP_PRESENT |
1057 SOF_RT5682_SSP_AMP(1)),
1060 .name = "adl_mx98373_rt5682",
1061 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1062 SOF_RT5682_SSP_CODEC(0) |
1063 SOF_SPEAKER_AMP_PRESENT |
1064 SOF_MAX98373_SPEAKER_AMP_PRESENT |
1065 SOF_RT5682_SSP_AMP(1) |
1066 SOF_RT5682_NUM_HDMIDEV(4) |
1067 SOF_BT_OFFLOAD_SSP(2) |
1068 SOF_SSP_BT_OFFLOAD_PRESENT),
1071 .name = "adl_mx98357_rt5682",
1072 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1073 SOF_RT5682_SSP_CODEC(0) |
1074 SOF_SPEAKER_AMP_PRESENT |
1075 SOF_RT5682_SSP_AMP(2) |
1076 SOF_RT5682_NUM_HDMIDEV(4)),
1079 .name = "adl_max98390_rt5682",
1080 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1081 SOF_RT5682_SSP_CODEC(0) |
1082 SOF_SPEAKER_AMP_PRESENT |
1083 SOF_MAX98390_SPEAKER_AMP_PRESENT |
1084 SOF_RT5682_SSP_AMP(1) |
1085 SOF_RT5682_NUM_HDMIDEV(4) |
1086 SOF_BT_OFFLOAD_SSP(2) |
1087 SOF_SSP_BT_OFFLOAD_PRESENT),
1090 .name = "adl_mx98360_rt5682",
1091 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1092 SOF_RT5682_SSP_CODEC(0) |
1093 SOF_SPEAKER_AMP_PRESENT |
1094 SOF_MAX98360A_SPEAKER_AMP_PRESENT |
1095 SOF_RT5682_SSP_AMP(1) |
1096 SOF_RT5682_NUM_HDMIDEV(4) |
1097 SOF_BT_OFFLOAD_SSP(2) |
1098 SOF_SSP_BT_OFFLOAD_PRESENT),
1101 .name = "adl_rt5682",
1102 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1103 SOF_RT5682_SSP_CODEC(0) |
1104 SOF_RT5682_NUM_HDMIDEV(4) |
1105 SOF_BT_OFFLOAD_SSP(2) |
1106 SOF_SSP_BT_OFFLOAD_PRESENT),
1109 .name = "adl_rt1019_rt5682",
1110 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1111 SOF_RT5682_SSP_CODEC(0) |
1112 SOF_SPEAKER_AMP_PRESENT |
1113 SOF_RT1019_SPEAKER_AMP_PRESENT |
1114 SOF_RT5682_SSP_AMP(1) |
1115 SOF_RT5682_NUM_HDMIDEV(4) |
1116 SOF_BT_OFFLOAD_SSP(2) |
1117 SOF_SSP_BT_OFFLOAD_PRESENT),
1120 .name = "rpl_mx98360_rt5682",
1121 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1122 SOF_RT5682_SSP_CODEC(0) |
1123 SOF_SPEAKER_AMP_PRESENT |
1124 SOF_MAX98360A_SPEAKER_AMP_PRESENT |
1125 SOF_RT5682_SSP_AMP(1) |
1126 SOF_RT5682_NUM_HDMIDEV(4) |
1127 SOF_BT_OFFLOAD_SSP(2) |
1128 SOF_SSP_BT_OFFLOAD_PRESENT),
1131 .name = "mtl_mx98357_rt5682",
1132 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1133 SOF_RT5682_SSP_CODEC(0) |
1134 SOF_SPEAKER_AMP_PRESENT |
1135 SOF_RT5682_SSP_AMP(1) |
1136 SOF_RT5682_NUM_HDMIDEV(4) |
1137 SOF_BT_OFFLOAD_SSP(2) |
1138 SOF_SSP_BT_OFFLOAD_PRESENT),
1141 .name = "mtl_mx98360_rt5682",
1142 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1143 SOF_RT5682_SSP_CODEC(0) |
1144 SOF_SPEAKER_AMP_PRESENT |
1145 SOF_MAX98360A_SPEAKER_AMP_PRESENT |
1146 SOF_RT5682_SSP_AMP(1) |
1147 SOF_RT5682_NUM_HDMIDEV(4)),
1150 .name = "jsl_rt5682",
1151 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1152 SOF_RT5682_MCLK_24MHZ |
1153 SOF_RT5682_SSP_CODEC(0)),
1157 MODULE_DEVICE_TABLE(platform, board_ids);
1159 static struct platform_driver sof_audio = {
1160 .probe = sof_audio_probe,
1162 .name = "sof_rt5682",
1163 .pm = &snd_soc_pm_ops,
1165 .id_table = board_ids,
1167 module_platform_driver(sof_audio)
1169 /* Module information */
1170 MODULE_DESCRIPTION("SOF Audio Machine driver");
1175 MODULE_LICENSE("GPL v2");
1176 MODULE_IMPORT_NS(SND_SOC_INTEL_HDA_DSP_COMMON);
1177 MODULE_IMPORT_NS(SND_SOC_INTEL_SOF_MAXIM_COMMON);
1178 MODULE_IMPORT_NS(SND_SOC_INTEL_SOF_REALTEK_COMMON);