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>
19 #define CREATE_TRACE_POINTS
20 #include <trace/events/regmap.h>
24 bool regmap_writeable(struct regmap *map, unsigned int reg)
26 if (map->max_register && reg > map->max_register)
29 if (map->writeable_reg)
30 return map->writeable_reg(map->dev, reg);
35 bool regmap_readable(struct regmap *map, unsigned int reg)
37 if (map->max_register && reg > map->max_register)
40 if (map->format.format_write)
43 if (map->readable_reg)
44 return map->readable_reg(map->dev, reg);
49 bool regmap_volatile(struct regmap *map, unsigned int reg)
51 if (!regmap_readable(map, reg))
54 if (map->volatile_reg)
55 return map->volatile_reg(map->dev, reg);
60 bool regmap_precious(struct regmap *map, unsigned int reg)
62 if (!regmap_readable(map, reg))
65 if (map->precious_reg)
66 return map->precious_reg(map->dev, reg);
71 static bool regmap_volatile_range(struct regmap *map, unsigned int reg,
76 for (i = 0; i < num; i++)
77 if (!regmap_volatile(map, reg + i))
83 static void regmap_format_2_6_write(struct regmap *map,
84 unsigned int reg, unsigned int val)
86 u8 *out = map->work_buf;
88 *out = (reg << 6) | val;
91 static void regmap_format_4_12_write(struct regmap *map,
92 unsigned int reg, unsigned int val)
94 __be16 *out = map->work_buf;
95 *out = cpu_to_be16((reg << 12) | val);
98 static void regmap_format_7_9_write(struct regmap *map,
99 unsigned int reg, unsigned int val)
101 __be16 *out = map->work_buf;
102 *out = cpu_to_be16((reg << 9) | val);
105 static void regmap_format_10_14_write(struct regmap *map,
106 unsigned int reg, unsigned int val)
108 u8 *out = map->work_buf;
111 out[1] = (val >> 8) | (reg << 6);
115 static void regmap_format_8(void *buf, unsigned int val, unsigned int shift)
122 static void regmap_format_16(void *buf, unsigned int val, unsigned int shift)
126 b[0] = cpu_to_be16(val << shift);
129 static void regmap_format_24(void *buf, unsigned int val, unsigned int shift)
140 static void regmap_format_32(void *buf, unsigned int val, unsigned int shift)
144 b[0] = cpu_to_be32(val << shift);
147 static unsigned int regmap_parse_8(void *buf)
154 static unsigned int regmap_parse_16(void *buf)
158 b[0] = be16_to_cpu(b[0]);
163 static unsigned int regmap_parse_24(void *buf)
166 unsigned int ret = b[2];
167 ret |= ((unsigned int)b[1]) << 8;
168 ret |= ((unsigned int)b[0]) << 16;
173 static unsigned int regmap_parse_32(void *buf)
177 b[0] = be32_to_cpu(b[0]);
182 static void regmap_lock_mutex(struct regmap *map)
184 mutex_lock(&map->mutex);
187 static void regmap_unlock_mutex(struct regmap *map)
189 mutex_unlock(&map->mutex);
192 static void regmap_lock_spinlock(struct regmap *map)
194 spin_lock(&map->spinlock);
197 static void regmap_unlock_spinlock(struct regmap *map)
199 spin_unlock(&map->spinlock);
202 static void dev_get_regmap_release(struct device *dev, void *res)
205 * We don't actually have anything to do here; the goal here
206 * is not to manage the regmap but to provide a simple way to
207 * get the regmap back given a struct device.
212 * regmap_init(): Initialise register map
214 * @dev: Device that will be interacted with
215 * @bus: Bus-specific callbacks to use with device
216 * @bus_context: Data passed to bus-specific callbacks
217 * @config: Configuration for register map
219 * The return value will be an ERR_PTR() on error or a valid pointer to
220 * a struct regmap. This function should generally not be called
221 * directly, it should be called by bus-specific init functions.
223 struct regmap *regmap_init(struct device *dev,
224 const struct regmap_bus *bus,
226 const struct regmap_config *config)
228 struct regmap *map, **m;
234 map = kzalloc(sizeof(*map), GFP_KERNEL);
241 spin_lock_init(&map->spinlock);
242 map->lock = regmap_lock_spinlock;
243 map->unlock = regmap_unlock_spinlock;
245 mutex_init(&map->mutex);
246 map->lock = regmap_lock_mutex;
247 map->unlock = regmap_unlock_mutex;
249 map->format.buf_size = (config->reg_bits + config->val_bits) / 8;
250 map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8);
251 map->format.pad_bytes = config->pad_bits / 8;
252 map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8);
253 map->format.buf_size += map->format.pad_bytes;
254 map->reg_shift = config->pad_bits % 8;
255 if (config->reg_stride)
256 map->reg_stride = config->reg_stride;
259 map->use_single_rw = config->use_single_rw;
262 map->bus_context = bus_context;
263 map->max_register = config->max_register;
264 map->writeable_reg = config->writeable_reg;
265 map->readable_reg = config->readable_reg;
266 map->volatile_reg = config->volatile_reg;
267 map->precious_reg = config->precious_reg;
268 map->cache_type = config->cache_type;
269 map->name = config->name;
271 if (config->read_flag_mask || config->write_flag_mask) {
272 map->read_flag_mask = config->read_flag_mask;
273 map->write_flag_mask = config->write_flag_mask;
275 map->read_flag_mask = bus->read_flag_mask;
278 switch (config->reg_bits + map->reg_shift) {
280 switch (config->val_bits) {
282 map->format.format_write = regmap_format_2_6_write;
290 switch (config->val_bits) {
292 map->format.format_write = regmap_format_4_12_write;
300 switch (config->val_bits) {
302 map->format.format_write = regmap_format_7_9_write;
310 switch (config->val_bits) {
312 map->format.format_write = regmap_format_10_14_write;
320 map->format.format_reg = regmap_format_8;
324 map->format.format_reg = regmap_format_16;
328 map->format.format_reg = regmap_format_32;
335 switch (config->val_bits) {
337 map->format.format_val = regmap_format_8;
338 map->format.parse_val = regmap_parse_8;
341 map->format.format_val = regmap_format_16;
342 map->format.parse_val = regmap_parse_16;
345 map->format.format_val = regmap_format_24;
346 map->format.parse_val = regmap_parse_24;
349 map->format.format_val = regmap_format_32;
350 map->format.parse_val = regmap_parse_32;
354 if (map->format.format_write)
355 map->use_single_rw = true;
357 if (!map->format.format_write &&
358 !(map->format.format_reg && map->format.format_val))
361 map->work_buf = kzalloc(map->format.buf_size, GFP_KERNEL);
362 if (map->work_buf == NULL) {
367 regmap_debugfs_init(map, config->name);
369 ret = regcache_init(map, config);
371 goto err_free_workbuf;
373 /* Add a devres resource for dev_get_regmap() */
374 m = devres_alloc(dev_get_regmap_release, sizeof(*m), GFP_KERNEL);
387 kfree(map->work_buf);
393 EXPORT_SYMBOL_GPL(regmap_init);
395 static void devm_regmap_release(struct device *dev, void *res)
397 regmap_exit(*(struct regmap **)res);
401 * devm_regmap_init(): Initialise managed register map
403 * @dev: Device that will be interacted with
404 * @bus: Bus-specific callbacks to use with device
405 * @bus_context: Data passed to bus-specific callbacks
406 * @config: Configuration for register map
408 * The return value will be an ERR_PTR() on error or a valid pointer
409 * to a struct regmap. This function should generally not be called
410 * directly, it should be called by bus-specific init functions. The
411 * map will be automatically freed by the device management code.
413 struct regmap *devm_regmap_init(struct device *dev,
414 const struct regmap_bus *bus,
416 const struct regmap_config *config)
418 struct regmap **ptr, *regmap;
420 ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL);
422 return ERR_PTR(-ENOMEM);
424 regmap = regmap_init(dev, bus, bus_context, config);
425 if (!IS_ERR(regmap)) {
427 devres_add(dev, ptr);
434 EXPORT_SYMBOL_GPL(devm_regmap_init);
437 * regmap_reinit_cache(): Reinitialise the current register cache
439 * @map: Register map to operate on.
440 * @config: New configuration. Only the cache data will be used.
442 * Discard any existing register cache for the map and initialize a
443 * new cache. This can be used to restore the cache to defaults or to
444 * update the cache configuration to reflect runtime discovery of the
447 int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
454 regmap_debugfs_exit(map);
456 map->max_register = config->max_register;
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;
463 regmap_debugfs_init(map, config->name);
465 map->cache_bypass = false;
466 map->cache_only = false;
468 ret = regcache_init(map, config);
476 * regmap_exit(): Free a previously allocated register map
478 void regmap_exit(struct regmap *map)
481 regmap_debugfs_exit(map);
482 if (map->bus->free_context)
483 map->bus->free_context(map->bus_context);
484 kfree(map->work_buf);
487 EXPORT_SYMBOL_GPL(regmap_exit);
489 static int dev_get_regmap_match(struct device *dev, void *res, void *data)
491 struct regmap **r = res;
497 /* If the user didn't specify a name match any */
499 return (*r)->name == data;
505 * dev_get_regmap(): Obtain the regmap (if any) for a device
507 * @dev: Device to retrieve the map for
508 * @name: Optional name for the register map, usually NULL.
510 * Returns the regmap for the device if one is present, or NULL. If
511 * name is specified then it must match the name specified when
512 * registering the device, if it is NULL then the first regmap found
513 * will be used. Devices with multiple register maps are very rare,
514 * generic code should normally not need to specify a name.
516 struct regmap *dev_get_regmap(struct device *dev, const char *name)
518 struct regmap **r = devres_find(dev, dev_get_regmap_release,
519 dev_get_regmap_match, (void *)name);
525 EXPORT_SYMBOL_GPL(dev_get_regmap);
527 static int _regmap_raw_write(struct regmap *map, unsigned int reg,
528 const void *val, size_t val_len)
530 u8 *u8 = map->work_buf;
536 /* Check for unwritable registers before we start */
537 if (map->writeable_reg)
538 for (i = 0; i < val_len / map->format.val_bytes; i++)
539 if (!map->writeable_reg(map->dev,
540 reg + (i * map->reg_stride)))
543 if (!map->cache_bypass && map->format.parse_val) {
545 int val_bytes = map->format.val_bytes;
546 for (i = 0; i < val_len / val_bytes; i++) {
547 memcpy(map->work_buf, val + (i * val_bytes), val_bytes);
548 ival = map->format.parse_val(map->work_buf);
549 ret = regcache_write(map, reg + (i * map->reg_stride),
553 "Error in caching of register: %u ret: %d\n",
558 if (map->cache_only) {
559 map->cache_dirty = true;
564 map->format.format_reg(map->work_buf, reg, map->reg_shift);
566 u8[0] |= map->write_flag_mask;
568 trace_regmap_hw_write_start(map->dev, reg,
569 val_len / map->format.val_bytes);
571 /* If we're doing a single register write we can probably just
572 * send the work_buf directly, otherwise try to do a gather
575 if (val == (map->work_buf + map->format.pad_bytes +
576 map->format.reg_bytes))
577 ret = map->bus->write(map->bus_context, map->work_buf,
578 map->format.reg_bytes +
579 map->format.pad_bytes +
581 else if (map->bus->gather_write)
582 ret = map->bus->gather_write(map->bus_context, map->work_buf,
583 map->format.reg_bytes +
584 map->format.pad_bytes,
587 /* If that didn't work fall back on linearising by hand. */
588 if (ret == -ENOTSUPP) {
589 len = map->format.reg_bytes + map->format.pad_bytes + val_len;
590 buf = kzalloc(len, GFP_KERNEL);
594 memcpy(buf, map->work_buf, map->format.reg_bytes);
595 memcpy(buf + map->format.reg_bytes + map->format.pad_bytes,
597 ret = map->bus->write(map->bus_context, buf, len);
602 trace_regmap_hw_write_done(map->dev, reg,
603 val_len / map->format.val_bytes);
608 int _regmap_write(struct regmap *map, unsigned int reg,
612 BUG_ON(!map->format.format_write && !map->format.format_val);
614 if (!map->cache_bypass && map->format.format_write) {
615 ret = regcache_write(map, reg, val);
618 if (map->cache_only) {
619 map->cache_dirty = true;
624 trace_regmap_reg_write(map->dev, reg, val);
626 if (map->format.format_write) {
627 map->format.format_write(map, reg, val);
629 trace_regmap_hw_write_start(map->dev, reg, 1);
631 ret = map->bus->write(map->bus_context, map->work_buf,
632 map->format.buf_size);
634 trace_regmap_hw_write_done(map->dev, reg, 1);
638 map->format.format_val(map->work_buf + map->format.reg_bytes
639 + map->format.pad_bytes, val, 0);
640 return _regmap_raw_write(map, reg,
642 map->format.reg_bytes +
643 map->format.pad_bytes,
644 map->format.val_bytes);
649 * regmap_write(): Write a value to a single register
651 * @map: Register map to write to
652 * @reg: Register to write to
653 * @val: Value to be written
655 * A value of zero will be returned on success, a negative errno will
656 * be returned in error cases.
658 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
662 if (reg % map->reg_stride)
667 ret = _regmap_write(map, reg, val);
673 EXPORT_SYMBOL_GPL(regmap_write);
676 * regmap_raw_write(): Write raw values to one or more registers
678 * @map: Register map to write to
679 * @reg: Initial register to write to
680 * @val: Block of data to be written, laid out for direct transmission to the
682 * @val_len: Length of data pointed to by val.
684 * This function is intended to be used for things like firmware
685 * download where a large block of data needs to be transferred to the
686 * device. No formatting will be done on the data provided.
688 * A value of zero will be returned on success, a negative errno will
689 * be returned in error cases.
691 int regmap_raw_write(struct regmap *map, unsigned int reg,
692 const void *val, size_t val_len)
696 if (val_len % map->format.val_bytes)
698 if (reg % map->reg_stride)
703 ret = _regmap_raw_write(map, reg, val, val_len);
709 EXPORT_SYMBOL_GPL(regmap_raw_write);
712 * regmap_bulk_write(): Write multiple registers to the device
714 * @map: Register map to write to
715 * @reg: First register to be write from
716 * @val: Block of data to be written, in native register size for device
717 * @val_count: Number of registers to write
719 * This function is intended to be used for writing a large block of
720 * data to be device either in single transfer or multiple transfer.
722 * A value of zero will be returned on success, a negative errno will
723 * be returned in error cases.
725 int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
729 size_t val_bytes = map->format.val_bytes;
732 if (!map->format.parse_val)
734 if (reg % map->reg_stride)
739 /* No formatting is require if val_byte is 1 */
740 if (val_bytes == 1) {
743 wval = kmemdup(val, val_count * val_bytes, GFP_KERNEL);
746 dev_err(map->dev, "Error in memory allocation\n");
749 for (i = 0; i < val_count * val_bytes; i += val_bytes)
750 map->format.parse_val(wval + i);
753 * Some devices does not support bulk write, for
754 * them we have a series of single write operations.
756 if (map->use_single_rw) {
757 for (i = 0; i < val_count; i++) {
758 ret = regmap_raw_write(map,
759 reg + (i * map->reg_stride),
760 val + (i * val_bytes),
766 ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count);
776 EXPORT_SYMBOL_GPL(regmap_bulk_write);
778 static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
779 unsigned int val_len)
781 u8 *u8 = map->work_buf;
784 map->format.format_reg(map->work_buf, reg, map->reg_shift);
787 * Some buses or devices flag reads by setting the high bits in the
788 * register addresss; since it's always the high bits for all
789 * current formats we can do this here rather than in
790 * formatting. This may break if we get interesting formats.
792 u8[0] |= map->read_flag_mask;
794 trace_regmap_hw_read_start(map->dev, reg,
795 val_len / map->format.val_bytes);
797 ret = map->bus->read(map->bus_context, map->work_buf,
798 map->format.reg_bytes + map->format.pad_bytes,
801 trace_regmap_hw_read_done(map->dev, reg,
802 val_len / map->format.val_bytes);
807 static int _regmap_read(struct regmap *map, unsigned int reg,
812 if (!map->cache_bypass) {
813 ret = regcache_read(map, reg, val);
818 if (!map->format.parse_val)
824 ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
826 *val = map->format.parse_val(map->work_buf);
827 trace_regmap_reg_read(map->dev, reg, *val);
830 if (ret == 0 && !map->cache_bypass)
831 regcache_write(map, reg, *val);
837 * regmap_read(): Read a value from a single register
839 * @map: Register map to write to
840 * @reg: Register to be read from
841 * @val: Pointer to store read value
843 * A value of zero will be returned on success, a negative errno will
844 * be returned in error cases.
846 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
850 if (reg % map->reg_stride)
855 ret = _regmap_read(map, reg, val);
861 EXPORT_SYMBOL_GPL(regmap_read);
864 * regmap_raw_read(): Read raw data from the device
866 * @map: Register map to write to
867 * @reg: First register to be read from
868 * @val: Pointer to store read value
869 * @val_len: Size of data to read
871 * A value of zero will be returned on success, a negative errno will
872 * be returned in error cases.
874 int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
877 size_t val_bytes = map->format.val_bytes;
878 size_t val_count = val_len / val_bytes;
882 if (val_len % map->format.val_bytes)
884 if (reg % map->reg_stride)
889 if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
890 map->cache_type == REGCACHE_NONE) {
891 /* Physical block read if there's no cache involved */
892 ret = _regmap_raw_read(map, reg, val, val_len);
895 /* Otherwise go word by word for the cache; should be low
896 * cost as we expect to hit the cache.
898 for (i = 0; i < val_count; i++) {
899 ret = _regmap_read(map, reg + (i * map->reg_stride),
904 map->format.format_val(val + (i * val_bytes), v, 0);
913 EXPORT_SYMBOL_GPL(regmap_raw_read);
916 * regmap_bulk_read(): Read multiple registers from the device
918 * @map: Register map to write to
919 * @reg: First register to be read from
920 * @val: Pointer to store read value, in native register size for device
921 * @val_count: Number of registers to read
923 * A value of zero will be returned on success, a negative errno will
924 * be returned in error cases.
926 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
930 size_t val_bytes = map->format.val_bytes;
931 bool vol = regmap_volatile_range(map, reg, val_count);
933 if (!map->format.parse_val)
935 if (reg % map->reg_stride)
938 if (vol || map->cache_type == REGCACHE_NONE) {
940 * Some devices does not support bulk read, for
941 * them we have a series of single read operations.
943 if (map->use_single_rw) {
944 for (i = 0; i < val_count; i++) {
945 ret = regmap_raw_read(map,
946 reg + (i * map->reg_stride),
947 val + (i * val_bytes),
953 ret = regmap_raw_read(map, reg, val,
954 val_bytes * val_count);
959 for (i = 0; i < val_count * val_bytes; i += val_bytes)
960 map->format.parse_val(val + i);
962 for (i = 0; i < val_count; i++) {
964 ret = regmap_read(map, reg + (i * map->reg_stride),
968 memcpy(val + (i * val_bytes), &ival, val_bytes);
974 EXPORT_SYMBOL_GPL(regmap_bulk_read);
976 static int _regmap_update_bits(struct regmap *map, unsigned int reg,
977 unsigned int mask, unsigned int val,
981 unsigned int tmp, orig;
985 ret = _regmap_read(map, reg, &orig);
993 ret = _regmap_write(map, reg, tmp);
1006 * regmap_update_bits: Perform a read/modify/write cycle on the register map
1008 * @map: Register map to update
1009 * @reg: Register to update
1010 * @mask: Bitmask to change
1011 * @val: New value for bitmask
1013 * Returns zero for success, a negative number on error.
1015 int regmap_update_bits(struct regmap *map, unsigned int reg,
1016 unsigned int mask, unsigned int val)
1019 return _regmap_update_bits(map, reg, mask, val, &change);
1021 EXPORT_SYMBOL_GPL(regmap_update_bits);
1024 * regmap_update_bits_check: Perform a read/modify/write cycle on the
1025 * register map and report if updated
1027 * @map: Register map to update
1028 * @reg: Register to update
1029 * @mask: Bitmask to change
1030 * @val: New value for bitmask
1031 * @change: Boolean indicating if a write was done
1033 * Returns zero for success, a negative number on error.
1035 int regmap_update_bits_check(struct regmap *map, unsigned int reg,
1036 unsigned int mask, unsigned int val,
1039 return _regmap_update_bits(map, reg, mask, val, change);
1041 EXPORT_SYMBOL_GPL(regmap_update_bits_check);
1044 * regmap_register_patch: Register and apply register updates to be applied
1045 * on device initialistion
1047 * @map: Register map to apply updates to.
1048 * @regs: Values to update.
1049 * @num_regs: Number of entries in regs.
1051 * Register a set of register updates to be applied to the device
1052 * whenever the device registers are synchronised with the cache and
1053 * apply them immediately. Typically this is used to apply
1054 * corrections to be applied to the device defaults on startup, such
1055 * as the updates some vendors provide to undocumented registers.
1057 int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
1063 /* If needed the implementation can be extended to support this */
1069 bypass = map->cache_bypass;
1071 map->cache_bypass = true;
1073 /* Write out first; it's useful to apply even if we fail later. */
1074 for (i = 0; i < num_regs; i++) {
1075 ret = _regmap_write(map, regs[i].reg, regs[i].def);
1077 dev_err(map->dev, "Failed to write %x = %x: %d\n",
1078 regs[i].reg, regs[i].def, ret);
1083 map->patch = kcalloc(num_regs, sizeof(struct reg_default), GFP_KERNEL);
1084 if (map->patch != NULL) {
1085 memcpy(map->patch, regs,
1086 num_regs * sizeof(struct reg_default));
1087 map->patch_regs = num_regs;
1093 map->cache_bypass = bypass;
1099 EXPORT_SYMBOL_GPL(regmap_register_patch);
1102 * regmap_get_val_bytes(): Report the size of a register value
1104 * Report the size of a register value, mainly intended to for use by
1105 * generic infrastructure built on top of regmap.
1107 int regmap_get_val_bytes(struct regmap *map)
1109 if (map->format.format_write)
1112 return map->format.val_bytes;
1114 EXPORT_SYMBOL_GPL(regmap_get_val_bytes);
1116 static int __init regmap_initcall(void)
1118 regmap_debugfs_initcall();
1122 postcore_initcall(regmap_initcall);