WIP FPC-III support
[linux/fpc-iii.git] / sound / soc / intel / boards / sof_sdw.c
blobca968901ac96f39070c7164446162a268bfd5ba8
1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright (c) 2020 Intel Corporation
4 /*
5 * sof_sdw - ASOC Machine driver for Intel SoundWire platforms
6 */
8 #include <linux/device.h>
9 #include <linux/dmi.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;
47 return 1;
50 static const struct dmi_system_id sof_sdw_quirk_table[] = {
52 .callback = sof_sdw_quirk_cb,
53 .matches = {
54 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
55 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0A32")
57 .driver_data = (void *)(SOF_RT711_JD_SRC_JD2 |
58 SOF_RT715_DAI_ID_FIX |
59 SOF_SDW_FOUR_SPK),
62 .callback = sof_sdw_quirk_cb,
63 .matches = {
64 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
65 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0A3E")
67 .driver_data = (void *)(SOF_RT711_JD_SRC_JD2 |
68 SOF_RT715_DAI_ID_FIX),
71 .callback = sof_sdw_quirk_cb,
72 .matches = {
73 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
74 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "09C6")
76 .driver_data = (void *)(SOF_RT711_JD_SRC_JD2 |
77 SOF_RT715_DAI_ID_FIX),
80 /* early version of SKU 09C6 */
81 .callback = sof_sdw_quirk_cb,
82 .matches = {
83 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
84 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0983")
86 .driver_data = (void *)(SOF_RT711_JD_SRC_JD2 |
87 SOF_RT715_DAI_ID_FIX),
90 .callback = sof_sdw_quirk_cb,
91 .matches = {
92 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
93 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "098F"),
95 .driver_data = (void *)(SOF_RT711_JD_SRC_JD2 |
96 SOF_RT715_DAI_ID_FIX |
97 SOF_SDW_FOUR_SPK),
100 .callback = sof_sdw_quirk_cb,
101 .matches = {
102 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
103 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0990"),
105 .driver_data = (void *)(SOF_RT711_JD_SRC_JD2 |
106 SOF_RT715_DAI_ID_FIX |
107 SOF_SDW_FOUR_SPK),
110 .callback = sof_sdw_quirk_cb,
111 .matches = {
112 DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
113 DMI_MATCH(DMI_PRODUCT_NAME,
114 "Tiger Lake Client Platform"),
116 .driver_data = (void *)(SOF_RT711_JD_SRC_JD1 |
117 SOF_SDW_TGL_HDMI | SOF_SDW_PCH_DMIC |
118 SOF_SSP_PORT(SOF_I2S_SSP2)),
121 .callback = sof_sdw_quirk_cb,
122 .matches = {
123 DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
124 DMI_MATCH(DMI_PRODUCT_NAME, "Ice Lake Client"),
126 .driver_data = (void *)SOF_SDW_PCH_DMIC,
129 .callback = sof_sdw_quirk_cb,
130 .matches = {
131 DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
132 DMI_MATCH(DMI_PRODUCT_NAME, "CometLake Client"),
134 .driver_data = (void *)SOF_SDW_PCH_DMIC,
137 .callback = sof_sdw_quirk_cb,
138 .matches = {
139 DMI_MATCH(DMI_SYS_VENDOR, "Google"),
140 DMI_MATCH(DMI_PRODUCT_NAME, "Volteer"),
142 .driver_data = (void *)(SOF_SDW_TGL_HDMI | SOF_SDW_PCH_DMIC |
143 SOF_SDW_FOUR_SPK),
146 .callback = sof_sdw_quirk_cb,
147 .matches = {
148 DMI_MATCH(DMI_SYS_VENDOR, "Google"),
149 DMI_MATCH(DMI_PRODUCT_NAME, "Ripto"),
151 .driver_data = (void *)(SOF_SDW_TGL_HDMI | SOF_SDW_PCH_DMIC |
152 SOF_SDW_FOUR_SPK),
158 static struct snd_soc_dai_link_component dmic_component[] = {
160 .name = "dmic-codec",
161 .dai_name = "dmic-hifi",
165 static struct snd_soc_dai_link_component platform_component[] = {
167 /* name might be overridden during probe */
168 .name = "0000:00:1f.3"
172 /* these wrappers are only needed to avoid typecast compilation errors */
173 int sdw_startup(struct snd_pcm_substream *substream)
175 return sdw_startup_stream(substream);
178 int sdw_prepare(struct snd_pcm_substream *substream)
180 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
181 struct sdw_stream_runtime *sdw_stream;
182 struct snd_soc_dai *dai;
184 /* Find stream from first CPU DAI */
185 dai = asoc_rtd_to_cpu(rtd, 0);
187 sdw_stream = snd_soc_dai_get_sdw_stream(dai, substream->stream);
189 if (IS_ERR(sdw_stream)) {
190 dev_err(rtd->dev, "no stream found for DAI %s", dai->name);
191 return PTR_ERR(sdw_stream);
194 return sdw_prepare_stream(sdw_stream);
197 int sdw_trigger(struct snd_pcm_substream *substream, int cmd)
199 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
200 struct sdw_stream_runtime *sdw_stream;
201 struct snd_soc_dai *dai;
202 int ret;
204 /* Find stream from first CPU DAI */
205 dai = asoc_rtd_to_cpu(rtd, 0);
207 sdw_stream = snd_soc_dai_get_sdw_stream(dai, substream->stream);
209 if (IS_ERR(sdw_stream)) {
210 dev_err(rtd->dev, "no stream found for DAI %s", dai->name);
211 return PTR_ERR(sdw_stream);
214 switch (cmd) {
215 case SNDRV_PCM_TRIGGER_START:
216 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
217 case SNDRV_PCM_TRIGGER_RESUME:
218 ret = sdw_enable_stream(sdw_stream);
219 break;
221 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
222 case SNDRV_PCM_TRIGGER_SUSPEND:
223 case SNDRV_PCM_TRIGGER_STOP:
224 ret = sdw_disable_stream(sdw_stream);
225 break;
226 default:
227 ret = -EINVAL;
228 break;
231 if (ret)
232 dev_err(rtd->dev, "%s trigger %d failed: %d", __func__, cmd, ret);
234 return ret;
237 int sdw_hw_free(struct snd_pcm_substream *substream)
239 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
240 struct sdw_stream_runtime *sdw_stream;
241 struct snd_soc_dai *dai;
243 /* Find stream from first CPU DAI */
244 dai = asoc_rtd_to_cpu(rtd, 0);
246 sdw_stream = snd_soc_dai_get_sdw_stream(dai, substream->stream);
248 if (IS_ERR(sdw_stream)) {
249 dev_err(rtd->dev, "no stream found for DAI %s", dai->name);
250 return PTR_ERR(sdw_stream);
253 return sdw_deprepare_stream(sdw_stream);
256 void sdw_shutdown(struct snd_pcm_substream *substream)
258 sdw_shutdown_stream(substream);
261 static const struct snd_soc_ops sdw_ops = {
262 .startup = sdw_startup,
263 .prepare = sdw_prepare,
264 .trigger = sdw_trigger,
265 .hw_free = sdw_hw_free,
266 .shutdown = sdw_shutdown,
269 static struct sof_sdw_codec_info codec_info_list[] = {
271 .part_id = 0x700,
272 .direction = {true, true},
273 .dai_name = "rt700-aif1",
274 .init = sof_sdw_rt700_init,
277 .part_id = 0x711,
278 .version_id = 3,
279 .direction = {true, true},
280 .dai_name = "rt711-sdca-aif1",
281 .init = sof_sdw_rt711_sdca_init,
282 .exit = sof_sdw_rt711_sdca_exit,
285 .part_id = 0x711,
286 .version_id = 2,
287 .direction = {true, true},
288 .dai_name = "rt711-aif1",
289 .init = sof_sdw_rt711_init,
290 .exit = sof_sdw_rt711_exit,
293 .part_id = 0x1308,
294 .acpi_id = "10EC1308",
295 .direction = {true, false},
296 .dai_name = "rt1308-aif",
297 .ops = &sof_sdw_rt1308_i2s_ops,
298 .init = sof_sdw_rt1308_init,
301 .part_id = 0x1316,
302 .direction = {true, true},
303 .dai_name = "rt1316-aif",
304 .init = sof_sdw_rt1316_init,
307 .part_id = 0x714,
308 .version_id = 3,
309 .direction = {false, true},
310 .dai_name = "rt715-aif2",
311 .init = sof_sdw_rt715_sdca_init,
314 .part_id = 0x715,
315 .version_id = 3,
316 .direction = {false, true},
317 .dai_name = "rt715-aif2",
318 .init = sof_sdw_rt715_sdca_init,
321 .part_id = 0x714,
322 .version_id = 2,
323 .direction = {false, true},
324 .dai_name = "rt715-aif2",
325 .init = sof_sdw_rt715_init,
328 .part_id = 0x715,
329 .version_id = 2,
330 .direction = {false, true},
331 .dai_name = "rt715-aif2",
332 .init = sof_sdw_rt715_init,
335 .part_id = 0x8373,
336 .direction = {true, true},
337 .dai_name = "max98373-aif1",
338 .init = sof_sdw_mx8373_init,
339 .codec_card_late_probe = sof_sdw_mx8373_late_probe,
342 .part_id = 0x5682,
343 .direction = {true, true},
344 .dai_name = "rt5682-sdw",
345 .init = sof_sdw_rt5682_init,
349 static inline int find_codec_info_part(u64 adr)
351 unsigned int part_id, sdw_version;
352 int i;
354 part_id = SDW_PART_ID(adr);
355 sdw_version = SDW_VERSION(adr);
356 for (i = 0; i < ARRAY_SIZE(codec_info_list); i++)
358 * A codec info is for all sdw version with the part id if
359 * version_id is not specified in the codec info.
361 if (part_id == codec_info_list[i].part_id &&
362 (!codec_info_list[i].version_id ||
363 sdw_version == codec_info_list[i].version_id))
364 return i;
366 return -EINVAL;
370 static inline int find_codec_info_acpi(const u8 *acpi_id)
372 int i;
374 if (!acpi_id[0])
375 return -EINVAL;
377 for (i = 0; i < ARRAY_SIZE(codec_info_list); i++)
378 if (!memcmp(codec_info_list[i].acpi_id, acpi_id,
379 ACPI_ID_LEN))
380 break;
382 if (i == ARRAY_SIZE(codec_info_list))
383 return -EINVAL;
385 return i;
389 * get BE dailink number and CPU DAI number based on sdw link adr.
390 * Since some sdw slaves may be aggregated, the CPU DAI number
391 * may be larger than the number of BE dailinks.
393 static int get_sdw_dailink_info(const struct snd_soc_acpi_link_adr *links,
394 int *sdw_be_num, int *sdw_cpu_dai_num)
396 const struct snd_soc_acpi_link_adr *link;
397 bool group_visited[SDW_MAX_GROUPS];
398 bool no_aggregation;
399 int i;
401 no_aggregation = sof_sdw_quirk & SOF_SDW_NO_AGGREGATION;
402 *sdw_cpu_dai_num = 0;
403 *sdw_be_num = 0;
405 if (!links)
406 return -EINVAL;
408 for (i = 0; i < SDW_MAX_GROUPS; i++)
409 group_visited[i] = false;
411 for (link = links; link->num_adr; link++) {
412 const struct snd_soc_acpi_endpoint *endpoint;
413 int codec_index;
414 int stream;
415 u64 adr;
417 adr = link->adr_d->adr;
418 codec_index = find_codec_info_part(adr);
419 if (codec_index < 0)
420 return codec_index;
422 endpoint = link->adr_d->endpoints;
424 /* count DAI number for playback and capture */
425 for_each_pcm_streams(stream) {
426 if (!codec_info_list[codec_index].direction[stream])
427 continue;
429 (*sdw_cpu_dai_num)++;
431 /* count BE for each non-aggregated slave or group */
432 if (!endpoint->aggregated || no_aggregation ||
433 !group_visited[endpoint->group_id])
434 (*sdw_be_num)++;
437 if (endpoint->aggregated)
438 group_visited[endpoint->group_id] = true;
441 return 0;
444 static void init_dai_link(struct snd_soc_dai_link *dai_links, int be_id,
445 char *name, int playback, int capture,
446 struct snd_soc_dai_link_component *cpus,
447 int cpus_num,
448 struct snd_soc_dai_link_component *codecs,
449 int codecs_num,
450 int (*init)(struct snd_soc_pcm_runtime *rtd),
451 const struct snd_soc_ops *ops)
453 dai_links->id = be_id;
454 dai_links->name = name;
455 dai_links->platforms = platform_component;
456 dai_links->num_platforms = ARRAY_SIZE(platform_component);
457 dai_links->nonatomic = true;
458 dai_links->no_pcm = 1;
459 dai_links->cpus = cpus;
460 dai_links->num_cpus = cpus_num;
461 dai_links->codecs = codecs;
462 dai_links->num_codecs = codecs_num;
463 dai_links->dpcm_playback = playback;
464 dai_links->dpcm_capture = capture;
465 dai_links->init = init;
466 dai_links->ops = ops;
469 static bool is_unique_device(const struct snd_soc_acpi_link_adr *link,
470 unsigned int sdw_version,
471 unsigned int mfg_id,
472 unsigned int part_id,
473 unsigned int class_id,
474 int index_in_link
477 int i;
479 for (i = 0; i < link->num_adr; i++) {
480 unsigned int sdw1_version, mfg1_id, part1_id, class1_id;
481 u64 adr;
483 /* skip itself */
484 if (i == index_in_link)
485 continue;
487 adr = link->adr_d[i].adr;
489 sdw1_version = SDW_VERSION(adr);
490 mfg1_id = SDW_MFG_ID(adr);
491 part1_id = SDW_PART_ID(adr);
492 class1_id = SDW_CLASS_ID(adr);
494 if (sdw_version == sdw1_version &&
495 mfg_id == mfg1_id &&
496 part_id == part1_id &&
497 class_id == class1_id)
498 return false;
501 return true;
504 static int create_codec_dai_name(struct device *dev,
505 const struct snd_soc_acpi_link_adr *link,
506 struct snd_soc_dai_link_component *codec,
507 int offset,
508 struct snd_soc_codec_conf *codec_conf,
509 int codec_count,
510 int *codec_conf_index)
512 int i;
514 /* sanity check */
515 if (*codec_conf_index + link->num_adr > codec_count) {
516 dev_err(dev, "codec_conf: out-of-bounds access requested\n");
517 return -EINVAL;
520 for (i = 0; i < link->num_adr; i++) {
521 unsigned int sdw_version, unique_id, mfg_id;
522 unsigned int link_id, part_id, class_id;
523 int codec_index, comp_index;
524 char *codec_str;
525 u64 adr;
527 adr = link->adr_d[i].adr;
529 sdw_version = SDW_VERSION(adr);
530 link_id = SDW_DISCO_LINK_ID(adr);
531 unique_id = SDW_UNIQUE_ID(adr);
532 mfg_id = SDW_MFG_ID(adr);
533 part_id = SDW_PART_ID(adr);
534 class_id = SDW_CLASS_ID(adr);
536 comp_index = i + offset;
537 if (is_unique_device(link, sdw_version, mfg_id, part_id,
538 class_id, i)) {
539 codec_str = "sdw:%x:%x:%x:%x";
540 codec[comp_index].name =
541 devm_kasprintf(dev, GFP_KERNEL, codec_str,
542 link_id, mfg_id, part_id,
543 class_id);
544 } else {
545 codec_str = "sdw:%x:%x:%x:%x:%x";
546 codec[comp_index].name =
547 devm_kasprintf(dev, GFP_KERNEL, codec_str,
548 link_id, mfg_id, part_id,
549 class_id, unique_id);
552 if (!codec[comp_index].name)
553 return -ENOMEM;
555 codec_index = find_codec_info_part(adr);
556 if (codec_index < 0)
557 return codec_index;
559 codec[comp_index].dai_name =
560 codec_info_list[codec_index].dai_name;
562 codec_conf[*codec_conf_index].dlc = codec[comp_index];
563 codec_conf[*codec_conf_index].name_prefix = link->adr_d[i].name_prefix;
565 ++*codec_conf_index;
568 return 0;
571 static int set_codec_init_func(const struct snd_soc_acpi_link_adr *link,
572 struct snd_soc_dai_link *dai_links,
573 bool playback, int group_id)
575 int i;
577 do {
579 * Initialize the codec. If codec is part of an aggregated
580 * group (group_id>0), initialize all codecs belonging to
581 * same group.
583 for (i = 0; i < link->num_adr; i++) {
584 int codec_index;
586 codec_index = find_codec_info_part(link->adr_d[i].adr);
588 if (codec_index < 0)
589 return codec_index;
590 /* The group_id is > 0 iff the codec is aggregated */
591 if (link->adr_d[i].endpoints->group_id != group_id)
592 continue;
593 if (codec_info_list[codec_index].init)
594 codec_info_list[codec_index].init(link,
595 dai_links,
596 &codec_info_list[codec_index],
597 playback);
599 link++;
600 } while (link->mask && group_id);
602 return 0;
606 * check endpoint status in slaves and gather link ID for all slaves in
607 * the same group to generate different CPU DAI. Now only support
608 * one sdw link with all slaves set with only single group id.
610 * one slave on one sdw link with aggregated = 0
611 * one sdw BE DAI <---> one-cpu DAI <---> one-codec DAI
613 * two or more slaves on one sdw link with aggregated = 0
614 * one sdw BE DAI <---> one-cpu DAI <---> multi-codec DAIs
616 * multiple links with multiple slaves with aggregated = 1
617 * one sdw BE DAI <---> 1 .. N CPU DAIs <----> 1 .. N codec DAIs
619 static int get_slave_info(const struct snd_soc_acpi_link_adr *adr_link,
620 struct device *dev, int *cpu_dai_id, int *cpu_dai_num,
621 int *codec_num, int *group_id,
622 bool *group_generated)
624 const struct snd_soc_acpi_adr_device *adr_d;
625 const struct snd_soc_acpi_link_adr *adr_next;
626 bool no_aggregation;
627 int index = 0;
629 no_aggregation = sof_sdw_quirk & SOF_SDW_NO_AGGREGATION;
630 *codec_num = adr_link->num_adr;
631 adr_d = adr_link->adr_d;
633 /* make sure the link mask has a single bit set */
634 if (!is_power_of_2(adr_link->mask))
635 return -EINVAL;
637 cpu_dai_id[index++] = ffs(adr_link->mask) - 1;
638 if (!adr_d->endpoints->aggregated || no_aggregation) {
639 *cpu_dai_num = 1;
640 *group_id = 0;
641 return 0;
644 *group_id = adr_d->endpoints->group_id;
646 /* gather other link ID of slaves in the same group */
647 for (adr_next = adr_link + 1; adr_next && adr_next->num_adr;
648 adr_next++) {
649 const struct snd_soc_acpi_endpoint *endpoint;
651 endpoint = adr_next->adr_d->endpoints;
652 if (!endpoint->aggregated ||
653 endpoint->group_id != *group_id)
654 continue;
656 /* make sure the link mask has a single bit set */
657 if (!is_power_of_2(adr_next->mask))
658 return -EINVAL;
660 if (index >= SDW_MAX_CPU_DAIS) {
661 dev_err(dev, " cpu_dai_id array overflows");
662 return -EINVAL;
665 cpu_dai_id[index++] = ffs(adr_next->mask) - 1;
666 *codec_num += adr_next->num_adr;
670 * indicate CPU DAIs for this group have been generated
671 * to avoid generating CPU DAIs for this group again.
673 group_generated[*group_id] = true;
674 *cpu_dai_num = index;
676 return 0;
679 static int create_sdw_dailink(struct device *dev, int *be_index,
680 struct snd_soc_dai_link *dai_links,
681 int sdw_be_num, int sdw_cpu_dai_num,
682 struct snd_soc_dai_link_component *cpus,
683 const struct snd_soc_acpi_link_adr *link,
684 int *cpu_id, bool *group_generated,
685 struct snd_soc_codec_conf *codec_conf,
686 int codec_count,
687 int *codec_conf_index)
689 const struct snd_soc_acpi_link_adr *link_next;
690 struct snd_soc_dai_link_component *codecs;
691 int cpu_dai_id[SDW_MAX_CPU_DAIS];
692 int cpu_dai_num, cpu_dai_index;
693 unsigned int group_id;
694 int codec_idx = 0;
695 int i = 0, j = 0;
696 int codec_index;
697 int codec_num;
698 int stream;
699 int ret;
700 int k;
702 ret = get_slave_info(link, dev, cpu_dai_id, &cpu_dai_num, &codec_num,
703 &group_id, group_generated);
704 if (ret)
705 return ret;
707 codecs = devm_kcalloc(dev, codec_num, sizeof(*codecs), GFP_KERNEL);
708 if (!codecs)
709 return -ENOMEM;
711 /* generate codec name on different links in the same group */
712 for (link_next = link; link_next && link_next->num_adr &&
713 i < cpu_dai_num; link_next++) {
714 const struct snd_soc_acpi_endpoint *endpoints;
716 endpoints = link_next->adr_d->endpoints;
717 if (group_id && (!endpoints->aggregated ||
718 endpoints->group_id != group_id))
719 continue;
721 /* skip the link excluded by this processed group */
722 if (cpu_dai_id[i] != ffs(link_next->mask) - 1)
723 continue;
725 ret = create_codec_dai_name(dev, link_next, codecs, codec_idx,
726 codec_conf, codec_count, codec_conf_index);
727 if (ret < 0)
728 return ret;
730 /* check next link to create codec dai in the processed group */
731 i++;
732 codec_idx += link_next->num_adr;
735 /* find codec info to create BE DAI */
736 codec_index = find_codec_info_part(link->adr_d[0].adr);
737 if (codec_index < 0)
738 return codec_index;
740 cpu_dai_index = *cpu_id;
741 for_each_pcm_streams(stream) {
742 char *name, *cpu_name;
743 int playback, capture;
744 static const char * const sdw_stream_name[] = {
745 "SDW%d-Playback",
746 "SDW%d-Capture",
749 if (!codec_info_list[codec_index].direction[stream])
750 continue;
752 /* create stream name according to first link id */
753 name = devm_kasprintf(dev, GFP_KERNEL,
754 sdw_stream_name[stream], cpu_dai_id[0]);
755 if (!name)
756 return -ENOMEM;
759 * generate CPU DAI name base on the sdw link ID and
760 * PIN ID with offset of 2 according to sdw dai driver.
762 for (k = 0; k < cpu_dai_num; k++) {
763 cpu_name = devm_kasprintf(dev, GFP_KERNEL,
764 "SDW%d Pin%d", cpu_dai_id[k],
765 j + SDW_INTEL_BIDIR_PDI_BASE);
766 if (!cpu_name)
767 return -ENOMEM;
769 if (cpu_dai_index >= sdw_cpu_dai_num) {
770 dev_err(dev, "invalid cpu dai index %d",
771 cpu_dai_index);
772 return -EINVAL;
775 cpus[cpu_dai_index++].dai_name = cpu_name;
778 if (*be_index >= sdw_be_num) {
779 dev_err(dev, " invalid be dai index %d", *be_index);
780 return -EINVAL;
783 if (*cpu_id >= sdw_cpu_dai_num) {
784 dev_err(dev, " invalid cpu dai index %d", *cpu_id);
785 return -EINVAL;
788 playback = (stream == SNDRV_PCM_STREAM_PLAYBACK);
789 capture = (stream == SNDRV_PCM_STREAM_CAPTURE);
790 init_dai_link(dai_links + *be_index, *be_index, name,
791 playback, capture,
792 cpus + *cpu_id, cpu_dai_num,
793 codecs, codec_num,
794 NULL, &sdw_ops);
796 ret = set_codec_init_func(link, dai_links + (*be_index)++,
797 playback, group_id);
798 if (ret < 0) {
799 dev_err(dev, "failed to init codec %d", codec_index);
800 return ret;
803 *cpu_id += cpu_dai_num;
804 j++;
807 return 0;
811 * DAI link ID of SSP & DMIC & HDMI are based on last
812 * link ID used by sdw link. Since be_id may be changed
813 * in init func of sdw codec, it is not equal to be_id
815 static inline int get_next_be_id(struct snd_soc_dai_link *links,
816 int be_id)
818 return links[be_id - 1].id + 1;
821 #define IDISP_CODEC_MASK 0x4
823 static int sof_card_codec_conf_alloc(struct device *dev,
824 struct snd_soc_acpi_mach_params *mach_params,
825 struct snd_soc_codec_conf **codec_conf,
826 int *codec_conf_count)
828 const struct snd_soc_acpi_link_adr *adr_link;
829 struct snd_soc_codec_conf *c_conf;
830 int num_codecs = 0;
831 int i;
833 adr_link = mach_params->links;
834 if (!adr_link)
835 return -EINVAL;
837 /* generate DAI links by each sdw link */
838 for (; adr_link->num_adr; adr_link++) {
839 for (i = 0; i < adr_link->num_adr; i++) {
840 if (!adr_link->adr_d[i].name_prefix) {
841 dev_err(dev, "codec 0x%llx does not have a name prefix\n",
842 adr_link->adr_d[i].adr);
843 return -EINVAL;
846 num_codecs += adr_link->num_adr;
849 c_conf = devm_kzalloc(dev, num_codecs * sizeof(*c_conf), GFP_KERNEL);
850 if (!c_conf)
851 return -ENOMEM;
853 *codec_conf = c_conf;
854 *codec_conf_count = num_codecs;
856 return 0;
859 static int sof_card_dai_links_create(struct device *dev,
860 struct snd_soc_acpi_mach *mach,
861 struct snd_soc_card *card)
863 int ssp_num, sdw_be_num = 0, hdmi_num = 0, dmic_num;
864 struct mc_private *ctx = snd_soc_card_get_drvdata(card);
865 struct snd_soc_dai_link_component *idisp_components;
866 struct snd_soc_dai_link_component *ssp_components;
867 struct snd_soc_acpi_mach_params *mach_params;
868 const struct snd_soc_acpi_link_adr *adr_link;
869 struct snd_soc_dai_link_component *cpus;
870 struct snd_soc_codec_conf *codec_conf;
871 int codec_conf_count;
872 int codec_conf_index = 0;
873 bool group_generated[SDW_MAX_GROUPS];
874 int ssp_codec_index, ssp_mask;
875 struct snd_soc_dai_link *links;
876 int num_links, link_id = 0;
877 char *name, *cpu_name;
878 int total_cpu_dai_num;
879 int sdw_cpu_dai_num;
880 int i, j, be_id = 0;
881 int cpu_id = 0;
882 int comp_num;
883 int ret;
885 mach_params = &mach->mach_params;
887 /* allocate codec conf, will be populated when dailinks are created */
888 ret = sof_card_codec_conf_alloc(dev, mach_params, &codec_conf, &codec_conf_count);
889 if (ret < 0)
890 return ret;
892 /* reset amp_num to ensure amp_num++ starts from 0 in each probe */
893 for (i = 0; i < ARRAY_SIZE(codec_info_list); i++)
894 codec_info_list[i].amp_num = 0;
896 if (sof_sdw_quirk & SOF_SDW_TGL_HDMI)
897 hdmi_num = SOF_TGL_HDMI_COUNT;
898 else
899 hdmi_num = SOF_PRE_TGL_HDMI_COUNT;
901 ssp_mask = SOF_SSP_GET_PORT(sof_sdw_quirk);
903 * on generic tgl platform, I2S or sdw mode is supported
904 * based on board rework. A ACPI device is registered in
905 * system only when I2S mode is supported, not sdw mode.
906 * Here check ACPI ID to confirm I2S is supported.
908 ssp_codec_index = find_codec_info_acpi(mach->id);
909 ssp_num = ssp_codec_index >= 0 ? hweight_long(ssp_mask) : 0;
910 comp_num = hdmi_num + ssp_num;
912 ret = get_sdw_dailink_info(mach_params->links,
913 &sdw_be_num, &sdw_cpu_dai_num);
914 if (ret < 0) {
915 dev_err(dev, "failed to get sdw link info %d", ret);
916 return ret;
919 if (mach_params->codec_mask & IDISP_CODEC_MASK)
920 ctx->idisp_codec = true;
922 /* enable dmic01 & dmic16k */
923 dmic_num = (sof_sdw_quirk & SOF_SDW_PCH_DMIC) ? 2 : 0;
924 comp_num += dmic_num;
926 dev_dbg(dev, "sdw %d, ssp %d, dmic %d, hdmi %d", sdw_be_num, ssp_num,
927 dmic_num, ctx->idisp_codec ? hdmi_num : 0);
929 /* allocate BE dailinks */
930 num_links = comp_num + sdw_be_num;
931 links = devm_kcalloc(dev, num_links, sizeof(*links), GFP_KERNEL);
933 /* allocated CPU DAIs */
934 total_cpu_dai_num = comp_num + sdw_cpu_dai_num;
935 cpus = devm_kcalloc(dev, total_cpu_dai_num, sizeof(*cpus),
936 GFP_KERNEL);
938 if (!links || !cpus)
939 return -ENOMEM;
941 /* SDW */
942 if (!sdw_be_num)
943 goto SSP;
945 adr_link = mach_params->links;
946 if (!adr_link)
947 return -EINVAL;
950 * SoundWire Slaves aggregated in the same group may be
951 * located on different hardware links. Clear array to indicate
952 * CPU DAIs for this group have not been generated.
954 for (i = 0; i < SDW_MAX_GROUPS; i++)
955 group_generated[i] = false;
957 /* generate DAI links by each sdw link */
958 for (; adr_link->num_adr; adr_link++) {
959 const struct snd_soc_acpi_endpoint *endpoint;
961 endpoint = adr_link->adr_d->endpoints;
962 if (endpoint->aggregated && !endpoint->group_id) {
963 dev_err(dev, "invalid group id on link %x",
964 adr_link->mask);
965 continue;
968 /* this group has been generated */
969 if (endpoint->aggregated &&
970 group_generated[endpoint->group_id])
971 continue;
973 ret = create_sdw_dailink(dev, &be_id, links, sdw_be_num,
974 sdw_cpu_dai_num, cpus, adr_link,
975 &cpu_id, group_generated,
976 codec_conf, codec_conf_count,
977 &codec_conf_index);
978 if (ret < 0) {
979 dev_err(dev, "failed to create dai link %d", be_id);
980 return -ENOMEM;
984 /* non-sdw DAI follows sdw DAI */
985 link_id = be_id;
987 /* get BE ID for non-sdw DAI */
988 be_id = get_next_be_id(links, be_id);
990 SSP:
991 /* SSP */
992 if (!ssp_num)
993 goto DMIC;
995 for (i = 0, j = 0; ssp_mask; i++, ssp_mask >>= 1) {
996 struct sof_sdw_codec_info *info;
997 int playback, capture;
998 char *codec_name;
1000 if (!(ssp_mask & 0x1))
1001 continue;
1003 name = devm_kasprintf(dev, GFP_KERNEL,
1004 "SSP%d-Codec", i);
1005 if (!name)
1006 return -ENOMEM;
1008 cpu_name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d Pin", i);
1009 if (!cpu_name)
1010 return -ENOMEM;
1012 ssp_components = devm_kzalloc(dev, sizeof(*ssp_components),
1013 GFP_KERNEL);
1014 if (!ssp_components)
1015 return -ENOMEM;
1017 info = &codec_info_list[ssp_codec_index];
1018 codec_name = devm_kasprintf(dev, GFP_KERNEL, "i2c-%s:0%d",
1019 info->acpi_id, j++);
1020 if (!codec_name)
1021 return -ENOMEM;
1023 ssp_components->name = codec_name;
1024 ssp_components->dai_name = info->dai_name;
1025 cpus[cpu_id].dai_name = cpu_name;
1027 playback = info->direction[SNDRV_PCM_STREAM_PLAYBACK];
1028 capture = info->direction[SNDRV_PCM_STREAM_CAPTURE];
1029 init_dai_link(links + link_id, be_id, name,
1030 playback, capture,
1031 cpus + cpu_id, 1,
1032 ssp_components, 1,
1033 NULL, info->ops);
1035 ret = info->init(NULL, links + link_id, info, 0);
1036 if (ret < 0)
1037 return ret;
1039 INC_ID(be_id, cpu_id, link_id);
1042 DMIC:
1043 /* dmic */
1044 if (dmic_num > 0) {
1045 cpus[cpu_id].dai_name = "DMIC01 Pin";
1046 init_dai_link(links + link_id, be_id, "dmic01",
1047 0, 1, // DMIC only supports capture
1048 cpus + cpu_id, 1,
1049 dmic_component, 1,
1050 sof_sdw_dmic_init, NULL);
1051 INC_ID(be_id, cpu_id, link_id);
1053 cpus[cpu_id].dai_name = "DMIC16k Pin";
1054 init_dai_link(links + link_id, be_id, "dmic16k",
1055 0, 1, // DMIC only supports capture
1056 cpus + cpu_id, 1,
1057 dmic_component, 1,
1058 /* don't call sof_sdw_dmic_init() twice */
1059 NULL, NULL);
1060 INC_ID(be_id, cpu_id, link_id);
1063 /* HDMI */
1064 if (hdmi_num > 0) {
1065 idisp_components = devm_kcalloc(dev, hdmi_num,
1066 sizeof(*idisp_components),
1067 GFP_KERNEL);
1068 if (!idisp_components)
1069 return -ENOMEM;
1072 for (i = 0; i < hdmi_num; i++) {
1073 name = devm_kasprintf(dev, GFP_KERNEL,
1074 "iDisp%d", i + 1);
1075 if (!name)
1076 return -ENOMEM;
1078 if (ctx->idisp_codec) {
1079 idisp_components[i].name = "ehdaudio0D2";
1080 idisp_components[i].dai_name = devm_kasprintf(dev,
1081 GFP_KERNEL,
1082 "intel-hdmi-hifi%d",
1083 i + 1);
1084 if (!idisp_components[i].dai_name)
1085 return -ENOMEM;
1086 } else {
1087 idisp_components[i].name = "snd-soc-dummy";
1088 idisp_components[i].dai_name = "snd-soc-dummy-dai";
1091 cpu_name = devm_kasprintf(dev, GFP_KERNEL,
1092 "iDisp%d Pin", i + 1);
1093 if (!cpu_name)
1094 return -ENOMEM;
1096 cpus[cpu_id].dai_name = cpu_name;
1097 init_dai_link(links + link_id, be_id, name,
1098 1, 0, // HDMI only supports playback
1099 cpus + cpu_id, 1,
1100 idisp_components + i, 1,
1101 sof_sdw_hdmi_init, NULL);
1102 INC_ID(be_id, cpu_id, link_id);
1105 card->dai_link = links;
1106 card->num_links = num_links;
1108 card->codec_conf = codec_conf;
1109 card->num_configs = codec_conf_count;
1111 return 0;
1114 static int sof_sdw_card_late_probe(struct snd_soc_card *card)
1116 int i, ret;
1118 for (i = 0; i < ARRAY_SIZE(codec_info_list); i++) {
1119 if (!codec_info_list[i].late_probe)
1120 continue;
1122 ret = codec_info_list[i].codec_card_late_probe(card);
1123 if (ret < 0)
1124 return ret;
1127 return sof_sdw_hdmi_card_late_probe(card);
1130 /* SoC card */
1131 static const char sdw_card_long_name[] = "Intel Soundwire SOF";
1133 static struct snd_soc_card card_sof_sdw = {
1134 .name = "soundwire",
1135 .owner = THIS_MODULE,
1136 .late_probe = sof_sdw_card_late_probe,
1139 static int mc_probe(struct platform_device *pdev)
1141 struct snd_soc_card *card = &card_sof_sdw;
1142 struct snd_soc_acpi_mach *mach;
1143 struct mc_private *ctx;
1144 int amp_num = 0, i;
1145 int ret;
1147 dev_dbg(&pdev->dev, "Entry %s\n", __func__);
1149 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
1150 if (!ctx)
1151 return -ENOMEM;
1153 dmi_check_system(sof_sdw_quirk_table);
1155 if (quirk_override != -1) {
1156 dev_info(&pdev->dev, "Overriding quirk 0x%lx => 0x%x\n",
1157 sof_sdw_quirk, quirk_override);
1158 sof_sdw_quirk = quirk_override;
1160 log_quirks(&pdev->dev);
1162 INIT_LIST_HEAD(&ctx->hdmi_pcm_list);
1164 card->dev = &pdev->dev;
1165 snd_soc_card_set_drvdata(card, ctx);
1167 mach = pdev->dev.platform_data;
1168 ret = sof_card_dai_links_create(&pdev->dev, mach,
1169 card);
1170 if (ret < 0)
1171 return ret;
1173 ctx->common_hdmi_codec_drv = mach->mach_params.common_hdmi_codec_drv;
1176 * the default amp_num is zero for each codec and
1177 * amp_num will only be increased for active amp
1178 * codecs on used platform
1180 for (i = 0; i < ARRAY_SIZE(codec_info_list); i++)
1181 amp_num += codec_info_list[i].amp_num;
1183 card->components = devm_kasprintf(card->dev, GFP_KERNEL,
1184 "cfg-spk:%d cfg-amp:%d",
1185 (sof_sdw_quirk & SOF_SDW_FOUR_SPK)
1186 ? 4 : 2, amp_num);
1187 if (!card->components)
1188 return -ENOMEM;
1190 card->long_name = sdw_card_long_name;
1192 /* Register the card */
1193 ret = devm_snd_soc_register_card(&pdev->dev, card);
1194 if (ret) {
1195 dev_err(card->dev, "snd_soc_register_card failed %d\n", ret);
1196 return ret;
1199 platform_set_drvdata(pdev, card);
1201 return ret;
1204 static int mc_remove(struct platform_device *pdev)
1206 struct snd_soc_card *card = platform_get_drvdata(pdev);
1207 struct snd_soc_dai_link *link;
1208 int ret;
1209 int i, j;
1211 for (i = 0; i < ARRAY_SIZE(codec_info_list); i++) {
1212 if (!codec_info_list[i].exit)
1213 continue;
1215 * We don't need to call .exit function if there is no matched
1216 * dai link found.
1218 for_each_card_prelinks(card, j, link) {
1219 if (!strcmp(link->codecs[0].dai_name,
1220 codec_info_list[i].dai_name)) {
1221 ret = codec_info_list[i].exit(&pdev->dev, link);
1222 if (ret)
1223 dev_warn(&pdev->dev,
1224 "codec exit failed %d\n",
1225 ret);
1226 break;
1231 return 0;
1234 static struct platform_driver sof_sdw_driver = {
1235 .driver = {
1236 .name = "sof_sdw",
1237 .pm = &snd_soc_pm_ops,
1239 .probe = mc_probe,
1240 .remove = mc_remove,
1243 module_platform_driver(sof_sdw_driver);
1245 MODULE_DESCRIPTION("ASoC SoundWire Generic Machine driver");
1246 MODULE_AUTHOR("Bard Liao <yung-chuan.liao@linux.intel.com>");
1247 MODULE_AUTHOR("Rander Wang <rander.wang@linux.intel.com>");
1248 MODULE_AUTHOR("Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>");
1249 MODULE_LICENSE("GPL v2");
1250 MODULE_ALIAS("platform:sof_sdw");