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>
23 bool regmap_writeable(struct regmap *map, unsigned int reg)
25 if (map->max_register && reg > map->max_register)
28 if (map->writeable_reg)
29 return map->writeable_reg(map->dev, reg);
34 bool regmap_readable(struct regmap *map, unsigned int reg)
36 if (map->max_register && reg > map->max_register)
39 if (map->readable_reg)
40 return map->readable_reg(map->dev, reg);
45 bool regmap_volatile(struct regmap *map, unsigned int reg)
47 if (map->max_register && reg > map->max_register)
50 if (map->volatile_reg)
51 return map->volatile_reg(map->dev, reg);
56 bool regmap_precious(struct regmap *map, unsigned int reg)
58 if (map->max_register && reg > map->max_register)
61 if (map->precious_reg)
62 return map->precious_reg(map->dev, reg);
67 static bool regmap_volatile_range(struct regmap *map, unsigned int reg,
72 for (i = 0; i < num; i++)
73 if (!regmap_volatile(map, reg + i))
79 static void regmap_format_4_12_write(struct regmap *map,
80 unsigned int reg, unsigned int val)
82 __be16 *out = map->work_buf;
83 *out = cpu_to_be16((reg << 12) | val);
86 static void regmap_format_7_9_write(struct regmap *map,
87 unsigned int reg, unsigned int val)
89 __be16 *out = map->work_buf;
90 *out = cpu_to_be16((reg << 9) | val);
93 static void regmap_format_8(void *buf, unsigned int val)
100 static void regmap_format_16(void *buf, unsigned int val)
104 b[0] = cpu_to_be16(val);
107 static unsigned int regmap_parse_8(void *buf)
114 static unsigned int regmap_parse_16(void *buf)
118 b[0] = be16_to_cpu(b[0]);
124 * regmap_init(): Initialise register map
126 * @dev: Device that will be interacted with
127 * @bus: Bus-specific callbacks to use with device
128 * @config: Configuration for register map
130 * The return value will be an ERR_PTR() on error or a valid pointer to
131 * a struct regmap. This function should generally not be called
132 * directly, it should be called by bus-specific init functions.
134 struct regmap *regmap_init(struct device *dev,
135 const struct regmap_bus *bus,
136 const struct regmap_config *config)
144 map = kzalloc(sizeof(*map), GFP_KERNEL);
150 mutex_init(&map->lock);
151 map->format.buf_size = (config->reg_bits + config->val_bits) / 8;
152 map->format.reg_bytes = config->reg_bits / 8;
153 map->format.val_bytes = config->val_bits / 8;
156 map->max_register = config->max_register;
157 map->writeable_reg = config->writeable_reg;
158 map->readable_reg = config->readable_reg;
159 map->volatile_reg = config->volatile_reg;
160 map->precious_reg = config->precious_reg;
161 map->cache_type = config->cache_type;
163 if (config->read_flag_mask || config->write_flag_mask) {
164 map->read_flag_mask = config->read_flag_mask;
165 map->write_flag_mask = config->write_flag_mask;
167 map->read_flag_mask = bus->read_flag_mask;
170 switch (config->reg_bits) {
172 switch (config->val_bits) {
174 map->format.format_write = regmap_format_4_12_write;
182 switch (config->val_bits) {
184 map->format.format_write = regmap_format_7_9_write;
192 map->format.format_reg = regmap_format_8;
196 map->format.format_reg = regmap_format_16;
203 switch (config->val_bits) {
205 map->format.format_val = regmap_format_8;
206 map->format.parse_val = regmap_parse_8;
209 map->format.format_val = regmap_format_16;
210 map->format.parse_val = regmap_parse_16;
214 if (!map->format.format_write &&
215 !(map->format.format_reg && map->format.format_val))
218 map->work_buf = kmalloc(map->format.buf_size, GFP_KERNEL);
219 if (map->work_buf == NULL) {
224 ret = regcache_init(map, config);
226 goto err_free_workbuf;
228 regmap_debugfs_init(map);
233 kfree(map->work_buf);
239 EXPORT_SYMBOL_GPL(regmap_init);
242 * regmap_exit(): Free a previously allocated register map
244 void regmap_exit(struct regmap *map)
247 regmap_debugfs_exit(map);
248 kfree(map->work_buf);
251 EXPORT_SYMBOL_GPL(regmap_exit);
253 static int _regmap_raw_write(struct regmap *map, unsigned int reg,
254 const void *val, size_t val_len)
256 u8 *u8 = map->work_buf;
262 /* Check for unwritable registers before we start */
263 if (map->writeable_reg)
264 for (i = 0; i < val_len / map->format.val_bytes; i++)
265 if (!map->writeable_reg(map->dev, reg + i))
268 map->format.format_reg(map->work_buf, reg);
270 u8[0] |= map->write_flag_mask;
272 trace_regmap_hw_write_start(map->dev, reg,
273 val_len / map->format.val_bytes);
275 /* If we're doing a single register write we can probably just
276 * send the work_buf directly, otherwise try to do a gather
279 if (val == map->work_buf + map->format.reg_bytes)
280 ret = map->bus->write(map->dev, map->work_buf,
281 map->format.reg_bytes + val_len);
282 else if (map->bus->gather_write)
283 ret = map->bus->gather_write(map->dev, map->work_buf,
284 map->format.reg_bytes,
287 /* If that didn't work fall back on linearising by hand. */
288 if (ret == -ENOTSUPP) {
289 len = map->format.reg_bytes + val_len;
290 buf = kmalloc(len, GFP_KERNEL);
294 memcpy(buf, map->work_buf, map->format.reg_bytes);
295 memcpy(buf + map->format.reg_bytes, val, val_len);
296 ret = map->bus->write(map->dev, buf, len);
301 trace_regmap_hw_write_done(map->dev, reg,
302 val_len / map->format.val_bytes);
307 int _regmap_write(struct regmap *map, unsigned int reg,
311 BUG_ON(!map->format.format_write && !map->format.format_val);
313 if (!map->cache_bypass) {
314 ret = regcache_write(map, reg, val);
317 if (map->cache_only) {
318 map->cache_dirty = true;
323 trace_regmap_reg_write(map->dev, reg, val);
325 if (map->format.format_write) {
326 map->format.format_write(map, reg, val);
328 trace_regmap_hw_write_start(map->dev, reg, 1);
330 ret = map->bus->write(map->dev, map->work_buf,
331 map->format.buf_size);
333 trace_regmap_hw_write_done(map->dev, reg, 1);
337 map->format.format_val(map->work_buf + map->format.reg_bytes,
339 return _regmap_raw_write(map, reg,
340 map->work_buf + map->format.reg_bytes,
341 map->format.val_bytes);
346 * regmap_write(): Write a value to a single register
348 * @map: Register map to write to
349 * @reg: Register to write to
350 * @val: Value to be written
352 * A value of zero will be returned on success, a negative errno will
353 * be returned in error cases.
355 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
359 mutex_lock(&map->lock);
361 ret = _regmap_write(map, reg, val);
363 mutex_unlock(&map->lock);
367 EXPORT_SYMBOL_GPL(regmap_write);
370 * regmap_raw_write(): Write raw values to one or more registers
372 * @map: Register map to write to
373 * @reg: Initial register to write to
374 * @val: Block of data to be written, laid out for direct transmission to the
376 * @val_len: Length of data pointed to by val.
378 * This function is intended to be used for things like firmware
379 * download where a large block of data needs to be transferred to the
380 * device. No formatting will be done on the data provided.
382 * A value of zero will be returned on success, a negative errno will
383 * be returned in error cases.
385 int regmap_raw_write(struct regmap *map, unsigned int reg,
386 const void *val, size_t val_len)
388 size_t val_count = val_len / map->format.val_bytes;
391 WARN_ON(!regmap_volatile_range(map, reg, val_count) &&
392 map->cache_type != REGCACHE_NONE);
394 mutex_lock(&map->lock);
396 ret = _regmap_raw_write(map, reg, val, val_len);
398 mutex_unlock(&map->lock);
402 EXPORT_SYMBOL_GPL(regmap_raw_write);
404 static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
405 unsigned int val_len)
407 u8 *u8 = map->work_buf;
410 map->format.format_reg(map->work_buf, reg);
413 * Some buses or devices flag reads by setting the high bits in the
414 * register addresss; since it's always the high bits for all
415 * current formats we can do this here rather than in
416 * formatting. This may break if we get interesting formats.
418 u8[0] |= map->read_flag_mask;
420 trace_regmap_hw_read_start(map->dev, reg,
421 val_len / map->format.val_bytes);
423 ret = map->bus->read(map->dev, map->work_buf, map->format.reg_bytes,
426 trace_regmap_hw_read_done(map->dev, reg,
427 val_len / map->format.val_bytes);
432 static int _regmap_read(struct regmap *map, unsigned int reg,
437 if (!map->format.parse_val)
440 if (!map->cache_bypass) {
441 ret = regcache_read(map, reg, val);
449 ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
451 *val = map->format.parse_val(map->work_buf);
452 trace_regmap_reg_read(map->dev, reg, *val);
459 * regmap_read(): Read a value from a single register
461 * @map: Register map to write to
462 * @reg: Register to be read from
463 * @val: Pointer to store read value
465 * A value of zero will be returned on success, a negative errno will
466 * be returned in error cases.
468 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
472 mutex_lock(&map->lock);
474 ret = _regmap_read(map, reg, val);
476 mutex_unlock(&map->lock);
480 EXPORT_SYMBOL_GPL(regmap_read);
483 * regmap_raw_read(): Read raw data from the device
485 * @map: Register map to write to
486 * @reg: First register to be read from
487 * @val: Pointer to store read value
488 * @val_len: Size of data to read
490 * A value of zero will be returned on success, a negative errno will
491 * be returned in error cases.
493 int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
496 size_t val_count = val_len / map->format.val_bytes;
499 WARN_ON(!regmap_volatile_range(map, reg, val_count) &&
500 map->cache_type != REGCACHE_NONE);
502 mutex_lock(&map->lock);
504 ret = _regmap_raw_read(map, reg, val, val_len);
506 mutex_unlock(&map->lock);
510 EXPORT_SYMBOL_GPL(regmap_raw_read);
513 * regmap_bulk_read(): Read multiple registers from the device
515 * @map: Register map to write to
516 * @reg: First register to be read from
517 * @val: Pointer to store read value, in native register size for device
518 * @val_count: Number of registers to read
520 * A value of zero will be returned on success, a negative errno will
521 * be returned in error cases.
523 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
527 size_t val_bytes = map->format.val_bytes;
528 bool vol = regmap_volatile_range(map, reg, val_count);
530 if (!map->format.parse_val)
533 if (vol || map->cache_type == REGCACHE_NONE) {
534 ret = regmap_raw_read(map, reg, val, val_bytes * val_count);
538 for (i = 0; i < val_count * val_bytes; i += val_bytes)
539 map->format.parse_val(val + i);
541 for (i = 0; i < val_count; i++) {
542 ret = regmap_read(map, reg + i, val + (i * val_bytes));
550 EXPORT_SYMBOL_GPL(regmap_bulk_read);
553 * regmap_update_bits: Perform a read/modify/write cycle on the register map
555 * @map: Register map to update
556 * @reg: Register to update
557 * @mask: Bitmask to change
558 * @val: New value for bitmask
560 * Returns zero for success, a negative number on error.
562 int regmap_update_bits(struct regmap *map, unsigned int reg,
563 unsigned int mask, unsigned int val)
568 mutex_lock(&map->lock);
570 ret = _regmap_read(map, reg, &tmp);
577 ret = _regmap_write(map, reg, tmp);
580 mutex_unlock(&map->lock);
584 EXPORT_SYMBOL_GPL(regmap_update_bits);
586 static int __init regmap_initcall(void)
588 regmap_debugfs_initcall();
592 postcore_initcall(regmap_initcall);