xref: /linux/sound/soc/intel/boards/sof_ssp_amp.c (revision 34f7c6e7d4396090692a09789db231e12cb4762b)
1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // Copyright(c) 2022 Intel Corporation. All rights reserved.
4 
5 /*
6  * sof_ssp_amp.c - ASoc Machine driver for Intel platforms
7  * with RT1308/CS35L41 codec.
8  */
9 
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"
23 
24 #define NAME_SIZE 32
25 
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))
29 
30 /* HDMI capture*/
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)
36 
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)
41 
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)
46 
47 /* HDMI playback */
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)
53 
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)
60 
61 /* Speaker amplifiers */
62 #define SOF_RT1308_SPEAKER_AMP_PRESENT		BIT(21)
63 #define SOF_CS35L41_SPEAKER_AMP_PRESENT		BIT(22)
64 
65 /* Default: SSP2  */
66 static unsigned long sof_ssp_amp_quirk = SOF_AMPLIFIER_SSP(2);
67 
68 struct sof_hdmi_pcm {
69 	struct list_head head;
70 	struct snd_soc_jack sof_hdmi;
71 	struct snd_soc_dai *codec_dai;
72 	int device;
73 };
74 
75 struct sof_card_private {
76 	struct list_head hdmi_pcm_list;
77 	bool common_hdmi_codec_drv;
78 	bool idisp_codec;
79 };
80 
81 static const struct snd_soc_dapm_widget sof_ssp_amp_dapm_widgets[] = {
82 	SND_SOC_DAPM_MIC("SoC DMIC", NULL),
83 };
84 
85 static const struct snd_soc_dapm_route sof_ssp_amp_dapm_routes[] = {
86 	/* digital mics */
87 	{"DMic", NULL, "SoC DMIC"},
88 };
89 
90 static int sof_card_late_probe(struct snd_soc_card *card)
91 {
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;
96 	int err;
97 	int i = 0;
98 
99 	if (!(sof_ssp_amp_quirk & SOF_HDMI_PLAYBACK_PRESENT))
100 		return 0;
101 
102 	/* HDMI is not supported by SOF on Baytrail/CherryTrail */
103 	if (!ctx->idisp_codec)
104 		return 0;
105 
106 	if (list_empty(&ctx->hdmi_pcm_list))
107 		return -EINVAL;
108 
109 	if (ctx->common_hdmi_codec_drv) {
110 		pcm = list_first_entry(&ctx->hdmi_pcm_list, struct sof_hdmi_pcm,
111 				       head);
112 		component = pcm->codec_dai->component;
113 		return hda_dsp_hdmi_build_controls(card, component);
114 	}
115 
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,
122 					    NULL, 0);
123 
124 		if (err)
125 			return err;
126 
127 		err = hdac_hdmi_jack_init(pcm->codec_dai, pcm->device,
128 					  &pcm->sof_hdmi);
129 		if (err < 0)
130 			return err;
131 
132 		i++;
133 	}
134 
135 	return hdac_hdmi_jack_port_init(component, &card->dapm);
136 }
137 
138 static struct snd_soc_card sof_ssp_amp_card = {
139 	.name         = "ssp_amp",
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,
147 };
148 
149 static struct snd_soc_dai_link_component platform_component[] = {
150 	{
151 		/* name might be overridden during probe */
152 		.name = "0000:00:1f.3"
153 	}
154 };
155 
156 static struct snd_soc_dai_link_component dmic_component[] = {
157 	{
158 		.name = "dmic-codec",
159 		.dai_name = "dmic-hifi",
160 	}
161 };
162 
163 static struct snd_soc_dai_link_component dummy_component[] = {
164 	{
165 		.name = "snd-soc-dummy",
166 		.dai_name = "snd-soc-dummy-dai",
167 	}
168 };
169 
170 static int sof_hdmi_init(struct snd_soc_pcm_runtime *rtd)
171 {
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;
175 
176 	pcm = devm_kzalloc(rtd->card->dev, sizeof(*pcm), GFP_KERNEL);
177 	if (!pcm)
178 		return -ENOMEM;
179 
180 	/* dai_link id is 1:1 mapped to the PCM device */
181 	pcm->device = rtd->dai_link->id;
182 	pcm->codec_dai = dai;
183 
184 	list_add_tail(&pcm->head, &ctx->hdmi_pcm_list);
185 
186 	return 0;
187 }
188 
189 #define IDISP_CODEC_MASK	0x4
190 
191 static struct snd_soc_dai_link *sof_card_dai_links_create(struct device *dev,
192 							  int ssp_codec,
193 							  int dmic_be_num,
194 							  int hdmi_num,
195 							  bool idisp_codec)
196 {
197 	struct snd_soc_dai_link_component *idisp_components;
198 	struct snd_soc_dai_link_component *cpus;
199 	struct snd_soc_dai_link *links;
200 	int i, id = 0;
201 
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);
206 	if (!links || !cpus)
207 		return NULL;
208 
209 	/* HDMI-In SSP */
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;
213 
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);
219 
220 			links[id].cpus = &cpus[id];
221 			links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
222 								  "SSP%d Pin", port);
223 			if (!links[id].cpus->dai_name)
224 				return NULL;
225 			links[id].name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-HDMI", port);
226 			if (!links[id].name)
227 				return NULL;
228 			links[id].id = id;
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;
236 			id++;
237 		}
238 	}
239 
240 	/* codec SSP */
241 	links[id].name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-Codec", ssp_codec);
242 	if (!links[id].name)
243 		return NULL;
244 
245 	links[id].id = id;
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]);
250 	}
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)
259 		return NULL;
260 
261 	id++;
262 
263 	/* dmic */
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";
274 			dmic_be_num = 2;
275 		}
276 	}
277 
278 	for (i = 0; i < dmic_be_num; i++) {
279 		links[id].id = id;
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;
288 		id++;
289 	}
290 
291 	/* HDMI playback */
292 	if (sof_ssp_amp_quirk & SOF_HDMI_PLAYBACK_PRESENT) {
293 		/* HDMI */
294 		if (hdmi_num > 0) {
295 			idisp_components = devm_kzalloc(dev,
296 					   sizeof(struct snd_soc_dai_link_component) *
297 					   hdmi_num, GFP_KERNEL);
298 			if (!idisp_components)
299 				goto devm_err;
300 		}
301 		for (i = 1; i <= hdmi_num; i++) {
302 			links[id].name = devm_kasprintf(dev, GFP_KERNEL,
303 							"iDisp%d", i);
304 			if (!links[id].name)
305 				goto devm_err;
306 
307 			links[id].id = id;
308 			links[id].cpus = &cpus[id];
309 			links[id].num_cpus = 1;
310 			links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
311 								  "iDisp%d Pin", i);
312 			if (!links[id].cpus->dai_name)
313 				goto devm_err;
314 
315 			if (idisp_codec) {
316 				idisp_components[i - 1].name = "ehdaudio0D2";
317 				idisp_components[i - 1].dai_name = devm_kasprintf(dev,
318 										  GFP_KERNEL,
319 										  "intel-hdmi-hifi%d",
320 										  i);
321 				if (!idisp_components[i - 1].dai_name)
322 					goto devm_err;
323 			} else {
324 				idisp_components[i - 1].name = "snd-soc-dummy";
325 				idisp_components[i - 1].dai_name = "snd-soc-dummy-dai";
326 			}
327 
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;
335 			id++;
336 		}
337 	}
338 
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;
343 
344 		links[id].id = id;
345 		links[id].cpus = &cpus[id];
346 		links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
347 							  "SSP%d Pin", port);
348 		if (!links[id].cpus->dai_name)
349 			goto devm_err;
350 		links[id].name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-BT", port);
351 		if (!links[id].name)
352 			goto devm_err;
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;
361 		id++;
362 	}
363 
364 	return links;
365 devm_err:
366 	return NULL;
367 }
368 
369 static int sof_ssp_amp_probe(struct platform_device *pdev)
370 {
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;
375 	int ret, ssp_codec;
376 
377 	ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
378 	if (!ctx)
379 		return -ENOMEM;
380 
381 	if (pdev->id_entry && pdev->id_entry->driver_data)
382 		sof_ssp_amp_quirk = (unsigned long)pdev->id_entry->driver_data;
383 
384 	mach = pdev->dev.platform_data;
385 
386 	dmic_be_num = mach->mach_params.dmic_num;
387 
388 	ssp_codec = sof_ssp_amp_quirk & SOF_AMPLIFIER_SSP_MASK;
389 
390 	/* set number of dai links */
391 	sof_ssp_amp_card.num_links = 1 + dmic_be_num;
392 
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;
396 
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 */
401 		if (!hdmi_num)
402 			hdmi_num = 3;
403 
404 		if (mach->mach_params.codec_mask & IDISP_CODEC_MASK)
405 			ctx->idisp_codec = true;
406 
407 		sof_ssp_amp_card.num_links += hdmi_num;
408 	}
409 
410 	if (sof_ssp_amp_quirk & SOF_SSP_BT_OFFLOAD_PRESENT)
411 		sof_ssp_amp_card.num_links++;
412 
413 	dai_links = sof_card_dai_links_create(&pdev->dev, ssp_codec, dmic_be_num, hdmi_num, ctx->idisp_codec);
414 	if (!dai_links)
415 		return -ENOMEM;
416 
417 	sof_ssp_amp_card.dai_link = dai_links;
418 
419 	/* update codec_conf */
420 	if (sof_ssp_amp_quirk & SOF_CS35L41_SPEAKER_AMP_PRESENT) {
421 		cs35l41_set_codec_conf(&sof_ssp_amp_card);
422 	}
423 
424 	INIT_LIST_HEAD(&ctx->hdmi_pcm_list);
425 
426 	sof_ssp_amp_card.dev = &pdev->dev;
427 
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);
431 	if (ret)
432 		return ret;
433 
434 	ctx->common_hdmi_codec_drv = mach->mach_params.common_hdmi_codec_drv;
435 
436 	snd_soc_card_set_drvdata(&sof_ssp_amp_card, ctx);
437 
438 	return devm_snd_soc_register_card(&pdev->dev, &sof_ssp_amp_card);
439 }
440 
441 static const struct platform_device_id board_ids[] = {
442 	{
443 		.name = "sof_ssp_amp",
444 	},
445 	{
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),
453 	},
454 	{
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),
462 	},
463 	{ }
464 };
465 MODULE_DEVICE_TABLE(platform, board_ids);
466 
467 static struct platform_driver sof_ssp_amp_driver = {
468 	.probe          = sof_ssp_amp_probe,
469 	.driver = {
470 		.name   = "sof_ssp_amp",
471 		.pm = &snd_soc_pm_ops,
472 	},
473 	.id_table = board_ids,
474 };
475 module_platform_driver(sof_ssp_amp_driver);
476 
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);
484