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_RT715_DAI_ID_FIX)
40 dev_dbg(dev, "quirk SOF_RT715_DAI_ID_FIX enabled\n");
41 if (sof_sdw_quirk & SOF_SDW_NO_AGGREGATION)
42 dev_dbg(dev, "quirk SOF_SDW_NO_AGGREGATION enabled\n");
45 static int sof_sdw_quirk_cb(const struct dmi_system_id *id)
47 sof_sdw_quirk = (unsigned long)id->driver_data;
51 static const struct dmi_system_id sof_sdw_quirk_table[] = {
52 /* CometLake devices */
54 .callback = sof_sdw_quirk_cb,
56 DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
57 DMI_MATCH(DMI_PRODUCT_NAME, "CometLake Client"),
59 .driver_data = (void *)SOF_SDW_PCH_DMIC,
62 .callback = sof_sdw_quirk_cb,
64 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
65 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "09C6")
67 .driver_data = (void *)(RT711_JD2 |
68 SOF_RT715_DAI_ID_FIX),
71 /* early version of SKU 09C6 */
72 .callback = sof_sdw_quirk_cb,
74 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
75 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0983")
77 .driver_data = (void *)(RT711_JD2 |
78 SOF_RT715_DAI_ID_FIX),
81 .callback = sof_sdw_quirk_cb,
83 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
84 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "098F"),
86 .driver_data = (void *)(RT711_JD2 |
87 SOF_RT715_DAI_ID_FIX |
91 .callback = sof_sdw_quirk_cb,
93 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
94 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0990"),
96 .driver_data = (void *)(RT711_JD2 |
97 SOF_RT715_DAI_ID_FIX |
100 /* IceLake devices */
102 .callback = sof_sdw_quirk_cb,
104 DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
105 DMI_MATCH(DMI_PRODUCT_NAME, "Ice Lake Client"),
107 .driver_data = (void *)SOF_SDW_PCH_DMIC,
109 /* TigerLake devices */
111 .callback = sof_sdw_quirk_cb,
113 DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
114 DMI_MATCH(DMI_PRODUCT_NAME,
115 "Tiger Lake Client Platform"),
117 .driver_data = (void *)(SOF_SDW_TGL_HDMI |
120 SOF_SSP_PORT(SOF_I2S_SSP2)),
123 .callback = sof_sdw_quirk_cb,
125 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
126 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0A3E")
128 .driver_data = (void *)(SOF_SDW_TGL_HDMI |
130 SOF_RT715_DAI_ID_FIX),
134 .callback = sof_sdw_quirk_cb,
136 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
137 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0A5D")
139 .driver_data = (void *)(SOF_SDW_TGL_HDMI |
141 SOF_RT715_DAI_ID_FIX |
145 .callback = sof_sdw_quirk_cb,
147 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
148 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0A5E")
150 .driver_data = (void *)(SOF_SDW_TGL_HDMI |
152 SOF_RT715_DAI_ID_FIX |
156 .callback = sof_sdw_quirk_cb,
158 DMI_MATCH(DMI_SYS_VENDOR, "Google"),
159 DMI_MATCH(DMI_PRODUCT_NAME, "Volteer"),
161 .driver_data = (void *)(SOF_SDW_TGL_HDMI |
164 SOF_BT_OFFLOAD_SSP(2) |
165 SOF_SSP_BT_OFFLOAD_PRESENT),
168 .callback = sof_sdw_quirk_cb,
170 DMI_MATCH(DMI_SYS_VENDOR, "Google"),
171 DMI_MATCH(DMI_PRODUCT_NAME, "Ripto"),
173 .driver_data = (void *)(SOF_SDW_TGL_HDMI |
179 * this entry covers multiple HP SKUs. The family name
180 * does not seem robust enough, so we use a partial
181 * match that ignores the product name suffix
182 * (e.g. 15-eb1xxx, 14t-ea000 or 13-aw2xxx)
184 .callback = sof_sdw_quirk_cb,
186 DMI_MATCH(DMI_SYS_VENDOR, "HP"),
187 DMI_MATCH(DMI_PRODUCT_NAME, "HP Spectre x360 Conv"),
189 .driver_data = (void *)(SOF_SDW_TGL_HDMI |
194 /* NUC15 'Bishop County' LAPBC510 and LAPBC710 skews */
195 .callback = sof_sdw_quirk_cb,
197 DMI_MATCH(DMI_SYS_VENDOR, "Intel(R) Client Systems"),
198 DMI_MATCH(DMI_PRODUCT_NAME, "LAPBC"),
200 .driver_data = (void *)(SOF_SDW_TGL_HDMI |
204 /* TigerLake-SDCA devices */
206 .callback = sof_sdw_quirk_cb,
208 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
209 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0A32")
211 .driver_data = (void *)(SOF_SDW_TGL_HDMI |
213 SOF_RT715_DAI_ID_FIX |
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 |
224 SOF_RT715_DAI_ID_FIX),
226 /* AlderLake devices */
228 .callback = sof_sdw_quirk_cb,
230 DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
231 DMI_MATCH(DMI_PRODUCT_NAME, "Alder Lake Client Platform"),
233 .driver_data = (void *)(RT711_JD2_100K |
235 SOF_RT715_DAI_ID_FIX |
236 SOF_BT_OFFLOAD_SSP(2) |
237 SOF_SSP_BT_OFFLOAD_PRESENT),
240 .callback = sof_sdw_quirk_cb,
242 DMI_MATCH(DMI_SYS_VENDOR, "Google"),
243 DMI_MATCH(DMI_PRODUCT_NAME, "Brya"),
245 .driver_data = (void *)(SOF_SDW_TGL_HDMI |
248 SOF_BT_OFFLOAD_SSP(2) |
249 SOF_SSP_BT_OFFLOAD_PRESENT),
254 static struct snd_soc_dai_link_component dmic_component[] = {
256 .name = "dmic-codec",
257 .dai_name = "dmic-hifi",
261 static struct snd_soc_dai_link_component platform_component[] = {
263 /* name might be overridden during probe */
264 .name = "0000:00:1f.3"
268 /* these wrappers are only needed to avoid typecast compilation errors */
269 int sdw_startup(struct snd_pcm_substream *substream)
271 return sdw_startup_stream(substream);
274 int sdw_prepare(struct snd_pcm_substream *substream)
276 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
277 struct sdw_stream_runtime *sdw_stream;
278 struct snd_soc_dai *dai;
280 /* Find stream from first CPU DAI */
281 dai = asoc_rtd_to_cpu(rtd, 0);
283 sdw_stream = snd_soc_dai_get_sdw_stream(dai, substream->stream);
285 if (IS_ERR(sdw_stream)) {
286 dev_err(rtd->dev, "no stream found for DAI %s", dai->name);
287 return PTR_ERR(sdw_stream);
290 return sdw_prepare_stream(sdw_stream);
293 int sdw_trigger(struct snd_pcm_substream *substream, int cmd)
295 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
296 struct sdw_stream_runtime *sdw_stream;
297 struct snd_soc_dai *dai;
300 /* Find stream from first CPU DAI */
301 dai = asoc_rtd_to_cpu(rtd, 0);
303 sdw_stream = snd_soc_dai_get_sdw_stream(dai, substream->stream);
305 if (IS_ERR(sdw_stream)) {
306 dev_err(rtd->dev, "no stream found for DAI %s", dai->name);
307 return PTR_ERR(sdw_stream);
311 case SNDRV_PCM_TRIGGER_START:
312 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
313 case SNDRV_PCM_TRIGGER_RESUME:
314 ret = sdw_enable_stream(sdw_stream);
317 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
318 case SNDRV_PCM_TRIGGER_SUSPEND:
319 case SNDRV_PCM_TRIGGER_STOP:
320 ret = sdw_disable_stream(sdw_stream);
328 dev_err(rtd->dev, "%s trigger %d failed: %d", __func__, cmd, ret);
333 int sdw_hw_free(struct snd_pcm_substream *substream)
335 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
336 struct sdw_stream_runtime *sdw_stream;
337 struct snd_soc_dai *dai;
339 /* Find stream from first CPU DAI */
340 dai = asoc_rtd_to_cpu(rtd, 0);
342 sdw_stream = snd_soc_dai_get_sdw_stream(dai, substream->stream);
344 if (IS_ERR(sdw_stream)) {
345 dev_err(rtd->dev, "no stream found for DAI %s", dai->name);
346 return PTR_ERR(sdw_stream);
349 return sdw_deprepare_stream(sdw_stream);
352 void sdw_shutdown(struct snd_pcm_substream *substream)
354 sdw_shutdown_stream(substream);
357 static const struct snd_soc_ops sdw_ops = {
358 .startup = sdw_startup,
359 .prepare = sdw_prepare,
360 .trigger = sdw_trigger,
361 .hw_free = sdw_hw_free,
362 .shutdown = sdw_shutdown,
365 static int sof_sdw_mic_codec_mockup_init(struct snd_soc_card *card,
366 const struct snd_soc_acpi_link_adr *link,
367 struct snd_soc_dai_link *dai_links,
368 struct sof_sdw_codec_info *info,
372 * force DAI link to use same ID as RT715 and DMIC
373 * to reuse topologies
375 dai_links->id = SDW_DMIC_DAI_ID;
379 static struct sof_sdw_codec_info codec_info_list[] = {
382 .direction = {true, true},
383 .dai_name = "rt700-aif1",
384 .init = sof_sdw_rt700_init,
389 .direction = {true, true},
390 .dai_name = "rt711-sdca-aif1",
391 .init = sof_sdw_rt711_sdca_init,
392 .exit = sof_sdw_rt711_sdca_exit,
397 .direction = {true, true},
398 .dai_name = "rt711-aif1",
399 .init = sof_sdw_rt711_init,
400 .exit = sof_sdw_rt711_exit,
404 .acpi_id = "10EC1308",
405 .direction = {true, false},
406 .dai_name = "rt1308-aif",
407 .ops = &sof_sdw_rt1308_i2s_ops,
408 .init = sof_sdw_rt1308_init,
412 .direction = {true, true},
413 .dai_name = "rt1316-aif",
414 .init = sof_sdw_rt1316_init,
419 .direction = {false, true},
420 .ignore_pch_dmic = true,
421 .dai_name = "rt715-aif2",
422 .init = sof_sdw_rt715_sdca_init,
427 .direction = {false, true},
428 .ignore_pch_dmic = true,
429 .dai_name = "rt715-aif2",
430 .init = sof_sdw_rt715_sdca_init,
435 .direction = {false, true},
436 .ignore_pch_dmic = true,
437 .dai_name = "rt715-aif2",
438 .init = sof_sdw_rt715_init,
443 .direction = {false, true},
444 .ignore_pch_dmic = true,
445 .dai_name = "rt715-aif2",
446 .init = sof_sdw_rt715_init,
450 .direction = {true, true},
451 .dai_name = "max98373-aif1",
452 .init = sof_sdw_mx8373_init,
453 .codec_card_late_probe = sof_sdw_mx8373_late_probe,
457 .direction = {true, true},
458 .dai_name = "rt5682-sdw",
459 .init = sof_sdw_rt5682_init,
462 .part_id = 0xaaaa, /* generic codec mockup */
464 .direction = {true, true},
465 .dai_name = "sdw-mockup-aif1",
469 .part_id = 0xaa55, /* headset codec mockup */
471 .direction = {true, true},
472 .dai_name = "sdw-mockup-aif1",
476 .part_id = 0x55aa, /* amplifier mockup */
478 .direction = {true, false},
479 .dai_name = "sdw-mockup-aif1",
485 .direction = {false, true},
486 .dai_name = "sdw-mockup-aif1",
487 .init = sof_sdw_mic_codec_mockup_init,
491 static inline int find_codec_info_part(u64 adr)
493 unsigned int part_id, sdw_version;
496 part_id = SDW_PART_ID(adr);
497 sdw_version = SDW_VERSION(adr);
498 for (i = 0; i < ARRAY_SIZE(codec_info_list); i++)
500 * A codec info is for all sdw version with the part id if
501 * version_id is not specified in the codec info.
503 if (part_id == codec_info_list[i].part_id &&
504 (!codec_info_list[i].version_id ||
505 sdw_version == codec_info_list[i].version_id))
512 static inline int find_codec_info_acpi(const u8 *acpi_id)
519 for (i = 0; i < ARRAY_SIZE(codec_info_list); i++)
520 if (!memcmp(codec_info_list[i].acpi_id, acpi_id,
524 if (i == ARRAY_SIZE(codec_info_list))
531 * get BE dailink number and CPU DAI number based on sdw link adr.
532 * Since some sdw slaves may be aggregated, the CPU DAI number
533 * may be larger than the number of BE dailinks.
535 static int get_sdw_dailink_info(const struct snd_soc_acpi_link_adr *links,
536 int *sdw_be_num, int *sdw_cpu_dai_num)
538 const struct snd_soc_acpi_link_adr *link;
539 bool group_visited[SDW_MAX_GROUPS];
543 no_aggregation = sof_sdw_quirk & SOF_SDW_NO_AGGREGATION;
544 *sdw_cpu_dai_num = 0;
550 for (i = 0; i < SDW_MAX_GROUPS; i++)
551 group_visited[i] = false;
553 for (link = links; link->num_adr; link++) {
554 const struct snd_soc_acpi_endpoint *endpoint;
559 adr = link->adr_d->adr;
560 codec_index = find_codec_info_part(adr);
564 endpoint = link->adr_d->endpoints;
566 /* count DAI number for playback and capture */
567 for_each_pcm_streams(stream) {
568 if (!codec_info_list[codec_index].direction[stream])
571 (*sdw_cpu_dai_num)++;
573 /* count BE for each non-aggregated slave or group */
574 if (!endpoint->aggregated || no_aggregation ||
575 !group_visited[endpoint->group_id])
579 if (endpoint->aggregated)
580 group_visited[endpoint->group_id] = true;
586 static void init_dai_link(struct device *dev, struct snd_soc_dai_link *dai_links,
587 int be_id, char *name, int playback, int capture,
588 struct snd_soc_dai_link_component *cpus, int cpus_num,
589 struct snd_soc_dai_link_component *codecs, int codecs_num,
590 int (*init)(struct snd_soc_pcm_runtime *rtd),
591 const struct snd_soc_ops *ops)
593 dev_dbg(dev, "create dai link %s, id %d\n", name, be_id);
594 dai_links->id = be_id;
595 dai_links->name = name;
596 dai_links->platforms = platform_component;
597 dai_links->num_platforms = ARRAY_SIZE(platform_component);
598 dai_links->no_pcm = 1;
599 dai_links->cpus = cpus;
600 dai_links->num_cpus = cpus_num;
601 dai_links->codecs = codecs;
602 dai_links->num_codecs = codecs_num;
603 dai_links->dpcm_playback = playback;
604 dai_links->dpcm_capture = capture;
605 dai_links->init = init;
606 dai_links->ops = ops;
609 static bool is_unique_device(const struct snd_soc_acpi_link_adr *link,
610 unsigned int sdw_version,
612 unsigned int part_id,
613 unsigned int class_id,
619 for (i = 0; i < link->num_adr; i++) {
620 unsigned int sdw1_version, mfg1_id, part1_id, class1_id;
624 if (i == index_in_link)
627 adr = link->adr_d[i].adr;
629 sdw1_version = SDW_VERSION(adr);
630 mfg1_id = SDW_MFG_ID(adr);
631 part1_id = SDW_PART_ID(adr);
632 class1_id = SDW_CLASS_ID(adr);
634 if (sdw_version == sdw1_version &&
636 part_id == part1_id &&
637 class_id == class1_id)
644 static int create_codec_dai_name(struct device *dev,
645 const struct snd_soc_acpi_link_adr *link,
646 struct snd_soc_dai_link_component *codec,
648 struct snd_soc_codec_conf *codec_conf,
650 int *codec_conf_index)
655 if (*codec_conf_index + link->num_adr > codec_count) {
656 dev_err(dev, "codec_conf: out-of-bounds access requested\n");
660 for (i = 0; i < link->num_adr; i++) {
661 unsigned int sdw_version, unique_id, mfg_id;
662 unsigned int link_id, part_id, class_id;
663 int codec_index, comp_index;
667 adr = link->adr_d[i].adr;
669 sdw_version = SDW_VERSION(adr);
670 link_id = SDW_DISCO_LINK_ID(adr);
671 unique_id = SDW_UNIQUE_ID(adr);
672 mfg_id = SDW_MFG_ID(adr);
673 part_id = SDW_PART_ID(adr);
674 class_id = SDW_CLASS_ID(adr);
676 comp_index = i + offset;
677 if (is_unique_device(link, sdw_version, mfg_id, part_id,
679 codec_str = "sdw:%01x:%04x:%04x:%02x";
680 codec[comp_index].name =
681 devm_kasprintf(dev, GFP_KERNEL, codec_str,
682 link_id, mfg_id, part_id,
685 codec_str = "sdw:%01x:%04x:%04x:%02x:%01x";
686 codec[comp_index].name =
687 devm_kasprintf(dev, GFP_KERNEL, codec_str,
688 link_id, mfg_id, part_id,
689 class_id, unique_id);
692 if (!codec[comp_index].name)
695 codec_index = find_codec_info_part(adr);
699 codec[comp_index].dai_name =
700 codec_info_list[codec_index].dai_name;
702 codec_conf[*codec_conf_index].dlc = codec[comp_index];
703 codec_conf[*codec_conf_index].name_prefix = link->adr_d[i].name_prefix;
711 static int set_codec_init_func(struct snd_soc_card *card,
712 const struct snd_soc_acpi_link_adr *link,
713 struct snd_soc_dai_link *dai_links,
714 bool playback, int group_id)
720 * Initialize the codec. If codec is part of an aggregated
721 * group (group_id>0), initialize all codecs belonging to
724 for (i = 0; i < link->num_adr; i++) {
727 codec_index = find_codec_info_part(link->adr_d[i].adr);
731 /* The group_id is > 0 iff the codec is aggregated */
732 if (link->adr_d[i].endpoints->group_id != group_id)
734 if (codec_info_list[codec_index].init)
735 codec_info_list[codec_index].init(card,
738 &codec_info_list[codec_index],
742 } while (link->mask && group_id);
748 * check endpoint status in slaves and gather link ID for all slaves in
749 * the same group to generate different CPU DAI. Now only support
750 * one sdw link with all slaves set with only single group id.
752 * one slave on one sdw link with aggregated = 0
753 * one sdw BE DAI <---> one-cpu DAI <---> one-codec DAI
755 * two or more slaves on one sdw link with aggregated = 0
756 * one sdw BE DAI <---> one-cpu DAI <---> multi-codec DAIs
758 * multiple links with multiple slaves with aggregated = 1
759 * one sdw BE DAI <---> 1 .. N CPU DAIs <----> 1 .. N codec DAIs
761 static int get_slave_info(const struct snd_soc_acpi_link_adr *adr_link,
762 struct device *dev, int *cpu_dai_id, int *cpu_dai_num,
763 int *codec_num, unsigned int *group_id,
764 bool *group_generated)
766 const struct snd_soc_acpi_adr_device *adr_d;
767 const struct snd_soc_acpi_link_adr *adr_next;
771 no_aggregation = sof_sdw_quirk & SOF_SDW_NO_AGGREGATION;
772 *codec_num = adr_link->num_adr;
773 adr_d = adr_link->adr_d;
775 /* make sure the link mask has a single bit set */
776 if (!is_power_of_2(adr_link->mask))
779 cpu_dai_id[index++] = ffs(adr_link->mask) - 1;
780 if (!adr_d->endpoints->aggregated || no_aggregation) {
786 *group_id = adr_d->endpoints->group_id;
788 /* gather other link ID of slaves in the same group */
789 for (adr_next = adr_link + 1; adr_next && adr_next->num_adr;
791 const struct snd_soc_acpi_endpoint *endpoint;
793 endpoint = adr_next->adr_d->endpoints;
794 if (!endpoint->aggregated ||
795 endpoint->group_id != *group_id)
798 /* make sure the link mask has a single bit set */
799 if (!is_power_of_2(adr_next->mask))
802 if (index >= SDW_MAX_CPU_DAIS) {
803 dev_err(dev, " cpu_dai_id array overflows");
807 cpu_dai_id[index++] = ffs(adr_next->mask) - 1;
808 *codec_num += adr_next->num_adr;
812 * indicate CPU DAIs for this group have been generated
813 * to avoid generating CPU DAIs for this group again.
815 group_generated[*group_id] = true;
816 *cpu_dai_num = index;
821 static int create_sdw_dailink(struct snd_soc_card *card,
822 struct device *dev, int *be_index,
823 struct snd_soc_dai_link *dai_links,
824 int sdw_be_num, int sdw_cpu_dai_num,
825 struct snd_soc_dai_link_component *cpus,
826 const struct snd_soc_acpi_link_adr *link,
827 int *cpu_id, bool *group_generated,
828 struct snd_soc_codec_conf *codec_conf,
830 int *codec_conf_index,
831 bool *ignore_pch_dmic)
833 const struct snd_soc_acpi_link_adr *link_next;
834 struct snd_soc_dai_link_component *codecs;
835 int cpu_dai_id[SDW_MAX_CPU_DAIS];
836 int cpu_dai_num, cpu_dai_index;
837 unsigned int group_id;
846 ret = get_slave_info(link, dev, cpu_dai_id, &cpu_dai_num, &codec_num,
847 &group_id, group_generated);
851 codecs = devm_kcalloc(dev, codec_num, sizeof(*codecs), GFP_KERNEL);
855 /* generate codec name on different links in the same group */
856 for (link_next = link; link_next && link_next->num_adr &&
857 i < cpu_dai_num; link_next++) {
858 const struct snd_soc_acpi_endpoint *endpoints;
860 endpoints = link_next->adr_d->endpoints;
861 if (group_id && (!endpoints->aggregated ||
862 endpoints->group_id != group_id))
865 /* skip the link excluded by this processed group */
866 if (cpu_dai_id[i] != ffs(link_next->mask) - 1)
869 ret = create_codec_dai_name(dev, link_next, codecs, codec_idx,
870 codec_conf, codec_count, codec_conf_index);
874 /* check next link to create codec dai in the processed group */
876 codec_idx += link_next->num_adr;
879 /* find codec info to create BE DAI */
880 codec_index = find_codec_info_part(link->adr_d[0].adr);
884 if (codec_info_list[codec_index].ignore_pch_dmic)
885 *ignore_pch_dmic = true;
887 cpu_dai_index = *cpu_id;
888 for_each_pcm_streams(stream) {
889 char *name, *cpu_name;
890 int playback, capture;
891 static const char * const sdw_stream_name[] = {
896 if (!codec_info_list[codec_index].direction[stream])
899 /* create stream name according to first link id */
900 name = devm_kasprintf(dev, GFP_KERNEL,
901 sdw_stream_name[stream], cpu_dai_id[0]);
906 * generate CPU DAI name base on the sdw link ID and
907 * PIN ID with offset of 2 according to sdw dai driver.
909 for (k = 0; k < cpu_dai_num; k++) {
910 cpu_name = devm_kasprintf(dev, GFP_KERNEL,
911 "SDW%d Pin%d", cpu_dai_id[k],
912 j + SDW_INTEL_BIDIR_PDI_BASE);
916 if (cpu_dai_index >= sdw_cpu_dai_num) {
917 dev_err(dev, "invalid cpu dai index %d",
922 cpus[cpu_dai_index++].dai_name = cpu_name;
925 if (*be_index >= sdw_be_num) {
926 dev_err(dev, " invalid be dai index %d", *be_index);
930 if (*cpu_id >= sdw_cpu_dai_num) {
931 dev_err(dev, " invalid cpu dai index %d", *cpu_id);
935 playback = (stream == SNDRV_PCM_STREAM_PLAYBACK);
936 capture = (stream == SNDRV_PCM_STREAM_CAPTURE);
937 init_dai_link(dev, dai_links + *be_index, *be_index, name,
939 cpus + *cpu_id, cpu_dai_num,
943 * SoundWire DAILINKs use 'stream' functions and Bank Switch operations
944 * based on wait_for_completion(), tag them as 'nonatomic'.
946 dai_links[*be_index].nonatomic = true;
948 ret = set_codec_init_func(card, link, dai_links + (*be_index)++,
951 dev_err(dev, "failed to init codec %d", codec_index);
955 *cpu_id += cpu_dai_num;
963 * DAI link ID of SSP & DMIC & HDMI are based on last
964 * link ID used by sdw link. Since be_id may be changed
965 * in init func of sdw codec, it is not equal to be_id
967 static inline int get_next_be_id(struct snd_soc_dai_link *links,
970 return links[be_id - 1].id + 1;
973 #define IDISP_CODEC_MASK 0x4
975 static int sof_card_codec_conf_alloc(struct device *dev,
976 struct snd_soc_acpi_mach_params *mach_params,
977 struct snd_soc_codec_conf **codec_conf,
978 int *codec_conf_count)
980 const struct snd_soc_acpi_link_adr *adr_link;
981 struct snd_soc_codec_conf *c_conf;
985 adr_link = mach_params->links;
989 /* generate DAI links by each sdw link */
990 for (; adr_link->num_adr; adr_link++) {
991 for (i = 0; i < adr_link->num_adr; i++) {
992 if (!adr_link->adr_d[i].name_prefix) {
993 dev_err(dev, "codec 0x%llx does not have a name prefix\n",
994 adr_link->adr_d[i].adr);
998 num_codecs += adr_link->num_adr;
1001 c_conf = devm_kzalloc(dev, num_codecs * sizeof(*c_conf), GFP_KERNEL);
1005 *codec_conf = c_conf;
1006 *codec_conf_count = num_codecs;
1011 static int sof_card_dai_links_create(struct device *dev,
1012 struct snd_soc_acpi_mach *mach,
1013 struct snd_soc_card *card)
1015 int ssp_num, sdw_be_num = 0, hdmi_num = 0, dmic_num;
1016 struct mc_private *ctx = snd_soc_card_get_drvdata(card);
1017 struct snd_soc_dai_link_component *idisp_components;
1018 struct snd_soc_dai_link_component *ssp_components;
1019 struct snd_soc_acpi_mach_params *mach_params;
1020 const struct snd_soc_acpi_link_adr *adr_link;
1021 struct snd_soc_dai_link_component *cpus;
1022 struct snd_soc_codec_conf *codec_conf;
1023 bool ignore_pch_dmic = false;
1024 int codec_conf_count;
1025 int codec_conf_index = 0;
1026 bool group_generated[SDW_MAX_GROUPS];
1027 int ssp_codec_index, ssp_mask;
1028 struct snd_soc_dai_link *links;
1029 int num_links, link_id = 0;
1030 char *name, *cpu_name;
1031 int total_cpu_dai_num;
1032 int sdw_cpu_dai_num;
1033 int i, j, be_id = 0;
1038 mach_params = &mach->mach_params;
1040 /* allocate codec conf, will be populated when dailinks are created */
1041 ret = sof_card_codec_conf_alloc(dev, mach_params, &codec_conf, &codec_conf_count);
1045 /* reset amp_num to ensure amp_num++ starts from 0 in each probe */
1046 for (i = 0; i < ARRAY_SIZE(codec_info_list); i++)
1047 codec_info_list[i].amp_num = 0;
1049 if (sof_sdw_quirk & SOF_SDW_TGL_HDMI)
1050 hdmi_num = SOF_TGL_HDMI_COUNT;
1052 hdmi_num = SOF_PRE_TGL_HDMI_COUNT;
1054 ssp_mask = SOF_SSP_GET_PORT(sof_sdw_quirk);
1056 * on generic tgl platform, I2S or sdw mode is supported
1057 * based on board rework. A ACPI device is registered in
1058 * system only when I2S mode is supported, not sdw mode.
1059 * Here check ACPI ID to confirm I2S is supported.
1061 ssp_codec_index = find_codec_info_acpi(mach->id);
1062 ssp_num = ssp_codec_index >= 0 ? hweight_long(ssp_mask) : 0;
1063 comp_num = hdmi_num + ssp_num;
1065 ret = get_sdw_dailink_info(mach_params->links,
1066 &sdw_be_num, &sdw_cpu_dai_num);
1068 dev_err(dev, "failed to get sdw link info %d", ret);
1072 if (mach_params->codec_mask & IDISP_CODEC_MASK)
1073 ctx->idisp_codec = true;
1075 /* enable dmic01 & dmic16k */
1076 dmic_num = (sof_sdw_quirk & SOF_SDW_PCH_DMIC || mach_params->dmic_num) ? 2 : 0;
1077 comp_num += dmic_num;
1079 if (sof_sdw_quirk & SOF_SSP_BT_OFFLOAD_PRESENT)
1082 dev_dbg(dev, "sdw %d, ssp %d, dmic %d, hdmi %d", sdw_be_num, ssp_num,
1083 dmic_num, ctx->idisp_codec ? hdmi_num : 0);
1085 /* allocate BE dailinks */
1086 num_links = comp_num + sdw_be_num;
1087 links = devm_kcalloc(dev, num_links, sizeof(*links), GFP_KERNEL);
1089 /* allocated CPU DAIs */
1090 total_cpu_dai_num = comp_num + sdw_cpu_dai_num;
1091 cpus = devm_kcalloc(dev, total_cpu_dai_num, sizeof(*cpus),
1094 if (!links || !cpus)
1101 adr_link = mach_params->links;
1106 * SoundWire Slaves aggregated in the same group may be
1107 * located on different hardware links. Clear array to indicate
1108 * CPU DAIs for this group have not been generated.
1110 for (i = 0; i < SDW_MAX_GROUPS; i++)
1111 group_generated[i] = false;
1113 /* generate DAI links by each sdw link */
1114 for (; adr_link->num_adr; adr_link++) {
1115 const struct snd_soc_acpi_endpoint *endpoint;
1117 endpoint = adr_link->adr_d->endpoints;
1118 if (endpoint->aggregated && !endpoint->group_id) {
1119 dev_err(dev, "invalid group id on link %x",
1124 /* this group has been generated */
1125 if (endpoint->aggregated &&
1126 group_generated[endpoint->group_id])
1129 ret = create_sdw_dailink(card, dev, &be_id, links, sdw_be_num,
1130 sdw_cpu_dai_num, cpus, adr_link,
1131 &cpu_id, group_generated,
1132 codec_conf, codec_conf_count,
1136 dev_err(dev, "failed to create dai link %d", be_id);
1141 /* non-sdw DAI follows sdw DAI */
1144 /* get BE ID for non-sdw DAI */
1145 be_id = get_next_be_id(links, be_id);
1152 for (i = 0, j = 0; ssp_mask; i++, ssp_mask >>= 1) {
1153 struct sof_sdw_codec_info *info;
1154 int playback, capture;
1157 if (!(ssp_mask & 0x1))
1160 name = devm_kasprintf(dev, GFP_KERNEL,
1165 cpu_name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d Pin", i);
1169 ssp_components = devm_kzalloc(dev, sizeof(*ssp_components),
1171 if (!ssp_components)
1174 info = &codec_info_list[ssp_codec_index];
1175 codec_name = devm_kasprintf(dev, GFP_KERNEL, "i2c-%s:0%d",
1176 info->acpi_id, j++);
1180 ssp_components->name = codec_name;
1181 ssp_components->dai_name = info->dai_name;
1182 cpus[cpu_id].dai_name = cpu_name;
1184 playback = info->direction[SNDRV_PCM_STREAM_PLAYBACK];
1185 capture = info->direction[SNDRV_PCM_STREAM_CAPTURE];
1186 init_dai_link(dev, links + link_id, be_id, name,
1192 ret = info->init(card, NULL, links + link_id, info, 0);
1196 INC_ID(be_id, cpu_id, link_id);
1202 if (ignore_pch_dmic) {
1203 dev_warn(dev, "Ignoring PCH DMIC\n");
1206 cpus[cpu_id].dai_name = "DMIC01 Pin";
1207 init_dai_link(dev, links + link_id, be_id, "dmic01",
1208 0, 1, // DMIC only supports capture
1211 sof_sdw_dmic_init, NULL);
1212 INC_ID(be_id, cpu_id, link_id);
1214 cpus[cpu_id].dai_name = "DMIC16k Pin";
1215 init_dai_link(dev, links + link_id, be_id, "dmic16k",
1216 0, 1, // DMIC only supports capture
1219 /* don't call sof_sdw_dmic_init() twice */
1221 INC_ID(be_id, cpu_id, link_id);
1227 idisp_components = devm_kcalloc(dev, hdmi_num,
1228 sizeof(*idisp_components),
1230 if (!idisp_components)
1234 for (i = 0; i < hdmi_num; i++) {
1235 name = devm_kasprintf(dev, GFP_KERNEL,
1240 if (ctx->idisp_codec) {
1241 idisp_components[i].name = "ehdaudio0D2";
1242 idisp_components[i].dai_name = devm_kasprintf(dev,
1244 "intel-hdmi-hifi%d",
1246 if (!idisp_components[i].dai_name)
1249 idisp_components[i].name = "snd-soc-dummy";
1250 idisp_components[i].dai_name = "snd-soc-dummy-dai";
1253 cpu_name = devm_kasprintf(dev, GFP_KERNEL,
1254 "iDisp%d Pin", i + 1);
1258 cpus[cpu_id].dai_name = cpu_name;
1259 init_dai_link(dev, links + link_id, be_id, name,
1260 1, 0, // HDMI only supports playback
1262 idisp_components + i, 1,
1263 sof_sdw_hdmi_init, NULL);
1264 INC_ID(be_id, cpu_id, link_id);
1267 if (sof_sdw_quirk & SOF_SSP_BT_OFFLOAD_PRESENT) {
1268 int port = (sof_sdw_quirk & SOF_BT_OFFLOAD_SSP_MASK) >>
1269 SOF_BT_OFFLOAD_SSP_SHIFT;
1271 name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-BT", port);
1275 ssp_components = devm_kzalloc(dev, sizeof(*ssp_components),
1277 if (!ssp_components)
1280 ssp_components->name = "snd-soc-dummy";
1281 ssp_components->dai_name = "snd-soc-dummy-dai";
1283 cpu_name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d Pin", port);
1287 cpus[cpu_id].dai_name = cpu_name;
1288 init_dai_link(dev, links + link_id, be_id, name, 1, 1,
1289 cpus + cpu_id, 1, ssp_components, 1, NULL, NULL);
1292 card->dai_link = links;
1293 card->num_links = num_links;
1295 card->codec_conf = codec_conf;
1296 card->num_configs = codec_conf_count;
1301 static int sof_sdw_card_late_probe(struct snd_soc_card *card)
1305 for (i = 0; i < ARRAY_SIZE(codec_info_list); i++) {
1306 if (!codec_info_list[i].late_probe)
1309 ret = codec_info_list[i].codec_card_late_probe(card);
1314 return sof_sdw_hdmi_card_late_probe(card);
1318 static const char sdw_card_long_name[] = "Intel Soundwire SOF";
1320 static struct snd_soc_card card_sof_sdw = {
1321 .name = "soundwire",
1322 .owner = THIS_MODULE,
1323 .late_probe = sof_sdw_card_late_probe,
1326 static void mc_dailink_exit_loop(struct snd_soc_card *card)
1328 struct snd_soc_dai_link *link;
1332 for (i = 0; i < ARRAY_SIZE(codec_info_list); i++) {
1333 if (!codec_info_list[i].exit)
1336 * We don't need to call .exit function if there is no matched
1339 for_each_card_prelinks(card, j, link) {
1340 if (!strcmp(link->codecs[0].dai_name,
1341 codec_info_list[i].dai_name)) {
1342 ret = codec_info_list[i].exit(card, link);
1345 "codec exit failed %d\n",
1353 static int mc_probe(struct platform_device *pdev)
1355 struct snd_soc_card *card = &card_sof_sdw;
1356 struct snd_soc_acpi_mach *mach;
1357 struct mc_private *ctx;
1361 dev_dbg(&pdev->dev, "Entry %s\n", __func__);
1363 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
1367 dmi_check_system(sof_sdw_quirk_table);
1369 if (quirk_override != -1) {
1370 dev_info(&pdev->dev, "Overriding quirk 0x%lx => 0x%x\n",
1371 sof_sdw_quirk, quirk_override);
1372 sof_sdw_quirk = quirk_override;
1374 log_quirks(&pdev->dev);
1376 INIT_LIST_HEAD(&ctx->hdmi_pcm_list);
1378 card->dev = &pdev->dev;
1379 snd_soc_card_set_drvdata(card, ctx);
1381 mach = pdev->dev.platform_data;
1382 ret = sof_card_dai_links_create(&pdev->dev, mach,
1388 * the default amp_num is zero for each codec and
1389 * amp_num will only be increased for active amp
1390 * codecs on used platform
1392 for (i = 0; i < ARRAY_SIZE(codec_info_list); i++)
1393 amp_num += codec_info_list[i].amp_num;
1395 card->components = devm_kasprintf(card->dev, GFP_KERNEL,
1396 "cfg-spk:%d cfg-amp:%d",
1397 (sof_sdw_quirk & SOF_SDW_FOUR_SPK)
1399 if (!card->components)
1402 if (mach->mach_params.dmic_num) {
1403 card->components = devm_kasprintf(card->dev, GFP_KERNEL,
1404 "%s mic:dmic cfg-mics:%d",
1406 mach->mach_params.dmic_num);
1407 if (!card->components)
1411 card->long_name = sdw_card_long_name;
1413 /* Register the card */
1414 ret = devm_snd_soc_register_card(&pdev->dev, card);
1416 dev_err(card->dev, "snd_soc_register_card failed %d\n", ret);
1417 mc_dailink_exit_loop(card);
1421 platform_set_drvdata(pdev, card);
1426 static int mc_remove(struct platform_device *pdev)
1428 struct snd_soc_card *card = platform_get_drvdata(pdev);
1430 mc_dailink_exit_loop(card);
1435 static struct platform_driver sof_sdw_driver = {
1438 .pm = &snd_soc_pm_ops,
1441 .remove = mc_remove,
1444 module_platform_driver(sof_sdw_driver);
1446 MODULE_DESCRIPTION("ASoC SoundWire Generic Machine driver");
1447 MODULE_AUTHOR("Bard Liao <yung-chuan.liao@linux.intel.com>");
1448 MODULE_AUTHOR("Rander Wang <rander.wang@linux.intel.com>");
1449 MODULE_AUTHOR("Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>");
1450 MODULE_LICENSE("GPL v2");
1451 MODULE_ALIAS("platform:sof_sdw");
1452 MODULE_IMPORT_NS(SND_SOC_INTEL_HDA_DSP_COMMON);
1453 MODULE_IMPORT_NS(SND_SOC_INTEL_SOF_MAXIM_COMMON);