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

* Re: [PATCH] ASoC: pcm1681: Establish contact with the DAC lazily
  2026-12-04 17:30 [PATCH] ASoC: pcm1681: Establish contact with the DAC lazily Mohammad Rafi Shaik
@ 2026-12-04 18:02 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-04 18:02 UTC (permalink / raw)
  To: Mohammad Rafi Shaik; +Cc: linux-sound

Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [High] ASoC: pcm1681: resume skips wait and leaves active stream in
  cache-only mode
- [Medium] ASoC: pcm1681: pcm1681_ensure_ready() clears cache_only
  before DAC is ready
- [Medium] ASoC: pcm1681: pcm1681_soft_reset() toggles cache_bypass
  without locking
- [Medium] ASoC: pcm1681: ready and cache-only only reset in
  pcm1681_runtime_suspend()
- [Medium] ASoC: pcm1681: probe and hw_params stall via
  pcm1681_ensure_ready()
--

commit 75104116faab40978c73eecd5c4a280bc41e56fd
Author: Mohammad Rafi Shaik <mohammad.rafi.shaik@oss.qualcomm.com>

ASoC: pcm1681: Establish contact with the DAC lazily

This commit keeps the pcm1681 regmap in cache-only mode until the DAC
control port responds, probing and soft-resetting it lazily from
pcm1681_ensure_ready() in probe, runtime resume, and hw_params.

> diff --git a/sound/soc/codecs/pcm1681.c b/sound/soc/codecs/pcm1681.c
> index 2aae372389ad6..3ad0582cb6f0d 100644
> --- a/sound/soc/codecs/pcm1681.c
> +++ b/sound/soc/codecs/pcm1681.c

[ ... ]

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

[Severity: Medium]
Can toggling regcache_cache_bypass() across separate regmap calls silently
drop a concurrent ALSA mixer update?

regcache_cache_bypass() releases the regmap lock as soon as it returns, and
ALSA kcontrol writes only hold card->controls_rwsem while
pcm1681_hw_params() holds card->pcm_mutex:

CPU0                                  CPU1
pcm1681_hw_params()                   snd_ctl_elem_write()
  pcm1681_ensure_ready()                soc_put_volsw()
    pcm1681_soft_reset()
      regcache_cache_bypass(..., true)
                                          regmap_update_bits()
                                            <-- skips regcache_write()
      regmap_write(..., PCM1681_SRST)
      regcache_cache_bypass(..., false)
    regcache_sync()                       <-- overwrites with stale cache

The kcontrol write returns success to userspace, bypasses the cache, is
wiped by PCM1681_SRST, and is then overwritten with the old cached value.
Using regmap_multi_reg_write_bypassed() (or a codec mutex) would keep the
bypassed write atomic with respect to other regmap callers.

[ ... ]

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

[Severity: Medium]
Does clearing cache_only before the settle delay and polling loop expose
concurrent ALSA mixer writes to the same failed-write cache eviction?

Clearing regcache_cache_only() opens an up to ~115 ms window across
pcm1681_sck_delay(), read_poll_timeout(), and the post-SRST delay in
pcm1681_soft_reset() while the DAC is still unresponsive or resetting:

CPU0                                CPU1
pcm1681_hw_params()                 snd_ctl_elem_write()
  pcm1681_ensure_ready()              soc_put_volsw()
    regcache_cache_only(..., false)
    pcm1681_sck_delay(...)
                                        regmap_update_bits()
                                          _regmap_raw_write_impl()
                                            <-- I2C NACK, drops cache entry

When that write fails on I2C, _regmap_raw_write_impl() calls
map->cache_ops->drop() on [reg, reg + 1].  Even if pcm1681_ensure_ready()
later succeeds, regcache_sync() skips the evicted registers and leaves the
DAC at its 0xff (0 dB) reset defaults; if readiness fails and restores
cache-only mode, later reads of those registers fail with -EBUSY.  Probing
while still in cache-only mode (for example via regmap_read_bypassed())
and serializing the reset/sync sequence against control writes would close
this window.

[ ... ]

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

[Severity: Medium]
Should pcm1681_i2c_probe() pass wait = false, or avoid polling here, to
prevent a synchronous boot-probe stall?

On Qualcomm q6prm boards where sck is initialized to 19.2 MHz but not
driven at probe, calling pcm1681_ensure_ready(..., true) sleeps 3.4 ms and
then polls for the full 50 ms timeout, generating roughly 50 to 200 NACKed
I2C reads before discarding the error (where probe previously slept 3.4 ms
with no I2C traffic).

On boards without an sck DT clock, falling back to PCM1681_SCK_MIN_RATE in
pcm1681_sck_delay() also adds a 65 ms delay to probe even when the DAC is
already ready (114 ms if unclocked).  Contrary to the comment on
PCM1681_SCK_MIN_RATE, that 65 ms delay also applies in pcm1681_hw_params()
on every stream start after runtime autosuspend whenever clk_get_rate()
returns 0.

[ ... ]

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

[Severity: Medium]
Can priv->ready stay true and regmap stay out of cache-only mode after the
host CPU DAI has already gated sck?

When a stream closes, soc_pcm_clean() calls snd_soc_dai_shutdown() on the
CPU DAI (tearing down the audio interface and stopping sck in
q6i2s_lpass_dai_shutdown()) before dropping the codec's runtime PM
reference via pm_runtime_put_autosuspend().  Because pcm1681_i2c_probe()
uses a 100 ms autosuspend delay (and .use_pmdown_time = 1 can extend this
to 5.1 s on non-DPCM links), pcm1681_runtime_suspend() does not run right
away.

During that window, a mixer write hits the unclocked DAC and fails with
-ENXIO, dropping the register from the cache.  A back-to-back stream
opened before autosuspend fires hits if (priv->ready) return 0 in
pcm1681_ensure_ready(), skipping settling, pcm1681_soft_reset(), and
regcache_sync(), so lost DAC registers remain at reset defaults.
Re-entering cache-only mode and clearing priv->ready in a DAI .shutdown
callback would cover the gap.

[ ... ]

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

[Severity: High]
Does skipping the wait here leave an active playback stream stuck in
cache-only mode across system suspend and resume?

pcm1681_pm_ops uses DEFINE_RUNTIME_DEV_PM_OPS(), so system suspend runs
pcm1681_runtime_suspend() (setting cache_only and priv->ready = false) and
system resume runs pcm1681_runtime_resume().  If sck is not running yet on
resume, or the DAC needs reset settling after sleep,
pcm1681_ensure_ready(..., false) fails and leaves the regmap in cache-only
mode.

On resume, snd_soc_card_resume_deferred() unmutes active DACs and
userspace recovers SNDRV_PCM_STATE_SUSPENDED via snd_pcm_prepare() ->
__soc_pcm_prepare() without calling pcm1681_hw_params().  Because
pcm1681_dai_ops has no .prepare callback and pcm1681_mute() does not call
pcm1681_ensure_ready(), the resumed stream never leaves cache-only mode
and never syncs format, attenuation, or unmute writes to the DAC until the
stream is closed and reopened.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261204173012.3644350-1-mohammad.rafi.shaik@oss.qualcomm.com?part=1

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