Linux Sound subsystem development
 help / color / mirror / Atom feed
* [PATCH] ASoC: codecs: wcd9378: defer SoundWire SCP init until attachment
@ 2026-12-03 14:25 Ajay Kumar Nandam
  2026-12-03 14:45 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Ajay Kumar Nandam @ 2026-12-03 14:25 UTC (permalink / raw)
  To: Srinivas Kandagatla, Liam Girdwood, Mark Brown, Jaroslav Kysela,
	Takashi Iwai
  Cc: linux-sound, linux-arm-msm, Ajay Kumar Nandam

wcd9378_sdca_probe() called sdw_slave_wait_for_init(slave, 5000) to
gate two SCP register writes (TX PDM clock-div bank-1 shadow and
SDW_SCP_COMMIT), waiting for the SoundWire core to signal that the
peripheral has completed initial attachment and enumeration.

This wait can only be satisfied once the SoundWire controller's own
probe has progressed through bus enumeration and slave status
handling. When the peripheral device is recreated and reprobed while
the controller is still inside its own probe call (e.g. after the
controller unbinds and rebinds its SoundWire master), the controller
thread is blocked inside this codec's probe function, so it can never
reach the status handling required to satisfy the wait. The probe
then fails after the 5 second timeout.

Move the SCP initialization out of .probe() and into a new
update_status() callback, performing it only once the core reports
SDW_SLAVE_ATTACHED, mirroring the pattern used by other SoundWire
peripheral drivers (e.g. rt711-sdw.c). Initialization state is tracked
explicitly so repeated ATTACHED notifications are idempotent, a
transient failure remains retryable on a later ATTACHED event, and an
UNATTACHED notification resets the state so a subsequent attach
reinitializes correctly. A mutex guards the state against concurrent
update_status invocations.

Signed-off-by: Ajay Kumar Nandam <ajay.nandam@oss.qualcomm.com>
---
This is a single, standalone fix in the wcd9378 codec driver; it does
not depend on anything else and has no series relationship to other
work I have sent.

I ran into this while testing SoundWire controller/peripheral restart
on a QCM6490-based board: wcd9378_sdca_probe() called
sdw_slave_wait_for_init() unconditionally, which deadlocks when the
device is reprobed while its SoundWire controller is itself still in
the middle of probing.
---
 sound/soc/codecs/wcd9378-sdca.c | 98 ++++++++++++++++++++++++++++++++++-------
 sound/soc/codecs/wcd9378-sdca.h |  2 +
 sound/soc/codecs/wcd9378-sdw.c  |  1 +
 3 files changed, 85 insertions(+), 16 deletions(-)

diff --git a/sound/soc/codecs/wcd9378-sdca.c b/sound/soc/codecs/wcd9378-sdca.c
index efbbeb3dc721..b00acae86654 100644
--- a/sound/soc/codecs/wcd9378-sdca.c
+++ b/sound/soc/codecs/wcd9378-sdca.c
@@ -27,8 +27,24 @@
 #include <sound/soc-dai.h>
 #include "wcd9378-sdca.h"
 
+/*
+ * SCP one-shot init state.  WCD9378_SCP_INIT_* tracks the TX PDM
+ * clock-div + SCP_COMMIT pair below, which must run exactly once per
+ * attach session, after the slave reaches SDW_SLAVE_ATTACHED and
+ * before any port is enabled.
+ */
+enum wcd9378_scp_init_state {
+	WCD9378_SCP_NOT_INITIALIZED = 0,
+	WCD9378_SCP_INITIALIZING,
+	WCD9378_SCP_INITIALIZED,
+	WCD9378_SCP_FAILED_RETRYABLE,
+};
+
 struct wcd9378_priv {
 	struct sdca_class_drv class;
+	/* guards scp_init_state against concurrent update_status calls */
+	struct mutex scp_init_lock;
+	enum wcd9378_scp_init_state scp_init_state;
 };
 
 static struct sdca_entity wcd9378_sdca_entities[];
