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 #include <linux/regmap.h>
20 #define CREATE_TRACE_POINTS
21 #include <trace/events/regmap.h>
25 struct regmap_format
{
29 void (*format_write
)(struct regmap
*map
,
30 unsigned int reg
, unsigned int val
);
31 void (*format_reg
)(void *buf
, unsigned int reg
);
32 void (*format_val
)(void *buf
, unsigned int val
);
33 unsigned int (*parse_val
)(void *buf
);
39 struct device
*dev
; /* Device we do I/O on */
40 void *work_buf
; /* Scratch buffer used to format I/O */
41 struct regmap_format format
; /* Buffer format */
42 const struct regmap_bus
*bus
;
44 unsigned int max_register
;
45 bool (*writeable_reg
)(struct device
*dev
, unsigned int reg
);
46 bool (*readable_reg
)(struct device
*dev
, unsigned int reg
);
47 bool (*volatile_reg
)(struct device
*dev
, unsigned int reg
);
50 static void regmap_format_4_12_write(struct regmap
*map
,
51 unsigned int reg
, unsigned int val
)
53 __be16
*out
= map
->work_buf
;
54 *out
= cpu_to_be16((reg
<< 12) | val
);
57 static void regmap_format_7_9_write(struct regmap
*map
,
58 unsigned int reg
, unsigned int val
)
60 __be16
*out
= map
->work_buf
;
61 *out
= cpu_to_be16((reg
<< 9) | val
);
64 static void regmap_format_8(void *buf
, unsigned int val
)
71 static void regmap_format_16(void *buf
, unsigned int val
)
75 b
[0] = cpu_to_be16(val
);
78 static unsigned int regmap_parse_8(void *buf
)
85 static unsigned int regmap_parse_16(void *buf
)
89 b
[0] = be16_to_cpu(b
[0]);
95 * regmap_init(): Initialise register map
97 * @dev: Device that will be interacted with
98 * @bus: Bus-specific callbacks to use with device
99 * @config: Configuration for register map
101 * The return value will be an ERR_PTR() on error or a valid pointer to
102 * a struct regmap. This function should generally not be called
103 * directly, it should be called by bus-specific init functions.
105 struct regmap
*regmap_init(struct device
*dev
,
106 const struct regmap_bus
*bus
,
107 const struct regmap_config
*config
)
115 map
= kzalloc(sizeof(*map
), GFP_KERNEL
);
121 mutex_init(&map
->lock
);
122 map
->format
.buf_size
= (config
->reg_bits
+ config
->val_bits
) / 8;
123 map
->format
.reg_bytes
= config
->reg_bits
/ 8;
124 map
->format
.val_bytes
= config
->val_bits
/ 8;
127 map
->max_register
= config
->max_register
;
128 map
->writeable_reg
= config
->writeable_reg
;
129 map
->readable_reg
= config
->readable_reg
;
130 map
->volatile_reg
= config
->volatile_reg
;
132 switch (config
->reg_bits
) {
134 switch (config
->val_bits
) {
136 map
->format
.format_write
= regmap_format_4_12_write
;
144 switch (config
->val_bits
) {
146 map
->format
.format_write
= regmap_format_7_9_write
;
154 map
->format
.format_reg
= regmap_format_8
;
158 map
->format
.format_reg
= regmap_format_16
;
165 switch (config
->val_bits
) {
167 map
->format
.format_val
= regmap_format_8
;
168 map
->format
.parse_val
= regmap_parse_8
;
171 map
->format
.format_val
= regmap_format_16
;
172 map
->format
.parse_val
= regmap_parse_16
;
176 if (!map
->format
.format_write
&&
177 !(map
->format
.format_reg
&& map
->format
.format_val
))
180 map
->work_buf
= kmalloc(map
->format
.buf_size
, GFP_KERNEL
);
181 if (map
->work_buf
== NULL
) {
189 module_put(map
->bus
->owner
);
195 EXPORT_SYMBOL_GPL(regmap_init
);
198 * regmap_exit(): Free a previously allocated register map
200 void regmap_exit(struct regmap
*map
)
202 kfree(map
->work_buf
);
203 module_put(map
->bus
->owner
);
206 EXPORT_SYMBOL_GPL(regmap_exit
);
208 static int _regmap_raw_write(struct regmap
*map
, unsigned int reg
,
209 const void *val
, size_t val_len
)
215 map
->format
.format_reg(map
->work_buf
, reg
);
217 trace_regmap_hw_write_start(map
->dev
, reg
,
218 val_len
/ map
->format
.val_bytes
);
220 /* If we're doing a single register write we can probably just
221 * send the work_buf directly, otherwise try to do a gather
224 if (val
== map
->work_buf
+ map
->format
.reg_bytes
)
225 ret
= map
->bus
->write(map
->dev
, map
->work_buf
,
226 map
->format
.reg_bytes
+ val_len
);
227 else if (map
->bus
->gather_write
)
228 ret
= map
->bus
->gather_write(map
->dev
, map
->work_buf
,
229 map
->format
.reg_bytes
,
232 /* If that didn't work fall back on linearising by hand. */
233 if (ret
== -ENOTSUPP
) {
234 len
= map
->format
.reg_bytes
+ val_len
;
235 buf
= kmalloc(len
, GFP_KERNEL
);
239 memcpy(buf
, map
->work_buf
, map
->format
.reg_bytes
);
240 memcpy(buf
+ map
->format
.reg_bytes
, val
, val_len
);
241 ret
= map
->bus
->write(map
->dev
, buf
, len
);
246 trace_regmap_hw_write_done(map
->dev
, reg
,
247 val_len
/ map
->format
.val_bytes
);
252 static int _regmap_write(struct regmap
*map
, unsigned int reg
,
256 BUG_ON(!map
->format
.format_write
&& !map
->format
.format_val
);
258 trace_regmap_reg_write(map
->dev
, reg
, val
);
260 if (map
->format
.format_write
) {
261 map
->format
.format_write(map
, reg
, val
);
263 trace_regmap_hw_write_start(map
->dev
, reg
, 1);
265 ret
= map
->bus
->write(map
->dev
, map
->work_buf
,
266 map
->format
.buf_size
);
268 trace_regmap_hw_write_done(map
->dev
, reg
, 1);
272 map
->format
.format_val(map
->work_buf
+ map
->format
.reg_bytes
,
274 return _regmap_raw_write(map
, reg
,
275 map
->work_buf
+ map
->format
.reg_bytes
,
276 map
->format
.val_bytes
);
281 * regmap_write(): Write a value to a single register
283 * @map: Register map to write to
284 * @reg: Register to write to
285 * @val: Value to be written
287 * A value of zero will be returned on success, a negative errno will
288 * be returned in error cases.
290 int regmap_write(struct regmap
*map
, unsigned int reg
, unsigned int val
)
294 mutex_lock(&map
->lock
);
296 ret
= _regmap_write(map
, reg
, val
);
298 mutex_unlock(&map
->lock
);
302 EXPORT_SYMBOL_GPL(regmap_write
);
305 * regmap_raw_write(): Write raw values to one or more registers
307 * @map: Register map to write to
308 * @reg: Initial register to write to
309 * @val: Block of data to be written, laid out for direct transmission to the
311 * @val_len: Length of data pointed to by val.
313 * This function is intended to be used for things like firmware
314 * download where a large block of data needs to be transferred to the
315 * device. No formatting will be done on the data provided.
317 * A value of zero will be returned on success, a negative errno will
318 * be returned in error cases.
320 int regmap_raw_write(struct regmap
*map
, unsigned int reg
,
321 const void *val
, size_t val_len
)
325 mutex_lock(&map
->lock
);
327 ret
= _regmap_raw_write(map
, reg
, val
, val_len
);
329 mutex_unlock(&map
->lock
);
333 EXPORT_SYMBOL_GPL(regmap_raw_write
);
335 static int _regmap_raw_read(struct regmap
*map
, unsigned int reg
, void *val
,
336 unsigned int val_len
)
338 u8
*u8
= map
->work_buf
;
341 map
->format
.format_reg(map
->work_buf
, reg
);
344 * Some buses flag reads by setting the high bits in the
345 * register addresss; since it's always the high bits for all
346 * current formats we can do this here rather than in
347 * formatting. This may break if we get interesting formats.
349 if (map
->bus
->read_flag_mask
)
350 u8
[0] |= map
->bus
->read_flag_mask
;
352 trace_regmap_hw_read_start(map
->dev
, reg
,
353 val_len
/ map
->format
.val_bytes
);
355 ret
= map
->bus
->read(map
->dev
, map
->work_buf
, map
->format
.reg_bytes
,
358 trace_regmap_hw_read_done(map
->dev
, reg
,
359 val_len
/ map
->format
.val_bytes
);
364 static int _regmap_read(struct regmap
*map
, unsigned int reg
,
369 if (!map
->format
.parse_val
)
372 ret
= _regmap_raw_read(map
, reg
, map
->work_buf
, map
->format
.val_bytes
);
374 *val
= map
->format
.parse_val(map
->work_buf
);
375 trace_regmap_reg_read(map
->dev
, reg
, *val
);
382 * regmap_read(): Read a value from a single register
384 * @map: Register map to write to
385 * @reg: Register to be read from
386 * @val: Pointer to store read value
388 * A value of zero will be returned on success, a negative errno will
389 * be returned in error cases.
391 int regmap_read(struct regmap
*map
, unsigned int reg
, unsigned int *val
)
395 mutex_lock(&map
->lock
);
397 ret
= _regmap_read(map
, reg
, val
);
399 mutex_unlock(&map
->lock
);
403 EXPORT_SYMBOL_GPL(regmap_read
);
406 * regmap_raw_read(): Read raw data from the device
408 * @map: Register map to write to
409 * @reg: First register to be read from
410 * @val: Pointer to store read value
411 * @val_len: Size of data to read
413 * A value of zero will be returned on success, a negative errno will
414 * be returned in error cases.
416 int regmap_raw_read(struct regmap
*map
, unsigned int reg
, void *val
,
421 mutex_lock(&map
->lock
);
423 ret
= _regmap_raw_read(map
, reg
, val
, val_len
);
425 mutex_unlock(&map
->lock
);
429 EXPORT_SYMBOL_GPL(regmap_raw_read
);
432 * regmap_bulk_read(): Read multiple registers from the device
434 * @map: Register map to write to
435 * @reg: First register to be read from
436 * @val: Pointer to store read value, in native register size for device
437 * @val_count: Number of registers to read
439 * A value of zero will be returned on success, a negative errno will
440 * be returned in error cases.
442 int regmap_bulk_read(struct regmap
*map
, unsigned int reg
, void *val
,
446 size_t val_bytes
= map
->format
.val_bytes
;
448 if (!map
->format
.parse_val
)
451 ret
= regmap_raw_read(map
, reg
, val
, val_bytes
* val_count
);
455 for (i
= 0; i
< val_count
* val_bytes
; i
+= val_bytes
)
456 map
->format
.parse_val(val
+ i
);
460 EXPORT_SYMBOL_GPL(regmap_bulk_read
);
463 * remap_update_bits: Perform a read/modify/write cycle on the register map
465 * @map: Register map to update
466 * @reg: Register to update
467 * @mask: Bitmask to change
468 * @val: New value for bitmask
470 * Returns zero for success, a negative number on error.
472 int regmap_update_bits(struct regmap
*map
, unsigned int reg
,
473 unsigned int mask
, unsigned int val
)
478 mutex_lock(&map
->lock
);
480 ret
= _regmap_read(map
, reg
, &tmp
);
487 ret
= _regmap_write(map
, reg
, tmp
);
490 mutex_unlock(&map
->lock
);
494 EXPORT_SYMBOL_GPL(regmap_update_bits
);