1 // SPDX-License-Identifier: GPL-2.0
3 // Register map access API - I2C support
5 // Copyright 2011 Wolfson Microelectronics plc
7 // Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
9 #include <linux/regmap.h>
10 #include <linux/i2c.h>
11 #include <linux/module.h>
15 static int regmap_smbus_byte_reg_read(void *context
, unsigned int reg
,
18 struct device
*dev
= context
;
19 struct i2c_client
*i2c
= to_i2c_client(dev
);
25 ret
= i2c_smbus_read_byte_data(i2c
, reg
);
34 static int regmap_smbus_byte_reg_write(void *context
, unsigned int reg
,
37 struct device
*dev
= context
;
38 struct i2c_client
*i2c
= to_i2c_client(dev
);
40 if (val
> 0xff || reg
> 0xff)
43 return i2c_smbus_write_byte_data(i2c
, reg
, val
);
46 static struct regmap_bus regmap_smbus_byte
= {
47 .reg_write
= regmap_smbus_byte_reg_write
,
48 .reg_read
= regmap_smbus_byte_reg_read
,
51 static int regmap_smbus_word_reg_read(void *context
, unsigned int reg
,
54 struct device
*dev
= context
;
55 struct i2c_client
*i2c
= to_i2c_client(dev
);
61 ret
= i2c_smbus_read_word_data(i2c
, reg
);
70 static int regmap_smbus_word_reg_write(void *context
, unsigned int reg
,
73 struct device
*dev
= context
;
74 struct i2c_client
*i2c
= to_i2c_client(dev
);
76 if (val
> 0xffff || reg
> 0xff)
79 return i2c_smbus_write_word_data(i2c
, reg
, val
);
82 static struct regmap_bus regmap_smbus_word
= {
83 .reg_write
= regmap_smbus_word_reg_write
,
84 .reg_read
= regmap_smbus_word_reg_read
,
87 static int regmap_smbus_word_read_swapped(void *context
, unsigned int reg
,
90 struct device
*dev
= context
;
91 struct i2c_client
*i2c
= to_i2c_client(dev
);
97 ret
= i2c_smbus_read_word_swapped(i2c
, reg
);
106 static int regmap_smbus_word_write_swapped(void *context
, unsigned int reg
,
109 struct device
*dev
= context
;
110 struct i2c_client
*i2c
= to_i2c_client(dev
);
112 if (val
> 0xffff || reg
> 0xff)
115 return i2c_smbus_write_word_swapped(i2c
, reg
, val
);
118 static struct regmap_bus regmap_smbus_word_swapped
= {
119 .reg_write
= regmap_smbus_word_write_swapped
,
120 .reg_read
= regmap_smbus_word_read_swapped
,
123 static int regmap_i2c_write(void *context
, const void *data
, size_t count
)
125 struct device
*dev
= context
;
126 struct i2c_client
*i2c
= to_i2c_client(dev
);
129 ret
= i2c_master_send(i2c
, data
, count
);
138 static int regmap_i2c_gather_write(void *context
,
139 const void *reg
, size_t reg_size
,
140 const void *val
, size_t val_size
)
142 struct device
*dev
= context
;
143 struct i2c_client
*i2c
= to_i2c_client(dev
);
144 struct i2c_msg xfer
[2];
147 /* If the I2C controller can't do a gather tell the core, it
148 * will substitute in a linear write for us.
150 if (!i2c_check_functionality(i2c
->adapter
, I2C_FUNC_NOSTART
))
153 xfer
[0].addr
= i2c
->addr
;
155 xfer
[0].len
= reg_size
;
156 xfer
[0].buf
= (void *)reg
;
158 xfer
[1].addr
= i2c
->addr
;
159 xfer
[1].flags
= I2C_M_NOSTART
;
160 xfer
[1].len
= val_size
;
161 xfer
[1].buf
= (void *)val
;
163 ret
= i2c_transfer(i2c
->adapter
, xfer
, 2);
172 static int regmap_i2c_read(void *context
,
173 const void *reg
, size_t reg_size
,
174 void *val
, size_t val_size
)
176 struct device
*dev
= context
;
177 struct i2c_client
*i2c
= to_i2c_client(dev
);
178 struct i2c_msg xfer
[2];
181 xfer
[0].addr
= i2c
->addr
;
183 xfer
[0].len
= reg_size
;
184 xfer
[0].buf
= (void *)reg
;
186 xfer
[1].addr
= i2c
->addr
;
187 xfer
[1].flags
= I2C_M_RD
;
188 xfer
[1].len
= val_size
;
191 ret
= i2c_transfer(i2c
->adapter
, xfer
, 2);
200 static struct regmap_bus regmap_i2c
= {
201 .write
= regmap_i2c_write
,
202 .gather_write
= regmap_i2c_gather_write
,
203 .read
= regmap_i2c_read
,
204 .reg_format_endian_default
= REGMAP_ENDIAN_BIG
,
205 .val_format_endian_default
= REGMAP_ENDIAN_BIG
,
208 static int regmap_i2c_smbus_i2c_write(void *context
, const void *data
,
211 struct device
*dev
= context
;
212 struct i2c_client
*i2c
= to_i2c_client(dev
);
218 return i2c_smbus_write_i2c_block_data(i2c
, ((u8
*)data
)[0], count
,
222 static int regmap_i2c_smbus_i2c_read(void *context
, const void *reg
,
223 size_t reg_size
, void *val
,
226 struct device
*dev
= context
;
227 struct i2c_client
*i2c
= to_i2c_client(dev
);
230 if (reg_size
!= 1 || val_size
< 1)
233 ret
= i2c_smbus_read_i2c_block_data(i2c
, ((u8
*)reg
)[0], val_size
, val
);
242 static struct regmap_bus regmap_i2c_smbus_i2c_block
= {
243 .write
= regmap_i2c_smbus_i2c_write
,
244 .read
= regmap_i2c_smbus_i2c_read
,
245 .max_raw_read
= I2C_SMBUS_BLOCK_MAX
,
246 .max_raw_write
= I2C_SMBUS_BLOCK_MAX
,
249 static const struct regmap_bus
*regmap_get_i2c_bus(struct i2c_client
*i2c
,
250 const struct regmap_config
*config
)
252 if (i2c_check_functionality(i2c
->adapter
, I2C_FUNC_I2C
))
254 else if (config
->val_bits
== 8 && config
->reg_bits
== 8 &&
255 i2c_check_functionality(i2c
->adapter
,
256 I2C_FUNC_SMBUS_I2C_BLOCK
))
257 return ®map_i2c_smbus_i2c_block
;
258 else if (config
->val_bits
== 16 && config
->reg_bits
== 8 &&
259 i2c_check_functionality(i2c
->adapter
,
260 I2C_FUNC_SMBUS_WORD_DATA
))
261 switch (regmap_get_val_endian(&i2c
->dev
, NULL
, config
)) {
262 case REGMAP_ENDIAN_LITTLE
:
263 return ®map_smbus_word
;
264 case REGMAP_ENDIAN_BIG
:
265 return ®map_smbus_word_swapped
;
266 default: /* everything else is not supported */
269 else if (config
->val_bits
== 8 && config
->reg_bits
== 8 &&
270 i2c_check_functionality(i2c
->adapter
,
271 I2C_FUNC_SMBUS_BYTE_DATA
))
272 return ®map_smbus_byte
;
274 return ERR_PTR(-ENOTSUPP
);
277 struct regmap
*__regmap_init_i2c(struct i2c_client
*i2c
,
278 const struct regmap_config
*config
,
279 struct lock_class_key
*lock_key
,
280 const char *lock_name
)
282 const struct regmap_bus
*bus
= regmap_get_i2c_bus(i2c
, config
);
285 return ERR_CAST(bus
);
287 return __regmap_init(&i2c
->dev
, bus
, &i2c
->dev
, config
,
288 lock_key
, lock_name
);
290 EXPORT_SYMBOL_GPL(__regmap_init_i2c
);
292 struct regmap
*__devm_regmap_init_i2c(struct i2c_client
*i2c
,
293 const struct regmap_config
*config
,
294 struct lock_class_key
*lock_key
,
295 const char *lock_name
)
297 const struct regmap_bus
*bus
= regmap_get_i2c_bus(i2c
, config
);
300 return ERR_CAST(bus
);
302 return __devm_regmap_init(&i2c
->dev
, bus
, &i2c
->dev
, config
,
303 lock_key
, lock_name
);
305 EXPORT_SYMBOL_GPL(__devm_regmap_init_i2c
);
307 MODULE_LICENSE("GPL");