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 |
205 /* TigerLake-SDCA devices */
207 .callback = sof_sdw_quirk_cb,
209 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
210 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0A32")
212 .driver_data = (void *)(SOF_SDW_TGL_HDMI |
217 .callback = sof_sdw_quirk_cb,
219 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
220 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0A45")
222 .driver_data = (void *)(SOF_SDW_TGL_HDMI |
225 /* AlderLake devices */
227 .callback = sof_sdw_quirk_cb,
229 DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
230 DMI_MATCH(DMI_PRODUCT_NAME, "Alder Lake Client Platform"),
232 .driver_data = (void *)(RT711_JD2_100K |
234 SOF_BT_OFFLOAD_SSP(2) |
235 SOF_SSP_BT_OFFLOAD_PRESENT),
238 .callback = sof_sdw_quirk_cb,
240 DMI_MATCH(DMI_SYS_VENDOR, "Google"),
241 DMI_MATCH(DMI_PRODUCT_NAME, "Brya"),
243 .driver_data = (void *)(SOF_SDW_TGL_HDMI |
246 SOF_BT_OFFLOAD_SSP(2) |
247 SOF_SSP_BT_OFFLOAD_PRESENT),
250 .callback = sof_sdw_quirk_cb,
252 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
253 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0AF0")
255 .driver_data = (void *)(SOF_SDW_TGL_HDMI |
260 .callback = sof_sdw_quirk_cb,
262 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
263 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0AF3"),
266 .driver_data = (void *)(SOF_SDW_TGL_HDMI |
270 .callback = sof_sdw_quirk_cb,
272 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
273 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0AFF")
275 .driver_data = (void *)(SOF_SDW_TGL_HDMI |
280 .callback = sof_sdw_quirk_cb,
282 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
283 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0B00")
285 .driver_data = (void *)(SOF_SDW_TGL_HDMI |
290 .callback = sof_sdw_quirk_cb,
292 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
293 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0B01")
295 .driver_data = (void *)(SOF_SDW_TGL_HDMI |
300 .callback = sof_sdw_quirk_cb,
302 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
303 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0B11")
305 .driver_data = (void *)(SOF_SDW_TGL_HDMI |
310 .callback = sof_sdw_quirk_cb,
312 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
313 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0B12")
315 .driver_data = (void *)(SOF_SDW_TGL_HDMI |
320 .callback = sof_sdw_quirk_cb,
322 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
323 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0B13"),
326 .driver_data = (void *)SOF_SDW_TGL_HDMI,
329 .callback = sof_sdw_quirk_cb,
331 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
332 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0B29"),
334 .driver_data = (void *)(SOF_SDW_TGL_HDMI |
339 .callback = sof_sdw_quirk_cb,
341 DMI_MATCH(DMI_SYS_VENDOR, "HP"),
342 DMI_MATCH(DMI_PRODUCT_NAME, "OMEN by HP Gaming Laptop 16-k0xxx"),
344 .driver_data = (void *)(SOF_SDW_TGL_HDMI |
347 /* MeteorLake devices */
349 .callback = sof_sdw_quirk_cb,
351 DMI_MATCH(DMI_PRODUCT_FAMILY, "Intel_mtlrvp"),
353 .driver_data = (void *)(RT711_JD1 | SOF_SDW_TGL_HDMI),
358 static struct snd_soc_dai_link_component dmic_component[] = {
360 .name = "dmic-codec",
361 .dai_name = "dmic-hifi",
365 static struct snd_soc_dai_link_component platform_component[] = {
367 /* name might be overridden during probe */
368 .name = "0000:00:1f.3"
372 /* these wrappers are only needed to avoid typecast compilation errors */
373 int sdw_startup(struct snd_pcm_substream *substream)
375 return sdw_startup_stream(substream);
378 int sdw_prepare(struct snd_pcm_substream *substream)
380 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
381 struct sdw_stream_runtime *sdw_stream;
382 struct snd_soc_dai *dai;
384 /* Find stream from first CPU DAI */
385 dai = asoc_rtd_to_cpu(rtd, 0);
387 sdw_stream = snd_soc_dai_get_stream(dai, substream->stream);
389 if (IS_ERR(sdw_stream)) {
390 dev_err(rtd->dev, "no stream found for DAI %s", dai->name);
391 return PTR_ERR(sdw_stream);
394 return sdw_prepare_stream(sdw_stream);
397 int sdw_trigger(struct snd_pcm_substream *substream, int cmd)
399 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
400 struct sdw_stream_runtime *sdw_stream;
401 struct snd_soc_dai *dai;
404 /* Find stream from first CPU DAI */
405 dai = asoc_rtd_to_cpu(rtd, 0);
407 sdw_stream = snd_soc_dai_get_stream(dai, substream->stream);
409 if (IS_ERR(sdw_stream)) {
410 dev_err(rtd->dev, "no stream found for DAI %s", dai->name);
411 return PTR_ERR(sdw_stream);
415 case SNDRV_PCM_TRIGGER_START:
416 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
417 case SNDRV_PCM_TRIGGER_RESUME:
418 ret = sdw_enable_stream(sdw_stream);
421 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
422 case SNDRV_PCM_TRIGGER_SUSPEND:
423 case SNDRV_PCM_TRIGGER_STOP:
424 ret = sdw_disable_stream(sdw_stream);
432 dev_err(rtd->dev, "%s trigger %d failed: %d", __func__, cmd, ret);
437 int sdw_hw_free(struct snd_pcm_substream *substream)
439 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
440 struct sdw_stream_runtime *sdw_stream;
441 struct snd_soc_dai *dai;
443 /* Find stream from first CPU DAI */
444 dai = asoc_rtd_to_cpu(rtd, 0);
446 sdw_stream = snd_soc_dai_get_stream(dai, substream->stream);
448 if (IS_ERR(sdw_stream)) {
449 dev_err(rtd->dev, "no stream found for DAI %s", dai->name);
450 return PTR_ERR(sdw_stream);
453 return sdw_deprepare_stream(sdw_stream);
456 void sdw_shutdown(struct snd_pcm_substream *substream)
458 sdw_shutdown_stream(substream);
461 static const struct snd_soc_ops sdw_ops = {
462 .startup = sdw_startup,
463 .prepare = sdw_prepare,
464 .trigger = sdw_trigger,
465 .hw_free = sdw_hw_free,
466 .shutdown = sdw_shutdown,
469 static struct sof_sdw_codec_info codec_info_list[] = {
472 .direction = {true, true},
473 .dai_name = "rt700-aif1",
474 .init = sof_sdw_rt700_init,
475 .codec_type = SOF_SDW_CODEC_TYPE_JACK,
480 .direction = {true, true},
481 .dai_name = "rt711-sdca-aif1",
482 .init = sof_sdw_rt711_sdca_init,
483 .exit = sof_sdw_rt711_sdca_exit,
484 .codec_type = SOF_SDW_CODEC_TYPE_JACK,
489 .direction = {true, true},
490 .dai_name = "rt711-aif1",
491 .init = sof_sdw_rt711_init,
492 .exit = sof_sdw_rt711_exit,
493 .codec_type = SOF_SDW_CODEC_TYPE_JACK,
497 .acpi_id = "10EC1308",
498 .direction = {true, false},
499 .dai_name = "rt1308-aif",
500 .ops = &sof_sdw_rt1308_i2s_ops,
501 .init = sof_sdw_rt1308_init,
502 .codec_type = SOF_SDW_CODEC_TYPE_AMP,
506 .direction = {true, true},
507 .dai_name = "rt1316-aif",
508 .init = sof_sdw_rt1316_init,
509 .codec_type = SOF_SDW_CODEC_TYPE_AMP,
514 .direction = {false, true},
515 .ignore_pch_dmic = true,
516 .dai_name = "rt715-aif2",
517 .init = sof_sdw_rt715_sdca_init,
518 .codec_type = SOF_SDW_CODEC_TYPE_MIC,
523 .direction = {false, true},
524 .ignore_pch_dmic = true,
525 .dai_name = "rt715-aif2",
526 .init = sof_sdw_rt715_sdca_init,
527 .codec_type = SOF_SDW_CODEC_TYPE_MIC,
532 .direction = {false, true},
533 .ignore_pch_dmic = true,
534 .dai_name = "rt715-aif2",
535 .init = sof_sdw_rt715_init,
536 .codec_type = SOF_SDW_CODEC_TYPE_MIC,
541 .direction = {false, true},
542 .ignore_pch_dmic = true,
543 .dai_name = "rt715-aif2",
544 .init = sof_sdw_rt715_init,
545 .codec_type = SOF_SDW_CODEC_TYPE_MIC,
549 .direction = {true, true},
550 .dai_name = "max98373-aif1",
551 .init = sof_sdw_mx8373_init,
552 .codec_card_late_probe = sof_sdw_mx8373_late_probe,
553 .codec_type = SOF_SDW_CODEC_TYPE_AMP,
557 .direction = {true, true},
558 .dai_name = "rt5682-sdw",
559 .init = sof_sdw_rt5682_init,
560 .codec_type = SOF_SDW_CODEC_TYPE_JACK,
563 .part_id = 0xaaaa, /* generic codec mockup */
565 .direction = {true, true},
566 .dai_name = "sdw-mockup-aif1",
568 .codec_type = SOF_SDW_CODEC_TYPE_JACK,
571 .part_id = 0xaa55, /* headset codec mockup */
573 .direction = {true, true},
574 .dai_name = "sdw-mockup-aif1",
576 .codec_type = SOF_SDW_CODEC_TYPE_JACK,
579 .part_id = 0x55aa, /* amplifier mockup */
581 .direction = {true, false},
582 .dai_name = "sdw-mockup-aif1",
584 .codec_type = SOF_SDW_CODEC_TYPE_AMP,
589 .direction = {false, true},
590 .dai_name = "sdw-mockup-aif1",
591 .codec_type = SOF_SDW_CODEC_TYPE_MIC,
595 static inline int find_codec_info_part(u64 adr)
597 unsigned int part_id, sdw_version;
600 part_id = SDW_PART_ID(adr);
601 sdw_version = SDW_VERSION(adr);
602 for (i = 0; i < ARRAY_SIZE(codec_info_list); i++)
604 * A codec info is for all sdw version with the part id if
605 * version_id is not specified in the codec info.
607 if (part_id == codec_info_list[i].part_id &&
608 (!codec_info_list[i].version_id ||
609 sdw_version == codec_info_list[i].version_id))
616 static inline int find_codec_info_acpi(const u8 *acpi_id)
623 for (i = 0; i < ARRAY_SIZE(codec_info_list); i++)
624 if (!memcmp(codec_info_list[i].acpi_id, acpi_id,
628 if (i == ARRAY_SIZE(codec_info_list))
635 * get BE dailink number and CPU DAI number based on sdw link adr.
636 * Since some sdw slaves may be aggregated, the CPU DAI number
637 * may be larger than the number of BE dailinks.
639 static int get_sdw_dailink_info(struct device *dev, const struct snd_soc_acpi_link_adr *links,
640 int *sdw_be_num, int *sdw_cpu_dai_num)
642 const struct snd_soc_acpi_link_adr *link;
643 int _codec_type = SOF_SDW_CODEC_TYPE_JACK;
644 bool group_visited[SDW_MAX_GROUPS];
648 no_aggregation = sof_sdw_quirk & SOF_SDW_NO_AGGREGATION;
649 *sdw_cpu_dai_num = 0;
655 for (i = 0; i < SDW_MAX_GROUPS; i++)
656 group_visited[i] = false;
658 for (link = links; link->num_adr; link++) {
659 const struct snd_soc_acpi_endpoint *endpoint;
664 adr = link->adr_d->adr;
665 codec_index = find_codec_info_part(adr);
669 if (codec_info_list[codec_index].codec_type < _codec_type)
671 "Unexpected address table ordering. Expected order: jack -> amp -> mic\n");
673 _codec_type = codec_info_list[codec_index].codec_type;
675 endpoint = link->adr_d->endpoints;
677 /* count DAI number for playback and capture */
678 for_each_pcm_streams(stream) {
679 if (!codec_info_list[codec_index].direction[stream])
682 (*sdw_cpu_dai_num)++;
684 /* count BE for each non-aggregated slave or group */
685 if (!endpoint->aggregated || no_aggregation ||
686 !group_visited[endpoint->group_id])
690 if (endpoint->aggregated)
691 group_visited[endpoint->group_id] = true;
697 static void init_dai_link(struct device *dev, struct snd_soc_dai_link *dai_links,
698 int be_id, char *name, int playback, int capture,
699 struct snd_soc_dai_link_component *cpus, int cpus_num,
700 struct snd_soc_dai_link_component *codecs, int codecs_num,
701 int (*init)(struct snd_soc_pcm_runtime *rtd),
702 const struct snd_soc_ops *ops)
704 dev_dbg(dev, "create dai link %s, id %d\n", name, be_id);
705 dai_links->id = be_id;
706 dai_links->name = name;
707 dai_links->platforms = platform_component;
708 dai_links->num_platforms = ARRAY_SIZE(platform_component);
709 dai_links->no_pcm = 1;
710 dai_links->cpus = cpus;
711 dai_links->num_cpus = cpus_num;
712 dai_links->codecs = codecs;
713 dai_links->num_codecs = codecs_num;
714 dai_links->dpcm_playback = playback;
715 dai_links->dpcm_capture = capture;
716 dai_links->init = init;
717 dai_links->ops = ops;
720 static bool is_unique_device(const struct snd_soc_acpi_link_adr *link,
721 unsigned int sdw_version,
723 unsigned int part_id,
724 unsigned int class_id,
730 for (i = 0; i < link->num_adr; i++) {
731 unsigned int sdw1_version, mfg1_id, part1_id, class1_id;
735 if (i == index_in_link)
738 adr = link->adr_d[i].adr;
740 sdw1_version = SDW_VERSION(adr);
741 mfg1_id = SDW_MFG_ID(adr);
742 part1_id = SDW_PART_ID(adr);
743 class1_id = SDW_CLASS_ID(adr);
745 if (sdw_version == sdw1_version &&
747 part_id == part1_id &&
748 class_id == class1_id)
755 static int create_codec_dai_name(struct device *dev,
756 const struct snd_soc_acpi_link_adr *link,
757 struct snd_soc_dai_link_component *codec,
759 struct snd_soc_codec_conf *codec_conf,
761 int *codec_conf_index)
766 if (*codec_conf_index + link->num_adr > codec_count) {
767 dev_err(dev, "codec_conf: out-of-bounds access requested\n");
771 for (i = 0; i < link->num_adr; i++) {
772 unsigned int sdw_version, unique_id, mfg_id;
773 unsigned int link_id, part_id, class_id;
774 int codec_index, comp_index;
778 adr = link->adr_d[i].adr;
780 sdw_version = SDW_VERSION(adr);
781 link_id = SDW_DISCO_LINK_ID(adr);
782 unique_id = SDW_UNIQUE_ID(adr);
783 mfg_id = SDW_MFG_ID(adr);
784 part_id = SDW_PART_ID(adr);
785 class_id = SDW_CLASS_ID(adr);
787 comp_index = i + offset;
788 if (is_unique_device(link, sdw_version, mfg_id, part_id,
790 codec_str = "sdw:%01x:%04x:%04x:%02x";
791 codec[comp_index].name =
792 devm_kasprintf(dev, GFP_KERNEL, codec_str,
793 link_id, mfg_id, part_id,
796 codec_str = "sdw:%01x:%04x:%04x:%02x:%01x";
797 codec[comp_index].name =
798 devm_kasprintf(dev, GFP_KERNEL, codec_str,
799 link_id, mfg_id, part_id,
800 class_id, unique_id);
803 if (!codec[comp_index].name)
806 codec_index = find_codec_info_part(adr);
810 codec[comp_index].dai_name =
811 codec_info_list[codec_index].dai_name;
813 codec_conf[*codec_conf_index].dlc = codec[comp_index];
814 codec_conf[*codec_conf_index].name_prefix = link->adr_d[i].name_prefix;
822 static int set_codec_init_func(struct snd_soc_card *card,
823 const struct snd_soc_acpi_link_adr *link,
824 struct snd_soc_dai_link *dai_links,
825 bool playback, int group_id)
831 * Initialize the codec. If codec is part of an aggregated
832 * group (group_id>0), initialize all codecs belonging to
835 for (i = 0; i < link->num_adr; i++) {
838 codec_index = find_codec_info_part(link->adr_d[i].adr);
842 /* The group_id is > 0 iff the codec is aggregated */
843 if (link->adr_d[i].endpoints->group_id != group_id)
845 if (codec_info_list[codec_index].init)
846 codec_info_list[codec_index].init(card,
849 &codec_info_list[codec_index],
853 } while (link->mask && group_id);
859 * check endpoint status in slaves and gather link ID for all slaves in
860 * the same group to generate different CPU DAI. Now only support
861 * one sdw link with all slaves set with only single group id.
863 * one slave on one sdw link with aggregated = 0
864 * one sdw BE DAI <---> one-cpu DAI <---> one-codec DAI
866 * two or more slaves on one sdw link with aggregated = 0
867 * one sdw BE DAI <---> one-cpu DAI <---> multi-codec DAIs
869 * multiple links with multiple slaves with aggregated = 1
870 * one sdw BE DAI <---> 1 .. N CPU DAIs <----> 1 .. N codec DAIs
872 static int get_slave_info(const struct snd_soc_acpi_link_adr *adr_link,
873 struct device *dev, int *cpu_dai_id, int *cpu_dai_num,
874 int *codec_num, unsigned int *group_id,
875 bool *group_generated)
877 const struct snd_soc_acpi_adr_device *adr_d;
878 const struct snd_soc_acpi_link_adr *adr_next;
882 no_aggregation = sof_sdw_quirk & SOF_SDW_NO_AGGREGATION;
883 *codec_num = adr_link->num_adr;
884 adr_d = adr_link->adr_d;
886 /* make sure the link mask has a single bit set */
887 if (!is_power_of_2(adr_link->mask))
890 cpu_dai_id[index++] = ffs(adr_link->mask) - 1;
891 if (!adr_d->endpoints->aggregated || no_aggregation) {
897 *group_id = adr_d->endpoints->group_id;
899 /* gather other link ID of slaves in the same group */
900 for (adr_next = adr_link + 1; adr_next && adr_next->num_adr;
902 const struct snd_soc_acpi_endpoint *endpoint;
904 endpoint = adr_next->adr_d->endpoints;
905 if (!endpoint->aggregated ||
906 endpoint->group_id != *group_id)
909 /* make sure the link mask has a single bit set */
910 if (!is_power_of_2(adr_next->mask))
913 if (index >= SDW_MAX_CPU_DAIS) {
914 dev_err(dev, " cpu_dai_id array overflows");
918 cpu_dai_id[index++] = ffs(adr_next->mask) - 1;
919 *codec_num += adr_next->num_adr;
923 * indicate CPU DAIs for this group have been generated
924 * to avoid generating CPU DAIs for this group again.
926 group_generated[*group_id] = true;
927 *cpu_dai_num = index;
932 static int create_sdw_dailink(struct snd_soc_card *card,
933 struct device *dev, int *link_index,
934 struct snd_soc_dai_link *dai_links,
935 int sdw_be_num, int sdw_cpu_dai_num,
936 struct snd_soc_dai_link_component *cpus,
937 const struct snd_soc_acpi_link_adr *link,
938 int *cpu_id, bool *group_generated,
939 struct snd_soc_codec_conf *codec_conf,
940 int codec_count, int *link_id,
941 int *codec_conf_index,
942 bool *ignore_pch_dmic)
944 const struct snd_soc_acpi_link_adr *link_next;
945 struct snd_soc_dai_link_component *codecs;
946 int cpu_dai_id[SDW_MAX_CPU_DAIS];
947 int cpu_dai_num, cpu_dai_index;
948 unsigned int group_id;
957 ret = get_slave_info(link, dev, cpu_dai_id, &cpu_dai_num, &codec_num,
958 &group_id, group_generated);
962 codecs = devm_kcalloc(dev, codec_num, sizeof(*codecs), GFP_KERNEL);
966 /* generate codec name on different links in the same group */
967 for (link_next = link; link_next && link_next->num_adr &&
968 i < cpu_dai_num; link_next++) {
969 const struct snd_soc_acpi_endpoint *endpoints;
971 endpoints = link_next->adr_d->endpoints;
972 if (group_id && (!endpoints->aggregated ||
973 endpoints->group_id != group_id))
976 /* skip the link excluded by this processed group */
977 if (cpu_dai_id[i] != ffs(link_next->mask) - 1)
980 ret = create_codec_dai_name(dev, link_next, codecs, codec_idx,
981 codec_conf, codec_count, codec_conf_index);
985 /* check next link to create codec dai in the processed group */
987 codec_idx += link_next->num_adr;
990 /* find codec info to create BE DAI */
991 codec_index = find_codec_info_part(link->adr_d[0].adr);
995 if (codec_info_list[codec_index].ignore_pch_dmic)
996 *ignore_pch_dmic = true;
998 /* Shift the first amplifier's *link_id to SDW_AMP_DAI_ID */
999 if (codec_info_list[codec_index].codec_type == SOF_SDW_CODEC_TYPE_AMP &&
1000 *link_id < SDW_AMP_DAI_ID)
1001 *link_id = SDW_AMP_DAI_ID;
1004 * DAI ID is fixed at SDW_DMIC_DAI_ID for MICs to
1005 * keep sdw DMIC and HDMI setting static in UCM
1007 if (codec_info_list[codec_index].codec_type == SOF_SDW_CODEC_TYPE_MIC &&
1008 *link_id < SDW_DMIC_DAI_ID)
1009 *link_id = SDW_DMIC_DAI_ID;
1011 cpu_dai_index = *cpu_id;
1012 for_each_pcm_streams(stream) {
1013 char *name, *cpu_name;
1014 int playback, capture;
1015 static const char * const sdw_stream_name[] = {
1020 if (!codec_info_list[codec_index].direction[stream])
1023 /* create stream name according to first link id */
1024 name = devm_kasprintf(dev, GFP_KERNEL,
1025 sdw_stream_name[stream], cpu_dai_id[0]);
1030 * generate CPU DAI name base on the sdw link ID and
1031 * PIN ID with offset of 2 according to sdw dai driver.
1033 for (k = 0; k < cpu_dai_num; k++) {
1034 cpu_name = devm_kasprintf(dev, GFP_KERNEL,
1035 "SDW%d Pin%d", cpu_dai_id[k],
1036 j + SDW_INTEL_BIDIR_PDI_BASE);
1040 if (cpu_dai_index >= sdw_cpu_dai_num) {
1041 dev_err(dev, "invalid cpu dai index %d",
1046 cpus[cpu_dai_index++].dai_name = cpu_name;
1050 * We create sdw dai links at first stage, so link index should
1051 * not be larger than sdw_be_num
1053 if (*link_index >= sdw_be_num) {
1054 dev_err(dev, "invalid dai link index %d", *link_index);
1058 if (*cpu_id >= sdw_cpu_dai_num) {
1059 dev_err(dev, " invalid cpu dai index %d", *cpu_id);
1063 playback = (stream == SNDRV_PCM_STREAM_PLAYBACK);
1064 capture = (stream == SNDRV_PCM_STREAM_CAPTURE);
1065 init_dai_link(dev, dai_links + *link_index, (*link_id)++, name,
1067 cpus + *cpu_id, cpu_dai_num,
1072 * SoundWire DAILINKs use 'stream' functions and Bank Switch operations
1073 * based on wait_for_completion(), tag them as 'nonatomic'.
1075 dai_links[*link_index].nonatomic = true;
1077 ret = set_codec_init_func(card, link, dai_links + (*link_index)++,
1078 playback, group_id);
1080 dev_err(dev, "failed to init codec %d", codec_index);
1084 *cpu_id += cpu_dai_num;
1091 #define IDISP_CODEC_MASK 0x4
1093 static int sof_card_codec_conf_alloc(struct device *dev,
1094 struct snd_soc_acpi_mach_params *mach_params,
1095 struct snd_soc_codec_conf **codec_conf,
1096 int *codec_conf_count)
1098 const struct snd_soc_acpi_link_adr *adr_link;
1099 struct snd_soc_codec_conf *c_conf;
1103 adr_link = mach_params->links;
1107 /* generate DAI links by each sdw link */
1108 for (; adr_link->num_adr; adr_link++) {
1109 for (i = 0; i < adr_link->num_adr; i++) {
1110 if (!adr_link->adr_d[i].name_prefix) {
1111 dev_err(dev, "codec 0x%llx does not have a name prefix\n",
1112 adr_link->adr_d[i].adr);
1116 num_codecs += adr_link->num_adr;
1119 c_conf = devm_kzalloc(dev, num_codecs * sizeof(*c_conf), GFP_KERNEL);
1123 *codec_conf = c_conf;
1124 *codec_conf_count = num_codecs;
1129 static int sof_card_dai_links_create(struct device *dev,
1130 struct snd_soc_acpi_mach *mach,
1131 struct snd_soc_card *card)
1133 int ssp_num, sdw_be_num = 0, hdmi_num = 0, dmic_num;
1134 struct mc_private *ctx = snd_soc_card_get_drvdata(card);
1135 struct snd_soc_dai_link_component *idisp_components;
1136 struct snd_soc_dai_link_component *ssp_components;
1137 struct snd_soc_acpi_mach_params *mach_params;
1138 const struct snd_soc_acpi_link_adr *adr_link;
1139 struct snd_soc_dai_link_component *cpus;
1140 struct snd_soc_codec_conf *codec_conf;
1141 bool ignore_pch_dmic = false;
1142 int codec_conf_count;
1143 int codec_conf_index = 0;
1144 bool group_generated[SDW_MAX_GROUPS];
1145 int ssp_codec_index, ssp_mask;
1146 struct snd_soc_dai_link *links;
1147 int num_links, link_index = 0;
1148 char *name, *cpu_name;
1149 int total_cpu_dai_num;
1150 int sdw_cpu_dai_num;
1151 int i, j, be_id = 0;
1156 mach_params = &mach->mach_params;
1158 /* allocate codec conf, will be populated when dailinks are created */
1159 ret = sof_card_codec_conf_alloc(dev, mach_params, &codec_conf, &codec_conf_count);
1163 /* reset amp_num to ensure amp_num++ starts from 0 in each probe */
1164 for (i = 0; i < ARRAY_SIZE(codec_info_list); i++)
1165 codec_info_list[i].amp_num = 0;
1167 if (mach_params->codec_mask & IDISP_CODEC_MASK) {
1168 ctx->idisp_codec = true;
1170 if (sof_sdw_quirk & SOF_SDW_TGL_HDMI)
1171 hdmi_num = SOF_TGL_HDMI_COUNT;
1173 hdmi_num = SOF_PRE_TGL_HDMI_COUNT;
1176 ssp_mask = SOF_SSP_GET_PORT(sof_sdw_quirk);
1178 * on generic tgl platform, I2S or sdw mode is supported
1179 * based on board rework. A ACPI device is registered in
1180 * system only when I2S mode is supported, not sdw mode.
1181 * Here check ACPI ID to confirm I2S is supported.
1183 ssp_codec_index = find_codec_info_acpi(mach->id);
1184 ssp_num = ssp_codec_index >= 0 ? hweight_long(ssp_mask) : 0;
1185 comp_num = hdmi_num + ssp_num;
1187 ret = get_sdw_dailink_info(dev, mach_params->links,
1188 &sdw_be_num, &sdw_cpu_dai_num);
1190 dev_err(dev, "failed to get sdw link info %d", ret);
1194 /* enable dmic01 & dmic16k */
1195 dmic_num = (sof_sdw_quirk & SOF_SDW_PCH_DMIC || mach_params->dmic_num) ? 2 : 0;
1196 comp_num += dmic_num;
1198 if (sof_sdw_quirk & SOF_SSP_BT_OFFLOAD_PRESENT)
1201 dev_dbg(dev, "sdw %d, ssp %d, dmic %d, hdmi %d", sdw_be_num, ssp_num,
1202 dmic_num, ctx->idisp_codec ? hdmi_num : 0);
1204 /* allocate BE dailinks */
1205 num_links = comp_num + sdw_be_num;
1206 links = devm_kcalloc(dev, num_links, sizeof(*links), GFP_KERNEL);
1208 /* allocated CPU DAIs */
1209 total_cpu_dai_num = comp_num + sdw_cpu_dai_num;
1210 cpus = devm_kcalloc(dev, total_cpu_dai_num, sizeof(*cpus),
1213 if (!links || !cpus)
1220 adr_link = mach_params->links;
1225 * SoundWire Slaves aggregated in the same group may be
1226 * located on different hardware links. Clear array to indicate
1227 * CPU DAIs for this group have not been generated.
1229 for (i = 0; i < SDW_MAX_GROUPS; i++)
1230 group_generated[i] = false;
1232 /* generate DAI links by each sdw link */
1233 for (; adr_link->num_adr; adr_link++) {
1234 const struct snd_soc_acpi_endpoint *endpoint;
1236 endpoint = adr_link->adr_d->endpoints;
1237 if (endpoint->aggregated && !endpoint->group_id) {
1238 dev_err(dev, "invalid group id on link %x",
1243 /* this group has been generated */
1244 if (endpoint->aggregated &&
1245 group_generated[endpoint->group_id])
1248 ret = create_sdw_dailink(card, dev, &link_index, links, sdw_be_num,
1249 sdw_cpu_dai_num, cpus, adr_link,
1250 &cpu_id, group_generated,
1251 codec_conf, codec_conf_count,
1252 &be_id, &codec_conf_index,
1255 dev_err(dev, "failed to create dai link %d", link_index);
1265 for (i = 0, j = 0; ssp_mask; i++, ssp_mask >>= 1) {
1266 struct sof_sdw_codec_info *info;
1267 int playback, capture;
1270 if (!(ssp_mask & 0x1))
1273 name = devm_kasprintf(dev, GFP_KERNEL,
1278 cpu_name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d Pin", i);
1282 ssp_components = devm_kzalloc(dev, sizeof(*ssp_components),
1284 if (!ssp_components)
1287 info = &codec_info_list[ssp_codec_index];
1288 codec_name = devm_kasprintf(dev, GFP_KERNEL, "i2c-%s:0%d",
1289 info->acpi_id, j++);
1293 ssp_components->name = codec_name;
1294 ssp_components->dai_name = info->dai_name;
1295 cpus[cpu_id].dai_name = cpu_name;
1297 playback = info->direction[SNDRV_PCM_STREAM_PLAYBACK];
1298 capture = info->direction[SNDRV_PCM_STREAM_CAPTURE];
1299 init_dai_link(dev, links + link_index, be_id, name,
1305 ret = info->init(card, NULL, links + link_index, info, 0);
1309 INC_ID(be_id, cpu_id, link_index);
1315 if (ignore_pch_dmic) {
1316 dev_warn(dev, "Ignoring PCH DMIC\n");
1319 cpus[cpu_id].dai_name = "DMIC01 Pin";
1320 init_dai_link(dev, links + link_index, be_id, "dmic01",
1321 0, 1, // DMIC only supports capture
1324 sof_sdw_dmic_init, NULL);
1325 INC_ID(be_id, cpu_id, link_index);
1327 cpus[cpu_id].dai_name = "DMIC16k Pin";
1328 init_dai_link(dev, links + link_index, be_id, "dmic16k",
1329 0, 1, // DMIC only supports capture
1332 /* don't call sof_sdw_dmic_init() twice */
1334 INC_ID(be_id, cpu_id, link_index);
1340 idisp_components = devm_kcalloc(dev, hdmi_num,
1341 sizeof(*idisp_components),
1343 if (!idisp_components)
1347 for (i = 0; i < hdmi_num; i++) {
1348 name = devm_kasprintf(dev, GFP_KERNEL,
1353 if (ctx->idisp_codec) {
1354 idisp_components[i].name = "ehdaudio0D2";
1355 idisp_components[i].dai_name = devm_kasprintf(dev,
1357 "intel-hdmi-hifi%d",
1359 if (!idisp_components[i].dai_name)
1362 idisp_components[i].name = "snd-soc-dummy";
1363 idisp_components[i].dai_name = "snd-soc-dummy-dai";
1366 cpu_name = devm_kasprintf(dev, GFP_KERNEL,
1367 "iDisp%d Pin", i + 1);
1371 cpus[cpu_id].dai_name = cpu_name;
1372 init_dai_link(dev, links + link_index, be_id, name,
1373 1, 0, // HDMI only supports playback
1375 idisp_components + i, 1,
1376 sof_sdw_hdmi_init, NULL);
1377 INC_ID(be_id, cpu_id, link_index);
1380 if (sof_sdw_quirk & SOF_SSP_BT_OFFLOAD_PRESENT) {
1381 int port = (sof_sdw_quirk & SOF_BT_OFFLOAD_SSP_MASK) >>
1382 SOF_BT_OFFLOAD_SSP_SHIFT;
1384 name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-BT", port);
1388 ssp_components = devm_kzalloc(dev, sizeof(*ssp_components),
1390 if (!ssp_components)
1393 ssp_components->name = "snd-soc-dummy";
1394 ssp_components->dai_name = "snd-soc-dummy-dai";
1396 cpu_name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d Pin", port);
1400 cpus[cpu_id].dai_name = cpu_name;
1401 init_dai_link(dev, links + link_index, be_id, name, 1, 1,
1402 cpus + cpu_id, 1, ssp_components, 1, NULL, NULL);
1405 card->dai_link = links;
1406 card->num_links = num_links;
1408 card->codec_conf = codec_conf;
1409 card->num_configs = codec_conf_count;
1414 static int sof_sdw_card_late_probe(struct snd_soc_card *card)
1416 struct mc_private *ctx = snd_soc_card_get_drvdata(card);
1420 for (i = 0; i < ARRAY_SIZE(codec_info_list); i++) {
1421 if (!codec_info_list[i].late_probe)
1424 ret = codec_info_list[i].codec_card_late_probe(card);
1429 if (ctx->idisp_codec)
1430 ret = sof_sdw_hdmi_card_late_probe(card);
1436 static const char sdw_card_long_name[] = "Intel Soundwire SOF";
1438 static struct snd_soc_card card_sof_sdw = {
1439 .name = "soundwire",
1440 .owner = THIS_MODULE,
1441 .late_probe = sof_sdw_card_late_probe,
1444 static void mc_dailink_exit_loop(struct snd_soc_card *card)
1446 struct snd_soc_dai_link *link;
1450 for (i = 0; i < ARRAY_SIZE(codec_info_list); i++) {
1451 if (!codec_info_list[i].exit)
1454 * We don't need to call .exit function if there is no matched
1457 for_each_card_prelinks(card, j, link) {
1458 if (!strcmp(link->codecs[0].dai_name,
1459 codec_info_list[i].dai_name)) {
1460 ret = codec_info_list[i].exit(card, link);
1463 "codec exit failed %d\n",
1471 static int mc_probe(struct platform_device *pdev)
1473 struct snd_soc_card *card = &card_sof_sdw;
1474 struct snd_soc_acpi_mach *mach;
1475 struct mc_private *ctx;
1479 dev_dbg(&pdev->dev, "Entry\n");
1481 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
1485 dmi_check_system(sof_sdw_quirk_table);
1487 if (quirk_override != -1) {
1488 dev_info(&pdev->dev, "Overriding quirk 0x%lx => 0x%x\n",
1489 sof_sdw_quirk, quirk_override);
1490 sof_sdw_quirk = quirk_override;
1492 log_quirks(&pdev->dev);
1494 INIT_LIST_HEAD(&ctx->hdmi_pcm_list);
1496 card->dev = &pdev->dev;
1497 snd_soc_card_set_drvdata(card, ctx);
1499 mach = pdev->dev.platform_data;
1500 ret = sof_card_dai_links_create(&pdev->dev, mach,
1506 * the default amp_num is zero for each codec and
1507 * amp_num will only be increased for active amp
1508 * codecs on used platform
1510 for (i = 0; i < ARRAY_SIZE(codec_info_list); i++)
1511 amp_num += codec_info_list[i].amp_num;
1513 card->components = devm_kasprintf(card->dev, GFP_KERNEL,
1514 "cfg-spk:%d cfg-amp:%d",
1515 (sof_sdw_quirk & SOF_SDW_FOUR_SPK)
1517 if (!card->components)
1520 if (mach->mach_params.dmic_num) {
1521 card->components = devm_kasprintf(card->dev, GFP_KERNEL,
1522 "%s mic:dmic cfg-mics:%d",
1524 mach->mach_params.dmic_num);
1525 if (!card->components)
1529 card->long_name = sdw_card_long_name;
1531 /* Register the card */
1532 ret = devm_snd_soc_register_card(&pdev->dev, card);
1534 dev_err(card->dev, "snd_soc_register_card failed %d\n", ret);
1535 mc_dailink_exit_loop(card);
1539 platform_set_drvdata(pdev, card);
1544 static int mc_remove(struct platform_device *pdev)
1546 struct snd_soc_card *card = platform_get_drvdata(pdev);
1548 mc_dailink_exit_loop(card);
1553 static struct platform_driver sof_sdw_driver = {
1556 .pm = &snd_soc_pm_ops,
1559 .remove = mc_remove,
1562 module_platform_driver(sof_sdw_driver);
1564 MODULE_DESCRIPTION("ASoC SoundWire Generic Machine driver");
1565 MODULE_AUTHOR("Bard Liao <yung-chuan.liao@linux.intel.com>");
1566 MODULE_AUTHOR("Rander Wang <rander.wang@linux.intel.com>");
1567 MODULE_AUTHOR("Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>");
1568 MODULE_LICENSE("GPL v2");
1569 MODULE_ALIAS("platform:sof_sdw");
1570 MODULE_IMPORT_NS(SND_SOC_INTEL_HDA_DSP_COMMON);
1571 MODULE_IMPORT_NS(SND_SOC_INTEL_SOF_MAXIM_COMMON);