2 * Register map access API
4 * Copyright 2011 Wolfson Microelectronics plc
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/device.h>
14 #include <linux/slab.h>
15 #include <linux/export.h>
16 #include <linux/mutex.h>
17 #include <linux/err.h>
19 #include <linux/rbtree.h>
20 #include <linux/sched.h>
22 #define CREATE_TRACE_POINTS
28 * Sometimes for failures during very early init the trace
29 * infrastructure isn't available early enough to be used. For this
30 * sort of problem defining LOG_DEVICE will add printks for basic
31 * register I/O on a specific device.
35 static int _regmap_update_bits(struct regmap *map, unsigned int reg,
36 unsigned int mask, unsigned int val,
39 static int _regmap_bus_reg_read(void *context, unsigned int reg,
41 static int _regmap_bus_read(void *context, unsigned int reg,
43 static int _regmap_bus_formatted_write(void *context, unsigned int reg,
45 static int _regmap_bus_reg_write(void *context, unsigned int reg,
47 static int _regmap_bus_raw_write(void *context, unsigned int reg,
50 bool regmap_reg_in_ranges(unsigned int reg,
51 const struct regmap_range *ranges,
54 const struct regmap_range *r;
57 for (i = 0, r = ranges; i < nranges; i++, r++)
58 if (regmap_reg_in_range(reg, r))
62 EXPORT_SYMBOL_GPL(regmap_reg_in_ranges);
64 bool regmap_check_range_table(struct regmap *map, unsigned int reg,
65 const struct regmap_access_table *table)
67 /* Check "no ranges" first */
68 if (regmap_reg_in_ranges(reg, table->no_ranges, table->n_no_ranges))
71 /* In case zero "yes ranges" are supplied, any reg is OK */
72 if (!table->n_yes_ranges)
75 return regmap_reg_in_ranges(reg, table->yes_ranges,
78 EXPORT_SYMBOL_GPL(regmap_check_range_table);
80 bool regmap_writeable(struct regmap *map, unsigned int reg)
82 if (map->max_register && reg > map->max_register)
85 if (map->writeable_reg)
86 return map->writeable_reg(map->dev, reg);
89 return regmap_check_range_table(map, reg, map->wr_table);
94 bool regmap_readable(struct regmap *map, unsigned int reg)
99 if (map->max_register && reg > map->max_register)
102 if (map->format.format_write)
105 if (map->readable_reg)
106 return map->readable_reg(map->dev, reg);
109 return regmap_check_range_table(map, reg, map->rd_table);
114 bool regmap_volatile(struct regmap *map, unsigned int reg)
116 if (!map->format.format_write && !regmap_readable(map, reg))
119 if (map->volatile_reg)
120 return map->volatile_reg(map->dev, reg);
122 if (map->volatile_table)
123 return regmap_check_range_table(map, reg, map->volatile_table);
131 bool regmap_precious(struct regmap *map, unsigned int reg)
133 if (!regmap_readable(map, reg))
136 if (map->precious_reg)
137 return map->precious_reg(map->dev, reg);
139 if (map->precious_table)
140 return regmap_check_range_table(map, reg, map->precious_table);
145 static bool regmap_volatile_range(struct regmap *map, unsigned int reg,
150 for (i = 0; i < num; i++)
151 if (!regmap_volatile(map, reg + i))
157 static void regmap_format_2_6_write(struct regmap *map,
158 unsigned int reg, unsigned int val)
160 u8 *out = map->work_buf;
162 *out = (reg << 6) | val;
165 static void regmap_format_4_12_write(struct regmap *map,
166 unsigned int reg, unsigned int val)
168 __be16 *out = map->work_buf;
169 *out = cpu_to_be16((reg << 12) | val);
172 static void regmap_format_7_9_write(struct regmap *map,
173 unsigned int reg, unsigned int val)
175 __be16 *out = map->work_buf;
176 *out = cpu_to_be16((reg << 9) | val);
179 static void regmap_format_10_14_write(struct regmap *map,
180 unsigned int reg, unsigned int val)
182 u8 *out = map->work_buf;
185 out[1] = (val >> 8) | (reg << 6);
189 static void regmap_format_8(void *buf, unsigned int val, unsigned int shift)
196 static void regmap_format_16_be(void *buf, unsigned int val, unsigned int shift)
200 b[0] = cpu_to_be16(val << shift);
203 static void regmap_format_16_le(void *buf, unsigned int val, unsigned int shift)
207 b[0] = cpu_to_le16(val << shift);
210 static void regmap_format_16_native(void *buf, unsigned int val,
213 *(u16 *)buf = val << shift;
216 static void regmap_format_24(void *buf, unsigned int val, unsigned int shift)
227 static void regmap_format_32_be(void *buf, unsigned int val, unsigned int shift)
231 b[0] = cpu_to_be32(val << shift);
234 static void regmap_format_32_le(void *buf, unsigned int val, unsigned int shift)
238 b[0] = cpu_to_le32(val << shift);
241 static void regmap_format_32_native(void *buf, unsigned int val,
244 *(u32 *)buf = val << shift;
247 static void regmap_parse_inplace_noop(void *buf)
251 static unsigned int regmap_parse_8(const void *buf)
258 static unsigned int regmap_parse_16_be(const void *buf)
260 const __be16 *b = buf;
262 return be16_to_cpu(b[0]);
265 static unsigned int regmap_parse_16_le(const void *buf)
267 const __le16 *b = buf;
269 return le16_to_cpu(b[0]);
272 static void regmap_parse_16_be_inplace(void *buf)
276 b[0] = be16_to_cpu(b[0]);
279 static void regmap_parse_16_le_inplace(void *buf)
283 b[0] = le16_to_cpu(b[0]);
286 static unsigned int regmap_parse_16_native(const void *buf)
291 static unsigned int regmap_parse_24(const void *buf)
294 unsigned int ret = b[2];
295 ret |= ((unsigned int)b[1]) << 8;
296 ret |= ((unsigned int)b[0]) << 16;
301 static unsigned int regmap_parse_32_be(const void *buf)
303 const __be32 *b = buf;
305 return be32_to_cpu(b[0]);
308 static unsigned int regmap_parse_32_le(const void *buf)
310 const __le32 *b = buf;
312 return le32_to_cpu(b[0]);
315 static void regmap_parse_32_be_inplace(void *buf)
319 b[0] = be32_to_cpu(b[0]);
322 static void regmap_parse_32_le_inplace(void *buf)
326 b[0] = le32_to_cpu(b[0]);
329 static unsigned int regmap_parse_32_native(const void *buf)
334 static void regmap_lock_mutex(void *__map)
336 struct regmap *map = __map;
337 mutex_lock(&map->mutex);
340 static void regmap_unlock_mutex(void *__map)
342 struct regmap *map = __map;
343 mutex_unlock(&map->mutex);
346 static void regmap_lock_spinlock(void *__map)
347 __acquires(&map->spinlock)
349 struct regmap *map = __map;
352 spin_lock_irqsave(&map->spinlock, flags);
353 map->spinlock_flags = flags;
356 static void regmap_unlock_spinlock(void *__map)
357 __releases(&map->spinlock)
359 struct regmap *map = __map;
360 spin_unlock_irqrestore(&map->spinlock, map->spinlock_flags);
363 static void dev_get_regmap_release(struct device *dev, void *res)
366 * We don't actually have anything to do here; the goal here
367 * is not to manage the regmap but to provide a simple way to
368 * get the regmap back given a struct device.
372 static bool _regmap_range_add(struct regmap *map,
373 struct regmap_range_node *data)
375 struct rb_root *root = &map->range_tree;
376 struct rb_node **new = &(root->rb_node), *parent = NULL;
379 struct regmap_range_node *this =
380 container_of(*new, struct regmap_range_node, node);
383 if (data->range_max < this->range_min)
384 new = &((*new)->rb_left);
385 else if (data->range_min > this->range_max)
386 new = &((*new)->rb_right);
391 rb_link_node(&data->node, parent, new);
392 rb_insert_color(&data->node, root);
397 static struct regmap_range_node *_regmap_range_lookup(struct regmap *map,
400 struct rb_node *node = map->range_tree.rb_node;
403 struct regmap_range_node *this =
404 container_of(node, struct regmap_range_node, node);
406 if (reg < this->range_min)
407 node = node->rb_left;
408 else if (reg > this->range_max)
409 node = node->rb_right;
417 static void regmap_range_exit(struct regmap *map)
419 struct rb_node *next;
420 struct regmap_range_node *range_node;
422 next = rb_first(&map->range_tree);
424 range_node = rb_entry(next, struct regmap_range_node, node);
425 next = rb_next(&range_node->node);
426 rb_erase(&range_node->node, &map->range_tree);
430 kfree(map->selector_work_buf);
433 int regmap_attach_dev(struct device *dev, struct regmap *map,
434 const struct regmap_config *config)
440 regmap_debugfs_init(map, config->name);
442 /* Add a devres resource for dev_get_regmap() */
443 m = devres_alloc(dev_get_regmap_release, sizeof(*m), GFP_KERNEL);
445 regmap_debugfs_exit(map);
453 EXPORT_SYMBOL_GPL(regmap_attach_dev);
455 static enum regmap_endian regmap_get_reg_endian(const struct regmap_bus *bus,
456 const struct regmap_config *config)
458 enum regmap_endian endian;
460 /* Retrieve the endianness specification from the regmap config */
461 endian = config->reg_format_endian;
463 /* If the regmap config specified a non-default value, use that */
464 if (endian != REGMAP_ENDIAN_DEFAULT)
467 /* Retrieve the endianness specification from the bus config */
468 if (bus && bus->reg_format_endian_default)
469 endian = bus->reg_format_endian_default;
471 /* If the bus specified a non-default value, use that */
472 if (endian != REGMAP_ENDIAN_DEFAULT)
475 /* Use this if no other value was found */
476 return REGMAP_ENDIAN_BIG;
479 enum regmap_endian regmap_get_val_endian(struct device *dev,
480 const struct regmap_bus *bus,
481 const struct regmap_config *config)
483 struct device_node *np;
484 enum regmap_endian endian;
486 /* Retrieve the endianness specification from the regmap config */
487 endian = config->val_format_endian;
489 /* If the regmap config specified a non-default value, use that */
490 if (endian != REGMAP_ENDIAN_DEFAULT)
493 /* If the dev and dev->of_node exist try to get endianness from DT */
494 if (dev && dev->of_node) {
497 /* Parse the device's DT node for an endianness specification */
498 if (of_property_read_bool(np, "big-endian"))
499 endian = REGMAP_ENDIAN_BIG;
500 else if (of_property_read_bool(np, "little-endian"))
501 endian = REGMAP_ENDIAN_LITTLE;
503 /* If the endianness was specified in DT, use that */
504 if (endian != REGMAP_ENDIAN_DEFAULT)
508 /* Retrieve the endianness specification from the bus config */
509 if (bus && bus->val_format_endian_default)
510 endian = bus->val_format_endian_default;
512 /* If the bus specified a non-default value, use that */
513 if (endian != REGMAP_ENDIAN_DEFAULT)
516 /* Use this if no other value was found */
517 return REGMAP_ENDIAN_BIG;
519 EXPORT_SYMBOL_GPL(regmap_get_val_endian);
522 * regmap_init(): Initialise register map
524 * @dev: Device that will be interacted with
525 * @bus: Bus-specific callbacks to use with device
526 * @bus_context: Data passed to bus-specific callbacks
527 * @config: Configuration for register map
529 * The return value will be an ERR_PTR() on error or a valid pointer to
530 * a struct regmap. This function should generally not be called
531 * directly, it should be called by bus-specific init functions.
533 struct regmap *regmap_init(struct device *dev,
534 const struct regmap_bus *bus,
536 const struct regmap_config *config)
540 enum regmap_endian reg_endian, val_endian;
546 map = kzalloc(sizeof(*map), GFP_KERNEL);
552 if (config->lock && config->unlock) {
553 map->lock = config->lock;
554 map->unlock = config->unlock;
555 map->lock_arg = config->lock_arg;
557 if ((bus && bus->fast_io) ||
559 spin_lock_init(&map->spinlock);
560 map->lock = regmap_lock_spinlock;
561 map->unlock = regmap_unlock_spinlock;
563 mutex_init(&map->mutex);
564 map->lock = regmap_lock_mutex;
565 map->unlock = regmap_unlock_mutex;
569 map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8);
570 map->format.pad_bytes = config->pad_bits / 8;
571 map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8);
572 map->format.buf_size = DIV_ROUND_UP(config->reg_bits +
573 config->val_bits + config->pad_bits, 8);
574 map->reg_shift = config->pad_bits % 8;
575 if (config->reg_stride)
576 map->reg_stride = config->reg_stride;
579 map->use_single_read = config->use_single_rw || !bus || !bus->read;
580 map->use_single_write = config->use_single_rw || !bus || !bus->write;
581 map->can_multi_write = config->can_multi_write && bus && bus->write;
583 map->max_raw_read = bus->max_raw_read;
584 map->max_raw_write = bus->max_raw_write;
588 map->bus_context = bus_context;
589 map->max_register = config->max_register;
590 map->wr_table = config->wr_table;
591 map->rd_table = config->rd_table;
592 map->volatile_table = config->volatile_table;
593 map->precious_table = config->precious_table;
594 map->writeable_reg = config->writeable_reg;
595 map->readable_reg = config->readable_reg;
596 map->volatile_reg = config->volatile_reg;
597 map->precious_reg = config->precious_reg;
598 map->cache_type = config->cache_type;
599 map->name = config->name;
601 spin_lock_init(&map->async_lock);
602 INIT_LIST_HEAD(&map->async_list);
603 INIT_LIST_HEAD(&map->async_free);
604 init_waitqueue_head(&map->async_waitq);
606 if (config->read_flag_mask || config->write_flag_mask) {
607 map->read_flag_mask = config->read_flag_mask;
608 map->write_flag_mask = config->write_flag_mask;
610 map->read_flag_mask = bus->read_flag_mask;
614 map->reg_read = config->reg_read;
615 map->reg_write = config->reg_write;
617 map->defer_caching = false;
618 goto skip_format_initialization;
619 } else if (!bus->read || !bus->write) {
620 map->reg_read = _regmap_bus_reg_read;
621 map->reg_write = _regmap_bus_reg_write;
623 map->defer_caching = false;
624 goto skip_format_initialization;
626 map->reg_read = _regmap_bus_read;
629 reg_endian = regmap_get_reg_endian(bus, config);
630 val_endian = regmap_get_val_endian(dev, bus, config);
632 switch (config->reg_bits + map->reg_shift) {
634 switch (config->val_bits) {
636 map->format.format_write = regmap_format_2_6_write;
644 switch (config->val_bits) {
646 map->format.format_write = regmap_format_4_12_write;
654 switch (config->val_bits) {
656 map->format.format_write = regmap_format_7_9_write;
664 switch (config->val_bits) {
666 map->format.format_write = regmap_format_10_14_write;
674 map->format.format_reg = regmap_format_8;
678 switch (reg_endian) {
679 case REGMAP_ENDIAN_BIG:
680 map->format.format_reg = regmap_format_16_be;
682 case REGMAP_ENDIAN_NATIVE:
683 map->format.format_reg = regmap_format_16_native;
691 if (reg_endian != REGMAP_ENDIAN_BIG)
693 map->format.format_reg = regmap_format_24;
697 switch (reg_endian) {
698 case REGMAP_ENDIAN_BIG:
699 map->format.format_reg = regmap_format_32_be;
701 case REGMAP_ENDIAN_NATIVE:
702 map->format.format_reg = regmap_format_32_native;
713 if (val_endian == REGMAP_ENDIAN_NATIVE)
714 map->format.parse_inplace = regmap_parse_inplace_noop;
716 switch (config->val_bits) {
718 map->format.format_val = regmap_format_8;
719 map->format.parse_val = regmap_parse_8;
720 map->format.parse_inplace = regmap_parse_inplace_noop;
723 switch (val_endian) {
724 case REGMAP_ENDIAN_BIG:
725 map->format.format_val = regmap_format_16_be;
726 map->format.parse_val = regmap_parse_16_be;
727 map->format.parse_inplace = regmap_parse_16_be_inplace;
729 case REGMAP_ENDIAN_LITTLE:
730 map->format.format_val = regmap_format_16_le;
731 map->format.parse_val = regmap_parse_16_le;
732 map->format.parse_inplace = regmap_parse_16_le_inplace;
734 case REGMAP_ENDIAN_NATIVE:
735 map->format.format_val = regmap_format_16_native;
736 map->format.parse_val = regmap_parse_16_native;
743 if (val_endian != REGMAP_ENDIAN_BIG)
745 map->format.format_val = regmap_format_24;
746 map->format.parse_val = regmap_parse_24;
749 switch (val_endian) {
750 case REGMAP_ENDIAN_BIG:
751 map->format.format_val = regmap_format_32_be;
752 map->format.parse_val = regmap_parse_32_be;
753 map->format.parse_inplace = regmap_parse_32_be_inplace;
755 case REGMAP_ENDIAN_LITTLE:
756 map->format.format_val = regmap_format_32_le;
757 map->format.parse_val = regmap_parse_32_le;
758 map->format.parse_inplace = regmap_parse_32_le_inplace;
760 case REGMAP_ENDIAN_NATIVE:
761 map->format.format_val = regmap_format_32_native;
762 map->format.parse_val = regmap_parse_32_native;
770 if (map->format.format_write) {
771 if ((reg_endian != REGMAP_ENDIAN_BIG) ||
772 (val_endian != REGMAP_ENDIAN_BIG))
774 map->use_single_write = true;
777 if (!map->format.format_write &&
778 !(map->format.format_reg && map->format.format_val))
781 map->work_buf = kzalloc(map->format.buf_size, GFP_KERNEL);
782 if (map->work_buf == NULL) {
787 if (map->format.format_write) {
788 map->defer_caching = false;
789 map->reg_write = _regmap_bus_formatted_write;
790 } else if (map->format.format_val) {
791 map->defer_caching = true;
792 map->reg_write = _regmap_bus_raw_write;
795 skip_format_initialization:
797 map->range_tree = RB_ROOT;
798 for (i = 0; i < config->num_ranges; i++) {
799 const struct regmap_range_cfg *range_cfg = &config->ranges[i];
800 struct regmap_range_node *new;
803 if (range_cfg->range_max < range_cfg->range_min) {
804 dev_err(map->dev, "Invalid range %d: %d < %d\n", i,
805 range_cfg->range_max, range_cfg->range_min);
809 if (range_cfg->range_max > map->max_register) {
810 dev_err(map->dev, "Invalid range %d: %d > %d\n", i,
811 range_cfg->range_max, map->max_register);
815 if (range_cfg->selector_reg > map->max_register) {
817 "Invalid range %d: selector out of map\n", i);
821 if (range_cfg->window_len == 0) {
822 dev_err(map->dev, "Invalid range %d: window_len 0\n",
827 /* Make sure, that this register range has no selector
828 or data window within its boundary */
829 for (j = 0; j < config->num_ranges; j++) {
830 unsigned sel_reg = config->ranges[j].selector_reg;
831 unsigned win_min = config->ranges[j].window_start;
832 unsigned win_max = win_min +
833 config->ranges[j].window_len - 1;
835 /* Allow data window inside its own virtual range */
839 if (range_cfg->range_min <= sel_reg &&
840 sel_reg <= range_cfg->range_max) {
842 "Range %d: selector for %d in window\n",
847 if (!(win_max < range_cfg->range_min ||
848 win_min > range_cfg->range_max)) {
850 "Range %d: window for %d in window\n",
856 new = kzalloc(sizeof(*new), GFP_KERNEL);
863 new->name = range_cfg->name;
864 new->range_min = range_cfg->range_min;
865 new->range_max = range_cfg->range_max;
866 new->selector_reg = range_cfg->selector_reg;
867 new->selector_mask = range_cfg->selector_mask;
868 new->selector_shift = range_cfg->selector_shift;
869 new->window_start = range_cfg->window_start;
870 new->window_len = range_cfg->window_len;
872 if (!_regmap_range_add(map, new)) {
873 dev_err(map->dev, "Failed to add range %d\n", i);
878 if (map->selector_work_buf == NULL) {
879 map->selector_work_buf =
880 kzalloc(map->format.buf_size, GFP_KERNEL);
881 if (map->selector_work_buf == NULL) {
888 ret = regcache_init(map, config);
893 ret = regmap_attach_dev(dev, map, config);
903 regmap_range_exit(map);
904 kfree(map->work_buf);
910 EXPORT_SYMBOL_GPL(regmap_init);
912 static void devm_regmap_release(struct device *dev, void *res)
914 regmap_exit(*(struct regmap **)res);
918 * devm_regmap_init(): Initialise managed register map
920 * @dev: Device that will be interacted with
921 * @bus: Bus-specific callbacks to use with device
922 * @bus_context: Data passed to bus-specific callbacks
923 * @config: Configuration for register map
925 * The return value will be an ERR_PTR() on error or a valid pointer
926 * to a struct regmap. This function should generally not be called
927 * directly, it should be called by bus-specific init functions. The
928 * map will be automatically freed by the device management code.
930 struct regmap *devm_regmap_init(struct device *dev,
931 const struct regmap_bus *bus,
933 const struct regmap_config *config)
935 struct regmap **ptr, *regmap;
937 ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL);
939 return ERR_PTR(-ENOMEM);
941 regmap = regmap_init(dev, bus, bus_context, config);
942 if (!IS_ERR(regmap)) {
944 devres_add(dev, ptr);
951 EXPORT_SYMBOL_GPL(devm_regmap_init);
953 static void regmap_field_init(struct regmap_field *rm_field,
954 struct regmap *regmap, struct reg_field reg_field)
956 rm_field->regmap = regmap;
957 rm_field->reg = reg_field.reg;
958 rm_field->shift = reg_field.lsb;
959 rm_field->mask = GENMASK(reg_field.msb, reg_field.lsb);
960 rm_field->id_size = reg_field.id_size;
961 rm_field->id_offset = reg_field.id_offset;
965 * devm_regmap_field_alloc(): Allocate and initialise a register field
968 * @dev: Device that will be interacted with
969 * @regmap: regmap bank in which this register field is located.
970 * @reg_field: Register field with in the bank.
972 * The return value will be an ERR_PTR() on error or a valid pointer
973 * to a struct regmap_field. The regmap_field will be automatically freed
974 * by the device management code.
976 struct regmap_field *devm_regmap_field_alloc(struct device *dev,
977 struct regmap *regmap, struct reg_field reg_field)
979 struct regmap_field *rm_field = devm_kzalloc(dev,
980 sizeof(*rm_field), GFP_KERNEL);
982 return ERR_PTR(-ENOMEM);
984 regmap_field_init(rm_field, regmap, reg_field);
989 EXPORT_SYMBOL_GPL(devm_regmap_field_alloc);
992 * devm_regmap_field_free(): Free register field allocated using
993 * devm_regmap_field_alloc. Usally drivers need not call this function,
994 * as the memory allocated via devm will be freed as per device-driver
997 * @dev: Device that will be interacted with
998 * @field: regmap field which should be freed.
1000 void devm_regmap_field_free(struct device *dev,
1001 struct regmap_field *field)
1003 devm_kfree(dev, field);
1005 EXPORT_SYMBOL_GPL(devm_regmap_field_free);
1008 * regmap_field_alloc(): Allocate and initialise a register field
1009 * in a register map.
1011 * @regmap: regmap bank in which this register field is located.
1012 * @reg_field: Register field with in the bank.
1014 * The return value will be an ERR_PTR() on error or a valid pointer
1015 * to a struct regmap_field. The regmap_field should be freed by the
1016 * user once its finished working with it using regmap_field_free().
1018 struct regmap_field *regmap_field_alloc(struct regmap *regmap,
1019 struct reg_field reg_field)
1021 struct regmap_field *rm_field = kzalloc(sizeof(*rm_field), GFP_KERNEL);
1024 return ERR_PTR(-ENOMEM);
1026 regmap_field_init(rm_field, regmap, reg_field);
1030 EXPORT_SYMBOL_GPL(regmap_field_alloc);
1033 * regmap_field_free(): Free register field allocated using regmap_field_alloc
1035 * @field: regmap field which should be freed.
1037 void regmap_field_free(struct regmap_field *field)
1041 EXPORT_SYMBOL_GPL(regmap_field_free);
1044 * regmap_reinit_cache(): Reinitialise the current register cache
1046 * @map: Register map to operate on.
1047 * @config: New configuration. Only the cache data will be used.
1049 * Discard any existing register cache for the map and initialize a
1050 * new cache. This can be used to restore the cache to defaults or to
1051 * update the cache configuration to reflect runtime discovery of the
1054 * No explicit locking is done here, the user needs to ensure that
1055 * this function will not race with other calls to regmap.
1057 int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
1060 regmap_debugfs_exit(map);
1062 map->max_register = config->max_register;
1063 map->writeable_reg = config->writeable_reg;
1064 map->readable_reg = config->readable_reg;
1065 map->volatile_reg = config->volatile_reg;
1066 map->precious_reg = config->precious_reg;
1067 map->cache_type = config->cache_type;
1069 regmap_debugfs_init(map, config->name);
1071 map->cache_bypass = false;
1072 map->cache_only = false;
1074 return regcache_init(map, config);
1076 EXPORT_SYMBOL_GPL(regmap_reinit_cache);
1079 * regmap_exit(): Free a previously allocated register map
1081 void regmap_exit(struct regmap *map)
1083 struct regmap_async *async;
1086 regmap_debugfs_exit(map);
1087 regmap_range_exit(map);
1088 if (map->bus && map->bus->free_context)
1089 map->bus->free_context(map->bus_context);
1090 kfree(map->work_buf);
1091 while (!list_empty(&map->async_free)) {
1092 async = list_first_entry_or_null(&map->async_free,
1093 struct regmap_async,
1095 list_del(&async->list);
1096 kfree(async->work_buf);
1101 EXPORT_SYMBOL_GPL(regmap_exit);
1103 static int dev_get_regmap_match(struct device *dev, void *res, void *data)
1105 struct regmap **r = res;
1111 /* If the user didn't specify a name match any */
1113 return (*r)->name == data;
1119 * dev_get_regmap(): Obtain the regmap (if any) for a device
1121 * @dev: Device to retrieve the map for
1122 * @name: Optional name for the register map, usually NULL.
1124 * Returns the regmap for the device if one is present, or NULL. If
1125 * name is specified then it must match the name specified when
1126 * registering the device, if it is NULL then the first regmap found
1127 * will be used. Devices with multiple register maps are very rare,
1128 * generic code should normally not need to specify a name.
1130 struct regmap *dev_get_regmap(struct device *dev, const char *name)
1132 struct regmap **r = devres_find(dev, dev_get_regmap_release,
1133 dev_get_regmap_match, (void *)name);
1139 EXPORT_SYMBOL_GPL(dev_get_regmap);
1142 * regmap_get_device(): Obtain the device from a regmap
1144 * @map: Register map to operate on.
1146 * Returns the underlying device that the regmap has been created for.
1148 struct device *regmap_get_device(struct regmap *map)
1152 EXPORT_SYMBOL_GPL(regmap_get_device);
1154 static int _regmap_select_page(struct regmap *map, unsigned int *reg,
1155 struct regmap_range_node *range,
1156 unsigned int val_num)
1158 void *orig_work_buf;
1159 unsigned int win_offset;
1160 unsigned int win_page;
1164 win_offset = (*reg - range->range_min) % range->window_len;
1165 win_page = (*reg - range->range_min) / range->window_len;
1168 /* Bulk write shouldn't cross range boundary */
1169 if (*reg + val_num - 1 > range->range_max)
1172 /* ... or single page boundary */
1173 if (val_num > range->window_len - win_offset)
1177 /* It is possible to have selector register inside data window.
1178 In that case, selector register is located on every page and
1179 it needs no page switching, when accessed alone. */
1181 range->window_start + win_offset != range->selector_reg) {
1182 /* Use separate work_buf during page switching */
1183 orig_work_buf = map->work_buf;
1184 map->work_buf = map->selector_work_buf;
1186 ret = _regmap_update_bits(map, range->selector_reg,
1187 range->selector_mask,
1188 win_page << range->selector_shift,
1191 map->work_buf = orig_work_buf;
1197 *reg = range->window_start + win_offset;
1202 int _regmap_raw_write(struct regmap *map, unsigned int reg,
1203 const void *val, size_t val_len)
1205 struct regmap_range_node *range;
1206 unsigned long flags;
1207 u8 *u8 = map->work_buf;
1208 void *work_val = map->work_buf + map->format.reg_bytes +
1209 map->format.pad_bytes;
1211 int ret = -ENOTSUPP;
1217 /* Check for unwritable registers before we start */
1218 if (map->writeable_reg)
1219 for (i = 0; i < val_len / map->format.val_bytes; i++)
1220 if (!map->writeable_reg(map->dev,
1221 reg + (i * map->reg_stride)))
1224 if (!map->cache_bypass && map->format.parse_val) {
1226 int val_bytes = map->format.val_bytes;
1227 for (i = 0; i < val_len / val_bytes; i++) {
1228 ival = map->format.parse_val(val + (i * val_bytes));
1229 ret = regcache_write(map, reg + (i * map->reg_stride),
1233 "Error in caching of register: %x ret: %d\n",
1238 if (map->cache_only) {
1239 map->cache_dirty = true;
1244 range = _regmap_range_lookup(map, reg);
1246 int val_num = val_len / map->format.val_bytes;
1247 int win_offset = (reg - range->range_min) % range->window_len;
1248 int win_residue = range->window_len - win_offset;
1250 /* If the write goes beyond the end of the window split it */
1251 while (val_num > win_residue) {
1252 dev_dbg(map->dev, "Writing window %d/%zu\n",
1253 win_residue, val_len / map->format.val_bytes);
1254 ret = _regmap_raw_write(map, reg, val, win_residue *
1255 map->format.val_bytes);
1260 val_num -= win_residue;
1261 val += win_residue * map->format.val_bytes;
1262 val_len -= win_residue * map->format.val_bytes;
1264 win_offset = (reg - range->range_min) %
1266 win_residue = range->window_len - win_offset;
1269 ret = _regmap_select_page(map, ®, range, val_num);
1274 map->format.format_reg(map->work_buf, reg, map->reg_shift);
1276 u8[0] |= map->write_flag_mask;
1279 * Essentially all I/O mechanisms will be faster with a single
1280 * buffer to write. Since register syncs often generate raw
1281 * writes of single registers optimise that case.
1283 if (val != work_val && val_len == map->format.val_bytes) {
1284 memcpy(work_val, val, map->format.val_bytes);
1288 if (map->async && map->bus->async_write) {
1289 struct regmap_async *async;
1291 trace_regmap_async_write_start(map, reg, val_len);
1293 spin_lock_irqsave(&map->async_lock, flags);
1294 async = list_first_entry_or_null(&map->async_free,
1295 struct regmap_async,
1298 list_del(&async->list);
1299 spin_unlock_irqrestore(&map->async_lock, flags);
1302 async = map->bus->async_alloc();
1306 async->work_buf = kzalloc(map->format.buf_size,
1307 GFP_KERNEL | GFP_DMA);
1308 if (!async->work_buf) {
1316 /* If the caller supplied the value we can use it safely. */
1317 memcpy(async->work_buf, map->work_buf, map->format.pad_bytes +
1318 map->format.reg_bytes + map->format.val_bytes);
1320 spin_lock_irqsave(&map->async_lock, flags);
1321 list_add_tail(&async->list, &map->async_list);
1322 spin_unlock_irqrestore(&map->async_lock, flags);
1324 if (val != work_val)
1325 ret = map->bus->async_write(map->bus_context,
1327 map->format.reg_bytes +
1328 map->format.pad_bytes,
1329 val, val_len, async);
1331 ret = map->bus->async_write(map->bus_context,
1333 map->format.reg_bytes +
1334 map->format.pad_bytes +
1335 val_len, NULL, 0, async);
1338 dev_err(map->dev, "Failed to schedule write: %d\n",
1341 spin_lock_irqsave(&map->async_lock, flags);
1342 list_move(&async->list, &map->async_free);
1343 spin_unlock_irqrestore(&map->async_lock, flags);
1349 trace_regmap_hw_write_start(map, reg, val_len / map->format.val_bytes);
1351 /* If we're doing a single register write we can probably just
1352 * send the work_buf directly, otherwise try to do a gather
1355 if (val == work_val)
1356 ret = map->bus->write(map->bus_context, map->work_buf,
1357 map->format.reg_bytes +
1358 map->format.pad_bytes +
1360 else if (map->bus->gather_write)
1361 ret = map->bus->gather_write(map->bus_context, map->work_buf,
1362 map->format.reg_bytes +
1363 map->format.pad_bytes,
1366 /* If that didn't work fall back on linearising by hand. */
1367 if (ret == -ENOTSUPP) {
1368 len = map->format.reg_bytes + map->format.pad_bytes + val_len;
1369 buf = kzalloc(len, GFP_KERNEL);
1373 memcpy(buf, map->work_buf, map->format.reg_bytes);
1374 memcpy(buf + map->format.reg_bytes + map->format.pad_bytes,
1376 ret = map->bus->write(map->bus_context, buf, len);
1381 trace_regmap_hw_write_done(map, reg, val_len / map->format.val_bytes);
1387 * regmap_can_raw_write - Test if regmap_raw_write() is supported
1389 * @map: Map to check.
1391 bool regmap_can_raw_write(struct regmap *map)
1393 return map->bus && map->bus->write && map->format.format_val &&
1394 map->format.format_reg;
1396 EXPORT_SYMBOL_GPL(regmap_can_raw_write);
1399 * regmap_get_raw_read_max - Get the maximum size we can read
1401 * @map: Map to check.
1403 size_t regmap_get_raw_read_max(struct regmap *map)
1405 return map->max_raw_read;
1407 EXPORT_SYMBOL_GPL(regmap_get_raw_read_max);
1410 * regmap_get_raw_write_max - Get the maximum size we can read
1412 * @map: Map to check.
1414 size_t regmap_get_raw_write_max(struct regmap *map)
1416 return map->max_raw_write;
1418 EXPORT_SYMBOL_GPL(regmap_get_raw_write_max);
1420 static int _regmap_bus_formatted_write(void *context, unsigned int reg,
1424 struct regmap_range_node *range;
1425 struct regmap *map = context;
1427 WARN_ON(!map->bus || !map->format.format_write);
1429 range = _regmap_range_lookup(map, reg);
1431 ret = _regmap_select_page(map, ®, range, 1);
1436 map->format.format_write(map, reg, val);
1438 trace_regmap_hw_write_start(map, reg, 1);
1440 ret = map->bus->write(map->bus_context, map->work_buf,
1441 map->format.buf_size);
1443 trace_regmap_hw_write_done(map, reg, 1);
1448 static int _regmap_bus_reg_write(void *context, unsigned int reg,
1451 struct regmap *map = context;
1453 return map->bus->reg_write(map->bus_context, reg, val);
1456 static int _regmap_bus_raw_write(void *context, unsigned int reg,
1459 struct regmap *map = context;
1461 WARN_ON(!map->bus || !map->format.format_val);
1463 map->format.format_val(map->work_buf + map->format.reg_bytes
1464 + map->format.pad_bytes, val, 0);
1465 return _regmap_raw_write(map, reg,
1467 map->format.reg_bytes +
1468 map->format.pad_bytes,
1469 map->format.val_bytes);
1472 static inline void *_regmap_map_get_context(struct regmap *map)
1474 return (map->bus) ? map : map->bus_context;
1477 int _regmap_write(struct regmap *map, unsigned int reg,
1481 void *context = _regmap_map_get_context(map);
1483 if (!regmap_writeable(map, reg))
1486 if (!map->cache_bypass && !map->defer_caching) {
1487 ret = regcache_write(map, reg, val);
1490 if (map->cache_only) {
1491 map->cache_dirty = true;
1497 if (map->dev && strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
1498 dev_info(map->dev, "%x <= %x\n", reg, val);
1501 trace_regmap_reg_write(map, reg, val);
1503 return map->reg_write(context, reg, val);
1507 * regmap_write(): Write a value to a single register
1509 * @map: Register map to write to
1510 * @reg: Register to write to
1511 * @val: Value to be written
1513 * A value of zero will be returned on success, a negative errno will
1514 * be returned in error cases.
1516 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
1520 if (reg % map->reg_stride)
1523 map->lock(map->lock_arg);
1525 ret = _regmap_write(map, reg, val);
1527 map->unlock(map->lock_arg);
1531 EXPORT_SYMBOL_GPL(regmap_write);
1534 * regmap_write_async(): Write a value to a single register asynchronously
1536 * @map: Register map to write to
1537 * @reg: Register to write to
1538 * @val: Value to be written
1540 * A value of zero will be returned on success, a negative errno will
1541 * be returned in error cases.
1543 int regmap_write_async(struct regmap *map, unsigned int reg, unsigned int val)
1547 if (reg % map->reg_stride)
1550 map->lock(map->lock_arg);
1554 ret = _regmap_write(map, reg, val);
1558 map->unlock(map->lock_arg);
1562 EXPORT_SYMBOL_GPL(regmap_write_async);
1565 * regmap_raw_write(): Write raw values to one or more registers
1567 * @map: Register map to write to
1568 * @reg: Initial register to write to
1569 * @val: Block of data to be written, laid out for direct transmission to the
1571 * @val_len: Length of data pointed to by val.
1573 * This function is intended to be used for things like firmware
1574 * download where a large block of data needs to be transferred to the
1575 * device. No formatting will be done on the data provided.
1577 * A value of zero will be returned on success, a negative errno will
1578 * be returned in error cases.
1580 int regmap_raw_write(struct regmap *map, unsigned int reg,
1581 const void *val, size_t val_len)
1585 if (!regmap_can_raw_write(map))
1587 if (val_len % map->format.val_bytes)
1589 if (map->max_raw_write && map->max_raw_write > val_len)
1592 map->lock(map->lock_arg);
1594 ret = _regmap_raw_write(map, reg, val, val_len);
1596 map->unlock(map->lock_arg);
1600 EXPORT_SYMBOL_GPL(regmap_raw_write);
1603 * regmap_field_write(): Write a value to a single register field
1605 * @field: Register field to write to
1606 * @val: Value to be written
1608 * A value of zero will be returned on success, a negative errno will
1609 * be returned in error cases.
1611 int regmap_field_write(struct regmap_field *field, unsigned int val)
1613 return regmap_update_bits(field->regmap, field->reg,
1614 field->mask, val << field->shift);
1616 EXPORT_SYMBOL_GPL(regmap_field_write);
1619 * regmap_field_update_bits(): Perform a read/modify/write cycle
1620 * on the register field
1622 * @field: Register field to write to
1623 * @mask: Bitmask to change
1624 * @val: Value to be written
1626 * A value of zero will be returned on success, a negative errno will
1627 * be returned in error cases.
1629 int regmap_field_update_bits(struct regmap_field *field, unsigned int mask, unsigned int val)
1631 mask = (mask << field->shift) & field->mask;
1633 return regmap_update_bits(field->regmap, field->reg,
1634 mask, val << field->shift);
1636 EXPORT_SYMBOL_GPL(regmap_field_update_bits);
1639 * regmap_fields_write(): Write a value to a single register field with port ID
1641 * @field: Register field to write to
1643 * @val: Value to be written
1645 * A value of zero will be returned on success, a negative errno will
1646 * be returned in error cases.
1648 int regmap_fields_write(struct regmap_field *field, unsigned int id,
1651 if (id >= field->id_size)
1654 return regmap_update_bits(field->regmap,
1655 field->reg + (field->id_offset * id),
1656 field->mask, val << field->shift);
1658 EXPORT_SYMBOL_GPL(regmap_fields_write);
1661 * regmap_fields_update_bits(): Perform a read/modify/write cycle
1662 * on the register field
1664 * @field: Register field to write to
1666 * @mask: Bitmask to change
1667 * @val: Value to be written
1669 * A value of zero will be returned on success, a negative errno will
1670 * be returned in error cases.
1672 int regmap_fields_update_bits(struct regmap_field *field, unsigned int id,
1673 unsigned int mask, unsigned int val)
1675 if (id >= field->id_size)
1678 mask = (mask << field->shift) & field->mask;
1680 return regmap_update_bits(field->regmap,
1681 field->reg + (field->id_offset * id),
1682 mask, val << field->shift);
1684 EXPORT_SYMBOL_GPL(regmap_fields_update_bits);
1687 * regmap_bulk_write(): Write multiple registers to the device
1689 * @map: Register map to write to
1690 * @reg: First register to be write from
1691 * @val: Block of data to be written, in native register size for device
1692 * @val_count: Number of registers to write
1694 * This function is intended to be used for writing a large block of
1695 * data to the device either in single transfer or multiple transfer.
1697 * A value of zero will be returned on success, a negative errno will
1698 * be returned in error cases.
1700 int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
1704 size_t val_bytes = map->format.val_bytes;
1705 size_t total_size = val_bytes * val_count;
1707 if (map->bus && !map->format.parse_inplace)
1709 if (reg % map->reg_stride)
1713 * Some devices don't support bulk write, for
1714 * them we have a series of single write operations in the first two if
1717 * The first if block is used for memory mapped io. It does not allow
1718 * val_bytes of 3 for example.
1719 * The second one is used for busses which do not have this limitation
1720 * and can write arbitrary value lengths.
1723 map->lock(map->lock_arg);
1724 for (i = 0; i < val_count; i++) {
1727 switch (val_bytes) {
1729 ival = *(u8 *)(val + (i * val_bytes));
1732 ival = *(u16 *)(val + (i * val_bytes));
1735 ival = *(u32 *)(val + (i * val_bytes));
1739 ival = *(u64 *)(val + (i * val_bytes));
1747 ret = _regmap_write(map, reg + (i * map->reg_stride),
1753 map->unlock(map->lock_arg);
1754 } else if (map->use_single_write ||
1755 (map->max_raw_write && map->max_raw_write < total_size)) {
1756 int chunk_stride = map->reg_stride;
1757 size_t chunk_size = val_bytes;
1758 size_t chunk_count = val_count;
1760 if (!map->use_single_write) {
1761 chunk_size = map->max_raw_write;
1762 if (chunk_size % val_bytes)
1763 chunk_size -= chunk_size % val_bytes;
1764 chunk_count = total_size / chunk_size;
1765 chunk_stride *= chunk_size / val_bytes;
1768 map->lock(map->lock_arg);
1769 /* Write as many bytes as possible with chunk_size */
1770 for (i = 0; i < chunk_count; i++) {
1771 ret = _regmap_raw_write(map,
1772 reg + (i * chunk_stride),
1773 val + (i * chunk_size),
1779 /* Write remaining bytes */
1780 if (!ret && chunk_size * i < total_size) {
1781 ret = _regmap_raw_write(map, reg + (i * chunk_stride),
1782 val + (i * chunk_size),
1783 total_size - i * chunk_size);
1785 map->unlock(map->lock_arg);
1792 wval = kmemdup(val, val_count * val_bytes, GFP_KERNEL);
1794 dev_err(map->dev, "Error in memory allocation\n");
1797 for (i = 0; i < val_count * val_bytes; i += val_bytes)
1798 map->format.parse_inplace(wval + i);
1800 map->lock(map->lock_arg);
1801 ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count);
1802 map->unlock(map->lock_arg);
1808 EXPORT_SYMBOL_GPL(regmap_bulk_write);
1811 * _regmap_raw_multi_reg_write()
1813 * the (register,newvalue) pairs in regs have not been formatted, but
1814 * they are all in the same page and have been changed to being page
1815 * relative. The page register has been written if that was necessary.
1817 static int _regmap_raw_multi_reg_write(struct regmap *map,
1818 const struct reg_default *regs,
1825 size_t val_bytes = map->format.val_bytes;
1826 size_t reg_bytes = map->format.reg_bytes;
1827 size_t pad_bytes = map->format.pad_bytes;
1828 size_t pair_size = reg_bytes + pad_bytes + val_bytes;
1829 size_t len = pair_size * num_regs;
1834 buf = kzalloc(len, GFP_KERNEL);
1838 /* We have to linearise by hand. */
1842 for (i = 0; i < num_regs; i++) {
1843 int reg = regs[i].reg;
1844 int val = regs[i].def;
1845 trace_regmap_hw_write_start(map, reg, 1);
1846 map->format.format_reg(u8, reg, map->reg_shift);
1847 u8 += reg_bytes + pad_bytes;
1848 map->format.format_val(u8, val, 0);
1852 *u8 |= map->write_flag_mask;
1854 ret = map->bus->write(map->bus_context, buf, len);
1858 for (i = 0; i < num_regs; i++) {
1859 int reg = regs[i].reg;
1860 trace_regmap_hw_write_done(map, reg, 1);
1865 static unsigned int _regmap_register_page(struct regmap *map,
1867 struct regmap_range_node *range)
1869 unsigned int win_page = (reg - range->range_min) / range->window_len;
1874 static int _regmap_range_multi_paged_reg_write(struct regmap *map,
1875 struct reg_default *regs,
1880 struct reg_default *base;
1881 unsigned int this_page = 0;
1883 * the set of registers are not neccessarily in order, but
1884 * since the order of write must be preserved this algorithm
1885 * chops the set each time the page changes
1888 for (i = 0, n = 0; i < num_regs; i++, n++) {
1889 unsigned int reg = regs[i].reg;
1890 struct regmap_range_node *range;
1892 range = _regmap_range_lookup(map, reg);
1894 unsigned int win_page = _regmap_register_page(map, reg,
1898 this_page = win_page;
1899 if (win_page != this_page) {
1900 this_page = win_page;
1901 ret = _regmap_raw_multi_reg_write(map, base, n);
1907 ret = _regmap_select_page(map, &base[n].reg, range, 1);
1913 return _regmap_raw_multi_reg_write(map, base, n);
1917 static int _regmap_multi_reg_write(struct regmap *map,
1918 const struct reg_default *regs,
1924 if (!map->can_multi_write) {
1925 for (i = 0; i < num_regs; i++) {
1926 ret = _regmap_write(map, regs[i].reg, regs[i].def);
1933 if (!map->format.parse_inplace)
1936 if (map->writeable_reg)
1937 for (i = 0; i < num_regs; i++) {
1938 int reg = regs[i].reg;
1939 if (!map->writeable_reg(map->dev, reg))
1941 if (reg % map->reg_stride)
1945 if (!map->cache_bypass) {
1946 for (i = 0; i < num_regs; i++) {
1947 unsigned int val = regs[i].def;
1948 unsigned int reg = regs[i].reg;
1949 ret = regcache_write(map, reg, val);
1952 "Error in caching of register: %x ret: %d\n",
1957 if (map->cache_only) {
1958 map->cache_dirty = true;
1965 for (i = 0; i < num_regs; i++) {
1966 unsigned int reg = regs[i].reg;
1967 struct regmap_range_node *range;
1968 range = _regmap_range_lookup(map, reg);
1970 size_t len = sizeof(struct reg_default)*num_regs;
1971 struct reg_default *base = kmemdup(regs, len,
1975 ret = _regmap_range_multi_paged_reg_write(map, base,
1982 return _regmap_raw_multi_reg_write(map, regs, num_regs);
1986 * regmap_multi_reg_write(): Write multiple registers to the device
1988 * where the set of register,value pairs are supplied in any order,
1989 * possibly not all in a single range.
1991 * @map: Register map to write to
1992 * @regs: Array of structures containing register,value to be written
1993 * @num_regs: Number of registers to write
1995 * The 'normal' block write mode will send ultimately send data on the
1996 * target bus as R,V1,V2,V3,..,Vn where successively higer registers are
1997 * addressed. However, this alternative block multi write mode will send
1998 * the data as R1,V1,R2,V2,..,Rn,Vn on the target bus. The target device
1999 * must of course support the mode.
2001 * A value of zero will be returned on success, a negative errno will be
2002 * returned in error cases.
2004 int regmap_multi_reg_write(struct regmap *map, const struct reg_default *regs,
2009 map->lock(map->lock_arg);
2011 ret = _regmap_multi_reg_write(map, regs, num_regs);
2013 map->unlock(map->lock_arg);
2017 EXPORT_SYMBOL_GPL(regmap_multi_reg_write);
2020 * regmap_multi_reg_write_bypassed(): Write multiple registers to the
2021 * device but not the cache
2023 * where the set of register are supplied in any order
2025 * @map: Register map to write to
2026 * @regs: Array of structures containing register,value to be written
2027 * @num_regs: Number of registers to write
2029 * This function is intended to be used for writing a large block of data
2030 * atomically to the device in single transfer for those I2C client devices
2031 * that implement this alternative block write mode.
2033 * A value of zero will be returned on success, a negative errno will
2034 * be returned in error cases.
2036 int regmap_multi_reg_write_bypassed(struct regmap *map,
2037 const struct reg_default *regs,
2043 map->lock(map->lock_arg);
2045 bypass = map->cache_bypass;
2046 map->cache_bypass = true;
2048 ret = _regmap_multi_reg_write(map, regs, num_regs);
2050 map->cache_bypass = bypass;
2052 map->unlock(map->lock_arg);
2056 EXPORT_SYMBOL_GPL(regmap_multi_reg_write_bypassed);
2059 * regmap_raw_write_async(): Write raw values to one or more registers
2062 * @map: Register map to write to
2063 * @reg: Initial register to write to
2064 * @val: Block of data to be written, laid out for direct transmission to the
2065 * device. Must be valid until regmap_async_complete() is called.
2066 * @val_len: Length of data pointed to by val.
2068 * This function is intended to be used for things like firmware
2069 * download where a large block of data needs to be transferred to the
2070 * device. No formatting will be done on the data provided.
2072 * If supported by the underlying bus the write will be scheduled
2073 * asynchronously, helping maximise I/O speed on higher speed buses
2074 * like SPI. regmap_async_complete() can be called to ensure that all
2075 * asynchrnous writes have been completed.
2077 * A value of zero will be returned on success, a negative errno will
2078 * be returned in error cases.
2080 int regmap_raw_write_async(struct regmap *map, unsigned int reg,
2081 const void *val, size_t val_len)
2085 if (val_len % map->format.val_bytes)
2087 if (reg % map->reg_stride)
2090 map->lock(map->lock_arg);
2094 ret = _regmap_raw_write(map, reg, val, val_len);
2098 map->unlock(map->lock_arg);
2102 EXPORT_SYMBOL_GPL(regmap_raw_write_async);
2104 static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
2105 unsigned int val_len)
2107 struct regmap_range_node *range;
2108 u8 *u8 = map->work_buf;
2113 range = _regmap_range_lookup(map, reg);
2115 ret = _regmap_select_page(map, ®, range,
2116 val_len / map->format.val_bytes);
2121 map->format.format_reg(map->work_buf, reg, map->reg_shift);
2124 * Some buses or devices flag reads by setting the high bits in the
2125 * register address; since it's always the high bits for all
2126 * current formats we can do this here rather than in
2127 * formatting. This may break if we get interesting formats.
2129 u8[0] |= map->read_flag_mask;
2131 trace_regmap_hw_read_start(map, reg, val_len / map->format.val_bytes);
2133 ret = map->bus->read(map->bus_context, map->work_buf,
2134 map->format.reg_bytes + map->format.pad_bytes,
2137 trace_regmap_hw_read_done(map, reg, val_len / map->format.val_bytes);
2142 static int _regmap_bus_reg_read(void *context, unsigned int reg,
2145 struct regmap *map = context;
2147 return map->bus->reg_read(map->bus_context, reg, val);
2150 static int _regmap_bus_read(void *context, unsigned int reg,
2154 struct regmap *map = context;
2156 if (!map->format.parse_val)
2159 ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
2161 *val = map->format.parse_val(map->work_buf);
2166 static int _regmap_read(struct regmap *map, unsigned int reg,
2170 void *context = _regmap_map_get_context(map);
2172 if (!map->cache_bypass) {
2173 ret = regcache_read(map, reg, val);
2178 if (map->cache_only)
2181 if (!regmap_readable(map, reg))
2184 ret = map->reg_read(context, reg, val);
2187 if (map->dev && strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
2188 dev_info(map->dev, "%x => %x\n", reg, *val);
2191 trace_regmap_reg_read(map, reg, *val);
2193 if (!map->cache_bypass)
2194 regcache_write(map, reg, *val);
2201 * regmap_read(): Read a value from a single register
2203 * @map: Register map to read from
2204 * @reg: Register to be read from
2205 * @val: Pointer to store read value
2207 * A value of zero will be returned on success, a negative errno will
2208 * be returned in error cases.
2210 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
2214 if (reg % map->reg_stride)
2217 map->lock(map->lock_arg);
2219 ret = _regmap_read(map, reg, val);
2221 map->unlock(map->lock_arg);
2225 EXPORT_SYMBOL_GPL(regmap_read);
2228 * regmap_raw_read(): Read raw data from the device
2230 * @map: Register map to read from
2231 * @reg: First register to be read from
2232 * @val: Pointer to store read value
2233 * @val_len: Size of data to read
2235 * A value of zero will be returned on success, a negative errno will
2236 * be returned in error cases.
2238 int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
2241 size_t val_bytes = map->format.val_bytes;
2242 size_t val_count = val_len / val_bytes;
2248 if (val_len % map->format.val_bytes)
2250 if (reg % map->reg_stride)
2255 map->lock(map->lock_arg);
2257 if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
2258 map->cache_type == REGCACHE_NONE) {
2259 if (!map->bus->read) {
2263 if (map->max_raw_read && map->max_raw_read < val_len) {
2268 /* Physical block read if there's no cache involved */
2269 ret = _regmap_raw_read(map, reg, val, val_len);
2272 /* Otherwise go word by word for the cache; should be low
2273 * cost as we expect to hit the cache.
2275 for (i = 0; i < val_count; i++) {
2276 ret = _regmap_read(map, reg + (i * map->reg_stride),
2281 map->format.format_val(val + (i * val_bytes), v, 0);
2286 map->unlock(map->lock_arg);
2290 EXPORT_SYMBOL_GPL(regmap_raw_read);
2293 * regmap_field_read(): Read a value to a single register field
2295 * @field: Register field to read from
2296 * @val: Pointer to store read value
2298 * A value of zero will be returned on success, a negative errno will
2299 * be returned in error cases.
2301 int regmap_field_read(struct regmap_field *field, unsigned int *val)
2304 unsigned int reg_val;
2305 ret = regmap_read(field->regmap, field->reg, ®_val);
2309 reg_val &= field->mask;
2310 reg_val >>= field->shift;
2315 EXPORT_SYMBOL_GPL(regmap_field_read);
2318 * regmap_fields_read(): Read a value to a single register field with port ID
2320 * @field: Register field to read from
2322 * @val: Pointer to store read value
2324 * A value of zero will be returned on success, a negative errno will
2325 * be returned in error cases.
2327 int regmap_fields_read(struct regmap_field *field, unsigned int id,
2331 unsigned int reg_val;
2333 if (id >= field->id_size)
2336 ret = regmap_read(field->regmap,
2337 field->reg + (field->id_offset * id),
2342 reg_val &= field->mask;
2343 reg_val >>= field->shift;
2348 EXPORT_SYMBOL_GPL(regmap_fields_read);
2351 * regmap_bulk_read(): Read multiple registers from the device
2353 * @map: Register map to read from
2354 * @reg: First register to be read from
2355 * @val: Pointer to store read value, in native register size for device
2356 * @val_count: Number of registers to read
2358 * A value of zero will be returned on success, a negative errno will
2359 * be returned in error cases.
2361 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
2365 size_t val_bytes = map->format.val_bytes;
2366 bool vol = regmap_volatile_range(map, reg, val_count);
2368 if (reg % map->reg_stride)
2371 if (map->bus && map->format.parse_inplace && (vol || map->cache_type == REGCACHE_NONE)) {
2373 * Some devices does not support bulk read, for
2374 * them we have a series of single read operations.
2376 size_t total_size = val_bytes * val_count;
2378 if (!map->use_single_read &&
2379 (!map->max_raw_read || map->max_raw_read > total_size)) {
2380 ret = regmap_raw_read(map, reg, val,
2381 val_bytes * val_count);
2386 * Some devices do not support bulk read or do not
2387 * support large bulk reads, for them we have a series
2388 * of read operations.
2390 int chunk_stride = map->reg_stride;
2391 size_t chunk_size = val_bytes;
2392 size_t chunk_count = val_count;
2394 if (!map->use_single_read) {
2395 chunk_size = map->max_raw_read;
2396 if (chunk_size % val_bytes)
2397 chunk_size -= chunk_size % val_bytes;
2398 chunk_count = total_size / chunk_size;
2399 chunk_stride *= chunk_size / val_bytes;
2402 /* Read bytes that fit into a multiple of chunk_size */
2403 for (i = 0; i < chunk_count; i++) {
2404 ret = regmap_raw_read(map,
2405 reg + (i * chunk_stride),
2406 val + (i * chunk_size),
2412 /* Read remaining bytes */
2413 if (chunk_size * i < total_size) {
2414 ret = regmap_raw_read(map,
2415 reg + (i * chunk_stride),
2416 val + (i * chunk_size),
2417 total_size - i * chunk_size);
2423 for (i = 0; i < val_count * val_bytes; i += val_bytes)
2424 map->format.parse_inplace(val + i);
2426 for (i = 0; i < val_count; i++) {
2428 ret = regmap_read(map, reg + (i * map->reg_stride),
2433 if (map->format.format_val) {
2434 map->format.format_val(val + (i * val_bytes), ival, 0);
2436 /* Devices providing read and write
2437 * operations can use the bulk I/O
2438 * functions if they define a val_bytes,
2439 * we assume that the values are native
2446 switch (map->format.val_bytes) {
2465 EXPORT_SYMBOL_GPL(regmap_bulk_read);
2467 static int _regmap_update_bits(struct regmap *map, unsigned int reg,
2468 unsigned int mask, unsigned int val,
2472 unsigned int tmp, orig;
2474 ret = _regmap_read(map, reg, &orig);
2482 ret = _regmap_write(map, reg, tmp);
2494 * regmap_update_bits: Perform a read/modify/write cycle on the register map
2496 * @map: Register map to update
2497 * @reg: Register to update
2498 * @mask: Bitmask to change
2499 * @val: New value for bitmask
2501 * Returns zero for success, a negative number on error.
2503 int regmap_update_bits(struct regmap *map, unsigned int reg,
2504 unsigned int mask, unsigned int val)
2508 map->lock(map->lock_arg);
2509 ret = _regmap_update_bits(map, reg, mask, val, NULL);
2510 map->unlock(map->lock_arg);
2514 EXPORT_SYMBOL_GPL(regmap_update_bits);
2517 * regmap_update_bits_async: Perform a read/modify/write cycle on the register
2518 * map asynchronously
2520 * @map: Register map to update
2521 * @reg: Register to update
2522 * @mask: Bitmask to change
2523 * @val: New value for bitmask
2525 * With most buses the read must be done synchronously so this is most
2526 * useful for devices with a cache which do not need to interact with
2527 * the hardware to determine the current register value.
2529 * Returns zero for success, a negative number on error.
2531 int regmap_update_bits_async(struct regmap *map, unsigned int reg,
2532 unsigned int mask, unsigned int val)
2536 map->lock(map->lock_arg);
2540 ret = _regmap_update_bits(map, reg, mask, val, NULL);
2544 map->unlock(map->lock_arg);
2548 EXPORT_SYMBOL_GPL(regmap_update_bits_async);
2551 * regmap_update_bits_check: Perform a read/modify/write cycle on the
2552 * register map and report if updated
2554 * @map: Register map to update
2555 * @reg: Register to update
2556 * @mask: Bitmask to change
2557 * @val: New value for bitmask
2558 * @change: Boolean indicating if a write was done
2560 * Returns zero for success, a negative number on error.
2562 int regmap_update_bits_check(struct regmap *map, unsigned int reg,
2563 unsigned int mask, unsigned int val,
2568 map->lock(map->lock_arg);
2569 ret = _regmap_update_bits(map, reg, mask, val, change);
2570 map->unlock(map->lock_arg);
2573 EXPORT_SYMBOL_GPL(regmap_update_bits_check);
2576 * regmap_update_bits_check_async: Perform a read/modify/write cycle on the
2577 * register map asynchronously and report if
2580 * @map: Register map to update
2581 * @reg: Register to update
2582 * @mask: Bitmask to change
2583 * @val: New value for bitmask
2584 * @change: Boolean indicating if a write was done
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_check_async(struct regmap *map, unsigned int reg,
2593 unsigned int mask, unsigned int val,
2598 map->lock(map->lock_arg);
2602 ret = _regmap_update_bits(map, reg, mask, val, change);
2606 map->unlock(map->lock_arg);
2610 EXPORT_SYMBOL_GPL(regmap_update_bits_check_async);
2612 void regmap_async_complete_cb(struct regmap_async *async, int ret)
2614 struct regmap *map = async->map;
2617 trace_regmap_async_io_complete(map);
2619 spin_lock(&map->async_lock);
2620 list_move(&async->list, &map->async_free);
2621 wake = list_empty(&map->async_list);
2624 map->async_ret = ret;
2626 spin_unlock(&map->async_lock);
2629 wake_up(&map->async_waitq);
2631 EXPORT_SYMBOL_GPL(regmap_async_complete_cb);
2633 static int regmap_async_is_done(struct regmap *map)
2635 unsigned long flags;
2638 spin_lock_irqsave(&map->async_lock, flags);
2639 ret = list_empty(&map->async_list);
2640 spin_unlock_irqrestore(&map->async_lock, flags);
2646 * regmap_async_complete: Ensure all asynchronous I/O has completed.
2648 * @map: Map to operate on.
2650 * Blocks until any pending asynchronous I/O has completed. Returns
2651 * an error code for any failed I/O operations.
2653 int regmap_async_complete(struct regmap *map)
2655 unsigned long flags;
2658 /* Nothing to do with no async support */
2659 if (!map->bus || !map->bus->async_write)
2662 trace_regmap_async_complete_start(map);
2664 wait_event(map->async_waitq, regmap_async_is_done(map));
2666 spin_lock_irqsave(&map->async_lock, flags);
2667 ret = map->async_ret;
2669 spin_unlock_irqrestore(&map->async_lock, flags);
2671 trace_regmap_async_complete_done(map);
2675 EXPORT_SYMBOL_GPL(regmap_async_complete);
2678 * regmap_register_patch: Register and apply register updates to be applied
2679 * on device initialistion
2681 * @map: Register map to apply updates to.
2682 * @regs: Values to update.
2683 * @num_regs: Number of entries in regs.
2685 * Register a set of register updates to be applied to the device
2686 * whenever the device registers are synchronised with the cache and
2687 * apply them immediately. Typically this is used to apply
2688 * corrections to be applied to the device defaults on startup, such
2689 * as the updates some vendors provide to undocumented registers.
2691 * The caller must ensure that this function cannot be called
2692 * concurrently with either itself or regcache_sync().
2694 int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
2697 struct reg_default *p;
2701 if (WARN_ONCE(num_regs <= 0, "invalid registers number (%d)\n",
2705 p = krealloc(map->patch,
2706 sizeof(struct reg_default) * (map->patch_regs + num_regs),
2709 memcpy(p + map->patch_regs, regs, num_regs * sizeof(*regs));
2711 map->patch_regs += num_regs;
2716 map->lock(map->lock_arg);
2718 bypass = map->cache_bypass;
2720 map->cache_bypass = true;
2723 ret = _regmap_multi_reg_write(map, regs, num_regs);
2726 map->cache_bypass = bypass;
2728 map->unlock(map->lock_arg);
2730 regmap_async_complete(map);
2734 EXPORT_SYMBOL_GPL(regmap_register_patch);
2737 * regmap_get_val_bytes(): Report the size of a register value
2739 * Report the size of a register value, mainly intended to for use by
2740 * generic infrastructure built on top of regmap.
2742 int regmap_get_val_bytes(struct regmap *map)
2744 if (map->format.format_write)
2747 return map->format.val_bytes;
2749 EXPORT_SYMBOL_GPL(regmap_get_val_bytes);
2752 * regmap_get_max_register(): Report the max register value
2754 * Report the max register value, mainly intended to for use by
2755 * generic infrastructure built on top of regmap.
2757 int regmap_get_max_register(struct regmap *map)
2759 return map->max_register ? map->max_register : -EINVAL;
2761 EXPORT_SYMBOL_GPL(regmap_get_max_register);
2764 * regmap_get_reg_stride(): Report the register address stride
2766 * Report the register address stride, mainly intended to for use by
2767 * generic infrastructure built on top of regmap.
2769 int regmap_get_reg_stride(struct regmap *map)
2771 return map->reg_stride;
2773 EXPORT_SYMBOL_GPL(regmap_get_reg_stride);
2775 int regmap_parse_val(struct regmap *map, const void *buf,
2778 if (!map->format.parse_val)
2781 *val = map->format.parse_val(buf);
2785 EXPORT_SYMBOL_GPL(regmap_parse_val);
2787 static int __init regmap_initcall(void)
2789 regmap_debugfs_initcall();
2793 postcore_initcall(regmap_initcall);