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 hwspin_lock_free(map->hwlock);
1123 return ERR_PTR(ret);
1125 EXPORT_SYMBOL_GPL(__regmap_init);
1127 static void devm_regmap_release(struct device *dev, void *res)
1129 regmap_exit(*(struct regmap **)res);
1132 struct regmap *__devm_regmap_init(struct device *dev,
1133 const struct regmap_bus *bus,
1135 const struct regmap_config *config,
1136 struct lock_class_key *lock_key,
1137 const char *lock_name)
1139 struct regmap **ptr, *regmap;
1141 ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL);
1143 return ERR_PTR(-ENOMEM);
1145 regmap = __regmap_init(dev, bus, bus_context, config,
1146 lock_key, lock_name);
1147 if (!IS_ERR(regmap)) {
1149 devres_add(dev, ptr);
1156 EXPORT_SYMBOL_GPL(__devm_regmap_init);
1158 static void regmap_field_init(struct regmap_field *rm_field,
1159 struct regmap *regmap, struct reg_field reg_field)
1161 rm_field->regmap = regmap;
1162 rm_field->reg = reg_field.reg;
1163 rm_field->shift = reg_field.lsb;
1164 rm_field->mask = GENMASK(reg_field.msb, reg_field.lsb);
1165 rm_field->id_size = reg_field.id_size;
1166 rm_field->id_offset = reg_field.id_offset;
1170 * devm_regmap_field_alloc() - Allocate and initialise a register field.
1172 * @dev: Device that will be interacted with
1173 * @regmap: regmap bank in which this register field is located.
1174 * @reg_field: Register field with in the bank.
1176 * The return value will be an ERR_PTR() on error or a valid pointer
1177 * to a struct regmap_field. The regmap_field will be automatically freed
1178 * by the device management code.
1180 struct regmap_field *devm_regmap_field_alloc(struct device *dev,
1181 struct regmap *regmap, struct reg_field reg_field)
1183 struct regmap_field *rm_field = devm_kzalloc(dev,
1184 sizeof(*rm_field), GFP_KERNEL);
1186 return ERR_PTR(-ENOMEM);
1188 regmap_field_init(rm_field, regmap, reg_field);
1193 EXPORT_SYMBOL_GPL(devm_regmap_field_alloc);
1196 * devm_regmap_field_free() - Free a register field allocated using
1197 * devm_regmap_field_alloc.
1199 * @dev: Device that will be interacted with
1200 * @field: regmap field which should be freed.
1202 * Free register field allocated using devm_regmap_field_alloc(). Usually
1203 * drivers need not call this function, as the memory allocated via devm
1204 * will be freed as per device-driver life-cyle.
1206 void devm_regmap_field_free(struct device *dev,
1207 struct regmap_field *field)
1209 devm_kfree(dev, field);
1211 EXPORT_SYMBOL_GPL(devm_regmap_field_free);
1214 * regmap_field_alloc() - Allocate and initialise a register field.
1216 * @regmap: regmap bank in which this register field is located.
1217 * @reg_field: Register field with in the bank.
1219 * The return value will be an ERR_PTR() on error or a valid pointer
1220 * to a struct regmap_field. The regmap_field should be freed by the
1221 * user once its finished working with it using regmap_field_free().
1223 struct regmap_field *regmap_field_alloc(struct regmap *regmap,
1224 struct reg_field reg_field)
1226 struct regmap_field *rm_field = kzalloc(sizeof(*rm_field), GFP_KERNEL);
1229 return ERR_PTR(-ENOMEM);
1231 regmap_field_init(rm_field, regmap, reg_field);
1235 EXPORT_SYMBOL_GPL(regmap_field_alloc);
1238 * regmap_field_free() - Free register field allocated using
1239 * regmap_field_alloc.
1241 * @field: regmap field which should be freed.
1243 void regmap_field_free(struct regmap_field *field)
1247 EXPORT_SYMBOL_GPL(regmap_field_free);
1250 * regmap_reinit_cache() - Reinitialise the current register cache
1252 * @map: Register map to operate on.
1253 * @config: New configuration. Only the cache data will be used.
1255 * Discard any existing register cache for the map and initialize a
1256 * new cache. This can be used to restore the cache to defaults or to
1257 * update the cache configuration to reflect runtime discovery of the
1260 * No explicit locking is done here, the user needs to ensure that
1261 * this function will not race with other calls to regmap.
1263 int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
1266 regmap_debugfs_exit(map);
1268 map->max_register = config->max_register;
1269 map->writeable_reg = config->writeable_reg;
1270 map->readable_reg = config->readable_reg;
1271 map->volatile_reg = config->volatile_reg;
1272 map->precious_reg = config->precious_reg;
1273 map->cache_type = config->cache_type;
1275 regmap_debugfs_init(map, config->name);
1277 map->cache_bypass = false;
1278 map->cache_only = false;
1280 return regcache_init(map, config);
1282 EXPORT_SYMBOL_GPL(regmap_reinit_cache);
1285 * regmap_exit() - Free a previously allocated register map
1287 * @map: Register map to operate on.
1289 void regmap_exit(struct regmap *map)
1291 struct regmap_async *async;
1294 regmap_debugfs_exit(map);
1295 regmap_range_exit(map);
1296 if (map->bus && map->bus->free_context)
1297 map->bus->free_context(map->bus_context);
1298 kfree(map->work_buf);
1299 while (!list_empty(&map->async_free)) {
1300 async = list_first_entry_or_null(&map->async_free,
1301 struct regmap_async,
1303 list_del(&async->list);
1304 kfree(async->work_buf);
1309 EXPORT_SYMBOL_GPL(regmap_exit);
1311 static int dev_get_regmap_match(struct device *dev, void *res, void *data)
1313 struct regmap **r = res;
1319 /* If the user didn't specify a name match any */
1321 return (*r)->name == data;
1327 * dev_get_regmap() - Obtain the regmap (if any) for a device
1329 * @dev: Device to retrieve the map for
1330 * @name: Optional name for the register map, usually NULL.
1332 * Returns the regmap for the device if one is present, or NULL. If
1333 * name is specified then it must match the name specified when
1334 * registering the device, if it is NULL then the first regmap found
1335 * will be used. Devices with multiple register maps are very rare,
1336 * generic code should normally not need to specify a name.
1338 struct regmap *dev_get_regmap(struct device *dev, const char *name)
1340 struct regmap **r = devres_find(dev, dev_get_regmap_release,
1341 dev_get_regmap_match, (void *)name);
1347 EXPORT_SYMBOL_GPL(dev_get_regmap);
1350 * regmap_get_device() - Obtain the device from a regmap
1352 * @map: Register map to operate on.
1354 * Returns the underlying device that the regmap has been created for.
1356 struct device *regmap_get_device(struct regmap *map)
1360 EXPORT_SYMBOL_GPL(regmap_get_device);
1362 static int _regmap_select_page(struct regmap *map, unsigned int *reg,
1363 struct regmap_range_node *range,
1364 unsigned int val_num)
1366 void *orig_work_buf;
1367 unsigned int win_offset;
1368 unsigned int win_page;
1372 win_offset = (*reg - range->range_min) % range->window_len;
1373 win_page = (*reg - range->range_min) / range->window_len;
1376 /* Bulk write shouldn't cross range boundary */
1377 if (*reg + val_num - 1 > range->range_max)
1380 /* ... or single page boundary */
1381 if (val_num > range->window_len - win_offset)
1385 /* It is possible to have selector register inside data window.
1386 In that case, selector register is located on every page and
1387 it needs no page switching, when accessed alone. */
1389 range->window_start + win_offset != range->selector_reg) {
1390 /* Use separate work_buf during page switching */
1391 orig_work_buf = map->work_buf;
1392 map->work_buf = map->selector_work_buf;
1394 ret = _regmap_update_bits(map, range->selector_reg,
1395 range->selector_mask,
1396 win_page << range->selector_shift,
1399 map->work_buf = orig_work_buf;
1405 *reg = range->window_start + win_offset;
1410 static void regmap_set_work_buf_flag_mask(struct regmap *map, int max_bytes,
1416 if (!mask || !map->work_buf)
1419 buf = map->work_buf;
1421 for (i = 0; i < max_bytes; i++)
1422 buf[i] |= (mask >> (8 * i)) & 0xff;
1425 int _regmap_raw_write(struct regmap *map, unsigned int reg,
1426 const void *val, size_t val_len)
1428 struct regmap_range_node *range;
1429 unsigned long flags;
1430 void *work_val = map->work_buf + map->format.reg_bytes +
1431 map->format.pad_bytes;
1433 int ret = -ENOTSUPP;
1439 /* Check for unwritable registers before we start */
1440 if (map->writeable_reg)
1441 for (i = 0; i < val_len / map->format.val_bytes; i++)
1442 if (!map->writeable_reg(map->dev,
1443 reg + regmap_get_offset(map, i)))
1446 if (!map->cache_bypass && map->format.parse_val) {
1448 int val_bytes = map->format.val_bytes;
1449 for (i = 0; i < val_len / val_bytes; i++) {
1450 ival = map->format.parse_val(val + (i * val_bytes));
1451 ret = regcache_write(map,
1452 reg + regmap_get_offset(map, i),
1456 "Error in caching of register: %x ret: %d\n",
1461 if (map->cache_only) {
1462 map->cache_dirty = true;
1467 range = _regmap_range_lookup(map, reg);
1469 int val_num = val_len / map->format.val_bytes;
1470 int win_offset = (reg - range->range_min) % range->window_len;
1471 int win_residue = range->window_len - win_offset;
1473 /* If the write goes beyond the end of the window split it */
1474 while (val_num > win_residue) {
1475 dev_dbg(map->dev, "Writing window %d/%zu\n",
1476 win_residue, val_len / map->format.val_bytes);
1477 ret = _regmap_raw_write(map, reg, val, win_residue *
1478 map->format.val_bytes);
1483 val_num -= win_residue;
1484 val += win_residue * map->format.val_bytes;
1485 val_len -= win_residue * map->format.val_bytes;
1487 win_offset = (reg - range->range_min) %
1489 win_residue = range->window_len - win_offset;
1492 ret = _regmap_select_page(map, ®, range, val_num);
1497 map->format.format_reg(map->work_buf, reg, map->reg_shift);
1498 regmap_set_work_buf_flag_mask(map, map->format.reg_bytes,
1499 map->write_flag_mask);
1502 * Essentially all I/O mechanisms will be faster with a single
1503 * buffer to write. Since register syncs often generate raw
1504 * writes of single registers optimise that case.
1506 if (val != work_val && val_len == map->format.val_bytes) {
1507 memcpy(work_val, val, map->format.val_bytes);
1511 if (map->async && map->bus->async_write) {
1512 struct regmap_async *async;
1514 trace_regmap_async_write_start(map, reg, val_len);
1516 spin_lock_irqsave(&map->async_lock, flags);
1517 async = list_first_entry_or_null(&map->async_free,
1518 struct regmap_async,
1521 list_del(&async->list);
1522 spin_unlock_irqrestore(&map->async_lock, flags);
1525 async = map->bus->async_alloc();
1529 async->work_buf = kzalloc(map->format.buf_size,
1530 GFP_KERNEL | GFP_DMA);
1531 if (!async->work_buf) {
1539 /* If the caller supplied the value we can use it safely. */
1540 memcpy(async->work_buf, map->work_buf, map->format.pad_bytes +
1541 map->format.reg_bytes + map->format.val_bytes);
1543 spin_lock_irqsave(&map->async_lock, flags);
1544 list_add_tail(&async->list, &map->async_list);
1545 spin_unlock_irqrestore(&map->async_lock, flags);
1547 if (val != work_val)
1548 ret = map->bus->async_write(map->bus_context,
1550 map->format.reg_bytes +
1551 map->format.pad_bytes,
1552 val, val_len, async);
1554 ret = map->bus->async_write(map->bus_context,
1556 map->format.reg_bytes +
1557 map->format.pad_bytes +
1558 val_len, NULL, 0, async);
1561 dev_err(map->dev, "Failed to schedule write: %d\n",
1564 spin_lock_irqsave(&map->async_lock, flags);
1565 list_move(&async->list, &map->async_free);
1566 spin_unlock_irqrestore(&map->async_lock, flags);
1572 trace_regmap_hw_write_start(map, reg, val_len / map->format.val_bytes);
1574 /* If we're doing a single register write we can probably just
1575 * send the work_buf directly, otherwise try to do a gather
1578 if (val == work_val)
1579 ret = map->bus->write(map->bus_context, map->work_buf,
1580 map->format.reg_bytes +
1581 map->format.pad_bytes +
1583 else if (map->bus->gather_write)
1584 ret = map->bus->gather_write(map->bus_context, map->work_buf,
1585 map->format.reg_bytes +
1586 map->format.pad_bytes,
1589 /* If that didn't work fall back on linearising by hand. */
1590 if (ret == -ENOTSUPP) {
1591 len = map->format.reg_bytes + map->format.pad_bytes + val_len;
1592 buf = kzalloc(len, GFP_KERNEL);
1596 memcpy(buf, map->work_buf, map->format.reg_bytes);
1597 memcpy(buf + map->format.reg_bytes + map->format.pad_bytes,
1599 ret = map->bus->write(map->bus_context, buf, len);
1602 } else if (ret != 0 && !map->cache_bypass && map->format.parse_val) {
1603 /* regcache_drop_region() takes lock that we already have,
1604 * thus call map->cache_ops->drop() directly
1606 if (map->cache_ops && map->cache_ops->drop)
1607 map->cache_ops->drop(map, reg, reg + 1);
1610 trace_regmap_hw_write_done(map, reg, val_len / map->format.val_bytes);
1616 * regmap_can_raw_write - Test if regmap_raw_write() is supported
1618 * @map: Map to check.
1620 bool regmap_can_raw_write(struct regmap *map)
1622 return map->bus && map->bus->write && map->format.format_val &&
1623 map->format.format_reg;
1625 EXPORT_SYMBOL_GPL(regmap_can_raw_write);
1628 * regmap_get_raw_read_max - Get the maximum size we can read
1630 * @map: Map to check.
1632 size_t regmap_get_raw_read_max(struct regmap *map)
1634 return map->max_raw_read;
1636 EXPORT_SYMBOL_GPL(regmap_get_raw_read_max);
1639 * regmap_get_raw_write_max - Get the maximum size we can read
1641 * @map: Map to check.
1643 size_t regmap_get_raw_write_max(struct regmap *map)
1645 return map->max_raw_write;
1647 EXPORT_SYMBOL_GPL(regmap_get_raw_write_max);
1649 static int _regmap_bus_formatted_write(void *context, unsigned int reg,
1653 struct regmap_range_node *range;
1654 struct regmap *map = context;
1656 WARN_ON(!map->bus || !map->format.format_write);
1658 range = _regmap_range_lookup(map, reg);
1660 ret = _regmap_select_page(map, ®, range, 1);
1665 map->format.format_write(map, reg, val);
1667 trace_regmap_hw_write_start(map, reg, 1);
1669 ret = map->bus->write(map->bus_context, map->work_buf,
1670 map->format.buf_size);
1672 trace_regmap_hw_write_done(map, reg, 1);
1677 static int _regmap_bus_reg_write(void *context, unsigned int reg,
1680 struct regmap *map = context;
1682 return map->bus->reg_write(map->bus_context, reg, val);
1685 static int _regmap_bus_raw_write(void *context, unsigned int reg,
1688 struct regmap *map = context;
1690 WARN_ON(!map->bus || !map->format.format_val);
1692 map->format.format_val(map->work_buf + map->format.reg_bytes
1693 + map->format.pad_bytes, val, 0);
1694 return _regmap_raw_write(map, reg,
1696 map->format.reg_bytes +
1697 map->format.pad_bytes,
1698 map->format.val_bytes);
1701 static inline void *_regmap_map_get_context(struct regmap *map)
1703 return (map->bus) ? map : map->bus_context;
1706 int _regmap_write(struct regmap *map, unsigned int reg,
1710 void *context = _regmap_map_get_context(map);
1712 if (!regmap_writeable(map, reg))
1715 if (!map->cache_bypass && !map->defer_caching) {
1716 ret = regcache_write(map, reg, val);
1719 if (map->cache_only) {
1720 map->cache_dirty = true;
1726 if (map->dev && strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
1727 dev_info(map->dev, "%x <= %x\n", reg, val);
1730 trace_regmap_reg_write(map, reg, val);
1732 return map->reg_write(context, reg, val);
1736 * regmap_write() - Write a value to a single register
1738 * @map: Register map to write to
1739 * @reg: Register to write to
1740 * @val: Value to be written
1742 * A value of zero will be returned on success, a negative errno will
1743 * be returned in error cases.
1745 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
1749 if (!IS_ALIGNED(reg, map->reg_stride))
1752 map->lock(map->lock_arg);
1754 ret = _regmap_write(map, reg, val);
1756 map->unlock(map->lock_arg);
1760 EXPORT_SYMBOL_GPL(regmap_write);
1763 * regmap_write_async() - Write a value to a single register asynchronously
1765 * @map: Register map to write to
1766 * @reg: Register to write to
1767 * @val: Value to be written
1769 * A value of zero will be returned on success, a negative errno will
1770 * be returned in error cases.
1772 int regmap_write_async(struct regmap *map, unsigned int reg, unsigned int val)
1776 if (!IS_ALIGNED(reg, map->reg_stride))
1779 map->lock(map->lock_arg);
1783 ret = _regmap_write(map, reg, val);
1787 map->unlock(map->lock_arg);
1791 EXPORT_SYMBOL_GPL(regmap_write_async);
1794 * regmap_raw_write() - Write raw values to one or more registers
1796 * @map: Register map to write to
1797 * @reg: Initial register to write to
1798 * @val: Block of data to be written, laid out for direct transmission to the
1800 * @val_len: Length of data pointed to by val.
1802 * This function is intended to be used for things like firmware
1803 * download where a large block of data needs to be transferred to the
1804 * device. No formatting will be done on the data provided.
1806 * A value of zero will be returned on success, a negative errno will
1807 * be returned in error cases.
1809 int regmap_raw_write(struct regmap *map, unsigned int reg,
1810 const void *val, size_t val_len)
1814 if (!regmap_can_raw_write(map))
1816 if (val_len % map->format.val_bytes)
1818 if (map->max_raw_write && map->max_raw_write > val_len)
1821 map->lock(map->lock_arg);
1823 ret = _regmap_raw_write(map, reg, val, val_len);
1825 map->unlock(map->lock_arg);
1829 EXPORT_SYMBOL_GPL(regmap_raw_write);
1832 * regmap_field_update_bits_base() - Perform a read/modify/write cycle a
1835 * @field: Register field to write to
1836 * @mask: Bitmask to change
1837 * @val: Value to be written
1838 * @change: Boolean indicating if a write was done
1839 * @async: Boolean indicating asynchronously
1840 * @force: Boolean indicating use force update
1842 * Perform a read/modify/write cycle on the register field with change,
1843 * async, force option.
1845 * A value of zero will be returned on success, a negative errno will
1846 * be returned in error cases.
1848 int regmap_field_update_bits_base(struct regmap_field *field,
1849 unsigned int mask, unsigned int val,
1850 bool *change, bool async, bool force)
1852 mask = (mask << field->shift) & field->mask;
1854 return regmap_update_bits_base(field->regmap, field->reg,
1855 mask, val << field->shift,
1856 change, async, force);
1858 EXPORT_SYMBOL_GPL(regmap_field_update_bits_base);
1861 * regmap_fields_update_bits_base() - Perform a read/modify/write cycle a
1862 * register field with port ID
1864 * @field: Register field to write to
1866 * @mask: Bitmask to change
1867 * @val: Value to be written
1868 * @change: Boolean indicating if a write was done
1869 * @async: Boolean indicating asynchronously
1870 * @force: Boolean indicating use force update
1872 * A value of zero will be returned on success, a negative errno will
1873 * be returned in error cases.
1875 int regmap_fields_update_bits_base(struct regmap_field *field, unsigned int id,
1876 unsigned int mask, unsigned int val,
1877 bool *change, bool async, bool force)
1879 if (id >= field->id_size)
1882 mask = (mask << field->shift) & field->mask;
1884 return regmap_update_bits_base(field->regmap,
1885 field->reg + (field->id_offset * id),
1886 mask, val << field->shift,
1887 change, async, force);
1889 EXPORT_SYMBOL_GPL(regmap_fields_update_bits_base);
1892 * regmap_bulk_write() - Write multiple registers to the device
1894 * @map: Register map to write to
1895 * @reg: First register to be write from
1896 * @val: Block of data to be written, in native register size for device
1897 * @val_count: Number of registers to write
1899 * This function is intended to be used for writing a large block of
1900 * data to the device either in single transfer or multiple transfer.
1902 * A value of zero will be returned on success, a negative errno will
1903 * be returned in error cases.
1905 int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
1909 size_t val_bytes = map->format.val_bytes;
1910 size_t total_size = val_bytes * val_count;
1912 if (!IS_ALIGNED(reg, map->reg_stride))
1916 * Some devices don't support bulk write, for
1917 * them we have a series of single write operations in the first two if
1920 * The first if block is used for memory mapped io. It does not allow
1921 * val_bytes of 3 for example.
1922 * The second one is for busses that do not provide raw I/O.
1923 * The third one is used for busses which do not have these limitations
1924 * and can write arbitrary value lengths.
1927 map->lock(map->lock_arg);
1928 for (i = 0; i < val_count; i++) {
1931 switch (val_bytes) {
1933 ival = *(u8 *)(val + (i * val_bytes));
1936 ival = *(u16 *)(val + (i * val_bytes));
1939 ival = *(u32 *)(val + (i * val_bytes));
1943 ival = *(u64 *)(val + (i * val_bytes));
1951 ret = _regmap_write(map,
1952 reg + regmap_get_offset(map, i),
1958 map->unlock(map->lock_arg);
1959 } else if (map->bus && !map->format.parse_inplace) {
1961 const u16 *u16 = val;
1962 const u32 *u32 = val;
1965 for (i = 0; i < val_count; i++) {
1966 switch (map->format.val_bytes) {
1980 ret = regmap_write(map, reg + (i * map->reg_stride),
1985 } else if (map->use_single_write ||
1986 (map->max_raw_write && map->max_raw_write < total_size)) {
1987 int chunk_stride = map->reg_stride;
1988 size_t chunk_size = val_bytes;
1989 size_t chunk_count = val_count;
1991 if (!map->use_single_write) {
1992 chunk_size = map->max_raw_write;
1993 if (chunk_size % val_bytes)
1994 chunk_size -= chunk_size % val_bytes;
1995 chunk_count = total_size / chunk_size;
1996 chunk_stride *= chunk_size / val_bytes;
1999 map->lock(map->lock_arg);
2000 /* Write as many bytes as possible with chunk_size */
2001 for (i = 0; i < chunk_count; i++) {
2002 ret = _regmap_raw_write(map,
2003 reg + (i * chunk_stride),
2004 val + (i * chunk_size),
2010 /* Write remaining bytes */
2011 if (!ret && chunk_size * i < total_size) {
2012 ret = _regmap_raw_write(map, reg + (i * chunk_stride),
2013 val + (i * chunk_size),
2014 total_size - i * chunk_size);
2016 map->unlock(map->lock_arg);
2023 wval = kmemdup(val, val_count * val_bytes, map->alloc_flags);
2025 dev_err(map->dev, "Error in memory allocation\n");
2028 for (i = 0; i < val_count * val_bytes; i += val_bytes)
2029 map->format.parse_inplace(wval + i);
2031 map->lock(map->lock_arg);
2032 ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count);
2033 map->unlock(map->lock_arg);
2039 EXPORT_SYMBOL_GPL(regmap_bulk_write);
2042 * _regmap_raw_multi_reg_write()
2044 * the (register,newvalue) pairs in regs have not been formatted, but
2045 * they are all in the same page and have been changed to being page
2046 * relative. The page register has been written if that was necessary.
2048 static int _regmap_raw_multi_reg_write(struct regmap *map,
2049 const struct reg_sequence *regs,
2056 size_t val_bytes = map->format.val_bytes;
2057 size_t reg_bytes = map->format.reg_bytes;
2058 size_t pad_bytes = map->format.pad_bytes;
2059 size_t pair_size = reg_bytes + pad_bytes + val_bytes;
2060 size_t len = pair_size * num_regs;
2065 buf = kzalloc(len, GFP_KERNEL);
2069 /* We have to linearise by hand. */
2073 for (i = 0; i < num_regs; i++) {
2074 unsigned int reg = regs[i].reg;
2075 unsigned int val = regs[i].def;
2076 trace_regmap_hw_write_start(map, reg, 1);
2077 map->format.format_reg(u8, reg, map->reg_shift);
2078 u8 += reg_bytes + pad_bytes;
2079 map->format.format_val(u8, val, 0);
2083 *u8 |= map->write_flag_mask;
2085 ret = map->bus->write(map->bus_context, buf, len);
2089 for (i = 0; i < num_regs; i++) {
2090 int reg = regs[i].reg;
2091 trace_regmap_hw_write_done(map, reg, 1);
2096 static unsigned int _regmap_register_page(struct regmap *map,
2098 struct regmap_range_node *range)
2100 unsigned int win_page = (reg - range->range_min) / range->window_len;
2105 static int _regmap_range_multi_paged_reg_write(struct regmap *map,
2106 struct reg_sequence *regs,
2111 struct reg_sequence *base;
2112 unsigned int this_page = 0;
2113 unsigned int page_change = 0;
2115 * the set of registers are not neccessarily in order, but
2116 * since the order of write must be preserved this algorithm
2117 * chops the set each time the page changes. This also applies
2118 * if there is a delay required at any point in the sequence.
2121 for (i = 0, n = 0; i < num_regs; i++, n++) {
2122 unsigned int reg = regs[i].reg;
2123 struct regmap_range_node *range;
2125 range = _regmap_range_lookup(map, reg);
2127 unsigned int win_page = _regmap_register_page(map, reg,
2131 this_page = win_page;
2132 if (win_page != this_page) {
2133 this_page = win_page;
2138 /* If we have both a page change and a delay make sure to
2139 * write the regs and apply the delay before we change the
2143 if (page_change || regs[i].delay_us) {
2145 /* For situations where the first write requires
2146 * a delay we need to make sure we don't call
2147 * raw_multi_reg_write with n=0
2148 * This can't occur with page breaks as we
2149 * never write on the first iteration
2151 if (regs[i].delay_us && i == 0)
2154 ret = _regmap_raw_multi_reg_write(map, base, n);
2158 if (regs[i].delay_us)
2159 udelay(regs[i].delay_us);
2165 ret = _regmap_select_page(map,
2178 return _regmap_raw_multi_reg_write(map, base, n);
2182 static int _regmap_multi_reg_write(struct regmap *map,
2183 const struct reg_sequence *regs,
2189 if (!map->can_multi_write) {
2190 for (i = 0; i < num_regs; i++) {
2191 ret = _regmap_write(map, regs[i].reg, regs[i].def);
2195 if (regs[i].delay_us)
2196 udelay(regs[i].delay_us);
2201 if (!map->format.parse_inplace)
2204 if (map->writeable_reg)
2205 for (i = 0; i < num_regs; i++) {
2206 int reg = regs[i].reg;
2207 if (!map->writeable_reg(map->dev, reg))
2209 if (!IS_ALIGNED(reg, map->reg_stride))
2213 if (!map->cache_bypass) {
2214 for (i = 0; i < num_regs; i++) {
2215 unsigned int val = regs[i].def;
2216 unsigned int reg = regs[i].reg;
2217 ret = regcache_write(map, reg, val);
2220 "Error in caching of register: %x ret: %d\n",
2225 if (map->cache_only) {
2226 map->cache_dirty = true;
2233 for (i = 0; i < num_regs; i++) {
2234 unsigned int reg = regs[i].reg;
2235 struct regmap_range_node *range;
2237 /* Coalesce all the writes between a page break or a delay
2240 range = _regmap_range_lookup(map, reg);
2241 if (range || regs[i].delay_us) {
2242 size_t len = sizeof(struct reg_sequence)*num_regs;
2243 struct reg_sequence *base = kmemdup(regs, len,
2247 ret = _regmap_range_multi_paged_reg_write(map, base,
2254 return _regmap_raw_multi_reg_write(map, regs, num_regs);
2258 * regmap_multi_reg_write() - Write multiple registers to the device
2260 * @map: Register map to write to
2261 * @regs: Array of structures containing register,value to be written
2262 * @num_regs: Number of registers to write
2264 * Write multiple registers to the device where the set of register, value
2265 * pairs are supplied in any order, possibly not all in a single range.
2267 * The 'normal' block write mode will send ultimately send data on the
2268 * target bus as R,V1,V2,V3,..,Vn where successively higher registers are
2269 * addressed. However, this alternative block multi write mode will send
2270 * the data as R1,V1,R2,V2,..,Rn,Vn on the target bus. The target device
2271 * must of course support the mode.
2273 * A value of zero will be returned on success, a negative errno will be
2274 * returned in error cases.
2276 int regmap_multi_reg_write(struct regmap *map, const struct reg_sequence *regs,
2281 map->lock(map->lock_arg);
2283 ret = _regmap_multi_reg_write(map, regs, num_regs);
2285 map->unlock(map->lock_arg);
2289 EXPORT_SYMBOL_GPL(regmap_multi_reg_write);
2292 * regmap_multi_reg_write_bypassed() - Write multiple registers to the
2293 * device but not the cache
2295 * @map: Register map to write to
2296 * @regs: Array of structures containing register,value to be written
2297 * @num_regs: Number of registers to write
2299 * Write multiple registers to the device but not the cache where the set
2300 * of register are supplied in any order.
2302 * This function is intended to be used for writing a large block of data
2303 * atomically to the device in single transfer for those I2C client devices
2304 * that implement this alternative block write mode.
2306 * A value of zero will be returned on success, a negative errno will
2307 * be returned in error cases.
2309 int regmap_multi_reg_write_bypassed(struct regmap *map,
2310 const struct reg_sequence *regs,
2316 map->lock(map->lock_arg);
2318 bypass = map->cache_bypass;
2319 map->cache_bypass = true;
2321 ret = _regmap_multi_reg_write(map, regs, num_regs);
2323 map->cache_bypass = bypass;
2325 map->unlock(map->lock_arg);
2329 EXPORT_SYMBOL_GPL(regmap_multi_reg_write_bypassed);
2332 * regmap_raw_write_async() - Write raw values to one or more registers
2335 * @map: Register map to write to
2336 * @reg: Initial register to write to
2337 * @val: Block of data to be written, laid out for direct transmission to the
2338 * device. Must be valid until regmap_async_complete() is called.
2339 * @val_len: Length of data pointed to by val.
2341 * This function is intended to be used for things like firmware
2342 * download where a large block of data needs to be transferred to the
2343 * device. No formatting will be done on the data provided.
2345 * If supported by the underlying bus the write will be scheduled
2346 * asynchronously, helping maximise I/O speed on higher speed buses
2347 * like SPI. regmap_async_complete() can be called to ensure that all
2348 * asynchrnous writes have been completed.
2350 * A value of zero will be returned on success, a negative errno will
2351 * be returned in error cases.
2353 int regmap_raw_write_async(struct regmap *map, unsigned int reg,
2354 const void *val, size_t val_len)
2358 if (val_len % map->format.val_bytes)
2360 if (!IS_ALIGNED(reg, map->reg_stride))
2363 map->lock(map->lock_arg);
2367 ret = _regmap_raw_write(map, reg, val, val_len);
2371 map->unlock(map->lock_arg);
2375 EXPORT_SYMBOL_GPL(regmap_raw_write_async);
2377 static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
2378 unsigned int val_len)
2380 struct regmap_range_node *range;
2385 if (!map->bus || !map->bus->read)
2388 range = _regmap_range_lookup(map, reg);
2390 ret = _regmap_select_page(map, ®, range,
2391 val_len / map->format.val_bytes);
2396 map->format.format_reg(map->work_buf, reg, map->reg_shift);
2397 regmap_set_work_buf_flag_mask(map, map->format.reg_bytes,
2398 map->read_flag_mask);
2399 trace_regmap_hw_read_start(map, reg, val_len / map->format.val_bytes);
2401 ret = map->bus->read(map->bus_context, map->work_buf,
2402 map->format.reg_bytes + map->format.pad_bytes,
2405 trace_regmap_hw_read_done(map, reg, val_len / map->format.val_bytes);
2410 static int _regmap_bus_reg_read(void *context, unsigned int reg,
2413 struct regmap *map = context;
2415 return map->bus->reg_read(map->bus_context, reg, val);
2418 static int _regmap_bus_read(void *context, unsigned int reg,
2422 struct regmap *map = context;
2424 if (!map->format.parse_val)
2427 ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
2429 *val = map->format.parse_val(map->work_buf);
2434 static int _regmap_read(struct regmap *map, unsigned int reg,
2438 void *context = _regmap_map_get_context(map);
2440 if (!map->cache_bypass) {
2441 ret = regcache_read(map, reg, val);
2446 if (map->cache_only)
2449 if (!regmap_readable(map, reg))
2452 ret = map->reg_read(context, reg, val);
2455 if (map->dev && strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
2456 dev_info(map->dev, "%x => %x\n", reg, *val);
2459 trace_regmap_reg_read(map, reg, *val);
2461 if (!map->cache_bypass)
2462 regcache_write(map, reg, *val);
2469 * regmap_read() - Read a value from a single register
2471 * @map: Register map to read from
2472 * @reg: Register to be read from
2473 * @val: Pointer to store read value
2475 * A value of zero will be returned on success, a negative errno will
2476 * be returned in error cases.
2478 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
2482 if (!IS_ALIGNED(reg, map->reg_stride))
2485 map->lock(map->lock_arg);
2487 ret = _regmap_read(map, reg, val);
2489 map->unlock(map->lock_arg);
2493 EXPORT_SYMBOL_GPL(regmap_read);
2496 * regmap_raw_read() - Read raw data from the device
2498 * @map: Register map to read from
2499 * @reg: First register to be read from
2500 * @val: Pointer to store read value
2501 * @val_len: Size of data to read
2503 * A value of zero will be returned on success, a negative errno will
2504 * be returned in error cases.
2506 int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
2509 size_t val_bytes = map->format.val_bytes;
2510 size_t val_count = val_len / val_bytes;
2516 if (val_len % map->format.val_bytes)
2518 if (!IS_ALIGNED(reg, map->reg_stride))
2523 map->lock(map->lock_arg);
2525 if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
2526 map->cache_type == REGCACHE_NONE) {
2527 if (!map->bus->read) {
2531 if (map->max_raw_read && map->max_raw_read < val_len) {
2536 /* Physical block read if there's no cache involved */
2537 ret = _regmap_raw_read(map, reg, val, val_len);
2540 /* Otherwise go word by word for the cache; should be low
2541 * cost as we expect to hit the cache.
2543 for (i = 0; i < val_count; i++) {
2544 ret = _regmap_read(map, reg + regmap_get_offset(map, i),
2549 map->format.format_val(val + (i * val_bytes), v, 0);
2554 map->unlock(map->lock_arg);
2558 EXPORT_SYMBOL_GPL(regmap_raw_read);
2561 * regmap_field_read() - Read a value to a single register field
2563 * @field: Register field to read from
2564 * @val: Pointer to store read value
2566 * A value of zero will be returned on success, a negative errno will
2567 * be returned in error cases.
2569 int regmap_field_read(struct regmap_field *field, unsigned int *val)
2572 unsigned int reg_val;
2573 ret = regmap_read(field->regmap, field->reg, ®_val);
2577 reg_val &= field->mask;
2578 reg_val >>= field->shift;
2583 EXPORT_SYMBOL_GPL(regmap_field_read);
2586 * regmap_fields_read() - Read a value to a single register field with port ID
2588 * @field: Register field to read from
2590 * @val: Pointer to store read value
2592 * A value of zero will be returned on success, a negative errno will
2593 * be returned in error cases.
2595 int regmap_fields_read(struct regmap_field *field, unsigned int id,
2599 unsigned int reg_val;
2601 if (id >= field->id_size)
2604 ret = regmap_read(field->regmap,
2605 field->reg + (field->id_offset * id),
2610 reg_val &= field->mask;
2611 reg_val >>= field->shift;
2616 EXPORT_SYMBOL_GPL(regmap_fields_read);
2619 * regmap_bulk_read() - Read multiple registers from the device
2621 * @map: Register map to read from
2622 * @reg: First register to be read from
2623 * @val: Pointer to store read value, in native register size for device
2624 * @val_count: Number of registers to read
2626 * A value of zero will be returned on success, a negative errno will
2627 * be returned in error cases.
2629 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
2633 size_t val_bytes = map->format.val_bytes;
2634 bool vol = regmap_volatile_range(map, reg, val_count);
2636 if (!IS_ALIGNED(reg, map->reg_stride))
2639 if (map->bus && map->format.parse_inplace && (vol || map->cache_type == REGCACHE_NONE)) {
2641 * Some devices does not support bulk read, for
2642 * them we have a series of single read operations.
2644 size_t total_size = val_bytes * val_count;
2646 if (!map->use_single_read &&
2647 (!map->max_raw_read || map->max_raw_read > total_size)) {
2648 ret = regmap_raw_read(map, reg, val,
2649 val_bytes * val_count);
2654 * Some devices do not support bulk read or do not
2655 * support large bulk reads, for them we have a series
2656 * of read operations.
2658 int chunk_stride = map->reg_stride;
2659 size_t chunk_size = val_bytes;
2660 size_t chunk_count = val_count;
2662 if (!map->use_single_read) {
2663 chunk_size = map->max_raw_read;
2664 if (chunk_size % val_bytes)
2665 chunk_size -= chunk_size % val_bytes;
2666 chunk_count = total_size / chunk_size;
2667 chunk_stride *= chunk_size / val_bytes;
2670 /* Read bytes that fit into a multiple of chunk_size */
2671 for (i = 0; i < chunk_count; i++) {
2672 ret = regmap_raw_read(map,
2673 reg + (i * chunk_stride),
2674 val + (i * chunk_size),
2680 /* Read remaining bytes */
2681 if (chunk_size * i < total_size) {
2682 ret = regmap_raw_read(map,
2683 reg + (i * chunk_stride),
2684 val + (i * chunk_size),
2685 total_size - i * chunk_size);
2691 for (i = 0; i < val_count * val_bytes; i += val_bytes)
2692 map->format.parse_inplace(val + i);
2694 for (i = 0; i < val_count; i++) {
2696 ret = regmap_read(map, reg + regmap_get_offset(map, i),
2701 if (map->format.format_val) {
2702 map->format.format_val(val + (i * val_bytes), ival, 0);
2704 /* Devices providing read and write
2705 * operations can use the bulk I/O
2706 * functions if they define a val_bytes,
2707 * we assume that the values are native
2717 switch (map->format.val_bytes) {
2741 EXPORT_SYMBOL_GPL(regmap_bulk_read);
2743 static int _regmap_update_bits(struct regmap *map, unsigned int reg,
2744 unsigned int mask, unsigned int val,
2745 bool *change, bool force_write)
2748 unsigned int tmp, orig;
2753 if (regmap_volatile(map, reg) && map->reg_update_bits) {
2754 ret = map->reg_update_bits(map->bus_context, reg, mask, val);
2755 if (ret == 0 && change)
2758 ret = _regmap_read(map, reg, &orig);
2765 if (force_write || (tmp != orig)) {
2766 ret = _regmap_write(map, reg, tmp);
2767 if (ret == 0 && change)
2776 * regmap_update_bits_base() - Perform a read/modify/write cycle on a register
2778 * @map: Register map to update
2779 * @reg: Register to update
2780 * @mask: Bitmask to change
2781 * @val: New value for bitmask
2782 * @change: Boolean indicating if a write was done
2783 * @async: Boolean indicating asynchronously
2784 * @force: Boolean indicating use force update
2786 * Perform a read/modify/write cycle on a register map with change, async, force
2791 * With most buses the read must be done synchronously so this is most useful
2792 * for devices with a cache which do not need to interact with the hardware to
2793 * determine the current register value.
2795 * Returns zero for success, a negative number on error.
2797 int regmap_update_bits_base(struct regmap *map, unsigned int reg,
2798 unsigned int mask, unsigned int val,
2799 bool *change, bool async, bool force)
2803 map->lock(map->lock_arg);
2807 ret = _regmap_update_bits(map, reg, mask, val, change, force);
2811 map->unlock(map->lock_arg);
2815 EXPORT_SYMBOL_GPL(regmap_update_bits_base);
2817 void regmap_async_complete_cb(struct regmap_async *async, int ret)
2819 struct regmap *map = async->map;
2822 trace_regmap_async_io_complete(map);
2824 spin_lock(&map->async_lock);
2825 list_move(&async->list, &map->async_free);
2826 wake = list_empty(&map->async_list);
2829 map->async_ret = ret;
2831 spin_unlock(&map->async_lock);
2834 wake_up(&map->async_waitq);
2836 EXPORT_SYMBOL_GPL(regmap_async_complete_cb);
2838 static int regmap_async_is_done(struct regmap *map)
2840 unsigned long flags;
2843 spin_lock_irqsave(&map->async_lock, flags);
2844 ret = list_empty(&map->async_list);
2845 spin_unlock_irqrestore(&map->async_lock, flags);
2851 * regmap_async_complete - Ensure all asynchronous I/O has completed.
2853 * @map: Map to operate on.
2855 * Blocks until any pending asynchronous I/O has completed. Returns
2856 * an error code for any failed I/O operations.
2858 int regmap_async_complete(struct regmap *map)
2860 unsigned long flags;
2863 /* Nothing to do with no async support */
2864 if (!map->bus || !map->bus->async_write)
2867 trace_regmap_async_complete_start(map);
2869 wait_event(map->async_waitq, regmap_async_is_done(map));
2871 spin_lock_irqsave(&map->async_lock, flags);
2872 ret = map->async_ret;
2874 spin_unlock_irqrestore(&map->async_lock, flags);
2876 trace_regmap_async_complete_done(map);
2880 EXPORT_SYMBOL_GPL(regmap_async_complete);
2883 * regmap_register_patch - Register and apply register updates to be applied
2884 * on device initialistion
2886 * @map: Register map to apply updates to.
2887 * @regs: Values to update.
2888 * @num_regs: Number of entries in regs.
2890 * Register a set of register updates to be applied to the device
2891 * whenever the device registers are synchronised with the cache and
2892 * apply them immediately. Typically this is used to apply
2893 * corrections to be applied to the device defaults on startup, such
2894 * as the updates some vendors provide to undocumented registers.
2896 * The caller must ensure that this function cannot be called
2897 * concurrently with either itself or regcache_sync().
2899 int regmap_register_patch(struct regmap *map, const struct reg_sequence *regs,
2902 struct reg_sequence *p;
2906 if (WARN_ONCE(num_regs <= 0, "invalid registers number (%d)\n",
2910 p = krealloc(map->patch,
2911 sizeof(struct reg_sequence) * (map->patch_regs + num_regs),
2914 memcpy(p + map->patch_regs, regs, num_regs * sizeof(*regs));
2916 map->patch_regs += num_regs;
2921 map->lock(map->lock_arg);
2923 bypass = map->cache_bypass;
2925 map->cache_bypass = true;
2928 ret = _regmap_multi_reg_write(map, regs, num_regs);
2931 map->cache_bypass = bypass;
2933 map->unlock(map->lock_arg);
2935 regmap_async_complete(map);
2939 EXPORT_SYMBOL_GPL(regmap_register_patch);
2942 * regmap_get_val_bytes() - Report the size of a register value
2944 * @map: Register map to operate on.
2946 * Report the size of a register value, mainly intended to for use by
2947 * generic infrastructure built on top of regmap.
2949 int regmap_get_val_bytes(struct regmap *map)
2951 if (map->format.format_write)
2954 return map->format.val_bytes;
2956 EXPORT_SYMBOL_GPL(regmap_get_val_bytes);
2959 * regmap_get_max_register() - Report the max register value
2961 * @map: Register map to operate on.
2963 * Report the max register value, mainly intended to for use by
2964 * generic infrastructure built on top of regmap.
2966 int regmap_get_max_register(struct regmap *map)
2968 return map->max_register ? map->max_register : -EINVAL;
2970 EXPORT_SYMBOL_GPL(regmap_get_max_register);
2973 * regmap_get_reg_stride() - Report the register address stride
2975 * @map: Register map to operate on.
2977 * Report the register address stride, mainly intended to for use by
2978 * generic infrastructure built on top of regmap.
2980 int regmap_get_reg_stride(struct regmap *map)
2982 return map->reg_stride;
2984 EXPORT_SYMBOL_GPL(regmap_get_reg_stride);
2986 int regmap_parse_val(struct regmap *map, const void *buf,
2989 if (!map->format.parse_val)
2992 *val = map->format.parse_val(buf);
2996 EXPORT_SYMBOL_GPL(regmap_parse_val);
2998 static int __init regmap_initcall(void)
3000 regmap_debugfs_initcall();
3004 postcore_initcall(regmap_initcall);