Linux Sound subsystem development
 help / color / mirror / Atom feed
* [PATCH] ASoC: pcm1681: Establish contact with the DAC lazily
@ 2026-12-04 17:30 Mohammad Rafi Shaik
  2026-12-04 18:02 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Mohammad Rafi Shaik @ 2026-12-04 17:30 UTC (permalink / raw)
  To: Shenghao Ding, Kevin Lu, Baojun Xu, Sen Wang, Liam Girdwood,
	Mark Brown, Jaroslav Kysela, Takashi Iwai, Shawn Guo,
	Mohammad Rafi Shaik
  Cc: linux-sound

The DAC clocks through an internal reset over the 65536 system clock
cycles that follow sck appearing, and its I2C control port does not
answer until that reset has completed. The driver waits out those cycles
right after enabling sck, in probe and in runtime resume, and syncs the
register cache once the wait is over.

That assumes sck is running by the time clk_prepare_enable() returns,
and not every provider works that way. On Qualcomm boards sck is an
LPASS clock exported by the q6prm clock controller, which turns the
enable into an APR command to the ADSP. The command succeeds, but the
pad is only driven while the audio interface the DAC hangs off is
active, and that interface is brought up from the machine driver's
hw_params, by snd_soc_dai_set_sysclk() on the CPU DAI. The machine
driver runs first and the codec DAI straight after it, so the DAC is
microseconds into a 5.3 ms reset when the first stream programs it:

 pcm1681 0-004c: ASoC error (-6): at snd_soc_dai_hw_params()
 pcm1681 0-004c: ASoC error (-6): at snd_soc_dai_digital_mute()

The second stream works, because by then sck has been running long
enough for the reset to complete, which makes the DAC look like it needs
a longer settle time than it does. The -6 is the i2c-qcom-geni mapping
of an address NACK, so the failure reads as a device that is not there
rather than one that is not ready yet.

Those failed writes also cost the register cache: regmap drops the cache
entry behind a write it could not complete, so the format and soft mute
writes of the failed stream take their registers out of the cache, and
reads of them go to a device that still is not answering.

Stop trying to pick the moment at which the device has to be reachable.
Keep the regmap in cache-only mode until the control port has answered
once, and do the waiting in pcm1681_ensure_ready(), called from
hw_params where the host interface is up and sck is known to be running.
Probe and runtime resume call it too, so a board whose sck runs
continuously is still set up as early as it was before, but neither
fails when the device stays quiet: failing probe would take the DAC away
for good over a clock that is simply not running yet, and while no
stream is running there is nothing that needs the device to be reachable
anyway. Resume only probes the control port, it leaves the reset to
hw_params rather than stalling a resume for a clock that may never
start.

A device that does answer is soft reset before the cache is replayed
onto it, so the cache describes the device instead of assuming the
registers still hold their power-on defaults.

Fixes: 8987e9a24f45 ("ASoC: codec: pcm1681: Add optional SCK clock and runtime PM support")
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Mohammad Rafi Shaik <mohammad.rafi.shaik@oss.qualcomm.com>
---
 sound/soc/codecs/pcm1681.c | 167 ++++++++++++++++++++++++++++++++-----
 1 file changed, 148 insertions(+), 19 deletions(-)

diff --git a/sound/soc/codecs/pcm1681.c b/sound/soc/codecs/pcm1681.c
index 2aae372389ad..3ad0582cb6f0 100644
--- a/sound/soc/codecs/pcm1681.c
+++ b/sound/soc/codecs/pcm1681.c
@@ -10,6 +10,7 @@
 #include <linux/slab.h>
 #include <linux/delay.h>
 #include <linux/i2c.h>
+#include <linux/iopoll.h>
 #include <linux/regmap.h>
 #include <linux/of.h>
 #include <linux/clk.h>
@@ -37,13 +38,32 @@
 #define PCM1681_DAC_CONTROL	0x08	/* DAC operation control */
 #define PCM1681_FMT_CONTROL	0x09	/* Audio interface data format */
 #define PCM1681_DEEMPH_CONTROL	0x0a	/* De-emphasis control */
+#define PCM1681_SRST			0x80	/* Soft reset, self clearing */
 #define PCM1681_ZERO_DETECT_STATUS	0x0e	/* Zero detect status reg */
 
 /*
  * The DAC requires 65536 system clock cycles after the clock is supplied to
- * complete its internal reset sequence before it is ready to be used.
+ * complete its internal reset sequence before it is ready to be used, and a
+ * soft reset takes 1024 of them.
  */
 #define PCM1681_SCK_SETTLE_CYCLES	65536
