blob: b0a0dcf32fb7d05a0abdf725c158c47453652e34 [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;
Mark Brown3245d462016-02-02 10:16:51 -020033 unsigned int reg, val;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010034 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++)
Maarten ter Huurneb2c7f5d2016-07-29 23:42:12 +020041 if (regmap_readable(map, i * map->reg_stride) &&
42 !regmap_volatile(map, i * map->reg_stride))
Xiubo Lifb700672014-10-09 17:02:57 +080043 count++;
44
Maarten ter Huurneb2c7f5d2016-07-29 23:42:12 +020045 /* all registers are unreadable or volatile, so just bypass */
Xiubo Lifb700672014-10-09 17:02:57 +080046 if (!count) {
47 map->cache_bypass = true;
48 return 0;
49 }
50
51 map->num_reg_defaults = count;
52 map->reg_defaults = kmalloc_array(count, sizeof(struct reg_default),
53 GFP_KERNEL);
54 if (!map->reg_defaults)
55 return -ENOMEM;
56
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010057 if (!map->reg_defaults_raw) {
Viresh Kumar621a5f72015-09-26 15:04:07 -070058 bool cache_bypass = map->cache_bypass;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010059 dev_warn(map->dev, "No cache defaults, reading back from HW\n");
Laxman Dewangandf00c792012-02-17 18:57:26 +053060
Maciej S. Szmigierod51fe1f2016-01-13 22:41:12 +010061 /* Bypass the cache access till data read from HW */
Viresh Kumar621a5f72015-09-26 15:04:07 -070062 map->cache_bypass = true;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010063 tmp_buf = kmalloc(map->cache_size_raw, GFP_KERNEL);
Xiubo Lifb700672014-10-09 17:02:57 +080064 if (!tmp_buf) {
65 ret = -ENOMEM;
66 goto err_free;
67 }
Mark Browneb4cb762013-02-21 18:39:47 +000068 ret = regmap_raw_read(map, 0, tmp_buf,
Maciej S. Szmigierod51fe1f2016-01-13 22:41:12 +010069 map->cache_size_raw);
Laxman Dewangandf00c792012-02-17 18:57:26 +053070 map->cache_bypass = cache_bypass;
Mark Brown3245d462016-02-02 10:16:51 -020071 if (ret == 0) {
72 map->reg_defaults_raw = tmp_buf;
73 map->cache_free = 1;
74 } else {
75 kfree(tmp_buf);
76 }
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010077 }
78
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010079 /* fill the reg_defaults */
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010080 for (i = 0, j = 0; i < map->num_reg_defaults_raw; i++) {
Mark Brown3245d462016-02-02 10:16:51 -020081 reg = i * map->reg_stride;
82
83 if (!regmap_readable(map, reg))
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010084 continue;
Mark Brown3245d462016-02-02 10:16:51 -020085
86 if (regmap_volatile(map, reg))
87 continue;
88
89 if (map->reg_defaults_raw) {
90 val = regcache_get_val(map, map->reg_defaults_raw, i);
91 } else {
92 bool cache_bypass = map->cache_bypass;
93
94 map->cache_bypass = true;
95 ret = regmap_read(map, reg, &val);
96 map->cache_bypass = cache_bypass;
97 if (ret != 0) {
98 dev_err(map->dev, "Failed to read %d: %d\n",
99 reg, ret);
100 goto err_free;
101 }
102 }
103
104 map->reg_defaults[j].reg = reg;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100105 map->reg_defaults[j].def = val;
106 j++;
107 }
108
109 return 0;
Lars-Peter Clausen021cd612011-11-14 10:40:16 +0100110
111err_free:
Xiubo Lifb700672014-10-09 17:02:57 +0800112 kfree(map->reg_defaults);
Lars-Peter Clausen021cd612011-11-14 10:40:16 +0100113
114 return ret;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100115}
116
Lars-Peter Clausene5e3b8a2011-11-16 16:28:16 +0100117int regcache_init(struct regmap *map, const struct regmap_config *config)
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100118{
119 int ret;
120 int i;
121 void *tmp_buf;
122
Mark Browne7a6db32011-09-19 16:08:03 +0100123 if (map->cache_type == REGCACHE_NONE) {
Xiubo Li8cfe2fd2015-12-11 11:23:19 +0800124 if (config->reg_defaults || config->num_reg_defaults_raw)
125 dev_warn(map->dev,
126 "No cache used with register defaults set!\n");
127
Mark Browne7a6db32011-09-19 16:08:03 +0100128 map->cache_bypass = true;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100129 return 0;
Mark Browne7a6db32011-09-19 16:08:03 +0100130 }
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100131
Xiubo Li167f7062015-12-11 11:23:20 +0800132 if (config->reg_defaults && !config->num_reg_defaults) {
133 dev_err(map->dev,
134 "Register defaults are set without the number!\n");
135 return -EINVAL;
136 }
137
Xiubo Li8cfe2fd2015-12-11 11:23:19 +0800138 for (i = 0; i < config->num_reg_defaults; i++)
139 if (config->reg_defaults[i].reg % map->reg_stride)
140 return -EINVAL;
141
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100142 for (i = 0; i < ARRAY_SIZE(cache_types); i++)
143 if (cache_types[i]->type == map->cache_type)
144 break;
145
146 if (i == ARRAY_SIZE(cache_types)) {
147 dev_err(map->dev, "Could not match compress type: %d\n",
148 map->cache_type);
149 return -EINVAL;
150 }
151
Lars-Peter Clausene5e3b8a2011-11-16 16:28:16 +0100152 map->num_reg_defaults = config->num_reg_defaults;
153 map->num_reg_defaults_raw = config->num_reg_defaults_raw;
154 map->reg_defaults_raw = config->reg_defaults_raw;
Lars-Peter Clausen064d4db2011-11-16 20:34:03 +0100155 map->cache_word_size = DIV_ROUND_UP(config->val_bits, 8);
156 map->cache_size_raw = map->cache_word_size * config->num_reg_defaults_raw;
Lars-Peter Clausene5e3b8a2011-11-16 16:28:16 +0100157
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100158 map->cache = NULL;
159 map->cache_ops = cache_types[i];
160
161 if (!map->cache_ops->read ||
162 !map->cache_ops->write ||
163 !map->cache_ops->name)
164 return -EINVAL;
165
166 /* We still need to ensure that the reg_defaults
167 * won't vanish from under us. We'll need to make
168 * a copy of it.
169 */
Lars-Peter Clausen720e4612011-11-16 16:28:17 +0100170 if (config->reg_defaults) {
Lars-Peter Clausen720e4612011-11-16 16:28:17 +0100171 tmp_buf = kmemdup(config->reg_defaults, map->num_reg_defaults *
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100172 sizeof(struct reg_default), GFP_KERNEL);
173 if (!tmp_buf)
174 return -ENOMEM;
175 map->reg_defaults = tmp_buf;
Mark Brown8528bdd2011-10-09 13:13:58 +0100176 } else if (map->num_reg_defaults_raw) {
Mark Brown5fcd2562011-09-29 15:24:54 +0100177 /* Some devices such as PMICs don't have cache defaults,
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100178 * we cope with this by reading back the HW registers and
179 * crafting the cache defaults by hand.
180 */
181 ret = regcache_hw_init(map);
182 if (ret < 0)
183 return ret;
Xiubo Lifb700672014-10-09 17:02:57 +0800184 if (map->cache_bypass)
185 return 0;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100186 }
187
188 if (!map->max_register)
189 map->max_register = map->num_reg_defaults_raw;
190
191 if (map->cache_ops->init) {
192 dev_dbg(map->dev, "Initializing %s cache\n",
193 map->cache_ops->name);
Lars-Peter Clausenbd061c72011-11-14 10:40:17 +0100194 ret = map->cache_ops->init(map);
195 if (ret)
196 goto err_free;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100197 }
198 return 0;
Lars-Peter Clausenbd061c72011-11-14 10:40:17 +0100199
200err_free:
201 kfree(map->reg_defaults);
202 if (map->cache_free)
203 kfree(map->reg_defaults_raw);
204
205 return ret;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100206}
207
208void regcache_exit(struct regmap *map)
209{
210 if (map->cache_type == REGCACHE_NONE)
211 return;
212
213 BUG_ON(!map->cache_ops);
214
215 kfree(map->reg_defaults);
216 if (map->cache_free)
217 kfree(map->reg_defaults_raw);
218
219 if (map->cache_ops->exit) {
220 dev_dbg(map->dev, "Destroying %s cache\n",
221 map->cache_ops->name);
222 map->cache_ops->exit(map);
223 }
224}
225
226/**
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000227 * regcache_read - Fetch the value of a given register from the cache.
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100228 *
229 * @map: map to configure.
230 * @reg: The register index.
231 * @value: The value to be returned.
232 *
233 * Return a negative value on failure, 0 on success.
234 */
235int regcache_read(struct regmap *map,
236 unsigned int reg, unsigned int *value)
237{
Mark Brownbc7ee552011-11-30 14:27:08 +0000238 int ret;
239
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100240 if (map->cache_type == REGCACHE_NONE)
241 return -ENOSYS;
242
243 BUG_ON(!map->cache_ops);
244
Mark Brownbc7ee552011-11-30 14:27:08 +0000245 if (!regmap_volatile(map, reg)) {
246 ret = map->cache_ops->read(map, reg, value);
247
248 if (ret == 0)
Philipp Zabelc6b570d2015-03-09 12:20:13 +0100249 trace_regmap_reg_read_cache(map, reg, *value);
Mark Brownbc7ee552011-11-30 14:27:08 +0000250
251 return ret;
252 }
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100253
254 return -EINVAL;
255}
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100256
257/**
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000258 * regcache_write - Set the value of a given register in the cache.
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100259 *
260 * @map: map to configure.
261 * @reg: The register index.
262 * @value: The new register value.
263 *
264 * Return a negative value on failure, 0 on success.
265 */
266int regcache_write(struct regmap *map,
267 unsigned int reg, unsigned int value)
268{
269 if (map->cache_type == REGCACHE_NONE)
270 return 0;
271
272 BUG_ON(!map->cache_ops);
273
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100274 if (!regmap_volatile(map, reg))
275 return map->cache_ops->write(map, reg, value);
276
277 return 0;
278}
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100279
Kevin Cernekee3969fa082015-05-05 15:14:13 -0700280static bool regcache_reg_needs_sync(struct regmap *map, unsigned int reg,
281 unsigned int val)
282{
283 int ret;
284
Kevin Cernekee1c797712015-05-05 15:14:14 -0700285 /* If we don't know the chip just got reset, then sync everything. */
286 if (!map->no_sync_defaults)
287 return true;
288
Kevin Cernekee3969fa082015-05-05 15:14:13 -0700289 /* Is this the hardware default? If so skip. */
290 ret = regcache_lookup_reg(map, reg);
291 if (ret >= 0 && val == map->reg_defaults[ret].def)
292 return false;
293 return true;
294}
295
Maarten ter Huurned856fce2013-06-03 00:15:26 +0200296static int regcache_default_sync(struct regmap *map, unsigned int min,
297 unsigned int max)
298{
299 unsigned int reg;
300
Dylan Reid75617322014-03-18 13:45:08 -0700301 for (reg = min; reg <= max; reg += map->reg_stride) {
Maarten ter Huurned856fce2013-06-03 00:15:26 +0200302 unsigned int val;
303 int ret;
304
Dylan Reid83f84752014-03-18 13:45:09 -0700305 if (regmap_volatile(map, reg) ||
306 !regmap_writeable(map, reg))
Maarten ter Huurned856fce2013-06-03 00:15:26 +0200307 continue;
308
309 ret = regcache_read(map, reg, &val);
310 if (ret)
311 return ret;
312
Kevin Cernekee3969fa082015-05-05 15:14:13 -0700313 if (!regcache_reg_needs_sync(map, reg, val))
Maarten ter Huurned856fce2013-06-03 00:15:26 +0200314 continue;
315
Viresh Kumar621a5f72015-09-26 15:04:07 -0700316 map->cache_bypass = true;
Maarten ter Huurned856fce2013-06-03 00:15:26 +0200317 ret = _regmap_write(map, reg, val);
Viresh Kumar621a5f72015-09-26 15:04:07 -0700318 map->cache_bypass = false;
Jarkko Nikulaf29a4322014-09-16 14:04:14 +0300319 if (ret) {
320 dev_err(map->dev, "Unable to sync register %#x. %d\n",
321 reg, ret);
Maarten ter Huurned856fce2013-06-03 00:15:26 +0200322 return ret;
Jarkko Nikulaf29a4322014-09-16 14:04:14 +0300323 }
Maarten ter Huurned856fce2013-06-03 00:15:26 +0200324 dev_dbg(map->dev, "Synced register %#x, value %#x\n", reg, val);
325 }
326
327 return 0;
328}
329
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100330/**
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000331 * regcache_sync - Sync the register cache with the hardware.
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100332 *
333 * @map: map to configure.
334 *
335 * Any registers that should not be synced should be marked as
336 * volatile. In general drivers can choose not to use the provided
337 * syncing functionality if they so require.
338 *
339 * Return a negative value on failure, 0 on success.
340 */
341int regcache_sync(struct regmap *map)
342{
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100343 int ret = 0;
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100344 unsigned int i;
Dimitris Papastamos59360082011-09-19 14:34:04 +0100345 const char *name;
Viresh Kumar621a5f72015-09-26 15:04:07 -0700346 bool bypass;
Dimitris Papastamos59360082011-09-19 14:34:04 +0100347
Maarten ter Huurned856fce2013-06-03 00:15:26 +0200348 BUG_ON(!map->cache_ops);
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100349
Lars-Peter Clausen81485f52013-05-23 15:06:15 +0200350 map->lock(map->lock_arg);
Dimitris Papastamosbeb1a102011-09-29 14:36:26 +0100351 /* Remember the initial bypass state */
352 bypass = map->cache_bypass;
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100353 dev_dbg(map->dev, "Syncing %s cache\n",
354 map->cache_ops->name);
355 name = map->cache_ops->name;
Philipp Zabelc6b570d2015-03-09 12:20:13 +0100356 trace_regcache_sync(map, name, "start");
Mark Brown22f0d902012-01-21 12:01:14 +0000357
Mark Brown8ae0d7e2011-10-26 10:34:22 +0200358 if (!map->cache_dirty)
359 goto out;
Mark Brownd9db7622012-01-25 21:06:33 +0000360
Mark Brownaffbe882013-10-10 21:06:32 +0100361 map->async = true;
362
Mark Brown22f0d902012-01-21 12:01:14 +0000363 /* Apply any patch first */
Viresh Kumar621a5f72015-09-26 15:04:07 -0700364 map->cache_bypass = true;
Mark Brown22f0d902012-01-21 12:01:14 +0000365 for (i = 0; i < map->patch_regs; i++) {
366 ret = _regmap_write(map, map->patch[i].reg, map->patch[i].def);
367 if (ret != 0) {
368 dev_err(map->dev, "Failed to write %x = %x: %d\n",
369 map->patch[i].reg, map->patch[i].def, ret);
370 goto out;
371 }
372 }
Viresh Kumar621a5f72015-09-26 15:04:07 -0700373 map->cache_bypass = false;
Mark Brown22f0d902012-01-21 12:01:14 +0000374
Maarten ter Huurned856fce2013-06-03 00:15:26 +0200375 if (map->cache_ops->sync)
376 ret = map->cache_ops->sync(map, 0, map->max_register);
377 else
378 ret = regcache_default_sync(map, 0, map->max_register);
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100379
Mark Brown6ff73732012-02-23 22:05:59 +0000380 if (ret == 0)
381 map->cache_dirty = false;
382
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100383out:
Dimitris Papastamosbeb1a102011-09-29 14:36:26 +0100384 /* Restore the bypass state */
Mark Brownaffbe882013-10-10 21:06:32 +0100385 map->async = false;
Dimitris Papastamosbeb1a102011-09-29 14:36:26 +0100386 map->cache_bypass = bypass;
Kevin Cernekee1c797712015-05-05 15:14:14 -0700387 map->no_sync_defaults = false;
Lars-Peter Clausen81485f52013-05-23 15:06:15 +0200388 map->unlock(map->lock_arg);
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100389
Mark Brownaffbe882013-10-10 21:06:32 +0100390 regmap_async_complete(map);
391
Philipp Zabelc6b570d2015-03-09 12:20:13 +0100392 trace_regcache_sync(map, name, "stop");
Mark Brownaffbe882013-10-10 21:06:32 +0100393
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100394 return ret;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100395}
396EXPORT_SYMBOL_GPL(regcache_sync);
397
Mark Brown92afb282011-09-19 18:22:14 +0100398/**
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000399 * regcache_sync_region - Sync part of the register cache with the hardware.
Mark Brown4d4cfd12012-02-23 20:53:37 +0000400 *
401 * @map: map to sync.
402 * @min: first register to sync
403 * @max: last register to sync
404 *
405 * Write all non-default register values in the specified region to
406 * the hardware.
407 *
408 * Return a negative value on failure, 0 on success.
409 */
410int regcache_sync_region(struct regmap *map, unsigned int min,
411 unsigned int max)
412{
413 int ret = 0;
414 const char *name;
Viresh Kumar621a5f72015-09-26 15:04:07 -0700415 bool bypass;
Mark Brown4d4cfd12012-02-23 20:53:37 +0000416
Maarten ter Huurned856fce2013-06-03 00:15:26 +0200417 BUG_ON(!map->cache_ops);
Mark Brown4d4cfd12012-02-23 20:53:37 +0000418
Lars-Peter Clausen81485f52013-05-23 15:06:15 +0200419 map->lock(map->lock_arg);
Mark Brown4d4cfd12012-02-23 20:53:37 +0000420
421 /* Remember the initial bypass state */
422 bypass = map->cache_bypass;
423
424 name = map->cache_ops->name;
425 dev_dbg(map->dev, "Syncing %s cache from %d-%d\n", name, min, max);
426
Philipp Zabelc6b570d2015-03-09 12:20:13 +0100427 trace_regcache_sync(map, name, "start region");
Mark Brown4d4cfd12012-02-23 20:53:37 +0000428
429 if (!map->cache_dirty)
430 goto out;
431
Mark Brownaffbe882013-10-10 21:06:32 +0100432 map->async = true;
433
Maarten ter Huurned856fce2013-06-03 00:15:26 +0200434 if (map->cache_ops->sync)
435 ret = map->cache_ops->sync(map, min, max);
436 else
437 ret = regcache_default_sync(map, min, max);
Mark Brown4d4cfd12012-02-23 20:53:37 +0000438
439out:
Mark Brown4d4cfd12012-02-23 20:53:37 +0000440 /* Restore the bypass state */
441 map->cache_bypass = bypass;
Mark Brownaffbe882013-10-10 21:06:32 +0100442 map->async = false;
Kevin Cernekee1c797712015-05-05 15:14:14 -0700443 map->no_sync_defaults = false;
Lars-Peter Clausen81485f52013-05-23 15:06:15 +0200444 map->unlock(map->lock_arg);
Mark Brown4d4cfd12012-02-23 20:53:37 +0000445
Mark Brownaffbe882013-10-10 21:06:32 +0100446 regmap_async_complete(map);
447
Philipp Zabelc6b570d2015-03-09 12:20:13 +0100448 trace_regcache_sync(map, name, "stop region");
Mark Brownaffbe882013-10-10 21:06:32 +0100449
Mark Brown4d4cfd12012-02-23 20:53:37 +0000450 return ret;
451}
Mark Browne466de02012-04-03 13:08:53 +0100452EXPORT_SYMBOL_GPL(regcache_sync_region);
Mark Brown4d4cfd12012-02-23 20:53:37 +0000453
454/**
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000455 * regcache_drop_region - Discard part of the register cache
Mark Brown697e85b2013-05-08 13:55:22 +0100456 *
457 * @map: map to operate on
458 * @min: first register to discard
459 * @max: last register to discard
460 *
461 * Discard part of the register cache.
462 *
463 * Return a negative value on failure, 0 on success.
464 */
465int regcache_drop_region(struct regmap *map, unsigned int min,
466 unsigned int max)
467{
Mark Brown697e85b2013-05-08 13:55:22 +0100468 int ret = 0;
469
Lars-Peter Clausen3f4ff562013-08-29 10:26:34 +0200470 if (!map->cache_ops || !map->cache_ops->drop)
Mark Brown697e85b2013-05-08 13:55:22 +0100471 return -EINVAL;
472
Lars-Peter Clausen81485f52013-05-23 15:06:15 +0200473 map->lock(map->lock_arg);
Mark Brown697e85b2013-05-08 13:55:22 +0100474
Philipp Zabelc6b570d2015-03-09 12:20:13 +0100475 trace_regcache_drop_region(map, min, max);
Mark Brown697e85b2013-05-08 13:55:22 +0100476
Lars-Peter Clausen3f4ff562013-08-29 10:26:34 +0200477 ret = map->cache_ops->drop(map, min, max);
Mark Brown697e85b2013-05-08 13:55:22 +0100478
Lars-Peter Clausen81485f52013-05-23 15:06:15 +0200479 map->unlock(map->lock_arg);
Mark Brown697e85b2013-05-08 13:55:22 +0100480
481 return ret;
482}
483EXPORT_SYMBOL_GPL(regcache_drop_region);
484
485/**
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000486 * regcache_cache_only - Put a register map into cache only mode
Mark Brown92afb282011-09-19 18:22:14 +0100487 *
488 * @map: map to configure
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000489 * @enable: flag if changes should be written to the hardware
Mark Brown92afb282011-09-19 18:22:14 +0100490 *
491 * When a register map is marked as cache only writes to the register
492 * map API will only update the register cache, they will not cause
493 * any hardware changes. This is useful for allowing portions of
494 * drivers to act as though the device were functioning as normal when
495 * it is disabled for power saving reasons.
496 */
497void regcache_cache_only(struct regmap *map, bool enable)
498{
Lars-Peter Clausen81485f52013-05-23 15:06:15 +0200499 map->lock(map->lock_arg);
Dimitris Papastamosac77a762011-09-29 14:36:28 +0100500 WARN_ON(map->cache_bypass && enable);
Mark Brown92afb282011-09-19 18:22:14 +0100501 map->cache_only = enable;
Philipp Zabelc6b570d2015-03-09 12:20:13 +0100502 trace_regmap_cache_only(map, enable);
Lars-Peter Clausen81485f52013-05-23 15:06:15 +0200503 map->unlock(map->lock_arg);
Mark Brown92afb282011-09-19 18:22:14 +0100504}
505EXPORT_SYMBOL_GPL(regcache_cache_only);
506
Dimitris Papastamos6eb0f5e2011-09-29 14:36:27 +0100507/**
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000508 * regcache_mark_dirty - Indicate that HW registers were reset to default values
Mark Brown8ae0d7e2011-10-26 10:34:22 +0200509 *
510 * @map: map to mark
511 *
Kevin Cernekee1c797712015-05-05 15:14:14 -0700512 * Inform regcache that the device has been powered down or reset, so that
513 * on resume, regcache_sync() knows to write out all non-default values
514 * stored in the cache.
515 *
516 * If this function is not called, regcache_sync() will assume that
517 * the hardware state still matches the cache state, modulo any writes that
518 * happened when cache_only was true.
Mark Brown8ae0d7e2011-10-26 10:34:22 +0200519 */
520void regcache_mark_dirty(struct regmap *map)
521{
Lars-Peter Clausen81485f52013-05-23 15:06:15 +0200522 map->lock(map->lock_arg);
Mark Brown8ae0d7e2011-10-26 10:34:22 +0200523 map->cache_dirty = true;
Kevin Cernekee1c797712015-05-05 15:14:14 -0700524 map->no_sync_defaults = true;
Lars-Peter Clausen81485f52013-05-23 15:06:15 +0200525 map->unlock(map->lock_arg);
Mark Brown8ae0d7e2011-10-26 10:34:22 +0200526}
527EXPORT_SYMBOL_GPL(regcache_mark_dirty);
528
529/**
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000530 * regcache_cache_bypass - Put a register map into cache bypass mode
Dimitris Papastamos6eb0f5e2011-09-29 14:36:27 +0100531 *
532 * @map: map to configure
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000533 * @enable: flag if changes should not be written to the cache
Dimitris Papastamos6eb0f5e2011-09-29 14:36:27 +0100534 *
535 * When a register map is marked with the cache bypass option, writes
536 * to the register map API will only update the hardware and not the
537 * the cache directly. This is useful when syncing the cache back to
538 * the hardware.
539 */
540void regcache_cache_bypass(struct regmap *map, bool enable)
541{
Lars-Peter Clausen81485f52013-05-23 15:06:15 +0200542 map->lock(map->lock_arg);
Dimitris Papastamosac77a762011-09-29 14:36:28 +0100543 WARN_ON(map->cache_only && enable);
Dimitris Papastamos6eb0f5e2011-09-29 14:36:27 +0100544 map->cache_bypass = enable;
Philipp Zabelc6b570d2015-03-09 12:20:13 +0100545 trace_regmap_cache_bypass(map, enable);
Lars-Peter Clausen81485f52013-05-23 15:06:15 +0200546 map->unlock(map->lock_arg);
Dimitris Papastamos6eb0f5e2011-09-29 14:36:27 +0100547}
548EXPORT_SYMBOL_GPL(regcache_cache_bypass);
549
Mark Brown879082c2013-02-21 18:03:13 +0000550bool regcache_set_val(struct regmap *map, void *base, unsigned int idx,
551 unsigned int val)
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100552{
Mark Brown325acab2013-02-21 18:07:01 +0000553 if (regcache_get_val(map, base, idx) == val)
554 return true;
555
Mark Browneb4cb762013-02-21 18:39:47 +0000556 /* Use device native format if possible */
557 if (map->format.format_val) {
558 map->format.format_val(base + (map->cache_word_size * idx),
559 val, 0);
560 return false;
561 }
562
Mark Brown879082c2013-02-21 18:03:13 +0000563 switch (map->cache_word_size) {
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100564 case 1: {
565 u8 *cache = base;
Xiubo Li2fd69022015-12-09 13:09:06 +0800566
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100567 cache[idx] = val;
568 break;
569 }
570 case 2: {
571 u16 *cache = base;
Xiubo Li2fd69022015-12-09 13:09:06 +0800572
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100573 cache[idx] = val;
574 break;
575 }
Mark Brown7d5e5252012-02-17 15:58:25 -0800576 case 4: {
577 u32 *cache = base;
Xiubo Li2fd69022015-12-09 13:09:06 +0800578
Mark Brown7d5e5252012-02-17 15:58:25 -0800579 cache[idx] = val;
580 break;
581 }
Xiubo Li8b7663d2015-12-09 13:09:07 +0800582#ifdef CONFIG_64BIT
583 case 8: {
584 u64 *cache = base;
585
586 cache[idx] = val;
587 break;
588 }
589#endif
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100590 default:
591 BUG();
592 }
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100593 return false;
594}
595
Mark Brown879082c2013-02-21 18:03:13 +0000596unsigned int regcache_get_val(struct regmap *map, const void *base,
597 unsigned int idx)
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100598{
599 if (!base)
600 return -EINVAL;
601
Mark Browneb4cb762013-02-21 18:39:47 +0000602 /* Use device native format if possible */
603 if (map->format.parse_val)
Mark Brown88177962013-03-13 19:29:36 +0000604 return map->format.parse_val(regcache_get_val_addr(map, base,
605 idx));
Mark Browneb4cb762013-02-21 18:39:47 +0000606
Mark Brown879082c2013-02-21 18:03:13 +0000607 switch (map->cache_word_size) {
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100608 case 1: {
609 const u8 *cache = base;
Xiubo Li2fd69022015-12-09 13:09:06 +0800610
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100611 return cache[idx];
612 }
613 case 2: {
614 const u16 *cache = base;
Xiubo Li2fd69022015-12-09 13:09:06 +0800615
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100616 return cache[idx];
617 }
Mark Brown7d5e5252012-02-17 15:58:25 -0800618 case 4: {
619 const u32 *cache = base;
Xiubo Li2fd69022015-12-09 13:09:06 +0800620
Mark Brown7d5e5252012-02-17 15:58:25 -0800621 return cache[idx];
622 }
Xiubo Li8b7663d2015-12-09 13:09:07 +0800623#ifdef CONFIG_64BIT
624 case 8: {
625 const u64 *cache = base;
626
627 return cache[idx];
628 }
629#endif
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100630 default:
631 BUG();
632 }
633 /* unreachable */
634 return -1;
635}
636
Mark Brownf094fea2011-10-04 22:05:47 +0100637static int regcache_default_cmp(const void *a, const void *b)
Dimitris Papastamosc08604b2011-10-03 10:50:14 +0100638{
639 const struct reg_default *_a = a;
640 const struct reg_default *_b = b;
641
642 return _a->reg - _b->reg;
643}
644
Mark Brownf094fea2011-10-04 22:05:47 +0100645int regcache_lookup_reg(struct regmap *map, unsigned int reg)
646{
647 struct reg_default key;
648 struct reg_default *r;
649
650 key.reg = reg;
651 key.def = 0;
652
653 r = bsearch(&key, map->reg_defaults, map->num_reg_defaults,
654 sizeof(struct reg_default), regcache_default_cmp);
655
656 if (r)
657 return r - map->reg_defaults;
658 else
Mark Brown6e6ace02011-10-09 13:23:31 +0100659 return -ENOENT;
Mark Brownf094fea2011-10-04 22:05:47 +0100660}
Mark Brownf8bd8222013-03-29 19:32:28 +0000661
Lars-Peter Clausen3f4ff562013-08-29 10:26:34 +0200662static bool regcache_reg_present(unsigned long *cache_present, unsigned int idx)
663{
664 if (!cache_present)
665 return true;
666
667 return test_bit(idx, cache_present);
668}
669
Mark Browncfdeb8c2013-03-29 20:12:21 +0000670static int regcache_sync_block_single(struct regmap *map, void *block,
Lars-Peter Clausen3f4ff562013-08-29 10:26:34 +0200671 unsigned long *cache_present,
Mark Browncfdeb8c2013-03-29 20:12:21 +0000672 unsigned int block_base,
673 unsigned int start, unsigned int end)
674{
675 unsigned int i, regtmp, val;
676 int ret;
677
678 for (i = start; i < end; i++) {
679 regtmp = block_base + (i * map->reg_stride);
680
Takashi Iwai4ceba982015-03-04 15:29:17 +0100681 if (!regcache_reg_present(cache_present, i) ||
682 !regmap_writeable(map, regtmp))
Mark Browncfdeb8c2013-03-29 20:12:21 +0000683 continue;
684
685 val = regcache_get_val(map, block, i);
Kevin Cernekee3969fa082015-05-05 15:14:13 -0700686 if (!regcache_reg_needs_sync(map, regtmp, val))
Mark Browncfdeb8c2013-03-29 20:12:21 +0000687 continue;
688
Viresh Kumar621a5f72015-09-26 15:04:07 -0700689 map->cache_bypass = true;
Mark Browncfdeb8c2013-03-29 20:12:21 +0000690
691 ret = _regmap_write(map, regtmp, val);
692
Viresh Kumar621a5f72015-09-26 15:04:07 -0700693 map->cache_bypass = false;
Jarkko Nikulaf29a4322014-09-16 14:04:14 +0300694 if (ret != 0) {
695 dev_err(map->dev, "Unable to sync register %#x. %d\n",
696 regtmp, ret);
Mark Browncfdeb8c2013-03-29 20:12:21 +0000697 return ret;
Jarkko Nikulaf29a4322014-09-16 14:04:14 +0300698 }
Mark Browncfdeb8c2013-03-29 20:12:21 +0000699 dev_dbg(map->dev, "Synced register %#x, value %#x\n",
700 regtmp, val);
701 }
702
703 return 0;
704}
705
Mark Brown75a5f892013-03-29 20:50:07 +0000706static int regcache_sync_block_raw_flush(struct regmap *map, const void **data,
707 unsigned int base, unsigned int cur)
708{
709 size_t val_bytes = map->format.val_bytes;
710 int ret, count;
711
712 if (*data == NULL)
713 return 0;
714
Dylan Reid78ba73e2014-01-24 15:40:39 -0800715 count = (cur - base) / map->reg_stride;
Mark Brown75a5f892013-03-29 20:50:07 +0000716
Stratos Karafotis96592932013-04-04 19:40:45 +0300717 dev_dbg(map->dev, "Writing %zu bytes for %d registers from 0x%x-0x%x\n",
Dylan Reid78ba73e2014-01-24 15:40:39 -0800718 count * val_bytes, count, base, cur - map->reg_stride);
Mark Brown75a5f892013-03-29 20:50:07 +0000719
Viresh Kumar621a5f72015-09-26 15:04:07 -0700720 map->cache_bypass = true;
Mark Brown75a5f892013-03-29 20:50:07 +0000721
Mark Brown0a819802013-10-09 12:28:52 +0100722 ret = _regmap_raw_write(map, base, *data, count * val_bytes);
Jarkko Nikulaf29a4322014-09-16 14:04:14 +0300723 if (ret)
724 dev_err(map->dev, "Unable to sync registers %#x-%#x. %d\n",
725 base, cur - map->reg_stride, ret);
Mark Brown75a5f892013-03-29 20:50:07 +0000726
Viresh Kumar621a5f72015-09-26 15:04:07 -0700727 map->cache_bypass = false;
Mark Brown75a5f892013-03-29 20:50:07 +0000728
729 *data = NULL;
730
731 return ret;
732}
733
Sachin Kamatf52687a2013-04-04 14:36:18 +0530734static int regcache_sync_block_raw(struct regmap *map, void *block,
Lars-Peter Clausen3f4ff562013-08-29 10:26:34 +0200735 unsigned long *cache_present,
Mark Browncfdeb8c2013-03-29 20:12:21 +0000736 unsigned int block_base, unsigned int start,
737 unsigned int end)
Mark Brownf8bd8222013-03-29 19:32:28 +0000738{
Mark Brown75a5f892013-03-29 20:50:07 +0000739 unsigned int i, val;
740 unsigned int regtmp = 0;
741 unsigned int base = 0;
742 const void *data = NULL;
Mark Brownf8bd8222013-03-29 19:32:28 +0000743 int ret;
744
745 for (i = start; i < end; i++) {
746 regtmp = block_base + (i * map->reg_stride);
747
Takashi Iwai4ceba982015-03-04 15:29:17 +0100748 if (!regcache_reg_present(cache_present, i) ||
749 !regmap_writeable(map, regtmp)) {
Mark Brown75a5f892013-03-29 20:50:07 +0000750 ret = regcache_sync_block_raw_flush(map, &data,
751 base, regtmp);
752 if (ret != 0)
753 return ret;
Mark Brownf8bd8222013-03-29 19:32:28 +0000754 continue;
Mark Brown75a5f892013-03-29 20:50:07 +0000755 }
Mark Brownf8bd8222013-03-29 19:32:28 +0000756
757 val = regcache_get_val(map, block, i);
Kevin Cernekee3969fa082015-05-05 15:14:13 -0700758 if (!regcache_reg_needs_sync(map, regtmp, val)) {
Mark Brown75a5f892013-03-29 20:50:07 +0000759 ret = regcache_sync_block_raw_flush(map, &data,
760 base, regtmp);
761 if (ret != 0)
762 return ret;
Mark Brownf8bd8222013-03-29 19:32:28 +0000763 continue;
Mark Brown75a5f892013-03-29 20:50:07 +0000764 }
Mark Brownf8bd8222013-03-29 19:32:28 +0000765
Mark Brown75a5f892013-03-29 20:50:07 +0000766 if (!data) {
767 data = regcache_get_val_addr(map, block, i);
768 base = regtmp;
769 }
Mark Brownf8bd8222013-03-29 19:32:28 +0000770 }
771
Lars-Peter Clausen2d49b592013-08-05 11:21:29 +0200772 return regcache_sync_block_raw_flush(map, &data, base, regtmp +
773 map->reg_stride);
Mark Brownf8bd8222013-03-29 19:32:28 +0000774}
Mark Browncfdeb8c2013-03-29 20:12:21 +0000775
776int regcache_sync_block(struct regmap *map, void *block,
Lars-Peter Clausen3f4ff562013-08-29 10:26:34 +0200777 unsigned long *cache_present,
Mark Browncfdeb8c2013-03-29 20:12:21 +0000778 unsigned int block_base, unsigned int start,
779 unsigned int end)
780{
Markus Pargmann67921a12015-08-21 10:26:42 +0200781 if (regmap_can_raw_write(map) && !map->use_single_write)
Lars-Peter Clausen3f4ff562013-08-29 10:26:34 +0200782 return regcache_sync_block_raw(map, block, cache_present,
783 block_base, start, end);
Mark Browncfdeb8c2013-03-29 20:12:21 +0000784 else
Lars-Peter Clausen3f4ff562013-08-29 10:26:34 +0200785 return regcache_sync_block_single(map, block, cache_present,
786 block_base, start, end);
Mark Browncfdeb8c2013-03-29 20:12:21 +0000787}