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]);
183 * regmap_init(): Initialise register map
185 * @dev: Device that will be interacted with
186 * @bus: Bus-specific callbacks to use with device
187 * @config: Configuration for register map
189 * The return value will be an ERR_PTR() on error or a valid pointer to
190 * a struct regmap. This function should generally not be called
191 * directly, it should be called by bus-specific init functions.
193 struct regmap *regmap_init(struct device *dev,
194 const struct regmap_bus *bus,
195 const struct regmap_config *config)
203 map = kzalloc(sizeof(*map), GFP_KERNEL);
209 mutex_init(&map->lock);
210 map->format.buf_size = (config->reg_bits + config->val_bits) / 8;
211 map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8);
212 map->format.pad_bytes = config->pad_bits / 8;
213 map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8);
214 map->format.buf_size += map->format.pad_bytes;
215 map->reg_shift = config->pad_bits % 8;
218 map->max_register = config->max_register;
219 map->writeable_reg = config->writeable_reg;
220 map->readable_reg = config->readable_reg;
221 map->volatile_reg = config->volatile_reg;
222 map->precious_reg = config->precious_reg;
223 map->cache_type = config->cache_type;
225 if (config->read_flag_mask || config->write_flag_mask) {
226 map->read_flag_mask = config->read_flag_mask;
227 map->write_flag_mask = config->write_flag_mask;
229 map->read_flag_mask = bus->read_flag_mask;
232 switch (config->reg_bits + map->reg_shift) {
234 switch (config->val_bits) {
236 map->format.format_write = regmap_format_2_6_write;
244 switch (config->val_bits) {
246 map->format.format_write = regmap_format_4_12_write;
254 switch (config->val_bits) {
256 map->format.format_write = regmap_format_7_9_write;
264 switch (config->val_bits) {
266 map->format.format_write = regmap_format_10_14_write;
274 map->format.format_reg = regmap_format_8;
278 map->format.format_reg = regmap_format_16;
282 map->format.format_reg = regmap_format_32;
289 switch (config->val_bits) {
291 map->format.format_val = regmap_format_8;
292 map->format.parse_val = regmap_parse_8;
295 map->format.format_val = regmap_format_16;
296 map->format.parse_val = regmap_parse_16;
299 map->format.format_val = regmap_format_24;
300 map->format.parse_val = regmap_parse_24;
303 map->format.format_val = regmap_format_32;
304 map->format.parse_val = regmap_parse_32;
308 if (!map->format.format_write &&
309 !(map->format.format_reg && map->format.format_val))
312 map->work_buf = kzalloc(map->format.buf_size, GFP_KERNEL);
313 if (map->work_buf == NULL) {
318 regmap_debugfs_init(map);
320 ret = regcache_init(map, config);
322 goto err_free_workbuf;
327 kfree(map->work_buf);
333 EXPORT_SYMBOL_GPL(regmap_init);
335 static void devm_regmap_release(struct device *dev, void *res)
337 regmap_exit(*(struct regmap **)res);
341 * devm_regmap_init(): Initialise managed register map
343 * @dev: Device that will be interacted with
344 * @bus: Bus-specific callbacks to use with device
345 * @config: Configuration for register map
347 * The return value will be an ERR_PTR() on error or a valid pointer
348 * to a struct regmap. This function should generally not be called
349 * directly, it should be called by bus-specific init functions. The
350 * map will be automatically freed by the device management code.
352 struct regmap *devm_regmap_init(struct device *dev,
353 const struct regmap_bus *bus,
354 const struct regmap_config *config)
356 struct regmap **ptr, *regmap;
358 ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL);
360 return ERR_PTR(-ENOMEM);
362 regmap = regmap_init(dev, bus, config);
363 if (!IS_ERR(regmap)) {
365 devres_add(dev, ptr);
372 EXPORT_SYMBOL_GPL(devm_regmap_init);
375 * regmap_reinit_cache(): Reinitialise the current register cache
377 * @map: Register map to operate on.
378 * @config: New configuration. Only the cache data will be used.
380 * Discard any existing register cache for the map and initialize a
381 * new cache. This can be used to restore the cache to defaults or to
382 * update the cache configuration to reflect runtime discovery of the
385 int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
389 mutex_lock(&map->lock);
392 regmap_debugfs_exit(map);
394 map->max_register = config->max_register;
395 map->writeable_reg = config->writeable_reg;
396 map->readable_reg = config->readable_reg;
397 map->volatile_reg = config->volatile_reg;
398 map->precious_reg = config->precious_reg;
399 map->cache_type = config->cache_type;
401 regmap_debugfs_init(map);
403 map->cache_bypass = false;
404 map->cache_only = false;
406 ret = regcache_init(map, config);
408 mutex_unlock(&map->lock);
414 * regmap_exit(): Free a previously allocated register map
416 void regmap_exit(struct regmap *map)
419 regmap_debugfs_exit(map);
420 kfree(map->work_buf);
423 EXPORT_SYMBOL_GPL(regmap_exit);
425 static int _regmap_raw_write(struct regmap *map, unsigned int reg,
426 const void *val, size_t val_len)
428 u8 *u8 = map->work_buf;
434 /* Check for unwritable registers before we start */
435 if (map->writeable_reg)
436 for (i = 0; i < val_len / map->format.val_bytes; i++)
437 if (!map->writeable_reg(map->dev, reg + i))
440 if (!map->cache_bypass && map->format.parse_val) {
442 int val_bytes = map->format.val_bytes;
443 for (i = 0; i < val_len / val_bytes; i++) {
444 memcpy(map->work_buf, val + (i * val_bytes), val_bytes);
445 ival = map->format.parse_val(map->work_buf);
446 ret = regcache_write(map, reg + i, ival);
449 "Error in caching of register: %u ret: %d\n",
454 if (map->cache_only) {
455 map->cache_dirty = true;
460 map->format.format_reg(map->work_buf, reg, map->reg_shift);
462 u8[0] |= map->write_flag_mask;
464 trace_regmap_hw_write_start(map->dev, reg,
465 val_len / map->format.val_bytes);
467 /* If we're doing a single register write we can probably just
468 * send the work_buf directly, otherwise try to do a gather
471 if (val == (map->work_buf + map->format.pad_bytes +
472 map->format.reg_bytes))
473 ret = map->bus->write(map->dev, map->work_buf,
474 map->format.reg_bytes +
475 map->format.pad_bytes +
477 else if (map->bus->gather_write)
478 ret = map->bus->gather_write(map->dev, map->work_buf,
479 map->format.reg_bytes +
480 map->format.pad_bytes,
483 /* If that didn't work fall back on linearising by hand. */
484 if (ret == -ENOTSUPP) {
485 len = map->format.reg_bytes + map->format.pad_bytes + val_len;
486 buf = kzalloc(len, GFP_KERNEL);
490 memcpy(buf, map->work_buf, map->format.reg_bytes);
491 memcpy(buf + map->format.reg_bytes + map->format.pad_bytes,
493 ret = map->bus->write(map->dev, buf, len);
498 trace_regmap_hw_write_done(map->dev, reg,
499 val_len / map->format.val_bytes);
504 int _regmap_write(struct regmap *map, unsigned int reg,
508 BUG_ON(!map->format.format_write && !map->format.format_val);
510 if (!map->cache_bypass && map->format.format_write) {
511 ret = regcache_write(map, reg, val);
514 if (map->cache_only) {
515 map->cache_dirty = true;
520 trace_regmap_reg_write(map->dev, reg, val);
522 if (map->format.format_write) {
523 map->format.format_write(map, reg, val);
525 trace_regmap_hw_write_start(map->dev, reg, 1);
527 ret = map->bus->write(map->dev, map->work_buf,
528 map->format.buf_size);
530 trace_regmap_hw_write_done(map->dev, reg, 1);
534 map->format.format_val(map->work_buf + map->format.reg_bytes
535 + map->format.pad_bytes, val, 0);
536 return _regmap_raw_write(map, reg,
538 map->format.reg_bytes +
539 map->format.pad_bytes,
540 map->format.val_bytes);
545 * regmap_write(): Write a value to a single register
547 * @map: Register map to write to
548 * @reg: Register to write to
549 * @val: Value to be written
551 * A value of zero will be returned on success, a negative errno will
552 * be returned in error cases.
554 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
558 mutex_lock(&map->lock);
560 ret = _regmap_write(map, reg, val);
562 mutex_unlock(&map->lock);
566 EXPORT_SYMBOL_GPL(regmap_write);
569 * regmap_raw_write(): Write raw values to one or more registers
571 * @map: Register map to write to
572 * @reg: Initial register to write to
573 * @val: Block of data to be written, laid out for direct transmission to the
575 * @val_len: Length of data pointed to by val.
577 * This function is intended to be used for things like firmware
578 * download where a large block of data needs to be transferred to the
579 * device. No formatting will be done on the data provided.
581 * A value of zero will be returned on success, a negative errno will
582 * be returned in error cases.
584 int regmap_raw_write(struct regmap *map, unsigned int reg,
585 const void *val, size_t val_len)
589 mutex_lock(&map->lock);
591 ret = _regmap_raw_write(map, reg, val, val_len);
593 mutex_unlock(&map->lock);
597 EXPORT_SYMBOL_GPL(regmap_raw_write);
600 * regmap_bulk_write(): Write multiple registers to the device
602 * @map: Register map to write to
603 * @reg: First register to be write from
604 * @val: Block of data to be written, in native register size for device
605 * @val_count: Number of registers to write
607 * This function is intended to be used for writing a large block of
608 * data to be device either in single transfer or multiple transfer.
610 * A value of zero will be returned on success, a negative errno will
611 * be returned in error cases.
613 int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
617 size_t val_bytes = map->format.val_bytes;
620 if (!map->format.parse_val)
623 mutex_lock(&map->lock);
625 /* No formatting is require if val_byte is 1 */
626 if (val_bytes == 1) {
629 wval = kmemdup(val, val_count * val_bytes, GFP_KERNEL);
632 dev_err(map->dev, "Error in memory allocation\n");
635 for (i = 0; i < val_count * val_bytes; i += val_bytes)
636 map->format.parse_val(wval + i);
638 ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count);
644 mutex_unlock(&map->lock);
647 EXPORT_SYMBOL_GPL(regmap_bulk_write);
649 static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
650 unsigned int val_len)
652 u8 *u8 = map->work_buf;
655 map->format.format_reg(map->work_buf, reg, map->reg_shift);
658 * Some buses or devices flag reads by setting the high bits in the
659 * register addresss; since it's always the high bits for all
660 * current formats we can do this here rather than in
661 * formatting. This may break if we get interesting formats.
663 u8[0] |= map->read_flag_mask;
665 trace_regmap_hw_read_start(map->dev, reg,
666 val_len / map->format.val_bytes);
668 ret = map->bus->read(map->dev, map->work_buf,
669 map->format.reg_bytes + map->format.pad_bytes,
672 trace_regmap_hw_read_done(map->dev, reg,
673 val_len / map->format.val_bytes);
678 static int _regmap_read(struct regmap *map, unsigned int reg,
683 if (!map->cache_bypass) {
684 ret = regcache_read(map, reg, val);
689 if (!map->format.parse_val)
695 ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
697 *val = map->format.parse_val(map->work_buf);
698 trace_regmap_reg_read(map->dev, reg, *val);
701 if (ret == 0 && !map->cache_bypass)
702 regcache_write(map, reg, *val);
708 * regmap_read(): Read a value from a single register
710 * @map: Register map to write to
711 * @reg: Register to be read from
712 * @val: Pointer to store read value
714 * A value of zero will be returned on success, a negative errno will
715 * be returned in error cases.
717 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
721 mutex_lock(&map->lock);
723 ret = _regmap_read(map, reg, val);
725 mutex_unlock(&map->lock);
729 EXPORT_SYMBOL_GPL(regmap_read);
732 * regmap_raw_read(): Read raw data from the device
734 * @map: Register map to write to
735 * @reg: First register to be read from
736 * @val: Pointer to store read value
737 * @val_len: Size of data to read
739 * A value of zero will be returned on success, a negative errno will
740 * be returned in error cases.
742 int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
745 size_t val_bytes = map->format.val_bytes;
746 size_t val_count = val_len / val_bytes;
750 mutex_lock(&map->lock);
752 if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
753 map->cache_type == REGCACHE_NONE) {
754 /* Physical block read if there's no cache involved */
755 ret = _regmap_raw_read(map, reg, val, val_len);
758 /* Otherwise go word by word for the cache; should be low
759 * cost as we expect to hit the cache.
761 for (i = 0; i < val_count; i++) {
762 ret = _regmap_read(map, reg + i, &v);
766 map->format.format_val(val + (i * val_bytes), v, 0);
771 mutex_unlock(&map->lock);
775 EXPORT_SYMBOL_GPL(regmap_raw_read);
778 * regmap_bulk_read(): Read multiple registers from the device
780 * @map: Register map to write to
781 * @reg: First register to be read from
782 * @val: Pointer to store read value, in native register size for device
783 * @val_count: Number of registers to read
785 * A value of zero will be returned on success, a negative errno will
786 * be returned in error cases.
788 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
792 size_t val_bytes = map->format.val_bytes;
793 bool vol = regmap_volatile_range(map, reg, val_count);
795 if (!map->format.parse_val)
798 if (vol || map->cache_type == REGCACHE_NONE) {
799 ret = regmap_raw_read(map, reg, val, val_bytes * val_count);
803 for (i = 0; i < val_count * val_bytes; i += val_bytes)
804 map->format.parse_val(val + i);
806 for (i = 0; i < val_count; i++) {
807 ret = regmap_read(map, reg + i, val + (i * val_bytes));
815 EXPORT_SYMBOL_GPL(regmap_bulk_read);
817 static int _regmap_update_bits(struct regmap *map, unsigned int reg,
818 unsigned int mask, unsigned int val,
822 unsigned int tmp, orig;
824 mutex_lock(&map->lock);
826 ret = _regmap_read(map, reg, &orig);
834 ret = _regmap_write(map, reg, tmp);
841 mutex_unlock(&map->lock);
847 * regmap_update_bits: Perform a read/modify/write cycle on the register map
849 * @map: Register map to update
850 * @reg: Register to update
851 * @mask: Bitmask to change
852 * @val: New value for bitmask
854 * Returns zero for success, a negative number on error.
856 int regmap_update_bits(struct regmap *map, unsigned int reg,
857 unsigned int mask, unsigned int val)
860 return _regmap_update_bits(map, reg, mask, val, &change);
862 EXPORT_SYMBOL_GPL(regmap_update_bits);
865 * regmap_update_bits_check: Perform a read/modify/write cycle on the
866 * register map and report if updated
868 * @map: Register map to update
869 * @reg: Register to update
870 * @mask: Bitmask to change
871 * @val: New value for bitmask
872 * @change: Boolean indicating if a write was done
874 * Returns zero for success, a negative number on error.
876 int regmap_update_bits_check(struct regmap *map, unsigned int reg,
877 unsigned int mask, unsigned int val,
880 return _regmap_update_bits(map, reg, mask, val, change);
882 EXPORT_SYMBOL_GPL(regmap_update_bits_check);
885 * regmap_register_patch: Register and apply register updates to be applied
886 * on device initialistion
888 * @map: Register map to apply updates to.
889 * @regs: Values to update.
890 * @num_regs: Number of entries in regs.
892 * Register a set of register updates to be applied to the device
893 * whenever the device registers are synchronised with the cache and
894 * apply them immediately. Typically this is used to apply
895 * corrections to be applied to the device defaults on startup, such
896 * as the updates some vendors provide to undocumented registers.
898 int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
904 /* If needed the implementation can be extended to support this */
908 mutex_lock(&map->lock);
910 bypass = map->cache_bypass;
912 map->cache_bypass = true;
914 /* Write out first; it's useful to apply even if we fail later. */
915 for (i = 0; i < num_regs; i++) {
916 ret = _regmap_write(map, regs[i].reg, regs[i].def);
918 dev_err(map->dev, "Failed to write %x = %x: %d\n",
919 regs[i].reg, regs[i].def, ret);
924 map->patch = kcalloc(num_regs, sizeof(struct reg_default), GFP_KERNEL);
925 if (map->patch != NULL) {
926 memcpy(map->patch, regs,
927 num_regs * sizeof(struct reg_default));
928 map->patch_regs = num_regs;
934 map->cache_bypass = bypass;
936 mutex_unlock(&map->lock);
940 EXPORT_SYMBOL_GPL(regmap_register_patch);
943 * regmap_get_val_bytes(): Report the size of a register value
945 * Report the size of a register value, mainly intended to for use by
946 * generic infrastructure built on top of regmap.
948 int regmap_get_val_bytes(struct regmap *map)
950 if (map->format.format_write)
953 return map->format.val_bytes;
955 EXPORT_SYMBOL_GPL(regmap_get_val_bytes);
957 static int __init regmap_initcall(void)
959 regmap_debugfs_initcall();
963 postcore_initcall(regmap_initcall);