blob: 6f8a13ec32a410838d9c702af143dbdcbf0fff0c [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
Mark Brownf094fea2011-10-04 22:05:47 +010013#include <linux/bsearch.h>
Xiubo Lie39be3a2014-10-09 17:02:52 +080014#include <linux/device.h>
15#include <linux/export.h>
16#include <linux/slab.h>
Dimitris Papastamosc08604b2011-10-03 10:50:14 +010017#include <linux/sort.h>
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010018
Steven Rostedtf58078d2015-03-19 17:50:47 -040019#include "trace.h"
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010020#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
Xiubo Lifb700672014-10-09 17:02:57 +080039 /* calculate the size of reg_defaults */
40 for (count = 0, i = 0; i < map->num_reg_defaults_raw; i++)
41 if (!regmap_volatile(map, i * map->reg_stride))
42 count++;
43
44 /* all registers are volatile, so just bypass */
45 if (!count) {
46 map->cache_bypass = true;
47 return 0;
48 }
49
50 map->num_reg_defaults = count;
51 map->reg_defaults = kmalloc_array(count, sizeof(struct reg_default),
52 GFP_KERNEL);
53 if (!map->reg_defaults)
54 return -ENOMEM;
55
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010056 if (!map->reg_defaults_raw) {
Laxman Dewangandf00c792012-02-17 18:57:26 +053057 u32 cache_bypass = map->cache_bypass;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010058 dev_warn(map->dev, "No cache defaults, reading back from HW\n");
Laxman Dewangandf00c792012-02-17 18:57:26 +053059
60 /* Bypass the cache access till data read from HW*/
61 map->cache_bypass = 1;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010062 tmp_buf = kmalloc(map->cache_size_raw, GFP_KERNEL);
Xiubo Lifb700672014-10-09 17:02:57 +080063 if (!tmp_buf) {
64 ret = -ENOMEM;
65 goto err_free;
66 }
Mark Browneb4cb762013-02-21 18:39:47 +000067 ret = regmap_raw_read(map, 0, tmp_buf,
68 map->num_reg_defaults_raw);
Laxman Dewangandf00c792012-02-17 18:57:26 +053069 map->cache_bypass = cache_bypass;
Xiubo Lifb700672014-10-09 17:02:57 +080070 if (ret < 0)
71 goto err_cache_free;
72
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010073 map->reg_defaults_raw = tmp_buf;
74 map->cache_free = 1;
75 }
76
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010077 /* fill the reg_defaults */
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010078 for (i = 0, j = 0; i < map->num_reg_defaults_raw; i++) {
Stephen Warrenf01ee602012-04-09 13:40:24 -060079 if (regmap_volatile(map, i * map->reg_stride))
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010080 continue;
Xiubo Lifbba43c2014-10-09 17:02:55 +080081 val = regcache_get_val(map, map->reg_defaults_raw, i);
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
Xiubo Lifb700672014-10-09 17:02:57 +080089err_cache_free:
90 kfree(tmp_buf);
Lars-Peter Clausen021cd612011-11-14 10:40:16 +010091err_free:
Xiubo Lifb700672014-10-09 17:02:57 +080092 kfree(map->reg_defaults);
Lars-Peter Clausen021cd612011-11-14 10:40:16 +010093
94 return ret;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010095}
96
Lars-Peter Clausene5e3b8a2011-11-16 16:28:16 +010097int regcache_init(struct regmap *map, const struct regmap_config *config)
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010098{
99 int ret;
100 int i;
101 void *tmp_buf;
102
Stephen Warrenf01ee602012-04-09 13:40:24 -0600103 for (i = 0; i < config->num_reg_defaults; i++)
104 if (config->reg_defaults[i].reg % map->reg_stride)
105 return -EINVAL;
106
Mark Browne7a6db32011-09-19 16:08:03 +0100107 if (map->cache_type == REGCACHE_NONE) {
108 map->cache_bypass = true;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100109 return 0;
Mark Browne7a6db32011-09-19 16:08:03 +0100110 }
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100111
112 for (i = 0; i < ARRAY_SIZE(cache_types); i++)
113 if (cache_types[i]->type == map->cache_type)
114 break;
115
116 if (i == ARRAY_SIZE(cache_types)) {
117 dev_err(map->dev, "Could not match compress type: %d\n",
118 map->cache_type);
119 return -EINVAL;
120 }
121
Lars-Peter Clausene5e3b8a2011-11-16 16:28:16 +0100122 map->num_reg_defaults = config->num_reg_defaults;
123 map->num_reg_defaults_raw = config->num_reg_defaults_raw;
124 map->reg_defaults_raw = config->reg_defaults_raw;
Lars-Peter Clausen064d4db2011-11-16 20:34:03 +0100125 map->cache_word_size = DIV_ROUND_UP(config->val_bits, 8);
126 map->cache_size_raw = map->cache_word_size * config->num_reg_defaults_raw;
Lars-Peter Clausene5e3b8a2011-11-16 16:28:16 +0100127
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100128 map->cache = NULL;
129 map->cache_ops = cache_types[i];
130
131 if (!map->cache_ops->read ||
132 !map->cache_ops->write ||
133 !map->cache_ops->name)
134 return -EINVAL;
135
136 /* We still need to ensure that the reg_defaults
137 * won't vanish from under us. We'll need to make
138 * a copy of it.
139 */
Lars-Peter Clausen720e4612011-11-16 16:28:17 +0100140 if (config->reg_defaults) {
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100141 if (!map->num_reg_defaults)
142 return -EINVAL;
Lars-Peter Clausen720e4612011-11-16 16:28:17 +0100143 tmp_buf = kmemdup(config->reg_defaults, map->num_reg_defaults *
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100144 sizeof(struct reg_default), GFP_KERNEL);
145 if (!tmp_buf)
146 return -ENOMEM;
147 map->reg_defaults = tmp_buf;
Mark Brown8528bdd2011-10-09 13:13:58 +0100148 } else if (map->num_reg_defaults_raw) {
Mark Brown5fcd2562011-09-29 15:24:54 +0100149 /* Some devices such as PMICs don't have cache defaults,
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100150 * we cope with this by reading back the HW registers and
151 * crafting the cache defaults by hand.
152 */
153 ret = regcache_hw_init(map);
154 if (ret < 0)
155 return ret;
Xiubo Lifb700672014-10-09 17:02:57 +0800156 if (map->cache_bypass)
157 return 0;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100158 }
159
160 if (!map->max_register)
161 map->max_register = map->num_reg_defaults_raw;
162
163 if (map->cache_ops->init) {
164 dev_dbg(map->dev, "Initializing %s cache\n",
165 map->cache_ops->name);
Lars-Peter Clausenbd061c72011-11-14 10:40:17 +0100166 ret = map->cache_ops->init(map);
167 if (ret)
168 goto err_free;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100169 }
170 return 0;
Lars-Peter Clausenbd061c72011-11-14 10:40:17 +0100171
172err_free:
173 kfree(map->reg_defaults);
174 if (map->cache_free)
175 kfree(map->reg_defaults_raw);
176
177 return ret;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100178}
179
180void regcache_exit(struct regmap *map)
181{
182 if (map->cache_type == REGCACHE_NONE)
183 return;
184
185 BUG_ON(!map->cache_ops);
186
187 kfree(map->reg_defaults);
188 if (map->cache_free)
189 kfree(map->reg_defaults_raw);
190
191 if (map->cache_ops->exit) {
192 dev_dbg(map->dev, "Destroying %s cache\n",
193 map->cache_ops->name);
194 map->cache_ops->exit(map);
195 }
196}
197
198/**
199 * regcache_read: Fetch the value of a given register from the cache.
200 *
201 * @map: map to configure.
202 * @reg: The register index.
203 * @value: The value to be returned.
204 *
205 * Return a negative value on failure, 0 on success.
206 */
207int regcache_read(struct regmap *map,
208 unsigned int reg, unsigned int *value)
209{
Mark Brownbc7ee552011-11-30 14:27:08 +0000210 int ret;
211
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100212 if (map->cache_type == REGCACHE_NONE)
213 return -ENOSYS;
214
215 BUG_ON(!map->cache_ops);
216
Mark Brownbc7ee552011-11-30 14:27:08 +0000217 if (!regmap_volatile(map, reg)) {
218 ret = map->cache_ops->read(map, reg, value);
219
220 if (ret == 0)
Philipp Zabelc6b570d2015-03-09 12:20:13 +0100221 trace_regmap_reg_read_cache(map, reg, *value);
Mark Brownbc7ee552011-11-30 14:27:08 +0000222
223 return ret;
224 }
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100225
226 return -EINVAL;
227}
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100228
229/**
230 * regcache_write: Set the value of a given register in the cache.
231 *
232 * @map: map to configure.
233 * @reg: The register index.
234 * @value: The new register value.
235 *
236 * Return a negative value on failure, 0 on success.
237 */
238int regcache_write(struct regmap *map,
239 unsigned int reg, unsigned int value)
240{
241 if (map->cache_type == REGCACHE_NONE)
242 return 0;
243
244 BUG_ON(!map->cache_ops);
245
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100246 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
Kevin Cernekee3969fa082015-05-05 15:14:13 -0700252static bool regcache_reg_needs_sync(struct regmap *map, unsigned int reg,
253 unsigned int val)
254{
255 int ret;
256
Kevin Cernekee1c797712015-05-05 15:14:14 -0700257 /* If we don't know the chip just got reset, then sync everything. */
258 if (!map->no_sync_defaults)
259 return true;
260
Kevin Cernekee3969fa082015-05-05 15:14:13 -0700261 /* Is this the hardware default? If so skip. */
262 ret = regcache_lookup_reg(map, reg);
263 if (ret >= 0 && val == map->reg_defaults[ret].def)
264 return false;
265 return true;
266}
267
Maarten ter Huurned856fce2013-06-03 00:15:26 +0200268static int regcache_default_sync(struct regmap *map, unsigned int min,
269 unsigned int max)
270{
271 unsigned int reg;
272
Dylan Reid75617322014-03-18 13:45:08 -0700273 for (reg = min; reg <= max; reg += map->reg_stride) {
Maarten ter Huurned856fce2013-06-03 00:15:26 +0200274 unsigned int val;
275 int ret;
276
Dylan Reid83f84752014-03-18 13:45:09 -0700277 if (regmap_volatile(map, reg) ||
278 !regmap_writeable(map, reg))
Maarten ter Huurned856fce2013-06-03 00:15:26 +0200279 continue;
280
281 ret = regcache_read(map, reg, &val);
282 if (ret)
283 return ret;
284
Kevin Cernekee3969fa082015-05-05 15:14:13 -0700285 if (!regcache_reg_needs_sync(map, reg, val))
Maarten ter Huurned856fce2013-06-03 00:15:26 +0200286 continue;
287
288 map->cache_bypass = 1;
289 ret = _regmap_write(map, reg, val);
290 map->cache_bypass = 0;
Jarkko Nikulaf29a4322014-09-16 14:04:14 +0300291 if (ret) {
292 dev_err(map->dev, "Unable to sync register %#x. %d\n",
293 reg, ret);
Maarten ter Huurned856fce2013-06-03 00:15:26 +0200294 return ret;
Jarkko Nikulaf29a4322014-09-16 14:04:14 +0300295 }
Maarten ter Huurned856fce2013-06-03 00:15:26 +0200296 dev_dbg(map->dev, "Synced register %#x, value %#x\n", reg, val);
297 }
298
299 return 0;
300}
301
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100302/**
303 * regcache_sync: Sync the register cache with the hardware.
304 *
305 * @map: map to configure.
306 *
307 * Any registers that should not be synced should be marked as
308 * volatile. In general drivers can choose not to use the provided
309 * syncing functionality if they so require.
310 *
311 * Return a negative value on failure, 0 on success.
312 */
313int regcache_sync(struct regmap *map)
314{
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100315 int ret = 0;
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100316 unsigned int i;
Dimitris Papastamos59360082011-09-19 14:34:04 +0100317 const char *name;
Dimitris Papastamosbeb1a102011-09-29 14:36:26 +0100318 unsigned int bypass;
Dimitris Papastamos59360082011-09-19 14:34:04 +0100319
Maarten ter Huurned856fce2013-06-03 00:15:26 +0200320 BUG_ON(!map->cache_ops);
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100321
Lars-Peter Clausen81485f52013-05-23 15:06:15 +0200322 map->lock(map->lock_arg);
Dimitris Papastamosbeb1a102011-09-29 14:36:26 +0100323 /* Remember the initial bypass state */
324 bypass = map->cache_bypass;
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100325 dev_dbg(map->dev, "Syncing %s cache\n",
326 map->cache_ops->name);
327 name = map->cache_ops->name;
Philipp Zabelc6b570d2015-03-09 12:20:13 +0100328 trace_regcache_sync(map, name, "start");
Mark Brown22f0d902012-01-21 12:01:14 +0000329
Mark Brown8ae0d7e2011-10-26 10:34:22 +0200330 if (!map->cache_dirty)
331 goto out;
Mark Brownd9db7622012-01-25 21:06:33 +0000332
Mark Brownaffbe882013-10-10 21:06:32 +0100333 map->async = true;
334
Mark Brown22f0d902012-01-21 12:01:14 +0000335 /* Apply any patch first */
Mark Brown8a892d62012-01-25 21:05:48 +0000336 map->cache_bypass = 1;
Mark Brown22f0d902012-01-21 12:01:14 +0000337 for (i = 0; i < map->patch_regs; i++) {
338 ret = _regmap_write(map, map->patch[i].reg, map->patch[i].def);
339 if (ret != 0) {
340 dev_err(map->dev, "Failed to write %x = %x: %d\n",
341 map->patch[i].reg, map->patch[i].def, ret);
342 goto out;
343 }
344 }
Mark Brown8a892d62012-01-25 21:05:48 +0000345 map->cache_bypass = 0;
Mark Brown22f0d902012-01-21 12:01:14 +0000346
Maarten ter Huurned856fce2013-06-03 00:15:26 +0200347 if (map->cache_ops->sync)
348 ret = map->cache_ops->sync(map, 0, map->max_register);
349 else
350 ret = regcache_default_sync(map, 0, map->max_register);
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100351
Mark Brown6ff73732012-02-23 22:05:59 +0000352 if (ret == 0)
353 map->cache_dirty = false;
354
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100355out:
Dimitris Papastamosbeb1a102011-09-29 14:36:26 +0100356 /* Restore the bypass state */
Mark Brownaffbe882013-10-10 21:06:32 +0100357 map->async = false;
Dimitris Papastamosbeb1a102011-09-29 14:36:26 +0100358 map->cache_bypass = bypass;
Kevin Cernekee1c797712015-05-05 15:14:14 -0700359 map->no_sync_defaults = false;
Lars-Peter Clausen81485f52013-05-23 15:06:15 +0200360 map->unlock(map->lock_arg);
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100361
Mark Brownaffbe882013-10-10 21:06:32 +0100362 regmap_async_complete(map);
363
Philipp Zabelc6b570d2015-03-09 12:20:13 +0100364 trace_regcache_sync(map, name, "stop");
Mark Brownaffbe882013-10-10 21:06:32 +0100365
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100366 return ret;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100367}
368EXPORT_SYMBOL_GPL(regcache_sync);
369
Mark Brown92afb282011-09-19 18:22:14 +0100370/**
Mark Brown4d4cfd12012-02-23 20:53:37 +0000371 * regcache_sync_region: Sync part of the register cache with the hardware.
372 *
373 * @map: map to sync.
374 * @min: first register to sync
375 * @max: last register to sync
376 *
377 * Write all non-default register values in the specified region to
378 * the hardware.
379 *
380 * Return a negative value on failure, 0 on success.
381 */
382int regcache_sync_region(struct regmap *map, unsigned int min,
383 unsigned int max)
384{
385 int ret = 0;
386 const char *name;
387 unsigned int bypass;
388
Maarten ter Huurned856fce2013-06-03 00:15:26 +0200389 BUG_ON(!map->cache_ops);
Mark Brown4d4cfd12012-02-23 20:53:37 +0000390
Lars-Peter Clausen81485f52013-05-23 15:06:15 +0200391 map->lock(map->lock_arg);
Mark Brown4d4cfd12012-02-23 20:53:37 +0000392
393 /* Remember the initial bypass state */
394 bypass = map->cache_bypass;
395
396 name = map->cache_ops->name;
397 dev_dbg(map->dev, "Syncing %s cache from %d-%d\n", name, min, max);
398
Philipp Zabelc6b570d2015-03-09 12:20:13 +0100399 trace_regcache_sync(map, name, "start region");
Mark Brown4d4cfd12012-02-23 20:53:37 +0000400
401 if (!map->cache_dirty)
402 goto out;
403
Mark Brownaffbe882013-10-10 21:06:32 +0100404 map->async = true;
405
Maarten ter Huurned856fce2013-06-03 00:15:26 +0200406 if (map->cache_ops->sync)
407 ret = map->cache_ops->sync(map, min, max);
408 else
409 ret = regcache_default_sync(map, min, max);
Mark Brown4d4cfd12012-02-23 20:53:37 +0000410
411out:
Mark Brown4d4cfd12012-02-23 20:53:37 +0000412 /* Restore the bypass state */
413 map->cache_bypass = bypass;
Mark Brownaffbe882013-10-10 21:06:32 +0100414 map->async = false;
Kevin Cernekee1c797712015-05-05 15:14:14 -0700415 map->no_sync_defaults = false;
Lars-Peter Clausen81485f52013-05-23 15:06:15 +0200416 map->unlock(map->lock_arg);
Mark Brown4d4cfd12012-02-23 20:53:37 +0000417
Mark Brownaffbe882013-10-10 21:06:32 +0100418 regmap_async_complete(map);
419
Philipp Zabelc6b570d2015-03-09 12:20:13 +0100420 trace_regcache_sync(map, name, "stop region");
Mark Brownaffbe882013-10-10 21:06:32 +0100421
Mark Brown4d4cfd12012-02-23 20:53:37 +0000422 return ret;
423}
Mark Browne466de02012-04-03 13:08:53 +0100424EXPORT_SYMBOL_GPL(regcache_sync_region);
Mark Brown4d4cfd12012-02-23 20:53:37 +0000425
426/**
Mark Brown697e85b2013-05-08 13:55:22 +0100427 * regcache_drop_region: Discard part of the register cache
428 *
429 * @map: map to operate on
430 * @min: first register to discard
431 * @max: last register to discard
432 *
433 * Discard part of the register cache.
434 *
435 * Return a negative value on failure, 0 on success.
436 */
437int regcache_drop_region(struct regmap *map, unsigned int min,
438 unsigned int max)
439{
Mark Brown697e85b2013-05-08 13:55:22 +0100440 int ret = 0;
441
Lars-Peter Clausen3f4ff562013-08-29 10:26:34 +0200442 if (!map->cache_ops || !map->cache_ops->drop)
Mark Brown697e85b2013-05-08 13:55:22 +0100443 return -EINVAL;
444
Lars-Peter Clausen81485f52013-05-23 15:06:15 +0200445 map->lock(map->lock_arg);
Mark Brown697e85b2013-05-08 13:55:22 +0100446
Philipp Zabelc6b570d2015-03-09 12:20:13 +0100447 trace_regcache_drop_region(map, min, max);
Mark Brown697e85b2013-05-08 13:55:22 +0100448
Lars-Peter Clausen3f4ff562013-08-29 10:26:34 +0200449 ret = map->cache_ops->drop(map, min, max);
Mark Brown697e85b2013-05-08 13:55:22 +0100450
Lars-Peter Clausen81485f52013-05-23 15:06:15 +0200451 map->unlock(map->lock_arg);
Mark Brown697e85b2013-05-08 13:55:22 +0100452
453 return ret;
454}
455EXPORT_SYMBOL_GPL(regcache_drop_region);
456
457/**
Mark Brown92afb282011-09-19 18:22:14 +0100458 * regcache_cache_only: Put a register map into cache only mode
459 *
460 * @map: map to configure
461 * @cache_only: flag if changes should be written to the hardware
462 *
463 * When a register map is marked as cache only writes to the register
464 * map API will only update the register cache, they will not cause
465 * any hardware changes. This is useful for allowing portions of
466 * drivers to act as though the device were functioning as normal when
467 * it is disabled for power saving reasons.
468 */
469void regcache_cache_only(struct regmap *map, bool enable)
470{
Lars-Peter Clausen81485f52013-05-23 15:06:15 +0200471 map->lock(map->lock_arg);
Dimitris Papastamosac77a762011-09-29 14:36:28 +0100472 WARN_ON(map->cache_bypass && enable);
Mark Brown92afb282011-09-19 18:22:14 +0100473 map->cache_only = enable;
Philipp Zabelc6b570d2015-03-09 12:20:13 +0100474 trace_regmap_cache_only(map, enable);
Lars-Peter Clausen81485f52013-05-23 15:06:15 +0200475 map->unlock(map->lock_arg);
Mark Brown92afb282011-09-19 18:22:14 +0100476}
477EXPORT_SYMBOL_GPL(regcache_cache_only);
478
Dimitris Papastamos6eb0f5e2011-09-29 14:36:27 +0100479/**
Kevin Cernekee1c797712015-05-05 15:14:14 -0700480 * regcache_mark_dirty: Indicate that HW registers were reset to default values
Mark Brown8ae0d7e2011-10-26 10:34:22 +0200481 *
482 * @map: map to mark
483 *
Kevin Cernekee1c797712015-05-05 15:14:14 -0700484 * Inform regcache that the device has been powered down or reset, so that
485 * on resume, regcache_sync() knows to write out all non-default values
486 * stored in the cache.
487 *
488 * If this function is not called, regcache_sync() will assume that
489 * the hardware state still matches the cache state, modulo any writes that
490 * happened when cache_only was true.
Mark Brown8ae0d7e2011-10-26 10:34:22 +0200491 */
492void regcache_mark_dirty(struct regmap *map)
493{
Lars-Peter Clausen81485f52013-05-23 15:06:15 +0200494 map->lock(map->lock_arg);
Mark Brown8ae0d7e2011-10-26 10:34:22 +0200495 map->cache_dirty = true;
Kevin Cernekee1c797712015-05-05 15:14:14 -0700496 map->no_sync_defaults = true;
Lars-Peter Clausen81485f52013-05-23 15:06:15 +0200497 map->unlock(map->lock_arg);
Mark Brown8ae0d7e2011-10-26 10:34:22 +0200498}
499EXPORT_SYMBOL_GPL(regcache_mark_dirty);
500
501/**
Dimitris Papastamos6eb0f5e2011-09-29 14:36:27 +0100502 * regcache_cache_bypass: Put a register map into cache bypass mode
503 *
504 * @map: map to configure
Dimitris Papastamos0eef6b02011-10-03 06:54:16 +0100505 * @cache_bypass: flag if changes should not be written to the hardware
Dimitris Papastamos6eb0f5e2011-09-29 14:36:27 +0100506 *
507 * When a register map is marked with the cache bypass option, writes
508 * to the register map API will only update the hardware and not the
509 * the cache directly. This is useful when syncing the cache back to
510 * the hardware.
511 */
512void regcache_cache_bypass(struct regmap *map, bool enable)
513{
Lars-Peter Clausen81485f52013-05-23 15:06:15 +0200514 map->lock(map->lock_arg);
Dimitris Papastamosac77a762011-09-29 14:36:28 +0100515 WARN_ON(map->cache_only && enable);
Dimitris Papastamos6eb0f5e2011-09-29 14:36:27 +0100516 map->cache_bypass = enable;
Philipp Zabelc6b570d2015-03-09 12:20:13 +0100517 trace_regmap_cache_bypass(map, enable);
Lars-Peter Clausen81485f52013-05-23 15:06:15 +0200518 map->unlock(map->lock_arg);
Dimitris Papastamos6eb0f5e2011-09-29 14:36:27 +0100519}
520EXPORT_SYMBOL_GPL(regcache_cache_bypass);
521
Mark Brown879082c2013-02-21 18:03:13 +0000522bool regcache_set_val(struct regmap *map, void *base, unsigned int idx,
523 unsigned int val)
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100524{
Mark Brown325acab2013-02-21 18:07:01 +0000525 if (regcache_get_val(map, base, idx) == val)
526 return true;
527
Mark Browneb4cb762013-02-21 18:39:47 +0000528 /* Use device native format if possible */
529 if (map->format.format_val) {
530 map->format.format_val(base + (map->cache_word_size * idx),
531 val, 0);
532 return false;
533 }
534
Mark Brown879082c2013-02-21 18:03:13 +0000535 switch (map->cache_word_size) {
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100536 case 1: {
537 u8 *cache = base;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100538 cache[idx] = val;
539 break;
540 }
541 case 2: {
542 u16 *cache = base;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100543 cache[idx] = val;
544 break;
545 }
Mark Brown7d5e5252012-02-17 15:58:25 -0800546 case 4: {
547 u32 *cache = base;
Mark Brown7d5e5252012-02-17 15:58:25 -0800548 cache[idx] = val;
549 break;
550 }
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100551 default:
552 BUG();
553 }
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100554 return false;
555}
556
Mark Brown879082c2013-02-21 18:03:13 +0000557unsigned int regcache_get_val(struct regmap *map, const void *base,
558 unsigned int idx)
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100559{
560 if (!base)
561 return -EINVAL;
562
Mark Browneb4cb762013-02-21 18:39:47 +0000563 /* Use device native format if possible */
564 if (map->format.parse_val)
Mark Brown88177962013-03-13 19:29:36 +0000565 return map->format.parse_val(regcache_get_val_addr(map, base,
566 idx));
Mark Browneb4cb762013-02-21 18:39:47 +0000567
Mark Brown879082c2013-02-21 18:03:13 +0000568 switch (map->cache_word_size) {
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100569 case 1: {
570 const u8 *cache = base;
571 return cache[idx];
572 }
573 case 2: {
574 const u16 *cache = base;
575 return cache[idx];
576 }
Mark Brown7d5e5252012-02-17 15:58:25 -0800577 case 4: {
578 const u32 *cache = base;
579 return cache[idx];
580 }
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100581 default:
582 BUG();
583 }
584 /* unreachable */
585 return -1;
586}
587
Mark Brownf094fea2011-10-04 22:05:47 +0100588static int regcache_default_cmp(const void *a, const void *b)
Dimitris Papastamosc08604b2011-10-03 10:50:14 +0100589{
590 const struct reg_default *_a = a;
591 const struct reg_default *_b = b;
592
593 return _a->reg - _b->reg;
594}
595
Mark Brownf094fea2011-10-04 22:05:47 +0100596int regcache_lookup_reg(struct regmap *map, unsigned int reg)
597{
598 struct reg_default key;
599 struct reg_default *r;
600
601 key.reg = reg;
602 key.def = 0;
603
604 r = bsearch(&key, map->reg_defaults, map->num_reg_defaults,
605 sizeof(struct reg_default), regcache_default_cmp);
606
607 if (r)
608 return r - map->reg_defaults;
609 else
Mark Brown6e6ace02011-10-09 13:23:31 +0100610 return -ENOENT;
Mark Brownf094fea2011-10-04 22:05:47 +0100611}
Mark Brownf8bd8222013-03-29 19:32:28 +0000612
Lars-Peter Clausen3f4ff562013-08-29 10:26:34 +0200613static bool regcache_reg_present(unsigned long *cache_present, unsigned int idx)
614{
615 if (!cache_present)
616 return true;
617
618 return test_bit(idx, cache_present);
619}
620
Mark Browncfdeb8c2013-03-29 20:12:21 +0000621static int regcache_sync_block_single(struct regmap *map, void *block,
Lars-Peter Clausen3f4ff562013-08-29 10:26:34 +0200622 unsigned long *cache_present,
Mark Browncfdeb8c2013-03-29 20:12:21 +0000623 unsigned int block_base,
624 unsigned int start, unsigned int end)
625{
626 unsigned int i, regtmp, val;
627 int ret;
628
629 for (i = start; i < end; i++) {
630 regtmp = block_base + (i * map->reg_stride);
631
Takashi Iwai4ceba982015-03-04 15:29:17 +0100632 if (!regcache_reg_present(cache_present, i) ||
633 !regmap_writeable(map, regtmp))
Mark Browncfdeb8c2013-03-29 20:12:21 +0000634 continue;
635
636 val = regcache_get_val(map, block, i);
Kevin Cernekee3969fa082015-05-05 15:14:13 -0700637 if (!regcache_reg_needs_sync(map, regtmp, val))
Mark Browncfdeb8c2013-03-29 20:12:21 +0000638 continue;
639
640 map->cache_bypass = 1;
641
642 ret = _regmap_write(map, regtmp, val);
643
644 map->cache_bypass = 0;
Jarkko Nikulaf29a4322014-09-16 14:04:14 +0300645 if (ret != 0) {
646 dev_err(map->dev, "Unable to sync register %#x. %d\n",
647 regtmp, ret);
Mark Browncfdeb8c2013-03-29 20:12:21 +0000648 return ret;
Jarkko Nikulaf29a4322014-09-16 14:04:14 +0300649 }
Mark Browncfdeb8c2013-03-29 20:12:21 +0000650 dev_dbg(map->dev, "Synced register %#x, value %#x\n",
651 regtmp, val);
652 }
653
654 return 0;
655}
656
Mark Brown75a5f892013-03-29 20:50:07 +0000657static int regcache_sync_block_raw_flush(struct regmap *map, const void **data,
658 unsigned int base, unsigned int cur)
659{
660 size_t val_bytes = map->format.val_bytes;
661 int ret, count;
662
663 if (*data == NULL)
664 return 0;
665
Dylan Reid78ba73e2014-01-24 15:40:39 -0800666 count = (cur - base) / map->reg_stride;
Mark Brown75a5f892013-03-29 20:50:07 +0000667
Stratos Karafotis96592932013-04-04 19:40:45 +0300668 dev_dbg(map->dev, "Writing %zu bytes for %d registers from 0x%x-0x%x\n",
Dylan Reid78ba73e2014-01-24 15:40:39 -0800669 count * val_bytes, count, base, cur - map->reg_stride);
Mark Brown75a5f892013-03-29 20:50:07 +0000670
671 map->cache_bypass = 1;
672
Mark Brown0a819802013-10-09 12:28:52 +0100673 ret = _regmap_raw_write(map, base, *data, count * val_bytes);
Jarkko Nikulaf29a4322014-09-16 14:04:14 +0300674 if (ret)
675 dev_err(map->dev, "Unable to sync registers %#x-%#x. %d\n",
676 base, cur - map->reg_stride, ret);
Mark Brown75a5f892013-03-29 20:50:07 +0000677
678 map->cache_bypass = 0;
679
680 *data = NULL;
681
682 return ret;
683}
684
Sachin Kamatf52687a2013-04-04 14:36:18 +0530685static int regcache_sync_block_raw(struct regmap *map, void *block,
Lars-Peter Clausen3f4ff562013-08-29 10:26:34 +0200686 unsigned long *cache_present,
Mark Browncfdeb8c2013-03-29 20:12:21 +0000687 unsigned int block_base, unsigned int start,
688 unsigned int end)
Mark Brownf8bd8222013-03-29 19:32:28 +0000689{
Mark Brown75a5f892013-03-29 20:50:07 +0000690 unsigned int i, val;
691 unsigned int regtmp = 0;
692 unsigned int base = 0;
693 const void *data = NULL;
Mark Brownf8bd8222013-03-29 19:32:28 +0000694 int ret;
695
696 for (i = start; i < end; i++) {
697 regtmp = block_base + (i * map->reg_stride);
698
Takashi Iwai4ceba982015-03-04 15:29:17 +0100699 if (!regcache_reg_present(cache_present, i) ||
700 !regmap_writeable(map, regtmp)) {
Mark Brown75a5f892013-03-29 20:50:07 +0000701 ret = regcache_sync_block_raw_flush(map, &data,
702 base, regtmp);
703 if (ret != 0)
704 return ret;
Mark Brownf8bd8222013-03-29 19:32:28 +0000705 continue;
Mark Brown75a5f892013-03-29 20:50:07 +0000706 }
Mark Brownf8bd8222013-03-29 19:32:28 +0000707
708 val = regcache_get_val(map, block, i);
Kevin Cernekee3969fa082015-05-05 15:14:13 -0700709 if (!regcache_reg_needs_sync(map, regtmp, val)) {
Mark Brown75a5f892013-03-29 20:50:07 +0000710 ret = regcache_sync_block_raw_flush(map, &data,
711 base, regtmp);
712 if (ret != 0)
713 return ret;
Mark Brownf8bd8222013-03-29 19:32:28 +0000714 continue;
Mark Brown75a5f892013-03-29 20:50:07 +0000715 }
Mark Brownf8bd8222013-03-29 19:32:28 +0000716
Mark Brown75a5f892013-03-29 20:50:07 +0000717 if (!data) {
718 data = regcache_get_val_addr(map, block, i);
719 base = regtmp;
720 }
Mark Brownf8bd8222013-03-29 19:32:28 +0000721 }
722
Lars-Peter Clausen2d49b592013-08-05 11:21:29 +0200723 return regcache_sync_block_raw_flush(map, &data, base, regtmp +
724 map->reg_stride);
Mark Brownf8bd8222013-03-29 19:32:28 +0000725}
Mark Browncfdeb8c2013-03-29 20:12:21 +0000726
727int regcache_sync_block(struct regmap *map, void *block,
Lars-Peter Clausen3f4ff562013-08-29 10:26:34 +0200728 unsigned long *cache_present,
Mark Browncfdeb8c2013-03-29 20:12:21 +0000729 unsigned int block_base, unsigned int start,
730 unsigned int end)
731{
Markus Pargmann67921a12015-08-21 10:26:42 +0200732 if (regmap_can_raw_write(map) && !map->use_single_write)
Lars-Peter Clausen3f4ff562013-08-29 10:26:34 +0200733 return regcache_sync_block_raw(map, block, cache_present,
734 block_base, start, end);
Mark Browncfdeb8c2013-03-29 20:12:21 +0000735 else
Lars-Peter Clausen3f4ff562013-08-29 10:26:34 +0200736 return regcache_sync_block_single(map, block, cache_present,
737 block_base, start, end);
Mark Browncfdeb8c2013-03-29 20:12:21 +0000738}