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);
261 static void devm_regmap_release(struct device *dev, void *res)
263 regmap_exit(*(struct regmap **)res);
267 * devm_regmap_init(): Initialise managed register map
269 * @dev: Device that will be interacted with
270 * @bus: Bus-specific callbacks to use with device
271 * @config: Configuration for register map
273 * The return value will be an ERR_PTR() on error or a valid pointer
274 * to a struct regmap. This function should generally not be called
275 * directly, it should be called by bus-specific init functions. The
276 * map will be automatically freed by the device management code.
278 struct regmap *devm_regmap_init(struct device *dev,
279 const struct regmap_bus *bus,
280 const struct regmap_config *config)
282 struct regmap **ptr, *regmap;
284 ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL);
286 return ERR_PTR(-ENOMEM);
288 regmap = regmap_init(dev, bus, config);
289 if (!IS_ERR(regmap)) {
291 devres_add(dev, ptr);
298 EXPORT_SYMBOL_GPL(devm_regmap_init);
301 * regmap_reinit_cache(): Reinitialise the current register cache
303 * @map: Register map to operate on.
304 * @config: New configuration. Only the cache data will be used.
306 * Discard any existing register cache for the map and initialize a
307 * new cache. This can be used to restore the cache to defaults or to
308 * update the cache configuration to reflect runtime discovery of the
311 int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
315 mutex_lock(&map->lock);
319 map->max_register = config->max_register;
320 map->writeable_reg = config->writeable_reg;
321 map->readable_reg = config->readable_reg;
322 map->volatile_reg = config->volatile_reg;
323 map->precious_reg = config->precious_reg;
324 map->cache_type = config->cache_type;
326 map->cache_bypass = false;
327 map->cache_only = false;
329 ret = regcache_init(map, config);
331 mutex_unlock(&map->lock);
337 * regmap_exit(): Free a previously allocated register map
339 void regmap_exit(struct regmap *map)
342 regmap_debugfs_exit(map);
343 kfree(map->work_buf);
346 EXPORT_SYMBOL_GPL(regmap_exit);
348 static int _regmap_raw_write(struct regmap *map, unsigned int reg,
349 const void *val, size_t val_len)
351 u8 *u8 = map->work_buf;
357 /* Check for unwritable registers before we start */
358 if (map->writeable_reg)
359 for (i = 0; i < val_len / map->format.val_bytes; i++)
360 if (!map->writeable_reg(map->dev, reg + i))
363 map->format.format_reg(map->work_buf, reg);
365 u8[0] |= map->write_flag_mask;
367 trace_regmap_hw_write_start(map->dev, reg,
368 val_len / map->format.val_bytes);
370 /* If we're doing a single register write we can probably just
371 * send the work_buf directly, otherwise try to do a gather
374 if (val == map->work_buf + map->format.reg_bytes)
375 ret = map->bus->write(map->dev, map->work_buf,
376 map->format.reg_bytes + val_len);
377 else if (map->bus->gather_write)
378 ret = map->bus->gather_write(map->dev, map->work_buf,
379 map->format.reg_bytes,
382 /* If that didn't work fall back on linearising by hand. */
383 if (ret == -ENOTSUPP) {
384 len = map->format.reg_bytes + val_len;
385 buf = kmalloc(len, GFP_KERNEL);
389 memcpy(buf, map->work_buf, map->format.reg_bytes);
390 memcpy(buf + map->format.reg_bytes, val, val_len);
391 ret = map->bus->write(map->dev, buf, len);
396 trace_regmap_hw_write_done(map->dev, reg,
397 val_len / map->format.val_bytes);
402 int _regmap_write(struct regmap *map, unsigned int reg,
406 BUG_ON(!map->format.format_write && !map->format.format_val);
408 if (!map->cache_bypass) {
409 ret = regcache_write(map, reg, val);
412 if (map->cache_only) {
413 map->cache_dirty = true;
418 trace_regmap_reg_write(map->dev, reg, val);
420 if (map->format.format_write) {
421 map->format.format_write(map, reg, val);
423 trace_regmap_hw_write_start(map->dev, reg, 1);
425 ret = map->bus->write(map->dev, map->work_buf,
426 map->format.buf_size);
428 trace_regmap_hw_write_done(map->dev, reg, 1);
432 map->format.format_val(map->work_buf + map->format.reg_bytes,
434 return _regmap_raw_write(map, reg,
435 map->work_buf + map->format.reg_bytes,
436 map->format.val_bytes);
441 * regmap_write(): Write a value to a single register
443 * @map: Register map to write to
444 * @reg: Register to write to
445 * @val: Value to be written
447 * A value of zero will be returned on success, a negative errno will
448 * be returned in error cases.
450 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
454 mutex_lock(&map->lock);
456 ret = _regmap_write(map, reg, val);
458 mutex_unlock(&map->lock);
462 EXPORT_SYMBOL_GPL(regmap_write);
465 * regmap_raw_write(): Write raw values to one or more registers
467 * @map: Register map to write to
468 * @reg: Initial register to write to
469 * @val: Block of data to be written, laid out for direct transmission to the
471 * @val_len: Length of data pointed to by val.
473 * This function is intended to be used for things like firmware
474 * download where a large block of data needs to be transferred to the
475 * device. No formatting will be done on the data provided.
477 * A value of zero will be returned on success, a negative errno will
478 * be returned in error cases.
480 int regmap_raw_write(struct regmap *map, unsigned int reg,
481 const void *val, size_t val_len)
483 size_t val_count = val_len / map->format.val_bytes;
486 WARN_ON(!regmap_volatile_range(map, reg, val_count) &&
487 map->cache_type != REGCACHE_NONE);
489 mutex_lock(&map->lock);
491 ret = _regmap_raw_write(map, reg, val, val_len);
493 mutex_unlock(&map->lock);
497 EXPORT_SYMBOL_GPL(regmap_raw_write);
499 static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
500 unsigned int val_len)
502 u8 *u8 = map->work_buf;
505 map->format.format_reg(map->work_buf, reg);
508 * Some buses or devices flag reads by setting the high bits in the
509 * register addresss; since it's always the high bits for all
510 * current formats we can do this here rather than in
511 * formatting. This may break if we get interesting formats.
513 u8[0] |= map->read_flag_mask;
515 trace_regmap_hw_read_start(map->dev, reg,
516 val_len / map->format.val_bytes);
518 ret = map->bus->read(map->dev, map->work_buf, map->format.reg_bytes,
521 trace_regmap_hw_read_done(map->dev, reg,
522 val_len / map->format.val_bytes);
527 static int _regmap_read(struct regmap *map, unsigned int reg,
532 if (!map->cache_bypass) {
533 ret = regcache_read(map, reg, val);
538 if (!map->format.parse_val)
544 ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
546 *val = map->format.parse_val(map->work_buf);
547 trace_regmap_reg_read(map->dev, reg, *val);
554 * regmap_read(): Read a value from a single register
556 * @map: Register map to write to
557 * @reg: Register to be read from
558 * @val: Pointer to store read value
560 * A value of zero will be returned on success, a negative errno will
561 * be returned in error cases.
563 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
567 mutex_lock(&map->lock);
569 ret = _regmap_read(map, reg, val);
571 mutex_unlock(&map->lock);
575 EXPORT_SYMBOL_GPL(regmap_read);
578 * regmap_raw_read(): Read raw data from the device
580 * @map: Register map to write to
581 * @reg: First register to be read from
582 * @val: Pointer to store read value
583 * @val_len: Size of data to read
585 * A value of zero will be returned on success, a negative errno will
586 * be returned in error cases.
588 int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
591 size_t val_count = val_len / map->format.val_bytes;
594 WARN_ON(!regmap_volatile_range(map, reg, val_count) &&
595 map->cache_type != REGCACHE_NONE);
597 mutex_lock(&map->lock);
599 ret = _regmap_raw_read(map, reg, val, val_len);
601 mutex_unlock(&map->lock);
605 EXPORT_SYMBOL_GPL(regmap_raw_read);
608 * regmap_bulk_read(): Read multiple registers from the device
610 * @map: Register map to write to
611 * @reg: First register to be read from
612 * @val: Pointer to store read value, in native register size for device
613 * @val_count: Number of registers to read
615 * A value of zero will be returned on success, a negative errno will
616 * be returned in error cases.
618 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
622 size_t val_bytes = map->format.val_bytes;
623 bool vol = regmap_volatile_range(map, reg, val_count);
625 if (!map->format.parse_val)
628 if (vol || map->cache_type == REGCACHE_NONE) {
629 ret = regmap_raw_read(map, reg, val, val_bytes * val_count);
633 for (i = 0; i < val_count * val_bytes; i += val_bytes)
634 map->format.parse_val(val + i);
636 for (i = 0; i < val_count; i++) {
637 ret = regmap_read(map, reg + i, val + (i * val_bytes));
645 EXPORT_SYMBOL_GPL(regmap_bulk_read);
647 static int _regmap_update_bits(struct regmap *map, unsigned int reg,
648 unsigned int mask, unsigned int val,
652 unsigned int tmp, orig;
654 mutex_lock(&map->lock);
656 ret = _regmap_read(map, reg, &orig);
664 ret = _regmap_write(map, reg, tmp);
671 mutex_unlock(&map->lock);
677 * regmap_update_bits: Perform a read/modify/write cycle on the register map
679 * @map: Register map to update
680 * @reg: Register to update
681 * @mask: Bitmask to change
682 * @val: New value for bitmask
684 * Returns zero for success, a negative number on error.
686 int regmap_update_bits(struct regmap *map, unsigned int reg,
687 unsigned int mask, unsigned int val)
690 return _regmap_update_bits(map, reg, mask, val, &change);
692 EXPORT_SYMBOL_GPL(regmap_update_bits);
695 * regmap_update_bits_check: Perform a read/modify/write cycle on the
696 * register map and report if updated
698 * @map: Register map to update
699 * @reg: Register to update
700 * @mask: Bitmask to change
701 * @val: New value for bitmask
702 * @change: Boolean indicating if a write was done
704 * Returns zero for success, a negative number on error.
706 int regmap_update_bits_check(struct regmap *map, unsigned int reg,
707 unsigned int mask, unsigned int val,
710 return _regmap_update_bits(map, reg, mask, val, change);
712 EXPORT_SYMBOL_GPL(regmap_update_bits_check);
715 * regmap_register_patch: Register and apply register updates to be applied
716 * on device initialistion
718 * @map: Register map to apply updates to.
719 * @regs: Values to update.
720 * @num_regs: Number of entries in regs.
722 * Register a set of register updates to be applied to the device
723 * whenever the device registers are synchronised with the cache and
724 * apply them immediately. Typically this is used to apply
725 * corrections to be applied to the device defaults on startup, such
726 * as the updates some vendors provide to undocumented registers.
728 int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
734 /* If needed the implementation can be extended to support this */
738 mutex_lock(&map->lock);
740 bypass = map->cache_bypass;
742 map->cache_bypass = true;
744 /* Write out first; it's useful to apply even if we fail later. */
745 for (i = 0; i < num_regs; i++) {
746 ret = _regmap_write(map, regs[i].reg, regs[i].def);
748 dev_err(map->dev, "Failed to write %x = %x: %d\n",
749 regs[i].reg, regs[i].def, ret);
754 map->patch = kcalloc(num_regs, sizeof(struct reg_default), GFP_KERNEL);
755 if (map->patch != NULL) {
756 memcpy(map->patch, regs,
757 num_regs * sizeof(struct reg_default));
758 map->patch_regs = num_regs;
764 map->cache_bypass = bypass;
766 mutex_unlock(&map->lock);
770 EXPORT_SYMBOL_GPL(regmap_register_patch);
772 static int __init regmap_initcall(void)
774 regmap_debugfs_initcall();
778 postcore_initcall(regmap_initcall);