[PATCH] soundwire: stream: add multi-direction stream lifecycle API

From: Srinivas Kandagatla

Date: Mon Nov 30 2026 - 17:40:43 EST


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@xxxxxxxxxxxxxxxx>
---
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