2 * Register map access API
4 * Copyright 2011 Wolfson Microelectronics plc
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/device.h>
14 #include <linux/slab.h>
15 #include <linux/export.h>
16 #include <linux/mutex.h>
17 #include <linux/err.h>
19 #include <linux/rbtree.h>
20 #include <linux/sched.h>
21 #include <linux/delay.h>
23 #define CREATE_TRACE_POINTS
29 * Sometimes for failures during very early init the trace
30 * infrastructure isn't available early enough to be used. For this
31 * sort of problem defining LOG_DEVICE will add printks for basic
32 * register I/O on a specific device.
36 static int _regmap_update_bits(struct regmap *map, unsigned int reg,
37 unsigned int mask, unsigned int val,
38 bool *change, bool force_write);
40 static int _regmap_bus_reg_read(void *context, unsigned int reg,
42 static int _regmap_bus_read(void *context, unsigned int reg,
44 static int _regmap_bus_formatted_write(void *context, unsigned int reg,
46 static int _regmap_bus_reg_write(void *context, unsigned int reg,
48 static int _regmap_bus_raw_write(void *context, unsigned int reg,
51 bool regmap_reg_in_ranges(unsigned int reg,
52 const struct regmap_range *ranges,
55 const struct regmap_range *r;
58 for (i = 0, r = ranges; i < nranges; i++, r++)
59 if (regmap_reg_in_range(reg, r))
63 EXPORT_SYMBOL_GPL(regmap_reg_in_ranges);
65 bool regmap_check_range_table(struct regmap *map, unsigned int reg,
66 const struct regmap_access_table *table)
68 /* Check "no ranges" first */
69 if (regmap_reg_in_ranges(reg, table->no_ranges, table->n_no_ranges))
72 /* In case zero "yes ranges" are supplied, any reg is OK */
73 if (!table->n_yes_ranges)
76 return regmap_reg_in_ranges(reg, table->yes_ranges,
79 EXPORT_SYMBOL_GPL(regmap_check_range_table);
81 bool regmap_writeable(struct regmap *map, unsigned int reg)
83 if (map->max_register && reg > map->max_register)
86 if (map->writeable_reg)
87 return map->writeable_reg(map->dev, reg);
90 return regmap_check_range_table(map, reg, map->wr_table);
95 bool regmap_readable(struct regmap *map, unsigned int reg)
100 if (map->max_register && reg > map->max_register)
103 if (map->format.format_write)
106 if (map->readable_reg)
107 return map->readable_reg(map->dev, reg);
110 return regmap_check_range_table(map, reg, map->rd_table);
115 bool regmap_volatile(struct regmap *map, unsigned int reg)
117 if (!map->format.format_write && !regmap_readable(map, reg))
120 if (map->volatile_reg)
121 return map->volatile_reg(map->dev, reg);
123 if (map->volatile_table)
124 return regmap_check_range_table(map, reg, map->volatile_table);
132 bool regmap_precious(struct regmap *map, unsigned int reg)
134 if (!regmap_readable(map, reg))
137 if (map->precious_reg)
138 return map->precious_reg(map->dev, reg);
140 if (map->precious_table)
141 return regmap_check_range_table(map, reg, map->precious_table);
146 static bool regmap_volatile_range(struct regmap *map, unsigned int reg,
151 for (i = 0; i < num; i++)
152 if (!regmap_volatile(map, reg + i))
158 static void regmap_format_2_6_write(struct regmap *map,
159 unsigned int reg, unsigned int val)
161 u8 *out = map->work_buf;
163 *out = (reg << 6) | val;
166 static void regmap_format_4_12_write(struct regmap *map,
167 unsigned int reg, unsigned int val)
169 __be16 *out = map->work_buf;
170 *out = cpu_to_be16((reg << 12) | val);
173 static void regmap_format_7_9_write(struct regmap *map,
174 unsigned int reg, unsigned int val)
176 __be16 *out = map->work_buf;
177 *out = cpu_to_be16((reg << 9) | val);
180 static void regmap_format_10_14_write(struct regmap *map,
181 unsigned int reg, unsigned int val)
183 u8 *out = map->work_buf;
186 out[1] = (val >> 8) | (reg << 6);
190 static void regmap_format_8(void *buf, unsigned int val, unsigned int shift)
197 static void regmap_format_16_be(void *buf, unsigned int val, unsigned int shift)
201 b[0] = cpu_to_be16(val << shift);
204 static void regmap_format_16_le(void *buf, unsigned int val, unsigned int shift)
208 b[0] = cpu_to_le16(val << shift);
211 static void regmap_format_16_native(void *buf, unsigned int val,
214 *(u16 *)buf = val << shift;
217 static void regmap_format_24(void *buf, unsigned int val, unsigned int shift)
228 static void regmap_format_32_be(void *buf, unsigned int val, unsigned int shift)
232 b[0] = cpu_to_be32(val << shift);
235 static void regmap_format_32_le(void *buf, unsigned int val, unsigned int shift)
239 b[0] = cpu_to_le32(val << shift);
242 static void regmap_format_32_native(void *buf, unsigned int val,
245 *(u32 *)buf = val << shift;
249 static void regmap_format_64_be(void *buf, unsigned int val, unsigned int shift)
253 b[0] = cpu_to_be64((u64)val << shift);
256 static void regmap_format_64_le(void *buf, unsigned int val, unsigned int shift)
260 b[0] = cpu_to_le64((u64)val << shift);
263 static void regmap_format_64_native(void *buf, unsigned int val,
266 *(u64 *)buf = (u64)val << shift;
270 static void regmap_parse_inplace_noop(void *buf)
274 static unsigned int regmap_parse_8(const void *buf)
281 static unsigned int regmap_parse_16_be(const void *buf)
283 const __be16 *b = buf;
285 return be16_to_cpu(b[0]);
288 static unsigned int regmap_parse_16_le(const void *buf)
290 const __le16 *b = buf;
292 return le16_to_cpu(b[0]);
295 static void regmap_parse_16_be_inplace(void *buf)
299 b[0] = be16_to_cpu(b[0]);
302 static void regmap_parse_16_le_inplace(void *buf)
306 b[0] = le16_to_cpu(b[0]);
309 static unsigned int regmap_parse_16_native(const void *buf)
314 static unsigned int regmap_parse_24(const void *buf)
317 unsigned int ret = b[2];
318 ret |= ((unsigned int)b[1]) << 8;
319 ret |= ((unsigned int)b[0]) << 16;
324 static unsigned int regmap_parse_32_be(const void *buf)
326 const __be32 *b = buf;
328 return be32_to_cpu(b[0]);
331 static unsigned int regmap_parse_32_le(const void *buf)
333 const __le32 *b = buf;
335 return le32_to_cpu(b[0]);
338 static void regmap_parse_32_be_inplace(void *buf)
342 b[0] = be32_to_cpu(b[0]);
345 static void regmap_parse_32_le_inplace(void *buf)
349 b[0] = le32_to_cpu(b[0]);
352 static unsigned int regmap_parse_32_native(const void *buf)
358 static unsigned int regmap_parse_64_be(const void *buf)
360 const __be64 *b = buf;
362 return be64_to_cpu(b[0]);
365 static unsigned int regmap_parse_64_le(const void *buf)
367 const __le64 *b = buf;
369 return le64_to_cpu(b[0]);
372 static void regmap_parse_64_be_inplace(void *buf)
376 b[0] = be64_to_cpu(b[0]);
379 static void regmap_parse_64_le_inplace(void *buf)
383 b[0] = le64_to_cpu(b[0]);
386 static unsigned int regmap_parse_64_native(const void *buf)
392 static void regmap_lock_mutex(void *__map)
394 struct regmap *map = __map;
395 mutex_lock(&map->mutex);
398 static void regmap_unlock_mutex(void *__map)
400 struct regmap *map = __map;
401 mutex_unlock(&map->mutex);
404 static void regmap_lock_spinlock(void *__map)
405 __acquires(&map->spinlock)
407 struct regmap *map = __map;
410 spin_lock_irqsave(&map->spinlock, flags);
411 map->spinlock_flags = flags;
414 static void regmap_unlock_spinlock(void *__map)
415 __releases(&map->spinlock)
417 struct regmap *map = __map;
418 spin_unlock_irqrestore(&map->spinlock, map->spinlock_flags);
421 static void dev_get_regmap_release(struct device *dev, void *res)
424 * We don't actually have anything to do here; the goal here
425 * is not to manage the regmap but to provide a simple way to
426 * get the regmap back given a struct device.
430 static bool _regmap_range_add(struct regmap *map,
431 struct regmap_range_node *data)
433 struct rb_root *root = &map->range_tree;
434 struct rb_node **new = &(root->rb_node), *parent = NULL;
437 struct regmap_range_node *this =
438 container_of(*new, struct regmap_range_node, node);
441 if (data->range_max < this->range_min)
442 new = &((*new)->rb_left);
443 else if (data->range_min > this->range_max)
444 new = &((*new)->rb_right);
449 rb_link_node(&data->node, parent, new);
450 rb_insert_color(&data->node, root);
455 static struct regmap_range_node *_regmap_range_lookup(struct regmap *map,
458 struct rb_node *node = map->range_tree.rb_node;
461 struct regmap_range_node *this =
462 container_of(node, struct regmap_range_node, node);
464 if (reg < this->range_min)
465 node = node->rb_left;
466 else if (reg > this->range_max)
467 node = node->rb_right;
475 static void regmap_range_exit(struct regmap *map)
477 struct rb_node *next;
478 struct regmap_range_node *range_node;
480 next = rb_first(&map->range_tree);
482 range_node = rb_entry(next, struct regmap_range_node, node);
483 next = rb_next(&range_node->node);
484 rb_erase(&range_node->node, &map->range_tree);
488 kfree(map->selector_work_buf);
491 int regmap_attach_dev(struct device *dev, struct regmap *map,
492 const struct regmap_config *config)
498 regmap_debugfs_init(map, config->name);
500 /* Add a devres resource for dev_get_regmap() */
501 m = devres_alloc(dev_get_regmap_release, sizeof(*m), GFP_KERNEL);
503 regmap_debugfs_exit(map);
511 EXPORT_SYMBOL_GPL(regmap_attach_dev);
513 static enum regmap_endian regmap_get_reg_endian(const struct regmap_bus *bus,
514 const struct regmap_config *config)
516 enum regmap_endian endian;
518 /* Retrieve the endianness specification from the regmap config */
519 endian = config->reg_format_endian;
521 /* If the regmap config specified a non-default value, use that */
522 if (endian != REGMAP_ENDIAN_DEFAULT)
525 /* Retrieve the endianness specification from the bus config */
526 if (bus && bus->reg_format_endian_default)
527 endian = bus->reg_format_endian_default;
529 /* If the bus specified a non-default value, use that */
530 if (endian != REGMAP_ENDIAN_DEFAULT)
533 /* Use this if no other value was found */
534 return REGMAP_ENDIAN_BIG;
537 enum regmap_endian regmap_get_val_endian(struct device *dev,
538 const struct regmap_bus *bus,
539 const struct regmap_config *config)
541 struct device_node *np;
542 enum regmap_endian endian;
544 /* Retrieve the endianness specification from the regmap config */
545 endian = config->val_format_endian;
547 /* If the regmap config specified a non-default value, use that */
548 if (endian != REGMAP_ENDIAN_DEFAULT)
551 /* If the dev and dev->of_node exist try to get endianness from DT */
552 if (dev && dev->of_node) {
555 /* Parse the device's DT node for an endianness specification */
556 if (of_property_read_bool(np, "big-endian"))
557 endian = REGMAP_ENDIAN_BIG;
558 else if (of_property_read_bool(np, "little-endian"))
559 endian = REGMAP_ENDIAN_LITTLE;
560 else if (of_property_read_bool(np, "native-endian"))
561 endian = REGMAP_ENDIAN_NATIVE;
563 /* If the endianness was specified in DT, use that */
564 if (endian != REGMAP_ENDIAN_DEFAULT)
568 /* Retrieve the endianness specification from the bus config */
569 if (bus && bus->val_format_endian_default)
570 endian = bus->val_format_endian_default;
572 /* If the bus specified a non-default value, use that */
573 if (endian != REGMAP_ENDIAN_DEFAULT)
576 /* Use this if no other value was found */
577 return REGMAP_ENDIAN_BIG;
579 EXPORT_SYMBOL_GPL(regmap_get_val_endian);
581 struct regmap *__regmap_init(struct device *dev,
582 const struct regmap_bus *bus,
584 const struct regmap_config *config,
585 struct lock_class_key *lock_key,
586 const char *lock_name)
590 enum regmap_endian reg_endian, val_endian;
596 map = kzalloc(sizeof(*map), GFP_KERNEL);
602 if (config->lock && config->unlock) {
603 map->lock = config->lock;
604 map->unlock = config->unlock;
605 map->lock_arg = config->lock_arg;
607 if ((bus && bus->fast_io) ||
609 spin_lock_init(&map->spinlock);
610 map->lock = regmap_lock_spinlock;
611 map->unlock = regmap_unlock_spinlock;
612 lockdep_set_class_and_name(&map->spinlock,
613 lock_key, lock_name);
615 mutex_init(&map->mutex);
616 map->lock = regmap_lock_mutex;
617 map->unlock = regmap_unlock_mutex;
618 lockdep_set_class_and_name(&map->mutex,
619 lock_key, lock_name);
625 * When we write in fast-paths with regmap_bulk_write() don't allocate
626 * scratch buffers with sleeping allocations.
628 if ((bus && bus->fast_io) || config->fast_io)
629 map->alloc_flags = GFP_ATOMIC;
631 map->alloc_flags = GFP_KERNEL;
633 map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8);
634 map->format.pad_bytes = config->pad_bits / 8;
635 map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8);
636 map->format.buf_size = DIV_ROUND_UP(config->reg_bits +
637 config->val_bits + config->pad_bits, 8);
638 map->reg_shift = config->pad_bits % 8;
639 if (config->reg_stride)
640 map->reg_stride = config->reg_stride;
643 map->use_single_read = config->use_single_rw || !bus || !bus->read;
644 map->use_single_write = config->use_single_rw || !bus || !bus->write;
645 map->can_multi_write = config->can_multi_write && bus && bus->write;
647 map->max_raw_read = bus->max_raw_read;
648 map->max_raw_write = bus->max_raw_write;
652 map->bus_context = bus_context;
653 map->max_register = config->max_register;
654 map->wr_table = config->wr_table;
655 map->rd_table = config->rd_table;
656 map->volatile_table = config->volatile_table;
657 map->precious_table = config->precious_table;
658 map->writeable_reg = config->writeable_reg;
659 map->readable_reg = config->readable_reg;
660 map->volatile_reg = config->volatile_reg;
661 map->precious_reg = config->precious_reg;
662 map->cache_type = config->cache_type;
663 map->name = config->name;
665 spin_lock_init(&map->async_lock);
666 INIT_LIST_HEAD(&map->async_list);
667 INIT_LIST_HEAD(&map->async_free);
668 init_waitqueue_head(&map->async_waitq);
670 if (config->read_flag_mask || config->write_flag_mask) {
671 map->read_flag_mask = config->read_flag_mask;
672 map->write_flag_mask = config->write_flag_mask;
674 map->read_flag_mask = bus->read_flag_mask;
678 map->reg_read = config->reg_read;
679 map->reg_write = config->reg_write;
681 map->defer_caching = false;
682 goto skip_format_initialization;
683 } else if (!bus->read || !bus->write) {
684 map->reg_read = _regmap_bus_reg_read;
685 map->reg_write = _regmap_bus_reg_write;
687 map->defer_caching = false;
688 goto skip_format_initialization;
690 map->reg_read = _regmap_bus_read;
691 map->reg_update_bits = bus->reg_update_bits;
694 reg_endian = regmap_get_reg_endian(bus, config);
695 val_endian = regmap_get_val_endian(dev, bus, config);
697 switch (config->reg_bits + map->reg_shift) {
699 switch (config->val_bits) {
701 map->format.format_write = regmap_format_2_6_write;
709 switch (config->val_bits) {
711 map->format.format_write = regmap_format_4_12_write;
719 switch (config->val_bits) {
721 map->format.format_write = regmap_format_7_9_write;
729 switch (config->val_bits) {
731 map->format.format_write = regmap_format_10_14_write;
739 map->format.format_reg = regmap_format_8;
743 switch (reg_endian) {
744 case REGMAP_ENDIAN_BIG:
745 map->format.format_reg = regmap_format_16_be;
747 case REGMAP_ENDIAN_NATIVE:
748 map->format.format_reg = regmap_format_16_native;
756 if (reg_endian != REGMAP_ENDIAN_BIG)
758 map->format.format_reg = regmap_format_24;
762 switch (reg_endian) {
763 case REGMAP_ENDIAN_BIG:
764 map->format.format_reg = regmap_format_32_be;
766 case REGMAP_ENDIAN_NATIVE:
767 map->format.format_reg = regmap_format_32_native;
776 switch (reg_endian) {
777 case REGMAP_ENDIAN_BIG:
778 map->format.format_reg = regmap_format_64_be;
780 case REGMAP_ENDIAN_NATIVE:
781 map->format.format_reg = regmap_format_64_native;
793 if (val_endian == REGMAP_ENDIAN_NATIVE)
794 map->format.parse_inplace = regmap_parse_inplace_noop;
796 switch (config->val_bits) {
798 map->format.format_val = regmap_format_8;
799 map->format.parse_val = regmap_parse_8;
800 map->format.parse_inplace = regmap_parse_inplace_noop;
803 switch (val_endian) {
804 case REGMAP_ENDIAN_BIG:
805 map->format.format_val = regmap_format_16_be;
806 map->format.parse_val = regmap_parse_16_be;
807 map->format.parse_inplace = regmap_parse_16_be_inplace;
809 case REGMAP_ENDIAN_LITTLE:
810 map->format.format_val = regmap_format_16_le;
811 map->format.parse_val = regmap_parse_16_le;
812 map->format.parse_inplace = regmap_parse_16_le_inplace;
814 case REGMAP_ENDIAN_NATIVE:
815 map->format.format_val = regmap_format_16_native;
816 map->format.parse_val = regmap_parse_16_native;
823 if (val_endian != REGMAP_ENDIAN_BIG)
825 map->format.format_val = regmap_format_24;
826 map->format.parse_val = regmap_parse_24;
829 switch (val_endian) {
830 case REGMAP_ENDIAN_BIG:
831 map->format.format_val = regmap_format_32_be;
832 map->format.parse_val = regmap_parse_32_be;
833 map->format.parse_inplace = regmap_parse_32_be_inplace;
835 case REGMAP_ENDIAN_LITTLE:
836 map->format.format_val = regmap_format_32_le;
837 map->format.parse_val = regmap_parse_32_le;
838 map->format.parse_inplace = regmap_parse_32_le_inplace;
840 case REGMAP_ENDIAN_NATIVE:
841 map->format.format_val = regmap_format_32_native;
842 map->format.parse_val = regmap_parse_32_native;
850 switch (val_endian) {
851 case REGMAP_ENDIAN_BIG:
852 map->format.format_val = regmap_format_64_be;
853 map->format.parse_val = regmap_parse_64_be;
854 map->format.parse_inplace = regmap_parse_64_be_inplace;
856 case REGMAP_ENDIAN_LITTLE:
857 map->format.format_val = regmap_format_64_le;
858 map->format.parse_val = regmap_parse_64_le;
859 map->format.parse_inplace = regmap_parse_64_le_inplace;
861 case REGMAP_ENDIAN_NATIVE:
862 map->format.format_val = regmap_format_64_native;
863 map->format.parse_val = regmap_parse_64_native;
872 if (map->format.format_write) {
873 if ((reg_endian != REGMAP_ENDIAN_BIG) ||
874 (val_endian != REGMAP_ENDIAN_BIG))
876 map->use_single_write = true;
879 if (!map->format.format_write &&
880 !(map->format.format_reg && map->format.format_val))
883 map->work_buf = kzalloc(map->format.buf_size, GFP_KERNEL);
884 if (map->work_buf == NULL) {
889 if (map->format.format_write) {
890 map->defer_caching = false;
891 map->reg_write = _regmap_bus_formatted_write;
892 } else if (map->format.format_val) {
893 map->defer_caching = true;
894 map->reg_write = _regmap_bus_raw_write;
897 skip_format_initialization:
899 map->range_tree = RB_ROOT;
900 for (i = 0; i < config->num_ranges; i++) {
901 const struct regmap_range_cfg *range_cfg = &config->ranges[i];
902 struct regmap_range_node *new;
905 if (range_cfg->range_max < range_cfg->range_min) {
906 dev_err(map->dev, "Invalid range %d: %d < %d\n", i,
907 range_cfg->range_max, range_cfg->range_min);
911 if (range_cfg->range_max > map->max_register) {
912 dev_err(map->dev, "Invalid range %d: %d > %d\n", i,
913 range_cfg->range_max, map->max_register);
917 if (range_cfg->selector_reg > map->max_register) {
919 "Invalid range %d: selector out of map\n", i);
923 if (range_cfg->window_len == 0) {
924 dev_err(map->dev, "Invalid range %d: window_len 0\n",
929 /* Make sure, that this register range has no selector
930 or data window within its boundary */
931 for (j = 0; j < config->num_ranges; j++) {
932 unsigned sel_reg = config->ranges[j].selector_reg;
933 unsigned win_min = config->ranges[j].window_start;
934 unsigned win_max = win_min +
935 config->ranges[j].window_len - 1;
937 /* Allow data window inside its own virtual range */
941 if (range_cfg->range_min <= sel_reg &&
942 sel_reg <= range_cfg->range_max) {
944 "Range %d: selector for %d in window\n",
949 if (!(win_max < range_cfg->range_min ||
950 win_min > range_cfg->range_max)) {
952 "Range %d: window for %d in window\n",
958 new = kzalloc(sizeof(*new), GFP_KERNEL);
965 new->name = range_cfg->name;
966 new->range_min = range_cfg->range_min;
967 new->range_max = range_cfg->range_max;
968 new->selector_reg = range_cfg->selector_reg;
969 new->selector_mask = range_cfg->selector_mask;
970 new->selector_shift = range_cfg->selector_shift;
971 new->window_start = range_cfg->window_start;
972 new->window_len = range_cfg->window_len;
974 if (!_regmap_range_add(map, new)) {
975 dev_err(map->dev, "Failed to add range %d\n", i);
980 if (map->selector_work_buf == NULL) {
981 map->selector_work_buf =
982 kzalloc(map->format.buf_size, GFP_KERNEL);
983 if (map->selector_work_buf == NULL) {
990 ret = regcache_init(map, config);
995 ret = regmap_attach_dev(dev, map, config);
1005 regmap_range_exit(map);
1006 kfree(map->work_buf);
1010 return ERR_PTR(ret);
1012 EXPORT_SYMBOL_GPL(__regmap_init);
1014 static void devm_regmap_release(struct device *dev, void *res)
1016 regmap_exit(*(struct regmap **)res);
1019 struct regmap *__devm_regmap_init(struct device *dev,
1020 const struct regmap_bus *bus,
1022 const struct regmap_config *config,
1023 struct lock_class_key *lock_key,
1024 const char *lock_name)
1026 struct regmap **ptr, *regmap;
1028 ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL);
1030 return ERR_PTR(-ENOMEM);
1032 regmap = __regmap_init(dev, bus, bus_context, config,
1033 lock_key, lock_name);
1034 if (!IS_ERR(regmap)) {
1036 devres_add(dev, ptr);
1043 EXPORT_SYMBOL_GPL(__devm_regmap_init);
1045 static void regmap_field_init(struct regmap_field *rm_field,
1046 struct regmap *regmap, struct reg_field reg_field)
1048 rm_field->regmap = regmap;
1049 rm_field->reg = reg_field.reg;
1050 rm_field->shift = reg_field.lsb;
1051 rm_field->mask = GENMASK(reg_field.msb, reg_field.lsb);
1052 rm_field->id_size = reg_field.id_size;
1053 rm_field->id_offset = reg_field.id_offset;
1057 * devm_regmap_field_alloc(): Allocate and initialise a register field
1058 * in a register map.
1060 * @dev: Device that will be interacted with
1061 * @regmap: regmap bank in which this register field is located.
1062 * @reg_field: Register field with in the bank.
1064 * The return value will be an ERR_PTR() on error or a valid pointer
1065 * to a struct regmap_field. The regmap_field will be automatically freed
1066 * by the device management code.
1068 struct regmap_field *devm_regmap_field_alloc(struct device *dev,
1069 struct regmap *regmap, struct reg_field reg_field)
1071 struct regmap_field *rm_field = devm_kzalloc(dev,
1072 sizeof(*rm_field), GFP_KERNEL);
1074 return ERR_PTR(-ENOMEM);
1076 regmap_field_init(rm_field, regmap, reg_field);
1081 EXPORT_SYMBOL_GPL(devm_regmap_field_alloc);
1084 * devm_regmap_field_free(): Free register field allocated using
1085 * devm_regmap_field_alloc. Usally drivers need not call this function,
1086 * as the memory allocated via devm will be freed as per device-driver
1089 * @dev: Device that will be interacted with
1090 * @field: regmap field which should be freed.
1092 void devm_regmap_field_free(struct device *dev,
1093 struct regmap_field *field)
1095 devm_kfree(dev, field);
1097 EXPORT_SYMBOL_GPL(devm_regmap_field_free);
1100 * regmap_field_alloc(): Allocate and initialise a register field
1101 * in a register map.
1103 * @regmap: regmap bank in which this register field is located.
1104 * @reg_field: Register field with in the bank.
1106 * The return value will be an ERR_PTR() on error or a valid pointer
1107 * to a struct regmap_field. The regmap_field should be freed by the
1108 * user once its finished working with it using regmap_field_free().
1110 struct regmap_field *regmap_field_alloc(struct regmap *regmap,
1111 struct reg_field reg_field)
1113 struct regmap_field *rm_field = kzalloc(sizeof(*rm_field), GFP_KERNEL);
1116 return ERR_PTR(-ENOMEM);
1118 regmap_field_init(rm_field, regmap, reg_field);
1122 EXPORT_SYMBOL_GPL(regmap_field_alloc);
1125 * regmap_field_free(): Free register field allocated using regmap_field_alloc
1127 * @field: regmap field which should be freed.
1129 void regmap_field_free(struct regmap_field *field)
1133 EXPORT_SYMBOL_GPL(regmap_field_free);
1136 * regmap_reinit_cache(): Reinitialise the current register cache
1138 * @map: Register map to operate on.
1139 * @config: New configuration. Only the cache data will be used.
1141 * Discard any existing register cache for the map and initialize a
1142 * new cache. This can be used to restore the cache to defaults or to
1143 * update the cache configuration to reflect runtime discovery of the
1146 * No explicit locking is done here, the user needs to ensure that
1147 * this function will not race with other calls to regmap.
1149 int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
1152 regmap_debugfs_exit(map);
1154 map->max_register = config->max_register;
1155 map->writeable_reg = config->writeable_reg;
1156 map->readable_reg = config->readable_reg;
1157 map->volatile_reg = config->volatile_reg;
1158 map->precious_reg = config->precious_reg;
1159 map->cache_type = config->cache_type;
1161 regmap_debugfs_init(map, config->name);
1163 map->cache_bypass = false;
1164 map->cache_only = false;
1166 return regcache_init(map, config);
1168 EXPORT_SYMBOL_GPL(regmap_reinit_cache);
1171 * regmap_exit(): Free a previously allocated register map
1173 void regmap_exit(struct regmap *map)
1175 struct regmap_async *async;
1178 regmap_debugfs_exit(map);
1179 regmap_range_exit(map);
1180 if (map->bus && map->bus->free_context)
1181 map->bus->free_context(map->bus_context);
1182 kfree(map->work_buf);
1183 while (!list_empty(&map->async_free)) {
1184 async = list_first_entry_or_null(&map->async_free,
1185 struct regmap_async,
1187 list_del(&async->list);
1188 kfree(async->work_buf);
1193 EXPORT_SYMBOL_GPL(regmap_exit);
1195 static int dev_get_regmap_match(struct device *dev, void *res, void *data)
1197 struct regmap **r = res;
1203 /* If the user didn't specify a name match any */
1205 return (*r)->name == data;
1211 * dev_get_regmap(): Obtain the regmap (if any) for a device
1213 * @dev: Device to retrieve the map for
1214 * @name: Optional name for the register map, usually NULL.
1216 * Returns the regmap for the device if one is present, or NULL. If
1217 * name is specified then it must match the name specified when
1218 * registering the device, if it is NULL then the first regmap found
1219 * will be used. Devices with multiple register maps are very rare,
1220 * generic code should normally not need to specify a name.
1222 struct regmap *dev_get_regmap(struct device *dev, const char *name)
1224 struct regmap **r = devres_find(dev, dev_get_regmap_release,
1225 dev_get_regmap_match, (void *)name);
1231 EXPORT_SYMBOL_GPL(dev_get_regmap);
1234 * regmap_get_device(): Obtain the device from a regmap
1236 * @map: Register map to operate on.
1238 * Returns the underlying device that the regmap has been created for.
1240 struct device *regmap_get_device(struct regmap *map)
1244 EXPORT_SYMBOL_GPL(regmap_get_device);
1246 static int _regmap_select_page(struct regmap *map, unsigned int *reg,
1247 struct regmap_range_node *range,
1248 unsigned int val_num)
1250 void *orig_work_buf;
1251 unsigned int win_offset;
1252 unsigned int win_page;
1256 win_offset = (*reg - range->range_min) % range->window_len;
1257 win_page = (*reg - range->range_min) / range->window_len;
1260 /* Bulk write shouldn't cross range boundary */
1261 if (*reg + val_num - 1 > range->range_max)
1264 /* ... or single page boundary */
1265 if (val_num > range->window_len - win_offset)
1269 /* It is possible to have selector register inside data window.
1270 In that case, selector register is located on every page and
1271 it needs no page switching, when accessed alone. */
1273 range->window_start + win_offset != range->selector_reg) {
1274 /* Use separate work_buf during page switching */
1275 orig_work_buf = map->work_buf;
1276 map->work_buf = map->selector_work_buf;
1278 ret = _regmap_update_bits(map, range->selector_reg,
1279 range->selector_mask,
1280 win_page << range->selector_shift,
1283 map->work_buf = orig_work_buf;
1289 *reg = range->window_start + win_offset;
1294 int _regmap_raw_write(struct regmap *map, unsigned int reg,
1295 const void *val, size_t val_len)
1297 struct regmap_range_node *range;
1298 unsigned long flags;
1299 u8 *u8 = map->work_buf;
1300 void *work_val = map->work_buf + map->format.reg_bytes +
1301 map->format.pad_bytes;
1303 int ret = -ENOTSUPP;
1309 /* Check for unwritable registers before we start */
1310 if (map->writeable_reg)
1311 for (i = 0; i < val_len / map->format.val_bytes; i++)
1312 if (!map->writeable_reg(map->dev,
1313 reg + (i * map->reg_stride)))
1316 if (!map->cache_bypass && map->format.parse_val) {
1318 int val_bytes = map->format.val_bytes;
1319 for (i = 0; i < val_len / val_bytes; i++) {
1320 ival = map->format.parse_val(val + (i * val_bytes));
1321 ret = regcache_write(map, reg + (i * map->reg_stride),
1325 "Error in caching of register: %x ret: %d\n",
1330 if (map->cache_only) {
1331 map->cache_dirty = true;
1336 range = _regmap_range_lookup(map, reg);
1338 int val_num = val_len / map->format.val_bytes;
1339 int win_offset = (reg - range->range_min) % range->window_len;
1340 int win_residue = range->window_len - win_offset;
1342 /* If the write goes beyond the end of the window split it */
1343 while (val_num > win_residue) {
1344 dev_dbg(map->dev, "Writing window %d/%zu\n",
1345 win_residue, val_len / map->format.val_bytes);
1346 ret = _regmap_raw_write(map, reg, val, win_residue *
1347 map->format.val_bytes);
1352 val_num -= win_residue;
1353 val += win_residue * map->format.val_bytes;
1354 val_len -= win_residue * map->format.val_bytes;
1356 win_offset = (reg - range->range_min) %
1358 win_residue = range->window_len - win_offset;
1361 ret = _regmap_select_page(map, ®, range, val_num);
1366 map->format.format_reg(map->work_buf, reg, map->reg_shift);
1368 u8[0] |= map->write_flag_mask;
1371 * Essentially all I/O mechanisms will be faster with a single
1372 * buffer to write. Since register syncs often generate raw
1373 * writes of single registers optimise that case.
1375 if (val != work_val && val_len == map->format.val_bytes) {
1376 memcpy(work_val, val, map->format.val_bytes);
1380 if (map->async && map->bus->async_write) {
1381 struct regmap_async *async;
1383 trace_regmap_async_write_start(map, reg, val_len);
1385 spin_lock_irqsave(&map->async_lock, flags);
1386 async = list_first_entry_or_null(&map->async_free,
1387 struct regmap_async,
1390 list_del(&async->list);
1391 spin_unlock_irqrestore(&map->async_lock, flags);
1394 async = map->bus->async_alloc();
1398 async->work_buf = kzalloc(map->format.buf_size,
1399 GFP_KERNEL | GFP_DMA);
1400 if (!async->work_buf) {
1408 /* If the caller supplied the value we can use it safely. */
1409 memcpy(async->work_buf, map->work_buf, map->format.pad_bytes +
1410 map->format.reg_bytes + map->format.val_bytes);
1412 spin_lock_irqsave(&map->async_lock, flags);
1413 list_add_tail(&async->list, &map->async_list);
1414 spin_unlock_irqrestore(&map->async_lock, flags);
1416 if (val != work_val)
1417 ret = map->bus->async_write(map->bus_context,
1419 map->format.reg_bytes +
1420 map->format.pad_bytes,
1421 val, val_len, async);
1423 ret = map->bus->async_write(map->bus_context,
1425 map->format.reg_bytes +
1426 map->format.pad_bytes +
1427 val_len, NULL, 0, async);
1430 dev_err(map->dev, "Failed to schedule write: %d\n",
1433 spin_lock_irqsave(&map->async_lock, flags);
1434 list_move(&async->list, &map->async_free);
1435 spin_unlock_irqrestore(&map->async_lock, flags);
1441 trace_regmap_hw_write_start(map, reg, val_len / map->format.val_bytes);
1443 /* If we're doing a single register write we can probably just
1444 * send the work_buf directly, otherwise try to do a gather
1447 if (val == work_val)
1448 ret = map->bus->write(map->bus_context, map->work_buf,
1449 map->format.reg_bytes +
1450 map->format.pad_bytes +
1452 else if (map->bus->gather_write)
1453 ret = map->bus->gather_write(map->bus_context, map->work_buf,
1454 map->format.reg_bytes +
1455 map->format.pad_bytes,
1458 /* If that didn't work fall back on linearising by hand. */
1459 if (ret == -ENOTSUPP) {
1460 len = map->format.reg_bytes + map->format.pad_bytes + val_len;
1461 buf = kzalloc(len, GFP_KERNEL);
1465 memcpy(buf, map->work_buf, map->format.reg_bytes);
1466 memcpy(buf + map->format.reg_bytes + map->format.pad_bytes,
1468 ret = map->bus->write(map->bus_context, buf, len);
1473 trace_regmap_hw_write_done(map, reg, val_len / map->format.val_bytes);
1479 * regmap_can_raw_write - Test if regmap_raw_write() is supported
1481 * @map: Map to check.
1483 bool regmap_can_raw_write(struct regmap *map)
1485 return map->bus && map->bus->write && map->format.format_val &&
1486 map->format.format_reg;
1488 EXPORT_SYMBOL_GPL(regmap_can_raw_write);
1491 * regmap_get_raw_read_max - Get the maximum size we can read
1493 * @map: Map to check.
1495 size_t regmap_get_raw_read_max(struct regmap *map)
1497 return map->max_raw_read;
1499 EXPORT_SYMBOL_GPL(regmap_get_raw_read_max);
1502 * regmap_get_raw_write_max - Get the maximum size we can read
1504 * @map: Map to check.
1506 size_t regmap_get_raw_write_max(struct regmap *map)
1508 return map->max_raw_write;
1510 EXPORT_SYMBOL_GPL(regmap_get_raw_write_max);
1512 static int _regmap_bus_formatted_write(void *context, unsigned int reg,
1516 struct regmap_range_node *range;
1517 struct regmap *map = context;
1519 WARN_ON(!map->bus || !map->format.format_write);
1521 range = _regmap_range_lookup(map, reg);
1523 ret = _regmap_select_page(map, ®, range, 1);
1528 map->format.format_write(map, reg, val);
1530 trace_regmap_hw_write_start(map, reg, 1);
1532 ret = map->bus->write(map->bus_context, map->work_buf,
1533 map->format.buf_size);
1535 trace_regmap_hw_write_done(map, reg, 1);
1540 static int _regmap_bus_reg_write(void *context, unsigned int reg,
1543 struct regmap *map = context;
1545 return map->bus->reg_write(map->bus_context, reg, val);
1548 static int _regmap_bus_raw_write(void *context, unsigned int reg,
1551 struct regmap *map = context;
1553 WARN_ON(!map->bus || !map->format.format_val);
1555 map->format.format_val(map->work_buf + map->format.reg_bytes
1556 + map->format.pad_bytes, val, 0);
1557 return _regmap_raw_write(map, reg,
1559 map->format.reg_bytes +
1560 map->format.pad_bytes,
1561 map->format.val_bytes);
1564 static inline void *_regmap_map_get_context(struct regmap *map)
1566 return (map->bus) ? map : map->bus_context;
1569 int _regmap_write(struct regmap *map, unsigned int reg,
1573 void *context = _regmap_map_get_context(map);
1575 if (!regmap_writeable(map, reg))
1578 if (!map->cache_bypass && !map->defer_caching) {
1579 ret = regcache_write(map, reg, val);
1582 if (map->cache_only) {
1583 map->cache_dirty = true;
1589 if (map->dev && strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
1590 dev_info(map->dev, "%x <= %x\n", reg, val);
1593 trace_regmap_reg_write(map, reg, val);
1595 return map->reg_write(context, reg, val);
1599 * regmap_write(): Write a value to a single register
1601 * @map: Register map to write to
1602 * @reg: Register to write to
1603 * @val: Value to be written
1605 * A value of zero will be returned on success, a negative errno will
1606 * be returned in error cases.
1608 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
1612 if (!IS_ALIGNED(reg, map->reg_stride))
1615 map->lock(map->lock_arg);
1617 ret = _regmap_write(map, reg, val);
1619 map->unlock(map->lock_arg);
1623 EXPORT_SYMBOL_GPL(regmap_write);
1626 * regmap_write_async(): Write a value to a single register asynchronously
1628 * @map: Register map to write to
1629 * @reg: Register to write to
1630 * @val: Value to be written
1632 * A value of zero will be returned on success, a negative errno will
1633 * be returned in error cases.
1635 int regmap_write_async(struct regmap *map, unsigned int reg, unsigned int val)
1639 if (!IS_ALIGNED(reg, map->reg_stride))
1642 map->lock(map->lock_arg);
1646 ret = _regmap_write(map, reg, val);
1650 map->unlock(map->lock_arg);
1654 EXPORT_SYMBOL_GPL(regmap_write_async);
1657 * regmap_raw_write(): Write raw values to one or more registers
1659 * @map: Register map to write to
1660 * @reg: Initial register to write to
1661 * @val: Block of data to be written, laid out for direct transmission to the
1663 * @val_len: Length of data pointed to by val.
1665 * This function is intended to be used for things like firmware
1666 * download where a large block of data needs to be transferred to the
1667 * device. No formatting will be done on the data provided.
1669 * A value of zero will be returned on success, a negative errno will
1670 * be returned in error cases.
1672 int regmap_raw_write(struct regmap *map, unsigned int reg,
1673 const void *val, size_t val_len)
1677 if (!regmap_can_raw_write(map))
1679 if (val_len % map->format.val_bytes)
1681 if (map->max_raw_write && map->max_raw_write > val_len)
1684 map->lock(map->lock_arg);
1686 ret = _regmap_raw_write(map, reg, val, val_len);
1688 map->unlock(map->lock_arg);
1692 EXPORT_SYMBOL_GPL(regmap_raw_write);
1695 * regmap_field_write(): Write a value to a single register field
1697 * @field: Register field to write to
1698 * @val: Value to be written
1700 * A value of zero will be returned on success, a negative errno will
1701 * be returned in error cases.
1703 int regmap_field_write(struct regmap_field *field, unsigned int val)
1705 return regmap_update_bits(field->regmap, field->reg,
1706 field->mask, val << field->shift);
1708 EXPORT_SYMBOL_GPL(regmap_field_write);
1711 * regmap_field_update_bits(): Perform a read/modify/write cycle
1712 * on the register field
1714 * @field: Register field to write to
1715 * @mask: Bitmask to change
1716 * @val: Value to be written
1718 * A value of zero will be returned on success, a negative errno will
1719 * be returned in error cases.
1721 int regmap_field_update_bits(struct regmap_field *field, unsigned int mask, unsigned int val)
1723 mask = (mask << field->shift) & field->mask;
1725 return regmap_update_bits(field->regmap, field->reg,
1726 mask, val << field->shift);
1728 EXPORT_SYMBOL_GPL(regmap_field_update_bits);
1731 * regmap_fields_write(): Write a value to a single register field with port ID
1733 * @field: Register field to write to
1735 * @val: Value to be written
1737 * A value of zero will be returned on success, a negative errno will
1738 * be returned in error cases.
1740 int regmap_fields_write(struct regmap_field *field, unsigned int id,
1743 if (id >= field->id_size)
1746 return regmap_update_bits(field->regmap,
1747 field->reg + (field->id_offset * id),
1748 field->mask, val << field->shift);
1750 EXPORT_SYMBOL_GPL(regmap_fields_write);
1752 int regmap_fields_force_write(struct regmap_field *field, unsigned int id,
1755 if (id >= field->id_size)
1758 return regmap_write_bits(field->regmap,
1759 field->reg + (field->id_offset * id),
1760 field->mask, val << field->shift);
1762 EXPORT_SYMBOL_GPL(regmap_fields_force_write);
1765 * regmap_fields_update_bits(): Perform a read/modify/write cycle
1766 * on the register field
1768 * @field: Register field to write to
1770 * @mask: Bitmask to change
1771 * @val: Value to be written
1773 * A value of zero will be returned on success, a negative errno will
1774 * be returned in error cases.
1776 int regmap_fields_update_bits(struct regmap_field *field, unsigned int id,
1777 unsigned int mask, unsigned int val)
1779 if (id >= field->id_size)
1782 mask = (mask << field->shift) & field->mask;
1784 return regmap_update_bits(field->regmap,
1785 field->reg + (field->id_offset * id),
1786 mask, val << field->shift);
1788 EXPORT_SYMBOL_GPL(regmap_fields_update_bits);
1791 * regmap_bulk_write(): Write multiple registers to the device
1793 * @map: Register map to write to
1794 * @reg: First register to be write from
1795 * @val: Block of data to be written, in native register size for device
1796 * @val_count: Number of registers to write
1798 * This function is intended to be used for writing a large block of
1799 * data to the device either in single transfer or multiple transfer.
1801 * A value of zero will be returned on success, a negative errno will
1802 * be returned in error cases.
1804 int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
1808 size_t val_bytes = map->format.val_bytes;
1809 size_t total_size = val_bytes * val_count;
1811 if (map->bus && !map->format.parse_inplace)
1813 if (!IS_ALIGNED(reg, map->reg_stride))
1817 * Some devices don't support bulk write, for
1818 * them we have a series of single write operations in the first two if
1821 * The first if block is used for memory mapped io. It does not allow
1822 * val_bytes of 3 for example.
1823 * The second one is used for busses which do not have this limitation
1824 * and can write arbitrary value lengths.
1827 map->lock(map->lock_arg);
1828 for (i = 0; i < val_count; i++) {
1831 switch (val_bytes) {
1833 ival = *(u8 *)(val + (i * val_bytes));
1836 ival = *(u16 *)(val + (i * val_bytes));
1839 ival = *(u32 *)(val + (i * val_bytes));
1843 ival = *(u64 *)(val + (i * val_bytes));
1851 ret = _regmap_write(map, reg + (i * map->reg_stride),
1857 map->unlock(map->lock_arg);
1858 } else if (map->use_single_write ||
1859 (map->max_raw_write && map->max_raw_write < total_size)) {
1860 int chunk_stride = map->reg_stride;
1861 size_t chunk_size = val_bytes;
1862 size_t chunk_count = val_count;
1864 if (!map->use_single_write) {
1865 chunk_size = map->max_raw_write;
1866 if (chunk_size % val_bytes)
1867 chunk_size -= chunk_size % val_bytes;
1868 chunk_count = total_size / chunk_size;
1869 chunk_stride *= chunk_size / val_bytes;
1872 map->lock(map->lock_arg);
1873 /* Write as many bytes as possible with chunk_size */
1874 for (i = 0; i < chunk_count; i++) {
1875 ret = _regmap_raw_write(map,
1876 reg + (i * chunk_stride),
1877 val + (i * chunk_size),
1883 /* Write remaining bytes */
1884 if (!ret && chunk_size * i < total_size) {
1885 ret = _regmap_raw_write(map, reg + (i * chunk_stride),
1886 val + (i * chunk_size),
1887 total_size - i * chunk_size);
1889 map->unlock(map->lock_arg);
1896 wval = kmemdup(val, val_count * val_bytes, map->alloc_flags);
1898 dev_err(map->dev, "Error in memory allocation\n");
1901 for (i = 0; i < val_count * val_bytes; i += val_bytes)
1902 map->format.parse_inplace(wval + i);
1904 map->lock(map->lock_arg);
1905 ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count);
1906 map->unlock(map->lock_arg);
1912 EXPORT_SYMBOL_GPL(regmap_bulk_write);
1915 * _regmap_raw_multi_reg_write()
1917 * the (register,newvalue) pairs in regs have not been formatted, but
1918 * they are all in the same page and have been changed to being page
1919 * relative. The page register has been written if that was necessary.
1921 static int _regmap_raw_multi_reg_write(struct regmap *map,
1922 const struct reg_sequence *regs,
1929 size_t val_bytes = map->format.val_bytes;
1930 size_t reg_bytes = map->format.reg_bytes;
1931 size_t pad_bytes = map->format.pad_bytes;
1932 size_t pair_size = reg_bytes + pad_bytes + val_bytes;
1933 size_t len = pair_size * num_regs;
1938 buf = kzalloc(len, GFP_KERNEL);
1942 /* We have to linearise by hand. */
1946 for (i = 0; i < num_regs; i++) {
1947 unsigned int reg = regs[i].reg;
1948 unsigned int val = regs[i].def;
1949 trace_regmap_hw_write_start(map, reg, 1);
1950 map->format.format_reg(u8, reg, map->reg_shift);
1951 u8 += reg_bytes + pad_bytes;
1952 map->format.format_val(u8, val, 0);
1956 *u8 |= map->write_flag_mask;
1958 ret = map->bus->write(map->bus_context, buf, len);
1962 for (i = 0; i < num_regs; i++) {
1963 int reg = regs[i].reg;
1964 trace_regmap_hw_write_done(map, reg, 1);
1969 static unsigned int _regmap_register_page(struct regmap *map,
1971 struct regmap_range_node *range)
1973 unsigned int win_page = (reg - range->range_min) / range->window_len;
1978 static int _regmap_range_multi_paged_reg_write(struct regmap *map,
1979 struct reg_sequence *regs,
1984 struct reg_sequence *base;
1985 unsigned int this_page = 0;
1986 unsigned int page_change = 0;
1988 * the set of registers are not neccessarily in order, but
1989 * since the order of write must be preserved this algorithm
1990 * chops the set each time the page changes. This also applies
1991 * if there is a delay required at any point in the sequence.
1994 for (i = 0, n = 0; i < num_regs; i++, n++) {
1995 unsigned int reg = regs[i].reg;
1996 struct regmap_range_node *range;
1998 range = _regmap_range_lookup(map, reg);
2000 unsigned int win_page = _regmap_register_page(map, reg,
2004 this_page = win_page;
2005 if (win_page != this_page) {
2006 this_page = win_page;
2011 /* If we have both a page change and a delay make sure to
2012 * write the regs and apply the delay before we change the
2016 if (page_change || regs[i].delay_us) {
2018 /* For situations where the first write requires
2019 * a delay we need to make sure we don't call
2020 * raw_multi_reg_write with n=0
2021 * This can't occur with page breaks as we
2022 * never write on the first iteration
2024 if (regs[i].delay_us && i == 0)
2027 ret = _regmap_raw_multi_reg_write(map, base, n);
2031 if (regs[i].delay_us)
2032 udelay(regs[i].delay_us);
2038 ret = _regmap_select_page(map,
2051 return _regmap_raw_multi_reg_write(map, base, n);
2055 static int _regmap_multi_reg_write(struct regmap *map,
2056 const struct reg_sequence *regs,
2062 if (!map->can_multi_write) {
2063 for (i = 0; i < num_regs; i++) {
2064 ret = _regmap_write(map, regs[i].reg, regs[i].def);
2068 if (regs[i].delay_us)
2069 udelay(regs[i].delay_us);
2074 if (!map->format.parse_inplace)
2077 if (map->writeable_reg)
2078 for (i = 0; i < num_regs; i++) {
2079 int reg = regs[i].reg;
2080 if (!map->writeable_reg(map->dev, reg))
2082 if (!IS_ALIGNED(reg, map->reg_stride))
2086 if (!map->cache_bypass) {
2087 for (i = 0; i < num_regs; i++) {
2088 unsigned int val = regs[i].def;
2089 unsigned int reg = regs[i].reg;
2090 ret = regcache_write(map, reg, val);
2093 "Error in caching of register: %x ret: %d\n",
2098 if (map->cache_only) {
2099 map->cache_dirty = true;
2106 for (i = 0; i < num_regs; i++) {
2107 unsigned int reg = regs[i].reg;
2108 struct regmap_range_node *range;
2110 /* Coalesce all the writes between a page break or a delay
2113 range = _regmap_range_lookup(map, reg);
2114 if (range || regs[i].delay_us) {
2115 size_t len = sizeof(struct reg_sequence)*num_regs;
2116 struct reg_sequence *base = kmemdup(regs, len,
2120 ret = _regmap_range_multi_paged_reg_write(map, base,
2127 return _regmap_raw_multi_reg_write(map, regs, num_regs);
2131 * regmap_multi_reg_write(): Write multiple registers to the device
2133 * where the set of register,value pairs are supplied in any order,
2134 * possibly not all in a single range.
2136 * @map: Register map to write to
2137 * @regs: Array of structures containing register,value to be written
2138 * @num_regs: Number of registers to write
2140 * The 'normal' block write mode will send ultimately send data on the
2141 * target bus as R,V1,V2,V3,..,Vn where successively higer registers are
2142 * addressed. However, this alternative block multi write mode will send
2143 * the data as R1,V1,R2,V2,..,Rn,Vn on the target bus. The target device
2144 * must of course support the mode.
2146 * A value of zero will be returned on success, a negative errno will be
2147 * returned in error cases.
2149 int regmap_multi_reg_write(struct regmap *map, const struct reg_sequence *regs,
2154 map->lock(map->lock_arg);
2156 ret = _regmap_multi_reg_write(map, regs, num_regs);
2158 map->unlock(map->lock_arg);
2162 EXPORT_SYMBOL_GPL(regmap_multi_reg_write);
2165 * regmap_multi_reg_write_bypassed(): Write multiple registers to the
2166 * device but not the cache
2168 * where the set of register are supplied in any order
2170 * @map: Register map to write to
2171 * @regs: Array of structures containing register,value to be written
2172 * @num_regs: Number of registers to write
2174 * This function is intended to be used for writing a large block of data
2175 * atomically to the device in single transfer for those I2C client devices
2176 * that implement this alternative block write mode.
2178 * A value of zero will be returned on success, a negative errno will
2179 * be returned in error cases.
2181 int regmap_multi_reg_write_bypassed(struct regmap *map,
2182 const struct reg_sequence *regs,
2188 map->lock(map->lock_arg);
2190 bypass = map->cache_bypass;
2191 map->cache_bypass = true;
2193 ret = _regmap_multi_reg_write(map, regs, num_regs);
2195 map->cache_bypass = bypass;
2197 map->unlock(map->lock_arg);
2201 EXPORT_SYMBOL_GPL(regmap_multi_reg_write_bypassed);
2204 * regmap_raw_write_async(): Write raw values to one or more registers
2207 * @map: Register map to write to
2208 * @reg: Initial register to write to
2209 * @val: Block of data to be written, laid out for direct transmission to the
2210 * device. Must be valid until regmap_async_complete() is called.
2211 * @val_len: Length of data pointed to by val.
2213 * This function is intended to be used for things like firmware
2214 * download where a large block of data needs to be transferred to the
2215 * device. No formatting will be done on the data provided.
2217 * If supported by the underlying bus the write will be scheduled
2218 * asynchronously, helping maximise I/O speed on higher speed buses
2219 * like SPI. regmap_async_complete() can be called to ensure that all
2220 * asynchrnous writes have been completed.
2222 * A value of zero will be returned on success, a negative errno will
2223 * be returned in error cases.
2225 int regmap_raw_write_async(struct regmap *map, unsigned int reg,
2226 const void *val, size_t val_len)
2230 if (val_len % map->format.val_bytes)
2232 if (!IS_ALIGNED(reg, map->reg_stride))
2235 map->lock(map->lock_arg);
2239 ret = _regmap_raw_write(map, reg, val, val_len);
2243 map->unlock(map->lock_arg);
2247 EXPORT_SYMBOL_GPL(regmap_raw_write_async);
2249 static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
2250 unsigned int val_len)
2252 struct regmap_range_node *range;
2253 u8 *u8 = map->work_buf;
2258 if (!map->bus || !map->bus->read)
2261 range = _regmap_range_lookup(map, reg);
2263 ret = _regmap_select_page(map, ®, range,
2264 val_len / map->format.val_bytes);
2269 map->format.format_reg(map->work_buf, reg, map->reg_shift);
2272 * Some buses or devices flag reads by setting the high bits in the
2273 * register address; since it's always the high bits for all
2274 * current formats we can do this here rather than in
2275 * formatting. This may break if we get interesting formats.
2277 u8[0] |= map->read_flag_mask;
2279 trace_regmap_hw_read_start(map, reg, val_len / map->format.val_bytes);
2281 ret = map->bus->read(map->bus_context, map->work_buf,
2282 map->format.reg_bytes + map->format.pad_bytes,
2285 trace_regmap_hw_read_done(map, reg, val_len / map->format.val_bytes);
2290 static int _regmap_bus_reg_read(void *context, unsigned int reg,
2293 struct regmap *map = context;
2295 return map->bus->reg_read(map->bus_context, reg, val);
2298 static int _regmap_bus_read(void *context, unsigned int reg,
2302 struct regmap *map = context;
2304 if (!map->format.parse_val)
2307 ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
2309 *val = map->format.parse_val(map->work_buf);
2314 static int _regmap_read(struct regmap *map, unsigned int reg,
2318 void *context = _regmap_map_get_context(map);
2320 if (!map->cache_bypass) {
2321 ret = regcache_read(map, reg, val);
2326 if (map->cache_only)
2329 if (!regmap_readable(map, reg))
2332 ret = map->reg_read(context, reg, val);
2335 if (map->dev && strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
2336 dev_info(map->dev, "%x => %x\n", reg, *val);
2339 trace_regmap_reg_read(map, reg, *val);
2341 if (!map->cache_bypass)
2342 regcache_write(map, reg, *val);
2349 * regmap_read(): Read a value from a single register
2351 * @map: Register map to read from
2352 * @reg: Register to be read from
2353 * @val: Pointer to store read value
2355 * A value of zero will be returned on success, a negative errno will
2356 * be returned in error cases.
2358 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
2362 if (!IS_ALIGNED(reg, map->reg_stride))
2365 map->lock(map->lock_arg);
2367 ret = _regmap_read(map, reg, val);
2369 map->unlock(map->lock_arg);
2373 EXPORT_SYMBOL_GPL(regmap_read);
2376 * regmap_raw_read(): Read raw data from the device
2378 * @map: Register map to read from
2379 * @reg: First register to be read from
2380 * @val: Pointer to store read value
2381 * @val_len: Size of data to read
2383 * A value of zero will be returned on success, a negative errno will
2384 * be returned in error cases.
2386 int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
2389 size_t val_bytes = map->format.val_bytes;
2390 size_t val_count = val_len / val_bytes;
2396 if (val_len % map->format.val_bytes)
2398 if (!IS_ALIGNED(reg, map->reg_stride))
2403 map->lock(map->lock_arg);
2405 if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
2406 map->cache_type == REGCACHE_NONE) {
2407 if (!map->bus->read) {
2411 if (map->max_raw_read && map->max_raw_read < val_len) {
2416 /* Physical block read if there's no cache involved */
2417 ret = _regmap_raw_read(map, reg, val, val_len);
2420 /* Otherwise go word by word for the cache; should be low
2421 * cost as we expect to hit the cache.
2423 for (i = 0; i < val_count; i++) {
2424 ret = _regmap_read(map, reg + (i * map->reg_stride),
2429 map->format.format_val(val + (i * val_bytes), v, 0);
2434 map->unlock(map->lock_arg);
2438 EXPORT_SYMBOL_GPL(regmap_raw_read);
2441 * regmap_field_read(): Read a value to a single register field
2443 * @field: Register field to read from
2444 * @val: Pointer to store read value
2446 * A value of zero will be returned on success, a negative errno will
2447 * be returned in error cases.
2449 int regmap_field_read(struct regmap_field *field, unsigned int *val)
2452 unsigned int reg_val;
2453 ret = regmap_read(field->regmap, field->reg, ®_val);
2457 reg_val &= field->mask;
2458 reg_val >>= field->shift;
2463 EXPORT_SYMBOL_GPL(regmap_field_read);
2466 * regmap_fields_read(): Read a value to a single register field with port ID
2468 * @field: Register field to read from
2470 * @val: Pointer to store read value
2472 * A value of zero will be returned on success, a negative errno will
2473 * be returned in error cases.
2475 int regmap_fields_read(struct regmap_field *field, unsigned int id,
2479 unsigned int reg_val;
2481 if (id >= field->id_size)
2484 ret = regmap_read(field->regmap,
2485 field->reg + (field->id_offset * id),
2490 reg_val &= field->mask;
2491 reg_val >>= field->shift;
2496 EXPORT_SYMBOL_GPL(regmap_fields_read);
2499 * regmap_bulk_read(): Read multiple registers from the device
2501 * @map: Register map to read from
2502 * @reg: First register to be read from
2503 * @val: Pointer to store read value, in native register size for device
2504 * @val_count: Number of registers to read
2506 * A value of zero will be returned on success, a negative errno will
2507 * be returned in error cases.
2509 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
2513 size_t val_bytes = map->format.val_bytes;
2514 bool vol = regmap_volatile_range(map, reg, val_count);
2516 if (!IS_ALIGNED(reg, map->reg_stride))
2519 if (map->bus && map->format.parse_inplace && (vol || map->cache_type == REGCACHE_NONE)) {
2521 * Some devices does not support bulk read, for
2522 * them we have a series of single read operations.
2524 size_t total_size = val_bytes * val_count;
2526 if (!map->use_single_read &&
2527 (!map->max_raw_read || map->max_raw_read > total_size)) {
2528 ret = regmap_raw_read(map, reg, val,
2529 val_bytes * val_count);
2534 * Some devices do not support bulk read or do not
2535 * support large bulk reads, for them we have a series
2536 * of read operations.
2538 int chunk_stride = map->reg_stride;
2539 size_t chunk_size = val_bytes;
2540 size_t chunk_count = val_count;
2542 if (!map->use_single_read) {
2543 chunk_size = map->max_raw_read;
2544 if (chunk_size % val_bytes)
2545 chunk_size -= chunk_size % val_bytes;
2546 chunk_count = total_size / chunk_size;
2547 chunk_stride *= chunk_size / val_bytes;
2550 /* Read bytes that fit into a multiple of chunk_size */
2551 for (i = 0; i < chunk_count; i++) {
2552 ret = regmap_raw_read(map,
2553 reg + (i * chunk_stride),
2554 val + (i * chunk_size),
2560 /* Read remaining bytes */
2561 if (chunk_size * i < total_size) {
2562 ret = regmap_raw_read(map,
2563 reg + (i * chunk_stride),
2564 val + (i * chunk_size),
2565 total_size - i * chunk_size);
2571 for (i = 0; i < val_count * val_bytes; i += val_bytes)
2572 map->format.parse_inplace(val + i);
2574 for (i = 0; i < val_count; i++) {
2576 ret = regmap_read(map, reg + (i * map->reg_stride),
2581 if (map->format.format_val) {
2582 map->format.format_val(val + (i * val_bytes), ival, 0);
2584 /* Devices providing read and write
2585 * operations can use the bulk I/O
2586 * functions if they define a val_bytes,
2587 * we assume that the values are native
2597 switch (map->format.val_bytes) {
2621 EXPORT_SYMBOL_GPL(regmap_bulk_read);
2623 static int _regmap_update_bits(struct regmap *map, unsigned int reg,
2624 unsigned int mask, unsigned int val,
2625 bool *change, bool force_write)
2628 unsigned int tmp, orig;
2633 if (regmap_volatile(map, reg) && map->reg_update_bits) {
2634 ret = map->reg_update_bits(map->bus_context, reg, mask, val);
2635 if (ret == 0 && change)
2638 ret = _regmap_read(map, reg, &orig);
2645 if (force_write || (tmp != orig)) {
2646 ret = _regmap_write(map, reg, tmp);
2647 if (ret == 0 && change)
2656 * regmap_update_bits: Perform a read/modify/write cycle on the register map
2658 * @map: Register map to update
2659 * @reg: Register to update
2660 * @mask: Bitmask to change
2661 * @val: New value for bitmask
2663 * Returns zero for success, a negative number on error.
2665 int regmap_update_bits(struct regmap *map, unsigned int reg,
2666 unsigned int mask, unsigned int val)
2670 map->lock(map->lock_arg);
2671 ret = _regmap_update_bits(map, reg, mask, val, NULL, false);
2672 map->unlock(map->lock_arg);
2676 EXPORT_SYMBOL_GPL(regmap_update_bits);
2679 * regmap_write_bits: Perform a read/modify/write cycle on the register map
2681 * @map: Register map to update
2682 * @reg: Register to update
2683 * @mask: Bitmask to change
2684 * @val: New value for bitmask
2686 * Returns zero for success, a negative number on error.
2688 int regmap_write_bits(struct regmap *map, unsigned int reg,
2689 unsigned int mask, unsigned int val)
2693 map->lock(map->lock_arg);
2694 ret = _regmap_update_bits(map, reg, mask, val, NULL, true);
2695 map->unlock(map->lock_arg);
2699 EXPORT_SYMBOL_GPL(regmap_write_bits);
2702 * regmap_update_bits_async: Perform a read/modify/write cycle on the register
2703 * map asynchronously
2705 * @map: Register map to update
2706 * @reg: Register to update
2707 * @mask: Bitmask to change
2708 * @val: New value for bitmask
2710 * With most buses the read must be done synchronously so this is most
2711 * useful for devices with a cache which do not need to interact with
2712 * the hardware to determine the current register value.
2714 * Returns zero for success, a negative number on error.
2716 int regmap_update_bits_async(struct regmap *map, unsigned int reg,
2717 unsigned int mask, unsigned int val)
2721 map->lock(map->lock_arg);
2725 ret = _regmap_update_bits(map, reg, mask, val, NULL, false);
2729 map->unlock(map->lock_arg);
2733 EXPORT_SYMBOL_GPL(regmap_update_bits_async);
2736 * regmap_update_bits_check: Perform a read/modify/write cycle on the
2737 * register map and report if updated
2739 * @map: Register map to update
2740 * @reg: Register to update
2741 * @mask: Bitmask to change
2742 * @val: New value for bitmask
2743 * @change: Boolean indicating if a write was done
2745 * Returns zero for success, a negative number on error.
2747 int regmap_update_bits_check(struct regmap *map, unsigned int reg,
2748 unsigned int mask, unsigned int val,
2753 map->lock(map->lock_arg);
2754 ret = _regmap_update_bits(map, reg, mask, val, change, false);
2755 map->unlock(map->lock_arg);
2758 EXPORT_SYMBOL_GPL(regmap_update_bits_check);
2761 * regmap_update_bits_check_async: Perform a read/modify/write cycle on the
2762 * register map asynchronously and report if
2765 * @map: Register map to update
2766 * @reg: Register to update
2767 * @mask: Bitmask to change
2768 * @val: New value for bitmask
2769 * @change: Boolean indicating if a write was done
2771 * With most buses the read must be done synchronously so this is most
2772 * useful for devices with a cache which do not need to interact with
2773 * the hardware to determine the current register value.
2775 * Returns zero for success, a negative number on error.
2777 int regmap_update_bits_check_async(struct regmap *map, unsigned int reg,
2778 unsigned int mask, unsigned int val,
2783 map->lock(map->lock_arg);
2787 ret = _regmap_update_bits(map, reg, mask, val, change, false);
2791 map->unlock(map->lock_arg);
2795 EXPORT_SYMBOL_GPL(regmap_update_bits_check_async);
2797 void regmap_async_complete_cb(struct regmap_async *async, int ret)
2799 struct regmap *map = async->map;
2802 trace_regmap_async_io_complete(map);
2804 spin_lock(&map->async_lock);
2805 list_move(&async->list, &map->async_free);
2806 wake = list_empty(&map->async_list);
2809 map->async_ret = ret;
2811 spin_unlock(&map->async_lock);
2814 wake_up(&map->async_waitq);
2816 EXPORT_SYMBOL_GPL(regmap_async_complete_cb);
2818 static int regmap_async_is_done(struct regmap *map)
2820 unsigned long flags;
2823 spin_lock_irqsave(&map->async_lock, flags);
2824 ret = list_empty(&map->async_list);
2825 spin_unlock_irqrestore(&map->async_lock, flags);
2831 * regmap_async_complete: Ensure all asynchronous I/O has completed.
2833 * @map: Map to operate on.
2835 * Blocks until any pending asynchronous I/O has completed. Returns
2836 * an error code for any failed I/O operations.
2838 int regmap_async_complete(struct regmap *map)
2840 unsigned long flags;
2843 /* Nothing to do with no async support */
2844 if (!map->bus || !map->bus->async_write)
2847 trace_regmap_async_complete_start(map);
2849 wait_event(map->async_waitq, regmap_async_is_done(map));
2851 spin_lock_irqsave(&map->async_lock, flags);
2852 ret = map->async_ret;
2854 spin_unlock_irqrestore(&map->async_lock, flags);
2856 trace_regmap_async_complete_done(map);
2860 EXPORT_SYMBOL_GPL(regmap_async_complete);
2863 * regmap_register_patch: Register and apply register updates to be applied
2864 * on device initialistion
2866 * @map: Register map to apply updates to.
2867 * @regs: Values to update.
2868 * @num_regs: Number of entries in regs.
2870 * Register a set of register updates to be applied to the device
2871 * whenever the device registers are synchronised with the cache and
2872 * apply them immediately. Typically this is used to apply
2873 * corrections to be applied to the device defaults on startup, such
2874 * as the updates some vendors provide to undocumented registers.
2876 * The caller must ensure that this function cannot be called
2877 * concurrently with either itself or regcache_sync().
2879 int regmap_register_patch(struct regmap *map, const struct reg_sequence *regs,
2882 struct reg_sequence *p;
2886 if (WARN_ONCE(num_regs <= 0, "invalid registers number (%d)\n",
2890 p = krealloc(map->patch,
2891 sizeof(struct reg_sequence) * (map->patch_regs + num_regs),
2894 memcpy(p + map->patch_regs, regs, num_regs * sizeof(*regs));
2896 map->patch_regs += num_regs;
2901 map->lock(map->lock_arg);
2903 bypass = map->cache_bypass;
2905 map->cache_bypass = true;
2908 ret = _regmap_multi_reg_write(map, regs, num_regs);
2911 map->cache_bypass = bypass;
2913 map->unlock(map->lock_arg);
2915 regmap_async_complete(map);
2919 EXPORT_SYMBOL_GPL(regmap_register_patch);
2922 * regmap_get_val_bytes(): Report the size of a register value
2924 * Report the size of a register value, mainly intended to for use by
2925 * generic infrastructure built on top of regmap.
2927 int regmap_get_val_bytes(struct regmap *map)
2929 if (map->format.format_write)
2932 return map->format.val_bytes;
2934 EXPORT_SYMBOL_GPL(regmap_get_val_bytes);
2937 * regmap_get_max_register(): Report the max register value
2939 * Report the max register value, mainly intended to for use by
2940 * generic infrastructure built on top of regmap.
2942 int regmap_get_max_register(struct regmap *map)
2944 return map->max_register ? map->max_register : -EINVAL;
2946 EXPORT_SYMBOL_GPL(regmap_get_max_register);
2949 * regmap_get_reg_stride(): Report the register address stride
2951 * Report the register address stride, mainly intended to for use by
2952 * generic infrastructure built on top of regmap.
2954 int regmap_get_reg_stride(struct regmap *map)
2956 return map->reg_stride;
2958 EXPORT_SYMBOL_GPL(regmap_get_reg_stride);
2960 int regmap_parse_val(struct regmap *map, const void *buf,
2963 if (!map->format.parse_val)
2966 *val = map->format.parse_val(buf);
2970 EXPORT_SYMBOL_GPL(regmap_parse_val);
2972 static int __init regmap_initcall(void)
2974 regmap_debugfs_initcall();
2978 postcore_initcall(regmap_initcall);