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 | |
| 13 | #include <linux/slab.h> |
Paul Gortmaker | 1b6bc32 | 2011-05-27 07:12:15 -0400 | [diff] [blame] | 14 | #include <linux/export.h> |
Paul Gortmaker | 51990e8 | 2012-01-22 11:23:42 -0500 | [diff] [blame] | 15 | #include <linux/device.h> |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 16 | #include <trace/events/regmap.h> |
Mark Brown | f094fea | 2011-10-04 22:05:47 +0100 | [diff] [blame] | 17 | #include <linux/bsearch.h> |
Dimitris Papastamos | c08604b | 2011-10-03 10:50:14 +0100 | [diff] [blame] | 18 | #include <linux/sort.h> |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 19 | |
| 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 | |
| 39 | if (!map->reg_defaults_raw) { |
Laxman Dewangan | df00c79 | 2012-02-17 18:57:26 +0530 | [diff] [blame] | 40 | u32 cache_bypass = map->cache_bypass; |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 41 | dev_warn(map->dev, "No cache defaults, reading back from HW\n"); |
Laxman Dewangan | df00c79 | 2012-02-17 18:57:26 +0530 | [diff] [blame] | 42 | |
| 43 | /* Bypass the cache access till data read from HW*/ |
| 44 | map->cache_bypass = 1; |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 45 | tmp_buf = kmalloc(map->cache_size_raw, GFP_KERNEL); |
| 46 | if (!tmp_buf) |
| 47 | return -EINVAL; |
Mark Brown | eb4cb76 | 2013-02-21 18:39:47 +0000 | [diff] [blame^] | 48 | ret = regmap_raw_read(map, 0, tmp_buf, |
| 49 | map->num_reg_defaults_raw); |
Laxman Dewangan | df00c79 | 2012-02-17 18:57:26 +0530 | [diff] [blame] | 50 | map->cache_bypass = cache_bypass; |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 51 | if (ret < 0) { |
| 52 | kfree(tmp_buf); |
| 53 | return ret; |
| 54 | } |
| 55 | map->reg_defaults_raw = tmp_buf; |
| 56 | map->cache_free = 1; |
| 57 | } |
| 58 | |
| 59 | /* calculate the size of reg_defaults */ |
| 60 | for (count = 0, i = 0; i < map->num_reg_defaults_raw; i++) { |
Mark Brown | 879082c | 2013-02-21 18:03:13 +0000 | [diff] [blame] | 61 | val = regcache_get_val(map, map->reg_defaults_raw, i); |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 62 | if (regmap_volatile(map, i * map->reg_stride)) |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 63 | continue; |
| 64 | count++; |
| 65 | } |
| 66 | |
| 67 | map->reg_defaults = kmalloc(count * sizeof(struct reg_default), |
| 68 | GFP_KERNEL); |
Lars-Peter Clausen | 021cd61 | 2011-11-14 10:40:16 +0100 | [diff] [blame] | 69 | if (!map->reg_defaults) { |
| 70 | ret = -ENOMEM; |
| 71 | goto err_free; |
| 72 | } |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 73 | |
| 74 | /* fill the reg_defaults */ |
| 75 | map->num_reg_defaults = count; |
| 76 | for (i = 0, j = 0; i < map->num_reg_defaults_raw; i++) { |
Mark Brown | 879082c | 2013-02-21 18:03:13 +0000 | [diff] [blame] | 77 | val = regcache_get_val(map, map->reg_defaults_raw, i); |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 78 | if (regmap_volatile(map, i * map->reg_stride)) |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 79 | continue; |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 80 | map->reg_defaults[j].reg = i * map->reg_stride; |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 81 | map->reg_defaults[j].def = val; |
| 82 | j++; |
| 83 | } |
| 84 | |
| 85 | return 0; |
Lars-Peter Clausen | 021cd61 | 2011-11-14 10:40:16 +0100 | [diff] [blame] | 86 | |
| 87 | err_free: |
| 88 | if (map->cache_free) |
| 89 | kfree(map->reg_defaults_raw); |
| 90 | |
| 91 | return ret; |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 92 | } |
| 93 | |
Lars-Peter Clausen | e5e3b8a | 2011-11-16 16:28:16 +0100 | [diff] [blame] | 94 | int regcache_init(struct regmap *map, const struct regmap_config *config) |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 95 | { |
| 96 | int ret; |
| 97 | int i; |
| 98 | void *tmp_buf; |
| 99 | |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 100 | for (i = 0; i < config->num_reg_defaults; i++) |
| 101 | if (config->reg_defaults[i].reg % map->reg_stride) |
| 102 | return -EINVAL; |
| 103 | |
Mark Brown | e7a6db3 | 2011-09-19 16:08:03 +0100 | [diff] [blame] | 104 | if (map->cache_type == REGCACHE_NONE) { |
| 105 | map->cache_bypass = true; |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 106 | return 0; |
Mark Brown | e7a6db3 | 2011-09-19 16:08:03 +0100 | [diff] [blame] | 107 | } |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 108 | |
| 109 | for (i = 0; i < ARRAY_SIZE(cache_types); i++) |
| 110 | if (cache_types[i]->type == map->cache_type) |
| 111 | break; |
| 112 | |
| 113 | if (i == ARRAY_SIZE(cache_types)) { |
| 114 | dev_err(map->dev, "Could not match compress type: %d\n", |
| 115 | map->cache_type); |
| 116 | return -EINVAL; |
| 117 | } |
| 118 | |
Lars-Peter Clausen | e5e3b8a | 2011-11-16 16:28:16 +0100 | [diff] [blame] | 119 | map->num_reg_defaults = config->num_reg_defaults; |
| 120 | map->num_reg_defaults_raw = config->num_reg_defaults_raw; |
| 121 | map->reg_defaults_raw = config->reg_defaults_raw; |
Lars-Peter Clausen | 064d4db | 2011-11-16 20:34:03 +0100 | [diff] [blame] | 122 | map->cache_word_size = DIV_ROUND_UP(config->val_bits, 8); |
| 123 | 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] | 124 | |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 125 | map->cache = NULL; |
| 126 | map->cache_ops = cache_types[i]; |
| 127 | |
| 128 | if (!map->cache_ops->read || |
| 129 | !map->cache_ops->write || |
| 130 | !map->cache_ops->name) |
| 131 | return -EINVAL; |
| 132 | |
| 133 | /* We still need to ensure that the reg_defaults |
| 134 | * won't vanish from under us. We'll need to make |
| 135 | * a copy of it. |
| 136 | */ |
Lars-Peter Clausen | 720e461 | 2011-11-16 16:28:17 +0100 | [diff] [blame] | 137 | if (config->reg_defaults) { |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 138 | if (!map->num_reg_defaults) |
| 139 | return -EINVAL; |
Lars-Peter Clausen | 720e461 | 2011-11-16 16:28:17 +0100 | [diff] [blame] | 140 | tmp_buf = kmemdup(config->reg_defaults, map->num_reg_defaults * |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 141 | sizeof(struct reg_default), GFP_KERNEL); |
| 142 | if (!tmp_buf) |
| 143 | return -ENOMEM; |
| 144 | map->reg_defaults = tmp_buf; |
Mark Brown | 8528bdd | 2011-10-09 13:13:58 +0100 | [diff] [blame] | 145 | } else if (map->num_reg_defaults_raw) { |
Mark Brown | 5fcd256 | 2011-09-29 15:24:54 +0100 | [diff] [blame] | 146 | /* Some devices such as PMICs don't have cache defaults, |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 147 | * we cope with this by reading back the HW registers and |
| 148 | * crafting the cache defaults by hand. |
| 149 | */ |
| 150 | ret = regcache_hw_init(map); |
| 151 | if (ret < 0) |
| 152 | return ret; |
| 153 | } |
| 154 | |
| 155 | if (!map->max_register) |
| 156 | map->max_register = map->num_reg_defaults_raw; |
| 157 | |
| 158 | if (map->cache_ops->init) { |
| 159 | dev_dbg(map->dev, "Initializing %s cache\n", |
| 160 | map->cache_ops->name); |
Lars-Peter Clausen | bd061c7 | 2011-11-14 10:40:17 +0100 | [diff] [blame] | 161 | ret = map->cache_ops->init(map); |
| 162 | if (ret) |
| 163 | goto err_free; |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 164 | } |
| 165 | return 0; |
Lars-Peter Clausen | bd061c7 | 2011-11-14 10:40:17 +0100 | [diff] [blame] | 166 | |
| 167 | err_free: |
| 168 | kfree(map->reg_defaults); |
| 169 | if (map->cache_free) |
| 170 | kfree(map->reg_defaults_raw); |
| 171 | |
| 172 | return ret; |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | void regcache_exit(struct regmap *map) |
| 176 | { |
| 177 | if (map->cache_type == REGCACHE_NONE) |
| 178 | return; |
| 179 | |
| 180 | BUG_ON(!map->cache_ops); |
| 181 | |
| 182 | kfree(map->reg_defaults); |
| 183 | if (map->cache_free) |
| 184 | kfree(map->reg_defaults_raw); |
| 185 | |
| 186 | if (map->cache_ops->exit) { |
| 187 | dev_dbg(map->dev, "Destroying %s cache\n", |
| 188 | map->cache_ops->name); |
| 189 | map->cache_ops->exit(map); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * regcache_read: Fetch the value of a given register from the cache. |
| 195 | * |
| 196 | * @map: map to configure. |
| 197 | * @reg: The register index. |
| 198 | * @value: The value to be returned. |
| 199 | * |
| 200 | * Return a negative value on failure, 0 on success. |
| 201 | */ |
| 202 | int regcache_read(struct regmap *map, |
| 203 | unsigned int reg, unsigned int *value) |
| 204 | { |
Mark Brown | bc7ee55 | 2011-11-30 14:27:08 +0000 | [diff] [blame] | 205 | int ret; |
| 206 | |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 207 | if (map->cache_type == REGCACHE_NONE) |
| 208 | return -ENOSYS; |
| 209 | |
| 210 | BUG_ON(!map->cache_ops); |
| 211 | |
Mark Brown | bc7ee55 | 2011-11-30 14:27:08 +0000 | [diff] [blame] | 212 | if (!regmap_volatile(map, reg)) { |
| 213 | ret = map->cache_ops->read(map, reg, value); |
| 214 | |
| 215 | if (ret == 0) |
| 216 | trace_regmap_reg_read_cache(map->dev, reg, *value); |
| 217 | |
| 218 | return ret; |
| 219 | } |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 220 | |
| 221 | return -EINVAL; |
| 222 | } |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 223 | |
| 224 | /** |
| 225 | * regcache_write: Set the value of a given register in the cache. |
| 226 | * |
| 227 | * @map: map to configure. |
| 228 | * @reg: The register index. |
| 229 | * @value: The new register value. |
| 230 | * |
| 231 | * Return a negative value on failure, 0 on success. |
| 232 | */ |
| 233 | int regcache_write(struct regmap *map, |
| 234 | unsigned int reg, unsigned int value) |
| 235 | { |
| 236 | if (map->cache_type == REGCACHE_NONE) |
| 237 | return 0; |
| 238 | |
| 239 | BUG_ON(!map->cache_ops); |
| 240 | |
| 241 | if (!regmap_writeable(map, reg)) |
| 242 | return -EIO; |
| 243 | |
| 244 | if (!regmap_volatile(map, reg)) |
| 245 | return map->cache_ops->write(map, reg, value); |
| 246 | |
| 247 | return 0; |
| 248 | } |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 249 | |
| 250 | /** |
| 251 | * regcache_sync: Sync the register cache with the hardware. |
| 252 | * |
| 253 | * @map: map to configure. |
| 254 | * |
| 255 | * Any registers that should not be synced should be marked as |
| 256 | * volatile. In general drivers can choose not to use the provided |
| 257 | * syncing functionality if they so require. |
| 258 | * |
| 259 | * Return a negative value on failure, 0 on success. |
| 260 | */ |
| 261 | int regcache_sync(struct regmap *map) |
| 262 | { |
Dimitris Papastamos | 954757d | 2011-09-27 11:25:06 +0100 | [diff] [blame] | 263 | int ret = 0; |
Dimitris Papastamos | 954757d | 2011-09-27 11:25:06 +0100 | [diff] [blame] | 264 | unsigned int i; |
Dimitris Papastamos | 5936008 | 2011-09-19 14:34:04 +0100 | [diff] [blame] | 265 | const char *name; |
Dimitris Papastamos | beb1a10 | 2011-09-29 14:36:26 +0100 | [diff] [blame] | 266 | unsigned int bypass; |
Dimitris Papastamos | 5936008 | 2011-09-19 14:34:04 +0100 | [diff] [blame] | 267 | |
Mark Brown | c3ec232 | 2012-02-23 20:48:40 +0000 | [diff] [blame] | 268 | BUG_ON(!map->cache_ops || !map->cache_ops->sync); |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 269 | |
Stephen Warren | bacdbe0 | 2012-04-04 15:48:28 -0600 | [diff] [blame] | 270 | map->lock(map); |
Dimitris Papastamos | beb1a10 | 2011-09-29 14:36:26 +0100 | [diff] [blame] | 271 | /* Remember the initial bypass state */ |
| 272 | bypass = map->cache_bypass; |
Dimitris Papastamos | 954757d | 2011-09-27 11:25:06 +0100 | [diff] [blame] | 273 | dev_dbg(map->dev, "Syncing %s cache\n", |
| 274 | map->cache_ops->name); |
| 275 | name = map->cache_ops->name; |
| 276 | trace_regcache_sync(map->dev, name, "start"); |
Mark Brown | 22f0d90 | 2012-01-21 12:01:14 +0000 | [diff] [blame] | 277 | |
Mark Brown | 8ae0d7e | 2011-10-26 10:34:22 +0200 | [diff] [blame] | 278 | if (!map->cache_dirty) |
| 279 | goto out; |
Mark Brown | d9db762 | 2012-01-25 21:06:33 +0000 | [diff] [blame] | 280 | |
Mark Brown | 22f0d90 | 2012-01-21 12:01:14 +0000 | [diff] [blame] | 281 | /* Apply any patch first */ |
Mark Brown | 8a892d6 | 2012-01-25 21:05:48 +0000 | [diff] [blame] | 282 | map->cache_bypass = 1; |
Mark Brown | 22f0d90 | 2012-01-21 12:01:14 +0000 | [diff] [blame] | 283 | for (i = 0; i < map->patch_regs; i++) { |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 284 | if (map->patch[i].reg % map->reg_stride) { |
| 285 | ret = -EINVAL; |
| 286 | goto out; |
| 287 | } |
Mark Brown | 22f0d90 | 2012-01-21 12:01:14 +0000 | [diff] [blame] | 288 | ret = _regmap_write(map, map->patch[i].reg, map->patch[i].def); |
| 289 | if (ret != 0) { |
| 290 | dev_err(map->dev, "Failed to write %x = %x: %d\n", |
| 291 | map->patch[i].reg, map->patch[i].def, ret); |
| 292 | goto out; |
| 293 | } |
| 294 | } |
Mark Brown | 8a892d6 | 2012-01-25 21:05:48 +0000 | [diff] [blame] | 295 | map->cache_bypass = 0; |
Mark Brown | 22f0d90 | 2012-01-21 12:01:14 +0000 | [diff] [blame] | 296 | |
Mark Brown | ac8d91c | 2012-02-23 19:31:04 +0000 | [diff] [blame] | 297 | ret = map->cache_ops->sync(map, 0, map->max_register); |
Dimitris Papastamos | 954757d | 2011-09-27 11:25:06 +0100 | [diff] [blame] | 298 | |
Mark Brown | 6ff7373 | 2012-02-23 22:05:59 +0000 | [diff] [blame] | 299 | if (ret == 0) |
| 300 | map->cache_dirty = false; |
| 301 | |
Dimitris Papastamos | 954757d | 2011-09-27 11:25:06 +0100 | [diff] [blame] | 302 | out: |
| 303 | trace_regcache_sync(map->dev, name, "stop"); |
Dimitris Papastamos | beb1a10 | 2011-09-29 14:36:26 +0100 | [diff] [blame] | 304 | /* Restore the bypass state */ |
| 305 | map->cache_bypass = bypass; |
Stephen Warren | bacdbe0 | 2012-04-04 15:48:28 -0600 | [diff] [blame] | 306 | map->unlock(map); |
Dimitris Papastamos | 954757d | 2011-09-27 11:25:06 +0100 | [diff] [blame] | 307 | |
| 308 | return ret; |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 309 | } |
| 310 | EXPORT_SYMBOL_GPL(regcache_sync); |
| 311 | |
Mark Brown | 92afb28 | 2011-09-19 18:22:14 +0100 | [diff] [blame] | 312 | /** |
Mark Brown | 4d4cfd1 | 2012-02-23 20:53:37 +0000 | [diff] [blame] | 313 | * regcache_sync_region: Sync part of the register cache with the hardware. |
| 314 | * |
| 315 | * @map: map to sync. |
| 316 | * @min: first register to sync |
| 317 | * @max: last register to sync |
| 318 | * |
| 319 | * Write all non-default register values in the specified region to |
| 320 | * the hardware. |
| 321 | * |
| 322 | * Return a negative value on failure, 0 on success. |
| 323 | */ |
| 324 | int regcache_sync_region(struct regmap *map, unsigned int min, |
| 325 | unsigned int max) |
| 326 | { |
| 327 | int ret = 0; |
| 328 | const char *name; |
| 329 | unsigned int bypass; |
| 330 | |
| 331 | BUG_ON(!map->cache_ops || !map->cache_ops->sync); |
| 332 | |
Stephen Warren | bacdbe0 | 2012-04-04 15:48:28 -0600 | [diff] [blame] | 333 | map->lock(map); |
Mark Brown | 4d4cfd1 | 2012-02-23 20:53:37 +0000 | [diff] [blame] | 334 | |
| 335 | /* Remember the initial bypass state */ |
| 336 | bypass = map->cache_bypass; |
| 337 | |
| 338 | name = map->cache_ops->name; |
| 339 | dev_dbg(map->dev, "Syncing %s cache from %d-%d\n", name, min, max); |
| 340 | |
| 341 | trace_regcache_sync(map->dev, name, "start region"); |
| 342 | |
| 343 | if (!map->cache_dirty) |
| 344 | goto out; |
| 345 | |
| 346 | ret = map->cache_ops->sync(map, min, max); |
| 347 | |
| 348 | out: |
| 349 | trace_regcache_sync(map->dev, name, "stop region"); |
| 350 | /* Restore the bypass state */ |
| 351 | map->cache_bypass = bypass; |
Stephen Warren | bacdbe0 | 2012-04-04 15:48:28 -0600 | [diff] [blame] | 352 | map->unlock(map); |
Mark Brown | 4d4cfd1 | 2012-02-23 20:53:37 +0000 | [diff] [blame] | 353 | |
| 354 | return ret; |
| 355 | } |
Mark Brown | e466de0 | 2012-04-03 13:08:53 +0100 | [diff] [blame] | 356 | EXPORT_SYMBOL_GPL(regcache_sync_region); |
Mark Brown | 4d4cfd1 | 2012-02-23 20:53:37 +0000 | [diff] [blame] | 357 | |
| 358 | /** |
Mark Brown | 92afb28 | 2011-09-19 18:22:14 +0100 | [diff] [blame] | 359 | * regcache_cache_only: Put a register map into cache only mode |
| 360 | * |
| 361 | * @map: map to configure |
| 362 | * @cache_only: flag if changes should be written to the hardware |
| 363 | * |
| 364 | * When a register map is marked as cache only writes to the register |
| 365 | * map API will only update the register cache, they will not cause |
| 366 | * any hardware changes. This is useful for allowing portions of |
| 367 | * drivers to act as though the device were functioning as normal when |
| 368 | * it is disabled for power saving reasons. |
| 369 | */ |
| 370 | void regcache_cache_only(struct regmap *map, bool enable) |
| 371 | { |
Stephen Warren | bacdbe0 | 2012-04-04 15:48:28 -0600 | [diff] [blame] | 372 | map->lock(map); |
Dimitris Papastamos | ac77a76 | 2011-09-29 14:36:28 +0100 | [diff] [blame] | 373 | WARN_ON(map->cache_bypass && enable); |
Mark Brown | 92afb28 | 2011-09-19 18:22:14 +0100 | [diff] [blame] | 374 | map->cache_only = enable; |
Mark Brown | 5d5b7d4 | 2012-02-23 22:02:57 +0000 | [diff] [blame] | 375 | trace_regmap_cache_only(map->dev, enable); |
Stephen Warren | bacdbe0 | 2012-04-04 15:48:28 -0600 | [diff] [blame] | 376 | map->unlock(map); |
Mark Brown | 92afb28 | 2011-09-19 18:22:14 +0100 | [diff] [blame] | 377 | } |
| 378 | EXPORT_SYMBOL_GPL(regcache_cache_only); |
| 379 | |
Dimitris Papastamos | 6eb0f5e | 2011-09-29 14:36:27 +0100 | [diff] [blame] | 380 | /** |
Mark Brown | 8ae0d7e | 2011-10-26 10:34:22 +0200 | [diff] [blame] | 381 | * regcache_mark_dirty: Mark the register cache as dirty |
| 382 | * |
| 383 | * @map: map to mark |
| 384 | * |
| 385 | * Mark the register cache as dirty, for example due to the device |
| 386 | * having been powered down for suspend. If the cache is not marked |
| 387 | * as dirty then the cache sync will be suppressed. |
| 388 | */ |
| 389 | void regcache_mark_dirty(struct regmap *map) |
| 390 | { |
Stephen Warren | bacdbe0 | 2012-04-04 15:48:28 -0600 | [diff] [blame] | 391 | map->lock(map); |
Mark Brown | 8ae0d7e | 2011-10-26 10:34:22 +0200 | [diff] [blame] | 392 | map->cache_dirty = true; |
Stephen Warren | bacdbe0 | 2012-04-04 15:48:28 -0600 | [diff] [blame] | 393 | map->unlock(map); |
Mark Brown | 8ae0d7e | 2011-10-26 10:34:22 +0200 | [diff] [blame] | 394 | } |
| 395 | EXPORT_SYMBOL_GPL(regcache_mark_dirty); |
| 396 | |
| 397 | /** |
Dimitris Papastamos | 6eb0f5e | 2011-09-29 14:36:27 +0100 | [diff] [blame] | 398 | * regcache_cache_bypass: Put a register map into cache bypass mode |
| 399 | * |
| 400 | * @map: map to configure |
Dimitris Papastamos | 0eef6b0 | 2011-10-03 06:54:16 +0100 | [diff] [blame] | 401 | * @cache_bypass: flag if changes should not be written to the hardware |
Dimitris Papastamos | 6eb0f5e | 2011-09-29 14:36:27 +0100 | [diff] [blame] | 402 | * |
| 403 | * When a register map is marked with the cache bypass option, writes |
| 404 | * to the register map API will only update the hardware and not the |
| 405 | * the cache directly. This is useful when syncing the cache back to |
| 406 | * the hardware. |
| 407 | */ |
| 408 | void regcache_cache_bypass(struct regmap *map, bool enable) |
| 409 | { |
Stephen Warren | bacdbe0 | 2012-04-04 15:48:28 -0600 | [diff] [blame] | 410 | map->lock(map); |
Dimitris Papastamos | ac77a76 | 2011-09-29 14:36:28 +0100 | [diff] [blame] | 411 | WARN_ON(map->cache_only && enable); |
Dimitris Papastamos | 6eb0f5e | 2011-09-29 14:36:27 +0100 | [diff] [blame] | 412 | map->cache_bypass = enable; |
Mark Brown | 5d5b7d4 | 2012-02-23 22:02:57 +0000 | [diff] [blame] | 413 | trace_regmap_cache_bypass(map->dev, enable); |
Stephen Warren | bacdbe0 | 2012-04-04 15:48:28 -0600 | [diff] [blame] | 414 | map->unlock(map); |
Dimitris Papastamos | 6eb0f5e | 2011-09-29 14:36:27 +0100 | [diff] [blame] | 415 | } |
| 416 | EXPORT_SYMBOL_GPL(regcache_cache_bypass); |
| 417 | |
Mark Brown | 879082c | 2013-02-21 18:03:13 +0000 | [diff] [blame] | 418 | bool regcache_set_val(struct regmap *map, void *base, unsigned int idx, |
| 419 | unsigned int val) |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 420 | { |
Mark Brown | 325acab | 2013-02-21 18:07:01 +0000 | [diff] [blame] | 421 | if (regcache_get_val(map, base, idx) == val) |
| 422 | return true; |
| 423 | |
Mark Brown | eb4cb76 | 2013-02-21 18:39:47 +0000 | [diff] [blame^] | 424 | /* Use device native format if possible */ |
| 425 | if (map->format.format_val) { |
| 426 | map->format.format_val(base + (map->cache_word_size * idx), |
| 427 | val, 0); |
| 428 | return false; |
| 429 | } |
| 430 | |
Mark Brown | 879082c | 2013-02-21 18:03:13 +0000 | [diff] [blame] | 431 | switch (map->cache_word_size) { |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 432 | case 1: { |
| 433 | u8 *cache = base; |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 434 | cache[idx] = val; |
| 435 | break; |
| 436 | } |
| 437 | case 2: { |
| 438 | u16 *cache = base; |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 439 | cache[idx] = val; |
| 440 | break; |
| 441 | } |
Mark Brown | 7d5e525 | 2012-02-17 15:58:25 -0800 | [diff] [blame] | 442 | case 4: { |
| 443 | u32 *cache = base; |
Mark Brown | 7d5e525 | 2012-02-17 15:58:25 -0800 | [diff] [blame] | 444 | cache[idx] = val; |
| 445 | break; |
| 446 | } |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 447 | default: |
| 448 | BUG(); |
| 449 | } |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 450 | return false; |
| 451 | } |
| 452 | |
Mark Brown | 879082c | 2013-02-21 18:03:13 +0000 | [diff] [blame] | 453 | unsigned int regcache_get_val(struct regmap *map, const void *base, |
| 454 | unsigned int idx) |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 455 | { |
| 456 | if (!base) |
| 457 | return -EINVAL; |
| 458 | |
Mark Brown | eb4cb76 | 2013-02-21 18:39:47 +0000 | [diff] [blame^] | 459 | /* Use device native format if possible */ |
| 460 | if (map->format.parse_val) |
| 461 | return map->format.parse_val(base + |
| 462 | (map->cache_word_size * idx)); |
| 463 | |
Mark Brown | 879082c | 2013-02-21 18:03:13 +0000 | [diff] [blame] | 464 | switch (map->cache_word_size) { |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 465 | case 1: { |
| 466 | const u8 *cache = base; |
| 467 | return cache[idx]; |
| 468 | } |
| 469 | case 2: { |
| 470 | const u16 *cache = base; |
| 471 | return cache[idx]; |
| 472 | } |
Mark Brown | 7d5e525 | 2012-02-17 15:58:25 -0800 | [diff] [blame] | 473 | case 4: { |
| 474 | const u32 *cache = base; |
| 475 | return cache[idx]; |
| 476 | } |
Dimitris Papastamos | 9fabe24 | 2011-09-19 14:34:00 +0100 | [diff] [blame] | 477 | default: |
| 478 | BUG(); |
| 479 | } |
| 480 | /* unreachable */ |
| 481 | return -1; |
| 482 | } |
| 483 | |
Mark Brown | f094fea | 2011-10-04 22:05:47 +0100 | [diff] [blame] | 484 | static int regcache_default_cmp(const void *a, const void *b) |
Dimitris Papastamos | c08604b | 2011-10-03 10:50:14 +0100 | [diff] [blame] | 485 | { |
| 486 | const struct reg_default *_a = a; |
| 487 | const struct reg_default *_b = b; |
| 488 | |
| 489 | return _a->reg - _b->reg; |
| 490 | } |
| 491 | |
Mark Brown | f094fea | 2011-10-04 22:05:47 +0100 | [diff] [blame] | 492 | int regcache_lookup_reg(struct regmap *map, unsigned int reg) |
| 493 | { |
| 494 | struct reg_default key; |
| 495 | struct reg_default *r; |
| 496 | |
| 497 | key.reg = reg; |
| 498 | key.def = 0; |
| 499 | |
| 500 | r = bsearch(&key, map->reg_defaults, map->num_reg_defaults, |
| 501 | sizeof(struct reg_default), regcache_default_cmp); |
| 502 | |
| 503 | if (r) |
| 504 | return r - map->reg_defaults; |
| 505 | else |
Mark Brown | 6e6ace0 | 2011-10-09 13:23:31 +0100 | [diff] [blame] | 506 | return -ENOENT; |
Mark Brown | f094fea | 2011-10-04 22:05:47 +0100 | [diff] [blame] | 507 | } |