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