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 | |
| 13 | #include <linux/slab.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/mutex.h> |
| 16 | #include <linux/err.h> |
| 17 | |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 18 | #define CREATE_TRACE_POINTS |
| 19 | #include <trace/events/regmap.h> |
| 20 | |
Mark Brown | 93de912 | 2011-07-20 22:35:37 +0100 | [diff] [blame] | 21 | #include "internal.h" |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 22 | |
Mark Brown | 8de2f08 | 2011-08-10 17:14:41 +0900 | [diff] [blame] | 23 | bool regmap_writeable(struct regmap *map, unsigned int reg) |
| 24 | { |
| 25 | if (map->max_register && reg > map->max_register) |
| 26 | return false; |
| 27 | |
| 28 | if (map->writeable_reg) |
| 29 | return map->writeable_reg(map->dev, reg); |
| 30 | |
| 31 | return true; |
| 32 | } |
| 33 | |
| 34 | bool regmap_readable(struct regmap *map, unsigned int reg) |
| 35 | { |
| 36 | if (map->max_register && reg > map->max_register) |
| 37 | return false; |
| 38 | |
| 39 | if (map->readable_reg) |
| 40 | return map->readable_reg(map->dev, reg); |
| 41 | |
| 42 | return true; |
| 43 | } |
| 44 | |
| 45 | bool regmap_volatile(struct regmap *map, unsigned int reg) |
| 46 | { |
| 47 | if (map->max_register && reg > map->max_register) |
| 48 | return false; |
| 49 | |
| 50 | if (map->volatile_reg) |
| 51 | return map->volatile_reg(map->dev, reg); |
| 52 | |
| 53 | return true; |
| 54 | } |
| 55 | |
| 56 | bool regmap_precious(struct regmap *map, unsigned int reg) |
| 57 | { |
| 58 | if (map->max_register && reg > map->max_register) |
| 59 | return false; |
| 60 | |
| 61 | if (map->precious_reg) |
| 62 | return map->precious_reg(map->dev, reg); |
| 63 | |
| 64 | return false; |
| 65 | } |
| 66 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 67 | static void regmap_format_4_12_write(struct regmap *map, |
| 68 | unsigned int reg, unsigned int val) |
| 69 | { |
| 70 | __be16 *out = map->work_buf; |
| 71 | *out = cpu_to_be16((reg << 12) | val); |
| 72 | } |
| 73 | |
| 74 | static void regmap_format_7_9_write(struct regmap *map, |
| 75 | unsigned int reg, unsigned int val) |
| 76 | { |
| 77 | __be16 *out = map->work_buf; |
| 78 | *out = cpu_to_be16((reg << 9) | val); |
| 79 | } |
| 80 | |
| 81 | static void regmap_format_8(void *buf, unsigned int val) |
| 82 | { |
| 83 | u8 *b = buf; |
| 84 | |
| 85 | b[0] = val; |
| 86 | } |
| 87 | |
| 88 | static void regmap_format_16(void *buf, unsigned int val) |
| 89 | { |
| 90 | __be16 *b = buf; |
| 91 | |
| 92 | b[0] = cpu_to_be16(val); |
| 93 | } |
| 94 | |
| 95 | static unsigned int regmap_parse_8(void *buf) |
| 96 | { |
| 97 | u8 *b = buf; |
| 98 | |
| 99 | return b[0]; |
| 100 | } |
| 101 | |
| 102 | static unsigned int regmap_parse_16(void *buf) |
| 103 | { |
| 104 | __be16 *b = buf; |
| 105 | |
| 106 | b[0] = be16_to_cpu(b[0]); |
| 107 | |
| 108 | return b[0]; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * regmap_init(): Initialise register map |
| 113 | * |
| 114 | * @dev: Device that will be interacted with |
| 115 | * @bus: Bus-specific callbacks to use with device |
| 116 | * @config: Configuration for register map |
| 117 | * |
| 118 | * The return value will be an ERR_PTR() on error or a valid pointer to |
| 119 | * a struct regmap. This function should generally not be called |
| 120 | * directly, it should be called by bus-specific init functions. |
| 121 | */ |
| 122 | struct regmap *regmap_init(struct device *dev, |
| 123 | const struct regmap_bus *bus, |
| 124 | const struct regmap_config *config) |
| 125 | { |
| 126 | struct regmap *map; |
| 127 | int ret = -EINVAL; |
| 128 | |
| 129 | if (!bus || !config) |
| 130 | return NULL; |
| 131 | |
| 132 | map = kzalloc(sizeof(*map), GFP_KERNEL); |
| 133 | if (map == NULL) { |
| 134 | ret = -ENOMEM; |
| 135 | goto err; |
| 136 | } |
| 137 | |
| 138 | mutex_init(&map->lock); |
| 139 | map->format.buf_size = (config->reg_bits + config->val_bits) / 8; |
| 140 | map->format.reg_bytes = config->reg_bits / 8; |
| 141 | map->format.val_bytes = config->val_bits / 8; |
| 142 | map->dev = dev; |
| 143 | map->bus = bus; |
Mark Brown | 2e2ae66 | 2011-07-20 22:33:39 +0100 | [diff] [blame] | 144 | map->max_register = config->max_register; |
| 145 | map->writeable_reg = config->writeable_reg; |
| 146 | map->readable_reg = config->readable_reg; |
| 147 | map->volatile_reg = config->volatile_reg; |
Mark Brown | 2efe164 | 2011-08-08 15:41:46 +0900 | [diff] [blame] | 148 | map->precious_reg = config->precious_reg; |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 149 | map->cache_type = config->cache_type; |
| 150 | map->reg_defaults = config->reg_defaults; |
| 151 | map->num_reg_defaults = config->num_reg_defaults; |
| 152 | map->num_reg_defaults_raw = config->num_reg_defaults_raw; |
| 153 | map->reg_defaults_raw = config->reg_defaults_raw; |
| 154 | map->cache_size_raw = (config->val_bits / 8) * config->num_reg_defaults_raw; |
| 155 | map->cache_word_size = config->val_bits / 8; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 156 | |
Lars-Peter Clausen | 6f30644 | 2011-09-05 20:46:32 +0200 | [diff] [blame] | 157 | if (config->read_flag_mask || config->write_flag_mask) { |
| 158 | map->read_flag_mask = config->read_flag_mask; |
| 159 | map->write_flag_mask = config->write_flag_mask; |
| 160 | } else { |
| 161 | map->read_flag_mask = bus->read_flag_mask; |
| 162 | } |
| 163 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 164 | switch (config->reg_bits) { |
| 165 | case 4: |
| 166 | switch (config->val_bits) { |
| 167 | case 12: |
| 168 | map->format.format_write = regmap_format_4_12_write; |
| 169 | break; |
| 170 | default: |
| 171 | goto err_map; |
| 172 | } |
| 173 | break; |
| 174 | |
| 175 | case 7: |
| 176 | switch (config->val_bits) { |
| 177 | case 9: |
| 178 | map->format.format_write = regmap_format_7_9_write; |
| 179 | break; |
| 180 | default: |
| 181 | goto err_map; |
| 182 | } |
| 183 | break; |
| 184 | |
| 185 | case 8: |
| 186 | map->format.format_reg = regmap_format_8; |
| 187 | break; |
| 188 | |
| 189 | case 16: |
| 190 | map->format.format_reg = regmap_format_16; |
| 191 | break; |
| 192 | |
| 193 | default: |
| 194 | goto err_map; |
| 195 | } |
| 196 | |
| 197 | switch (config->val_bits) { |
| 198 | case 8: |
| 199 | map->format.format_val = regmap_format_8; |
| 200 | map->format.parse_val = regmap_parse_8; |
| 201 | break; |
| 202 | case 16: |
| 203 | map->format.format_val = regmap_format_16; |
| 204 | map->format.parse_val = regmap_parse_16; |
| 205 | break; |
| 206 | } |
| 207 | |
| 208 | if (!map->format.format_write && |
| 209 | !(map->format.format_reg && map->format.format_val)) |
| 210 | goto err_map; |
| 211 | |
| 212 | map->work_buf = kmalloc(map->format.buf_size, GFP_KERNEL); |
| 213 | if (map->work_buf == NULL) { |
| 214 | ret = -ENOMEM; |
Mark Brown | 5204f5e | 2011-09-05 08:07:47 -0700 | [diff] [blame] | 215 | goto err_map; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 216 | } |
| 217 | |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 218 | ret = regcache_init(map); |
| 219 | if (ret < 0) |
| 220 | goto err_map; |
| 221 | |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 222 | regmap_debugfs_init(map); |
| 223 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 224 | return map; |
| 225 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 226 | err_map: |
| 227 | kfree(map); |
| 228 | err: |
| 229 | return ERR_PTR(ret); |
| 230 | } |
| 231 | EXPORT_SYMBOL_GPL(regmap_init); |
| 232 | |
| 233 | /** |
| 234 | * regmap_exit(): Free a previously allocated register map |
| 235 | */ |
| 236 | void regmap_exit(struct regmap *map) |
| 237 | { |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 238 | regcache_exit(map); |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 239 | regmap_debugfs_exit(map); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 240 | kfree(map->work_buf); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 241 | kfree(map); |
| 242 | } |
| 243 | EXPORT_SYMBOL_GPL(regmap_exit); |
| 244 | |
| 245 | static int _regmap_raw_write(struct regmap *map, unsigned int reg, |
| 246 | const void *val, size_t val_len) |
| 247 | { |
Lars-Peter Clausen | 6f30644 | 2011-09-05 20:46:32 +0200 | [diff] [blame] | 248 | u8 *u8 = map->work_buf; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 249 | void *buf; |
| 250 | int ret = -ENOTSUPP; |
| 251 | size_t len; |
Mark Brown | 7330478 | 2011-07-24 11:46:20 +0100 | [diff] [blame] | 252 | int i; |
| 253 | |
| 254 | /* Check for unwritable registers before we start */ |
| 255 | if (map->writeable_reg) |
| 256 | for (i = 0; i < val_len / map->format.val_bytes; i++) |
| 257 | if (!map->writeable_reg(map->dev, reg + i)) |
| 258 | return -EINVAL; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 259 | |
| 260 | map->format.format_reg(map->work_buf, reg); |
| 261 | |
Lars-Peter Clausen | 6f30644 | 2011-09-05 20:46:32 +0200 | [diff] [blame] | 262 | u8[0] |= map->write_flag_mask; |
| 263 | |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 264 | trace_regmap_hw_write_start(map->dev, reg, |
| 265 | val_len / map->format.val_bytes); |
| 266 | |
Mark Brown | 2547e20 | 2011-07-20 21:47:22 +0100 | [diff] [blame] | 267 | /* If we're doing a single register write we can probably just |
| 268 | * send the work_buf directly, otherwise try to do a gather |
| 269 | * write. |
| 270 | */ |
| 271 | if (val == map->work_buf + map->format.reg_bytes) |
| 272 | ret = map->bus->write(map->dev, map->work_buf, |
| 273 | map->format.reg_bytes + val_len); |
| 274 | else if (map->bus->gather_write) |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 275 | ret = map->bus->gather_write(map->dev, map->work_buf, |
| 276 | map->format.reg_bytes, |
| 277 | val, val_len); |
| 278 | |
Mark Brown | 2547e20 | 2011-07-20 21:47:22 +0100 | [diff] [blame] | 279 | /* If that didn't work fall back on linearising by hand. */ |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 280 | if (ret == -ENOTSUPP) { |
| 281 | len = map->format.reg_bytes + val_len; |
| 282 | buf = kmalloc(len, GFP_KERNEL); |
| 283 | if (!buf) |
| 284 | return -ENOMEM; |
| 285 | |
| 286 | memcpy(buf, map->work_buf, map->format.reg_bytes); |
| 287 | memcpy(buf + map->format.reg_bytes, val, val_len); |
| 288 | ret = map->bus->write(map->dev, buf, len); |
| 289 | |
| 290 | kfree(buf); |
| 291 | } |
| 292 | |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 293 | trace_regmap_hw_write_done(map->dev, reg, |
| 294 | val_len / map->format.val_bytes); |
| 295 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 296 | return ret; |
| 297 | } |
| 298 | |
Dimitris Papastamos | 4d2dc09 | 2011-09-29 10:39:07 +0100 | [diff] [blame] | 299 | int _regmap_write(struct regmap *map, unsigned int reg, |
| 300 | unsigned int val) |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 301 | { |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 302 | int ret; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 303 | BUG_ON(!map->format.format_write && !map->format.format_val); |
| 304 | |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 305 | if (!map->cache_bypass) { |
| 306 | ret = regcache_write(map, reg, val); |
| 307 | if (ret != 0) |
| 308 | return ret; |
Mark Brown | 8ae0d7e | 2011-10-26 10:34:22 +0200 | [diff] [blame^] | 309 | if (map->cache_only) { |
| 310 | map->cache_dirty = true; |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 311 | return 0; |
Mark Brown | 8ae0d7e | 2011-10-26 10:34:22 +0200 | [diff] [blame^] | 312 | } |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 313 | } |
| 314 | |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 315 | trace_regmap_reg_write(map->dev, reg, val); |
| 316 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 317 | if (map->format.format_write) { |
| 318 | map->format.format_write(map, reg, val); |
| 319 | |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 320 | trace_regmap_hw_write_start(map->dev, reg, 1); |
| 321 | |
| 322 | ret = map->bus->write(map->dev, map->work_buf, |
| 323 | map->format.buf_size); |
| 324 | |
| 325 | trace_regmap_hw_write_done(map->dev, reg, 1); |
| 326 | |
| 327 | return ret; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 328 | } else { |
| 329 | map->format.format_val(map->work_buf + map->format.reg_bytes, |
| 330 | val); |
| 331 | return _regmap_raw_write(map, reg, |
| 332 | map->work_buf + map->format.reg_bytes, |
| 333 | map->format.val_bytes); |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * regmap_write(): Write a value to a single register |
| 339 | * |
| 340 | * @map: Register map to write to |
| 341 | * @reg: Register to write to |
| 342 | * @val: Value to be written |
| 343 | * |
| 344 | * A value of zero will be returned on success, a negative errno will |
| 345 | * be returned in error cases. |
| 346 | */ |
| 347 | int regmap_write(struct regmap *map, unsigned int reg, unsigned int val) |
| 348 | { |
| 349 | int ret; |
| 350 | |
| 351 | mutex_lock(&map->lock); |
| 352 | |
| 353 | ret = _regmap_write(map, reg, val); |
| 354 | |
| 355 | mutex_unlock(&map->lock); |
| 356 | |
| 357 | return ret; |
| 358 | } |
| 359 | EXPORT_SYMBOL_GPL(regmap_write); |
| 360 | |
| 361 | /** |
| 362 | * regmap_raw_write(): Write raw values to one or more registers |
| 363 | * |
| 364 | * @map: Register map to write to |
| 365 | * @reg: Initial register to write to |
| 366 | * @val: Block of data to be written, laid out for direct transmission to the |
| 367 | * device |
| 368 | * @val_len: Length of data pointed to by val. |
| 369 | * |
| 370 | * This function is intended to be used for things like firmware |
| 371 | * download where a large block of data needs to be transferred to the |
| 372 | * device. No formatting will be done on the data provided. |
| 373 | * |
| 374 | * A value of zero will be returned on success, a negative errno will |
| 375 | * be returned in error cases. |
| 376 | */ |
| 377 | int regmap_raw_write(struct regmap *map, unsigned int reg, |
| 378 | const void *val, size_t val_len) |
| 379 | { |
| 380 | int ret; |
| 381 | |
Mark Brown | 04e016a | 2011-10-09 13:35:43 +0100 | [diff] [blame] | 382 | WARN_ON(map->cache_type != REGCACHE_NONE); |
| 383 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 384 | mutex_lock(&map->lock); |
| 385 | |
| 386 | ret = _regmap_raw_write(map, reg, val, val_len); |
| 387 | |
| 388 | mutex_unlock(&map->lock); |
| 389 | |
| 390 | return ret; |
| 391 | } |
| 392 | EXPORT_SYMBOL_GPL(regmap_raw_write); |
| 393 | |
| 394 | static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val, |
| 395 | unsigned int val_len) |
| 396 | { |
| 397 | u8 *u8 = map->work_buf; |
| 398 | int ret; |
| 399 | |
| 400 | map->format.format_reg(map->work_buf, reg); |
| 401 | |
| 402 | /* |
Lars-Peter Clausen | 6f30644 | 2011-09-05 20:46:32 +0200 | [diff] [blame] | 403 | * 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] | 404 | * register addresss; since it's always the high bits for all |
| 405 | * current formats we can do this here rather than in |
| 406 | * formatting. This may break if we get interesting formats. |
| 407 | */ |
Lars-Peter Clausen | 6f30644 | 2011-09-05 20:46:32 +0200 | [diff] [blame] | 408 | u8[0] |= map->read_flag_mask; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 409 | |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 410 | trace_regmap_hw_read_start(map->dev, reg, |
| 411 | val_len / map->format.val_bytes); |
| 412 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 413 | ret = map->bus->read(map->dev, map->work_buf, map->format.reg_bytes, |
Mark Brown | 40c5cc2 | 2011-07-24 22:39:12 +0100 | [diff] [blame] | 414 | val, val_len); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 415 | |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 416 | trace_regmap_hw_read_done(map->dev, reg, |
| 417 | val_len / map->format.val_bytes); |
| 418 | |
| 419 | return ret; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | static int _regmap_read(struct regmap *map, unsigned int reg, |
| 423 | unsigned int *val) |
| 424 | { |
| 425 | int ret; |
| 426 | |
| 427 | if (!map->format.parse_val) |
| 428 | return -EINVAL; |
| 429 | |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 430 | if (!map->cache_bypass) { |
| 431 | ret = regcache_read(map, reg, val); |
| 432 | if (ret == 0) |
| 433 | return 0; |
| 434 | } |
| 435 | |
| 436 | if (map->cache_only) |
| 437 | return -EBUSY; |
| 438 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 439 | 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] | 440 | if (ret == 0) { |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 441 | *val = map->format.parse_val(map->work_buf); |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 442 | trace_regmap_reg_read(map->dev, reg, *val); |
| 443 | } |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 444 | |
| 445 | return ret; |
| 446 | } |
| 447 | |
| 448 | /** |
| 449 | * regmap_read(): Read a value from a single register |
| 450 | * |
| 451 | * @map: Register map to write to |
| 452 | * @reg: Register to be read from |
| 453 | * @val: Pointer to store read value |
| 454 | * |
| 455 | * A value of zero will be returned on success, a negative errno will |
| 456 | * be returned in error cases. |
| 457 | */ |
| 458 | int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val) |
| 459 | { |
| 460 | int ret; |
| 461 | |
| 462 | mutex_lock(&map->lock); |
| 463 | |
| 464 | ret = _regmap_read(map, reg, val); |
| 465 | |
| 466 | mutex_unlock(&map->lock); |
| 467 | |
| 468 | return ret; |
| 469 | } |
| 470 | EXPORT_SYMBOL_GPL(regmap_read); |
| 471 | |
| 472 | /** |
| 473 | * regmap_raw_read(): Read raw data from the device |
| 474 | * |
| 475 | * @map: Register map to write to |
| 476 | * @reg: First register to be read from |
| 477 | * @val: Pointer to store read value |
| 478 | * @val_len: Size of data to read |
| 479 | * |
| 480 | * A value of zero will be returned on success, a negative errno will |
| 481 | * be returned in error cases. |
| 482 | */ |
| 483 | int regmap_raw_read(struct regmap *map, unsigned int reg, void *val, |
| 484 | size_t val_len) |
| 485 | { |
| 486 | int ret; |
Mark Brown | de2d808 | 2011-10-10 13:24:52 +0100 | [diff] [blame] | 487 | int i; |
| 488 | bool vol = true; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 489 | |
Mark Brown | de2d808 | 2011-10-10 13:24:52 +0100 | [diff] [blame] | 490 | for (i = 0; i < val_len / map->format.val_bytes; i++) |
| 491 | if (!regmap_volatile(map, reg + i)) |
| 492 | vol = false; |
| 493 | |
| 494 | WARN_ON(!vol && map->cache_type != REGCACHE_NONE); |
Mark Brown | 04e016a | 2011-10-09 13:35:43 +0100 | [diff] [blame] | 495 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 496 | mutex_lock(&map->lock); |
| 497 | |
| 498 | ret = _regmap_raw_read(map, reg, val, val_len); |
| 499 | |
| 500 | mutex_unlock(&map->lock); |
| 501 | |
| 502 | return ret; |
| 503 | } |
| 504 | EXPORT_SYMBOL_GPL(regmap_raw_read); |
| 505 | |
| 506 | /** |
| 507 | * regmap_bulk_read(): Read multiple registers from the device |
| 508 | * |
| 509 | * @map: Register map to write to |
| 510 | * @reg: First register to be read from |
| 511 | * @val: Pointer to store read value, in native register size for device |
| 512 | * @val_count: Number of registers to read |
| 513 | * |
| 514 | * A value of zero will be returned on success, a negative errno will |
| 515 | * be returned in error cases. |
| 516 | */ |
| 517 | int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val, |
| 518 | size_t val_count) |
| 519 | { |
| 520 | int ret, i; |
| 521 | size_t val_bytes = map->format.val_bytes; |
Mark Brown | de2d808 | 2011-10-10 13:24:52 +0100 | [diff] [blame] | 522 | bool vol = true; |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 523 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 524 | if (!map->format.parse_val) |
| 525 | return -EINVAL; |
| 526 | |
Mark Brown | de2d808 | 2011-10-10 13:24:52 +0100 | [diff] [blame] | 527 | /* Is this a block of volatile registers? */ |
| 528 | for (i = 0; i < val_count; i++) |
| 529 | if (!regmap_volatile(map, reg + i)) |
| 530 | vol = false; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 531 | |
Mark Brown | de2d808 | 2011-10-10 13:24:52 +0100 | [diff] [blame] | 532 | if (vol || map->cache_type == REGCACHE_NONE) { |
| 533 | ret = regmap_raw_read(map, reg, val, val_bytes * val_count); |
| 534 | if (ret != 0) |
| 535 | return ret; |
| 536 | |
| 537 | for (i = 0; i < val_count * val_bytes; i += val_bytes) |
| 538 | map->format.parse_val(val + i); |
| 539 | } else { |
| 540 | for (i = 0; i < val_count; i++) { |
| 541 | ret = regmap_read(map, reg + i, val + (i * val_bytes)); |
| 542 | if (ret != 0) |
| 543 | return ret; |
| 544 | } |
| 545 | } |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 546 | |
| 547 | return 0; |
| 548 | } |
| 549 | EXPORT_SYMBOL_GPL(regmap_bulk_read); |
| 550 | |
| 551 | /** |
| 552 | * remap_update_bits: Perform a read/modify/write cycle on the register map |
| 553 | * |
| 554 | * @map: Register map to update |
| 555 | * @reg: Register to update |
| 556 | * @mask: Bitmask to change |
| 557 | * @val: New value for bitmask |
| 558 | * |
| 559 | * Returns zero for success, a negative number on error. |
| 560 | */ |
| 561 | int regmap_update_bits(struct regmap *map, unsigned int reg, |
| 562 | unsigned int mask, unsigned int val) |
| 563 | { |
| 564 | int ret; |
| 565 | unsigned int tmp; |
| 566 | |
| 567 | mutex_lock(&map->lock); |
| 568 | |
| 569 | ret = _regmap_read(map, reg, &tmp); |
| 570 | if (ret != 0) |
| 571 | goto out; |
| 572 | |
| 573 | tmp &= ~mask; |
| 574 | tmp |= val & mask; |
| 575 | |
| 576 | ret = _regmap_write(map, reg, tmp); |
| 577 | |
| 578 | out: |
| 579 | mutex_unlock(&map->lock); |
| 580 | |
| 581 | return ret; |
| 582 | } |
| 583 | EXPORT_SYMBOL_GPL(regmap_update_bits); |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 584 | |
| 585 | static int __init regmap_initcall(void) |
| 586 | { |
| 587 | regmap_debugfs_initcall(); |
| 588 | |
| 589 | return 0; |
| 590 | } |
| 591 | postcore_initcall(regmap_initcall); |