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