Skip to content

Commit 05a280a

Browse files
committed
ASoC: SOF: Intel: use sof_sdw as default SDW machine driver
If there is no SoundWire machine matches the existing acpi match table, get the required machine date from the acpi table and construct the link adrs and endpoints. Pass the data to the default Intel SoundWire machine driver. And we don't need to add new item to the acpi match table in common cases. Signed-off-by: Bard Liao <yung-chuan.liao@linux.intel.com>
1 parent 5b31d21 commit 05a280a

File tree

1 file changed

+175
-2
lines changed

1 file changed

+175
-2
lines changed

sound/soc/sof/intel/hda.c

Lines changed: 175 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,15 @@
2626
#include <sound/intel-dsp-config.h>
2727
#include <sound/intel-nhlt.h>
2828
#include <sound/soc-acpi-intel-ssp-common.h>
29+
#include <sound/soc_sdw_utils.h>
2930
#include <sound/sof.h>
3031
#include <sound/sof/xtensa.h>
3132
#include <sound/hda-mlink.h>
3233
#include "../sof-audio.h"
3334
#include "../sof-pci-dev.h"
3435
#include "../ops.h"
3536
#include "../ipc4-topology.h"
37+
#include "../../intel/common/sof-function-topology-lib.h"
3638
#include "hda.h"
3739

