2 * Register map access API - I2C support
4 * Copyright 2011 Wolfson Microelectronics plc
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/regmap.h>
14 #include <linux/i2c.h>
15 #include <linux/module.h>
19 static int regmap_smbus_byte_reg_read(void *context
, unsigned int reg
,
22 struct device
*dev
= context
;
23 struct i2c_client
*i2c
= to_i2c_client(dev
);
29 ret
= i2c_smbus_read_byte_data(i2c
, reg
);
38 static int regmap_smbus_byte_reg_write(void *context
, unsigned int reg
,
41 struct device
*dev
= context
;
42 struct i2c_client
*i2c
= to_i2c_client(dev
);
44 if (val
> 0xff || reg
> 0xff)
47 return i2c_smbus_write_byte_data(i2c
, reg
, val
);
50 static struct regmap_bus regmap_smbus_byte
= {
51 .reg_write
= regmap_smbus_byte_reg_write
,
52 .reg_read
= regmap_smbus_byte_reg_read
,
55 static int regmap_smbus_word_reg_read(void *context
, unsigned int reg
,
58 struct device
*dev
= context
;
59 struct i2c_client
*i2c
= to_i2c_client(dev
);
65 ret
= i2c_smbus_read_word_data(i2c
, reg
);
74 static int regmap_smbus_word_reg_write(void *context
, unsigned int reg
,
77 struct device
*dev
= context
;
78 struct i2c_client
*i2c
= to_i2c_client(dev
);
80 if (val
> 0xffff || reg
> 0xff)
83 return i2c_smbus_write_word_data(i2c
, reg
, val
);
86 static struct regmap_bus regmap_smbus_word
= {
87 .reg_write
= regmap_smbus_word_reg_write
,
88 .reg_read
= regmap_smbus_word_reg_read
,
91 static int regmap_smbus_word_read_swapped(void *context
, unsigned int reg
,
94 struct device
*dev
= context
;
95 struct i2c_client
*i2c
= to_i2c_client(dev
);
101 ret
= i2c_smbus_read_word_swapped(i2c
, reg
);
110 static int regmap_smbus_word_write_swapped(void *context
, unsigned int reg
,
113 struct device
*dev
= context
;
114 struct i2c_client
*i2c
= to_i2c_client(dev
);
116 if (val
> 0xffff || reg
> 0xff)
119 return i2c_smbus_write_word_swapped(i2c
, reg
, val
);
122 static struct regmap_bus regmap_smbus_word_swapped
= {
123 .reg_write
= regmap_smbus_word_write_swapped
,
124 .reg_read
= regmap_smbus_word_read_swapped
,
127 static int regmap_i2c_write(void *context
, const void *data
, size_t count
)
129 struct device
*dev
= context
;
130 struct i2c_client
*i2c
= to_i2c_client(dev
);
133 ret
= i2c_master_send(i2c
, data
, count
);
142 static int regmap_i2c_gather_write(void *context
,
143 const void *reg
, size_t reg_size
,
144 const void *val
, size_t val_size
)
146 struct device
*dev
= context
;
147 struct i2c_client
*i2c
= to_i2c_client(dev
);
148 struct i2c_msg xfer
[2];
151 /* If the I2C controller can't do a gather tell the core, it
152 * will substitute in a linear write for us.
154 if (!i2c_check_functionality(i2c
->adapter
, I2C_FUNC_NOSTART
))
157 xfer
[0].addr
= i2c
->addr
;
159 xfer
[0].len
= reg_size
;
160 xfer
[0].buf
= (void *)reg
;
162 xfer
[1].addr
= i2c
->addr
;
163 xfer
[1].flags
= I2C_M_NOSTART
;
164 xfer
[1].len
= val_size
;
165 xfer
[1].buf
= (void *)val
;
167 ret
= i2c_transfer(i2c
->adapter
, xfer
, 2);
176 static int regmap_i2c_read(void *context
,
177 const void *reg
, size_t reg_size
,
178 void *val
, size_t val_size
)
180 struct device
*dev
= context
;
181 struct i2c_client
*i2c
= to_i2c_client(dev
);
182 struct i2c_msg xfer
[2];
185 xfer
[0].addr
= i2c
->addr
;
187 xfer
[0].len
= reg_size
;
188 xfer
[0].buf
= (void *)reg
;
190 xfer
[1].addr
= i2c
->addr
;
191 xfer
[1].flags
= I2C_M_RD
;
192 xfer
[1].len
= val_size
;
195 ret
= i2c_transfer(i2c
->adapter
, xfer
, 2);
204 static struct regmap_bus regmap_i2c
= {
205 .write
= regmap_i2c_write
,
206 .gather_write
= regmap_i2c_gather_write
,
207 .read
= regmap_i2c_read
,
208 .reg_format_endian_default
= REGMAP_ENDIAN_BIG
,
209 .val_format_endian_default
= REGMAP_ENDIAN_BIG
,
212 static int regmap_i2c_smbus_i2c_write(void *context
, const void *data
,
215 struct device
*dev
= context
;
216 struct i2c_client
*i2c
= to_i2c_client(dev
);
220 if (count
>= I2C_SMBUS_BLOCK_MAX
)
224 return i2c_smbus_write_i2c_block_data(i2c
, ((u8
*)data
)[0], count
,
228 static int regmap_i2c_smbus_i2c_read(void *context
, const void *reg
,
229 size_t reg_size
, void *val
,
232 struct device
*dev
= context
;
233 struct i2c_client
*i2c
= to_i2c_client(dev
);
236 if (reg_size
!= 1 || val_size
< 1)
238 if (val_size
>= I2C_SMBUS_BLOCK_MAX
)
241 ret
= i2c_smbus_read_i2c_block_data(i2c
, ((u8
*)reg
)[0], val_size
, val
);
250 static struct regmap_bus regmap_i2c_smbus_i2c_block
= {
251 .write
= regmap_i2c_smbus_i2c_write
,
252 .read
= regmap_i2c_smbus_i2c_read
,
253 .max_raw_read
= I2C_SMBUS_BLOCK_MAX
,
254 .max_raw_write
= I2C_SMBUS_BLOCK_MAX
,
257 static const struct regmap_bus
*regmap_get_i2c_bus(struct i2c_client
*i2c
,
258 const struct regmap_config
*config
)
260 if (i2c_check_functionality(i2c
->adapter
, I2C_FUNC_I2C
))
262 else if (config
->reg_bits
== 8 &&
263 i2c_check_functionality(i2c
->adapter
,
264 I2C_FUNC_SMBUS_I2C_BLOCK
))
265 return ®map_i2c_smbus_i2c_block
;
266 else if (config
->val_bits
== 16 && config
->reg_bits
== 8 &&
267 i2c_check_functionality(i2c
->adapter
,
268 I2C_FUNC_SMBUS_WORD_DATA
))
269 switch (regmap_get_val_endian(&i2c
->dev
, NULL
, config
)) {
270 case REGMAP_ENDIAN_LITTLE
:
271 return ®map_smbus_word
;
272 case REGMAP_ENDIAN_BIG
:
273 return ®map_smbus_word_swapped
;
274 default: /* everything else is not supported */
277 else if (config
->val_bits
== 8 && config
->reg_bits
== 8 &&
278 i2c_check_functionality(i2c
->adapter
,
279 I2C_FUNC_SMBUS_BYTE_DATA
))
280 return ®map_smbus_byte
;
282 return ERR_PTR(-ENOTSUPP
);
285 struct regmap
*__regmap_init_i2c(struct i2c_client
*i2c
,
286 const struct regmap_config
*config
,
287 struct lock_class_key
*lock_key
,
288 const char *lock_name
)
290 const struct regmap_bus
*bus
= regmap_get_i2c_bus(i2c
, config
);
293 return ERR_CAST(bus
);
295 return __regmap_init(&i2c
->dev
, bus
, &i2c
->dev
, config
,
296 lock_key
, lock_name
);
298 EXPORT_SYMBOL_GPL(__regmap_init_i2c
);
300 struct regmap
*__devm_regmap_init_i2c(struct i2c_client
*i2c
,
301 const struct regmap_config
*config
,
302 struct lock_class_key
*lock_key
,
303 const char *lock_name
)
305 const struct regmap_bus
*bus
= regmap_get_i2c_bus(i2c
, config
);
308 return ERR_CAST(bus
);
310 return __devm_regmap_init(&i2c
->dev
, bus
, &i2c
->dev
, config
,
311 lock_key
, lock_name
);
313 EXPORT_SYMBOL_GPL(__devm_regmap_init_i2c
);
315 MODULE_LICENSE("GPL");