mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2 0/3] regmap: handle values wider than val_bits consistently
@ 2026-11-25  8:09 Raffaele Tranfaglia via B4 Relay
  2026-11-25  8:09 ` [PATCH v2 1/3] ASoC: codecs: nau8360: set val_bits to the widest register size Raffaele Tranfaglia via B4 Relay
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Raffaele Tranfaglia via B4 Relay @ 2026-11-25  8:09 UTC (permalink / raw)
  To: Mark Brown, Greg Kroah-Hartman, Rafael J. Wysocki,
	Danilo Krummrich, Johan Hovold, Liam Girdwood, Jaroslav Kysela,
	Takashi Iwai
  Cc: Andy Shevchenko, Jonathan Cameron, linux-iio, linux-kernel,
	driver-core, linux-sound, Neo Chang, Raffaele Tranfaglia

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/20261117-regmap-kunit-val-bits-v1-1-a1cd02063217@gmail.com

---
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,
--  
Raffaele Tranfaglia <raffaele.tranfaglia@gmail.com>



^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2 1/3] ASoC: codecs: nau8360: set val_bits to the widest register size
  2026-11-25  8:09 [PATCH v2 0/3] regmap: handle values wider than val_bits consistently Raffaele Tranfaglia via B4 Relay
@ 2026-11-25  8:09 ` Raffaele Tranfaglia via B4 Relay
  2026-11-25  8:09 ` [PATCH v2 2/3] regmap: mask values to val_bits on write Raffaele Tranfaglia via B4 Relay
  2026-11-25  8:09 ` [PATCH v2 3/3] regmap: kunit: add tests for values wider than val_bits Raffaele Tranfaglia via B4 Relay
  2 siblings, 0 replies; 4+ messages in thread
From: Raffaele Tranfaglia via B4 Relay @ 2026-11-25  8:09 UTC (permalink / raw)
  To: Mark Brown, Greg Kroah-Hartman, Rafael J. Wysocki,
	Danilo Krummrich, Johan Hovold, Liam Girdwood, Jaroslav Kysela,
	Takashi Iwai
  Cc: Andy Shevchenko, Jonathan Cameron, linux-iio, linux-kernel,
	driver-core, linux-sound, Neo Chang, Raffaele Tranfaglia

From: Raffaele Tranfaglia <raffaele.tranfaglia@gmail.com>

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

Signed-off-by: Raffaele Tranfaglia <raffaele.tranfaglia@gmail.com>
---
 sound/soc/codecs/nau8360.c | 3 ++-
 sound/soc/codecs/nau8360.h | 3 +--
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/sound/soc/codecs/nau8360.c b/sound/soc/codecs/nau8360.c
index f0942739d7ab..d4cab3c8e29d 100644
--- a/sound/soc/codecs/nau8360.c
+++ b/sound/soc/codecs/nau8360.c
@@ -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,
diff --git a/sound/soc/codecs/nau8360.h b/sound/soc/codecs/nau8360.h
index 71396747c0fa..3d9711804073 100644
--- a/sound/soc/codecs/nau8360.h
+++ b/sound/soc/codecs/nau8360.h
@@ -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

-- 
2.56.0



