Skip to content

Commit 76f55c9

Browse files
committed
Merge remote-tracking branch 'regmap/for-next' into sound/upstream-20260415
2 parents 2f846c1 + a0508e5 commit 76f55c9

7 files changed

Lines changed: 167 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-i2c.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,50 @@ static const struct regmap_bus regmap_i2c_smbus_i2c_block_reg16 = {
303303
.max_raw_write = I2C_SMBUS_BLOCK_MAX - 2,
304304
};
305305

306+
/*
307+
* SMBus byte/word reg16 support for adapters that have SMBUS_BYTE_DATA
308+
* and SMBUS_WORD_DATA but lack I2C_FUNC_I2C and I2C_FUNC_SMBUS_I2C_BLOCK,
309+
* such as the AMD PIIX4.
310+
*
311+
* READ: set 16-bit EEPROM address via write_byte_data(addr_lo, addr_hi),
312+
* then sequentially read bytes via read_byte() (EEPROM auto-
313+
* increments the address pointer). Same as the I2C-block reg16
314+
* read path above.
315+
*
316+
* WRITE: encode the low address byte and data into a word transaction:
317+
* write_word_data(addr_hi, (data_byte << 8) | addr_lo).
318+
* Only single-byte writes are supported (one value per transaction).
319+
*/
320+
static int regmap_smbus_word_write_reg16(void *context, const void *data,
321+
size_t count)
322+
{
323+
struct device *dev = context;
324+
struct i2c_client *i2c = to_i2c_client(dev);
325+
u8 addr_hi, addr_lo, val;
326+
327+
/*
328+
* data layout: [addr_hi, addr_lo, val0, val1, ...].
329+
* Only single-byte value writes are supported; multi-byte would
330+
* require raw I2C (or repeated word writes with incrementing address).
331+
*/
332+
if (count != 3)
333+
return -EINVAL;
334+
335+
addr_hi = ((u8 *)data)[0];
336+
addr_lo = ((u8 *)data)[1];
337+
val = ((u8 *)data)[2];
338+
339+
return i2c_smbus_write_word_data(i2c, addr_hi,
340+
cpu_to_le16(((u16)val << 8) | addr_lo));
341+
}
342+
343+
static const struct regmap_bus regmap_smbus_byte_word_reg16 = {
344+
.write = regmap_smbus_word_write_reg16,
345+
.read = regmap_i2c_smbus_i2c_read_reg16,
346+
.max_raw_read = I2C_SMBUS_BLOCK_MAX - 2,
347+
.max_raw_write = 1,
348+
};
349+
306350
static const struct regmap_bus *regmap_get_i2c_bus(struct i2c_client *i2c,
307351
const struct regmap_config *config)
308352
{
@@ -321,6 +365,11 @@ static const struct regmap_bus *regmap_get_i2c_bus(struct i2c_client *i2c,
321365
i2c_check_functionality(i2c->adapter,
322366
I2C_FUNC_SMBUS_I2C_BLOCK))
323367
bus = &regmap_i2c_smbus_i2c_block_reg16;
368+
else if (config->val_bits == 8 && config->reg_bits == 16 &&
369+
i2c_check_functionality(i2c->adapter,
370+
I2C_FUNC_SMBUS_BYTE_DATA |
371+
I2C_FUNC_SMBUS_WORD_DATA))
372+
bus = &regmap_smbus_byte_word_reg16;
324373
else if (config->val_bits == 16 && config->reg_bits == 8 &&
325374
i2c_check_functionality(i2c->adapter,
326375
I2C_FUNC_SMBUS_WORD_DATA))

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,

0 commit comments

Comments
 (0)