Skip to content

Commit 2867dcf

Browse files
committed
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 826f58d commit 2867dcf

1 file changed

Lines changed: 96 additions & 53 deletions

File tree

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

Lines changed: 96 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,90 @@ 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 all_tplg_files_exist(struct device *dev, const char ***tplg_files, int tplg_num)
47+
{
48+
const struct firmware *fw;
49+
int ret;
50+
int i;
51+
52+
for (i = 0; i < tplg_num; i++) {
53+
ret = firmware_request_nowarn(&fw, (*tplg_files)[i], dev);
54+
if (!ret) {
55+
release_firmware(fw);
56+
} else {
57+
dev_warn(dev,
58+
"Failed to open topology file: %s, you might need to\n",
59+
(*tplg_files)[i]);
60+
dev_warn(dev,
61+
"download it from https://github.com/thesofproject/sof-bin/\n");
62+
return false;
63+
}
64+
}
65+
66+
return true;
67+
}
68+
69+
static char *get_tplg_filename(struct device *dev, const char *prefix,
70+
const char *platform, const char *tplg_dev_name,
71+
int dai_link_id, int tplg_dev)
72+
{
73+
char *filename = NULL;
74+
75+
/*
76+
* The tplg file naming rule is sof-<platform>-<function>-id<BE id number>.tplg
77+
* where <platform> is only required for the devices that need NHLT blob like DMIC
78+
* as the nhlt blob is platform dependent.
79+
*/
80+
switch (tplg_dev) {
81+
case TPLG_DEVICE_INTEL_PCH_DMIC:
82+
filename = devm_kasprintf(dev, GFP_KERNEL, "%s/sof-%s-%s-id%d.tplg",
83+
prefix, platform, tplg_dev_name, dai_link_id);
84+
break;
85+
default:
86+
filename = devm_kasprintf(dev, GFP_KERNEL, "%s/sof-%s-id%d.tplg",
87+
prefix, tplg_dev_name, dai_link_id);
88+
break;
89+
}
90+
91+
return filename;
92+
}
93+
94+
static int get_dmic_tplg_dev(struct device *dev, int dmic_num,
95+
int *tplg_dev, char **tplg_dev_name)
96+
{
97+
switch (dmic_num) {
98+
case 2:
99+
*tplg_dev_name = "dmic-2ch";
100+
break;
101+
case 4:
102+
*tplg_dev_name = "dmic-4ch";
103+
break;
104+
default:
105+
dev_warn(dev,
106+
"unsupported number of dmics: %d\n",
107+
dmic_num);
108+
return -EINVAL;
109+
}
110+
*tplg_dev = TPLG_DEVICE_INTEL_PCH_DMIC;
111+
112+
return 0;
113+
}
114+
31115
int sof_sdw_get_tplg_files(struct snd_soc_card *card, const struct snd_soc_acpi_mach *mach,
32116
const char *prefix, const char ***tplg_files, bool best_effort)
33117
{
@@ -38,20 +122,16 @@ int sof_sdw_get_tplg_files(struct snd_soc_card *card, const struct snd_soc_acpi_
38122
*/
39123
struct snd_soc_acpi_mach_params mach_params = card_mach->mach_params;
40124
struct snd_soc_dai_link *dai_link;
41-
const struct firmware *fw;
42125
char platform[SOF_INTEL_PLATFORM_NAME_MAX];
43126
unsigned long tplg_mask = 0;
44127
int tplg_num = 0;
45128
int tplg_dev;
46129
int ret;
47130
int i;
48131

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-
}
132+
ret = get_platform_name(card, mach, platform);
133+
if (ret < 0)
134+
return ret;
55135

56136
for_each_card_prelinks(card, i, dai_link) {
57137
char *tplg_dev_name;
@@ -70,20 +150,9 @@ int sof_sdw_get_tplg_files(struct snd_soc_card *card, const struct snd_soc_acpi_
70150
tplg_dev = TPLG_DEVICE_SDCA_MIC;
71151
tplg_dev_name = "sdca-mic";
72152
} 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);
153+
if (get_dmic_tplg_dev(card->dev, mach_params.dmic_num,
154+
&tplg_dev, &tplg_dev_name) < 0)
84155
continue;
85-
}
86-
tplg_dev = TPLG_DEVICE_INTEL_PCH_DMIC;
87156
} else if (strstr(dai_link->name, "iDisp")) {
88157
tplg_dev = TPLG_DEVICE_HDMI;
89158
tplg_dev_name = "hdmi-pcm5";
@@ -111,25 +180,9 @@ int sof_sdw_get_tplg_files(struct snd_soc_card *card, const struct snd_soc_acpi_
111180

112181
tplg_mask |= BIT(tplg_dev);
113182

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-
}
183+
(*tplg_files)[tplg_num] = get_tplg_filename(card->dev, prefix, platform,
184+
tplg_dev_name, dai_link->id,
185+
tplg_dev);
133186
if (!(*tplg_files)[tplg_num])
134187
return -ENOMEM;
135188
tplg_num++;
@@ -138,20 +191,10 @@ int sof_sdw_get_tplg_files(struct snd_soc_card *card, const struct snd_soc_acpi_
138191
dev_dbg(card->dev, "tplg_mask %#lx tplg_num %d\n", tplg_mask, tplg_num);
139192

140193
/* Check presence of sub-topologies */
141-
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");
151-
return 0;
152-
}
153-
}
194+
if (all_tplg_files_exist(card->dev, tplg_files, tplg_num))
195+
return tplg_num;
154196

155-
return tplg_num;
197+
/* return 0 to use monolithic topology */
198+
return 0;
156199
}
157200
EXPORT_SYMBOL_GPL(sof_sdw_get_tplg_files);

0 commit comments

Comments
 (0)