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/device.h>
14 #include <linux/slab.h>
15 #include <linux/export.h>
16 #include <linux/mutex.h>
17 #include <linux/err.h>
19 #define CREATE_TRACE_POINTS
20 #include <trace/events/regmap.h>
24 bool regmap_writeable(struct regmap *map, unsigned int reg)
26 if (map->max_register && reg > map->max_register)
29 if (map->writeable_reg)
30 return map->writeable_reg(map->dev, reg);
35 bool regmap_readable(struct regmap *map, unsigned int reg)
37 if (map->max_register && reg > map->max_register)
40 if (map->format.format_write)
43 if (map->readable_reg)
44 return map->readable_reg(map->dev, reg);
49 bool regmap_volatile(struct regmap *map, unsigned int reg)
51 if (!regmap_readable(map, reg))
54 if (map->volatile_reg)
55 return map->volatile_reg(map->dev, reg);
60 bool regmap_precious(struct regmap *map, unsigned int reg)
62 if (!regmap_readable(map, reg))
65 if (map->precious_reg)
66 return map->precious_reg(map->dev, reg);
71 static bool regmap_volatile_range(struct regmap *map, unsigned int reg,
76 for (i = 0; i < num; i++)
77 if (!regmap_volatile(map, reg + i))
83 static void regmap_format_2_6_write(struct regmap *map,
84 unsigned int reg, unsigned int val)
86 u8 *out = map->work_buf;
88 *out = (reg << 6) | val;
91 static void regmap_format_4_12_write(struct regmap *map,
92 unsigned int reg, unsigned int val)
94 __be16 *out = map->work_buf;
95 *out = cpu_to_be16((reg << 12) | val);
98 static void regmap_format_7_9_write(struct regmap *map,
99 unsigned int reg, unsigned int val)
101 __be16 *out = map->work_buf;
102 *out = cpu_to_be16((reg << 9) | val);
105 static void regmap_format_10_14_write(struct regmap *map,
106 unsigned int reg, unsigned int val)
108 u8 *out = map->work_buf;
111 out[1] = (val >> 8) | (reg << 6);
115 static void regmap_format_8(void *buf, unsigned int val)
122 static void regmap_format_16(void *buf, unsigned int val)
126 b[0] = cpu_to_be16(val);
129 static void regmap_format_32(void *buf, unsigned int val)
133 b[0] = cpu_to_be32(val);
136 static unsigned int regmap_parse_8(void *buf)
143 static unsigned int regmap_parse_16(void *buf)
147 b[0] = be16_to_cpu(b[0]);
152 static unsigned int regmap_parse_32(void *buf)
156 b[0] = be32_to_cpu(b[0]);
162 * regmap_init(): Initialise register map
164 * @dev: Device that will be interacted with
165 * @bus: Bus-specific callbacks to use with device
166 * @config: Configuration for register map
168 * The return value will be an ERR_PTR() on error or a valid pointer to
169 * a struct regmap. This function should generally not be called
170 * directly, it should be called by bus-specific init functions.
172 struct regmap *regmap_init(struct device *dev,
173 const struct regmap_bus *bus,
174 const struct regmap_config *config)
182 map = kzalloc(sizeof(*map), GFP_KERNEL);
188 mutex_init(&map->lock);
189 map->format.buf_size = (config->reg_bits + config->val_bits) / 8;
190 map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8);
191 map->format.pad_bytes = config->pad_bits / 8;
192 map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8);
193 map->format.buf_size += map->format.pad_bytes;
196 map->max_register = config->max_register;
197 map->writeable_reg = config->writeable_reg;
198 map->readable_reg = config->readable_reg;
199 map->volatile_reg = config->volatile_reg;
200 map->precious_reg = config->precious_reg;
201 map->cache_type = config->cache_type;
203 if (config->read_flag_mask || config->write_flag_mask) {
204 map->read_flag_mask = config->read_flag_mask;
205 map->write_flag_mask = config->write_flag_mask;
207 map->read_flag_mask = bus->read_flag_mask;
210 switch (config->reg_bits) {
212 switch (config->val_bits) {
214 map->format.format_write = regmap_format_2_6_write;
222 switch (config->val_bits) {
224 map->format.format_write = regmap_format_4_12_write;
232 switch (config->val_bits) {
234 map->format.format_write = regmap_format_7_9_write;
242 switch (config->val_bits) {
244 map->format.format_write = regmap_format_10_14_write;
252 map->format.format_reg = regmap_format_8;
256 map->format.format_reg = regmap_format_16;
260 map->format.format_reg = regmap_format_32;
267 switch (config->val_bits) {
269 map->format.format_val = regmap_format_8;
270 map->format.parse_val = regmap_parse_8;
273 map->format.format_val = regmap_format_16;
274 map->format.parse_val = regmap_parse_16;
277 map->format.format_val = regmap_format_32;
278 map->format.parse_val = regmap_parse_32;
282 if (!map->format.format_write &&
283 !(map->format.format_reg && map->format.format_val))
286 map->work_buf = kzalloc(map->format.buf_size, GFP_KERNEL);
287 if (map->work_buf == NULL) {
292 regmap_debugfs_init(map);
294 ret = regcache_init(map, config);
296 goto err_free_workbuf;
301 kfree(map->work_buf);
307 EXPORT_SYMBOL_GPL(regmap_init);
310 * regmap_reinit_cache(): Reinitialise the current register cache
312 * @map: Register map to operate on.
313 * @config: New configuration. Only the cache data will be used.
315 * Discard any existing register cache for the map and initialize a
316 * new cache. This can be used to restore the cache to defaults or to
317 * update the cache configuration to reflect runtime discovery of the
320 int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
324 mutex_lock(&map->lock);
327 regmap_debugfs_exit(map);
329 map->max_register = config->max_register;
330 map->writeable_reg = config->writeable_reg;
331 map->readable_reg = config->readable_reg;
332 map->volatile_reg = config->volatile_reg;
333 map->precious_reg = config->precious_reg;
334 map->cache_type = config->cache_type;
336 regmap_debugfs_init(map);
338 ret = regcache_init(map, config);
340 mutex_unlock(&map->lock);
346 * regmap_exit(): Free a previously allocated register map
348 void regmap_exit(struct regmap *map)
351 regmap_debugfs_exit(map);
352 kfree(map->work_buf);
355 EXPORT_SYMBOL_GPL(regmap_exit);
357 static int _regmap_raw_write(struct regmap *map, unsigned int reg,
358 const void *val, size_t val_len)
360 u8 *u8 = map->work_buf;
366 /* Check for unwritable registers before we start */
367 if (map->writeable_reg)
368 for (i = 0; i < val_len / map->format.val_bytes; i++)
369 if (!map->writeable_reg(map->dev, reg + i))
372 map->format.format_reg(map->work_buf, reg);
374 u8[0] |= map->write_flag_mask;
376 trace_regmap_hw_write_start(map->dev, reg,
377 val_len / map->format.val_bytes);
379 /* If we're doing a single register write we can probably just
380 * send the work_buf directly, otherwise try to do a gather
383 if (val == (map->work_buf + map->format.pad_bytes +
384 map->format.reg_bytes))
385 ret = map->bus->write(map->dev, map->work_buf,
386 map->format.reg_bytes +
387 map->format.pad_bytes +
389 else if (map->bus->gather_write)
390 ret = map->bus->gather_write(map->dev, map->work_buf,
391 map->format.reg_bytes +
392 map->format.pad_bytes,
395 /* If that didn't work fall back on linearising by hand. */
396 if (ret == -ENOTSUPP) {
397 len = map->format.reg_bytes + map->format.pad_bytes + val_len;
398 buf = kzalloc(len, GFP_KERNEL);
402 memcpy(buf, map->work_buf, map->format.reg_bytes);
403 memcpy(buf + map->format.reg_bytes + map->format.pad_bytes,
405 ret = map->bus->write(map->dev, buf, len);
410 trace_regmap_hw_write_done(map->dev, reg,
411 val_len / map->format.val_bytes);
416 int _regmap_write(struct regmap *map, unsigned int reg,
420 BUG_ON(!map->format.format_write && !map->format.format_val);
422 if (!map->cache_bypass) {
423 ret = regcache_write(map, reg, val);
426 if (map->cache_only) {
427 map->cache_dirty = true;
432 trace_regmap_reg_write(map->dev, reg, val);
434 if (map->format.format_write) {
435 map->format.format_write(map, reg, val);
437 trace_regmap_hw_write_start(map->dev, reg, 1);
439 ret = map->bus->write(map->dev, map->work_buf,
440 map->format.buf_size);
442 trace_regmap_hw_write_done(map->dev, reg, 1);
446 map->format.format_val(map->work_buf + map->format.reg_bytes
447 + map->format.pad_bytes, val);
448 return _regmap_raw_write(map, reg,
450 map->format.reg_bytes +
451 map->format.pad_bytes,
452 map->format.val_bytes);
457 * regmap_write(): Write a value to a single register
459 * @map: Register map to write to
460 * @reg: Register to write to
461 * @val: Value to be written
463 * A value of zero will be returned on success, a negative errno will
464 * be returned in error cases.
466 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
470 mutex_lock(&map->lock);
472 ret = _regmap_write(map, reg, val);
474 mutex_unlock(&map->lock);
478 EXPORT_SYMBOL_GPL(regmap_write);
481 * regmap_raw_write(): Write raw values to one or more registers
483 * @map: Register map to write to
484 * @reg: Initial register to write to
485 * @val: Block of data to be written, laid out for direct transmission to the
487 * @val_len: Length of data pointed to by val.
489 * This function is intended to be used for things like firmware
490 * download where a large block of data needs to be transferred to the
491 * device. No formatting will be done on the data provided.
493 * A value of zero will be returned on success, a negative errno will
494 * be returned in error cases.
496 int regmap_raw_write(struct regmap *map, unsigned int reg,
497 const void *val, size_t val_len)
499 size_t val_count = val_len / map->format.val_bytes;
502 WARN_ON(!regmap_volatile_range(map, reg, val_count) &&
503 map->cache_type != REGCACHE_NONE);
505 mutex_lock(&map->lock);
507 ret = _regmap_raw_write(map, reg, val, val_len);
509 mutex_unlock(&map->lock);
513 EXPORT_SYMBOL_GPL(regmap_raw_write);
515 static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
516 unsigned int val_len)
518 u8 *u8 = map->work_buf;
521 map->format.format_reg(map->work_buf, reg);
524 * Some buses or devices flag reads by setting the high bits in the
525 * register addresss; since it's always the high bits for all
526 * current formats we can do this here rather than in
527 * formatting. This may break if we get interesting formats.
529 u8[0] |= map->read_flag_mask;
531 trace_regmap_hw_read_start(map->dev, reg,
532 val_len / map->format.val_bytes);
534 ret = map->bus->read(map->dev, map->work_buf,
535 map->format.reg_bytes + map->format.pad_bytes,
538 trace_regmap_hw_read_done(map->dev, reg,
539 val_len / map->format.val_bytes);
544 static int _regmap_read(struct regmap *map, unsigned int reg,
549 if (!map->cache_bypass) {
550 ret = regcache_read(map, reg, val);
555 if (!map->format.parse_val)
561 ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
563 *val = map->format.parse_val(map->work_buf);
564 trace_regmap_reg_read(map->dev, reg, *val);
571 * regmap_read(): Read a value from a single register
573 * @map: Register map to write to
574 * @reg: Register to be read from
575 * @val: Pointer to store read value
577 * A value of zero will be returned on success, a negative errno will
578 * be returned in error cases.
580 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
584 mutex_lock(&map->lock);
586 ret = _regmap_read(map, reg, val);
588 mutex_unlock(&map->lock);
592 EXPORT_SYMBOL_GPL(regmap_read);
595 * regmap_raw_read(): Read raw data from the device
597 * @map: Register map to write to
598 * @reg: First register to be read from
599 * @val: Pointer to store read value
600 * @val_len: Size of data to read
602 * A value of zero will be returned on success, a negative errno will
603 * be returned in error cases.
605 int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
608 size_t val_bytes = map->format.val_bytes;
609 size_t val_count = val_len / val_bytes;
613 mutex_lock(&map->lock);
615 if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
616 map->cache_type == REGCACHE_NONE) {
617 /* Physical block read if there's no cache involved */
618 ret = _regmap_raw_read(map, reg, val, val_len);
621 /* Otherwise go word by word for the cache; should be low
622 * cost as we expect to hit the cache.
624 for (i = 0; i < val_count; i++) {
625 ret = _regmap_read(map, reg + i, &v);
629 map->format.format_val(val + (i * val_bytes), v);
634 mutex_unlock(&map->lock);
638 EXPORT_SYMBOL_GPL(regmap_raw_read);
641 * regmap_bulk_read(): Read multiple registers from the device
643 * @map: Register map to write to
644 * @reg: First register to be read from
645 * @val: Pointer to store read value, in native register size for device
646 * @val_count: Number of registers to read
648 * A value of zero will be returned on success, a negative errno will
649 * be returned in error cases.
651 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
655 size_t val_bytes = map->format.val_bytes;
656 bool vol = regmap_volatile_range(map, reg, val_count);
658 if (!map->format.parse_val)
661 if (vol || map->cache_type == REGCACHE_NONE) {
662 ret = regmap_raw_read(map, reg, val, val_bytes * val_count);
666 for (i = 0; i < val_count * val_bytes; i += val_bytes)
667 map->format.parse_val(val + i);
669 for (i = 0; i < val_count; i++) {
670 ret = regmap_read(map, reg + i, val + (i * val_bytes));
678 EXPORT_SYMBOL_GPL(regmap_bulk_read);
680 static int _regmap_update_bits(struct regmap *map, unsigned int reg,
681 unsigned int mask, unsigned int val,
685 unsigned int tmp, orig;
687 mutex_lock(&map->lock);
689 ret = _regmap_read(map, reg, &orig);
697 ret = _regmap_write(map, reg, tmp);
704 mutex_unlock(&map->lock);
710 * regmap_update_bits: Perform a read/modify/write cycle on the register map
712 * @map: Register map to update
713 * @reg: Register to update
714 * @mask: Bitmask to change
715 * @val: New value for bitmask
717 * Returns zero for success, a negative number on error.
719 int regmap_update_bits(struct regmap *map, unsigned int reg,
720 unsigned int mask, unsigned int val)
723 return _regmap_update_bits(map, reg, mask, val, &change);
725 EXPORT_SYMBOL_GPL(regmap_update_bits);
728 * regmap_update_bits_check: Perform a read/modify/write cycle on the
729 * register map and report if updated
731 * @map: Register map to update
732 * @reg: Register to update
733 * @mask: Bitmask to change
734 * @val: New value for bitmask
735 * @change: Boolean indicating if a write was done
737 * Returns zero for success, a negative number on error.
739 int regmap_update_bits_check(struct regmap *map, unsigned int reg,
740 unsigned int mask, unsigned int val,
743 return _regmap_update_bits(map, reg, mask, val, change);
745 EXPORT_SYMBOL_GPL(regmap_update_bits_check);
747 static int __init regmap_initcall(void)
749 regmap_debugfs_initcall();
753 postcore_initcall(regmap_initcall);