mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Srinivas Kandagatla <srinivas.kandagatla@oss.qualcomm.com>
To: Vinod Koul <vkoul@kernel.org>,
	Bard Liao <yung-chuan.liao@linux.intel.com>
Cc: Pierre-Louis Bossart <pierre-louis.bossart@linux.dev>,
	Sanyog Kale <sanyog.r.kale@intel.com>,
	Mark Brown <broonie@kernel.org>, Jonathan Corbet <corbet@lwn.net>,
	Bjorn Andersson <andersson@kernel.org>,
	Konrad Dybcio <konrad.dybcio@oss.qualcomm.com>,
	Srinivas Kandagatla <srinivas.kandagatla@oss.qualcomm.com>,
	alsa-devel@alsa-project.org, linux-sound@vger.kernel.org,
	linux-arm-msm@vger.kernel.org, linux-doc@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: [PATCH] soundwire: stream: add multi-direction stream lifecycle API
Date: Mon, 30 Nov 2026 22:40:17 +0000	[thread overview]
Message-ID: <20261130224018.20359-2-srinivas.kandagatla@oss.qualcomm.com> (raw)
In-Reply-To: <20261130224018.20359-1-srinivas.kandagatla@oss.qualcomm.com>

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

  reply	other threads:[~2026-11-30 22:40 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30 22:40 [RFC PATCH 0/2] soundwire: multi-direction stream API Srinivas Kandagatla
2026-11-30 22:40 ` Srinivas Kandagatla [this message]
2026-11-30 23:24   ` [PATCH] soundwire: stream: add multi-direction stream lifecycle API 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
2026-12-01 23:36   ` Srinivas Kandagatla

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261130224018.20359-2-srinivas.kandagatla@oss.qualcomm.com \
    --to=srinivas.kandagatla@oss.qualcomm.com \
    --cc=alsa-devel@alsa-project.org \
    --cc=andersson@kernel.org \
    --cc=broonie@kernel.org \
    --cc=corbet@lwn.net \
    --cc=konrad.dybcio@oss.qualcomm.com \
    --cc=linux-arm-msm@vger.kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-sound@vger.kernel.org \
    --cc=pierre-louis.bossart@linux.dev \
    --cc=sanyog.r.kale@intel.com \
    --cc=vkoul@kernel.org \
    --cc=yung-chuan.liao@linux.intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®