2 * Register cache access API
4 * Copyright 2011 Wolfson Microelectronics plc
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/bsearch.h>
14 #include <linux/device.h>
15 #include <linux/export.h>
16 #include <linux/slab.h>
17 #include <linux/sort.h>
22 static const struct regcache_ops *cache_types[] = {
28 static int regcache_hw_init(struct regmap *map)
33 unsigned int reg, val;
36 if (!map->num_reg_defaults_raw)
39 /* calculate the size of reg_defaults */
40 for (count = 0, i = 0; i < map->num_reg_defaults_raw; i++)
41 if (regmap_readable(map, i * map->reg_stride) &&
42 !regmap_volatile(map, i * map->reg_stride))
45 /* all registers are unreadable or volatile, so just bypass */
47 map->cache_bypass = true;
51 map->num_reg_defaults = count;
52 map->reg_defaults = kmalloc_array(count, sizeof(struct reg_default),
54 if (!map->reg_defaults)
57 if (!map->reg_defaults_raw) {
58 bool cache_bypass = map->cache_bypass;
59 dev_warn(map->dev, "No cache defaults, reading back from HW\n");
61 /* Bypass the cache access till data read from HW */
62 map->cache_bypass = true;
63 tmp_buf = kmalloc(map->cache_size_raw, GFP_KERNEL);
68 ret = regmap_raw_read(map, 0, tmp_buf,
70 map->cache_bypass = cache_bypass;
72 map->reg_defaults_raw = tmp_buf;
79 /* fill the reg_defaults */
80 for (i = 0, j = 0; i < map->num_reg_defaults_raw; i++) {
81 reg = i * map->reg_stride;
83 if (!regmap_readable(map, reg))
86 if (regmap_volatile(map, reg))
89 if (map->reg_defaults_raw) {
90 val = regcache_get_val(map, map->reg_defaults_raw, i);
92 bool cache_bypass = map->cache_bypass;
94 map->cache_bypass = true;
95 ret = regmap_read(map, reg, &val);
96 map->cache_bypass = cache_bypass;
98 dev_err(map->dev, "Failed to read %d: %d\n",
104 map->reg_defaults[j].reg = reg;
105 map->reg_defaults[j].def = val;
112 kfree(map->reg_defaults);
117 int regcache_init(struct regmap *map, const struct regmap_config *config)
123 if (map->cache_type == REGCACHE_NONE) {
124 if (config->reg_defaults || config->num_reg_defaults_raw)
126 "No cache used with register defaults set!\n");
128 map->cache_bypass = true;
132 if (config->reg_defaults && !config->num_reg_defaults) {
134 "Register defaults are set without the number!\n");
138 for (i = 0; i < config->num_reg_defaults; i++)
139 if (config->reg_defaults[i].reg % map->reg_stride)
142 for (i = 0; i < ARRAY_SIZE(cache_types); i++)
143 if (cache_types[i]->type == map->cache_type)
146 if (i == ARRAY_SIZE(cache_types)) {
147 dev_err(map->dev, "Could not match compress type: %d\n",
152 map->num_reg_defaults = config->num_reg_defaults;
153 map->num_reg_defaults_raw = config->num_reg_defaults_raw;
154 map->reg_defaults_raw = config->reg_defaults_raw;
155 map->cache_word_size = DIV_ROUND_UP(config->val_bits, 8);
156 map->cache_size_raw = map->cache_word_size * config->num_reg_defaults_raw;
159 map->cache_ops = cache_types[i];
161 if (!map->cache_ops->read ||
162 !map->cache_ops->write ||
163 !map->cache_ops->name)
166 /* We still need to ensure that the reg_defaults
167 * won't vanish from under us. We'll need to make
170 if (config->reg_defaults) {
171 tmp_buf = kmemdup(config->reg_defaults, map->num_reg_defaults *
172 sizeof(struct reg_default), GFP_KERNEL);
175 map->reg_defaults = tmp_buf;
176 } else if (map->num_reg_defaults_raw) {
177 /* Some devices such as PMICs don't have cache defaults,
178 * we cope with this by reading back the HW registers and
179 * crafting the cache defaults by hand.
181 ret = regcache_hw_init(map);
184 if (map->cache_bypass)
188 if (!map->max_register)
189 map->max_register = map->num_reg_defaults_raw;
191 if (map->cache_ops->init) {
192 dev_dbg(map->dev, "Initializing %s cache\n",
193 map->cache_ops->name);
194 ret = map->cache_ops->init(map);
201 kfree(map->reg_defaults);
203 kfree(map->reg_defaults_raw);
208 void regcache_exit(struct regmap *map)
210 if (map->cache_type == REGCACHE_NONE)
213 BUG_ON(!map->cache_ops);
215 kfree(map->reg_defaults);
217 kfree(map->reg_defaults_raw);
219 if (map->cache_ops->exit) {
220 dev_dbg(map->dev, "Destroying %s cache\n",
221 map->cache_ops->name);
222 map->cache_ops->exit(map);
227 * regcache_read: Fetch the value of a given register from the cache.
229 * @map: map to configure.
230 * @reg: The register index.
231 * @value: The value to be returned.
233 * Return a negative value on failure, 0 on success.
235 int regcache_read(struct regmap *map,
236 unsigned int reg, unsigned int *value)
240 if (map->cache_type == REGCACHE_NONE)
243 BUG_ON(!map->cache_ops);
245 if (!regmap_volatile(map, reg)) {
246 ret = map->cache_ops->read(map, reg, value);
249 trace_regmap_reg_read_cache(map, reg, *value);
258 * regcache_write: Set the value of a given register in the cache.
260 * @map: map to configure.
261 * @reg: The register index.
262 * @value: The new register value.
264 * Return a negative value on failure, 0 on success.
266 int regcache_write(struct regmap *map,
267 unsigned int reg, unsigned int value)
269 if (map->cache_type == REGCACHE_NONE)
272 BUG_ON(!map->cache_ops);
274 if (!regmap_volatile(map, reg))
275 return map->cache_ops->write(map, reg, value);
280 static bool regcache_reg_needs_sync(struct regmap *map, unsigned int reg,
285 /* If we don't know the chip just got reset, then sync everything. */
286 if (!map->no_sync_defaults)
289 /* Is this the hardware default? If so skip. */
290 ret = regcache_lookup_reg(map, reg);
291 if (ret >= 0 && val == map->reg_defaults[ret].def)
296 static int regcache_default_sync(struct regmap *map, unsigned int min,
301 for (reg = min; reg <= max; reg += map->reg_stride) {
305 if (regmap_volatile(map, reg) ||
306 !regmap_writeable(map, reg))
309 ret = regcache_read(map, reg, &val);
313 if (!regcache_reg_needs_sync(map, reg, val))
316 map->cache_bypass = true;
317 ret = _regmap_write(map, reg, val);
318 map->cache_bypass = false;
320 dev_err(map->dev, "Unable to sync register %#x. %d\n",
324 dev_dbg(map->dev, "Synced register %#x, value %#x\n", reg, val);
331 * regcache_sync: Sync the register cache with the hardware.
333 * @map: map to configure.
335 * Any registers that should not be synced should be marked as
336 * volatile. In general drivers can choose not to use the provided
337 * syncing functionality if they so require.
339 * Return a negative value on failure, 0 on success.
341 int regcache_sync(struct regmap *map)
348 BUG_ON(!map->cache_ops);
350 map->lock(map->lock_arg);
351 /* Remember the initial bypass state */
352 bypass = map->cache_bypass;
353 dev_dbg(map->dev, "Syncing %s cache\n",
354 map->cache_ops->name);
355 name = map->cache_ops->name;
356 trace_regcache_sync(map, name, "start");
358 if (!map->cache_dirty)
363 /* Apply any patch first */
364 map->cache_bypass = true;
365 for (i = 0; i < map->patch_regs; i++) {
366 ret = _regmap_write(map, map->patch[i].reg, map->patch[i].def);
368 dev_err(map->dev, "Failed to write %x = %x: %d\n",
369 map->patch[i].reg, map->patch[i].def, ret);
373 map->cache_bypass = false;
375 if (map->cache_ops->sync)
376 ret = map->cache_ops->sync(map, 0, map->max_register);
378 ret = regcache_default_sync(map, 0, map->max_register);
381 map->cache_dirty = false;
384 /* Restore the bypass state */
386 map->cache_bypass = bypass;
387 map->no_sync_defaults = false;
388 map->unlock(map->lock_arg);
390 regmap_async_complete(map);
392 trace_regcache_sync(map, name, "stop");
396 EXPORT_SYMBOL_GPL(regcache_sync);
399 * regcache_sync_region: Sync part of the register cache with the hardware.
402 * @min: first register to sync
403 * @max: last register to sync
405 * Write all non-default register values in the specified region to
408 * Return a negative value on failure, 0 on success.
410 int regcache_sync_region(struct regmap *map, unsigned int min,
417 BUG_ON(!map->cache_ops);
419 map->lock(map->lock_arg);
421 /* Remember the initial bypass state */
422 bypass = map->cache_bypass;
424 name = map->cache_ops->name;
425 dev_dbg(map->dev, "Syncing %s cache from %d-%d\n", name, min, max);
427 trace_regcache_sync(map, name, "start region");
429 if (!map->cache_dirty)
434 if (map->cache_ops->sync)
435 ret = map->cache_ops->sync(map, min, max);
437 ret = regcache_default_sync(map, min, max);
440 /* Restore the bypass state */
441 map->cache_bypass = bypass;
443 map->no_sync_defaults = false;
444 map->unlock(map->lock_arg);
446 regmap_async_complete(map);
448 trace_regcache_sync(map, name, "stop region");
452 EXPORT_SYMBOL_GPL(regcache_sync_region);
455 * regcache_drop_region: Discard part of the register cache
457 * @map: map to operate on
458 * @min: first register to discard
459 * @max: last register to discard
461 * Discard part of the register cache.
463 * Return a negative value on failure, 0 on success.
465 int regcache_drop_region(struct regmap *map, unsigned int min,
470 if (!map->cache_ops || !map->cache_ops->drop)
473 map->lock(map->lock_arg);
475 trace_regcache_drop_region(map, min, max);
477 ret = map->cache_ops->drop(map, min, max);
479 map->unlock(map->lock_arg);
483 EXPORT_SYMBOL_GPL(regcache_drop_region);
486 * regcache_cache_only: Put a register map into cache only mode
488 * @map: map to configure
489 * @cache_only: flag if changes should be written to the hardware
491 * When a register map is marked as cache only writes to the register
492 * map API will only update the register cache, they will not cause
493 * any hardware changes. This is useful for allowing portions of
494 * drivers to act as though the device were functioning as normal when
495 * it is disabled for power saving reasons.
497 void regcache_cache_only(struct regmap *map, bool enable)
499 map->lock(map->lock_arg);
500 WARN_ON(map->cache_bypass && enable);
501 map->cache_only = enable;
502 trace_regmap_cache_only(map, enable);
503 map->unlock(map->lock_arg);
505 EXPORT_SYMBOL_GPL(regcache_cache_only);
508 * regcache_mark_dirty: Indicate that HW registers were reset to default values
512 * Inform regcache that the device has been powered down or reset, so that
513 * on resume, regcache_sync() knows to write out all non-default values
514 * stored in the cache.
516 * If this function is not called, regcache_sync() will assume that
517 * the hardware state still matches the cache state, modulo any writes that
518 * happened when cache_only was true.
520 void regcache_mark_dirty(struct regmap *map)
522 map->lock(map->lock_arg);
523 map->cache_dirty = true;
524 map->no_sync_defaults = true;
525 map->unlock(map->lock_arg);
527 EXPORT_SYMBOL_GPL(regcache_mark_dirty);
530 * regcache_cache_bypass: Put a register map into cache bypass mode
532 * @map: map to configure
533 * @cache_bypass: flag if changes should not be written to the cache
535 * When a register map is marked with the cache bypass option, writes
536 * to the register map API will only update the hardware and not the
537 * the cache directly. This is useful when syncing the cache back to
540 void regcache_cache_bypass(struct regmap *map, bool enable)
542 map->lock(map->lock_arg);
543 WARN_ON(map->cache_only && enable);
544 map->cache_bypass = enable;
545 trace_regmap_cache_bypass(map, enable);
546 map->unlock(map->lock_arg);
548 EXPORT_SYMBOL_GPL(regcache_cache_bypass);
550 bool regcache_set_val(struct regmap *map, void *base, unsigned int idx,
553 if (regcache_get_val(map, base, idx) == val)
556 /* Use device native format if possible */
557 if (map->format.format_val) {
558 map->format.format_val(base + (map->cache_word_size * idx),
563 switch (map->cache_word_size) {
596 unsigned int regcache_get_val(struct regmap *map, const void *base,
602 /* Use device native format if possible */
603 if (map->format.parse_val)
604 return map->format.parse_val(regcache_get_val_addr(map, base,
607 switch (map->cache_word_size) {
609 const u8 *cache = base;
614 const u16 *cache = base;
619 const u32 *cache = base;
625 const u64 *cache = base;
637 static int regcache_default_cmp(const void *a, const void *b)
639 const struct reg_default *_a = a;
640 const struct reg_default *_b = b;
642 return _a->reg - _b->reg;
645 int regcache_lookup_reg(struct regmap *map, unsigned int reg)
647 struct reg_default key;
648 struct reg_default *r;
653 r = bsearch(&key, map->reg_defaults, map->num_reg_defaults,
654 sizeof(struct reg_default), regcache_default_cmp);
657 return r - map->reg_defaults;
662 static bool regcache_reg_present(unsigned long *cache_present, unsigned int idx)
667 return test_bit(idx, cache_present);
670 static int regcache_sync_block_single(struct regmap *map, void *block,
671 unsigned long *cache_present,
672 unsigned int block_base,
673 unsigned int start, unsigned int end)
675 unsigned int i, regtmp, val;
678 for (i = start; i < end; i++) {
679 regtmp = block_base + (i * map->reg_stride);
681 if (!regcache_reg_present(cache_present, i) ||
682 !regmap_writeable(map, regtmp))
685 val = regcache_get_val(map, block, i);
686 if (!regcache_reg_needs_sync(map, regtmp, val))
689 map->cache_bypass = true;
691 ret = _regmap_write(map, regtmp, val);
693 map->cache_bypass = false;
695 dev_err(map->dev, "Unable to sync register %#x. %d\n",
699 dev_dbg(map->dev, "Synced register %#x, value %#x\n",
706 static int regcache_sync_block_raw_flush(struct regmap *map, const void **data,
707 unsigned int base, unsigned int cur)
709 size_t val_bytes = map->format.val_bytes;
715 count = (cur - base) / map->reg_stride;
717 dev_dbg(map->dev, "Writing %zu bytes for %d registers from 0x%x-0x%x\n",
718 count * val_bytes, count, base, cur - map->reg_stride);
720 map->cache_bypass = true;
722 ret = _regmap_raw_write(map, base, *data, count * val_bytes);
724 dev_err(map->dev, "Unable to sync registers %#x-%#x. %d\n",
725 base, cur - map->reg_stride, ret);
727 map->cache_bypass = false;
734 static int regcache_sync_block_raw(struct regmap *map, void *block,
735 unsigned long *cache_present,
736 unsigned int block_base, unsigned int start,
740 unsigned int regtmp = 0;
741 unsigned int base = 0;
742 const void *data = NULL;
745 for (i = start; i < end; i++) {
746 regtmp = block_base + (i * map->reg_stride);
748 if (!regcache_reg_present(cache_present, i) ||
749 !regmap_writeable(map, regtmp)) {
750 ret = regcache_sync_block_raw_flush(map, &data,
757 val = regcache_get_val(map, block, i);
758 if (!regcache_reg_needs_sync(map, regtmp, val)) {
759 ret = regcache_sync_block_raw_flush(map, &data,
767 data = regcache_get_val_addr(map, block, i);
772 return regcache_sync_block_raw_flush(map, &data, base, regtmp +
776 int regcache_sync_block(struct regmap *map, void *block,
777 unsigned long *cache_present,
778 unsigned int block_base, unsigned int start,
781 if (regmap_can_raw_write(map) && !map->use_single_write)
782 return regcache_sync_block_raw(map, block, cache_present,
783 block_base, start, end);
785 return regcache_sync_block_single(map, block, cache_present,
786 block_base, start, end);