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