fb84d711fbb4098f51fdff33a2681b6c655bf5df
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/slab.h>
14 #include <linux/export.h>
15 #include <trace/events/regmap.h>
16 #include <linux/bsearch.h>
17 #include <linux/sort.h>
21 static const struct regcache_ops
*cache_types
[] = {
26 static int regcache_hw_init(struct regmap
*map
)
34 if (!map
->num_reg_defaults_raw
)
37 if (!map
->reg_defaults_raw
) {
38 dev_warn(map
->dev
, "No cache defaults, reading back from HW\n");
39 tmp_buf
= kmalloc(map
->cache_size_raw
, GFP_KERNEL
);
42 ret
= regmap_bulk_read(map
, 0, tmp_buf
,
43 map
->num_reg_defaults_raw
);
48 map
->reg_defaults_raw
= tmp_buf
;
52 /* calculate the size of reg_defaults */
53 for (count
= 0, i
= 0; i
< map
->num_reg_defaults_raw
; i
++) {
54 val
= regcache_get_val(map
->reg_defaults_raw
,
55 i
, map
->cache_word_size
);
56 if (regmap_volatile(map
, i
))
61 map
->reg_defaults
= kmalloc(count
* sizeof(struct reg_default
),
63 if (!map
->reg_defaults
) {
68 /* fill the reg_defaults */
69 map
->num_reg_defaults
= count
;
70 for (i
= 0, j
= 0; i
< map
->num_reg_defaults_raw
; i
++) {
71 val
= regcache_get_val(map
->reg_defaults_raw
,
72 i
, map
->cache_word_size
);
73 if (regmap_volatile(map
, i
))
75 map
->reg_defaults
[j
].reg
= i
;
76 map
->reg_defaults
[j
].def
= val
;
84 kfree(map
->reg_defaults_raw
);
89 int regcache_init(struct regmap
*map
, const struct regmap_config
*config
)
95 if (map
->cache_type
== REGCACHE_NONE
) {
96 map
->cache_bypass
= true;
100 for (i
= 0; i
< ARRAY_SIZE(cache_types
); i
++)
101 if (cache_types
[i
]->type
== map
->cache_type
)
104 if (i
== ARRAY_SIZE(cache_types
)) {
105 dev_err(map
->dev
, "Could not match compress type: %d\n",
110 map
->num_reg_defaults
= config
->num_reg_defaults
;
111 map
->num_reg_defaults_raw
= config
->num_reg_defaults_raw
;
112 map
->reg_defaults_raw
= config
->reg_defaults_raw
;
113 map
->cache_word_size
= DIV_ROUND_UP(config
->val_bits
, 8);
114 map
->cache_size_raw
= map
->cache_word_size
* config
->num_reg_defaults_raw
;
117 map
->cache_ops
= cache_types
[i
];
119 if (!map
->cache_ops
->read
||
120 !map
->cache_ops
->write
||
121 !map
->cache_ops
->name
)
124 /* We still need to ensure that the reg_defaults
125 * won't vanish from under us. We'll need to make
128 if (config
->reg_defaults
) {
129 if (!map
->num_reg_defaults
)
131 tmp_buf
= kmemdup(config
->reg_defaults
, map
->num_reg_defaults
*
132 sizeof(struct reg_default
), GFP_KERNEL
);
135 map
->reg_defaults
= tmp_buf
;
136 } else if (map
->num_reg_defaults_raw
) {
137 /* Some devices such as PMICs don't have cache defaults,
138 * we cope with this by reading back the HW registers and
139 * crafting the cache defaults by hand.
141 ret
= regcache_hw_init(map
);
146 if (!map
->max_register
)
147 map
->max_register
= map
->num_reg_defaults_raw
;
149 if (map
->cache_ops
->init
) {
150 dev_dbg(map
->dev
, "Initializing %s cache\n",
151 map
->cache_ops
->name
);
152 ret
= map
->cache_ops
->init(map
);
159 kfree(map
->reg_defaults
);
161 kfree(map
->reg_defaults_raw
);
166 void regcache_exit(struct regmap
*map
)
168 if (map
->cache_type
== REGCACHE_NONE
)
171 BUG_ON(!map
->cache_ops
);
173 kfree(map
->reg_defaults
);
175 kfree(map
->reg_defaults_raw
);
177 if (map
->cache_ops
->exit
) {
178 dev_dbg(map
->dev
, "Destroying %s cache\n",
179 map
->cache_ops
->name
);
180 map
->cache_ops
->exit(map
);
185 * regcache_read: Fetch the value of a given register from the cache.
187 * @map: map to configure.
188 * @reg: The register index.
189 * @value: The value to be returned.
191 * Return a negative value on failure, 0 on success.
193 int regcache_read(struct regmap
*map
,
194 unsigned int reg
, unsigned int *value
)
198 if (map
->cache_type
== REGCACHE_NONE
)
201 BUG_ON(!map
->cache_ops
);
203 if (!regmap_volatile(map
, reg
)) {
204 ret
= map
->cache_ops
->read(map
, reg
, value
);
207 trace_regmap_reg_read_cache(map
->dev
, reg
, *value
);
216 * regcache_write: Set the value of a given register in the cache.
218 * @map: map to configure.
219 * @reg: The register index.
220 * @value: The new register value.
222 * Return a negative value on failure, 0 on success.
224 int regcache_write(struct regmap
*map
,
225 unsigned int reg
, unsigned int value
)
227 if (map
->cache_type
== REGCACHE_NONE
)
230 BUG_ON(!map
->cache_ops
);
232 if (!regmap_writeable(map
, reg
))
235 if (!regmap_volatile(map
, reg
))
236 return map
->cache_ops
->write(map
, reg
, value
);
242 * regcache_sync: Sync the register cache with the hardware.
244 * @map: map to configure.
246 * Any registers that should not be synced should be marked as
247 * volatile. In general drivers can choose not to use the provided
248 * syncing functionality if they so require.
250 * Return a negative value on failure, 0 on success.
252 int regcache_sync(struct regmap
*map
)
259 BUG_ON(!map
->cache_ops
|| !map
->cache_ops
->sync
);
261 mutex_lock(&map
->lock
);
262 /* Remember the initial bypass state */
263 bypass
= map
->cache_bypass
;
264 dev_dbg(map
->dev
, "Syncing %s cache\n",
265 map
->cache_ops
->name
);
266 name
= map
->cache_ops
->name
;
267 trace_regcache_sync(map
->dev
, name
, "start");
269 if (!map
->cache_dirty
)
272 /* Apply any patch first */
273 map
->cache_bypass
= 1;
274 for (i
= 0; i
< map
->patch_regs
; i
++) {
275 ret
= _regmap_write(map
, map
->patch
[i
].reg
, map
->patch
[i
].def
);
277 dev_err(map
->dev
, "Failed to write %x = %x: %d\n",
278 map
->patch
[i
].reg
, map
->patch
[i
].def
, ret
);
282 map
->cache_bypass
= 0;
284 ret
= map
->cache_ops
->sync(map
, 0, map
->max_register
);
287 map
->cache_dirty
= false;
290 trace_regcache_sync(map
->dev
, name
, "stop");
291 /* Restore the bypass state */
292 map
->cache_bypass
= bypass
;
293 mutex_unlock(&map
->lock
);
297 EXPORT_SYMBOL_GPL(regcache_sync
);
300 * regcache_sync_region: Sync part of the register cache with the hardware.
303 * @min: first register to sync
304 * @max: last register to sync
306 * Write all non-default register values in the specified region to
309 * Return a negative value on failure, 0 on success.
311 int regcache_sync_region(struct regmap
*map
, unsigned int min
,
318 BUG_ON(!map
->cache_ops
|| !map
->cache_ops
->sync
);
320 mutex_lock(&map
->lock
);
322 /* Remember the initial bypass state */
323 bypass
= map
->cache_bypass
;
325 name
= map
->cache_ops
->name
;
326 dev_dbg(map
->dev
, "Syncing %s cache from %d-%d\n", name
, min
, max
);
328 trace_regcache_sync(map
->dev
, name
, "start region");
330 if (!map
->cache_dirty
)
333 ret
= map
->cache_ops
->sync(map
, min
, max
);
336 trace_regcache_sync(map
->dev
, name
, "stop region");
337 /* Restore the bypass state */
338 map
->cache_bypass
= bypass
;
339 mutex_unlock(&map
->lock
);
345 * regcache_cache_only: Put a register map into cache only mode
347 * @map: map to configure
348 * @cache_only: flag if changes should be written to the hardware
350 * When a register map is marked as cache only writes to the register
351 * map API will only update the register cache, they will not cause
352 * any hardware changes. This is useful for allowing portions of
353 * drivers to act as though the device were functioning as normal when
354 * it is disabled for power saving reasons.
356 void regcache_cache_only(struct regmap
*map
, bool enable
)
358 mutex_lock(&map
->lock
);
359 WARN_ON(map
->cache_bypass
&& enable
);
360 map
->cache_only
= enable
;
361 trace_regmap_cache_only(map
->dev
, enable
);
362 mutex_unlock(&map
->lock
);
364 EXPORT_SYMBOL_GPL(regcache_cache_only
);
367 * regcache_mark_dirty: Mark the register cache as dirty
371 * Mark the register cache as dirty, for example due to the device
372 * having been powered down for suspend. If the cache is not marked
373 * as dirty then the cache sync will be suppressed.
375 void regcache_mark_dirty(struct regmap
*map
)
377 mutex_lock(&map
->lock
);
378 map
->cache_dirty
= true;
379 mutex_unlock(&map
->lock
);
381 EXPORT_SYMBOL_GPL(regcache_mark_dirty
);
384 * regcache_cache_bypass: Put a register map into cache bypass mode
386 * @map: map to configure
387 * @cache_bypass: flag if changes should not be written to the hardware
389 * When a register map is marked with the cache bypass option, writes
390 * to the register map API will only update the hardware and not the
391 * the cache directly. This is useful when syncing the cache back to
394 void regcache_cache_bypass(struct regmap
*map
, bool enable
)
396 mutex_lock(&map
->lock
);
397 WARN_ON(map
->cache_only
&& enable
);
398 map
->cache_bypass
= enable
;
399 trace_regmap_cache_bypass(map
->dev
, enable
);
400 mutex_unlock(&map
->lock
);
402 EXPORT_SYMBOL_GPL(regcache_cache_bypass
);
404 bool regcache_set_val(void *base
, unsigned int idx
,
405 unsigned int val
, unsigned int word_size
)
410 if (cache
[idx
] == val
)
417 if (cache
[idx
] == val
)
424 if (cache
[idx
] == val
)
435 unsigned int regcache_get_val(const void *base
, unsigned int idx
,
436 unsigned int word_size
)
443 const u8
*cache
= base
;
447 const u16
*cache
= base
;
451 const u32
*cache
= base
;
461 static int regcache_default_cmp(const void *a
, const void *b
)
463 const struct reg_default
*_a
= a
;
464 const struct reg_default
*_b
= b
;
466 return _a
->reg
- _b
->reg
;
469 int regcache_lookup_reg(struct regmap
*map
, unsigned int reg
)
471 struct reg_default key
;
472 struct reg_default
*r
;
477 r
= bsearch(&key
, map
->reg_defaults
, map
->num_reg_defaults
,
478 sizeof(struct reg_default
), regcache_default_cmp
);
481 return r
- map
->reg_defaults
;
This page took 0.054479 seconds and 5 git commands to generate.