Skip to contentSkip to conversation

Add SoundWire port constraints into SDCA stream

PATCH Superseded Charles Keepax 2 participants 6 messages Nov 9

Subsystems (3)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Charles Keepax

author maintainer
#0 lore raw

v3 cover Add SoundWire port constraints into SDCA stream

SDCA adds constraints onto the DAI based on the DisCo properties for the Input/Output Terminal. As the SoundWire port for a Terminal can be selected it is possible for the SoundWire port to support fewer options than the Terminal. These patches add the SoundWire port properties into the constraints for the DAI startup to cover these situations.

Changes since v2:
- Add check for ending up with a zero mask for channels
- Switch to using SNDRV_PCM_HW_PARAM_FORMAT, rather than SAMPLE_BITS

Changes since v1:
- Move constraint allocation to DAI probe/remove
- Use BIT macro where appropriate
- Correct GENMASK usage
- Add missing array indexing
- Correct channel masking
- Reorder the priority of SDCA properties
- Handle the case of the word length not being specified
- Add a static_assert to check the constraint array is large enough for the word length mask

Charles Keepax (3):
ASoC: SDCA: Move DAI constraint allocation
ASoC: SDCA: Factor out helper for creating mask of Cluster channels
ASoC: SDCA: Add port constraints into sdca_asoc_set_constraints()

 include/sound/sdca_asoc.h            |   9 +-
 sound/soc/sdca/sdca_asoc.c           | 242 +++++++++++++++++++++------
 sound/soc/sdca/sdca_class_function.c |   6 +-
 3 files changed, 202 insertions(+), 55 deletions(-)
Signature · 1 line
2.47.3
author maintainer
#1 lore raw

PATCH v3 1/3 ASoC: SDCA: Move DAI constraint allocation

It isn't necessary to allocate memory for constraints every time a DAI link is opened. It is faster to allocate them once when the DAI link is probed, update the SDCA code to do so.

---

No changes since v2.

 include/sound/sdca_asoc.h            |  5 +-
 sound/soc/sdca/sdca_asoc.c           | 75 +++++++++++++++++-----------
 sound/soc/sdca/sdca_class_function.c |  3 +-
 3 files changed, 52 insertions(+), 31 deletions(-)
include/sound/sdca_asoc.h +3 -2
index 599a72f335153..236a441ec9d89 100644
@@ -89,12 +89,13 @@ int sdca_asoc_populate_rate_format(struct device *dev,
 				struct sdca_entity *entity,
 				struct snd_soc_pcm_stream *stream);
 
+int sdca_asoc_alloc_constraints(struct snd_soc_dai *dai);
+int sdca_asoc_free_constraints(struct snd_soc_dai *dai);
 int sdca_asoc_set_constraints(struct device *dev, struct regmap *regmap,
 			      struct sdca_function_data *function,
 			      struct snd_pcm_substream *substream,
 			      struct snd_soc_dai *dai);
-void sdca_asoc_free_constraints(struct snd_pcm_substream *substream,
-				struct snd_soc_dai *dai);
+
 int sdca_asoc_get_port(struct device *dev, struct regmap *regmap,
 		       struct sdca_function_data *function,
 		       struct snd_soc_dai *dai);
sound/soc/sdca/sdca_asoc.c +47 -28

106 lines not on this page · raw message

