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 void regmap_format_4_12_write(struct regmap
*map
,
68 unsigned int reg
, unsigned int val
)
70 __be16
*out
= map
->work_buf
;
71 *out
= cpu_to_be16((reg
<< 12) | val
);
74 static void regmap_format_7_9_write(struct regmap
*map
,
75 unsigned int reg
, unsigned int val
)
77 __be16
*out
= map
->work_buf
;
78 *out
= cpu_to_be16((reg
<< 9) | val
);
81 static void regmap_format_8(void *buf
, unsigned int val
)
88 static void regmap_format_16(void *buf
, unsigned int val
)
92 b
[0] = cpu_to_be16(val
);
95 static unsigned int regmap_parse_8(void *buf
)
102 static unsigned int regmap_parse_16(void *buf
)
106 b
[0] = be16_to_cpu(b
[0]);
112 * regmap_init(): Initialise register map
114 * @dev: Device that will be interacted with
115 * @bus: Bus-specific callbacks to use with device
116 * @config: Configuration for register map
118 * The return value will be an ERR_PTR() on error or a valid pointer to
119 * a struct regmap. This function should generally not be called
120 * directly, it should be called by bus-specific init functions.
122 struct regmap
*regmap_init(struct device
*dev
,
123 const struct regmap_bus
*bus
,
124 const struct regmap_config
*config
)
132 map
= kzalloc(sizeof(*map
), GFP_KERNEL
);
138 mutex_init(&map
->lock
);
139 map
->format
.buf_size
= (config
->reg_bits
+ config
->val_bits
) / 8;
140 map
->format
.reg_bytes
= config
->reg_bits
/ 8;
141 map
->format
.val_bytes
= config
->val_bits
/ 8;
144 map
->max_register
= config
->max_register
;
145 map
->writeable_reg
= config
->writeable_reg
;
146 map
->readable_reg
= config
->readable_reg
;
147 map
->volatile_reg
= config
->volatile_reg
;
148 map
->precious_reg
= config
->precious_reg
;
150 switch (config
->reg_bits
) {
152 switch (config
->val_bits
) {
154 map
->format
.format_write
= regmap_format_4_12_write
;
162 switch (config
->val_bits
) {
164 map
->format
.format_write
= regmap_format_7_9_write
;
172 map
->format
.format_reg
= regmap_format_8
;
176 map
->format
.format_reg
= regmap_format_16
;
183 switch (config
->val_bits
) {
185 map
->format
.format_val
= regmap_format_8
;
186 map
->format
.parse_val
= regmap_parse_8
;
189 map
->format
.format_val
= regmap_format_16
;
190 map
->format
.parse_val
= regmap_parse_16
;
194 if (!map
->format
.format_write
&&
195 !(map
->format
.format_reg
&& map
->format
.format_val
))
198 map
->work_buf
= kmalloc(map
->format
.buf_size
, GFP_KERNEL
);
199 if (map
->work_buf
== NULL
) {
204 regmap_debugfs_init(map
);
209 module_put(map
->bus
->owner
);
215 EXPORT_SYMBOL_GPL(regmap_init
);
218 * regmap_exit(): Free a previously allocated register map
220 void regmap_exit(struct regmap
*map
)
222 regmap_debugfs_exit(map
);
223 kfree(map
->work_buf
);
224 module_put(map
->bus
->owner
);
227 EXPORT_SYMBOL_GPL(regmap_exit
);
229 static int _regmap_raw_write(struct regmap
*map
, unsigned int reg
,
230 const void *val
, size_t val_len
)
237 /* Check for unwritable registers before we start */
238 if (map
->writeable_reg
)
239 for (i
= 0; i
< val_len
/ map
->format
.val_bytes
; i
++)
240 if (!map
->writeable_reg(map
->dev
, reg
+ i
))
243 map
->format
.format_reg(map
->work_buf
, reg
);
245 trace_regmap_hw_write_start(map
->dev
, reg
,
246 val_len
/ map
->format
.val_bytes
);
248 /* If we're doing a single register write we can probably just
249 * send the work_buf directly, otherwise try to do a gather
252 if (val
== map
->work_buf
+ map
->format
.reg_bytes
)
253 ret
= map
->bus
->write(map
->dev
, map
->work_buf
,
254 map
->format
.reg_bytes
+ val_len
);
255 else if (map
->bus
->gather_write
)
256 ret
= map
->bus
->gather_write(map
->dev
, map
->work_buf
,
257 map
->format
.reg_bytes
,
260 /* If that didn't work fall back on linearising by hand. */
261 if (ret
== -ENOTSUPP
) {
262 len
= map
->format
.reg_bytes
+ val_len
;
263 buf
= kmalloc(len
, GFP_KERNEL
);
267 memcpy(buf
, map
->work_buf
, map
->format
.reg_bytes
);
268 memcpy(buf
+ map
->format
.reg_bytes
, val
, val_len
);
269 ret
= map
->bus
->write(map
->dev
, buf
, len
);
274 trace_regmap_hw_write_done(map
->dev
, reg
,
275 val_len
/ map
->format
.val_bytes
);
280 static int _regmap_write(struct regmap
*map
, unsigned int reg
,
284 BUG_ON(!map
->format
.format_write
&& !map
->format
.format_val
);
286 trace_regmap_reg_write(map
->dev
, reg
, val
);
288 if (map
->format
.format_write
) {
289 map
->format
.format_write(map
, reg
, val
);
291 trace_regmap_hw_write_start(map
->dev
, reg
, 1);
293 ret
= map
->bus
->write(map
->dev
, map
->work_buf
,
294 map
->format
.buf_size
);
296 trace_regmap_hw_write_done(map
->dev
, reg
, 1);
300 map
->format
.format_val(map
->work_buf
+ map
->format
.reg_bytes
,
302 return _regmap_raw_write(map
, reg
,
303 map
->work_buf
+ map
->format
.reg_bytes
,
304 map
->format
.val_bytes
);
309 * regmap_write(): Write a value to a single register
311 * @map: Register map to write to
312 * @reg: Register to write to
313 * @val: Value to be written
315 * A value of zero will be returned on success, a negative errno will
316 * be returned in error cases.
318 int regmap_write(struct regmap
*map
, unsigned int reg
, unsigned int val
)
322 mutex_lock(&map
->lock
);
324 ret
= _regmap_write(map
, reg
, val
);
326 mutex_unlock(&map
->lock
);
330 EXPORT_SYMBOL_GPL(regmap_write
);
333 * regmap_raw_write(): Write raw values to one or more registers
335 * @map: Register map to write to
336 * @reg: Initial register to write to
337 * @val: Block of data to be written, laid out for direct transmission to the
339 * @val_len: Length of data pointed to by val.
341 * This function is intended to be used for things like firmware
342 * download where a large block of data needs to be transferred to the
343 * device. No formatting will be done on the data provided.
345 * A value of zero will be returned on success, a negative errno will
346 * be returned in error cases.
348 int regmap_raw_write(struct regmap
*map
, unsigned int reg
,
349 const void *val
, size_t val_len
)
353 mutex_lock(&map
->lock
);
355 ret
= _regmap_raw_write(map
, reg
, val
, val_len
);
357 mutex_unlock(&map
->lock
);
361 EXPORT_SYMBOL_GPL(regmap_raw_write
);
363 static int _regmap_raw_read(struct regmap
*map
, unsigned int reg
, void *val
,
364 unsigned int val_len
)
366 u8
*u8
= map
->work_buf
;
369 map
->format
.format_reg(map
->work_buf
, reg
);
372 * Some buses flag reads by setting the high bits in the
373 * register addresss; since it's always the high bits for all
374 * current formats we can do this here rather than in
375 * formatting. This may break if we get interesting formats.
377 if (map
->bus
->read_flag_mask
)
378 u8
[0] |= map
->bus
->read_flag_mask
;
380 trace_regmap_hw_read_start(map
->dev
, reg
,
381 val_len
/ map
->format
.val_bytes
);
383 ret
= map
->bus
->read(map
->dev
, map
->work_buf
, map
->format
.reg_bytes
,
386 trace_regmap_hw_read_done(map
->dev
, reg
,
387 val_len
/ map
->format
.val_bytes
);
392 static int _regmap_read(struct regmap
*map
, unsigned int reg
,
397 if (!map
->format
.parse_val
)
400 ret
= _regmap_raw_read(map
, reg
, map
->work_buf
, map
->format
.val_bytes
);
402 *val
= map
->format
.parse_val(map
->work_buf
);
403 trace_regmap_reg_read(map
->dev
, reg
, *val
);
410 * regmap_read(): Read a value from a single register
412 * @map: Register map to write to
413 * @reg: Register to be read from
414 * @val: Pointer to store read value
416 * A value of zero will be returned on success, a negative errno will
417 * be returned in error cases.
419 int regmap_read(struct regmap
*map
, unsigned int reg
, unsigned int *val
)
423 mutex_lock(&map
->lock
);
425 ret
= _regmap_read(map
, reg
, val
);
427 mutex_unlock(&map
->lock
);
431 EXPORT_SYMBOL_GPL(regmap_read
);
434 * regmap_raw_read(): Read raw data from the device
436 * @map: Register map to write to
437 * @reg: First register to be read from
438 * @val: Pointer to store read value
439 * @val_len: Size of data to read
441 * A value of zero will be returned on success, a negative errno will
442 * be returned in error cases.
444 int regmap_raw_read(struct regmap
*map
, unsigned int reg
, void *val
,
449 mutex_lock(&map
->lock
);
451 ret
= _regmap_raw_read(map
, reg
, val
, val_len
);
453 mutex_unlock(&map
->lock
);
457 EXPORT_SYMBOL_GPL(regmap_raw_read
);
460 * regmap_bulk_read(): Read multiple registers from the device
462 * @map: Register map to write to
463 * @reg: First register to be read from
464 * @val: Pointer to store read value, in native register size for device
465 * @val_count: Number of registers to read
467 * A value of zero will be returned on success, a negative errno will
468 * be returned in error cases.
470 int regmap_bulk_read(struct regmap
*map
, unsigned int reg
, void *val
,
474 size_t val_bytes
= map
->format
.val_bytes
;
476 if (!map
->format
.parse_val
)
479 ret
= regmap_raw_read(map
, reg
, val
, val_bytes
* val_count
);
483 for (i
= 0; i
< val_count
* val_bytes
; i
+= val_bytes
)
484 map
->format
.parse_val(val
+ i
);
488 EXPORT_SYMBOL_GPL(regmap_bulk_read
);
491 * remap_update_bits: Perform a read/modify/write cycle on the register map
493 * @map: Register map to update
494 * @reg: Register to update
495 * @mask: Bitmask to change
496 * @val: New value for bitmask
498 * Returns zero for success, a negative number on error.
500 int regmap_update_bits(struct regmap
*map
, unsigned int reg
,
501 unsigned int mask
, unsigned int val
)
506 mutex_lock(&map
->lock
);
508 ret
= _regmap_read(map
, reg
, &tmp
);
515 ret
= _regmap_write(map
, reg
, tmp
);
518 mutex_unlock(&map
->lock
);
522 EXPORT_SYMBOL_GPL(regmap_update_bits
);
524 static int __init
regmap_initcall(void)
526 regmap_debugfs_initcall();
530 postcore_initcall(regmap_initcall
);