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>
22 #define CREATE_TRACE_POINTS
28 * Sometimes for failures during very early init the trace
29 * infrastructure isn't available early enough to be used. For this
30 * sort of problem defining LOG_DEVICE will add printks for basic
31 * register I/O on a specific device.
35 static int _regmap_update_bits(struct regmap *map, unsigned int reg,
36 unsigned int mask, unsigned int val,
39 static int _regmap_bus_reg_read(void *context, unsigned int reg,
41 static int _regmap_bus_read(void *context, unsigned int reg,
43 static int _regmap_bus_formatted_write(void *context, unsigned int reg,
45 static int _regmap_bus_reg_write(void *context, unsigned int reg,
47 static int _regmap_bus_raw_write(void *context, unsigned int reg,
50 bool regmap_reg_in_ranges(unsigned int reg,
51 const struct regmap_range *ranges,
54 const struct regmap_range *r;
57 for (i = 0, r = ranges; i < nranges; i++, r++)
58 if (regmap_reg_in_range(reg, r))
62 EXPORT_SYMBOL_GPL(regmap_reg_in_ranges);
64 bool regmap_check_range_table(struct regmap *map, unsigned int reg,
65 const struct regmap_access_table *table)
67 /* Check "no ranges" first */
68 if (regmap_reg_in_ranges(reg, table->no_ranges, table->n_no_ranges))
71 /* In case zero "yes ranges" are supplied, any reg is OK */
72 if (!table->n_yes_ranges)
75 return regmap_reg_in_ranges(reg, table->yes_ranges,
78 EXPORT_SYMBOL_GPL(regmap_check_range_table);
80 bool regmap_writeable(struct regmap *map, unsigned int reg)
82 if (map->max_register && reg > map->max_register)
85 if (map->writeable_reg)
86 return map->writeable_reg(map->dev, reg);
89 return regmap_check_range_table(map, reg, map->wr_table);
94 bool regmap_readable(struct regmap *map, unsigned int reg)
96 if (map->max_register && reg > map->max_register)
99 if (map->format.format_write)
102 if (map->readable_reg)
103 return map->readable_reg(map->dev, reg);
106 return regmap_check_range_table(map, reg, map->rd_table);
111 bool regmap_volatile(struct regmap *map, unsigned int reg)
113 if (!map->format.format_write && !regmap_readable(map, reg))
116 if (map->volatile_reg)
117 return map->volatile_reg(map->dev, reg);
119 if (map->volatile_table)
120 return regmap_check_range_table(map, reg, map->volatile_table);
128 bool regmap_precious(struct regmap *map, unsigned int reg)
130 if (!regmap_readable(map, reg))
133 if (map->precious_reg)
134 return map->precious_reg(map->dev, reg);
136 if (map->precious_table)
137 return regmap_check_range_table(map, reg, map->precious_table);
142 static bool regmap_volatile_range(struct regmap *map, unsigned int reg,
147 for (i = 0; i < num; i++)
148 if (!regmap_volatile(map, reg + i))
154 static void regmap_format_2_6_write(struct regmap *map,
155 unsigned int reg, unsigned int val)
157 u8 *out = map->work_buf;
159 *out = (reg << 6) | val;
162 static void regmap_format_4_12_write(struct regmap *map,
163 unsigned int reg, unsigned int val)
165 __be16 *out = map->work_buf;
166 *out = cpu_to_be16((reg << 12) | val);
169 static void regmap_format_7_9_write(struct regmap *map,
170 unsigned int reg, unsigned int val)
172 __be16 *out = map->work_buf;
173 *out = cpu_to_be16((reg << 9) | val);
176 static void regmap_format_10_14_write(struct regmap *map,
177 unsigned int reg, unsigned int val)
179 u8 *out = map->work_buf;
182 out[1] = (val >> 8) | (reg << 6);
186 static void regmap_format_8(void *buf, unsigned int val, unsigned int shift)
193 static void regmap_format_16_be(void *buf, unsigned int val, unsigned int shift)
197 b[0] = cpu_to_be16(val << shift);
200 static void regmap_format_16_le(void *buf, unsigned int val, unsigned int shift)
204 b[0] = cpu_to_le16(val << shift);
207 static void regmap_format_16_native(void *buf, unsigned int val,
210 *(u16 *)buf = val << shift;
213 static void regmap_format_24(void *buf, unsigned int val, unsigned int shift)
224 static void regmap_format_32_be(void *buf, unsigned int val, unsigned int shift)
228 b[0] = cpu_to_be32(val << shift);
231 static void regmap_format_32_le(void *buf, unsigned int val, unsigned int shift)
235 b[0] = cpu_to_le32(val << shift);
238 static void regmap_format_32_native(void *buf, unsigned int val,
241 *(u32 *)buf = val << shift;
244 static void regmap_parse_inplace_noop(void *buf)
248 static unsigned int regmap_parse_8(const void *buf)
255 static unsigned int regmap_parse_16_be(const void *buf)
257 const __be16 *b = buf;
259 return be16_to_cpu(b[0]);
262 static unsigned int regmap_parse_16_le(const void *buf)
264 const __le16 *b = buf;
266 return le16_to_cpu(b[0]);
269 static void regmap_parse_16_be_inplace(void *buf)
273 b[0] = be16_to_cpu(b[0]);
276 static void regmap_parse_16_le_inplace(void *buf)
280 b[0] = le16_to_cpu(b[0]);
283 static unsigned int regmap_parse_16_native(const void *buf)
288 static unsigned int regmap_parse_24(const void *buf)
291 unsigned int ret = b[2];
292 ret |= ((unsigned int)b[1]) << 8;
293 ret |= ((unsigned int)b[0]) << 16;
298 static unsigned int regmap_parse_32_be(const void *buf)
300 const __be32 *b = buf;
302 return be32_to_cpu(b[0]);
305 static unsigned int regmap_parse_32_le(const void *buf)
307 const __le32 *b = buf;
309 return le32_to_cpu(b[0]);
312 static void regmap_parse_32_be_inplace(void *buf)
316 b[0] = be32_to_cpu(b[0]);
319 static void regmap_parse_32_le_inplace(void *buf)
323 b[0] = le32_to_cpu(b[0]);
326 static unsigned int regmap_parse_32_native(const void *buf)
331 static void regmap_lock_mutex(void *__map)
333 struct regmap *map = __map;
334 mutex_lock(&map->mutex);
337 static void regmap_unlock_mutex(void *__map)
339 struct regmap *map = __map;
340 mutex_unlock(&map->mutex);
343 static void regmap_lock_spinlock(void *__map)
344 __acquires(&map->spinlock)
346 struct regmap *map = __map;
349 spin_lock_irqsave(&map->spinlock, flags);
350 map->spinlock_flags = flags;
353 static void regmap_unlock_spinlock(void *__map)
354 __releases(&map->spinlock)
356 struct regmap *map = __map;
357 spin_unlock_irqrestore(&map->spinlock, map->spinlock_flags);
360 static void dev_get_regmap_release(struct device *dev, void *res)
363 * We don't actually have anything to do here; the goal here
364 * is not to manage the regmap but to provide a simple way to
365 * get the regmap back given a struct device.
369 static bool _regmap_range_add(struct regmap *map,
370 struct regmap_range_node *data)
372 struct rb_root *root = &map->range_tree;
373 struct rb_node **new = &(root->rb_node), *parent = NULL;
376 struct regmap_range_node *this =
377 container_of(*new, struct regmap_range_node, node);
380 if (data->range_max < this->range_min)
381 new = &((*new)->rb_left);
382 else if (data->range_min > this->range_max)
383 new = &((*new)->rb_right);
388 rb_link_node(&data->node, parent, new);
389 rb_insert_color(&data->node, root);
394 static struct regmap_range_node *_regmap_range_lookup(struct regmap *map,
397 struct rb_node *node = map->range_tree.rb_node;
400 struct regmap_range_node *this =
401 container_of(node, struct regmap_range_node, node);
403 if (reg < this->range_min)
404 node = node->rb_left;
405 else if (reg > this->range_max)
406 node = node->rb_right;
414 static void regmap_range_exit(struct regmap *map)
416 struct rb_node *next;
417 struct regmap_range_node *range_node;
419 next = rb_first(&map->range_tree);
421 range_node = rb_entry(next, struct regmap_range_node, node);
422 next = rb_next(&range_node->node);
423 rb_erase(&range_node->node, &map->range_tree);
427 kfree(map->selector_work_buf);
430 int regmap_attach_dev(struct device *dev, struct regmap *map,
431 const struct regmap_config *config)
437 regmap_debugfs_init(map, config->name);
439 /* Add a devres resource for dev_get_regmap() */
440 m = devres_alloc(dev_get_regmap_release, sizeof(*m), GFP_KERNEL);
442 regmap_debugfs_exit(map);
450 EXPORT_SYMBOL_GPL(regmap_attach_dev);
452 static enum regmap_endian regmap_get_reg_endian(const struct regmap_bus *bus,
453 const struct regmap_config *config)
455 enum regmap_endian endian;
457 /* Retrieve the endianness specification from the regmap config */
458 endian = config->reg_format_endian;
460 /* If the regmap config specified a non-default value, use that */
461 if (endian != REGMAP_ENDIAN_DEFAULT)
464 /* Retrieve the endianness specification from the bus config */
465 if (bus && bus->reg_format_endian_default)
466 endian = bus->reg_format_endian_default;
468 /* If the bus specified a non-default value, use that */
469 if (endian != REGMAP_ENDIAN_DEFAULT)
472 /* Use this if no other value was found */
473 return REGMAP_ENDIAN_BIG;
476 enum regmap_endian regmap_get_val_endian(struct device *dev,
477 const struct regmap_bus *bus,
478 const struct regmap_config *config)
480 struct device_node *np;
481 enum regmap_endian endian;
483 /* Retrieve the endianness specification from the regmap config */
484 endian = config->val_format_endian;
486 /* If the regmap config specified a non-default value, use that */
487 if (endian != REGMAP_ENDIAN_DEFAULT)
490 /* If the dev and dev->of_node exist try to get endianness from DT */
491 if (dev && dev->of_node) {
494 /* Parse the device's DT node for an endianness specification */
495 if (of_property_read_bool(np, "big-endian"))
496 endian = REGMAP_ENDIAN_BIG;
497 else if (of_property_read_bool(np, "little-endian"))
498 endian = REGMAP_ENDIAN_LITTLE;
500 /* If the endianness was specified in DT, use that */
501 if (endian != REGMAP_ENDIAN_DEFAULT)
505 /* Retrieve the endianness specification from the bus config */
506 if (bus && bus->val_format_endian_default)
507 endian = bus->val_format_endian_default;
509 /* If the bus specified a non-default value, use that */
510 if (endian != REGMAP_ENDIAN_DEFAULT)
513 /* Use this if no other value was found */
514 return REGMAP_ENDIAN_BIG;
516 EXPORT_SYMBOL_GPL(regmap_get_val_endian);
519 * regmap_init(): Initialise register map
521 * @dev: Device that will be interacted with
522 * @bus: Bus-specific callbacks to use with device
523 * @bus_context: Data passed to bus-specific callbacks
524 * @config: Configuration for register map
526 * The return value will be an ERR_PTR() on error or a valid pointer to
527 * a struct regmap. This function should generally not be called
528 * directly, it should be called by bus-specific init functions.
530 struct regmap *__regmap_init(struct device *dev,
531 const struct regmap_bus *bus,
533 const struct regmap_config *config,
534 struct lock_class_key *lock_key,
535 const char *lock_name)
539 enum regmap_endian reg_endian, val_endian;
545 map = kzalloc(sizeof(*map), GFP_KERNEL);
551 if (config->lock && config->unlock) {
552 map->lock = config->lock;
553 map->unlock = config->unlock;
554 map->lock_arg = config->lock_arg;
556 if ((bus && bus->fast_io) ||
558 spin_lock_init(&map->spinlock);
559 map->lock = regmap_lock_spinlock;
560 map->unlock = regmap_unlock_spinlock;
561 lockdep_set_class_and_name(&map->spinlock,
562 lock_key, lock_name);
564 mutex_init(&map->mutex);
565 map->lock = regmap_lock_mutex;
566 map->unlock = regmap_unlock_mutex;
567 lockdep_set_class_and_name(&map->mutex,
568 lock_key, lock_name);
572 map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8);
573 map->format.pad_bytes = config->pad_bits / 8;
574 map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8);
575 map->format.buf_size = DIV_ROUND_UP(config->reg_bits +
576 config->val_bits + config->pad_bits, 8);
577 map->reg_shift = config->pad_bits % 8;
578 if (config->reg_stride)
579 map->reg_stride = config->reg_stride;
582 map->use_single_rw = config->use_single_rw;
583 map->can_multi_write = config->can_multi_write;
586 map->bus_context = bus_context;
587 map->max_register = config->max_register;
588 map->wr_table = config->wr_table;
589 map->rd_table = config->rd_table;
590 map->volatile_table = config->volatile_table;
591 map->precious_table = config->precious_table;
592 map->writeable_reg = config->writeable_reg;
593 map->readable_reg = config->readable_reg;
594 map->volatile_reg = config->volatile_reg;
595 map->precious_reg = config->precious_reg;
596 map->cache_type = config->cache_type;
597 map->name = config->name;
599 spin_lock_init(&map->async_lock);
600 INIT_LIST_HEAD(&map->async_list);
601 INIT_LIST_HEAD(&map->async_free);
602 init_waitqueue_head(&map->async_waitq);
604 if (config->read_flag_mask || config->write_flag_mask) {
605 map->read_flag_mask = config->read_flag_mask;
606 map->write_flag_mask = config->write_flag_mask;
608 map->read_flag_mask = bus->read_flag_mask;
612 map->reg_read = config->reg_read;
613 map->reg_write = config->reg_write;
615 map->defer_caching = false;
616 goto skip_format_initialization;
617 } else if (!bus->read || !bus->write) {
618 map->reg_read = _regmap_bus_reg_read;
619 map->reg_write = _regmap_bus_reg_write;
621 map->defer_caching = false;
622 goto skip_format_initialization;
624 map->reg_read = _regmap_bus_read;
627 reg_endian = regmap_get_reg_endian(bus, config);
628 val_endian = regmap_get_val_endian(dev, bus, config);
630 switch (config->reg_bits + map->reg_shift) {
632 switch (config->val_bits) {
634 map->format.format_write = regmap_format_2_6_write;
642 switch (config->val_bits) {
644 map->format.format_write = regmap_format_4_12_write;
652 switch (config->val_bits) {
654 map->format.format_write = regmap_format_7_9_write;
662 switch (config->val_bits) {
664 map->format.format_write = regmap_format_10_14_write;
672 map->format.format_reg = regmap_format_8;
676 switch (reg_endian) {
677 case REGMAP_ENDIAN_BIG:
678 map->format.format_reg = regmap_format_16_be;
680 case REGMAP_ENDIAN_NATIVE:
681 map->format.format_reg = regmap_format_16_native;
689 if (reg_endian != REGMAP_ENDIAN_BIG)
691 map->format.format_reg = regmap_format_24;
695 switch (reg_endian) {
696 case REGMAP_ENDIAN_BIG:
697 map->format.format_reg = regmap_format_32_be;
699 case REGMAP_ENDIAN_NATIVE:
700 map->format.format_reg = regmap_format_32_native;
711 if (val_endian == REGMAP_ENDIAN_NATIVE)
712 map->format.parse_inplace = regmap_parse_inplace_noop;
714 switch (config->val_bits) {
716 map->format.format_val = regmap_format_8;
717 map->format.parse_val = regmap_parse_8;
718 map->format.parse_inplace = regmap_parse_inplace_noop;
721 switch (val_endian) {
722 case REGMAP_ENDIAN_BIG:
723 map->format.format_val = regmap_format_16_be;
724 map->format.parse_val = regmap_parse_16_be;
725 map->format.parse_inplace = regmap_parse_16_be_inplace;
727 case REGMAP_ENDIAN_LITTLE:
728 map->format.format_val = regmap_format_16_le;
729 map->format.parse_val = regmap_parse_16_le;
730 map->format.parse_inplace = regmap_parse_16_le_inplace;
732 case REGMAP_ENDIAN_NATIVE:
733 map->format.format_val = regmap_format_16_native;
734 map->format.parse_val = regmap_parse_16_native;
741 if (val_endian != REGMAP_ENDIAN_BIG)
743 map->format.format_val = regmap_format_24;
744 map->format.parse_val = regmap_parse_24;
747 switch (val_endian) {
748 case REGMAP_ENDIAN_BIG:
749 map->format.format_val = regmap_format_32_be;
750 map->format.parse_val = regmap_parse_32_be;
751 map->format.parse_inplace = regmap_parse_32_be_inplace;
753 case REGMAP_ENDIAN_LITTLE:
754 map->format.format_val = regmap_format_32_le;
755 map->format.parse_val = regmap_parse_32_le;
756 map->format.parse_inplace = regmap_parse_32_le_inplace;
758 case REGMAP_ENDIAN_NATIVE:
759 map->format.format_val = regmap_format_32_native;
760 map->format.parse_val = regmap_parse_32_native;
768 if (map->format.format_write) {
769 if ((reg_endian != REGMAP_ENDIAN_BIG) ||
770 (val_endian != REGMAP_ENDIAN_BIG))
772 map->use_single_rw = true;
775 if (!map->format.format_write &&
776 !(map->format.format_reg && map->format.format_val))
779 map->work_buf = kzalloc(map->format.buf_size, GFP_KERNEL);
780 if (map->work_buf == NULL) {
785 if (map->format.format_write) {
786 map->defer_caching = false;
787 map->reg_write = _regmap_bus_formatted_write;
788 } else if (map->format.format_val) {
789 map->defer_caching = true;
790 map->reg_write = _regmap_bus_raw_write;
793 skip_format_initialization:
795 map->range_tree = RB_ROOT;
796 for (i = 0; i < config->num_ranges; i++) {
797 const struct regmap_range_cfg *range_cfg = &config->ranges[i];
798 struct regmap_range_node *new;
801 if (range_cfg->range_max < range_cfg->range_min) {
802 dev_err(map->dev, "Invalid range %d: %d < %d\n", i,
803 range_cfg->range_max, range_cfg->range_min);
807 if (range_cfg->range_max > map->max_register) {
808 dev_err(map->dev, "Invalid range %d: %d > %d\n", i,
809 range_cfg->range_max, map->max_register);
813 if (range_cfg->selector_reg > map->max_register) {
815 "Invalid range %d: selector out of map\n", i);
819 if (range_cfg->window_len == 0) {
820 dev_err(map->dev, "Invalid range %d: window_len 0\n",
825 /* Make sure, that this register range has no selector
826 or data window within its boundary */
827 for (j = 0; j < config->num_ranges; j++) {
828 unsigned sel_reg = config->ranges[j].selector_reg;
829 unsigned win_min = config->ranges[j].window_start;
830 unsigned win_max = win_min +
831 config->ranges[j].window_len - 1;
833 /* Allow data window inside its own virtual range */
837 if (range_cfg->range_min <= sel_reg &&
838 sel_reg <= range_cfg->range_max) {
840 "Range %d: selector for %d in window\n",
845 if (!(win_max < range_cfg->range_min ||
846 win_min > range_cfg->range_max)) {
848 "Range %d: window for %d in window\n",
854 new = kzalloc(sizeof(*new), GFP_KERNEL);
861 new->name = range_cfg->name;
862 new->range_min = range_cfg->range_min;
863 new->range_max = range_cfg->range_max;
864 new->selector_reg = range_cfg->selector_reg;
865 new->selector_mask = range_cfg->selector_mask;
866 new->selector_shift = range_cfg->selector_shift;
867 new->window_start = range_cfg->window_start;
868 new->window_len = range_cfg->window_len;
870 if (!_regmap_range_add(map, new)) {
871 dev_err(map->dev, "Failed to add range %d\n", i);
876 if (map->selector_work_buf == NULL) {
877 map->selector_work_buf =
878 kzalloc(map->format.buf_size, GFP_KERNEL);
879 if (map->selector_work_buf == NULL) {
886 ret = regcache_init(map, config);
891 ret = regmap_attach_dev(dev, map, config);
901 regmap_range_exit(map);
902 kfree(map->work_buf);
908 EXPORT_SYMBOL_GPL(__regmap_init);
910 static void devm_regmap_release(struct device *dev, void *res)
912 regmap_exit(*(struct regmap **)res);
916 * devm_regmap_init(): Initialise managed register map
918 * @dev: Device that will be interacted with
919 * @bus: Bus-specific callbacks to use with device
920 * @bus_context: Data passed to bus-specific callbacks
921 * @config: Configuration for register map
923 * The return value will be an ERR_PTR() on error or a valid pointer
924 * to a struct regmap. This function should generally not be called
925 * directly, it should be called by bus-specific init functions. The
926 * map will be automatically freed by the device management code.
928 struct regmap *__devm_regmap_init(struct device *dev,
929 const struct regmap_bus *bus,
931 const struct regmap_config *config,
932 struct lock_class_key *lock_key,
933 const char *lock_name)
935 struct regmap **ptr, *regmap;
937 ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL);
939 return ERR_PTR(-ENOMEM);
941 regmap = __regmap_init(dev, bus, bus_context, config,
942 lock_key, lock_name);
943 if (!IS_ERR(regmap)) {
945 devres_add(dev, ptr);
952 EXPORT_SYMBOL_GPL(__devm_regmap_init);
954 static void regmap_field_init(struct regmap_field *rm_field,
955 struct regmap *regmap, struct reg_field reg_field)
957 rm_field->regmap = regmap;
958 rm_field->reg = reg_field.reg;
959 rm_field->shift = reg_field.lsb;
960 rm_field->mask = GENMASK(reg_field.msb, reg_field.lsb);
961 rm_field->id_size = reg_field.id_size;
962 rm_field->id_offset = reg_field.id_offset;
966 * devm_regmap_field_alloc(): Allocate and initialise a register field
969 * @dev: Device that will be interacted with
970 * @regmap: regmap bank in which this register field is located.
971 * @reg_field: Register field with in the bank.
973 * The return value will be an ERR_PTR() on error or a valid pointer
974 * to a struct regmap_field. The regmap_field will be automatically freed
975 * by the device management code.
977 struct regmap_field *devm_regmap_field_alloc(struct device *dev,
978 struct regmap *regmap, struct reg_field reg_field)
980 struct regmap_field *rm_field = devm_kzalloc(dev,
981 sizeof(*rm_field), GFP_KERNEL);
983 return ERR_PTR(-ENOMEM);
985 regmap_field_init(rm_field, regmap, reg_field);
990 EXPORT_SYMBOL_GPL(devm_regmap_field_alloc);
993 * devm_regmap_field_free(): Free register field allocated using
994 * devm_regmap_field_alloc. Usally drivers need not call this function,
995 * as the memory allocated via devm will be freed as per device-driver
998 * @dev: Device that will be interacted with
999 * @field: regmap field which should be freed.
1001 void devm_regmap_field_free(struct device *dev,
1002 struct regmap_field *field)
1004 devm_kfree(dev, field);
1006 EXPORT_SYMBOL_GPL(devm_regmap_field_free);
1009 * regmap_field_alloc(): Allocate and initialise a register field
1010 * in a register map.
1012 * @regmap: regmap bank in which this register field is located.
1013 * @reg_field: Register field with in the bank.
1015 * The return value will be an ERR_PTR() on error or a valid pointer
1016 * to a struct regmap_field. The regmap_field should be freed by the
1017 * user once its finished working with it using regmap_field_free().
1019 struct regmap_field *regmap_field_alloc(struct regmap *regmap,
1020 struct reg_field reg_field)
1022 struct regmap_field *rm_field = kzalloc(sizeof(*rm_field), GFP_KERNEL);
1025 return ERR_PTR(-ENOMEM);
1027 regmap_field_init(rm_field, regmap, reg_field);
1031 EXPORT_SYMBOL_GPL(regmap_field_alloc);
1034 * regmap_field_free(): Free register field allocated using regmap_field_alloc
1036 * @field: regmap field which should be freed.
1038 void regmap_field_free(struct regmap_field *field)
1042 EXPORT_SYMBOL_GPL(regmap_field_free);
1045 * regmap_reinit_cache(): Reinitialise the current register cache
1047 * @map: Register map to operate on.
1048 * @config: New configuration. Only the cache data will be used.
1050 * Discard any existing register cache for the map and initialize a
1051 * new cache. This can be used to restore the cache to defaults or to
1052 * update the cache configuration to reflect runtime discovery of the
1055 * No explicit locking is done here, the user needs to ensure that
1056 * this function will not race with other calls to regmap.
1058 int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
1061 regmap_debugfs_exit(map);
1063 map->max_register = config->max_register;
1064 map->writeable_reg = config->writeable_reg;
1065 map->readable_reg = config->readable_reg;
1066 map->volatile_reg = config->volatile_reg;
1067 map->precious_reg = config->precious_reg;
1068 map->cache_type = config->cache_type;
1070 regmap_debugfs_init(map, config->name);
1072 map->cache_bypass = false;
1073 map->cache_only = false;
1075 return regcache_init(map, config);
1077 EXPORT_SYMBOL_GPL(regmap_reinit_cache);
1080 * regmap_exit(): Free a previously allocated register map
1082 void regmap_exit(struct regmap *map)
1084 struct regmap_async *async;
1087 regmap_debugfs_exit(map);
1088 regmap_range_exit(map);
1089 if (map->bus && map->bus->free_context)
1090 map->bus->free_context(map->bus_context);
1091 kfree(map->work_buf);
1092 while (!list_empty(&map->async_free)) {
1093 async = list_first_entry_or_null(&map->async_free,
1094 struct regmap_async,
1096 list_del(&async->list);
1097 kfree(async->work_buf);
1102 EXPORT_SYMBOL_GPL(regmap_exit);
1104 static int dev_get_regmap_match(struct device *dev, void *res, void *data)
1106 struct regmap **r = res;
1112 /* If the user didn't specify a name match any */
1114 return (*r)->name == data;
1120 * dev_get_regmap(): Obtain the regmap (if any) for a device
1122 * @dev: Device to retrieve the map for
1123 * @name: Optional name for the register map, usually NULL.
1125 * Returns the regmap for the device if one is present, or NULL. If
1126 * name is specified then it must match the name specified when
1127 * registering the device, if it is NULL then the first regmap found
1128 * will be used. Devices with multiple register maps are very rare,
1129 * generic code should normally not need to specify a name.
1131 struct regmap *dev_get_regmap(struct device *dev, const char *name)
1133 struct regmap **r = devres_find(dev, dev_get_regmap_release,
1134 dev_get_regmap_match, (void *)name);
1140 EXPORT_SYMBOL_GPL(dev_get_regmap);
1143 * regmap_get_device(): Obtain the device from a regmap
1145 * @map: Register map to operate on.
1147 * Returns the underlying device that the regmap has been created for.
1149 struct device *regmap_get_device(struct regmap *map)
1153 EXPORT_SYMBOL_GPL(regmap_get_device);
1155 static int _regmap_select_page(struct regmap *map, unsigned int *reg,
1156 struct regmap_range_node *range,
1157 unsigned int val_num)
1159 void *orig_work_buf;
1160 unsigned int win_offset;
1161 unsigned int win_page;
1165 win_offset = (*reg - range->range_min) % range->window_len;
1166 win_page = (*reg - range->range_min) / range->window_len;
1169 /* Bulk write shouldn't cross range boundary */
1170 if (*reg + val_num - 1 > range->range_max)
1173 /* ... or single page boundary */
1174 if (val_num > range->window_len - win_offset)
1178 /* It is possible to have selector register inside data window.
1179 In that case, selector register is located on every page and
1180 it needs no page switching, when accessed alone. */
1182 range->window_start + win_offset != range->selector_reg) {
1183 /* Use separate work_buf during page switching */
1184 orig_work_buf = map->work_buf;
1185 map->work_buf = map->selector_work_buf;
1187 ret = _regmap_update_bits(map, range->selector_reg,
1188 range->selector_mask,
1189 win_page << range->selector_shift,
1192 map->work_buf = orig_work_buf;
1198 *reg = range->window_start + win_offset;
1203 int _regmap_raw_write(struct regmap *map, unsigned int reg,
1204 const void *val, size_t val_len)
1206 struct regmap_range_node *range;
1207 unsigned long flags;
1208 u8 *u8 = map->work_buf;
1209 void *work_val = map->work_buf + map->format.reg_bytes +
1210 map->format.pad_bytes;
1212 int ret = -ENOTSUPP;
1218 /* Check for unwritable registers before we start */
1219 if (map->writeable_reg)
1220 for (i = 0; i < val_len / map->format.val_bytes; i++)
1221 if (!map->writeable_reg(map->dev,
1222 reg + (i * map->reg_stride)))
1225 if (!map->cache_bypass && map->format.parse_val) {
1227 int val_bytes = map->format.val_bytes;
1228 for (i = 0; i < val_len / val_bytes; i++) {
1229 ival = map->format.parse_val(val + (i * val_bytes));
1230 ret = regcache_write(map, reg + (i * map->reg_stride),
1234 "Error in caching of register: %x ret: %d\n",
1239 if (map->cache_only) {
1240 map->cache_dirty = true;
1245 range = _regmap_range_lookup(map, reg);
1247 int val_num = val_len / map->format.val_bytes;
1248 int win_offset = (reg - range->range_min) % range->window_len;
1249 int win_residue = range->window_len - win_offset;
1251 /* If the write goes beyond the end of the window split it */
1252 while (val_num > win_residue) {
1253 dev_dbg(map->dev, "Writing window %d/%zu\n",
1254 win_residue, val_len / map->format.val_bytes);
1255 ret = _regmap_raw_write(map, reg, val, win_residue *
1256 map->format.val_bytes);
1261 val_num -= win_residue;
1262 val += win_residue * map->format.val_bytes;
1263 val_len -= win_residue * map->format.val_bytes;
1265 win_offset = (reg - range->range_min) %
1267 win_residue = range->window_len - win_offset;
1270 ret = _regmap_select_page(map, ®, range, val_num);
1275 map->format.format_reg(map->work_buf, reg, map->reg_shift);
1277 u8[0] |= map->write_flag_mask;
1280 * Essentially all I/O mechanisms will be faster with a single
1281 * buffer to write. Since register syncs often generate raw
1282 * writes of single registers optimise that case.
1284 if (val != work_val && val_len == map->format.val_bytes) {
1285 memcpy(work_val, val, map->format.val_bytes);
1289 if (map->async && map->bus->async_write) {
1290 struct regmap_async *async;
1292 trace_regmap_async_write_start(map, reg, val_len);
1294 spin_lock_irqsave(&map->async_lock, flags);
1295 async = list_first_entry_or_null(&map->async_free,
1296 struct regmap_async,
1299 list_del(&async->list);
1300 spin_unlock_irqrestore(&map->async_lock, flags);
1303 async = map->bus->async_alloc();
1307 async->work_buf = kzalloc(map->format.buf_size,
1308 GFP_KERNEL | GFP_DMA);
1309 if (!async->work_buf) {
1317 /* If the caller supplied the value we can use it safely. */
1318 memcpy(async->work_buf, map->work_buf, map->format.pad_bytes +
1319 map->format.reg_bytes + map->format.val_bytes);
1321 spin_lock_irqsave(&map->async_lock, flags);
1322 list_add_tail(&async->list, &map->async_list);
1323 spin_unlock_irqrestore(&map->async_lock, flags);
1325 if (val != work_val)
1326 ret = map->bus->async_write(map->bus_context,
1328 map->format.reg_bytes +
1329 map->format.pad_bytes,
1330 val, val_len, async);
1332 ret = map->bus->async_write(map->bus_context,
1334 map->format.reg_bytes +
1335 map->format.pad_bytes +
1336 val_len, NULL, 0, async);
1339 dev_err(map->dev, "Failed to schedule write: %d\n",
1342 spin_lock_irqsave(&map->async_lock, flags);
1343 list_move(&async->list, &map->async_free);
1344 spin_unlock_irqrestore(&map->async_lock, flags);
1350 trace_regmap_hw_write_start(map, reg, val_len / map->format.val_bytes);
1352 /* If we're doing a single register write we can probably just
1353 * send the work_buf directly, otherwise try to do a gather
1356 if (val == work_val)
1357 ret = map->bus->write(map->bus_context, map->work_buf,
1358 map->format.reg_bytes +
1359 map->format.pad_bytes +
1361 else if (map->bus->gather_write)
1362 ret = map->bus->gather_write(map->bus_context, map->work_buf,
1363 map->format.reg_bytes +
1364 map->format.pad_bytes,
1367 /* If that didn't work fall back on linearising by hand. */
1368 if (ret == -ENOTSUPP) {
1369 len = map->format.reg_bytes + map->format.pad_bytes + val_len;
1370 buf = kzalloc(len, GFP_KERNEL);
1374 memcpy(buf, map->work_buf, map->format.reg_bytes);
1375 memcpy(buf + map->format.reg_bytes + map->format.pad_bytes,
1377 ret = map->bus->write(map->bus_context, buf, len);
1382 trace_regmap_hw_write_done(map, reg, val_len / map->format.val_bytes);
1388 * regmap_can_raw_write - Test if regmap_raw_write() is supported
1390 * @map: Map to check.
1392 bool regmap_can_raw_write(struct regmap *map)
1394 return map->bus && map->format.format_val && map->format.format_reg;
1396 EXPORT_SYMBOL_GPL(regmap_can_raw_write);
1398 static int _regmap_bus_formatted_write(void *context, unsigned int reg,
1402 struct regmap_range_node *range;
1403 struct regmap *map = context;
1405 WARN_ON(!map->bus || !map->format.format_write);
1407 range = _regmap_range_lookup(map, reg);
1409 ret = _regmap_select_page(map, ®, range, 1);
1414 map->format.format_write(map, reg, val);
1416 trace_regmap_hw_write_start(map, reg, 1);
1418 ret = map->bus->write(map->bus_context, map->work_buf,
1419 map->format.buf_size);
1421 trace_regmap_hw_write_done(map, reg, 1);
1426 static int _regmap_bus_reg_write(void *context, unsigned int reg,
1429 struct regmap *map = context;
1431 return map->bus->reg_write(map->bus_context, reg, val);
1434 static int _regmap_bus_raw_write(void *context, unsigned int reg,
1437 struct regmap *map = context;
1439 WARN_ON(!map->bus || !map->format.format_val);
1441 map->format.format_val(map->work_buf + map->format.reg_bytes
1442 + map->format.pad_bytes, val, 0);
1443 return _regmap_raw_write(map, reg,
1445 map->format.reg_bytes +
1446 map->format.pad_bytes,
1447 map->format.val_bytes);
1450 static inline void *_regmap_map_get_context(struct regmap *map)
1452 return (map->bus) ? map : map->bus_context;
1455 int _regmap_write(struct regmap *map, unsigned int reg,
1459 void *context = _regmap_map_get_context(map);
1461 if (!regmap_writeable(map, reg))
1464 if (!map->cache_bypass && !map->defer_caching) {
1465 ret = regcache_write(map, reg, val);
1468 if (map->cache_only) {
1469 map->cache_dirty = true;
1475 if (map->dev && strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
1476 dev_info(map->dev, "%x <= %x\n", reg, val);
1479 trace_regmap_reg_write(map, reg, val);
1481 return map->reg_write(context, reg, val);
1485 * regmap_write(): Write a value to a single register
1487 * @map: Register map to write to
1488 * @reg: Register to write to
1489 * @val: Value to be written
1491 * A value of zero will be returned on success, a negative errno will
1492 * be returned in error cases.
1494 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
1498 if (reg % map->reg_stride)
1501 map->lock(map->lock_arg);
1503 ret = _regmap_write(map, reg, val);
1505 map->unlock(map->lock_arg);
1509 EXPORT_SYMBOL_GPL(regmap_write);
1512 * regmap_write_async(): Write a value to a single register asynchronously
1514 * @map: Register map to write to
1515 * @reg: Register to write to
1516 * @val: Value to be written
1518 * A value of zero will be returned on success, a negative errno will
1519 * be returned in error cases.
1521 int regmap_write_async(struct regmap *map, unsigned int reg, unsigned int val)
1525 if (reg % map->reg_stride)
1528 map->lock(map->lock_arg);
1532 ret = _regmap_write(map, reg, val);
1536 map->unlock(map->lock_arg);
1540 EXPORT_SYMBOL_GPL(regmap_write_async);
1543 * regmap_raw_write(): Write raw values to one or more registers
1545 * @map: Register map to write to
1546 * @reg: Initial register to write to
1547 * @val: Block of data to be written, laid out for direct transmission to the
1549 * @val_len: Length of data pointed to by val.
1551 * This function is intended to be used for things like firmware
1552 * download where a large block of data needs to be transferred to the
1553 * device. No formatting will be done on the data provided.
1555 * A value of zero will be returned on success, a negative errno will
1556 * be returned in error cases.
1558 int regmap_raw_write(struct regmap *map, unsigned int reg,
1559 const void *val, size_t val_len)
1563 if (!regmap_can_raw_write(map))
1565 if (val_len % map->format.val_bytes)
1568 map->lock(map->lock_arg);
1570 ret = _regmap_raw_write(map, reg, val, val_len);
1572 map->unlock(map->lock_arg);
1576 EXPORT_SYMBOL_GPL(regmap_raw_write);
1579 * regmap_field_write(): Write a value to a single register field
1581 * @field: Register field to write to
1582 * @val: Value to be written
1584 * A value of zero will be returned on success, a negative errno will
1585 * be returned in error cases.
1587 int regmap_field_write(struct regmap_field *field, unsigned int val)
1589 return regmap_update_bits(field->regmap, field->reg,
1590 field->mask, val << field->shift);
1592 EXPORT_SYMBOL_GPL(regmap_field_write);
1595 * regmap_field_update_bits(): Perform a read/modify/write cycle
1596 * on the register field
1598 * @field: Register field to write to
1599 * @mask: Bitmask to change
1600 * @val: Value to be written
1602 * A value of zero will be returned on success, a negative errno will
1603 * be returned in error cases.
1605 int regmap_field_update_bits(struct regmap_field *field, unsigned int mask, unsigned int val)
1607 mask = (mask << field->shift) & field->mask;
1609 return regmap_update_bits(field->regmap, field->reg,
1610 mask, val << field->shift);
1612 EXPORT_SYMBOL_GPL(regmap_field_update_bits);
1615 * regmap_fields_write(): Write a value to a single register field with port ID
1617 * @field: Register field to write to
1619 * @val: Value to be written
1621 * A value of zero will be returned on success, a negative errno will
1622 * be returned in error cases.
1624 int regmap_fields_write(struct regmap_field *field, unsigned int id,
1627 if (id >= field->id_size)
1630 return regmap_update_bits(field->regmap,
1631 field->reg + (field->id_offset * id),
1632 field->mask, val << field->shift);
1634 EXPORT_SYMBOL_GPL(regmap_fields_write);
1637 * regmap_fields_update_bits(): Perform a read/modify/write cycle
1638 * on the register field
1640 * @field: Register field to write to
1642 * @mask: Bitmask to change
1643 * @val: Value to be written
1645 * A value of zero will be returned on success, a negative errno will
1646 * be returned in error cases.
1648 int regmap_fields_update_bits(struct regmap_field *field, unsigned int id,
1649 unsigned int mask, unsigned int val)
1651 if (id >= field->id_size)
1654 mask = (mask << field->shift) & field->mask;
1656 return regmap_update_bits(field->regmap,
1657 field->reg + (field->id_offset * id),
1658 mask, val << field->shift);
1660 EXPORT_SYMBOL_GPL(regmap_fields_update_bits);
1663 * regmap_bulk_write(): Write multiple registers to the device
1665 * @map: Register map to write to
1666 * @reg: First register to be write from
1667 * @val: Block of data to be written, in native register size for device
1668 * @val_count: Number of registers to write
1670 * This function is intended to be used for writing a large block of
1671 * data to the device either in single transfer or multiple transfer.
1673 * A value of zero will be returned on success, a negative errno will
1674 * be returned in error cases.
1676 int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
1680 size_t val_bytes = map->format.val_bytes;
1682 if (map->bus && !map->format.parse_inplace)
1684 if (reg % map->reg_stride)
1688 * Some devices don't support bulk write, for
1689 * them we have a series of single write operations.
1691 if (!map->bus || map->use_single_rw) {
1692 map->lock(map->lock_arg);
1693 for (i = 0; i < val_count; i++) {
1696 switch (val_bytes) {
1698 ival = *(u8 *)(val + (i * val_bytes));
1701 ival = *(u16 *)(val + (i * val_bytes));
1704 ival = *(u32 *)(val + (i * val_bytes));
1708 ival = *(u64 *)(val + (i * val_bytes));
1716 ret = _regmap_write(map, reg + (i * map->reg_stride),
1722 map->unlock(map->lock_arg);
1729 wval = kmemdup(val, val_count * val_bytes, GFP_KERNEL);
1731 dev_err(map->dev, "Error in memory allocation\n");
1734 for (i = 0; i < val_count * val_bytes; i += val_bytes)
1735 map->format.parse_inplace(wval + i);
1737 map->lock(map->lock_arg);
1738 ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count);
1739 map->unlock(map->lock_arg);
1745 EXPORT_SYMBOL_GPL(regmap_bulk_write);
1748 * _regmap_raw_multi_reg_write()
1750 * the (register,newvalue) pairs in regs have not been formatted, but
1751 * they are all in the same page and have been changed to being page
1752 * relative. The page register has been written if that was neccessary.
1754 static int _regmap_raw_multi_reg_write(struct regmap *map,
1755 const struct reg_default *regs,
1762 size_t val_bytes = map->format.val_bytes;
1763 size_t reg_bytes = map->format.reg_bytes;
1764 size_t pad_bytes = map->format.pad_bytes;
1765 size_t pair_size = reg_bytes + pad_bytes + val_bytes;
1766 size_t len = pair_size * num_regs;
1771 buf = kzalloc(len, GFP_KERNEL);
1775 /* We have to linearise by hand. */
1779 for (i = 0; i < num_regs; i++) {
1780 int reg = regs[i].reg;
1781 int val = regs[i].def;
1782 trace_regmap_hw_write_start(map, reg, 1);
1783 map->format.format_reg(u8, reg, map->reg_shift);
1784 u8 += reg_bytes + pad_bytes;
1785 map->format.format_val(u8, val, 0);
1789 *u8 |= map->write_flag_mask;
1791 ret = map->bus->write(map->bus_context, buf, len);
1795 for (i = 0; i < num_regs; i++) {
1796 int reg = regs[i].reg;
1797 trace_regmap_hw_write_done(map, reg, 1);
1802 static unsigned int _regmap_register_page(struct regmap *map,
1804 struct regmap_range_node *range)
1806 unsigned int win_page = (reg - range->range_min) / range->window_len;
1811 static int _regmap_range_multi_paged_reg_write(struct regmap *map,
1812 struct reg_default *regs,
1817 struct reg_default *base;
1818 unsigned int this_page = 0;
1820 * the set of registers are not neccessarily in order, but
1821 * since the order of write must be preserved this algorithm
1822 * chops the set each time the page changes
1825 for (i = 0, n = 0; i < num_regs; i++, n++) {
1826 unsigned int reg = regs[i].reg;
1827 struct regmap_range_node *range;
1829 range = _regmap_range_lookup(map, reg);
1831 unsigned int win_page = _regmap_register_page(map, reg,
1835 this_page = win_page;
1836 if (win_page != this_page) {
1837 this_page = win_page;
1838 ret = _regmap_raw_multi_reg_write(map, base, n);
1844 ret = _regmap_select_page(map, &base[n].reg, range, 1);
1850 return _regmap_raw_multi_reg_write(map, base, n);
1854 static int _regmap_multi_reg_write(struct regmap *map,
1855 const struct reg_default *regs,
1861 if (!map->can_multi_write) {
1862 for (i = 0; i < num_regs; i++) {
1863 ret = _regmap_write(map, regs[i].reg, regs[i].def);
1870 if (!map->format.parse_inplace)
1873 if (map->writeable_reg)
1874 for (i = 0; i < num_regs; i++) {
1875 int reg = regs[i].reg;
1876 if (!map->writeable_reg(map->dev, reg))
1878 if (reg % map->reg_stride)
1882 if (!map->cache_bypass) {
1883 for (i = 0; i < num_regs; i++) {
1884 unsigned int val = regs[i].def;
1885 unsigned int reg = regs[i].reg;
1886 ret = regcache_write(map, reg, val);
1889 "Error in caching of register: %x ret: %d\n",
1894 if (map->cache_only) {
1895 map->cache_dirty = true;
1902 for (i = 0; i < num_regs; i++) {
1903 unsigned int reg = regs[i].reg;
1904 struct regmap_range_node *range;
1905 range = _regmap_range_lookup(map, reg);
1907 size_t len = sizeof(struct reg_default)*num_regs;
1908 struct reg_default *base = kmemdup(regs, len,
1912 ret = _regmap_range_multi_paged_reg_write(map, base,
1919 return _regmap_raw_multi_reg_write(map, regs, num_regs);
1923 * regmap_multi_reg_write(): Write multiple registers to the device
1925 * where the set of register,value pairs are supplied in any order,
1926 * possibly not all in a single range.
1928 * @map: Register map to write to
1929 * @regs: Array of structures containing register,value to be written
1930 * @num_regs: Number of registers to write
1932 * The 'normal' block write mode will send ultimately send data on the
1933 * target bus as R,V1,V2,V3,..,Vn where successively higer registers are
1934 * addressed. However, this alternative block multi write mode will send
1935 * the data as R1,V1,R2,V2,..,Rn,Vn on the target bus. The target device
1936 * must of course support the mode.
1938 * A value of zero will be returned on success, a negative errno will be
1939 * returned in error cases.
1941 int regmap_multi_reg_write(struct regmap *map, const struct reg_default *regs,
1946 map->lock(map->lock_arg);
1948 ret = _regmap_multi_reg_write(map, regs, num_regs);
1950 map->unlock(map->lock_arg);
1954 EXPORT_SYMBOL_GPL(regmap_multi_reg_write);
1957 * regmap_multi_reg_write_bypassed(): Write multiple registers to the
1958 * device but not the cache
1960 * where the set of register are supplied in any order
1962 * @map: Register map to write to
1963 * @regs: Array of structures containing register,value to be written
1964 * @num_regs: Number of registers to write
1966 * This function is intended to be used for writing a large block of data
1967 * atomically to the device in single transfer for those I2C client devices
1968 * that implement this alternative block write mode.
1970 * A value of zero will be returned on success, a negative errno will
1971 * be returned in error cases.
1973 int regmap_multi_reg_write_bypassed(struct regmap *map,
1974 const struct reg_default *regs,
1980 map->lock(map->lock_arg);
1982 bypass = map->cache_bypass;
1983 map->cache_bypass = true;
1985 ret = _regmap_multi_reg_write(map, regs, num_regs);
1987 map->cache_bypass = bypass;
1989 map->unlock(map->lock_arg);
1993 EXPORT_SYMBOL_GPL(regmap_multi_reg_write_bypassed);
1996 * regmap_raw_write_async(): Write raw values to one or more registers
1999 * @map: Register map to write to
2000 * @reg: Initial register to write to
2001 * @val: Block of data to be written, laid out for direct transmission to the
2002 * device. Must be valid until regmap_async_complete() is called.
2003 * @val_len: Length of data pointed to by val.
2005 * This function is intended to be used for things like firmware
2006 * download where a large block of data needs to be transferred to the
2007 * device. No formatting will be done on the data provided.
2009 * If supported by the underlying bus the write will be scheduled
2010 * asynchronously, helping maximise I/O speed on higher speed buses
2011 * like SPI. regmap_async_complete() can be called to ensure that all
2012 * asynchrnous writes have been completed.
2014 * A value of zero will be returned on success, a negative errno will
2015 * be returned in error cases.
2017 int regmap_raw_write_async(struct regmap *map, unsigned int reg,
2018 const void *val, size_t val_len)
2022 if (val_len % map->format.val_bytes)
2024 if (reg % map->reg_stride)
2027 map->lock(map->lock_arg);
2031 ret = _regmap_raw_write(map, reg, val, val_len);
2035 map->unlock(map->lock_arg);
2039 EXPORT_SYMBOL_GPL(regmap_raw_write_async);
2041 static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
2042 unsigned int val_len)
2044 struct regmap_range_node *range;
2045 u8 *u8 = map->work_buf;
2050 range = _regmap_range_lookup(map, reg);
2052 ret = _regmap_select_page(map, ®, range,
2053 val_len / map->format.val_bytes);
2058 map->format.format_reg(map->work_buf, reg, map->reg_shift);
2061 * Some buses or devices flag reads by setting the high bits in the
2062 * register addresss; since it's always the high bits for all
2063 * current formats we can do this here rather than in
2064 * formatting. This may break if we get interesting formats.
2066 u8[0] |= map->read_flag_mask;
2068 trace_regmap_hw_read_start(map, reg, val_len / map->format.val_bytes);
2070 ret = map->bus->read(map->bus_context, map->work_buf,
2071 map->format.reg_bytes + map->format.pad_bytes,
2074 trace_regmap_hw_read_done(map, reg, val_len / map->format.val_bytes);
2079 static int _regmap_bus_reg_read(void *context, unsigned int reg,
2082 struct regmap *map = context;
2084 return map->bus->reg_read(map->bus_context, reg, val);
2087 static int _regmap_bus_read(void *context, unsigned int reg,
2091 struct regmap *map = context;
2093 if (!map->format.parse_val)
2096 ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
2098 *val = map->format.parse_val(map->work_buf);
2103 static int _regmap_read(struct regmap *map, unsigned int reg,
2107 void *context = _regmap_map_get_context(map);
2109 WARN_ON(!map->reg_read);
2111 if (!map->cache_bypass) {
2112 ret = regcache_read(map, reg, val);
2117 if (map->cache_only)
2120 if (!regmap_readable(map, reg))
2123 ret = map->reg_read(context, reg, val);
2126 if (map->dev && strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
2127 dev_info(map->dev, "%x => %x\n", reg, *val);
2130 trace_regmap_reg_read(map, reg, *val);
2132 if (!map->cache_bypass)
2133 regcache_write(map, reg, *val);
2140 * regmap_read(): Read a value from a single register
2142 * @map: Register map to read from
2143 * @reg: Register to be read from
2144 * @val: Pointer to store read value
2146 * A value of zero will be returned on success, a negative errno will
2147 * be returned in error cases.
2149 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
2153 if (reg % map->reg_stride)
2156 map->lock(map->lock_arg);
2158 ret = _regmap_read(map, reg, val);
2160 map->unlock(map->lock_arg);
2164 EXPORT_SYMBOL_GPL(regmap_read);
2167 * regmap_raw_read(): Read raw data from the device
2169 * @map: Register map to read from
2170 * @reg: First register to be read from
2171 * @val: Pointer to store read value
2172 * @val_len: Size of data to read
2174 * A value of zero will be returned on success, a negative errno will
2175 * be returned in error cases.
2177 int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
2180 size_t val_bytes = map->format.val_bytes;
2181 size_t val_count = val_len / val_bytes;
2187 if (val_len % map->format.val_bytes)
2189 if (reg % map->reg_stride)
2192 map->lock(map->lock_arg);
2194 if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
2195 map->cache_type == REGCACHE_NONE) {
2196 /* Physical block read if there's no cache involved */
2197 ret = _regmap_raw_read(map, reg, val, val_len);
2200 /* Otherwise go word by word for the cache; should be low
2201 * cost as we expect to hit the cache.
2203 for (i = 0; i < val_count; i++) {
2204 ret = _regmap_read(map, reg + (i * map->reg_stride),
2209 map->format.format_val(val + (i * val_bytes), v, 0);
2214 map->unlock(map->lock_arg);
2218 EXPORT_SYMBOL_GPL(regmap_raw_read);
2221 * regmap_field_read(): Read a value to a single register field
2223 * @field: Register field to read from
2224 * @val: Pointer to store read value
2226 * A value of zero will be returned on success, a negative errno will
2227 * be returned in error cases.
2229 int regmap_field_read(struct regmap_field *field, unsigned int *val)
2232 unsigned int reg_val;
2233 ret = regmap_read(field->regmap, field->reg, ®_val);
2237 reg_val &= field->mask;
2238 reg_val >>= field->shift;
2243 EXPORT_SYMBOL_GPL(regmap_field_read);
2246 * regmap_fields_read(): Read a value to a single register field with port ID
2248 * @field: Register field to read from
2250 * @val: Pointer to store read value
2252 * A value of zero will be returned on success, a negative errno will
2253 * be returned in error cases.
2255 int regmap_fields_read(struct regmap_field *field, unsigned int id,
2259 unsigned int reg_val;
2261 if (id >= field->id_size)
2264 ret = regmap_read(field->regmap,
2265 field->reg + (field->id_offset * id),
2270 reg_val &= field->mask;
2271 reg_val >>= field->shift;
2276 EXPORT_SYMBOL_GPL(regmap_fields_read);
2279 * regmap_bulk_read(): Read multiple registers from the device
2281 * @map: Register map to read from
2282 * @reg: First register to be read from
2283 * @val: Pointer to store read value, in native register size for device
2284 * @val_count: Number of registers to read
2286 * A value of zero will be returned on success, a negative errno will
2287 * be returned in error cases.
2289 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
2293 size_t val_bytes = map->format.val_bytes;
2294 bool vol = regmap_volatile_range(map, reg, val_count);
2296 if (reg % map->reg_stride)
2299 if (map->bus && map->format.parse_inplace && (vol || map->cache_type == REGCACHE_NONE)) {
2301 * Some devices does not support bulk read, for
2302 * them we have a series of single read operations.
2304 if (map->use_single_rw) {
2305 for (i = 0; i < val_count; i++) {
2306 ret = regmap_raw_read(map,
2307 reg + (i * map->reg_stride),
2308 val + (i * val_bytes),
2314 ret = regmap_raw_read(map, reg, val,
2315 val_bytes * val_count);
2320 for (i = 0; i < val_count * val_bytes; i += val_bytes)
2321 map->format.parse_inplace(val + i);
2323 for (i = 0; i < val_count; i++) {
2325 ret = regmap_read(map, reg + (i * map->reg_stride),
2329 map->format.format_val(val + (i * val_bytes), ival, 0);
2335 EXPORT_SYMBOL_GPL(regmap_bulk_read);
2337 static int _regmap_update_bits(struct regmap *map, unsigned int reg,
2338 unsigned int mask, unsigned int val,
2342 unsigned int tmp, orig;
2344 ret = _regmap_read(map, reg, &orig);
2352 ret = _regmap_write(map, reg, tmp);
2364 * regmap_update_bits: Perform a read/modify/write cycle on the register map
2366 * @map: Register map to update
2367 * @reg: Register to update
2368 * @mask: Bitmask to change
2369 * @val: New value for bitmask
2371 * Returns zero for success, a negative number on error.
2373 int regmap_update_bits(struct regmap *map, unsigned int reg,
2374 unsigned int mask, unsigned int val)
2378 map->lock(map->lock_arg);
2379 ret = _regmap_update_bits(map, reg, mask, val, NULL);
2380 map->unlock(map->lock_arg);
2384 EXPORT_SYMBOL_GPL(regmap_update_bits);
2387 * regmap_update_bits_async: Perform a read/modify/write cycle on the register
2388 * map asynchronously
2390 * @map: Register map to update
2391 * @reg: Register to update
2392 * @mask: Bitmask to change
2393 * @val: New value for bitmask
2395 * With most buses the read must be done synchronously so this is most
2396 * useful for devices with a cache which do not need to interact with
2397 * the hardware to determine the current register value.
2399 * Returns zero for success, a negative number on error.
2401 int regmap_update_bits_async(struct regmap *map, unsigned int reg,
2402 unsigned int mask, unsigned int val)
2406 map->lock(map->lock_arg);
2410 ret = _regmap_update_bits(map, reg, mask, val, NULL);
2414 map->unlock(map->lock_arg);
2418 EXPORT_SYMBOL_GPL(regmap_update_bits_async);
2421 * regmap_update_bits_check: Perform a read/modify/write cycle on the
2422 * register map and report if updated
2424 * @map: Register map to update
2425 * @reg: Register to update
2426 * @mask: Bitmask to change
2427 * @val: New value for bitmask
2428 * @change: Boolean indicating if a write was done
2430 * Returns zero for success, a negative number on error.
2432 int regmap_update_bits_check(struct regmap *map, unsigned int reg,
2433 unsigned int mask, unsigned int val,
2438 map->lock(map->lock_arg);
2439 ret = _regmap_update_bits(map, reg, mask, val, change);
2440 map->unlock(map->lock_arg);
2443 EXPORT_SYMBOL_GPL(regmap_update_bits_check);
2446 * regmap_update_bits_check_async: Perform a read/modify/write cycle on the
2447 * register map asynchronously and report if
2450 * @map: Register map to update
2451 * @reg: Register to update
2452 * @mask: Bitmask to change
2453 * @val: New value for bitmask
2454 * @change: Boolean indicating if a write was done
2456 * With most buses the read must be done synchronously so this is most
2457 * useful for devices with a cache which do not need to interact with
2458 * the hardware to determine the current register value.
2460 * Returns zero for success, a negative number on error.
2462 int regmap_update_bits_check_async(struct regmap *map, unsigned int reg,
2463 unsigned int mask, unsigned int val,
2468 map->lock(map->lock_arg);
2472 ret = _regmap_update_bits(map, reg, mask, val, change);
2476 map->unlock(map->lock_arg);
2480 EXPORT_SYMBOL_GPL(regmap_update_bits_check_async);
2482 void regmap_async_complete_cb(struct regmap_async *async, int ret)
2484 struct regmap *map = async->map;
2487 trace_regmap_async_io_complete(map);
2489 spin_lock(&map->async_lock);
2490 list_move(&async->list, &map->async_free);
2491 wake = list_empty(&map->async_list);
2494 map->async_ret = ret;
2496 spin_unlock(&map->async_lock);
2499 wake_up(&map->async_waitq);
2501 EXPORT_SYMBOL_GPL(regmap_async_complete_cb);
2503 static int regmap_async_is_done(struct regmap *map)
2505 unsigned long flags;
2508 spin_lock_irqsave(&map->async_lock, flags);
2509 ret = list_empty(&map->async_list);
2510 spin_unlock_irqrestore(&map->async_lock, flags);
2516 * regmap_async_complete: Ensure all asynchronous I/O has completed.
2518 * @map: Map to operate on.
2520 * Blocks until any pending asynchronous I/O has completed. Returns
2521 * an error code for any failed I/O operations.
2523 int regmap_async_complete(struct regmap *map)
2525 unsigned long flags;
2528 /* Nothing to do with no async support */
2529 if (!map->bus || !map->bus->async_write)
2532 trace_regmap_async_complete_start(map);
2534 wait_event(map->async_waitq, regmap_async_is_done(map));
2536 spin_lock_irqsave(&map->async_lock, flags);
2537 ret = map->async_ret;
2539 spin_unlock_irqrestore(&map->async_lock, flags);
2541 trace_regmap_async_complete_done(map);
2545 EXPORT_SYMBOL_GPL(regmap_async_complete);
2548 * regmap_register_patch: Register and apply register updates to be applied
2549 * on device initialistion
2551 * @map: Register map to apply updates to.
2552 * @regs: Values to update.
2553 * @num_regs: Number of entries in regs.
2555 * Register a set of register updates to be applied to the device
2556 * whenever the device registers are synchronised with the cache and
2557 * apply them immediately. Typically this is used to apply
2558 * corrections to be applied to the device defaults on startup, such
2559 * as the updates some vendors provide to undocumented registers.
2561 * The caller must ensure that this function cannot be called
2562 * concurrently with either itself or regcache_sync().
2564 int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
2567 struct reg_default *p;
2571 if (WARN_ONCE(num_regs <= 0, "invalid registers number (%d)\n",
2575 p = krealloc(map->patch,
2576 sizeof(struct reg_default) * (map->patch_regs + num_regs),
2579 memcpy(p + map->patch_regs, regs, num_regs * sizeof(*regs));
2581 map->patch_regs += num_regs;
2586 map->lock(map->lock_arg);
2588 bypass = map->cache_bypass;
2590 map->cache_bypass = true;
2593 ret = _regmap_multi_reg_write(map, regs, num_regs);
2596 map->cache_bypass = bypass;
2598 map->unlock(map->lock_arg);
2600 regmap_async_complete(map);
2604 EXPORT_SYMBOL_GPL(regmap_register_patch);
2607 * regmap_get_val_bytes(): Report the size of a register value
2609 * Report the size of a register value, mainly intended to for use by
2610 * generic infrastructure built on top of regmap.
2612 int regmap_get_val_bytes(struct regmap *map)
2614 if (map->format.format_write)
2617 return map->format.val_bytes;
2619 EXPORT_SYMBOL_GPL(regmap_get_val_bytes);
2622 * regmap_get_max_register(): Report the max register value
2624 * Report the max register value, mainly intended to for use by
2625 * generic infrastructure built on top of regmap.
2627 int regmap_get_max_register(struct regmap *map)
2629 return map->max_register ? map->max_register : -EINVAL;
2631 EXPORT_SYMBOL_GPL(regmap_get_max_register);
2634 * regmap_get_reg_stride(): Report the register address stride
2636 * Report the register address stride, mainly intended to for use by
2637 * generic infrastructure built on top of regmap.
2639 int regmap_get_reg_stride(struct regmap *map)
2641 return map->reg_stride;
2643 EXPORT_SYMBOL_GPL(regmap_get_reg_stride);
2645 int regmap_parse_val(struct regmap *map, const void *buf,
2648 if (!map->format.parse_val)
2651 *val = map->format.parse_val(buf);
2655 EXPORT_SYMBOL_GPL(regmap_parse_val);
2657 static int __init regmap_initcall(void)
2659 regmap_debugfs_initcall();
2663 postcore_initcall(regmap_initcall);