Linux 5.9.7
[linux/fpc-iii.git] / drivers / base / regmap / regmap.c
blobfff0547c26c53db46e90343e2b1b81fadf7feba7
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Register map access API
4 //
5 // Copyright 2011 Wolfson Microelectronics plc
6 //
7 // Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
9 #include <linux/device.h>
10 #include <linux/slab.h>
11 #include <linux/export.h>
12 #include <linux/mutex.h>
13 #include <linux/err.h>
14 #include <linux/property.h>
15 #include <linux/rbtree.h>
16 #include <linux/sched.h>
17 #include <linux/delay.h>
18 #include <linux/log2.h>
19 #include <linux/hwspinlock.h>
20 #include <asm/unaligned.h>
22 #define CREATE_TRACE_POINTS
23 #include "trace.h"
25 #include "internal.h"
28 * Sometimes for failures during very early init the trace
29 * infrastructure isn't available early enough to be used. For this
30 * sort of problem defining LOG_DEVICE will add printks for basic
31 * register I/O on a specific device.
33 #undef LOG_DEVICE
35 #ifdef LOG_DEVICE
36 static inline bool regmap_should_log(struct regmap *map)
38 return (map->dev && strcmp(dev_name(map->dev), LOG_DEVICE) == 0);
40 #else
41 static inline bool regmap_should_log(struct regmap *map) { return false; }
42 #endif
45 static int _regmap_update_bits(struct regmap *map, unsigned int reg,
46 unsigned int mask, unsigned int val,
47 bool *change, bool force_write);
49 static int _regmap_bus_reg_read(void *context, unsigned int reg,
50 unsigned int *val);
51 static int _regmap_bus_read(void *context, unsigned int reg,
52 unsigned int *val);
53 static int _regmap_bus_formatted_write(void *context, unsigned int reg,
54 unsigned int val);
55 static int _regmap_bus_reg_write(void *context, unsigned int reg,
56 unsigned int val);
57 static int _regmap_bus_raw_write(void *context, unsigned int reg,
58 unsigned int val);
60 bool regmap_reg_in_ranges(unsigned int reg,
61 const struct regmap_range *ranges,
62 unsigned int nranges)
64 const struct regmap_range *r;
65 int i;
67 for (i = 0, r = ranges; i < nranges; i++, r++)
68 if (regmap_reg_in_range(reg, r))
69 return true;
70 return false;
72 EXPORT_SYMBOL_GPL(regmap_reg_in_ranges);
74 bool regmap_check_range_table(struct regmap *map, unsigned int reg,
75 const struct regmap_access_table *table)
77 /* Check "no ranges" first */
78 if (regmap_reg_in_ranges(reg, table->no_ranges, table->n_no_ranges))
79 return false;
81 /* In case zero "yes ranges" are supplied, any reg is OK */
82 if (!table->n_yes_ranges)
83 return true;
85 return regmap_reg_in_ranges(reg, table->yes_ranges,
86 table->n_yes_ranges);
88 EXPORT_SYMBOL_GPL(regmap_check_range_table);
90 bool regmap_writeable(struct regmap *map, unsigned int reg)
92 if (map->max_register && reg > map->max_register)
93 return false;
95 if (map->writeable_reg)
96 return map->writeable_reg(map->dev, reg);
98 if (map->wr_table)
99 return regmap_check_range_table(map, reg, map->wr_table);
101 return true;
104 bool regmap_cached(struct regmap *map, unsigned int reg)
106 int ret;
107 unsigned int val;
109 if (map->cache_type == REGCACHE_NONE)
110 return false;
112 if (!map->cache_ops)
113 return false;
115 if (map->max_register && reg > map->max_register)
116 return false;
118 map->lock(map->lock_arg);
119 ret = regcache_read(map, reg, &val);
120 map->unlock(map->lock_arg);
121 if (ret)
122 return false;
124 return true;
127 bool regmap_readable(struct regmap *map, unsigned int reg)
129 if (!map->reg_read)
130 return false;
132 if (map->max_register && reg > map->max_register)
133 return false;
135 if (map->format.format_write)
136 return false;
138 if (map->readable_reg)
139 return map->readable_reg(map->dev, reg);
141 if (map->rd_table)
142 return regmap_check_range_table(map, reg, map->rd_table);
144 return true;
147 bool regmap_volatile(struct regmap *map, unsigned int reg)
149 if (!map->format.format_write && !regmap_readable(map, reg))
150 return false;
152 if (map->volatile_reg)
153 return map->volatile_reg(map->dev, reg);
155 if (map->volatile_table)
156 return regmap_check_range_table(map, reg, map->volatile_table);
158 if (map->cache_ops)
159 return false;
160 else
161 return true;
164 bool regmap_precious(struct regmap *map, unsigned int reg)
166 if (!regmap_readable(map, reg))
167 return false;
169 if (map->precious_reg)
170 return map->precious_reg(map->dev, reg);
172 if (map->precious_table)
173 return regmap_check_range_table(map, reg, map->precious_table);
175 return false;
178 bool regmap_writeable_noinc(struct regmap *map, unsigned int reg)
180 if (map->writeable_noinc_reg)
181 return map->writeable_noinc_reg(map->dev, reg);
183 if (map->wr_noinc_table)
184 return regmap_check_range_table(map, reg, map->wr_noinc_table);
186 return true;
189 bool regmap_readable_noinc(struct regmap *map, unsigned int reg)
191 if (map->readable_noinc_reg)
192 return map->readable_noinc_reg(map->dev, reg);
194 if (map->rd_noinc_table)
195 return regmap_check_range_table(map, reg, map->rd_noinc_table);
197 return true;
200 static bool regmap_volatile_range(struct regmap *map, unsigned int reg,
201 size_t num)
203 unsigned int i;
205 for (i = 0; i < num; i++)
206 if (!regmap_volatile(map, reg + regmap_get_offset(map, i)))
207 return false;
209 return true;
212 static void regmap_format_2_6_write(struct regmap *map,
213 unsigned int reg, unsigned int val)
215 u8 *out = map->work_buf;
217 *out = (reg << 6) | val;
220 static void regmap_format_4_12_write(struct regmap *map,
221 unsigned int reg, unsigned int val)
223 __be16 *out = map->work_buf;
224 *out = cpu_to_be16((reg << 12) | val);
227 static void regmap_format_7_9_write(struct regmap *map,
228 unsigned int reg, unsigned int val)
230 __be16 *out = map->work_buf;
231 *out = cpu_to_be16((reg << 9) | val);
234 static void regmap_format_10_14_write(struct regmap *map,
235 unsigned int reg, unsigned int val)
237 u8 *out = map->work_buf;
239 out[2] = val;
240 out[1] = (val >> 8) | (reg << 6);
241 out[0] = reg >> 2;
244 static void regmap_format_8(void *buf, unsigned int val, unsigned int shift)
246 u8 *b = buf;
248 b[0] = val << shift;
251 static void regmap_format_16_be(void *buf, unsigned int val, unsigned int shift)
253 put_unaligned_be16(val << shift, buf);
256 static void regmap_format_16_le(void *buf, unsigned int val, unsigned int shift)
258 put_unaligned_le16(val << shift, buf);
261 static void regmap_format_16_native(void *buf, unsigned int val,
262 unsigned int shift)
264 u16 v = val << shift;
266 memcpy(buf, &v, sizeof(v));
269 static void regmap_format_24(void *buf, unsigned int val, unsigned int shift)
271 u8 *b = buf;
273 val <<= shift;
275 b[0] = val >> 16;
276 b[1] = val >> 8;
277 b[2] = val;
280 static void regmap_format_32_be(void *buf, unsigned int val, unsigned int shift)
282 put_unaligned_be32(val << shift, buf);
285 static void regmap_format_32_le(void *buf, unsigned int val, unsigned int shift)
287 put_unaligned_le32(val << shift, buf);
290 static void regmap_format_32_native(void *buf, unsigned int val,
291 unsigned int shift)
293 u32 v = val << shift;
295 memcpy(buf, &v, sizeof(v));
298 #ifdef CONFIG_64BIT
299 static void regmap_format_64_be(void *buf, unsigned int val, unsigned int shift)
301 put_unaligned_be64((u64) val << shift, buf);
304 static void regmap_format_64_le(void *buf, unsigned int val, unsigned int shift)
306 put_unaligned_le64((u64) val << shift, buf);
309 static void regmap_format_64_native(void *buf, unsigned int val,
310 unsigned int shift)
312 u64 v = (u64) val << shift;
314 memcpy(buf, &v, sizeof(v));
316 #endif
318 static void regmap_parse_inplace_noop(void *buf)
322 static unsigned int regmap_parse_8(const void *buf)
324 const u8 *b = buf;
326 return b[0];
329 static unsigned int regmap_parse_16_be(const void *buf)
331 return get_unaligned_be16(buf);
334 static unsigned int regmap_parse_16_le(const void *buf)
336 return get_unaligned_le16(buf);
339 static void regmap_parse_16_be_inplace(void *buf)
341 u16 v = get_unaligned_be16(buf);
343 memcpy(buf, &v, sizeof(v));
346 static void regmap_parse_16_le_inplace(void *buf)
348 u16 v = get_unaligned_le16(buf);
350 memcpy(buf, &v, sizeof(v));
353 static unsigned int regmap_parse_16_native(const void *buf)
355 u16 v;
357 memcpy(&v, buf, sizeof(v));
358 return v;
361 static unsigned int regmap_parse_24(const void *buf)
363 const u8 *b = buf;
364 unsigned int ret = b[2];
365 ret |= ((unsigned int)b[1]) << 8;
366 ret |= ((unsigned int)b[0]) << 16;
368 return ret;
371 static unsigned int regmap_parse_32_be(const void *buf)
373 return get_unaligned_be32(buf);
376 static unsigned int regmap_parse_32_le(const void *buf)
378 return get_unaligned_le32(buf);
381 static void regmap_parse_32_be_inplace(void *buf)
383 u32 v = get_unaligned_be32(buf);
385 memcpy(buf, &v, sizeof(v));
388 static void regmap_parse_32_le_inplace(void *buf)
390 u32 v = get_unaligned_le32(buf);
392 memcpy(buf, &v, sizeof(v));
395 static unsigned int regmap_parse_32_native(const void *buf)
397 u32 v;
399 memcpy(&v, buf, sizeof(v));
400 return v;
403 #ifdef CONFIG_64BIT
404 static unsigned int regmap_parse_64_be(const void *buf)
406 return get_unaligned_be64(buf);
409 static unsigned int regmap_parse_64_le(const void *buf)
411 return get_unaligned_le64(buf);
414 static void regmap_parse_64_be_inplace(void *buf)
416 u64 v = get_unaligned_be64(buf);
418 memcpy(buf, &v, sizeof(v));
421 static void regmap_parse_64_le_inplace(void *buf)
423 u64 v = get_unaligned_le64(buf);
425 memcpy(buf, &v, sizeof(v));
428 static unsigned int regmap_parse_64_native(const void *buf)
430 u64 v;
432 memcpy(&v, buf, sizeof(v));
433 return v;
435 #endif
437 static void regmap_lock_hwlock(void *__map)
439 struct regmap *map = __map;
441 hwspin_lock_timeout(map->hwlock, UINT_MAX);
444 static void regmap_lock_hwlock_irq(void *__map)
446 struct regmap *map = __map;
448 hwspin_lock_timeout_irq(map->hwlock, UINT_MAX);
451 static void regmap_lock_hwlock_irqsave(void *__map)
453 struct regmap *map = __map;
455 hwspin_lock_timeout_irqsave(map->hwlock, UINT_MAX,
456 &map->spinlock_flags);
459 static void regmap_unlock_hwlock(void *__map)
461 struct regmap *map = __map;
463 hwspin_unlock(map->hwlock);
466 static void regmap_unlock_hwlock_irq(void *__map)
468 struct regmap *map = __map;
470 hwspin_unlock_irq(map->hwlock);
473 static void regmap_unlock_hwlock_irqrestore(void *__map)
475 struct regmap *map = __map;
477 hwspin_unlock_irqrestore(map->hwlock, &map->spinlock_flags);
480 static void regmap_lock_unlock_none(void *__map)
485 static void regmap_lock_mutex(void *__map)
487 struct regmap *map = __map;
488 mutex_lock(&map->mutex);
491 static void regmap_unlock_mutex(void *__map)
493 struct regmap *map = __map;
494 mutex_unlock(&map->mutex);
497 static void regmap_lock_spinlock(void *__map)
498 __acquires(&map->spinlock)
500 struct regmap *map = __map;
501 unsigned long flags;
503 spin_lock_irqsave(&map->spinlock, flags);
504 map->spinlock_flags = flags;
507 static void regmap_unlock_spinlock(void *__map)
508 __releases(&map->spinlock)
510 struct regmap *map = __map;
511 spin_unlock_irqrestore(&map->spinlock, map->spinlock_flags);
514 static void dev_get_regmap_release(struct device *dev, void *res)
517 * We don't actually have anything to do here; the goal here
518 * is not to manage the regmap but to provide a simple way to
519 * get the regmap back given a struct device.
523 static bool _regmap_range_add(struct regmap *map,
524 struct regmap_range_node *data)
526 struct rb_root *root = &map->range_tree;
527 struct rb_node **new = &(root->rb_node), *parent = NULL;
529 while (*new) {
530 struct regmap_range_node *this =
531 rb_entry(*new, struct regmap_range_node, node);
533 parent = *new;
534 if (data->range_max < this->range_min)
535 new = &((*new)->rb_left);
536 else if (data->range_min > this->range_max)
537 new = &((*new)->rb_right);
538 else
539 return false;
542 rb_link_node(&data->node, parent, new);
543 rb_insert_color(&data->node, root);
545 return true;
548 static struct regmap_range_node *_regmap_range_lookup(struct regmap *map,
549 unsigned int reg)
551 struct rb_node *node = map->range_tree.rb_node;
553 while (node) {
554 struct regmap_range_node *this =
555 rb_entry(node, struct regmap_range_node, node);
557 if (reg < this->range_min)
558 node = node->rb_left;
559 else if (reg > this->range_max)
560 node = node->rb_right;
561 else
562 return this;
565 return NULL;
568 static void regmap_range_exit(struct regmap *map)
570 struct rb_node *next;
571 struct regmap_range_node *range_node;
573 next = rb_first(&map->range_tree);
574 while (next) {
575 range_node = rb_entry(next, struct regmap_range_node, node);
576 next = rb_next(&range_node->node);
577 rb_erase(&range_node->node, &map->range_tree);
578 kfree(range_node);
581 kfree(map->selector_work_buf);
584 static int regmap_set_name(struct regmap *map, const struct regmap_config *config)
586 if (config->name) {
587 const char *name = kstrdup_const(config->name, GFP_KERNEL);
589 if (!name)
590 return -ENOMEM;
592 kfree_const(map->name);
593 map->name = name;
596 return 0;
599 int regmap_attach_dev(struct device *dev, struct regmap *map,
600 const struct regmap_config *config)
602 struct regmap **m;
603 int ret;
605 map->dev = dev;
607 ret = regmap_set_name(map, config);
608 if (ret)
609 return ret;
611 regmap_debugfs_init(map);
613 /* Add a devres resource for dev_get_regmap() */
614 m = devres_alloc(dev_get_regmap_release, sizeof(*m), GFP_KERNEL);
615 if (!m) {
616 regmap_debugfs_exit(map);
617 return -ENOMEM;
619 *m = map;
620 devres_add(dev, m);
622 return 0;
624 EXPORT_SYMBOL_GPL(regmap_attach_dev);
626 static enum regmap_endian regmap_get_reg_endian(const struct regmap_bus *bus,
627 const struct regmap_config *config)
629 enum regmap_endian endian;
631 /* Retrieve the endianness specification from the regmap config */
632 endian = config->reg_format_endian;
634 /* If the regmap config specified a non-default value, use that */
635 if (endian != REGMAP_ENDIAN_DEFAULT)
636 return endian;
638 /* Retrieve the endianness specification from the bus config */
639 if (bus && bus->reg_format_endian_default)
640 endian = bus->reg_format_endian_default;
642 /* If the bus specified a non-default value, use that */
643 if (endian != REGMAP_ENDIAN_DEFAULT)
644 return endian;
646 /* Use this if no other value was found */
647 return REGMAP_ENDIAN_BIG;
650 enum regmap_endian regmap_get_val_endian(struct device *dev,
651 const struct regmap_bus *bus,
652 const struct regmap_config *config)
654 struct fwnode_handle *fwnode = dev ? dev_fwnode(dev) : NULL;
655 enum regmap_endian endian;
657 /* Retrieve the endianness specification from the regmap config */
658 endian = config->val_format_endian;
660 /* If the regmap config specified a non-default value, use that */
661 if (endian != REGMAP_ENDIAN_DEFAULT)
662 return endian;
664 /* If the firmware node exist try to get endianness from it */
665 if (fwnode_property_read_bool(fwnode, "big-endian"))
666 endian = REGMAP_ENDIAN_BIG;
667 else if (fwnode_property_read_bool(fwnode, "little-endian"))
668 endian = REGMAP_ENDIAN_LITTLE;
669 else if (fwnode_property_read_bool(fwnode, "native-endian"))
670 endian = REGMAP_ENDIAN_NATIVE;
672 /* If the endianness was specified in fwnode, use that */
673 if (endian != REGMAP_ENDIAN_DEFAULT)
674 return endian;
676 /* Retrieve the endianness specification from the bus config */
677 if (bus && bus->val_format_endian_default)
678 endian = bus->val_format_endian_default;
680 /* If the bus specified a non-default value, use that */
681 if (endian != REGMAP_ENDIAN_DEFAULT)
682 return endian;
684 /* Use this if no other value was found */
685 return REGMAP_ENDIAN_BIG;
687 EXPORT_SYMBOL_GPL(regmap_get_val_endian);
689 struct regmap *__regmap_init(struct device *dev,
690 const struct regmap_bus *bus,
691 void *bus_context,
692 const struct regmap_config *config,
693 struct lock_class_key *lock_key,
694 const char *lock_name)
696 struct regmap *map;
697 int ret = -EINVAL;
698 enum regmap_endian reg_endian, val_endian;
699 int i, j;
701 if (!config)
702 goto err;
704 map = kzalloc(sizeof(*map), GFP_KERNEL);
705 if (map == NULL) {
706 ret = -ENOMEM;
707 goto err;
710 ret = regmap_set_name(map, config);
711 if (ret)
712 goto err_map;
714 ret = -EINVAL; /* Later error paths rely on this */
716 if (config->disable_locking) {
717 map->lock = map->unlock = regmap_lock_unlock_none;
718 regmap_debugfs_disable(map);
719 } else if (config->lock && config->unlock) {
720 map->lock = config->lock;
721 map->unlock = config->unlock;
722 map->lock_arg = config->lock_arg;
723 } else if (config->use_hwlock) {
724 map->hwlock = hwspin_lock_request_specific(config->hwlock_id);
725 if (!map->hwlock) {
726 ret = -ENXIO;
727 goto err_name;
730 switch (config->hwlock_mode) {
731 case HWLOCK_IRQSTATE:
732 map->lock = regmap_lock_hwlock_irqsave;
733 map->unlock = regmap_unlock_hwlock_irqrestore;
734 break;
735 case HWLOCK_IRQ:
736 map->lock = regmap_lock_hwlock_irq;
737 map->unlock = regmap_unlock_hwlock_irq;
738 break;
739 default:
740 map->lock = regmap_lock_hwlock;
741 map->unlock = regmap_unlock_hwlock;
742 break;
745 map->lock_arg = map;
746 } else {
747 if ((bus && bus->fast_io) ||
748 config->fast_io) {
749 spin_lock_init(&map->spinlock);
750 map->lock = regmap_lock_spinlock;
751 map->unlock = regmap_unlock_spinlock;
752 lockdep_set_class_and_name(&map->spinlock,
753 lock_key, lock_name);
754 } else {
755 mutex_init(&map->mutex);
756 map->lock = regmap_lock_mutex;
757 map->unlock = regmap_unlock_mutex;
758 lockdep_set_class_and_name(&map->mutex,
759 lock_key, lock_name);
761 map->lock_arg = map;
765 * When we write in fast-paths with regmap_bulk_write() don't allocate
766 * scratch buffers with sleeping allocations.
768 if ((bus && bus->fast_io) || config->fast_io)
769 map->alloc_flags = GFP_ATOMIC;
770 else
771 map->alloc_flags = GFP_KERNEL;
773 map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8);
774 map->format.pad_bytes = config->pad_bits / 8;
775 map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8);
776 map->format.buf_size = DIV_ROUND_UP(config->reg_bits +
777 config->val_bits + config->pad_bits, 8);
778 map->reg_shift = config->pad_bits % 8;
779 if (config->reg_stride)
780 map->reg_stride = config->reg_stride;
781 else
782 map->reg_stride = 1;
783 if (is_power_of_2(map->reg_stride))
784 map->reg_stride_order = ilog2(map->reg_stride);
785 else
786 map->reg_stride_order = -1;
787 map->use_single_read = config->use_single_read || !bus || !bus->read;
788 map->use_single_write = config->use_single_write || !bus || !bus->write;
789 map->can_multi_write = config->can_multi_write && bus && bus->write;
790 if (bus) {
791 map->max_raw_read = bus->max_raw_read;
792 map->max_raw_write = bus->max_raw_write;
794 map->dev = dev;
795 map->bus = bus;
796 map->bus_context = bus_context;
797 map->max_register = config->max_register;
798 map->wr_table = config->wr_table;
799 map->rd_table = config->rd_table;
800 map->volatile_table = config->volatile_table;
801 map->precious_table = config->precious_table;
802 map->wr_noinc_table = config->wr_noinc_table;
803 map->rd_noinc_table = config->rd_noinc_table;
804 map->writeable_reg = config->writeable_reg;
805 map->readable_reg = config->readable_reg;
806 map->volatile_reg = config->volatile_reg;
807 map->precious_reg = config->precious_reg;
808 map->writeable_noinc_reg = config->writeable_noinc_reg;
809 map->readable_noinc_reg = config->readable_noinc_reg;
810 map->cache_type = config->cache_type;
812 spin_lock_init(&map->async_lock);
813 INIT_LIST_HEAD(&map->async_list);
814 INIT_LIST_HEAD(&map->async_free);
815 init_waitqueue_head(&map->async_waitq);
817 if (config->read_flag_mask ||
818 config->write_flag_mask ||
819 config->zero_flag_mask) {
820 map->read_flag_mask = config->read_flag_mask;
821 map->write_flag_mask = config->write_flag_mask;
822 } else if (bus) {
823 map->read_flag_mask = bus->read_flag_mask;
826 if (!bus) {
827 map->reg_read = config->reg_read;
828 map->reg_write = config->reg_write;
830 map->defer_caching = false;
831 goto skip_format_initialization;
832 } else if (!bus->read || !bus->write) {
833 map->reg_read = _regmap_bus_reg_read;
834 map->reg_write = _regmap_bus_reg_write;
835 map->reg_update_bits = bus->reg_update_bits;
837 map->defer_caching = false;
838 goto skip_format_initialization;
839 } else {
840 map->reg_read = _regmap_bus_read;
841 map->reg_update_bits = bus->reg_update_bits;
844 reg_endian = regmap_get_reg_endian(bus, config);
845 val_endian = regmap_get_val_endian(dev, bus, config);
847 switch (config->reg_bits + map->reg_shift) {
848 case 2:
849 switch (config->val_bits) {
850 case 6:
851 map->format.format_write = regmap_format_2_6_write;
852 break;
853 default:
854 goto err_hwlock;
856 break;
858 case 4:
859 switch (config->val_bits) {
860 case 12:
861 map->format.format_write = regmap_format_4_12_write;
862 break;
863 default:
864 goto err_hwlock;
866 break;
868 case 7:
869 switch (config->val_bits) {
870 case 9:
871 map->format.format_write = regmap_format_7_9_write;
872 break;
873 default:
874 goto err_hwlock;
876 break;
878 case 10:
879 switch (config->val_bits) {
880 case 14:
881 map->format.format_write = regmap_format_10_14_write;
882 break;
883 default:
884 goto err_hwlock;
886 break;
888 case 8:
889 map->format.format_reg = regmap_format_8;
890 break;
892 case 16:
893 switch (reg_endian) {
894 case REGMAP_ENDIAN_BIG:
895 map->format.format_reg = regmap_format_16_be;
896 break;
897 case REGMAP_ENDIAN_LITTLE:
898 map->format.format_reg = regmap_format_16_le;
899 break;
900 case REGMAP_ENDIAN_NATIVE:
901 map->format.format_reg = regmap_format_16_native;
902 break;
903 default:
904 goto err_hwlock;
906 break;
908 case 24:
909 if (reg_endian != REGMAP_ENDIAN_BIG)
910 goto err_hwlock;
911 map->format.format_reg = regmap_format_24;
912 break;
914 case 32:
915 switch (reg_endian) {
916 case REGMAP_ENDIAN_BIG:
917 map->format.format_reg = regmap_format_32_be;
918 break;
919 case REGMAP_ENDIAN_LITTLE:
920 map->format.format_reg = regmap_format_32_le;
921 break;
922 case REGMAP_ENDIAN_NATIVE:
923 map->format.format_reg = regmap_format_32_native;
924 break;
925 default:
926 goto err_hwlock;
928 break;
930 #ifdef CONFIG_64BIT
931 case 64:
932 switch (reg_endian) {
933 case REGMAP_ENDIAN_BIG:
934 map->format.format_reg = regmap_format_64_be;
935 break;
936 case REGMAP_ENDIAN_LITTLE:
937 map->format.format_reg = regmap_format_64_le;
938 break;
939 case REGMAP_ENDIAN_NATIVE:
940 map->format.format_reg = regmap_format_64_native;
941 break;
942 default:
943 goto err_hwlock;
945 break;
946 #endif
948 default:
949 goto err_hwlock;
952 if (val_endian == REGMAP_ENDIAN_NATIVE)
953 map->format.parse_inplace = regmap_parse_inplace_noop;
955 switch (config->val_bits) {
956 case 8:
957 map->format.format_val = regmap_format_8;
958 map->format.parse_val = regmap_parse_8;
959 map->format.parse_inplace = regmap_parse_inplace_noop;
960 break;
961 case 16:
962 switch (val_endian) {
963 case REGMAP_ENDIAN_BIG:
964 map->format.format_val = regmap_format_16_be;
965 map->format.parse_val = regmap_parse_16_be;
966 map->format.parse_inplace = regmap_parse_16_be_inplace;
967 break;
968 case REGMAP_ENDIAN_LITTLE:
969 map->format.format_val = regmap_format_16_le;
970 map->format.parse_val = regmap_parse_16_le;
971 map->format.parse_inplace = regmap_parse_16_le_inplace;
972 break;
973 case REGMAP_ENDIAN_NATIVE:
974 map->format.format_val = regmap_format_16_native;
975 map->format.parse_val = regmap_parse_16_native;
976 break;
977 default:
978 goto err_hwlock;
980 break;
981 case 24:
982 if (val_endian != REGMAP_ENDIAN_BIG)
983 goto err_hwlock;
984 map->format.format_val = regmap_format_24;
985 map->format.parse_val = regmap_parse_24;
986 break;
987 case 32:
988 switch (val_endian) {
989 case REGMAP_ENDIAN_BIG:
990 map->format.format_val = regmap_format_32_be;
991 map->format.parse_val = regmap_parse_32_be;
992 map->format.parse_inplace = regmap_parse_32_be_inplace;
993 break;
994 case REGMAP_ENDIAN_LITTLE:
995 map->format.format_val = regmap_format_32_le;
996 map->format.parse_val = regmap_parse_32_le;
997 map->format.parse_inplace = regmap_parse_32_le_inplace;
998 break;
999 case REGMAP_ENDIAN_NATIVE:
1000 map->format.format_val = regmap_format_32_native;
1001 map->format.parse_val = regmap_parse_32_native;
1002 break;
1003 default:
1004 goto err_hwlock;
1006 break;
1007 #ifdef CONFIG_64BIT
1008 case 64:
1009 switch (val_endian) {
1010 case REGMAP_ENDIAN_BIG:
1011 map->format.format_val = regmap_format_64_be;
1012 map->format.parse_val = regmap_parse_64_be;
1013 map->format.parse_inplace = regmap_parse_64_be_inplace;
1014 break;
1015 case REGMAP_ENDIAN_LITTLE:
1016 map->format.format_val = regmap_format_64_le;
1017 map->format.parse_val = regmap_parse_64_le;
1018 map->format.parse_inplace = regmap_parse_64_le_inplace;
1019 break;
1020 case REGMAP_ENDIAN_NATIVE:
1021 map->format.format_val = regmap_format_64_native;
1022 map->format.parse_val = regmap_parse_64_native;
1023 break;
1024 default:
1025 goto err_hwlock;
1027 break;
1028 #endif
1031 if (map->format.format_write) {
1032 if ((reg_endian != REGMAP_ENDIAN_BIG) ||
1033 (val_endian != REGMAP_ENDIAN_BIG))
1034 goto err_hwlock;
1035 map->use_single_write = true;
1038 if (!map->format.format_write &&
1039 !(map->format.format_reg && map->format.format_val))
1040 goto err_hwlock;
1042 map->work_buf = kzalloc(map->format.buf_size, GFP_KERNEL);
1043 if (map->work_buf == NULL) {
1044 ret = -ENOMEM;
1045 goto err_hwlock;
1048 if (map->format.format_write) {
1049 map->defer_caching = false;
1050 map->reg_write = _regmap_bus_formatted_write;
1051 } else if (map->format.format_val) {
1052 map->defer_caching = true;
1053 map->reg_write = _regmap_bus_raw_write;
1056 skip_format_initialization:
1058 map->range_tree = RB_ROOT;
1059 for (i = 0; i < config->num_ranges; i++) {
1060 const struct regmap_range_cfg *range_cfg = &config->ranges[i];
1061 struct regmap_range_node *new;
1063 /* Sanity check */
1064 if (range_cfg->range_max < range_cfg->range_min) {
1065 dev_err(map->dev, "Invalid range %d: %d < %d\n", i,
1066 range_cfg->range_max, range_cfg->range_min);
1067 goto err_range;
1070 if (range_cfg->range_max > map->max_register) {
1071 dev_err(map->dev, "Invalid range %d: %d > %d\n", i,
1072 range_cfg->range_max, map->max_register);
1073 goto err_range;
1076 if (range_cfg->selector_reg > map->max_register) {
1077 dev_err(map->dev,
1078 "Invalid range %d: selector out of map\n", i);
1079 goto err_range;
1082 if (range_cfg->window_len == 0) {
1083 dev_err(map->dev, "Invalid range %d: window_len 0\n",
1085 goto err_range;
1088 /* Make sure, that this register range has no selector
1089 or data window within its boundary */
1090 for (j = 0; j < config->num_ranges; j++) {
1091 unsigned sel_reg = config->ranges[j].selector_reg;
1092 unsigned win_min = config->ranges[j].window_start;
1093 unsigned win_max = win_min +
1094 config->ranges[j].window_len - 1;
1096 /* Allow data window inside its own virtual range */
1097 if (j == i)
1098 continue;
1100 if (range_cfg->range_min <= sel_reg &&
1101 sel_reg <= range_cfg->range_max) {
1102 dev_err(map->dev,
1103 "Range %d: selector for %d in window\n",
1104 i, j);
1105 goto err_range;
1108 if (!(win_max < range_cfg->range_min ||
1109 win_min > range_cfg->range_max)) {
1110 dev_err(map->dev,
1111 "Range %d: window for %d in window\n",
1112 i, j);
1113 goto err_range;
1117 new = kzalloc(sizeof(*new), GFP_KERNEL);
1118 if (new == NULL) {
1119 ret = -ENOMEM;
1120 goto err_range;
1123 new->map = map;
1124 new->name = range_cfg->name;
1125 new->range_min = range_cfg->range_min;
1126 new->range_max = range_cfg->range_max;
1127 new->selector_reg = range_cfg->selector_reg;
1128 new->selector_mask = range_cfg->selector_mask;
1129 new->selector_shift = range_cfg->selector_shift;
1130 new->window_start = range_cfg->window_start;
1131 new->window_len = range_cfg->window_len;
1133 if (!_regmap_range_add(map, new)) {
1134 dev_err(map->dev, "Failed to add range %d\n", i);
1135 kfree(new);
1136 goto err_range;
1139 if (map->selector_work_buf == NULL) {
1140 map->selector_work_buf =
1141 kzalloc(map->format.buf_size, GFP_KERNEL);
1142 if (map->selector_work_buf == NULL) {
1143 ret = -ENOMEM;
1144 goto err_range;
1149 ret = regcache_init(map, config);
1150 if (ret != 0)
1151 goto err_range;
1153 if (dev) {
1154 ret = regmap_attach_dev(dev, map, config);
1155 if (ret != 0)
1156 goto err_regcache;
1157 } else {
1158 regmap_debugfs_init(map);
1161 return map;
1163 err_regcache:
1164 regcache_exit(map);
1165 err_range:
1166 regmap_range_exit(map);
1167 kfree(map->work_buf);
1168 err_hwlock:
1169 if (map->hwlock)
1170 hwspin_lock_free(map->hwlock);
1171 err_name:
1172 kfree_const(map->name);
1173 err_map:
1174 kfree(map);
1175 err:
1176 return ERR_PTR(ret);
1178 EXPORT_SYMBOL_GPL(__regmap_init);
1180 static void devm_regmap_release(struct device *dev, void *res)
1182 regmap_exit(*(struct regmap **)res);
1185 struct regmap *__devm_regmap_init(struct device *dev,
1186 const struct regmap_bus *bus,
1187 void *bus_context,
1188 const struct regmap_config *config,
1189 struct lock_class_key *lock_key,
1190 const char *lock_name)
1192 struct regmap **ptr, *regmap;
1194 ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL);
1195 if (!ptr)
1196 return ERR_PTR(-ENOMEM);
1198 regmap = __regmap_init(dev, bus, bus_context, config,
1199 lock_key, lock_name);
1200 if (!IS_ERR(regmap)) {
1201 *ptr = regmap;
1202 devres_add(dev, ptr);
1203 } else {
1204 devres_free(ptr);
1207 return regmap;
1209 EXPORT_SYMBOL_GPL(__devm_regmap_init);
1211 static void regmap_field_init(struct regmap_field *rm_field,
1212 struct regmap *regmap, struct reg_field reg_field)
1214 rm_field->regmap = regmap;
1215 rm_field->reg = reg_field.reg;
1216 rm_field->shift = reg_field.lsb;
1217 rm_field->mask = GENMASK(reg_field.msb, reg_field.lsb);
1218 rm_field->id_size = reg_field.id_size;
1219 rm_field->id_offset = reg_field.id_offset;
1223 * devm_regmap_field_alloc() - Allocate and initialise a register field.
1225 * @dev: Device that will be interacted with
1226 * @regmap: regmap bank in which this register field is located.
1227 * @reg_field: Register field with in the bank.
1229 * The return value will be an ERR_PTR() on error or a valid pointer
1230 * to a struct regmap_field. The regmap_field will be automatically freed
1231 * by the device management code.
1233 struct regmap_field *devm_regmap_field_alloc(struct device *dev,
1234 struct regmap *regmap, struct reg_field reg_field)
1236 struct regmap_field *rm_field = devm_kzalloc(dev,
1237 sizeof(*rm_field), GFP_KERNEL);
1238 if (!rm_field)
1239 return ERR_PTR(-ENOMEM);
1241 regmap_field_init(rm_field, regmap, reg_field);
1243 return rm_field;
1246 EXPORT_SYMBOL_GPL(devm_regmap_field_alloc);
1249 * devm_regmap_field_free() - Free a register field allocated using
1250 * devm_regmap_field_alloc.
1252 * @dev: Device that will be interacted with
1253 * @field: regmap field which should be freed.
1255 * Free register field allocated using devm_regmap_field_alloc(). Usually
1256 * drivers need not call this function, as the memory allocated via devm
1257 * will be freed as per device-driver life-cyle.
1259 void devm_regmap_field_free(struct device *dev,
1260 struct regmap_field *field)
1262 devm_kfree(dev, field);
1264 EXPORT_SYMBOL_GPL(devm_regmap_field_free);
1267 * regmap_field_alloc() - Allocate and initialise a register field.
1269 * @regmap: regmap bank in which this register field is located.
1270 * @reg_field: Register field with in the bank.
1272 * The return value will be an ERR_PTR() on error or a valid pointer
1273 * to a struct regmap_field. The regmap_field should be freed by the
1274 * user once its finished working with it using regmap_field_free().
1276 struct regmap_field *regmap_field_alloc(struct regmap *regmap,
1277 struct reg_field reg_field)
1279 struct regmap_field *rm_field = kzalloc(sizeof(*rm_field), GFP_KERNEL);
1281 if (!rm_field)
1282 return ERR_PTR(-ENOMEM);
1284 regmap_field_init(rm_field, regmap, reg_field);
1286 return rm_field;
1288 EXPORT_SYMBOL_GPL(regmap_field_alloc);
1291 * regmap_field_free() - Free register field allocated using
1292 * regmap_field_alloc.
1294 * @field: regmap field which should be freed.
1296 void regmap_field_free(struct regmap_field *field)
1298 kfree(field);
1300 EXPORT_SYMBOL_GPL(regmap_field_free);
1303 * regmap_reinit_cache() - Reinitialise the current register cache
1305 * @map: Register map to operate on.
1306 * @config: New configuration. Only the cache data will be used.
1308 * Discard any existing register cache for the map and initialize a
1309 * new cache. This can be used to restore the cache to defaults or to
1310 * update the cache configuration to reflect runtime discovery of the
1311 * hardware.
1313 * No explicit locking is done here, the user needs to ensure that
1314 * this function will not race with other calls to regmap.
1316 int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
1318 int ret;
1320 regcache_exit(map);
1321 regmap_debugfs_exit(map);
1323 map->max_register = config->max_register;
1324 map->writeable_reg = config->writeable_reg;
1325 map->readable_reg = config->readable_reg;
1326 map->volatile_reg = config->volatile_reg;
1327 map->precious_reg = config->precious_reg;
1328 map->writeable_noinc_reg = config->writeable_noinc_reg;
1329 map->readable_noinc_reg = config->readable_noinc_reg;
1330 map->cache_type = config->cache_type;
1332 ret = regmap_set_name(map, config);
1333 if (ret)
1334 return ret;
1336 regmap_debugfs_init(map);
1338 map->cache_bypass = false;
1339 map->cache_only = false;
1341 return regcache_init(map, config);
1343 EXPORT_SYMBOL_GPL(regmap_reinit_cache);
1346 * regmap_exit() - Free a previously allocated register map
1348 * @map: Register map to operate on.
1350 void regmap_exit(struct regmap *map)
1352 struct regmap_async *async;
1354 regcache_exit(map);
1355 regmap_debugfs_exit(map);
1356 regmap_range_exit(map);
1357 if (map->bus && map->bus->free_context)
1358 map->bus->free_context(map->bus_context);
1359 kfree(map->work_buf);
1360 while (!list_empty(&map->async_free)) {
1361 async = list_first_entry_or_null(&map->async_free,
1362 struct regmap_async,
1363 list);
1364 list_del(&async->list);
1365 kfree(async->work_buf);
1366 kfree(async);
1368 if (map->hwlock)
1369 hwspin_lock_free(map->hwlock);
1370 kfree_const(map->name);
1371 kfree(map->patch);
1372 kfree(map);
1374 EXPORT_SYMBOL_GPL(regmap_exit);
1376 static int dev_get_regmap_match(struct device *dev, void *res, void *data)
1378 struct regmap **r = res;
1379 if (!r || !*r) {
1380 WARN_ON(!r || !*r);
1381 return 0;
1384 /* If the user didn't specify a name match any */
1385 if (data)
1386 return !strcmp((*r)->name, data);
1387 else
1388 return 1;
1392 * dev_get_regmap() - Obtain the regmap (if any) for a device
1394 * @dev: Device to retrieve the map for
1395 * @name: Optional name for the register map, usually NULL.
1397 * Returns the regmap for the device if one is present, or NULL. If
1398 * name is specified then it must match the name specified when
1399 * registering the device, if it is NULL then the first regmap found
1400 * will be used. Devices with multiple register maps are very rare,
1401 * generic code should normally not need to specify a name.
1403 struct regmap *dev_get_regmap(struct device *dev, const char *name)
1405 struct regmap **r = devres_find(dev, dev_get_regmap_release,
1406 dev_get_regmap_match, (void *)name);
1408 if (!r)
1409 return NULL;
1410 return *r;
1412 EXPORT_SYMBOL_GPL(dev_get_regmap);
1415 * regmap_get_device() - Obtain the device from a regmap
1417 * @map: Register map to operate on.
1419 * Returns the underlying device that the regmap has been created for.
1421 struct device *regmap_get_device(struct regmap *map)
1423 return map->dev;
1425 EXPORT_SYMBOL_GPL(regmap_get_device);
1427 static int _regmap_select_page(struct regmap *map, unsigned int *reg,
1428 struct regmap_range_node *range,
1429 unsigned int val_num)
1431 void *orig_work_buf;
1432 unsigned int win_offset;
1433 unsigned int win_page;
1434 bool page_chg;
1435 int ret;
1437 win_offset = (*reg - range->range_min) % range->window_len;
1438 win_page = (*reg - range->range_min) / range->window_len;
1440 if (val_num > 1) {
1441 /* Bulk write shouldn't cross range boundary */
1442 if (*reg + val_num - 1 > range->range_max)
1443 return -EINVAL;
1445 /* ... or single page boundary */
1446 if (val_num > range->window_len - win_offset)
1447 return -EINVAL;
1450 /* It is possible to have selector register inside data window.
1451 In that case, selector register is located on every page and
1452 it needs no page switching, when accessed alone. */
1453 if (val_num > 1 ||
1454 range->window_start + win_offset != range->selector_reg) {
1455 /* Use separate work_buf during page switching */
1456 orig_work_buf = map->work_buf;
1457 map->work_buf = map->selector_work_buf;
1459 ret = _regmap_update_bits(map, range->selector_reg,
1460 range->selector_mask,
1461 win_page << range->selector_shift,
1462 &page_chg, false);
1464 map->work_buf = orig_work_buf;
1466 if (ret != 0)
1467 return ret;
1470 *reg = range->window_start + win_offset;
1472 return 0;
1475 static void regmap_set_work_buf_flag_mask(struct regmap *map, int max_bytes,
1476 unsigned long mask)
1478 u8 *buf;
1479 int i;
1481 if (!mask || !map->work_buf)
1482 return;
1484 buf = map->work_buf;
1486 for (i = 0; i < max_bytes; i++)
1487 buf[i] |= (mask >> (8 * i)) & 0xff;
1490 static int _regmap_raw_write_impl(struct regmap *map, unsigned int reg,
1491 const void *val, size_t val_len, bool noinc)
1493 struct regmap_range_node *range;
1494 unsigned long flags;
1495 void *work_val = map->work_buf + map->format.reg_bytes +
1496 map->format.pad_bytes;
1497 void *buf;
1498 int ret = -ENOTSUPP;
1499 size_t len;
1500 int i;
1502 WARN_ON(!map->bus);
1504 /* Check for unwritable or noinc registers in range
1505 * before we start
1507 if (!regmap_writeable_noinc(map, reg)) {
1508 for (i = 0; i < val_len / map->format.val_bytes; i++) {
1509 unsigned int element =
1510 reg + regmap_get_offset(map, i);
1511 if (!regmap_writeable(map, element) ||
1512 regmap_writeable_noinc(map, element))
1513 return -EINVAL;
1517 if (!map->cache_bypass && map->format.parse_val) {
1518 unsigned int ival;
1519 int val_bytes = map->format.val_bytes;
1520 for (i = 0; i < val_len / val_bytes; i++) {
1521 ival = map->format.parse_val(val + (i * val_bytes));
1522 ret = regcache_write(map,
1523 reg + regmap_get_offset(map, i),
1524 ival);
1525 if (ret) {
1526 dev_err(map->dev,
1527 "Error in caching of register: %x ret: %d\n",
1528 reg + i, ret);
1529 return ret;
1532 if (map->cache_only) {
1533 map->cache_dirty = true;
1534 return 0;
1538 range = _regmap_range_lookup(map, reg);
1539 if (range) {
1540 int val_num = val_len / map->format.val_bytes;
1541 int win_offset = (reg - range->range_min) % range->window_len;
1542 int win_residue = range->window_len - win_offset;
1544 /* If the write goes beyond the end of the window split it */
1545 while (val_num > win_residue) {
1546 dev_dbg(map->dev, "Writing window %d/%zu\n",
1547 win_residue, val_len / map->format.val_bytes);
1548 ret = _regmap_raw_write_impl(map, reg, val,
1549 win_residue *
1550 map->format.val_bytes, noinc);
1551 if (ret != 0)
1552 return ret;
1554 reg += win_residue;
1555 val_num -= win_residue;
1556 val += win_residue * map->format.val_bytes;
1557 val_len -= win_residue * map->format.val_bytes;
1559 win_offset = (reg - range->range_min) %
1560 range->window_len;
1561 win_residue = range->window_len - win_offset;
1564 ret = _regmap_select_page(map, &reg, range, noinc ? 1 : val_num);
1565 if (ret != 0)
1566 return ret;
1569 map->format.format_reg(map->work_buf, reg, map->reg_shift);
1570 regmap_set_work_buf_flag_mask(map, map->format.reg_bytes,
1571 map->write_flag_mask);
1574 * Essentially all I/O mechanisms will be faster with a single
1575 * buffer to write. Since register syncs often generate raw
1576 * writes of single registers optimise that case.
1578 if (val != work_val && val_len == map->format.val_bytes) {
1579 memcpy(work_val, val, map->format.val_bytes);
1580 val = work_val;
1583 if (map->async && map->bus->async_write) {
1584 struct regmap_async *async;
1586 trace_regmap_async_write_start(map, reg, val_len);
1588 spin_lock_irqsave(&map->async_lock, flags);
1589 async = list_first_entry_or_null(&map->async_free,
1590 struct regmap_async,
1591 list);
1592 if (async)
1593 list_del(&async->list);
1594 spin_unlock_irqrestore(&map->async_lock, flags);
1596 if (!async) {
1597 async = map->bus->async_alloc();
1598 if (!async)
1599 return -ENOMEM;
1601 async->work_buf = kzalloc(map->format.buf_size,
1602 GFP_KERNEL | GFP_DMA);
1603 if (!async->work_buf) {
1604 kfree(async);
1605 return -ENOMEM;
1609 async->map = map;
1611 /* If the caller supplied the value we can use it safely. */
1612 memcpy(async->work_buf, map->work_buf, map->format.pad_bytes +
1613 map->format.reg_bytes + map->format.val_bytes);
1615 spin_lock_irqsave(&map->async_lock, flags);
1616 list_add_tail(&async->list, &map->async_list);
1617 spin_unlock_irqrestore(&map->async_lock, flags);
1619 if (val != work_val)
1620 ret = map->bus->async_write(map->bus_context,
1621 async->work_buf,
1622 map->format.reg_bytes +
1623 map->format.pad_bytes,
1624 val, val_len, async);
1625 else
1626 ret = map->bus->async_write(map->bus_context,
1627 async->work_buf,
1628 map->format.reg_bytes +
1629 map->format.pad_bytes +
1630 val_len, NULL, 0, async);
1632 if (ret != 0) {
1633 dev_err(map->dev, "Failed to schedule write: %d\n",
1634 ret);
1636 spin_lock_irqsave(&map->async_lock, flags);
1637 list_move(&async->list, &map->async_free);
1638 spin_unlock_irqrestore(&map->async_lock, flags);
1641 return ret;
1644 trace_regmap_hw_write_start(map, reg, val_len / map->format.val_bytes);
1646 /* If we're doing a single register write we can probably just
1647 * send the work_buf directly, otherwise try to do a gather
1648 * write.
1650 if (val == work_val)
1651 ret = map->bus->write(map->bus_context, map->work_buf,
1652 map->format.reg_bytes +
1653 map->format.pad_bytes +
1654 val_len);
1655 else if (map->bus->gather_write)
1656 ret = map->bus->gather_write(map->bus_context, map->work_buf,
1657 map->format.reg_bytes +
1658 map->format.pad_bytes,
1659 val, val_len);
1660 else
1661 ret = -ENOTSUPP;
1663 /* If that didn't work fall back on linearising by hand. */
1664 if (ret == -ENOTSUPP) {
1665 len = map->format.reg_bytes + map->format.pad_bytes + val_len;
1666 buf = kzalloc(len, GFP_KERNEL);
1667 if (!buf)
1668 return -ENOMEM;
1670 memcpy(buf, map->work_buf, map->format.reg_bytes);
1671 memcpy(buf + map->format.reg_bytes + map->format.pad_bytes,
1672 val, val_len);
1673 ret = map->bus->write(map->bus_context, buf, len);
1675 kfree(buf);
1676 } else if (ret != 0 && !map->cache_bypass && map->format.parse_val) {
1677 /* regcache_drop_region() takes lock that we already have,
1678 * thus call map->cache_ops->drop() directly
1680 if (map->cache_ops && map->cache_ops->drop)
1681 map->cache_ops->drop(map, reg, reg + 1);
1684 trace_regmap_hw_write_done(map, reg, val_len / map->format.val_bytes);
1686 return ret;
1690 * regmap_can_raw_write - Test if regmap_raw_write() is supported
1692 * @map: Map to check.
1694 bool regmap_can_raw_write(struct regmap *map)
1696 return map->bus && map->bus->write && map->format.format_val &&
1697 map->format.format_reg;
1699 EXPORT_SYMBOL_GPL(regmap_can_raw_write);
1702 * regmap_get_raw_read_max - Get the maximum size we can read
1704 * @map: Map to check.
1706 size_t regmap_get_raw_read_max(struct regmap *map)
1708 return map->max_raw_read;
1710 EXPORT_SYMBOL_GPL(regmap_get_raw_read_max);
1713 * regmap_get_raw_write_max - Get the maximum size we can read
1715 * @map: Map to check.
1717 size_t regmap_get_raw_write_max(struct regmap *map)
1719 return map->max_raw_write;
1721 EXPORT_SYMBOL_GPL(regmap_get_raw_write_max);
1723 static int _regmap_bus_formatted_write(void *context, unsigned int reg,
1724 unsigned int val)
1726 int ret;
1727 struct regmap_range_node *range;
1728 struct regmap *map = context;
1730 WARN_ON(!map->bus || !map->format.format_write);
1732 range = _regmap_range_lookup(map, reg);
1733 if (range) {
1734 ret = _regmap_select_page(map, &reg, range, 1);
1735 if (ret != 0)
1736 return ret;
1739 map->format.format_write(map, reg, val);
1741 trace_regmap_hw_write_start(map, reg, 1);
1743 ret = map->bus->write(map->bus_context, map->work_buf,
1744 map->format.buf_size);
1746 trace_regmap_hw_write_done(map, reg, 1);
1748 return ret;
1751 static int _regmap_bus_reg_write(void *context, unsigned int reg,
1752 unsigned int val)
1754 struct regmap *map = context;
1756 return map->bus->reg_write(map->bus_context, reg, val);
1759 static int _regmap_bus_raw_write(void *context, unsigned int reg,
1760 unsigned int val)
1762 struct regmap *map = context;
1764 WARN_ON(!map->bus || !map->format.format_val);
1766 map->format.format_val(map->work_buf + map->format.reg_bytes
1767 + map->format.pad_bytes, val, 0);
1768 return _regmap_raw_write_impl(map, reg,
1769 map->work_buf +
1770 map->format.reg_bytes +
1771 map->format.pad_bytes,
1772 map->format.val_bytes,
1773 false);
1776 static inline void *_regmap_map_get_context(struct regmap *map)
1778 return (map->bus) ? map : map->bus_context;
1781 int _regmap_write(struct regmap *map, unsigned int reg,
1782 unsigned int val)
1784 int ret;
1785 void *context = _regmap_map_get_context(map);
1787 if (!regmap_writeable(map, reg))
1788 return -EIO;
1790 if (!map->cache_bypass && !map->defer_caching) {
1791 ret = regcache_write(map, reg, val);
1792 if (ret != 0)
1793 return ret;
1794 if (map->cache_only) {
1795 map->cache_dirty = true;
1796 return 0;
1800 if (regmap_should_log(map))
1801 dev_info(map->dev, "%x <= %x\n", reg, val);
1803 trace_regmap_reg_write(map, reg, val);
1805 return map->reg_write(context, reg, val);
1809 * regmap_write() - Write a value to a single register
1811 * @map: Register map to write to
1812 * @reg: Register to write to
1813 * @val: Value to be written
1815 * A value of zero will be returned on success, a negative errno will
1816 * be returned in error cases.
1818 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
1820 int ret;
1822 if (!IS_ALIGNED(reg, map->reg_stride))
1823 return -EINVAL;
1825 map->lock(map->lock_arg);
1827 ret = _regmap_write(map, reg, val);
1829 map->unlock(map->lock_arg);
1831 return ret;
1833 EXPORT_SYMBOL_GPL(regmap_write);
1836 * regmap_write_async() - Write a value to a single register asynchronously
1838 * @map: Register map to write to
1839 * @reg: Register to write to
1840 * @val: Value to be written
1842 * A value of zero will be returned on success, a negative errno will
1843 * be returned in error cases.
1845 int regmap_write_async(struct regmap *map, unsigned int reg, unsigned int val)
1847 int ret;
1849 if (!IS_ALIGNED(reg, map->reg_stride))
1850 return -EINVAL;
1852 map->lock(map->lock_arg);
1854 map->async = true;
1856 ret = _regmap_write(map, reg, val);
1858 map->async = false;
1860 map->unlock(map->lock_arg);
1862 return ret;
1864 EXPORT_SYMBOL_GPL(regmap_write_async);
1866 int _regmap_raw_write(struct regmap *map, unsigned int reg,
1867 const void *val, size_t val_len, bool noinc)
1869 size_t val_bytes = map->format.val_bytes;
1870 size_t val_count = val_len / val_bytes;
1871 size_t chunk_count, chunk_bytes;
1872 size_t chunk_regs = val_count;
1873 int ret, i;
1875 if (!val_count)
1876 return -EINVAL;
1878 if (map->use_single_write)
1879 chunk_regs = 1;
1880 else if (map->max_raw_write && val_len > map->max_raw_write)
1881 chunk_regs = map->max_raw_write / val_bytes;
1883 chunk_count = val_count / chunk_regs;
1884 chunk_bytes = chunk_regs * val_bytes;
1886 /* Write as many bytes as possible with chunk_size */
1887 for (i = 0; i < chunk_count; i++) {
1888 ret = _regmap_raw_write_impl(map, reg, val, chunk_bytes, noinc);
1889 if (ret)
1890 return ret;
1892 reg += regmap_get_offset(map, chunk_regs);
1893 val += chunk_bytes;
1894 val_len -= chunk_bytes;
1897 /* Write remaining bytes */
1898 if (val_len)
1899 ret = _regmap_raw_write_impl(map, reg, val, val_len, noinc);
1901 return ret;
1905 * regmap_raw_write() - Write raw values to one or more registers
1907 * @map: Register map to write to
1908 * @reg: Initial register to write to
1909 * @val: Block of data to be written, laid out for direct transmission to the
1910 * device
1911 * @val_len: Length of data pointed to by val.
1913 * This function is intended to be used for things like firmware
1914 * download where a large block of data needs to be transferred to the
1915 * device. No formatting will be done on the data provided.
1917 * A value of zero will be returned on success, a negative errno will
1918 * be returned in error cases.
1920 int regmap_raw_write(struct regmap *map, unsigned int reg,
1921 const void *val, size_t val_len)
1923 int ret;
1925 if (!regmap_can_raw_write(map))
1926 return -EINVAL;
1927 if (val_len % map->format.val_bytes)
1928 return -EINVAL;
1930 map->lock(map->lock_arg);
1932 ret = _regmap_raw_write(map, reg, val, val_len, false);
1934 map->unlock(map->lock_arg);
1936 return ret;
1938 EXPORT_SYMBOL_GPL(regmap_raw_write);
1941 * regmap_noinc_write(): Write data from a register without incrementing the
1942 * register number
1944 * @map: Register map to write to
1945 * @reg: Register to write to
1946 * @val: Pointer to data buffer
1947 * @val_len: Length of output buffer in bytes.
1949 * The regmap API usually assumes that bulk bus write operations will write a
1950 * range of registers. Some devices have certain registers for which a write
1951 * operation can write to an internal FIFO.
1953 * The target register must be volatile but registers after it can be
1954 * completely unrelated cacheable registers.
1956 * This will attempt multiple writes as required to write val_len bytes.
1958 * A value of zero will be returned on success, a negative errno will be
1959 * returned in error cases.
1961 int regmap_noinc_write(struct regmap *map, unsigned int reg,
1962 const void *val, size_t val_len)
1964 size_t write_len;
1965 int ret;
1967 if (!map->bus)
1968 return -EINVAL;
1969 if (!map->bus->write)
1970 return -ENOTSUPP;
1971 if (val_len % map->format.val_bytes)
1972 return -EINVAL;
1973 if (!IS_ALIGNED(reg, map->reg_stride))
1974 return -EINVAL;
1975 if (val_len == 0)
1976 return -EINVAL;
1978 map->lock(map->lock_arg);
1980 if (!regmap_volatile(map, reg) || !regmap_writeable_noinc(map, reg)) {
1981 ret = -EINVAL;
1982 goto out_unlock;
1985 while (val_len) {
1986 if (map->max_raw_write && map->max_raw_write < val_len)
1987 write_len = map->max_raw_write;
1988 else
1989 write_len = val_len;
1990 ret = _regmap_raw_write(map, reg, val, write_len, true);
1991 if (ret)
1992 goto out_unlock;
1993 val = ((u8 *)val) + write_len;
1994 val_len -= write_len;
1997 out_unlock:
1998 map->unlock(map->lock_arg);
1999 return ret;
2001 EXPORT_SYMBOL_GPL(regmap_noinc_write);
2004 * regmap_field_update_bits_base() - Perform a read/modify/write cycle a
2005 * register field.
2007 * @field: Register field to write to
2008 * @mask: Bitmask to change
2009 * @val: Value to be written
2010 * @change: Boolean indicating if a write was done
2011 * @async: Boolean indicating asynchronously
2012 * @force: Boolean indicating use force update
2014 * Perform a read/modify/write cycle on the register field with change,
2015 * async, force option.
2017 * A value of zero will be returned on success, a negative errno will
2018 * be returned in error cases.
2020 int regmap_field_update_bits_base(struct regmap_field *field,
2021 unsigned int mask, unsigned int val,
2022 bool *change, bool async, bool force)
2024 mask = (mask << field->shift) & field->mask;
2026 return regmap_update_bits_base(field->regmap, field->reg,
2027 mask, val << field->shift,
2028 change, async, force);
2030 EXPORT_SYMBOL_GPL(regmap_field_update_bits_base);
2033 * regmap_fields_update_bits_base() - Perform a read/modify/write cycle a
2034 * register field with port ID
2036 * @field: Register field to write to
2037 * @id: port ID
2038 * @mask: Bitmask to change
2039 * @val: Value to be written
2040 * @change: Boolean indicating if a write was done
2041 * @async: Boolean indicating asynchronously
2042 * @force: Boolean indicating use force update
2044 * A value of zero will be returned on success, a negative errno will
2045 * be returned in error cases.
2047 int regmap_fields_update_bits_base(struct regmap_field *field, unsigned int id,
2048 unsigned int mask, unsigned int val,
2049 bool *change, bool async, bool force)
2051 if (id >= field->id_size)
2052 return -EINVAL;
2054 mask = (mask << field->shift) & field->mask;
2056 return regmap_update_bits_base(field->regmap,
2057 field->reg + (field->id_offset * id),
2058 mask, val << field->shift,
2059 change, async, force);
2061 EXPORT_SYMBOL_GPL(regmap_fields_update_bits_base);
2064 * regmap_bulk_write() - Write multiple registers to the device
2066 * @map: Register map to write to
2067 * @reg: First register to be write from
2068 * @val: Block of data to be written, in native register size for device
2069 * @val_count: Number of registers to write
2071 * This function is intended to be used for writing a large block of
2072 * data to the device either in single transfer or multiple transfer.
2074 * A value of zero will be returned on success, a negative errno will
2075 * be returned in error cases.
2077 int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
2078 size_t val_count)
2080 int ret = 0, i;
2081 size_t val_bytes = map->format.val_bytes;
2083 if (!IS_ALIGNED(reg, map->reg_stride))
2084 return -EINVAL;
2087 * Some devices don't support bulk write, for them we have a series of
2088 * single write operations.
2090 if (!map->bus || !map->format.parse_inplace) {
2091 map->lock(map->lock_arg);
2092 for (i = 0; i < val_count; i++) {
2093 unsigned int ival;
2095 switch (val_bytes) {
2096 case 1:
2097 ival = *(u8 *)(val + (i * val_bytes));
2098 break;
2099 case 2:
2100 ival = *(u16 *)(val + (i * val_bytes));
2101 break;
2102 case 4:
2103 ival = *(u32 *)(val + (i * val_bytes));
2104 break;
2105 #ifdef CONFIG_64BIT
2106 case 8:
2107 ival = *(u64 *)(val + (i * val_bytes));
2108 break;
2109 #endif
2110 default:
2111 ret = -EINVAL;
2112 goto out;
2115 ret = _regmap_write(map,
2116 reg + regmap_get_offset(map, i),
2117 ival);
2118 if (ret != 0)
2119 goto out;
2121 out:
2122 map->unlock(map->lock_arg);
2123 } else {
2124 void *wval;
2126 wval = kmemdup(val, val_count * val_bytes, map->alloc_flags);
2127 if (!wval)
2128 return -ENOMEM;
2130 for (i = 0; i < val_count * val_bytes; i += val_bytes)
2131 map->format.parse_inplace(wval + i);
2133 ret = regmap_raw_write(map, reg, wval, val_bytes * val_count);
2135 kfree(wval);
2137 return ret;
2139 EXPORT_SYMBOL_GPL(regmap_bulk_write);
2142 * _regmap_raw_multi_reg_write()
2144 * the (register,newvalue) pairs in regs have not been formatted, but
2145 * they are all in the same page and have been changed to being page
2146 * relative. The page register has been written if that was necessary.
2148 static int _regmap_raw_multi_reg_write(struct regmap *map,
2149 const struct reg_sequence *regs,
2150 size_t num_regs)
2152 int ret;
2153 void *buf;
2154 int i;
2155 u8 *u8;
2156 size_t val_bytes = map->format.val_bytes;
2157 size_t reg_bytes = map->format.reg_bytes;
2158 size_t pad_bytes = map->format.pad_bytes;
2159 size_t pair_size = reg_bytes + pad_bytes + val_bytes;
2160 size_t len = pair_size * num_regs;
2162 if (!len)
2163 return -EINVAL;
2165 buf = kzalloc(len, GFP_KERNEL);
2166 if (!buf)
2167 return -ENOMEM;
2169 /* We have to linearise by hand. */
2171 u8 = buf;
2173 for (i = 0; i < num_regs; i++) {
2174 unsigned int reg = regs[i].reg;
2175 unsigned int val = regs[i].def;
2176 trace_regmap_hw_write_start(map, reg, 1);
2177 map->format.format_reg(u8, reg, map->reg_shift);
2178 u8 += reg_bytes + pad_bytes;
2179 map->format.format_val(u8, val, 0);
2180 u8 += val_bytes;
2182 u8 = buf;
2183 *u8 |= map->write_flag_mask;
2185 ret = map->bus->write(map->bus_context, buf, len);
2187 kfree(buf);
2189 for (i = 0; i < num_regs; i++) {
2190 int reg = regs[i].reg;
2191 trace_regmap_hw_write_done(map, reg, 1);
2193 return ret;
2196 static unsigned int _regmap_register_page(struct regmap *map,
2197 unsigned int reg,
2198 struct regmap_range_node *range)
2200 unsigned int win_page = (reg - range->range_min) / range->window_len;
2202 return win_page;
2205 static int _regmap_range_multi_paged_reg_write(struct regmap *map,
2206 struct reg_sequence *regs,
2207 size_t num_regs)
2209 int ret;
2210 int i, n;
2211 struct reg_sequence *base;
2212 unsigned int this_page = 0;
2213 unsigned int page_change = 0;
2215 * the set of registers are not neccessarily in order, but
2216 * since the order of write must be preserved this algorithm
2217 * chops the set each time the page changes. This also applies
2218 * if there is a delay required at any point in the sequence.
2220 base = regs;
2221 for (i = 0, n = 0; i < num_regs; i++, n++) {
2222 unsigned int reg = regs[i].reg;
2223 struct regmap_range_node *range;
2225 range = _regmap_range_lookup(map, reg);
2226 if (range) {
2227 unsigned int win_page = _regmap_register_page(map, reg,
2228 range);
2230 if (i == 0)
2231 this_page = win_page;
2232 if (win_page != this_page) {
2233 this_page = win_page;
2234 page_change = 1;
2238 /* If we have both a page change and a delay make sure to
2239 * write the regs and apply the delay before we change the
2240 * page.
2243 if (page_change || regs[i].delay_us) {
2245 /* For situations where the first write requires
2246 * a delay we need to make sure we don't call
2247 * raw_multi_reg_write with n=0
2248 * This can't occur with page breaks as we
2249 * never write on the first iteration
2251 if (regs[i].delay_us && i == 0)
2252 n = 1;
2254 ret = _regmap_raw_multi_reg_write(map, base, n);
2255 if (ret != 0)
2256 return ret;
2258 if (regs[i].delay_us)
2259 udelay(regs[i].delay_us);
2261 base += n;
2262 n = 0;
2264 if (page_change) {
2265 ret = _regmap_select_page(map,
2266 &base[n].reg,
2267 range, 1);
2268 if (ret != 0)
2269 return ret;
2271 page_change = 0;
2277 if (n > 0)
2278 return _regmap_raw_multi_reg_write(map, base, n);
2279 return 0;
2282 static int _regmap_multi_reg_write(struct regmap *map,
2283 const struct reg_sequence *regs,
2284 size_t num_regs)
2286 int i;
2287 int ret;
2289 if (!map->can_multi_write) {
2290 for (i = 0; i < num_regs; i++) {
2291 ret = _regmap_write(map, regs[i].reg, regs[i].def);
2292 if (ret != 0)
2293 return ret;
2295 if (regs[i].delay_us)
2296 udelay(regs[i].delay_us);
2298 return 0;
2301 if (!map->format.parse_inplace)
2302 return -EINVAL;
2304 if (map->writeable_reg)
2305 for (i = 0; i < num_regs; i++) {
2306 int reg = regs[i].reg;
2307 if (!map->writeable_reg(map->dev, reg))
2308 return -EINVAL;
2309 if (!IS_ALIGNED(reg, map->reg_stride))
2310 return -EINVAL;
2313 if (!map->cache_bypass) {
2314 for (i = 0; i < num_regs; i++) {
2315 unsigned int val = regs[i].def;
2316 unsigned int reg = regs[i].reg;
2317 ret = regcache_write(map, reg, val);
2318 if (ret) {
2319 dev_err(map->dev,
2320 "Error in caching of register: %x ret: %d\n",
2321 reg, ret);
2322 return ret;
2325 if (map->cache_only) {
2326 map->cache_dirty = true;
2327 return 0;
2331 WARN_ON(!map->bus);
2333 for (i = 0; i < num_regs; i++) {
2334 unsigned int reg = regs[i].reg;
2335 struct regmap_range_node *range;
2337 /* Coalesce all the writes between a page break or a delay
2338 * in a sequence
2340 range = _regmap_range_lookup(map, reg);
2341 if (range || regs[i].delay_us) {
2342 size_t len = sizeof(struct reg_sequence)*num_regs;
2343 struct reg_sequence *base = kmemdup(regs, len,
2344 GFP_KERNEL);
2345 if (!base)
2346 return -ENOMEM;
2347 ret = _regmap_range_multi_paged_reg_write(map, base,
2348 num_regs);
2349 kfree(base);
2351 return ret;
2354 return _regmap_raw_multi_reg_write(map, regs, num_regs);
2358 * regmap_multi_reg_write() - Write multiple registers to the device
2360 * @map: Register map to write to
2361 * @regs: Array of structures containing register,value to be written
2362 * @num_regs: Number of registers to write
2364 * Write multiple registers to the device where the set of register, value
2365 * pairs are supplied in any order, possibly not all in a single range.
2367 * The 'normal' block write mode will send ultimately send data on the
2368 * target bus as R,V1,V2,V3,..,Vn where successively higher registers are
2369 * addressed. However, this alternative block multi write mode will send
2370 * the data as R1,V1,R2,V2,..,Rn,Vn on the target bus. The target device
2371 * must of course support the mode.
2373 * A value of zero will be returned on success, a negative errno will be
2374 * returned in error cases.
2376 int regmap_multi_reg_write(struct regmap *map, const struct reg_sequence *regs,
2377 int num_regs)
2379 int ret;
2381 map->lock(map->lock_arg);
2383 ret = _regmap_multi_reg_write(map, regs, num_regs);
2385 map->unlock(map->lock_arg);
2387 return ret;
2389 EXPORT_SYMBOL_GPL(regmap_multi_reg_write);
2392 * regmap_multi_reg_write_bypassed() - Write multiple registers to the
2393 * device but not the cache
2395 * @map: Register map to write to
2396 * @regs: Array of structures containing register,value to be written
2397 * @num_regs: Number of registers to write
2399 * Write multiple registers to the device but not the cache where the set
2400 * of register are supplied in any order.
2402 * This function is intended to be used for writing a large block of data
2403 * atomically to the device in single transfer for those I2C client devices
2404 * that implement this alternative block write mode.
2406 * A value of zero will be returned on success, a negative errno will
2407 * be returned in error cases.
2409 int regmap_multi_reg_write_bypassed(struct regmap *map,
2410 const struct reg_sequence *regs,
2411 int num_regs)
2413 int ret;
2414 bool bypass;
2416 map->lock(map->lock_arg);
2418 bypass = map->cache_bypass;
2419 map->cache_bypass = true;
2421 ret = _regmap_multi_reg_write(map, regs, num_regs);
2423 map->cache_bypass = bypass;
2425 map->unlock(map->lock_arg);
2427 return ret;
2429 EXPORT_SYMBOL_GPL(regmap_multi_reg_write_bypassed);
2432 * regmap_raw_write_async() - Write raw values to one or more registers
2433 * asynchronously
2435 * @map: Register map to write to
2436 * @reg: Initial register to write to
2437 * @val: Block of data to be written, laid out for direct transmission to the
2438 * device. Must be valid until regmap_async_complete() is called.
2439 * @val_len: Length of data pointed to by val.
2441 * This function is intended to be used for things like firmware
2442 * download where a large block of data needs to be transferred to the
2443 * device. No formatting will be done on the data provided.
2445 * If supported by the underlying bus the write will be scheduled
2446 * asynchronously, helping maximise I/O speed on higher speed buses
2447 * like SPI. regmap_async_complete() can be called to ensure that all
2448 * asynchrnous writes have been completed.
2450 * A value of zero will be returned on success, a negative errno will
2451 * be returned in error cases.
2453 int regmap_raw_write_async(struct regmap *map, unsigned int reg,
2454 const void *val, size_t val_len)
2456 int ret;
2458 if (val_len % map->format.val_bytes)
2459 return -EINVAL;
2460 if (!IS_ALIGNED(reg, map->reg_stride))
2461 return -EINVAL;
2463 map->lock(map->lock_arg);
2465 map->async = true;
2467 ret = _regmap_raw_write(map, reg, val, val_len, false);
2469 map->async = false;
2471 map->unlock(map->lock_arg);
2473 return ret;
2475 EXPORT_SYMBOL_GPL(regmap_raw_write_async);
2477 static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
2478 unsigned int val_len, bool noinc)
2480 struct regmap_range_node *range;
2481 int ret;
2483 WARN_ON(!map->bus);
2485 if (!map->bus || !map->bus->read)
2486 return -EINVAL;
2488 range = _regmap_range_lookup(map, reg);
2489 if (range) {
2490 ret = _regmap_select_page(map, &reg, range,
2491 noinc ? 1 : val_len / map->format.val_bytes);
2492 if (ret != 0)
2493 return ret;
2496 map->format.format_reg(map->work_buf, reg, map->reg_shift);
2497 regmap_set_work_buf_flag_mask(map, map->format.reg_bytes,
2498 map->read_flag_mask);
2499 trace_regmap_hw_read_start(map, reg, val_len / map->format.val_bytes);
2501 ret = map->bus->read(map->bus_context, map->work_buf,
2502 map->format.reg_bytes + map->format.pad_bytes,
2503 val, val_len);
2505 trace_regmap_hw_read_done(map, reg, val_len / map->format.val_bytes);
2507 return ret;
2510 static int _regmap_bus_reg_read(void *context, unsigned int reg,
2511 unsigned int *val)
2513 struct regmap *map = context;
2515 return map->bus->reg_read(map->bus_context, reg, val);
2518 static int _regmap_bus_read(void *context, unsigned int reg,
2519 unsigned int *val)
2521 int ret;
2522 struct regmap *map = context;
2523 void *work_val = map->work_buf + map->format.reg_bytes +
2524 map->format.pad_bytes;
2526 if (!map->format.parse_val)
2527 return -EINVAL;
2529 ret = _regmap_raw_read(map, reg, work_val, map->format.val_bytes, false);
2530 if (ret == 0)
2531 *val = map->format.parse_val(work_val);
2533 return ret;
2536 static int _regmap_read(struct regmap *map, unsigned int reg,
2537 unsigned int *val)
2539 int ret;
2540 void *context = _regmap_map_get_context(map);
2542 if (!map->cache_bypass) {
2543 ret = regcache_read(map, reg, val);
2544 if (ret == 0)
2545 return 0;
2548 if (map->cache_only)
2549 return -EBUSY;
2551 if (!regmap_readable(map, reg))
2552 return -EIO;
2554 ret = map->reg_read(context, reg, val);
2555 if (ret == 0) {
2556 if (regmap_should_log(map))
2557 dev_info(map->dev, "%x => %x\n", reg, *val);
2559 trace_regmap_reg_read(map, reg, *val);
2561 if (!map->cache_bypass)
2562 regcache_write(map, reg, *val);
2565 return ret;
2569 * regmap_read() - Read a value from a single register
2571 * @map: Register map to read from
2572 * @reg: Register to be read from
2573 * @val: Pointer to store read value
2575 * A value of zero will be returned on success, a negative errno will
2576 * be returned in error cases.
2578 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
2580 int ret;
2582 if (!IS_ALIGNED(reg, map->reg_stride))
2583 return -EINVAL;
2585 map->lock(map->lock_arg);
2587 ret = _regmap_read(map, reg, val);
2589 map->unlock(map->lock_arg);
2591 return ret;
2593 EXPORT_SYMBOL_GPL(regmap_read);
2596 * regmap_raw_read() - Read raw data from the device
2598 * @map: Register map to read from
2599 * @reg: First register to be read from
2600 * @val: Pointer to store read value
2601 * @val_len: Size of data to read
2603 * A value of zero will be returned on success, a negative errno will
2604 * be returned in error cases.
2606 int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
2607 size_t val_len)
2609 size_t val_bytes = map->format.val_bytes;
2610 size_t val_count = val_len / val_bytes;
2611 unsigned int v;
2612 int ret, i;
2614 if (!map->bus)
2615 return -EINVAL;
2616 if (val_len % map->format.val_bytes)
2617 return -EINVAL;
2618 if (!IS_ALIGNED(reg, map->reg_stride))
2619 return -EINVAL;
2620 if (val_count == 0)
2621 return -EINVAL;
2623 map->lock(map->lock_arg);
2625 if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
2626 map->cache_type == REGCACHE_NONE) {
2627 size_t chunk_count, chunk_bytes;
2628 size_t chunk_regs = val_count;
2630 if (!map->bus->read) {
2631 ret = -ENOTSUPP;
2632 goto out;
2635 if (map->use_single_read)
2636 chunk_regs = 1;
2637 else if (map->max_raw_read && val_len > map->max_raw_read)
2638 chunk_regs = map->max_raw_read / val_bytes;
2640 chunk_count = val_count / chunk_regs;
2641 chunk_bytes = chunk_regs * val_bytes;
2643 /* Read bytes that fit into whole chunks */
2644 for (i = 0; i < chunk_count; i++) {
2645 ret = _regmap_raw_read(map, reg, val, chunk_bytes, false);
2646 if (ret != 0)
2647 goto out;
2649 reg += regmap_get_offset(map, chunk_regs);
2650 val += chunk_bytes;
2651 val_len -= chunk_bytes;
2654 /* Read remaining bytes */
2655 if (val_len) {
2656 ret = _regmap_raw_read(map, reg, val, val_len, false);
2657 if (ret != 0)
2658 goto out;
2660 } else {
2661 /* Otherwise go word by word for the cache; should be low
2662 * cost as we expect to hit the cache.
2664 for (i = 0; i < val_count; i++) {
2665 ret = _regmap_read(map, reg + regmap_get_offset(map, i),
2666 &v);
2667 if (ret != 0)
2668 goto out;
2670 map->format.format_val(val + (i * val_bytes), v, 0);
2674 out:
2675 map->unlock(map->lock_arg);
2677 return ret;
2679 EXPORT_SYMBOL_GPL(regmap_raw_read);
2682 * regmap_noinc_read(): Read data from a register without incrementing the
2683 * register number
2685 * @map: Register map to read from
2686 * @reg: Register to read from
2687 * @val: Pointer to data buffer
2688 * @val_len: Length of output buffer in bytes.
2690 * The regmap API usually assumes that bulk bus read operations will read a
2691 * range of registers. Some devices have certain registers for which a read
2692 * operation read will read from an internal FIFO.
2694 * The target register must be volatile but registers after it can be
2695 * completely unrelated cacheable registers.
2697 * This will attempt multiple reads as required to read val_len bytes.
2699 * A value of zero will be returned on success, a negative errno will be
2700 * returned in error cases.
2702 int regmap_noinc_read(struct regmap *map, unsigned int reg,
2703 void *val, size_t val_len)
2705 size_t read_len;
2706 int ret;
2708 if (!map->bus)
2709 return -EINVAL;
2710 if (!map->bus->read)
2711 return -ENOTSUPP;
2712 if (val_len % map->format.val_bytes)
2713 return -EINVAL;
2714 if (!IS_ALIGNED(reg, map->reg_stride))
2715 return -EINVAL;
2716 if (val_len == 0)
2717 return -EINVAL;
2719 map->lock(map->lock_arg);
2721 if (!regmap_volatile(map, reg) || !regmap_readable_noinc(map, reg)) {
2722 ret = -EINVAL;
2723 goto out_unlock;
2726 while (val_len) {
2727 if (map->max_raw_read && map->max_raw_read < val_len)
2728 read_len = map->max_raw_read;
2729 else
2730 read_len = val_len;
2731 ret = _regmap_raw_read(map, reg, val, read_len, true);
2732 if (ret)
2733 goto out_unlock;
2734 val = ((u8 *)val) + read_len;
2735 val_len -= read_len;
2738 out_unlock:
2739 map->unlock(map->lock_arg);
2740 return ret;
2742 EXPORT_SYMBOL_GPL(regmap_noinc_read);
2745 * regmap_field_read(): Read a value to a single register field
2747 * @field: Register field to read from
2748 * @val: Pointer to store read value
2750 * A value of zero will be returned on success, a negative errno will
2751 * be returned in error cases.
2753 int regmap_field_read(struct regmap_field *field, unsigned int *val)
2755 int ret;
2756 unsigned int reg_val;
2757 ret = regmap_read(field->regmap, field->reg, &reg_val);
2758 if (ret != 0)
2759 return ret;
2761 reg_val &= field->mask;
2762 reg_val >>= field->shift;
2763 *val = reg_val;
2765 return ret;
2767 EXPORT_SYMBOL_GPL(regmap_field_read);
2770 * regmap_fields_read() - Read a value to a single register field with port ID
2772 * @field: Register field to read from
2773 * @id: port ID
2774 * @val: Pointer to store read value
2776 * A value of zero will be returned on success, a negative errno will
2777 * be returned in error cases.
2779 int regmap_fields_read(struct regmap_field *field, unsigned int id,
2780 unsigned int *val)
2782 int ret;
2783 unsigned int reg_val;
2785 if (id >= field->id_size)
2786 return -EINVAL;
2788 ret = regmap_read(field->regmap,
2789 field->reg + (field->id_offset * id),
2790 &reg_val);
2791 if (ret != 0)
2792 return ret;
2794 reg_val &= field->mask;
2795 reg_val >>= field->shift;
2796 *val = reg_val;
2798 return ret;
2800 EXPORT_SYMBOL_GPL(regmap_fields_read);
2803 * regmap_bulk_read() - Read multiple registers from the device
2805 * @map: Register map to read from
2806 * @reg: First register to be read from
2807 * @val: Pointer to store read value, in native register size for device
2808 * @val_count: Number of registers to read
2810 * A value of zero will be returned on success, a negative errno will
2811 * be returned in error cases.
2813 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
2814 size_t val_count)
2816 int ret, i;
2817 size_t val_bytes = map->format.val_bytes;
2818 bool vol = regmap_volatile_range(map, reg, val_count);
2820 if (!IS_ALIGNED(reg, map->reg_stride))
2821 return -EINVAL;
2822 if (val_count == 0)
2823 return -EINVAL;
2825 if (map->bus && map->format.parse_inplace && (vol || map->cache_type == REGCACHE_NONE)) {
2826 ret = regmap_raw_read(map, reg, val, val_bytes * val_count);
2827 if (ret != 0)
2828 return ret;
2830 for (i = 0; i < val_count * val_bytes; i += val_bytes)
2831 map->format.parse_inplace(val + i);
2832 } else {
2833 #ifdef CONFIG_64BIT
2834 u64 *u64 = val;
2835 #endif
2836 u32 *u32 = val;
2837 u16 *u16 = val;
2838 u8 *u8 = val;
2840 map->lock(map->lock_arg);
2842 for (i = 0; i < val_count; i++) {
2843 unsigned int ival;
2845 ret = _regmap_read(map, reg + regmap_get_offset(map, i),
2846 &ival);
2847 if (ret != 0)
2848 goto out;
2850 switch (map->format.val_bytes) {
2851 #ifdef CONFIG_64BIT
2852 case 8:
2853 u64[i] = ival;
2854 break;
2855 #endif
2856 case 4:
2857 u32[i] = ival;
2858 break;
2859 case 2:
2860 u16[i] = ival;
2861 break;
2862 case 1:
2863 u8[i] = ival;
2864 break;
2865 default:
2866 ret = -EINVAL;
2867 goto out;
2871 out:
2872 map->unlock(map->lock_arg);
2875 return ret;
2877 EXPORT_SYMBOL_GPL(regmap_bulk_read);
2879 static int _regmap_update_bits(struct regmap *map, unsigned int reg,
2880 unsigned int mask, unsigned int val,
2881 bool *change, bool force_write)
2883 int ret;
2884 unsigned int tmp, orig;
2886 if (change)
2887 *change = false;
2889 if (regmap_volatile(map, reg) && map->reg_update_bits) {
2890 ret = map->reg_update_bits(map->bus_context, reg, mask, val);
2891 if (ret == 0 && change)
2892 *change = true;
2893 } else {
2894 ret = _regmap_read(map, reg, &orig);
2895 if (ret != 0)
2896 return ret;
2898 tmp = orig & ~mask;
2899 tmp |= val & mask;
2901 if (force_write || (tmp != orig)) {
2902 ret = _regmap_write(map, reg, tmp);
2903 if (ret == 0 && change)
2904 *change = true;
2908 return ret;
2912 * regmap_update_bits_base() - Perform a read/modify/write cycle on a register
2914 * @map: Register map to update
2915 * @reg: Register to update
2916 * @mask: Bitmask to change
2917 * @val: New value for bitmask
2918 * @change: Boolean indicating if a write was done
2919 * @async: Boolean indicating asynchronously
2920 * @force: Boolean indicating use force update
2922 * Perform a read/modify/write cycle on a register map with change, async, force
2923 * options.
2925 * If async is true:
2927 * With most buses the read must be done synchronously so this is most useful
2928 * for devices with a cache which do not need to interact with the hardware to
2929 * determine the current register value.
2931 * Returns zero for success, a negative number on error.
2933 int regmap_update_bits_base(struct regmap *map, unsigned int reg,
2934 unsigned int mask, unsigned int val,
2935 bool *change, bool async, bool force)
2937 int ret;
2939 map->lock(map->lock_arg);
2941 map->async = async;
2943 ret = _regmap_update_bits(map, reg, mask, val, change, force);
2945 map->async = false;
2947 map->unlock(map->lock_arg);
2949 return ret;
2951 EXPORT_SYMBOL_GPL(regmap_update_bits_base);
2954 * regmap_test_bits() - Check if all specified bits are set in a register.
2956 * @map: Register map to operate on
2957 * @reg: Register to read from
2958 * @bits: Bits to test
2960 * Returns 0 if at least one of the tested bits is not set, 1 if all tested
2961 * bits are set and a negative error number if the underlying regmap_read()
2962 * fails.
2964 int regmap_test_bits(struct regmap *map, unsigned int reg, unsigned int bits)
2966 unsigned int val, ret;
2968 ret = regmap_read(map, reg, &val);
2969 if (ret)
2970 return ret;
2972 return (val & bits) == bits;
2974 EXPORT_SYMBOL_GPL(regmap_test_bits);
2976 void regmap_async_complete_cb(struct regmap_async *async, int ret)
2978 struct regmap *map = async->map;
2979 bool wake;
2981 trace_regmap_async_io_complete(map);
2983 spin_lock(&map->async_lock);
2984 list_move(&async->list, &map->async_free);
2985 wake = list_empty(&map->async_list);
2987 if (ret != 0)
2988 map->async_ret = ret;
2990 spin_unlock(&map->async_lock);
2992 if (wake)
2993 wake_up(&map->async_waitq);
2995 EXPORT_SYMBOL_GPL(regmap_async_complete_cb);
2997 static int regmap_async_is_done(struct regmap *map)
2999 unsigned long flags;
3000 int ret;
3002 spin_lock_irqsave(&map->async_lock, flags);
3003 ret = list_empty(&map->async_list);
3004 spin_unlock_irqrestore(&map->async_lock, flags);
3006 return ret;
3010 * regmap_async_complete - Ensure all asynchronous I/O has completed.
3012 * @map: Map to operate on.
3014 * Blocks until any pending asynchronous I/O has completed. Returns
3015 * an error code for any failed I/O operations.
3017 int regmap_async_complete(struct regmap *map)
3019 unsigned long flags;
3020 int ret;
3022 /* Nothing to do with no async support */
3023 if (!map->bus || !map->bus->async_write)
3024 return 0;
3026 trace_regmap_async_complete_start(map);
3028 wait_event(map->async_waitq, regmap_async_is_done(map));
3030 spin_lock_irqsave(&map->async_lock, flags);
3031 ret = map->async_ret;
3032 map->async_ret = 0;
3033 spin_unlock_irqrestore(&map->async_lock, flags);
3035 trace_regmap_async_complete_done(map);
3037 return ret;
3039 EXPORT_SYMBOL_GPL(regmap_async_complete);
3042 * regmap_register_patch - Register and apply register updates to be applied
3043 * on device initialistion
3045 * @map: Register map to apply updates to.
3046 * @regs: Values to update.
3047 * @num_regs: Number of entries in regs.
3049 * Register a set of register updates to be applied to the device
3050 * whenever the device registers are synchronised with the cache and
3051 * apply them immediately. Typically this is used to apply
3052 * corrections to be applied to the device defaults on startup, such
3053 * as the updates some vendors provide to undocumented registers.
3055 * The caller must ensure that this function cannot be called
3056 * concurrently with either itself or regcache_sync().
3058 int regmap_register_patch(struct regmap *map, const struct reg_sequence *regs,
3059 int num_regs)
3061 struct reg_sequence *p;
3062 int ret;
3063 bool bypass;
3065 if (WARN_ONCE(num_regs <= 0, "invalid registers number (%d)\n",
3066 num_regs))
3067 return 0;
3069 p = krealloc(map->patch,
3070 sizeof(struct reg_sequence) * (map->patch_regs + num_regs),
3071 GFP_KERNEL);
3072 if (p) {
3073 memcpy(p + map->patch_regs, regs, num_regs * sizeof(*regs));
3074 map->patch = p;
3075 map->patch_regs += num_regs;
3076 } else {
3077 return -ENOMEM;
3080 map->lock(map->lock_arg);
3082 bypass = map->cache_bypass;
3084 map->cache_bypass = true;
3085 map->async = true;
3087 ret = _regmap_multi_reg_write(map, regs, num_regs);
3089 map->async = false;
3090 map->cache_bypass = bypass;
3092 map->unlock(map->lock_arg);
3094 regmap_async_complete(map);
3096 return ret;
3098 EXPORT_SYMBOL_GPL(regmap_register_patch);
3101 * regmap_get_val_bytes() - Report the size of a register value
3103 * @map: Register map to operate on.
3105 * Report the size of a register value, mainly intended to for use by
3106 * generic infrastructure built on top of regmap.
3108 int regmap_get_val_bytes(struct regmap *map)
3110 if (map->format.format_write)
3111 return -EINVAL;
3113 return map->format.val_bytes;
3115 EXPORT_SYMBOL_GPL(regmap_get_val_bytes);
3118 * regmap_get_max_register() - Report the max register value
3120 * @map: Register map to operate on.
3122 * Report the max register value, mainly intended to for use by
3123 * generic infrastructure built on top of regmap.
3125 int regmap_get_max_register(struct regmap *map)
3127 return map->max_register ? map->max_register : -EINVAL;
3129 EXPORT_SYMBOL_GPL(regmap_get_max_register);
3132 * regmap_get_reg_stride() - Report the register address stride
3134 * @map: Register map to operate on.
3136 * Report the register address stride, mainly intended to for use by
3137 * generic infrastructure built on top of regmap.
3139 int regmap_get_reg_stride(struct regmap *map)
3141 return map->reg_stride;
3143 EXPORT_SYMBOL_GPL(regmap_get_reg_stride);
3145 int regmap_parse_val(struct regmap *map, const void *buf,
3146 unsigned int *val)
3148 if (!map->format.parse_val)
3149 return -EINVAL;
3151 *val = map->format.parse_val(buf);
3153 return 0;
3155 EXPORT_SYMBOL_GPL(regmap_parse_val);
3157 static int __init regmap_initcall(void)
3159 regmap_debugfs_initcall();
3161 return 0;
3163 postcore_initcall(regmap_initcall);