blob: 4b903a8e92a28c0c0de6fcea5478665cdcbcae76 [file] [log] [blame]
Dimitris Papastamos9fabe242011-09-19 14:34:00 +01001/*
2 * Register cache access API
3 *
4 * Copyright 2011 Wolfson Microelectronics plc
5 *
6 * Author: Dimitris Papastamos <dp@opensource.wolfsonmicro.com>
7 *
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.
11 */
12
13#include <linux/slab.h>
Paul Gortmaker1b6bc322011-05-27 07:12:15 -040014#include <linux/export.h>
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010015#include <trace/events/regmap.h>
Mark Brownf094fea2011-10-04 22:05:47 +010016#include <linux/bsearch.h>
Dimitris Papastamosc08604b2011-10-03 10:50:14 +010017#include <linux/sort.h>
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010018
19#include "internal.h"
20
21static const struct regcache_ops *cache_types[] = {
Dimitris Papastamos28644c802011-09-19 14:34:02 +010022 &regcache_rbtree_ops,
Dimitris Papastamos2cbbb572011-09-19 14:34:03 +010023 &regcache_lzo_ops,
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010024};
25
26static int regcache_hw_init(struct regmap *map)
27{
28 int i, j;
29 int ret;
30 int count;
31 unsigned int val;
32 void *tmp_buf;
33
34 if (!map->num_reg_defaults_raw)
35 return -EINVAL;
36
37 if (!map->reg_defaults_raw) {
Laxman Dewangandf00c792012-02-17 18:57:26 +053038 u32 cache_bypass = map->cache_bypass;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010039 dev_warn(map->dev, "No cache defaults, reading back from HW\n");
Laxman Dewangandf00c792012-02-17 18:57:26 +053040
41 /* Bypass the cache access till data read from HW*/
42 map->cache_bypass = 1;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010043 tmp_buf = kmalloc(map->cache_size_raw, GFP_KERNEL);
44 if (!tmp_buf)
45 return -EINVAL;
46 ret = regmap_bulk_read(map, 0, tmp_buf,
47 map->num_reg_defaults_raw);
Laxman Dewangandf00c792012-02-17 18:57:26 +053048 map->cache_bypass = cache_bypass;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010049 if (ret < 0) {
50 kfree(tmp_buf);
51 return ret;
52 }
53 map->reg_defaults_raw = tmp_buf;
54 map->cache_free = 1;
55 }
56
57 /* calculate the size of reg_defaults */
58 for (count = 0, i = 0; i < map->num_reg_defaults_raw; i++) {
59 val = regcache_get_val(map->reg_defaults_raw,
60 i, map->cache_word_size);
61 if (!val)
62 continue;
63 count++;
64 }
65
66 map->reg_defaults = kmalloc(count * sizeof(struct reg_default),
67 GFP_KERNEL);
Lars-Peter Clausen021cd612011-11-14 10:40:16 +010068 if (!map->reg_defaults) {
69 ret = -ENOMEM;
70 goto err_free;
71 }
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010072
73 /* fill the reg_defaults */
74 map->num_reg_defaults = count;
75 for (i = 0, j = 0; i < map->num_reg_defaults_raw; i++) {
76 val = regcache_get_val(map->reg_defaults_raw,
77 i, map->cache_word_size);
78 if (!val)
79 continue;
80 map->reg_defaults[j].reg = i;
81 map->reg_defaults[j].def = val;
82 j++;
83 }
84
85 return 0;
Lars-Peter Clausen021cd612011-11-14 10:40:16 +010086
87err_free:
88 if (map->cache_free)
89 kfree(map->reg_defaults_raw);
90
91 return ret;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010092}
93
Lars-Peter Clausene5e3b8a2011-11-16 16:28:16 +010094int regcache_init(struct regmap *map, const struct regmap_config *config)
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010095{
96 int ret;
97 int i;
98 void *tmp_buf;
99
Mark Browne7a6db32011-09-19 16:08:03 +0100100 if (map->cache_type == REGCACHE_NONE) {
101 map->cache_bypass = true;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100102 return 0;
Mark Browne7a6db32011-09-19 16:08:03 +0100103 }
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100104
105 for (i = 0; i < ARRAY_SIZE(cache_types); i++)
106 if (cache_types[i]->type == map->cache_type)
107 break;
108
109 if (i == ARRAY_SIZE(cache_types)) {
110 dev_err(map->dev, "Could not match compress type: %d\n",
111 map->cache_type);
112 return -EINVAL;
113 }
114
Lars-Peter Clausene5e3b8a2011-11-16 16:28:16 +0100115 map->num_reg_defaults = config->num_reg_defaults;
116 map->num_reg_defaults_raw = config->num_reg_defaults_raw;
117 map->reg_defaults_raw = config->reg_defaults_raw;
Lars-Peter Clausen064d4db2011-11-16 20:34:03 +0100118 map->cache_word_size = DIV_ROUND_UP(config->val_bits, 8);
119 map->cache_size_raw = map->cache_word_size * config->num_reg_defaults_raw;
Lars-Peter Clausene5e3b8a2011-11-16 16:28:16 +0100120
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100121 map->cache = NULL;
122 map->cache_ops = cache_types[i];
123
124 if (!map->cache_ops->read ||
125 !map->cache_ops->write ||
126 !map->cache_ops->name)
127 return -EINVAL;
128
129 /* We still need to ensure that the reg_defaults
130 * won't vanish from under us. We'll need to make
131 * a copy of it.
132 */
Lars-Peter Clausen720e4612011-11-16 16:28:17 +0100133 if (config->reg_defaults) {
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100134 if (!map->num_reg_defaults)
135 return -EINVAL;
Lars-Peter Clausen720e4612011-11-16 16:28:17 +0100136 tmp_buf = kmemdup(config->reg_defaults, map->num_reg_defaults *
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100137 sizeof(struct reg_default), GFP_KERNEL);
138 if (!tmp_buf)
139 return -ENOMEM;
140 map->reg_defaults = tmp_buf;
Mark Brown8528bdd2011-10-09 13:13:58 +0100141 } else if (map->num_reg_defaults_raw) {
Mark Brown5fcd2562011-09-29 15:24:54 +0100142 /* Some devices such as PMICs don't have cache defaults,
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100143 * we cope with this by reading back the HW registers and
144 * crafting the cache defaults by hand.
145 */
146 ret = regcache_hw_init(map);
147 if (ret < 0)
148 return ret;
149 }
150
151 if (!map->max_register)
152 map->max_register = map->num_reg_defaults_raw;
153
154 if (map->cache_ops->init) {
155 dev_dbg(map->dev, "Initializing %s cache\n",
156 map->cache_ops->name);
Lars-Peter Clausenbd061c72011-11-14 10:40:17 +0100157 ret = map->cache_ops->init(map);
158 if (ret)
159 goto err_free;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100160 }
161 return 0;
Lars-Peter Clausenbd061c72011-11-14 10:40:17 +0100162
163err_free:
164 kfree(map->reg_defaults);
165 if (map->cache_free)
166 kfree(map->reg_defaults_raw);
167
168 return ret;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100169}
170
171void regcache_exit(struct regmap *map)
172{
173 if (map->cache_type == REGCACHE_NONE)
174 return;
175
176 BUG_ON(!map->cache_ops);
177
178 kfree(map->reg_defaults);
179 if (map->cache_free)
180 kfree(map->reg_defaults_raw);
181
182 if (map->cache_ops->exit) {
183 dev_dbg(map->dev, "Destroying %s cache\n",
184 map->cache_ops->name);
185 map->cache_ops->exit(map);
186 }
187}
188
189/**
190 * regcache_read: Fetch the value of a given register from the cache.
191 *
192 * @map: map to configure.
193 * @reg: The register index.
194 * @value: The value to be returned.
195 *
196 * Return a negative value on failure, 0 on success.
197 */
198int regcache_read(struct regmap *map,
199 unsigned int reg, unsigned int *value)
200{
Mark Brownbc7ee552011-11-30 14:27:08 +0000201 int ret;
202
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100203 if (map->cache_type == REGCACHE_NONE)
204 return -ENOSYS;
205
206 BUG_ON(!map->cache_ops);
207
Mark Brownbc7ee552011-11-30 14:27:08 +0000208 if (!regmap_volatile(map, reg)) {
209 ret = map->cache_ops->read(map, reg, value);
210
211 if (ret == 0)
212 trace_regmap_reg_read_cache(map->dev, reg, *value);
213
214 return ret;
215 }
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100216
217 return -EINVAL;
218}
219EXPORT_SYMBOL_GPL(regcache_read);
220
221/**
222 * regcache_write: Set the value of a given register in the cache.
223 *
224 * @map: map to configure.
225 * @reg: The register index.
226 * @value: The new register value.
227 *
228 * Return a negative value on failure, 0 on success.
229 */
230int regcache_write(struct regmap *map,
231 unsigned int reg, unsigned int value)
232{
233 if (map->cache_type == REGCACHE_NONE)
234 return 0;
235
236 BUG_ON(!map->cache_ops);
237
238 if (!regmap_writeable(map, reg))
239 return -EIO;
240
241 if (!regmap_volatile(map, reg))
242 return map->cache_ops->write(map, reg, value);
243
244 return 0;
245}
246EXPORT_SYMBOL_GPL(regcache_write);
247
248/**
249 * regcache_sync: Sync the register cache with the hardware.
250 *
251 * @map: map to configure.
252 *
253 * Any registers that should not be synced should be marked as
254 * volatile. In general drivers can choose not to use the provided
255 * syncing functionality if they so require.
256 *
257 * Return a negative value on failure, 0 on success.
258 */
259int regcache_sync(struct regmap *map)
260{
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100261 int ret = 0;
262 unsigned int val;
263 unsigned int i;
Dimitris Papastamos59360082011-09-19 14:34:04 +0100264 const char *name;
Dimitris Papastamosbeb1a102011-09-29 14:36:26 +0100265 unsigned int bypass;
Dimitris Papastamos59360082011-09-19 14:34:04 +0100266
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100267 BUG_ON(!map->cache_ops);
268
Dimitris Papastamos13753a92011-09-29 14:36:25 +0100269 mutex_lock(&map->lock);
Dimitris Papastamosbeb1a102011-09-29 14:36:26 +0100270 /* Remember the initial bypass state */
271 bypass = map->cache_bypass;
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100272 dev_dbg(map->dev, "Syncing %s cache\n",
273 map->cache_ops->name);
274 name = map->cache_ops->name;
275 trace_regcache_sync(map->dev, name, "start");
Mark Brown8ae0d7e2011-10-26 10:34:22 +0200276 if (!map->cache_dirty)
277 goto out;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100278 if (map->cache_ops->sync) {
Dimitris Papastamos59360082011-09-19 14:34:04 +0100279 ret = map->cache_ops->sync(map);
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100280 } else {
281 for (i = 0; i < map->num_reg_defaults; i++) {
282 ret = regcache_read(map, i, &val);
283 if (ret < 0)
284 goto out;
Dimitris Papastamosec8a3652011-09-28 11:43:42 +0100285 map->cache_bypass = 1;
Dimitris Papastamos13753a92011-09-29 14:36:25 +0100286 ret = _regmap_write(map, i, val);
Dimitris Papastamosec8a3652011-09-28 11:43:42 +0100287 map->cache_bypass = 0;
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100288 if (ret < 0)
289 goto out;
290 dev_dbg(map->dev, "Synced register %#x, value %#x\n",
291 map->reg_defaults[i].reg,
292 map->reg_defaults[i].def);
293 }
294
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100295 }
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100296out:
297 trace_regcache_sync(map->dev, name, "stop");
Dimitris Papastamosbeb1a102011-09-29 14:36:26 +0100298 /* Restore the bypass state */
299 map->cache_bypass = bypass;
Dimitris Papastamos13753a92011-09-29 14:36:25 +0100300 mutex_unlock(&map->lock);
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100301
302 return ret;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100303}
304EXPORT_SYMBOL_GPL(regcache_sync);
305
Mark Brown92afb282011-09-19 18:22:14 +0100306/**
307 * regcache_cache_only: Put a register map into cache only mode
308 *
309 * @map: map to configure
310 * @cache_only: flag if changes should be written to the hardware
311 *
312 * When a register map is marked as cache only writes to the register
313 * map API will only update the register cache, they will not cause
314 * any hardware changes. This is useful for allowing portions of
315 * drivers to act as though the device were functioning as normal when
316 * it is disabled for power saving reasons.
317 */
318void regcache_cache_only(struct regmap *map, bool enable)
319{
Mark Brown2cd148f2011-09-29 10:40:55 +0100320 mutex_lock(&map->lock);
Dimitris Papastamosac77a762011-09-29 14:36:28 +0100321 WARN_ON(map->cache_bypass && enable);
Mark Brown92afb282011-09-19 18:22:14 +0100322 map->cache_only = enable;
Mark Brown2cd148f2011-09-29 10:40:55 +0100323 mutex_unlock(&map->lock);
Mark Brown92afb282011-09-19 18:22:14 +0100324}
325EXPORT_SYMBOL_GPL(regcache_cache_only);
326
Dimitris Papastamos6eb0f5e2011-09-29 14:36:27 +0100327/**
Mark Brown8ae0d7e2011-10-26 10:34:22 +0200328 * regcache_mark_dirty: Mark the register cache as dirty
329 *
330 * @map: map to mark
331 *
332 * Mark the register cache as dirty, for example due to the device
333 * having been powered down for suspend. If the cache is not marked
334 * as dirty then the cache sync will be suppressed.
335 */
336void regcache_mark_dirty(struct regmap *map)
337{
338 mutex_lock(&map->lock);
339 map->cache_dirty = true;
340 mutex_unlock(&map->lock);
341}
342EXPORT_SYMBOL_GPL(regcache_mark_dirty);
343
344/**
Dimitris Papastamos6eb0f5e2011-09-29 14:36:27 +0100345 * regcache_cache_bypass: Put a register map into cache bypass mode
346 *
347 * @map: map to configure
Dimitris Papastamos0eef6b02011-10-03 06:54:16 +0100348 * @cache_bypass: flag if changes should not be written to the hardware
Dimitris Papastamos6eb0f5e2011-09-29 14:36:27 +0100349 *
350 * When a register map is marked with the cache bypass option, writes
351 * to the register map API will only update the hardware and not the
352 * the cache directly. This is useful when syncing the cache back to
353 * the hardware.
354 */
355void regcache_cache_bypass(struct regmap *map, bool enable)
356{
357 mutex_lock(&map->lock);
Dimitris Papastamosac77a762011-09-29 14:36:28 +0100358 WARN_ON(map->cache_only && enable);
Dimitris Papastamos6eb0f5e2011-09-29 14:36:27 +0100359 map->cache_bypass = enable;
360 mutex_unlock(&map->lock);
361}
362EXPORT_SYMBOL_GPL(regcache_cache_bypass);
363
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100364bool regcache_set_val(void *base, unsigned int idx,
365 unsigned int val, unsigned int word_size)
366{
367 switch (word_size) {
368 case 1: {
369 u8 *cache = base;
370 if (cache[idx] == val)
371 return true;
372 cache[idx] = val;
373 break;
374 }
375 case 2: {
376 u16 *cache = base;
377 if (cache[idx] == val)
378 return true;
379 cache[idx] = val;
380 break;
381 }
382 default:
383 BUG();
384 }
385 /* unreachable */
386 return false;
387}
388
389unsigned int regcache_get_val(const void *base, unsigned int idx,
390 unsigned int word_size)
391{
392 if (!base)
393 return -EINVAL;
394
395 switch (word_size) {
396 case 1: {
397 const u8 *cache = base;
398 return cache[idx];
399 }
400 case 2: {
401 const u16 *cache = base;
402 return cache[idx];
403 }
404 default:
405 BUG();
406 }
407 /* unreachable */
408 return -1;
409}
410
Mark Brownf094fea2011-10-04 22:05:47 +0100411static int regcache_default_cmp(const void *a, const void *b)
Dimitris Papastamosc08604b2011-10-03 10:50:14 +0100412{
413 const struct reg_default *_a = a;
414 const struct reg_default *_b = b;
415
416 return _a->reg - _b->reg;
417}
418
Mark Brownf094fea2011-10-04 22:05:47 +0100419int regcache_lookup_reg(struct regmap *map, unsigned int reg)
420{
421 struct reg_default key;
422 struct reg_default *r;
423
424 key.reg = reg;
425 key.def = 0;
426
427 r = bsearch(&key, map->reg_defaults, map->num_reg_defaults,
428 sizeof(struct reg_default), regcache_default_cmp);
429
430 if (r)
431 return r - map->reg_defaults;
432 else
Mark Brown6e6ace02011-10-09 13:23:31 +0100433 return -ENOENT;
Mark Brownf094fea2011-10-04 22:05:47 +0100434}