Skip to content

Commit 78eed41

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 78eed41

1 file changed

Lines changed: 164 additions & 3 deletions

File tree

sound/soc/sof/topology.c

Lines changed: 164 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,173 @@ 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+
struct topology_file {
2479+
char *device;
2480+
char *file;
2481+
int be_id;
2482+
};
2483+
2484+
static bool is_platform_support_separated_tplg(const char *platform)
2485+
{
2486+
if (!strcmp(platform, "hda") || !strcmp(platform, "mtl") ||
2487+
!strcmp(platform, "lnl") || !strcmp(platform, "ptl"))
2488+
return true;
2489+
2490+
return false;
2491+
}
2492+
2493+
/* The code is from https://stackoverflow.com/questions/47116974/remove-a-substring-from-a-string-in-c */
2494+
static char *strremove(char *str, const char *sub)
2495+
{
2496+
size_t len = strlen(sub);
2497+
2498+
if (len > 0) {
2499+
char *p = str;
2500+
size_t size = 0;
2501+
2502+
while ((p = strstr(p, sub)) != NULL) {
2503+
size = (size == 0) ? (p - str) + strlen(p + len) + 1 : size - len;
2504+
memmove(p, p + len, size - (p - str));
2505+
}
2506+
}
2507+
return str;
2508+
}
2509+
24672510
int snd_sof_load_topology(struct snd_soc_component *scomp, const char *file)
24682511
{
24692512
struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2513+
struct snd_sof_pdata *sof_pdata = sdev->pdata;
2514+
struct topology_file tplg_files[MAX_TPLG_NUM];
2515+
struct snd_soc_dai_link *dai_link;
24702516
const struct firmware *fw;
2517+
bool load_default_tplg = false;
2518+
unsigned long tplg_mask = 0;
2519+
char platform[4];
2520+
char *tplg_name;
2521+
int tplg_num = 0;
2522+
int tplg_dev;
24712523
int ret;
2524+
int i;
24722525

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

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

2643+
sof_tplg_ops.complete = sof_complete;
24842644
if (sdev->dspless_mode_selected)
24852645
ret = snd_soc_tplg_component_load(scomp, &sof_dspless_tplg_ops, fw);
24862646
else
@@ -2493,6 +2653,7 @@ int snd_sof_load_topology(struct snd_soc_component *scomp, const char *file)
24932653

24942654
release_firmware(fw);
24952655

2656+
out:
24962657
if (ret >= 0 && sdev->led_present)
24972658
ret = snd_ctl_led_request();
24982659

0 commit comments

Comments
 (0)