3840
#include <trace/events/sof_intel.h>
@@ -1117,14 +1119,153 @@ static void hda_generic_machine_select(struct snd_sof_dev *sdev,
11171119

11181120
#if IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_SOUNDWIRE)
11191121

1122+
static bool is_endpoint_present(struct sdw_slave *sdw_device,
1123+
struct asoc_sdw_codec_info *dai_info, int dai_type)
1124+
{
1125+
int i;
1126+
1127+
for (i = 0; i < sdw_device->sdca_data.num_functions; i++) {
1128+
if (dai_type == dai_info->dais[i].dai_type)
1129+
return true;
1130+
}
1131+
dev_dbg(&sdw_device->dev, "Endpoint DAI type %d not found\n", dai_type);
1132+
return false;
1133+
}
1134+
1135+
static struct snd_soc_acpi_adr_device *find_acpi_adr_device(struct device *dev,
1136+
struct sdw_slave *sdw_device,
1137+
struct snd_soc_acpi_link_adr *link,
1138+
int *amp_index)
1139+
{
1140+
struct snd_soc_acpi_adr_device *adr_dev;
1141+
int index = link->num_adr;
1142+
const char *name_prefix;
1143+
int ep_index = 0;
1144+
int i, j;
1145+
1146+
link->mask = BIT(sdw_device->bus->link_id);
1147+
/* index is 0 based, we need allocate index + 1 for the array size */
1148+
if (!index)
1149+
adr_dev = devm_kzalloc(dev, sizeof(*adr_dev), GFP_KERNEL);
1150+
else
1151+
adr_dev = devm_krealloc(dev, (struct snd_soc_acpi_adr_device *)link->adr_d,
1152+
(index + 1) * sizeof(*adr_dev), GFP_KERNEL);
1153+
1154+
if (!adr_dev)
1155+
return NULL;
1156+
1157+
for (i = 0; i < asoc_sdw_get_codec_info_list_count(); i++) {
1158+
struct snd_soc_acpi_endpoint *endpoints;
1159+
int amp_group_id = 1;
1160+
1161+
if (sdw_device->id.part_id != codec_info_list[i].part_id)
1162+
continue;
1163+
1164+
endpoints = devm_kcalloc(dev, codec_info_list[i].dai_num,
1165+
sizeof(struct snd_soc_acpi_endpoint), GFP_KERNEL);
1166+
if (!endpoints) {
1167+
dev_err(dev, "failed to allocate memory for endpoints\n");
1168+
return NULL;
1169+
}
1170+
1171+
name_prefix = codec_info_list[i].name_prefix;
1172+
for (j = 0; j < codec_info_list[i].dai_num; j++) {
1173+
/* Check if the endpoint is present by the SDCA DisCo table */
1174+
if (!is_endpoint_present(sdw_device, &codec_info_list[i],
1175+
codec_info_list[i].dais[j].dai_type))
1176+
continue;
1177+
1178+
endpoints[ep_index].num = ep_index;
1179+
if (codec_info_list[i].dais[j].dai_type == SOC_SDW_DAI_TYPE_AMP) {
1180+
/* Assume all amp are aggregated */
1181+
endpoints[ep_index].aggregated = 1;
1182+
endpoints[ep_index].group_id = amp_group_id;
1183+
endpoints[ep_index].group_position = *amp_index;
1184+
/* Set group id = 2 for feedback capture endpoint */
1185+
amp_group_id++;
1186+
} else {
1187+
endpoints[ep_index].aggregated = 0;
1188+
endpoints[ep_index].group_id = 0;
1189+
endpoints[ep_index].group_position = 0;
1190+
}
1191+
ep_index++;
1192+
}
1193+
adr_dev[index].endpoints = endpoints;
1194+
adr_dev[index].num_endpoints = ep_index;
1195+
break;
1196+
}
1197+
1198+
if (i == asoc_sdw_get_codec_info_list_count()) {
1199+
dev_err(dev, "part id %#x is not supported\n", sdw_device->id.part_id);
1200+
return NULL;
1201+
}
1202+
1203+
adr_dev[index].adr = ((u64)sdw_device->id.class_id & 0xFF) |
1204+
((u64)sdw_device->id.part_id & 0xFFFF) << 8 |
1205+
((u64)sdw_device->id.mfg_id & 0xFFFF) << 24 |
1206+
((u64)(sdw_device->id.unique_id & 0xF) << 40) |
1207+
((u64)(sdw_device->id.sdw_version & 0xF) << 44) |
1208+
((u64)(sdw_device->bus->link_id & 0xF) << 48);
1209+
1210+
/*
1211+
* The name_prefix comes from codec_info_list which has a name_prefix per codec.
1212+
* And we need to give a unique name_prefix for each amp and should be backwards
1213+
* compatible to the existing acpi match tables to not break existing UCMs.
1214+
* For the "AMP" name_prefix, we append the amp index to it. However, for the
1215+
* "Left" name_prefix, we convert the second amp name_prefix to "Right" and
1216+
* for the third and further amps, we set the name_prefix to "AMP<amp_index>".
1217+
*/
1218+
if (!strcmp(name_prefix, "AMP")) {
1219+
adr_dev[index].name_prefix = devm_kasprintf(dev, GFP_KERNEL, "%s%d",
1220+
name_prefix,
1221+
*amp_index);
1222+
(*amp_index)++;
1223+
} else if (!strcmp(name_prefix, "Left")) {
1224+
switch (*amp_index) {
1225+
case 1:
1226+
adr_dev[index].name_prefix = devm_kasprintf(dev, GFP_KERNEL,
1227+
"%s", "Left");
1228+
break;
1229+
case 2:
1230+
adr_dev[index].name_prefix = devm_kasprintf(dev, GFP_KERNEL,
1231+
"%s", "Right");
1232+
break;
1233+
default:
1234+
/* Set the name_fix to AMP<amp_index> if there are more than 2 amps */
1235+
adr_dev[index].name_prefix = devm_kasprintf(dev, GFP_KERNEL, "%s%d",
1236+
"AMP", *amp_index);
1237+
break;
1238+
}
1239+
(*amp_index)++;
1240+
} else {
1241+
/* For all other codecs, get name_prefix from codec_info_list[] */
1242+
adr_dev[index].name_prefix = devm_kasprintf(dev, GFP_KERNEL, "%s", name_prefix);
1243+
}
1244+
1245+
if (!adr_dev[index].name_prefix) {
1246+
dev_err(dev, "failed to allocate memory for name_prefix\n");
1247+
return NULL;
1248+
}
1249+
1250+
dev_dbg(dev, "adr[%d] 0x%llx link id %d name_prefix \"%s\" is found\n",
1251+
index, adr_dev[index].adr, sdw_device->bus->link_id, adr_dev[index].name_prefix);
1252+
1253+
link->num_adr++;
1254+
1255+
return adr_dev;
1256+
}
1257+
11201258
static struct snd_soc_acpi_mach *hda_sdw_machine_select(struct snd_sof_dev *sdev)
11211259
{
11221260
struct snd_sof_pdata *pdata = sdev->pdata;
11231261
const struct snd_soc_acpi_link_adr *link;
1262+
struct snd_soc_acpi_link_adr *links;
11241263
struct sdw_peripherals *peripherals;
11251264
struct snd_soc_acpi_mach *mach;
11261265
struct sof_intel_hda_dev *hdev;
1127-
u32 link_mask;
1266+
int link_index, link_num;
1267+
int amp_index = 1;
1268+
u32 link_mask = 0;
11281269
int i;
11291270

11301271
hdev = pdata->hw_pdata;
@@ -1201,7 +1342,38 @@ static struct snd_soc_acpi_mach *hda_sdw_machine_select(struct snd_sof_dev *sdev
12011342
peripherals->array[i]->id.part_id,
12021343
peripherals->array[i]->id.sdw_version);
12031344

1204-
return NULL;
1345+
if (!peripherals->num_peripherals)
1346+
return NULL;
1347+
1348+
/* Create default SDW mach */
1349+
mach = devm_kzalloc(sdev->dev, sizeof(*mach), GFP_KERNEL);
1350+
if (!mach)
1351+
return NULL;
1352+
1353+
/* Get link mask and link number */
1354+
for (i = 0; i < peripherals->num_peripherals; i++)
1355+
link_mask |= BIT(peripherals->array[i]->bus->link_id);
1356+
1357+
link_num = hweight32(link_mask);
1358+
links = devm_kcalloc(sdev->dev, link_num, sizeof(*links), GFP_KERNEL);
1359+
if (!links)
1360+
return NULL;
1361+
1362+
/* Generate snd_soc_acpi_link_adr struct for each peripheral reported by the ACPI table */
1363+
for (i = 0; i < peripherals->num_peripherals; i++) {
1364+
/* link_index = the number of used links below the current link */
1365+
link_index = hweight32(link_mask & (BIT(peripherals->array[i]->bus->link_id) - 1));
1366+
links[link_index].adr_d = find_acpi_adr_device(sdev->dev, peripherals->array[i],
1367+
&links[link_index], &amp_index);
1368+
}
1369+
1370+
mach->drv_name = "sof_sdw";
1371+
mach->sof_tplg_filename = "sof-sdw-generic.tplg";
1372+
mach->mach_params.links = links;
1373+
mach->mach_params.link_mask = link_mask;
1374+
mach->mach_params.platform = dev_name(sdev->dev);
1375+
mach->get_function_tplg_files = sof_sdw_get_tplg_files;
1376+
return mach;
12051377
}
12061378
#else
12071379
static struct snd_soc_acpi_mach *hda_sdw_machine_select(struct snd_sof_dev *sdev)
@@ -1529,6 +1701,7 @@ MODULE_IMPORT_NS("SND_SOC_SOF_XTENSA");
15291701
MODULE_IMPORT_NS("SND_INTEL_SOUNDWIRE_ACPI");
15301702
MODULE_IMPORT_NS("SOUNDWIRE_INTEL_INIT");
15311703
MODULE_IMPORT_NS("SOUNDWIRE_INTEL");
1704+
MODULE_IMPORT_NS("SND_SOC_SDW_UTILS");
15321705
MODULE_IMPORT_NS("SND_SOC_SOF_HDA_MLINK");
15331706
MODULE_IMPORT_NS("SND_SOC_SOF_INTEL_HDA_COMMON");
15341707
MODULE_IMPORT_NS("SND_SOC_ACPI_INTEL_MATCH");

0 commit comments

Comments
 (0)