1 // SPDX-License-Identifier: GPL-2.0-only
3 // Copyright(c) 2022 Intel Corporation. All rights reserved.
6 * sof_ssp_amp.c - ASoc Machine driver for Intel platforms
7 * with RT1308/CS35L41 codec.
10 #include <linux/acpi.h>
11 #include <linux/delay.h>
12 #include <linux/module.h>
13 #include <linux/platform_device.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/sof.h>
19 #include "../../codecs/hdac_hdmi.h"
20 #include "hda_dsp_common.h"
21 #include "sof_realtek_common.h"
22 #include "sof_cirrus_common.h"
26 /* SSP port ID for speaker amplifier */
27 #define SOF_AMPLIFIER_SSP(quirk) ((quirk) & GENMASK(3, 0))
28 #define SOF_AMPLIFIER_SSP_MASK (GENMASK(3, 0))
31 #define SOF_SSP_HDMI_CAPTURE_PRESENT BIT(4)
32 #define SOF_NO_OF_HDMI_CAPTURE_SSP_SHIFT 5
33 #define SOF_NO_OF_HDMI_CAPTURE_SSP_MASK (GENMASK(6, 5))
34 #define SOF_NO_OF_HDMI_CAPTURE_SSP(quirk) \
35 (((quirk) << SOF_NO_OF_HDMI_CAPTURE_SSP_SHIFT) & SOF_NO_OF_HDMI_CAPTURE_SSP_MASK)
37 #define SOF_HDMI_CAPTURE_1_SSP_SHIFT 7
38 #define SOF_HDMI_CAPTURE_1_SSP_MASK (GENMASK(9, 7))
39 #define SOF_HDMI_CAPTURE_1_SSP(quirk) \
40 (((quirk) << SOF_HDMI_CAPTURE_1_SSP_SHIFT) & SOF_HDMI_CAPTURE_1_SSP_MASK)
42 #define SOF_HDMI_CAPTURE_2_SSP_SHIFT 10
43 #define SOF_HDMI_CAPTURE_2_SSP_MASK (GENMASK(12, 10))
44 #define SOF_HDMI_CAPTURE_2_SSP(quirk) \
45 (((quirk) << SOF_HDMI_CAPTURE_2_SSP_SHIFT) & SOF_HDMI_CAPTURE_2_SSP_MASK)
48 #define SOF_HDMI_PLAYBACK_PRESENT BIT(13)
49 #define SOF_NO_OF_HDMI_PLAYBACK_SHIFT 14
50 #define SOF_NO_OF_HDMI_PLAYBACK_MASK (GENMASK(16, 14))
51 #define SOF_NO_OF_HDMI_PLAYBACK(quirk) \
52 (((quirk) << SOF_NO_OF_HDMI_PLAYBACK_SHIFT) & SOF_NO_OF_HDMI_PLAYBACK_MASK)
54 /* BT audio offload */
55 #define SOF_SSP_BT_OFFLOAD_PRESENT BIT(17)
56 #define SOF_BT_OFFLOAD_SSP_SHIFT 18
57 #define SOF_BT_OFFLOAD_SSP_MASK (GENMASK(20, 18))
58 #define SOF_BT_OFFLOAD_SSP(quirk) \
59 (((quirk) << SOF_BT_OFFLOAD_SSP_SHIFT) & SOF_BT_OFFLOAD_SSP_MASK)
61 /* Speaker amplifiers */
62 #define SOF_RT1308_SPEAKER_AMP_PRESENT BIT(21)
63 #define SOF_CS35L41_SPEAKER_AMP_PRESENT BIT(22)
66 static unsigned long sof_ssp_amp_quirk = SOF_AMPLIFIER_SSP(2);
69 struct list_head head;
70 struct snd_soc_jack sof_hdmi;
71 struct snd_soc_dai *codec_dai;
75 struct sof_card_private {
76 struct list_head hdmi_pcm_list;
77 bool common_hdmi_codec_drv;
81 static const struct snd_soc_dapm_widget sof_ssp_amp_dapm_widgets[] = {
82 SND_SOC_DAPM_MIC("SoC DMIC", NULL),
85 static const struct snd_soc_dapm_route sof_ssp_amp_dapm_routes[] = {
87 {"DMic", NULL, "SoC DMIC"},
90 static int sof_card_late_probe(struct snd_soc_card *card)
92 struct sof_card_private *ctx = snd_soc_card_get_drvdata(card);
93 struct snd_soc_component *component = NULL;
94 char jack_name[NAME_SIZE];
95 struct sof_hdmi_pcm *pcm;
99 if (!(sof_ssp_amp_quirk & SOF_HDMI_PLAYBACK_PRESENT))
102 /* HDMI is not supported by SOF on Baytrail/CherryTrail */
103 if (!ctx->idisp_codec)
106 if (list_empty(&ctx->hdmi_pcm_list))
109 if (ctx->common_hdmi_codec_drv) {
110 pcm = list_first_entry(&ctx->hdmi_pcm_list, struct sof_hdmi_pcm,
112 component = pcm->codec_dai->component;
113 return hda_dsp_hdmi_build_controls(card, component);
116 list_for_each_entry(pcm, &ctx->hdmi_pcm_list, head) {
117 component = pcm->codec_dai->component;
118 snprintf(jack_name, sizeof(jack_name),
119 "HDMI/DP, pcm=%d Jack", pcm->device);
120 err = snd_soc_card_jack_new(card, jack_name,
121 SND_JACK_AVOUT, &pcm->sof_hdmi,
127 err = hdac_hdmi_jack_init(pcm->codec_dai, pcm->device,
135 return hdac_hdmi_jack_port_init(component, &card->dapm);
138 static struct snd_soc_card sof_ssp_amp_card = {
140 .owner = THIS_MODULE,
141 .dapm_widgets = sof_ssp_amp_dapm_widgets,
142 .num_dapm_widgets = ARRAY_SIZE(sof_ssp_amp_dapm_widgets),
143 .dapm_routes = sof_ssp_amp_dapm_routes,
144 .num_dapm_routes = ARRAY_SIZE(sof_ssp_amp_dapm_routes),
145 .fully_routed = true,
146 .late_probe = sof_card_late_probe,
149 static struct snd_soc_dai_link_component platform_component[] = {
151 /* name might be overridden during probe */
152 .name = "0000:00:1f.3"
156 static struct snd_soc_dai_link_component dmic_component[] = {
158 .name = "dmic-codec",
159 .dai_name = "dmic-hifi",
163 static struct snd_soc_dai_link_component dummy_component[] = {
165 .name = "snd-soc-dummy",
166 .dai_name = "snd-soc-dummy-dai",
170 static int sof_hdmi_init(struct snd_soc_pcm_runtime *rtd)
172 struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
173 struct snd_soc_dai *dai = asoc_rtd_to_codec(rtd, 0);
174 struct sof_hdmi_pcm *pcm;
176 pcm = devm_kzalloc(rtd->card->dev, sizeof(*pcm), GFP_KERNEL);
180 /* dai_link id is 1:1 mapped to the PCM device */
181 pcm->device = rtd->dai_link->id;
182 pcm->codec_dai = dai;
184 list_add_tail(&pcm->head, &ctx->hdmi_pcm_list);
189 #define IDISP_CODEC_MASK 0x4
191 static struct snd_soc_dai_link *sof_card_dai_links_create(struct device *dev,
197 struct snd_soc_dai_link_component *idisp_components;
198 struct snd_soc_dai_link_component *cpus;
199 struct snd_soc_dai_link *links;
202 links = devm_kzalloc(dev, sizeof(struct snd_soc_dai_link) *
203 sof_ssp_amp_card.num_links, GFP_KERNEL);
204 cpus = devm_kzalloc(dev, sizeof(struct snd_soc_dai_link_component) *
205 sof_ssp_amp_card.num_links, GFP_KERNEL);
210 if (sof_ssp_amp_quirk & SOF_SSP_HDMI_CAPTURE_PRESENT) {
211 int num_of_hdmi_ssp = (sof_ssp_amp_quirk & SOF_NO_OF_HDMI_CAPTURE_SSP_MASK) >>
212 SOF_NO_OF_HDMI_CAPTURE_SSP_SHIFT;
214 for (i = 1; i <= num_of_hdmi_ssp; i++) {
215 int port = (i == 1 ? (sof_ssp_amp_quirk & SOF_HDMI_CAPTURE_1_SSP_MASK) >>
216 SOF_HDMI_CAPTURE_1_SSP_SHIFT :
217 (sof_ssp_amp_quirk & SOF_HDMI_CAPTURE_2_SSP_MASK) >>
218 SOF_HDMI_CAPTURE_2_SSP_SHIFT);
220 links[id].cpus = &cpus[id];
221 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
223 if (!links[id].cpus->dai_name)
225 links[id].name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-HDMI", port);
229 links[id].codecs = dummy_component;
230 links[id].num_codecs = ARRAY_SIZE(dummy_component);
231 links[id].platforms = platform_component;
232 links[id].num_platforms = ARRAY_SIZE(platform_component);
233 links[id].dpcm_capture = 1;
234 links[id].no_pcm = 1;
235 links[id].num_cpus = 1;
241 links[id].name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-Codec", ssp_codec);
246 if (sof_ssp_amp_quirk & SOF_RT1308_SPEAKER_AMP_PRESENT) {
247 sof_rt1308_dai_link(&links[id]);
248 } else if (sof_ssp_amp_quirk & SOF_CS35L41_SPEAKER_AMP_PRESENT) {
249 cs35l41_set_dai_link(&links[id]);
251 links[id].platforms = platform_component;
252 links[id].num_platforms = ARRAY_SIZE(platform_component);
253 links[id].dpcm_playback = 1;
254 links[id].no_pcm = 1;
255 links[id].cpus = &cpus[id];
256 links[id].num_cpus = 1;
257 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d Pin", ssp_codec);
258 if (!links[id].cpus->dai_name)
264 if (dmic_be_num > 0) {
265 /* at least we have dmic01 */
266 links[id].name = "dmic01";
267 links[id].cpus = &cpus[id];
268 links[id].cpus->dai_name = "DMIC01 Pin";
269 if (dmic_be_num > 1) {
270 /* set up 2 BE links at most */
271 links[id + 1].name = "dmic16k";
272 links[id + 1].cpus = &cpus[id + 1];
273 links[id + 1].cpus->dai_name = "DMIC16k Pin";
278 for (i = 0; i < dmic_be_num; i++) {
280 links[id].num_cpus = 1;
281 links[id].codecs = dmic_component;
282 links[id].num_codecs = ARRAY_SIZE(dmic_component);
283 links[id].platforms = platform_component;
284 links[id].num_platforms = ARRAY_SIZE(platform_component);
285 links[id].ignore_suspend = 1;
286 links[id].dpcm_capture = 1;
287 links[id].no_pcm = 1;
292 if (sof_ssp_amp_quirk & SOF_HDMI_PLAYBACK_PRESENT) {
295 idisp_components = devm_kzalloc(dev,
296 sizeof(struct snd_soc_dai_link_component) *
297 hdmi_num, GFP_KERNEL);
298 if (!idisp_components)
301 for (i = 1; i <= hdmi_num; i++) {
302 links[id].name = devm_kasprintf(dev, GFP_KERNEL,
308 links[id].cpus = &cpus[id];
309 links[id].num_cpus = 1;
310 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
312 if (!links[id].cpus->dai_name)
316 idisp_components[i - 1].name = "ehdaudio0D2";
317 idisp_components[i - 1].dai_name = devm_kasprintf(dev,
321 if (!idisp_components[i - 1].dai_name)
324 idisp_components[i - 1].name = "snd-soc-dummy";
325 idisp_components[i - 1].dai_name = "snd-soc-dummy-dai";
328 links[id].codecs = &idisp_components[i - 1];
329 links[id].num_codecs = 1;
330 links[id].platforms = platform_component;
331 links[id].num_platforms = ARRAY_SIZE(platform_component);
332 links[id].init = sof_hdmi_init;
333 links[id].dpcm_playback = 1;
334 links[id].no_pcm = 1;
339 /* BT audio offload */
340 if (sof_ssp_amp_quirk & SOF_SSP_BT_OFFLOAD_PRESENT) {
341 int port = (sof_ssp_amp_quirk & SOF_BT_OFFLOAD_SSP_MASK) >>
342 SOF_BT_OFFLOAD_SSP_SHIFT;
345 links[id].cpus = &cpus[id];
346 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
348 if (!links[id].cpus->dai_name)
350 links[id].name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-BT", port);
353 links[id].codecs = dummy_component;
354 links[id].num_codecs = ARRAY_SIZE(dummy_component);
355 links[id].platforms = platform_component;
356 links[id].num_platforms = ARRAY_SIZE(platform_component);
357 links[id].dpcm_playback = 1;
358 links[id].dpcm_capture = 1;
359 links[id].no_pcm = 1;
360 links[id].num_cpus = 1;
369 static int sof_ssp_amp_probe(struct platform_device *pdev)
371 struct snd_soc_dai_link *dai_links;
372 struct snd_soc_acpi_mach *mach;
373 struct sof_card_private *ctx;
374 int dmic_be_num, hdmi_num = 0;
377 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
381 if (pdev->id_entry && pdev->id_entry->driver_data)
382 sof_ssp_amp_quirk = (unsigned long)pdev->id_entry->driver_data;
384 mach = pdev->dev.platform_data;
386 dmic_be_num = mach->mach_params.dmic_num;
388 ssp_codec = sof_ssp_amp_quirk & SOF_AMPLIFIER_SSP_MASK;
390 /* set number of dai links */
391 sof_ssp_amp_card.num_links = 1 + dmic_be_num;
393 if (sof_ssp_amp_quirk & SOF_SSP_HDMI_CAPTURE_PRESENT)
394 sof_ssp_amp_card.num_links += (sof_ssp_amp_quirk & SOF_NO_OF_HDMI_CAPTURE_SSP_MASK) >>
395 SOF_NO_OF_HDMI_CAPTURE_SSP_SHIFT;
397 if (sof_ssp_amp_quirk & SOF_HDMI_PLAYBACK_PRESENT) {
398 hdmi_num = (sof_ssp_amp_quirk & SOF_NO_OF_HDMI_PLAYBACK_MASK) >>
399 SOF_NO_OF_HDMI_PLAYBACK_SHIFT;
400 /* default number of HDMI DAI's */
404 if (mach->mach_params.codec_mask & IDISP_CODEC_MASK)
405 ctx->idisp_codec = true;
407 sof_ssp_amp_card.num_links += hdmi_num;
410 if (sof_ssp_amp_quirk & SOF_SSP_BT_OFFLOAD_PRESENT)
411 sof_ssp_amp_card.num_links++;
413 dai_links = sof_card_dai_links_create(&pdev->dev, ssp_codec, dmic_be_num, hdmi_num, ctx->idisp_codec);
417 sof_ssp_amp_card.dai_link = dai_links;
419 /* update codec_conf */
420 if (sof_ssp_amp_quirk & SOF_CS35L41_SPEAKER_AMP_PRESENT) {
421 cs35l41_set_codec_conf(&sof_ssp_amp_card);
424 INIT_LIST_HEAD(&ctx->hdmi_pcm_list);
426 sof_ssp_amp_card.dev = &pdev->dev;
428 /* set platform name for each dailink */
429 ret = snd_soc_fixup_dai_links_platform_name(&sof_ssp_amp_card,
430 mach->mach_params.platform);
434 ctx->common_hdmi_codec_drv = mach->mach_params.common_hdmi_codec_drv;
436 snd_soc_card_set_drvdata(&sof_ssp_amp_card, ctx);
438 return devm_snd_soc_register_card(&pdev->dev, &sof_ssp_amp_card);
441 static const struct platform_device_id board_ids[] = {
443 .name = "sof_ssp_amp",
446 .name = "tgl_rt1308_hdmi_ssp",
447 .driver_data = (kernel_ulong_t)(SOF_AMPLIFIER_SSP(2) |
448 SOF_NO_OF_HDMI_CAPTURE_SSP(2) |
449 SOF_HDMI_CAPTURE_1_SSP(1) |
450 SOF_HDMI_CAPTURE_2_SSP(5) |
451 SOF_SSP_HDMI_CAPTURE_PRESENT |
452 SOF_RT1308_SPEAKER_AMP_PRESENT),
455 .name = "adl_cs35l41",
456 .driver_data = (kernel_ulong_t)(SOF_AMPLIFIER_SSP(1) |
457 SOF_NO_OF_HDMI_PLAYBACK(4) |
458 SOF_HDMI_PLAYBACK_PRESENT |
459 SOF_BT_OFFLOAD_SSP(2) |
460 SOF_SSP_BT_OFFLOAD_PRESENT |
461 SOF_CS35L41_SPEAKER_AMP_PRESENT),
465 MODULE_DEVICE_TABLE(platform, board_ids);
467 static struct platform_driver sof_ssp_amp_driver = {
468 .probe = sof_ssp_amp_probe,
470 .name = "sof_ssp_amp",
471 .pm = &snd_soc_pm_ops,
473 .id_table = board_ids,
475 module_platform_driver(sof_ssp_amp_driver);
477 MODULE_DESCRIPTION("ASoC Intel(R) SOF Amplifier Machine driver");
478 MODULE_AUTHOR("balamurugan.c <balamurugan.c@intel.com>");
479 MODULE_AUTHOR("Brent Lu <brent.lu@intel.com>");
480 MODULE_LICENSE("GPL");
481 MODULE_IMPORT_NS(SND_SOC_INTEL_HDA_DSP_COMMON);
482 MODULE_IMPORT_NS(SND_SOC_INTEL_SOF_REALTEK_COMMON);
483 MODULE_IMPORT_NS(SND_SOC_INTEL_SOF_CIRRUS_COMMON);