Re: [PATCH] iio: potentiostat: lmp91000: Handle configuration write errors

From: Andy Shevchenko

Date: Thu Nov 12 2026 - 11:10:45 EST


On Thu, Nov 12, 2026 at 03:17:47PM +0800, Zhu Jun 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, and report a relock failure as well.

...

> static int lmp91000_read_config(struct lmp91000_data *data)
> {
> struct device *dev = data->dev;
> unsigned int reg, val;
> - int i, ret;
> + int i, ret, lock_ret;

While at it, it's better to split semantically different variables:

int ret, lock_ret;
int i;

> + ret = regmap_write(data->regmap, LMP91000_REG_TIACN, reg);
> + if (!ret)
> + ret = regmap_write(data->regmap, LMP91000_REG_REFCN,
> + LMP91000_REG_REFCN_EXT_REF | LMP91000_REG_REFCN_50_ZERO);
> +
> + /* Restore write protection even if programming the registers failed. */
> + lock_ret = regmap_write(data->regmap, LMP91000_REG_LOCK, 1);
> +
> + return ret ? ret : lock_ret;

It doesn't use any guard()() or so, hence just make a goto instead
(and use Elvis):

ret = regmap_write(data->regmap, LMP91000_REG_TIACN, reg);
if (ret)
goto restore_lock;

ret = regmap_write(data->regmap, LMP91000_REG_REFCN,
LMP91000_REG_REFCN_EXT_REF | LMP91000_REG_REFCN_50_ZERO);

restore_lock:

/* Restore write protection even if programming the registers failed. */
lock_ret = regmap_write(data->regmap, LMP91000_REG_LOCK, 1);

return ret ?: lock_ret;

> }

Alternatively, wrap the existing function (this will make code cleaner in some
maintainer's opinion).

...

In any case there is a question how may the caller distinguish the error from
register write and from the lock restoration? And what will it do in either of
the cases?

--
With Best Regards,
Andy Shevchenko