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/slab.h>
14 #include <linux/module.h>
15 #include <linux/mutex.h>
16 #include <linux/err.h>
18 #define CREATE_TRACE_POINTS
19 #include <trace/events/regmap.h>
23 bool regmap_writeable(struct regmap
*map
, unsigned int reg
)
25 if (map
->max_register
&& reg
> map
->max_register
)
28 if (map
->writeable_reg
)
29 return map
->writeable_reg(map
->dev
, reg
);
34 bool regmap_readable(struct regmap
*map
, unsigned int reg
)
36 if (map
->max_register
&& reg
> map
->max_register
)
39 if (map
->readable_reg
)
40 return map
->readable_reg(map
->dev
, reg
);
45 bool regmap_volatile(struct regmap
*map
, unsigned int reg
)
47 if (map
->max_register
&& reg
> map
->max_register
)
50 if (map
->volatile_reg
)
51 return map
->volatile_reg(map
->dev
, reg
);
56 bool regmap_precious(struct regmap
*map
, unsigned int reg
)
58 if (map
->max_register
&& reg
> map
->max_register
)
61 if (map
->precious_reg
)
62 return map
->precious_reg(map
->dev
, reg
);
67 static bool regmap_volatile_range(struct regmap
*map
, unsigned int reg
,
72 for (i
= 0; i
< num
; i
++)
73 if (!regmap_volatile(map
, reg
+ i
))
79 static void regmap_format_4_12_write(struct regmap
*map
,
80 unsigned int reg
, unsigned int val
)
82 __be16
*out
= map
->work_buf
;
83 *out
= cpu_to_be16((reg
<< 12) | val
);
86 static void regmap_format_7_9_write(struct regmap
*map
,
87 unsigned int reg
, unsigned int val
)
89 __be16
*out
= map
->work_buf
;
90 *out
= cpu_to_be16((reg
<< 9) | val
);
93 static void regmap_format_10_14_write(struct regmap
*map
,
94 unsigned int reg
, unsigned int val
)
96 u8
*out
= map
->work_buf
;
99 out
[1] = (val
>> 8) | (reg
<< 6);
103 static void regmap_format_8(void *buf
, unsigned int val
)
110 static void regmap_format_16(void *buf
, unsigned int val
)
114 b
[0] = cpu_to_be16(val
);
117 static unsigned int regmap_parse_8(void *buf
)
124 static unsigned int regmap_parse_16(void *buf
)
128 b
[0] = be16_to_cpu(b
[0]);
134 * regmap_init(): Initialise register map
136 * @dev: Device that will be interacted with
137 * @bus: Bus-specific callbacks to use with device
138 * @config: Configuration for register map
140 * The return value will be an ERR_PTR() on error or a valid pointer to
141 * a struct regmap. This function should generally not be called
142 * directly, it should be called by bus-specific init functions.
144 struct regmap
*regmap_init(struct device
*dev
,
145 const struct regmap_bus
*bus
,
146 const struct regmap_config
*config
)
154 map
= kzalloc(sizeof(*map
), GFP_KERNEL
);
160 mutex_init(&map
->lock
);
161 map
->format
.buf_size
= (config
->reg_bits
+ config
->val_bits
) / 8;
162 map
->format
.reg_bytes
= config
->reg_bits
/ 8;
163 map
->format
.val_bytes
= config
->val_bits
/ 8;
166 map
->max_register
= config
->max_register
;
167 map
->writeable_reg
= config
->writeable_reg
;
168 map
->readable_reg
= config
->readable_reg
;
169 map
->volatile_reg
= config
->volatile_reg
;
170 map
->precious_reg
= config
->precious_reg
;
171 map
->cache_type
= config
->cache_type
;
173 if (config
->read_flag_mask
|| config
->write_flag_mask
) {
174 map
->read_flag_mask
= config
->read_flag_mask
;
175 map
->write_flag_mask
= config
->write_flag_mask
;
177 map
->read_flag_mask
= bus
->read_flag_mask
;
180 switch (config
->reg_bits
) {
182 switch (config
->val_bits
) {
184 map
->format
.format_write
= regmap_format_4_12_write
;
192 switch (config
->val_bits
) {
194 map
->format
.format_write
= regmap_format_7_9_write
;
202 switch (config
->val_bits
) {
204 map
->format
.format_write
= regmap_format_10_14_write
;
212 map
->format
.format_reg
= regmap_format_8
;
216 map
->format
.format_reg
= regmap_format_16
;
223 switch (config
->val_bits
) {
225 map
->format
.format_val
= regmap_format_8
;
226 map
->format
.parse_val
= regmap_parse_8
;
229 map
->format
.format_val
= regmap_format_16
;
230 map
->format
.parse_val
= regmap_parse_16
;
234 if (!map
->format
.format_write
&&
235 !(map
->format
.format_reg
&& map
->format
.format_val
))
238 map
->work_buf
= kmalloc(map
->format
.buf_size
, GFP_KERNEL
);
239 if (map
->work_buf
== NULL
) {
244 regmap_debugfs_init(map
);
246 ret
= regcache_init(map
, config
);
248 goto err_free_workbuf
;
253 kfree(map
->work_buf
);
259 EXPORT_SYMBOL_GPL(regmap_init
);
262 * regmap_reinit_cache(): Reinitialise the current register cache
264 * @map: Register map to operate on.
265 * @config: New configuration. Only the cache data will be used.
267 * Discard any existing register cache for the map and initialize a
268 * new cache. This can be used to restore the cache to defaults or to
269 * update the cache configuration to reflect runtime discovery of the
272 int regmap_reinit_cache(struct regmap
*map
, const struct regmap_config
*config
)
276 mutex_lock(&map
->lock
);
280 map
->max_register
= config
->max_register
;
281 map
->writeable_reg
= config
->writeable_reg
;
282 map
->readable_reg
= config
->readable_reg
;
283 map
->volatile_reg
= config
->volatile_reg
;
284 map
->precious_reg
= config
->precious_reg
;
285 map
->cache_type
= config
->cache_type
;
287 map
->cache_bypass
= false;
288 map
->cache_only
= false;
290 ret
= regcache_init(map
, config
);
292 mutex_unlock(&map
->lock
);
298 * regmap_exit(): Free a previously allocated register map
300 void regmap_exit(struct regmap
*map
)
303 regmap_debugfs_exit(map
);
304 kfree(map
->work_buf
);
307 EXPORT_SYMBOL_GPL(regmap_exit
);
309 static int _regmap_raw_write(struct regmap
*map
, unsigned int reg
,
310 const void *val
, size_t val_len
)
312 u8
*u8
= map
->work_buf
;
318 /* Check for unwritable registers before we start */
319 if (map
->writeable_reg
)
320 for (i
= 0; i
< val_len
/ map
->format
.val_bytes
; i
++)
321 if (!map
->writeable_reg(map
->dev
, reg
+ i
))
324 map
->format
.format_reg(map
->work_buf
, reg
);
326 u8
[0] |= map
->write_flag_mask
;
328 trace_regmap_hw_write_start(map
->dev
, reg
,
329 val_len
/ map
->format
.val_bytes
);
331 /* If we're doing a single register write we can probably just
332 * send the work_buf directly, otherwise try to do a gather
335 if (val
== map
->work_buf
+ map
->format
.reg_bytes
)
336 ret
= map
->bus
->write(map
->dev
, map
->work_buf
,
337 map
->format
.reg_bytes
+ val_len
);
338 else if (map
->bus
->gather_write
)
339 ret
= map
->bus
->gather_write(map
->dev
, map
->work_buf
,
340 map
->format
.reg_bytes
,
343 /* If that didn't work fall back on linearising by hand. */
344 if (ret
== -ENOTSUPP
) {
345 len
= map
->format
.reg_bytes
+ val_len
;
346 buf
= kmalloc(len
, GFP_KERNEL
);
350 memcpy(buf
, map
->work_buf
, map
->format
.reg_bytes
);
351 memcpy(buf
+ map
->format
.reg_bytes
, val
, val_len
);
352 ret
= map
->bus
->write(map
->dev
, buf
, len
);
357 trace_regmap_hw_write_done(map
->dev
, reg
,
358 val_len
/ map
->format
.val_bytes
);
363 int _regmap_write(struct regmap
*map
, unsigned int reg
,
367 BUG_ON(!map
->format
.format_write
&& !map
->format
.format_val
);
369 if (!map
->cache_bypass
) {
370 ret
= regcache_write(map
, reg
, val
);
373 if (map
->cache_only
) {
374 map
->cache_dirty
= true;
379 trace_regmap_reg_write(map
->dev
, reg
, val
);
381 if (map
->format
.format_write
) {
382 map
->format
.format_write(map
, reg
, val
);
384 trace_regmap_hw_write_start(map
->dev
, reg
, 1);
386 ret
= map
->bus
->write(map
->dev
, map
->work_buf
,
387 map
->format
.buf_size
);
389 trace_regmap_hw_write_done(map
->dev
, reg
, 1);
393 map
->format
.format_val(map
->work_buf
+ map
->format
.reg_bytes
,
395 return _regmap_raw_write(map
, reg
,
396 map
->work_buf
+ map
->format
.reg_bytes
,
397 map
->format
.val_bytes
);
402 * regmap_write(): Write a value to a single register
404 * @map: Register map to write to
405 * @reg: Register to write to
406 * @val: Value to be written
408 * A value of zero will be returned on success, a negative errno will
409 * be returned in error cases.
411 int regmap_write(struct regmap
*map
, unsigned int reg
, unsigned int val
)
415 mutex_lock(&map
->lock
);
417 ret
= _regmap_write(map
, reg
, val
);
419 mutex_unlock(&map
->lock
);
423 EXPORT_SYMBOL_GPL(regmap_write
);
426 * regmap_raw_write(): Write raw values to one or more registers
428 * @map: Register map to write to
429 * @reg: Initial register to write to
430 * @val: Block of data to be written, laid out for direct transmission to the
432 * @val_len: Length of data pointed to by val.
434 * This function is intended to be used for things like firmware
435 * download where a large block of data needs to be transferred to the
436 * device. No formatting will be done on the data provided.
438 * A value of zero will be returned on success, a negative errno will
439 * be returned in error cases.
441 int regmap_raw_write(struct regmap
*map
, unsigned int reg
,
442 const void *val
, size_t val_len
)
444 size_t val_count
= val_len
/ map
->format
.val_bytes
;
447 WARN_ON(!regmap_volatile_range(map
, reg
, val_count
) &&
448 map
->cache_type
!= REGCACHE_NONE
);
450 mutex_lock(&map
->lock
);
452 ret
= _regmap_raw_write(map
, reg
, val
, val_len
);
454 mutex_unlock(&map
->lock
);
458 EXPORT_SYMBOL_GPL(regmap_raw_write
);
460 static int _regmap_raw_read(struct regmap
*map
, unsigned int reg
, void *val
,
461 unsigned int val_len
)
463 u8
*u8
= map
->work_buf
;
466 map
->format
.format_reg(map
->work_buf
, reg
);
469 * Some buses or devices flag reads by setting the high bits in the
470 * register addresss; since it's always the high bits for all
471 * current formats we can do this here rather than in
472 * formatting. This may break if we get interesting formats.
474 u8
[0] |= map
->read_flag_mask
;
476 trace_regmap_hw_read_start(map
->dev
, reg
,
477 val_len
/ map
->format
.val_bytes
);
479 ret
= map
->bus
->read(map
->dev
, map
->work_buf
, map
->format
.reg_bytes
,
482 trace_regmap_hw_read_done(map
->dev
, reg
,
483 val_len
/ map
->format
.val_bytes
);
488 static int _regmap_read(struct regmap
*map
, unsigned int reg
,
493 if (!map
->cache_bypass
) {
494 ret
= regcache_read(map
, reg
, val
);
499 if (!map
->format
.parse_val
)
505 ret
= _regmap_raw_read(map
, reg
, map
->work_buf
, map
->format
.val_bytes
);
507 *val
= map
->format
.parse_val(map
->work_buf
);
508 trace_regmap_reg_read(map
->dev
, reg
, *val
);
515 * regmap_read(): Read a value from a single register
517 * @map: Register map to write to
518 * @reg: Register to be read from
519 * @val: Pointer to store read value
521 * A value of zero will be returned on success, a negative errno will
522 * be returned in error cases.
524 int regmap_read(struct regmap
*map
, unsigned int reg
, unsigned int *val
)
528 mutex_lock(&map
->lock
);
530 ret
= _regmap_read(map
, reg
, val
);
532 mutex_unlock(&map
->lock
);
536 EXPORT_SYMBOL_GPL(regmap_read
);
539 * regmap_raw_read(): Read raw data from the device
541 * @map: Register map to write to
542 * @reg: First register to be read from
543 * @val: Pointer to store read value
544 * @val_len: Size of data to read
546 * A value of zero will be returned on success, a negative errno will
547 * be returned in error cases.
549 int regmap_raw_read(struct regmap
*map
, unsigned int reg
, void *val
,
552 size_t val_count
= val_len
/ map
->format
.val_bytes
;
555 WARN_ON(!regmap_volatile_range(map
, reg
, val_count
) &&
556 map
->cache_type
!= REGCACHE_NONE
);
558 mutex_lock(&map
->lock
);
560 ret
= _regmap_raw_read(map
, reg
, val
, val_len
);
562 mutex_unlock(&map
->lock
);
566 EXPORT_SYMBOL_GPL(regmap_raw_read
);
569 * regmap_bulk_read(): Read multiple registers from the device
571 * @map: Register map to write to
572 * @reg: First register to be read from
573 * @val: Pointer to store read value, in native register size for device
574 * @val_count: Number of registers to read
576 * A value of zero will be returned on success, a negative errno will
577 * be returned in error cases.
579 int regmap_bulk_read(struct regmap
*map
, unsigned int reg
, void *val
,
583 size_t val_bytes
= map
->format
.val_bytes
;
584 bool vol
= regmap_volatile_range(map
, reg
, val_count
);
586 if (!map
->format
.parse_val
)
589 if (vol
|| map
->cache_type
== REGCACHE_NONE
) {
590 ret
= regmap_raw_read(map
, reg
, val
, val_bytes
* val_count
);
594 for (i
= 0; i
< val_count
* val_bytes
; i
+= val_bytes
)
595 map
->format
.parse_val(val
+ i
);
597 for (i
= 0; i
< val_count
; i
++) {
598 ret
= regmap_read(map
, reg
+ i
, val
+ (i
* val_bytes
));
606 EXPORT_SYMBOL_GPL(regmap_bulk_read
);
608 static int _regmap_update_bits(struct regmap
*map
, unsigned int reg
,
609 unsigned int mask
, unsigned int val
,
613 unsigned int tmp
, orig
;
615 mutex_lock(&map
->lock
);
617 ret
= _regmap_read(map
, reg
, &orig
);
625 ret
= _regmap_write(map
, reg
, tmp
);
632 mutex_unlock(&map
->lock
);
638 * regmap_update_bits: Perform a read/modify/write cycle on the register map
640 * @map: Register map to update
641 * @reg: Register to update
642 * @mask: Bitmask to change
643 * @val: New value for bitmask
645 * Returns zero for success, a negative number on error.
647 int regmap_update_bits(struct regmap
*map
, unsigned int reg
,
648 unsigned int mask
, unsigned int val
)
651 return _regmap_update_bits(map
, reg
, mask
, val
, &change
);
653 EXPORT_SYMBOL_GPL(regmap_update_bits
);
656 * regmap_update_bits_check: Perform a read/modify/write cycle on the
657 * register map and report if updated
659 * @map: Register map to update
660 * @reg: Register to update
661 * @mask: Bitmask to change
662 * @val: New value for bitmask
663 * @change: Boolean indicating if a write was done
665 * Returns zero for success, a negative number on error.
667 int regmap_update_bits_check(struct regmap
*map
, unsigned int reg
,
668 unsigned int mask
, unsigned int val
,
671 return _regmap_update_bits(map
, reg
, mask
, val
, change
);
673 EXPORT_SYMBOL_GPL(regmap_update_bits_check
);
675 static int __init
regmap_initcall(void)
677 regmap_debugfs_initcall();
681 postcore_initcall(regmap_initcall
);