Skip to content

Commit 8e25831

Browse files
committed
Merge tag 'regmap-v7.1' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie/regmap
Pull regmap updates from Mark Brown: "This has been quite a busy release for regmap, the user visible changes are quite minor but there's some quite good work on internal code improvements: - Cleanup helper for __free()ing regmap_fields - Support non-devm I3C regmaps - A bunch of cleanup work, mostly from Andy Shevchenko - Fix for bootstrapping issues with hardware initialised regmaps, which was the main inspiration for some of the cleanups" * tag 'regmap-v7.1' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie/regmap: regmap: i3c: Add non-devm regmap_init_i3c() helper regmap: debugfs: fix race condition in dummy name allocation regmap: Synchronize cache for the page selector regmap: Simplify devres handling regcache: Move HW readback after cache initialisation regcache: Allocate and free reg_defaults on the same level regcache: Move count check and cache_bypass assignment to the caller regcache: Factor out regcache_hw_exit() helper regcache: Amend printf() specifiers when printing registers regcache: Define iterator inside for-loop and align their types regmap: define cleanup helper for regmap_field regmap: sort header includes regcache: Split regcache_count_cacheable_registers() helper regcache: Remove duplicate check in regcache_hw_init()
2 parents e41a25c + 8ad7f3b commit 8e25831

6 files changed

Lines changed: 118 additions & 73 deletions

File tree