@@ -991,6 +1007,12 @@ int wcd9378_sdca_probe(struct sdw_slave *slave,
 	if (!priv)
 		return -ENOMEM;
 
+	ret = devm_mutex_init(dev, &priv->scp_init_lock);
+	if (ret)
+		return ret;
+
+	priv->scp_init_state = WCD9378_SCP_NOT_INITIALIZED;
+
 	dev_set_drvdata(dev, priv);
 
 	/* No SPMI parent: supplies and reset live on the SoundWire DT node. */
@@ -1012,24 +1034,12 @@ int wcd9378_sdca_probe(struct sdw_slave *slave,
 		usleep_range(20, 30);
 	}
 
-	/* SCP writes below need the slave attached. */
-	ret = sdw_slave_wait_for_init(slave, 5000);
-	if (ret)
-		return dev_err_probe(dev, ret,
-				     "slave attach timeout: %d\n", ret);
-
 	/*
-	 * TX PDM clock: bank-1 shadow + SCP_COMMIT.  SCP survives PDE
-	 * cycles; one-shot before any port is enabled.
+	 * The SCP init below moved to wcd9378_sdca_update_status() on first
+	 * ATTACHED: waiting here synchronously deadlocks a warm re-probe, since
+	 * the controller thread that would satisfy the wait is blocked in this
+	 * very probe call.
 	 */
-	ret = sdw_write_no_pm(slave, WCD9378_SCP_HOST_CLK_DIV2_CTL_B1, 0x01);
-	if (ret)
-		return dev_err_probe(dev, ret,
-				     "HOST_CLK_DIV2_CTL_B1: %d\n", ret);
-
-	ret = sdw_write_no_pm(slave, SDW_SCP_COMMIT, 0x02);
-	if (ret)
-		return dev_err_probe(dev, ret, "SCP_COMMIT: %d\n", ret);
 
 	/* DT has no DisCo enumeration; seed the descriptor here. */
 	if (!data->num_functions) {
@@ -1049,6 +1059,62 @@ void wcd9378_sdca_remove(struct sdw_slave *slave)
 	sdca_class_remove(&priv->class);
 }
 
+/*
+ * TX PDM clock: bank-1 shadow + SCP_COMMIT.  SCP survives PDE cycles;
+ * one-shot per attach session, before any port is enabled.
+ */
+static int wcd9378_sdca_scp_init(struct sdw_slave *slave)
+{
+	int ret;
+
+	ret = sdw_write_no_pm(slave, WCD9378_SCP_HOST_CLK_DIV2_CTL_B1, 0x01);
+	if (ret) {
+		dev_err(&slave->dev, "HOST_CLK_DIV2_CTL_B1: %d\n", ret);
+		return ret;
+	}
+
+	ret = sdw_write_no_pm(slave, SDW_SCP_COMMIT, 0x02);
+	if (ret) {
+		dev_err(&slave->dev, "SCP_COMMIT: %d\n", ret);
+		return ret;
+	}
+
+	return 0;
+}
+
+int wcd9378_sdca_update_status(struct sdw_slave *slave,
+			       enum sdw_slave_status status)
+{
+	struct wcd9378_priv *priv = dev_get_drvdata(&slave->dev);
+	int ret;
+
+	if (status == SDW_SLAVE_UNATTACHED) {
+		guard(mutex)(&priv->scp_init_lock);
+		priv->scp_init_state = WCD9378_SCP_NOT_INITIALIZED;
+		return 0;
+	}
+
+	if (status != SDW_SLAVE_ATTACHED)
+		return 0;
+
+	guard(mutex)(&priv->scp_init_lock);
+
+	if (priv->scp_init_state == WCD9378_SCP_INITIALIZED)
+		return 0;
+
+	priv->scp_init_state = WCD9378_SCP_INITIALIZING;
+
+	ret = wcd9378_sdca_scp_init(slave);
+	if (ret) {
+		priv->scp_init_state = WCD9378_SCP_FAILED_RETRYABLE;
+		return ret;
+	}
+
+	priv->scp_init_state = WCD9378_SCP_INITIALIZED;
+
+	return 0;
+}
+
 int wcd9378_sdca_runtime_suspend(struct device *dev)
 {
 	struct wcd9378_priv *priv = dev_get_drvdata(dev);
diff --git a/sound/soc/codecs/wcd9378-sdca.h b/sound/soc/codecs/wcd9378-sdca.h
index 6ddae9da395b..da7c70c923bd 100644
--- a/sound/soc/codecs/wcd9378-sdca.h
+++ b/sound/soc/codecs/wcd9378-sdca.h
@@ -11,6 +11,8 @@ int  wcd9378_sdca_probe(struct sdw_slave *slave,
 			const struct sdw_device_id *id);
 void wcd9378_sdca_remove(struct sdw_slave *slave);
 int  wcd9378_sdca_read_prop(struct sdw_slave *slave);
+int  wcd9378_sdca_update_status(struct sdw_slave *slave,
+				enum sdw_slave_status status);
 
 int wcd9378_sdca_runtime_suspend(struct device *dev);
 int wcd9378_sdca_runtime_resume(struct device *dev);
diff --git a/sound/soc/codecs/wcd9378-sdw.c b/sound/soc/codecs/wcd9378-sdw.c
index 52295e7646cf..1ca0990d520d 100644
--- a/sound/soc/codecs/wcd9378-sdw.c
+++ b/sound/soc/codecs/wcd9378-sdw.c
@@ -24,6 +24,7 @@ static const struct dev_pm_ops wcd9378_sdw_pm_ops = {
 
 static const struct sdw_slave_ops wcd9378_sdw_ops = {
 	.read_prop = wcd9378_sdca_read_prop,
+	.update_status = wcd9378_sdca_update_status,
 };
 
 static const struct sdw_device_id wcd9378_sdw_id[] = {

---
base-commit: d138596e9f822033490b2e577ec2ea64f2aa246c
change-id: 20261203-ak-gt-wcd9378-defer-scp-init-fa9cb6a631e6

Best regards,
--  
Ajay Kumar Nandam <ajay.nandam@oss.qualcomm.com>


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-03 14:45 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 14:25 [PATCH] ASoC: codecs: wcd9378: defer SoundWire SCP init until attachment Ajay Kumar Nandam
2026-12-03 14:45 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox