xref: /openbmc/linux/sound/soc/intel/boards/sof_sdw.c (revision e82c878d)
1 // SPDX-License-Identifier: GPL-2.0
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 
19 #define INC_ID(BE, CPU, LINK)	do { (BE)++; (CPU)++; (LINK)++; } while (0)
20 
21 static int sof_sdw_quirk_cb(const struct dmi_system_id *id)
22 {
23 	sof_sdw_quirk = (unsigned long)id->driver_data;
24 	return 1;
25 }
26 
27 static const struct dmi_system_id sof_sdw_quirk_table[] = {
28 	{
29 		.callback = sof_sdw_quirk_cb,
30 		.matches = {
31 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
32 			DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "09C6")
33 		},
34 		.driver_data = (void *)(SOF_RT711_JD_SRC_JD2 |
35 					SOF_RT715_DAI_ID_FIX),
36 	},
37 	{
38 		/* early version of SKU 09C6 */
39 		.callback = sof_sdw_quirk_cb,
40 		.matches = {
41 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
42 			DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0983")
43 		},
44 		.driver_data = (void *)(SOF_RT711_JD_SRC_JD2 |
45 					SOF_RT715_DAI_ID_FIX),
46 	},
47 	{
48 		.callback = sof_sdw_quirk_cb,
49 		.matches = {
50 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
51 			DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "098F"),
52 		},
53 		.driver_data = (void *)(SOF_RT711_JD_SRC_JD2 |
54 					SOF_RT715_DAI_ID_FIX |
55 					SOF_SDW_FOUR_SPK),
56 	},
57 		{
58 		.callback = sof_sdw_quirk_cb,
59 		.matches = {
60 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
61 			DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0990"),
62 		},
63 		.driver_data = (void *)(SOF_RT711_JD_SRC_JD2 |
64 					SOF_RT715_DAI_ID_FIX |
65 					SOF_SDW_FOUR_SPK),
66 	},
67 	{
68 		.callback = sof_sdw_quirk_cb,
69 		.matches = {
70 			DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
71 			DMI_MATCH(DMI_PRODUCT_NAME,
72 				  "Tiger Lake Client Platform"),
73 		},
74 		.driver_data = (void *)(SOF_RT711_JD_SRC_JD1 |
75 				SOF_SDW_TGL_HDMI | SOF_SDW_PCH_DMIC |
76 				SOF_SSP_PORT(SOF_I2S_SSP2)),
77 	},
78 	{
79 		.callback = sof_sdw_quirk_cb,
80 		.matches = {
81 			DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
82 			DMI_MATCH(DMI_PRODUCT_NAME, "Ice Lake Client"),
83 		},
84 		.driver_data = (void *)SOF_SDW_PCH_DMIC,
85 	},
86 	{
87 		.callback = sof_sdw_quirk_cb,
88 		.matches = {
89 			DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
90 			DMI_MATCH(DMI_PRODUCT_NAME, "CometLake Client"),
91 		},
92 		.driver_data = (void *)SOF_SDW_PCH_DMIC,
93 	},
94 	{
95 		.callback = sof_sdw_quirk_cb,
96 		.matches = {
97 			DMI_MATCH(DMI_SYS_VENDOR, "Google"),
98 			DMI_MATCH(DMI_PRODUCT_NAME, "Volteer"),
99 		},
100 		.driver_data = (void *)(SOF_SDW_TGL_HDMI | SOF_SDW_PCH_DMIC),
101 	},
102 
103 	{}
104 };
105 
106 static struct snd_soc_codec_conf codec_conf[] = {
107 	{
108 		.dlc = COMP_CODEC_CONF("sdw:0:25d:711:0"),
109 		.name_prefix = "rt711",
110 	},
111 	/* rt1308 w/ I2S connection */
112 	{
113 		.dlc = COMP_CODEC_CONF("i2c-10EC1308:00"),
114 		.name_prefix = "rt1308-1",
115 	},
116 	/* rt1308 left on link 1 */
117 	{
118 		.dlc = COMP_CODEC_CONF("sdw:1:25d:1308:0"),
119 		.name_prefix = "rt1308-1",
120 	},
121 	/* two 1308s on link1 with different unique id */
122 	{
123 		.dlc = COMP_CODEC_CONF("sdw:1:25d:1308:0:0"),
124 		.name_prefix = "rt1308-1",
125 	},
126 	{
127 		.dlc = COMP_CODEC_CONF("sdw:1:25d:1308:0:2"),
128 		.name_prefix = "rt1308-2",
129 	},
130 	/* rt1308 right on link 2 */
131 	{
132 		.dlc = COMP_CODEC_CONF("sdw:2:25d:1308:0"),
133 		.name_prefix = "rt1308-2",
134 	},
135 	{
136 		.dlc = COMP_CODEC_CONF("sdw:3:25d:715:0"),
137 		.name_prefix = "rt715",
138 	},
139 	{
140 		.dlc = COMP_CODEC_CONF("sdw:0:25d:5682:0"),
141 		.name_prefix = "rt5682",
142 	},
143 };
144 
145 static struct snd_soc_dai_link_component dmic_component[] = {
146 	{
147 		.name = "dmic-codec",
148 		.dai_name = "dmic-hifi",
149 	}
150 };
151 
152 static struct snd_soc_dai_link_component platform_component[] = {
153 	{
154 		/* name might be overridden during probe */
155 		.name = "0000:00:1f.3"
156 	}
157 };
158 
159 /* these wrappers are only needed to avoid typecast compilation errors */
160 static int sdw_startup(struct snd_pcm_substream *substream)
161 {
162 	return sdw_startup_stream(substream);
163 }
164 
165 static void sdw_shutdown(struct snd_pcm_substream *substream)
166 {
167 	sdw_shutdown_stream(substream);
168 }
169 
170 static const struct snd_soc_ops sdw_ops = {
171 	.startup = sdw_startup,
172 	.shutdown = sdw_shutdown,
173 };
174 
175 static struct sof_sdw_codec_info codec_info_list[] = {
176 	{
177 		.id = 0x700,
178 		.direction = {true, true},
179 		.dai_name = "rt700-aif1",
180 		.init = sof_sdw_rt700_init,
181 	},
182 	{
183 		.id = 0x711,
184 		.direction = {true, true},
185 		.dai_name = "rt711-aif1",
186 		.init = sof_sdw_rt711_init,
187 	},
188 	{
189 		.id = 0x1308,
190 		.acpi_id = "10EC1308",
191 		.direction = {true, false},
192 		.dai_name = "rt1308-aif",
193 		.ops = &sof_sdw_rt1308_i2s_ops,
194 		.init = sof_sdw_rt1308_init,
195 	},
196 	{
197 		.id = 0x715,
198 		.direction = {false, true},
199 		.dai_name = "rt715-aif2",
200 		.init = sof_sdw_rt715_init,
201 	},
202 	{
203 		.id = 0x5682,
204 		.direction = {true, true},
205 		.dai_name = "rt5682-sdw",
206 		.init = sof_sdw_rt5682_init,
207 	},
208 };
209 
210 static inline int find_codec_info_part(unsigned int part_id)
211 {
212 	int i;
213 
214 	for (i = 0; i < ARRAY_SIZE(codec_info_list); i++)
215 		if (part_id == codec_info_list[i].id)
216 			break;
217 
218 	if (i == ARRAY_SIZE(codec_info_list))
219 		return -EINVAL;
220 
221 	return i;
222 }
223 
224 static inline int find_codec_info_acpi(const u8 *acpi_id)
225 {
226 	int i;
227 
228 	if (!acpi_id[0])
229 		return -EINVAL;
230 
231 	for (i = 0; i < ARRAY_SIZE(codec_info_list); i++)
232 		if (!memcmp(codec_info_list[i].acpi_id, acpi_id,
233 			    ACPI_ID_LEN))
234 			break;
235 
236 	if (i == ARRAY_SIZE(codec_info_list))
237 		return -EINVAL;
238 
239 	return i;
240 }
241 
242 /*
243  * get BE dailink number and CPU DAI number based on sdw link adr.
244  * Since some sdw slaves may be aggregated, the CPU DAI number
245  * may be larger than the number of BE dailinks.
246  */
247 static int get_sdw_dailink_info(const struct snd_soc_acpi_link_adr *links,
248 				int *sdw_be_num, int *sdw_cpu_dai_num)
249 {
250 	const struct snd_soc_acpi_link_adr *link;
251 	bool group_visited[SDW_MAX_GROUPS];
252 	bool no_aggregation;
253 	int i;
254 
255 	no_aggregation = sof_sdw_quirk & SOF_SDW_NO_AGGREGATION;
256 	*sdw_cpu_dai_num = 0;
257 	*sdw_be_num  = 0;
258 
259 	if (!links)
260 		return -EINVAL;
261 
262 	for (i = 0; i < SDW_MAX_GROUPS; i++)
263 		group_visited[i] = false;
264 
265 	for (link = links; link->num_adr; link++) {
266 		const struct snd_soc_acpi_endpoint *endpoint;
267 		int part_id, codec_index;
268 		int stream;
269 		u64 adr;
270 
271 		adr = link->adr_d->adr;
272 		part_id = SDW_PART_ID(adr);
273 		codec_index = find_codec_info_part(part_id);
274 		if (codec_index < 0)
275 			return codec_index;
276 
277 		endpoint = link->adr_d->endpoints;
278 
279 		/* count DAI number for playback and capture */
280 		for_each_pcm_streams(stream) {
281 			if (!codec_info_list[codec_index].direction[stream])
282 				continue;
283 
284 			(*sdw_cpu_dai_num)++;
285 
286 			/* count BE for each non-aggregated slave or group */
287 			if (!endpoint->aggregated || no_aggregation ||
288 			    !group_visited[endpoint->group_id])
289 				(*sdw_be_num)++;
290 		}
291 
292 		if (endpoint->aggregated)
293 			group_visited[endpoint->group_id] = true;
294 	}
295 
296 	return 0;
297 }
298 
299 static void init_dai_link(struct snd_soc_dai_link *dai_links, int be_id,
300 			  char *name, int playback, int capture,
301 			  struct snd_soc_dai_link_component *cpus,
302 			  int cpus_num,
303 			  struct snd_soc_dai_link_component *codecs,
304 			  int codecs_num,
305 			  int (*init)(struct snd_soc_pcm_runtime *rtd),
306 			  const struct snd_soc_ops *ops)
307 {
308 	dai_links->id = be_id;
309 	dai_links->name = name;
310 	dai_links->platforms = platform_component;
311 	dai_links->num_platforms = ARRAY_SIZE(platform_component);
312 	dai_links->nonatomic = true;
313 	dai_links->no_pcm = 1;
314 	dai_links->cpus = cpus;
315 	dai_links->num_cpus = cpus_num;
316 	dai_links->codecs = codecs;
317 	dai_links->num_codecs = codecs_num;
318 	dai_links->dpcm_playback = playback;
319 	dai_links->dpcm_capture = capture;
320 	dai_links->init = init;
321 	dai_links->ops = ops;
322 }
323 
324 static bool is_unique_device(const struct snd_soc_acpi_link_adr *link,
325 			     unsigned int sdw_version,
326 			     unsigned int mfg_id,
327 			     unsigned int part_id,
328 			     unsigned int class_id,
329 			     int index_in_link
330 			    )
331 {
332 	int i;
333 
334 	for (i = 0; i < link->num_adr; i++) {
335 		unsigned int sdw1_version, mfg1_id, part1_id, class1_id;
336 		u64 adr;
337 
338 		/* skip itself */
339 		if (i == index_in_link)
340 			continue;
341 
342 		adr = link->adr_d[i].adr;
343 
344 		sdw1_version = SDW_VERSION(adr);
345 		mfg1_id = SDW_MFG_ID(adr);
346 		part1_id = SDW_PART_ID(adr);
347 		class1_id = SDW_CLASS_ID(adr);
348 
349 		if (sdw_version == sdw1_version &&
350 		    mfg_id == mfg1_id &&
351 		    part_id == part1_id &&
352 		    class_id == class1_id)
353 			return false;
354 	}
355 
356 	return true;
357 }
358 
359 static int create_codec_dai_name(struct device *dev,
360 				 const struct snd_soc_acpi_link_adr *link,
361 				 struct snd_soc_dai_link_component *codec,
362 				 int offset)
363 {
364 	int i;
365 
366 	for (i = 0; i < link->num_adr; i++) {
367 		unsigned int sdw_version, unique_id, mfg_id;
368 		unsigned int link_id, part_id, class_id;
369 		int codec_index, comp_index;
370 		char *codec_str;
371 		u64 adr;
372 
373 		adr = link->adr_d[i].adr;
374 
375 		sdw_version = SDW_VERSION(adr);
376 		link_id = SDW_DISCO_LINK_ID(adr);
377 		unique_id = SDW_UNIQUE_ID(adr);
378 		mfg_id = SDW_MFG_ID(adr);
379 		part_id = SDW_PART_ID(adr);
380 		class_id = SDW_CLASS_ID(adr);
381 
382 		comp_index = i + offset;
383 		if (is_unique_device(link, sdw_version, mfg_id, part_id,
384 				     class_id, i)) {
385 			codec_str = "sdw:%x:%x:%x:%x";
386 			codec[comp_index].name =
387 				devm_kasprintf(dev, GFP_KERNEL, codec_str,
388 					       link_id, mfg_id, part_id,
389 					       class_id);
390 		} else {
391 			codec_str = "sdw:%x:%x:%x:%x:%x";
392 			codec[comp_index].name =
393 				devm_kasprintf(dev, GFP_KERNEL, codec_str,
394 					       link_id, mfg_id, part_id,
395 					       class_id, unique_id);
396 		}
397 
398 		if (!codec[comp_index].name)
399 			return -ENOMEM;
400 
401 		codec_index = find_codec_info_part(part_id);
402 		if (codec_index < 0)
403 			return codec_index;
404 
405 		codec[comp_index].dai_name =
406 			codec_info_list[codec_index].dai_name;
407 	}
408 
409 	return 0;
410 }
411 
412 static int set_codec_init_func(const struct snd_soc_acpi_link_adr *link,
413 			       struct snd_soc_dai_link *dai_links,
414 			       bool playback)
415 {
416 	int i;
417 
418 	for (i = 0; i < link->num_adr; i++) {
419 		unsigned int part_id;
420 		int codec_index;
421 
422 		part_id = SDW_PART_ID(link->adr_d[i].adr);
423 		codec_index = find_codec_info_part(part_id);
424 
425 		if (codec_index < 0)
426 			return codec_index;
427 
428 		if (codec_info_list[codec_index].init)
429 			codec_info_list[codec_index].init(link, dai_links,
430 						 &codec_info_list[codec_index],
431 						 playback);
432 	}
433 
434 	return 0;
435 }
436 
437 /*
438  * check endpoint status in slaves and gather link ID for all slaves in
439  * the same group to generate different CPU DAI. Now only support
440  * one sdw link with all slaves set with only single group id.
441  *
442  * one slave on one sdw link with aggregated = 0
443  * one sdw BE DAI <---> one-cpu DAI <---> one-codec DAI
444  *
445  * two or more slaves on one sdw link with aggregated = 0
446  * one sdw BE DAI  <---> one-cpu DAI <---> multi-codec DAIs
447  *
448  * multiple links with multiple slaves with aggregated = 1
449  * one sdw BE DAI  <---> 1 .. N CPU DAIs <----> 1 .. N codec DAIs
450  */
451 static int get_slave_info(const struct snd_soc_acpi_link_adr *adr_link,
452 			  struct device *dev, int *cpu_dai_id, int *cpu_dai_num,
453 			  int *codec_num, int *group_id,
454 			  bool *group_generated)
455 {
456 	const struct snd_soc_acpi_adr_device *adr_d;
457 	const struct snd_soc_acpi_link_adr *adr_next;
458 	bool no_aggregation;
459 	int index = 0;
460 
461 	no_aggregation = sof_sdw_quirk & SOF_SDW_NO_AGGREGATION;
462 	*codec_num = adr_link->num_adr;
463 	adr_d = adr_link->adr_d;
464 
465 	/* make sure the link mask has a single bit set */
466 	if (!is_power_of_2(adr_link->mask))
467 		return -EINVAL;
468 
469 	cpu_dai_id[index++] = ffs(adr_link->mask) - 1;
470 	if (!adr_d->endpoints->aggregated || no_aggregation) {
471 		*cpu_dai_num = 1;
472 		*group_id = 0;
473 		return 0;
474 	}
475 
476 	*group_id = adr_d->endpoints->group_id;
477 
478 	/* gather other link ID of slaves in the same group */
479 	for (adr_next = adr_link + 1; adr_next && adr_next->num_adr;
480 		adr_next++) {
481 		const struct snd_soc_acpi_endpoint *endpoint;
482 
483 		endpoint = adr_next->adr_d->endpoints;
484 		if (!endpoint->aggregated ||
485 		    endpoint->group_id != *group_id)
486 			continue;
487 
488 		/* make sure the link mask has a single bit set */
489 		if (!is_power_of_2(adr_next->mask))
490 			return -EINVAL;
491 
492 		if (index >= SDW_MAX_CPU_DAIS) {
493 			dev_err(dev, " cpu_dai_id array overflows");
494 			return -EINVAL;
495 		}
496 
497 		cpu_dai_id[index++] = ffs(adr_next->mask) - 1;
498 		*codec_num += adr_next->num_adr;
499 	}
500 
501 	/*
502 	 * indicate CPU DAIs for this group have been generated
503 	 * to avoid generating CPU DAIs for this group again.
504 	 */
505 	group_generated[*group_id] = true;
506 	*cpu_dai_num = index;
507 
508 	return 0;
509 }
510 
511 static int create_sdw_dailink(struct device *dev, int *be_index,
512 			      struct snd_soc_dai_link *dai_links,
513 			      int sdw_be_num, int sdw_cpu_dai_num,
514 			      struct snd_soc_dai_link_component *cpus,
515 			      const struct snd_soc_acpi_link_adr *link,
516 			      int *cpu_id, bool *group_generated)
517 {
518 	const struct snd_soc_acpi_link_adr *link_next;
519 	struct snd_soc_dai_link_component *codecs;
520 	int cpu_dai_id[SDW_MAX_CPU_DAIS];
521 	int cpu_dai_num, cpu_dai_index;
522 	unsigned int part_id, group_id;
523 	int codec_idx = 0;
524 	int i = 0, j = 0;
525 	int codec_index;
526 	int codec_num;
527 	int stream;
528 	int ret;
529 	int k;
530 
531 	ret = get_slave_info(link, dev, cpu_dai_id, &cpu_dai_num, &codec_num,
532 			     &group_id, group_generated);
533 	if (ret)
534 		return ret;
535 
536 	codecs = devm_kcalloc(dev, codec_num, sizeof(*codecs), GFP_KERNEL);
537 	if (!codecs)
538 		return -ENOMEM;
539 
540 	/* generate codec name on different links in the same group */
541 	for (link_next = link; link_next && link_next->num_adr &&
542 	     i < cpu_dai_num; link_next++) {
543 		const struct snd_soc_acpi_endpoint *endpoints;
544 
545 		endpoints = link_next->adr_d->endpoints;
546 		if (group_id && (!endpoints->aggregated ||
547 				 endpoints->group_id != group_id))
548 			continue;
549 
550 		/* skip the link excluded by this processed group */
551 		if (cpu_dai_id[i] != ffs(link_next->mask) - 1)
552 			continue;
553 
554 		ret = create_codec_dai_name(dev, link_next, codecs, codec_idx);
555 		if (ret < 0)
556 			return ret;
557 
558 		/* check next link to create codec dai in the processed group */
559 		i++;
560 		codec_idx += link_next->num_adr;
561 	}
562 
563 	/* find codec info to create BE DAI */
564 	part_id = SDW_PART_ID(link->adr_d[0].adr);
565 	codec_index = find_codec_info_part(part_id);
566 	if (codec_index < 0)
567 		return codec_index;
568 
569 	cpu_dai_index = *cpu_id;
570 	for_each_pcm_streams(stream) {
571 		char *name, *cpu_name;
572 		int playback, capture;
573 		static const char * const sdw_stream_name[] = {
574 			"SDW%d-Playback",
575 			"SDW%d-Capture",
576 		};
577 
578 		if (!codec_info_list[codec_index].direction[stream])
579 			continue;
580 
581 		/* create stream name according to first link id */
582 		name = devm_kasprintf(dev, GFP_KERNEL,
583 				      sdw_stream_name[stream], cpu_dai_id[0]);
584 		if (!name)
585 			return -ENOMEM;
586 
587 		/*
588 		 * generate CPU DAI name base on the sdw link ID and
589 		 * PIN ID with offset of 2 according to sdw dai driver.
590 		 */
591 		for (k = 0; k < cpu_dai_num; k++) {
592 			cpu_name = devm_kasprintf(dev, GFP_KERNEL,
593 						  "SDW%d Pin%d", cpu_dai_id[k],
594 						  j + SDW_INTEL_BIDIR_PDI_BASE);
595 			if (!cpu_name)
596 				return -ENOMEM;
597 
598 			if (cpu_dai_index >= sdw_cpu_dai_num) {
599 				dev_err(dev, "invalid cpu dai index %d",
600 					cpu_dai_index);
601 				return -EINVAL;
602 			}
603 
604 			cpus[cpu_dai_index++].dai_name = cpu_name;
605 		}
606 
607 		if (*be_index >= sdw_be_num) {
608 			dev_err(dev, " invalid be dai index %d", *be_index);
609 			return -EINVAL;
610 		}
611 
612 		if (*cpu_id >= sdw_cpu_dai_num) {
613 			dev_err(dev, " invalid cpu dai index %d", *cpu_id);
614 			return -EINVAL;
615 		}
616 
617 		playback = (stream == SNDRV_PCM_STREAM_PLAYBACK);
618 		capture = (stream == SNDRV_PCM_STREAM_CAPTURE);
619 		init_dai_link(dai_links + *be_index, *be_index, name,
620 			      playback, capture,
621 			      cpus + *cpu_id, cpu_dai_num,
622 			      codecs, codec_num,
623 			      NULL, &sdw_ops);
624 
625 		ret = set_codec_init_func(link, dai_links + (*be_index)++,
626 					  playback);
627 		if (ret < 0) {
628 			dev_err(dev, "failed to init codec %d", codec_index);
629 			return ret;
630 		}
631 
632 		*cpu_id += cpu_dai_num;
633 		j++;
634 	}
635 
636 	return 0;
637 }
638 
639 /*
640  * DAI link ID of SSP & DMIC & HDMI are based on last
641  * link ID used by sdw link. Since be_id may be changed
642  * in init func of sdw codec, it is not equal to be_id
643  */
644 static inline int get_next_be_id(struct snd_soc_dai_link *links,
645 				 int be_id)
646 {
647 	return links[be_id - 1].id + 1;
648 }
649 
650 static int sof_card_dai_links_create(struct device *dev,
651 				     struct snd_soc_acpi_mach *mach,
652 				     struct snd_soc_card *card)
653 {
654 	int ssp_num, sdw_be_num = 0, hdmi_num = 0, dmic_num;
655 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC)
656 	struct snd_soc_dai_link_component *idisp_components;
657 #endif
658 	struct snd_soc_dai_link_component *ssp_components;
659 	struct snd_soc_acpi_mach_params *mach_params;
660 	const struct snd_soc_acpi_link_adr *adr_link;
661 	struct snd_soc_dai_link_component *cpus;
662 	bool group_generated[SDW_MAX_GROUPS];
663 	int ssp_codec_index, ssp_mask;
664 	struct snd_soc_dai_link *links;
665 	int num_links, link_id = 0;
666 	char *name, *cpu_name;
667 	int total_cpu_dai_num;
668 	int sdw_cpu_dai_num;
669 	int i, j, be_id = 0;
670 	int cpu_id = 0;
671 	int comp_num;
672 	int ret;
673 
674 	/* reset amp_num to ensure amp_num++ starts from 0 in each probe */
675 	for (i = 0; i < ARRAY_SIZE(codec_info_list); i++)
676 		codec_info_list[i].amp_num = 0;
677 
678 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC)
679 	hdmi_num = sof_sdw_quirk & SOF_SDW_TGL_HDMI ?
680 				SOF_TGL_HDMI_COUNT : SOF_PRE_TGL_HDMI_COUNT;
681 #endif
682 
683 	ssp_mask = SOF_SSP_GET_PORT(sof_sdw_quirk);
684 	/*
685 	 * on generic tgl platform, I2S or sdw mode is supported
686 	 * based on board rework. A ACPI device is registered in
687 	 * system only when I2S mode is supported, not sdw mode.
688 	 * Here check ACPI ID to confirm I2S is supported.
689 	 */
690 	ssp_codec_index = find_codec_info_acpi(mach->id);
691 	ssp_num = ssp_codec_index >= 0 ? hweight_long(ssp_mask) : 0;
692 	comp_num = hdmi_num + ssp_num;
693 
694 	mach_params = &mach->mach_params;
695 	ret = get_sdw_dailink_info(mach_params->links,
696 				   &sdw_be_num, &sdw_cpu_dai_num);
697 	if (ret < 0) {
698 		dev_err(dev, "failed to get sdw link info %d", ret);
699 		return ret;
700 	}
701 
702 	/* enable dmic01 & dmic16k */
703 	dmic_num = (sof_sdw_quirk & SOF_SDW_PCH_DMIC) ? 2 : 0;
704 	comp_num += dmic_num;
705 
706 	dev_dbg(dev, "sdw %d, ssp %d, dmic %d, hdmi %d", sdw_be_num, ssp_num,
707 		dmic_num, hdmi_num);
708 
709 	/* allocate BE dailinks */
710 	num_links = comp_num + sdw_be_num;
711 	links = devm_kcalloc(dev, num_links, sizeof(*links), GFP_KERNEL);
712 
713 	/* allocated CPU DAIs */
714 	total_cpu_dai_num = comp_num + sdw_cpu_dai_num;
715 	cpus = devm_kcalloc(dev, total_cpu_dai_num, sizeof(*cpus),
716 			    GFP_KERNEL);
717 
718 	if (!links || !cpus)
719 		return -ENOMEM;
720 
721 	/* SDW */
722 	if (!sdw_be_num)
723 		goto SSP;
724 
725 	adr_link = mach_params->links;
726 	if (!adr_link)
727 		return -EINVAL;
728 
729 	/*
730 	 * SoundWire Slaves aggregated in the same group may be
731 	 * located on different hardware links. Clear array to indicate
732 	 * CPU DAIs for this group have not been generated.
733 	 */
734 	for (i = 0; i < SDW_MAX_GROUPS; i++)
735 		group_generated[i] = false;
736 
737 	/* generate DAI links by each sdw link */
738 	for (; adr_link->num_adr; adr_link++) {
739 		const struct snd_soc_acpi_endpoint *endpoint;
740 
741 		endpoint = adr_link->adr_d->endpoints;
742 		if (endpoint->aggregated && !endpoint->group_id) {
743 			dev_err(dev, "invalid group id on link %x",
744 				adr_link->mask);
745 			continue;
746 		}
747 
748 		/* this group has been generated */
749 		if (endpoint->aggregated &&
750 		    group_generated[endpoint->group_id])
751 			continue;
752 
753 		ret = create_sdw_dailink(dev, &be_id, links, sdw_be_num,
754 					 sdw_cpu_dai_num, cpus, adr_link,
755 					 &cpu_id, group_generated);
756 		if (ret < 0) {
757 			dev_err(dev, "failed to create dai link %d", be_id);
758 			return -ENOMEM;
759 		}
760 	}
761 
762 	/* non-sdw DAI follows sdw DAI */
763 	link_id = be_id;
764 
765 	/* get BE ID for non-sdw DAI */
766 	be_id = get_next_be_id(links, be_id);
767 
768 SSP:
769 	/* SSP */
770 	if (!ssp_num)
771 		goto DMIC;
772 
773 	for (i = 0, j = 0; ssp_mask; i++, ssp_mask >>= 1) {
774 		struct sof_sdw_codec_info *info;
775 		int playback, capture;
776 		char *codec_name;
777 
778 		if (!(ssp_mask & 0x1))
779 			continue;
780 
781 		name = devm_kasprintf(dev, GFP_KERNEL,
782 				      "SSP%d-Codec", i);
783 		if (!name)
784 			return -ENOMEM;
785 
786 		cpu_name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d Pin", i);
787 		if (!cpu_name)
788 			return -ENOMEM;
789 
790 		ssp_components = devm_kzalloc(dev, sizeof(*ssp_components),
791 					      GFP_KERNEL);
792 		if (!ssp_components)
793 			return -ENOMEM;
794 
795 		info = &codec_info_list[ssp_codec_index];
796 		codec_name = devm_kasprintf(dev, GFP_KERNEL, "i2c-%s:0%d",
797 					    info->acpi_id, j++);
798 		if (!codec_name)
799 			return -ENOMEM;
800 
801 		ssp_components->name = codec_name;
802 		ssp_components->dai_name = info->dai_name;
803 		cpus[cpu_id].dai_name = cpu_name;
804 
805 		playback = info->direction[SNDRV_PCM_STREAM_PLAYBACK];
806 		capture = info->direction[SNDRV_PCM_STREAM_CAPTURE];
807 		init_dai_link(links + link_id, be_id, name,
808 			      playback, capture,
809 			      cpus + cpu_id, 1,
810 			      ssp_components, 1,
811 			      NULL, info->ops);
812 
813 		ret = info->init(NULL, links + link_id, info, 0);
814 		if (ret < 0)
815 			return ret;
816 
817 		INC_ID(be_id, cpu_id, link_id);
818 	}
819 
820 DMIC:
821 	/* dmic */
822 	if (dmic_num > 0) {
823 		cpus[cpu_id].dai_name = "DMIC01 Pin";
824 		init_dai_link(links + link_id, be_id, "dmic01",
825 			      0, 1, // DMIC only supports capture
826 			      cpus + cpu_id, 1,
827 			      dmic_component, 1,
828 			      sof_sdw_dmic_init, NULL);
829 		INC_ID(be_id, cpu_id, link_id);
830 
831 		cpus[cpu_id].dai_name = "DMIC16k Pin";
832 		init_dai_link(links + link_id, be_id, "dmic16k",
833 			      0, 1, // DMIC only supports capture
834 			      cpus + cpu_id, 1,
835 			      dmic_component, 1,
836 			      /* don't call sof_sdw_dmic_init() twice */
837 			      NULL, NULL);
838 		INC_ID(be_id, cpu_id, link_id);
839 	}
840 
841 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC)
842 	/* HDMI */
843 	if (hdmi_num > 0) {
844 		idisp_components = devm_kcalloc(dev, hdmi_num,
845 						sizeof(*idisp_components),
846 						GFP_KERNEL);
847 		if (!idisp_components)
848 			return -ENOMEM;
849 	}
850 
851 	for (i = 0; i < hdmi_num; i++) {
852 		name = devm_kasprintf(dev, GFP_KERNEL,
853 				      "iDisp%d", i + 1);
854 		if (!name)
855 			return -ENOMEM;
856 
857 		idisp_components[i].name = "ehdaudio0D2";
858 		idisp_components[i].dai_name = devm_kasprintf(dev,
859 							      GFP_KERNEL,
860 							      "intel-hdmi-hifi%d",
861 							      i + 1);
862 		if (!idisp_components[i].dai_name)
863 			return -ENOMEM;
864 
865 		cpu_name = devm_kasprintf(dev, GFP_KERNEL,
866 					  "iDisp%d Pin", i + 1);
867 		if (!cpu_name)
868 			return -ENOMEM;
869 
870 		cpus[cpu_id].dai_name = cpu_name;
871 		init_dai_link(links + link_id, be_id, name,
872 			      1, 0, // HDMI only supports playback
873 			      cpus + cpu_id, 1,
874 			      idisp_components + i, 1,
875 			      sof_sdw_hdmi_init, NULL);
876 		INC_ID(be_id, cpu_id, link_id);
877 	}
878 #endif
879 
880 	card->dai_link = links;
881 	card->num_links = num_links;
882 
883 	return 0;
884 }
885 
886 /* SoC card */
887 static const char sdw_card_long_name[] = "Intel Soundwire SOF";
888 
889 static struct snd_soc_card card_sof_sdw = {
890 	.name = "soundwire",
891 	.late_probe = sof_sdw_hdmi_card_late_probe,
892 	.codec_conf = codec_conf,
893 	.num_configs = ARRAY_SIZE(codec_conf),
894 };
895 
896 static int mc_probe(struct platform_device *pdev)
897 {
898 	struct snd_soc_card *card = &card_sof_sdw;
899 	struct snd_soc_acpi_mach *mach;
900 	struct mc_private *ctx;
901 	int ret;
902 
903 	dev_dbg(&pdev->dev, "Entry %s\n", __func__);
904 
905 	ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
906 	if (!ctx)
907 		return -ENOMEM;
908 
909 	dmi_check_system(sof_sdw_quirk_table);
910 
911 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC)
912 	INIT_LIST_HEAD(&ctx->hdmi_pcm_list);
913 #endif
914 
915 	card->dev = &pdev->dev;
916 
917 	mach = pdev->dev.platform_data;
918 	ret = sof_card_dai_links_create(&pdev->dev, mach,
919 					card);
920 	if (ret < 0)
921 		return ret;
922 
923 	ctx->common_hdmi_codec_drv = mach->mach_params.common_hdmi_codec_drv;
924 
925 	snd_soc_card_set_drvdata(card, ctx);
926 
927 	card->components = devm_kasprintf(card->dev, GFP_KERNEL,
928 					  "cfg-spk:%d",
929 					  (sof_sdw_quirk & SOF_SDW_FOUR_SPK) ? 4 : 2);
930 	if (!card->components)
931 		return -ENOMEM;
932 
933 	card->long_name = sdw_card_long_name;
934 
935 	/* Register the card */
936 	ret = devm_snd_soc_register_card(&pdev->dev, card);
937 	if (ret) {
938 		dev_err(card->dev, "snd_soc_register_card failed %d\n", ret);
939 		return ret;
940 	}
941 
942 	platform_set_drvdata(pdev, card);
943 
944 	return ret;
945 }
946 
947 static struct platform_driver sof_sdw_driver = {
948 	.driver = {
949 		.name = "sof_sdw",
950 		.pm = &snd_soc_pm_ops,
951 	},
952 	.probe = mc_probe,
953 };
954 
955 module_platform_driver(sof_sdw_driver);
956 
957 MODULE_DESCRIPTION("ASoC SoundWire Generic Machine driver");
958 MODULE_AUTHOR("Bard Liao <yung-chuan.liao@linux.intel.com>");
959 MODULE_AUTHOR("Rander Wang <rander.wang@linux.intel.com>");
960 MODULE_AUTHOR("Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>");
961 MODULE_LICENSE("GPL v2");
962 MODULE_ALIAS("platform:sof_sdw");
963