2 * Register map access API
4 * Copyright 2011 Wolfson Microelectronics plc
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/device.h>
14 #include <linux/slab.h>
15 #include <linux/export.h>
16 #include <linux/mutex.h>
17 #include <linux/err.h>
19 #include <linux/rbtree.h>
20 #include <linux/sched.h>
21 #include <linux/delay.h>
23 #define CREATE_TRACE_POINTS
29 * Sometimes for failures during very early init the trace
30 * infrastructure isn't available early enough to be used. For this
31 * sort of problem defining LOG_DEVICE will add printks for basic
32 * register I/O on a specific device.
36 static int _regmap_update_bits(struct regmap *map, unsigned int reg,
37 unsigned int mask, unsigned int val,
38 bool *change, bool force_write);
40 static int _regmap_bus_reg_read(void *context, unsigned int reg,
42 static int _regmap_bus_read(void *context, unsigned int reg,
44 static int _regmap_bus_formatted_write(void *context, unsigned int reg,
46 static int _regmap_bus_reg_write(void *context, unsigned int reg,
48 static int _regmap_bus_raw_write(void *context, unsigned int reg,
51 bool regmap_reg_in_ranges(unsigned int reg,
52 const struct regmap_range *ranges,
55 const struct regmap_range *r;
58 for (i = 0, r = ranges; i < nranges; i++, r++)
59 if (regmap_reg_in_range(reg, r))
63 EXPORT_SYMBOL_GPL(regmap_reg_in_ranges);
65 bool regmap_check_range_table(struct regmap *map, unsigned int reg,
66 const struct regmap_access_table *table)
68 /* Check "no ranges" first */
69 if (regmap_reg_in_ranges(reg, table->no_ranges, table->n_no_ranges))
72 /* In case zero "yes ranges" are supplied, any reg is OK */
73 if (!table->n_yes_ranges)
76 return regmap_reg_in_ranges(reg, table->yes_ranges,
79 EXPORT_SYMBOL_GPL(regmap_check_range_table);
81 bool regmap_writeable(struct regmap *map, unsigned int reg)
83 if (map->max_register && reg > map->max_register)
86 if (map->writeable_reg)
87 return map->writeable_reg(map->dev, reg);
90 return regmap_check_range_table(map, reg, map->wr_table);
95 bool regmap_readable(struct regmap *map, unsigned int reg)
100 if (map->max_register && reg > map->max_register)
103 if (map->format.format_write)
106 if (map->readable_reg)
107 return map->readable_reg(map->dev, reg);
110 return regmap_check_range_table(map, reg, map->rd_table);
115 bool regmap_volatile(struct regmap *map, unsigned int reg)
117 if (!map->format.format_write && !regmap_readable(map, reg))
120 if (map->volatile_reg)
121 return map->volatile_reg(map->dev, reg);
123 if (map->volatile_table)
124 return regmap_check_range_table(map, reg, map->volatile_table);
132 bool regmap_precious(struct regmap *map, unsigned int reg)
134 if (!regmap_readable(map, reg))
137 if (map->precious_reg)
138 return map->precious_reg(map->dev, reg);
140 if (map->precious_table)
141 return regmap_check_range_table(map, reg, map->precious_table);
146 static bool regmap_volatile_range(struct regmap *map, unsigned int reg,
151 for (i = 0; i < num; i++)
152 if (!regmap_volatile(map, reg + i))
158 static void regmap_format_2_6_write(struct regmap *map,
159 unsigned int reg, unsigned int val)
161 u8 *out = map->work_buf;
163 *out = (reg << 6) | val;
166 static void regmap_format_4_12_write(struct regmap *map,
167 unsigned int reg, unsigned int val)
169 __be16 *out = map->work_buf;
170 *out = cpu_to_be16((reg << 12) | val);
173 static void regmap_format_7_9_write(struct regmap *map,
174 unsigned int reg, unsigned int val)
176 __be16 *out = map->work_buf;
177 *out = cpu_to_be16((reg << 9) | val);
180 static void regmap_format_10_14_write(struct regmap *map,
181 unsigned int reg, unsigned int val)
183 u8 *out = map->work_buf;
186 out[1] = (val >> 8) | (reg << 6);
190 static void regmap_format_8(void *buf, unsigned int val, unsigned int shift)
197 static void regmap_format_16_be(void *buf, unsigned int val, unsigned int shift)
201 b[0] = cpu_to_be16(val << shift);
204 static void regmap_format_16_le(void *buf, unsigned int val, unsigned int shift)
208 b[0] = cpu_to_le16(val << shift);
211 static void regmap_format_16_native(void *buf, unsigned int val,
214 *(u16 *)buf = val << shift;
217 static void regmap_format_24(void *buf, unsigned int val, unsigned int shift)
228 static void regmap_format_32_be(void *buf, unsigned int val, unsigned int shift)
232 b[0] = cpu_to_be32(val << shift);
235 static void regmap_format_32_le(void *buf, unsigned int val, unsigned int shift)
239 b[0] = cpu_to_le32(val << shift);
242 static void regmap_format_32_native(void *buf, unsigned int val,
245 *(u32 *)buf = val << shift;
248 static void regmap_parse_inplace_noop(void *buf)
252 static unsigned int regmap_parse_8(const void *buf)
259 static unsigned int regmap_parse_16_be(const void *buf)
261 const __be16 *b = buf;
263 return be16_to_cpu(b[0]);
266 static unsigned int regmap_parse_16_le(const void *buf)
268 const __le16 *b = buf;
270 return le16_to_cpu(b[0]);
273 static void regmap_parse_16_be_inplace(void *buf)
277 b[0] = be16_to_cpu(b[0]);
280 static void regmap_parse_16_le_inplace(void *buf)
284 b[0] = le16_to_cpu(b[0]);
287 static unsigned int regmap_parse_16_native(const void *buf)
292 static unsigned int regmap_parse_24(const void *buf)
295 unsigned int ret = b[2];
296 ret |= ((unsigned int)b[1]) << 8;
297 ret |= ((unsigned int)b[0]) << 16;
302 static unsigned int regmap_parse_32_be(const void *buf)
304 const __be32 *b = buf;
306 return be32_to_cpu(b[0]);
309 static unsigned int regmap_parse_32_le(const void *buf)
311 const __le32 *b = buf;
313 return le32_to_cpu(b[0]);
316 static void regmap_parse_32_be_inplace(void *buf)
320 b[0] = be32_to_cpu(b[0]);
323 static void regmap_parse_32_le_inplace(void *buf)
327 b[0] = le32_to_cpu(b[0]);
330 static unsigned int regmap_parse_32_native(const void *buf)
335 static void regmap_lock_mutex(void *__map)
337 struct regmap *map = __map;
338 mutex_lock(&map->mutex);
341 static void regmap_unlock_mutex(void *__map)
343 struct regmap *map = __map;
344 mutex_unlock(&map->mutex);
347 static void regmap_lock_spinlock(void *__map)
348 __acquires(&map->spinlock)
350 struct regmap *map = __map;
353 spin_lock_irqsave(&map->spinlock, flags);
354 map->spinlock_flags = flags;
357 static void regmap_unlock_spinlock(void *__map)
358 __releases(&map->spinlock)
360 struct regmap *map = __map;
361 spin_unlock_irqrestore(&map->spinlock, map->spinlock_flags);
364 static void dev_get_regmap_release(struct device *dev, void *res)
367 * We don't actually have anything to do here; the goal here
368 * is not to manage the regmap but to provide a simple way to
369 * get the regmap back given a struct device.
373 static bool _regmap_range_add(struct regmap *map,
374 struct regmap_range_node *data)
376 struct rb_root *root = &map->range_tree;
377 struct rb_node **new = &(root->rb_node), *parent = NULL;
380 struct regmap_range_node *this =
381 container_of(*new, struct regmap_range_node, node);
384 if (data->range_max < this->range_min)
385 new = &((*new)->rb_left);
386 else if (data->range_min > this->range_max)
387 new = &((*new)->rb_right);
392 rb_link_node(&data->node, parent, new);
393 rb_insert_color(&data->node, root);
398 static struct regmap_range_node *_regmap_range_lookup(struct regmap *map,
401 struct rb_node *node = map->range_tree.rb_node;
404 struct regmap_range_node *this =
405 container_of(node, struct regmap_range_node, node);
407 if (reg < this->range_min)
408 node = node->rb_left;
409 else if (reg > this->range_max)
410 node = node->rb_right;
418 static void regmap_range_exit(struct regmap *map)
420 struct rb_node *next;
421 struct regmap_range_node *range_node;
423 next = rb_first(&map->range_tree);
425 range_node = rb_entry(next, struct regmap_range_node, node);
426 next = rb_next(&range_node->node);
427 rb_erase(&range_node->node, &map->range_tree);
431 kfree(map->selector_work_buf);
434 int regmap_attach_dev(struct device *dev, struct regmap *map,
435 const struct regmap_config *config)
441 regmap_debugfs_init(map, config->name);
443 /* Add a devres resource for dev_get_regmap() */
444 m = devres_alloc(dev_get_regmap_release, sizeof(*m), GFP_KERNEL);
446 regmap_debugfs_exit(map);
454 EXPORT_SYMBOL_GPL(regmap_attach_dev);
456 static enum regmap_endian regmap_get_reg_endian(const struct regmap_bus *bus,
457 const struct regmap_config *config)
459 enum regmap_endian endian;
461 /* Retrieve the endianness specification from the regmap config */
462 endian = config->reg_format_endian;
464 /* If the regmap config specified a non-default value, use that */
465 if (endian != REGMAP_ENDIAN_DEFAULT)
468 /* Retrieve the endianness specification from the bus config */
469 if (bus && bus->reg_format_endian_default)
470 endian = bus->reg_format_endian_default;
472 /* If the bus specified a non-default value, use that */
473 if (endian != REGMAP_ENDIAN_DEFAULT)
476 /* Use this if no other value was found */
477 return REGMAP_ENDIAN_BIG;
480 enum regmap_endian regmap_get_val_endian(struct device *dev,
481 const struct regmap_bus *bus,
482 const struct regmap_config *config)
484 struct device_node *np;
485 enum regmap_endian endian;
487 /* Retrieve the endianness specification from the regmap config */
488 endian = config->val_format_endian;
490 /* If the regmap config specified a non-default value, use that */
491 if (endian != REGMAP_ENDIAN_DEFAULT)
494 /* If the dev and dev->of_node exist try to get endianness from DT */
495 if (dev && dev->of_node) {
498 /* Parse the device's DT node for an endianness specification */
499 if (of_property_read_bool(np, "big-endian"))
500 endian = REGMAP_ENDIAN_BIG;
501 else if (of_property_read_bool(np, "little-endian"))
502 endian = REGMAP_ENDIAN_LITTLE;
504 /* If the endianness was specified in DT, use that */
505 if (endian != REGMAP_ENDIAN_DEFAULT)
509 /* Retrieve the endianness specification from the bus config */
510 if (bus && bus->val_format_endian_default)
511 endian = bus->val_format_endian_default;
513 /* If the bus specified a non-default value, use that */
514 if (endian != REGMAP_ENDIAN_DEFAULT)
517 /* Use this if no other value was found */
518 return REGMAP_ENDIAN_BIG;
520 EXPORT_SYMBOL_GPL(regmap_get_val_endian);
522 struct regmap *__regmap_init(struct device *dev,
523 const struct regmap_bus *bus,
525 const struct regmap_config *config,
526 struct lock_class_key *lock_key,
527 const char *lock_name)
531 enum regmap_endian reg_endian, val_endian;
537 map = kzalloc(sizeof(*map), GFP_KERNEL);
543 if (config->lock && config->unlock) {
544 map->lock = config->lock;
545 map->unlock = config->unlock;
546 map->lock_arg = config->lock_arg;
548 if ((bus && bus->fast_io) ||
550 spin_lock_init(&map->spinlock);
551 map->lock = regmap_lock_spinlock;
552 map->unlock = regmap_unlock_spinlock;
553 lockdep_set_class_and_name(&map->spinlock,
554 lock_key, lock_name);
556 mutex_init(&map->mutex);
557 map->lock = regmap_lock_mutex;
558 map->unlock = regmap_unlock_mutex;
559 lockdep_set_class_and_name(&map->mutex,
560 lock_key, lock_name);
564 map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8);
565 map->format.pad_bytes = config->pad_bits / 8;
566 map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8);
567 map->format.buf_size = DIV_ROUND_UP(config->reg_bits +
568 config->val_bits + config->pad_bits, 8);
569 map->reg_shift = config->pad_bits % 8;
570 if (config->reg_stride)
571 map->reg_stride = config->reg_stride;
574 map->use_single_read = config->use_single_rw || !bus || !bus->read;
575 map->use_single_write = config->use_single_rw || !bus || !bus->write;
576 map->can_multi_write = config->can_multi_write && bus && bus->write;
578 map->max_raw_read = bus->max_raw_read;
579 map->max_raw_write = bus->max_raw_write;
583 map->bus_context = bus_context;
584 map->max_register = config->max_register;
585 map->wr_table = config->wr_table;
586 map->rd_table = config->rd_table;
587 map->volatile_table = config->volatile_table;
588 map->precious_table = config->precious_table;
589 map->writeable_reg = config->writeable_reg;
590 map->readable_reg = config->readable_reg;
591 map->volatile_reg = config->volatile_reg;
592 map->precious_reg = config->precious_reg;
593 map->cache_type = config->cache_type;
594 map->name = config->name;
596 spin_lock_init(&map->async_lock);
597 INIT_LIST_HEAD(&map->async_list);
598 INIT_LIST_HEAD(&map->async_free);
599 init_waitqueue_head(&map->async_waitq);
601 if (config->read_flag_mask || config->write_flag_mask) {
602 map->read_flag_mask = config->read_flag_mask;
603 map->write_flag_mask = config->write_flag_mask;
605 map->read_flag_mask = bus->read_flag_mask;
609 map->reg_read = config->reg_read;
610 map->reg_write = config->reg_write;
612 map->defer_caching = false;
613 goto skip_format_initialization;
614 } else if (!bus->read || !bus->write) {
615 map->reg_read = _regmap_bus_reg_read;
616 map->reg_write = _regmap_bus_reg_write;
618 map->defer_caching = false;
619 goto skip_format_initialization;
621 map->reg_read = _regmap_bus_read;
624 reg_endian = regmap_get_reg_endian(bus, config);
625 val_endian = regmap_get_val_endian(dev, bus, config);
627 switch (config->reg_bits + map->reg_shift) {
629 switch (config->val_bits) {
631 map->format.format_write = regmap_format_2_6_write;
639 switch (config->val_bits) {
641 map->format.format_write = regmap_format_4_12_write;
649 switch (config->val_bits) {
651 map->format.format_write = regmap_format_7_9_write;
659 switch (config->val_bits) {
661 map->format.format_write = regmap_format_10_14_write;
669 map->format.format_reg = regmap_format_8;
673 switch (reg_endian) {
674 case REGMAP_ENDIAN_BIG:
675 map->format.format_reg = regmap_format_16_be;
677 case REGMAP_ENDIAN_NATIVE:
678 map->format.format_reg = regmap_format_16_native;
686 if (reg_endian != REGMAP_ENDIAN_BIG)
688 map->format.format_reg = regmap_format_24;
692 switch (reg_endian) {
693 case REGMAP_ENDIAN_BIG:
694 map->format.format_reg = regmap_format_32_be;
696 case REGMAP_ENDIAN_NATIVE:
697 map->format.format_reg = regmap_format_32_native;
708 if (val_endian == REGMAP_ENDIAN_NATIVE)
709 map->format.parse_inplace = regmap_parse_inplace_noop;
711 switch (config->val_bits) {
713 map->format.format_val = regmap_format_8;
714 map->format.parse_val = regmap_parse_8;
715 map->format.parse_inplace = regmap_parse_inplace_noop;
718 switch (val_endian) {
719 case REGMAP_ENDIAN_BIG:
720 map->format.format_val = regmap_format_16_be;
721 map->format.parse_val = regmap_parse_16_be;
722 map->format.parse_inplace = regmap_parse_16_be_inplace;
724 case REGMAP_ENDIAN_LITTLE:
725 map->format.format_val = regmap_format_16_le;
726 map->format.parse_val = regmap_parse_16_le;
727 map->format.parse_inplace = regmap_parse_16_le_inplace;
729 case REGMAP_ENDIAN_NATIVE:
730 map->format.format_val = regmap_format_16_native;
731 map->format.parse_val = regmap_parse_16_native;
738 if (val_endian != REGMAP_ENDIAN_BIG)
740 map->format.format_val = regmap_format_24;
741 map->format.parse_val = regmap_parse_24;
744 switch (val_endian) {
745 case REGMAP_ENDIAN_BIG:
746 map->format.format_val = regmap_format_32_be;
747 map->format.parse_val = regmap_parse_32_be;
748 map->format.parse_inplace = regmap_parse_32_be_inplace;
750 case REGMAP_ENDIAN_LITTLE:
751 map->format.format_val = regmap_format_32_le;
752 map->format.parse_val = regmap_parse_32_le;
753 map->format.parse_inplace = regmap_parse_32_le_inplace;
755 case REGMAP_ENDIAN_NATIVE:
756 map->format.format_val = regmap_format_32_native;
757 map->format.parse_val = regmap_parse_32_native;
765 if (map->format.format_write) {
766 if ((reg_endian != REGMAP_ENDIAN_BIG) ||
767 (val_endian != REGMAP_ENDIAN_BIG))
769 map->use_single_write = true;
772 if (!map->format.format_write &&
773 !(map->format.format_reg && map->format.format_val))
776 map->work_buf = kzalloc(map->format.buf_size, GFP_KERNEL);
777 if (map->work_buf == NULL) {
782 if (map->format.format_write) {
783 map->defer_caching = false;
784 map->reg_write = _regmap_bus_formatted_write;
785 } else if (map->format.format_val) {
786 map->defer_caching = true;
787 map->reg_write = _regmap_bus_raw_write;
790 skip_format_initialization:
792 map->range_tree = RB_ROOT;
793 for (i = 0; i < config->num_ranges; i++) {
794 const struct regmap_range_cfg *range_cfg = &config->ranges[i];
795 struct regmap_range_node *new;
798 if (range_cfg->range_max < range_cfg->range_min) {
799 dev_err(map->dev, "Invalid range %d: %d < %d\n", i,
800 range_cfg->range_max, range_cfg->range_min);
804 if (range_cfg->range_max > map->max_register) {
805 dev_err(map->dev, "Invalid range %d: %d > %d\n", i,
806 range_cfg->range_max, map->max_register);
810 if (range_cfg->selector_reg > map->max_register) {
812 "Invalid range %d: selector out of map\n", i);
816 if (range_cfg->window_len == 0) {
817 dev_err(map->dev, "Invalid range %d: window_len 0\n",
822 /* Make sure, that this register range has no selector
823 or data window within its boundary */
824 for (j = 0; j < config->num_ranges; j++) {
825 unsigned sel_reg = config->ranges[j].selector_reg;
826 unsigned win_min = config->ranges[j].window_start;
827 unsigned win_max = win_min +
828 config->ranges[j].window_len - 1;
830 /* Allow data window inside its own virtual range */
834 if (range_cfg->range_min <= sel_reg &&
835 sel_reg <= range_cfg->range_max) {
837 "Range %d: selector for %d in window\n",
842 if (!(win_max < range_cfg->range_min ||
843 win_min > range_cfg->range_max)) {
845 "Range %d: window for %d in window\n",
851 new = kzalloc(sizeof(*new), GFP_KERNEL);
858 new->name = range_cfg->name;
859 new->range_min = range_cfg->range_min;
860 new->range_max = range_cfg->range_max;
861 new->selector_reg = range_cfg->selector_reg;
862 new->selector_mask = range_cfg->selector_mask;
863 new->selector_shift = range_cfg->selector_shift;
864 new->window_start = range_cfg->window_start;
865 new->window_len = range_cfg->window_len;
867 if (!_regmap_range_add(map, new)) {
868 dev_err(map->dev, "Failed to add range %d\n", i);
873 if (map->selector_work_buf == NULL) {
874 map->selector_work_buf =
875 kzalloc(map->format.buf_size, GFP_KERNEL);
876 if (map->selector_work_buf == NULL) {
883 ret = regcache_init(map, config);
888 ret = regmap_attach_dev(dev, map, config);
898 regmap_range_exit(map);
899 kfree(map->work_buf);
905 EXPORT_SYMBOL_GPL(__regmap_init);
907 static void devm_regmap_release(struct device *dev, void *res)
909 regmap_exit(*(struct regmap **)res);
912 struct regmap *__devm_regmap_init(struct device *dev,
913 const struct regmap_bus *bus,
915 const struct regmap_config *config,
916 struct lock_class_key *lock_key,
917 const char *lock_name)
919 struct regmap **ptr, *regmap;
921 ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL);
923 return ERR_PTR(-ENOMEM);
925 regmap = __regmap_init(dev, bus, bus_context, config,
926 lock_key, lock_name);
927 if (!IS_ERR(regmap)) {
929 devres_add(dev, ptr);
936 EXPORT_SYMBOL_GPL(__devm_regmap_init);
938 static void regmap_field_init(struct regmap_field *rm_field,
939 struct regmap *regmap, struct reg_field reg_field)
941 rm_field->regmap = regmap;
942 rm_field->reg = reg_field.reg;
943 rm_field->shift = reg_field.lsb;
944 rm_field->mask = GENMASK(reg_field.msb, reg_field.lsb);
945 rm_field->id_size = reg_field.id_size;
946 rm_field->id_offset = reg_field.id_offset;
950 * devm_regmap_field_alloc(): Allocate and initialise a register field
953 * @dev: Device that will be interacted with
954 * @regmap: regmap bank in which this register field is located.
955 * @reg_field: Register field with in the bank.
957 * The return value will be an ERR_PTR() on error or a valid pointer
958 * to a struct regmap_field. The regmap_field will be automatically freed
959 * by the device management code.
961 struct regmap_field *devm_regmap_field_alloc(struct device *dev,
962 struct regmap *regmap, struct reg_field reg_field)
964 struct regmap_field *rm_field = devm_kzalloc(dev,
965 sizeof(*rm_field), GFP_KERNEL);
967 return ERR_PTR(-ENOMEM);
969 regmap_field_init(rm_field, regmap, reg_field);
974 EXPORT_SYMBOL_GPL(devm_regmap_field_alloc);
977 * devm_regmap_field_free(): Free register field allocated using
978 * devm_regmap_field_alloc. Usally drivers need not call this function,
979 * as the memory allocated via devm will be freed as per device-driver
982 * @dev: Device that will be interacted with
983 * @field: regmap field which should be freed.
985 void devm_regmap_field_free(struct device *dev,
986 struct regmap_field *field)
988 devm_kfree(dev, field);
990 EXPORT_SYMBOL_GPL(devm_regmap_field_free);
993 * regmap_field_alloc(): Allocate and initialise a register field
996 * @regmap: regmap bank in which this register field is located.
997 * @reg_field: Register field with in the bank.
999 * The return value will be an ERR_PTR() on error or a valid pointer
1000 * to a struct regmap_field. The regmap_field should be freed by the
1001 * user once its finished working with it using regmap_field_free().
1003 struct regmap_field *regmap_field_alloc(struct regmap *regmap,
1004 struct reg_field reg_field)
1006 struct regmap_field *rm_field = kzalloc(sizeof(*rm_field), GFP_KERNEL);
1009 return ERR_PTR(-ENOMEM);
1011 regmap_field_init(rm_field, regmap, reg_field);
1015 EXPORT_SYMBOL_GPL(regmap_field_alloc);
1018 * regmap_field_free(): Free register field allocated using regmap_field_alloc
1020 * @field: regmap field which should be freed.
1022 void regmap_field_free(struct regmap_field *field)
1026 EXPORT_SYMBOL_GPL(regmap_field_free);
1029 * regmap_reinit_cache(): Reinitialise the current register cache
1031 * @map: Register map to operate on.
1032 * @config: New configuration. Only the cache data will be used.
1034 * Discard any existing register cache for the map and initialize a
1035 * new cache. This can be used to restore the cache to defaults or to
1036 * update the cache configuration to reflect runtime discovery of the
1039 * No explicit locking is done here, the user needs to ensure that
1040 * this function will not race with other calls to regmap.
1042 int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
1045 regmap_debugfs_exit(map);
1047 map->max_register = config->max_register;
1048 map->writeable_reg = config->writeable_reg;
1049 map->readable_reg = config->readable_reg;
1050 map->volatile_reg = config->volatile_reg;
1051 map->precious_reg = config->precious_reg;
1052 map->cache_type = config->cache_type;
1054 regmap_debugfs_init(map, config->name);
1056 map->cache_bypass = false;
1057 map->cache_only = false;
1059 return regcache_init(map, config);
1061 EXPORT_SYMBOL_GPL(regmap_reinit_cache);
1064 * regmap_exit(): Free a previously allocated register map
1066 void regmap_exit(struct regmap *map)
1068 struct regmap_async *async;
1071 regmap_debugfs_exit(map);
1072 regmap_range_exit(map);
1073 if (map->bus && map->bus->free_context)
1074 map->bus->free_context(map->bus_context);
1075 kfree(map->work_buf);
1076 while (!list_empty(&map->async_free)) {
1077 async = list_first_entry_or_null(&map->async_free,
1078 struct regmap_async,
1080 list_del(&async->list);
1081 kfree(async->work_buf);
1086 EXPORT_SYMBOL_GPL(regmap_exit);
1088 static int dev_get_regmap_match(struct device *dev, void *res, void *data)
1090 struct regmap **r = res;
1096 /* If the user didn't specify a name match any */
1098 return (*r)->name == data;
1104 * dev_get_regmap(): Obtain the regmap (if any) for a device
1106 * @dev: Device to retrieve the map for
1107 * @name: Optional name for the register map, usually NULL.
1109 * Returns the regmap for the device if one is present, or NULL. If
1110 * name is specified then it must match the name specified when
1111 * registering the device, if it is NULL then the first regmap found
1112 * will be used. Devices with multiple register maps are very rare,
1113 * generic code should normally not need to specify a name.
1115 struct regmap *dev_get_regmap(struct device *dev, const char *name)
1117 struct regmap **r = devres_find(dev, dev_get_regmap_release,
1118 dev_get_regmap_match, (void *)name);
1124 EXPORT_SYMBOL_GPL(dev_get_regmap);
1127 * regmap_get_device(): Obtain the device from a regmap
1129 * @map: Register map to operate on.
1131 * Returns the underlying device that the regmap has been created for.
1133 struct device *regmap_get_device(struct regmap *map)
1137 EXPORT_SYMBOL_GPL(regmap_get_device);
1139 static int _regmap_select_page(struct regmap *map, unsigned int *reg,
1140 struct regmap_range_node *range,
1141 unsigned int val_num)
1143 void *orig_work_buf;
1144 unsigned int win_offset;
1145 unsigned int win_page;
1149 win_offset = (*reg - range->range_min) % range->window_len;
1150 win_page = (*reg - range->range_min) / range->window_len;
1153 /* Bulk write shouldn't cross range boundary */
1154 if (*reg + val_num - 1 > range->range_max)
1157 /* ... or single page boundary */
1158 if (val_num > range->window_len - win_offset)
1162 /* It is possible to have selector register inside data window.
1163 In that case, selector register is located on every page and
1164 it needs no page switching, when accessed alone. */
1166 range->window_start + win_offset != range->selector_reg) {
1167 /* Use separate work_buf during page switching */
1168 orig_work_buf = map->work_buf;
1169 map->work_buf = map->selector_work_buf;
1171 ret = _regmap_update_bits(map, range->selector_reg,
1172 range->selector_mask,
1173 win_page << range->selector_shift,
1176 map->work_buf = orig_work_buf;
1182 *reg = range->window_start + win_offset;
1187 int _regmap_raw_write(struct regmap *map, unsigned int reg,
1188 const void *val, size_t val_len)
1190 struct regmap_range_node *range;
1191 unsigned long flags;
1192 u8 *u8 = map->work_buf;
1193 void *work_val = map->work_buf + map->format.reg_bytes +
1194 map->format.pad_bytes;
1196 int ret = -ENOTSUPP;
1202 /* Check for unwritable registers before we start */
1203 if (map->writeable_reg)
1204 for (i = 0; i < val_len / map->format.val_bytes; i++)
1205 if (!map->writeable_reg(map->dev,
1206 reg + (i * map->reg_stride)))
1209 if (!map->cache_bypass && map->format.parse_val) {
1211 int val_bytes = map->format.val_bytes;
1212 for (i = 0; i < val_len / val_bytes; i++) {
1213 ival = map->format.parse_val(val + (i * val_bytes));
1214 ret = regcache_write(map, reg + (i * map->reg_stride),
1218 "Error in caching of register: %x ret: %d\n",
1223 if (map->cache_only) {
1224 map->cache_dirty = true;
1229 range = _regmap_range_lookup(map, reg);
1231 int val_num = val_len / map->format.val_bytes;
1232 int win_offset = (reg - range->range_min) % range->window_len;
1233 int win_residue = range->window_len - win_offset;
1235 /* If the write goes beyond the end of the window split it */
1236 while (val_num > win_residue) {
1237 dev_dbg(map->dev, "Writing window %d/%zu\n",
1238 win_residue, val_len / map->format.val_bytes);
1239 ret = _regmap_raw_write(map, reg, val, win_residue *
1240 map->format.val_bytes);
1245 val_num -= win_residue;
1246 val += win_residue * map->format.val_bytes;
1247 val_len -= win_residue * map->format.val_bytes;
1249 win_offset = (reg - range->range_min) %
1251 win_residue = range->window_len - win_offset;
1254 ret = _regmap_select_page(map, ®, range, val_num);
1259 map->format.format_reg(map->work_buf, reg, map->reg_shift);
1261 u8[0] |= map->write_flag_mask;
1264 * Essentially all I/O mechanisms will be faster with a single
1265 * buffer to write. Since register syncs often generate raw
1266 * writes of single registers optimise that case.
1268 if (val != work_val && val_len == map->format.val_bytes) {
1269 memcpy(work_val, val, map->format.val_bytes);
1273 if (map->async && map->bus->async_write) {
1274 struct regmap_async *async;
1276 trace_regmap_async_write_start(map, reg, val_len);
1278 spin_lock_irqsave(&map->async_lock, flags);
1279 async = list_first_entry_or_null(&map->async_free,
1280 struct regmap_async,
1283 list_del(&async->list);
1284 spin_unlock_irqrestore(&map->async_lock, flags);
1287 async = map->bus->async_alloc();
1291 async->work_buf = kzalloc(map->format.buf_size,
1292 GFP_KERNEL | GFP_DMA);
1293 if (!async->work_buf) {
1301 /* If the caller supplied the value we can use it safely. */
1302 memcpy(async->work_buf, map->work_buf, map->format.pad_bytes +
1303 map->format.reg_bytes + map->format.val_bytes);
1305 spin_lock_irqsave(&map->async_lock, flags);
1306 list_add_tail(&async->list, &map->async_list);
1307 spin_unlock_irqrestore(&map->async_lock, flags);
1309 if (val != work_val)
1310 ret = map->bus->async_write(map->bus_context,
1312 map->format.reg_bytes +
1313 map->format.pad_bytes,
1314 val, val_len, async);
1316 ret = map->bus->async_write(map->bus_context,
1318 map->format.reg_bytes +
1319 map->format.pad_bytes +
1320 val_len, NULL, 0, async);
1323 dev_err(map->dev, "Failed to schedule write: %d\n",
1326 spin_lock_irqsave(&map->async_lock, flags);
1327 list_move(&async->list, &map->async_free);
1328 spin_unlock_irqrestore(&map->async_lock, flags);
1334 trace_regmap_hw_write_start(map, reg, val_len / map->format.val_bytes);
1336 /* If we're doing a single register write we can probably just
1337 * send the work_buf directly, otherwise try to do a gather
1340 if (val == work_val)
1341 ret = map->bus->write(map->bus_context, map->work_buf,
1342 map->format.reg_bytes +
1343 map->format.pad_bytes +
1345 else if (map->bus->gather_write)
1346 ret = map->bus->gather_write(map->bus_context, map->work_buf,
1347 map->format.reg_bytes +
1348 map->format.pad_bytes,
1351 /* If that didn't work fall back on linearising by hand. */
1352 if (ret == -ENOTSUPP) {
1353 len = map->format.reg_bytes + map->format.pad_bytes + val_len;
1354 buf = kzalloc(len, GFP_KERNEL);
1358 memcpy(buf, map->work_buf, map->format.reg_bytes);
1359 memcpy(buf + map->format.reg_bytes + map->format.pad_bytes,
1361 ret = map->bus->write(map->bus_context, buf, len);
1366 trace_regmap_hw_write_done(map, reg, val_len / map->format.val_bytes);
1372 * regmap_can_raw_write - Test if regmap_raw_write() is supported
1374 * @map: Map to check.
1376 bool regmap_can_raw_write(struct regmap *map)
1378 return map->bus && map->bus->write && map->format.format_val &&
1379 map->format.format_reg;
1381 EXPORT_SYMBOL_GPL(regmap_can_raw_write);
1384 * regmap_get_raw_read_max - Get the maximum size we can read
1386 * @map: Map to check.
1388 size_t regmap_get_raw_read_max(struct regmap *map)
1390 return map->max_raw_read;
1392 EXPORT_SYMBOL_GPL(regmap_get_raw_read_max);
1395 * regmap_get_raw_write_max - Get the maximum size we can read
1397 * @map: Map to check.
1399 size_t regmap_get_raw_write_max(struct regmap *map)
1401 return map->max_raw_write;
1403 EXPORT_SYMBOL_GPL(regmap_get_raw_write_max);
1405 static int _regmap_bus_formatted_write(void *context, unsigned int reg,
1409 struct regmap_range_node *range;
1410 struct regmap *map = context;
1412 WARN_ON(!map->bus || !map->format.format_write);
1414 range = _regmap_range_lookup(map, reg);
1416 ret = _regmap_select_page(map, ®, range, 1);
1421 map->format.format_write(map, reg, val);
1423 trace_regmap_hw_write_start(map, reg, 1);
1425 ret = map->bus->write(map->bus_context, map->work_buf,
1426 map->format.buf_size);
1428 trace_regmap_hw_write_done(map, reg, 1);
1433 static int _regmap_bus_reg_write(void *context, unsigned int reg,
1436 struct regmap *map = context;
1438 return map->bus->reg_write(map->bus_context, reg, val);
1441 static int _regmap_bus_raw_write(void *context, unsigned int reg,
1444 struct regmap *map = context;
1446 WARN_ON(!map->bus || !map->format.format_val);
1448 map->format.format_val(map->work_buf + map->format.reg_bytes
1449 + map->format.pad_bytes, val, 0);
1450 return _regmap_raw_write(map, reg,
1452 map->format.reg_bytes +
1453 map->format.pad_bytes,
1454 map->format.val_bytes);
1457 static inline void *_regmap_map_get_context(struct regmap *map)
1459 return (map->bus) ? map : map->bus_context;
1462 int _regmap_write(struct regmap *map, unsigned int reg,
1466 void *context = _regmap_map_get_context(map);
1468 if (!regmap_writeable(map, reg))
1471 if (!map->cache_bypass && !map->defer_caching) {
1472 ret = regcache_write(map, reg, val);
1475 if (map->cache_only) {
1476 map->cache_dirty = true;
1482 if (map->dev && strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
1483 dev_info(map->dev, "%x <= %x\n", reg, val);
1486 trace_regmap_reg_write(map, reg, val);
1488 return map->reg_write(context, reg, val);
1492 * regmap_write(): Write a value to a single register
1494 * @map: Register map to write to
1495 * @reg: Register to write to
1496 * @val: Value to be written
1498 * A value of zero will be returned on success, a negative errno will
1499 * be returned in error cases.
1501 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
1505 if (reg % map->reg_stride)
1508 map->lock(map->lock_arg);
1510 ret = _regmap_write(map, reg, val);
1512 map->unlock(map->lock_arg);
1516 EXPORT_SYMBOL_GPL(regmap_write);
1519 * regmap_write_async(): Write a value to a single register asynchronously
1521 * @map: Register map to write to
1522 * @reg: Register to write to
1523 * @val: Value to be written
1525 * A value of zero will be returned on success, a negative errno will
1526 * be returned in error cases.
1528 int regmap_write_async(struct regmap *map, unsigned int reg, unsigned int val)
1532 if (reg % map->reg_stride)
1535 map->lock(map->lock_arg);
1539 ret = _regmap_write(map, reg, val);
1543 map->unlock(map->lock_arg);
1547 EXPORT_SYMBOL_GPL(regmap_write_async);
1550 * regmap_raw_write(): Write raw values to one or more registers
1552 * @map: Register map to write to
1553 * @reg: Initial register to write to
1554 * @val: Block of data to be written, laid out for direct transmission to the
1556 * @val_len: Length of data pointed to by val.
1558 * This function is intended to be used for things like firmware
1559 * download where a large block of data needs to be transferred to the
1560 * device. No formatting will be done on the data provided.
1562 * A value of zero will be returned on success, a negative errno will
1563 * be returned in error cases.
1565 int regmap_raw_write(struct regmap *map, unsigned int reg,
1566 const void *val, size_t val_len)
1570 if (!regmap_can_raw_write(map))
1572 if (val_len % map->format.val_bytes)
1574 if (map->max_raw_write && map->max_raw_write > val_len)
1577 map->lock(map->lock_arg);
1579 ret = _regmap_raw_write(map, reg, val, val_len);
1581 map->unlock(map->lock_arg);
1585 EXPORT_SYMBOL_GPL(regmap_raw_write);
1588 * regmap_field_write(): Write a value to a single register field
1590 * @field: Register field to write to
1591 * @val: Value to be written
1593 * A value of zero will be returned on success, a negative errno will
1594 * be returned in error cases.
1596 int regmap_field_write(struct regmap_field *field, unsigned int val)
1598 return regmap_update_bits(field->regmap, field->reg,
1599 field->mask, val << field->shift);
1601 EXPORT_SYMBOL_GPL(regmap_field_write);
1604 * regmap_field_update_bits(): Perform a read/modify/write cycle
1605 * on the register field
1607 * @field: Register field to write to
1608 * @mask: Bitmask to change
1609 * @val: Value to be written
1611 * A value of zero will be returned on success, a negative errno will
1612 * be returned in error cases.
1614 int regmap_field_update_bits(struct regmap_field *field, unsigned int mask, unsigned int val)
1616 mask = (mask << field->shift) & field->mask;
1618 return regmap_update_bits(field->regmap, field->reg,
1619 mask, val << field->shift);
1621 EXPORT_SYMBOL_GPL(regmap_field_update_bits);
1624 * regmap_fields_write(): Write a value to a single register field with port ID
1626 * @field: Register field to write to
1628 * @val: Value to be written
1630 * A value of zero will be returned on success, a negative errno will
1631 * be returned in error cases.
1633 int regmap_fields_write(struct regmap_field *field, unsigned int id,
1636 if (id >= field->id_size)
1639 return regmap_update_bits(field->regmap,
1640 field->reg + (field->id_offset * id),
1641 field->mask, val << field->shift);
1643 EXPORT_SYMBOL_GPL(regmap_fields_write);
1645 int regmap_fields_force_write(struct regmap_field *field, unsigned int id,
1648 if (id >= field->id_size)
1651 return regmap_write_bits(field->regmap,
1652 field->reg + (field->id_offset * id),
1653 field->mask, val << field->shift);
1655 EXPORT_SYMBOL_GPL(regmap_fields_force_write);
1658 * regmap_fields_update_bits(): Perform a read/modify/write cycle
1659 * on the register field
1661 * @field: Register field to write to
1663 * @mask: Bitmask to change
1664 * @val: Value to be written
1666 * A value of zero will be returned on success, a negative errno will
1667 * be returned in error cases.
1669 int regmap_fields_update_bits(struct regmap_field *field, unsigned int id,
1670 unsigned int mask, unsigned int val)
1672 if (id >= field->id_size)
1675 mask = (mask << field->shift) & field->mask;
1677 return regmap_update_bits(field->regmap,
1678 field->reg + (field->id_offset * id),
1679 mask, val << field->shift);
1681 EXPORT_SYMBOL_GPL(regmap_fields_update_bits);
1684 * regmap_bulk_write(): Write multiple registers to the device
1686 * @map: Register map to write to
1687 * @reg: First register to be write from
1688 * @val: Block of data to be written, in native register size for device
1689 * @val_count: Number of registers to write
1691 * This function is intended to be used for writing a large block of
1692 * data to the device either in single transfer or multiple transfer.
1694 * A value of zero will be returned on success, a negative errno will
1695 * be returned in error cases.
1697 int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
1701 size_t val_bytes = map->format.val_bytes;
1702 size_t total_size = val_bytes * val_count;
1704 if (map->bus && !map->format.parse_inplace)
1706 if (reg % map->reg_stride)
1710 * Some devices don't support bulk write, for
1711 * them we have a series of single write operations in the first two if
1714 * The first if block is used for memory mapped io. It does not allow
1715 * val_bytes of 3 for example.
1716 * The second one is used for busses which do not have this limitation
1717 * and can write arbitrary value lengths.
1720 map->lock(map->lock_arg);
1721 for (i = 0; i < val_count; i++) {
1724 switch (val_bytes) {
1726 ival = *(u8 *)(val + (i * val_bytes));
1729 ival = *(u16 *)(val + (i * val_bytes));
1732 ival = *(u32 *)(val + (i * val_bytes));
1736 ival = *(u64 *)(val + (i * val_bytes));
1744 ret = _regmap_write(map, reg + (i * map->reg_stride),
1750 map->unlock(map->lock_arg);
1751 } else if (map->use_single_write ||
1752 (map->max_raw_write && map->max_raw_write < total_size)) {
1753 int chunk_stride = map->reg_stride;
1754 size_t chunk_size = val_bytes;
1755 size_t chunk_count = val_count;
1757 if (!map->use_single_write) {
1758 chunk_size = map->max_raw_write;
1759 if (chunk_size % val_bytes)
1760 chunk_size -= chunk_size % val_bytes;
1761 chunk_count = total_size / chunk_size;
1762 chunk_stride *= chunk_size / val_bytes;
1765 map->lock(map->lock_arg);
1766 /* Write as many bytes as possible with chunk_size */
1767 for (i = 0; i < chunk_count; i++) {
1768 ret = _regmap_raw_write(map,
1769 reg + (i * chunk_stride),
1770 val + (i * chunk_size),
1776 /* Write remaining bytes */
1777 if (!ret && chunk_size * i < total_size) {
1778 ret = _regmap_raw_write(map, reg + (i * chunk_stride),
1779 val + (i * chunk_size),
1780 total_size - i * chunk_size);
1782 map->unlock(map->lock_arg);
1789 wval = kmemdup(val, val_count * val_bytes, GFP_KERNEL);
1791 dev_err(map->dev, "Error in memory allocation\n");
1794 for (i = 0; i < val_count * val_bytes; i += val_bytes)
1795 map->format.parse_inplace(wval + i);
1797 map->lock(map->lock_arg);
1798 ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count);
1799 map->unlock(map->lock_arg);
1805 EXPORT_SYMBOL_GPL(regmap_bulk_write);
1808 * _regmap_raw_multi_reg_write()
1810 * the (register,newvalue) pairs in regs have not been formatted, but
1811 * they are all in the same page and have been changed to being page
1812 * relative. The page register has been written if that was necessary.
1814 static int _regmap_raw_multi_reg_write(struct regmap *map,
1815 const struct reg_sequence *regs,
1822 size_t val_bytes = map->format.val_bytes;
1823 size_t reg_bytes = map->format.reg_bytes;
1824 size_t pad_bytes = map->format.pad_bytes;
1825 size_t pair_size = reg_bytes + pad_bytes + val_bytes;
1826 size_t len = pair_size * num_regs;
1831 buf = kzalloc(len, GFP_KERNEL);
1835 /* We have to linearise by hand. */
1839 for (i = 0; i < num_regs; i++) {
1840 unsigned int reg = regs[i].reg;
1841 unsigned int val = regs[i].def;
1842 trace_regmap_hw_write_start(map, reg, 1);
1843 map->format.format_reg(u8, reg, map->reg_shift);
1844 u8 += reg_bytes + pad_bytes;
1845 map->format.format_val(u8, val, 0);
1849 *u8 |= map->write_flag_mask;
1851 ret = map->bus->write(map->bus_context, buf, len);
1855 for (i = 0; i < num_regs; i++) {
1856 int reg = regs[i].reg;
1857 trace_regmap_hw_write_done(map, reg, 1);
1862 static unsigned int _regmap_register_page(struct regmap *map,
1864 struct regmap_range_node *range)
1866 unsigned int win_page = (reg - range->range_min) / range->window_len;
1871 static int _regmap_range_multi_paged_reg_write(struct regmap *map,
1872 struct reg_sequence *regs,
1877 struct reg_sequence *base;
1878 unsigned int this_page = 0;
1879 unsigned int page_change = 0;
1881 * the set of registers are not neccessarily in order, but
1882 * since the order of write must be preserved this algorithm
1883 * chops the set each time the page changes. This also applies
1884 * if there is a delay required at any point in the sequence.
1887 for (i = 0, n = 0; i < num_regs; i++, n++) {
1888 unsigned int reg = regs[i].reg;
1889 struct regmap_range_node *range;
1891 range = _regmap_range_lookup(map, reg);
1893 unsigned int win_page = _regmap_register_page(map, reg,
1897 this_page = win_page;
1898 if (win_page != this_page) {
1899 this_page = win_page;
1904 /* If we have both a page change and a delay make sure to
1905 * write the regs and apply the delay before we change the
1909 if (page_change || regs[i].delay_us) {
1911 /* For situations where the first write requires
1912 * a delay we need to make sure we don't call
1913 * raw_multi_reg_write with n=0
1914 * This can't occur with page breaks as we
1915 * never write on the first iteration
1917 if (regs[i].delay_us && i == 0)
1920 ret = _regmap_raw_multi_reg_write(map, base, n);
1924 if (regs[i].delay_us)
1925 udelay(regs[i].delay_us);
1931 ret = _regmap_select_page(map,
1944 return _regmap_raw_multi_reg_write(map, base, n);
1948 static int _regmap_multi_reg_write(struct regmap *map,
1949 const struct reg_sequence *regs,
1955 if (!map->can_multi_write) {
1956 for (i = 0; i < num_regs; i++) {
1957 ret = _regmap_write(map, regs[i].reg, regs[i].def);
1961 if (regs[i].delay_us)
1962 udelay(regs[i].delay_us);
1967 if (!map->format.parse_inplace)
1970 if (map->writeable_reg)
1971 for (i = 0; i < num_regs; i++) {
1972 int reg = regs[i].reg;
1973 if (!map->writeable_reg(map->dev, reg))
1975 if (reg % map->reg_stride)
1979 if (!map->cache_bypass) {
1980 for (i = 0; i < num_regs; i++) {
1981 unsigned int val = regs[i].def;
1982 unsigned int reg = regs[i].reg;
1983 ret = regcache_write(map, reg, val);
1986 "Error in caching of register: %x ret: %d\n",
1991 if (map->cache_only) {
1992 map->cache_dirty = true;
1999 for (i = 0; i < num_regs; i++) {
2000 unsigned int reg = regs[i].reg;
2001 struct regmap_range_node *range;
2003 /* Coalesce all the writes between a page break or a delay
2006 range = _regmap_range_lookup(map, reg);
2007 if (range || regs[i].delay_us) {
2008 size_t len = sizeof(struct reg_sequence)*num_regs;
2009 struct reg_sequence *base = kmemdup(regs, len,
2013 ret = _regmap_range_multi_paged_reg_write(map, base,
2020 return _regmap_raw_multi_reg_write(map, regs, num_regs);
2024 * regmap_multi_reg_write(): Write multiple registers to the device
2026 * where the set of register,value pairs are supplied in any order,
2027 * possibly not all in a single range.
2029 * @map: Register map to write to
2030 * @regs: Array of structures containing register,value to be written
2031 * @num_regs: Number of registers to write
2033 * The 'normal' block write mode will send ultimately send data on the
2034 * target bus as R,V1,V2,V3,..,Vn where successively higer registers are
2035 * addressed. However, this alternative block multi write mode will send
2036 * the data as R1,V1,R2,V2,..,Rn,Vn on the target bus. The target device
2037 * must of course support the mode.
2039 * A value of zero will be returned on success, a negative errno will be
2040 * returned in error cases.
2042 int regmap_multi_reg_write(struct regmap *map, const struct reg_sequence *regs,
2047 map->lock(map->lock_arg);
2049 ret = _regmap_multi_reg_write(map, regs, num_regs);
2051 map->unlock(map->lock_arg);
2055 EXPORT_SYMBOL_GPL(regmap_multi_reg_write);
2058 * regmap_multi_reg_write_bypassed(): Write multiple registers to the
2059 * device but not the cache
2061 * where the set of register are supplied in any order
2063 * @map: Register map to write to
2064 * @regs: Array of structures containing register,value to be written
2065 * @num_regs: Number of registers to write
2067 * This function is intended to be used for writing a large block of data
2068 * atomically to the device in single transfer for those I2C client devices
2069 * that implement this alternative block write mode.
2071 * A value of zero will be returned on success, a negative errno will
2072 * be returned in error cases.
2074 int regmap_multi_reg_write_bypassed(struct regmap *map,
2075 const struct reg_sequence *regs,
2081 map->lock(map->lock_arg);
2083 bypass = map->cache_bypass;
2084 map->cache_bypass = true;
2086 ret = _regmap_multi_reg_write(map, regs, num_regs);
2088 map->cache_bypass = bypass;
2090 map->unlock(map->lock_arg);
2094 EXPORT_SYMBOL_GPL(regmap_multi_reg_write_bypassed);
2097 * regmap_raw_write_async(): Write raw values to one or more registers
2100 * @map: Register map to write to
2101 * @reg: Initial register to write to
2102 * @val: Block of data to be written, laid out for direct transmission to the
2103 * device. Must be valid until regmap_async_complete() is called.
2104 * @val_len: Length of data pointed to by val.
2106 * This function is intended to be used for things like firmware
2107 * download where a large block of data needs to be transferred to the
2108 * device. No formatting will be done on the data provided.
2110 * If supported by the underlying bus the write will be scheduled
2111 * asynchronously, helping maximise I/O speed on higher speed buses
2112 * like SPI. regmap_async_complete() can be called to ensure that all
2113 * asynchrnous writes have been completed.
2115 * A value of zero will be returned on success, a negative errno will
2116 * be returned in error cases.
2118 int regmap_raw_write_async(struct regmap *map, unsigned int reg,
2119 const void *val, size_t val_len)
2123 if (val_len % map->format.val_bytes)
2125 if (reg % map->reg_stride)
2128 map->lock(map->lock_arg);
2132 ret = _regmap_raw_write(map, reg, val, val_len);
2136 map->unlock(map->lock_arg);
2140 EXPORT_SYMBOL_GPL(regmap_raw_write_async);
2142 static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
2143 unsigned int val_len)
2145 struct regmap_range_node *range;
2146 u8 *u8 = map->work_buf;
2151 range = _regmap_range_lookup(map, reg);
2153 ret = _regmap_select_page(map, ®, range,
2154 val_len / map->format.val_bytes);
2159 map->format.format_reg(map->work_buf, reg, map->reg_shift);
2162 * Some buses or devices flag reads by setting the high bits in the
2163 * register address; since it's always the high bits for all
2164 * current formats we can do this here rather than in
2165 * formatting. This may break if we get interesting formats.
2167 u8[0] |= map->read_flag_mask;
2169 trace_regmap_hw_read_start(map, reg, val_len / map->format.val_bytes);
2171 ret = map->bus->read(map->bus_context, map->work_buf,
2172 map->format.reg_bytes + map->format.pad_bytes,
2175 trace_regmap_hw_read_done(map, reg, val_len / map->format.val_bytes);
2180 static int _regmap_bus_reg_read(void *context, unsigned int reg,
2183 struct regmap *map = context;
2185 return map->bus->reg_read(map->bus_context, reg, val);
2188 static int _regmap_bus_read(void *context, unsigned int reg,
2192 struct regmap *map = context;
2194 if (!map->format.parse_val)
2197 ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
2199 *val = map->format.parse_val(map->work_buf);
2204 static int _regmap_read(struct regmap *map, unsigned int reg,
2208 void *context = _regmap_map_get_context(map);
2210 if (!map->cache_bypass) {
2211 ret = regcache_read(map, reg, val);
2216 if (map->cache_only)
2219 if (!regmap_readable(map, reg))
2222 ret = map->reg_read(context, reg, val);
2225 if (map->dev && strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
2226 dev_info(map->dev, "%x => %x\n", reg, *val);
2229 trace_regmap_reg_read(map, reg, *val);
2231 if (!map->cache_bypass)
2232 regcache_write(map, reg, *val);
2239 * regmap_read(): Read a value from a single register
2241 * @map: Register map to read from
2242 * @reg: Register to be read from
2243 * @val: Pointer to store read value
2245 * A value of zero will be returned on success, a negative errno will
2246 * be returned in error cases.
2248 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
2252 if (reg % map->reg_stride)
2255 map->lock(map->lock_arg);
2257 ret = _regmap_read(map, reg, val);
2259 map->unlock(map->lock_arg);
2263 EXPORT_SYMBOL_GPL(regmap_read);
2266 * regmap_raw_read(): Read raw data from the device
2268 * @map: Register map to read from
2269 * @reg: First register to be read from
2270 * @val: Pointer to store read value
2271 * @val_len: Size of data to read
2273 * A value of zero will be returned on success, a negative errno will
2274 * be returned in error cases.
2276 int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
2279 size_t val_bytes = map->format.val_bytes;
2280 size_t val_count = val_len / val_bytes;
2286 if (val_len % map->format.val_bytes)
2288 if (reg % map->reg_stride)
2293 map->lock(map->lock_arg);
2295 if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
2296 map->cache_type == REGCACHE_NONE) {
2297 if (!map->bus->read) {
2301 if (map->max_raw_read && map->max_raw_read < val_len) {
2306 /* Physical block read if there's no cache involved */
2307 ret = _regmap_raw_read(map, reg, val, val_len);
2310 /* Otherwise go word by word for the cache; should be low
2311 * cost as we expect to hit the cache.
2313 for (i = 0; i < val_count; i++) {
2314 ret = _regmap_read(map, reg + (i * map->reg_stride),
2319 map->format.format_val(val + (i * val_bytes), v, 0);
2324 map->unlock(map->lock_arg);
2328 EXPORT_SYMBOL_GPL(regmap_raw_read);
2331 * regmap_field_read(): Read a value to a single register field
2333 * @field: Register field to read from
2334 * @val: Pointer to store read value
2336 * A value of zero will be returned on success, a negative errno will
2337 * be returned in error cases.
2339 int regmap_field_read(struct regmap_field *field, unsigned int *val)
2342 unsigned int reg_val;
2343 ret = regmap_read(field->regmap, field->reg, ®_val);
2347 reg_val &= field->mask;
2348 reg_val >>= field->shift;
2353 EXPORT_SYMBOL_GPL(regmap_field_read);
2356 * regmap_fields_read(): Read a value to a single register field with port ID
2358 * @field: Register field to read from
2360 * @val: Pointer to store read value
2362 * A value of zero will be returned on success, a negative errno will
2363 * be returned in error cases.
2365 int regmap_fields_read(struct regmap_field *field, unsigned int id,
2369 unsigned int reg_val;
2371 if (id >= field->id_size)
2374 ret = regmap_read(field->regmap,
2375 field->reg + (field->id_offset * id),
2380 reg_val &= field->mask;
2381 reg_val >>= field->shift;
2386 EXPORT_SYMBOL_GPL(regmap_fields_read);
2389 * regmap_bulk_read(): Read multiple registers from the device
2391 * @map: Register map to read from
2392 * @reg: First register to be read from
2393 * @val: Pointer to store read value, in native register size for device
2394 * @val_count: Number of registers to read
2396 * A value of zero will be returned on success, a negative errno will
2397 * be returned in error cases.
2399 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
2403 size_t val_bytes = map->format.val_bytes;
2404 bool vol = regmap_volatile_range(map, reg, val_count);
2406 if (reg % map->reg_stride)
2409 if (map->bus && map->format.parse_inplace && (vol || map->cache_type == REGCACHE_NONE)) {
2411 * Some devices does not support bulk read, for
2412 * them we have a series of single read operations.
2414 size_t total_size = val_bytes * val_count;
2416 if (!map->use_single_read &&
2417 (!map->max_raw_read || map->max_raw_read > total_size)) {
2418 ret = regmap_raw_read(map, reg, val,
2419 val_bytes * val_count);
2424 * Some devices do not support bulk read or do not
2425 * support large bulk reads, for them we have a series
2426 * of read operations.
2428 int chunk_stride = map->reg_stride;
2429 size_t chunk_size = val_bytes;
2430 size_t chunk_count = val_count;
2432 if (!map->use_single_read) {
2433 chunk_size = map->max_raw_read;
2434 if (chunk_size % val_bytes)
2435 chunk_size -= chunk_size % val_bytes;
2436 chunk_count = total_size / chunk_size;
2437 chunk_stride *= chunk_size / val_bytes;
2440 /* Read bytes that fit into a multiple of chunk_size */
2441 for (i = 0; i < chunk_count; i++) {
2442 ret = regmap_raw_read(map,
2443 reg + (i * chunk_stride),
2444 val + (i * chunk_size),
2450 /* Read remaining bytes */
2451 if (chunk_size * i < total_size) {
2452 ret = regmap_raw_read(map,
2453 reg + (i * chunk_stride),
2454 val + (i * chunk_size),
2455 total_size - i * chunk_size);
2461 for (i = 0; i < val_count * val_bytes; i += val_bytes)
2462 map->format.parse_inplace(val + i);
2464 for (i = 0; i < val_count; i++) {
2466 ret = regmap_read(map, reg + (i * map->reg_stride),
2471 if (map->format.format_val) {
2472 map->format.format_val(val + (i * val_bytes), ival, 0);
2474 /* Devices providing read and write
2475 * operations can use the bulk I/O
2476 * functions if they define a val_bytes,
2477 * we assume that the values are native
2484 switch (map->format.val_bytes) {
2503 EXPORT_SYMBOL_GPL(regmap_bulk_read);
2505 static int _regmap_update_bits(struct regmap *map, unsigned int reg,
2506 unsigned int mask, unsigned int val,
2507 bool *change, bool force_write)
2510 unsigned int tmp, orig;
2512 ret = _regmap_read(map, reg, &orig);
2519 if (force_write || (tmp != orig)) {
2520 ret = _regmap_write(map, reg, tmp);
2532 * regmap_update_bits: Perform a read/modify/write cycle on the register map
2534 * @map: Register map to update
2535 * @reg: Register to update
2536 * @mask: Bitmask to change
2537 * @val: New value for bitmask
2539 * Returns zero for success, a negative number on error.
2541 int regmap_update_bits(struct regmap *map, unsigned int reg,
2542 unsigned int mask, unsigned int val)
2546 map->lock(map->lock_arg);
2547 ret = _regmap_update_bits(map, reg, mask, val, NULL, false);
2548 map->unlock(map->lock_arg);
2552 EXPORT_SYMBOL_GPL(regmap_update_bits);
2555 * regmap_write_bits: Perform a read/modify/write cycle on the register map
2557 * @map: Register map to update
2558 * @reg: Register to update
2559 * @mask: Bitmask to change
2560 * @val: New value for bitmask
2562 * Returns zero for success, a negative number on error.
2564 int regmap_write_bits(struct regmap *map, unsigned int reg,
2565 unsigned int mask, unsigned int val)
2569 map->lock(map->lock_arg);
2570 ret = _regmap_update_bits(map, reg, mask, val, NULL, true);
2571 map->unlock(map->lock_arg);
2575 EXPORT_SYMBOL_GPL(regmap_write_bits);
2578 * regmap_update_bits_async: Perform a read/modify/write cycle on the register
2579 * map asynchronously
2581 * @map: Register map to update
2582 * @reg: Register to update
2583 * @mask: Bitmask to change
2584 * @val: New value for bitmask
2586 * With most buses the read must be done synchronously so this is most
2587 * useful for devices with a cache which do not need to interact with
2588 * the hardware to determine the current register value.
2590 * Returns zero for success, a negative number on error.
2592 int regmap_update_bits_async(struct regmap *map, unsigned int reg,
2593 unsigned int mask, unsigned int val)
2597 map->lock(map->lock_arg);
2601 ret = _regmap_update_bits(map, reg, mask, val, NULL, false);
2605 map->unlock(map->lock_arg);
2609 EXPORT_SYMBOL_GPL(regmap_update_bits_async);
2612 * regmap_update_bits_check: Perform a read/modify/write cycle on the
2613 * register map and report if updated
2615 * @map: Register map to update
2616 * @reg: Register to update
2617 * @mask: Bitmask to change
2618 * @val: New value for bitmask
2619 * @change: Boolean indicating if a write was done
2621 * Returns zero for success, a negative number on error.
2623 int regmap_update_bits_check(struct regmap *map, unsigned int reg,
2624 unsigned int mask, unsigned int val,
2629 map->lock(map->lock_arg);
2630 ret = _regmap_update_bits(map, reg, mask, val, change, false);
2631 map->unlock(map->lock_arg);
2634 EXPORT_SYMBOL_GPL(regmap_update_bits_check);
2637 * regmap_update_bits_check_async: Perform a read/modify/write cycle on the
2638 * register map asynchronously and report if
2641 * @map: Register map to update
2642 * @reg: Register to update
2643 * @mask: Bitmask to change
2644 * @val: New value for bitmask
2645 * @change: Boolean indicating if a write was done
2647 * With most buses the read must be done synchronously so this is most
2648 * useful for devices with a cache which do not need to interact with
2649 * the hardware to determine the current register value.
2651 * Returns zero for success, a negative number on error.
2653 int regmap_update_bits_check_async(struct regmap *map, unsigned int reg,
2654 unsigned int mask, unsigned int val,
2659 map->lock(map->lock_arg);
2663 ret = _regmap_update_bits(map, reg, mask, val, change, false);
2667 map->unlock(map->lock_arg);
2671 EXPORT_SYMBOL_GPL(regmap_update_bits_check_async);
2673 void regmap_async_complete_cb(struct regmap_async *async, int ret)
2675 struct regmap *map = async->map;
2678 trace_regmap_async_io_complete(map);
2680 spin_lock(&map->async_lock);
2681 list_move(&async->list, &map->async_free);
2682 wake = list_empty(&map->async_list);
2685 map->async_ret = ret;
2687 spin_unlock(&map->async_lock);
2690 wake_up(&map->async_waitq);
2692 EXPORT_SYMBOL_GPL(regmap_async_complete_cb);
2694 static int regmap_async_is_done(struct regmap *map)
2696 unsigned long flags;
2699 spin_lock_irqsave(&map->async_lock, flags);
2700 ret = list_empty(&map->async_list);
2701 spin_unlock_irqrestore(&map->async_lock, flags);
2707 * regmap_async_complete: Ensure all asynchronous I/O has completed.
2709 * @map: Map to operate on.
2711 * Blocks until any pending asynchronous I/O has completed. Returns
2712 * an error code for any failed I/O operations.
2714 int regmap_async_complete(struct regmap *map)
2716 unsigned long flags;
2719 /* Nothing to do with no async support */
2720 if (!map->bus || !map->bus->async_write)
2723 trace_regmap_async_complete_start(map);
2725 wait_event(map->async_waitq, regmap_async_is_done(map));
2727 spin_lock_irqsave(&map->async_lock, flags);
2728 ret = map->async_ret;
2730 spin_unlock_irqrestore(&map->async_lock, flags);
2732 trace_regmap_async_complete_done(map);
2736 EXPORT_SYMBOL_GPL(regmap_async_complete);
2739 * regmap_register_patch: Register and apply register updates to be applied
2740 * on device initialistion
2742 * @map: Register map to apply updates to.
2743 * @regs: Values to update.
2744 * @num_regs: Number of entries in regs.
2746 * Register a set of register updates to be applied to the device
2747 * whenever the device registers are synchronised with the cache and
2748 * apply them immediately. Typically this is used to apply
2749 * corrections to be applied to the device defaults on startup, such
2750 * as the updates some vendors provide to undocumented registers.
2752 * The caller must ensure that this function cannot be called
2753 * concurrently with either itself or regcache_sync().
2755 int regmap_register_patch(struct regmap *map, const struct reg_sequence *regs,
2758 struct reg_sequence *p;
2762 if (WARN_ONCE(num_regs <= 0, "invalid registers number (%d)\n",
2766 p = krealloc(map->patch,
2767 sizeof(struct reg_sequence) * (map->patch_regs + num_regs),
2770 memcpy(p + map->patch_regs, regs, num_regs * sizeof(*regs));
2772 map->patch_regs += num_regs;
2777 map->lock(map->lock_arg);
2779 bypass = map->cache_bypass;
2781 map->cache_bypass = true;
2784 ret = _regmap_multi_reg_write(map, regs, num_regs);
2787 map->cache_bypass = bypass;
2789 map->unlock(map->lock_arg);
2791 regmap_async_complete(map);
2795 EXPORT_SYMBOL_GPL(regmap_register_patch);
2798 * regmap_get_val_bytes(): Report the size of a register value
2800 * Report the size of a register value, mainly intended to for use by
2801 * generic infrastructure built on top of regmap.
2803 int regmap_get_val_bytes(struct regmap *map)
2805 if (map->format.format_write)
2808 return map->format.val_bytes;
2810 EXPORT_SYMBOL_GPL(regmap_get_val_bytes);
2813 * regmap_get_max_register(): Report the max register value
2815 * Report the max register value, mainly intended to for use by
2816 * generic infrastructure built on top of regmap.
2818 int regmap_get_max_register(struct regmap *map)
2820 return map->max_register ? map->max_register : -EINVAL;
2822 EXPORT_SYMBOL_GPL(regmap_get_max_register);
2825 * regmap_get_reg_stride(): Report the register address stride
2827 * Report the register address stride, mainly intended to for use by
2828 * generic infrastructure built on top of regmap.
2830 int regmap_get_reg_stride(struct regmap *map)
2832 return map->reg_stride;
2834 EXPORT_SYMBOL_GPL(regmap_get_reg_stride);
2836 int regmap_parse_val(struct regmap *map, const void *buf,
2839 if (!map->format.parse_val)
2842 *val = map->format.parse_val(buf);
2846 EXPORT_SYMBOL_GPL(regmap_parse_val);
2848 static int __init regmap_initcall(void)
2850 regmap_debugfs_initcall();
2854 postcore_initcall(regmap_initcall);