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