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>
20 #define CREATE_TRACE_POINTS
21 #include <trace/events/regmap.h>
26 * Sometimes for failures during very early init the trace
27 * infrastructure isn't available early enough to be used. For this
28 * sort of problem defining LOG_DEVICE will add printks for basic
29 * register I/O on a specific device.
33 static int _regmap_update_bits(struct regmap *map, unsigned int reg,
34 unsigned int mask, unsigned int val,
37 bool regmap_reg_in_ranges(unsigned int reg,
38 const struct regmap_range *ranges,
41 const struct regmap_range *r;
44 for (i = 0, r = ranges; i < nranges; i++, r++)
45 if (regmap_reg_in_range(reg, r))
49 EXPORT_SYMBOL_GPL(regmap_reg_in_ranges);
51 static bool _regmap_check_range_table(struct regmap *map,
53 const struct regmap_access_table *table)
55 /* Check "no ranges" first */
56 if (regmap_reg_in_ranges(reg, table->no_ranges, table->n_no_ranges))
59 /* In case zero "yes ranges" are supplied, any reg is OK */
60 if (!table->n_yes_ranges)
63 return regmap_reg_in_ranges(reg, table->yes_ranges,
67 bool regmap_writeable(struct regmap *map, unsigned int reg)
69 if (map->max_register && reg > map->max_register)
72 if (map->writeable_reg)
73 return map->writeable_reg(map->dev, reg);
76 return _regmap_check_range_table(map, reg, map->wr_table);
81 bool regmap_readable(struct regmap *map, unsigned int reg)
83 if (map->max_register && reg > map->max_register)
86 if (map->format.format_write)
89 if (map->readable_reg)
90 return map->readable_reg(map->dev, reg);
93 return _regmap_check_range_table(map, reg, map->rd_table);
98 bool regmap_volatile(struct regmap *map, unsigned int reg)
100 if (!regmap_readable(map, reg))
103 if (map->volatile_reg)
104 return map->volatile_reg(map->dev, reg);
106 if (map->volatile_table)
107 return _regmap_check_range_table(map, reg, map->volatile_table);
112 bool regmap_precious(struct regmap *map, unsigned int reg)
114 if (!regmap_readable(map, reg))
117 if (map->precious_reg)
118 return map->precious_reg(map->dev, reg);
120 if (map->precious_table)
121 return _regmap_check_range_table(map, reg, map->precious_table);
126 static bool regmap_volatile_range(struct regmap *map, unsigned int reg,
131 for (i = 0; i < num; i++)
132 if (!regmap_volatile(map, reg + i))
138 static void regmap_format_2_6_write(struct regmap *map,
139 unsigned int reg, unsigned int val)
141 u8 *out = map->work_buf;
143 *out = (reg << 6) | val;
146 static void regmap_format_4_12_write(struct regmap *map,
147 unsigned int reg, unsigned int val)
149 __be16 *out = map->work_buf;
150 *out = cpu_to_be16((reg << 12) | val);
153 static void regmap_format_7_9_write(struct regmap *map,
154 unsigned int reg, unsigned int val)
156 __be16 *out = map->work_buf;
157 *out = cpu_to_be16((reg << 9) | val);
160 static void regmap_format_10_14_write(struct regmap *map,
161 unsigned int reg, unsigned int val)
163 u8 *out = map->work_buf;
166 out[1] = (val >> 8) | (reg << 6);
170 static void regmap_format_8(void *buf, unsigned int val, unsigned int shift)
177 static void regmap_format_16_be(void *buf, unsigned int val, unsigned int shift)
181 b[0] = cpu_to_be16(val << shift);
184 static void regmap_format_16_native(void *buf, unsigned int val,
187 *(u16 *)buf = val << shift;
190 static void regmap_format_24(void *buf, unsigned int val, unsigned int shift)
201 static void regmap_format_32_be(void *buf, unsigned int val, unsigned int shift)
205 b[0] = cpu_to_be32(val << shift);
208 static void regmap_format_32_native(void *buf, unsigned int val,
211 *(u32 *)buf = val << shift;
214 static unsigned int regmap_parse_8(void *buf)
221 static unsigned int regmap_parse_16_be(void *buf)
225 b[0] = be16_to_cpu(b[0]);
230 static unsigned int regmap_parse_16_native(void *buf)
235 static unsigned int regmap_parse_24(void *buf)
238 unsigned int ret = b[2];
239 ret |= ((unsigned int)b[1]) << 8;
240 ret |= ((unsigned int)b[0]) << 16;
245 static unsigned int regmap_parse_32_be(void *buf)
249 b[0] = be32_to_cpu(b[0]);
254 static unsigned int regmap_parse_32_native(void *buf)
259 static void regmap_lock_mutex(void *__map)
261 struct regmap *map = __map;
262 mutex_lock(&map->mutex);
265 static void regmap_unlock_mutex(void *__map)
267 struct regmap *map = __map;
268 mutex_unlock(&map->mutex);
271 static void regmap_lock_spinlock(void *__map)
273 struct regmap *map = __map;
274 spin_lock(&map->spinlock);
277 static void regmap_unlock_spinlock(void *__map)
279 struct regmap *map = __map;
280 spin_unlock(&map->spinlock);
283 static void dev_get_regmap_release(struct device *dev, void *res)
286 * We don't actually have anything to do here; the goal here
287 * is not to manage the regmap but to provide a simple way to
288 * get the regmap back given a struct device.
292 static bool _regmap_range_add(struct regmap *map,
293 struct regmap_range_node *data)
295 struct rb_root *root = &map->range_tree;
296 struct rb_node **new = &(root->rb_node), *parent = NULL;
299 struct regmap_range_node *this =
300 container_of(*new, struct regmap_range_node, node);
303 if (data->range_max < this->range_min)
304 new = &((*new)->rb_left);
305 else if (data->range_min > this->range_max)
306 new = &((*new)->rb_right);
311 rb_link_node(&data->node, parent, new);
312 rb_insert_color(&data->node, root);
317 static struct regmap_range_node *_regmap_range_lookup(struct regmap *map,
320 struct rb_node *node = map->range_tree.rb_node;
323 struct regmap_range_node *this =
324 container_of(node, struct regmap_range_node, node);
326 if (reg < this->range_min)
327 node = node->rb_left;
328 else if (reg > this->range_max)
329 node = node->rb_right;
337 static void regmap_range_exit(struct regmap *map)
339 struct rb_node *next;
340 struct regmap_range_node *range_node;
342 next = rb_first(&map->range_tree);
344 range_node = rb_entry(next, struct regmap_range_node, node);
345 next = rb_next(&range_node->node);
346 rb_erase(&range_node->node, &map->range_tree);
350 kfree(map->selector_work_buf);
354 * regmap_init(): Initialise register map
356 * @dev: Device that will be interacted with
357 * @bus: Bus-specific callbacks to use with device
358 * @bus_context: Data passed to bus-specific callbacks
359 * @config: Configuration for register map
361 * The return value will be an ERR_PTR() on error or a valid pointer to
362 * a struct regmap. This function should generally not be called
363 * directly, it should be called by bus-specific init functions.
365 struct regmap *regmap_init(struct device *dev,
366 const struct regmap_bus *bus,
368 const struct regmap_config *config)
370 struct regmap *map, **m;
372 enum regmap_endian reg_endian, val_endian;
378 map = kzalloc(sizeof(*map), GFP_KERNEL);
384 if (config->lock && config->unlock) {
385 map->lock = config->lock;
386 map->unlock = config->unlock;
387 map->lock_arg = config->lock_arg;
390 spin_lock_init(&map->spinlock);
391 map->lock = regmap_lock_spinlock;
392 map->unlock = regmap_unlock_spinlock;
394 mutex_init(&map->mutex);
395 map->lock = regmap_lock_mutex;
396 map->unlock = regmap_unlock_mutex;
400 map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8);
401 map->format.pad_bytes = config->pad_bits / 8;
402 map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8);
403 map->format.buf_size = DIV_ROUND_UP(config->reg_bits +
404 config->val_bits + config->pad_bits, 8);
405 map->reg_shift = config->pad_bits % 8;
406 if (config->reg_stride)
407 map->reg_stride = config->reg_stride;
410 map->use_single_rw = config->use_single_rw;
413 map->bus_context = bus_context;
414 map->max_register = config->max_register;
415 map->wr_table = config->wr_table;
416 map->rd_table = config->rd_table;
417 map->volatile_table = config->volatile_table;
418 map->precious_table = config->precious_table;
419 map->writeable_reg = config->writeable_reg;
420 map->readable_reg = config->readable_reg;
421 map->volatile_reg = config->volatile_reg;
422 map->precious_reg = config->precious_reg;
423 map->cache_type = config->cache_type;
424 map->name = config->name;
426 if (config->read_flag_mask || config->write_flag_mask) {
427 map->read_flag_mask = config->read_flag_mask;
428 map->write_flag_mask = config->write_flag_mask;
430 map->read_flag_mask = bus->read_flag_mask;
433 reg_endian = config->reg_format_endian;
434 if (reg_endian == REGMAP_ENDIAN_DEFAULT)
435 reg_endian = bus->reg_format_endian_default;
436 if (reg_endian == REGMAP_ENDIAN_DEFAULT)
437 reg_endian = REGMAP_ENDIAN_BIG;
439 val_endian = config->val_format_endian;
440 if (val_endian == REGMAP_ENDIAN_DEFAULT)
441 val_endian = bus->val_format_endian_default;
442 if (val_endian == REGMAP_ENDIAN_DEFAULT)
443 val_endian = REGMAP_ENDIAN_BIG;
445 switch (config->reg_bits + map->reg_shift) {
447 switch (config->val_bits) {
449 map->format.format_write = regmap_format_2_6_write;
457 switch (config->val_bits) {
459 map->format.format_write = regmap_format_4_12_write;
467 switch (config->val_bits) {
469 map->format.format_write = regmap_format_7_9_write;
477 switch (config->val_bits) {
479 map->format.format_write = regmap_format_10_14_write;
487 map->format.format_reg = regmap_format_8;
491 switch (reg_endian) {
492 case REGMAP_ENDIAN_BIG:
493 map->format.format_reg = regmap_format_16_be;
495 case REGMAP_ENDIAN_NATIVE:
496 map->format.format_reg = regmap_format_16_native;
504 switch (reg_endian) {
505 case REGMAP_ENDIAN_BIG:
506 map->format.format_reg = regmap_format_32_be;
508 case REGMAP_ENDIAN_NATIVE:
509 map->format.format_reg = regmap_format_32_native;
520 switch (config->val_bits) {
522 map->format.format_val = regmap_format_8;
523 map->format.parse_val = regmap_parse_8;
526 switch (val_endian) {
527 case REGMAP_ENDIAN_BIG:
528 map->format.format_val = regmap_format_16_be;
529 map->format.parse_val = regmap_parse_16_be;
531 case REGMAP_ENDIAN_NATIVE:
532 map->format.format_val = regmap_format_16_native;
533 map->format.parse_val = regmap_parse_16_native;
540 if (val_endian != REGMAP_ENDIAN_BIG)
542 map->format.format_val = regmap_format_24;
543 map->format.parse_val = regmap_parse_24;
546 switch (val_endian) {
547 case REGMAP_ENDIAN_BIG:
548 map->format.format_val = regmap_format_32_be;
549 map->format.parse_val = regmap_parse_32_be;
551 case REGMAP_ENDIAN_NATIVE:
552 map->format.format_val = regmap_format_32_native;
553 map->format.parse_val = regmap_parse_32_native;
561 if (map->format.format_write) {
562 if ((reg_endian != REGMAP_ENDIAN_BIG) ||
563 (val_endian != REGMAP_ENDIAN_BIG))
565 map->use_single_rw = true;
568 if (!map->format.format_write &&
569 !(map->format.format_reg && map->format.format_val))
572 map->work_buf = kzalloc(map->format.buf_size, GFP_KERNEL);
573 if (map->work_buf == NULL) {
578 map->range_tree = RB_ROOT;
579 for (i = 0; i < config->num_ranges; i++) {
580 const struct regmap_range_cfg *range_cfg = &config->ranges[i];
581 struct regmap_range_node *new;
584 if (range_cfg->range_max < range_cfg->range_min) {
585 dev_err(map->dev, "Invalid range %d: %d < %d\n", i,
586 range_cfg->range_max, range_cfg->range_min);
590 if (range_cfg->range_max > map->max_register) {
591 dev_err(map->dev, "Invalid range %d: %d > %d\n", i,
592 range_cfg->range_max, map->max_register);
596 if (range_cfg->selector_reg > map->max_register) {
598 "Invalid range %d: selector out of map\n", i);
602 if (range_cfg->window_len == 0) {
603 dev_err(map->dev, "Invalid range %d: window_len 0\n",
608 /* Make sure, that this register range has no selector
609 or data window within its boundary */
610 for (j = 0; j < config->num_ranges; j++) {
611 unsigned sel_reg = config->ranges[j].selector_reg;
612 unsigned win_min = config->ranges[j].window_start;
613 unsigned win_max = win_min +
614 config->ranges[j].window_len - 1;
616 if (range_cfg->range_min <= sel_reg &&
617 sel_reg <= range_cfg->range_max) {
619 "Range %d: selector for %d in window\n",
624 if (!(win_max < range_cfg->range_min ||
625 win_min > range_cfg->range_max)) {
627 "Range %d: window for %d in window\n",
633 new = kzalloc(sizeof(*new), GFP_KERNEL);
640 new->name = range_cfg->name;
641 new->range_min = range_cfg->range_min;
642 new->range_max = range_cfg->range_max;
643 new->selector_reg = range_cfg->selector_reg;
644 new->selector_mask = range_cfg->selector_mask;
645 new->selector_shift = range_cfg->selector_shift;
646 new->window_start = range_cfg->window_start;
647 new->window_len = range_cfg->window_len;
649 if (_regmap_range_add(map, new) == false) {
650 dev_err(map->dev, "Failed to add range %d\n", i);
655 if (map->selector_work_buf == NULL) {
656 map->selector_work_buf =
657 kzalloc(map->format.buf_size, GFP_KERNEL);
658 if (map->selector_work_buf == NULL) {
665 ret = regcache_init(map, config);
669 regmap_debugfs_init(map, config->name);
671 /* Add a devres resource for dev_get_regmap() */
672 m = devres_alloc(dev_get_regmap_release, sizeof(*m), GFP_KERNEL);
683 regmap_debugfs_exit(map);
686 regmap_range_exit(map);
687 kfree(map->work_buf);
693 EXPORT_SYMBOL_GPL(regmap_init);
695 static void devm_regmap_release(struct device *dev, void *res)
697 regmap_exit(*(struct regmap **)res);
701 * devm_regmap_init(): Initialise managed register map
703 * @dev: Device that will be interacted with
704 * @bus: Bus-specific callbacks to use with device
705 * @bus_context: Data passed to bus-specific callbacks
706 * @config: Configuration for register map
708 * The return value will be an ERR_PTR() on error or a valid pointer
709 * to a struct regmap. This function should generally not be called
710 * directly, it should be called by bus-specific init functions. The
711 * map will be automatically freed by the device management code.
713 struct regmap *devm_regmap_init(struct device *dev,
714 const struct regmap_bus *bus,
716 const struct regmap_config *config)
718 struct regmap **ptr, *regmap;
720 ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL);
722 return ERR_PTR(-ENOMEM);
724 regmap = regmap_init(dev, bus, bus_context, config);
725 if (!IS_ERR(regmap)) {
727 devres_add(dev, ptr);
734 EXPORT_SYMBOL_GPL(devm_regmap_init);
737 * regmap_reinit_cache(): Reinitialise the current register cache
739 * @map: Register map to operate on.
740 * @config: New configuration. Only the cache data will be used.
742 * Discard any existing register cache for the map and initialize a
743 * new cache. This can be used to restore the cache to defaults or to
744 * update the cache configuration to reflect runtime discovery of the
747 * No explicit locking is done here, the user needs to ensure that
748 * this function will not race with other calls to regmap.
750 int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
753 regmap_debugfs_exit(map);
755 map->max_register = config->max_register;
756 map->writeable_reg = config->writeable_reg;
757 map->readable_reg = config->readable_reg;
758 map->volatile_reg = config->volatile_reg;
759 map->precious_reg = config->precious_reg;
760 map->cache_type = config->cache_type;
762 regmap_debugfs_init(map, config->name);
764 map->cache_bypass = false;
765 map->cache_only = false;
767 return regcache_init(map, config);
769 EXPORT_SYMBOL_GPL(regmap_reinit_cache);
772 * regmap_exit(): Free a previously allocated register map
774 void regmap_exit(struct regmap *map)
777 regmap_debugfs_exit(map);
778 regmap_range_exit(map);
779 if (map->bus->free_context)
780 map->bus->free_context(map->bus_context);
781 kfree(map->work_buf);
784 EXPORT_SYMBOL_GPL(regmap_exit);
786 static int dev_get_regmap_match(struct device *dev, void *res, void *data)
788 struct regmap **r = res;
794 /* If the user didn't specify a name match any */
796 return (*r)->name == data;
802 * dev_get_regmap(): Obtain the regmap (if any) for a device
804 * @dev: Device to retrieve the map for
805 * @name: Optional name for the register map, usually NULL.
807 * Returns the regmap for the device if one is present, or NULL. If
808 * name is specified then it must match the name specified when
809 * registering the device, if it is NULL then the first regmap found
810 * will be used. Devices with multiple register maps are very rare,
811 * generic code should normally not need to specify a name.
813 struct regmap *dev_get_regmap(struct device *dev, const char *name)
815 struct regmap **r = devres_find(dev, dev_get_regmap_release,
816 dev_get_regmap_match, (void *)name);
822 EXPORT_SYMBOL_GPL(dev_get_regmap);
824 static int _regmap_select_page(struct regmap *map, unsigned int *reg,
825 struct regmap_range_node *range,
826 unsigned int val_num)
829 unsigned int win_offset;
830 unsigned int win_page;
834 win_offset = (*reg - range->range_min) % range->window_len;
835 win_page = (*reg - range->range_min) / range->window_len;
838 /* Bulk write shouldn't cross range boundary */
839 if (*reg + val_num - 1 > range->range_max)
842 /* ... or single page boundary */
843 if (val_num > range->window_len - win_offset)
847 /* It is possible to have selector register inside data window.
848 In that case, selector register is located on every page and
849 it needs no page switching, when accessed alone. */
851 range->window_start + win_offset != range->selector_reg) {
852 /* Use separate work_buf during page switching */
853 orig_work_buf = map->work_buf;
854 map->work_buf = map->selector_work_buf;
856 ret = _regmap_update_bits(map, range->selector_reg,
857 range->selector_mask,
858 win_page << range->selector_shift,
861 map->work_buf = orig_work_buf;
867 *reg = range->window_start + win_offset;
872 static int _regmap_raw_write(struct regmap *map, unsigned int reg,
873 const void *val, size_t val_len)
875 struct regmap_range_node *range;
876 u8 *u8 = map->work_buf;
882 /* Check for unwritable registers before we start */
883 if (map->writeable_reg)
884 for (i = 0; i < val_len / map->format.val_bytes; i++)
885 if (!map->writeable_reg(map->dev,
886 reg + (i * map->reg_stride)))
889 if (!map->cache_bypass && map->format.parse_val) {
891 int val_bytes = map->format.val_bytes;
892 for (i = 0; i < val_len / val_bytes; i++) {
893 memcpy(map->work_buf, val + (i * val_bytes), val_bytes);
894 ival = map->format.parse_val(map->work_buf);
895 ret = regcache_write(map, reg + (i * map->reg_stride),
899 "Error in caching of register: %x ret: %d\n",
904 if (map->cache_only) {
905 map->cache_dirty = true;
910 range = _regmap_range_lookup(map, reg);
912 int val_num = val_len / map->format.val_bytes;
913 int win_offset = (reg - range->range_min) % range->window_len;
914 int win_residue = range->window_len - win_offset;
916 /* If the write goes beyond the end of the window split it */
917 while (val_num > win_residue) {
918 dev_dbg(map->dev, "Writing window %d/%zu\n",
919 win_residue, val_len / map->format.val_bytes);
920 ret = _regmap_raw_write(map, reg, val, win_residue *
921 map->format.val_bytes);
926 val_num -= win_residue;
927 val += win_residue * map->format.val_bytes;
928 val_len -= win_residue * map->format.val_bytes;
930 win_offset = (reg - range->range_min) %
932 win_residue = range->window_len - win_offset;
935 ret = _regmap_select_page(map, ®, range, val_num);
940 map->format.format_reg(map->work_buf, reg, map->reg_shift);
942 u8[0] |= map->write_flag_mask;
944 trace_regmap_hw_write_start(map->dev, reg,
945 val_len / map->format.val_bytes);
947 /* If we're doing a single register write we can probably just
948 * send the work_buf directly, otherwise try to do a gather
951 if (val == (map->work_buf + map->format.pad_bytes +
952 map->format.reg_bytes))
953 ret = map->bus->write(map->bus_context, map->work_buf,
954 map->format.reg_bytes +
955 map->format.pad_bytes +
957 else if (map->bus->gather_write)
958 ret = map->bus->gather_write(map->bus_context, map->work_buf,
959 map->format.reg_bytes +
960 map->format.pad_bytes,
963 /* If that didn't work fall back on linearising by hand. */
964 if (ret == -ENOTSUPP) {
965 len = map->format.reg_bytes + map->format.pad_bytes + val_len;
966 buf = kzalloc(len, GFP_KERNEL);
970 memcpy(buf, map->work_buf, map->format.reg_bytes);
971 memcpy(buf + map->format.reg_bytes + map->format.pad_bytes,
973 ret = map->bus->write(map->bus_context, buf, len);
978 trace_regmap_hw_write_done(map->dev, reg,
979 val_len / map->format.val_bytes);
984 int _regmap_write(struct regmap *map, unsigned int reg,
987 struct regmap_range_node *range;
989 BUG_ON(!map->format.format_write && !map->format.format_val);
991 if (!map->cache_bypass && map->format.format_write) {
992 ret = regcache_write(map, reg, val);
995 if (map->cache_only) {
996 map->cache_dirty = true;
1002 if (strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
1003 dev_info(map->dev, "%x <= %x\n", reg, val);
1006 trace_regmap_reg_write(map->dev, reg, val);
1008 if (map->format.format_write) {
1009 range = _regmap_range_lookup(map, reg);
1011 ret = _regmap_select_page(map, ®, range, 1);
1016 map->format.format_write(map, reg, val);
1018 trace_regmap_hw_write_start(map->dev, reg, 1);
1020 ret = map->bus->write(map->bus_context, map->work_buf,
1021 map->format.buf_size);
1023 trace_regmap_hw_write_done(map->dev, reg, 1);
1027 map->format.format_val(map->work_buf + map->format.reg_bytes
1028 + map->format.pad_bytes, val, 0);
1029 return _regmap_raw_write(map, reg,
1031 map->format.reg_bytes +
1032 map->format.pad_bytes,
1033 map->format.val_bytes);
1038 * regmap_write(): Write a value to a single register
1040 * @map: Register map to write to
1041 * @reg: Register to write to
1042 * @val: Value to be written
1044 * A value of zero will be returned on success, a negative errno will
1045 * be returned in error cases.
1047 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
1051 if (reg % map->reg_stride)
1054 map->lock(map->lock_arg);
1056 ret = _regmap_write(map, reg, val);
1058 map->unlock(map->lock_arg);
1062 EXPORT_SYMBOL_GPL(regmap_write);
1065 * regmap_raw_write(): Write raw values to one or more registers
1067 * @map: Register map to write to
1068 * @reg: Initial register to write to
1069 * @val: Block of data to be written, laid out for direct transmission to the
1071 * @val_len: Length of data pointed to by val.
1073 * This function is intended to be used for things like firmware
1074 * download where a large block of data needs to be transferred to the
1075 * device. No formatting will be done on the data provided.
1077 * A value of zero will be returned on success, a negative errno will
1078 * be returned in error cases.
1080 int regmap_raw_write(struct regmap *map, unsigned int reg,
1081 const void *val, size_t val_len)
1085 if (val_len % map->format.val_bytes)
1087 if (reg % map->reg_stride)
1090 map->lock(map->lock_arg);
1092 ret = _regmap_raw_write(map, reg, val, val_len);
1094 map->unlock(map->lock_arg);
1098 EXPORT_SYMBOL_GPL(regmap_raw_write);
1101 * regmap_bulk_write(): Write multiple registers to the device
1103 * @map: Register map to write to
1104 * @reg: First register to be write from
1105 * @val: Block of data to be written, in native register size for device
1106 * @val_count: Number of registers to write
1108 * This function is intended to be used for writing a large block of
1109 * data to be device either in single transfer or multiple transfer.
1111 * A value of zero will be returned on success, a negative errno will
1112 * be returned in error cases.
1114 int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
1118 size_t val_bytes = map->format.val_bytes;
1121 if (!map->format.parse_val)
1123 if (reg % map->reg_stride)
1126 map->lock(map->lock_arg);
1128 /* No formatting is require if val_byte is 1 */
1129 if (val_bytes == 1) {
1132 wval = kmemdup(val, val_count * val_bytes, GFP_KERNEL);
1135 dev_err(map->dev, "Error in memory allocation\n");
1138 for (i = 0; i < val_count * val_bytes; i += val_bytes)
1139 map->format.parse_val(wval + i);
1142 * Some devices does not support bulk write, for
1143 * them we have a series of single write operations.
1145 if (map->use_single_rw) {
1146 for (i = 0; i < val_count; i++) {
1147 ret = regmap_raw_write(map,
1148 reg + (i * map->reg_stride),
1149 val + (i * val_bytes),
1155 ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count);
1162 map->unlock(map->lock_arg);
1165 EXPORT_SYMBOL_GPL(regmap_bulk_write);
1167 static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
1168 unsigned int val_len)
1170 struct regmap_range_node *range;
1171 u8 *u8 = map->work_buf;
1174 range = _regmap_range_lookup(map, reg);
1176 ret = _regmap_select_page(map, ®, range,
1177 val_len / map->format.val_bytes);
1182 map->format.format_reg(map->work_buf, reg, map->reg_shift);
1185 * Some buses or devices flag reads by setting the high bits in the
1186 * register addresss; since it's always the high bits for all
1187 * current formats we can do this here rather than in
1188 * formatting. This may break if we get interesting formats.
1190 u8[0] |= map->read_flag_mask;
1192 trace_regmap_hw_read_start(map->dev, reg,
1193 val_len / map->format.val_bytes);
1195 ret = map->bus->read(map->bus_context, map->work_buf,
1196 map->format.reg_bytes + map->format.pad_bytes,
1199 trace_regmap_hw_read_done(map->dev, reg,
1200 val_len / map->format.val_bytes);
1205 static int _regmap_read(struct regmap *map, unsigned int reg,
1210 if (!map->cache_bypass) {
1211 ret = regcache_read(map, reg, val);
1216 if (!map->format.parse_val)
1219 if (map->cache_only)
1222 ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
1224 *val = map->format.parse_val(map->work_buf);
1227 if (strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
1228 dev_info(map->dev, "%x => %x\n", reg, *val);
1231 trace_regmap_reg_read(map->dev, reg, *val);
1234 if (ret == 0 && !map->cache_bypass)
1235 regcache_write(map, reg, *val);
1241 * regmap_read(): Read a value from a single register
1243 * @map: Register map to write to
1244 * @reg: Register to be read from
1245 * @val: Pointer to store read value
1247 * A value of zero will be returned on success, a negative errno will
1248 * be returned in error cases.
1250 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
1254 if (reg % map->reg_stride)
1257 map->lock(map->lock_arg);
1259 ret = _regmap_read(map, reg, val);
1261 map->unlock(map->lock_arg);
1265 EXPORT_SYMBOL_GPL(regmap_read);
1268 * regmap_raw_read(): Read raw data from the device
1270 * @map: Register map to write to
1271 * @reg: First register to be read from
1272 * @val: Pointer to store read value
1273 * @val_len: Size of data to read
1275 * A value of zero will be returned on success, a negative errno will
1276 * be returned in error cases.
1278 int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
1281 size_t val_bytes = map->format.val_bytes;
1282 size_t val_count = val_len / val_bytes;
1286 if (val_len % map->format.val_bytes)
1288 if (reg % map->reg_stride)
1291 map->lock(map->lock_arg);
1293 if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
1294 map->cache_type == REGCACHE_NONE) {
1295 /* Physical block read if there's no cache involved */
1296 ret = _regmap_raw_read(map, reg, val, val_len);
1299 /* Otherwise go word by word for the cache; should be low
1300 * cost as we expect to hit the cache.
1302 for (i = 0; i < val_count; i++) {
1303 ret = _regmap_read(map, reg + (i * map->reg_stride),
1308 map->format.format_val(val + (i * val_bytes), v, 0);
1313 map->unlock(map->lock_arg);
1317 EXPORT_SYMBOL_GPL(regmap_raw_read);
1320 * regmap_bulk_read(): Read multiple registers from the device
1322 * @map: Register map to write to
1323 * @reg: First register to be read from
1324 * @val: Pointer to store read value, in native register size for device
1325 * @val_count: Number of registers to read
1327 * A value of zero will be returned on success, a negative errno will
1328 * be returned in error cases.
1330 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
1334 size_t val_bytes = map->format.val_bytes;
1335 bool vol = regmap_volatile_range(map, reg, val_count);
1337 if (!map->format.parse_val)
1339 if (reg % map->reg_stride)
1342 if (vol || map->cache_type == REGCACHE_NONE) {
1344 * Some devices does not support bulk read, for
1345 * them we have a series of single read operations.
1347 if (map->use_single_rw) {
1348 for (i = 0; i < val_count; i++) {
1349 ret = regmap_raw_read(map,
1350 reg + (i * map->reg_stride),
1351 val + (i * val_bytes),
1357 ret = regmap_raw_read(map, reg, val,
1358 val_bytes * val_count);
1363 for (i = 0; i < val_count * val_bytes; i += val_bytes)
1364 map->format.parse_val(val + i);
1366 for (i = 0; i < val_count; i++) {
1368 ret = regmap_read(map, reg + (i * map->reg_stride),
1372 memcpy(val + (i * val_bytes), &ival, val_bytes);
1378 EXPORT_SYMBOL_GPL(regmap_bulk_read);
1380 static int _regmap_update_bits(struct regmap *map, unsigned int reg,
1381 unsigned int mask, unsigned int val,
1385 unsigned int tmp, orig;
1387 ret = _regmap_read(map, reg, &orig);
1395 ret = _regmap_write(map, reg, tmp);
1405 * regmap_update_bits: Perform a read/modify/write cycle on the register map
1407 * @map: Register map to update
1408 * @reg: Register to update
1409 * @mask: Bitmask to change
1410 * @val: New value for bitmask
1412 * Returns zero for success, a negative number on error.
1414 int regmap_update_bits(struct regmap *map, unsigned int reg,
1415 unsigned int mask, unsigned int val)
1420 map->lock(map->lock_arg);
1421 ret = _regmap_update_bits(map, reg, mask, val, &change);
1422 map->unlock(map->lock_arg);
1426 EXPORT_SYMBOL_GPL(regmap_update_bits);
1429 * regmap_update_bits_check: Perform a read/modify/write cycle on the
1430 * register map and report if updated
1432 * @map: Register map to update
1433 * @reg: Register to update
1434 * @mask: Bitmask to change
1435 * @val: New value for bitmask
1436 * @change: Boolean indicating if a write was done
1438 * Returns zero for success, a negative number on error.
1440 int regmap_update_bits_check(struct regmap *map, unsigned int reg,
1441 unsigned int mask, unsigned int val,
1446 map->lock(map->lock_arg);
1447 ret = _regmap_update_bits(map, reg, mask, val, change);
1448 map->unlock(map->lock_arg);
1451 EXPORT_SYMBOL_GPL(regmap_update_bits_check);
1454 * regmap_register_patch: Register and apply register updates to be applied
1455 * on device initialistion
1457 * @map: Register map to apply updates to.
1458 * @regs: Values to update.
1459 * @num_regs: Number of entries in regs.
1461 * Register a set of register updates to be applied to the device
1462 * whenever the device registers are synchronised with the cache and
1463 * apply them immediately. Typically this is used to apply
1464 * corrections to be applied to the device defaults on startup, such
1465 * as the updates some vendors provide to undocumented registers.
1467 int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
1473 /* If needed the implementation can be extended to support this */
1477 map->lock(map->lock_arg);
1479 bypass = map->cache_bypass;
1481 map->cache_bypass = true;
1483 /* Write out first; it's useful to apply even if we fail later. */
1484 for (i = 0; i < num_regs; i++) {
1485 ret = _regmap_write(map, regs[i].reg, regs[i].def);
1487 dev_err(map->dev, "Failed to write %x = %x: %d\n",
1488 regs[i].reg, regs[i].def, ret);
1493 map->patch = kcalloc(num_regs, sizeof(struct reg_default), GFP_KERNEL);
1494 if (map->patch != NULL) {
1495 memcpy(map->patch, regs,
1496 num_regs * sizeof(struct reg_default));
1497 map->patch_regs = num_regs;
1503 map->cache_bypass = bypass;
1505 map->unlock(map->lock_arg);
1509 EXPORT_SYMBOL_GPL(regmap_register_patch);
1512 * regmap_get_val_bytes(): Report the size of a register value
1514 * Report the size of a register value, mainly intended to for use by
1515 * generic infrastructure built on top of regmap.
1517 int regmap_get_val_bytes(struct regmap *map)
1519 if (map->format.format_write)
1522 return map->format.val_bytes;
1524 EXPORT_SYMBOL_GPL(regmap_get_val_bytes);
1526 static int __init regmap_initcall(void)
1528 regmap_debugfs_initcall();
1532 postcore_initcall(regmap_initcall);