Skip to content

Commit 8f5e601

Browse files
bardliaolgirdwood
authored andcommitted
ASoC: Intel: sof-function-topology-lib: create common helpers
The existing code supports get_function_tplg_files callback for SoundWire machine driver only. Some common sections can be used to extend the support to other machines. Signed-off-by: Bard Liao <yung-chuan.liao@linux.intel.com>
1 parent eaf8955 commit 8f5e601

1 file changed

Lines changed: 91 additions & 49 deletions

File tree

sound/soc/intel/common/sof-function-topology-lib.c

Lines changed: 91 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,87 @@ enum tplg_device_id {
2828

2929
#define SOF_INTEL_PLATFORM_NAME_MAX 4
3030

31+
static int get_platform_name(struct snd_soc_card *card,
32+
const struct snd_soc_acpi_mach *mach, char *platform)
33+
{
34+
int ret;
35+
36+
ret = sscanf(mach->sof_tplg_filename, "sof-%3s-*.tplg", platform);
37+
if (ret != 1) {
38+
dev_err(card->dev, "Invalid platform name of tplg %s\n",
39+
mach->sof_tplg_filename);
40+
return -EINVAL;
41+
}
42+
43+
return 0;
44+
}
45+
46+
static bool tplg_files_exist(struct device *dev, const char *tplg_files)
47+
{
48+
const struct firmware *fw;
49+
int ret;
50+
51+
ret = firmware_request_nowarn(&fw, tplg_files, dev);
52+
if (!ret) {
53+
release_firmware(fw);
54+
return true;
55+
}
56+
57+
dev_warn(dev,
58+
"Failed to open topology file: %s, you might need to\n",
59+
tplg_files);
60+
dev_warn(dev,
61+
"download it from https://github.com/thesofproject/sof-bin/\n");
62+
return false;
63+
64+
}
65+
66+
static char *get_tplg_filename(struct device *dev, const char *prefix,
67+
const char *platform, const char *tplg_dev_name,
68+
int dai_link_id, int tplg_dev)
69+
{
70+
char *filename = NULL;
71+
72+
/*
73+
* The tplg file naming rule is sof-<platform>-<function>-id<BE id number>.tplg
74+
* where <platform> is only required for the devices that need NHLT blob like DMIC
75+
* as the nhlt blob is platform dependent.
76+
*/
77+
switch (tplg_dev) {
78+
case TPLG_DEVICE_INTEL_PCH_DMIC:
79+
filename = devm_kasprintf(dev, GFP_KERNEL, "%s/sof-%s-%s-id%d.tplg",
80+
prefix, platform, tplg_dev_name, dai_link_id);
81+
break;
82+
default:
83+
filename = devm_kasprintf(dev, GFP_KERNEL, "%s/sof-%s-id%d.tplg",
84+
prefix, tplg_dev_name, dai_link_id);
85+
break;
86+
}
87+
88+
return filename;
89+
}
90+
91+
static int get_dmic_tplg_dev(struct device *dev, int dmic_num,
92+
int *tplg_dev, char **tplg_dev_name)
93+
{
94+
switch (dmic_num) {
95+
case 2:
96+
*tplg_dev_name = "dmic-2ch";
97+
break;
98+
case 4:
99+
*tplg_dev_name = "dmic-4ch";
100+
break;
101+
default:
102+
dev_warn(dev,
103+
"unsupported number of dmics: %d\n",
104+
dmic_num);
105+
return -EINVAL;
106+
}
107+
*tplg_dev = TPLG_DEVICE_INTEL_PCH_DMIC;
108+
109+
return 0;
110+
}
111+
31112
int sof_sdw_get_tplg_files(struct snd_soc_card *card, const struct snd_soc_acpi_mach *mach,
32113
const char *prefix, const char ***tplg_files, bool best_effort)
33114
{
@@ -38,20 +119,16 @@ int sof_sdw_get_tplg_files(struct snd_soc_card *card, const struct snd_soc_acpi_
38119
*/
39120
struct snd_soc_acpi_mach_params mach_params = card_mach->mach_params;
40121
struct snd_soc_dai_link *dai_link;
41-
const struct firmware *fw;
42122
char platform[SOF_INTEL_PLATFORM_NAME_MAX];
43123
unsigned long tplg_mask = 0;
44124
int tplg_num = 0;
45125
int tplg_dev;
46126
int ret;
47127
int i;
48128

49-
ret = sscanf(mach->sof_tplg_filename, "sof-%3s-*.tplg", platform);
50-
if (ret != 1) {
51-
dev_err(card->dev, "Invalid platform name %s of tplg %s\n",
52-
platform, mach->sof_tplg_filename);
53-
return -EINVAL;
54-
}
129+
ret = get_platform_name(card, mach, platform);
130+
if (ret < 0)
131+
return ret;
55132

56133
for_each_card_prelinks(card, i, dai_link) {
57134
char *tplg_dev_name;
@@ -70,20 +147,9 @@ int sof_sdw_get_tplg_files(struct snd_soc_card *card, const struct snd_soc_acpi_
70147
tplg_dev = TPLG_DEVICE_SDCA_MIC;
71148
tplg_dev_name = "sdca-mic";
72149
} else if (strstr(dai_link->name, "dmic")) {
73-
switch (mach_params.dmic_num) {
74-
case 2:
75-
tplg_dev_name = "dmic-2ch";
76-
break;
77-
case 4:
78-
tplg_dev_name = "dmic-4ch";
79-
break;
80-
default:
81-
dev_warn(card->dev,
82-
"unsupported number of dmics: %d\n",
83-
mach_params.dmic_num);
150+
if (get_dmic_tplg_dev(card->dev, mach_params.dmic_num,
151+
&tplg_dev, &tplg_dev_name) < 0)
84152
continue;
85-
}
86-
tplg_dev = TPLG_DEVICE_INTEL_PCH_DMIC;
87153
} else if (strstr(dai_link->name, "iDisp")) {
88154
tplg_dev = TPLG_DEVICE_HDMI;
89155
tplg_dev_name = "hdmi-pcm5";
@@ -111,25 +177,9 @@ int sof_sdw_get_tplg_files(struct snd_soc_card *card, const struct snd_soc_acpi_
111177

112178
tplg_mask |= BIT(tplg_dev);
113179

114-
/*
115-
* The tplg file naming rule is sof-<platform>-<function>-id<BE id number>.tplg
116-
* where <platform> is only required for the DMIC function as the nhlt blob
117-
* is platform dependent.
118-
*/
119-
switch (tplg_dev) {
120-
case TPLG_DEVICE_INTEL_PCH_DMIC:
121-
(*tplg_files)[tplg_num] = devm_kasprintf(card->dev, GFP_KERNEL,
122-
"%s/sof-%s-%s-id%d.tplg",
123-
prefix, platform,
124-
tplg_dev_name, dai_link->id);
125-
break;
126-
default:
127-
(*tplg_files)[tplg_num] = devm_kasprintf(card->dev, GFP_KERNEL,
128-
"%s/sof-%s-id%d.tplg",
129-
prefix, tplg_dev_name,
130-
dai_link->id);
131-
break;
132-
}
180+
(*tplg_files)[tplg_num] = get_tplg_filename(card->dev, prefix, platform,
181+
tplg_dev_name, dai_link->id,
182+
tplg_dev);
133183
if (!(*tplg_files)[tplg_num])
134184
return -ENOMEM;
135185
tplg_num++;
@@ -139,17 +189,9 @@ int sof_sdw_get_tplg_files(struct snd_soc_card *card, const struct snd_soc_acpi_
139189

140190
/* Check presence of sub-topologies */
141191
for (i = 0; i < tplg_num; i++) {
142-
ret = firmware_request_nowarn(&fw, (*tplg_files)[i], card->dev);
143-
if (!ret) {
144-
release_firmware(fw);
145-
} else {
146-
dev_warn(card->dev,
147-
"Failed to open topology file: %s, you might need to\n",
148-
(*tplg_files)[i]);
149-
dev_warn(card->dev,
150-
"download it from https://github.com/thesofproject/sof-bin/\n");
192+
if (!tplg_files_exist(card->dev, (*tplg_files)[i]))
193+
/* return 0 to use monolithic topology */
151194
return 0;
152-
}
153195
}
154196

155197
return tplg_num;

0 commit comments

Comments
 (0)