regmap: kunit: add tests for values wider than val_bits
Subsystems (3)
RFC PATCH 1/1 regmap: kunit: add tests for values wider than val_bits
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.
---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;
- 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.
I prepared this patch with the help of an AI coding assistant, and reviewed and tested it myself.
[1] https://lore.kernel.org/linux-iio/[email protected]/
--- drivers/base/regmap/regmap-kunit.c | 62 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+)
drivers/base/regmap/regmap-kunit.c +62 -0
@@ -319,6 +319,33 @@ static void basic_read_write(struct kunit *test)KUNIT_EXPECT_EQ (test, config.cache_type ==REGCACHE_NONE , data->read[0 ]); } +static void write_wider_than_val_bits (struct kunit *test) +{ +const struct regmap_test_param *param = test->param_value; +struct regmap *map; +struct regmap_config config; +struct regmap_ram_data *data; +unsigned int rval; + + config = test_regmap_config; + config.val_bits =16 ; + + map =gen_regmap (test, &config, &data); +KUNIT_ASSERT_FALSE (test,IS_ERR (map)); +if (IS_ERR (map)) +return ; + +/* Write a value with bits set above val_bits */ +KUNIT_EXPECT_EQ (test,0 ,regmap_write (map, param->from_reg,0xffffff0f )); + +/* The bus should only have seen the low val_bits bits */ +KUNIT_EXPECT_EQ (test,0xff0f , data->vals[param->from_reg]); + +/* And that is what we should read back */ +KUNIT_EXPECT_EQ (test,0 ,regmap_read (map, param->from_reg, &rval)); +KUNIT_EXPECT_EQ (test,0xff0f , rval); +} +static void bulk_write (struct kunit *test) {struct regmap *map;
@@ -1984,6 +2011,39 @@ static void raw_write_read_single(struct kunit *test)KUNIT_EXPECT_EQ (test, val, rval); } +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; + + 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 ])); +else +KUNIT_EXPECT_EQ (test,0xff0f , +le16_to_cpu ((__force __le16 )hw_buf[0 ])); + +/* And that is what we should read back */ +KUNIT_EXPECT_EQ (test,0 ,regmap_read (map,0 , &rval)); +KUNIT_EXPECT_EQ (test,0xff0f , rval); +} +static void raw_write (struct kunit *test) {struct regmap *map;
@@ -2229,6 +2289,7 @@ static void raw_ranges(struct kunit *test)static struct kunit_case regmap_test_cases[] = {KUNIT_CASE_PARAM (basic_read_write, regcache_types_gen_params), +KUNIT_CASE_PARAM (write_wider_than_val_bits, regcache_types_gen_params),KUNIT_CASE_PARAM (read_bypassed, real_cache_types_gen_params),KUNIT_CASE_PARAM (read_bypassed_volatile, real_cache_types_gen_params),KUNIT_CASE_PARAM (bulk_write, regcache_types_gen_params),
@@ -2265,6 +2326,7 @@ static struct kunit_case regmap_test_cases[] = {KUNIT_CASE_PARAM (raw_read_defaults_single, raw_test_types_gen_params),KUNIT_CASE_PARAM (raw_read_defaults, raw_test_types_gen_params),KUNIT_CASE_PARAM (raw_write_read_single, raw_test_types_gen_params), +KUNIT_CASE_PARAM (raw_write_wider_than_val_bits, raw_test_types_gen_params),KUNIT_CASE_PARAM (raw_write, raw_test_types_gen_params),KUNIT_CASE_PARAM (raw_noinc_write, raw_test_types_gen_params), KUNIT_CASE_PARAM(raw_sync, raw_test_cache_types_gen_params),
---
base-commit: 666539eecdf4db623f5e3c3e55cf9f2dfad24aa4
change-id: 20261117-regmap-kunit-val-bits-6473305e083b
Best regards,
Signature · 1 line
Raffaele Tranfaglia <[email protected]>
On Tue, Nov 17, 2026 at 07:55:14AM +0000, Raffaele Tranfaglia via B4 Relay wrote:
Thanks for starting this discussion! My comments below.
Quoted 8 lines
Add two test cases writing
0xffffff0fwithregmap_write()to a regmap withval_bits= 16:-
write_wider_than_val_bitsuses the RAM regmap, a bus that only provides reg_write()/reg_read();
-raw_write_wider_than_val_bitsuses the raw RAM regmap, which goes through the value formatters.
Quoted 6 lines
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 tobus->reg_write()unmodified, so the bus sees0xffffff0f. 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.
Quoted 11 lines
Signed-off-by Raffaele Tranfaglia <[email protected]>---
regmap_write()takes an unsigned int, also for regmaps withval_bits< 32. What happens to the bits aboveval_bitscurrently 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 onlyreg_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.
Quoted 14 lines
- 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
-EINVALon 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_bitson both kinds of buses, so thereg_writeone fails today. This is only to show the
difference: shouldregmap_write()mask the value toval_bitsforreg_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.
...
drivers/base/regmap/regmap-kunit.c static void raw_write_read_single(struct kunit *test) · line 2020
Quoted 7 lines
drivers/base/regmap/regmap-kunit.c +7
-0+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?
Quoted 6 lines
drivers/base/regmap/regmap-kunit.c +6
-0+ config = raw_regmap_config; + + map =gen_raw_regmap (test, &config, &data); +KUNIT_ASSERT_FALSE (test,IS_ERR (map)); +if (IS_ERR (map)) +return ;
drivers/base/regmap/regmap-kunit.c static void raw_write_read_single(struct kunit *test) · line 2037
Quoted 10 lines
drivers/base/regmap/regmap-kunit.c +10
-0+ + 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.
drivers/base/regmap/regmap-kunit.c static void raw_write_read_single(struct kunit *test) · line 2040
Quoted 3 lines
drivers/base/regmap/regmap-kunit.c +3
-0+else +KUNIT_EXPECT_EQ (test,0xff0f , +le16_to_cpu ((__force __le16 )hw_buf[0 ]));
Ditto.
Quoted 4 lines
drivers/base/regmap/regmap-kunit.c +4
-0+/* And that is what we should read back */ +KUNIT_EXPECT_EQ (test,0 ,regmap_read (map,0 , &rval)); +KUNIT_EXPECT_EQ (test,0xff0f , rval); +}
Signature · 2 lines
With Best Regards, Andy Shevchenko
Mark Brown
maintainerQuoted 11 lines · Andy Shevchenko
drivers/base/regmap/regmap-kunit.c static void raw_write_read_single(struct kunit *test) · line 2020
Quoted 7 lines · On Tue, Nov 17, 2026 at 07:55:14AM +0000, Raffaele Tranfaglia via B4 Relay…
drivers/base/regmap/regmap-kunit.c +7
-0+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?
I simply do not care about this.
Quoted 9 lines
drivers/base/regmap/regmap-kunit.c static void raw_write_read_single(struct kunit *test) · line 2037
Quoted 5 lines
drivers/base/regmap/regmap-kunit.c +5
-0+ +/* 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()andle16_to_cpup()which might help.
If we do need the casts that probably indicates a problem.
Quoted 7 lines · Mark Brown
Quoted 3 lines · Andy Shevchenko
Do we really need those explicit castings? I think they are already inside the byteorder macros. Also we have
be16_to_cpup()andle16_to_cpup()which might help.If we do need the casts that probably indicates a problem.
Yes, the buffer was declared as u16 * while it holds __be16/__le16 data. I will use be16_to_cpup()/le16_to_cpup() on a void pointer in v2, which needs no casts.
Quoted 3 lines · Andy
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.
Mark, would you be ok with masking in the core? I would mask val at the start of _regmap_write(), before the cache is updated, so that the cache and the bus see the same value, and document it for regmap_write().
Some regmaps leave val_bits at 0 (e.g. ad4130, ad4170-4 and phy-cadence-sierra), so masking has to be skipped for those. The reg_update_bits() and reg_noinc_write() callbacks do not go through _regmap_write(), so I would check them separately.
The alternative is to reject such values with -EINVAL for all buses, but that could break drivers that currently rely on the formatters dropping the upper bits.
If masking is fine, v2 will be a series with the regmap change first and the tests after it, so that they pass.
Best regards,
Raffaele
Mark Brown
maintainerQuoted 7 lines · Raffaele Tranfaglia
Quoted 3 lines
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.
Mark, would you be ok with masking in the core? I would mask val at the start of
_regmap_write(), before the cache is updated, so that the cache and the bus see the same value, and document it forregmap_write().
Yes.