blob: f2469d3435ca384db5f014c031ca3bd2e4687dd9 [file] [log] [blame]
Greg Kroah-Hartman37613fa2019-04-25 20:06:18 +02001// SPDX-License-Identifier: GPL-2.0
2//
3// Register cache access API
4//
5// Copyright 2011 Wolfson Microelectronics plc
6//
7// Author: Dimitris Papastamos <dp@opensource.wolfsonmicro.com>
Dimitris Papastamos9fabe242011-09-19 14:34:00 +01008
Mark Brownf094fea2011-10-04 22:05:47 +01009#include <linux/bsearch.h>
Xiubo Lie39be3a2014-10-09 17:02:52 +080010#include <linux/device.h>
11#include <linux/export.h>
12#include <linux/slab.h>
Dimitris Papastamosc08604b2011-10-03 10:50:14 +010013#include <linux/sort.h>
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010014
Steven Rostedtf58078d2015-03-19 17:50:47 -040015#include "trace.h"
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010016#include "internal.h"
17
18static const struct regcache_ops *cache_types[] = {
Dimitris Papastamos28644c802011-09-19 14:34:02 +010019 &regcache_rbtree_ops,
Mark Brownf458e612017-06-08 15:43:19 +010020#if IS_ENABLED(CONFIG_REGCACHE_COMPRESSED)
Dimitris Papastamos2cbbb572011-09-19 14:34:03 +010021 &regcache_lzo_ops,
Jonas Gorski34a730aa2017-06-02 15:15:37 +020022#endif
Mark Brown2ac902c2012-12-19 14:51:55 +000023 &regcache_flat_ops,
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010024};
25
26static int regcache_hw_init(struct regmap *map)
27{
28 int i, j;
29 int ret;
30 int count;
Mark Brown3245d462016-02-02 10:16:51 -020031 unsigned int reg, val;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010032 void *tmp_buf;
33
34 if (!map->num_reg_defaults_raw)
35 return -EINVAL;
36
Xiubo Lifb700672014-10-09 17:02:57 +080037 /* calculate the size of reg_defaults */
38 for (count = 0, i = 0; i < map->num_reg_defaults_raw; i++)
Maarten ter Huurneb2c7f5d2016-07-29 23:42:12 +020039 if (regmap_readable(map, i * map->reg_stride) &&
40 !regmap_volatile(map, i * map->reg_stride))
Xiubo Lifb700672014-10-09 17:02:57 +080041 count++;
42
Maarten ter Huurneb2c7f5d2016-07-29 23:42:12 +020043 /* all registers are unreadable or volatile, so just bypass */
Xiubo Lifb700672014-10-09 17:02:57 +080044 if (!count) {
45 map->cache_bypass = true;
46 return 0;
47 }
48
49 map->num_reg_defaults = count;
50 map->reg_defaults = kmalloc_array(count, sizeof(struct reg_default),
51 GFP_KERNEL);
52 if (!map->reg_defaults)
53 return -ENOMEM;
54
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010055 if (!map->reg_defaults_raw) {
Viresh Kumar621a5f72015-09-26 15:04:07 -070056 bool cache_bypass = map->cache_bypass;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010057 dev_warn(map->dev, "No cache defaults, reading back from HW\n");
Laxman Dewangandf00c792012-02-17 18:57:26 +053058
Maciej S. Szmigierod51fe1f2016-01-13 22:41:12 +010059 /* Bypass the cache access till data read from HW */
Viresh Kumar621a5f72015-09-26 15:04:07 -070060 map->cache_bypass = true;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010061 tmp_buf = kmalloc(map->cache_size_raw, GFP_KERNEL);
Xiubo Lifb700672014-10-09 17:02:57 +080062 if (!tmp_buf) {
63 ret = -ENOMEM;
64 goto err_free;
65 }
Mark Browneb4cb762013-02-21 18:39:47 +000066 ret = regmap_raw_read(map, 0, tmp_buf,
Maciej S. Szmigierod51fe1f2016-01-13 22:41:12 +010067 map->cache_size_raw);
Laxman Dewangandf00c792012-02-17 18:57:26 +053068 map->cache_bypass = cache_bypass;
Mark Brown3245d462016-02-02 10:16:51 -020069 if (ret == 0) {
70 map->reg_defaults_raw = tmp_buf;
Jiapeng Zhongb67498d62021-01-21 15:59:21 +080071 map->cache_free = true;
Mark Brown3245d462016-02-02 10:16:51 -020072 } else {
73 kfree(tmp_buf);
74 }
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010075 }
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++) {
Mark Brown3245d462016-02-02 10:16:51 -020079 reg = i * map->reg_stride;
80
81 if (!regmap_readable(map, reg))
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010082 continue;
Mark Brown3245d462016-02-02 10:16:51 -020083
84 if (regmap_volatile(map, reg))
85 continue;
86
87 if (map->reg_defaults_raw) {
88 val = regcache_get_val(map, map->reg_defaults_raw, i);
89 } else {
90 bool cache_bypass = map->cache_bypass;
91
92 map->cache_bypass = true;
93 ret = regmap_read(map, reg, &val);
94 map->cache_bypass = cache_bypass;
95 if (ret != 0) {
96 dev_err(map->dev, "Failed to read %d: %d\n",
97 reg, ret);
98 goto err_free;
99 }
100 }
101
102 map->reg_defaults[j].reg = reg;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100103 map->reg_defaults[j].def = val;
104 j++;
105 }
106
107 return 0;
Lars-Peter Clausen021cd612011-11-14 10:40:16 +0100108
109err_free:
Xiubo Lifb700672014-10-09 17:02:57 +0800110 kfree(map->reg_defaults);
Lars-Peter Clausen021cd612011-11-14 10:40:16 +0100111
112 return ret;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100113}
114
Lars-Peter Clausene5e3b8a2011-11-16 16:28:16 +0100115int regcache_init(struct regmap *map, const struct regmap_config *config)
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100116{
117 int ret;
118 int i;
119 void *tmp_buf;
120
Mark Browne7a6db32011-09-19 16:08:03 +0100121 if (map->cache_type == REGCACHE_NONE) {
Xiubo Li8cfe2fd2015-12-11 11:23:19 +0800122 if (config->reg_defaults || config->num_reg_defaults_raw)
123 dev_warn(map->dev,
124 "No cache used with register defaults set!\n");
125
Mark Browne7a6db32011-09-19 16:08:03 +0100126 map->cache_bypass = true;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100127 return 0;
Mark Browne7a6db32011-09-19 16:08:03 +0100128 }
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100129
Xiubo Li167f7062015-12-11 11:23:20 +0800130 if (config->reg_defaults && !config->num_reg_defaults) {
131 dev_err(map->dev,
132 "Register defaults are set without the number!\n");
133 return -EINVAL;
134 }
135
Xiubo Li8cfe2fd2015-12-11 11:23:19 +0800136 for (i = 0; i < config->num_reg_defaults; i++)
137 if (config->reg_defaults[i].reg % map->reg_stride)
138 return -EINVAL;
139
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100140 for (i = 0; i < ARRAY_SIZE(cache_types); i++)
141 if (cache_types[i]->type == map->cache_type)
142 break;
143
144 if (i == ARRAY_SIZE(cache_types)) {
145 dev_err(map->dev, "Could not match compress type: %d\n",
146 map->cache_type);
147 return -EINVAL;
148 }
149
Lars-Peter Clausene5e3b8a2011-11-16 16:28:16 +0100150 map->num_reg_defaults = config->num_reg_defaults;
151 map->num_reg_defaults_raw = config->num_reg_defaults_raw;
152 map->reg_defaults_raw = config->reg_defaults_raw;
Lars-Peter Clausen064d4db2011-11-16 20:34:03 +0100153 map->cache_word_size = DIV_ROUND_UP(config->val_bits, 8);
154 map->cache_size_raw = map->cache_word_size * config->num_reg_defaults_raw;
Lars-Peter Clausene5e3b8a2011-11-16 16:28:16 +0100155
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100156 map->cache = NULL;
157 map->cache_ops = cache_types[i];
158
159 if (!map->cache_ops->read ||
160 !map->cache_ops->write ||
161 !map->cache_ops->name)
162 return -EINVAL;
163
164 /* We still need to ensure that the reg_defaults
165 * won't vanish from under us. We'll need to make
166 * a copy of it.
167 */
Lars-Peter Clausen720e4612011-11-16 16:28:17 +0100168 if (config->reg_defaults) {
Lars-Peter Clausen720e4612011-11-16 16:28:17 +0100169 tmp_buf = kmemdup(config->reg_defaults, map->num_reg_defaults *
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100170 sizeof(struct reg_default), GFP_KERNEL);
171 if (!tmp_buf)
172 return -ENOMEM;
173 map->reg_defaults = tmp_buf;
Mark Brown8528bdd2011-10-09 13:13:58 +0100174 } else if (map->num_reg_defaults_raw) {
Mark Brown5fcd2562011-09-29 15:24:54 +0100175 /* Some devices such as PMICs don't have cache defaults,
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100176 * we cope with this by reading back the HW registers and
177 * crafting the cache defaults by hand.
178 */
179 ret = regcache_hw_init(map);
180 if (ret < 0)
181 return ret;
Xiubo Lifb700672014-10-09 17:02:57 +0800182 if (map->cache_bypass)
183 return 0;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100184 }
185
186 if (!map->max_register)
187 map->max_register = map->num_reg_defaults_raw;
188
189 if (map->cache_ops->init) {
190 dev_dbg(map->dev, "Initializing %s cache\n",
191 map->cache_ops->name);
Lars-Peter Clausenbd061c72011-11-14 10:40:17 +0100192 ret = map->cache_ops->init(map);
193 if (ret)
194 goto err_free;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100195 }
196 return 0;
Lars-Peter Clausenbd061c72011-11-14 10:40:17 +0100197
198err_free:
199 kfree(map->reg_defaults);
200 if (map->cache_free)
201 kfree(map->reg_defaults_raw);
202
203 return ret;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100204}
205
206void regcache_exit(struct regmap *map)
207{
208 if (map->cache_type == REGCACHE_NONE)
209 return;
210
211 BUG_ON(!map->cache_ops);
212
213 kfree(map->reg_defaults);
214 if (map->cache_free)
215 kfree(map->reg_defaults_raw);
216
217 if (map->cache_ops->exit) {
218 dev_dbg(map->dev, "Destroying %s cache\n",
219 map->cache_ops->name);
220 map->cache_ops->exit(map);
221 }
222}
223
224/**
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000225 * regcache_read - Fetch the value of a given register from the cache.
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100226 *
227 * @map: map to configure.
228 * @reg: The register index.
229 * @value: The value to be returned.
230 *
231 * Return a negative value on failure, 0 on success.
232 */
233int regcache_read(struct regmap *map,
234 unsigned int reg, unsigned int *value)
235{
Mark Brownbc7ee552011-11-30 14:27:08 +0000236 int ret;
237
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100238 if (map->cache_type == REGCACHE_NONE)
239 return -ENOSYS;
240
241 BUG_ON(!map->cache_ops);
242
Mark Brownbc7ee552011-11-30 14:27:08 +0000243 if (!regmap_volatile(map, reg)) {
244 ret = map->cache_ops->read(map, reg, value);
245
246 if (ret == 0)
Philipp Zabelc6b570d2015-03-09 12:20:13 +0100247 trace_regmap_reg_read_cache(map, reg, *value);
Mark Brownbc7ee552011-11-30 14:27:08 +0000248
249 return ret;
250 }
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100251
252 return -EINVAL;
253}
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100254
255/**
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000256 * regcache_write - Set the value of a given register in the cache.
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100257 *
258 * @map: map to configure.
259 * @reg: The register index.
260 * @value: The new register value.
261 *
262 * Return a negative value on failure, 0 on success.
263 */
264int regcache_write(struct regmap *map,
265 unsigned int reg, unsigned int value)
266{
267 if (map->cache_type == REGCACHE_NONE)
268 return 0;
269
270 BUG_ON(!map->cache_ops);
271
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100272 if (!regmap_volatile(map, reg))
273 return map->cache_ops->write(map, reg, value);
274
275 return 0;
276}
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100277
Kevin Cernekee3969fa082015-05-05 15:14:13 -0700278static bool regcache_reg_needs_sync(struct regmap *map, unsigned int reg,
279 unsigned int val)
280{
281 int ret;
282
Kevin Cernekee1c797712015-05-05 15:14:14 -0700283 /* If we don't know the chip just got reset, then sync everything. */
284 if (!map->no_sync_defaults)
285 return true;
286
Kevin Cernekee3969fa082015-05-05 15:14:13 -0700287 /* Is this the hardware default? If so skip. */
288 ret = regcache_lookup_reg(map, reg);
289 if (ret >= 0 && val == map->reg_defaults[ret].def)
290 return false;
291 return true;
292}
293
Maarten ter Huurned856fce2013-06-03 00:15:26 +0200294static int regcache_default_sync(struct regmap *map, unsigned int min,
295 unsigned int max)
296{
297 unsigned int reg;
298
Dylan Reid75617322014-03-18 13:45:08 -0700299 for (reg = min; reg <= max; reg += map->reg_stride) {
Maarten ter Huurned856fce2013-06-03 00:15:26 +0200300 unsigned int val;
301 int ret;
302
Dylan Reid83f84752014-03-18 13:45:09 -0700303 if (regmap_volatile(map, reg) ||
304 !regmap_writeable(map, reg))
Maarten ter Huurned856fce2013-06-03 00:15:26 +0200305 continue;
306
307 ret = regcache_read(map, reg, &val);
308 if (ret)
309 return ret;
310
Kevin Cernekee3969fa082015-05-05 15:14:13 -0700311 if (!regcache_reg_needs_sync(map, reg, val))
Maarten ter Huurned856fce2013-06-03 00:15:26 +0200312 continue;
313
Viresh Kumar621a5f72015-09-26 15:04:07 -0700314 map->cache_bypass = true;
Maarten ter Huurned856fce2013-06-03 00:15:26 +0200315 ret = _regmap_write(map, reg, val);
Viresh Kumar621a5f72015-09-26 15:04:07 -0700316 map->cache_bypass = false;
Jarkko Nikulaf29a4322014-09-16 14:04:14 +0300317 if (ret) {
318 dev_err(map->dev, "Unable to sync register %#x. %d\n",
319 reg, ret);
Maarten ter Huurned856fce2013-06-03 00:15:26 +0200320 return ret;
Jarkko Nikulaf29a4322014-09-16 14:04:14 +0300321 }
Maarten ter Huurned856fce2013-06-03 00:15:26 +0200322 dev_dbg(map->dev, "Synced register %#x, value %#x\n", reg, val);
323 }
324
325 return 0;
326}
327
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100328/**
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000329 * regcache_sync - Sync the register cache with the hardware.
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100330 *
331 * @map: map to configure.
332 *
333 * Any registers that should not be synced should be marked as
334 * volatile. In general drivers can choose not to use the provided
335 * syncing functionality if they so require.
336 *
337 * Return a negative value on failure, 0 on success.
338 */
339int regcache_sync(struct regmap *map)
340{
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100341 int ret = 0;
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100342 unsigned int i;
Dimitris Papastamos59360082011-09-19 14:34:04 +0100343 const char *name;
Viresh Kumar621a5f72015-09-26 15:04:07 -0700344 bool bypass;
Dimitris Papastamos59360082011-09-19 14:34:04 +0100345
Maarten ter Huurned856fce2013-06-03 00:15:26 +0200346 BUG_ON(!map->cache_ops);
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100347
Lars-Peter Clausen81485f52013-05-23 15:06:15 +0200348 map->lock(map->lock_arg);
Dimitris Papastamosbeb1a102011-09-29 14:36:26 +0100349 /* Remember the initial bypass state */
350 bypass = map->cache_bypass;
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100351 dev_dbg(map->dev, "Syncing %s cache\n",
352 map->cache_ops->name);
353 name = map->cache_ops->name;
Philipp Zabelc6b570d2015-03-09 12:20:13 +0100354 trace_regcache_sync(map, name, "start");
Mark Brown22f0d902012-01-21 12:01:14 +0000355
Mark Brown8ae0d7e2011-10-26 10:34:22 +0200356 if (!map->cache_dirty)
357 goto out;
Mark Brownd9db7622012-01-25 21:06:33 +0000358
Mark Brownaffbe882013-10-10 21:06:32 +0100359 map->async = true;
360
Mark Brown22f0d902012-01-21 12:01:14 +0000361 /* Apply any patch first */
Viresh Kumar621a5f72015-09-26 15:04:07 -0700362 map->cache_bypass = true;
Mark Brown22f0d902012-01-21 12:01:14 +0000363 for (i = 0; i < map->patch_regs; i++) {
364 ret = _regmap_write(map, map->patch[i].reg, map->patch[i].def);
365 if (ret != 0) {
366 dev_err(map->dev, "Failed to write %x = %x: %d\n",
367 map->patch[i].reg, map->patch[i].def, ret);
368 goto out;
369 }
370 }
Viresh Kumar621a5f72015-09-26 15:04:07 -0700371 map->cache_bypass = false;
Mark Brown22f0d902012-01-21 12:01:14 +0000372
Maarten ter Huurned856fce2013-06-03 00:15:26 +0200373 if (map->cache_ops->sync)
374 ret = map->cache_ops->sync(map, 0, map->max_register);
375 else
376 ret = regcache_default_sync(map, 0, map->max_register);
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100377
Mark Brown6ff73732012-02-23 22:05:59 +0000378 if (ret == 0)
379 map->cache_dirty = false;
380
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100381out:
Dimitris Papastamosbeb1a102011-09-29 14:36:26 +0100382 /* Restore the bypass state */
Mark Brownaffbe882013-10-10 21:06:32 +0100383 map->async = false;
Dimitris Papastamosbeb1a102011-09-29 14:36:26 +0100384 map->cache_bypass = bypass;
Kevin Cernekee1c797712015-05-05 15:14:14 -0700385 map->no_sync_defaults = false;
Lars-Peter Clausen81485f52013-05-23 15:06:15 +0200386 map->unlock(map->lock_arg);
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100387
Mark Brownaffbe882013-10-10 21:06:32 +0100388 regmap_async_complete(map);
389
Philipp Zabelc6b570d2015-03-09 12:20:13 +0100390 trace_regcache_sync(map, name, "stop");
Mark Brownaffbe882013-10-10 21:06:32 +0100391
Dimitris Papastamos954757d2011-09-27 11:25:06 +0100392 return ret;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100393}
394EXPORT_SYMBOL_GPL(regcache_sync);
395
Mark Brown92afb282011-09-19 18:22:14 +0100396/**
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000397 * regcache_sync_region - Sync part of the register cache with the hardware.
Mark Brown4d4cfd12012-02-23 20:53:37 +0000398 *
399 * @map: map to sync.
400 * @min: first register to sync
401 * @max: last register to sync
402 *
403 * Write all non-default register values in the specified region to
404 * the hardware.
405 *
406 * Return a negative value on failure, 0 on success.
407 */
408int regcache_sync_region(struct regmap *map, unsigned int min,
409 unsigned int max)
410{
411 int ret = 0;
412 const char *name;
Viresh Kumar621a5f72015-09-26 15:04:07 -0700413 bool bypass;
Mark Brown4d4cfd12012-02-23 20:53:37 +0000414
Maarten ter Huurned856fce2013-06-03 00:15:26 +0200415 BUG_ON(!map->cache_ops);
Mark Brown4d4cfd12012-02-23 20:53:37 +0000416
Lars-Peter Clausen81485f52013-05-23 15:06:15 +0200417 map->lock(map->lock_arg);
Mark Brown4d4cfd12012-02-23 20:53:37 +0000418
419 /* Remember the initial bypass state */
420 bypass = map->cache_bypass;
421
422 name = map->cache_ops->name;
423 dev_dbg(map->dev, "Syncing %s cache from %d-%d\n", name, min, max);
424
Philipp Zabelc6b570d2015-03-09 12:20:13 +0100425 trace_regcache_sync(map, name, "start region");
Mark Brown4d4cfd12012-02-23 20:53:37 +0000426
427 if (!map->cache_dirty)
428 goto out;
429
Mark Brownaffbe882013-10-10 21:06:32 +0100430 map->async = true;
431
Maarten ter Huurned856fce2013-06-03 00:15:26 +0200432 if (map->cache_ops->sync)
433 ret = map->cache_ops->sync(map, min, max);
434 else
435 ret = regcache_default_sync(map, min, max);
Mark Brown4d4cfd12012-02-23 20:53:37 +0000436
437out:
Mark Brown4d4cfd12012-02-23 20:53:37 +0000438 /* Restore the bypass state */
439 map->cache_bypass = bypass;
Mark Brownaffbe882013-10-10 21:06:32 +0100440 map->async = false;
Kevin Cernekee1c797712015-05-05 15:14:14 -0700441 map->no_sync_defaults = false;
Lars-Peter Clausen81485f52013-05-23 15:06:15 +0200442 map->unlock(map->lock_arg);
Mark Brown4d4cfd12012-02-23 20:53:37 +0000443
Mark Brownaffbe882013-10-10 21:06:32 +0100444 regmap_async_complete(map);
445
Philipp Zabelc6b570d2015-03-09 12:20:13 +0100446 trace_regcache_sync(map, name, "stop region");
Mark Brownaffbe882013-10-10 21:06:32 +0100447
Mark Brown4d4cfd12012-02-23 20:53:37 +0000448 return ret;
449}
Mark Browne466de02012-04-03 13:08:53 +0100450EXPORT_SYMBOL_GPL(regcache_sync_region);
Mark Brown4d4cfd12012-02-23 20:53:37 +0000451
452/**
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000453 * regcache_drop_region - Discard part of the register cache
Mark Brown697e85b2013-05-08 13:55:22 +0100454 *
455 * @map: map to operate on
456 * @min: first register to discard
457 * @max: last register to discard
458 *
459 * Discard part of the register cache.
460 *
461 * Return a negative value on failure, 0 on success.
462 */
463int regcache_drop_region(struct regmap *map, unsigned int min,
464 unsigned int max)
465{
Mark Brown697e85b2013-05-08 13:55:22 +0100466 int ret = 0;
467
Lars-Peter Clausen3f4ff562013-08-29 10:26:34 +0200468 if (!map->cache_ops || !map->cache_ops->drop)
Mark Brown697e85b2013-05-08 13:55:22 +0100469 return -EINVAL;
470
Lars-Peter Clausen81485f52013-05-23 15:06:15 +0200471 map->lock(map->lock_arg);
Mark Brown697e85b2013-05-08 13:55:22 +0100472
Philipp Zabelc6b570d2015-03-09 12:20:13 +0100473 trace_regcache_drop_region(map, min, max);
Mark Brown697e85b2013-05-08 13:55:22 +0100474
Lars-Peter Clausen3f4ff562013-08-29 10:26:34 +0200475 ret = map->cache_ops->drop(map, min, max);
Mark Brown697e85b2013-05-08 13:55:22 +0100476
Lars-Peter Clausen81485f52013-05-23 15:06:15 +0200477 map->unlock(map->lock_arg);
Mark Brown697e85b2013-05-08 13:55:22 +0100478
479 return ret;
480}
481EXPORT_SYMBOL_GPL(regcache_drop_region);
482
483/**
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000484 * regcache_cache_only - Put a register map into cache only mode
Mark Brown92afb282011-09-19 18:22:14 +0100485 *
486 * @map: map to configure
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000487 * @enable: flag if changes should be written to the hardware
Mark Brown92afb282011-09-19 18:22:14 +0100488 *
489 * When a register map is marked as cache only writes to the register
490 * map API will only update the register cache, they will not cause
491 * any hardware changes. This is useful for allowing portions of
492 * drivers to act as though the device were functioning as normal when
493 * it is disabled for power saving reasons.
494 */
495void regcache_cache_only(struct regmap *map, bool enable)
496{
Lars-Peter Clausen81485f52013-05-23 15:06:15 +0200497 map->lock(map->lock_arg);
Dimitris Papastamosac77a762011-09-29 14:36:28 +0100498 WARN_ON(map->cache_bypass && enable);
Mark Brown92afb282011-09-19 18:22:14 +0100499 map->cache_only = enable;
Philipp Zabelc6b570d2015-03-09 12:20:13 +0100500 trace_regmap_cache_only(map, enable);
Lars-Peter Clausen81485f52013-05-23 15:06:15 +0200501 map->unlock(map->lock_arg);
Mark Brown92afb282011-09-19 18:22:14 +0100502}
503EXPORT_SYMBOL_GPL(regcache_cache_only);
504
Dimitris Papastamos6eb0f5e2011-09-29 14:36:27 +0100505/**
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000506 * regcache_mark_dirty - Indicate that HW registers were reset to default values
Mark Brown8ae0d7e2011-10-26 10:34:22 +0200507 *
508 * @map: map to mark
509 *
Kevin Cernekee1c797712015-05-05 15:14:14 -0700510 * Inform regcache that the device has been powered down or reset, so that
511 * on resume, regcache_sync() knows to write out all non-default values
512 * stored in the cache.
513 *
514 * If this function is not called, regcache_sync() will assume that
515 * the hardware state still matches the cache state, modulo any writes that
516 * happened when cache_only was true.
Mark Brown8ae0d7e2011-10-26 10:34:22 +0200517 */
518void regcache_mark_dirty(struct regmap *map)
519{
Lars-Peter Clausen81485f52013-05-23 15:06:15 +0200520 map->lock(map->lock_arg);
Mark Brown8ae0d7e2011-10-26 10:34:22 +0200521 map->cache_dirty = true;
Kevin Cernekee1c797712015-05-05 15:14:14 -0700522 map->no_sync_defaults = true;
Lars-Peter Clausen81485f52013-05-23 15:06:15 +0200523 map->unlock(map->lock_arg);
Mark Brown8ae0d7e2011-10-26 10:34:22 +0200524}
525EXPORT_SYMBOL_GPL(regcache_mark_dirty);
526
527/**
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000528 * regcache_cache_bypass - Put a register map into cache bypass mode
Dimitris Papastamos6eb0f5e2011-09-29 14:36:27 +0100529 *
530 * @map: map to configure
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000531 * @enable: flag if changes should not be written to the cache
Dimitris Papastamos6eb0f5e2011-09-29 14:36:27 +0100532 *
533 * When a register map is marked with the cache bypass option, writes
534 * to the register map API will only update the hardware and not the
535 * the cache directly. This is useful when syncing the cache back to
536 * the hardware.
537 */
538void regcache_cache_bypass(struct regmap *map, bool enable)
539{
Lars-Peter Clausen81485f52013-05-23 15:06:15 +0200540 map->lock(map->lock_arg);
Dimitris Papastamosac77a762011-09-29 14:36:28 +0100541 WARN_ON(map->cache_only && enable);
Dimitris Papastamos6eb0f5e2011-09-29 14:36:27 +0100542 map->cache_bypass = enable;
Philipp Zabelc6b570d2015-03-09 12:20:13 +0100543 trace_regmap_cache_bypass(map, enable);
Lars-Peter Clausen81485f52013-05-23 15:06:15 +0200544 map->unlock(map->lock_arg);
Dimitris Papastamos6eb0f5e2011-09-29 14:36:27 +0100545}
546EXPORT_SYMBOL_GPL(regcache_cache_bypass);
547
Mark Brown879082c2013-02-21 18:03:13 +0000548bool regcache_set_val(struct regmap *map, void *base, unsigned int idx,
549 unsigned int val)
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100550{
Mark Brown325acab2013-02-21 18:07:01 +0000551 if (regcache_get_val(map, base, idx) == val)
552 return true;
553
Mark Browneb4cb762013-02-21 18:39:47 +0000554 /* Use device native format if possible */
555 if (map->format.format_val) {
556 map->format.format_val(base + (map->cache_word_size * idx),
557 val, 0);
558 return false;
559 }
560
Mark Brown879082c2013-02-21 18:03:13 +0000561 switch (map->cache_word_size) {
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100562 case 1: {
563 u8 *cache = base;
Xiubo Li2fd69022015-12-09 13:09:06 +0800564
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100565 cache[idx] = val;
566 break;
567 }
568 case 2: {
569 u16 *cache = base;
Xiubo Li2fd69022015-12-09 13:09:06 +0800570
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100571 cache[idx] = val;
572 break;
573 }
Mark Brown7d5e5252012-02-17 15:58:25 -0800574 case 4: {
575 u32 *cache = base;
Xiubo Li2fd69022015-12-09 13:09:06 +0800576
Mark Brown7d5e5252012-02-17 15:58:25 -0800577 cache[idx] = val;
578 break;
579 }
Xiubo Li8b7663d2015-12-09 13:09:07 +0800580#ifdef CONFIG_64BIT
581 case 8: {
582 u64 *cache = base;
583
584 cache[idx] = val;
585 break;
586 }
587#endif
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100588 default:
589 BUG();
590 }
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100591 return false;
592}
593
Mark Brown879082c2013-02-21 18:03:13 +0000594unsigned int regcache_get_val(struct regmap *map, const void *base,
595 unsigned int idx)
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100596{
597 if (!base)
598 return -EINVAL;
599
Mark Browneb4cb762013-02-21 18:39:47 +0000600 /* Use device native format if possible */
601 if (map->format.parse_val)
Mark Brown88177962013-03-13 19:29:36 +0000602 return map->format.parse_val(regcache_get_val_addr(map, base,
603 idx));
Mark Browneb4cb762013-02-21 18:39:47 +0000604
Mark Brown879082c2013-02-21 18:03:13 +0000605 switch (map->cache_word_size) {
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100606 case 1: {
607 const u8 *cache = base;
Xiubo Li2fd69022015-12-09 13:09:06 +0800608
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100609 return cache[idx];
610 }
611 case 2: {
612 const u16 *cache = base;
Xiubo Li2fd69022015-12-09 13:09:06 +0800613
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100614 return cache[idx];
615 }
Mark Brown7d5e5252012-02-17 15:58:25 -0800616 case 4: {
617 const u32 *cache = base;
Xiubo Li2fd69022015-12-09 13:09:06 +0800618
Mark Brown7d5e5252012-02-17 15:58:25 -0800619 return cache[idx];
620 }
Xiubo Li8b7663d2015-12-09 13:09:07 +0800621#ifdef CONFIG_64BIT
622 case 8: {
623 const u64 *cache = base;
624
625 return cache[idx];
626 }
627#endif
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100628 default:
629 BUG();
630 }
631 /* unreachable */
632 return -1;
633}
634
Mark Brownf094fea2011-10-04 22:05:47 +0100635static int regcache_default_cmp(const void *a, const void *b)
Dimitris Papastamosc08604b2011-10-03 10:50:14 +0100636{
637 const struct reg_default *_a = a;
638 const struct reg_default *_b = b;
639
640 return _a->reg - _b->reg;
641}
642
Mark Brownf094fea2011-10-04 22:05:47 +0100643int regcache_lookup_reg(struct regmap *map, unsigned int reg)
644{
645 struct reg_default key;
646 struct reg_default *r;
647
648 key.reg = reg;
649 key.def = 0;
650
651 r = bsearch(&key, map->reg_defaults, map->num_reg_defaults,
652 sizeof(struct reg_default), regcache_default_cmp);
653
654 if (r)
655 return r - map->reg_defaults;
656 else
Mark Brown6e6ace02011-10-09 13:23:31 +0100657 return -ENOENT;
Mark Brownf094fea2011-10-04 22:05:47 +0100658}
Mark Brownf8bd8222013-03-29 19:32:28 +0000659
Lars-Peter Clausen3f4ff562013-08-29 10:26:34 +0200660static bool regcache_reg_present(unsigned long *cache_present, unsigned int idx)
661{
662 if (!cache_present)
663 return true;
664
665 return test_bit(idx, cache_present);
666}
667
Mark Browncfdeb8c2013-03-29 20:12:21 +0000668static int regcache_sync_block_single(struct regmap *map, void *block,
Lars-Peter Clausen3f4ff562013-08-29 10:26:34 +0200669 unsigned long *cache_present,
Mark Browncfdeb8c2013-03-29 20:12:21 +0000670 unsigned int block_base,
671 unsigned int start, unsigned int end)
672{
673 unsigned int i, regtmp, val;
674 int ret;
675
676 for (i = start; i < end; i++) {
677 regtmp = block_base + (i * map->reg_stride);
678
Takashi Iwai4ceba982015-03-04 15:29:17 +0100679 if (!regcache_reg_present(cache_present, i) ||
680 !regmap_writeable(map, regtmp))
Mark Browncfdeb8c2013-03-29 20:12:21 +0000681 continue;
682
683 val = regcache_get_val(map, block, i);
Kevin Cernekee3969fa082015-05-05 15:14:13 -0700684 if (!regcache_reg_needs_sync(map, regtmp, val))
Mark Browncfdeb8c2013-03-29 20:12:21 +0000685 continue;
686
Viresh Kumar621a5f72015-09-26 15:04:07 -0700687 map->cache_bypass = true;
Mark Browncfdeb8c2013-03-29 20:12:21 +0000688
689 ret = _regmap_write(map, regtmp, val);
690
Viresh Kumar621a5f72015-09-26 15:04:07 -0700691 map->cache_bypass = false;
Jarkko Nikulaf29a4322014-09-16 14:04:14 +0300692 if (ret != 0) {
693 dev_err(map->dev, "Unable to sync register %#x. %d\n",
694 regtmp, ret);
Mark Browncfdeb8c2013-03-29 20:12:21 +0000695 return ret;
Jarkko Nikulaf29a4322014-09-16 14:04:14 +0300696 }
Mark Browncfdeb8c2013-03-29 20:12:21 +0000697 dev_dbg(map->dev, "Synced register %#x, value %#x\n",
698 regtmp, val);
699 }
700
701 return 0;
702}
703
Mark Brown75a5f892013-03-29 20:50:07 +0000704static int regcache_sync_block_raw_flush(struct regmap *map, const void **data,
705 unsigned int base, unsigned int cur)
706{
707 size_t val_bytes = map->format.val_bytes;
708 int ret, count;
709
710 if (*data == NULL)
711 return 0;
712
Dylan Reid78ba73e2014-01-24 15:40:39 -0800713 count = (cur - base) / map->reg_stride;
Mark Brown75a5f892013-03-29 20:50:07 +0000714
Stratos Karafotis96592932013-04-04 19:40:45 +0300715 dev_dbg(map->dev, "Writing %zu bytes for %d registers from 0x%x-0x%x\n",
Dylan Reid78ba73e2014-01-24 15:40:39 -0800716 count * val_bytes, count, base, cur - map->reg_stride);
Mark Brown75a5f892013-03-29 20:50:07 +0000717
Viresh Kumar621a5f72015-09-26 15:04:07 -0700718 map->cache_bypass = true;
Mark Brown75a5f892013-03-29 20:50:07 +0000719
Dmitry Baryshkov05669b62020-09-17 18:34:05 +0300720 ret = _regmap_raw_write(map, base, *data, count * val_bytes, false);
Jarkko Nikulaf29a4322014-09-16 14:04:14 +0300721 if (ret)
722 dev_err(map->dev, "Unable to sync registers %#x-%#x. %d\n",
723 base, cur - map->reg_stride, ret);
Mark Brown75a5f892013-03-29 20:50:07 +0000724
Viresh Kumar621a5f72015-09-26 15:04:07 -0700725 map->cache_bypass = false;
Mark Brown75a5f892013-03-29 20:50:07 +0000726
727 *data = NULL;
728
729 return ret;
730}
731
Sachin Kamatf52687a2013-04-04 14:36:18 +0530732static int regcache_sync_block_raw(struct regmap *map, void *block,
Lars-Peter Clausen3f4ff562013-08-29 10:26:34 +0200733 unsigned long *cache_present,
Mark Browncfdeb8c2013-03-29 20:12:21 +0000734 unsigned int block_base, unsigned int start,
735 unsigned int end)
Mark Brownf8bd8222013-03-29 19:32:28 +0000736{
Mark Brown75a5f892013-03-29 20:50:07 +0000737 unsigned int i, val;
738 unsigned int regtmp = 0;
739 unsigned int base = 0;
740 const void *data = NULL;
Mark Brownf8bd8222013-03-29 19:32:28 +0000741 int ret;
742
743 for (i = start; i < end; i++) {
744 regtmp = block_base + (i * map->reg_stride);
745
Takashi Iwai4ceba982015-03-04 15:29:17 +0100746 if (!regcache_reg_present(cache_present, i) ||
747 !regmap_writeable(map, regtmp)) {
Mark Brown75a5f892013-03-29 20:50:07 +0000748 ret = regcache_sync_block_raw_flush(map, &data,
749 base, regtmp);
750 if (ret != 0)
751 return ret;
Mark Brownf8bd8222013-03-29 19:32:28 +0000752 continue;
Mark Brown75a5f892013-03-29 20:50:07 +0000753 }
Mark Brownf8bd8222013-03-29 19:32:28 +0000754
755 val = regcache_get_val(map, block, i);
Kevin Cernekee3969fa082015-05-05 15:14:13 -0700756 if (!regcache_reg_needs_sync(map, regtmp, val)) {
Mark Brown75a5f892013-03-29 20:50:07 +0000757 ret = regcache_sync_block_raw_flush(map, &data,
758 base, regtmp);
759 if (ret != 0)
760 return ret;
Mark Brownf8bd8222013-03-29 19:32:28 +0000761 continue;
Mark Brown75a5f892013-03-29 20:50:07 +0000762 }
Mark Brownf8bd8222013-03-29 19:32:28 +0000763
Mark Brown75a5f892013-03-29 20:50:07 +0000764 if (!data) {
765 data = regcache_get_val_addr(map, block, i);
766 base = regtmp;
767 }
Mark Brownf8bd8222013-03-29 19:32:28 +0000768 }
769
Lars-Peter Clausen2d49b592013-08-05 11:21:29 +0200770 return regcache_sync_block_raw_flush(map, &data, base, regtmp +
771 map->reg_stride);
Mark Brownf8bd8222013-03-29 19:32:28 +0000772}
Mark Browncfdeb8c2013-03-29 20:12:21 +0000773
774int regcache_sync_block(struct regmap *map, void *block,
Lars-Peter Clausen3f4ff562013-08-29 10:26:34 +0200775 unsigned long *cache_present,
Mark Browncfdeb8c2013-03-29 20:12:21 +0000776 unsigned int block_base, unsigned int start,
777 unsigned int end)
778{
Markus Pargmann67921a12015-08-21 10:26:42 +0200779 if (regmap_can_raw_write(map) && !map->use_single_write)
Lars-Peter Clausen3f4ff562013-08-29 10:26:34 +0200780 return regcache_sync_block_raw(map, block, cache_present,
781 block_base, start, end);
Mark Browncfdeb8c2013-03-29 20:12:21 +0000782 else
Lars-Peter Clausen3f4ff562013-08-29 10:26:34 +0200783 return regcache_sync_block_single(map, block, cache_present,
784 block_base, start, end);
Mark Browncfdeb8c2013-03-29 20:12:21 +0000785}