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