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
.pad_bytes
= config
->pad_bits
/ 8;
164 map
->format
.val_bytes
= config
->val_bits
/ 8;
165 map
->format
.buf_size
+= map
->format
.pad_bytes
;
168 map
->max_register
= config
->max_register
;
169 map
->writeable_reg
= config
->writeable_reg
;
170 map
->readable_reg
= config
->readable_reg
;
171 map
->volatile_reg
= config
->volatile_reg
;
172 map
->precious_reg
= config
->precious_reg
;
173 map
->cache_type
= config
->cache_type
;
175 if (config
->read_flag_mask
|| config
->write_flag_mask
) {
176 map
->read_flag_mask
= config
->read_flag_mask
;
177 map
->write_flag_mask
= config
->write_flag_mask
;
179 map
->read_flag_mask
= bus
->read_flag_mask
;
182 switch (config
->reg_bits
) {
184 switch (config
->val_bits
) {
186 map
->format
.format_write
= regmap_format_4_12_write
;
194 switch (config
->val_bits
) {
196 map
->format
.format_write
= regmap_format_7_9_write
;
204 switch (config
->val_bits
) {
206 map
->format
.format_write
= regmap_format_10_14_write
;
214 map
->format
.format_reg
= regmap_format_8
;
218 map
->format
.format_reg
= regmap_format_16
;
225 switch (config
->val_bits
) {
227 map
->format
.format_val
= regmap_format_8
;
228 map
->format
.parse_val
= regmap_parse_8
;
231 map
->format
.format_val
= regmap_format_16
;
232 map
->format
.parse_val
= regmap_parse_16
;
236 if (!map
->format
.format_write
&&
237 !(map
->format
.format_reg
&& map
->format
.format_val
))
240 map
->work_buf
= kzalloc(map
->format
.buf_size
, GFP_KERNEL
);
241 if (map
->work_buf
== NULL
) {
246 regmap_debugfs_init(map
);
248 ret
= regcache_init(map
, config
);
250 goto err_free_workbuf
;
255 kfree(map
->work_buf
);
261 EXPORT_SYMBOL_GPL(regmap_init
);
264 * regmap_reinit_cache(): Reinitialise the current register cache
266 * @map: Register map to operate on.
267 * @config: New configuration. Only the cache data will be used.
269 * Discard any existing register cache for the map and initialize a
270 * new cache. This can be used to restore the cache to defaults or to
271 * update the cache configuration to reflect runtime discovery of the
274 int regmap_reinit_cache(struct regmap
*map
, const struct regmap_config
*config
)
278 mutex_lock(&map
->lock
);
282 map
->max_register
= config
->max_register
;
283 map
->writeable_reg
= config
->writeable_reg
;
284 map
->readable_reg
= config
->readable_reg
;
285 map
->volatile_reg
= config
->volatile_reg
;
286 map
->precious_reg
= config
->precious_reg
;
287 map
->cache_type
= config
->cache_type
;
289 ret
= regcache_init(map
, config
);
291 mutex_unlock(&map
->lock
);
297 * regmap_exit(): Free a previously allocated register map
299 void regmap_exit(struct regmap
*map
)
302 regmap_debugfs_exit(map
);
303 kfree(map
->work_buf
);
306 EXPORT_SYMBOL_GPL(regmap_exit
);
308 static int _regmap_raw_write(struct regmap
*map
, unsigned int reg
,
309 const void *val
, size_t val_len
)
311 u8
*u8
= map
->work_buf
;
317 /* Check for unwritable registers before we start */
318 if (map
->writeable_reg
)
319 for (i
= 0; i
< val_len
/ map
->format
.val_bytes
; i
++)
320 if (!map
->writeable_reg(map
->dev
, reg
+ i
))
323 map
->format
.format_reg(map
->work_buf
, reg
);
325 u8
[0] |= map
->write_flag_mask
;
327 trace_regmap_hw_write_start(map
->dev
, reg
,
328 val_len
/ map
->format
.val_bytes
);
330 /* If we're doing a single register write we can probably just
331 * send the work_buf directly, otherwise try to do a gather
334 if (val
== (map
->work_buf
+ map
->format
.pad_bytes
+
335 map
->format
.reg_bytes
))
336 ret
= map
->bus
->write(map
->dev
, map
->work_buf
,
337 map
->format
.reg_bytes
+
338 map
->format
.pad_bytes
+
340 else if (map
->bus
->gather_write
)
341 ret
= map
->bus
->gather_write(map
->dev
, map
->work_buf
,
342 map
->format
.reg_bytes
+
343 map
->format
.pad_bytes
,
346 /* If that didn't work fall back on linearising by hand. */
347 if (ret
== -ENOTSUPP
) {
348 len
= map
->format
.reg_bytes
+ map
->format
.pad_bytes
+ val_len
;
349 buf
= kzalloc(len
, GFP_KERNEL
);
353 memcpy(buf
, map
->work_buf
, map
->format
.reg_bytes
);
354 memcpy(buf
+ map
->format
.reg_bytes
+ map
->format
.pad_bytes
,
356 ret
= map
->bus
->write(map
->dev
, buf
, len
);
361 trace_regmap_hw_write_done(map
->dev
, reg
,
362 val_len
/ map
->format
.val_bytes
);
367 int _regmap_write(struct regmap
*map
, unsigned int reg
,
371 BUG_ON(!map
->format
.format_write
&& !map
->format
.format_val
);
373 if (!map
->cache_bypass
) {
374 ret
= regcache_write(map
, reg
, val
);
377 if (map
->cache_only
) {
378 map
->cache_dirty
= true;
383 trace_regmap_reg_write(map
->dev
, reg
, val
);
385 if (map
->format
.format_write
) {
386 map
->format
.format_write(map
, reg
, val
);
388 trace_regmap_hw_write_start(map
->dev
, reg
, 1);
390 ret
= map
->bus
->write(map
->dev
, map
->work_buf
,
391 map
->format
.buf_size
);
393 trace_regmap_hw_write_done(map
->dev
, reg
, 1);
397 map
->format
.format_val(map
->work_buf
+ map
->format
.reg_bytes
398 + map
->format
.pad_bytes
, val
);
399 return _regmap_raw_write(map
, reg
,
401 map
->format
.reg_bytes
+
402 map
->format
.pad_bytes
,
403 map
->format
.val_bytes
);
408 * regmap_write(): Write a value to a single register
410 * @map: Register map to write to
411 * @reg: Register to write to
412 * @val: Value to be written
414 * A value of zero will be returned on success, a negative errno will
415 * be returned in error cases.
417 int regmap_write(struct regmap
*map
, unsigned int reg
, unsigned int val
)
421 mutex_lock(&map
->lock
);
423 ret
= _regmap_write(map
, reg
, val
);
425 mutex_unlock(&map
->lock
);
429 EXPORT_SYMBOL_GPL(regmap_write
);
432 * regmap_raw_write(): Write raw values to one or more registers
434 * @map: Register map to write to
435 * @reg: Initial register to write to
436 * @val: Block of data to be written, laid out for direct transmission to the
438 * @val_len: Length of data pointed to by val.
440 * This function is intended to be used for things like firmware
441 * download where a large block of data needs to be transferred to the
442 * device. No formatting will be done on the data provided.
444 * A value of zero will be returned on success, a negative errno will
445 * be returned in error cases.
447 int regmap_raw_write(struct regmap
*map
, unsigned int reg
,
448 const void *val
, size_t val_len
)
450 size_t val_count
= val_len
/ map
->format
.val_bytes
;
453 WARN_ON(!regmap_volatile_range(map
, reg
, val_count
) &&
454 map
->cache_type
!= REGCACHE_NONE
);
456 mutex_lock(&map
->lock
);
458 ret
= _regmap_raw_write(map
, reg
, val
, val_len
);
460 mutex_unlock(&map
->lock
);
464 EXPORT_SYMBOL_GPL(regmap_raw_write
);
466 static int _regmap_raw_read(struct regmap
*map
, unsigned int reg
, void *val
,
467 unsigned int val_len
)
469 u8
*u8
= map
->work_buf
;
472 map
->format
.format_reg(map
->work_buf
, reg
);
475 * Some buses or devices flag reads by setting the high bits in the
476 * register addresss; since it's always the high bits for all
477 * current formats we can do this here rather than in
478 * formatting. This may break if we get interesting formats.
480 u8
[0] |= map
->read_flag_mask
;
482 trace_regmap_hw_read_start(map
->dev
, reg
,
483 val_len
/ map
->format
.val_bytes
);
485 ret
= map
->bus
->read(map
->dev
, map
->work_buf
,
486 map
->format
.reg_bytes
+ map
->format
.pad_bytes
,
489 trace_regmap_hw_read_done(map
->dev
, reg
,
490 val_len
/ map
->format
.val_bytes
);
495 static int _regmap_read(struct regmap
*map
, unsigned int reg
,
500 if (!map
->cache_bypass
) {
501 ret
= regcache_read(map
, reg
, val
);
506 if (!map
->format
.parse_val
)
512 ret
= _regmap_raw_read(map
, reg
, map
->work_buf
, map
->format
.val_bytes
);
514 *val
= map
->format
.parse_val(map
->work_buf
);
515 trace_regmap_reg_read(map
->dev
, reg
, *val
);
522 * regmap_read(): Read a value from a single register
524 * @map: Register map to write to
525 * @reg: Register to be read from
526 * @val: Pointer to store read value
528 * A value of zero will be returned on success, a negative errno will
529 * be returned in error cases.
531 int regmap_read(struct regmap
*map
, unsigned int reg
, unsigned int *val
)
535 mutex_lock(&map
->lock
);
537 ret
= _regmap_read(map
, reg
, val
);
539 mutex_unlock(&map
->lock
);
543 EXPORT_SYMBOL_GPL(regmap_read
);
546 * regmap_raw_read(): Read raw data from the device
548 * @map: Register map to write to
549 * @reg: First register to be read from
550 * @val: Pointer to store read value
551 * @val_len: Size of data to read
553 * A value of zero will be returned on success, a negative errno will
554 * be returned in error cases.
556 int regmap_raw_read(struct regmap
*map
, unsigned int reg
, void *val
,
559 size_t val_count
= val_len
/ map
->format
.val_bytes
;
562 WARN_ON(!regmap_volatile_range(map
, reg
, val_count
) &&
563 map
->cache_type
!= REGCACHE_NONE
);
565 mutex_lock(&map
->lock
);
567 ret
= _regmap_raw_read(map
, reg
, val
, val_len
);
569 mutex_unlock(&map
->lock
);
573 EXPORT_SYMBOL_GPL(regmap_raw_read
);
576 * regmap_bulk_read(): Read multiple registers from the device
578 * @map: Register map to write to
579 * @reg: First register to be read from
580 * @val: Pointer to store read value, in native register size for device
581 * @val_count: Number of registers to read
583 * A value of zero will be returned on success, a negative errno will
584 * be returned in error cases.
586 int regmap_bulk_read(struct regmap
*map
, unsigned int reg
, void *val
,
590 size_t val_bytes
= map
->format
.val_bytes
;
591 bool vol
= regmap_volatile_range(map
, reg
, val_count
);
593 if (!map
->format
.parse_val
)
596 if (vol
|| map
->cache_type
== REGCACHE_NONE
) {
597 ret
= regmap_raw_read(map
, reg
, val
, val_bytes
* val_count
);
601 for (i
= 0; i
< val_count
* val_bytes
; i
+= val_bytes
)
602 map
->format
.parse_val(val
+ i
);
604 for (i
= 0; i
< val_count
; i
++) {
605 ret
= regmap_read(map
, reg
+ i
, val
+ (i
* val_bytes
));
613 EXPORT_SYMBOL_GPL(regmap_bulk_read
);
615 static int _regmap_update_bits(struct regmap
*map
, unsigned int reg
,
616 unsigned int mask
, unsigned int val
,
620 unsigned int tmp
, orig
;
622 mutex_lock(&map
->lock
);
624 ret
= _regmap_read(map
, reg
, &orig
);
632 ret
= _regmap_write(map
, reg
, tmp
);
639 mutex_unlock(&map
->lock
);
645 * regmap_update_bits: Perform a read/modify/write cycle on the register map
647 * @map: Register map to update
648 * @reg: Register to update
649 * @mask: Bitmask to change
650 * @val: New value for bitmask
652 * Returns zero for success, a negative number on error.
654 int regmap_update_bits(struct regmap
*map
, unsigned int reg
,
655 unsigned int mask
, unsigned int val
)
658 return _regmap_update_bits(map
, reg
, mask
, val
, &change
);
660 EXPORT_SYMBOL_GPL(regmap_update_bits
);
663 * regmap_update_bits_check: Perform a read/modify/write cycle on the
664 * register map and report if updated
666 * @map: Register map to update
667 * @reg: Register to update
668 * @mask: Bitmask to change
669 * @val: New value for bitmask
670 * @change: Boolean indicating if a write was done
672 * Returns zero for success, a negative number on error.
674 int regmap_update_bits_check(struct regmap
*map
, unsigned int reg
,
675 unsigned int mask
, unsigned int val
,
678 return _regmap_update_bits(map
, reg
, mask
, val
, change
);
680 EXPORT_SYMBOL_GPL(regmap_update_bits_check
);
682 static int __init
regmap_initcall(void)
684 regmap_debugfs_initcall();
688 postcore_initcall(regmap_initcall
);