Skip to content

Commit 9641e65

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 b0a6bab commit 9641e65

1 file changed

Lines changed: 171 additions & 3 deletions

File tree

sound/soc/sof/topology.c

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

2650+
sof_tplg_ops.complete = sof_complete;
24842651
if (sdev->dspless_mode_selected)
24852652
ret = snd_soc_tplg_component_load(scomp, &sof_dspless_tplg_ops, fw);
24862653
else
@@ -2493,6 +2660,7 @@ int snd_sof_load_topology(struct snd_soc_component *scomp, const char *file)
24932660

24942661
release_firmware(fw);
24952662

2663+
out:
24962664
if (ret >= 0 && sdev->led_present)
24972665
ret = snd_ctl_led_request();
24982666

0 commit comments

Comments
 (0)