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"
17 unsigned long sof_sdw_quirk = SOF_RT711_JD_SRC_JD1;
18 static int quirk_override = -1;
19 module_param_named(quirk, quirk_override, int, 0444);
20 MODULE_PARM_DESC(quirk, "Board-specific quirk override");
22 #define INC_ID(BE, CPU, LINK) do { (BE)++; (CPU)++; (LINK)++; } while (0)
24 static void log_quirks(struct device *dev)
26 if (SOF_RT711_JDSRC(sof_sdw_quirk))
27 dev_dbg(dev, "quirk realtek,jack-detect-source %ld\n",
28 SOF_RT711_JDSRC(sof_sdw_quirk));
29 if (sof_sdw_quirk & SOF_SDW_FOUR_SPK)
30 dev_dbg(dev, "quirk SOF_SDW_FOUR_SPK enabled\n");
31 if (sof_sdw_quirk & SOF_SDW_TGL_HDMI)
32 dev_dbg(dev, "quirk SOF_SDW_TGL_HDMI enabled\n");
33 if (sof_sdw_quirk & SOF_SDW_PCH_DMIC)
34 dev_dbg(dev, "quirk SOF_SDW_PCH_DMIC enabled\n");
35 if (SOF_SSP_GET_PORT(sof_sdw_quirk))
36 dev_dbg(dev, "SSP port %ld\n",
37 SOF_SSP_GET_PORT(sof_sdw_quirk));
38 if (sof_sdw_quirk & SOF_RT715_DAI_ID_FIX)
39 dev_dbg(dev, "quirk SOF_RT715_DAI_ID_FIX enabled\n");
40 if (sof_sdw_quirk & SOF_SDW_NO_AGGREGATION)
41 dev_dbg(dev, "quirk SOF_SDW_NO_AGGREGATION enabled\n");
44 static int sof_sdw_quirk_cb(const struct dmi_system_id *id)
46 sof_sdw_quirk = (unsigned long)id->driver_data;
50 static const struct dmi_system_id sof_sdw_quirk_table[] = {
51 /* CometLake devices */
53 .callback = sof_sdw_quirk_cb,
55 DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
56 DMI_MATCH(DMI_PRODUCT_NAME, "CometLake Client"),
58 .driver_data = (void *)SOF_SDW_PCH_DMIC,
61 .callback = sof_sdw_quirk_cb,
63 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
64 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "09C6")
66 .driver_data = (void *)(SOF_RT711_JD_SRC_JD2 |
67 SOF_RT715_DAI_ID_FIX),
70 /* early version of SKU 09C6 */
71 .callback = sof_sdw_quirk_cb,
73 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
74 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0983")
76 .driver_data = (void *)(SOF_RT711_JD_SRC_JD2 |
77 SOF_RT715_DAI_ID_FIX),
80 .callback = sof_sdw_quirk_cb,
82 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
83 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "098F"),
85 .driver_data = (void *)(SOF_RT711_JD_SRC_JD2 |
86 SOF_RT715_DAI_ID_FIX |
90 .callback = sof_sdw_quirk_cb,
92 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
93 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0990"),
95 .driver_data = (void *)(SOF_RT711_JD_SRC_JD2 |
96 SOF_RT715_DAI_ID_FIX |
101 .callback = sof_sdw_quirk_cb,
103 DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
104 DMI_MATCH(DMI_PRODUCT_NAME, "Ice Lake Client"),
106 .driver_data = (void *)SOF_SDW_PCH_DMIC,
108 /* TigerLake devices */
110 .callback = sof_sdw_quirk_cb,
112 DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
113 DMI_MATCH(DMI_PRODUCT_NAME,
114 "Tiger Lake Client Platform"),
116 .driver_data = (void *)(SOF_SDW_TGL_HDMI |
117 SOF_RT711_JD_SRC_JD1 |
119 SOF_SSP_PORT(SOF_I2S_SSP2)),
122 .callback = sof_sdw_quirk_cb,
124 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
125 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0A3E")
127 .driver_data = (void *)(SOF_SDW_TGL_HDMI |
128 SOF_RT711_JD_SRC_JD2 |
129 SOF_RT715_DAI_ID_FIX),
132 .callback = sof_sdw_quirk_cb,
134 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
135 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0A5E")
137 .driver_data = (void *)(SOF_SDW_TGL_HDMI |
138 SOF_RT711_JD_SRC_JD2 |
139 SOF_RT715_DAI_ID_FIX |
143 .callback = sof_sdw_quirk_cb,
145 DMI_MATCH(DMI_SYS_VENDOR, "Google"),
146 DMI_MATCH(DMI_PRODUCT_NAME, "Volteer"),
148 .driver_data = (void *)(SOF_SDW_TGL_HDMI |
153 .callback = sof_sdw_quirk_cb,
155 DMI_MATCH(DMI_SYS_VENDOR, "Google"),
156 DMI_MATCH(DMI_PRODUCT_NAME, "Ripto"),
158 .driver_data = (void *)(SOF_SDW_TGL_HDMI |
164 * this entry covers multiple HP SKUs. The family name
165 * does not seem robust enough, so we use a partial
166 * match that ignores the product name suffix
167 * (e.g. 15-eb1xxx, 14t-ea000 or 13-aw2xxx)
169 .callback = sof_sdw_quirk_cb,
171 DMI_MATCH(DMI_SYS_VENDOR, "HP"),
172 DMI_MATCH(DMI_PRODUCT_NAME, "HP Spectre x360 Convertible"),
174 .driver_data = (void *)(SOF_SDW_TGL_HDMI |
176 SOF_RT711_JD_SRC_JD2),
178 /* TigerLake-SDCA devices */
180 .callback = sof_sdw_quirk_cb,
182 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
183 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0A32")
185 .driver_data = (void *)(SOF_SDW_TGL_HDMI |
186 SOF_RT711_JD_SRC_JD2 |
187 SOF_RT715_DAI_ID_FIX |
190 /* AlderLake devices */
192 .callback = sof_sdw_quirk_cb,
194 DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
195 DMI_MATCH(DMI_PRODUCT_NAME, "Alder Lake Client Platform"),
197 .driver_data = (void *)(SOF_RT711_JD_SRC_JD1 |
204 static struct snd_soc_dai_link_component dmic_component[] = {
206 .name = "dmic-codec",
207 .dai_name = "dmic-hifi",
211 static struct snd_soc_dai_link_component platform_component[] = {
213 /* name might be overridden during probe */
214 .name = "0000:00:1f.3"
218 /* these wrappers are only needed to avoid typecast compilation errors */
219 int sdw_startup(struct snd_pcm_substream *substream)
221 return sdw_startup_stream(substream);
224 int sdw_prepare(struct snd_pcm_substream *substream)
226 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
227 struct sdw_stream_runtime *sdw_stream;
228 struct snd_soc_dai *dai;
230 /* Find stream from first CPU DAI */
231 dai = asoc_rtd_to_cpu(rtd, 0);
233 sdw_stream = snd_soc_dai_get_sdw_stream(dai, substream->stream);
235 if (IS_ERR(sdw_stream)) {
236 dev_err(rtd->dev, "no stream found for DAI %s", dai->name);
237 return PTR_ERR(sdw_stream);
240 return sdw_prepare_stream(sdw_stream);
243 int sdw_trigger(struct snd_pcm_substream *substream, int cmd)
245 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
246 struct sdw_stream_runtime *sdw_stream;
247 struct snd_soc_dai *dai;
250 /* Find stream from first CPU DAI */
251 dai = asoc_rtd_to_cpu(rtd, 0);
253 sdw_stream = snd_soc_dai_get_sdw_stream(dai, substream->stream);
255 if (IS_ERR(sdw_stream)) {
256 dev_err(rtd->dev, "no stream found for DAI %s", dai->name);
257 return PTR_ERR(sdw_stream);
261 case SNDRV_PCM_TRIGGER_START:
262 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
263 case SNDRV_PCM_TRIGGER_RESUME:
264 ret = sdw_enable_stream(sdw_stream);
267 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
268 case SNDRV_PCM_TRIGGER_SUSPEND:
269 case SNDRV_PCM_TRIGGER_STOP:
270 ret = sdw_disable_stream(sdw_stream);
278 dev_err(rtd->dev, "%s trigger %d failed: %d", __func__, cmd, ret);
283 int sdw_hw_free(struct snd_pcm_substream *substream)
285 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
286 struct sdw_stream_runtime *sdw_stream;
287 struct snd_soc_dai *dai;
289 /* Find stream from first CPU DAI */
290 dai = asoc_rtd_to_cpu(rtd, 0);
292 sdw_stream = snd_soc_dai_get_sdw_stream(dai, substream->stream);
294 if (IS_ERR(sdw_stream)) {
295 dev_err(rtd->dev, "no stream found for DAI %s", dai->name);
296 return PTR_ERR(sdw_stream);
299 return sdw_deprepare_stream(sdw_stream);
302 void sdw_shutdown(struct snd_pcm_substream *substream)
304 sdw_shutdown_stream(substream);
307 static const struct snd_soc_ops sdw_ops = {
308 .startup = sdw_startup,
309 .prepare = sdw_prepare,
310 .trigger = sdw_trigger,
311 .hw_free = sdw_hw_free,
312 .shutdown = sdw_shutdown,
315 static struct sof_sdw_codec_info codec_info_list[] = {
318 .direction = {true, true},
319 .dai_name = "rt700-aif1",
320 .init = sof_sdw_rt700_init,
325 .direction = {true, true},
326 .dai_name = "rt711-sdca-aif1",
327 .init = sof_sdw_rt711_sdca_init,
328 .exit = sof_sdw_rt711_sdca_exit,
333 .direction = {true, true},
334 .dai_name = "rt711-aif1",
335 .init = sof_sdw_rt711_init,
336 .exit = sof_sdw_rt711_exit,
340 .acpi_id = "10EC1308",
341 .direction = {true, false},
342 .dai_name = "rt1308-aif",
343 .ops = &sof_sdw_rt1308_i2s_ops,
344 .init = sof_sdw_rt1308_init,
348 .direction = {true, true},
349 .dai_name = "rt1316-aif",
350 .init = sof_sdw_rt1316_init,
355 .direction = {false, true},
356 .dai_name = "rt715-aif2",
357 .init = sof_sdw_rt715_sdca_init,
362 .direction = {false, true},
363 .dai_name = "rt715-aif2",
364 .init = sof_sdw_rt715_sdca_init,
369 .direction = {false, true},
370 .dai_name = "rt715-aif2",
371 .init = sof_sdw_rt715_init,
376 .direction = {false, true},
377 .dai_name = "rt715-aif2",
378 .init = sof_sdw_rt715_init,
382 .direction = {true, true},
383 .dai_name = "max98373-aif1",
384 .init = sof_sdw_mx8373_init,
385 .codec_card_late_probe = sof_sdw_mx8373_late_probe,
389 .direction = {true, true},
390 .dai_name = "rt5682-sdw",
391 .init = sof_sdw_rt5682_init,
395 static inline int find_codec_info_part(u64 adr)
397 unsigned int part_id, sdw_version;
400 part_id = SDW_PART_ID(adr);
401 sdw_version = SDW_VERSION(adr);
402 for (i = 0; i < ARRAY_SIZE(codec_info_list); i++)
404 * A codec info is for all sdw version with the part id if
405 * version_id is not specified in the codec info.
407 if (part_id == codec_info_list[i].part_id &&
408 (!codec_info_list[i].version_id ||
409 sdw_version == codec_info_list[i].version_id))
416 static inline int find_codec_info_acpi(const u8 *acpi_id)
423 for (i = 0; i < ARRAY_SIZE(codec_info_list); i++)
424 if (!memcmp(codec_info_list[i].acpi_id, acpi_id,
428 if (i == ARRAY_SIZE(codec_info_list))
435 * get BE dailink number and CPU DAI number based on sdw link adr.
436 * Since some sdw slaves may be aggregated, the CPU DAI number
437 * may be larger than the number of BE dailinks.
439 static int get_sdw_dailink_info(const struct snd_soc_acpi_link_adr *links,
440 int *sdw_be_num, int *sdw_cpu_dai_num)
442 const struct snd_soc_acpi_link_adr *link;
443 bool group_visited[SDW_MAX_GROUPS];
447 no_aggregation = sof_sdw_quirk & SOF_SDW_NO_AGGREGATION;
448 *sdw_cpu_dai_num = 0;
454 for (i = 0; i < SDW_MAX_GROUPS; i++)
455 group_visited[i] = false;
457 for (link = links; link->num_adr; link++) {
458 const struct snd_soc_acpi_endpoint *endpoint;
463 adr = link->adr_d->adr;
464 codec_index = find_codec_info_part(adr);
468 endpoint = link->adr_d->endpoints;
470 /* count DAI number for playback and capture */
471 for_each_pcm_streams(stream) {
472 if (!codec_info_list[codec_index].direction[stream])
475 (*sdw_cpu_dai_num)++;
477 /* count BE for each non-aggregated slave or group */
478 if (!endpoint->aggregated || no_aggregation ||
479 !group_visited[endpoint->group_id])
483 if (endpoint->aggregated)
484 group_visited[endpoint->group_id] = true;
490 static void init_dai_link(struct device *dev, struct snd_soc_dai_link *dai_links,
491 int be_id, char *name, int playback, int capture,
492 struct snd_soc_dai_link_component *cpus, int cpus_num,
493 struct snd_soc_dai_link_component *codecs, int codecs_num,
494 int (*init)(struct snd_soc_pcm_runtime *rtd),
495 const struct snd_soc_ops *ops)
497 dev_dbg(dev, "create dai link %s, id %d\n", name, be_id);
498 dai_links->id = be_id;
499 dai_links->name = name;
500 dai_links->platforms = platform_component;
501 dai_links->num_platforms = ARRAY_SIZE(platform_component);
502 dai_links->nonatomic = true;
503 dai_links->no_pcm = 1;
504 dai_links->cpus = cpus;
505 dai_links->num_cpus = cpus_num;
506 dai_links->codecs = codecs;
507 dai_links->num_codecs = codecs_num;
508 dai_links->dpcm_playback = playback;
509 dai_links->dpcm_capture = capture;
510 dai_links->init = init;
511 dai_links->ops = ops;
514 static bool is_unique_device(const struct snd_soc_acpi_link_adr *link,
515 unsigned int sdw_version,
517 unsigned int part_id,
518 unsigned int class_id,
524 for (i = 0; i < link->num_adr; i++) {
525 unsigned int sdw1_version, mfg1_id, part1_id, class1_id;
529 if (i == index_in_link)
532 adr = link->adr_d[i].adr;
534 sdw1_version = SDW_VERSION(adr);
535 mfg1_id = SDW_MFG_ID(adr);
536 part1_id = SDW_PART_ID(adr);
537 class1_id = SDW_CLASS_ID(adr);
539 if (sdw_version == sdw1_version &&
541 part_id == part1_id &&
542 class_id == class1_id)
549 static int create_codec_dai_name(struct device *dev,
550 const struct snd_soc_acpi_link_adr *link,
551 struct snd_soc_dai_link_component *codec,
553 struct snd_soc_codec_conf *codec_conf,
555 int *codec_conf_index)
560 if (*codec_conf_index + link->num_adr > codec_count) {
561 dev_err(dev, "codec_conf: out-of-bounds access requested\n");
565 for (i = 0; i < link->num_adr; i++) {
566 unsigned int sdw_version, unique_id, mfg_id;
567 unsigned int link_id, part_id, class_id;
568 int codec_index, comp_index;
572 adr = link->adr_d[i].adr;
574 sdw_version = SDW_VERSION(adr);
575 link_id = SDW_DISCO_LINK_ID(adr);
576 unique_id = SDW_UNIQUE_ID(adr);
577 mfg_id = SDW_MFG_ID(adr);
578 part_id = SDW_PART_ID(adr);
579 class_id = SDW_CLASS_ID(adr);
581 comp_index = i + offset;
582 if (is_unique_device(link, sdw_version, mfg_id, part_id,
584 codec_str = "sdw:%x:%x:%x:%x";
585 codec[comp_index].name =
586 devm_kasprintf(dev, GFP_KERNEL, codec_str,
587 link_id, mfg_id, part_id,
590 codec_str = "sdw:%x:%x:%x:%x:%x";
591 codec[comp_index].name =
592 devm_kasprintf(dev, GFP_KERNEL, codec_str,
593 link_id, mfg_id, part_id,
594 class_id, unique_id);
597 if (!codec[comp_index].name)
600 codec_index = find_codec_info_part(adr);
604 codec[comp_index].dai_name =
605 codec_info_list[codec_index].dai_name;
607 codec_conf[*codec_conf_index].dlc = codec[comp_index];
608 codec_conf[*codec_conf_index].name_prefix = link->adr_d[i].name_prefix;
616 static int set_codec_init_func(const struct snd_soc_acpi_link_adr *link,
617 struct snd_soc_dai_link *dai_links,
618 bool playback, int group_id)
624 * Initialize the codec. If codec is part of an aggregated
625 * group (group_id>0), initialize all codecs belonging to
628 for (i = 0; i < link->num_adr; i++) {
631 codec_index = find_codec_info_part(link->adr_d[i].adr);
635 /* The group_id is > 0 iff the codec is aggregated */
636 if (link->adr_d[i].endpoints->group_id != group_id)
638 if (codec_info_list[codec_index].init)
639 codec_info_list[codec_index].init(link,
641 &codec_info_list[codec_index],
645 } while (link->mask && group_id);
651 * check endpoint status in slaves and gather link ID for all slaves in
652 * the same group to generate different CPU DAI. Now only support
653 * one sdw link with all slaves set with only single group id.
655 * one slave on one sdw link with aggregated = 0
656 * one sdw BE DAI <---> one-cpu DAI <---> one-codec DAI
658 * two or more slaves on one sdw link with aggregated = 0
659 * one sdw BE DAI <---> one-cpu DAI <---> multi-codec DAIs
661 * multiple links with multiple slaves with aggregated = 1
662 * one sdw BE DAI <---> 1 .. N CPU DAIs <----> 1 .. N codec DAIs
664 static int get_slave_info(const struct snd_soc_acpi_link_adr *adr_link,
665 struct device *dev, int *cpu_dai_id, int *cpu_dai_num,
666 int *codec_num, int *group_id,
667 bool *group_generated)
669 const struct snd_soc_acpi_adr_device *adr_d;
670 const struct snd_soc_acpi_link_adr *adr_next;
674 no_aggregation = sof_sdw_quirk & SOF_SDW_NO_AGGREGATION;
675 *codec_num = adr_link->num_adr;
676 adr_d = adr_link->adr_d;
678 /* make sure the link mask has a single bit set */
679 if (!is_power_of_2(adr_link->mask))
682 cpu_dai_id[index++] = ffs(adr_link->mask) - 1;
683 if (!adr_d->endpoints->aggregated || no_aggregation) {
689 *group_id = adr_d->endpoints->group_id;
691 /* gather other link ID of slaves in the same group */
692 for (adr_next = adr_link + 1; adr_next && adr_next->num_adr;
694 const struct snd_soc_acpi_endpoint *endpoint;
696 endpoint = adr_next->adr_d->endpoints;
697 if (!endpoint->aggregated ||
698 endpoint->group_id != *group_id)
701 /* make sure the link mask has a single bit set */
702 if (!is_power_of_2(adr_next->mask))
705 if (index >= SDW_MAX_CPU_DAIS) {
706 dev_err(dev, " cpu_dai_id array overflows");
710 cpu_dai_id[index++] = ffs(adr_next->mask) - 1;
711 *codec_num += adr_next->num_adr;
715 * indicate CPU DAIs for this group have been generated
716 * to avoid generating CPU DAIs for this group again.
718 group_generated[*group_id] = true;
719 *cpu_dai_num = index;
724 static int create_sdw_dailink(struct device *dev, int *be_index,
725 struct snd_soc_dai_link *dai_links,
726 int sdw_be_num, int sdw_cpu_dai_num,
727 struct snd_soc_dai_link_component *cpus,
728 const struct snd_soc_acpi_link_adr *link,
729 int *cpu_id, bool *group_generated,
730 struct snd_soc_codec_conf *codec_conf,
732 int *codec_conf_index)
734 const struct snd_soc_acpi_link_adr *link_next;
735 struct snd_soc_dai_link_component *codecs;
736 int cpu_dai_id[SDW_MAX_CPU_DAIS];
737 int cpu_dai_num, cpu_dai_index;
738 unsigned int group_id;
747 ret = get_slave_info(link, dev, cpu_dai_id, &cpu_dai_num, &codec_num,
748 &group_id, group_generated);
752 codecs = devm_kcalloc(dev, codec_num, sizeof(*codecs), GFP_KERNEL);
756 /* generate codec name on different links in the same group */
757 for (link_next = link; link_next && link_next->num_adr &&
758 i < cpu_dai_num; link_next++) {
759 const struct snd_soc_acpi_endpoint *endpoints;
761 endpoints = link_next->adr_d->endpoints;
762 if (group_id && (!endpoints->aggregated ||
763 endpoints->group_id != group_id))
766 /* skip the link excluded by this processed group */
767 if (cpu_dai_id[i] != ffs(link_next->mask) - 1)
770 ret = create_codec_dai_name(dev, link_next, codecs, codec_idx,
771 codec_conf, codec_count, codec_conf_index);
775 /* check next link to create codec dai in the processed group */
777 codec_idx += link_next->num_adr;
780 /* find codec info to create BE DAI */
781 codec_index = find_codec_info_part(link->adr_d[0].adr);
785 cpu_dai_index = *cpu_id;
786 for_each_pcm_streams(stream) {
787 char *name, *cpu_name;
788 int playback, capture;
789 static const char * const sdw_stream_name[] = {
794 if (!codec_info_list[codec_index].direction[stream])
797 /* create stream name according to first link id */
798 name = devm_kasprintf(dev, GFP_KERNEL,
799 sdw_stream_name[stream], cpu_dai_id[0]);
804 * generate CPU DAI name base on the sdw link ID and
805 * PIN ID with offset of 2 according to sdw dai driver.
807 for (k = 0; k < cpu_dai_num; k++) {
808 cpu_name = devm_kasprintf(dev, GFP_KERNEL,
809 "SDW%d Pin%d", cpu_dai_id[k],
810 j + SDW_INTEL_BIDIR_PDI_BASE);
814 if (cpu_dai_index >= sdw_cpu_dai_num) {
815 dev_err(dev, "invalid cpu dai index %d",
820 cpus[cpu_dai_index++].dai_name = cpu_name;
823 if (*be_index >= sdw_be_num) {
824 dev_err(dev, " invalid be dai index %d", *be_index);
828 if (*cpu_id >= sdw_cpu_dai_num) {
829 dev_err(dev, " invalid cpu dai index %d", *cpu_id);
833 playback = (stream == SNDRV_PCM_STREAM_PLAYBACK);
834 capture = (stream == SNDRV_PCM_STREAM_CAPTURE);
835 init_dai_link(dev, dai_links + *be_index, *be_index, name,
837 cpus + *cpu_id, cpu_dai_num,
841 ret = set_codec_init_func(link, dai_links + (*be_index)++,
844 dev_err(dev, "failed to init codec %d", codec_index);
848 *cpu_id += cpu_dai_num;
856 * DAI link ID of SSP & DMIC & HDMI are based on last
857 * link ID used by sdw link. Since be_id may be changed
858 * in init func of sdw codec, it is not equal to be_id
860 static inline int get_next_be_id(struct snd_soc_dai_link *links,
863 return links[be_id - 1].id + 1;
866 #define IDISP_CODEC_MASK 0x4
868 static int sof_card_codec_conf_alloc(struct device *dev,
869 struct snd_soc_acpi_mach_params *mach_params,
870 struct snd_soc_codec_conf **codec_conf,
871 int *codec_conf_count)
873 const struct snd_soc_acpi_link_adr *adr_link;
874 struct snd_soc_codec_conf *c_conf;
878 adr_link = mach_params->links;
882 /* generate DAI links by each sdw link */
883 for (; adr_link->num_adr; adr_link++) {
884 for (i = 0; i < adr_link->num_adr; i++) {
885 if (!adr_link->adr_d[i].name_prefix) {
886 dev_err(dev, "codec 0x%llx does not have a name prefix\n",
887 adr_link->adr_d[i].adr);
891 num_codecs += adr_link->num_adr;
894 c_conf = devm_kzalloc(dev, num_codecs * sizeof(*c_conf), GFP_KERNEL);
898 *codec_conf = c_conf;
899 *codec_conf_count = num_codecs;
904 static int sof_card_dai_links_create(struct device *dev,
905 struct snd_soc_acpi_mach *mach,
906 struct snd_soc_card *card)
908 int ssp_num, sdw_be_num = 0, hdmi_num = 0, dmic_num;
909 struct mc_private *ctx = snd_soc_card_get_drvdata(card);
910 struct snd_soc_dai_link_component *idisp_components;
911 struct snd_soc_dai_link_component *ssp_components;
912 struct snd_soc_acpi_mach_params *mach_params;
913 const struct snd_soc_acpi_link_adr *adr_link;
914 struct snd_soc_dai_link_component *cpus;
915 struct snd_soc_codec_conf *codec_conf;
916 int codec_conf_count;
917 int codec_conf_index = 0;
918 bool group_generated[SDW_MAX_GROUPS];
919 int ssp_codec_index, ssp_mask;
920 struct snd_soc_dai_link *links;
921 int num_links, link_id = 0;
922 char *name, *cpu_name;
923 int total_cpu_dai_num;
930 mach_params = &mach->mach_params;
932 /* allocate codec conf, will be populated when dailinks are created */
933 ret = sof_card_codec_conf_alloc(dev, mach_params, &codec_conf, &codec_conf_count);
937 /* reset amp_num to ensure amp_num++ starts from 0 in each probe */
938 for (i = 0; i < ARRAY_SIZE(codec_info_list); i++)
939 codec_info_list[i].amp_num = 0;
941 if (sof_sdw_quirk & SOF_SDW_TGL_HDMI)
942 hdmi_num = SOF_TGL_HDMI_COUNT;
944 hdmi_num = SOF_PRE_TGL_HDMI_COUNT;
946 ssp_mask = SOF_SSP_GET_PORT(sof_sdw_quirk);
948 * on generic tgl platform, I2S or sdw mode is supported
949 * based on board rework. A ACPI device is registered in
950 * system only when I2S mode is supported, not sdw mode.
951 * Here check ACPI ID to confirm I2S is supported.
953 ssp_codec_index = find_codec_info_acpi(mach->id);
954 ssp_num = ssp_codec_index >= 0 ? hweight_long(ssp_mask) : 0;
955 comp_num = hdmi_num + ssp_num;
957 ret = get_sdw_dailink_info(mach_params->links,
958 &sdw_be_num, &sdw_cpu_dai_num);
960 dev_err(dev, "failed to get sdw link info %d", ret);
964 if (mach_params->codec_mask & IDISP_CODEC_MASK)
965 ctx->idisp_codec = true;
967 /* enable dmic01 & dmic16k */
968 dmic_num = (sof_sdw_quirk & SOF_SDW_PCH_DMIC || mach_params->dmic_num) ? 2 : 0;
969 comp_num += dmic_num;
971 dev_dbg(dev, "sdw %d, ssp %d, dmic %d, hdmi %d", sdw_be_num, ssp_num,
972 dmic_num, ctx->idisp_codec ? hdmi_num : 0);
974 /* allocate BE dailinks */
975 num_links = comp_num + sdw_be_num;
976 links = devm_kcalloc(dev, num_links, sizeof(*links), GFP_KERNEL);
978 /* allocated CPU DAIs */
979 total_cpu_dai_num = comp_num + sdw_cpu_dai_num;
980 cpus = devm_kcalloc(dev, total_cpu_dai_num, sizeof(*cpus),
990 adr_link = mach_params->links;
995 * SoundWire Slaves aggregated in the same group may be
996 * located on different hardware links. Clear array to indicate
997 * CPU DAIs for this group have not been generated.
999 for (i = 0; i < SDW_MAX_GROUPS; i++)
1000 group_generated[i] = false;
1002 /* generate DAI links by each sdw link */
1003 for (; adr_link->num_adr; adr_link++) {
1004 const struct snd_soc_acpi_endpoint *endpoint;
1006 endpoint = adr_link->adr_d->endpoints;
1007 if (endpoint->aggregated && !endpoint->group_id) {
1008 dev_err(dev, "invalid group id on link %x",
1013 /* this group has been generated */
1014 if (endpoint->aggregated &&
1015 group_generated[endpoint->group_id])
1018 ret = create_sdw_dailink(dev, &be_id, links, sdw_be_num,
1019 sdw_cpu_dai_num, cpus, adr_link,
1020 &cpu_id, group_generated,
1021 codec_conf, codec_conf_count,
1024 dev_err(dev, "failed to create dai link %d", be_id);
1029 /* non-sdw DAI follows sdw DAI */
1032 /* get BE ID for non-sdw DAI */
1033 be_id = get_next_be_id(links, be_id);
1040 for (i = 0, j = 0; ssp_mask; i++, ssp_mask >>= 1) {
1041 struct sof_sdw_codec_info *info;
1042 int playback, capture;
1045 if (!(ssp_mask & 0x1))
1048 name = devm_kasprintf(dev, GFP_KERNEL,
1053 cpu_name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d Pin", i);
1057 ssp_components = devm_kzalloc(dev, sizeof(*ssp_components),
1059 if (!ssp_components)
1062 info = &codec_info_list[ssp_codec_index];
1063 codec_name = devm_kasprintf(dev, GFP_KERNEL, "i2c-%s:0%d",
1064 info->acpi_id, j++);
1068 ssp_components->name = codec_name;
1069 ssp_components->dai_name = info->dai_name;
1070 cpus[cpu_id].dai_name = cpu_name;
1072 playback = info->direction[SNDRV_PCM_STREAM_PLAYBACK];
1073 capture = info->direction[SNDRV_PCM_STREAM_CAPTURE];
1074 init_dai_link(dev, links + link_id, be_id, name,
1080 ret = info->init(NULL, links + link_id, info, 0);
1084 INC_ID(be_id, cpu_id, link_id);
1090 cpus[cpu_id].dai_name = "DMIC01 Pin";
1091 init_dai_link(dev, links + link_id, be_id, "dmic01",
1092 0, 1, // DMIC only supports capture
1095 sof_sdw_dmic_init, NULL);
1096 INC_ID(be_id, cpu_id, link_id);
1098 cpus[cpu_id].dai_name = "DMIC16k Pin";
1099 init_dai_link(dev, links + link_id, be_id, "dmic16k",
1100 0, 1, // DMIC only supports capture
1103 /* don't call sof_sdw_dmic_init() twice */
1105 INC_ID(be_id, cpu_id, link_id);
1110 idisp_components = devm_kcalloc(dev, hdmi_num,
1111 sizeof(*idisp_components),
1113 if (!idisp_components)
1117 for (i = 0; i < hdmi_num; i++) {
1118 name = devm_kasprintf(dev, GFP_KERNEL,
1123 if (ctx->idisp_codec) {
1124 idisp_components[i].name = "ehdaudio0D2";
1125 idisp_components[i].dai_name = devm_kasprintf(dev,
1127 "intel-hdmi-hifi%d",
1129 if (!idisp_components[i].dai_name)
1132 idisp_components[i].name = "snd-soc-dummy";
1133 idisp_components[i].dai_name = "snd-soc-dummy-dai";
1136 cpu_name = devm_kasprintf(dev, GFP_KERNEL,
1137 "iDisp%d Pin", i + 1);
1141 cpus[cpu_id].dai_name = cpu_name;
1142 init_dai_link(dev, links + link_id, be_id, name,
1143 1, 0, // HDMI only supports playback
1145 idisp_components + i, 1,
1146 sof_sdw_hdmi_init, NULL);
1147 INC_ID(be_id, cpu_id, link_id);
1150 card->dai_link = links;
1151 card->num_links = num_links;
1153 card->codec_conf = codec_conf;
1154 card->num_configs = codec_conf_count;
1159 static int sof_sdw_card_late_probe(struct snd_soc_card *card)
1163 for (i = 0; i < ARRAY_SIZE(codec_info_list); i++) {
1164 if (!codec_info_list[i].late_probe)
1167 ret = codec_info_list[i].codec_card_late_probe(card);
1172 return sof_sdw_hdmi_card_late_probe(card);
1176 static const char sdw_card_long_name[] = "Intel Soundwire SOF";
1178 static struct snd_soc_card card_sof_sdw = {
1179 .name = "soundwire",
1180 .owner = THIS_MODULE,
1181 .late_probe = sof_sdw_card_late_probe,
1184 static int mc_probe(struct platform_device *pdev)
1186 struct snd_soc_card *card = &card_sof_sdw;
1187 struct snd_soc_acpi_mach *mach;
1188 struct mc_private *ctx;
1192 dev_dbg(&pdev->dev, "Entry %s\n", __func__);
1194 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
1198 dmi_check_system(sof_sdw_quirk_table);
1200 if (quirk_override != -1) {
1201 dev_info(&pdev->dev, "Overriding quirk 0x%lx => 0x%x\n",
1202 sof_sdw_quirk, quirk_override);
1203 sof_sdw_quirk = quirk_override;
1205 log_quirks(&pdev->dev);
1207 INIT_LIST_HEAD(&ctx->hdmi_pcm_list);
1209 card->dev = &pdev->dev;
1210 snd_soc_card_set_drvdata(card, ctx);
1212 mach = pdev->dev.platform_data;
1213 ret = sof_card_dai_links_create(&pdev->dev, mach,
1218 ctx->common_hdmi_codec_drv = mach->mach_params.common_hdmi_codec_drv;
1221 * the default amp_num is zero for each codec and
1222 * amp_num will only be increased for active amp
1223 * codecs on used platform
1225 for (i = 0; i < ARRAY_SIZE(codec_info_list); i++)
1226 amp_num += codec_info_list[i].amp_num;
1228 card->components = devm_kasprintf(card->dev, GFP_KERNEL,
1229 "cfg-spk:%d cfg-amp:%d",
1230 (sof_sdw_quirk & SOF_SDW_FOUR_SPK)
1232 if (!card->components)
1235 if (mach->mach_params.dmic_num) {
1236 card->components = devm_kasprintf(card->dev, GFP_KERNEL,
1237 "%s mic:dmic cfg-mics:%d",
1239 mach->mach_params.dmic_num);
1240 if (!card->components)
1244 card->long_name = sdw_card_long_name;
1246 /* Register the card */
1247 ret = devm_snd_soc_register_card(&pdev->dev, card);
1249 dev_err(card->dev, "snd_soc_register_card failed %d\n", ret);
1253 platform_set_drvdata(pdev, card);
1258 static int mc_remove(struct platform_device *pdev)
1260 struct snd_soc_card *card = platform_get_drvdata(pdev);
1261 struct snd_soc_dai_link *link;
1265 for (i = 0; i < ARRAY_SIZE(codec_info_list); i++) {
1266 if (!codec_info_list[i].exit)
1269 * We don't need to call .exit function if there is no matched
1272 for_each_card_prelinks(card, j, link) {
1273 if (!strcmp(link->codecs[0].dai_name,
1274 codec_info_list[i].dai_name)) {
1275 ret = codec_info_list[i].exit(&pdev->dev, link);
1277 dev_warn(&pdev->dev,
1278 "codec exit failed %d\n",
1288 static struct platform_driver sof_sdw_driver = {
1291 .pm = &snd_soc_pm_ops,
1294 .remove = mc_remove,
1297 module_platform_driver(sof_sdw_driver);
1299 MODULE_DESCRIPTION("ASoC SoundWire Generic Machine driver");
1300 MODULE_AUTHOR("Bard Liao <yung-chuan.liao@linux.intel.com>");
1301 MODULE_AUTHOR("Rander Wang <rander.wang@linux.intel.com>");
1302 MODULE_AUTHOR("Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>");
1303 MODULE_LICENSE("GPL v2");
1304 MODULE_ALIAS("platform:sof_sdw");