]>
git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - drivers/base/regmap/regcache.c
2 * Register cache access API
4 * Copyright 2011 Wolfson Microelectronics plc
6 * Author: Dimitris Papastamos <dp@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/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
)
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_volatile(map
, i
* map
->reg_stride
))
44 /* all registers are volatile, so just bypass */
46 map
->cache_bypass
= true;
50 map
->num_reg_defaults
= count
;
51 map
->reg_defaults
= kmalloc_array(count
, sizeof(struct reg_default
),
53 if (!map
->reg_defaults
)
56 if (!map
->reg_defaults_raw
) {
57 bool cache_bypass
= map
->cache_bypass
;
58 dev_warn(map
->dev
, "No cache defaults, reading back from HW\n");
60 /* Bypass the cache access till data read from HW */
61 map
->cache_bypass
= true;
62 tmp_buf
= kmalloc(map
->cache_size_raw
, GFP_KERNEL
);
67 ret
= regmap_raw_read(map
, 0, tmp_buf
,
69 map
->cache_bypass
= cache_bypass
;
73 map
->reg_defaults_raw
= tmp_buf
;
77 /* fill the reg_defaults */
78 for (i
= 0, j
= 0; i
< map
->num_reg_defaults_raw
; i
++) {
79 if (regmap_volatile(map
, i
* map
->reg_stride
))
81 val
= regcache_get_val(map
, map
->reg_defaults_raw
, i
);
82 map
->reg_defaults
[j
].reg
= i
* map
->reg_stride
;
83 map
->reg_defaults
[j
].def
= val
;
92 kfree(map
->reg_defaults
);
97 int regcache_init(struct regmap
*map
, const struct regmap_config
*config
)
103 if (map
->cache_type
== REGCACHE_NONE
) {
104 if (config
->reg_defaults
|| config
->num_reg_defaults_raw
)
106 "No cache used with register defaults set!\n");
108 map
->cache_bypass
= true;
112 if (config
->reg_defaults
&& !config
->num_reg_defaults
) {
114 "Register defaults are set without the number!\n");
118 for (i
= 0; i
< config
->num_reg_defaults
; i
++)
119 if (config
->reg_defaults
[i
].reg
% map
->reg_stride
)
122 for (i
= 0; i
< ARRAY_SIZE(cache_types
); i
++)
123 if (cache_types
[i
]->type
== map
->cache_type
)
126 if (i
== ARRAY_SIZE(cache_types
)) {
127 dev_err(map
->dev
, "Could not match compress type: %d\n",
132 map
->num_reg_defaults
= config
->num_reg_defaults
;
133 map
->num_reg_defaults_raw
= config
->num_reg_defaults_raw
;
134 map
->reg_defaults_raw
= config
->reg_defaults_raw
;
135 map
->cache_word_size
= DIV_ROUND_UP(config
->val_bits
, 8);
136 map
->cache_size_raw
= map
->cache_word_size
* config
->num_reg_defaults_raw
;
139 map
->cache_ops
= cache_types
[i
];
141 if (!map
->cache_ops
->read
||
142 !map
->cache_ops
->write
||
143 !map
->cache_ops
->name
)
146 /* We still need to ensure that the reg_defaults
147 * won't vanish from under us. We'll need to make
150 if (config
->reg_defaults
) {
151 tmp_buf
= kmemdup(config
->reg_defaults
, map
->num_reg_defaults
*
152 sizeof(struct reg_default
), GFP_KERNEL
);
155 map
->reg_defaults
= tmp_buf
;
156 } else if (map
->num_reg_defaults_raw
) {
157 /* Some devices such as PMICs don't have cache defaults,
158 * we cope with this by reading back the HW registers and
159 * crafting the cache defaults by hand.
161 ret
= regcache_hw_init(map
);
164 if (map
->cache_bypass
)
168 if (!map
->max_register
)
169 map
->max_register
= map
->num_reg_defaults_raw
;
171 if (map
->cache_ops
->init
) {
172 dev_dbg(map
->dev
, "Initializing %s cache\n",
173 map
->cache_ops
->name
);
174 ret
= map
->cache_ops
->init(map
);
181 kfree(map
->reg_defaults
);
183 kfree(map
->reg_defaults_raw
);
188 void regcache_exit(struct regmap
*map
)
190 if (map
->cache_type
== REGCACHE_NONE
)
193 BUG_ON(!map
->cache_ops
);
195 kfree(map
->reg_defaults
);
197 kfree(map
->reg_defaults_raw
);
199 if (map
->cache_ops
->exit
) {
200 dev_dbg(map
->dev
, "Destroying %s cache\n",
201 map
->cache_ops
->name
);
202 map
->cache_ops
->exit(map
);
207 * regcache_read: Fetch the value of a given register from the cache.
209 * @map: map to configure.
210 * @reg: The register index.
211 * @value: The value to be returned.
213 * Return a negative value on failure, 0 on success.
215 int regcache_read(struct regmap
*map
,
216 unsigned int reg
, unsigned int *value
)
220 if (map
->cache_type
== REGCACHE_NONE
)
223 BUG_ON(!map
->cache_ops
);
225 if (!regmap_volatile(map
, reg
)) {
226 ret
= map
->cache_ops
->read(map
, reg
, value
);
229 trace_regmap_reg_read_cache(map
, reg
, *value
);
238 * regcache_write: Set the value of a given register in the cache.
240 * @map: map to configure.
241 * @reg: The register index.
242 * @value: The new register value.
244 * Return a negative value on failure, 0 on success.
246 int regcache_write(struct regmap
*map
,
247 unsigned int reg
, unsigned int value
)
249 if (map
->cache_type
== REGCACHE_NONE
)
252 BUG_ON(!map
->cache_ops
);
254 if (!regmap_volatile(map
, reg
))
255 return map
->cache_ops
->write(map
, reg
, value
);
260 static bool regcache_reg_needs_sync(struct regmap
*map
, unsigned int reg
,
265 /* If we don't know the chip just got reset, then sync everything. */
266 if (!map
->no_sync_defaults
)
269 /* Is this the hardware default? If so skip. */
270 ret
= regcache_lookup_reg(map
, reg
);
271 if (ret
>= 0 && val
== map
->reg_defaults
[ret
].def
)
276 static int regcache_default_sync(struct regmap
*map
, unsigned int min
,
281 for (reg
= min
; reg
<= max
; reg
+= map
->reg_stride
) {
285 if (regmap_volatile(map
, reg
) ||
286 !regmap_writeable(map
, reg
))
289 ret
= regcache_read(map
, reg
, &val
);
293 if (!regcache_reg_needs_sync(map
, reg
, val
))
296 map
->cache_bypass
= true;
297 ret
= _regmap_write(map
, reg
, val
);
298 map
->cache_bypass
= false;
300 dev_err(map
->dev
, "Unable to sync register %#x. %d\n",
304 dev_dbg(map
->dev
, "Synced register %#x, value %#x\n", reg
, val
);
311 * regcache_sync: Sync the register cache with the hardware.
313 * @map: map to configure.
315 * Any registers that should not be synced should be marked as
316 * volatile. In general drivers can choose not to use the provided
317 * syncing functionality if they so require.
319 * Return a negative value on failure, 0 on success.
321 int regcache_sync(struct regmap
*map
)
328 BUG_ON(!map
->cache_ops
);
330 map
->lock(map
->lock_arg
);
331 /* Remember the initial bypass state */
332 bypass
= map
->cache_bypass
;
333 dev_dbg(map
->dev
, "Syncing %s cache\n",
334 map
->cache_ops
->name
);
335 name
= map
->cache_ops
->name
;
336 trace_regcache_sync(map
, name
, "start");
338 if (!map
->cache_dirty
)
343 /* Apply any patch first */
344 map
->cache_bypass
= true;
345 for (i
= 0; i
< map
->patch_regs
; i
++) {
346 ret
= _regmap_write(map
, map
->patch
[i
].reg
, map
->patch
[i
].def
);
348 dev_err(map
->dev
, "Failed to write %x = %x: %d\n",
349 map
->patch
[i
].reg
, map
->patch
[i
].def
, ret
);
353 map
->cache_bypass
= false;
355 if (map
->cache_ops
->sync
)
356 ret
= map
->cache_ops
->sync(map
, 0, map
->max_register
);
358 ret
= regcache_default_sync(map
, 0, map
->max_register
);
361 map
->cache_dirty
= false;
364 /* Restore the bypass state */
366 map
->cache_bypass
= bypass
;
367 map
->no_sync_defaults
= false;
368 map
->unlock(map
->lock_arg
);
370 regmap_async_complete(map
);
372 trace_regcache_sync(map
, name
, "stop");
376 EXPORT_SYMBOL_GPL(regcache_sync
);
379 * regcache_sync_region: Sync part of the register cache with the hardware.
382 * @min: first register to sync
383 * @max: last register to sync
385 * Write all non-default register values in the specified region to
388 * Return a negative value on failure, 0 on success.
390 int regcache_sync_region(struct regmap
*map
, unsigned int min
,
397 BUG_ON(!map
->cache_ops
);
399 map
->lock(map
->lock_arg
);
401 /* Remember the initial bypass state */
402 bypass
= map
->cache_bypass
;
404 name
= map
->cache_ops
->name
;
405 dev_dbg(map
->dev
, "Syncing %s cache from %d-%d\n", name
, min
, max
);
407 trace_regcache_sync(map
, name
, "start region");
409 if (!map
->cache_dirty
)
414 if (map
->cache_ops
->sync
)
415 ret
= map
->cache_ops
->sync(map
, min
, max
);
417 ret
= regcache_default_sync(map
, min
, max
);
420 /* Restore the bypass state */
421 map
->cache_bypass
= bypass
;
423 map
->no_sync_defaults
= false;
424 map
->unlock(map
->lock_arg
);
426 regmap_async_complete(map
);
428 trace_regcache_sync(map
, name
, "stop region");
432 EXPORT_SYMBOL_GPL(regcache_sync_region
);
435 * regcache_drop_region: Discard part of the register cache
437 * @map: map to operate on
438 * @min: first register to discard
439 * @max: last register to discard
441 * Discard part of the register cache.
443 * Return a negative value on failure, 0 on success.
445 int regcache_drop_region(struct regmap
*map
, unsigned int min
,
450 if (!map
->cache_ops
|| !map
->cache_ops
->drop
)
453 map
->lock(map
->lock_arg
);
455 trace_regcache_drop_region(map
, min
, max
);
457 ret
= map
->cache_ops
->drop(map
, min
, max
);
459 map
->unlock(map
->lock_arg
);
463 EXPORT_SYMBOL_GPL(regcache_drop_region
);
466 * regcache_cache_only: Put a register map into cache only mode
468 * @map: map to configure
469 * @cache_only: flag if changes should be written to the hardware
471 * When a register map is marked as cache only writes to the register
472 * map API will only update the register cache, they will not cause
473 * any hardware changes. This is useful for allowing portions of
474 * drivers to act as though the device were functioning as normal when
475 * it is disabled for power saving reasons.
477 void regcache_cache_only(struct regmap
*map
, bool enable
)
479 map
->lock(map
->lock_arg
);
480 WARN_ON(map
->cache_bypass
&& enable
);
481 map
->cache_only
= enable
;
482 trace_regmap_cache_only(map
, enable
);
483 map
->unlock(map
->lock_arg
);
485 EXPORT_SYMBOL_GPL(regcache_cache_only
);
488 * regcache_mark_dirty: Indicate that HW registers were reset to default values
492 * Inform regcache that the device has been powered down or reset, so that
493 * on resume, regcache_sync() knows to write out all non-default values
494 * stored in the cache.
496 * If this function is not called, regcache_sync() will assume that
497 * the hardware state still matches the cache state, modulo any writes that
498 * happened when cache_only was true.
500 void regcache_mark_dirty(struct regmap
*map
)
502 map
->lock(map
->lock_arg
);
503 map
->cache_dirty
= true;
504 map
->no_sync_defaults
= true;
505 map
->unlock(map
->lock_arg
);
507 EXPORT_SYMBOL_GPL(regcache_mark_dirty
);
510 * regcache_cache_bypass: Put a register map into cache bypass mode
512 * @map: map to configure
513 * @cache_bypass: flag if changes should not be written to the hardware
515 * When a register map is marked with the cache bypass option, writes
516 * to the register map API will only update the hardware and not the
517 * the cache directly. This is useful when syncing the cache back to
520 void regcache_cache_bypass(struct regmap
*map
, bool enable
)
522 map
->lock(map
->lock_arg
);
523 WARN_ON(map
->cache_only
&& enable
);
524 map
->cache_bypass
= enable
;
525 trace_regmap_cache_bypass(map
, enable
);
526 map
->unlock(map
->lock_arg
);
528 EXPORT_SYMBOL_GPL(regcache_cache_bypass
);
530 bool regcache_set_val(struct regmap
*map
, void *base
, unsigned int idx
,
533 if (regcache_get_val(map
, base
, idx
) == val
)
536 /* Use device native format if possible */
537 if (map
->format
.format_val
) {
538 map
->format
.format_val(base
+ (map
->cache_word_size
* idx
),
543 switch (map
->cache_word_size
) {
576 unsigned int regcache_get_val(struct regmap
*map
, const void *base
,
582 /* Use device native format if possible */
583 if (map
->format
.parse_val
)
584 return map
->format
.parse_val(regcache_get_val_addr(map
, base
,
587 switch (map
->cache_word_size
) {
589 const u8
*cache
= base
;
594 const u16
*cache
= base
;
599 const u32
*cache
= base
;
605 const u64
*cache
= base
;
617 static int regcache_default_cmp(const void *a
, const void *b
)
619 const struct reg_default
*_a
= a
;
620 const struct reg_default
*_b
= b
;
622 return _a
->reg
- _b
->reg
;
625 int regcache_lookup_reg(struct regmap
*map
, unsigned int reg
)
627 struct reg_default key
;
628 struct reg_default
*r
;
633 r
= bsearch(&key
, map
->reg_defaults
, map
->num_reg_defaults
,
634 sizeof(struct reg_default
), regcache_default_cmp
);
637 return r
- map
->reg_defaults
;
642 static bool regcache_reg_present(unsigned long *cache_present
, unsigned int idx
)
647 return test_bit(idx
, cache_present
);
650 static int regcache_sync_block_single(struct regmap
*map
, void *block
,
651 unsigned long *cache_present
,
652 unsigned int block_base
,
653 unsigned int start
, unsigned int end
)
655 unsigned int i
, regtmp
, val
;
658 for (i
= start
; i
< end
; i
++) {
659 regtmp
= block_base
+ (i
* map
->reg_stride
);
661 if (!regcache_reg_present(cache_present
, i
) ||
662 !regmap_writeable(map
, regtmp
))
665 val
= regcache_get_val(map
, block
, i
);
666 if (!regcache_reg_needs_sync(map
, regtmp
, val
))
669 map
->cache_bypass
= true;
671 ret
= _regmap_write(map
, regtmp
, val
);
673 map
->cache_bypass
= false;
675 dev_err(map
->dev
, "Unable to sync register %#x. %d\n",
679 dev_dbg(map
->dev
, "Synced register %#x, value %#x\n",
686 static int regcache_sync_block_raw_flush(struct regmap
*map
, const void **data
,
687 unsigned int base
, unsigned int cur
)
689 size_t val_bytes
= map
->format
.val_bytes
;
695 count
= (cur
- base
) / map
->reg_stride
;
697 dev_dbg(map
->dev
, "Writing %zu bytes for %d registers from 0x%x-0x%x\n",
698 count
* val_bytes
, count
, base
, cur
- map
->reg_stride
);
700 map
->cache_bypass
= true;
702 ret
= _regmap_raw_write(map
, base
, *data
, count
* val_bytes
);
704 dev_err(map
->dev
, "Unable to sync registers %#x-%#x. %d\n",
705 base
, cur
- map
->reg_stride
, ret
);
707 map
->cache_bypass
= false;
714 static int regcache_sync_block_raw(struct regmap
*map
, void *block
,
715 unsigned long *cache_present
,
716 unsigned int block_base
, unsigned int start
,
720 unsigned int regtmp
= 0;
721 unsigned int base
= 0;
722 const void *data
= NULL
;
725 for (i
= start
; i
< end
; i
++) {
726 regtmp
= block_base
+ (i
* map
->reg_stride
);
728 if (!regcache_reg_present(cache_present
, i
) ||
729 !regmap_writeable(map
, regtmp
)) {
730 ret
= regcache_sync_block_raw_flush(map
, &data
,
737 val
= regcache_get_val(map
, block
, i
);
738 if (!regcache_reg_needs_sync(map
, regtmp
, val
)) {
739 ret
= regcache_sync_block_raw_flush(map
, &data
,
747 data
= regcache_get_val_addr(map
, block
, i
);
752 return regcache_sync_block_raw_flush(map
, &data
, base
, regtmp
+
756 int regcache_sync_block(struct regmap
*map
, void *block
,
757 unsigned long *cache_present
,
758 unsigned int block_base
, unsigned int start
,
761 if (regmap_can_raw_write(map
) && !map
->use_single_write
)
762 return regcache_sync_block_raw(map
, block
, cache_present
,
763 block_base
, start
, end
);
765 return regcache_sync_block_single(map
, block
, cache_present
,
766 block_base
, start
, end
);