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 * @bus_context: Data passed to bus-specific callbacks
188 * @config: Configuration for register map
190 * The return value will be an ERR_PTR() on error or a valid pointer to
191 * a struct regmap. This function should generally not be called
192 * directly, it should be called by bus-specific init functions.
194 struct regmap *regmap_init(struct device *dev,
195 const struct regmap_bus *bus,
197 const struct regmap_config *config)
205 map = kzalloc(sizeof(*map), GFP_KERNEL);
211 mutex_init(&map->lock);
212 map->format.buf_size = (config->reg_bits + config->val_bits) / 8;
213 map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8);
214 map->format.pad_bytes = config->pad_bits / 8;
215 map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8);
216 map->format.buf_size += map->format.pad_bytes;
217 map->reg_shift = config->pad_bits % 8;
220 map->bus_context = bus_context;
221 map->max_register = config->max_register;
222 map->writeable_reg = config->writeable_reg;
223 map->readable_reg = config->readable_reg;
224 map->volatile_reg = config->volatile_reg;
225 map->precious_reg = config->precious_reg;
226 map->cache_type = config->cache_type;
228 if (config->read_flag_mask || config->write_flag_mask) {
229 map->read_flag_mask = config->read_flag_mask;
230 map->write_flag_mask = config->write_flag_mask;
232 map->read_flag_mask = bus->read_flag_mask;
235 switch (config->reg_bits + map->reg_shift) {
237 switch (config->val_bits) {
239 map->format.format_write = regmap_format_2_6_write;
247 switch (config->val_bits) {
249 map->format.format_write = regmap_format_4_12_write;
257 switch (config->val_bits) {
259 map->format.format_write = regmap_format_7_9_write;
267 switch (config->val_bits) {
269 map->format.format_write = regmap_format_10_14_write;
277 map->format.format_reg = regmap_format_8;
281 map->format.format_reg = regmap_format_16;
285 map->format.format_reg = regmap_format_32;
292 switch (config->val_bits) {
294 map->format.format_val = regmap_format_8;
295 map->format.parse_val = regmap_parse_8;
298 map->format.format_val = regmap_format_16;
299 map->format.parse_val = regmap_parse_16;
302 map->format.format_val = regmap_format_24;
303 map->format.parse_val = regmap_parse_24;
306 map->format.format_val = regmap_format_32;
307 map->format.parse_val = regmap_parse_32;
311 if (!map->format.format_write &&
312 !(map->format.format_reg && map->format.format_val))
315 map->work_buf = kzalloc(map->format.buf_size, GFP_KERNEL);
316 if (map->work_buf == NULL) {
321 regmap_debugfs_init(map);
323 ret = regcache_init(map, config);
325 goto err_free_workbuf;
330 kfree(map->work_buf);
336 EXPORT_SYMBOL_GPL(regmap_init);
338 static void devm_regmap_release(struct device *dev, void *res)
340 regmap_exit(*(struct regmap **)res);
344 * devm_regmap_init(): Initialise managed register map
346 * @dev: Device that will be interacted with
347 * @bus: Bus-specific callbacks to use with device
348 * @bus_context: Data passed to bus-specific callbacks
349 * @config: Configuration for register map
351 * The return value will be an ERR_PTR() on error or a valid pointer
352 * to a struct regmap. This function should generally not be called
353 * directly, it should be called by bus-specific init functions. The
354 * map will be automatically freed by the device management code.
356 struct regmap *devm_regmap_init(struct device *dev,
357 const struct regmap_bus *bus,
359 const struct regmap_config *config)
361 struct regmap **ptr, *regmap;
363 ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL);
365 return ERR_PTR(-ENOMEM);
367 regmap = regmap_init(dev, bus, bus_context, config);
368 if (!IS_ERR(regmap)) {
370 devres_add(dev, ptr);
377 EXPORT_SYMBOL_GPL(devm_regmap_init);
380 * regmap_reinit_cache(): Reinitialise the current register cache
382 * @map: Register map to operate on.
383 * @config: New configuration. Only the cache data will be used.
385 * Discard any existing register cache for the map and initialize a
386 * new cache. This can be used to restore the cache to defaults or to
387 * update the cache configuration to reflect runtime discovery of the
390 int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
394 mutex_lock(&map->lock);
397 regmap_debugfs_exit(map);
399 map->max_register = config->max_register;
400 map->writeable_reg = config->writeable_reg;
401 map->readable_reg = config->readable_reg;
402 map->volatile_reg = config->volatile_reg;
403 map->precious_reg = config->precious_reg;
404 map->cache_type = config->cache_type;
406 regmap_debugfs_init(map);
408 map->cache_bypass = false;
409 map->cache_only = false;
411 ret = regcache_init(map, config);
413 mutex_unlock(&map->lock);
419 * regmap_exit(): Free a previously allocated register map
421 void regmap_exit(struct regmap *map)
424 regmap_debugfs_exit(map);
425 if (map->bus->free_context)
426 map->bus->free_context(map->bus_context);
427 kfree(map->work_buf);
430 EXPORT_SYMBOL_GPL(regmap_exit);
432 static int _regmap_raw_write(struct regmap *map, unsigned int reg,
433 const void *val, size_t val_len)
435 u8 *u8 = map->work_buf;
441 /* Check for unwritable registers before we start */
442 if (map->writeable_reg)
443 for (i = 0; i < val_len / map->format.val_bytes; i++)
444 if (!map->writeable_reg(map->dev, reg + i))
447 if (!map->cache_bypass && map->format.parse_val) {
449 int val_bytes = map->format.val_bytes;
450 for (i = 0; i < val_len / val_bytes; i++) {
451 memcpy(map->work_buf, val + (i * val_bytes), val_bytes);
452 ival = map->format.parse_val(map->work_buf);
453 ret = regcache_write(map, reg + i, ival);
456 "Error in caching of register: %u ret: %d\n",
461 if (map->cache_only) {
462 map->cache_dirty = true;
467 map->format.format_reg(map->work_buf, reg, map->reg_shift);
469 u8[0] |= map->write_flag_mask;
471 trace_regmap_hw_write_start(map->dev, reg,
472 val_len / map->format.val_bytes);
474 /* If we're doing a single register write we can probably just
475 * send the work_buf directly, otherwise try to do a gather
478 if (val == (map->work_buf + map->format.pad_bytes +
479 map->format.reg_bytes))
480 ret = map->bus->write(map->bus_context, map->work_buf,
481 map->format.reg_bytes +
482 map->format.pad_bytes +
484 else if (map->bus->gather_write)
485 ret = map->bus->gather_write(map->bus_context, map->work_buf,
486 map->format.reg_bytes +
487 map->format.pad_bytes,
490 /* If that didn't work fall back on linearising by hand. */
491 if (ret == -ENOTSUPP) {
492 len = map->format.reg_bytes + map->format.pad_bytes + val_len;
493 buf = kzalloc(len, GFP_KERNEL);
497 memcpy(buf, map->work_buf, map->format.reg_bytes);
498 memcpy(buf + map->format.reg_bytes + map->format.pad_bytes,
500 ret = map->bus->write(map->bus_context, buf, len);
505 trace_regmap_hw_write_done(map->dev, reg,
506 val_len / map->format.val_bytes);
511 int _regmap_write(struct regmap *map, unsigned int reg,
515 BUG_ON(!map->format.format_write && !map->format.format_val);
517 if (!map->cache_bypass && map->format.format_write) {
518 ret = regcache_write(map, reg, val);
521 if (map->cache_only) {
522 map->cache_dirty = true;
527 trace_regmap_reg_write(map->dev, reg, val);
529 if (map->format.format_write) {
530 map->format.format_write(map, reg, val);
532 trace_regmap_hw_write_start(map->dev, reg, 1);
534 ret = map->bus->write(map->bus_context, map->work_buf,
535 map->format.buf_size);
537 trace_regmap_hw_write_done(map->dev, reg, 1);
541 map->format.format_val(map->work_buf + map->format.reg_bytes
542 + map->format.pad_bytes, val, 0);
543 return _regmap_raw_write(map, reg,
545 map->format.reg_bytes +
546 map->format.pad_bytes,
547 map->format.val_bytes);
552 * regmap_write(): Write a value to a single register
554 * @map: Register map to write to
555 * @reg: Register to write to
556 * @val: Value to be written
558 * A value of zero will be returned on success, a negative errno will
559 * be returned in error cases.
561 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
565 mutex_lock(&map->lock);
567 ret = _regmap_write(map, reg, val);
569 mutex_unlock(&map->lock);
573 EXPORT_SYMBOL_GPL(regmap_write);
576 * regmap_raw_write(): Write raw values to one or more registers
578 * @map: Register map to write to
579 * @reg: Initial register to write to
580 * @val: Block of data to be written, laid out for direct transmission to the
582 * @val_len: Length of data pointed to by val.
584 * This function is intended to be used for things like firmware
585 * download where a large block of data needs to be transferred to the
586 * device. No formatting will be done on the data provided.
588 * A value of zero will be returned on success, a negative errno will
589 * be returned in error cases.
591 int regmap_raw_write(struct regmap *map, unsigned int reg,
592 const void *val, size_t val_len)
596 mutex_lock(&map->lock);
598 ret = _regmap_raw_write(map, reg, val, val_len);
600 mutex_unlock(&map->lock);
604 EXPORT_SYMBOL_GPL(regmap_raw_write);
607 * regmap_bulk_write(): Write multiple registers to the device
609 * @map: Register map to write to
610 * @reg: First register to be write from
611 * @val: Block of data to be written, in native register size for device
612 * @val_count: Number of registers to write
614 * This function is intended to be used for writing a large block of
615 * data to be device either in single transfer or multiple transfer.
617 * A value of zero will be returned on success, a negative errno will
618 * be returned in error cases.
620 int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
624 size_t val_bytes = map->format.val_bytes;
627 if (!map->format.parse_val)
630 mutex_lock(&map->lock);
632 /* No formatting is require if val_byte is 1 */
633 if (val_bytes == 1) {
636 wval = kmemdup(val, val_count * val_bytes, GFP_KERNEL);
639 dev_err(map->dev, "Error in memory allocation\n");
642 for (i = 0; i < val_count * val_bytes; i += val_bytes)
643 map->format.parse_val(wval + i);
645 ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count);
651 mutex_unlock(&map->lock);
654 EXPORT_SYMBOL_GPL(regmap_bulk_write);
656 static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
657 unsigned int val_len)
659 u8 *u8 = map->work_buf;
662 map->format.format_reg(map->work_buf, reg, map->reg_shift);
665 * Some buses or devices flag reads by setting the high bits in the
666 * register addresss; since it's always the high bits for all
667 * current formats we can do this here rather than in
668 * formatting. This may break if we get interesting formats.
670 u8[0] |= map->read_flag_mask;
672 trace_regmap_hw_read_start(map->dev, reg,
673 val_len / map->format.val_bytes);
675 ret = map->bus->read(map->bus_context, map->work_buf,
676 map->format.reg_bytes + map->format.pad_bytes,
679 trace_regmap_hw_read_done(map->dev, reg,
680 val_len / map->format.val_bytes);
685 static int _regmap_read(struct regmap *map, unsigned int reg,
690 if (!map->cache_bypass) {
691 ret = regcache_read(map, reg, val);
696 if (!map->format.parse_val)
702 ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
704 *val = map->format.parse_val(map->work_buf);
705 trace_regmap_reg_read(map->dev, reg, *val);
712 * regmap_read(): Read a value from a single register
714 * @map: Register map to write to
715 * @reg: Register to be read from
716 * @val: Pointer to store read value
718 * A value of zero will be returned on success, a negative errno will
719 * be returned in error cases.
721 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
725 mutex_lock(&map->lock);
727 ret = _regmap_read(map, reg, val);
729 mutex_unlock(&map->lock);
733 EXPORT_SYMBOL_GPL(regmap_read);
736 * regmap_raw_read(): Read raw data from the device
738 * @map: Register map to write to
739 * @reg: First register to be read from
740 * @val: Pointer to store read value
741 * @val_len: Size of data to read
743 * A value of zero will be returned on success, a negative errno will
744 * be returned in error cases.
746 int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
749 size_t val_bytes = map->format.val_bytes;
750 size_t val_count = val_len / val_bytes;
754 mutex_lock(&map->lock);
756 if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
757 map->cache_type == REGCACHE_NONE) {
758 /* Physical block read if there's no cache involved */
759 ret = _regmap_raw_read(map, reg, val, val_len);
762 /* Otherwise go word by word for the cache; should be low
763 * cost as we expect to hit the cache.
765 for (i = 0; i < val_count; i++) {
766 ret = _regmap_read(map, reg + i, &v);
770 map->format.format_val(val + (i * val_bytes), v, 0);
775 mutex_unlock(&map->lock);
779 EXPORT_SYMBOL_GPL(regmap_raw_read);
782 * regmap_bulk_read(): Read multiple registers from the device
784 * @map: Register map to write to
785 * @reg: First register to be read from
786 * @val: Pointer to store read value, in native register size for device
787 * @val_count: Number of registers to read
789 * A value of zero will be returned on success, a negative errno will
790 * be returned in error cases.
792 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
796 size_t val_bytes = map->format.val_bytes;
797 bool vol = regmap_volatile_range(map, reg, val_count);
799 if (!map->format.parse_val)
802 if (vol || map->cache_type == REGCACHE_NONE) {
803 ret = regmap_raw_read(map, reg, val, val_bytes * val_count);
807 for (i = 0; i < val_count * val_bytes; i += val_bytes)
808 map->format.parse_val(val + i);
810 for (i = 0; i < val_count; i++) {
811 ret = regmap_read(map, reg + i, val + (i * val_bytes));
819 EXPORT_SYMBOL_GPL(regmap_bulk_read);
821 static int _regmap_update_bits(struct regmap *map, unsigned int reg,
822 unsigned int mask, unsigned int val,
826 unsigned int tmp, orig;
828 mutex_lock(&map->lock);
830 ret = _regmap_read(map, reg, &orig);
838 ret = _regmap_write(map, reg, tmp);
845 mutex_unlock(&map->lock);
851 * regmap_update_bits: Perform a read/modify/write cycle on the register map
853 * @map: Register map to update
854 * @reg: Register to update
855 * @mask: Bitmask to change
856 * @val: New value for bitmask
858 * Returns zero for success, a negative number on error.
860 int regmap_update_bits(struct regmap *map, unsigned int reg,
861 unsigned int mask, unsigned int val)
864 return _regmap_update_bits(map, reg, mask, val, &change);
866 EXPORT_SYMBOL_GPL(regmap_update_bits);
869 * regmap_update_bits_check: Perform a read/modify/write cycle on the
870 * register map and report if updated
872 * @map: Register map to update
873 * @reg: Register to update
874 * @mask: Bitmask to change
875 * @val: New value for bitmask
876 * @change: Boolean indicating if a write was done
878 * Returns zero for success, a negative number on error.
880 int regmap_update_bits_check(struct regmap *map, unsigned int reg,
881 unsigned int mask, unsigned int val,
884 return _regmap_update_bits(map, reg, mask, val, change);
886 EXPORT_SYMBOL_GPL(regmap_update_bits_check);
889 * regmap_register_patch: Register and apply register updates to be applied
890 * on device initialistion
892 * @map: Register map to apply updates to.
893 * @regs: Values to update.
894 * @num_regs: Number of entries in regs.
896 * Register a set of register updates to be applied to the device
897 * whenever the device registers are synchronised with the cache and
898 * apply them immediately. Typically this is used to apply
899 * corrections to be applied to the device defaults on startup, such
900 * as the updates some vendors provide to undocumented registers.
902 int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
908 /* If needed the implementation can be extended to support this */
912 mutex_lock(&map->lock);
914 bypass = map->cache_bypass;
916 map->cache_bypass = true;
918 /* Write out first; it's useful to apply even if we fail later. */
919 for (i = 0; i < num_regs; i++) {
920 ret = _regmap_write(map, regs[i].reg, regs[i].def);
922 dev_err(map->dev, "Failed to write %x = %x: %d\n",
923 regs[i].reg, regs[i].def, ret);
928 map->patch = kcalloc(num_regs, sizeof(struct reg_default), GFP_KERNEL);
929 if (map->patch != NULL) {
930 memcpy(map->patch, regs,
931 num_regs * sizeof(struct reg_default));
932 map->patch_regs = num_regs;
938 map->cache_bypass = bypass;
940 mutex_unlock(&map->lock);
944 EXPORT_SYMBOL_GPL(regmap_register_patch);
947 * regmap_get_val_bytes(): Report the size of a register value
949 * Report the size of a register value, mainly intended to for use by
950 * generic infrastructure built on top of regmap.
952 int regmap_get_val_bytes(struct regmap *map)
954 if (map->format.format_write)
957 return map->format.val_bytes;
959 EXPORT_SYMBOL_GPL(regmap_get_val_bytes);
961 static int __init regmap_initcall(void)
963 regmap_debugfs_initcall();
967 postcore_initcall(regmap_initcall);