blob: 0ad6cfb2c8cc27d0cdd3b2cf6b389ab3f0a115c3 [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);
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->reg_defaults = config->reg_defaults;
112 map->num_reg_defaults = config->num_reg_defaults;
113 map->num_reg_defaults_raw = config->num_reg_defaults_raw;
114 map->reg_defaults_raw = config->reg_defaults_raw;
115 map->cache_size_raw = (config->val_bits / 8) * config->num_reg_defaults_raw;
116 map->cache_word_size = config->val_bits / 8;
117
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100118 map->cache = NULL;
119 map->cache_ops = cache_types[i];
120
121 if (!map->cache_ops->read ||
122 !map->cache_ops->write ||
123 !map->cache_ops->name)
124 return -EINVAL;
125
126 /* We still need to ensure that the reg_defaults
127 * won't vanish from under us. We'll need to make
128 * a copy of it.
129 */
130 if (map->reg_defaults) {
131 if (!map->num_reg_defaults)
132 return -EINVAL;
133 tmp_buf = kmemdup(map->reg_defaults, map->num_reg_defaults *
134 sizeof(struct reg_default), GFP_KERNEL);
135 if (!tmp_buf)
136 return -ENOMEM;
137 map->reg_defaults = tmp_buf;
Mark Brown8528bdd2011-10-09 13:13:58 +0100138 } else if (map->num_reg_defaults_raw) {
Mark Brown5fcd2562011-09-29 15:24:54 +0100139 /* Some devices such as PMICs don't have cache defaults,
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100140 * we cope with this by reading back the HW registers and
141 * crafting the cache defaults by hand.
142 */
143 ret = regcache_hw_init(map);
144 if (ret < 0)
145 return ret;
146 }
147
148 if (!map->max_register)
149 map->max_register = map->num_reg_defaults_raw;
150
151 if (map->cache_ops->init) {
152 dev_dbg(map->dev, "Initializing %s cache\n",
153 map->cache_ops->name);
Lars-Peter Clausenbd061c72011-11-14 10:40:17 +0100154 ret = map->cache_ops->init(map);
155 if (ret)
156 goto err_free;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100157 }
158 return 0;
Lars-Peter Clausenbd061c72011-11-14 10:40:17 +0100159
160err_free:
161 kfree(map->reg_defaults);
162 if (map->cache_free)
163 kfree(map->reg_defaults_raw);
164
165 return ret;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100166}
167
168void regcache_exit(struct regmap *map)
169{
170 if (map->cache_type == REGCACHE_NONE)
171 return;
172
173 BUG_ON(!map->cache_ops);
174
175 kfree(map->reg_defaults);
176 if (map->cache_free)
177 kfree(map->reg_defaults_raw);
178
179 if (map->cache_ops->exit) {
180 dev_dbg(map->dev, "Destroying %s cache\n",
181 map->cache_ops->name);
182 map->cache_ops->exit(map);
183 }
184}
185
186/**
187 * regcache_read: Fetch the value of a given register from the cache.
188 *
189 * @map: map to configure.
190 * @reg: The register index.
191 * @value: The value to be returned.
192 *
193 * Return a negative value on failure, 0 on success.
194 */
195int regcache_read(struct regmap *map,
196 unsigned int reg, unsigned int *value)
197{
198 if (map->cache_type == REGCACHE_NONE)
199 return -ENOSYS;
200
201 BUG_ON(!map->cache_ops);
202
203 if (!regmap_readable(map, reg))
204 return -EIO;
205
206 if (!regmap_volatile(map, reg))
207 return map->cache_ops->read(map, reg, value);
208
209 return -EINVAL;
210}
211EXPORT_SYMBOL_GPL(regcache_read);
212
213/**
214 * regcache_write: Set the value of a given register in the cache.
215 *
216 * @map: map to configure.
217 * @reg: The register index.
218 * @value: The new register value.
219 *
220 * Return a negative value on failure, 0 on success.
221 */
222int regcache_write(struct regmap *map,
223 unsigned int reg, unsigned int value)
224{
225 if (map->cache_type == REGCACHE_NONE)
226 return 0;
227
228 BUG_ON(!map->cache_ops);
229
230 if (!regmap_writeable(map, reg))
231 return -EIO;
232
233 if (!regmap_volatile(map, reg))
234 return map->cache_ops->write(map, reg, value);
235
236 return 0;
237}
238EXPORT_SYMBOL_GPL(regcache_write);
239
240/**
241 * regcache_sync: Sync the register cache with the hardware.
242 *
243 * @map: map to configure.
244 *
245 * Any registers that should not be synced should be marked as
246 * volatile. In general drivers can choose not to use the provided
247 * syncing functionality if they so require.
248 *
249 * Return a negative value on failure, 0 on success.
250 */
251int regcache_sync(struct regmap *map)
252{
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100253 int ret = 0;
254 unsigned int val;
255 unsigned int i;
Dimitris Papastamos59360082011-09-19 14:34:04 +0100256 const char *name;
Dimitris Papastamosbeb1a102011-09-29 14:36:26 +0100257 unsigned int bypass;
Dimitris Papastamos59360082011-09-19 14:34:04 +0100258
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100259 BUG_ON(!map->cache_ops);
260
Dimitris Papastamos13753a92011-09-29 14:36:25 +0100261 mutex_lock(&map->lock);
Dimitris Papastamosbeb1a102011-09-29 14:36:26 +0100262 /* Remember the initial bypass state */
263 bypass = map->cache_bypass;
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100264 dev_dbg(map->dev, "Syncing %s cache\n",
265 map->cache_ops->name);
266 name = map->cache_ops->name;
267 trace_regcache_sync(map->dev, name, "start");
Mark Brown8ae0d7e2011-10-26 10:34:22 +0200268 if (!map->cache_dirty)
269 goto out;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100270 if (map->cache_ops->sync) {
Dimitris Papastamos59360082011-09-19 14:34:04 +0100271 ret = map->cache_ops->sync(map);
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100272 } else {
273 for (i = 0; i < map->num_reg_defaults; i++) {
274 ret = regcache_read(map, i, &val);
275 if (ret < 0)
276 goto out;
Dimitris Papastamosec8a3652011-09-28 11:43:42 +0100277 map->cache_bypass = 1;
Dimitris Papastamos13753a92011-09-29 14:36:25 +0100278 ret = _regmap_write(map, i, val);
Dimitris Papastamosec8a3652011-09-28 11:43:42 +0100279 map->cache_bypass = 0;
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100280 if (ret < 0)
281 goto out;
282 dev_dbg(map->dev, "Synced register %#x, value %#x\n",
283 map->reg_defaults[i].reg,
284 map->reg_defaults[i].def);
285 }
286
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100287 }
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100288out:
289 trace_regcache_sync(map->dev, name, "stop");
Dimitris Papastamosbeb1a102011-09-29 14:36:26 +0100290 /* Restore the bypass state */
291 map->cache_bypass = bypass;
Dimitris Papastamos13753a92011-09-29 14:36:25 +0100292 mutex_unlock(&map->lock);
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100293
294 return ret;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100295}
296EXPORT_SYMBOL_GPL(regcache_sync);
297
Mark Brown92afb282011-09-19 18:22:14 +0100298/**
299 * regcache_cache_only: Put a register map into cache only mode
300 *
301 * @map: map to configure
302 * @cache_only: flag if changes should be written to the hardware
303 *
304 * When a register map is marked as cache only writes to the register
305 * map API will only update the register cache, they will not cause
306 * any hardware changes. This is useful for allowing portions of
307 * drivers to act as though the device were functioning as normal when
308 * it is disabled for power saving reasons.
309 */
310void regcache_cache_only(struct regmap *map, bool enable)
311{
Mark Brown2cd148f2011-09-29 10:40:55 +0100312 mutex_lock(&map->lock);
Dimitris Papastamosac77a762011-09-29 14:36:28 +0100313 WARN_ON(map->cache_bypass && enable);
Mark Brown92afb282011-09-19 18:22:14 +0100314 map->cache_only = enable;
Mark Brown2cd148f2011-09-29 10:40:55 +0100315 mutex_unlock(&map->lock);
Mark Brown92afb282011-09-19 18:22:14 +0100316}
317EXPORT_SYMBOL_GPL(regcache_cache_only);
318
Dimitris Papastamos6eb0f5e2011-09-29 14:36:27 +0100319/**
Mark Brown8ae0d7e2011-10-26 10:34:22 +0200320 * regcache_mark_dirty: Mark the register cache as dirty
321 *
322 * @map: map to mark
323 *
324 * Mark the register cache as dirty, for example due to the device
325 * having been powered down for suspend. If the cache is not marked
326 * as dirty then the cache sync will be suppressed.
327 */
328void regcache_mark_dirty(struct regmap *map)
329{
330 mutex_lock(&map->lock);
331 map->cache_dirty = true;
332 mutex_unlock(&map->lock);
333}
334EXPORT_SYMBOL_GPL(regcache_mark_dirty);
335
336/**
Dimitris Papastamos6eb0f5e2011-09-29 14:36:27 +0100337 * regcache_cache_bypass: Put a register map into cache bypass mode
338 *
339 * @map: map to configure
Dimitris Papastamos0eef6b02011-10-03 06:54:16 +0100340 * @cache_bypass: flag if changes should not be written to the hardware
Dimitris Papastamos6eb0f5e2011-09-29 14:36:27 +0100341 *
342 * When a register map is marked with the cache bypass option, writes
343 * to the register map API will only update the hardware and not the
344 * the cache directly. This is useful when syncing the cache back to
345 * the hardware.
346 */
347void regcache_cache_bypass(struct regmap *map, bool enable)
348{
349 mutex_lock(&map->lock);
Dimitris Papastamosac77a762011-09-29 14:36:28 +0100350 WARN_ON(map->cache_only && enable);
Dimitris Papastamos6eb0f5e2011-09-29 14:36:27 +0100351 map->cache_bypass = enable;
352 mutex_unlock(&map->lock);
353}
354EXPORT_SYMBOL_GPL(regcache_cache_bypass);
355
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100356bool regcache_set_val(void *base, unsigned int idx,
357 unsigned int val, unsigned int word_size)
358{
359 switch (word_size) {
360 case 1: {
361 u8 *cache = base;
362 if (cache[idx] == val)
363 return true;
364 cache[idx] = val;
365 break;
366 }
367 case 2: {
368 u16 *cache = base;
369 if (cache[idx] == val)
370 return true;
371 cache[idx] = val;
372 break;
373 }
374 default:
375 BUG();
376 }
377 /* unreachable */
378 return false;
379}
380
381unsigned int regcache_get_val(const void *base, unsigned int idx,
382 unsigned int word_size)
383{
384 if (!base)
385 return -EINVAL;
386
387 switch (word_size) {
388 case 1: {
389 const u8 *cache = base;
390 return cache[idx];
391 }
392 case 2: {
393 const u16 *cache = base;
394 return cache[idx];
395 }
396 default:
397 BUG();
398 }
399 /* unreachable */
400 return -1;
401}
402
Mark Brownf094fea2011-10-04 22:05:47 +0100403static int regcache_default_cmp(const void *a, const void *b)
Dimitris Papastamosc08604b2011-10-03 10:50:14 +0100404{
405 const struct reg_default *_a = a;
406 const struct reg_default *_b = b;
407
408 return _a->reg - _b->reg;
409}
410
Mark Brownf094fea2011-10-04 22:05:47 +0100411int regcache_lookup_reg(struct regmap *map, unsigned int reg)
412{
413 struct reg_default key;
414 struct reg_default *r;
415
416 key.reg = reg;
417 key.def = 0;
418
419 r = bsearch(&key, map->reg_defaults, map->num_reg_defaults,
420 sizeof(struct reg_default), regcache_default_cmp);
421
422 if (r)
423 return r - map->reg_defaults;
424 else
Mark Brown6e6ace02011-10-09 13:23:31 +0100425 return -ENOENT;
Mark Brownf094fea2011-10-04 22:05:47 +0100426}
427
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100428int regcache_insert_reg(struct regmap *map, unsigned int reg,
429 unsigned int val)
430{
431 void *tmp;
432
433 tmp = krealloc(map->reg_defaults,
434 (map->num_reg_defaults + 1) * sizeof(struct reg_default),
435 GFP_KERNEL);
436 if (!tmp)
437 return -ENOMEM;
438 map->reg_defaults = tmp;
439 map->num_reg_defaults++;
440 map->reg_defaults[map->num_reg_defaults - 1].reg = reg;
441 map->reg_defaults[map->num_reg_defaults - 1].def = val;
Dimitris Papastamosc08604b2011-10-03 10:50:14 +0100442 sort(map->reg_defaults, map->num_reg_defaults,
Mark Brownf094fea2011-10-04 22:05:47 +0100443 sizeof(struct reg_default), regcache_default_cmp, NULL);
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100444 return 0;
445}