1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // Copyright(c) 2021-2022 Intel Corporation. All rights reserved.
4 //
5 // Authors: Cezary Rojewski <cezary.rojewski@intel.com>
6 //          Amadeusz Slawinski <amadeuszx.slawinski@linux.intel.com>
7 //
8 
9 #include <linux/acpi.h>
10 #include <linux/module.h>
11 #include <linux/dmi.h>
12 #include <linux/pci.h>
13 #include <linux/platform_device.h>
14 #include <sound/hda_codec.h>
15 #include <sound/hda_register.h>
16 #include <sound/intel-nhlt.h>
17 #include <sound/soc-acpi.h>
18 #include <sound/soc-component.h>
19 #include "avs.h"
20 
21 static bool i2s_test;
22 module_param(i2s_test, bool, 0444);
23 MODULE_PARM_DESC(i2s_test, "Probe I2S test-board and skip all other I2S boards");
24 
25 static const struct dmi_system_id kbl_dmi_table[] = {
26 	{
27 		.matches = {
28 			DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
29 			DMI_MATCH(DMI_BOARD_NAME, "Skylake Y LPDDR3 RVP3"),
30 		},
31 	},
32 	{
33 		.matches = {
34 			DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
35 			DMI_MATCH(DMI_BOARD_NAME, "AmberLake Y"),
36 		},
37 	},
38 	{}
39 };
40 
41 static const struct dmi_system_id kblr_dmi_table[] = {
42 	{
43 		.matches = {
44 			DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
45 			DMI_MATCH(DMI_BOARD_NAME, "Kabylake R DDR4 RVP"),
46 		},
47 	},
48 	{}
49 };
50 
51 static struct snd_soc_acpi_mach *dmi_match_quirk(void *arg)
52 {
53 	struct snd_soc_acpi_mach *mach = arg;
54 	const struct dmi_system_id *dmi_id;
55 	struct dmi_system_id *dmi_table;
56 
57 	if (mach->quirk_data == NULL)
58 		return mach;
59 
60 	dmi_table = (struct dmi_system_id *)mach->quirk_data;
61 
62 	dmi_id = dmi_first_match(dmi_table);
63 	if (!dmi_id)
64 		return NULL;
65 
66 	return mach;
67 }
68 
69 #define AVS_SSP(x)		(BIT(x))
70 #define AVS_SSP_RANGE(a, b)	(GENMASK(b, a))
71 
72 /* supported I2S board codec configurations */
73 static struct snd_soc_acpi_mach avs_skl_i2s_machines[] = {
74 	{
75 		.id = "INT343A",
76 		.drv_name = "avs_rt286",
77 		.mach_params = {
78 			.i2s_link_mask = AVS_SSP(0),
79 		},
80 		.tplg_filename = "rt286-tplg.bin",
81 	},
82 	{
83 		.id = "10508825",
84 		.drv_name = "avs_nau8825",
85 		.mach_params = {
86 			.i2s_link_mask = AVS_SSP(1),
87 		},
88 		.tplg_filename = "nau8825-tplg.bin",
89 	},
90 	{
91 		.id = "INT343B",
92 		.drv_name = "avs_ssm4567",
93 		.mach_params = {
94 			.i2s_link_mask = AVS_SSP(0),
95 		},
96 		.tplg_filename = "ssm4567-tplg.bin",
97 	},
98 	{
99 		.id = "MX98357A",
100 		.drv_name = "avs_max98357a",
101 		.mach_params = {
102 			.i2s_link_mask = AVS_SSP(0),
103 		},
104 		.tplg_filename = "max98357a-tplg.bin",
105 	},
106 	{},
107 };
108 
109 static struct snd_soc_acpi_mach avs_kbl_i2s_machines[] = {
110 	{
111 		.id = "INT343A",
112 		.drv_name = "avs_rt286",
113 		.mach_params = {
114 			.i2s_link_mask = AVS_SSP(0),
115 		},
116 		.quirk_data = &kbl_dmi_table,
117 		.machine_quirk = dmi_match_quirk,
118 		.tplg_filename = "rt286-tplg.bin",
119 	},
120 	{
121 		.id = "INT343A",
122 		.drv_name = "avs_rt298",
123 		.mach_params = {
124 			.i2s_link_mask = AVS_SSP(0),
125 		},
126 		.quirk_data = &kblr_dmi_table,
127 		.machine_quirk = dmi_match_quirk,
128 		.tplg_filename = "rt298-tplg.bin",
129 	},
130 	{
131 		.id = "MX98927",
132 		.drv_name = "avs_max98927",
133 		.mach_params = {
134 			.i2s_link_mask = AVS_SSP(0),
135 		},
136 		.tplg_filename = "max98927-tplg.bin",
137 	},
138 	{
139 		.id = "10EC5663",
140 		.drv_name = "avs_rt5663",
141 		.mach_params = {
142 			.i2s_link_mask = AVS_SSP(1),
143 		},
144 		.tplg_filename = "rt5663-tplg.bin",
145 	},
146 	{
147 		.id = "MX98373",
148 		.drv_name = "avs_max98373",
149 		.mach_params = {
150 			.i2s_link_mask = AVS_SSP(0),
151 		},
152 		.tplg_filename = "max98373-tplg.bin",
153 	},
154 	{
155 		.id = "MX98357A",
156 		.drv_name = "avs_max98357a",
157 		.mach_params = {
158 			.i2s_link_mask = AVS_SSP(0),
159 		},
160 		.tplg_filename = "max98357a-tplg.bin",
161 	},
162 	{
163 		.id = "DLGS7219",
164 		.drv_name = "avs_da7219",
165 		.mach_params = {
166 			.i2s_link_mask = AVS_SSP(1),
167 		},
168 		.tplg_filename = "da7219-tplg.bin",
169 	},
170 	{
171 		.id = "ESSX8336",
172 		.drv_name = "avs_es8336",
173 		.mach_params = {
174 			.i2s_link_mask = AVS_SSP(0),
175 		},
176 		.tplg_filename = "es8336-tplg.bin",
177 	},
178 	{},
179 };
180 
181 static struct snd_soc_acpi_mach avs_apl_i2s_machines[] = {
182 	{
183 		.id = "INT343A",
184 		.drv_name = "avs_rt298",
185 		.mach_params = {
186 			.i2s_link_mask = AVS_SSP(5),
187 		},
188 		.tplg_filename = "rt298-tplg.bin",
189 	},
190 	{
191 		.id = "INT34C3",
192 		.drv_name = "avs_tdf8532",
193 		.mach_params = {
194 			.i2s_link_mask = AVS_SSP_RANGE(0, 5),
195 		},
196 		.pdata = (unsigned long[]){ 0, 0, 0x14, 0, 0, 0 }, /* SSP2 TDMs */
197 		.tplg_filename = "tdf8532-tplg.bin",
198 	},
199 	{
200 		.id = "MX98357A",
201 		.drv_name = "avs_max98357a",
202 		.mach_params = {
203 			.i2s_link_mask = AVS_SSP(5),
204 		},
205 		.tplg_filename = "max98357a-tplg.bin",
206 	},
207 	{
208 		.id = "DLGS7219",
209 		.drv_name = "avs_da7219",
210 		.mach_params = {
211 			.i2s_link_mask = AVS_SSP(1),
212 		},
213 		.tplg_filename = "da7219-tplg.bin",
214 	},
215 	{},
216 };
217 
218 static struct snd_soc_acpi_mach avs_gml_i2s_machines[] = {
219 	{
220 		.id = "INT343A",
221 		.drv_name = "avs_rt298",
222 		.mach_params = {
223 			.i2s_link_mask = AVS_SSP(2),
224 		},
225 		.tplg_filename = "rt298-tplg.bin",
226 	},
227 	{},
228 };
229 
230 static struct snd_soc_acpi_mach avs_cnl_i2s_machines[] = {
231 	{
232 		.id = "INT34C2",
233 		.drv_name = "avs_rt274",
234 		.mach_params = {
235 			.i2s_link_mask = AVS_SSP(0),
236 		},
237 		.tplg_filename = "rt274-tplg.bin",
238 	},
239 	{
240 		.id = "10EC5682",
241 		.drv_name = "avs_rt5682",
242 		.mach_params = {
243 			.i2s_link_mask = AVS_SSP(1),
244 		},
245 		.tplg_filename = "rt5682-tplg.bin",
246 	},
247 	{},
248 };
249 
250 static struct snd_soc_acpi_mach avs_icl_i2s_machines[] = {
251 	{
252 		.id = "INT343A",
253 		.drv_name = "avs_rt298",
254 		.mach_params = {
255 			.i2s_link_mask = AVS_SSP(0),
256 		},
257 		.tplg_filename = "rt298-tplg.bin",
258 	},
259 	{
260 		.id = "INT34C2",
261 		.drv_name = "avs_rt274",
262 		.mach_params = {
263 			.i2s_link_mask = AVS_SSP(0),
264 		},
265 		.tplg_filename = "rt274-tplg.bin",
266 	},
267 	{},
268 };
269 
270 static struct snd_soc_acpi_mach avs_tgl_i2s_machines[] = {
271 	{
272 		.id = "INT34C2",
273 		.drv_name = "avs_rt274",
274 		.mach_params = {
275 			.i2s_link_mask = AVS_SSP(0),
276 		},
277 		.tplg_filename = "rt274-tplg.bin",
278 	},
279 	{
280 		.id = "10EC0298",
281 		.drv_name = "avs_rt298",
282 		.mach_params = {
283 			.i2s_link_mask = AVS_SSP(0),
284 		},
285 		.tplg_filename = "rt298-tplg.bin",
286 	},
287 	{
288 		.id = "10EC1308",
289 		.drv_name = "avs_rt1308",
290 		.mach_params = {
291 			.i2s_link_mask = AVS_SSP(1),
292 		},
293 		.tplg_filename = "rt1308-tplg.bin",
294 	},
295 	{
296 		.id = "ESSX8336",
297 		.drv_name = "avs_es8336",
298 		.mach_params = {
299 			.i2s_link_mask = AVS_SSP(0),
300 		},
301 		.tplg_filename = "es8336-tplg.bin",
302 	},
303 	{},
304 };
305 
306 static struct snd_soc_acpi_mach avs_test_i2s_machines[] = {
307 	{
308 		.drv_name = "avs_i2s_test",
309 		.mach_params = {
310 			.i2s_link_mask = AVS_SSP(0),
311 		},
312 		.tplg_filename = "i2s-test-tplg.bin",
313 	},
314 	{
315 		.drv_name = "avs_i2s_test",
316 		.mach_params = {
317 			.i2s_link_mask = AVS_SSP(1),
318 		},
319 		.tplg_filename = "i2s-test-tplg.bin",
320 	},
321 	{
322 		.drv_name = "avs_i2s_test",
323 		.mach_params = {
324 			.i2s_link_mask = AVS_SSP(2),
325 		},
326 		.tplg_filename = "i2s-test-tplg.bin",
327 	},
328 	{
329 		.drv_name = "avs_i2s_test",
330 		.mach_params = {
331 			.i2s_link_mask = AVS_SSP(3),
332 		},
333 		.tplg_filename = "i2s-test-tplg.bin",
334 	},
335 	{
336 		.drv_name = "avs_i2s_test",
337 		.mach_params = {
338 			.i2s_link_mask = AVS_SSP(4),
339 		},
340 		.tplg_filename = "i2s-test-tplg.bin",
341 	},
342 	{
343 		.drv_name = "avs_i2s_test",
344 		.mach_params = {
345 			.i2s_link_mask = AVS_SSP(5),
346 		},
347 		.tplg_filename = "i2s-test-tplg.bin",
348 	},
349 	/* no NULL terminator, as we depend on ARRAY SIZE due to .id == NULL */
350 };
351 
352 struct avs_acpi_boards {
353 	int id;
354 	struct snd_soc_acpi_mach *machs;
355 };
356 
357 #define AVS_MACH_ENTRY(_id, _mach) \
358 	{ .id = PCI_DEVICE_ID_INTEL_##_id, .machs = (_mach), }
359 
360 /* supported I2S boards per platform */
361 static const struct avs_acpi_boards i2s_boards[] = {
362 	AVS_MACH_ENTRY(HDA_SKL_LP, avs_skl_i2s_machines),
363 	AVS_MACH_ENTRY(HDA_KBL_LP, avs_kbl_i2s_machines),
364 	AVS_MACH_ENTRY(HDA_APL, avs_apl_i2s_machines),
365 	AVS_MACH_ENTRY(HDA_GML, avs_gml_i2s_machines),
366 	AVS_MACH_ENTRY(HDA_CNL_LP,	avs_cnl_i2s_machines),
367 	AVS_MACH_ENTRY(HDA_CNL_H,	avs_cnl_i2s_machines),
368 	AVS_MACH_ENTRY(HDA_CML_LP,	avs_cnl_i2s_machines),
369 	AVS_MACH_ENTRY(HDA_ICL_LP,	avs_icl_i2s_machines),
370 	AVS_MACH_ENTRY(HDA_TGL_LP,	avs_tgl_i2s_machines),
371 	AVS_MACH_ENTRY(HDA_EHL_0,	avs_tgl_i2s_machines),
372 	AVS_MACH_ENTRY(HDA_ADL_P,	avs_tgl_i2s_machines),
373 	AVS_MACH_ENTRY(HDA_RPL_P_0,	avs_tgl_i2s_machines),
374 	AVS_MACH_ENTRY(HDA_RPL_M,	avs_tgl_i2s_machines),
375 	{},
376 };
377 
378 static const struct avs_acpi_boards *avs_get_i2s_boards(struct avs_dev *adev)
379 {
380 	int id, i;
381 
382 	id = adev->base.pci->device;
383 	for (i = 0; i < ARRAY_SIZE(i2s_boards); i++)
384 		if (i2s_boards[i].id == id)
385 			return &i2s_boards[i];
386 	return NULL;
387 }
388 
389 /* platform devices owned by AVS audio are removed with this hook */
390 static void board_pdev_unregister(void *data)
391 {
392 	platform_device_unregister(data);
393 }
394 
395 static int __maybe_unused avs_register_probe_board(struct avs_dev *adev)
396 {
397 	struct platform_device *board;
398 	struct snd_soc_acpi_mach mach = {{0}};
399 	int ret;
400 
401 	ret = avs_probe_platform_register(adev, "probe-platform");
402 	if (ret < 0)
403 		return ret;
404 
405 	mach.mach_params.platform = "probe-platform";
406 
407 	board = platform_device_register_data(NULL, "avs_probe_mb", PLATFORM_DEVID_NONE,
408 					      (const void *)&mach, sizeof(mach));
409 	if (IS_ERR(board)) {
410 		dev_err(adev->dev, "probe board register failed\n");
411 		return PTR_ERR(board);
412 	}
413 
414 	ret = devm_add_action(adev->dev, board_pdev_unregister, board);
415 	if (ret < 0) {
416 		platform_device_unregister(board);
417 		return ret;
418 	}
419 	return 0;
420 }
421 
422 static int avs_register_dmic_board(struct avs_dev *adev)
423 {
424 	struct platform_device *codec, *board;
425 	struct snd_soc_acpi_mach mach = {{0}};
426 	int ret;
427 
428 	if (!adev->nhlt ||
429 	    !intel_nhlt_has_endpoint_type(adev->nhlt, NHLT_LINK_DMIC)) {
430 		dev_dbg(adev->dev, "no DMIC endpoints present\n");
431 		return 0;
432 	}
433 
434 	codec = platform_device_register_simple("dmic-codec", PLATFORM_DEVID_NONE, NULL, 0);
435 	if (IS_ERR(codec)) {
436 		dev_err(adev->dev, "dmic codec register failed\n");
437 		return PTR_ERR(codec);
438 	}
439 
440 	ret = devm_add_action(adev->dev, board_pdev_unregister, codec);
441 	if (ret < 0) {
442 		platform_device_unregister(codec);
443 		return ret;
444 	}
445 
446 	ret = avs_dmic_platform_register(adev, "dmic-platform");
447 	if (ret < 0)
448 		return ret;
449 
450 	mach.tplg_filename = "dmic-tplg.bin";
451 	mach.mach_params.platform = "dmic-platform";
452 
453 	board = platform_device_register_data(NULL, "avs_dmic", PLATFORM_DEVID_NONE,
454 					(const void *)&mach, sizeof(mach));
455 	if (IS_ERR(board)) {
456 		dev_err(adev->dev, "dmic board register failed\n");
457 		return PTR_ERR(board);
458 	}
459 
460 	ret = devm_add_action(adev->dev, board_pdev_unregister, board);
461 	if (ret < 0) {
462 		platform_device_unregister(board);
463 		return ret;
464 	}
465 
466 	return 0;
467 }
468 
469 static int avs_register_i2s_board(struct avs_dev *adev, struct snd_soc_acpi_mach *mach)
470 {
471 	struct platform_device *board;
472 	int num_ssps;
473 	char *name;
474 	int ret;
475 
476 	num_ssps = adev->hw_cfg.i2s_caps.ctrl_count;
477 	if (fls(mach->mach_params.i2s_link_mask) > num_ssps) {
478 		dev_err(adev->dev, "Platform supports %d SSPs but board %s requires SSP%ld\n",
479 			num_ssps, mach->drv_name,
480 			(unsigned long)__fls(mach->mach_params.i2s_link_mask));
481 		return -ENODEV;
482 	}
483 
484 	name = devm_kasprintf(adev->dev, GFP_KERNEL, "%s.%d-platform", mach->drv_name,
485 			      mach->mach_params.i2s_link_mask);
486 	if (!name)
487 		return -ENOMEM;
488 
489 	ret = avs_i2s_platform_register(adev, name, mach->mach_params.i2s_link_mask, mach->pdata);
490 	if (ret < 0)
491 		return ret;
492 
493 	mach->mach_params.platform = name;
494 
495 	board = platform_device_register_data(NULL, mach->drv_name, mach->mach_params.i2s_link_mask,
496 					      (const void *)mach, sizeof(*mach));
497 	if (IS_ERR(board)) {
498 		dev_err(adev->dev, "ssp board register failed\n");
499 		return PTR_ERR(board);
500 	}
501 
502 	ret = devm_add_action(adev->dev, board_pdev_unregister, board);
503 	if (ret < 0) {
504 		platform_device_unregister(board);
505 		return ret;
506 	}
507 
508 	return 0;
509 }
510 
511 static int avs_register_i2s_boards(struct avs_dev *adev)
512 {
513 	const struct avs_acpi_boards *boards;
514 	struct snd_soc_acpi_mach *mach;
515 	int ret;
516 
517 	if (!adev->nhlt || !intel_nhlt_has_endpoint_type(adev->nhlt, NHLT_LINK_SSP)) {
518 		dev_dbg(adev->dev, "no I2S endpoints present\n");
519 		return 0;
520 	}
521 
522 	if (i2s_test) {
523 		int i, num_ssps;
524 
525 		num_ssps = adev->hw_cfg.i2s_caps.ctrl_count;
526 		/* constrain just in case FW says there can be more SSPs than possible */
527 		num_ssps = min_t(int, ARRAY_SIZE(avs_test_i2s_machines), num_ssps);
528 
529 		mach = avs_test_i2s_machines;
530 
531 		for (i = 0; i < num_ssps; i++) {
532 			ret = avs_register_i2s_board(adev, &mach[i]);
533 			if (ret < 0)
534 				dev_warn(adev->dev, "register i2s %s failed: %d\n", mach->drv_name,
535 					 ret);
536 		}
537 		return 0;
538 	}
539 
540 	boards = avs_get_i2s_boards(adev);
541 	if (!boards) {
542 		dev_dbg(adev->dev, "no I2S endpoints supported\n");
543 		return 0;
544 	}
545 
546 	for (mach = boards->machs; mach->id[0]; mach++) {
547 		if (!acpi_dev_present(mach->id, mach->uid, -1))
548 			continue;
549 
550 		if (mach->machine_quirk)
551 			if (!mach->machine_quirk(mach))
552 				continue;
553 
554 		ret = avs_register_i2s_board(adev, mach);
555 		if (ret < 0)
556 			dev_warn(adev->dev, "register i2s %s failed: %d\n", mach->drv_name, ret);
557 	}
558 
559 	return 0;
560 }
561 
562 static int avs_register_hda_board(struct avs_dev *adev, struct hda_codec *codec)
563 {
564 	struct snd_soc_acpi_mach mach = {{0}};
565 	struct platform_device *board;
566 	struct hdac_device *hdev = &codec->core;
567 	char *pname;
568 	int ret, id;
569 
570 	pname = devm_kasprintf(adev->dev, GFP_KERNEL, "%s-platform", dev_name(&hdev->dev));
571 	if (!pname)
572 		return -ENOMEM;
573 
574 	ret = avs_hda_platform_register(adev, pname);
575 	if (ret < 0)
576 		return ret;
577 
578 	mach.pdata = codec;
579 	mach.mach_params.platform = pname;
580 	mach.tplg_filename = devm_kasprintf(adev->dev, GFP_KERNEL, "hda-%08x-tplg.bin",
581 					    hdev->vendor_id);
582 	if (!mach.tplg_filename)
583 		return -ENOMEM;
584 
585 	id = adev->base.core.idx * HDA_MAX_CODECS + hdev->addr;
586 	board = platform_device_register_data(NULL, "avs_hdaudio", id, (const void *)&mach,
587 					      sizeof(mach));
588 	if (IS_ERR(board)) {
589 		dev_err(adev->dev, "hda board register failed\n");
590 		return PTR_ERR(board);
591 	}
592 
593 	ret = devm_add_action(adev->dev, board_pdev_unregister, board);
594 	if (ret < 0) {
595 		platform_device_unregister(board);
596 		return ret;
597 	}
598 
599 	return 0;
600 }
601 
602 static int avs_register_hda_boards(struct avs_dev *adev)
603 {
604 	struct hdac_bus *bus = &adev->base.core;
605 	struct hdac_device *hdev;
606 	int ret;
607 
608 	if (!bus->num_codecs) {
609 		dev_dbg(adev->dev, "no HDA endpoints present\n");
610 		return 0;
611 	}
612 
613 	list_for_each_entry(hdev, &bus->codec_list, list) {
614 		struct hda_codec *codec;
615 
616 		codec = dev_to_hda_codec(&hdev->dev);
617 
618 		ret = avs_register_hda_board(adev, codec);
619 		if (ret < 0)
620 			dev_warn(adev->dev, "register hda-%08x failed: %d\n",
621 				 codec->core.vendor_id, ret);
622 	}
623 
624 	return 0;
625 }
626 
627 int avs_register_all_boards(struct avs_dev *adev)
628 {
629 	int ret;
630 
631 #ifdef CONFIG_DEBUG_FS
632 	ret = avs_register_probe_board(adev);
633 	if (ret < 0)
634 		dev_warn(adev->dev, "enumerate PROBE endpoints failed: %d\n", ret);
635 #endif
636 
637 	ret = avs_register_dmic_board(adev);
638 	if (ret < 0)
639 		dev_warn(adev->dev, "enumerate DMIC endpoints failed: %d\n",
640 			 ret);
641 
642 	ret = avs_register_i2s_boards(adev);
643 	if (ret < 0)
644 		dev_warn(adev->dev, "enumerate I2S endpoints failed: %d\n",
645 			 ret);
646 
647 	ret = avs_register_hda_boards(adev);
648 	if (ret < 0)
649 		dev_warn(adev->dev, "enumerate HDA endpoints failed: %d\n",
650 			 ret);
651 
652 	return 0;
653 }
654 
655 void avs_unregister_all_boards(struct avs_dev *adev)
656 {
657 	snd_soc_unregister_component(adev->dev);
658 }
659