Merge remote-tracking branch 'moduleh/module.h-split'
[linux-2.6/next.git] / drivers / base / regmap / regmap.c
blobfa2bd896eb208fa8a92732eb6a2accf3ee3a4732
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 #define CREATE_TRACE_POINTS
19 #include <trace/events/regmap.h>
21 #include "internal.h"
23 bool regmap_writeable(struct regmap *map, unsigned int reg)
25 if (map->max_register && reg > map->max_register)
26 return false;
28 if (map->writeable_reg)
29 return map->writeable_reg(map->dev, reg);
31 return true;
34 bool regmap_readable(struct regmap *map, unsigned int reg)
36 if (map->max_register && reg > map->max_register)
37 return false;
39 if (map->readable_reg)
40 return map->readable_reg(map->dev, reg);
42 return true;
45 bool regmap_volatile(struct regmap *map, unsigned int reg)
47 if (map->max_register && reg > map->max_register)
48 return false;
50 if (map->volatile_reg)
51 return map->volatile_reg(map->dev, reg);
53 return true;
56 bool regmap_precious(struct regmap *map, unsigned int reg)
58 if (map->max_register && reg > map->max_register)
59 return false;
61 if (map->precious_reg)
62 return map->precious_reg(map->dev, reg);
64 return false;
67 static void regmap_format_4_12_write(struct regmap *map,
68 unsigned int reg, unsigned int val)
70 __be16 *out = map->work_buf;
71 *out = cpu_to_be16((reg << 12) | val);
74 static void regmap_format_7_9_write(struct regmap *map,
75 unsigned int reg, unsigned int val)
77 __be16 *out = map->work_buf;
78 *out = cpu_to_be16((reg << 9) | val);
81 static void regmap_format_8(void *buf, unsigned int val)
83 u8 *b = buf;
85 b[0] = val;
88 static void regmap_format_16(void *buf, unsigned int val)
90 __be16 *b = buf;
92 b[0] = cpu_to_be16(val);
95 static unsigned int regmap_parse_8(void *buf)
97 u8 *b = buf;
99 return b[0];
102 static unsigned int regmap_parse_16(void *buf)
104 __be16 *b = buf;
106 b[0] = be16_to_cpu(b[0]);
108 return b[0];
112 * regmap_init(): Initialise register map
114 * @dev: Device that will be interacted with
115 * @bus: Bus-specific callbacks to use with device
116 * @config: Configuration for register map
118 * The return value will be an ERR_PTR() on error or a valid pointer to
119 * a struct regmap. This function should generally not be called
120 * directly, it should be called by bus-specific init functions.
122 struct regmap *regmap_init(struct device *dev,
123 const struct regmap_bus *bus,
124 const struct regmap_config *config)
126 struct regmap *map;
127 int ret = -EINVAL;
129 if (!bus || !config)
130 return NULL;
132 map = kzalloc(sizeof(*map), GFP_KERNEL);
133 if (map == NULL) {
134 ret = -ENOMEM;
135 goto err;
138 mutex_init(&map->lock);
139 map->format.buf_size = (config->reg_bits + config->val_bits) / 8;
140 map->format.reg_bytes = config->reg_bits / 8;
141 map->format.val_bytes = config->val_bits / 8;
142 map->dev = dev;
143 map->bus = bus;
144 map->max_register = config->max_register;
145 map->writeable_reg = config->writeable_reg;
146 map->readable_reg = config->readable_reg;
147 map->volatile_reg = config->volatile_reg;
148 map->precious_reg = config->precious_reg;
150 switch (config->reg_bits) {
151 case 4:
152 switch (config->val_bits) {
153 case 12:
154 map->format.format_write = regmap_format_4_12_write;
155 break;
156 default:
157 goto err_map;
159 break;
161 case 7:
162 switch (config->val_bits) {
163 case 9:
164 map->format.format_write = regmap_format_7_9_write;
165 break;
166 default:
167 goto err_map;
169 break;
171 case 8:
172 map->format.format_reg = regmap_format_8;
173 break;
175 case 16:
176 map->format.format_reg = regmap_format_16;
177 break;
179 default:
180 goto err_map;
183 switch (config->val_bits) {
184 case 8:
185 map->format.format_val = regmap_format_8;
186 map->format.parse_val = regmap_parse_8;
187 break;
188 case 16:
189 map->format.format_val = regmap_format_16;
190 map->format.parse_val = regmap_parse_16;
191 break;
194 if (!map->format.format_write &&
195 !(map->format.format_reg && map->format.format_val))
196 goto err_map;
198 map->work_buf = kmalloc(map->format.buf_size, GFP_KERNEL);
199 if (map->work_buf == NULL) {
200 ret = -ENOMEM;
201 goto err_bus;
204 regmap_debugfs_init(map);
206 return map;
208 err_bus:
209 module_put(map->bus->owner);
210 err_map:
211 kfree(map);
212 err:
213 return ERR_PTR(ret);
215 EXPORT_SYMBOL_GPL(regmap_init);
218 * regmap_exit(): Free a previously allocated register map
220 void regmap_exit(struct regmap *map)
222 regmap_debugfs_exit(map);
223 kfree(map->work_buf);
224 module_put(map->bus->owner);
225 kfree(map);
227 EXPORT_SYMBOL_GPL(regmap_exit);
229 static int _regmap_raw_write(struct regmap *map, unsigned int reg,
230 const void *val, size_t val_len)
232 void *buf;
233 int ret = -ENOTSUPP;
234 size_t len;
235 int i;
237 /* Check for unwritable registers before we start */
238 if (map->writeable_reg)
239 for (i = 0; i < val_len / map->format.val_bytes; i++)
240 if (!map->writeable_reg(map->dev, reg + i))
241 return -EINVAL;
243 map->format.format_reg(map->work_buf, reg);
245 trace_regmap_hw_write_start(map->dev, reg,
246 val_len / map->format.val_bytes);
248 /* If we're doing a single register write we can probably just
249 * send the work_buf directly, otherwise try to do a gather
250 * write.
252 if (val == map->work_buf + map->format.reg_bytes)
253 ret = map->bus->write(map->dev, map->work_buf,
254 map->format.reg_bytes + val_len);
255 else if (map->bus->gather_write)
256 ret = map->bus->gather_write(map->dev, map->work_buf,
257 map->format.reg_bytes,
258 val, val_len);
260 /* If that didn't work fall back on linearising by hand. */
261 if (ret == -ENOTSUPP) {
262 len = map->format.reg_bytes + val_len;
263 buf = kmalloc(len, GFP_KERNEL);
264 if (!buf)
265 return -ENOMEM;
267 memcpy(buf, map->work_buf, map->format.reg_bytes);
268 memcpy(buf + map->format.reg_bytes, val, val_len);
269 ret = map->bus->write(map->dev, buf, len);
271 kfree(buf);
274 trace_regmap_hw_write_done(map->dev, reg,
275 val_len / map->format.val_bytes);
277 return ret;
280 static int _regmap_write(struct regmap *map, unsigned int reg,
281 unsigned int val)
283 int ret;
284 BUG_ON(!map->format.format_write && !map->format.format_val);
286 trace_regmap_reg_write(map->dev, reg, val);
288 if (map->format.format_write) {
289 map->format.format_write(map, reg, val);
291 trace_regmap_hw_write_start(map->dev, reg, 1);
293 ret = map->bus->write(map->dev, map->work_buf,
294 map->format.buf_size);
296 trace_regmap_hw_write_done(map->dev, reg, 1);
298 return ret;
299 } else {
300 map->format.format_val(map->work_buf + map->format.reg_bytes,
301 val);
302 return _regmap_raw_write(map, reg,
303 map->work_buf + map->format.reg_bytes,
304 map->format.val_bytes);
309 * regmap_write(): Write a value to a single register
311 * @map: Register map to write to
312 * @reg: Register to write to
313 * @val: Value to be written
315 * A value of zero will be returned on success, a negative errno will
316 * be returned in error cases.
318 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
320 int ret;
322 mutex_lock(&map->lock);
324 ret = _regmap_write(map, reg, val);
326 mutex_unlock(&map->lock);
328 return ret;
330 EXPORT_SYMBOL_GPL(regmap_write);
333 * regmap_raw_write(): Write raw values to one or more registers
335 * @map: Register map to write to
336 * @reg: Initial register to write to
337 * @val: Block of data to be written, laid out for direct transmission to the
338 * device
339 * @val_len: Length of data pointed to by val.
341 * This function is intended to be used for things like firmware
342 * download where a large block of data needs to be transferred to the
343 * device. No formatting will be done on the data provided.
345 * A value of zero will be returned on success, a negative errno will
346 * be returned in error cases.
348 int regmap_raw_write(struct regmap *map, unsigned int reg,
349 const void *val, size_t val_len)
351 int ret;
353 mutex_lock(&map->lock);
355 ret = _regmap_raw_write(map, reg, val, val_len);
357 mutex_unlock(&map->lock);
359 return ret;
361 EXPORT_SYMBOL_GPL(regmap_raw_write);
363 static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
364 unsigned int val_len)
366 u8 *u8 = map->work_buf;
367 int ret;
369 map->format.format_reg(map->work_buf, reg);
372 * Some buses flag reads by setting the high bits in the
373 * register addresss; since it's always the high bits for all
374 * current formats we can do this here rather than in
375 * formatting. This may break if we get interesting formats.
377 if (map->bus->read_flag_mask)
378 u8[0] |= map->bus->read_flag_mask;
380 trace_regmap_hw_read_start(map->dev, reg,
381 val_len / map->format.val_bytes);
383 ret = map->bus->read(map->dev, map->work_buf, map->format.reg_bytes,
384 val, val_len);
386 trace_regmap_hw_read_done(map->dev, reg,
387 val_len / map->format.val_bytes);
389 return ret;
392 static int _regmap_read(struct regmap *map, unsigned int reg,
393 unsigned int *val)
395 int ret;
397 if (!map->format.parse_val)
398 return -EINVAL;
400 ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
401 if (ret == 0) {
402 *val = map->format.parse_val(map->work_buf);
403 trace_regmap_reg_read(map->dev, reg, *val);
406 return ret;
410 * regmap_read(): Read a value from a single register
412 * @map: Register map to write to
413 * @reg: Register to be read from
414 * @val: Pointer to store read value
416 * A value of zero will be returned on success, a negative errno will
417 * be returned in error cases.
419 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
421 int ret;
423 mutex_lock(&map->lock);
425 ret = _regmap_read(map, reg, val);
427 mutex_unlock(&map->lock);
429 return ret;
431 EXPORT_SYMBOL_GPL(regmap_read);
434 * regmap_raw_read(): Read raw data from the device
436 * @map: Register map to write to
437 * @reg: First register to be read from
438 * @val: Pointer to store read value
439 * @val_len: Size of data to read
441 * A value of zero will be returned on success, a negative errno will
442 * be returned in error cases.
444 int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
445 size_t val_len)
447 int ret;
449 mutex_lock(&map->lock);
451 ret = _regmap_raw_read(map, reg, val, val_len);
453 mutex_unlock(&map->lock);
455 return ret;
457 EXPORT_SYMBOL_GPL(regmap_raw_read);
460 * regmap_bulk_read(): Read multiple registers from the device
462 * @map: Register map to write to
463 * @reg: First register to be read from
464 * @val: Pointer to store read value, in native register size for device
465 * @val_count: Number of registers to read
467 * A value of zero will be returned on success, a negative errno will
468 * be returned in error cases.
470 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
471 size_t val_count)
473 int ret, i;
474 size_t val_bytes = map->format.val_bytes;
476 if (!map->format.parse_val)
477 return -EINVAL;
479 ret = regmap_raw_read(map, reg, val, val_bytes * val_count);
480 if (ret != 0)
481 return ret;
483 for (i = 0; i < val_count * val_bytes; i += val_bytes)
484 map->format.parse_val(val + i);
486 return 0;
488 EXPORT_SYMBOL_GPL(regmap_bulk_read);
491 * remap_update_bits: Perform a read/modify/write cycle on the register map
493 * @map: Register map to update
494 * @reg: Register to update
495 * @mask: Bitmask to change
496 * @val: New value for bitmask
498 * Returns zero for success, a negative number on error.
500 int regmap_update_bits(struct regmap *map, unsigned int reg,
501 unsigned int mask, unsigned int val)
503 int ret;
504 unsigned int tmp;
506 mutex_lock(&map->lock);
508 ret = _regmap_read(map, reg, &tmp);
509 if (ret != 0)
510 goto out;
512 tmp &= ~mask;
513 tmp |= val & mask;
515 ret = _regmap_write(map, reg, tmp);
517 out:
518 mutex_unlock(&map->lock);
520 return ret;
522 EXPORT_SYMBOL_GPL(regmap_update_bits);
524 static int __init regmap_initcall(void)
526 regmap_debugfs_initcall();
528 return 0;
530 postcore_initcall(regmap_initcall);