Linux Sound subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Mohammad Rafi Shaik" <mohammad.rafi.shaik@oss.qualcomm.com>
Cc: linux-sound@vger.kernel.org
Subject: Re: [PATCH] ASoC: pcm1681: Establish contact with the DAC lazily
Date: Fri, 04 Dec 2026 18:02:03 +0000	[thread overview]
Message-ID: <sashiko-outbox-250361@kernel.org> (raw)
In-Reply-To: <20261204173012.3644350-1-mohammad.rafi.shaik@oss.qualcomm.com>

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

      reply	other threads:[~2026-12-04 18:02 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-250361@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-sound@vger.kernel.org \
    --cc=mohammad.rafi.shaik@oss.qualcomm.com \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox