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 bool regmap_check_range_table(struct regmap *map, unsigned int reg,
69 const struct regmap_access_table *table)
71 /* Check "no ranges" first */
72 if (regmap_reg_in_ranges(reg, table->no_ranges, table->n_no_ranges))
75 /* In case zero "yes ranges" are supplied, any reg is OK */
76 if (!table->n_yes_ranges)
79 return regmap_reg_in_ranges(reg, table->yes_ranges,
82 EXPORT_SYMBOL_GPL(regmap_check_range_table);
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);
132 bool regmap_precious(struct regmap *map, unsigned int reg)
134 if (!regmap_readable(map, reg))
137 if (map->precious_reg)
138 return map->precious_reg(map->dev, reg);
140 if (map->precious_table)
141 return regmap_check_range_table(map, reg, map->precious_table);
146 static bool regmap_volatile_range(struct regmap *map, unsigned int reg,
151 for (i = 0; i < num; i++)
152 if (!regmap_volatile(map, reg + i))
158 static void regmap_format_2_6_write(struct regmap *map,
159 unsigned int reg, unsigned int val)
161 u8 *out = map->work_buf;
163 *out = (reg << 6) | val;
166 static void regmap_format_4_12_write(struct regmap *map,
167 unsigned int reg, unsigned int val)
169 __be16 *out = map->work_buf;
170 *out = cpu_to_be16((reg << 12) | val);
173 static void regmap_format_7_9_write(struct regmap *map,
174 unsigned int reg, unsigned int val)
176 __be16 *out = map->work_buf;
177 *out = cpu_to_be16((reg << 9) | val);
180 static void regmap_format_10_14_write(struct regmap *map,
181 unsigned int reg, unsigned int val)
183 u8 *out = map->work_buf;
186 out[1] = (val >> 8) | (reg << 6);
190 static void regmap_format_8(void *buf, unsigned int val, unsigned int shift)
197 static void regmap_format_16_be(void *buf, unsigned int val, unsigned int shift)
201 b[0] = cpu_to_be16(val << shift);
204 static void regmap_format_16_native(void *buf, unsigned int val,
207 *(u16 *)buf = val << shift;
210 static void regmap_format_24(void *buf, unsigned int val, unsigned int shift)
221 static void regmap_format_32_be(void *buf, unsigned int val, unsigned int shift)
225 b[0] = cpu_to_be32(val << shift);
228 static void regmap_format_32_native(void *buf, unsigned int val,
231 *(u32 *)buf = val << shift;
234 static void regmap_parse_inplace_noop(void *buf)
238 static unsigned int regmap_parse_8(const void *buf)
245 static unsigned int regmap_parse_16_be(const void *buf)
247 const __be16 *b = buf;
249 return be16_to_cpu(b[0]);
252 static void regmap_parse_16_be_inplace(void *buf)
256 b[0] = be16_to_cpu(b[0]);
259 static unsigned int regmap_parse_16_native(const void *buf)
264 static unsigned int regmap_parse_24(const void *buf)
267 unsigned int ret = b[2];
268 ret |= ((unsigned int)b[1]) << 8;
269 ret |= ((unsigned int)b[0]) << 16;
274 static unsigned int regmap_parse_32_be(const void *buf)
276 const __be32 *b = buf;
278 return be32_to_cpu(b[0]);
281 static void regmap_parse_32_be_inplace(void *buf)
285 b[0] = be32_to_cpu(b[0]);
288 static unsigned int regmap_parse_32_native(const void *buf)
293 static void regmap_lock_mutex(void *__map)
295 struct regmap *map = __map;
296 mutex_lock(&map->mutex);
299 static void regmap_unlock_mutex(void *__map)
301 struct regmap *map = __map;
302 mutex_unlock(&map->mutex);
305 static void regmap_lock_spinlock(void *__map)
307 struct regmap *map = __map;
310 spin_lock_irqsave(&map->spinlock, flags);
311 map->spinlock_flags = flags;
314 static void regmap_unlock_spinlock(void *__map)
316 struct regmap *map = __map;
317 spin_unlock_irqrestore(&map->spinlock, map->spinlock_flags);
320 static void dev_get_regmap_release(struct device *dev, void *res)
323 * We don't actually have anything to do here; the goal here
324 * is not to manage the regmap but to provide a simple way to
325 * get the regmap back given a struct device.
329 static bool _regmap_range_add(struct regmap *map,
330 struct regmap_range_node *data)
332 struct rb_root *root = &map->range_tree;
333 struct rb_node **new = &(root->rb_node), *parent = NULL;
336 struct regmap_range_node *this =
337 container_of(*new, struct regmap_range_node, node);
340 if (data->range_max < this->range_min)
341 new = &((*new)->rb_left);
342 else if (data->range_min > this->range_max)
343 new = &((*new)->rb_right);
348 rb_link_node(&data->node, parent, new);
349 rb_insert_color(&data->node, root);
354 static struct regmap_range_node *_regmap_range_lookup(struct regmap *map,
357 struct rb_node *node = map->range_tree.rb_node;
360 struct regmap_range_node *this =
361 container_of(node, struct regmap_range_node, node);
363 if (reg < this->range_min)
364 node = node->rb_left;
365 else if (reg > this->range_max)
366 node = node->rb_right;
374 static void regmap_range_exit(struct regmap *map)
376 struct rb_node *next;
377 struct regmap_range_node *range_node;
379 next = rb_first(&map->range_tree);
381 range_node = rb_entry(next, struct regmap_range_node, node);
382 next = rb_next(&range_node->node);
383 rb_erase(&range_node->node, &map->range_tree);
387 kfree(map->selector_work_buf);
391 * regmap_init(): Initialise register map
393 * @dev: Device that will be interacted with
394 * @bus: Bus-specific callbacks to use with device
395 * @bus_context: Data passed to bus-specific callbacks
396 * @config: Configuration for register map
398 * The return value will be an ERR_PTR() on error or a valid pointer to
399 * a struct regmap. This function should generally not be called
400 * directly, it should be called by bus-specific init functions.
402 struct regmap *regmap_init(struct device *dev,
403 const struct regmap_bus *bus,
405 const struct regmap_config *config)
407 struct regmap *map, **m;
409 enum regmap_endian reg_endian, val_endian;
415 map = kzalloc(sizeof(*map), GFP_KERNEL);
421 if (config->lock && config->unlock) {
422 map->lock = config->lock;
423 map->unlock = config->unlock;
424 map->lock_arg = config->lock_arg;
426 if ((bus && bus->fast_io) ||
428 spin_lock_init(&map->spinlock);
429 map->lock = regmap_lock_spinlock;
430 map->unlock = regmap_unlock_spinlock;
432 mutex_init(&map->mutex);
433 map->lock = regmap_lock_mutex;
434 map->unlock = regmap_unlock_mutex;
438 map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8);
439 map->format.pad_bytes = config->pad_bits / 8;
440 map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8);
441 map->format.buf_size = DIV_ROUND_UP(config->reg_bits +
442 config->val_bits + config->pad_bits, 8);
443 map->reg_shift = config->pad_bits % 8;
444 if (config->reg_stride)
445 map->reg_stride = config->reg_stride;
448 map->use_single_rw = config->use_single_rw;
451 map->bus_context = bus_context;
452 map->max_register = config->max_register;
453 map->wr_table = config->wr_table;
454 map->rd_table = config->rd_table;
455 map->volatile_table = config->volatile_table;
456 map->precious_table = config->precious_table;
457 map->writeable_reg = config->writeable_reg;
458 map->readable_reg = config->readable_reg;
459 map->volatile_reg = config->volatile_reg;
460 map->precious_reg = config->precious_reg;
461 map->cache_type = config->cache_type;
462 map->name = config->name;
464 spin_lock_init(&map->async_lock);
465 INIT_LIST_HEAD(&map->async_list);
466 init_waitqueue_head(&map->async_waitq);
468 if (config->read_flag_mask || config->write_flag_mask) {
469 map->read_flag_mask = config->read_flag_mask;
470 map->write_flag_mask = config->write_flag_mask;
472 map->read_flag_mask = bus->read_flag_mask;
476 map->reg_read = config->reg_read;
477 map->reg_write = config->reg_write;
479 map->defer_caching = false;
480 goto skip_format_initialization;
482 map->reg_read = _regmap_bus_read;
485 reg_endian = config->reg_format_endian;
486 if (reg_endian == REGMAP_ENDIAN_DEFAULT)
487 reg_endian = bus->reg_format_endian_default;
488 if (reg_endian == REGMAP_ENDIAN_DEFAULT)
489 reg_endian = REGMAP_ENDIAN_BIG;
491 val_endian = config->val_format_endian;
492 if (val_endian == REGMAP_ENDIAN_DEFAULT)
493 val_endian = bus->val_format_endian_default;
494 if (val_endian == REGMAP_ENDIAN_DEFAULT)
495 val_endian = REGMAP_ENDIAN_BIG;
497 switch (config->reg_bits + map->reg_shift) {
499 switch (config->val_bits) {
501 map->format.format_write = regmap_format_2_6_write;
509 switch (config->val_bits) {
511 map->format.format_write = regmap_format_4_12_write;
519 switch (config->val_bits) {
521 map->format.format_write = regmap_format_7_9_write;
529 switch (config->val_bits) {
531 map->format.format_write = regmap_format_10_14_write;
539 map->format.format_reg = regmap_format_8;
543 switch (reg_endian) {
544 case REGMAP_ENDIAN_BIG:
545 map->format.format_reg = regmap_format_16_be;
547 case REGMAP_ENDIAN_NATIVE:
548 map->format.format_reg = regmap_format_16_native;
556 if (reg_endian != REGMAP_ENDIAN_BIG)
558 map->format.format_reg = regmap_format_24;
562 switch (reg_endian) {
563 case REGMAP_ENDIAN_BIG:
564 map->format.format_reg = regmap_format_32_be;
566 case REGMAP_ENDIAN_NATIVE:
567 map->format.format_reg = regmap_format_32_native;
578 if (val_endian == REGMAP_ENDIAN_NATIVE)
579 map->format.parse_inplace = regmap_parse_inplace_noop;
581 switch (config->val_bits) {
583 map->format.format_val = regmap_format_8;
584 map->format.parse_val = regmap_parse_8;
585 map->format.parse_inplace = regmap_parse_inplace_noop;
588 switch (val_endian) {
589 case REGMAP_ENDIAN_BIG:
590 map->format.format_val = regmap_format_16_be;
591 map->format.parse_val = regmap_parse_16_be;
592 map->format.parse_inplace = regmap_parse_16_be_inplace;
594 case REGMAP_ENDIAN_NATIVE:
595 map->format.format_val = regmap_format_16_native;
596 map->format.parse_val = regmap_parse_16_native;
603 if (val_endian != REGMAP_ENDIAN_BIG)
605 map->format.format_val = regmap_format_24;
606 map->format.parse_val = regmap_parse_24;
609 switch (val_endian) {
610 case REGMAP_ENDIAN_BIG:
611 map->format.format_val = regmap_format_32_be;
612 map->format.parse_val = regmap_parse_32_be;
613 map->format.parse_inplace = regmap_parse_32_be_inplace;
615 case REGMAP_ENDIAN_NATIVE:
616 map->format.format_val = regmap_format_32_native;
617 map->format.parse_val = regmap_parse_32_native;
625 if (map->format.format_write) {
626 if ((reg_endian != REGMAP_ENDIAN_BIG) ||
627 (val_endian != REGMAP_ENDIAN_BIG))
629 map->use_single_rw = true;
632 if (!map->format.format_write &&
633 !(map->format.format_reg && map->format.format_val))
636 map->work_buf = kzalloc(map->format.buf_size, GFP_KERNEL);
637 if (map->work_buf == NULL) {
642 if (map->format.format_write) {
643 map->defer_caching = false;
644 map->reg_write = _regmap_bus_formatted_write;
645 } else if (map->format.format_val) {
646 map->defer_caching = true;
647 map->reg_write = _regmap_bus_raw_write;
650 skip_format_initialization:
652 map->range_tree = RB_ROOT;
653 for (i = 0; i < config->num_ranges; i++) {
654 const struct regmap_range_cfg *range_cfg = &config->ranges[i];
655 struct regmap_range_node *new;
658 if (range_cfg->range_max < range_cfg->range_min) {
659 dev_err(map->dev, "Invalid range %d: %d < %d\n", i,
660 range_cfg->range_max, range_cfg->range_min);
664 if (range_cfg->range_max > map->max_register) {
665 dev_err(map->dev, "Invalid range %d: %d > %d\n", i,
666 range_cfg->range_max, map->max_register);
670 if (range_cfg->selector_reg > map->max_register) {
672 "Invalid range %d: selector out of map\n", i);
676 if (range_cfg->window_len == 0) {
677 dev_err(map->dev, "Invalid range %d: window_len 0\n",
682 /* Make sure, that this register range has no selector
683 or data window within its boundary */
684 for (j = 0; j < config->num_ranges; j++) {
685 unsigned sel_reg = config->ranges[j].selector_reg;
686 unsigned win_min = config->ranges[j].window_start;
687 unsigned win_max = win_min +
688 config->ranges[j].window_len - 1;
690 /* Allow data window inside its own virtual range */
694 if (range_cfg->range_min <= sel_reg &&
695 sel_reg <= range_cfg->range_max) {
697 "Range %d: selector for %d in window\n",
702 if (!(win_max < range_cfg->range_min ||
703 win_min > range_cfg->range_max)) {
705 "Range %d: window for %d in window\n",
711 new = kzalloc(sizeof(*new), GFP_KERNEL);
718 new->name = range_cfg->name;
719 new->range_min = range_cfg->range_min;
720 new->range_max = range_cfg->range_max;
721 new->selector_reg = range_cfg->selector_reg;
722 new->selector_mask = range_cfg->selector_mask;
723 new->selector_shift = range_cfg->selector_shift;
724 new->window_start = range_cfg->window_start;
725 new->window_len = range_cfg->window_len;
727 if (_regmap_range_add(map, new) == false) {
728 dev_err(map->dev, "Failed to add range %d\n", i);
733 if (map->selector_work_buf == NULL) {
734 map->selector_work_buf =
735 kzalloc(map->format.buf_size, GFP_KERNEL);
736 if (map->selector_work_buf == NULL) {
743 regmap_debugfs_init(map, config->name);
745 ret = regcache_init(map, config);
749 /* Add a devres resource for dev_get_regmap() */
750 m = devres_alloc(dev_get_regmap_release, sizeof(*m), GFP_KERNEL);
761 regmap_debugfs_exit(map);
764 regmap_range_exit(map);
765 kfree(map->work_buf);
771 EXPORT_SYMBOL_GPL(regmap_init);
773 static void devm_regmap_release(struct device *dev, void *res)
775 regmap_exit(*(struct regmap **)res);
779 * devm_regmap_init(): Initialise managed register map
781 * @dev: Device that will be interacted with
782 * @bus: Bus-specific callbacks to use with device
783 * @bus_context: Data passed to bus-specific callbacks
784 * @config: Configuration for register map
786 * The return value will be an ERR_PTR() on error or a valid pointer
787 * to a struct regmap. This function should generally not be called
788 * directly, it should be called by bus-specific init functions. The
789 * map will be automatically freed by the device management code.
791 struct regmap *devm_regmap_init(struct device *dev,
792 const struct regmap_bus *bus,
794 const struct regmap_config *config)
796 struct regmap **ptr, *regmap;
798 ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL);
800 return ERR_PTR(-ENOMEM);
802 regmap = regmap_init(dev, bus, bus_context, config);
803 if (!IS_ERR(regmap)) {
805 devres_add(dev, ptr);
812 EXPORT_SYMBOL_GPL(devm_regmap_init);
814 static void regmap_field_init(struct regmap_field *rm_field,
815 struct regmap *regmap, struct reg_field reg_field)
817 int field_bits = reg_field.msb - reg_field.lsb + 1;
818 rm_field->regmap = regmap;
819 rm_field->reg = reg_field.reg;
820 rm_field->shift = reg_field.lsb;
821 rm_field->mask = ((BIT(field_bits) - 1) << reg_field.lsb);
825 * devm_regmap_field_alloc(): Allocate and initialise a register field
828 * @dev: Device that will be interacted with
829 * @regmap: regmap bank in which this register field is located.
830 * @reg_field: Register field with in the bank.
832 * The return value will be an ERR_PTR() on error or a valid pointer
833 * to a struct regmap_field. The regmap_field will be automatically freed
834 * by the device management code.
836 struct regmap_field *devm_regmap_field_alloc(struct device *dev,
837 struct regmap *regmap, struct reg_field reg_field)
839 struct regmap_field *rm_field = devm_kzalloc(dev,
840 sizeof(*rm_field), GFP_KERNEL);
842 return ERR_PTR(-ENOMEM);
844 regmap_field_init(rm_field, regmap, reg_field);
849 EXPORT_SYMBOL_GPL(devm_regmap_field_alloc);
852 * devm_regmap_field_free(): Free register field allocated using
853 * devm_regmap_field_alloc. Usally drivers need not call this function,
854 * as the memory allocated via devm will be freed as per device-driver
857 * @dev: Device that will be interacted with
858 * @field: regmap field which should be freed.
860 void devm_regmap_field_free(struct device *dev,
861 struct regmap_field *field)
863 devm_kfree(dev, field);
865 EXPORT_SYMBOL_GPL(devm_regmap_field_free);
868 * regmap_field_alloc(): Allocate and initialise a register field
871 * @regmap: regmap bank in which this register field is located.
872 * @reg_field: Register field with in the bank.
874 * The return value will be an ERR_PTR() on error or a valid pointer
875 * to a struct regmap_field. The regmap_field should be freed by the
876 * user once its finished working with it using regmap_field_free().
878 struct regmap_field *regmap_field_alloc(struct regmap *regmap,
879 struct reg_field reg_field)
881 struct regmap_field *rm_field = kzalloc(sizeof(*rm_field), GFP_KERNEL);
884 return ERR_PTR(-ENOMEM);
886 regmap_field_init(rm_field, regmap, reg_field);
890 EXPORT_SYMBOL_GPL(regmap_field_alloc);
893 * regmap_field_free(): Free register field allocated using regmap_field_alloc
895 * @field: regmap field which should be freed.
897 void regmap_field_free(struct regmap_field *field)
901 EXPORT_SYMBOL_GPL(regmap_field_free);
904 * regmap_reinit_cache(): Reinitialise the current register cache
906 * @map: Register map to operate on.
907 * @config: New configuration. Only the cache data will be used.
909 * Discard any existing register cache for the map and initialize a
910 * new cache. This can be used to restore the cache to defaults or to
911 * update the cache configuration to reflect runtime discovery of the
914 * No explicit locking is done here, the user needs to ensure that
915 * this function will not race with other calls to regmap.
917 int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
920 regmap_debugfs_exit(map);
922 map->max_register = config->max_register;
923 map->writeable_reg = config->writeable_reg;
924 map->readable_reg = config->readable_reg;
925 map->volatile_reg = config->volatile_reg;
926 map->precious_reg = config->precious_reg;
927 map->cache_type = config->cache_type;
929 regmap_debugfs_init(map, config->name);
931 map->cache_bypass = false;
932 map->cache_only = false;
934 return regcache_init(map, config);
936 EXPORT_SYMBOL_GPL(regmap_reinit_cache);
939 * regmap_exit(): Free a previously allocated register map
941 void regmap_exit(struct regmap *map)
944 regmap_debugfs_exit(map);
945 regmap_range_exit(map);
946 if (map->bus && map->bus->free_context)
947 map->bus->free_context(map->bus_context);
948 kfree(map->work_buf);
951 EXPORT_SYMBOL_GPL(regmap_exit);
953 static int dev_get_regmap_match(struct device *dev, void *res, void *data)
955 struct regmap **r = res;
961 /* If the user didn't specify a name match any */
963 return (*r)->name == data;
969 * dev_get_regmap(): Obtain the regmap (if any) for a device
971 * @dev: Device to retrieve the map for
972 * @name: Optional name for the register map, usually NULL.
974 * Returns the regmap for the device if one is present, or NULL. If
975 * name is specified then it must match the name specified when
976 * registering the device, if it is NULL then the first regmap found
977 * will be used. Devices with multiple register maps are very rare,
978 * generic code should normally not need to specify a name.
980 struct regmap *dev_get_regmap(struct device *dev, const char *name)
982 struct regmap **r = devres_find(dev, dev_get_regmap_release,
983 dev_get_regmap_match, (void *)name);
989 EXPORT_SYMBOL_GPL(dev_get_regmap);
991 static int _regmap_select_page(struct regmap *map, unsigned int *reg,
992 struct regmap_range_node *range,
993 unsigned int val_num)
996 unsigned int win_offset;
997 unsigned int win_page;
1001 win_offset = (*reg - range->range_min) % range->window_len;
1002 win_page = (*reg - range->range_min) / range->window_len;
1005 /* Bulk write shouldn't cross range boundary */
1006 if (*reg + val_num - 1 > range->range_max)
1009 /* ... or single page boundary */
1010 if (val_num > range->window_len - win_offset)
1014 /* It is possible to have selector register inside data window.
1015 In that case, selector register is located on every page and
1016 it needs no page switching, when accessed alone. */
1018 range->window_start + win_offset != range->selector_reg) {
1019 /* Use separate work_buf during page switching */
1020 orig_work_buf = map->work_buf;
1021 map->work_buf = map->selector_work_buf;
1023 ret = _regmap_update_bits(map, range->selector_reg,
1024 range->selector_mask,
1025 win_page << range->selector_shift,
1028 map->work_buf = orig_work_buf;
1034 *reg = range->window_start + win_offset;
1039 int _regmap_raw_write(struct regmap *map, unsigned int reg,
1040 const void *val, size_t val_len, bool async)
1042 struct regmap_range_node *range;
1043 unsigned long flags;
1044 u8 *u8 = map->work_buf;
1045 void *work_val = map->work_buf + map->format.reg_bytes +
1046 map->format.pad_bytes;
1048 int ret = -ENOTSUPP;
1054 /* Check for unwritable registers before we start */
1055 if (map->writeable_reg)
1056 for (i = 0; i < val_len / map->format.val_bytes; i++)
1057 if (!map->writeable_reg(map->dev,
1058 reg + (i * map->reg_stride)))
1061 if (!map->cache_bypass && map->format.parse_val) {
1063 int val_bytes = map->format.val_bytes;
1064 for (i = 0; i < val_len / val_bytes; i++) {
1065 ival = map->format.parse_val(val + (i * val_bytes));
1066 ret = regcache_write(map, reg + (i * map->reg_stride),
1070 "Error in caching of register: %x ret: %d\n",
1075 if (map->cache_only) {
1076 map->cache_dirty = true;
1081 range = _regmap_range_lookup(map, reg);
1083 int val_num = val_len / map->format.val_bytes;
1084 int win_offset = (reg - range->range_min) % range->window_len;
1085 int win_residue = range->window_len - win_offset;
1087 /* If the write goes beyond the end of the window split it */
1088 while (val_num > win_residue) {
1089 dev_dbg(map->dev, "Writing window %d/%zu\n",
1090 win_residue, val_len / map->format.val_bytes);
1091 ret = _regmap_raw_write(map, reg, val, win_residue *
1092 map->format.val_bytes, async);
1097 val_num -= win_residue;
1098 val += win_residue * map->format.val_bytes;
1099 val_len -= win_residue * map->format.val_bytes;
1101 win_offset = (reg - range->range_min) %
1103 win_residue = range->window_len - win_offset;
1106 ret = _regmap_select_page(map, ®, range, val_num);
1111 map->format.format_reg(map->work_buf, reg, map->reg_shift);
1113 u8[0] |= map->write_flag_mask;
1115 if (async && map->bus->async_write) {
1116 struct regmap_async *async = map->bus->async_alloc();
1120 trace_regmap_async_write_start(map->dev, reg, val_len);
1122 async->work_buf = kzalloc(map->format.buf_size,
1123 GFP_KERNEL | GFP_DMA);
1124 if (!async->work_buf) {
1129 INIT_WORK(&async->cleanup, async_cleanup);
1132 /* If the caller supplied the value we can use it safely. */
1133 memcpy(async->work_buf, map->work_buf, map->format.pad_bytes +
1134 map->format.reg_bytes + map->format.val_bytes);
1135 if (val == work_val)
1136 val = async->work_buf + map->format.pad_bytes +
1137 map->format.reg_bytes;
1139 spin_lock_irqsave(&map->async_lock, flags);
1140 list_add_tail(&async->list, &map->async_list);
1141 spin_unlock_irqrestore(&map->async_lock, flags);
1143 ret = map->bus->async_write(map->bus_context, async->work_buf,
1144 map->format.reg_bytes +
1145 map->format.pad_bytes,
1146 val, val_len, async);
1149 dev_err(map->dev, "Failed to schedule write: %d\n",
1152 spin_lock_irqsave(&map->async_lock, flags);
1153 list_del(&async->list);
1154 spin_unlock_irqrestore(&map->async_lock, flags);
1156 kfree(async->work_buf);
1163 trace_regmap_hw_write_start(map->dev, reg,
1164 val_len / map->format.val_bytes);
1166 /* If we're doing a single register write we can probably just
1167 * send the work_buf directly, otherwise try to do a gather
1170 if (val == work_val)
1171 ret = map->bus->write(map->bus_context, map->work_buf,
1172 map->format.reg_bytes +
1173 map->format.pad_bytes +
1175 else if (map->bus->gather_write)
1176 ret = map->bus->gather_write(map->bus_context, map->work_buf,
1177 map->format.reg_bytes +
1178 map->format.pad_bytes,
1181 /* If that didn't work fall back on linearising by hand. */
1182 if (ret == -ENOTSUPP) {
1183 len = map->format.reg_bytes + map->format.pad_bytes + val_len;
1184 buf = kzalloc(len, GFP_KERNEL);
1188 memcpy(buf, map->work_buf, map->format.reg_bytes);
1189 memcpy(buf + map->format.reg_bytes + map->format.pad_bytes,
1191 ret = map->bus->write(map->bus_context, buf, len);
1196 trace_regmap_hw_write_done(map->dev, reg,
1197 val_len / map->format.val_bytes);
1203 * regmap_can_raw_write - Test if regmap_raw_write() is supported
1205 * @map: Map to check.
1207 bool regmap_can_raw_write(struct regmap *map)
1209 return map->bus && map->format.format_val && map->format.format_reg;
1211 EXPORT_SYMBOL_GPL(regmap_can_raw_write);
1213 static int _regmap_bus_formatted_write(void *context, unsigned int reg,
1217 struct regmap_range_node *range;
1218 struct regmap *map = context;
1220 WARN_ON(!map->bus || !map->format.format_write);
1222 range = _regmap_range_lookup(map, reg);
1224 ret = _regmap_select_page(map, ®, range, 1);
1229 map->format.format_write(map, reg, val);
1231 trace_regmap_hw_write_start(map->dev, reg, 1);
1233 ret = map->bus->write(map->bus_context, map->work_buf,
1234 map->format.buf_size);
1236 trace_regmap_hw_write_done(map->dev, reg, 1);
1241 static int _regmap_bus_raw_write(void *context, unsigned int reg,
1244 struct regmap *map = context;
1246 WARN_ON(!map->bus || !map->format.format_val);
1248 map->format.format_val(map->work_buf + map->format.reg_bytes
1249 + map->format.pad_bytes, val, 0);
1250 return _regmap_raw_write(map, reg,
1252 map->format.reg_bytes +
1253 map->format.pad_bytes,
1254 map->format.val_bytes, false);
1257 static inline void *_regmap_map_get_context(struct regmap *map)
1259 return (map->bus) ? map : map->bus_context;
1262 int _regmap_write(struct regmap *map, unsigned int reg,
1266 void *context = _regmap_map_get_context(map);
1268 if (!regmap_writeable(map, reg))
1271 if (!map->cache_bypass && !map->defer_caching) {
1272 ret = regcache_write(map, reg, val);
1275 if (map->cache_only) {
1276 map->cache_dirty = true;
1282 if (strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
1283 dev_info(map->dev, "%x <= %x\n", reg, val);
1286 trace_regmap_reg_write(map->dev, reg, val);
1288 return map->reg_write(context, reg, val);
1292 * regmap_write(): Write a value to a single register
1294 * @map: Register map to write to
1295 * @reg: Register to write to
1296 * @val: Value to be written
1298 * A value of zero will be returned on success, a negative errno will
1299 * be returned in error cases.
1301 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
1305 if (reg % map->reg_stride)
1308 map->lock(map->lock_arg);
1310 ret = _regmap_write(map, reg, val);
1312 map->unlock(map->lock_arg);
1316 EXPORT_SYMBOL_GPL(regmap_write);
1319 * regmap_raw_write(): Write raw values to one or more registers
1321 * @map: Register map to write to
1322 * @reg: Initial register to write to
1323 * @val: Block of data to be written, laid out for direct transmission to the
1325 * @val_len: Length of data pointed to by val.
1327 * This function is intended to be used for things like firmware
1328 * download where a large block of data needs to be transferred to the
1329 * device. No formatting will be done on the data provided.
1331 * A value of zero will be returned on success, a negative errno will
1332 * be returned in error cases.
1334 int regmap_raw_write(struct regmap *map, unsigned int reg,
1335 const void *val, size_t val_len)
1339 if (!regmap_can_raw_write(map))
1341 if (val_len % map->format.val_bytes)
1344 map->lock(map->lock_arg);
1346 ret = _regmap_raw_write(map, reg, val, val_len, false);
1348 map->unlock(map->lock_arg);
1352 EXPORT_SYMBOL_GPL(regmap_raw_write);
1355 * regmap_field_write(): Write a value to a single register field
1357 * @field: Register field to write to
1358 * @val: Value to be written
1360 * A value of zero will be returned on success, a negative errno will
1361 * be returned in error cases.
1363 int regmap_field_write(struct regmap_field *field, unsigned int val)
1365 return regmap_update_bits(field->regmap, field->reg,
1366 field->mask, val << field->shift);
1368 EXPORT_SYMBOL_GPL(regmap_field_write);
1371 * regmap_bulk_write(): Write multiple registers to the device
1373 * @map: Register map to write to
1374 * @reg: First register to be write from
1375 * @val: Block of data to be written, in native register size for device
1376 * @val_count: Number of registers to write
1378 * This function is intended to be used for writing a large block of
1379 * data to the device either in single transfer or multiple transfer.
1381 * A value of zero will be returned on success, a negative errno will
1382 * be returned in error cases.
1384 int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
1388 size_t val_bytes = map->format.val_bytes;
1393 if (!map->format.parse_inplace)
1395 if (reg % map->reg_stride)
1398 map->lock(map->lock_arg);
1400 /* No formatting is require if val_byte is 1 */
1401 if (val_bytes == 1) {
1404 wval = kmemdup(val, val_count * val_bytes, GFP_KERNEL);
1407 dev_err(map->dev, "Error in memory allocation\n");
1410 for (i = 0; i < val_count * val_bytes; i += val_bytes)
1411 map->format.parse_inplace(wval + i);
1414 * Some devices does not support bulk write, for
1415 * them we have a series of single write operations.
1417 if (map->use_single_rw) {
1418 for (i = 0; i < val_count; i++) {
1419 ret = regmap_raw_write(map,
1420 reg + (i * map->reg_stride),
1421 val + (i * val_bytes),
1427 ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count,
1435 map->unlock(map->lock_arg);
1438 EXPORT_SYMBOL_GPL(regmap_bulk_write);
1441 * regmap_raw_write_async(): Write raw values to one or more registers
1444 * @map: Register map to write to
1445 * @reg: Initial register to write to
1446 * @val: Block of data to be written, laid out for direct transmission to the
1447 * device. Must be valid until regmap_async_complete() is called.
1448 * @val_len: Length of data pointed to by val.
1450 * This function is intended to be used for things like firmware
1451 * download where a large block of data needs to be transferred to the
1452 * device. No formatting will be done on the data provided.
1454 * If supported by the underlying bus the write will be scheduled
1455 * asynchronously, helping maximise I/O speed on higher speed buses
1456 * like SPI. regmap_async_complete() can be called to ensure that all
1457 * asynchrnous writes have been completed.
1459 * A value of zero will be returned on success, a negative errno will
1460 * be returned in error cases.
1462 int regmap_raw_write_async(struct regmap *map, unsigned int reg,
1463 const void *val, size_t val_len)
1467 if (val_len % map->format.val_bytes)
1469 if (reg % map->reg_stride)
1472 map->lock(map->lock_arg);
1474 ret = _regmap_raw_write(map, reg, val, val_len, true);
1476 map->unlock(map->lock_arg);
1480 EXPORT_SYMBOL_GPL(regmap_raw_write_async);
1482 static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
1483 unsigned int val_len)
1485 struct regmap_range_node *range;
1486 u8 *u8 = map->work_buf;
1491 range = _regmap_range_lookup(map, reg);
1493 ret = _regmap_select_page(map, ®, range,
1494 val_len / map->format.val_bytes);
1499 map->format.format_reg(map->work_buf, reg, map->reg_shift);
1502 * Some buses or devices flag reads by setting the high bits in the
1503 * register addresss; since it's always the high bits for all
1504 * current formats we can do this here rather than in
1505 * formatting. This may break if we get interesting formats.
1507 u8[0] |= map->read_flag_mask;
1509 trace_regmap_hw_read_start(map->dev, reg,
1510 val_len / map->format.val_bytes);
1512 ret = map->bus->read(map->bus_context, map->work_buf,
1513 map->format.reg_bytes + map->format.pad_bytes,
1516 trace_regmap_hw_read_done(map->dev, reg,
1517 val_len / map->format.val_bytes);
1522 static int _regmap_bus_read(void *context, unsigned int reg,
1526 struct regmap *map = context;
1528 if (!map->format.parse_val)
1531 ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
1533 *val = map->format.parse_val(map->work_buf);
1538 static int _regmap_read(struct regmap *map, unsigned int reg,
1542 void *context = _regmap_map_get_context(map);
1544 WARN_ON(!map->reg_read);
1546 if (!map->cache_bypass) {
1547 ret = regcache_read(map, reg, val);
1552 if (map->cache_only)
1555 ret = map->reg_read(context, reg, val);
1558 if (strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
1559 dev_info(map->dev, "%x => %x\n", reg, *val);
1562 trace_regmap_reg_read(map->dev, reg, *val);
1564 if (!map->cache_bypass)
1565 regcache_write(map, reg, *val);
1572 * regmap_read(): Read a value from a single register
1574 * @map: Register map to write to
1575 * @reg: Register to be read from
1576 * @val: Pointer to store read value
1578 * A value of zero will be returned on success, a negative errno will
1579 * be returned in error cases.
1581 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
1585 if (reg % map->reg_stride)
1588 map->lock(map->lock_arg);
1590 ret = _regmap_read(map, reg, val);
1592 map->unlock(map->lock_arg);
1596 EXPORT_SYMBOL_GPL(regmap_read);
1599 * regmap_raw_read(): Read raw data from the device
1601 * @map: Register map to write to
1602 * @reg: First register to be read from
1603 * @val: Pointer to store read value
1604 * @val_len: Size of data to read
1606 * A value of zero will be returned on success, a negative errno will
1607 * be returned in error cases.
1609 int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
1612 size_t val_bytes = map->format.val_bytes;
1613 size_t val_count = val_len / val_bytes;
1619 if (val_len % map->format.val_bytes)
1621 if (reg % map->reg_stride)
1624 map->lock(map->lock_arg);
1626 if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
1627 map->cache_type == REGCACHE_NONE) {
1628 /* Physical block read if there's no cache involved */
1629 ret = _regmap_raw_read(map, reg, val, val_len);
1632 /* Otherwise go word by word for the cache; should be low
1633 * cost as we expect to hit the cache.
1635 for (i = 0; i < val_count; i++) {
1636 ret = _regmap_read(map, reg + (i * map->reg_stride),
1641 map->format.format_val(val + (i * val_bytes), v, 0);
1646 map->unlock(map->lock_arg);
1650 EXPORT_SYMBOL_GPL(regmap_raw_read);
1653 * regmap_field_read(): Read a value to a single register field
1655 * @field: Register field to read from
1656 * @val: Pointer to store read value
1658 * A value of zero will be returned on success, a negative errno will
1659 * be returned in error cases.
1661 int regmap_field_read(struct regmap_field *field, unsigned int *val)
1664 unsigned int reg_val;
1665 ret = regmap_read(field->regmap, field->reg, ®_val);
1669 reg_val &= field->mask;
1670 reg_val >>= field->shift;
1675 EXPORT_SYMBOL_GPL(regmap_field_read);
1678 * regmap_bulk_read(): Read multiple registers from the device
1680 * @map: Register map to write to
1681 * @reg: First register to be read from
1682 * @val: Pointer to store read value, in native register size for device
1683 * @val_count: Number of registers to read
1685 * A value of zero will be returned on success, a negative errno will
1686 * be returned in error cases.
1688 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
1692 size_t val_bytes = map->format.val_bytes;
1693 bool vol = regmap_volatile_range(map, reg, val_count);
1697 if (!map->format.parse_inplace)
1699 if (reg % map->reg_stride)
1702 if (vol || map->cache_type == REGCACHE_NONE) {
1704 * Some devices does not support bulk read, for
1705 * them we have a series of single read operations.
1707 if (map->use_single_rw) {
1708 for (i = 0; i < val_count; i++) {
1709 ret = regmap_raw_read(map,
1710 reg + (i * map->reg_stride),
1711 val + (i * val_bytes),
1717 ret = regmap_raw_read(map, reg, val,
1718 val_bytes * val_count);
1723 for (i = 0; i < val_count * val_bytes; i += val_bytes)
1724 map->format.parse_inplace(val + i);
1726 for (i = 0; i < val_count; i++) {
1728 ret = regmap_read(map, reg + (i * map->reg_stride),
1732 memcpy(val + (i * val_bytes), &ival, val_bytes);
1738 EXPORT_SYMBOL_GPL(regmap_bulk_read);
1740 static int _regmap_update_bits(struct regmap *map, unsigned int reg,
1741 unsigned int mask, unsigned int val,
1745 unsigned int tmp, orig;
1747 ret = _regmap_read(map, reg, &orig);
1755 ret = _regmap_write(map, reg, tmp);
1765 * regmap_update_bits: Perform a read/modify/write cycle on the register map
1767 * @map: Register map to update
1768 * @reg: Register to update
1769 * @mask: Bitmask to change
1770 * @val: New value for bitmask
1772 * Returns zero for success, a negative number on error.
1774 int regmap_update_bits(struct regmap *map, unsigned int reg,
1775 unsigned int mask, unsigned int val)
1780 map->lock(map->lock_arg);
1781 ret = _regmap_update_bits(map, reg, mask, val, &change);
1782 map->unlock(map->lock_arg);
1786 EXPORT_SYMBOL_GPL(regmap_update_bits);
1789 * regmap_update_bits_check: Perform a read/modify/write cycle on the
1790 * register map and report if updated
1792 * @map: Register map to update
1793 * @reg: Register to update
1794 * @mask: Bitmask to change
1795 * @val: New value for bitmask
1796 * @change: Boolean indicating if a write was done
1798 * Returns zero for success, a negative number on error.
1800 int regmap_update_bits_check(struct regmap *map, unsigned int reg,
1801 unsigned int mask, unsigned int val,
1806 map->lock(map->lock_arg);
1807 ret = _regmap_update_bits(map, reg, mask, val, change);
1808 map->unlock(map->lock_arg);
1811 EXPORT_SYMBOL_GPL(regmap_update_bits_check);
1813 void regmap_async_complete_cb(struct regmap_async *async, int ret)
1815 struct regmap *map = async->map;
1818 trace_regmap_async_io_complete(map->dev);
1820 spin_lock(&map->async_lock);
1822 list_del(&async->list);
1823 wake = list_empty(&map->async_list);
1826 map->async_ret = ret;
1828 spin_unlock(&map->async_lock);
1830 schedule_work(&async->cleanup);
1833 wake_up(&map->async_waitq);
1835 EXPORT_SYMBOL_GPL(regmap_async_complete_cb);
1837 static int regmap_async_is_done(struct regmap *map)
1839 unsigned long flags;
1842 spin_lock_irqsave(&map->async_lock, flags);
1843 ret = list_empty(&map->async_list);
1844 spin_unlock_irqrestore(&map->async_lock, flags);
1850 * regmap_async_complete: Ensure all asynchronous I/O has completed.
1852 * @map: Map to operate on.
1854 * Blocks until any pending asynchronous I/O has completed. Returns
1855 * an error code for any failed I/O operations.
1857 int regmap_async_complete(struct regmap *map)
1859 unsigned long flags;
1862 /* Nothing to do with no async support */
1863 if (!map->bus || !map->bus->async_write)
1866 trace_regmap_async_complete_start(map->dev);
1868 wait_event(map->async_waitq, regmap_async_is_done(map));
1870 spin_lock_irqsave(&map->async_lock, flags);
1871 ret = map->async_ret;
1873 spin_unlock_irqrestore(&map->async_lock, flags);
1875 trace_regmap_async_complete_done(map->dev);
1879 EXPORT_SYMBOL_GPL(regmap_async_complete);
1882 * regmap_register_patch: Register and apply register updates to be applied
1883 * on device initialistion
1885 * @map: Register map to apply updates to.
1886 * @regs: Values to update.
1887 * @num_regs: Number of entries in regs.
1889 * Register a set of register updates to be applied to the device
1890 * whenever the device registers are synchronised with the cache and
1891 * apply them immediately. Typically this is used to apply
1892 * corrections to be applied to the device defaults on startup, such
1893 * as the updates some vendors provide to undocumented registers.
1895 int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
1898 struct reg_default *p;
1902 map->lock(map->lock_arg);
1904 bypass = map->cache_bypass;
1906 map->cache_bypass = true;
1908 /* Write out first; it's useful to apply even if we fail later. */
1909 for (i = 0; i < num_regs; i++) {
1910 ret = _regmap_write(map, regs[i].reg, regs[i].def);
1912 dev_err(map->dev, "Failed to write %x = %x: %d\n",
1913 regs[i].reg, regs[i].def, ret);
1918 p = krealloc(map->patch,
1919 sizeof(struct reg_default) * (map->patch_regs + num_regs),
1922 memcpy(p + map->patch_regs, regs, num_regs * sizeof(*regs));
1924 map->patch_regs += num_regs;
1930 map->cache_bypass = bypass;
1932 map->unlock(map->lock_arg);
1936 EXPORT_SYMBOL_GPL(regmap_register_patch);
1939 * regmap_get_val_bytes(): Report the size of a register value
1941 * Report the size of a register value, mainly intended to for use by
1942 * generic infrastructure built on top of regmap.
1944 int regmap_get_val_bytes(struct regmap *map)
1946 if (map->format.format_write)
1949 return map->format.val_bytes;
1951 EXPORT_SYMBOL_GPL(regmap_get_val_bytes);
1953 static int __init regmap_initcall(void)
1955 regmap_debugfs_initcall();
1959 postcore_initcall(regmap_initcall);