blob: e69ff3e4742c262eceda875b10cc384af8149496 [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,
Mark Brown2ac902c2012-12-19 14:51:55 +000025 &regcache_flat_ops,
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010026};
27
28static int regcache_hw_init(struct regmap *map)
29{
30 int i, j;
31 int ret;
32 int count;
33 unsigned int val;
34 void *tmp_buf;
35
36 if (!map->num_reg_defaults_raw)
37 return -EINVAL;
38
39 if (!map->reg_defaults_raw) {
Laxman Dewangandf00c792012-02-17 18:57:26 +053040 u32 cache_bypass = map->cache_bypass;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010041 dev_warn(map->dev, "No cache defaults, reading back from HW\n");
Laxman Dewangandf00c792012-02-17 18:57:26 +053042
43 /* Bypass the cache access till data read from HW*/
44 map->cache_bypass = 1;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010045 tmp_buf = kmalloc(map->cache_size_raw, GFP_KERNEL);
46 if (!tmp_buf)
47 return -EINVAL;
48 ret = regmap_bulk_read(map, 0, tmp_buf,
49 map->num_reg_defaults_raw);
Laxman Dewangandf00c792012-02-17 18:57:26 +053050 map->cache_bypass = cache_bypass;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010051 if (ret < 0) {
52 kfree(tmp_buf);
53 return ret;
54 }
55 map->reg_defaults_raw = tmp_buf;
56 map->cache_free = 1;
57 }
58
59 /* calculate the size of reg_defaults */
60 for (count = 0, i = 0; i < map->num_reg_defaults_raw; i++) {
61 val = regcache_get_val(map->reg_defaults_raw,
62 i, map->cache_word_size);
Stephen Warrenf01ee602012-04-09 13:40:24 -060063 if (regmap_volatile(map, i * map->reg_stride))
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010064 continue;
65 count++;
66 }
67
68 map->reg_defaults = kmalloc(count * sizeof(struct reg_default),
69 GFP_KERNEL);
Lars-Peter Clausen021cd612011-11-14 10:40:16 +010070 if (!map->reg_defaults) {
71 ret = -ENOMEM;
72 goto err_free;
73 }
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010074
75 /* fill the reg_defaults */
76 map->num_reg_defaults = count;
77 for (i = 0, j = 0; i < map->num_reg_defaults_raw; i++) {
78 val = regcache_get_val(map->reg_defaults_raw,
79 i, map->cache_word_size);
Stephen Warrenf01ee602012-04-09 13:40:24 -060080 if (regmap_volatile(map, i * map->reg_stride))
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010081 continue;
Stephen Warrenf01ee602012-04-09 13:40:24 -060082 map->reg_defaults[j].reg = i * map->reg_stride;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010083 map->reg_defaults[j].def = val;
84 j++;
85 }
86
87 return 0;
Lars-Peter Clausen021cd612011-11-14 10:40:16 +010088
89err_free:
90 if (map->cache_free)
91 kfree(map->reg_defaults_raw);
92
93 return ret;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010094}
95
Lars-Peter Clausene5e3b8a2011-11-16 16:28:16 +010096int regcache_init(struct regmap *map, const struct regmap_config *config)
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010097{
98 int ret;
99 int i;
100 void *tmp_buf;
101
Stephen Warrenf01ee602012-04-09 13:40:24 -0600102 for (i = 0; i < config->num_reg_defaults; i++)
103 if (config->reg_defaults[i].reg % map->reg_stride)
104 return -EINVAL;
105
Mark Browne7a6db32011-09-19 16:08:03 +0100106 if (map->cache_type == REGCACHE_NONE) {
107 map->cache_bypass = true;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100108 return 0;
Mark Browne7a6db32011-09-19 16:08:03 +0100109 }
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100110
111 for (i = 0; i < ARRAY_SIZE(cache_types); i++)
112 if (cache_types[i]->type == map->cache_type)
113 break;
114
115 if (i == ARRAY_SIZE(cache_types)) {
116 dev_err(map->dev, "Could not match compress type: %d\n",
117 map->cache_type);
118 return -EINVAL;
119 }
120
Lars-Peter Clausene5e3b8a2011-11-16 16:28:16 +0100121 map->num_reg_defaults = config->num_reg_defaults;
122 map->num_reg_defaults_raw = config->num_reg_defaults_raw;
123 map->reg_defaults_raw = config->reg_defaults_raw;
Lars-Peter Clausen064d4db2011-11-16 20:34:03 +0100124 map->cache_word_size = DIV_ROUND_UP(config->val_bits, 8);
125 map->cache_size_raw = map->cache_word_size * config->num_reg_defaults_raw;
Lars-Peter Clausene5e3b8a2011-11-16 16:28:16 +0100126
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100127 map->cache = NULL;
128 map->cache_ops = cache_types[i];
129
130 if (!map->cache_ops->read ||
131 !map->cache_ops->write ||
132 !map->cache_ops->name)
133 return -EINVAL;
134
135 /* We still need to ensure that the reg_defaults
136 * won't vanish from under us. We'll need to make
137 * a copy of it.
138 */
Lars-Peter Clausen720e4612011-11-16 16:28:17 +0100139 if (config->reg_defaults) {
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100140 if (!map->num_reg_defaults)
141 return -EINVAL;
Lars-Peter Clausen720e4612011-11-16 16:28:17 +0100142 tmp_buf = kmemdup(config->reg_defaults, map->num_reg_defaults *
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100143 sizeof(struct reg_default), GFP_KERNEL);
144 if (!tmp_buf)
145 return -ENOMEM;
146 map->reg_defaults = tmp_buf;
Mark Brown8528bdd2011-10-09 13:13:58 +0100147 } else if (map->num_reg_defaults_raw) {
Mark Brown5fcd2562011-09-29 15:24:54 +0100148 /* Some devices such as PMICs don't have cache defaults,
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100149 * we cope with this by reading back the HW registers and
150 * crafting the cache defaults by hand.
151 */
152 ret = regcache_hw_init(map);
153 if (ret < 0)
154 return ret;
155 }
156
157 if (!map->max_register)
158 map->max_register = map->num_reg_defaults_raw;
159
160 if (map->cache_ops->init) {
161 dev_dbg(map->dev, "Initializing %s cache\n",
162 map->cache_ops->name);
Lars-Peter Clausenbd061c72011-11-14 10:40:17 +0100163 ret = map->cache_ops->init(map);
164 if (ret)
165 goto err_free;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100166 }
167 return 0;
Lars-Peter Clausenbd061c72011-11-14 10:40:17 +0100168
169err_free:
170 kfree(map->reg_defaults);
171 if (map->cache_free)
172 kfree(map->reg_defaults_raw);
173
174 return ret;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100175}
176
177void regcache_exit(struct regmap *map)
178{
179 if (map->cache_type == REGCACHE_NONE)
180 return;
181
182 BUG_ON(!map->cache_ops);
183
184 kfree(map->reg_defaults);
185 if (map->cache_free)
186 kfree(map->reg_defaults_raw);
187
188 if (map->cache_ops->exit) {
189 dev_dbg(map->dev, "Destroying %s cache\n",
190 map->cache_ops->name);
191 map->cache_ops->exit(map);
192 }
193}
194
195/**
196 * regcache_read: Fetch the value of a given register from the cache.
197 *
198 * @map: map to configure.
199 * @reg: The register index.
200 * @value: The value to be returned.
201 *
202 * Return a negative value on failure, 0 on success.
203 */
204int regcache_read(struct regmap *map,
205 unsigned int reg, unsigned int *value)
206{
Mark Brownbc7ee552011-11-30 14:27:08 +0000207 int ret;
208
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100209 if (map->cache_type == REGCACHE_NONE)
210 return -ENOSYS;
211
212 BUG_ON(!map->cache_ops);
213
Mark Brownbc7ee552011-11-30 14:27:08 +0000214 if (!regmap_volatile(map, reg)) {
215 ret = map->cache_ops->read(map, reg, value);
216
217 if (ret == 0)
218 trace_regmap_reg_read_cache(map->dev, reg, *value);
219
220 return ret;
221 }
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100222
223 return -EINVAL;
224}
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100225
226/**
227 * regcache_write: Set the value of a given register in the cache.
228 *
229 * @map: map to configure.
230 * @reg: The register index.
231 * @value: The new register value.
232 *
233 * Return a negative value on failure, 0 on success.
234 */
235int regcache_write(struct regmap *map,
236 unsigned int reg, unsigned int value)
237{
238 if (map->cache_type == REGCACHE_NONE)
239 return 0;
240
241 BUG_ON(!map->cache_ops);
242
243 if (!regmap_writeable(map, reg))
244 return -EIO;
245
246 if (!regmap_volatile(map, reg))
247 return map->cache_ops->write(map, reg, value);
248
249 return 0;
250}
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100251
252/**
253 * regcache_sync: Sync the register cache with the hardware.
254 *
255 * @map: map to configure.
256 *
257 * Any registers that should not be synced should be marked as
258 * volatile. In general drivers can choose not to use the provided
259 * syncing functionality if they so require.
260 *
261 * Return a negative value on failure, 0 on success.
262 */
263int regcache_sync(struct regmap *map)
264{
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100265 int ret = 0;
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100266 unsigned int i;
Dimitris Papastamos59360082011-09-19 14:34:04 +0100267 const char *name;
Dimitris Papastamosbeb1a102011-09-29 14:36:26 +0100268 unsigned int bypass;
Dimitris Papastamos59360082011-09-19 14:34:04 +0100269
Mark Brownc3ec2322012-02-23 20:48:40 +0000270 BUG_ON(!map->cache_ops || !map->cache_ops->sync);
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100271
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600272 map->lock(map);
Dimitris Papastamosbeb1a102011-09-29 14:36:26 +0100273 /* Remember the initial bypass state */
274 bypass = map->cache_bypass;
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100275 dev_dbg(map->dev, "Syncing %s cache\n",
276 map->cache_ops->name);
277 name = map->cache_ops->name;
278 trace_regcache_sync(map->dev, name, "start");
Mark Brown22f0d902012-01-21 12:01:14 +0000279
Mark Brown8ae0d7e2011-10-26 10:34:22 +0200280 if (!map->cache_dirty)
281 goto out;
Mark Brownd9db7622012-01-25 21:06:33 +0000282
Mark Brown22f0d902012-01-21 12:01:14 +0000283 /* Apply any patch first */
Mark Brown8a892d62012-01-25 21:05:48 +0000284 map->cache_bypass = 1;
Mark Brown22f0d902012-01-21 12:01:14 +0000285 for (i = 0; i < map->patch_regs; i++) {
Stephen Warrenf01ee602012-04-09 13:40:24 -0600286 if (map->patch[i].reg % map->reg_stride) {
287 ret = -EINVAL;
288 goto out;
289 }
Mark Brown22f0d902012-01-21 12:01:14 +0000290 ret = _regmap_write(map, map->patch[i].reg, map->patch[i].def);
291 if (ret != 0) {
292 dev_err(map->dev, "Failed to write %x = %x: %d\n",
293 map->patch[i].reg, map->patch[i].def, ret);
294 goto out;
295 }
296 }
Mark Brown8a892d62012-01-25 21:05:48 +0000297 map->cache_bypass = 0;
Mark Brown22f0d902012-01-21 12:01:14 +0000298
Mark Brownac8d91c2012-02-23 19:31:04 +0000299 ret = map->cache_ops->sync(map, 0, map->max_register);
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100300
Mark Brown6ff73732012-02-23 22:05:59 +0000301 if (ret == 0)
302 map->cache_dirty = false;
303
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100304out:
305 trace_regcache_sync(map->dev, name, "stop");
Dimitris Papastamosbeb1a102011-09-29 14:36:26 +0100306 /* Restore the bypass state */
307 map->cache_bypass = bypass;
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600308 map->unlock(map);
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100309
310 return ret;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100311}
312EXPORT_SYMBOL_GPL(regcache_sync);
313
Mark Brown92afb282011-09-19 18:22:14 +0100314/**
Mark Brown4d4cfd12012-02-23 20:53:37 +0000315 * regcache_sync_region: Sync part of the register cache with the hardware.
316 *
317 * @map: map to sync.
318 * @min: first register to sync
319 * @max: last register to sync
320 *
321 * Write all non-default register values in the specified region to
322 * the hardware.
323 *
324 * Return a negative value on failure, 0 on success.
325 */
326int regcache_sync_region(struct regmap *map, unsigned int min,
327 unsigned int max)
328{
329 int ret = 0;
330 const char *name;
331 unsigned int bypass;
332
333 BUG_ON(!map->cache_ops || !map->cache_ops->sync);
334
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600335 map->lock(map);
Mark Brown4d4cfd12012-02-23 20:53:37 +0000336
337 /* Remember the initial bypass state */
338 bypass = map->cache_bypass;
339
340 name = map->cache_ops->name;
341 dev_dbg(map->dev, "Syncing %s cache from %d-%d\n", name, min, max);
342
343 trace_regcache_sync(map->dev, name, "start region");
344
345 if (!map->cache_dirty)
346 goto out;
347
348 ret = map->cache_ops->sync(map, min, max);
349
350out:
351 trace_regcache_sync(map->dev, name, "stop region");
352 /* Restore the bypass state */
353 map->cache_bypass = bypass;
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600354 map->unlock(map);
Mark Brown4d4cfd12012-02-23 20:53:37 +0000355
356 return ret;
357}
Mark Browne466de02012-04-03 13:08:53 +0100358EXPORT_SYMBOL_GPL(regcache_sync_region);
Mark Brown4d4cfd12012-02-23 20:53:37 +0000359
360/**
Mark Brown92afb282011-09-19 18:22:14 +0100361 * regcache_cache_only: Put a register map into cache only mode
362 *
363 * @map: map to configure
364 * @cache_only: flag if changes should be written to the hardware
365 *
366 * When a register map is marked as cache only writes to the register
367 * map API will only update the register cache, they will not cause
368 * any hardware changes. This is useful for allowing portions of
369 * drivers to act as though the device were functioning as normal when
370 * it is disabled for power saving reasons.
371 */
372void regcache_cache_only(struct regmap *map, bool enable)
373{
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600374 map->lock(map);
Dimitris Papastamosac77a762011-09-29 14:36:28 +0100375 WARN_ON(map->cache_bypass && enable);
Mark Brown92afb282011-09-19 18:22:14 +0100376 map->cache_only = enable;
Mark Brown5d5b7d42012-02-23 22:02:57 +0000377 trace_regmap_cache_only(map->dev, enable);
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600378 map->unlock(map);
Mark Brown92afb282011-09-19 18:22:14 +0100379}
380EXPORT_SYMBOL_GPL(regcache_cache_only);
381
Dimitris Papastamos6eb0f5e2011-09-29 14:36:27 +0100382/**
Mark Brown8ae0d7e2011-10-26 10:34:22 +0200383 * regcache_mark_dirty: Mark the register cache as dirty
384 *
385 * @map: map to mark
386 *
387 * Mark the register cache as dirty, for example due to the device
388 * having been powered down for suspend. If the cache is not marked
389 * as dirty then the cache sync will be suppressed.
390 */
391void regcache_mark_dirty(struct regmap *map)
392{
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600393 map->lock(map);
Mark Brown8ae0d7e2011-10-26 10:34:22 +0200394 map->cache_dirty = true;
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600395 map->unlock(map);
Mark Brown8ae0d7e2011-10-26 10:34:22 +0200396}
397EXPORT_SYMBOL_GPL(regcache_mark_dirty);
398
399/**
Dimitris Papastamos6eb0f5e2011-09-29 14:36:27 +0100400 * regcache_cache_bypass: Put a register map into cache bypass mode
401 *
402 * @map: map to configure
Dimitris Papastamos0eef6b02011-10-03 06:54:16 +0100403 * @cache_bypass: flag if changes should not be written to the hardware
Dimitris Papastamos6eb0f5e2011-09-29 14:36:27 +0100404 *
405 * When a register map is marked with the cache bypass option, writes
406 * to the register map API will only update the hardware and not the
407 * the cache directly. This is useful when syncing the cache back to
408 * the hardware.
409 */
410void regcache_cache_bypass(struct regmap *map, bool enable)
411{
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600412 map->lock(map);
Dimitris Papastamosac77a762011-09-29 14:36:28 +0100413 WARN_ON(map->cache_only && enable);
Dimitris Papastamos6eb0f5e2011-09-29 14:36:27 +0100414 map->cache_bypass = enable;
Mark Brown5d5b7d42012-02-23 22:02:57 +0000415 trace_regmap_cache_bypass(map->dev, enable);
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600416 map->unlock(map);
Dimitris Papastamos6eb0f5e2011-09-29 14:36:27 +0100417}
418EXPORT_SYMBOL_GPL(regcache_cache_bypass);
419
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100420bool regcache_set_val(void *base, unsigned int idx,
421 unsigned int val, unsigned int word_size)
422{
423 switch (word_size) {
424 case 1: {
425 u8 *cache = base;
426 if (cache[idx] == val)
427 return true;
428 cache[idx] = val;
429 break;
430 }
431 case 2: {
432 u16 *cache = base;
433 if (cache[idx] == val)
434 return true;
435 cache[idx] = val;
436 break;
437 }
Mark Brown7d5e5252012-02-17 15:58:25 -0800438 case 4: {
439 u32 *cache = base;
440 if (cache[idx] == val)
441 return true;
442 cache[idx] = val;
443 break;
444 }
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100445 default:
446 BUG();
447 }
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100448 return false;
449}
450
451unsigned int regcache_get_val(const void *base, unsigned int idx,
452 unsigned int word_size)
453{
454 if (!base)
455 return -EINVAL;
456
457 switch (word_size) {
458 case 1: {
459 const u8 *cache = base;
460 return cache[idx];
461 }
462 case 2: {
463 const u16 *cache = base;
464 return cache[idx];
465 }
Mark Brown7d5e5252012-02-17 15:58:25 -0800466 case 4: {
467 const u32 *cache = base;
468 return cache[idx];
469 }
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100470 default:
471 BUG();
472 }
473 /* unreachable */
474 return -1;
475}
476
Mark Brownf094fea2011-10-04 22:05:47 +0100477static int regcache_default_cmp(const void *a, const void *b)
Dimitris Papastamosc08604b2011-10-03 10:50:14 +0100478{
479 const struct reg_default *_a = a;
480 const struct reg_default *_b = b;
481
482 return _a->reg - _b->reg;
483}
484
Mark Brownf094fea2011-10-04 22:05:47 +0100485int regcache_lookup_reg(struct regmap *map, unsigned int reg)
486{
487 struct reg_default key;
488 struct reg_default *r;
489
490 key.reg = reg;
491 key.def = 0;
492
493 r = bsearch(&key, map->reg_defaults, map->num_reg_defaults,
494 sizeof(struct reg_default), regcache_default_cmp);
495
496 if (r)
497 return r - map->reg_defaults;
498 else
Mark Brown6e6ace02011-10-09 13:23:31 +0100499 return -ENOENT;
Mark Brownf094fea2011-10-04 22:05:47 +0100500}