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, "0AF3"),
256 .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, "0B00")
265 .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, "0B01")
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, "0B11")
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, "0B12")
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, "0B13"),
306 .driver_data = (void *)SOF_SDW_TGL_HDMI,
309 .callback = sof_sdw_quirk_cb,
311 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
312 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0B29"),
314 .driver_data = (void *)(SOF_SDW_TGL_HDMI |
321 static struct snd_soc_dai_link_component dmic_component[] = {
323 .name = "dmic-codec",
324 .dai_name = "dmic-hifi",
328 static struct snd_soc_dai_link_component platform_component[] = {
330 /* name might be overridden during probe */
331 .name = "0000:00:1f.3"
335 /* these wrappers are only needed to avoid typecast compilation errors */
336 int sdw_startup(struct snd_pcm_substream *substream)
338 return sdw_startup_stream(substream);
341 int sdw_prepare(struct snd_pcm_substream *substream)
343 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
344 struct sdw_stream_runtime *sdw_stream;
345 struct snd_soc_dai *dai;
347 /* Find stream from first CPU DAI */
348 dai = asoc_rtd_to_cpu(rtd, 0);
350 sdw_stream = snd_soc_dai_get_stream(dai, substream->stream);
352 if (IS_ERR(sdw_stream)) {
353 dev_err(rtd->dev, "no stream found for DAI %s", dai->name);
354 return PTR_ERR(sdw_stream);
357 return sdw_prepare_stream(sdw_stream);
360 int sdw_trigger(struct snd_pcm_substream *substream, int cmd)
362 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
363 struct sdw_stream_runtime *sdw_stream;
364 struct snd_soc_dai *dai;
367 /* Find stream from first CPU DAI */
368 dai = asoc_rtd_to_cpu(rtd, 0);
370 sdw_stream = snd_soc_dai_get_stream(dai, substream->stream);
372 if (IS_ERR(sdw_stream)) {
373 dev_err(rtd->dev, "no stream found for DAI %s", dai->name);
374 return PTR_ERR(sdw_stream);
378 case SNDRV_PCM_TRIGGER_START:
379 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
380 case SNDRV_PCM_TRIGGER_RESUME:
381 ret = sdw_enable_stream(sdw_stream);
384 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
385 case SNDRV_PCM_TRIGGER_SUSPEND:
386 case SNDRV_PCM_TRIGGER_STOP:
387 ret = sdw_disable_stream(sdw_stream);
395 dev_err(rtd->dev, "%s trigger %d failed: %d", __func__, cmd, ret);
400 int sdw_hw_free(struct snd_pcm_substream *substream)
402 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
403 struct sdw_stream_runtime *sdw_stream;
404 struct snd_soc_dai *dai;
406 /* Find stream from first CPU DAI */
407 dai = asoc_rtd_to_cpu(rtd, 0);
409 sdw_stream = snd_soc_dai_get_stream(dai, substream->stream);
411 if (IS_ERR(sdw_stream)) {
412 dev_err(rtd->dev, "no stream found for DAI %s", dai->name);
413 return PTR_ERR(sdw_stream);
416 return sdw_deprepare_stream(sdw_stream);
419 void sdw_shutdown(struct snd_pcm_substream *substream)
421 sdw_shutdown_stream(substream);
424 static const struct snd_soc_ops sdw_ops = {
425 .startup = sdw_startup,
426 .prepare = sdw_prepare,
427 .trigger = sdw_trigger,
428 .hw_free = sdw_hw_free,
429 .shutdown = sdw_shutdown,
432 static struct sof_sdw_codec_info codec_info_list[] = {
435 .direction = {true, true},
436 .dai_name = "rt700-aif1",
437 .init = sof_sdw_rt700_init,
438 .codec_type = SOF_SDW_CODEC_TYPE_JACK,
443 .direction = {true, true},
444 .dai_name = "rt711-sdca-aif1",
445 .init = sof_sdw_rt711_sdca_init,
446 .exit = sof_sdw_rt711_sdca_exit,
447 .codec_type = SOF_SDW_CODEC_TYPE_JACK,
452 .direction = {true, true},
453 .dai_name = "rt711-aif1",
454 .init = sof_sdw_rt711_init,
455 .exit = sof_sdw_rt711_exit,
456 .codec_type = SOF_SDW_CODEC_TYPE_JACK,
460 .acpi_id = "10EC1308",
461 .direction = {true, false},
462 .dai_name = "rt1308-aif",
463 .ops = &sof_sdw_rt1308_i2s_ops,
464 .init = sof_sdw_rt1308_init,
465 .codec_type = SOF_SDW_CODEC_TYPE_AMP,
469 .direction = {true, true},
470 .dai_name = "rt1316-aif",
471 .init = sof_sdw_rt1316_init,
472 .codec_type = SOF_SDW_CODEC_TYPE_AMP,
477 .direction = {false, true},
478 .ignore_pch_dmic = true,
479 .dai_name = "rt715-aif2",
480 .init = sof_sdw_rt715_sdca_init,
481 .codec_type = SOF_SDW_CODEC_TYPE_MIC,
486 .direction = {false, true},
487 .ignore_pch_dmic = true,
488 .dai_name = "rt715-aif2",
489 .init = sof_sdw_rt715_sdca_init,
490 .codec_type = SOF_SDW_CODEC_TYPE_MIC,
495 .direction = {false, true},
496 .ignore_pch_dmic = true,
497 .dai_name = "rt715-aif2",
498 .init = sof_sdw_rt715_init,
499 .codec_type = SOF_SDW_CODEC_TYPE_MIC,
504 .direction = {false, true},
505 .ignore_pch_dmic = true,
506 .dai_name = "rt715-aif2",
507 .init = sof_sdw_rt715_init,
508 .codec_type = SOF_SDW_CODEC_TYPE_MIC,
512 .direction = {true, true},
513 .dai_name = "max98373-aif1",
514 .init = sof_sdw_mx8373_init,
515 .codec_card_late_probe = sof_sdw_mx8373_late_probe,
516 .codec_type = SOF_SDW_CODEC_TYPE_AMP,
520 .direction = {true, true},
521 .dai_name = "rt5682-sdw",
522 .init = sof_sdw_rt5682_init,
523 .codec_type = SOF_SDW_CODEC_TYPE_JACK,
526 .part_id = 0xaaaa, /* generic codec mockup */
528 .direction = {true, true},
529 .dai_name = "sdw-mockup-aif1",
531 .codec_type = SOF_SDW_CODEC_TYPE_JACK,
534 .part_id = 0xaa55, /* headset codec mockup */
536 .direction = {true, true},
537 .dai_name = "sdw-mockup-aif1",
539 .codec_type = SOF_SDW_CODEC_TYPE_JACK,
542 .part_id = 0x55aa, /* amplifier mockup */
544 .direction = {true, false},
545 .dai_name = "sdw-mockup-aif1",
547 .codec_type = SOF_SDW_CODEC_TYPE_AMP,
552 .direction = {false, true},
553 .dai_name = "sdw-mockup-aif1",
554 .codec_type = SOF_SDW_CODEC_TYPE_MIC,
558 static inline int find_codec_info_part(u64 adr)
560 unsigned int part_id, sdw_version;
563 part_id = SDW_PART_ID(adr);
564 sdw_version = SDW_VERSION(adr);
565 for (i = 0; i < ARRAY_SIZE(codec_info_list); i++)
567 * A codec info is for all sdw version with the part id if
568 * version_id is not specified in the codec info.
570 if (part_id == codec_info_list[i].part_id &&
571 (!codec_info_list[i].version_id ||
572 sdw_version == codec_info_list[i].version_id))
579 static inline int find_codec_info_acpi(const u8 *acpi_id)
586 for (i = 0; i < ARRAY_SIZE(codec_info_list); i++)
587 if (!memcmp(codec_info_list[i].acpi_id, acpi_id,
591 if (i == ARRAY_SIZE(codec_info_list))
598 * get BE dailink number and CPU DAI number based on sdw link adr.
599 * Since some sdw slaves may be aggregated, the CPU DAI number
600 * may be larger than the number of BE dailinks.
602 static int get_sdw_dailink_info(struct device *dev, const struct snd_soc_acpi_link_adr *links,
603 int *sdw_be_num, int *sdw_cpu_dai_num)
605 const struct snd_soc_acpi_link_adr *link;
606 int _codec_type = SOF_SDW_CODEC_TYPE_JACK;
607 bool group_visited[SDW_MAX_GROUPS];
611 no_aggregation = sof_sdw_quirk & SOF_SDW_NO_AGGREGATION;
612 *sdw_cpu_dai_num = 0;
618 for (i = 0; i < SDW_MAX_GROUPS; i++)
619 group_visited[i] = false;
621 for (link = links; link->num_adr; link++) {
622 const struct snd_soc_acpi_endpoint *endpoint;
627 adr = link->adr_d->adr;
628 codec_index = find_codec_info_part(adr);
632 if (codec_info_list[codec_index].codec_type < _codec_type)
634 "Unexpected address table ordering. Expected order: jack -> amp -> mic\n");
636 _codec_type = codec_info_list[codec_index].codec_type;
638 endpoint = link->adr_d->endpoints;
640 /* count DAI number for playback and capture */
641 for_each_pcm_streams(stream) {
642 if (!codec_info_list[codec_index].direction[stream])
645 (*sdw_cpu_dai_num)++;
647 /* count BE for each non-aggregated slave or group */
648 if (!endpoint->aggregated || no_aggregation ||
649 !group_visited[endpoint->group_id])
653 if (endpoint->aggregated)
654 group_visited[endpoint->group_id] = true;
660 static void init_dai_link(struct device *dev, struct snd_soc_dai_link *dai_links,
661 int be_id, char *name, int playback, int capture,
662 struct snd_soc_dai_link_component *cpus, int cpus_num,
663 struct snd_soc_dai_link_component *codecs, int codecs_num,
664 int (*init)(struct snd_soc_pcm_runtime *rtd),
665 const struct snd_soc_ops *ops)
667 dev_dbg(dev, "create dai link %s, id %d\n", name, be_id);
668 dai_links->id = be_id;
669 dai_links->name = name;
670 dai_links->platforms = platform_component;
671 dai_links->num_platforms = ARRAY_SIZE(platform_component);
672 dai_links->no_pcm = 1;
673 dai_links->cpus = cpus;
674 dai_links->num_cpus = cpus_num;
675 dai_links->codecs = codecs;
676 dai_links->num_codecs = codecs_num;
677 dai_links->dpcm_playback = playback;
678 dai_links->dpcm_capture = capture;
679 dai_links->init = init;
680 dai_links->ops = ops;
683 static bool is_unique_device(const struct snd_soc_acpi_link_adr *link,
684 unsigned int sdw_version,
686 unsigned int part_id,
687 unsigned int class_id,
693 for (i = 0; i < link->num_adr; i++) {
694 unsigned int sdw1_version, mfg1_id, part1_id, class1_id;
698 if (i == index_in_link)
701 adr = link->adr_d[i].adr;
703 sdw1_version = SDW_VERSION(adr);
704 mfg1_id = SDW_MFG_ID(adr);
705 part1_id = SDW_PART_ID(adr);
706 class1_id = SDW_CLASS_ID(adr);
708 if (sdw_version == sdw1_version &&
710 part_id == part1_id &&
711 class_id == class1_id)
718 static int create_codec_dai_name(struct device *dev,
719 const struct snd_soc_acpi_link_adr *link,
720 struct snd_soc_dai_link_component *codec,
722 struct snd_soc_codec_conf *codec_conf,
724 int *codec_conf_index)
729 if (*codec_conf_index + link->num_adr > codec_count) {
730 dev_err(dev, "codec_conf: out-of-bounds access requested\n");
734 for (i = 0; i < link->num_adr; i++) {
735 unsigned int sdw_version, unique_id, mfg_id;
736 unsigned int link_id, part_id, class_id;
737 int codec_index, comp_index;
741 adr = link->adr_d[i].adr;
743 sdw_version = SDW_VERSION(adr);
744 link_id = SDW_DISCO_LINK_ID(adr);
745 unique_id = SDW_UNIQUE_ID(adr);
746 mfg_id = SDW_MFG_ID(adr);
747 part_id = SDW_PART_ID(adr);
748 class_id = SDW_CLASS_ID(adr);
750 comp_index = i + offset;
751 if (is_unique_device(link, sdw_version, mfg_id, part_id,
753 codec_str = "sdw:%01x:%04x:%04x:%02x";
754 codec[comp_index].name =
755 devm_kasprintf(dev, GFP_KERNEL, codec_str,
756 link_id, mfg_id, part_id,
759 codec_str = "sdw:%01x:%04x:%04x:%02x:%01x";
760 codec[comp_index].name =
761 devm_kasprintf(dev, GFP_KERNEL, codec_str,
762 link_id, mfg_id, part_id,
763 class_id, unique_id);
766 if (!codec[comp_index].name)
769 codec_index = find_codec_info_part(adr);
773 codec[comp_index].dai_name =
774 codec_info_list[codec_index].dai_name;
776 codec_conf[*codec_conf_index].dlc = codec[comp_index];
777 codec_conf[*codec_conf_index].name_prefix = link->adr_d[i].name_prefix;
785 static int set_codec_init_func(struct snd_soc_card *card,
786 const struct snd_soc_acpi_link_adr *link,
787 struct snd_soc_dai_link *dai_links,
788 bool playback, int group_id)
794 * Initialize the codec. If codec is part of an aggregated
795 * group (group_id>0), initialize all codecs belonging to
798 for (i = 0; i < link->num_adr; i++) {
801 codec_index = find_codec_info_part(link->adr_d[i].adr);
805 /* The group_id is > 0 iff the codec is aggregated */
806 if (link->adr_d[i].endpoints->group_id != group_id)
808 if (codec_info_list[codec_index].init)
809 codec_info_list[codec_index].init(card,
812 &codec_info_list[codec_index],
816 } while (link->mask && group_id);
822 * check endpoint status in slaves and gather link ID for all slaves in
823 * the same group to generate different CPU DAI. Now only support
824 * one sdw link with all slaves set with only single group id.
826 * one slave on one sdw link with aggregated = 0
827 * one sdw BE DAI <---> one-cpu DAI <---> one-codec DAI
829 * two or more slaves on one sdw link with aggregated = 0
830 * one sdw BE DAI <---> one-cpu DAI <---> multi-codec DAIs
832 * multiple links with multiple slaves with aggregated = 1
833 * one sdw BE DAI <---> 1 .. N CPU DAIs <----> 1 .. N codec DAIs
835 static int get_slave_info(const struct snd_soc_acpi_link_adr *adr_link,
836 struct device *dev, int *cpu_dai_id, int *cpu_dai_num,
837 int *codec_num, unsigned int *group_id,
838 bool *group_generated)
840 const struct snd_soc_acpi_adr_device *adr_d;
841 const struct snd_soc_acpi_link_adr *adr_next;
845 no_aggregation = sof_sdw_quirk & SOF_SDW_NO_AGGREGATION;
846 *codec_num = adr_link->num_adr;
847 adr_d = adr_link->adr_d;
849 /* make sure the link mask has a single bit set */
850 if (!is_power_of_2(adr_link->mask))
853 cpu_dai_id[index++] = ffs(adr_link->mask) - 1;
854 if (!adr_d->endpoints->aggregated || no_aggregation) {
860 *group_id = adr_d->endpoints->group_id;
862 /* gather other link ID of slaves in the same group */
863 for (adr_next = adr_link + 1; adr_next && adr_next->num_adr;
865 const struct snd_soc_acpi_endpoint *endpoint;
867 endpoint = adr_next->adr_d->endpoints;
868 if (!endpoint->aggregated ||
869 endpoint->group_id != *group_id)
872 /* make sure the link mask has a single bit set */
873 if (!is_power_of_2(adr_next->mask))
876 if (index >= SDW_MAX_CPU_DAIS) {
877 dev_err(dev, " cpu_dai_id array overflows");
881 cpu_dai_id[index++] = ffs(adr_next->mask) - 1;
882 *codec_num += adr_next->num_adr;
886 * indicate CPU DAIs for this group have been generated
887 * to avoid generating CPU DAIs for this group again.
889 group_generated[*group_id] = true;
890 *cpu_dai_num = index;
895 static int create_sdw_dailink(struct snd_soc_card *card,
896 struct device *dev, int *link_index,
897 struct snd_soc_dai_link *dai_links,
898 int sdw_be_num, int sdw_cpu_dai_num,
899 struct snd_soc_dai_link_component *cpus,
900 const struct snd_soc_acpi_link_adr *link,
901 int *cpu_id, bool *group_generated,
902 struct snd_soc_codec_conf *codec_conf,
903 int codec_count, int *link_id,
904 int *codec_conf_index,
905 bool *ignore_pch_dmic)
907 const struct snd_soc_acpi_link_adr *link_next;
908 struct snd_soc_dai_link_component *codecs;
909 int cpu_dai_id[SDW_MAX_CPU_DAIS];
910 int cpu_dai_num, cpu_dai_index;
911 unsigned int group_id;
920 ret = get_slave_info(link, dev, cpu_dai_id, &cpu_dai_num, &codec_num,
921 &group_id, group_generated);
925 codecs = devm_kcalloc(dev, codec_num, sizeof(*codecs), GFP_KERNEL);
929 /* generate codec name on different links in the same group */
930 for (link_next = link; link_next && link_next->num_adr &&
931 i < cpu_dai_num; link_next++) {
932 const struct snd_soc_acpi_endpoint *endpoints;
934 endpoints = link_next->adr_d->endpoints;
935 if (group_id && (!endpoints->aggregated ||
936 endpoints->group_id != group_id))
939 /* skip the link excluded by this processed group */
940 if (cpu_dai_id[i] != ffs(link_next->mask) - 1)
943 ret = create_codec_dai_name(dev, link_next, codecs, codec_idx,
944 codec_conf, codec_count, codec_conf_index);
948 /* check next link to create codec dai in the processed group */
950 codec_idx += link_next->num_adr;
953 /* find codec info to create BE DAI */
954 codec_index = find_codec_info_part(link->adr_d[0].adr);
958 if (codec_info_list[codec_index].ignore_pch_dmic)
959 *ignore_pch_dmic = true;
961 /* Shift the first amplifier's *link_id to SDW_AMP_DAI_ID */
962 if (codec_info_list[codec_index].codec_type == SOF_SDW_CODEC_TYPE_AMP &&
963 *link_id < SDW_AMP_DAI_ID)
964 *link_id = SDW_AMP_DAI_ID;
967 * DAI ID is fixed at SDW_DMIC_DAI_ID for MICs to
968 * keep sdw DMIC and HDMI setting static in UCM
970 if (codec_info_list[codec_index].codec_type == SOF_SDW_CODEC_TYPE_MIC &&
971 *link_id < SDW_DMIC_DAI_ID)
972 *link_id = SDW_DMIC_DAI_ID;
974 cpu_dai_index = *cpu_id;
975 for_each_pcm_streams(stream) {
976 char *name, *cpu_name;
977 int playback, capture;
978 static const char * const sdw_stream_name[] = {
983 if (!codec_info_list[codec_index].direction[stream])
986 /* create stream name according to first link id */
987 name = devm_kasprintf(dev, GFP_KERNEL,
988 sdw_stream_name[stream], cpu_dai_id[0]);
993 * generate CPU DAI name base on the sdw link ID and
994 * PIN ID with offset of 2 according to sdw dai driver.
996 for (k = 0; k < cpu_dai_num; k++) {
997 cpu_name = devm_kasprintf(dev, GFP_KERNEL,
998 "SDW%d Pin%d", cpu_dai_id[k],
999 j + SDW_INTEL_BIDIR_PDI_BASE);
1003 if (cpu_dai_index >= sdw_cpu_dai_num) {
1004 dev_err(dev, "invalid cpu dai index %d",
1009 cpus[cpu_dai_index++].dai_name = cpu_name;
1013 * We create sdw dai links at first stage, so link index should
1014 * not be larger than sdw_be_num
1016 if (*link_index >= sdw_be_num) {
1017 dev_err(dev, "invalid dai link index %d", *link_index);
1021 if (*cpu_id >= sdw_cpu_dai_num) {
1022 dev_err(dev, " invalid cpu dai index %d", *cpu_id);
1026 playback = (stream == SNDRV_PCM_STREAM_PLAYBACK);
1027 capture = (stream == SNDRV_PCM_STREAM_CAPTURE);
1028 init_dai_link(dev, dai_links + *link_index, (*link_id)++, name,
1030 cpus + *cpu_id, cpu_dai_num,
1035 * SoundWire DAILINKs use 'stream' functions and Bank Switch operations
1036 * based on wait_for_completion(), tag them as 'nonatomic'.
1038 dai_links[*link_index].nonatomic = true;
1040 ret = set_codec_init_func(card, link, dai_links + (*link_index)++,
1041 playback, group_id);
1043 dev_err(dev, "failed to init codec %d", codec_index);
1047 *cpu_id += cpu_dai_num;
1054 #define IDISP_CODEC_MASK 0x4
1056 static int sof_card_codec_conf_alloc(struct device *dev,
1057 struct snd_soc_acpi_mach_params *mach_params,
1058 struct snd_soc_codec_conf **codec_conf,
1059 int *codec_conf_count)
1061 const struct snd_soc_acpi_link_adr *adr_link;
1062 struct snd_soc_codec_conf *c_conf;
1066 adr_link = mach_params->links;
1070 /* generate DAI links by each sdw link */
1071 for (; adr_link->num_adr; adr_link++) {
1072 for (i = 0; i < adr_link->num_adr; i++) {
1073 if (!adr_link->adr_d[i].name_prefix) {
1074 dev_err(dev, "codec 0x%llx does not have a name prefix\n",
1075 adr_link->adr_d[i].adr);
1079 num_codecs += adr_link->num_adr;
1082 c_conf = devm_kzalloc(dev, num_codecs * sizeof(*c_conf), GFP_KERNEL);
1086 *codec_conf = c_conf;
1087 *codec_conf_count = num_codecs;
1092 static int sof_card_dai_links_create(struct device *dev,
1093 struct snd_soc_acpi_mach *mach,
1094 struct snd_soc_card *card)
1096 int ssp_num, sdw_be_num = 0, hdmi_num = 0, dmic_num;
1097 struct mc_private *ctx = snd_soc_card_get_drvdata(card);
1098 struct snd_soc_dai_link_component *idisp_components;
1099 struct snd_soc_dai_link_component *ssp_components;
1100 struct snd_soc_acpi_mach_params *mach_params;
1101 const struct snd_soc_acpi_link_adr *adr_link;
1102 struct snd_soc_dai_link_component *cpus;
1103 struct snd_soc_codec_conf *codec_conf;
1104 bool ignore_pch_dmic = false;
1105 int codec_conf_count;
1106 int codec_conf_index = 0;
1107 bool group_generated[SDW_MAX_GROUPS];
1108 int ssp_codec_index, ssp_mask;
1109 struct snd_soc_dai_link *links;
1110 int num_links, link_index = 0;
1111 char *name, *cpu_name;
1112 int total_cpu_dai_num;
1113 int sdw_cpu_dai_num;
1114 int i, j, be_id = 0;
1119 mach_params = &mach->mach_params;
1121 /* allocate codec conf, will be populated when dailinks are created */
1122 ret = sof_card_codec_conf_alloc(dev, mach_params, &codec_conf, &codec_conf_count);
1126 /* reset amp_num to ensure amp_num++ starts from 0 in each probe */
1127 for (i = 0; i < ARRAY_SIZE(codec_info_list); i++)
1128 codec_info_list[i].amp_num = 0;
1130 if (sof_sdw_quirk & SOF_SDW_TGL_HDMI)
1131 hdmi_num = SOF_TGL_HDMI_COUNT;
1133 hdmi_num = SOF_PRE_TGL_HDMI_COUNT;
1135 ssp_mask = SOF_SSP_GET_PORT(sof_sdw_quirk);
1137 * on generic tgl platform, I2S or sdw mode is supported
1138 * based on board rework. A ACPI device is registered in
1139 * system only when I2S mode is supported, not sdw mode.
1140 * Here check ACPI ID to confirm I2S is supported.
1142 ssp_codec_index = find_codec_info_acpi(mach->id);
1143 ssp_num = ssp_codec_index >= 0 ? hweight_long(ssp_mask) : 0;
1144 comp_num = hdmi_num + ssp_num;
1146 ret = get_sdw_dailink_info(dev, mach_params->links,
1147 &sdw_be_num, &sdw_cpu_dai_num);
1149 dev_err(dev, "failed to get sdw link info %d", ret);
1153 if (mach_params->codec_mask & IDISP_CODEC_MASK)
1154 ctx->idisp_codec = true;
1156 /* enable dmic01 & dmic16k */
1157 dmic_num = (sof_sdw_quirk & SOF_SDW_PCH_DMIC || mach_params->dmic_num) ? 2 : 0;
1158 comp_num += dmic_num;
1160 if (sof_sdw_quirk & SOF_SSP_BT_OFFLOAD_PRESENT)
1163 dev_dbg(dev, "sdw %d, ssp %d, dmic %d, hdmi %d", sdw_be_num, ssp_num,
1164 dmic_num, ctx->idisp_codec ? hdmi_num : 0);
1166 /* allocate BE dailinks */
1167 num_links = comp_num + sdw_be_num;
1168 links = devm_kcalloc(dev, num_links, sizeof(*links), GFP_KERNEL);
1170 /* allocated CPU DAIs */
1171 total_cpu_dai_num = comp_num + sdw_cpu_dai_num;
1172 cpus = devm_kcalloc(dev, total_cpu_dai_num, sizeof(*cpus),
1175 if (!links || !cpus)
1182 adr_link = mach_params->links;
1187 * SoundWire Slaves aggregated in the same group may be
1188 * located on different hardware links. Clear array to indicate
1189 * CPU DAIs for this group have not been generated.
1191 for (i = 0; i < SDW_MAX_GROUPS; i++)
1192 group_generated[i] = false;
1194 /* generate DAI links by each sdw link */
1195 for (; adr_link->num_adr; adr_link++) {
1196 const struct snd_soc_acpi_endpoint *endpoint;
1198 endpoint = adr_link->adr_d->endpoints;
1199 if (endpoint->aggregated && !endpoint->group_id) {
1200 dev_err(dev, "invalid group id on link %x",
1205 /* this group has been generated */
1206 if (endpoint->aggregated &&
1207 group_generated[endpoint->group_id])
1210 ret = create_sdw_dailink(card, dev, &link_index, links, sdw_be_num,
1211 sdw_cpu_dai_num, cpus, adr_link,
1212 &cpu_id, group_generated,
1213 codec_conf, codec_conf_count,
1214 &be_id, &codec_conf_index,
1217 dev_err(dev, "failed to create dai link %d", link_index);
1227 for (i = 0, j = 0; ssp_mask; i++, ssp_mask >>= 1) {
1228 struct sof_sdw_codec_info *info;
1229 int playback, capture;
1232 if (!(ssp_mask & 0x1))
1235 name = devm_kasprintf(dev, GFP_KERNEL,
1240 cpu_name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d Pin", i);
1244 ssp_components = devm_kzalloc(dev, sizeof(*ssp_components),
1246 if (!ssp_components)
1249 info = &codec_info_list[ssp_codec_index];
1250 codec_name = devm_kasprintf(dev, GFP_KERNEL, "i2c-%s:0%d",
1251 info->acpi_id, j++);
1255 ssp_components->name = codec_name;
1256 ssp_components->dai_name = info->dai_name;
1257 cpus[cpu_id].dai_name = cpu_name;
1259 playback = info->direction[SNDRV_PCM_STREAM_PLAYBACK];
1260 capture = info->direction[SNDRV_PCM_STREAM_CAPTURE];
1261 init_dai_link(dev, links + link_index, be_id, name,
1267 ret = info->init(card, NULL, links + link_index, info, 0);
1271 INC_ID(be_id, cpu_id, link_index);
1277 if (ignore_pch_dmic) {
1278 dev_warn(dev, "Ignoring PCH DMIC\n");
1281 cpus[cpu_id].dai_name = "DMIC01 Pin";
1282 init_dai_link(dev, links + link_index, be_id, "dmic01",
1283 0, 1, // DMIC only supports capture
1286 sof_sdw_dmic_init, NULL);
1287 INC_ID(be_id, cpu_id, link_index);
1289 cpus[cpu_id].dai_name = "DMIC16k Pin";
1290 init_dai_link(dev, links + link_index, be_id, "dmic16k",
1291 0, 1, // DMIC only supports capture
1294 /* don't call sof_sdw_dmic_init() twice */
1296 INC_ID(be_id, cpu_id, link_index);
1302 idisp_components = devm_kcalloc(dev, hdmi_num,
1303 sizeof(*idisp_components),
1305 if (!idisp_components)
1309 for (i = 0; i < hdmi_num; i++) {
1310 name = devm_kasprintf(dev, GFP_KERNEL,
1315 if (ctx->idisp_codec) {
1316 idisp_components[i].name = "ehdaudio0D2";
1317 idisp_components[i].dai_name = devm_kasprintf(dev,
1319 "intel-hdmi-hifi%d",
1321 if (!idisp_components[i].dai_name)
1324 idisp_components[i].name = "snd-soc-dummy";
1325 idisp_components[i].dai_name = "snd-soc-dummy-dai";
1328 cpu_name = devm_kasprintf(dev, GFP_KERNEL,
1329 "iDisp%d Pin", i + 1);
1333 cpus[cpu_id].dai_name = cpu_name;
1334 init_dai_link(dev, links + link_index, be_id, name,
1335 1, 0, // HDMI only supports playback
1337 idisp_components + i, 1,
1338 sof_sdw_hdmi_init, NULL);
1339 INC_ID(be_id, cpu_id, link_index);
1342 if (sof_sdw_quirk & SOF_SSP_BT_OFFLOAD_PRESENT) {
1343 int port = (sof_sdw_quirk & SOF_BT_OFFLOAD_SSP_MASK) >>
1344 SOF_BT_OFFLOAD_SSP_SHIFT;
1346 name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-BT", port);
1350 ssp_components = devm_kzalloc(dev, sizeof(*ssp_components),
1352 if (!ssp_components)
1355 ssp_components->name = "snd-soc-dummy";
1356 ssp_components->dai_name = "snd-soc-dummy-dai";
1358 cpu_name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d Pin", port);
1362 cpus[cpu_id].dai_name = cpu_name;
1363 init_dai_link(dev, links + link_index, be_id, name, 1, 1,
1364 cpus + cpu_id, 1, ssp_components, 1, NULL, NULL);
1367 card->dai_link = links;
1368 card->num_links = num_links;
1370 card->codec_conf = codec_conf;
1371 card->num_configs = codec_conf_count;
1376 static int sof_sdw_card_late_probe(struct snd_soc_card *card)
1380 for (i = 0; i < ARRAY_SIZE(codec_info_list); i++) {
1381 if (!codec_info_list[i].late_probe)
1384 ret = codec_info_list[i].codec_card_late_probe(card);
1389 return sof_sdw_hdmi_card_late_probe(card);
1393 static const char sdw_card_long_name[] = "Intel Soundwire SOF";
1395 static struct snd_soc_card card_sof_sdw = {
1396 .name = "soundwire",
1397 .owner = THIS_MODULE,
1398 .late_probe = sof_sdw_card_late_probe,
1401 static int mc_probe(struct platform_device *pdev)
1403 struct snd_soc_card *card = &card_sof_sdw;
1404 struct snd_soc_acpi_mach *mach;
1405 struct mc_private *ctx;
1409 dev_dbg(&pdev->dev, "Entry %s\n", __func__);
1411 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
1415 dmi_check_system(sof_sdw_quirk_table);
1417 if (quirk_override != -1) {
1418 dev_info(&pdev->dev, "Overriding quirk 0x%lx => 0x%x\n",
1419 sof_sdw_quirk, quirk_override);
1420 sof_sdw_quirk = quirk_override;
1422 log_quirks(&pdev->dev);
1424 INIT_LIST_HEAD(&ctx->hdmi_pcm_list);
1426 card->dev = &pdev->dev;
1427 snd_soc_card_set_drvdata(card, ctx);
1429 mach = pdev->dev.platform_data;
1430 ret = sof_card_dai_links_create(&pdev->dev, mach,
1436 * the default amp_num is zero for each codec and
1437 * amp_num will only be increased for active amp
1438 * codecs on used platform
1440 for (i = 0; i < ARRAY_SIZE(codec_info_list); i++)
1441 amp_num += codec_info_list[i].amp_num;
1443 card->components = devm_kasprintf(card->dev, GFP_KERNEL,
1444 "cfg-spk:%d cfg-amp:%d",
1445 (sof_sdw_quirk & SOF_SDW_FOUR_SPK)
1447 if (!card->components)
1450 if (mach->mach_params.dmic_num) {
1451 card->components = devm_kasprintf(card->dev, GFP_KERNEL,
1452 "%s mic:dmic cfg-mics:%d",
1454 mach->mach_params.dmic_num);
1455 if (!card->components)
1459 card->long_name = sdw_card_long_name;
1461 /* Register the card */
1462 ret = devm_snd_soc_register_card(&pdev->dev, card);
1464 dev_err(card->dev, "snd_soc_register_card failed %d\n", ret);
1468 platform_set_drvdata(pdev, card);
1473 static int mc_remove(struct platform_device *pdev)
1475 struct snd_soc_card *card = platform_get_drvdata(pdev);
1476 struct snd_soc_dai_link *link;
1480 for (i = 0; i < ARRAY_SIZE(codec_info_list); i++) {
1481 if (!codec_info_list[i].exit)
1484 * We don't need to call .exit function if there is no matched
1487 for_each_card_prelinks(card, j, link) {
1488 if (!strcmp(link->codecs[0].dai_name,
1489 codec_info_list[i].dai_name)) {
1490 ret = codec_info_list[i].exit(card, link);
1492 dev_warn(&pdev->dev,
1493 "codec exit failed %d\n",
1503 static struct platform_driver sof_sdw_driver = {
1506 .pm = &snd_soc_pm_ops,
1509 .remove = mc_remove,
1512 module_platform_driver(sof_sdw_driver);
1514 MODULE_DESCRIPTION("ASoC SoundWire Generic Machine driver");
1515 MODULE_AUTHOR("Bard Liao <yung-chuan.liao@linux.intel.com>");
1516 MODULE_AUTHOR("Rander Wang <rander.wang@linux.intel.com>");
1517 MODULE_AUTHOR("Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>");
1518 MODULE_LICENSE("GPL v2");
1519 MODULE_ALIAS("platform:sof_sdw");
1520 MODULE_IMPORT_NS(SND_SOC_INTEL_HDA_DSP_COMMON);
1521 MODULE_IMPORT_NS(SND_SOC_INTEL_SOF_MAXIM_COMMON);