blob: 214f704c34db0353abfd9f5afbdb52d64f9123e6 [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) {
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);
Lars-Peter Clausen021cd612011-11-14 10:40:16 +010064 if (!map->reg_defaults) {
65 ret = -ENOMEM;
66 goto err_free;
67 }
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010068
69 /* fill the reg_defaults */
70 map->num_reg_defaults = count;
71 for (i = 0, j = 0; i < map->num_reg_defaults_raw; i++) {
72 val = regcache_get_val(map->reg_defaults_raw,
73 i, map->cache_word_size);
74 if (!val)
75 continue;
76 map->reg_defaults[j].reg = i;
77 map->reg_defaults[j].def = val;
78 j++;
79 }
80
81 return 0;
Lars-Peter Clausen021cd612011-11-14 10:40:16 +010082
83err_free:
84 if (map->cache_free)
85 kfree(map->reg_defaults_raw);
86
87 return ret;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010088}
89
Lars-Peter Clausene5e3b8a2011-11-16 16:28:16 +010090int regcache_init(struct regmap *map, const struct regmap_config *config)
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010091{
92 int ret;
93 int i;
94 void *tmp_buf;
95
Mark Browne7a6db32011-09-19 16:08:03 +010096 if (map->cache_type == REGCACHE_NONE) {
97 map->cache_bypass = true;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010098 return 0;
Mark Browne7a6db32011-09-19 16:08:03 +010099 }
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100100
101 for (i = 0; i < ARRAY_SIZE(cache_types); i++)
102 if (cache_types[i]->type == map->cache_type)
103 break;
104
105 if (i == ARRAY_SIZE(cache_types)) {
106 dev_err(map->dev, "Could not match compress type: %d\n",
107 map->cache_type);
108 return -EINVAL;
109 }
110
Lars-Peter Clausene5e3b8a2011-11-16 16:28:16 +0100111 map->num_reg_defaults = config->num_reg_defaults;
112 map->num_reg_defaults_raw = config->num_reg_defaults_raw;
113 map->reg_defaults_raw = config->reg_defaults_raw;
Lars-Peter Clausen064d4db2011-11-16 20:34:03 +0100114 map->cache_word_size = DIV_ROUND_UP(config->val_bits, 8);
115 map->cache_size_raw = map->cache_word_size * config->num_reg_defaults_raw;
Lars-Peter Clausene5e3b8a2011-11-16 16:28:16 +0100116
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100117 map->cache = NULL;
118 map->cache_ops = cache_types[i];
119
120 if (!map->cache_ops->read ||
121 !map->cache_ops->write ||
122 !map->cache_ops->name)
123 return -EINVAL;
124
125 /* We still need to ensure that the reg_defaults
126 * won't vanish from under us. We'll need to make
127 * a copy of it.
128 */
Lars-Peter Clausen720e4612011-11-16 16:28:17 +0100129 if (config->reg_defaults) {
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100130 if (!map->num_reg_defaults)
131 return -EINVAL;
Lars-Peter Clausen720e4612011-11-16 16:28:17 +0100132 tmp_buf = kmemdup(config->reg_defaults, map->num_reg_defaults *
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100133 sizeof(struct reg_default), GFP_KERNEL);
134 if (!tmp_buf)
135 return -ENOMEM;
136 map->reg_defaults = tmp_buf;
Mark Brown8528bdd2011-10-09 13:13:58 +0100137 } else if (map->num_reg_defaults_raw) {
Mark Brown5fcd2562011-09-29 15:24:54 +0100138 /* Some devices such as PMICs don't have cache defaults,
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100139 * we cope with this by reading back the HW registers and
140 * crafting the cache defaults by hand.
141 */
142 ret = regcache_hw_init(map);
143 if (ret < 0)
144 return ret;
145 }
146
147 if (!map->max_register)
148 map->max_register = map->num_reg_defaults_raw;
149
150 if (map->cache_ops->init) {
151 dev_dbg(map->dev, "Initializing %s cache\n",
152 map->cache_ops->name);
Lars-Peter Clausenbd061c72011-11-14 10:40:17 +0100153 ret = map->cache_ops->init(map);
154 if (ret)
155 goto err_free;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100156 }
157 return 0;
Lars-Peter Clausenbd061c72011-11-14 10:40:17 +0100158
159err_free:
160 kfree(map->reg_defaults);
161 if (map->cache_free)
162 kfree(map->reg_defaults_raw);
163
164 return ret;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100165}
166
167void regcache_exit(struct regmap *map)
168{
169 if (map->cache_type == REGCACHE_NONE)
170 return;
171
172 BUG_ON(!map->cache_ops);
173
174 kfree(map->reg_defaults);
175 if (map->cache_free)
176 kfree(map->reg_defaults_raw);
177
178 if (map->cache_ops->exit) {
179 dev_dbg(map->dev, "Destroying %s cache\n",
180 map->cache_ops->name);
181 map->cache_ops->exit(map);
182 }
183}
184
185/**
186 * regcache_read: Fetch the value of a given register from the cache.
187 *
188 * @map: map to configure.
189 * @reg: The register index.
190 * @value: The value to be returned.
191 *
192 * Return a negative value on failure, 0 on success.
193 */
194int regcache_read(struct regmap *map,
195 unsigned int reg, unsigned int *value)
196{
Mark Brownbc7ee552011-11-30 14:27:08 +0000197 int ret;
198
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100199 if (map->cache_type == REGCACHE_NONE)
200 return -ENOSYS;
201
202 BUG_ON(!map->cache_ops);
203
Mark Brownbc7ee552011-11-30 14:27:08 +0000204 if (!regmap_volatile(map, reg)) {
205 ret = map->cache_ops->read(map, reg, value);
206
207 if (ret == 0)
208 trace_regmap_reg_read_cache(map->dev, reg, *value);
209
210 return ret;
211 }
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100212
213 return -EINVAL;
214}
215EXPORT_SYMBOL_GPL(regcache_read);
216
217/**
218 * regcache_write: Set the value of a given register in the cache.
219 *
220 * @map: map to configure.
221 * @reg: The register index.
222 * @value: The new register value.
223 *
224 * Return a negative value on failure, 0 on success.
225 */
226int regcache_write(struct regmap *map,
227 unsigned int reg, unsigned int value)
228{
229 if (map->cache_type == REGCACHE_NONE)
230 return 0;
231
232 BUG_ON(!map->cache_ops);
233
234 if (!regmap_writeable(map, reg))
235 return -EIO;
236
237 if (!regmap_volatile(map, reg))
238 return map->cache_ops->write(map, reg, value);
239
240 return 0;
241}
242EXPORT_SYMBOL_GPL(regcache_write);
243
244/**
245 * regcache_sync: Sync the register cache with the hardware.
246 *
247 * @map: map to configure.
248 *
249 * Any registers that should not be synced should be marked as
250 * volatile. In general drivers can choose not to use the provided
251 * syncing functionality if they so require.
252 *
253 * Return a negative value on failure, 0 on success.
254 */
255int regcache_sync(struct regmap *map)
256{
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100257 int ret = 0;
258 unsigned int val;
259 unsigned int i;
Dimitris Papastamos59360082011-09-19 14:34:04 +0100260 const char *name;
Dimitris Papastamosbeb1a102011-09-29 14:36:26 +0100261 unsigned int bypass;
Dimitris Papastamos59360082011-09-19 14:34:04 +0100262
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100263 BUG_ON(!map->cache_ops);
264
Dimitris Papastamos13753a92011-09-29 14:36:25 +0100265 mutex_lock(&map->lock);
Dimitris Papastamosbeb1a102011-09-29 14:36:26 +0100266 /* Remember the initial bypass state */
267 bypass = map->cache_bypass;
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100268 dev_dbg(map->dev, "Syncing %s cache\n",
269 map->cache_ops->name);
270 name = map->cache_ops->name;
271 trace_regcache_sync(map->dev, name, "start");
Mark Brown8ae0d7e2011-10-26 10:34:22 +0200272 if (!map->cache_dirty)
273 goto out;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100274 if (map->cache_ops->sync) {
Dimitris Papastamos59360082011-09-19 14:34:04 +0100275 ret = map->cache_ops->sync(map);
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100276 } else {
277 for (i = 0; i < map->num_reg_defaults; i++) {
278 ret = regcache_read(map, i, &val);
279 if (ret < 0)
280 goto out;
Dimitris Papastamosec8a3652011-09-28 11:43:42 +0100281 map->cache_bypass = 1;
Dimitris Papastamos13753a92011-09-29 14:36:25 +0100282 ret = _regmap_write(map, i, val);
Dimitris Papastamosec8a3652011-09-28 11:43:42 +0100283 map->cache_bypass = 0;
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100284 if (ret < 0)
285 goto out;
286 dev_dbg(map->dev, "Synced register %#x, value %#x\n",
287 map->reg_defaults[i].reg,
288 map->reg_defaults[i].def);
289 }
290
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100291 }
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100292out:
293 trace_regcache_sync(map->dev, name, "stop");
Dimitris Papastamosbeb1a102011-09-29 14:36:26 +0100294 /* Restore the bypass state */
295 map->cache_bypass = bypass;
Dimitris Papastamos13753a92011-09-29 14:36:25 +0100296 mutex_unlock(&map->lock);
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100297
298 return ret;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100299}
300EXPORT_SYMBOL_GPL(regcache_sync);
301
Mark Brown92afb282011-09-19 18:22:14 +0100302/**
303 * regcache_cache_only: Put a register map into cache only mode
304 *
305 * @map: map to configure
306 * @cache_only: flag if changes should be written to the hardware
307 *
308 * When a register map is marked as cache only writes to the register
309 * map API will only update the register cache, they will not cause
310 * any hardware changes. This is useful for allowing portions of
311 * drivers to act as though the device were functioning as normal when
312 * it is disabled for power saving reasons.
313 */
314void regcache_cache_only(struct regmap *map, bool enable)
315{
Mark Brown2cd148f2011-09-29 10:40:55 +0100316 mutex_lock(&map->lock);
Dimitris Papastamosac77a762011-09-29 14:36:28 +0100317 WARN_ON(map->cache_bypass && enable);
Mark Brown92afb282011-09-19 18:22:14 +0100318 map->cache_only = enable;
Mark Brown2cd148f2011-09-29 10:40:55 +0100319 mutex_unlock(&map->lock);
Mark Brown92afb282011-09-19 18:22:14 +0100320}
321EXPORT_SYMBOL_GPL(regcache_cache_only);
322
Dimitris Papastamos6eb0f5e2011-09-29 14:36:27 +0100323/**
Mark Brown8ae0d7e2011-10-26 10:34:22 +0200324 * regcache_mark_dirty: Mark the register cache as dirty
325 *
326 * @map: map to mark
327 *
328 * Mark the register cache as dirty, for example due to the device
329 * having been powered down for suspend. If the cache is not marked
330 * as dirty then the cache sync will be suppressed.
331 */
332void regcache_mark_dirty(struct regmap *map)
333{
334 mutex_lock(&map->lock);
335 map->cache_dirty = true;
336 mutex_unlock(&map->lock);
337}
338EXPORT_SYMBOL_GPL(regcache_mark_dirty);
339
340/**
Dimitris Papastamos6eb0f5e2011-09-29 14:36:27 +0100341 * regcache_cache_bypass: Put a register map into cache bypass mode
342 *
343 * @map: map to configure
Dimitris Papastamos0eef6b02011-10-03 06:54:16 +0100344 * @cache_bypass: flag if changes should not be written to the hardware
Dimitris Papastamos6eb0f5e2011-09-29 14:36:27 +0100345 *
346 * When a register map is marked with the cache bypass option, writes
347 * to the register map API will only update the hardware and not the
348 * the cache directly. This is useful when syncing the cache back to
349 * the hardware.
350 */
351void regcache_cache_bypass(struct regmap *map, bool enable)
352{
353 mutex_lock(&map->lock);
Dimitris Papastamosac77a762011-09-29 14:36:28 +0100354 WARN_ON(map->cache_only && enable);
Dimitris Papastamos6eb0f5e2011-09-29 14:36:27 +0100355 map->cache_bypass = enable;
356 mutex_unlock(&map->lock);
357}
358EXPORT_SYMBOL_GPL(regcache_cache_bypass);
359
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100360bool regcache_set_val(void *base, unsigned int idx,
361 unsigned int val, unsigned int word_size)
362{
363 switch (word_size) {
364 case 1: {
365 u8 *cache = base;
366 if (cache[idx] == val)
367 return true;
368 cache[idx] = val;
369 break;
370 }
371 case 2: {
372 u16 *cache = base;
373 if (cache[idx] == val)
374 return true;
375 cache[idx] = val;
376 break;
377 }
378 default:
379 BUG();
380 }
381 /* unreachable */
382 return false;
383}
384
385unsigned int regcache_get_val(const void *base, unsigned int idx,
386 unsigned int word_size)
387{
388 if (!base)
389 return -EINVAL;
390
391 switch (word_size) {
392 case 1: {
393 const u8 *cache = base;
394 return cache[idx];
395 }
396 case 2: {
397 const u16 *cache = base;
398 return cache[idx];
399 }
400 default:
401 BUG();
402 }
403 /* unreachable */
404 return -1;
405}
406
Mark Brownf094fea2011-10-04 22:05:47 +0100407static int regcache_default_cmp(const void *a, const void *b)
Dimitris Papastamosc08604b2011-10-03 10:50:14 +0100408{
409 const struct reg_default *_a = a;
410 const struct reg_default *_b = b;
411
412 return _a->reg - _b->reg;
413}
414
Mark Brownf094fea2011-10-04 22:05:47 +0100415int regcache_lookup_reg(struct regmap *map, unsigned int reg)
416{
417 struct reg_default key;
418 struct reg_default *r;
419
420 key.reg = reg;
421 key.def = 0;
422
423 r = bsearch(&key, map->reg_defaults, map->num_reg_defaults,
424 sizeof(struct reg_default), regcache_default_cmp);
425
426 if (r)
427 return r - map->reg_defaults;
428 else
Mark Brown6e6ace02011-10-09 13:23:31 +0100429 return -ENOENT;
Mark Brownf094fea2011-10-04 22:05:47 +0100430}