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->format.format_write)
42 if (map->readable_reg)
43 return map->readable_reg(map->dev, reg);
48 bool regmap_volatile(struct regmap *map, unsigned int reg)
50 if (!map->format.format_write && !regmap_readable(map, reg))
53 if (map->volatile_reg)
54 return map->volatile_reg(map->dev, reg);
59 bool regmap_precious(struct regmap *map, unsigned int reg)
61 if (!regmap_readable(map, reg))
64 if (map->precious_reg)
65 return map->precious_reg(map->dev, reg);
70 static void regmap_format_4_12_write(struct regmap *map,
71 unsigned int reg, unsigned int val)
73 __be16 *out = map->work_buf;
74 *out = cpu_to_be16((reg << 12) | val);
77 static void regmap_format_7_9_write(struct regmap *map,
78 unsigned int reg, unsigned int val)
80 __be16 *out = map->work_buf;
81 *out = cpu_to_be16((reg << 9) | val);
84 static void regmap_format_8(void *buf, unsigned int val)
91 static void regmap_format_16(void *buf, unsigned int val)
95 b[0] = cpu_to_be16(val);
98 static unsigned int regmap_parse_8(void *buf)
105 static unsigned int regmap_parse_16(void *buf)
109 b[0] = be16_to_cpu(b[0]);
115 * regmap_init(): Initialise register map
117 * @dev: Device that will be interacted with
118 * @bus: Bus-specific callbacks to use with device
119 * @config: Configuration for register map
121 * The return value will be an ERR_PTR() on error or a valid pointer to
122 * a struct regmap. This function should generally not be called
123 * directly, it should be called by bus-specific init functions.
125 struct regmap *regmap_init(struct device *dev,
126 const struct regmap_bus *bus,
127 const struct regmap_config *config)
135 map = kzalloc(sizeof(*map), GFP_KERNEL);
141 mutex_init(&map->lock);
142 map->format.buf_size = (config->reg_bits + config->val_bits) / 8;
143 map->format.reg_bytes = config->reg_bits / 8;
144 map->format.val_bytes = config->val_bits / 8;
147 map->max_register = config->max_register;
148 map->writeable_reg = config->writeable_reg;
149 map->readable_reg = config->readable_reg;
150 map->volatile_reg = config->volatile_reg;
151 map->precious_reg = config->precious_reg;
152 map->cache_type = config->cache_type;
153 map->reg_defaults = config->reg_defaults;
154 map->num_reg_defaults = config->num_reg_defaults;
155 map->num_reg_defaults_raw = config->num_reg_defaults_raw;
156 map->reg_defaults_raw = config->reg_defaults_raw;
157 map->cache_size_raw = (config->val_bits / 8) * config->num_reg_defaults_raw;
158 map->cache_word_size = config->val_bits / 8;
160 if (config->read_flag_mask || config->write_flag_mask) {
161 map->read_flag_mask = config->read_flag_mask;
162 map->write_flag_mask = config->write_flag_mask;
164 map->read_flag_mask = bus->read_flag_mask;
167 switch (config->reg_bits) {
169 switch (config->val_bits) {
171 map->format.format_write = regmap_format_4_12_write;
179 switch (config->val_bits) {
181 map->format.format_write = regmap_format_7_9_write;
189 map->format.format_reg = regmap_format_8;
193 map->format.format_reg = regmap_format_16;
200 switch (config->val_bits) {
202 map->format.format_val = regmap_format_8;
203 map->format.parse_val = regmap_parse_8;
206 map->format.format_val = regmap_format_16;
207 map->format.parse_val = regmap_parse_16;
211 if (!map->format.format_write &&
212 !(map->format.format_reg && map->format.format_val))
215 map->work_buf = kmalloc(map->format.buf_size, GFP_KERNEL);
216 if (map->work_buf == NULL) {
221 ret = regcache_init(map);
225 regmap_debugfs_init(map);
234 EXPORT_SYMBOL_GPL(regmap_init);
237 * regmap_exit(): Free a previously allocated register map
239 void regmap_exit(struct regmap *map)
242 regmap_debugfs_exit(map);
243 kfree(map->work_buf);
246 EXPORT_SYMBOL_GPL(regmap_exit);
248 static int _regmap_raw_write(struct regmap *map, unsigned int reg,
249 const void *val, size_t val_len)
251 u8 *u8 = map->work_buf;
257 /* Check for unwritable registers before we start */
258 if (map->writeable_reg)
259 for (i = 0; i < val_len / map->format.val_bytes; i++)
260 if (!map->writeable_reg(map->dev, reg + i))
263 map->format.format_reg(map->work_buf, reg);
265 u8[0] |= map->write_flag_mask;
267 trace_regmap_hw_write_start(map->dev, reg,
268 val_len / map->format.val_bytes);
270 /* If we're doing a single register write we can probably just
271 * send the work_buf directly, otherwise try to do a gather
274 if (val == map->work_buf + map->format.reg_bytes)
275 ret = map->bus->write(map->dev, map->work_buf,
276 map->format.reg_bytes + val_len);
277 else if (map->bus->gather_write)
278 ret = map->bus->gather_write(map->dev, map->work_buf,
279 map->format.reg_bytes,
282 /* If that didn't work fall back on linearising by hand. */
283 if (ret == -ENOTSUPP) {
284 len = map->format.reg_bytes + val_len;
285 buf = kmalloc(len, GFP_KERNEL);
289 memcpy(buf, map->work_buf, map->format.reg_bytes);
290 memcpy(buf + map->format.reg_bytes, val, val_len);
291 ret = map->bus->write(map->dev, buf, len);
296 trace_regmap_hw_write_done(map->dev, reg,
297 val_len / map->format.val_bytes);
302 int _regmap_write(struct regmap *map, unsigned int reg,
306 BUG_ON(!map->format.format_write && !map->format.format_val);
308 if (!map->cache_bypass) {
309 ret = regcache_write(map, reg, val);
316 trace_regmap_reg_write(map->dev, reg, val);
318 if (map->format.format_write) {
319 map->format.format_write(map, reg, val);
321 trace_regmap_hw_write_start(map->dev, reg, 1);
323 ret = map->bus->write(map->dev, map->work_buf,
324 map->format.buf_size);
326 trace_regmap_hw_write_done(map->dev, reg, 1);
330 map->format.format_val(map->work_buf + map->format.reg_bytes,
332 return _regmap_raw_write(map, reg,
333 map->work_buf + map->format.reg_bytes,
334 map->format.val_bytes);
339 * regmap_write(): Write a value to a single register
341 * @map: Register map to write to
342 * @reg: Register to write to
343 * @val: Value to be written
345 * A value of zero will be returned on success, a negative errno will
346 * be returned in error cases.
348 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
352 mutex_lock(&map->lock);
354 ret = _regmap_write(map, reg, val);
356 mutex_unlock(&map->lock);
360 EXPORT_SYMBOL_GPL(regmap_write);
363 * regmap_raw_write(): Write raw values to one or more registers
365 * @map: Register map to write to
366 * @reg: Initial register to write to
367 * @val: Block of data to be written, laid out for direct transmission to the
369 * @val_len: Length of data pointed to by val.
371 * This function is intended to be used for things like firmware
372 * download where a large block of data needs to be transferred to the
373 * device. No formatting will be done on the data provided.
375 * A value of zero will be returned on success, a negative errno will
376 * be returned in error cases.
378 int regmap_raw_write(struct regmap *map, unsigned int reg,
379 const void *val, size_t val_len)
383 WARN_ON(map->cache_type != REGCACHE_NONE);
385 mutex_lock(&map->lock);
387 ret = _regmap_raw_write(map, reg, val, val_len);
389 mutex_unlock(&map->lock);
393 EXPORT_SYMBOL_GPL(regmap_raw_write);
395 static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
396 unsigned int val_len)
398 u8 *u8 = map->work_buf;
401 map->format.format_reg(map->work_buf, reg);
404 * Some buses or devices flag reads by setting the high bits in the
405 * register addresss; since it's always the high bits for all
406 * current formats we can do this here rather than in
407 * formatting. This may break if we get interesting formats.
409 u8[0] |= map->read_flag_mask;
411 trace_regmap_hw_read_start(map->dev, reg,
412 val_len / map->format.val_bytes);
414 ret = map->bus->read(map->dev, map->work_buf, map->format.reg_bytes,
417 trace_regmap_hw_read_done(map->dev, reg,
418 val_len / map->format.val_bytes);
423 static int _regmap_read(struct regmap *map, unsigned int reg,
428 if (!map->format.parse_val)
431 if (!map->cache_bypass) {
432 ret = regcache_read(map, reg, val);
440 ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
442 *val = map->format.parse_val(map->work_buf);
443 trace_regmap_reg_read(map->dev, reg, *val);
450 * regmap_read(): Read a value from a single register
452 * @map: Register map to write to
453 * @reg: Register to be read from
454 * @val: Pointer to store read value
456 * A value of zero will be returned on success, a negative errno will
457 * be returned in error cases.
459 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
463 mutex_lock(&map->lock);
465 ret = _regmap_read(map, reg, val);
467 mutex_unlock(&map->lock);
471 EXPORT_SYMBOL_GPL(regmap_read);
474 * regmap_raw_read(): Read raw data from the device
476 * @map: Register map to write to
477 * @reg: First register to be read from
478 * @val: Pointer to store read value
479 * @val_len: Size of data to read
481 * A value of zero will be returned on success, a negative errno will
482 * be returned in error cases.
484 int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
491 for (i = 0; i < val_len / map->format.val_bytes; i++)
492 if (!regmap_volatile(map, reg + i))
495 WARN_ON(!vol && map->cache_type != REGCACHE_NONE);
497 mutex_lock(&map->lock);
499 ret = _regmap_raw_read(map, reg, val, val_len);
501 mutex_unlock(&map->lock);
505 EXPORT_SYMBOL_GPL(regmap_raw_read);
508 * regmap_bulk_read(): Read multiple registers from the device
510 * @map: Register map to write to
511 * @reg: First register to be read from
512 * @val: Pointer to store read value, in native register size for device
513 * @val_count: Number of registers to read
515 * A value of zero will be returned on success, a negative errno will
516 * be returned in error cases.
518 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
522 size_t val_bytes = map->format.val_bytes;
525 if (!map->format.parse_val)
528 /* Is this a block of volatile registers? */
529 for (i = 0; i < val_count; i++)
530 if (!regmap_volatile(map, reg + i))
533 if (vol || map->cache_type == REGCACHE_NONE) {
534 ret = regmap_raw_read(map, reg, val, val_bytes * val_count);
538 for (i = 0; i < val_count * val_bytes; i += val_bytes)
539 map->format.parse_val(val + i);
541 for (i = 0; i < val_count; i++) {
542 ret = regmap_read(map, reg + i, val + (i * val_bytes));
550 EXPORT_SYMBOL_GPL(regmap_bulk_read);
553 * remap_update_bits: Perform a read/modify/write cycle on the register map
555 * @map: Register map to update
556 * @reg: Register to update
557 * @mask: Bitmask to change
558 * @val: New value for bitmask
560 * Returns zero for success, a negative number on error.
562 int regmap_update_bits(struct regmap *map, unsigned int reg,
563 unsigned int mask, unsigned int val)
568 mutex_lock(&map->lock);
570 ret = _regmap_read(map, reg, &tmp);
577 ret = _regmap_write(map, reg, tmp);
580 mutex_unlock(&map->lock);
584 EXPORT_SYMBOL_GPL(regmap_update_bits);
586 static int __init regmap_initcall(void)
588 regmap_debugfs_initcall();
592 postcore_initcall(regmap_initcall);