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/dmi.h>
13 #include <asm/cpu_device_id.h>
14 #include <asm/intel-family.h>
15 #include <sound/core.h>
16 #include <sound/jack.h>
17 #include <sound/pcm.h>
18 #include <sound/pcm_params.h>
19 #include <sound/soc.h>
20 #include <sound/rt5682.h>
21 #include <sound/soc-acpi.h>
22 #include "../../codecs/rt5682.h"
23 #include "../../codecs/hdac_hdmi.h"
27 #define SOF_RT5682_SSP_CODEC(quirk) ((quirk) & GENMASK(2, 0))
28 #define SOF_RT5682_SSP_CODEC_MASK (GENMASK(2, 0))
29 #define SOF_RT5682_MCLK_EN BIT(3)
30 #define SOF_RT5682_MCLK_24MHZ BIT(4)
31 #define SOF_SPEAKER_AMP_PRESENT BIT(5)
32 #define SOF_RT5682_SSP_AMP(quirk) ((quirk) & GENMASK(8, 6))
33 #define SOF_RT5682_SSP_AMP_MASK (GENMASK(8, 6))
34 #define SOF_RT5682_SSP_AMP_SHIFT 6
36 /* Default: MCLK on, MCLK 19.2M, SSP0 */
37 static unsigned long sof_rt5682_quirk = SOF_RT5682_MCLK_EN |
38 SOF_RT5682_SSP_CODEC(0);
40 static int is_legacy_cpu;
42 static struct snd_soc_jack sof_hdmi[3];
45 struct list_head head;
46 struct snd_soc_dai *codec_dai;
50 struct sof_card_private {
51 struct snd_soc_jack sof_headset;
52 struct list_head hdmi_pcm_list;
55 static int sof_rt5682_quirk_cb(const struct dmi_system_id *id)
57 sof_rt5682_quirk = (unsigned long)id->driver_data;
61 static const struct dmi_system_id sof_rt5682_quirk_table[] = {
63 .callback = sof_rt5682_quirk_cb,
65 DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
66 DMI_MATCH(DMI_PRODUCT_NAME, "WhiskeyLake Client"),
68 .driver_data = (void *)(SOF_RT5682_MCLK_EN |
69 SOF_RT5682_MCLK_24MHZ |
70 SOF_RT5682_SSP_CODEC(1)),
73 .callback = sof_rt5682_quirk_cb,
75 DMI_MATCH(DMI_SYS_VENDOR, "Google"),
76 DMI_MATCH(DMI_PRODUCT_NAME, "Hatch"),
78 .driver_data = (void *)(SOF_RT5682_MCLK_EN |
79 SOF_RT5682_MCLK_24MHZ |
80 SOF_RT5682_SSP_CODEC(0) |
81 SOF_SPEAKER_AMP_PRESENT |
82 SOF_RT5682_SSP_AMP(1)),
85 .callback = sof_rt5682_quirk_cb,
87 DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
88 DMI_MATCH(DMI_PRODUCT_NAME, "Ice Lake Client"),
90 .driver_data = (void *)(SOF_RT5682_MCLK_EN |
91 SOF_RT5682_SSP_CODEC(0)),
96 static int sof_hdmi_init(struct snd_soc_pcm_runtime *rtd)
98 struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
99 struct snd_soc_dai *dai = rtd->codec_dai;
100 struct sof_hdmi_pcm *pcm;
102 pcm = devm_kzalloc(rtd->card->dev, sizeof(*pcm), GFP_KERNEL);
106 /* dai_link id is 1:1 mapped to the PCM device */
107 pcm->device = rtd->dai_link->id;
108 pcm->codec_dai = dai;
110 list_add_tail(&pcm->head, &ctx->hdmi_pcm_list);
115 static int sof_rt5682_codec_init(struct snd_soc_pcm_runtime *rtd)
117 struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
118 struct snd_soc_component *component = rtd->codec_dai->component;
119 struct snd_soc_jack *jack;
122 /* need to enable ASRC function for 24MHz mclk rate */
123 if ((sof_rt5682_quirk & SOF_RT5682_MCLK_EN) &&
124 (sof_rt5682_quirk & SOF_RT5682_MCLK_24MHZ)) {
125 rt5682_sel_asrc_clk_src(component, RT5682_DA_STEREO1_FILTER |
126 RT5682_AD_STEREO1_FILTER,
127 RT5682_CLK_SEL_I2S1_ASRC);
131 * Headset buttons map to the google Reference headset.
132 * These can be configured by userspace.
134 ret = snd_soc_card_jack_new(rtd->card, "Headset Jack",
135 SND_JACK_HEADSET | SND_JACK_BTN_0 |
136 SND_JACK_BTN_1 | SND_JACK_BTN_2 |
138 &ctx->sof_headset, NULL, 0);
140 dev_err(rtd->dev, "Headset Jack creation failed: %d\n", ret);
144 jack = &ctx->sof_headset;
146 snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
147 snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOLUMEUP);
148 snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEDOWN);
149 snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOICECOMMAND);
150 ret = snd_soc_component_set_jack(component, jack, NULL);
153 dev_err(rtd->dev, "Headset Jack call-back failed: %d\n", ret);
160 static int sof_rt5682_hw_params(struct snd_pcm_substream *substream,
161 struct snd_pcm_hw_params *params)
163 struct snd_soc_pcm_runtime *rtd = substream->private_data;
164 struct snd_soc_dai *codec_dai = rtd->codec_dai;
165 int clk_id, clk_freq, pll_out, ret;
167 if (sof_rt5682_quirk & SOF_RT5682_MCLK_EN) {
168 clk_id = RT5682_PLL1_S_MCLK;
169 if (sof_rt5682_quirk & SOF_RT5682_MCLK_24MHZ)
174 clk_id = RT5682_PLL1_S_BCLK1;
175 clk_freq = params_rate(params) * 50;
178 pll_out = params_rate(params) * 512;
180 ret = snd_soc_dai_set_pll(codec_dai, 0, clk_id, clk_freq, pll_out);
182 dev_err(rtd->dev, "snd_soc_dai_set_pll err = %d\n", ret);
184 /* Configure sysclk for codec */
185 ret = snd_soc_dai_set_sysclk(codec_dai, RT5682_SCLK_S_PLL1,
186 pll_out, SND_SOC_CLOCK_IN);
188 dev_err(rtd->dev, "snd_soc_dai_set_sysclk err = %d\n", ret);
191 * slot_width should equal or large than data length, set them
194 ret = snd_soc_dai_set_tdm_slot(codec_dai, 0x0, 0x0, 2,
195 params_width(params));
197 dev_err(rtd->dev, "set TDM slot err:%d\n", ret);
204 static struct snd_soc_ops sof_rt5682_ops = {
205 .hw_params = sof_rt5682_hw_params,
208 static struct snd_soc_dai_link_component platform_component[] = {
210 /* name might be overridden during probe */
211 .name = "0000:00:1f.3"
215 static int sof_card_late_probe(struct snd_soc_card *card)
217 struct sof_card_private *ctx = snd_soc_card_get_drvdata(card);
218 struct snd_soc_component *component = NULL;
219 char jack_name[NAME_SIZE];
220 struct sof_hdmi_pcm *pcm;
224 /* HDMI is not supported by SOF on Baytrail/CherryTrail */
228 list_for_each_entry(pcm, &ctx->hdmi_pcm_list, head) {
229 component = pcm->codec_dai->component;
230 snprintf(jack_name, sizeof(jack_name),
231 "HDMI/DP, pcm=%d Jack", pcm->device);
232 err = snd_soc_card_jack_new(card, jack_name,
233 SND_JACK_AVOUT, &sof_hdmi[i],
239 err = hdac_hdmi_jack_init(pcm->codec_dai, pcm->device,
249 return hdac_hdmi_jack_port_init(component, &card->dapm);
252 static const struct snd_kcontrol_new sof_controls[] = {
253 SOC_DAPM_PIN_SWITCH("Headphone Jack"),
254 SOC_DAPM_PIN_SWITCH("Headset Mic"),
255 SOC_DAPM_PIN_SWITCH("Spk"),
258 static const struct snd_soc_dapm_widget sof_widgets[] = {
259 SND_SOC_DAPM_HP("Headphone Jack", NULL),
260 SND_SOC_DAPM_MIC("Headset Mic", NULL),
261 SND_SOC_DAPM_SPK("Spk", NULL),
264 static const struct snd_soc_dapm_route sof_map[] = {
265 /* HP jack connectors - unknown if we have jack detection */
266 { "Headphone Jack", NULL, "HPOL" },
267 { "Headphone Jack", NULL, "HPOR" },
270 { "IN1P", NULL, "Headset Mic" },
274 static const struct snd_soc_dapm_route speaker_map[] = {
276 { "Spk", NULL, "Speaker" },
279 static int speaker_codec_init(struct snd_soc_pcm_runtime *rtd)
281 struct snd_soc_card *card = rtd->card;
284 ret = snd_soc_dapm_add_routes(&card->dapm, speaker_map,
285 ARRAY_SIZE(speaker_map));
288 dev_err(rtd->dev, "Speaker map addition failed: %d\n", ret);
292 /* sof audio machine driver for rt5682 codec */
293 static struct snd_soc_card sof_audio_card_rt5682 = {
294 .name = "sof_rt5682",
295 .owner = THIS_MODULE,
296 .controls = sof_controls,
297 .num_controls = ARRAY_SIZE(sof_controls),
298 .dapm_widgets = sof_widgets,
299 .num_dapm_widgets = ARRAY_SIZE(sof_widgets),
300 .dapm_routes = sof_map,
301 .num_dapm_routes = ARRAY_SIZE(sof_map),
302 .fully_routed = true,
303 .late_probe = sof_card_late_probe,
306 static const struct x86_cpu_id legacy_cpi_ids[] = {
307 { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_SILVERMONT }, /* Baytrail */
308 { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_AIRMONT }, /* Cherrytrail */
312 static struct snd_soc_dai_link_component rt5682_component[] = {
314 .name = "i2c-10EC5682:00",
315 .dai_name = "rt5682-aif1",
319 static struct snd_soc_dai_link_component dmic_component[] = {
321 .name = "dmic-codec",
322 .dai_name = "dmic-hifi",
326 static struct snd_soc_dai_link_component max98357a_component[] = {
328 .name = "MX98357A:00",
333 static struct snd_soc_dai_link *sof_card_dai_links_create(struct device *dev,
339 struct snd_soc_dai_link_component *idisp_components;
340 struct snd_soc_dai_link *links;
343 links = devm_kzalloc(dev, sizeof(struct snd_soc_dai_link) *
344 sof_audio_card_rt5682.num_links, GFP_KERNEL);
349 links[id].name = devm_kasprintf(dev, GFP_KERNEL,
350 "SSP%d-Codec", ssp_codec);
355 links[id].codecs = rt5682_component;
356 links[id].num_codecs = ARRAY_SIZE(rt5682_component);
357 links[id].platforms = platform_component;
358 links[id].num_platforms = ARRAY_SIZE(platform_component);
359 links[id].init = sof_rt5682_codec_init;
360 links[id].ops = &sof_rt5682_ops;
361 links[id].nonatomic = true;
362 links[id].dpcm_playback = 1;
363 links[id].dpcm_capture = 1;
364 links[id].no_pcm = 1;
366 links[id].cpu_dai_name = devm_kasprintf(dev, GFP_KERNEL,
369 if (!links[id].cpu_dai_name)
373 * Currently, On SKL+ platforms MCLK will be turned off in sof
374 * runtime suspended, and it will go into runtime suspended
375 * right after playback is stop. However, rt5682 will output
376 * static noise if sysclk turns off during playback. Set
377 * ignore_pmdown_time to power down rt5682 immediately and
379 * It can be removed once we can control MCLK by driver.
381 links[id].ignore_pmdown_time = 1;
382 links[id].cpu_dai_name = devm_kasprintf(dev, GFP_KERNEL,
385 if (!links[id].cpu_dai_name)
391 for (i = 1; i <= dmic_num; i++) {
392 links[id].name = devm_kasprintf(dev, GFP_KERNEL,
398 links[id].cpu_dai_name = devm_kasprintf(dev, GFP_KERNEL,
400 if (!links[id].cpu_dai_name)
403 links[id].codecs = dmic_component;
404 links[id].num_codecs = ARRAY_SIZE(dmic_component);
405 links[id].platforms = platform_component;
406 links[id].num_platforms = ARRAY_SIZE(platform_component);
407 links[id].ignore_suspend = 1;
408 links[id].dpcm_capture = 1;
409 links[id].no_pcm = 1;
415 idisp_components = devm_kzalloc(dev,
416 sizeof(struct snd_soc_dai_link_component) *
417 hdmi_num, GFP_KERNEL);
418 if (!idisp_components)
421 for (i = 1; i <= hdmi_num; i++) {
422 links[id].name = devm_kasprintf(dev, GFP_KERNEL,
428 links[id].cpu_dai_name = devm_kasprintf(dev, GFP_KERNEL,
430 if (!links[id].cpu_dai_name)
433 idisp_components[i - 1].name = "ehdaudio0D2";
434 idisp_components[i - 1].dai_name = devm_kasprintf(dev,
438 if (!idisp_components[i - 1].dai_name)
441 links[id].codecs = &idisp_components[i - 1];
442 links[id].num_codecs = 1;
443 links[id].platforms = platform_component;
444 links[id].num_platforms = ARRAY_SIZE(platform_component);
445 links[id].init = sof_hdmi_init;
446 links[id].dpcm_playback = 1;
447 links[id].no_pcm = 1;
452 if (sof_rt5682_quirk & SOF_SPEAKER_AMP_PRESENT) {
453 links[id].name = devm_kasprintf(dev, GFP_KERNEL,
454 "SSP%d-Codec", ssp_amp);
459 links[id].codecs = max98357a_component;
460 links[id].num_codecs = ARRAY_SIZE(max98357a_component);
461 links[id].platforms = platform_component;
462 links[id].num_platforms = ARRAY_SIZE(platform_component);
463 links[id].init = speaker_codec_init,
464 links[id].nonatomic = true;
465 links[id].dpcm_playback = 1;
466 links[id].no_pcm = 1;
468 links[id].cpu_dai_name = devm_kasprintf(dev, GFP_KERNEL,
471 if (!links[id].cpu_dai_name)
475 links[id].cpu_dai_name = devm_kasprintf(dev, GFP_KERNEL,
478 if (!links[id].cpu_dai_name)
488 static int sof_audio_probe(struct platform_device *pdev)
490 struct snd_soc_dai_link *dai_links;
491 struct snd_soc_acpi_mach *mach;
492 struct sof_card_private *ctx;
493 int dmic_num, hdmi_num;
494 int ret, ssp_amp, ssp_codec;
496 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_ATOMIC);
500 if (x86_match_cpu(legacy_cpi_ids)) {
504 /* default quirk for legacy cpu */
505 sof_rt5682_quirk = SOF_RT5682_SSP_CODEC(2);
511 dmi_check_system(sof_rt5682_quirk_table);
513 dev_dbg(&pdev->dev, "sof_rt5682_quirk = %lx\n", sof_rt5682_quirk);
515 ssp_amp = (sof_rt5682_quirk & SOF_RT5682_SSP_AMP_MASK) >>
516 SOF_RT5682_SSP_AMP_SHIFT;
518 ssp_codec = sof_rt5682_quirk & SOF_RT5682_SSP_CODEC_MASK;
520 /* compute number of dai links */
521 sof_audio_card_rt5682.num_links = 1 + dmic_num + hdmi_num;
522 if (sof_rt5682_quirk & SOF_SPEAKER_AMP_PRESENT)
523 sof_audio_card_rt5682.num_links++;
525 dai_links = sof_card_dai_links_create(&pdev->dev, ssp_codec, ssp_amp,
530 sof_audio_card_rt5682.dai_link = dai_links;
532 INIT_LIST_HEAD(&ctx->hdmi_pcm_list);
534 sof_audio_card_rt5682.dev = &pdev->dev;
535 mach = (&pdev->dev)->platform_data;
537 /* set platform name for each dailink */
538 ret = snd_soc_fixup_dai_links_platform_name(&sof_audio_card_rt5682,
539 mach->mach_params.platform);
543 snd_soc_card_set_drvdata(&sof_audio_card_rt5682, ctx);
545 return devm_snd_soc_register_card(&pdev->dev,
546 &sof_audio_card_rt5682);
549 static struct platform_driver sof_audio = {
550 .probe = sof_audio_probe,
552 .name = "sof_rt5682",
553 .pm = &snd_soc_pm_ops,
556 module_platform_driver(sof_audio)
558 /* Module information */
559 MODULE_DESCRIPTION("SOF Audio Machine driver");
560 MODULE_AUTHOR("Bard Liao <bard.liao@intel.com>");
561 MODULE_AUTHOR("Sathya Prakash M R <sathya.prakash.m.r@intel.com>");
562 MODULE_LICENSE("GPL v2");
563 MODULE_ALIAS("platform:sof_rt5682");