1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright (c) 2020 Intel Corporation
5 * sof_sdw - ASOC Machine driver for Intel SoundWire platforms
8 #include <linux/device.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"
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");
23 #define INC_ID(BE, CPU, LINK) do { (BE)++; (CPU)++; (LINK)++; } while (0)
25 static void log_quirks(struct device *dev)
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");
43 static int sof_sdw_quirk_cb(const struct dmi_system_id *id)
45 sof_sdw_quirk = (unsigned long)id->driver_data;
49 static const struct dmi_system_id sof_sdw_quirk_table[] = {
50 /* CometLake devices */
52 .callback = sof_sdw_quirk_cb,
54 DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
55 DMI_MATCH(DMI_PRODUCT_NAME, "CometLake Client"),
57 .driver_data = (void *)SOF_SDW_PCH_DMIC,
60 .callback = sof_sdw_quirk_cb,
62 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
63 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "09C6")
65 .driver_data = (void *)RT711_JD2,
68 /* early version of SKU 09C6 */
69 .callback = sof_sdw_quirk_cb,
71 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
72 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0983")
74 .driver_data = (void *)RT711_JD2,
77 .callback = sof_sdw_quirk_cb,
79 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
80 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "098F"),
82 .driver_data = (void *)(RT711_JD2 |
86 .callback = sof_sdw_quirk_cb,
88 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
89 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0990"),
91 .driver_data = (void *)(RT711_JD2 |
96 .callback = sof_sdw_quirk_cb,
98 DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
99 DMI_MATCH(DMI_PRODUCT_NAME, "Ice Lake Client"),
101 .driver_data = (void *)SOF_SDW_PCH_DMIC,
103 /* TigerLake devices */
105 .callback = sof_sdw_quirk_cb,
107 DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
108 DMI_MATCH(DMI_PRODUCT_NAME,
109 "Tiger Lake Client Platform"),
111 .driver_data = (void *)(SOF_SDW_TGL_HDMI |
114 SOF_SSP_PORT(SOF_I2S_SSP2)),
117 .callback = sof_sdw_quirk_cb,
119 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
120 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0A3E")
122 .driver_data = (void *)(SOF_SDW_TGL_HDMI |
126 /* another SKU of Dell Latitude 9520 */
127 .callback = sof_sdw_quirk_cb,
129 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
130 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0A3F")
132 .driver_data = (void *)(SOF_SDW_TGL_HDMI |
137 .callback = sof_sdw_quirk_cb,
139 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
140 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0A5D")
142 .driver_data = (void *)(SOF_SDW_TGL_HDMI |
147 .callback = sof_sdw_quirk_cb,
149 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
150 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0A5E")
152 .driver_data = (void *)(SOF_SDW_TGL_HDMI |
157 .callback = sof_sdw_quirk_cb,
159 DMI_MATCH(DMI_SYS_VENDOR, "Google"),
160 DMI_MATCH(DMI_PRODUCT_NAME, "Volteer"),
162 .driver_data = (void *)(SOF_SDW_TGL_HDMI |
165 SOF_BT_OFFLOAD_SSP(2) |
166 SOF_SSP_BT_OFFLOAD_PRESENT),
169 .callback = sof_sdw_quirk_cb,
171 DMI_MATCH(DMI_SYS_VENDOR, "Google"),
172 DMI_MATCH(DMI_PRODUCT_NAME, "Ripto"),
174 .driver_data = (void *)(SOF_SDW_TGL_HDMI |
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)
185 .callback = sof_sdw_quirk_cb,
187 DMI_MATCH(DMI_SYS_VENDOR, "HP"),
188 DMI_MATCH(DMI_PRODUCT_NAME, "HP Spectre x360 Conv"),
190 .driver_data = (void *)(SOF_SDW_TGL_HDMI |
195 /* NUC15 'Bishop County' LAPBC510 and LAPBC710 skews */
196 .callback = sof_sdw_quirk_cb,
198 DMI_MATCH(DMI_SYS_VENDOR, "Intel(R) Client Systems"),
199 DMI_MATCH(DMI_PRODUCT_NAME, "LAPBC"),
201 .driver_data = (void *)(SOF_SDW_TGL_HDMI |
206 /* NUC15 LAPBC710 skews */
207 .callback = sof_sdw_quirk_cb,
209 DMI_MATCH(DMI_BOARD_VENDOR, "Intel Corporation"),
210 DMI_MATCH(DMI_BOARD_NAME, "LAPBC710"),
212 .driver_data = (void *)(SOF_SDW_TGL_HDMI |
216 /* TigerLake-SDCA devices */
218 .callback = sof_sdw_quirk_cb,
220 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
221 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0A32")
223 .driver_data = (void *)(SOF_SDW_TGL_HDMI |
228 .callback = sof_sdw_quirk_cb,
230 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
231 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0A45")
233 .driver_data = (void *)(SOF_SDW_TGL_HDMI |
236 /* AlderLake devices */
238 .callback = sof_sdw_quirk_cb,
240 DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
241 DMI_MATCH(DMI_PRODUCT_NAME, "Alder Lake Client Platform"),
243 .driver_data = (void *)(RT711_JD2_100K |
245 SOF_BT_OFFLOAD_SSP(2) |
246 SOF_SSP_BT_OFFLOAD_PRESENT),
249 .callback = sof_sdw_quirk_cb,
251 DMI_MATCH(DMI_SYS_VENDOR, "Google"),
252 DMI_MATCH(DMI_PRODUCT_NAME, "Brya"),
254 .driver_data = (void *)(SOF_SDW_TGL_HDMI |
257 SOF_BT_OFFLOAD_SSP(2) |
258 SOF_SSP_BT_OFFLOAD_PRESENT),
261 .callback = sof_sdw_quirk_cb,
263 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
264 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0AF0")
266 .driver_data = (void *)(SOF_SDW_TGL_HDMI |
271 .callback = sof_sdw_quirk_cb,
273 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
274 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0AF3"),
277 .driver_data = (void *)(SOF_SDW_TGL_HDMI |
281 .callback = sof_sdw_quirk_cb,
283 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
284 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0B00")
286 .driver_data = (void *)(SOF_SDW_TGL_HDMI |
291 .callback = sof_sdw_quirk_cb,
293 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
294 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0B01")
296 .driver_data = (void *)(SOF_SDW_TGL_HDMI |
301 .callback = sof_sdw_quirk_cb,
303 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
304 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0B11")
306 .driver_data = (void *)(SOF_SDW_TGL_HDMI |
311 .callback = sof_sdw_quirk_cb,
313 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
314 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0B12")
316 .driver_data = (void *)(SOF_SDW_TGL_HDMI |
321 .callback = sof_sdw_quirk_cb,
323 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
324 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0B13"),
327 .driver_data = (void *)SOF_SDW_TGL_HDMI,
330 .callback = sof_sdw_quirk_cb,
332 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
333 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0B29"),
335 .driver_data = (void *)(SOF_SDW_TGL_HDMI |
340 .callback = sof_sdw_quirk_cb,
342 DMI_MATCH(DMI_SYS_VENDOR, "HP"),
343 DMI_MATCH(DMI_PRODUCT_NAME, "OMEN by HP Gaming Laptop 16-k0xxx"),
345 .driver_data = (void *)(SOF_SDW_TGL_HDMI |
348 /* MeteorLake devices */
350 .callback = sof_sdw_quirk_cb,
352 DMI_MATCH(DMI_PRODUCT_FAMILY, "Intel_mtlrvp"),
354 .driver_data = (void *)(RT711_JD1 | SOF_SDW_TGL_HDMI),
359 static struct snd_soc_dai_link_component dmic_component[] = {
361 .name = "dmic-codec",
362 .dai_name = "dmic-hifi",
366 static struct snd_soc_dai_link_component platform_component[] = {
368 /* name might be overridden during probe */
369 .name = "0000:00:1f.3"
373 /* these wrappers are only needed to avoid typecast compilation errors */
374 int sdw_startup(struct snd_pcm_substream *substream)
376 return sdw_startup_stream(substream);
379 int sdw_prepare(struct snd_pcm_substream *substream)
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;
385 /* Find stream from first CPU DAI */
386 dai = asoc_rtd_to_cpu(rtd, 0);
388 sdw_stream = snd_soc_dai_get_stream(dai, substream->stream);
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);
395 return sdw_prepare_stream(sdw_stream);
398 int sdw_trigger(struct snd_pcm_substream *substream, int cmd)
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;
405 /* Find stream from first CPU DAI */
406 dai = asoc_rtd_to_cpu(rtd, 0);
408 sdw_stream = snd_soc_dai_get_stream(dai, substream->stream);
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);
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);
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);
433 dev_err(rtd->dev, "%s trigger %d failed: %d", __func__, cmd, ret);
438 int sdw_hw_free(struct snd_pcm_substream *substream)
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;
444 /* Find stream from first CPU DAI */
445 dai = asoc_rtd_to_cpu(rtd, 0);
447 sdw_stream = snd_soc_dai_get_stream(dai, substream->stream);
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);
454 return sdw_deprepare_stream(sdw_stream);
457 void sdw_shutdown(struct snd_pcm_substream *substream)
459 sdw_shutdown_stream(substream);
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,
470 static struct sof_sdw_codec_info codec_info_list[] = {
473 .direction = {true, true},
474 .dai_name = "rt700-aif1",
475 .init = sof_sdw_rt700_init,
476 .codec_type = SOF_SDW_CODEC_TYPE_JACK,
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,
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,
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,
507 .direction = {true, true},
508 .dai_name = "rt1316-aif",
509 .init = sof_sdw_rt1316_init,
510 .codec_type = SOF_SDW_CODEC_TYPE_AMP,
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,
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,
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,
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,
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,
558 .direction = {true, true},
559 .dai_name = "rt5682-sdw",
560 .init = sof_sdw_rt5682_init,
561 .codec_type = SOF_SDW_CODEC_TYPE_JACK,
564 .part_id = 0xaaaa, /* generic codec mockup */
566 .direction = {true, true},
567 .dai_name = "sdw-mockup-aif1",
569 .codec_type = SOF_SDW_CODEC_TYPE_JACK,
572 .part_id = 0xaa55, /* headset codec mockup */
574 .direction = {true, true},
575 .dai_name = "sdw-mockup-aif1",
577 .codec_type = SOF_SDW_CODEC_TYPE_JACK,
580 .part_id = 0x55aa, /* amplifier mockup */
582 .direction = {true, false},
583 .dai_name = "sdw-mockup-aif1",
585 .codec_type = SOF_SDW_CODEC_TYPE_AMP,
590 .direction = {false, true},
591 .dai_name = "sdw-mockup-aif1",
592 .codec_type = SOF_SDW_CODEC_TYPE_MIC,
596 static inline int find_codec_info_part(u64 adr)
598 unsigned int part_id, sdw_version;
601 part_id = SDW_PART_ID(adr);
602 sdw_version = SDW_VERSION(adr);
603 for (i = 0; i < ARRAY_SIZE(codec_info_list); i++)
605 * A codec info is for all sdw version with the part id if
606 * version_id is not specified in the codec info.
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))
617 static inline int find_codec_info_acpi(const u8 *acpi_id)
624 for (i = 0; i < ARRAY_SIZE(codec_info_list); i++)
625 if (!memcmp(codec_info_list[i].acpi_id, acpi_id,
629 if (i == ARRAY_SIZE(codec_info_list))
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.
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)
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];
649 no_aggregation = sof_sdw_quirk & SOF_SDW_NO_AGGREGATION;
650 *sdw_cpu_dai_num = 0;
656 for (i = 0; i < SDW_MAX_GROUPS; i++)
657 group_visited[i] = false;
659 for (link = links; link->num_adr; link++) {
660 const struct snd_soc_acpi_endpoint *endpoint;
665 adr = link->adr_d->adr;
666 codec_index = find_codec_info_part(adr);
670 if (codec_info_list[codec_index].codec_type < _codec_type)
672 "Unexpected address table ordering. Expected order: jack -> amp -> mic\n");
674 _codec_type = codec_info_list[codec_index].codec_type;
676 endpoint = link->adr_d->endpoints;
678 /* count DAI number for playback and capture */
679 for_each_pcm_streams(stream) {
680 if (!codec_info_list[codec_index].direction[stream])
683 (*sdw_cpu_dai_num)++;
685 /* count BE for each non-aggregated slave or group */
686 if (!endpoint->aggregated || no_aggregation ||
687 !group_visited[endpoint->group_id])
691 if (endpoint->aggregated)
692 group_visited[endpoint->group_id] = true;
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)
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;
721 static bool is_unique_device(const struct snd_soc_acpi_link_adr *link,
722 unsigned int sdw_version,
724 unsigned int part_id,
725 unsigned int class_id,
731 for (i = 0; i < link->num_adr; i++) {
732 unsigned int sdw1_version, mfg1_id, part1_id, class1_id;
736 if (i == index_in_link)
739 adr = link->adr_d[i].adr;
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);
746 if (sdw_version == sdw1_version &&
748 part_id == part1_id &&
749 class_id == class1_id)
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,
760 struct snd_soc_codec_conf *codec_conf,
762 int *codec_conf_index)
767 if (*codec_conf_index + link->num_adr > codec_count) {
768 dev_err(dev, "codec_conf: out-of-bounds access requested\n");
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;
779 adr = link->adr_d[i].adr;
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);
788 comp_index = i + offset;
789 if (is_unique_device(link, sdw_version, mfg_id, part_id,
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,
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);
804 if (!codec[comp_index].name)
807 codec_index = find_codec_info_part(adr);
811 codec[comp_index].dai_name =
812 codec_info_list[codec_index].dai_name;
814 codec_conf[*codec_conf_index].dlc = codec[comp_index];
815 codec_conf[*codec_conf_index].name_prefix = link->adr_d[i].name_prefix;
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)
832 * Initialize the codec. If codec is part of an aggregated
833 * group (group_id>0), initialize all codecs belonging to
836 for (i = 0; i < link->num_adr; i++) {
839 codec_index = find_codec_info_part(link->adr_d[i].adr);
843 /* The group_id is > 0 iff the codec is aggregated */
844 if (link->adr_d[i].endpoints->group_id != group_id)
846 if (codec_info_list[codec_index].init)
847 codec_info_list[codec_index].init(card,
850 &codec_info_list[codec_index],
854 } while (link->mask && group_id);
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.
864 * one slave on one sdw link with aggregated = 0
865 * one sdw BE DAI <---> one-cpu DAI <---> one-codec DAI
867 * two or more slaves on one sdw link with aggregated = 0
868 * one sdw BE DAI <---> one-cpu DAI <---> multi-codec DAIs
870 * multiple links with multiple slaves with aggregated = 1
871 * one sdw BE DAI <---> 1 .. N CPU DAIs <----> 1 .. N codec DAIs
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)
878 const struct snd_soc_acpi_adr_device *adr_d;
879 const struct snd_soc_acpi_link_adr *adr_next;
883 no_aggregation = sof_sdw_quirk & SOF_SDW_NO_AGGREGATION;
884 *codec_num = adr_link->num_adr;
885 adr_d = adr_link->adr_d;
887 /* make sure the link mask has a single bit set */
888 if (!is_power_of_2(adr_link->mask))
891 cpu_dai_id[index++] = ffs(adr_link->mask) - 1;
892 if (!adr_d->endpoints->aggregated || no_aggregation) {
898 *group_id = adr_d->endpoints->group_id;
900 /* gather other link ID of slaves in the same group */
901 for (adr_next = adr_link + 1; adr_next && adr_next->num_adr;
903 const struct snd_soc_acpi_endpoint *endpoint;
905 endpoint = adr_next->adr_d->endpoints;
906 if (!endpoint->aggregated ||
907 endpoint->group_id != *group_id)
910 /* make sure the link mask has a single bit set */
911 if (!is_power_of_2(adr_next->mask))
914 if (index >= SDW_MAX_CPU_DAIS) {
915 dev_err(dev, " cpu_dai_id array overflows");
919 cpu_dai_id[index++] = ffs(adr_next->mask) - 1;
920 *codec_num += adr_next->num_adr;
924 * indicate CPU DAIs for this group have been generated
925 * to avoid generating CPU DAIs for this group again.
927 group_generated[*group_id] = true;
928 *cpu_dai_num = index;
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)
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;
958 ret = get_slave_info(link, dev, cpu_dai_id, &cpu_dai_num, &codec_num,
959 &group_id, group_generated);
963 codecs = devm_kcalloc(dev, codec_num, sizeof(*codecs), GFP_KERNEL);
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;
972 endpoints = link_next->adr_d->endpoints;
973 if (group_id && (!endpoints->aggregated ||
974 endpoints->group_id != group_id))
977 /* skip the link excluded by this processed group */
978 if (cpu_dai_id[i] != ffs(link_next->mask) - 1)
981 ret = create_codec_dai_name(dev, link_next, codecs, codec_idx,
982 codec_conf, codec_count, codec_conf_index);
986 /* check next link to create codec dai in the processed group */
988 codec_idx += link_next->num_adr;
991 /* find codec info to create BE DAI */
992 codec_index = find_codec_info_part(link->adr_d[0].adr);
996 if (codec_info_list[codec_index].ignore_pch_dmic)
997 *ignore_pch_dmic = true;
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;
1005 * DAI ID is fixed at SDW_DMIC_DAI_ID for MICs to
1006 * keep sdw DMIC and HDMI setting static in UCM
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;
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[] = {
1021 if (!codec_info_list[codec_index].direction[stream])
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]);
1031 * generate CPU DAI name base on the sdw link ID and
1032 * PIN ID with offset of 2 according to sdw dai driver.
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);
1041 if (cpu_dai_index >= sdw_cpu_dai_num) {
1042 dev_err(dev, "invalid cpu dai index %d",
1047 cpus[cpu_dai_index++].dai_name = cpu_name;
1051 * We create sdw dai links at first stage, so link index should
1052 * not be larger than sdw_be_num
1054 if (*link_index >= sdw_be_num) {
1055 dev_err(dev, "invalid dai link index %d", *link_index);
1059 if (*cpu_id >= sdw_cpu_dai_num) {
1060 dev_err(dev, " invalid cpu dai index %d", *cpu_id);
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,
1068 cpus + *cpu_id, cpu_dai_num,
1073 * SoundWire DAILINKs use 'stream' functions and Bank Switch operations
1074 * based on wait_for_completion(), tag them as 'nonatomic'.
1076 dai_links[*link_index].nonatomic = true;
1078 ret = set_codec_init_func(card, link, dai_links + (*link_index)++,
1079 playback, group_id);
1081 dev_err(dev, "failed to init codec %d", codec_index);
1085 *cpu_id += cpu_dai_num;
1092 #define IDISP_CODEC_MASK 0x4
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)
1099 const struct snd_soc_acpi_link_adr *adr_link;
1100 struct snd_soc_codec_conf *c_conf;
1104 adr_link = mach_params->links;
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);
1117 num_codecs += adr_link->num_adr;
1120 c_conf = devm_kzalloc(dev, num_codecs * sizeof(*c_conf), GFP_KERNEL);
1124 *codec_conf = c_conf;
1125 *codec_conf_count = num_codecs;
1130 static int sof_card_dai_links_create(struct device *dev,
1131 struct snd_soc_acpi_mach *mach,
1132 struct snd_soc_card *card)
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;
1157 mach_params = &mach->mach_params;
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);
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;
1168 if (mach_params->codec_mask & IDISP_CODEC_MASK) {
1169 ctx->idisp_codec = true;
1171 if (sof_sdw_quirk & SOF_SDW_TGL_HDMI)
1172 hdmi_num = SOF_TGL_HDMI_COUNT;
1174 hdmi_num = SOF_PRE_TGL_HDMI_COUNT;
1177 ssp_mask = SOF_SSP_GET_PORT(sof_sdw_quirk);
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.
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;
1188 ret = get_sdw_dailink_info(dev, mach_params->links,
1189 &sdw_be_num, &sdw_cpu_dai_num);
1191 dev_err(dev, "failed to get sdw link info %d", ret);
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;
1199 if (sof_sdw_quirk & SOF_SSP_BT_OFFLOAD_PRESENT)
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);
1205 /* allocate BE dailinks */
1206 num_links = comp_num + sdw_be_num;
1207 links = devm_kcalloc(dev, num_links, sizeof(*links), GFP_KERNEL);
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),
1214 if (!links || !cpus)
1221 adr_link = mach_params->links;
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.
1230 for (i = 0; i < SDW_MAX_GROUPS; i++)
1231 group_generated[i] = false;
1233 /* generate DAI links by each sdw link */
1234 for (; adr_link->num_adr; adr_link++) {
1235 const struct snd_soc_acpi_endpoint *endpoint;
1237 endpoint = adr_link->adr_d->endpoints;
1238 if (endpoint->aggregated && !endpoint->group_id) {
1239 dev_err(dev, "invalid group id on link %x",
1244 /* this group has been generated */
1245 if (endpoint->aggregated &&
1246 group_generated[endpoint->group_id])
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,
1256 dev_err(dev, "failed to create dai link %d", link_index);
1266 for (i = 0, j = 0; ssp_mask; i++, ssp_mask >>= 1) {
1267 struct sof_sdw_codec_info *info;
1268 int playback, capture;
1271 if (!(ssp_mask & 0x1))
1274 name = devm_kasprintf(dev, GFP_KERNEL,
1279 cpu_name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d Pin", i);
1283 ssp_components = devm_kzalloc(dev, sizeof(*ssp_components),
1285 if (!ssp_components)
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++);
1294 ssp_components->name = codec_name;
1295 ssp_components->dai_name = info->dai_name;
1296 cpus[cpu_id].dai_name = cpu_name;
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,
1306 ret = info->init(card, NULL, links + link_index, info, 0);
1310 INC_ID(be_id, cpu_id, link_index);
1316 if (ignore_pch_dmic) {
1317 dev_warn(dev, "Ignoring PCH DMIC\n");
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
1325 sof_sdw_dmic_init, NULL);
1326 INC_ID(be_id, cpu_id, link_index);
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
1333 /* don't call sof_sdw_dmic_init() twice */
1335 INC_ID(be_id, cpu_id, link_index);
1341 idisp_components = devm_kcalloc(dev, hdmi_num,
1342 sizeof(*idisp_components),
1344 if (!idisp_components)
1348 for (i = 0; i < hdmi_num; i++) {
1349 name = devm_kasprintf(dev, GFP_KERNEL,
1354 if (ctx->idisp_codec) {
1355 idisp_components[i].name = "ehdaudio0D2";
1356 idisp_components[i].dai_name = devm_kasprintf(dev,
1358 "intel-hdmi-hifi%d",
1360 if (!idisp_components[i].dai_name)
1363 idisp_components[i].name = "snd-soc-dummy";
1364 idisp_components[i].dai_name = "snd-soc-dummy-dai";
1367 cpu_name = devm_kasprintf(dev, GFP_KERNEL,
1368 "iDisp%d Pin", i + 1);
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
1376 idisp_components + i, 1,
1377 sof_sdw_hdmi_init, NULL);
1378 INC_ID(be_id, cpu_id, link_index);
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;
1385 name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-BT", port);
1389 ssp_components = devm_kzalloc(dev, sizeof(*ssp_components),
1391 if (!ssp_components)
1394 ssp_components->name = "snd-soc-dummy";
1395 ssp_components->dai_name = "snd-soc-dummy-dai";
1397 cpu_name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d Pin", port);
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);
1406 card->dai_link = links;
1407 card->num_links = num_links;
1409 card->codec_conf = codec_conf;
1410 card->num_configs = codec_conf_count;
1415 static int sof_sdw_card_late_probe(struct snd_soc_card *card)
1417 struct mc_private *ctx = snd_soc_card_get_drvdata(card);
1421 for (i = 0; i < ARRAY_SIZE(codec_info_list); i++) {
1422 if (!codec_info_list[i].late_probe)
1425 ret = codec_info_list[i].codec_card_late_probe(card);
1430 if (ctx->idisp_codec)
1431 ret = sof_sdw_hdmi_card_late_probe(card);
1437 static const char sdw_card_long_name[] = "Intel Soundwire SOF";
1439 static struct snd_soc_card card_sof_sdw = {
1440 .name = "soundwire",
1441 .owner = THIS_MODULE,
1442 .late_probe = sof_sdw_card_late_probe,
1445 static void mc_dailink_exit_loop(struct snd_soc_card *card)
1447 struct snd_soc_dai_link *link;
1451 for (i = 0; i < ARRAY_SIZE(codec_info_list); i++) {
1452 if (!codec_info_list[i].exit)
1455 * We don't need to call .exit function if there is no matched
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);
1464 "codec exit failed %d\n",
1472 static int mc_probe(struct platform_device *pdev)
1474 struct snd_soc_card *card = &card_sof_sdw;
1475 struct snd_soc_acpi_mach *mach;
1476 struct mc_private *ctx;
1480 dev_dbg(&pdev->dev, "Entry\n");
1482 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
1486 dmi_check_system(sof_sdw_quirk_table);
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;
1493 log_quirks(&pdev->dev);
1495 INIT_LIST_HEAD(&ctx->hdmi_pcm_list);
1497 card->dev = &pdev->dev;
1498 snd_soc_card_set_drvdata(card, ctx);
1500 mach = pdev->dev.platform_data;
1501 ret = sof_card_dai_links_create(&pdev->dev, mach,
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
1511 for (i = 0; i < ARRAY_SIZE(codec_info_list); i++)
1512 amp_num += codec_info_list[i].amp_num;
1514 card->components = devm_kasprintf(card->dev, GFP_KERNEL,
1515 "cfg-spk:%d cfg-amp:%d",
1516 (sof_sdw_quirk & SOF_SDW_FOUR_SPK)
1518 if (!card->components)
1521 if (mach->mach_params.dmic_num) {
1522 card->components = devm_kasprintf(card->dev, GFP_KERNEL,
1523 "%s mic:dmic cfg-mics:%d",
1525 mach->mach_params.dmic_num);
1526 if (!card->components)
1530 card->long_name = sdw_card_long_name;
1532 /* Register the card */
1533 ret = devm_snd_soc_register_card(&pdev->dev, card);
1535 dev_err(card->dev, "snd_soc_register_card failed %d\n", ret);
1536 mc_dailink_exit_loop(card);
1540 platform_set_drvdata(pdev, card);
1545 static int mc_remove(struct platform_device *pdev)
1547 struct snd_soc_card *card = platform_get_drvdata(pdev);
1549 mc_dailink_exit_loop(card);
1554 static struct platform_driver sof_sdw_driver = {
1557 .pm = &snd_soc_pm_ops,
1560 .remove = mc_remove,
1563 module_platform_driver(sof_sdw_driver);
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);