+#define PCM1681_SRST_CYCLES		1024
+
+/*
+ * Worst case rate assumed when converting a cycle count into a delay and the
+ * sck rate cannot be read, which is 128fs at the lowest rate in
+ * PCM1681_PCM_RATES. Waiting too long only costs time at probe, not waiting
+ * long enough leaves the control port unresponsive.
+ */
+#define PCM1681_SCK_MIN_RATE		(8000 * 128)
+
+/*
+ * Slack on top of the reset time, for a clock that starts a little later than
+ * it is enabled. Kept short because a device that is not there costs this on
+ * every attempt to reach it.
+ */
+#define PCM1681_READY_POLL_US		1000
+#define PCM1681_READY_TIMEOUT_US	(50 * USEC_PER_MSEC)
 
 static const struct reg_default pcm1681_reg_defaults[] = {
 	{ 0x01,	0xff },
@@ -90,21 +110,117 @@ struct pcm1681_private {
 	/* Current rate for deemphasis control */
 	unsigned int rate;
 	struct clk *sck;
+	/* The control port answers and the cache describes the device */
+	bool ready;
 };
 
-/* Wait for the DAC to come out of its internal reset after sck is enabled */
-static void pcm1681_sck_settle(struct pcm1681_private *priv)
+/* Delay covering @cycles system clock cycles */
+static void pcm1681_sck_delay(struct pcm1681_private *priv, unsigned int cycles)
 {
 	unsigned long rate = clk_get_rate(priv->sck);
 
 	/*
 	 * sck is optional, so there may be no clock to wait on, and its rate
-	 * is not always discoverable. Only wait when the cycle count can be
-	 * converted into a delay.
+	 * is not always discoverable. Assume the slowest supported clock when
+	 * the rate cannot be read, the resulting delays are still short.
 	 */
-	if (rate)
-		fsleep(DIV_ROUND_UP_ULL(PCM1681_SCK_SETTLE_CYCLES *
-					(u64)USEC_PER_SEC, rate));
+	if (!rate)
+		rate = PCM1681_SCK_MIN_RATE;
+
+	fsleep(DIV_ROUND_UP_ULL(cycles * (u64)USEC_PER_SEC, rate));
+}
+
+/*
+ * Return the DAC to its reset defaults, which is the state the register cache
+ * describes. The cache is otherwise only as accurate as the assumption that
+ * nothing else has touched the device.
+ */
+static int pcm1681_soft_reset(struct pcm1681_private *priv)
+{
+	int ret;
+
+	/*
+	 * SRST is cleared by the reset it triggers, so it must not end up in
+	 * the cache, where it would be replayed on every resume.
+	 */
+	regcache_cache_bypass(priv->regmap, true);
+	ret = regmap_write(priv->regmap, PCM1681_DEEMPH_CONTROL, PCM1681_SRST);
+	regcache_cache_bypass(priv->regmap, false);
+	if (ret)
+		return ret;
+
+	pcm1681_sck_delay(priv, PCM1681_SRST_CYCLES);
+
+	return 0;
+}
+
+/*
+ * The DAC needs 65536 sck cycles after the clock appears to clock through its
+ * internal reset, and its control port does not answer before that has
+ * completed. Which means the driver cannot establish contact with the device
+ * at a time of its own choosing: sck is an output of the host interface the
+ * DAC is attached to, and there are host controllers that only drive it while
+ * that interface is active, so the clock can be enabled as far as the clock
+ * API is concerned and still not be running.
+ *
+ * So treat the reset as something to be waited out at the first access that
+ * has to reach the device, rather than once at probe, and keep the regmap in
+ * cache only mode until it has been. Writes that fail are worse than writes
+ * that are deferred: regmap drops the cache entry behind a failed write, which
+ * costs the state the cache was keeping for the device.
+ */
+static int pcm1681_ensure_ready(struct pcm1681_private *priv, struct device *dev,
+				bool wait)
+{
+	unsigned int val;
+	int err = 0, ret;
+
+	if (priv->ready)
+		return 0;
+
+	regcache_cache_only(priv->regmap, false);
+
+	/* The status register is volatile, so a read of it reaches the device */
+	if (wait) {
+		pcm1681_sck_delay(priv, PCM1681_SCK_SETTLE_CYCLES);
+
+		ret = read_poll_timeout(regmap_read, err, !err,
+					PCM1681_READY_POLL_US,
+					PCM1681_READY_TIMEOUT_US, false,
+					priv->regmap, PCM1681_ZERO_DETECT_STATUS,
+					&val);
+		if (ret && err)
+			ret = err;
+	} else {
+		ret = regmap_read(priv->regmap, PCM1681_ZERO_DETECT_STATUS, &val);
+	}
+	if (ret) {
+		dev_dbg(dev, "Control port does not answer yet (%d)\n", ret);
+		goto err_cache_only;
+	}
+
+	/* Put the device in the state the cache describes, then replay it */
+	ret = pcm1681_soft_reset(priv);
+	if (ret) {
+		dev_err(dev, "Failed to reset the DAC: %d\n", ret);
+		goto err_cache_only;
+	}
+
+	regcache_mark_dirty(priv->regmap);
+	ret = regcache_sync(priv->regmap);
+	if (ret) {
+		dev_err(dev, "Failed to sync regcache: %d\n", ret);
+		goto err_cache_only;
+	}
+
+	priv->ready = true;
+
+	return 0;
+
+err_cache_only:
+	regcache_cache_only(priv->regmap, true);
+
+	return ret;
 }
 
 static const int pcm1681_deemph[] = { 44100, 48000, 32000 };
@@ -197,6 +313,13 @@ static int pcm1681_hw_params(struct snd_pcm_substream *substream,
 	struct pcm1681_private *priv = snd_soc_component_get_drvdata(component);
 	int val = 0, ret;
 
+	ret = pcm1681_ensure_ready(priv, component->dev, true);
+	if (ret) {
+		dev_err(component->dev,
+			"Control port not responding, check sck and the supplies\n");
+		return ret;
+	}
+
 	priv->rate = params_rate(params);
 
 	switch (priv->format & SND_SOC_DAIFMT_FORMAT_MASK) {
@@ -371,6 +494,9 @@ static int pcm1681_i2c_probe(struct i2c_client *client)
 		return ret;
 	}
 
+	/* Nothing reaches the DAC until it is known to answer */
+	regcache_cache_only(priv->regmap, true);
+
 	/* Must be set before runtime PM is enabled, the callbacks use it */
 	i2c_set_clientdata(client, priv);
 
@@ -378,7 +504,12 @@ static int pcm1681_i2c_probe(struct i2c_client *client)
 	if (ret)
 		return dev_err_probe(dev, ret, "Failed to enable sck\n");
 
-	pcm1681_sck_settle(priv);
+	/*
+	 * Take the first chance to set the DAC up. Not being able to is not
+	 * fatal, it only means sck is not running yet and the first stream has
+	 * to wait out the reset instead.
+	 */
+	pcm1681_ensure_ready(priv, dev, true);
 
 	/* The clock is on, so hand the now-active device over to runtime PM */
 	pm_runtime_set_autosuspend_delay(dev, 100);
@@ -430,6 +561,7 @@ static int pcm1681_runtime_suspend(struct device *dev)
 	 */
 	regcache_cache_only(priv->regmap, true);
 	regcache_mark_dirty(priv->regmap);
+	priv->ready = false;
 
 	clk_disable_unprepare(priv->sck);
 
@@ -447,16 +579,13 @@ static int pcm1681_runtime_resume(struct device *dev)
 		return ret;
 	}
 
-	pcm1681_sck_settle(priv);
-
-	regcache_cache_only(priv->regmap, false);
-	ret = regcache_sync(priv->regmap);
-	if (ret) {
-		dev_err(dev, "Failed to sync regcache: %d\n", ret);
-		regcache_cache_only(priv->regmap, true);
-		clk_disable_unprepare(priv->sck);
-		return ret;
-	}
+	/*
+	 * Gating sck does not put the DAC back into reset, so if sck is running
+	 * the control port answers right away and the cache can be replayed
+	 * here. If it is not, hw_params is left to wait out the reset, resuming
+	 * is not a reason to fail and not a reason to stall either.
+	 */
+	pcm1681_ensure_ready(priv, dev, false);
 
 	return 0;
 }
-- 
2.34.1


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

end of thread, other threads:[~2026-12-04 18:02 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 17:30 [PATCH] ASoC: pcm1681: Establish contact with the DAC lazily Mohammad Rafi Shaik
2026-12-04 18:02 ` sashiko-bot

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