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; |
Mark Brown | 3245d46 | 2016-02-02 10:16:51 -0200 | [diff] [blame] | 33 | unsigned int reg, val; |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 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++) |
Maarten ter Huurne | b2c7f5d | 2016-07-29 23:42:12 +0200 | [diff] [blame] | 41 | if (regmap_readable(map, i * map->reg_stride) && |
| 42 | !regmap_volatile(map, i * map->reg_stride)) |
Xiubo Li | fb70067 | 2014-10-09 17:02:57 +0800 | [diff] [blame] | 43 | count++; |
| 44 | |
Maarten ter Huurne | b2c7f5d | 2016-07-29 23:42:12 +0200 | [diff] [blame] | 45 | /* all registers are unreadable or volatile, so just bypass */ |
Xiubo Li | fb70067 | 2014-10-09 17:02:57 +0800 | [diff] [blame] | 46 | 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 Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 57 | if (!map->reg_defaults_raw) { |
Viresh Kumar | 621a5f7 | 2015-09-26 15:04:07 -0700 | [diff] [blame] | 58 | bool cache_bypass = map->cache_bypass; |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 59 | dev_warn(map->dev, "No cache defaults, reading back from HW\n"); |
Laxman Dewangan | df00c79 | 2012-02-17 18:57:26 +0530 | [diff] [blame] | 60 | |
Maciej S. Szmigiero | d51fe1f | 2016-01-13 22:41:12 +0100 | [diff] [blame] | 61 | /* Bypass the cache access till data read from HW */ |
Viresh Kumar | 621a5f7 | 2015-09-26 15:04:07 -0700 | [diff] [blame] | 62 | map->cache_bypass = true; |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 63 | tmp_buf = kmalloc(map->cache_size_raw, GFP_KERNEL); |
Xiubo Li | fb70067 | 2014-10-09 17:02:57 +0800 | [diff] [blame] | 64 | if (!tmp_buf) { |
| 65 | ret = -ENOMEM; |
| 66 | goto err_free; |
| 67 | } |
Mark Brown | eb4cb76 | 2013-02-21 18:39:47 +0000 | [diff] [blame] | 68 | ret = regmap_raw_read(map, 0, tmp_buf, |
Maciej S. Szmigiero | d51fe1f | 2016-01-13 22:41:12 +0100 | [diff] [blame] | 69 | map->cache_size_raw); |
Laxman Dewangan | df00c79 | 2012-02-17 18:57:26 +0530 | [diff] [blame] | 70 | map->cache_bypass = cache_bypass; |
Mark Brown | 3245d46 | 2016-02-02 10:16:51 -0200 | [diff] [blame] | 71 | if (ret == 0) { |
| 72 | map->reg_defaults_raw = tmp_buf; |
| 73 | map->cache_free = 1; |
| 74 | } else { |
| 75 | kfree(tmp_buf); |
| 76 | } |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 77 | } |
| 78 | |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 79 | /* fill the reg_defaults */ |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 80 | for (i = 0, j = 0; i < map->num_reg_defaults_raw; i++) { |
Mark Brown | 3245d46 | 2016-02-02 10:16:51 -0200 | [diff] [blame] | 81 | reg = i * map->reg_stride; |
| 82 | |
| 83 | if (!regmap_readable(map, reg)) |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 84 | continue; |
Mark Brown | 3245d46 | 2016-02-02 10:16:51 -0200 | [diff] [blame] | 85 | |
| 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 Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 105 | map->reg_defaults[j].def = val; |
| 106 | j++; |
| 107 | } |
| 108 | |
| 109 | return 0; |
Lars-Peter Clausen | 021cd61 | 2011-11-14 10:40:16 +0100 | [diff] [blame] | 110 | |
| 111 | err_free: |
Xiubo Li | fb70067 | 2014-10-09 17:02:57 +0800 | [diff] [blame] | 112 | kfree(map->reg_defaults); |
Lars-Peter Clausen | 021cd61 | 2011-11-14 10:40:16 +0100 | [diff] [blame] | 113 | |
| 114 | return ret; |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 115 | } |
| 116 | |
Lars-Peter Clausen | e5e3b8a | 2011-11-16 16:28:16 +0100 | [diff] [blame] | 117 | int regcache_init(struct regmap *map, const struct regmap_config *config) |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 118 | { |
| 119 | int ret; |
| 120 | int i; |
| 121 | void *tmp_buf; |
| 122 | |
Mark Brown | e7a6db3 | 2011-09-19 16:08:03 +0100 | [diff] [blame] | 123 | if (map->cache_type == REGCACHE_NONE) { |
Xiubo Li | 8cfe2fd | 2015-12-11 11:23:19 +0800 | [diff] [blame] | 124 | 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 Brown | e7a6db3 | 2011-09-19 16:08:03 +0100 | [diff] [blame] | 128 | map->cache_bypass = true; |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 129 | return 0; |
Mark Brown | e7a6db3 | 2011-09-19 16:08:03 +0100 | [diff] [blame] | 130 | } |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 131 | |
Xiubo Li | 167f706 | 2015-12-11 11:23:20 +0800 | [diff] [blame] | 132 | 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 Li | 8cfe2fd | 2015-12-11 11:23:19 +0800 | [diff] [blame] | 138 | for (i = 0; i < config->num_reg_defaults; i++) |
| 139 | if (config->reg_defaults[i].reg % map->reg_stride) |
| 140 | return -EINVAL; |
| 141 | |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 142 | 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 Clausen | e5e3b8a | 2011-11-16 16:28:16 +0100 | [diff] [blame] | 152 | 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 Clausen | 064d4db | 2011-11-16 20:34:03 +0100 | [diff] [blame] | 155 | 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 Clausen | e5e3b8a | 2011-11-16 16:28:16 +0100 | [diff] [blame] | 157 | |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 158 | 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 Clausen | 720e461 | 2011-11-16 16:28:17 +0100 | [diff] [blame] | 170 | if (config->reg_defaults) { |
Lars-Peter Clausen | 720e461 | 2011-11-16 16:28:17 +0100 | [diff] [blame] | 171 | tmp_buf = kmemdup(config->reg_defaults, map->num_reg_defaults * |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 172 | sizeof(struct reg_default), GFP_KERNEL); |
| 173 | if (!tmp_buf) |
| 174 | return -ENOMEM; |
| 175 | map->reg_defaults = tmp_buf; |
Mark Brown | 8528bdd | 2011-10-09 13:13:58 +0100 | [diff] [blame] | 176 | } else if (map->num_reg_defaults_raw) { |
Mark Brown | 5fcd256 | 2011-09-29 15:24:54 +0100 | [diff] [blame] | 177 | /* Some devices such as PMICs don't have cache defaults, |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 178 | * 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 Li | fb70067 | 2014-10-09 17:02:57 +0800 | [diff] [blame] | 184 | if (map->cache_bypass) |
| 185 | return 0; |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 186 | } |
| 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 Clausen | bd061c7 | 2011-11-14 10:40:17 +0100 | [diff] [blame] | 194 | ret = map->cache_ops->init(map); |
| 195 | if (ret) |
| 196 | goto err_free; |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 197 | } |
| 198 | return 0; |
Lars-Peter Clausen | bd061c7 | 2011-11-14 10:40:17 +0100 | [diff] [blame] | 199 | |
| 200 | err_free: |
| 201 | kfree(map->reg_defaults); |
| 202 | if (map->cache_free) |
| 203 | kfree(map->reg_defaults_raw); |
| 204 | |
| 205 | return ret; |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | void 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 Keepax | 2cf8e2d | 2017-01-12 11:17:39 +0000 | [diff] [blame] | 227 | * regcache_read - Fetch the value of a given register from the cache. |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 228 | * |
| 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 | */ |
| 235 | int regcache_read(struct regmap *map, |
| 236 | unsigned int reg, unsigned int *value) |
| 237 | { |
Mark Brown | bc7ee55 | 2011-11-30 14:27:08 +0000 | [diff] [blame] | 238 | int ret; |
| 239 | |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 240 | if (map->cache_type == REGCACHE_NONE) |
| 241 | return -ENOSYS; |
| 242 | |
| 243 | BUG_ON(!map->cache_ops); |
| 244 | |
Mark Brown | bc7ee55 | 2011-11-30 14:27:08 +0000 | [diff] [blame] | 245 | if (!regmap_volatile(map, reg)) { |
| 246 | ret = map->cache_ops->read(map, reg, value); |
| 247 | |
| 248 | if (ret == 0) |
Philipp Zabel | c6b570d | 2015-03-09 12:20:13 +0100 | [diff] [blame] | 249 | trace_regmap_reg_read_cache(map, reg, *value); |
Mark Brown | bc7ee55 | 2011-11-30 14:27:08 +0000 | [diff] [blame] | 250 | |
| 251 | return ret; |
| 252 | } |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 253 | |
| 254 | return -EINVAL; |
| 255 | } |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 256 | |
| 257 | /** |
Charles Keepax | 2cf8e2d | 2017-01-12 11:17:39 +0000 | [diff] [blame] | 258 | * regcache_write - Set the value of a given register in the cache. |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 259 | * |
| 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 | */ |
| 266 | int 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 Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 274 | if (!regmap_volatile(map, reg)) |
| 275 | return map->cache_ops->write(map, reg, value); |
| 276 | |
| 277 | return 0; |
| 278 | } |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 279 | |
Kevin Cernekee | 3969fa08 | 2015-05-05 15:14:13 -0700 | [diff] [blame] | 280 | static bool regcache_reg_needs_sync(struct regmap *map, unsigned int reg, |
| 281 | unsigned int val) |
| 282 | { |
| 283 | int ret; |
| 284 | |
Kevin Cernekee | 1c79771 | 2015-05-05 15:14:14 -0700 | [diff] [blame] | 285 | /* If we don't know the chip just got reset, then sync everything. */ |
| 286 | if (!map->no_sync_defaults) |
| 287 | return true; |
| 288 | |
Kevin Cernekee | 3969fa08 | 2015-05-05 15:14:13 -0700 | [diff] [blame] | 289 | /* 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 Huurne | d856fce | 2013-06-03 00:15:26 +0200 | [diff] [blame] | 296 | static int regcache_default_sync(struct regmap *map, unsigned int min, |
| 297 | unsigned int max) |
| 298 | { |
| 299 | unsigned int reg; |
| 300 | |
Dylan Reid | 7561732 | 2014-03-18 13:45:08 -0700 | [diff] [blame] | 301 | for (reg = min; reg <= max; reg += map->reg_stride) { |
Maarten ter Huurne | d856fce | 2013-06-03 00:15:26 +0200 | [diff] [blame] | 302 | unsigned int val; |
| 303 | int ret; |
| 304 | |
Dylan Reid | 83f8475 | 2014-03-18 13:45:09 -0700 | [diff] [blame] | 305 | if (regmap_volatile(map, reg) || |
| 306 | !regmap_writeable(map, reg)) |
Maarten ter Huurne | d856fce | 2013-06-03 00:15:26 +0200 | [diff] [blame] | 307 | continue; |
| 308 | |
| 309 | ret = regcache_read(map, reg, &val); |
| 310 | if (ret) |
| 311 | return ret; |
| 312 | |
Kevin Cernekee | 3969fa08 | 2015-05-05 15:14:13 -0700 | [diff] [blame] | 313 | if (!regcache_reg_needs_sync(map, reg, val)) |
Maarten ter Huurne | d856fce | 2013-06-03 00:15:26 +0200 | [diff] [blame] | 314 | continue; |
| 315 | |
Viresh Kumar | 621a5f7 | 2015-09-26 15:04:07 -0700 | [diff] [blame] | 316 | map->cache_bypass = true; |
Maarten ter Huurne | d856fce | 2013-06-03 00:15:26 +0200 | [diff] [blame] | 317 | ret = _regmap_write(map, reg, val); |
Viresh Kumar | 621a5f7 | 2015-09-26 15:04:07 -0700 | [diff] [blame] | 318 | map->cache_bypass = false; |
Jarkko Nikula | f29a432 | 2014-09-16 14:04:14 +0300 | [diff] [blame] | 319 | if (ret) { |
| 320 | dev_err(map->dev, "Unable to sync register %#x. %d\n", |
| 321 | reg, ret); |
Maarten ter Huurne | d856fce | 2013-06-03 00:15:26 +0200 | [diff] [blame] | 322 | return ret; |
Jarkko Nikula | f29a432 | 2014-09-16 14:04:14 +0300 | [diff] [blame] | 323 | } |
Maarten ter Huurne | d856fce | 2013-06-03 00:15:26 +0200 | [diff] [blame] | 324 | dev_dbg(map->dev, "Synced register %#x, value %#x\n", reg, val); |
| 325 | } |
| 326 | |
| 327 | return 0; |
| 328 | } |
| 329 | |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 330 | /** |
Charles Keepax | 2cf8e2d | 2017-01-12 11:17:39 +0000 | [diff] [blame] | 331 | * regcache_sync - Sync the register cache with the hardware. |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 332 | * |
| 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 | */ |
| 341 | int regcache_sync(struct regmap *map) |
| 342 | { |
Dimitris Papastamos | 954757d | 2011-09-27 11:25:06 +0100 | [diff] [blame] | 343 | int ret = 0; |
Dimitris Papastamos | 954757d | 2011-09-27 11:25:06 +0100 | [diff] [blame] | 344 | unsigned int i; |
Dimitris Papastamos | 5936008 | 2011-09-19 14:34:04 +0100 | [diff] [blame] | 345 | const char *name; |
Viresh Kumar | 621a5f7 | 2015-09-26 15:04:07 -0700 | [diff] [blame] | 346 | bool bypass; |
Dimitris Papastamos | 5936008 | 2011-09-19 14:34:04 +0100 | [diff] [blame] | 347 | |
Maarten ter Huurne | d856fce | 2013-06-03 00:15:26 +0200 | [diff] [blame] | 348 | BUG_ON(!map->cache_ops); |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 349 | |
Lars-Peter Clausen | 81485f5 | 2013-05-23 15:06:15 +0200 | [diff] [blame] | 350 | map->lock(map->lock_arg); |
Dimitris Papastamos | beb1a10 | 2011-09-29 14:36:26 +0100 | [diff] [blame] | 351 | /* Remember the initial bypass state */ |
| 352 | bypass = map->cache_bypass; |
Dimitris Papastamos | 954757d | 2011-09-27 11:25:06 +0100 | [diff] [blame] | 353 | dev_dbg(map->dev, "Syncing %s cache\n", |
| 354 | map->cache_ops->name); |
| 355 | name = map->cache_ops->name; |
Philipp Zabel | c6b570d | 2015-03-09 12:20:13 +0100 | [diff] [blame] | 356 | trace_regcache_sync(map, name, "start"); |
Mark Brown | 22f0d90 | 2012-01-21 12:01:14 +0000 | [diff] [blame] | 357 | |
Mark Brown | 8ae0d7e | 2011-10-26 10:34:22 +0200 | [diff] [blame] | 358 | if (!map->cache_dirty) |
| 359 | goto out; |
Mark Brown | d9db762 | 2012-01-25 21:06:33 +0000 | [diff] [blame] | 360 | |
Mark Brown | affbe88 | 2013-10-10 21:06:32 +0100 | [diff] [blame] | 361 | map->async = true; |
| 362 | |
Mark Brown | 22f0d90 | 2012-01-21 12:01:14 +0000 | [diff] [blame] | 363 | /* Apply any patch first */ |
Viresh Kumar | 621a5f7 | 2015-09-26 15:04:07 -0700 | [diff] [blame] | 364 | map->cache_bypass = true; |
Mark Brown | 22f0d90 | 2012-01-21 12:01:14 +0000 | [diff] [blame] | 365 | 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 Kumar | 621a5f7 | 2015-09-26 15:04:07 -0700 | [diff] [blame] | 373 | map->cache_bypass = false; |
Mark Brown | 22f0d90 | 2012-01-21 12:01:14 +0000 | [diff] [blame] | 374 | |
Maarten ter Huurne | d856fce | 2013-06-03 00:15:26 +0200 | [diff] [blame] | 375 | 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 Papastamos | 954757d | 2011-09-27 11:25:06 +0100 | [diff] [blame] | 379 | |
Mark Brown | 6ff7373 | 2012-02-23 22:05:59 +0000 | [diff] [blame] | 380 | if (ret == 0) |
| 381 | map->cache_dirty = false; |
| 382 | |
Dimitris Papastamos | 954757d | 2011-09-27 11:25:06 +0100 | [diff] [blame] | 383 | out: |
Dimitris Papastamos | beb1a10 | 2011-09-29 14:36:26 +0100 | [diff] [blame] | 384 | /* Restore the bypass state */ |
Mark Brown | affbe88 | 2013-10-10 21:06:32 +0100 | [diff] [blame] | 385 | map->async = false; |
Dimitris Papastamos | beb1a10 | 2011-09-29 14:36:26 +0100 | [diff] [blame] | 386 | map->cache_bypass = bypass; |
Kevin Cernekee | 1c79771 | 2015-05-05 15:14:14 -0700 | [diff] [blame] | 387 | map->no_sync_defaults = false; |
Lars-Peter Clausen | 81485f5 | 2013-05-23 15:06:15 +0200 | [diff] [blame] | 388 | map->unlock(map->lock_arg); |
Dimitris Papastamos | 954757d | 2011-09-27 11:25:06 +0100 | [diff] [blame] | 389 | |
Mark Brown | affbe88 | 2013-10-10 21:06:32 +0100 | [diff] [blame] | 390 | regmap_async_complete(map); |
| 391 | |
Philipp Zabel | c6b570d | 2015-03-09 12:20:13 +0100 | [diff] [blame] | 392 | trace_regcache_sync(map, name, "stop"); |
Mark Brown | affbe88 | 2013-10-10 21:06:32 +0100 | [diff] [blame] | 393 | |
Dimitris Papastamos | 954757d | 2011-09-27 11:25:06 +0100 | [diff] [blame] | 394 | return ret; |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 395 | } |
| 396 | EXPORT_SYMBOL_GPL(regcache_sync); |
| 397 | |
Mark Brown | 92afb28 | 2011-09-19 18:22:14 +0100 | [diff] [blame] | 398 | /** |
Charles Keepax | 2cf8e2d | 2017-01-12 11:17:39 +0000 | [diff] [blame] | 399 | * regcache_sync_region - Sync part of the register cache with the hardware. |
Mark Brown | 4d4cfd1 | 2012-02-23 20:53:37 +0000 | [diff] [blame] | 400 | * |
| 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 | */ |
| 410 | int regcache_sync_region(struct regmap *map, unsigned int min, |
| 411 | unsigned int max) |
| 412 | { |
| 413 | int ret = 0; |
| 414 | const char *name; |
Viresh Kumar | 621a5f7 | 2015-09-26 15:04:07 -0700 | [diff] [blame] | 415 | bool bypass; |
Mark Brown | 4d4cfd1 | 2012-02-23 20:53:37 +0000 | [diff] [blame] | 416 | |
Maarten ter Huurne | d856fce | 2013-06-03 00:15:26 +0200 | [diff] [blame] | 417 | BUG_ON(!map->cache_ops); |
Mark Brown | 4d4cfd1 | 2012-02-23 20:53:37 +0000 | [diff] [blame] | 418 | |
Lars-Peter Clausen | 81485f5 | 2013-05-23 15:06:15 +0200 | [diff] [blame] | 419 | map->lock(map->lock_arg); |
Mark Brown | 4d4cfd1 | 2012-02-23 20:53:37 +0000 | [diff] [blame] | 420 | |
| 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 Zabel | c6b570d | 2015-03-09 12:20:13 +0100 | [diff] [blame] | 427 | trace_regcache_sync(map, name, "start region"); |
Mark Brown | 4d4cfd1 | 2012-02-23 20:53:37 +0000 | [diff] [blame] | 428 | |
| 429 | if (!map->cache_dirty) |
| 430 | goto out; |
| 431 | |
Mark Brown | affbe88 | 2013-10-10 21:06:32 +0100 | [diff] [blame] | 432 | map->async = true; |
| 433 | |
Maarten ter Huurne | d856fce | 2013-06-03 00:15:26 +0200 | [diff] [blame] | 434 | 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 Brown | 4d4cfd1 | 2012-02-23 20:53:37 +0000 | [diff] [blame] | 438 | |
| 439 | out: |
Mark Brown | 4d4cfd1 | 2012-02-23 20:53:37 +0000 | [diff] [blame] | 440 | /* Restore the bypass state */ |
| 441 | map->cache_bypass = bypass; |
Mark Brown | affbe88 | 2013-10-10 21:06:32 +0100 | [diff] [blame] | 442 | map->async = false; |
Kevin Cernekee | 1c79771 | 2015-05-05 15:14:14 -0700 | [diff] [blame] | 443 | map->no_sync_defaults = false; |
Lars-Peter Clausen | 81485f5 | 2013-05-23 15:06:15 +0200 | [diff] [blame] | 444 | map->unlock(map->lock_arg); |
Mark Brown | 4d4cfd1 | 2012-02-23 20:53:37 +0000 | [diff] [blame] | 445 | |
Mark Brown | affbe88 | 2013-10-10 21:06:32 +0100 | [diff] [blame] | 446 | regmap_async_complete(map); |
| 447 | |
Philipp Zabel | c6b570d | 2015-03-09 12:20:13 +0100 | [diff] [blame] | 448 | trace_regcache_sync(map, name, "stop region"); |
Mark Brown | affbe88 | 2013-10-10 21:06:32 +0100 | [diff] [blame] | 449 | |
Mark Brown | 4d4cfd1 | 2012-02-23 20:53:37 +0000 | [diff] [blame] | 450 | return ret; |
| 451 | } |
Mark Brown | e466de0 | 2012-04-03 13:08:53 +0100 | [diff] [blame] | 452 | EXPORT_SYMBOL_GPL(regcache_sync_region); |
Mark Brown | 4d4cfd1 | 2012-02-23 20:53:37 +0000 | [diff] [blame] | 453 | |
| 454 | /** |
Charles Keepax | 2cf8e2d | 2017-01-12 11:17:39 +0000 | [diff] [blame] | 455 | * regcache_drop_region - Discard part of the register cache |
Mark Brown | 697e85b | 2013-05-08 13:55:22 +0100 | [diff] [blame] | 456 | * |
| 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 | */ |
| 465 | int regcache_drop_region(struct regmap *map, unsigned int min, |
| 466 | unsigned int max) |
| 467 | { |
Mark Brown | 697e85b | 2013-05-08 13:55:22 +0100 | [diff] [blame] | 468 | int ret = 0; |
| 469 | |
Lars-Peter Clausen | 3f4ff56 | 2013-08-29 10:26:34 +0200 | [diff] [blame] | 470 | if (!map->cache_ops || !map->cache_ops->drop) |
Mark Brown | 697e85b | 2013-05-08 13:55:22 +0100 | [diff] [blame] | 471 | return -EINVAL; |
| 472 | |
Lars-Peter Clausen | 81485f5 | 2013-05-23 15:06:15 +0200 | [diff] [blame] | 473 | map->lock(map->lock_arg); |
Mark Brown | 697e85b | 2013-05-08 13:55:22 +0100 | [diff] [blame] | 474 | |
Philipp Zabel | c6b570d | 2015-03-09 12:20:13 +0100 | [diff] [blame] | 475 | trace_regcache_drop_region(map, min, max); |
Mark Brown | 697e85b | 2013-05-08 13:55:22 +0100 | [diff] [blame] | 476 | |
Lars-Peter Clausen | 3f4ff56 | 2013-08-29 10:26:34 +0200 | [diff] [blame] | 477 | ret = map->cache_ops->drop(map, min, max); |
Mark Brown | 697e85b | 2013-05-08 13:55:22 +0100 | [diff] [blame] | 478 | |
Lars-Peter Clausen | 81485f5 | 2013-05-23 15:06:15 +0200 | [diff] [blame] | 479 | map->unlock(map->lock_arg); |
Mark Brown | 697e85b | 2013-05-08 13:55:22 +0100 | [diff] [blame] | 480 | |
| 481 | return ret; |
| 482 | } |
| 483 | EXPORT_SYMBOL_GPL(regcache_drop_region); |
| 484 | |
| 485 | /** |
Charles Keepax | 2cf8e2d | 2017-01-12 11:17:39 +0000 | [diff] [blame] | 486 | * regcache_cache_only - Put a register map into cache only mode |
Mark Brown | 92afb28 | 2011-09-19 18:22:14 +0100 | [diff] [blame] | 487 | * |
| 488 | * @map: map to configure |
Charles Keepax | 2cf8e2d | 2017-01-12 11:17:39 +0000 | [diff] [blame] | 489 | * @enable: flag if changes should be written to the hardware |
Mark Brown | 92afb28 | 2011-09-19 18:22:14 +0100 | [diff] [blame] | 490 | * |
| 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 | */ |
| 497 | void regcache_cache_only(struct regmap *map, bool enable) |
| 498 | { |
Lars-Peter Clausen | 81485f5 | 2013-05-23 15:06:15 +0200 | [diff] [blame] | 499 | map->lock(map->lock_arg); |
Dimitris Papastamos | ac77a76 | 2011-09-29 14:36:28 +0100 | [diff] [blame] | 500 | WARN_ON(map->cache_bypass && enable); |
Mark Brown | 92afb28 | 2011-09-19 18:22:14 +0100 | [diff] [blame] | 501 | map->cache_only = enable; |
Philipp Zabel | c6b570d | 2015-03-09 12:20:13 +0100 | [diff] [blame] | 502 | trace_regmap_cache_only(map, enable); |
Lars-Peter Clausen | 81485f5 | 2013-05-23 15:06:15 +0200 | [diff] [blame] | 503 | map->unlock(map->lock_arg); |
Mark Brown | 92afb28 | 2011-09-19 18:22:14 +0100 | [diff] [blame] | 504 | } |
| 505 | EXPORT_SYMBOL_GPL(regcache_cache_only); |
| 506 | |
Dimitris Papastamos | 6eb0f5e | 2011-09-29 14:36:27 +0100 | [diff] [blame] | 507 | /** |
Charles Keepax | 2cf8e2d | 2017-01-12 11:17:39 +0000 | [diff] [blame] | 508 | * regcache_mark_dirty - Indicate that HW registers were reset to default values |
Mark Brown | 8ae0d7e | 2011-10-26 10:34:22 +0200 | [diff] [blame] | 509 | * |
| 510 | * @map: map to mark |
| 511 | * |
Kevin Cernekee | 1c79771 | 2015-05-05 15:14:14 -0700 | [diff] [blame] | 512 | * 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 Brown | 8ae0d7e | 2011-10-26 10:34:22 +0200 | [diff] [blame] | 519 | */ |
| 520 | void regcache_mark_dirty(struct regmap *map) |
| 521 | { |
Lars-Peter Clausen | 81485f5 | 2013-05-23 15:06:15 +0200 | [diff] [blame] | 522 | map->lock(map->lock_arg); |
Mark Brown | 8ae0d7e | 2011-10-26 10:34:22 +0200 | [diff] [blame] | 523 | map->cache_dirty = true; |
Kevin Cernekee | 1c79771 | 2015-05-05 15:14:14 -0700 | [diff] [blame] | 524 | map->no_sync_defaults = true; |
Lars-Peter Clausen | 81485f5 | 2013-05-23 15:06:15 +0200 | [diff] [blame] | 525 | map->unlock(map->lock_arg); |
Mark Brown | 8ae0d7e | 2011-10-26 10:34:22 +0200 | [diff] [blame] | 526 | } |
| 527 | EXPORT_SYMBOL_GPL(regcache_mark_dirty); |
| 528 | |
| 529 | /** |
Charles Keepax | 2cf8e2d | 2017-01-12 11:17:39 +0000 | [diff] [blame] | 530 | * regcache_cache_bypass - Put a register map into cache bypass mode |
Dimitris Papastamos | 6eb0f5e | 2011-09-29 14:36:27 +0100 | [diff] [blame] | 531 | * |
| 532 | * @map: map to configure |
Charles Keepax | 2cf8e2d | 2017-01-12 11:17:39 +0000 | [diff] [blame] | 533 | * @enable: flag if changes should not be written to the cache |
Dimitris Papastamos | 6eb0f5e | 2011-09-29 14:36:27 +0100 | [diff] [blame] | 534 | * |
| 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 | */ |
| 540 | void regcache_cache_bypass(struct regmap *map, bool enable) |
| 541 | { |
Lars-Peter Clausen | 81485f5 | 2013-05-23 15:06:15 +0200 | [diff] [blame] | 542 | map->lock(map->lock_arg); |
Dimitris Papastamos | ac77a76 | 2011-09-29 14:36:28 +0100 | [diff] [blame] | 543 | WARN_ON(map->cache_only && enable); |
Dimitris Papastamos | 6eb0f5e | 2011-09-29 14:36:27 +0100 | [diff] [blame] | 544 | map->cache_bypass = enable; |
Philipp Zabel | c6b570d | 2015-03-09 12:20:13 +0100 | [diff] [blame] | 545 | trace_regmap_cache_bypass(map, enable); |
Lars-Peter Clausen | 81485f5 | 2013-05-23 15:06:15 +0200 | [diff] [blame] | 546 | map->unlock(map->lock_arg); |
Dimitris Papastamos | 6eb0f5e | 2011-09-29 14:36:27 +0100 | [diff] [blame] | 547 | } |
| 548 | EXPORT_SYMBOL_GPL(regcache_cache_bypass); |
| 549 | |
Mark Brown | 879082c | 2013-02-21 18:03:13 +0000 | [diff] [blame] | 550 | bool regcache_set_val(struct regmap *map, void *base, unsigned int idx, |
| 551 | unsigned int val) |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 552 | { |
Mark Brown | 325acab | 2013-02-21 18:07:01 +0000 | [diff] [blame] | 553 | if (regcache_get_val(map, base, idx) == val) |
| 554 | return true; |
| 555 | |
Mark Brown | eb4cb76 | 2013-02-21 18:39:47 +0000 | [diff] [blame] | 556 | /* 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 Brown | 879082c | 2013-02-21 18:03:13 +0000 | [diff] [blame] | 563 | switch (map->cache_word_size) { |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 564 | case 1: { |
| 565 | u8 *cache = base; |
Xiubo Li | 2fd6902 | 2015-12-09 13:09:06 +0800 | [diff] [blame] | 566 | |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 567 | cache[idx] = val; |
| 568 | break; |
| 569 | } |
| 570 | case 2: { |
| 571 | u16 *cache = base; |
Xiubo Li | 2fd6902 | 2015-12-09 13:09:06 +0800 | [diff] [blame] | 572 | |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 573 | cache[idx] = val; |
| 574 | break; |
| 575 | } |
Mark Brown | 7d5e525 | 2012-02-17 15:58:25 -0800 | [diff] [blame] | 576 | case 4: { |
| 577 | u32 *cache = base; |
Xiubo Li | 2fd6902 | 2015-12-09 13:09:06 +0800 | [diff] [blame] | 578 | |
Mark Brown | 7d5e525 | 2012-02-17 15:58:25 -0800 | [diff] [blame] | 579 | cache[idx] = val; |
| 580 | break; |
| 581 | } |
Xiubo Li | 8b7663d | 2015-12-09 13:09:07 +0800 | [diff] [blame] | 582 | #ifdef CONFIG_64BIT |
| 583 | case 8: { |
| 584 | u64 *cache = base; |
| 585 | |
| 586 | cache[idx] = val; |
| 587 | break; |
| 588 | } |
| 589 | #endif |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 590 | default: |
| 591 | BUG(); |
| 592 | } |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 593 | return false; |
| 594 | } |
| 595 | |
Mark Brown | 879082c | 2013-02-21 18:03:13 +0000 | [diff] [blame] | 596 | unsigned int regcache_get_val(struct regmap *map, const void *base, |
| 597 | unsigned int idx) |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 598 | { |
| 599 | if (!base) |
| 600 | return -EINVAL; |
| 601 | |
Mark Brown | eb4cb76 | 2013-02-21 18:39:47 +0000 | [diff] [blame] | 602 | /* Use device native format if possible */ |
| 603 | if (map->format.parse_val) |
Mark Brown | 8817796 | 2013-03-13 19:29:36 +0000 | [diff] [blame] | 604 | return map->format.parse_val(regcache_get_val_addr(map, base, |
| 605 | idx)); |
Mark Brown | eb4cb76 | 2013-02-21 18:39:47 +0000 | [diff] [blame] | 606 | |
Mark Brown | 879082c | 2013-02-21 18:03:13 +0000 | [diff] [blame] | 607 | switch (map->cache_word_size) { |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 608 | case 1: { |
| 609 | const u8 *cache = base; |
Xiubo Li | 2fd6902 | 2015-12-09 13:09:06 +0800 | [diff] [blame] | 610 | |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 611 | return cache[idx]; |
| 612 | } |
| 613 | case 2: { |
| 614 | const u16 *cache = base; |
Xiubo Li | 2fd6902 | 2015-12-09 13:09:06 +0800 | [diff] [blame] | 615 | |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 616 | return cache[idx]; |
| 617 | } |
Mark Brown | 7d5e525 | 2012-02-17 15:58:25 -0800 | [diff] [blame] | 618 | case 4: { |
| 619 | const u32 *cache = base; |
Xiubo Li | 2fd6902 | 2015-12-09 13:09:06 +0800 | [diff] [blame] | 620 | |
Mark Brown | 7d5e525 | 2012-02-17 15:58:25 -0800 | [diff] [blame] | 621 | return cache[idx]; |
| 622 | } |
Xiubo Li | 8b7663d | 2015-12-09 13:09:07 +0800 | [diff] [blame] | 623 | #ifdef CONFIG_64BIT |
| 624 | case 8: { |
| 625 | const u64 *cache = base; |
| 626 | |
| 627 | return cache[idx]; |
| 628 | } |
| 629 | #endif |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 630 | default: |
| 631 | BUG(); |
| 632 | } |
| 633 | /* unreachable */ |
| 634 | return -1; |
| 635 | } |
| 636 | |
Mark Brown | f094fea | 2011-10-04 22:05:47 +0100 | [diff] [blame] | 637 | static int regcache_default_cmp(const void *a, const void *b) |
Dimitris Papastamos | c08604b | 2011-10-03 10:50:14 +0100 | [diff] [blame] | 638 | { |
| 639 | const struct reg_default *_a = a; |
| 640 | const struct reg_default *_b = b; |
| 641 | |
| 642 | return _a->reg - _b->reg; |
| 643 | } |
| 644 | |
Mark Brown | f094fea | 2011-10-04 22:05:47 +0100 | [diff] [blame] | 645 | int 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 Brown | 6e6ace0 | 2011-10-09 13:23:31 +0100 | [diff] [blame] | 659 | return -ENOENT; |
Mark Brown | f094fea | 2011-10-04 22:05:47 +0100 | [diff] [blame] | 660 | } |
Mark Brown | f8bd822 | 2013-03-29 19:32:28 +0000 | [diff] [blame] | 661 | |
Lars-Peter Clausen | 3f4ff56 | 2013-08-29 10:26:34 +0200 | [diff] [blame] | 662 | static 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 Brown | cfdeb8c | 2013-03-29 20:12:21 +0000 | [diff] [blame] | 670 | static int regcache_sync_block_single(struct regmap *map, void *block, |
Lars-Peter Clausen | 3f4ff56 | 2013-08-29 10:26:34 +0200 | [diff] [blame] | 671 | unsigned long *cache_present, |
Mark Brown | cfdeb8c | 2013-03-29 20:12:21 +0000 | [diff] [blame] | 672 | 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 Iwai | 4ceba98 | 2015-03-04 15:29:17 +0100 | [diff] [blame] | 681 | if (!regcache_reg_present(cache_present, i) || |
| 682 | !regmap_writeable(map, regtmp)) |
Mark Brown | cfdeb8c | 2013-03-29 20:12:21 +0000 | [diff] [blame] | 683 | continue; |
| 684 | |
| 685 | val = regcache_get_val(map, block, i); |
Kevin Cernekee | 3969fa08 | 2015-05-05 15:14:13 -0700 | [diff] [blame] | 686 | if (!regcache_reg_needs_sync(map, regtmp, val)) |
Mark Brown | cfdeb8c | 2013-03-29 20:12:21 +0000 | [diff] [blame] | 687 | continue; |
| 688 | |
Viresh Kumar | 621a5f7 | 2015-09-26 15:04:07 -0700 | [diff] [blame] | 689 | map->cache_bypass = true; |
Mark Brown | cfdeb8c | 2013-03-29 20:12:21 +0000 | [diff] [blame] | 690 | |
| 691 | ret = _regmap_write(map, regtmp, val); |
| 692 | |
Viresh Kumar | 621a5f7 | 2015-09-26 15:04:07 -0700 | [diff] [blame] | 693 | map->cache_bypass = false; |
Jarkko Nikula | f29a432 | 2014-09-16 14:04:14 +0300 | [diff] [blame] | 694 | if (ret != 0) { |
| 695 | dev_err(map->dev, "Unable to sync register %#x. %d\n", |
| 696 | regtmp, ret); |
Mark Brown | cfdeb8c | 2013-03-29 20:12:21 +0000 | [diff] [blame] | 697 | return ret; |
Jarkko Nikula | f29a432 | 2014-09-16 14:04:14 +0300 | [diff] [blame] | 698 | } |
Mark Brown | cfdeb8c | 2013-03-29 20:12:21 +0000 | [diff] [blame] | 699 | dev_dbg(map->dev, "Synced register %#x, value %#x\n", |
| 700 | regtmp, val); |
| 701 | } |
| 702 | |
| 703 | return 0; |
| 704 | } |
| 705 | |
Mark Brown | 75a5f89 | 2013-03-29 20:50:07 +0000 | [diff] [blame] | 706 | static 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 Reid | 78ba73e | 2014-01-24 15:40:39 -0800 | [diff] [blame] | 715 | count = (cur - base) / map->reg_stride; |
Mark Brown | 75a5f89 | 2013-03-29 20:50:07 +0000 | [diff] [blame] | 716 | |
Stratos Karafotis | 9659293 | 2013-04-04 19:40:45 +0300 | [diff] [blame] | 717 | 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] | 718 | count * val_bytes, count, base, cur - map->reg_stride); |
Mark Brown | 75a5f89 | 2013-03-29 20:50:07 +0000 | [diff] [blame] | 719 | |
Viresh Kumar | 621a5f7 | 2015-09-26 15:04:07 -0700 | [diff] [blame] | 720 | map->cache_bypass = true; |
Mark Brown | 75a5f89 | 2013-03-29 20:50:07 +0000 | [diff] [blame] | 721 | |
Mark Brown | 0a81980 | 2013-10-09 12:28:52 +0100 | [diff] [blame] | 722 | ret = _regmap_raw_write(map, base, *data, count * val_bytes); |
Jarkko Nikula | f29a432 | 2014-09-16 14:04:14 +0300 | [diff] [blame] | 723 | if (ret) |
| 724 | dev_err(map->dev, "Unable to sync registers %#x-%#x. %d\n", |
| 725 | base, cur - map->reg_stride, ret); |
Mark Brown | 75a5f89 | 2013-03-29 20:50:07 +0000 | [diff] [blame] | 726 | |
Viresh Kumar | 621a5f7 | 2015-09-26 15:04:07 -0700 | [diff] [blame] | 727 | map->cache_bypass = false; |
Mark Brown | 75a5f89 | 2013-03-29 20:50:07 +0000 | [diff] [blame] | 728 | |
| 729 | *data = NULL; |
| 730 | |
| 731 | return ret; |
| 732 | } |
| 733 | |
Sachin Kamat | f52687a | 2013-04-04 14:36:18 +0530 | [diff] [blame] | 734 | static int regcache_sync_block_raw(struct regmap *map, void *block, |
Lars-Peter Clausen | 3f4ff56 | 2013-08-29 10:26:34 +0200 | [diff] [blame] | 735 | unsigned long *cache_present, |
Mark Brown | cfdeb8c | 2013-03-29 20:12:21 +0000 | [diff] [blame] | 736 | unsigned int block_base, unsigned int start, |
| 737 | unsigned int end) |
Mark Brown | f8bd822 | 2013-03-29 19:32:28 +0000 | [diff] [blame] | 738 | { |
Mark Brown | 75a5f89 | 2013-03-29 20:50:07 +0000 | [diff] [blame] | 739 | unsigned int i, val; |
| 740 | unsigned int regtmp = 0; |
| 741 | unsigned int base = 0; |
| 742 | const void *data = NULL; |
Mark Brown | f8bd822 | 2013-03-29 19:32:28 +0000 | [diff] [blame] | 743 | int ret; |
| 744 | |
| 745 | for (i = start; i < end; i++) { |
| 746 | regtmp = block_base + (i * map->reg_stride); |
| 747 | |
Takashi Iwai | 4ceba98 | 2015-03-04 15:29:17 +0100 | [diff] [blame] | 748 | if (!regcache_reg_present(cache_present, i) || |
| 749 | !regmap_writeable(map, regtmp)) { |
Mark Brown | 75a5f89 | 2013-03-29 20:50:07 +0000 | [diff] [blame] | 750 | ret = regcache_sync_block_raw_flush(map, &data, |
| 751 | base, regtmp); |
| 752 | if (ret != 0) |
| 753 | return ret; |
Mark Brown | f8bd822 | 2013-03-29 19:32:28 +0000 | [diff] [blame] | 754 | continue; |
Mark Brown | 75a5f89 | 2013-03-29 20:50:07 +0000 | [diff] [blame] | 755 | } |
Mark Brown | f8bd822 | 2013-03-29 19:32:28 +0000 | [diff] [blame] | 756 | |
| 757 | val = regcache_get_val(map, block, i); |
Kevin Cernekee | 3969fa08 | 2015-05-05 15:14:13 -0700 | [diff] [blame] | 758 | if (!regcache_reg_needs_sync(map, regtmp, val)) { |
Mark Brown | 75a5f89 | 2013-03-29 20:50:07 +0000 | [diff] [blame] | 759 | ret = regcache_sync_block_raw_flush(map, &data, |
| 760 | base, regtmp); |
| 761 | if (ret != 0) |
| 762 | return ret; |
Mark Brown | f8bd822 | 2013-03-29 19:32:28 +0000 | [diff] [blame] | 763 | continue; |
Mark Brown | 75a5f89 | 2013-03-29 20:50:07 +0000 | [diff] [blame] | 764 | } |
Mark Brown | f8bd822 | 2013-03-29 19:32:28 +0000 | [diff] [blame] | 765 | |
Mark Brown | 75a5f89 | 2013-03-29 20:50:07 +0000 | [diff] [blame] | 766 | if (!data) { |
| 767 | data = regcache_get_val_addr(map, block, i); |
| 768 | base = regtmp; |
| 769 | } |
Mark Brown | f8bd822 | 2013-03-29 19:32:28 +0000 | [diff] [blame] | 770 | } |
| 771 | |
Lars-Peter Clausen | 2d49b59 | 2013-08-05 11:21:29 +0200 | [diff] [blame] | 772 | return regcache_sync_block_raw_flush(map, &data, base, regtmp + |
| 773 | map->reg_stride); |
Mark Brown | f8bd822 | 2013-03-29 19:32:28 +0000 | [diff] [blame] | 774 | } |
Mark Brown | cfdeb8c | 2013-03-29 20:12:21 +0000 | [diff] [blame] | 775 | |
| 776 | int regcache_sync_block(struct regmap *map, void *block, |
Lars-Peter Clausen | 3f4ff56 | 2013-08-29 10:26:34 +0200 | [diff] [blame] | 777 | unsigned long *cache_present, |
Mark Brown | cfdeb8c | 2013-03-29 20:12:21 +0000 | [diff] [blame] | 778 | unsigned int block_base, unsigned int start, |
| 779 | unsigned int end) |
| 780 | { |
Markus Pargmann | 67921a1 | 2015-08-21 10:26:42 +0200 | [diff] [blame] | 781 | if (regmap_can_raw_write(map) && !map->use_single_write) |
Lars-Peter Clausen | 3f4ff56 | 2013-08-29 10:26:34 +0200 | [diff] [blame] | 782 | return regcache_sync_block_raw(map, block, cache_present, |
| 783 | block_base, start, end); |
Mark Brown | cfdeb8c | 2013-03-29 20:12:21 +0000 | [diff] [blame] | 784 | else |
Lars-Peter Clausen | 3f4ff56 | 2013-08-29 10:26:34 +0200 | [diff] [blame] | 785 | return regcache_sync_block_single(map, block, cache_present, |
| 786 | block_base, start, end); |
Mark Brown | cfdeb8c | 2013-03-29 20:12:21 +0000 | [diff] [blame] | 787 | } |