sound/soc/sdca/sdca_class_function.c +2 -1
index 13b70f0f0dbf7..6406c9d0f6119 100644
@@ -184,8 +184,9 @@ static int class_function_sdw_set_stream(struct snd_soc_dai *dai, void *sdw_stre
 }
 
 static const struct snd_soc_dai_ops class_function_sdw_ops = {
+	.probe		= sdca_asoc_alloc_constraints,
+	.remove		= sdca_asoc_free_constraints,
 	.startup	= class_function_startup,
-	.shutdown	= sdca_asoc_free_constraints,
 	.set_stream	= class_function_sdw_set_stream,
 	.hw_params	= class_function_sdw_add_peripheral,
 	.hw_free	= class_function_sdw_remove_peripheral,
Signature · 1 line
2.47.3
author maintainer
#2 lore raw

PATCH v3 2/3 ASoC: SDCA: Factor out helper for creating mask of Cluster channels

In reply to Charles Keepax

Factor out the code that builds a mask of the allowed channels from the Cluster information. This is slightly cleaner and prepares for additional constraints to be added to sdca_asoc_set_constraints().

---

No changes since v2.

 sound/soc/sdca/sdca_asoc.c | 64 +++++++++++++++++++++++---------------
 1 file changed, 39 insertions(+), 25 deletions(-)
sound/soc/sdca/sdca_asoc.c +39 -25
index b8bde56298706..be1b4c0075796 100644
@@ -1476,6 +1476,37 @@ int sdca_asoc_free_constraints(struct snd_soc_dai *dai)
 }
 EXPORT_SYMBOL_NS(sdca_asoc_free_constraints, "SND_SOC_SDCA");
 
