Re: [PATCH RFC] regmap: kunit: add tests for values wider than val_bits
From: Andy Shevchenko
Date: Wed Nov 18 2026 - 04:05:25 EST
On Tue, Nov 17, 2026 at 07:55:14AM +0000, Raffaele Tranfaglia via B4 Relay wrote:
Thanks for starting this discussion! My comments below.
> Add two test cases writing 0xffffff0f with regmap_write() to a regmap
> with val_bits = 16:
>
> - write_wider_than_val_bits uses the RAM regmap, a bus that only
> provides reg_write()/reg_read();
> - raw_write_wider_than_val_bits uses the raw RAM regmap, which goes
> through the value formatters.
>
> Both expect that only the low 16 bits reach the bus and can be read
> back. The raw test passes, since regmap_format_16_*() only store 16
> bits. The RAM test currently fails: _regmap_bus_reg_write() hands the
> value to bus->reg_write() unmodified, so the bus sees 0xffffff0f. With
> the rbtree cache the value is in addition truncated in the cache only,
> so the cache and the bus disagree.
It means we can't enable this test cases without addressing the underneath
implementation(s) of the regmap. I think the proper solution is to cut upper
bits on the regmap level, so the cache and HW will see it as expected.
> Signed-off-by: Raffaele Tranfaglia <raffaele.tranfaglia@xxxxxxxxx>
> ---
> regmap_write() takes an unsigned int, also for regmaps with
> val_bits < 32. What happens to the bits above val_bits currently
> depends on the bus and on the cache type:
>
> - buses with raw write() go through the value formatters, which
> silently drop the upper bits;
> - buses with only reg_write() get the value unmodified. regmap-i2c's
> SMBus backends and regmap-fsi then return -EINVAL, others (like
> regmap-ram here) take the full value;
To make the users' lives easier we should not move the burden on their
shoulders to carefully cut the upper bits all the time it might be required.
I see the only solution is to fix either globally in regmap core or in each
bus and document that somewhere that developers for the new regmap busses
won't repeat the same mistakes.
> - with a reg_write() bus and the rbtree cache, the cache stores the
> truncated value while the bus got the full one.
>
> This was found in the adux1020 IIO driver, which writes ~u16 (promoted
> to int) to a 16-bit register. It works on I2C adapters and fails with
> -EINVAL on SMBus-only ones [1]. I am raising it here as other drivers
> can hit the same thing.
>
> The tests in this patch expect masking to val_bits on both kinds of
> buses, so the reg_write one fails today. This is only to show the
> difference: should regmap_write() mask the value to val_bits for
> reg_write() buses, or should it reject such values consistently for all
> buses? I can send a patch for whichever is preferred and adjust the
> tests accordingly.
...
> +static void raw_write_wider_than_val_bits(struct kunit *test)
> +{
> + struct regmap *map;
> + struct regmap_config config;
> + struct regmap_ram_data *data;
> + u16 *hw_buf;
> + unsigned int rval;
Reversed xmas tree order?
> + config = raw_regmap_config;
> +
> + map = gen_raw_regmap(test, &config, &data);
> + KUNIT_ASSERT_FALSE(test, IS_ERR(map));
> + if (IS_ERR(map))
> + return;
> +
> + hw_buf = (u16 *)data->vals;
> +
> + /* Write a value with bits set above val_bits */
> + KUNIT_EXPECT_EQ(test, 0, regmap_write(map, 0, 0xffffff0f));
> +
> + /* The "hardware" should only have seen the low val_bits bits */
> + if (config.val_format_endian == REGMAP_ENDIAN_BIG)
> + KUNIT_EXPECT_EQ(test, 0xff0f,
> + be16_to_cpu((__force __be16)hw_buf[0]));
Do we really need those explicit castings? I think they are already inside
the byteorder macros. Also we have be16_to_cpup() and le16_to_cpup() which
might help.
> + else
> + KUNIT_EXPECT_EQ(test, 0xff0f,
> + le16_to_cpu((__force __le16)hw_buf[0]));
Ditto.
> + /* And that is what we should read back */
> + KUNIT_EXPECT_EQ(test, 0, regmap_read(map, 0, &rval));
> + KUNIT_EXPECT_EQ(test, 0xff0f, rval);
> +}
--
With Best Regards,
Andy Shevchenko