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"),
231 .driver_data = (void *)(SOF_RT5682_MCLK_EN |
232 SOF_RT5682_SSP_CODEC(2) |
233 SOF_SPEAKER_AMP_PRESENT |
234 SOF_RT5682_SSP_AMP(0) |
235 SOF_RT5682_NUM_HDMIDEV(4)
241 static int sof_hdmi_init(struct snd_soc_pcm_runtime *rtd)
243 struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
244 struct snd_soc_dai *dai = asoc_rtd_to_codec(rtd, 0);
245 struct sof_hdmi_pcm *pcm;
247 pcm = devm_kzalloc(rtd->card->dev, sizeof(*pcm), GFP_KERNEL);
251 /* dai_link id is 1:1 mapped to the PCM device */
252 pcm->device = rtd->dai_link->id;
253 pcm->codec_dai = dai;
255 list_add_tail(&pcm->head, &ctx->hdmi_pcm_list);
260 static struct snd_soc_jack_pin jack_pins[] = {
262 .pin = "Headphone Jack",
263 .mask = SND_JACK_HEADPHONE,
266 .pin = "Headset Mic",
267 .mask = SND_JACK_MICROPHONE,
271 static int sof_rt5682_codec_init(struct snd_soc_pcm_runtime *rtd)
273 struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
274 struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
275 struct snd_soc_jack *jack;
278 /* need to enable ASRC function for 24MHz mclk rate */
279 if ((sof_rt5682_quirk & SOF_RT5682_MCLK_EN) &&
280 (sof_rt5682_quirk & SOF_RT5682_MCLK_24MHZ)) {
281 if (sof_rt5682_quirk & SOF_RT5682S_HEADPHONE_CODEC_PRESENT)
282 rt5682s_sel_asrc_clk_src(component,
283 RT5682S_DA_STEREO1_FILTER |
284 RT5682S_AD_STEREO1_FILTER,
285 RT5682S_CLK_SEL_I2S1_ASRC);
287 rt5682_sel_asrc_clk_src(component,
288 RT5682_DA_STEREO1_FILTER |
289 RT5682_AD_STEREO1_FILTER,
290 RT5682_CLK_SEL_I2S1_ASRC);
293 if (sof_rt5682_quirk & SOF_RT5682_MCLK_BYTCHT_EN) {
295 * The firmware might enable the clock at
296 * boot (this information may or may not
297 * be reflected in the enable clock register).
298 * To change the rate we must disable the clock
299 * first to cover these cases. Due to common
300 * clock framework restrictions that do not allow
301 * to disable a clock that has not been enabled,
302 * we need to enable the clock first.
304 ret = clk_prepare_enable(ctx->mclk);
306 clk_disable_unprepare(ctx->mclk);
308 ret = clk_set_rate(ctx->mclk, 19200000);
311 dev_err(rtd->dev, "unable to set MCLK rate\n");
315 * Headset buttons map to the google Reference headset.
316 * These can be configured by userspace.
318 ret = snd_soc_card_jack_new_pins(rtd->card, "Headset Jack",
319 SND_JACK_HEADSET | SND_JACK_BTN_0 |
320 SND_JACK_BTN_1 | SND_JACK_BTN_2 |
324 ARRAY_SIZE(jack_pins));
326 dev_err(rtd->dev, "Headset Jack creation failed: %d\n", ret);
330 jack = &ctx->sof_headset;
332 snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
333 snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
334 snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
335 snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
336 ret = snd_soc_component_set_jack(component, jack, NULL);
339 dev_err(rtd->dev, "Headset Jack call-back failed: %d\n", ret);
346 static void sof_rt5682_codec_exit(struct snd_soc_pcm_runtime *rtd)
348 struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
350 snd_soc_component_set_jack(component, NULL, NULL);
353 static int sof_rt5682_hw_params(struct snd_pcm_substream *substream,
354 struct snd_pcm_hw_params *params)
356 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
357 struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
358 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
359 int pll_id, pll_source, pll_in, pll_out, clk_id, ret;
361 if (sof_rt5682_quirk & SOF_RT5682_MCLK_EN) {
362 if (sof_rt5682_quirk & SOF_RT5682_MCLK_BYTCHT_EN) {
363 ret = clk_prepare_enable(ctx->mclk);
366 "could not configure MCLK state");
371 if (sof_rt5682_quirk & SOF_RT5682S_HEADPHONE_CODEC_PRESENT)
372 pll_source = RT5682S_PLL_S_MCLK;
374 pll_source = RT5682_PLL1_S_MCLK;
376 /* get the tplg configured mclk. */
377 pll_in = sof_dai_get_mclk(rtd);
379 /* mclk from the quirk is the first choice */
380 if (sof_rt5682_quirk & SOF_RT5682_MCLK_24MHZ) {
381 if (pll_in != 24000000)
382 dev_warn(rtd->dev, "configure wrong mclk in tplg, please use 24MHz.\n");
384 } else if (pll_in == 0) {
385 /* use default mclk if not specified correct in topology */
387 } else if (pll_in < 0) {
391 if (sof_rt5682_quirk & SOF_RT5682S_HEADPHONE_CODEC_PRESENT)
392 pll_source = RT5682S_PLL_S_BCLK1;
394 pll_source = RT5682_PLL1_S_BCLK1;
396 pll_in = params_rate(params) * 50;
399 if (sof_rt5682_quirk & SOF_RT5682S_HEADPHONE_CODEC_PRESENT) {
400 pll_id = RT5682S_PLL2;
401 clk_id = RT5682S_SCLK_S_PLL2;
403 pll_id = RT5682_PLL1;
404 clk_id = RT5682_SCLK_S_PLL1;
407 pll_out = params_rate(params) * 512;
409 /* when MCLK is 512FS, no need to set PLL configuration additionally. */
410 if (pll_in == pll_out)
411 clk_id = RT5682S_SCLK_S_MCLK;
413 /* Configure pll for codec */
414 ret = snd_soc_dai_set_pll(codec_dai, pll_id, pll_source, pll_in,
417 dev_err(rtd->dev, "snd_soc_dai_set_pll err = %d\n", ret);
420 /* Configure sysclk for codec */
421 ret = snd_soc_dai_set_sysclk(codec_dai, clk_id,
422 pll_out, SND_SOC_CLOCK_IN);
424 dev_err(rtd->dev, "snd_soc_dai_set_sysclk err = %d\n", ret);
427 * slot_width should equal or large than data length, set them
430 ret = snd_soc_dai_set_tdm_slot(codec_dai, 0x0, 0x0, 2,
431 params_width(params));
433 dev_err(rtd->dev, "set TDM slot err:%d\n", ret);
440 static struct snd_soc_ops sof_rt5682_ops = {
441 .hw_params = sof_rt5682_hw_params,
444 static struct snd_soc_dai_link_component platform_component[] = {
446 /* name might be overridden during probe */
447 .name = "0000:00:1f.3"
451 static int sof_card_late_probe(struct snd_soc_card *card)
453 struct sof_card_private *ctx = snd_soc_card_get_drvdata(card);
454 struct snd_soc_component *component = NULL;
455 struct snd_soc_dapm_context *dapm = &card->dapm;
456 char jack_name[NAME_SIZE];
457 struct sof_hdmi_pcm *pcm;
460 if (sof_rt5682_quirk & SOF_MAX98373_SPEAKER_AMP_PRESENT) {
461 /* Disable Left and Right Spk pin after boot */
462 snd_soc_dapm_disable_pin(dapm, "Left Spk");
463 snd_soc_dapm_disable_pin(dapm, "Right Spk");
464 err = snd_soc_dapm_sync(dapm);
469 /* HDMI is not supported by SOF on Baytrail/CherryTrail */
470 if (is_legacy_cpu || !ctx->idisp_codec)
473 if (list_empty(&ctx->hdmi_pcm_list))
476 if (ctx->common_hdmi_codec_drv) {
477 pcm = list_first_entry(&ctx->hdmi_pcm_list, struct sof_hdmi_pcm,
479 component = pcm->codec_dai->component;
480 return hda_dsp_hdmi_build_controls(card, component);
483 list_for_each_entry(pcm, &ctx->hdmi_pcm_list, head) {
484 component = pcm->codec_dai->component;
485 snprintf(jack_name, sizeof(jack_name),
486 "HDMI/DP, pcm=%d Jack", pcm->device);
487 err = snd_soc_card_jack_new(card, jack_name,
488 SND_JACK_AVOUT, &pcm->hdmi_jack);
493 err = hdac_hdmi_jack_init(pcm->codec_dai, pcm->device,
499 return hdac_hdmi_jack_port_init(component, &card->dapm);
502 static const struct snd_kcontrol_new sof_controls[] = {
503 SOC_DAPM_PIN_SWITCH("Headphone Jack"),
504 SOC_DAPM_PIN_SWITCH("Headset Mic"),
505 SOC_DAPM_PIN_SWITCH("Left Spk"),
506 SOC_DAPM_PIN_SWITCH("Right Spk"),
510 static const struct snd_soc_dapm_widget sof_widgets[] = {
511 SND_SOC_DAPM_HP("Headphone Jack", NULL),
512 SND_SOC_DAPM_MIC("Headset Mic", NULL),
513 SND_SOC_DAPM_SPK("Left Spk", NULL),
514 SND_SOC_DAPM_SPK("Right Spk", NULL),
517 static const struct snd_soc_dapm_widget dmic_widgets[] = {
518 SND_SOC_DAPM_MIC("SoC DMIC", NULL),
521 static const struct snd_soc_dapm_route sof_map[] = {
522 /* HP jack connectors - unknown if we have jack detection */
523 { "Headphone Jack", NULL, "HPOL" },
524 { "Headphone Jack", NULL, "HPOR" },
527 { "IN1P", NULL, "Headset Mic" },
530 static const struct snd_soc_dapm_route dmic_map[] = {
532 {"DMic", NULL, "SoC DMIC"},
535 static int dmic_init(struct snd_soc_pcm_runtime *rtd)
537 struct snd_soc_card *card = rtd->card;
540 ret = snd_soc_dapm_new_controls(&card->dapm, dmic_widgets,
541 ARRAY_SIZE(dmic_widgets));
543 dev_err(card->dev, "DMic widget addition failed: %d\n", ret);
544 /* Don't need to add routes if widget addition failed */
548 ret = snd_soc_dapm_add_routes(&card->dapm, dmic_map,
549 ARRAY_SIZE(dmic_map));
552 dev_err(card->dev, "DMic map addition failed: %d\n", ret);
557 /* sof audio machine driver for rt5682 codec */
558 static struct snd_soc_card sof_audio_card_rt5682 = {
559 .name = "rt5682", /* the sof- prefix is added by the core */
560 .owner = THIS_MODULE,
561 .controls = sof_controls,
562 .num_controls = ARRAY_SIZE(sof_controls),
563 .dapm_widgets = sof_widgets,
564 .num_dapm_widgets = ARRAY_SIZE(sof_widgets),
565 .dapm_routes = sof_map,
566 .num_dapm_routes = ARRAY_SIZE(sof_map),
567 .fully_routed = true,
568 .late_probe = sof_card_late_probe,
571 static struct snd_soc_dai_link_component rt5682_component[] = {
573 .name = "i2c-10EC5682:00",
574 .dai_name = "rt5682-aif1",
578 static struct snd_soc_dai_link_component rt5682s_component[] = {
580 .name = "i2c-RTL5682:00",
581 .dai_name = "rt5682s-aif1",
585 static struct snd_soc_dai_link_component dmic_component[] = {
587 .name = "dmic-codec",
588 .dai_name = "dmic-hifi",
592 static struct snd_soc_dai_link_component dummy_component[] = {
594 .name = "snd-soc-dummy",
595 .dai_name = "snd-soc-dummy-dai",
599 #define IDISP_CODEC_MASK 0x4
601 static struct snd_soc_dai_link *sof_card_dai_links_create(struct device *dev,
608 struct snd_soc_dai_link_component *idisp_components;
609 struct snd_soc_dai_link_component *cpus;
610 struct snd_soc_dai_link *links;
613 links = devm_kcalloc(dev, sof_audio_card_rt5682.num_links,
614 sizeof(struct snd_soc_dai_link), GFP_KERNEL);
615 cpus = devm_kcalloc(dev, sof_audio_card_rt5682.num_links,
616 sizeof(struct snd_soc_dai_link_component), GFP_KERNEL);
621 links[id].name = devm_kasprintf(dev, GFP_KERNEL,
622 "SSP%d-Codec", ssp_codec);
627 if (sof_rt5682_quirk & SOF_RT5682S_HEADPHONE_CODEC_PRESENT) {
628 links[id].codecs = rt5682s_component;
629 links[id].num_codecs = ARRAY_SIZE(rt5682s_component);
631 links[id].codecs = rt5682_component;
632 links[id].num_codecs = ARRAY_SIZE(rt5682_component);
634 links[id].platforms = platform_component;
635 links[id].num_platforms = ARRAY_SIZE(platform_component);
636 links[id].init = sof_rt5682_codec_init;
637 links[id].exit = sof_rt5682_codec_exit;
638 links[id].ops = &sof_rt5682_ops;
639 links[id].dpcm_playback = 1;
640 links[id].dpcm_capture = 1;
641 links[id].no_pcm = 1;
642 links[id].cpus = &cpus[id];
643 links[id].num_cpus = 1;
645 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
648 if (!links[id].cpus->dai_name)
652 * Currently, On SKL+ platforms MCLK will be turned off in sof
653 * runtime suspended, and it will go into runtime suspended
654 * right after playback is stop. However, rt5682 will output
655 * static noise if sysclk turns off during playback. Set
656 * ignore_pmdown_time to power down rt5682 immediately and
658 * It can be removed once we can control MCLK by driver.
660 links[id].ignore_pmdown_time = 1;
661 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
664 if (!links[id].cpus->dai_name)
670 if (dmic_be_num > 0) {
671 /* at least we have dmic01 */
672 links[id].name = "dmic01";
673 links[id].cpus = &cpus[id];
674 links[id].cpus->dai_name = "DMIC01 Pin";
675 links[id].init = dmic_init;
676 if (dmic_be_num > 1) {
677 /* set up 2 BE links at most */
678 links[id + 1].name = "dmic16k";
679 links[id + 1].cpus = &cpus[id + 1];
680 links[id + 1].cpus->dai_name = "DMIC16k Pin";
685 for (i = 0; i < dmic_be_num; i++) {
687 links[id].num_cpus = 1;
688 links[id].codecs = dmic_component;
689 links[id].num_codecs = ARRAY_SIZE(dmic_component);
690 links[id].platforms = platform_component;
691 links[id].num_platforms = ARRAY_SIZE(platform_component);
692 links[id].ignore_suspend = 1;
693 links[id].dpcm_capture = 1;
694 links[id].no_pcm = 1;
700 idisp_components = devm_kcalloc(dev,
702 sizeof(struct snd_soc_dai_link_component),
704 if (!idisp_components)
707 for (i = 1; i <= hdmi_num; i++) {
708 links[id].name = devm_kasprintf(dev, GFP_KERNEL,
714 links[id].cpus = &cpus[id];
715 links[id].num_cpus = 1;
716 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
718 if (!links[id].cpus->dai_name)
722 idisp_components[i - 1].name = "ehdaudio0D2";
723 idisp_components[i - 1].dai_name = devm_kasprintf(dev,
727 if (!idisp_components[i - 1].dai_name)
730 idisp_components[i - 1].name = "snd-soc-dummy";
731 idisp_components[i - 1].dai_name = "snd-soc-dummy-dai";
734 links[id].codecs = &idisp_components[i - 1];
735 links[id].num_codecs = 1;
736 links[id].platforms = platform_component;
737 links[id].num_platforms = ARRAY_SIZE(platform_component);
738 links[id].init = sof_hdmi_init;
739 links[id].dpcm_playback = 1;
740 links[id].no_pcm = 1;
745 if (sof_rt5682_quirk & SOF_SPEAKER_AMP_PRESENT) {
746 links[id].name = devm_kasprintf(dev, GFP_KERNEL,
747 "SSP%d-Codec", ssp_amp);
752 if (sof_rt5682_quirk & SOF_RT1015_SPEAKER_AMP_PRESENT) {
753 sof_rt1015_dai_link(&links[id]);
754 } else if (sof_rt5682_quirk & SOF_RT1015P_SPEAKER_AMP_PRESENT) {
755 sof_rt1015p_dai_link(&links[id]);
756 } else if (sof_rt5682_quirk & SOF_RT1019_SPEAKER_AMP_PRESENT) {
757 sof_rt1019p_dai_link(&links[id]);
758 } else if (sof_rt5682_quirk &
759 SOF_MAX98373_SPEAKER_AMP_PRESENT) {
760 links[id].codecs = max_98373_components;
761 links[id].num_codecs = ARRAY_SIZE(max_98373_components);
762 links[id].init = max_98373_spk_codec_init;
763 links[id].ops = &max_98373_ops;
764 /* feedback stream */
765 links[id].dpcm_capture = 1;
766 } else if (sof_rt5682_quirk &
767 SOF_MAX98360A_SPEAKER_AMP_PRESENT) {
768 max_98360a_dai_link(&links[id]);
769 } else if (sof_rt5682_quirk &
770 SOF_RT1011_SPEAKER_AMP_PRESENT) {
771 sof_rt1011_dai_link(&links[id]);
772 } else if (sof_rt5682_quirk &
773 SOF_MAX98390_SPEAKER_AMP_PRESENT) {
774 if (sof_rt5682_quirk &
775 SOF_MAX98390_TWEETER_SPEAKER_PRESENT) {
776 links[id].codecs = max_98390_4spk_components;
777 links[id].num_codecs = ARRAY_SIZE(max_98390_4spk_components);
779 links[id].codecs = max_98390_components;
780 links[id].num_codecs = ARRAY_SIZE(max_98390_components);
782 links[id].init = max_98390_spk_codec_init;
783 links[id].ops = &max_98390_ops;
784 links[id].dpcm_capture = 1;
787 max_98357a_dai_link(&links[id]);
789 links[id].platforms = platform_component;
790 links[id].num_platforms = ARRAY_SIZE(platform_component);
791 links[id].dpcm_playback = 1;
792 links[id].no_pcm = 1;
793 links[id].cpus = &cpus[id];
794 links[id].num_cpus = 1;
796 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
799 if (!links[id].cpus->dai_name)
803 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
806 if (!links[id].cpus->dai_name)
812 /* BT audio offload */
813 if (sof_rt5682_quirk & SOF_SSP_BT_OFFLOAD_PRESENT) {
814 int port = (sof_rt5682_quirk & SOF_BT_OFFLOAD_SSP_MASK) >>
815 SOF_BT_OFFLOAD_SSP_SHIFT;
818 links[id].cpus = &cpus[id];
819 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
821 if (!links[id].cpus->dai_name)
823 links[id].name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-BT", port);
826 links[id].codecs = dummy_component;
827 links[id].num_codecs = ARRAY_SIZE(dummy_component);
828 links[id].platforms = platform_component;
829 links[id].num_platforms = ARRAY_SIZE(platform_component);
830 links[id].dpcm_playback = 1;
831 links[id].dpcm_capture = 1;
832 links[id].no_pcm = 1;
833 links[id].num_cpus = 1;
841 static int sof_audio_probe(struct platform_device *pdev)
843 struct snd_soc_dai_link *dai_links;
844 struct snd_soc_acpi_mach *mach;
845 struct sof_card_private *ctx;
846 int dmic_be_num, hdmi_num;
847 int ret, ssp_amp, ssp_codec;
849 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
853 if (pdev->id_entry && pdev->id_entry->driver_data)
854 sof_rt5682_quirk = (unsigned long)pdev->id_entry->driver_data;
856 dmi_check_system(sof_rt5682_quirk_table);
858 mach = pdev->dev.platform_data;
860 /* A speaker amp might not be present when the quirk claims one is.
861 * Detect this via whether the machine driver match includes quirk_data.
863 if ((sof_rt5682_quirk & SOF_SPEAKER_AMP_PRESENT) && !mach->quirk_data)
864 sof_rt5682_quirk &= ~SOF_SPEAKER_AMP_PRESENT;
866 /* Detect the headset codec variant */
867 if (acpi_dev_present("RTL5682", NULL, -1))
868 sof_rt5682_quirk |= SOF_RT5682S_HEADPHONE_CODEC_PRESENT;
870 if (soc_intel_is_byt() || soc_intel_is_cht()) {
874 /* default quirk for legacy cpu */
875 sof_rt5682_quirk = SOF_RT5682_MCLK_EN |
876 SOF_RT5682_MCLK_BYTCHT_EN |
877 SOF_RT5682_SSP_CODEC(2);
880 hdmi_num = (sof_rt5682_quirk & SOF_RT5682_NUM_HDMIDEV_MASK) >>
881 SOF_RT5682_NUM_HDMIDEV_SHIFT;
882 /* default number of HDMI DAI's */
886 if (mach->mach_params.codec_mask & IDISP_CODEC_MASK)
887 ctx->idisp_codec = true;
890 /* need to get main clock from pmc */
891 if (sof_rt5682_quirk & SOF_RT5682_MCLK_BYTCHT_EN) {
892 ctx->mclk = devm_clk_get(&pdev->dev, "pmc_plt_clk_3");
893 if (IS_ERR(ctx->mclk)) {
894 ret = PTR_ERR(ctx->mclk);
897 "Failed to get MCLK from pmc_plt_clk_3: %d\n",
902 ret = clk_prepare_enable(ctx->mclk);
905 "could not configure MCLK state");
910 dev_dbg(&pdev->dev, "sof_rt5682_quirk = %lx\n", sof_rt5682_quirk);
912 ssp_amp = (sof_rt5682_quirk & SOF_RT5682_SSP_AMP_MASK) >>
913 SOF_RT5682_SSP_AMP_SHIFT;
915 ssp_codec = sof_rt5682_quirk & SOF_RT5682_SSP_CODEC_MASK;
917 /* compute number of dai links */
918 sof_audio_card_rt5682.num_links = 1 + dmic_be_num + hdmi_num;
920 if (sof_rt5682_quirk & SOF_SPEAKER_AMP_PRESENT)
921 sof_audio_card_rt5682.num_links++;
923 if (sof_rt5682_quirk & SOF_MAX98373_SPEAKER_AMP_PRESENT)
924 max_98373_set_codec_conf(&sof_audio_card_rt5682);
925 else if (sof_rt5682_quirk & SOF_RT1011_SPEAKER_AMP_PRESENT)
926 sof_rt1011_codec_conf(&sof_audio_card_rt5682);
927 else if (sof_rt5682_quirk & SOF_RT1015P_SPEAKER_AMP_PRESENT)
928 sof_rt1015p_codec_conf(&sof_audio_card_rt5682);
929 else if (sof_rt5682_quirk & SOF_MAX98390_SPEAKER_AMP_PRESENT) {
930 if (sof_rt5682_quirk & SOF_MAX98390_TWEETER_SPEAKER_PRESENT)
931 max_98390_set_codec_conf(&sof_audio_card_rt5682,
932 ARRAY_SIZE(max_98390_4spk_components));
934 max_98390_set_codec_conf(&sof_audio_card_rt5682,
935 ARRAY_SIZE(max_98390_components));
938 if (sof_rt5682_quirk & SOF_SSP_BT_OFFLOAD_PRESENT)
939 sof_audio_card_rt5682.num_links++;
941 dai_links = sof_card_dai_links_create(&pdev->dev, ssp_codec, ssp_amp,
942 dmic_be_num, hdmi_num, ctx->idisp_codec);
946 sof_audio_card_rt5682.dai_link = dai_links;
948 if (sof_rt5682_quirk & SOF_RT1015_SPEAKER_AMP_PRESENT)
949 sof_rt1015_codec_conf(&sof_audio_card_rt5682);
951 INIT_LIST_HEAD(&ctx->hdmi_pcm_list);
953 sof_audio_card_rt5682.dev = &pdev->dev;
955 /* set platform name for each dailink */
956 ret = snd_soc_fixup_dai_links_platform_name(&sof_audio_card_rt5682,
957 mach->mach_params.platform);
961 ctx->common_hdmi_codec_drv = mach->mach_params.common_hdmi_codec_drv;
963 snd_soc_card_set_drvdata(&sof_audio_card_rt5682, ctx);
965 return devm_snd_soc_register_card(&pdev->dev,
966 &sof_audio_card_rt5682);
969 static const struct platform_device_id board_ids[] = {
971 .name = "sof_rt5682",
974 .name = "tgl_mx98357_rt5682",
975 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
976 SOF_RT5682_SSP_CODEC(0) |
977 SOF_SPEAKER_AMP_PRESENT |
978 SOF_RT5682_SSP_AMP(1) |
979 SOF_RT5682_NUM_HDMIDEV(4) |
980 SOF_BT_OFFLOAD_SSP(2) |
981 SOF_SSP_BT_OFFLOAD_PRESENT),
984 .name = "jsl_rt5682_rt1015",
985 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
986 SOF_RT5682_MCLK_24MHZ |
987 SOF_RT5682_SSP_CODEC(0) |
988 SOF_SPEAKER_AMP_PRESENT |
989 SOF_RT1015_SPEAKER_AMP_PRESENT |
990 SOF_RT5682_SSP_AMP(1)),
993 .name = "tgl_mx98373_rt5682",
994 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
995 SOF_RT5682_SSP_CODEC(0) |
996 SOF_SPEAKER_AMP_PRESENT |
997 SOF_MAX98373_SPEAKER_AMP_PRESENT |
998 SOF_RT5682_SSP_AMP(1) |
999 SOF_RT5682_NUM_HDMIDEV(4) |
1000 SOF_BT_OFFLOAD_SSP(2) |
1001 SOF_SSP_BT_OFFLOAD_PRESENT),
1004 .name = "jsl_rt5682_mx98360",
1005 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1006 SOF_RT5682_MCLK_24MHZ |
1007 SOF_RT5682_SSP_CODEC(0) |
1008 SOF_SPEAKER_AMP_PRESENT |
1009 SOF_MAX98360A_SPEAKER_AMP_PRESENT |
1010 SOF_RT5682_SSP_AMP(1)),
1013 .name = "cml_rt1015_rt5682",
1014 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1015 SOF_RT5682_MCLK_24MHZ |
1016 SOF_RT5682_SSP_CODEC(0) |
1017 SOF_SPEAKER_AMP_PRESENT |
1018 SOF_RT1015_SPEAKER_AMP_PRESENT |
1019 SOF_RT5682_SSP_AMP(1)),
1022 .name = "tgl_rt1011_rt5682",
1023 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1024 SOF_RT5682_SSP_CODEC(0) |
1025 SOF_SPEAKER_AMP_PRESENT |
1026 SOF_RT1011_SPEAKER_AMP_PRESENT |
1027 SOF_RT5682_SSP_AMP(1) |
1028 SOF_RT5682_NUM_HDMIDEV(4) |
1029 SOF_BT_OFFLOAD_SSP(2) |
1030 SOF_SSP_BT_OFFLOAD_PRESENT),
1033 .name = "jsl_rt5682_rt1015p",
1034 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1035 SOF_RT5682_MCLK_24MHZ |
1036 SOF_RT5682_SSP_CODEC(0) |
1037 SOF_SPEAKER_AMP_PRESENT |
1038 SOF_RT1015P_SPEAKER_AMP_PRESENT |
1039 SOF_RT5682_SSP_AMP(1)),
1042 .name = "adl_mx98373_rt5682",
1043 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1044 SOF_RT5682_SSP_CODEC(0) |
1045 SOF_SPEAKER_AMP_PRESENT |
1046 SOF_MAX98373_SPEAKER_AMP_PRESENT |
1047 SOF_RT5682_SSP_AMP(1) |
1048 SOF_RT5682_NUM_HDMIDEV(4) |
1049 SOF_BT_OFFLOAD_SSP(2) |
1050 SOF_SSP_BT_OFFLOAD_PRESENT),
1053 .name = "adl_mx98357_rt5682",
1054 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1055 SOF_RT5682_SSP_CODEC(0) |
1056 SOF_SPEAKER_AMP_PRESENT |
1057 SOF_RT5682_SSP_AMP(2) |
1058 SOF_RT5682_NUM_HDMIDEV(4)),
1061 .name = "adl_max98390_rt5682",
1062 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1063 SOF_RT5682_SSP_CODEC(0) |
1064 SOF_SPEAKER_AMP_PRESENT |
1065 SOF_MAX98390_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_mx98360_rt5682",
1073 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1074 SOF_RT5682_SSP_CODEC(0) |
1075 SOF_SPEAKER_AMP_PRESENT |
1076 SOF_MAX98360A_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_rt5682",
1084 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1085 SOF_RT5682_SSP_CODEC(0) |
1086 SOF_RT5682_NUM_HDMIDEV(4) |
1087 SOF_BT_OFFLOAD_SSP(2) |
1088 SOF_SSP_BT_OFFLOAD_PRESENT),
1091 .name = "adl_rt1019_rt5682",
1092 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1093 SOF_RT5682_SSP_CODEC(0) |
1094 SOF_SPEAKER_AMP_PRESENT |
1095 SOF_RT1019_SPEAKER_AMP_PRESENT |
1096 SOF_RT5682_SSP_AMP(1) |
1097 SOF_RT5682_NUM_HDMIDEV(4)),
1100 .name = "mtl_mx98357_rt5682",
1101 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1102 SOF_RT5682_SSP_CODEC(0) |
1103 SOF_SPEAKER_AMP_PRESENT |
1104 SOF_RT5682_SSP_AMP(1) |
1105 SOF_RT5682_NUM_HDMIDEV(4)),
1109 MODULE_DEVICE_TABLE(platform, board_ids);
1111 static struct platform_driver sof_audio = {
1112 .probe = sof_audio_probe,
1114 .name = "sof_rt5682",
1115 .pm = &snd_soc_pm_ops,
1117 .id_table = board_ids,
1119 module_platform_driver(sof_audio)
1121 /* Module information */
1122 MODULE_DESCRIPTION("SOF Audio Machine driver");
1123 MODULE_AUTHOR("Bard Liao <bard.liao@intel.com>");
1124 MODULE_AUTHOR("Sathya Prakash M R <sathya.prakash.m.r@intel.com>");
1125 MODULE_AUTHOR("Brent Lu <brent.lu@intel.com>");
1126 MODULE_AUTHOR("Mac Chiang <mac.chiang@intel.com>");
1127 MODULE_LICENSE("GPL v2");
1128 MODULE_IMPORT_NS(SND_SOC_INTEL_HDA_DSP_COMMON);
1129 MODULE_IMPORT_NS(SND_SOC_INTEL_SOF_MAXIM_COMMON);
1130 MODULE_IMPORT_NS(SND_SOC_INTEL_SOF_REALTEK_COMMON);