Skip to contentSkip to conversation

regmap: handle values wider than val_bits consistently

PATCH In review Raffaele Tranfaglia 3 participants 8 messages Nov 25 1 message not in the archive

Subsystems (5)
Sound ASoC IIO Driver core +1 more subsystems
lore
Share & tools
lore
#0 lore raw

v2 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 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       | 14 ++++++++-
 sound/soc/codecs/nau8360.c         |  3 +-
 sound/soc/codecs/nau8360.h         |  3 +-
 5 files changed, 77 insertions(+), 4 deletions(-)

---
base-commit: 573c689219af87f6d5a4bbf809c174e7f28a23a1
change-id: 20261117-regmap-kunit-val-bits-6473305e083b

Best regards,

Signature · 1 line
Raffaele Tranfaglia <[email protected]>
#1 lore raw

PATCH v2 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
index f0942739d7ab..d4cab3c8e29d 100644
@@ -1918,7 +1918,8 @@ static int nau8360_reg_read(void *context, unsigned int reg, unsigned int *value
 
 static 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
index 71396747c0fa..3d9711804073 100644
@@ -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
#4 lore raw

PATCH v2 2/3 regmap: mask values to val_bits on write

In reply to Raffaele Tranfaglia

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.

---
 drivers/base/regmap/internal.h |  1 +
 drivers/base/regmap/regmap.c   | 14 +++++++++++++-
 2 files changed, 14 insertions(+), 1 deletion(-)
drivers/base/regmap/internal.h +1 -0
index ea375cc8ae87..6a7efb10127b 100644
@@ -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 +13 -1
index 45b5b4f76b0c..eab1bd2043ce 100644
@@ -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>
@@ -1082,6 +1083,12 @@ 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 > 0 &&
+	    config->val_bits < BITS_PER_TYPE(map->format.val_mask))
+		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;
@@ -1986,6 +1993,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)
@@ -2014,6 +2024,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.
  */
@@ -2611,7 +2623,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
#7 lore raw

PATCH v2 3/3 regmap: kunit: add tests for values wider than val_bits

In reply to Raffaele Tranfaglia

Add 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
index c948219f46a3..b8b14995a548 100644
@@ -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;
@@ -2249,6 +2307,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),
@@ -2286,6 +2345,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
#2 lore raw

Re: [PATCH v2 1/3] ASoC: codecs: nau8360: set val_bits to the widest register size

In reply to Raffaele Tranfaglia
Quoted 10 lines · On Wed, Nov 25, 2026 at 08:09:30AM +0000, Raffaele Tranfaglia via B4 Relay…

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().

Hmm... While it works, it might be not the best approach. In cases like this elsewhere we create two regmaps (one per each val_bits size) and choose them based on the register offset.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
Reviewed-by on v2 3/3
#8 lore raw

Re: [PATCH v2 3/3] regmap: kunit: add tests for values wider than val_bits

In reply to Raffaele Tranfaglia
Quoted 9 lines · On Wed, Nov 25, 2026 at 08:09:32AM +0000, Raffaele Tranfaglia via B4 Relay…

Add 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.

If I haven't given a tag earlier

as test cases are always warmly welcome!

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#6 lore raw

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

In reply to a message not in the archive
Quoted 1 line · Andy Shevchenko

On Wed, Nov 25, 2026 at 08:09:31AM +0000, Raffaele Tranfaglia via B4 Relay wrote:

...

Quoted 10 lines

drivers/base/regmap/regmap.c struct regmap *__regmap_init(struct device *dev, · line 1091

Quoted 6 lines
drivers/base/regmap/regmap.c +6 -0
+	/* Some users with their own register accessors don't set val_bits */
+	if (config->val_bits > 0 &&
+	    config->val_bits < BITS_PER_TYPE(map->format.val_mask))
+		map->format.val_mask = GENMASK(config->val_bits - 1, 0);
+	else
+		map->format.val_mask = UINT_MAX;

This is simply

	map->format.val_mask = UINT_MAX >> config->val_bits;

Answering to myself, no.

Quoted 1 line

no? In other words, do we have cases IRL when val_bits > 32?

But this question stays for this check

	    config->val_bits < BITS_PER_TYPE(map->format.val_mask)
Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#3 lore raw

Re: [PATCH v2 1/3] ASoC: codecs: nau8360: set val_bits to the widest register size

In reply to Andy Shevchenko
Quoted 3 lines · Andy Shevchenko

Hmm... While it works, it might be not the best approach. In cases like this elsewhere we create two regmaps (one per each val_bits size) and choose them based on the register offset.

Either approach works fine, neither is particuarly elegant - you do end up with a bit of open coding with the register I/O functions, but equally it causes surprises elsewhere to have multiple register maps for the same device and that's also error prone so there's something to be said for putting the special casing in the two functions.