Re: [PATCH] eeprom: at24: wait for write cycle before releasing runtime PM

From: Kristian Otnes (kotnes)

Date: Sun Nov 29 2026 - 09:12:06 EST



On Sun, 29 Nov 2026 13:17:49 +0100, Greg KH wrote:
> Then please quote the email properly, don't expect us to be able to look
> it up somehow. Everything you need us to respond to should be in the
> email itself.

My apologies again. I really don't want to disturb you unnecessary with
this detail. Hope the following is sufficient.

On Thu, 26 Nov 2026 01:15:35 +0100, Daniel Svendsen wrote:
> at24_regmap_write() returns as soon as the chip has ACKed the
> transfer, but the EEPROM only programs the data afterwards, during its
> internal write cycle (typically up to 5 ms). For all but the last
> chunk, the following write is NACKed and retried until that cycle has
> finished. After the last chunk nothing waits, and at24_write() drops
> its runtime PM reference right away. When vcc is controlled through
> runtime PM and has no other users, at24_suspend() disables the
> regulator in the middle of the write cycle and the last chunk is lost,
> while the write is reported as successful.
>
> This was seen on a board where the EEPROM supply is a switchable
> regulator used only by at24: 32-byte writes with a 16-byte page size
> programmed just the first 16 bytes.
>
> Wait for the write cycle to finish before releasing runtime PM by
> doing a one-byte read of the last written address. at24_regmap_read()
> retries while the chip NACKs, up to write_timeout.
>
> Fixes: cd5676db0574 ("misc: eeprom: at24: support pm_runtime control")
> Cc: stable@xxxxxxxxxxxxxxx
> Signed-off-by: Daniel Svendsen <das@xxxxxxxxxx>
> ---
> drivers/nvmem/at24.c | 9 ++++++++-
> 1 file changed, 8 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/nvmem/at24.c b/drivers/nvmem/at24.c
> index b9db13cc3e33..03cd83e5caff 100644
> --- a/drivers/nvmem/at24.c
> +++ b/drivers/nvmem/at24.c
> @@ -482,6 +482,7 @@ static int at24_write(void *priv, unsigned int off, void *val, size_t count)
> struct at24_data *at24;
> struct device *dev;
> char *buf = val;
> + char dummy;
> int ret;
>
> at24 = priv;
> @@ -514,11 +515,17 @@ static int at24_write(void *priv, unsigned int off, void *val, size_t count)
> count -= ret;
> }
>
> + /*
> + * Wait for the last chunk's internal write cycle to finish before
> + * dropping the runtime PM reference, which may switch off vcc.
> + */
> + ret = at24_regmap_read(at24, &dummy, off - 1, 1);
> +
> mutex_unlock(&at24->lock);
>
> pm_runtime_put(dev);
>
> - return 0;
> + return ret < 0 ? ret : 0;
> }
>
> static int at24_make_dummy_client(struct at24_data *at24, unsigned int index,

The patch reads the last written byte before releasing the runtime PM
reference. This uses the driver's existing retry mechanism to wait for
the EEPROM to respond.

My question is about the behavior and trade-off, rather than an
objection to fixing the reported data-loss case.

The driver is based on the principle of retries prior to write or
read in case the EEPROM is still busy with a previous write cycle.

One possible reason for this behavior is to avoid making the
caller wait for the final write cycle. This may have been an
intentional latency optimization. The proposed change would
keep at24_write() from returning successfully until the final write
cycle completes. Is that an acceptable trade-off for the suggested
patch?

Could this failure instead be addressed in the runtime PM handling?
Since it was introduced by later adding runtime PM support, autosuspend
could preserve the driver's existing write behavior. For example,
the autosuspend delay could be set to at24_write_timeout, with
at24_write() using pm_runtime_put_autosuspend(). If this seems
preferable, that alternative would require a new patch proposal
with additional runtime PM changes to configure and use autosuspend.

Thanks,
Kristian