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 | /** |
Mark Brown | bf31517 | 2011-12-03 17:06:20 +0000 | [diff] [blame] | 262 | * regmap_reinit_cache(): Reinitialise the current register cache |
| 263 | * |
| 264 | * @map: Register map to operate on. |
| 265 | * @config: New configuration. Only the cache data will be used. |
| 266 | * |
| 267 | * Discard any existing register cache for the map and initialize a |
| 268 | * new cache. This can be used to restore the cache to defaults or to |
| 269 | * update the cache configuration to reflect runtime discovery of the |
| 270 | * hardware. |
| 271 | */ |
| 272 | int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config) |
| 273 | { |
| 274 | int ret; |
| 275 | |
| 276 | mutex_lock(&map->lock); |
| 277 | |
| 278 | regcache_exit(map); |
| 279 | |
| 280 | map->max_register = config->max_register; |
| 281 | map->writeable_reg = config->writeable_reg; |
| 282 | map->readable_reg = config->readable_reg; |
| 283 | map->volatile_reg = config->volatile_reg; |
| 284 | map->precious_reg = config->precious_reg; |
| 285 | map->cache_type = config->cache_type; |
| 286 | |
Mark Brown | 421e8d2 | 2012-01-20 13:39:37 +0000 | [diff] [blame] | 287 | map->cache_bypass = false; |
| 288 | map->cache_only = false; |
| 289 | |
Mark Brown | bf31517 | 2011-12-03 17:06:20 +0000 | [diff] [blame] | 290 | ret = regcache_init(map, config); |
| 291 | |
| 292 | mutex_unlock(&map->lock); |
| 293 | |
| 294 | return ret; |
| 295 | } |
| 296 | |
| 297 | /** |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 298 | * regmap_exit(): Free a previously allocated register map |
| 299 | */ |
| 300 | void regmap_exit(struct regmap *map) |
| 301 | { |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 302 | regcache_exit(map); |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 303 | regmap_debugfs_exit(map); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 304 | kfree(map->work_buf); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 305 | kfree(map); |
| 306 | } |
| 307 | EXPORT_SYMBOL_GPL(regmap_exit); |
| 308 | |
| 309 | static int _regmap_raw_write(struct regmap *map, unsigned int reg, |
| 310 | const void *val, size_t val_len) |
| 311 | { |
Lars-Peter Clausen | 6f30644 | 2011-09-05 20:46:32 +0200 | [diff] [blame] | 312 | u8 *u8 = map->work_buf; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 313 | void *buf; |
| 314 | int ret = -ENOTSUPP; |
| 315 | size_t len; |
Mark Brown | 7330478 | 2011-07-24 11:46:20 +0100 | [diff] [blame] | 316 | int i; |
| 317 | |
| 318 | /* Check for unwritable registers before we start */ |
| 319 | if (map->writeable_reg) |
| 320 | for (i = 0; i < val_len / map->format.val_bytes; i++) |
| 321 | if (!map->writeable_reg(map->dev, reg + i)) |
| 322 | return -EINVAL; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 323 | |
Laxman Dewangan | c915719 | 2012-02-10 21:30:27 +0530 | [diff] [blame] | 324 | if (!map->cache_bypass && map->format.parse_val) { |
| 325 | unsigned int ival; |
| 326 | int val_bytes = map->format.val_bytes; |
| 327 | for (i = 0; i < val_len / val_bytes; i++) { |
| 328 | memcpy(map->work_buf, val + (i * val_bytes), val_bytes); |
| 329 | ival = map->format.parse_val(map->work_buf); |
| 330 | ret = regcache_write(map, reg + i, ival); |
| 331 | if (ret) { |
| 332 | dev_err(map->dev, |
| 333 | "Error in caching of register: %u ret: %d\n", |
| 334 | reg + i, ret); |
| 335 | return ret; |
| 336 | } |
| 337 | } |
| 338 | if (map->cache_only) { |
| 339 | map->cache_dirty = true; |
| 340 | return 0; |
| 341 | } |
| 342 | } |
| 343 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 344 | map->format.format_reg(map->work_buf, reg); |
| 345 | |
Lars-Peter Clausen | 6f30644 | 2011-09-05 20:46:32 +0200 | [diff] [blame] | 346 | u8[0] |= map->write_flag_mask; |
| 347 | |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 348 | trace_regmap_hw_write_start(map->dev, reg, |
| 349 | val_len / map->format.val_bytes); |
| 350 | |
Mark Brown | 2547e20 | 2011-07-20 21:47:22 +0100 | [diff] [blame] | 351 | /* If we're doing a single register write we can probably just |
| 352 | * send the work_buf directly, otherwise try to do a gather |
| 353 | * write. |
| 354 | */ |
| 355 | if (val == map->work_buf + map->format.reg_bytes) |
| 356 | ret = map->bus->write(map->dev, map->work_buf, |
| 357 | map->format.reg_bytes + val_len); |
| 358 | else if (map->bus->gather_write) |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 359 | ret = map->bus->gather_write(map->dev, map->work_buf, |
| 360 | map->format.reg_bytes, |
| 361 | val, val_len); |
| 362 | |
Mark Brown | 2547e20 | 2011-07-20 21:47:22 +0100 | [diff] [blame] | 363 | /* If that didn't work fall back on linearising by hand. */ |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 364 | if (ret == -ENOTSUPP) { |
| 365 | len = map->format.reg_bytes + val_len; |
| 366 | buf = kmalloc(len, GFP_KERNEL); |
| 367 | if (!buf) |
| 368 | return -ENOMEM; |
| 369 | |
| 370 | memcpy(buf, map->work_buf, map->format.reg_bytes); |
| 371 | memcpy(buf + map->format.reg_bytes, val, val_len); |
| 372 | ret = map->bus->write(map->dev, buf, len); |
| 373 | |
| 374 | kfree(buf); |
| 375 | } |
| 376 | |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 377 | trace_regmap_hw_write_done(map->dev, reg, |
| 378 | val_len / map->format.val_bytes); |
| 379 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 380 | return ret; |
| 381 | } |
| 382 | |
Dimitris Papastamos | 4d2dc09 | 2011-09-29 10:39:07 +0100 | [diff] [blame] | 383 | int _regmap_write(struct regmap *map, unsigned int reg, |
| 384 | unsigned int val) |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 385 | { |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 386 | int ret; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 387 | BUG_ON(!map->format.format_write && !map->format.format_val); |
| 388 | |
Laxman Dewangan | c915719 | 2012-02-10 21:30:27 +0530 | [diff] [blame] | 389 | if (!map->cache_bypass && map->format.format_write) { |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 390 | ret = regcache_write(map, reg, val); |
| 391 | if (ret != 0) |
| 392 | return ret; |
Mark Brown | 8ae0d7e | 2011-10-26 10:34:22 +0200 | [diff] [blame] | 393 | if (map->cache_only) { |
| 394 | map->cache_dirty = true; |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 395 | return 0; |
Mark Brown | 8ae0d7e | 2011-10-26 10:34:22 +0200 | [diff] [blame] | 396 | } |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 397 | } |
| 398 | |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 399 | trace_regmap_reg_write(map->dev, reg, val); |
| 400 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 401 | if (map->format.format_write) { |
| 402 | map->format.format_write(map, reg, val); |
| 403 | |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 404 | trace_regmap_hw_write_start(map->dev, reg, 1); |
| 405 | |
| 406 | ret = map->bus->write(map->dev, map->work_buf, |
| 407 | map->format.buf_size); |
| 408 | |
| 409 | trace_regmap_hw_write_done(map->dev, reg, 1); |
| 410 | |
| 411 | return ret; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 412 | } else { |
| 413 | map->format.format_val(map->work_buf + map->format.reg_bytes, |
| 414 | val); |
| 415 | return _regmap_raw_write(map, reg, |
| 416 | map->work_buf + map->format.reg_bytes, |
| 417 | map->format.val_bytes); |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | /** |
| 422 | * regmap_write(): Write a value to a single register |
| 423 | * |
| 424 | * @map: Register map to write to |
| 425 | * @reg: Register to write to |
| 426 | * @val: Value to be written |
| 427 | * |
| 428 | * A value of zero will be returned on success, a negative errno will |
| 429 | * be returned in error cases. |
| 430 | */ |
| 431 | int regmap_write(struct regmap *map, unsigned int reg, unsigned int val) |
| 432 | { |
| 433 | int ret; |
| 434 | |
| 435 | mutex_lock(&map->lock); |
| 436 | |
| 437 | ret = _regmap_write(map, reg, val); |
| 438 | |
| 439 | mutex_unlock(&map->lock); |
| 440 | |
| 441 | return ret; |
| 442 | } |
| 443 | EXPORT_SYMBOL_GPL(regmap_write); |
| 444 | |
| 445 | /** |
| 446 | * regmap_raw_write(): Write raw values to one or more registers |
| 447 | * |
| 448 | * @map: Register map to write to |
| 449 | * @reg: Initial register to write to |
| 450 | * @val: Block of data to be written, laid out for direct transmission to the |
| 451 | * device |
| 452 | * @val_len: Length of data pointed to by val. |
| 453 | * |
| 454 | * This function is intended to be used for things like firmware |
| 455 | * download where a large block of data needs to be transferred to the |
| 456 | * device. No formatting will be done on the data provided. |
| 457 | * |
| 458 | * A value of zero will be returned on success, a negative errno will |
| 459 | * be returned in error cases. |
| 460 | */ |
| 461 | int regmap_raw_write(struct regmap *map, unsigned int reg, |
| 462 | const void *val, size_t val_len) |
| 463 | { |
| 464 | int ret; |
| 465 | |
| 466 | mutex_lock(&map->lock); |
| 467 | |
| 468 | ret = _regmap_raw_write(map, reg, val, val_len); |
| 469 | |
| 470 | mutex_unlock(&map->lock); |
| 471 | |
| 472 | return ret; |
| 473 | } |
| 474 | EXPORT_SYMBOL_GPL(regmap_raw_write); |
| 475 | |
Laxman Dewangan | 8eaeb21 | 2012-02-12 19:49:43 +0530 | [diff] [blame^] | 476 | /* |
| 477 | * regmap_bulk_write(): Write multiple registers to the device |
| 478 | * |
| 479 | * @map: Register map to write to |
| 480 | * @reg: First register to be write from |
| 481 | * @val: Block of data to be written, in native register size for device |
| 482 | * @val_count: Number of registers to write |
| 483 | * |
| 484 | * This function is intended to be used for writing a large block of |
| 485 | * data to be device either in single transfer or multiple transfer. |
| 486 | * |
| 487 | * A value of zero will be returned on success, a negative errno will |
| 488 | * be returned in error cases. |
| 489 | */ |
| 490 | int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val, |
| 491 | size_t val_count) |
| 492 | { |
| 493 | int ret = 0, i; |
| 494 | size_t val_bytes = map->format.val_bytes; |
| 495 | void *wval; |
| 496 | |
| 497 | if (!map->format.parse_val) |
| 498 | return -EINVAL; |
| 499 | |
| 500 | mutex_lock(&map->lock); |
| 501 | |
| 502 | /* No formatting is require if val_byte is 1 */ |
| 503 | if (val_bytes == 1) { |
| 504 | wval = (void *)val; |
| 505 | } else { |
| 506 | wval = kmemdup(val, val_count * val_bytes, GFP_KERNEL); |
| 507 | if (!wval) { |
| 508 | ret = -ENOMEM; |
| 509 | dev_err(map->dev, "Error in memory allocation\n"); |
| 510 | goto out; |
| 511 | } |
| 512 | for (i = 0; i < val_count * val_bytes; i += val_bytes) |
| 513 | map->format.parse_val(wval + i); |
| 514 | } |
| 515 | ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count); |
| 516 | |
| 517 | if (val_bytes != 1) |
| 518 | kfree(wval); |
| 519 | |
| 520 | out: |
| 521 | mutex_unlock(&map->lock); |
| 522 | return ret; |
| 523 | } |
| 524 | EXPORT_SYMBOL_GPL(regmap_bulk_write); |
| 525 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 526 | static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val, |
| 527 | unsigned int val_len) |
| 528 | { |
| 529 | u8 *u8 = map->work_buf; |
| 530 | int ret; |
| 531 | |
| 532 | map->format.format_reg(map->work_buf, reg); |
| 533 | |
| 534 | /* |
Lars-Peter Clausen | 6f30644 | 2011-09-05 20:46:32 +0200 | [diff] [blame] | 535 | * 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] | 536 | * register addresss; since it's always the high bits for all |
| 537 | * current formats we can do this here rather than in |
| 538 | * formatting. This may break if we get interesting formats. |
| 539 | */ |
Lars-Peter Clausen | 6f30644 | 2011-09-05 20:46:32 +0200 | [diff] [blame] | 540 | u8[0] |= map->read_flag_mask; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 541 | |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 542 | trace_regmap_hw_read_start(map->dev, reg, |
| 543 | val_len / map->format.val_bytes); |
| 544 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 545 | 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] | 546 | val, val_len); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 547 | |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 548 | trace_regmap_hw_read_done(map->dev, reg, |
| 549 | val_len / map->format.val_bytes); |
| 550 | |
| 551 | return ret; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | static int _regmap_read(struct regmap *map, unsigned int reg, |
| 555 | unsigned int *val) |
| 556 | { |
| 557 | int ret; |
| 558 | |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 559 | if (!map->cache_bypass) { |
| 560 | ret = regcache_read(map, reg, val); |
| 561 | if (ret == 0) |
| 562 | return 0; |
| 563 | } |
| 564 | |
Lars-Peter Clausen | 1925441 | 2011-11-16 16:28:19 +0100 | [diff] [blame] | 565 | if (!map->format.parse_val) |
| 566 | return -EINVAL; |
| 567 | |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 568 | if (map->cache_only) |
| 569 | return -EBUSY; |
| 570 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 571 | 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] | 572 | if (ret == 0) { |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 573 | *val = map->format.parse_val(map->work_buf); |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 574 | trace_regmap_reg_read(map->dev, reg, *val); |
| 575 | } |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 576 | |
| 577 | return ret; |
| 578 | } |
| 579 | |
| 580 | /** |
| 581 | * regmap_read(): Read a value from a single register |
| 582 | * |
| 583 | * @map: Register map to write to |
| 584 | * @reg: Register to be read from |
| 585 | * @val: Pointer to store read value |
| 586 | * |
| 587 | * A value of zero will be returned on success, a negative errno will |
| 588 | * be returned in error cases. |
| 589 | */ |
| 590 | int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val) |
| 591 | { |
| 592 | int ret; |
| 593 | |
| 594 | mutex_lock(&map->lock); |
| 595 | |
| 596 | ret = _regmap_read(map, reg, val); |
| 597 | |
| 598 | mutex_unlock(&map->lock); |
| 599 | |
| 600 | return ret; |
| 601 | } |
| 602 | EXPORT_SYMBOL_GPL(regmap_read); |
| 603 | |
| 604 | /** |
| 605 | * regmap_raw_read(): Read raw data from the device |
| 606 | * |
| 607 | * @map: Register map to write to |
| 608 | * @reg: First register to be read from |
| 609 | * @val: Pointer to store read value |
| 610 | * @val_len: Size of data to read |
| 611 | * |
| 612 | * A value of zero will be returned on success, a negative errno will |
| 613 | * be returned in error cases. |
| 614 | */ |
| 615 | int regmap_raw_read(struct regmap *map, unsigned int reg, void *val, |
| 616 | size_t val_len) |
| 617 | { |
Lars-Peter Clausen | 82cd996 | 2011-11-08 18:37:25 +0100 | [diff] [blame] | 618 | size_t val_count = val_len / map->format.val_bytes; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 619 | int ret; |
| 620 | |
Lars-Peter Clausen | 82cd996 | 2011-11-08 18:37:25 +0100 | [diff] [blame] | 621 | WARN_ON(!regmap_volatile_range(map, reg, val_count) && |
| 622 | map->cache_type != REGCACHE_NONE); |
Mark Brown | 04e016a | 2011-10-09 13:35:43 +0100 | [diff] [blame] | 623 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 624 | mutex_lock(&map->lock); |
| 625 | |
| 626 | ret = _regmap_raw_read(map, reg, val, val_len); |
| 627 | |
| 628 | mutex_unlock(&map->lock); |
| 629 | |
| 630 | return ret; |
| 631 | } |
| 632 | EXPORT_SYMBOL_GPL(regmap_raw_read); |
| 633 | |
| 634 | /** |
| 635 | * regmap_bulk_read(): Read multiple registers from the device |
| 636 | * |
| 637 | * @map: Register map to write to |
| 638 | * @reg: First register to be read from |
| 639 | * @val: Pointer to store read value, in native register size for device |
| 640 | * @val_count: Number of registers to read |
| 641 | * |
| 642 | * A value of zero will be returned on success, a negative errno will |
| 643 | * be returned in error cases. |
| 644 | */ |
| 645 | int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val, |
| 646 | size_t val_count) |
| 647 | { |
| 648 | int ret, i; |
| 649 | size_t val_bytes = map->format.val_bytes; |
Lars-Peter Clausen | 82cd996 | 2011-11-08 18:37:25 +0100 | [diff] [blame] | 650 | bool vol = regmap_volatile_range(map, reg, val_count); |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 651 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 652 | if (!map->format.parse_val) |
| 653 | return -EINVAL; |
| 654 | |
Mark Brown | de2d808 | 2011-10-10 13:24:52 +0100 | [diff] [blame] | 655 | if (vol || map->cache_type == REGCACHE_NONE) { |
| 656 | ret = regmap_raw_read(map, reg, val, val_bytes * val_count); |
| 657 | if (ret != 0) |
| 658 | return ret; |
| 659 | |
| 660 | for (i = 0; i < val_count * val_bytes; i += val_bytes) |
| 661 | map->format.parse_val(val + i); |
| 662 | } else { |
| 663 | for (i = 0; i < val_count; i++) { |
| 664 | ret = regmap_read(map, reg + i, val + (i * val_bytes)); |
| 665 | if (ret != 0) |
| 666 | return ret; |
| 667 | } |
| 668 | } |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 669 | |
| 670 | return 0; |
| 671 | } |
| 672 | EXPORT_SYMBOL_GPL(regmap_bulk_read); |
| 673 | |
Mark Brown | 018690d | 2011-11-29 20:10:36 +0000 | [diff] [blame] | 674 | static int _regmap_update_bits(struct regmap *map, unsigned int reg, |
| 675 | unsigned int mask, unsigned int val, |
| 676 | bool *change) |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 677 | { |
| 678 | int ret; |
Mark Brown | d91e8db | 2011-11-18 16:03:50 +0000 | [diff] [blame] | 679 | unsigned int tmp, orig; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 680 | |
| 681 | mutex_lock(&map->lock); |
| 682 | |
Mark Brown | d91e8db | 2011-11-18 16:03:50 +0000 | [diff] [blame] | 683 | ret = _regmap_read(map, reg, &orig); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 684 | if (ret != 0) |
| 685 | goto out; |
| 686 | |
Mark Brown | d91e8db | 2011-11-18 16:03:50 +0000 | [diff] [blame] | 687 | tmp = orig & ~mask; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 688 | tmp |= val & mask; |
| 689 | |
Mark Brown | 018690d | 2011-11-29 20:10:36 +0000 | [diff] [blame] | 690 | if (tmp != orig) { |
Mark Brown | d91e8db | 2011-11-18 16:03:50 +0000 | [diff] [blame] | 691 | ret = _regmap_write(map, reg, tmp); |
Mark Brown | 018690d | 2011-11-29 20:10:36 +0000 | [diff] [blame] | 692 | *change = true; |
| 693 | } else { |
| 694 | *change = false; |
| 695 | } |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 696 | |
| 697 | out: |
| 698 | mutex_unlock(&map->lock); |
| 699 | |
| 700 | return ret; |
| 701 | } |
Mark Brown | 018690d | 2011-11-29 20:10:36 +0000 | [diff] [blame] | 702 | |
| 703 | /** |
| 704 | * regmap_update_bits: Perform a read/modify/write cycle on the register map |
| 705 | * |
| 706 | * @map: Register map to update |
| 707 | * @reg: Register to update |
| 708 | * @mask: Bitmask to change |
| 709 | * @val: New value for bitmask |
| 710 | * |
| 711 | * Returns zero for success, a negative number on error. |
| 712 | */ |
| 713 | int regmap_update_bits(struct regmap *map, unsigned int reg, |
| 714 | unsigned int mask, unsigned int val) |
| 715 | { |
| 716 | bool change; |
| 717 | return _regmap_update_bits(map, reg, mask, val, &change); |
| 718 | } |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 719 | EXPORT_SYMBOL_GPL(regmap_update_bits); |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 720 | |
Mark Brown | 018690d | 2011-11-29 20:10:36 +0000 | [diff] [blame] | 721 | /** |
| 722 | * regmap_update_bits_check: Perform a read/modify/write cycle on the |
| 723 | * register map and report if updated |
| 724 | * |
| 725 | * @map: Register map to update |
| 726 | * @reg: Register to update |
| 727 | * @mask: Bitmask to change |
| 728 | * @val: New value for bitmask |
| 729 | * @change: Boolean indicating if a write was done |
| 730 | * |
| 731 | * Returns zero for success, a negative number on error. |
| 732 | */ |
| 733 | int regmap_update_bits_check(struct regmap *map, unsigned int reg, |
| 734 | unsigned int mask, unsigned int val, |
| 735 | bool *change) |
| 736 | { |
| 737 | return _regmap_update_bits(map, reg, mask, val, change); |
| 738 | } |
| 739 | EXPORT_SYMBOL_GPL(regmap_update_bits_check); |
| 740 | |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 741 | static int __init regmap_initcall(void) |
| 742 | { |
| 743 | regmap_debugfs_initcall(); |
| 744 | |
| 745 | return 0; |
| 746 | } |
| 747 | postcore_initcall(regmap_initcall); |