Skip to content

Commit d07b49e

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 d00fd36 commit d07b49e

File tree

1 file changed

+183
-2
lines changed

1 file changed

+183
-2
lines changed

sound/soc/sof/intel/hda.c

Lines changed: 183 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,145 @@ 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+
int ep_index = 0;
1143+
int i, j;
1144+
1145+
link->mask = BIT(sdw_device->bus->link_id);
1146+
/* index is 0 based, we need allocate index + 1 for the array size */
1147+
if (!index)
1148+
adr_dev = devm_kzalloc(dev, sizeof(*adr_dev), GFP_KERNEL);
1149+
else
1150+
adr_dev = devm_krealloc(dev, (struct snd_soc_acpi_adr_device *)link->adr_d,
1151+
(index + 1) * sizeof(*adr_dev), GFP_KERNEL);
1152+
1153+
if (!adr_dev)
1154+
return NULL;
1155+
1156+
for (i = 0; i < asoc_sdw_get_codec_info_list_count(); i++) {
1157+
struct snd_soc_acpi_endpoint *endpoints;
1158+
int amp_group_id = 1;
1159+
1160+
if (sdw_device->id.part_id != codec_info_list[i].part_id)
1161+
continue;
1162+
1163+
endpoints = devm_kzalloc(dev, codec_info_list[i].dai_num *
1164+
sizeof(struct snd_soc_acpi_endpoint), GFP_KERNEL);
1165+
1166+
/* Get name_prefix from codec_info_list[] */
1167+
adr_dev[index].name_prefix = devm_kasprintf(dev, GFP_KERNEL, "%s",
1168+
codec_info_list[i].name_prefix);
1169+
for (j = 0; j < codec_info_list[i].dai_num; j++) {
1170+
/* Check if the endpoint is present by the SDCA DisCo table */
1171+
if (!is_endpoint_present(sdw_device, &codec_info_list[i],
1172+
codec_info_list[i].dais[j].dai_type))
1173+
continue;
1174+
1175+
endpoints[ep_index].num = ep_index;
1176+
if (codec_info_list[i].dais[j].dai_type == SOC_SDW_DAI_TYPE_AMP) {
1177+
/* Assume all amp are aggregated */
1178+
endpoints[ep_index].aggregated = 1;
1179+
endpoints[ep_index].group_id = amp_group_id;
1180+
endpoints[ep_index].group_position = *amp_index;
1181+
/* Set group id = 2 for feedback capture endpoint */
1182+
amp_group_id++;
1183+
} else {
1184+
endpoints[ep_index].aggregated = 0;
1185+
endpoints[ep_index].group_id = 0;
1186+
endpoints[ep_index].group_position = 0;
1187+
}
1188+
ep_index++;
1189+
}
1190+
adr_dev[index].endpoints = endpoints;
1191+
adr_dev[index].num_endpoints = ep_index;
1192+
break;
1193+
}
1194+
1195+
if (i == asoc_sdw_get_codec_info_list_count()) {
1196+
dev_err(dev, "part id %#x is not supported\n", sdw_device->id.part_id);
1197+
return NULL;
1198+
}
1199+
1200+
adr_dev[index].adr = ((u64)sdw_device->id.class_id & 0xFF) |
1201+
((u64)sdw_device->id.part_id & 0xFFFF) << 8 |
1202+
((u64)sdw_device->id.mfg_id & 0xFFFF) << 24 |
1203+
((u64)(sdw_device->id.unique_id & 0xF) << 40) |
1204+
((u64)(sdw_device->id.sdw_version & 0xF) << 44) |
1205+
((u64)(sdw_device->bus->link_id & 0xF) << 48);
1206+
1207+
/*
1208+
* The name_prefix comes from codec_info_list which has a name_prefix per codec.
1209+
* And we need to give a unique name_prefix for each amp and should be backwards
1210+
* compatible to the existing acpi match tables to not break existing UCMs.
1211+
* For the "AMP" name_prefix, we append the amp index to it. However, for the
1212+
* "Left" name_prefix, we convert the second amp name_prefix to "Right" and
1213+
* for the third and further amps, we set the name_prefix to "AMP<amp_index>".
1214+
*/
1215+
if (!strcmp(adr_dev[index].name_prefix, "AMP")) {
1216+
adr_dev[index].name_prefix = devm_kasprintf(dev, GFP_KERNEL, "%s%d",
1217+
adr_dev[index].name_prefix,
1218+
*amp_index);
1219+
if (!adr_dev[index].name_prefix)
1220+
return NULL;
1221+
(*amp_index)++;
1222+
}
1223+
1224+
if (!strcmp(adr_dev[index].name_prefix, "Left")) {
1225+
/* Convert the second amp name_prefix to Right */
1226+
if ((*amp_index) == 2) {
1227+
adr_dev[index].name_prefix = devm_kasprintf(dev, GFP_KERNEL,
1228+
"%s", "Right");
1229+
if (!adr_dev[index].name_prefix)
1230+
return NULL;
1231+
}
1232+
/* Set the name_fix to AMP<amp_index> if there are more than 2 amps */
1233+
if (*amp_index > 2) {
1234+
adr_dev[index].name_prefix = devm_kasprintf(dev, GFP_KERNEL, "%s%d",
1235+
"AMP", *amp_index);
1236+
if (!adr_dev[index].name_prefix)
1237+
return NULL;
1238+
}
1239+
(*amp_index)++;
1240+
}
1241+
1242+
dev_dbg(dev, "adr[%d] 0x%llx link id %d name_prefix \"%s\" is found\n",
1243+
index, adr_dev[index].adr, sdw_device->bus->link_id, adr_dev[index].name_prefix);
1244+
1245+
link->num_adr++;
1246+
1247+
return adr_dev;
1248+
}
1249+
11201250
static struct snd_soc_acpi_mach *hda_sdw_machine_select(struct snd_sof_dev *sdev)
11211251
{
11221252
struct snd_sof_pdata *pdata = sdev->pdata;
11231253
const struct snd_soc_acpi_link_adr *link;
1254+
struct snd_soc_acpi_link_adr *links;
11241255
struct sdw_peripherals *peripherals;
11251256
struct snd_soc_acpi_mach *mach;
11261257
struct sof_intel_hda_dev *hdev;
1127-
u32 link_mask;
1258+
int link_index, link_num;
1259+
int amp_index = 1;
1260+
u32 link_mask = 0;
11281261
int i;
11291262

11301263
hdev = pdata->hw_pdata;
@@ -1201,7 +1334,54 @@ static struct snd_soc_acpi_mach *hda_sdw_machine_select(struct snd_sof_dev *sdev
12011334
peripherals->array[i]->id.part_id,
12021335
peripherals->array[i]->id.sdw_version);
12031336

1204-
return NULL;
1337+
if (!peripherals->num_peripherals)
1338+
return NULL;
1339+
1340+
/* Create default SDW mach */
1341+
mach = devm_kzalloc(sdev->dev, sizeof(*mach), GFP_KERNEL);
1342+
if (!mach)
1343+
return NULL;
1344+
1345+
/* Get link mask and link number */
1346+
for (i = 0; i < peripherals->num_peripherals; i++)
1347+
link_mask |= BIT(peripherals->array[i]->bus->link_id);
1348+
1349+
link_num = hweight32(link_mask);
1350+
links = devm_kcalloc(sdev->dev, link_num, sizeof(*links), GFP_KERNEL);
1351+
if (!links)
1352+
return NULL;
1353+
1354+
link_index = 0;
1355+
/* Generate snd_soc_acpi_link_adr struct for each peripheral reported by the ACPI table */
1356+
for (i = 0; i < peripherals->num_peripherals; i++) {
1357+
links[link_index].adr_d = find_acpi_adr_device(sdev->dev, peripherals->array[i],
1358+
&links[link_index], &amp_index);
1359+
1360+
/*
1361+
* Only increase the link_index when the next periperal is on a different link.
1362+
* Assume that peripherals are grouped by link_id in the ACPI table.
1363+
*/
1364+
if (i < peripherals->num_peripherals - 1 &&
1365+
peripherals->array[i + 1]->bus->link_id != peripherals->array[i]->bus->link_id)
1366+
link_index++;
1367+
1368+
if (link_index > link_num) {
1369+
dev_err(sdev->dev,
1370+
"link_index %d exceeds the link number %d link_mask #%x\n",
1371+
link_index, link_num, link_mask);
1372+
return NULL;
1373+
}
1374+
}
1375+
1376+
mach->drv_name = "sof_sdw";
1377+
mach->sof_tplg_filename = "sof-sdw-generic.tplg";
1378+
mach->mach_params.links = links;
1379+
mach->mach_params.link_mask = link_mask;
1380+
mach->mach_params.platform = dev_name(sdev->dev);
1381+
mach->get_function_tplg_files = sof_sdw_get_tplg_files;
1382+
dev_info(sdev->dev, "Use default SDW machine driver %s topology: %s\n",
1383+
mach->drv_name, mach->sof_tplg_filename);
1384+
return mach;
12051385
}
12061386
#else
12071387
static struct snd_soc_acpi_mach *hda_sdw_machine_select(struct snd_sof_dev *sdev)
@@ -1529,6 +1709,7 @@ MODULE_IMPORT_NS("SND_SOC_SOF_XTENSA");
15291709
MODULE_IMPORT_NS("SND_INTEL_SOUNDWIRE_ACPI");
15301710
MODULE_IMPORT_NS("SOUNDWIRE_INTEL_INIT");
15311711
MODULE_IMPORT_NS("SOUNDWIRE_INTEL");
1712+
MODULE_IMPORT_NS("SND_SOC_SDW_UTILS");
15321713
MODULE_IMPORT_NS("SND_SOC_SOF_HDA_MLINK");
15331714
MODULE_IMPORT_NS("SND_SOC_SOF_INTEL_HDA_COMMON");
15341715
MODULE_IMPORT_NS("SND_SOC_ACPI_INTEL_MATCH");

0 commit comments

Comments
 (0)