* [PATCH 0/3] regmap: 2nd clean up and improve __regmap_init()
@ 2026-11-21 12:44 Peng Fan
2026-11-21 12:44 ` [PATCH 1/3] regmap: drop regmap_bus::free_on_exit Peng Fan
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Peng Fan @ 2026-11-21 12:44 UTC (permalink / raw)
To: Mark Brown, Greg Kroah-Hartman, Rafael J. Wysocki,
Danilo Krummrich, Johan Hovold
Cc: linux-kernel, driver-core, Peng Fan
This is a re-post of patch 8-10 from [1] with addressing Mark's comments
to patch 8 [2]
[1] https://lore.kernel.org/all/20261113-regmap-improve-v2-0-5dfd6a89078f@nxp.com/
[2] https://lore.kernel.org/all/179510759875.116416.1025341724543668395.b4-review@b4/
This series removes the per-device regmap_bus copies used by the I2C and
SPI helpers to override max_raw_read and max_raw_write, converts the
__regmap_init() error unwinding to the scope-based cleanup API, and adds
KUnit coverage for the new init_limits callback.
The first patch replaces regmap_bus::free_on_exit with an optional
init_limits() callback. It is invoked after the bus raw I/O limits have
been copied and before the cache is initialized. The ordering matters
for maps with raw register defaults: regcache_hw_init() reads them back
from the device during initialization and chunks that transfer using
max_raw_read. Following review feedback, the callback takes the bus
context, the config and pointers to the limits instead of struct regmap,
which is private to drivers/base/regmap, so bus implementations outside
the regmap core can use it.
The second patch converts the __regmap_init() error unwinding to the
cleanup API. A dedicated release helper unwinds a partially initialized
map in the same order as the old labels. map->cache_type is now assigned
just before regcache_init() and reset if it fails, which makes
regcache_exit() safe to call from the cleanup path at any point.
The third patch adds a KUnit test that lowers the raw I/O limits from the
init_limits callback and relies on raw register defaults. The test
verifies that the cache readback is split according to the limits and
that the callback runs before regcache_init().
No functional changes.
Signed-off-by: Peng Fan <peng.fan@nxp.com>
---
Peng Fan (3):
regmap: drop regmap_bus::free_on_exit
regmap: use cleanup API in __regmap_init()
regmap: kunit: add test for regmap_bus::init_limits
drivers/base/regmap/regmap-i2c.c | 85 ++++++++++++++++++-----------------
drivers/base/regmap/regmap-kunit.c | 91 ++++++++++++++++++++++++++++++++++++++
drivers/base/regmap/regmap-spi.c | 66 +++++++++++----------------
drivers/base/regmap/regmap.c | 77 ++++++++++++++++----------------
include/linux/regmap.h | 10 ++++-
5 files changed, 207 insertions(+), 122 deletions(-)
---
base-commit: 909ef7a9a30fa139e69e555e3606680f5cca7726
change-id: 20261121-regmap-improve-2nd-376762e4d823
Best regards,
--
Peng Fan <peng.fan@nxp.com>
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH 1/3] regmap: drop regmap_bus::free_on_exit
2026-11-21 12:44 [PATCH 0/3] regmap: 2nd clean up and improve __regmap_init() Peng Fan
@ 2026-11-21 12:44 ` Peng Fan
2026-11-21 12:44 ` [PATCH 2/3] regmap: use cleanup API in __regmap_init() Peng Fan
2026-11-21 12:44 ` [PATCH 3/3] regmap: kunit: add test for regmap_bus::init_limits Peng Fan
2 siblings, 0 replies; 4+ messages in thread
From: Peng Fan @ 2026-11-21 12:44 UTC (permalink / raw)
To: Mark Brown, Greg Kroah-Hartman, Rafael J. Wysocki,
Danilo Krummrich, Johan Hovold
Cc: linux-kernel, driver-core, Peng Fan
From: Peng Fan <peng.fan@nxp.com>
The SPI and I2C bus helpers kmemdup the static regmap_bus to override
max_raw_read and max_raw_write per device and mark the copy with
free_on_exit so that the regmap core frees it. This is the only user of
free_on_exit, and the per-device limits can instead be applied to the
map while it is being initialized.
Add an optional regmap_bus::init_limits() callback which is invoked by
__regmap_init() after the bus raw I/O limits have been copied and before
the cache is initialized. The latter matters for maps with raw register
defaults: regcache_hw_init() reads them from the device while the map is
still being initialized and uses max_raw_read to chunk the transfer.
The callback takes the bus context, the config and pointers to the raw
I/O limits. struct regmap is private to drivers/base/regmap, so passing
it would make the callback unusable for bus implementations outside the
regmap core.
Use the callback for the I2C and SPI buses, which allows using the static
bus structures directly. This removes the per-device bus allocations and
lets us drop free_on_exit along with the associated error and exit paths
in the regmap core.
No functional changes.
Signed-off-by: Peng Fan <peng.fan@nxp.com>
---
drivers/base/regmap/regmap-i2c.c | 85 +++++++++++++++++++++-------------------
drivers/base/regmap/regmap-spi.c | 66 +++++++++++++------------------
drivers/base/regmap/regmap.c | 8 ++--
include/linux/regmap.h | 10 ++++-
4 files changed, 83 insertions(+), 86 deletions(-)
diff --git a/drivers/base/regmap/regmap-i2c.c b/drivers/base/regmap/regmap-i2c.c
index a4a259232980..7635eca2cbee 100644
--- a/drivers/base/regmap/regmap-i2c.c
+++ b/drivers/base/regmap/regmap-i2c.c
@@ -13,6 +13,43 @@
#include "internal.h"
+static void regmap_i2c_init_limits(void *context,
+ const struct regmap_config *config,
+ size_t *max_raw_read,
+ size_t *max_raw_write)
+{
+ struct device *dev = context;
+ struct i2c_client *i2c = to_i2c_client(dev);
+ const struct i2c_adapter_quirks *quirks = i2c->adapter->quirks;
+ size_t val_bytes = BITS_TO_BYTES(config->val_bits);
+ size_t max_read = *max_raw_read;
+ size_t max_write = *max_raw_write;
+
+ if (!quirks)
+ return;
+
+ if (quirks->max_read_len &&
+ quirks->max_read_len >= val_bytes &&
+ (max_read == 0 || max_read > quirks->max_read_len))
+ max_read = quirks->max_read_len;
+
+ if (quirks->max_write_len) {
+ size_t reg_reserve_size, max_payload;
+
+ reg_reserve_size = (config->reg_bits + config->pad_bits) /
+ BITS_PER_BYTE;
+ if (quirks->max_write_len > reg_reserve_size) {
+ max_payload = quirks->max_write_len - reg_reserve_size;
+ if (max_payload >= val_bytes &&
+ (max_write == 0 || max_write > max_payload))
+ max_write = max_payload;
+ }
+ }
+
+ *max_raw_read = max_read;
+ *max_raw_write = max_write;
+}
+
static int regmap_smbus_byte_reg_read(void *context, unsigned int reg,
unsigned int *val)
{
@@ -47,6 +84,7 @@ static int regmap_smbus_byte_reg_write(void *context, unsigned int reg,
static const struct regmap_bus regmap_smbus_byte = {
.reg_write = regmap_smbus_byte_reg_write,
.reg_read = regmap_smbus_byte_reg_read,
+ .init_limits = regmap_i2c_init_limits,
};
static int regmap_smbus_word_reg_read(void *context, unsigned int reg,
@@ -83,6 +121,7 @@ static int regmap_smbus_word_reg_write(void *context, unsigned int reg,
static const struct regmap_bus regmap_smbus_word = {
.reg_write = regmap_smbus_word_reg_write,
.reg_read = regmap_smbus_word_reg_read,
+ .init_limits = regmap_i2c_init_limits,
};
static int regmap_smbus_word_read_swapped(void *context, unsigned int reg,
@@ -119,6 +158,7 @@ static int regmap_smbus_word_write_swapped(void *context, unsigned int reg,
static const struct regmap_bus regmap_smbus_word_swapped = {
.reg_write = regmap_smbus_word_write_swapped,
.reg_read = regmap_smbus_word_read_swapped,
+ .init_limits = regmap_i2c_init_limits,
};
static int regmap_i2c_write(void *context, const void *data, size_t count)
@@ -204,6 +244,7 @@ static const struct regmap_bus regmap_i2c = {
.read = regmap_i2c_read,
.reg_format_endian_default = REGMAP_ENDIAN_BIG,
.val_format_endian_default = REGMAP_ENDIAN_BIG,
+ .init_limits = regmap_i2c_init_limits,
};
static int regmap_i2c_smbus_i2c_write(void *context, const void *data,
@@ -245,6 +286,7 @@ static const struct regmap_bus regmap_i2c_smbus_i2c_block = {
.read = regmap_i2c_smbus_i2c_read,
.max_raw_read = I2C_SMBUS_BLOCK_MAX - 1,
.max_raw_write = I2C_SMBUS_BLOCK_MAX - 1,
+ .init_limits = regmap_i2c_init_limits,
};
static int regmap_i2c_smbus_i2c_write_reg16(void *context, const void *data,
@@ -302,6 +344,7 @@ static const struct regmap_bus regmap_i2c_smbus_i2c_block_reg16 = {
.read = regmap_i2c_smbus_i2c_read_reg16,
.max_raw_read = I2C_SMBUS_BLOCK_MAX - 2,
.max_raw_write = I2C_SMBUS_BLOCK_MAX - 2,
+ .init_limits = regmap_i2c_init_limits,
};
/*
@@ -346,16 +389,13 @@ static const struct regmap_bus regmap_smbus_byte_word_reg16 = {
.read = regmap_i2c_smbus_i2c_read_reg16,
.max_raw_read = I2C_SMBUS_BLOCK_MAX - 2,
.max_raw_write = 1,
+ .init_limits = regmap_i2c_init_limits,
};
static const struct regmap_bus *regmap_get_i2c_bus(struct i2c_client *i2c,
const struct regmap_config *config)
{
- const struct i2c_adapter_quirks *quirks;
const struct regmap_bus *bus = NULL;
- struct regmap_bus *ret_bus;
- size_t val_bytes;
- u16 max_read, max_write;
if (i2c_check_functionality(i2c->adapter, I2C_FUNC_I2C))
bus = ®map_i2c;
@@ -393,43 +433,6 @@ static const struct regmap_bus *regmap_get_i2c_bus(struct i2c_client *i2c,
if (!bus)
return ERR_PTR(-ENOTSUPP);
- max_read = bus->max_raw_read;
- max_write = bus->max_raw_write;
- val_bytes = BITS_TO_BYTES(config->val_bits);
-
- quirks = i2c->adapter->quirks;
- if (quirks) {
- if (quirks->max_read_len &&
- quirks->max_read_len >= val_bytes &&
- (max_read == 0 || max_read > quirks->max_read_len))
- max_read = quirks->max_read_len;
-
- if (quirks->max_write_len) {
- size_t reg_reserve_size, max_payload;
-
- reg_reserve_size = (config->reg_bits + config->pad_bits) /
- BITS_PER_BYTE;
- if (quirks->max_write_len > reg_reserve_size) {
- max_payload = quirks->max_write_len -
- reg_reserve_size;
- if (max_payload >= val_bytes &&
- (max_write == 0 || max_write > max_payload))
- max_write = max_payload;
- }
- }
-
- if (max_read != bus->max_raw_read ||
- max_write != bus->max_raw_write) {
- ret_bus = kmemdup(bus, sizeof(*bus), GFP_KERNEL);
- if (!ret_bus)
- return ERR_PTR(-ENOMEM);
- ret_bus->free_on_exit = true;
- ret_bus->max_raw_read = max_read;
- ret_bus->max_raw_write = max_write;
- bus = ret_bus;
- }
- }
-
return bus;
}
diff --git a/drivers/base/regmap/regmap-spi.c b/drivers/base/regmap/regmap-spi.c
index b9fec387997e..e095e0bba10d 100644
--- a/drivers/base/regmap/regmap-spi.c
+++ b/drivers/base/regmap/regmap-spi.c
@@ -98,6 +98,28 @@ static int regmap_spi_read(void *context,
return spi_write_then_read(spi, reg, reg_size, val, val_size);
}
+static void regmap_spi_init_limits(void *context,
+ const struct regmap_config *config,
+ size_t *max_raw_read,
+ size_t *max_raw_write)
+{
+ struct device *dev = context;
+ struct spi_device *spi = to_spi_device(dev);
+ size_t max_size = spi_max_transfer_size(spi);
+ size_t max_msg_size, reg_reserve_size;
+
+ if (max_size == SIZE_MAX)
+ return;
+
+ max_msg_size = spi_max_message_size(spi);
+ reg_reserve_size = (config->reg_bits + config->pad_bits) / BITS_PER_BYTE;
+ if (max_size + reg_reserve_size > max_msg_size)
+ max_size -= reg_reserve_size;
+
+ *max_raw_read = max_size;
+ *max_raw_write = max_size;
+}
+
static const struct regmap_bus regmap_spi = {
.write = regmap_spi_write,
.gather_write = regmap_spi_gather_write,
@@ -107,46 +129,16 @@ static const struct regmap_bus regmap_spi = {
.read_flag_mask = 0x80,
.reg_format_endian_default = REGMAP_ENDIAN_BIG,
.val_format_endian_default = REGMAP_ENDIAN_BIG,
+ .init_limits = regmap_spi_init_limits,
};
-static const struct regmap_bus *regmap_get_spi_bus(struct spi_device *spi,
- const struct regmap_config *config)
-{
- size_t max_size = spi_max_transfer_size(spi);
- size_t max_msg_size, reg_reserve_size;
- struct regmap_bus *bus;
-
- if (max_size != SIZE_MAX) {
- bus = kmemdup(®map_spi, sizeof(*bus), GFP_KERNEL);
- if (!bus)
- return ERR_PTR(-ENOMEM);
-
- max_msg_size = spi_max_message_size(spi);
- reg_reserve_size = (config->reg_bits + config->pad_bits) / BITS_PER_BYTE;
- if (max_size + reg_reserve_size > max_msg_size)
- max_size -= reg_reserve_size;
-
- bus->free_on_exit = true;
- bus->max_raw_read = max_size;
- bus->max_raw_write = max_size;
-
- return bus;
- }
-
- return ®map_spi;
-}
-
struct regmap *__regmap_init_spi(struct spi_device *spi,
const struct regmap_config *config,
struct lock_class_key *lock_key,
const char *lock_name)
{
- const struct regmap_bus *bus = regmap_get_spi_bus(spi, config);
-
- if (IS_ERR(bus))
- return ERR_CAST(bus);
-
- return __regmap_init(&spi->dev, bus, &spi->dev, config, lock_key, lock_name);
+ return __regmap_init(&spi->dev, ®map_spi, &spi->dev, config,
+ lock_key, lock_name);
}
EXPORT_SYMBOL_GPL(__regmap_init_spi);
@@ -155,12 +147,8 @@ struct regmap *__devm_regmap_init_spi(struct spi_device *spi,
struct lock_class_key *lock_key,
const char *lock_name)
{
- const struct regmap_bus *bus = regmap_get_spi_bus(spi, config);
-
- if (IS_ERR(bus))
- return ERR_CAST(bus);
-
- return __devm_regmap_init(&spi->dev, bus, &spi->dev, config, lock_key, lock_name);
+ return __devm_regmap_init(&spi->dev, ®map_spi, &spi->dev, config,
+ lock_key, lock_name);
}
EXPORT_SYMBOL_GPL(__devm_regmap_init_spi);
diff --git a/drivers/base/regmap/regmap.c b/drivers/base/regmap/regmap.c
index 910f66bcff40..7968c30bd09a 100644
--- a/drivers/base/regmap/regmap.c
+++ b/drivers/base/regmap/regmap.c
@@ -1180,6 +1180,10 @@ struct regmap *__regmap_init(struct device *dev,
if (ret)
goto err_range;
+ if (bus && bus->init_limits)
+ bus->init_limits(bus_context, config,
+ &map->max_raw_read, &map->max_raw_write);
+
ret = regcache_init(map, config);
if (ret != 0)
goto err_range;
@@ -1207,8 +1211,6 @@ struct regmap *__regmap_init(struct device *dev,
err_map:
kfree(map);
err:
- if (bus && bus->free_on_exit)
- kfree(bus);
return ERR_PTR(ret);
}
EXPORT_SYMBOL_GPL(__regmap_init);
@@ -1521,8 +1523,6 @@ void regmap_exit(struct regmap *map)
mutex_destroy(&map->mutex);
kfree_const(map->name);
kfree(map->patch);
- if (map->bus && map->bus->free_on_exit)
- kfree(map->bus);
kfree(map);
}
EXPORT_SYMBOL_GPL(regmap_exit);
diff --git a/include/linux/regmap.h b/include/linux/regmap.h
index 8f3034881f8a..a1809be66235 100644
--- a/include/linux/regmap.h
+++ b/include/linux/regmap.h
@@ -580,7 +580,6 @@ typedef void (*regmap_hw_free_context)(void *context);
* to perform locking. This field is ignored if custom lock/unlock
* functions are used (see fields lock/unlock of
* struct regmap_config).
- * @free_on_exit: kfree this on exit of regmap
* @write: Write operation.
* @gather_write: Write operation with split register/value, return -ENOTSUPP
* if not implemented on a given device.
@@ -611,10 +610,13 @@ typedef void (*regmap_hw_free_context)(void *context);
* DEFAULT, BIG is assumed.
* @max_raw_read: Max raw read size that can be used on the bus.
* @max_raw_write: Max raw write size that can be used on the bus.
+ * @init_limits: Optional callback to adjust the maximum raw read and write
+ * sizes for a device. Called with the bus context, the config and
+ * pointers to the limits during regmap initialization before the cache
+ * is initialized.
*/
struct regmap_bus {
bool fast_io;
- bool free_on_exit;
regmap_hw_write write;
regmap_hw_gather_write gather_write;
regmap_hw_async_write async_write;
@@ -631,6 +633,10 @@ struct regmap_bus {
enum regmap_endian val_format_endian_default;
size_t max_raw_read;
size_t max_raw_write;
+ void (*init_limits)(void *context,
+ const struct regmap_config *config,
+ size_t *max_raw_read,
+ size_t *max_raw_write);
};
/*
--
2.56.0
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH 2/3] regmap: use cleanup API in __regmap_init()
2026-11-21 12:44 [PATCH 0/3] regmap: 2nd clean up and improve __regmap_init() Peng Fan
2026-11-21 12:44 ` [PATCH 1/3] regmap: drop regmap_bus::free_on_exit Peng Fan
@ 2026-11-21 12:44 ` Peng Fan
2026-11-21 12:44 ` [PATCH 3/3] regmap: kunit: add test for regmap_bus::init_limits Peng Fan
2 siblings, 0 replies; 4+ messages in thread
From: Peng Fan @ 2026-11-21 12:44 UTC (permalink / raw)
To: Mark Brown, Greg Kroah-Hartman, Rafael J. Wysocki,
Danilo Krummrich, Johan Hovold
Cc: linux-kernel, driver-core, Peng Fan
From: Peng Fan <peng.fan@nxp.com>
Convert the error unwinding in __regmap_init() to the scope-based
cleanup API and drop all of the err_* labels. A dedicated
regmap_init_release() helper unwinds a partially initialised map in the
same order as the old labels.
The err label only returned ERR_PTR(ret) after the per-device bus
allocation was removed, so the two early failure paths now return
directly as well.
regcache_exit() requires an initialised cache, so map->cache_type is now
set right before regcache_init() instead of during the initial map setup
and reset to REGCACHE_NONE if regcache_init() fails. This makes
regcache_exit() safe to call from the cleanup path at any point. Nothing
between the old and new assignment reads cache_type.
No functional change.
Signed-off-by: Peng Fan <peng.fan@nxp.com>
---
drivers/base/regmap/regmap.c | 69 +++++++++++++++++++++-----------------------
1 file changed, 33 insertions(+), 36 deletions(-)
diff --git a/drivers/base/regmap/regmap.c b/drivers/base/regmap/regmap.c
index 7968c30bd09a..0ba82fcc55bf 100644
--- a/drivers/base/regmap/regmap.c
+++ b/drivers/base/regmap/regmap.c
@@ -1037,6 +1037,22 @@ static int regmap_init_format(struct regmap *map,
return 0;
}
+static void regmap_init_release(struct regmap *map)
+{
+ if (!map)
+ return;
+
+ regcache_exit(map);
+ regmap_range_exit(map);
+ kfree(map->work_buf);
+ if (map->hwlock)
+ hwspin_lock_free(map->hwlock);
+ kfree_const(map->name);
+ kfree(map);
+}
+
+DEFINE_FREE(regmap_init_release, struct regmap *, regmap_init_release(_T));
+
struct regmap *__regmap_init(struct device *dev,
const struct regmap_bus *bus,
void *bus_context,
@@ -1044,30 +1060,25 @@ struct regmap *__regmap_init(struct device *dev,
struct lock_class_key *lock_key,
const char *lock_name)
{
- struct regmap *map;
bool fast_io;
int ret;
- if (!config) {
- ret = -EINVAL;
- goto err;
- }
+ if (!config)
+ return ERR_PTR(-EINVAL);
- map = kzalloc_obj(*map);
- if (map == NULL) {
- ret = -ENOMEM;
- goto err;
- }
+ struct regmap *map __free(regmap_init_release) = kzalloc_obj(*map);
+ if (map == NULL)
+ return ERR_PTR(-ENOMEM);
ret = regmap_set_name(map, config);
if (ret)
- goto err_map;
+ return ERR_PTR(ret);
fast_io = (bus && bus->fast_io) || config->fast_io;
ret = regmap_set_lock(map, config, fast_io, lock_key, lock_name);
if (ret)
- goto err_name;
+ return ERR_PTR(ret);
/*
* When we write in fast-paths with regmap_bulk_write() don't allocate
@@ -1119,7 +1130,6 @@ struct regmap *__regmap_init(struct device *dev,
map->writeable_noinc_reg = config->writeable_noinc_reg;
map->readable_noinc_reg = config->readable_noinc_reg;
map->reg_default_cb = config->reg_default_cb;
- map->cache_type = config->cache_type;
spin_lock_init(&map->async_lock);
INIT_LIST_HEAD(&map->async_list);
@@ -1148,7 +1158,7 @@ struct regmap *__regmap_init(struct device *dev,
REGMAP_ENDIAN_NATIVE,
REGMAP_ENDIAN_NATIVE);
if (ret)
- goto err_hwlock;
+ return ERR_PTR(ret);
} else if (!bus) {
map->reg_read = config->reg_read;
map->reg_write = config->reg_write;
@@ -1172,46 +1182,33 @@ struct regmap *__regmap_init(struct device *dev,
regmap_get_reg_endian(bus, config),
regmap_get_val_endian(dev, bus, config));
if (ret)
- goto err_hwlock;
+ return ERR_PTR(ret);
}
-
ret = regmap_init_ranges(map, config);
if (ret)
- goto err_range;
+ return ERR_PTR(ret);
if (bus && bus->init_limits)
bus->init_limits(bus_context, config,
&map->max_raw_read, &map->max_raw_write);
+ map->cache_type = config->cache_type;
ret = regcache_init(map, config);
- if (ret != 0)
- goto err_range;
+ if (ret) {
+ map->cache_type = REGCACHE_NONE;
+ return ERR_PTR(ret);
+ }
if (dev) {
ret = regmap_attach_dev(dev, map, config);
if (ret != 0)
- goto err_regcache;
+ return ERR_PTR(ret);
} else {
regmap_debugfs_init(map);
}
- return map;
-
-err_regcache:
- regcache_exit(map);
-err_range:
- regmap_range_exit(map);
- kfree(map->work_buf);
-err_hwlock:
- if (map->hwlock)
- hwspin_lock_free(map->hwlock);
-err_name:
- kfree_const(map->name);
-err_map:
- kfree(map);
-err:
- return ERR_PTR(ret);
+ return_ptr(map);
}
EXPORT_SYMBOL_GPL(__regmap_init);
--
2.56.0
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH 3/3] regmap: kunit: add test for regmap_bus::init_limits
2026-11-21 12:44 [PATCH 0/3] regmap: 2nd clean up and improve __regmap_init() Peng Fan
2026-11-21 12:44 ` [PATCH 1/3] regmap: drop regmap_bus::free_on_exit Peng Fan
2026-11-21 12:44 ` [PATCH 2/3] regmap: use cleanup API in __regmap_init() Peng Fan
@ 2026-11-21 12:44 ` Peng Fan
2 siblings, 0 replies; 4+ messages in thread
From: Peng Fan @ 2026-11-21 12:44 UTC (permalink / raw)
To: Mark Brown, Greg Kroah-Hartman, Rafael J. Wysocki,
Danilo Krummrich, Johan Hovold
Cc: linux-kernel, driver-core, Peng Fan
From: Peng Fan <peng.fan@nxp.com>
Add coverage for the optional regmap_bus::init_limits() callback. The
test bus lowers the raw I/O limits and relies on raw register defaults so
that regcache_hw_init() reads from the device during map initialization.
The readback must be split according to the limits set by the callback,
which also verifies that __regmap_init() invokes the callback before
regcache_init().
Signed-off-by: Peng Fan <peng.fan@nxp.com>
---
drivers/base/regmap/regmap-kunit.c | 91 ++++++++++++++++++++++++++++++++++++++
1 file changed, 91 insertions(+)
diff --git a/drivers/base/regmap/regmap-kunit.c b/drivers/base/regmap/regmap-kunit.c
index c948219f46a3..75c251132c89 100644
--- a/drivers/base/regmap/regmap-kunit.c
+++ b/drivers/base/regmap/regmap-kunit.c
@@ -2247,6 +2247,96 @@ static void raw_ranges(struct kunit *test)
}
}
+#define TEST_INIT_LIMITS_REGS 16
+#define TEST_INIT_LIMITS_MAX_RAW 4
+
+struct test_init_limits_data {
+ int num_reads;
+ size_t read_sizes[TEST_INIT_LIMITS_REGS];
+};
+
+static void test_init_limits_bus_init_limits(void *context,
+ const struct regmap_config *config,
+ size_t *max_raw_read,
+ size_t *max_raw_write)
+{
+ *max_raw_read = TEST_INIT_LIMITS_MAX_RAW;
+ *max_raw_write = TEST_INIT_LIMITS_MAX_RAW;
+}
+
+static int test_init_limits_bus_read(void *context, const void *reg_buf,
+ size_t reg_size, void *val_buf,
+ size_t val_size)
+{
+ struct test_init_limits_data *data = context;
+
+ if (data->num_reads < ARRAY_SIZE(data->read_sizes))
+ data->read_sizes[data->num_reads] = val_size;
+ data->num_reads++;
+
+ memset(val_buf, 0, val_size);
+
+ return 0;
+}
+
+static int test_init_limits_bus_write(void *context, const void *data,
+ size_t count)
+{
+ return 0;
+}
+
+static const struct regmap_bus test_init_limits_bus = {
+ .read = test_init_limits_bus_read,
+ .write = test_init_limits_bus_write,
+ .init_limits = test_init_limits_bus_init_limits,
+};
+
+static const struct regmap_config test_init_limits_config = {
+ .reg_bits = 8,
+ .val_bits = 8,
+ .max_register = TEST_INIT_LIMITS_REGS - 1,
+ .cache_type = REGCACHE_FLAT,
+ .num_reg_defaults_raw = TEST_INIT_LIMITS_REGS,
+};
+
+static void test_init_limits(struct kunit *test)
+{
+ struct regmap_test_priv *priv = test->priv;
+ struct test_init_limits_data *data;
+ struct regmap *map;
+ int i, ret;
+
+ data = kunit_kzalloc(test, sizeof(*data), GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, data);
+
+ map = regmap_init(priv->dev, &test_init_limits_bus, data,
+ &test_init_limits_config);
+ KUNIT_ASSERT_FALSE(test, IS_ERR(map));
+ if (IS_ERR(map))
+ return;
+
+ ret = kunit_add_action_or_reset(test, regmap_exit_action, map);
+ KUNIT_ASSERT_EQ(test, 0, ret);
+ if (ret)
+ return;
+
+ KUNIT_EXPECT_EQ(test, (size_t)TEST_INIT_LIMITS_MAX_RAW,
+ regmap_get_raw_read_max(map));
+ KUNIT_EXPECT_EQ(test, (size_t)TEST_INIT_LIMITS_MAX_RAW,
+ regmap_get_raw_write_max(map));
+
+ /*
+ * regcache_hw_init() reads raw defaults during map initialization,
+ * so the callback must already have set the limit which chunks the
+ * readback.
+ */
+ KUNIT_ASSERT_EQ(test, TEST_INIT_LIMITS_REGS / TEST_INIT_LIMITS_MAX_RAW,
+ data->num_reads);
+ for (i = 0; i < data->num_reads; i++)
+ KUNIT_EXPECT_EQ(test, (size_t)TEST_INIT_LIMITS_MAX_RAW,
+ data->read_sizes[i]);
+}
+
static struct kunit_case regmap_test_cases[] = {
KUNIT_CASE_PARAM(basic_read_write, regcache_types_gen_params),
KUNIT_CASE_PARAM(read_bypassed, real_cache_types_gen_params),
@@ -2290,6 +2380,7 @@ static struct kunit_case regmap_test_cases[] = {
KUNIT_CASE_PARAM(raw_noinc_write, raw_test_types_gen_params),
KUNIT_CASE_PARAM(raw_sync, raw_test_cache_types_gen_params),
KUNIT_CASE_PARAM(raw_ranges, raw_test_cache_types_gen_params),
+ KUNIT_CASE(test_init_limits),
{}
};
--
2.56.0
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-21 12:44 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-21 12:44 [PATCH 0/3] regmap: 2nd clean up and improve __regmap_init() Peng Fan
2026-11-21 12:44 ` [PATCH 1/3] regmap: drop regmap_bus::free_on_exit Peng Fan
2026-11-21 12:44 ` [PATCH 2/3] regmap: use cleanup API in __regmap_init() Peng Fan
2026-11-21 12:44 ` [PATCH 3/3] regmap: kunit: add test for regmap_bus::init_limits Peng Fan
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®