+static int get_cluster_mask(struct device *dev, struct sdca_function_data *function,
+			    struct sdca_entity *entity, unsigned int *ch_mask)
+{
+	struct sdca_control_range *range;
+	struct sdca_control *control;
+	int i;
+
+	control = sdca_selector_find_control(dev, entity, SDCA_CTL_IT_CLUSTERINDEX);
+	if (!control)
+		return -EINVAL;
+
+	range = sdca_control_find_range(dev, entity, control, SDCA_CLUSTER_NCOLS, 0);
+	if (!range)
+		return -EINVAL;
+
+	*ch_mask = 0;
+
+	for (i = 0; i < range->rows; i++) {
+		int clusterid = sdca_range(range, SDCA_CLUSTER_CLUSTERID, i);
+		struct sdca_cluster *cluster;
+
+		cluster = sdca_id_find_cluster(dev, function, clusterid);
+		if (!cluster)
+			return -ENODEV;
+
+		*ch_mask |= BIT(cluster->num_channels - 1);
+	}
+
+	return 0;
+}
+
 /**
  * sdca_asoc_set_constraints - constrain channels available on a DAI
  * @dev: Pointer to the device, used for error messages.
@@ -1499,42 +1530,25 @@ int sdca_asoc_set_constraints(struct device *dev, struct regmap *regmap,
 	};
 	struct sdca_asoc_constraints *constraints = snd_soc_dai_to_priv(dai);
 	struct sdca_entity *entity = &function->entities[dai->id];
-	struct sdca_control_range *range;
-	struct sdca_control *control;
-	unsigned int channel_mask = 0;
-	int i, ret;
+	unsigned int cluster_ch_mask;
+	int ret;
 
 	static_assert(ARRAY_SIZE(channel_list) == SDCA_MAX_CHANNEL_COUNT);
-	static_assert(sizeof(channel_mask) * BITS_PER_BYTE >= SDCA_MAX_CHANNEL_COUNT);
+	static_assert(sizeof(cluster_ch_mask) * BITS_PER_BYTE >= SDCA_MAX_CHANNEL_COUNT);
 
 	if (entity->type != SDCA_ENTITY_TYPE_IT)
 		return 0;
 
-	control = sdca_selector_find_control(dev, entity, SDCA_CTL_IT_CLUSTERINDEX);
-	if (!control)
-		return -EINVAL;
-
-	range = sdca_control_find_range(dev, entity, control, SDCA_CLUSTER_NCOLS, 0);
-	if (!range)
-		return -EINVAL;
-
-	for (i = 0; i < range->rows; i++) {
-		int clusterid = sdca_range(range, SDCA_CLUSTER_CLUSTERID, i);
-		struct sdca_cluster *cluster;
-
-		cluster = sdca_id_find_cluster(dev, function, clusterid);
-		if (!cluster)
-			return -ENODEV;
-
-		channel_mask |= (1 << (cluster->num_channels - 1));
-	}
+	ret = get_cluster_mask(dev, function, entity, &cluster_ch_mask);
+	if (ret)
+		return ret;
 
 	dev_dbg(dev, "%s: set channel constraint mask: %#x\n",
-		entity->label, channel_mask);
+		entity->label, cluster_ch_mask);
 
 	constraints->channels.count = ARRAY_SIZE(channel_list);
 	constraints->channels.list = channel_list;
-	constraints->channels.mask = channel_mask;
+	constraints->channels.mask = cluster_ch_mask;
 
 	ret = snd_pcm_hw_constraint_list(substream->runtime, 0,
 					 SNDRV_PCM_HW_PARAM_CHANNELS,
Signature · 1 line
2.47.3
author maintainer
#3 lore raw

PATCH v3 3/3 ASoC: SDCA: Add port constraints into sdca_asoc_set_constraints()

In reply to Charles Keepax

Currently the SDCA code constrains the params based on the DisCo for the SDCA Terminal used. However, it ignores the properties of the SoundWire port, which in some cases may further constrain the allowed params. Add additional constraints to apply these restrictions.

Some minor additional refactoring is required since port constraints should be applied for both Input and Output Terminals, whereas currently Cluster constraints are only handled for Input Terminals.

---

Changes since v2:
- Add check for ending up with a zero mask for channels, as this would allow all channel options
- Switch to using SNDRV_PCM_HW_PARAM_FORMAT, rather than SAMPLE_BITS, as SAMPLE_BITS equates to memory size not data size as was intended

 include/sound/sdca_asoc.h            |   4 +-
 sound/soc/sdca/sdca_asoc.c           | 137 ++++++++++++++++++++++++---
 sound/soc/sdca/sdca_class_function.c |   3 +-
 3 files changed, 128 insertions(+), 16 deletions(-)
include/sound/sdca_asoc.h +3 -1
index 236a441ec9d89..26740f69f2bee 100644
@@ -12,6 +12,7 @@
 
 struct device;
 struct regmap;
+struct sdw_slave;
 struct sdca_function_data;
 struct sdca_pde_delay;
 struct snd_ctl_elem_value;
@@ -91,7 +92,8 @@ int sdca_asoc_populate_rate_format(struct device *dev,
 
 int sdca_asoc_alloc_constraints(struct snd_soc_dai *dai);
 int sdca_asoc_free_constraints(struct snd_soc_dai *dai);
-int sdca_asoc_set_constraints(struct device *dev, struct regmap *regmap,
+int sdca_asoc_set_constraints(struct device *dev, struct sdw_slave *sdw,
+			      struct regmap *regmap,
 			      struct sdca_function_data *function,
 			      struct snd_pcm_substream *substream,
 			      struct snd_soc_dai *dai);
sound/soc/sdca/sdca_asoc.c +123 -14

191 lines not on this page · raw message

sound/soc/sdca/sdca_class_function.c +2 -1
index 6406c9d0f6119..abccc63640c68 100644
@@ -121,8 +121,9 @@ static int class_function_startup(struct snd_pcm_substream *substream,
 				  struct snd_soc_dai *dai)
 {
 	struct class_function_drv *drv = snd_soc_component_get_drvdata(dai->component);
+	struct sdw_slave *sdw = dev_to_sdw_dev(drv->dev->parent);
 
-	return sdca_asoc_set_constraints(drv->dev, drv->regmap, drv->function,
+	return sdca_asoc_set_constraints(drv->dev, sdw, drv->regmap, drv->function,
 					 substream, dai);
 }
 
Signature · 1 line
2.47.3
#4 lore raw

Re: [PATCH v3 3/3] ASoC: SDCA: Add port constraints into sdca_asoc_set_constraints()

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] ASoC: SDCA: zero word_mask in get_port_masks() when wordlengths are unset - [Medium] ASoC: SDCA…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] ASoC: SDCA: zero word_mask in get_port_masks() when wordlengths are unset
- [Medium] ASoC: SDCA: 1-channel port_ch_mask in get_port_masks() when min_ch/max_ch are 0
--

commit 8d09b518756cef71f9c60a48b6568b96a8c9556b
Author: Charles Keepax <[email protected]>

ASoC: SDCA: Add port constraints into sdca_asoc_set_constraints()

This commit updates sdca_asoc_set_constraints() to apply channel count and sample format constraints derived from the SoundWire data port properties.

Quoted 4 lines
sound/soc/sdca/sdca_asoc.c +0 -0
index be1b4c0075796..ccbb2c4b10267 100644

[ ... ]

Quoted 53 lines
sound/soc/sdca/sdca_asoc.c +49 -0
@@ -1507,9 +1510,86 @@ static int get_cluster_mask(struct device *dev, struct sdca_function_data *funct
 	return 0;
 }
 
+static u64 get_word_mask(unsigned int width)
+{
+	switch (width) {
+	case 8:
+		return SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8;
+	case 16:
+		return SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
+		       SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE;
+	case 18:
+		return SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_U18_3LE |
+		       SNDRV_PCM_FMTBIT_S18_3BE | SNDRV_PCM_FMTBIT_U18_3BE;
+	case 20:
+		return SNDRV_PCM_FMTBIT_S20_LE | SNDRV_PCM_FMTBIT_U20_LE |
+		       SNDRV_PCM_FMTBIT_S20_BE | SNDRV_PCM_FMTBIT_U20_BE |
+		       SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_U20_3LE |
+		       SNDRV_PCM_FMTBIT_S20_3BE | SNDRV_PCM_FMTBIT_U20_3BE;
+	case 24:
+		return SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE |
+		       SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE |
+		       SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3LE |
+		       SNDRV_PCM_FMTBIT_S24_3BE | SNDRV_PCM_FMTBIT_U24_3BE;
+	case 32:
+		return SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE |
+		       SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE;
+	default:
+		return 0;
+	}
+}
+
+static int get_port_masks(struct sdw_slave *sdw, unsigned int port,
+			  struct snd_pcm_substream *substream,
+			  u64 *word_mask, unsigned int *ch_mask)
+{
+	struct sdw_dpn_prop *prop;
+	unsigned int nprop;
+	int i, j;
+
+	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
+		prop = sdw->prop.sink_dpn_prop;
+		nprop = hweight32(sdw->prop.sink_ports);
+	} else {
+		prop = sdw->prop.src_dpn_prop;
+		nprop = hweight32(sdw->prop.source_ports);
+	}
+
+	*word_mask = 0;
+	*ch_mask = 0;
+
+	for (i = 0; i < nprop; i++) {

sound/soc/sdca/sdca_asoc.c static int get_cluster_mask(struct device *dev, struct sdca_function_data *funct · line 1571

Quoted 10 lines
sound/soc/sdca/sdca_asoc.c +10 -0
+		if (prop[i].num != port)
+			continue;
+
+		if (prop[i].num_words) {
+			for (j = 0; j < prop[i].num_words; j++)
+				*word_mask |= get_word_mask(prop[i].words[j]);
+		} else {
+			for (j = prop[i].min_word; j <= prop[i].max_word; j++)
+				*word_mask |= get_word_mask(j);
+		}

[Severity: High]
Does get_port_masks() leave *word_mask as 0 when a port does not specify wordlength properties?

The MIPI DisCo wordlength properties are optional, and drivers such as wcd9378_sdca_read_prop() leave num_words, min_word, and max_word as 0. When all three are 0, the else loop runs once with j = 0, get_word_mask(0) returns 0, and sdca_asoc_set_constraints() fails PCM startup with -EINVAL because constraints->width is 0.

sound/soc/sdca/sdca_asoc.c static int get_cluster_mask(struct device *dev, struct sdca_function_data *funct · line 1581

Quoted 10 lines
sound/soc/sdca/sdca_asoc.c +10 -0
+
+		/* Only handle the number of channels for constraints. */
+		if (prop[i].num_ch_combinations) {
+			for (j = 0; j < prop[i].num_ch_combinations; j++)
+				*ch_mask |= BIT(hweight32(prop[i].ch_combinations[j]) - 1);
+		} else if (prop[i].num_channels) {
+			*ch_mask = GENMASK(prop[i].num_channels - 1, 0);
+		} else {
+			*ch_mask = GENMASK(prop[i].max_ch - prop[i].min_ch, 0);
+		}

