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_be(void *buf
, unsigned int val
, unsigned int shift
)
126 b
[0] = cpu_to_be16(val
<< shift
);
129 static void regmap_format_16_native(void *buf
, unsigned int val
,
132 *(u16
*)buf
= val
<< shift
;
135 static void regmap_format_24(void *buf
, unsigned int val
, unsigned int shift
)
146 static void regmap_format_32_be(void *buf
, unsigned int val
, unsigned int shift
)
150 b
[0] = cpu_to_be32(val
<< shift
);
153 static void regmap_format_32_native(void *buf
, unsigned int val
,
156 *(u32
*)buf
= val
<< shift
;
159 static unsigned int regmap_parse_8(void *buf
)
166 static unsigned int regmap_parse_16_be(void *buf
)
170 b
[0] = be16_to_cpu(b
[0]);
175 static unsigned int regmap_parse_16_native(void *buf
)
180 static unsigned int regmap_parse_24(void *buf
)
183 unsigned int ret
= b
[2];
184 ret
|= ((unsigned int)b
[1]) << 8;
185 ret
|= ((unsigned int)b
[0]) << 16;
190 static unsigned int regmap_parse_32_be(void *buf
)
194 b
[0] = be32_to_cpu(b
[0]);
199 static unsigned int regmap_parse_32_native(void *buf
)
204 static void regmap_lock_mutex(struct regmap
*map
)
206 mutex_lock(&map
->mutex
);
209 static void regmap_unlock_mutex(struct regmap
*map
)
211 mutex_unlock(&map
->mutex
);
214 static void regmap_lock_spinlock(struct regmap
*map
)
216 spin_lock(&map
->spinlock
);
219 static void regmap_unlock_spinlock(struct regmap
*map
)
221 spin_unlock(&map
->spinlock
);
224 static void dev_get_regmap_release(struct device
*dev
, void *res
)
227 * We don't actually have anything to do here; the goal here
228 * is not to manage the regmap but to provide a simple way to
229 * get the regmap back given a struct device.
234 * regmap_init(): Initialise register map
236 * @dev: Device that will be interacted with
237 * @bus: Bus-specific callbacks to use with device
238 * @bus_context: Data passed to bus-specific callbacks
239 * @config: Configuration for register map
241 * The return value will be an ERR_PTR() on error or a valid pointer to
242 * a struct regmap. This function should generally not be called
243 * directly, it should be called by bus-specific init functions.
245 struct regmap
*regmap_init(struct device
*dev
,
246 const struct regmap_bus
*bus
,
248 const struct regmap_config
*config
)
250 struct regmap
*map
, **m
;
252 enum regmap_endian reg_endian
, val_endian
;
257 map
= kzalloc(sizeof(*map
), GFP_KERNEL
);
264 spin_lock_init(&map
->spinlock
);
265 map
->lock
= regmap_lock_spinlock
;
266 map
->unlock
= regmap_unlock_spinlock
;
268 mutex_init(&map
->mutex
);
269 map
->lock
= regmap_lock_mutex
;
270 map
->unlock
= regmap_unlock_mutex
;
272 map
->format
.buf_size
= (config
->reg_bits
+ config
->val_bits
) / 8;
273 map
->format
.reg_bytes
= DIV_ROUND_UP(config
->reg_bits
, 8);
274 map
->format
.pad_bytes
= config
->pad_bits
/ 8;
275 map
->format
.val_bytes
= DIV_ROUND_UP(config
->val_bits
, 8);
276 map
->format
.buf_size
+= map
->format
.pad_bytes
;
277 map
->reg_shift
= config
->pad_bits
% 8;
278 if (config
->reg_stride
)
279 map
->reg_stride
= config
->reg_stride
;
282 map
->use_single_rw
= config
->use_single_rw
;
285 map
->bus_context
= bus_context
;
286 map
->max_register
= config
->max_register
;
287 map
->writeable_reg
= config
->writeable_reg
;
288 map
->readable_reg
= config
->readable_reg
;
289 map
->volatile_reg
= config
->volatile_reg
;
290 map
->precious_reg
= config
->precious_reg
;
291 map
->cache_type
= config
->cache_type
;
292 map
->name
= config
->name
;
294 if (config
->read_flag_mask
|| config
->write_flag_mask
) {
295 map
->read_flag_mask
= config
->read_flag_mask
;
296 map
->write_flag_mask
= config
->write_flag_mask
;
298 map
->read_flag_mask
= bus
->read_flag_mask
;
301 reg_endian
= config
->reg_format_endian
;
302 if (reg_endian
== REGMAP_ENDIAN_DEFAULT
)
303 reg_endian
= bus
->reg_format_endian_default
;
304 if (reg_endian
== REGMAP_ENDIAN_DEFAULT
)
305 reg_endian
= REGMAP_ENDIAN_BIG
;
307 val_endian
= config
->val_format_endian
;
308 if (val_endian
== REGMAP_ENDIAN_DEFAULT
)
309 val_endian
= bus
->val_format_endian_default
;
310 if (val_endian
== REGMAP_ENDIAN_DEFAULT
)
311 val_endian
= REGMAP_ENDIAN_BIG
;
313 switch (config
->reg_bits
+ map
->reg_shift
) {
315 switch (config
->val_bits
) {
317 map
->format
.format_write
= regmap_format_2_6_write
;
325 switch (config
->val_bits
) {
327 map
->format
.format_write
= regmap_format_4_12_write
;
335 switch (config
->val_bits
) {
337 map
->format
.format_write
= regmap_format_7_9_write
;
345 switch (config
->val_bits
) {
347 map
->format
.format_write
= regmap_format_10_14_write
;
355 map
->format
.format_reg
= regmap_format_8
;
359 switch (reg_endian
) {
360 case REGMAP_ENDIAN_BIG
:
361 map
->format
.format_reg
= regmap_format_16_be
;
363 case REGMAP_ENDIAN_NATIVE
:
364 map
->format
.format_reg
= regmap_format_16_native
;
372 switch (reg_endian
) {
373 case REGMAP_ENDIAN_BIG
:
374 map
->format
.format_reg
= regmap_format_32_be
;
376 case REGMAP_ENDIAN_NATIVE
:
377 map
->format
.format_reg
= regmap_format_32_native
;
388 switch (config
->val_bits
) {
390 map
->format
.format_val
= regmap_format_8
;
391 map
->format
.parse_val
= regmap_parse_8
;
394 switch (val_endian
) {
395 case REGMAP_ENDIAN_BIG
:
396 map
->format
.format_val
= regmap_format_16_be
;
397 map
->format
.parse_val
= regmap_parse_16_be
;
399 case REGMAP_ENDIAN_NATIVE
:
400 map
->format
.format_val
= regmap_format_16_native
;
401 map
->format
.parse_val
= regmap_parse_16_native
;
408 if (val_endian
!= REGMAP_ENDIAN_BIG
)
410 map
->format
.format_val
= regmap_format_24
;
411 map
->format
.parse_val
= regmap_parse_24
;
414 switch (val_endian
) {
415 case REGMAP_ENDIAN_BIG
:
416 map
->format
.format_val
= regmap_format_32_be
;
417 map
->format
.parse_val
= regmap_parse_32_be
;
419 case REGMAP_ENDIAN_NATIVE
:
420 map
->format
.format_val
= regmap_format_32_native
;
421 map
->format
.parse_val
= regmap_parse_32_native
;
429 if (map
->format
.format_write
) {
430 if ((reg_endian
!= REGMAP_ENDIAN_BIG
) ||
431 (val_endian
!= REGMAP_ENDIAN_BIG
))
433 map
->use_single_rw
= true;
436 if (!map
->format
.format_write
&&
437 !(map
->format
.format_reg
&& map
->format
.format_val
))
440 map
->work_buf
= kzalloc(map
->format
.buf_size
, GFP_KERNEL
);
441 if (map
->work_buf
== NULL
) {
446 regmap_debugfs_init(map
, config
->name
);
448 ret
= regcache_init(map
, config
);
450 goto err_free_workbuf
;
452 /* Add a devres resource for dev_get_regmap() */
453 m
= devres_alloc(dev_get_regmap_release
, sizeof(*m
), GFP_KERNEL
);
466 kfree(map
->work_buf
);
472 EXPORT_SYMBOL_GPL(regmap_init
);
474 static void devm_regmap_release(struct device
*dev
, void *res
)
476 regmap_exit(*(struct regmap
**)res
);
480 * devm_regmap_init(): Initialise managed register map
482 * @dev: Device that will be interacted with
483 * @bus: Bus-specific callbacks to use with device
484 * @bus_context: Data passed to bus-specific callbacks
485 * @config: Configuration for register map
487 * The return value will be an ERR_PTR() on error or a valid pointer
488 * to a struct regmap. This function should generally not be called
489 * directly, it should be called by bus-specific init functions. The
490 * map will be automatically freed by the device management code.
492 struct regmap
*devm_regmap_init(struct device
*dev
,
493 const struct regmap_bus
*bus
,
495 const struct regmap_config
*config
)
497 struct regmap
**ptr
, *regmap
;
499 ptr
= devres_alloc(devm_regmap_release
, sizeof(*ptr
), GFP_KERNEL
);
501 return ERR_PTR(-ENOMEM
);
503 regmap
= regmap_init(dev
, bus
, bus_context
, config
);
504 if (!IS_ERR(regmap
)) {
506 devres_add(dev
, ptr
);
513 EXPORT_SYMBOL_GPL(devm_regmap_init
);
516 * regmap_reinit_cache(): Reinitialise the current register cache
518 * @map: Register map to operate on.
519 * @config: New configuration. Only the cache data will be used.
521 * Discard any existing register cache for the map and initialize a
522 * new cache. This can be used to restore the cache to defaults or to
523 * update the cache configuration to reflect runtime discovery of the
526 int regmap_reinit_cache(struct regmap
*map
, const struct regmap_config
*config
)
533 regmap_debugfs_exit(map
);
535 map
->max_register
= config
->max_register
;
536 map
->writeable_reg
= config
->writeable_reg
;
537 map
->readable_reg
= config
->readable_reg
;
538 map
->volatile_reg
= config
->volatile_reg
;
539 map
->precious_reg
= config
->precious_reg
;
540 map
->cache_type
= config
->cache_type
;
542 regmap_debugfs_init(map
, config
->name
);
544 map
->cache_bypass
= false;
545 map
->cache_only
= false;
547 ret
= regcache_init(map
, config
);
555 * regmap_exit(): Free a previously allocated register map
557 void regmap_exit(struct regmap
*map
)
560 regmap_debugfs_exit(map
);
561 if (map
->bus
->free_context
)
562 map
->bus
->free_context(map
->bus_context
);
563 kfree(map
->work_buf
);
566 EXPORT_SYMBOL_GPL(regmap_exit
);
568 static int dev_get_regmap_match(struct device
*dev
, void *res
, void *data
)
570 struct regmap
**r
= res
;
576 /* If the user didn't specify a name match any */
578 return (*r
)->name
== data
;
584 * dev_get_regmap(): Obtain the regmap (if any) for a device
586 * @dev: Device to retrieve the map for
587 * @name: Optional name for the register map, usually NULL.
589 * Returns the regmap for the device if one is present, or NULL. If
590 * name is specified then it must match the name specified when
591 * registering the device, if it is NULL then the first regmap found
592 * will be used. Devices with multiple register maps are very rare,
593 * generic code should normally not need to specify a name.
595 struct regmap
*dev_get_regmap(struct device
*dev
, const char *name
)
597 struct regmap
**r
= devres_find(dev
, dev_get_regmap_release
,
598 dev_get_regmap_match
, (void *)name
);
604 EXPORT_SYMBOL_GPL(dev_get_regmap
);
606 static int _regmap_raw_write(struct regmap
*map
, unsigned int reg
,
607 const void *val
, size_t val_len
)
609 u8
*u8
= map
->work_buf
;
615 /* Check for unwritable registers before we start */
616 if (map
->writeable_reg
)
617 for (i
= 0; i
< val_len
/ map
->format
.val_bytes
; i
++)
618 if (!map
->writeable_reg(map
->dev
,
619 reg
+ (i
* map
->reg_stride
)))
622 if (!map
->cache_bypass
&& map
->format
.parse_val
) {
624 int val_bytes
= map
->format
.val_bytes
;
625 for (i
= 0; i
< val_len
/ val_bytes
; i
++) {
626 memcpy(map
->work_buf
, val
+ (i
* val_bytes
), val_bytes
);
627 ival
= map
->format
.parse_val(map
->work_buf
);
628 ret
= regcache_write(map
, reg
+ (i
* map
->reg_stride
),
632 "Error in caching of register: %u ret: %d\n",
637 if (map
->cache_only
) {
638 map
->cache_dirty
= true;
643 map
->format
.format_reg(map
->work_buf
, reg
, map
->reg_shift
);
645 u8
[0] |= map
->write_flag_mask
;
647 trace_regmap_hw_write_start(map
->dev
, reg
,
648 val_len
/ map
->format
.val_bytes
);
650 /* If we're doing a single register write we can probably just
651 * send the work_buf directly, otherwise try to do a gather
654 if (val
== (map
->work_buf
+ map
->format
.pad_bytes
+
655 map
->format
.reg_bytes
))
656 ret
= map
->bus
->write(map
->bus_context
, map
->work_buf
,
657 map
->format
.reg_bytes
+
658 map
->format
.pad_bytes
+
660 else if (map
->bus
->gather_write
)
661 ret
= map
->bus
->gather_write(map
->bus_context
, map
->work_buf
,
662 map
->format
.reg_bytes
+
663 map
->format
.pad_bytes
,
666 /* If that didn't work fall back on linearising by hand. */
667 if (ret
== -ENOTSUPP
) {
668 len
= map
->format
.reg_bytes
+ map
->format
.pad_bytes
+ val_len
;
669 buf
= kzalloc(len
, GFP_KERNEL
);
673 memcpy(buf
, map
->work_buf
, map
->format
.reg_bytes
);
674 memcpy(buf
+ map
->format
.reg_bytes
+ map
->format
.pad_bytes
,
676 ret
= map
->bus
->write(map
->bus_context
, buf
, len
);
681 trace_regmap_hw_write_done(map
->dev
, reg
,
682 val_len
/ map
->format
.val_bytes
);
687 int _regmap_write(struct regmap
*map
, unsigned int reg
,
691 BUG_ON(!map
->format
.format_write
&& !map
->format
.format_val
);
693 if (!map
->cache_bypass
&& map
->format
.format_write
) {
694 ret
= regcache_write(map
, reg
, val
);
697 if (map
->cache_only
) {
698 map
->cache_dirty
= true;
703 trace_regmap_reg_write(map
->dev
, reg
, val
);
705 if (map
->format
.format_write
) {
706 map
->format
.format_write(map
, reg
, val
);
708 trace_regmap_hw_write_start(map
->dev
, reg
, 1);
710 ret
= map
->bus
->write(map
->bus_context
, map
->work_buf
,
711 map
->format
.buf_size
);
713 trace_regmap_hw_write_done(map
->dev
, reg
, 1);
717 map
->format
.format_val(map
->work_buf
+ map
->format
.reg_bytes
718 + map
->format
.pad_bytes
, val
, 0);
719 return _regmap_raw_write(map
, reg
,
721 map
->format
.reg_bytes
+
722 map
->format
.pad_bytes
,
723 map
->format
.val_bytes
);
728 * regmap_write(): Write a value to a single register
730 * @map: Register map to write to
731 * @reg: Register to write to
732 * @val: Value to be written
734 * A value of zero will be returned on success, a negative errno will
735 * be returned in error cases.
737 int regmap_write(struct regmap
*map
, unsigned int reg
, unsigned int val
)
741 if (reg
% map
->reg_stride
)
746 ret
= _regmap_write(map
, reg
, val
);
752 EXPORT_SYMBOL_GPL(regmap_write
);
755 * regmap_raw_write(): Write raw values to one or more registers
757 * @map: Register map to write to
758 * @reg: Initial register to write to
759 * @val: Block of data to be written, laid out for direct transmission to the
761 * @val_len: Length of data pointed to by val.
763 * This function is intended to be used for things like firmware
764 * download where a large block of data needs to be transferred to the
765 * device. No formatting will be done on the data provided.
767 * A value of zero will be returned on success, a negative errno will
768 * be returned in error cases.
770 int regmap_raw_write(struct regmap
*map
, unsigned int reg
,
771 const void *val
, size_t val_len
)
775 if (val_len
% map
->format
.val_bytes
)
777 if (reg
% map
->reg_stride
)
782 ret
= _regmap_raw_write(map
, reg
, val
, val_len
);
788 EXPORT_SYMBOL_GPL(regmap_raw_write
);
791 * regmap_bulk_write(): Write multiple registers to the device
793 * @map: Register map to write to
794 * @reg: First register to be write from
795 * @val: Block of data to be written, in native register size for device
796 * @val_count: Number of registers to write
798 * This function is intended to be used for writing a large block of
799 * data to be device either in single transfer or multiple transfer.
801 * A value of zero will be returned on success, a negative errno will
802 * be returned in error cases.
804 int regmap_bulk_write(struct regmap
*map
, unsigned int reg
, const void *val
,
808 size_t val_bytes
= map
->format
.val_bytes
;
811 if (!map
->format
.parse_val
)
813 if (reg
% map
->reg_stride
)
818 /* No formatting is require if val_byte is 1 */
819 if (val_bytes
== 1) {
822 wval
= kmemdup(val
, val_count
* val_bytes
, GFP_KERNEL
);
825 dev_err(map
->dev
, "Error in memory allocation\n");
828 for (i
= 0; i
< val_count
* val_bytes
; i
+= val_bytes
)
829 map
->format
.parse_val(wval
+ i
);
832 * Some devices does not support bulk write, for
833 * them we have a series of single write operations.
835 if (map
->use_single_rw
) {
836 for (i
= 0; i
< val_count
; i
++) {
837 ret
= regmap_raw_write(map
,
838 reg
+ (i
* map
->reg_stride
),
839 val
+ (i
* val_bytes
),
845 ret
= _regmap_raw_write(map
, reg
, wval
, val_bytes
* val_count
);
855 EXPORT_SYMBOL_GPL(regmap_bulk_write
);
857 static int _regmap_raw_read(struct regmap
*map
, unsigned int reg
, void *val
,
858 unsigned int val_len
)
860 u8
*u8
= map
->work_buf
;
863 map
->format
.format_reg(map
->work_buf
, reg
, map
->reg_shift
);
866 * Some buses or devices flag reads by setting the high bits in the
867 * register addresss; since it's always the high bits for all
868 * current formats we can do this here rather than in
869 * formatting. This may break if we get interesting formats.
871 u8
[0] |= map
->read_flag_mask
;
873 trace_regmap_hw_read_start(map
->dev
, reg
,
874 val_len
/ map
->format
.val_bytes
);
876 ret
= map
->bus
->read(map
->bus_context
, map
->work_buf
,
877 map
->format
.reg_bytes
+ map
->format
.pad_bytes
,
880 trace_regmap_hw_read_done(map
->dev
, reg
,
881 val_len
/ map
->format
.val_bytes
);
886 static int _regmap_read(struct regmap
*map
, unsigned int reg
,
891 if (!map
->cache_bypass
) {
892 ret
= regcache_read(map
, reg
, val
);
897 if (!map
->format
.parse_val
)
903 ret
= _regmap_raw_read(map
, reg
, map
->work_buf
, map
->format
.val_bytes
);
905 *val
= map
->format
.parse_val(map
->work_buf
);
906 trace_regmap_reg_read(map
->dev
, reg
, *val
);
909 if (ret
== 0 && !map
->cache_bypass
)
910 regcache_write(map
, reg
, *val
);
916 * regmap_read(): Read a value from a single register
918 * @map: Register map to write to
919 * @reg: Register to be read from
920 * @val: Pointer to store read value
922 * A value of zero will be returned on success, a negative errno will
923 * be returned in error cases.
925 int regmap_read(struct regmap
*map
, unsigned int reg
, unsigned int *val
)
929 if (reg
% map
->reg_stride
)
934 ret
= _regmap_read(map
, reg
, val
);
940 EXPORT_SYMBOL_GPL(regmap_read
);
943 * regmap_raw_read(): Read raw data from the device
945 * @map: Register map to write to
946 * @reg: First register to be read from
947 * @val: Pointer to store read value
948 * @val_len: Size of data to read
950 * A value of zero will be returned on success, a negative errno will
951 * be returned in error cases.
953 int regmap_raw_read(struct regmap
*map
, unsigned int reg
, void *val
,
956 size_t val_bytes
= map
->format
.val_bytes
;
957 size_t val_count
= val_len
/ val_bytes
;
961 if (val_len
% map
->format
.val_bytes
)
963 if (reg
% map
->reg_stride
)
968 if (regmap_volatile_range(map
, reg
, val_count
) || map
->cache_bypass
||
969 map
->cache_type
== REGCACHE_NONE
) {
970 /* Physical block read if there's no cache involved */
971 ret
= _regmap_raw_read(map
, reg
, val
, val_len
);
974 /* Otherwise go word by word for the cache; should be low
975 * cost as we expect to hit the cache.
977 for (i
= 0; i
< val_count
; i
++) {
978 ret
= _regmap_read(map
, reg
+ (i
* map
->reg_stride
),
983 map
->format
.format_val(val
+ (i
* val_bytes
), v
, 0);
992 EXPORT_SYMBOL_GPL(regmap_raw_read
);
995 * regmap_bulk_read(): Read multiple registers from the device
997 * @map: Register map to write to
998 * @reg: First register to be read from
999 * @val: Pointer to store read value, in native register size for device
1000 * @val_count: Number of registers to read
1002 * A value of zero will be returned on success, a negative errno will
1003 * be returned in error cases.
1005 int regmap_bulk_read(struct regmap
*map
, unsigned int reg
, void *val
,
1009 size_t val_bytes
= map
->format
.val_bytes
;
1010 bool vol
= regmap_volatile_range(map
, reg
, val_count
);
1012 if (!map
->format
.parse_val
)
1014 if (reg
% map
->reg_stride
)
1017 if (vol
|| map
->cache_type
== REGCACHE_NONE
) {
1019 * Some devices does not support bulk read, for
1020 * them we have a series of single read operations.
1022 if (map
->use_single_rw
) {
1023 for (i
= 0; i
< val_count
; i
++) {
1024 ret
= regmap_raw_read(map
,
1025 reg
+ (i
* map
->reg_stride
),
1026 val
+ (i
* val_bytes
),
1032 ret
= regmap_raw_read(map
, reg
, val
,
1033 val_bytes
* val_count
);
1038 for (i
= 0; i
< val_count
* val_bytes
; i
+= val_bytes
)
1039 map
->format
.parse_val(val
+ i
);
1041 for (i
= 0; i
< val_count
; i
++) {
1043 ret
= regmap_read(map
, reg
+ (i
* map
->reg_stride
),
1047 memcpy(val
+ (i
* val_bytes
), &ival
, val_bytes
);
1053 EXPORT_SYMBOL_GPL(regmap_bulk_read
);
1055 static int _regmap_update_bits(struct regmap
*map
, unsigned int reg
,
1056 unsigned int mask
, unsigned int val
,
1060 unsigned int tmp
, orig
;
1064 ret
= _regmap_read(map
, reg
, &orig
);
1072 ret
= _regmap_write(map
, reg
, tmp
);
1085 * regmap_update_bits: Perform a read/modify/write cycle on the register map
1087 * @map: Register map to update
1088 * @reg: Register to update
1089 * @mask: Bitmask to change
1090 * @val: New value for bitmask
1092 * Returns zero for success, a negative number on error.
1094 int regmap_update_bits(struct regmap
*map
, unsigned int reg
,
1095 unsigned int mask
, unsigned int val
)
1098 return _regmap_update_bits(map
, reg
, mask
, val
, &change
);
1100 EXPORT_SYMBOL_GPL(regmap_update_bits
);
1103 * regmap_update_bits_check: Perform a read/modify/write cycle on the
1104 * register map and report if updated
1106 * @map: Register map to update
1107 * @reg: Register to update
1108 * @mask: Bitmask to change
1109 * @val: New value for bitmask
1110 * @change: Boolean indicating if a write was done
1112 * Returns zero for success, a negative number on error.
1114 int regmap_update_bits_check(struct regmap
*map
, unsigned int reg
,
1115 unsigned int mask
, unsigned int val
,
1118 return _regmap_update_bits(map
, reg
, mask
, val
, change
);
1120 EXPORT_SYMBOL_GPL(regmap_update_bits_check
);
1123 * regmap_register_patch: Register and apply register updates to be applied
1124 * on device initialistion
1126 * @map: Register map to apply updates to.
1127 * @regs: Values to update.
1128 * @num_regs: Number of entries in regs.
1130 * Register a set of register updates to be applied to the device
1131 * whenever the device registers are synchronised with the cache and
1132 * apply them immediately. Typically this is used to apply
1133 * corrections to be applied to the device defaults on startup, such
1134 * as the updates some vendors provide to undocumented registers.
1136 int regmap_register_patch(struct regmap
*map
, const struct reg_default
*regs
,
1142 /* If needed the implementation can be extended to support this */
1148 bypass
= map
->cache_bypass
;
1150 map
->cache_bypass
= true;
1152 /* Write out first; it's useful to apply even if we fail later. */
1153 for (i
= 0; i
< num_regs
; i
++) {
1154 ret
= _regmap_write(map
, regs
[i
].reg
, regs
[i
].def
);
1156 dev_err(map
->dev
, "Failed to write %x = %x: %d\n",
1157 regs
[i
].reg
, regs
[i
].def
, ret
);
1162 map
->patch
= kcalloc(num_regs
, sizeof(struct reg_default
), GFP_KERNEL
);
1163 if (map
->patch
!= NULL
) {
1164 memcpy(map
->patch
, regs
,
1165 num_regs
* sizeof(struct reg_default
));
1166 map
->patch_regs
= num_regs
;
1172 map
->cache_bypass
= bypass
;
1178 EXPORT_SYMBOL_GPL(regmap_register_patch
);
1181 * regmap_get_val_bytes(): Report the size of a register value
1183 * Report the size of a register value, mainly intended to for use by
1184 * generic infrastructure built on top of regmap.
1186 int regmap_get_val_bytes(struct regmap
*map
)
1188 if (map
->format
.format_write
)
1191 return map
->format
.val_bytes
;
1193 EXPORT_SYMBOL_GPL(regmap_get_val_bytes
);
1195 static int __init
regmap_initcall(void)
1197 regmap_debugfs_initcall();
1201 postcore_initcall(regmap_initcall
);