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 ret = regcache_init(map, config);
289 mutex_unlock(&map->lock);
295 * regmap_exit(): Free a previously allocated register map
297 void regmap_exit(struct regmap *map)
300 regmap_debugfs_exit(map);
301 kfree(map->work_buf);
304 EXPORT_SYMBOL_GPL(regmap_exit);
306 static int _regmap_raw_write(struct regmap *map, unsigned int reg,
307 const void *val, size_t val_len)
309 u8 *u8 = map->work_buf;
315 /* Check for unwritable registers before we start */
316 if (map->writeable_reg)
317 for (i = 0; i < val_len / map->format.val_bytes; i++)
318 if (!map->writeable_reg(map->dev, reg + i))
321 map->format.format_reg(map->work_buf, reg);
323 u8[0] |= map->write_flag_mask;
325 trace_regmap_hw_write_start(map->dev, reg,
326 val_len / map->format.val_bytes);
328 /* If we're doing a single register write we can probably just
329 * send the work_buf directly, otherwise try to do a gather
332 if (val == map->work_buf + map->format.reg_bytes)
333 ret = map->bus->write(map->dev, map->work_buf,
334 map->format.reg_bytes + val_len);
335 else if (map->bus->gather_write)
336 ret = map->bus->gather_write(map->dev, map->work_buf,
337 map->format.reg_bytes,
340 /* If that didn't work fall back on linearising by hand. */
341 if (ret == -ENOTSUPP) {
342 len = map->format.reg_bytes + val_len;
343 buf = kmalloc(len, GFP_KERNEL);
347 memcpy(buf, map->work_buf, map->format.reg_bytes);
348 memcpy(buf + map->format.reg_bytes, val, val_len);
349 ret = map->bus->write(map->dev, buf, len);
354 trace_regmap_hw_write_done(map->dev, reg,
355 val_len / map->format.val_bytes);
360 int _regmap_write(struct regmap *map, unsigned int reg,
364 BUG_ON(!map->format.format_write && !map->format.format_val);
366 if (!map->cache_bypass) {
367 ret = regcache_write(map, reg, val);
370 if (map->cache_only) {
371 map->cache_dirty = true;
376 trace_regmap_reg_write(map->dev, reg, val);
378 if (map->format.format_write) {
379 map->format.format_write(map, reg, val);
381 trace_regmap_hw_write_start(map->dev, reg, 1);
383 ret = map->bus->write(map->dev, map->work_buf,
384 map->format.buf_size);
386 trace_regmap_hw_write_done(map->dev, reg, 1);
390 map->format.format_val(map->work_buf + map->format.reg_bytes,
392 return _regmap_raw_write(map, reg,
393 map->work_buf + map->format.reg_bytes,
394 map->format.val_bytes);
399 * regmap_write(): Write a value to a single register
401 * @map: Register map to write to
402 * @reg: Register to write to
403 * @val: Value to be written
405 * A value of zero will be returned on success, a negative errno will
406 * be returned in error cases.
408 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
412 mutex_lock(&map->lock);
414 ret = _regmap_write(map, reg, val);
416 mutex_unlock(&map->lock);
420 EXPORT_SYMBOL_GPL(regmap_write);
423 * regmap_raw_write(): Write raw values to one or more registers
425 * @map: Register map to write to
426 * @reg: Initial register to write to
427 * @val: Block of data to be written, laid out for direct transmission to the
429 * @val_len: Length of data pointed to by val.
431 * This function is intended to be used for things like firmware
432 * download where a large block of data needs to be transferred to the
433 * device. No formatting will be done on the data provided.
435 * A value of zero will be returned on success, a negative errno will
436 * be returned in error cases.
438 int regmap_raw_write(struct regmap *map, unsigned int reg,
439 const void *val, size_t val_len)
441 size_t val_count = val_len / map->format.val_bytes;
444 WARN_ON(!regmap_volatile_range(map, reg, val_count) &&
445 map->cache_type != REGCACHE_NONE);
447 mutex_lock(&map->lock);
449 ret = _regmap_raw_write(map, reg, val, val_len);
451 mutex_unlock(&map->lock);
455 EXPORT_SYMBOL_GPL(regmap_raw_write);
457 static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
458 unsigned int val_len)
460 u8 *u8 = map->work_buf;
463 map->format.format_reg(map->work_buf, reg);
466 * Some buses or devices flag reads by setting the high bits in the
467 * register addresss; since it's always the high bits for all
468 * current formats we can do this here rather than in
469 * formatting. This may break if we get interesting formats.
471 u8[0] |= map->read_flag_mask;
473 trace_regmap_hw_read_start(map->dev, reg,
474 val_len / map->format.val_bytes);
476 ret = map->bus->read(map->dev, map->work_buf, map->format.reg_bytes,
479 trace_regmap_hw_read_done(map->dev, reg,
480 val_len / map->format.val_bytes);
485 static int _regmap_read(struct regmap *map, unsigned int reg,
490 if (!map->cache_bypass) {
491 ret = regcache_read(map, reg, val);
496 if (!map->format.parse_val)
502 ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
504 *val = map->format.parse_val(map->work_buf);
505 trace_regmap_reg_read(map->dev, reg, *val);
512 * regmap_read(): Read a value from a single register
514 * @map: Register map to write to
515 * @reg: Register to be read from
516 * @val: Pointer to store read value
518 * A value of zero will be returned on success, a negative errno will
519 * be returned in error cases.
521 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
525 mutex_lock(&map->lock);
527 ret = _regmap_read(map, reg, val);
529 mutex_unlock(&map->lock);
533 EXPORT_SYMBOL_GPL(regmap_read);
536 * regmap_raw_read(): Read raw data from the device
538 * @map: Register map to write to
539 * @reg: First register to be read from
540 * @val: Pointer to store read value
541 * @val_len: Size of data to read
543 * A value of zero will be returned on success, a negative errno will
544 * be returned in error cases.
546 int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
549 size_t val_count = val_len / map->format.val_bytes;
552 WARN_ON(!regmap_volatile_range(map, reg, val_count) &&
553 map->cache_type != REGCACHE_NONE);
555 mutex_lock(&map->lock);
557 ret = _regmap_raw_read(map, reg, val, val_len);
559 mutex_unlock(&map->lock);
563 EXPORT_SYMBOL_GPL(regmap_raw_read);
566 * regmap_bulk_read(): Read multiple registers from the device
568 * @map: Register map to write to
569 * @reg: First register to be read from
570 * @val: Pointer to store read value, in native register size for device
571 * @val_count: Number of registers to read
573 * A value of zero will be returned on success, a negative errno will
574 * be returned in error cases.
576 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
580 size_t val_bytes = map->format.val_bytes;
581 bool vol = regmap_volatile_range(map, reg, val_count);
583 if (!map->format.parse_val)
586 if (vol || map->cache_type == REGCACHE_NONE) {
587 ret = regmap_raw_read(map, reg, val, val_bytes * val_count);
591 for (i = 0; i < val_count * val_bytes; i += val_bytes)
592 map->format.parse_val(val + i);
594 for (i = 0; i < val_count; i++) {
595 ret = regmap_read(map, reg + i, val + (i * val_bytes));
603 EXPORT_SYMBOL_GPL(regmap_bulk_read);
605 static int _regmap_update_bits(struct regmap *map, unsigned int reg,
606 unsigned int mask, unsigned int val,
610 unsigned int tmp, orig;
612 mutex_lock(&map->lock);
614 ret = _regmap_read(map, reg, &orig);
622 ret = _regmap_write(map, reg, tmp);
629 mutex_unlock(&map->lock);
635 * regmap_update_bits: Perform a read/modify/write cycle on the register map
637 * @map: Register map to update
638 * @reg: Register to update
639 * @mask: Bitmask to change
640 * @val: New value for bitmask
642 * Returns zero for success, a negative number on error.
644 int regmap_update_bits(struct regmap *map, unsigned int reg,
645 unsigned int mask, unsigned int val)
648 return _regmap_update_bits(map, reg, mask, val, &change);
650 EXPORT_SYMBOL_GPL(regmap_update_bits);
653 * regmap_update_bits_check: Perform a read/modify/write cycle on the
654 * register map and report if updated
656 * @map: Register map to update
657 * @reg: Register to update
658 * @mask: Bitmask to change
659 * @val: New value for bitmask
660 * @change: Boolean indicating if a write was done
662 * Returns zero for success, a negative number on error.
664 int regmap_update_bits_check(struct regmap *map, unsigned int reg,
665 unsigned int mask, unsigned int val,
668 return _regmap_update_bits(map, reg, mask, val, change);
670 EXPORT_SYMBOL_GPL(regmap_update_bits_check);
672 static int __init regmap_initcall(void)
674 regmap_debugfs_initcall();
678 postcore_initcall(regmap_initcall);