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_RT1015_SPEAKER_AMP_100FS BIT(15)
50 #define SOF_RT1015P_SPEAKER_AMP_PRESENT BIT(16)
51 #define SOF_MAX98373_SPEAKER_AMP_PRESENT BIT(17)
52 #define SOF_MAX98360A_SPEAKER_AMP_PRESENT BIT(18)
54 /* BT audio offload: reserve 3 bits for future */
55 #define SOF_BT_OFFLOAD_SSP_SHIFT 19
56 #define SOF_BT_OFFLOAD_SSP_MASK (GENMASK(21, 19))
57 #define SOF_BT_OFFLOAD_SSP(quirk) \
58 (((quirk) << SOF_BT_OFFLOAD_SSP_SHIFT) & SOF_BT_OFFLOAD_SSP_MASK)
59 #define SOF_SSP_BT_OFFLOAD_PRESENT BIT(22)
60 #define SOF_RT5682S_HEADPHONE_CODEC_PRESENT BIT(23)
61 #define SOF_MAX98390_SPEAKER_AMP_PRESENT BIT(24)
62 #define SOF_MAX98390_TWEETER_SPEAKER_PRESENT BIT(25)
63 #define SOF_RT1019_SPEAKER_AMP_PRESENT BIT(26)
66 /* Default: MCLK on, MCLK 19.2M, SSP0 */
67 static unsigned long sof_rt5682_quirk = SOF_RT5682_MCLK_EN |
68 SOF_RT5682_SSP_CODEC(0);
70 static int is_legacy_cpu;
72 static struct snd_soc_jack sof_hdmi[3];
75 struct list_head head;
76 struct snd_soc_dai *codec_dai;
80 struct sof_card_private {
82 struct snd_soc_jack sof_headset;
83 struct list_head hdmi_pcm_list;
84 bool common_hdmi_codec_drv;
88 static int sof_rt5682_quirk_cb(const struct dmi_system_id *id)
90 sof_rt5682_quirk = (unsigned long)id->driver_data;
94 static const struct dmi_system_id sof_rt5682_quirk_table[] = {
96 .callback = sof_rt5682_quirk_cb,
98 DMI_MATCH(DMI_SYS_VENDOR, "Circuitco"),
99 DMI_MATCH(DMI_PRODUCT_NAME, "Minnowboard Max"),
101 .driver_data = (void *)(SOF_RT5682_SSP_CODEC(2)),
104 .callback = sof_rt5682_quirk_cb,
106 DMI_MATCH(DMI_SYS_VENDOR, "AAEON"),
107 DMI_MATCH(DMI_PRODUCT_NAME, "UP-CHT01"),
109 .driver_data = (void *)(SOF_RT5682_SSP_CODEC(2)),
112 .callback = sof_rt5682_quirk_cb,
114 DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
115 DMI_MATCH(DMI_PRODUCT_NAME, "WhiskeyLake Client"),
117 .driver_data = (void *)(SOF_RT5682_MCLK_EN |
118 SOF_RT5682_MCLK_24MHZ |
119 SOF_RT5682_SSP_CODEC(1)),
123 * Dooly is hatch family but using rt1015 amp so it
124 * requires a quirk before "Google_Hatch".
126 .callback = sof_rt5682_quirk_cb,
128 DMI_MATCH(DMI_SYS_VENDOR, "HP"),
129 DMI_MATCH(DMI_PRODUCT_NAME, "Dooly"),
131 .driver_data = (void *)(SOF_RT5682_MCLK_EN |
132 SOF_RT5682_MCLK_24MHZ |
133 SOF_RT5682_SSP_CODEC(0) |
134 SOF_SPEAKER_AMP_PRESENT |
135 SOF_RT1015_SPEAKER_AMP_PRESENT |
136 SOF_RT1015_SPEAKER_AMP_100FS |
137 SOF_RT5682_SSP_AMP(1)),
140 .callback = sof_rt5682_quirk_cb,
142 DMI_MATCH(DMI_PRODUCT_FAMILY, "Google_Hatch"),
144 .driver_data = (void *)(SOF_RT5682_MCLK_EN |
145 SOF_RT5682_MCLK_24MHZ |
146 SOF_RT5682_SSP_CODEC(0) |
147 SOF_SPEAKER_AMP_PRESENT |
148 SOF_RT5682_SSP_AMP(1)),
151 .callback = sof_rt5682_quirk_cb,
153 DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
154 DMI_MATCH(DMI_PRODUCT_NAME, "Ice Lake Client"),
156 .driver_data = (void *)(SOF_RT5682_MCLK_EN |
157 SOF_RT5682_SSP_CODEC(0)),
160 .callback = sof_rt5682_quirk_cb,
162 DMI_MATCH(DMI_PRODUCT_FAMILY, "Google_Volteer"),
163 DMI_MATCH(DMI_OEM_STRING, "AUDIO-MAX98373_ALC5682I_I2S_UP4"),
165 .driver_data = (void *)(SOF_RT5682_MCLK_EN |
166 SOF_RT5682_SSP_CODEC(0) |
167 SOF_SPEAKER_AMP_PRESENT |
168 SOF_MAX98373_SPEAKER_AMP_PRESENT |
169 SOF_RT5682_SSP_AMP(2) |
170 SOF_RT5682_NUM_HDMIDEV(4)),
173 .callback = sof_rt5682_quirk_cb,
175 DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
176 DMI_MATCH(DMI_PRODUCT_NAME, "Alder Lake Client Platform"),
177 DMI_MATCH(DMI_OEM_STRING, "AUDIO-ADL_MAX98373_ALC5682I_I2S"),
179 .driver_data = (void *)(SOF_RT5682_MCLK_EN |
180 SOF_RT5682_SSP_CODEC(0) |
181 SOF_SPEAKER_AMP_PRESENT |
182 SOF_MAX98373_SPEAKER_AMP_PRESENT |
183 SOF_RT5682_SSP_AMP(2) |
184 SOF_RT5682_NUM_HDMIDEV(4)),
187 .callback = sof_rt5682_quirk_cb,
189 DMI_MATCH(DMI_PRODUCT_FAMILY, "Google_Brya"),
190 DMI_MATCH(DMI_OEM_STRING, "AUDIO-MAX98390_ALC5682I_I2S"),
192 .driver_data = (void *)(SOF_RT5682_MCLK_EN |
193 SOF_RT5682_SSP_CODEC(0) |
194 SOF_SPEAKER_AMP_PRESENT |
195 SOF_MAX98390_SPEAKER_AMP_PRESENT |
196 SOF_RT5682_SSP_AMP(2) |
197 SOF_RT5682_NUM_HDMIDEV(4)),
200 .callback = sof_rt5682_quirk_cb,
202 DMI_MATCH(DMI_PRODUCT_FAMILY, "Google_Brya"),
203 DMI_MATCH(DMI_OEM_STRING, "AUDIO-MAX98390_ALC5682I_I2S_4SPK"),
205 .driver_data = (void *)(SOF_RT5682_MCLK_EN |
206 SOF_RT5682_SSP_CODEC(0) |
207 SOF_SPEAKER_AMP_PRESENT |
208 SOF_MAX98390_SPEAKER_AMP_PRESENT |
209 SOF_MAX98390_TWEETER_SPEAKER_PRESENT |
210 SOF_RT5682_SSP_AMP(1) |
211 SOF_RT5682_NUM_HDMIDEV(4) |
212 SOF_BT_OFFLOAD_SSP(2) |
213 SOF_SSP_BT_OFFLOAD_PRESENT),
217 .callback = sof_rt5682_quirk_cb,
219 DMI_MATCH(DMI_PRODUCT_FAMILY, "Google_Brya"),
220 DMI_MATCH(DMI_OEM_STRING, "AUDIO-MAX98360_ALC5682I_I2S_AMP_SSP2"),
222 .driver_data = (void *)(SOF_RT5682_MCLK_EN |
223 SOF_RT5682_SSP_CODEC(0) |
224 SOF_SPEAKER_AMP_PRESENT |
225 SOF_MAX98360A_SPEAKER_AMP_PRESENT |
226 SOF_RT5682_SSP_AMP(2) |
227 SOF_RT5682_NUM_HDMIDEV(4)),
232 static int sof_hdmi_init(struct snd_soc_pcm_runtime *rtd)
234 struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
235 struct snd_soc_dai *dai = asoc_rtd_to_codec(rtd, 0);
236 struct sof_hdmi_pcm *pcm;
238 pcm = devm_kzalloc(rtd->card->dev, sizeof(*pcm), GFP_KERNEL);
242 /* dai_link id is 1:1 mapped to the PCM device */
243 pcm->device = rtd->dai_link->id;
244 pcm->codec_dai = dai;
246 list_add_tail(&pcm->head, &ctx->hdmi_pcm_list);
251 static int sof_rt5682_codec_init(struct snd_soc_pcm_runtime *rtd)
253 struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
254 struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
255 struct snd_soc_jack *jack;
258 /* need to enable ASRC function for 24MHz mclk rate */
259 if ((sof_rt5682_quirk & SOF_RT5682_MCLK_EN) &&
260 (sof_rt5682_quirk & SOF_RT5682_MCLK_24MHZ)) {
261 if (sof_rt5682_quirk & SOF_RT5682S_HEADPHONE_CODEC_PRESENT)
262 rt5682s_sel_asrc_clk_src(component,
263 RT5682S_DA_STEREO1_FILTER |
264 RT5682S_AD_STEREO1_FILTER,
265 RT5682S_CLK_SEL_I2S1_ASRC);
267 rt5682_sel_asrc_clk_src(component,
268 RT5682_DA_STEREO1_FILTER |
269 RT5682_AD_STEREO1_FILTER,
270 RT5682_CLK_SEL_I2S1_ASRC);
273 if (sof_rt5682_quirk & SOF_RT5682_MCLK_BYTCHT_EN) {
275 * The firmware might enable the clock at
276 * boot (this information may or may not
277 * be reflected in the enable clock register).
278 * To change the rate we must disable the clock
279 * first to cover these cases. Due to common
280 * clock framework restrictions that do not allow
281 * to disable a clock that has not been enabled,
282 * we need to enable the clock first.
284 ret = clk_prepare_enable(ctx->mclk);
286 clk_disable_unprepare(ctx->mclk);
288 ret = clk_set_rate(ctx->mclk, 19200000);
291 dev_err(rtd->dev, "unable to set MCLK rate\n");
295 * Headset buttons map to the google Reference headset.
296 * These can be configured by userspace.
298 ret = snd_soc_card_jack_new(rtd->card, "Headset Jack",
299 SND_JACK_HEADSET | SND_JACK_BTN_0 |
300 SND_JACK_BTN_1 | SND_JACK_BTN_2 |
304 dev_err(rtd->dev, "Headset Jack creation failed: %d\n", ret);
308 jack = &ctx->sof_headset;
310 snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
311 snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
312 snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
313 snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
314 ret = snd_soc_component_set_jack(component, jack, NULL);
317 dev_err(rtd->dev, "Headset Jack call-back failed: %d\n", ret);
324 static void sof_rt5682_codec_exit(struct snd_soc_pcm_runtime *rtd)
326 struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
328 snd_soc_component_set_jack(component, NULL, NULL);
331 static int sof_rt5682_hw_params(struct snd_pcm_substream *substream,
332 struct snd_pcm_hw_params *params)
334 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
335 struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
336 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
337 int pll_id, pll_source, pll_in, pll_out, clk_id, ret;
339 if (sof_rt5682_quirk & SOF_RT5682_MCLK_EN) {
340 if (sof_rt5682_quirk & SOF_RT5682_MCLK_BYTCHT_EN) {
341 ret = clk_prepare_enable(ctx->mclk);
344 "could not configure MCLK state");
349 if (sof_rt5682_quirk & SOF_RT5682S_HEADPHONE_CODEC_PRESENT)
350 pll_source = RT5682S_PLL_S_MCLK;
352 pll_source = RT5682_PLL1_S_MCLK;
354 /* get the tplg configured mclk. */
355 pll_in = sof_dai_get_mclk(rtd);
357 /* mclk from the quirk is the first choice */
358 if (sof_rt5682_quirk & SOF_RT5682_MCLK_24MHZ) {
359 if (pll_in != 24000000)
360 dev_warn(rtd->dev, "configure wrong mclk in tplg, please use 24MHz.\n");
362 } else if (pll_in == 0) {
363 /* use default mclk if not specified correct in topology */
365 } else if (pll_in < 0) {
369 if (sof_rt5682_quirk & SOF_RT5682S_HEADPHONE_CODEC_PRESENT)
370 pll_source = RT5682S_PLL_S_BCLK1;
372 pll_source = RT5682_PLL1_S_BCLK1;
374 pll_in = params_rate(params) * 50;
377 if (sof_rt5682_quirk & SOF_RT5682S_HEADPHONE_CODEC_PRESENT) {
378 pll_id = RT5682S_PLL2;
379 clk_id = RT5682S_SCLK_S_PLL2;
381 pll_id = RT5682_PLL1;
382 clk_id = RT5682_SCLK_S_PLL1;
385 pll_out = params_rate(params) * 512;
387 /* when MCLK is 512FS, no need to set PLL configuration additionally. */
388 if (pll_in == pll_out)
389 clk_id = RT5682S_SCLK_S_MCLK;
391 /* Configure pll for codec */
392 ret = snd_soc_dai_set_pll(codec_dai, pll_id, pll_source, pll_in,
395 dev_err(rtd->dev, "snd_soc_dai_set_pll err = %d\n", ret);
398 /* Configure sysclk for codec */
399 ret = snd_soc_dai_set_sysclk(codec_dai, clk_id,
400 pll_out, SND_SOC_CLOCK_IN);
402 dev_err(rtd->dev, "snd_soc_dai_set_sysclk err = %d\n", ret);
405 * slot_width should equal or large than data length, set them
408 ret = snd_soc_dai_set_tdm_slot(codec_dai, 0x0, 0x0, 2,
409 params_width(params));
411 dev_err(rtd->dev, "set TDM slot err:%d\n", ret);
418 static struct snd_soc_ops sof_rt5682_ops = {
419 .hw_params = sof_rt5682_hw_params,
422 static struct snd_soc_dai_link_component platform_component[] = {
424 /* name might be overridden during probe */
425 .name = "0000:00:1f.3"
429 static int sof_card_late_probe(struct snd_soc_card *card)
431 struct sof_card_private *ctx = snd_soc_card_get_drvdata(card);
432 struct snd_soc_component *component = NULL;
433 struct snd_soc_dapm_context *dapm = &card->dapm;
434 char jack_name[NAME_SIZE];
435 struct sof_hdmi_pcm *pcm;
439 /* HDMI is not supported by SOF on Baytrail/CherryTrail */
440 if (is_legacy_cpu || !ctx->idisp_codec)
443 if (list_empty(&ctx->hdmi_pcm_list))
446 if (ctx->common_hdmi_codec_drv) {
447 pcm = list_first_entry(&ctx->hdmi_pcm_list, struct sof_hdmi_pcm,
449 component = pcm->codec_dai->component;
450 return hda_dsp_hdmi_build_controls(card, component);
453 list_for_each_entry(pcm, &ctx->hdmi_pcm_list, head) {
454 component = pcm->codec_dai->component;
455 snprintf(jack_name, sizeof(jack_name),
456 "HDMI/DP, pcm=%d Jack", pcm->device);
457 err = snd_soc_card_jack_new(card, jack_name,
458 SND_JACK_AVOUT, &sof_hdmi[i]);
463 err = hdac_hdmi_jack_init(pcm->codec_dai, pcm->device,
471 if (sof_rt5682_quirk & SOF_MAX98373_SPEAKER_AMP_PRESENT) {
472 /* Disable Left and Right Spk pin after boot */
473 snd_soc_dapm_disable_pin(dapm, "Left Spk");
474 snd_soc_dapm_disable_pin(dapm, "Right Spk");
475 err = snd_soc_dapm_sync(dapm);
480 return hdac_hdmi_jack_port_init(component, &card->dapm);
483 static const struct snd_kcontrol_new sof_controls[] = {
484 SOC_DAPM_PIN_SWITCH("Headphone Jack"),
485 SOC_DAPM_PIN_SWITCH("Headset Mic"),
486 SOC_DAPM_PIN_SWITCH("Left Spk"),
487 SOC_DAPM_PIN_SWITCH("Right Spk"),
491 static const struct snd_soc_dapm_widget sof_widgets[] = {
492 SND_SOC_DAPM_HP("Headphone Jack", NULL),
493 SND_SOC_DAPM_MIC("Headset Mic", NULL),
494 SND_SOC_DAPM_SPK("Left Spk", NULL),
495 SND_SOC_DAPM_SPK("Right Spk", NULL),
498 static const struct snd_soc_dapm_widget dmic_widgets[] = {
499 SND_SOC_DAPM_MIC("SoC DMIC", NULL),
502 static const struct snd_soc_dapm_route sof_map[] = {
503 /* HP jack connectors - unknown if we have jack detection */
504 { "Headphone Jack", NULL, "HPOL" },
505 { "Headphone Jack", NULL, "HPOR" },
508 { "IN1P", NULL, "Headset Mic" },
511 static const struct snd_soc_dapm_route dmic_map[] = {
513 {"DMic", NULL, "SoC DMIC"},
516 static int dmic_init(struct snd_soc_pcm_runtime *rtd)
518 struct snd_soc_card *card = rtd->card;
521 ret = snd_soc_dapm_new_controls(&card->dapm, dmic_widgets,
522 ARRAY_SIZE(dmic_widgets));
524 dev_err(card->dev, "DMic widget addition failed: %d\n", ret);
525 /* Don't need to add routes if widget addition failed */
529 ret = snd_soc_dapm_add_routes(&card->dapm, dmic_map,
530 ARRAY_SIZE(dmic_map));
533 dev_err(card->dev, "DMic map addition failed: %d\n", ret);
538 /* sof audio machine driver for rt5682 codec */
539 static struct snd_soc_card sof_audio_card_rt5682 = {
540 .name = "rt5682", /* the sof- prefix is added by the core */
541 .owner = THIS_MODULE,
542 .controls = sof_controls,
543 .num_controls = ARRAY_SIZE(sof_controls),
544 .dapm_widgets = sof_widgets,
545 .num_dapm_widgets = ARRAY_SIZE(sof_widgets),
546 .dapm_routes = sof_map,
547 .num_dapm_routes = ARRAY_SIZE(sof_map),
548 .fully_routed = true,
549 .late_probe = sof_card_late_probe,
552 static struct snd_soc_dai_link_component rt5682_component[] = {
554 .name = "i2c-10EC5682:00",
555 .dai_name = "rt5682-aif1",
559 static struct snd_soc_dai_link_component rt5682s_component[] = {
561 .name = "i2c-RTL5682:00",
562 .dai_name = "rt5682s-aif1",
566 static struct snd_soc_dai_link_component dmic_component[] = {
568 .name = "dmic-codec",
569 .dai_name = "dmic-hifi",
573 static struct snd_soc_dai_link_component dummy_component[] = {
575 .name = "snd-soc-dummy",
576 .dai_name = "snd-soc-dummy-dai",
580 #define IDISP_CODEC_MASK 0x4
582 static struct snd_soc_dai_link *sof_card_dai_links_create(struct device *dev,
589 struct snd_soc_dai_link_component *idisp_components;
590 struct snd_soc_dai_link_component *cpus;
591 struct snd_soc_dai_link *links;
594 links = devm_kzalloc(dev, sizeof(struct snd_soc_dai_link) *
595 sof_audio_card_rt5682.num_links, GFP_KERNEL);
596 cpus = devm_kzalloc(dev, sizeof(struct snd_soc_dai_link_component) *
597 sof_audio_card_rt5682.num_links, GFP_KERNEL);
602 links[id].name = devm_kasprintf(dev, GFP_KERNEL,
603 "SSP%d-Codec", ssp_codec);
608 if (sof_rt5682_quirk & SOF_RT5682S_HEADPHONE_CODEC_PRESENT) {
609 links[id].codecs = rt5682s_component;
610 links[id].num_codecs = ARRAY_SIZE(rt5682s_component);
612 links[id].codecs = rt5682_component;
613 links[id].num_codecs = ARRAY_SIZE(rt5682_component);
615 links[id].platforms = platform_component;
616 links[id].num_platforms = ARRAY_SIZE(platform_component);
617 links[id].init = sof_rt5682_codec_init;
618 links[id].exit = sof_rt5682_codec_exit;
619 links[id].ops = &sof_rt5682_ops;
620 links[id].dpcm_playback = 1;
621 links[id].dpcm_capture = 1;
622 links[id].no_pcm = 1;
623 links[id].cpus = &cpus[id];
624 links[id].num_cpus = 1;
626 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
629 if (!links[id].cpus->dai_name)
633 * Currently, On SKL+ platforms MCLK will be turned off in sof
634 * runtime suspended, and it will go into runtime suspended
635 * right after playback is stop. However, rt5682 will output
636 * static noise if sysclk turns off during playback. Set
637 * ignore_pmdown_time to power down rt5682 immediately and
639 * It can be removed once we can control MCLK by driver.
641 links[id].ignore_pmdown_time = 1;
642 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
645 if (!links[id].cpus->dai_name)
651 if (dmic_be_num > 0) {
652 /* at least we have dmic01 */
653 links[id].name = "dmic01";
654 links[id].cpus = &cpus[id];
655 links[id].cpus->dai_name = "DMIC01 Pin";
656 links[id].init = dmic_init;
657 if (dmic_be_num > 1) {
658 /* set up 2 BE links at most */
659 links[id + 1].name = "dmic16k";
660 links[id + 1].cpus = &cpus[id + 1];
661 links[id + 1].cpus->dai_name = "DMIC16k Pin";
666 for (i = 0; i < dmic_be_num; i++) {
668 links[id].num_cpus = 1;
669 links[id].codecs = dmic_component;
670 links[id].num_codecs = ARRAY_SIZE(dmic_component);
671 links[id].platforms = platform_component;
672 links[id].num_platforms = ARRAY_SIZE(platform_component);
673 links[id].ignore_suspend = 1;
674 links[id].dpcm_capture = 1;
675 links[id].no_pcm = 1;
681 idisp_components = devm_kzalloc(dev,
682 sizeof(struct snd_soc_dai_link_component) *
683 hdmi_num, GFP_KERNEL);
684 if (!idisp_components)
687 for (i = 1; i <= hdmi_num; i++) {
688 links[id].name = devm_kasprintf(dev, GFP_KERNEL,
694 links[id].cpus = &cpus[id];
695 links[id].num_cpus = 1;
696 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
698 if (!links[id].cpus->dai_name)
702 idisp_components[i - 1].name = "ehdaudio0D2";
703 idisp_components[i - 1].dai_name = devm_kasprintf(dev,
707 if (!idisp_components[i - 1].dai_name)
710 idisp_components[i - 1].name = "snd-soc-dummy";
711 idisp_components[i - 1].dai_name = "snd-soc-dummy-dai";
714 links[id].codecs = &idisp_components[i - 1];
715 links[id].num_codecs = 1;
716 links[id].platforms = platform_component;
717 links[id].num_platforms = ARRAY_SIZE(platform_component);
718 links[id].init = sof_hdmi_init;
719 links[id].dpcm_playback = 1;
720 links[id].no_pcm = 1;
725 if (sof_rt5682_quirk & SOF_SPEAKER_AMP_PRESENT) {
726 links[id].name = devm_kasprintf(dev, GFP_KERNEL,
727 "SSP%d-Codec", ssp_amp);
732 if (sof_rt5682_quirk & SOF_RT1015_SPEAKER_AMP_PRESENT) {
733 sof_rt1015_dai_link(&links[id], (sof_rt5682_quirk &
734 SOF_RT1015_SPEAKER_AMP_100FS) ? 100 : 64);
735 } else if (sof_rt5682_quirk & SOF_RT1015P_SPEAKER_AMP_PRESENT) {
736 sof_rt1015p_dai_link(&links[id]);
737 } else if (sof_rt5682_quirk & SOF_RT1019_SPEAKER_AMP_PRESENT) {
738 sof_rt1019_dai_link(&links[id]);
739 } else if (sof_rt5682_quirk &
740 SOF_MAX98373_SPEAKER_AMP_PRESENT) {
741 links[id].codecs = max_98373_components;
742 links[id].num_codecs = ARRAY_SIZE(max_98373_components);
743 links[id].init = max_98373_spk_codec_init;
744 links[id].ops = &max_98373_ops;
745 /* feedback stream */
746 links[id].dpcm_capture = 1;
747 } else if (sof_rt5682_quirk &
748 SOF_MAX98360A_SPEAKER_AMP_PRESENT) {
749 max_98360a_dai_link(&links[id]);
750 } else if (sof_rt5682_quirk &
751 SOF_RT1011_SPEAKER_AMP_PRESENT) {
752 sof_rt1011_dai_link(&links[id]);
753 } else if (sof_rt5682_quirk &
754 SOF_MAX98390_SPEAKER_AMP_PRESENT) {
755 if (sof_rt5682_quirk &
756 SOF_MAX98390_TWEETER_SPEAKER_PRESENT) {
757 links[id].codecs = max_98390_4spk_components;
758 links[id].num_codecs = ARRAY_SIZE(max_98390_4spk_components);
760 links[id].codecs = max_98390_components;
761 links[id].num_codecs = ARRAY_SIZE(max_98390_components);
763 links[id].init = max_98390_spk_codec_init;
764 links[id].ops = &max_98390_ops;
765 links[id].dpcm_capture = 1;
768 max_98357a_dai_link(&links[id]);
770 links[id].platforms = platform_component;
771 links[id].num_platforms = ARRAY_SIZE(platform_component);
772 links[id].dpcm_playback = 1;
773 links[id].no_pcm = 1;
774 links[id].cpus = &cpus[id];
775 links[id].num_cpus = 1;
777 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
780 if (!links[id].cpus->dai_name)
784 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
787 if (!links[id].cpus->dai_name)
793 /* BT audio offload */
794 if (sof_rt5682_quirk & SOF_SSP_BT_OFFLOAD_PRESENT) {
795 int port = (sof_rt5682_quirk & SOF_BT_OFFLOAD_SSP_MASK) >>
796 SOF_BT_OFFLOAD_SSP_SHIFT;
799 links[id].cpus = &cpus[id];
800 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
802 if (!links[id].cpus->dai_name)
804 links[id].name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-BT", port);
807 links[id].codecs = dummy_component;
808 links[id].num_codecs = ARRAY_SIZE(dummy_component);
809 links[id].platforms = platform_component;
810 links[id].num_platforms = ARRAY_SIZE(platform_component);
811 links[id].dpcm_playback = 1;
812 links[id].dpcm_capture = 1;
813 links[id].no_pcm = 1;
814 links[id].num_cpus = 1;
822 static int sof_audio_probe(struct platform_device *pdev)
824 struct snd_soc_dai_link *dai_links;
825 struct snd_soc_acpi_mach *mach;
826 struct sof_card_private *ctx;
827 int dmic_be_num, hdmi_num;
828 int ret, ssp_amp, ssp_codec;
830 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
834 if (pdev->id_entry && pdev->id_entry->driver_data)
835 sof_rt5682_quirk = (unsigned long)pdev->id_entry->driver_data;
837 dmi_check_system(sof_rt5682_quirk_table);
839 mach = pdev->dev.platform_data;
841 /* A speaker amp might not be present when the quirk claims one is.
842 * Detect this via whether the machine driver match includes quirk_data.
844 if ((sof_rt5682_quirk & SOF_SPEAKER_AMP_PRESENT) && !mach->quirk_data)
845 sof_rt5682_quirk &= ~SOF_SPEAKER_AMP_PRESENT;
847 /* Detect the headset codec variant */
848 if (acpi_dev_present("RTL5682", NULL, -1))
849 sof_rt5682_quirk |= SOF_RT5682S_HEADPHONE_CODEC_PRESENT;
851 /* Detect the headset codec variant to support machines in DMI quirk */
852 if (acpi_dev_present("RTL5682", NULL, -1))
853 sof_rt5682_quirk |= SOF_RT5682S_HEADPHONE_CODEC_PRESENT;
855 if (soc_intel_is_byt() || soc_intel_is_cht()) {
859 /* default quirk for legacy cpu */
860 sof_rt5682_quirk = SOF_RT5682_MCLK_EN |
861 SOF_RT5682_MCLK_BYTCHT_EN |
862 SOF_RT5682_SSP_CODEC(2);
865 hdmi_num = (sof_rt5682_quirk & SOF_RT5682_NUM_HDMIDEV_MASK) >>
866 SOF_RT5682_NUM_HDMIDEV_SHIFT;
867 /* default number of HDMI DAI's */
871 if (mach->mach_params.codec_mask & IDISP_CODEC_MASK)
872 ctx->idisp_codec = true;
875 /* need to get main clock from pmc */
876 if (sof_rt5682_quirk & SOF_RT5682_MCLK_BYTCHT_EN) {
877 ctx->mclk = devm_clk_get(&pdev->dev, "pmc_plt_clk_3");
878 if (IS_ERR(ctx->mclk)) {
879 ret = PTR_ERR(ctx->mclk);
882 "Failed to get MCLK from pmc_plt_clk_3: %d\n",
887 ret = clk_prepare_enable(ctx->mclk);
890 "could not configure MCLK state");
895 dev_dbg(&pdev->dev, "sof_rt5682_quirk = %lx\n", sof_rt5682_quirk);
897 ssp_amp = (sof_rt5682_quirk & SOF_RT5682_SSP_AMP_MASK) >>
898 SOF_RT5682_SSP_AMP_SHIFT;
900 ssp_codec = sof_rt5682_quirk & SOF_RT5682_SSP_CODEC_MASK;
902 /* compute number of dai links */
903 sof_audio_card_rt5682.num_links = 1 + dmic_be_num + hdmi_num;
905 if (sof_rt5682_quirk & SOF_SPEAKER_AMP_PRESENT)
906 sof_audio_card_rt5682.num_links++;
908 if (sof_rt5682_quirk & SOF_MAX98373_SPEAKER_AMP_PRESENT)
909 max_98373_set_codec_conf(&sof_audio_card_rt5682);
910 else if (sof_rt5682_quirk & SOF_RT1011_SPEAKER_AMP_PRESENT)
911 sof_rt1011_codec_conf(&sof_audio_card_rt5682);
912 else if (sof_rt5682_quirk & SOF_RT1015P_SPEAKER_AMP_PRESENT)
913 sof_rt1015p_codec_conf(&sof_audio_card_rt5682);
914 else if (sof_rt5682_quirk & SOF_MAX98390_SPEAKER_AMP_PRESENT) {
915 if (sof_rt5682_quirk & SOF_MAX98390_TWEETER_SPEAKER_PRESENT)
916 max_98390_set_codec_conf(&sof_audio_card_rt5682,
917 ARRAY_SIZE(max_98390_4spk_components));
919 max_98390_set_codec_conf(&sof_audio_card_rt5682,
920 ARRAY_SIZE(max_98390_components));
923 if (sof_rt5682_quirk & SOF_SSP_BT_OFFLOAD_PRESENT)
924 sof_audio_card_rt5682.num_links++;
926 dai_links = sof_card_dai_links_create(&pdev->dev, ssp_codec, ssp_amp,
927 dmic_be_num, hdmi_num, ctx->idisp_codec);
931 sof_audio_card_rt5682.dai_link = dai_links;
933 if (sof_rt5682_quirk & SOF_RT1015_SPEAKER_AMP_PRESENT)
934 sof_rt1015_codec_conf(&sof_audio_card_rt5682);
936 INIT_LIST_HEAD(&ctx->hdmi_pcm_list);
938 sof_audio_card_rt5682.dev = &pdev->dev;
940 /* set platform name for each dailink */
941 ret = snd_soc_fixup_dai_links_platform_name(&sof_audio_card_rt5682,
942 mach->mach_params.platform);
946 ctx->common_hdmi_codec_drv = mach->mach_params.common_hdmi_codec_drv;
948 snd_soc_card_set_drvdata(&sof_audio_card_rt5682, ctx);
950 return devm_snd_soc_register_card(&pdev->dev,
951 &sof_audio_card_rt5682);
954 static const struct platform_device_id board_ids[] = {
956 .name = "sof_rt5682",
959 .name = "tgl_mx98357_rt5682",
960 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
961 SOF_RT5682_SSP_CODEC(0) |
962 SOF_SPEAKER_AMP_PRESENT |
963 SOF_RT5682_SSP_AMP(1) |
964 SOF_RT5682_NUM_HDMIDEV(4) |
965 SOF_BT_OFFLOAD_SSP(2) |
966 SOF_SSP_BT_OFFLOAD_PRESENT),
969 .name = "jsl_rt5682_rt1015",
970 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
971 SOF_RT5682_MCLK_24MHZ |
972 SOF_RT5682_SSP_CODEC(0) |
973 SOF_SPEAKER_AMP_PRESENT |
974 SOF_RT1015_SPEAKER_AMP_PRESENT |
975 SOF_RT5682_SSP_AMP(1)),
978 .name = "tgl_mx98373_rt5682",
979 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
980 SOF_RT5682_SSP_CODEC(0) |
981 SOF_SPEAKER_AMP_PRESENT |
982 SOF_MAX98373_SPEAKER_AMP_PRESENT |
983 SOF_RT5682_SSP_AMP(1) |
984 SOF_RT5682_NUM_HDMIDEV(4) |
985 SOF_BT_OFFLOAD_SSP(2) |
986 SOF_SSP_BT_OFFLOAD_PRESENT),
989 .name = "jsl_rt5682_mx98360",
990 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
991 SOF_RT5682_MCLK_24MHZ |
992 SOF_RT5682_SSP_CODEC(0) |
993 SOF_SPEAKER_AMP_PRESENT |
994 SOF_MAX98360A_SPEAKER_AMP_PRESENT |
995 SOF_RT5682_SSP_AMP(1)),
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_RT1015_SPEAKER_AMP_100FS |
1005 SOF_RT5682_SSP_AMP(1)),
1008 .name = "tgl_rt1011_rt5682",
1009 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1010 SOF_RT5682_SSP_CODEC(0) |
1011 SOF_SPEAKER_AMP_PRESENT |
1012 SOF_RT1011_SPEAKER_AMP_PRESENT |
1013 SOF_RT5682_SSP_AMP(1) |
1014 SOF_RT5682_NUM_HDMIDEV(4) |
1015 SOF_BT_OFFLOAD_SSP(2) |
1016 SOF_SSP_BT_OFFLOAD_PRESENT),
1019 .name = "jsl_rt5682_rt1015p",
1020 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1021 SOF_RT5682_MCLK_24MHZ |
1022 SOF_RT5682_SSP_CODEC(0) |
1023 SOF_SPEAKER_AMP_PRESENT |
1024 SOF_RT1015P_SPEAKER_AMP_PRESENT |
1025 SOF_RT5682_SSP_AMP(1)),
1028 .name = "adl_mx98373_rt5682",
1029 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1030 SOF_RT5682_SSP_CODEC(0) |
1031 SOF_SPEAKER_AMP_PRESENT |
1032 SOF_MAX98373_SPEAKER_AMP_PRESENT |
1033 SOF_RT5682_SSP_AMP(1) |
1034 SOF_RT5682_NUM_HDMIDEV(4) |
1035 SOF_BT_OFFLOAD_SSP(2) |
1036 SOF_SSP_BT_OFFLOAD_PRESENT),
1039 .name = "adl_mx98357_rt5682",
1040 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1041 SOF_RT5682_SSP_CODEC(0) |
1042 SOF_SPEAKER_AMP_PRESENT |
1043 SOF_RT5682_SSP_AMP(2) |
1044 SOF_RT5682_NUM_HDMIDEV(4)),
1047 .name = "adl_max98390_rt5682",
1048 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1049 SOF_RT5682_SSP_CODEC(0) |
1050 SOF_SPEAKER_AMP_PRESENT |
1051 SOF_MAX98390_SPEAKER_AMP_PRESENT |
1052 SOF_RT5682_SSP_AMP(1) |
1053 SOF_RT5682_NUM_HDMIDEV(4) |
1054 SOF_BT_OFFLOAD_SSP(2) |
1055 SOF_SSP_BT_OFFLOAD_PRESENT),
1058 .name = "adl_mx98360_rt5682",
1059 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1060 SOF_RT5682_SSP_CODEC(0) |
1061 SOF_SPEAKER_AMP_PRESENT |
1062 SOF_MAX98360A_SPEAKER_AMP_PRESENT |
1063 SOF_RT5682_SSP_AMP(1) |
1064 SOF_RT5682_NUM_HDMIDEV(4) |
1065 SOF_BT_OFFLOAD_SSP(2) |
1066 SOF_SSP_BT_OFFLOAD_PRESENT),
1069 .name = "adl_rt5682",
1070 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1071 SOF_RT5682_SSP_CODEC(0) |
1072 SOF_RT5682_NUM_HDMIDEV(4) |
1073 SOF_BT_OFFLOAD_SSP(2) |
1074 SOF_SSP_BT_OFFLOAD_PRESENT),
1077 .name = "adl_rt1019_rt5682s",
1078 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1079 SOF_RT5682_SSP_CODEC(0) |
1080 SOF_RT5682S_HEADPHONE_CODEC_PRESENT |
1081 SOF_SPEAKER_AMP_PRESENT |
1082 SOF_RT1019_SPEAKER_AMP_PRESENT |
1083 SOF_RT5682_SSP_AMP(1) |
1084 SOF_RT5682_NUM_HDMIDEV(4)),
1088 MODULE_DEVICE_TABLE(platform, board_ids);
1090 static struct platform_driver sof_audio = {
1091 .probe = sof_audio_probe,
1093 .name = "sof_rt5682",
1094 .pm = &snd_soc_pm_ops,
1096 .id_table = board_ids,
1098 module_platform_driver(sof_audio)
1100 /* Module information */
1101 MODULE_DESCRIPTION("SOF Audio Machine driver");
1102 MODULE_AUTHOR("Bard Liao <bard.liao@intel.com>");
1103 MODULE_AUTHOR("Sathya Prakash M R <sathya.prakash.m.r@intel.com>");
1104 MODULE_AUTHOR("Brent Lu <brent.lu@intel.com>");
1105 MODULE_AUTHOR("Mac Chiang <mac.chiang@intel.com>");
1106 MODULE_LICENSE("GPL v2");
1107 MODULE_IMPORT_NS(SND_SOC_INTEL_HDA_DSP_COMMON);
1108 MODULE_IMPORT_NS(SND_SOC_INTEL_SOF_MAXIM_COMMON);
1109 MODULE_IMPORT_NS(SND_SOC_INTEL_SOF_REALTEK_COMMON);