Skip to content

Commit 95da27b

Browse files
committed
ASoC: SOF: topology: load multiple topologies
Get device information from dai links and load topology for each device. This allow user create a topology for single device. The driver will select the needed topologies and we don't need to create topologies for each product. Signed-off-by: Bard Liao <yung-chuan.liao@linux.intel.com>
1 parent 6bc9bd3 commit 95da27b

File tree

1 file changed

+167
-3
lines changed

1 file changed

+167
-3
lines changed

sound/soc/sof/topology.c

Lines changed: 167 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <linux/errno.h>
1414
#include <linux/firmware.h>
1515
#include <linux/workqueue.h>
16+
#include <sound/soc_sdw_utils.h>
1617
#include <sound/tlv.h>
1718
#include <uapi/sound/sof/tokens.h>
1819
#include "sof-priv.h"
@@ -2288,7 +2289,7 @@ static const struct snd_soc_tplg_bytes_ext_ops sof_bytes_ext_ops[] = {
22882289
{SOF_TPLG_KCTL_BYTES_VOLATILE_RO, snd_sof_bytes_ext_volatile_get},
22892290
};
22902291

2291-
static const struct snd_soc_tplg_ops sof_tplg_ops = {
2292+
static struct snd_soc_tplg_ops sof_tplg_ops = {
22922293
/* external kcontrol init - used for any driver specific init */
22932294
.control_load = sof_control_load,
22942295
.control_unload = sof_control_unload,
@@ -2311,7 +2312,7 @@ static const struct snd_soc_tplg_ops sof_tplg_ops = {
23112312
.link_unload = sof_link_unload,
23122313

23132314
/* completion - called at completion of firmware loading */
2314-
.complete = sof_complete,
2315+
/* complete will be added in the last tplg ops */
23152316

23162317
/* manifest - optional to inform component of manifest */
23172318
.manifest = sof_manifest,
@@ -2464,15 +2465,176 @@ static const struct snd_soc_tplg_ops sof_dspless_tplg_ops = {
24642465
.bytes_ext_ops_count = ARRAY_SIZE(sof_dspless_bytes_ext_ops),
24652466
};
24662467

2468+
#define MAX_TPLG_NUM 5
2469+
2470+
enum tplg_device_id {
2471+
TPLG_DEVICE_SDW_JACK,
2472+
TPLG_DEVICE_SDW_AMP,
2473+
TPLG_DEVICE_SDW_MIC,
2474+
TPLG_DEVICE_HOST_DMIC,
2475+
TPLG_DEVICE_HDMI,
2476+
};
2477+
2478+
#define SDCA_DEVICE_MASK (BIT(TPLG_DEVICE_SDW_JACK) | BIT(TPLG_DEVICE_SDW_AMP) | \
2479+
BIT(TPLG_DEVICE_SDW_MIC))
2480+
2481+
struct topology_file {
2482+
char *device;
2483+
char *file;
2484+
int be_id;
2485+
};
2486+
2487+
static bool is_platform_support_separated_tplg(const char *platform)
2488+
{
2489+
if (!strcmp(platform, "mtl") || !strcmp(platform, "lnl") || !strcmp(platform, "ptl"))
2490+
return true;
2491+
2492+
return false;
2493+
}
2494+
2495+
/* The code is from https://stackoverflow.com/questions/47116974/remove-a-substring-from-a-string-in-c */
2496+
static char *strremove(char *str, const char *sub)
2497+
{
2498+
size_t len = strlen(sub);
2499+
2500+
if (len > 0) {
2501+
char *p = str;
2502+
size_t size = 0;
2503+
2504+
while ((p = strstr(p, sub)) != NULL) {
2505+
size = (size == 0) ? (p - str) + strlen(p + len) + 1 : size - len;
2506+
memmove(p, p + len, size - (p - str));
2507+
}
2508+
}
2509+
return str;
2510+
}
2511+
24672512
int snd_sof_load_topology(struct snd_soc_component *scomp, const char *file)
24682513
{
24692514
struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2515+
struct snd_sof_pdata *sof_pdata = sdev->pdata;
2516+
struct topology_file tplg_files[MAX_TPLG_NUM];
2517+
struct snd_soc_dai_link *dai_link;
24702518
const struct firmware *fw;
2519+
bool load_default_tplg = false;
2520+
unsigned long tplg_mask = 0;
2521+
char platform[4];
2522+
char *tplg_name;
2523+
int tplg_num = 0;
2524+
int tplg_dev;
24712525
int ret;
2526+
int i;
24722527

24732528
dev_dbg(scomp->dev, "loading topology:%s\n", file);
24742529

2475-
ret = request_firmware(&fw, file, scomp->dev);
2530+
tplg_name = (char *)file;
2531+
if (sdev->pdata->ipc_type == SOF_IPC_TYPE_3)
2532+
goto legacy_tplg;
2533+
2534+
ret = sscanf(sof_pdata->tplg_filename, "sof-%3s-*.tplg", platform);
2535+
if (!is_platform_support_separated_tplg(platform))
2536+
goto legacy_tplg;
2537+
2538+
for_each_card_prelinks(scomp->card, i, dai_link) {
2539+
char *tplg_device;
2540+
2541+
if (tplg_num >= MAX_TPLG_NUM) {
2542+
dev_err(scomp->dev,
2543+
"Invalid tplg_num %d, check what happened\n", tplg_num);
2544+
return -EINVAL;
2545+
}
2546+
2547+
dev_dbg(scomp->dev, "dai_link %s id %d\n", dai_link->name, dai_link->id);
2548+
if (strstr(dai_link->name, "SimpleJack")) {
2549+
tplg_dev = TPLG_DEVICE_SDW_JACK;
2550+
tplg_device = "sdca-jack";
2551+
} else if (strstr(dai_link->name, "SmartAmp")) {
2552+
tplg_dev = TPLG_DEVICE_SDW_AMP;
2553+
tplg_device = devm_kasprintf(sdev->dev, GFP_KERNEL,
2554+
"sdca-%damp", dai_link->num_cpus);
2555+
if (!tplg_device)
2556+
return -ENOMEM;
2557+
} else if (strstr(dai_link->name, "SmartMic")) {
2558+
tplg_dev = TPLG_DEVICE_SDW_MIC;
2559+
tplg_device = "sdca-mic";
2560+
} else if (strstr(dai_link->name, "dmic")) {
2561+
if (strstr(file, "-2ch")) {
2562+
tplg_device = "dmic-2ch";
2563+
tplg_name = strremove(tplg_name, "-2ch");
2564+
} else if (strstr(file, "-4ch")) {
2565+
tplg_device = "dmic-4ch";
2566+
tplg_name = strremove(tplg_name, "-4ch");
2567+
} else {
2568+
dev_warn(scomp->dev,
2569+
"only -2ch and -4ch are supported for dmic\n");
2570+
continue;
2571+
}
2572+
tplg_dev = TPLG_DEVICE_HOST_DMIC;
2573+
} else if (strstr(dai_link->name, "iDisp")) {
2574+
tplg_dev = TPLG_DEVICE_HDMI;
2575+
tplg_device = "sdca-hdmi";
2576+
2577+
} else {
2578+
/* The dai link is not supported by sperated tplg yet */
2579+
load_default_tplg = true;
2580+
continue;
2581+
}
2582+
if (tplg_mask & BIT(tplg_dev))
2583+
continue;
2584+
tplg_mask |= BIT(tplg_dev);
2585+
tplg_files[tplg_num].be_id = dai_link->id;
2586+
tplg_files[tplg_num].device = tplg_device;
2587+
tplg_num++;
2588+
}
2589+
dev_dbg(scomp->dev, "tplg_mask %#lx tplg_num %d\n", tplg_mask, tplg_num);
2590+
2591+
/* Currently, only SDCA topology supported */
2592+
if (!(tplg_mask & SDCA_DEVICE_MASK))
2593+
goto legacy_tplg;
2594+
2595+
for (i = 0; i < tplg_num; i++) {
2596+
tplg_files[i].file = devm_kasprintf(sdev->dev, GFP_KERNEL,
2597+
"%s/sof-%s-%s-id%d.tplg",
2598+
sof_pdata->tplg_filename_prefix, platform,
2599+
tplg_files[i].device,
2600+
tplg_files[i].be_id);
2601+
if (!tplg_files[i].file)
2602+
return -ENOMEM;
2603+
2604+
dev_dbg(scomp->dev, "Requesting %d %s\n", i, tplg_files[i].file);
2605+
ret = request_firmware(&fw, tplg_files[i].file, scomp->dev);
2606+
if (ret < 0) {
2607+
if (i == 0) {
2608+
dev_dbg(scomp->dev, "Fail back to %s\n", tplg_name);
2609+
goto legacy_tplg;
2610+
}
2611+
2612+
dev_err(scomp->dev, "error: tplg request firmware %s failed err: %d\n",
2613+
tplg_files[i].file, ret);
2614+
goto out;
2615+
}
2616+
/* set complete = sof_complete if it is the last topology */
2617+
if (!load_default_tplg && i == tplg_num - 1)
2618+
sof_tplg_ops.complete = sof_complete;
2619+
if (sdev->dspless_mode_selected)
2620+
ret = snd_soc_tplg_component_load(scomp, &sof_dspless_tplg_ops, fw);
2621+
else
2622+
ret = snd_soc_tplg_component_load(scomp, &sof_tplg_ops, fw);
2623+
2624+
release_firmware(fw);
2625+
2626+
if (ret < 0) {
2627+
dev_err(scomp->dev, "error: tplg component load failed %d\n",
2628+
ret);
2629+
return ret;
2630+
}
2631+
}
2632+
/* Load topology successfully, goto out */
2633+
if (!load_default_tplg)
2634+
goto out;
2635+
2636+
legacy_tplg:
2637+
ret = request_firmware(&fw, tplg_name, scomp->dev);
24762638
if (ret < 0) {
24772639
dev_err(scomp->dev, "error: tplg request firmware %s failed err: %d\n",
24782640
file, ret);
@@ -2481,6 +2643,7 @@ int snd_sof_load_topology(struct snd_soc_component *scomp, const char *file)
24812643
return ret;
24822644
}
24832645

2646+
sof_tplg_ops.complete = sof_complete;
24842647
if (sdev->dspless_mode_selected)
24852648
ret = snd_soc_tplg_component_load(scomp, &sof_dspless_tplg_ops, fw);
24862649
else
@@ -2493,6 +2656,7 @@ int snd_sof_load_topology(struct snd_soc_component *scomp, const char *file)
24932656

24942657
release_firmware(fw);
24952658

2659+
out:
24962660
if (ret >= 0 && sdev->led_present)
24972661
ret = snd_ctl_led_request();
24982662

0 commit comments

Comments
 (0)