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
23 #include <trace/events/regmap.h>
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 static 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;
518 * regmap_init(): Initialise register map
520 * @dev: Device that will be interacted with
521 * @bus: Bus-specific callbacks to use with device
522 * @bus_context: Data passed to bus-specific callbacks
523 * @config: Configuration for register map
525 * The return value will be an ERR_PTR() on error or a valid pointer to
526 * a struct regmap. This function should generally not be called
527 * directly, it should be called by bus-specific init functions.
529 struct regmap *regmap_init(struct device *dev,
530 const struct regmap_bus *bus,
532 const struct regmap_config *config)
536 enum regmap_endian reg_endian, val_endian;
542 map = kzalloc(sizeof(*map), GFP_KERNEL);
548 if (config->lock && config->unlock) {
549 map->lock = config->lock;
550 map->unlock = config->unlock;
551 map->lock_arg = config->lock_arg;
553 if ((bus && bus->fast_io) ||
555 spin_lock_init(&map->spinlock);
556 map->lock = regmap_lock_spinlock;
557 map->unlock = regmap_unlock_spinlock;
559 mutex_init(&map->mutex);
560 map->lock = regmap_lock_mutex;
561 map->unlock = regmap_unlock_mutex;
565 map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8);
566 map->format.pad_bytes = config->pad_bits / 8;
567 map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8);
568 map->format.buf_size = DIV_ROUND_UP(config->reg_bits +
569 config->val_bits + config->pad_bits, 8);
570 map->reg_shift = config->pad_bits % 8;
571 if (config->reg_stride)
572 map->reg_stride = config->reg_stride;
575 map->use_single_rw = config->use_single_rw;
576 map->can_multi_write = config->can_multi_write;
579 map->bus_context = bus_context;
580 map->max_register = config->max_register;
581 map->wr_table = config->wr_table;
582 map->rd_table = config->rd_table;
583 map->volatile_table = config->volatile_table;
584 map->precious_table = config->precious_table;
585 map->writeable_reg = config->writeable_reg;
586 map->readable_reg = config->readable_reg;
587 map->volatile_reg = config->volatile_reg;
588 map->precious_reg = config->precious_reg;
589 map->cache_type = config->cache_type;
590 map->name = config->name;
592 spin_lock_init(&map->async_lock);
593 INIT_LIST_HEAD(&map->async_list);
594 INIT_LIST_HEAD(&map->async_free);
595 init_waitqueue_head(&map->async_waitq);
597 if (config->read_flag_mask || config->write_flag_mask) {
598 map->read_flag_mask = config->read_flag_mask;
599 map->write_flag_mask = config->write_flag_mask;
601 map->read_flag_mask = bus->read_flag_mask;
605 map->reg_read = config->reg_read;
606 map->reg_write = config->reg_write;
608 map->defer_caching = false;
609 goto skip_format_initialization;
610 } else if (!bus->read || !bus->write) {
611 map->reg_read = _regmap_bus_reg_read;
612 map->reg_write = _regmap_bus_reg_write;
614 map->defer_caching = false;
615 goto skip_format_initialization;
617 map->reg_read = _regmap_bus_read;
620 reg_endian = regmap_get_reg_endian(bus, config);
621 val_endian = regmap_get_val_endian(dev, bus, config);
623 switch (config->reg_bits + map->reg_shift) {
625 switch (config->val_bits) {
627 map->format.format_write = regmap_format_2_6_write;
635 switch (config->val_bits) {
637 map->format.format_write = regmap_format_4_12_write;
645 switch (config->val_bits) {
647 map->format.format_write = regmap_format_7_9_write;
655 switch (config->val_bits) {
657 map->format.format_write = regmap_format_10_14_write;
665 map->format.format_reg = regmap_format_8;
669 switch (reg_endian) {
670 case REGMAP_ENDIAN_BIG:
671 map->format.format_reg = regmap_format_16_be;
673 case REGMAP_ENDIAN_NATIVE:
674 map->format.format_reg = regmap_format_16_native;
682 if (reg_endian != REGMAP_ENDIAN_BIG)
684 map->format.format_reg = regmap_format_24;
688 switch (reg_endian) {
689 case REGMAP_ENDIAN_BIG:
690 map->format.format_reg = regmap_format_32_be;
692 case REGMAP_ENDIAN_NATIVE:
693 map->format.format_reg = regmap_format_32_native;
704 if (val_endian == REGMAP_ENDIAN_NATIVE)
705 map->format.parse_inplace = regmap_parse_inplace_noop;
707 switch (config->val_bits) {
709 map->format.format_val = regmap_format_8;
710 map->format.parse_val = regmap_parse_8;
711 map->format.parse_inplace = regmap_parse_inplace_noop;
714 switch (val_endian) {
715 case REGMAP_ENDIAN_BIG:
716 map->format.format_val = regmap_format_16_be;
717 map->format.parse_val = regmap_parse_16_be;
718 map->format.parse_inplace = regmap_parse_16_be_inplace;
720 case REGMAP_ENDIAN_LITTLE:
721 map->format.format_val = regmap_format_16_le;
722 map->format.parse_val = regmap_parse_16_le;
723 map->format.parse_inplace = regmap_parse_16_le_inplace;
725 case REGMAP_ENDIAN_NATIVE:
726 map->format.format_val = regmap_format_16_native;
727 map->format.parse_val = regmap_parse_16_native;
734 if (val_endian != REGMAP_ENDIAN_BIG)
736 map->format.format_val = regmap_format_24;
737 map->format.parse_val = regmap_parse_24;
740 switch (val_endian) {
741 case REGMAP_ENDIAN_BIG:
742 map->format.format_val = regmap_format_32_be;
743 map->format.parse_val = regmap_parse_32_be;
744 map->format.parse_inplace = regmap_parse_32_be_inplace;
746 case REGMAP_ENDIAN_LITTLE:
747 map->format.format_val = regmap_format_32_le;
748 map->format.parse_val = regmap_parse_32_le;
749 map->format.parse_inplace = regmap_parse_32_le_inplace;
751 case REGMAP_ENDIAN_NATIVE:
752 map->format.format_val = regmap_format_32_native;
753 map->format.parse_val = regmap_parse_32_native;
761 if (map->format.format_write) {
762 if ((reg_endian != REGMAP_ENDIAN_BIG) ||
763 (val_endian != REGMAP_ENDIAN_BIG))
765 map->use_single_rw = true;
768 if (!map->format.format_write &&
769 !(map->format.format_reg && map->format.format_val))
772 map->work_buf = kzalloc(map->format.buf_size, GFP_KERNEL);
773 if (map->work_buf == NULL) {
778 if (map->format.format_write) {
779 map->defer_caching = false;
780 map->reg_write = _regmap_bus_formatted_write;
781 } else if (map->format.format_val) {
782 map->defer_caching = true;
783 map->reg_write = _regmap_bus_raw_write;
786 skip_format_initialization:
788 map->range_tree = RB_ROOT;
789 for (i = 0; i < config->num_ranges; i++) {
790 const struct regmap_range_cfg *range_cfg = &config->ranges[i];
791 struct regmap_range_node *new;
794 if (range_cfg->range_max < range_cfg->range_min) {
795 dev_err(map->dev, "Invalid range %d: %d < %d\n", i,
796 range_cfg->range_max, range_cfg->range_min);
800 if (range_cfg->range_max > map->max_register) {
801 dev_err(map->dev, "Invalid range %d: %d > %d\n", i,
802 range_cfg->range_max, map->max_register);
806 if (range_cfg->selector_reg > map->max_register) {
808 "Invalid range %d: selector out of map\n", i);
812 if (range_cfg->window_len == 0) {
813 dev_err(map->dev, "Invalid range %d: window_len 0\n",
818 /* Make sure, that this register range has no selector
819 or data window within its boundary */
820 for (j = 0; j < config->num_ranges; j++) {
821 unsigned sel_reg = config->ranges[j].selector_reg;
822 unsigned win_min = config->ranges[j].window_start;
823 unsigned win_max = win_min +
824 config->ranges[j].window_len - 1;
826 /* Allow data window inside its own virtual range */
830 if (range_cfg->range_min <= sel_reg &&
831 sel_reg <= range_cfg->range_max) {
833 "Range %d: selector for %d in window\n",
838 if (!(win_max < range_cfg->range_min ||
839 win_min > range_cfg->range_max)) {
841 "Range %d: window for %d in window\n",
847 new = kzalloc(sizeof(*new), GFP_KERNEL);
854 new->name = range_cfg->name;
855 new->range_min = range_cfg->range_min;
856 new->range_max = range_cfg->range_max;
857 new->selector_reg = range_cfg->selector_reg;
858 new->selector_mask = range_cfg->selector_mask;
859 new->selector_shift = range_cfg->selector_shift;
860 new->window_start = range_cfg->window_start;
861 new->window_len = range_cfg->window_len;
863 if (!_regmap_range_add(map, new)) {
864 dev_err(map->dev, "Failed to add range %d\n", i);
869 if (map->selector_work_buf == NULL) {
870 map->selector_work_buf =
871 kzalloc(map->format.buf_size, GFP_KERNEL);
872 if (map->selector_work_buf == NULL) {
879 ret = regcache_init(map, config);
884 ret = regmap_attach_dev(dev, map, config);
894 regmap_range_exit(map);
895 kfree(map->work_buf);
901 EXPORT_SYMBOL_GPL(regmap_init);
903 static void devm_regmap_release(struct device *dev, void *res)
905 regmap_exit(*(struct regmap **)res);
909 * devm_regmap_init(): Initialise managed register map
911 * @dev: Device that will be interacted with
912 * @bus: Bus-specific callbacks to use with device
913 * @bus_context: Data passed to bus-specific callbacks
914 * @config: Configuration for register map
916 * The return value will be an ERR_PTR() on error or a valid pointer
917 * to a struct regmap. This function should generally not be called
918 * directly, it should be called by bus-specific init functions. The
919 * map will be automatically freed by the device management code.
921 struct regmap *devm_regmap_init(struct device *dev,
922 const struct regmap_bus *bus,
924 const struct regmap_config *config)
926 struct regmap **ptr, *regmap;
928 ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL);
930 return ERR_PTR(-ENOMEM);
932 regmap = regmap_init(dev, bus, bus_context, config);
933 if (!IS_ERR(regmap)) {
935 devres_add(dev, ptr);
942 EXPORT_SYMBOL_GPL(devm_regmap_init);
944 static void regmap_field_init(struct regmap_field *rm_field,
945 struct regmap *regmap, struct reg_field reg_field)
947 int field_bits = reg_field.msb - reg_field.lsb + 1;
948 rm_field->regmap = regmap;
949 rm_field->reg = reg_field.reg;
950 rm_field->shift = reg_field.lsb;
951 rm_field->mask = ((BIT(field_bits) - 1) << reg_field.lsb);
952 rm_field->id_size = reg_field.id_size;
953 rm_field->id_offset = reg_field.id_offset;
957 * devm_regmap_field_alloc(): Allocate and initialise a register field
960 * @dev: Device that will be interacted with
961 * @regmap: regmap bank in which this register field is located.
962 * @reg_field: Register field with in the bank.
964 * The return value will be an ERR_PTR() on error or a valid pointer
965 * to a struct regmap_field. The regmap_field will be automatically freed
966 * by the device management code.
968 struct regmap_field *devm_regmap_field_alloc(struct device *dev,
969 struct regmap *regmap, struct reg_field reg_field)
971 struct regmap_field *rm_field = devm_kzalloc(dev,
972 sizeof(*rm_field), GFP_KERNEL);
974 return ERR_PTR(-ENOMEM);
976 regmap_field_init(rm_field, regmap, reg_field);
981 EXPORT_SYMBOL_GPL(devm_regmap_field_alloc);
984 * devm_regmap_field_free(): Free register field allocated using
985 * devm_regmap_field_alloc. Usally drivers need not call this function,
986 * as the memory allocated via devm will be freed as per device-driver
989 * @dev: Device that will be interacted with
990 * @field: regmap field which should be freed.
992 void devm_regmap_field_free(struct device *dev,
993 struct regmap_field *field)
995 devm_kfree(dev, field);
997 EXPORT_SYMBOL_GPL(devm_regmap_field_free);
1000 * regmap_field_alloc(): Allocate and initialise a register field
1001 * in a register map.
1003 * @regmap: regmap bank in which this register field is located.
1004 * @reg_field: Register field with in the bank.
1006 * The return value will be an ERR_PTR() on error or a valid pointer
1007 * to a struct regmap_field. The regmap_field should be freed by the
1008 * user once its finished working with it using regmap_field_free().
1010 struct regmap_field *regmap_field_alloc(struct regmap *regmap,
1011 struct reg_field reg_field)
1013 struct regmap_field *rm_field = kzalloc(sizeof(*rm_field), GFP_KERNEL);
1016 return ERR_PTR(-ENOMEM);
1018 regmap_field_init(rm_field, regmap, reg_field);
1022 EXPORT_SYMBOL_GPL(regmap_field_alloc);
1025 * regmap_field_free(): Free register field allocated using regmap_field_alloc
1027 * @field: regmap field which should be freed.
1029 void regmap_field_free(struct regmap_field *field)
1033 EXPORT_SYMBOL_GPL(regmap_field_free);
1036 * regmap_reinit_cache(): Reinitialise the current register cache
1038 * @map: Register map to operate on.
1039 * @config: New configuration. Only the cache data will be used.
1041 * Discard any existing register cache for the map and initialize a
1042 * new cache. This can be used to restore the cache to defaults or to
1043 * update the cache configuration to reflect runtime discovery of the
1046 * No explicit locking is done here, the user needs to ensure that
1047 * this function will not race with other calls to regmap.
1049 int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
1052 regmap_debugfs_exit(map);
1054 map->max_register = config->max_register;
1055 map->writeable_reg = config->writeable_reg;
1056 map->readable_reg = config->readable_reg;
1057 map->volatile_reg = config->volatile_reg;
1058 map->precious_reg = config->precious_reg;
1059 map->cache_type = config->cache_type;
1061 regmap_debugfs_init(map, config->name);
1063 map->cache_bypass = false;
1064 map->cache_only = false;
1066 return regcache_init(map, config);
1068 EXPORT_SYMBOL_GPL(regmap_reinit_cache);
1071 * regmap_exit(): Free a previously allocated register map
1073 void regmap_exit(struct regmap *map)
1075 struct regmap_async *async;
1078 regmap_debugfs_exit(map);
1079 regmap_range_exit(map);
1080 if (map->bus && map->bus->free_context)
1081 map->bus->free_context(map->bus_context);
1082 kfree(map->work_buf);
1083 while (!list_empty(&map->async_free)) {
1084 async = list_first_entry_or_null(&map->async_free,
1085 struct regmap_async,
1087 list_del(&async->list);
1088 kfree(async->work_buf);
1093 EXPORT_SYMBOL_GPL(regmap_exit);
1095 static int dev_get_regmap_match(struct device *dev, void *res, void *data)
1097 struct regmap **r = res;
1103 /* If the user didn't specify a name match any */
1105 return (*r)->name == data;
1111 * dev_get_regmap(): Obtain the regmap (if any) for a device
1113 * @dev: Device to retrieve the map for
1114 * @name: Optional name for the register map, usually NULL.
1116 * Returns the regmap for the device if one is present, or NULL. If
1117 * name is specified then it must match the name specified when
1118 * registering the device, if it is NULL then the first regmap found
1119 * will be used. Devices with multiple register maps are very rare,
1120 * generic code should normally not need to specify a name.
1122 struct regmap *dev_get_regmap(struct device *dev, const char *name)
1124 struct regmap **r = devres_find(dev, dev_get_regmap_release,
1125 dev_get_regmap_match, (void *)name);
1131 EXPORT_SYMBOL_GPL(dev_get_regmap);
1134 * regmap_get_device(): Obtain the device from a regmap
1136 * @map: Register map to operate on.
1138 * Returns the underlying device that the regmap has been created for.
1140 struct device *regmap_get_device(struct regmap *map)
1144 EXPORT_SYMBOL_GPL(regmap_get_device);
1146 static int _regmap_select_page(struct regmap *map, unsigned int *reg,
1147 struct regmap_range_node *range,
1148 unsigned int val_num)
1150 void *orig_work_buf;
1151 unsigned int win_offset;
1152 unsigned int win_page;
1156 win_offset = (*reg - range->range_min) % range->window_len;
1157 win_page = (*reg - range->range_min) / range->window_len;
1160 /* Bulk write shouldn't cross range boundary */
1161 if (*reg + val_num - 1 > range->range_max)
1164 /* ... or single page boundary */
1165 if (val_num > range->window_len - win_offset)
1169 /* It is possible to have selector register inside data window.
1170 In that case, selector register is located on every page and
1171 it needs no page switching, when accessed alone. */
1173 range->window_start + win_offset != range->selector_reg) {
1174 /* Use separate work_buf during page switching */
1175 orig_work_buf = map->work_buf;
1176 map->work_buf = map->selector_work_buf;
1178 ret = _regmap_update_bits(map, range->selector_reg,
1179 range->selector_mask,
1180 win_page << range->selector_shift,
1183 map->work_buf = orig_work_buf;
1189 *reg = range->window_start + win_offset;
1194 int _regmap_raw_write(struct regmap *map, unsigned int reg,
1195 const void *val, size_t val_len)
1197 struct regmap_range_node *range;
1198 unsigned long flags;
1199 u8 *u8 = map->work_buf;
1200 void *work_val = map->work_buf + map->format.reg_bytes +
1201 map->format.pad_bytes;
1203 int ret = -ENOTSUPP;
1209 /* Check for unwritable registers before we start */
1210 if (map->writeable_reg)
1211 for (i = 0; i < val_len / map->format.val_bytes; i++)
1212 if (!map->writeable_reg(map->dev,
1213 reg + (i * map->reg_stride)))
1216 if (!map->cache_bypass && map->format.parse_val) {
1218 int val_bytes = map->format.val_bytes;
1219 for (i = 0; i < val_len / val_bytes; i++) {
1220 ival = map->format.parse_val(val + (i * val_bytes));
1221 ret = regcache_write(map, reg + (i * map->reg_stride),
1225 "Error in caching of register: %x ret: %d\n",
1230 if (map->cache_only) {
1231 map->cache_dirty = true;
1236 range = _regmap_range_lookup(map, reg);
1238 int val_num = val_len / map->format.val_bytes;
1239 int win_offset = (reg - range->range_min) % range->window_len;
1240 int win_residue = range->window_len - win_offset;
1242 /* If the write goes beyond the end of the window split it */
1243 while (val_num > win_residue) {
1244 dev_dbg(map->dev, "Writing window %d/%zu\n",
1245 win_residue, val_len / map->format.val_bytes);
1246 ret = _regmap_raw_write(map, reg, val, win_residue *
1247 map->format.val_bytes);
1252 val_num -= win_residue;
1253 val += win_residue * map->format.val_bytes;
1254 val_len -= win_residue * map->format.val_bytes;
1256 win_offset = (reg - range->range_min) %
1258 win_residue = range->window_len - win_offset;
1261 ret = _regmap_select_page(map, ®, range, val_num);
1266 map->format.format_reg(map->work_buf, reg, map->reg_shift);
1268 u8[0] |= map->write_flag_mask;
1271 * Essentially all I/O mechanisms will be faster with a single
1272 * buffer to write. Since register syncs often generate raw
1273 * writes of single registers optimise that case.
1275 if (val != work_val && val_len == map->format.val_bytes) {
1276 memcpy(work_val, val, map->format.val_bytes);
1280 if (map->async && map->bus->async_write) {
1281 struct regmap_async *async;
1283 trace_regmap_async_write_start(map->dev, reg, val_len);
1285 spin_lock_irqsave(&map->async_lock, flags);
1286 async = list_first_entry_or_null(&map->async_free,
1287 struct regmap_async,
1290 list_del(&async->list);
1291 spin_unlock_irqrestore(&map->async_lock, flags);
1294 async = map->bus->async_alloc();
1298 async->work_buf = kzalloc(map->format.buf_size,
1299 GFP_KERNEL | GFP_DMA);
1300 if (!async->work_buf) {
1308 /* If the caller supplied the value we can use it safely. */
1309 memcpy(async->work_buf, map->work_buf, map->format.pad_bytes +
1310 map->format.reg_bytes + map->format.val_bytes);
1312 spin_lock_irqsave(&map->async_lock, flags);
1313 list_add_tail(&async->list, &map->async_list);
1314 spin_unlock_irqrestore(&map->async_lock, flags);
1316 if (val != work_val)
1317 ret = map->bus->async_write(map->bus_context,
1319 map->format.reg_bytes +
1320 map->format.pad_bytes,
1321 val, val_len, async);
1323 ret = map->bus->async_write(map->bus_context,
1325 map->format.reg_bytes +
1326 map->format.pad_bytes +
1327 val_len, NULL, 0, async);
1330 dev_err(map->dev, "Failed to schedule write: %d\n",
1333 spin_lock_irqsave(&map->async_lock, flags);
1334 list_move(&async->list, &map->async_free);
1335 spin_unlock_irqrestore(&map->async_lock, flags);
1341 trace_regmap_hw_write_start(map->dev, reg,
1342 val_len / map->format.val_bytes);
1344 /* If we're doing a single register write we can probably just
1345 * send the work_buf directly, otherwise try to do a gather
1348 if (val == work_val)
1349 ret = map->bus->write(map->bus_context, map->work_buf,
1350 map->format.reg_bytes +
1351 map->format.pad_bytes +
1353 else if (map->bus->gather_write)
1354 ret = map->bus->gather_write(map->bus_context, map->work_buf,
1355 map->format.reg_bytes +
1356 map->format.pad_bytes,
1359 /* If that didn't work fall back on linearising by hand. */
1360 if (ret == -ENOTSUPP) {
1361 len = map->format.reg_bytes + map->format.pad_bytes + val_len;
1362 buf = kzalloc(len, GFP_KERNEL);
1366 memcpy(buf, map->work_buf, map->format.reg_bytes);
1367 memcpy(buf + map->format.reg_bytes + map->format.pad_bytes,
1369 ret = map->bus->write(map->bus_context, buf, len);
1374 trace_regmap_hw_write_done(map->dev, reg,
1375 val_len / map->format.val_bytes);
1381 * regmap_can_raw_write - Test if regmap_raw_write() is supported
1383 * @map: Map to check.
1385 bool regmap_can_raw_write(struct regmap *map)
1387 return map->bus && map->format.format_val && map->format.format_reg;
1389 EXPORT_SYMBOL_GPL(regmap_can_raw_write);
1391 static int _regmap_bus_formatted_write(void *context, unsigned int reg,
1395 struct regmap_range_node *range;
1396 struct regmap *map = context;
1398 WARN_ON(!map->bus || !map->format.format_write);
1400 range = _regmap_range_lookup(map, reg);
1402 ret = _regmap_select_page(map, ®, range, 1);
1407 map->format.format_write(map, reg, val);
1409 trace_regmap_hw_write_start(map->dev, reg, 1);
1411 ret = map->bus->write(map->bus_context, map->work_buf,
1412 map->format.buf_size);
1414 trace_regmap_hw_write_done(map->dev, reg, 1);
1419 static int _regmap_bus_reg_write(void *context, unsigned int reg,
1422 struct regmap *map = context;
1424 return map->bus->reg_write(map->bus_context, reg, val);
1427 static int _regmap_bus_raw_write(void *context, unsigned int reg,
1430 struct regmap *map = context;
1432 WARN_ON(!map->bus || !map->format.format_val);
1434 map->format.format_val(map->work_buf + map->format.reg_bytes
1435 + map->format.pad_bytes, val, 0);
1436 return _regmap_raw_write(map, reg,
1438 map->format.reg_bytes +
1439 map->format.pad_bytes,
1440 map->format.val_bytes);
1443 static inline void *_regmap_map_get_context(struct regmap *map)
1445 return (map->bus) ? map : map->bus_context;
1448 int _regmap_write(struct regmap *map, unsigned int reg,
1452 void *context = _regmap_map_get_context(map);
1454 if (!regmap_writeable(map, reg))
1457 if (!map->cache_bypass && !map->defer_caching) {
1458 ret = regcache_write(map, reg, val);
1461 if (map->cache_only) {
1462 map->cache_dirty = true;
1468 if (map->dev && strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
1469 dev_info(map->dev, "%x <= %x\n", reg, val);
1472 trace_regmap_reg_write(map->dev, reg, val);
1474 return map->reg_write(context, reg, val);
1478 * regmap_write(): Write a value to a single register
1480 * @map: Register map to write to
1481 * @reg: Register to write to
1482 * @val: Value to be written
1484 * A value of zero will be returned on success, a negative errno will
1485 * be returned in error cases.
1487 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
1491 if (reg % map->reg_stride)
1494 map->lock(map->lock_arg);
1496 ret = _regmap_write(map, reg, val);
1498 map->unlock(map->lock_arg);
1502 EXPORT_SYMBOL_GPL(regmap_write);
1505 * regmap_write_async(): Write a value to a single register asynchronously
1507 * @map: Register map to write to
1508 * @reg: Register to write to
1509 * @val: Value to be written
1511 * A value of zero will be returned on success, a negative errno will
1512 * be returned in error cases.
1514 int regmap_write_async(struct regmap *map, unsigned int reg, unsigned int val)
1518 if (reg % map->reg_stride)
1521 map->lock(map->lock_arg);
1525 ret = _regmap_write(map, reg, val);
1529 map->unlock(map->lock_arg);
1533 EXPORT_SYMBOL_GPL(regmap_write_async);
1536 * regmap_raw_write(): Write raw values to one or more registers
1538 * @map: Register map to write to
1539 * @reg: Initial register to write to
1540 * @val: Block of data to be written, laid out for direct transmission to the
1542 * @val_len: Length of data pointed to by val.
1544 * This function is intended to be used for things like firmware
1545 * download where a large block of data needs to be transferred to the
1546 * device. No formatting will be done on the data provided.
1548 * A value of zero will be returned on success, a negative errno will
1549 * be returned in error cases.
1551 int regmap_raw_write(struct regmap *map, unsigned int reg,
1552 const void *val, size_t val_len)
1556 if (!regmap_can_raw_write(map))
1558 if (val_len % map->format.val_bytes)
1561 map->lock(map->lock_arg);
1563 ret = _regmap_raw_write(map, reg, val, val_len);
1565 map->unlock(map->lock_arg);
1569 EXPORT_SYMBOL_GPL(regmap_raw_write);
1572 * regmap_field_write(): Write a value to a single register field
1574 * @field: Register field to write to
1575 * @val: Value to be written
1577 * A value of zero will be returned on success, a negative errno will
1578 * be returned in error cases.
1580 int regmap_field_write(struct regmap_field *field, unsigned int val)
1582 return regmap_update_bits(field->regmap, field->reg,
1583 field->mask, val << field->shift);
1585 EXPORT_SYMBOL_GPL(regmap_field_write);
1588 * regmap_field_update_bits(): Perform a read/modify/write cycle
1589 * on the register field
1591 * @field: Register field to write to
1592 * @mask: Bitmask to change
1593 * @val: Value to be written
1595 * A value of zero will be returned on success, a negative errno will
1596 * be returned in error cases.
1598 int regmap_field_update_bits(struct regmap_field *field, unsigned int mask, unsigned int val)
1600 mask = (mask << field->shift) & field->mask;
1602 return regmap_update_bits(field->regmap, field->reg,
1603 mask, val << field->shift);
1605 EXPORT_SYMBOL_GPL(regmap_field_update_bits);
1608 * regmap_fields_write(): Write a value to a single register field with port ID
1610 * @field: Register field to write to
1612 * @val: Value to be written
1614 * A value of zero will be returned on success, a negative errno will
1615 * be returned in error cases.
1617 int regmap_fields_write(struct regmap_field *field, unsigned int id,
1620 if (id >= field->id_size)
1623 return regmap_update_bits(field->regmap,
1624 field->reg + (field->id_offset * id),
1625 field->mask, val << field->shift);
1627 EXPORT_SYMBOL_GPL(regmap_fields_write);
1630 * regmap_fields_update_bits(): Perform a read/modify/write cycle
1631 * on the register field
1633 * @field: Register field to write to
1635 * @mask: Bitmask to change
1636 * @val: Value to be written
1638 * A value of zero will be returned on success, a negative errno will
1639 * be returned in error cases.
1641 int regmap_fields_update_bits(struct regmap_field *field, unsigned int id,
1642 unsigned int mask, unsigned int val)
1644 if (id >= field->id_size)
1647 mask = (mask << field->shift) & field->mask;
1649 return regmap_update_bits(field->regmap,
1650 field->reg + (field->id_offset * id),
1651 mask, val << field->shift);
1653 EXPORT_SYMBOL_GPL(regmap_fields_update_bits);
1656 * regmap_bulk_write(): Write multiple registers to the device
1658 * @map: Register map to write to
1659 * @reg: First register to be write from
1660 * @val: Block of data to be written, in native register size for device
1661 * @val_count: Number of registers to write
1663 * This function is intended to be used for writing a large block of
1664 * data to the device either in single transfer or multiple transfer.
1666 * A value of zero will be returned on success, a negative errno will
1667 * be returned in error cases.
1669 int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
1673 size_t val_bytes = map->format.val_bytes;
1675 if (map->bus && !map->format.parse_inplace)
1677 if (reg % map->reg_stride)
1681 * Some devices don't support bulk write, for
1682 * them we have a series of single write operations.
1684 if (!map->bus || map->use_single_rw) {
1685 map->lock(map->lock_arg);
1686 for (i = 0; i < val_count; i++) {
1689 switch (val_bytes) {
1691 ival = *(u8 *)(val + (i * val_bytes));
1694 ival = *(u16 *)(val + (i * val_bytes));
1697 ival = *(u32 *)(val + (i * val_bytes));
1701 ival = *(u64 *)(val + (i * val_bytes));
1709 ret = _regmap_write(map, reg + (i * map->reg_stride),
1715 map->unlock(map->lock_arg);
1722 wval = kmemdup(val, val_count * val_bytes, GFP_KERNEL);
1724 dev_err(map->dev, "Error in memory allocation\n");
1727 for (i = 0; i < val_count * val_bytes; i += val_bytes)
1728 map->format.parse_inplace(wval + i);
1730 map->lock(map->lock_arg);
1731 ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count);
1732 map->unlock(map->lock_arg);
1738 EXPORT_SYMBOL_GPL(regmap_bulk_write);
1741 * _regmap_raw_multi_reg_write()
1743 * the (register,newvalue) pairs in regs have not been formatted, but
1744 * they are all in the same page and have been changed to being page
1745 * relative. The page register has been written if that was neccessary.
1747 static int _regmap_raw_multi_reg_write(struct regmap *map,
1748 const struct reg_default *regs,
1755 size_t val_bytes = map->format.val_bytes;
1756 size_t reg_bytes = map->format.reg_bytes;
1757 size_t pad_bytes = map->format.pad_bytes;
1758 size_t pair_size = reg_bytes + pad_bytes + val_bytes;
1759 size_t len = pair_size * num_regs;
1764 buf = kzalloc(len, GFP_KERNEL);
1768 /* We have to linearise by hand. */
1772 for (i = 0; i < num_regs; i++) {
1773 int reg = regs[i].reg;
1774 int val = regs[i].def;
1775 trace_regmap_hw_write_start(map->dev, reg, 1);
1776 map->format.format_reg(u8, reg, map->reg_shift);
1777 u8 += reg_bytes + pad_bytes;
1778 map->format.format_val(u8, val, 0);
1782 *u8 |= map->write_flag_mask;
1784 ret = map->bus->write(map->bus_context, buf, len);
1788 for (i = 0; i < num_regs; i++) {
1789 int reg = regs[i].reg;
1790 trace_regmap_hw_write_done(map->dev, reg, 1);
1795 static unsigned int _regmap_register_page(struct regmap *map,
1797 struct regmap_range_node *range)
1799 unsigned int win_page = (reg - range->range_min) / range->window_len;
1804 static int _regmap_range_multi_paged_reg_write(struct regmap *map,
1805 struct reg_default *regs,
1810 struct reg_default *base;
1811 unsigned int this_page = 0;
1813 * the set of registers are not neccessarily in order, but
1814 * since the order of write must be preserved this algorithm
1815 * chops the set each time the page changes
1818 for (i = 0, n = 0; i < num_regs; i++, n++) {
1819 unsigned int reg = regs[i].reg;
1820 struct regmap_range_node *range;
1822 range = _regmap_range_lookup(map, reg);
1824 unsigned int win_page = _regmap_register_page(map, reg,
1828 this_page = win_page;
1829 if (win_page != this_page) {
1830 this_page = win_page;
1831 ret = _regmap_raw_multi_reg_write(map, base, n);
1837 ret = _regmap_select_page(map, &base[n].reg, range, 1);
1843 return _regmap_raw_multi_reg_write(map, base, n);
1847 static int _regmap_multi_reg_write(struct regmap *map,
1848 const struct reg_default *regs,
1854 if (!map->can_multi_write) {
1855 for (i = 0; i < num_regs; i++) {
1856 ret = _regmap_write(map, regs[i].reg, regs[i].def);
1863 if (!map->format.parse_inplace)
1866 if (map->writeable_reg)
1867 for (i = 0; i < num_regs; i++) {
1868 int reg = regs[i].reg;
1869 if (!map->writeable_reg(map->dev, reg))
1871 if (reg % map->reg_stride)
1875 if (!map->cache_bypass) {
1876 for (i = 0; i < num_regs; i++) {
1877 unsigned int val = regs[i].def;
1878 unsigned int reg = regs[i].reg;
1879 ret = regcache_write(map, reg, val);
1882 "Error in caching of register: %x ret: %d\n",
1887 if (map->cache_only) {
1888 map->cache_dirty = true;
1895 for (i = 0; i < num_regs; i++) {
1896 unsigned int reg = regs[i].reg;
1897 struct regmap_range_node *range;
1898 range = _regmap_range_lookup(map, reg);
1900 size_t len = sizeof(struct reg_default)*num_regs;
1901 struct reg_default *base = kmemdup(regs, len,
1905 ret = _regmap_range_multi_paged_reg_write(map, base,
1912 return _regmap_raw_multi_reg_write(map, regs, num_regs);
1916 * regmap_multi_reg_write(): Write multiple registers to the device
1918 * where the set of register,value pairs are supplied in any order,
1919 * possibly not all in a single range.
1921 * @map: Register map to write to
1922 * @regs: Array of structures containing register,value to be written
1923 * @num_regs: Number of registers to write
1925 * The 'normal' block write mode will send ultimately send data on the
1926 * target bus as R,V1,V2,V3,..,Vn where successively higer registers are
1927 * addressed. However, this alternative block multi write mode will send
1928 * the data as R1,V1,R2,V2,..,Rn,Vn on the target bus. The target device
1929 * must of course support the mode.
1931 * A value of zero will be returned on success, a negative errno will be
1932 * returned in error cases.
1934 int regmap_multi_reg_write(struct regmap *map, const struct reg_default *regs,
1939 map->lock(map->lock_arg);
1941 ret = _regmap_multi_reg_write(map, regs, num_regs);
1943 map->unlock(map->lock_arg);
1947 EXPORT_SYMBOL_GPL(regmap_multi_reg_write);
1950 * regmap_multi_reg_write_bypassed(): Write multiple registers to the
1951 * device but not the cache
1953 * where the set of register are supplied in any order
1955 * @map: Register map to write to
1956 * @regs: Array of structures containing register,value to be written
1957 * @num_regs: Number of registers to write
1959 * This function is intended to be used for writing a large block of data
1960 * atomically to the device in single transfer for those I2C client devices
1961 * that implement this alternative block write mode.
1963 * A value of zero will be returned on success, a negative errno will
1964 * be returned in error cases.
1966 int regmap_multi_reg_write_bypassed(struct regmap *map,
1967 const struct reg_default *regs,
1973 map->lock(map->lock_arg);
1975 bypass = map->cache_bypass;
1976 map->cache_bypass = true;
1978 ret = _regmap_multi_reg_write(map, regs, num_regs);
1980 map->cache_bypass = bypass;
1982 map->unlock(map->lock_arg);
1986 EXPORT_SYMBOL_GPL(regmap_multi_reg_write_bypassed);
1989 * regmap_raw_write_async(): Write raw values to one or more registers
1992 * @map: Register map to write to
1993 * @reg: Initial register to write to
1994 * @val: Block of data to be written, laid out for direct transmission to the
1995 * device. Must be valid until regmap_async_complete() is called.
1996 * @val_len: Length of data pointed to by val.
1998 * This function is intended to be used for things like firmware
1999 * download where a large block of data needs to be transferred to the
2000 * device. No formatting will be done on the data provided.
2002 * If supported by the underlying bus the write will be scheduled
2003 * asynchronously, helping maximise I/O speed on higher speed buses
2004 * like SPI. regmap_async_complete() can be called to ensure that all
2005 * asynchrnous writes have been completed.
2007 * A value of zero will be returned on success, a negative errno will
2008 * be returned in error cases.
2010 int regmap_raw_write_async(struct regmap *map, unsigned int reg,
2011 const void *val, size_t val_len)
2015 if (val_len % map->format.val_bytes)
2017 if (reg % map->reg_stride)
2020 map->lock(map->lock_arg);
2024 ret = _regmap_raw_write(map, reg, val, val_len);
2028 map->unlock(map->lock_arg);
2032 EXPORT_SYMBOL_GPL(regmap_raw_write_async);
2034 static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
2035 unsigned int val_len)
2037 struct regmap_range_node *range;
2038 u8 *u8 = map->work_buf;
2043 range = _regmap_range_lookup(map, reg);
2045 ret = _regmap_select_page(map, ®, range,
2046 val_len / map->format.val_bytes);
2051 map->format.format_reg(map->work_buf, reg, map->reg_shift);
2054 * Some buses or devices flag reads by setting the high bits in the
2055 * register addresss; since it's always the high bits for all
2056 * current formats we can do this here rather than in
2057 * formatting. This may break if we get interesting formats.
2059 u8[0] |= map->read_flag_mask;
2061 trace_regmap_hw_read_start(map->dev, reg,
2062 val_len / map->format.val_bytes);
2064 ret = map->bus->read(map->bus_context, map->work_buf,
2065 map->format.reg_bytes + map->format.pad_bytes,
2068 trace_regmap_hw_read_done(map->dev, reg,
2069 val_len / map->format.val_bytes);
2074 static int _regmap_bus_reg_read(void *context, unsigned int reg,
2077 struct regmap *map = context;
2079 return map->bus->reg_read(map->bus_context, reg, val);
2082 static int _regmap_bus_read(void *context, unsigned int reg,
2086 struct regmap *map = context;
2088 if (!map->format.parse_val)
2091 ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
2093 *val = map->format.parse_val(map->work_buf);
2098 static int _regmap_read(struct regmap *map, unsigned int reg,
2102 void *context = _regmap_map_get_context(map);
2104 WARN_ON(!map->reg_read);
2106 if (!map->cache_bypass) {
2107 ret = regcache_read(map, reg, val);
2112 if (map->cache_only)
2115 if (!regmap_readable(map, reg))
2118 ret = map->reg_read(context, reg, val);
2121 if (map->dev && strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
2122 dev_info(map->dev, "%x => %x\n", reg, *val);
2125 trace_regmap_reg_read(map->dev, reg, *val);
2127 if (!map->cache_bypass)
2128 regcache_write(map, reg, *val);
2135 * regmap_read(): Read a value from a single register
2137 * @map: Register map to read from
2138 * @reg: Register to be read from
2139 * @val: Pointer to store read value
2141 * A value of zero will be returned on success, a negative errno will
2142 * be returned in error cases.
2144 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
2148 if (reg % map->reg_stride)
2151 map->lock(map->lock_arg);
2153 ret = _regmap_read(map, reg, val);
2155 map->unlock(map->lock_arg);
2159 EXPORT_SYMBOL_GPL(regmap_read);
2162 * regmap_raw_read(): Read raw data from the device
2164 * @map: Register map to read from
2165 * @reg: First register to be read from
2166 * @val: Pointer to store read value
2167 * @val_len: Size of data to read
2169 * A value of zero will be returned on success, a negative errno will
2170 * be returned in error cases.
2172 int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
2175 size_t val_bytes = map->format.val_bytes;
2176 size_t val_count = val_len / val_bytes;
2182 if (val_len % map->format.val_bytes)
2184 if (reg % map->reg_stride)
2187 map->lock(map->lock_arg);
2189 if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
2190 map->cache_type == REGCACHE_NONE) {
2191 /* Physical block read if there's no cache involved */
2192 ret = _regmap_raw_read(map, reg, val, val_len);
2195 /* Otherwise go word by word for the cache; should be low
2196 * cost as we expect to hit the cache.
2198 for (i = 0; i < val_count; i++) {
2199 ret = _regmap_read(map, reg + (i * map->reg_stride),
2204 map->format.format_val(val + (i * val_bytes), v, 0);
2209 map->unlock(map->lock_arg);
2213 EXPORT_SYMBOL_GPL(regmap_raw_read);
2216 * regmap_field_read(): Read a value to a single register field
2218 * @field: Register field to read from
2219 * @val: Pointer to store read value
2221 * A value of zero will be returned on success, a negative errno will
2222 * be returned in error cases.
2224 int regmap_field_read(struct regmap_field *field, unsigned int *val)
2227 unsigned int reg_val;
2228 ret = regmap_read(field->regmap, field->reg, ®_val);
2232 reg_val &= field->mask;
2233 reg_val >>= field->shift;
2238 EXPORT_SYMBOL_GPL(regmap_field_read);
2241 * regmap_fields_read(): Read a value to a single register field with port ID
2243 * @field: Register field to read from
2245 * @val: Pointer to store read value
2247 * A value of zero will be returned on success, a negative errno will
2248 * be returned in error cases.
2250 int regmap_fields_read(struct regmap_field *field, unsigned int id,
2254 unsigned int reg_val;
2256 if (id >= field->id_size)
2259 ret = regmap_read(field->regmap,
2260 field->reg + (field->id_offset * id),
2265 reg_val &= field->mask;
2266 reg_val >>= field->shift;
2271 EXPORT_SYMBOL_GPL(regmap_fields_read);
2274 * regmap_bulk_read(): Read multiple registers from the device
2276 * @map: Register map to read from
2277 * @reg: First register to be read from
2278 * @val: Pointer to store read value, in native register size for device
2279 * @val_count: Number of registers to read
2281 * A value of zero will be returned on success, a negative errno will
2282 * be returned in error cases.
2284 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
2288 size_t val_bytes = map->format.val_bytes;
2289 bool vol = regmap_volatile_range(map, reg, val_count);
2291 if (reg % map->reg_stride)
2294 if (map->bus && map->format.parse_inplace && (vol || map->cache_type == REGCACHE_NONE)) {
2296 * Some devices does not support bulk read, for
2297 * them we have a series of single read operations.
2299 if (map->use_single_rw) {
2300 for (i = 0; i < val_count; i++) {
2301 ret = regmap_raw_read(map,
2302 reg + (i * map->reg_stride),
2303 val + (i * val_bytes),
2309 ret = regmap_raw_read(map, reg, val,
2310 val_bytes * val_count);
2315 for (i = 0; i < val_count * val_bytes; i += val_bytes)
2316 map->format.parse_inplace(val + i);
2318 for (i = 0; i < val_count; i++) {
2320 ret = regmap_read(map, reg + (i * map->reg_stride),
2324 memcpy(val + (i * val_bytes), &ival, val_bytes);
2330 EXPORT_SYMBOL_GPL(regmap_bulk_read);
2332 static int _regmap_update_bits(struct regmap *map, unsigned int reg,
2333 unsigned int mask, unsigned int val,
2337 unsigned int tmp, orig;
2339 ret = _regmap_read(map, reg, &orig);
2347 ret = _regmap_write(map, reg, tmp);
2359 * regmap_update_bits: Perform a read/modify/write cycle on the register map
2361 * @map: Register map to update
2362 * @reg: Register to update
2363 * @mask: Bitmask to change
2364 * @val: New value for bitmask
2366 * Returns zero for success, a negative number on error.
2368 int regmap_update_bits(struct regmap *map, unsigned int reg,
2369 unsigned int mask, unsigned int val)
2373 map->lock(map->lock_arg);
2374 ret = _regmap_update_bits(map, reg, mask, val, NULL);
2375 map->unlock(map->lock_arg);
2379 EXPORT_SYMBOL_GPL(regmap_update_bits);
2382 * regmap_update_bits_async: Perform a read/modify/write cycle on the register
2383 * map asynchronously
2385 * @map: Register map to update
2386 * @reg: Register to update
2387 * @mask: Bitmask to change
2388 * @val: New value for bitmask
2390 * With most buses the read must be done synchronously so this is most
2391 * useful for devices with a cache which do not need to interact with
2392 * the hardware to determine the current register value.
2394 * Returns zero for success, a negative number on error.
2396 int regmap_update_bits_async(struct regmap *map, unsigned int reg,
2397 unsigned int mask, unsigned int val)
2401 map->lock(map->lock_arg);
2405 ret = _regmap_update_bits(map, reg, mask, val, NULL);
2409 map->unlock(map->lock_arg);
2413 EXPORT_SYMBOL_GPL(regmap_update_bits_async);
2416 * regmap_update_bits_check: Perform a read/modify/write cycle on the
2417 * register map and report if updated
2419 * @map: Register map to update
2420 * @reg: Register to update
2421 * @mask: Bitmask to change
2422 * @val: New value for bitmask
2423 * @change: Boolean indicating if a write was done
2425 * Returns zero for success, a negative number on error.
2427 int regmap_update_bits_check(struct regmap *map, unsigned int reg,
2428 unsigned int mask, unsigned int val,
2433 map->lock(map->lock_arg);
2434 ret = _regmap_update_bits(map, reg, mask, val, change);
2435 map->unlock(map->lock_arg);
2438 EXPORT_SYMBOL_GPL(regmap_update_bits_check);
2441 * regmap_update_bits_check_async: Perform a read/modify/write cycle on the
2442 * register map asynchronously and report if
2445 * @map: Register map to update
2446 * @reg: Register to update
2447 * @mask: Bitmask to change
2448 * @val: New value for bitmask
2449 * @change: Boolean indicating if a write was done
2451 * With most buses the read must be done synchronously so this is most
2452 * useful for devices with a cache which do not need to interact with
2453 * the hardware to determine the current register value.
2455 * Returns zero for success, a negative number on error.
2457 int regmap_update_bits_check_async(struct regmap *map, unsigned int reg,
2458 unsigned int mask, unsigned int val,
2463 map->lock(map->lock_arg);
2467 ret = _regmap_update_bits(map, reg, mask, val, change);
2471 map->unlock(map->lock_arg);
2475 EXPORT_SYMBOL_GPL(regmap_update_bits_check_async);
2477 void regmap_async_complete_cb(struct regmap_async *async, int ret)
2479 struct regmap *map = async->map;
2482 trace_regmap_async_io_complete(map->dev);
2484 spin_lock(&map->async_lock);
2485 list_move(&async->list, &map->async_free);
2486 wake = list_empty(&map->async_list);
2489 map->async_ret = ret;
2491 spin_unlock(&map->async_lock);
2494 wake_up(&map->async_waitq);
2496 EXPORT_SYMBOL_GPL(regmap_async_complete_cb);
2498 static int regmap_async_is_done(struct regmap *map)
2500 unsigned long flags;
2503 spin_lock_irqsave(&map->async_lock, flags);
2504 ret = list_empty(&map->async_list);
2505 spin_unlock_irqrestore(&map->async_lock, flags);
2511 * regmap_async_complete: Ensure all asynchronous I/O has completed.
2513 * @map: Map to operate on.
2515 * Blocks until any pending asynchronous I/O has completed. Returns
2516 * an error code for any failed I/O operations.
2518 int regmap_async_complete(struct regmap *map)
2520 unsigned long flags;
2523 /* Nothing to do with no async support */
2524 if (!map->bus || !map->bus->async_write)
2527 trace_regmap_async_complete_start(map->dev);
2529 wait_event(map->async_waitq, regmap_async_is_done(map));
2531 spin_lock_irqsave(&map->async_lock, flags);
2532 ret = map->async_ret;
2534 spin_unlock_irqrestore(&map->async_lock, flags);
2536 trace_regmap_async_complete_done(map->dev);
2540 EXPORT_SYMBOL_GPL(regmap_async_complete);
2543 * regmap_register_patch: Register and apply register updates to be applied
2544 * on device initialistion
2546 * @map: Register map to apply updates to.
2547 * @regs: Values to update.
2548 * @num_regs: Number of entries in regs.
2550 * Register a set of register updates to be applied to the device
2551 * whenever the device registers are synchronised with the cache and
2552 * apply them immediately. Typically this is used to apply
2553 * corrections to be applied to the device defaults on startup, such
2554 * as the updates some vendors provide to undocumented registers.
2556 * The caller must ensure that this function cannot be called
2557 * concurrently with either itself or regcache_sync().
2559 int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
2562 struct reg_default *p;
2566 if (WARN_ONCE(num_regs <= 0, "invalid registers number (%d)\n",
2570 p = krealloc(map->patch,
2571 sizeof(struct reg_default) * (map->patch_regs + num_regs),
2574 memcpy(p + map->patch_regs, regs, num_regs * sizeof(*regs));
2576 map->patch_regs += num_regs;
2581 map->lock(map->lock_arg);
2583 bypass = map->cache_bypass;
2585 map->cache_bypass = true;
2588 ret = _regmap_multi_reg_write(map, regs, num_regs);
2594 map->cache_bypass = bypass;
2596 map->unlock(map->lock_arg);
2598 regmap_async_complete(map);
2602 EXPORT_SYMBOL_GPL(regmap_register_patch);
2605 * regmap_get_val_bytes(): Report the size of a register value
2607 * Report the size of a register value, mainly intended to for use by
2608 * generic infrastructure built on top of regmap.
2610 int regmap_get_val_bytes(struct regmap *map)
2612 if (map->format.format_write)
2615 return map->format.val_bytes;
2617 EXPORT_SYMBOL_GPL(regmap_get_val_bytes);
2619 int regmap_parse_val(struct regmap *map, const void *buf,
2622 if (!map->format.parse_val)
2625 *val = map->format.parse_val(buf);
2629 EXPORT_SYMBOL_GPL(regmap_parse_val);
2631 static int __init regmap_initcall(void)
2633 regmap_debugfs_initcall();
2637 postcore_initcall(regmap_initcall);