[PATCH v3 4/6] ASoC: amd: acp: add SOF function topology support

From: Syed Saba Kareem

Date: Sun Nov 08 2026 - 14:35:09 EST


From: Syed Saba Kareem <Syed.SabaKareem@xxxxxxx>

From: Siva Subramanian Ravi Saravanan <sravisar@xxxxxxx>

Add amd_sof_sdw_get_tplg_files() to build the list of SOF function
topologies from the card DAI links, so that a card can be formed from
per-function topologies instead of one monolithic topology per codec
configuration.

Signed-off-by: Siva Subramanian Ravi Saravanan <sravisar@xxxxxxx>
Signed-off-by: Syed Saba Kareem <Syed.SabaKareem@xxxxxxx>
---
sound/soc/amd/acp/Makefile | 3 +-
.../amd/acp/amd-sof-function-topology-lib.c | 129 ++++++++++++++++++
.../amd/acp/amd-sof-function-topology-lib.h | 19 +++
3 files changed, 150 insertions(+), 1 deletion(-)
create mode 100644 sound/soc/amd/acp/amd-sof-function-topology-lib.c
create mode 100644 sound/soc/amd/acp/amd-sof-function-topology-lib.h

diff --git a/sound/soc/amd/acp/Makefile b/sound/soc/amd/acp/Makefile
index 42e2bd34da06..91bbbeef5b8a 100644
--- a/sound/soc/amd/acp/Makefile
+++ b/sound/soc/amd/acp/Makefile
@@ -23,7 +23,8 @@ snd-acp70-y := acp70.o
snd-acp-mach-y := acp-mach-common.o
snd-acp-legacy-mach-y := acp-legacy-mach.o acp3x-es83xx/acp3x-es83xx.o
snd-acp-sof-mach-y := acp-sof-mach.o
-snd-soc-acpi-amd-match-y := amd-acp63-acpi-match.o amd-acp70-acpi-match.o amd-acp7x-acpi-match.o
+snd-soc-acpi-amd-match-y := amd-acp63-acpi-match.o amd-acp70-acpi-match.o amd-acp7x-acpi-match.o \
+ amd-sof-function-topology-lib.o
snd-soc-acpi-amd-match-$(CONFIG_SND_SOC_ACPI_AMD_SDCA_QUIRKS) += soc-acpi-amd-sdca-quirks.o
snd-acp-sdw-mach-y := acp-sdw-mach-common.o
snd-acp-sdw-sof-mach-y += acp-sdw-sof-mach.o
diff --git a/sound/soc/amd/acp/amd-sof-function-topology-lib.c b/sound/soc/amd/acp/amd-sof-function-topology-lib.c
new file mode 100644
index 000000000000..8d70d46d769e
--- /dev/null
+++ b/sound/soc/amd/acp/amd-sof-function-topology-lib.c
@@ -0,0 +1,129 @@
+// SPDX-License-Identifier: GPL-2.0-only
+//
+// Copyright(c) 2026 Advanced Micro Devices, Inc.
+//
+
+#include <linux/bits.h>
+#include <linux/device.h>
+#include <linux/errno.h>
+#include <linux/kernel.h>
+#include <linux/string.h>
+#include <sound/soc.h>
+#include <sound/soc-acpi.h>
+#include "amd-sof-function-topology-lib.h"
+
+#define SOF_AMD_PLATFORM_NAME_MAX 8
+#define SOF_AMD_SDW_MAX_LINKS 4
+
+static int get_sdw_link_num(struct device *dev, struct snd_soc_dai_link *dai_link,
+ unsigned int *link_num)
+{
+ if (sscanf(dai_link->name, "SDW%u", link_num) != 1 ||
+ *link_num >= SOF_AMD_SDW_MAX_LINKS) {
+ dev_err(dev, "Can't get SDW link from dai_link->name %s\n", dai_link->name);
+ return -EINVAL;
+ }
+
+ return 0;
+}
+
+int amd_sof_sdw_get_tplg_files(struct snd_soc_card *card, const struct snd_soc_acpi_mach *mach,
+ const char *prefix, const char ***tplg_files, bool best_effort)
+{
+ struct snd_soc_dai_link *dai_link;
+ char platform[SOF_AMD_PLATFORM_NAME_MAX];
+ unsigned long tplg_mask = 0;
+ int tplg_num = 0;
+ char *tplg_file;
+ int tplg_bit;
+ int tplg_dev;
+ int ret;
+ int i;
+
+ /* ACP SoundWire and PDM DAI configuration is platform dependent */
+ ret = snd_soc_acpi_get_platform_name(card, mach, platform);
+ if (ret < 0)
+ return ret;
+
+ /*
+ * ACP SoundWire DAI index and stream names in the topologies are
+ * specific to the SoundWire link, so the link number is part of the
+ * SoundWire function names.
+ */
+ for_each_card_prelinks(card, i, dai_link) {
+ unsigned int link_num = 0;
+ const char *tplg_dev_name;
+
+ dev_dbg(card->dev, "dai_link %s id %d\n", dai_link->name, dai_link->id);
+ if (strstr(dai_link->name, "SimpleJack")) {
+ ret = get_sdw_link_num(card->dev, dai_link, &link_num);
+ if (ret)
+ return ret;
+
+ tplg_dev = TPLG_DEVICE_ACP_SDW_JACK;
+ tplg_dev_name = devm_kasprintf(card->dev, GFP_KERNEL, "jack-l%u", link_num);
+ if (!tplg_dev_name)
+ return -ENOMEM;
+ } else if (strstr(dai_link->name, "SmartAmp")) {
+ ret = get_sdw_link_num(card->dev, dai_link, &link_num);
+ if (ret)
+ return ret;
+
+ tplg_dev = TPLG_DEVICE_ACP_SDW_AMP;
+ tplg_dev_name = devm_kasprintf(card->dev, GFP_KERNEL, "%damp-l%u",
+ dai_link->num_cpus, link_num);
+ if (!tplg_dev_name)
+ return -ENOMEM;
+ } else if (strstr(dai_link->name, "SmartMic")) {
+ ret = get_sdw_link_num(card->dev, dai_link, &link_num);
+ if (ret)
+ return ret;
+
+ tplg_dev = TPLG_DEVICE_ACP_SDW_MIC;
+ tplg_dev_name = devm_kasprintf(card->dev, GFP_KERNEL, "mic-l%u", link_num);
+ if (!tplg_dev_name)
+ return -ENOMEM;
+ } else if (strstr(dai_link->name, "dmic")) {
+ /* ACP PDM DMIC is always stereo */
+ tplg_dev = TPLG_DEVICE_ACP_DMIC;
+ tplg_dev_name = "dmic-2ch";
+ } else {
+ /* The dai link is not supported by separated tplg yet */
+ dev_dbg(card->dev,
+ "dai_link %s is not supported by separated tplg yet\n",
+ dai_link->name);
+ if (best_effort)
+ continue;
+
+ return 0;
+ }
+
+ /* the same function on different links needs its own topology */
+ tplg_bit = (tplg_dev - TPLG_DEVICE_ACP_SDW_JACK) * SOF_AMD_SDW_MAX_LINKS + link_num;
+ if (tplg_mask & BIT(tplg_bit))
+ continue;
+
+ tplg_file = snd_soc_acpi_get_tplg_filename(card->dev, prefix, platform,
+ tplg_dev_name, dai_link->id, tplg_dev);
+ if (!tplg_file)
+ return -ENOMEM;
+
+ /* Check presence of sub-topologies */
+ if (!snd_soc_acpi_tplg_files_exist(card->dev, tplg_file)) {
+ devm_kfree(card->dev, tplg_file);
+ if (best_effort)
+ continue;
+
+ return 0;
+ }
+
+ tplg_mask |= BIT(tplg_bit);
+
+ (*tplg_files)[tplg_num] = tplg_file;
+ tplg_num++;
+ }
+
+ dev_dbg(card->dev, "tplg_mask %#lx tplg_num %d\n", tplg_mask, tplg_num);
+
+ return tplg_num;
+}
diff --git a/sound/soc/amd/acp/amd-sof-function-topology-lib.h b/sound/soc/amd/acp/amd-sof-function-topology-lib.h
new file mode 100644
index 000000000000..cea49aa9c200
--- /dev/null
+++ b/sound/soc/amd/acp/amd-sof-function-topology-lib.h
@@ -0,0 +1,19 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * amd-sof-function-topology-lib.h - get-tplg-files ops
+ *
+ * Copyright(c) 2026 Advanced Micro Devices, Inc.
+ */
+
+#ifndef __AMD_SOF_FUNCTION_TOPOLOGY_LIB_H
+#define __AMD_SOF_FUNCTION_TOPOLOGY_LIB_H
+
+#include <linux/types.h>
+
+struct snd_soc_acpi_mach;
+struct snd_soc_card;
+
+int amd_sof_sdw_get_tplg_files(struct snd_soc_card *card, const struct snd_soc_acpi_mach *mach,
+ const char *prefix, const char ***tplg_files, bool best_effort);
+
+#endif
--
2.43.0