Re: [PATCH v3 2/3] regmap: mask values to val_bits on write

From: Andy Shevchenko

Date: Thu Dec 03 2026 - 05:09:03 EST


On Sun, Nov 29, 2026 at 12:18:06PM +0000, Raffaele Tranfaglia via B4 Relay wrote:
>
> regmap_write() takes an unsigned int, also for regmaps with val_bits
> smaller than 32. What happens to the bits above val_bits depends on the
> bus and on the cache type:
>
> - buses with a raw write() go through the value formatters, which drop
> the bits that do not fit in the formatted value;
> - buses and users with only reg_write() get the value unmodified. Some
> of them, like the SMBus backends of regmap-i2c, then return -EINVAL;
> - the cache may store a value that differs from the one written to the
> device.
>
> For example the adux1020 driver writes ~u16, which is promoted to int,
> to a 16-bit register. This works on I2C adapters and fails with -EINVAL
> on SMBus-only ones.
>
> Mask the value to val_bits in _regmap_write(), before updating the
> cache, so that all buses and cache types behave the same. Do the same
> for the values cached by _regmap_multi_reg_write(), which does not use
> _regmap_write() on devices supporting multi register writes.
>
> val_bits is documented as mandatory, but some users providing their own
> reg_read() and reg_write() leave it at 0, for example because their
> registers have different sizes. Do not mask values for these.

...

> + /* Make sure that the cache and the device see the same value */
> + val &= map->format.val_mask;

...

> + * If the regmap_config sets val_bits, any bits of @val above it are ignored.

s/are/will be/ ?

...

> for (i = 0; i < num_regs; i++) {
> - unsigned int val = regs[i].def;
> + unsigned int val = regs[i].def & map->format.val_mask;
> unsigned int reg = regs[i].reg;

I believe I asked for a blank line here and split of the AND operator to make
it look like the above.

unsigned int val = regs[i].def;
unsigned int reg = regs[i].reg;

val &= map->format.val_mask;

ret = regcache_write(map, reg, val);


Blank line is style requirement to split definitions from the code.

> ret = regcache_write(map, reg, val);

--
With Best Regards,
Andy Shevchenko