2 * Register map access API
4 * Copyright 2011 Wolfson Microelectronics plc
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/device.h>
14 #include <linux/slab.h>
15 #include <linux/export.h>
16 #include <linux/mutex.h>
17 #include <linux/err.h>
19 #include <linux/rbtree.h>
20 #include <linux/sched.h>
21 #include <linux/delay.h>
22 #include <linux/log2.h>
23 #include <linux/hwspinlock.h>
25 #define CREATE_TRACE_POINTS
31 * Sometimes for failures during very early init the trace
32 * infrastructure isn't available early enough to be used. For this
33 * sort of problem defining LOG_DEVICE will add printks for basic
34 * register I/O on a specific device.
38 static int _regmap_update_bits(struct regmap *map, unsigned int reg,
39 unsigned int mask, unsigned int val,
40 bool *change, bool force_write);
42 static int _regmap_bus_reg_read(void *context, unsigned int reg,
44 static int _regmap_bus_read(void *context, unsigned int reg,
46 static int _regmap_bus_formatted_write(void *context, unsigned int reg,
48 static int _regmap_bus_reg_write(void *context, unsigned int reg,
50 static int _regmap_bus_raw_write(void *context, unsigned int reg,
53 bool regmap_reg_in_ranges(unsigned int reg,
54 const struct regmap_range *ranges,
57 const struct regmap_range *r;
60 for (i = 0, r = ranges; i < nranges; i++, r++)
61 if (regmap_reg_in_range(reg, r))
65 EXPORT_SYMBOL_GPL(regmap_reg_in_ranges);
67 bool regmap_check_range_table(struct regmap *map, unsigned int reg,
68 const struct regmap_access_table *table)
70 /* Check "no ranges" first */
71 if (regmap_reg_in_ranges(reg, table->no_ranges, table->n_no_ranges))
74 /* In case zero "yes ranges" are supplied, any reg is OK */
75 if (!table->n_yes_ranges)
78 return regmap_reg_in_ranges(reg, table->yes_ranges,
81 EXPORT_SYMBOL_GPL(regmap_check_range_table);
83 bool regmap_writeable(struct regmap *map, unsigned int reg)
85 if (map->max_register && reg > map->max_register)
88 if (map->writeable_reg)
89 return map->writeable_reg(map->dev, reg);
92 return regmap_check_range_table(map, reg, map->wr_table);
97 bool regmap_cached(struct regmap *map, unsigned int reg)
102 if (map->cache_type == REGCACHE_NONE)
108 if (map->max_register && reg > map->max_register)
111 map->lock(map->lock_arg);
112 ret = regcache_read(map, reg, &val);
113 map->unlock(map->lock_arg);
120 bool regmap_readable(struct regmap *map, unsigned int reg)
125 if (map->max_register && reg > map->max_register)
128 if (map->format.format_write)
131 if (map->readable_reg)
132 return map->readable_reg(map->dev, reg);
135 return regmap_check_range_table(map, reg, map->rd_table);
140 bool regmap_volatile(struct regmap *map, unsigned int reg)
142 if (!map->format.format_write && !regmap_readable(map, reg))
145 if (map->volatile_reg)
146 return map->volatile_reg(map->dev, reg);
148 if (map->volatile_table)
149 return regmap_check_range_table(map, reg, map->volatile_table);
157 bool regmap_precious(struct regmap *map, unsigned int reg)
159 if (!regmap_readable(map, reg))
162 if (map->precious_reg)
163 return map->precious_reg(map->dev, reg);
165 if (map->precious_table)
166 return regmap_check_range_table(map, reg, map->precious_table);
171 bool regmap_readable_noinc(struct regmap *map, unsigned int reg)
173 if (map->readable_noinc_reg)
174 return map->readable_noinc_reg(map->dev, reg);
176 if (map->rd_noinc_table)
177 return regmap_check_range_table(map, reg, map->rd_noinc_table);
182 static bool regmap_volatile_range(struct regmap *map, unsigned int reg,
187 for (i = 0; i < num; i++)
188 if (!regmap_volatile(map, reg + regmap_get_offset(map, i)))
194 static void regmap_format_2_6_write(struct regmap *map,
195 unsigned int reg, unsigned int val)
197 u8 *out = map->work_buf;
199 *out = (reg << 6) | val;
202 static void regmap_format_4_12_write(struct regmap *map,
203 unsigned int reg, unsigned int val)
205 __be16 *out = map->work_buf;
206 *out = cpu_to_be16((reg << 12) | val);
209 static void regmap_format_7_9_write(struct regmap *map,
210 unsigned int reg, unsigned int val)
212 __be16 *out = map->work_buf;
213 *out = cpu_to_be16((reg << 9) | val);
216 static void regmap_format_10_14_write(struct regmap *map,
217 unsigned int reg, unsigned int val)
219 u8 *out = map->work_buf;
222 out[1] = (val >> 8) | (reg << 6);
226 static void regmap_format_8(void *buf, unsigned int val, unsigned int shift)
233 static void regmap_format_16_be(void *buf, unsigned int val, unsigned int shift)
237 b[0] = cpu_to_be16(val << shift);
240 static void regmap_format_16_le(void *buf, unsigned int val, unsigned int shift)
244 b[0] = cpu_to_le16(val << shift);
247 static void regmap_format_16_native(void *buf, unsigned int val,
250 *(u16 *)buf = val << shift;
253 static void regmap_format_24(void *buf, unsigned int val, unsigned int shift)
264 static void regmap_format_32_be(void *buf, unsigned int val, unsigned int shift)
268 b[0] = cpu_to_be32(val << shift);
271 static void regmap_format_32_le(void *buf, unsigned int val, unsigned int shift)
275 b[0] = cpu_to_le32(val << shift);
278 static void regmap_format_32_native(void *buf, unsigned int val,
281 *(u32 *)buf = val << shift;
285 static void regmap_format_64_be(void *buf, unsigned int val, unsigned int shift)
289 b[0] = cpu_to_be64((u64)val << shift);
292 static void regmap_format_64_le(void *buf, unsigned int val, unsigned int shift)
296 b[0] = cpu_to_le64((u64)val << shift);
299 static void regmap_format_64_native(void *buf, unsigned int val,
302 *(u64 *)buf = (u64)val << shift;
306 static void regmap_parse_inplace_noop(void *buf)
310 static unsigned int regmap_parse_8(const void *buf)
317 static unsigned int regmap_parse_16_be(const void *buf)
319 const __be16 *b = buf;
321 return be16_to_cpu(b[0]);
324 static unsigned int regmap_parse_16_le(const void *buf)
326 const __le16 *b = buf;
328 return le16_to_cpu(b[0]);
331 static void regmap_parse_16_be_inplace(void *buf)
335 b[0] = be16_to_cpu(b[0]);
338 static void regmap_parse_16_le_inplace(void *buf)
342 b[0] = le16_to_cpu(b[0]);
345 static unsigned int regmap_parse_16_native(const void *buf)
350 static unsigned int regmap_parse_24(const void *buf)
353 unsigned int ret = b[2];
354 ret |= ((unsigned int)b[1]) << 8;
355 ret |= ((unsigned int)b[0]) << 16;
360 static unsigned int regmap_parse_32_be(const void *buf)
362 const __be32 *b = buf;
364 return be32_to_cpu(b[0]);
367 static unsigned int regmap_parse_32_le(const void *buf)
369 const __le32 *b = buf;
371 return le32_to_cpu(b[0]);
374 static void regmap_parse_32_be_inplace(void *buf)
378 b[0] = be32_to_cpu(b[0]);
381 static void regmap_parse_32_le_inplace(void *buf)
385 b[0] = le32_to_cpu(b[0]);
388 static unsigned int regmap_parse_32_native(const void *buf)
394 static unsigned int regmap_parse_64_be(const void *buf)
396 const __be64 *b = buf;
398 return be64_to_cpu(b[0]);
401 static unsigned int regmap_parse_64_le(const void *buf)
403 const __le64 *b = buf;
405 return le64_to_cpu(b[0]);
408 static void regmap_parse_64_be_inplace(void *buf)
412 b[0] = be64_to_cpu(b[0]);
415 static void regmap_parse_64_le_inplace(void *buf)
419 b[0] = le64_to_cpu(b[0]);
422 static unsigned int regmap_parse_64_native(const void *buf)
428 static void regmap_lock_hwlock(void *__map)
430 struct regmap *map = __map;
432 hwspin_lock_timeout(map->hwlock, UINT_MAX);
435 static void regmap_lock_hwlock_irq(void *__map)
437 struct regmap *map = __map;
439 hwspin_lock_timeout_irq(map->hwlock, UINT_MAX);
442 static void regmap_lock_hwlock_irqsave(void *__map)
444 struct regmap *map = __map;
446 hwspin_lock_timeout_irqsave(map->hwlock, UINT_MAX,
447 &map->spinlock_flags);
450 static void regmap_unlock_hwlock(void *__map)
452 struct regmap *map = __map;
454 hwspin_unlock(map->hwlock);
457 static void regmap_unlock_hwlock_irq(void *__map)
459 struct regmap *map = __map;
461 hwspin_unlock_irq(map->hwlock);
464 static void regmap_unlock_hwlock_irqrestore(void *__map)
466 struct regmap *map = __map;
468 hwspin_unlock_irqrestore(map->hwlock, &map->spinlock_flags);
471 static void regmap_lock_unlock_none(void *__map)
476 static void regmap_lock_mutex(void *__map)
478 struct regmap *map = __map;
479 mutex_lock(&map->mutex);
482 static void regmap_unlock_mutex(void *__map)
484 struct regmap *map = __map;
485 mutex_unlock(&map->mutex);
488 static void regmap_lock_spinlock(void *__map)
489 __acquires(&map->spinlock)
491 struct regmap *map = __map;
494 spin_lock_irqsave(&map->spinlock, flags);
495 map->spinlock_flags = flags;
498 static void regmap_unlock_spinlock(void *__map)
499 __releases(&map->spinlock)
501 struct regmap *map = __map;
502 spin_unlock_irqrestore(&map->spinlock, map->spinlock_flags);
505 static void dev_get_regmap_release(struct device *dev, void *res)
508 * We don't actually have anything to do here; the goal here
509 * is not to manage the regmap but to provide a simple way to
510 * get the regmap back given a struct device.
514 static bool _regmap_range_add(struct regmap *map,
515 struct regmap_range_node *data)
517 struct rb_root *root = &map->range_tree;
518 struct rb_node **new = &(root->rb_node), *parent = NULL;
521 struct regmap_range_node *this =
522 rb_entry(*new, struct regmap_range_node, node);
525 if (data->range_max < this->range_min)
526 new = &((*new)->rb_left);
527 else if (data->range_min > this->range_max)
528 new = &((*new)->rb_right);
533 rb_link_node(&data->node, parent, new);
534 rb_insert_color(&data->node, root);
539 static struct regmap_range_node *_regmap_range_lookup(struct regmap *map,
542 struct rb_node *node = map->range_tree.rb_node;
545 struct regmap_range_node *this =
546 rb_entry(node, struct regmap_range_node, node);
548 if (reg < this->range_min)
549 node = node->rb_left;
550 else if (reg > this->range_max)
551 node = node->rb_right;
559 static void regmap_range_exit(struct regmap *map)
561 struct rb_node *next;
562 struct regmap_range_node *range_node;
564 next = rb_first(&map->range_tree);
566 range_node = rb_entry(next, struct regmap_range_node, node);
567 next = rb_next(&range_node->node);
568 rb_erase(&range_node->node, &map->range_tree);
572 kfree(map->selector_work_buf);
575 int regmap_attach_dev(struct device *dev, struct regmap *map,
576 const struct regmap_config *config)
582 regmap_debugfs_init(map, config->name);
584 /* Add a devres resource for dev_get_regmap() */
585 m = devres_alloc(dev_get_regmap_release, sizeof(*m), GFP_KERNEL);
587 regmap_debugfs_exit(map);
595 EXPORT_SYMBOL_GPL(regmap_attach_dev);
597 static enum regmap_endian regmap_get_reg_endian(const struct regmap_bus *bus,
598 const struct regmap_config *config)
600 enum regmap_endian endian;
602 /* Retrieve the endianness specification from the regmap config */
603 endian = config->reg_format_endian;
605 /* If the regmap config specified a non-default value, use that */
606 if (endian != REGMAP_ENDIAN_DEFAULT)
609 /* Retrieve the endianness specification from the bus config */
610 if (bus && bus->reg_format_endian_default)
611 endian = bus->reg_format_endian_default;
613 /* If the bus specified a non-default value, use that */
614 if (endian != REGMAP_ENDIAN_DEFAULT)
617 /* Use this if no other value was found */
618 return REGMAP_ENDIAN_BIG;
621 enum regmap_endian regmap_get_val_endian(struct device *dev,
622 const struct regmap_bus *bus,
623 const struct regmap_config *config)
625 struct device_node *np;
626 enum regmap_endian endian;
628 /* Retrieve the endianness specification from the regmap config */
629 endian = config->val_format_endian;
631 /* If the regmap config specified a non-default value, use that */
632 if (endian != REGMAP_ENDIAN_DEFAULT)
635 /* If the dev and dev->of_node exist try to get endianness from DT */
636 if (dev && dev->of_node) {
639 /* Parse the device's DT node for an endianness specification */
640 if (of_property_read_bool(np, "big-endian"))
641 endian = REGMAP_ENDIAN_BIG;
642 else if (of_property_read_bool(np, "little-endian"))
643 endian = REGMAP_ENDIAN_LITTLE;
644 else if (of_property_read_bool(np, "native-endian"))
645 endian = REGMAP_ENDIAN_NATIVE;
647 /* If the endianness was specified in DT, use that */
648 if (endian != REGMAP_ENDIAN_DEFAULT)
652 /* Retrieve the endianness specification from the bus config */
653 if (bus && bus->val_format_endian_default)
654 endian = bus->val_format_endian_default;
656 /* If the bus specified a non-default value, use that */
657 if (endian != REGMAP_ENDIAN_DEFAULT)
660 /* Use this if no other value was found */
661 return REGMAP_ENDIAN_BIG;
663 EXPORT_SYMBOL_GPL(regmap_get_val_endian);
665 struct regmap *__regmap_init(struct device *dev,
666 const struct regmap_bus *bus,
668 const struct regmap_config *config,
669 struct lock_class_key *lock_key,
670 const char *lock_name)
674 enum regmap_endian reg_endian, val_endian;
680 map = kzalloc(sizeof(*map), GFP_KERNEL);
687 map->name = kstrdup_const(config->name, GFP_KERNEL);
694 if (config->disable_locking) {
695 map->lock = map->unlock = regmap_lock_unlock_none;
696 regmap_debugfs_disable(map);
697 } else if (config->lock && config->unlock) {
698 map->lock = config->lock;
699 map->unlock = config->unlock;
700 map->lock_arg = config->lock_arg;
701 } else if (config->use_hwlock) {
702 map->hwlock = hwspin_lock_request_specific(config->hwlock_id);
708 switch (config->hwlock_mode) {
709 case HWLOCK_IRQSTATE:
710 map->lock = regmap_lock_hwlock_irqsave;
711 map->unlock = regmap_unlock_hwlock_irqrestore;
714 map->lock = regmap_lock_hwlock_irq;
715 map->unlock = regmap_unlock_hwlock_irq;
718 map->lock = regmap_lock_hwlock;
719 map->unlock = regmap_unlock_hwlock;
725 if ((bus && bus->fast_io) ||
727 spin_lock_init(&map->spinlock);
728 map->lock = regmap_lock_spinlock;
729 map->unlock = regmap_unlock_spinlock;
730 lockdep_set_class_and_name(&map->spinlock,
731 lock_key, lock_name);
733 mutex_init(&map->mutex);
734 map->lock = regmap_lock_mutex;
735 map->unlock = regmap_unlock_mutex;
736 lockdep_set_class_and_name(&map->mutex,
737 lock_key, lock_name);
743 * When we write in fast-paths with regmap_bulk_write() don't allocate
744 * scratch buffers with sleeping allocations.
746 if ((bus && bus->fast_io) || config->fast_io)
747 map->alloc_flags = GFP_ATOMIC;
749 map->alloc_flags = GFP_KERNEL;
751 map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8);
752 map->format.pad_bytes = config->pad_bits / 8;
753 map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8);
754 map->format.buf_size = DIV_ROUND_UP(config->reg_bits +
755 config->val_bits + config->pad_bits, 8);
756 map->reg_shift = config->pad_bits % 8;
757 if (config->reg_stride)
758 map->reg_stride = config->reg_stride;
761 if (is_power_of_2(map->reg_stride))
762 map->reg_stride_order = ilog2(map->reg_stride);
764 map->reg_stride_order = -1;
765 map->use_single_read = config->use_single_rw || !bus || !bus->read;
766 map->use_single_write = config->use_single_rw || !bus || !bus->write;
767 map->can_multi_write = config->can_multi_write && bus && bus->write;
769 map->max_raw_read = bus->max_raw_read;
770 map->max_raw_write = bus->max_raw_write;
774 map->bus_context = bus_context;
775 map->max_register = config->max_register;
776 map->wr_table = config->wr_table;
777 map->rd_table = config->rd_table;
778 map->volatile_table = config->volatile_table;
779 map->precious_table = config->precious_table;
780 map->rd_noinc_table = config->rd_noinc_table;
781 map->writeable_reg = config->writeable_reg;
782 map->readable_reg = config->readable_reg;
783 map->volatile_reg = config->volatile_reg;
784 map->precious_reg = config->precious_reg;
785 map->readable_noinc_reg = config->readable_noinc_reg;
786 map->cache_type = config->cache_type;
788 spin_lock_init(&map->async_lock);
789 INIT_LIST_HEAD(&map->async_list);
790 INIT_LIST_HEAD(&map->async_free);
791 init_waitqueue_head(&map->async_waitq);
793 if (config->read_flag_mask ||
794 config->write_flag_mask ||
795 config->zero_flag_mask) {
796 map->read_flag_mask = config->read_flag_mask;
797 map->write_flag_mask = config->write_flag_mask;
799 map->read_flag_mask = bus->read_flag_mask;
803 map->reg_read = config->reg_read;
804 map->reg_write = config->reg_write;
806 map->defer_caching = false;
807 goto skip_format_initialization;
808 } else if (!bus->read || !bus->write) {
809 map->reg_read = _regmap_bus_reg_read;
810 map->reg_write = _regmap_bus_reg_write;
812 map->defer_caching = false;
813 goto skip_format_initialization;
815 map->reg_read = _regmap_bus_read;
816 map->reg_update_bits = bus->reg_update_bits;
819 reg_endian = regmap_get_reg_endian(bus, config);
820 val_endian = regmap_get_val_endian(dev, bus, config);
822 switch (config->reg_bits + map->reg_shift) {
824 switch (config->val_bits) {
826 map->format.format_write = regmap_format_2_6_write;
834 switch (config->val_bits) {
836 map->format.format_write = regmap_format_4_12_write;
844 switch (config->val_bits) {
846 map->format.format_write = regmap_format_7_9_write;
854 switch (config->val_bits) {
856 map->format.format_write = regmap_format_10_14_write;
864 map->format.format_reg = regmap_format_8;
868 switch (reg_endian) {
869 case REGMAP_ENDIAN_BIG:
870 map->format.format_reg = regmap_format_16_be;
872 case REGMAP_ENDIAN_LITTLE:
873 map->format.format_reg = regmap_format_16_le;
875 case REGMAP_ENDIAN_NATIVE:
876 map->format.format_reg = regmap_format_16_native;
884 if (reg_endian != REGMAP_ENDIAN_BIG)
886 map->format.format_reg = regmap_format_24;
890 switch (reg_endian) {
891 case REGMAP_ENDIAN_BIG:
892 map->format.format_reg = regmap_format_32_be;
894 case REGMAP_ENDIAN_LITTLE:
895 map->format.format_reg = regmap_format_32_le;
897 case REGMAP_ENDIAN_NATIVE:
898 map->format.format_reg = regmap_format_32_native;
907 switch (reg_endian) {
908 case REGMAP_ENDIAN_BIG:
909 map->format.format_reg = regmap_format_64_be;
911 case REGMAP_ENDIAN_LITTLE:
912 map->format.format_reg = regmap_format_64_le;
914 case REGMAP_ENDIAN_NATIVE:
915 map->format.format_reg = regmap_format_64_native;
927 if (val_endian == REGMAP_ENDIAN_NATIVE)
928 map->format.parse_inplace = regmap_parse_inplace_noop;
930 switch (config->val_bits) {
932 map->format.format_val = regmap_format_8;
933 map->format.parse_val = regmap_parse_8;
934 map->format.parse_inplace = regmap_parse_inplace_noop;
937 switch (val_endian) {
938 case REGMAP_ENDIAN_BIG:
939 map->format.format_val = regmap_format_16_be;
940 map->format.parse_val = regmap_parse_16_be;
941 map->format.parse_inplace = regmap_parse_16_be_inplace;
943 case REGMAP_ENDIAN_LITTLE:
944 map->format.format_val = regmap_format_16_le;
945 map->format.parse_val = regmap_parse_16_le;
946 map->format.parse_inplace = regmap_parse_16_le_inplace;
948 case REGMAP_ENDIAN_NATIVE:
949 map->format.format_val = regmap_format_16_native;
950 map->format.parse_val = regmap_parse_16_native;
957 if (val_endian != REGMAP_ENDIAN_BIG)
959 map->format.format_val = regmap_format_24;
960 map->format.parse_val = regmap_parse_24;
963 switch (val_endian) {
964 case REGMAP_ENDIAN_BIG:
965 map->format.format_val = regmap_format_32_be;
966 map->format.parse_val = regmap_parse_32_be;
967 map->format.parse_inplace = regmap_parse_32_be_inplace;
969 case REGMAP_ENDIAN_LITTLE:
970 map->format.format_val = regmap_format_32_le;
971 map->format.parse_val = regmap_parse_32_le;
972 map->format.parse_inplace = regmap_parse_32_le_inplace;
974 case REGMAP_ENDIAN_NATIVE:
975 map->format.format_val = regmap_format_32_native;
976 map->format.parse_val = regmap_parse_32_native;
984 switch (val_endian) {
985 case REGMAP_ENDIAN_BIG:
986 map->format.format_val = regmap_format_64_be;
987 map->format.parse_val = regmap_parse_64_be;
988 map->format.parse_inplace = regmap_parse_64_be_inplace;
990 case REGMAP_ENDIAN_LITTLE:
991 map->format.format_val = regmap_format_64_le;
992 map->format.parse_val = regmap_parse_64_le;
993 map->format.parse_inplace = regmap_parse_64_le_inplace;
995 case REGMAP_ENDIAN_NATIVE:
996 map->format.format_val = regmap_format_64_native;
997 map->format.parse_val = regmap_parse_64_native;
1006 if (map->format.format_write) {
1007 if ((reg_endian != REGMAP_ENDIAN_BIG) ||
1008 (val_endian != REGMAP_ENDIAN_BIG))
1010 map->use_single_write = true;
1013 if (!map->format.format_write &&
1014 !(map->format.format_reg && map->format.format_val))
1017 map->work_buf = kzalloc(map->format.buf_size, GFP_KERNEL);
1018 if (map->work_buf == NULL) {
1023 if (map->format.format_write) {
1024 map->defer_caching = false;
1025 map->reg_write = _regmap_bus_formatted_write;
1026 } else if (map->format.format_val) {
1027 map->defer_caching = true;
1028 map->reg_write = _regmap_bus_raw_write;
1031 skip_format_initialization:
1033 map->range_tree = RB_ROOT;
1034 for (i = 0; i < config->num_ranges; i++) {
1035 const struct regmap_range_cfg *range_cfg = &config->ranges[i];
1036 struct regmap_range_node *new;
1039 if (range_cfg->range_max < range_cfg->range_min) {
1040 dev_err(map->dev, "Invalid range %d: %d < %d\n", i,
1041 range_cfg->range_max, range_cfg->range_min);
1045 if (range_cfg->range_max > map->max_register) {
1046 dev_err(map->dev, "Invalid range %d: %d > %d\n", i,
1047 range_cfg->range_max, map->max_register);
1051 if (range_cfg->selector_reg > map->max_register) {
1053 "Invalid range %d: selector out of map\n", i);
1057 if (range_cfg->window_len == 0) {
1058 dev_err(map->dev, "Invalid range %d: window_len 0\n",
1063 /* Make sure, that this register range has no selector
1064 or data window within its boundary */
1065 for (j = 0; j < config->num_ranges; j++) {
1066 unsigned sel_reg = config->ranges[j].selector_reg;
1067 unsigned win_min = config->ranges[j].window_start;
1068 unsigned win_max = win_min +
1069 config->ranges[j].window_len - 1;
1071 /* Allow data window inside its own virtual range */
1075 if (range_cfg->range_min <= sel_reg &&
1076 sel_reg <= range_cfg->range_max) {
1078 "Range %d: selector for %d in window\n",
1083 if (!(win_max < range_cfg->range_min ||
1084 win_min > range_cfg->range_max)) {
1086 "Range %d: window for %d in window\n",
1092 new = kzalloc(sizeof(*new), GFP_KERNEL);
1099 new->name = range_cfg->name;
1100 new->range_min = range_cfg->range_min;
1101 new->range_max = range_cfg->range_max;
1102 new->selector_reg = range_cfg->selector_reg;
1103 new->selector_mask = range_cfg->selector_mask;
1104 new->selector_shift = range_cfg->selector_shift;
1105 new->window_start = range_cfg->window_start;
1106 new->window_len = range_cfg->window_len;
1108 if (!_regmap_range_add(map, new)) {
1109 dev_err(map->dev, "Failed to add range %d\n", i);
1114 if (map->selector_work_buf == NULL) {
1115 map->selector_work_buf =
1116 kzalloc(map->format.buf_size, GFP_KERNEL);
1117 if (map->selector_work_buf == NULL) {
1124 ret = regcache_init(map, config);
1129 ret = regmap_attach_dev(dev, map, config);
1133 regmap_debugfs_init(map, config->name);
1141 regmap_range_exit(map);
1142 kfree(map->work_buf);
1145 hwspin_lock_free(map->hwlock);
1147 kfree_const(map->name);
1151 return ERR_PTR(ret);
1153 EXPORT_SYMBOL_GPL(__regmap_init);
1155 static void devm_regmap_release(struct device *dev, void *res)
1157 regmap_exit(*(struct regmap **)res);
1160 struct regmap *__devm_regmap_init(struct device *dev,
1161 const struct regmap_bus *bus,
1163 const struct regmap_config *config,
1164 struct lock_class_key *lock_key,
1165 const char *lock_name)
1167 struct regmap **ptr, *regmap;
1169 ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL);
1171 return ERR_PTR(-ENOMEM);
1173 regmap = __regmap_init(dev, bus, bus_context, config,
1174 lock_key, lock_name);
1175 if (!IS_ERR(regmap)) {
1177 devres_add(dev, ptr);
1184 EXPORT_SYMBOL_GPL(__devm_regmap_init);
1186 static void regmap_field_init(struct regmap_field *rm_field,
1187 struct regmap *regmap, struct reg_field reg_field)
1189 rm_field->regmap = regmap;
1190 rm_field->reg = reg_field.reg;
1191 rm_field->shift = reg_field.lsb;
1192 rm_field->mask = GENMASK(reg_field.msb, reg_field.lsb);
1193 rm_field->id_size = reg_field.id_size;
1194 rm_field->id_offset = reg_field.id_offset;
1198 * devm_regmap_field_alloc() - Allocate and initialise a register field.
1200 * @dev: Device that will be interacted with
1201 * @regmap: regmap bank in which this register field is located.
1202 * @reg_field: Register field with in the bank.
1204 * The return value will be an ERR_PTR() on error or a valid pointer
1205 * to a struct regmap_field. The regmap_field will be automatically freed
1206 * by the device management code.
1208 struct regmap_field *devm_regmap_field_alloc(struct device *dev,
1209 struct regmap *regmap, struct reg_field reg_field)
1211 struct regmap_field *rm_field = devm_kzalloc(dev,
1212 sizeof(*rm_field), GFP_KERNEL);
1214 return ERR_PTR(-ENOMEM);
1216 regmap_field_init(rm_field, regmap, reg_field);
1221 EXPORT_SYMBOL_GPL(devm_regmap_field_alloc);
1224 * devm_regmap_field_free() - Free a register field allocated using
1225 * devm_regmap_field_alloc.
1227 * @dev: Device that will be interacted with
1228 * @field: regmap field which should be freed.
1230 * Free register field allocated using devm_regmap_field_alloc(). Usually
1231 * drivers need not call this function, as the memory allocated via devm
1232 * will be freed as per device-driver life-cyle.
1234 void devm_regmap_field_free(struct device *dev,
1235 struct regmap_field *field)
1237 devm_kfree(dev, field);
1239 EXPORT_SYMBOL_GPL(devm_regmap_field_free);
1242 * regmap_field_alloc() - Allocate and initialise a register field.
1244 * @regmap: regmap bank in which this register field is located.
1245 * @reg_field: Register field with in the bank.
1247 * The return value will be an ERR_PTR() on error or a valid pointer
1248 * to a struct regmap_field. The regmap_field should be freed by the
1249 * user once its finished working with it using regmap_field_free().
1251 struct regmap_field *regmap_field_alloc(struct regmap *regmap,
1252 struct reg_field reg_field)
1254 struct regmap_field *rm_field = kzalloc(sizeof(*rm_field), GFP_KERNEL);
1257 return ERR_PTR(-ENOMEM);
1259 regmap_field_init(rm_field, regmap, reg_field);
1263 EXPORT_SYMBOL_GPL(regmap_field_alloc);
1266 * regmap_field_free() - Free register field allocated using
1267 * regmap_field_alloc.
1269 * @field: regmap field which should be freed.
1271 void regmap_field_free(struct regmap_field *field)
1275 EXPORT_SYMBOL_GPL(regmap_field_free);
1278 * regmap_reinit_cache() - Reinitialise the current register cache
1280 * @map: Register map to operate on.
1281 * @config: New configuration. Only the cache data will be used.
1283 * Discard any existing register cache for the map and initialize a
1284 * new cache. This can be used to restore the cache to defaults or to
1285 * update the cache configuration to reflect runtime discovery of the
1288 * No explicit locking is done here, the user needs to ensure that
1289 * this function will not race with other calls to regmap.
1291 int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
1294 regmap_debugfs_exit(map);
1296 map->max_register = config->max_register;
1297 map->writeable_reg = config->writeable_reg;
1298 map->readable_reg = config->readable_reg;
1299 map->volatile_reg = config->volatile_reg;
1300 map->precious_reg = config->precious_reg;
1301 map->readable_noinc_reg = config->readable_noinc_reg;
1302 map->cache_type = config->cache_type;
1304 regmap_debugfs_init(map, config->name);
1306 map->cache_bypass = false;
1307 map->cache_only = false;
1309 return regcache_init(map, config);
1311 EXPORT_SYMBOL_GPL(regmap_reinit_cache);
1314 * regmap_exit() - Free a previously allocated register map
1316 * @map: Register map to operate on.
1318 void regmap_exit(struct regmap *map)
1320 struct regmap_async *async;
1323 regmap_debugfs_exit(map);
1324 regmap_range_exit(map);
1325 if (map->bus && map->bus->free_context)
1326 map->bus->free_context(map->bus_context);
1327 kfree(map->work_buf);
1328 while (!list_empty(&map->async_free)) {
1329 async = list_first_entry_or_null(&map->async_free,
1330 struct regmap_async,
1332 list_del(&async->list);
1333 kfree(async->work_buf);
1337 hwspin_lock_free(map->hwlock);
1338 kfree_const(map->name);
1341 EXPORT_SYMBOL_GPL(regmap_exit);
1343 static int dev_get_regmap_match(struct device *dev, void *res, void *data)
1345 struct regmap **r = res;
1351 /* If the user didn't specify a name match any */
1353 return (*r)->name == data;
1359 * dev_get_regmap() - Obtain the regmap (if any) for a device
1361 * @dev: Device to retrieve the map for
1362 * @name: Optional name for the register map, usually NULL.
1364 * Returns the regmap for the device if one is present, or NULL. If
1365 * name is specified then it must match the name specified when
1366 * registering the device, if it is NULL then the first regmap found
1367 * will be used. Devices with multiple register maps are very rare,
1368 * generic code should normally not need to specify a name.
1370 struct regmap *dev_get_regmap(struct device *dev, const char *name)
1372 struct regmap **r = devres_find(dev, dev_get_regmap_release,
1373 dev_get_regmap_match, (void *)name);
1379 EXPORT_SYMBOL_GPL(dev_get_regmap);
1382 * regmap_get_device() - Obtain the device from a regmap
1384 * @map: Register map to operate on.
1386 * Returns the underlying device that the regmap has been created for.
1388 struct device *regmap_get_device(struct regmap *map)
1392 EXPORT_SYMBOL_GPL(regmap_get_device);
1394 static int _regmap_select_page(struct regmap *map, unsigned int *reg,
1395 struct regmap_range_node *range,
1396 unsigned int val_num)
1398 void *orig_work_buf;
1399 unsigned int win_offset;
1400 unsigned int win_page;
1404 win_offset = (*reg - range->range_min) % range->window_len;
1405 win_page = (*reg - range->range_min) / range->window_len;
1408 /* Bulk write shouldn't cross range boundary */
1409 if (*reg + val_num - 1 > range->range_max)
1412 /* ... or single page boundary */
1413 if (val_num > range->window_len - win_offset)
1417 /* It is possible to have selector register inside data window.
1418 In that case, selector register is located on every page and
1419 it needs no page switching, when accessed alone. */
1421 range->window_start + win_offset != range->selector_reg) {
1422 /* Use separate work_buf during page switching */
1423 orig_work_buf = map->work_buf;
1424 map->work_buf = map->selector_work_buf;
1426 ret = _regmap_update_bits(map, range->selector_reg,
1427 range->selector_mask,
1428 win_page << range->selector_shift,
1431 map->work_buf = orig_work_buf;
1437 *reg = range->window_start + win_offset;
1442 static void regmap_set_work_buf_flag_mask(struct regmap *map, int max_bytes,
1448 if (!mask || !map->work_buf)
1451 buf = map->work_buf;
1453 for (i = 0; i < max_bytes; i++)
1454 buf[i] |= (mask >> (8 * i)) & 0xff;
1457 static int _regmap_raw_write_impl(struct regmap *map, unsigned int reg,
1458 const void *val, size_t val_len)
1460 struct regmap_range_node *range;
1461 unsigned long flags;
1462 void *work_val = map->work_buf + map->format.reg_bytes +
1463 map->format.pad_bytes;
1465 int ret = -ENOTSUPP;
1471 /* Check for unwritable registers before we start */
1472 if (map->writeable_reg)
1473 for (i = 0; i < val_len / map->format.val_bytes; i++)
1474 if (!map->writeable_reg(map->dev,
1475 reg + regmap_get_offset(map, i)))
1478 if (!map->cache_bypass && map->format.parse_val) {
1480 int val_bytes = map->format.val_bytes;
1481 for (i = 0; i < val_len / val_bytes; i++) {
1482 ival = map->format.parse_val(val + (i * val_bytes));
1483 ret = regcache_write(map,
1484 reg + regmap_get_offset(map, i),
1488 "Error in caching of register: %x ret: %d\n",
1493 if (map->cache_only) {
1494 map->cache_dirty = true;
1499 range = _regmap_range_lookup(map, reg);
1501 int val_num = val_len / map->format.val_bytes;
1502 int win_offset = (reg - range->range_min) % range->window_len;
1503 int win_residue = range->window_len - win_offset;
1505 /* If the write goes beyond the end of the window split it */
1506 while (val_num > win_residue) {
1507 dev_dbg(map->dev, "Writing window %d/%zu\n",
1508 win_residue, val_len / map->format.val_bytes);
1509 ret = _regmap_raw_write_impl(map, reg, val,
1511 map->format.val_bytes);
1516 val_num -= win_residue;
1517 val += win_residue * map->format.val_bytes;
1518 val_len -= win_residue * map->format.val_bytes;
1520 win_offset = (reg - range->range_min) %
1522 win_residue = range->window_len - win_offset;
1525 ret = _regmap_select_page(map, ®, range, val_num);
1530 map->format.format_reg(map->work_buf, reg, map->reg_shift);
1531 regmap_set_work_buf_flag_mask(map, map->format.reg_bytes,
1532 map->write_flag_mask);
1535 * Essentially all I/O mechanisms will be faster with a single
1536 * buffer to write. Since register syncs often generate raw
1537 * writes of single registers optimise that case.
1539 if (val != work_val && val_len == map->format.val_bytes) {
1540 memcpy(work_val, val, map->format.val_bytes);
1544 if (map->async && map->bus->async_write) {
1545 struct regmap_async *async;
1547 trace_regmap_async_write_start(map, reg, val_len);
1549 spin_lock_irqsave(&map->async_lock, flags);
1550 async = list_first_entry_or_null(&map->async_free,
1551 struct regmap_async,
1554 list_del(&async->list);
1555 spin_unlock_irqrestore(&map->async_lock, flags);
1558 async = map->bus->async_alloc();
1562 async->work_buf = kzalloc(map->format.buf_size,
1563 GFP_KERNEL | GFP_DMA);
1564 if (!async->work_buf) {
1572 /* If the caller supplied the value we can use it safely. */
1573 memcpy(async->work_buf, map->work_buf, map->format.pad_bytes +
1574 map->format.reg_bytes + map->format.val_bytes);
1576 spin_lock_irqsave(&map->async_lock, flags);
1577 list_add_tail(&async->list, &map->async_list);
1578 spin_unlock_irqrestore(&map->async_lock, flags);
1580 if (val != work_val)
1581 ret = map->bus->async_write(map->bus_context,
1583 map->format.reg_bytes +
1584 map->format.pad_bytes,
1585 val, val_len, async);
1587 ret = map->bus->async_write(map->bus_context,
1589 map->format.reg_bytes +
1590 map->format.pad_bytes +
1591 val_len, NULL, 0, async);
1594 dev_err(map->dev, "Failed to schedule write: %d\n",
1597 spin_lock_irqsave(&map->async_lock, flags);
1598 list_move(&async->list, &map->async_free);
1599 spin_unlock_irqrestore(&map->async_lock, flags);
1605 trace_regmap_hw_write_start(map, reg, val_len / map->format.val_bytes);
1607 /* If we're doing a single register write we can probably just
1608 * send the work_buf directly, otherwise try to do a gather
1611 if (val == work_val)
1612 ret = map->bus->write(map->bus_context, map->work_buf,
1613 map->format.reg_bytes +
1614 map->format.pad_bytes +
1616 else if (map->bus->gather_write)
1617 ret = map->bus->gather_write(map->bus_context, map->work_buf,
1618 map->format.reg_bytes +
1619 map->format.pad_bytes,
1622 /* If that didn't work fall back on linearising by hand. */
1623 if (ret == -ENOTSUPP) {
1624 len = map->format.reg_bytes + map->format.pad_bytes + val_len;
1625 buf = kzalloc(len, GFP_KERNEL);
1629 memcpy(buf, map->work_buf, map->format.reg_bytes);
1630 memcpy(buf + map->format.reg_bytes + map->format.pad_bytes,
1632 ret = map->bus->write(map->bus_context, buf, len);
1635 } else if (ret != 0 && !map->cache_bypass && map->format.parse_val) {
1636 /* regcache_drop_region() takes lock that we already have,
1637 * thus call map->cache_ops->drop() directly
1639 if (map->cache_ops && map->cache_ops->drop)
1640 map->cache_ops->drop(map, reg, reg + 1);
1643 trace_regmap_hw_write_done(map, reg, val_len / map->format.val_bytes);
1649 * regmap_can_raw_write - Test if regmap_raw_write() is supported
1651 * @map: Map to check.
1653 bool regmap_can_raw_write(struct regmap *map)
1655 return map->bus && map->bus->write && map->format.format_val &&
1656 map->format.format_reg;
1658 EXPORT_SYMBOL_GPL(regmap_can_raw_write);
1661 * regmap_get_raw_read_max - Get the maximum size we can read
1663 * @map: Map to check.
1665 size_t regmap_get_raw_read_max(struct regmap *map)
1667 return map->max_raw_read;
1669 EXPORT_SYMBOL_GPL(regmap_get_raw_read_max);
1672 * regmap_get_raw_write_max - Get the maximum size we can read
1674 * @map: Map to check.
1676 size_t regmap_get_raw_write_max(struct regmap *map)
1678 return map->max_raw_write;
1680 EXPORT_SYMBOL_GPL(regmap_get_raw_write_max);
1682 static int _regmap_bus_formatted_write(void *context, unsigned int reg,
1686 struct regmap_range_node *range;
1687 struct regmap *map = context;
1689 WARN_ON(!map->bus || !map->format.format_write);
1691 range = _regmap_range_lookup(map, reg);
1693 ret = _regmap_select_page(map, ®, range, 1);
1698 map->format.format_write(map, reg, val);
1700 trace_regmap_hw_write_start(map, reg, 1);
1702 ret = map->bus->write(map->bus_context, map->work_buf,
1703 map->format.buf_size);
1705 trace_regmap_hw_write_done(map, reg, 1);
1710 static int _regmap_bus_reg_write(void *context, unsigned int reg,
1713 struct regmap *map = context;
1715 return map->bus->reg_write(map->bus_context, reg, val);
1718 static int _regmap_bus_raw_write(void *context, unsigned int reg,
1721 struct regmap *map = context;
1723 WARN_ON(!map->bus || !map->format.format_val);
1725 map->format.format_val(map->work_buf + map->format.reg_bytes
1726 + map->format.pad_bytes, val, 0);
1727 return _regmap_raw_write_impl(map, reg,
1729 map->format.reg_bytes +
1730 map->format.pad_bytes,
1731 map->format.val_bytes);
1734 static inline void *_regmap_map_get_context(struct regmap *map)
1736 return (map->bus) ? map : map->bus_context;
1739 int _regmap_write(struct regmap *map, unsigned int reg,
1743 void *context = _regmap_map_get_context(map);
1745 if (!regmap_writeable(map, reg))
1748 if (!map->cache_bypass && !map->defer_caching) {
1749 ret = regcache_write(map, reg, val);
1752 if (map->cache_only) {
1753 map->cache_dirty = true;
1759 if (map->dev && strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
1760 dev_info(map->dev, "%x <= %x\n", reg, val);
1763 trace_regmap_reg_write(map, reg, val);
1765 return map->reg_write(context, reg, val);
1769 * regmap_write() - Write a value to a single register
1771 * @map: Register map to write to
1772 * @reg: Register to write to
1773 * @val: Value to be written
1775 * A value of zero will be returned on success, a negative errno will
1776 * be returned in error cases.
1778 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
1782 if (!IS_ALIGNED(reg, map->reg_stride))
1785 map->lock(map->lock_arg);
1787 ret = _regmap_write(map, reg, val);
1789 map->unlock(map->lock_arg);
1793 EXPORT_SYMBOL_GPL(regmap_write);
1796 * regmap_write_async() - Write a value to a single register asynchronously
1798 * @map: Register map to write to
1799 * @reg: Register to write to
1800 * @val: Value to be written
1802 * A value of zero will be returned on success, a negative errno will
1803 * be returned in error cases.
1805 int regmap_write_async(struct regmap *map, unsigned int reg, unsigned int val)
1809 if (!IS_ALIGNED(reg, map->reg_stride))
1812 map->lock(map->lock_arg);
1816 ret = _regmap_write(map, reg, val);
1820 map->unlock(map->lock_arg);
1824 EXPORT_SYMBOL_GPL(regmap_write_async);
1826 int _regmap_raw_write(struct regmap *map, unsigned int reg,
1827 const void *val, size_t val_len)
1829 size_t val_bytes = map->format.val_bytes;
1830 size_t val_count = val_len / val_bytes;
1831 size_t chunk_count, chunk_bytes;
1832 size_t chunk_regs = val_count;
1838 if (map->use_single_write)
1840 else if (map->max_raw_write && val_len > map->max_raw_write)
1841 chunk_regs = map->max_raw_write / val_bytes;
1843 chunk_count = val_count / chunk_regs;
1844 chunk_bytes = chunk_regs * val_bytes;
1846 /* Write as many bytes as possible with chunk_size */
1847 for (i = 0; i < chunk_count; i++) {
1848 ret = _regmap_raw_write_impl(map, reg, val, chunk_bytes);
1852 reg += regmap_get_offset(map, chunk_regs);
1854 val_len -= chunk_bytes;
1857 /* Write remaining bytes */
1859 ret = _regmap_raw_write_impl(map, reg, val, val_len);
1865 * regmap_raw_write() - Write raw values to one or more registers
1867 * @map: Register map to write to
1868 * @reg: Initial register to write to
1869 * @val: Block of data to be written, laid out for direct transmission to the
1871 * @val_len: Length of data pointed to by val.
1873 * This function is intended to be used for things like firmware
1874 * download where a large block of data needs to be transferred to the
1875 * device. No formatting will be done on the data provided.
1877 * A value of zero will be returned on success, a negative errno will
1878 * be returned in error cases.
1880 int regmap_raw_write(struct regmap *map, unsigned int reg,
1881 const void *val, size_t val_len)
1885 if (!regmap_can_raw_write(map))
1887 if (val_len % map->format.val_bytes)
1890 map->lock(map->lock_arg);
1892 ret = _regmap_raw_write(map, reg, val, val_len);
1894 map->unlock(map->lock_arg);
1898 EXPORT_SYMBOL_GPL(regmap_raw_write);
1901 * regmap_field_update_bits_base() - Perform a read/modify/write cycle a
1904 * @field: Register field to write to
1905 * @mask: Bitmask to change
1906 * @val: Value to be written
1907 * @change: Boolean indicating if a write was done
1908 * @async: Boolean indicating asynchronously
1909 * @force: Boolean indicating use force update
1911 * Perform a read/modify/write cycle on the register field with change,
1912 * async, force option.
1914 * A value of zero will be returned on success, a negative errno will
1915 * be returned in error cases.
1917 int regmap_field_update_bits_base(struct regmap_field *field,
1918 unsigned int mask, unsigned int val,
1919 bool *change, bool async, bool force)
1921 mask = (mask << field->shift) & field->mask;
1923 return regmap_update_bits_base(field->regmap, field->reg,
1924 mask, val << field->shift,
1925 change, async, force);
1927 EXPORT_SYMBOL_GPL(regmap_field_update_bits_base);
1930 * regmap_fields_update_bits_base() - Perform a read/modify/write cycle a
1931 * register field with port ID
1933 * @field: Register field to write to
1935 * @mask: Bitmask to change
1936 * @val: Value to be written
1937 * @change: Boolean indicating if a write was done
1938 * @async: Boolean indicating asynchronously
1939 * @force: Boolean indicating use force update
1941 * A value of zero will be returned on success, a negative errno will
1942 * be returned in error cases.
1944 int regmap_fields_update_bits_base(struct regmap_field *field, unsigned int id,
1945 unsigned int mask, unsigned int val,
1946 bool *change, bool async, bool force)
1948 if (id >= field->id_size)
1951 mask = (mask << field->shift) & field->mask;
1953 return regmap_update_bits_base(field->regmap,
1954 field->reg + (field->id_offset * id),
1955 mask, val << field->shift,
1956 change, async, force);
1958 EXPORT_SYMBOL_GPL(regmap_fields_update_bits_base);
1961 * regmap_bulk_write() - Write multiple registers to the device
1963 * @map: Register map to write to
1964 * @reg: First register to be write from
1965 * @val: Block of data to be written, in native register size for device
1966 * @val_count: Number of registers to write
1968 * This function is intended to be used for writing a large block of
1969 * data to the device either in single transfer or multiple transfer.
1971 * A value of zero will be returned on success, a negative errno will
1972 * be returned in error cases.
1974 int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
1978 size_t val_bytes = map->format.val_bytes;
1980 if (!IS_ALIGNED(reg, map->reg_stride))
1984 * Some devices don't support bulk write, for them we have a series of
1985 * single write operations.
1987 if (!map->bus || !map->format.parse_inplace) {
1988 map->lock(map->lock_arg);
1989 for (i = 0; i < val_count; i++) {
1992 switch (val_bytes) {
1994 ival = *(u8 *)(val + (i * val_bytes));
1997 ival = *(u16 *)(val + (i * val_bytes));
2000 ival = *(u32 *)(val + (i * val_bytes));
2004 ival = *(u64 *)(val + (i * val_bytes));
2012 ret = _regmap_write(map,
2013 reg + regmap_get_offset(map, i),
2019 map->unlock(map->lock_arg);
2023 wval = kmemdup(val, val_count * val_bytes, map->alloc_flags);
2027 for (i = 0; i < val_count * val_bytes; i += val_bytes)
2028 map->format.parse_inplace(wval + i);
2030 ret = regmap_raw_write(map, reg, wval, val_bytes * val_count);
2036 EXPORT_SYMBOL_GPL(regmap_bulk_write);
2039 * _regmap_raw_multi_reg_write()
2041 * the (register,newvalue) pairs in regs have not been formatted, but
2042 * they are all in the same page and have been changed to being page
2043 * relative. The page register has been written if that was necessary.
2045 static int _regmap_raw_multi_reg_write(struct regmap *map,
2046 const struct reg_sequence *regs,
2053 size_t val_bytes = map->format.val_bytes;
2054 size_t reg_bytes = map->format.reg_bytes;
2055 size_t pad_bytes = map->format.pad_bytes;
2056 size_t pair_size = reg_bytes + pad_bytes + val_bytes;
2057 size_t len = pair_size * num_regs;
2062 buf = kzalloc(len, GFP_KERNEL);
2066 /* We have to linearise by hand. */
2070 for (i = 0; i < num_regs; i++) {
2071 unsigned int reg = regs[i].reg;
2072 unsigned int val = regs[i].def;
2073 trace_regmap_hw_write_start(map, reg, 1);
2074 map->format.format_reg(u8, reg, map->reg_shift);
2075 u8 += reg_bytes + pad_bytes;
2076 map->format.format_val(u8, val, 0);
2080 *u8 |= map->write_flag_mask;
2082 ret = map->bus->write(map->bus_context, buf, len);
2086 for (i = 0; i < num_regs; i++) {
2087 int reg = regs[i].reg;
2088 trace_regmap_hw_write_done(map, reg, 1);
2093 static unsigned int _regmap_register_page(struct regmap *map,
2095 struct regmap_range_node *range)
2097 unsigned int win_page = (reg - range->range_min) / range->window_len;
2102 static int _regmap_range_multi_paged_reg_write(struct regmap *map,
2103 struct reg_sequence *regs,
2108 struct reg_sequence *base;
2109 unsigned int this_page = 0;
2110 unsigned int page_change = 0;
2112 * the set of registers are not neccessarily in order, but
2113 * since the order of write must be preserved this algorithm
2114 * chops the set each time the page changes. This also applies
2115 * if there is a delay required at any point in the sequence.
2118 for (i = 0, n = 0; i < num_regs; i++, n++) {
2119 unsigned int reg = regs[i].reg;
2120 struct regmap_range_node *range;
2122 range = _regmap_range_lookup(map, reg);
2124 unsigned int win_page = _regmap_register_page(map, reg,
2128 this_page = win_page;
2129 if (win_page != this_page) {
2130 this_page = win_page;
2135 /* If we have both a page change and a delay make sure to
2136 * write the regs and apply the delay before we change the
2140 if (page_change || regs[i].delay_us) {
2142 /* For situations where the first write requires
2143 * a delay we need to make sure we don't call
2144 * raw_multi_reg_write with n=0
2145 * This can't occur with page breaks as we
2146 * never write on the first iteration
2148 if (regs[i].delay_us && i == 0)
2151 ret = _regmap_raw_multi_reg_write(map, base, n);
2155 if (regs[i].delay_us)
2156 udelay(regs[i].delay_us);
2162 ret = _regmap_select_page(map,
2175 return _regmap_raw_multi_reg_write(map, base, n);
2179 static int _regmap_multi_reg_write(struct regmap *map,
2180 const struct reg_sequence *regs,
2186 if (!map->can_multi_write) {
2187 for (i = 0; i < num_regs; i++) {
2188 ret = _regmap_write(map, regs[i].reg, regs[i].def);
2192 if (regs[i].delay_us)
2193 udelay(regs[i].delay_us);
2198 if (!map->format.parse_inplace)
2201 if (map->writeable_reg)
2202 for (i = 0; i < num_regs; i++) {
2203 int reg = regs[i].reg;
2204 if (!map->writeable_reg(map->dev, reg))
2206 if (!IS_ALIGNED(reg, map->reg_stride))
2210 if (!map->cache_bypass) {
2211 for (i = 0; i < num_regs; i++) {
2212 unsigned int val = regs[i].def;
2213 unsigned int reg = regs[i].reg;
2214 ret = regcache_write(map, reg, val);
2217 "Error in caching of register: %x ret: %d\n",
2222 if (map->cache_only) {
2223 map->cache_dirty = true;
2230 for (i = 0; i < num_regs; i++) {
2231 unsigned int reg = regs[i].reg;
2232 struct regmap_range_node *range;
2234 /* Coalesce all the writes between a page break or a delay
2237 range = _regmap_range_lookup(map, reg);
2238 if (range || regs[i].delay_us) {
2239 size_t len = sizeof(struct reg_sequence)*num_regs;
2240 struct reg_sequence *base = kmemdup(regs, len,
2244 ret = _regmap_range_multi_paged_reg_write(map, base,
2251 return _regmap_raw_multi_reg_write(map, regs, num_regs);
2255 * regmap_multi_reg_write() - Write multiple registers to the device
2257 * @map: Register map to write to
2258 * @regs: Array of structures containing register,value to be written
2259 * @num_regs: Number of registers to write
2261 * Write multiple registers to the device where the set of register, value
2262 * pairs are supplied in any order, possibly not all in a single range.
2264 * The 'normal' block write mode will send ultimately send data on the
2265 * target bus as R,V1,V2,V3,..,Vn where successively higher registers are
2266 * addressed. However, this alternative block multi write mode will send
2267 * the data as R1,V1,R2,V2,..,Rn,Vn on the target bus. The target device
2268 * must of course support the mode.
2270 * A value of zero will be returned on success, a negative errno will be
2271 * returned in error cases.
2273 int regmap_multi_reg_write(struct regmap *map, const struct reg_sequence *regs,
2278 map->lock(map->lock_arg);
2280 ret = _regmap_multi_reg_write(map, regs, num_regs);
2282 map->unlock(map->lock_arg);
2286 EXPORT_SYMBOL_GPL(regmap_multi_reg_write);
2289 * regmap_multi_reg_write_bypassed() - Write multiple registers to the
2290 * device but not the cache
2292 * @map: Register map to write to
2293 * @regs: Array of structures containing register,value to be written
2294 * @num_regs: Number of registers to write
2296 * Write multiple registers to the device but not the cache where the set
2297 * of register are supplied in any order.
2299 * This function is intended to be used for writing a large block of data
2300 * atomically to the device in single transfer for those I2C client devices
2301 * that implement this alternative block write mode.
2303 * A value of zero will be returned on success, a negative errno will
2304 * be returned in error cases.
2306 int regmap_multi_reg_write_bypassed(struct regmap *map,
2307 const struct reg_sequence *regs,
2313 map->lock(map->lock_arg);
2315 bypass = map->cache_bypass;
2316 map->cache_bypass = true;
2318 ret = _regmap_multi_reg_write(map, regs, num_regs);
2320 map->cache_bypass = bypass;
2322 map->unlock(map->lock_arg);
2326 EXPORT_SYMBOL_GPL(regmap_multi_reg_write_bypassed);
2329 * regmap_raw_write_async() - Write raw values to one or more registers
2332 * @map: Register map to write to
2333 * @reg: Initial register to write to
2334 * @val: Block of data to be written, laid out for direct transmission to the
2335 * device. Must be valid until regmap_async_complete() is called.
2336 * @val_len: Length of data pointed to by val.
2338 * This function is intended to be used for things like firmware
2339 * download where a large block of data needs to be transferred to the
2340 * device. No formatting will be done on the data provided.
2342 * If supported by the underlying bus the write will be scheduled
2343 * asynchronously, helping maximise I/O speed on higher speed buses
2344 * like SPI. regmap_async_complete() can be called to ensure that all
2345 * asynchrnous writes have been completed.
2347 * A value of zero will be returned on success, a negative errno will
2348 * be returned in error cases.
2350 int regmap_raw_write_async(struct regmap *map, unsigned int reg,
2351 const void *val, size_t val_len)
2355 if (val_len % map->format.val_bytes)
2357 if (!IS_ALIGNED(reg, map->reg_stride))
2360 map->lock(map->lock_arg);
2364 ret = _regmap_raw_write(map, reg, val, val_len);
2368 map->unlock(map->lock_arg);
2372 EXPORT_SYMBOL_GPL(regmap_raw_write_async);
2374 static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
2375 unsigned int val_len)
2377 struct regmap_range_node *range;
2382 if (!map->bus || !map->bus->read)
2385 range = _regmap_range_lookup(map, reg);
2387 ret = _regmap_select_page(map, ®, range,
2388 val_len / map->format.val_bytes);
2393 map->format.format_reg(map->work_buf, reg, map->reg_shift);
2394 regmap_set_work_buf_flag_mask(map, map->format.reg_bytes,
2395 map->read_flag_mask);
2396 trace_regmap_hw_read_start(map, reg, val_len / map->format.val_bytes);
2398 ret = map->bus->read(map->bus_context, map->work_buf,
2399 map->format.reg_bytes + map->format.pad_bytes,
2402 trace_regmap_hw_read_done(map, reg, val_len / map->format.val_bytes);
2407 static int _regmap_bus_reg_read(void *context, unsigned int reg,
2410 struct regmap *map = context;
2412 return map->bus->reg_read(map->bus_context, reg, val);
2415 static int _regmap_bus_read(void *context, unsigned int reg,
2419 struct regmap *map = context;
2420 void *work_val = map->work_buf + map->format.reg_bytes +
2421 map->format.pad_bytes;
2423 if (!map->format.parse_val)
2426 ret = _regmap_raw_read(map, reg, work_val, map->format.val_bytes);
2428 *val = map->format.parse_val(work_val);
2433 static int _regmap_read(struct regmap *map, unsigned int reg,
2437 void *context = _regmap_map_get_context(map);
2439 if (!map->cache_bypass) {
2440 ret = regcache_read(map, reg, val);
2445 if (map->cache_only)
2448 if (!regmap_readable(map, reg))
2451 ret = map->reg_read(context, reg, val);
2454 if (map->dev && strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
2455 dev_info(map->dev, "%x => %x\n", reg, *val);
2458 trace_regmap_reg_read(map, reg, *val);
2460 if (!map->cache_bypass)
2461 regcache_write(map, reg, *val);
2468 * regmap_read() - Read a value from a single register
2470 * @map: Register map to read from
2471 * @reg: Register to be read from
2472 * @val: Pointer to store read value
2474 * A value of zero will be returned on success, a negative errno will
2475 * be returned in error cases.
2477 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
2481 if (!IS_ALIGNED(reg, map->reg_stride))
2484 map->lock(map->lock_arg);
2486 ret = _regmap_read(map, reg, val);
2488 map->unlock(map->lock_arg);
2492 EXPORT_SYMBOL_GPL(regmap_read);
2495 * regmap_raw_read() - Read raw data from the device
2497 * @map: Register map to read from
2498 * @reg: First register to be read from
2499 * @val: Pointer to store read value
2500 * @val_len: Size of data to read
2502 * A value of zero will be returned on success, a negative errno will
2503 * be returned in error cases.
2505 int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
2508 size_t val_bytes = map->format.val_bytes;
2509 size_t val_count = val_len / val_bytes;
2515 if (val_len % map->format.val_bytes)
2517 if (!IS_ALIGNED(reg, map->reg_stride))
2522 map->lock(map->lock_arg);
2524 if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
2525 map->cache_type == REGCACHE_NONE) {
2526 size_t chunk_count, chunk_bytes;
2527 size_t chunk_regs = val_count;
2529 if (!map->bus->read) {
2534 if (map->use_single_read)
2536 else if (map->max_raw_read && val_len > map->max_raw_read)
2537 chunk_regs = map->max_raw_read / val_bytes;
2539 chunk_count = val_count / chunk_regs;
2540 chunk_bytes = chunk_regs * val_bytes;
2542 /* Read bytes that fit into whole chunks */
2543 for (i = 0; i < chunk_count; i++) {
2544 ret = _regmap_raw_read(map, reg, val, chunk_bytes);
2548 reg += regmap_get_offset(map, chunk_regs);
2550 val_len -= chunk_bytes;
2553 /* Read remaining bytes */
2555 ret = _regmap_raw_read(map, reg, val, val_len);
2560 /* Otherwise go word by word for the cache; should be low
2561 * cost as we expect to hit the cache.
2563 for (i = 0; i < val_count; i++) {
2564 ret = _regmap_read(map, reg + regmap_get_offset(map, i),
2569 map->format.format_val(val + (i * val_bytes), v, 0);
2574 map->unlock(map->lock_arg);
2578 EXPORT_SYMBOL_GPL(regmap_raw_read);
2581 * regmap_noinc_read(): Read data from a register without incrementing the
2584 * @map: Register map to read from
2585 * @reg: Register to read from
2586 * @val: Pointer to data buffer
2587 * @val_len: Length of output buffer in bytes.
2589 * The regmap API usually assumes that bulk bus read operations will read a
2590 * range of registers. Some devices have certain registers for which a read
2591 * operation read will read from an internal FIFO.
2593 * The target register must be volatile but registers after it can be
2594 * completely unrelated cacheable registers.
2596 * This will attempt multiple reads as required to read val_len bytes.
2598 * A value of zero will be returned on success, a negative errno will be
2599 * returned in error cases.
2601 int regmap_noinc_read(struct regmap *map, unsigned int reg,
2602 void *val, size_t val_len)
2609 if (!map->bus->read)
2611 if (val_len % map->format.val_bytes)
2613 if (!IS_ALIGNED(reg, map->reg_stride))
2618 map->lock(map->lock_arg);
2620 if (!regmap_volatile(map, reg) || !regmap_readable_noinc(map, reg)) {
2626 if (map->max_raw_read && map->max_raw_read < val_len)
2627 read_len = map->max_raw_read;
2630 ret = _regmap_raw_read(map, reg, val, read_len);
2633 val = ((u8 *)val) + read_len;
2634 val_len -= read_len;
2638 map->unlock(map->lock_arg);
2641 EXPORT_SYMBOL_GPL(regmap_noinc_read);
2644 * regmap_field_read(): Read a value to a single register field
2646 * @field: Register field to read from
2647 * @val: Pointer to store read value
2649 * A value of zero will be returned on success, a negative errno will
2650 * be returned in error cases.
2652 int regmap_field_read(struct regmap_field *field, unsigned int *val)
2655 unsigned int reg_val;
2656 ret = regmap_read(field->regmap, field->reg, ®_val);
2660 reg_val &= field->mask;
2661 reg_val >>= field->shift;
2666 EXPORT_SYMBOL_GPL(regmap_field_read);
2669 * regmap_fields_read() - Read a value to a single register field with port ID
2671 * @field: Register field to read from
2673 * @val: Pointer to store read value
2675 * A value of zero will be returned on success, a negative errno will
2676 * be returned in error cases.
2678 int regmap_fields_read(struct regmap_field *field, unsigned int id,
2682 unsigned int reg_val;
2684 if (id >= field->id_size)
2687 ret = regmap_read(field->regmap,
2688 field->reg + (field->id_offset * id),
2693 reg_val &= field->mask;
2694 reg_val >>= field->shift;
2699 EXPORT_SYMBOL_GPL(regmap_fields_read);
2702 * regmap_bulk_read() - Read multiple registers from the device
2704 * @map: Register map to read from
2705 * @reg: First register to be read from
2706 * @val: Pointer to store read value, in native register size for device
2707 * @val_count: Number of registers to read
2709 * A value of zero will be returned on success, a negative errno will
2710 * be returned in error cases.
2712 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
2716 size_t val_bytes = map->format.val_bytes;
2717 bool vol = regmap_volatile_range(map, reg, val_count);
2719 if (!IS_ALIGNED(reg, map->reg_stride))
2724 if (map->bus && map->format.parse_inplace && (vol || map->cache_type == REGCACHE_NONE)) {
2725 ret = regmap_raw_read(map, reg, val, val_bytes * val_count);
2729 for (i = 0; i < val_count * val_bytes; i += val_bytes)
2730 map->format.parse_inplace(val + i);
2739 map->lock(map->lock_arg);
2741 for (i = 0; i < val_count; i++) {
2744 ret = _regmap_read(map, reg + regmap_get_offset(map, i),
2749 switch (map->format.val_bytes) {
2771 map->unlock(map->lock_arg);
2776 EXPORT_SYMBOL_GPL(regmap_bulk_read);
2778 static int _regmap_update_bits(struct regmap *map, unsigned int reg,
2779 unsigned int mask, unsigned int val,
2780 bool *change, bool force_write)
2783 unsigned int tmp, orig;
2788 if (regmap_volatile(map, reg) && map->reg_update_bits) {
2789 ret = map->reg_update_bits(map->bus_context, reg, mask, val);
2790 if (ret == 0 && change)
2793 ret = _regmap_read(map, reg, &orig);
2800 if (force_write || (tmp != orig)) {
2801 ret = _regmap_write(map, reg, tmp);
2802 if (ret == 0 && change)
2811 * regmap_update_bits_base() - Perform a read/modify/write cycle on a register
2813 * @map: Register map to update
2814 * @reg: Register to update
2815 * @mask: Bitmask to change
2816 * @val: New value for bitmask
2817 * @change: Boolean indicating if a write was done
2818 * @async: Boolean indicating asynchronously
2819 * @force: Boolean indicating use force update
2821 * Perform a read/modify/write cycle on a register map with change, async, force
2826 * With most buses the read must be done synchronously so this is most useful
2827 * for devices with a cache which do not need to interact with the hardware to
2828 * determine the current register value.
2830 * Returns zero for success, a negative number on error.
2832 int regmap_update_bits_base(struct regmap *map, unsigned int reg,
2833 unsigned int mask, unsigned int val,
2834 bool *change, bool async, bool force)
2838 map->lock(map->lock_arg);
2842 ret = _regmap_update_bits(map, reg, mask, val, change, force);
2846 map->unlock(map->lock_arg);
2850 EXPORT_SYMBOL_GPL(regmap_update_bits_base);
2852 void regmap_async_complete_cb(struct regmap_async *async, int ret)
2854 struct regmap *map = async->map;
2857 trace_regmap_async_io_complete(map);
2859 spin_lock(&map->async_lock);
2860 list_move(&async->list, &map->async_free);
2861 wake = list_empty(&map->async_list);
2864 map->async_ret = ret;
2866 spin_unlock(&map->async_lock);
2869 wake_up(&map->async_waitq);
2871 EXPORT_SYMBOL_GPL(regmap_async_complete_cb);
2873 static int regmap_async_is_done(struct regmap *map)
2875 unsigned long flags;
2878 spin_lock_irqsave(&map->async_lock, flags);
2879 ret = list_empty(&map->async_list);
2880 spin_unlock_irqrestore(&map->async_lock, flags);
2886 * regmap_async_complete - Ensure all asynchronous I/O has completed.
2888 * @map: Map to operate on.
2890 * Blocks until any pending asynchronous I/O has completed. Returns
2891 * an error code for any failed I/O operations.
2893 int regmap_async_complete(struct regmap *map)
2895 unsigned long flags;
2898 /* Nothing to do with no async support */
2899 if (!map->bus || !map->bus->async_write)
2902 trace_regmap_async_complete_start(map);
2904 wait_event(map->async_waitq, regmap_async_is_done(map));
2906 spin_lock_irqsave(&map->async_lock, flags);
2907 ret = map->async_ret;
2909 spin_unlock_irqrestore(&map->async_lock, flags);
2911 trace_regmap_async_complete_done(map);
2915 EXPORT_SYMBOL_GPL(regmap_async_complete);
2918 * regmap_register_patch - Register and apply register updates to be applied
2919 * on device initialistion
2921 * @map: Register map to apply updates to.
2922 * @regs: Values to update.
2923 * @num_regs: Number of entries in regs.
2925 * Register a set of register updates to be applied to the device
2926 * whenever the device registers are synchronised with the cache and
2927 * apply them immediately. Typically this is used to apply
2928 * corrections to be applied to the device defaults on startup, such
2929 * as the updates some vendors provide to undocumented registers.
2931 * The caller must ensure that this function cannot be called
2932 * concurrently with either itself or regcache_sync().
2934 int regmap_register_patch(struct regmap *map, const struct reg_sequence *regs,
2937 struct reg_sequence *p;
2941 if (WARN_ONCE(num_regs <= 0, "invalid registers number (%d)\n",
2945 p = krealloc(map->patch,
2946 sizeof(struct reg_sequence) * (map->patch_regs + num_regs),
2949 memcpy(p + map->patch_regs, regs, num_regs * sizeof(*regs));
2951 map->patch_regs += num_regs;
2956 map->lock(map->lock_arg);
2958 bypass = map->cache_bypass;
2960 map->cache_bypass = true;
2963 ret = _regmap_multi_reg_write(map, regs, num_regs);
2966 map->cache_bypass = bypass;
2968 map->unlock(map->lock_arg);
2970 regmap_async_complete(map);
2974 EXPORT_SYMBOL_GPL(regmap_register_patch);
2977 * regmap_get_val_bytes() - Report the size of a register value
2979 * @map: Register map to operate on.
2981 * Report the size of a register value, mainly intended to for use by
2982 * generic infrastructure built on top of regmap.
2984 int regmap_get_val_bytes(struct regmap *map)
2986 if (map->format.format_write)
2989 return map->format.val_bytes;
2991 EXPORT_SYMBOL_GPL(regmap_get_val_bytes);
2994 * regmap_get_max_register() - Report the max register value
2996 * @map: Register map to operate on.
2998 * Report the max register value, mainly intended to for use by
2999 * generic infrastructure built on top of regmap.
3001 int regmap_get_max_register(struct regmap *map)
3003 return map->max_register ? map->max_register : -EINVAL;
3005 EXPORT_SYMBOL_GPL(regmap_get_max_register);
3008 * regmap_get_reg_stride() - Report the register address stride
3010 * @map: Register map to operate on.
3012 * Report the register address stride, mainly intended to for use by
3013 * generic infrastructure built on top of regmap.
3015 int regmap_get_reg_stride(struct regmap *map)
3017 return map->reg_stride;
3019 EXPORT_SYMBOL_GPL(regmap_get_reg_stride);
3021 int regmap_parse_val(struct regmap *map, const void *buf,
3024 if (!map->format.parse_val)
3027 *val = map->format.parse_val(buf);
3031 EXPORT_SYMBOL_GPL(regmap_parse_val);
3033 static int __init regmap_initcall(void)
3035 regmap_debugfs_initcall();
3039 postcore_initcall(regmap_initcall);