[Severity: Medium]
As reported by Mark Brown on v2 [1], does this restrict *ch_mask to a single channel (GENMASK(0, 0) == 0x1) when the optional channel properties (num_ch_combinations, num_channels, max_ch, and min_ch) are all 0? That constrains the DAI to 1 channel instead of leaving the port channel count unconstrained, and causes sdca_asoc_set_constraints() to fail with -EINVAL if the terminal cluster only supports multi-channel configurations.

[1] https://lore.kernel.org/r/179413707633.3630016.13654136728294242171.b4-review@b4

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
author maintainer
#5 lore raw

Re: [PATCH v3 3/3] ASoC: SDCA: Add port constraints into sdca_asoc_set_constraints()

Quoted 23 lines · On Mon, Nov 09, 2026 at 04:16:52PM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] ASoC: SDCA: zero word_mask in get_port_masks() when wordlengths are unset
- [Medium] ASoC: SDCA: 1-channel port_ch_mask in get_port_masks() when min_ch/max_ch are 0
--

sound/soc/sdca/sdca_asoc.c static int get_cluster_mask(struct device *dev, struct sdca_function_data *funct · line 1571

Quoted 11 lines
sound/soc/sdca/sdca_asoc.c +11 -0
+	for (i = 0; i < nprop; i++) {
+		if (prop[i].num != port)
+			continue;
+
+		if (prop[i].num_words) {
+			for (j = 0; j < prop[i].num_words; j++)
+				*word_mask |= get_word_mask(prop[i].words[j]);
+		} else {
+			for (j = prop[i].min_word; j <= prop[i].max_word; j++)
+				*word_mask |= get_word_mask(j);
+		}

[Severity: High]
Does get_port_masks() leave *word_mask as 0 when a port does not specify wordlength properties?

Quoted 5 lines

The MIPI DisCo wordlength properties are optional, and drivers such as wcd9378_sdca_read_prop() leave num_words, min_word, and max_word as 0. When all three are 0, the else loop runs once with j = 0, get_word_mask(0) returns 0, and sdca_asoc_set_constraints() fails PCM startup with -EINVAL because constraints->width is 0.

Argh... sorry I had this in but then it got lost when I moved over to FORMAT instead of sample bits.

Quoted 19 lines

sound/soc/sdca/sdca_asoc.c static int get_cluster_mask(struct device *dev, struct sdca_function_data *funct · line 1581

Quoted 10 lines
sound/soc/sdca/sdca_asoc.c +10 -0
+
+		/* Only handle the number of channels for constraints. */
+		if (prop[i].num_ch_combinations) {
+			for (j = 0; j < prop[i].num_ch_combinations; j++)
+				*ch_mask |= BIT(hweight32(prop[i].ch_combinations[j]) - 1);
+		} else if (prop[i].num_channels) {
+			*ch_mask = GENMASK(prop[i].num_channels - 1, 0);
+		} else {
+			*ch_mask = GENMASK(prop[i].max_ch - prop[i].min_ch, 0);
+		}

[Severity: Medium]
As reported by Mark Brown on v2 [1], does this restrict *ch_mask to a single channel (GENMASK(0, 0) == 0x1) when the optional channel properties (num_ch_combinations, num_channels, max_ch, and min_ch) are all 0? That constrains the DAI to 1 channel instead of leaving the port channel count unconstrained, and causes sdca_asoc_set_constraints() to fail with -EINVAL if the terminal cluster only supports multi-channel configurations.

Quoted 1 line

[1] https://lore.kernel.org/r/179413707633.3630016.13654136728294242171.b4-review@b4

This one can't be fixed locally the problem is min_ch and max_ch are channel indexes. So both set to zero is a totally valid setup. I have a patch for soundwire that applies the defaults there was probably going to send that up tomorrow. But could pull it in to this chain if we felt strongly on it.

Thanks,
Charles