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[] = {
22 ®cache_indexed_ops,
27 static int regcache_hw_init(struct regmap *map)
35 if (!map->num_reg_defaults_raw)
38 if (!map->reg_defaults_raw) {
39 dev_warn(map->dev, "No cache defaults, reading back from HW\n");
40 tmp_buf = kmalloc(map->cache_size_raw, GFP_KERNEL);
43 ret = regmap_bulk_read(map, 0, tmp_buf,
44 map->num_reg_defaults_raw);
49 map->reg_defaults_raw = tmp_buf;
53 /* calculate the size of reg_defaults */
54 for (count = 0, i = 0; i < map->num_reg_defaults_raw; i++) {
55 val = regcache_get_val(map->reg_defaults_raw,
56 i, map->cache_word_size);
62 map->reg_defaults = kmalloc(count * sizeof(struct reg_default),
64 if (!map->reg_defaults) {
69 /* fill the reg_defaults */
70 map->num_reg_defaults = count;
71 for (i = 0, j = 0; i < map->num_reg_defaults_raw; i++) {
72 val = regcache_get_val(map->reg_defaults_raw,
73 i, map->cache_word_size);
76 map->reg_defaults[j].reg = i;
77 map->reg_defaults[j].def = val;
85 kfree(map->reg_defaults_raw);
90 int regcache_init(struct regmap *map, const struct regmap_config *config)
96 if (map->cache_type == REGCACHE_NONE) {
97 map->cache_bypass = true;
101 for (i = 0; i < ARRAY_SIZE(cache_types); i++)
102 if (cache_types[i]->type == map->cache_type)
105 if (i == ARRAY_SIZE(cache_types)) {
106 dev_err(map->dev, "Could not match compress type: %d\n",
111 map->num_reg_defaults = config->num_reg_defaults;
112 map->num_reg_defaults_raw = config->num_reg_defaults_raw;
113 map->reg_defaults_raw = config->reg_defaults_raw;
114 map->cache_size_raw = (config->val_bits / 8) * config->num_reg_defaults_raw;
115 map->cache_word_size = config->val_bits / 8;
118 map->cache_ops = cache_types[i];
120 if (!map->cache_ops->read ||
121 !map->cache_ops->write ||
122 !map->cache_ops->name)
125 /* We still need to ensure that the reg_defaults
126 * won't vanish from under us. We'll need to make
129 if (config->reg_defaults) {
130 if (!map->num_reg_defaults)
132 tmp_buf = kmemdup(config->reg_defaults, map->num_reg_defaults *
133 sizeof(struct reg_default), GFP_KERNEL);
136 map->reg_defaults = tmp_buf;
137 } else if (map->num_reg_defaults_raw) {
138 /* Some devices such as PMICs don't have cache defaults,
139 * we cope with this by reading back the HW registers and
140 * crafting the cache defaults by hand.
142 ret = regcache_hw_init(map);
147 if (!map->max_register)
148 map->max_register = map->num_reg_defaults_raw;
150 if (map->cache_ops->init) {
151 dev_dbg(map->dev, "Initializing %s cache\n",
152 map->cache_ops->name);
153 ret = map->cache_ops->init(map);
160 kfree(map->reg_defaults);
162 kfree(map->reg_defaults_raw);
167 void regcache_exit(struct regmap *map)
169 if (map->cache_type == REGCACHE_NONE)
172 BUG_ON(!map->cache_ops);
174 kfree(map->reg_defaults);
176 kfree(map->reg_defaults_raw);
178 if (map->cache_ops->exit) {
179 dev_dbg(map->dev, "Destroying %s cache\n",
180 map->cache_ops->name);
181 map->cache_ops->exit(map);
186 * regcache_read: Fetch the value of a given register from the cache.
188 * @map: map to configure.
189 * @reg: The register index.
190 * @value: The value to be returned.
192 * Return a negative value on failure, 0 on success.
194 int regcache_read(struct regmap *map,
195 unsigned int reg, unsigned int *value)
197 if (map->cache_type == REGCACHE_NONE)
200 BUG_ON(!map->cache_ops);
202 if (!regmap_readable(map, reg))
205 if (!regmap_volatile(map, reg))
206 return map->cache_ops->read(map, reg, value);
210 EXPORT_SYMBOL_GPL(regcache_read);
213 * regcache_write: Set the value of a given register in the cache.
215 * @map: map to configure.
216 * @reg: The register index.
217 * @value: The new register value.
219 * Return a negative value on failure, 0 on success.
221 int regcache_write(struct regmap *map,
222 unsigned int reg, unsigned int value)
224 if (map->cache_type == REGCACHE_NONE)
227 BUG_ON(!map->cache_ops);
229 if (!regmap_writeable(map, reg))
232 if (!regmap_volatile(map, reg))
233 return map->cache_ops->write(map, reg, value);
237 EXPORT_SYMBOL_GPL(regcache_write);
240 * regcache_sync: Sync the register cache with the hardware.
242 * @map: map to configure.
244 * Any registers that should not be synced should be marked as
245 * volatile. In general drivers can choose not to use the provided
246 * syncing functionality if they so require.
248 * Return a negative value on failure, 0 on success.
250 int regcache_sync(struct regmap *map)
258 BUG_ON(!map->cache_ops);
260 mutex_lock(&map->lock);
261 /* Remember the initial bypass state */
262 bypass = map->cache_bypass;
263 dev_dbg(map->dev, "Syncing %s cache\n",
264 map->cache_ops->name);
265 name = map->cache_ops->name;
266 trace_regcache_sync(map->dev, name, "start");
267 if (!map->cache_dirty)
269 if (map->cache_ops->sync) {
270 ret = map->cache_ops->sync(map);
272 for (i = 0; i < map->num_reg_defaults; i++) {
273 ret = regcache_read(map, i, &val);
276 map->cache_bypass = 1;
277 ret = _regmap_write(map, i, val);
278 map->cache_bypass = 0;
281 dev_dbg(map->dev, "Synced register %#x, value %#x\n",
282 map->reg_defaults[i].reg,
283 map->reg_defaults[i].def);
288 trace_regcache_sync(map->dev, name, "stop");
289 /* Restore the bypass state */
290 map->cache_bypass = bypass;
291 mutex_unlock(&map->lock);
295 EXPORT_SYMBOL_GPL(regcache_sync);
298 * regcache_cache_only: Put a register map into cache only mode
300 * @map: map to configure
301 * @cache_only: flag if changes should be written to the hardware
303 * When a register map is marked as cache only writes to the register
304 * map API will only update the register cache, they will not cause
305 * any hardware changes. This is useful for allowing portions of
306 * drivers to act as though the device were functioning as normal when
307 * it is disabled for power saving reasons.
309 void regcache_cache_only(struct regmap *map, bool enable)
311 mutex_lock(&map->lock);
312 WARN_ON(map->cache_bypass && enable);
313 map->cache_only = enable;
314 mutex_unlock(&map->lock);
316 EXPORT_SYMBOL_GPL(regcache_cache_only);
319 * regcache_mark_dirty: Mark the register cache as dirty
323 * Mark the register cache as dirty, for example due to the device
324 * having been powered down for suspend. If the cache is not marked
325 * as dirty then the cache sync will be suppressed.
327 void regcache_mark_dirty(struct regmap *map)
329 mutex_lock(&map->lock);
330 map->cache_dirty = true;
331 mutex_unlock(&map->lock);
333 EXPORT_SYMBOL_GPL(regcache_mark_dirty);
336 * regcache_cache_bypass: Put a register map into cache bypass mode
338 * @map: map to configure
339 * @cache_bypass: flag if changes should not be written to the hardware
341 * When a register map is marked with the cache bypass option, writes
342 * to the register map API will only update the hardware and not the
343 * the cache directly. This is useful when syncing the cache back to
346 void regcache_cache_bypass(struct regmap *map, bool enable)
348 mutex_lock(&map->lock);
349 WARN_ON(map->cache_only && enable);
350 map->cache_bypass = enable;
351 mutex_unlock(&map->lock);
353 EXPORT_SYMBOL_GPL(regcache_cache_bypass);
355 bool regcache_set_val(void *base, unsigned int idx,
356 unsigned int val, unsigned int word_size)
361 if (cache[idx] == val)
368 if (cache[idx] == val)
380 unsigned int regcache_get_val(const void *base, unsigned int idx,
381 unsigned int word_size)
388 const u8 *cache = base;
392 const u16 *cache = base;
402 static int regcache_default_cmp(const void *a, const void *b)
404 const struct reg_default *_a = a;
405 const struct reg_default *_b = b;
407 return _a->reg - _b->reg;
410 int regcache_lookup_reg(struct regmap *map, unsigned int reg)
412 struct reg_default key;
413 struct reg_default *r;
418 r = bsearch(&key, map->reg_defaults, map->num_reg_defaults,
419 sizeof(struct reg_default), regcache_default_cmp);
422 return r - map->reg_defaults;
427 int regcache_insert_reg(struct regmap *map, unsigned int reg,
432 tmp = krealloc(map->reg_defaults,
433 (map->num_reg_defaults + 1) * sizeof(struct reg_default),
437 map->reg_defaults = tmp;
438 map->num_reg_defaults++;
439 map->reg_defaults[map->num_reg_defaults - 1].reg = reg;
440 map->reg_defaults[map->num_reg_defaults - 1].def = val;
441 sort(map->reg_defaults, map->num_reg_defaults,
442 sizeof(struct reg_default), regcache_default_cmp, NULL);