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>
18 #include <linux/rbtree.h>
20 #define CREATE_TRACE_POINTS
21 #include <trace/events/regmap.h>
26 * Sometimes for failures during very early init the trace
27 * infrastructure isn't available early enough to be used. For this
28 * sort of problem defining LOG_DEVICE will add printks for basic
29 * register I/O on a specific device.
33 static int _regmap_update_bits(struct regmap
*map
, unsigned int reg
,
34 unsigned int mask
, unsigned int val
,
37 bool regmap_writeable(struct regmap
*map
, unsigned int reg
)
39 if (map
->max_register
&& reg
> map
->max_register
)
42 if (map
->writeable_reg
)
43 return map
->writeable_reg(map
->dev
, reg
);
48 bool regmap_readable(struct regmap
*map
, unsigned int reg
)
50 if (map
->max_register
&& reg
> map
->max_register
)
53 if (map
->format
.format_write
)
56 if (map
->readable_reg
)
57 return map
->readable_reg(map
->dev
, reg
);
62 bool regmap_volatile(struct regmap
*map
, unsigned int reg
)
64 if (!regmap_readable(map
, reg
))
67 if (map
->volatile_reg
)
68 return map
->volatile_reg(map
->dev
, reg
);
73 bool regmap_precious(struct regmap
*map
, unsigned int reg
)
75 if (!regmap_readable(map
, reg
))
78 if (map
->precious_reg
)
79 return map
->precious_reg(map
->dev
, reg
);
84 static bool regmap_volatile_range(struct regmap
*map
, unsigned int reg
,
89 for (i
= 0; i
< num
; i
++)
90 if (!regmap_volatile(map
, reg
+ i
))
96 static void regmap_format_2_6_write(struct regmap
*map
,
97 unsigned int reg
, unsigned int val
)
99 u8
*out
= map
->work_buf
;
101 *out
= (reg
<< 6) | val
;
104 static void regmap_format_4_12_write(struct regmap
*map
,
105 unsigned int reg
, unsigned int val
)
107 __be16
*out
= map
->work_buf
;
108 *out
= cpu_to_be16((reg
<< 12) | val
);
111 static void regmap_format_7_9_write(struct regmap
*map
,
112 unsigned int reg
, unsigned int val
)
114 __be16
*out
= map
->work_buf
;
115 *out
= cpu_to_be16((reg
<< 9) | val
);
118 static void regmap_format_10_14_write(struct regmap
*map
,
119 unsigned int reg
, unsigned int val
)
121 u8
*out
= map
->work_buf
;
124 out
[1] = (val
>> 8) | (reg
<< 6);
128 static void regmap_format_8(void *buf
, unsigned int val
, unsigned int shift
)
135 static void regmap_format_16_be(void *buf
, unsigned int val
, unsigned int shift
)
139 b
[0] = cpu_to_be16(val
<< shift
);
142 static void regmap_format_16_native(void *buf
, unsigned int val
,
145 *(u16
*)buf
= val
<< shift
;
148 static void regmap_format_24(void *buf
, unsigned int val
, unsigned int shift
)
159 static void regmap_format_32_be(void *buf
, unsigned int val
, unsigned int shift
)
163 b
[0] = cpu_to_be32(val
<< shift
);
166 static void regmap_format_32_native(void *buf
, unsigned int val
,
169 *(u32
*)buf
= val
<< shift
;
172 static unsigned int regmap_parse_8(void *buf
)
179 static unsigned int regmap_parse_16_be(void *buf
)
183 b
[0] = be16_to_cpu(b
[0]);
188 static unsigned int regmap_parse_16_native(void *buf
)
193 static unsigned int regmap_parse_24(void *buf
)
196 unsigned int ret
= b
[2];
197 ret
|= ((unsigned int)b
[1]) << 8;
198 ret
|= ((unsigned int)b
[0]) << 16;
203 static unsigned int regmap_parse_32_be(void *buf
)
207 b
[0] = be32_to_cpu(b
[0]);
212 static unsigned int regmap_parse_32_native(void *buf
)
217 static void regmap_lock_mutex(struct regmap
*map
)
219 mutex_lock(&map
->mutex
);
222 static void regmap_unlock_mutex(struct regmap
*map
)
224 mutex_unlock(&map
->mutex
);
227 static void regmap_lock_spinlock(struct regmap
*map
)
229 spin_lock(&map
->spinlock
);
232 static void regmap_unlock_spinlock(struct regmap
*map
)
234 spin_unlock(&map
->spinlock
);
237 static void dev_get_regmap_release(struct device
*dev
, void *res
)
240 * We don't actually have anything to do here; the goal here
241 * is not to manage the regmap but to provide a simple way to
242 * get the regmap back given a struct device.
246 static bool _regmap_range_add(struct regmap
*map
,
247 struct regmap_range_node
*data
)
249 struct rb_root
*root
= &map
->range_tree
;
250 struct rb_node
**new = &(root
->rb_node
), *parent
= NULL
;
253 struct regmap_range_node
*this =
254 container_of(*new, struct regmap_range_node
, node
);
257 if (data
->range_max
< this->range_min
)
258 new = &((*new)->rb_left
);
259 else if (data
->range_min
> this->range_max
)
260 new = &((*new)->rb_right
);
265 rb_link_node(&data
->node
, parent
, new);
266 rb_insert_color(&data
->node
, root
);
271 static struct regmap_range_node
*_regmap_range_lookup(struct regmap
*map
,
274 struct rb_node
*node
= map
->range_tree
.rb_node
;
277 struct regmap_range_node
*this =
278 container_of(node
, struct regmap_range_node
, node
);
280 if (reg
< this->range_min
)
281 node
= node
->rb_left
;
282 else if (reg
> this->range_max
)
283 node
= node
->rb_right
;
291 static void regmap_range_exit(struct regmap
*map
)
293 struct rb_node
*next
;
294 struct regmap_range_node
*range_node
;
296 next
= rb_first(&map
->range_tree
);
298 range_node
= rb_entry(next
, struct regmap_range_node
, node
);
299 next
= rb_next(&range_node
->node
);
300 rb_erase(&range_node
->node
, &map
->range_tree
);
304 kfree(map
->selector_work_buf
);
308 * regmap_init(): Initialise register map
310 * @dev: Device that will be interacted with
311 * @bus: Bus-specific callbacks to use with device
312 * @bus_context: Data passed to bus-specific callbacks
313 * @config: Configuration for register map
315 * The return value will be an ERR_PTR() on error or a valid pointer to
316 * a struct regmap. This function should generally not be called
317 * directly, it should be called by bus-specific init functions.
319 struct regmap
*regmap_init(struct device
*dev
,
320 const struct regmap_bus
*bus
,
322 const struct regmap_config
*config
)
324 struct regmap
*map
, **m
;
326 enum regmap_endian reg_endian
, val_endian
;
332 map
= kzalloc(sizeof(*map
), GFP_KERNEL
);
339 spin_lock_init(&map
->spinlock
);
340 map
->lock
= regmap_lock_spinlock
;
341 map
->unlock
= regmap_unlock_spinlock
;
343 mutex_init(&map
->mutex
);
344 map
->lock
= regmap_lock_mutex
;
345 map
->unlock
= regmap_unlock_mutex
;
347 map
->format
.reg_bytes
= DIV_ROUND_UP(config
->reg_bits
, 8);
348 map
->format
.pad_bytes
= config
->pad_bits
/ 8;
349 map
->format
.val_bytes
= DIV_ROUND_UP(config
->val_bits
, 8);
350 map
->format
.buf_size
= DIV_ROUND_UP(config
->reg_bits
+
351 config
->val_bits
+ config
->pad_bits
, 8);
352 map
->reg_shift
= config
->pad_bits
% 8;
353 if (config
->reg_stride
)
354 map
->reg_stride
= config
->reg_stride
;
357 map
->use_single_rw
= config
->use_single_rw
;
360 map
->bus_context
= bus_context
;
361 map
->max_register
= config
->max_register
;
362 map
->writeable_reg
= config
->writeable_reg
;
363 map
->readable_reg
= config
->readable_reg
;
364 map
->volatile_reg
= config
->volatile_reg
;
365 map
->precious_reg
= config
->precious_reg
;
366 map
->cache_type
= config
->cache_type
;
367 map
->name
= config
->name
;
369 if (config
->read_flag_mask
|| config
->write_flag_mask
) {
370 map
->read_flag_mask
= config
->read_flag_mask
;
371 map
->write_flag_mask
= config
->write_flag_mask
;
373 map
->read_flag_mask
= bus
->read_flag_mask
;
376 reg_endian
= config
->reg_format_endian
;
377 if (reg_endian
== REGMAP_ENDIAN_DEFAULT
)
378 reg_endian
= bus
->reg_format_endian_default
;
379 if (reg_endian
== REGMAP_ENDIAN_DEFAULT
)
380 reg_endian
= REGMAP_ENDIAN_BIG
;
382 val_endian
= config
->val_format_endian
;
383 if (val_endian
== REGMAP_ENDIAN_DEFAULT
)
384 val_endian
= bus
->val_format_endian_default
;
385 if (val_endian
== REGMAP_ENDIAN_DEFAULT
)
386 val_endian
= REGMAP_ENDIAN_BIG
;
388 switch (config
->reg_bits
+ map
->reg_shift
) {
390 switch (config
->val_bits
) {
392 map
->format
.format_write
= regmap_format_2_6_write
;
400 switch (config
->val_bits
) {
402 map
->format
.format_write
= regmap_format_4_12_write
;
410 switch (config
->val_bits
) {
412 map
->format
.format_write
= regmap_format_7_9_write
;
420 switch (config
->val_bits
) {
422 map
->format
.format_write
= regmap_format_10_14_write
;
430 map
->format
.format_reg
= regmap_format_8
;
434 switch (reg_endian
) {
435 case REGMAP_ENDIAN_BIG
:
436 map
->format
.format_reg
= regmap_format_16_be
;
438 case REGMAP_ENDIAN_NATIVE
:
439 map
->format
.format_reg
= regmap_format_16_native
;
447 switch (reg_endian
) {
448 case REGMAP_ENDIAN_BIG
:
449 map
->format
.format_reg
= regmap_format_32_be
;
451 case REGMAP_ENDIAN_NATIVE
:
452 map
->format
.format_reg
= regmap_format_32_native
;
463 switch (config
->val_bits
) {
465 map
->format
.format_val
= regmap_format_8
;
466 map
->format
.parse_val
= regmap_parse_8
;
469 switch (val_endian
) {
470 case REGMAP_ENDIAN_BIG
:
471 map
->format
.format_val
= regmap_format_16_be
;
472 map
->format
.parse_val
= regmap_parse_16_be
;
474 case REGMAP_ENDIAN_NATIVE
:
475 map
->format
.format_val
= regmap_format_16_native
;
476 map
->format
.parse_val
= regmap_parse_16_native
;
483 if (val_endian
!= REGMAP_ENDIAN_BIG
)
485 map
->format
.format_val
= regmap_format_24
;
486 map
->format
.parse_val
= regmap_parse_24
;
489 switch (val_endian
) {
490 case REGMAP_ENDIAN_BIG
:
491 map
->format
.format_val
= regmap_format_32_be
;
492 map
->format
.parse_val
= regmap_parse_32_be
;
494 case REGMAP_ENDIAN_NATIVE
:
495 map
->format
.format_val
= regmap_format_32_native
;
496 map
->format
.parse_val
= regmap_parse_32_native
;
504 if (map
->format
.format_write
) {
505 if ((reg_endian
!= REGMAP_ENDIAN_BIG
) ||
506 (val_endian
!= REGMAP_ENDIAN_BIG
))
508 map
->use_single_rw
= true;
511 if (!map
->format
.format_write
&&
512 !(map
->format
.format_reg
&& map
->format
.format_val
))
515 map
->work_buf
= kzalloc(map
->format
.buf_size
, GFP_KERNEL
);
516 if (map
->work_buf
== NULL
) {
521 map
->range_tree
= RB_ROOT
;
522 for (i
= 0; i
< config
->n_ranges
; i
++) {
523 const struct regmap_range_cfg
*range_cfg
= &config
->ranges
[i
];
524 struct regmap_range_node
*new;
527 if (range_cfg
->range_max
< range_cfg
->range_min
||
528 range_cfg
->range_max
> map
->max_register
||
529 range_cfg
->selector_reg
> map
->max_register
||
530 range_cfg
->window_len
== 0)
533 /* Make sure, that this register range has no selector
534 or data window within its boundary */
535 for (j
= 0; j
< config
->n_ranges
; j
++) {
536 unsigned sel_reg
= config
->ranges
[j
].selector_reg
;
537 unsigned win_min
= config
->ranges
[j
].window_start
;
538 unsigned win_max
= win_min
+
539 config
->ranges
[j
].window_len
- 1;
541 if (range_cfg
->range_min
<= sel_reg
&&
542 sel_reg
<= range_cfg
->range_max
) {
546 if (!(win_max
< range_cfg
->range_min
||
547 win_min
> range_cfg
->range_max
)) {
552 new = kzalloc(sizeof(*new), GFP_KERNEL
);
558 new->range_min
= range_cfg
->range_min
;
559 new->range_max
= range_cfg
->range_max
;
560 new->selector_reg
= range_cfg
->selector_reg
;
561 new->selector_mask
= range_cfg
->selector_mask
;
562 new->selector_shift
= range_cfg
->selector_shift
;
563 new->window_start
= range_cfg
->window_start
;
564 new->window_len
= range_cfg
->window_len
;
566 if (_regmap_range_add(map
, new) == false) {
571 if (map
->selector_work_buf
== NULL
) {
572 map
->selector_work_buf
=
573 kzalloc(map
->format
.buf_size
, GFP_KERNEL
);
574 if (map
->selector_work_buf
== NULL
) {
581 ret
= regcache_init(map
, config
);
585 regmap_debugfs_init(map
, config
->name
);
587 /* Add a devres resource for dev_get_regmap() */
588 m
= devres_alloc(dev_get_regmap_release
, sizeof(*m
), GFP_KERNEL
);
599 regmap_debugfs_exit(map
);
602 regmap_range_exit(map
);
603 kfree(map
->work_buf
);
609 EXPORT_SYMBOL_GPL(regmap_init
);
611 static void devm_regmap_release(struct device
*dev
, void *res
)
613 regmap_exit(*(struct regmap
**)res
);
617 * devm_regmap_init(): Initialise managed register map
619 * @dev: Device that will be interacted with
620 * @bus: Bus-specific callbacks to use with device
621 * @bus_context: Data passed to bus-specific callbacks
622 * @config: Configuration for register map
624 * The return value will be an ERR_PTR() on error or a valid pointer
625 * to a struct regmap. This function should generally not be called
626 * directly, it should be called by bus-specific init functions. The
627 * map will be automatically freed by the device management code.
629 struct regmap
*devm_regmap_init(struct device
*dev
,
630 const struct regmap_bus
*bus
,
632 const struct regmap_config
*config
)
634 struct regmap
**ptr
, *regmap
;
636 ptr
= devres_alloc(devm_regmap_release
, sizeof(*ptr
), GFP_KERNEL
);
638 return ERR_PTR(-ENOMEM
);
640 regmap
= regmap_init(dev
, bus
, bus_context
, config
);
641 if (!IS_ERR(regmap
)) {
643 devres_add(dev
, ptr
);
650 EXPORT_SYMBOL_GPL(devm_regmap_init
);
653 * regmap_reinit_cache(): Reinitialise the current register cache
655 * @map: Register map to operate on.
656 * @config: New configuration. Only the cache data will be used.
658 * Discard any existing register cache for the map and initialize a
659 * new cache. This can be used to restore the cache to defaults or to
660 * update the cache configuration to reflect runtime discovery of the
663 int regmap_reinit_cache(struct regmap
*map
, const struct regmap_config
*config
)
670 regmap_debugfs_exit(map
);
672 map
->max_register
= config
->max_register
;
673 map
->writeable_reg
= config
->writeable_reg
;
674 map
->readable_reg
= config
->readable_reg
;
675 map
->volatile_reg
= config
->volatile_reg
;
676 map
->precious_reg
= config
->precious_reg
;
677 map
->cache_type
= config
->cache_type
;
679 regmap_debugfs_init(map
, config
->name
);
681 map
->cache_bypass
= false;
682 map
->cache_only
= false;
684 ret
= regcache_init(map
, config
);
690 EXPORT_SYMBOL_GPL(regmap_reinit_cache
);
693 * regmap_exit(): Free a previously allocated register map
695 void regmap_exit(struct regmap
*map
)
698 regmap_debugfs_exit(map
);
699 regmap_range_exit(map
);
700 if (map
->bus
->free_context
)
701 map
->bus
->free_context(map
->bus_context
);
702 kfree(map
->work_buf
);
705 EXPORT_SYMBOL_GPL(regmap_exit
);
707 static int dev_get_regmap_match(struct device
*dev
, void *res
, void *data
)
709 struct regmap
**r
= res
;
715 /* If the user didn't specify a name match any */
717 return (*r
)->name
== data
;
723 * dev_get_regmap(): Obtain the regmap (if any) for a device
725 * @dev: Device to retrieve the map for
726 * @name: Optional name for the register map, usually NULL.
728 * Returns the regmap for the device if one is present, or NULL. If
729 * name is specified then it must match the name specified when
730 * registering the device, if it is NULL then the first regmap found
731 * will be used. Devices with multiple register maps are very rare,
732 * generic code should normally not need to specify a name.
734 struct regmap
*dev_get_regmap(struct device
*dev
, const char *name
)
736 struct regmap
**r
= devres_find(dev
, dev_get_regmap_release
,
737 dev_get_regmap_match
, (void *)name
);
743 EXPORT_SYMBOL_GPL(dev_get_regmap
);
745 static int _regmap_select_page(struct regmap
*map
, unsigned int *reg
,
746 unsigned int val_num
)
748 struct regmap_range_node
*range
;
750 unsigned int win_offset
;
751 unsigned int win_page
;
755 range
= _regmap_range_lookup(map
, *reg
);
757 win_offset
= (*reg
- range
->range_min
) % range
->window_len
;
758 win_page
= (*reg
- range
->range_min
) / range
->window_len
;
761 /* Bulk write shouldn't cross range boundary */
762 if (*reg
+ val_num
- 1 > range
->range_max
)
765 /* ... or single page boundary */
766 if (val_num
> range
->window_len
- win_offset
)
770 /* It is possible to have selector register inside data window.
771 In that case, selector register is located on every page and
772 it needs no page switching, when accessed alone. */
774 range
->window_start
+ win_offset
!= range
->selector_reg
) {
775 /* Use separate work_buf during page switching */
776 orig_work_buf
= map
->work_buf
;
777 map
->work_buf
= map
->selector_work_buf
;
779 ret
= _regmap_update_bits(map
, range
->selector_reg
,
780 range
->selector_mask
,
781 win_page
<< range
->selector_shift
,
784 map
->work_buf
= orig_work_buf
;
790 *reg
= range
->window_start
+ win_offset
;
796 static int _regmap_raw_write(struct regmap
*map
, unsigned int reg
,
797 const void *val
, size_t val_len
)
799 u8
*u8
= map
->work_buf
;
805 /* Check for unwritable registers before we start */
806 if (map
->writeable_reg
)
807 for (i
= 0; i
< val_len
/ map
->format
.val_bytes
; i
++)
808 if (!map
->writeable_reg(map
->dev
,
809 reg
+ (i
* map
->reg_stride
)))
812 if (!map
->cache_bypass
&& map
->format
.parse_val
) {
814 int val_bytes
= map
->format
.val_bytes
;
815 for (i
= 0; i
< val_len
/ val_bytes
; i
++) {
816 memcpy(map
->work_buf
, val
+ (i
* val_bytes
), val_bytes
);
817 ival
= map
->format
.parse_val(map
->work_buf
);
818 ret
= regcache_write(map
, reg
+ (i
* map
->reg_stride
),
822 "Error in caching of register: %u ret: %d\n",
827 if (map
->cache_only
) {
828 map
->cache_dirty
= true;
833 ret
= _regmap_select_page(map
, ®
, val_len
/ map
->format
.val_bytes
);
837 map
->format
.format_reg(map
->work_buf
, reg
, map
->reg_shift
);
839 u8
[0] |= map
->write_flag_mask
;
841 trace_regmap_hw_write_start(map
->dev
, reg
,
842 val_len
/ map
->format
.val_bytes
);
844 /* If we're doing a single register write we can probably just
845 * send the work_buf directly, otherwise try to do a gather
848 if (val
== (map
->work_buf
+ map
->format
.pad_bytes
+
849 map
->format
.reg_bytes
))
850 ret
= map
->bus
->write(map
->bus_context
, map
->work_buf
,
851 map
->format
.reg_bytes
+
852 map
->format
.pad_bytes
+
854 else if (map
->bus
->gather_write
)
855 ret
= map
->bus
->gather_write(map
->bus_context
, map
->work_buf
,
856 map
->format
.reg_bytes
+
857 map
->format
.pad_bytes
,
860 /* If that didn't work fall back on linearising by hand. */
861 if (ret
== -ENOTSUPP
) {
862 len
= map
->format
.reg_bytes
+ map
->format
.pad_bytes
+ val_len
;
863 buf
= kzalloc(len
, GFP_KERNEL
);
867 memcpy(buf
, map
->work_buf
, map
->format
.reg_bytes
);
868 memcpy(buf
+ map
->format
.reg_bytes
+ map
->format
.pad_bytes
,
870 ret
= map
->bus
->write(map
->bus_context
, buf
, len
);
875 trace_regmap_hw_write_done(map
->dev
, reg
,
876 val_len
/ map
->format
.val_bytes
);
881 int _regmap_write(struct regmap
*map
, unsigned int reg
,
885 BUG_ON(!map
->format
.format_write
&& !map
->format
.format_val
);
887 if (!map
->cache_bypass
&& map
->format
.format_write
) {
888 ret
= regcache_write(map
, reg
, val
);
891 if (map
->cache_only
) {
892 map
->cache_dirty
= true;
898 if (strcmp(dev_name(map
->dev
), LOG_DEVICE
) == 0)
899 dev_info(map
->dev
, "%x <= %x\n", reg
, val
);
902 trace_regmap_reg_write(map
->dev
, reg
, val
);
904 if (map
->format
.format_write
) {
905 ret
= _regmap_select_page(map
, ®
, 1);
909 map
->format
.format_write(map
, reg
, val
);
911 trace_regmap_hw_write_start(map
->dev
, reg
, 1);
913 ret
= map
->bus
->write(map
->bus_context
, map
->work_buf
,
914 map
->format
.buf_size
);
916 trace_regmap_hw_write_done(map
->dev
, reg
, 1);
920 map
->format
.format_val(map
->work_buf
+ map
->format
.reg_bytes
921 + map
->format
.pad_bytes
, val
, 0);
922 return _regmap_raw_write(map
, reg
,
924 map
->format
.reg_bytes
+
925 map
->format
.pad_bytes
,
926 map
->format
.val_bytes
);
931 * regmap_write(): Write a value to a single register
933 * @map: Register map to write to
934 * @reg: Register to write to
935 * @val: Value to be written
937 * A value of zero will be returned on success, a negative errno will
938 * be returned in error cases.
940 int regmap_write(struct regmap
*map
, unsigned int reg
, unsigned int val
)
944 if (reg
% map
->reg_stride
)
949 ret
= _regmap_write(map
, reg
, val
);
955 EXPORT_SYMBOL_GPL(regmap_write
);
958 * regmap_raw_write(): Write raw values to one or more registers
960 * @map: Register map to write to
961 * @reg: Initial register to write to
962 * @val: Block of data to be written, laid out for direct transmission to the
964 * @val_len: Length of data pointed to by val.
966 * This function is intended to be used for things like firmware
967 * download where a large block of data needs to be transferred to the
968 * device. No formatting will be done on the data provided.
970 * A value of zero will be returned on success, a negative errno will
971 * be returned in error cases.
973 int regmap_raw_write(struct regmap
*map
, unsigned int reg
,
974 const void *val
, size_t val_len
)
978 if (val_len
% map
->format
.val_bytes
)
980 if (reg
% map
->reg_stride
)
985 ret
= _regmap_raw_write(map
, reg
, val
, val_len
);
991 EXPORT_SYMBOL_GPL(regmap_raw_write
);
994 * regmap_bulk_write(): Write multiple registers to the device
996 * @map: Register map to write to
997 * @reg: First register to be write from
998 * @val: Block of data to be written, in native register size for device
999 * @val_count: Number of registers to write
1001 * This function is intended to be used for writing a large block of
1002 * data to be device either in single transfer or multiple transfer.
1004 * A value of zero will be returned on success, a negative errno will
1005 * be returned in error cases.
1007 int regmap_bulk_write(struct regmap
*map
, unsigned int reg
, const void *val
,
1011 size_t val_bytes
= map
->format
.val_bytes
;
1014 if (!map
->format
.parse_val
)
1016 if (reg
% map
->reg_stride
)
1021 /* No formatting is require if val_byte is 1 */
1022 if (val_bytes
== 1) {
1025 wval
= kmemdup(val
, val_count
* val_bytes
, GFP_KERNEL
);
1028 dev_err(map
->dev
, "Error in memory allocation\n");
1031 for (i
= 0; i
< val_count
* val_bytes
; i
+= val_bytes
)
1032 map
->format
.parse_val(wval
+ i
);
1035 * Some devices does not support bulk write, for
1036 * them we have a series of single write operations.
1038 if (map
->use_single_rw
) {
1039 for (i
= 0; i
< val_count
; i
++) {
1040 ret
= regmap_raw_write(map
,
1041 reg
+ (i
* map
->reg_stride
),
1042 val
+ (i
* val_bytes
),
1048 ret
= _regmap_raw_write(map
, reg
, wval
, val_bytes
* val_count
);
1058 EXPORT_SYMBOL_GPL(regmap_bulk_write
);
1060 static int _regmap_raw_read(struct regmap
*map
, unsigned int reg
, void *val
,
1061 unsigned int val_len
)
1063 u8
*u8
= map
->work_buf
;
1066 ret
= _regmap_select_page(map
, ®
, val_len
/ map
->format
.val_bytes
);
1070 map
->format
.format_reg(map
->work_buf
, reg
, map
->reg_shift
);
1073 * Some buses or devices flag reads by setting the high bits in the
1074 * register addresss; since it's always the high bits for all
1075 * current formats we can do this here rather than in
1076 * formatting. This may break if we get interesting formats.
1078 u8
[0] |= map
->read_flag_mask
;
1080 trace_regmap_hw_read_start(map
->dev
, reg
,
1081 val_len
/ map
->format
.val_bytes
);
1083 ret
= map
->bus
->read(map
->bus_context
, map
->work_buf
,
1084 map
->format
.reg_bytes
+ map
->format
.pad_bytes
,
1087 trace_regmap_hw_read_done(map
->dev
, reg
,
1088 val_len
/ map
->format
.val_bytes
);
1093 static int _regmap_read(struct regmap
*map
, unsigned int reg
,
1098 if (!map
->cache_bypass
) {
1099 ret
= regcache_read(map
, reg
, val
);
1104 if (!map
->format
.parse_val
)
1107 if (map
->cache_only
)
1110 ret
= _regmap_raw_read(map
, reg
, map
->work_buf
, map
->format
.val_bytes
);
1112 *val
= map
->format
.parse_val(map
->work_buf
);
1115 if (strcmp(dev_name(map
->dev
), LOG_DEVICE
) == 0)
1116 dev_info(map
->dev
, "%x => %x\n", reg
, *val
);
1119 trace_regmap_reg_read(map
->dev
, reg
, *val
);
1122 if (ret
== 0 && !map
->cache_bypass
)
1123 regcache_write(map
, reg
, *val
);
1129 * regmap_read(): Read a value from a single register
1131 * @map: Register map to write to
1132 * @reg: Register to be read from
1133 * @val: Pointer to store read value
1135 * A value of zero will be returned on success, a negative errno will
1136 * be returned in error cases.
1138 int regmap_read(struct regmap
*map
, unsigned int reg
, unsigned int *val
)
1142 if (reg
% map
->reg_stride
)
1147 ret
= _regmap_read(map
, reg
, val
);
1153 EXPORT_SYMBOL_GPL(regmap_read
);
1156 * regmap_raw_read(): Read raw data from the device
1158 * @map: Register map to write to
1159 * @reg: First register to be read from
1160 * @val: Pointer to store read value
1161 * @val_len: Size of data to read
1163 * A value of zero will be returned on success, a negative errno will
1164 * be returned in error cases.
1166 int regmap_raw_read(struct regmap
*map
, unsigned int reg
, void *val
,
1169 size_t val_bytes
= map
->format
.val_bytes
;
1170 size_t val_count
= val_len
/ val_bytes
;
1174 if (val_len
% map
->format
.val_bytes
)
1176 if (reg
% map
->reg_stride
)
1181 if (regmap_volatile_range(map
, reg
, val_count
) || map
->cache_bypass
||
1182 map
->cache_type
== REGCACHE_NONE
) {
1183 /* Physical block read if there's no cache involved */
1184 ret
= _regmap_raw_read(map
, reg
, val
, val_len
);
1187 /* Otherwise go word by word for the cache; should be low
1188 * cost as we expect to hit the cache.
1190 for (i
= 0; i
< val_count
; i
++) {
1191 ret
= _regmap_read(map
, reg
+ (i
* map
->reg_stride
),
1196 map
->format
.format_val(val
+ (i
* val_bytes
), v
, 0);
1205 EXPORT_SYMBOL_GPL(regmap_raw_read
);
1208 * regmap_bulk_read(): Read multiple registers from the device
1210 * @map: Register map to write to
1211 * @reg: First register to be read from
1212 * @val: Pointer to store read value, in native register size for device
1213 * @val_count: Number of registers to read
1215 * A value of zero will be returned on success, a negative errno will
1216 * be returned in error cases.
1218 int regmap_bulk_read(struct regmap
*map
, unsigned int reg
, void *val
,
1222 size_t val_bytes
= map
->format
.val_bytes
;
1223 bool vol
= regmap_volatile_range(map
, reg
, val_count
);
1225 if (!map
->format
.parse_val
)
1227 if (reg
% map
->reg_stride
)
1230 if (vol
|| map
->cache_type
== REGCACHE_NONE
) {
1232 * Some devices does not support bulk read, for
1233 * them we have a series of single read operations.
1235 if (map
->use_single_rw
) {
1236 for (i
= 0; i
< val_count
; i
++) {
1237 ret
= regmap_raw_read(map
,
1238 reg
+ (i
* map
->reg_stride
),
1239 val
+ (i
* val_bytes
),
1245 ret
= regmap_raw_read(map
, reg
, val
,
1246 val_bytes
* val_count
);
1251 for (i
= 0; i
< val_count
* val_bytes
; i
+= val_bytes
)
1252 map
->format
.parse_val(val
+ i
);
1254 for (i
= 0; i
< val_count
; i
++) {
1256 ret
= regmap_read(map
, reg
+ (i
* map
->reg_stride
),
1260 memcpy(val
+ (i
* val_bytes
), &ival
, val_bytes
);
1266 EXPORT_SYMBOL_GPL(regmap_bulk_read
);
1268 static int _regmap_update_bits(struct regmap
*map
, unsigned int reg
,
1269 unsigned int mask
, unsigned int val
,
1273 unsigned int tmp
, orig
;
1275 ret
= _regmap_read(map
, reg
, &orig
);
1283 ret
= _regmap_write(map
, reg
, tmp
);
1293 * regmap_update_bits: Perform a read/modify/write cycle on the register map
1295 * @map: Register map to update
1296 * @reg: Register to update
1297 * @mask: Bitmask to change
1298 * @val: New value for bitmask
1300 * Returns zero for success, a negative number on error.
1302 int regmap_update_bits(struct regmap
*map
, unsigned int reg
,
1303 unsigned int mask
, unsigned int val
)
1309 ret
= _regmap_update_bits(map
, reg
, mask
, val
, &change
);
1314 EXPORT_SYMBOL_GPL(regmap_update_bits
);
1317 * regmap_update_bits_check: Perform a read/modify/write cycle on the
1318 * register map and report if updated
1320 * @map: Register map to update
1321 * @reg: Register to update
1322 * @mask: Bitmask to change
1323 * @val: New value for bitmask
1324 * @change: Boolean indicating if a write was done
1326 * Returns zero for success, a negative number on error.
1328 int regmap_update_bits_check(struct regmap
*map
, unsigned int reg
,
1329 unsigned int mask
, unsigned int val
,
1335 ret
= _regmap_update_bits(map
, reg
, mask
, val
, change
);
1339 EXPORT_SYMBOL_GPL(regmap_update_bits_check
);
1342 * regmap_register_patch: Register and apply register updates to be applied
1343 * on device initialistion
1345 * @map: Register map to apply updates to.
1346 * @regs: Values to update.
1347 * @num_regs: Number of entries in regs.
1349 * Register a set of register updates to be applied to the device
1350 * whenever the device registers are synchronised with the cache and
1351 * apply them immediately. Typically this is used to apply
1352 * corrections to be applied to the device defaults on startup, such
1353 * as the updates some vendors provide to undocumented registers.
1355 int regmap_register_patch(struct regmap
*map
, const struct reg_default
*regs
,
1361 /* If needed the implementation can be extended to support this */
1367 bypass
= map
->cache_bypass
;
1369 map
->cache_bypass
= true;
1371 /* Write out first; it's useful to apply even if we fail later. */
1372 for (i
= 0; i
< num_regs
; i
++) {
1373 ret
= _regmap_write(map
, regs
[i
].reg
, regs
[i
].def
);
1375 dev_err(map
->dev
, "Failed to write %x = %x: %d\n",
1376 regs
[i
].reg
, regs
[i
].def
, ret
);
1381 map
->patch
= kcalloc(num_regs
, sizeof(struct reg_default
), GFP_KERNEL
);
1382 if (map
->patch
!= NULL
) {
1383 memcpy(map
->patch
, regs
,
1384 num_regs
* sizeof(struct reg_default
));
1385 map
->patch_regs
= num_regs
;
1391 map
->cache_bypass
= bypass
;
1397 EXPORT_SYMBOL_GPL(regmap_register_patch
);
1400 * regmap_get_val_bytes(): Report the size of a register value
1402 * Report the size of a register value, mainly intended to for use by
1403 * generic infrastructure built on top of regmap.
1405 int regmap_get_val_bytes(struct regmap
*map
)
1407 if (map
->format
.format_write
)
1410 return map
->format
.val_bytes
;
1412 EXPORT_SYMBOL_GPL(regmap_get_val_bytes
);
1414 static int __init
regmap_initcall(void)
1416 regmap_debugfs_initcall();
1420 postcore_initcall(regmap_initcall
);