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_writeable(struct regmap *map, unsigned int reg)
39 if (map->max_register && reg > map->max_register)
42 if (map->writeable_reg)
43 return map->writeable_reg(map->dev, reg);
48 bool regmap_readable(struct regmap *map, unsigned int reg)
50 if (map->max_register && reg > map->max_register)
53 if (map->format.format_write)
56 if (map->readable_reg)
57 return map->readable_reg(map->dev, reg);
62 bool regmap_volatile(struct regmap *map, unsigned int reg)
64 if (!regmap_readable(map, reg))
67 if (map->volatile_reg)
68 return map->volatile_reg(map->dev, reg);
73 bool regmap_precious(struct regmap *map, unsigned int reg)
75 if (!regmap_readable(map, reg))
78 if (map->precious_reg)
79 return map->precious_reg(map->dev, reg);
84 static bool regmap_volatile_range(struct regmap *map, unsigned int reg,
89 for (i = 0; i < num; i++)
90 if (!regmap_volatile(map, reg + i))
96 static void regmap_format_2_6_write(struct regmap *map,
97 unsigned int reg, unsigned int val)
99 u8 *out = map->work_buf;
101 *out = (reg << 6) | val;
104 static void regmap_format_4_12_write(struct regmap *map,
105 unsigned int reg, unsigned int val)
107 __be16 *out = map->work_buf;
108 *out = cpu_to_be16((reg << 12) | val);
111 static void regmap_format_7_9_write(struct regmap *map,
112 unsigned int reg, unsigned int val)
114 __be16 *out = map->work_buf;
115 *out = cpu_to_be16((reg << 9) | val);
118 static void regmap_format_10_14_write(struct regmap *map,
119 unsigned int reg, unsigned int val)
121 u8 *out = map->work_buf;
124 out[1] = (val >> 8) | (reg << 6);
128 static void regmap_format_8(void *buf, unsigned int val, unsigned int shift)
135 static void regmap_format_16_be(void *buf, unsigned int val, unsigned int shift)
139 b[0] = cpu_to_be16(val << shift);
142 static void regmap_format_16_native(void *buf, unsigned int val,
145 *(u16 *)buf = val << shift;
148 static void regmap_format_24(void *buf, unsigned int val, unsigned int shift)
159 static void regmap_format_32_be(void *buf, unsigned int val, unsigned int shift)
163 b[0] = cpu_to_be32(val << shift);
166 static void regmap_format_32_native(void *buf, unsigned int val,
169 *(u32 *)buf = val << shift;
172 static unsigned int regmap_parse_8(void *buf)
179 static unsigned int regmap_parse_16_be(void *buf)
183 b[0] = be16_to_cpu(b[0]);
188 static unsigned int regmap_parse_16_native(void *buf)
193 static unsigned int regmap_parse_24(void *buf)
196 unsigned int ret = b[2];
197 ret |= ((unsigned int)b[1]) << 8;
198 ret |= ((unsigned int)b[0]) << 16;
203 static unsigned int regmap_parse_32_be(void *buf)
207 b[0] = be32_to_cpu(b[0]);
212 static unsigned int regmap_parse_32_native(void *buf)
217 static void regmap_lock_mutex(void *__map)
219 struct regmap *map = __map;
220 mutex_lock(&map->mutex);
223 static void regmap_unlock_mutex(void *__map)
225 struct regmap *map = __map;
226 mutex_unlock(&map->mutex);
229 static void regmap_lock_spinlock(void *__map)
231 struct regmap *map = __map;
232 spin_lock(&map->spinlock);
235 static void regmap_unlock_spinlock(void *__map)
237 struct regmap *map = __map;
238 spin_unlock(&map->spinlock);
241 static void dev_get_regmap_release(struct device *dev, void *res)
244 * We don't actually have anything to do here; the goal here
245 * is not to manage the regmap but to provide a simple way to
246 * get the regmap back given a struct device.
250 static bool _regmap_range_add(struct regmap *map,
251 struct regmap_range_node *data)
253 struct rb_root *root = &map->range_tree;
254 struct rb_node **new = &(root->rb_node), *parent = NULL;
257 struct regmap_range_node *this =
258 container_of(*new, struct regmap_range_node, node);
261 if (data->range_max < this->range_min)
262 new = &((*new)->rb_left);
263 else if (data->range_min > this->range_max)
264 new = &((*new)->rb_right);
269 rb_link_node(&data->node, parent, new);
270 rb_insert_color(&data->node, root);
275 static struct regmap_range_node *_regmap_range_lookup(struct regmap *map,
278 struct rb_node *node = map->range_tree.rb_node;
281 struct regmap_range_node *this =
282 container_of(node, struct regmap_range_node, node);
284 if (reg < this->range_min)
285 node = node->rb_left;
286 else if (reg > this->range_max)
287 node = node->rb_right;
295 static void regmap_range_exit(struct regmap *map)
297 struct rb_node *next;
298 struct regmap_range_node *range_node;
300 next = rb_first(&map->range_tree);
302 range_node = rb_entry(next, struct regmap_range_node, node);
303 next = rb_next(&range_node->node);
304 rb_erase(&range_node->node, &map->range_tree);
308 kfree(map->selector_work_buf);
312 * regmap_init(): Initialise register map
314 * @dev: Device that will be interacted with
315 * @bus: Bus-specific callbacks to use with device
316 * @bus_context: Data passed to bus-specific callbacks
317 * @config: Configuration for register map
319 * The return value will be an ERR_PTR() on error or a valid pointer to
320 * a struct regmap. This function should generally not be called
321 * directly, it should be called by bus-specific init functions.
323 struct regmap *regmap_init(struct device *dev,
324 const struct regmap_bus *bus,
326 const struct regmap_config *config)
328 struct regmap *map, **m;
330 enum regmap_endian reg_endian, val_endian;
336 map = kzalloc(sizeof(*map), GFP_KERNEL);
342 if (config->lock && config->unlock) {
343 map->lock = config->lock;
344 map->unlock = config->unlock;
345 map->lock_arg = config->lock_arg;
348 spin_lock_init(&map->spinlock);
349 map->lock = regmap_lock_spinlock;
350 map->unlock = regmap_unlock_spinlock;
352 mutex_init(&map->mutex);
353 map->lock = regmap_lock_mutex;
354 map->unlock = regmap_unlock_mutex;
358 map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8);
359 map->format.pad_bytes = config->pad_bits / 8;
360 map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8);
361 map->format.buf_size = DIV_ROUND_UP(config->reg_bits +
362 config->val_bits + config->pad_bits, 8);
363 map->reg_shift = config->pad_bits % 8;
364 if (config->reg_stride)
365 map->reg_stride = config->reg_stride;
368 map->use_single_rw = config->use_single_rw;
371 map->bus_context = bus_context;
372 map->max_register = config->max_register;
373 map->writeable_reg = config->writeable_reg;
374 map->readable_reg = config->readable_reg;
375 map->volatile_reg = config->volatile_reg;
376 map->precious_reg = config->precious_reg;
377 map->cache_type = config->cache_type;
378 map->name = config->name;
380 if (config->read_flag_mask || config->write_flag_mask) {
381 map->read_flag_mask = config->read_flag_mask;
382 map->write_flag_mask = config->write_flag_mask;
384 map->read_flag_mask = bus->read_flag_mask;
387 reg_endian = config->reg_format_endian;
388 if (reg_endian == REGMAP_ENDIAN_DEFAULT)
389 reg_endian = bus->reg_format_endian_default;
390 if (reg_endian == REGMAP_ENDIAN_DEFAULT)
391 reg_endian = REGMAP_ENDIAN_BIG;
393 val_endian = config->val_format_endian;
394 if (val_endian == REGMAP_ENDIAN_DEFAULT)
395 val_endian = bus->val_format_endian_default;
396 if (val_endian == REGMAP_ENDIAN_DEFAULT)
397 val_endian = REGMAP_ENDIAN_BIG;
399 switch (config->reg_bits + map->reg_shift) {
401 switch (config->val_bits) {
403 map->format.format_write = regmap_format_2_6_write;
411 switch (config->val_bits) {
413 map->format.format_write = regmap_format_4_12_write;
421 switch (config->val_bits) {
423 map->format.format_write = regmap_format_7_9_write;
431 switch (config->val_bits) {
433 map->format.format_write = regmap_format_10_14_write;
441 map->format.format_reg = regmap_format_8;
445 switch (reg_endian) {
446 case REGMAP_ENDIAN_BIG:
447 map->format.format_reg = regmap_format_16_be;
449 case REGMAP_ENDIAN_NATIVE:
450 map->format.format_reg = regmap_format_16_native;
458 switch (reg_endian) {
459 case REGMAP_ENDIAN_BIG:
460 map->format.format_reg = regmap_format_32_be;
462 case REGMAP_ENDIAN_NATIVE:
463 map->format.format_reg = regmap_format_32_native;
474 switch (config->val_bits) {
476 map->format.format_val = regmap_format_8;
477 map->format.parse_val = regmap_parse_8;
480 switch (val_endian) {
481 case REGMAP_ENDIAN_BIG:
482 map->format.format_val = regmap_format_16_be;
483 map->format.parse_val = regmap_parse_16_be;
485 case REGMAP_ENDIAN_NATIVE:
486 map->format.format_val = regmap_format_16_native;
487 map->format.parse_val = regmap_parse_16_native;
494 if (val_endian != REGMAP_ENDIAN_BIG)
496 map->format.format_val = regmap_format_24;
497 map->format.parse_val = regmap_parse_24;
500 switch (val_endian) {
501 case REGMAP_ENDIAN_BIG:
502 map->format.format_val = regmap_format_32_be;
503 map->format.parse_val = regmap_parse_32_be;
505 case REGMAP_ENDIAN_NATIVE:
506 map->format.format_val = regmap_format_32_native;
507 map->format.parse_val = regmap_parse_32_native;
515 if (map->format.format_write) {
516 if ((reg_endian != REGMAP_ENDIAN_BIG) ||
517 (val_endian != REGMAP_ENDIAN_BIG))
519 map->use_single_rw = true;
522 if (!map->format.format_write &&
523 !(map->format.format_reg && map->format.format_val))
526 map->work_buf = kzalloc(map->format.buf_size, GFP_KERNEL);
527 if (map->work_buf == NULL) {
532 map->range_tree = RB_ROOT;
533 for (i = 0; i < config->num_ranges; i++) {
534 const struct regmap_range_cfg *range_cfg = &config->ranges[i];
535 struct regmap_range_node *new;
538 if (range_cfg->range_max < range_cfg->range_min) {
539 dev_err(map->dev, "Invalid range %d: %d < %d\n", i,
540 range_cfg->range_max, range_cfg->range_min);
544 if (range_cfg->range_max > map->max_register) {
545 dev_err(map->dev, "Invalid range %d: %d > %d\n", i,
546 range_cfg->range_max, map->max_register);
550 if (range_cfg->selector_reg > map->max_register) {
552 "Invalid range %d: selector out of map\n", i);
556 if (range_cfg->window_len == 0) {
557 dev_err(map->dev, "Invalid range %d: window_len 0\n",
562 /* Make sure, that this register range has no selector
563 or data window within its boundary */
564 for (j = 0; j < config->num_ranges; j++) {
565 unsigned sel_reg = config->ranges[j].selector_reg;
566 unsigned win_min = config->ranges[j].window_start;
567 unsigned win_max = win_min +
568 config->ranges[j].window_len - 1;
570 if (range_cfg->range_min <= sel_reg &&
571 sel_reg <= range_cfg->range_max) {
573 "Range %d: selector for %d in window\n",
578 if (!(win_max < range_cfg->range_min ||
579 win_min > range_cfg->range_max)) {
581 "Range %d: window for %d in window\n",
587 new = kzalloc(sizeof(*new), GFP_KERNEL);
594 new->name = range_cfg->name;
595 new->range_min = range_cfg->range_min;
596 new->range_max = range_cfg->range_max;
597 new->selector_reg = range_cfg->selector_reg;
598 new->selector_mask = range_cfg->selector_mask;
599 new->selector_shift = range_cfg->selector_shift;
600 new->window_start = range_cfg->window_start;
601 new->window_len = range_cfg->window_len;
603 if (_regmap_range_add(map, new) == false) {
604 dev_err(map->dev, "Failed to add range %d\n", i);
609 if (map->selector_work_buf == NULL) {
610 map->selector_work_buf =
611 kzalloc(map->format.buf_size, GFP_KERNEL);
612 if (map->selector_work_buf == NULL) {
619 ret = regcache_init(map, config);
623 regmap_debugfs_init(map, config->name);
625 /* Add a devres resource for dev_get_regmap() */
626 m = devres_alloc(dev_get_regmap_release, sizeof(*m), GFP_KERNEL);
637 regmap_debugfs_exit(map);
640 regmap_range_exit(map);
641 kfree(map->work_buf);
647 EXPORT_SYMBOL_GPL(regmap_init);
649 static void devm_regmap_release(struct device *dev, void *res)
651 regmap_exit(*(struct regmap **)res);
655 * devm_regmap_init(): Initialise managed register map
657 * @dev: Device that will be interacted with
658 * @bus: Bus-specific callbacks to use with device
659 * @bus_context: Data passed to bus-specific callbacks
660 * @config: Configuration for register map
662 * The return value will be an ERR_PTR() on error or a valid pointer
663 * to a struct regmap. This function should generally not be called
664 * directly, it should be called by bus-specific init functions. The
665 * map will be automatically freed by the device management code.
667 struct regmap *devm_regmap_init(struct device *dev,
668 const struct regmap_bus *bus,
670 const struct regmap_config *config)
672 struct regmap **ptr, *regmap;
674 ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL);
676 return ERR_PTR(-ENOMEM);
678 regmap = regmap_init(dev, bus, bus_context, config);
679 if (!IS_ERR(regmap)) {
681 devres_add(dev, ptr);
688 EXPORT_SYMBOL_GPL(devm_regmap_init);
691 * regmap_reinit_cache(): Reinitialise the current register cache
693 * @map: Register map to operate on.
694 * @config: New configuration. Only the cache data will be used.
696 * Discard any existing register cache for the map and initialize a
697 * new cache. This can be used to restore the cache to defaults or to
698 * update the cache configuration to reflect runtime discovery of the
701 * No explicit locking is done here, the user needs to ensure that
702 * this function will not race with other calls to regmap.
704 int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
707 regmap_debugfs_exit(map);
709 map->max_register = config->max_register;
710 map->writeable_reg = config->writeable_reg;
711 map->readable_reg = config->readable_reg;
712 map->volatile_reg = config->volatile_reg;
713 map->precious_reg = config->precious_reg;
714 map->cache_type = config->cache_type;
716 regmap_debugfs_init(map, config->name);
718 map->cache_bypass = false;
719 map->cache_only = false;
721 return regcache_init(map, config);
723 EXPORT_SYMBOL_GPL(regmap_reinit_cache);
726 * regmap_exit(): Free a previously allocated register map
728 void regmap_exit(struct regmap *map)
731 regmap_debugfs_exit(map);
732 regmap_range_exit(map);
733 if (map->bus->free_context)
734 map->bus->free_context(map->bus_context);
735 kfree(map->work_buf);
738 EXPORT_SYMBOL_GPL(regmap_exit);
740 static int dev_get_regmap_match(struct device *dev, void *res, void *data)
742 struct regmap **r = res;
748 /* If the user didn't specify a name match any */
750 return (*r)->name == data;
756 * dev_get_regmap(): Obtain the regmap (if any) for a device
758 * @dev: Device to retrieve the map for
759 * @name: Optional name for the register map, usually NULL.
761 * Returns the regmap for the device if one is present, or NULL. If
762 * name is specified then it must match the name specified when
763 * registering the device, if it is NULL then the first regmap found
764 * will be used. Devices with multiple register maps are very rare,
765 * generic code should normally not need to specify a name.
767 struct regmap *dev_get_regmap(struct device *dev, const char *name)
769 struct regmap **r = devres_find(dev, dev_get_regmap_release,
770 dev_get_regmap_match, (void *)name);
776 EXPORT_SYMBOL_GPL(dev_get_regmap);
778 static int _regmap_select_page(struct regmap *map, unsigned int *reg,
779 struct regmap_range_node *range,
780 unsigned int val_num)
783 unsigned int win_offset;
784 unsigned int win_page;
788 win_offset = (*reg - range->range_min) % range->window_len;
789 win_page = (*reg - range->range_min) / range->window_len;
792 /* Bulk write shouldn't cross range boundary */
793 if (*reg + val_num - 1 > range->range_max)
796 /* ... or single page boundary */
797 if (val_num > range->window_len - win_offset)
801 /* It is possible to have selector register inside data window.
802 In that case, selector register is located on every page and
803 it needs no page switching, when accessed alone. */
805 range->window_start + win_offset != range->selector_reg) {
806 /* Use separate work_buf during page switching */
807 orig_work_buf = map->work_buf;
808 map->work_buf = map->selector_work_buf;
810 ret = _regmap_update_bits(map, range->selector_reg,
811 range->selector_mask,
812 win_page << range->selector_shift,
815 map->work_buf = orig_work_buf;
821 *reg = range->window_start + win_offset;
826 static int _regmap_raw_write(struct regmap *map, unsigned int reg,
827 const void *val, size_t val_len)
829 struct regmap_range_node *range;
830 u8 *u8 = map->work_buf;
836 /* Check for unwritable registers before we start */
837 if (map->writeable_reg)
838 for (i = 0; i < val_len / map->format.val_bytes; i++)
839 if (!map->writeable_reg(map->dev,
840 reg + (i * map->reg_stride)))
843 if (!map->cache_bypass && map->format.parse_val) {
845 int val_bytes = map->format.val_bytes;
846 for (i = 0; i < val_len / val_bytes; i++) {
847 memcpy(map->work_buf, val + (i * val_bytes), val_bytes);
848 ival = map->format.parse_val(map->work_buf);
849 ret = regcache_write(map, reg + (i * map->reg_stride),
853 "Error in caching of register: %x ret: %d\n",
858 if (map->cache_only) {
859 map->cache_dirty = true;
864 range = _regmap_range_lookup(map, reg);
866 int val_num = val_len / map->format.val_bytes;
867 int win_offset = (reg - range->range_min) % range->window_len;
868 int win_residue = range->window_len - win_offset;
870 /* If the write goes beyond the end of the window split it */
871 while (val_num > win_residue) {
872 dev_dbg(map->dev, "Writing window %d/%zu\n",
873 win_residue, val_len / map->format.val_bytes);
874 ret = _regmap_raw_write(map, reg, val, win_residue *
875 map->format.val_bytes);
880 val_num -= win_residue;
881 val += win_residue * map->format.val_bytes;
882 val_len -= win_residue * map->format.val_bytes;
884 win_offset = (reg - range->range_min) %
886 win_residue = range->window_len - win_offset;
889 ret = _regmap_select_page(map, ®, range, val_num);
894 map->format.format_reg(map->work_buf, reg, map->reg_shift);
896 u8[0] |= map->write_flag_mask;
898 trace_regmap_hw_write_start(map->dev, reg,
899 val_len / map->format.val_bytes);
901 /* If we're doing a single register write we can probably just
902 * send the work_buf directly, otherwise try to do a gather
905 if (val == (map->work_buf + map->format.pad_bytes +
906 map->format.reg_bytes))
907 ret = map->bus->write(map->bus_context, map->work_buf,
908 map->format.reg_bytes +
909 map->format.pad_bytes +
911 else if (map->bus->gather_write)
912 ret = map->bus->gather_write(map->bus_context, map->work_buf,
913 map->format.reg_bytes +
914 map->format.pad_bytes,
917 /* If that didn't work fall back on linearising by hand. */
918 if (ret == -ENOTSUPP) {
919 len = map->format.reg_bytes + map->format.pad_bytes + val_len;
920 buf = kzalloc(len, GFP_KERNEL);
924 memcpy(buf, map->work_buf, map->format.reg_bytes);
925 memcpy(buf + map->format.reg_bytes + map->format.pad_bytes,
927 ret = map->bus->write(map->bus_context, buf, len);
932 trace_regmap_hw_write_done(map->dev, reg,
933 val_len / map->format.val_bytes);
938 int _regmap_write(struct regmap *map, unsigned int reg,
941 struct regmap_range_node *range;
943 BUG_ON(!map->format.format_write && !map->format.format_val);
945 if (!map->cache_bypass && map->format.format_write) {
946 ret = regcache_write(map, reg, val);
949 if (map->cache_only) {
950 map->cache_dirty = true;
956 if (strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
957 dev_info(map->dev, "%x <= %x\n", reg, val);
960 trace_regmap_reg_write(map->dev, reg, val);
962 if (map->format.format_write) {
963 range = _regmap_range_lookup(map, reg);
965 ret = _regmap_select_page(map, ®, range, 1);
970 map->format.format_write(map, reg, val);
972 trace_regmap_hw_write_start(map->dev, reg, 1);
974 ret = map->bus->write(map->bus_context, map->work_buf,
975 map->format.buf_size);
977 trace_regmap_hw_write_done(map->dev, reg, 1);
981 map->format.format_val(map->work_buf + map->format.reg_bytes
982 + map->format.pad_bytes, val, 0);
983 return _regmap_raw_write(map, reg,
985 map->format.reg_bytes +
986 map->format.pad_bytes,
987 map->format.val_bytes);
992 * regmap_write(): Write a value to a single register
994 * @map: Register map to write to
995 * @reg: Register to write to
996 * @val: Value to be written
998 * A value of zero will be returned on success, a negative errno will
999 * be returned in error cases.
1001 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
1005 if (reg % map->reg_stride)
1008 map->lock(map->lock_arg);
1010 ret = _regmap_write(map, reg, val);
1012 map->unlock(map->lock_arg);
1016 EXPORT_SYMBOL_GPL(regmap_write);
1019 * regmap_raw_write(): Write raw values to one or more registers
1021 * @map: Register map to write to
1022 * @reg: Initial register to write to
1023 * @val: Block of data to be written, laid out for direct transmission to the
1025 * @val_len: Length of data pointed to by val.
1027 * This function is intended to be used for things like firmware
1028 * download where a large block of data needs to be transferred to the
1029 * device. No formatting will be done on the data provided.
1031 * A value of zero will be returned on success, a negative errno will
1032 * be returned in error cases.
1034 int regmap_raw_write(struct regmap *map, unsigned int reg,
1035 const void *val, size_t val_len)
1039 if (val_len % map->format.val_bytes)
1041 if (reg % map->reg_stride)
1044 map->lock(map->lock_arg);
1046 ret = _regmap_raw_write(map, reg, val, val_len);
1048 map->unlock(map->lock_arg);
1052 EXPORT_SYMBOL_GPL(regmap_raw_write);
1055 * regmap_bulk_write(): Write multiple registers to the device
1057 * @map: Register map to write to
1058 * @reg: First register to be write from
1059 * @val: Block of data to be written, in native register size for device
1060 * @val_count: Number of registers to write
1062 * This function is intended to be used for writing a large block of
1063 * data to be device either in single transfer or multiple transfer.
1065 * A value of zero will be returned on success, a negative errno will
1066 * be returned in error cases.
1068 int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
1072 size_t val_bytes = map->format.val_bytes;
1075 if (!map->format.parse_val)
1077 if (reg % map->reg_stride)
1080 map->lock(map->lock_arg);
1082 /* No formatting is require if val_byte is 1 */
1083 if (val_bytes == 1) {
1086 wval = kmemdup(val, val_count * val_bytes, GFP_KERNEL);
1089 dev_err(map->dev, "Error in memory allocation\n");
1092 for (i = 0; i < val_count * val_bytes; i += val_bytes)
1093 map->format.parse_val(wval + i);
1096 * Some devices does not support bulk write, for
1097 * them we have a series of single write operations.
1099 if (map->use_single_rw) {
1100 for (i = 0; i < val_count; i++) {
1101 ret = regmap_raw_write(map,
1102 reg + (i * map->reg_stride),
1103 val + (i * val_bytes),
1109 ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count);
1116 map->unlock(map->lock_arg);
1119 EXPORT_SYMBOL_GPL(regmap_bulk_write);
1121 static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
1122 unsigned int val_len)
1124 struct regmap_range_node *range;
1125 u8 *u8 = map->work_buf;
1128 range = _regmap_range_lookup(map, reg);
1130 ret = _regmap_select_page(map, ®, range,
1131 val_len / map->format.val_bytes);
1136 map->format.format_reg(map->work_buf, reg, map->reg_shift);
1139 * Some buses or devices flag reads by setting the high bits in the
1140 * register addresss; since it's always the high bits for all
1141 * current formats we can do this here rather than in
1142 * formatting. This may break if we get interesting formats.
1144 u8[0] |= map->read_flag_mask;
1146 trace_regmap_hw_read_start(map->dev, reg,
1147 val_len / map->format.val_bytes);
1149 ret = map->bus->read(map->bus_context, map->work_buf,
1150 map->format.reg_bytes + map->format.pad_bytes,
1153 trace_regmap_hw_read_done(map->dev, reg,
1154 val_len / map->format.val_bytes);
1159 static int _regmap_read(struct regmap *map, unsigned int reg,
1164 if (!map->cache_bypass) {
1165 ret = regcache_read(map, reg, val);
1170 if (!map->format.parse_val)
1173 if (map->cache_only)
1176 ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
1178 *val = map->format.parse_val(map->work_buf);
1181 if (strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
1182 dev_info(map->dev, "%x => %x\n", reg, *val);
1185 trace_regmap_reg_read(map->dev, reg, *val);
1188 if (ret == 0 && !map->cache_bypass)
1189 regcache_write(map, reg, *val);
1195 * regmap_read(): Read a value from a single register
1197 * @map: Register map to write to
1198 * @reg: Register to be read from
1199 * @val: Pointer to store read value
1201 * A value of zero will be returned on success, a negative errno will
1202 * be returned in error cases.
1204 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
1208 if (reg % map->reg_stride)
1211 map->lock(map->lock_arg);
1213 ret = _regmap_read(map, reg, val);
1215 map->unlock(map->lock_arg);
1219 EXPORT_SYMBOL_GPL(regmap_read);
1222 * regmap_raw_read(): Read raw data from the device
1224 * @map: Register map to write to
1225 * @reg: First register to be read from
1226 * @val: Pointer to store read value
1227 * @val_len: Size of data to read
1229 * A value of zero will be returned on success, a negative errno will
1230 * be returned in error cases.
1232 int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
1235 size_t val_bytes = map->format.val_bytes;
1236 size_t val_count = val_len / val_bytes;
1240 if (val_len % map->format.val_bytes)
1242 if (reg % map->reg_stride)
1245 map->lock(map->lock_arg);
1247 if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
1248 map->cache_type == REGCACHE_NONE) {
1249 /* Physical block read if there's no cache involved */
1250 ret = _regmap_raw_read(map, reg, val, val_len);
1253 /* Otherwise go word by word for the cache; should be low
1254 * cost as we expect to hit the cache.
1256 for (i = 0; i < val_count; i++) {
1257 ret = _regmap_read(map, reg + (i * map->reg_stride),
1262 map->format.format_val(val + (i * val_bytes), v, 0);
1267 map->unlock(map->lock_arg);
1271 EXPORT_SYMBOL_GPL(regmap_raw_read);
1274 * regmap_bulk_read(): Read multiple registers from the device
1276 * @map: Register map to write to
1277 * @reg: First register to be read from
1278 * @val: Pointer to store read value, in native register size for device
1279 * @val_count: Number of registers to read
1281 * A value of zero will be returned on success, a negative errno will
1282 * be returned in error cases.
1284 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
1288 size_t val_bytes = map->format.val_bytes;
1289 bool vol = regmap_volatile_range(map, reg, val_count);
1291 if (!map->format.parse_val)
1293 if (reg % map->reg_stride)
1296 if (vol || map->cache_type == REGCACHE_NONE) {
1298 * Some devices does not support bulk read, for
1299 * them we have a series of single read operations.
1301 if (map->use_single_rw) {
1302 for (i = 0; i < val_count; i++) {
1303 ret = regmap_raw_read(map,
1304 reg + (i * map->reg_stride),
1305 val + (i * val_bytes),
1311 ret = regmap_raw_read(map, reg, val,
1312 val_bytes * val_count);
1317 for (i = 0; i < val_count * val_bytes; i += val_bytes)
1318 map->format.parse_val(val + i);
1320 for (i = 0; i < val_count; i++) {
1322 ret = regmap_read(map, reg + (i * map->reg_stride),
1326 memcpy(val + (i * val_bytes), &ival, val_bytes);
1332 EXPORT_SYMBOL_GPL(regmap_bulk_read);
1334 static int _regmap_update_bits(struct regmap *map, unsigned int reg,
1335 unsigned int mask, unsigned int val,
1339 unsigned int tmp, orig;
1341 ret = _regmap_read(map, reg, &orig);
1349 ret = _regmap_write(map, reg, tmp);
1359 * regmap_update_bits: Perform a read/modify/write cycle on the register map
1361 * @map: Register map to update
1362 * @reg: Register to update
1363 * @mask: Bitmask to change
1364 * @val: New value for bitmask
1366 * Returns zero for success, a negative number on error.
1368 int regmap_update_bits(struct regmap *map, unsigned int reg,
1369 unsigned int mask, unsigned int val)
1374 map->lock(map->lock_arg);
1375 ret = _regmap_update_bits(map, reg, mask, val, &change);
1376 map->unlock(map->lock_arg);
1380 EXPORT_SYMBOL_GPL(regmap_update_bits);
1383 * regmap_update_bits_check: Perform a read/modify/write cycle on the
1384 * register map and report if updated
1386 * @map: Register map to update
1387 * @reg: Register to update
1388 * @mask: Bitmask to change
1389 * @val: New value for bitmask
1390 * @change: Boolean indicating if a write was done
1392 * Returns zero for success, a negative number on error.
1394 int regmap_update_bits_check(struct regmap *map, unsigned int reg,
1395 unsigned int mask, unsigned int val,
1400 map->lock(map->lock_arg);
1401 ret = _regmap_update_bits(map, reg, mask, val, change);
1402 map->unlock(map->lock_arg);
1405 EXPORT_SYMBOL_GPL(regmap_update_bits_check);
1408 * regmap_register_patch: Register and apply register updates to be applied
1409 * on device initialistion
1411 * @map: Register map to apply updates to.
1412 * @regs: Values to update.
1413 * @num_regs: Number of entries in regs.
1415 * Register a set of register updates to be applied to the device
1416 * whenever the device registers are synchronised with the cache and
1417 * apply them immediately. Typically this is used to apply
1418 * corrections to be applied to the device defaults on startup, such
1419 * as the updates some vendors provide to undocumented registers.
1421 int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
1427 /* If needed the implementation can be extended to support this */
1431 map->lock(map->lock_arg);
1433 bypass = map->cache_bypass;
1435 map->cache_bypass = true;
1437 /* Write out first; it's useful to apply even if we fail later. */
1438 for (i = 0; i < num_regs; i++) {
1439 ret = _regmap_write(map, regs[i].reg, regs[i].def);
1441 dev_err(map->dev, "Failed to write %x = %x: %d\n",
1442 regs[i].reg, regs[i].def, ret);
1447 map->patch = kcalloc(num_regs, sizeof(struct reg_default), GFP_KERNEL);
1448 if (map->patch != NULL) {
1449 memcpy(map->patch, regs,
1450 num_regs * sizeof(struct reg_default));
1451 map->patch_regs = num_regs;
1457 map->cache_bypass = bypass;
1459 map->unlock(map->lock_arg);
1463 EXPORT_SYMBOL_GPL(regmap_register_patch);
1466 * regmap_get_val_bytes(): Report the size of a register value
1468 * Report the size of a register value, mainly intended to for use by
1469 * generic infrastructure built on top of regmap.
1471 int regmap_get_val_bytes(struct regmap *map)
1473 if (map->format.format_write)
1476 return map->format.val_bytes;
1478 EXPORT_SYMBOL_GPL(regmap_get_val_bytes);
1480 static int __init regmap_initcall(void)
1482 regmap_debugfs_initcall();
1486 postcore_initcall(regmap_initcall);