From: Peng Fan <peng.fan@linux.dev>
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>,
Lucas Tanure <tanureal@opensource.cirrus.com>
Cc: linux-kernel@vger.kernel.org, driver-core@lists.linux.dev,
Peng Fan <peng.fan@nxp.com>
Subject: [PATCH v2 06/10] regmap: factor out regmap_init_ranges()
Date: Fri, 13 Nov 2026 23:31:54 +0800 [thread overview]
Message-ID: <20261113-regmap-improve-v2-6-5dfd6a89078f@nxp.com> (raw)
In-Reply-To: <20261113-regmap-improve-v2-0-5dfd6a89078f@nxp.com>
From: Peng Fan <peng.fan@nxp.com>
Move the register range validation and rbtree construction loop out of
__regmap_init() into a dedicated regmap_init_ranges() helper. This is a
mechanical code movement with no functional change apart from fixing the
block comment style of an existing comment that checkpatch flags when it
appears in the diff context.
Signed-off-by: Peng Fan <peng.fan@nxp.com>
---
drivers/base/regmap/regmap.c | 192 +++++++++++++++++++++++--------------------
1 file changed, 101 insertions(+), 91 deletions(-)
diff --git a/drivers/base/regmap/regmap.c b/drivers/base/regmap/regmap.c
index aca63b0d03e0..eaf7555f9765 100644
--- a/drivers/base/regmap/regmap.c
+++ b/drivers/base/regmap/regmap.c
@@ -748,6 +748,104 @@ static int regmap_set_lock(struct regmap *map, const struct regmap_config *confi
return 0;
}
+/* Validate config->ranges and build the range rbtree. */
+static int regmap_init_ranges(struct regmap *map, const struct regmap_config *config)
+{
+ int i, j;
+
+ map->range_tree = RB_ROOT;
+
+ for (i = 0; i < config->num_ranges; i++) {
+ const struct regmap_range_cfg *range_cfg = &config->ranges[i];
+ struct regmap_range_node *new;
+
+ /* Sanity check */
+ if (range_cfg->range_max < range_cfg->range_min) {
+ dev_err(map->dev, "Invalid range %d: %u < %u\n", i,
+ range_cfg->range_max, range_cfg->range_min);
+ return -EINVAL;
+ }
+
+ if (range_cfg->range_max > map->max_register) {
+ dev_err(map->dev, "Invalid range %d: %u > %u\n", i,
+ range_cfg->range_max, map->max_register);
+ return -EINVAL;
+ }
+
+ if (range_cfg->selector_reg > map->max_register) {
+ dev_err(map->dev,
+ "Invalid range %d: selector out of map\n", i);
+ return -EINVAL;
+ }
+
+ if (range_cfg->window_len == 0) {
+ dev_err(map->dev, "Invalid range %d: window_len 0\n",
+ i);
+ return -EINVAL;
+ }
+
+ /*
+ * Make sure that this register range has no selector
+ * or data window within its boundary.
+ */
+ for (j = 0; j < config->num_ranges; j++) {
+ unsigned int sel_reg = config->ranges[j].selector_reg;
+ unsigned int win_min = config->ranges[j].window_start;
+ unsigned int win_max = win_min +
+ config->ranges[j].window_len - 1;
+
+ /* Allow data window inside its own virtual range */
+ if (j == i)
+ continue;
+
+ if (range_cfg->range_min <= sel_reg &&
+ sel_reg <= range_cfg->range_max) {
+ dev_err(map->dev,
+ "Range %d: selector for %d in window\n",
+ i, j);
+ return -EINVAL;
+ }
+
+ if (!(win_max < range_cfg->range_min ||
+ win_min > range_cfg->range_max)) {
+ dev_err(map->dev,
+ "Range %d: window for %d in window\n",
+ i, j);
+ return -EINVAL;
+ }
+ }
+
+ new = kzalloc_obj(*new);
+ if (new == NULL)
+ return -ENOMEM;
+
+ new->map = map;
+ new->name = range_cfg->name;
+ new->range_min = range_cfg->range_min;
+ new->range_max = range_cfg->range_max;
+ new->selector_reg = range_cfg->selector_reg;
+ new->selector_mask = range_cfg->selector_mask;
+ new->selector_shift = range_cfg->selector_shift;
+ new->window_start = range_cfg->window_start;
+ new->window_len = range_cfg->window_len;
+
+ if (!_regmap_range_add(map, new)) {
+ dev_err(map->dev, "Failed to add range %d\n", i);
+ kfree(new);
+ return -EINVAL;
+ }
+
+ if (map->selector_work_buf == NULL) {
+ map->selector_work_buf =
+ kzalloc(map->format.buf_size, GFP_KERNEL);
+ if (map->selector_work_buf == NULL)
+ return -ENOMEM;
+ }
+ }
+
+ return 0;
+}
+
struct regmap *__regmap_init(struct device *dev,
const struct regmap_bus *bus,
void *bus_context,
@@ -758,7 +856,6 @@ struct regmap *__regmap_init(struct device *dev,
struct regmap *map;
enum regmap_endian reg_endian, val_endian;
bool fast_io;
- int i, j;
int ret;
if (!config) {
@@ -1078,96 +1175,9 @@ struct regmap *__regmap_init(struct device *dev,
skip_format_initialization:
- map->range_tree = RB_ROOT;
- for (i = 0; i < config->num_ranges; i++) {
- const struct regmap_range_cfg *range_cfg = &config->ranges[i];
- struct regmap_range_node *new;
-
- /* Sanity check */
- if (range_cfg->range_max < range_cfg->range_min) {
- dev_err(map->dev, "Invalid range %d: %u < %u\n", i,
- range_cfg->range_max, range_cfg->range_min);
- goto err_range;
- }
-
- if (range_cfg->range_max > map->max_register) {
- dev_err(map->dev, "Invalid range %d: %u > %u\n", i,
- range_cfg->range_max, map->max_register);
- goto err_range;
- }
-
- if (range_cfg->selector_reg > map->max_register) {
- dev_err(map->dev,
- "Invalid range %d: selector out of map\n", i);
- goto err_range;
- }
-
- if (range_cfg->window_len == 0) {
- dev_err(map->dev, "Invalid range %d: window_len 0\n",
- i);
- goto err_range;
- }
-
- /* Make sure, that this register range has no selector
- or data window within its boundary */
- for (j = 0; j < config->num_ranges; j++) {
- unsigned int sel_reg = config->ranges[j].selector_reg;
- unsigned int win_min = config->ranges[j].window_start;
- unsigned int win_max = win_min +
- config->ranges[j].window_len - 1;
-
- /* Allow data window inside its own virtual range */
- if (j == i)
- continue;
-
- if (range_cfg->range_min <= sel_reg &&
- sel_reg <= range_cfg->range_max) {
- dev_err(map->dev,
- "Range %d: selector for %d in window\n",
- i, j);
- goto err_range;
- }
-
- if (!(win_max < range_cfg->range_min ||
- win_min > range_cfg->range_max)) {
- dev_err(map->dev,
- "Range %d: window for %d in window\n",
- i, j);
- goto err_range;
- }
- }
-
- new = kzalloc_obj(*new);
- if (new == NULL) {
- ret = -ENOMEM;
- goto err_range;
- }
-
- new->map = map;
- new->name = range_cfg->name;
- new->range_min = range_cfg->range_min;
- new->range_max = range_cfg->range_max;
- new->selector_reg = range_cfg->selector_reg;
- new->selector_mask = range_cfg->selector_mask;
- new->selector_shift = range_cfg->selector_shift;
- new->window_start = range_cfg->window_start;
- new->window_len = range_cfg->window_len;
-
- if (!_regmap_range_add(map, new)) {
- dev_err(map->dev, "Failed to add range %d\n", i);
- kfree(new);
- goto err_range;
- }
-
- if (map->selector_work_buf == NULL) {
- map->selector_work_buf =
- kzalloc(map->format.buf_size, GFP_KERNEL);
- if (map->selector_work_buf == NULL) {
- ret = -ENOMEM;
- goto err_range;
- }
- }
- }
+ ret = regmap_init_ranges(map, config);
+ if (ret)
+ goto err_range;
ret = regcache_init(map, config);
if (ret != 0)
--
2.56.0
next prev parent reply other threads:[~2026-11-13 15:32 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-13 15:31 [PATCH v2 00/10] regmap: clean up and improve __regmap_init() Peng Fan
2026-11-13 15:31 ` [PATCH v2 01/10] regmap: fix reinit cache failure handling Peng Fan
2026-11-13 15:31 ` [PATCH v2 02/10] regmap-i2c: keep bus raw write limit when applying read quirk Peng Fan
2026-11-13 15:31 ` [PATCH v2 03/10] regmap-i2c: fix write limit clamping for adapter quirks Peng Fan
2026-11-13 15:31 ` [PATCH v2 04/10] regmap: deduplicate fast_io checks Peng Fan
2026-11-13 15:31 ` [PATCH v2 05/10] regmap: factor out regmap_set_lock() Peng Fan
2026-11-13 15:31 ` Peng Fan [this message]
2026-11-13 15:31 ` [PATCH v2 07/10] regmap: factor out format initialisation into helpers Peng Fan
2026-11-13 15:31 ` [PATCH v2 08/10] regmap: drop regmap_bus::free_on_exit Peng Fan
2026-11-13 15:31 ` [PATCH v2 09/10] regmap: use cleanup API in __regmap_init() Peng Fan
2026-11-13 15:31 ` [PATCH v2 10/10] regmap: kunit: add test for regmap_bus::init_limits Peng Fan
2026-11-14 13:01 ` [PATCH v2 00/10] regmap: clean up and improve __regmap_init() Peng Fan
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=20261113-regmap-improve-v2-6-5dfd6a89078f@nxp.com \
--to=peng.fan@linux.dev \
--cc=broonie@kernel.org \
--cc=dakr@kernel.org \
--cc=driver-core@lists.linux.dev \
--cc=gregkh@linuxfoundation.org \
--cc=johan@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=peng.fan@nxp.com \
--cc=rafael@kernel.org \
--cc=tanureal@opensource.cirrus.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.