Skip to content

Commit 5bffc96

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 7579776 commit 5bffc96

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
@@ -27,25 +27,105 @@ enum tplg_device_id {
2727

2828
#define SOF_INTEL_PLATFORM_NAME_MAX 4
2929

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

43-
ret = sscanf(mach->sof_tplg_filename, "sof-%3s-*.tplg", platform);
44-
if (ret != 1) {
45-
dev_err(card->dev, "Invalid platform name %s of tplg %s\n",
46-
platform, mach->sof_tplg_filename);
47-
return -EINVAL;
48-
}
126+
ret = get_platform_name(card, mach, platform);
127+
if (ret < 0)
128+
return ret;
49129

50130
for_each_card_prelinks(card, i, dai_link) {
51131
char *tplg_dev_name;
@@ -64,20 +144,9 @@ int sof_sdw_get_tplg_files(struct snd_soc_card *card, const struct snd_soc_acpi_
64144
tplg_dev = TPLG_DEVICE_SDCA_MIC;
65145
tplg_dev_name = "sdca-mic";
66146
} else if (strstr(dai_link->name, "dmic")) {
67-
switch (mach_params.dmic_num) {
68-
case 2:
69-
tplg_dev_name = "dmic-2ch";
70-
break;
71-
case 4:
72-
tplg_dev_name = "dmic-4ch";
73-
break;
74-
default:
75-
dev_warn(card->dev,
76-
"unsupported number of dmics: %d\n",
77-
mach_params.dmic_num);
147+
if (get_dmic_tplg_dev(card->dev, mach_params.dmic_num,
148+
&tplg_dev, &tplg_dev_name) < 0)
78149
continue;
79-
}
80-
tplg_dev = TPLG_DEVICE_INTEL_PCH_DMIC;
81150
} else if (strstr(dai_link->name, "iDisp")) {
82151
tplg_dev = TPLG_DEVICE_HDMI;
83152
tplg_dev_name = "hdmi-pcm5";
@@ -97,25 +166,9 @@ int sof_sdw_get_tplg_files(struct snd_soc_card *card, const struct snd_soc_acpi_
97166

98167
tplg_mask |= BIT(tplg_dev);
99168

100-
/*
101-
* The tplg file naming rule is sof-<platform>-<function>-id<BE id number>.tplg
102-
* where <platform> is only required for the DMIC function as the nhlt blob
103-
* is platform dependent.
104-
*/
105-
switch (tplg_dev) {
106-
case TPLG_DEVICE_INTEL_PCH_DMIC:
107-
(*tplg_files)[tplg_num] = devm_kasprintf(card->dev, GFP_KERNEL,
108-
"%s/sof-%s-%s-id%d.tplg",
109-
prefix, platform,
110-
tplg_dev_name, dai_link->id);
111-
break;
112-
default:
113-
(*tplg_files)[tplg_num] = devm_kasprintf(card->dev, GFP_KERNEL,
114-
"%s/sof-%s-id%d.tplg",
115-
prefix, tplg_dev_name,
116-
dai_link->id);
117-
break;
118-
}
169+
(*tplg_files)[tplg_num] = get_tplg_filename(card->dev, prefix, platform,
170+
tplg_dev_name, dai_link->id,
171+
tplg_dev);
119172
if (!(*tplg_files)[tplg_num])
120173
return -ENOMEM;
121174
tplg_num++;
@@ -124,20 +177,10 @@ int sof_sdw_get_tplg_files(struct snd_soc_card *card, const struct snd_soc_acpi_
124177
dev_dbg(card->dev, "tplg_mask %#lx tplg_num %d\n", tplg_mask, tplg_num);
125178

126179
/* Check presence of sub-topologies */
127-
for (i = 0; i < tplg_num; i++) {
128-
ret = firmware_request_nowarn(&fw, (*tplg_files)[i], card->dev);
129-
if (!ret) {
130-
release_firmware(fw);
131-
} else {
132-
dev_warn(card->dev,
133-
"Failed to open topology file: %s, you might need to\n",
134-
(*tplg_files)[i]);
135-
dev_warn(card->dev,
136-
"download it from https://github.com/thesofproject/sof-bin/\n");
137-
return 0;
138-
}
139-
}
180+
if (all_tplg_files_exist(card->dev, tplg_files, tplg_num))
181+
return tplg_num;
140182

141-
return tplg_num;
183+
/* return 0 to use monolithic topology */
184+
return 0;
142185
}
143186
EXPORT_SYMBOL_GPL(sof_sdw_get_tplg_files);

0 commit comments

Comments
 (0)