1 // SPDX-License-Identifier: GPL-2.0
3 // Register map access API
5 // Copyright 2011 Wolfson Microelectronics plc
7 // Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
9 #include <linux/device.h>
10 #include <linux/slab.h>
11 #include <linux/export.h>
12 #include <linux/mutex.h>
13 #include <linux/err.h>
14 #include <linux/property.h>
15 #include <linux/rbtree.h>
16 #include <linux/sched.h>
17 #include <linux/delay.h>
18 #include <linux/log2.h>
19 #include <linux/hwspinlock.h>
20 #include <asm/unaligned.h>
22 #define CREATE_TRACE_POINTS
28 * Sometimes for failures during very early init the trace
29 * infrastructure isn't available early enough to be used. For this
30 * sort of problem defining LOG_DEVICE will add printks for basic
31 * register I/O on a specific device.
36 static inline bool regmap_should_log(struct regmap
*map
)
38 return (map
->dev
&& strcmp(dev_name(map
->dev
), LOG_DEVICE
) == 0);
41 static inline bool regmap_should_log(struct regmap
*map
) { return false; }
45 static int _regmap_update_bits(struct regmap
*map
, unsigned int reg
,
46 unsigned int mask
, unsigned int val
,
47 bool *change
, bool force_write
);
49 static int _regmap_bus_reg_read(void *context
, unsigned int reg
,
51 static int _regmap_bus_read(void *context
, unsigned int reg
,
53 static int _regmap_bus_formatted_write(void *context
, unsigned int reg
,
55 static int _regmap_bus_reg_write(void *context
, unsigned int reg
,
57 static int _regmap_bus_raw_write(void *context
, unsigned int reg
,
60 bool regmap_reg_in_ranges(unsigned int reg
,
61 const struct regmap_range
*ranges
,
64 const struct regmap_range
*r
;
67 for (i
= 0, r
= ranges
; i
< nranges
; i
++, r
++)
68 if (regmap_reg_in_range(reg
, r
))
72 EXPORT_SYMBOL_GPL(regmap_reg_in_ranges
);
74 bool regmap_check_range_table(struct regmap
*map
, unsigned int reg
,
75 const struct regmap_access_table
*table
)
77 /* Check "no ranges" first */
78 if (regmap_reg_in_ranges(reg
, table
->no_ranges
, table
->n_no_ranges
))
81 /* In case zero "yes ranges" are supplied, any reg is OK */
82 if (!table
->n_yes_ranges
)
85 return regmap_reg_in_ranges(reg
, table
->yes_ranges
,
88 EXPORT_SYMBOL_GPL(regmap_check_range_table
);
90 bool regmap_writeable(struct regmap
*map
, unsigned int reg
)
92 if (map
->max_register
&& reg
> map
->max_register
)
95 if (map
->writeable_reg
)
96 return map
->writeable_reg(map
->dev
, reg
);
99 return regmap_check_range_table(map
, reg
, map
->wr_table
);
104 bool regmap_cached(struct regmap
*map
, unsigned int reg
)
109 if (map
->cache_type
== REGCACHE_NONE
)
115 if (map
->max_register
&& reg
> map
->max_register
)
118 map
->lock(map
->lock_arg
);
119 ret
= regcache_read(map
, reg
, &val
);
120 map
->unlock(map
->lock_arg
);
127 bool regmap_readable(struct regmap
*map
, unsigned int reg
)
132 if (map
->max_register
&& reg
> map
->max_register
)
135 if (map
->format
.format_write
)
138 if (map
->readable_reg
)
139 return map
->readable_reg(map
->dev
, reg
);
142 return regmap_check_range_table(map
, reg
, map
->rd_table
);
147 bool regmap_volatile(struct regmap
*map
, unsigned int reg
)
149 if (!map
->format
.format_write
&& !regmap_readable(map
, reg
))
152 if (map
->volatile_reg
)
153 return map
->volatile_reg(map
->dev
, reg
);
155 if (map
->volatile_table
)
156 return regmap_check_range_table(map
, reg
, map
->volatile_table
);
164 bool regmap_precious(struct regmap
*map
, unsigned int reg
)
166 if (!regmap_readable(map
, reg
))
169 if (map
->precious_reg
)
170 return map
->precious_reg(map
->dev
, reg
);
172 if (map
->precious_table
)
173 return regmap_check_range_table(map
, reg
, map
->precious_table
);
178 bool regmap_writeable_noinc(struct regmap
*map
, unsigned int reg
)
180 if (map
->writeable_noinc_reg
)
181 return map
->writeable_noinc_reg(map
->dev
, reg
);
183 if (map
->wr_noinc_table
)
184 return regmap_check_range_table(map
, reg
, map
->wr_noinc_table
);
189 bool regmap_readable_noinc(struct regmap
*map
, unsigned int reg
)
191 if (map
->readable_noinc_reg
)
192 return map
->readable_noinc_reg(map
->dev
, reg
);
194 if (map
->rd_noinc_table
)
195 return regmap_check_range_table(map
, reg
, map
->rd_noinc_table
);
200 static bool regmap_volatile_range(struct regmap
*map
, unsigned int reg
,
205 for (i
= 0; i
< num
; i
++)
206 if (!regmap_volatile(map
, reg
+ regmap_get_offset(map
, i
)))
212 static void regmap_format_2_6_write(struct regmap
*map
,
213 unsigned int reg
, unsigned int val
)
215 u8
*out
= map
->work_buf
;
217 *out
= (reg
<< 6) | val
;
220 static void regmap_format_4_12_write(struct regmap
*map
,
221 unsigned int reg
, unsigned int val
)
223 __be16
*out
= map
->work_buf
;
224 *out
= cpu_to_be16((reg
<< 12) | val
);
227 static void regmap_format_7_9_write(struct regmap
*map
,
228 unsigned int reg
, unsigned int val
)
230 __be16
*out
= map
->work_buf
;
231 *out
= cpu_to_be16((reg
<< 9) | val
);
234 static void regmap_format_10_14_write(struct regmap
*map
,
235 unsigned int reg
, unsigned int val
)
237 u8
*out
= map
->work_buf
;
240 out
[1] = (val
>> 8) | (reg
<< 6);
244 static void regmap_format_8(void *buf
, unsigned int val
, unsigned int shift
)
251 static void regmap_format_16_be(void *buf
, unsigned int val
, unsigned int shift
)
253 put_unaligned_be16(val
<< shift
, buf
);
256 static void regmap_format_16_le(void *buf
, unsigned int val
, unsigned int shift
)
258 put_unaligned_le16(val
<< shift
, buf
);
261 static void regmap_format_16_native(void *buf
, unsigned int val
,
264 u16 v
= val
<< shift
;
266 memcpy(buf
, &v
, sizeof(v
));
269 static void regmap_format_24(void *buf
, unsigned int val
, unsigned int shift
)
280 static void regmap_format_32_be(void *buf
, unsigned int val
, unsigned int shift
)
282 put_unaligned_be32(val
<< shift
, buf
);
285 static void regmap_format_32_le(void *buf
, unsigned int val
, unsigned int shift
)
287 put_unaligned_le32(val
<< shift
, buf
);
290 static void regmap_format_32_native(void *buf
, unsigned int val
,
293 u32 v
= val
<< shift
;
295 memcpy(buf
, &v
, sizeof(v
));
299 static void regmap_format_64_be(void *buf
, unsigned int val
, unsigned int shift
)
301 put_unaligned_be64((u64
) val
<< shift
, buf
);
304 static void regmap_format_64_le(void *buf
, unsigned int val
, unsigned int shift
)
306 put_unaligned_le64((u64
) val
<< shift
, buf
);
309 static void regmap_format_64_native(void *buf
, unsigned int val
,
312 u64 v
= (u64
) val
<< shift
;
314 memcpy(buf
, &v
, sizeof(v
));
318 static void regmap_parse_inplace_noop(void *buf
)
322 static unsigned int regmap_parse_8(const void *buf
)
329 static unsigned int regmap_parse_16_be(const void *buf
)
331 return get_unaligned_be16(buf
);
334 static unsigned int regmap_parse_16_le(const void *buf
)
336 return get_unaligned_le16(buf
);
339 static void regmap_parse_16_be_inplace(void *buf
)
341 u16 v
= get_unaligned_be16(buf
);
343 memcpy(buf
, &v
, sizeof(v
));
346 static void regmap_parse_16_le_inplace(void *buf
)
348 u16 v
= get_unaligned_le16(buf
);
350 memcpy(buf
, &v
, sizeof(v
));
353 static unsigned int regmap_parse_16_native(const void *buf
)
357 memcpy(&v
, buf
, sizeof(v
));
361 static unsigned int regmap_parse_24(const void *buf
)
364 unsigned int ret
= b
[2];
365 ret
|= ((unsigned int)b
[1]) << 8;
366 ret
|= ((unsigned int)b
[0]) << 16;
371 static unsigned int regmap_parse_32_be(const void *buf
)
373 return get_unaligned_be32(buf
);
376 static unsigned int regmap_parse_32_le(const void *buf
)
378 return get_unaligned_le32(buf
);
381 static void regmap_parse_32_be_inplace(void *buf
)
383 u32 v
= get_unaligned_be32(buf
);
385 memcpy(buf
, &v
, sizeof(v
));
388 static void regmap_parse_32_le_inplace(void *buf
)
390 u32 v
= get_unaligned_le32(buf
);
392 memcpy(buf
, &v
, sizeof(v
));
395 static unsigned int regmap_parse_32_native(const void *buf
)
399 memcpy(&v
, buf
, sizeof(v
));
404 static unsigned int regmap_parse_64_be(const void *buf
)
406 return get_unaligned_be64(buf
);
409 static unsigned int regmap_parse_64_le(const void *buf
)
411 return get_unaligned_le64(buf
);
414 static void regmap_parse_64_be_inplace(void *buf
)
416 u64 v
= get_unaligned_be64(buf
);
418 memcpy(buf
, &v
, sizeof(v
));
421 static void regmap_parse_64_le_inplace(void *buf
)
423 u64 v
= get_unaligned_le64(buf
);
425 memcpy(buf
, &v
, sizeof(v
));
428 static unsigned int regmap_parse_64_native(const void *buf
)
432 memcpy(&v
, buf
, sizeof(v
));
437 static void regmap_lock_hwlock(void *__map
)
439 struct regmap
*map
= __map
;
441 hwspin_lock_timeout(map
->hwlock
, UINT_MAX
);
444 static void regmap_lock_hwlock_irq(void *__map
)
446 struct regmap
*map
= __map
;
448 hwspin_lock_timeout_irq(map
->hwlock
, UINT_MAX
);
451 static void regmap_lock_hwlock_irqsave(void *__map
)
453 struct regmap
*map
= __map
;
455 hwspin_lock_timeout_irqsave(map
->hwlock
, UINT_MAX
,
456 &map
->spinlock_flags
);
459 static void regmap_unlock_hwlock(void *__map
)
461 struct regmap
*map
= __map
;
463 hwspin_unlock(map
->hwlock
);
466 static void regmap_unlock_hwlock_irq(void *__map
)
468 struct regmap
*map
= __map
;
470 hwspin_unlock_irq(map
->hwlock
);
473 static void regmap_unlock_hwlock_irqrestore(void *__map
)
475 struct regmap
*map
= __map
;
477 hwspin_unlock_irqrestore(map
->hwlock
, &map
->spinlock_flags
);
480 static void regmap_lock_unlock_none(void *__map
)
485 static void regmap_lock_mutex(void *__map
)
487 struct regmap
*map
= __map
;
488 mutex_lock(&map
->mutex
);
491 static void regmap_unlock_mutex(void *__map
)
493 struct regmap
*map
= __map
;
494 mutex_unlock(&map
->mutex
);
497 static void regmap_lock_spinlock(void *__map
)
498 __acquires(&map
->spinlock
)
500 struct regmap
*map
= __map
;
503 spin_lock_irqsave(&map
->spinlock
, flags
);
504 map
->spinlock_flags
= flags
;
507 static void regmap_unlock_spinlock(void *__map
)
508 __releases(&map
->spinlock
)
510 struct regmap
*map
= __map
;
511 spin_unlock_irqrestore(&map
->spinlock
, map
->spinlock_flags
);
514 static void dev_get_regmap_release(struct device
*dev
, void *res
)
517 * We don't actually have anything to do here; the goal here
518 * is not to manage the regmap but to provide a simple way to
519 * get the regmap back given a struct device.
523 static bool _regmap_range_add(struct regmap
*map
,
524 struct regmap_range_node
*data
)
526 struct rb_root
*root
= &map
->range_tree
;
527 struct rb_node
**new = &(root
->rb_node
), *parent
= NULL
;
530 struct regmap_range_node
*this =
531 rb_entry(*new, struct regmap_range_node
, node
);
534 if (data
->range_max
< this->range_min
)
535 new = &((*new)->rb_left
);
536 else if (data
->range_min
> this->range_max
)
537 new = &((*new)->rb_right
);
542 rb_link_node(&data
->node
, parent
, new);
543 rb_insert_color(&data
->node
, root
);
548 static struct regmap_range_node
*_regmap_range_lookup(struct regmap
*map
,
551 struct rb_node
*node
= map
->range_tree
.rb_node
;
554 struct regmap_range_node
*this =
555 rb_entry(node
, struct regmap_range_node
, node
);
557 if (reg
< this->range_min
)
558 node
= node
->rb_left
;
559 else if (reg
> this->range_max
)
560 node
= node
->rb_right
;
568 static void regmap_range_exit(struct regmap
*map
)
570 struct rb_node
*next
;
571 struct regmap_range_node
*range_node
;
573 next
= rb_first(&map
->range_tree
);
575 range_node
= rb_entry(next
, struct regmap_range_node
, node
);
576 next
= rb_next(&range_node
->node
);
577 rb_erase(&range_node
->node
, &map
->range_tree
);
581 kfree(map
->selector_work_buf
);
584 static int regmap_set_name(struct regmap
*map
, const struct regmap_config
*config
)
587 const char *name
= kstrdup_const(config
->name
, GFP_KERNEL
);
592 kfree_const(map
->name
);
599 int regmap_attach_dev(struct device
*dev
, struct regmap
*map
,
600 const struct regmap_config
*config
)
607 ret
= regmap_set_name(map
, config
);
611 regmap_debugfs_init(map
);
613 /* Add a devres resource for dev_get_regmap() */
614 m
= devres_alloc(dev_get_regmap_release
, sizeof(*m
), GFP_KERNEL
);
616 regmap_debugfs_exit(map
);
624 EXPORT_SYMBOL_GPL(regmap_attach_dev
);
626 static enum regmap_endian
regmap_get_reg_endian(const struct regmap_bus
*bus
,
627 const struct regmap_config
*config
)
629 enum regmap_endian endian
;
631 /* Retrieve the endianness specification from the regmap config */
632 endian
= config
->reg_format_endian
;
634 /* If the regmap config specified a non-default value, use that */
635 if (endian
!= REGMAP_ENDIAN_DEFAULT
)
638 /* Retrieve the endianness specification from the bus config */
639 if (bus
&& bus
->reg_format_endian_default
)
640 endian
= bus
->reg_format_endian_default
;
642 /* If the bus specified a non-default value, use that */
643 if (endian
!= REGMAP_ENDIAN_DEFAULT
)
646 /* Use this if no other value was found */
647 return REGMAP_ENDIAN_BIG
;
650 enum regmap_endian
regmap_get_val_endian(struct device
*dev
,
651 const struct regmap_bus
*bus
,
652 const struct regmap_config
*config
)
654 struct fwnode_handle
*fwnode
= dev
? dev_fwnode(dev
) : NULL
;
655 enum regmap_endian endian
;
657 /* Retrieve the endianness specification from the regmap config */
658 endian
= config
->val_format_endian
;
660 /* If the regmap config specified a non-default value, use that */
661 if (endian
!= REGMAP_ENDIAN_DEFAULT
)
664 /* If the firmware node exist try to get endianness from it */
665 if (fwnode_property_read_bool(fwnode
, "big-endian"))
666 endian
= REGMAP_ENDIAN_BIG
;
667 else if (fwnode_property_read_bool(fwnode
, "little-endian"))
668 endian
= REGMAP_ENDIAN_LITTLE
;
669 else if (fwnode_property_read_bool(fwnode
, "native-endian"))
670 endian
= REGMAP_ENDIAN_NATIVE
;
672 /* If the endianness was specified in fwnode, use that */
673 if (endian
!= REGMAP_ENDIAN_DEFAULT
)
676 /* Retrieve the endianness specification from the bus config */
677 if (bus
&& bus
->val_format_endian_default
)
678 endian
= bus
->val_format_endian_default
;
680 /* If the bus specified a non-default value, use that */
681 if (endian
!= REGMAP_ENDIAN_DEFAULT
)
684 /* Use this if no other value was found */
685 return REGMAP_ENDIAN_BIG
;
687 EXPORT_SYMBOL_GPL(regmap_get_val_endian
);
689 struct regmap
*__regmap_init(struct device
*dev
,
690 const struct regmap_bus
*bus
,
692 const struct regmap_config
*config
,
693 struct lock_class_key
*lock_key
,
694 const char *lock_name
)
698 enum regmap_endian reg_endian
, val_endian
;
704 map
= kzalloc(sizeof(*map
), GFP_KERNEL
);
710 ret
= regmap_set_name(map
, config
);
714 ret
= -EINVAL
; /* Later error paths rely on this */
716 if (config
->disable_locking
) {
717 map
->lock
= map
->unlock
= regmap_lock_unlock_none
;
718 regmap_debugfs_disable(map
);
719 } else if (config
->lock
&& config
->unlock
) {
720 map
->lock
= config
->lock
;
721 map
->unlock
= config
->unlock
;
722 map
->lock_arg
= config
->lock_arg
;
723 } else if (config
->use_hwlock
) {
724 map
->hwlock
= hwspin_lock_request_specific(config
->hwlock_id
);
730 switch (config
->hwlock_mode
) {
731 case HWLOCK_IRQSTATE
:
732 map
->lock
= regmap_lock_hwlock_irqsave
;
733 map
->unlock
= regmap_unlock_hwlock_irqrestore
;
736 map
->lock
= regmap_lock_hwlock_irq
;
737 map
->unlock
= regmap_unlock_hwlock_irq
;
740 map
->lock
= regmap_lock_hwlock
;
741 map
->unlock
= regmap_unlock_hwlock
;
747 if ((bus
&& bus
->fast_io
) ||
749 spin_lock_init(&map
->spinlock
);
750 map
->lock
= regmap_lock_spinlock
;
751 map
->unlock
= regmap_unlock_spinlock
;
752 lockdep_set_class_and_name(&map
->spinlock
,
753 lock_key
, lock_name
);
755 mutex_init(&map
->mutex
);
756 map
->lock
= regmap_lock_mutex
;
757 map
->unlock
= regmap_unlock_mutex
;
758 lockdep_set_class_and_name(&map
->mutex
,
759 lock_key
, lock_name
);
765 * When we write in fast-paths with regmap_bulk_write() don't allocate
766 * scratch buffers with sleeping allocations.
768 if ((bus
&& bus
->fast_io
) || config
->fast_io
)
769 map
->alloc_flags
= GFP_ATOMIC
;
771 map
->alloc_flags
= GFP_KERNEL
;
773 map
->format
.reg_bytes
= DIV_ROUND_UP(config
->reg_bits
, 8);
774 map
->format
.pad_bytes
= config
->pad_bits
/ 8;
775 map
->format
.val_bytes
= DIV_ROUND_UP(config
->val_bits
, 8);
776 map
->format
.buf_size
= DIV_ROUND_UP(config
->reg_bits
+
777 config
->val_bits
+ config
->pad_bits
, 8);
778 map
->reg_shift
= config
->pad_bits
% 8;
779 if (config
->reg_stride
)
780 map
->reg_stride
= config
->reg_stride
;
783 if (is_power_of_2(map
->reg_stride
))
784 map
->reg_stride_order
= ilog2(map
->reg_stride
);
786 map
->reg_stride_order
= -1;
787 map
->use_single_read
= config
->use_single_read
|| !bus
|| !bus
->read
;
788 map
->use_single_write
= config
->use_single_write
|| !bus
|| !bus
->write
;
789 map
->can_multi_write
= config
->can_multi_write
&& bus
&& bus
->write
;
791 map
->max_raw_read
= bus
->max_raw_read
;
792 map
->max_raw_write
= bus
->max_raw_write
;
796 map
->bus_context
= bus_context
;
797 map
->max_register
= config
->max_register
;
798 map
->wr_table
= config
->wr_table
;
799 map
->rd_table
= config
->rd_table
;
800 map
->volatile_table
= config
->volatile_table
;
801 map
->precious_table
= config
->precious_table
;
802 map
->wr_noinc_table
= config
->wr_noinc_table
;
803 map
->rd_noinc_table
= config
->rd_noinc_table
;
804 map
->writeable_reg
= config
->writeable_reg
;
805 map
->readable_reg
= config
->readable_reg
;
806 map
->volatile_reg
= config
->volatile_reg
;
807 map
->precious_reg
= config
->precious_reg
;
808 map
->writeable_noinc_reg
= config
->writeable_noinc_reg
;
809 map
->readable_noinc_reg
= config
->readable_noinc_reg
;
810 map
->cache_type
= config
->cache_type
;
812 spin_lock_init(&map
->async_lock
);
813 INIT_LIST_HEAD(&map
->async_list
);
814 INIT_LIST_HEAD(&map
->async_free
);
815 init_waitqueue_head(&map
->async_waitq
);
817 if (config
->read_flag_mask
||
818 config
->write_flag_mask
||
819 config
->zero_flag_mask
) {
820 map
->read_flag_mask
= config
->read_flag_mask
;
821 map
->write_flag_mask
= config
->write_flag_mask
;
823 map
->read_flag_mask
= bus
->read_flag_mask
;
827 map
->reg_read
= config
->reg_read
;
828 map
->reg_write
= config
->reg_write
;
830 map
->defer_caching
= false;
831 goto skip_format_initialization
;
832 } else if (!bus
->read
|| !bus
->write
) {
833 map
->reg_read
= _regmap_bus_reg_read
;
834 map
->reg_write
= _regmap_bus_reg_write
;
835 map
->reg_update_bits
= bus
->reg_update_bits
;
837 map
->defer_caching
= false;
838 goto skip_format_initialization
;
840 map
->reg_read
= _regmap_bus_read
;
841 map
->reg_update_bits
= bus
->reg_update_bits
;
844 reg_endian
= regmap_get_reg_endian(bus
, config
);
845 val_endian
= regmap_get_val_endian(dev
, bus
, config
);
847 switch (config
->reg_bits
+ map
->reg_shift
) {
849 switch (config
->val_bits
) {
851 map
->format
.format_write
= regmap_format_2_6_write
;
859 switch (config
->val_bits
) {
861 map
->format
.format_write
= regmap_format_4_12_write
;
869 switch (config
->val_bits
) {
871 map
->format
.format_write
= regmap_format_7_9_write
;
879 switch (config
->val_bits
) {
881 map
->format
.format_write
= regmap_format_10_14_write
;
889 map
->format
.format_reg
= regmap_format_8
;
893 switch (reg_endian
) {
894 case REGMAP_ENDIAN_BIG
:
895 map
->format
.format_reg
= regmap_format_16_be
;
897 case REGMAP_ENDIAN_LITTLE
:
898 map
->format
.format_reg
= regmap_format_16_le
;
900 case REGMAP_ENDIAN_NATIVE
:
901 map
->format
.format_reg
= regmap_format_16_native
;
909 if (reg_endian
!= REGMAP_ENDIAN_BIG
)
911 map
->format
.format_reg
= regmap_format_24
;
915 switch (reg_endian
) {
916 case REGMAP_ENDIAN_BIG
:
917 map
->format
.format_reg
= regmap_format_32_be
;
919 case REGMAP_ENDIAN_LITTLE
:
920 map
->format
.format_reg
= regmap_format_32_le
;
922 case REGMAP_ENDIAN_NATIVE
:
923 map
->format
.format_reg
= regmap_format_32_native
;
932 switch (reg_endian
) {
933 case REGMAP_ENDIAN_BIG
:
934 map
->format
.format_reg
= regmap_format_64_be
;
936 case REGMAP_ENDIAN_LITTLE
:
937 map
->format
.format_reg
= regmap_format_64_le
;
939 case REGMAP_ENDIAN_NATIVE
:
940 map
->format
.format_reg
= regmap_format_64_native
;
952 if (val_endian
== REGMAP_ENDIAN_NATIVE
)
953 map
->format
.parse_inplace
= regmap_parse_inplace_noop
;
955 switch (config
->val_bits
) {
957 map
->format
.format_val
= regmap_format_8
;
958 map
->format
.parse_val
= regmap_parse_8
;
959 map
->format
.parse_inplace
= regmap_parse_inplace_noop
;
962 switch (val_endian
) {
963 case REGMAP_ENDIAN_BIG
:
964 map
->format
.format_val
= regmap_format_16_be
;
965 map
->format
.parse_val
= regmap_parse_16_be
;
966 map
->format
.parse_inplace
= regmap_parse_16_be_inplace
;
968 case REGMAP_ENDIAN_LITTLE
:
969 map
->format
.format_val
= regmap_format_16_le
;
970 map
->format
.parse_val
= regmap_parse_16_le
;
971 map
->format
.parse_inplace
= regmap_parse_16_le_inplace
;
973 case REGMAP_ENDIAN_NATIVE
:
974 map
->format
.format_val
= regmap_format_16_native
;
975 map
->format
.parse_val
= regmap_parse_16_native
;
982 if (val_endian
!= REGMAP_ENDIAN_BIG
)
984 map
->format
.format_val
= regmap_format_24
;
985 map
->format
.parse_val
= regmap_parse_24
;
988 switch (val_endian
) {
989 case REGMAP_ENDIAN_BIG
:
990 map
->format
.format_val
= regmap_format_32_be
;
991 map
->format
.parse_val
= regmap_parse_32_be
;
992 map
->format
.parse_inplace
= regmap_parse_32_be_inplace
;
994 case REGMAP_ENDIAN_LITTLE
:
995 map
->format
.format_val
= regmap_format_32_le
;
996 map
->format
.parse_val
= regmap_parse_32_le
;
997 map
->format
.parse_inplace
= regmap_parse_32_le_inplace
;
999 case REGMAP_ENDIAN_NATIVE
:
1000 map
->format
.format_val
= regmap_format_32_native
;
1001 map
->format
.parse_val
= regmap_parse_32_native
;
1009 switch (val_endian
) {
1010 case REGMAP_ENDIAN_BIG
:
1011 map
->format
.format_val
= regmap_format_64_be
;
1012 map
->format
.parse_val
= regmap_parse_64_be
;
1013 map
->format
.parse_inplace
= regmap_parse_64_be_inplace
;
1015 case REGMAP_ENDIAN_LITTLE
:
1016 map
->format
.format_val
= regmap_format_64_le
;
1017 map
->format
.parse_val
= regmap_parse_64_le
;
1018 map
->format
.parse_inplace
= regmap_parse_64_le_inplace
;
1020 case REGMAP_ENDIAN_NATIVE
:
1021 map
->format
.format_val
= regmap_format_64_native
;
1022 map
->format
.parse_val
= regmap_parse_64_native
;
1031 if (map
->format
.format_write
) {
1032 if ((reg_endian
!= REGMAP_ENDIAN_BIG
) ||
1033 (val_endian
!= REGMAP_ENDIAN_BIG
))
1035 map
->use_single_write
= true;
1038 if (!map
->format
.format_write
&&
1039 !(map
->format
.format_reg
&& map
->format
.format_val
))
1042 map
->work_buf
= kzalloc(map
->format
.buf_size
, GFP_KERNEL
);
1043 if (map
->work_buf
== NULL
) {
1048 if (map
->format
.format_write
) {
1049 map
->defer_caching
= false;
1050 map
->reg_write
= _regmap_bus_formatted_write
;
1051 } else if (map
->format
.format_val
) {
1052 map
->defer_caching
= true;
1053 map
->reg_write
= _regmap_bus_raw_write
;
1056 skip_format_initialization
:
1058 map
->range_tree
= RB_ROOT
;
1059 for (i
= 0; i
< config
->num_ranges
; i
++) {
1060 const struct regmap_range_cfg
*range_cfg
= &config
->ranges
[i
];
1061 struct regmap_range_node
*new;
1064 if (range_cfg
->range_max
< range_cfg
->range_min
) {
1065 dev_err(map
->dev
, "Invalid range %d: %d < %d\n", i
,
1066 range_cfg
->range_max
, range_cfg
->range_min
);
1070 if (range_cfg
->range_max
> map
->max_register
) {
1071 dev_err(map
->dev
, "Invalid range %d: %d > %d\n", i
,
1072 range_cfg
->range_max
, map
->max_register
);
1076 if (range_cfg
->selector_reg
> map
->max_register
) {
1078 "Invalid range %d: selector out of map\n", i
);
1082 if (range_cfg
->window_len
== 0) {
1083 dev_err(map
->dev
, "Invalid range %d: window_len 0\n",
1088 /* Make sure, that this register range has no selector
1089 or data window within its boundary */
1090 for (j
= 0; j
< config
->num_ranges
; j
++) {
1091 unsigned sel_reg
= config
->ranges
[j
].selector_reg
;
1092 unsigned win_min
= config
->ranges
[j
].window_start
;
1093 unsigned win_max
= win_min
+
1094 config
->ranges
[j
].window_len
- 1;
1096 /* Allow data window inside its own virtual range */
1100 if (range_cfg
->range_min
<= sel_reg
&&
1101 sel_reg
<= range_cfg
->range_max
) {
1103 "Range %d: selector for %d in window\n",
1108 if (!(win_max
< range_cfg
->range_min
||
1109 win_min
> range_cfg
->range_max
)) {
1111 "Range %d: window for %d in window\n",
1117 new = kzalloc(sizeof(*new), GFP_KERNEL
);
1124 new->name
= range_cfg
->name
;
1125 new->range_min
= range_cfg
->range_min
;
1126 new->range_max
= range_cfg
->range_max
;
1127 new->selector_reg
= range_cfg
->selector_reg
;
1128 new->selector_mask
= range_cfg
->selector_mask
;
1129 new->selector_shift
= range_cfg
->selector_shift
;
1130 new->window_start
= range_cfg
->window_start
;
1131 new->window_len
= range_cfg
->window_len
;
1133 if (!_regmap_range_add(map
, new)) {
1134 dev_err(map
->dev
, "Failed to add range %d\n", i
);
1139 if (map
->selector_work_buf
== NULL
) {
1140 map
->selector_work_buf
=
1141 kzalloc(map
->format
.buf_size
, GFP_KERNEL
);
1142 if (map
->selector_work_buf
== NULL
) {
1149 ret
= regcache_init(map
, config
);
1154 ret
= regmap_attach_dev(dev
, map
, config
);
1158 regmap_debugfs_init(map
);
1166 regmap_range_exit(map
);
1167 kfree(map
->work_buf
);
1170 hwspin_lock_free(map
->hwlock
);
1172 kfree_const(map
->name
);
1176 return ERR_PTR(ret
);
1178 EXPORT_SYMBOL_GPL(__regmap_init
);
1180 static void devm_regmap_release(struct device
*dev
, void *res
)
1182 regmap_exit(*(struct regmap
**)res
);
1185 struct regmap
*__devm_regmap_init(struct device
*dev
,
1186 const struct regmap_bus
*bus
,
1188 const struct regmap_config
*config
,
1189 struct lock_class_key
*lock_key
,
1190 const char *lock_name
)
1192 struct regmap
**ptr
, *regmap
;
1194 ptr
= devres_alloc(devm_regmap_release
, sizeof(*ptr
), GFP_KERNEL
);
1196 return ERR_PTR(-ENOMEM
);
1198 regmap
= __regmap_init(dev
, bus
, bus_context
, config
,
1199 lock_key
, lock_name
);
1200 if (!IS_ERR(regmap
)) {
1202 devres_add(dev
, ptr
);
1209 EXPORT_SYMBOL_GPL(__devm_regmap_init
);
1211 static void regmap_field_init(struct regmap_field
*rm_field
,
1212 struct regmap
*regmap
, struct reg_field reg_field
)
1214 rm_field
->regmap
= regmap
;
1215 rm_field
->reg
= reg_field
.reg
;
1216 rm_field
->shift
= reg_field
.lsb
;
1217 rm_field
->mask
= GENMASK(reg_field
.msb
, reg_field
.lsb
);
1218 rm_field
->id_size
= reg_field
.id_size
;
1219 rm_field
->id_offset
= reg_field
.id_offset
;
1223 * devm_regmap_field_alloc() - Allocate and initialise a register field.
1225 * @dev: Device that will be interacted with
1226 * @regmap: regmap bank in which this register field is located.
1227 * @reg_field: Register field with in the bank.
1229 * The return value will be an ERR_PTR() on error or a valid pointer
1230 * to a struct regmap_field. The regmap_field will be automatically freed
1231 * by the device management code.
1233 struct regmap_field
*devm_regmap_field_alloc(struct device
*dev
,
1234 struct regmap
*regmap
, struct reg_field reg_field
)
1236 struct regmap_field
*rm_field
= devm_kzalloc(dev
,
1237 sizeof(*rm_field
), GFP_KERNEL
);
1239 return ERR_PTR(-ENOMEM
);
1241 regmap_field_init(rm_field
, regmap
, reg_field
);
1246 EXPORT_SYMBOL_GPL(devm_regmap_field_alloc
);
1249 * devm_regmap_field_free() - Free a register field allocated using
1250 * devm_regmap_field_alloc.
1252 * @dev: Device that will be interacted with
1253 * @field: regmap field which should be freed.
1255 * Free register field allocated using devm_regmap_field_alloc(). Usually
1256 * drivers need not call this function, as the memory allocated via devm
1257 * will be freed as per device-driver life-cyle.
1259 void devm_regmap_field_free(struct device
*dev
,
1260 struct regmap_field
*field
)
1262 devm_kfree(dev
, field
);
1264 EXPORT_SYMBOL_GPL(devm_regmap_field_free
);
1267 * regmap_field_alloc() - Allocate and initialise a register field.
1269 * @regmap: regmap bank in which this register field is located.
1270 * @reg_field: Register field with in the bank.
1272 * The return value will be an ERR_PTR() on error or a valid pointer
1273 * to a struct regmap_field. The regmap_field should be freed by the
1274 * user once its finished working with it using regmap_field_free().
1276 struct regmap_field
*regmap_field_alloc(struct regmap
*regmap
,
1277 struct reg_field reg_field
)
1279 struct regmap_field
*rm_field
= kzalloc(sizeof(*rm_field
), GFP_KERNEL
);
1282 return ERR_PTR(-ENOMEM
);
1284 regmap_field_init(rm_field
, regmap
, reg_field
);
1288 EXPORT_SYMBOL_GPL(regmap_field_alloc
);
1291 * regmap_field_free() - Free register field allocated using
1292 * regmap_field_alloc.
1294 * @field: regmap field which should be freed.
1296 void regmap_field_free(struct regmap_field
*field
)
1300 EXPORT_SYMBOL_GPL(regmap_field_free
);
1303 * regmap_reinit_cache() - Reinitialise the current register cache
1305 * @map: Register map to operate on.
1306 * @config: New configuration. Only the cache data will be used.
1308 * Discard any existing register cache for the map and initialize a
1309 * new cache. This can be used to restore the cache to defaults or to
1310 * update the cache configuration to reflect runtime discovery of the
1313 * No explicit locking is done here, the user needs to ensure that
1314 * this function will not race with other calls to regmap.
1316 int regmap_reinit_cache(struct regmap
*map
, const struct regmap_config
*config
)
1321 regmap_debugfs_exit(map
);
1323 map
->max_register
= config
->max_register
;
1324 map
->writeable_reg
= config
->writeable_reg
;
1325 map
->readable_reg
= config
->readable_reg
;
1326 map
->volatile_reg
= config
->volatile_reg
;
1327 map
->precious_reg
= config
->precious_reg
;
1328 map
->writeable_noinc_reg
= config
->writeable_noinc_reg
;
1329 map
->readable_noinc_reg
= config
->readable_noinc_reg
;
1330 map
->cache_type
= config
->cache_type
;
1332 ret
= regmap_set_name(map
, config
);
1336 regmap_debugfs_init(map
);
1338 map
->cache_bypass
= false;
1339 map
->cache_only
= false;
1341 return regcache_init(map
, config
);
1343 EXPORT_SYMBOL_GPL(regmap_reinit_cache
);
1346 * regmap_exit() - Free a previously allocated register map
1348 * @map: Register map to operate on.
1350 void regmap_exit(struct regmap
*map
)
1352 struct regmap_async
*async
;
1355 regmap_debugfs_exit(map
);
1356 regmap_range_exit(map
);
1357 if (map
->bus
&& map
->bus
->free_context
)
1358 map
->bus
->free_context(map
->bus_context
);
1359 kfree(map
->work_buf
);
1360 while (!list_empty(&map
->async_free
)) {
1361 async
= list_first_entry_or_null(&map
->async_free
,
1362 struct regmap_async
,
1364 list_del(&async
->list
);
1365 kfree(async
->work_buf
);
1369 hwspin_lock_free(map
->hwlock
);
1370 kfree_const(map
->name
);
1374 EXPORT_SYMBOL_GPL(regmap_exit
);
1376 static int dev_get_regmap_match(struct device
*dev
, void *res
, void *data
)
1378 struct regmap
**r
= res
;
1384 /* If the user didn't specify a name match any */
1386 return !strcmp((*r
)->name
, data
);
1392 * dev_get_regmap() - Obtain the regmap (if any) for a device
1394 * @dev: Device to retrieve the map for
1395 * @name: Optional name for the register map, usually NULL.
1397 * Returns the regmap for the device if one is present, or NULL. If
1398 * name is specified then it must match the name specified when
1399 * registering the device, if it is NULL then the first regmap found
1400 * will be used. Devices with multiple register maps are very rare,
1401 * generic code should normally not need to specify a name.
1403 struct regmap
*dev_get_regmap(struct device
*dev
, const char *name
)
1405 struct regmap
**r
= devres_find(dev
, dev_get_regmap_release
,
1406 dev_get_regmap_match
, (void *)name
);
1412 EXPORT_SYMBOL_GPL(dev_get_regmap
);
1415 * regmap_get_device() - Obtain the device from a regmap
1417 * @map: Register map to operate on.
1419 * Returns the underlying device that the regmap has been created for.
1421 struct device
*regmap_get_device(struct regmap
*map
)
1425 EXPORT_SYMBOL_GPL(regmap_get_device
);
1427 static int _regmap_select_page(struct regmap
*map
, unsigned int *reg
,
1428 struct regmap_range_node
*range
,
1429 unsigned int val_num
)
1431 void *orig_work_buf
;
1432 unsigned int win_offset
;
1433 unsigned int win_page
;
1437 win_offset
= (*reg
- range
->range_min
) % range
->window_len
;
1438 win_page
= (*reg
- range
->range_min
) / range
->window_len
;
1441 /* Bulk write shouldn't cross range boundary */
1442 if (*reg
+ val_num
- 1 > range
->range_max
)
1445 /* ... or single page boundary */
1446 if (val_num
> range
->window_len
- win_offset
)
1450 /* It is possible to have selector register inside data window.
1451 In that case, selector register is located on every page and
1452 it needs no page switching, when accessed alone. */
1454 range
->window_start
+ win_offset
!= range
->selector_reg
) {
1455 /* Use separate work_buf during page switching */
1456 orig_work_buf
= map
->work_buf
;
1457 map
->work_buf
= map
->selector_work_buf
;
1459 ret
= _regmap_update_bits(map
, range
->selector_reg
,
1460 range
->selector_mask
,
1461 win_page
<< range
->selector_shift
,
1464 map
->work_buf
= orig_work_buf
;
1470 *reg
= range
->window_start
+ win_offset
;
1475 static void regmap_set_work_buf_flag_mask(struct regmap
*map
, int max_bytes
,
1481 if (!mask
|| !map
->work_buf
)
1484 buf
= map
->work_buf
;
1486 for (i
= 0; i
< max_bytes
; i
++)
1487 buf
[i
] |= (mask
>> (8 * i
)) & 0xff;
1490 static int _regmap_raw_write_impl(struct regmap
*map
, unsigned int reg
,
1491 const void *val
, size_t val_len
, bool noinc
)
1493 struct regmap_range_node
*range
;
1494 unsigned long flags
;
1495 void *work_val
= map
->work_buf
+ map
->format
.reg_bytes
+
1496 map
->format
.pad_bytes
;
1498 int ret
= -ENOTSUPP
;
1504 /* Check for unwritable or noinc registers in range
1507 if (!regmap_writeable_noinc(map
, reg
)) {
1508 for (i
= 0; i
< val_len
/ map
->format
.val_bytes
; i
++) {
1509 unsigned int element
=
1510 reg
+ regmap_get_offset(map
, i
);
1511 if (!regmap_writeable(map
, element
) ||
1512 regmap_writeable_noinc(map
, element
))
1517 if (!map
->cache_bypass
&& map
->format
.parse_val
) {
1519 int val_bytes
= map
->format
.val_bytes
;
1520 for (i
= 0; i
< val_len
/ val_bytes
; i
++) {
1521 ival
= map
->format
.parse_val(val
+ (i
* val_bytes
));
1522 ret
= regcache_write(map
,
1523 reg
+ regmap_get_offset(map
, i
),
1527 "Error in caching of register: %x ret: %d\n",
1532 if (map
->cache_only
) {
1533 map
->cache_dirty
= true;
1538 range
= _regmap_range_lookup(map
, reg
);
1540 int val_num
= val_len
/ map
->format
.val_bytes
;
1541 int win_offset
= (reg
- range
->range_min
) % range
->window_len
;
1542 int win_residue
= range
->window_len
- win_offset
;
1544 /* If the write goes beyond the end of the window split it */
1545 while (val_num
> win_residue
) {
1546 dev_dbg(map
->dev
, "Writing window %d/%zu\n",
1547 win_residue
, val_len
/ map
->format
.val_bytes
);
1548 ret
= _regmap_raw_write_impl(map
, reg
, val
,
1550 map
->format
.val_bytes
, noinc
);
1555 val_num
-= win_residue
;
1556 val
+= win_residue
* map
->format
.val_bytes
;
1557 val_len
-= win_residue
* map
->format
.val_bytes
;
1559 win_offset
= (reg
- range
->range_min
) %
1561 win_residue
= range
->window_len
- win_offset
;
1564 ret
= _regmap_select_page(map
, ®
, range
, noinc
? 1 : val_num
);
1569 map
->format
.format_reg(map
->work_buf
, reg
, map
->reg_shift
);
1570 regmap_set_work_buf_flag_mask(map
, map
->format
.reg_bytes
,
1571 map
->write_flag_mask
);
1574 * Essentially all I/O mechanisms will be faster with a single
1575 * buffer to write. Since register syncs often generate raw
1576 * writes of single registers optimise that case.
1578 if (val
!= work_val
&& val_len
== map
->format
.val_bytes
) {
1579 memcpy(work_val
, val
, map
->format
.val_bytes
);
1583 if (map
->async
&& map
->bus
->async_write
) {
1584 struct regmap_async
*async
;
1586 trace_regmap_async_write_start(map
, reg
, val_len
);
1588 spin_lock_irqsave(&map
->async_lock
, flags
);
1589 async
= list_first_entry_or_null(&map
->async_free
,
1590 struct regmap_async
,
1593 list_del(&async
->list
);
1594 spin_unlock_irqrestore(&map
->async_lock
, flags
);
1597 async
= map
->bus
->async_alloc();
1601 async
->work_buf
= kzalloc(map
->format
.buf_size
,
1602 GFP_KERNEL
| GFP_DMA
);
1603 if (!async
->work_buf
) {
1611 /* If the caller supplied the value we can use it safely. */
1612 memcpy(async
->work_buf
, map
->work_buf
, map
->format
.pad_bytes
+
1613 map
->format
.reg_bytes
+ map
->format
.val_bytes
);
1615 spin_lock_irqsave(&map
->async_lock
, flags
);
1616 list_add_tail(&async
->list
, &map
->async_list
);
1617 spin_unlock_irqrestore(&map
->async_lock
, flags
);
1619 if (val
!= work_val
)
1620 ret
= map
->bus
->async_write(map
->bus_context
,
1622 map
->format
.reg_bytes
+
1623 map
->format
.pad_bytes
,
1624 val
, val_len
, async
);
1626 ret
= map
->bus
->async_write(map
->bus_context
,
1628 map
->format
.reg_bytes
+
1629 map
->format
.pad_bytes
+
1630 val_len
, NULL
, 0, async
);
1633 dev_err(map
->dev
, "Failed to schedule write: %d\n",
1636 spin_lock_irqsave(&map
->async_lock
, flags
);
1637 list_move(&async
->list
, &map
->async_free
);
1638 spin_unlock_irqrestore(&map
->async_lock
, flags
);
1644 trace_regmap_hw_write_start(map
, reg
, val_len
/ map
->format
.val_bytes
);
1646 /* If we're doing a single register write we can probably just
1647 * send the work_buf directly, otherwise try to do a gather
1650 if (val
== work_val
)
1651 ret
= map
->bus
->write(map
->bus_context
, map
->work_buf
,
1652 map
->format
.reg_bytes
+
1653 map
->format
.pad_bytes
+
1655 else if (map
->bus
->gather_write
)
1656 ret
= map
->bus
->gather_write(map
->bus_context
, map
->work_buf
,
1657 map
->format
.reg_bytes
+
1658 map
->format
.pad_bytes
,
1663 /* If that didn't work fall back on linearising by hand. */
1664 if (ret
== -ENOTSUPP
) {
1665 len
= map
->format
.reg_bytes
+ map
->format
.pad_bytes
+ val_len
;
1666 buf
= kzalloc(len
, GFP_KERNEL
);
1670 memcpy(buf
, map
->work_buf
, map
->format
.reg_bytes
);
1671 memcpy(buf
+ map
->format
.reg_bytes
+ map
->format
.pad_bytes
,
1673 ret
= map
->bus
->write(map
->bus_context
, buf
, len
);
1676 } else if (ret
!= 0 && !map
->cache_bypass
&& map
->format
.parse_val
) {
1677 /* regcache_drop_region() takes lock that we already have,
1678 * thus call map->cache_ops->drop() directly
1680 if (map
->cache_ops
&& map
->cache_ops
->drop
)
1681 map
->cache_ops
->drop(map
, reg
, reg
+ 1);
1684 trace_regmap_hw_write_done(map
, reg
, val_len
/ map
->format
.val_bytes
);
1690 * regmap_can_raw_write - Test if regmap_raw_write() is supported
1692 * @map: Map to check.
1694 bool regmap_can_raw_write(struct regmap
*map
)
1696 return map
->bus
&& map
->bus
->write
&& map
->format
.format_val
&&
1697 map
->format
.format_reg
;
1699 EXPORT_SYMBOL_GPL(regmap_can_raw_write
);
1702 * regmap_get_raw_read_max - Get the maximum size we can read
1704 * @map: Map to check.
1706 size_t regmap_get_raw_read_max(struct regmap
*map
)
1708 return map
->max_raw_read
;
1710 EXPORT_SYMBOL_GPL(regmap_get_raw_read_max
);
1713 * regmap_get_raw_write_max - Get the maximum size we can read
1715 * @map: Map to check.
1717 size_t regmap_get_raw_write_max(struct regmap
*map
)
1719 return map
->max_raw_write
;
1721 EXPORT_SYMBOL_GPL(regmap_get_raw_write_max
);
1723 static int _regmap_bus_formatted_write(void *context
, unsigned int reg
,
1727 struct regmap_range_node
*range
;
1728 struct regmap
*map
= context
;
1730 WARN_ON(!map
->bus
|| !map
->format
.format_write
);
1732 range
= _regmap_range_lookup(map
, reg
);
1734 ret
= _regmap_select_page(map
, ®
, range
, 1);
1739 map
->format
.format_write(map
, reg
, val
);
1741 trace_regmap_hw_write_start(map
, reg
, 1);
1743 ret
= map
->bus
->write(map
->bus_context
, map
->work_buf
,
1744 map
->format
.buf_size
);
1746 trace_regmap_hw_write_done(map
, reg
, 1);
1751 static int _regmap_bus_reg_write(void *context
, unsigned int reg
,
1754 struct regmap
*map
= context
;
1756 return map
->bus
->reg_write(map
->bus_context
, reg
, val
);
1759 static int _regmap_bus_raw_write(void *context
, unsigned int reg
,
1762 struct regmap
*map
= context
;
1764 WARN_ON(!map
->bus
|| !map
->format
.format_val
);
1766 map
->format
.format_val(map
->work_buf
+ map
->format
.reg_bytes
1767 + map
->format
.pad_bytes
, val
, 0);
1768 return _regmap_raw_write_impl(map
, reg
,
1770 map
->format
.reg_bytes
+
1771 map
->format
.pad_bytes
,
1772 map
->format
.val_bytes
,
1776 static inline void *_regmap_map_get_context(struct regmap
*map
)
1778 return (map
->bus
) ? map
: map
->bus_context
;
1781 int _regmap_write(struct regmap
*map
, unsigned int reg
,
1785 void *context
= _regmap_map_get_context(map
);
1787 if (!regmap_writeable(map
, reg
))
1790 if (!map
->cache_bypass
&& !map
->defer_caching
) {
1791 ret
= regcache_write(map
, reg
, val
);
1794 if (map
->cache_only
) {
1795 map
->cache_dirty
= true;
1800 if (regmap_should_log(map
))
1801 dev_info(map
->dev
, "%x <= %x\n", reg
, val
);
1803 trace_regmap_reg_write(map
, reg
, val
);
1805 return map
->reg_write(context
, reg
, val
);
1809 * regmap_write() - Write a value to a single register
1811 * @map: Register map to write to
1812 * @reg: Register to write to
1813 * @val: Value to be written
1815 * A value of zero will be returned on success, a negative errno will
1816 * be returned in error cases.
1818 int regmap_write(struct regmap
*map
, unsigned int reg
, unsigned int val
)
1822 if (!IS_ALIGNED(reg
, map
->reg_stride
))
1825 map
->lock(map
->lock_arg
);
1827 ret
= _regmap_write(map
, reg
, val
);
1829 map
->unlock(map
->lock_arg
);
1833 EXPORT_SYMBOL_GPL(regmap_write
);
1836 * regmap_write_async() - Write a value to a single register asynchronously
1838 * @map: Register map to write to
1839 * @reg: Register to write to
1840 * @val: Value to be written
1842 * A value of zero will be returned on success, a negative errno will
1843 * be returned in error cases.
1845 int regmap_write_async(struct regmap
*map
, unsigned int reg
, unsigned int val
)
1849 if (!IS_ALIGNED(reg
, map
->reg_stride
))
1852 map
->lock(map
->lock_arg
);
1856 ret
= _regmap_write(map
, reg
, val
);
1860 map
->unlock(map
->lock_arg
);
1864 EXPORT_SYMBOL_GPL(regmap_write_async
);
1866 int _regmap_raw_write(struct regmap
*map
, unsigned int reg
,
1867 const void *val
, size_t val_len
, bool noinc
)
1869 size_t val_bytes
= map
->format
.val_bytes
;
1870 size_t val_count
= val_len
/ val_bytes
;
1871 size_t chunk_count
, chunk_bytes
;
1872 size_t chunk_regs
= val_count
;
1878 if (map
->use_single_write
)
1880 else if (map
->max_raw_write
&& val_len
> map
->max_raw_write
)
1881 chunk_regs
= map
->max_raw_write
/ val_bytes
;
1883 chunk_count
= val_count
/ chunk_regs
;
1884 chunk_bytes
= chunk_regs
* val_bytes
;
1886 /* Write as many bytes as possible with chunk_size */
1887 for (i
= 0; i
< chunk_count
; i
++) {
1888 ret
= _regmap_raw_write_impl(map
, reg
, val
, chunk_bytes
, noinc
);
1892 reg
+= regmap_get_offset(map
, chunk_regs
);
1894 val_len
-= chunk_bytes
;
1897 /* Write remaining bytes */
1899 ret
= _regmap_raw_write_impl(map
, reg
, val
, val_len
, noinc
);
1905 * regmap_raw_write() - Write raw values to one or more registers
1907 * @map: Register map to write to
1908 * @reg: Initial register to write to
1909 * @val: Block of data to be written, laid out for direct transmission to the
1911 * @val_len: Length of data pointed to by val.
1913 * This function is intended to be used for things like firmware
1914 * download where a large block of data needs to be transferred to the
1915 * device. No formatting will be done on the data provided.
1917 * A value of zero will be returned on success, a negative errno will
1918 * be returned in error cases.
1920 int regmap_raw_write(struct regmap
*map
, unsigned int reg
,
1921 const void *val
, size_t val_len
)
1925 if (!regmap_can_raw_write(map
))
1927 if (val_len
% map
->format
.val_bytes
)
1930 map
->lock(map
->lock_arg
);
1932 ret
= _regmap_raw_write(map
, reg
, val
, val_len
, false);
1934 map
->unlock(map
->lock_arg
);
1938 EXPORT_SYMBOL_GPL(regmap_raw_write
);
1941 * regmap_noinc_write(): Write data from a register without incrementing the
1944 * @map: Register map to write to
1945 * @reg: Register to write to
1946 * @val: Pointer to data buffer
1947 * @val_len: Length of output buffer in bytes.
1949 * The regmap API usually assumes that bulk bus write operations will write a
1950 * range of registers. Some devices have certain registers for which a write
1951 * operation can write to an internal FIFO.
1953 * The target register must be volatile but registers after it can be
1954 * completely unrelated cacheable registers.
1956 * This will attempt multiple writes as required to write val_len bytes.
1958 * A value of zero will be returned on success, a negative errno will be
1959 * returned in error cases.
1961 int regmap_noinc_write(struct regmap
*map
, unsigned int reg
,
1962 const void *val
, size_t val_len
)
1969 if (!map
->bus
->write
)
1971 if (val_len
% map
->format
.val_bytes
)
1973 if (!IS_ALIGNED(reg
, map
->reg_stride
))
1978 map
->lock(map
->lock_arg
);
1980 if (!regmap_volatile(map
, reg
) || !regmap_writeable_noinc(map
, reg
)) {
1986 if (map
->max_raw_write
&& map
->max_raw_write
< val_len
)
1987 write_len
= map
->max_raw_write
;
1989 write_len
= val_len
;
1990 ret
= _regmap_raw_write(map
, reg
, val
, write_len
, true);
1993 val
= ((u8
*)val
) + write_len
;
1994 val_len
-= write_len
;
1998 map
->unlock(map
->lock_arg
);
2001 EXPORT_SYMBOL_GPL(regmap_noinc_write
);
2004 * regmap_field_update_bits_base() - Perform a read/modify/write cycle a
2007 * @field: Register field to write to
2008 * @mask: Bitmask to change
2009 * @val: Value to be written
2010 * @change: Boolean indicating if a write was done
2011 * @async: Boolean indicating asynchronously
2012 * @force: Boolean indicating use force update
2014 * Perform a read/modify/write cycle on the register field with change,
2015 * async, force option.
2017 * A value of zero will be returned on success, a negative errno will
2018 * be returned in error cases.
2020 int regmap_field_update_bits_base(struct regmap_field
*field
,
2021 unsigned int mask
, unsigned int val
,
2022 bool *change
, bool async
, bool force
)
2024 mask
= (mask
<< field
->shift
) & field
->mask
;
2026 return regmap_update_bits_base(field
->regmap
, field
->reg
,
2027 mask
, val
<< field
->shift
,
2028 change
, async
, force
);
2030 EXPORT_SYMBOL_GPL(regmap_field_update_bits_base
);
2033 * regmap_fields_update_bits_base() - Perform a read/modify/write cycle a
2034 * register field with port ID
2036 * @field: Register field to write to
2038 * @mask: Bitmask to change
2039 * @val: Value to be written
2040 * @change: Boolean indicating if a write was done
2041 * @async: Boolean indicating asynchronously
2042 * @force: Boolean indicating use force update
2044 * A value of zero will be returned on success, a negative errno will
2045 * be returned in error cases.
2047 int regmap_fields_update_bits_base(struct regmap_field
*field
, unsigned int id
,
2048 unsigned int mask
, unsigned int val
,
2049 bool *change
, bool async
, bool force
)
2051 if (id
>= field
->id_size
)
2054 mask
= (mask
<< field
->shift
) & field
->mask
;
2056 return regmap_update_bits_base(field
->regmap
,
2057 field
->reg
+ (field
->id_offset
* id
),
2058 mask
, val
<< field
->shift
,
2059 change
, async
, force
);
2061 EXPORT_SYMBOL_GPL(regmap_fields_update_bits_base
);
2064 * regmap_bulk_write() - Write multiple registers to the device
2066 * @map: Register map to write to
2067 * @reg: First register to be write from
2068 * @val: Block of data to be written, in native register size for device
2069 * @val_count: Number of registers to write
2071 * This function is intended to be used for writing a large block of
2072 * data to the device either in single transfer or multiple transfer.
2074 * A value of zero will be returned on success, a negative errno will
2075 * be returned in error cases.
2077 int regmap_bulk_write(struct regmap
*map
, unsigned int reg
, const void *val
,
2081 size_t val_bytes
= map
->format
.val_bytes
;
2083 if (!IS_ALIGNED(reg
, map
->reg_stride
))
2087 * Some devices don't support bulk write, for them we have a series of
2088 * single write operations.
2090 if (!map
->bus
|| !map
->format
.parse_inplace
) {
2091 map
->lock(map
->lock_arg
);
2092 for (i
= 0; i
< val_count
; i
++) {
2095 switch (val_bytes
) {
2097 ival
= *(u8
*)(val
+ (i
* val_bytes
));
2100 ival
= *(u16
*)(val
+ (i
* val_bytes
));
2103 ival
= *(u32
*)(val
+ (i
* val_bytes
));
2107 ival
= *(u64
*)(val
+ (i
* val_bytes
));
2115 ret
= _regmap_write(map
,
2116 reg
+ regmap_get_offset(map
, i
),
2122 map
->unlock(map
->lock_arg
);
2126 wval
= kmemdup(val
, val_count
* val_bytes
, map
->alloc_flags
);
2130 for (i
= 0; i
< val_count
* val_bytes
; i
+= val_bytes
)
2131 map
->format
.parse_inplace(wval
+ i
);
2133 ret
= regmap_raw_write(map
, reg
, wval
, val_bytes
* val_count
);
2139 EXPORT_SYMBOL_GPL(regmap_bulk_write
);
2142 * _regmap_raw_multi_reg_write()
2144 * the (register,newvalue) pairs in regs have not been formatted, but
2145 * they are all in the same page and have been changed to being page
2146 * relative. The page register has been written if that was necessary.
2148 static int _regmap_raw_multi_reg_write(struct regmap
*map
,
2149 const struct reg_sequence
*regs
,
2156 size_t val_bytes
= map
->format
.val_bytes
;
2157 size_t reg_bytes
= map
->format
.reg_bytes
;
2158 size_t pad_bytes
= map
->format
.pad_bytes
;
2159 size_t pair_size
= reg_bytes
+ pad_bytes
+ val_bytes
;
2160 size_t len
= pair_size
* num_regs
;
2165 buf
= kzalloc(len
, GFP_KERNEL
);
2169 /* We have to linearise by hand. */
2173 for (i
= 0; i
< num_regs
; i
++) {
2174 unsigned int reg
= regs
[i
].reg
;
2175 unsigned int val
= regs
[i
].def
;
2176 trace_regmap_hw_write_start(map
, reg
, 1);
2177 map
->format
.format_reg(u8
, reg
, map
->reg_shift
);
2178 u8
+= reg_bytes
+ pad_bytes
;
2179 map
->format
.format_val(u8
, val
, 0);
2183 *u8
|= map
->write_flag_mask
;
2185 ret
= map
->bus
->write(map
->bus_context
, buf
, len
);
2189 for (i
= 0; i
< num_regs
; i
++) {
2190 int reg
= regs
[i
].reg
;
2191 trace_regmap_hw_write_done(map
, reg
, 1);
2196 static unsigned int _regmap_register_page(struct regmap
*map
,
2198 struct regmap_range_node
*range
)
2200 unsigned int win_page
= (reg
- range
->range_min
) / range
->window_len
;
2205 static int _regmap_range_multi_paged_reg_write(struct regmap
*map
,
2206 struct reg_sequence
*regs
,
2211 struct reg_sequence
*base
;
2212 unsigned int this_page
= 0;
2213 unsigned int page_change
= 0;
2215 * the set of registers are not neccessarily in order, but
2216 * since the order of write must be preserved this algorithm
2217 * chops the set each time the page changes. This also applies
2218 * if there is a delay required at any point in the sequence.
2221 for (i
= 0, n
= 0; i
< num_regs
; i
++, n
++) {
2222 unsigned int reg
= regs
[i
].reg
;
2223 struct regmap_range_node
*range
;
2225 range
= _regmap_range_lookup(map
, reg
);
2227 unsigned int win_page
= _regmap_register_page(map
, reg
,
2231 this_page
= win_page
;
2232 if (win_page
!= this_page
) {
2233 this_page
= win_page
;
2238 /* If we have both a page change and a delay make sure to
2239 * write the regs and apply the delay before we change the
2243 if (page_change
|| regs
[i
].delay_us
) {
2245 /* For situations where the first write requires
2246 * a delay we need to make sure we don't call
2247 * raw_multi_reg_write with n=0
2248 * This can't occur with page breaks as we
2249 * never write on the first iteration
2251 if (regs
[i
].delay_us
&& i
== 0)
2254 ret
= _regmap_raw_multi_reg_write(map
, base
, n
);
2258 if (regs
[i
].delay_us
)
2259 udelay(regs
[i
].delay_us
);
2265 ret
= _regmap_select_page(map
,
2278 return _regmap_raw_multi_reg_write(map
, base
, n
);
2282 static int _regmap_multi_reg_write(struct regmap
*map
,
2283 const struct reg_sequence
*regs
,
2289 if (!map
->can_multi_write
) {
2290 for (i
= 0; i
< num_regs
; i
++) {
2291 ret
= _regmap_write(map
, regs
[i
].reg
, regs
[i
].def
);
2295 if (regs
[i
].delay_us
)
2296 udelay(regs
[i
].delay_us
);
2301 if (!map
->format
.parse_inplace
)
2304 if (map
->writeable_reg
)
2305 for (i
= 0; i
< num_regs
; i
++) {
2306 int reg
= regs
[i
].reg
;
2307 if (!map
->writeable_reg(map
->dev
, reg
))
2309 if (!IS_ALIGNED(reg
, map
->reg_stride
))
2313 if (!map
->cache_bypass
) {
2314 for (i
= 0; i
< num_regs
; i
++) {
2315 unsigned int val
= regs
[i
].def
;
2316 unsigned int reg
= regs
[i
].reg
;
2317 ret
= regcache_write(map
, reg
, val
);
2320 "Error in caching of register: %x ret: %d\n",
2325 if (map
->cache_only
) {
2326 map
->cache_dirty
= true;
2333 for (i
= 0; i
< num_regs
; i
++) {
2334 unsigned int reg
= regs
[i
].reg
;
2335 struct regmap_range_node
*range
;
2337 /* Coalesce all the writes between a page break or a delay
2340 range
= _regmap_range_lookup(map
, reg
);
2341 if (range
|| regs
[i
].delay_us
) {
2342 size_t len
= sizeof(struct reg_sequence
)*num_regs
;
2343 struct reg_sequence
*base
= kmemdup(regs
, len
,
2347 ret
= _regmap_range_multi_paged_reg_write(map
, base
,
2354 return _regmap_raw_multi_reg_write(map
, regs
, num_regs
);
2358 * regmap_multi_reg_write() - Write multiple registers to the device
2360 * @map: Register map to write to
2361 * @regs: Array of structures containing register,value to be written
2362 * @num_regs: Number of registers to write
2364 * Write multiple registers to the device where the set of register, value
2365 * pairs are supplied in any order, possibly not all in a single range.
2367 * The 'normal' block write mode will send ultimately send data on the
2368 * target bus as R,V1,V2,V3,..,Vn where successively higher registers are
2369 * addressed. However, this alternative block multi write mode will send
2370 * the data as R1,V1,R2,V2,..,Rn,Vn on the target bus. The target device
2371 * must of course support the mode.
2373 * A value of zero will be returned on success, a negative errno will be
2374 * returned in error cases.
2376 int regmap_multi_reg_write(struct regmap
*map
, const struct reg_sequence
*regs
,
2381 map
->lock(map
->lock_arg
);
2383 ret
= _regmap_multi_reg_write(map
, regs
, num_regs
);
2385 map
->unlock(map
->lock_arg
);
2389 EXPORT_SYMBOL_GPL(regmap_multi_reg_write
);
2392 * regmap_multi_reg_write_bypassed() - Write multiple registers to the
2393 * device but not the cache
2395 * @map: Register map to write to
2396 * @regs: Array of structures containing register,value to be written
2397 * @num_regs: Number of registers to write
2399 * Write multiple registers to the device but not the cache where the set
2400 * of register are supplied in any order.
2402 * This function is intended to be used for writing a large block of data
2403 * atomically to the device in single transfer for those I2C client devices
2404 * that implement this alternative block write mode.
2406 * A value of zero will be returned on success, a negative errno will
2407 * be returned in error cases.
2409 int regmap_multi_reg_write_bypassed(struct regmap
*map
,
2410 const struct reg_sequence
*regs
,
2416 map
->lock(map
->lock_arg
);
2418 bypass
= map
->cache_bypass
;
2419 map
->cache_bypass
= true;
2421 ret
= _regmap_multi_reg_write(map
, regs
, num_regs
);
2423 map
->cache_bypass
= bypass
;
2425 map
->unlock(map
->lock_arg
);
2429 EXPORT_SYMBOL_GPL(regmap_multi_reg_write_bypassed
);
2432 * regmap_raw_write_async() - Write raw values to one or more registers
2435 * @map: Register map to write to
2436 * @reg: Initial register to write to
2437 * @val: Block of data to be written, laid out for direct transmission to the
2438 * device. Must be valid until regmap_async_complete() is called.
2439 * @val_len: Length of data pointed to by val.
2441 * This function is intended to be used for things like firmware
2442 * download where a large block of data needs to be transferred to the
2443 * device. No formatting will be done on the data provided.
2445 * If supported by the underlying bus the write will be scheduled
2446 * asynchronously, helping maximise I/O speed on higher speed buses
2447 * like SPI. regmap_async_complete() can be called to ensure that all
2448 * asynchrnous writes have been completed.
2450 * A value of zero will be returned on success, a negative errno will
2451 * be returned in error cases.
2453 int regmap_raw_write_async(struct regmap
*map
, unsigned int reg
,
2454 const void *val
, size_t val_len
)
2458 if (val_len
% map
->format
.val_bytes
)
2460 if (!IS_ALIGNED(reg
, map
->reg_stride
))
2463 map
->lock(map
->lock_arg
);
2467 ret
= _regmap_raw_write(map
, reg
, val
, val_len
, false);
2471 map
->unlock(map
->lock_arg
);
2475 EXPORT_SYMBOL_GPL(regmap_raw_write_async
);
2477 static int _regmap_raw_read(struct regmap
*map
, unsigned int reg
, void *val
,
2478 unsigned int val_len
, bool noinc
)
2480 struct regmap_range_node
*range
;
2485 if (!map
->bus
|| !map
->bus
->read
)
2488 range
= _regmap_range_lookup(map
, reg
);
2490 ret
= _regmap_select_page(map
, ®
, range
,
2491 noinc
? 1 : val_len
/ map
->format
.val_bytes
);
2496 map
->format
.format_reg(map
->work_buf
, reg
, map
->reg_shift
);
2497 regmap_set_work_buf_flag_mask(map
, map
->format
.reg_bytes
,
2498 map
->read_flag_mask
);
2499 trace_regmap_hw_read_start(map
, reg
, val_len
/ map
->format
.val_bytes
);
2501 ret
= map
->bus
->read(map
->bus_context
, map
->work_buf
,
2502 map
->format
.reg_bytes
+ map
->format
.pad_bytes
,
2505 trace_regmap_hw_read_done(map
, reg
, val_len
/ map
->format
.val_bytes
);
2510 static int _regmap_bus_reg_read(void *context
, unsigned int reg
,
2513 struct regmap
*map
= context
;
2515 return map
->bus
->reg_read(map
->bus_context
, reg
, val
);
2518 static int _regmap_bus_read(void *context
, unsigned int reg
,
2522 struct regmap
*map
= context
;
2523 void *work_val
= map
->work_buf
+ map
->format
.reg_bytes
+
2524 map
->format
.pad_bytes
;
2526 if (!map
->format
.parse_val
)
2529 ret
= _regmap_raw_read(map
, reg
, work_val
, map
->format
.val_bytes
, false);
2531 *val
= map
->format
.parse_val(work_val
);
2536 static int _regmap_read(struct regmap
*map
, unsigned int reg
,
2540 void *context
= _regmap_map_get_context(map
);
2542 if (!map
->cache_bypass
) {
2543 ret
= regcache_read(map
, reg
, val
);
2548 if (map
->cache_only
)
2551 if (!regmap_readable(map
, reg
))
2554 ret
= map
->reg_read(context
, reg
, val
);
2556 if (regmap_should_log(map
))
2557 dev_info(map
->dev
, "%x => %x\n", reg
, *val
);
2559 trace_regmap_reg_read(map
, reg
, *val
);
2561 if (!map
->cache_bypass
)
2562 regcache_write(map
, reg
, *val
);
2569 * regmap_read() - Read a value from a single register
2571 * @map: Register map to read from
2572 * @reg: Register to be read from
2573 * @val: Pointer to store read value
2575 * A value of zero will be returned on success, a negative errno will
2576 * be returned in error cases.
2578 int regmap_read(struct regmap
*map
, unsigned int reg
, unsigned int *val
)
2582 if (!IS_ALIGNED(reg
, map
->reg_stride
))
2585 map
->lock(map
->lock_arg
);
2587 ret
= _regmap_read(map
, reg
, val
);
2589 map
->unlock(map
->lock_arg
);
2593 EXPORT_SYMBOL_GPL(regmap_read
);
2596 * regmap_raw_read() - Read raw data from the device
2598 * @map: Register map to read from
2599 * @reg: First register to be read from
2600 * @val: Pointer to store read value
2601 * @val_len: Size of data to read
2603 * A value of zero will be returned on success, a negative errno will
2604 * be returned in error cases.
2606 int regmap_raw_read(struct regmap
*map
, unsigned int reg
, void *val
,
2609 size_t val_bytes
= map
->format
.val_bytes
;
2610 size_t val_count
= val_len
/ val_bytes
;
2616 if (val_len
% map
->format
.val_bytes
)
2618 if (!IS_ALIGNED(reg
, map
->reg_stride
))
2623 map
->lock(map
->lock_arg
);
2625 if (regmap_volatile_range(map
, reg
, val_count
) || map
->cache_bypass
||
2626 map
->cache_type
== REGCACHE_NONE
) {
2627 size_t chunk_count
, chunk_bytes
;
2628 size_t chunk_regs
= val_count
;
2630 if (!map
->bus
->read
) {
2635 if (map
->use_single_read
)
2637 else if (map
->max_raw_read
&& val_len
> map
->max_raw_read
)
2638 chunk_regs
= map
->max_raw_read
/ val_bytes
;
2640 chunk_count
= val_count
/ chunk_regs
;
2641 chunk_bytes
= chunk_regs
* val_bytes
;
2643 /* Read bytes that fit into whole chunks */
2644 for (i
= 0; i
< chunk_count
; i
++) {
2645 ret
= _regmap_raw_read(map
, reg
, val
, chunk_bytes
, false);
2649 reg
+= regmap_get_offset(map
, chunk_regs
);
2651 val_len
-= chunk_bytes
;
2654 /* Read remaining bytes */
2656 ret
= _regmap_raw_read(map
, reg
, val
, val_len
, false);
2661 /* Otherwise go word by word for the cache; should be low
2662 * cost as we expect to hit the cache.
2664 for (i
= 0; i
< val_count
; i
++) {
2665 ret
= _regmap_read(map
, reg
+ regmap_get_offset(map
, i
),
2670 map
->format
.format_val(val
+ (i
* val_bytes
), v
, 0);
2675 map
->unlock(map
->lock_arg
);
2679 EXPORT_SYMBOL_GPL(regmap_raw_read
);
2682 * regmap_noinc_read(): Read data from a register without incrementing the
2685 * @map: Register map to read from
2686 * @reg: Register to read from
2687 * @val: Pointer to data buffer
2688 * @val_len: Length of output buffer in bytes.
2690 * The regmap API usually assumes that bulk bus read operations will read a
2691 * range of registers. Some devices have certain registers for which a read
2692 * operation read will read from an internal FIFO.
2694 * The target register must be volatile but registers after it can be
2695 * completely unrelated cacheable registers.
2697 * This will attempt multiple reads as required to read val_len bytes.
2699 * A value of zero will be returned on success, a negative errno will be
2700 * returned in error cases.
2702 int regmap_noinc_read(struct regmap
*map
, unsigned int reg
,
2703 void *val
, size_t val_len
)
2710 if (!map
->bus
->read
)
2712 if (val_len
% map
->format
.val_bytes
)
2714 if (!IS_ALIGNED(reg
, map
->reg_stride
))
2719 map
->lock(map
->lock_arg
);
2721 if (!regmap_volatile(map
, reg
) || !regmap_readable_noinc(map
, reg
)) {
2727 if (map
->max_raw_read
&& map
->max_raw_read
< val_len
)
2728 read_len
= map
->max_raw_read
;
2731 ret
= _regmap_raw_read(map
, reg
, val
, read_len
, true);
2734 val
= ((u8
*)val
) + read_len
;
2735 val_len
-= read_len
;
2739 map
->unlock(map
->lock_arg
);
2742 EXPORT_SYMBOL_GPL(regmap_noinc_read
);
2745 * regmap_field_read(): Read a value to a single register field
2747 * @field: Register field to read from
2748 * @val: Pointer to store read value
2750 * A value of zero will be returned on success, a negative errno will
2751 * be returned in error cases.
2753 int regmap_field_read(struct regmap_field
*field
, unsigned int *val
)
2756 unsigned int reg_val
;
2757 ret
= regmap_read(field
->regmap
, field
->reg
, ®_val
);
2761 reg_val
&= field
->mask
;
2762 reg_val
>>= field
->shift
;
2767 EXPORT_SYMBOL_GPL(regmap_field_read
);
2770 * regmap_fields_read() - Read a value to a single register field with port ID
2772 * @field: Register field to read from
2774 * @val: Pointer to store read value
2776 * A value of zero will be returned on success, a negative errno will
2777 * be returned in error cases.
2779 int regmap_fields_read(struct regmap_field
*field
, unsigned int id
,
2783 unsigned int reg_val
;
2785 if (id
>= field
->id_size
)
2788 ret
= regmap_read(field
->regmap
,
2789 field
->reg
+ (field
->id_offset
* id
),
2794 reg_val
&= field
->mask
;
2795 reg_val
>>= field
->shift
;
2800 EXPORT_SYMBOL_GPL(regmap_fields_read
);
2803 * regmap_bulk_read() - Read multiple registers from the device
2805 * @map: Register map to read from
2806 * @reg: First register to be read from
2807 * @val: Pointer to store read value, in native register size for device
2808 * @val_count: Number of registers to read
2810 * A value of zero will be returned on success, a negative errno will
2811 * be returned in error cases.
2813 int regmap_bulk_read(struct regmap
*map
, unsigned int reg
, void *val
,
2817 size_t val_bytes
= map
->format
.val_bytes
;
2818 bool vol
= regmap_volatile_range(map
, reg
, val_count
);
2820 if (!IS_ALIGNED(reg
, map
->reg_stride
))
2825 if (map
->bus
&& map
->format
.parse_inplace
&& (vol
|| map
->cache_type
== REGCACHE_NONE
)) {
2826 ret
= regmap_raw_read(map
, reg
, val
, val_bytes
* val_count
);
2830 for (i
= 0; i
< val_count
* val_bytes
; i
+= val_bytes
)
2831 map
->format
.parse_inplace(val
+ i
);
2840 map
->lock(map
->lock_arg
);
2842 for (i
= 0; i
< val_count
; i
++) {
2845 ret
= _regmap_read(map
, reg
+ regmap_get_offset(map
, i
),
2850 switch (map
->format
.val_bytes
) {
2872 map
->unlock(map
->lock_arg
);
2877 EXPORT_SYMBOL_GPL(regmap_bulk_read
);
2879 static int _regmap_update_bits(struct regmap
*map
, unsigned int reg
,
2880 unsigned int mask
, unsigned int val
,
2881 bool *change
, bool force_write
)
2884 unsigned int tmp
, orig
;
2889 if (regmap_volatile(map
, reg
) && map
->reg_update_bits
) {
2890 ret
= map
->reg_update_bits(map
->bus_context
, reg
, mask
, val
);
2891 if (ret
== 0 && change
)
2894 ret
= _regmap_read(map
, reg
, &orig
);
2901 if (force_write
|| (tmp
!= orig
)) {
2902 ret
= _regmap_write(map
, reg
, tmp
);
2903 if (ret
== 0 && change
)
2912 * regmap_update_bits_base() - Perform a read/modify/write cycle on a register
2914 * @map: Register map to update
2915 * @reg: Register to update
2916 * @mask: Bitmask to change
2917 * @val: New value for bitmask
2918 * @change: Boolean indicating if a write was done
2919 * @async: Boolean indicating asynchronously
2920 * @force: Boolean indicating use force update
2922 * Perform a read/modify/write cycle on a register map with change, async, force
2927 * With most buses the read must be done synchronously so this is most useful
2928 * for devices with a cache which do not need to interact with the hardware to
2929 * determine the current register value.
2931 * Returns zero for success, a negative number on error.
2933 int regmap_update_bits_base(struct regmap
*map
, unsigned int reg
,
2934 unsigned int mask
, unsigned int val
,
2935 bool *change
, bool async
, bool force
)
2939 map
->lock(map
->lock_arg
);
2943 ret
= _regmap_update_bits(map
, reg
, mask
, val
, change
, force
);
2947 map
->unlock(map
->lock_arg
);
2951 EXPORT_SYMBOL_GPL(regmap_update_bits_base
);
2954 * regmap_test_bits() - Check if all specified bits are set in a register.
2956 * @map: Register map to operate on
2957 * @reg: Register to read from
2958 * @bits: Bits to test
2960 * Returns 0 if at least one of the tested bits is not set, 1 if all tested
2961 * bits are set and a negative error number if the underlying regmap_read()
2964 int regmap_test_bits(struct regmap
*map
, unsigned int reg
, unsigned int bits
)
2966 unsigned int val
, ret
;
2968 ret
= regmap_read(map
, reg
, &val
);
2972 return (val
& bits
) == bits
;
2974 EXPORT_SYMBOL_GPL(regmap_test_bits
);
2976 void regmap_async_complete_cb(struct regmap_async
*async
, int ret
)
2978 struct regmap
*map
= async
->map
;
2981 trace_regmap_async_io_complete(map
);
2983 spin_lock(&map
->async_lock
);
2984 list_move(&async
->list
, &map
->async_free
);
2985 wake
= list_empty(&map
->async_list
);
2988 map
->async_ret
= ret
;
2990 spin_unlock(&map
->async_lock
);
2993 wake_up(&map
->async_waitq
);
2995 EXPORT_SYMBOL_GPL(regmap_async_complete_cb
);
2997 static int regmap_async_is_done(struct regmap
*map
)
2999 unsigned long flags
;
3002 spin_lock_irqsave(&map
->async_lock
, flags
);
3003 ret
= list_empty(&map
->async_list
);
3004 spin_unlock_irqrestore(&map
->async_lock
, flags
);
3010 * regmap_async_complete - Ensure all asynchronous I/O has completed.
3012 * @map: Map to operate on.
3014 * Blocks until any pending asynchronous I/O has completed. Returns
3015 * an error code for any failed I/O operations.
3017 int regmap_async_complete(struct regmap
*map
)
3019 unsigned long flags
;
3022 /* Nothing to do with no async support */
3023 if (!map
->bus
|| !map
->bus
->async_write
)
3026 trace_regmap_async_complete_start(map
);
3028 wait_event(map
->async_waitq
, regmap_async_is_done(map
));
3030 spin_lock_irqsave(&map
->async_lock
, flags
);
3031 ret
= map
->async_ret
;
3033 spin_unlock_irqrestore(&map
->async_lock
, flags
);
3035 trace_regmap_async_complete_done(map
);
3039 EXPORT_SYMBOL_GPL(regmap_async_complete
);
3042 * regmap_register_patch - Register and apply register updates to be applied
3043 * on device initialistion
3045 * @map: Register map to apply updates to.
3046 * @regs: Values to update.
3047 * @num_regs: Number of entries in regs.
3049 * Register a set of register updates to be applied to the device
3050 * whenever the device registers are synchronised with the cache and
3051 * apply them immediately. Typically this is used to apply
3052 * corrections to be applied to the device defaults on startup, such
3053 * as the updates some vendors provide to undocumented registers.
3055 * The caller must ensure that this function cannot be called
3056 * concurrently with either itself or regcache_sync().
3058 int regmap_register_patch(struct regmap
*map
, const struct reg_sequence
*regs
,
3061 struct reg_sequence
*p
;
3065 if (WARN_ONCE(num_regs
<= 0, "invalid registers number (%d)\n",
3069 p
= krealloc(map
->patch
,
3070 sizeof(struct reg_sequence
) * (map
->patch_regs
+ num_regs
),
3073 memcpy(p
+ map
->patch_regs
, regs
, num_regs
* sizeof(*regs
));
3075 map
->patch_regs
+= num_regs
;
3080 map
->lock(map
->lock_arg
);
3082 bypass
= map
->cache_bypass
;
3084 map
->cache_bypass
= true;
3087 ret
= _regmap_multi_reg_write(map
, regs
, num_regs
);
3090 map
->cache_bypass
= bypass
;
3092 map
->unlock(map
->lock_arg
);
3094 regmap_async_complete(map
);
3098 EXPORT_SYMBOL_GPL(regmap_register_patch
);
3101 * regmap_get_val_bytes() - Report the size of a register value
3103 * @map: Register map to operate on.
3105 * Report the size of a register value, mainly intended to for use by
3106 * generic infrastructure built on top of regmap.
3108 int regmap_get_val_bytes(struct regmap
*map
)
3110 if (map
->format
.format_write
)
3113 return map
->format
.val_bytes
;
3115 EXPORT_SYMBOL_GPL(regmap_get_val_bytes
);
3118 * regmap_get_max_register() - Report the max register value
3120 * @map: Register map to operate on.
3122 * Report the max register value, mainly intended to for use by
3123 * generic infrastructure built on top of regmap.
3125 int regmap_get_max_register(struct regmap
*map
)
3127 return map
->max_register
? map
->max_register
: -EINVAL
;
3129 EXPORT_SYMBOL_GPL(regmap_get_max_register
);
3132 * regmap_get_reg_stride() - Report the register address stride
3134 * @map: Register map to operate on.
3136 * Report the register address stride, mainly intended to for use by
3137 * generic infrastructure built on top of regmap.
3139 int regmap_get_reg_stride(struct regmap
*map
)
3141 return map
->reg_stride
;
3143 EXPORT_SYMBOL_GPL(regmap_get_reg_stride
);
3145 int regmap_parse_val(struct regmap
*map
, const void *buf
,
3148 if (!map
->format
.parse_val
)
3151 *val
= map
->format
.parse_val(buf
);
3155 EXPORT_SYMBOL_GPL(regmap_parse_val
);
3157 static int __init
regmap_initcall(void)
3159 regmap_debugfs_initcall();
3163 postcore_initcall(regmap_initcall
);