Skip to content

Commit 0135bbc

Browse files
nvswarrenbroonie
authored andcommitted
regmap: introduce explicit bus_context for bus callbacks
The only context needed by I2C and SPI bus definitions is the device itself; this can be converted to an i2c_client or spi_device in order to perform IO on the device. However, other bus types may need more context in order to perform IO. Enable this by having regmap_init accept a bus_context parameter, and pass this to all bus callbacks. The existing callbacks simply pass the struct device here. Future bus types may pass something else. Signed-off-by: Stephen Warren <[email protected]> Signed-off-by: Mark Brown <[email protected]>
1 parent dd775ae commit 0135bbc

File tree

5 files changed

+37
-19
lines changed

5 files changed

+37
-19
lines changed

drivers/base/regmap/internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ struct regmap {
3838
void *work_buf; /* Scratch buffer used to format I/O */
3939
struct regmap_format format; /* Buffer format */
4040
const struct regmap_bus *bus;
41+
void *bus_context;
4142

4243
#ifdef CONFIG_DEBUG_FS
4344
struct dentry *debugfs;

drivers/base/regmap/regmap-i2c.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
#include <linux/module.h>
1616
#include <linux/init.h>
1717

18-
static int regmap_i2c_write(struct device *dev, const void *data, size_t count)
18+
static int regmap_i2c_write(void *context, const void *data, size_t count)
1919
{
20+
struct device *dev = context;
2021
struct i2c_client *i2c = to_i2c_client(dev);
2122
int ret;
2223

@@ -29,10 +30,11 @@ static int regmap_i2c_write(struct device *dev, const void *data, size_t count)
2930
return -EIO;
3031
}
3132

32-
static int regmap_i2c_gather_write(struct device *dev,
33+
static int regmap_i2c_gather_write(void *context,
3334
const void *reg, size_t reg_size,
3435
const void *val, size_t val_size)
3536
{
37+
struct device *dev = context;
3638
struct i2c_client *i2c = to_i2c_client(dev);
3739
struct i2c_msg xfer[2];
3840
int ret;
@@ -62,10 +64,11 @@ static int regmap_i2c_gather_write(struct device *dev,
6264
return -EIO;
6365
}
6466

65-
static int regmap_i2c_read(struct device *dev,
67+
static int regmap_i2c_read(void *context,
6668
const void *reg, size_t reg_size,
6769
void *val, size_t val_size)
6870
{
71+
struct device *dev = context;
6972
struct i2c_client *i2c = to_i2c_client(dev);
7073
struct i2c_msg xfer[2];
7174
int ret;
@@ -107,7 +110,7 @@ static struct regmap_bus regmap_i2c = {
107110
struct regmap *regmap_init_i2c(struct i2c_client *i2c,
108111
const struct regmap_config *config)
109112
{
110-
return regmap_init(&i2c->dev, &regmap_i2c, config);
113+
return regmap_init(&i2c->dev, &regmap_i2c, &i2c->dev, config);
111114
}
112115
EXPORT_SYMBOL_GPL(regmap_init_i2c);
113116

@@ -124,7 +127,7 @@ EXPORT_SYMBOL_GPL(regmap_init_i2c);
124127
struct regmap *devm_regmap_init_i2c(struct i2c_client *i2c,
125128
const struct regmap_config *config)
126129
{
127-
return devm_regmap_init(&i2c->dev, &regmap_i2c, config);
130+
return devm_regmap_init(&i2c->dev, &regmap_i2c, &i2c->dev, config);
128131
}
129132
EXPORT_SYMBOL_GPL(devm_regmap_init_i2c);
130133

drivers/base/regmap/regmap-spi.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,19 @@
1515
#include <linux/init.h>
1616
#include <linux/module.h>
1717

18-
static int regmap_spi_write(struct device *dev, const void *data, size_t count)
18+
static int regmap_spi_write(void *context, const void *data, size_t count)
1919
{
20+
struct device *dev = context;
2021
struct spi_device *spi = to_spi_device(dev);
2122

2223
return spi_write(spi, data, count);
2324
}
2425

25-
static int regmap_spi_gather_write(struct device *dev,
26+
static int regmap_spi_gather_write(void *context,
2627
const void *reg, size_t reg_len,
2728
const void *val, size_t val_len)
2829
{
30+
struct device *dev = context;
2931
struct spi_device *spi = to_spi_device(dev);
3032
struct spi_message m;
3133
struct spi_transfer t[2] = { { .tx_buf = reg, .len = reg_len, },
@@ -38,10 +40,11 @@ static int regmap_spi_gather_write(struct device *dev,
3840
return spi_sync(spi, &m);
3941
}
4042

41-
static int regmap_spi_read(struct device *dev,
43+
static int regmap_spi_read(void *context,
4244
const void *reg, size_t reg_size,
4345
void *val, size_t val_size)
4446
{
47+
struct device *dev = context;
4548
struct spi_device *spi = to_spi_device(dev);
4649

4750
return spi_write_then_read(spi, reg, reg_size, val, val_size);
@@ -66,7 +69,7 @@ static struct regmap_bus regmap_spi = {
6669
struct regmap *regmap_init_spi(struct spi_device *spi,
6770
const struct regmap_config *config)
6871
{
69-
return regmap_init(&spi->dev, &regmap_spi, config);
72+
return regmap_init(&spi->dev, &regmap_spi, &spi->dev, config);
7073
}
7174
EXPORT_SYMBOL_GPL(regmap_init_spi);
7275

@@ -83,7 +86,7 @@ EXPORT_SYMBOL_GPL(regmap_init_spi);
8386
struct regmap *devm_regmap_init_spi(struct spi_device *spi,
8487
const struct regmap_config *config)
8588
{
86-
return devm_regmap_init(&spi->dev, &regmap_spi, config);
89+
return devm_regmap_init(&spi->dev, &regmap_spi, &spi->dev, config);
8790
}
8891
EXPORT_SYMBOL_GPL(devm_regmap_init_spi);
8992

drivers/base/regmap/regmap.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ static unsigned int regmap_parse_32(void *buf)
163163
*
164164
* @dev: Device that will be interacted with
165165
* @bus: Bus-specific callbacks to use with device
166+
* @bus_context: Data passed to bus-specific callbacks
166167
* @config: Configuration for register map
167168
*
168169
* The return value will be an ERR_PTR() on error or a valid pointer to
@@ -171,6 +172,7 @@ static unsigned int regmap_parse_32(void *buf)
171172
*/
172173
struct regmap *regmap_init(struct device *dev,
173174
const struct regmap_bus *bus,
175+
void *bus_context,
174176
const struct regmap_config *config)
175177
{
176178
struct regmap *map;
@@ -193,6 +195,7 @@ struct regmap *regmap_init(struct device *dev,
193195
map->format.buf_size += map->format.pad_bytes;
194196
map->dev = dev;
195197
map->bus = bus;
198+
map->bus_context = bus_context;
196199
map->max_register = config->max_register;
197200
map->writeable_reg = config->writeable_reg;
198201
map->readable_reg = config->readable_reg;
@@ -316,6 +319,7 @@ static void devm_regmap_release(struct device *dev, void *res)
316319
*
317320
* @dev: Device that will be interacted with
318321
* @bus: Bus-specific callbacks to use with device
322+
* @bus_context: Data passed to bus-specific callbacks
319323
* @config: Configuration for register map
320324
*
321325
* The return value will be an ERR_PTR() on error or a valid pointer
@@ -325,6 +329,7 @@ static void devm_regmap_release(struct device *dev, void *res)
325329
*/
326330
struct regmap *devm_regmap_init(struct device *dev,
327331
const struct regmap_bus *bus,
332+
void *bus_context,
328333
const struct regmap_config *config)
329334
{
330335
struct regmap **ptr, *regmap;
@@ -333,7 +338,7 @@ struct regmap *devm_regmap_init(struct device *dev,
333338
if (!ptr)
334339
return ERR_PTR(-ENOMEM);
335340

336-
regmap = regmap_init(dev, bus, config);
341+
regmap = regmap_init(dev, bus, bus_context, config);
337342
if (!IS_ERR(regmap)) {
338343
*ptr = regmap;
339344
devres_add(dev, ptr);
@@ -391,6 +396,8 @@ void regmap_exit(struct regmap *map)
391396
{
392397
regcache_exit(map);
393398
regmap_debugfs_exit(map);
399+
if (map->bus->free_context)
400+
map->bus->free_context(map->bus_context);
394401
kfree(map->work_buf);
395402
kfree(map);
396403
}
@@ -444,12 +451,12 @@ static int _regmap_raw_write(struct regmap *map, unsigned int reg,
444451
*/
445452
if (val == (map->work_buf + map->format.pad_bytes +
446453
map->format.reg_bytes))
447-
ret = map->bus->write(map->dev, map->work_buf,
454+
ret = map->bus->write(map->bus_context, map->work_buf,
448455
map->format.reg_bytes +
449456
map->format.pad_bytes +
450457
val_len);
451458
else if (map->bus->gather_write)
452-
ret = map->bus->gather_write(map->dev, map->work_buf,
459+
ret = map->bus->gather_write(map->bus_context, map->work_buf,
453460
map->format.reg_bytes +
454461
map->format.pad_bytes,
455462
val, val_len);
@@ -464,7 +471,7 @@ static int _regmap_raw_write(struct regmap *map, unsigned int reg,
464471
memcpy(buf, map->work_buf, map->format.reg_bytes);
465472
memcpy(buf + map->format.reg_bytes + map->format.pad_bytes,
466473
val, val_len);
467-
ret = map->bus->write(map->dev, buf, len);
474+
ret = map->bus->write(map->bus_context, buf, len);
468475

469476
kfree(buf);
470477
}
@@ -498,7 +505,7 @@ int _regmap_write(struct regmap *map, unsigned int reg,
498505

499506
trace_regmap_hw_write_start(map->dev, reg, 1);
500507

501-
ret = map->bus->write(map->dev, map->work_buf,
508+
ret = map->bus->write(map->bus_context, map->work_buf,
502509
map->format.buf_size);
503510

504511
trace_regmap_hw_write_done(map->dev, reg, 1);
@@ -639,7 +646,7 @@ static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
639646
trace_regmap_hw_read_start(map->dev, reg,
640647
val_len / map->format.val_bytes);
641648

642-
ret = map->bus->read(map->dev, map->work_buf,
649+
ret = map->bus->read(map->bus_context, map->work_buf,
643650
map->format.reg_bytes + map->format.pad_bytes,
644651
val, val_len);
645652

include/linux/regmap.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,15 @@ struct regmap_config {
9797
u8 write_flag_mask;
9898
};
9999

100-
typedef int (*regmap_hw_write)(struct device *dev, const void *data,
100+
typedef int (*regmap_hw_write)(void *context, const void *data,
101101
size_t count);
102-
typedef int (*regmap_hw_gather_write)(struct device *dev,
102+
typedef int (*regmap_hw_gather_write)(void *context,
103103
const void *reg, size_t reg_len,
104104
const void *val, size_t val_len);
105-
typedef int (*regmap_hw_read)(struct device *dev,
105+
typedef int (*regmap_hw_read)(void *context,
106106
const void *reg_buf, size_t reg_size,
107107
void *val_buf, size_t val_size);
108+
typedef void (*regmap_hw_free_context)(void *context);
108109

109110
/**
110111
* Description of a hardware bus for the register map infrastructure.
@@ -121,11 +122,13 @@ struct regmap_bus {
121122
regmap_hw_write write;
122123
regmap_hw_gather_write gather_write;
123124
regmap_hw_read read;
125+
regmap_hw_free_context free_context;
124126
u8 read_flag_mask;
125127
};
126128

127129
struct regmap *regmap_init(struct device *dev,
128130
const struct regmap_bus *bus,
131+
void *bus_context,
129132
const struct regmap_config *config);
130133
struct regmap *regmap_init_i2c(struct i2c_client *i2c,
131134
const struct regmap_config *config);
@@ -134,6 +137,7 @@ struct regmap *regmap_init_spi(struct spi_device *dev,
134137

135138
struct regmap *devm_regmap_init(struct device *dev,
136139
const struct regmap_bus *bus,
140+
void *bus_context,
137141
const struct regmap_config *config);
138142
struct regmap *devm_regmap_init_i2c(struct i2c_client *i2c,
139143
const struct regmap_config *config);

0 commit comments

Comments
 (0)