1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright(c) 2019 Intel Corporation.
5 * Intel SOF Machine Driver with Realtek rt5682 Codec
6 * and speaker codec MAX98357A
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/rt5682.h>
20 #include <sound/soc-acpi.h>
21 #include "../../codecs/rt5682.h"
22 #include "../../codecs/hdac_hdmi.h"
23 #include "../common/soc-intel-quirks.h"
24 #include "hda_dsp_common.h"
28 #define SOF_RT5682_SSP_CODEC(quirk) ((quirk) & GENMASK(2, 0))
29 #define SOF_RT5682_SSP_CODEC_MASK (GENMASK(2, 0))
30 #define SOF_RT5682_MCLK_EN BIT(3)
31 #define SOF_RT5682_MCLK_24MHZ BIT(4)
32 #define SOF_SPEAKER_AMP_PRESENT BIT(5)
33 #define SOF_RT5682_SSP_AMP_SHIFT 6
34 #define SOF_RT5682_SSP_AMP_MASK (GENMASK(8, 6))
35 #define SOF_RT5682_SSP_AMP(quirk) \
36 (((quirk) << SOF_RT5682_SSP_AMP_SHIFT) & SOF_RT5682_SSP_AMP_MASK)
37 #define SOF_RT5682_MCLK_BYTCHT_EN BIT(9)
39 /* Default: MCLK on, MCLK 19.2M, SSP0 */
40 static unsigned long sof_rt5682_quirk = SOF_RT5682_MCLK_EN |
41 SOF_RT5682_SSP_CODEC(0);
43 static int is_legacy_cpu;
45 static struct snd_soc_jack sof_hdmi[3];
48 struct list_head head;
49 struct snd_soc_dai *codec_dai;
53 struct sof_card_private {
55 struct snd_soc_jack sof_headset;
56 struct list_head hdmi_pcm_list;
57 bool common_hdmi_codec_drv;
60 static int sof_rt5682_quirk_cb(const struct dmi_system_id *id)
62 sof_rt5682_quirk = (unsigned long)id->driver_data;
66 static const struct dmi_system_id sof_rt5682_quirk_table[] = {
68 .callback = sof_rt5682_quirk_cb,
70 DMI_MATCH(DMI_SYS_VENDOR, "Circuitco"),
71 DMI_MATCH(DMI_PRODUCT_NAME, "Minnowboard Max"),
73 .driver_data = (void *)(SOF_RT5682_SSP_CODEC(2)),
76 .callback = sof_rt5682_quirk_cb,
78 DMI_MATCH(DMI_SYS_VENDOR, "AAEON"),
79 DMI_MATCH(DMI_PRODUCT_NAME, "UP-CHT01"),
81 .driver_data = (void *)(SOF_RT5682_SSP_CODEC(2)),
84 .callback = sof_rt5682_quirk_cb,
86 DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
87 DMI_MATCH(DMI_PRODUCT_NAME, "WhiskeyLake Client"),
89 .driver_data = (void *)(SOF_RT5682_MCLK_EN |
90 SOF_RT5682_MCLK_24MHZ |
91 SOF_RT5682_SSP_CODEC(1)),
94 .callback = sof_rt5682_quirk_cb,
96 DMI_MATCH(DMI_PRODUCT_FAMILY, "Google_Hatch"),
98 .driver_data = (void *)(SOF_RT5682_MCLK_EN |
99 SOF_RT5682_MCLK_24MHZ |
100 SOF_RT5682_SSP_CODEC(0) |
101 SOF_SPEAKER_AMP_PRESENT |
102 SOF_RT5682_SSP_AMP(1)),
105 .callback = sof_rt5682_quirk_cb,
107 DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
108 DMI_MATCH(DMI_PRODUCT_NAME, "Ice Lake Client"),
110 .driver_data = (void *)(SOF_RT5682_MCLK_EN |
111 SOF_RT5682_SSP_CODEC(0)),
116 static int sof_hdmi_init(struct snd_soc_pcm_runtime *rtd)
118 struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
119 struct snd_soc_dai *dai = rtd->codec_dai;
120 struct sof_hdmi_pcm *pcm;
122 pcm = devm_kzalloc(rtd->card->dev, sizeof(*pcm), GFP_KERNEL);
126 /* dai_link id is 1:1 mapped to the PCM device */
127 pcm->device = rtd->dai_link->id;
128 pcm->codec_dai = dai;
130 list_add_tail(&pcm->head, &ctx->hdmi_pcm_list);
135 static int sof_rt5682_codec_init(struct snd_soc_pcm_runtime *rtd)
137 struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
138 struct snd_soc_component *component = rtd->codec_dai->component;
139 struct snd_soc_jack *jack;
142 /* need to enable ASRC function for 24MHz mclk rate */
143 if ((sof_rt5682_quirk & SOF_RT5682_MCLK_EN) &&
144 (sof_rt5682_quirk & SOF_RT5682_MCLK_24MHZ)) {
145 rt5682_sel_asrc_clk_src(component, RT5682_DA_STEREO1_FILTER |
146 RT5682_AD_STEREO1_FILTER,
147 RT5682_CLK_SEL_I2S1_ASRC);
150 if (sof_rt5682_quirk & SOF_RT5682_MCLK_BYTCHT_EN) {
152 * The firmware might enable the clock at
153 * boot (this information may or may not
154 * be reflected in the enable clock register).
155 * To change the rate we must disable the clock
156 * first to cover these cases. Due to common
157 * clock framework restrictions that do not allow
158 * to disable a clock that has not been enabled,
159 * we need to enable the clock first.
161 ret = clk_prepare_enable(ctx->mclk);
163 clk_disable_unprepare(ctx->mclk);
165 ret = clk_set_rate(ctx->mclk, 19200000);
168 dev_err(rtd->dev, "unable to set MCLK rate\n");
172 * Headset buttons map to the google Reference headset.
173 * These can be configured by userspace.
175 ret = snd_soc_card_jack_new(rtd->card, "Headset Jack",
176 SND_JACK_HEADSET | SND_JACK_BTN_0 |
177 SND_JACK_BTN_1 | SND_JACK_BTN_2 |
179 &ctx->sof_headset, NULL, 0);
181 dev_err(rtd->dev, "Headset Jack creation failed: %d\n", ret);
185 jack = &ctx->sof_headset;
187 snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
188 snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
189 snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
190 snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
191 ret = snd_soc_component_set_jack(component, jack, NULL);
194 dev_err(rtd->dev, "Headset Jack call-back failed: %d\n", ret);
201 static int sof_rt5682_hw_params(struct snd_pcm_substream *substream,
202 struct snd_pcm_hw_params *params)
204 struct snd_soc_pcm_runtime *rtd = substream->private_data;
205 struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
206 struct snd_soc_dai *codec_dai = rtd->codec_dai;
207 int clk_id, clk_freq, pll_out, ret;
209 if (sof_rt5682_quirk & SOF_RT5682_MCLK_EN) {
210 if (sof_rt5682_quirk & SOF_RT5682_MCLK_BYTCHT_EN) {
211 ret = clk_prepare_enable(ctx->mclk);
214 "could not configure MCLK state");
219 clk_id = RT5682_PLL1_S_MCLK;
220 if (sof_rt5682_quirk & SOF_RT5682_MCLK_24MHZ)
225 clk_id = RT5682_PLL1_S_BCLK1;
226 clk_freq = params_rate(params) * 50;
229 pll_out = params_rate(params) * 512;
231 ret = snd_soc_dai_set_pll(codec_dai, 0, clk_id, clk_freq, pll_out);
233 dev_err(rtd->dev, "snd_soc_dai_set_pll err = %d\n", ret);
235 /* Configure sysclk for codec */
236 ret = snd_soc_dai_set_sysclk(codec_dai, RT5682_SCLK_S_PLL1,
237 pll_out, SND_SOC_CLOCK_IN);
239 dev_err(rtd->dev, "snd_soc_dai_set_sysclk err = %d\n", ret);
242 * slot_width should equal or large than data length, set them
245 ret = snd_soc_dai_set_tdm_slot(codec_dai, 0x0, 0x0, 2,
246 params_width(params));
248 dev_err(rtd->dev, "set TDM slot err:%d\n", ret);
255 static struct snd_soc_ops sof_rt5682_ops = {
256 .hw_params = sof_rt5682_hw_params,
259 static struct snd_soc_dai_link_component platform_component[] = {
261 /* name might be overridden during probe */
262 .name = "0000:00:1f.3"
266 static int sof_card_late_probe(struct snd_soc_card *card)
268 struct sof_card_private *ctx = snd_soc_card_get_drvdata(card);
269 struct snd_soc_component *component = NULL;
270 char jack_name[NAME_SIZE];
271 struct sof_hdmi_pcm *pcm;
275 /* HDMI is not supported by SOF on Baytrail/CherryTrail */
279 pcm = list_first_entry(&ctx->hdmi_pcm_list, struct sof_hdmi_pcm,
281 component = pcm->codec_dai->component;
283 if (ctx->common_hdmi_codec_drv)
284 return hda_dsp_hdmi_build_controls(card, component);
286 list_for_each_entry(pcm, &ctx->hdmi_pcm_list, head) {
287 component = pcm->codec_dai->component;
288 snprintf(jack_name, sizeof(jack_name),
289 "HDMI/DP, pcm=%d Jack", pcm->device);
290 err = snd_soc_card_jack_new(card, jack_name,
291 SND_JACK_AVOUT, &sof_hdmi[i],
297 err = hdac_hdmi_jack_init(pcm->codec_dai, pcm->device,
307 return hdac_hdmi_jack_port_init(component, &card->dapm);
310 static const struct snd_kcontrol_new sof_controls[] = {
311 SOC_DAPM_PIN_SWITCH("Headphone Jack"),
312 SOC_DAPM_PIN_SWITCH("Headset Mic"),
313 SOC_DAPM_PIN_SWITCH("Spk"),
316 static const struct snd_soc_dapm_widget sof_widgets[] = {
317 SND_SOC_DAPM_HP("Headphone Jack", NULL),
318 SND_SOC_DAPM_MIC("Headset Mic", NULL),
319 SND_SOC_DAPM_SPK("Spk", NULL),
322 static const struct snd_soc_dapm_widget dmic_widgets[] = {
323 SND_SOC_DAPM_MIC("SoC DMIC", NULL),
326 static const struct snd_soc_dapm_route sof_map[] = {
327 /* HP jack connectors - unknown if we have jack detection */
328 { "Headphone Jack", NULL, "HPOL" },
329 { "Headphone Jack", NULL, "HPOR" },
332 { "IN1P", NULL, "Headset Mic" },
335 static const struct snd_soc_dapm_route speaker_map[] = {
337 { "Spk", NULL, "Speaker" },
340 static const struct snd_soc_dapm_route dmic_map[] = {
342 {"DMic", NULL, "SoC DMIC"},
345 static int speaker_codec_init(struct snd_soc_pcm_runtime *rtd)
347 struct snd_soc_card *card = rtd->card;
350 ret = snd_soc_dapm_add_routes(&card->dapm, speaker_map,
351 ARRAY_SIZE(speaker_map));
354 dev_err(rtd->dev, "Speaker map addition failed: %d\n", ret);
358 static int dmic_init(struct snd_soc_pcm_runtime *rtd)
360 struct snd_soc_card *card = rtd->card;
363 ret = snd_soc_dapm_new_controls(&card->dapm, dmic_widgets,
364 ARRAY_SIZE(dmic_widgets));
366 dev_err(card->dev, "DMic widget addition failed: %d\n", ret);
367 /* Don't need to add routes if widget addition failed */
371 ret = snd_soc_dapm_add_routes(&card->dapm, dmic_map,
372 ARRAY_SIZE(dmic_map));
375 dev_err(card->dev, "DMic map addition failed: %d\n", ret);
380 /* sof audio machine driver for rt5682 codec */
381 static struct snd_soc_card sof_audio_card_rt5682 = {
382 .name = "rt5682", /* the sof- prefix is added by the core */
383 .owner = THIS_MODULE,
384 .controls = sof_controls,
385 .num_controls = ARRAY_SIZE(sof_controls),
386 .dapm_widgets = sof_widgets,
387 .num_dapm_widgets = ARRAY_SIZE(sof_widgets),
388 .dapm_routes = sof_map,
389 .num_dapm_routes = ARRAY_SIZE(sof_map),
390 .fully_routed = true,
391 .late_probe = sof_card_late_probe,
394 static struct snd_soc_dai_link_component rt5682_component[] = {
396 .name = "i2c-10EC5682:00",
397 .dai_name = "rt5682-aif1",
401 static struct snd_soc_dai_link_component dmic_component[] = {
403 .name = "dmic-codec",
404 .dai_name = "dmic-hifi",
408 static struct snd_soc_dai_link_component max98357a_component[] = {
410 .name = "MX98357A:00",
415 static struct snd_soc_dai_link *sof_card_dai_links_create(struct device *dev,
421 struct snd_soc_dai_link_component *idisp_components;
422 struct snd_soc_dai_link_component *cpus;
423 struct snd_soc_dai_link *links;
426 links = devm_kzalloc(dev, sizeof(struct snd_soc_dai_link) *
427 sof_audio_card_rt5682.num_links, GFP_KERNEL);
428 cpus = devm_kzalloc(dev, sizeof(struct snd_soc_dai_link_component) *
429 sof_audio_card_rt5682.num_links, GFP_KERNEL);
434 links[id].name = devm_kasprintf(dev, GFP_KERNEL,
435 "SSP%d-Codec", ssp_codec);
440 links[id].codecs = rt5682_component;
441 links[id].num_codecs = ARRAY_SIZE(rt5682_component);
442 links[id].platforms = platform_component;
443 links[id].num_platforms = ARRAY_SIZE(platform_component);
444 links[id].init = sof_rt5682_codec_init;
445 links[id].ops = &sof_rt5682_ops;
446 links[id].nonatomic = true;
447 links[id].dpcm_playback = 1;
448 links[id].dpcm_capture = 1;
449 links[id].no_pcm = 1;
450 links[id].cpus = &cpus[id];
451 links[id].num_cpus = 1;
453 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
456 if (!links[id].cpus->dai_name)
460 * Currently, On SKL+ platforms MCLK will be turned off in sof
461 * runtime suspended, and it will go into runtime suspended
462 * right after playback is stop. However, rt5682 will output
463 * static noise if sysclk turns off during playback. Set
464 * ignore_pmdown_time to power down rt5682 immediately and
466 * It can be removed once we can control MCLK by driver.
468 links[id].ignore_pmdown_time = 1;
469 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
472 if (!links[id].cpus->dai_name)
478 if (dmic_be_num > 0) {
479 /* at least we have dmic01 */
480 links[id].name = "dmic01";
481 links[id].cpus = &cpus[id];
482 links[id].cpus->dai_name = "DMIC01 Pin";
483 links[id].init = dmic_init;
484 if (dmic_be_num > 1) {
485 /* set up 2 BE links at most */
486 links[id + 1].name = "dmic16k";
487 links[id + 1].cpus = &cpus[id + 1];
488 links[id + 1].cpus->dai_name = "DMIC16k Pin";
493 for (i = 0; i < dmic_be_num; i++) {
495 links[id].num_cpus = 1;
496 links[id].codecs = dmic_component;
497 links[id].num_codecs = ARRAY_SIZE(dmic_component);
498 links[id].platforms = platform_component;
499 links[id].num_platforms = ARRAY_SIZE(platform_component);
500 links[id].ignore_suspend = 1;
501 links[id].dpcm_capture = 1;
502 links[id].no_pcm = 1;
508 idisp_components = devm_kzalloc(dev,
509 sizeof(struct snd_soc_dai_link_component) *
510 hdmi_num, GFP_KERNEL);
511 if (!idisp_components)
514 for (i = 1; i <= hdmi_num; i++) {
515 links[id].name = devm_kasprintf(dev, GFP_KERNEL,
521 links[id].cpus = &cpus[id];
522 links[id].num_cpus = 1;
523 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
525 if (!links[id].cpus->dai_name)
528 idisp_components[i - 1].name = "ehdaudio0D2";
529 idisp_components[i - 1].dai_name = devm_kasprintf(dev,
533 if (!idisp_components[i - 1].dai_name)
536 links[id].codecs = &idisp_components[i - 1];
537 links[id].num_codecs = 1;
538 links[id].platforms = platform_component;
539 links[id].num_platforms = ARRAY_SIZE(platform_component);
540 links[id].init = sof_hdmi_init;
541 links[id].dpcm_playback = 1;
542 links[id].no_pcm = 1;
547 if (sof_rt5682_quirk & SOF_SPEAKER_AMP_PRESENT) {
548 links[id].name = devm_kasprintf(dev, GFP_KERNEL,
549 "SSP%d-Codec", ssp_amp);
554 links[id].codecs = max98357a_component;
555 links[id].num_codecs = ARRAY_SIZE(max98357a_component);
556 links[id].platforms = platform_component;
557 links[id].num_platforms = ARRAY_SIZE(platform_component);
558 links[id].init = speaker_codec_init,
559 links[id].nonatomic = true;
560 links[id].dpcm_playback = 1;
561 links[id].no_pcm = 1;
562 links[id].cpus = &cpus[id];
563 links[id].num_cpus = 1;
565 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
568 if (!links[id].cpus->dai_name)
572 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
575 if (!links[id].cpus->dai_name)
585 static int sof_audio_probe(struct platform_device *pdev)
587 struct snd_soc_dai_link *dai_links;
588 struct snd_soc_acpi_mach *mach;
589 struct sof_card_private *ctx;
590 int dmic_be_num, hdmi_num;
591 int ret, ssp_amp, ssp_codec;
593 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
597 if (soc_intel_is_byt() || soc_intel_is_cht()) {
601 /* default quirk for legacy cpu */
602 sof_rt5682_quirk = SOF_RT5682_MCLK_EN |
603 SOF_RT5682_MCLK_BYTCHT_EN |
604 SOF_RT5682_SSP_CODEC(2);
610 dmi_check_system(sof_rt5682_quirk_table);
612 /* need to get main clock from pmc */
613 if (sof_rt5682_quirk & SOF_RT5682_MCLK_BYTCHT_EN) {
614 ctx->mclk = devm_clk_get(&pdev->dev, "pmc_plt_clk_3");
615 ret = clk_prepare_enable(ctx->mclk);
618 "could not configure MCLK state");
623 dev_dbg(&pdev->dev, "sof_rt5682_quirk = %lx\n", sof_rt5682_quirk);
625 ssp_amp = (sof_rt5682_quirk & SOF_RT5682_SSP_AMP_MASK) >>
626 SOF_RT5682_SSP_AMP_SHIFT;
628 ssp_codec = sof_rt5682_quirk & SOF_RT5682_SSP_CODEC_MASK;
630 /* compute number of dai links */
631 sof_audio_card_rt5682.num_links = 1 + dmic_be_num + hdmi_num;
633 if (sof_rt5682_quirk & SOF_SPEAKER_AMP_PRESENT)
634 sof_audio_card_rt5682.num_links++;
636 dai_links = sof_card_dai_links_create(&pdev->dev, ssp_codec, ssp_amp,
637 dmic_be_num, hdmi_num);
641 sof_audio_card_rt5682.dai_link = dai_links;
643 INIT_LIST_HEAD(&ctx->hdmi_pcm_list);
645 sof_audio_card_rt5682.dev = &pdev->dev;
646 mach = (&pdev->dev)->platform_data;
648 /* set platform name for each dailink */
649 ret = snd_soc_fixup_dai_links_platform_name(&sof_audio_card_rt5682,
650 mach->mach_params.platform);
654 ctx->common_hdmi_codec_drv = mach->mach_params.common_hdmi_codec_drv;
656 snd_soc_card_set_drvdata(&sof_audio_card_rt5682, ctx);
658 return devm_snd_soc_register_card(&pdev->dev,
659 &sof_audio_card_rt5682);
662 static int sof_rt5682_remove(struct platform_device *pdev)
664 struct snd_soc_card *card = platform_get_drvdata(pdev);
665 struct snd_soc_component *component = NULL;
667 for_each_card_components(card, component) {
668 if (!strcmp(component->name, rt5682_component[0].name)) {
669 snd_soc_component_set_jack(component, NULL, NULL);
677 static struct platform_driver sof_audio = {
678 .probe = sof_audio_probe,
679 .remove = sof_rt5682_remove,
681 .name = "sof_rt5682",
682 .pm = &snd_soc_pm_ops,
685 module_platform_driver(sof_audio)
687 /* Module information */
688 MODULE_DESCRIPTION("SOF Audio Machine driver");
689 MODULE_AUTHOR("Bard Liao <bard.liao@intel.com>");
690 MODULE_AUTHOR("Sathya Prakash M R <sathya.prakash.m.r@intel.com>");
691 MODULE_LICENSE("GPL v2");
692 MODULE_ALIAS("platform:sof_rt5682");