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