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>
22 #include <linux/log2.h>
23 #include <linux/hwspinlock.h>
25 #define CREATE_TRACE_POINTS
31 * Sometimes for failures during very early init the trace
32 * infrastructure isn't available early enough to be used. For this
33 * sort of problem defining LOG_DEVICE will add printks for basic
34 * register I/O on a specific device.
38 static int _regmap_update_bits(struct regmap *map, unsigned int reg,
39 unsigned int mask, unsigned int val,
40 bool *change, bool force_write);
42 static int _regmap_bus_reg_read(void *context, unsigned int reg,
44 static int _regmap_bus_read(void *context, unsigned int reg,
46 static int _regmap_bus_formatted_write(void *context, unsigned int reg,
48 static int _regmap_bus_reg_write(void *context, unsigned int reg,
50 static int _regmap_bus_raw_write(void *context, unsigned int reg,
53 bool regmap_reg_in_ranges(unsigned int reg,
54 const struct regmap_range *ranges,
57 const struct regmap_range *r;
60 for (i = 0, r = ranges; i < nranges; i++, r++)
61 if (regmap_reg_in_range(reg, r))
65 EXPORT_SYMBOL_GPL(regmap_reg_in_ranges);
67 bool regmap_check_range_table(struct regmap *map, unsigned int reg,
68 const struct regmap_access_table *table)
70 /* Check "no ranges" first */
71 if (regmap_reg_in_ranges(reg, table->no_ranges, table->n_no_ranges))
74 /* In case zero "yes ranges" are supplied, any reg is OK */
75 if (!table->n_yes_ranges)
78 return regmap_reg_in_ranges(reg, table->yes_ranges,
81 EXPORT_SYMBOL_GPL(regmap_check_range_table);
83 bool regmap_writeable(struct regmap *map, unsigned int reg)
85 if (map->max_register && reg > map->max_register)
88 if (map->writeable_reg)
89 return map->writeable_reg(map->dev, reg);
92 return regmap_check_range_table(map, reg, map->wr_table);
97 bool regmap_cached(struct regmap *map, unsigned int reg)
102 if (map->cache == REGCACHE_NONE)
108 if (map->max_register && reg > map->max_register)
111 map->lock(map->lock_arg);
112 ret = regcache_read(map, reg, &val);
113 map->unlock(map->lock_arg);
120 bool regmap_readable(struct regmap *map, unsigned int reg)
125 if (map->max_register && reg > map->max_register)
128 if (map->format.format_write)
131 if (map->readable_reg)
132 return map->readable_reg(map->dev, reg);
135 return regmap_check_range_table(map, reg, map->rd_table);
140 bool regmap_volatile(struct regmap *map, unsigned int reg)
142 if (!map->format.format_write && !regmap_readable(map, reg))
145 if (map->volatile_reg)
146 return map->volatile_reg(map->dev, reg);
148 if (map->volatile_table)
149 return regmap_check_range_table(map, reg, map->volatile_table);
157 bool regmap_precious(struct regmap *map, unsigned int reg)
159 if (!regmap_readable(map, reg))
162 if (map->precious_reg)
163 return map->precious_reg(map->dev, reg);
165 if (map->precious_table)
166 return regmap_check_range_table(map, reg, map->precious_table);
171 static bool regmap_volatile_range(struct regmap *map, unsigned int reg,
176 for (i = 0; i < num; i++)
177 if (!regmap_volatile(map, reg + i))
183 static void regmap_format_2_6_write(struct regmap *map,
184 unsigned int reg, unsigned int val)
186 u8 *out = map->work_buf;
188 *out = (reg << 6) | val;
191 static void regmap_format_4_12_write(struct regmap *map,
192 unsigned int reg, unsigned int val)
194 __be16 *out = map->work_buf;
195 *out = cpu_to_be16((reg << 12) | val);
198 static void regmap_format_7_9_write(struct regmap *map,
199 unsigned int reg, unsigned int val)
201 __be16 *out = map->work_buf;
202 *out = cpu_to_be16((reg << 9) | val);
205 static void regmap_format_10_14_write(struct regmap *map,
206 unsigned int reg, unsigned int val)
208 u8 *out = map->work_buf;
211 out[1] = (val >> 8) | (reg << 6);
215 static void regmap_format_8(void *buf, unsigned int val, unsigned int shift)
222 static void regmap_format_16_be(void *buf, unsigned int val, unsigned int shift)
226 b[0] = cpu_to_be16(val << shift);
229 static void regmap_format_16_le(void *buf, unsigned int val, unsigned int shift)
233 b[0] = cpu_to_le16(val << shift);
236 static void regmap_format_16_native(void *buf, unsigned int val,
239 *(u16 *)buf = val << shift;
242 static void regmap_format_24(void *buf, unsigned int val, unsigned int shift)
253 static void regmap_format_32_be(void *buf, unsigned int val, unsigned int shift)
257 b[0] = cpu_to_be32(val << shift);
260 static void regmap_format_32_le(void *buf, unsigned int val, unsigned int shift)
264 b[0] = cpu_to_le32(val << shift);
267 static void regmap_format_32_native(void *buf, unsigned int val,
270 *(u32 *)buf = val << shift;
274 static void regmap_format_64_be(void *buf, unsigned int val, unsigned int shift)
278 b[0] = cpu_to_be64((u64)val << shift);
281 static void regmap_format_64_le(void *buf, unsigned int val, unsigned int shift)
285 b[0] = cpu_to_le64((u64)val << shift);
288 static void regmap_format_64_native(void *buf, unsigned int val,
291 *(u64 *)buf = (u64)val << shift;
295 static void regmap_parse_inplace_noop(void *buf)
299 static unsigned int regmap_parse_8(const void *buf)
306 static unsigned int regmap_parse_16_be(const void *buf)
308 const __be16 *b = buf;
310 return be16_to_cpu(b[0]);
313 static unsigned int regmap_parse_16_le(const void *buf)
315 const __le16 *b = buf;
317 return le16_to_cpu(b[0]);
320 static void regmap_parse_16_be_inplace(void *buf)
324 b[0] = be16_to_cpu(b[0]);
327 static void regmap_parse_16_le_inplace(void *buf)
331 b[0] = le16_to_cpu(b[0]);
334 static unsigned int regmap_parse_16_native(const void *buf)
339 static unsigned int regmap_parse_24(const void *buf)
342 unsigned int ret = b[2];
343 ret |= ((unsigned int)b[1]) << 8;
344 ret |= ((unsigned int)b[0]) << 16;
349 static unsigned int regmap_parse_32_be(const void *buf)
351 const __be32 *b = buf;
353 return be32_to_cpu(b[0]);
356 static unsigned int regmap_parse_32_le(const void *buf)
358 const __le32 *b = buf;
360 return le32_to_cpu(b[0]);
363 static void regmap_parse_32_be_inplace(void *buf)
367 b[0] = be32_to_cpu(b[0]);
370 static void regmap_parse_32_le_inplace(void *buf)
374 b[0] = le32_to_cpu(b[0]);
377 static unsigned int regmap_parse_32_native(const void *buf)
383 static unsigned int regmap_parse_64_be(const void *buf)
385 const __be64 *b = buf;
387 return be64_to_cpu(b[0]);
390 static unsigned int regmap_parse_64_le(const void *buf)
392 const __le64 *b = buf;
394 return le64_to_cpu(b[0]);
397 static void regmap_parse_64_be_inplace(void *buf)
401 b[0] = be64_to_cpu(b[0]);
404 static void regmap_parse_64_le_inplace(void *buf)
408 b[0] = le64_to_cpu(b[0]);
411 static unsigned int regmap_parse_64_native(const void *buf)
417 static void regmap_lock_hwlock(void *__map)
419 struct regmap *map = __map;
421 hwspin_lock_timeout(map->hwlock, UINT_MAX);
424 static void regmap_lock_hwlock_irq(void *__map)
426 struct regmap *map = __map;
428 hwspin_lock_timeout_irq(map->hwlock, UINT_MAX);
431 static void regmap_lock_hwlock_irqsave(void *__map)
433 struct regmap *map = __map;
435 hwspin_lock_timeout_irqsave(map->hwlock, UINT_MAX,
436 &map->spinlock_flags);
439 static void regmap_unlock_hwlock(void *__map)
441 struct regmap *map = __map;
443 hwspin_unlock(map->hwlock);
446 static void regmap_unlock_hwlock_irq(void *__map)
448 struct regmap *map = __map;
450 hwspin_unlock_irq(map->hwlock);
453 static void regmap_unlock_hwlock_irqrestore(void *__map)
455 struct regmap *map = __map;
457 hwspin_unlock_irqrestore(map->hwlock, &map->spinlock_flags);
460 static void regmap_lock_mutex(void *__map)
462 struct regmap *map = __map;
463 mutex_lock(&map->mutex);
466 static void regmap_unlock_mutex(void *__map)
468 struct regmap *map = __map;
469 mutex_unlock(&map->mutex);
472 static void regmap_lock_spinlock(void *__map)
473 __acquires(&map->spinlock)
475 struct regmap *map = __map;
478 spin_lock_irqsave(&map->spinlock, flags);
479 map->spinlock_flags = flags;
482 static void regmap_unlock_spinlock(void *__map)
483 __releases(&map->spinlock)
485 struct regmap *map = __map;
486 spin_unlock_irqrestore(&map->spinlock, map->spinlock_flags);
489 static void dev_get_regmap_release(struct device *dev, void *res)
492 * We don't actually have anything to do here; the goal here
493 * is not to manage the regmap but to provide a simple way to
494 * get the regmap back given a struct device.
498 static bool _regmap_range_add(struct regmap *map,
499 struct regmap_range_node *data)
501 struct rb_root *root = &map->range_tree;
502 struct rb_node **new = &(root->rb_node), *parent = NULL;
505 struct regmap_range_node *this =
506 rb_entry(*new, struct regmap_range_node, node);
509 if (data->range_max < this->range_min)
510 new = &((*new)->rb_left);
511 else if (data->range_min > this->range_max)
512 new = &((*new)->rb_right);
517 rb_link_node(&data->node, parent, new);
518 rb_insert_color(&data->node, root);
523 static struct regmap_range_node *_regmap_range_lookup(struct regmap *map,
526 struct rb_node *node = map->range_tree.rb_node;
529 struct regmap_range_node *this =
530 rb_entry(node, struct regmap_range_node, node);
532 if (reg < this->range_min)
533 node = node->rb_left;
534 else if (reg > this->range_max)
535 node = node->rb_right;
543 static void regmap_range_exit(struct regmap *map)
545 struct rb_node *next;
546 struct regmap_range_node *range_node;
548 next = rb_first(&map->range_tree);
550 range_node = rb_entry(next, struct regmap_range_node, node);
551 next = rb_next(&range_node->node);
552 rb_erase(&range_node->node, &map->range_tree);
556 kfree(map->selector_work_buf);
559 int regmap_attach_dev(struct device *dev, struct regmap *map,
560 const struct regmap_config *config)
566 regmap_debugfs_init(map, config->name);
568 /* Add a devres resource for dev_get_regmap() */
569 m = devres_alloc(dev_get_regmap_release, sizeof(*m), GFP_KERNEL);
571 regmap_debugfs_exit(map);
579 EXPORT_SYMBOL_GPL(regmap_attach_dev);
581 static enum regmap_endian regmap_get_reg_endian(const struct regmap_bus *bus,
582 const struct regmap_config *config)
584 enum regmap_endian endian;
586 /* Retrieve the endianness specification from the regmap config */
587 endian = config->reg_format_endian;
589 /* If the regmap config specified a non-default value, use that */
590 if (endian != REGMAP_ENDIAN_DEFAULT)
593 /* Retrieve the endianness specification from the bus config */
594 if (bus && bus->reg_format_endian_default)
595 endian = bus->reg_format_endian_default;
597 /* If the bus specified a non-default value, use that */
598 if (endian != REGMAP_ENDIAN_DEFAULT)
601 /* Use this if no other value was found */
602 return REGMAP_ENDIAN_BIG;
605 enum regmap_endian regmap_get_val_endian(struct device *dev,
606 const struct regmap_bus *bus,
607 const struct regmap_config *config)
609 struct device_node *np;
610 enum regmap_endian endian;
612 /* Retrieve the endianness specification from the regmap config */
613 endian = config->val_format_endian;
615 /* If the regmap config specified a non-default value, use that */
616 if (endian != REGMAP_ENDIAN_DEFAULT)
619 /* If the dev and dev->of_node exist try to get endianness from DT */
620 if (dev && dev->of_node) {
623 /* Parse the device's DT node for an endianness specification */
624 if (of_property_read_bool(np, "big-endian"))
625 endian = REGMAP_ENDIAN_BIG;
626 else if (of_property_read_bool(np, "little-endian"))
627 endian = REGMAP_ENDIAN_LITTLE;
628 else if (of_property_read_bool(np, "native-endian"))
629 endian = REGMAP_ENDIAN_NATIVE;
631 /* If the endianness was specified in DT, use that */
632 if (endian != REGMAP_ENDIAN_DEFAULT)
636 /* Retrieve the endianness specification from the bus config */
637 if (bus && bus->val_format_endian_default)
638 endian = bus->val_format_endian_default;
640 /* If the bus specified a non-default value, use that */
641 if (endian != REGMAP_ENDIAN_DEFAULT)
644 /* Use this if no other value was found */
645 return REGMAP_ENDIAN_BIG;
647 EXPORT_SYMBOL_GPL(regmap_get_val_endian);
649 struct regmap *__regmap_init(struct device *dev,
650 const struct regmap_bus *bus,
652 const struct regmap_config *config,
653 struct lock_class_key *lock_key,
654 const char *lock_name)
658 enum regmap_endian reg_endian, val_endian;
664 map = kzalloc(sizeof(*map), GFP_KERNEL);
670 if (config->lock && config->unlock) {
671 map->lock = config->lock;
672 map->unlock = config->unlock;
673 map->lock_arg = config->lock_arg;
674 } else if (config->hwlock_id) {
675 map->hwlock = hwspin_lock_request_specific(config->hwlock_id);
681 switch (config->hwlock_mode) {
682 case HWLOCK_IRQSTATE:
683 map->lock = regmap_lock_hwlock_irqsave;
684 map->unlock = regmap_unlock_hwlock_irqrestore;
687 map->lock = regmap_lock_hwlock_irq;
688 map->unlock = regmap_unlock_hwlock_irq;
691 map->lock = regmap_lock_hwlock;
692 map->unlock = regmap_unlock_hwlock;
698 if ((bus && bus->fast_io) ||
700 spin_lock_init(&map->spinlock);
701 map->lock = regmap_lock_spinlock;
702 map->unlock = regmap_unlock_spinlock;
703 lockdep_set_class_and_name(&map->spinlock,
704 lock_key, lock_name);
706 mutex_init(&map->mutex);
707 map->lock = regmap_lock_mutex;
708 map->unlock = regmap_unlock_mutex;
709 lockdep_set_class_and_name(&map->mutex,
710 lock_key, lock_name);
716 * When we write in fast-paths with regmap_bulk_write() don't allocate
717 * scratch buffers with sleeping allocations.
719 if ((bus && bus->fast_io) || config->fast_io)
720 map->alloc_flags = GFP_ATOMIC;
722 map->alloc_flags = GFP_KERNEL;
724 map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8);
725 map->format.pad_bytes = config->pad_bits / 8;
726 map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8);
727 map->format.buf_size = DIV_ROUND_UP(config->reg_bits +
728 config->val_bits + config->pad_bits, 8);
729 map->reg_shift = config->pad_bits % 8;
730 if (config->reg_stride)
731 map->reg_stride = config->reg_stride;
734 if (is_power_of_2(map->reg_stride))
735 map->reg_stride_order = ilog2(map->reg_stride);
737 map->reg_stride_order = -1;
738 map->use_single_read = config->use_single_rw || !bus || !bus->read;
739 map->use_single_write = config->use_single_rw || !bus || !bus->write;
740 map->can_multi_write = config->can_multi_write && bus && bus->write;
742 map->max_raw_read = bus->max_raw_read;
743 map->max_raw_write = bus->max_raw_write;
747 map->bus_context = bus_context;
748 map->max_register = config->max_register;
749 map->wr_table = config->wr_table;
750 map->rd_table = config->rd_table;
751 map->volatile_table = config->volatile_table;
752 map->precious_table = config->precious_table;
753 map->writeable_reg = config->writeable_reg;
754 map->readable_reg = config->readable_reg;
755 map->volatile_reg = config->volatile_reg;
756 map->precious_reg = config->precious_reg;
757 map->cache_type = config->cache_type;
758 map->name = config->name;
760 spin_lock_init(&map->async_lock);
761 INIT_LIST_HEAD(&map->async_list);
762 INIT_LIST_HEAD(&map->async_free);
763 init_waitqueue_head(&map->async_waitq);
765 if (config->read_flag_mask || config->write_flag_mask) {
766 map->read_flag_mask = config->read_flag_mask;
767 map->write_flag_mask = config->write_flag_mask;
769 map->read_flag_mask = bus->read_flag_mask;
773 map->reg_read = config->reg_read;
774 map->reg_write = config->reg_write;
776 map->defer_caching = false;
777 goto skip_format_initialization;
778 } else if (!bus->read || !bus->write) {
779 map->reg_read = _regmap_bus_reg_read;
780 map->reg_write = _regmap_bus_reg_write;
782 map->defer_caching = false;
783 goto skip_format_initialization;
785 map->reg_read = _regmap_bus_read;
786 map->reg_update_bits = bus->reg_update_bits;
789 reg_endian = regmap_get_reg_endian(bus, config);
790 val_endian = regmap_get_val_endian(dev, bus, config);
792 switch (config->reg_bits + map->reg_shift) {
794 switch (config->val_bits) {
796 map->format.format_write = regmap_format_2_6_write;
804 switch (config->val_bits) {
806 map->format.format_write = regmap_format_4_12_write;
814 switch (config->val_bits) {
816 map->format.format_write = regmap_format_7_9_write;
824 switch (config->val_bits) {
826 map->format.format_write = regmap_format_10_14_write;
834 map->format.format_reg = regmap_format_8;
838 switch (reg_endian) {
839 case REGMAP_ENDIAN_BIG:
840 map->format.format_reg = regmap_format_16_be;
842 case REGMAP_ENDIAN_LITTLE:
843 map->format.format_reg = regmap_format_16_le;
845 case REGMAP_ENDIAN_NATIVE:
846 map->format.format_reg = regmap_format_16_native;
854 if (reg_endian != REGMAP_ENDIAN_BIG)
856 map->format.format_reg = regmap_format_24;
860 switch (reg_endian) {
861 case REGMAP_ENDIAN_BIG:
862 map->format.format_reg = regmap_format_32_be;
864 case REGMAP_ENDIAN_LITTLE:
865 map->format.format_reg = regmap_format_32_le;
867 case REGMAP_ENDIAN_NATIVE:
868 map->format.format_reg = regmap_format_32_native;
877 switch (reg_endian) {
878 case REGMAP_ENDIAN_BIG:
879 map->format.format_reg = regmap_format_64_be;
881 case REGMAP_ENDIAN_LITTLE:
882 map->format.format_reg = regmap_format_64_le;
884 case REGMAP_ENDIAN_NATIVE:
885 map->format.format_reg = regmap_format_64_native;
897 if (val_endian == REGMAP_ENDIAN_NATIVE)
898 map->format.parse_inplace = regmap_parse_inplace_noop;
900 switch (config->val_bits) {
902 map->format.format_val = regmap_format_8;
903 map->format.parse_val = regmap_parse_8;
904 map->format.parse_inplace = regmap_parse_inplace_noop;
907 switch (val_endian) {
908 case REGMAP_ENDIAN_BIG:
909 map->format.format_val = regmap_format_16_be;
910 map->format.parse_val = regmap_parse_16_be;
911 map->format.parse_inplace = regmap_parse_16_be_inplace;
913 case REGMAP_ENDIAN_LITTLE:
914 map->format.format_val = regmap_format_16_le;
915 map->format.parse_val = regmap_parse_16_le;
916 map->format.parse_inplace = regmap_parse_16_le_inplace;
918 case REGMAP_ENDIAN_NATIVE:
919 map->format.format_val = regmap_format_16_native;
920 map->format.parse_val = regmap_parse_16_native;
927 if (val_endian != REGMAP_ENDIAN_BIG)
929 map->format.format_val = regmap_format_24;
930 map->format.parse_val = regmap_parse_24;
933 switch (val_endian) {
934 case REGMAP_ENDIAN_BIG:
935 map->format.format_val = regmap_format_32_be;
936 map->format.parse_val = regmap_parse_32_be;
937 map->format.parse_inplace = regmap_parse_32_be_inplace;
939 case REGMAP_ENDIAN_LITTLE:
940 map->format.format_val = regmap_format_32_le;
941 map->format.parse_val = regmap_parse_32_le;
942 map->format.parse_inplace = regmap_parse_32_le_inplace;
944 case REGMAP_ENDIAN_NATIVE:
945 map->format.format_val = regmap_format_32_native;
946 map->format.parse_val = regmap_parse_32_native;
954 switch (val_endian) {
955 case REGMAP_ENDIAN_BIG:
956 map->format.format_val = regmap_format_64_be;
957 map->format.parse_val = regmap_parse_64_be;
958 map->format.parse_inplace = regmap_parse_64_be_inplace;
960 case REGMAP_ENDIAN_LITTLE:
961 map->format.format_val = regmap_format_64_le;
962 map->format.parse_val = regmap_parse_64_le;
963 map->format.parse_inplace = regmap_parse_64_le_inplace;
965 case REGMAP_ENDIAN_NATIVE:
966 map->format.format_val = regmap_format_64_native;
967 map->format.parse_val = regmap_parse_64_native;
976 if (map->format.format_write) {
977 if ((reg_endian != REGMAP_ENDIAN_BIG) ||
978 (val_endian != REGMAP_ENDIAN_BIG))
980 map->use_single_write = true;
983 if (!map->format.format_write &&
984 !(map->format.format_reg && map->format.format_val))
987 map->work_buf = kzalloc(map->format.buf_size, GFP_KERNEL);
988 if (map->work_buf == NULL) {
993 if (map->format.format_write) {
994 map->defer_caching = false;
995 map->reg_write = _regmap_bus_formatted_write;
996 } else if (map->format.format_val) {
997 map->defer_caching = true;
998 map->reg_write = _regmap_bus_raw_write;
1001 skip_format_initialization:
1003 map->range_tree = RB_ROOT;
1004 for (i = 0; i < config->num_ranges; i++) {
1005 const struct regmap_range_cfg *range_cfg = &config->ranges[i];
1006 struct regmap_range_node *new;
1009 if (range_cfg->range_max < range_cfg->range_min) {
1010 dev_err(map->dev, "Invalid range %d: %d < %d\n", i,
1011 range_cfg->range_max, range_cfg->range_min);
1015 if (range_cfg->range_max > map->max_register) {
1016 dev_err(map->dev, "Invalid range %d: %d > %d\n", i,
1017 range_cfg->range_max, map->max_register);
1021 if (range_cfg->selector_reg > map->max_register) {
1023 "Invalid range %d: selector out of map\n", i);
1027 if (range_cfg->window_len == 0) {
1028 dev_err(map->dev, "Invalid range %d: window_len 0\n",
1033 /* Make sure, that this register range has no selector
1034 or data window within its boundary */
1035 for (j = 0; j < config->num_ranges; j++) {
1036 unsigned sel_reg = config->ranges[j].selector_reg;
1037 unsigned win_min = config->ranges[j].window_start;
1038 unsigned win_max = win_min +
1039 config->ranges[j].window_len - 1;
1041 /* Allow data window inside its own virtual range */
1045 if (range_cfg->range_min <= sel_reg &&
1046 sel_reg <= range_cfg->range_max) {
1048 "Range %d: selector for %d in window\n",
1053 if (!(win_max < range_cfg->range_min ||
1054 win_min > range_cfg->range_max)) {
1056 "Range %d: window for %d in window\n",
1062 new = kzalloc(sizeof(*new), GFP_KERNEL);
1069 new->name = range_cfg->name;
1070 new->range_min = range_cfg->range_min;
1071 new->range_max = range_cfg->range_max;
1072 new->selector_reg = range_cfg->selector_reg;
1073 new->selector_mask = range_cfg->selector_mask;
1074 new->selector_shift = range_cfg->selector_shift;
1075 new->window_start = range_cfg->window_start;
1076 new->window_len = range_cfg->window_len;
1078 if (!_regmap_range_add(map, new)) {
1079 dev_err(map->dev, "Failed to add range %d\n", i);
1084 if (map->selector_work_buf == NULL) {
1085 map->selector_work_buf =
1086 kzalloc(map->format.buf_size, GFP_KERNEL);
1087 if (map->selector_work_buf == NULL) {
1094 ret = regcache_init(map, config);
1099 ret = regmap_attach_dev(dev, map, config);
1109 regmap_range_exit(map);
1110 kfree(map->work_buf);
1112 hwspin_lock_free(map->hwlock);
1116 return ERR_PTR(ret);
1118 EXPORT_SYMBOL_GPL(__regmap_init);
1120 static void devm_regmap_release(struct device *dev, void *res)
1122 regmap_exit(*(struct regmap **)res);
1125 struct regmap *__devm_regmap_init(struct device *dev,
1126 const struct regmap_bus *bus,
1128 const struct regmap_config *config,
1129 struct lock_class_key *lock_key,
1130 const char *lock_name)
1132 struct regmap **ptr, *regmap;
1134 ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL);
1136 return ERR_PTR(-ENOMEM);
1138 regmap = __regmap_init(dev, bus, bus_context, config,
1139 lock_key, lock_name);
1140 if (!IS_ERR(regmap)) {
1142 devres_add(dev, ptr);
1149 EXPORT_SYMBOL_GPL(__devm_regmap_init);
1151 static void regmap_field_init(struct regmap_field *rm_field,
1152 struct regmap *regmap, struct reg_field reg_field)
1154 rm_field->regmap = regmap;
1155 rm_field->reg = reg_field.reg;
1156 rm_field->shift = reg_field.lsb;
1157 rm_field->mask = GENMASK(reg_field.msb, reg_field.lsb);
1158 rm_field->id_size = reg_field.id_size;
1159 rm_field->id_offset = reg_field.id_offset;
1163 * devm_regmap_field_alloc() - Allocate and initialise a register field.
1165 * @dev: Device that will be interacted with
1166 * @regmap: regmap bank in which this register field is located.
1167 * @reg_field: Register field with in the bank.
1169 * The return value will be an ERR_PTR() on error or a valid pointer
1170 * to a struct regmap_field. The regmap_field will be automatically freed
1171 * by the device management code.
1173 struct regmap_field *devm_regmap_field_alloc(struct device *dev,
1174 struct regmap *regmap, struct reg_field reg_field)
1176 struct regmap_field *rm_field = devm_kzalloc(dev,
1177 sizeof(*rm_field), GFP_KERNEL);
1179 return ERR_PTR(-ENOMEM);
1181 regmap_field_init(rm_field, regmap, reg_field);
1186 EXPORT_SYMBOL_GPL(devm_regmap_field_alloc);
1189 * devm_regmap_field_free() - Free a register field allocated using
1190 * devm_regmap_field_alloc.
1192 * @dev: Device that will be interacted with
1193 * @field: regmap field which should be freed.
1195 * Free register field allocated using devm_regmap_field_alloc(). Usually
1196 * drivers need not call this function, as the memory allocated via devm
1197 * will be freed as per device-driver life-cyle.
1199 void devm_regmap_field_free(struct device *dev,
1200 struct regmap_field *field)
1202 devm_kfree(dev, field);
1204 EXPORT_SYMBOL_GPL(devm_regmap_field_free);
1207 * regmap_field_alloc() - Allocate and initialise a register field.
1209 * @regmap: regmap bank in which this register field is located.
1210 * @reg_field: Register field with in the bank.
1212 * The return value will be an ERR_PTR() on error or a valid pointer
1213 * to a struct regmap_field. The regmap_field should be freed by the
1214 * user once its finished working with it using regmap_field_free().
1216 struct regmap_field *regmap_field_alloc(struct regmap *regmap,
1217 struct reg_field reg_field)
1219 struct regmap_field *rm_field = kzalloc(sizeof(*rm_field), GFP_KERNEL);
1222 return ERR_PTR(-ENOMEM);
1224 regmap_field_init(rm_field, regmap, reg_field);
1228 EXPORT_SYMBOL_GPL(regmap_field_alloc);
1231 * regmap_field_free() - Free register field allocated using
1232 * regmap_field_alloc.
1234 * @field: regmap field which should be freed.
1236 void regmap_field_free(struct regmap_field *field)
1240 EXPORT_SYMBOL_GPL(regmap_field_free);
1243 * regmap_reinit_cache() - Reinitialise the current register cache
1245 * @map: Register map to operate on.
1246 * @config: New configuration. Only the cache data will be used.
1248 * Discard any existing register cache for the map and initialize a
1249 * new cache. This can be used to restore the cache to defaults or to
1250 * update the cache configuration to reflect runtime discovery of the
1253 * No explicit locking is done here, the user needs to ensure that
1254 * this function will not race with other calls to regmap.
1256 int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
1259 regmap_debugfs_exit(map);
1261 map->max_register = config->max_register;
1262 map->writeable_reg = config->writeable_reg;
1263 map->readable_reg = config->readable_reg;
1264 map->volatile_reg = config->volatile_reg;
1265 map->precious_reg = config->precious_reg;
1266 map->cache_type = config->cache_type;
1268 regmap_debugfs_init(map, config->name);
1270 map->cache_bypass = false;
1271 map->cache_only = false;
1273 return regcache_init(map, config);
1275 EXPORT_SYMBOL_GPL(regmap_reinit_cache);
1278 * regmap_exit() - Free a previously allocated register map
1280 * @map: Register map to operate on.
1282 void regmap_exit(struct regmap *map)
1284 struct regmap_async *async;
1287 regmap_debugfs_exit(map);
1288 regmap_range_exit(map);
1289 if (map->bus && map->bus->free_context)
1290 map->bus->free_context(map->bus_context);
1291 kfree(map->work_buf);
1292 while (!list_empty(&map->async_free)) {
1293 async = list_first_entry_or_null(&map->async_free,
1294 struct regmap_async,
1296 list_del(&async->list);
1297 kfree(async->work_buf);
1302 EXPORT_SYMBOL_GPL(regmap_exit);
1304 static int dev_get_regmap_match(struct device *dev, void *res, void *data)
1306 struct regmap **r = res;
1312 /* If the user didn't specify a name match any */
1314 return (*r)->name == data;
1320 * dev_get_regmap() - Obtain the regmap (if any) for a device
1322 * @dev: Device to retrieve the map for
1323 * @name: Optional name for the register map, usually NULL.
1325 * Returns the regmap for the device if one is present, or NULL. If
1326 * name is specified then it must match the name specified when
1327 * registering the device, if it is NULL then the first regmap found
1328 * will be used. Devices with multiple register maps are very rare,
1329 * generic code should normally not need to specify a name.
1331 struct regmap *dev_get_regmap(struct device *dev, const char *name)
1333 struct regmap **r = devres_find(dev, dev_get_regmap_release,
1334 dev_get_regmap_match, (void *)name);
1340 EXPORT_SYMBOL_GPL(dev_get_regmap);
1343 * regmap_get_device() - Obtain the device from a regmap
1345 * @map: Register map to operate on.
1347 * Returns the underlying device that the regmap has been created for.
1349 struct device *regmap_get_device(struct regmap *map)
1353 EXPORT_SYMBOL_GPL(regmap_get_device);
1355 static int _regmap_select_page(struct regmap *map, unsigned int *reg,
1356 struct regmap_range_node *range,
1357 unsigned int val_num)
1359 void *orig_work_buf;
1360 unsigned int win_offset;
1361 unsigned int win_page;
1365 win_offset = (*reg - range->range_min) % range->window_len;
1366 win_page = (*reg - range->range_min) / range->window_len;
1369 /* Bulk write shouldn't cross range boundary */
1370 if (*reg + val_num - 1 > range->range_max)
1373 /* ... or single page boundary */
1374 if (val_num > range->window_len - win_offset)
1378 /* It is possible to have selector register inside data window.
1379 In that case, selector register is located on every page and
1380 it needs no page switching, when accessed alone. */
1382 range->window_start + win_offset != range->selector_reg) {
1383 /* Use separate work_buf during page switching */
1384 orig_work_buf = map->work_buf;
1385 map->work_buf = map->selector_work_buf;
1387 ret = _regmap_update_bits(map, range->selector_reg,
1388 range->selector_mask,
1389 win_page << range->selector_shift,
1392 map->work_buf = orig_work_buf;
1398 *reg = range->window_start + win_offset;
1403 static void regmap_set_work_buf_flag_mask(struct regmap *map, int max_bytes,
1409 if (!mask || !map->work_buf)
1412 buf = map->work_buf;
1414 for (i = 0; i < max_bytes; i++)
1415 buf[i] |= (mask >> (8 * i)) & 0xff;
1418 int _regmap_raw_write(struct regmap *map, unsigned int reg,
1419 const void *val, size_t val_len)
1421 struct regmap_range_node *range;
1422 unsigned long flags;
1423 void *work_val = map->work_buf + map->format.reg_bytes +
1424 map->format.pad_bytes;
1426 int ret = -ENOTSUPP;
1432 /* Check for unwritable registers before we start */
1433 if (map->writeable_reg)
1434 for (i = 0; i < val_len / map->format.val_bytes; i++)
1435 if (!map->writeable_reg(map->dev,
1436 reg + regmap_get_offset(map, i)))
1439 if (!map->cache_bypass && map->format.parse_val) {
1441 int val_bytes = map->format.val_bytes;
1442 for (i = 0; i < val_len / val_bytes; i++) {
1443 ival = map->format.parse_val(val + (i * val_bytes));
1444 ret = regcache_write(map,
1445 reg + regmap_get_offset(map, i),
1449 "Error in caching of register: %x ret: %d\n",
1454 if (map->cache_only) {
1455 map->cache_dirty = true;
1460 range = _regmap_range_lookup(map, reg);
1462 int val_num = val_len / map->format.val_bytes;
1463 int win_offset = (reg - range->range_min) % range->window_len;
1464 int win_residue = range->window_len - win_offset;
1466 /* If the write goes beyond the end of the window split it */
1467 while (val_num > win_residue) {
1468 dev_dbg(map->dev, "Writing window %d/%zu\n",
1469 win_residue, val_len / map->format.val_bytes);
1470 ret = _regmap_raw_write(map, reg, val, win_residue *
1471 map->format.val_bytes);
1476 val_num -= win_residue;
1477 val += win_residue * map->format.val_bytes;
1478 val_len -= win_residue * map->format.val_bytes;
1480 win_offset = (reg - range->range_min) %
1482 win_residue = range->window_len - win_offset;
1485 ret = _regmap_select_page(map, ®, range, val_num);
1490 map->format.format_reg(map->work_buf, reg, map->reg_shift);
1491 regmap_set_work_buf_flag_mask(map, map->format.reg_bytes,
1492 map->write_flag_mask);
1495 * Essentially all I/O mechanisms will be faster with a single
1496 * buffer to write. Since register syncs often generate raw
1497 * writes of single registers optimise that case.
1499 if (val != work_val && val_len == map->format.val_bytes) {
1500 memcpy(work_val, val, map->format.val_bytes);
1504 if (map->async && map->bus->async_write) {
1505 struct regmap_async *async;
1507 trace_regmap_async_write_start(map, reg, val_len);
1509 spin_lock_irqsave(&map->async_lock, flags);
1510 async = list_first_entry_or_null(&map->async_free,
1511 struct regmap_async,
1514 list_del(&async->list);
1515 spin_unlock_irqrestore(&map->async_lock, flags);
1518 async = map->bus->async_alloc();
1522 async->work_buf = kzalloc(map->format.buf_size,
1523 GFP_KERNEL | GFP_DMA);
1524 if (!async->work_buf) {
1532 /* If the caller supplied the value we can use it safely. */
1533 memcpy(async->work_buf, map->work_buf, map->format.pad_bytes +
1534 map->format.reg_bytes + map->format.val_bytes);
1536 spin_lock_irqsave(&map->async_lock, flags);
1537 list_add_tail(&async->list, &map->async_list);
1538 spin_unlock_irqrestore(&map->async_lock, flags);
1540 if (val != work_val)
1541 ret = map->bus->async_write(map->bus_context,
1543 map->format.reg_bytes +
1544 map->format.pad_bytes,
1545 val, val_len, async);
1547 ret = map->bus->async_write(map->bus_context,
1549 map->format.reg_bytes +
1550 map->format.pad_bytes +
1551 val_len, NULL, 0, async);
1554 dev_err(map->dev, "Failed to schedule write: %d\n",
1557 spin_lock_irqsave(&map->async_lock, flags);
1558 list_move(&async->list, &map->async_free);
1559 spin_unlock_irqrestore(&map->async_lock, flags);
1565 trace_regmap_hw_write_start(map, reg, val_len / map->format.val_bytes);
1567 /* If we're doing a single register write we can probably just
1568 * send the work_buf directly, otherwise try to do a gather
1571 if (val == work_val)
1572 ret = map->bus->write(map->bus_context, map->work_buf,
1573 map->format.reg_bytes +
1574 map->format.pad_bytes +
1576 else if (map->bus->gather_write)
1577 ret = map->bus->gather_write(map->bus_context, map->work_buf,
1578 map->format.reg_bytes +
1579 map->format.pad_bytes,
1582 /* If that didn't work fall back on linearising by hand. */
1583 if (ret == -ENOTSUPP) {
1584 len = map->format.reg_bytes + map->format.pad_bytes + val_len;
1585 buf = kzalloc(len, GFP_KERNEL);
1589 memcpy(buf, map->work_buf, map->format.reg_bytes);
1590 memcpy(buf + map->format.reg_bytes + map->format.pad_bytes,
1592 ret = map->bus->write(map->bus_context, buf, len);
1595 } else if (ret != 0 && !map->cache_bypass && map->format.parse_val) {
1596 /* regcache_drop_region() takes lock that we already have,
1597 * thus call map->cache_ops->drop() directly
1599 if (map->cache_ops && map->cache_ops->drop)
1600 map->cache_ops->drop(map, reg, reg + 1);
1603 trace_regmap_hw_write_done(map, reg, val_len / map->format.val_bytes);
1609 * regmap_can_raw_write - Test if regmap_raw_write() is supported
1611 * @map: Map to check.
1613 bool regmap_can_raw_write(struct regmap *map)
1615 return map->bus && map->bus->write && map->format.format_val &&
1616 map->format.format_reg;
1618 EXPORT_SYMBOL_GPL(regmap_can_raw_write);
1621 * regmap_get_raw_read_max - Get the maximum size we can read
1623 * @map: Map to check.
1625 size_t regmap_get_raw_read_max(struct regmap *map)
1627 return map->max_raw_read;
1629 EXPORT_SYMBOL_GPL(regmap_get_raw_read_max);
1632 * regmap_get_raw_write_max - Get the maximum size we can read
1634 * @map: Map to check.
1636 size_t regmap_get_raw_write_max(struct regmap *map)
1638 return map->max_raw_write;
1640 EXPORT_SYMBOL_GPL(regmap_get_raw_write_max);
1642 static int _regmap_bus_formatted_write(void *context, unsigned int reg,
1646 struct regmap_range_node *range;
1647 struct regmap *map = context;
1649 WARN_ON(!map->bus || !map->format.format_write);
1651 range = _regmap_range_lookup(map, reg);
1653 ret = _regmap_select_page(map, ®, range, 1);
1658 map->format.format_write(map, reg, val);
1660 trace_regmap_hw_write_start(map, reg, 1);
1662 ret = map->bus->write(map->bus_context, map->work_buf,
1663 map->format.buf_size);
1665 trace_regmap_hw_write_done(map, reg, 1);
1670 static int _regmap_bus_reg_write(void *context, unsigned int reg,
1673 struct regmap *map = context;
1675 return map->bus->reg_write(map->bus_context, reg, val);
1678 static int _regmap_bus_raw_write(void *context, unsigned int reg,
1681 struct regmap *map = context;
1683 WARN_ON(!map->bus || !map->format.format_val);
1685 map->format.format_val(map->work_buf + map->format.reg_bytes
1686 + map->format.pad_bytes, val, 0);
1687 return _regmap_raw_write(map, reg,
1689 map->format.reg_bytes +
1690 map->format.pad_bytes,
1691 map->format.val_bytes);
1694 static inline void *_regmap_map_get_context(struct regmap *map)
1696 return (map->bus) ? map : map->bus_context;
1699 int _regmap_write(struct regmap *map, unsigned int reg,
1703 void *context = _regmap_map_get_context(map);
1705 if (!regmap_writeable(map, reg))
1708 if (!map->cache_bypass && !map->defer_caching) {
1709 ret = regcache_write(map, reg, val);
1712 if (map->cache_only) {
1713 map->cache_dirty = true;
1719 if (map->dev && strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
1720 dev_info(map->dev, "%x <= %x\n", reg, val);
1723 trace_regmap_reg_write(map, reg, val);
1725 return map->reg_write(context, reg, val);
1729 * regmap_write() - Write a value to a single register
1731 * @map: Register map to write to
1732 * @reg: Register to write to
1733 * @val: Value to be written
1735 * A value of zero will be returned on success, a negative errno will
1736 * be returned in error cases.
1738 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
1742 if (!IS_ALIGNED(reg, map->reg_stride))
1745 map->lock(map->lock_arg);
1747 ret = _regmap_write(map, reg, val);
1749 map->unlock(map->lock_arg);
1753 EXPORT_SYMBOL_GPL(regmap_write);
1756 * regmap_write_async() - Write a value to a single register asynchronously
1758 * @map: Register map to write to
1759 * @reg: Register to write to
1760 * @val: Value to be written
1762 * A value of zero will be returned on success, a negative errno will
1763 * be returned in error cases.
1765 int regmap_write_async(struct regmap *map, unsigned int reg, unsigned int val)
1769 if (!IS_ALIGNED(reg, map->reg_stride))
1772 map->lock(map->lock_arg);
1776 ret = _regmap_write(map, reg, val);
1780 map->unlock(map->lock_arg);
1784 EXPORT_SYMBOL_GPL(regmap_write_async);
1787 * regmap_raw_write() - Write raw values to one or more registers
1789 * @map: Register map to write to
1790 * @reg: Initial register to write to
1791 * @val: Block of data to be written, laid out for direct transmission to the
1793 * @val_len: Length of data pointed to by val.
1795 * This function is intended to be used for things like firmware
1796 * download where a large block of data needs to be transferred to the
1797 * device. No formatting will be done on the data provided.
1799 * A value of zero will be returned on success, a negative errno will
1800 * be returned in error cases.
1802 int regmap_raw_write(struct regmap *map, unsigned int reg,
1803 const void *val, size_t val_len)
1807 if (!regmap_can_raw_write(map))
1809 if (val_len % map->format.val_bytes)
1811 if (map->max_raw_write && map->max_raw_write > val_len)
1814 map->lock(map->lock_arg);
1816 ret = _regmap_raw_write(map, reg, val, val_len);
1818 map->unlock(map->lock_arg);
1822 EXPORT_SYMBOL_GPL(regmap_raw_write);
1825 * regmap_field_update_bits_base() - Perform a read/modify/write cycle a
1828 * @field: Register field to write to
1829 * @mask: Bitmask to change
1830 * @val: Value to be written
1831 * @change: Boolean indicating if a write was done
1832 * @async: Boolean indicating asynchronously
1833 * @force: Boolean indicating use force update
1835 * Perform a read/modify/write cycle on the register field with change,
1836 * async, force option.
1838 * A value of zero will be returned on success, a negative errno will
1839 * be returned in error cases.
1841 int regmap_field_update_bits_base(struct regmap_field *field,
1842 unsigned int mask, unsigned int val,
1843 bool *change, bool async, bool force)
1845 mask = (mask << field->shift) & field->mask;
1847 return regmap_update_bits_base(field->regmap, field->reg,
1848 mask, val << field->shift,
1849 change, async, force);
1851 EXPORT_SYMBOL_GPL(regmap_field_update_bits_base);
1854 * regmap_fields_update_bits_base() - Perform a read/modify/write cycle a
1855 * register field with port ID
1857 * @field: Register field to write to
1859 * @mask: Bitmask to change
1860 * @val: Value to be written
1861 * @change: Boolean indicating if a write was done
1862 * @async: Boolean indicating asynchronously
1863 * @force: Boolean indicating use force update
1865 * A value of zero will be returned on success, a negative errno will
1866 * be returned in error cases.
1868 int regmap_fields_update_bits_base(struct regmap_field *field, unsigned int id,
1869 unsigned int mask, unsigned int val,
1870 bool *change, bool async, bool force)
1872 if (id >= field->id_size)
1875 mask = (mask << field->shift) & field->mask;
1877 return regmap_update_bits_base(field->regmap,
1878 field->reg + (field->id_offset * id),
1879 mask, val << field->shift,
1880 change, async, force);
1882 EXPORT_SYMBOL_GPL(regmap_fields_update_bits_base);
1885 * regmap_bulk_write() - Write multiple registers to the device
1887 * @map: Register map to write to
1888 * @reg: First register to be write from
1889 * @val: Block of data to be written, in native register size for device
1890 * @val_count: Number of registers to write
1892 * This function is intended to be used for writing a large block of
1893 * data to the device either in single transfer or multiple transfer.
1895 * A value of zero will be returned on success, a negative errno will
1896 * be returned in error cases.
1898 int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
1902 size_t val_bytes = map->format.val_bytes;
1903 size_t total_size = val_bytes * val_count;
1905 if (!IS_ALIGNED(reg, map->reg_stride))
1909 * Some devices don't support bulk write, for
1910 * them we have a series of single write operations in the first two if
1913 * The first if block is used for memory mapped io. It does not allow
1914 * val_bytes of 3 for example.
1915 * The second one is for busses that do not provide raw I/O.
1916 * The third one is used for busses which do not have these limitations
1917 * and can write arbitrary value lengths.
1920 map->lock(map->lock_arg);
1921 for (i = 0; i < val_count; i++) {
1924 switch (val_bytes) {
1926 ival = *(u8 *)(val + (i * val_bytes));
1929 ival = *(u16 *)(val + (i * val_bytes));
1932 ival = *(u32 *)(val + (i * val_bytes));
1936 ival = *(u64 *)(val + (i * val_bytes));
1944 ret = _regmap_write(map,
1945 reg + regmap_get_offset(map, i),
1951 map->unlock(map->lock_arg);
1952 } else if (map->bus && !map->format.parse_inplace) {
1954 const u16 *u16 = val;
1955 const u32 *u32 = val;
1958 for (i = 0; i < val_count; i++) {
1959 switch (map->format.val_bytes) {
1973 ret = regmap_write(map, reg + (i * map->reg_stride),
1978 } else if (map->use_single_write ||
1979 (map->max_raw_write && map->max_raw_write < total_size)) {
1980 int chunk_stride = map->reg_stride;
1981 size_t chunk_size = val_bytes;
1982 size_t chunk_count = val_count;
1984 if (!map->use_single_write) {
1985 chunk_size = map->max_raw_write;
1986 if (chunk_size % val_bytes)
1987 chunk_size -= chunk_size % val_bytes;
1988 chunk_count = total_size / chunk_size;
1989 chunk_stride *= chunk_size / val_bytes;
1992 map->lock(map->lock_arg);
1993 /* Write as many bytes as possible with chunk_size */
1994 for (i = 0; i < chunk_count; i++) {
1995 ret = _regmap_raw_write(map,
1996 reg + (i * chunk_stride),
1997 val + (i * chunk_size),
2003 /* Write remaining bytes */
2004 if (!ret && chunk_size * i < total_size) {
2005 ret = _regmap_raw_write(map, reg + (i * chunk_stride),
2006 val + (i * chunk_size),
2007 total_size - i * chunk_size);
2009 map->unlock(map->lock_arg);
2016 wval = kmemdup(val, val_count * val_bytes, map->alloc_flags);
2018 dev_err(map->dev, "Error in memory allocation\n");
2021 for (i = 0; i < val_count * val_bytes; i += val_bytes)
2022 map->format.parse_inplace(wval + i);
2024 map->lock(map->lock_arg);
2025 ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count);
2026 map->unlock(map->lock_arg);
2032 EXPORT_SYMBOL_GPL(regmap_bulk_write);
2035 * _regmap_raw_multi_reg_write()
2037 * the (register,newvalue) pairs in regs have not been formatted, but
2038 * they are all in the same page and have been changed to being page
2039 * relative. The page register has been written if that was necessary.
2041 static int _regmap_raw_multi_reg_write(struct regmap *map,
2042 const struct reg_sequence *regs,
2049 size_t val_bytes = map->format.val_bytes;
2050 size_t reg_bytes = map->format.reg_bytes;
2051 size_t pad_bytes = map->format.pad_bytes;
2052 size_t pair_size = reg_bytes + pad_bytes + val_bytes;
2053 size_t len = pair_size * num_regs;
2058 buf = kzalloc(len, GFP_KERNEL);
2062 /* We have to linearise by hand. */
2066 for (i = 0; i < num_regs; i++) {
2067 unsigned int reg = regs[i].reg;
2068 unsigned int val = regs[i].def;
2069 trace_regmap_hw_write_start(map, reg, 1);
2070 map->format.format_reg(u8, reg, map->reg_shift);
2071 u8 += reg_bytes + pad_bytes;
2072 map->format.format_val(u8, val, 0);
2076 *u8 |= map->write_flag_mask;
2078 ret = map->bus->write(map->bus_context, buf, len);
2082 for (i = 0; i < num_regs; i++) {
2083 int reg = regs[i].reg;
2084 trace_regmap_hw_write_done(map, reg, 1);
2089 static unsigned int _regmap_register_page(struct regmap *map,
2091 struct regmap_range_node *range)
2093 unsigned int win_page = (reg - range->range_min) / range->window_len;
2098 static int _regmap_range_multi_paged_reg_write(struct regmap *map,
2099 struct reg_sequence *regs,
2104 struct reg_sequence *base;
2105 unsigned int this_page = 0;
2106 unsigned int page_change = 0;
2108 * the set of registers are not neccessarily in order, but
2109 * since the order of write must be preserved this algorithm
2110 * chops the set each time the page changes. This also applies
2111 * if there is a delay required at any point in the sequence.
2114 for (i = 0, n = 0; i < num_regs; i++, n++) {
2115 unsigned int reg = regs[i].reg;
2116 struct regmap_range_node *range;
2118 range = _regmap_range_lookup(map, reg);
2120 unsigned int win_page = _regmap_register_page(map, reg,
2124 this_page = win_page;
2125 if (win_page != this_page) {
2126 this_page = win_page;
2131 /* If we have both a page change and a delay make sure to
2132 * write the regs and apply the delay before we change the
2136 if (page_change || regs[i].delay_us) {
2138 /* For situations where the first write requires
2139 * a delay we need to make sure we don't call
2140 * raw_multi_reg_write with n=0
2141 * This can't occur with page breaks as we
2142 * never write on the first iteration
2144 if (regs[i].delay_us && i == 0)
2147 ret = _regmap_raw_multi_reg_write(map, base, n);
2151 if (regs[i].delay_us)
2152 udelay(regs[i].delay_us);
2158 ret = _regmap_select_page(map,
2171 return _regmap_raw_multi_reg_write(map, base, n);
2175 static int _regmap_multi_reg_write(struct regmap *map,
2176 const struct reg_sequence *regs,
2182 if (!map->can_multi_write) {
2183 for (i = 0; i < num_regs; i++) {
2184 ret = _regmap_write(map, regs[i].reg, regs[i].def);
2188 if (regs[i].delay_us)
2189 udelay(regs[i].delay_us);
2194 if (!map->format.parse_inplace)
2197 if (map->writeable_reg)
2198 for (i = 0; i < num_regs; i++) {
2199 int reg = regs[i].reg;
2200 if (!map->writeable_reg(map->dev, reg))
2202 if (!IS_ALIGNED(reg, map->reg_stride))
2206 if (!map->cache_bypass) {
2207 for (i = 0; i < num_regs; i++) {
2208 unsigned int val = regs[i].def;
2209 unsigned int reg = regs[i].reg;
2210 ret = regcache_write(map, reg, val);
2213 "Error in caching of register: %x ret: %d\n",
2218 if (map->cache_only) {
2219 map->cache_dirty = true;
2226 for (i = 0; i < num_regs; i++) {
2227 unsigned int reg = regs[i].reg;
2228 struct regmap_range_node *range;
2230 /* Coalesce all the writes between a page break or a delay
2233 range = _regmap_range_lookup(map, reg);
2234 if (range || regs[i].delay_us) {
2235 size_t len = sizeof(struct reg_sequence)*num_regs;
2236 struct reg_sequence *base = kmemdup(regs, len,
2240 ret = _regmap_range_multi_paged_reg_write(map, base,
2247 return _regmap_raw_multi_reg_write(map, regs, num_regs);
2251 * regmap_multi_reg_write() - Write multiple registers to the device
2253 * @map: Register map to write to
2254 * @regs: Array of structures containing register,value to be written
2255 * @num_regs: Number of registers to write
2257 * Write multiple registers to the device where the set of register, value
2258 * pairs are supplied in any order, possibly not all in a single range.
2260 * The 'normal' block write mode will send ultimately send data on the
2261 * target bus as R,V1,V2,V3,..,Vn where successively higher registers are
2262 * addressed. However, this alternative block multi write mode will send
2263 * the data as R1,V1,R2,V2,..,Rn,Vn on the target bus. The target device
2264 * must of course support the mode.
2266 * A value of zero will be returned on success, a negative errno will be
2267 * returned in error cases.
2269 int regmap_multi_reg_write(struct regmap *map, const struct reg_sequence *regs,
2274 map->lock(map->lock_arg);
2276 ret = _regmap_multi_reg_write(map, regs, num_regs);
2278 map->unlock(map->lock_arg);
2282 EXPORT_SYMBOL_GPL(regmap_multi_reg_write);
2285 * regmap_multi_reg_write_bypassed() - Write multiple registers to the
2286 * device but not the cache
2288 * @map: Register map to write to
2289 * @regs: Array of structures containing register,value to be written
2290 * @num_regs: Number of registers to write
2292 * Write multiple registers to the device but not the cache where the set
2293 * of register are supplied in any order.
2295 * This function is intended to be used for writing a large block of data
2296 * atomically to the device in single transfer for those I2C client devices
2297 * that implement this alternative block write mode.
2299 * A value of zero will be returned on success, a negative errno will
2300 * be returned in error cases.
2302 int regmap_multi_reg_write_bypassed(struct regmap *map,
2303 const struct reg_sequence *regs,
2309 map->lock(map->lock_arg);
2311 bypass = map->cache_bypass;
2312 map->cache_bypass = true;
2314 ret = _regmap_multi_reg_write(map, regs, num_regs);
2316 map->cache_bypass = bypass;
2318 map->unlock(map->lock_arg);
2322 EXPORT_SYMBOL_GPL(regmap_multi_reg_write_bypassed);
2325 * regmap_raw_write_async() - Write raw values to one or more registers
2328 * @map: Register map to write to
2329 * @reg: Initial register to write to
2330 * @val: Block of data to be written, laid out for direct transmission to the
2331 * device. Must be valid until regmap_async_complete() is called.
2332 * @val_len: Length of data pointed to by val.
2334 * This function is intended to be used for things like firmware
2335 * download where a large block of data needs to be transferred to the
2336 * device. No formatting will be done on the data provided.
2338 * If supported by the underlying bus the write will be scheduled
2339 * asynchronously, helping maximise I/O speed on higher speed buses
2340 * like SPI. regmap_async_complete() can be called to ensure that all
2341 * asynchrnous writes have been completed.
2343 * A value of zero will be returned on success, a negative errno will
2344 * be returned in error cases.
2346 int regmap_raw_write_async(struct regmap *map, unsigned int reg,
2347 const void *val, size_t val_len)
2351 if (val_len % map->format.val_bytes)
2353 if (!IS_ALIGNED(reg, map->reg_stride))
2356 map->lock(map->lock_arg);
2360 ret = _regmap_raw_write(map, reg, val, val_len);
2364 map->unlock(map->lock_arg);
2368 EXPORT_SYMBOL_GPL(regmap_raw_write_async);
2370 static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
2371 unsigned int val_len)
2373 struct regmap_range_node *range;
2378 if (!map->bus || !map->bus->read)
2381 range = _regmap_range_lookup(map, reg);
2383 ret = _regmap_select_page(map, ®, range,
2384 val_len / map->format.val_bytes);
2389 map->format.format_reg(map->work_buf, reg, map->reg_shift);
2390 regmap_set_work_buf_flag_mask(map, map->format.reg_bytes,
2391 map->read_flag_mask);
2392 trace_regmap_hw_read_start(map, reg, val_len / map->format.val_bytes);
2394 ret = map->bus->read(map->bus_context, map->work_buf,
2395 map->format.reg_bytes + map->format.pad_bytes,
2398 trace_regmap_hw_read_done(map, reg, val_len / map->format.val_bytes);
2403 static int _regmap_bus_reg_read(void *context, unsigned int reg,
2406 struct regmap *map = context;
2408 return map->bus->reg_read(map->bus_context, reg, val);
2411 static int _regmap_bus_read(void *context, unsigned int reg,
2415 struct regmap *map = context;
2417 if (!map->format.parse_val)
2420 ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
2422 *val = map->format.parse_val(map->work_buf);
2427 static int _regmap_read(struct regmap *map, unsigned int reg,
2431 void *context = _regmap_map_get_context(map);
2433 if (!map->cache_bypass) {
2434 ret = regcache_read(map, reg, val);
2439 if (map->cache_only)
2442 if (!regmap_readable(map, reg))
2445 ret = map->reg_read(context, reg, val);
2448 if (map->dev && strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
2449 dev_info(map->dev, "%x => %x\n", reg, *val);
2452 trace_regmap_reg_read(map, reg, *val);
2454 if (!map->cache_bypass)
2455 regcache_write(map, reg, *val);
2462 * regmap_read() - Read a value from a single register
2464 * @map: Register map to read from
2465 * @reg: Register to be read from
2466 * @val: Pointer to store read value
2468 * A value of zero will be returned on success, a negative errno will
2469 * be returned in error cases.
2471 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
2475 if (!IS_ALIGNED(reg, map->reg_stride))
2478 map->lock(map->lock_arg);
2480 ret = _regmap_read(map, reg, val);
2482 map->unlock(map->lock_arg);
2486 EXPORT_SYMBOL_GPL(regmap_read);
2489 * regmap_raw_read() - Read raw data from the device
2491 * @map: Register map to read from
2492 * @reg: First register to be read from
2493 * @val: Pointer to store read value
2494 * @val_len: Size of data to read
2496 * A value of zero will be returned on success, a negative errno will
2497 * be returned in error cases.
2499 int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
2502 size_t val_bytes = map->format.val_bytes;
2503 size_t val_count = val_len / val_bytes;
2509 if (val_len % map->format.val_bytes)
2511 if (!IS_ALIGNED(reg, map->reg_stride))
2516 map->lock(map->lock_arg);
2518 if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
2519 map->cache_type == REGCACHE_NONE) {
2520 if (!map->bus->read) {
2524 if (map->max_raw_read && map->max_raw_read < val_len) {
2529 /* Physical block read if there's no cache involved */
2530 ret = _regmap_raw_read(map, reg, val, val_len);
2533 /* Otherwise go word by word for the cache; should be low
2534 * cost as we expect to hit the cache.
2536 for (i = 0; i < val_count; i++) {
2537 ret = _regmap_read(map, reg + regmap_get_offset(map, i),
2542 map->format.format_val(val + (i * val_bytes), v, 0);
2547 map->unlock(map->lock_arg);
2551 EXPORT_SYMBOL_GPL(regmap_raw_read);
2554 * regmap_field_read() - Read a value to a single register field
2556 * @field: Register field to read from
2557 * @val: Pointer to store read value
2559 * A value of zero will be returned on success, a negative errno will
2560 * be returned in error cases.
2562 int regmap_field_read(struct regmap_field *field, unsigned int *val)
2565 unsigned int reg_val;
2566 ret = regmap_read(field->regmap, field->reg, ®_val);
2570 reg_val &= field->mask;
2571 reg_val >>= field->shift;
2576 EXPORT_SYMBOL_GPL(regmap_field_read);
2579 * regmap_fields_read() - Read a value to a single register field with port ID
2581 * @field: Register field to read from
2583 * @val: Pointer to store read value
2585 * A value of zero will be returned on success, a negative errno will
2586 * be returned in error cases.
2588 int regmap_fields_read(struct regmap_field *field, unsigned int id,
2592 unsigned int reg_val;
2594 if (id >= field->id_size)
2597 ret = regmap_read(field->regmap,
2598 field->reg + (field->id_offset * id),
2603 reg_val &= field->mask;
2604 reg_val >>= field->shift;
2609 EXPORT_SYMBOL_GPL(regmap_fields_read);
2612 * regmap_bulk_read() - Read multiple registers from the device
2614 * @map: Register map to read from
2615 * @reg: First register to be read from
2616 * @val: Pointer to store read value, in native register size for device
2617 * @val_count: Number of registers to read
2619 * A value of zero will be returned on success, a negative errno will
2620 * be returned in error cases.
2622 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
2626 size_t val_bytes = map->format.val_bytes;
2627 bool vol = regmap_volatile_range(map, reg, val_count);
2629 if (!IS_ALIGNED(reg, map->reg_stride))
2632 if (map->bus && map->format.parse_inplace && (vol || map->cache_type == REGCACHE_NONE)) {
2634 * Some devices does not support bulk read, for
2635 * them we have a series of single read operations.
2637 size_t total_size = val_bytes * val_count;
2639 if (!map->use_single_read &&
2640 (!map->max_raw_read || map->max_raw_read > total_size)) {
2641 ret = regmap_raw_read(map, reg, val,
2642 val_bytes * val_count);
2647 * Some devices do not support bulk read or do not
2648 * support large bulk reads, for them we have a series
2649 * of read operations.
2651 int chunk_stride = map->reg_stride;
2652 size_t chunk_size = val_bytes;
2653 size_t chunk_count = val_count;
2655 if (!map->use_single_read) {
2656 chunk_size = map->max_raw_read;
2657 if (chunk_size % val_bytes)
2658 chunk_size -= chunk_size % val_bytes;
2659 chunk_count = total_size / chunk_size;
2660 chunk_stride *= chunk_size / val_bytes;
2663 /* Read bytes that fit into a multiple of chunk_size */
2664 for (i = 0; i < chunk_count; i++) {
2665 ret = regmap_raw_read(map,
2666 reg + (i * chunk_stride),
2667 val + (i * chunk_size),
2673 /* Read remaining bytes */
2674 if (chunk_size * i < total_size) {
2675 ret = regmap_raw_read(map,
2676 reg + (i * chunk_stride),
2677 val + (i * chunk_size),
2678 total_size - i * chunk_size);
2684 for (i = 0; i < val_count * val_bytes; i += val_bytes)
2685 map->format.parse_inplace(val + i);
2687 for (i = 0; i < val_count; i++) {
2689 ret = regmap_read(map, reg + regmap_get_offset(map, i),
2694 if (map->format.format_val) {
2695 map->format.format_val(val + (i * val_bytes), ival, 0);
2697 /* Devices providing read and write
2698 * operations can use the bulk I/O
2699 * functions if they define a val_bytes,
2700 * we assume that the values are native
2710 switch (map->format.val_bytes) {
2734 EXPORT_SYMBOL_GPL(regmap_bulk_read);
2736 static int _regmap_update_bits(struct regmap *map, unsigned int reg,
2737 unsigned int mask, unsigned int val,
2738 bool *change, bool force_write)
2741 unsigned int tmp, orig;
2746 if (regmap_volatile(map, reg) && map->reg_update_bits) {
2747 ret = map->reg_update_bits(map->bus_context, reg, mask, val);
2748 if (ret == 0 && change)
2751 ret = _regmap_read(map, reg, &orig);
2758 if (force_write || (tmp != orig)) {
2759 ret = _regmap_write(map, reg, tmp);
2760 if (ret == 0 && change)
2769 * regmap_update_bits_base() - Perform a read/modify/write cycle on a register
2771 * @map: Register map to update
2772 * @reg: Register to update
2773 * @mask: Bitmask to change
2774 * @val: New value for bitmask
2775 * @change: Boolean indicating if a write was done
2776 * @async: Boolean indicating asynchronously
2777 * @force: Boolean indicating use force update
2779 * Perform a read/modify/write cycle on a register map with change, async, force
2784 * With most buses the read must be done synchronously so this is most useful
2785 * for devices with a cache which do not need to interact with the hardware to
2786 * determine the current register value.
2788 * Returns zero for success, a negative number on error.
2790 int regmap_update_bits_base(struct regmap *map, unsigned int reg,
2791 unsigned int mask, unsigned int val,
2792 bool *change, bool async, bool force)
2796 map->lock(map->lock_arg);
2800 ret = _regmap_update_bits(map, reg, mask, val, change, force);
2804 map->unlock(map->lock_arg);
2808 EXPORT_SYMBOL_GPL(regmap_update_bits_base);
2810 void regmap_async_complete_cb(struct regmap_async *async, int ret)
2812 struct regmap *map = async->map;
2815 trace_regmap_async_io_complete(map);
2817 spin_lock(&map->async_lock);
2818 list_move(&async->list, &map->async_free);
2819 wake = list_empty(&map->async_list);
2822 map->async_ret = ret;
2824 spin_unlock(&map->async_lock);
2827 wake_up(&map->async_waitq);
2829 EXPORT_SYMBOL_GPL(regmap_async_complete_cb);
2831 static int regmap_async_is_done(struct regmap *map)
2833 unsigned long flags;
2836 spin_lock_irqsave(&map->async_lock, flags);
2837 ret = list_empty(&map->async_list);
2838 spin_unlock_irqrestore(&map->async_lock, flags);
2844 * regmap_async_complete - Ensure all asynchronous I/O has completed.
2846 * @map: Map to operate on.
2848 * Blocks until any pending asynchronous I/O has completed. Returns
2849 * an error code for any failed I/O operations.
2851 int regmap_async_complete(struct regmap *map)
2853 unsigned long flags;
2856 /* Nothing to do with no async support */
2857 if (!map->bus || !map->bus->async_write)
2860 trace_regmap_async_complete_start(map);
2862 wait_event(map->async_waitq, regmap_async_is_done(map));
2864 spin_lock_irqsave(&map->async_lock, flags);
2865 ret = map->async_ret;
2867 spin_unlock_irqrestore(&map->async_lock, flags);
2869 trace_regmap_async_complete_done(map);
2873 EXPORT_SYMBOL_GPL(regmap_async_complete);
2876 * regmap_register_patch - Register and apply register updates to be applied
2877 * on device initialistion
2879 * @map: Register map to apply updates to.
2880 * @regs: Values to update.
2881 * @num_regs: Number of entries in regs.
2883 * Register a set of register updates to be applied to the device
2884 * whenever the device registers are synchronised with the cache and
2885 * apply them immediately. Typically this is used to apply
2886 * corrections to be applied to the device defaults on startup, such
2887 * as the updates some vendors provide to undocumented registers.
2889 * The caller must ensure that this function cannot be called
2890 * concurrently with either itself or regcache_sync().
2892 int regmap_register_patch(struct regmap *map, const struct reg_sequence *regs,
2895 struct reg_sequence *p;
2899 if (WARN_ONCE(num_regs <= 0, "invalid registers number (%d)\n",
2903 p = krealloc(map->patch,
2904 sizeof(struct reg_sequence) * (map->patch_regs + num_regs),
2907 memcpy(p + map->patch_regs, regs, num_regs * sizeof(*regs));
2909 map->patch_regs += num_regs;
2914 map->lock(map->lock_arg);
2916 bypass = map->cache_bypass;
2918 map->cache_bypass = true;
2921 ret = _regmap_multi_reg_write(map, regs, num_regs);
2924 map->cache_bypass = bypass;
2926 map->unlock(map->lock_arg);
2928 regmap_async_complete(map);
2932 EXPORT_SYMBOL_GPL(regmap_register_patch);
2935 * regmap_get_val_bytes() - Report the size of a register value
2937 * @map: Register map to operate on.
2939 * Report the size of a register value, mainly intended to for use by
2940 * generic infrastructure built on top of regmap.
2942 int regmap_get_val_bytes(struct regmap *map)
2944 if (map->format.format_write)
2947 return map->format.val_bytes;
2949 EXPORT_SYMBOL_GPL(regmap_get_val_bytes);
2952 * regmap_get_max_register() - Report the max register value
2954 * @map: Register map to operate on.
2956 * Report the max register value, mainly intended to for use by
2957 * generic infrastructure built on top of regmap.
2959 int regmap_get_max_register(struct regmap *map)
2961 return map->max_register ? map->max_register : -EINVAL;
2963 EXPORT_SYMBOL_GPL(regmap_get_max_register);
2966 * regmap_get_reg_stride() - Report the register address stride
2968 * @map: Register map to operate on.
2970 * Report the register address stride, mainly intended to for use by
2971 * generic infrastructure built on top of regmap.
2973 int regmap_get_reg_stride(struct regmap *map)
2975 return map->reg_stride;
2977 EXPORT_SYMBOL_GPL(regmap_get_reg_stride);
2979 int regmap_parse_val(struct regmap *map, const void *buf,
2982 if (!map->format.parse_val)
2985 *val = map->format.parse_val(buf);
2989 EXPORT_SYMBOL_GPL(regmap_parse_val);
2991 static int __init regmap_initcall(void)
2993 regmap_debugfs_initcall();
2997 postcore_initcall(regmap_initcall);