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_MAX98357A_SPEAKER_AMP_PRESENT BIT(25)
45 #define SOF_MAX98360A_SPEAKER_AMP_PRESENT BIT(26)
56 static unsigned long sof_cs42l42_quirk = SOF_CS42L42_SSP_CODEC(2);
59 struct list_head head;
60 struct snd_soc_dai *codec_dai;
61 struct snd_soc_jack hdmi_jack;
65 struct sof_card_private {
66 struct snd_soc_jack headset_jack;
67 struct list_head hdmi_pcm_list;
68 bool common_hdmi_codec_drv;
71 static int sof_hdmi_init(struct snd_soc_pcm_runtime *rtd)
73 struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
74 struct snd_soc_dai *dai = asoc_rtd_to_codec(rtd, 0);
75 struct sof_hdmi_pcm *pcm;
77 pcm = devm_kzalloc(rtd->card->dev, sizeof(*pcm), GFP_KERNEL);
81 /* dai_link id is 1:1 mapped to the PCM device */
82 pcm->device = rtd->dai_link->id;
85 list_add_tail(&pcm->head, &ctx->hdmi_pcm_list);
90 static int sof_cs42l42_init(struct snd_soc_pcm_runtime *rtd)
92 struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
93 struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
94 struct snd_soc_jack *jack = &ctx->headset_jack;
98 * Headset buttons map to the google Reference headset.
99 * These can be configured by userspace.
101 ret = snd_soc_card_jack_new(rtd->card, "Headset Jack",
102 SND_JACK_HEADSET | SND_JACK_BTN_0 |
103 SND_JACK_BTN_1 | SND_JACK_BTN_2 |
107 dev_err(rtd->dev, "Headset Jack creation failed: %d\n", ret);
111 snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
112 snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOLUMEUP);
113 snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEDOWN);
114 snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOICECOMMAND);
116 ret = snd_soc_component_set_jack(component, jack, NULL);
118 dev_err(rtd->dev, "Headset Jack call-back failed: %d\n", ret);
125 static void sof_cs42l42_exit(struct snd_soc_pcm_runtime *rtd)
127 struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
129 snd_soc_component_set_jack(component, NULL, NULL);
132 static int sof_cs42l42_hw_params(struct snd_pcm_substream *substream,
133 struct snd_pcm_hw_params *params)
135 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
136 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
139 clk_freq = sof_dai_get_bclk(rtd); /* BCLK freq */
142 dev_err(rtd->dev, "get bclk freq failed: %d\n", clk_freq);
146 /* Configure sysclk for codec */
147 ret = snd_soc_dai_set_sysclk(codec_dai, 0,
148 clk_freq, SND_SOC_CLOCK_IN);
150 dev_err(rtd->dev, "snd_soc_dai_set_sysclk err = %d\n", ret);
155 static const struct snd_soc_ops sof_cs42l42_ops = {
156 .hw_params = sof_cs42l42_hw_params,
159 static struct snd_soc_dai_link_component platform_component[] = {
161 /* name might be overridden during probe */
162 .name = "0000:00:1f.3"
166 static int sof_card_late_probe(struct snd_soc_card *card)
168 struct sof_card_private *ctx = snd_soc_card_get_drvdata(card);
169 struct snd_soc_component *component = NULL;
170 char jack_name[NAME_SIZE];
171 struct sof_hdmi_pcm *pcm;
174 if (list_empty(&ctx->hdmi_pcm_list))
177 if (ctx->common_hdmi_codec_drv) {
178 pcm = list_first_entry(&ctx->hdmi_pcm_list, struct sof_hdmi_pcm,
180 component = pcm->codec_dai->component;
181 return hda_dsp_hdmi_build_controls(card, component);
184 list_for_each_entry(pcm, &ctx->hdmi_pcm_list, head) {
185 component = pcm->codec_dai->component;
186 snprintf(jack_name, sizeof(jack_name),
187 "HDMI/DP, pcm=%d Jack", pcm->device);
188 err = snd_soc_card_jack_new(card, jack_name,
189 SND_JACK_AVOUT, &pcm->hdmi_jack);
194 err = hdac_hdmi_jack_init(pcm->codec_dai, pcm->device,
200 return hdac_hdmi_jack_port_init(component, &card->dapm);
203 static const struct snd_kcontrol_new sof_controls[] = {
204 SOC_DAPM_PIN_SWITCH("Headphone Jack"),
205 SOC_DAPM_PIN_SWITCH("Headset Mic"),
208 static const struct snd_soc_dapm_widget sof_widgets[] = {
209 SND_SOC_DAPM_HP("Headphone Jack", NULL),
210 SND_SOC_DAPM_MIC("Headset Mic", NULL),
213 static const struct snd_soc_dapm_widget dmic_widgets[] = {
214 SND_SOC_DAPM_MIC("SoC DMIC", NULL),
217 static const struct snd_soc_dapm_route sof_map[] = {
218 /* HP jack connectors - unknown if we have jack detection */
219 {"Headphone Jack", NULL, "HP"},
222 {"HS", NULL, "Headset Mic"},
225 static const struct snd_soc_dapm_route dmic_map[] = {
227 {"DMic", NULL, "SoC DMIC"},
230 static int dmic_init(struct snd_soc_pcm_runtime *rtd)
232 struct snd_soc_card *card = rtd->card;
235 ret = snd_soc_dapm_new_controls(&card->dapm, dmic_widgets,
236 ARRAY_SIZE(dmic_widgets));
238 dev_err(card->dev, "DMic widget addition failed: %d\n", ret);
239 /* Don't need to add routes if widget addition failed */
243 ret = snd_soc_dapm_add_routes(&card->dapm, dmic_map,
244 ARRAY_SIZE(dmic_map));
247 dev_err(card->dev, "DMic map addition failed: %d\n", ret);
252 /* sof audio machine driver for cs42l42 codec */
253 static struct snd_soc_card sof_audio_card_cs42l42 = {
254 .name = "cs42l42", /* the sof- prefix is added by the core */
255 .owner = THIS_MODULE,
256 .controls = sof_controls,
257 .num_controls = ARRAY_SIZE(sof_controls),
258 .dapm_widgets = sof_widgets,
259 .num_dapm_widgets = ARRAY_SIZE(sof_widgets),
260 .dapm_routes = sof_map,
261 .num_dapm_routes = ARRAY_SIZE(sof_map),
262 .fully_routed = true,
263 .late_probe = sof_card_late_probe,
266 static struct snd_soc_dai_link_component cs42l42_component[] = {
268 .name = "i2c-10134242:00",
269 .dai_name = "cs42l42",
273 static struct snd_soc_dai_link_component dmic_component[] = {
275 .name = "dmic-codec",
276 .dai_name = "dmic-hifi",
280 static int create_spk_amp_dai_links(struct device *dev,
281 struct snd_soc_dai_link *links,
282 struct snd_soc_dai_link_component *cpus,
283 int *id, int ssp_amp)
288 if (!(sof_cs42l42_quirk & SOF_SPEAKER_AMP_PRESENT))
291 links[*id].name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-Codec",
293 if (!links[*id].name) {
300 if (sof_cs42l42_quirk & SOF_MAX98357A_SPEAKER_AMP_PRESENT) {
301 max_98357a_dai_link(&links[*id]);
302 } else if (sof_cs42l42_quirk & SOF_MAX98360A_SPEAKER_AMP_PRESENT) {
303 max_98360a_dai_link(&links[*id]);
305 dev_err(dev, "no amp defined\n");
310 links[*id].platforms = platform_component;
311 links[*id].num_platforms = ARRAY_SIZE(platform_component);
312 links[*id].dpcm_playback = 1;
313 links[*id].no_pcm = 1;
314 links[*id].cpus = &cpus[*id];
315 links[*id].num_cpus = 1;
317 links[*id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
318 "SSP%d Pin", ssp_amp);
319 if (!links[*id].cpus->dai_name) {
330 static int create_hp_codec_dai_links(struct device *dev,
331 struct snd_soc_dai_link *links,
332 struct snd_soc_dai_link_component *cpus,
333 int *id, int ssp_codec)
336 links[*id].name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-Codec",
338 if (!links[*id].name)
342 links[*id].codecs = cs42l42_component;
343 links[*id].num_codecs = ARRAY_SIZE(cs42l42_component);
344 links[*id].platforms = platform_component;
345 links[*id].num_platforms = ARRAY_SIZE(platform_component);
346 links[*id].init = sof_cs42l42_init;
347 links[*id].exit = sof_cs42l42_exit;
348 links[*id].ops = &sof_cs42l42_ops;
349 links[*id].dpcm_playback = 1;
350 links[*id].dpcm_capture = 1;
351 links[*id].no_pcm = 1;
352 links[*id].cpus = &cpus[*id];
353 links[*id].num_cpus = 1;
355 links[*id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
358 if (!links[*id].cpus->dai_name)
369 static int create_dmic_dai_links(struct device *dev,
370 struct snd_soc_dai_link *links,
371 struct snd_soc_dai_link_component *cpus,
372 int *id, int dmic_be_num)
377 if (dmic_be_num <= 0)
380 /* at least we have dmic01 */
381 links[*id].name = "dmic01";
382 links[*id].cpus = &cpus[*id];
383 links[*id].cpus->dai_name = "DMIC01 Pin";
384 links[*id].init = dmic_init;
385 if (dmic_be_num > 1) {
386 /* set up 2 BE links at most */
387 links[*id + 1].name = "dmic16k";
388 links[*id + 1].cpus = &cpus[*id + 1];
389 links[*id + 1].cpus->dai_name = "DMIC16k Pin";
393 for (i = 0; i < dmic_be_num; i++) {
395 links[*id].num_cpus = 1;
396 links[*id].codecs = dmic_component;
397 links[*id].num_codecs = ARRAY_SIZE(dmic_component);
398 links[*id].platforms = platform_component;
399 links[*id].num_platforms = ARRAY_SIZE(platform_component);
400 links[*id].ignore_suspend = 1;
401 links[*id].dpcm_capture = 1;
402 links[*id].no_pcm = 1;
410 static int create_hdmi_dai_links(struct device *dev,
411 struct snd_soc_dai_link *links,
412 struct snd_soc_dai_link_component *cpus,
413 int *id, int hdmi_num)
415 struct snd_soc_dai_link_component *idisp_components;
422 idisp_components = devm_kzalloc(dev,
423 sizeof(struct snd_soc_dai_link_component) *
424 hdmi_num, GFP_KERNEL);
425 if (!idisp_components)
428 for (i = 1; i <= hdmi_num; i++) {
429 links[*id].name = devm_kasprintf(dev, GFP_KERNEL,
431 if (!links[*id].name)
435 links[*id].cpus = &cpus[*id];
436 links[*id].num_cpus = 1;
437 links[*id].cpus->dai_name = devm_kasprintf(dev,
441 if (!links[*id].cpus->dai_name)
444 idisp_components[i - 1].name = "ehdaudio0D2";
445 idisp_components[i - 1].dai_name = devm_kasprintf(dev,
449 if (!idisp_components[i - 1].dai_name)
452 links[*id].codecs = &idisp_components[i - 1];
453 links[*id].num_codecs = 1;
454 links[*id].platforms = platform_component;
455 links[*id].num_platforms = ARRAY_SIZE(platform_component);
456 links[*id].init = sof_hdmi_init;
457 links[*id].dpcm_playback = 1;
458 links[*id].no_pcm = 1;
469 static struct snd_soc_dai_link *sof_card_dai_links_create(struct device *dev,
475 struct snd_soc_dai_link_component *cpus;
476 struct snd_soc_dai_link *links;
477 int ret, id = 0, link_seq;
479 links = devm_kzalloc(dev, sizeof(struct snd_soc_dai_link) *
480 sof_audio_card_cs42l42.num_links, GFP_KERNEL);
481 cpus = devm_kzalloc(dev, sizeof(struct snd_soc_dai_link_component) *
482 sof_audio_card_cs42l42.num_links, GFP_KERNEL);
486 link_seq = (sof_cs42l42_quirk & SOF_CS42L42_DAILINK_MASK) >> SOF_CS42L42_DAILINK_SHIFT;
489 int link_type = link_seq & 0x07;
493 ret = create_hp_codec_dai_links(dev, links, cpus, &id, ssp_codec);
495 dev_err(dev, "fail to create hp codec dai links, ret %d\n",
501 ret = create_spk_amp_dai_links(dev, links, cpus, &id, ssp_amp);
503 dev_err(dev, "fail to create spk amp dai links, ret %d\n",
509 ret = create_dmic_dai_links(dev, links, cpus, &id, dmic_be_num);
511 dev_err(dev, "fail to create dmic dai links, ret %d\n",
517 ret = create_hdmi_dai_links(dev, links, cpus, &id, hdmi_num);
519 dev_err(dev, "fail to create hdmi dai links, ret %d\n",
525 /* caught here if it's not used as terminator in macro */
527 dev_err(dev, "invalid link type %d\n", link_type);
539 static int sof_audio_probe(struct platform_device *pdev)
541 struct snd_soc_dai_link *dai_links;
542 struct snd_soc_acpi_mach *mach;
543 struct sof_card_private *ctx;
544 int dmic_be_num, hdmi_num;
545 int ret, ssp_amp, ssp_codec;
547 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
551 if (pdev->id_entry && pdev->id_entry->driver_data)
552 sof_cs42l42_quirk = (unsigned long)pdev->id_entry->driver_data;
554 mach = pdev->dev.platform_data;
556 if (soc_intel_is_glk()) {
561 hdmi_num = (sof_cs42l42_quirk & SOF_CS42L42_NUM_HDMIDEV_MASK) >>
562 SOF_CS42L42_NUM_HDMIDEV_SHIFT;
563 /* default number of HDMI DAI's */
568 dev_dbg(&pdev->dev, "sof_cs42l42_quirk = %lx\n", sof_cs42l42_quirk);
570 ssp_amp = (sof_cs42l42_quirk & SOF_CS42L42_SSP_AMP_MASK) >>
571 SOF_CS42L42_SSP_AMP_SHIFT;
573 ssp_codec = sof_cs42l42_quirk & SOF_CS42L42_SSP_CODEC_MASK;
575 /* compute number of dai links */
576 sof_audio_card_cs42l42.num_links = 1 + dmic_be_num + hdmi_num;
578 if (sof_cs42l42_quirk & SOF_SPEAKER_AMP_PRESENT)
579 sof_audio_card_cs42l42.num_links++;
581 dai_links = sof_card_dai_links_create(&pdev->dev, ssp_codec, ssp_amp,
582 dmic_be_num, hdmi_num);
586 sof_audio_card_cs42l42.dai_link = dai_links;
588 INIT_LIST_HEAD(&ctx->hdmi_pcm_list);
590 sof_audio_card_cs42l42.dev = &pdev->dev;
592 /* set platform name for each dailink */
593 ret = snd_soc_fixup_dai_links_platform_name(&sof_audio_card_cs42l42,
594 mach->mach_params.platform);
598 ctx->common_hdmi_codec_drv = mach->mach_params.common_hdmi_codec_drv;
600 snd_soc_card_set_drvdata(&sof_audio_card_cs42l42, ctx);
602 return devm_snd_soc_register_card(&pdev->dev,
603 &sof_audio_card_cs42l42);
606 static const struct platform_device_id board_ids[] = {
608 .name = "glk_cs4242_mx98357a",
609 .driver_data = (kernel_ulong_t)(SOF_CS42L42_SSP_CODEC(2) |
610 SOF_SPEAKER_AMP_PRESENT |
611 SOF_MAX98357A_SPEAKER_AMP_PRESENT |
612 SOF_CS42L42_SSP_AMP(1)) |
613 SOF_CS42L42_DAILINK(LINK_SPK, LINK_HP, LINK_DMIC, LINK_HDMI, LINK_NONE),
616 .name = "jsl_cs4242_mx98360a",
617 .driver_data = (kernel_ulong_t)(SOF_CS42L42_SSP_CODEC(0) |
618 SOF_SPEAKER_AMP_PRESENT |
619 SOF_MAX98360A_SPEAKER_AMP_PRESENT |
620 SOF_CS42L42_SSP_AMP(1)) |
621 SOF_CS42L42_DAILINK(LINK_HP, LINK_DMIC, LINK_HDMI, LINK_SPK, LINK_NONE),
625 MODULE_DEVICE_TABLE(platform, board_ids);
627 static struct platform_driver sof_audio = {
628 .probe = sof_audio_probe,
630 .name = "sof_cs42l42",
631 .pm = &snd_soc_pm_ops,
633 .id_table = board_ids,
635 module_platform_driver(sof_audio)
637 /* Module information */
638 MODULE_DESCRIPTION("SOF Audio Machine driver for CS42L42");
639 MODULE_AUTHOR("Brent Lu <brent.lu@intel.com>");
640 MODULE_LICENSE("GPL");
641 MODULE_IMPORT_NS(SND_SOC_INTEL_HDA_DSP_COMMON);
642 MODULE_IMPORT_NS(SND_SOC_INTEL_SOF_MAXIM_COMMON);