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/dmi.h>
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <sound/core.h>
16 #include <sound/jack.h>
17 #include <sound/pcm.h>
18 #include <sound/pcm_params.h>
19 #include <sound/sof.h>
20 #include "../../codecs/hdac_hdmi.h"
21 #include "hda_dsp_common.h"
22 #include "sof_realtek_common.h"
23 #include "sof_cirrus_common.h"
24 
25 #define NAME_SIZE 32
26 
27 /* SSP port ID for speaker amplifier */
28 #define SOF_AMPLIFIER_SSP(quirk)		((quirk) & GENMASK(3, 0))
29 #define SOF_AMPLIFIER_SSP_MASK			(GENMASK(3, 0))
30 
31 /* HDMI capture*/
32 #define SOF_SSP_HDMI_CAPTURE_PRESENT		BIT(4)
33 #define SOF_NO_OF_HDMI_CAPTURE_SSP_SHIFT		5
34 #define SOF_NO_OF_HDMI_CAPTURE_SSP_MASK		(GENMASK(6, 5))
35 #define SOF_NO_OF_HDMI_CAPTURE_SSP(quirk)	\
36 	(((quirk) << SOF_NO_OF_HDMI_CAPTURE_SSP_SHIFT) & SOF_NO_OF_HDMI_CAPTURE_SSP_MASK)
37 
38 #define SOF_HDMI_CAPTURE_1_SSP_SHIFT		7
39 #define SOF_HDMI_CAPTURE_1_SSP_MASK		(GENMASK(9, 7))
40 #define SOF_HDMI_CAPTURE_1_SSP(quirk)	\
41 	(((quirk) << SOF_HDMI_CAPTURE_1_SSP_SHIFT) & SOF_HDMI_CAPTURE_1_SSP_MASK)
42 
43 #define SOF_HDMI_CAPTURE_2_SSP_SHIFT		10
44 #define SOF_HDMI_CAPTURE_2_SSP_MASK		(GENMASK(12, 10))
45 #define SOF_HDMI_CAPTURE_2_SSP(quirk)	\
46 	(((quirk) << SOF_HDMI_CAPTURE_2_SSP_SHIFT) & SOF_HDMI_CAPTURE_2_SSP_MASK)
47 
48 /* HDMI playback */
49 #define SOF_HDMI_PLAYBACK_PRESENT		BIT(13)
50 #define SOF_NO_OF_HDMI_PLAYBACK_SHIFT		14
51 #define SOF_NO_OF_HDMI_PLAYBACK_MASK		(GENMASK(16, 14))
52 #define SOF_NO_OF_HDMI_PLAYBACK(quirk)	\
53 	(((quirk) << SOF_NO_OF_HDMI_PLAYBACK_SHIFT) & SOF_NO_OF_HDMI_PLAYBACK_MASK)
54 
55 /* BT audio offload */
56 #define SOF_SSP_BT_OFFLOAD_PRESENT		BIT(17)
57 #define SOF_BT_OFFLOAD_SSP_SHIFT		18
58 #define SOF_BT_OFFLOAD_SSP_MASK			(GENMASK(20, 18))
59 #define SOF_BT_OFFLOAD_SSP(quirk)	\
60 	(((quirk) << SOF_BT_OFFLOAD_SSP_SHIFT) & SOF_BT_OFFLOAD_SSP_MASK)
61 
62 /* Speaker amplifiers */
63 #define SOF_RT1308_SPEAKER_AMP_PRESENT		BIT(21)
64 #define SOF_CS35L41_SPEAKER_AMP_PRESENT		BIT(22)
65 
66 /* Default: SSP2  */
67 static unsigned long sof_ssp_amp_quirk = SOF_AMPLIFIER_SSP(2);
68 
69 struct sof_hdmi_pcm {
70 	struct list_head head;
71 	struct snd_soc_jack sof_hdmi;
72 	struct snd_soc_dai *codec_dai;
73 	int device;
74 };
75 
76 struct sof_card_private {
77 	struct list_head hdmi_pcm_list;
78 	bool common_hdmi_codec_drv;
79 	bool idisp_codec;
80 };
81 
82 static const struct dmi_system_id chromebook_platforms[] = {
83 	{
84 		.ident = "Google Chromebooks",
85 		.matches = {
86 			DMI_MATCH(DMI_SYS_VENDOR, "Google"),
87 		}
88 	},
89 	{},
90 };
91 
92 static const struct snd_soc_dapm_widget sof_ssp_amp_dapm_widgets[] = {
93 	SND_SOC_DAPM_MIC("SoC DMIC", NULL),
94 };
95 
96 static const struct snd_soc_dapm_route sof_ssp_amp_dapm_routes[] = {
97 	/* digital mics */
98 	{"DMic", NULL, "SoC DMIC"},
99 };
100 
101 static int sof_card_late_probe(struct snd_soc_card *card)
102 {
103 	struct sof_card_private *ctx = snd_soc_card_get_drvdata(card);
104 	struct snd_soc_component *component = NULL;
105 	char jack_name[NAME_SIZE];
106 	struct sof_hdmi_pcm *pcm;
107 	int err;
108 	int i;
109 
110 	if (!(sof_ssp_amp_quirk & SOF_HDMI_PLAYBACK_PRESENT))
111 		return 0;
112 
113 	/* HDMI is not supported by SOF on Baytrail/CherryTrail */
114 	if (!ctx->idisp_codec)
115 		return 0;
116 
117 	if (list_empty(&ctx->hdmi_pcm_list))
118 		return -EINVAL;
119 
120 	if (ctx->common_hdmi_codec_drv) {
121 		pcm = list_first_entry(&ctx->hdmi_pcm_list, struct sof_hdmi_pcm,
122 				       head);
123 		component = pcm->codec_dai->component;
124 		return hda_dsp_hdmi_build_controls(card, component);
125 	}
126 
127 	i = 0;
128 	list_for_each_entry(pcm, &ctx->hdmi_pcm_list, head) {
129 		component = pcm->codec_dai->component;
130 		snprintf(jack_name, sizeof(jack_name),
131 			 "HDMI/DP, pcm=%d Jack", pcm->device);
132 		err = snd_soc_card_jack_new(card, jack_name,
133 					    SND_JACK_AVOUT, &pcm->sof_hdmi);
134 
135 		if (err)
136 			return err;
137 
138 		err = hdac_hdmi_jack_init(pcm->codec_dai, pcm->device,
139 					  &pcm->sof_hdmi);
140 		if (err < 0)
141 			return err;
142 
143 		i++;
144 	}
145 
146 	return hdac_hdmi_jack_port_init(component, &card->dapm);
147 }
148 
149 static struct snd_soc_card sof_ssp_amp_card = {
150 	.name         = "ssp_amp",
151 	.owner        = THIS_MODULE,
152 	.dapm_widgets = sof_ssp_amp_dapm_widgets,
153 	.num_dapm_widgets = ARRAY_SIZE(sof_ssp_amp_dapm_widgets),
154 	.dapm_routes = sof_ssp_amp_dapm_routes,
155 	.num_dapm_routes = ARRAY_SIZE(sof_ssp_amp_dapm_routes),
156 	.fully_routed = true,
157 	.late_probe = sof_card_late_probe,
158 };
159 
160 static struct snd_soc_dai_link_component platform_component[] = {
161 	{
162 		/* name might be overridden during probe */
163 		.name = "0000:00:1f.3"
164 	}
165 };
166 
167 static struct snd_soc_dai_link_component dmic_component[] = {
168 	{
169 		.name = "dmic-codec",
170 		.dai_name = "dmic-hifi",
171 	}
172 };
173 
174 static struct snd_soc_dai_link_component dummy_component[] = {
175 	{
176 		.name = "snd-soc-dummy",
177 		.dai_name = "snd-soc-dummy-dai",
178 	}
179 };
180 
181 static int sof_hdmi_init(struct snd_soc_pcm_runtime *rtd)
182 {
183 	struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
184 	struct snd_soc_dai *dai = asoc_rtd_to_codec(rtd, 0);
185 	struct sof_hdmi_pcm *pcm;
186 
187 	pcm = devm_kzalloc(rtd->card->dev, sizeof(*pcm), GFP_KERNEL);
188 	if (!pcm)
189 		return -ENOMEM;
190 
191 	/* dai_link id is 1:1 mapped to the PCM device */
192 	pcm->device = rtd->dai_link->id;
193 	pcm->codec_dai = dai;
194 
195 	list_add_tail(&pcm->head, &ctx->hdmi_pcm_list);
196 
197 	return 0;
198 }
199 
200 #define IDISP_CODEC_MASK	0x4
201 
202 static struct snd_soc_dai_link *sof_card_dai_links_create(struct device *dev,
203 							  int ssp_codec,
204 							  int dmic_be_num,
205 							  int hdmi_num,
206 							  bool idisp_codec)
207 {
208 	struct snd_soc_dai_link_component *idisp_components;
209 	struct snd_soc_dai_link_component *cpus;
210 	struct snd_soc_dai_link *links;
211 	int i, id = 0;
212 
213 	links = devm_kzalloc(dev, sizeof(struct snd_soc_dai_link) *
214 					sof_ssp_amp_card.num_links, GFP_KERNEL);
215 	cpus = devm_kzalloc(dev, sizeof(struct snd_soc_dai_link_component) *
216 					sof_ssp_amp_card.num_links, GFP_KERNEL);
217 	if (!links || !cpus)
218 		return NULL;
219 
220 	/* HDMI-In SSP */
221 	if (sof_ssp_amp_quirk & SOF_SSP_HDMI_CAPTURE_PRESENT) {
222 		int num_of_hdmi_ssp = (sof_ssp_amp_quirk & SOF_NO_OF_HDMI_CAPTURE_SSP_MASK) >>
223 				SOF_NO_OF_HDMI_CAPTURE_SSP_SHIFT;
224 
225 		for (i = 1; i <= num_of_hdmi_ssp; i++) {
226 			int port = (i == 1 ? (sof_ssp_amp_quirk & SOF_HDMI_CAPTURE_1_SSP_MASK) >>
227 						SOF_HDMI_CAPTURE_1_SSP_SHIFT :
228 						(sof_ssp_amp_quirk & SOF_HDMI_CAPTURE_2_SSP_MASK) >>
229 						SOF_HDMI_CAPTURE_2_SSP_SHIFT);
230 
231 			links[id].cpus = &cpus[id];
232 			links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
233 								  "SSP%d Pin", port);
234 			if (!links[id].cpus->dai_name)
235 				return NULL;
236 			links[id].name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-HDMI", port);
237 			if (!links[id].name)
238 				return NULL;
239 			links[id].id = id;
240 			links[id].codecs = dummy_component;
241 			links[id].num_codecs = ARRAY_SIZE(dummy_component);
242 			links[id].platforms = platform_component;
243 			links[id].num_platforms = ARRAY_SIZE(platform_component);
244 			links[id].dpcm_capture = 1;
245 			links[id].no_pcm = 1;
246 			links[id].num_cpus = 1;
247 			id++;
248 		}
249 	}
250 
251 	/* codec SSP */
252 	links[id].name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-Codec", ssp_codec);
253 	if (!links[id].name)
254 		return NULL;
255 
256 	links[id].id = id;
257 	if (sof_ssp_amp_quirk & SOF_RT1308_SPEAKER_AMP_PRESENT) {
258 		sof_rt1308_dai_link(&links[id]);
259 	} else if (sof_ssp_amp_quirk & SOF_CS35L41_SPEAKER_AMP_PRESENT) {
260 		cs35l41_set_dai_link(&links[id]);
261 	}
262 	links[id].platforms = platform_component;
263 	links[id].num_platforms = ARRAY_SIZE(platform_component);
264 	links[id].dpcm_playback = 1;
265 	links[id].no_pcm = 1;
266 	links[id].cpus = &cpus[id];
267 	links[id].num_cpus = 1;
268 	links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d Pin", ssp_codec);
269 	if (!links[id].cpus->dai_name)
270 		return NULL;
271 
272 	id++;
273 
274 	/* dmic */
275 	if (dmic_be_num > 0) {
276 		/* at least we have dmic01 */
277 		links[id].name = "dmic01";
278 		links[id].cpus = &cpus[id];
279 		links[id].cpus->dai_name = "DMIC01 Pin";
280 		if (dmic_be_num > 1) {
281 			/* set up 2 BE links at most */
282 			links[id + 1].name = "dmic16k";
283 			links[id + 1].cpus = &cpus[id + 1];
284 			links[id + 1].cpus->dai_name = "DMIC16k Pin";
285 			dmic_be_num = 2;
286 		}
287 	}
288 
289 	for (i = 0; i < dmic_be_num; i++) {
290 		links[id].id = id;
291 		links[id].num_cpus = 1;
292 		links[id].codecs = dmic_component;
293 		links[id].num_codecs = ARRAY_SIZE(dmic_component);
294 		links[id].platforms = platform_component;
295 		links[id].num_platforms = ARRAY_SIZE(platform_component);
296 		links[id].ignore_suspend = 1;
297 		links[id].dpcm_capture = 1;
298 		links[id].no_pcm = 1;
299 		id++;
300 	}
301 
302 	/* HDMI playback */
303 	if (sof_ssp_amp_quirk & SOF_HDMI_PLAYBACK_PRESENT) {
304 		/* HDMI */
305 		if (hdmi_num > 0) {
306 			idisp_components = devm_kzalloc(dev,
307 					   sizeof(struct snd_soc_dai_link_component) *
308 					   hdmi_num, GFP_KERNEL);
309 			if (!idisp_components)
310 				goto devm_err;
311 		}
312 		for (i = 1; i <= hdmi_num; i++) {
313 			links[id].name = devm_kasprintf(dev, GFP_KERNEL,
314 							"iDisp%d", i);
315 			if (!links[id].name)
316 				goto devm_err;
317 
318 			links[id].id = id;
319 			links[id].cpus = &cpus[id];
320 			links[id].num_cpus = 1;
321 			links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
322 								  "iDisp%d Pin", i);
323 			if (!links[id].cpus->dai_name)
324 				goto devm_err;
325 
326 			if (idisp_codec) {
327 				idisp_components[i - 1].name = "ehdaudio0D2";
328 				idisp_components[i - 1].dai_name = devm_kasprintf(dev,
329 										  GFP_KERNEL,
330 										  "intel-hdmi-hifi%d",
331 										  i);
332 				if (!idisp_components[i - 1].dai_name)
333 					goto devm_err;
334 			} else {
335 				idisp_components[i - 1].name = "snd-soc-dummy";
336 				idisp_components[i - 1].dai_name = "snd-soc-dummy-dai";
337 			}
338 
339 			links[id].codecs = &idisp_components[i - 1];
340 			links[id].num_codecs = 1;
341 			links[id].platforms = platform_component;
342 			links[id].num_platforms = ARRAY_SIZE(platform_component);
343 			links[id].init = sof_hdmi_init;
344 			links[id].dpcm_playback = 1;
345 			links[id].no_pcm = 1;
346 			id++;
347 		}
348 	}
349 
350 	/* BT audio offload */
351 	if (sof_ssp_amp_quirk & SOF_SSP_BT_OFFLOAD_PRESENT) {
352 		int port = (sof_ssp_amp_quirk & SOF_BT_OFFLOAD_SSP_MASK) >>
353 				SOF_BT_OFFLOAD_SSP_SHIFT;
354 
355 		links[id].id = id;
356 		links[id].cpus = &cpus[id];
357 		links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
358 							  "SSP%d Pin", port);
359 		if (!links[id].cpus->dai_name)
360 			goto devm_err;
361 		links[id].name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-BT", port);
362 		if (!links[id].name)
363 			goto devm_err;
364 		links[id].codecs = dummy_component;
365 		links[id].num_codecs = ARRAY_SIZE(dummy_component);
366 		links[id].platforms = platform_component;
367 		links[id].num_platforms = ARRAY_SIZE(platform_component);
368 		links[id].dpcm_playback = 1;
369 		links[id].dpcm_capture = 1;
370 		links[id].no_pcm = 1;
371 		links[id].num_cpus = 1;
372 		id++;
373 	}
374 
375 	return links;
376 devm_err:
377 	return NULL;
378 }
379 
380 static int sof_ssp_amp_probe(struct platform_device *pdev)
381 {
382 	struct snd_soc_dai_link *dai_links;
383 	struct snd_soc_acpi_mach *mach;
384 	struct sof_card_private *ctx;
385 	int dmic_be_num = 0, hdmi_num = 0;
386 	int ret, ssp_codec;
387 
388 	ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
389 	if (!ctx)
390 		return -ENOMEM;
391 
392 	if (pdev->id_entry && pdev->id_entry->driver_data)
393 		sof_ssp_amp_quirk = (unsigned long)pdev->id_entry->driver_data;
394 
395 	mach = pdev->dev.platform_data;
396 
397 	if (dmi_check_system(chromebook_platforms) || mach->mach_params.dmic_num > 0)
398 		dmic_be_num = 2;
399 
400 	ssp_codec = sof_ssp_amp_quirk & SOF_AMPLIFIER_SSP_MASK;
401 
402 	/* set number of dai links */
403 	sof_ssp_amp_card.num_links = 1 + dmic_be_num;
404 
405 	if (sof_ssp_amp_quirk & SOF_SSP_HDMI_CAPTURE_PRESENT)
406 		sof_ssp_amp_card.num_links += (sof_ssp_amp_quirk & SOF_NO_OF_HDMI_CAPTURE_SSP_MASK) >>
407 				SOF_NO_OF_HDMI_CAPTURE_SSP_SHIFT;
408 
409 	if (sof_ssp_amp_quirk & SOF_HDMI_PLAYBACK_PRESENT) {
410 		hdmi_num = (sof_ssp_amp_quirk & SOF_NO_OF_HDMI_PLAYBACK_MASK) >>
411 				SOF_NO_OF_HDMI_PLAYBACK_SHIFT;
412 		/* default number of HDMI DAI's */
413 		if (!hdmi_num)
414 			hdmi_num = 3;
415 
416 		if (mach->mach_params.codec_mask & IDISP_CODEC_MASK)
417 			ctx->idisp_codec = true;
418 
419 		sof_ssp_amp_card.num_links += hdmi_num;
420 	}
421 
422 	if (sof_ssp_amp_quirk & SOF_SSP_BT_OFFLOAD_PRESENT)
423 		sof_ssp_amp_card.num_links++;
424 
425 	dai_links = sof_card_dai_links_create(&pdev->dev, ssp_codec, dmic_be_num, hdmi_num, ctx->idisp_codec);
426 	if (!dai_links)
427 		return -ENOMEM;
428 
429 	sof_ssp_amp_card.dai_link = dai_links;
430 
431 	/* update codec_conf */
432 	if (sof_ssp_amp_quirk & SOF_CS35L41_SPEAKER_AMP_PRESENT) {
433 		cs35l41_set_codec_conf(&sof_ssp_amp_card);
434 	}
435 
436 	INIT_LIST_HEAD(&ctx->hdmi_pcm_list);
437 
438 	sof_ssp_amp_card.dev = &pdev->dev;
439 
440 	/* set platform name for each dailink */
441 	ret = snd_soc_fixup_dai_links_platform_name(&sof_ssp_amp_card,
442 						    mach->mach_params.platform);
443 	if (ret)
444 		return ret;
445 
446 	ctx->common_hdmi_codec_drv = mach->mach_params.common_hdmi_codec_drv;
447 
448 	snd_soc_card_set_drvdata(&sof_ssp_amp_card, ctx);
449 
450 	return devm_snd_soc_register_card(&pdev->dev, &sof_ssp_amp_card);
451 }
452 
453 static const struct platform_device_id board_ids[] = {
454 	{
455 		.name = "sof_ssp_amp",
456 	},
457 	{
458 		.name = "tgl_rt1308_hdmi_ssp",
459 		.driver_data = (kernel_ulong_t)(SOF_AMPLIFIER_SSP(2) |
460 					SOF_NO_OF_HDMI_CAPTURE_SSP(2) |
461 					SOF_HDMI_CAPTURE_1_SSP(1) |
462 					SOF_HDMI_CAPTURE_2_SSP(5) |
463 					SOF_SSP_HDMI_CAPTURE_PRESENT |
464 					SOF_RT1308_SPEAKER_AMP_PRESENT),
465 	},
466 	{
467 		.name = "adl_cs35l41",
468 		.driver_data = (kernel_ulong_t)(SOF_AMPLIFIER_SSP(1) |
469 					SOF_NO_OF_HDMI_PLAYBACK(4) |
470 					SOF_HDMI_PLAYBACK_PRESENT |
471 					SOF_BT_OFFLOAD_SSP(2) |
472 					SOF_SSP_BT_OFFLOAD_PRESENT |
473 					SOF_CS35L41_SPEAKER_AMP_PRESENT),
474 	},
475 	{ }
476 };
477 MODULE_DEVICE_TABLE(platform, board_ids);
478 
479 static struct platform_driver sof_ssp_amp_driver = {
480 	.probe          = sof_ssp_amp_probe,
481 	.driver = {
482 		.name   = "sof_ssp_amp",
483 		.pm = &snd_soc_pm_ops,
484 	},
485 	.id_table = board_ids,
486 };
487 module_platform_driver(sof_ssp_amp_driver);
488 
489 MODULE_DESCRIPTION("ASoC Intel(R) SOF Amplifier Machine driver");
490 MODULE_AUTHOR("balamurugan.c <balamurugan.c@intel.com>");
491 MODULE_AUTHOR("Brent Lu <brent.lu@intel.com>");
492 MODULE_LICENSE("GPL");
493 MODULE_IMPORT_NS(SND_SOC_INTEL_HDA_DSP_COMMON);
494 MODULE_IMPORT_NS(SND_SOC_INTEL_SOF_REALTEK_COMMON);
495 MODULE_IMPORT_NS(SND_SOC_INTEL_SOF_CIRRUS_COMMON);
496