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)
122 static void regmap_format_16(void *buf, unsigned int val)
126 b[0] = cpu_to_be16(val);
129 static void regmap_format_32(void *buf, unsigned int val)
133 b[0] = cpu_to_be32(val);
136 static unsigned int regmap_parse_8(void *buf)
143 static unsigned int regmap_parse_16(void *buf)
147 b[0] = be16_to_cpu(b[0]);
152 static unsigned int regmap_parse_32(void *buf)
156 b[0] = be32_to_cpu(b[0]);
161 static void regmap_lock_mutex(struct regmap *map)
163 mutex_lock(&map->mutex);
166 static void regmap_unlock_mutex(struct regmap *map)
168 mutex_unlock(&map->mutex);
171 static void regmap_lock_spinlock(struct regmap *map)
173 spin_lock(&map->spinlock);
176 static void regmap_unlock_spinlock(struct regmap *map)
178 spin_unlock(&map->spinlock);
181 static void dev_get_regmap_release(struct device *dev, void *res)
184 * We don't actually have anything to do here; the goal here
185 * is not to manage the regmap but to provide a simple way to
186 * get the regmap back given a struct device.
191 * regmap_init(): Initialise register map
193 * @dev: Device that will be interacted with
194 * @bus: Bus-specific callbacks to use with device
195 * @bus_context: Data passed to bus-specific callbacks
196 * @config: Configuration for register map
198 * The return value will be an ERR_PTR() on error or a valid pointer to
199 * a struct regmap. This function should generally not be called
200 * directly, it should be called by bus-specific init functions.
202 struct regmap *regmap_init(struct device *dev,
203 const struct regmap_bus *bus,
205 const struct regmap_config *config)
207 struct regmap *map, **m;
213 map = kzalloc(sizeof(*map), GFP_KERNEL);
220 spin_lock_init(&map->spinlock);
221 map->lock = regmap_lock_spinlock;
222 map->unlock = regmap_unlock_spinlock;
224 mutex_init(&map->mutex);
225 map->lock = regmap_lock_mutex;
226 map->unlock = regmap_unlock_mutex;
228 map->format.buf_size = (config->reg_bits + config->val_bits) / 8;
229 map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8);
230 map->format.pad_bytes = config->pad_bits / 8;
231 map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8);
232 map->format.buf_size += map->format.pad_bytes;
235 map->bus_context = bus_context;
236 map->max_register = config->max_register;
237 map->writeable_reg = config->writeable_reg;
238 map->readable_reg = config->readable_reg;
239 map->volatile_reg = config->volatile_reg;
240 map->precious_reg = config->precious_reg;
241 map->cache_type = config->cache_type;
242 map->name = config->name;
244 if (config->read_flag_mask || config->write_flag_mask) {
245 map->read_flag_mask = config->read_flag_mask;
246 map->write_flag_mask = config->write_flag_mask;
248 map->read_flag_mask = bus->read_flag_mask;
251 switch (config->reg_bits) {
253 switch (config->val_bits) {
255 map->format.format_write = regmap_format_2_6_write;
263 switch (config->val_bits) {
265 map->format.format_write = regmap_format_4_12_write;
273 switch (config->val_bits) {
275 map->format.format_write = regmap_format_7_9_write;
283 switch (config->val_bits) {
285 map->format.format_write = regmap_format_10_14_write;
293 map->format.format_reg = regmap_format_8;
297 map->format.format_reg = regmap_format_16;
301 map->format.format_reg = regmap_format_32;
308 switch (config->val_bits) {
310 map->format.format_val = regmap_format_8;
311 map->format.parse_val = regmap_parse_8;
314 map->format.format_val = regmap_format_16;
315 map->format.parse_val = regmap_parse_16;
318 map->format.format_val = regmap_format_32;
319 map->format.parse_val = regmap_parse_32;
323 if (!map->format.format_write &&
324 !(map->format.format_reg && map->format.format_val))
327 map->work_buf = kzalloc(map->format.buf_size, GFP_KERNEL);
328 if (map->work_buf == NULL) {
333 regmap_debugfs_init(map);
335 ret = regcache_init(map, config);
337 goto err_free_workbuf;
339 /* Add a devres resource for dev_get_regmap() */
340 m = devres_alloc(dev_get_regmap_release, sizeof(*m), GFP_KERNEL);
353 kfree(map->work_buf);
359 EXPORT_SYMBOL_GPL(regmap_init);
361 static void devm_regmap_release(struct device *dev, void *res)
363 regmap_exit(*(struct regmap **)res);
367 * devm_regmap_init(): Initialise managed register map
369 * @dev: Device that will be interacted with
370 * @bus: Bus-specific callbacks to use with device
371 * @bus_context: Data passed to bus-specific callbacks
372 * @config: Configuration for register map
374 * The return value will be an ERR_PTR() on error or a valid pointer
375 * to a struct regmap. This function should generally not be called
376 * directly, it should be called by bus-specific init functions. The
377 * map will be automatically freed by the device management code.
379 struct regmap *devm_regmap_init(struct device *dev,
380 const struct regmap_bus *bus,
382 const struct regmap_config *config)
384 struct regmap **ptr, *regmap;
386 ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL);
388 return ERR_PTR(-ENOMEM);
390 regmap = regmap_init(dev, bus, bus_context, config);
391 if (!IS_ERR(regmap)) {
393 devres_add(dev, ptr);
400 EXPORT_SYMBOL_GPL(devm_regmap_init);
403 * regmap_reinit_cache(): Reinitialise the current register cache
405 * @map: Register map to operate on.
406 * @config: New configuration. Only the cache data will be used.
408 * Discard any existing register cache for the map and initialize a
409 * new cache. This can be used to restore the cache to defaults or to
410 * update the cache configuration to reflect runtime discovery of the
413 int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
420 regmap_debugfs_exit(map);
422 map->max_register = config->max_register;
423 map->writeable_reg = config->writeable_reg;
424 map->readable_reg = config->readable_reg;
425 map->volatile_reg = config->volatile_reg;
426 map->precious_reg = config->precious_reg;
427 map->cache_type = config->cache_type;
429 regmap_debugfs_init(map);
431 map->cache_bypass = false;
432 map->cache_only = false;
434 ret = regcache_init(map, config);
442 * regmap_exit(): Free a previously allocated register map
444 void regmap_exit(struct regmap *map)
447 regmap_debugfs_exit(map);
448 if (map->bus->free_context)
449 map->bus->free_context(map->bus_context);
450 kfree(map->work_buf);
453 EXPORT_SYMBOL_GPL(regmap_exit);
455 static int dev_get_regmap_match(struct device *dev, void *res, void *data)
457 struct regmap **r = res;
463 /* If the user didn't specify a name match any */
465 return (*r)->name == data;
471 * dev_get_regmap(): Obtain the regmap (if any) for a device
473 * @dev: Device to retrieve the map for
474 * @name: Optional name for the register map, usually NULL.
476 * Returns the regmap for the device if one is present, or NULL. If
477 * name is specified then it must match the name specified when
478 * registering the device, if it is NULL then the first regmap found
479 * will be used. Devices with multiple register maps are very rare,
480 * generic code should normally not need to specify a name.
482 struct regmap *dev_get_regmap(struct device *dev, const char *name)
484 struct regmap **r = devres_find(dev, dev_get_regmap_release,
485 dev_get_regmap_match, (void *)name);
491 EXPORT_SYMBOL_GPL(dev_get_regmap);
493 static int _regmap_raw_write(struct regmap *map, unsigned int reg,
494 const void *val, size_t val_len)
496 u8 *u8 = map->work_buf;
502 /* Check for unwritable registers before we start */
503 if (map->writeable_reg)
504 for (i = 0; i < val_len / map->format.val_bytes; i++)
505 if (!map->writeable_reg(map->dev, reg + i))
508 if (!map->cache_bypass && map->format.parse_val) {
510 int val_bytes = map->format.val_bytes;
511 for (i = 0; i < val_len / val_bytes; i++) {
512 memcpy(map->work_buf, val + (i * val_bytes), val_bytes);
513 ival = map->format.parse_val(map->work_buf);
514 ret = regcache_write(map, reg + i, ival);
517 "Error in caching of register: %u ret: %d\n",
522 if (map->cache_only) {
523 map->cache_dirty = true;
528 map->format.format_reg(map->work_buf, reg);
530 u8[0] |= map->write_flag_mask;
532 trace_regmap_hw_write_start(map->dev, reg,
533 val_len / map->format.val_bytes);
535 /* If we're doing a single register write we can probably just
536 * send the work_buf directly, otherwise try to do a gather
539 if (val == (map->work_buf + map->format.pad_bytes +
540 map->format.reg_bytes))
541 ret = map->bus->write(map->bus_context, map->work_buf,
542 map->format.reg_bytes +
543 map->format.pad_bytes +
545 else if (map->bus->gather_write)
546 ret = map->bus->gather_write(map->bus_context, map->work_buf,
547 map->format.reg_bytes +
548 map->format.pad_bytes,
551 /* If that didn't work fall back on linearising by hand. */
552 if (ret == -ENOTSUPP) {
553 len = map->format.reg_bytes + map->format.pad_bytes + val_len;
554 buf = kzalloc(len, GFP_KERNEL);
558 memcpy(buf, map->work_buf, map->format.reg_bytes);
559 memcpy(buf + map->format.reg_bytes + map->format.pad_bytes,
561 ret = map->bus->write(map->bus_context, buf, len);
566 trace_regmap_hw_write_done(map->dev, reg,
567 val_len / map->format.val_bytes);
572 int _regmap_write(struct regmap *map, unsigned int reg,
576 BUG_ON(!map->format.format_write && !map->format.format_val);
578 if (!map->cache_bypass && map->format.format_write) {
579 ret = regcache_write(map, reg, val);
582 if (map->cache_only) {
583 map->cache_dirty = true;
588 trace_regmap_reg_write(map->dev, reg, val);
590 if (map->format.format_write) {
591 map->format.format_write(map, reg, val);
593 trace_regmap_hw_write_start(map->dev, reg, 1);
595 ret = map->bus->write(map->bus_context, map->work_buf,
596 map->format.buf_size);
598 trace_regmap_hw_write_done(map->dev, reg, 1);
602 map->format.format_val(map->work_buf + map->format.reg_bytes
603 + map->format.pad_bytes, val);
604 return _regmap_raw_write(map, reg,
606 map->format.reg_bytes +
607 map->format.pad_bytes,
608 map->format.val_bytes);
613 * regmap_write(): Write a value to a single register
615 * @map: Register map to write to
616 * @reg: Register to write to
617 * @val: Value to be written
619 * A value of zero will be returned on success, a negative errno will
620 * be returned in error cases.
622 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
628 ret = _regmap_write(map, reg, val);
634 EXPORT_SYMBOL_GPL(regmap_write);
637 * regmap_raw_write(): Write raw values to one or more registers
639 * @map: Register map to write to
640 * @reg: Initial register to write to
641 * @val: Block of data to be written, laid out for direct transmission to the
643 * @val_len: Length of data pointed to by val.
645 * This function is intended to be used for things like firmware
646 * download where a large block of data needs to be transferred to the
647 * device. No formatting will be done on the data provided.
649 * A value of zero will be returned on success, a negative errno will
650 * be returned in error cases.
652 int regmap_raw_write(struct regmap *map, unsigned int reg,
653 const void *val, size_t val_len)
657 if (val_len % map->format.val_bytes)
662 ret = _regmap_raw_write(map, reg, val, val_len);
668 EXPORT_SYMBOL_GPL(regmap_raw_write);
671 * regmap_bulk_write(): Write multiple registers to the device
673 * @map: Register map to write to
674 * @reg: First register to be write from
675 * @val: Block of data to be written, in native register size for device
676 * @val_count: Number of registers to write
678 * This function is intended to be used for writing a large block of
679 * data to be device either in single transfer or multiple transfer.
681 * A value of zero will be returned on success, a negative errno will
682 * be returned in error cases.
684 int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
688 size_t val_bytes = map->format.val_bytes;
691 if (!map->format.parse_val)
696 /* No formatting is require if val_byte is 1 */
697 if (val_bytes == 1) {
700 wval = kmemdup(val, val_count * val_bytes, GFP_KERNEL);
703 dev_err(map->dev, "Error in memory allocation\n");
706 for (i = 0; i < val_count * val_bytes; i += val_bytes)
707 map->format.parse_val(wval + i);
709 ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count);
718 EXPORT_SYMBOL_GPL(regmap_bulk_write);
720 static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
721 unsigned int val_len)
723 u8 *u8 = map->work_buf;
726 map->format.format_reg(map->work_buf, reg);
729 * Some buses or devices flag reads by setting the high bits in the
730 * register addresss; since it's always the high bits for all
731 * current formats we can do this here rather than in
732 * formatting. This may break if we get interesting formats.
734 u8[0] |= map->read_flag_mask;
736 trace_regmap_hw_read_start(map->dev, reg,
737 val_len / map->format.val_bytes);
739 ret = map->bus->read(map->bus_context, map->work_buf,
740 map->format.reg_bytes + map->format.pad_bytes,
743 trace_regmap_hw_read_done(map->dev, reg,
744 val_len / map->format.val_bytes);
749 static int _regmap_read(struct regmap *map, unsigned int reg,
754 if (!map->cache_bypass) {
755 ret = regcache_read(map, reg, val);
760 if (!map->format.parse_val)
766 ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
768 *val = map->format.parse_val(map->work_buf);
769 trace_regmap_reg_read(map->dev, reg, *val);
776 * regmap_read(): Read a value from a single register
778 * @map: Register map to write to
779 * @reg: Register to be read from
780 * @val: Pointer to store read value
782 * A value of zero will be returned on success, a negative errno will
783 * be returned in error cases.
785 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
791 ret = _regmap_read(map, reg, val);
797 EXPORT_SYMBOL_GPL(regmap_read);
800 * regmap_raw_read(): Read raw data from the device
802 * @map: Register map to write to
803 * @reg: First register to be read from
804 * @val: Pointer to store read value
805 * @val_len: Size of data to read
807 * A value of zero will be returned on success, a negative errno will
808 * be returned in error cases.
810 int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
813 size_t val_bytes = map->format.val_bytes;
814 size_t val_count = val_len / val_bytes;
818 if (val_len % map->format.val_bytes)
823 if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
824 map->cache_type == REGCACHE_NONE) {
825 /* Physical block read if there's no cache involved */
826 ret = _regmap_raw_read(map, reg, val, val_len);
829 /* Otherwise go word by word for the cache; should be low
830 * cost as we expect to hit the cache.
832 for (i = 0; i < val_count; i++) {
833 ret = _regmap_read(map, reg + i, &v);
837 map->format.format_val(val + (i * val_bytes), v);
846 EXPORT_SYMBOL_GPL(regmap_raw_read);
849 * regmap_bulk_read(): Read multiple registers from the device
851 * @map: Register map to write to
852 * @reg: First register to be read from
853 * @val: Pointer to store read value, in native register size for device
854 * @val_count: Number of registers to read
856 * A value of zero will be returned on success, a negative errno will
857 * be returned in error cases.
859 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
863 size_t val_bytes = map->format.val_bytes;
864 bool vol = regmap_volatile_range(map, reg, val_count);
866 if (!map->format.parse_val)
869 if (vol || map->cache_type == REGCACHE_NONE) {
870 ret = regmap_raw_read(map, reg, val, val_bytes * val_count);
874 for (i = 0; i < val_count * val_bytes; i += val_bytes)
875 map->format.parse_val(val + i);
877 for (i = 0; i < val_count; i++) {
878 ret = regmap_read(map, reg + i, val + (i * val_bytes));
886 EXPORT_SYMBOL_GPL(regmap_bulk_read);
888 static int _regmap_update_bits(struct regmap *map, unsigned int reg,
889 unsigned int mask, unsigned int val,
893 unsigned int tmp, orig;
897 ret = _regmap_read(map, reg, &orig);
905 ret = _regmap_write(map, reg, tmp);
918 * regmap_update_bits: Perform a read/modify/write cycle on the register map
920 * @map: Register map to update
921 * @reg: Register to update
922 * @mask: Bitmask to change
923 * @val: New value for bitmask
925 * Returns zero for success, a negative number on error.
927 int regmap_update_bits(struct regmap *map, unsigned int reg,
928 unsigned int mask, unsigned int val)
931 return _regmap_update_bits(map, reg, mask, val, &change);
933 EXPORT_SYMBOL_GPL(regmap_update_bits);
936 * regmap_update_bits_check: Perform a read/modify/write cycle on the
937 * register map and report if updated
939 * @map: Register map to update
940 * @reg: Register to update
941 * @mask: Bitmask to change
942 * @val: New value for bitmask
943 * @change: Boolean indicating if a write was done
945 * Returns zero for success, a negative number on error.
947 int regmap_update_bits_check(struct regmap *map, unsigned int reg,
948 unsigned int mask, unsigned int val,
951 return _regmap_update_bits(map, reg, mask, val, change);
953 EXPORT_SYMBOL_GPL(regmap_update_bits_check);
956 * regmap_register_patch: Register and apply register updates to be applied
957 * on device initialistion
959 * @map: Register map to apply updates to.
960 * @regs: Values to update.
961 * @num_regs: Number of entries in regs.
963 * Register a set of register updates to be applied to the device
964 * whenever the device registers are synchronised with the cache and
965 * apply them immediately. Typically this is used to apply
966 * corrections to be applied to the device defaults on startup, such
967 * as the updates some vendors provide to undocumented registers.
969 int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
975 /* If needed the implementation can be extended to support this */
981 bypass = map->cache_bypass;
983 map->cache_bypass = true;
985 /* Write out first; it's useful to apply even if we fail later. */
986 for (i = 0; i < num_regs; i++) {
987 ret = _regmap_write(map, regs[i].reg, regs[i].def);
989 dev_err(map->dev, "Failed to write %x = %x: %d\n",
990 regs[i].reg, regs[i].def, ret);
995 map->patch = kcalloc(num_regs, sizeof(struct reg_default), GFP_KERNEL);
996 if (map->patch != NULL) {
997 memcpy(map->patch, regs,
998 num_regs * sizeof(struct reg_default));
999 map->patch_regs = num_regs;
1005 map->cache_bypass = bypass;
1011 EXPORT_SYMBOL_GPL(regmap_register_patch);
1014 * regmap_get_val_bytes(): Report the size of a register value
1016 * Report the size of a register value, mainly intended to for use by
1017 * generic infrastructure built on top of regmap.
1019 int regmap_get_val_bytes(struct regmap *map)
1021 if (map->format.format_write)
1024 return map->format.val_bytes;
1026 EXPORT_SYMBOL_GPL(regmap_get_val_bytes);
1028 static int __init regmap_initcall(void)
1030 regmap_debugfs_initcall();
1034 postcore_initcall(regmap_initcall);