blob: 4735318f42685290daffbd21186922cd7bbeadca [file] [log] [blame]
Mark Brown9943fa32011-06-20 19:02:29 +01001/*
2 * Register map access API - I2C support
3 *
4 * Copyright 2011 Wolfson Microelectronics plc
5 *
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7 *
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.
11 */
12
13#include <linux/regmap.h>
14#include <linux/i2c.h>
15#include <linux/module.h>
Mark Brown9943fa32011-06-20 19:02:29 +010016
Guenter Roeck5892ded2015-02-03 10:01:19 -080017#include "internal.h"
Boris BREZILLONb4226102014-04-21 22:56:59 +020018
19static int regmap_smbus_byte_reg_read(void *context, unsigned int reg,
20 unsigned int *val)
21{
22 struct device *dev = context;
23 struct i2c_client *i2c = to_i2c_client(dev);
24 int ret;
25
26 if (reg > 0xff)
27 return -EINVAL;
28
29 ret = i2c_smbus_read_byte_data(i2c, reg);
30 if (ret < 0)
31 return ret;
32
33 *val = ret;
34
35 return 0;
36}
37
38static int regmap_smbus_byte_reg_write(void *context, unsigned int reg,
39 unsigned int val)
40{
41 struct device *dev = context;
42 struct i2c_client *i2c = to_i2c_client(dev);
43
44 if (val > 0xff || reg > 0xff)
45 return -EINVAL;
46
47 return i2c_smbus_write_byte_data(i2c, reg, val);
48}
49
50static struct regmap_bus regmap_smbus_byte = {
51 .reg_write = regmap_smbus_byte_reg_write,
52 .reg_read = regmap_smbus_byte_reg_read,
53};
54
55static int regmap_smbus_word_reg_read(void *context, unsigned int reg,
56 unsigned int *val)
57{
58 struct device *dev = context;
59 struct i2c_client *i2c = to_i2c_client(dev);
60 int ret;
61
62 if (reg > 0xff)
63 return -EINVAL;
64
65 ret = i2c_smbus_read_word_data(i2c, reg);
66 if (ret < 0)
67 return ret;
68
69 *val = ret;
70
71 return 0;
72}
73
74static int regmap_smbus_word_reg_write(void *context, unsigned int reg,
75 unsigned int val)
76{
77 struct device *dev = context;
78 struct i2c_client *i2c = to_i2c_client(dev);
79
80 if (val > 0xffff || reg > 0xff)
81 return -EINVAL;
82
83 return i2c_smbus_write_word_data(i2c, reg, val);
84}
85
86static struct regmap_bus regmap_smbus_word = {
87 .reg_write = regmap_smbus_word_reg_write,
88 .reg_read = regmap_smbus_word_reg_read,
89};
90
Guenter Roeck5892ded2015-02-03 10:01:19 -080091static int regmap_smbus_word_read_swapped(void *context, unsigned int reg,
92 unsigned int *val)
93{
94 struct device *dev = context;
95 struct i2c_client *i2c = to_i2c_client(dev);
96 int ret;
97
98 if (reg > 0xff)
99 return -EINVAL;
100
101 ret = i2c_smbus_read_word_swapped(i2c, reg);
102 if (ret < 0)
103 return ret;
104
105 *val = ret;
106
107 return 0;
108}
109
110static int regmap_smbus_word_write_swapped(void *context, unsigned int reg,
111 unsigned int val)
112{
113 struct device *dev = context;
114 struct i2c_client *i2c = to_i2c_client(dev);
115
116 if (val > 0xffff || reg > 0xff)
117 return -EINVAL;
118
119 return i2c_smbus_write_word_swapped(i2c, reg, val);
120}
121
122static struct regmap_bus regmap_smbus_word_swapped = {
123 .reg_write = regmap_smbus_word_write_swapped,
124 .reg_read = regmap_smbus_word_read_swapped,
125};
126
Stephen Warren0135bbc2012-04-04 15:48:30 -0600127static int regmap_i2c_write(void *context, const void *data, size_t count)
Mark Brown9943fa32011-06-20 19:02:29 +0100128{
Stephen Warren0135bbc2012-04-04 15:48:30 -0600129 struct device *dev = context;
Mark Brown9943fa32011-06-20 19:02:29 +0100130 struct i2c_client *i2c = to_i2c_client(dev);
131 int ret;
132
133 ret = i2c_master_send(i2c, data, count);
134 if (ret == count)
135 return 0;
136 else if (ret < 0)
137 return ret;
138 else
139 return -EIO;
140}
141
Stephen Warren0135bbc2012-04-04 15:48:30 -0600142static int regmap_i2c_gather_write(void *context,
Mark Brown9943fa32011-06-20 19:02:29 +0100143 const void *reg, size_t reg_size,
144 const void *val, size_t val_size)
145{
Stephen Warren0135bbc2012-04-04 15:48:30 -0600146 struct device *dev = context;
Mark Brown9943fa32011-06-20 19:02:29 +0100147 struct i2c_client *i2c = to_i2c_client(dev);
148 struct i2c_msg xfer[2];
149 int ret;
150
151 /* If the I2C controller can't do a gather tell the core, it
152 * will substitute in a linear write for us.
153 */
Mark Brown14674e72012-05-30 10:55:34 +0200154 if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_NOSTART))
Mark Brown9943fa32011-06-20 19:02:29 +0100155 return -ENOTSUPP;
156
157 xfer[0].addr = i2c->addr;
158 xfer[0].flags = 0;
159 xfer[0].len = reg_size;
160 xfer[0].buf = (void *)reg;
161
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;
166
167 ret = i2c_transfer(i2c->adapter, xfer, 2);
168 if (ret == 2)
169 return 0;
170 if (ret < 0)
171 return ret;
172 else
173 return -EIO;
174}
175
Stephen Warren0135bbc2012-04-04 15:48:30 -0600176static int regmap_i2c_read(void *context,
Mark Brown9943fa32011-06-20 19:02:29 +0100177 const void *reg, size_t reg_size,
178 void *val, size_t val_size)
179{
Stephen Warren0135bbc2012-04-04 15:48:30 -0600180 struct device *dev = context;
Mark Brown9943fa32011-06-20 19:02:29 +0100181 struct i2c_client *i2c = to_i2c_client(dev);
182 struct i2c_msg xfer[2];
183 int ret;
184
185 xfer[0].addr = i2c->addr;
186 xfer[0].flags = 0;
187 xfer[0].len = reg_size;
188 xfer[0].buf = (void *)reg;
189
190 xfer[1].addr = i2c->addr;
191 xfer[1].flags = I2C_M_RD;
192 xfer[1].len = val_size;
193 xfer[1].buf = val;
194
195 ret = i2c_transfer(i2c->adapter, xfer, 2);
196 if (ret == 2)
197 return 0;
198 else if (ret < 0)
199 return ret;
200 else
201 return -EIO;
202}
203
204static struct regmap_bus regmap_i2c = {
Mark Brown9943fa32011-06-20 19:02:29 +0100205 .write = regmap_i2c_write,
206 .gather_write = regmap_i2c_gather_write,
207 .read = regmap_i2c_read,
Xiubo Lid647c192014-07-15 12:23:02 +0800208 .reg_format_endian_default = REGMAP_ENDIAN_BIG,
209 .val_format_endian_default = REGMAP_ENDIAN_BIG,
Mark Brown9943fa32011-06-20 19:02:29 +0100210};
211
Markus Pargmann29332532015-08-30 09:33:56 +0200212static int regmap_i2c_smbus_i2c_write(void *context, const void *data,
213 size_t count)
214{
215 struct device *dev = context;
216 struct i2c_client *i2c = to_i2c_client(dev);
217
218 if (count < 1)
219 return -EINVAL;
220 if (count >= I2C_SMBUS_BLOCK_MAX)
221 return -E2BIG;
222
223 --count;
224 return i2c_smbus_write_i2c_block_data(i2c, ((u8 *)data)[0], count,
225 ((u8 *)data + 1));
226}
227
228static int regmap_i2c_smbus_i2c_read(void *context, const void *reg,
229 size_t reg_size, void *val,
230 size_t val_size)
231{
232 struct device *dev = context;
233 struct i2c_client *i2c = to_i2c_client(dev);
234 int ret;
235
236 if (reg_size != 1 || val_size < 1)
237 return -EINVAL;
238 if (val_size >= I2C_SMBUS_BLOCK_MAX)
239 return -E2BIG;
240
241 ret = i2c_smbus_read_i2c_block_data(i2c, ((u8 *)reg)[0], val_size, val);
242 if (ret == val_size)
243 return 0;
244 else if (ret < 0)
245 return ret;
246 else
247 return -EIO;
248}
249
250static 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,
255};
256
Boris BREZILLONb4226102014-04-21 22:56:59 +0200257static const struct regmap_bus *regmap_get_i2c_bus(struct i2c_client *i2c,
258 const struct regmap_config *config)
259{
260 if (i2c_check_functionality(i2c->adapter, I2C_FUNC_I2C))
261 return &regmap_i2c;
Guenter Roeckd4ef9302016-06-21 11:04:23 -0700262 else if (config->val_bits == 8 && config->reg_bits == 8 &&
Markus Pargmann29332532015-08-30 09:33:56 +0200263 i2c_check_functionality(i2c->adapter,
264 I2C_FUNC_SMBUS_I2C_BLOCK))
265 return &regmap_i2c_smbus_i2c_block;
Boris BREZILLONb4226102014-04-21 22:56:59 +0200266 else if (config->val_bits == 16 && config->reg_bits == 8 &&
267 i2c_check_functionality(i2c->adapter,
268 I2C_FUNC_SMBUS_WORD_DATA))
Guenter Roeck5892ded2015-02-03 10:01:19 -0800269 switch (regmap_get_val_endian(&i2c->dev, NULL, config)) {
270 case REGMAP_ENDIAN_LITTLE:
271 return &regmap_smbus_word;
272 case REGMAP_ENDIAN_BIG:
273 return &regmap_smbus_word_swapped;
274 default: /* everything else is not supported */
275 break;
276 }
Boris BREZILLONb4226102014-04-21 22:56:59 +0200277 else if (config->val_bits == 8 && config->reg_bits == 8 &&
278 i2c_check_functionality(i2c->adapter,
279 I2C_FUNC_SMBUS_BYTE_DATA))
280 return &regmap_smbus_byte;
281
282 return ERR_PTR(-ENOTSUPP);
283}
284
Nicolas Boichat3cfe7a72015-07-08 14:30:18 +0800285struct 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)
Mark Brown9943fa32011-06-20 19:02:29 +0100289{
Boris BREZILLONb4226102014-04-21 22:56:59 +0200290 const struct regmap_bus *bus = regmap_get_i2c_bus(i2c, config);
291
292 if (IS_ERR(bus))
293 return ERR_CAST(bus);
294
Nicolas Boichat3cfe7a72015-07-08 14:30:18 +0800295 return __regmap_init(&i2c->dev, bus, &i2c->dev, config,
296 lock_key, lock_name);
Mark Brown9943fa32011-06-20 19:02:29 +0100297}
Nicolas Boichat3cfe7a72015-07-08 14:30:18 +0800298EXPORT_SYMBOL_GPL(__regmap_init_i2c);
Mark Brown9943fa32011-06-20 19:02:29 +0100299
Nicolas Boichat3cfe7a72015-07-08 14:30:18 +0800300struct 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)
Mark Brownc0eb4672012-01-30 19:56:52 +0000304{
Boris BREZILLONb4226102014-04-21 22:56:59 +0200305 const struct regmap_bus *bus = regmap_get_i2c_bus(i2c, config);
306
307 if (IS_ERR(bus))
308 return ERR_CAST(bus);
309
Nicolas Boichat3cfe7a72015-07-08 14:30:18 +0800310 return __devm_regmap_init(&i2c->dev, bus, &i2c->dev, config,
311 lock_key, lock_name);
Mark Brownc0eb4672012-01-30 19:56:52 +0000312}
Nicolas Boichat3cfe7a72015-07-08 14:30:18 +0800313EXPORT_SYMBOL_GPL(__devm_regmap_init_i2c);
Mark Brownc0eb4672012-01-30 19:56:52 +0000314
Stephen Warrenb33f9cb2011-08-11 11:59:10 -0600315MODULE_LICENSE("GPL");