blob: 348be3a354108eb2533587dabed9495e9c324b5a [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 Papastamos28644c82011-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) {
Viresh Kumar621a5f72015-09-26 15:04:07 -070057 bool 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*/
Viresh Kumar621a5f72015-09-26 15:04:07 -070061 map->cache_bypass = true;
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
Mark Browne7a6db32011-09-19 16:08:03 +0100103 if (map->cache_type == REGCACHE_NONE) {
Xiubo Li8cfe2fd2015-12-11 11:23:19 +0800104 if (config->reg_defaults || config->num_reg_defaults_raw)
105 dev_warn(map->dev,
106 "No cache used with register defaults set!\n");
107
Mark Browne7a6db32011-09-19 16:08:03 +0100108 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
Xiubo Li167f7062015-12-11 11:23:20 +0800112 if (config->reg_defaults && !config->num_reg_defaults) {
113 dev_err(map->dev,
114 "Register defaults are set without the number!\n");
115 return -EINVAL;
116 }
117
Xiubo Li8cfe2fd2015-12-11 11:23:19 +0800118 for (i = 0; i < config->num_reg_defaults; i++)
119 if (config->reg_defaults[i].reg % map->reg_stride)
120 return -EINVAL;
121
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100122 for (i = 0; i < ARRAY_SIZE(cache_types); i++)
123 if (cache_types[i]->type == map->cache_type)
124 break;
125
126 if (i == ARRAY_SIZE(cache_types)) {
127 dev_err(map->dev, "Could not match compress type: %d\n",
128 map->cache_type);
129 return -EINVAL;
130 }
131
Lars-Peter Clausene5e3b8a2011-11-16 16:28:16 +0100132 map->num_reg_defaults = config->num_reg_defaults;
133 map->num_reg_defaults_raw = config->num_reg_defaults_raw;
134 map->reg_defaults_raw = config->reg_defaults_raw;
Lars-Peter Clausen064d4db2011-11-16 20:34:03 +0100135 map->cache_word_size = DIV_ROUND_UP(config->val_bits, 8);
136 map->cache_size_raw = map->cache_word_size * config->num_reg_defaults_raw;
Lars-Peter Clausene5e3b8a2011-11-16 16:28:16 +0100137
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100138 map->cache = NULL;
139 map->cache_ops = cache_types[i];
140
141 if (!map->cache_ops->read ||
142 !map->cache_ops->write ||
143 !map->cache_ops->name)
144 return -EINVAL;
145
146 /* We still need to ensure that the reg_defaults
147 * won't vanish from under us. We'll need to make
148 * a copy of it.
149 */
Lars-Peter Clausen720e4612011-11-16 16:28:17 +0100150 if (config->reg_defaults) {
Lars-Peter Clausen720e4612011-11-16 16:28:17 +0100151 tmp_buf = kmemdup(config->reg_defaults, map->num_reg_defaults *
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100152 sizeof(struct reg_default), GFP_KERNEL);
153 if (!tmp_buf)
154 return -ENOMEM;
155 map->reg_defaults = tmp_buf;
Mark Brown8528bdd2011-10-09 13:13:58 +0100156 } else if (map->num_reg_defaults_raw) {
Mark Brown5fcd2562011-09-29 15:24:54 +0100157 /* Some devices such as PMICs don't have cache defaults,
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100158 * we cope with this by reading back the HW registers and
159 * crafting the cache defaults by hand.
160 */
161 ret = regcache_hw_init(map);
162 if (ret < 0)
163 return ret;
Xiubo Lifb700672014-10-09 17:02:57 +0800164 if (map->cache_bypass)
165 return 0;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100166 }
167
168 if (!map->max_register)
169 map->max_register = map->num_reg_defaults_raw;
170
171 if (map->cache_ops->init) {
172 dev_dbg(map->dev, "Initializing %s cache\n",
173 map->cache_ops->name);
Lars-Peter Clausenbd061c72011-11-14 10:40:17 +0100174 ret = map->cache_ops->init(map);
175 if (ret)
176 goto err_free;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100177 }
178 return 0;
Lars-Peter Clausenbd061c72011-11-14 10:40:17 +0100179
180err_free:
181 kfree(map->reg_defaults);
182 if (map->cache_free)
183 kfree(map->reg_defaults_raw);
184
185 return ret;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100186}
187
188void regcache_exit(struct regmap *map)
189{
190 if (map->cache_type == REGCACHE_NONE)
191 return;
192
193 BUG_ON(!map->cache_ops);
194
195 kfree(map->reg_defaults);
196 if (map->cache_free)
197 kfree(map->reg_defaults_raw);
198
199 if (map->cache_ops->exit) {
200 dev_dbg(map->dev, "Destroying %s cache\n",
201 map->cache_ops->name);
202 map->cache_ops->exit(map);
203 }
204}
205
206/**
207 * regcache_read: Fetch the value of a given register from the cache.
208 *
209 * @map: map to configure.
210 * @reg: The register index.
211 * @value: The value to be returned.
212 *
213 * Return a negative value on failure, 0 on success.
214 */
215int regcache_read(struct regmap *map,
216 unsigned int reg, unsigned int *value)
217{
Mark Brownbc7ee552011-11-30 14:27:08 +0000218 int ret;
219
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100220 if (map->cache_type == REGCACHE_NONE)
221 return -ENOSYS;
222
223 BUG_ON(!map->cache_ops);
224
Mark Brownbc7ee552011-11-30 14:27:08 +0000225 if (!regmap_volatile(map, reg)) {
226 ret = map->cache_ops->read(map, reg, value);
227
228 if (ret == 0)
Philipp Zabelc6b570d2015-03-09 12:20:13 +0100229 trace_regmap_reg_read_cache(map, reg, *value);
Mark Brownbc7ee552011-11-30 14:27:08 +0000230
231 return ret;
232 }
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100233
234 return -EINVAL;
235}
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100236
237/**
238 * regcache_write: Set the value of a given register in the cache.
239 *
240 * @map: map to configure.
241 * @reg: The register index.
242 * @value: The new register value.
243 *
244 * Return a negative value on failure, 0 on success.
245 */
246int regcache_write(struct regmap *map,
247 unsigned int reg, unsigned int value)
248{
249 if (map->cache_type == REGCACHE_NONE)
250 return 0;
251
252 BUG_ON(!map->cache_ops);
253
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100254 if (!regmap_volatile(map, reg))
255 return map->cache_ops->write(map, reg, value);
256
257 return 0;
258}
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100259
Kevin Cernekee3969fa082015-05-05 15:14:13 -0700260static bool regcache_reg_needs_sync(struct regmap *map, unsigned int reg,
261 unsigned int val)
262{
263 int ret;
264
Kevin Cernekee1c797712015-05-05 15:14:14 -0700265 /* If we don't know the chip just got reset, then sync everything. */
266 if (!map->no_sync_defaults)
267 return true;
268
Kevin Cernekee3969fa082015-05-05 15:14:13 -0700269 /* Is this the hardware default? If so skip. */
270 ret = regcache_lookup_reg(map, reg);
271 if (ret >= 0 && val == map->reg_defaults[ret].def)
272 return false;
273 return true;
274}
275
Maarten ter Huurned856fce2013-06-03 00:15:26 +0200276static int regcache_default_sync(struct regmap *map, unsigned int min,
277 unsigned int max)
278{
279 unsigned int reg;
280
Dylan Reid75617322014-03-18 13:45:08 -0700281 for (reg = min; reg <= max; reg += map->reg_stride) {
Maarten ter Huurned856fce2013-06-03 00:15:26 +0200282 unsigned int val;
283 int ret;
284
Dylan Reid83f84752014-03-18 13:45:09 -0700285 if (regmap_volatile(map, reg) ||
286 !regmap_writeable(map, reg))
Maarten ter Huurned856fce2013-06-03 00:15:26 +0200287 continue;
288
289 ret = regcache_read(map, reg, &val);
290 if (ret)
291 return ret;
292
Kevin Cernekee3969fa082015-05-05 15:14:13 -0700293 if (!regcache_reg_needs_sync(map, reg, val))
Maarten ter Huurned856fce2013-06-03 00:15:26 +0200294 continue;
295
Viresh Kumar621a5f72015-09-26 15:04:07 -0700296 map->cache_bypass = true;
Maarten ter Huurned856fce2013-06-03 00:15:26 +0200297 ret = _regmap_write(map, reg, val);
Viresh Kumar621a5f72015-09-26 15:04:07 -0700298 map->cache_bypass = false;
Jarkko Nikulaf29a4322014-09-16 14:04:14 +0300299 if (ret) {
300 dev_err(map->dev, "Unable to sync register %#x. %d\n",
301 reg, ret);
Maarten ter Huurned856fce2013-06-03 00:15:26 +0200302 return ret;
Jarkko Nikulaf29a4322014-09-16 14:04:14 +0300303 }
Maarten ter Huurned856fce2013-06-03 00:15:26 +0200304 dev_dbg(map->dev, "Synced register %#x, value %#x\n", reg, val);
305 }
306
307 return 0;
308}
309
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100310/**
311 * regcache_sync: Sync the register cache with the hardware.
312 *
313 * @map: map to configure.
314 *
315 * Any registers that should not be synced should be marked as
316 * volatile. In general drivers can choose not to use the provided
317 * syncing functionality if they so require.
318 *
319 * Return a negative value on failure, 0 on success.
320 */
321int regcache_sync(struct regmap *map)
322{
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100323 int ret = 0;
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100324 unsigned int i;
Dimitris Papastamos59360082011-09-19 14:34:04 +0100325 const char *name;
Viresh Kumar621a5f72015-09-26 15:04:07 -0700326 bool bypass;
Dimitris Papastamos59360082011-09-19 14:34:04 +0100327
Maarten ter Huurned856fce2013-06-03 00:15:26 +0200328 BUG_ON(!map->cache_ops);
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100329
Lars-Peter Clausen81485f52013-05-23 15:06:15 +0200330 map->lock(map->lock_arg);
Dimitris Papastamosbeb1a102011-09-29 14:36:26 +0100331 /* Remember the initial bypass state */
332 bypass = map->cache_bypass;
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100333 dev_dbg(map->dev, "Syncing %s cache\n",
334 map->cache_ops->name);
335 name = map->cache_ops->name;
Philipp Zabelc6b570d2015-03-09 12:20:13 +0100336 trace_regcache_sync(map, name, "start");
Mark Brown22f0d902012-01-21 12:01:14 +0000337
Mark Brown8ae0d7e2011-10-26 10:34:22 +0200338 if (!map->cache_dirty)
339 goto out;
Mark Brownd9db7622012-01-25 21:06:33 +0000340
Mark Brownaffbe882013-10-10 21:06:32 +0100341 map->async = true;
342
Mark Brown22f0d902012-01-21 12:01:14 +0000343 /* Apply any patch first */
Viresh Kumar621a5f72015-09-26 15:04:07 -0700344 map->cache_bypass = true;
Mark Brown22f0d902012-01-21 12:01:14 +0000345 for (i = 0; i < map->patch_regs; i++) {
346 ret = _regmap_write(map, map->patch[i].reg, map->patch[i].def);
347 if (ret != 0) {
348 dev_err(map->dev, "Failed to write %x = %x: %d\n",
349 map->patch[i].reg, map->patch[i].def, ret);
350 goto out;
351 }
352 }
Viresh Kumar621a5f72015-09-26 15:04:07 -0700353 map->cache_bypass = false;
Mark Brown22f0d902012-01-21 12:01:14 +0000354
Maarten ter Huurned856fce2013-06-03 00:15:26 +0200355 if (map->cache_ops->sync)
356 ret = map->cache_ops->sync(map, 0, map->max_register);
357 else
358 ret = regcache_default_sync(map, 0, map->max_register);
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100359
Mark Brown6ff73732012-02-23 22:05:59 +0000360 if (ret == 0)
361 map->cache_dirty = false;
362
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100363out:
Dimitris Papastamosbeb1a102011-09-29 14:36:26 +0100364 /* Restore the bypass state */
Mark Brownaffbe882013-10-10 21:06:32 +0100365 map->async = false;
Dimitris Papastamosbeb1a102011-09-29 14:36:26 +0100366 map->cache_bypass = bypass;
Kevin Cernekee1c797712015-05-05 15:14:14 -0700367 map->no_sync_defaults = false;
Lars-Peter Clausen81485f52013-05-23 15:06:15 +0200368 map->unlock(map->lock_arg);
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100369
Mark Brownaffbe882013-10-10 21:06:32 +0100370 regmap_async_complete(map);
371
Philipp Zabelc6b570d2015-03-09 12:20:13 +0100372 trace_regcache_sync(map, name, "stop");
Mark Brownaffbe882013-10-10 21:06:32 +0100373
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100374 return ret;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100375}
376EXPORT_SYMBOL_GPL(regcache_sync);
377
Mark Brown92afb282011-09-19 18:22:14 +0100378/**
Mark Brown4d4cfd12012-02-23 20:53:37 +0000379 * regcache_sync_region: Sync part of the register cache with the hardware.
380 *
381 * @map: map to sync.
382 * @min: first register to sync
383 * @max: last register to sync
384 *
385 * Write all non-default register values in the specified region to
386 * the hardware.
387 *
388 * Return a negative value on failure, 0 on success.
389 */
390int regcache_sync_region(struct regmap *map, unsigned int min,
391 unsigned int max)
392{
393 int ret = 0;
394 const char *name;
Viresh Kumar621a5f72015-09-26 15:04:07 -0700395 bool bypass;
Mark Brown4d4cfd12012-02-23 20:53:37 +0000396
Maarten ter Huurned856fce2013-06-03 00:15:26 +0200397 BUG_ON(!map->cache_ops);
Mark Brown4d4cfd12012-02-23 20:53:37 +0000398
Lars-Peter Clausen81485f52013-05-23 15:06:15 +0200399 map->lock(map->lock_arg);
Mark Brown4d4cfd12012-02-23 20:53:37 +0000400
401 /* Remember the initial bypass state */
402 bypass = map->cache_bypass;
403
404 name = map->cache_ops->name;
405 dev_dbg(map->dev, "Syncing %s cache from %d-%d\n", name, min, max);
406
Philipp Zabelc6b570d2015-03-09 12:20:13 +0100407 trace_regcache_sync(map, name, "start region");
Mark Brown4d4cfd12012-02-23 20:53:37 +0000408
409 if (!map->cache_dirty)
410 goto out;
411
Mark Brownaffbe882013-10-10 21:06:32 +0100412 map->async = true;
413
Maarten ter Huurned856fce2013-06-03 00:15:26 +0200414 if (map->cache_ops->sync)
415 ret = map->cache_ops->sync(map, min, max);
416 else
417 ret = regcache_default_sync(map, min, max);
Mark Brown4d4cfd12012-02-23 20:53:37 +0000418
419out:
Mark Brown4d4cfd12012-02-23 20:53:37 +0000420 /* Restore the bypass state */
421 map->cache_bypass = bypass;
Mark Brownaffbe882013-10-10 21:06:32 +0100422 map->async = false;
Kevin Cernekee1c797712015-05-05 15:14:14 -0700423 map->no_sync_defaults = false;
Lars-Peter Clausen81485f52013-05-23 15:06:15 +0200424 map->unlock(map->lock_arg);
Mark Brown4d4cfd12012-02-23 20:53:37 +0000425
Mark Brownaffbe882013-10-10 21:06:32 +0100426 regmap_async_complete(map);
427
Philipp Zabelc6b570d2015-03-09 12:20:13 +0100428 trace_regcache_sync(map, name, "stop region");
Mark Brownaffbe882013-10-10 21:06:32 +0100429
Mark Brown4d4cfd12012-02-23 20:53:37 +0000430 return ret;
431}
Mark Browne466de02012-04-03 13:08:53 +0100432EXPORT_SYMBOL_GPL(regcache_sync_region);
Mark Brown4d4cfd12012-02-23 20:53:37 +0000433
434/**
Mark Brown697e85b2013-05-08 13:55:22 +0100435 * regcache_drop_region: Discard part of the register cache
436 *
437 * @map: map to operate on
438 * @min: first register to discard
439 * @max: last register to discard
440 *
441 * Discard part of the register cache.
442 *
443 * Return a negative value on failure, 0 on success.
444 */
445int regcache_drop_region(struct regmap *map, unsigned int min,
446 unsigned int max)
447{
Mark Brown697e85b2013-05-08 13:55:22 +0100448 int ret = 0;
449
Lars-Peter Clausen3f4ff562013-08-29 10:26:34 +0200450 if (!map->cache_ops || !map->cache_ops->drop)
Mark Brown697e85b2013-05-08 13:55:22 +0100451 return -EINVAL;
452
Lars-Peter Clausen81485f52013-05-23 15:06:15 +0200453 map->lock(map->lock_arg);
Mark Brown697e85b2013-05-08 13:55:22 +0100454
Philipp Zabelc6b570d2015-03-09 12:20:13 +0100455 trace_regcache_drop_region(map, min, max);
Mark Brown697e85b2013-05-08 13:55:22 +0100456
Lars-Peter Clausen3f4ff562013-08-29 10:26:34 +0200457 ret = map->cache_ops->drop(map, min, max);
Mark Brown697e85b2013-05-08 13:55:22 +0100458
Lars-Peter Clausen81485f52013-05-23 15:06:15 +0200459 map->unlock(map->lock_arg);
Mark Brown697e85b2013-05-08 13:55:22 +0100460
461 return ret;
462}
463EXPORT_SYMBOL_GPL(regcache_drop_region);
464
465/**
Mark Brown92afb282011-09-19 18:22:14 +0100466 * regcache_cache_only: Put a register map into cache only mode
467 *
468 * @map: map to configure
469 * @cache_only: flag if changes should be written to the hardware
470 *
471 * When a register map is marked as cache only writes to the register
472 * map API will only update the register cache, they will not cause
473 * any hardware changes. This is useful for allowing portions of
474 * drivers to act as though the device were functioning as normal when
475 * it is disabled for power saving reasons.
476 */
477void regcache_cache_only(struct regmap *map, bool enable)
478{
Lars-Peter Clausen81485f52013-05-23 15:06:15 +0200479 map->lock(map->lock_arg);
Dimitris Papastamosac77a762011-09-29 14:36:28 +0100480 WARN_ON(map->cache_bypass && enable);
Mark Brown92afb282011-09-19 18:22:14 +0100481 map->cache_only = enable;
Philipp Zabelc6b570d2015-03-09 12:20:13 +0100482 trace_regmap_cache_only(map, enable);
Lars-Peter Clausen81485f52013-05-23 15:06:15 +0200483 map->unlock(map->lock_arg);
Mark Brown92afb282011-09-19 18:22:14 +0100484}
485EXPORT_SYMBOL_GPL(regcache_cache_only);
486
Dimitris Papastamos6eb0f5e2011-09-29 14:36:27 +0100487/**
Kevin Cernekee1c797712015-05-05 15:14:14 -0700488 * regcache_mark_dirty: Indicate that HW registers were reset to default values
Mark Brown8ae0d7e2011-10-26 10:34:22 +0200489 *
490 * @map: map to mark
491 *
Kevin Cernekee1c797712015-05-05 15:14:14 -0700492 * Inform regcache that the device has been powered down or reset, so that
493 * on resume, regcache_sync() knows to write out all non-default values
494 * stored in the cache.
495 *
496 * If this function is not called, regcache_sync() will assume that
497 * the hardware state still matches the cache state, modulo any writes that
498 * happened when cache_only was true.
Mark Brown8ae0d7e2011-10-26 10:34:22 +0200499 */
500void regcache_mark_dirty(struct regmap *map)
501{
Lars-Peter Clausen81485f52013-05-23 15:06:15 +0200502 map->lock(map->lock_arg);
Mark Brown8ae0d7e2011-10-26 10:34:22 +0200503 map->cache_dirty = true;
Kevin Cernekee1c797712015-05-05 15:14:14 -0700504 map->no_sync_defaults = true;
Lars-Peter Clausen81485f52013-05-23 15:06:15 +0200505 map->unlock(map->lock_arg);
Mark Brown8ae0d7e2011-10-26 10:34:22 +0200506}
507EXPORT_SYMBOL_GPL(regcache_mark_dirty);
508
509/**
Dimitris Papastamos6eb0f5e2011-09-29 14:36:27 +0100510 * regcache_cache_bypass: Put a register map into cache bypass mode
511 *
512 * @map: map to configure
Dimitris Papastamos0eef6b02011-10-03 06:54:16 +0100513 * @cache_bypass: flag if changes should not be written to the hardware
Dimitris Papastamos6eb0f5e2011-09-29 14:36:27 +0100514 *
515 * When a register map is marked with the cache bypass option, writes
516 * to the register map API will only update the hardware and not the
517 * the cache directly. This is useful when syncing the cache back to
518 * the hardware.
519 */
520void regcache_cache_bypass(struct regmap *map, bool enable)
521{
Lars-Peter Clausen81485f52013-05-23 15:06:15 +0200522 map->lock(map->lock_arg);
Dimitris Papastamosac77a762011-09-29 14:36:28 +0100523 WARN_ON(map->cache_only && enable);
Dimitris Papastamos6eb0f5e2011-09-29 14:36:27 +0100524 map->cache_bypass = enable;
Philipp Zabelc6b570d2015-03-09 12:20:13 +0100525 trace_regmap_cache_bypass(map, enable);
Lars-Peter Clausen81485f52013-05-23 15:06:15 +0200526 map->unlock(map->lock_arg);
Dimitris Papastamos6eb0f5e2011-09-29 14:36:27 +0100527}
528EXPORT_SYMBOL_GPL(regcache_cache_bypass);
529
Mark Brown879082c2013-02-21 18:03:13 +0000530bool regcache_set_val(struct regmap *map, void *base, unsigned int idx,
531 unsigned int val)
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100532{
Mark Brown325acab2013-02-21 18:07:01 +0000533 if (regcache_get_val(map, base, idx) == val)
534 return true;
535
Mark Browneb4cb762013-02-21 18:39:47 +0000536 /* Use device native format if possible */
537 if (map->format.format_val) {
538 map->format.format_val(base + (map->cache_word_size * idx),
539 val, 0);
540 return false;
541 }
542
Mark Brown879082c2013-02-21 18:03:13 +0000543 switch (map->cache_word_size) {
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100544 case 1: {
545 u8 *cache = base;
Xiubo Li2fd69022015-12-09 13:09:06 +0800546
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100547 cache[idx] = val;
548 break;
549 }
550 case 2: {
551 u16 *cache = base;
Xiubo Li2fd69022015-12-09 13:09:06 +0800552
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100553 cache[idx] = val;
554 break;
555 }
Mark Brown7d5e5252012-02-17 15:58:25 -0800556 case 4: {
557 u32 *cache = base;
Xiubo Li2fd69022015-12-09 13:09:06 +0800558
Mark Brown7d5e5252012-02-17 15:58:25 -0800559 cache[idx] = val;
560 break;
561 }
Xiubo Li8b7663d2015-12-09 13:09:07 +0800562#ifdef CONFIG_64BIT
563 case 8: {
564 u64 *cache = base;
565
566 cache[idx] = val;
567 break;
568 }
569#endif
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100570 default:
571 BUG();
572 }
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100573 return false;
574}
575
Mark Brown879082c2013-02-21 18:03:13 +0000576unsigned int regcache_get_val(struct regmap *map, const void *base,
577 unsigned int idx)
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100578{
579 if (!base)
580 return -EINVAL;
581
Mark Browneb4cb762013-02-21 18:39:47 +0000582 /* Use device native format if possible */
583 if (map->format.parse_val)
Mark Brown88177962013-03-13 19:29:36 +0000584 return map->format.parse_val(regcache_get_val_addr(map, base,
585 idx));
Mark Browneb4cb762013-02-21 18:39:47 +0000586
Mark Brown879082c2013-02-21 18:03:13 +0000587 switch (map->cache_word_size) {
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100588 case 1: {
589 const u8 *cache = base;
Xiubo Li2fd69022015-12-09 13:09:06 +0800590
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100591 return cache[idx];
592 }
593 case 2: {
594 const u16 *cache = base;
Xiubo Li2fd69022015-12-09 13:09:06 +0800595
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100596 return cache[idx];
597 }
Mark Brown7d5e5252012-02-17 15:58:25 -0800598 case 4: {
599 const u32 *cache = base;
Xiubo Li2fd69022015-12-09 13:09:06 +0800600
Mark Brown7d5e5252012-02-17 15:58:25 -0800601 return cache[idx];
602 }
Xiubo Li8b7663d2015-12-09 13:09:07 +0800603#ifdef CONFIG_64BIT
604 case 8: {
605 const u64 *cache = base;
606
607 return cache[idx];
608 }
609#endif
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100610 default:
611 BUG();
612 }
613 /* unreachable */
614 return -1;
615}
616
Mark Brownf094fea2011-10-04 22:05:47 +0100617static int regcache_default_cmp(const void *a, const void *b)
Dimitris Papastamosc08604b2011-10-03 10:50:14 +0100618{
619 const struct reg_default *_a = a;
620 const struct reg_default *_b = b;
621
622 return _a->reg - _b->reg;
623}
624
Mark Brownf094fea2011-10-04 22:05:47 +0100625int regcache_lookup_reg(struct regmap *map, unsigned int reg)
626{
627 struct reg_default key;
628 struct reg_default *r;
629
630 key.reg = reg;
631 key.def = 0;
632
633 r = bsearch(&key, map->reg_defaults, map->num_reg_defaults,
634 sizeof(struct reg_default), regcache_default_cmp);
635
636 if (r)
637 return r - map->reg_defaults;
638 else
Mark Brown6e6ace02011-10-09 13:23:31 +0100639 return -ENOENT;
Mark Brownf094fea2011-10-04 22:05:47 +0100640}
Mark Brownf8bd8222013-03-29 19:32:28 +0000641
Lars-Peter Clausen3f4ff562013-08-29 10:26:34 +0200642static bool regcache_reg_present(unsigned long *cache_present, unsigned int idx)
643{
644 if (!cache_present)
645 return true;
646
647 return test_bit(idx, cache_present);
648}
649
Mark Browncfdeb8c2013-03-29 20:12:21 +0000650static int regcache_sync_block_single(struct regmap *map, void *block,
Lars-Peter Clausen3f4ff562013-08-29 10:26:34 +0200651 unsigned long *cache_present,
Mark Browncfdeb8c2013-03-29 20:12:21 +0000652 unsigned int block_base,
653 unsigned int start, unsigned int end)
654{
655 unsigned int i, regtmp, val;
656 int ret;
657
658 for (i = start; i < end; i++) {
659 regtmp = block_base + (i * map->reg_stride);
660
Takashi Iwai4ceba982015-03-04 15:29:17 +0100661 if (!regcache_reg_present(cache_present, i) ||
662 !regmap_writeable(map, regtmp))
Mark Browncfdeb8c2013-03-29 20:12:21 +0000663 continue;
664
665 val = regcache_get_val(map, block, i);
Kevin Cernekee3969fa082015-05-05 15:14:13 -0700666 if (!regcache_reg_needs_sync(map, regtmp, val))
Mark Browncfdeb8c2013-03-29 20:12:21 +0000667 continue;
668
Viresh Kumar621a5f72015-09-26 15:04:07 -0700669 map->cache_bypass = true;
Mark Browncfdeb8c2013-03-29 20:12:21 +0000670
671 ret = _regmap_write(map, regtmp, val);
672
Viresh Kumar621a5f72015-09-26 15:04:07 -0700673 map->cache_bypass = false;
Jarkko Nikulaf29a4322014-09-16 14:04:14 +0300674 if (ret != 0) {
675 dev_err(map->dev, "Unable to sync register %#x. %d\n",
676 regtmp, ret);
Mark Browncfdeb8c2013-03-29 20:12:21 +0000677 return ret;
Jarkko Nikulaf29a4322014-09-16 14:04:14 +0300678 }
Mark Browncfdeb8c2013-03-29 20:12:21 +0000679 dev_dbg(map->dev, "Synced register %#x, value %#x\n",
680 regtmp, val);
681 }
682
683 return 0;
684}
685
Mark Brown75a5f892013-03-29 20:50:07 +0000686static int regcache_sync_block_raw_flush(struct regmap *map, const void **data,
687 unsigned int base, unsigned int cur)
688{
689 size_t val_bytes = map->format.val_bytes;
690 int ret, count;
691
692 if (*data == NULL)
693 return 0;
694
Dylan Reid78ba73e2014-01-24 15:40:39 -0800695 count = (cur - base) / map->reg_stride;
Mark Brown75a5f892013-03-29 20:50:07 +0000696
Stratos Karafotis96592932013-04-04 19:40:45 +0300697 dev_dbg(map->dev, "Writing %zu bytes for %d registers from 0x%x-0x%x\n",
Dylan Reid78ba73e2014-01-24 15:40:39 -0800698 count * val_bytes, count, base, cur - map->reg_stride);
Mark Brown75a5f892013-03-29 20:50:07 +0000699
Viresh Kumar621a5f72015-09-26 15:04:07 -0700700 map->cache_bypass = true;
Mark Brown75a5f892013-03-29 20:50:07 +0000701
Mark Brown0a819802013-10-09 12:28:52 +0100702 ret = _regmap_raw_write(map, base, *data, count * val_bytes);
Jarkko Nikulaf29a4322014-09-16 14:04:14 +0300703 if (ret)
704 dev_err(map->dev, "Unable to sync registers %#x-%#x. %d\n",
705 base, cur - map->reg_stride, ret);
Mark Brown75a5f892013-03-29 20:50:07 +0000706
Viresh Kumar621a5f72015-09-26 15:04:07 -0700707 map->cache_bypass = false;
Mark Brown75a5f892013-03-29 20:50:07 +0000708
709 *data = NULL;
710
711 return ret;
712}
713
Sachin Kamatf52687a2013-04-04 14:36:18 +0530714static int regcache_sync_block_raw(struct regmap *map, void *block,
Lars-Peter Clausen3f4ff562013-08-29 10:26:34 +0200715 unsigned long *cache_present,
Mark Browncfdeb8c2013-03-29 20:12:21 +0000716 unsigned int block_base, unsigned int start,
717 unsigned int end)
Mark Brownf8bd8222013-03-29 19:32:28 +0000718{
Mark Brown75a5f892013-03-29 20:50:07 +0000719 unsigned int i, val;
720 unsigned int regtmp = 0;
721 unsigned int base = 0;
722 const void *data = NULL;
Mark Brownf8bd8222013-03-29 19:32:28 +0000723 int ret;
724
725 for (i = start; i < end; i++) {
726 regtmp = block_base + (i * map->reg_stride);
727
Takashi Iwai4ceba982015-03-04 15:29:17 +0100728 if (!regcache_reg_present(cache_present, i) ||
729 !regmap_writeable(map, regtmp)) {
Mark Brown75a5f892013-03-29 20:50:07 +0000730 ret = regcache_sync_block_raw_flush(map, &data,
731 base, regtmp);
732 if (ret != 0)
733 return ret;
Mark Brownf8bd8222013-03-29 19:32:28 +0000734 continue;
Mark Brown75a5f892013-03-29 20:50:07 +0000735 }
Mark Brownf8bd8222013-03-29 19:32:28 +0000736
737 val = regcache_get_val(map, block, i);
Kevin Cernekee3969fa082015-05-05 15:14:13 -0700738 if (!regcache_reg_needs_sync(map, regtmp, val)) {
Mark Brown75a5f892013-03-29 20:50:07 +0000739 ret = regcache_sync_block_raw_flush(map, &data,
740 base, regtmp);
741 if (ret != 0)
742 return ret;
Mark Brownf8bd8222013-03-29 19:32:28 +0000743 continue;
Mark Brown75a5f892013-03-29 20:50:07 +0000744 }
Mark Brownf8bd8222013-03-29 19:32:28 +0000745
Mark Brown75a5f892013-03-29 20:50:07 +0000746 if (!data) {
747 data = regcache_get_val_addr(map, block, i);
748 base = regtmp;
749 }
Mark Brownf8bd8222013-03-29 19:32:28 +0000750 }
751
Lars-Peter Clausen2d49b592013-08-05 11:21:29 +0200752 return regcache_sync_block_raw_flush(map, &data, base, regtmp +
753 map->reg_stride);
Mark Brownf8bd8222013-03-29 19:32:28 +0000754}
Mark Browncfdeb8c2013-03-29 20:12:21 +0000755
756int regcache_sync_block(struct regmap *map, void *block,
Lars-Peter Clausen3f4ff562013-08-29 10:26:34 +0200757 unsigned long *cache_present,
Mark Browncfdeb8c2013-03-29 20:12:21 +0000758 unsigned int block_base, unsigned int start,
759 unsigned int end)
760{
Markus Pargmann67921a12015-08-21 10:26:42 +0200761 if (regmap_can_raw_write(map) && !map->use_single_write)
Lars-Peter Clausen3f4ff562013-08-29 10:26:34 +0200762 return regcache_sync_block_raw(map, block, cache_present,
763 block_base, start, end);
Mark Browncfdeb8c2013-03-29 20:12:21 +0000764 else
Lars-Peter Clausen3f4ff562013-08-29 10:26:34 +0200765 return regcache_sync_block_single(map, block, cache_present,
766 block_base, start, end);
Mark Browncfdeb8c2013-03-29 20:12:21 +0000767}