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]);
162 * regmap_init(): Initialise register map
164 * @dev: Device that will be interacted with
165 * @bus: Bus-specific callbacks to use with device
166 * @config: Configuration for register map
168 * The return value will be an ERR_PTR() on error or a valid pointer to
169 * a struct regmap. This function should generally not be called
170 * directly, it should be called by bus-specific init functions.
172 struct regmap *regmap_init(struct device *dev,
173 const struct regmap_bus *bus,
174 const struct regmap_config *config)
182 map = kzalloc(sizeof(*map), GFP_KERNEL);
188 mutex_init(&map->lock);
189 map->format.buf_size = (config->reg_bits + config->val_bits) / 8;
190 map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8);
191 map->format.pad_bytes = config->pad_bits / 8;
192 map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8);
193 map->format.buf_size += map->format.pad_bytes;
196 map->max_register = config->max_register;
197 map->writeable_reg = config->writeable_reg;
198 map->readable_reg = config->readable_reg;
199 map->volatile_reg = config->volatile_reg;
200 map->precious_reg = config->precious_reg;
201 map->cache_type = config->cache_type;
203 if (config->read_flag_mask || config->write_flag_mask) {
204 map->read_flag_mask = config->read_flag_mask;
205 map->write_flag_mask = config->write_flag_mask;
207 map->read_flag_mask = bus->read_flag_mask;
210 switch (config->reg_bits) {
212 switch (config->val_bits) {
214 map->format.format_write = regmap_format_2_6_write;
222 switch (config->val_bits) {
224 map->format.format_write = regmap_format_4_12_write;
232 switch (config->val_bits) {
234 map->format.format_write = regmap_format_7_9_write;
242 switch (config->val_bits) {
244 map->format.format_write = regmap_format_10_14_write;
252 map->format.format_reg = regmap_format_8;
256 map->format.format_reg = regmap_format_16;
260 map->format.format_reg = regmap_format_32;
267 switch (config->val_bits) {
269 map->format.format_val = regmap_format_8;
270 map->format.parse_val = regmap_parse_8;
273 map->format.format_val = regmap_format_16;
274 map->format.parse_val = regmap_parse_16;
277 map->format.format_val = regmap_format_32;
278 map->format.parse_val = regmap_parse_32;
282 if (!map->format.format_write &&
283 !(map->format.format_reg && map->format.format_val))
286 map->work_buf = kzalloc(map->format.buf_size, GFP_KERNEL);
287 if (map->work_buf == NULL) {
292 regmap_debugfs_init(map);
294 ret = regcache_init(map, config);
296 goto err_free_workbuf;
301 kfree(map->work_buf);
307 EXPORT_SYMBOL_GPL(regmap_init);
309 static void devm_regmap_release(struct device *dev, void *res)
311 regmap_exit(*(struct regmap **)res);
315 * devm_regmap_init(): Initialise managed register map
317 * @dev: Device that will be interacted with
318 * @bus: Bus-specific callbacks to use with device
319 * @config: Configuration for register map
321 * The return value will be an ERR_PTR() on error or a valid pointer
322 * to a struct regmap. This function should generally not be called
323 * directly, it should be called by bus-specific init functions. The
324 * map will be automatically freed by the device management code.
326 struct regmap *devm_regmap_init(struct device *dev,
327 const struct regmap_bus *bus,
328 const struct regmap_config *config)
330 struct regmap **ptr, *regmap;
332 ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL);
334 return ERR_PTR(-ENOMEM);
336 regmap = regmap_init(dev, bus, config);
337 if (!IS_ERR(regmap)) {
339 devres_add(dev, ptr);
346 EXPORT_SYMBOL_GPL(devm_regmap_init);
349 * regmap_reinit_cache(): Reinitialise the current register cache
351 * @map: Register map to operate on.
352 * @config: New configuration. Only the cache data will be used.
354 * Discard any existing register cache for the map and initialize a
355 * new cache. This can be used to restore the cache to defaults or to
356 * update the cache configuration to reflect runtime discovery of the
359 int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
363 mutex_lock(&map->lock);
366 regmap_debugfs_exit(map);
368 map->max_register = config->max_register;
369 map->writeable_reg = config->writeable_reg;
370 map->readable_reg = config->readable_reg;
371 map->volatile_reg = config->volatile_reg;
372 map->precious_reg = config->precious_reg;
373 map->cache_type = config->cache_type;
375 regmap_debugfs_init(map);
377 map->cache_bypass = false;
378 map->cache_only = false;
380 ret = regcache_init(map, config);
382 mutex_unlock(&map->lock);
388 * regmap_exit(): Free a previously allocated register map
390 void regmap_exit(struct regmap *map)
393 regmap_debugfs_exit(map);
394 kfree(map->work_buf);
397 EXPORT_SYMBOL_GPL(regmap_exit);
399 static int _regmap_raw_write(struct regmap *map, unsigned int reg,
400 const void *val, size_t val_len)
402 u8 *u8 = map->work_buf;
408 /* Check for unwritable registers before we start */
409 if (map->writeable_reg)
410 for (i = 0; i < val_len / map->format.val_bytes; i++)
411 if (!map->writeable_reg(map->dev, reg + i))
414 map->format.format_reg(map->work_buf, reg);
416 u8[0] |= map->write_flag_mask;
418 trace_regmap_hw_write_start(map->dev, reg,
419 val_len / map->format.val_bytes);
421 /* If we're doing a single register write we can probably just
422 * send the work_buf directly, otherwise try to do a gather
425 if (val == (map->work_buf + map->format.pad_bytes +
426 map->format.reg_bytes))
427 ret = map->bus->write(map->dev, map->work_buf,
428 map->format.reg_bytes +
429 map->format.pad_bytes +
431 else if (map->bus->gather_write)
432 ret = map->bus->gather_write(map->dev, map->work_buf,
433 map->format.reg_bytes +
434 map->format.pad_bytes,
437 /* If that didn't work fall back on linearising by hand. */
438 if (ret == -ENOTSUPP) {
439 len = map->format.reg_bytes + map->format.pad_bytes + val_len;
440 buf = kzalloc(len, GFP_KERNEL);
444 memcpy(buf, map->work_buf, map->format.reg_bytes);
445 memcpy(buf + map->format.reg_bytes + map->format.pad_bytes,
447 ret = map->bus->write(map->dev, buf, len);
452 trace_regmap_hw_write_done(map->dev, reg,
453 val_len / map->format.val_bytes);
458 int _regmap_write(struct regmap *map, unsigned int reg,
462 BUG_ON(!map->format.format_write && !map->format.format_val);
464 if (!map->cache_bypass) {
465 ret = regcache_write(map, reg, val);
468 if (map->cache_only) {
469 map->cache_dirty = true;
474 trace_regmap_reg_write(map->dev, reg, val);
476 if (map->format.format_write) {
477 map->format.format_write(map, reg, val);
479 trace_regmap_hw_write_start(map->dev, reg, 1);
481 ret = map->bus->write(map->dev, map->work_buf,
482 map->format.buf_size);
484 trace_regmap_hw_write_done(map->dev, reg, 1);
488 map->format.format_val(map->work_buf + map->format.reg_bytes
489 + map->format.pad_bytes, val);
490 return _regmap_raw_write(map, reg,
492 map->format.reg_bytes +
493 map->format.pad_bytes,
494 map->format.val_bytes);
499 * regmap_write(): Write a value to a single register
501 * @map: Register map to write to
502 * @reg: Register to write to
503 * @val: Value to be written
505 * A value of zero will be returned on success, a negative errno will
506 * be returned in error cases.
508 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
512 mutex_lock(&map->lock);
514 ret = _regmap_write(map, reg, val);
516 mutex_unlock(&map->lock);
520 EXPORT_SYMBOL_GPL(regmap_write);
523 * regmap_raw_write(): Write raw values to one or more registers
525 * @map: Register map to write to
526 * @reg: Initial register to write to
527 * @val: Block of data to be written, laid out for direct transmission to the
529 * @val_len: Length of data pointed to by val.
531 * This function is intended to be used for things like firmware
532 * download where a large block of data needs to be transferred to the
533 * device. No formatting will be done on the data provided.
535 * A value of zero will be returned on success, a negative errno will
536 * be returned in error cases.
538 int regmap_raw_write(struct regmap *map, unsigned int reg,
539 const void *val, size_t val_len)
541 size_t val_count = val_len / map->format.val_bytes;
544 WARN_ON(!regmap_volatile_range(map, reg, val_count) &&
545 map->cache_type != REGCACHE_NONE);
547 mutex_lock(&map->lock);
549 ret = _regmap_raw_write(map, reg, val, val_len);
551 mutex_unlock(&map->lock);
555 EXPORT_SYMBOL_GPL(regmap_raw_write);
557 static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
558 unsigned int val_len)
560 u8 *u8 = map->work_buf;
563 map->format.format_reg(map->work_buf, reg);
566 * Some buses or devices flag reads by setting the high bits in the
567 * register addresss; since it's always the high bits for all
568 * current formats we can do this here rather than in
569 * formatting. This may break if we get interesting formats.
571 u8[0] |= map->read_flag_mask;
573 trace_regmap_hw_read_start(map->dev, reg,
574 val_len / map->format.val_bytes);
576 ret = map->bus->read(map->dev, map->work_buf,
577 map->format.reg_bytes + map->format.pad_bytes,
580 trace_regmap_hw_read_done(map->dev, reg,
581 val_len / map->format.val_bytes);
586 static int _regmap_read(struct regmap *map, unsigned int reg,
591 if (!map->cache_bypass) {
592 ret = regcache_read(map, reg, val);
597 if (!map->format.parse_val)
603 ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
605 *val = map->format.parse_val(map->work_buf);
606 trace_regmap_reg_read(map->dev, reg, *val);
613 * regmap_read(): Read a value from a single register
615 * @map: Register map to write to
616 * @reg: Register to be read from
617 * @val: Pointer to store read value
619 * A value of zero will be returned on success, a negative errno will
620 * be returned in error cases.
622 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
626 mutex_lock(&map->lock);
628 ret = _regmap_read(map, reg, val);
630 mutex_unlock(&map->lock);
634 EXPORT_SYMBOL_GPL(regmap_read);
637 * regmap_raw_read(): Read raw data from the device
639 * @map: Register map to write to
640 * @reg: First register to be read from
641 * @val: Pointer to store read value
642 * @val_len: Size of data to read
644 * A value of zero will be returned on success, a negative errno will
645 * be returned in error cases.
647 int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
650 size_t val_bytes = map->format.val_bytes;
651 size_t val_count = val_len / val_bytes;
655 mutex_lock(&map->lock);
657 if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
658 map->cache_type == REGCACHE_NONE) {
659 /* Physical block read if there's no cache involved */
660 ret = _regmap_raw_read(map, reg, val, val_len);
663 /* Otherwise go word by word for the cache; should be low
664 * cost as we expect to hit the cache.
666 for (i = 0; i < val_count; i++) {
667 ret = _regmap_read(map, reg + i, &v);
671 map->format.format_val(val + (i * val_bytes), v);
676 mutex_unlock(&map->lock);
680 EXPORT_SYMBOL_GPL(regmap_raw_read);
683 * regmap_bulk_read(): Read multiple registers from the device
685 * @map: Register map to write to
686 * @reg: First register to be read from
687 * @val: Pointer to store read value, in native register size for device
688 * @val_count: Number of registers to read
690 * A value of zero will be returned on success, a negative errno will
691 * be returned in error cases.
693 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
697 size_t val_bytes = map->format.val_bytes;
698 bool vol = regmap_volatile_range(map, reg, val_count);
700 if (!map->format.parse_val)
703 if (vol || map->cache_type == REGCACHE_NONE) {
704 ret = regmap_raw_read(map, reg, val, val_bytes * val_count);
708 for (i = 0; i < val_count * val_bytes; i += val_bytes)
709 map->format.parse_val(val + i);
711 for (i = 0; i < val_count; i++) {
712 ret = regmap_read(map, reg + i, val + (i * val_bytes));
720 EXPORT_SYMBOL_GPL(regmap_bulk_read);
722 static int _regmap_update_bits(struct regmap *map, unsigned int reg,
723 unsigned int mask, unsigned int val,
727 unsigned int tmp, orig;
729 mutex_lock(&map->lock);
731 ret = _regmap_read(map, reg, &orig);
739 ret = _regmap_write(map, reg, tmp);
746 mutex_unlock(&map->lock);
752 * regmap_update_bits: Perform a read/modify/write cycle on the register map
754 * @map: Register map to update
755 * @reg: Register to update
756 * @mask: Bitmask to change
757 * @val: New value for bitmask
759 * Returns zero for success, a negative number on error.
761 int regmap_update_bits(struct regmap *map, unsigned int reg,
762 unsigned int mask, unsigned int val)
765 return _regmap_update_bits(map, reg, mask, val, &change);
767 EXPORT_SYMBOL_GPL(regmap_update_bits);
770 * regmap_update_bits_check: Perform a read/modify/write cycle on the
771 * register map and report if updated
773 * @map: Register map to update
774 * @reg: Register to update
775 * @mask: Bitmask to change
776 * @val: New value for bitmask
777 * @change: Boolean indicating if a write was done
779 * Returns zero for success, a negative number on error.
781 int regmap_update_bits_check(struct regmap *map, unsigned int reg,
782 unsigned int mask, unsigned int val,
785 return _regmap_update_bits(map, reg, mask, val, change);
787 EXPORT_SYMBOL_GPL(regmap_update_bits_check);
790 * regmap_register_patch: Register and apply register updates to be applied
791 * on device initialistion
793 * @map: Register map to apply updates to.
794 * @regs: Values to update.
795 * @num_regs: Number of entries in regs.
797 * Register a set of register updates to be applied to the device
798 * whenever the device registers are synchronised with the cache and
799 * apply them immediately. Typically this is used to apply
800 * corrections to be applied to the device defaults on startup, such
801 * as the updates some vendors provide to undocumented registers.
803 int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
809 /* If needed the implementation can be extended to support this */
813 mutex_lock(&map->lock);
815 bypass = map->cache_bypass;
817 map->cache_bypass = true;
819 /* Write out first; it's useful to apply even if we fail later. */
820 for (i = 0; i < num_regs; i++) {
821 ret = _regmap_write(map, regs[i].reg, regs[i].def);
823 dev_err(map->dev, "Failed to write %x = %x: %d\n",
824 regs[i].reg, regs[i].def, ret);
829 map->patch = kcalloc(num_regs, sizeof(struct reg_default), GFP_KERNEL);
830 if (map->patch != NULL) {
831 memcpy(map->patch, regs,
832 num_regs * sizeof(struct reg_default));
833 map->patch_regs = num_regs;
839 map->cache_bypass = bypass;
841 mutex_unlock(&map->lock);
845 EXPORT_SYMBOL_GPL(regmap_register_patch);
847 static int __init regmap_initcall(void)
849 regmap_debugfs_initcall();
853 postcore_initcall(regmap_initcall);