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 | |
Stephen Warren | f5d6eba | 2012-03-09 13:17:28 -0700 | [diff] [blame] | 13 | #include <linux/device.h> |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 14 | #include <linux/slab.h> |
Paul Gortmaker | 19694b5 | 2012-02-28 19:28:02 -0500 | [diff] [blame] | 15 | #include <linux/export.h> |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 16 | #include <linux/mutex.h> |
| 17 | #include <linux/err.h> |
| 18 | |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 19 | #define CREATE_TRACE_POINTS |
| 20 | #include <trace/events/regmap.h> |
| 21 | |
Mark Brown | 93de912 | 2011-07-20 22:35:37 +0100 | [diff] [blame] | 22 | #include "internal.h" |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 23 | |
Mark Brown | 8de2f08 | 2011-08-10 17:14:41 +0900 | [diff] [blame] | 24 | bool regmap_writeable(struct regmap *map, unsigned int reg) |
| 25 | { |
| 26 | if (map->max_register && reg > map->max_register) |
| 27 | return false; |
| 28 | |
| 29 | if (map->writeable_reg) |
| 30 | return map->writeable_reg(map->dev, reg); |
| 31 | |
| 32 | return true; |
| 33 | } |
| 34 | |
| 35 | bool regmap_readable(struct regmap *map, unsigned int reg) |
| 36 | { |
| 37 | if (map->max_register && reg > map->max_register) |
| 38 | return false; |
| 39 | |
Wolfram Sang | 4191f19 | 2012-01-30 15:08:16 +0100 | [diff] [blame] | 40 | if (map->format.format_write) |
| 41 | return false; |
| 42 | |
Mark Brown | 8de2f08 | 2011-08-10 17:14:41 +0900 | [diff] [blame] | 43 | if (map->readable_reg) |
| 44 | return map->readable_reg(map->dev, reg); |
| 45 | |
| 46 | return true; |
| 47 | } |
| 48 | |
| 49 | bool regmap_volatile(struct regmap *map, unsigned int reg) |
| 50 | { |
Wolfram Sang | 4191f19 | 2012-01-30 15:08:16 +0100 | [diff] [blame] | 51 | if (!regmap_readable(map, reg)) |
Mark Brown | 8de2f08 | 2011-08-10 17:14:41 +0900 | [diff] [blame] | 52 | return false; |
| 53 | |
| 54 | if (map->volatile_reg) |
| 55 | return map->volatile_reg(map->dev, reg); |
| 56 | |
| 57 | return true; |
| 58 | } |
| 59 | |
| 60 | bool regmap_precious(struct regmap *map, unsigned int reg) |
| 61 | { |
Wolfram Sang | 4191f19 | 2012-01-30 15:08:16 +0100 | [diff] [blame] | 62 | if (!regmap_readable(map, reg)) |
Mark Brown | 8de2f08 | 2011-08-10 17:14:41 +0900 | [diff] [blame] | 63 | return false; |
| 64 | |
| 65 | if (map->precious_reg) |
| 66 | return map->precious_reg(map->dev, reg); |
| 67 | |
| 68 | return false; |
| 69 | } |
| 70 | |
Lars-Peter Clausen | 82cd996 | 2011-11-08 18:37:25 +0100 | [diff] [blame] | 71 | static bool regmap_volatile_range(struct regmap *map, unsigned int reg, |
| 72 | unsigned int num) |
| 73 | { |
| 74 | unsigned int i; |
| 75 | |
| 76 | for (i = 0; i < num; i++) |
| 77 | if (!regmap_volatile(map, reg + i)) |
| 78 | return false; |
| 79 | |
| 80 | return true; |
| 81 | } |
| 82 | |
Wolfram Sang | 9aa5075 | 2012-01-27 16:10:22 +0100 | [diff] [blame] | 83 | static void regmap_format_2_6_write(struct regmap *map, |
| 84 | unsigned int reg, unsigned int val) |
| 85 | { |
| 86 | u8 *out = map->work_buf; |
| 87 | |
| 88 | *out = (reg << 6) | val; |
| 89 | } |
| 90 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 91 | static void regmap_format_4_12_write(struct regmap *map, |
| 92 | unsigned int reg, unsigned int val) |
| 93 | { |
| 94 | __be16 *out = map->work_buf; |
| 95 | *out = cpu_to_be16((reg << 12) | val); |
| 96 | } |
| 97 | |
| 98 | static void regmap_format_7_9_write(struct regmap *map, |
| 99 | unsigned int reg, unsigned int val) |
| 100 | { |
| 101 | __be16 *out = map->work_buf; |
| 102 | *out = cpu_to_be16((reg << 9) | val); |
| 103 | } |
| 104 | |
Lars-Peter Clausen | 7e5ec63 | 2011-11-16 16:28:21 +0100 | [diff] [blame] | 105 | static void regmap_format_10_14_write(struct regmap *map, |
| 106 | unsigned int reg, unsigned int val) |
| 107 | { |
| 108 | u8 *out = map->work_buf; |
| 109 | |
| 110 | out[2] = val; |
| 111 | out[1] = (val >> 8) | (reg << 6); |
| 112 | out[0] = reg >> 2; |
| 113 | } |
| 114 | |
Marc Reilly | d939fb9 | 2012-03-16 12:11:43 +1100 | [diff] [blame] | 115 | static void regmap_format_8(void *buf, unsigned int val, unsigned int shift) |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 116 | { |
| 117 | u8 *b = buf; |
| 118 | |
Marc Reilly | d939fb9 | 2012-03-16 12:11:43 +1100 | [diff] [blame] | 119 | b[0] = val << shift; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 120 | } |
| 121 | |
Marc Reilly | d939fb9 | 2012-03-16 12:11:43 +1100 | [diff] [blame] | 122 | static void regmap_format_16(void *buf, unsigned int val, unsigned int shift) |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 123 | { |
| 124 | __be16 *b = buf; |
| 125 | |
Marc Reilly | d939fb9 | 2012-03-16 12:11:43 +1100 | [diff] [blame] | 126 | b[0] = cpu_to_be16(val << shift); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 127 | } |
| 128 | |
Marc Reilly | d939fb9 | 2012-03-16 12:11:43 +1100 | [diff] [blame] | 129 | static void regmap_format_24(void *buf, unsigned int val, unsigned int shift) |
Marc Reilly | ea279fc | 2012-03-16 12:11:42 +1100 | [diff] [blame] | 130 | { |
| 131 | u8 *b = buf; |
| 132 | |
Marc Reilly | d939fb9 | 2012-03-16 12:11:43 +1100 | [diff] [blame] | 133 | val <<= shift; |
| 134 | |
Marc Reilly | ea279fc | 2012-03-16 12:11:42 +1100 | [diff] [blame] | 135 | b[0] = val >> 16; |
| 136 | b[1] = val >> 8; |
| 137 | b[2] = val; |
| 138 | } |
| 139 | |
Marc Reilly | d939fb9 | 2012-03-16 12:11:43 +1100 | [diff] [blame] | 140 | static void regmap_format_32(void *buf, unsigned int val, unsigned int shift) |
Mark Brown | 7d5e525 | 2012-02-17 15:58:25 -0800 | [diff] [blame] | 141 | { |
| 142 | __be32 *b = buf; |
| 143 | |
Marc Reilly | d939fb9 | 2012-03-16 12:11:43 +1100 | [diff] [blame] | 144 | b[0] = cpu_to_be32(val << shift); |
Mark Brown | 7d5e525 | 2012-02-17 15:58:25 -0800 | [diff] [blame] | 145 | } |
| 146 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 147 | static unsigned int regmap_parse_8(void *buf) |
| 148 | { |
| 149 | u8 *b = buf; |
| 150 | |
| 151 | return b[0]; |
| 152 | } |
| 153 | |
| 154 | static unsigned int regmap_parse_16(void *buf) |
| 155 | { |
| 156 | __be16 *b = buf; |
| 157 | |
| 158 | b[0] = be16_to_cpu(b[0]); |
| 159 | |
| 160 | return b[0]; |
| 161 | } |
| 162 | |
Marc Reilly | ea279fc | 2012-03-16 12:11:42 +1100 | [diff] [blame] | 163 | static unsigned int regmap_parse_24(void *buf) |
| 164 | { |
| 165 | u8 *b = buf; |
| 166 | unsigned int ret = b[2]; |
| 167 | ret |= ((unsigned int)b[1]) << 8; |
| 168 | ret |= ((unsigned int)b[0]) << 16; |
| 169 | |
| 170 | return ret; |
| 171 | } |
| 172 | |
Mark Brown | 7d5e525 | 2012-02-17 15:58:25 -0800 | [diff] [blame] | 173 | static unsigned int regmap_parse_32(void *buf) |
| 174 | { |
| 175 | __be32 *b = buf; |
| 176 | |
| 177 | b[0] = be32_to_cpu(b[0]); |
| 178 | |
| 179 | return b[0]; |
| 180 | } |
| 181 | |
Stephen Warren | bacdbe0 | 2012-04-04 15:48:28 -0600 | [diff] [blame] | 182 | static void regmap_lock_mutex(struct regmap *map) |
| 183 | { |
| 184 | mutex_lock(&map->mutex); |
| 185 | } |
| 186 | |
| 187 | static void regmap_unlock_mutex(struct regmap *map) |
| 188 | { |
| 189 | mutex_unlock(&map->mutex); |
| 190 | } |
| 191 | |
| 192 | static void regmap_lock_spinlock(struct regmap *map) |
| 193 | { |
| 194 | spin_lock(&map->spinlock); |
| 195 | } |
| 196 | |
| 197 | static void regmap_unlock_spinlock(struct regmap *map) |
| 198 | { |
| 199 | spin_unlock(&map->spinlock); |
| 200 | } |
| 201 | |
Mark Brown | 72b39f6 | 2012-05-08 17:44:40 +0100 | [diff] [blame] | 202 | static void dev_get_regmap_release(struct device *dev, void *res) |
| 203 | { |
| 204 | /* |
| 205 | * We don't actually have anything to do here; the goal here |
| 206 | * is not to manage the regmap but to provide a simple way to |
| 207 | * get the regmap back given a struct device. |
| 208 | */ |
| 209 | } |
| 210 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 211 | /** |
| 212 | * regmap_init(): Initialise register map |
| 213 | * |
| 214 | * @dev: Device that will be interacted with |
| 215 | * @bus: Bus-specific callbacks to use with device |
Stephen Warren | 0135bbc | 2012-04-04 15:48:30 -0600 | [diff] [blame] | 216 | * @bus_context: Data passed to bus-specific callbacks |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 217 | * @config: Configuration for register map |
| 218 | * |
| 219 | * The return value will be an ERR_PTR() on error or a valid pointer to |
| 220 | * a struct regmap. This function should generally not be called |
| 221 | * directly, it should be called by bus-specific init functions. |
| 222 | */ |
| 223 | struct regmap *regmap_init(struct device *dev, |
| 224 | const struct regmap_bus *bus, |
Stephen Warren | 0135bbc | 2012-04-04 15:48:30 -0600 | [diff] [blame] | 225 | void *bus_context, |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 226 | const struct regmap_config *config) |
| 227 | { |
Mark Brown | 72b39f6 | 2012-05-08 17:44:40 +0100 | [diff] [blame] | 228 | struct regmap *map, **m; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 229 | int ret = -EINVAL; |
| 230 | |
| 231 | if (!bus || !config) |
Lars-Peter Clausen | abbb18f | 2011-11-14 10:40:15 +0100 | [diff] [blame] | 232 | goto err; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 233 | |
| 234 | map = kzalloc(sizeof(*map), GFP_KERNEL); |
| 235 | if (map == NULL) { |
| 236 | ret = -ENOMEM; |
| 237 | goto err; |
| 238 | } |
| 239 | |
Stephen Warren | bacdbe0 | 2012-04-04 15:48:28 -0600 | [diff] [blame] | 240 | if (bus->fast_io) { |
| 241 | spin_lock_init(&map->spinlock); |
| 242 | map->lock = regmap_lock_spinlock; |
| 243 | map->unlock = regmap_unlock_spinlock; |
| 244 | } else { |
| 245 | mutex_init(&map->mutex); |
| 246 | map->lock = regmap_lock_mutex; |
| 247 | map->unlock = regmap_unlock_mutex; |
| 248 | } |
Wolfram Sang | c212acc | 2012-01-28 02:16:41 +0100 | [diff] [blame] | 249 | map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8); |
Mark Brown | 82159ba | 2012-01-18 10:52:25 +0000 | [diff] [blame] | 250 | map->format.pad_bytes = config->pad_bits / 8; |
Wolfram Sang | c212acc | 2012-01-28 02:16:41 +0100 | [diff] [blame] | 251 | map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8); |
Fabio Estevam | 5494a98 | 2012-05-31 21:10:30 -0300 | [diff] [blame] | 252 | map->format.buf_size = DIV_ROUND_UP(config->reg_bits + |
| 253 | config->val_bits + config->pad_bits, 8); |
Marc Reilly | d939fb9 | 2012-03-16 12:11:43 +1100 | [diff] [blame] | 254 | map->reg_shift = config->pad_bits % 8; |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 255 | if (config->reg_stride) |
| 256 | map->reg_stride = config->reg_stride; |
| 257 | else |
| 258 | map->reg_stride = 1; |
Ashish Jangam | 2e33caf | 2012-04-30 23:23:40 +0100 | [diff] [blame] | 259 | map->use_single_rw = config->use_single_rw; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 260 | map->dev = dev; |
| 261 | map->bus = bus; |
Stephen Warren | 0135bbc | 2012-04-04 15:48:30 -0600 | [diff] [blame] | 262 | map->bus_context = bus_context; |
Mark Brown | 2e2ae66 | 2011-07-20 22:33:39 +0100 | [diff] [blame] | 263 | map->max_register = config->max_register; |
| 264 | map->writeable_reg = config->writeable_reg; |
| 265 | map->readable_reg = config->readable_reg; |
| 266 | map->volatile_reg = config->volatile_reg; |
Mark Brown | 2efe164 | 2011-08-08 15:41:46 +0900 | [diff] [blame] | 267 | map->precious_reg = config->precious_reg; |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 268 | map->cache_type = config->cache_type; |
Mark Brown | 72b39f6 | 2012-05-08 17:44:40 +0100 | [diff] [blame] | 269 | map->name = config->name; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 270 | |
Lars-Peter Clausen | 6f30644 | 2011-09-05 20:46:32 +0200 | [diff] [blame] | 271 | if (config->read_flag_mask || config->write_flag_mask) { |
| 272 | map->read_flag_mask = config->read_flag_mask; |
| 273 | map->write_flag_mask = config->write_flag_mask; |
| 274 | } else { |
| 275 | map->read_flag_mask = bus->read_flag_mask; |
| 276 | } |
| 277 | |
Marc Reilly | d939fb9 | 2012-03-16 12:11:43 +1100 | [diff] [blame] | 278 | switch (config->reg_bits + map->reg_shift) { |
Wolfram Sang | 9aa5075 | 2012-01-27 16:10:22 +0100 | [diff] [blame] | 279 | case 2: |
| 280 | switch (config->val_bits) { |
| 281 | case 6: |
| 282 | map->format.format_write = regmap_format_2_6_write; |
| 283 | break; |
| 284 | default: |
| 285 | goto err_map; |
| 286 | } |
| 287 | break; |
| 288 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 289 | case 4: |
| 290 | switch (config->val_bits) { |
| 291 | case 12: |
| 292 | map->format.format_write = regmap_format_4_12_write; |
| 293 | break; |
| 294 | default: |
| 295 | goto err_map; |
| 296 | } |
| 297 | break; |
| 298 | |
| 299 | case 7: |
| 300 | switch (config->val_bits) { |
| 301 | case 9: |
| 302 | map->format.format_write = regmap_format_7_9_write; |
| 303 | break; |
| 304 | default: |
| 305 | goto err_map; |
| 306 | } |
| 307 | break; |
| 308 | |
Lars-Peter Clausen | 7e5ec63 | 2011-11-16 16:28:21 +0100 | [diff] [blame] | 309 | case 10: |
| 310 | switch (config->val_bits) { |
| 311 | case 14: |
| 312 | map->format.format_write = regmap_format_10_14_write; |
| 313 | break; |
| 314 | default: |
| 315 | goto err_map; |
| 316 | } |
| 317 | break; |
| 318 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 319 | case 8: |
| 320 | map->format.format_reg = regmap_format_8; |
| 321 | break; |
| 322 | |
| 323 | case 16: |
| 324 | map->format.format_reg = regmap_format_16; |
| 325 | break; |
| 326 | |
Mark Brown | 7d5e525 | 2012-02-17 15:58:25 -0800 | [diff] [blame] | 327 | case 32: |
| 328 | map->format.format_reg = regmap_format_32; |
| 329 | break; |
| 330 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 331 | default: |
| 332 | goto err_map; |
| 333 | } |
| 334 | |
| 335 | switch (config->val_bits) { |
| 336 | case 8: |
| 337 | map->format.format_val = regmap_format_8; |
| 338 | map->format.parse_val = regmap_parse_8; |
| 339 | break; |
| 340 | case 16: |
| 341 | map->format.format_val = regmap_format_16; |
| 342 | map->format.parse_val = regmap_parse_16; |
| 343 | break; |
Marc Reilly | ea279fc | 2012-03-16 12:11:42 +1100 | [diff] [blame] | 344 | case 24: |
| 345 | map->format.format_val = regmap_format_24; |
| 346 | map->format.parse_val = regmap_parse_24; |
| 347 | break; |
Mark Brown | 7d5e525 | 2012-02-17 15:58:25 -0800 | [diff] [blame] | 348 | case 32: |
| 349 | map->format.format_val = regmap_format_32; |
| 350 | map->format.parse_val = regmap_parse_32; |
| 351 | break; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 352 | } |
| 353 | |
Mark Brown | 7a64761 | 2012-04-30 23:26:32 +0100 | [diff] [blame] | 354 | if (map->format.format_write) |
| 355 | map->use_single_rw = true; |
| 356 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 357 | if (!map->format.format_write && |
| 358 | !(map->format.format_reg && map->format.format_val)) |
| 359 | goto err_map; |
| 360 | |
Mark Brown | 82159ba | 2012-01-18 10:52:25 +0000 | [diff] [blame] | 361 | map->work_buf = kzalloc(map->format.buf_size, GFP_KERNEL); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 362 | if (map->work_buf == NULL) { |
| 363 | ret = -ENOMEM; |
Mark Brown | 5204f5e | 2011-09-05 08:07:47 -0700 | [diff] [blame] | 364 | goto err_map; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 365 | } |
| 366 | |
Stephen Warren | d3c242e | 2012-04-04 15:48:29 -0600 | [diff] [blame] | 367 | regmap_debugfs_init(map, config->name); |
Mark Brown | 052d2cd | 2011-11-21 19:05:13 +0000 | [diff] [blame] | 368 | |
Lars-Peter Clausen | e5e3b8a | 2011-11-16 16:28:16 +0100 | [diff] [blame] | 369 | ret = regcache_init(map, config); |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 370 | if (ret < 0) |
Stephen Warren | bfaa25f | 2012-05-23 16:30:53 -0600 | [diff] [blame] | 371 | goto err_debugfs; |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 372 | |
Mark Brown | 72b39f6 | 2012-05-08 17:44:40 +0100 | [diff] [blame] | 373 | /* Add a devres resource for dev_get_regmap() */ |
| 374 | m = devres_alloc(dev_get_regmap_release, sizeof(*m), GFP_KERNEL); |
| 375 | if (!m) { |
| 376 | ret = -ENOMEM; |
| 377 | goto err_cache; |
| 378 | } |
| 379 | *m = map; |
| 380 | devres_add(dev, m); |
| 381 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 382 | return map; |
| 383 | |
Mark Brown | 72b39f6 | 2012-05-08 17:44:40 +0100 | [diff] [blame] | 384 | err_cache: |
| 385 | regcache_exit(map); |
Stephen Warren | bfaa25f | 2012-05-23 16:30:53 -0600 | [diff] [blame] | 386 | err_debugfs: |
| 387 | regmap_debugfs_exit(map); |
Lars-Peter Clausen | 58072cb | 2011-11-10 18:15:15 +0100 | [diff] [blame] | 388 | kfree(map->work_buf); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 389 | err_map: |
| 390 | kfree(map); |
| 391 | err: |
| 392 | return ERR_PTR(ret); |
| 393 | } |
| 394 | EXPORT_SYMBOL_GPL(regmap_init); |
| 395 | |
Mark Brown | c0eb467 | 2012-01-30 19:56:52 +0000 | [diff] [blame] | 396 | static void devm_regmap_release(struct device *dev, void *res) |
| 397 | { |
| 398 | regmap_exit(*(struct regmap **)res); |
| 399 | } |
| 400 | |
| 401 | /** |
| 402 | * devm_regmap_init(): Initialise managed register map |
| 403 | * |
| 404 | * @dev: Device that will be interacted with |
| 405 | * @bus: Bus-specific callbacks to use with device |
Stephen Warren | 0135bbc | 2012-04-04 15:48:30 -0600 | [diff] [blame] | 406 | * @bus_context: Data passed to bus-specific callbacks |
Mark Brown | c0eb467 | 2012-01-30 19:56:52 +0000 | [diff] [blame] | 407 | * @config: Configuration for register map |
| 408 | * |
| 409 | * The return value will be an ERR_PTR() on error or a valid pointer |
| 410 | * to a struct regmap. This function should generally not be called |
| 411 | * directly, it should be called by bus-specific init functions. The |
| 412 | * map will be automatically freed by the device management code. |
| 413 | */ |
| 414 | struct regmap *devm_regmap_init(struct device *dev, |
| 415 | const struct regmap_bus *bus, |
Stephen Warren | 0135bbc | 2012-04-04 15:48:30 -0600 | [diff] [blame] | 416 | void *bus_context, |
Mark Brown | c0eb467 | 2012-01-30 19:56:52 +0000 | [diff] [blame] | 417 | const struct regmap_config *config) |
| 418 | { |
| 419 | struct regmap **ptr, *regmap; |
| 420 | |
| 421 | ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL); |
| 422 | if (!ptr) |
| 423 | return ERR_PTR(-ENOMEM); |
| 424 | |
Stephen Warren | 0135bbc | 2012-04-04 15:48:30 -0600 | [diff] [blame] | 425 | regmap = regmap_init(dev, bus, bus_context, config); |
Mark Brown | c0eb467 | 2012-01-30 19:56:52 +0000 | [diff] [blame] | 426 | if (!IS_ERR(regmap)) { |
| 427 | *ptr = regmap; |
| 428 | devres_add(dev, ptr); |
| 429 | } else { |
| 430 | devres_free(ptr); |
| 431 | } |
| 432 | |
| 433 | return regmap; |
| 434 | } |
| 435 | EXPORT_SYMBOL_GPL(devm_regmap_init); |
| 436 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 437 | /** |
Mark Brown | bf31517 | 2011-12-03 17:06:20 +0000 | [diff] [blame] | 438 | * regmap_reinit_cache(): Reinitialise the current register cache |
| 439 | * |
| 440 | * @map: Register map to operate on. |
| 441 | * @config: New configuration. Only the cache data will be used. |
| 442 | * |
| 443 | * Discard any existing register cache for the map and initialize a |
| 444 | * new cache. This can be used to restore the cache to defaults or to |
| 445 | * update the cache configuration to reflect runtime discovery of the |
| 446 | * hardware. |
| 447 | */ |
| 448 | int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config) |
| 449 | { |
| 450 | int ret; |
| 451 | |
Stephen Warren | bacdbe0 | 2012-04-04 15:48:28 -0600 | [diff] [blame] | 452 | map->lock(map); |
Mark Brown | bf31517 | 2011-12-03 17:06:20 +0000 | [diff] [blame] | 453 | |
| 454 | regcache_exit(map); |
Mark Brown | a24f64a | 2012-01-26 18:30:16 +0000 | [diff] [blame] | 455 | regmap_debugfs_exit(map); |
Mark Brown | bf31517 | 2011-12-03 17:06:20 +0000 | [diff] [blame] | 456 | |
| 457 | map->max_register = config->max_register; |
| 458 | map->writeable_reg = config->writeable_reg; |
| 459 | map->readable_reg = config->readable_reg; |
| 460 | map->volatile_reg = config->volatile_reg; |
| 461 | map->precious_reg = config->precious_reg; |
| 462 | map->cache_type = config->cache_type; |
| 463 | |
Stephen Warren | d3c242e | 2012-04-04 15:48:29 -0600 | [diff] [blame] | 464 | regmap_debugfs_init(map, config->name); |
Mark Brown | a24f64a | 2012-01-26 18:30:16 +0000 | [diff] [blame] | 465 | |
Mark Brown | 421e8d2 | 2012-01-20 13:39:37 +0000 | [diff] [blame] | 466 | map->cache_bypass = false; |
| 467 | map->cache_only = false; |
| 468 | |
Mark Brown | bf31517 | 2011-12-03 17:06:20 +0000 | [diff] [blame] | 469 | ret = regcache_init(map, config); |
| 470 | |
Stephen Warren | bacdbe0 | 2012-04-04 15:48:28 -0600 | [diff] [blame] | 471 | map->unlock(map); |
Mark Brown | bf31517 | 2011-12-03 17:06:20 +0000 | [diff] [blame] | 472 | |
| 473 | return ret; |
| 474 | } |
Mark Brown | 752a6a5 | 2012-05-14 10:00:12 +0100 | [diff] [blame] | 475 | EXPORT_SYMBOL_GPL(regmap_reinit_cache); |
Mark Brown | bf31517 | 2011-12-03 17:06:20 +0000 | [diff] [blame] | 476 | |
| 477 | /** |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 478 | * regmap_exit(): Free a previously allocated register map |
| 479 | */ |
| 480 | void regmap_exit(struct regmap *map) |
| 481 | { |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 482 | regcache_exit(map); |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 483 | regmap_debugfs_exit(map); |
Stephen Warren | 0135bbc | 2012-04-04 15:48:30 -0600 | [diff] [blame] | 484 | if (map->bus->free_context) |
| 485 | map->bus->free_context(map->bus_context); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 486 | kfree(map->work_buf); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 487 | kfree(map); |
| 488 | } |
| 489 | EXPORT_SYMBOL_GPL(regmap_exit); |
| 490 | |
Mark Brown | 72b39f6 | 2012-05-08 17:44:40 +0100 | [diff] [blame] | 491 | static int dev_get_regmap_match(struct device *dev, void *res, void *data) |
| 492 | { |
| 493 | struct regmap **r = res; |
| 494 | if (!r || !*r) { |
| 495 | WARN_ON(!r || !*r); |
| 496 | return 0; |
| 497 | } |
| 498 | |
| 499 | /* If the user didn't specify a name match any */ |
| 500 | if (data) |
| 501 | return (*r)->name == data; |
| 502 | else |
| 503 | return 1; |
| 504 | } |
| 505 | |
| 506 | /** |
| 507 | * dev_get_regmap(): Obtain the regmap (if any) for a device |
| 508 | * |
| 509 | * @dev: Device to retrieve the map for |
| 510 | * @name: Optional name for the register map, usually NULL. |
| 511 | * |
| 512 | * Returns the regmap for the device if one is present, or NULL. If |
| 513 | * name is specified then it must match the name specified when |
| 514 | * registering the device, if it is NULL then the first regmap found |
| 515 | * will be used. Devices with multiple register maps are very rare, |
| 516 | * generic code should normally not need to specify a name. |
| 517 | */ |
| 518 | struct regmap *dev_get_regmap(struct device *dev, const char *name) |
| 519 | { |
| 520 | struct regmap **r = devres_find(dev, dev_get_regmap_release, |
| 521 | dev_get_regmap_match, (void *)name); |
| 522 | |
| 523 | if (!r) |
| 524 | return NULL; |
| 525 | return *r; |
| 526 | } |
| 527 | EXPORT_SYMBOL_GPL(dev_get_regmap); |
| 528 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 529 | static int _regmap_raw_write(struct regmap *map, unsigned int reg, |
| 530 | const void *val, size_t val_len) |
| 531 | { |
Lars-Peter Clausen | 6f30644 | 2011-09-05 20:46:32 +0200 | [diff] [blame] | 532 | u8 *u8 = map->work_buf; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 533 | void *buf; |
| 534 | int ret = -ENOTSUPP; |
| 535 | size_t len; |
Mark Brown | 7330478 | 2011-07-24 11:46:20 +0100 | [diff] [blame] | 536 | int i; |
| 537 | |
| 538 | /* Check for unwritable registers before we start */ |
| 539 | if (map->writeable_reg) |
| 540 | for (i = 0; i < val_len / map->format.val_bytes; i++) |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 541 | if (!map->writeable_reg(map->dev, |
| 542 | reg + (i * map->reg_stride))) |
Mark Brown | 7330478 | 2011-07-24 11:46:20 +0100 | [diff] [blame] | 543 | return -EINVAL; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 544 | |
Laxman Dewangan | c915719 | 2012-02-10 21:30:27 +0530 | [diff] [blame] | 545 | if (!map->cache_bypass && map->format.parse_val) { |
| 546 | unsigned int ival; |
| 547 | int val_bytes = map->format.val_bytes; |
| 548 | for (i = 0; i < val_len / val_bytes; i++) { |
| 549 | memcpy(map->work_buf, val + (i * val_bytes), val_bytes); |
| 550 | ival = map->format.parse_val(map->work_buf); |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 551 | ret = regcache_write(map, reg + (i * map->reg_stride), |
| 552 | ival); |
Laxman Dewangan | c915719 | 2012-02-10 21:30:27 +0530 | [diff] [blame] | 553 | if (ret) { |
| 554 | dev_err(map->dev, |
| 555 | "Error in caching of register: %u ret: %d\n", |
| 556 | reg + i, ret); |
| 557 | return ret; |
| 558 | } |
| 559 | } |
| 560 | if (map->cache_only) { |
| 561 | map->cache_dirty = true; |
| 562 | return 0; |
| 563 | } |
| 564 | } |
| 565 | |
Marc Reilly | d939fb9 | 2012-03-16 12:11:43 +1100 | [diff] [blame] | 566 | map->format.format_reg(map->work_buf, reg, map->reg_shift); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 567 | |
Lars-Peter Clausen | 6f30644 | 2011-09-05 20:46:32 +0200 | [diff] [blame] | 568 | u8[0] |= map->write_flag_mask; |
| 569 | |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 570 | trace_regmap_hw_write_start(map->dev, reg, |
| 571 | val_len / map->format.val_bytes); |
| 572 | |
Mark Brown | 2547e20 | 2011-07-20 21:47:22 +0100 | [diff] [blame] | 573 | /* If we're doing a single register write we can probably just |
| 574 | * send the work_buf directly, otherwise try to do a gather |
| 575 | * write. |
| 576 | */ |
Mark Brown | 82159ba | 2012-01-18 10:52:25 +0000 | [diff] [blame] | 577 | if (val == (map->work_buf + map->format.pad_bytes + |
| 578 | map->format.reg_bytes)) |
Stephen Warren | 0135bbc | 2012-04-04 15:48:30 -0600 | [diff] [blame] | 579 | ret = map->bus->write(map->bus_context, map->work_buf, |
Mark Brown | 82159ba | 2012-01-18 10:52:25 +0000 | [diff] [blame] | 580 | map->format.reg_bytes + |
| 581 | map->format.pad_bytes + |
| 582 | val_len); |
Mark Brown | 2547e20 | 2011-07-20 21:47:22 +0100 | [diff] [blame] | 583 | else if (map->bus->gather_write) |
Stephen Warren | 0135bbc | 2012-04-04 15:48:30 -0600 | [diff] [blame] | 584 | ret = map->bus->gather_write(map->bus_context, map->work_buf, |
Mark Brown | 82159ba | 2012-01-18 10:52:25 +0000 | [diff] [blame] | 585 | map->format.reg_bytes + |
| 586 | map->format.pad_bytes, |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 587 | val, val_len); |
| 588 | |
Mark Brown | 2547e20 | 2011-07-20 21:47:22 +0100 | [diff] [blame] | 589 | /* If that didn't work fall back on linearising by hand. */ |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 590 | if (ret == -ENOTSUPP) { |
Mark Brown | 82159ba | 2012-01-18 10:52:25 +0000 | [diff] [blame] | 591 | len = map->format.reg_bytes + map->format.pad_bytes + val_len; |
| 592 | buf = kzalloc(len, GFP_KERNEL); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 593 | if (!buf) |
| 594 | return -ENOMEM; |
| 595 | |
| 596 | memcpy(buf, map->work_buf, map->format.reg_bytes); |
Mark Brown | 82159ba | 2012-01-18 10:52:25 +0000 | [diff] [blame] | 597 | memcpy(buf + map->format.reg_bytes + map->format.pad_bytes, |
| 598 | val, val_len); |
Stephen Warren | 0135bbc | 2012-04-04 15:48:30 -0600 | [diff] [blame] | 599 | ret = map->bus->write(map->bus_context, buf, len); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 600 | |
| 601 | kfree(buf); |
| 602 | } |
| 603 | |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 604 | trace_regmap_hw_write_done(map->dev, reg, |
| 605 | val_len / map->format.val_bytes); |
| 606 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 607 | return ret; |
| 608 | } |
| 609 | |
Dimitris Papastamos | 4d2dc09 | 2011-09-29 10:39:07 +0100 | [diff] [blame] | 610 | int _regmap_write(struct regmap *map, unsigned int reg, |
| 611 | unsigned int val) |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 612 | { |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 613 | int ret; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 614 | BUG_ON(!map->format.format_write && !map->format.format_val); |
| 615 | |
Laxman Dewangan | c915719 | 2012-02-10 21:30:27 +0530 | [diff] [blame] | 616 | if (!map->cache_bypass && map->format.format_write) { |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 617 | ret = regcache_write(map, reg, val); |
| 618 | if (ret != 0) |
| 619 | return ret; |
Mark Brown | 8ae0d7e | 2011-10-26 10:34:22 +0200 | [diff] [blame] | 620 | if (map->cache_only) { |
| 621 | map->cache_dirty = true; |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 622 | return 0; |
Mark Brown | 8ae0d7e | 2011-10-26 10:34:22 +0200 | [diff] [blame] | 623 | } |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 624 | } |
| 625 | |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 626 | trace_regmap_reg_write(map->dev, reg, val); |
| 627 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 628 | if (map->format.format_write) { |
| 629 | map->format.format_write(map, reg, val); |
| 630 | |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 631 | trace_regmap_hw_write_start(map->dev, reg, 1); |
| 632 | |
Stephen Warren | 0135bbc | 2012-04-04 15:48:30 -0600 | [diff] [blame] | 633 | ret = map->bus->write(map->bus_context, map->work_buf, |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 634 | map->format.buf_size); |
| 635 | |
| 636 | trace_regmap_hw_write_done(map->dev, reg, 1); |
| 637 | |
| 638 | return ret; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 639 | } else { |
Mark Brown | 82159ba | 2012-01-18 10:52:25 +0000 | [diff] [blame] | 640 | map->format.format_val(map->work_buf + map->format.reg_bytes |
Marc Reilly | d939fb9 | 2012-03-16 12:11:43 +1100 | [diff] [blame] | 641 | + map->format.pad_bytes, val, 0); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 642 | return _regmap_raw_write(map, reg, |
Mark Brown | 82159ba | 2012-01-18 10:52:25 +0000 | [diff] [blame] | 643 | map->work_buf + |
| 644 | map->format.reg_bytes + |
| 645 | map->format.pad_bytes, |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 646 | map->format.val_bytes); |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | /** |
| 651 | * regmap_write(): Write a value to a single register |
| 652 | * |
| 653 | * @map: Register map to write to |
| 654 | * @reg: Register to write to |
| 655 | * @val: Value to be written |
| 656 | * |
| 657 | * A value of zero will be returned on success, a negative errno will |
| 658 | * be returned in error cases. |
| 659 | */ |
| 660 | int regmap_write(struct regmap *map, unsigned int reg, unsigned int val) |
| 661 | { |
| 662 | int ret; |
| 663 | |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 664 | if (reg % map->reg_stride) |
| 665 | return -EINVAL; |
| 666 | |
Stephen Warren | bacdbe0 | 2012-04-04 15:48:28 -0600 | [diff] [blame] | 667 | map->lock(map); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 668 | |
| 669 | ret = _regmap_write(map, reg, val); |
| 670 | |
Stephen Warren | bacdbe0 | 2012-04-04 15:48:28 -0600 | [diff] [blame] | 671 | map->unlock(map); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 672 | |
| 673 | return ret; |
| 674 | } |
| 675 | EXPORT_SYMBOL_GPL(regmap_write); |
| 676 | |
| 677 | /** |
| 678 | * regmap_raw_write(): Write raw values to one or more registers |
| 679 | * |
| 680 | * @map: Register map to write to |
| 681 | * @reg: Initial register to write to |
| 682 | * @val: Block of data to be written, laid out for direct transmission to the |
| 683 | * device |
| 684 | * @val_len: Length of data pointed to by val. |
| 685 | * |
| 686 | * This function is intended to be used for things like firmware |
| 687 | * download where a large block of data needs to be transferred to the |
| 688 | * device. No formatting will be done on the data provided. |
| 689 | * |
| 690 | * A value of zero will be returned on success, a negative errno will |
| 691 | * be returned in error cases. |
| 692 | */ |
| 693 | int regmap_raw_write(struct regmap *map, unsigned int reg, |
| 694 | const void *val, size_t val_len) |
| 695 | { |
| 696 | int ret; |
| 697 | |
Stephen Warren | 851960b | 2012-04-06 15:16:03 -0600 | [diff] [blame] | 698 | if (val_len % map->format.val_bytes) |
| 699 | return -EINVAL; |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 700 | if (reg % map->reg_stride) |
| 701 | return -EINVAL; |
Stephen Warren | 851960b | 2012-04-06 15:16:03 -0600 | [diff] [blame] | 702 | |
Stephen Warren | bacdbe0 | 2012-04-04 15:48:28 -0600 | [diff] [blame] | 703 | map->lock(map); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 704 | |
| 705 | ret = _regmap_raw_write(map, reg, val, val_len); |
| 706 | |
Stephen Warren | bacdbe0 | 2012-04-04 15:48:28 -0600 | [diff] [blame] | 707 | map->unlock(map); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 708 | |
| 709 | return ret; |
| 710 | } |
| 711 | EXPORT_SYMBOL_GPL(regmap_raw_write); |
| 712 | |
Laxman Dewangan | 8eaeb21 | 2012-02-12 19:49:43 +0530 | [diff] [blame] | 713 | /* |
| 714 | * regmap_bulk_write(): Write multiple registers to the device |
| 715 | * |
| 716 | * @map: Register map to write to |
| 717 | * @reg: First register to be write from |
| 718 | * @val: Block of data to be written, in native register size for device |
| 719 | * @val_count: Number of registers to write |
| 720 | * |
| 721 | * This function is intended to be used for writing a large block of |
| 722 | * data to be device either in single transfer or multiple transfer. |
| 723 | * |
| 724 | * A value of zero will be returned on success, a negative errno will |
| 725 | * be returned in error cases. |
| 726 | */ |
| 727 | int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val, |
| 728 | size_t val_count) |
| 729 | { |
| 730 | int ret = 0, i; |
| 731 | size_t val_bytes = map->format.val_bytes; |
| 732 | void *wval; |
| 733 | |
| 734 | if (!map->format.parse_val) |
| 735 | return -EINVAL; |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 736 | if (reg % map->reg_stride) |
| 737 | return -EINVAL; |
Laxman Dewangan | 8eaeb21 | 2012-02-12 19:49:43 +0530 | [diff] [blame] | 738 | |
Stephen Warren | bacdbe0 | 2012-04-04 15:48:28 -0600 | [diff] [blame] | 739 | map->lock(map); |
Laxman Dewangan | 8eaeb21 | 2012-02-12 19:49:43 +0530 | [diff] [blame] | 740 | |
| 741 | /* No formatting is require if val_byte is 1 */ |
| 742 | if (val_bytes == 1) { |
| 743 | wval = (void *)val; |
| 744 | } else { |
| 745 | wval = kmemdup(val, val_count * val_bytes, GFP_KERNEL); |
| 746 | if (!wval) { |
| 747 | ret = -ENOMEM; |
| 748 | dev_err(map->dev, "Error in memory allocation\n"); |
| 749 | goto out; |
| 750 | } |
| 751 | for (i = 0; i < val_count * val_bytes; i += val_bytes) |
| 752 | map->format.parse_val(wval + i); |
| 753 | } |
Ashish Jangam | 2e33caf | 2012-04-30 23:23:40 +0100 | [diff] [blame] | 754 | /* |
| 755 | * Some devices does not support bulk write, for |
| 756 | * them we have a series of single write operations. |
| 757 | */ |
| 758 | if (map->use_single_rw) { |
| 759 | for (i = 0; i < val_count; i++) { |
| 760 | ret = regmap_raw_write(map, |
| 761 | reg + (i * map->reg_stride), |
| 762 | val + (i * val_bytes), |
| 763 | val_bytes); |
| 764 | if (ret != 0) |
| 765 | return ret; |
| 766 | } |
| 767 | } else { |
| 768 | ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count); |
| 769 | } |
Laxman Dewangan | 8eaeb21 | 2012-02-12 19:49:43 +0530 | [diff] [blame] | 770 | |
| 771 | if (val_bytes != 1) |
| 772 | kfree(wval); |
| 773 | |
| 774 | out: |
Stephen Warren | bacdbe0 | 2012-04-04 15:48:28 -0600 | [diff] [blame] | 775 | map->unlock(map); |
Laxman Dewangan | 8eaeb21 | 2012-02-12 19:49:43 +0530 | [diff] [blame] | 776 | return ret; |
| 777 | } |
| 778 | EXPORT_SYMBOL_GPL(regmap_bulk_write); |
| 779 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 780 | static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val, |
| 781 | unsigned int val_len) |
| 782 | { |
| 783 | u8 *u8 = map->work_buf; |
| 784 | int ret; |
| 785 | |
Marc Reilly | d939fb9 | 2012-03-16 12:11:43 +1100 | [diff] [blame] | 786 | map->format.format_reg(map->work_buf, reg, map->reg_shift); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 787 | |
| 788 | /* |
Lars-Peter Clausen | 6f30644 | 2011-09-05 20:46:32 +0200 | [diff] [blame] | 789 | * 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] | 790 | * register addresss; since it's always the high bits for all |
| 791 | * current formats we can do this here rather than in |
| 792 | * formatting. This may break if we get interesting formats. |
| 793 | */ |
Lars-Peter Clausen | 6f30644 | 2011-09-05 20:46:32 +0200 | [diff] [blame] | 794 | u8[0] |= map->read_flag_mask; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 795 | |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 796 | trace_regmap_hw_read_start(map->dev, reg, |
| 797 | val_len / map->format.val_bytes); |
| 798 | |
Stephen Warren | 0135bbc | 2012-04-04 15:48:30 -0600 | [diff] [blame] | 799 | ret = map->bus->read(map->bus_context, map->work_buf, |
Mark Brown | 82159ba | 2012-01-18 10:52:25 +0000 | [diff] [blame] | 800 | map->format.reg_bytes + map->format.pad_bytes, |
Mark Brown | 40c5cc2 | 2011-07-24 22:39:12 +0100 | [diff] [blame] | 801 | val, val_len); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 802 | |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 803 | trace_regmap_hw_read_done(map->dev, reg, |
| 804 | val_len / map->format.val_bytes); |
| 805 | |
| 806 | return ret; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | static int _regmap_read(struct regmap *map, unsigned int reg, |
| 810 | unsigned int *val) |
| 811 | { |
| 812 | int ret; |
| 813 | |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 814 | if (!map->cache_bypass) { |
| 815 | ret = regcache_read(map, reg, val); |
| 816 | if (ret == 0) |
| 817 | return 0; |
| 818 | } |
| 819 | |
Lars-Peter Clausen | 1925441 | 2011-11-16 16:28:19 +0100 | [diff] [blame] | 820 | if (!map->format.parse_val) |
| 821 | return -EINVAL; |
| 822 | |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 823 | if (map->cache_only) |
| 824 | return -EBUSY; |
| 825 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 826 | 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] | 827 | if (ret == 0) { |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 828 | *val = map->format.parse_val(map->work_buf); |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 829 | trace_regmap_reg_read(map->dev, reg, *val); |
| 830 | } |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 831 | |
Mark Brown | f298536 | 2012-04-30 21:25:05 +0100 | [diff] [blame] | 832 | if (ret == 0 && !map->cache_bypass) |
| 833 | regcache_write(map, reg, *val); |
| 834 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 835 | return ret; |
| 836 | } |
| 837 | |
| 838 | /** |
| 839 | * regmap_read(): Read a value from a single register |
| 840 | * |
| 841 | * @map: Register map to write to |
| 842 | * @reg: Register to be read from |
| 843 | * @val: Pointer to store read value |
| 844 | * |
| 845 | * A value of zero will be returned on success, a negative errno will |
| 846 | * be returned in error cases. |
| 847 | */ |
| 848 | int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val) |
| 849 | { |
| 850 | int ret; |
| 851 | |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 852 | if (reg % map->reg_stride) |
| 853 | return -EINVAL; |
| 854 | |
Stephen Warren | bacdbe0 | 2012-04-04 15:48:28 -0600 | [diff] [blame] | 855 | map->lock(map); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 856 | |
| 857 | ret = _regmap_read(map, reg, val); |
| 858 | |
Stephen Warren | bacdbe0 | 2012-04-04 15:48:28 -0600 | [diff] [blame] | 859 | map->unlock(map); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 860 | |
| 861 | return ret; |
| 862 | } |
| 863 | EXPORT_SYMBOL_GPL(regmap_read); |
| 864 | |
| 865 | /** |
| 866 | * regmap_raw_read(): Read raw data from the device |
| 867 | * |
| 868 | * @map: Register map to write to |
| 869 | * @reg: First register to be read from |
| 870 | * @val: Pointer to store read value |
| 871 | * @val_len: Size of data to read |
| 872 | * |
| 873 | * A value of zero will be returned on success, a negative errno will |
| 874 | * be returned in error cases. |
| 875 | */ |
| 876 | int regmap_raw_read(struct regmap *map, unsigned int reg, void *val, |
| 877 | size_t val_len) |
| 878 | { |
Mark Brown | b8fb5ab | 2012-02-21 19:12:47 +0000 | [diff] [blame] | 879 | size_t val_bytes = map->format.val_bytes; |
| 880 | size_t val_count = val_len / val_bytes; |
| 881 | unsigned int v; |
| 882 | int ret, i; |
Mark Brown | 04e016a | 2011-10-09 13:35:43 +0100 | [diff] [blame] | 883 | |
Stephen Warren | 851960b | 2012-04-06 15:16:03 -0600 | [diff] [blame] | 884 | if (val_len % map->format.val_bytes) |
| 885 | return -EINVAL; |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 886 | if (reg % map->reg_stride) |
| 887 | return -EINVAL; |
Stephen Warren | 851960b | 2012-04-06 15:16:03 -0600 | [diff] [blame] | 888 | |
Stephen Warren | bacdbe0 | 2012-04-04 15:48:28 -0600 | [diff] [blame] | 889 | map->lock(map); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 890 | |
Mark Brown | b8fb5ab | 2012-02-21 19:12:47 +0000 | [diff] [blame] | 891 | if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass || |
| 892 | map->cache_type == REGCACHE_NONE) { |
| 893 | /* Physical block read if there's no cache involved */ |
| 894 | ret = _regmap_raw_read(map, reg, val, val_len); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 895 | |
Mark Brown | b8fb5ab | 2012-02-21 19:12:47 +0000 | [diff] [blame] | 896 | } else { |
| 897 | /* Otherwise go word by word for the cache; should be low |
| 898 | * cost as we expect to hit the cache. |
| 899 | */ |
| 900 | for (i = 0; i < val_count; i++) { |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 901 | ret = _regmap_read(map, reg + (i * map->reg_stride), |
| 902 | &v); |
Mark Brown | b8fb5ab | 2012-02-21 19:12:47 +0000 | [diff] [blame] | 903 | if (ret != 0) |
| 904 | goto out; |
| 905 | |
Marc Reilly | d939fb9 | 2012-03-16 12:11:43 +1100 | [diff] [blame] | 906 | map->format.format_val(val + (i * val_bytes), v, 0); |
Mark Brown | b8fb5ab | 2012-02-21 19:12:47 +0000 | [diff] [blame] | 907 | } |
| 908 | } |
| 909 | |
| 910 | out: |
Stephen Warren | bacdbe0 | 2012-04-04 15:48:28 -0600 | [diff] [blame] | 911 | map->unlock(map); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 912 | |
| 913 | return ret; |
| 914 | } |
| 915 | EXPORT_SYMBOL_GPL(regmap_raw_read); |
| 916 | |
| 917 | /** |
| 918 | * regmap_bulk_read(): Read multiple registers from the device |
| 919 | * |
| 920 | * @map: Register map to write to |
| 921 | * @reg: First register to be read from |
| 922 | * @val: Pointer to store read value, in native register size for device |
| 923 | * @val_count: Number of registers to read |
| 924 | * |
| 925 | * A value of zero will be returned on success, a negative errno will |
| 926 | * be returned in error cases. |
| 927 | */ |
| 928 | int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val, |
| 929 | size_t val_count) |
| 930 | { |
| 931 | int ret, i; |
| 932 | size_t val_bytes = map->format.val_bytes; |
Lars-Peter Clausen | 82cd996 | 2011-11-08 18:37:25 +0100 | [diff] [blame] | 933 | bool vol = regmap_volatile_range(map, reg, val_count); |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 934 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 935 | if (!map->format.parse_val) |
| 936 | return -EINVAL; |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 937 | if (reg % map->reg_stride) |
| 938 | return -EINVAL; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 939 | |
Mark Brown | de2d808 | 2011-10-10 13:24:52 +0100 | [diff] [blame] | 940 | if (vol || map->cache_type == REGCACHE_NONE) { |
Ashish Jangam | 2e33caf | 2012-04-30 23:23:40 +0100 | [diff] [blame] | 941 | /* |
| 942 | * Some devices does not support bulk read, for |
| 943 | * them we have a series of single read operations. |
| 944 | */ |
| 945 | if (map->use_single_rw) { |
| 946 | for (i = 0; i < val_count; i++) { |
| 947 | ret = regmap_raw_read(map, |
| 948 | reg + (i * map->reg_stride), |
| 949 | val + (i * val_bytes), |
| 950 | val_bytes); |
| 951 | if (ret != 0) |
| 952 | return ret; |
| 953 | } |
| 954 | } else { |
| 955 | ret = regmap_raw_read(map, reg, val, |
| 956 | val_bytes * val_count); |
| 957 | if (ret != 0) |
| 958 | return ret; |
| 959 | } |
Mark Brown | de2d808 | 2011-10-10 13:24:52 +0100 | [diff] [blame] | 960 | |
| 961 | for (i = 0; i < val_count * val_bytes; i += val_bytes) |
| 962 | map->format.parse_val(val + i); |
| 963 | } else { |
| 964 | for (i = 0; i < val_count; i++) { |
Laxman Dewangan | 6560ffd | 2012-05-09 17:43:12 +0530 | [diff] [blame] | 965 | unsigned int ival; |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 966 | ret = regmap_read(map, reg + (i * map->reg_stride), |
Mark Brown | 25061d2 | 2012-05-12 13:06:08 +0100 | [diff] [blame] | 967 | &ival); |
Mark Brown | de2d808 | 2011-10-10 13:24:52 +0100 | [diff] [blame] | 968 | if (ret != 0) |
| 969 | return ret; |
Laxman Dewangan | 6560ffd | 2012-05-09 17:43:12 +0530 | [diff] [blame] | 970 | memcpy(val + (i * val_bytes), &ival, val_bytes); |
Mark Brown | de2d808 | 2011-10-10 13:24:52 +0100 | [diff] [blame] | 971 | } |
| 972 | } |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 973 | |
| 974 | return 0; |
| 975 | } |
| 976 | EXPORT_SYMBOL_GPL(regmap_bulk_read); |
| 977 | |
Mark Brown | 018690d | 2011-11-29 20:10:36 +0000 | [diff] [blame] | 978 | static int _regmap_update_bits(struct regmap *map, unsigned int reg, |
| 979 | unsigned int mask, unsigned int val, |
| 980 | bool *change) |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 981 | { |
| 982 | int ret; |
Mark Brown | d91e8db2 | 2011-11-18 16:03:50 +0000 | [diff] [blame] | 983 | unsigned int tmp, orig; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 984 | |
Mark Brown | d91e8db2 | 2011-11-18 16:03:50 +0000 | [diff] [blame] | 985 | ret = _regmap_read(map, reg, &orig); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 986 | if (ret != 0) |
Krystian Garbaciak | fc3ebd7 | 2012-06-15 11:23:56 +0100 | [diff] [blame^] | 987 | return ret; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 988 | |
Mark Brown | d91e8db2 | 2011-11-18 16:03:50 +0000 | [diff] [blame] | 989 | tmp = orig & ~mask; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 990 | tmp |= val & mask; |
| 991 | |
Mark Brown | 018690d | 2011-11-29 20:10:36 +0000 | [diff] [blame] | 992 | if (tmp != orig) { |
Mark Brown | d91e8db2 | 2011-11-18 16:03:50 +0000 | [diff] [blame] | 993 | ret = _regmap_write(map, reg, tmp); |
Mark Brown | 018690d | 2011-11-29 20:10:36 +0000 | [diff] [blame] | 994 | *change = true; |
| 995 | } else { |
| 996 | *change = false; |
| 997 | } |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 998 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 999 | return ret; |
| 1000 | } |
Mark Brown | 018690d | 2011-11-29 20:10:36 +0000 | [diff] [blame] | 1001 | |
| 1002 | /** |
| 1003 | * regmap_update_bits: Perform a read/modify/write cycle on the register map |
| 1004 | * |
| 1005 | * @map: Register map to update |
| 1006 | * @reg: Register to update |
| 1007 | * @mask: Bitmask to change |
| 1008 | * @val: New value for bitmask |
| 1009 | * |
| 1010 | * Returns zero for success, a negative number on error. |
| 1011 | */ |
| 1012 | int regmap_update_bits(struct regmap *map, unsigned int reg, |
| 1013 | unsigned int mask, unsigned int val) |
| 1014 | { |
| 1015 | bool change; |
Krystian Garbaciak | fc3ebd7 | 2012-06-15 11:23:56 +0100 | [diff] [blame^] | 1016 | int ret; |
| 1017 | |
| 1018 | map->lock(map); |
| 1019 | ret = _regmap_update_bits(map, reg, mask, val, &change); |
| 1020 | map->unlock(map); |
| 1021 | |
| 1022 | return ret; |
Mark Brown | 018690d | 2011-11-29 20:10:36 +0000 | [diff] [blame] | 1023 | } |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1024 | EXPORT_SYMBOL_GPL(regmap_update_bits); |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 1025 | |
Mark Brown | 018690d | 2011-11-29 20:10:36 +0000 | [diff] [blame] | 1026 | /** |
| 1027 | * regmap_update_bits_check: Perform a read/modify/write cycle on the |
| 1028 | * register map and report if updated |
| 1029 | * |
| 1030 | * @map: Register map to update |
| 1031 | * @reg: Register to update |
| 1032 | * @mask: Bitmask to change |
| 1033 | * @val: New value for bitmask |
| 1034 | * @change: Boolean indicating if a write was done |
| 1035 | * |
| 1036 | * Returns zero for success, a negative number on error. |
| 1037 | */ |
| 1038 | int regmap_update_bits_check(struct regmap *map, unsigned int reg, |
| 1039 | unsigned int mask, unsigned int val, |
| 1040 | bool *change) |
| 1041 | { |
Krystian Garbaciak | fc3ebd7 | 2012-06-15 11:23:56 +0100 | [diff] [blame^] | 1042 | int ret; |
| 1043 | |
| 1044 | map->lock(map); |
| 1045 | ret = _regmap_update_bits(map, reg, mask, val, change); |
| 1046 | map->unlock(map); |
| 1047 | return ret; |
Mark Brown | 018690d | 2011-11-29 20:10:36 +0000 | [diff] [blame] | 1048 | } |
| 1049 | EXPORT_SYMBOL_GPL(regmap_update_bits_check); |
| 1050 | |
Mark Brown | 22f0d90 | 2012-01-21 12:01:14 +0000 | [diff] [blame] | 1051 | /** |
| 1052 | * regmap_register_patch: Register and apply register updates to be applied |
| 1053 | * on device initialistion |
| 1054 | * |
| 1055 | * @map: Register map to apply updates to. |
| 1056 | * @regs: Values to update. |
| 1057 | * @num_regs: Number of entries in regs. |
| 1058 | * |
| 1059 | * Register a set of register updates to be applied to the device |
| 1060 | * whenever the device registers are synchronised with the cache and |
| 1061 | * apply them immediately. Typically this is used to apply |
| 1062 | * corrections to be applied to the device defaults on startup, such |
| 1063 | * as the updates some vendors provide to undocumented registers. |
| 1064 | */ |
| 1065 | int regmap_register_patch(struct regmap *map, const struct reg_default *regs, |
| 1066 | int num_regs) |
| 1067 | { |
| 1068 | int i, ret; |
| 1069 | bool bypass; |
| 1070 | |
| 1071 | /* If needed the implementation can be extended to support this */ |
| 1072 | if (map->patch) |
| 1073 | return -EBUSY; |
| 1074 | |
Stephen Warren | bacdbe0 | 2012-04-04 15:48:28 -0600 | [diff] [blame] | 1075 | map->lock(map); |
Mark Brown | 22f0d90 | 2012-01-21 12:01:14 +0000 | [diff] [blame] | 1076 | |
| 1077 | bypass = map->cache_bypass; |
| 1078 | |
| 1079 | map->cache_bypass = true; |
| 1080 | |
| 1081 | /* Write out first; it's useful to apply even if we fail later. */ |
| 1082 | for (i = 0; i < num_regs; i++) { |
| 1083 | ret = _regmap_write(map, regs[i].reg, regs[i].def); |
| 1084 | if (ret != 0) { |
| 1085 | dev_err(map->dev, "Failed to write %x = %x: %d\n", |
| 1086 | regs[i].reg, regs[i].def, ret); |
| 1087 | goto out; |
| 1088 | } |
| 1089 | } |
| 1090 | |
Axel Lin | 2a14d7d9 | 2012-02-10 19:29:55 +0800 | [diff] [blame] | 1091 | map->patch = kcalloc(num_regs, sizeof(struct reg_default), GFP_KERNEL); |
Mark Brown | 22f0d90 | 2012-01-21 12:01:14 +0000 | [diff] [blame] | 1092 | if (map->patch != NULL) { |
| 1093 | memcpy(map->patch, regs, |
| 1094 | num_regs * sizeof(struct reg_default)); |
| 1095 | map->patch_regs = num_regs; |
| 1096 | } else { |
| 1097 | ret = -ENOMEM; |
| 1098 | } |
| 1099 | |
| 1100 | out: |
| 1101 | map->cache_bypass = bypass; |
| 1102 | |
Stephen Warren | bacdbe0 | 2012-04-04 15:48:28 -0600 | [diff] [blame] | 1103 | map->unlock(map); |
Mark Brown | 22f0d90 | 2012-01-21 12:01:14 +0000 | [diff] [blame] | 1104 | |
| 1105 | return ret; |
| 1106 | } |
| 1107 | EXPORT_SYMBOL_GPL(regmap_register_patch); |
| 1108 | |
Mark Brown | eae4b51 | 2012-03-14 13:15:03 +0000 | [diff] [blame] | 1109 | /* |
Mark Brown | a6539c3 | 2012-02-17 14:20:14 -0800 | [diff] [blame] | 1110 | * regmap_get_val_bytes(): Report the size of a register value |
| 1111 | * |
| 1112 | * Report the size of a register value, mainly intended to for use by |
| 1113 | * generic infrastructure built on top of regmap. |
| 1114 | */ |
| 1115 | int regmap_get_val_bytes(struct regmap *map) |
| 1116 | { |
| 1117 | if (map->format.format_write) |
| 1118 | return -EINVAL; |
| 1119 | |
| 1120 | return map->format.val_bytes; |
| 1121 | } |
| 1122 | EXPORT_SYMBOL_GPL(regmap_get_val_bytes); |
| 1123 | |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 1124 | static int __init regmap_initcall(void) |
| 1125 | { |
| 1126 | regmap_debugfs_initcall(); |
| 1127 | |
| 1128 | return 0; |
| 1129 | } |
| 1130 | postcore_initcall(regmap_initcall); |