^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2 2/3] regmap: mask values to val_bits on write
  2026-11-25  8:09 [PATCH v2 0/3] regmap: handle values wider than val_bits consistently Raffaele Tranfaglia via B4 Relay
  2026-11-25  8:09 ` [PATCH v2 1/3] ASoC: codecs: nau8360: set val_bits to the widest register size Raffaele Tranfaglia via B4 Relay
@ 2026-11-25  8:09 ` Raffaele Tranfaglia via B4 Relay
  2026-11-25  8:09 ` [PATCH v2 3/3] regmap: kunit: add tests for values wider than val_bits Raffaele Tranfaglia via B4 Relay
  2 siblings, 0 replies; 4+ messages in thread
From: Raffaele Tranfaglia via B4 Relay @ 2026-11-25  8:09 UTC (permalink / raw)
  To: Mark Brown, Greg Kroah-Hartman, Rafael J. Wysocki,
	Danilo Krummrich, Johan Hovold, Liam Girdwood, Jaroslav Kysela,
	Takashi Iwai
  Cc: Andy Shevchenko, Jonathan Cameron, linux-iio, linux-kernel,
	driver-core, linux-sound, Neo Chang, Raffaele Tranfaglia

From: Raffaele Tranfaglia <raffaele.tranfaglia@gmail.com>

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.

Suggested-by: Andy Shevchenko <andriy.shevchenko@intel.com>
Signed-off-by: Raffaele Tranfaglia <raffaele.tranfaglia@gmail.com>
---
 drivers/base/regmap/internal.h |  1 +
 drivers/base/regmap/regmap.c   | 14 +++++++++++++-
 2 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/drivers/base/regmap/internal.h b/drivers/base/regmap/internal.h
index ea375cc8ae87..6a7efb10127b 100644
--- a/drivers/base/regmap/internal.h
+++ b/drivers/base/regmap/internal.h
@@ -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);
diff --git a/drivers/base/regmap/regmap.c b/drivers/base/regmap/regmap.c
index 45b5b4f76b0c..eab1bd2043ce 100644
--- a/drivers/base/regmap/regmap.c
+++ b/drivers/base/regmap/regmap.c
@@ -6,6 +6,7 @@
 //
 // Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
 
+#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) {

-- 
2.56.0



^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2 3/3] regmap: kunit: add tests for values wider than val_bits
  2026-11-25  8:09 [PATCH v2 0/3] regmap: handle values wider than val_bits consistently Raffaele Tranfaglia via B4 Relay
  2026-11-25  8:09 ` [PATCH v2 1/3] ASoC: codecs: nau8360: set val_bits to the widest register size Raffaele Tranfaglia via B4 Relay
  2026-11-25  8:09 ` [PATCH v2 2/3] regmap: mask values to val_bits on write Raffaele Tranfaglia via B4 Relay
@ 2026-11-25  8:09 ` Raffaele Tranfaglia via B4 Relay
  2 siblings, 0 replies; 4+ messages in thread
From: Raffaele Tranfaglia via B4 Relay @ 2026-11-25  8:09 UTC (permalink / raw)
  To: Mark Brown, Greg Kroah-Hartman, Rafael J. Wysocki,
	Danilo Krummrich, Johan Hovold, Liam Girdwood, Jaroslav Kysela,
	Takashi Iwai
  Cc: Andy Shevchenko, Jonathan Cameron, linux-iio, linux-kernel,
	driver-core, linux-sound, Neo Chang, Raffaele Tranfaglia

From: Raffaele Tranfaglia <raffaele.tranfaglia@gmail.com>

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.

Signed-off-by: Raffaele Tranfaglia <raffaele.tranfaglia@gmail.com>
---
 drivers/base/regmap/regmap-kunit.c | 60 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 60 insertions(+)

diff --git a/drivers/base/regmap/regmap-kunit.c b/drivers/base/regmap/regmap-kunit.c
index c948219f46a3..b8b14995a548 100644
--- a/drivers/base/regmap/regmap-kunit.c
+++ b/drivers/base/regmap/regmap-kunit.c
@@ -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),

-- 
2.56.0



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-25  8:09 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-25  8:09 [PATCH v2 0/3] regmap: handle values wider than val_bits consistently Raffaele Tranfaglia via B4 Relay
2026-11-25  8:09 ` [PATCH v2 1/3] ASoC: codecs: nau8360: set val_bits to the widest register size Raffaele Tranfaglia via B4 Relay
2026-11-25  8:09 ` [PATCH v2 2/3] regmap: mask values to val_bits on write Raffaele Tranfaglia via B4 Relay
2026-11-25  8:09 ` [PATCH v2 3/3] regmap: kunit: add tests for values wider than val_bits Raffaele Tranfaglia via B4 Relay

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®