drivers/base/regmap/internal.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ struct regmap {
8484
bool debugfs_disable;
8585
struct dentry *debugfs;
8686
const char *debugfs_name;
87+
int debugfs_dummy_id;
8788

8889
unsigned int debugfs_reg_len;
8990
unsigned int debugfs_val_len;
@@ -162,7 +163,7 @@ struct regmap {
162163
bool no_sync_defaults;
163164

164165
struct reg_sequence *patch;
165-
int patch_regs;
166+
unsigned int patch_regs;
166167

167168
/* if set, the regmap core can sleep */
168169
bool can_sleep;

drivers/base/regmap/regcache.c

Lines changed: 54 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -42,33 +42,25 @@ void regcache_sort_defaults(struct reg_default *defaults, unsigned int ndefaults
4242
}
4343
EXPORT_SYMBOL_GPL(regcache_sort_defaults);
4444

45-
static int regcache_hw_init(struct regmap *map)
45+
static int regcache_count_cacheable_registers(struct regmap *map)
4646
{
47-
int i, j;
48-
int ret;
49-
int count;
50-
unsigned int reg, val;
51-
void *tmp_buf;
52-
53-
if (!map->num_reg_defaults_raw)
54-
return -EINVAL;
47+
unsigned int count;
5548

5649
/* calculate the size of reg_defaults */
57-
for (count = 0, i = 0; i < map->num_reg_defaults_raw; i++)
50+
count = 0;
51+
for (unsigned int i = 0; i < map->num_reg_defaults_raw; i++)
5852
if (regmap_readable(map, i * map->reg_stride) &&
5953
!regmap_volatile(map, i * map->reg_stride))
6054
count++;
6155

62-
/* all registers are unreadable or volatile, so just bypass */
63-
if (!count) {
64-
map->cache_bypass = true;
65-
return 0;
66-
}
56+
return count;
57+
}
6758

68-
map->num_reg_defaults = count;
69-
map->reg_defaults = kmalloc_objs(struct reg_default, count);
70-
if (!map->reg_defaults)
71-
return -ENOMEM;
59+
static int regcache_hw_init(struct regmap *map)
60+
{
61+
int ret;
62+
unsigned int reg, val;
63+
void *tmp_buf;
7264

7365
if (!map->reg_defaults_raw) {
7466
bool cache_bypass = map->cache_bypass;
@@ -77,10 +69,8 @@ static int regcache_hw_init(struct regmap *map)
7769
/* Bypass the cache access till data read from HW */
7870
map->cache_bypass = true;
7971
tmp_buf = kmalloc(map->cache_size_raw, GFP_KERNEL);
80-
if (!tmp_buf) {
81-
ret = -ENOMEM;
82-
goto err_free;
83-
}
72+
if (!tmp_buf)
73+
return -ENOMEM;
8474
ret = regmap_raw_read(map, 0, tmp_buf,
8575
map->cache_size_raw);
8676
map->cache_bypass = cache_bypass;
@@ -93,7 +83,7 @@ static int regcache_hw_init(struct regmap *map)
9383
}
9484

9585
/* fill the reg_defaults */
96-
for (i = 0, j = 0; i < map->num_reg_defaults_raw; i++) {
86+
for (unsigned int i = 0, j = 0; i < map->num_reg_defaults_raw; i++) {
9787
reg = i * map->reg_stride;
9888

9989
if (!regmap_readable(map, reg))
@@ -111,9 +101,9 @@ static int regcache_hw_init(struct regmap *map)
111101
ret = regmap_read(map, reg, &val);
112102
map->cache_bypass = cache_bypass;
113103
if (ret != 0) {
114-
dev_err(map->dev, "Failed to read %d: %d\n",
104+
dev_err(map->dev, "Failed to read %x: %d\n",
115105
reg, ret);
116-
goto err_free;
106+
return ret;
117107
}
118108
}
119109

@@ -123,15 +113,17 @@ static int regcache_hw_init(struct regmap *map)
123113
}
124114

125115
return 0;
116+
}
126117

127-
err_free:
128-
kfree(map->reg_defaults);
129-
130-
return ret;
118+
static void regcache_hw_exit(struct regmap *map)
119+
{
120+
if (map->cache_free)
121+
kfree(map->reg_defaults_raw);
131122
}
132123

133124
int regcache_init(struct regmap *map, const struct regmap_config *config)
134125
{
126+
int count = 0;
135127
int ret;
136128
int i;
137129
void *tmp_buf;
@@ -196,15 +188,18 @@ int regcache_init(struct regmap *map, const struct regmap_config *config)
196188
return -ENOMEM;
197189
map->reg_defaults = tmp_buf;
198190
} else if (map->num_reg_defaults_raw) {
199-
/* Some devices such as PMICs don't have cache defaults,
200-
* we cope with this by reading back the HW registers and
201-
* crafting the cache defaults by hand.
202-
*/
203-
ret = regcache_hw_init(map);
204-
if (ret < 0)
205-
return ret;
191+
count = regcache_count_cacheable_registers(map);
192+
if (!count)
193+
map->cache_bypass = true;
194+
195+
/* All registers are unreadable or volatile, so just bypass */
206196
if (map->cache_bypass)
207197
return 0;
198+
199+
map->num_reg_defaults = count;
200+
map->reg_defaults = kmalloc_objs(struct reg_default, count);
201+
if (!map->reg_defaults)
202+
return -ENOMEM;
208203
}
209204

210205
if (!map->max_register_is_set && map->num_reg_defaults_raw) {
@@ -219,7 +214,18 @@ int regcache_init(struct regmap *map, const struct regmap_config *config)
219214
ret = map->cache_ops->init(map);
220215
map->unlock(map->lock_arg);
221216
if (ret)
222-
goto err_free;
217+
goto err_free_reg_defaults;
218+
}
219+
220+
/*
221+
* Some devices such as PMICs don't have cache defaults,
222+
* we cope with this by reading back the HW registers and
223+
* crafting the cache defaults by hand.
224+
*/
225+
if (count) {
226+
ret = regcache_hw_init(map);
227+
if (ret)
228+
goto err_exit;
223229
}
224230

225231
if (map->cache_ops->populate &&
@@ -229,21 +235,21 @@ int regcache_init(struct regmap *map, const struct regmap_config *config)
229235
ret = map->cache_ops->populate(map);
230236
map->unlock(map->lock_arg);
231237
if (ret)
232-
goto err_exit;
238+
goto err_free;
233239
}
234240
return 0;
235241

242+
err_free:
243+
regcache_hw_exit(map);
236244
err_exit:
237245
if (map->cache_ops->exit) {
238246
dev_dbg(map->dev, "Destroying %s cache\n", map->cache_ops->name);
239247
map->lock(map->lock_arg);
240248
ret = map->cache_ops->exit(map);
241249
map->unlock(map->lock_arg);
242250
}
243-
err_free:
251+
err_free_reg_defaults:
244252
kfree(map->reg_defaults);
245-
if (map->cache_free)
246-
kfree(map->reg_defaults_raw);
247253

248254
return ret;
249255
}
@@ -255,9 +261,7 @@ void regcache_exit(struct regmap *map)
255261

256262
BUG_ON(!map->cache_ops);
257263

258-
kfree(map->reg_defaults);
259-
if (map->cache_free)
260-
kfree(map->reg_defaults_raw);
264+
regcache_hw_exit(map);
261265

262266
if (map->cache_ops->exit) {
263267
dev_dbg(map->dev, "Destroying %s cache\n",
@@ -266,6 +270,8 @@ void regcache_exit(struct regmap *map)
266270
map->cache_ops->exit(map);
267271
map->unlock(map->lock_arg);
268272
}
273+
274+
kfree(map->reg_defaults);
269275
}
270276

271277
/**
@@ -504,7 +510,7 @@ int regcache_sync_region(struct regmap *map, unsigned int min,
504510
bypass = map->cache_bypass;
505511

506512
name = map->cache_ops->name;
507-
dev_dbg(map->dev, "Syncing %s cache from %d-%d\n", name, min, max);
513+
dev_dbg(map->dev, "Syncing %s cache from %#x-%#x\n", name, min, max);
508514

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

@@ -835,13 +841,13 @@ static int regcache_sync_block_raw(struct regmap *map, void *block,
835841
unsigned int block_base, unsigned int start,
836842
unsigned int end)
837843
{
838-
unsigned int i, val;
839844
unsigned int regtmp = 0;
840845
unsigned int base = 0;
841846
const void *data = NULL;
847+
unsigned int val;
842848
int ret;
843849

844-
for (i = start; i < end; i++) {
850+
for (unsigned int i = start; i < end; i++) {
845851
regtmp = block_base + (i * map->reg_stride);
846852

847853
if (!regcache_reg_present(cache_present, i) ||

drivers/base/regmap/regmap-debugfs.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <linux/uaccess.h>
1313
#include <linux/device.h>
1414
#include <linux/list.h>
15+
#include <linux/idr.h>
1516

1617
#include "internal.h"
1718

@@ -20,7 +21,7 @@ struct regmap_debugfs_node {
2021
struct list_head link;
2122
};
2223

23-
static unsigned int dummy_index;
24+
static DEFINE_IDA(dummy_ida);
2425
static struct dentry *regmap_debugfs_root;
2526
static LIST_HEAD(regmap_debugfs_early_list);
2627
static DEFINE_MUTEX(regmap_debugfs_early_lock);
@@ -539,6 +540,7 @@ void regmap_debugfs_init(struct regmap *map)
539540
struct regmap_range_node *range_node;
540541
const char *devname = "dummy";
541542
const char *name = map->name;
543+
int id;
542544

543545
/*
544546
* Userspace can initiate reads from the hardware over debugfs.
@@ -567,6 +569,7 @@ void regmap_debugfs_init(struct regmap *map)
567569

568570
INIT_LIST_HEAD(&map->debugfs_off_cache);
569571
mutex_init(&map->cache_lock);
572+
map->debugfs_dummy_id = -1;
570573

571574
if (map->dev)
572575
devname = dev_name(map->dev);
@@ -585,12 +588,16 @@ void regmap_debugfs_init(struct regmap *map)
585588

586589
if (!strcmp(name, "dummy")) {
587590
kfree(map->debugfs_name);
588-
map->debugfs_name = kasprintf(GFP_KERNEL, "dummy%d",
589-
dummy_index);
590-
if (!map->debugfs_name)
591+
id = ida_alloc(&dummy_ida, GFP_KERNEL);
592+
if (id < 0)
591593
return;
594+
map->debugfs_name = kasprintf(GFP_KERNEL, "dummy%d", id);
595+
if (!map->debugfs_name) {
596+
ida_free(&dummy_ida, id);
597+
return;
598+
}
599+
map->debugfs_dummy_id = id;
592600
name = map->debugfs_name;
593-
dummy_index++;
594601
}
595602

596603
map->debugfs = debugfs_create_dir(name, regmap_debugfs_root);
@@ -660,6 +667,10 @@ void regmap_debugfs_exit(struct regmap *map)
660667
mutex_lock(&map->cache_lock);
661668
regmap_debugfs_free_dump_cache(map);
662669
mutex_unlock(&map->cache_lock);
670+
if (map->debugfs_dummy_id >= 0) {
671+
ida_free(&dummy_ida, map->debugfs_dummy_id);
672+
map->debugfs_dummy_id = -1;
673+
}
663674
kfree(map->debugfs_name);
664675
map->debugfs_name = NULL;
665676
} else {

drivers/base/regmap/regmap-i3c.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ static const struct regmap_bus regmap_i3c = {
4646
.read = regmap_i3c_read,
4747
};
4848

49+
struct regmap *__regmap_init_i3c(struct i3c_device *i3c,
50+
const struct regmap_config *config,
51+
struct lock_class_key *lock_key,
52+
const char *lock_name)
53+
{
54+
return __regmap_init(&i3c->dev, &regmap_i3c, &i3c->dev, config,
55+
lock_key, lock_name);
56+
}
57+
EXPORT_SYMBOL_GPL(__regmap_init_i3c);
58+
4959
struct regmap *__devm_regmap_init_i3c(struct i3c_device *i3c,
5060
const struct regmap_config *config,
5161
struct lock_class_key *lock_key,

drivers/base/regmap/regmap.c

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,9 +1182,9 @@ struct regmap *__regmap_init(struct device *dev,
11821182
}
11831183
EXPORT_SYMBOL_GPL(__regmap_init);
11841184

1185-
static void devm_regmap_release(struct device *dev, void *res)
1185+
static void devm_regmap_release(void *regmap)
11861186
{
1187-
regmap_exit(*(struct regmap **)res);
1187+
regmap_exit(regmap);
11881188
}
11891189

11901190
struct regmap *__devm_regmap_init(struct device *dev,
@@ -1194,20 +1194,17 @@ struct regmap *__devm_regmap_init(struct device *dev,
11941194
struct lock_class_key *lock_key,
11951195
const char *lock_name)
11961196
{
1197-
struct regmap **ptr, *regmap;
1198-
1199-
ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL);
1200-
if (!ptr)
1201-
return ERR_PTR(-ENOMEM);
1197+
struct regmap *regmap;
1198+
int ret;
12021199

12031200
regmap = __regmap_init(dev, bus, bus_context, config,
12041201
lock_key, lock_name);
1205-
if (!IS_ERR(regmap)) {
1206-
*ptr = regmap;
1207-
devres_add(dev, ptr);
1208-
} else {
1209-
devres_free(ptr);
1210-
}
1202+
if (IS_ERR(regmap))
1203+
return regmap;
1204+
1205+
ret = devm_add_action_or_reset(dev, devm_regmap_release, regmap);
1206+
if (ret)
1207+
return ERR_PTR(ret);
12111208

12121209
return regmap;
12131210
}

0 commit comments

Comments
 (0)