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
);
309 static void devm_regmap_release(struct device
*dev
, void *res
)
311 regmap_exit(*(struct regmap
**)res
);
315 * devm_regmap_init(): Initialise managed register map
317 * @dev: Device that will be interacted with
318 * @bus: Bus-specific callbacks to use with device
319 * @config: Configuration for register map
321 * The return value will be an ERR_PTR() on error or a valid pointer
322 * to a struct regmap. This function should generally not be called
323 * directly, it should be called by bus-specific init functions. The
324 * map will be automatically freed by the device management code.
326 struct regmap
*devm_regmap_init(struct device
*dev
,
327 const struct regmap_bus
*bus
,
328 const struct regmap_config
*config
)
330 struct regmap
**ptr
, *regmap
;
332 ptr
= devres_alloc(devm_regmap_release
, sizeof(*ptr
), GFP_KERNEL
);
334 return ERR_PTR(-ENOMEM
);
336 regmap
= regmap_init(dev
, bus
, config
);
337 if (!IS_ERR(regmap
)) {
339 devres_add(dev
, ptr
);
346 EXPORT_SYMBOL_GPL(devm_regmap_init
);
349 * regmap_reinit_cache(): Reinitialise the current register cache
351 * @map: Register map to operate on.
352 * @config: New configuration. Only the cache data will be used.
354 * Discard any existing register cache for the map and initialize a
355 * new cache. This can be used to restore the cache to defaults or to
356 * update the cache configuration to reflect runtime discovery of the
359 int regmap_reinit_cache(struct regmap
*map
, const struct regmap_config
*config
)
363 mutex_lock(&map
->lock
);
366 regmap_debugfs_exit(map
);
368 map
->max_register
= config
->max_register
;
369 map
->writeable_reg
= config
->writeable_reg
;
370 map
->readable_reg
= config
->readable_reg
;
371 map
->volatile_reg
= config
->volatile_reg
;
372 map
->precious_reg
= config
->precious_reg
;
373 map
->cache_type
= config
->cache_type
;
375 regmap_debugfs_init(map
);
377 map
->cache_bypass
= false;
378 map
->cache_only
= false;
380 ret
= regcache_init(map
, config
);
382 mutex_unlock(&map
->lock
);
388 * regmap_exit(): Free a previously allocated register map
390 void regmap_exit(struct regmap
*map
)
393 regmap_debugfs_exit(map
);
394 kfree(map
->work_buf
);
397 EXPORT_SYMBOL_GPL(regmap_exit
);
399 static int _regmap_raw_write(struct regmap
*map
, unsigned int reg
,
400 const void *val
, size_t val_len
)
402 u8
*u8
= map
->work_buf
;
408 /* Check for unwritable registers before we start */
409 if (map
->writeable_reg
)
410 for (i
= 0; i
< val_len
/ map
->format
.val_bytes
; i
++)
411 if (!map
->writeable_reg(map
->dev
, reg
+ i
))
414 if (!map
->cache_bypass
&& map
->format
.parse_val
) {
416 int val_bytes
= map
->format
.val_bytes
;
417 for (i
= 0; i
< val_len
/ val_bytes
; i
++) {
418 memcpy(map
->work_buf
, val
+ (i
* val_bytes
), val_bytes
);
419 ival
= map
->format
.parse_val(map
->work_buf
);
420 ret
= regcache_write(map
, reg
+ i
, ival
);
423 "Error in caching of register: %u ret: %d\n",
428 if (map
->cache_only
) {
429 map
->cache_dirty
= true;
434 map
->format
.format_reg(map
->work_buf
, reg
);
436 u8
[0] |= map
->write_flag_mask
;
438 trace_regmap_hw_write_start(map
->dev
, reg
,
439 val_len
/ map
->format
.val_bytes
);
441 /* If we're doing a single register write we can probably just
442 * send the work_buf directly, otherwise try to do a gather
445 if (val
== (map
->work_buf
+ map
->format
.pad_bytes
+
446 map
->format
.reg_bytes
))
447 ret
= map
->bus
->write(map
->dev
, map
->work_buf
,
448 map
->format
.reg_bytes
+
449 map
->format
.pad_bytes
+
451 else if (map
->bus
->gather_write
)
452 ret
= map
->bus
->gather_write(map
->dev
, map
->work_buf
,
453 map
->format
.reg_bytes
+
454 map
->format
.pad_bytes
,
457 /* If that didn't work fall back on linearising by hand. */
458 if (ret
== -ENOTSUPP
) {
459 len
= map
->format
.reg_bytes
+ map
->format
.pad_bytes
+ val_len
;
460 buf
= kzalloc(len
, GFP_KERNEL
);
464 memcpy(buf
, map
->work_buf
, map
->format
.reg_bytes
);
465 memcpy(buf
+ map
->format
.reg_bytes
+ map
->format
.pad_bytes
,
467 ret
= map
->bus
->write(map
->dev
, buf
, len
);
472 trace_regmap_hw_write_done(map
->dev
, reg
,
473 val_len
/ map
->format
.val_bytes
);
478 int _regmap_write(struct regmap
*map
, unsigned int reg
,
482 BUG_ON(!map
->format
.format_write
&& !map
->format
.format_val
);
484 if (!map
->cache_bypass
&& map
->format
.format_write
) {
485 ret
= regcache_write(map
, reg
, val
);
488 if (map
->cache_only
) {
489 map
->cache_dirty
= true;
494 trace_regmap_reg_write(map
->dev
, reg
, val
);
496 if (map
->format
.format_write
) {
497 map
->format
.format_write(map
, reg
, val
);
499 trace_regmap_hw_write_start(map
->dev
, reg
, 1);
501 ret
= map
->bus
->write(map
->dev
, map
->work_buf
,
502 map
->format
.buf_size
);
504 trace_regmap_hw_write_done(map
->dev
, reg
, 1);
508 map
->format
.format_val(map
->work_buf
+ map
->format
.reg_bytes
509 + map
->format
.pad_bytes
, val
);
510 return _regmap_raw_write(map
, reg
,
512 map
->format
.reg_bytes
+
513 map
->format
.pad_bytes
,
514 map
->format
.val_bytes
);
519 * regmap_write(): Write a value to a single register
521 * @map: Register map to write to
522 * @reg: Register to write to
523 * @val: Value to be written
525 * A value of zero will be returned on success, a negative errno will
526 * be returned in error cases.
528 int regmap_write(struct regmap
*map
, unsigned int reg
, unsigned int val
)
532 mutex_lock(&map
->lock
);
534 ret
= _regmap_write(map
, reg
, val
);
536 mutex_unlock(&map
->lock
);
540 EXPORT_SYMBOL_GPL(regmap_write
);
543 * regmap_raw_write(): Write raw values to one or more registers
545 * @map: Register map to write to
546 * @reg: Initial register to write to
547 * @val: Block of data to be written, laid out for direct transmission to the
549 * @val_len: Length of data pointed to by val.
551 * This function is intended to be used for things like firmware
552 * download where a large block of data needs to be transferred to the
553 * device. No formatting will be done on the data provided.
555 * A value of zero will be returned on success, a negative errno will
556 * be returned in error cases.
558 int regmap_raw_write(struct regmap
*map
, unsigned int reg
,
559 const void *val
, size_t val_len
)
563 mutex_lock(&map
->lock
);
565 ret
= _regmap_raw_write(map
, reg
, val
, val_len
);
567 mutex_unlock(&map
->lock
);
571 EXPORT_SYMBOL_GPL(regmap_raw_write
);
574 * regmap_bulk_write(): Write multiple registers to the device
576 * @map: Register map to write to
577 * @reg: First register to be write from
578 * @val: Block of data to be written, in native register size for device
579 * @val_count: Number of registers to write
581 * This function is intended to be used for writing a large block of
582 * data to be device either in single transfer or multiple transfer.
584 * A value of zero will be returned on success, a negative errno will
585 * be returned in error cases.
587 int regmap_bulk_write(struct regmap
*map
, unsigned int reg
, const void *val
,
591 size_t val_bytes
= map
->format
.val_bytes
;
594 if (!map
->format
.parse_val
)
597 mutex_lock(&map
->lock
);
599 /* No formatting is require if val_byte is 1 */
600 if (val_bytes
== 1) {
603 wval
= kmemdup(val
, val_count
* val_bytes
, GFP_KERNEL
);
606 dev_err(map
->dev
, "Error in memory allocation\n");
609 for (i
= 0; i
< val_count
* val_bytes
; i
+= val_bytes
)
610 map
->format
.parse_val(wval
+ i
);
612 ret
= _regmap_raw_write(map
, reg
, wval
, val_bytes
* val_count
);
618 mutex_unlock(&map
->lock
);
621 EXPORT_SYMBOL_GPL(regmap_bulk_write
);
623 static int _regmap_raw_read(struct regmap
*map
, unsigned int reg
, void *val
,
624 unsigned int val_len
)
626 u8
*u8
= map
->work_buf
;
629 map
->format
.format_reg(map
->work_buf
, reg
);
632 * Some buses or devices flag reads by setting the high bits in the
633 * register addresss; since it's always the high bits for all
634 * current formats we can do this here rather than in
635 * formatting. This may break if we get interesting formats.
637 u8
[0] |= map
->read_flag_mask
;
639 trace_regmap_hw_read_start(map
->dev
, reg
,
640 val_len
/ map
->format
.val_bytes
);
642 ret
= map
->bus
->read(map
->dev
, map
->work_buf
,
643 map
->format
.reg_bytes
+ map
->format
.pad_bytes
,
646 trace_regmap_hw_read_done(map
->dev
, reg
,
647 val_len
/ map
->format
.val_bytes
);
652 static int _regmap_read(struct regmap
*map
, unsigned int reg
,
657 if (!map
->cache_bypass
) {
658 ret
= regcache_read(map
, reg
, val
);
663 if (!map
->format
.parse_val
)
669 ret
= _regmap_raw_read(map
, reg
, map
->work_buf
, map
->format
.val_bytes
);
671 *val
= map
->format
.parse_val(map
->work_buf
);
672 trace_regmap_reg_read(map
->dev
, reg
, *val
);
679 * regmap_read(): Read a value from a single register
681 * @map: Register map to write to
682 * @reg: Register to be read from
683 * @val: Pointer to store read value
685 * A value of zero will be returned on success, a negative errno will
686 * be returned in error cases.
688 int regmap_read(struct regmap
*map
, unsigned int reg
, unsigned int *val
)
692 mutex_lock(&map
->lock
);
694 ret
= _regmap_read(map
, reg
, val
);
696 mutex_unlock(&map
->lock
);
700 EXPORT_SYMBOL_GPL(regmap_read
);
703 * regmap_raw_read(): Read raw data from the device
705 * @map: Register map to write to
706 * @reg: First register to be read from
707 * @val: Pointer to store read value
708 * @val_len: Size of data to read
710 * A value of zero will be returned on success, a negative errno will
711 * be returned in error cases.
713 int regmap_raw_read(struct regmap
*map
, unsigned int reg
, void *val
,
716 size_t val_bytes
= map
->format
.val_bytes
;
717 size_t val_count
= val_len
/ val_bytes
;
721 mutex_lock(&map
->lock
);
723 if (regmap_volatile_range(map
, reg
, val_count
) || map
->cache_bypass
||
724 map
->cache_type
== REGCACHE_NONE
) {
725 /* Physical block read if there's no cache involved */
726 ret
= _regmap_raw_read(map
, reg
, val
, val_len
);
729 /* Otherwise go word by word for the cache; should be low
730 * cost as we expect to hit the cache.
732 for (i
= 0; i
< val_count
; i
++) {
733 ret
= _regmap_read(map
, reg
+ i
, &v
);
737 map
->format
.format_val(val
+ (i
* val_bytes
), v
);
742 mutex_unlock(&map
->lock
);
746 EXPORT_SYMBOL_GPL(regmap_raw_read
);
749 * regmap_bulk_read(): Read multiple registers from the device
751 * @map: Register map to write to
752 * @reg: First register to be read from
753 * @val: Pointer to store read value, in native register size for device
754 * @val_count: Number of registers to read
756 * A value of zero will be returned on success, a negative errno will
757 * be returned in error cases.
759 int regmap_bulk_read(struct regmap
*map
, unsigned int reg
, void *val
,
763 size_t val_bytes
= map
->format
.val_bytes
;
764 bool vol
= regmap_volatile_range(map
, reg
, val_count
);
766 if (!map
->format
.parse_val
)
769 if (vol
|| map
->cache_type
== REGCACHE_NONE
) {
770 ret
= regmap_raw_read(map
, reg
, val
, val_bytes
* val_count
);
774 for (i
= 0; i
< val_count
* val_bytes
; i
+= val_bytes
)
775 map
->format
.parse_val(val
+ i
);
777 for (i
= 0; i
< val_count
; i
++) {
779 ret
= regmap_read(map
, reg
+ i
, &ival
);
782 memcpy(val
+ (i
* val_bytes
), &ival
, val_bytes
);
788 EXPORT_SYMBOL_GPL(regmap_bulk_read
);
790 static int _regmap_update_bits(struct regmap
*map
, unsigned int reg
,
791 unsigned int mask
, unsigned int val
,
795 unsigned int tmp
, orig
;
797 mutex_lock(&map
->lock
);
799 ret
= _regmap_read(map
, reg
, &orig
);
807 ret
= _regmap_write(map
, reg
, tmp
);
814 mutex_unlock(&map
->lock
);
820 * regmap_update_bits: Perform a read/modify/write cycle on the register map
822 * @map: Register map to update
823 * @reg: Register to update
824 * @mask: Bitmask to change
825 * @val: New value for bitmask
827 * Returns zero for success, a negative number on error.
829 int regmap_update_bits(struct regmap
*map
, unsigned int reg
,
830 unsigned int mask
, unsigned int val
)
833 return _regmap_update_bits(map
, reg
, mask
, val
, &change
);
835 EXPORT_SYMBOL_GPL(regmap_update_bits
);
838 * regmap_update_bits_check: Perform a read/modify/write cycle on the
839 * register map and report if updated
841 * @map: Register map to update
842 * @reg: Register to update
843 * @mask: Bitmask to change
844 * @val: New value for bitmask
845 * @change: Boolean indicating if a write was done
847 * Returns zero for success, a negative number on error.
849 int regmap_update_bits_check(struct regmap
*map
, unsigned int reg
,
850 unsigned int mask
, unsigned int val
,
853 return _regmap_update_bits(map
, reg
, mask
, val
, change
);
855 EXPORT_SYMBOL_GPL(regmap_update_bits_check
);
858 * regmap_register_patch: Register and apply register updates to be applied
859 * on device initialistion
861 * @map: Register map to apply updates to.
862 * @regs: Values to update.
863 * @num_regs: Number of entries in regs.
865 * Register a set of register updates to be applied to the device
866 * whenever the device registers are synchronised with the cache and
867 * apply them immediately. Typically this is used to apply
868 * corrections to be applied to the device defaults on startup, such
869 * as the updates some vendors provide to undocumented registers.
871 int regmap_register_patch(struct regmap
*map
, const struct reg_default
*regs
,
877 /* If needed the implementation can be extended to support this */
881 mutex_lock(&map
->lock
);
883 bypass
= map
->cache_bypass
;
885 map
->cache_bypass
= true;
887 /* Write out first; it's useful to apply even if we fail later. */
888 for (i
= 0; i
< num_regs
; i
++) {
889 ret
= _regmap_write(map
, regs
[i
].reg
, regs
[i
].def
);
891 dev_err(map
->dev
, "Failed to write %x = %x: %d\n",
892 regs
[i
].reg
, regs
[i
].def
, ret
);
897 map
->patch
= kcalloc(num_regs
, sizeof(struct reg_default
), GFP_KERNEL
);
898 if (map
->patch
!= NULL
) {
899 memcpy(map
->patch
, regs
,
900 num_regs
* sizeof(struct reg_default
));
901 map
->patch_regs
= num_regs
;
907 map
->cache_bypass
= bypass
;
909 mutex_unlock(&map
->lock
);
913 EXPORT_SYMBOL_GPL(regmap_register_patch
);
916 * regmap_get_val_bytes(): Report the size of a register value
918 * Report the size of a register value, mainly intended to for use by
919 * generic infrastructure built on top of regmap.
921 int regmap_get_val_bytes(struct regmap
*map
)
923 if (map
->format
.format_write
)
926 return map
->format
.val_bytes
;
928 EXPORT_SYMBOL_GPL(regmap_get_val_bytes
);
930 static int __init
regmap_initcall(void)
932 regmap_debugfs_initcall();
936 postcore_initcall(regmap_initcall
);