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 <trace/events/regmap.h>
18 static const struct regcache_ops *cache_types[] = {
19 ®cache_indexed_ops,
24 static int regcache_hw_init(struct regmap *map)
32 if (!map->num_reg_defaults_raw)
35 if (!map->reg_defaults_raw) {
36 dev_warn(map->dev, "No cache defaults, reading back from HW\n");
37 tmp_buf = kmalloc(map->cache_size_raw, GFP_KERNEL);
40 ret = regmap_bulk_read(map, 0, tmp_buf,
41 map->num_reg_defaults_raw);
46 map->reg_defaults_raw = tmp_buf;
50 /* calculate the size of reg_defaults */
51 for (count = 0, i = 0; i < map->num_reg_defaults_raw; i++) {
52 val = regcache_get_val(map->reg_defaults_raw,
53 i, map->cache_word_size);
59 map->reg_defaults = kmalloc(count * sizeof(struct reg_default),
61 if (!map->reg_defaults)
64 /* fill the reg_defaults */
65 map->num_reg_defaults = count;
66 for (i = 0, j = 0; i < map->num_reg_defaults_raw; i++) {
67 val = regcache_get_val(map->reg_defaults_raw,
68 i, map->cache_word_size);
71 map->reg_defaults[j].reg = i;
72 map->reg_defaults[j].def = val;
79 int regcache_init(struct regmap *map)
85 if (map->cache_type == REGCACHE_NONE) {
86 map->cache_bypass = true;
90 for (i = 0; i < ARRAY_SIZE(cache_types); i++)
91 if (cache_types[i]->type == map->cache_type)
94 if (i == ARRAY_SIZE(cache_types)) {
95 dev_err(map->dev, "Could not match compress type: %d\n",
101 map->cache_ops = cache_types[i];
103 if (!map->cache_ops->read ||
104 !map->cache_ops->write ||
105 !map->cache_ops->name)
108 /* We still need to ensure that the reg_defaults
109 * won't vanish from under us. We'll need to make
112 if (map->reg_defaults) {
113 if (!map->num_reg_defaults)
115 tmp_buf = kmemdup(map->reg_defaults, map->num_reg_defaults *
116 sizeof(struct reg_default), GFP_KERNEL);
119 map->reg_defaults = tmp_buf;
121 /* Some devices such as PMICs don't have cache defaults,
122 * we cope with this by reading back the HW registers and
123 * crafting the cache defaults by hand.
125 ret = regcache_hw_init(map);
130 if (!map->max_register)
131 map->max_register = map->num_reg_defaults_raw;
133 if (map->cache_ops->init) {
134 dev_dbg(map->dev, "Initializing %s cache\n",
135 map->cache_ops->name);
136 return map->cache_ops->init(map);
141 void regcache_exit(struct regmap *map)
143 if (map->cache_type == REGCACHE_NONE)
146 BUG_ON(!map->cache_ops);
148 kfree(map->reg_defaults);
150 kfree(map->reg_defaults_raw);
152 if (map->cache_ops->exit) {
153 dev_dbg(map->dev, "Destroying %s cache\n",
154 map->cache_ops->name);
155 map->cache_ops->exit(map);
160 * regcache_read: Fetch the value of a given register from the cache.
162 * @map: map to configure.
163 * @reg: The register index.
164 * @value: The value to be returned.
166 * Return a negative value on failure, 0 on success.
168 int regcache_read(struct regmap *map,
169 unsigned int reg, unsigned int *value)
171 if (map->cache_type == REGCACHE_NONE)
174 BUG_ON(!map->cache_ops);
176 if (!regmap_readable(map, reg))
179 if (!regmap_volatile(map, reg))
180 return map->cache_ops->read(map, reg, value);
184 EXPORT_SYMBOL_GPL(regcache_read);
187 * regcache_write: Set the value of a given register in the cache.
189 * @map: map to configure.
190 * @reg: The register index.
191 * @value: The new register value.
193 * Return a negative value on failure, 0 on success.
195 int regcache_write(struct regmap *map,
196 unsigned int reg, unsigned int value)
198 if (map->cache_type == REGCACHE_NONE)
201 BUG_ON(!map->cache_ops);
203 if (!regmap_writeable(map, reg))
206 if (!regmap_volatile(map, reg))
207 return map->cache_ops->write(map, reg, value);
211 EXPORT_SYMBOL_GPL(regcache_write);
214 * regcache_sync: Sync the register cache with the hardware.
216 * @map: map to configure.
218 * Any registers that should not be synced should be marked as
219 * volatile. In general drivers can choose not to use the provided
220 * syncing functionality if they so require.
222 * Return a negative value on failure, 0 on success.
224 int regcache_sync(struct regmap *map)
231 BUG_ON(!map->cache_ops);
233 mutex_lock(&map->lock);
234 dev_dbg(map->dev, "Syncing %s cache\n",
235 map->cache_ops->name);
236 name = map->cache_ops->name;
237 trace_regcache_sync(map->dev, name, "start");
238 if (map->cache_ops->sync) {
239 ret = map->cache_ops->sync(map);
241 for (i = 0; i < map->num_reg_defaults; i++) {
242 ret = regcache_read(map, i, &val);
245 map->cache_bypass = 1;
246 ret = _regmap_write(map, i, val);
247 map->cache_bypass = 0;
250 dev_dbg(map->dev, "Synced register %#x, value %#x\n",
251 map->reg_defaults[i].reg,
252 map->reg_defaults[i].def);
257 trace_regcache_sync(map->dev, name, "stop");
258 mutex_unlock(&map->lock);
262 EXPORT_SYMBOL_GPL(regcache_sync);
265 * regcache_cache_only: Put a register map into cache only mode
267 * @map: map to configure
268 * @cache_only: flag if changes should be written to the hardware
270 * When a register map is marked as cache only writes to the register
271 * map API will only update the register cache, they will not cause
272 * any hardware changes. This is useful for allowing portions of
273 * drivers to act as though the device were functioning as normal when
274 * it is disabled for power saving reasons.
276 void regcache_cache_only(struct regmap *map, bool enable)
278 mutex_lock(&map->lock);
279 map->cache_only = enable;
280 mutex_unlock(&map->lock);
282 EXPORT_SYMBOL_GPL(regcache_cache_only);
284 bool regcache_set_val(void *base, unsigned int idx,
285 unsigned int val, unsigned int word_size)
290 if (cache[idx] == val)
297 if (cache[idx] == val)
309 unsigned int regcache_get_val(const void *base, unsigned int idx,
310 unsigned int word_size)
317 const u8 *cache = base;
321 const u16 *cache = base;
331 int regcache_lookup_reg(struct regmap *map, unsigned int reg)
335 for (i = 0; i < map->num_reg_defaults; i++)
336 if (map->reg_defaults[i].reg == reg)
341 int regcache_insert_reg(struct regmap *map, unsigned int reg,
346 tmp = krealloc(map->reg_defaults,
347 (map->num_reg_defaults + 1) * sizeof(struct reg_default),
351 map->reg_defaults = tmp;
352 map->num_reg_defaults++;
353 map->reg_defaults[map->num_reg_defaults - 1].reg = reg;
354 map->reg_defaults[map->num_reg_defaults - 1].def = val;