blob: 835883bda977c27cdeb1d5ec45e63a6e69c47269 [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>
Paul Gortmaker51990e82012-01-22 11:23:42 -050015#include <linux/device.h>
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010016#include <trace/events/regmap.h>
Mark Brownf094fea2011-10-04 22:05:47 +010017#include <linux/bsearch.h>
Dimitris Papastamosc08604b2011-10-03 10:50:14 +010018#include <linux/sort.h>
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010019
20#include "internal.h"
21
22static const struct regcache_ops *cache_types[] = {
Dimitris Papastamos28644c802011-09-19 14:34:02 +010023 &regcache_rbtree_ops,
Dimitris Papastamos2cbbb572011-09-19 14:34:03 +010024 &regcache_lzo_ops,
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010025};
26
27static int regcache_hw_init(struct regmap *map)
28{
29 int i, j;
30 int ret;
31 int count;
32 unsigned int val;
33 void *tmp_buf;
34
35 if (!map->num_reg_defaults_raw)
36 return -EINVAL;
37
38 if (!map->reg_defaults_raw) {
Laxman Dewangandf00c792012-02-17 18:57:26 +053039 u32 cache_bypass = map->cache_bypass;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010040 dev_warn(map->dev, "No cache defaults, reading back from HW\n");
Laxman Dewangandf00c792012-02-17 18:57:26 +053041
42 /* Bypass the cache access till data read from HW*/
43 map->cache_bypass = 1;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010044 tmp_buf = kmalloc(map->cache_size_raw, GFP_KERNEL);
45 if (!tmp_buf)
46 return -EINVAL;
47 ret = regmap_bulk_read(map, 0, tmp_buf,
48 map->num_reg_defaults_raw);
Laxman Dewangandf00c792012-02-17 18:57:26 +053049 map->cache_bypass = cache_bypass;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010050 if (ret < 0) {
51 kfree(tmp_buf);
52 return ret;
53 }
54 map->reg_defaults_raw = tmp_buf;
55 map->cache_free = 1;
56 }
57
58 /* calculate the size of reg_defaults */
59 for (count = 0, i = 0; i < map->num_reg_defaults_raw; i++) {
60 val = regcache_get_val(map->reg_defaults_raw,
61 i, map->cache_word_size);
Stephen Warrenf01ee602012-04-09 13:40:24 -060062 if (regmap_volatile(map, i * map->reg_stride))
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010063 continue;
64 count++;
65 }
66
67 map->reg_defaults = kmalloc(count * sizeof(struct reg_default),
68 GFP_KERNEL);
Lars-Peter Clausen021cd612011-11-14 10:40:16 +010069 if (!map->reg_defaults) {
70 ret = -ENOMEM;
71 goto err_free;
72 }
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010073
74 /* fill the reg_defaults */
75 map->num_reg_defaults = count;
76 for (i = 0, j = 0; i < map->num_reg_defaults_raw; i++) {
77 val = regcache_get_val(map->reg_defaults_raw,
78 i, map->cache_word_size);
Stephen Warrenf01ee602012-04-09 13:40:24 -060079 if (regmap_volatile(map, i * map->reg_stride))
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010080 continue;
Stephen Warrenf01ee602012-04-09 13:40:24 -060081 map->reg_defaults[j].reg = i * map->reg_stride;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010082 map->reg_defaults[j].def = val;
83 j++;
84 }
85
86 return 0;
Lars-Peter Clausen021cd612011-11-14 10:40:16 +010087
88err_free:
89 if (map->cache_free)
90 kfree(map->reg_defaults_raw);
91
92 return ret;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010093}
94
Lars-Peter Clausene5e3b8a2011-11-16 16:28:16 +010095int regcache_init(struct regmap *map, const struct regmap_config *config)
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010096{
97 int ret;
98 int i;
99 void *tmp_buf;
100
Stephen Warrenf01ee602012-04-09 13:40:24 -0600101 for (i = 0; i < config->num_reg_defaults; i++)
102 if (config->reg_defaults[i].reg % map->reg_stride)
103 return -EINVAL;
104
Mark Browne7a6db32011-09-19 16:08:03 +0100105 if (map->cache_type == REGCACHE_NONE) {
106 map->cache_bypass = true;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100107 return 0;
Mark Browne7a6db32011-09-19 16:08:03 +0100108 }
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100109
110 for (i = 0; i < ARRAY_SIZE(cache_types); i++)
111 if (cache_types[i]->type == map->cache_type)
112 break;
113
114 if (i == ARRAY_SIZE(cache_types)) {
115 dev_err(map->dev, "Could not match compress type: %d\n",
116 map->cache_type);
117 return -EINVAL;
118 }
119
Lars-Peter Clausene5e3b8a2011-11-16 16:28:16 +0100120 map->num_reg_defaults = config->num_reg_defaults;
121 map->num_reg_defaults_raw = config->num_reg_defaults_raw;
122 map->reg_defaults_raw = config->reg_defaults_raw;
Lars-Peter Clausen064d4db2011-11-16 20:34:03 +0100123 map->cache_word_size = DIV_ROUND_UP(config->val_bits, 8);
124 map->cache_size_raw = map->cache_word_size * config->num_reg_defaults_raw;
Lars-Peter Clausene5e3b8a2011-11-16 16:28:16 +0100125
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100126 map->cache = NULL;
127 map->cache_ops = cache_types[i];
128
129 if (!map->cache_ops->read ||
130 !map->cache_ops->write ||
131 !map->cache_ops->name)
132 return -EINVAL;
133
134 /* We still need to ensure that the reg_defaults
135 * won't vanish from under us. We'll need to make
136 * a copy of it.
137 */
Lars-Peter Clausen720e4612011-11-16 16:28:17 +0100138 if (config->reg_defaults) {
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100139 if (!map->num_reg_defaults)
140 return -EINVAL;
Lars-Peter Clausen720e4612011-11-16 16:28:17 +0100141 tmp_buf = kmemdup(config->reg_defaults, map->num_reg_defaults *
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100142 sizeof(struct reg_default), GFP_KERNEL);
143 if (!tmp_buf)
144 return -ENOMEM;
145 map->reg_defaults = tmp_buf;
Mark Brown8528bdd2011-10-09 13:13:58 +0100146 } else if (map->num_reg_defaults_raw) {
Mark Brown5fcd2562011-09-29 15:24:54 +0100147 /* Some devices such as PMICs don't have cache defaults,
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100148 * we cope with this by reading back the HW registers and
149 * crafting the cache defaults by hand.
150 */
151 ret = regcache_hw_init(map);
152 if (ret < 0)
153 return ret;
154 }
155
156 if (!map->max_register)
157 map->max_register = map->num_reg_defaults_raw;
158
159 if (map->cache_ops->init) {
160 dev_dbg(map->dev, "Initializing %s cache\n",
161 map->cache_ops->name);
Lars-Peter Clausenbd061c72011-11-14 10:40:17 +0100162 ret = map->cache_ops->init(map);
163 if (ret)
164 goto err_free;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100165 }
166 return 0;
Lars-Peter Clausenbd061c72011-11-14 10:40:17 +0100167
168err_free:
169 kfree(map->reg_defaults);
170 if (map->cache_free)
171 kfree(map->reg_defaults_raw);
172
173 return ret;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100174}
175
176void regcache_exit(struct regmap *map)
177{
178 if (map->cache_type == REGCACHE_NONE)
179 return;
180
181 BUG_ON(!map->cache_ops);
182
183 kfree(map->reg_defaults);
184 if (map->cache_free)
185 kfree(map->reg_defaults_raw);
186
187 if (map->cache_ops->exit) {
188 dev_dbg(map->dev, "Destroying %s cache\n",
189 map->cache_ops->name);
190 map->cache_ops->exit(map);
191 }
192}
193
194/**
195 * regcache_read: Fetch the value of a given register from the cache.
196 *
197 * @map: map to configure.
198 * @reg: The register index.
199 * @value: The value to be returned.
200 *
201 * Return a negative value on failure, 0 on success.
202 */
203int regcache_read(struct regmap *map,
204 unsigned int reg, unsigned int *value)
205{
Mark Brownbc7ee552011-11-30 14:27:08 +0000206 int ret;
207
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100208 if (map->cache_type == REGCACHE_NONE)
209 return -ENOSYS;
210
211 BUG_ON(!map->cache_ops);
212
Mark Brownbc7ee552011-11-30 14:27:08 +0000213 if (!regmap_volatile(map, reg)) {
214 ret = map->cache_ops->read(map, reg, value);
215
216 if (ret == 0)
217 trace_regmap_reg_read_cache(map->dev, reg, *value);
218
219 return ret;
220 }
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100221
222 return -EINVAL;
223}
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100224
225/**
226 * regcache_write: Set the value of a given register in the cache.
227 *
228 * @map: map to configure.
229 * @reg: The register index.
230 * @value: The new register value.
231 *
232 * Return a negative value on failure, 0 on success.
233 */
234int regcache_write(struct regmap *map,
235 unsigned int reg, unsigned int value)
236{
237 if (map->cache_type == REGCACHE_NONE)
238 return 0;
239
240 BUG_ON(!map->cache_ops);
241
242 if (!regmap_writeable(map, reg))
243 return -EIO;
244
245 if (!regmap_volatile(map, reg))
246 return map->cache_ops->write(map, reg, value);
247
248 return 0;
249}
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100250
251/**
252 * regcache_sync: Sync the register cache with the hardware.
253 *
254 * @map: map to configure.
255 *
256 * Any registers that should not be synced should be marked as
257 * volatile. In general drivers can choose not to use the provided
258 * syncing functionality if they so require.
259 *
260 * Return a negative value on failure, 0 on success.
261 */
262int regcache_sync(struct regmap *map)
263{
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100264 int ret = 0;
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100265 unsigned int i;
Dimitris Papastamos59360082011-09-19 14:34:04 +0100266 const char *name;
Dimitris Papastamosbeb1a102011-09-29 14:36:26 +0100267 unsigned int bypass;
Dimitris Papastamos59360082011-09-19 14:34:04 +0100268
Mark Brownc3ec2322012-02-23 20:48:40 +0000269 BUG_ON(!map->cache_ops || !map->cache_ops->sync);
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100270
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600271 map->lock(map);
Dimitris Papastamosbeb1a102011-09-29 14:36:26 +0100272 /* Remember the initial bypass state */
273 bypass = map->cache_bypass;
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100274 dev_dbg(map->dev, "Syncing %s cache\n",
275 map->cache_ops->name);
276 name = map->cache_ops->name;
277 trace_regcache_sync(map->dev, name, "start");
Mark Brown22f0d902012-01-21 12:01:14 +0000278
Mark Brown8ae0d7e2011-10-26 10:34:22 +0200279 if (!map->cache_dirty)
280 goto out;
Mark Brownd9db7622012-01-25 21:06:33 +0000281
Mark Brown22f0d902012-01-21 12:01:14 +0000282 /* Apply any patch first */
Mark Brown8a892d62012-01-25 21:05:48 +0000283 map->cache_bypass = 1;
Mark Brown22f0d902012-01-21 12:01:14 +0000284 for (i = 0; i < map->patch_regs; i++) {
Stephen Warrenf01ee602012-04-09 13:40:24 -0600285 if (map->patch[i].reg % map->reg_stride) {
286 ret = -EINVAL;
287 goto out;
288 }
Mark Brown22f0d902012-01-21 12:01:14 +0000289 ret = _regmap_write(map, map->patch[i].reg, map->patch[i].def);
290 if (ret != 0) {
291 dev_err(map->dev, "Failed to write %x = %x: %d\n",
292 map->patch[i].reg, map->patch[i].def, ret);
293 goto out;
294 }
295 }
Mark Brown8a892d62012-01-25 21:05:48 +0000296 map->cache_bypass = 0;
Mark Brown22f0d902012-01-21 12:01:14 +0000297
Mark Brownac8d91c2012-02-23 19:31:04 +0000298 ret = map->cache_ops->sync(map, 0, map->max_register);
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100299
Mark Brown6ff73732012-02-23 22:05:59 +0000300 if (ret == 0)
301 map->cache_dirty = false;
302
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100303out:
304 trace_regcache_sync(map->dev, name, "stop");
Dimitris Papastamosbeb1a102011-09-29 14:36:26 +0100305 /* Restore the bypass state */
306 map->cache_bypass = bypass;
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600307 map->unlock(map);
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100308
309 return ret;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100310}
311EXPORT_SYMBOL_GPL(regcache_sync);
312
Mark Brown92afb282011-09-19 18:22:14 +0100313/**
Mark Brown4d4cfd12012-02-23 20:53:37 +0000314 * regcache_sync_region: Sync part of the register cache with the hardware.
315 *
316 * @map: map to sync.
317 * @min: first register to sync
318 * @max: last register to sync
319 *
320 * Write all non-default register values in the specified region to
321 * the hardware.
322 *
323 * Return a negative value on failure, 0 on success.
324 */
325int regcache_sync_region(struct regmap *map, unsigned int min,
326 unsigned int max)
327{
328 int ret = 0;
329 const char *name;
330 unsigned int bypass;
331
332 BUG_ON(!map->cache_ops || !map->cache_ops->sync);
333
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600334 map->lock(map);
Mark Brown4d4cfd12012-02-23 20:53:37 +0000335
336 /* Remember the initial bypass state */
337 bypass = map->cache_bypass;
338
339 name = map->cache_ops->name;
340 dev_dbg(map->dev, "Syncing %s cache from %d-%d\n", name, min, max);
341
342 trace_regcache_sync(map->dev, name, "start region");
343
344 if (!map->cache_dirty)
345 goto out;
346
347 ret = map->cache_ops->sync(map, min, max);
348
349out:
350 trace_regcache_sync(map->dev, name, "stop region");
351 /* Restore the bypass state */
352 map->cache_bypass = bypass;
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600353 map->unlock(map);
Mark Brown4d4cfd12012-02-23 20:53:37 +0000354
355 return ret;
356}
Mark Browne466de02012-04-03 13:08:53 +0100357EXPORT_SYMBOL_GPL(regcache_sync_region);
Mark Brown4d4cfd12012-02-23 20:53:37 +0000358
359/**
Mark Brown92afb282011-09-19 18:22:14 +0100360 * regcache_cache_only: Put a register map into cache only mode
361 *
362 * @map: map to configure
363 * @cache_only: flag if changes should be written to the hardware
364 *
365 * When a register map is marked as cache only writes to the register
366 * map API will only update the register cache, they will not cause
367 * any hardware changes. This is useful for allowing portions of
368 * drivers to act as though the device were functioning as normal when
369 * it is disabled for power saving reasons.
370 */
371void regcache_cache_only(struct regmap *map, bool enable)
372{
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600373 map->lock(map);
Dimitris Papastamosac77a762011-09-29 14:36:28 +0100374 WARN_ON(map->cache_bypass && enable);
Mark Brown92afb282011-09-19 18:22:14 +0100375 map->cache_only = enable;
Mark Brown5d5b7d42012-02-23 22:02:57 +0000376 trace_regmap_cache_only(map->dev, enable);
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600377 map->unlock(map);
Mark Brown92afb282011-09-19 18:22:14 +0100378}
379EXPORT_SYMBOL_GPL(regcache_cache_only);
380
Dimitris Papastamos6eb0f5e2011-09-29 14:36:27 +0100381/**
Mark Brown8ae0d7e2011-10-26 10:34:22 +0200382 * regcache_mark_dirty: Mark the register cache as dirty
383 *
384 * @map: map to mark
385 *
386 * Mark the register cache as dirty, for example due to the device
387 * having been powered down for suspend. If the cache is not marked
388 * as dirty then the cache sync will be suppressed.
389 */
390void regcache_mark_dirty(struct regmap *map)
391{
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600392 map->lock(map);
Mark Brown8ae0d7e2011-10-26 10:34:22 +0200393 map->cache_dirty = true;
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600394 map->unlock(map);
Mark Brown8ae0d7e2011-10-26 10:34:22 +0200395}
396EXPORT_SYMBOL_GPL(regcache_mark_dirty);
397
398/**
Dimitris Papastamos6eb0f5e2011-09-29 14:36:27 +0100399 * regcache_cache_bypass: Put a register map into cache bypass mode
400 *
401 * @map: map to configure
Dimitris Papastamos0eef6b02011-10-03 06:54:16 +0100402 * @cache_bypass: flag if changes should not be written to the hardware
Dimitris Papastamos6eb0f5e2011-09-29 14:36:27 +0100403 *
404 * When a register map is marked with the cache bypass option, writes
405 * to the register map API will only update the hardware and not the
406 * the cache directly. This is useful when syncing the cache back to
407 * the hardware.
408 */
409void regcache_cache_bypass(struct regmap *map, bool enable)
410{
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600411 map->lock(map);
Dimitris Papastamosac77a762011-09-29 14:36:28 +0100412 WARN_ON(map->cache_only && enable);
Dimitris Papastamos6eb0f5e2011-09-29 14:36:27 +0100413 map->cache_bypass = enable;
Mark Brown5d5b7d42012-02-23 22:02:57 +0000414 trace_regmap_cache_bypass(map->dev, enable);
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600415 map->unlock(map);
Dimitris Papastamos6eb0f5e2011-09-29 14:36:27 +0100416}
417EXPORT_SYMBOL_GPL(regcache_cache_bypass);
418
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100419bool regcache_set_val(void *base, unsigned int idx,
420 unsigned int val, unsigned int word_size)
421{
422 switch (word_size) {
423 case 1: {
424 u8 *cache = base;
425 if (cache[idx] == val)
426 return true;
427 cache[idx] = val;
428 break;
429 }
430 case 2: {
431 u16 *cache = base;
432 if (cache[idx] == val)
433 return true;
434 cache[idx] = val;
435 break;
436 }
Mark Brown7d5e5252012-02-17 15:58:25 -0800437 case 4: {
438 u32 *cache = base;
439 if (cache[idx] == val)
440 return true;
441 cache[idx] = val;
442 break;
443 }
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100444 default:
445 BUG();
446 }
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100447 return false;
448}
449
450unsigned int regcache_get_val(const void *base, unsigned int idx,
451 unsigned int word_size)
452{
453 if (!base)
454 return -EINVAL;
455
456 switch (word_size) {
457 case 1: {
458 const u8 *cache = base;
459 return cache[idx];
460 }
461 case 2: {
462 const u16 *cache = base;
463 return cache[idx];
464 }
Mark Brown7d5e5252012-02-17 15:58:25 -0800465 case 4: {
466 const u32 *cache = base;
467 return cache[idx];
468 }
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100469 default:
470 BUG();
471 }
472 /* unreachable */
473 return -1;
474}
475
Mark Brownf094fea2011-10-04 22:05:47 +0100476static int regcache_default_cmp(const void *a, const void *b)
Dimitris Papastamosc08604b2011-10-03 10:50:14 +0100477{
478 const struct reg_default *_a = a;
479 const struct reg_default *_b = b;
480
481 return _a->reg - _b->reg;
482}
483
Mark Brownf094fea2011-10-04 22:05:47 +0100484int regcache_lookup_reg(struct regmap *map, unsigned int reg)
485{
486 struct reg_default key;
487 struct reg_default *r;
488
489 key.reg = reg;
490 key.def = 0;
491
492 r = bsearch(&key, map->reg_defaults, map->num_reg_defaults,
493 sizeof(struct reg_default), regcache_default_cmp);
494
495 if (r)
496 return r - map->reg_defaults;
497 else
Mark Brown6e6ace02011-10-09 13:23:31 +0100498 return -ENOENT;
Mark Brownf094fea2011-10-04 22:05:47 +0100499}