* [PATCH v3] iio: potentiostat: lmp91000: Handle configuration write errors
@ 2026-11-13 8:25 Zhu Jun
2026-11-13 8:41 ` Andy Shevchenko
0 siblings, 1 reply; 7+ messages in thread
From: Zhu Jun @ 2026-11-13 8:25 UTC (permalink / raw)
To: jic23; +Cc: dlechner, nuno.sa, andy, linux-iio, linux-kernel, zhujun2
lmp91000_read_config() ignores errors when unlocking and programming
the TIA and reference registers. Probe can then succeed even though
the hardware configuration does not match the parameters used to
interpret measurements.
Check the configuration writes and return the first error. After a
successful unlock, attempt to restore write protection even if a
configuration write fails. Return a relock error when both configuration
writes succeeded.
lmp91000_probe() returns either error before registering the IIO device,
so the caller does not need to distinguish their source. Preserve the
configuration error if restoring write protection also fails.
Fixes: 67e17300dc1d ("iio: potentiostat: add LMP91000 support")
Signed-off-by: Zhu Jun <zhujun2@cmss.chinamobile.com>
---
Changes in v3:
- Declare the loop counter separately from the return code (Andy Shevchenko).
- Explain how probe handles configuration and relock failures.
Changes in v2:
- Use an explicit error path to restore write protection and return the
original configuration error, as suggested by Jonathan Cameron.
- Return the final lock write result directly on the successful path.
drivers/iio/potentiostat/lmp91000.c | 25 +++++++++++++++++++------
1 file changed, 19 insertions(+), 6 deletions(-)
diff --git a/drivers/iio/potentiostat/lmp91000.c b/drivers/iio/potentiostat/lmp91000.c
index 00cb3fea97ca..1e07917ff76a 100644
--- a/drivers/iio/potentiostat/lmp91000.c
+++ b/drivers/iio/potentiostat/lmp91000.c
@@ -205,7 +205,8 @@ static int lmp91000_read_config(struct lmp91000_data *data)
{
struct device *dev = data->dev;
unsigned int reg, val;
- int i, ret;
+ int ret;
+ int i;
ret = device_property_read_u32(dev, "ti,tia-gain-ohm", &val);
if (ret) {
@@ -250,13 +251,25 @@ static int lmp91000_read_config(struct lmp91000_data *data)
return ret;
}
- regmap_write(data->regmap, LMP91000_REG_LOCK, 0);
- regmap_write(data->regmap, LMP91000_REG_TIACN, reg);
- regmap_write(data->regmap, LMP91000_REG_REFCN,
- LMP91000_REG_REFCN_EXT_REF | LMP91000_REG_REFCN_50_ZERO);
+ ret = regmap_write(data->regmap, LMP91000_REG_LOCK, 0);
+ if (ret)
+ return ret;
+
+ ret = regmap_write(data->regmap, LMP91000_REG_TIACN, reg);
+ if (ret)
+ goto relock;
+
+ ret = regmap_write(data->regmap, LMP91000_REG_REFCN,
+ LMP91000_REG_REFCN_EXT_REF | LMP91000_REG_REFCN_50_ZERO);
+ if (ret)
+ goto relock;
+
+ return regmap_write(data->regmap, LMP91000_REG_LOCK, 1);
+
+relock:
regmap_write(data->regmap, LMP91000_REG_LOCK, 1);
- return 0;
+ return ret;
}
static int lmp91000_buffer_cb(const void *val, void *private)
--
2.51.0
^ permalink raw reply [flat|nested] 7+ messages in thread* Re: [PATCH v3] iio: potentiostat: lmp91000: Handle configuration write errors
2026-11-13 8:25 [PATCH v3] iio: potentiostat: lmp91000: Handle configuration write errors Zhu Jun
@ 2026-11-13 8:41 ` Andy Shevchenko
2026-11-13 8:47 ` Andy Shevchenko
0 siblings, 1 reply; 7+ messages in thread
From: Andy Shevchenko @ 2026-11-13 8:41 UTC (permalink / raw)
To: Zhu Jun; +Cc: jic23, dlechner, nuno.sa, andy, linux-iio, linux-kernel
On Fri, Nov 13, 2026 at 10:25 AM Zhu Jun <zhujun2@cmss.chinamobile.com> wrote:
>
> lmp91000_read_config() ignores errors when unlocking and programming
> the TIA and reference registers. Probe can then succeed even though
> the hardware configuration does not match the parameters used to
> interpret measurements.
>
> Check the configuration writes and return the first error. After a
> successful unlock, attempt to restore write protection even if a
> configuration write fails. Return a relock error when both configuration
> writes succeeded.
>
> lmp91000_probe() returns either error before registering the IIO device,
> so the caller does not need to distinguish their source. Preserve the
> configuration error if restoring write protection also fails.
...
> Changes in v3:
> - Declare the loop counter separately from the return code (Andy Shevchenko).
Now this makes no sense as you haven't added any new variable.
> - Explain how probe handles configuration and relock failures.
> - regmap_write(data->regmap, LMP91000_REG_LOCK, 0);
> - regmap_write(data->regmap, LMP91000_REG_TIACN, reg);
> - regmap_write(data->regmap, LMP91000_REG_REFCN,
> - LMP91000_REG_REFCN_EXT_REF | LMP91000_REG_REFCN_50_ZERO);
> + ret = regmap_write(data->regmap, LMP91000_REG_LOCK, 0);
> + if (ret)
> + return ret;
> +
> + ret = regmap_write(data->regmap, LMP91000_REG_TIACN, reg);
> + if (ret)
> + goto relock;
> +
> + ret = regmap_write(data->regmap, LMP91000_REG_REFCN,
> + LMP91000_REG_REFCN_EXT_REF | LMP91000_REG_REFCN_50_ZERO);
> + if (ret)
> + goto relock;
> +
> + return regmap_write(data->regmap, LMP91000_REG_LOCK, 1);
> +
> +relock:
> regmap_write(data->regmap, LMP91000_REG_LOCK, 1);
What's the point of duplicating this?
Before label you can easily do
ret = 0;
relock:
...
>
> - return 0;
> + return ret;
> }
--
With Best Regards,
Andy Shevchenko
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v3] iio: potentiostat: lmp91000: Handle configuration write errors
2026-11-13 8:41 ` Andy Shevchenko
@ 2026-11-13 8:47 ` Andy Shevchenko
2026-11-14 1:41 ` Jonathan Cameron
0 siblings, 1 reply; 7+ messages in thread
From: Andy Shevchenko @ 2026-11-13 8:47 UTC (permalink / raw)
To: Zhu Jun; +Cc: jic23, dlechner, nuno.sa, andy, linux-iio, linux-kernel
On Fri, Nov 13, 2026 at 10:41 AM Andy Shevchenko
<andy.shevchenko@gmail.com> wrote:
> On Fri, Nov 13, 2026 at 10:25 AM Zhu Jun <zhujun2@cmss.chinamobile.com> wrote:
...
> > Check the configuration writes and return the first error. After a
> > successful unlock, attempt to restore write protection even if a
> > configuration write fails. Return a relock error when both configuration
> > writes succeeded.
> >
> > lmp91000_probe() returns either error before registering the IIO device,
> > so the caller does not need to distinguish their source. Preserve the
> > configuration error if restoring write protection also fails.
Btw, this is still unclear. See below.
...
> > - regmap_write(data->regmap, LMP91000_REG_LOCK, 0);
> > - regmap_write(data->regmap, LMP91000_REG_TIACN, reg);
> > - regmap_write(data->regmap, LMP91000_REG_REFCN,
> > - LMP91000_REG_REFCN_EXT_REF | LMP91000_REG_REFCN_50_ZERO);
> > + ret = regmap_write(data->regmap, LMP91000_REG_LOCK, 0);
> > + if (ret)
> > + return ret;
> > +
> > + ret = regmap_write(data->regmap, LMP91000_REG_TIACN, reg);
> > + if (ret)
> > + goto relock;
> > +
> > + ret = regmap_write(data->regmap, LMP91000_REG_REFCN,
> > + LMP91000_REG_REFCN_EXT_REF | LMP91000_REG_REFCN_50_ZERO);
> > + if (ret)
> > + goto relock;
> > +
> > + return regmap_write(data->regmap, LMP91000_REG_LOCK, 1);
> > +
> > +relock:
> > regmap_write(data->regmap, LMP91000_REG_LOCK, 1);
>
> What's the point of duplicating this?
> Before label you can easily do
>
> ret = 0;
>
> relock:
> ...
Ah, okay, I see that the relock error is returned only on success of
the configuration update. Why is this inconsistency? Really, you need
to elaborate first on what the caller is suppose to do when
1) unlock failed;
2) unlock was successful, but configuration was failed;
3) unlock and configuration was successful, but relock failed.
In all of these cases the user gets an error, but it doesn't know
which stage was failed. What is the caller supposed to do?
No need to send a new patch, let's keep discussion here to understand
the code flow better.
> > - return 0;
> > + return ret;
> > }
--
With Best Regards,
Andy Shevchenko
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v3] iio: potentiostat: lmp91000: Handle configuration write errors
2026-11-13 8:47 ` Andy Shevchenko
@ 2026-11-14 1:41 ` Jonathan Cameron
2026-11-16 9:53 ` Andy Shevchenko
0 siblings, 1 reply; 7+ messages in thread
From: Jonathan Cameron @ 2026-11-14 1:41 UTC (permalink / raw)
To: Andy Shevchenko; +Cc: Zhu Jun, dlechner, nuno.sa, andy, linux-iio, linux-kernel
On Fri, 13 Nov 2026 10:47:32 +0200
Andy Shevchenko <andy.shevchenko@gmail.com> wrote:
> On Fri, Nov 13, 2026 at 10:41 AM Andy Shevchenko
> <andy.shevchenko@gmail.com> wrote:
> > On Fri, Nov 13, 2026 at 10:25 AM Zhu Jun <zhujun2@cmss.chinamobile.com> wrote:
>
> ...
>
> > > Check the configuration writes and return the first error. After a
> > > successful unlock, attempt to restore write protection even if a
> > > configuration write fails. Return a relock error when both configuration
> > > writes succeeded.
> > >
> > > lmp91000_probe() returns either error before registering the IIO device,
> > > so the caller does not need to distinguish their source. Preserve the
> > > configuration error if restoring write protection also fails.
>
> Btw, this is still unclear. See below.
>
> ...
>
> > > - regmap_write(data->regmap, LMP91000_REG_LOCK, 0);
> > > - regmap_write(data->regmap, LMP91000_REG_TIACN, reg);
> > > - regmap_write(data->regmap, LMP91000_REG_REFCN,
> > > - LMP91000_REG_REFCN_EXT_REF | LMP91000_REG_REFCN_50_ZERO);
> > > + ret = regmap_write(data->regmap, LMP91000_REG_LOCK, 0);
> > > + if (ret)
> > > + return ret;
> > > +
> > > + ret = regmap_write(data->regmap, LMP91000_REG_TIACN, reg);
> > > + if (ret)
> > > + goto relock;
> > > +
> > > + ret = regmap_write(data->regmap, LMP91000_REG_REFCN,
> > > + LMP91000_REG_REFCN_EXT_REF | LMP91000_REG_REFCN_50_ZERO);
> > > + if (ret)
> > > + goto relock;
> > > +
> > > + return regmap_write(data->regmap, LMP91000_REG_LOCK, 1);
> > > +
> > > +relock:
> > > regmap_write(data->regmap, LMP91000_REG_LOCK, 1);
> >
> > What's the point of duplicating this?
> > Before label you can easily do
> >
> > ret = 0;
> >
> > relock:
> > ...
>
> Ah, okay, I see that the relock error is returned only on success of
> the configuration update. Why is this inconsistency? Really, you need
> to elaborate first on what the caller is suppose to do when
> 1) unlock failed;
> 2) unlock was successful, but configuration was failed;
> 3) unlock and configuration was successful, but relock failed.
>
> In all of these cases the user gets an error, but it doesn't know
> which stage was failed. What is the caller supposed to do?
>
> No need to send a new patch, let's keep discussion here to understand
> the code flow better.
I think I requested this flow. I didn't like v1 doing some dance
with ret2 and only return that if ret wasn't set. Argument of the patch is
that we must make sure to unlock even on error.
>
> > > - return 0;
> > > + return ret;
> > > }
>
>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v3] iio: potentiostat: lmp91000: Handle configuration write errors
2026-11-14 1:41 ` Jonathan Cameron
@ 2026-11-16 9:53 ` Andy Shevchenko
2026-11-19 3:21 ` Zhu Jun
0 siblings, 1 reply; 7+ messages in thread
From: Andy Shevchenko @ 2026-11-16 9:53 UTC (permalink / raw)
To: Jonathan Cameron
Cc: Andy Shevchenko, Zhu Jun, dlechner, nuno.sa, andy, linux-iio,
linux-kernel
On Sat, Nov 14, 2026 at 01:41:49AM +0000, Jonathan Cameron wrote:
> On Fri, 13 Nov 2026 10:47:32 +0200
> Andy Shevchenko <andy.shevchenko@gmail.com> wrote:
> > On Fri, Nov 13, 2026 at 10:41 AM Andy Shevchenko
> > <andy.shevchenko@gmail.com> wrote:
> > > On Fri, Nov 13, 2026 at 10:25 AM Zhu Jun <zhujun2@cmss.chinamobile.com> wrote:
...
> > > > Check the configuration writes and return the first error. After a
> > > > successful unlock, attempt to restore write protection even if a
> > > > configuration write fails. Return a relock error when both configuration
> > > > writes succeeded.
> > > >
> > > > lmp91000_probe() returns either error before registering the IIO device,
> > > > so the caller does not need to distinguish their source. Preserve the
> > > > configuration error if restoring write protection also fails.
> >
> > Btw, this is still unclear. See below.
...
> > > > - regmap_write(data->regmap, LMP91000_REG_LOCK, 0);
> > > > - regmap_write(data->regmap, LMP91000_REG_TIACN, reg);
> > > > - regmap_write(data->regmap, LMP91000_REG_REFCN,
> > > > - LMP91000_REG_REFCN_EXT_REF | LMP91000_REG_REFCN_50_ZERO);
> > > > + ret = regmap_write(data->regmap, LMP91000_REG_LOCK, 0);
> > > > + if (ret)
> > > > + return ret;
> > > > +
> > > > + ret = regmap_write(data->regmap, LMP91000_REG_TIACN, reg);
> > > > + if (ret)
> > > > + goto relock;
> > > > +
> > > > + ret = regmap_write(data->regmap, LMP91000_REG_REFCN,
> > > > + LMP91000_REG_REFCN_EXT_REF | LMP91000_REG_REFCN_50_ZERO);
> > > > + if (ret)
> > > > + goto relock;
> > > > +
> > > > + return regmap_write(data->regmap, LMP91000_REG_LOCK, 1);
> > > > +
> > > > +relock:
> > > > regmap_write(data->regmap, LMP91000_REG_LOCK, 1);
> > >
> > > What's the point of duplicating this?
> > > Before label you can easily do
> > >
> > > ret = 0;
> > >
> > > relock:
> > > ...
> >
> > Ah, okay, I see that the relock error is returned only on success of
> > the configuration update. Why is this inconsistency? Really, you need
> > to elaborate first on what the caller is suppose to do when
> > 1) unlock failed;
> > 2) unlock was successful, but configuration was failed;
> > 3) unlock and configuration was successful, but relock failed.
> >
> > In all of these cases the user gets an error, but it doesn't know
> > which stage was failed. What is the caller supposed to do?
> >
> > No need to send a new patch, let's keep discussion here to understand
> > the code flow better.
>
> I think I requested this flow. I didn't like v1 doing some dance
> with ret2 and only return that if ret wasn't set. Argument of the patch is
> that we must make sure to unlock even on error.
Right, but I think this is a slippery slope to leave device in the inconsistent
state and caller will not know if the locking failed or actual configuration.
How is HW being recovered from the inconsistent state(s)? That's basically my
main concern.
Note, I haven't checked with datasheet, so I have no clue how critical those
erroneous states are. If it's natural flow on the next iteration that clears
this problem, then we may relax on the error checking to begin with.
> > > > - return 0;
> > > > + return ret;
> > > > }
--
With Best Regards,
Andy Shevchenko
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v3] iio: potentiostat: lmp91000: Handle configuration write errors
2026-11-16 9:53 ` Andy Shevchenko
@ 2026-11-19 3:21 ` Zhu Jun
2026-11-19 10:06 ` Andy Shevchenko
0 siblings, 1 reply; 7+ messages in thread
From: Zhu Jun @ 2026-11-19 3:21 UTC (permalink / raw)
To: andriy.shevchenko
Cc: jic23, andy.shevchenko, andy, dlechner, nuno.sa, linux-iio,
linux-kernel, zhujun2
On Mon, Nov 16, 2026, Andy Shevchenko wrote:
> How is HW being recovered from the inconsistent state(s)?
Hi Andy,
v3 has no hardware recovery step. The LOCK bit only controls write
access to TIACN and REFCN (datasheet section 7.6.2). Setting it again
leaves the register values as they are.
If the unlock write fails, we stop before writing either configuration
register. The resulting lock state is unconfirmed. If a configuration
write fails, the chip may be partly configured. We try to restore write
protection and return the configuration error. If both configuration
writes succeed and the relock write fails, we return the relock error.
In that case the configuration writes succeeded, but write protection
is unconfirmed. If both a configuration write and the relock fail, v3
only reports the configuration error.
In all three cases probe exits before registering the IIO device. If
the device is probed again and I2C is working, the same sequence writes
LOCK=0, rewrites both registers and writes LOCK=1. That would restore
these register settings regardless of the previous lock state. The
driver does not schedule a retry or reset the chip after these errors,
and I have not verified recovery on hardware.
Datasheet: https://www.ti.com/lit/ds/symlink/lmp91000.pdf
Thanks,
Zhu Jun
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v3] iio: potentiostat: lmp91000: Handle configuration write errors
2026-11-19 3:21 ` Zhu Jun
@ 2026-11-19 10:06 ` Andy Shevchenko
0 siblings, 0 replies; 7+ messages in thread
From: Andy Shevchenko @ 2026-11-19 10:06 UTC (permalink / raw)
To: Zhu Jun
Cc: jic23, andy.shevchenko, andy, dlechner, nuno.sa, linux-iio, linux-kernel
On Thu, Nov 19, 2026 at 11:21:55AM +0800, Zhu Jun wrote:
> On Mon, Nov 16, 2026, Andy Shevchenko wrote:
> > How is HW being recovered from the inconsistent state(s)?
>
> v3 has no hardware recovery step. The LOCK bit only controls write
> access to TIACN and REFCN (datasheet section 7.6.2). Setting it again
> leaves the register values as they are.
>
> If the unlock write fails, we stop before writing either configuration
> register. The resulting lock state is unconfirmed. If a configuration
> write fails, the chip may be partly configured. We try to restore write
> protection and return the configuration error. If both configuration
> writes succeed and the relock write fails, we return the relock error.
> In that case the configuration writes succeeded, but write protection
> is unconfirmed. If both a configuration write and the relock fail, v3
> only reports the configuration error.
Right, in each of these cases what will be the outcome to the user?
> In all three cases probe exits before registering the IIO device. If
> the device is probed again and I2C is working, the same sequence writes
> LOCK=0, rewrites both registers and writes LOCK=1. That would restore
> these register settings regardless of the previous lock state.
This is a recovery mechanism! But specific question is the above: what
will happen with HW in each of these states when we try to re-probe the
driver?
> The driver does not schedule a retry or reset the chip after these errors,
> and I have not verified recovery on hardware.
>
> Datasheet: https://www.ti.com/lit/ds/symlink/lmp91000.pdf
--
With Best Regards,
Andy Shevchenko
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-11-19 10:06 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-13 8:25 [PATCH v3] iio: potentiostat: lmp91000: Handle configuration write errors Zhu Jun
2026-11-13 8:41 ` Andy Shevchenko
2026-11-13 8:47 ` Andy Shevchenko
2026-11-14 1:41 ` Jonathan Cameron
2026-11-16 9:53 ` Andy Shevchenko
2026-11-19 3:21 ` Zhu Jun
2026-11-19 10:06 ` Andy Shevchenko
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®