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