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_24(void *buf
, unsigned int val
)
138 static void regmap_format_32(void *buf
, unsigned int val
)
142 b
[0] = cpu_to_be32(val
);
145 static unsigned int regmap_parse_8(void *buf
)
152 static unsigned int regmap_parse_16(void *buf
)
156 b
[0] = be16_to_cpu(b
[0]);
161 static unsigned int regmap_parse_24(void *buf
)
164 unsigned int ret
= b
[2];
165 ret
|= ((unsigned int)b
[1]) << 8;
166 ret
|= ((unsigned int)b
[0]) << 16;
171 static unsigned int regmap_parse_32(void *buf
)
175 b
[0] = be32_to_cpu(b
[0]);
181 * regmap_init(): Initialise register map
183 * @dev: Device that will be interacted with
184 * @bus: Bus-specific callbacks to use with device
185 * @config: Configuration for register map
187 * The return value will be an ERR_PTR() on error or a valid pointer to
188 * a struct regmap. This function should generally not be called
189 * directly, it should be called by bus-specific init functions.
191 struct regmap
*regmap_init(struct device
*dev
,
192 const struct regmap_bus
*bus
,
193 const struct regmap_config
*config
)
201 map
= kzalloc(sizeof(*map
), GFP_KERNEL
);
207 mutex_init(&map
->lock
);
208 map
->format
.buf_size
= (config
->reg_bits
+ config
->val_bits
) / 8;
209 map
->format
.reg_bytes
= DIV_ROUND_UP(config
->reg_bits
, 8);
210 map
->format
.pad_bytes
= config
->pad_bits
/ 8;
211 map
->format
.val_bytes
= DIV_ROUND_UP(config
->val_bits
, 8);
212 map
->format
.buf_size
+= map
->format
.pad_bytes
;
215 map
->max_register
= config
->max_register
;
216 map
->writeable_reg
= config
->writeable_reg
;
217 map
->readable_reg
= config
->readable_reg
;
218 map
->volatile_reg
= config
->volatile_reg
;
219 map
->precious_reg
= config
->precious_reg
;
220 map
->cache_type
= config
->cache_type
;
222 if (config
->read_flag_mask
|| config
->write_flag_mask
) {
223 map
->read_flag_mask
= config
->read_flag_mask
;
224 map
->write_flag_mask
= config
->write_flag_mask
;
226 map
->read_flag_mask
= bus
->read_flag_mask
;
229 switch (config
->reg_bits
) {
231 switch (config
->val_bits
) {
233 map
->format
.format_write
= regmap_format_2_6_write
;
241 switch (config
->val_bits
) {
243 map
->format
.format_write
= regmap_format_4_12_write
;
251 switch (config
->val_bits
) {
253 map
->format
.format_write
= regmap_format_7_9_write
;
261 switch (config
->val_bits
) {
263 map
->format
.format_write
= regmap_format_10_14_write
;
271 map
->format
.format_reg
= regmap_format_8
;
275 map
->format
.format_reg
= regmap_format_16
;
279 map
->format
.format_reg
= regmap_format_32
;
286 switch (config
->val_bits
) {
288 map
->format
.format_val
= regmap_format_8
;
289 map
->format
.parse_val
= regmap_parse_8
;
292 map
->format
.format_val
= regmap_format_16
;
293 map
->format
.parse_val
= regmap_parse_16
;
296 map
->format
.format_val
= regmap_format_24
;
297 map
->format
.parse_val
= regmap_parse_24
;
300 map
->format
.format_val
= regmap_format_32
;
301 map
->format
.parse_val
= regmap_parse_32
;
305 if (!map
->format
.format_write
&&
306 !(map
->format
.format_reg
&& map
->format
.format_val
))
309 map
->work_buf
= kzalloc(map
->format
.buf_size
, GFP_KERNEL
);
310 if (map
->work_buf
== NULL
) {
315 regmap_debugfs_init(map
);
317 ret
= regcache_init(map
, config
);
319 goto err_free_workbuf
;
324 kfree(map
->work_buf
);
330 EXPORT_SYMBOL_GPL(regmap_init
);
332 static void devm_regmap_release(struct device
*dev
, void *res
)
334 regmap_exit(*(struct regmap
**)res
);
338 * devm_regmap_init(): Initialise managed register map
340 * @dev: Device that will be interacted with
341 * @bus: Bus-specific callbacks to use with device
342 * @config: Configuration for register map
344 * The return value will be an ERR_PTR() on error or a valid pointer
345 * to a struct regmap. This function should generally not be called
346 * directly, it should be called by bus-specific init functions. The
347 * map will be automatically freed by the device management code.
349 struct regmap
*devm_regmap_init(struct device
*dev
,
350 const struct regmap_bus
*bus
,
351 const struct regmap_config
*config
)
353 struct regmap
**ptr
, *regmap
;
355 ptr
= devres_alloc(devm_regmap_release
, sizeof(*ptr
), GFP_KERNEL
);
357 return ERR_PTR(-ENOMEM
);
359 regmap
= regmap_init(dev
, bus
, config
);
360 if (!IS_ERR(regmap
)) {
362 devres_add(dev
, ptr
);
369 EXPORT_SYMBOL_GPL(devm_regmap_init
);
372 * regmap_reinit_cache(): Reinitialise the current register cache
374 * @map: Register map to operate on.
375 * @config: New configuration. Only the cache data will be used.
377 * Discard any existing register cache for the map and initialize a
378 * new cache. This can be used to restore the cache to defaults or to
379 * update the cache configuration to reflect runtime discovery of the
382 int regmap_reinit_cache(struct regmap
*map
, const struct regmap_config
*config
)
386 mutex_lock(&map
->lock
);
389 regmap_debugfs_exit(map
);
391 map
->max_register
= config
->max_register
;
392 map
->writeable_reg
= config
->writeable_reg
;
393 map
->readable_reg
= config
->readable_reg
;
394 map
->volatile_reg
= config
->volatile_reg
;
395 map
->precious_reg
= config
->precious_reg
;
396 map
->cache_type
= config
->cache_type
;
398 regmap_debugfs_init(map
);
400 map
->cache_bypass
= false;
401 map
->cache_only
= false;
403 ret
= regcache_init(map
, config
);
405 mutex_unlock(&map
->lock
);
411 * regmap_exit(): Free a previously allocated register map
413 void regmap_exit(struct regmap
*map
)
416 regmap_debugfs_exit(map
);
417 kfree(map
->work_buf
);
420 EXPORT_SYMBOL_GPL(regmap_exit
);
422 static int _regmap_raw_write(struct regmap
*map
, unsigned int reg
,
423 const void *val
, size_t val_len
)
425 u8
*u8
= map
->work_buf
;
431 /* Check for unwritable registers before we start */
432 if (map
->writeable_reg
)
433 for (i
= 0; i
< val_len
/ map
->format
.val_bytes
; i
++)
434 if (!map
->writeable_reg(map
->dev
, reg
+ i
))
437 if (!map
->cache_bypass
&& map
->format
.parse_val
) {
439 int val_bytes
= map
->format
.val_bytes
;
440 for (i
= 0; i
< val_len
/ val_bytes
; i
++) {
441 memcpy(map
->work_buf
, val
+ (i
* val_bytes
), val_bytes
);
442 ival
= map
->format
.parse_val(map
->work_buf
);
443 ret
= regcache_write(map
, reg
+ i
, ival
);
446 "Error in caching of register: %u ret: %d\n",
451 if (map
->cache_only
) {
452 map
->cache_dirty
= true;
457 map
->format
.format_reg(map
->work_buf
, reg
);
459 u8
[0] |= map
->write_flag_mask
;
461 trace_regmap_hw_write_start(map
->dev
, reg
,
462 val_len
/ map
->format
.val_bytes
);
464 /* If we're doing a single register write we can probably just
465 * send the work_buf directly, otherwise try to do a gather
468 if (val
== (map
->work_buf
+ map
->format
.pad_bytes
+
469 map
->format
.reg_bytes
))
470 ret
= map
->bus
->write(map
->dev
, map
->work_buf
,
471 map
->format
.reg_bytes
+
472 map
->format
.pad_bytes
+
474 else if (map
->bus
->gather_write
)
475 ret
= map
->bus
->gather_write(map
->dev
, map
->work_buf
,
476 map
->format
.reg_bytes
+
477 map
->format
.pad_bytes
,
480 /* If that didn't work fall back on linearising by hand. */
481 if (ret
== -ENOTSUPP
) {
482 len
= map
->format
.reg_bytes
+ map
->format
.pad_bytes
+ val_len
;
483 buf
= kzalloc(len
, GFP_KERNEL
);
487 memcpy(buf
, map
->work_buf
, map
->format
.reg_bytes
);
488 memcpy(buf
+ map
->format
.reg_bytes
+ map
->format
.pad_bytes
,
490 ret
= map
->bus
->write(map
->dev
, buf
, len
);
495 trace_regmap_hw_write_done(map
->dev
, reg
,
496 val_len
/ map
->format
.val_bytes
);
501 int _regmap_write(struct regmap
*map
, unsigned int reg
,
505 BUG_ON(!map
->format
.format_write
&& !map
->format
.format_val
);
507 if (!map
->cache_bypass
&& map
->format
.format_write
) {
508 ret
= regcache_write(map
, reg
, val
);
511 if (map
->cache_only
) {
512 map
->cache_dirty
= true;
517 trace_regmap_reg_write(map
->dev
, reg
, val
);
519 if (map
->format
.format_write
) {
520 map
->format
.format_write(map
, reg
, val
);
522 trace_regmap_hw_write_start(map
->dev
, reg
, 1);
524 ret
= map
->bus
->write(map
->dev
, map
->work_buf
,
525 map
->format
.buf_size
);
527 trace_regmap_hw_write_done(map
->dev
, reg
, 1);
531 map
->format
.format_val(map
->work_buf
+ map
->format
.reg_bytes
532 + map
->format
.pad_bytes
, val
);
533 return _regmap_raw_write(map
, reg
,
535 map
->format
.reg_bytes
+
536 map
->format
.pad_bytes
,
537 map
->format
.val_bytes
);
542 * regmap_write(): Write a value to a single register
544 * @map: Register map to write to
545 * @reg: Register to write to
546 * @val: Value to be written
548 * A value of zero will be returned on success, a negative errno will
549 * be returned in error cases.
551 int regmap_write(struct regmap
*map
, unsigned int reg
, unsigned int val
)
555 mutex_lock(&map
->lock
);
557 ret
= _regmap_write(map
, reg
, val
);
559 mutex_unlock(&map
->lock
);
563 EXPORT_SYMBOL_GPL(regmap_write
);
566 * regmap_raw_write(): Write raw values to one or more registers
568 * @map: Register map to write to
569 * @reg: Initial register to write to
570 * @val: Block of data to be written, laid out for direct transmission to the
572 * @val_len: Length of data pointed to by val.
574 * This function is intended to be used for things like firmware
575 * download where a large block of data needs to be transferred to the
576 * device. No formatting will be done on the data provided.
578 * A value of zero will be returned on success, a negative errno will
579 * be returned in error cases.
581 int regmap_raw_write(struct regmap
*map
, unsigned int reg
,
582 const void *val
, size_t val_len
)
586 mutex_lock(&map
->lock
);
588 ret
= _regmap_raw_write(map
, reg
, val
, val_len
);
590 mutex_unlock(&map
->lock
);
594 EXPORT_SYMBOL_GPL(regmap_raw_write
);
597 * regmap_bulk_write(): Write multiple registers to the device
599 * @map: Register map to write to
600 * @reg: First register to be write from
601 * @val: Block of data to be written, in native register size for device
602 * @val_count: Number of registers to write
604 * This function is intended to be used for writing a large block of
605 * data to be device either in single transfer or multiple transfer.
607 * A value of zero will be returned on success, a negative errno will
608 * be returned in error cases.
610 int regmap_bulk_write(struct regmap
*map
, unsigned int reg
, const void *val
,
614 size_t val_bytes
= map
->format
.val_bytes
;
617 if (!map
->format
.parse_val
)
620 mutex_lock(&map
->lock
);
622 /* No formatting is require if val_byte is 1 */
623 if (val_bytes
== 1) {
626 wval
= kmemdup(val
, val_count
* val_bytes
, GFP_KERNEL
);
629 dev_err(map
->dev
, "Error in memory allocation\n");
632 for (i
= 0; i
< val_count
* val_bytes
; i
+= val_bytes
)
633 map
->format
.parse_val(wval
+ i
);
635 ret
= _regmap_raw_write(map
, reg
, wval
, val_bytes
* val_count
);
641 mutex_unlock(&map
->lock
);
644 EXPORT_SYMBOL_GPL(regmap_bulk_write
);
646 static int _regmap_raw_read(struct regmap
*map
, unsigned int reg
, void *val
,
647 unsigned int val_len
)
649 u8
*u8
= map
->work_buf
;
652 map
->format
.format_reg(map
->work_buf
, reg
);
655 * Some buses or devices flag reads by setting the high bits in the
656 * register addresss; since it's always the high bits for all
657 * current formats we can do this here rather than in
658 * formatting. This may break if we get interesting formats.
660 u8
[0] |= map
->read_flag_mask
;
662 trace_regmap_hw_read_start(map
->dev
, reg
,
663 val_len
/ map
->format
.val_bytes
);
665 ret
= map
->bus
->read(map
->dev
, map
->work_buf
,
666 map
->format
.reg_bytes
+ map
->format
.pad_bytes
,
669 trace_regmap_hw_read_done(map
->dev
, reg
,
670 val_len
/ map
->format
.val_bytes
);
675 static int _regmap_read(struct regmap
*map
, unsigned int reg
,
680 if (!map
->cache_bypass
) {
681 ret
= regcache_read(map
, reg
, val
);
686 if (!map
->format
.parse_val
)
692 ret
= _regmap_raw_read(map
, reg
, map
->work_buf
, map
->format
.val_bytes
);
694 *val
= map
->format
.parse_val(map
->work_buf
);
695 trace_regmap_reg_read(map
->dev
, reg
, *val
);
702 * regmap_read(): Read a value from a single register
704 * @map: Register map to write to
705 * @reg: Register to be read from
706 * @val: Pointer to store read value
708 * A value of zero will be returned on success, a negative errno will
709 * be returned in error cases.
711 int regmap_read(struct regmap
*map
, unsigned int reg
, unsigned int *val
)
715 mutex_lock(&map
->lock
);
717 ret
= _regmap_read(map
, reg
, val
);
719 mutex_unlock(&map
->lock
);
723 EXPORT_SYMBOL_GPL(regmap_read
);
726 * regmap_raw_read(): Read raw data from the device
728 * @map: Register map to write to
729 * @reg: First register to be read from
730 * @val: Pointer to store read value
731 * @val_len: Size of data to read
733 * A value of zero will be returned on success, a negative errno will
734 * be returned in error cases.
736 int regmap_raw_read(struct regmap
*map
, unsigned int reg
, void *val
,
739 size_t val_bytes
= map
->format
.val_bytes
;
740 size_t val_count
= val_len
/ val_bytes
;
744 mutex_lock(&map
->lock
);
746 if (regmap_volatile_range(map
, reg
, val_count
) || map
->cache_bypass
||
747 map
->cache_type
== REGCACHE_NONE
) {
748 /* Physical block read if there's no cache involved */
749 ret
= _regmap_raw_read(map
, reg
, val
, val_len
);
752 /* Otherwise go word by word for the cache; should be low
753 * cost as we expect to hit the cache.
755 for (i
= 0; i
< val_count
; i
++) {
756 ret
= _regmap_read(map
, reg
+ i
, &v
);
760 map
->format
.format_val(val
+ (i
* val_bytes
), v
);
765 mutex_unlock(&map
->lock
);
769 EXPORT_SYMBOL_GPL(regmap_raw_read
);
772 * regmap_bulk_read(): Read multiple registers from the device
774 * @map: Register map to write to
775 * @reg: First register to be read from
776 * @val: Pointer to store read value, in native register size for device
777 * @val_count: Number of registers to read
779 * A value of zero will be returned on success, a negative errno will
780 * be returned in error cases.
782 int regmap_bulk_read(struct regmap
*map
, unsigned int reg
, void *val
,
786 size_t val_bytes
= map
->format
.val_bytes
;
787 bool vol
= regmap_volatile_range(map
, reg
, val_count
);
789 if (!map
->format
.parse_val
)
792 if (vol
|| map
->cache_type
== REGCACHE_NONE
) {
793 ret
= regmap_raw_read(map
, reg
, val
, val_bytes
* val_count
);
797 for (i
= 0; i
< val_count
* val_bytes
; i
+= val_bytes
)
798 map
->format
.parse_val(val
+ i
);
800 for (i
= 0; i
< val_count
; i
++) {
801 ret
= regmap_read(map
, reg
+ i
, val
+ (i
* val_bytes
));
809 EXPORT_SYMBOL_GPL(regmap_bulk_read
);
811 static int _regmap_update_bits(struct regmap
*map
, unsigned int reg
,
812 unsigned int mask
, unsigned int val
,
816 unsigned int tmp
, orig
;
818 mutex_lock(&map
->lock
);
820 ret
= _regmap_read(map
, reg
, &orig
);
828 ret
= _regmap_write(map
, reg
, tmp
);
835 mutex_unlock(&map
->lock
);
841 * regmap_update_bits: Perform a read/modify/write cycle on the register map
843 * @map: Register map to update
844 * @reg: Register to update
845 * @mask: Bitmask to change
846 * @val: New value for bitmask
848 * Returns zero for success, a negative number on error.
850 int regmap_update_bits(struct regmap
*map
, unsigned int reg
,
851 unsigned int mask
, unsigned int val
)
854 return _regmap_update_bits(map
, reg
, mask
, val
, &change
);
856 EXPORT_SYMBOL_GPL(regmap_update_bits
);
859 * regmap_update_bits_check: Perform a read/modify/write cycle on the
860 * register map and report if updated
862 * @map: Register map to update
863 * @reg: Register to update
864 * @mask: Bitmask to change
865 * @val: New value for bitmask
866 * @change: Boolean indicating if a write was done
868 * Returns zero for success, a negative number on error.
870 int regmap_update_bits_check(struct regmap
*map
, unsigned int reg
,
871 unsigned int mask
, unsigned int val
,
874 return _regmap_update_bits(map
, reg
, mask
, val
, change
);
876 EXPORT_SYMBOL_GPL(regmap_update_bits_check
);
879 * regmap_register_patch: Register and apply register updates to be applied
880 * on device initialistion
882 * @map: Register map to apply updates to.
883 * @regs: Values to update.
884 * @num_regs: Number of entries in regs.
886 * Register a set of register updates to be applied to the device
887 * whenever the device registers are synchronised with the cache and
888 * apply them immediately. Typically this is used to apply
889 * corrections to be applied to the device defaults on startup, such
890 * as the updates some vendors provide to undocumented registers.
892 int regmap_register_patch(struct regmap
*map
, const struct reg_default
*regs
,
898 /* If needed the implementation can be extended to support this */
902 mutex_lock(&map
->lock
);
904 bypass
= map
->cache_bypass
;
906 map
->cache_bypass
= true;
908 /* Write out first; it's useful to apply even if we fail later. */
909 for (i
= 0; i
< num_regs
; i
++) {
910 ret
= _regmap_write(map
, regs
[i
].reg
, regs
[i
].def
);
912 dev_err(map
->dev
, "Failed to write %x = %x: %d\n",
913 regs
[i
].reg
, regs
[i
].def
, ret
);
918 map
->patch
= kcalloc(num_regs
, sizeof(struct reg_default
), GFP_KERNEL
);
919 if (map
->patch
!= NULL
) {
920 memcpy(map
->patch
, regs
,
921 num_regs
* sizeof(struct reg_default
));
922 map
->patch_regs
= num_regs
;
928 map
->cache_bypass
= bypass
;
930 mutex_unlock(&map
->lock
);
934 EXPORT_SYMBOL_GPL(regmap_register_patch
);
937 * regmap_get_val_bytes(): Report the size of a register value
939 * Report the size of a register value, mainly intended to for use by
940 * generic infrastructure built on top of regmap.
942 int regmap_get_val_bytes(struct regmap
*map
)
944 if (map
->format
.format_write
)
947 return map
->format
.val_bytes
;
949 EXPORT_SYMBOL_GPL(regmap_get_val_bytes
);
951 static int __init
regmap_initcall(void)
953 regmap_debugfs_initcall();
957 postcore_initcall(regmap_initcall
);