Skip to content

Commit 0cb7ae9

Browse files
andy-shevbroonie
authored andcommitted
regcache: Allocate and free reg_defaults on the same level
Currently reg_defaults buffer may be allocated on two different levels when the user provided them and we duplicate it in regcache_init() or when user wants us to read back from HW in regcache_hw_init(). This inconsistency makes code harder to follow and maintain. Allocate and free reg_defaults on the same level in regcache_init() to improve the readability and maintenance efforts. Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com> Link: https://patch.msgid.link/20260305085449.3184020-3-andriy.shevchenko@linux.intel.com Signed-off-by: Mark Brown <broonie@kernel.org>
1 parent 8e2d279 commit 0cb7ae9

1 file changed

Lines changed: 15 additions & 19 deletions

File tree

drivers/base/regmap/regcache.c

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -56,28 +56,21 @@ static int regcache_count_cacheable_registers(struct regmap *map)
5656
return count;
5757
}
5858

59-
static int regcache_hw_init(struct regmap *map, int count)
59+
static int regcache_hw_init(struct regmap *map)
6060
{
6161
int ret;
6262
unsigned int reg, val;
6363
void *tmp_buf;
6464

65-
map->num_reg_defaults = count;
66-
map->reg_defaults = kmalloc_objs(struct reg_default, count);
67-
if (!map->reg_defaults)
68-
return -ENOMEM;
69-
7065
if (!map->reg_defaults_raw) {
7166
bool cache_bypass = map->cache_bypass;
7267
dev_dbg(map->dev, "No cache defaults, reading back from HW\n");
7368

7469
/* Bypass the cache access till data read from HW */
7570
map->cache_bypass = true;
7671
tmp_buf = kmalloc(map->cache_size_raw, GFP_KERNEL);
77-
if (!tmp_buf) {
78-
ret = -ENOMEM;
79-
goto err_free;
80-
}
72+
if (!tmp_buf)
73+
return -ENOMEM;
8174
ret = regmap_raw_read(map, 0, tmp_buf,
8275
map->cache_size_raw);
8376
map->cache_bypass = cache_bypass;
@@ -110,7 +103,7 @@ static int regcache_hw_init(struct regmap *map, int count)
110103
if (ret != 0) {
111104
dev_err(map->dev, "Failed to read %x: %d\n",
112105
reg, ret);
113-
goto err_free;
106+
return ret;
114107
}
115108
}
116109

@@ -120,16 +113,10 @@ static int regcache_hw_init(struct regmap *map, int count)
120113
}
121114

122115
return 0;
123-
124-
err_free:
125-
kfree(map->reg_defaults);
126-
127-
return ret;
128116
}
129117

130118
static void regcache_hw_exit(struct regmap *map)
131119
{
132-
kfree(map->reg_defaults);
133120
if (map->cache_free)
134121
kfree(map->reg_defaults_raw);
135122
}
@@ -209,13 +196,18 @@ int regcache_init(struct regmap *map, const struct regmap_config *config)
209196
if (map->cache_bypass)
210197
return 0;
211198

199+
map->num_reg_defaults = count;
200+
map->reg_defaults = kmalloc_objs(struct reg_default, count);
201+
if (!map->reg_defaults)
202+
return -ENOMEM;
203+
212204
/* Some devices such as PMICs don't have cache defaults,
213205
* we cope with this by reading back the HW registers and
214206
* crafting the cache defaults by hand.
215207
*/
216-
ret = regcache_hw_init(map, count);
208+
ret = regcache_hw_init(map);
217209
if (ret < 0)
218-
return ret;
210+
goto err_free_reg_defaults;
219211
}
220212

221213
if (!map->max_register_is_set && map->num_reg_defaults_raw) {
@@ -253,6 +245,8 @@ int regcache_init(struct regmap *map, const struct regmap_config *config)
253245
}
254246
err_free:
255247
regcache_hw_exit(map);
248+
err_free_reg_defaults:
249+
kfree(map->reg_defaults);
256250

257251
return ret;
258252
}
@@ -273,6 +267,8 @@ void regcache_exit(struct regmap *map)
273267
map->cache_ops->exit(map);
274268
map->unlock(map->lock_arg);
275269
}
270+
271+
kfree(map->reg_defaults);
276272
}
277273

278274
/**

0 commit comments

Comments
 (0)