mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] soundwire: qcom: wait for the link to stop before gating the clock
@ 2026-11-29 20:22 Luke Macken
  0 siblings, 0 replies; only message in thread
From: Luke Macken @ 2026-11-29 20:22 UTC (permalink / raw)
  To: srini, vkoul, yung-chuan.liao
  Cc: pierre-louis.bossart, okerixx, gopi.kanala, linux-sound,
	linux-arm-msm, linux-kernel, Luke Macken

On a Lenovo ThinkPad T14s Gen 7 (v3.1.0 controller, two WSA884x
amplifiers), the speakers are intermittently silent when playback starts
after the bus has been in clock stop. The kernel logs "SWR bus clsh
detected" and "Parity error detected". This is with commit 6c067677192f
("soundwire: qcom: disable the interrupt before gating the clock")
applied.

swrm_runtime_suspend() gates hclk right after sdw_bus_clk_stop()
returns, without checking that the frame generator has stopped. On this
laptop, hclk is the clock output of the LPASS WSA macro. Disabling it
clears the macro's SoundWire clock enable, so the bus clock can be taken
away while the frame generator is still running. In a trace, the frame
generator was still enabled when sdw_bus_clk_stop() returned in 13 of 40
runtime suspends.

Qualcomm's downstream driver sleeps 100 us after ClockStopNow. On
controller versions above v1.5.1, it then polls the link status until
the frame generator is disabled or a retry limit is reached. Only then
is the clock released [1]. Do the same; on timeout (about 50 ms) warn
and gate the clock anyway. A single read was enough in all 40 traced
runtime suspends, so the usual cost is the sleep and one register read.

A 2.5 s tone was started every 15 s, each time after a confirmed runtime
suspend of the controller. Without this change, 34 of 120 starts were
silent. With it, none of 120 were silent and neither message was logged.

Link: https://git.codelinaro.org/clo/la/platform/vendor/opensource/audio-kernel/-/blob/5adf8aa04e8f4d987a398c8be3e3d3cfd2feb9f9/soc/swr-mstr-ctrl.c#L2886-2895 # [1]
Fixes: 74e79da9fd46 ("soundwire: qcom: add runtime pm support")
Assisted-by: LLM
Signed-off-by: Luke Macken <luke@phorex.org>
---
Based on soundwire/fixes. There is no Cc: stable because I have seen
this fail only on this laptop, whose devicetree is new in v7.4.

Gen 7 tests and trace: v7.4-rc4 + 6c067677192f, soundwire/next, the
Qualcomm and sound branches queued for 7.5, the pending patch [2] and
series [3], and this laptop's out-of-tree patches (none touches
drivers/soundwire or sound/). [2] changes the same function; the two
patches do not conflict. In the same test, none of 240 starts were
silent with only this patch's sleep, and none of 120 with only its poll.

Limitations:
- A ThinkPad T14s Gen 6 (v2.0.0 controller) does not fail, and I do not
  know why. It ran Fedora's 7.3.5 kernel, which does not have
  6c067677192f. With that kernel unmodified, sdw_bus_clk_stop() also
  returned with the frame generator still enabled in 8 of 99 traced
  runtime suspends. Yet none of 120 starts, all after a clock stop, were
  silent. With soundwire-qcom.ko rebuilt with this change, none of 120
  were silent either; 39 of those were confirmed to follow a clock stop.
- Not tested: controllers other than v2.0.0 and v3.1.0 (up to v1.5.1
  only the sleep is added); the multi-master mode in soundwire/next,
  where the dependent controller's runtime suspend returns before the
  new code; the "link failed to disconnect" path, which never ran.

[2] https://lore.kernel.org/r/20261128-sdw-qcom-irq-v1-1-d053ac530faa@oss.qualcomm.com
[3] https://lore.kernel.org/r/20261125103825.936939-1-srinivas.kandagatla@oss.qualcomm.com

An AI coding assistant (Claude Code) traced the driver, found the
downstream handling, wrote the patch and this text, and ran the tests. I
have reviewed all of it.

 drivers/soundwire/qcom.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)

diff --git a/drivers/soundwire/qcom.c b/drivers/soundwire/qcom.c
index 147e55b7a6..588bd6acf4 100644
--- a/drivers/soundwire/qcom.c
+++ b/drivers/soundwire/qcom.c
@@ -915,6 +915,30 @@ static irqreturn_t qcom_swrm_irq_handler(int irq, void *dev_id)
 	return ret;
 }
 
+static void swrm_wait_for_frame_gen_disabled(struct qcom_swrm_ctrl *ctrl)
+{
+	int retry = SWRM_LINK_STATUS_RETRY_CNT;
+	u32 comp_sts;
+
+	/*
+	 * Downstream only checks the link status from v1.6 on, where HCLK
+	 * and MCLK are decoupled.
+	 */
+	if (ctrl->version <= SWRM_VERSION_1_5_1)
+		return;
+
+	do {
+		ctrl->reg_read(ctrl, ctrl->reg_layout[SWRM_REG_FRAME_GEN_ENABLED],
+			       &comp_sts);
+		if (!(comp_sts & SWRM_FRM_GEN_ENABLED))
+			return;
+
+		usleep_range(500, 510);
+	} while (retry--);
+
+	dev_warn_ratelimited(ctrl->dev, "link failed to disconnect\n");
+}
+
 static bool swrm_wait_for_frame_gen_enabled(struct qcom_swrm_ctrl *ctrl)
 {
 	int retry = SWRM_LINK_STATUS_RETRY_CNT;
@@ -1886,6 +1910,10 @@ static int __maybe_unused swrm_runtime_suspend(struct device *dev)
 			dev_err(dev, "bus clock stop failed %d", ret);
 			return ret;
 		}
+
+		/* Let the link stop before its clock is gated */
+		usleep_range(100, 105);
+		swrm_wait_for_frame_gen_disabled(ctrl);
 	}
 
 	disable_irq(ctrl->irq);

base-commit: 6c067677192f054bbfa815caf04f536bb8dcdee2
-- 
2.43.0


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-29 20:23 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-29 20:22 [PATCH] soundwire: qcom: wait for the link to stop before gating the clock Luke Macken

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®