[PATCH v2 2/7] regcache: extract __regcache_sync() to deduplicate sync dispatch

From: Peng Fan (OSS)

Date: Fri Oct 02 2026 - 09:21:10 EST


From: Peng Fan <peng.fan@xxxxxxx>

regcache_sync() and regcache_sync_region() both open-code the same
pattern: check cache_dirty, then dispatch to either cache_ops->sync()
or regcache_default_sync().

Extract this into a static __regcache_sync() helper that both callers
share. The helper checks cache_dirty and returns 0 early when the
cache is clean, dispatches to the backend sync callback when present,
and falls back to regcache_default_sync() otherwise.

In regcache_sync_region(), the cache_dirty early-exit previously
skipped setting map->async. With the helper, map->async is set to
true before the call and restored to false unconditionally afterward,
so when cache_dirty is false the flag is toggled and immediately
restored - functionally equivalent.

No functional change.

Assisted-by: LLM
Signed-off-by: Peng Fan <peng.fan@xxxxxxx>
---
drivers/base/regmap/regcache.c | 28 +++++++++++++++-------------
1 file changed, 15 insertions(+), 13 deletions(-)

diff --git a/drivers/base/regmap/regcache.c b/drivers/base/regmap/regcache.c
index ebfa27bb6f54..b486cd826b3c 100644
--- a/drivers/base/regmap/regcache.c
+++ b/drivers/base/regmap/regcache.c
@@ -22,6 +22,8 @@ static const struct regcache_ops *cache_types[] = {
&regcache_flat_ops,
};

+static int regcache_default_sync(struct regmap *map, unsigned int min, unsigned int max);
+
static int regcache_defaults_cmp(const void *a, const void *b)
{
const struct reg_default *x = a;
@@ -143,6 +145,17 @@ static void regcache_locked_exit(struct regmap *map)
map->cache_ops->exit(map);
}

+static int __regcache_sync(struct regmap *map, unsigned int min, unsigned int max)
+{
+ if (!map->cache_dirty)
+ return 0;
+
+ if (map->cache_ops->sync)
+ return map->cache_ops->sync(map, min, max);
+
+ return regcache_default_sync(map, min, max);
+}
+
int regcache_init(struct regmap *map, const struct regmap_config *config)
{
bool sort_defaults = false;
@@ -461,11 +474,7 @@ int regcache_sync(struct regmap *map)
}
map->cache_bypass = false;

- if (map->cache_ops->sync)
- sync_ret = map->cache_ops->sync(map, 0, map->max_register);
- else
- sync_ret = regcache_default_sync(map, 0, map->max_register);
-
+ sync_ret = __regcache_sync(map, 0, map->max_register);
if (sync_ret == 0)
map->cache_dirty = false;

@@ -545,17 +554,10 @@ int regcache_sync_region(struct regmap *map, unsigned int min,

trace_regcache_sync(map, name, "start region");

- if (!map->cache_dirty)
- goto out;
-
map->async = true;

- if (map->cache_ops->sync)
- ret = map->cache_ops->sync(map, min, max);
- else
- ret = regcache_default_sync(map, min, max);
+ ret = __regcache_sync(map, min, max);

-out:
/* Restore the bypass state */
map->cache_bypass = bypass;
map->async = false;

--
2.51.0