Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Register map access API |
| 3 | * |
| 4 | * Copyright 2011 Wolfson Microelectronics plc |
| 5 | * |
| 6 | * Author: Mark Brown <broonie@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 | |
Stephen Warren | f5d6eba | 2012-03-09 13:17:28 -0700 | [diff] [blame] | 13 | #include <linux/device.h> |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 14 | #include <linux/slab.h> |
Paul Gortmaker | 19694b5 | 2012-02-28 19:28:02 -0500 | [diff] [blame] | 15 | #include <linux/export.h> |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 16 | #include <linux/mutex.h> |
| 17 | #include <linux/err.h> |
| 18 | |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 19 | #define CREATE_TRACE_POINTS |
| 20 | #include <trace/events/regmap.h> |
| 21 | |
Mark Brown | 93de912 | 2011-07-20 22:35:37 +0100 | [diff] [blame] | 22 | #include "internal.h" |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 23 | |
Mark Brown | 8de2f08 | 2011-08-10 17:14:41 +0900 | [diff] [blame] | 24 | bool regmap_writeable(struct regmap *map, unsigned int reg) |
| 25 | { |
| 26 | if (map->max_register && reg > map->max_register) |
| 27 | return false; |
| 28 | |
| 29 | if (map->writeable_reg) |
| 30 | return map->writeable_reg(map->dev, reg); |
| 31 | |
| 32 | return true; |
| 33 | } |
| 34 | |
| 35 | bool regmap_readable(struct regmap *map, unsigned int reg) |
| 36 | { |
| 37 | if (map->max_register && reg > map->max_register) |
| 38 | return false; |
| 39 | |
Wolfram Sang | 4191f19 | 2012-01-30 15:08:16 +0100 | [diff] [blame] | 40 | if (map->format.format_write) |
| 41 | return false; |
| 42 | |
Mark Brown | 8de2f08 | 2011-08-10 17:14:41 +0900 | [diff] [blame] | 43 | if (map->readable_reg) |
| 44 | return map->readable_reg(map->dev, reg); |
| 45 | |
| 46 | return true; |
| 47 | } |
| 48 | |
| 49 | bool regmap_volatile(struct regmap *map, unsigned int reg) |
| 50 | { |
Wolfram Sang | 4191f19 | 2012-01-30 15:08:16 +0100 | [diff] [blame] | 51 | if (!regmap_readable(map, reg)) |
Mark Brown | 8de2f08 | 2011-08-10 17:14:41 +0900 | [diff] [blame] | 52 | return false; |
| 53 | |
| 54 | if (map->volatile_reg) |
| 55 | return map->volatile_reg(map->dev, reg); |
| 56 | |
| 57 | return true; |
| 58 | } |
| 59 | |
| 60 | bool regmap_precious(struct regmap *map, unsigned int reg) |
| 61 | { |
Wolfram Sang | 4191f19 | 2012-01-30 15:08:16 +0100 | [diff] [blame] | 62 | if (!regmap_readable(map, reg)) |
Mark Brown | 8de2f08 | 2011-08-10 17:14:41 +0900 | [diff] [blame] | 63 | return false; |
| 64 | |
| 65 | if (map->precious_reg) |
| 66 | return map->precious_reg(map->dev, reg); |
| 67 | |
| 68 | return false; |
| 69 | } |
| 70 | |
Lars-Peter Clausen | 82cd996 | 2011-11-08 18:37:25 +0100 | [diff] [blame] | 71 | static bool regmap_volatile_range(struct regmap *map, unsigned int reg, |
| 72 | unsigned int num) |
| 73 | { |
| 74 | unsigned int i; |
| 75 | |
| 76 | for (i = 0; i < num; i++) |
| 77 | if (!regmap_volatile(map, reg + i)) |
| 78 | return false; |
| 79 | |
| 80 | return true; |
| 81 | } |
| 82 | |
Wolfram Sang | 9aa5075 | 2012-01-27 16:10:22 +0100 | [diff] [blame] | 83 | static void regmap_format_2_6_write(struct regmap *map, |
| 84 | unsigned int reg, unsigned int val) |
| 85 | { |
| 86 | u8 *out = map->work_buf; |
| 87 | |
| 88 | *out = (reg << 6) | val; |
| 89 | } |
| 90 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 91 | static void regmap_format_4_12_write(struct regmap *map, |
| 92 | unsigned int reg, unsigned int val) |
| 93 | { |
| 94 | __be16 *out = map->work_buf; |
| 95 | *out = cpu_to_be16((reg << 12) | val); |
| 96 | } |
| 97 | |
| 98 | static void regmap_format_7_9_write(struct regmap *map, |
| 99 | unsigned int reg, unsigned int val) |
| 100 | { |
| 101 | __be16 *out = map->work_buf; |
| 102 | *out = cpu_to_be16((reg << 9) | val); |
| 103 | } |
| 104 | |
Lars-Peter Clausen | 7e5ec63 | 2011-11-16 16:28:21 +0100 | [diff] [blame] | 105 | static void regmap_format_10_14_write(struct regmap *map, |
| 106 | unsigned int reg, unsigned int val) |
| 107 | { |
| 108 | u8 *out = map->work_buf; |
| 109 | |
| 110 | out[2] = val; |
| 111 | out[1] = (val >> 8) | (reg << 6); |
| 112 | out[0] = reg >> 2; |
| 113 | } |
| 114 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 115 | static void regmap_format_8(void *buf, unsigned int val) |
| 116 | { |
| 117 | u8 *b = buf; |
| 118 | |
| 119 | b[0] = val; |
| 120 | } |
| 121 | |
| 122 | static void regmap_format_16(void *buf, unsigned int val) |
| 123 | { |
| 124 | __be16 *b = buf; |
| 125 | |
| 126 | b[0] = cpu_to_be16(val); |
| 127 | } |
| 128 | |
Mark Brown | 7d5e525 | 2012-02-17 15:58:25 -0800 | [diff] [blame] | 129 | static void regmap_format_32(void *buf, unsigned int val) |
| 130 | { |
| 131 | __be32 *b = buf; |
| 132 | |
| 133 | b[0] = cpu_to_be32(val); |
| 134 | } |
| 135 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 136 | static unsigned int regmap_parse_8(void *buf) |
| 137 | { |
| 138 | u8 *b = buf; |
| 139 | |
| 140 | return b[0]; |
| 141 | } |
| 142 | |
| 143 | static unsigned int regmap_parse_16(void *buf) |
| 144 | { |
| 145 | __be16 *b = buf; |
| 146 | |
| 147 | b[0] = be16_to_cpu(b[0]); |
| 148 | |
| 149 | return b[0]; |
| 150 | } |
| 151 | |
Mark Brown | 7d5e525 | 2012-02-17 15:58:25 -0800 | [diff] [blame] | 152 | static unsigned int regmap_parse_32(void *buf) |
| 153 | { |
| 154 | __be32 *b = buf; |
| 155 | |
| 156 | b[0] = be32_to_cpu(b[0]); |
| 157 | |
| 158 | return b[0]; |
| 159 | } |
| 160 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 161 | /** |
| 162 | * regmap_init(): Initialise register map |
| 163 | * |
| 164 | * @dev: Device that will be interacted with |
| 165 | * @bus: Bus-specific callbacks to use with device |
| 166 | * @config: Configuration for register map |
| 167 | * |
| 168 | * The return value will be an ERR_PTR() on error or a valid pointer to |
| 169 | * a struct regmap. This function should generally not be called |
| 170 | * directly, it should be called by bus-specific init functions. |
| 171 | */ |
| 172 | struct regmap *regmap_init(struct device *dev, |
| 173 | const struct regmap_bus *bus, |
| 174 | const struct regmap_config *config) |
| 175 | { |
| 176 | struct regmap *map; |
| 177 | int ret = -EINVAL; |
| 178 | |
| 179 | if (!bus || !config) |
Lars-Peter Clausen | abbb18f | 2011-11-14 10:40:15 +0100 | [diff] [blame] | 180 | goto err; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 181 | |
| 182 | map = kzalloc(sizeof(*map), GFP_KERNEL); |
| 183 | if (map == NULL) { |
| 184 | ret = -ENOMEM; |
| 185 | goto err; |
| 186 | } |
| 187 | |
| 188 | mutex_init(&map->lock); |
| 189 | map->format.buf_size = (config->reg_bits + config->val_bits) / 8; |
Wolfram Sang | c212acc | 2012-01-28 02:16:41 +0100 | [diff] [blame] | 190 | map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8); |
Mark Brown | 82159ba | 2012-01-18 10:52:25 +0000 | [diff] [blame] | 191 | map->format.pad_bytes = config->pad_bits / 8; |
Wolfram Sang | c212acc | 2012-01-28 02:16:41 +0100 | [diff] [blame] | 192 | map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8); |
Mark Brown | 82159ba | 2012-01-18 10:52:25 +0000 | [diff] [blame] | 193 | map->format.buf_size += map->format.pad_bytes; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 194 | map->dev = dev; |
| 195 | map->bus = bus; |
Mark Brown | 2e2ae66 | 2011-07-20 22:33:39 +0100 | [diff] [blame] | 196 | map->max_register = config->max_register; |
| 197 | map->writeable_reg = config->writeable_reg; |
| 198 | map->readable_reg = config->readable_reg; |
| 199 | map->volatile_reg = config->volatile_reg; |
Mark Brown | 2efe164 | 2011-08-08 15:41:46 +0900 | [diff] [blame] | 200 | map->precious_reg = config->precious_reg; |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 201 | map->cache_type = config->cache_type; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 202 | |
Lars-Peter Clausen | 6f30644 | 2011-09-05 20:46:32 +0200 | [diff] [blame] | 203 | if (config->read_flag_mask || config->write_flag_mask) { |
| 204 | map->read_flag_mask = config->read_flag_mask; |
| 205 | map->write_flag_mask = config->write_flag_mask; |
| 206 | } else { |
| 207 | map->read_flag_mask = bus->read_flag_mask; |
| 208 | } |
| 209 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 210 | switch (config->reg_bits) { |
Wolfram Sang | 9aa5075 | 2012-01-27 16:10:22 +0100 | [diff] [blame] | 211 | case 2: |
| 212 | switch (config->val_bits) { |
| 213 | case 6: |
| 214 | map->format.format_write = regmap_format_2_6_write; |
| 215 | break; |
| 216 | default: |
| 217 | goto err_map; |
| 218 | } |
| 219 | break; |
| 220 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 221 | case 4: |
| 222 | switch (config->val_bits) { |
| 223 | case 12: |
| 224 | map->format.format_write = regmap_format_4_12_write; |
| 225 | break; |
| 226 | default: |
| 227 | goto err_map; |
| 228 | } |
| 229 | break; |
| 230 | |
| 231 | case 7: |
| 232 | switch (config->val_bits) { |
| 233 | case 9: |
| 234 | map->format.format_write = regmap_format_7_9_write; |
| 235 | break; |
| 236 | default: |
| 237 | goto err_map; |
| 238 | } |
| 239 | break; |
| 240 | |
Lars-Peter Clausen | 7e5ec63 | 2011-11-16 16:28:21 +0100 | [diff] [blame] | 241 | case 10: |
| 242 | switch (config->val_bits) { |
| 243 | case 14: |
| 244 | map->format.format_write = regmap_format_10_14_write; |
| 245 | break; |
| 246 | default: |
| 247 | goto err_map; |
| 248 | } |
| 249 | break; |
| 250 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 251 | case 8: |
| 252 | map->format.format_reg = regmap_format_8; |
| 253 | break; |
| 254 | |
| 255 | case 16: |
| 256 | map->format.format_reg = regmap_format_16; |
| 257 | break; |
| 258 | |
Mark Brown | 7d5e525 | 2012-02-17 15:58:25 -0800 | [diff] [blame] | 259 | case 32: |
| 260 | map->format.format_reg = regmap_format_32; |
| 261 | break; |
| 262 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 263 | default: |
| 264 | goto err_map; |
| 265 | } |
| 266 | |
| 267 | switch (config->val_bits) { |
| 268 | case 8: |
| 269 | map->format.format_val = regmap_format_8; |
| 270 | map->format.parse_val = regmap_parse_8; |
| 271 | break; |
| 272 | case 16: |
| 273 | map->format.format_val = regmap_format_16; |
| 274 | map->format.parse_val = regmap_parse_16; |
| 275 | break; |
Mark Brown | 7d5e525 | 2012-02-17 15:58:25 -0800 | [diff] [blame] | 276 | case 32: |
| 277 | map->format.format_val = regmap_format_32; |
| 278 | map->format.parse_val = regmap_parse_32; |
| 279 | break; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | if (!map->format.format_write && |
| 283 | !(map->format.format_reg && map->format.format_val)) |
| 284 | goto err_map; |
| 285 | |
Mark Brown | 82159ba | 2012-01-18 10:52:25 +0000 | [diff] [blame] | 286 | map->work_buf = kzalloc(map->format.buf_size, GFP_KERNEL); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 287 | if (map->work_buf == NULL) { |
| 288 | ret = -ENOMEM; |
Mark Brown | 5204f5e | 2011-09-05 08:07:47 -0700 | [diff] [blame] | 289 | goto err_map; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 290 | } |
| 291 | |
Stephen Warren | d3c242e | 2012-04-04 15:48:29 -0600 | [diff] [blame^] | 292 | regmap_debugfs_init(map, config->name); |
Mark Brown | 052d2cd | 2011-11-21 19:05:13 +0000 | [diff] [blame] | 293 | |
Lars-Peter Clausen | e5e3b8a | 2011-11-16 16:28:16 +0100 | [diff] [blame] | 294 | ret = regcache_init(map, config); |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 295 | if (ret < 0) |
Lars-Peter Clausen | 58072cb | 2011-11-10 18:15:15 +0100 | [diff] [blame] | 296 | goto err_free_workbuf; |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 297 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 298 | return map; |
| 299 | |
Lars-Peter Clausen | 58072cb | 2011-11-10 18:15:15 +0100 | [diff] [blame] | 300 | err_free_workbuf: |
| 301 | kfree(map->work_buf); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 302 | err_map: |
| 303 | kfree(map); |
| 304 | err: |
| 305 | return ERR_PTR(ret); |
| 306 | } |
| 307 | EXPORT_SYMBOL_GPL(regmap_init); |
| 308 | |
Mark Brown | c0eb467 | 2012-01-30 19:56:52 +0000 | [diff] [blame] | 309 | static void devm_regmap_release(struct device *dev, void *res) |
| 310 | { |
| 311 | regmap_exit(*(struct regmap **)res); |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * devm_regmap_init(): Initialise managed register map |
| 316 | * |
| 317 | * @dev: Device that will be interacted with |
| 318 | * @bus: Bus-specific callbacks to use with device |
| 319 | * @config: Configuration for register map |
| 320 | * |
| 321 | * The return value will be an ERR_PTR() on error or a valid pointer |
| 322 | * to a struct regmap. This function should generally not be called |
| 323 | * directly, it should be called by bus-specific init functions. The |
| 324 | * map will be automatically freed by the device management code. |
| 325 | */ |
| 326 | struct regmap *devm_regmap_init(struct device *dev, |
| 327 | const struct regmap_bus *bus, |
| 328 | const struct regmap_config *config) |
| 329 | { |
| 330 | struct regmap **ptr, *regmap; |
| 331 | |
| 332 | ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL); |
| 333 | if (!ptr) |
| 334 | return ERR_PTR(-ENOMEM); |
| 335 | |
| 336 | regmap = regmap_init(dev, bus, config); |
| 337 | if (!IS_ERR(regmap)) { |
| 338 | *ptr = regmap; |
| 339 | devres_add(dev, ptr); |
| 340 | } else { |
| 341 | devres_free(ptr); |
| 342 | } |
| 343 | |
| 344 | return regmap; |
| 345 | } |
| 346 | EXPORT_SYMBOL_GPL(devm_regmap_init); |
| 347 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 348 | /** |
Mark Brown | bf31517 | 2011-12-03 17:06:20 +0000 | [diff] [blame] | 349 | * regmap_reinit_cache(): Reinitialise the current register cache |
| 350 | * |
| 351 | * @map: Register map to operate on. |
| 352 | * @config: New configuration. Only the cache data will be used. |
| 353 | * |
| 354 | * Discard any existing register cache for the map and initialize a |
| 355 | * new cache. This can be used to restore the cache to defaults or to |
| 356 | * update the cache configuration to reflect runtime discovery of the |
| 357 | * hardware. |
| 358 | */ |
| 359 | int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config) |
| 360 | { |
| 361 | int ret; |
| 362 | |
| 363 | mutex_lock(&map->lock); |
| 364 | |
| 365 | regcache_exit(map); |
Mark Brown | a24f64a | 2012-01-26 18:30:16 +0000 | [diff] [blame] | 366 | regmap_debugfs_exit(map); |
Mark Brown | bf31517 | 2011-12-03 17:06:20 +0000 | [diff] [blame] | 367 | |
| 368 | map->max_register = config->max_register; |
| 369 | map->writeable_reg = config->writeable_reg; |
| 370 | map->readable_reg = config->readable_reg; |
| 371 | map->volatile_reg = config->volatile_reg; |
| 372 | map->precious_reg = config->precious_reg; |
| 373 | map->cache_type = config->cache_type; |
| 374 | |
Stephen Warren | d3c242e | 2012-04-04 15:48:29 -0600 | [diff] [blame^] | 375 | regmap_debugfs_init(map, config->name); |
Mark Brown | a24f64a | 2012-01-26 18:30:16 +0000 | [diff] [blame] | 376 | |
Mark Brown | 421e8d2 | 2012-01-20 13:39:37 +0000 | [diff] [blame] | 377 | map->cache_bypass = false; |
| 378 | map->cache_only = false; |
| 379 | |
Mark Brown | bf31517 | 2011-12-03 17:06:20 +0000 | [diff] [blame] | 380 | ret = regcache_init(map, config); |
| 381 | |
| 382 | mutex_unlock(&map->lock); |
| 383 | |
| 384 | return ret; |
| 385 | } |
| 386 | |
| 387 | /** |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 388 | * regmap_exit(): Free a previously allocated register map |
| 389 | */ |
| 390 | void regmap_exit(struct regmap *map) |
| 391 | { |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 392 | regcache_exit(map); |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 393 | regmap_debugfs_exit(map); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 394 | kfree(map->work_buf); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 395 | kfree(map); |
| 396 | } |
| 397 | EXPORT_SYMBOL_GPL(regmap_exit); |
| 398 | |
| 399 | static int _regmap_raw_write(struct regmap *map, unsigned int reg, |
| 400 | const void *val, size_t val_len) |
| 401 | { |
Lars-Peter Clausen | 6f30644 | 2011-09-05 20:46:32 +0200 | [diff] [blame] | 402 | u8 *u8 = map->work_buf; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 403 | void *buf; |
| 404 | int ret = -ENOTSUPP; |
| 405 | size_t len; |
Mark Brown | 7330478 | 2011-07-24 11:46:20 +0100 | [diff] [blame] | 406 | int i; |
| 407 | |
| 408 | /* Check for unwritable registers before we start */ |
| 409 | if (map->writeable_reg) |
| 410 | for (i = 0; i < val_len / map->format.val_bytes; i++) |
| 411 | if (!map->writeable_reg(map->dev, reg + i)) |
| 412 | return -EINVAL; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 413 | |
Laxman Dewangan | c915719 | 2012-02-10 21:30:27 +0530 | [diff] [blame] | 414 | if (!map->cache_bypass && map->format.parse_val) { |
| 415 | unsigned int ival; |
| 416 | int val_bytes = map->format.val_bytes; |
| 417 | for (i = 0; i < val_len / val_bytes; i++) { |
| 418 | memcpy(map->work_buf, val + (i * val_bytes), val_bytes); |
| 419 | ival = map->format.parse_val(map->work_buf); |
| 420 | ret = regcache_write(map, reg + i, ival); |
| 421 | if (ret) { |
| 422 | dev_err(map->dev, |
| 423 | "Error in caching of register: %u ret: %d\n", |
| 424 | reg + i, ret); |
| 425 | return ret; |
| 426 | } |
| 427 | } |
| 428 | if (map->cache_only) { |
| 429 | map->cache_dirty = true; |
| 430 | return 0; |
| 431 | } |
| 432 | } |
| 433 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 434 | map->format.format_reg(map->work_buf, reg); |
| 435 | |
Lars-Peter Clausen | 6f30644 | 2011-09-05 20:46:32 +0200 | [diff] [blame] | 436 | u8[0] |= map->write_flag_mask; |
| 437 | |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 438 | trace_regmap_hw_write_start(map->dev, reg, |
| 439 | val_len / map->format.val_bytes); |
| 440 | |
Mark Brown | 2547e20 | 2011-07-20 21:47:22 +0100 | [diff] [blame] | 441 | /* If we're doing a single register write we can probably just |
| 442 | * send the work_buf directly, otherwise try to do a gather |
| 443 | * write. |
| 444 | */ |
Mark Brown | 82159ba | 2012-01-18 10:52:25 +0000 | [diff] [blame] | 445 | if (val == (map->work_buf + map->format.pad_bytes + |
| 446 | map->format.reg_bytes)) |
Mark Brown | 2547e20 | 2011-07-20 21:47:22 +0100 | [diff] [blame] | 447 | ret = map->bus->write(map->dev, map->work_buf, |
Mark Brown | 82159ba | 2012-01-18 10:52:25 +0000 | [diff] [blame] | 448 | map->format.reg_bytes + |
| 449 | map->format.pad_bytes + |
| 450 | val_len); |
Mark Brown | 2547e20 | 2011-07-20 21:47:22 +0100 | [diff] [blame] | 451 | else if (map->bus->gather_write) |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 452 | ret = map->bus->gather_write(map->dev, map->work_buf, |
Mark Brown | 82159ba | 2012-01-18 10:52:25 +0000 | [diff] [blame] | 453 | map->format.reg_bytes + |
| 454 | map->format.pad_bytes, |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 455 | val, val_len); |
| 456 | |
Mark Brown | 2547e20 | 2011-07-20 21:47:22 +0100 | [diff] [blame] | 457 | /* If that didn't work fall back on linearising by hand. */ |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 458 | if (ret == -ENOTSUPP) { |
Mark Brown | 82159ba | 2012-01-18 10:52:25 +0000 | [diff] [blame] | 459 | len = map->format.reg_bytes + map->format.pad_bytes + val_len; |
| 460 | buf = kzalloc(len, GFP_KERNEL); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 461 | if (!buf) |
| 462 | return -ENOMEM; |
| 463 | |
| 464 | memcpy(buf, map->work_buf, map->format.reg_bytes); |
Mark Brown | 82159ba | 2012-01-18 10:52:25 +0000 | [diff] [blame] | 465 | memcpy(buf + map->format.reg_bytes + map->format.pad_bytes, |
| 466 | val, val_len); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 467 | ret = map->bus->write(map->dev, buf, len); |
| 468 | |
| 469 | kfree(buf); |
| 470 | } |
| 471 | |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 472 | trace_regmap_hw_write_done(map->dev, reg, |
| 473 | val_len / map->format.val_bytes); |
| 474 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 475 | return ret; |
| 476 | } |
| 477 | |
Dimitris Papastamos | 4d2dc09 | 2011-09-29 10:39:07 +0100 | [diff] [blame] | 478 | int _regmap_write(struct regmap *map, unsigned int reg, |
| 479 | unsigned int val) |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 480 | { |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 481 | int ret; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 482 | BUG_ON(!map->format.format_write && !map->format.format_val); |
| 483 | |
Laxman Dewangan | c915719 | 2012-02-10 21:30:27 +0530 | [diff] [blame] | 484 | if (!map->cache_bypass && map->format.format_write) { |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 485 | ret = regcache_write(map, reg, val); |
| 486 | if (ret != 0) |
| 487 | return ret; |
Mark Brown | 8ae0d7e | 2011-10-26 10:34:22 +0200 | [diff] [blame] | 488 | if (map->cache_only) { |
| 489 | map->cache_dirty = true; |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 490 | return 0; |
Mark Brown | 8ae0d7e | 2011-10-26 10:34:22 +0200 | [diff] [blame] | 491 | } |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 492 | } |
| 493 | |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 494 | trace_regmap_reg_write(map->dev, reg, val); |
| 495 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 496 | if (map->format.format_write) { |
| 497 | map->format.format_write(map, reg, val); |
| 498 | |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 499 | trace_regmap_hw_write_start(map->dev, reg, 1); |
| 500 | |
| 501 | ret = map->bus->write(map->dev, map->work_buf, |
| 502 | map->format.buf_size); |
| 503 | |
| 504 | trace_regmap_hw_write_done(map->dev, reg, 1); |
| 505 | |
| 506 | return ret; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 507 | } else { |
Mark Brown | 82159ba | 2012-01-18 10:52:25 +0000 | [diff] [blame] | 508 | map->format.format_val(map->work_buf + map->format.reg_bytes |
| 509 | + map->format.pad_bytes, val); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 510 | return _regmap_raw_write(map, reg, |
Mark Brown | 82159ba | 2012-01-18 10:52:25 +0000 | [diff] [blame] | 511 | map->work_buf + |
| 512 | map->format.reg_bytes + |
| 513 | map->format.pad_bytes, |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 514 | map->format.val_bytes); |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | /** |
| 519 | * regmap_write(): Write a value to a single register |
| 520 | * |
| 521 | * @map: Register map to write to |
| 522 | * @reg: Register to write to |
| 523 | * @val: Value to be written |
| 524 | * |
| 525 | * A value of zero will be returned on success, a negative errno will |
| 526 | * be returned in error cases. |
| 527 | */ |
| 528 | int regmap_write(struct regmap *map, unsigned int reg, unsigned int val) |
| 529 | { |
| 530 | int ret; |
| 531 | |
| 532 | mutex_lock(&map->lock); |
| 533 | |
| 534 | ret = _regmap_write(map, reg, val); |
| 535 | |
| 536 | mutex_unlock(&map->lock); |
| 537 | |
| 538 | return ret; |
| 539 | } |
| 540 | EXPORT_SYMBOL_GPL(regmap_write); |
| 541 | |
| 542 | /** |
| 543 | * regmap_raw_write(): Write raw values to one or more registers |
| 544 | * |
| 545 | * @map: Register map to write to |
| 546 | * @reg: Initial register to write to |
| 547 | * @val: Block of data to be written, laid out for direct transmission to the |
| 548 | * device |
| 549 | * @val_len: Length of data pointed to by val. |
| 550 | * |
| 551 | * This function is intended to be used for things like firmware |
| 552 | * download where a large block of data needs to be transferred to the |
| 553 | * device. No formatting will be done on the data provided. |
| 554 | * |
| 555 | * A value of zero will be returned on success, a negative errno will |
| 556 | * be returned in error cases. |
| 557 | */ |
| 558 | int regmap_raw_write(struct regmap *map, unsigned int reg, |
| 559 | const void *val, size_t val_len) |
| 560 | { |
| 561 | int ret; |
| 562 | |
| 563 | mutex_lock(&map->lock); |
| 564 | |
| 565 | ret = _regmap_raw_write(map, reg, val, val_len); |
| 566 | |
| 567 | mutex_unlock(&map->lock); |
| 568 | |
| 569 | return ret; |
| 570 | } |
| 571 | EXPORT_SYMBOL_GPL(regmap_raw_write); |
| 572 | |
Laxman Dewangan | 8eaeb21 | 2012-02-12 19:49:43 +0530 | [diff] [blame] | 573 | /* |
| 574 | * regmap_bulk_write(): Write multiple registers to the device |
| 575 | * |
| 576 | * @map: Register map to write to |
| 577 | * @reg: First register to be write from |
| 578 | * @val: Block of data to be written, in native register size for device |
| 579 | * @val_count: Number of registers to write |
| 580 | * |
| 581 | * This function is intended to be used for writing a large block of |
| 582 | * data to be device either in single transfer or multiple transfer. |
| 583 | * |
| 584 | * A value of zero will be returned on success, a negative errno will |
| 585 | * be returned in error cases. |
| 586 | */ |
| 587 | int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val, |
| 588 | size_t val_count) |
| 589 | { |
| 590 | int ret = 0, i; |
| 591 | size_t val_bytes = map->format.val_bytes; |
| 592 | void *wval; |
| 593 | |
| 594 | if (!map->format.parse_val) |
| 595 | return -EINVAL; |
| 596 | |
| 597 | mutex_lock(&map->lock); |
| 598 | |
| 599 | /* No formatting is require if val_byte is 1 */ |
| 600 | if (val_bytes == 1) { |
| 601 | wval = (void *)val; |
| 602 | } else { |
| 603 | wval = kmemdup(val, val_count * val_bytes, GFP_KERNEL); |
| 604 | if (!wval) { |
| 605 | ret = -ENOMEM; |
| 606 | dev_err(map->dev, "Error in memory allocation\n"); |
| 607 | goto out; |
| 608 | } |
| 609 | for (i = 0; i < val_count * val_bytes; i += val_bytes) |
| 610 | map->format.parse_val(wval + i); |
| 611 | } |
| 612 | ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count); |
| 613 | |
| 614 | if (val_bytes != 1) |
| 615 | kfree(wval); |
| 616 | |
| 617 | out: |
| 618 | mutex_unlock(&map->lock); |
| 619 | return ret; |
| 620 | } |
| 621 | EXPORT_SYMBOL_GPL(regmap_bulk_write); |
| 622 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 623 | static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val, |
| 624 | unsigned int val_len) |
| 625 | { |
| 626 | u8 *u8 = map->work_buf; |
| 627 | int ret; |
| 628 | |
| 629 | map->format.format_reg(map->work_buf, reg); |
| 630 | |
| 631 | /* |
Lars-Peter Clausen | 6f30644 | 2011-09-05 20:46:32 +0200 | [diff] [blame] | 632 | * Some buses or devices flag reads by setting the high bits in the |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 633 | * register addresss; since it's always the high bits for all |
| 634 | * current formats we can do this here rather than in |
| 635 | * formatting. This may break if we get interesting formats. |
| 636 | */ |
Lars-Peter Clausen | 6f30644 | 2011-09-05 20:46:32 +0200 | [diff] [blame] | 637 | u8[0] |= map->read_flag_mask; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 638 | |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 639 | trace_regmap_hw_read_start(map->dev, reg, |
| 640 | val_len / map->format.val_bytes); |
| 641 | |
Mark Brown | 82159ba | 2012-01-18 10:52:25 +0000 | [diff] [blame] | 642 | ret = map->bus->read(map->dev, map->work_buf, |
| 643 | map->format.reg_bytes + map->format.pad_bytes, |
Mark Brown | 40c5cc2 | 2011-07-24 22:39:12 +0100 | [diff] [blame] | 644 | val, val_len); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 645 | |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 646 | trace_regmap_hw_read_done(map->dev, reg, |
| 647 | val_len / map->format.val_bytes); |
| 648 | |
| 649 | return ret; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | static int _regmap_read(struct regmap *map, unsigned int reg, |
| 653 | unsigned int *val) |
| 654 | { |
| 655 | int ret; |
| 656 | |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 657 | if (!map->cache_bypass) { |
| 658 | ret = regcache_read(map, reg, val); |
| 659 | if (ret == 0) |
| 660 | return 0; |
| 661 | } |
| 662 | |
Lars-Peter Clausen | 1925441 | 2011-11-16 16:28:19 +0100 | [diff] [blame] | 663 | if (!map->format.parse_val) |
| 664 | return -EINVAL; |
| 665 | |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 666 | if (map->cache_only) |
| 667 | return -EBUSY; |
| 668 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 669 | ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes); |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 670 | if (ret == 0) { |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 671 | *val = map->format.parse_val(map->work_buf); |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 672 | trace_regmap_reg_read(map->dev, reg, *val); |
| 673 | } |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 674 | |
| 675 | return ret; |
| 676 | } |
| 677 | |
| 678 | /** |
| 679 | * regmap_read(): Read a value from a single register |
| 680 | * |
| 681 | * @map: Register map to write to |
| 682 | * @reg: Register to be read from |
| 683 | * @val: Pointer to store read value |
| 684 | * |
| 685 | * A value of zero will be returned on success, a negative errno will |
| 686 | * be returned in error cases. |
| 687 | */ |
| 688 | int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val) |
| 689 | { |
| 690 | int ret; |
| 691 | |
| 692 | mutex_lock(&map->lock); |
| 693 | |
| 694 | ret = _regmap_read(map, reg, val); |
| 695 | |
| 696 | mutex_unlock(&map->lock); |
| 697 | |
| 698 | return ret; |
| 699 | } |
| 700 | EXPORT_SYMBOL_GPL(regmap_read); |
| 701 | |
| 702 | /** |
| 703 | * regmap_raw_read(): Read raw data from the device |
| 704 | * |
| 705 | * @map: Register map to write to |
| 706 | * @reg: First register to be read from |
| 707 | * @val: Pointer to store read value |
| 708 | * @val_len: Size of data to read |
| 709 | * |
| 710 | * A value of zero will be returned on success, a negative errno will |
| 711 | * be returned in error cases. |
| 712 | */ |
| 713 | int regmap_raw_read(struct regmap *map, unsigned int reg, void *val, |
| 714 | size_t val_len) |
| 715 | { |
Mark Brown | b8fb5ab | 2012-02-21 19:12:47 +0000 | [diff] [blame] | 716 | size_t val_bytes = map->format.val_bytes; |
| 717 | size_t val_count = val_len / val_bytes; |
| 718 | unsigned int v; |
| 719 | int ret, i; |
Mark Brown | 04e016a | 2011-10-09 13:35:43 +0100 | [diff] [blame] | 720 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 721 | mutex_lock(&map->lock); |
| 722 | |
Mark Brown | b8fb5ab | 2012-02-21 19:12:47 +0000 | [diff] [blame] | 723 | if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass || |
| 724 | map->cache_type == REGCACHE_NONE) { |
| 725 | /* Physical block read if there's no cache involved */ |
| 726 | ret = _regmap_raw_read(map, reg, val, val_len); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 727 | |
Mark Brown | b8fb5ab | 2012-02-21 19:12:47 +0000 | [diff] [blame] | 728 | } else { |
| 729 | /* Otherwise go word by word for the cache; should be low |
| 730 | * cost as we expect to hit the cache. |
| 731 | */ |
| 732 | for (i = 0; i < val_count; i++) { |
| 733 | ret = _regmap_read(map, reg + i, &v); |
| 734 | if (ret != 0) |
| 735 | goto out; |
| 736 | |
| 737 | map->format.format_val(val + (i * val_bytes), v); |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | out: |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 742 | mutex_unlock(&map->lock); |
| 743 | |
| 744 | return ret; |
| 745 | } |
| 746 | EXPORT_SYMBOL_GPL(regmap_raw_read); |
| 747 | |
| 748 | /** |
| 749 | * regmap_bulk_read(): Read multiple registers from the device |
| 750 | * |
| 751 | * @map: Register map to write to |
| 752 | * @reg: First register to be read from |
| 753 | * @val: Pointer to store read value, in native register size for device |
| 754 | * @val_count: Number of registers to read |
| 755 | * |
| 756 | * A value of zero will be returned on success, a negative errno will |
| 757 | * be returned in error cases. |
| 758 | */ |
| 759 | int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val, |
| 760 | size_t val_count) |
| 761 | { |
| 762 | int ret, i; |
| 763 | size_t val_bytes = map->format.val_bytes; |
Lars-Peter Clausen | 82cd996 | 2011-11-08 18:37:25 +0100 | [diff] [blame] | 764 | bool vol = regmap_volatile_range(map, reg, val_count); |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 765 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 766 | if (!map->format.parse_val) |
| 767 | return -EINVAL; |
| 768 | |
Mark Brown | de2d808 | 2011-10-10 13:24:52 +0100 | [diff] [blame] | 769 | if (vol || map->cache_type == REGCACHE_NONE) { |
| 770 | ret = regmap_raw_read(map, reg, val, val_bytes * val_count); |
| 771 | if (ret != 0) |
| 772 | return ret; |
| 773 | |
| 774 | for (i = 0; i < val_count * val_bytes; i += val_bytes) |
| 775 | map->format.parse_val(val + i); |
| 776 | } else { |
| 777 | for (i = 0; i < val_count; i++) { |
| 778 | ret = regmap_read(map, reg + i, val + (i * val_bytes)); |
| 779 | if (ret != 0) |
| 780 | return ret; |
| 781 | } |
| 782 | } |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 783 | |
| 784 | return 0; |
| 785 | } |
| 786 | EXPORT_SYMBOL_GPL(regmap_bulk_read); |
| 787 | |
Mark Brown | 018690d | 2011-11-29 20:10:36 +0000 | [diff] [blame] | 788 | static int _regmap_update_bits(struct regmap *map, unsigned int reg, |
| 789 | unsigned int mask, unsigned int val, |
| 790 | bool *change) |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 791 | { |
| 792 | int ret; |
Mark Brown | d91e8db2 | 2011-11-18 16:03:50 +0000 | [diff] [blame] | 793 | unsigned int tmp, orig; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 794 | |
| 795 | mutex_lock(&map->lock); |
| 796 | |
Mark Brown | d91e8db2 | 2011-11-18 16:03:50 +0000 | [diff] [blame] | 797 | ret = _regmap_read(map, reg, &orig); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 798 | if (ret != 0) |
| 799 | goto out; |
| 800 | |
Mark Brown | d91e8db2 | 2011-11-18 16:03:50 +0000 | [diff] [blame] | 801 | tmp = orig & ~mask; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 802 | tmp |= val & mask; |
| 803 | |
Mark Brown | 018690d | 2011-11-29 20:10:36 +0000 | [diff] [blame] | 804 | if (tmp != orig) { |
Mark Brown | d91e8db2 | 2011-11-18 16:03:50 +0000 | [diff] [blame] | 805 | ret = _regmap_write(map, reg, tmp); |
Mark Brown | 018690d | 2011-11-29 20:10:36 +0000 | [diff] [blame] | 806 | *change = true; |
| 807 | } else { |
| 808 | *change = false; |
| 809 | } |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 810 | |
| 811 | out: |
| 812 | mutex_unlock(&map->lock); |
| 813 | |
| 814 | return ret; |
| 815 | } |
Mark Brown | 018690d | 2011-11-29 20:10:36 +0000 | [diff] [blame] | 816 | |
| 817 | /** |
| 818 | * regmap_update_bits: Perform a read/modify/write cycle on the register map |
| 819 | * |
| 820 | * @map: Register map to update |
| 821 | * @reg: Register to update |
| 822 | * @mask: Bitmask to change |
| 823 | * @val: New value for bitmask |
| 824 | * |
| 825 | * Returns zero for success, a negative number on error. |
| 826 | */ |
| 827 | int regmap_update_bits(struct regmap *map, unsigned int reg, |
| 828 | unsigned int mask, unsigned int val) |
| 829 | { |
| 830 | bool change; |
| 831 | return _regmap_update_bits(map, reg, mask, val, &change); |
| 832 | } |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 833 | EXPORT_SYMBOL_GPL(regmap_update_bits); |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 834 | |
Mark Brown | 018690d | 2011-11-29 20:10:36 +0000 | [diff] [blame] | 835 | /** |
| 836 | * regmap_update_bits_check: Perform a read/modify/write cycle on the |
| 837 | * register map and report if updated |
| 838 | * |
| 839 | * @map: Register map to update |
| 840 | * @reg: Register to update |
| 841 | * @mask: Bitmask to change |
| 842 | * @val: New value for bitmask |
| 843 | * @change: Boolean indicating if a write was done |
| 844 | * |
| 845 | * Returns zero for success, a negative number on error. |
| 846 | */ |
| 847 | int regmap_update_bits_check(struct regmap *map, unsigned int reg, |
| 848 | unsigned int mask, unsigned int val, |
| 849 | bool *change) |
| 850 | { |
| 851 | return _regmap_update_bits(map, reg, mask, val, change); |
| 852 | } |
| 853 | EXPORT_SYMBOL_GPL(regmap_update_bits_check); |
| 854 | |
Mark Brown | 22f0d90 | 2012-01-21 12:01:14 +0000 | [diff] [blame] | 855 | /** |
| 856 | * regmap_register_patch: Register and apply register updates to be applied |
| 857 | * on device initialistion |
| 858 | * |
| 859 | * @map: Register map to apply updates to. |
| 860 | * @regs: Values to update. |
| 861 | * @num_regs: Number of entries in regs. |
| 862 | * |
| 863 | * Register a set of register updates to be applied to the device |
| 864 | * whenever the device registers are synchronised with the cache and |
| 865 | * apply them immediately. Typically this is used to apply |
| 866 | * corrections to be applied to the device defaults on startup, such |
| 867 | * as the updates some vendors provide to undocumented registers. |
| 868 | */ |
| 869 | int regmap_register_patch(struct regmap *map, const struct reg_default *regs, |
| 870 | int num_regs) |
| 871 | { |
| 872 | int i, ret; |
| 873 | bool bypass; |
| 874 | |
| 875 | /* If needed the implementation can be extended to support this */ |
| 876 | if (map->patch) |
| 877 | return -EBUSY; |
| 878 | |
| 879 | mutex_lock(&map->lock); |
| 880 | |
| 881 | bypass = map->cache_bypass; |
| 882 | |
| 883 | map->cache_bypass = true; |
| 884 | |
| 885 | /* Write out first; it's useful to apply even if we fail later. */ |
| 886 | for (i = 0; i < num_regs; i++) { |
| 887 | ret = _regmap_write(map, regs[i].reg, regs[i].def); |
| 888 | if (ret != 0) { |
| 889 | dev_err(map->dev, "Failed to write %x = %x: %d\n", |
| 890 | regs[i].reg, regs[i].def, ret); |
| 891 | goto out; |
| 892 | } |
| 893 | } |
| 894 | |
Axel Lin | 2a14d7d9 | 2012-02-10 19:29:55 +0800 | [diff] [blame] | 895 | map->patch = kcalloc(num_regs, sizeof(struct reg_default), GFP_KERNEL); |
Mark Brown | 22f0d90 | 2012-01-21 12:01:14 +0000 | [diff] [blame] | 896 | if (map->patch != NULL) { |
| 897 | memcpy(map->patch, regs, |
| 898 | num_regs * sizeof(struct reg_default)); |
| 899 | map->patch_regs = num_regs; |
| 900 | } else { |
| 901 | ret = -ENOMEM; |
| 902 | } |
| 903 | |
| 904 | out: |
| 905 | map->cache_bypass = bypass; |
| 906 | |
| 907 | mutex_unlock(&map->lock); |
| 908 | |
| 909 | return ret; |
| 910 | } |
| 911 | EXPORT_SYMBOL_GPL(regmap_register_patch); |
| 912 | |
Mark Brown | eae4b51 | 2012-03-14 13:15:03 +0000 | [diff] [blame] | 913 | /* |
Mark Brown | a6539c3 | 2012-02-17 14:20:14 -0800 | [diff] [blame] | 914 | * regmap_get_val_bytes(): Report the size of a register value |
| 915 | * |
| 916 | * Report the size of a register value, mainly intended to for use by |
| 917 | * generic infrastructure built on top of regmap. |
| 918 | */ |
| 919 | int regmap_get_val_bytes(struct regmap *map) |
| 920 | { |
| 921 | if (map->format.format_write) |
| 922 | return -EINVAL; |
| 923 | |
| 924 | return map->format.val_bytes; |
| 925 | } |
| 926 | EXPORT_SYMBOL_GPL(regmap_get_val_bytes); |
| 927 | |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 928 | static int __init regmap_initcall(void) |
| 929 | { |
| 930 | regmap_debugfs_initcall(); |
| 931 | |
| 932 | return 0; |
| 933 | } |
| 934 | postcore_initcall(regmap_initcall); |