Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 1 | /* |
| 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 Brown | f094fea | 2011-10-04 22:05:47 +0100 | [diff] [blame] | 13 | #include <linux/bsearch.h> |
Xiubo Li | e39be3a | 2014-10-09 17:02:52 +0800 | [diff] [blame] | 14 | #include <linux/device.h> |
| 15 | #include <linux/export.h> |
| 16 | #include <linux/slab.h> |
Dimitris Papastamos | c08604b | 2011-10-03 10:50:14 +0100 | [diff] [blame] | 17 | #include <linux/sort.h> |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 18 | |
Steven Rostedt | f58078d | 2015-03-19 17:50:47 -0400 | [diff] [blame] | 19 | #include "trace.h" |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 20 | #include "internal.h" |
| 21 | |
| 22 | static const struct regcache_ops *cache_types[] = { |
Dimitris Papastamos | 28644c80 | 2011-09-19 14:34:02 +0100 | [diff] [blame] | 23 | ®cache_rbtree_ops, |
Dimitris Papastamos | 2cbbb57 | 2011-09-19 14:34:03 +0100 | [diff] [blame] | 24 | ®cache_lzo_ops, |
Mark Brown | 2ac902c | 2012-12-19 14:51:55 +0000 | [diff] [blame] | 25 | ®cache_flat_ops, |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 26 | }; |
| 27 | |
| 28 | static 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 Li | fb70067 | 2014-10-09 17:02:57 +0800 | [diff] [blame] | 39 | /* 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 Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 56 | if (!map->reg_defaults_raw) { |
Laxman Dewangan | df00c79 | 2012-02-17 18:57:26 +0530 | [diff] [blame] | 57 | u32 cache_bypass = map->cache_bypass; |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 58 | dev_warn(map->dev, "No cache defaults, reading back from HW\n"); |
Laxman Dewangan | df00c79 | 2012-02-17 18:57:26 +0530 | [diff] [blame] | 59 | |
| 60 | /* Bypass the cache access till data read from HW*/ |
| 61 | map->cache_bypass = 1; |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 62 | tmp_buf = kmalloc(map->cache_size_raw, GFP_KERNEL); |
Xiubo Li | fb70067 | 2014-10-09 17:02:57 +0800 | [diff] [blame] | 63 | if (!tmp_buf) { |
| 64 | ret = -ENOMEM; |
| 65 | goto err_free; |
| 66 | } |
Mark Brown | eb4cb76 | 2013-02-21 18:39:47 +0000 | [diff] [blame] | 67 | ret = regmap_raw_read(map, 0, tmp_buf, |
| 68 | map->num_reg_defaults_raw); |
Laxman Dewangan | df00c79 | 2012-02-17 18:57:26 +0530 | [diff] [blame] | 69 | map->cache_bypass = cache_bypass; |
Xiubo Li | fb70067 | 2014-10-09 17:02:57 +0800 | [diff] [blame] | 70 | if (ret < 0) |
| 71 | goto err_cache_free; |
| 72 | |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 73 | map->reg_defaults_raw = tmp_buf; |
| 74 | map->cache_free = 1; |
| 75 | } |
| 76 | |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 77 | /* fill the reg_defaults */ |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 78 | for (i = 0, j = 0; i < map->num_reg_defaults_raw; i++) { |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 79 | if (regmap_volatile(map, i * map->reg_stride)) |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 80 | continue; |
Xiubo Li | fbba43c | 2014-10-09 17:02:55 +0800 | [diff] [blame] | 81 | val = regcache_get_val(map, map->reg_defaults_raw, i); |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 82 | map->reg_defaults[j].reg = i * map->reg_stride; |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 83 | map->reg_defaults[j].def = val; |
| 84 | j++; |
| 85 | } |
| 86 | |
| 87 | return 0; |
Lars-Peter Clausen | 021cd61 | 2011-11-14 10:40:16 +0100 | [diff] [blame] | 88 | |
Xiubo Li | fb70067 | 2014-10-09 17:02:57 +0800 | [diff] [blame] | 89 | err_cache_free: |
| 90 | kfree(tmp_buf); |
Lars-Peter Clausen | 021cd61 | 2011-11-14 10:40:16 +0100 | [diff] [blame] | 91 | err_free: |
Xiubo Li | fb70067 | 2014-10-09 17:02:57 +0800 | [diff] [blame] | 92 | kfree(map->reg_defaults); |
Lars-Peter Clausen | 021cd61 | 2011-11-14 10:40:16 +0100 | [diff] [blame] | 93 | |
| 94 | return ret; |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 95 | } |
| 96 | |
Lars-Peter Clausen | e5e3b8a | 2011-11-16 16:28:16 +0100 | [diff] [blame] | 97 | int regcache_init(struct regmap *map, const struct regmap_config *config) |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 98 | { |
| 99 | int ret; |
| 100 | int i; |
| 101 | void *tmp_buf; |
| 102 | |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 103 | for (i = 0; i < config->num_reg_defaults; i++) |
| 104 | if (config->reg_defaults[i].reg % map->reg_stride) |
| 105 | return -EINVAL; |
| 106 | |
Mark Brown | e7a6db3 | 2011-09-19 16:08:03 +0100 | [diff] [blame] | 107 | if (map->cache_type == REGCACHE_NONE) { |
| 108 | map->cache_bypass = true; |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 109 | return 0; |
Mark Brown | e7a6db3 | 2011-09-19 16:08:03 +0100 | [diff] [blame] | 110 | } |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 111 | |
| 112 | for (i = 0; i < ARRAY_SIZE(cache_types); i++) |
| 113 | if (cache_types[i]->type == map->cache_type) |
| 114 | break; |
| 115 | |
| 116 | if (i == ARRAY_SIZE(cache_types)) { |
| 117 | dev_err(map->dev, "Could not match compress type: %d\n", |
| 118 | map->cache_type); |
| 119 | return -EINVAL; |
| 120 | } |
| 121 | |
Lars-Peter Clausen | e5e3b8a | 2011-11-16 16:28:16 +0100 | [diff] [blame] | 122 | map->num_reg_defaults = config->num_reg_defaults; |
| 123 | map->num_reg_defaults_raw = config->num_reg_defaults_raw; |
| 124 | map->reg_defaults_raw = config->reg_defaults_raw; |
Lars-Peter Clausen | 064d4db | 2011-11-16 20:34:03 +0100 | [diff] [blame] | 125 | map->cache_word_size = DIV_ROUND_UP(config->val_bits, 8); |
| 126 | map->cache_size_raw = map->cache_word_size * config->num_reg_defaults_raw; |
Lars-Peter Clausen | e5e3b8a | 2011-11-16 16:28:16 +0100 | [diff] [blame] | 127 | |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 128 | map->cache = NULL; |
| 129 | map->cache_ops = cache_types[i]; |
| 130 | |
| 131 | if (!map->cache_ops->read || |
| 132 | !map->cache_ops->write || |
| 133 | !map->cache_ops->name) |
| 134 | return -EINVAL; |
| 135 | |
| 136 | /* We still need to ensure that the reg_defaults |
| 137 | * won't vanish from under us. We'll need to make |
| 138 | * a copy of it. |
| 139 | */ |
Lars-Peter Clausen | 720e461 | 2011-11-16 16:28:17 +0100 | [diff] [blame] | 140 | if (config->reg_defaults) { |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 141 | if (!map->num_reg_defaults) |
| 142 | return -EINVAL; |
Lars-Peter Clausen | 720e461 | 2011-11-16 16:28:17 +0100 | [diff] [blame] | 143 | tmp_buf = kmemdup(config->reg_defaults, map->num_reg_defaults * |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 144 | sizeof(struct reg_default), GFP_KERNEL); |
| 145 | if (!tmp_buf) |
| 146 | return -ENOMEM; |
| 147 | map->reg_defaults = tmp_buf; |
Mark Brown | 8528bdd | 2011-10-09 13:13:58 +0100 | [diff] [blame] | 148 | } else if (map->num_reg_defaults_raw) { |
Mark Brown | 5fcd256 | 2011-09-29 15:24:54 +0100 | [diff] [blame] | 149 | /* Some devices such as PMICs don't have cache defaults, |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 150 | * we cope with this by reading back the HW registers and |
| 151 | * crafting the cache defaults by hand. |
| 152 | */ |
| 153 | ret = regcache_hw_init(map); |
| 154 | if (ret < 0) |
| 155 | return ret; |
Xiubo Li | fb70067 | 2014-10-09 17:02:57 +0800 | [diff] [blame] | 156 | if (map->cache_bypass) |
| 157 | return 0; |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | if (!map->max_register) |
| 161 | map->max_register = map->num_reg_defaults_raw; |
| 162 | |
| 163 | if (map->cache_ops->init) { |
| 164 | dev_dbg(map->dev, "Initializing %s cache\n", |
| 165 | map->cache_ops->name); |
Lars-Peter Clausen | bd061c7 | 2011-11-14 10:40:17 +0100 | [diff] [blame] | 166 | ret = map->cache_ops->init(map); |
| 167 | if (ret) |
| 168 | goto err_free; |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 169 | } |
| 170 | return 0; |
Lars-Peter Clausen | bd061c7 | 2011-11-14 10:40:17 +0100 | [diff] [blame] | 171 | |
| 172 | err_free: |
| 173 | kfree(map->reg_defaults); |
| 174 | if (map->cache_free) |
| 175 | kfree(map->reg_defaults_raw); |
| 176 | |
| 177 | return ret; |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | void regcache_exit(struct regmap *map) |
| 181 | { |
| 182 | if (map->cache_type == REGCACHE_NONE) |
| 183 | return; |
| 184 | |
| 185 | BUG_ON(!map->cache_ops); |
| 186 | |
| 187 | kfree(map->reg_defaults); |
| 188 | if (map->cache_free) |
| 189 | kfree(map->reg_defaults_raw); |
| 190 | |
| 191 | if (map->cache_ops->exit) { |
| 192 | dev_dbg(map->dev, "Destroying %s cache\n", |
| 193 | map->cache_ops->name); |
| 194 | map->cache_ops->exit(map); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * regcache_read: Fetch the value of a given register from the cache. |
| 200 | * |
| 201 | * @map: map to configure. |
| 202 | * @reg: The register index. |
| 203 | * @value: The value to be returned. |
| 204 | * |
| 205 | * Return a negative value on failure, 0 on success. |
| 206 | */ |
| 207 | int regcache_read(struct regmap *map, |
| 208 | unsigned int reg, unsigned int *value) |
| 209 | { |
Mark Brown | bc7ee55 | 2011-11-30 14:27:08 +0000 | [diff] [blame] | 210 | int ret; |
| 211 | |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 212 | if (map->cache_type == REGCACHE_NONE) |
| 213 | return -ENOSYS; |
| 214 | |
| 215 | BUG_ON(!map->cache_ops); |
| 216 | |
Mark Brown | bc7ee55 | 2011-11-30 14:27:08 +0000 | [diff] [blame] | 217 | if (!regmap_volatile(map, reg)) { |
| 218 | ret = map->cache_ops->read(map, reg, value); |
| 219 | |
| 220 | if (ret == 0) |
Philipp Zabel | c6b570d | 2015-03-09 12:20:13 +0100 | [diff] [blame] | 221 | trace_regmap_reg_read_cache(map, reg, *value); |
Mark Brown | bc7ee55 | 2011-11-30 14:27:08 +0000 | [diff] [blame] | 222 | |
| 223 | return ret; |
| 224 | } |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 225 | |
| 226 | return -EINVAL; |
| 227 | } |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 228 | |
| 229 | /** |
| 230 | * regcache_write: Set the value of a given register in the cache. |
| 231 | * |
| 232 | * @map: map to configure. |
| 233 | * @reg: The register index. |
| 234 | * @value: The new register value. |
| 235 | * |
| 236 | * Return a negative value on failure, 0 on success. |
| 237 | */ |
| 238 | int regcache_write(struct regmap *map, |
| 239 | unsigned int reg, unsigned int value) |
| 240 | { |
| 241 | if (map->cache_type == REGCACHE_NONE) |
| 242 | return 0; |
| 243 | |
| 244 | BUG_ON(!map->cache_ops); |
| 245 | |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 246 | if (!regmap_volatile(map, reg)) |
| 247 | return map->cache_ops->write(map, reg, value); |
| 248 | |
| 249 | return 0; |
| 250 | } |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 251 | |
Kevin Cernekee | 3969fa08 | 2015-05-05 15:14:13 -0700 | [diff] [blame^] | 252 | static bool regcache_reg_needs_sync(struct regmap *map, unsigned int reg, |
| 253 | unsigned int val) |
| 254 | { |
| 255 | int ret; |
| 256 | |
| 257 | /* Is this the hardware default? If so skip. */ |
| 258 | ret = regcache_lookup_reg(map, reg); |
| 259 | if (ret >= 0 && val == map->reg_defaults[ret].def) |
| 260 | return false; |
| 261 | return true; |
| 262 | } |
| 263 | |
Maarten ter Huurne | d856fce | 2013-06-03 00:15:26 +0200 | [diff] [blame] | 264 | static int regcache_default_sync(struct regmap *map, unsigned int min, |
| 265 | unsigned int max) |
| 266 | { |
| 267 | unsigned int reg; |
| 268 | |
Dylan Reid | 7561732 | 2014-03-18 13:45:08 -0700 | [diff] [blame] | 269 | for (reg = min; reg <= max; reg += map->reg_stride) { |
Maarten ter Huurne | d856fce | 2013-06-03 00:15:26 +0200 | [diff] [blame] | 270 | unsigned int val; |
| 271 | int ret; |
| 272 | |
Dylan Reid | 83f8475 | 2014-03-18 13:45:09 -0700 | [diff] [blame] | 273 | if (regmap_volatile(map, reg) || |
| 274 | !regmap_writeable(map, reg)) |
Maarten ter Huurne | d856fce | 2013-06-03 00:15:26 +0200 | [diff] [blame] | 275 | continue; |
| 276 | |
| 277 | ret = regcache_read(map, reg, &val); |
| 278 | if (ret) |
| 279 | return ret; |
| 280 | |
Kevin Cernekee | 3969fa08 | 2015-05-05 15:14:13 -0700 | [diff] [blame^] | 281 | if (!regcache_reg_needs_sync(map, reg, val)) |
Maarten ter Huurne | d856fce | 2013-06-03 00:15:26 +0200 | [diff] [blame] | 282 | continue; |
| 283 | |
| 284 | map->cache_bypass = 1; |
| 285 | ret = _regmap_write(map, reg, val); |
| 286 | map->cache_bypass = 0; |
Jarkko Nikula | f29a432 | 2014-09-16 14:04:14 +0300 | [diff] [blame] | 287 | if (ret) { |
| 288 | dev_err(map->dev, "Unable to sync register %#x. %d\n", |
| 289 | reg, ret); |
Maarten ter Huurne | d856fce | 2013-06-03 00:15:26 +0200 | [diff] [blame] | 290 | return ret; |
Jarkko Nikula | f29a432 | 2014-09-16 14:04:14 +0300 | [diff] [blame] | 291 | } |
Maarten ter Huurne | d856fce | 2013-06-03 00:15:26 +0200 | [diff] [blame] | 292 | dev_dbg(map->dev, "Synced register %#x, value %#x\n", reg, val); |
| 293 | } |
| 294 | |
| 295 | return 0; |
| 296 | } |
| 297 | |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 298 | /** |
| 299 | * regcache_sync: Sync the register cache with the hardware. |
| 300 | * |
| 301 | * @map: map to configure. |
| 302 | * |
| 303 | * Any registers that should not be synced should be marked as |
| 304 | * volatile. In general drivers can choose not to use the provided |
| 305 | * syncing functionality if they so require. |
| 306 | * |
| 307 | * Return a negative value on failure, 0 on success. |
| 308 | */ |
| 309 | int regcache_sync(struct regmap *map) |
| 310 | { |
Dimitris Papastamos | 954757d | 2011-09-27 11:25:06 +0100 | [diff] [blame] | 311 | int ret = 0; |
Dimitris Papastamos | 954757d | 2011-09-27 11:25:06 +0100 | [diff] [blame] | 312 | unsigned int i; |
Dimitris Papastamos | 5936008 | 2011-09-19 14:34:04 +0100 | [diff] [blame] | 313 | const char *name; |
Dimitris Papastamos | beb1a10 | 2011-09-29 14:36:26 +0100 | [diff] [blame] | 314 | unsigned int bypass; |
Dimitris Papastamos | 5936008 | 2011-09-19 14:34:04 +0100 | [diff] [blame] | 315 | |
Maarten ter Huurne | d856fce | 2013-06-03 00:15:26 +0200 | [diff] [blame] | 316 | BUG_ON(!map->cache_ops); |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 317 | |
Lars-Peter Clausen | 81485f5 | 2013-05-23 15:06:15 +0200 | [diff] [blame] | 318 | map->lock(map->lock_arg); |
Dimitris Papastamos | beb1a10 | 2011-09-29 14:36:26 +0100 | [diff] [blame] | 319 | /* Remember the initial bypass state */ |
| 320 | bypass = map->cache_bypass; |
Dimitris Papastamos | 954757d | 2011-09-27 11:25:06 +0100 | [diff] [blame] | 321 | dev_dbg(map->dev, "Syncing %s cache\n", |
| 322 | map->cache_ops->name); |
| 323 | name = map->cache_ops->name; |
Philipp Zabel | c6b570d | 2015-03-09 12:20:13 +0100 | [diff] [blame] | 324 | trace_regcache_sync(map, name, "start"); |
Mark Brown | 22f0d90 | 2012-01-21 12:01:14 +0000 | [diff] [blame] | 325 | |
Mark Brown | 8ae0d7e | 2011-10-26 10:34:22 +0200 | [diff] [blame] | 326 | if (!map->cache_dirty) |
| 327 | goto out; |
Mark Brown | d9db762 | 2012-01-25 21:06:33 +0000 | [diff] [blame] | 328 | |
Mark Brown | affbe88 | 2013-10-10 21:06:32 +0100 | [diff] [blame] | 329 | map->async = true; |
| 330 | |
Mark Brown | 22f0d90 | 2012-01-21 12:01:14 +0000 | [diff] [blame] | 331 | /* Apply any patch first */ |
Mark Brown | 8a892d6 | 2012-01-25 21:05:48 +0000 | [diff] [blame] | 332 | map->cache_bypass = 1; |
Mark Brown | 22f0d90 | 2012-01-21 12:01:14 +0000 | [diff] [blame] | 333 | for (i = 0; i < map->patch_regs; i++) { |
| 334 | ret = _regmap_write(map, map->patch[i].reg, map->patch[i].def); |
| 335 | if (ret != 0) { |
| 336 | dev_err(map->dev, "Failed to write %x = %x: %d\n", |
| 337 | map->patch[i].reg, map->patch[i].def, ret); |
| 338 | goto out; |
| 339 | } |
| 340 | } |
Mark Brown | 8a892d6 | 2012-01-25 21:05:48 +0000 | [diff] [blame] | 341 | map->cache_bypass = 0; |
Mark Brown | 22f0d90 | 2012-01-21 12:01:14 +0000 | [diff] [blame] | 342 | |
Maarten ter Huurne | d856fce | 2013-06-03 00:15:26 +0200 | [diff] [blame] | 343 | if (map->cache_ops->sync) |
| 344 | ret = map->cache_ops->sync(map, 0, map->max_register); |
| 345 | else |
| 346 | ret = regcache_default_sync(map, 0, map->max_register); |
Dimitris Papastamos | 954757d | 2011-09-27 11:25:06 +0100 | [diff] [blame] | 347 | |
Mark Brown | 6ff7373 | 2012-02-23 22:05:59 +0000 | [diff] [blame] | 348 | if (ret == 0) |
| 349 | map->cache_dirty = false; |
| 350 | |
Dimitris Papastamos | 954757d | 2011-09-27 11:25:06 +0100 | [diff] [blame] | 351 | out: |
Dimitris Papastamos | beb1a10 | 2011-09-29 14:36:26 +0100 | [diff] [blame] | 352 | /* Restore the bypass state */ |
Mark Brown | affbe88 | 2013-10-10 21:06:32 +0100 | [diff] [blame] | 353 | map->async = false; |
Dimitris Papastamos | beb1a10 | 2011-09-29 14:36:26 +0100 | [diff] [blame] | 354 | map->cache_bypass = bypass; |
Lars-Peter Clausen | 81485f5 | 2013-05-23 15:06:15 +0200 | [diff] [blame] | 355 | map->unlock(map->lock_arg); |
Dimitris Papastamos | 954757d | 2011-09-27 11:25:06 +0100 | [diff] [blame] | 356 | |
Mark Brown | affbe88 | 2013-10-10 21:06:32 +0100 | [diff] [blame] | 357 | regmap_async_complete(map); |
| 358 | |
Philipp Zabel | c6b570d | 2015-03-09 12:20:13 +0100 | [diff] [blame] | 359 | trace_regcache_sync(map, name, "stop"); |
Mark Brown | affbe88 | 2013-10-10 21:06:32 +0100 | [diff] [blame] | 360 | |
Dimitris Papastamos | 954757d | 2011-09-27 11:25:06 +0100 | [diff] [blame] | 361 | return ret; |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 362 | } |
| 363 | EXPORT_SYMBOL_GPL(regcache_sync); |
| 364 | |
Mark Brown | 92afb28 | 2011-09-19 18:22:14 +0100 | [diff] [blame] | 365 | /** |
Mark Brown | 4d4cfd1 | 2012-02-23 20:53:37 +0000 | [diff] [blame] | 366 | * regcache_sync_region: Sync part of the register cache with the hardware. |
| 367 | * |
| 368 | * @map: map to sync. |
| 369 | * @min: first register to sync |
| 370 | * @max: last register to sync |
| 371 | * |
| 372 | * Write all non-default register values in the specified region to |
| 373 | * the hardware. |
| 374 | * |
| 375 | * Return a negative value on failure, 0 on success. |
| 376 | */ |
| 377 | int regcache_sync_region(struct regmap *map, unsigned int min, |
| 378 | unsigned int max) |
| 379 | { |
| 380 | int ret = 0; |
| 381 | const char *name; |
| 382 | unsigned int bypass; |
| 383 | |
Maarten ter Huurne | d856fce | 2013-06-03 00:15:26 +0200 | [diff] [blame] | 384 | BUG_ON(!map->cache_ops); |
Mark Brown | 4d4cfd1 | 2012-02-23 20:53:37 +0000 | [diff] [blame] | 385 | |
Lars-Peter Clausen | 81485f5 | 2013-05-23 15:06:15 +0200 | [diff] [blame] | 386 | map->lock(map->lock_arg); |
Mark Brown | 4d4cfd1 | 2012-02-23 20:53:37 +0000 | [diff] [blame] | 387 | |
| 388 | /* Remember the initial bypass state */ |
| 389 | bypass = map->cache_bypass; |
| 390 | |
| 391 | name = map->cache_ops->name; |
| 392 | dev_dbg(map->dev, "Syncing %s cache from %d-%d\n", name, min, max); |
| 393 | |
Philipp Zabel | c6b570d | 2015-03-09 12:20:13 +0100 | [diff] [blame] | 394 | trace_regcache_sync(map, name, "start region"); |
Mark Brown | 4d4cfd1 | 2012-02-23 20:53:37 +0000 | [diff] [blame] | 395 | |
| 396 | if (!map->cache_dirty) |
| 397 | goto out; |
| 398 | |
Mark Brown | affbe88 | 2013-10-10 21:06:32 +0100 | [diff] [blame] | 399 | map->async = true; |
| 400 | |
Maarten ter Huurne | d856fce | 2013-06-03 00:15:26 +0200 | [diff] [blame] | 401 | if (map->cache_ops->sync) |
| 402 | ret = map->cache_ops->sync(map, min, max); |
| 403 | else |
| 404 | ret = regcache_default_sync(map, min, max); |
Mark Brown | 4d4cfd1 | 2012-02-23 20:53:37 +0000 | [diff] [blame] | 405 | |
| 406 | out: |
Mark Brown | 4d4cfd1 | 2012-02-23 20:53:37 +0000 | [diff] [blame] | 407 | /* Restore the bypass state */ |
| 408 | map->cache_bypass = bypass; |
Mark Brown | affbe88 | 2013-10-10 21:06:32 +0100 | [diff] [blame] | 409 | map->async = false; |
Lars-Peter Clausen | 81485f5 | 2013-05-23 15:06:15 +0200 | [diff] [blame] | 410 | map->unlock(map->lock_arg); |
Mark Brown | 4d4cfd1 | 2012-02-23 20:53:37 +0000 | [diff] [blame] | 411 | |
Mark Brown | affbe88 | 2013-10-10 21:06:32 +0100 | [diff] [blame] | 412 | regmap_async_complete(map); |
| 413 | |
Philipp Zabel | c6b570d | 2015-03-09 12:20:13 +0100 | [diff] [blame] | 414 | trace_regcache_sync(map, name, "stop region"); |
Mark Brown | affbe88 | 2013-10-10 21:06:32 +0100 | [diff] [blame] | 415 | |
Mark Brown | 4d4cfd1 | 2012-02-23 20:53:37 +0000 | [diff] [blame] | 416 | return ret; |
| 417 | } |
Mark Brown | e466de0 | 2012-04-03 13:08:53 +0100 | [diff] [blame] | 418 | EXPORT_SYMBOL_GPL(regcache_sync_region); |
Mark Brown | 4d4cfd1 | 2012-02-23 20:53:37 +0000 | [diff] [blame] | 419 | |
| 420 | /** |
Mark Brown | 697e85b | 2013-05-08 13:55:22 +0100 | [diff] [blame] | 421 | * regcache_drop_region: Discard part of the register cache |
| 422 | * |
| 423 | * @map: map to operate on |
| 424 | * @min: first register to discard |
| 425 | * @max: last register to discard |
| 426 | * |
| 427 | * Discard part of the register cache. |
| 428 | * |
| 429 | * Return a negative value on failure, 0 on success. |
| 430 | */ |
| 431 | int regcache_drop_region(struct regmap *map, unsigned int min, |
| 432 | unsigned int max) |
| 433 | { |
Mark Brown | 697e85b | 2013-05-08 13:55:22 +0100 | [diff] [blame] | 434 | int ret = 0; |
| 435 | |
Lars-Peter Clausen | 3f4ff56 | 2013-08-29 10:26:34 +0200 | [diff] [blame] | 436 | if (!map->cache_ops || !map->cache_ops->drop) |
Mark Brown | 697e85b | 2013-05-08 13:55:22 +0100 | [diff] [blame] | 437 | return -EINVAL; |
| 438 | |
Lars-Peter Clausen | 81485f5 | 2013-05-23 15:06:15 +0200 | [diff] [blame] | 439 | map->lock(map->lock_arg); |
Mark Brown | 697e85b | 2013-05-08 13:55:22 +0100 | [diff] [blame] | 440 | |
Philipp Zabel | c6b570d | 2015-03-09 12:20:13 +0100 | [diff] [blame] | 441 | trace_regcache_drop_region(map, min, max); |
Mark Brown | 697e85b | 2013-05-08 13:55:22 +0100 | [diff] [blame] | 442 | |
Lars-Peter Clausen | 3f4ff56 | 2013-08-29 10:26:34 +0200 | [diff] [blame] | 443 | ret = map->cache_ops->drop(map, min, max); |
Mark Brown | 697e85b | 2013-05-08 13:55:22 +0100 | [diff] [blame] | 444 | |
Lars-Peter Clausen | 81485f5 | 2013-05-23 15:06:15 +0200 | [diff] [blame] | 445 | map->unlock(map->lock_arg); |
Mark Brown | 697e85b | 2013-05-08 13:55:22 +0100 | [diff] [blame] | 446 | |
| 447 | return ret; |
| 448 | } |
| 449 | EXPORT_SYMBOL_GPL(regcache_drop_region); |
| 450 | |
| 451 | /** |
Mark Brown | 92afb28 | 2011-09-19 18:22:14 +0100 | [diff] [blame] | 452 | * regcache_cache_only: Put a register map into cache only mode |
| 453 | * |
| 454 | * @map: map to configure |
| 455 | * @cache_only: flag if changes should be written to the hardware |
| 456 | * |
| 457 | * When a register map is marked as cache only writes to the register |
| 458 | * map API will only update the register cache, they will not cause |
| 459 | * any hardware changes. This is useful for allowing portions of |
| 460 | * drivers to act as though the device were functioning as normal when |
| 461 | * it is disabled for power saving reasons. |
| 462 | */ |
| 463 | void regcache_cache_only(struct regmap *map, bool enable) |
| 464 | { |
Lars-Peter Clausen | 81485f5 | 2013-05-23 15:06:15 +0200 | [diff] [blame] | 465 | map->lock(map->lock_arg); |
Dimitris Papastamos | ac77a76 | 2011-09-29 14:36:28 +0100 | [diff] [blame] | 466 | WARN_ON(map->cache_bypass && enable); |
Mark Brown | 92afb28 | 2011-09-19 18:22:14 +0100 | [diff] [blame] | 467 | map->cache_only = enable; |
Philipp Zabel | c6b570d | 2015-03-09 12:20:13 +0100 | [diff] [blame] | 468 | trace_regmap_cache_only(map, enable); |
Lars-Peter Clausen | 81485f5 | 2013-05-23 15:06:15 +0200 | [diff] [blame] | 469 | map->unlock(map->lock_arg); |
Mark Brown | 92afb28 | 2011-09-19 18:22:14 +0100 | [diff] [blame] | 470 | } |
| 471 | EXPORT_SYMBOL_GPL(regcache_cache_only); |
| 472 | |
Dimitris Papastamos | 6eb0f5e | 2011-09-29 14:36:27 +0100 | [diff] [blame] | 473 | /** |
Mark Brown | 8ae0d7e | 2011-10-26 10:34:22 +0200 | [diff] [blame] | 474 | * regcache_mark_dirty: Mark the register cache as dirty |
| 475 | * |
| 476 | * @map: map to mark |
| 477 | * |
| 478 | * Mark the register cache as dirty, for example due to the device |
| 479 | * having been powered down for suspend. If the cache is not marked |
| 480 | * as dirty then the cache sync will be suppressed. |
| 481 | */ |
| 482 | void regcache_mark_dirty(struct regmap *map) |
| 483 | { |
Lars-Peter Clausen | 81485f5 | 2013-05-23 15:06:15 +0200 | [diff] [blame] | 484 | map->lock(map->lock_arg); |
Mark Brown | 8ae0d7e | 2011-10-26 10:34:22 +0200 | [diff] [blame] | 485 | map->cache_dirty = true; |
Lars-Peter Clausen | 81485f5 | 2013-05-23 15:06:15 +0200 | [diff] [blame] | 486 | map->unlock(map->lock_arg); |
Mark Brown | 8ae0d7e | 2011-10-26 10:34:22 +0200 | [diff] [blame] | 487 | } |
| 488 | EXPORT_SYMBOL_GPL(regcache_mark_dirty); |
| 489 | |
| 490 | /** |
Dimitris Papastamos | 6eb0f5e | 2011-09-29 14:36:27 +0100 | [diff] [blame] | 491 | * regcache_cache_bypass: Put a register map into cache bypass mode |
| 492 | * |
| 493 | * @map: map to configure |
Dimitris Papastamos | 0eef6b0 | 2011-10-03 06:54:16 +0100 | [diff] [blame] | 494 | * @cache_bypass: flag if changes should not be written to the hardware |
Dimitris Papastamos | 6eb0f5e | 2011-09-29 14:36:27 +0100 | [diff] [blame] | 495 | * |
| 496 | * When a register map is marked with the cache bypass option, writes |
| 497 | * to the register map API will only update the hardware and not the |
| 498 | * the cache directly. This is useful when syncing the cache back to |
| 499 | * the hardware. |
| 500 | */ |
| 501 | void regcache_cache_bypass(struct regmap *map, bool enable) |
| 502 | { |
Lars-Peter Clausen | 81485f5 | 2013-05-23 15:06:15 +0200 | [diff] [blame] | 503 | map->lock(map->lock_arg); |
Dimitris Papastamos | ac77a76 | 2011-09-29 14:36:28 +0100 | [diff] [blame] | 504 | WARN_ON(map->cache_only && enable); |
Dimitris Papastamos | 6eb0f5e | 2011-09-29 14:36:27 +0100 | [diff] [blame] | 505 | map->cache_bypass = enable; |
Philipp Zabel | c6b570d | 2015-03-09 12:20:13 +0100 | [diff] [blame] | 506 | trace_regmap_cache_bypass(map, enable); |
Lars-Peter Clausen | 81485f5 | 2013-05-23 15:06:15 +0200 | [diff] [blame] | 507 | map->unlock(map->lock_arg); |
Dimitris Papastamos | 6eb0f5e | 2011-09-29 14:36:27 +0100 | [diff] [blame] | 508 | } |
| 509 | EXPORT_SYMBOL_GPL(regcache_cache_bypass); |
| 510 | |
Mark Brown | 879082c | 2013-02-21 18:03:13 +0000 | [diff] [blame] | 511 | bool regcache_set_val(struct regmap *map, void *base, unsigned int idx, |
| 512 | unsigned int val) |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 513 | { |
Mark Brown | 325acab | 2013-02-21 18:07:01 +0000 | [diff] [blame] | 514 | if (regcache_get_val(map, base, idx) == val) |
| 515 | return true; |
| 516 | |
Mark Brown | eb4cb76 | 2013-02-21 18:39:47 +0000 | [diff] [blame] | 517 | /* Use device native format if possible */ |
| 518 | if (map->format.format_val) { |
| 519 | map->format.format_val(base + (map->cache_word_size * idx), |
| 520 | val, 0); |
| 521 | return false; |
| 522 | } |
| 523 | |
Mark Brown | 879082c | 2013-02-21 18:03:13 +0000 | [diff] [blame] | 524 | switch (map->cache_word_size) { |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 525 | case 1: { |
| 526 | u8 *cache = base; |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 527 | cache[idx] = val; |
| 528 | break; |
| 529 | } |
| 530 | case 2: { |
| 531 | u16 *cache = base; |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 532 | cache[idx] = val; |
| 533 | break; |
| 534 | } |
Mark Brown | 7d5e525 | 2012-02-17 15:58:25 -0800 | [diff] [blame] | 535 | case 4: { |
| 536 | u32 *cache = base; |
Mark Brown | 7d5e525 | 2012-02-17 15:58:25 -0800 | [diff] [blame] | 537 | cache[idx] = val; |
| 538 | break; |
| 539 | } |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 540 | default: |
| 541 | BUG(); |
| 542 | } |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 543 | return false; |
| 544 | } |
| 545 | |
Mark Brown | 879082c | 2013-02-21 18:03:13 +0000 | [diff] [blame] | 546 | unsigned int regcache_get_val(struct regmap *map, const void *base, |
| 547 | unsigned int idx) |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 548 | { |
| 549 | if (!base) |
| 550 | return -EINVAL; |
| 551 | |
Mark Brown | eb4cb76 | 2013-02-21 18:39:47 +0000 | [diff] [blame] | 552 | /* Use device native format if possible */ |
| 553 | if (map->format.parse_val) |
Mark Brown | 8817796 | 2013-03-13 19:29:36 +0000 | [diff] [blame] | 554 | return map->format.parse_val(regcache_get_val_addr(map, base, |
| 555 | idx)); |
Mark Brown | eb4cb76 | 2013-02-21 18:39:47 +0000 | [diff] [blame] | 556 | |
Mark Brown | 879082c | 2013-02-21 18:03:13 +0000 | [diff] [blame] | 557 | switch (map->cache_word_size) { |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 558 | case 1: { |
| 559 | const u8 *cache = base; |
| 560 | return cache[idx]; |
| 561 | } |
| 562 | case 2: { |
| 563 | const u16 *cache = base; |
| 564 | return cache[idx]; |
| 565 | } |
Mark Brown | 7d5e525 | 2012-02-17 15:58:25 -0800 | [diff] [blame] | 566 | case 4: { |
| 567 | const u32 *cache = base; |
| 568 | return cache[idx]; |
| 569 | } |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 570 | default: |
| 571 | BUG(); |
| 572 | } |
| 573 | /* unreachable */ |
| 574 | return -1; |
| 575 | } |
| 576 | |
Mark Brown | f094fea | 2011-10-04 22:05:47 +0100 | [diff] [blame] | 577 | static int regcache_default_cmp(const void *a, const void *b) |
Dimitris Papastamos | c08604b | 2011-10-03 10:50:14 +0100 | [diff] [blame] | 578 | { |
| 579 | const struct reg_default *_a = a; |
| 580 | const struct reg_default *_b = b; |
| 581 | |
| 582 | return _a->reg - _b->reg; |
| 583 | } |
| 584 | |
Mark Brown | f094fea | 2011-10-04 22:05:47 +0100 | [diff] [blame] | 585 | int regcache_lookup_reg(struct regmap *map, unsigned int reg) |
| 586 | { |
| 587 | struct reg_default key; |
| 588 | struct reg_default *r; |
| 589 | |
| 590 | key.reg = reg; |
| 591 | key.def = 0; |
| 592 | |
| 593 | r = bsearch(&key, map->reg_defaults, map->num_reg_defaults, |
| 594 | sizeof(struct reg_default), regcache_default_cmp); |
| 595 | |
| 596 | if (r) |
| 597 | return r - map->reg_defaults; |
| 598 | else |
Mark Brown | 6e6ace0 | 2011-10-09 13:23:31 +0100 | [diff] [blame] | 599 | return -ENOENT; |
Mark Brown | f094fea | 2011-10-04 22:05:47 +0100 | [diff] [blame] | 600 | } |
Mark Brown | f8bd822 | 2013-03-29 19:32:28 +0000 | [diff] [blame] | 601 | |
Lars-Peter Clausen | 3f4ff56 | 2013-08-29 10:26:34 +0200 | [diff] [blame] | 602 | static bool regcache_reg_present(unsigned long *cache_present, unsigned int idx) |
| 603 | { |
| 604 | if (!cache_present) |
| 605 | return true; |
| 606 | |
| 607 | return test_bit(idx, cache_present); |
| 608 | } |
| 609 | |
Mark Brown | cfdeb8c | 2013-03-29 20:12:21 +0000 | [diff] [blame] | 610 | static int regcache_sync_block_single(struct regmap *map, void *block, |
Lars-Peter Clausen | 3f4ff56 | 2013-08-29 10:26:34 +0200 | [diff] [blame] | 611 | unsigned long *cache_present, |
Mark Brown | cfdeb8c | 2013-03-29 20:12:21 +0000 | [diff] [blame] | 612 | unsigned int block_base, |
| 613 | unsigned int start, unsigned int end) |
| 614 | { |
| 615 | unsigned int i, regtmp, val; |
| 616 | int ret; |
| 617 | |
| 618 | for (i = start; i < end; i++) { |
| 619 | regtmp = block_base + (i * map->reg_stride); |
| 620 | |
Takashi Iwai | 4ceba98 | 2015-03-04 15:29:17 +0100 | [diff] [blame] | 621 | if (!regcache_reg_present(cache_present, i) || |
| 622 | !regmap_writeable(map, regtmp)) |
Mark Brown | cfdeb8c | 2013-03-29 20:12:21 +0000 | [diff] [blame] | 623 | continue; |
| 624 | |
| 625 | val = regcache_get_val(map, block, i); |
Kevin Cernekee | 3969fa08 | 2015-05-05 15:14:13 -0700 | [diff] [blame^] | 626 | if (!regcache_reg_needs_sync(map, regtmp, val)) |
Mark Brown | cfdeb8c | 2013-03-29 20:12:21 +0000 | [diff] [blame] | 627 | continue; |
| 628 | |
| 629 | map->cache_bypass = 1; |
| 630 | |
| 631 | ret = _regmap_write(map, regtmp, val); |
| 632 | |
| 633 | map->cache_bypass = 0; |
Jarkko Nikula | f29a432 | 2014-09-16 14:04:14 +0300 | [diff] [blame] | 634 | if (ret != 0) { |
| 635 | dev_err(map->dev, "Unable to sync register %#x. %d\n", |
| 636 | regtmp, ret); |
Mark Brown | cfdeb8c | 2013-03-29 20:12:21 +0000 | [diff] [blame] | 637 | return ret; |
Jarkko Nikula | f29a432 | 2014-09-16 14:04:14 +0300 | [diff] [blame] | 638 | } |
Mark Brown | cfdeb8c | 2013-03-29 20:12:21 +0000 | [diff] [blame] | 639 | dev_dbg(map->dev, "Synced register %#x, value %#x\n", |
| 640 | regtmp, val); |
| 641 | } |
| 642 | |
| 643 | return 0; |
| 644 | } |
| 645 | |
Mark Brown | 75a5f89 | 2013-03-29 20:50:07 +0000 | [diff] [blame] | 646 | static int regcache_sync_block_raw_flush(struct regmap *map, const void **data, |
| 647 | unsigned int base, unsigned int cur) |
| 648 | { |
| 649 | size_t val_bytes = map->format.val_bytes; |
| 650 | int ret, count; |
| 651 | |
| 652 | if (*data == NULL) |
| 653 | return 0; |
| 654 | |
Dylan Reid | 78ba73e | 2014-01-24 15:40:39 -0800 | [diff] [blame] | 655 | count = (cur - base) / map->reg_stride; |
Mark Brown | 75a5f89 | 2013-03-29 20:50:07 +0000 | [diff] [blame] | 656 | |
Stratos Karafotis | 9659293 | 2013-04-04 19:40:45 +0300 | [diff] [blame] | 657 | dev_dbg(map->dev, "Writing %zu bytes for %d registers from 0x%x-0x%x\n", |
Dylan Reid | 78ba73e | 2014-01-24 15:40:39 -0800 | [diff] [blame] | 658 | count * val_bytes, count, base, cur - map->reg_stride); |
Mark Brown | 75a5f89 | 2013-03-29 20:50:07 +0000 | [diff] [blame] | 659 | |
| 660 | map->cache_bypass = 1; |
| 661 | |
Mark Brown | 0a81980 | 2013-10-09 12:28:52 +0100 | [diff] [blame] | 662 | ret = _regmap_raw_write(map, base, *data, count * val_bytes); |
Jarkko Nikula | f29a432 | 2014-09-16 14:04:14 +0300 | [diff] [blame] | 663 | if (ret) |
| 664 | dev_err(map->dev, "Unable to sync registers %#x-%#x. %d\n", |
| 665 | base, cur - map->reg_stride, ret); |
Mark Brown | 75a5f89 | 2013-03-29 20:50:07 +0000 | [diff] [blame] | 666 | |
| 667 | map->cache_bypass = 0; |
| 668 | |
| 669 | *data = NULL; |
| 670 | |
| 671 | return ret; |
| 672 | } |
| 673 | |
Sachin Kamat | f52687a | 2013-04-04 14:36:18 +0530 | [diff] [blame] | 674 | static int regcache_sync_block_raw(struct regmap *map, void *block, |
Lars-Peter Clausen | 3f4ff56 | 2013-08-29 10:26:34 +0200 | [diff] [blame] | 675 | unsigned long *cache_present, |
Mark Brown | cfdeb8c | 2013-03-29 20:12:21 +0000 | [diff] [blame] | 676 | unsigned int block_base, unsigned int start, |
| 677 | unsigned int end) |
Mark Brown | f8bd822 | 2013-03-29 19:32:28 +0000 | [diff] [blame] | 678 | { |
Mark Brown | 75a5f89 | 2013-03-29 20:50:07 +0000 | [diff] [blame] | 679 | unsigned int i, val; |
| 680 | unsigned int regtmp = 0; |
| 681 | unsigned int base = 0; |
| 682 | const void *data = NULL; |
Mark Brown | f8bd822 | 2013-03-29 19:32:28 +0000 | [diff] [blame] | 683 | int ret; |
| 684 | |
| 685 | for (i = start; i < end; i++) { |
| 686 | regtmp = block_base + (i * map->reg_stride); |
| 687 | |
Takashi Iwai | 4ceba98 | 2015-03-04 15:29:17 +0100 | [diff] [blame] | 688 | if (!regcache_reg_present(cache_present, i) || |
| 689 | !regmap_writeable(map, regtmp)) { |
Mark Brown | 75a5f89 | 2013-03-29 20:50:07 +0000 | [diff] [blame] | 690 | ret = regcache_sync_block_raw_flush(map, &data, |
| 691 | base, regtmp); |
| 692 | if (ret != 0) |
| 693 | return ret; |
Mark Brown | f8bd822 | 2013-03-29 19:32:28 +0000 | [diff] [blame] | 694 | continue; |
Mark Brown | 75a5f89 | 2013-03-29 20:50:07 +0000 | [diff] [blame] | 695 | } |
Mark Brown | f8bd822 | 2013-03-29 19:32:28 +0000 | [diff] [blame] | 696 | |
| 697 | val = regcache_get_val(map, block, i); |
Kevin Cernekee | 3969fa08 | 2015-05-05 15:14:13 -0700 | [diff] [blame^] | 698 | if (!regcache_reg_needs_sync(map, regtmp, val)) { |
Mark Brown | 75a5f89 | 2013-03-29 20:50:07 +0000 | [diff] [blame] | 699 | ret = regcache_sync_block_raw_flush(map, &data, |
| 700 | base, regtmp); |
| 701 | if (ret != 0) |
| 702 | return ret; |
Mark Brown | f8bd822 | 2013-03-29 19:32:28 +0000 | [diff] [blame] | 703 | continue; |
Mark Brown | 75a5f89 | 2013-03-29 20:50:07 +0000 | [diff] [blame] | 704 | } |
Mark Brown | f8bd822 | 2013-03-29 19:32:28 +0000 | [diff] [blame] | 705 | |
Mark Brown | 75a5f89 | 2013-03-29 20:50:07 +0000 | [diff] [blame] | 706 | if (!data) { |
| 707 | data = regcache_get_val_addr(map, block, i); |
| 708 | base = regtmp; |
| 709 | } |
Mark Brown | f8bd822 | 2013-03-29 19:32:28 +0000 | [diff] [blame] | 710 | } |
| 711 | |
Lars-Peter Clausen | 2d49b59 | 2013-08-05 11:21:29 +0200 | [diff] [blame] | 712 | return regcache_sync_block_raw_flush(map, &data, base, regtmp + |
| 713 | map->reg_stride); |
Mark Brown | f8bd822 | 2013-03-29 19:32:28 +0000 | [diff] [blame] | 714 | } |
Mark Brown | cfdeb8c | 2013-03-29 20:12:21 +0000 | [diff] [blame] | 715 | |
| 716 | int regcache_sync_block(struct regmap *map, void *block, |
Lars-Peter Clausen | 3f4ff56 | 2013-08-29 10:26:34 +0200 | [diff] [blame] | 717 | unsigned long *cache_present, |
Mark Brown | cfdeb8c | 2013-03-29 20:12:21 +0000 | [diff] [blame] | 718 | unsigned int block_base, unsigned int start, |
| 719 | unsigned int end) |
| 720 | { |
Mark Brown | 5c1ebe7 | 2014-08-27 13:09:12 +0100 | [diff] [blame] | 721 | if (regmap_can_raw_write(map) && !map->use_single_rw) |
Lars-Peter Clausen | 3f4ff56 | 2013-08-29 10:26:34 +0200 | [diff] [blame] | 722 | return regcache_sync_block_raw(map, block, cache_present, |
| 723 | block_base, start, end); |
Mark Brown | cfdeb8c | 2013-03-29 20:12:21 +0000 | [diff] [blame] | 724 | else |
Lars-Peter Clausen | 3f4ff56 | 2013-08-29 10:26:34 +0200 | [diff] [blame] | 725 | return regcache_sync_block_single(map, block, cache_present, |
| 726 | block_base, start, end); |
Mark Brown | cfdeb8c | 2013-03-29 20:12:21 +0000 | [diff] [blame] | 727 | } |