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>
19 #include <linux/sched.h>
21 #define CREATE_TRACE_POINTS
22 #include <trace/events/regmap.h>
27 * Sometimes for failures during very early init the trace
28 * infrastructure isn't available early enough to be used. For this
29 * sort of problem defining LOG_DEVICE will add printks for basic
30 * register I/O on a specific device.
34 static int _regmap_update_bits(struct regmap
*map
, unsigned int reg
,
35 unsigned int mask
, unsigned int val
,
38 static int _regmap_bus_read(void *context
, unsigned int reg
,
40 static int _regmap_bus_formatted_write(void *context
, unsigned int reg
,
42 static int _regmap_bus_raw_write(void *context
, unsigned int reg
,
45 static void async_cleanup(struct work_struct
*work
)
47 struct regmap_async
*async
= container_of(work
, struct regmap_async
,
50 kfree(async
->work_buf
);
54 bool regmap_reg_in_ranges(unsigned int reg
,
55 const struct regmap_range
*ranges
,
58 const struct regmap_range
*r
;
61 for (i
= 0, r
= ranges
; i
< nranges
; i
++, r
++)
62 if (regmap_reg_in_range(reg
, r
))
66 EXPORT_SYMBOL_GPL(regmap_reg_in_ranges
);
68 bool regmap_check_range_table(struct regmap
*map
, unsigned int reg
,
69 const struct regmap_access_table
*table
)
71 /* Check "no ranges" first */
72 if (regmap_reg_in_ranges(reg
, table
->no_ranges
, table
->n_no_ranges
))
75 /* In case zero "yes ranges" are supplied, any reg is OK */
76 if (!table
->n_yes_ranges
)
79 return regmap_reg_in_ranges(reg
, table
->yes_ranges
,
82 EXPORT_SYMBOL_GPL(regmap_check_range_table
);
84 bool regmap_writeable(struct regmap
*map
, unsigned int reg
)
86 if (map
->max_register
&& reg
> map
->max_register
)
89 if (map
->writeable_reg
)
90 return map
->writeable_reg(map
->dev
, reg
);
93 return regmap_check_range_table(map
, reg
, map
->wr_table
);
98 bool regmap_readable(struct regmap
*map
, unsigned int reg
)
100 if (map
->max_register
&& reg
> map
->max_register
)
103 if (map
->format
.format_write
)
106 if (map
->readable_reg
)
107 return map
->readable_reg(map
->dev
, reg
);
110 return regmap_check_range_table(map
, reg
, map
->rd_table
);
115 bool regmap_volatile(struct regmap
*map
, unsigned int reg
)
117 if (!regmap_readable(map
, reg
))
120 if (map
->volatile_reg
)
121 return map
->volatile_reg(map
->dev
, reg
);
123 if (map
->volatile_table
)
124 return regmap_check_range_table(map
, reg
, map
->volatile_table
);
132 bool regmap_precious(struct regmap
*map
, unsigned int reg
)
134 if (!regmap_readable(map
, reg
))
137 if (map
->precious_reg
)
138 return map
->precious_reg(map
->dev
, reg
);
140 if (map
->precious_table
)
141 return regmap_check_range_table(map
, reg
, map
->precious_table
);
146 static bool regmap_volatile_range(struct regmap
*map
, unsigned int reg
,
151 for (i
= 0; i
< num
; i
++)
152 if (!regmap_volatile(map
, reg
+ i
))
158 static void regmap_format_2_6_write(struct regmap
*map
,
159 unsigned int reg
, unsigned int val
)
161 u8
*out
= map
->work_buf
;
163 *out
= (reg
<< 6) | val
;
166 static void regmap_format_4_12_write(struct regmap
*map
,
167 unsigned int reg
, unsigned int val
)
169 __be16
*out
= map
->work_buf
;
170 *out
= cpu_to_be16((reg
<< 12) | val
);
173 static void regmap_format_7_9_write(struct regmap
*map
,
174 unsigned int reg
, unsigned int val
)
176 __be16
*out
= map
->work_buf
;
177 *out
= cpu_to_be16((reg
<< 9) | val
);
180 static void regmap_format_10_14_write(struct regmap
*map
,
181 unsigned int reg
, unsigned int val
)
183 u8
*out
= map
->work_buf
;
186 out
[1] = (val
>> 8) | (reg
<< 6);
190 static void regmap_format_8(void *buf
, unsigned int val
, unsigned int shift
)
197 static void regmap_format_16_be(void *buf
, unsigned int val
, unsigned int shift
)
201 b
[0] = cpu_to_be16(val
<< shift
);
204 static void regmap_format_16_native(void *buf
, unsigned int val
,
207 *(u16
*)buf
= val
<< shift
;
210 static void regmap_format_24(void *buf
, unsigned int val
, unsigned int shift
)
221 static void regmap_format_32_be(void *buf
, unsigned int val
, unsigned int shift
)
225 b
[0] = cpu_to_be32(val
<< shift
);
228 static void regmap_format_32_native(void *buf
, unsigned int val
,
231 *(u32
*)buf
= val
<< shift
;
234 static void regmap_parse_inplace_noop(void *buf
)
238 static unsigned int regmap_parse_8(const void *buf
)
245 static unsigned int regmap_parse_16_be(const void *buf
)
247 const __be16
*b
= buf
;
249 return be16_to_cpu(b
[0]);
252 static void regmap_parse_16_be_inplace(void *buf
)
256 b
[0] = be16_to_cpu(b
[0]);
259 static unsigned int regmap_parse_16_native(const void *buf
)
264 static unsigned int regmap_parse_24(const void *buf
)
267 unsigned int ret
= b
[2];
268 ret
|= ((unsigned int)b
[1]) << 8;
269 ret
|= ((unsigned int)b
[0]) << 16;
274 static unsigned int regmap_parse_32_be(const void *buf
)
276 const __be32
*b
= buf
;
278 return be32_to_cpu(b
[0]);
281 static void regmap_parse_32_be_inplace(void *buf
)
285 b
[0] = be32_to_cpu(b
[0]);
288 static unsigned int regmap_parse_32_native(const void *buf
)
293 static void regmap_lock_mutex(void *__map
)
295 struct regmap
*map
= __map
;
296 mutex_lock(&map
->mutex
);
299 static void regmap_unlock_mutex(void *__map
)
301 struct regmap
*map
= __map
;
302 mutex_unlock(&map
->mutex
);
305 static void regmap_lock_spinlock(void *__map
)
306 __acquires(&map
->spinlock
)
308 struct regmap
*map
= __map
;
311 spin_lock_irqsave(&map
->spinlock
, flags
);
312 map
->spinlock_flags
= flags
;
315 static void regmap_unlock_spinlock(void *__map
)
316 __releases(&map
->spinlock
)
318 struct regmap
*map
= __map
;
319 spin_unlock_irqrestore(&map
->spinlock
, map
->spinlock_flags
);
322 static void dev_get_regmap_release(struct device
*dev
, void *res
)
325 * We don't actually have anything to do here; the goal here
326 * is not to manage the regmap but to provide a simple way to
327 * get the regmap back given a struct device.
331 static bool _regmap_range_add(struct regmap
*map
,
332 struct regmap_range_node
*data
)
334 struct rb_root
*root
= &map
->range_tree
;
335 struct rb_node
**new = &(root
->rb_node
), *parent
= NULL
;
338 struct regmap_range_node
*this =
339 container_of(*new, struct regmap_range_node
, node
);
342 if (data
->range_max
< this->range_min
)
343 new = &((*new)->rb_left
);
344 else if (data
->range_min
> this->range_max
)
345 new = &((*new)->rb_right
);
350 rb_link_node(&data
->node
, parent
, new);
351 rb_insert_color(&data
->node
, root
);
356 static struct regmap_range_node
*_regmap_range_lookup(struct regmap
*map
,
359 struct rb_node
*node
= map
->range_tree
.rb_node
;
362 struct regmap_range_node
*this =
363 container_of(node
, struct regmap_range_node
, node
);
365 if (reg
< this->range_min
)
366 node
= node
->rb_left
;
367 else if (reg
> this->range_max
)
368 node
= node
->rb_right
;
376 static void regmap_range_exit(struct regmap
*map
)
378 struct rb_node
*next
;
379 struct regmap_range_node
*range_node
;
381 next
= rb_first(&map
->range_tree
);
383 range_node
= rb_entry(next
, struct regmap_range_node
, node
);
384 next
= rb_next(&range_node
->node
);
385 rb_erase(&range_node
->node
, &map
->range_tree
);
389 kfree(map
->selector_work_buf
);
393 * regmap_init(): Initialise register map
395 * @dev: Device that will be interacted with
396 * @bus: Bus-specific callbacks to use with device
397 * @bus_context: Data passed to bus-specific callbacks
398 * @config: Configuration for register map
400 * The return value will be an ERR_PTR() on error or a valid pointer to
401 * a struct regmap. This function should generally not be called
402 * directly, it should be called by bus-specific init functions.
404 struct regmap
*regmap_init(struct device
*dev
,
405 const struct regmap_bus
*bus
,
407 const struct regmap_config
*config
)
409 struct regmap
*map
, **m
;
411 enum regmap_endian reg_endian
, val_endian
;
417 map
= kzalloc(sizeof(*map
), GFP_KERNEL
);
423 if (config
->lock
&& config
->unlock
) {
424 map
->lock
= config
->lock
;
425 map
->unlock
= config
->unlock
;
426 map
->lock_arg
= config
->lock_arg
;
428 if ((bus
&& bus
->fast_io
) ||
430 spin_lock_init(&map
->spinlock
);
431 map
->lock
= regmap_lock_spinlock
;
432 map
->unlock
= regmap_unlock_spinlock
;
434 mutex_init(&map
->mutex
);
435 map
->lock
= regmap_lock_mutex
;
436 map
->unlock
= regmap_unlock_mutex
;
440 map
->format
.reg_bytes
= DIV_ROUND_UP(config
->reg_bits
, 8);
441 map
->format
.pad_bytes
= config
->pad_bits
/ 8;
442 map
->format
.val_bytes
= DIV_ROUND_UP(config
->val_bits
, 8);
443 map
->format
.buf_size
= DIV_ROUND_UP(config
->reg_bits
+
444 config
->val_bits
+ config
->pad_bits
, 8);
445 map
->reg_shift
= config
->pad_bits
% 8;
446 if (config
->reg_stride
)
447 map
->reg_stride
= config
->reg_stride
;
450 map
->use_single_rw
= config
->use_single_rw
;
453 map
->bus_context
= bus_context
;
454 map
->max_register
= config
->max_register
;
455 map
->wr_table
= config
->wr_table
;
456 map
->rd_table
= config
->rd_table
;
457 map
->volatile_table
= config
->volatile_table
;
458 map
->precious_table
= config
->precious_table
;
459 map
->writeable_reg
= config
->writeable_reg
;
460 map
->readable_reg
= config
->readable_reg
;
461 map
->volatile_reg
= config
->volatile_reg
;
462 map
->precious_reg
= config
->precious_reg
;
463 map
->cache_type
= config
->cache_type
;
464 map
->name
= config
->name
;
466 spin_lock_init(&map
->async_lock
);
467 INIT_LIST_HEAD(&map
->async_list
);
468 init_waitqueue_head(&map
->async_waitq
);
470 if (config
->read_flag_mask
|| config
->write_flag_mask
) {
471 map
->read_flag_mask
= config
->read_flag_mask
;
472 map
->write_flag_mask
= config
->write_flag_mask
;
474 map
->read_flag_mask
= bus
->read_flag_mask
;
478 map
->reg_read
= config
->reg_read
;
479 map
->reg_write
= config
->reg_write
;
481 map
->defer_caching
= false;
482 goto skip_format_initialization
;
484 map
->reg_read
= _regmap_bus_read
;
487 reg_endian
= config
->reg_format_endian
;
488 if (reg_endian
== REGMAP_ENDIAN_DEFAULT
)
489 reg_endian
= bus
->reg_format_endian_default
;
490 if (reg_endian
== REGMAP_ENDIAN_DEFAULT
)
491 reg_endian
= REGMAP_ENDIAN_BIG
;
493 val_endian
= config
->val_format_endian
;
494 if (val_endian
== REGMAP_ENDIAN_DEFAULT
)
495 val_endian
= bus
->val_format_endian_default
;
496 if (val_endian
== REGMAP_ENDIAN_DEFAULT
)
497 val_endian
= REGMAP_ENDIAN_BIG
;
499 switch (config
->reg_bits
+ map
->reg_shift
) {
501 switch (config
->val_bits
) {
503 map
->format
.format_write
= regmap_format_2_6_write
;
511 switch (config
->val_bits
) {
513 map
->format
.format_write
= regmap_format_4_12_write
;
521 switch (config
->val_bits
) {
523 map
->format
.format_write
= regmap_format_7_9_write
;
531 switch (config
->val_bits
) {
533 map
->format
.format_write
= regmap_format_10_14_write
;
541 map
->format
.format_reg
= regmap_format_8
;
545 switch (reg_endian
) {
546 case REGMAP_ENDIAN_BIG
:
547 map
->format
.format_reg
= regmap_format_16_be
;
549 case REGMAP_ENDIAN_NATIVE
:
550 map
->format
.format_reg
= regmap_format_16_native
;
558 if (reg_endian
!= REGMAP_ENDIAN_BIG
)
560 map
->format
.format_reg
= regmap_format_24
;
564 switch (reg_endian
) {
565 case REGMAP_ENDIAN_BIG
:
566 map
->format
.format_reg
= regmap_format_32_be
;
568 case REGMAP_ENDIAN_NATIVE
:
569 map
->format
.format_reg
= regmap_format_32_native
;
580 if (val_endian
== REGMAP_ENDIAN_NATIVE
)
581 map
->format
.parse_inplace
= regmap_parse_inplace_noop
;
583 switch (config
->val_bits
) {
585 map
->format
.format_val
= regmap_format_8
;
586 map
->format
.parse_val
= regmap_parse_8
;
587 map
->format
.parse_inplace
= regmap_parse_inplace_noop
;
590 switch (val_endian
) {
591 case REGMAP_ENDIAN_BIG
:
592 map
->format
.format_val
= regmap_format_16_be
;
593 map
->format
.parse_val
= regmap_parse_16_be
;
594 map
->format
.parse_inplace
= regmap_parse_16_be_inplace
;
596 case REGMAP_ENDIAN_NATIVE
:
597 map
->format
.format_val
= regmap_format_16_native
;
598 map
->format
.parse_val
= regmap_parse_16_native
;
605 if (val_endian
!= REGMAP_ENDIAN_BIG
)
607 map
->format
.format_val
= regmap_format_24
;
608 map
->format
.parse_val
= regmap_parse_24
;
611 switch (val_endian
) {
612 case REGMAP_ENDIAN_BIG
:
613 map
->format
.format_val
= regmap_format_32_be
;
614 map
->format
.parse_val
= regmap_parse_32_be
;
615 map
->format
.parse_inplace
= regmap_parse_32_be_inplace
;
617 case REGMAP_ENDIAN_NATIVE
:
618 map
->format
.format_val
= regmap_format_32_native
;
619 map
->format
.parse_val
= regmap_parse_32_native
;
627 if (map
->format
.format_write
) {
628 if ((reg_endian
!= REGMAP_ENDIAN_BIG
) ||
629 (val_endian
!= REGMAP_ENDIAN_BIG
))
631 map
->use_single_rw
= true;
634 if (!map
->format
.format_write
&&
635 !(map
->format
.format_reg
&& map
->format
.format_val
))
638 map
->work_buf
= kzalloc(map
->format
.buf_size
, GFP_KERNEL
);
639 if (map
->work_buf
== NULL
) {
644 if (map
->format
.format_write
) {
645 map
->defer_caching
= false;
646 map
->reg_write
= _regmap_bus_formatted_write
;
647 } else if (map
->format
.format_val
) {
648 map
->defer_caching
= true;
649 map
->reg_write
= _regmap_bus_raw_write
;
652 skip_format_initialization
:
654 map
->range_tree
= RB_ROOT
;
655 for (i
= 0; i
< config
->num_ranges
; i
++) {
656 const struct regmap_range_cfg
*range_cfg
= &config
->ranges
[i
];
657 struct regmap_range_node
*new;
660 if (range_cfg
->range_max
< range_cfg
->range_min
) {
661 dev_err(map
->dev
, "Invalid range %d: %d < %d\n", i
,
662 range_cfg
->range_max
, range_cfg
->range_min
);
666 if (range_cfg
->range_max
> map
->max_register
) {
667 dev_err(map
->dev
, "Invalid range %d: %d > %d\n", i
,
668 range_cfg
->range_max
, map
->max_register
);
672 if (range_cfg
->selector_reg
> map
->max_register
) {
674 "Invalid range %d: selector out of map\n", i
);
678 if (range_cfg
->window_len
== 0) {
679 dev_err(map
->dev
, "Invalid range %d: window_len 0\n",
684 /* Make sure, that this register range has no selector
685 or data window within its boundary */
686 for (j
= 0; j
< config
->num_ranges
; j
++) {
687 unsigned sel_reg
= config
->ranges
[j
].selector_reg
;
688 unsigned win_min
= config
->ranges
[j
].window_start
;
689 unsigned win_max
= win_min
+
690 config
->ranges
[j
].window_len
- 1;
692 /* Allow data window inside its own virtual range */
696 if (range_cfg
->range_min
<= sel_reg
&&
697 sel_reg
<= range_cfg
->range_max
) {
699 "Range %d: selector for %d in window\n",
704 if (!(win_max
< range_cfg
->range_min
||
705 win_min
> range_cfg
->range_max
)) {
707 "Range %d: window for %d in window\n",
713 new = kzalloc(sizeof(*new), GFP_KERNEL
);
720 new->name
= range_cfg
->name
;
721 new->range_min
= range_cfg
->range_min
;
722 new->range_max
= range_cfg
->range_max
;
723 new->selector_reg
= range_cfg
->selector_reg
;
724 new->selector_mask
= range_cfg
->selector_mask
;
725 new->selector_shift
= range_cfg
->selector_shift
;
726 new->window_start
= range_cfg
->window_start
;
727 new->window_len
= range_cfg
->window_len
;
729 if (_regmap_range_add(map
, new) == false) {
730 dev_err(map
->dev
, "Failed to add range %d\n", i
);
735 if (map
->selector_work_buf
== NULL
) {
736 map
->selector_work_buf
=
737 kzalloc(map
->format
.buf_size
, GFP_KERNEL
);
738 if (map
->selector_work_buf
== NULL
) {
745 regmap_debugfs_init(map
, config
->name
);
747 ret
= regcache_init(map
, config
);
751 /* Add a devres resource for dev_get_regmap() */
752 m
= devres_alloc(dev_get_regmap_release
, sizeof(*m
), GFP_KERNEL
);
763 regmap_debugfs_exit(map
);
766 regmap_range_exit(map
);
767 kfree(map
->work_buf
);
773 EXPORT_SYMBOL_GPL(regmap_init
);
775 static void devm_regmap_release(struct device
*dev
, void *res
)
777 regmap_exit(*(struct regmap
**)res
);
781 * devm_regmap_init(): Initialise managed register map
783 * @dev: Device that will be interacted with
784 * @bus: Bus-specific callbacks to use with device
785 * @bus_context: Data passed to bus-specific callbacks
786 * @config: Configuration for register map
788 * The return value will be an ERR_PTR() on error or a valid pointer
789 * to a struct regmap. This function should generally not be called
790 * directly, it should be called by bus-specific init functions. The
791 * map will be automatically freed by the device management code.
793 struct regmap
*devm_regmap_init(struct device
*dev
,
794 const struct regmap_bus
*bus
,
796 const struct regmap_config
*config
)
798 struct regmap
**ptr
, *regmap
;
800 ptr
= devres_alloc(devm_regmap_release
, sizeof(*ptr
), GFP_KERNEL
);
802 return ERR_PTR(-ENOMEM
);
804 regmap
= regmap_init(dev
, bus
, bus_context
, config
);
805 if (!IS_ERR(regmap
)) {
807 devres_add(dev
, ptr
);
814 EXPORT_SYMBOL_GPL(devm_regmap_init
);
816 static void regmap_field_init(struct regmap_field
*rm_field
,
817 struct regmap
*regmap
, struct reg_field reg_field
)
819 int field_bits
= reg_field
.msb
- reg_field
.lsb
+ 1;
820 rm_field
->regmap
= regmap
;
821 rm_field
->reg
= reg_field
.reg
;
822 rm_field
->shift
= reg_field
.lsb
;
823 rm_field
->mask
= ((BIT(field_bits
) - 1) << reg_field
.lsb
);
827 * devm_regmap_field_alloc(): Allocate and initialise a register field
830 * @dev: Device that will be interacted with
831 * @regmap: regmap bank in which this register field is located.
832 * @reg_field: Register field with in the bank.
834 * The return value will be an ERR_PTR() on error or a valid pointer
835 * to a struct regmap_field. The regmap_field will be automatically freed
836 * by the device management code.
838 struct regmap_field
*devm_regmap_field_alloc(struct device
*dev
,
839 struct regmap
*regmap
, struct reg_field reg_field
)
841 struct regmap_field
*rm_field
= devm_kzalloc(dev
,
842 sizeof(*rm_field
), GFP_KERNEL
);
844 return ERR_PTR(-ENOMEM
);
846 regmap_field_init(rm_field
, regmap
, reg_field
);
851 EXPORT_SYMBOL_GPL(devm_regmap_field_alloc
);
854 * devm_regmap_field_free(): Free register field allocated using
855 * devm_regmap_field_alloc. Usally drivers need not call this function,
856 * as the memory allocated via devm will be freed as per device-driver
859 * @dev: Device that will be interacted with
860 * @field: regmap field which should be freed.
862 void devm_regmap_field_free(struct device
*dev
,
863 struct regmap_field
*field
)
865 devm_kfree(dev
, field
);
867 EXPORT_SYMBOL_GPL(devm_regmap_field_free
);
870 * regmap_field_alloc(): Allocate and initialise a register field
873 * @regmap: regmap bank in which this register field is located.
874 * @reg_field: Register field with in the bank.
876 * The return value will be an ERR_PTR() on error or a valid pointer
877 * to a struct regmap_field. The regmap_field should be freed by the
878 * user once its finished working with it using regmap_field_free().
880 struct regmap_field
*regmap_field_alloc(struct regmap
*regmap
,
881 struct reg_field reg_field
)
883 struct regmap_field
*rm_field
= kzalloc(sizeof(*rm_field
), GFP_KERNEL
);
886 return ERR_PTR(-ENOMEM
);
888 regmap_field_init(rm_field
, regmap
, reg_field
);
892 EXPORT_SYMBOL_GPL(regmap_field_alloc
);
895 * regmap_field_free(): Free register field allocated using regmap_field_alloc
897 * @field: regmap field which should be freed.
899 void regmap_field_free(struct regmap_field
*field
)
903 EXPORT_SYMBOL_GPL(regmap_field_free
);
906 * regmap_reinit_cache(): Reinitialise the current register cache
908 * @map: Register map to operate on.
909 * @config: New configuration. Only the cache data will be used.
911 * Discard any existing register cache for the map and initialize a
912 * new cache. This can be used to restore the cache to defaults or to
913 * update the cache configuration to reflect runtime discovery of the
916 * No explicit locking is done here, the user needs to ensure that
917 * this function will not race with other calls to regmap.
919 int regmap_reinit_cache(struct regmap
*map
, const struct regmap_config
*config
)
922 regmap_debugfs_exit(map
);
924 map
->max_register
= config
->max_register
;
925 map
->writeable_reg
= config
->writeable_reg
;
926 map
->readable_reg
= config
->readable_reg
;
927 map
->volatile_reg
= config
->volatile_reg
;
928 map
->precious_reg
= config
->precious_reg
;
929 map
->cache_type
= config
->cache_type
;
931 regmap_debugfs_init(map
, config
->name
);
933 map
->cache_bypass
= false;
934 map
->cache_only
= false;
936 return regcache_init(map
, config
);
938 EXPORT_SYMBOL_GPL(regmap_reinit_cache
);
941 * regmap_exit(): Free a previously allocated register map
943 void regmap_exit(struct regmap
*map
)
946 regmap_debugfs_exit(map
);
947 regmap_range_exit(map
);
948 if (map
->bus
&& map
->bus
->free_context
)
949 map
->bus
->free_context(map
->bus_context
);
950 kfree(map
->work_buf
);
953 EXPORT_SYMBOL_GPL(regmap_exit
);
955 static int dev_get_regmap_match(struct device
*dev
, void *res
, void *data
)
957 struct regmap
**r
= res
;
963 /* If the user didn't specify a name match any */
965 return (*r
)->name
== data
;
971 * dev_get_regmap(): Obtain the regmap (if any) for a device
973 * @dev: Device to retrieve the map for
974 * @name: Optional name for the register map, usually NULL.
976 * Returns the regmap for the device if one is present, or NULL. If
977 * name is specified then it must match the name specified when
978 * registering the device, if it is NULL then the first regmap found
979 * will be used. Devices with multiple register maps are very rare,
980 * generic code should normally not need to specify a name.
982 struct regmap
*dev_get_regmap(struct device
*dev
, const char *name
)
984 struct regmap
**r
= devres_find(dev
, dev_get_regmap_release
,
985 dev_get_regmap_match
, (void *)name
);
991 EXPORT_SYMBOL_GPL(dev_get_regmap
);
993 static int _regmap_select_page(struct regmap
*map
, unsigned int *reg
,
994 struct regmap_range_node
*range
,
995 unsigned int val_num
)
998 unsigned int win_offset
;
999 unsigned int win_page
;
1003 win_offset
= (*reg
- range
->range_min
) % range
->window_len
;
1004 win_page
= (*reg
- range
->range_min
) / range
->window_len
;
1007 /* Bulk write shouldn't cross range boundary */
1008 if (*reg
+ val_num
- 1 > range
->range_max
)
1011 /* ... or single page boundary */
1012 if (val_num
> range
->window_len
- win_offset
)
1016 /* It is possible to have selector register inside data window.
1017 In that case, selector register is located on every page and
1018 it needs no page switching, when accessed alone. */
1020 range
->window_start
+ win_offset
!= range
->selector_reg
) {
1021 /* Use separate work_buf during page switching */
1022 orig_work_buf
= map
->work_buf
;
1023 map
->work_buf
= map
->selector_work_buf
;
1025 ret
= _regmap_update_bits(map
, range
->selector_reg
,
1026 range
->selector_mask
,
1027 win_page
<< range
->selector_shift
,
1030 map
->work_buf
= orig_work_buf
;
1036 *reg
= range
->window_start
+ win_offset
;
1041 int _regmap_raw_write(struct regmap
*map
, unsigned int reg
,
1042 const void *val
, size_t val_len
, bool async
)
1044 struct regmap_range_node
*range
;
1045 unsigned long flags
;
1046 u8
*u8
= map
->work_buf
;
1047 void *work_val
= map
->work_buf
+ map
->format
.reg_bytes
+
1048 map
->format
.pad_bytes
;
1050 int ret
= -ENOTSUPP
;
1056 /* Check for unwritable registers before we start */
1057 if (map
->writeable_reg
)
1058 for (i
= 0; i
< val_len
/ map
->format
.val_bytes
; i
++)
1059 if (!map
->writeable_reg(map
->dev
,
1060 reg
+ (i
* map
->reg_stride
)))
1063 if (!map
->cache_bypass
&& map
->format
.parse_val
) {
1065 int val_bytes
= map
->format
.val_bytes
;
1066 for (i
= 0; i
< val_len
/ val_bytes
; i
++) {
1067 ival
= map
->format
.parse_val(val
+ (i
* val_bytes
));
1068 ret
= regcache_write(map
, reg
+ (i
* map
->reg_stride
),
1072 "Error in caching of register: %x ret: %d\n",
1077 if (map
->cache_only
) {
1078 map
->cache_dirty
= true;
1083 range
= _regmap_range_lookup(map
, reg
);
1085 int val_num
= val_len
/ map
->format
.val_bytes
;
1086 int win_offset
= (reg
- range
->range_min
) % range
->window_len
;
1087 int win_residue
= range
->window_len
- win_offset
;
1089 /* If the write goes beyond the end of the window split it */
1090 while (val_num
> win_residue
) {
1091 dev_dbg(map
->dev
, "Writing window %d/%zu\n",
1092 win_residue
, val_len
/ map
->format
.val_bytes
);
1093 ret
= _regmap_raw_write(map
, reg
, val
, win_residue
*
1094 map
->format
.val_bytes
, async
);
1099 val_num
-= win_residue
;
1100 val
+= win_residue
* map
->format
.val_bytes
;
1101 val_len
-= win_residue
* map
->format
.val_bytes
;
1103 win_offset
= (reg
- range
->range_min
) %
1105 win_residue
= range
->window_len
- win_offset
;
1108 ret
= _regmap_select_page(map
, ®
, range
, val_num
);
1113 map
->format
.format_reg(map
->work_buf
, reg
, map
->reg_shift
);
1115 u8
[0] |= map
->write_flag_mask
;
1117 if (async
&& map
->bus
->async_write
) {
1118 struct regmap_async
*async
= map
->bus
->async_alloc();
1122 trace_regmap_async_write_start(map
->dev
, reg
, val_len
);
1124 async
->work_buf
= kzalloc(map
->format
.buf_size
,
1125 GFP_KERNEL
| GFP_DMA
);
1126 if (!async
->work_buf
) {
1131 INIT_WORK(&async
->cleanup
, async_cleanup
);
1134 /* If the caller supplied the value we can use it safely. */
1135 memcpy(async
->work_buf
, map
->work_buf
, map
->format
.pad_bytes
+
1136 map
->format
.reg_bytes
+ map
->format
.val_bytes
);
1137 if (val
== work_val
)
1138 val
= async
->work_buf
+ map
->format
.pad_bytes
+
1139 map
->format
.reg_bytes
;
1141 spin_lock_irqsave(&map
->async_lock
, flags
);
1142 list_add_tail(&async
->list
, &map
->async_list
);
1143 spin_unlock_irqrestore(&map
->async_lock
, flags
);
1145 ret
= map
->bus
->async_write(map
->bus_context
, async
->work_buf
,
1146 map
->format
.reg_bytes
+
1147 map
->format
.pad_bytes
,
1148 val
, val_len
, async
);
1151 dev_err(map
->dev
, "Failed to schedule write: %d\n",
1154 spin_lock_irqsave(&map
->async_lock
, flags
);
1155 list_del(&async
->list
);
1156 spin_unlock_irqrestore(&map
->async_lock
, flags
);
1158 kfree(async
->work_buf
);
1165 trace_regmap_hw_write_start(map
->dev
, reg
,
1166 val_len
/ map
->format
.val_bytes
);
1168 /* If we're doing a single register write we can probably just
1169 * send the work_buf directly, otherwise try to do a gather
1172 if (val
== work_val
)
1173 ret
= map
->bus
->write(map
->bus_context
, map
->work_buf
,
1174 map
->format
.reg_bytes
+
1175 map
->format
.pad_bytes
+
1177 else if (map
->bus
->gather_write
)
1178 ret
= map
->bus
->gather_write(map
->bus_context
, map
->work_buf
,
1179 map
->format
.reg_bytes
+
1180 map
->format
.pad_bytes
,
1183 /* If that didn't work fall back on linearising by hand. */
1184 if (ret
== -ENOTSUPP
) {
1185 len
= map
->format
.reg_bytes
+ map
->format
.pad_bytes
+ val_len
;
1186 buf
= kzalloc(len
, GFP_KERNEL
);
1190 memcpy(buf
, map
->work_buf
, map
->format
.reg_bytes
);
1191 memcpy(buf
+ map
->format
.reg_bytes
+ map
->format
.pad_bytes
,
1193 ret
= map
->bus
->write(map
->bus_context
, buf
, len
);
1198 trace_regmap_hw_write_done(map
->dev
, reg
,
1199 val_len
/ map
->format
.val_bytes
);
1205 * regmap_can_raw_write - Test if regmap_raw_write() is supported
1207 * @map: Map to check.
1209 bool regmap_can_raw_write(struct regmap
*map
)
1211 return map
->bus
&& map
->format
.format_val
&& map
->format
.format_reg
;
1213 EXPORT_SYMBOL_GPL(regmap_can_raw_write
);
1215 static int _regmap_bus_formatted_write(void *context
, unsigned int reg
,
1219 struct regmap_range_node
*range
;
1220 struct regmap
*map
= context
;
1222 WARN_ON(!map
->bus
|| !map
->format
.format_write
);
1224 range
= _regmap_range_lookup(map
, reg
);
1226 ret
= _regmap_select_page(map
, ®
, range
, 1);
1231 map
->format
.format_write(map
, reg
, val
);
1233 trace_regmap_hw_write_start(map
->dev
, reg
, 1);
1235 ret
= map
->bus
->write(map
->bus_context
, map
->work_buf
,
1236 map
->format
.buf_size
);
1238 trace_regmap_hw_write_done(map
->dev
, reg
, 1);
1243 static int _regmap_bus_raw_write(void *context
, unsigned int reg
,
1246 struct regmap
*map
= context
;
1248 WARN_ON(!map
->bus
|| !map
->format
.format_val
);
1250 map
->format
.format_val(map
->work_buf
+ map
->format
.reg_bytes
1251 + map
->format
.pad_bytes
, val
, 0);
1252 return _regmap_raw_write(map
, reg
,
1254 map
->format
.reg_bytes
+
1255 map
->format
.pad_bytes
,
1256 map
->format
.val_bytes
, false);
1259 static inline void *_regmap_map_get_context(struct regmap
*map
)
1261 return (map
->bus
) ? map
: map
->bus_context
;
1264 int _regmap_write(struct regmap
*map
, unsigned int reg
,
1268 void *context
= _regmap_map_get_context(map
);
1270 if (!regmap_writeable(map
, reg
))
1273 if (!map
->cache_bypass
&& !map
->defer_caching
) {
1274 ret
= regcache_write(map
, reg
, val
);
1277 if (map
->cache_only
) {
1278 map
->cache_dirty
= true;
1284 if (strcmp(dev_name(map
->dev
), LOG_DEVICE
) == 0)
1285 dev_info(map
->dev
, "%x <= %x\n", reg
, val
);
1288 trace_regmap_reg_write(map
->dev
, reg
, val
);
1290 return map
->reg_write(context
, reg
, val
);
1294 * regmap_write(): Write a value to a single register
1296 * @map: Register map to write to
1297 * @reg: Register to write to
1298 * @val: Value to be written
1300 * A value of zero will be returned on success, a negative errno will
1301 * be returned in error cases.
1303 int regmap_write(struct regmap
*map
, unsigned int reg
, unsigned int val
)
1307 if (reg
% map
->reg_stride
)
1310 map
->lock(map
->lock_arg
);
1312 ret
= _regmap_write(map
, reg
, val
);
1314 map
->unlock(map
->lock_arg
);
1318 EXPORT_SYMBOL_GPL(regmap_write
);
1321 * regmap_raw_write(): Write raw values to one or more registers
1323 * @map: Register map to write to
1324 * @reg: Initial register to write to
1325 * @val: Block of data to be written, laid out for direct transmission to the
1327 * @val_len: Length of data pointed to by val.
1329 * This function is intended to be used for things like firmware
1330 * download where a large block of data needs to be transferred to the
1331 * device. No formatting will be done on the data provided.
1333 * A value of zero will be returned on success, a negative errno will
1334 * be returned in error cases.
1336 int regmap_raw_write(struct regmap
*map
, unsigned int reg
,
1337 const void *val
, size_t val_len
)
1341 if (!regmap_can_raw_write(map
))
1343 if (val_len
% map
->format
.val_bytes
)
1346 map
->lock(map
->lock_arg
);
1348 ret
= _regmap_raw_write(map
, reg
, val
, val_len
, false);
1350 map
->unlock(map
->lock_arg
);
1354 EXPORT_SYMBOL_GPL(regmap_raw_write
);
1357 * regmap_field_write(): Write a value to a single register field
1359 * @field: Register field to write to
1360 * @val: Value to be written
1362 * A value of zero will be returned on success, a negative errno will
1363 * be returned in error cases.
1365 int regmap_field_write(struct regmap_field
*field
, unsigned int val
)
1367 return regmap_update_bits(field
->regmap
, field
->reg
,
1368 field
->mask
, val
<< field
->shift
);
1370 EXPORT_SYMBOL_GPL(regmap_field_write
);
1373 * regmap_bulk_write(): Write multiple registers to the device
1375 * @map: Register map to write to
1376 * @reg: First register to be write from
1377 * @val: Block of data to be written, in native register size for device
1378 * @val_count: Number of registers to write
1380 * This function is intended to be used for writing a large block of
1381 * data to the device either in single transfer or multiple transfer.
1383 * A value of zero will be returned on success, a negative errno will
1384 * be returned in error cases.
1386 int regmap_bulk_write(struct regmap
*map
, unsigned int reg
, const void *val
,
1390 size_t val_bytes
= map
->format
.val_bytes
;
1395 if (!map
->format
.parse_inplace
)
1397 if (reg
% map
->reg_stride
)
1400 map
->lock(map
->lock_arg
);
1402 /* No formatting is require if val_byte is 1 */
1403 if (val_bytes
== 1) {
1406 wval
= kmemdup(val
, val_count
* val_bytes
, GFP_KERNEL
);
1409 dev_err(map
->dev
, "Error in memory allocation\n");
1412 for (i
= 0; i
< val_count
* val_bytes
; i
+= val_bytes
)
1413 map
->format
.parse_inplace(wval
+ i
);
1416 * Some devices does not support bulk write, for
1417 * them we have a series of single write operations.
1419 if (map
->use_single_rw
) {
1420 for (i
= 0; i
< val_count
; i
++) {
1421 ret
= regmap_raw_write(map
,
1422 reg
+ (i
* map
->reg_stride
),
1423 val
+ (i
* val_bytes
),
1429 ret
= _regmap_raw_write(map
, reg
, wval
, val_bytes
* val_count
,
1437 map
->unlock(map
->lock_arg
);
1440 EXPORT_SYMBOL_GPL(regmap_bulk_write
);
1443 * regmap_raw_write_async(): Write raw values to one or more registers
1446 * @map: Register map to write to
1447 * @reg: Initial register to write to
1448 * @val: Block of data to be written, laid out for direct transmission to the
1449 * device. Must be valid until regmap_async_complete() is called.
1450 * @val_len: Length of data pointed to by val.
1452 * This function is intended to be used for things like firmware
1453 * download where a large block of data needs to be transferred to the
1454 * device. No formatting will be done on the data provided.
1456 * If supported by the underlying bus the write will be scheduled
1457 * asynchronously, helping maximise I/O speed on higher speed buses
1458 * like SPI. regmap_async_complete() can be called to ensure that all
1459 * asynchrnous writes have been completed.
1461 * A value of zero will be returned on success, a negative errno will
1462 * be returned in error cases.
1464 int regmap_raw_write_async(struct regmap
*map
, unsigned int reg
,
1465 const void *val
, size_t val_len
)
1469 if (val_len
% map
->format
.val_bytes
)
1471 if (reg
% map
->reg_stride
)
1474 map
->lock(map
->lock_arg
);
1476 ret
= _regmap_raw_write(map
, reg
, val
, val_len
, true);
1478 map
->unlock(map
->lock_arg
);
1482 EXPORT_SYMBOL_GPL(regmap_raw_write_async
);
1484 static int _regmap_raw_read(struct regmap
*map
, unsigned int reg
, void *val
,
1485 unsigned int val_len
)
1487 struct regmap_range_node
*range
;
1488 u8
*u8
= map
->work_buf
;
1493 range
= _regmap_range_lookup(map
, reg
);
1495 ret
= _regmap_select_page(map
, ®
, range
,
1496 val_len
/ map
->format
.val_bytes
);
1501 map
->format
.format_reg(map
->work_buf
, reg
, map
->reg_shift
);
1504 * Some buses or devices flag reads by setting the high bits in the
1505 * register addresss; since it's always the high bits for all
1506 * current formats we can do this here rather than in
1507 * formatting. This may break if we get interesting formats.
1509 u8
[0] |= map
->read_flag_mask
;
1511 trace_regmap_hw_read_start(map
->dev
, reg
,
1512 val_len
/ map
->format
.val_bytes
);
1514 ret
= map
->bus
->read(map
->bus_context
, map
->work_buf
,
1515 map
->format
.reg_bytes
+ map
->format
.pad_bytes
,
1518 trace_regmap_hw_read_done(map
->dev
, reg
,
1519 val_len
/ map
->format
.val_bytes
);
1524 static int _regmap_bus_read(void *context
, unsigned int reg
,
1528 struct regmap
*map
= context
;
1530 if (!map
->format
.parse_val
)
1533 ret
= _regmap_raw_read(map
, reg
, map
->work_buf
, map
->format
.val_bytes
);
1535 *val
= map
->format
.parse_val(map
->work_buf
);
1540 static int _regmap_read(struct regmap
*map
, unsigned int reg
,
1544 void *context
= _regmap_map_get_context(map
);
1546 WARN_ON(!map
->reg_read
);
1548 if (!map
->cache_bypass
) {
1549 ret
= regcache_read(map
, reg
, val
);
1554 if (map
->cache_only
)
1557 ret
= map
->reg_read(context
, reg
, val
);
1560 if (strcmp(dev_name(map
->dev
), LOG_DEVICE
) == 0)
1561 dev_info(map
->dev
, "%x => %x\n", reg
, *val
);
1564 trace_regmap_reg_read(map
->dev
, reg
, *val
);
1566 if (!map
->cache_bypass
)
1567 regcache_write(map
, reg
, *val
);
1574 * regmap_read(): Read a value from a single register
1576 * @map: Register map to write to
1577 * @reg: Register to be read from
1578 * @val: Pointer to store read value
1580 * A value of zero will be returned on success, a negative errno will
1581 * be returned in error cases.
1583 int regmap_read(struct regmap
*map
, unsigned int reg
, unsigned int *val
)
1587 if (reg
% map
->reg_stride
)
1590 map
->lock(map
->lock_arg
);
1592 ret
= _regmap_read(map
, reg
, val
);
1594 map
->unlock(map
->lock_arg
);
1598 EXPORT_SYMBOL_GPL(regmap_read
);
1601 * regmap_raw_read(): Read raw data from the device
1603 * @map: Register map to write to
1604 * @reg: First register to be read from
1605 * @val: Pointer to store read value
1606 * @val_len: Size of data to read
1608 * A value of zero will be returned on success, a negative errno will
1609 * be returned in error cases.
1611 int regmap_raw_read(struct regmap
*map
, unsigned int reg
, void *val
,
1614 size_t val_bytes
= map
->format
.val_bytes
;
1615 size_t val_count
= val_len
/ val_bytes
;
1621 if (val_len
% map
->format
.val_bytes
)
1623 if (reg
% map
->reg_stride
)
1626 map
->lock(map
->lock_arg
);
1628 if (regmap_volatile_range(map
, reg
, val_count
) || map
->cache_bypass
||
1629 map
->cache_type
== REGCACHE_NONE
) {
1630 /* Physical block read if there's no cache involved */
1631 ret
= _regmap_raw_read(map
, reg
, val
, val_len
);
1634 /* Otherwise go word by word for the cache; should be low
1635 * cost as we expect to hit the cache.
1637 for (i
= 0; i
< val_count
; i
++) {
1638 ret
= _regmap_read(map
, reg
+ (i
* map
->reg_stride
),
1643 map
->format
.format_val(val
+ (i
* val_bytes
), v
, 0);
1648 map
->unlock(map
->lock_arg
);
1652 EXPORT_SYMBOL_GPL(regmap_raw_read
);
1655 * regmap_field_read(): Read a value to a single register field
1657 * @field: Register field to read from
1658 * @val: Pointer to store read value
1660 * A value of zero will be returned on success, a negative errno will
1661 * be returned in error cases.
1663 int regmap_field_read(struct regmap_field
*field
, unsigned int *val
)
1666 unsigned int reg_val
;
1667 ret
= regmap_read(field
->regmap
, field
->reg
, ®_val
);
1671 reg_val
&= field
->mask
;
1672 reg_val
>>= field
->shift
;
1677 EXPORT_SYMBOL_GPL(regmap_field_read
);
1680 * regmap_bulk_read(): Read multiple registers from the device
1682 * @map: Register map to write to
1683 * @reg: First register to be read from
1684 * @val: Pointer to store read value, in native register size for device
1685 * @val_count: Number of registers to read
1687 * A value of zero will be returned on success, a negative errno will
1688 * be returned in error cases.
1690 int regmap_bulk_read(struct regmap
*map
, unsigned int reg
, void *val
,
1694 size_t val_bytes
= map
->format
.val_bytes
;
1695 bool vol
= regmap_volatile_range(map
, reg
, val_count
);
1699 if (!map
->format
.parse_inplace
)
1701 if (reg
% map
->reg_stride
)
1704 if (vol
|| map
->cache_type
== REGCACHE_NONE
) {
1706 * Some devices does not support bulk read, for
1707 * them we have a series of single read operations.
1709 if (map
->use_single_rw
) {
1710 for (i
= 0; i
< val_count
; i
++) {
1711 ret
= regmap_raw_read(map
,
1712 reg
+ (i
* map
->reg_stride
),
1713 val
+ (i
* val_bytes
),
1719 ret
= regmap_raw_read(map
, reg
, val
,
1720 val_bytes
* val_count
);
1725 for (i
= 0; i
< val_count
* val_bytes
; i
+= val_bytes
)
1726 map
->format
.parse_inplace(val
+ i
);
1728 for (i
= 0; i
< val_count
; i
++) {
1730 ret
= regmap_read(map
, reg
+ (i
* map
->reg_stride
),
1734 memcpy(val
+ (i
* val_bytes
), &ival
, val_bytes
);
1740 EXPORT_SYMBOL_GPL(regmap_bulk_read
);
1742 static int _regmap_update_bits(struct regmap
*map
, unsigned int reg
,
1743 unsigned int mask
, unsigned int val
,
1747 unsigned int tmp
, orig
;
1749 ret
= _regmap_read(map
, reg
, &orig
);
1757 ret
= _regmap_write(map
, reg
, tmp
);
1767 * regmap_update_bits: Perform a read/modify/write cycle on the register map
1769 * @map: Register map to update
1770 * @reg: Register to update
1771 * @mask: Bitmask to change
1772 * @val: New value for bitmask
1774 * Returns zero for success, a negative number on error.
1776 int regmap_update_bits(struct regmap
*map
, unsigned int reg
,
1777 unsigned int mask
, unsigned int val
)
1782 map
->lock(map
->lock_arg
);
1783 ret
= _regmap_update_bits(map
, reg
, mask
, val
, &change
);
1784 map
->unlock(map
->lock_arg
);
1788 EXPORT_SYMBOL_GPL(regmap_update_bits
);
1791 * regmap_update_bits_check: Perform a read/modify/write cycle on the
1792 * register map and report if updated
1794 * @map: Register map to update
1795 * @reg: Register to update
1796 * @mask: Bitmask to change
1797 * @val: New value for bitmask
1798 * @change: Boolean indicating if a write was done
1800 * Returns zero for success, a negative number on error.
1802 int regmap_update_bits_check(struct regmap
*map
, unsigned int reg
,
1803 unsigned int mask
, unsigned int val
,
1808 map
->lock(map
->lock_arg
);
1809 ret
= _regmap_update_bits(map
, reg
, mask
, val
, change
);
1810 map
->unlock(map
->lock_arg
);
1813 EXPORT_SYMBOL_GPL(regmap_update_bits_check
);
1815 void regmap_async_complete_cb(struct regmap_async
*async
, int ret
)
1817 struct regmap
*map
= async
->map
;
1820 trace_regmap_async_io_complete(map
->dev
);
1822 spin_lock(&map
->async_lock
);
1824 list_del(&async
->list
);
1825 wake
= list_empty(&map
->async_list
);
1828 map
->async_ret
= ret
;
1830 spin_unlock(&map
->async_lock
);
1832 schedule_work(&async
->cleanup
);
1835 wake_up(&map
->async_waitq
);
1837 EXPORT_SYMBOL_GPL(regmap_async_complete_cb
);
1839 static int regmap_async_is_done(struct regmap
*map
)
1841 unsigned long flags
;
1844 spin_lock_irqsave(&map
->async_lock
, flags
);
1845 ret
= list_empty(&map
->async_list
);
1846 spin_unlock_irqrestore(&map
->async_lock
, flags
);
1852 * regmap_async_complete: Ensure all asynchronous I/O has completed.
1854 * @map: Map to operate on.
1856 * Blocks until any pending asynchronous I/O has completed. Returns
1857 * an error code for any failed I/O operations.
1859 int regmap_async_complete(struct regmap
*map
)
1861 unsigned long flags
;
1864 /* Nothing to do with no async support */
1865 if (!map
->bus
|| !map
->bus
->async_write
)
1868 trace_regmap_async_complete_start(map
->dev
);
1870 wait_event(map
->async_waitq
, regmap_async_is_done(map
));
1872 spin_lock_irqsave(&map
->async_lock
, flags
);
1873 ret
= map
->async_ret
;
1875 spin_unlock_irqrestore(&map
->async_lock
, flags
);
1877 trace_regmap_async_complete_done(map
->dev
);
1881 EXPORT_SYMBOL_GPL(regmap_async_complete
);
1884 * regmap_register_patch: Register and apply register updates to be applied
1885 * on device initialistion
1887 * @map: Register map to apply updates to.
1888 * @regs: Values to update.
1889 * @num_regs: Number of entries in regs.
1891 * Register a set of register updates to be applied to the device
1892 * whenever the device registers are synchronised with the cache and
1893 * apply them immediately. Typically this is used to apply
1894 * corrections to be applied to the device defaults on startup, such
1895 * as the updates some vendors provide to undocumented registers.
1897 int regmap_register_patch(struct regmap
*map
, const struct reg_default
*regs
,
1900 struct reg_default
*p
;
1904 map
->lock(map
->lock_arg
);
1906 bypass
= map
->cache_bypass
;
1908 map
->cache_bypass
= true;
1910 /* Write out first; it's useful to apply even if we fail later. */
1911 for (i
= 0; i
< num_regs
; i
++) {
1912 ret
= _regmap_write(map
, regs
[i
].reg
, regs
[i
].def
);
1914 dev_err(map
->dev
, "Failed to write %x = %x: %d\n",
1915 regs
[i
].reg
, regs
[i
].def
, ret
);
1920 p
= krealloc(map
->patch
,
1921 sizeof(struct reg_default
) * (map
->patch_regs
+ num_regs
),
1924 memcpy(p
+ map
->patch_regs
, regs
, num_regs
* sizeof(*regs
));
1926 map
->patch_regs
+= num_regs
;
1932 map
->cache_bypass
= bypass
;
1934 map
->unlock(map
->lock_arg
);
1938 EXPORT_SYMBOL_GPL(regmap_register_patch
);
1941 * regmap_get_val_bytes(): Report the size of a register value
1943 * Report the size of a register value, mainly intended to for use by
1944 * generic infrastructure built on top of regmap.
1946 int regmap_get_val_bytes(struct regmap
*map
)
1948 if (map
->format
.format_write
)
1951 return map
->format
.val_bytes
;
1953 EXPORT_SYMBOL_GPL(regmap_get_val_bytes
);
1955 static int __init
regmap_initcall(void)
1957 regmap_debugfs_initcall();
1961 postcore_initcall(regmap_initcall
);