* [PATCH] soundwire: stream: add multi-direction stream lifecycle API
2026-11-30 22:40 [RFC PATCH 0/2] soundwire: multi-direction stream API Srinivas Kandagatla
@ 2026-11-30 22:40 ` Srinivas Kandagatla
2026-11-30 23:24 ` Randy Dunlap
2026-11-30 22:40 ` [RFC PATCH 2/2] ASoC: qcom: sdw: use multi-direction stream lifecycle for prepare/hw_free Srinivas Kandagatla
2026-12-01 16:44 ` [RFC PATCH 0/2] soundwire: multi-direction stream API Pierre-Louis Bossart
2 siblings, 1 reply; 5+ messages in thread
From: Srinivas Kandagatla @ 2026-11-30 22:40 UTC (permalink / raw)
To: Vinod Koul, Bard Liao
Cc: Pierre-Louis Bossart, Sanyog Kale, Mark Brown, Jonathan Corbet,
Bjorn Andersson, Konrad Dybcio, Srinivas Kandagatla, alsa-devel,
linux-sound, linux-arm-msm, linux-doc, linux-kernel
Add sdw_multi_direction_stream, a lightweight group that aggregates one
or more sdw_stream_runtimes that share a lifecycle on the bus. New
entry points:
sdw_multi_direction_stream_alloc / _release / _add
sdw_prepare_multi_direction_stream
sdw_enable_multi_direction_stream
sdw_disable_multi_direction_stream
sdw_deprepare_multi_direction_stream
A multi-direction stream group with a single constituent behaves
exactly like the direct sdw_prepare_stream() path, so callers can use
this API unconditionally without having to branch on how many
directions a dai_link uses.
Groups with more than one constituent currently iterate the per-stream
primitives with all-or-nothing rollback on prepare/enable errors.
This already gives callers a uniform API and well-defined failure
semantics but still triggers one bank switch per constituent stream.
The follow-up step is to split _sdw_prepare_stream() and
_sdw_enable_stream() into "params" and "commit" halves so the group
can program every constituent stream's bus parameters first and then
trigger a single do_bank_switch() across them. That is left out of
this change to keep the diff reviewable and because it touches the
SoundWire core state machine; it can land on top without changing
the public API introduced here.
Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@oss.qualcomm.com>
---
Documentation/driver-api/soundwire/stream.rst | 43 ++++
drivers/soundwire/stream.c | 184 ++++++++++++++++++
include/linux/soundwire/sdw.h | 37 ++++
3 files changed, 264 insertions(+)
diff --git a/Documentation/driver-api/soundwire/stream.rst b/Documentation/driver-api/soundwire/stream.rst
index 58b05f2d0b0e..c8f593f2ee4f 100644
--- a/Documentation/driver-api/soundwire/stream.rst
+++ b/Documentation/driver-api/soundwire/stream.rst
@@ -518,6 +518,49 @@ typically called during a dailink .shutdown() callback, which clears
the stream pointer for all DAIS connected to a stream and releases the
memory allocated for the stream.
+Multi-direction stream lifecycle
+================================
+
+Some dailinks run more than one sdw_stream_runtime that must come up
+and go down together on the bus: for example a WSA playback link whose
+V/I-sense capture ports have to track playback prepare/enable, or any
+multi-direction codec-codec link where the FE opens a single substream
+but the BE drives one runtime per direction.
+
+The SoundWire core exposes a lightweight group, sdw_multi_direction_stream,
+that aggregates one or more sdw_stream_runtimes and lets the caller
+drive the shared lifecycle through a single API:
+
+.. code-block:: c
+
+ struct sdw_multi_direction_stream *sdw_multi_direction_stream_alloc(const char *name);
+ void sdw_multi_direction_stream_release(struct sdw_multi_direction_stream *md);
+ int sdw_multi_direction_stream_add(struct sdw_multi_direction_stream *md,
+ struct sdw_stream_runtime *stream);
+
+ int sdw_prepare_multi_direction_stream(struct sdw_multi_direction_stream *md);
+ int sdw_enable_multi_direction_stream(struct sdw_multi_direction_stream *md);
+ int sdw_disable_multi_direction_stream(struct sdw_multi_direction_stream *md);
+ int sdw_deprepare_multi_direction_stream(struct sdw_multi_direction_stream *md);
+
+A group with a single constituent is handled by calling the matching
+sdw_prepare_stream() / sdw_enable_stream() / ... directly, so callers
+sdw_prepare_stream() / sdw_enable_stream() / ... directly, so callers
+that may or may not need more than one direction can use this API
+unconditionally without branching on the count.
+
+With more than one constituent the primitives iterate the per-stream
+calls in insertion order, with all-or-nothing rollback on prepare and
+enable errors: on failure at step N the API tears down streams 0..N-1
+in reverse order before returning the error, so the bus is left in the
+same state it was in before the call.
+
+Current limitation: each constituent still triggers its own bank
+switch, so a two-direction group costs two switches on prepare and two
+on enable. The public API is final; a later change can split the
+per-stream primitives into "params" and "commit" halves so the group
+can program every constituent's bus parameters first and then issue a
+single do_bank_switch(), without breaking callers.
+
Not Supported
=============
diff --git a/drivers/soundwire/stream.c b/drivers/soundwire/stream.c
index eccb279ea662..84d349c6663f 100644
--- a/drivers/soundwire/stream.c
+++ b/drivers/soundwire/stream.c
@@ -1884,6 +1884,208 @@ int sdw_deprepare_stream(struct sdw_stream_runtime *stream)
}
EXPORT_SYMBOL(sdw_deprepare_stream);
+/*
+ * SoundWire multi-direction
+ *
+ * The multi-direction acts as a lifecycle coordinator across multiple constituent
+ * streams. Today the lifecycle primitives (prepare/enable/disable/
+ * deprepare) are implemented by iterating sdw_*_stream() on each
+ * constituent stream; this already gives callers a single API surface
+ * and a well-defined atomic rollback on error, but still performs one
+ * bus parameter recompute + bank switch per stream.
+ *
+ * TODO: coalesce the per-stream bus programming + bank switch into one
+ * pass across all constituent streams. This requires splitting
+ * _sdw_prepare_stream() / _sdw_enable_stream() into "params" and
+ * "commit" halves so that params for every constituent stream can
+ * be programmed before a single do_bank_switch() is triggered.
+ * Left out of this RFC to keep the diff reviewable.
+ */
+
+/**
+ * sdw_multi_direction_stream_init() - initialise a caller-owned handle
+ * @md: multi-direction handle to initialise
+ * @name: identifier used in diagnostics
+ *
+ * For callers that embed the handle in their own state (e.g. dai_link
+ * drvdata) to avoid allocation on PCM hot paths. The paired teardown
+ * is a no-op; callers must not invoke sdw_multi_direction_stream_release()
+ * on a handle that was set up with _init().
+ */
+void sdw_multi_direction_stream_init(struct sdw_multi_direction_stream *md,
+ const char *name)
+{
+ memset(md, 0, sizeof(*md));
+ md->name = name;
+}
+EXPORT_SYMBOL(sdw_multi_direction_stream_init);
+
+/**
+ * sdw_multi_direction_stream_alloc() - allocate a SoundWire multi-direction
+ * @name: identifier used in diagnostics
+ *
+ * Return: a new multi-direction handle, or NULL on failure.
+ */
+struct sdw_multi_direction_stream *sdw_multi_direction_stream_alloc(const char *name)
+{
+ struct sdw_multi_direction_stream *md;
+
+ md = kzalloc(sizeof(*md), GFP_KERNEL);
+ if (!md)
+ return NULL;
+ md->name = name;
+ return md;
+}
+EXPORT_SYMBOL(sdw_multi_direction_stream_alloc);
+
+/**
+ * sdw_multi_direction_stream_release() - release a SoundWire multi-direction
+ * @md: multi-direction handle
+ *
+ * The constituent sdw_stream_runtimes are not freed; the caller still
+ * owns them and must release them with sdw_release_stream().
+ */
+void sdw_multi_direction_stream_release(struct sdw_multi_direction_stream *md)
+{
+ if (!md)
+ return;
+ kfree(md);
+}
+EXPORT_SYMBOL(sdw_multi_direction_stream_release);
+
+/**
+ * sdw_multi_direction_stream_add() - add a stream to a multi-direction
+ * @md: multi-direction handle
+ * @stream: stream to add
+ *
+ * Return: 0 on success, -ENOSPC if the multi-direction is full.
+ */
+int sdw_multi_direction_stream_add(struct sdw_multi_direction_stream *md,
+ struct sdw_stream_runtime *stream)
+{
+ if (!md || !stream)
+ return -EINVAL;
+ if (md->num_streams >= SDW_MULTI_DIRECTION_STREAM_MAX)
+ return -ENOSPC;
+ md->streams[md->num_streams++] = stream;
+ return 0;
+}
+EXPORT_SYMBOL(sdw_multi_direction_stream_add);
+
+static int multi_direction_rollback_prepare(struct sdw_multi_direction_stream *md, int upto)
+{
+ int i;
+
+ for (i = upto - 1; i >= 0; i--)
+ sdw_deprepare_stream(md->streams[i]);
+ return 0;
+}
+
+static int multi_direction_rollback_enable(struct sdw_multi_direction_stream *md, int upto)
+{
+ int i;
+
+ for (i = upto - 1; i >= 0; i--)
+ sdw_disable_stream(md->streams[i]);
+ return 0;
+}
+
+/**
+ * sdw_prepare_multi_direction_stream() - prepare every constituent stream
+ * @md: multi-direction handle
+ *
+ * On error, any streams that were prepared successfully are rolled back
+ * (deprepared) so the caller always sees an all-or-nothing outcome.
+ */
+int sdw_prepare_multi_direction_stream(struct sdw_multi_direction_stream *md)
+{
+ int ret, i;
+
+ if (!md || !md->num_streams)
+ return -EINVAL;
+
+ for (i = 0; i < md->num_streams; i++) {
+ ret = sdw_prepare_stream(md->streams[i]);
+ if (ret) {
+ multi_direction_rollback_prepare(md, i);
+ return ret;
+ }
+ }
+ return 0;
+}
+EXPORT_SYMBOL(sdw_prepare_multi_direction_stream);
+
+/**
+ * sdw_enable_multi_direction_stream() - enable every constituent stream
+ * @md: multi-direction handle
+ *
+ * On error, any streams that were enabled successfully are rolled back
+ * (disabled) so the caller always sees an all-or-nothing outcome.
+ */
+int sdw_enable_multi_direction_stream(struct sdw_multi_direction_stream *md)
+{
+ int ret, i;
+
+ if (!md || !md->num_streams)
+ return -EINVAL;
+
+ for (i = 0; i < md->num_streams; i++) {
+ ret = sdw_enable_stream(md->streams[i]);
+ if (ret) {
+ multi_direction_rollback_enable(md, i);
+ return ret;
+ }
+ }
+ return 0;
+}
+EXPORT_SYMBOL(sdw_enable_multi_direction_stream);
+
+/**
+ * sdw_disable_multi_direction_stream() - disable every constituent stream
+ * @md: multi-direction handle
+ *
+ * Disables are best-effort; the first error is returned but all streams
+ * are walked so the bus ends up quiesced.
+ */
+int sdw_disable_multi_direction_stream(struct sdw_multi_direction_stream *md)
+{
+ int ret, first_err = 0, i;
+
+ if (!md || !md->num_streams)
+ return -EINVAL;
+
+ for (i = md->num_streams - 1; i >= 0; i--) {
+ ret = sdw_disable_stream(md->streams[i]);
+ if (ret && !first_err)
+ first_err = ret;
+ }
+ return first_err;
+}
+EXPORT_SYMBOL(sdw_disable_multi_direction_stream);
+
+/**
+ * sdw_deprepare_multi_direction_stream() - deprepare every constituent stream
+ * @md: multi-direction handle
+ *
+ * Deprepares are best-effort; the first error is returned but all
+ * streams are walked.
+ */
+int sdw_deprepare_multi_direction_stream(struct sdw_multi_direction_stream *md)
+{
+ int ret, first_err = 0, i;
+
+ if (!md || !md->num_streams)
+ return -EINVAL;
+
+ for (i = md->num_streams - 1; i >= 0; i--) {
+ ret = sdw_deprepare_stream(md->streams[i]);
+ if (ret && !first_err)
+ first_err = ret;
+ }
+ return first_err;
+}
+EXPORT_SYMBOL(sdw_deprepare_multi_direction_stream);
+
+ * - A playback-only ASoC FE whose BE link carries both the audio stream
+ * (RX direction on the wire) and a companion V/I-sense capture
+ * stream (TX direction) that the DSP consumes; the two directions
+ * share the same clock and must be enabled atomically.
+ * - Companion-amp device-to-device (D2D) streams that mirror a host
+ * stream.
+ * - Any set of streams that need one bank switch rather than N.
+ *
+ * Each stream still lives in its own sdw_stream_runtime; the multi-direction is a
+ * lightweight container that coordinates the lifecycle.
+ */
+#define SDW_MULTI_DIRECTION_STREAM_MAX 4
+
+struct sdw_multi_direction_stream {
+ const char *name;
+ struct sdw_stream_runtime *streams[SDW_MULTI_DIRECTION_STREAM_MAX];
+ unsigned int num_streams;
+};
+
+struct sdw_multi_direction_stream *sdw_multi_direction_stream_alloc(const char *name);
+void sdw_multi_direction_stream_release(struct sdw_multi_direction_stream *md);
+int sdw_multi_direction_stream_add(struct sdw_multi_direction_stream *md,
+ struct sdw_stream_runtime *stream);
+
+int sdw_prepare_multi_direction_stream(struct sdw_multi_direction_stream *md);
+int sdw_enable_multi_direction_stream(struct sdw_multi_direction_stream *md);
+int sdw_disable_multi_direction_stream(struct sdw_multi_direction_stream *md);
+int sdw_deprepare_multi_direction_stream(struct sdw_multi_direction_stream *md);
+
int sdw_stream_add_master(struct sdw_bus *bus,
struct sdw_stream_config *stream_config,
const struct sdw_port_config *port_config,
base-commit: 253f86660c1f882fbf554db5eaa34bc5d66dc7e8
^ permalink raw reply related [flat|nested] 5+ messages in thread* Re: [PATCH] soundwire: stream: add multi-direction stream lifecycle API
2026-11-30 22:40 ` [PATCH] soundwire: stream: add multi-direction stream lifecycle API Srinivas Kandagatla
@ 2026-11-30 23:24 ` Randy Dunlap
0 siblings, 0 replies; 5+ messages in thread
From: Randy Dunlap @ 2026-11-30 23:24 UTC (permalink / raw)
To: Srinivas Kandagatla, Vinod Koul, Bard Liao
Cc: Pierre-Louis Bossart, Sanyog Kale, Mark Brown, Jonathan Corbet,
Bjorn Andersson, Konrad Dybcio, alsa-devel, linux-sound,
linux-arm-msm, linux-doc, linux-kernel
On 11/30/26 2:40 PM, Srinivas Kandagatla wrote:
> Add sdw_multi_direction_stream, a lightweight group that aggregates one
> or more sdw_stream_runtimes that share a lifecycle on the bus. New
> entry points:
>
> sdw_multi_direction_stream_alloc / _release / _add
> sdw_prepare_multi_direction_stream
> sdw_enable_multi_direction_stream
> sdw_disable_multi_direction_stream
> sdw_deprepare_multi_direction_stream
>
> A multi-direction stream group with a single constituent behaves
> exactly like the direct sdw_prepare_stream() path, so callers can use
> this API unconditionally without having to branch on how many
> directions a dai_link uses.
>
> Groups with more than one constituent currently iterate the per-stream
> primitives with all-or-nothing rollback on prepare/enable errors.
> This already gives callers a uniform API and well-defined failure
> semantics but still triggers one bank switch per constituent stream.
>
> The follow-up step is to split _sdw_prepare_stream() and
> _sdw_enable_stream() into "params" and "commit" halves so the group
> can program every constituent stream's bus parameters first and then
> trigger a single do_bank_switch() across them. That is left out of
> this change to keep the diff reviewable and because it touches the
> SoundWire core state machine; it can land on top without changing
> the public API introduced here.
>
> Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@oss.qualcomm.com>
> ---
> Documentation/driver-api/soundwire/stream.rst | 43 ++++
> drivers/soundwire/stream.c | 184 ++++++++++++++++++
> include/linux/soundwire/sdw.h | 37 ++++
> 3 files changed, 264 insertions(+)
> base-commit: 253f86660c1f882fbf554db5eaa34bc5d66dc7e8
Unless my linux-next tree is mucked up, this base-commit is
linux-next-20261130 but the patch does not apply cleanly to it
for a few reasons:
a. The last 3 lines of the context diff are incorrect.
b. The number of lines in the Documentation block is incorrect,
causing a malformed patch [@@ -518,6 +518,49 @@]: 49 should be 50.
c. The number of lines in the source file diff is incorrect,
causing a malformed patch [@@ -1884,6 +1884,208 @@]:
208 should be 237.
Maybe just regenerate the patch?
--
~Randy
^ permalink raw reply [flat|nested] 5+ messages in thread
* [RFC PATCH 2/2] ASoC: qcom: sdw: use multi-direction stream lifecycle for prepare/hw_free
2026-11-30 22:40 [RFC PATCH 0/2] soundwire: multi-direction stream API Srinivas Kandagatla
2026-11-30 22:40 ` [PATCH] soundwire: stream: add multi-direction stream lifecycle API Srinivas Kandagatla
@ 2026-11-30 22:40 ` Srinivas Kandagatla
2026-12-01 16:44 ` [RFC PATCH 0/2] soundwire: multi-direction stream API Pierre-Louis Bossart
2 siblings, 0 replies; 5+ messages in thread
From: Srinivas Kandagatla @ 2026-11-30 22:40 UTC (permalink / raw)
To: Vinod Koul, Bard Liao
Cc: Pierre-Louis Bossart, Sanyog Kale, Mark Brown, Jonathan Corbet,
Bjorn Andersson, Konrad Dybcio, Srinivas Kandagatla, alsa-devel,
linux-sound, linux-arm-msm, linux-doc, linux-kernel
Replace the open-coded per-direction handling with the multi-direction
stream lifecycle API. Single-direction and multi_direction_codecs
dai_links go through the same path, and the hand-rolled rollback is
gone.
Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@oss.qualcomm.com>
---
sound/soc/qcom/sdw.c | 113 ++++++++++++++++++++++++-------------------
1 file changed, 62 insertions(+), 51 deletions(-)
diff --git a/sound/soc/qcom/sdw.c b/sound/soc/qcom/sdw.c
index 82f1c47a788c..1ddb6c3fbc0a 100644
--- a/sound/soc/qcom/sdw.c
+++ b/sound/soc/qcom/sdw.c
@@ -203,26 +203,50 @@ int qcom_snd_sdw_startup(struct snd_pcm_substream *substream)
}
EXPORT_SYMBOL_GPL(qcom_snd_sdw_startup);
-/* Prepare+enable the SDW stream for one direction. */
-static int qcom_snd_sdw_prepare_dir(struct snd_soc_pcm_runtime *rtd, int dir)
+/*
+ * Build a multi-stream covering every direction runtime that this
+ * dai_link uses. For a conventional single-direction link the multi-stream
+ * holds one stream; for a multi_direction_codecs link it holds both
+ * directions. The lifecycle (prepare/enable/disable/deprepare) is
+ * driven through the multi-stream API in all cases.
+ */
+static struct sdw_multi_direction_stream *
+qcom_snd_sdw_build_multi_direction_stream(struct snd_soc_pcm_runtime *rtd, int dir)
{
- struct sdw_stream_runtime *sruntime;
+ struct sdw_stream_runtime *sr;
+ struct sdw_multi_direction_stream *md;
int ret;
- sruntime = sdw_find_dir_stream(rtd, dir);
- if (!sruntime)
- return 0;
+ md = sdw_multi_direction_stream_alloc(rtd->dai_link->name);
+ if (!md)
+ return ERR_PTR(-ENOMEM);
- ret = sdw_prepare_stream(sruntime);
- if (ret)
- return ret;
+ sr = sdw_find_dir_stream(rtd, dir);
+ if (sr) {
+ ret = sdw_multi_direction_stream_add(md, sr);
+ if (ret)
+ goto err;
+ }
- ret = sdw_enable_stream(sruntime);
- if (ret) {
- sdw_deprepare_stream(sruntime);
- return ret;
+ if (rtd->dai_link->multi_direction_codecs) {
+ sr = sdw_find_dir_stream(rtd, dir ^ 1);
+ /* Skip if no slave registered at hw_params (e.g. VI off) */
+ if (sr && sr->state != SDW_STREAM_ALLOCATED) {
+ ret = sdw_multi_direction_stream_add(md, sr);
+ if (ret)
+ goto err;
+ }
}
- return 0;
+
+ if (!md->num_streams) {
+ sdw_multi_direction_stream_release(md);
+ return NULL;
+ }
+
+ return md;
+err:
+ sdw_multi_direction_stream_release(md);
+ return ERR_PTR(ret);
}
int qcom_snd_sdw_prepare(struct snd_pcm_substream *substream,
@@ -230,6 +254,7 @@ int qcom_snd_sdw_prepare(struct snd_pcm_substream *substream,
{
struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
+ struct sdw_multi_direction_stream *md;
int ret;
if (!qcom_snd_is_sdw_dai(cpu_dai->id))
@@ -245,32 +270,24 @@ int qcom_snd_sdw_prepare(struct snd_pcm_substream *substream,
* accumulated resulting in Click/Pop Noise
* PA enable/mute are handled as part of codec DAPM and digital mute.
*/
- ret = qcom_snd_sdw_prepare_dir(rtd, substream->stream);
- if (ret)
- return ret;
-
- if (rtd->dai_link->multi_direction_codecs) {
- struct sdw_stream_runtime *sr_opp =
- sdw_find_dir_stream(rtd, substream->stream ^ 1);
+ md = qcom_snd_sdw_build_multi_direction_stream(rtd, substream->stream);
+ if (IS_ERR(md))
+ return PTR_ERR(md);
+ if (!md)
+ return 0;
- /* Skip if no slave registered at hw_params (e.g. VI off) */
- if (sr_opp && sr_opp->state != SDW_STREAM_ALLOCATED) {
- ret = qcom_snd_sdw_prepare_dir(rtd,
- substream->stream ^ 1);
- if (ret) {
- struct sdw_stream_runtime *sr =
- sdw_find_dir_stream(rtd, substream->stream);
- if (sr) {
- sdw_disable_stream(sr);
- sdw_deprepare_stream(sr);
- }
- return ret;
- }
- }
- }
+ ret = sdw_prepare_multi_direction_stream(md);
+ if (ret)
+ goto out;
- *stream_prepared = true;
- return 0;
+ ret = sdw_enable_multi_direction_stream(md);
+ if (ret)
+ sdw_deprepare_multi_direction_stream(md);
+ else
+ *stream_prepared = true;
+out:
+ sdw_multi_direction_stream_release(md);
+ return ret;
}
EXPORT_SYMBOL_GPL(qcom_snd_sdw_prepare);
@@ -310,7 +327,7 @@ int qcom_snd_sdw_hw_free(struct snd_pcm_substream *substream, bool *stream_prepa
{
struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
- struct sdw_stream_runtime *sr;
+ struct sdw_multi_direction_stream *md;
if (!qcom_snd_is_sdw_dai(cpu_dai->id))
return 0;
@@ -318,19 +335,13 @@ int qcom_snd_sdw_hw_free(struct snd_pcm_substream *substream, bool *stream_prepa
if (!*stream_prepared)
return 0;
- sr = sdw_find_dir_stream(rtd, substream->stream);
- if (sr) {
- sdw_disable_stream(sr);
- sdw_deprepare_stream(sr);
- }
+ md = qcom_snd_sdw_build_multi_direction_stream(rtd, substream->stream);
+ if (IS_ERR_OR_NULL(md))
+ return 0;
- if (rtd->dai_link->multi_direction_codecs) {
- sr = sdw_find_dir_stream(rtd, substream->stream ^ 1);
- if (sr && sr->state != SDW_STREAM_ALLOCATED) {
- sdw_disable_stream(sr);
- sdw_deprepare_stream(sr);
- }
- }
+ sdw_disable_multi_direction_stream(md);
+ sdw_deprepare_multi_direction_stream(md);
+ sdw_multi_direction_stream_release(md);
*stream_prepared = false;
return 0;
--
2.53.0
^ permalink raw reply related [flat|nested] 5+ messages in thread* Re: [RFC PATCH 0/2] soundwire: multi-direction stream API
2026-11-30 22:40 [RFC PATCH 0/2] soundwire: multi-direction stream API Srinivas Kandagatla
2026-11-30 22:40 ` [PATCH] soundwire: stream: add multi-direction stream lifecycle API Srinivas Kandagatla
2026-11-30 22:40 ` [RFC PATCH 2/2] ASoC: qcom: sdw: use multi-direction stream lifecycle for prepare/hw_free Srinivas Kandagatla
@ 2026-12-01 16:44 ` Pierre-Louis Bossart
2 siblings, 0 replies; 5+ messages in thread
From: Pierre-Louis Bossart @ 2026-12-01 16:44 UTC (permalink / raw)
To: Srinivas Kandagatla, Vinod Koul, Bard Liao
Cc: Sanyog Kale, Mark Brown, Jonathan Corbet, Bjorn Andersson,
Konrad Dybcio, alsa-devel, linux-sound, linux-arm-msm, linux-doc,
linux-kernel
On 11/30/26 11:40 PM, Srinivas Kandagatla wrote:
> During Speaker Protection patches for Qualcomm SoCs, Pierre suggested
> that we should add support to multi-direction in sdw_stream.
>
> This patchset is to start that discussion on how these apis should look
> like.
Thanks for starting this!
> Some SoundWire codec-codec links drive more than one direction on the
> same BE and the two directions are coupled on the bus: they share a
> bring-up sequence and want to fail or succeed together. The first
> in-tree case is WSA playback plus V/I-sense feedback on the same BE
> (DSP speaker protection and thermal/excursion feedback).
Humm, maybe before we start looking further, can you clarify if the
feedback has the same format as the playback PCM? I vaguely recall that
some amplifiers use custom formats, e.g. 10bits, for IV sense. The
resolution doesn't need to be as precise as for PCM, does it?
I initially thought that we could combine all ports for all
'constituents', but if they transport different data formats we do have
to stick with the rule 'one stream - one data format'.
The second question is whether the goal is to only deal with the
direction problem, or if we want a way to 'link' or 'bind' data streams
that need to be prepared together and later activated together. In that
case you could simplify the API to 'sdw_multi_stream' and forget about
the direction?
The feedback in your example does flow in the opposite direction, but
for companion amps the data comes from the main amp. SDCA also has a
concept of a DSP-generated 'enhancement' stream that would flow in
parallel to the main playback stream.
One important point is also that the concept of multiple streams is
'hidden' below the ASoC dailink level, where I don't think we can
work-around the direction. This API would only be for low-level data
that is not exposed to userspace, ever. IOW the Linux drivers would
provide configuration capability but no PCM devices/DMA support for
those auxiliary streams.
> Today callers like sound/soc/qcom/sdw.c hand-roll that: call
> sdw_prepare_stream() / sdw_enable_stream() per direction, open-code
> rollback when the second direction fails. The branching lives in
> every driver that wants a multi-direction codec-codec link.
>
> -- DO NOT APPLY THIS PATCH ---
>
> This RFC proposes a small first-class object for the concept, so the
> coupling is expressed in the type rather than left to caller
> discipline:
>
> struct sdw_multi_direction_stream;
>
> void sdw_multi_direction_stream_init (struct sdw_multi_direction_stream *md,
> const char *name);
> struct sdw_multi_direction_stream *
> sdw_multi_direction_stream_alloc (const char *name);
> void sdw_multi_direction_stream_release(struct sdw_multi_direction_stream *md);
> int sdw_multi_direction_stream_add (struct sdw_multi_direction_stream *md,
> struct sdw_stream_runtime *stream);
>
> int sdw_prepare_multi_direction_stream (struct sdw_multi_direction_stream *md);
> int sdw_enable_multi_direction_stream (struct sdw_multi_direction_stream *md);
> int sdw_disable_multi_direction_stream (struct sdw_multi_direction_stream *md);
> int sdw_deprepare_multi_direction_stream(struct sdw_multi_direction_stream *md);
>
> A group with one constituent behaves exactly like calling the matching
> sdw_prepare_stream() / ... directly, so callers can use this
> unconditionally instead of branching on dai_link->multi_direction_codecs.
>
> prepare/enable do all-or-nothing rollback: on failure at step N the
> earlier streams are torn down in reverse order. disable/deprepare are
> best-effort (first error returned, all entries walked) so the bus
> always ends up quiesced.
sorry, I am confused by the prepare/enable notation. Did you mean that
all streams are prepared in all-or-nothing manner, and separately that
the enable is also all-or-nothing? we're not combining prepare and
enable, are we?
edit: the patch2 does suggest combining prepare and enable in the same
routine, that's how I understand qcom_snd_sdw_build_multi_direction_stream()
This isn't how the Intel/AMD implementations work, see
sound/soc/sdw_utils/soc_sdw_utils.c. There are different callbacks for
prepare and trigger, not sure how they could ever be combined.
> sdw_multi_direction_stream_init() exists for callers that want to
> embed the handle in their own state (dai_link drvdata) and avoid any
> allocation on the PCM hot path; _alloc() is kept for callers that
> prefer a heap handle. Both expose the same lifecycle entry points.
>
> Patch 2 illustrates the shape at the call site by converting
> sound/soc/qcom/sdw.c. qcom_snd_sdw_prepare() and _hw_free() lose their
> hand-rolled rollback and funnel single-direction and multi-direction
> links through the same code. Patch 2 depends on the WSA
> speaker-protection series for the multi_direction_codecs dai_link
> flag; it is included here to validate the API shape and is not
> intended to land through the SoundWire tree.
>
> Open questions
> --------------
>
> 1. Future: coalescing the bank switch.
I think you mean that all 'constituents' of a multi_stream would start
at the same time on the same frame boundary?
I wonder how difficult that would be - in theory it's a broadcast write
command that doesn't depend on the number of ports, directions or
configuration.
One could push-back and argue that a single bank switch isn't really
needed, given the internal delays inside the peripherals and the lower
resolution of those additional data streams. Another point is that no
one will have a test rig to check if the synchronous start is actually
happening.
Ironically I think the prepare case is more complicated. One could have
a first pass to configure the bandwidth needs for the regular stream,
then a second pass for the next constituent. If that requires a clock
scaling or the use of another data lane this could be interesting to unroll.
At any rate, this is a very interesting extension indeed, thanks again
for starting this thread.
^ permalink raw reply [flat|nested] 5+ messages in thread