[RFC PATCH 2/2] ASoC: qcom: sdw: use multi-direction stream lifecycle for prepare/hw_free

From: Srinivas Kandagatla

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


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