xref: /openbmc/linux/sound/soc/intel/boards/sof_sdw.c (revision acaeb8c6)
1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright (c) 2020 Intel Corporation
3 
4 /*
5  *  sof_sdw - ASOC Machine driver for Intel SoundWire platforms
6  */
7 
8 #include <linux/device.h>
9 #include <linux/dmi.h>
10 #include <linux/module.h>
11 #include <linux/soundwire/sdw.h>
12 #include <linux/soundwire/sdw_type.h>
13 #include <sound/soc.h>
14 #include <sound/soc-acpi.h>
15 #include "sof_sdw_common.h"
16 #include "../../codecs/rt711.h"
17 
18 unsigned long sof_sdw_quirk = RT711_JD1;
19 static int quirk_override = -1;
20 module_param_named(quirk, quirk_override, int, 0444);
21 MODULE_PARM_DESC(quirk, "Board-specific quirk override");
22 
23 #define INC_ID(BE, CPU, LINK)	do { (BE)++; (CPU)++; (LINK)++; } while (0)
24 
25 static void log_quirks(struct device *dev)
26 {
27 	if (SOF_RT711_JDSRC(sof_sdw_quirk))
28 		dev_dbg(dev, "quirk realtek,jack-detect-source %ld\n",
29 			SOF_RT711_JDSRC(sof_sdw_quirk));
30 	if (sof_sdw_quirk & SOF_SDW_FOUR_SPK)
31 		dev_dbg(dev, "quirk SOF_SDW_FOUR_SPK enabled\n");
32 	if (sof_sdw_quirk & SOF_SDW_TGL_HDMI)
33 		dev_dbg(dev, "quirk SOF_SDW_TGL_HDMI enabled\n");
34 	if (sof_sdw_quirk & SOF_SDW_PCH_DMIC)
35 		dev_dbg(dev, "quirk SOF_SDW_PCH_DMIC enabled\n");
36 	if (SOF_SSP_GET_PORT(sof_sdw_quirk))
37 		dev_dbg(dev, "SSP port %ld\n",
38 			SOF_SSP_GET_PORT(sof_sdw_quirk));
39 	if (sof_sdw_quirk & SOF_SDW_NO_AGGREGATION)
40 		dev_dbg(dev, "quirk SOF_SDW_NO_AGGREGATION enabled\n");
41 }
42 
43 static int sof_sdw_quirk_cb(const struct dmi_system_id *id)
44 {
45 	sof_sdw_quirk = (unsigned long)id->driver_data;
46 	return 1;
47 }
48 
49 static const struct dmi_system_id sof_sdw_quirk_table[] = {
50 	/* CometLake devices */
51 	{
52 		.callback = sof_sdw_quirk_cb,
53 		.matches = {
54 			DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
55 			DMI_MATCH(DMI_PRODUCT_NAME, "CometLake Client"),
56 		},
57 		.driver_data = (void *)SOF_SDW_PCH_DMIC,
58 	},
59 	{
60 		.callback = sof_sdw_quirk_cb,
61 		.matches = {
62 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
63 			DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "09C6")
64 		},
65 		.driver_data = (void *)RT711_JD2,
66 	},
67 	{
68 		/* early version of SKU 09C6 */
69 		.callback = sof_sdw_quirk_cb,
70 		.matches = {
71 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
72 			DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0983")
73 		},
74 		.driver_data = (void *)RT711_JD2,
75 	},
76 	{
77 		.callback = sof_sdw_quirk_cb,
78 		.matches = {
79 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
80 			DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "098F"),
81 		},
82 		.driver_data = (void *)(RT711_JD2 |
83 					SOF_SDW_FOUR_SPK),
84 	},
85 	{
86 		.callback = sof_sdw_quirk_cb,
87 		.matches = {
88 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
89 			DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0990"),
90 		},
91 		.driver_data = (void *)(RT711_JD2 |
92 					SOF_SDW_FOUR_SPK),
93 	},
94 	/* IceLake devices */
95 	{
96 		.callback = sof_sdw_quirk_cb,
97 		.matches = {
98 			DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
99 			DMI_MATCH(DMI_PRODUCT_NAME, "Ice Lake Client"),
100 		},
101 		.driver_data = (void *)SOF_SDW_PCH_DMIC,
102 	},
103 	/* TigerLake devices */
104 	{
105 		.callback = sof_sdw_quirk_cb,
106 		.matches = {
107 			DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
108 			DMI_MATCH(DMI_PRODUCT_NAME,
109 				  "Tiger Lake Client Platform"),
110 		},
111 		.driver_data = (void *)(SOF_SDW_TGL_HDMI |
112 					RT711_JD1 |
113 					SOF_SDW_PCH_DMIC |
114 					SOF_SSP_PORT(SOF_I2S_SSP2)),
115 	},
116 	{
117 		.callback = sof_sdw_quirk_cb,
118 		.matches = {
119 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
120 			DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0A3E")
121 		},
122 		.driver_data = (void *)(SOF_SDW_TGL_HDMI |
123 					RT711_JD2),
124 	},
125 	{
126 		/* another SKU of Dell Latitude 9520 */
127 		.callback = sof_sdw_quirk_cb,
128 		.matches = {
129 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
130 			DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0A3F")
131 		},
132 		.driver_data = (void *)(SOF_SDW_TGL_HDMI |
133 					RT711_JD2),
134 	},
135 	{
136 		/* Dell XPS 9710 */
137 		.callback = sof_sdw_quirk_cb,
138 		.matches = {
139 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
140 			DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0A5D")
141 		},
142 		.driver_data = (void *)(SOF_SDW_TGL_HDMI |
143 					RT711_JD2 |
144 					SOF_SDW_FOUR_SPK),
145 	},
146 	{
147 		.callback = sof_sdw_quirk_cb,
148 		.matches = {
149 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
150 			DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0A5E")
151 		},
152 		.driver_data = (void *)(SOF_SDW_TGL_HDMI |
153 					RT711_JD2 |
154 					SOF_SDW_FOUR_SPK),
155 	},
156 	{
157 		.callback = sof_sdw_quirk_cb,
158 		.matches = {
159 			DMI_MATCH(DMI_SYS_VENDOR, "Google"),
160 			DMI_MATCH(DMI_PRODUCT_NAME, "Volteer"),
161 		},
162 		.driver_data = (void *)(SOF_SDW_TGL_HDMI |
163 					SOF_SDW_PCH_DMIC |
164 					SOF_SDW_FOUR_SPK |
165 					SOF_BT_OFFLOAD_SSP(2) |
166 					SOF_SSP_BT_OFFLOAD_PRESENT),
167 	},
168 	{
169 		.callback = sof_sdw_quirk_cb,
170 		.matches = {
171 			DMI_MATCH(DMI_SYS_VENDOR, "Google"),
172 			DMI_MATCH(DMI_PRODUCT_NAME, "Ripto"),
173 		},
174 		.driver_data = (void *)(SOF_SDW_TGL_HDMI |
175 					SOF_SDW_PCH_DMIC |
176 					SOF_SDW_FOUR_SPK),
177 	},
178 	{
179 		/*
180 		 * this entry covers multiple HP SKUs. The family name
181 		 * does not seem robust enough, so we use a partial
182 		 * match that ignores the product name suffix
183 		 * (e.g. 15-eb1xxx, 14t-ea000 or 13-aw2xxx)
184 		 */
185 		.callback = sof_sdw_quirk_cb,
186 		.matches = {
187 			DMI_MATCH(DMI_SYS_VENDOR, "HP"),
188 			DMI_MATCH(DMI_PRODUCT_NAME, "HP Spectre x360 Conv"),
189 		},
190 		.driver_data = (void *)(SOF_SDW_TGL_HDMI |
191 					SOF_SDW_PCH_DMIC |
192 					RT711_JD1),
193 	},
194 	{
195 		/* NUC15 'Bishop County' LAPBC510 and LAPBC710 skews */
196 		.callback = sof_sdw_quirk_cb,
197 		.matches = {
198 			DMI_MATCH(DMI_SYS_VENDOR, "Intel(R) Client Systems"),
199 			DMI_MATCH(DMI_PRODUCT_NAME, "LAPBC"),
200 		},
201 		.driver_data = (void *)(SOF_SDW_TGL_HDMI |
202 					SOF_SDW_PCH_DMIC |
203 					RT711_JD1),
204 	},
205 	/* TigerLake-SDCA devices */
206 	{
207 		.callback = sof_sdw_quirk_cb,
208 		.matches = {
209 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
210 			DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0A32")
211 		},
212 		.driver_data = (void *)(SOF_SDW_TGL_HDMI |
213 					RT711_JD2 |
214 					SOF_SDW_FOUR_SPK),
215 	},
216 	{
217 		.callback = sof_sdw_quirk_cb,
218 		.matches = {
219 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
220 			DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0A45")
221 		},
222 		.driver_data = (void *)(SOF_SDW_TGL_HDMI |
223 					RT711_JD2),
224 	},
225 	/* AlderLake devices */
226 	{
227 		.callback = sof_sdw_quirk_cb,
228 		.matches = {
229 			DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
230 			DMI_MATCH(DMI_PRODUCT_NAME, "Alder Lake Client Platform"),
231 		},
232 		.driver_data = (void *)(RT711_JD2_100K |
233 					SOF_SDW_TGL_HDMI |
234 					SOF_BT_OFFLOAD_SSP(2) |
235 					SOF_SSP_BT_OFFLOAD_PRESENT),
236 	},
237 	{
238 		.callback = sof_sdw_quirk_cb,
239 		.matches = {
240 			DMI_MATCH(DMI_SYS_VENDOR, "Google"),
241 			DMI_MATCH(DMI_PRODUCT_NAME, "Brya"),
242 		},
243 		.driver_data = (void *)(SOF_SDW_TGL_HDMI |
244 					SOF_SDW_PCH_DMIC |
245 					SOF_SDW_FOUR_SPK |
246 					SOF_BT_OFFLOAD_SSP(2) |
247 					SOF_SSP_BT_OFFLOAD_PRESENT),
248 	},
249 	{
250 		.callback = sof_sdw_quirk_cb,
251 		.matches = {
252 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
253 			DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0AF3"),
254 		},
255 		/* No Jack */
256 		.driver_data = (void *)(SOF_SDW_TGL_HDMI |
257 					SOF_SDW_FOUR_SPK),
258 	},
259 	{
260 		.callback = sof_sdw_quirk_cb,
261 		.matches = {
262 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
263 			DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0B00")
264 		},
265 		.driver_data = (void *)(SOF_SDW_TGL_HDMI |
266 					RT711_JD2 |
267 					SOF_SDW_FOUR_SPK),
268 	},
269 	{
270 		.callback = sof_sdw_quirk_cb,
271 		.matches = {
272 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
273 			DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0B01")
274 		},
275 		.driver_data = (void *)(SOF_SDW_TGL_HDMI |
276 					RT711_JD2 |
277 					SOF_SDW_FOUR_SPK),
278 	},
279 	{
280 		.callback = sof_sdw_quirk_cb,
281 		.matches = {
282 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
283 			DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0B11")
284 		},
285 		.driver_data = (void *)(SOF_SDW_TGL_HDMI |
286 					RT711_JD2 |
287 					SOF_SDW_FOUR_SPK),
288 	},
289 	{
290 		.callback = sof_sdw_quirk_cb,
291 		.matches = {
292 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
293 			DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0B12")
294 		},
295 		.driver_data = (void *)(SOF_SDW_TGL_HDMI |
296 					RT711_JD2 |
297 					SOF_SDW_FOUR_SPK),
298 	},
299 	{
300 		.callback = sof_sdw_quirk_cb,
301 		.matches = {
302 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
303 			DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0B13"),
304 		},
305 		/* No Jack */
306 		.driver_data = (void *)SOF_SDW_TGL_HDMI,
307 	},
308 	{
309 		.callback = sof_sdw_quirk_cb,
310 		.matches = {
311 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
312 			DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0B29"),
313 		},
314 		.driver_data = (void *)(SOF_SDW_TGL_HDMI |
315 					RT711_JD2 |
316 					SOF_SDW_FOUR_SPK),
317 	},
318 	/* MeteorLake devices */
319 	{
320 		.callback = sof_sdw_quirk_cb,
321 		.matches = {
322 			DMI_MATCH(DMI_PRODUCT_FAMILY, "Intel_mtlrvp"),
323 		},
324 		.driver_data = (void *)(RT711_JD1 | SOF_SDW_TGL_HDMI),
325 	},
326 	{}
327 };
328 
329 static struct snd_soc_dai_link_component dmic_component[] = {
330 	{
331 		.name = "dmic-codec",
332 		.dai_name = "dmic-hifi",
333 	}
334 };
335 
336 static struct snd_soc_dai_link_component platform_component[] = {
337 	{
338 		/* name might be overridden during probe */
339 		.name = "0000:00:1f.3"
340 	}
341 };
342 
343 /* these wrappers are only needed to avoid typecast compilation errors */
344 int sdw_startup(struct snd_pcm_substream *substream)
345 {
346 	return sdw_startup_stream(substream);
347 }
348 
349 int sdw_prepare(struct snd_pcm_substream *substream)
350 {
351 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
352 	struct sdw_stream_runtime *sdw_stream;
353 	struct snd_soc_dai *dai;
354 
355 	/* Find stream from first CPU DAI */
356 	dai = asoc_rtd_to_cpu(rtd, 0);
357 
358 	sdw_stream = snd_soc_dai_get_stream(dai, substream->stream);
359 
360 	if (IS_ERR(sdw_stream)) {
361 		dev_err(rtd->dev, "no stream found for DAI %s", dai->name);
362 		return PTR_ERR(sdw_stream);
363 	}
364 
365 	return sdw_prepare_stream(sdw_stream);
366 }
367 
368 int sdw_trigger(struct snd_pcm_substream *substream, int cmd)
369 {
370 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
371 	struct sdw_stream_runtime *sdw_stream;
372 	struct snd_soc_dai *dai;
373 	int ret;
374 
375 	/* Find stream from first CPU DAI */
376 	dai = asoc_rtd_to_cpu(rtd, 0);
377 
378 	sdw_stream = snd_soc_dai_get_stream(dai, substream->stream);
379 
380 	if (IS_ERR(sdw_stream)) {
381 		dev_err(rtd->dev, "no stream found for DAI %s", dai->name);
382 		return PTR_ERR(sdw_stream);
383 	}
384 
385 	switch (cmd) {
386 	case SNDRV_PCM_TRIGGER_START:
387 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
388 	case SNDRV_PCM_TRIGGER_RESUME:
389 		ret = sdw_enable_stream(sdw_stream);
390 		break;
391 
392 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
393 	case SNDRV_PCM_TRIGGER_SUSPEND:
394 	case SNDRV_PCM_TRIGGER_STOP:
395 		ret = sdw_disable_stream(sdw_stream);
396 		break;
397 	default:
398 		ret = -EINVAL;
399 		break;
400 	}
401 
402 	if (ret)
403 		dev_err(rtd->dev, "%s trigger %d failed: %d", __func__, cmd, ret);
404 
405 	return ret;
406 }
407 
408 int sdw_hw_free(struct snd_pcm_substream *substream)
409 {
410 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
411 	struct sdw_stream_runtime *sdw_stream;
412 	struct snd_soc_dai *dai;
413 
414 	/* Find stream from first CPU DAI */
415 	dai = asoc_rtd_to_cpu(rtd, 0);
416 
417 	sdw_stream = snd_soc_dai_get_stream(dai, substream->stream);
418 
419 	if (IS_ERR(sdw_stream)) {
420 		dev_err(rtd->dev, "no stream found for DAI %s", dai->name);
421 		return PTR_ERR(sdw_stream);
422 	}
423 
424 	return sdw_deprepare_stream(sdw_stream);
425 }
426 
427 void sdw_shutdown(struct snd_pcm_substream *substream)
428 {
429 	sdw_shutdown_stream(substream);
430 }
431 
432 static const struct snd_soc_ops sdw_ops = {
433 	.startup = sdw_startup,
434 	.prepare = sdw_prepare,
435 	.trigger = sdw_trigger,
436 	.hw_free = sdw_hw_free,
437 	.shutdown = sdw_shutdown,
438 };
439 
440 static struct sof_sdw_codec_info codec_info_list[] = {
441 	{
442 		.part_id = 0x700,
443 		.direction = {true, true},
444 		.dai_name = "rt700-aif1",
445 		.init = sof_sdw_rt700_init,
446 		.codec_type = SOF_SDW_CODEC_TYPE_JACK,
447 	},
448 	{
449 		.part_id = 0x711,
450 		.version_id = 3,
451 		.direction = {true, true},
452 		.dai_name = "rt711-sdca-aif1",
453 		.init = sof_sdw_rt711_sdca_init,
454 		.exit = sof_sdw_rt711_sdca_exit,
455 		.codec_type = SOF_SDW_CODEC_TYPE_JACK,
456 	},
457 	{
458 		.part_id = 0x711,
459 		.version_id = 2,
460 		.direction = {true, true},
461 		.dai_name = "rt711-aif1",
462 		.init = sof_sdw_rt711_init,
463 		.exit = sof_sdw_rt711_exit,
464 		.codec_type = SOF_SDW_CODEC_TYPE_JACK,
465 	},
466 	{
467 		.part_id = 0x1308,
468 		.acpi_id = "10EC1308",
469 		.direction = {true, false},
470 		.dai_name = "rt1308-aif",
471 		.ops = &sof_sdw_rt1308_i2s_ops,
472 		.init = sof_sdw_rt1308_init,
473 		.codec_type = SOF_SDW_CODEC_TYPE_AMP,
474 	},
475 	{
476 		.part_id = 0x1316,
477 		.direction = {true, true},
478 		.dai_name = "rt1316-aif",
479 		.init = sof_sdw_rt1316_init,
480 		.codec_type = SOF_SDW_CODEC_TYPE_AMP,
481 	},
482 	{
483 		.part_id = 0x714,
484 		.version_id = 3,
485 		.direction = {false, true},
486 		.ignore_pch_dmic = true,
487 		.dai_name = "rt715-aif2",
488 		.init = sof_sdw_rt715_sdca_init,
489 		.codec_type = SOF_SDW_CODEC_TYPE_MIC,
490 	},
491 	{
492 		.part_id = 0x715,
493 		.version_id = 3,
494 		.direction = {false, true},
495 		.ignore_pch_dmic = true,
496 		.dai_name = "rt715-aif2",
497 		.init = sof_sdw_rt715_sdca_init,
498 		.codec_type = SOF_SDW_CODEC_TYPE_MIC,
499 	},
500 	{
501 		.part_id = 0x714,
502 		.version_id = 2,
503 		.direction = {false, true},
504 		.ignore_pch_dmic = true,
505 		.dai_name = "rt715-aif2",
506 		.init = sof_sdw_rt715_init,
507 		.codec_type = SOF_SDW_CODEC_TYPE_MIC,
508 	},
509 	{
510 		.part_id = 0x715,
511 		.version_id = 2,
512 		.direction = {false, true},
513 		.ignore_pch_dmic = true,
514 		.dai_name = "rt715-aif2",
515 		.init = sof_sdw_rt715_init,
516 		.codec_type = SOF_SDW_CODEC_TYPE_MIC,
517 	},
518 	{
519 		.part_id = 0x8373,
520 		.direction = {true, true},
521 		.dai_name = "max98373-aif1",
522 		.init = sof_sdw_mx8373_init,
523 		.codec_card_late_probe = sof_sdw_mx8373_late_probe,
524 		.codec_type = SOF_SDW_CODEC_TYPE_AMP,
525 	},
526 	{
527 		.part_id = 0x5682,
528 		.direction = {true, true},
529 		.dai_name = "rt5682-sdw",
530 		.init = sof_sdw_rt5682_init,
531 		.codec_type = SOF_SDW_CODEC_TYPE_JACK,
532 	},
533 	{
534 		.part_id = 0xaaaa, /* generic codec mockup */
535 		.version_id = 0,
536 		.direction = {true, true},
537 		.dai_name = "sdw-mockup-aif1",
538 		.init = NULL,
539 		.codec_type = SOF_SDW_CODEC_TYPE_JACK,
540 	},
541 	{
542 		.part_id = 0xaa55, /* headset codec mockup */
543 		.version_id = 0,
544 		.direction = {true, true},
545 		.dai_name = "sdw-mockup-aif1",
546 		.init = NULL,
547 		.codec_type = SOF_SDW_CODEC_TYPE_JACK,
548 	},
549 	{
550 		.part_id = 0x55aa, /* amplifier mockup */
551 		.version_id = 0,
552 		.direction = {true, false},
553 		.dai_name = "sdw-mockup-aif1",
554 		.init = NULL,
555 		.codec_type = SOF_SDW_CODEC_TYPE_AMP,
556 	},
557 	{
558 		.part_id = 0x5555,
559 		.version_id = 0,
560 		.direction = {false, true},
561 		.dai_name = "sdw-mockup-aif1",
562 		.codec_type = SOF_SDW_CODEC_TYPE_MIC,
563 	},
564 };
565 
566 static inline int find_codec_info_part(u64 adr)
567 {
568 	unsigned int part_id, sdw_version;
569 	int i;
570 
571 	part_id = SDW_PART_ID(adr);
572 	sdw_version = SDW_VERSION(adr);
573 	for (i = 0; i < ARRAY_SIZE(codec_info_list); i++)
574 		/*
575 		 * A codec info is for all sdw version with the part id if
576 		 * version_id is not specified in the codec info.
577 		 */
578 		if (part_id == codec_info_list[i].part_id &&
579 		    (!codec_info_list[i].version_id ||
580 		     sdw_version == codec_info_list[i].version_id))
581 			return i;
582 
583 	return -EINVAL;
584 
585 }
586 
587 static inline int find_codec_info_acpi(const u8 *acpi_id)
588 {
589 	int i;
590 
591 	if (!acpi_id[0])
592 		return -EINVAL;
593 
594 	for (i = 0; i < ARRAY_SIZE(codec_info_list); i++)
595 		if (!memcmp(codec_info_list[i].acpi_id, acpi_id,
596 			    ACPI_ID_LEN))
597 			break;
598 
599 	if (i == ARRAY_SIZE(codec_info_list))
600 		return -EINVAL;
601 
602 	return i;
603 }
604 
605 /*
606  * get BE dailink number and CPU DAI number based on sdw link adr.
607  * Since some sdw slaves may be aggregated, the CPU DAI number
608  * may be larger than the number of BE dailinks.
609  */
610 static int get_sdw_dailink_info(struct device *dev, const struct snd_soc_acpi_link_adr *links,
611 				int *sdw_be_num, int *sdw_cpu_dai_num)
612 {
613 	const struct snd_soc_acpi_link_adr *link;
614 	int _codec_type = SOF_SDW_CODEC_TYPE_JACK;
615 	bool group_visited[SDW_MAX_GROUPS];
616 	bool no_aggregation;
617 	int i;
618 
619 	no_aggregation = sof_sdw_quirk & SOF_SDW_NO_AGGREGATION;
620 	*sdw_cpu_dai_num = 0;
621 	*sdw_be_num  = 0;
622 
623 	if (!links)
624 		return -EINVAL;
625 
626 	for (i = 0; i < SDW_MAX_GROUPS; i++)
627 		group_visited[i] = false;
628 
629 	for (link = links; link->num_adr; link++) {
630 		const struct snd_soc_acpi_endpoint *endpoint;
631 		int codec_index;
632 		int stream;
633 		u64 adr;
634 
635 		adr = link->adr_d->adr;
636 		codec_index = find_codec_info_part(adr);
637 		if (codec_index < 0)
638 			return codec_index;
639 
640 		if (codec_info_list[codec_index].codec_type < _codec_type)
641 			dev_warn(dev,
642 				 "Unexpected address table ordering. Expected order: jack -> amp -> mic\n");
643 
644 		_codec_type = codec_info_list[codec_index].codec_type;
645 
646 		endpoint = link->adr_d->endpoints;
647 
648 		/* count DAI number for playback and capture */
649 		for_each_pcm_streams(stream) {
650 			if (!codec_info_list[codec_index].direction[stream])
651 				continue;
652 
653 			(*sdw_cpu_dai_num)++;
654 
655 			/* count BE for each non-aggregated slave or group */
656 			if (!endpoint->aggregated || no_aggregation ||
657 			    !group_visited[endpoint->group_id])
658 				(*sdw_be_num)++;
659 		}
660 
661 		if (endpoint->aggregated)
662 			group_visited[endpoint->group_id] = true;
663 	}
664 
665 	return 0;
666 }
667 
668 static void init_dai_link(struct device *dev, struct snd_soc_dai_link *dai_links,
669 			  int be_id, char *name, int playback, int capture,
670 			  struct snd_soc_dai_link_component *cpus, int cpus_num,
671 			  struct snd_soc_dai_link_component *codecs, int codecs_num,
672 			  int (*init)(struct snd_soc_pcm_runtime *rtd),
673 			  const struct snd_soc_ops *ops)
674 {
675 	dev_dbg(dev, "create dai link %s, id %d\n", name, be_id);
676 	dai_links->id = be_id;
677 	dai_links->name = name;
678 	dai_links->platforms = platform_component;
679 	dai_links->num_platforms = ARRAY_SIZE(platform_component);
680 	dai_links->no_pcm = 1;
681 	dai_links->cpus = cpus;
682 	dai_links->num_cpus = cpus_num;
683 	dai_links->codecs = codecs;
684 	dai_links->num_codecs = codecs_num;
685 	dai_links->dpcm_playback = playback;
686 	dai_links->dpcm_capture = capture;
687 	dai_links->init = init;
688 	dai_links->ops = ops;
689 }
690 
691 static bool is_unique_device(const struct snd_soc_acpi_link_adr *link,
692 			     unsigned int sdw_version,
693 			     unsigned int mfg_id,
694 			     unsigned int part_id,
695 			     unsigned int class_id,
696 			     int index_in_link
697 			    )
698 {
699 	int i;
700 
701 	for (i = 0; i < link->num_adr; i++) {
702 		unsigned int sdw1_version, mfg1_id, part1_id, class1_id;
703 		u64 adr;
704 
705 		/* skip itself */
706 		if (i == index_in_link)
707 			continue;
708 
709 		adr = link->adr_d[i].adr;
710 
711 		sdw1_version = SDW_VERSION(adr);
712 		mfg1_id = SDW_MFG_ID(adr);
713 		part1_id = SDW_PART_ID(adr);
714 		class1_id = SDW_CLASS_ID(adr);
715 
716 		if (sdw_version == sdw1_version &&
717 		    mfg_id == mfg1_id &&
718 		    part_id == part1_id &&
719 		    class_id == class1_id)
720 			return false;
721 	}
722 
723 	return true;
724 }
725 
726 static int create_codec_dai_name(struct device *dev,
727 				 const struct snd_soc_acpi_link_adr *link,
728 				 struct snd_soc_dai_link_component *codec,
729 				 int offset,
730 				 struct snd_soc_codec_conf *codec_conf,
731 				 int codec_count,
732 				 int *codec_conf_index)
733 {
734 	int i;
735 
736 	/* sanity check */
737 	if (*codec_conf_index + link->num_adr > codec_count) {
738 		dev_err(dev, "codec_conf: out-of-bounds access requested\n");
739 		return -EINVAL;
740 	}
741 
742 	for (i = 0; i < link->num_adr; i++) {
743 		unsigned int sdw_version, unique_id, mfg_id;
744 		unsigned int link_id, part_id, class_id;
745 		int codec_index, comp_index;
746 		char *codec_str;
747 		u64 adr;
748 
749 		adr = link->adr_d[i].adr;
750 
751 		sdw_version = SDW_VERSION(adr);
752 		link_id = SDW_DISCO_LINK_ID(adr);
753 		unique_id = SDW_UNIQUE_ID(adr);
754 		mfg_id = SDW_MFG_ID(adr);
755 		part_id = SDW_PART_ID(adr);
756 		class_id = SDW_CLASS_ID(adr);
757 
758 		comp_index = i + offset;
759 		if (is_unique_device(link, sdw_version, mfg_id, part_id,
760 				     class_id, i)) {
761 			codec_str = "sdw:%01x:%04x:%04x:%02x";
762 			codec[comp_index].name =
763 				devm_kasprintf(dev, GFP_KERNEL, codec_str,
764 					       link_id, mfg_id, part_id,
765 					       class_id);
766 		} else {
767 			codec_str = "sdw:%01x:%04x:%04x:%02x:%01x";
768 			codec[comp_index].name =
769 				devm_kasprintf(dev, GFP_KERNEL, codec_str,
770 					       link_id, mfg_id, part_id,
771 					       class_id, unique_id);
772 		}
773 
774 		if (!codec[comp_index].name)
775 			return -ENOMEM;
776 
777 		codec_index = find_codec_info_part(adr);
778 		if (codec_index < 0)
779 			return codec_index;
780 
781 		codec[comp_index].dai_name =
782 			codec_info_list[codec_index].dai_name;
783 
784 		codec_conf[*codec_conf_index].dlc = codec[comp_index];
785 		codec_conf[*codec_conf_index].name_prefix = link->adr_d[i].name_prefix;
786 
787 		++*codec_conf_index;
788 	}
789 
790 	return 0;
791 }
792 
793 static int set_codec_init_func(struct snd_soc_card *card,
794 			       const struct snd_soc_acpi_link_adr *link,
795 			       struct snd_soc_dai_link *dai_links,
796 			       bool playback, int group_id)
797 {
798 	int i;
799 
800 	do {
801 		/*
802 		 * Initialize the codec. If codec is part of an aggregated
803 		 * group (group_id>0), initialize all codecs belonging to
804 		 * same group.
805 		 */
806 		for (i = 0; i < link->num_adr; i++) {
807 			int codec_index;
808 
809 			codec_index = find_codec_info_part(link->adr_d[i].adr);
810 
811 			if (codec_index < 0)
812 				return codec_index;
813 			/* The group_id is > 0 iff the codec is aggregated */
814 			if (link->adr_d[i].endpoints->group_id != group_id)
815 				continue;
816 			if (codec_info_list[codec_index].init)
817 				codec_info_list[codec_index].init(card,
818 						link,
819 						dai_links,
820 						&codec_info_list[codec_index],
821 						playback);
822 		}
823 		link++;
824 	} while (link->mask && group_id);
825 
826 	return 0;
827 }
828 
829 /*
830  * check endpoint status in slaves and gather link ID for all slaves in
831  * the same group to generate different CPU DAI. Now only support
832  * one sdw link with all slaves set with only single group id.
833  *
834  * one slave on one sdw link with aggregated = 0
835  * one sdw BE DAI <---> one-cpu DAI <---> one-codec DAI
836  *
837  * two or more slaves on one sdw link with aggregated = 0
838  * one sdw BE DAI  <---> one-cpu DAI <---> multi-codec DAIs
839  *
840  * multiple links with multiple slaves with aggregated = 1
841  * one sdw BE DAI  <---> 1 .. N CPU DAIs <----> 1 .. N codec DAIs
842  */
843 static int get_slave_info(const struct snd_soc_acpi_link_adr *adr_link,
844 			  struct device *dev, int *cpu_dai_id, int *cpu_dai_num,
845 			  int *codec_num, unsigned int *group_id,
846 			  bool *group_generated)
847 {
848 	const struct snd_soc_acpi_adr_device *adr_d;
849 	const struct snd_soc_acpi_link_adr *adr_next;
850 	bool no_aggregation;
851 	int index = 0;
852 
853 	no_aggregation = sof_sdw_quirk & SOF_SDW_NO_AGGREGATION;
854 	*codec_num = adr_link->num_adr;
855 	adr_d = adr_link->adr_d;
856 
857 	/* make sure the link mask has a single bit set */
858 	if (!is_power_of_2(adr_link->mask))
859 		return -EINVAL;
860 
861 	cpu_dai_id[index++] = ffs(adr_link->mask) - 1;
862 	if (!adr_d->endpoints->aggregated || no_aggregation) {
863 		*cpu_dai_num = 1;
864 		*group_id = 0;
865 		return 0;
866 	}
867 
868 	*group_id = adr_d->endpoints->group_id;
869 
870 	/* gather other link ID of slaves in the same group */
871 	for (adr_next = adr_link + 1; adr_next && adr_next->num_adr;
872 		adr_next++) {
873 		const struct snd_soc_acpi_endpoint *endpoint;
874 
875 		endpoint = adr_next->adr_d->endpoints;
876 		if (!endpoint->aggregated ||
877 		    endpoint->group_id != *group_id)
878 			continue;
879 
880 		/* make sure the link mask has a single bit set */
881 		if (!is_power_of_2(adr_next->mask))
882 			return -EINVAL;
883 
884 		if (index >= SDW_MAX_CPU_DAIS) {
885 			dev_err(dev, " cpu_dai_id array overflows");
886 			return -EINVAL;
887 		}
888 
889 		cpu_dai_id[index++] = ffs(adr_next->mask) - 1;
890 		*codec_num += adr_next->num_adr;
891 	}
892 
893 	/*
894 	 * indicate CPU DAIs for this group have been generated
895 	 * to avoid generating CPU DAIs for this group again.
896 	 */
897 	group_generated[*group_id] = true;
898 	*cpu_dai_num = index;
899 
900 	return 0;
901 }
902 
903 static int create_sdw_dailink(struct snd_soc_card *card,
904 			      struct device *dev, int *link_index,
905 			      struct snd_soc_dai_link *dai_links,
906 			      int sdw_be_num, int sdw_cpu_dai_num,
907 			      struct snd_soc_dai_link_component *cpus,
908 			      const struct snd_soc_acpi_link_adr *link,
909 			      int *cpu_id, bool *group_generated,
910 			      struct snd_soc_codec_conf *codec_conf,
911 			      int codec_count, int *link_id,
912 			      int *codec_conf_index,
913 			      bool *ignore_pch_dmic)
914 {
915 	const struct snd_soc_acpi_link_adr *link_next;
916 	struct snd_soc_dai_link_component *codecs;
917 	int cpu_dai_id[SDW_MAX_CPU_DAIS];
918 	int cpu_dai_num, cpu_dai_index;
919 	unsigned int group_id;
920 	int codec_idx = 0;
921 	int i = 0, j = 0;
922 	int codec_index;
923 	int codec_num;
924 	int stream;
925 	int ret;
926 	int k;
927 
928 	ret = get_slave_info(link, dev, cpu_dai_id, &cpu_dai_num, &codec_num,
929 			     &group_id, group_generated);
930 	if (ret)
931 		return ret;
932 
933 	codecs = devm_kcalloc(dev, codec_num, sizeof(*codecs), GFP_KERNEL);
934 	if (!codecs)
935 		return -ENOMEM;
936 
937 	/* generate codec name on different links in the same group */
938 	for (link_next = link; link_next && link_next->num_adr &&
939 	     i < cpu_dai_num; link_next++) {
940 		const struct snd_soc_acpi_endpoint *endpoints;
941 
942 		endpoints = link_next->adr_d->endpoints;
943 		if (group_id && (!endpoints->aggregated ||
944 				 endpoints->group_id != group_id))
945 			continue;
946 
947 		/* skip the link excluded by this processed group */
948 		if (cpu_dai_id[i] != ffs(link_next->mask) - 1)
949 			continue;
950 
951 		ret = create_codec_dai_name(dev, link_next, codecs, codec_idx,
952 					    codec_conf, codec_count, codec_conf_index);
953 		if (ret < 0)
954 			return ret;
955 
956 		/* check next link to create codec dai in the processed group */
957 		i++;
958 		codec_idx += link_next->num_adr;
959 	}
960 
961 	/* find codec info to create BE DAI */
962 	codec_index = find_codec_info_part(link->adr_d[0].adr);
963 	if (codec_index < 0)
964 		return codec_index;
965 
966 	if (codec_info_list[codec_index].ignore_pch_dmic)
967 		*ignore_pch_dmic = true;
968 
969 	/* Shift the first amplifier's *link_id to SDW_AMP_DAI_ID */
970 	if (codec_info_list[codec_index].codec_type == SOF_SDW_CODEC_TYPE_AMP &&
971 	    *link_id < SDW_AMP_DAI_ID)
972 		*link_id = SDW_AMP_DAI_ID;
973 
974 	/*
975 	 * DAI ID is fixed at SDW_DMIC_DAI_ID for MICs to
976 	 * keep sdw DMIC and HDMI setting static in UCM
977 	 */
978 	if (codec_info_list[codec_index].codec_type == SOF_SDW_CODEC_TYPE_MIC &&
979 	    *link_id < SDW_DMIC_DAI_ID)
980 		*link_id = SDW_DMIC_DAI_ID;
981 
982 	cpu_dai_index = *cpu_id;
983 	for_each_pcm_streams(stream) {
984 		char *name, *cpu_name;
985 		int playback, capture;
986 		static const char * const sdw_stream_name[] = {
987 			"SDW%d-Playback",
988 			"SDW%d-Capture",
989 		};
990 
991 		if (!codec_info_list[codec_index].direction[stream])
992 			continue;
993 
994 		/* create stream name according to first link id */
995 		name = devm_kasprintf(dev, GFP_KERNEL,
996 				      sdw_stream_name[stream], cpu_dai_id[0]);
997 		if (!name)
998 			return -ENOMEM;
999 
1000 		/*
1001 		 * generate CPU DAI name base on the sdw link ID and
1002 		 * PIN ID with offset of 2 according to sdw dai driver.
1003 		 */
1004 		for (k = 0; k < cpu_dai_num; k++) {
1005 			cpu_name = devm_kasprintf(dev, GFP_KERNEL,
1006 						  "SDW%d Pin%d", cpu_dai_id[k],
1007 						  j + SDW_INTEL_BIDIR_PDI_BASE);
1008 			if (!cpu_name)
1009 				return -ENOMEM;
1010 
1011 			if (cpu_dai_index >= sdw_cpu_dai_num) {
1012 				dev_err(dev, "invalid cpu dai index %d",
1013 					cpu_dai_index);
1014 				return -EINVAL;
1015 			}
1016 
1017 			cpus[cpu_dai_index++].dai_name = cpu_name;
1018 		}
1019 
1020 		/*
1021 		 * We create sdw dai links at first stage, so link index should
1022 		 * not be larger than sdw_be_num
1023 		 */
1024 		if (*link_index >= sdw_be_num) {
1025 			dev_err(dev, "invalid dai link index %d", *link_index);
1026 			return -EINVAL;
1027 		}
1028 
1029 		if (*cpu_id >= sdw_cpu_dai_num) {
1030 			dev_err(dev, " invalid cpu dai index %d", *cpu_id);
1031 			return -EINVAL;
1032 		}
1033 
1034 		playback = (stream == SNDRV_PCM_STREAM_PLAYBACK);
1035 		capture = (stream == SNDRV_PCM_STREAM_CAPTURE);
1036 		init_dai_link(dev, dai_links + *link_index, (*link_id)++, name,
1037 			      playback, capture,
1038 			      cpus + *cpu_id, cpu_dai_num,
1039 			      codecs, codec_num,
1040 			      NULL, &sdw_ops);
1041 
1042 		/*
1043 		 * SoundWire DAILINKs use 'stream' functions and Bank Switch operations
1044 		 * based on wait_for_completion(), tag them as 'nonatomic'.
1045 		 */
1046 		dai_links[*link_index].nonatomic = true;
1047 
1048 		ret = set_codec_init_func(card, link, dai_links + (*link_index)++,
1049 					  playback, group_id);
1050 		if (ret < 0) {
1051 			dev_err(dev, "failed to init codec %d", codec_index);
1052 			return ret;
1053 		}
1054 
1055 		*cpu_id += cpu_dai_num;
1056 		j++;
1057 	}
1058 
1059 	return 0;
1060 }
1061 
1062 #define IDISP_CODEC_MASK	0x4
1063 
1064 static int sof_card_codec_conf_alloc(struct device *dev,
1065 				     struct snd_soc_acpi_mach_params *mach_params,
1066 				     struct snd_soc_codec_conf **codec_conf,
1067 				     int *codec_conf_count)
1068 {
1069 	const struct snd_soc_acpi_link_adr *adr_link;
1070 	struct snd_soc_codec_conf *c_conf;
1071 	int num_codecs = 0;
1072 	int i;
1073 
1074 	adr_link = mach_params->links;
1075 	if (!adr_link)
1076 		return -EINVAL;
1077 
1078 	/* generate DAI links by each sdw link */
1079 	for (; adr_link->num_adr; adr_link++) {
1080 		for (i = 0; i < adr_link->num_adr; i++) {
1081 			if (!adr_link->adr_d[i].name_prefix) {
1082 				dev_err(dev, "codec 0x%llx does not have a name prefix\n",
1083 					adr_link->adr_d[i].adr);
1084 				return -EINVAL;
1085 			}
1086 		}
1087 		num_codecs += adr_link->num_adr;
1088 	}
1089 
1090 	c_conf = devm_kzalloc(dev, num_codecs * sizeof(*c_conf), GFP_KERNEL);
1091 	if (!c_conf)
1092 		return -ENOMEM;
1093 
1094 	*codec_conf = c_conf;
1095 	*codec_conf_count = num_codecs;
1096 
1097 	return 0;
1098 }
1099 
1100 static int sof_card_dai_links_create(struct device *dev,
1101 				     struct snd_soc_acpi_mach *mach,
1102 				     struct snd_soc_card *card)
1103 {
1104 	int ssp_num, sdw_be_num = 0, hdmi_num = 0, dmic_num;
1105 	struct mc_private *ctx = snd_soc_card_get_drvdata(card);
1106 	struct snd_soc_dai_link_component *idisp_components;
1107 	struct snd_soc_dai_link_component *ssp_components;
1108 	struct snd_soc_acpi_mach_params *mach_params;
1109 	const struct snd_soc_acpi_link_adr *adr_link;
1110 	struct snd_soc_dai_link_component *cpus;
1111 	struct snd_soc_codec_conf *codec_conf;
1112 	bool ignore_pch_dmic = false;
1113 	int codec_conf_count;
1114 	int codec_conf_index = 0;
1115 	bool group_generated[SDW_MAX_GROUPS];
1116 	int ssp_codec_index, ssp_mask;
1117 	struct snd_soc_dai_link *links;
1118 	int num_links, link_index = 0;
1119 	char *name, *cpu_name;
1120 	int total_cpu_dai_num;
1121 	int sdw_cpu_dai_num;
1122 	int i, j, be_id = 0;
1123 	int cpu_id = 0;
1124 	int comp_num;
1125 	int ret;
1126 
1127 	mach_params = &mach->mach_params;
1128 
1129 	/* allocate codec conf, will be populated when dailinks are created */
1130 	ret = sof_card_codec_conf_alloc(dev, mach_params, &codec_conf, &codec_conf_count);
1131 	if (ret < 0)
1132 		return ret;
1133 
1134 	/* reset amp_num to ensure amp_num++ starts from 0 in each probe */
1135 	for (i = 0; i < ARRAY_SIZE(codec_info_list); i++)
1136 		codec_info_list[i].amp_num = 0;
1137 
1138 	if (mach_params->codec_mask & IDISP_CODEC_MASK) {
1139 		ctx->idisp_codec = true;
1140 
1141 		if (sof_sdw_quirk & SOF_SDW_TGL_HDMI)
1142 			hdmi_num = SOF_TGL_HDMI_COUNT;
1143 		else
1144 			hdmi_num = SOF_PRE_TGL_HDMI_COUNT;
1145 	}
1146 
1147 	ssp_mask = SOF_SSP_GET_PORT(sof_sdw_quirk);
1148 	/*
1149 	 * on generic tgl platform, I2S or sdw mode is supported
1150 	 * based on board rework. A ACPI device is registered in
1151 	 * system only when I2S mode is supported, not sdw mode.
1152 	 * Here check ACPI ID to confirm I2S is supported.
1153 	 */
1154 	ssp_codec_index = find_codec_info_acpi(mach->id);
1155 	ssp_num = ssp_codec_index >= 0 ? hweight_long(ssp_mask) : 0;
1156 	comp_num = hdmi_num + ssp_num;
1157 
1158 	ret = get_sdw_dailink_info(dev, mach_params->links,
1159 				   &sdw_be_num, &sdw_cpu_dai_num);
1160 	if (ret < 0) {
1161 		dev_err(dev, "failed to get sdw link info %d", ret);
1162 		return ret;
1163 	}
1164 
1165 	/* enable dmic01 & dmic16k */
1166 	dmic_num = (sof_sdw_quirk & SOF_SDW_PCH_DMIC || mach_params->dmic_num) ? 2 : 0;
1167 	comp_num += dmic_num;
1168 
1169 	if (sof_sdw_quirk & SOF_SSP_BT_OFFLOAD_PRESENT)
1170 		comp_num++;
1171 
1172 	dev_dbg(dev, "sdw %d, ssp %d, dmic %d, hdmi %d", sdw_be_num, ssp_num,
1173 		dmic_num, ctx->idisp_codec ? hdmi_num : 0);
1174 
1175 	/* allocate BE dailinks */
1176 	num_links = comp_num + sdw_be_num;
1177 	links = devm_kcalloc(dev, num_links, sizeof(*links), GFP_KERNEL);
1178 
1179 	/* allocated CPU DAIs */
1180 	total_cpu_dai_num = comp_num + sdw_cpu_dai_num;
1181 	cpus = devm_kcalloc(dev, total_cpu_dai_num, sizeof(*cpus),
1182 			    GFP_KERNEL);
1183 
1184 	if (!links || !cpus)
1185 		return -ENOMEM;
1186 
1187 	/* SDW */
1188 	if (!sdw_be_num)
1189 		goto SSP;
1190 
1191 	adr_link = mach_params->links;
1192 	if (!adr_link)
1193 		return -EINVAL;
1194 
1195 	/*
1196 	 * SoundWire Slaves aggregated in the same group may be
1197 	 * located on different hardware links. Clear array to indicate
1198 	 * CPU DAIs for this group have not been generated.
1199 	 */
1200 	for (i = 0; i < SDW_MAX_GROUPS; i++)
1201 		group_generated[i] = false;
1202 
1203 	/* generate DAI links by each sdw link */
1204 	for (; adr_link->num_adr; adr_link++) {
1205 		const struct snd_soc_acpi_endpoint *endpoint;
1206 
1207 		endpoint = adr_link->adr_d->endpoints;
1208 		if (endpoint->aggregated && !endpoint->group_id) {
1209 			dev_err(dev, "invalid group id on link %x",
1210 				adr_link->mask);
1211 			continue;
1212 		}
1213 
1214 		/* this group has been generated */
1215 		if (endpoint->aggregated &&
1216 		    group_generated[endpoint->group_id])
1217 			continue;
1218 
1219 		ret = create_sdw_dailink(card, dev, &link_index, links, sdw_be_num,
1220 					 sdw_cpu_dai_num, cpus, adr_link,
1221 					 &cpu_id, group_generated,
1222 					 codec_conf, codec_conf_count,
1223 					 &be_id, &codec_conf_index,
1224 					 &ignore_pch_dmic);
1225 		if (ret < 0) {
1226 			dev_err(dev, "failed to create dai link %d", link_index);
1227 			return ret;
1228 		}
1229 	}
1230 
1231 SSP:
1232 	/* SSP */
1233 	if (!ssp_num)
1234 		goto DMIC;
1235 
1236 	for (i = 0, j = 0; ssp_mask; i++, ssp_mask >>= 1) {
1237 		struct sof_sdw_codec_info *info;
1238 		int playback, capture;
1239 		char *codec_name;
1240 
1241 		if (!(ssp_mask & 0x1))
1242 			continue;
1243 
1244 		name = devm_kasprintf(dev, GFP_KERNEL,
1245 				      "SSP%d-Codec", i);
1246 		if (!name)
1247 			return -ENOMEM;
1248 
1249 		cpu_name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d Pin", i);
1250 		if (!cpu_name)
1251 			return -ENOMEM;
1252 
1253 		ssp_components = devm_kzalloc(dev, sizeof(*ssp_components),
1254 					      GFP_KERNEL);
1255 		if (!ssp_components)
1256 			return -ENOMEM;
1257 
1258 		info = &codec_info_list[ssp_codec_index];
1259 		codec_name = devm_kasprintf(dev, GFP_KERNEL, "i2c-%s:0%d",
1260 					    info->acpi_id, j++);
1261 		if (!codec_name)
1262 			return -ENOMEM;
1263 
1264 		ssp_components->name = codec_name;
1265 		ssp_components->dai_name = info->dai_name;
1266 		cpus[cpu_id].dai_name = cpu_name;
1267 
1268 		playback = info->direction[SNDRV_PCM_STREAM_PLAYBACK];
1269 		capture = info->direction[SNDRV_PCM_STREAM_CAPTURE];
1270 		init_dai_link(dev, links + link_index, be_id, name,
1271 			      playback, capture,
1272 			      cpus + cpu_id, 1,
1273 			      ssp_components, 1,
1274 			      NULL, info->ops);
1275 
1276 		ret = info->init(card, NULL, links + link_index, info, 0);
1277 		if (ret < 0)
1278 			return ret;
1279 
1280 		INC_ID(be_id, cpu_id, link_index);
1281 	}
1282 
1283 DMIC:
1284 	/* dmic */
1285 	if (dmic_num > 0) {
1286 		if (ignore_pch_dmic) {
1287 			dev_warn(dev, "Ignoring PCH DMIC\n");
1288 			goto HDMI;
1289 		}
1290 		cpus[cpu_id].dai_name = "DMIC01 Pin";
1291 		init_dai_link(dev, links + link_index, be_id, "dmic01",
1292 			      0, 1, // DMIC only supports capture
1293 			      cpus + cpu_id, 1,
1294 			      dmic_component, 1,
1295 			      sof_sdw_dmic_init, NULL);
1296 		INC_ID(be_id, cpu_id, link_index);
1297 
1298 		cpus[cpu_id].dai_name = "DMIC16k Pin";
1299 		init_dai_link(dev, links + link_index, be_id, "dmic16k",
1300 			      0, 1, // DMIC only supports capture
1301 			      cpus + cpu_id, 1,
1302 			      dmic_component, 1,
1303 			      /* don't call sof_sdw_dmic_init() twice */
1304 			      NULL, NULL);
1305 		INC_ID(be_id, cpu_id, link_index);
1306 	}
1307 
1308 HDMI:
1309 	/* HDMI */
1310 	if (hdmi_num > 0) {
1311 		idisp_components = devm_kcalloc(dev, hdmi_num,
1312 						sizeof(*idisp_components),
1313 						GFP_KERNEL);
1314 		if (!idisp_components)
1315 			return -ENOMEM;
1316 	}
1317 
1318 	for (i = 0; i < hdmi_num; i++) {
1319 		name = devm_kasprintf(dev, GFP_KERNEL,
1320 				      "iDisp%d", i + 1);
1321 		if (!name)
1322 			return -ENOMEM;
1323 
1324 		if (ctx->idisp_codec) {
1325 			idisp_components[i].name = "ehdaudio0D2";
1326 			idisp_components[i].dai_name = devm_kasprintf(dev,
1327 								      GFP_KERNEL,
1328 								      "intel-hdmi-hifi%d",
1329 								      i + 1);
1330 			if (!idisp_components[i].dai_name)
1331 				return -ENOMEM;
1332 		} else {
1333 			idisp_components[i].name = "snd-soc-dummy";
1334 			idisp_components[i].dai_name = "snd-soc-dummy-dai";
1335 		}
1336 
1337 		cpu_name = devm_kasprintf(dev, GFP_KERNEL,
1338 					  "iDisp%d Pin", i + 1);
1339 		if (!cpu_name)
1340 			return -ENOMEM;
1341 
1342 		cpus[cpu_id].dai_name = cpu_name;
1343 		init_dai_link(dev, links + link_index, be_id, name,
1344 			      1, 0, // HDMI only supports playback
1345 			      cpus + cpu_id, 1,
1346 			      idisp_components + i, 1,
1347 			      sof_sdw_hdmi_init, NULL);
1348 		INC_ID(be_id, cpu_id, link_index);
1349 	}
1350 
1351 	if (sof_sdw_quirk & SOF_SSP_BT_OFFLOAD_PRESENT) {
1352 		int port = (sof_sdw_quirk & SOF_BT_OFFLOAD_SSP_MASK) >>
1353 				SOF_BT_OFFLOAD_SSP_SHIFT;
1354 
1355 		name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-BT", port);
1356 		if (!name)
1357 			return -ENOMEM;
1358 
1359 		ssp_components = devm_kzalloc(dev, sizeof(*ssp_components),
1360 						GFP_KERNEL);
1361 		if (!ssp_components)
1362 			return -ENOMEM;
1363 
1364 		ssp_components->name = "snd-soc-dummy";
1365 		ssp_components->dai_name = "snd-soc-dummy-dai";
1366 
1367 		cpu_name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d Pin", port);
1368 		if (!cpu_name)
1369 			return -ENOMEM;
1370 
1371 		cpus[cpu_id].dai_name = cpu_name;
1372 		init_dai_link(dev, links + link_index, be_id, name, 1, 1,
1373 				cpus + cpu_id, 1, ssp_components, 1, NULL, NULL);
1374 	}
1375 
1376 	card->dai_link = links;
1377 	card->num_links = num_links;
1378 
1379 	card->codec_conf = codec_conf;
1380 	card->num_configs = codec_conf_count;
1381 
1382 	return 0;
1383 }
1384 
1385 static int sof_sdw_card_late_probe(struct snd_soc_card *card)
1386 {
1387 	struct mc_private *ctx = snd_soc_card_get_drvdata(card);
1388 	int ret = 0;
1389 	int i;
1390 
1391 	for (i = 0; i < ARRAY_SIZE(codec_info_list); i++) {
1392 		if (!codec_info_list[i].late_probe)
1393 			continue;
1394 
1395 		ret = codec_info_list[i].codec_card_late_probe(card);
1396 		if (ret < 0)
1397 			return ret;
1398 	}
1399 
1400 	if (ctx->idisp_codec)
1401 		ret = sof_sdw_hdmi_card_late_probe(card);
1402 
1403 	return ret;
1404 }
1405 
1406 /* SoC card */
1407 static const char sdw_card_long_name[] = "Intel Soundwire SOF";
1408 
1409 static struct snd_soc_card card_sof_sdw = {
1410 	.name = "soundwire",
1411 	.owner = THIS_MODULE,
1412 	.late_probe = sof_sdw_card_late_probe,
1413 };
1414 
1415 static void mc_dailink_exit_loop(struct snd_soc_card *card)
1416 {
1417 	struct snd_soc_dai_link *link;
1418 	int ret;
1419 	int i, j;
1420 
1421 	for (i = 0; i < ARRAY_SIZE(codec_info_list); i++) {
1422 		if (!codec_info_list[i].exit)
1423 			continue;
1424 		/*
1425 		 * We don't need to call .exit function if there is no matched
1426 		 * dai link found.
1427 		 */
1428 		for_each_card_prelinks(card, j, link) {
1429 			if (!strcmp(link->codecs[0].dai_name,
1430 				    codec_info_list[i].dai_name)) {
1431 				ret = codec_info_list[i].exit(card, link);
1432 				if (ret)
1433 					dev_warn(card->dev,
1434 						 "codec exit failed %d\n",
1435 						 ret);
1436 				break;
1437 			}
1438 		}
1439 	}
1440 }
1441 
1442 static int mc_probe(struct platform_device *pdev)
1443 {
1444 	struct snd_soc_card *card = &card_sof_sdw;
1445 	struct snd_soc_acpi_mach *mach;
1446 	struct mc_private *ctx;
1447 	int amp_num = 0, i;
1448 	int ret;
1449 
1450 	dev_dbg(&pdev->dev, "Entry\n");
1451 
1452 	ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
1453 	if (!ctx)
1454 		return -ENOMEM;
1455 
1456 	dmi_check_system(sof_sdw_quirk_table);
1457 
1458 	if (quirk_override != -1) {
1459 		dev_info(&pdev->dev, "Overriding quirk 0x%lx => 0x%x\n",
1460 			 sof_sdw_quirk, quirk_override);
1461 		sof_sdw_quirk = quirk_override;
1462 	}
1463 	log_quirks(&pdev->dev);
1464 
1465 	INIT_LIST_HEAD(&ctx->hdmi_pcm_list);
1466 
1467 	card->dev = &pdev->dev;
1468 	snd_soc_card_set_drvdata(card, ctx);
1469 
1470 	mach = pdev->dev.platform_data;
1471 	ret = sof_card_dai_links_create(&pdev->dev, mach,
1472 					card);
1473 	if (ret < 0)
1474 		return ret;
1475 
1476 	/*
1477 	 * the default amp_num is zero for each codec and
1478 	 * amp_num will only be increased for active amp
1479 	 * codecs on used platform
1480 	 */
1481 	for (i = 0; i < ARRAY_SIZE(codec_info_list); i++)
1482 		amp_num += codec_info_list[i].amp_num;
1483 
1484 	card->components = devm_kasprintf(card->dev, GFP_KERNEL,
1485 					  "cfg-spk:%d cfg-amp:%d",
1486 					  (sof_sdw_quirk & SOF_SDW_FOUR_SPK)
1487 					  ? 4 : 2, amp_num);
1488 	if (!card->components)
1489 		return -ENOMEM;
1490 
1491 	if (mach->mach_params.dmic_num) {
1492 		card->components = devm_kasprintf(card->dev, GFP_KERNEL,
1493 						  "%s mic:dmic cfg-mics:%d",
1494 						  card->components,
1495 						  mach->mach_params.dmic_num);
1496 		if (!card->components)
1497 			return -ENOMEM;
1498 	}
1499 
1500 	card->long_name = sdw_card_long_name;
1501 
1502 	/* Register the card */
1503 	ret = devm_snd_soc_register_card(&pdev->dev, card);
1504 	if (ret) {
1505 		dev_err(card->dev, "snd_soc_register_card failed %d\n", ret);
1506 		mc_dailink_exit_loop(card);
1507 		return ret;
1508 	}
1509 
1510 	platform_set_drvdata(pdev, card);
1511 
1512 	return ret;
1513 }
1514 
1515 static int mc_remove(struct platform_device *pdev)
1516 {
1517 	struct snd_soc_card *card = platform_get_drvdata(pdev);
1518 
1519 	mc_dailink_exit_loop(card);
1520 
1521 	return 0;
1522 }
1523 
1524 static struct platform_driver sof_sdw_driver = {
1525 	.driver = {
1526 		.name = "sof_sdw",
1527 		.pm = &snd_soc_pm_ops,
1528 	},
1529 	.probe = mc_probe,
1530 	.remove = mc_remove,
1531 };
1532 
1533 module_platform_driver(sof_sdw_driver);
1534 
1535 MODULE_DESCRIPTION("ASoC SoundWire Generic Machine driver");
1536 MODULE_AUTHOR("Bard Liao <yung-chuan.liao@linux.intel.com>");
1537 MODULE_AUTHOR("Rander Wang <rander.wang@linux.intel.com>");
1538 MODULE_AUTHOR("Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>");
1539 MODULE_LICENSE("GPL v2");
1540 MODULE_ALIAS("platform:sof_sdw");
1541 MODULE_IMPORT_NS(SND_SOC_INTEL_HDA_DSP_COMMON);
1542 MODULE_IMPORT_NS(SND_SOC_INTEL_SOF_MAXIM_COMMON);
1543