blob: 0f4fb8bc37e5d91c05732bc53f8d55236d9f0ee9 [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;
Mark Browneb4cb762013-02-21 18:39:47 +000048 ret = regmap_raw_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++) {
Mark Brown879082c2013-02-21 18:03:13 +000061 val = regcache_get_val(map, map->reg_defaults_raw, i);
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++) {
Mark Brown879082c2013-02-21 18:03:13 +000077 val = regcache_get_val(map, map->reg_defaults_raw, i);
Stephen Warrenf01ee602012-04-09 13:40:24 -060078 if (regmap_volatile(map, i * map->reg_stride))
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010079 continue;
Stephen Warrenf01ee602012-04-09 13:40:24 -060080 map->reg_defaults[j].reg = i * map->reg_stride;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010081 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
Stephen Warrenf01ee602012-04-09 13:40:24 -0600100 for (i = 0; i < config->num_reg_defaults; i++)
101 if (config->reg_defaults[i].reg % map->reg_stride)
102 return -EINVAL;
103
Mark Browne7a6db32011-09-19 16:08:03 +0100104 if (map->cache_type == REGCACHE_NONE) {
105 map->cache_bypass = true;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100106 return 0;
Mark Browne7a6db32011-09-19 16:08:03 +0100107 }
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100108
109 for (i = 0; i < ARRAY_SIZE(cache_types); i++)
110 if (cache_types[i]->type == map->cache_type)
111 break;
112
113 if (i == ARRAY_SIZE(cache_types)) {
114 dev_err(map->dev, "Could not match compress type: %d\n",
115 map->cache_type);
116 return -EINVAL;
117 }
118
Lars-Peter Clausene5e3b8a2011-11-16 16:28:16 +0100119 map->num_reg_defaults = config->num_reg_defaults;
120 map->num_reg_defaults_raw = config->num_reg_defaults_raw;
121 map->reg_defaults_raw = config->reg_defaults_raw;
Lars-Peter Clausen064d4db2011-11-16 20:34:03 +0100122 map->cache_word_size = DIV_ROUND_UP(config->val_bits, 8);
123 map->cache_size_raw = map->cache_word_size * config->num_reg_defaults_raw;
Lars-Peter Clausene5e3b8a2011-11-16 16:28:16 +0100124
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100125 map->cache = NULL;
126 map->cache_ops = cache_types[i];
127
128 if (!map->cache_ops->read ||
129 !map->cache_ops->write ||
130 !map->cache_ops->name)
131 return -EINVAL;
132
133 /* We still need to ensure that the reg_defaults
134 * won't vanish from under us. We'll need to make
135 * a copy of it.
136 */
Lars-Peter Clausen720e4612011-11-16 16:28:17 +0100137 if (config->reg_defaults) {
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100138 if (!map->num_reg_defaults)
139 return -EINVAL;
Lars-Peter Clausen720e4612011-11-16 16:28:17 +0100140 tmp_buf = kmemdup(config->reg_defaults, map->num_reg_defaults *
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100141 sizeof(struct reg_default), GFP_KERNEL);
142 if (!tmp_buf)
143 return -ENOMEM;
144 map->reg_defaults = tmp_buf;
Mark Brown8528bdd2011-10-09 13:13:58 +0100145 } else if (map->num_reg_defaults_raw) {
Mark Brown5fcd2562011-09-29 15:24:54 +0100146 /* Some devices such as PMICs don't have cache defaults,
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100147 * we cope with this by reading back the HW registers and
148 * crafting the cache defaults by hand.
149 */
150 ret = regcache_hw_init(map);
151 if (ret < 0)
152 return ret;
153 }
154
155 if (!map->max_register)
156 map->max_register = map->num_reg_defaults_raw;
157
158 if (map->cache_ops->init) {
159 dev_dbg(map->dev, "Initializing %s cache\n",
160 map->cache_ops->name);
Lars-Peter Clausenbd061c72011-11-14 10:40:17 +0100161 ret = map->cache_ops->init(map);
162 if (ret)
163 goto err_free;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100164 }
165 return 0;
Lars-Peter Clausenbd061c72011-11-14 10:40:17 +0100166
167err_free:
168 kfree(map->reg_defaults);
169 if (map->cache_free)
170 kfree(map->reg_defaults_raw);
171
172 return ret;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100173}
174
175void regcache_exit(struct regmap *map)
176{
177 if (map->cache_type == REGCACHE_NONE)
178 return;
179
180 BUG_ON(!map->cache_ops);
181
182 kfree(map->reg_defaults);
183 if (map->cache_free)
184 kfree(map->reg_defaults_raw);
185
186 if (map->cache_ops->exit) {
187 dev_dbg(map->dev, "Destroying %s cache\n",
188 map->cache_ops->name);
189 map->cache_ops->exit(map);
190 }
191}
192
193/**
194 * regcache_read: Fetch the value of a given register from the cache.
195 *
196 * @map: map to configure.
197 * @reg: The register index.
198 * @value: The value to be returned.
199 *
200 * Return a negative value on failure, 0 on success.
201 */
202int regcache_read(struct regmap *map,
203 unsigned int reg, unsigned int *value)
204{
Mark Brownbc7ee552011-11-30 14:27:08 +0000205 int ret;
206
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100207 if (map->cache_type == REGCACHE_NONE)
208 return -ENOSYS;
209
210 BUG_ON(!map->cache_ops);
211
Mark Brownbc7ee552011-11-30 14:27:08 +0000212 if (!regmap_volatile(map, reg)) {
213 ret = map->cache_ops->read(map, reg, value);
214
215 if (ret == 0)
216 trace_regmap_reg_read_cache(map->dev, reg, *value);
217
218 return ret;
219 }
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100220
221 return -EINVAL;
222}
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100223
224/**
225 * regcache_write: Set the value of a given register in the cache.
226 *
227 * @map: map to configure.
228 * @reg: The register index.
229 * @value: The new register value.
230 *
231 * Return a negative value on failure, 0 on success.
232 */
233int regcache_write(struct regmap *map,
234 unsigned int reg, unsigned int value)
235{
236 if (map->cache_type == REGCACHE_NONE)
237 return 0;
238
239 BUG_ON(!map->cache_ops);
240
241 if (!regmap_writeable(map, reg))
242 return -EIO;
243
244 if (!regmap_volatile(map, reg))
245 return map->cache_ops->write(map, reg, value);
246
247 return 0;
248}
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100249
250/**
251 * regcache_sync: Sync the register cache with the hardware.
252 *
253 * @map: map to configure.
254 *
255 * Any registers that should not be synced should be marked as
256 * volatile. In general drivers can choose not to use the provided
257 * syncing functionality if they so require.
258 *
259 * Return a negative value on failure, 0 on success.
260 */
261int regcache_sync(struct regmap *map)
262{
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100263 int ret = 0;
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100264 unsigned int i;
Dimitris Papastamos59360082011-09-19 14:34:04 +0100265 const char *name;
Dimitris Papastamosbeb1a102011-09-29 14:36:26 +0100266 unsigned int bypass;
Dimitris Papastamos59360082011-09-19 14:34:04 +0100267
Mark Brownc3ec2322012-02-23 20:48:40 +0000268 BUG_ON(!map->cache_ops || !map->cache_ops->sync);
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100269
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600270 map->lock(map);
Dimitris Papastamosbeb1a102011-09-29 14:36:26 +0100271 /* Remember the initial bypass state */
272 bypass = map->cache_bypass;
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100273 dev_dbg(map->dev, "Syncing %s cache\n",
274 map->cache_ops->name);
275 name = map->cache_ops->name;
276 trace_regcache_sync(map->dev, name, "start");
Mark Brown22f0d902012-01-21 12:01:14 +0000277
Mark Brown8ae0d7e2011-10-26 10:34:22 +0200278 if (!map->cache_dirty)
279 goto out;
Mark Brownd9db7622012-01-25 21:06:33 +0000280
Mark Brown22f0d902012-01-21 12:01:14 +0000281 /* Apply any patch first */
Mark Brown8a892d62012-01-25 21:05:48 +0000282 map->cache_bypass = 1;
Mark Brown22f0d902012-01-21 12:01:14 +0000283 for (i = 0; i < map->patch_regs; i++) {
Stephen Warrenf01ee602012-04-09 13:40:24 -0600284 if (map->patch[i].reg % map->reg_stride) {
285 ret = -EINVAL;
286 goto out;
287 }
Mark Brown22f0d902012-01-21 12:01:14 +0000288 ret = _regmap_write(map, map->patch[i].reg, map->patch[i].def);
289 if (ret != 0) {
290 dev_err(map->dev, "Failed to write %x = %x: %d\n",
291 map->patch[i].reg, map->patch[i].def, ret);
292 goto out;
293 }
294 }
Mark Brown8a892d62012-01-25 21:05:48 +0000295 map->cache_bypass = 0;
Mark Brown22f0d902012-01-21 12:01:14 +0000296
Mark Brownac8d91c2012-02-23 19:31:04 +0000297 ret = map->cache_ops->sync(map, 0, map->max_register);
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100298
Mark Brown6ff73732012-02-23 22:05:59 +0000299 if (ret == 0)
300 map->cache_dirty = false;
301
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100302out:
303 trace_regcache_sync(map->dev, name, "stop");
Dimitris Papastamosbeb1a102011-09-29 14:36:26 +0100304 /* Restore the bypass state */
305 map->cache_bypass = bypass;
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600306 map->unlock(map);
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100307
308 return ret;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100309}
310EXPORT_SYMBOL_GPL(regcache_sync);
311
Mark Brown92afb282011-09-19 18:22:14 +0100312/**
Mark Brown4d4cfd12012-02-23 20:53:37 +0000313 * regcache_sync_region: Sync part of the register cache with the hardware.
314 *
315 * @map: map to sync.
316 * @min: first register to sync
317 * @max: last register to sync
318 *
319 * Write all non-default register values in the specified region to
320 * the hardware.
321 *
322 * Return a negative value on failure, 0 on success.
323 */
324int regcache_sync_region(struct regmap *map, unsigned int min,
325 unsigned int max)
326{
327 int ret = 0;
328 const char *name;
329 unsigned int bypass;
330
331 BUG_ON(!map->cache_ops || !map->cache_ops->sync);
332
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600333 map->lock(map);
Mark Brown4d4cfd12012-02-23 20:53:37 +0000334
335 /* Remember the initial bypass state */
336 bypass = map->cache_bypass;
337
338 name = map->cache_ops->name;
339 dev_dbg(map->dev, "Syncing %s cache from %d-%d\n", name, min, max);
340
341 trace_regcache_sync(map->dev, name, "start region");
342
343 if (!map->cache_dirty)
344 goto out;
345
346 ret = map->cache_ops->sync(map, min, max);
347
348out:
349 trace_regcache_sync(map->dev, name, "stop region");
350 /* Restore the bypass state */
351 map->cache_bypass = bypass;
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600352 map->unlock(map);
Mark Brown4d4cfd12012-02-23 20:53:37 +0000353
354 return ret;
355}
Mark Browne466de02012-04-03 13:08:53 +0100356EXPORT_SYMBOL_GPL(regcache_sync_region);
Mark Brown4d4cfd12012-02-23 20:53:37 +0000357
358/**
Mark Brown92afb282011-09-19 18:22:14 +0100359 * regcache_cache_only: Put a register map into cache only mode
360 *
361 * @map: map to configure
362 * @cache_only: flag if changes should be written to the hardware
363 *
364 * When a register map is marked as cache only writes to the register
365 * map API will only update the register cache, they will not cause
366 * any hardware changes. This is useful for allowing portions of
367 * drivers to act as though the device were functioning as normal when
368 * it is disabled for power saving reasons.
369 */
370void regcache_cache_only(struct regmap *map, bool enable)
371{
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600372 map->lock(map);
Dimitris Papastamosac77a762011-09-29 14:36:28 +0100373 WARN_ON(map->cache_bypass && enable);
Mark Brown92afb282011-09-19 18:22:14 +0100374 map->cache_only = enable;
Mark Brown5d5b7d42012-02-23 22:02:57 +0000375 trace_regmap_cache_only(map->dev, enable);
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600376 map->unlock(map);
Mark Brown92afb282011-09-19 18:22:14 +0100377}
378EXPORT_SYMBOL_GPL(regcache_cache_only);
379
Dimitris Papastamos6eb0f5e2011-09-29 14:36:27 +0100380/**
Mark Brown8ae0d7e2011-10-26 10:34:22 +0200381 * regcache_mark_dirty: Mark the register cache as dirty
382 *
383 * @map: map to mark
384 *
385 * Mark the register cache as dirty, for example due to the device
386 * having been powered down for suspend. If the cache is not marked
387 * as dirty then the cache sync will be suppressed.
388 */
389void regcache_mark_dirty(struct regmap *map)
390{
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600391 map->lock(map);
Mark Brown8ae0d7e2011-10-26 10:34:22 +0200392 map->cache_dirty = true;
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600393 map->unlock(map);
Mark Brown8ae0d7e2011-10-26 10:34:22 +0200394}
395EXPORT_SYMBOL_GPL(regcache_mark_dirty);
396
397/**
Dimitris Papastamos6eb0f5e2011-09-29 14:36:27 +0100398 * regcache_cache_bypass: Put a register map into cache bypass mode
399 *
400 * @map: map to configure
Dimitris Papastamos0eef6b02011-10-03 06:54:16 +0100401 * @cache_bypass: flag if changes should not be written to the hardware
Dimitris Papastamos6eb0f5e2011-09-29 14:36:27 +0100402 *
403 * When a register map is marked with the cache bypass option, writes
404 * to the register map API will only update the hardware and not the
405 * the cache directly. This is useful when syncing the cache back to
406 * the hardware.
407 */
408void regcache_cache_bypass(struct regmap *map, bool enable)
409{
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600410 map->lock(map);
Dimitris Papastamosac77a762011-09-29 14:36:28 +0100411 WARN_ON(map->cache_only && enable);
Dimitris Papastamos6eb0f5e2011-09-29 14:36:27 +0100412 map->cache_bypass = enable;
Mark Brown5d5b7d42012-02-23 22:02:57 +0000413 trace_regmap_cache_bypass(map->dev, enable);
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600414 map->unlock(map);
Dimitris Papastamos6eb0f5e2011-09-29 14:36:27 +0100415}
416EXPORT_SYMBOL_GPL(regcache_cache_bypass);
417
Mark Brown879082c2013-02-21 18:03:13 +0000418bool regcache_set_val(struct regmap *map, void *base, unsigned int idx,
419 unsigned int val)
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100420{
Mark Brown325acab2013-02-21 18:07:01 +0000421 if (regcache_get_val(map, base, idx) == val)
422 return true;
423
Mark Browneb4cb762013-02-21 18:39:47 +0000424 /* Use device native format if possible */
425 if (map->format.format_val) {
426 map->format.format_val(base + (map->cache_word_size * idx),
427 val, 0);
428 return false;
429 }
430
Mark Brown879082c2013-02-21 18:03:13 +0000431 switch (map->cache_word_size) {
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100432 case 1: {
433 u8 *cache = base;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100434 cache[idx] = val;
435 break;
436 }
437 case 2: {
438 u16 *cache = base;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100439 cache[idx] = val;
440 break;
441 }
Mark Brown7d5e5252012-02-17 15:58:25 -0800442 case 4: {
443 u32 *cache = base;
Mark Brown7d5e5252012-02-17 15:58:25 -0800444 cache[idx] = val;
445 break;
446 }
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100447 default:
448 BUG();
449 }
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100450 return false;
451}
452
Mark Brown879082c2013-02-21 18:03:13 +0000453unsigned int regcache_get_val(struct regmap *map, const void *base,
454 unsigned int idx)
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100455{
456 if (!base)
457 return -EINVAL;
458
Mark Browneb4cb762013-02-21 18:39:47 +0000459 /* Use device native format if possible */
460 if (map->format.parse_val)
461 return map->format.parse_val(base +
462 (map->cache_word_size * idx));
463
Mark Brown879082c2013-02-21 18:03:13 +0000464 switch (map->cache_word_size) {
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100465 case 1: {
466 const u8 *cache = base;
467 return cache[idx];
468 }
469 case 2: {
470 const u16 *cache = base;
471 return cache[idx];
472 }
Mark Brown7d5e5252012-02-17 15:58:25 -0800473 case 4: {
474 const u32 *cache = base;
475 return cache[idx];
476 }
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100477 default:
478 BUG();
479 }
480 /* unreachable */
481 return -1;
482}
483
Mark Brownf094fea2011-10-04 22:05:47 +0100484static int regcache_default_cmp(const void *a, const void *b)
Dimitris Papastamosc08604b2011-10-03 10:50:14 +0100485{
486 const struct reg_default *_a = a;
487 const struct reg_default *_b = b;
488
489 return _a->reg - _b->reg;
490}
491
Mark Brownf094fea2011-10-04 22:05:47 +0100492int regcache_lookup_reg(struct regmap *map, unsigned int reg)
493{
494 struct reg_default key;
495 struct reg_default *r;
496
497 key.reg = reg;
498 key.def = 0;
499
500 r = bsearch(&key, map->reg_defaults, map->num_reg_defaults,
501 sizeof(struct reg_default), regcache_default_cmp);
502
503 if (r)
504 return r - map->reg_defaults;
505 else
Mark Brown6e6ace02011-10-09 13:23:31 +0100506 return -ENOENT;
Mark Brownf094fea2011-10-04 22:05:47 +0100507}