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 "../common/soc-intel-quirks.h"
23 #include "sof_board_helpers.h"
24 #include "sof_maxim_common.h"
25 #include "sof_ssp_common.h"
27 #define SOF_CS42L42_SSP_CODEC(quirk) ((quirk) & GENMASK(2, 0))
28 #define SOF_CS42L42_SSP_CODEC_MASK (GENMASK(2, 0))
29 #define SOF_CS42L42_SSP_AMP_SHIFT 4
30 #define SOF_CS42L42_SSP_AMP_MASK (GENMASK(6, 4))
31 #define SOF_CS42L42_SSP_AMP(quirk) \
32 (((quirk) << SOF_CS42L42_SSP_AMP_SHIFT) & SOF_CS42L42_SSP_AMP_MASK)
33 #define SOF_CS42L42_NUM_HDMIDEV_SHIFT 7
34 #define SOF_CS42L42_NUM_HDMIDEV_MASK (GENMASK(9, 7))
35 #define SOF_CS42L42_NUM_HDMIDEV(quirk) \
36 (((quirk) << SOF_CS42L42_NUM_HDMIDEV_SHIFT) & SOF_CS42L42_NUM_HDMIDEV_MASK)
37 #define SOF_CS42L42_DAILINK_SHIFT 10
38 #define SOF_CS42L42_DAILINK_MASK (GENMASK(24, 10))
39 #define SOF_CS42L42_DAILINK(link1, link2, link3, link4, link5) \
40 ((((link1) | ((link2) << 3) | ((link3) << 6) | ((link4) << 9) | ((link5) << 12)) << SOF_CS42L42_DAILINK_SHIFT) & SOF_CS42L42_DAILINK_MASK)
41 #define SOF_BT_OFFLOAD_PRESENT BIT(25)
42 #define SOF_CS42L42_SSP_BT_SHIFT 26
43 #define SOF_CS42L42_SSP_BT_MASK (GENMASK(28, 26))
44 #define SOF_CS42L42_SSP_BT(quirk) \
45 (((quirk) << SOF_CS42L42_SSP_BT_SHIFT) & SOF_CS42L42_SSP_BT_MASK)
56 static struct snd_soc_jack_pin jack_pins[] = {
58 .pin = "Headphone Jack",
59 .mask = SND_JACK_HEADPHONE,
63 .mask = SND_JACK_MICROPHONE,
68 static unsigned long sof_cs42l42_quirk = SOF_CS42L42_SSP_CODEC(2);
70 static int sof_cs42l42_init(struct snd_soc_pcm_runtime *rtd)
72 struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
73 struct snd_soc_component *component = snd_soc_rtd_to_codec(rtd, 0)->component;
74 struct snd_soc_jack *jack = &ctx->headset_jack;
78 * Headset buttons map to the google Reference headset.
79 * These can be configured by userspace.
81 ret = snd_soc_card_jack_new_pins(rtd->card, "Headset Jack",
82 SND_JACK_HEADSET | SND_JACK_BTN_0 |
83 SND_JACK_BTN_1 | SND_JACK_BTN_2 |
87 ARRAY_SIZE(jack_pins));
89 dev_err(rtd->dev, "Headset Jack creation failed: %d\n", ret);
93 snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
94 snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOLUMEUP);
95 snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEDOWN);
96 snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOICECOMMAND);
98 ret = snd_soc_component_set_jack(component, jack, NULL);
100 dev_err(rtd->dev, "Headset Jack call-back failed: %d\n", ret);
107 static void sof_cs42l42_exit(struct snd_soc_pcm_runtime *rtd)
109 struct snd_soc_component *component = snd_soc_rtd_to_codec(rtd, 0)->component;
111 snd_soc_component_set_jack(component, NULL, NULL);
114 static int sof_cs42l42_hw_params(struct snd_pcm_substream *substream,
115 struct snd_pcm_hw_params *params)
117 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
118 struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
121 clk_freq = sof_dai_get_bclk(rtd); /* BCLK freq */
124 dev_err(rtd->dev, "get bclk freq failed: %d\n", clk_freq);
128 /* Configure sysclk for codec */
129 ret = snd_soc_dai_set_sysclk(codec_dai, 0,
130 clk_freq, SND_SOC_CLOCK_IN);
132 dev_err(rtd->dev, "snd_soc_dai_set_sysclk err = %d\n", ret);
137 static const struct snd_soc_ops sof_cs42l42_ops = {
138 .hw_params = sof_cs42l42_hw_params,
141 static struct snd_soc_dai_link_component platform_component[] = {
143 /* name might be overridden during probe */
144 .name = "0000:00:1f.3"
148 static int sof_card_late_probe(struct snd_soc_card *card)
150 return sof_intel_board_card_late_probe(card);
153 static const struct snd_kcontrol_new sof_controls[] = {
154 SOC_DAPM_PIN_SWITCH("Headphone Jack"),
155 SOC_DAPM_PIN_SWITCH("Headset Mic"),
158 static const struct snd_soc_dapm_widget sof_widgets[] = {
159 SND_SOC_DAPM_HP("Headphone Jack", NULL),
160 SND_SOC_DAPM_MIC("Headset Mic", NULL),
163 static const struct snd_soc_dapm_route sof_map[] = {
164 /* HP jack connectors - unknown if we have jack detection */
165 {"Headphone Jack", NULL, "HP"},
168 {"HS", NULL, "Headset Mic"},
171 /* sof audio machine driver for cs42l42 codec */
172 static struct snd_soc_card sof_audio_card_cs42l42 = {
173 .name = "cs42l42", /* the sof- prefix is added by the core */
174 .owner = THIS_MODULE,
175 .controls = sof_controls,
176 .num_controls = ARRAY_SIZE(sof_controls),
177 .dapm_widgets = sof_widgets,
178 .num_dapm_widgets = ARRAY_SIZE(sof_widgets),
179 .dapm_routes = sof_map,
180 .num_dapm_routes = ARRAY_SIZE(sof_map),
181 .fully_routed = true,
182 .late_probe = sof_card_late_probe,
185 static struct snd_soc_dai_link_component cs42l42_component[] = {
187 .name = "i2c-10134242:00",
188 .dai_name = "cs42l42",
192 static int create_spk_amp_dai_links(struct device *dev,
193 struct snd_soc_dai_link *links,
194 struct snd_soc_dai_link_component *cpus,
195 int *id, enum sof_ssp_codec amp_type,
201 if (amp_type == CODEC_NONE)
204 links[*id].name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-Codec",
206 if (!links[*id].name) {
214 case CODEC_MAX98357A:
215 max_98357a_dai_link(&links[*id]);
217 case CODEC_MAX98360A:
218 max_98360a_dai_link(&links[*id]);
221 dev_err(dev, "invalid amp type %d\n", amp_type);
225 links[*id].platforms = platform_component;
226 links[*id].num_platforms = ARRAY_SIZE(platform_component);
227 links[*id].dpcm_playback = 1;
228 /* firmware-generated echo reference */
229 links[*id].dpcm_capture = 1;
231 links[*id].no_pcm = 1;
232 links[*id].cpus = &cpus[*id];
233 links[*id].num_cpus = 1;
235 links[*id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
236 "SSP%d Pin", ssp_amp);
237 if (!links[*id].cpus->dai_name) {
248 static int create_hp_codec_dai_links(struct device *dev,
249 struct snd_soc_dai_link *links,
250 struct snd_soc_dai_link_component *cpus,
251 int *id, int ssp_codec)
254 links[*id].name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-Codec",
256 if (!links[*id].name)
260 links[*id].codecs = cs42l42_component;
261 links[*id].num_codecs = ARRAY_SIZE(cs42l42_component);
262 links[*id].platforms = platform_component;
263 links[*id].num_platforms = ARRAY_SIZE(platform_component);
264 links[*id].init = sof_cs42l42_init;
265 links[*id].exit = sof_cs42l42_exit;
266 links[*id].ops = &sof_cs42l42_ops;
267 links[*id].dpcm_playback = 1;
268 links[*id].dpcm_capture = 1;
269 links[*id].no_pcm = 1;
270 links[*id].cpus = &cpus[*id];
271 links[*id].num_cpus = 1;
273 links[*id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
276 if (!links[*id].cpus->dai_name)
287 static int create_bt_offload_dai_links(struct device *dev,
288 struct snd_soc_dai_link *links,
289 struct snd_soc_dai_link_component *cpus,
293 if (!(sof_cs42l42_quirk & SOF_BT_OFFLOAD_PRESENT))
296 links[*id].name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-BT",
298 if (!links[*id].name)
302 links[*id].codecs = &snd_soc_dummy_dlc;
303 links[*id].num_codecs = 1;
304 links[*id].platforms = platform_component;
305 links[*id].num_platforms = ARRAY_SIZE(platform_component);
307 links[*id].dpcm_playback = 1;
308 links[*id].dpcm_capture = 1;
309 links[*id].no_pcm = 1;
310 links[*id].cpus = &cpus[*id];
311 links[*id].num_cpus = 1;
313 links[*id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
316 if (!links[*id].cpus->dai_name)
327 static struct snd_soc_dai_link *
328 sof_card_dai_links_create(struct device *dev, enum sof_ssp_codec amp_type,
329 int ssp_codec, int ssp_amp, int ssp_bt,
330 int dmic_be_num, int hdmi_num, bool idisp_codec)
332 struct snd_soc_dai_link_component *cpus;
333 struct snd_soc_dai_link *links;
339 links = devm_kcalloc(dev, sof_audio_card_cs42l42.num_links,
340 sizeof(struct snd_soc_dai_link), GFP_KERNEL);
341 cpus = devm_kcalloc(dev, sof_audio_card_cs42l42.num_links,
342 sizeof(struct snd_soc_dai_link_component), GFP_KERNEL);
346 link_seq = (sof_cs42l42_quirk & SOF_CS42L42_DAILINK_MASK) >> SOF_CS42L42_DAILINK_SHIFT;
349 int link_type = link_seq & 0x07;
353 ret = create_hp_codec_dai_links(dev, links, cpus, &id, ssp_codec);
355 dev_err(dev, "fail to create hp codec dai links, ret %d\n",
361 ret = create_spk_amp_dai_links(dev, links, cpus, &id,
364 dev_err(dev, "fail to create spk amp dai links, ret %d\n",
370 if (dmic_be_num > 0) {
371 /* at least we have dmic01 */
372 ret = sof_intel_board_set_dmic_link(dev,
377 dev_err(dev, "fail to create dmic01 link, ret %d\n",
385 if (dmic_be_num > 1) {
386 /* set up 2 BE links at most */
387 ret = sof_intel_board_set_dmic_link(dev,
392 dev_err(dev, "fail to create dmic16k link, ret %d\n",
401 for (i = 1; i <= hdmi_num; i++) {
402 ret = sof_intel_board_set_intel_hdmi_link(dev,
407 dev_err(dev, "fail to create hdmi link, ret %d\n",
416 ret = create_bt_offload_dai_links(dev, links, cpus, &id, ssp_bt);
418 dev_err(dev, "fail to create bt offload dai links, ret %d\n",
424 /* caught here if it's not used as terminator in macro */
426 dev_err(dev, "invalid link type %d\n", link_type);
438 static int sof_audio_probe(struct platform_device *pdev)
440 struct snd_soc_acpi_mach *mach = pdev->dev.platform_data;
441 struct snd_soc_dai_link *dai_links;
442 struct sof_card_private *ctx;
443 int ret, ssp_bt, ssp_amp, ssp_codec;
445 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
449 if (pdev->id_entry && pdev->id_entry->driver_data)
450 sof_cs42l42_quirk = (unsigned long)pdev->id_entry->driver_data;
452 ctx->codec_type = sof_ssp_detect_codec_type(&pdev->dev);
453 ctx->amp_type = sof_ssp_detect_amp_type(&pdev->dev);
455 if (soc_intel_is_glk()) {
456 ctx->dmic_be_num = 1;
459 ctx->dmic_be_num = 2;
460 ctx->hdmi_num = (sof_cs42l42_quirk & SOF_CS42L42_NUM_HDMIDEV_MASK) >>
461 SOF_CS42L42_NUM_HDMIDEV_SHIFT;
462 /* default number of HDMI DAI's */
467 if (mach->mach_params.codec_mask & IDISP_CODEC_MASK)
468 ctx->hdmi.idisp_codec = true;
470 dev_dbg(&pdev->dev, "sof_cs42l42_quirk = %lx\n", sof_cs42l42_quirk);
472 ssp_bt = (sof_cs42l42_quirk & SOF_CS42L42_SSP_BT_MASK) >>
473 SOF_CS42L42_SSP_BT_SHIFT;
475 ssp_amp = (sof_cs42l42_quirk & SOF_CS42L42_SSP_AMP_MASK) >>
476 SOF_CS42L42_SSP_AMP_SHIFT;
478 ssp_codec = sof_cs42l42_quirk & SOF_CS42L42_SSP_CODEC_MASK;
480 /* compute number of dai links */
481 sof_audio_card_cs42l42.num_links = 1 + ctx->dmic_be_num + ctx->hdmi_num;
483 if (ctx->amp_type != CODEC_NONE)
484 sof_audio_card_cs42l42.num_links++;
485 if (sof_cs42l42_quirk & SOF_BT_OFFLOAD_PRESENT)
486 sof_audio_card_cs42l42.num_links++;
488 dai_links = sof_card_dai_links_create(&pdev->dev, ctx->amp_type,
489 ssp_codec, ssp_amp, ssp_bt,
490 ctx->dmic_be_num, ctx->hdmi_num,
491 ctx->hdmi.idisp_codec);
495 sof_audio_card_cs42l42.dai_link = dai_links;
497 sof_audio_card_cs42l42.dev = &pdev->dev;
499 /* set platform name for each dailink */
500 ret = snd_soc_fixup_dai_links_platform_name(&sof_audio_card_cs42l42,
501 mach->mach_params.platform);
505 snd_soc_card_set_drvdata(&sof_audio_card_cs42l42, ctx);
507 return devm_snd_soc_register_card(&pdev->dev,
508 &sof_audio_card_cs42l42);
511 static const struct platform_device_id board_ids[] = {
513 .name = "glk_cs4242_mx98357a",
514 .driver_data = (kernel_ulong_t)(SOF_CS42L42_SSP_CODEC(2) |
515 SOF_CS42L42_SSP_AMP(1)) |
516 SOF_CS42L42_DAILINK(LINK_SPK, LINK_HP, LINK_DMIC, LINK_HDMI, LINK_NONE),
519 .name = "jsl_cs4242_mx98360a",
520 .driver_data = (kernel_ulong_t)(SOF_CS42L42_SSP_CODEC(0) |
521 SOF_CS42L42_SSP_AMP(1)) |
522 SOF_CS42L42_DAILINK(LINK_HP, LINK_DMIC, LINK_HDMI, LINK_SPK, LINK_NONE),
525 .name = "adl_mx98360a_cs4242",
526 .driver_data = (kernel_ulong_t)(SOF_CS42L42_SSP_CODEC(0) |
527 SOF_CS42L42_SSP_AMP(1) |
528 SOF_CS42L42_NUM_HDMIDEV(4) |
529 SOF_BT_OFFLOAD_PRESENT |
530 SOF_CS42L42_SSP_BT(2) |
531 SOF_CS42L42_DAILINK(LINK_HP, LINK_DMIC, LINK_HDMI, LINK_SPK, LINK_BT)),
535 MODULE_DEVICE_TABLE(platform, board_ids);
537 static struct platform_driver sof_audio = {
538 .probe = sof_audio_probe,
540 .name = "sof_cs42l42",
541 .pm = &snd_soc_pm_ops,
543 .id_table = board_ids,
545 module_platform_driver(sof_audio)
547 /* Module information */
548 MODULE_DESCRIPTION("SOF Audio Machine driver for CS42L42");
549 MODULE_AUTHOR("Brent Lu <brent.lu@intel.com>");
550 MODULE_LICENSE("GPL");
551 MODULE_IMPORT_NS(SND_SOC_INTEL_SOF_BOARD_HELPERS);
552 MODULE_IMPORT_NS(SND_SOC_INTEL_SOF_MAXIM_COMMON);
553 MODULE_IMPORT_NS(SND_SOC_INTEL_SOF_SSP_COMMON);