blob: 6ab9f0384d82c3fa82f6c575d45dac31d4b305ca [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 Papastamos195af652011-09-19 14:34:01 +010022 &regcache_indexed_ops,
Dimitris Papastamos28644c82011-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) {
39 dev_warn(map->dev, "No cache defaults, reading back from HW\n");
40 tmp_buf = kmalloc(map->cache_size_raw, GFP_KERNEL);
41 if (!tmp_buf)
42 return -EINVAL;
43 ret = regmap_bulk_read(map, 0, tmp_buf,
44 map->num_reg_defaults_raw);
45 if (ret < 0) {
46 kfree(tmp_buf);
47 return ret;
48 }
49 map->reg_defaults_raw = tmp_buf;
50 map->cache_free = 1;
51 }
52
53 /* calculate the size of reg_defaults */
54 for (count = 0, i = 0; i < map->num_reg_defaults_raw; i++) {
55 val = regcache_get_val(map->reg_defaults_raw,
56 i, map->cache_word_size);
57 if (!val)
58 continue;
59 count++;
60 }
61
62 map->reg_defaults = kmalloc(count * sizeof(struct reg_default),
63 GFP_KERNEL);
64 if (!map->reg_defaults)
65 return -ENOMEM;
66
67 /* fill the reg_defaults */
68 map->num_reg_defaults = count;
69 for (i = 0, j = 0; i < map->num_reg_defaults_raw; i++) {
70 val = regcache_get_val(map->reg_defaults_raw,
71 i, map->cache_word_size);
72 if (!val)
73 continue;
74 map->reg_defaults[j].reg = i;
75 map->reg_defaults[j].def = val;
76 j++;
77 }
78
79 return 0;
80}
81
82int regcache_init(struct regmap *map)
83{
84 int ret;
85 int i;
86 void *tmp_buf;
87
Mark Browne7a6db32011-09-19 16:08:03 +010088 if (map->cache_type == REGCACHE_NONE) {
89 map->cache_bypass = true;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010090 return 0;
Mark Browne7a6db32011-09-19 16:08:03 +010091 }
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010092
93 for (i = 0; i < ARRAY_SIZE(cache_types); i++)
94 if (cache_types[i]->type == map->cache_type)
95 break;
96
97 if (i == ARRAY_SIZE(cache_types)) {
98 dev_err(map->dev, "Could not match compress type: %d\n",
99 map->cache_type);
100 return -EINVAL;
101 }
102
103 map->cache = NULL;
104 map->cache_ops = cache_types[i];
105
106 if (!map->cache_ops->read ||
107 !map->cache_ops->write ||
108 !map->cache_ops->name)
109 return -EINVAL;
110
111 /* We still need to ensure that the reg_defaults
112 * won't vanish from under us. We'll need to make
113 * a copy of it.
114 */
115 if (map->reg_defaults) {
116 if (!map->num_reg_defaults)
117 return -EINVAL;
118 tmp_buf = kmemdup(map->reg_defaults, map->num_reg_defaults *
119 sizeof(struct reg_default), GFP_KERNEL);
120 if (!tmp_buf)
121 return -ENOMEM;
122 map->reg_defaults = tmp_buf;
Mark Brown8528bdd2011-10-09 13:13:58 +0100123 } else if (map->num_reg_defaults_raw) {
Mark Brown5fcd2562011-09-29 15:24:54 +0100124 /* Some devices such as PMICs don't have cache defaults,
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100125 * we cope with this by reading back the HW registers and
126 * crafting the cache defaults by hand.
127 */
128 ret = regcache_hw_init(map);
129 if (ret < 0)
130 return ret;
131 }
132
133 if (!map->max_register)
134 map->max_register = map->num_reg_defaults_raw;
135
136 if (map->cache_ops->init) {
137 dev_dbg(map->dev, "Initializing %s cache\n",
138 map->cache_ops->name);
139 return map->cache_ops->init(map);
140 }
141 return 0;
142}
143
144void regcache_exit(struct regmap *map)
145{
146 if (map->cache_type == REGCACHE_NONE)
147 return;
148
149 BUG_ON(!map->cache_ops);
150
151 kfree(map->reg_defaults);
152 if (map->cache_free)
153 kfree(map->reg_defaults_raw);
154
155 if (map->cache_ops->exit) {
156 dev_dbg(map->dev, "Destroying %s cache\n",
157 map->cache_ops->name);
158 map->cache_ops->exit(map);
159 }
160}
161
162/**
163 * regcache_read: Fetch the value of a given register from the cache.
164 *
165 * @map: map to configure.
166 * @reg: The register index.
167 * @value: The value to be returned.
168 *
169 * Return a negative value on failure, 0 on success.
170 */
171int regcache_read(struct regmap *map,
172 unsigned int reg, unsigned int *value)
173{
174 if (map->cache_type == REGCACHE_NONE)
175 return -ENOSYS;
176
177 BUG_ON(!map->cache_ops);
178
179 if (!regmap_readable(map, reg))
180 return -EIO;
181
182 if (!regmap_volatile(map, reg))
183 return map->cache_ops->read(map, reg, value);
184
185 return -EINVAL;
186}
187EXPORT_SYMBOL_GPL(regcache_read);
188
189/**
190 * regcache_write: Set the value of a given register in the cache.
191 *
192 * @map: map to configure.
193 * @reg: The register index.
194 * @value: The new register value.
195 *
196 * Return a negative value on failure, 0 on success.
197 */
198int regcache_write(struct regmap *map,
199 unsigned int reg, unsigned int value)
200{
201 if (map->cache_type == REGCACHE_NONE)
202 return 0;
203
204 BUG_ON(!map->cache_ops);
205
206 if (!regmap_writeable(map, reg))
207 return -EIO;
208
209 if (!regmap_volatile(map, reg))
210 return map->cache_ops->write(map, reg, value);
211
212 return 0;
213}
214EXPORT_SYMBOL_GPL(regcache_write);
215
216/**
217 * regcache_sync: Sync the register cache with the hardware.
218 *
219 * @map: map to configure.
220 *
221 * Any registers that should not be synced should be marked as
222 * volatile. In general drivers can choose not to use the provided
223 * syncing functionality if they so require.
224 *
225 * Return a negative value on failure, 0 on success.
226 */
227int regcache_sync(struct regmap *map)
228{
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100229 int ret = 0;
230 unsigned int val;
231 unsigned int i;
Dimitris Papastamos59360082011-09-19 14:34:04 +0100232 const char *name;
Dimitris Papastamosbeb1a102011-09-29 14:36:26 +0100233 unsigned int bypass;
Dimitris Papastamos59360082011-09-19 14:34:04 +0100234
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100235 BUG_ON(!map->cache_ops);
236
Dimitris Papastamos13753a92011-09-29 14:36:25 +0100237 mutex_lock(&map->lock);
Dimitris Papastamosbeb1a102011-09-29 14:36:26 +0100238 /* Remember the initial bypass state */
239 bypass = map->cache_bypass;
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100240 dev_dbg(map->dev, "Syncing %s cache\n",
241 map->cache_ops->name);
242 name = map->cache_ops->name;
243 trace_regcache_sync(map->dev, name, "start");
Mark Brown8ae0d7e2011-10-26 10:34:22 +0200244 if (!map->cache_dirty)
245 goto out;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100246 if (map->cache_ops->sync) {
Dimitris Papastamos59360082011-09-19 14:34:04 +0100247 ret = map->cache_ops->sync(map);
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100248 } else {
249 for (i = 0; i < map->num_reg_defaults; i++) {
250 ret = regcache_read(map, i, &val);
251 if (ret < 0)
252 goto out;
Dimitris Papastamosec8a3652011-09-28 11:43:42 +0100253 map->cache_bypass = 1;
Dimitris Papastamos13753a92011-09-29 14:36:25 +0100254 ret = _regmap_write(map, i, val);
Dimitris Papastamosec8a3652011-09-28 11:43:42 +0100255 map->cache_bypass = 0;
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100256 if (ret < 0)
257 goto out;
258 dev_dbg(map->dev, "Synced register %#x, value %#x\n",
259 map->reg_defaults[i].reg,
260 map->reg_defaults[i].def);
261 }
262
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100263 }
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100264out:
265 trace_regcache_sync(map->dev, name, "stop");
Dimitris Papastamosbeb1a102011-09-29 14:36:26 +0100266 /* Restore the bypass state */
267 map->cache_bypass = bypass;
Dimitris Papastamos13753a92011-09-29 14:36:25 +0100268 mutex_unlock(&map->lock);
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100269
270 return ret;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100271}
272EXPORT_SYMBOL_GPL(regcache_sync);
273
Mark Brown92afb282011-09-19 18:22:14 +0100274/**
275 * regcache_cache_only: Put a register map into cache only mode
276 *
277 * @map: map to configure
278 * @cache_only: flag if changes should be written to the hardware
279 *
280 * When a register map is marked as cache only writes to the register
281 * map API will only update the register cache, they will not cause
282 * any hardware changes. This is useful for allowing portions of
283 * drivers to act as though the device were functioning as normal when
284 * it is disabled for power saving reasons.
285 */
286void regcache_cache_only(struct regmap *map, bool enable)
287{
Mark Brown2cd148f2011-09-29 10:40:55 +0100288 mutex_lock(&map->lock);
Dimitris Papastamosac77a762011-09-29 14:36:28 +0100289 WARN_ON(map->cache_bypass && enable);
Mark Brown92afb282011-09-19 18:22:14 +0100290 map->cache_only = enable;
Mark Brown2cd148f2011-09-29 10:40:55 +0100291 mutex_unlock(&map->lock);
Mark Brown92afb282011-09-19 18:22:14 +0100292}
293EXPORT_SYMBOL_GPL(regcache_cache_only);
294
Dimitris Papastamos6eb0f5e2011-09-29 14:36:27 +0100295/**
Mark Brown8ae0d7e2011-10-26 10:34:22 +0200296 * regcache_mark_dirty: Mark the register cache as dirty
297 *
298 * @map: map to mark
299 *
300 * Mark the register cache as dirty, for example due to the device
301 * having been powered down for suspend. If the cache is not marked
302 * as dirty then the cache sync will be suppressed.
303 */
304void regcache_mark_dirty(struct regmap *map)
305{
306 mutex_lock(&map->lock);
307 map->cache_dirty = true;
308 mutex_unlock(&map->lock);
309}
310EXPORT_SYMBOL_GPL(regcache_mark_dirty);
311
312/**
Dimitris Papastamos6eb0f5e2011-09-29 14:36:27 +0100313 * regcache_cache_bypass: Put a register map into cache bypass mode
314 *
315 * @map: map to configure
Dimitris Papastamos0eef6b02011-10-03 06:54:16 +0100316 * @cache_bypass: flag if changes should not be written to the hardware
Dimitris Papastamos6eb0f5e2011-09-29 14:36:27 +0100317 *
318 * When a register map is marked with the cache bypass option, writes
319 * to the register map API will only update the hardware and not the
320 * the cache directly. This is useful when syncing the cache back to
321 * the hardware.
322 */
323void regcache_cache_bypass(struct regmap *map, bool enable)
324{
325 mutex_lock(&map->lock);
Dimitris Papastamosac77a762011-09-29 14:36:28 +0100326 WARN_ON(map->cache_only && enable);
Dimitris Papastamos6eb0f5e2011-09-29 14:36:27 +0100327 map->cache_bypass = enable;
328 mutex_unlock(&map->lock);
329}
330EXPORT_SYMBOL_GPL(regcache_cache_bypass);
331
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100332bool regcache_set_val(void *base, unsigned int idx,
333 unsigned int val, unsigned int word_size)
334{
335 switch (word_size) {
336 case 1: {
337 u8 *cache = base;
338 if (cache[idx] == val)
339 return true;
340 cache[idx] = val;
341 break;
342 }
343 case 2: {
344 u16 *cache = base;
345 if (cache[idx] == val)
346 return true;
347 cache[idx] = val;
348 break;
349 }
350 default:
351 BUG();
352 }
353 /* unreachable */
354 return false;
355}
356
357unsigned int regcache_get_val(const void *base, unsigned int idx,
358 unsigned int word_size)
359{
360 if (!base)
361 return -EINVAL;
362
363 switch (word_size) {
364 case 1: {
365 const u8 *cache = base;
366 return cache[idx];
367 }
368 case 2: {
369 const u16 *cache = base;
370 return cache[idx];
371 }
372 default:
373 BUG();
374 }
375 /* unreachable */
376 return -1;
377}
378
Mark Brownf094fea2011-10-04 22:05:47 +0100379static int regcache_default_cmp(const void *a, const void *b)
Dimitris Papastamosc08604b2011-10-03 10:50:14 +0100380{
381 const struct reg_default *_a = a;
382 const struct reg_default *_b = b;
383
384 return _a->reg - _b->reg;
385}
386
Mark Brownf094fea2011-10-04 22:05:47 +0100387int regcache_lookup_reg(struct regmap *map, unsigned int reg)
388{
389 struct reg_default key;
390 struct reg_default *r;
391
392 key.reg = reg;
393 key.def = 0;
394
395 r = bsearch(&key, map->reg_defaults, map->num_reg_defaults,
396 sizeof(struct reg_default), regcache_default_cmp);
397
398 if (r)
399 return r - map->reg_defaults;
400 else
Mark Brown6e6ace02011-10-09 13:23:31 +0100401 return -ENOENT;
Mark Brownf094fea2011-10-04 22:05:47 +0100402}
403
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100404int regcache_insert_reg(struct regmap *map, unsigned int reg,
405 unsigned int val)
406{
407 void *tmp;
408
409 tmp = krealloc(map->reg_defaults,
410 (map->num_reg_defaults + 1) * sizeof(struct reg_default),
411 GFP_KERNEL);
412 if (!tmp)
413 return -ENOMEM;
414 map->reg_defaults = tmp;
415 map->num_reg_defaults++;
416 map->reg_defaults[map->num_reg_defaults - 1].reg = reg;
417 map->reg_defaults[map->num_reg_defaults - 1].def = val;
Dimitris Papastamosc08604b2011-10-03 10:50:14 +0100418 sort(map->reg_defaults, map->num_reg_defaults,
Mark Brownf094fea2011-10-04 22:05:47 +0100419 sizeof(struct reg_default), regcache_default_cmp, NULL);
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100420 return 0;
421}