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

* Re: [PATCH] ASoC: codecs: wcd9378: defer SoundWire SCP init until attachment
  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
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-03 14:45 UTC (permalink / raw)
  To: Ajay Kumar Nandam; +Cc: linux-sound

Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [Medium] ASoC: wcd9378: class_boot_work() races with deferred SCP init
  on warm probe
- [Medium] ASoC: wcd9378: failed SCP init in update_status() is ignored
  and never retried
- [Medium] ASoC: wcd9378: update_status() skips SCP init when probed in
  SDW_SLAVE_ALERT
- [Low] ASoC: wcd9378: redundant scp_init_lock and dead enum
  wcd9378_scp_init_state
- [Low] ASoC: wcd9378: missing Fixes trailer referencing commit
  0c30b8ca84ae
--

commit 93531027bbe34ef9e8afd9391c50e46957480c54
Author: Ajay Kumar Nandam <ajay.nandam@oss.qualcomm.com>

> ASoC: codecs: wcd9378: defer SoundWire SCP init until attachment
>
> 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>

[Severity: Low]
This fixes a warm re-probe deadlock introduced when the driver was added.
Should this include:

  Fixes: 0c30b8ca84ae ("ASoC: codecs: add Qualcomm Tambora (WCD9378) SDCA codec")

> diff --git a/sound/soc/codecs/wcd9378-sdca.c b/sound/soc/codecs/wcd9378-sdca.c
> index efbbeb3dc721a..b00acae866541 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;
>  };

[Severity: Low]
Is scp_init_lock or the four-state enum needed here?  Every caller of
update_status() in the SoundWire core (sdw_bus_probe() and
sdw_update_slave_status()) already holds slave->sdw_dev_lock, so
update_status() cannot run concurrently.

WCD9378_SCP_INITIALIZING is overwritten before returning and never read,
and WCD9378_SCP_FAILED_RETRYABLE behaves the same as
WCD9378_SCP_NOT_INITIALIZED.  The comment also names WCD9378_SCP_INIT_*,
which does not match the enumerators.  A single bool flag would provide
the same behavior.

[ ... ]

> @@ -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);

[Severity: Medium]
Can class_boot_work() now race with or run ahead of
wcd9378_sdca_scp_init()?

When the peripheral attaches before slave->probed becomes true (for
example on late module load or driver rebind), sdw_handle_slave_status()
has already signaled slave->initialization_complete while skipping
update_status().  wcd9378_sdca_probe() then calls sdca_class_probe(),
which queues drv->boot_work before sdw_bus_probe() reaches
update_status():

CPU0 (sdw_bus_probe)                kworker (class_boot_work)
wcd9378_sdca_probe()
  sdca_class_probe()
    queue_work(&drv->boot_work)
  return 0
                                    sdw_slave_wait_for_init()
                                      /* already complete */
read_prop() / sysfs init            class_function_boot()
                                      <-- runs before SCP init
update_status(..., ATTACHED)
  wcd9378_sdca_scp_init()

class_function_boot() writes wcd9378_sdca_init_table (including SCP clock
registers and RESET_FUNCTION_NOW) and registers the ASoC component and
DAIs without waiting for the bank-1 shadow and SDW_SCP_COMMIT writes,
risking misconfigured TX PDM clocking.

[ ... ]

> +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;

[Severity: Medium]
Should SDW_SLAVE_ALERT also trigger wcd9378_sdca_scp_init()?

sdw_handle_slave_alerts() sets slave->status to SDW_SLAVE_ALERT and never
restores it to SDW_SLAVE_ATTACHED.  On the Qualcomm SoundWire controller,
qcom_swrm_get_alert_slave_dev_num() does not update ctrl->slave_status,
so once the alert clears, qcom_swrm_irq_handler() sees
ctrl->slave_status == slave_status and leaves slave->status stuck at
SDW_SLAVE_ALERT.

If wcd9378 binds or rebinds in that state, sdw_bus_probe() passes
SDW_SLAVE_ALERT here and skips SCP init while class_boot_work() still
registers the codec, leaving TX PDM clocking uninitialized.

[ ... ]

> +	ret = wcd9378_sdca_scp_init(slave);
> +	if (ret) {
> +		priv->scp_init_state = WCD9378_SCP_FAILED_RETRYABLE;
> +		return ret;
> +	}

[Severity: Medium]
Will wcd9378_sdca_scp_init() ever be retried if it fails here?

Both sdw_handle_slave_status() and sdw_bus_probe() only log a negative
return from update_status(), still complete slave->initialization_complete,
and let class_boot_work() register the SDCA function and DAIs.  While the
slave stays attached, neither the SoundWire core nor
qcom_swrm_irq_handler() emits another SDW_SLAVE_ATTACHED notification, so
TX PDM clocking remains broken until an unrelated unattach/reattach cycle.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261203-ak-gt-wcd9378-defer-scp-init-v1-1-33d834e8d307@oss.qualcomm.com?part=1

^ permalink raw reply	[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