Merge remote-tracking branch 'pm/linux-next'
[linux-2.6/next.git] / drivers / base / regmap / regmap.c
blob0eef4da1ac61f1deb274e56fb5601cfc6275c193
1 /*
2 * Register map access API
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/slab.h>
14 #include <linux/module.h>
15 #include <linux/mutex.h>
16 #include <linux/err.h>
18 #include <linux/regmap.h>
20 struct regmap;
22 struct regmap_format {
23 size_t buf_size;
24 size_t reg_bytes;
25 size_t val_bytes;
26 void (*format_write)(struct regmap *map,
27 unsigned int reg, unsigned int val);
28 void (*format_reg)(void *buf, unsigned int reg);
29 void (*format_val)(void *buf, unsigned int val);
30 unsigned int (*parse_val)(void *buf);
33 struct regmap {
34 struct mutex lock;
36 struct device *dev; /* Device we do I/O on */
37 void *work_buf; /* Scratch buffer used to format I/O */
38 struct regmap_format format; /* Buffer format */
39 const struct regmap_bus *bus;
42 static void regmap_format_4_12_write(struct regmap *map,
43 unsigned int reg, unsigned int val)
45 __be16 *out = map->work_buf;
46 *out = cpu_to_be16((reg << 12) | val);
49 static void regmap_format_7_9_write(struct regmap *map,
50 unsigned int reg, unsigned int val)
52 __be16 *out = map->work_buf;
53 *out = cpu_to_be16((reg << 9) | val);
56 static void regmap_format_8(void *buf, unsigned int val)
58 u8 *b = buf;
60 b[0] = val;
63 static void regmap_format_16(void *buf, unsigned int val)
65 __be16 *b = buf;
67 b[0] = cpu_to_be16(val);
70 static unsigned int regmap_parse_8(void *buf)
72 u8 *b = buf;
74 return b[0];
77 static unsigned int regmap_parse_16(void *buf)
79 __be16 *b = buf;
81 b[0] = be16_to_cpu(b[0]);
83 return b[0];
86 /**
87 * regmap_init(): Initialise register map
89 * @dev: Device that will be interacted with
90 * @bus: Bus-specific callbacks to use with device
91 * @config: Configuration for register map
93 * The return value will be an ERR_PTR() on error or a valid pointer to
94 * a struct regmap. This function should generally not be called
95 * directly, it should be called by bus-specific init functions.
97 struct regmap *regmap_init(struct device *dev,
98 const struct regmap_bus *bus,
99 const struct regmap_config *config)
101 struct regmap *map;
102 int ret = -EINVAL;
104 if (!bus || !config)
105 return NULL;
107 map = kzalloc(sizeof(*map), GFP_KERNEL);
108 if (map == NULL) {
109 ret = -ENOMEM;
110 goto err;
113 mutex_init(&map->lock);
114 map->format.buf_size = (config->reg_bits + config->val_bits) / 8;
115 map->format.reg_bytes = config->reg_bits / 8;
116 map->format.val_bytes = config->val_bits / 8;
117 map->dev = dev;
118 map->bus = bus;
120 switch (config->reg_bits) {
121 case 4:
122 switch (config->val_bits) {
123 case 12:
124 map->format.format_write = regmap_format_4_12_write;
125 break;
126 default:
127 goto err_map;
129 break;
131 case 7:
132 switch (config->val_bits) {
133 case 9:
134 map->format.format_write = regmap_format_7_9_write;
135 break;
136 default:
137 goto err_map;
139 break;
141 case 8:
142 map->format.format_reg = regmap_format_8;
143 break;
145 case 16:
146 map->format.format_reg = regmap_format_16;
147 break;
149 default:
150 goto err_map;
153 switch (config->val_bits) {
154 case 8:
155 map->format.format_val = regmap_format_8;
156 map->format.parse_val = regmap_parse_8;
157 break;
158 case 16:
159 map->format.format_val = regmap_format_16;
160 map->format.parse_val = regmap_parse_16;
161 break;
164 if (!map->format.format_write &&
165 !(map->format.format_reg && map->format.format_val))
166 goto err_map;
168 map->work_buf = kmalloc(map->format.buf_size, GFP_KERNEL);
169 if (map->work_buf == NULL) {
170 ret = -ENOMEM;
171 goto err_bus;
174 return map;
176 err_bus:
177 module_put(map->bus->owner);
178 err_map:
179 kfree(map);
180 err:
181 return ERR_PTR(ret);
183 EXPORT_SYMBOL_GPL(regmap_init);
186 * regmap_exit(): Free a previously allocated register map
188 void regmap_exit(struct regmap *map)
190 kfree(map->work_buf);
191 module_put(map->bus->owner);
192 kfree(map);
194 EXPORT_SYMBOL_GPL(regmap_exit);
196 static int _regmap_raw_write(struct regmap *map, unsigned int reg,
197 const void *val, size_t val_len)
199 void *buf;
200 int ret = -ENOTSUPP;
201 size_t len;
203 map->format.format_reg(map->work_buf, reg);
205 /* Try to do a gather write if we can */
206 if (map->bus->gather_write)
207 ret = map->bus->gather_write(map->dev, map->work_buf,
208 map->format.reg_bytes,
209 val, val_len);
211 /* Otherwise fall back on linearising by hand. */
212 if (ret == -ENOTSUPP) {
213 len = map->format.reg_bytes + val_len;
214 buf = kmalloc(len, GFP_KERNEL);
215 if (!buf)
216 return -ENOMEM;
218 memcpy(buf, map->work_buf, map->format.reg_bytes);
219 memcpy(buf + map->format.reg_bytes, val, val_len);
220 ret = map->bus->write(map->dev, buf, len);
222 kfree(buf);
225 return ret;
228 static int _regmap_write(struct regmap *map, unsigned int reg,
229 unsigned int val)
231 BUG_ON(!map->format.format_write && !map->format.format_val);
233 if (map->format.format_write) {
234 map->format.format_write(map, reg, val);
236 return map->bus->write(map->dev, map->work_buf,
237 map->format.buf_size);
238 } else {
239 map->format.format_val(map->work_buf + map->format.reg_bytes,
240 val);
241 return _regmap_raw_write(map, reg,
242 map->work_buf + map->format.reg_bytes,
243 map->format.val_bytes);
248 * regmap_write(): Write a value to a single register
250 * @map: Register map to write to
251 * @reg: Register to write to
252 * @val: Value to be written
254 * A value of zero will be returned on success, a negative errno will
255 * be returned in error cases.
257 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
259 int ret;
261 mutex_lock(&map->lock);
263 ret = _regmap_write(map, reg, val);
265 mutex_unlock(&map->lock);
267 return ret;
269 EXPORT_SYMBOL_GPL(regmap_write);
272 * regmap_raw_write(): Write raw values to one or more registers
274 * @map: Register map to write to
275 * @reg: Initial register to write to
276 * @val: Block of data to be written, laid out for direct transmission to the
277 * device
278 * @val_len: Length of data pointed to by val.
280 * This function is intended to be used for things like firmware
281 * download where a large block of data needs to be transferred to the
282 * device. No formatting will be done on the data provided.
284 * A value of zero will be returned on success, a negative errno will
285 * be returned in error cases.
287 int regmap_raw_write(struct regmap *map, unsigned int reg,
288 const void *val, size_t val_len)
290 int ret;
292 mutex_lock(&map->lock);
294 ret = _regmap_raw_write(map, reg, val, val_len);
296 mutex_unlock(&map->lock);
298 return ret;
300 EXPORT_SYMBOL_GPL(regmap_raw_write);
302 static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
303 unsigned int val_len)
305 u8 *u8 = map->work_buf;
306 int ret;
308 map->format.format_reg(map->work_buf, reg);
311 * Some buses flag reads by setting the high bits in the
312 * register addresss; since it's always the high bits for all
313 * current formats we can do this here rather than in
314 * formatting. This may break if we get interesting formats.
316 if (map->bus->read_flag_mask)
317 u8[0] |= map->bus->read_flag_mask;
319 ret = map->bus->read(map->dev, map->work_buf, map->format.reg_bytes,
320 val, val_len);
321 if (ret != 0)
322 return ret;
324 return 0;
327 static int _regmap_read(struct regmap *map, unsigned int reg,
328 unsigned int *val)
330 int ret;
332 if (!map->format.parse_val)
333 return -EINVAL;
335 ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
336 if (ret == 0)
337 *val = map->format.parse_val(map->work_buf);
339 return ret;
343 * regmap_read(): Read a value from a single register
345 * @map: Register map to write to
346 * @reg: Register to be read from
347 * @val: Pointer to store read value
349 * A value of zero will be returned on success, a negative errno will
350 * be returned in error cases.
352 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
354 int ret;
356 mutex_lock(&map->lock);
358 ret = _regmap_read(map, reg, val);
360 mutex_unlock(&map->lock);
362 return ret;
364 EXPORT_SYMBOL_GPL(regmap_read);
367 * regmap_raw_read(): Read raw data from the device
369 * @map: Register map to write to
370 * @reg: First register to be read from
371 * @val: Pointer to store read value
372 * @val_len: Size of data to read
374 * A value of zero will be returned on success, a negative errno will
375 * be returned in error cases.
377 int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
378 size_t val_len)
380 int ret;
382 mutex_lock(&map->lock);
384 ret = _regmap_raw_read(map, reg, val, val_len);
386 mutex_unlock(&map->lock);
388 return ret;
390 EXPORT_SYMBOL_GPL(regmap_raw_read);
393 * regmap_bulk_read(): Read multiple registers from the device
395 * @map: Register map to write to
396 * @reg: First register to be read from
397 * @val: Pointer to store read value, in native register size for device
398 * @val_count: Number of registers to read
400 * A value of zero will be returned on success, a negative errno will
401 * be returned in error cases.
403 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
404 size_t val_count)
406 int ret, i;
407 size_t val_bytes = map->format.val_bytes;
409 if (!map->format.parse_val)
410 return -EINVAL;
412 ret = regmap_raw_read(map, reg, val, val_bytes * val_count);
413 if (ret != 0)
414 return ret;
416 for (i = 0; i < val_count * val_bytes; i += val_bytes)
417 map->format.parse_val(val + i);
419 return 0;
421 EXPORT_SYMBOL_GPL(regmap_bulk_read);
424 * remap_update_bits: Perform a read/modify/write cycle on the register map
426 * @map: Register map to update
427 * @reg: Register to update
428 * @mask: Bitmask to change
429 * @val: New value for bitmask
431 * Returns zero for success, a negative number on error.
433 int regmap_update_bits(struct regmap *map, unsigned int reg,
434 unsigned int mask, unsigned int val)
436 int ret;
437 unsigned int tmp;
439 mutex_lock(&map->lock);
441 ret = _regmap_read(map, reg, &tmp);
442 if (ret != 0)
443 goto out;
445 tmp &= ~mask;
446 tmp |= val & mask;
448 ret = _regmap_write(map, reg, tmp);
450 out:
451 mutex_unlock(&map->lock);
453 return ret;
455 EXPORT_SYMBOL_GPL(regmap_update_bits);