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