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_10_14_write(struct regmap *map,
94 unsigned int reg, unsigned int val)
96 u8 *out = map->work_buf;
99 out[1] = (val >> 8) | (reg << 6);
103 static void regmap_format_8(void *buf, unsigned int val)
110 static void regmap_format_16(void *buf, unsigned int val)
114 b[0] = cpu_to_be16(val);
117 static unsigned int regmap_parse_8(void *buf)
124 static unsigned int regmap_parse_16(void *buf)
128 b[0] = be16_to_cpu(b[0]);
134 * regmap_init(): Initialise register map
136 * @dev: Device that will be interacted with
137 * @bus: Bus-specific callbacks to use with device
138 * @config: Configuration for register map
140 * The return value will be an ERR_PTR() on error or a valid pointer to
141 * a struct regmap. This function should generally not be called
142 * directly, it should be called by bus-specific init functions.
144 struct regmap *regmap_init(struct device *dev,
145 const struct regmap_bus *bus,
146 const struct regmap_config *config)
154 map = kzalloc(sizeof(*map), GFP_KERNEL);
160 mutex_init(&map->lock);
161 map->format.buf_size = (config->reg_bits + config->val_bits) / 8;
162 map->format.reg_bytes = config->reg_bits / 8;
163 map->format.val_bytes = config->val_bits / 8;
166 map->max_register = config->max_register;
167 map->writeable_reg = config->writeable_reg;
168 map->readable_reg = config->readable_reg;
169 map->volatile_reg = config->volatile_reg;
170 map->precious_reg = config->precious_reg;
171 map->cache_type = config->cache_type;
173 if (config->read_flag_mask || config->write_flag_mask) {
174 map->read_flag_mask = config->read_flag_mask;
175 map->write_flag_mask = config->write_flag_mask;
177 map->read_flag_mask = bus->read_flag_mask;
180 switch (config->reg_bits) {
182 switch (config->val_bits) {
184 map->format.format_write = regmap_format_4_12_write;
192 switch (config->val_bits) {
194 map->format.format_write = regmap_format_7_9_write;
202 switch (config->val_bits) {
204 map->format.format_write = regmap_format_10_14_write;
212 map->format.format_reg = regmap_format_8;
216 map->format.format_reg = regmap_format_16;
223 switch (config->val_bits) {
225 map->format.format_val = regmap_format_8;
226 map->format.parse_val = regmap_parse_8;
229 map->format.format_val = regmap_format_16;
230 map->format.parse_val = regmap_parse_16;
234 if (!map->format.format_write &&
235 !(map->format.format_reg && map->format.format_val))
238 map->work_buf = kmalloc(map->format.buf_size, GFP_KERNEL);
239 if (map->work_buf == NULL) {
244 regmap_debugfs_init(map);
246 ret = regcache_init(map, config);
248 goto err_free_workbuf;
253 kfree(map->work_buf);
259 EXPORT_SYMBOL_GPL(regmap_init);
262 * regmap_exit(): Free a previously allocated register map
264 void regmap_exit(struct regmap *map)
267 regmap_debugfs_exit(map);
268 kfree(map->work_buf);
271 EXPORT_SYMBOL_GPL(regmap_exit);
273 static int _regmap_raw_write(struct regmap *map, unsigned int reg,
274 const void *val, size_t val_len)
276 u8 *u8 = map->work_buf;
282 /* Check for unwritable registers before we start */
283 if (map->writeable_reg)
284 for (i = 0; i < val_len / map->format.val_bytes; i++)
285 if (!map->writeable_reg(map->dev, reg + i))
288 map->format.format_reg(map->work_buf, reg);
290 u8[0] |= map->write_flag_mask;
292 trace_regmap_hw_write_start(map->dev, reg,
293 val_len / map->format.val_bytes);
295 /* If we're doing a single register write we can probably just
296 * send the work_buf directly, otherwise try to do a gather
299 if (val == map->work_buf + map->format.reg_bytes)
300 ret = map->bus->write(map->dev, map->work_buf,
301 map->format.reg_bytes + val_len);
302 else if (map->bus->gather_write)
303 ret = map->bus->gather_write(map->dev, map->work_buf,
304 map->format.reg_bytes,
307 /* If that didn't work fall back on linearising by hand. */
308 if (ret == -ENOTSUPP) {
309 len = map->format.reg_bytes + val_len;
310 buf = kmalloc(len, GFP_KERNEL);
314 memcpy(buf, map->work_buf, map->format.reg_bytes);
315 memcpy(buf + map->format.reg_bytes, val, val_len);
316 ret = map->bus->write(map->dev, buf, len);
321 trace_regmap_hw_write_done(map->dev, reg,
322 val_len / map->format.val_bytes);
327 int _regmap_write(struct regmap *map, unsigned int reg,
331 BUG_ON(!map->format.format_write && !map->format.format_val);
333 if (!map->cache_bypass) {
334 ret = regcache_write(map, reg, val);
337 if (map->cache_only) {
338 map->cache_dirty = true;
343 trace_regmap_reg_write(map->dev, reg, val);
345 if (map->format.format_write) {
346 map->format.format_write(map, reg, val);
348 trace_regmap_hw_write_start(map->dev, reg, 1);
350 ret = map->bus->write(map->dev, map->work_buf,
351 map->format.buf_size);
353 trace_regmap_hw_write_done(map->dev, reg, 1);
357 map->format.format_val(map->work_buf + map->format.reg_bytes,
359 return _regmap_raw_write(map, reg,
360 map->work_buf + map->format.reg_bytes,
361 map->format.val_bytes);
366 * regmap_write(): Write a value to a single register
368 * @map: Register map to write to
369 * @reg: Register to write to
370 * @val: Value to be written
372 * A value of zero will be returned on success, a negative errno will
373 * be returned in error cases.
375 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
379 mutex_lock(&map->lock);
381 ret = _regmap_write(map, reg, val);
383 mutex_unlock(&map->lock);
387 EXPORT_SYMBOL_GPL(regmap_write);
390 * regmap_raw_write(): Write raw values to one or more registers
392 * @map: Register map to write to
393 * @reg: Initial register to write to
394 * @val: Block of data to be written, laid out for direct transmission to the
396 * @val_len: Length of data pointed to by val.
398 * This function is intended to be used for things like firmware
399 * download where a large block of data needs to be transferred to the
400 * device. No formatting will be done on the data provided.
402 * A value of zero will be returned on success, a negative errno will
403 * be returned in error cases.
405 int regmap_raw_write(struct regmap *map, unsigned int reg,
406 const void *val, size_t val_len)
408 size_t val_count = val_len / map->format.val_bytes;
411 WARN_ON(!regmap_volatile_range(map, reg, val_count) &&
412 map->cache_type != REGCACHE_NONE);
414 mutex_lock(&map->lock);
416 ret = _regmap_raw_write(map, reg, val, val_len);
418 mutex_unlock(&map->lock);
422 EXPORT_SYMBOL_GPL(regmap_raw_write);
424 static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
425 unsigned int val_len)
427 u8 *u8 = map->work_buf;
430 map->format.format_reg(map->work_buf, reg);
433 * Some buses or devices flag reads by setting the high bits in the
434 * register addresss; since it's always the high bits for all
435 * current formats we can do this here rather than in
436 * formatting. This may break if we get interesting formats.
438 u8[0] |= map->read_flag_mask;
440 trace_regmap_hw_read_start(map->dev, reg,
441 val_len / map->format.val_bytes);
443 ret = map->bus->read(map->dev, map->work_buf, map->format.reg_bytes,
446 trace_regmap_hw_read_done(map->dev, reg,
447 val_len / map->format.val_bytes);
452 static int _regmap_read(struct regmap *map, unsigned int reg,
457 if (!map->cache_bypass) {
458 ret = regcache_read(map, reg, val);
463 if (!map->format.parse_val)
469 ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
471 *val = map->format.parse_val(map->work_buf);
472 trace_regmap_reg_read(map->dev, reg, *val);
479 * regmap_read(): Read a value from a single register
481 * @map: Register map to write to
482 * @reg: Register to be read from
483 * @val: Pointer to store read value
485 * A value of zero will be returned on success, a negative errno will
486 * be returned in error cases.
488 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
492 mutex_lock(&map->lock);
494 ret = _regmap_read(map, reg, val);
496 mutex_unlock(&map->lock);
500 EXPORT_SYMBOL_GPL(regmap_read);
503 * regmap_raw_read(): Read raw data from the device
505 * @map: Register map to write to
506 * @reg: First register to be read from
507 * @val: Pointer to store read value
508 * @val_len: Size of data to read
510 * A value of zero will be returned on success, a negative errno will
511 * be returned in error cases.
513 int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
516 size_t val_count = val_len / map->format.val_bytes;
519 WARN_ON(!regmap_volatile_range(map, reg, val_count) &&
520 map->cache_type != REGCACHE_NONE);
522 mutex_lock(&map->lock);
524 ret = _regmap_raw_read(map, reg, val, val_len);
526 mutex_unlock(&map->lock);
530 EXPORT_SYMBOL_GPL(regmap_raw_read);
533 * regmap_bulk_read(): Read multiple registers from the device
535 * @map: Register map to write to
536 * @reg: First register to be read from
537 * @val: Pointer to store read value, in native register size for device
538 * @val_count: Number of registers to read
540 * A value of zero will be returned on success, a negative errno will
541 * be returned in error cases.
543 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
547 size_t val_bytes = map->format.val_bytes;
548 bool vol = regmap_volatile_range(map, reg, val_count);
550 if (!map->format.parse_val)
553 if (vol || map->cache_type == REGCACHE_NONE) {
554 ret = regmap_raw_read(map, reg, val, val_bytes * val_count);
558 for (i = 0; i < val_count * val_bytes; i += val_bytes)
559 map->format.parse_val(val + i);
561 for (i = 0; i < val_count; i++) {
562 ret = regmap_read(map, reg + i, val + (i * val_bytes));
570 EXPORT_SYMBOL_GPL(regmap_bulk_read);
572 static int _regmap_update_bits(struct regmap *map, unsigned int reg,
573 unsigned int mask, unsigned int val,
577 unsigned int tmp, orig;
579 mutex_lock(&map->lock);
581 ret = _regmap_read(map, reg, &orig);
589 ret = _regmap_write(map, reg, tmp);
596 mutex_unlock(&map->lock);
602 * regmap_update_bits: Perform a read/modify/write cycle on the register map
604 * @map: Register map to update
605 * @reg: Register to update
606 * @mask: Bitmask to change
607 * @val: New value for bitmask
609 * Returns zero for success, a negative number on error.
611 int regmap_update_bits(struct regmap *map, unsigned int reg,
612 unsigned int mask, unsigned int val)
615 return _regmap_update_bits(map, reg, mask, val, &change);
617 EXPORT_SYMBOL_GPL(regmap_update_bits);
620 * regmap_update_bits_check: Perform a read/modify/write cycle on the
621 * register map and report if updated
623 * @map: Register map to update
624 * @reg: Register to update
625 * @mask: Bitmask to change
626 * @val: New value for bitmask
627 * @change: Boolean indicating if a write was done
629 * Returns zero for success, a negative number on error.
631 int regmap_update_bits_check(struct regmap *map, unsigned int reg,
632 unsigned int mask, unsigned int val,
635 return _regmap_update_bits(map, reg, mask, val, change);
637 EXPORT_SYMBOL_GPL(regmap_update_bits_check);
639 static int __init regmap_initcall(void)
641 regmap_debugfs_initcall();
645 postcore_initcall(regmap_initcall);