From: Raffaele Tranfaglia via B4 Relay <devnull+raffaele.tranfaglia.gmail.com@kernel.org>
To: Mark Brown <broonie@kernel.org>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
"Rafael J. Wysocki" <rafael@kernel.org>,
Danilo Krummrich <dakr@kernel.org>,
Johan Hovold <johan@kernel.org>,
Liam Girdwood <lgirdwood@gmail.com>,
Jaroslav Kysela <perex@perex.cz>, Takashi Iwai <tiwai@suse.com>
Cc: Andy Shevchenko <andriy.shevchenko@intel.com>,
Jonathan Cameron <jic23@kernel.org>,
linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org,
driver-core@lists.linux.dev, linux-sound@vger.kernel.org,
Neo Chang <YLCHANG2@nuvoton.com>,
Raffaele Tranfaglia <raffaele.tranfaglia@gmail.com>
Subject: [PATCH v2 2/3] regmap: mask values to val_bits on write
Date: Wed, 25 Nov 2026 08:09:31 +0000 [thread overview]
Message-ID: <20261125-regmap-kunit-val-bits-v2-2-81db3b79cd3f@gmail.com> (raw)
In-Reply-To: <20261125-regmap-kunit-val-bits-v2-0-81db3b79cd3f@gmail.com>
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
next prev parent reply other threads:[~2026-11-25 8:09 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
2026-11-25 8:09 ` [PATCH v2 3/3] regmap: kunit: add tests for values wider than val_bits Raffaele Tranfaglia via B4 Relay
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261125-regmap-kunit-val-bits-v2-2-81db3b79cd3f@gmail.com \
--to=devnull+raffaele.tranfaglia.gmail.com@kernel.org \
--cc=YLCHANG2@nuvoton.com \
--cc=andriy.shevchenko@intel.com \
--cc=broonie@kernel.org \
--cc=dakr@kernel.org \
--cc=driver-core@lists.linux.dev \
--cc=gregkh@linuxfoundation.org \
--cc=jic23@kernel.org \
--cc=johan@kernel.org \
--cc=lgirdwood@gmail.com \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-sound@vger.kernel.org \
--cc=perex@perex.cz \
--cc=rafael@kernel.org \
--cc=raffaele.tranfaglia@gmail.com \
--cc=tiwai@suse.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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®