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