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