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>
18 #include <linux/rbtree.h>
19 #include <linux/sched.h>
21 #define CREATE_TRACE_POINTS
22 #include <trace/events/regmap.h>
27 * Sometimes for failures during very early init the trace
28 * infrastructure isn't available early enough to be used. For this
29 * sort of problem defining LOG_DEVICE will add printks for basic
30 * register I/O on a specific device.
34 static int _regmap_update_bits(struct regmap *map, unsigned int reg,
35 unsigned int mask, unsigned int val,
38 static int _regmap_bus_read(void *context, unsigned int reg,
40 static int _regmap_bus_formatted_write(void *context, unsigned int reg,
42 static int _regmap_bus_raw_write(void *context, unsigned int reg,
45 static void async_cleanup(struct work_struct *work)
47 struct regmap_async *async = container_of(work, struct regmap_async,
50 kfree(async->work_buf);
54 bool regmap_reg_in_ranges(unsigned int reg,
55 const struct regmap_range *ranges,
58 const struct regmap_range *r;
61 for (i = 0, r = ranges; i < nranges; i++, r++)
62 if (regmap_reg_in_range(reg, r))
66 EXPORT_SYMBOL_GPL(regmap_reg_in_ranges);
68 static bool _regmap_check_range_table(struct regmap *map,
70 const struct regmap_access_table *table)
72 /* Check "no ranges" first */
73 if (regmap_reg_in_ranges(reg, table->no_ranges, table->n_no_ranges))
76 /* In case zero "yes ranges" are supplied, any reg is OK */
77 if (!table->n_yes_ranges)
80 return regmap_reg_in_ranges(reg, table->yes_ranges,
84 bool regmap_writeable(struct regmap *map, unsigned int reg)
86 if (map->max_register && reg > map->max_register)
89 if (map->writeable_reg)
90 return map->writeable_reg(map->dev, reg);
93 return _regmap_check_range_table(map, reg, map->wr_table);
98 bool regmap_readable(struct regmap *map, unsigned int reg)
100 if (map->max_register && reg > map->max_register)
103 if (map->format.format_write)
106 if (map->readable_reg)
107 return map->readable_reg(map->dev, reg);
110 return _regmap_check_range_table(map, reg, map->rd_table);
115 bool regmap_volatile(struct regmap *map, unsigned int reg)
117 if (!regmap_readable(map, reg))
120 if (map->volatile_reg)
121 return map->volatile_reg(map->dev, reg);
123 if (map->volatile_table)
124 return _regmap_check_range_table(map, reg, map->volatile_table);
129 bool regmap_precious(struct regmap *map, unsigned int reg)
131 if (!regmap_readable(map, reg))
134 if (map->precious_reg)
135 return map->precious_reg(map->dev, reg);
137 if (map->precious_table)
138 return _regmap_check_range_table(map, reg, map->precious_table);
143 static bool regmap_volatile_range(struct regmap *map, unsigned int reg,
148 for (i = 0; i < num; i++)
149 if (!regmap_volatile(map, reg + i))
155 static void regmap_format_2_6_write(struct regmap *map,
156 unsigned int reg, unsigned int val)
158 u8 *out = map->work_buf;
160 *out = (reg << 6) | val;
163 static void regmap_format_4_12_write(struct regmap *map,
164 unsigned int reg, unsigned int val)
166 __be16 *out = map->work_buf;
167 *out = cpu_to_be16((reg << 12) | val);
170 static void regmap_format_7_9_write(struct regmap *map,
171 unsigned int reg, unsigned int val)
173 __be16 *out = map->work_buf;
174 *out = cpu_to_be16((reg << 9) | val);
177 static void regmap_format_10_14_write(struct regmap *map,
178 unsigned int reg, unsigned int val)
180 u8 *out = map->work_buf;
183 out[1] = (val >> 8) | (reg << 6);
187 static void regmap_format_8(void *buf, unsigned int val, unsigned int shift)
194 static void regmap_format_16_be(void *buf, unsigned int val, unsigned int shift)
198 b[0] = cpu_to_be16(val << shift);
201 static void regmap_format_16_native(void *buf, unsigned int val,
204 *(u16 *)buf = val << shift;
207 static void regmap_format_24(void *buf, unsigned int val, unsigned int shift)
218 static void regmap_format_32_be(void *buf, unsigned int val, unsigned int shift)
222 b[0] = cpu_to_be32(val << shift);
225 static void regmap_format_32_native(void *buf, unsigned int val,
228 *(u32 *)buf = val << shift;
231 static unsigned int regmap_parse_8(void *buf)
238 static unsigned int regmap_parse_16_be(void *buf)
242 b[0] = be16_to_cpu(b[0]);
247 static unsigned int regmap_parse_16_native(void *buf)
252 static unsigned int regmap_parse_24(void *buf)
255 unsigned int ret = b[2];
256 ret |= ((unsigned int)b[1]) << 8;
257 ret |= ((unsigned int)b[0]) << 16;
262 static unsigned int regmap_parse_32_be(void *buf)
266 b[0] = be32_to_cpu(b[0]);
271 static unsigned int regmap_parse_32_native(void *buf)
276 static void regmap_lock_mutex(void *__map)
278 struct regmap *map = __map;
279 mutex_lock(&map->mutex);
282 static void regmap_unlock_mutex(void *__map)
284 struct regmap *map = __map;
285 mutex_unlock(&map->mutex);
288 static void regmap_lock_spinlock(void *__map)
290 struct regmap *map = __map;
291 spin_lock(&map->spinlock);
294 static void regmap_unlock_spinlock(void *__map)
296 struct regmap *map = __map;
297 spin_unlock(&map->spinlock);
300 static void dev_get_regmap_release(struct device *dev, void *res)
303 * We don't actually have anything to do here; the goal here
304 * is not to manage the regmap but to provide a simple way to
305 * get the regmap back given a struct device.
309 static bool _regmap_range_add(struct regmap *map,
310 struct regmap_range_node *data)
312 struct rb_root *root = &map->range_tree;
313 struct rb_node **new = &(root->rb_node), *parent = NULL;
316 struct regmap_range_node *this =
317 container_of(*new, struct regmap_range_node, node);
320 if (data->range_max < this->range_min)
321 new = &((*new)->rb_left);
322 else if (data->range_min > this->range_max)
323 new = &((*new)->rb_right);
328 rb_link_node(&data->node, parent, new);
329 rb_insert_color(&data->node, root);
334 static struct regmap_range_node *_regmap_range_lookup(struct regmap *map,
337 struct rb_node *node = map->range_tree.rb_node;
340 struct regmap_range_node *this =
341 container_of(node, struct regmap_range_node, node);
343 if (reg < this->range_min)
344 node = node->rb_left;
345 else if (reg > this->range_max)
346 node = node->rb_right;
354 static void regmap_range_exit(struct regmap *map)
356 struct rb_node *next;
357 struct regmap_range_node *range_node;
359 next = rb_first(&map->range_tree);
361 range_node = rb_entry(next, struct regmap_range_node, node);
362 next = rb_next(&range_node->node);
363 rb_erase(&range_node->node, &map->range_tree);
367 kfree(map->selector_work_buf);
371 * regmap_init(): Initialise register map
373 * @dev: Device that will be interacted with
374 * @bus: Bus-specific callbacks to use with device
375 * @bus_context: Data passed to bus-specific callbacks
376 * @config: Configuration for register map
378 * The return value will be an ERR_PTR() on error or a valid pointer to
379 * a struct regmap. This function should generally not be called
380 * directly, it should be called by bus-specific init functions.
382 struct regmap *regmap_init(struct device *dev,
383 const struct regmap_bus *bus,
385 const struct regmap_config *config)
387 struct regmap *map, **m;
389 enum regmap_endian reg_endian, val_endian;
395 map = kzalloc(sizeof(*map), GFP_KERNEL);
401 if (config->lock && config->unlock) {
402 map->lock = config->lock;
403 map->unlock = config->unlock;
404 map->lock_arg = config->lock_arg;
406 if ((bus && bus->fast_io) ||
408 spin_lock_init(&map->spinlock);
409 map->lock = regmap_lock_spinlock;
410 map->unlock = regmap_unlock_spinlock;
412 mutex_init(&map->mutex);
413 map->lock = regmap_lock_mutex;
414 map->unlock = regmap_unlock_mutex;
418 map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8);
419 map->format.pad_bytes = config->pad_bits / 8;
420 map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8);
421 map->format.buf_size = DIV_ROUND_UP(config->reg_bits +
422 config->val_bits + config->pad_bits, 8);
423 map->reg_shift = config->pad_bits % 8;
424 if (config->reg_stride)
425 map->reg_stride = config->reg_stride;
428 map->use_single_rw = config->use_single_rw;
431 map->bus_context = bus_context;
432 map->max_register = config->max_register;
433 map->wr_table = config->wr_table;
434 map->rd_table = config->rd_table;
435 map->volatile_table = config->volatile_table;
436 map->precious_table = config->precious_table;
437 map->writeable_reg = config->writeable_reg;
438 map->readable_reg = config->readable_reg;
439 map->volatile_reg = config->volatile_reg;
440 map->precious_reg = config->precious_reg;
441 map->cache_type = config->cache_type;
442 map->name = config->name;
444 spin_lock_init(&map->async_lock);
445 INIT_LIST_HEAD(&map->async_list);
446 init_waitqueue_head(&map->async_waitq);
448 if (config->read_flag_mask || config->write_flag_mask) {
449 map->read_flag_mask = config->read_flag_mask;
450 map->write_flag_mask = config->write_flag_mask;
452 map->read_flag_mask = bus->read_flag_mask;
456 map->reg_read = config->reg_read;
457 map->reg_write = config->reg_write;
459 map->defer_caching = false;
460 goto skip_format_initialization;
462 map->reg_read = _regmap_bus_read;
465 reg_endian = config->reg_format_endian;
466 if (reg_endian == REGMAP_ENDIAN_DEFAULT)
467 reg_endian = bus->reg_format_endian_default;
468 if (reg_endian == REGMAP_ENDIAN_DEFAULT)
469 reg_endian = REGMAP_ENDIAN_BIG;
471 val_endian = config->val_format_endian;
472 if (val_endian == REGMAP_ENDIAN_DEFAULT)
473 val_endian = bus->val_format_endian_default;
474 if (val_endian == REGMAP_ENDIAN_DEFAULT)
475 val_endian = REGMAP_ENDIAN_BIG;
477 switch (config->reg_bits + map->reg_shift) {
479 switch (config->val_bits) {
481 map->format.format_write = regmap_format_2_6_write;
489 switch (config->val_bits) {
491 map->format.format_write = regmap_format_4_12_write;
499 switch (config->val_bits) {
501 map->format.format_write = regmap_format_7_9_write;
509 switch (config->val_bits) {
511 map->format.format_write = regmap_format_10_14_write;
519 map->format.format_reg = regmap_format_8;
523 switch (reg_endian) {
524 case REGMAP_ENDIAN_BIG:
525 map->format.format_reg = regmap_format_16_be;
527 case REGMAP_ENDIAN_NATIVE:
528 map->format.format_reg = regmap_format_16_native;
536 if (reg_endian != REGMAP_ENDIAN_BIG)
538 map->format.format_reg = regmap_format_24;
542 switch (reg_endian) {
543 case REGMAP_ENDIAN_BIG:
544 map->format.format_reg = regmap_format_32_be;
546 case REGMAP_ENDIAN_NATIVE:
547 map->format.format_reg = regmap_format_32_native;
558 switch (config->val_bits) {
560 map->format.format_val = regmap_format_8;
561 map->format.parse_val = regmap_parse_8;
564 switch (val_endian) {
565 case REGMAP_ENDIAN_BIG:
566 map->format.format_val = regmap_format_16_be;
567 map->format.parse_val = regmap_parse_16_be;
569 case REGMAP_ENDIAN_NATIVE:
570 map->format.format_val = regmap_format_16_native;
571 map->format.parse_val = regmap_parse_16_native;
578 if (val_endian != REGMAP_ENDIAN_BIG)
580 map->format.format_val = regmap_format_24;
581 map->format.parse_val = regmap_parse_24;
584 switch (val_endian) {
585 case REGMAP_ENDIAN_BIG:
586 map->format.format_val = regmap_format_32_be;
587 map->format.parse_val = regmap_parse_32_be;
589 case REGMAP_ENDIAN_NATIVE:
590 map->format.format_val = regmap_format_32_native;
591 map->format.parse_val = regmap_parse_32_native;
599 if (map->format.format_write) {
600 if ((reg_endian != REGMAP_ENDIAN_BIG) ||
601 (val_endian != REGMAP_ENDIAN_BIG))
603 map->use_single_rw = true;
606 if (!map->format.format_write &&
607 !(map->format.format_reg && map->format.format_val))
610 map->work_buf = kzalloc(map->format.buf_size, GFP_KERNEL);
611 if (map->work_buf == NULL) {
616 if (map->format.format_write) {
617 map->defer_caching = false;
618 map->reg_write = _regmap_bus_formatted_write;
619 } else if (map->format.format_val) {
620 map->defer_caching = true;
621 map->reg_write = _regmap_bus_raw_write;
624 skip_format_initialization:
626 map->range_tree = RB_ROOT;
627 for (i = 0; i < config->num_ranges; i++) {
628 const struct regmap_range_cfg *range_cfg = &config->ranges[i];
629 struct regmap_range_node *new;
632 if (range_cfg->range_max < range_cfg->range_min) {
633 dev_err(map->dev, "Invalid range %d: %d < %d\n", i,
634 range_cfg->range_max, range_cfg->range_min);
638 if (range_cfg->range_max > map->max_register) {
639 dev_err(map->dev, "Invalid range %d: %d > %d\n", i,
640 range_cfg->range_max, map->max_register);
644 if (range_cfg->selector_reg > map->max_register) {
646 "Invalid range %d: selector out of map\n", i);
650 if (range_cfg->window_len == 0) {
651 dev_err(map->dev, "Invalid range %d: window_len 0\n",
656 /* Make sure, that this register range has no selector
657 or data window within its boundary */
658 for (j = 0; j < config->num_ranges; j++) {
659 unsigned sel_reg = config->ranges[j].selector_reg;
660 unsigned win_min = config->ranges[j].window_start;
661 unsigned win_max = win_min +
662 config->ranges[j].window_len - 1;
664 if (range_cfg->range_min <= sel_reg &&
665 sel_reg <= range_cfg->range_max) {
667 "Range %d: selector for %d in window\n",
672 if (!(win_max < range_cfg->range_min ||
673 win_min > range_cfg->range_max)) {
675 "Range %d: window for %d in window\n",
681 new = kzalloc(sizeof(*new), GFP_KERNEL);
688 new->name = range_cfg->name;
689 new->range_min = range_cfg->range_min;
690 new->range_max = range_cfg->range_max;
691 new->selector_reg = range_cfg->selector_reg;
692 new->selector_mask = range_cfg->selector_mask;
693 new->selector_shift = range_cfg->selector_shift;
694 new->window_start = range_cfg->window_start;
695 new->window_len = range_cfg->window_len;
697 if (_regmap_range_add(map, new) == false) {
698 dev_err(map->dev, "Failed to add range %d\n", i);
703 if (map->selector_work_buf == NULL) {
704 map->selector_work_buf =
705 kzalloc(map->format.buf_size, GFP_KERNEL);
706 if (map->selector_work_buf == NULL) {
713 regmap_debugfs_init(map, config->name);
715 ret = regcache_init(map, config);
719 /* Add a devres resource for dev_get_regmap() */
720 m = devres_alloc(dev_get_regmap_release, sizeof(*m), GFP_KERNEL);
731 regmap_debugfs_exit(map);
734 regmap_range_exit(map);
735 kfree(map->work_buf);
741 EXPORT_SYMBOL_GPL(regmap_init);
743 static void devm_regmap_release(struct device *dev, void *res)
745 regmap_exit(*(struct regmap **)res);
749 * devm_regmap_init(): Initialise managed register map
751 * @dev: Device that will be interacted with
752 * @bus: Bus-specific callbacks to use with device
753 * @bus_context: Data passed to bus-specific callbacks
754 * @config: Configuration for register map
756 * The return value will be an ERR_PTR() on error or a valid pointer
757 * to a struct regmap. This function should generally not be called
758 * directly, it should be called by bus-specific init functions. The
759 * map will be automatically freed by the device management code.
761 struct regmap *devm_regmap_init(struct device *dev,
762 const struct regmap_bus *bus,
764 const struct regmap_config *config)
766 struct regmap **ptr, *regmap;
768 ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL);
770 return ERR_PTR(-ENOMEM);
772 regmap = regmap_init(dev, bus, bus_context, config);
773 if (!IS_ERR(regmap)) {
775 devres_add(dev, ptr);
782 EXPORT_SYMBOL_GPL(devm_regmap_init);
785 * regmap_reinit_cache(): Reinitialise the current register cache
787 * @map: Register map to operate on.
788 * @config: New configuration. Only the cache data will be used.
790 * Discard any existing register cache for the map and initialize a
791 * new cache. This can be used to restore the cache to defaults or to
792 * update the cache configuration to reflect runtime discovery of the
795 * No explicit locking is done here, the user needs to ensure that
796 * this function will not race with other calls to regmap.
798 int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
801 regmap_debugfs_exit(map);
803 map->max_register = config->max_register;
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->cache_type = config->cache_type;
810 regmap_debugfs_init(map, config->name);
812 map->cache_bypass = false;
813 map->cache_only = false;
815 return regcache_init(map, config);
817 EXPORT_SYMBOL_GPL(regmap_reinit_cache);
820 * regmap_exit(): Free a previously allocated register map
822 void regmap_exit(struct regmap *map)
825 regmap_debugfs_exit(map);
826 regmap_range_exit(map);
827 if (map->bus && map->bus->free_context)
828 map->bus->free_context(map->bus_context);
829 kfree(map->work_buf);
832 EXPORT_SYMBOL_GPL(regmap_exit);
834 static int dev_get_regmap_match(struct device *dev, void *res, void *data)
836 struct regmap **r = res;
842 /* If the user didn't specify a name match any */
844 return (*r)->name == data;
850 * dev_get_regmap(): Obtain the regmap (if any) for a device
852 * @dev: Device to retrieve the map for
853 * @name: Optional name for the register map, usually NULL.
855 * Returns the regmap for the device if one is present, or NULL. If
856 * name is specified then it must match the name specified when
857 * registering the device, if it is NULL then the first regmap found
858 * will be used. Devices with multiple register maps are very rare,
859 * generic code should normally not need to specify a name.
861 struct regmap *dev_get_regmap(struct device *dev, const char *name)
863 struct regmap **r = devres_find(dev, dev_get_regmap_release,
864 dev_get_regmap_match, (void *)name);
870 EXPORT_SYMBOL_GPL(dev_get_regmap);
872 static int _regmap_select_page(struct regmap *map, unsigned int *reg,
873 struct regmap_range_node *range,
874 unsigned int val_num)
877 unsigned int win_offset;
878 unsigned int win_page;
882 win_offset = (*reg - range->range_min) % range->window_len;
883 win_page = (*reg - range->range_min) / range->window_len;
886 /* Bulk write shouldn't cross range boundary */
887 if (*reg + val_num - 1 > range->range_max)
890 /* ... or single page boundary */
891 if (val_num > range->window_len - win_offset)
895 /* It is possible to have selector register inside data window.
896 In that case, selector register is located on every page and
897 it needs no page switching, when accessed alone. */
899 range->window_start + win_offset != range->selector_reg) {
900 /* Use separate work_buf during page switching */
901 orig_work_buf = map->work_buf;
902 map->work_buf = map->selector_work_buf;
904 ret = _regmap_update_bits(map, range->selector_reg,
905 range->selector_mask,
906 win_page << range->selector_shift,
909 map->work_buf = orig_work_buf;
915 *reg = range->window_start + win_offset;
920 static int _regmap_raw_write(struct regmap *map, unsigned int reg,
921 const void *val, size_t val_len, bool async)
923 struct regmap_range_node *range;
925 u8 *u8 = map->work_buf;
926 void *work_val = map->work_buf + map->format.reg_bytes +
927 map->format.pad_bytes;
935 /* Check for unwritable registers before we start */
936 if (map->writeable_reg)
937 for (i = 0; i < val_len / map->format.val_bytes; i++)
938 if (!map->writeable_reg(map->dev,
939 reg + (i * map->reg_stride)))
942 if (!map->cache_bypass && map->format.parse_val) {
944 int val_bytes = map->format.val_bytes;
945 for (i = 0; i < val_len / val_bytes; i++) {
946 ival = map->format.parse_val(val + (i * val_bytes));
947 ret = regcache_write(map, reg + (i * map->reg_stride),
951 "Error in caching of register: %x ret: %d\n",
956 if (map->cache_only) {
957 map->cache_dirty = true;
962 range = _regmap_range_lookup(map, reg);
964 int val_num = val_len / map->format.val_bytes;
965 int win_offset = (reg - range->range_min) % range->window_len;
966 int win_residue = range->window_len - win_offset;
968 /* If the write goes beyond the end of the window split it */
969 while (val_num > win_residue) {
970 dev_dbg(map->dev, "Writing window %d/%zu\n",
971 win_residue, val_len / map->format.val_bytes);
972 ret = _regmap_raw_write(map, reg, val, win_residue *
973 map->format.val_bytes, async);
978 val_num -= win_residue;
979 val += win_residue * map->format.val_bytes;
980 val_len -= win_residue * map->format.val_bytes;
982 win_offset = (reg - range->range_min) %
984 win_residue = range->window_len - win_offset;
987 ret = _regmap_select_page(map, ®, range, val_num);
992 map->format.format_reg(map->work_buf, reg, map->reg_shift);
994 u8[0] |= map->write_flag_mask;
996 if (async && map->bus->async_write) {
997 struct regmap_async *async = map->bus->async_alloc();
1001 async->work_buf = kzalloc(map->format.buf_size,
1002 GFP_KERNEL | GFP_DMA);
1003 if (!async->work_buf) {
1008 INIT_WORK(&async->cleanup, async_cleanup);
1011 /* If the caller supplied the value we can use it safely. */
1012 memcpy(async->work_buf, map->work_buf, map->format.pad_bytes +
1013 map->format.reg_bytes + map->format.val_bytes);
1014 if (val == work_val)
1015 val = async->work_buf + map->format.pad_bytes +
1016 map->format.reg_bytes;
1018 spin_lock_irqsave(&map->async_lock, flags);
1019 list_add_tail(&async->list, &map->async_list);
1020 spin_unlock_irqrestore(&map->async_lock, flags);
1022 ret = map->bus->async_write(map->bus_context, async->work_buf,
1023 map->format.reg_bytes +
1024 map->format.pad_bytes,
1025 val, val_len, async);
1028 dev_err(map->dev, "Failed to schedule write: %d\n",
1031 spin_lock_irqsave(&map->async_lock, flags);
1032 list_del(&async->list);
1033 spin_unlock_irqrestore(&map->async_lock, flags);
1035 kfree(async->work_buf);
1042 trace_regmap_hw_write_start(map->dev, reg,
1043 val_len / map->format.val_bytes);
1045 /* If we're doing a single register write we can probably just
1046 * send the work_buf directly, otherwise try to do a gather
1049 if (val == work_val)
1050 ret = map->bus->write(map->bus_context, map->work_buf,
1051 map->format.reg_bytes +
1052 map->format.pad_bytes +
1054 else if (map->bus->gather_write)
1055 ret = map->bus->gather_write(map->bus_context, map->work_buf,
1056 map->format.reg_bytes +
1057 map->format.pad_bytes,
1060 /* If that didn't work fall back on linearising by hand. */
1061 if (ret == -ENOTSUPP) {
1062 len = map->format.reg_bytes + map->format.pad_bytes + val_len;
1063 buf = kzalloc(len, GFP_KERNEL);
1067 memcpy(buf, map->work_buf, map->format.reg_bytes);
1068 memcpy(buf + map->format.reg_bytes + map->format.pad_bytes,
1070 ret = map->bus->write(map->bus_context, buf, len);
1075 trace_regmap_hw_write_done(map->dev, reg,
1076 val_len / map->format.val_bytes);
1081 static int _regmap_bus_formatted_write(void *context, unsigned int reg,
1085 struct regmap_range_node *range;
1086 struct regmap *map = context;
1088 BUG_ON(!map->bus || !map->format.format_write);
1090 range = _regmap_range_lookup(map, reg);
1092 ret = _regmap_select_page(map, ®, range, 1);
1097 map->format.format_write(map, reg, val);
1099 trace_regmap_hw_write_start(map->dev, reg, 1);
1101 ret = map->bus->write(map->bus_context, map->work_buf,
1102 map->format.buf_size);
1104 trace_regmap_hw_write_done(map->dev, reg, 1);
1109 static int _regmap_bus_raw_write(void *context, unsigned int reg,
1112 struct regmap *map = context;
1114 BUG_ON(!map->bus || !map->format.format_val);
1116 map->format.format_val(map->work_buf + map->format.reg_bytes
1117 + map->format.pad_bytes, val, 0);
1118 return _regmap_raw_write(map, reg,
1120 map->format.reg_bytes +
1121 map->format.pad_bytes,
1122 map->format.val_bytes, false);
1125 static inline void *_regmap_map_get_context(struct regmap *map)
1127 return (map->bus) ? map : map->bus_context;
1130 int _regmap_write(struct regmap *map, unsigned int reg,
1134 void *context = _regmap_map_get_context(map);
1136 if (!map->cache_bypass && !map->defer_caching) {
1137 ret = regcache_write(map, reg, val);
1140 if (map->cache_only) {
1141 map->cache_dirty = true;
1147 if (strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
1148 dev_info(map->dev, "%x <= %x\n", reg, val);
1151 trace_regmap_reg_write(map->dev, reg, val);
1153 return map->reg_write(context, reg, val);
1157 * regmap_write(): Write a value to a single register
1159 * @map: Register map to write to
1160 * @reg: Register to write to
1161 * @val: Value to be written
1163 * A value of zero will be returned on success, a negative errno will
1164 * be returned in error cases.
1166 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
1170 if (reg % map->reg_stride)
1173 map->lock(map->lock_arg);
1175 ret = _regmap_write(map, reg, val);
1177 map->unlock(map->lock_arg);
1181 EXPORT_SYMBOL_GPL(regmap_write);
1184 * regmap_raw_write(): Write raw values to one or more registers
1186 * @map: Register map to write to
1187 * @reg: Initial register to write to
1188 * @val: Block of data to be written, laid out for direct transmission to the
1190 * @val_len: Length of data pointed to by val.
1192 * This function is intended to be used for things like firmware
1193 * download where a large block of data needs to be transferred to the
1194 * device. No formatting will be done on the data provided.
1196 * A value of zero will be returned on success, a negative errno will
1197 * be returned in error cases.
1199 int regmap_raw_write(struct regmap *map, unsigned int reg,
1200 const void *val, size_t val_len)
1206 if (val_len % map->format.val_bytes)
1208 if (reg % map->reg_stride)
1211 map->lock(map->lock_arg);
1213 ret = _regmap_raw_write(map, reg, val, val_len, false);
1215 map->unlock(map->lock_arg);
1219 EXPORT_SYMBOL_GPL(regmap_raw_write);
1222 * regmap_bulk_write(): Write multiple registers to the device
1224 * @map: Register map to write to
1225 * @reg: First register to be write from
1226 * @val: Block of data to be written, in native register size for device
1227 * @val_count: Number of registers to write
1229 * This function is intended to be used for writing a large block of
1230 * data to the device either in single transfer or multiple transfer.
1232 * A value of zero will be returned on success, a negative errno will
1233 * be returned in error cases.
1235 int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
1239 size_t val_bytes = map->format.val_bytes;
1244 if (!map->format.parse_val)
1246 if (reg % map->reg_stride)
1249 map->lock(map->lock_arg);
1251 /* No formatting is require if val_byte is 1 */
1252 if (val_bytes == 1) {
1255 wval = kmemdup(val, val_count * val_bytes, GFP_KERNEL);
1258 dev_err(map->dev, "Error in memory allocation\n");
1261 for (i = 0; i < val_count * val_bytes; i += val_bytes)
1262 map->format.parse_val(wval + i);
1265 * Some devices does not support bulk write, for
1266 * them we have a series of single write operations.
1268 if (map->use_single_rw) {
1269 for (i = 0; i < val_count; i++) {
1270 ret = regmap_raw_write(map,
1271 reg + (i * map->reg_stride),
1272 val + (i * val_bytes),
1278 ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count,
1286 map->unlock(map->lock_arg);
1289 EXPORT_SYMBOL_GPL(regmap_bulk_write);
1292 * regmap_raw_write_async(): Write raw values to one or more registers
1295 * @map: Register map to write to
1296 * @reg: Initial register to write to
1297 * @val: Block of data to be written, laid out for direct transmission to the
1298 * device. Must be valid until regmap_async_complete() is called.
1299 * @val_len: Length of data pointed to by val.
1301 * This function is intended to be used for things like firmware
1302 * download where a large block of data needs to be transferred to the
1303 * device. No formatting will be done on the data provided.
1305 * If supported by the underlying bus the write will be scheduled
1306 * asynchronously, helping maximise I/O speed on higher speed buses
1307 * like SPI. regmap_async_complete() can be called to ensure that all
1308 * asynchrnous writes have been completed.
1310 * A value of zero will be returned on success, a negative errno will
1311 * be returned in error cases.
1313 int regmap_raw_write_async(struct regmap *map, unsigned int reg,
1314 const void *val, size_t val_len)
1318 if (val_len % map->format.val_bytes)
1320 if (reg % map->reg_stride)
1323 map->lock(map->lock_arg);
1325 ret = _regmap_raw_write(map, reg, val, val_len, true);
1327 map->unlock(map->lock_arg);
1331 EXPORT_SYMBOL_GPL(regmap_raw_write_async);
1333 static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
1334 unsigned int val_len)
1336 struct regmap_range_node *range;
1337 u8 *u8 = map->work_buf;
1342 range = _regmap_range_lookup(map, reg);
1344 ret = _regmap_select_page(map, ®, range,
1345 val_len / map->format.val_bytes);
1350 map->format.format_reg(map->work_buf, reg, map->reg_shift);
1353 * Some buses or devices flag reads by setting the high bits in the
1354 * register addresss; since it's always the high bits for all
1355 * current formats we can do this here rather than in
1356 * formatting. This may break if we get interesting formats.
1358 u8[0] |= map->read_flag_mask;
1360 trace_regmap_hw_read_start(map->dev, reg,
1361 val_len / map->format.val_bytes);
1363 ret = map->bus->read(map->bus_context, map->work_buf,
1364 map->format.reg_bytes + map->format.pad_bytes,
1367 trace_regmap_hw_read_done(map->dev, reg,
1368 val_len / map->format.val_bytes);
1373 static int _regmap_bus_read(void *context, unsigned int reg,
1377 struct regmap *map = context;
1379 if (!map->format.parse_val)
1382 ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
1384 *val = map->format.parse_val(map->work_buf);
1389 static int _regmap_read(struct regmap *map, unsigned int reg,
1393 void *context = _regmap_map_get_context(map);
1395 BUG_ON(!map->reg_read);
1397 if (!map->cache_bypass) {
1398 ret = regcache_read(map, reg, val);
1403 if (map->cache_only)
1406 ret = map->reg_read(context, reg, val);
1409 if (strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
1410 dev_info(map->dev, "%x => %x\n", reg, *val);
1413 trace_regmap_reg_read(map->dev, reg, *val);
1415 if (!map->cache_bypass)
1416 regcache_write(map, reg, *val);
1423 * regmap_read(): Read a value from a single register
1425 * @map: Register map to write to
1426 * @reg: Register to be read from
1427 * @val: Pointer to store read value
1429 * A value of zero will be returned on success, a negative errno will
1430 * be returned in error cases.
1432 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
1436 if (reg % map->reg_stride)
1439 map->lock(map->lock_arg);
1441 ret = _regmap_read(map, reg, val);
1443 map->unlock(map->lock_arg);
1447 EXPORT_SYMBOL_GPL(regmap_read);
1450 * regmap_raw_read(): Read raw data from the device
1452 * @map: Register map to write to
1453 * @reg: First register to be read from
1454 * @val: Pointer to store read value
1455 * @val_len: Size of data to read
1457 * A value of zero will be returned on success, a negative errno will
1458 * be returned in error cases.
1460 int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
1463 size_t val_bytes = map->format.val_bytes;
1464 size_t val_count = val_len / val_bytes;
1470 if (val_len % map->format.val_bytes)
1472 if (reg % map->reg_stride)
1475 map->lock(map->lock_arg);
1477 if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
1478 map->cache_type == REGCACHE_NONE) {
1479 /* Physical block read if there's no cache involved */
1480 ret = _regmap_raw_read(map, reg, val, val_len);
1483 /* Otherwise go word by word for the cache; should be low
1484 * cost as we expect to hit the cache.
1486 for (i = 0; i < val_count; i++) {
1487 ret = _regmap_read(map, reg + (i * map->reg_stride),
1492 map->format.format_val(val + (i * val_bytes), v, 0);
1497 map->unlock(map->lock_arg);
1501 EXPORT_SYMBOL_GPL(regmap_raw_read);
1504 * regmap_bulk_read(): Read multiple registers from the device
1506 * @map: Register map to write to
1507 * @reg: First register to be read from
1508 * @val: Pointer to store read value, in native register size for device
1509 * @val_count: Number of registers to read
1511 * A value of zero will be returned on success, a negative errno will
1512 * be returned in error cases.
1514 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
1518 size_t val_bytes = map->format.val_bytes;
1519 bool vol = regmap_volatile_range(map, reg, val_count);
1523 if (!map->format.parse_val)
1525 if (reg % map->reg_stride)
1528 if (vol || map->cache_type == REGCACHE_NONE) {
1530 * Some devices does not support bulk read, for
1531 * them we have a series of single read operations.
1533 if (map->use_single_rw) {
1534 for (i = 0; i < val_count; i++) {
1535 ret = regmap_raw_read(map,
1536 reg + (i * map->reg_stride),
1537 val + (i * val_bytes),
1543 ret = regmap_raw_read(map, reg, val,
1544 val_bytes * val_count);
1549 for (i = 0; i < val_count * val_bytes; i += val_bytes)
1550 map->format.parse_val(val + i);
1552 for (i = 0; i < val_count; i++) {
1554 ret = regmap_read(map, reg + (i * map->reg_stride),
1558 memcpy(val + (i * val_bytes), &ival, val_bytes);
1564 EXPORT_SYMBOL_GPL(regmap_bulk_read);
1566 static int _regmap_update_bits(struct regmap *map, unsigned int reg,
1567 unsigned int mask, unsigned int val,
1571 unsigned int tmp, orig;
1573 ret = _regmap_read(map, reg, &orig);
1581 ret = _regmap_write(map, reg, tmp);
1591 * regmap_update_bits: Perform a read/modify/write cycle on the register map
1593 * @map: Register map to update
1594 * @reg: Register to update
1595 * @mask: Bitmask to change
1596 * @val: New value for bitmask
1598 * Returns zero for success, a negative number on error.
1600 int regmap_update_bits(struct regmap *map, unsigned int reg,
1601 unsigned int mask, unsigned int val)
1606 map->lock(map->lock_arg);
1607 ret = _regmap_update_bits(map, reg, mask, val, &change);
1608 map->unlock(map->lock_arg);
1612 EXPORT_SYMBOL_GPL(regmap_update_bits);
1615 * regmap_update_bits_check: Perform a read/modify/write cycle on the
1616 * register map and report if updated
1618 * @map: Register map to update
1619 * @reg: Register to update
1620 * @mask: Bitmask to change
1621 * @val: New value for bitmask
1622 * @change: Boolean indicating if a write was done
1624 * Returns zero for success, a negative number on error.
1626 int regmap_update_bits_check(struct regmap *map, unsigned int reg,
1627 unsigned int mask, unsigned int val,
1632 map->lock(map->lock_arg);
1633 ret = _regmap_update_bits(map, reg, mask, val, change);
1634 map->unlock(map->lock_arg);
1637 EXPORT_SYMBOL_GPL(regmap_update_bits_check);
1639 void regmap_async_complete_cb(struct regmap_async *async, int ret)
1641 struct regmap *map = async->map;
1644 spin_lock(&map->async_lock);
1646 list_del(&async->list);
1647 wake = list_empty(&map->async_list);
1650 map->async_ret = ret;
1652 spin_unlock(&map->async_lock);
1654 schedule_work(&async->cleanup);
1657 wake_up(&map->async_waitq);
1659 EXPORT_SYMBOL_GPL(regmap_async_complete_cb);
1661 static int regmap_async_is_done(struct regmap *map)
1663 unsigned long flags;
1666 spin_lock_irqsave(&map->async_lock, flags);
1667 ret = list_empty(&map->async_list);
1668 spin_unlock_irqrestore(&map->async_lock, flags);
1674 * regmap_async_complete: Ensure all asynchronous I/O has completed.
1676 * @map: Map to operate on.
1678 * Blocks until any pending asynchronous I/O has completed. Returns
1679 * an error code for any failed I/O operations.
1681 int regmap_async_complete(struct regmap *map)
1683 unsigned long flags;
1686 /* Nothing to do with no async support */
1687 if (!map->bus->async_write)
1690 wait_event(map->async_waitq, regmap_async_is_done(map));
1692 spin_lock_irqsave(&map->async_lock, flags);
1693 ret = map->async_ret;
1695 spin_unlock_irqrestore(&map->async_lock, flags);
1699 EXPORT_SYMBOL_GPL(regmap_async_complete);
1702 * regmap_register_patch: Register and apply register updates to be applied
1703 * on device initialistion
1705 * @map: Register map to apply updates to.
1706 * @regs: Values to update.
1707 * @num_regs: Number of entries in regs.
1709 * Register a set of register updates to be applied to the device
1710 * whenever the device registers are synchronised with the cache and
1711 * apply them immediately. Typically this is used to apply
1712 * corrections to be applied to the device defaults on startup, such
1713 * as the updates some vendors provide to undocumented registers.
1715 int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
1721 /* If needed the implementation can be extended to support this */
1725 map->lock(map->lock_arg);
1727 bypass = map->cache_bypass;
1729 map->cache_bypass = true;
1731 /* Write out first; it's useful to apply even if we fail later. */
1732 for (i = 0; i < num_regs; i++) {
1733 ret = _regmap_write(map, regs[i].reg, regs[i].def);
1735 dev_err(map->dev, "Failed to write %x = %x: %d\n",
1736 regs[i].reg, regs[i].def, ret);
1741 map->patch = kcalloc(num_regs, sizeof(struct reg_default), GFP_KERNEL);
1742 if (map->patch != NULL) {
1743 memcpy(map->patch, regs,
1744 num_regs * sizeof(struct reg_default));
1745 map->patch_regs = num_regs;
1751 map->cache_bypass = bypass;
1753 map->unlock(map->lock_arg);
1757 EXPORT_SYMBOL_GPL(regmap_register_patch);
1760 * regmap_get_val_bytes(): Report the size of a register value
1762 * Report the size of a register value, mainly intended to for use by
1763 * generic infrastructure built on top of regmap.
1765 int regmap_get_val_bytes(struct regmap *map)
1767 if (map->format.format_write)
1770 return map->format.val_bytes;
1772 EXPORT_SYMBOL_GPL(regmap_get_val_bytes);
1774 static int __init regmap_initcall(void)
1776 regmap_debugfs_initcall();
1780 postcore_initcall(regmap_initcall);