From 7ca8aa98e2ba76274b6e4e20390f740fd1207fd8 Mon Sep 17 00:00:00 2001 From: Peng Fan Date: Fri, 2 Oct 2026 21:11:18 +0800 Subject: [PATCH] regcache: extract __regcache_sync() to deduplicate sync dispatch 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 Link: https://patch.msgid.link/20261002-regmap-lock-2nd-v2-2-e172b0501c52@nxp.com Signed-off-by: Mark Brown --- 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[] = { ®cache_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;