1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright(c) 2021 Intel Corporation.
5 * Intel SOF Machine Driver with Cirrus Logic CS42L42 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/regulator/consumer.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/soc-acpi.h>
21 #include <dt-bindings/sound/cs42l42.h>
22 #include "../../codecs/hdac_hdmi.h"
23 #include "../common/soc-intel-quirks.h"
24 #include "hda_dsp_common.h"
25 #include "sof_maxim_common.h"
29 #define SOF_CS42L42_SSP_CODEC(quirk) ((quirk) & GENMASK(2, 0))
30 #define SOF_CS42L42_SSP_CODEC_MASK (GENMASK(2, 0))
31 #define SOF_SPEAKER_AMP_PRESENT BIT(3)
32 #define SOF_CS42L42_SSP_AMP_SHIFT 4
33 #define SOF_CS42L42_SSP_AMP_MASK (GENMASK(6, 4))
34 #define SOF_CS42L42_SSP_AMP(quirk) \
35 (((quirk) << SOF_CS42L42_SSP_AMP_SHIFT) & SOF_CS42L42_SSP_AMP_MASK)
36 #define SOF_CS42L42_NUM_HDMIDEV_SHIFT 7
37 #define SOF_CS42L42_NUM_HDMIDEV_MASK (GENMASK(9, 7))
38 #define SOF_CS42L42_NUM_HDMIDEV(quirk) \
39 (((quirk) << SOF_CS42L42_NUM_HDMIDEV_SHIFT) & SOF_CS42L42_NUM_HDMIDEV_MASK)
40 #define SOF_CS42L42_DAILINK_SHIFT 10
41 #define SOF_CS42L42_DAILINK_MASK (GENMASK(24, 10))
42 #define SOF_CS42L42_DAILINK(link1, link2, link3, link4, link5) \
43 ((((link1) | ((link2) << 3) | ((link3) << 6) | ((link4) << 9) | ((link5) << 12)) << SOF_CS42L42_DAILINK_SHIFT) & SOF_CS42L42_DAILINK_MASK)
44 #define SOF_BT_OFFLOAD_PRESENT BIT(25)
45 #define SOF_CS42L42_SSP_BT_SHIFT 26
46 #define SOF_CS42L42_SSP_BT_MASK (GENMASK(28, 26))
47 #define SOF_CS42L42_SSP_BT(quirk) \
48 (((quirk) << SOF_CS42L42_SSP_BT_SHIFT) & SOF_CS42L42_SSP_BT_MASK)
49 #define SOF_MAX98357A_SPEAKER_AMP_PRESENT BIT(29)
50 #define SOF_MAX98360A_SPEAKER_AMP_PRESENT BIT(30)
61 static struct snd_soc_jack_pin jack_pins[] = {
63 .pin = "Headphone Jack",
64 .mask = SND_JACK_HEADPHONE,
68 .mask = SND_JACK_MICROPHONE,
73 static unsigned long sof_cs42l42_quirk = SOF_CS42L42_SSP_CODEC(2);
76 struct list_head head;
77 struct snd_soc_dai *codec_dai;
78 struct snd_soc_jack hdmi_jack;
82 struct sof_card_private {
83 struct snd_soc_jack headset_jack;
84 struct list_head hdmi_pcm_list;
85 bool common_hdmi_codec_drv;
88 static int sof_hdmi_init(struct snd_soc_pcm_runtime *rtd)
90 struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
91 struct snd_soc_dai *dai = asoc_rtd_to_codec(rtd, 0);
92 struct sof_hdmi_pcm *pcm;
94 pcm = devm_kzalloc(rtd->card->dev, sizeof(*pcm), GFP_KERNEL);
98 /* dai_link id is 1:1 mapped to the PCM device */
99 pcm->device = rtd->dai_link->id;
100 pcm->codec_dai = dai;
102 list_add_tail(&pcm->head, &ctx->hdmi_pcm_list);
107 static int sof_cs42l42_init(struct snd_soc_pcm_runtime *rtd)
109 struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
110 struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
111 struct snd_soc_jack *jack = &ctx->headset_jack;
115 * Headset buttons map to the google Reference headset.
116 * These can be configured by userspace.
118 ret = snd_soc_card_jack_new_pins(rtd->card, "Headset Jack",
119 SND_JACK_HEADSET | SND_JACK_BTN_0 |
120 SND_JACK_BTN_1 | SND_JACK_BTN_2 |
124 ARRAY_SIZE(jack_pins));
126 dev_err(rtd->dev, "Headset Jack creation failed: %d\n", ret);
130 snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
131 snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOLUMEUP);
132 snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEDOWN);
133 snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOICECOMMAND);
135 ret = snd_soc_component_set_jack(component, jack, NULL);
137 dev_err(rtd->dev, "Headset Jack call-back failed: %d\n", ret);
144 static void sof_cs42l42_exit(struct snd_soc_pcm_runtime *rtd)
146 struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
148 snd_soc_component_set_jack(component, NULL, NULL);
151 static int sof_cs42l42_hw_params(struct snd_pcm_substream *substream,
152 struct snd_pcm_hw_params *params)
154 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
155 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
158 clk_freq = sof_dai_get_bclk(rtd); /* BCLK freq */
161 dev_err(rtd->dev, "get bclk freq failed: %d\n", clk_freq);
165 /* Configure sysclk for codec */
166 ret = snd_soc_dai_set_sysclk(codec_dai, 0,
167 clk_freq, SND_SOC_CLOCK_IN);
169 dev_err(rtd->dev, "snd_soc_dai_set_sysclk err = %d\n", ret);
174 static const struct snd_soc_ops sof_cs42l42_ops = {
175 .hw_params = sof_cs42l42_hw_params,
178 static struct snd_soc_dai_link_component platform_component[] = {
180 /* name might be overridden during probe */
181 .name = "0000:00:1f.3"
185 static int sof_card_late_probe(struct snd_soc_card *card)
187 struct sof_card_private *ctx = snd_soc_card_get_drvdata(card);
188 struct snd_soc_component *component = NULL;
189 char jack_name[NAME_SIZE];
190 struct sof_hdmi_pcm *pcm;
193 if (list_empty(&ctx->hdmi_pcm_list))
196 if (ctx->common_hdmi_codec_drv) {
197 pcm = list_first_entry(&ctx->hdmi_pcm_list, struct sof_hdmi_pcm,
199 component = pcm->codec_dai->component;
200 return hda_dsp_hdmi_build_controls(card, component);
203 list_for_each_entry(pcm, &ctx->hdmi_pcm_list, head) {
204 component = pcm->codec_dai->component;
205 snprintf(jack_name, sizeof(jack_name),
206 "HDMI/DP, pcm=%d Jack", pcm->device);
207 err = snd_soc_card_jack_new(card, jack_name,
208 SND_JACK_AVOUT, &pcm->hdmi_jack);
213 err = hdac_hdmi_jack_init(pcm->codec_dai, pcm->device,
219 return hdac_hdmi_jack_port_init(component, &card->dapm);
222 static const struct snd_kcontrol_new sof_controls[] = {
223 SOC_DAPM_PIN_SWITCH("Headphone Jack"),
224 SOC_DAPM_PIN_SWITCH("Headset Mic"),
227 static const struct snd_soc_dapm_widget sof_widgets[] = {
228 SND_SOC_DAPM_HP("Headphone Jack", NULL),
229 SND_SOC_DAPM_MIC("Headset Mic", NULL),
232 static const struct snd_soc_dapm_widget dmic_widgets[] = {
233 SND_SOC_DAPM_MIC("SoC DMIC", NULL),
236 static const struct snd_soc_dapm_route sof_map[] = {
237 /* HP jack connectors - unknown if we have jack detection */
238 {"Headphone Jack", NULL, "HP"},
241 {"HS", NULL, "Headset Mic"},
244 static const struct snd_soc_dapm_route dmic_map[] = {
246 {"DMic", NULL, "SoC DMIC"},
249 static int dmic_init(struct snd_soc_pcm_runtime *rtd)
251 struct snd_soc_card *card = rtd->card;
254 ret = snd_soc_dapm_new_controls(&card->dapm, dmic_widgets,
255 ARRAY_SIZE(dmic_widgets));
257 dev_err(card->dev, "DMic widget addition failed: %d\n", ret);
258 /* Don't need to add routes if widget addition failed */
262 ret = snd_soc_dapm_add_routes(&card->dapm, dmic_map,
263 ARRAY_SIZE(dmic_map));
266 dev_err(card->dev, "DMic map addition failed: %d\n", ret);
271 /* sof audio machine driver for cs42l42 codec */
272 static struct snd_soc_card sof_audio_card_cs42l42 = {
273 .name = "cs42l42", /* the sof- prefix is added by the core */
274 .owner = THIS_MODULE,
275 .controls = sof_controls,
276 .num_controls = ARRAY_SIZE(sof_controls),
277 .dapm_widgets = sof_widgets,
278 .num_dapm_widgets = ARRAY_SIZE(sof_widgets),
279 .dapm_routes = sof_map,
280 .num_dapm_routes = ARRAY_SIZE(sof_map),
281 .fully_routed = true,
282 .late_probe = sof_card_late_probe,
285 static struct snd_soc_dai_link_component cs42l42_component[] = {
287 .name = "i2c-10134242:00",
288 .dai_name = "cs42l42",
292 static struct snd_soc_dai_link_component dmic_component[] = {
294 .name = "dmic-codec",
295 .dai_name = "dmic-hifi",
299 static struct snd_soc_dai_link_component dummy_component[] = {
301 .name = "snd-soc-dummy",
302 .dai_name = "snd-soc-dummy-dai",
306 static int create_spk_amp_dai_links(struct device *dev,
307 struct snd_soc_dai_link *links,
308 struct snd_soc_dai_link_component *cpus,
309 int *id, int ssp_amp)
314 if (!(sof_cs42l42_quirk & SOF_SPEAKER_AMP_PRESENT))
317 links[*id].name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-Codec",
319 if (!links[*id].name) {
326 if (sof_cs42l42_quirk & SOF_MAX98357A_SPEAKER_AMP_PRESENT) {
327 max_98357a_dai_link(&links[*id]);
328 } else if (sof_cs42l42_quirk & SOF_MAX98360A_SPEAKER_AMP_PRESENT) {
329 max_98360a_dai_link(&links[*id]);
331 dev_err(dev, "no amp defined\n");
336 links[*id].platforms = platform_component;
337 links[*id].num_platforms = ARRAY_SIZE(platform_component);
338 links[*id].dpcm_playback = 1;
339 links[*id].no_pcm = 1;
340 links[*id].cpus = &cpus[*id];
341 links[*id].num_cpus = 1;
343 links[*id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
344 "SSP%d Pin", ssp_amp);
345 if (!links[*id].cpus->dai_name) {
356 static int create_hp_codec_dai_links(struct device *dev,
357 struct snd_soc_dai_link *links,
358 struct snd_soc_dai_link_component *cpus,
359 int *id, int ssp_codec)
362 links[*id].name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-Codec",
364 if (!links[*id].name)
368 links[*id].codecs = cs42l42_component;
369 links[*id].num_codecs = ARRAY_SIZE(cs42l42_component);
370 links[*id].platforms = platform_component;
371 links[*id].num_platforms = ARRAY_SIZE(platform_component);
372 links[*id].init = sof_cs42l42_init;
373 links[*id].exit = sof_cs42l42_exit;
374 links[*id].ops = &sof_cs42l42_ops;
375 links[*id].dpcm_playback = 1;
376 links[*id].dpcm_capture = 1;
377 links[*id].no_pcm = 1;
378 links[*id].cpus = &cpus[*id];
379 links[*id].num_cpus = 1;
381 links[*id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
384 if (!links[*id].cpus->dai_name)
395 static int create_dmic_dai_links(struct device *dev,
396 struct snd_soc_dai_link *links,
397 struct snd_soc_dai_link_component *cpus,
398 int *id, int dmic_be_num)
403 if (dmic_be_num <= 0)
406 /* at least we have dmic01 */
407 links[*id].name = "dmic01";
408 links[*id].cpus = &cpus[*id];
409 links[*id].cpus->dai_name = "DMIC01 Pin";
410 links[*id].init = dmic_init;
411 if (dmic_be_num > 1) {
412 /* set up 2 BE links at most */
413 links[*id + 1].name = "dmic16k";
414 links[*id + 1].cpus = &cpus[*id + 1];
415 links[*id + 1].cpus->dai_name = "DMIC16k Pin";
419 for (i = 0; i < dmic_be_num; i++) {
421 links[*id].num_cpus = 1;
422 links[*id].codecs = dmic_component;
423 links[*id].num_codecs = ARRAY_SIZE(dmic_component);
424 links[*id].platforms = platform_component;
425 links[*id].num_platforms = ARRAY_SIZE(platform_component);
426 links[*id].ignore_suspend = 1;
427 links[*id].dpcm_capture = 1;
428 links[*id].no_pcm = 1;
436 static int create_hdmi_dai_links(struct device *dev,
437 struct snd_soc_dai_link *links,
438 struct snd_soc_dai_link_component *cpus,
439 int *id, int hdmi_num)
441 struct snd_soc_dai_link_component *idisp_components;
448 idisp_components = devm_kcalloc(dev,
450 sizeof(struct snd_soc_dai_link_component), GFP_KERNEL);
451 if (!idisp_components)
454 for (i = 1; i <= hdmi_num; i++) {
455 links[*id].name = devm_kasprintf(dev, GFP_KERNEL,
457 if (!links[*id].name)
461 links[*id].cpus = &cpus[*id];
462 links[*id].num_cpus = 1;
463 links[*id].cpus->dai_name = devm_kasprintf(dev,
467 if (!links[*id].cpus->dai_name)
470 idisp_components[i - 1].name = "ehdaudio0D2";
471 idisp_components[i - 1].dai_name = devm_kasprintf(dev,
475 if (!idisp_components[i - 1].dai_name)
478 links[*id].codecs = &idisp_components[i - 1];
479 links[*id].num_codecs = 1;
480 links[*id].platforms = platform_component;
481 links[*id].num_platforms = ARRAY_SIZE(platform_component);
482 links[*id].init = sof_hdmi_init;
483 links[*id].dpcm_playback = 1;
484 links[*id].no_pcm = 1;
495 static int create_bt_offload_dai_links(struct device *dev,
496 struct snd_soc_dai_link *links,
497 struct snd_soc_dai_link_component *cpus,
501 if (!(sof_cs42l42_quirk & SOF_BT_OFFLOAD_PRESENT))
504 links[*id].name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-BT",
506 if (!links[*id].name)
510 links[*id].codecs = dummy_component;
511 links[*id].num_codecs = ARRAY_SIZE(dummy_component);
512 links[*id].platforms = platform_component;
513 links[*id].num_platforms = ARRAY_SIZE(platform_component);
515 links[*id].dpcm_playback = 1;
516 links[*id].dpcm_capture = 1;
517 links[*id].no_pcm = 1;
518 links[*id].cpus = &cpus[*id];
519 links[*id].num_cpus = 1;
521 links[*id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
524 if (!links[*id].cpus->dai_name)
535 static struct snd_soc_dai_link *sof_card_dai_links_create(struct device *dev,
542 struct snd_soc_dai_link_component *cpus;
543 struct snd_soc_dai_link *links;
544 int ret, id = 0, link_seq;
546 links = devm_kcalloc(dev, sof_audio_card_cs42l42.num_links,
547 sizeof(struct snd_soc_dai_link), GFP_KERNEL);
548 cpus = devm_kcalloc(dev, sof_audio_card_cs42l42.num_links,
549 sizeof(struct snd_soc_dai_link_component), GFP_KERNEL);
553 link_seq = (sof_cs42l42_quirk & SOF_CS42L42_DAILINK_MASK) >> SOF_CS42L42_DAILINK_SHIFT;
556 int link_type = link_seq & 0x07;
560 ret = create_hp_codec_dai_links(dev, links, cpus, &id, ssp_codec);
562 dev_err(dev, "fail to create hp codec dai links, ret %d\n",
568 ret = create_spk_amp_dai_links(dev, links, cpus, &id, ssp_amp);
570 dev_err(dev, "fail to create spk amp dai links, ret %d\n",
576 ret = create_dmic_dai_links(dev, links, cpus, &id, dmic_be_num);
578 dev_err(dev, "fail to create dmic dai links, ret %d\n",
584 ret = create_hdmi_dai_links(dev, links, cpus, &id, hdmi_num);
586 dev_err(dev, "fail to create hdmi dai links, ret %d\n",
592 ret = create_bt_offload_dai_links(dev, links, cpus, &id, ssp_bt);
594 dev_err(dev, "fail to create bt offload dai links, ret %d\n",
600 /* caught here if it's not used as terminator in macro */
602 dev_err(dev, "invalid link type %d\n", link_type);
614 static int sof_audio_probe(struct platform_device *pdev)
616 struct snd_soc_dai_link *dai_links;
617 struct snd_soc_acpi_mach *mach;
618 struct sof_card_private *ctx;
619 int dmic_be_num, hdmi_num;
620 int ret, ssp_bt, ssp_amp, ssp_codec;
622 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
626 if (pdev->id_entry && pdev->id_entry->driver_data)
627 sof_cs42l42_quirk = (unsigned long)pdev->id_entry->driver_data;
629 mach = pdev->dev.platform_data;
631 if (soc_intel_is_glk()) {
636 hdmi_num = (sof_cs42l42_quirk & SOF_CS42L42_NUM_HDMIDEV_MASK) >>
637 SOF_CS42L42_NUM_HDMIDEV_SHIFT;
638 /* default number of HDMI DAI's */
643 dev_dbg(&pdev->dev, "sof_cs42l42_quirk = %lx\n", sof_cs42l42_quirk);
645 ssp_bt = (sof_cs42l42_quirk & SOF_CS42L42_SSP_BT_MASK) >>
646 SOF_CS42L42_SSP_BT_SHIFT;
648 ssp_amp = (sof_cs42l42_quirk & SOF_CS42L42_SSP_AMP_MASK) >>
649 SOF_CS42L42_SSP_AMP_SHIFT;
651 ssp_codec = sof_cs42l42_quirk & SOF_CS42L42_SSP_CODEC_MASK;
653 /* compute number of dai links */
654 sof_audio_card_cs42l42.num_links = 1 + dmic_be_num + hdmi_num;
656 if (sof_cs42l42_quirk & SOF_SPEAKER_AMP_PRESENT)
657 sof_audio_card_cs42l42.num_links++;
658 if (sof_cs42l42_quirk & SOF_BT_OFFLOAD_PRESENT)
659 sof_audio_card_cs42l42.num_links++;
661 dai_links = sof_card_dai_links_create(&pdev->dev, ssp_codec, ssp_amp,
662 ssp_bt, dmic_be_num, hdmi_num);
666 sof_audio_card_cs42l42.dai_link = dai_links;
668 INIT_LIST_HEAD(&ctx->hdmi_pcm_list);
670 sof_audio_card_cs42l42.dev = &pdev->dev;
672 /* set platform name for each dailink */
673 ret = snd_soc_fixup_dai_links_platform_name(&sof_audio_card_cs42l42,
674 mach->mach_params.platform);
678 ctx->common_hdmi_codec_drv = mach->mach_params.common_hdmi_codec_drv;
680 snd_soc_card_set_drvdata(&sof_audio_card_cs42l42, ctx);
682 return devm_snd_soc_register_card(&pdev->dev,
683 &sof_audio_card_cs42l42);
686 static const struct platform_device_id board_ids[] = {
688 .name = "glk_cs4242_mx98357a",
689 .driver_data = (kernel_ulong_t)(SOF_CS42L42_SSP_CODEC(2) |
690 SOF_SPEAKER_AMP_PRESENT |
691 SOF_MAX98357A_SPEAKER_AMP_PRESENT |
692 SOF_CS42L42_SSP_AMP(1)) |
693 SOF_CS42L42_DAILINK(LINK_SPK, LINK_HP, LINK_DMIC, LINK_HDMI, LINK_NONE),
696 .name = "jsl_cs4242_mx98360a",
697 .driver_data = (kernel_ulong_t)(SOF_CS42L42_SSP_CODEC(0) |
698 SOF_SPEAKER_AMP_PRESENT |
699 SOF_MAX98360A_SPEAKER_AMP_PRESENT |
700 SOF_CS42L42_SSP_AMP(1)) |
701 SOF_CS42L42_DAILINK(LINK_HP, LINK_DMIC, LINK_HDMI, LINK_SPK, LINK_NONE),
704 .name = "adl_mx98360a_cs4242",
705 .driver_data = (kernel_ulong_t)(SOF_CS42L42_SSP_CODEC(0) |
706 SOF_SPEAKER_AMP_PRESENT |
707 SOF_MAX98360A_SPEAKER_AMP_PRESENT |
708 SOF_CS42L42_SSP_AMP(1) |
709 SOF_CS42L42_NUM_HDMIDEV(4) |
710 SOF_BT_OFFLOAD_PRESENT |
711 SOF_CS42L42_SSP_BT(2) |
712 SOF_CS42L42_DAILINK(LINK_HP, LINK_DMIC, LINK_HDMI, LINK_SPK, LINK_BT)),
716 MODULE_DEVICE_TABLE(platform, board_ids);
718 static struct platform_driver sof_audio = {
719 .probe = sof_audio_probe,
721 .name = "sof_cs42l42",
722 .pm = &snd_soc_pm_ops,
724 .id_table = board_ids,
726 module_platform_driver(sof_audio)
728 /* Module information */
729 MODULE_DESCRIPTION("SOF Audio Machine driver for CS42L42");
730 MODULE_AUTHOR("Brent Lu <brent.lu@intel.com>");
731 MODULE_LICENSE("GPL");
732 MODULE_IMPORT_NS(SND_SOC_INTEL_HDA_DSP_COMMON);
733 MODULE_IMPORT_NS(SND_SOC_INTEL_SOF_MAXIM_COMMON);