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 #ifdef REGMAP_HWSPINLOCK
418 static void regmap_lock_hwlock(void *__map)
420 struct regmap *map = __map;
422 hwspin_lock_timeout(map->hwlock, UINT_MAX);
425 static void regmap_lock_hwlock_irq(void *__map)
427 struct regmap *map = __map;
429 hwspin_lock_timeout_irq(map->hwlock, UINT_MAX);
432 static void regmap_lock_hwlock_irqsave(void *__map)
434 struct regmap *map = __map;
436 hwspin_lock_timeout_irqsave(map->hwlock, UINT_MAX,
437 &map->spinlock_flags);
440 static void regmap_unlock_hwlock(void *__map)
442 struct regmap *map = __map;
444 hwspin_unlock(map->hwlock);
447 static void regmap_unlock_hwlock_irq(void *__map)
449 struct regmap *map = __map;
451 hwspin_unlock_irq(map->hwlock);
454 static void regmap_unlock_hwlock_irqrestore(void *__map)
456 struct regmap *map = __map;
458 hwspin_unlock_irqrestore(map->hwlock, &map->spinlock_flags);
462 static void regmap_lock_mutex(void *__map)
464 struct regmap *map = __map;
465 mutex_lock(&map->mutex);
468 static void regmap_unlock_mutex(void *__map)
470 struct regmap *map = __map;
471 mutex_unlock(&map->mutex);
474 static void regmap_lock_spinlock(void *__map)
475 __acquires(&map->spinlock)
477 struct regmap *map = __map;
480 spin_lock_irqsave(&map->spinlock, flags);
481 map->spinlock_flags = flags;
484 static void regmap_unlock_spinlock(void *__map)
485 __releases(&map->spinlock)
487 struct regmap *map = __map;
488 spin_unlock_irqrestore(&map->spinlock, map->spinlock_flags);
491 static void dev_get_regmap_release(struct device *dev, void *res)
494 * We don't actually have anything to do here; the goal here
495 * is not to manage the regmap but to provide a simple way to
496 * get the regmap back given a struct device.
500 static bool _regmap_range_add(struct regmap *map,
501 struct regmap_range_node *data)
503 struct rb_root *root = &map->range_tree;
504 struct rb_node **new = &(root->rb_node), *parent = NULL;
507 struct regmap_range_node *this =
508 rb_entry(*new, struct regmap_range_node, node);
511 if (data->range_max < this->range_min)
512 new = &((*new)->rb_left);
513 else if (data->range_min > this->range_max)
514 new = &((*new)->rb_right);
519 rb_link_node(&data->node, parent, new);
520 rb_insert_color(&data->node, root);
525 static struct regmap_range_node *_regmap_range_lookup(struct regmap *map,
528 struct rb_node *node = map->range_tree.rb_node;
531 struct regmap_range_node *this =
532 rb_entry(node, struct regmap_range_node, node);
534 if (reg < this->range_min)
535 node = node->rb_left;
536 else if (reg > this->range_max)
537 node = node->rb_right;
545 static void regmap_range_exit(struct regmap *map)
547 struct rb_node *next;
548 struct regmap_range_node *range_node;
550 next = rb_first(&map->range_tree);
552 range_node = rb_entry(next, struct regmap_range_node, node);
553 next = rb_next(&range_node->node);
554 rb_erase(&range_node->node, &map->range_tree);
558 kfree(map->selector_work_buf);
561 int regmap_attach_dev(struct device *dev, struct regmap *map,
562 const struct regmap_config *config)
568 regmap_debugfs_init(map, config->name);
570 /* Add a devres resource for dev_get_regmap() */
571 m = devres_alloc(dev_get_regmap_release, sizeof(*m), GFP_KERNEL);
573 regmap_debugfs_exit(map);
581 EXPORT_SYMBOL_GPL(regmap_attach_dev);
583 static enum regmap_endian regmap_get_reg_endian(const struct regmap_bus *bus,
584 const struct regmap_config *config)
586 enum regmap_endian endian;
588 /* Retrieve the endianness specification from the regmap config */
589 endian = config->reg_format_endian;
591 /* If the regmap config specified a non-default value, use that */
592 if (endian != REGMAP_ENDIAN_DEFAULT)
595 /* Retrieve the endianness specification from the bus config */
596 if (bus && bus->reg_format_endian_default)
597 endian = bus->reg_format_endian_default;
599 /* If the bus specified a non-default value, use that */
600 if (endian != REGMAP_ENDIAN_DEFAULT)
603 /* Use this if no other value was found */
604 return REGMAP_ENDIAN_BIG;
607 enum regmap_endian regmap_get_val_endian(struct device *dev,
608 const struct regmap_bus *bus,
609 const struct regmap_config *config)
611 struct device_node *np;
612 enum regmap_endian endian;
614 /* Retrieve the endianness specification from the regmap config */
615 endian = config->val_format_endian;
617 /* If the regmap config specified a non-default value, use that */
618 if (endian != REGMAP_ENDIAN_DEFAULT)
621 /* If the dev and dev->of_node exist try to get endianness from DT */
622 if (dev && dev->of_node) {
625 /* Parse the device's DT node for an endianness specification */
626 if (of_property_read_bool(np, "big-endian"))
627 endian = REGMAP_ENDIAN_BIG;
628 else if (of_property_read_bool(np, "little-endian"))
629 endian = REGMAP_ENDIAN_LITTLE;
630 else if (of_property_read_bool(np, "native-endian"))
631 endian = REGMAP_ENDIAN_NATIVE;
633 /* If the endianness was specified in DT, use that */
634 if (endian != REGMAP_ENDIAN_DEFAULT)
638 /* Retrieve the endianness specification from the bus config */
639 if (bus && bus->val_format_endian_default)
640 endian = bus->val_format_endian_default;
642 /* If the bus specified a non-default value, use that */
643 if (endian != REGMAP_ENDIAN_DEFAULT)
646 /* Use this if no other value was found */
647 return REGMAP_ENDIAN_BIG;
649 EXPORT_SYMBOL_GPL(regmap_get_val_endian);
651 struct regmap *__regmap_init(struct device *dev,
652 const struct regmap_bus *bus,
654 const struct regmap_config *config,
655 struct lock_class_key *lock_key,
656 const char *lock_name)
660 enum regmap_endian reg_endian, val_endian;
666 map = kzalloc(sizeof(*map), GFP_KERNEL);
672 if (config->lock && config->unlock) {
673 map->lock = config->lock;
674 map->unlock = config->unlock;
675 map->lock_arg = config->lock_arg;
676 } else if (config->hwlock_id) {
677 #ifdef REGMAP_HWSPINLOCK
678 map->hwlock = hwspin_lock_request_specific(config->hwlock_id);
684 switch (config->hwlock_mode) {
685 case HWLOCK_IRQSTATE:
686 map->lock = regmap_lock_hwlock_irqsave;
687 map->unlock = regmap_unlock_hwlock_irqrestore;
690 map->lock = regmap_lock_hwlock_irq;
691 map->unlock = regmap_unlock_hwlock_irq;
694 map->lock = regmap_lock_hwlock;
695 map->unlock = regmap_unlock_hwlock;
705 if ((bus && bus->fast_io) ||
707 spin_lock_init(&map->spinlock);
708 map->lock = regmap_lock_spinlock;
709 map->unlock = regmap_unlock_spinlock;
710 lockdep_set_class_and_name(&map->spinlock,
711 lock_key, lock_name);
713 mutex_init(&map->mutex);
714 map->lock = regmap_lock_mutex;
715 map->unlock = regmap_unlock_mutex;
716 lockdep_set_class_and_name(&map->mutex,
717 lock_key, lock_name);
723 * When we write in fast-paths with regmap_bulk_write() don't allocate
724 * scratch buffers with sleeping allocations.
726 if ((bus && bus->fast_io) || config->fast_io)
727 map->alloc_flags = GFP_ATOMIC;
729 map->alloc_flags = GFP_KERNEL;
731 map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8);
732 map->format.pad_bytes = config->pad_bits / 8;
733 map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8);
734 map->format.buf_size = DIV_ROUND_UP(config->reg_bits +
735 config->val_bits + config->pad_bits, 8);
736 map->reg_shift = config->pad_bits % 8;
737 if (config->reg_stride)
738 map->reg_stride = config->reg_stride;
741 if (is_power_of_2(map->reg_stride))
742 map->reg_stride_order = ilog2(map->reg_stride);
744 map->reg_stride_order = -1;
745 map->use_single_read = config->use_single_rw || !bus || !bus->read;
746 map->use_single_write = config->use_single_rw || !bus || !bus->write;
747 map->can_multi_write = config->can_multi_write && bus && bus->write;
749 map->max_raw_read = bus->max_raw_read;
750 map->max_raw_write = bus->max_raw_write;
754 map->bus_context = bus_context;
755 map->max_register = config->max_register;
756 map->wr_table = config->wr_table;
757 map->rd_table = config->rd_table;
758 map->volatile_table = config->volatile_table;
759 map->precious_table = config->precious_table;
760 map->writeable_reg = config->writeable_reg;
761 map->readable_reg = config->readable_reg;
762 map->volatile_reg = config->volatile_reg;
763 map->precious_reg = config->precious_reg;
764 map->cache_type = config->cache_type;
765 map->name = config->name;
767 spin_lock_init(&map->async_lock);
768 INIT_LIST_HEAD(&map->async_list);
769 INIT_LIST_HEAD(&map->async_free);
770 init_waitqueue_head(&map->async_waitq);
772 if (config->read_flag_mask || config->write_flag_mask) {
773 map->read_flag_mask = config->read_flag_mask;
774 map->write_flag_mask = config->write_flag_mask;
776 map->read_flag_mask = bus->read_flag_mask;
780 map->reg_read = config->reg_read;
781 map->reg_write = config->reg_write;
783 map->defer_caching = false;
784 goto skip_format_initialization;
785 } else if (!bus->read || !bus->write) {
786 map->reg_read = _regmap_bus_reg_read;
787 map->reg_write = _regmap_bus_reg_write;
789 map->defer_caching = false;
790 goto skip_format_initialization;
792 map->reg_read = _regmap_bus_read;
793 map->reg_update_bits = bus->reg_update_bits;
796 reg_endian = regmap_get_reg_endian(bus, config);
797 val_endian = regmap_get_val_endian(dev, bus, config);
799 switch (config->reg_bits + map->reg_shift) {
801 switch (config->val_bits) {
803 map->format.format_write = regmap_format_2_6_write;
811 switch (config->val_bits) {
813 map->format.format_write = regmap_format_4_12_write;
821 switch (config->val_bits) {
823 map->format.format_write = regmap_format_7_9_write;
831 switch (config->val_bits) {
833 map->format.format_write = regmap_format_10_14_write;
841 map->format.format_reg = regmap_format_8;
845 switch (reg_endian) {
846 case REGMAP_ENDIAN_BIG:
847 map->format.format_reg = regmap_format_16_be;
849 case REGMAP_ENDIAN_LITTLE:
850 map->format.format_reg = regmap_format_16_le;
852 case REGMAP_ENDIAN_NATIVE:
853 map->format.format_reg = regmap_format_16_native;
861 if (reg_endian != REGMAP_ENDIAN_BIG)
863 map->format.format_reg = regmap_format_24;
867 switch (reg_endian) {
868 case REGMAP_ENDIAN_BIG:
869 map->format.format_reg = regmap_format_32_be;
871 case REGMAP_ENDIAN_LITTLE:
872 map->format.format_reg = regmap_format_32_le;
874 case REGMAP_ENDIAN_NATIVE:
875 map->format.format_reg = regmap_format_32_native;
884 switch (reg_endian) {
885 case REGMAP_ENDIAN_BIG:
886 map->format.format_reg = regmap_format_64_be;
888 case REGMAP_ENDIAN_LITTLE:
889 map->format.format_reg = regmap_format_64_le;
891 case REGMAP_ENDIAN_NATIVE:
892 map->format.format_reg = regmap_format_64_native;
904 if (val_endian == REGMAP_ENDIAN_NATIVE)
905 map->format.parse_inplace = regmap_parse_inplace_noop;
907 switch (config->val_bits) {
909 map->format.format_val = regmap_format_8;
910 map->format.parse_val = regmap_parse_8;
911 map->format.parse_inplace = regmap_parse_inplace_noop;
914 switch (val_endian) {
915 case REGMAP_ENDIAN_BIG:
916 map->format.format_val = regmap_format_16_be;
917 map->format.parse_val = regmap_parse_16_be;
918 map->format.parse_inplace = regmap_parse_16_be_inplace;
920 case REGMAP_ENDIAN_LITTLE:
921 map->format.format_val = regmap_format_16_le;
922 map->format.parse_val = regmap_parse_16_le;
923 map->format.parse_inplace = regmap_parse_16_le_inplace;
925 case REGMAP_ENDIAN_NATIVE:
926 map->format.format_val = regmap_format_16_native;
927 map->format.parse_val = regmap_parse_16_native;
934 if (val_endian != REGMAP_ENDIAN_BIG)
936 map->format.format_val = regmap_format_24;
937 map->format.parse_val = regmap_parse_24;
940 switch (val_endian) {
941 case REGMAP_ENDIAN_BIG:
942 map->format.format_val = regmap_format_32_be;
943 map->format.parse_val = regmap_parse_32_be;
944 map->format.parse_inplace = regmap_parse_32_be_inplace;
946 case REGMAP_ENDIAN_LITTLE:
947 map->format.format_val = regmap_format_32_le;
948 map->format.parse_val = regmap_parse_32_le;
949 map->format.parse_inplace = regmap_parse_32_le_inplace;
951 case REGMAP_ENDIAN_NATIVE:
952 map->format.format_val = regmap_format_32_native;
953 map->format.parse_val = regmap_parse_32_native;
961 switch (val_endian) {
962 case REGMAP_ENDIAN_BIG:
963 map->format.format_val = regmap_format_64_be;
964 map->format.parse_val = regmap_parse_64_be;
965 map->format.parse_inplace = regmap_parse_64_be_inplace;
967 case REGMAP_ENDIAN_LITTLE:
968 map->format.format_val = regmap_format_64_le;
969 map->format.parse_val = regmap_parse_64_le;
970 map->format.parse_inplace = regmap_parse_64_le_inplace;
972 case REGMAP_ENDIAN_NATIVE:
973 map->format.format_val = regmap_format_64_native;
974 map->format.parse_val = regmap_parse_64_native;
983 if (map->format.format_write) {
984 if ((reg_endian != REGMAP_ENDIAN_BIG) ||
985 (val_endian != REGMAP_ENDIAN_BIG))
987 map->use_single_write = true;
990 if (!map->format.format_write &&
991 !(map->format.format_reg && map->format.format_val))
994 map->work_buf = kzalloc(map->format.buf_size, GFP_KERNEL);
995 if (map->work_buf == NULL) {
1000 if (map->format.format_write) {
1001 map->defer_caching = false;
1002 map->reg_write = _regmap_bus_formatted_write;
1003 } else if (map->format.format_val) {
1004 map->defer_caching = true;
1005 map->reg_write = _regmap_bus_raw_write;
1008 skip_format_initialization:
1010 map->range_tree = RB_ROOT;
1011 for (i = 0; i < config->num_ranges; i++) {
1012 const struct regmap_range_cfg *range_cfg = &config->ranges[i];
1013 struct regmap_range_node *new;
1016 if (range_cfg->range_max < range_cfg->range_min) {
1017 dev_err(map->dev, "Invalid range %d: %d < %d\n", i,
1018 range_cfg->range_max, range_cfg->range_min);
1022 if (range_cfg->range_max > map->max_register) {
1023 dev_err(map->dev, "Invalid range %d: %d > %d\n", i,
1024 range_cfg->range_max, map->max_register);
1028 if (range_cfg->selector_reg > map->max_register) {
1030 "Invalid range %d: selector out of map\n", i);
1034 if (range_cfg->window_len == 0) {
1035 dev_err(map->dev, "Invalid range %d: window_len 0\n",
1040 /* Make sure, that this register range has no selector
1041 or data window within its boundary */
1042 for (j = 0; j < config->num_ranges; j++) {
1043 unsigned sel_reg = config->ranges[j].selector_reg;
1044 unsigned win_min = config->ranges[j].window_start;
1045 unsigned win_max = win_min +
1046 config->ranges[j].window_len - 1;
1048 /* Allow data window inside its own virtual range */
1052 if (range_cfg->range_min <= sel_reg &&
1053 sel_reg <= range_cfg->range_max) {
1055 "Range %d: selector for %d in window\n",
1060 if (!(win_max < range_cfg->range_min ||
1061 win_min > range_cfg->range_max)) {
1063 "Range %d: window for %d in window\n",
1069 new = kzalloc(sizeof(*new), GFP_KERNEL);
1076 new->name = range_cfg->name;
1077 new->range_min = range_cfg->range_min;
1078 new->range_max = range_cfg->range_max;
1079 new->selector_reg = range_cfg->selector_reg;
1080 new->selector_mask = range_cfg->selector_mask;
1081 new->selector_shift = range_cfg->selector_shift;
1082 new->window_start = range_cfg->window_start;
1083 new->window_len = range_cfg->window_len;
1085 if (!_regmap_range_add(map, new)) {
1086 dev_err(map->dev, "Failed to add range %d\n", i);
1091 if (map->selector_work_buf == NULL) {
1092 map->selector_work_buf =
1093 kzalloc(map->format.buf_size, GFP_KERNEL);
1094 if (map->selector_work_buf == NULL) {
1101 ret = regcache_init(map, config);
1106 ret = regmap_attach_dev(dev, map, config);
1116 regmap_range_exit(map);
1117 kfree(map->work_buf);
1119 if (IS_ENABLED(REGMAP_HWSPINLOCK) && map->hwlock)
1120 hwspin_lock_free(map->hwlock);
1124 return ERR_PTR(ret);
1126 EXPORT_SYMBOL_GPL(__regmap_init);
1128 static void devm_regmap_release(struct device *dev, void *res)
1130 regmap_exit(*(struct regmap **)res);
1133 struct regmap *__devm_regmap_init(struct device *dev,
1134 const struct regmap_bus *bus,
1136 const struct regmap_config *config,
1137 struct lock_class_key *lock_key,
1138 const char *lock_name)
1140 struct regmap **ptr, *regmap;
1142 ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL);
1144 return ERR_PTR(-ENOMEM);
1146 regmap = __regmap_init(dev, bus, bus_context, config,
1147 lock_key, lock_name);
1148 if (!IS_ERR(regmap)) {
1150 devres_add(dev, ptr);
1157 EXPORT_SYMBOL_GPL(__devm_regmap_init);
1159 static void regmap_field_init(struct regmap_field *rm_field,
1160 struct regmap *regmap, struct reg_field reg_field)
1162 rm_field->regmap = regmap;
1163 rm_field->reg = reg_field.reg;
1164 rm_field->shift = reg_field.lsb;
1165 rm_field->mask = GENMASK(reg_field.msb, reg_field.lsb);
1166 rm_field->id_size = reg_field.id_size;
1167 rm_field->id_offset = reg_field.id_offset;
1171 * devm_regmap_field_alloc() - Allocate and initialise a register field.
1173 * @dev: Device that will be interacted with
1174 * @regmap: regmap bank in which this register field is located.
1175 * @reg_field: Register field with in the bank.
1177 * The return value will be an ERR_PTR() on error or a valid pointer
1178 * to a struct regmap_field. The regmap_field will be automatically freed
1179 * by the device management code.
1181 struct regmap_field *devm_regmap_field_alloc(struct device *dev,
1182 struct regmap *regmap, struct reg_field reg_field)
1184 struct regmap_field *rm_field = devm_kzalloc(dev,
1185 sizeof(*rm_field), GFP_KERNEL);
1187 return ERR_PTR(-ENOMEM);
1189 regmap_field_init(rm_field, regmap, reg_field);
1194 EXPORT_SYMBOL_GPL(devm_regmap_field_alloc);
1197 * devm_regmap_field_free() - Free a register field allocated using
1198 * devm_regmap_field_alloc.
1200 * @dev: Device that will be interacted with
1201 * @field: regmap field which should be freed.
1203 * Free register field allocated using devm_regmap_field_alloc(). Usually
1204 * drivers need not call this function, as the memory allocated via devm
1205 * will be freed as per device-driver life-cyle.
1207 void devm_regmap_field_free(struct device *dev,
1208 struct regmap_field *field)
1210 devm_kfree(dev, field);
1212 EXPORT_SYMBOL_GPL(devm_regmap_field_free);
1215 * regmap_field_alloc() - Allocate and initialise a register field.
1217 * @regmap: regmap bank in which this register field is located.
1218 * @reg_field: Register field with in the bank.
1220 * The return value will be an ERR_PTR() on error or a valid pointer
1221 * to a struct regmap_field. The regmap_field should be freed by the
1222 * user once its finished working with it using regmap_field_free().
1224 struct regmap_field *regmap_field_alloc(struct regmap *regmap,
1225 struct reg_field reg_field)
1227 struct regmap_field *rm_field = kzalloc(sizeof(*rm_field), GFP_KERNEL);
1230 return ERR_PTR(-ENOMEM);
1232 regmap_field_init(rm_field, regmap, reg_field);
1236 EXPORT_SYMBOL_GPL(regmap_field_alloc);
1239 * regmap_field_free() - Free register field allocated using
1240 * regmap_field_alloc.
1242 * @field: regmap field which should be freed.
1244 void regmap_field_free(struct regmap_field *field)
1248 EXPORT_SYMBOL_GPL(regmap_field_free);
1251 * regmap_reinit_cache() - Reinitialise the current register cache
1253 * @map: Register map to operate on.
1254 * @config: New configuration. Only the cache data will be used.
1256 * Discard any existing register cache for the map and initialize a
1257 * new cache. This can be used to restore the cache to defaults or to
1258 * update the cache configuration to reflect runtime discovery of the
1261 * No explicit locking is done here, the user needs to ensure that
1262 * this function will not race with other calls to regmap.
1264 int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
1267 regmap_debugfs_exit(map);
1269 map->max_register = config->max_register;
1270 map->writeable_reg = config->writeable_reg;
1271 map->readable_reg = config->readable_reg;
1272 map->volatile_reg = config->volatile_reg;
1273 map->precious_reg = config->precious_reg;
1274 map->cache_type = config->cache_type;
1276 regmap_debugfs_init(map, config->name);
1278 map->cache_bypass = false;
1279 map->cache_only = false;
1281 return regcache_init(map, config);
1283 EXPORT_SYMBOL_GPL(regmap_reinit_cache);
1286 * regmap_exit() - Free a previously allocated register map
1288 * @map: Register map to operate on.
1290 void regmap_exit(struct regmap *map)
1292 struct regmap_async *async;
1295 regmap_debugfs_exit(map);
1296 regmap_range_exit(map);
1297 if (map->bus && map->bus->free_context)
1298 map->bus->free_context(map->bus_context);
1299 kfree(map->work_buf);
1300 while (!list_empty(&map->async_free)) {
1301 async = list_first_entry_or_null(&map->async_free,
1302 struct regmap_async,
1304 list_del(&async->list);
1305 kfree(async->work_buf);
1308 if (IS_ENABLED(REGMAP_HWSPINLOCK) && map->hwlock)
1309 hwspin_lock_free(map->hwlock);
1312 EXPORT_SYMBOL_GPL(regmap_exit);
1314 static int dev_get_regmap_match(struct device *dev, void *res, void *data)
1316 struct regmap **r = res;
1322 /* If the user didn't specify a name match any */
1324 return (*r)->name == data;
1330 * dev_get_regmap() - Obtain the regmap (if any) for a device
1332 * @dev: Device to retrieve the map for
1333 * @name: Optional name for the register map, usually NULL.
1335 * Returns the regmap for the device if one is present, or NULL. If
1336 * name is specified then it must match the name specified when
1337 * registering the device, if it is NULL then the first regmap found
1338 * will be used. Devices with multiple register maps are very rare,
1339 * generic code should normally not need to specify a name.
1341 struct regmap *dev_get_regmap(struct device *dev, const char *name)
1343 struct regmap **r = devres_find(dev, dev_get_regmap_release,
1344 dev_get_regmap_match, (void *)name);
1350 EXPORT_SYMBOL_GPL(dev_get_regmap);
1353 * regmap_get_device() - Obtain the device from a regmap
1355 * @map: Register map to operate on.
1357 * Returns the underlying device that the regmap has been created for.
1359 struct device *regmap_get_device(struct regmap *map)
1363 EXPORT_SYMBOL_GPL(regmap_get_device);
1365 static int _regmap_select_page(struct regmap *map, unsigned int *reg,
1366 struct regmap_range_node *range,
1367 unsigned int val_num)
1369 void *orig_work_buf;
1370 unsigned int win_offset;
1371 unsigned int win_page;
1375 win_offset = (*reg - range->range_min) % range->window_len;
1376 win_page = (*reg - range->range_min) / range->window_len;
1379 /* Bulk write shouldn't cross range boundary */
1380 if (*reg + val_num - 1 > range->range_max)
1383 /* ... or single page boundary */
1384 if (val_num > range->window_len - win_offset)
1388 /* It is possible to have selector register inside data window.
1389 In that case, selector register is located on every page and
1390 it needs no page switching, when accessed alone. */
1392 range->window_start + win_offset != range->selector_reg) {
1393 /* Use separate work_buf during page switching */
1394 orig_work_buf = map->work_buf;
1395 map->work_buf = map->selector_work_buf;
1397 ret = _regmap_update_bits(map, range->selector_reg,
1398 range->selector_mask,
1399 win_page << range->selector_shift,
1402 map->work_buf = orig_work_buf;
1408 *reg = range->window_start + win_offset;
1413 static void regmap_set_work_buf_flag_mask(struct regmap *map, int max_bytes,
1419 if (!mask || !map->work_buf)
1422 buf = map->work_buf;
1424 for (i = 0; i < max_bytes; i++)
1425 buf[i] |= (mask >> (8 * i)) & 0xff;
1428 int _regmap_raw_write(struct regmap *map, unsigned int reg,
1429 const void *val, size_t val_len)
1431 struct regmap_range_node *range;
1432 unsigned long flags;
1433 void *work_val = map->work_buf + map->format.reg_bytes +
1434 map->format.pad_bytes;
1436 int ret = -ENOTSUPP;
1442 /* Check for unwritable registers before we start */
1443 if (map->writeable_reg)
1444 for (i = 0; i < val_len / map->format.val_bytes; i++)
1445 if (!map->writeable_reg(map->dev,
1446 reg + regmap_get_offset(map, i)))
1449 if (!map->cache_bypass && map->format.parse_val) {
1451 int val_bytes = map->format.val_bytes;
1452 for (i = 0; i < val_len / val_bytes; i++) {
1453 ival = map->format.parse_val(val + (i * val_bytes));
1454 ret = regcache_write(map,
1455 reg + regmap_get_offset(map, i),
1459 "Error in caching of register: %x ret: %d\n",
1464 if (map->cache_only) {
1465 map->cache_dirty = true;
1470 range = _regmap_range_lookup(map, reg);
1472 int val_num = val_len / map->format.val_bytes;
1473 int win_offset = (reg - range->range_min) % range->window_len;
1474 int win_residue = range->window_len - win_offset;
1476 /* If the write goes beyond the end of the window split it */
1477 while (val_num > win_residue) {
1478 dev_dbg(map->dev, "Writing window %d/%zu\n",
1479 win_residue, val_len / map->format.val_bytes);
1480 ret = _regmap_raw_write(map, reg, val, win_residue *
1481 map->format.val_bytes);
1486 val_num -= win_residue;
1487 val += win_residue * map->format.val_bytes;
1488 val_len -= win_residue * map->format.val_bytes;
1490 win_offset = (reg - range->range_min) %
1492 win_residue = range->window_len - win_offset;
1495 ret = _regmap_select_page(map, ®, range, val_num);
1500 map->format.format_reg(map->work_buf, reg, map->reg_shift);
1501 regmap_set_work_buf_flag_mask(map, map->format.reg_bytes,
1502 map->write_flag_mask);
1505 * Essentially all I/O mechanisms will be faster with a single
1506 * buffer to write. Since register syncs often generate raw
1507 * writes of single registers optimise that case.
1509 if (val != work_val && val_len == map->format.val_bytes) {
1510 memcpy(work_val, val, map->format.val_bytes);
1514 if (map->async && map->bus->async_write) {
1515 struct regmap_async *async;
1517 trace_regmap_async_write_start(map, reg, val_len);
1519 spin_lock_irqsave(&map->async_lock, flags);
1520 async = list_first_entry_or_null(&map->async_free,
1521 struct regmap_async,
1524 list_del(&async->list);
1525 spin_unlock_irqrestore(&map->async_lock, flags);
1528 async = map->bus->async_alloc();
1532 async->work_buf = kzalloc(map->format.buf_size,
1533 GFP_KERNEL | GFP_DMA);
1534 if (!async->work_buf) {
1542 /* If the caller supplied the value we can use it safely. */
1543 memcpy(async->work_buf, map->work_buf, map->format.pad_bytes +
1544 map->format.reg_bytes + map->format.val_bytes);
1546 spin_lock_irqsave(&map->async_lock, flags);
1547 list_add_tail(&async->list, &map->async_list);
1548 spin_unlock_irqrestore(&map->async_lock, flags);
1550 if (val != work_val)
1551 ret = map->bus->async_write(map->bus_context,
1553 map->format.reg_bytes +
1554 map->format.pad_bytes,
1555 val, val_len, async);
1557 ret = map->bus->async_write(map->bus_context,
1559 map->format.reg_bytes +
1560 map->format.pad_bytes +
1561 val_len, NULL, 0, async);
1564 dev_err(map->dev, "Failed to schedule write: %d\n",
1567 spin_lock_irqsave(&map->async_lock, flags);
1568 list_move(&async->list, &map->async_free);
1569 spin_unlock_irqrestore(&map->async_lock, flags);
1575 trace_regmap_hw_write_start(map, reg, val_len / map->format.val_bytes);
1577 /* If we're doing a single register write we can probably just
1578 * send the work_buf directly, otherwise try to do a gather
1581 if (val == work_val)
1582 ret = map->bus->write(map->bus_context, map->work_buf,
1583 map->format.reg_bytes +
1584 map->format.pad_bytes +
1586 else if (map->bus->gather_write)
1587 ret = map->bus->gather_write(map->bus_context, map->work_buf,
1588 map->format.reg_bytes +
1589 map->format.pad_bytes,
1592 /* If that didn't work fall back on linearising by hand. */
1593 if (ret == -ENOTSUPP) {
1594 len = map->format.reg_bytes + map->format.pad_bytes + val_len;
1595 buf = kzalloc(len, GFP_KERNEL);
1599 memcpy(buf, map->work_buf, map->format.reg_bytes);
1600 memcpy(buf + map->format.reg_bytes + map->format.pad_bytes,
1602 ret = map->bus->write(map->bus_context, buf, len);
1605 } else if (ret != 0 && !map->cache_bypass && map->format.parse_val) {
1606 /* regcache_drop_region() takes lock that we already have,
1607 * thus call map->cache_ops->drop() directly
1609 if (map->cache_ops && map->cache_ops->drop)
1610 map->cache_ops->drop(map, reg, reg + 1);
1613 trace_regmap_hw_write_done(map, reg, val_len / map->format.val_bytes);
1619 * regmap_can_raw_write - Test if regmap_raw_write() is supported
1621 * @map: Map to check.
1623 bool regmap_can_raw_write(struct regmap *map)
1625 return map->bus && map->bus->write && map->format.format_val &&
1626 map->format.format_reg;
1628 EXPORT_SYMBOL_GPL(regmap_can_raw_write);
1631 * regmap_get_raw_read_max - Get the maximum size we can read
1633 * @map: Map to check.
1635 size_t regmap_get_raw_read_max(struct regmap *map)
1637 return map->max_raw_read;
1639 EXPORT_SYMBOL_GPL(regmap_get_raw_read_max);
1642 * regmap_get_raw_write_max - Get the maximum size we can read
1644 * @map: Map to check.
1646 size_t regmap_get_raw_write_max(struct regmap *map)
1648 return map->max_raw_write;
1650 EXPORT_SYMBOL_GPL(regmap_get_raw_write_max);
1652 static int _regmap_bus_formatted_write(void *context, unsigned int reg,
1656 struct regmap_range_node *range;
1657 struct regmap *map = context;
1659 WARN_ON(!map->bus || !map->format.format_write);
1661 range = _regmap_range_lookup(map, reg);
1663 ret = _regmap_select_page(map, ®, range, 1);
1668 map->format.format_write(map, reg, val);
1670 trace_regmap_hw_write_start(map, reg, 1);
1672 ret = map->bus->write(map->bus_context, map->work_buf,
1673 map->format.buf_size);
1675 trace_regmap_hw_write_done(map, reg, 1);
1680 static int _regmap_bus_reg_write(void *context, unsigned int reg,
1683 struct regmap *map = context;
1685 return map->bus->reg_write(map->bus_context, reg, val);
1688 static int _regmap_bus_raw_write(void *context, unsigned int reg,
1691 struct regmap *map = context;
1693 WARN_ON(!map->bus || !map->format.format_val);
1695 map->format.format_val(map->work_buf + map->format.reg_bytes
1696 + map->format.pad_bytes, val, 0);
1697 return _regmap_raw_write(map, reg,
1699 map->format.reg_bytes +
1700 map->format.pad_bytes,
1701 map->format.val_bytes);
1704 static inline void *_regmap_map_get_context(struct regmap *map)
1706 return (map->bus) ? map : map->bus_context;
1709 int _regmap_write(struct regmap *map, unsigned int reg,
1713 void *context = _regmap_map_get_context(map);
1715 if (!regmap_writeable(map, reg))
1718 if (!map->cache_bypass && !map->defer_caching) {
1719 ret = regcache_write(map, reg, val);
1722 if (map->cache_only) {
1723 map->cache_dirty = true;
1729 if (map->dev && strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
1730 dev_info(map->dev, "%x <= %x\n", reg, val);
1733 trace_regmap_reg_write(map, reg, val);
1735 return map->reg_write(context, reg, val);
1739 * regmap_write() - Write a value to a single register
1741 * @map: Register map to write to
1742 * @reg: Register to write to
1743 * @val: Value to be written
1745 * A value of zero will be returned on success, a negative errno will
1746 * be returned in error cases.
1748 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
1752 if (!IS_ALIGNED(reg, map->reg_stride))
1755 map->lock(map->lock_arg);
1757 ret = _regmap_write(map, reg, val);
1759 map->unlock(map->lock_arg);
1763 EXPORT_SYMBOL_GPL(regmap_write);
1766 * regmap_write_async() - Write a value to a single register asynchronously
1768 * @map: Register map to write to
1769 * @reg: Register to write to
1770 * @val: Value to be written
1772 * A value of zero will be returned on success, a negative errno will
1773 * be returned in error cases.
1775 int regmap_write_async(struct regmap *map, unsigned int reg, unsigned int val)
1779 if (!IS_ALIGNED(reg, map->reg_stride))
1782 map->lock(map->lock_arg);
1786 ret = _regmap_write(map, reg, val);
1790 map->unlock(map->lock_arg);
1794 EXPORT_SYMBOL_GPL(regmap_write_async);
1797 * regmap_raw_write() - Write raw values to one or more registers
1799 * @map: Register map to write to
1800 * @reg: Initial register to write to
1801 * @val: Block of data to be written, laid out for direct transmission to the
1803 * @val_len: Length of data pointed to by val.
1805 * This function is intended to be used for things like firmware
1806 * download where a large block of data needs to be transferred to the
1807 * device. No formatting will be done on the data provided.
1809 * A value of zero will be returned on success, a negative errno will
1810 * be returned in error cases.
1812 int regmap_raw_write(struct regmap *map, unsigned int reg,
1813 const void *val, size_t val_len)
1817 if (!regmap_can_raw_write(map))
1819 if (val_len % map->format.val_bytes)
1821 if (map->max_raw_write && map->max_raw_write > val_len)
1824 map->lock(map->lock_arg);
1826 ret = _regmap_raw_write(map, reg, val, val_len);
1828 map->unlock(map->lock_arg);
1832 EXPORT_SYMBOL_GPL(regmap_raw_write);
1835 * regmap_field_update_bits_base() - Perform a read/modify/write cycle a
1838 * @field: Register field to write to
1839 * @mask: Bitmask to change
1840 * @val: Value to be written
1841 * @change: Boolean indicating if a write was done
1842 * @async: Boolean indicating asynchronously
1843 * @force: Boolean indicating use force update
1845 * Perform a read/modify/write cycle on the register field with change,
1846 * async, force option.
1848 * A value of zero will be returned on success, a negative errno will
1849 * be returned in error cases.
1851 int regmap_field_update_bits_base(struct regmap_field *field,
1852 unsigned int mask, unsigned int val,
1853 bool *change, bool async, bool force)
1855 mask = (mask << field->shift) & field->mask;
1857 return regmap_update_bits_base(field->regmap, field->reg,
1858 mask, val << field->shift,
1859 change, async, force);
1861 EXPORT_SYMBOL_GPL(regmap_field_update_bits_base);
1864 * regmap_fields_update_bits_base() - Perform a read/modify/write cycle a
1865 * register field with port ID
1867 * @field: Register field to write to
1869 * @mask: Bitmask to change
1870 * @val: Value to be written
1871 * @change: Boolean indicating if a write was done
1872 * @async: Boolean indicating asynchronously
1873 * @force: Boolean indicating use force update
1875 * A value of zero will be returned on success, a negative errno will
1876 * be returned in error cases.
1878 int regmap_fields_update_bits_base(struct regmap_field *field, unsigned int id,
1879 unsigned int mask, unsigned int val,
1880 bool *change, bool async, bool force)
1882 if (id >= field->id_size)
1885 mask = (mask << field->shift) & field->mask;
1887 return regmap_update_bits_base(field->regmap,
1888 field->reg + (field->id_offset * id),
1889 mask, val << field->shift,
1890 change, async, force);
1892 EXPORT_SYMBOL_GPL(regmap_fields_update_bits_base);
1895 * regmap_bulk_write() - Write multiple registers to the device
1897 * @map: Register map to write to
1898 * @reg: First register to be write from
1899 * @val: Block of data to be written, in native register size for device
1900 * @val_count: Number of registers to write
1902 * This function is intended to be used for writing a large block of
1903 * data to the device either in single transfer or multiple transfer.
1905 * A value of zero will be returned on success, a negative errno will
1906 * be returned in error cases.
1908 int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
1912 size_t val_bytes = map->format.val_bytes;
1913 size_t total_size = val_bytes * val_count;
1915 if (!IS_ALIGNED(reg, map->reg_stride))
1919 * Some devices don't support bulk write, for
1920 * them we have a series of single write operations in the first two if
1923 * The first if block is used for memory mapped io. It does not allow
1924 * val_bytes of 3 for example.
1925 * The second one is for busses that do not provide raw I/O.
1926 * The third one is used for busses which do not have these limitations
1927 * and can write arbitrary value lengths.
1930 map->lock(map->lock_arg);
1931 for (i = 0; i < val_count; i++) {
1934 switch (val_bytes) {
1936 ival = *(u8 *)(val + (i * val_bytes));
1939 ival = *(u16 *)(val + (i * val_bytes));
1942 ival = *(u32 *)(val + (i * val_bytes));
1946 ival = *(u64 *)(val + (i * val_bytes));
1954 ret = _regmap_write(map,
1955 reg + regmap_get_offset(map, i),
1961 map->unlock(map->lock_arg);
1962 } else if (map->bus && !map->format.parse_inplace) {
1964 const u16 *u16 = val;
1965 const u32 *u32 = val;
1968 for (i = 0; i < val_count; i++) {
1969 switch (map->format.val_bytes) {
1983 ret = regmap_write(map, reg + (i * map->reg_stride),
1988 } else if (map->use_single_write ||
1989 (map->max_raw_write && map->max_raw_write < total_size)) {
1990 int chunk_stride = map->reg_stride;
1991 size_t chunk_size = val_bytes;
1992 size_t chunk_count = val_count;
1994 if (!map->use_single_write) {
1995 chunk_size = map->max_raw_write;
1996 if (chunk_size % val_bytes)
1997 chunk_size -= chunk_size % val_bytes;
1998 chunk_count = total_size / chunk_size;
1999 chunk_stride *= chunk_size / val_bytes;
2002 map->lock(map->lock_arg);
2003 /* Write as many bytes as possible with chunk_size */
2004 for (i = 0; i < chunk_count; i++) {
2005 ret = _regmap_raw_write(map,
2006 reg + (i * chunk_stride),
2007 val + (i * chunk_size),
2013 /* Write remaining bytes */
2014 if (!ret && chunk_size * i < total_size) {
2015 ret = _regmap_raw_write(map, reg + (i * chunk_stride),
2016 val + (i * chunk_size),
2017 total_size - i * chunk_size);
2019 map->unlock(map->lock_arg);
2026 wval = kmemdup(val, val_count * val_bytes, map->alloc_flags);
2028 dev_err(map->dev, "Error in memory allocation\n");
2031 for (i = 0; i < val_count * val_bytes; i += val_bytes)
2032 map->format.parse_inplace(wval + i);
2034 map->lock(map->lock_arg);
2035 ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count);
2036 map->unlock(map->lock_arg);
2042 EXPORT_SYMBOL_GPL(regmap_bulk_write);
2045 * _regmap_raw_multi_reg_write()
2047 * the (register,newvalue) pairs in regs have not been formatted, but
2048 * they are all in the same page and have been changed to being page
2049 * relative. The page register has been written if that was necessary.
2051 static int _regmap_raw_multi_reg_write(struct regmap *map,
2052 const struct reg_sequence *regs,
2059 size_t val_bytes = map->format.val_bytes;
2060 size_t reg_bytes = map->format.reg_bytes;
2061 size_t pad_bytes = map->format.pad_bytes;
2062 size_t pair_size = reg_bytes + pad_bytes + val_bytes;
2063 size_t len = pair_size * num_regs;
2068 buf = kzalloc(len, GFP_KERNEL);
2072 /* We have to linearise by hand. */
2076 for (i = 0; i < num_regs; i++) {
2077 unsigned int reg = regs[i].reg;
2078 unsigned int val = regs[i].def;
2079 trace_regmap_hw_write_start(map, reg, 1);
2080 map->format.format_reg(u8, reg, map->reg_shift);
2081 u8 += reg_bytes + pad_bytes;
2082 map->format.format_val(u8, val, 0);
2086 *u8 |= map->write_flag_mask;
2088 ret = map->bus->write(map->bus_context, buf, len);
2092 for (i = 0; i < num_regs; i++) {
2093 int reg = regs[i].reg;
2094 trace_regmap_hw_write_done(map, reg, 1);
2099 static unsigned int _regmap_register_page(struct regmap *map,
2101 struct regmap_range_node *range)
2103 unsigned int win_page = (reg - range->range_min) / range->window_len;
2108 static int _regmap_range_multi_paged_reg_write(struct regmap *map,
2109 struct reg_sequence *regs,
2114 struct reg_sequence *base;
2115 unsigned int this_page = 0;
2116 unsigned int page_change = 0;
2118 * the set of registers are not neccessarily in order, but
2119 * since the order of write must be preserved this algorithm
2120 * chops the set each time the page changes. This also applies
2121 * if there is a delay required at any point in the sequence.
2124 for (i = 0, n = 0; i < num_regs; i++, n++) {
2125 unsigned int reg = regs[i].reg;
2126 struct regmap_range_node *range;
2128 range = _regmap_range_lookup(map, reg);
2130 unsigned int win_page = _regmap_register_page(map, reg,
2134 this_page = win_page;
2135 if (win_page != this_page) {
2136 this_page = win_page;
2141 /* If we have both a page change and a delay make sure to
2142 * write the regs and apply the delay before we change the
2146 if (page_change || regs[i].delay_us) {
2148 /* For situations where the first write requires
2149 * a delay we need to make sure we don't call
2150 * raw_multi_reg_write with n=0
2151 * This can't occur with page breaks as we
2152 * never write on the first iteration
2154 if (regs[i].delay_us && i == 0)
2157 ret = _regmap_raw_multi_reg_write(map, base, n);
2161 if (regs[i].delay_us)
2162 udelay(regs[i].delay_us);
2168 ret = _regmap_select_page(map,
2181 return _regmap_raw_multi_reg_write(map, base, n);
2185 static int _regmap_multi_reg_write(struct regmap *map,
2186 const struct reg_sequence *regs,
2192 if (!map->can_multi_write) {
2193 for (i = 0; i < num_regs; i++) {
2194 ret = _regmap_write(map, regs[i].reg, regs[i].def);
2198 if (regs[i].delay_us)
2199 udelay(regs[i].delay_us);
2204 if (!map->format.parse_inplace)
2207 if (map->writeable_reg)
2208 for (i = 0; i < num_regs; i++) {
2209 int reg = regs[i].reg;
2210 if (!map->writeable_reg(map->dev, reg))
2212 if (!IS_ALIGNED(reg, map->reg_stride))
2216 if (!map->cache_bypass) {
2217 for (i = 0; i < num_regs; i++) {
2218 unsigned int val = regs[i].def;
2219 unsigned int reg = regs[i].reg;
2220 ret = regcache_write(map, reg, val);
2223 "Error in caching of register: %x ret: %d\n",
2228 if (map->cache_only) {
2229 map->cache_dirty = true;
2236 for (i = 0; i < num_regs; i++) {
2237 unsigned int reg = regs[i].reg;
2238 struct regmap_range_node *range;
2240 /* Coalesce all the writes between a page break or a delay
2243 range = _regmap_range_lookup(map, reg);
2244 if (range || regs[i].delay_us) {
2245 size_t len = sizeof(struct reg_sequence)*num_regs;
2246 struct reg_sequence *base = kmemdup(regs, len,
2250 ret = _regmap_range_multi_paged_reg_write(map, base,
2257 return _regmap_raw_multi_reg_write(map, regs, num_regs);
2261 * regmap_multi_reg_write() - Write multiple registers to the device
2263 * @map: Register map to write to
2264 * @regs: Array of structures containing register,value to be written
2265 * @num_regs: Number of registers to write
2267 * Write multiple registers to the device where the set of register, value
2268 * pairs are supplied in any order, possibly not all in a single range.
2270 * The 'normal' block write mode will send ultimately send data on the
2271 * target bus as R,V1,V2,V3,..,Vn where successively higher registers are
2272 * addressed. However, this alternative block multi write mode will send
2273 * the data as R1,V1,R2,V2,..,Rn,Vn on the target bus. The target device
2274 * must of course support the mode.
2276 * A value of zero will be returned on success, a negative errno will be
2277 * returned in error cases.
2279 int regmap_multi_reg_write(struct regmap *map, const struct reg_sequence *regs,
2284 map->lock(map->lock_arg);
2286 ret = _regmap_multi_reg_write(map, regs, num_regs);
2288 map->unlock(map->lock_arg);
2292 EXPORT_SYMBOL_GPL(regmap_multi_reg_write);
2295 * regmap_multi_reg_write_bypassed() - Write multiple registers to the
2296 * device but not the cache
2298 * @map: Register map to write to
2299 * @regs: Array of structures containing register,value to be written
2300 * @num_regs: Number of registers to write
2302 * Write multiple registers to the device but not the cache where the set
2303 * of register are supplied in any order.
2305 * This function is intended to be used for writing a large block of data
2306 * atomically to the device in single transfer for those I2C client devices
2307 * that implement this alternative block write mode.
2309 * A value of zero will be returned on success, a negative errno will
2310 * be returned in error cases.
2312 int regmap_multi_reg_write_bypassed(struct regmap *map,
2313 const struct reg_sequence *regs,
2319 map->lock(map->lock_arg);
2321 bypass = map->cache_bypass;
2322 map->cache_bypass = true;
2324 ret = _regmap_multi_reg_write(map, regs, num_regs);
2326 map->cache_bypass = bypass;
2328 map->unlock(map->lock_arg);
2332 EXPORT_SYMBOL_GPL(regmap_multi_reg_write_bypassed);
2335 * regmap_raw_write_async() - Write raw values to one or more registers
2338 * @map: Register map to write to
2339 * @reg: Initial register to write to
2340 * @val: Block of data to be written, laid out for direct transmission to the
2341 * device. Must be valid until regmap_async_complete() is called.
2342 * @val_len: Length of data pointed to by val.
2344 * This function is intended to be used for things like firmware
2345 * download where a large block of data needs to be transferred to the
2346 * device. No formatting will be done on the data provided.
2348 * If supported by the underlying bus the write will be scheduled
2349 * asynchronously, helping maximise I/O speed on higher speed buses
2350 * like SPI. regmap_async_complete() can be called to ensure that all
2351 * asynchrnous writes have been completed.
2353 * A value of zero will be returned on success, a negative errno will
2354 * be returned in error cases.
2356 int regmap_raw_write_async(struct regmap *map, unsigned int reg,
2357 const void *val, size_t val_len)
2361 if (val_len % map->format.val_bytes)
2363 if (!IS_ALIGNED(reg, map->reg_stride))
2366 map->lock(map->lock_arg);
2370 ret = _regmap_raw_write(map, reg, val, val_len);
2374 map->unlock(map->lock_arg);
2378 EXPORT_SYMBOL_GPL(regmap_raw_write_async);
2380 static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
2381 unsigned int val_len)
2383 struct regmap_range_node *range;
2388 if (!map->bus || !map->bus->read)
2391 range = _regmap_range_lookup(map, reg);
2393 ret = _regmap_select_page(map, ®, range,
2394 val_len / map->format.val_bytes);
2399 map->format.format_reg(map->work_buf, reg, map->reg_shift);
2400 regmap_set_work_buf_flag_mask(map, map->format.reg_bytes,
2401 map->read_flag_mask);
2402 trace_regmap_hw_read_start(map, reg, val_len / map->format.val_bytes);
2404 ret = map->bus->read(map->bus_context, map->work_buf,
2405 map->format.reg_bytes + map->format.pad_bytes,
2408 trace_regmap_hw_read_done(map, reg, val_len / map->format.val_bytes);
2413 static int _regmap_bus_reg_read(void *context, unsigned int reg,
2416 struct regmap *map = context;
2418 return map->bus->reg_read(map->bus_context, reg, val);
2421 static int _regmap_bus_read(void *context, unsigned int reg,
2425 struct regmap *map = context;
2427 if (!map->format.parse_val)
2430 ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
2432 *val = map->format.parse_val(map->work_buf);
2437 static int _regmap_read(struct regmap *map, unsigned int reg,
2441 void *context = _regmap_map_get_context(map);
2443 if (!map->cache_bypass) {
2444 ret = regcache_read(map, reg, val);
2449 if (map->cache_only)
2452 if (!regmap_readable(map, reg))
2455 ret = map->reg_read(context, reg, val);
2458 if (map->dev && strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
2459 dev_info(map->dev, "%x => %x\n", reg, *val);
2462 trace_regmap_reg_read(map, reg, *val);
2464 if (!map->cache_bypass)
2465 regcache_write(map, reg, *val);
2472 * regmap_read() - Read a value from a single register
2474 * @map: Register map to read from
2475 * @reg: Register to be read from
2476 * @val: Pointer to store read value
2478 * A value of zero will be returned on success, a negative errno will
2479 * be returned in error cases.
2481 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
2485 if (!IS_ALIGNED(reg, map->reg_stride))
2488 map->lock(map->lock_arg);
2490 ret = _regmap_read(map, reg, val);
2492 map->unlock(map->lock_arg);
2496 EXPORT_SYMBOL_GPL(regmap_read);
2499 * regmap_raw_read() - Read raw data from the device
2501 * @map: Register map to read from
2502 * @reg: First register to be read from
2503 * @val: Pointer to store read value
2504 * @val_len: Size of data to read
2506 * A value of zero will be returned on success, a negative errno will
2507 * be returned in error cases.
2509 int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
2512 size_t val_bytes = map->format.val_bytes;
2513 size_t val_count = val_len / val_bytes;
2519 if (val_len % map->format.val_bytes)
2521 if (!IS_ALIGNED(reg, map->reg_stride))
2526 map->lock(map->lock_arg);
2528 if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
2529 map->cache_type == REGCACHE_NONE) {
2530 if (!map->bus->read) {
2534 if (map->max_raw_read && map->max_raw_read < val_len) {
2539 /* Physical block read if there's no cache involved */
2540 ret = _regmap_raw_read(map, reg, val, val_len);
2543 /* Otherwise go word by word for the cache; should be low
2544 * cost as we expect to hit the cache.
2546 for (i = 0; i < val_count; i++) {
2547 ret = _regmap_read(map, reg + regmap_get_offset(map, i),
2552 map->format.format_val(val + (i * val_bytes), v, 0);
2557 map->unlock(map->lock_arg);
2561 EXPORT_SYMBOL_GPL(regmap_raw_read);
2564 * regmap_field_read() - Read a value to a single register field
2566 * @field: Register field to read from
2567 * @val: Pointer to store read value
2569 * A value of zero will be returned on success, a negative errno will
2570 * be returned in error cases.
2572 int regmap_field_read(struct regmap_field *field, unsigned int *val)
2575 unsigned int reg_val;
2576 ret = regmap_read(field->regmap, field->reg, ®_val);
2580 reg_val &= field->mask;
2581 reg_val >>= field->shift;
2586 EXPORT_SYMBOL_GPL(regmap_field_read);
2589 * regmap_fields_read() - Read a value to a single register field with port ID
2591 * @field: Register field to read from
2593 * @val: Pointer to store read value
2595 * A value of zero will be returned on success, a negative errno will
2596 * be returned in error cases.
2598 int regmap_fields_read(struct regmap_field *field, unsigned int id,
2602 unsigned int reg_val;
2604 if (id >= field->id_size)
2607 ret = regmap_read(field->regmap,
2608 field->reg + (field->id_offset * id),
2613 reg_val &= field->mask;
2614 reg_val >>= field->shift;
2619 EXPORT_SYMBOL_GPL(regmap_fields_read);
2622 * regmap_bulk_read() - Read multiple registers from the device
2624 * @map: Register map to read from
2625 * @reg: First register to be read from
2626 * @val: Pointer to store read value, in native register size for device
2627 * @val_count: Number of registers to read
2629 * A value of zero will be returned on success, a negative errno will
2630 * be returned in error cases.
2632 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
2636 size_t val_bytes = map->format.val_bytes;
2637 bool vol = regmap_volatile_range(map, reg, val_count);
2639 if (!IS_ALIGNED(reg, map->reg_stride))
2642 if (map->bus && map->format.parse_inplace && (vol || map->cache_type == REGCACHE_NONE)) {
2644 * Some devices does not support bulk read, for
2645 * them we have a series of single read operations.
2647 size_t total_size = val_bytes * val_count;
2649 if (!map->use_single_read &&
2650 (!map->max_raw_read || map->max_raw_read > total_size)) {
2651 ret = regmap_raw_read(map, reg, val,
2652 val_bytes * val_count);
2657 * Some devices do not support bulk read or do not
2658 * support large bulk reads, for them we have a series
2659 * of read operations.
2661 int chunk_stride = map->reg_stride;
2662 size_t chunk_size = val_bytes;
2663 size_t chunk_count = val_count;
2665 if (!map->use_single_read) {
2666 chunk_size = map->max_raw_read;
2667 if (chunk_size % val_bytes)
2668 chunk_size -= chunk_size % val_bytes;
2669 chunk_count = total_size / chunk_size;
2670 chunk_stride *= chunk_size / val_bytes;
2673 /* Read bytes that fit into a multiple of chunk_size */
2674 for (i = 0; i < chunk_count; i++) {
2675 ret = regmap_raw_read(map,
2676 reg + (i * chunk_stride),
2677 val + (i * chunk_size),
2683 /* Read remaining bytes */
2684 if (chunk_size * i < total_size) {
2685 ret = regmap_raw_read(map,
2686 reg + (i * chunk_stride),
2687 val + (i * chunk_size),
2688 total_size - i * chunk_size);
2694 for (i = 0; i < val_count * val_bytes; i += val_bytes)
2695 map->format.parse_inplace(val + i);
2697 for (i = 0; i < val_count; i++) {
2699 ret = regmap_read(map, reg + regmap_get_offset(map, i),
2704 if (map->format.format_val) {
2705 map->format.format_val(val + (i * val_bytes), ival, 0);
2707 /* Devices providing read and write
2708 * operations can use the bulk I/O
2709 * functions if they define a val_bytes,
2710 * we assume that the values are native
2720 switch (map->format.val_bytes) {
2744 EXPORT_SYMBOL_GPL(regmap_bulk_read);
2746 static int _regmap_update_bits(struct regmap *map, unsigned int reg,
2747 unsigned int mask, unsigned int val,
2748 bool *change, bool force_write)
2751 unsigned int tmp, orig;
2756 if (regmap_volatile(map, reg) && map->reg_update_bits) {
2757 ret = map->reg_update_bits(map->bus_context, reg, mask, val);
2758 if (ret == 0 && change)
2761 ret = _regmap_read(map, reg, &orig);
2768 if (force_write || (tmp != orig)) {
2769 ret = _regmap_write(map, reg, tmp);
2770 if (ret == 0 && change)
2779 * regmap_update_bits_base() - Perform a read/modify/write cycle on a register
2781 * @map: Register map to update
2782 * @reg: Register to update
2783 * @mask: Bitmask to change
2784 * @val: New value for bitmask
2785 * @change: Boolean indicating if a write was done
2786 * @async: Boolean indicating asynchronously
2787 * @force: Boolean indicating use force update
2789 * Perform a read/modify/write cycle on a register map with change, async, force
2794 * With most buses the read must be done synchronously so this is most useful
2795 * for devices with a cache which do not need to interact with the hardware to
2796 * determine the current register value.
2798 * Returns zero for success, a negative number on error.
2800 int regmap_update_bits_base(struct regmap *map, unsigned int reg,
2801 unsigned int mask, unsigned int val,
2802 bool *change, bool async, bool force)
2806 map->lock(map->lock_arg);
2810 ret = _regmap_update_bits(map, reg, mask, val, change, force);
2814 map->unlock(map->lock_arg);
2818 EXPORT_SYMBOL_GPL(regmap_update_bits_base);
2820 void regmap_async_complete_cb(struct regmap_async *async, int ret)
2822 struct regmap *map = async->map;
2825 trace_regmap_async_io_complete(map);
2827 spin_lock(&map->async_lock);
2828 list_move(&async->list, &map->async_free);
2829 wake = list_empty(&map->async_list);
2832 map->async_ret = ret;
2834 spin_unlock(&map->async_lock);
2837 wake_up(&map->async_waitq);
2839 EXPORT_SYMBOL_GPL(regmap_async_complete_cb);
2841 static int regmap_async_is_done(struct regmap *map)
2843 unsigned long flags;
2846 spin_lock_irqsave(&map->async_lock, flags);
2847 ret = list_empty(&map->async_list);
2848 spin_unlock_irqrestore(&map->async_lock, flags);
2854 * regmap_async_complete - Ensure all asynchronous I/O has completed.
2856 * @map: Map to operate on.
2858 * Blocks until any pending asynchronous I/O has completed. Returns
2859 * an error code for any failed I/O operations.
2861 int regmap_async_complete(struct regmap *map)
2863 unsigned long flags;
2866 /* Nothing to do with no async support */
2867 if (!map->bus || !map->bus->async_write)
2870 trace_regmap_async_complete_start(map);
2872 wait_event(map->async_waitq, regmap_async_is_done(map));
2874 spin_lock_irqsave(&map->async_lock, flags);
2875 ret = map->async_ret;
2877 spin_unlock_irqrestore(&map->async_lock, flags);
2879 trace_regmap_async_complete_done(map);
2883 EXPORT_SYMBOL_GPL(regmap_async_complete);
2886 * regmap_register_patch - Register and apply register updates to be applied
2887 * on device initialistion
2889 * @map: Register map to apply updates to.
2890 * @regs: Values to update.
2891 * @num_regs: Number of entries in regs.
2893 * Register a set of register updates to be applied to the device
2894 * whenever the device registers are synchronised with the cache and
2895 * apply them immediately. Typically this is used to apply
2896 * corrections to be applied to the device defaults on startup, such
2897 * as the updates some vendors provide to undocumented registers.
2899 * The caller must ensure that this function cannot be called
2900 * concurrently with either itself or regcache_sync().
2902 int regmap_register_patch(struct regmap *map, const struct reg_sequence *regs,
2905 struct reg_sequence *p;
2909 if (WARN_ONCE(num_regs <= 0, "invalid registers number (%d)\n",
2913 p = krealloc(map->patch,
2914 sizeof(struct reg_sequence) * (map->patch_regs + num_regs),
2917 memcpy(p + map->patch_regs, regs, num_regs * sizeof(*regs));
2919 map->patch_regs += num_regs;
2924 map->lock(map->lock_arg);
2926 bypass = map->cache_bypass;
2928 map->cache_bypass = true;
2931 ret = _regmap_multi_reg_write(map, regs, num_regs);
2934 map->cache_bypass = bypass;
2936 map->unlock(map->lock_arg);
2938 regmap_async_complete(map);
2942 EXPORT_SYMBOL_GPL(regmap_register_patch);
2945 * regmap_get_val_bytes() - Report the size of a register value
2947 * @map: Register map to operate on.
2949 * Report the size of a register value, mainly intended to for use by
2950 * generic infrastructure built on top of regmap.
2952 int regmap_get_val_bytes(struct regmap *map)
2954 if (map->format.format_write)
2957 return map->format.val_bytes;
2959 EXPORT_SYMBOL_GPL(regmap_get_val_bytes);
2962 * regmap_get_max_register() - Report the max register value
2964 * @map: Register map to operate on.
2966 * Report the max register value, mainly intended to for use by
2967 * generic infrastructure built on top of regmap.
2969 int regmap_get_max_register(struct regmap *map)
2971 return map->max_register ? map->max_register : -EINVAL;
2973 EXPORT_SYMBOL_GPL(regmap_get_max_register);
2976 * regmap_get_reg_stride() - Report the register address stride
2978 * @map: Register map to operate on.
2980 * Report the register address stride, mainly intended to for use by
2981 * generic infrastructure built on top of regmap.
2983 int regmap_get_reg_stride(struct regmap *map)
2985 return map->reg_stride;
2987 EXPORT_SYMBOL_GPL(regmap_get_reg_stride);
2989 int regmap_parse_val(struct regmap *map, const void *buf,
2992 if (!map->format.parse_val)
2995 *val = map->format.parse_val(buf);
2999 EXPORT_SYMBOL_GPL(regmap_parse_val);
3001 static int __init regmap_initcall(void)
3003 regmap_debugfs_initcall();
3007 postcore_initcall(regmap_initcall);