regmap: handle values wider than val_bits consistently
Subsystems (5)
v3 cover regmap: handle values wider than val_bits consistently
regmap_write() takes an unsigned int, and what happens to the bits above val_bits currently depends on the bus and on the cache type. As agreed on the RFC, mask them in the core, before the cache is updated.
Patch 1 prepares nau8360, which sets val_bits = 16 but writes 32-bit values to its DSP registers, so it would break with the masking. Patch 2 adds the masking.
Patch 3 adds the KUnit tests from the RFC, which now pass.
I searched for other drivers whose reg_write() handles more bits than val_bits and found no other clear case, but the search cannot be exhaustive. Implementations of reg_update_bits() do not go through _regmap_write() and are not changed.
The regmap KUnit tests pass on arm64 QEMU. The nau8360 change is only compile tested.
I prepared these patches with the help of an AI coding assistant, and reviewed and tested them myself.
---
Changes in v3:
- Simplify the val_mask computation, no regmap uses val_bits above 32
(Andy).
- Rebased on the current regmap for-7.5.
- Collect Reviewed-by for patch 3 (Andy).
- Link to v2: https://patch.msgid.link/[email protected]
Changes in v2:
- No longer an RFC.
- New patch masking the values in the regmap core (Andy, Mark).
- New patch preparing nau8360.
- Use be16_to_cpup()/le16_to_cpup() instead of casts in the tests (Andy,
Mark).
- Rebased on regmap for-7.5.
- Link to v1: https://patch.msgid.link/[email protected]
---
Raffaele Tranfaglia (3):
ASoC: codecs: nau8360: set val_bits to the widest register size
regmap: mask values to val_bits on write
regmap: kunit: add tests for values wider than val_bits
drivers/base/regmap/internal.h | 1 + drivers/base/regmap/regmap-kunit.c | 60 ++++++++++++++++++++++++++++++++++++++ drivers/base/regmap/regmap.c | 13 ++++++++- sound/soc/codecs/nau8360.c | 3 +- sound/soc/codecs/nau8360.h | 3 +- 5 files changed, 76 insertions(+), 4 deletions(-)
---
base-commit: b65165787330d13228a0d89e211971f65607ca92
change-id: 20261117-regmap-kunit-val-bits-6473305e083b
Best regards,
Signature · 1 line
Raffaele Tranfaglia <[email protected]>
PATCH v3 1/3 ASoC: codecs: nau8360: set val_bits to the widest register size
The regmap is configured with val_bits = 16, but the two DSP registers are 32 bits wide: nau8360_reg_write() sends four bytes for them, and the DSP messages written with snd_soc_component_write() use all 32 bits.
This works because the driver provides its own reg_read() and reg_write() and regmap passes the value through unmodified. As regmap is going to mask values to val_bits, set it to 32, the size of the widest register. The size of each access is still selected in the driver's reg_read() and reg_write().
--- sound/soc/codecs/nau8360.c | 3 ++- sound/soc/codecs/nau8360.h | 3 +-- 2 files changed, 3 insertions(+), 3 deletions(-)
sound/soc/codecs/nau8360.c +2 -1
@@ -1918,7 +1918,8 @@ static int nau8360_reg_read(void *context, unsigned int reg, unsigned int *valuestatic const struct regmap_config nau8360_regmap_config = { .reg_bits =NAU8360_REG_ADDR_LEN , - .val_bits =NAU8360_REG_DATA_LEN , +/* Codec registers are 16 bits wide, DSP registers are 32 bits wide */ + .val_bits =32 , .max_register =NAU8360_REG_MAX , .readable_reg = nau8360_readable_reg,
sound/soc/codecs/nau8360.h +1 -2
@@ -123,9 +123,8 @@#define NAU8360_RF002_DSP_COMM 0xf002#define NAU8360_REG_MAX NAU8360_RF002_DSP_COMM -/* 16-bit control register address, and 16-bits control register data */ +/* 16-bit control register address */ #define NAU8360_REG_ADDR_LEN 16 -#define NAU8360_REG_DATA_LEN 16/* NAU8360_R03_CLK_CTRL0 (0x03) */ #define NAU8360_MCLK_DIV_SFT 10
Signature · 1 line
2.56.0
PATCH v3 2/3 regmap: mask values to val_bits on write
In reply to Raffaele Tranfagliaregmap_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.
--- drivers/base/regmap/internal.h | 1 + drivers/base/regmap/regmap.c | 13 ++++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-)
drivers/base/regmap/internal.h +1 -0
@@ -33,6 +33,7 @@ struct regmap_format {size_t reg_bytes;size_t pad_bytes;size_t val_bytes; +unsigned int val_mask;s8 reg_shift;void (*format_write)(struct regmap *map,unsigned int reg,unsigned int val);
drivers/base/regmap/regmap.c +12 -1
@@ -6,6 +6,7 @@// // Author: Mark Brown <[email protected]> +#include <linux/bits.h> #include <linux/device.h> #include <linux/slab.h> #include <linux/export.h>
@@ -1093,6 +1094,11 @@ struct regmap *__regmap_init(struct device *dev, map->format.reg_shift = config->reg_shift; map->format.reg_bytes =BITS_TO_BYTES (config->reg_bits); map->format.val_bytes =BITS_TO_BYTES (config->val_bits); +/* Some users with their own register accessors don't set val_bits */ +if (config->val_bits) + map->format.val_mask =GENMASK (config->val_bits -1 ,0 ); +else + map->format.val_mask =UINT_MAX ; map->format.buf_size =BITS_TO_BYTES (config->reg_bits + config->val_bits + config->pad_bits);if (config->reg_stride) map->reg_stride = config->reg_stride;
@@ -1983,6 +1989,9 @@ int _regmap_write(struct regmap *map, unsigned int reg,if (!regmap_writeable (map, reg))return -EIO ; +/* Make sure that the cache and the device see the same value */ + val &= map->format.val_mask; +if (!map->cache_bypass && !map->defer_caching) { ret =regcache_write (map, reg, val);if (ret !=0 )
@@ -2011,6 +2020,8 @@ int _regmap_write(struct regmap *map, unsigned int reg,* @reg: Register to write to * @val: Value to be written * +* If the regmap_config sets val_bits, any bits of @val above it are ignored. +* * A value of zero will be returned on success, a negative errno will * be returned in error cases. */
@@ -2608,7 +2619,7 @@ static int _regmap_multi_reg_write(struct regmap *map,if (!map->cache_bypass) {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; ret =regcache_write (map, reg, val);if (ret) {
Signature · 1 line
2.56.0
PATCH v3 3/3 regmap: kunit: add tests for values wider than val_bits
In reply to Raffaele TranfagliaAdd two test cases writing 0xffffff0f with regmap_write() to a regmap with val_bits = 16, and check that only the low 16 bits reach the bus and can be read back:
- 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.
--- drivers/base/regmap/regmap-kunit.c | 60 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+)
drivers/base/regmap/regmap-kunit.c +60 -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;
@@ -2004,6 +2031,37 @@ 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; +void *hw_val; +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_val = 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_cpup (hw_val)); +else +KUNIT_EXPECT_EQ (test,0xff0f ,le16_to_cpup (hw_val)); + +/* 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;
@@ -2339,6 +2397,7 @@ static void test_init_limits(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),
@@ -2376,6 +2435,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),
Signature · 1 line
2.56.0