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