xref: /linux/sound/soc/intel/boards/sof_ssp_amp.c (revision 99a15348d5842b3c1f95220dc9b119ee0fe0d81b)
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 
123 		if (err)
124 			return err;
125 
126 		err = hdac_hdmi_jack_init(pcm->codec_dai, pcm->device,
127 					  &pcm->sof_hdmi);
128 		if (err < 0)
129 			return err;
130 
131 		i++;
132 	}
133 
134 	return hdac_hdmi_jack_port_init(component, &card->dapm);
135 }
136 
137 static struct snd_soc_card sof_ssp_amp_card = {
138 	.name         = "ssp_amp",
139 	.owner        = THIS_MODULE,
140 	.dapm_widgets = sof_ssp_amp_dapm_widgets,
141 	.num_dapm_widgets = ARRAY_SIZE(sof_ssp_amp_dapm_widgets),
142 	.dapm_routes = sof_ssp_amp_dapm_routes,
143 	.num_dapm_routes = ARRAY_SIZE(sof_ssp_amp_dapm_routes),
144 	.fully_routed = true,
145 	.late_probe = sof_card_late_probe,
146 };
147 
148 static struct snd_soc_dai_link_component platform_component[] = {
149 	{
150 		/* name might be overridden during probe */
151 		.name = "0000:00:1f.3"
152 	}
153 };
154 
155 static struct snd_soc_dai_link_component dmic_component[] = {
156 	{
157 		.name = "dmic-codec",
158 		.dai_name = "dmic-hifi",
159 	}
160 };
161 
162 static struct snd_soc_dai_link_component dummy_component[] = {
163 	{
164 		.name = "snd-soc-dummy",
165 		.dai_name = "snd-soc-dummy-dai",
166 	}
167 };
168 
169 static int sof_hdmi_init(struct snd_soc_pcm_runtime *rtd)
170 {
171 	struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
172 	struct snd_soc_dai *dai = asoc_rtd_to_codec(rtd, 0);
173 	struct sof_hdmi_pcm *pcm;
174 
175 	pcm = devm_kzalloc(rtd->card->dev, sizeof(*pcm), GFP_KERNEL);
176 	if (!pcm)
177 		return -ENOMEM;
178 
179 	/* dai_link id is 1:1 mapped to the PCM device */
180 	pcm->device = rtd->dai_link->id;
181 	pcm->codec_dai = dai;
182 
183 	list_add_tail(&pcm->head, &ctx->hdmi_pcm_list);
184 
185 	return 0;
186 }
187 
188 #define IDISP_CODEC_MASK	0x4
189 
190 static struct snd_soc_dai_link *sof_card_dai_links_create(struct device *dev,
191 							  int ssp_codec,
192 							  int dmic_be_num,
193 							  int hdmi_num,
194 							  bool idisp_codec)
195 {
196 	struct snd_soc_dai_link_component *idisp_components;
197 	struct snd_soc_dai_link_component *cpus;
198 	struct snd_soc_dai_link *links;
199 	int i, id = 0;
200 
201 	links = devm_kzalloc(dev, sizeof(struct snd_soc_dai_link) *
202 					sof_ssp_amp_card.num_links, GFP_KERNEL);
203 	cpus = devm_kzalloc(dev, sizeof(struct snd_soc_dai_link_component) *
204 					sof_ssp_amp_card.num_links, GFP_KERNEL);
205 	if (!links || !cpus)
206 		return NULL;
207 
208 	/* HDMI-In SSP */
209 	if (sof_ssp_amp_quirk & SOF_SSP_HDMI_CAPTURE_PRESENT) {
210 		int num_of_hdmi_ssp = (sof_ssp_amp_quirk & SOF_NO_OF_HDMI_CAPTURE_SSP_MASK) >>
211 				SOF_NO_OF_HDMI_CAPTURE_SSP_SHIFT;
212 
213 		for (i = 1; i <= num_of_hdmi_ssp; i++) {
214 			int port = (i == 1 ? (sof_ssp_amp_quirk & SOF_HDMI_CAPTURE_1_SSP_MASK) >>
215 						SOF_HDMI_CAPTURE_1_SSP_SHIFT :
216 						(sof_ssp_amp_quirk & SOF_HDMI_CAPTURE_2_SSP_MASK) >>
217 						SOF_HDMI_CAPTURE_2_SSP_SHIFT);
218 
219 			links[id].cpus = &cpus[id];
220 			links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
221 								  "SSP%d Pin", port);
222 			if (!links[id].cpus->dai_name)
223 				return NULL;
224 			links[id].name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-HDMI", port);
225 			if (!links[id].name)
226 				return NULL;
227 			links[id].id = id;
228 			links[id].codecs = dummy_component;
229 			links[id].num_codecs = ARRAY_SIZE(dummy_component);
230 			links[id].platforms = platform_component;
231 			links[id].num_platforms = ARRAY_SIZE(platform_component);
232 			links[id].dpcm_capture = 1;
233 			links[id].no_pcm = 1;
234 			links[id].num_cpus = 1;
235 			id++;
236 		}
237 	}
238 
239 	/* codec SSP */
240 	links[id].name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-Codec", ssp_codec);
241 	if (!links[id].name)
242 		return NULL;
243 
244 	links[id].id = id;
245 	if (sof_ssp_amp_quirk & SOF_RT1308_SPEAKER_AMP_PRESENT) {
246 		sof_rt1308_dai_link(&links[id]);
247 	} else if (sof_ssp_amp_quirk & SOF_CS35L41_SPEAKER_AMP_PRESENT) {
248 		cs35l41_set_dai_link(&links[id]);
249 	}
250 	links[id].platforms = platform_component;
251 	links[id].num_platforms = ARRAY_SIZE(platform_component);
252 	links[id].dpcm_playback = 1;
253 	links[id].no_pcm = 1;
254 	links[id].cpus = &cpus[id];
255 	links[id].num_cpus = 1;
256 	links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d Pin", ssp_codec);
257 	if (!links[id].cpus->dai_name)
258 		return NULL;
259 
260 	id++;
261 
262 	/* dmic */
263 	if (dmic_be_num > 0) {
264 		/* at least we have dmic01 */
265 		links[id].name = "dmic01";
266 		links[id].cpus = &cpus[id];
267 		links[id].cpus->dai_name = "DMIC01 Pin";
268 		if (dmic_be_num > 1) {
269 			/* set up 2 BE links at most */
270 			links[id + 1].name = "dmic16k";
271 			links[id + 1].cpus = &cpus[id + 1];
272 			links[id + 1].cpus->dai_name = "DMIC16k Pin";
273 			dmic_be_num = 2;
274 		}
275 	}
276 
277 	for (i = 0; i < dmic_be_num; i++) {
278 		links[id].id = id;
279 		links[id].num_cpus = 1;
280 		links[id].codecs = dmic_component;
281 		links[id].num_codecs = ARRAY_SIZE(dmic_component);
282 		links[id].platforms = platform_component;
283 		links[id].num_platforms = ARRAY_SIZE(platform_component);
284 		links[id].ignore_suspend = 1;
285 		links[id].dpcm_capture = 1;
286 		links[id].no_pcm = 1;
287 		id++;
288 	}
289 
290 	/* HDMI playback */
291 	if (sof_ssp_amp_quirk & SOF_HDMI_PLAYBACK_PRESENT) {
292 		/* HDMI */
293 		if (hdmi_num > 0) {
294 			idisp_components = devm_kzalloc(dev,
295 					   sizeof(struct snd_soc_dai_link_component) *
296 					   hdmi_num, GFP_KERNEL);
297 			if (!idisp_components)
298 				goto devm_err;
299 		}
300 		for (i = 1; i <= hdmi_num; i++) {
301 			links[id].name = devm_kasprintf(dev, GFP_KERNEL,
302 							"iDisp%d", i);
303 			if (!links[id].name)
304 				goto devm_err;
305 
306 			links[id].id = id;
307 			links[id].cpus = &cpus[id];
308 			links[id].num_cpus = 1;
309 			links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
310 								  "iDisp%d Pin", i);
311 			if (!links[id].cpus->dai_name)
312 				goto devm_err;
313 
314 			if (idisp_codec) {
315 				idisp_components[i - 1].name = "ehdaudio0D2";
316 				idisp_components[i - 1].dai_name = devm_kasprintf(dev,
317 										  GFP_KERNEL,
318 										  "intel-hdmi-hifi%d",
319 										  i);
320 				if (!idisp_components[i - 1].dai_name)
321 					goto devm_err;
322 			} else {
323 				idisp_components[i - 1].name = "snd-soc-dummy";
324 				idisp_components[i - 1].dai_name = "snd-soc-dummy-dai";
325 			}
326 
327 			links[id].codecs = &idisp_components[i - 1];
328 			links[id].num_codecs = 1;
329 			links[id].platforms = platform_component;
330 			links[id].num_platforms = ARRAY_SIZE(platform_component);
331 			links[id].init = sof_hdmi_init;
332 			links[id].dpcm_playback = 1;
333 			links[id].no_pcm = 1;
334 			id++;
335 		}
336 	}
337 
338 	/* BT audio offload */
339 	if (sof_ssp_amp_quirk & SOF_SSP_BT_OFFLOAD_PRESENT) {
340 		int port = (sof_ssp_amp_quirk & SOF_BT_OFFLOAD_SSP_MASK) >>
341 				SOF_BT_OFFLOAD_SSP_SHIFT;
342 
343 		links[id].id = id;
344 		links[id].cpus = &cpus[id];
345 		links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
346 							  "SSP%d Pin", port);
347 		if (!links[id].cpus->dai_name)
348 			goto devm_err;
349 		links[id].name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-BT", port);
350 		if (!links[id].name)
351 			goto devm_err;
352 		links[id].codecs = dummy_component;
353 		links[id].num_codecs = ARRAY_SIZE(dummy_component);
354 		links[id].platforms = platform_component;
355 		links[id].num_platforms = ARRAY_SIZE(platform_component);
356 		links[id].dpcm_playback = 1;
357 		links[id].dpcm_capture = 1;
358 		links[id].no_pcm = 1;
359 		links[id].num_cpus = 1;
360 		id++;
361 	}
362 
363 	return links;
364 devm_err:
365 	return NULL;
366 }
367 
368 static int sof_ssp_amp_probe(struct platform_device *pdev)
369 {
370 	struct snd_soc_dai_link *dai_links;
371 	struct snd_soc_acpi_mach *mach;
372 	struct sof_card_private *ctx;
373 	int dmic_be_num, hdmi_num = 0;
374 	int ret, ssp_codec;
375 
376 	ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
377 	if (!ctx)
378 		return -ENOMEM;
379 
380 	if (pdev->id_entry && pdev->id_entry->driver_data)
381 		sof_ssp_amp_quirk = (unsigned long)pdev->id_entry->driver_data;
382 
383 	mach = pdev->dev.platform_data;
384 
385 	dmic_be_num = mach->mach_params.dmic_num;
386 
387 	ssp_codec = sof_ssp_amp_quirk & SOF_AMPLIFIER_SSP_MASK;
388 
389 	/* set number of dai links */
390 	sof_ssp_amp_card.num_links = 1 + dmic_be_num;
391 
392 	if (sof_ssp_amp_quirk & SOF_SSP_HDMI_CAPTURE_PRESENT)
393 		sof_ssp_amp_card.num_links += (sof_ssp_amp_quirk & SOF_NO_OF_HDMI_CAPTURE_SSP_MASK) >>
394 				SOF_NO_OF_HDMI_CAPTURE_SSP_SHIFT;
395 
396 	if (sof_ssp_amp_quirk & SOF_HDMI_PLAYBACK_PRESENT) {
397 		hdmi_num = (sof_ssp_amp_quirk & SOF_NO_OF_HDMI_PLAYBACK_MASK) >>
398 				SOF_NO_OF_HDMI_PLAYBACK_SHIFT;
399 		/* default number of HDMI DAI's */
400 		if (!hdmi_num)
401 			hdmi_num = 3;
402 
403 		if (mach->mach_params.codec_mask & IDISP_CODEC_MASK)
404 			ctx->idisp_codec = true;
405 
406 		sof_ssp_amp_card.num_links += hdmi_num;
407 	}
408 
409 	if (sof_ssp_amp_quirk & SOF_SSP_BT_OFFLOAD_PRESENT)
410 		sof_ssp_amp_card.num_links++;
411 
412 	dai_links = sof_card_dai_links_create(&pdev->dev, ssp_codec, dmic_be_num, hdmi_num, ctx->idisp_codec);
413 	if (!dai_links)
414 		return -ENOMEM;
415 
416 	sof_ssp_amp_card.dai_link = dai_links;
417 
418 	/* update codec_conf */
419 	if (sof_ssp_amp_quirk & SOF_CS35L41_SPEAKER_AMP_PRESENT) {
420 		cs35l41_set_codec_conf(&sof_ssp_amp_card);
421 	}
422 
423 	INIT_LIST_HEAD(&ctx->hdmi_pcm_list);
424 
425 	sof_ssp_amp_card.dev = &pdev->dev;
426 
427 	/* set platform name for each dailink */
428 	ret = snd_soc_fixup_dai_links_platform_name(&sof_ssp_amp_card,
429 						    mach->mach_params.platform);
430 	if (ret)
431 		return ret;
432 
433 	ctx->common_hdmi_codec_drv = mach->mach_params.common_hdmi_codec_drv;
434 
435 	snd_soc_card_set_drvdata(&sof_ssp_amp_card, ctx);
436 
437 	return devm_snd_soc_register_card(&pdev->dev, &sof_ssp_amp_card);
438 }
439 
440 static const struct platform_device_id board_ids[] = {
441 	{
442 		.name = "sof_ssp_amp",
443 	},
444 	{
445 		.name = "tgl_rt1308_hdmi_ssp",
446 		.driver_data = (kernel_ulong_t)(SOF_AMPLIFIER_SSP(2) |
447 					SOF_NO_OF_HDMI_CAPTURE_SSP(2) |
448 					SOF_HDMI_CAPTURE_1_SSP(1) |
449 					SOF_HDMI_CAPTURE_2_SSP(5) |
450 					SOF_SSP_HDMI_CAPTURE_PRESENT |
451 					SOF_RT1308_SPEAKER_AMP_PRESENT),
452 	},
453 	{
454 		.name = "adl_cs35l41",
455 		.driver_data = (kernel_ulong_t)(SOF_AMPLIFIER_SSP(1) |
456 					SOF_NO_OF_HDMI_PLAYBACK(4) |
457 					SOF_HDMI_PLAYBACK_PRESENT |
458 					SOF_BT_OFFLOAD_SSP(2) |
459 					SOF_SSP_BT_OFFLOAD_PRESENT |
460 					SOF_CS35L41_SPEAKER_AMP_PRESENT),
461 	},
462 	{ }
463 };
464 MODULE_DEVICE_TABLE(platform, board_ids);
465 
466 static struct platform_driver sof_ssp_amp_driver = {
467 	.probe          = sof_ssp_amp_probe,
468 	.driver = {
469 		.name   = "sof_ssp_amp",
470 		.pm = &snd_soc_pm_ops,
471 	},
472 	.id_table = board_ids,
473 };
474 module_platform_driver(sof_ssp_amp_driver);
475 
476 MODULE_DESCRIPTION("ASoC Intel(R) SOF Amplifier Machine driver");
477 MODULE_AUTHOR("balamurugan.c <balamurugan.c@intel.com>");
478 MODULE_AUTHOR("Brent Lu <brent.lu@intel.com>");
479 MODULE_LICENSE("GPL");
480 MODULE_IMPORT_NS(SND_SOC_INTEL_HDA_DSP_COMMON);
481 MODULE_IMPORT_NS(SND_SOC_INTEL_SOF_REALTEK_COMMON);
482 MODULE_IMPORT_NS(SND_SOC_INTEL_SOF_CIRRUS_COMMON);
483