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> |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 18 | #include <linux/rbtree.h> |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 19 | |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 20 | #define CREATE_TRACE_POINTS |
| 21 | #include <trace/events/regmap.h> |
| 22 | |
Mark Brown | 93de912 | 2011-07-20 22:35:37 +0100 | [diff] [blame] | 23 | #include "internal.h" |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 24 | |
Mark Brown | 1044c18 | 2012-07-06 14:10:23 +0100 | [diff] [blame] | 25 | /* |
| 26 | * Sometimes for failures during very early init the trace |
| 27 | * infrastructure isn't available early enough to be used. For this |
| 28 | * sort of problem defining LOG_DEVICE will add printks for basic |
| 29 | * register I/O on a specific device. |
| 30 | */ |
| 31 | #undef LOG_DEVICE |
| 32 | |
| 33 | static int _regmap_update_bits(struct regmap *map, unsigned int reg, |
| 34 | unsigned int mask, unsigned int val, |
| 35 | bool *change); |
| 36 | |
Mark Brown | 8de2f08 | 2011-08-10 17:14:41 +0900 | [diff] [blame] | 37 | bool regmap_writeable(struct regmap *map, unsigned int reg) |
| 38 | { |
| 39 | if (map->max_register && reg > map->max_register) |
| 40 | return false; |
| 41 | |
| 42 | if (map->writeable_reg) |
| 43 | return map->writeable_reg(map->dev, reg); |
| 44 | |
| 45 | return true; |
| 46 | } |
| 47 | |
| 48 | bool regmap_readable(struct regmap *map, unsigned int reg) |
| 49 | { |
| 50 | if (map->max_register && reg > map->max_register) |
| 51 | return false; |
| 52 | |
Wolfram Sang | 4191f19 | 2012-01-30 15:08:16 +0100 | [diff] [blame] | 53 | if (map->format.format_write) |
| 54 | return false; |
| 55 | |
Mark Brown | 8de2f08 | 2011-08-10 17:14:41 +0900 | [diff] [blame] | 56 | if (map->readable_reg) |
| 57 | return map->readable_reg(map->dev, reg); |
| 58 | |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | bool regmap_volatile(struct regmap *map, unsigned int reg) |
| 63 | { |
Wolfram Sang | 4191f19 | 2012-01-30 15:08:16 +0100 | [diff] [blame] | 64 | if (!regmap_readable(map, reg)) |
Mark Brown | 8de2f08 | 2011-08-10 17:14:41 +0900 | [diff] [blame] | 65 | return false; |
| 66 | |
| 67 | if (map->volatile_reg) |
| 68 | return map->volatile_reg(map->dev, reg); |
| 69 | |
| 70 | return true; |
| 71 | } |
| 72 | |
| 73 | bool regmap_precious(struct regmap *map, unsigned int reg) |
| 74 | { |
Wolfram Sang | 4191f19 | 2012-01-30 15:08:16 +0100 | [diff] [blame] | 75 | if (!regmap_readable(map, reg)) |
Mark Brown | 8de2f08 | 2011-08-10 17:14:41 +0900 | [diff] [blame] | 76 | return false; |
| 77 | |
| 78 | if (map->precious_reg) |
| 79 | return map->precious_reg(map->dev, reg); |
| 80 | |
| 81 | return false; |
| 82 | } |
| 83 | |
Lars-Peter Clausen | 82cd996 | 2011-11-08 18:37:25 +0100 | [diff] [blame] | 84 | static bool regmap_volatile_range(struct regmap *map, unsigned int reg, |
| 85 | unsigned int num) |
| 86 | { |
| 87 | unsigned int i; |
| 88 | |
| 89 | for (i = 0; i < num; i++) |
| 90 | if (!regmap_volatile(map, reg + i)) |
| 91 | return false; |
| 92 | |
| 93 | return true; |
| 94 | } |
| 95 | |
Wolfram Sang | 9aa5075 | 2012-01-27 16:10:22 +0100 | [diff] [blame] | 96 | static void regmap_format_2_6_write(struct regmap *map, |
| 97 | unsigned int reg, unsigned int val) |
| 98 | { |
| 99 | u8 *out = map->work_buf; |
| 100 | |
| 101 | *out = (reg << 6) | val; |
| 102 | } |
| 103 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 104 | static void regmap_format_4_12_write(struct regmap *map, |
| 105 | unsigned int reg, unsigned int val) |
| 106 | { |
| 107 | __be16 *out = map->work_buf; |
| 108 | *out = cpu_to_be16((reg << 12) | val); |
| 109 | } |
| 110 | |
| 111 | static void regmap_format_7_9_write(struct regmap *map, |
| 112 | unsigned int reg, unsigned int val) |
| 113 | { |
| 114 | __be16 *out = map->work_buf; |
| 115 | *out = cpu_to_be16((reg << 9) | val); |
| 116 | } |
| 117 | |
Lars-Peter Clausen | 7e5ec63 | 2011-11-16 16:28:21 +0100 | [diff] [blame] | 118 | static void regmap_format_10_14_write(struct regmap *map, |
| 119 | unsigned int reg, unsigned int val) |
| 120 | { |
| 121 | u8 *out = map->work_buf; |
| 122 | |
| 123 | out[2] = val; |
| 124 | out[1] = (val >> 8) | (reg << 6); |
| 125 | out[0] = reg >> 2; |
| 126 | } |
| 127 | |
Marc Reilly | d939fb9 | 2012-03-16 12:11:43 +1100 | [diff] [blame] | 128 | 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] | 129 | { |
| 130 | u8 *b = buf; |
| 131 | |
Marc Reilly | d939fb9 | 2012-03-16 12:11:43 +1100 | [diff] [blame] | 132 | b[0] = val << shift; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 133 | } |
| 134 | |
Stephen Warren | 141eba2 | 2012-05-24 10:47:26 -0600 | [diff] [blame] | 135 | static void regmap_format_16_be(void *buf, unsigned int val, unsigned int shift) |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 136 | { |
| 137 | __be16 *b = buf; |
| 138 | |
Marc Reilly | d939fb9 | 2012-03-16 12:11:43 +1100 | [diff] [blame] | 139 | b[0] = cpu_to_be16(val << shift); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 140 | } |
| 141 | |
Stephen Warren | 141eba2 | 2012-05-24 10:47:26 -0600 | [diff] [blame] | 142 | static void regmap_format_16_native(void *buf, unsigned int val, |
| 143 | unsigned int shift) |
| 144 | { |
| 145 | *(u16 *)buf = val << shift; |
| 146 | } |
| 147 | |
Marc Reilly | d939fb9 | 2012-03-16 12:11:43 +1100 | [diff] [blame] | 148 | 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] | 149 | { |
| 150 | u8 *b = buf; |
| 151 | |
Marc Reilly | d939fb9 | 2012-03-16 12:11:43 +1100 | [diff] [blame] | 152 | val <<= shift; |
| 153 | |
Marc Reilly | ea279fc | 2012-03-16 12:11:42 +1100 | [diff] [blame] | 154 | b[0] = val >> 16; |
| 155 | b[1] = val >> 8; |
| 156 | b[2] = val; |
| 157 | } |
| 158 | |
Stephen Warren | 141eba2 | 2012-05-24 10:47:26 -0600 | [diff] [blame] | 159 | static void regmap_format_32_be(void *buf, unsigned int val, unsigned int shift) |
Mark Brown | 7d5e525 | 2012-02-17 15:58:25 -0800 | [diff] [blame] | 160 | { |
| 161 | __be32 *b = buf; |
| 162 | |
Marc Reilly | d939fb9 | 2012-03-16 12:11:43 +1100 | [diff] [blame] | 163 | b[0] = cpu_to_be32(val << shift); |
Mark Brown | 7d5e525 | 2012-02-17 15:58:25 -0800 | [diff] [blame] | 164 | } |
| 165 | |
Stephen Warren | 141eba2 | 2012-05-24 10:47:26 -0600 | [diff] [blame] | 166 | static void regmap_format_32_native(void *buf, unsigned int val, |
| 167 | unsigned int shift) |
| 168 | { |
| 169 | *(u32 *)buf = val << shift; |
| 170 | } |
| 171 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 172 | static unsigned int regmap_parse_8(void *buf) |
| 173 | { |
| 174 | u8 *b = buf; |
| 175 | |
| 176 | return b[0]; |
| 177 | } |
| 178 | |
Stephen Warren | 141eba2 | 2012-05-24 10:47:26 -0600 | [diff] [blame] | 179 | static unsigned int regmap_parse_16_be(void *buf) |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 180 | { |
| 181 | __be16 *b = buf; |
| 182 | |
| 183 | b[0] = be16_to_cpu(b[0]); |
| 184 | |
| 185 | return b[0]; |
| 186 | } |
| 187 | |
Stephen Warren | 141eba2 | 2012-05-24 10:47:26 -0600 | [diff] [blame] | 188 | static unsigned int regmap_parse_16_native(void *buf) |
| 189 | { |
| 190 | return *(u16 *)buf; |
| 191 | } |
| 192 | |
Marc Reilly | ea279fc | 2012-03-16 12:11:42 +1100 | [diff] [blame] | 193 | static unsigned int regmap_parse_24(void *buf) |
| 194 | { |
| 195 | u8 *b = buf; |
| 196 | unsigned int ret = b[2]; |
| 197 | ret |= ((unsigned int)b[1]) << 8; |
| 198 | ret |= ((unsigned int)b[0]) << 16; |
| 199 | |
| 200 | return ret; |
| 201 | } |
| 202 | |
Stephen Warren | 141eba2 | 2012-05-24 10:47:26 -0600 | [diff] [blame] | 203 | static unsigned int regmap_parse_32_be(void *buf) |
Mark Brown | 7d5e525 | 2012-02-17 15:58:25 -0800 | [diff] [blame] | 204 | { |
| 205 | __be32 *b = buf; |
| 206 | |
| 207 | b[0] = be32_to_cpu(b[0]); |
| 208 | |
| 209 | return b[0]; |
| 210 | } |
| 211 | |
Stephen Warren | 141eba2 | 2012-05-24 10:47:26 -0600 | [diff] [blame] | 212 | static unsigned int regmap_parse_32_native(void *buf) |
| 213 | { |
| 214 | return *(u32 *)buf; |
| 215 | } |
| 216 | |
Stephen Warren | bacdbe0 | 2012-04-04 15:48:28 -0600 | [diff] [blame] | 217 | static void regmap_lock_mutex(struct regmap *map) |
| 218 | { |
| 219 | mutex_lock(&map->mutex); |
| 220 | } |
| 221 | |
| 222 | static void regmap_unlock_mutex(struct regmap *map) |
| 223 | { |
| 224 | mutex_unlock(&map->mutex); |
| 225 | } |
| 226 | |
| 227 | static void regmap_lock_spinlock(struct regmap *map) |
| 228 | { |
| 229 | spin_lock(&map->spinlock); |
| 230 | } |
| 231 | |
| 232 | static void regmap_unlock_spinlock(struct regmap *map) |
| 233 | { |
| 234 | spin_unlock(&map->spinlock); |
| 235 | } |
| 236 | |
Mark Brown | 72b39f6 | 2012-05-08 17:44:40 +0100 | [diff] [blame] | 237 | static void dev_get_regmap_release(struct device *dev, void *res) |
| 238 | { |
| 239 | /* |
| 240 | * We don't actually have anything to do here; the goal here |
| 241 | * is not to manage the regmap but to provide a simple way to |
| 242 | * get the regmap back given a struct device. |
| 243 | */ |
| 244 | } |
| 245 | |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 246 | static bool _regmap_range_add(struct regmap *map, |
| 247 | struct regmap_range_node *data) |
| 248 | { |
| 249 | struct rb_root *root = &map->range_tree; |
| 250 | struct rb_node **new = &(root->rb_node), *parent = NULL; |
| 251 | |
| 252 | while (*new) { |
| 253 | struct regmap_range_node *this = |
| 254 | container_of(*new, struct regmap_range_node, node); |
| 255 | |
| 256 | parent = *new; |
| 257 | if (data->range_max < this->range_min) |
| 258 | new = &((*new)->rb_left); |
| 259 | else if (data->range_min > this->range_max) |
| 260 | new = &((*new)->rb_right); |
| 261 | else |
| 262 | return false; |
| 263 | } |
| 264 | |
| 265 | rb_link_node(&data->node, parent, new); |
| 266 | rb_insert_color(&data->node, root); |
| 267 | |
| 268 | return true; |
| 269 | } |
| 270 | |
| 271 | static struct regmap_range_node *_regmap_range_lookup(struct regmap *map, |
| 272 | unsigned int reg) |
| 273 | { |
| 274 | struct rb_node *node = map->range_tree.rb_node; |
| 275 | |
| 276 | while (node) { |
| 277 | struct regmap_range_node *this = |
| 278 | container_of(node, struct regmap_range_node, node); |
| 279 | |
| 280 | if (reg < this->range_min) |
| 281 | node = node->rb_left; |
| 282 | else if (reg > this->range_max) |
| 283 | node = node->rb_right; |
| 284 | else |
| 285 | return this; |
| 286 | } |
| 287 | |
| 288 | return NULL; |
| 289 | } |
| 290 | |
| 291 | static void regmap_range_exit(struct regmap *map) |
| 292 | { |
| 293 | struct rb_node *next; |
| 294 | struct regmap_range_node *range_node; |
| 295 | |
| 296 | next = rb_first(&map->range_tree); |
| 297 | while (next) { |
| 298 | range_node = rb_entry(next, struct regmap_range_node, node); |
| 299 | next = rb_next(&range_node->node); |
| 300 | rb_erase(&range_node->node, &map->range_tree); |
| 301 | kfree(range_node); |
| 302 | } |
| 303 | |
| 304 | kfree(map->selector_work_buf); |
| 305 | } |
| 306 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 307 | /** |
| 308 | * regmap_init(): Initialise register map |
| 309 | * |
| 310 | * @dev: Device that will be interacted with |
| 311 | * @bus: Bus-specific callbacks to use with device |
Stephen Warren | 0135bbc | 2012-04-04 15:48:30 -0600 | [diff] [blame] | 312 | * @bus_context: Data passed to bus-specific callbacks |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 313 | * @config: Configuration for register map |
| 314 | * |
| 315 | * The return value will be an ERR_PTR() on error or a valid pointer to |
| 316 | * a struct regmap. This function should generally not be called |
| 317 | * directly, it should be called by bus-specific init functions. |
| 318 | */ |
| 319 | struct regmap *regmap_init(struct device *dev, |
| 320 | const struct regmap_bus *bus, |
Stephen Warren | 0135bbc | 2012-04-04 15:48:30 -0600 | [diff] [blame] | 321 | void *bus_context, |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 322 | const struct regmap_config *config) |
| 323 | { |
Mark Brown | 72b39f6 | 2012-05-08 17:44:40 +0100 | [diff] [blame] | 324 | struct regmap *map, **m; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 325 | int ret = -EINVAL; |
Stephen Warren | 141eba2 | 2012-05-24 10:47:26 -0600 | [diff] [blame] | 326 | enum regmap_endian reg_endian, val_endian; |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 327 | int i, j; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 328 | |
| 329 | if (!bus || !config) |
Lars-Peter Clausen | abbb18f | 2011-11-14 10:40:15 +0100 | [diff] [blame] | 330 | goto err; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 331 | |
| 332 | map = kzalloc(sizeof(*map), GFP_KERNEL); |
| 333 | if (map == NULL) { |
| 334 | ret = -ENOMEM; |
| 335 | goto err; |
| 336 | } |
| 337 | |
Stephen Warren | bacdbe0 | 2012-04-04 15:48:28 -0600 | [diff] [blame] | 338 | if (bus->fast_io) { |
| 339 | spin_lock_init(&map->spinlock); |
| 340 | map->lock = regmap_lock_spinlock; |
| 341 | map->unlock = regmap_unlock_spinlock; |
| 342 | } else { |
| 343 | mutex_init(&map->mutex); |
| 344 | map->lock = regmap_lock_mutex; |
| 345 | map->unlock = regmap_unlock_mutex; |
| 346 | } |
Wolfram Sang | c212acc | 2012-01-28 02:16:41 +0100 | [diff] [blame] | 347 | map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8); |
Mark Brown | 82159ba | 2012-01-18 10:52:25 +0000 | [diff] [blame] | 348 | map->format.pad_bytes = config->pad_bits / 8; |
Wolfram Sang | c212acc | 2012-01-28 02:16:41 +0100 | [diff] [blame] | 349 | map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8); |
Fabio Estevam | 5494a98 | 2012-05-31 21:10:30 -0300 | [diff] [blame] | 350 | map->format.buf_size = DIV_ROUND_UP(config->reg_bits + |
| 351 | config->val_bits + config->pad_bits, 8); |
Marc Reilly | d939fb9 | 2012-03-16 12:11:43 +1100 | [diff] [blame] | 352 | map->reg_shift = config->pad_bits % 8; |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 353 | if (config->reg_stride) |
| 354 | map->reg_stride = config->reg_stride; |
| 355 | else |
| 356 | map->reg_stride = 1; |
Ashish Jangam | 2e33caf | 2012-04-30 23:23:40 +0100 | [diff] [blame] | 357 | map->use_single_rw = config->use_single_rw; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 358 | map->dev = dev; |
| 359 | map->bus = bus; |
Stephen Warren | 0135bbc | 2012-04-04 15:48:30 -0600 | [diff] [blame] | 360 | map->bus_context = bus_context; |
Mark Brown | 2e2ae66 | 2011-07-20 22:33:39 +0100 | [diff] [blame] | 361 | map->max_register = config->max_register; |
| 362 | map->writeable_reg = config->writeable_reg; |
| 363 | map->readable_reg = config->readable_reg; |
| 364 | map->volatile_reg = config->volatile_reg; |
Mark Brown | 2efe164 | 2011-08-08 15:41:46 +0900 | [diff] [blame] | 365 | map->precious_reg = config->precious_reg; |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 366 | map->cache_type = config->cache_type; |
Mark Brown | 72b39f6 | 2012-05-08 17:44:40 +0100 | [diff] [blame] | 367 | map->name = config->name; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 368 | |
Lars-Peter Clausen | 6f30644 | 2011-09-05 20:46:32 +0200 | [diff] [blame] | 369 | if (config->read_flag_mask || config->write_flag_mask) { |
| 370 | map->read_flag_mask = config->read_flag_mask; |
| 371 | map->write_flag_mask = config->write_flag_mask; |
| 372 | } else { |
| 373 | map->read_flag_mask = bus->read_flag_mask; |
| 374 | } |
| 375 | |
Stephen Warren | 141eba2 | 2012-05-24 10:47:26 -0600 | [diff] [blame] | 376 | reg_endian = config->reg_format_endian; |
| 377 | if (reg_endian == REGMAP_ENDIAN_DEFAULT) |
| 378 | reg_endian = bus->reg_format_endian_default; |
| 379 | if (reg_endian == REGMAP_ENDIAN_DEFAULT) |
| 380 | reg_endian = REGMAP_ENDIAN_BIG; |
| 381 | |
| 382 | val_endian = config->val_format_endian; |
| 383 | if (val_endian == REGMAP_ENDIAN_DEFAULT) |
| 384 | val_endian = bus->val_format_endian_default; |
| 385 | if (val_endian == REGMAP_ENDIAN_DEFAULT) |
| 386 | val_endian = REGMAP_ENDIAN_BIG; |
| 387 | |
Marc Reilly | d939fb9 | 2012-03-16 12:11:43 +1100 | [diff] [blame] | 388 | switch (config->reg_bits + map->reg_shift) { |
Wolfram Sang | 9aa5075 | 2012-01-27 16:10:22 +0100 | [diff] [blame] | 389 | case 2: |
| 390 | switch (config->val_bits) { |
| 391 | case 6: |
| 392 | map->format.format_write = regmap_format_2_6_write; |
| 393 | break; |
| 394 | default: |
| 395 | goto err_map; |
| 396 | } |
| 397 | break; |
| 398 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 399 | case 4: |
| 400 | switch (config->val_bits) { |
| 401 | case 12: |
| 402 | map->format.format_write = regmap_format_4_12_write; |
| 403 | break; |
| 404 | default: |
| 405 | goto err_map; |
| 406 | } |
| 407 | break; |
| 408 | |
| 409 | case 7: |
| 410 | switch (config->val_bits) { |
| 411 | case 9: |
| 412 | map->format.format_write = regmap_format_7_9_write; |
| 413 | break; |
| 414 | default: |
| 415 | goto err_map; |
| 416 | } |
| 417 | break; |
| 418 | |
Lars-Peter Clausen | 7e5ec63 | 2011-11-16 16:28:21 +0100 | [diff] [blame] | 419 | case 10: |
| 420 | switch (config->val_bits) { |
| 421 | case 14: |
| 422 | map->format.format_write = regmap_format_10_14_write; |
| 423 | break; |
| 424 | default: |
| 425 | goto err_map; |
| 426 | } |
| 427 | break; |
| 428 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 429 | case 8: |
| 430 | map->format.format_reg = regmap_format_8; |
| 431 | break; |
| 432 | |
| 433 | case 16: |
Stephen Warren | 141eba2 | 2012-05-24 10:47:26 -0600 | [diff] [blame] | 434 | switch (reg_endian) { |
| 435 | case REGMAP_ENDIAN_BIG: |
| 436 | map->format.format_reg = regmap_format_16_be; |
| 437 | break; |
| 438 | case REGMAP_ENDIAN_NATIVE: |
| 439 | map->format.format_reg = regmap_format_16_native; |
| 440 | break; |
| 441 | default: |
| 442 | goto err_map; |
| 443 | } |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 444 | break; |
| 445 | |
Mark Brown | 7d5e525 | 2012-02-17 15:58:25 -0800 | [diff] [blame] | 446 | case 32: |
Stephen Warren | 141eba2 | 2012-05-24 10:47:26 -0600 | [diff] [blame] | 447 | switch (reg_endian) { |
| 448 | case REGMAP_ENDIAN_BIG: |
| 449 | map->format.format_reg = regmap_format_32_be; |
| 450 | break; |
| 451 | case REGMAP_ENDIAN_NATIVE: |
| 452 | map->format.format_reg = regmap_format_32_native; |
| 453 | break; |
| 454 | default: |
| 455 | goto err_map; |
| 456 | } |
Mark Brown | 7d5e525 | 2012-02-17 15:58:25 -0800 | [diff] [blame] | 457 | break; |
| 458 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 459 | default: |
| 460 | goto err_map; |
| 461 | } |
| 462 | |
| 463 | switch (config->val_bits) { |
| 464 | case 8: |
| 465 | map->format.format_val = regmap_format_8; |
| 466 | map->format.parse_val = regmap_parse_8; |
| 467 | break; |
| 468 | case 16: |
Stephen Warren | 141eba2 | 2012-05-24 10:47:26 -0600 | [diff] [blame] | 469 | switch (val_endian) { |
| 470 | case REGMAP_ENDIAN_BIG: |
| 471 | map->format.format_val = regmap_format_16_be; |
| 472 | map->format.parse_val = regmap_parse_16_be; |
| 473 | break; |
| 474 | case REGMAP_ENDIAN_NATIVE: |
| 475 | map->format.format_val = regmap_format_16_native; |
| 476 | map->format.parse_val = regmap_parse_16_native; |
| 477 | break; |
| 478 | default: |
| 479 | goto err_map; |
| 480 | } |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 481 | break; |
Marc Reilly | ea279fc | 2012-03-16 12:11:42 +1100 | [diff] [blame] | 482 | case 24: |
Stephen Warren | 141eba2 | 2012-05-24 10:47:26 -0600 | [diff] [blame] | 483 | if (val_endian != REGMAP_ENDIAN_BIG) |
| 484 | goto err_map; |
Marc Reilly | ea279fc | 2012-03-16 12:11:42 +1100 | [diff] [blame] | 485 | map->format.format_val = regmap_format_24; |
| 486 | map->format.parse_val = regmap_parse_24; |
| 487 | break; |
Mark Brown | 7d5e525 | 2012-02-17 15:58:25 -0800 | [diff] [blame] | 488 | case 32: |
Stephen Warren | 141eba2 | 2012-05-24 10:47:26 -0600 | [diff] [blame] | 489 | switch (val_endian) { |
| 490 | case REGMAP_ENDIAN_BIG: |
| 491 | map->format.format_val = regmap_format_32_be; |
| 492 | map->format.parse_val = regmap_parse_32_be; |
| 493 | break; |
| 494 | case REGMAP_ENDIAN_NATIVE: |
| 495 | map->format.format_val = regmap_format_32_native; |
| 496 | map->format.parse_val = regmap_parse_32_native; |
| 497 | break; |
| 498 | default: |
| 499 | goto err_map; |
| 500 | } |
Mark Brown | 7d5e525 | 2012-02-17 15:58:25 -0800 | [diff] [blame] | 501 | break; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 502 | } |
| 503 | |
Stephen Warren | 141eba2 | 2012-05-24 10:47:26 -0600 | [diff] [blame] | 504 | if (map->format.format_write) { |
| 505 | if ((reg_endian != REGMAP_ENDIAN_BIG) || |
| 506 | (val_endian != REGMAP_ENDIAN_BIG)) |
| 507 | goto err_map; |
Mark Brown | 7a64761 | 2012-04-30 23:26:32 +0100 | [diff] [blame] | 508 | map->use_single_rw = true; |
Stephen Warren | 141eba2 | 2012-05-24 10:47:26 -0600 | [diff] [blame] | 509 | } |
Mark Brown | 7a64761 | 2012-04-30 23:26:32 +0100 | [diff] [blame] | 510 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 511 | if (!map->format.format_write && |
| 512 | !(map->format.format_reg && map->format.format_val)) |
| 513 | goto err_map; |
| 514 | |
Mark Brown | 82159ba | 2012-01-18 10:52:25 +0000 | [diff] [blame] | 515 | map->work_buf = kzalloc(map->format.buf_size, GFP_KERNEL); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 516 | if (map->work_buf == NULL) { |
| 517 | ret = -ENOMEM; |
Mark Brown | 5204f5e | 2011-09-05 08:07:47 -0700 | [diff] [blame] | 518 | goto err_map; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 519 | } |
| 520 | |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 521 | map->range_tree = RB_ROOT; |
Mark Brown | e3549cd | 2012-10-02 20:17:15 +0100 | [diff] [blame] | 522 | for (i = 0; i < config->num_ranges; i++) { |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 523 | const struct regmap_range_cfg *range_cfg = &config->ranges[i]; |
| 524 | struct regmap_range_node *new; |
| 525 | |
| 526 | /* Sanity check */ |
Mark Brown | 061adc0 | 2012-10-03 12:17:51 +0100 | [diff] [blame^] | 527 | if (range_cfg->range_max < range_cfg->range_min) { |
| 528 | dev_err(map->dev, "Invalid range %d: %d < %d\n", i, |
| 529 | range_cfg->range_max, range_cfg->range_min); |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 530 | goto err_range; |
Mark Brown | 061adc0 | 2012-10-03 12:17:51 +0100 | [diff] [blame^] | 531 | } |
| 532 | |
| 533 | if (range_cfg->range_max > map->max_register) { |
| 534 | dev_err(map->dev, "Invalid range %d: %d > %d\n", i, |
| 535 | range_cfg->range_max, map->max_register); |
| 536 | goto err_range; |
| 537 | } |
| 538 | |
| 539 | if (range_cfg->selector_reg > map->max_register) { |
| 540 | dev_err(map->dev, |
| 541 | "Invalid range %d: selector out of map\n", i); |
| 542 | goto err_range; |
| 543 | } |
| 544 | |
| 545 | if (range_cfg->window_len == 0) { |
| 546 | dev_err(map->dev, "Invalid range %d: window_len 0\n", |
| 547 | i); |
| 548 | goto err_range; |
| 549 | } |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 550 | |
| 551 | /* Make sure, that this register range has no selector |
| 552 | or data window within its boundary */ |
Mark Brown | e3549cd | 2012-10-02 20:17:15 +0100 | [diff] [blame] | 553 | for (j = 0; j < config->num_ranges; j++) { |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 554 | unsigned sel_reg = config->ranges[j].selector_reg; |
| 555 | unsigned win_min = config->ranges[j].window_start; |
| 556 | unsigned win_max = win_min + |
| 557 | config->ranges[j].window_len - 1; |
| 558 | |
| 559 | if (range_cfg->range_min <= sel_reg && |
| 560 | sel_reg <= range_cfg->range_max) { |
Mark Brown | 061adc0 | 2012-10-03 12:17:51 +0100 | [diff] [blame^] | 561 | dev_err(map->dev, |
| 562 | "Range %d: selector for %d in window\n", |
| 563 | i, j); |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 564 | goto err_range; |
| 565 | } |
| 566 | |
| 567 | if (!(win_max < range_cfg->range_min || |
| 568 | win_min > range_cfg->range_max)) { |
Mark Brown | 061adc0 | 2012-10-03 12:17:51 +0100 | [diff] [blame^] | 569 | dev_err(map->dev, |
| 570 | "Range %d: window for %d in window\n", |
| 571 | i, j); |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 572 | goto err_range; |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
| 577 | if (new == NULL) { |
| 578 | ret = -ENOMEM; |
| 579 | goto err_range; |
| 580 | } |
| 581 | |
| 582 | new->range_min = range_cfg->range_min; |
| 583 | new->range_max = range_cfg->range_max; |
| 584 | new->selector_reg = range_cfg->selector_reg; |
| 585 | new->selector_mask = range_cfg->selector_mask; |
| 586 | new->selector_shift = range_cfg->selector_shift; |
| 587 | new->window_start = range_cfg->window_start; |
| 588 | new->window_len = range_cfg->window_len; |
| 589 | |
| 590 | if (_regmap_range_add(map, new) == false) { |
Mark Brown | 061adc0 | 2012-10-03 12:17:51 +0100 | [diff] [blame^] | 591 | dev_err(map->dev, "Failed to add range %d\n", i); |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 592 | kfree(new); |
| 593 | goto err_range; |
| 594 | } |
| 595 | |
| 596 | if (map->selector_work_buf == NULL) { |
| 597 | map->selector_work_buf = |
| 598 | kzalloc(map->format.buf_size, GFP_KERNEL); |
| 599 | if (map->selector_work_buf == NULL) { |
| 600 | ret = -ENOMEM; |
| 601 | goto err_range; |
| 602 | } |
| 603 | } |
| 604 | } |
Mark Brown | 052d2cd | 2011-11-21 19:05:13 +0000 | [diff] [blame] | 605 | |
Lars-Peter Clausen | e5e3b8a | 2011-11-16 16:28:16 +0100 | [diff] [blame] | 606 | ret = regcache_init(map, config); |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 607 | if (ret < 0) |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 608 | goto err_range; |
| 609 | |
| 610 | regmap_debugfs_init(map, config->name); |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 611 | |
Mark Brown | 72b39f6 | 2012-05-08 17:44:40 +0100 | [diff] [blame] | 612 | /* Add a devres resource for dev_get_regmap() */ |
| 613 | m = devres_alloc(dev_get_regmap_release, sizeof(*m), GFP_KERNEL); |
| 614 | if (!m) { |
| 615 | ret = -ENOMEM; |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 616 | goto err_debugfs; |
Mark Brown | 72b39f6 | 2012-05-08 17:44:40 +0100 | [diff] [blame] | 617 | } |
| 618 | *m = map; |
| 619 | devres_add(dev, m); |
| 620 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 621 | return map; |
| 622 | |
Stephen Warren | bfaa25f | 2012-05-23 16:30:53 -0600 | [diff] [blame] | 623 | err_debugfs: |
| 624 | regmap_debugfs_exit(map); |
Mark Brown | 72b39f6 | 2012-05-08 17:44:40 +0100 | [diff] [blame] | 625 | regcache_exit(map); |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 626 | err_range: |
| 627 | regmap_range_exit(map); |
Lars-Peter Clausen | 58072cb | 2011-11-10 18:15:15 +0100 | [diff] [blame] | 628 | kfree(map->work_buf); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 629 | err_map: |
| 630 | kfree(map); |
| 631 | err: |
| 632 | return ERR_PTR(ret); |
| 633 | } |
| 634 | EXPORT_SYMBOL_GPL(regmap_init); |
| 635 | |
Mark Brown | c0eb467 | 2012-01-30 19:56:52 +0000 | [diff] [blame] | 636 | static void devm_regmap_release(struct device *dev, void *res) |
| 637 | { |
| 638 | regmap_exit(*(struct regmap **)res); |
| 639 | } |
| 640 | |
| 641 | /** |
| 642 | * devm_regmap_init(): Initialise managed register map |
| 643 | * |
| 644 | * @dev: Device that will be interacted with |
| 645 | * @bus: Bus-specific callbacks to use with device |
Stephen Warren | 0135bbc | 2012-04-04 15:48:30 -0600 | [diff] [blame] | 646 | * @bus_context: Data passed to bus-specific callbacks |
Mark Brown | c0eb467 | 2012-01-30 19:56:52 +0000 | [diff] [blame] | 647 | * @config: Configuration for register map |
| 648 | * |
| 649 | * The return value will be an ERR_PTR() on error or a valid pointer |
| 650 | * to a struct regmap. This function should generally not be called |
| 651 | * directly, it should be called by bus-specific init functions. The |
| 652 | * map will be automatically freed by the device management code. |
| 653 | */ |
| 654 | struct regmap *devm_regmap_init(struct device *dev, |
| 655 | const struct regmap_bus *bus, |
Stephen Warren | 0135bbc | 2012-04-04 15:48:30 -0600 | [diff] [blame] | 656 | void *bus_context, |
Mark Brown | c0eb467 | 2012-01-30 19:56:52 +0000 | [diff] [blame] | 657 | const struct regmap_config *config) |
| 658 | { |
| 659 | struct regmap **ptr, *regmap; |
| 660 | |
| 661 | ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL); |
| 662 | if (!ptr) |
| 663 | return ERR_PTR(-ENOMEM); |
| 664 | |
Stephen Warren | 0135bbc | 2012-04-04 15:48:30 -0600 | [diff] [blame] | 665 | regmap = regmap_init(dev, bus, bus_context, config); |
Mark Brown | c0eb467 | 2012-01-30 19:56:52 +0000 | [diff] [blame] | 666 | if (!IS_ERR(regmap)) { |
| 667 | *ptr = regmap; |
| 668 | devres_add(dev, ptr); |
| 669 | } else { |
| 670 | devres_free(ptr); |
| 671 | } |
| 672 | |
| 673 | return regmap; |
| 674 | } |
| 675 | EXPORT_SYMBOL_GPL(devm_regmap_init); |
| 676 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 677 | /** |
Mark Brown | bf31517 | 2011-12-03 17:06:20 +0000 | [diff] [blame] | 678 | * regmap_reinit_cache(): Reinitialise the current register cache |
| 679 | * |
| 680 | * @map: Register map to operate on. |
| 681 | * @config: New configuration. Only the cache data will be used. |
| 682 | * |
| 683 | * Discard any existing register cache for the map and initialize a |
| 684 | * new cache. This can be used to restore the cache to defaults or to |
| 685 | * update the cache configuration to reflect runtime discovery of the |
| 686 | * hardware. |
Dimitris Papastamos | 4d87951 | 2012-07-27 14:54:15 +0100 | [diff] [blame] | 687 | * |
| 688 | * No explicit locking is done here, the user needs to ensure that |
| 689 | * this function will not race with other calls to regmap. |
Mark Brown | bf31517 | 2011-12-03 17:06:20 +0000 | [diff] [blame] | 690 | */ |
| 691 | int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config) |
| 692 | { |
Mark Brown | bf31517 | 2011-12-03 17:06:20 +0000 | [diff] [blame] | 693 | regcache_exit(map); |
Mark Brown | a24f64a | 2012-01-26 18:30:16 +0000 | [diff] [blame] | 694 | regmap_debugfs_exit(map); |
Mark Brown | bf31517 | 2011-12-03 17:06:20 +0000 | [diff] [blame] | 695 | |
| 696 | map->max_register = config->max_register; |
| 697 | map->writeable_reg = config->writeable_reg; |
| 698 | map->readable_reg = config->readable_reg; |
| 699 | map->volatile_reg = config->volatile_reg; |
| 700 | map->precious_reg = config->precious_reg; |
| 701 | map->cache_type = config->cache_type; |
| 702 | |
Stephen Warren | d3c242e | 2012-04-04 15:48:29 -0600 | [diff] [blame] | 703 | regmap_debugfs_init(map, config->name); |
Mark Brown | a24f64a | 2012-01-26 18:30:16 +0000 | [diff] [blame] | 704 | |
Mark Brown | 421e8d2 | 2012-01-20 13:39:37 +0000 | [diff] [blame] | 705 | map->cache_bypass = false; |
| 706 | map->cache_only = false; |
| 707 | |
Dimitris Papastamos | 4d87951 | 2012-07-27 14:54:15 +0100 | [diff] [blame] | 708 | return regcache_init(map, config); |
Mark Brown | bf31517 | 2011-12-03 17:06:20 +0000 | [diff] [blame] | 709 | } |
Mark Brown | 752a6a5 | 2012-05-14 10:00:12 +0100 | [diff] [blame] | 710 | EXPORT_SYMBOL_GPL(regmap_reinit_cache); |
Mark Brown | bf31517 | 2011-12-03 17:06:20 +0000 | [diff] [blame] | 711 | |
| 712 | /** |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 713 | * regmap_exit(): Free a previously allocated register map |
| 714 | */ |
| 715 | void regmap_exit(struct regmap *map) |
| 716 | { |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 717 | regcache_exit(map); |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 718 | regmap_debugfs_exit(map); |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 719 | regmap_range_exit(map); |
Stephen Warren | 0135bbc | 2012-04-04 15:48:30 -0600 | [diff] [blame] | 720 | if (map->bus->free_context) |
| 721 | map->bus->free_context(map->bus_context); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 722 | kfree(map->work_buf); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 723 | kfree(map); |
| 724 | } |
| 725 | EXPORT_SYMBOL_GPL(regmap_exit); |
| 726 | |
Mark Brown | 72b39f6 | 2012-05-08 17:44:40 +0100 | [diff] [blame] | 727 | static int dev_get_regmap_match(struct device *dev, void *res, void *data) |
| 728 | { |
| 729 | struct regmap **r = res; |
| 730 | if (!r || !*r) { |
| 731 | WARN_ON(!r || !*r); |
| 732 | return 0; |
| 733 | } |
| 734 | |
| 735 | /* If the user didn't specify a name match any */ |
| 736 | if (data) |
| 737 | return (*r)->name == data; |
| 738 | else |
| 739 | return 1; |
| 740 | } |
| 741 | |
| 742 | /** |
| 743 | * dev_get_regmap(): Obtain the regmap (if any) for a device |
| 744 | * |
| 745 | * @dev: Device to retrieve the map for |
| 746 | * @name: Optional name for the register map, usually NULL. |
| 747 | * |
| 748 | * Returns the regmap for the device if one is present, or NULL. If |
| 749 | * name is specified then it must match the name specified when |
| 750 | * registering the device, if it is NULL then the first regmap found |
| 751 | * will be used. Devices with multiple register maps are very rare, |
| 752 | * generic code should normally not need to specify a name. |
| 753 | */ |
| 754 | struct regmap *dev_get_regmap(struct device *dev, const char *name) |
| 755 | { |
| 756 | struct regmap **r = devres_find(dev, dev_get_regmap_release, |
| 757 | dev_get_regmap_match, (void *)name); |
| 758 | |
| 759 | if (!r) |
| 760 | return NULL; |
| 761 | return *r; |
| 762 | } |
| 763 | EXPORT_SYMBOL_GPL(dev_get_regmap); |
| 764 | |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 765 | static int _regmap_select_page(struct regmap *map, unsigned int *reg, |
| 766 | unsigned int val_num) |
| 767 | { |
| 768 | struct regmap_range_node *range; |
| 769 | void *orig_work_buf; |
| 770 | unsigned int win_offset; |
| 771 | unsigned int win_page; |
| 772 | bool page_chg; |
| 773 | int ret; |
| 774 | |
| 775 | range = _regmap_range_lookup(map, *reg); |
| 776 | if (range) { |
| 777 | win_offset = (*reg - range->range_min) % range->window_len; |
| 778 | win_page = (*reg - range->range_min) / range->window_len; |
| 779 | |
| 780 | if (val_num > 1) { |
| 781 | /* Bulk write shouldn't cross range boundary */ |
| 782 | if (*reg + val_num - 1 > range->range_max) |
| 783 | return -EINVAL; |
| 784 | |
| 785 | /* ... or single page boundary */ |
| 786 | if (val_num > range->window_len - win_offset) |
| 787 | return -EINVAL; |
| 788 | } |
| 789 | |
| 790 | /* It is possible to have selector register inside data window. |
| 791 | In that case, selector register is located on every page and |
| 792 | it needs no page switching, when accessed alone. */ |
| 793 | if (val_num > 1 || |
| 794 | range->window_start + win_offset != range->selector_reg) { |
| 795 | /* Use separate work_buf during page switching */ |
| 796 | orig_work_buf = map->work_buf; |
| 797 | map->work_buf = map->selector_work_buf; |
| 798 | |
| 799 | ret = _regmap_update_bits(map, range->selector_reg, |
| 800 | range->selector_mask, |
| 801 | win_page << range->selector_shift, |
| 802 | &page_chg); |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 803 | |
| 804 | map->work_buf = orig_work_buf; |
Krystian Garbaciak | 632a5b0 | 2012-06-18 13:04:29 +0100 | [diff] [blame] | 805 | |
| 806 | if (ret < 0) |
| 807 | return ret; |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 808 | } |
| 809 | |
| 810 | *reg = range->window_start + win_offset; |
| 811 | } |
| 812 | |
| 813 | return 0; |
| 814 | } |
| 815 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 816 | static int _regmap_raw_write(struct regmap *map, unsigned int reg, |
| 817 | const void *val, size_t val_len) |
| 818 | { |
Lars-Peter Clausen | 6f30644 | 2011-09-05 20:46:32 +0200 | [diff] [blame] | 819 | u8 *u8 = map->work_buf; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 820 | void *buf; |
| 821 | int ret = -ENOTSUPP; |
| 822 | size_t len; |
Mark Brown | 7330478 | 2011-07-24 11:46:20 +0100 | [diff] [blame] | 823 | int i; |
| 824 | |
| 825 | /* Check for unwritable registers before we start */ |
| 826 | if (map->writeable_reg) |
| 827 | for (i = 0; i < val_len / map->format.val_bytes; i++) |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 828 | if (!map->writeable_reg(map->dev, |
| 829 | reg + (i * map->reg_stride))) |
Mark Brown | 7330478 | 2011-07-24 11:46:20 +0100 | [diff] [blame] | 830 | return -EINVAL; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 831 | |
Laxman Dewangan | c915719 | 2012-02-10 21:30:27 +0530 | [diff] [blame] | 832 | if (!map->cache_bypass && map->format.parse_val) { |
| 833 | unsigned int ival; |
| 834 | int val_bytes = map->format.val_bytes; |
| 835 | for (i = 0; i < val_len / val_bytes; i++) { |
| 836 | memcpy(map->work_buf, val + (i * val_bytes), val_bytes); |
| 837 | ival = map->format.parse_val(map->work_buf); |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 838 | ret = regcache_write(map, reg + (i * map->reg_stride), |
| 839 | ival); |
Laxman Dewangan | c915719 | 2012-02-10 21:30:27 +0530 | [diff] [blame] | 840 | if (ret) { |
| 841 | dev_err(map->dev, |
| 842 | "Error in caching of register: %u ret: %d\n", |
| 843 | reg + i, ret); |
| 844 | return ret; |
| 845 | } |
| 846 | } |
| 847 | if (map->cache_only) { |
| 848 | map->cache_dirty = true; |
| 849 | return 0; |
| 850 | } |
| 851 | } |
| 852 | |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 853 | ret = _regmap_select_page(map, ®, val_len / map->format.val_bytes); |
| 854 | if (ret < 0) |
| 855 | return ret; |
| 856 | |
Marc Reilly | d939fb9 | 2012-03-16 12:11:43 +1100 | [diff] [blame] | 857 | map->format.format_reg(map->work_buf, reg, map->reg_shift); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 858 | |
Lars-Peter Clausen | 6f30644 | 2011-09-05 20:46:32 +0200 | [diff] [blame] | 859 | u8[0] |= map->write_flag_mask; |
| 860 | |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 861 | trace_regmap_hw_write_start(map->dev, reg, |
| 862 | val_len / map->format.val_bytes); |
| 863 | |
Mark Brown | 2547e20 | 2011-07-20 21:47:22 +0100 | [diff] [blame] | 864 | /* If we're doing a single register write we can probably just |
| 865 | * send the work_buf directly, otherwise try to do a gather |
| 866 | * write. |
| 867 | */ |
Mark Brown | 82159ba | 2012-01-18 10:52:25 +0000 | [diff] [blame] | 868 | if (val == (map->work_buf + map->format.pad_bytes + |
| 869 | map->format.reg_bytes)) |
Stephen Warren | 0135bbc | 2012-04-04 15:48:30 -0600 | [diff] [blame] | 870 | ret = map->bus->write(map->bus_context, map->work_buf, |
Mark Brown | 82159ba | 2012-01-18 10:52:25 +0000 | [diff] [blame] | 871 | map->format.reg_bytes + |
| 872 | map->format.pad_bytes + |
| 873 | val_len); |
Mark Brown | 2547e20 | 2011-07-20 21:47:22 +0100 | [diff] [blame] | 874 | else if (map->bus->gather_write) |
Stephen Warren | 0135bbc | 2012-04-04 15:48:30 -0600 | [diff] [blame] | 875 | ret = map->bus->gather_write(map->bus_context, map->work_buf, |
Mark Brown | 82159ba | 2012-01-18 10:52:25 +0000 | [diff] [blame] | 876 | map->format.reg_bytes + |
| 877 | map->format.pad_bytes, |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 878 | val, val_len); |
| 879 | |
Mark Brown | 2547e20 | 2011-07-20 21:47:22 +0100 | [diff] [blame] | 880 | /* If that didn't work fall back on linearising by hand. */ |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 881 | if (ret == -ENOTSUPP) { |
Mark Brown | 82159ba | 2012-01-18 10:52:25 +0000 | [diff] [blame] | 882 | len = map->format.reg_bytes + map->format.pad_bytes + val_len; |
| 883 | buf = kzalloc(len, GFP_KERNEL); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 884 | if (!buf) |
| 885 | return -ENOMEM; |
| 886 | |
| 887 | memcpy(buf, map->work_buf, map->format.reg_bytes); |
Mark Brown | 82159ba | 2012-01-18 10:52:25 +0000 | [diff] [blame] | 888 | memcpy(buf + map->format.reg_bytes + map->format.pad_bytes, |
| 889 | val, val_len); |
Stephen Warren | 0135bbc | 2012-04-04 15:48:30 -0600 | [diff] [blame] | 890 | ret = map->bus->write(map->bus_context, buf, len); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 891 | |
| 892 | kfree(buf); |
| 893 | } |
| 894 | |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 895 | trace_regmap_hw_write_done(map->dev, reg, |
| 896 | val_len / map->format.val_bytes); |
| 897 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 898 | return ret; |
| 899 | } |
| 900 | |
Dimitris Papastamos | 4d2dc09 | 2011-09-29 10:39:07 +0100 | [diff] [blame] | 901 | int _regmap_write(struct regmap *map, unsigned int reg, |
| 902 | unsigned int val) |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 903 | { |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 904 | int ret; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 905 | BUG_ON(!map->format.format_write && !map->format.format_val); |
| 906 | |
Laxman Dewangan | c915719 | 2012-02-10 21:30:27 +0530 | [diff] [blame] | 907 | if (!map->cache_bypass && map->format.format_write) { |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 908 | ret = regcache_write(map, reg, val); |
| 909 | if (ret != 0) |
| 910 | return ret; |
Mark Brown | 8ae0d7e | 2011-10-26 10:34:22 +0200 | [diff] [blame] | 911 | if (map->cache_only) { |
| 912 | map->cache_dirty = true; |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 913 | return 0; |
Mark Brown | 8ae0d7e | 2011-10-26 10:34:22 +0200 | [diff] [blame] | 914 | } |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 915 | } |
| 916 | |
Mark Brown | 1044c18 | 2012-07-06 14:10:23 +0100 | [diff] [blame] | 917 | #ifdef LOG_DEVICE |
| 918 | if (strcmp(dev_name(map->dev), LOG_DEVICE) == 0) |
| 919 | dev_info(map->dev, "%x <= %x\n", reg, val); |
| 920 | #endif |
| 921 | |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 922 | trace_regmap_reg_write(map->dev, reg, val); |
| 923 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 924 | if (map->format.format_write) { |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 925 | ret = _regmap_select_page(map, ®, 1); |
| 926 | if (ret < 0) |
| 927 | return ret; |
| 928 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 929 | map->format.format_write(map, reg, val); |
| 930 | |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 931 | trace_regmap_hw_write_start(map->dev, reg, 1); |
| 932 | |
Stephen Warren | 0135bbc | 2012-04-04 15:48:30 -0600 | [diff] [blame] | 933 | ret = map->bus->write(map->bus_context, map->work_buf, |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 934 | map->format.buf_size); |
| 935 | |
| 936 | trace_regmap_hw_write_done(map->dev, reg, 1); |
| 937 | |
| 938 | return ret; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 939 | } else { |
Mark Brown | 82159ba | 2012-01-18 10:52:25 +0000 | [diff] [blame] | 940 | map->format.format_val(map->work_buf + map->format.reg_bytes |
Marc Reilly | d939fb9 | 2012-03-16 12:11:43 +1100 | [diff] [blame] | 941 | + map->format.pad_bytes, val, 0); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 942 | return _regmap_raw_write(map, reg, |
Mark Brown | 82159ba | 2012-01-18 10:52:25 +0000 | [diff] [blame] | 943 | map->work_buf + |
| 944 | map->format.reg_bytes + |
| 945 | map->format.pad_bytes, |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 946 | map->format.val_bytes); |
| 947 | } |
| 948 | } |
| 949 | |
| 950 | /** |
| 951 | * regmap_write(): Write a value to a single register |
| 952 | * |
| 953 | * @map: Register map to write to |
| 954 | * @reg: Register to write to |
| 955 | * @val: Value to be written |
| 956 | * |
| 957 | * A value of zero will be returned on success, a negative errno will |
| 958 | * be returned in error cases. |
| 959 | */ |
| 960 | int regmap_write(struct regmap *map, unsigned int reg, unsigned int val) |
| 961 | { |
| 962 | int ret; |
| 963 | |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 964 | if (reg % map->reg_stride) |
| 965 | return -EINVAL; |
| 966 | |
Stephen Warren | bacdbe0 | 2012-04-04 15:48:28 -0600 | [diff] [blame] | 967 | map->lock(map); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 968 | |
| 969 | ret = _regmap_write(map, reg, val); |
| 970 | |
Stephen Warren | bacdbe0 | 2012-04-04 15:48:28 -0600 | [diff] [blame] | 971 | map->unlock(map); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 972 | |
| 973 | return ret; |
| 974 | } |
| 975 | EXPORT_SYMBOL_GPL(regmap_write); |
| 976 | |
| 977 | /** |
| 978 | * regmap_raw_write(): Write raw values to one or more registers |
| 979 | * |
| 980 | * @map: Register map to write to |
| 981 | * @reg: Initial register to write to |
| 982 | * @val: Block of data to be written, laid out for direct transmission to the |
| 983 | * device |
| 984 | * @val_len: Length of data pointed to by val. |
| 985 | * |
| 986 | * This function is intended to be used for things like firmware |
| 987 | * download where a large block of data needs to be transferred to the |
| 988 | * device. No formatting will be done on the data provided. |
| 989 | * |
| 990 | * A value of zero will be returned on success, a negative errno will |
| 991 | * be returned in error cases. |
| 992 | */ |
| 993 | int regmap_raw_write(struct regmap *map, unsigned int reg, |
| 994 | const void *val, size_t val_len) |
| 995 | { |
| 996 | int ret; |
| 997 | |
Stephen Warren | 851960b | 2012-04-06 15:16:03 -0600 | [diff] [blame] | 998 | if (val_len % map->format.val_bytes) |
| 999 | return -EINVAL; |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 1000 | if (reg % map->reg_stride) |
| 1001 | return -EINVAL; |
Stephen Warren | 851960b | 2012-04-06 15:16:03 -0600 | [diff] [blame] | 1002 | |
Stephen Warren | bacdbe0 | 2012-04-04 15:48:28 -0600 | [diff] [blame] | 1003 | map->lock(map); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1004 | |
| 1005 | ret = _regmap_raw_write(map, reg, val, val_len); |
| 1006 | |
Stephen Warren | bacdbe0 | 2012-04-04 15:48:28 -0600 | [diff] [blame] | 1007 | map->unlock(map); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1008 | |
| 1009 | return ret; |
| 1010 | } |
| 1011 | EXPORT_SYMBOL_GPL(regmap_raw_write); |
| 1012 | |
Laxman Dewangan | 8eaeb21 | 2012-02-12 19:49:43 +0530 | [diff] [blame] | 1013 | /* |
| 1014 | * regmap_bulk_write(): Write multiple registers to the device |
| 1015 | * |
| 1016 | * @map: Register map to write to |
| 1017 | * @reg: First register to be write from |
| 1018 | * @val: Block of data to be written, in native register size for device |
| 1019 | * @val_count: Number of registers to write |
| 1020 | * |
| 1021 | * This function is intended to be used for writing a large block of |
| 1022 | * data to be device either in single transfer or multiple transfer. |
| 1023 | * |
| 1024 | * A value of zero will be returned on success, a negative errno will |
| 1025 | * be returned in error cases. |
| 1026 | */ |
| 1027 | int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val, |
| 1028 | size_t val_count) |
| 1029 | { |
| 1030 | int ret = 0, i; |
| 1031 | size_t val_bytes = map->format.val_bytes; |
| 1032 | void *wval; |
| 1033 | |
| 1034 | if (!map->format.parse_val) |
| 1035 | return -EINVAL; |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 1036 | if (reg % map->reg_stride) |
| 1037 | return -EINVAL; |
Laxman Dewangan | 8eaeb21 | 2012-02-12 19:49:43 +0530 | [diff] [blame] | 1038 | |
Stephen Warren | bacdbe0 | 2012-04-04 15:48:28 -0600 | [diff] [blame] | 1039 | map->lock(map); |
Laxman Dewangan | 8eaeb21 | 2012-02-12 19:49:43 +0530 | [diff] [blame] | 1040 | |
| 1041 | /* No formatting is require if val_byte is 1 */ |
| 1042 | if (val_bytes == 1) { |
| 1043 | wval = (void *)val; |
| 1044 | } else { |
| 1045 | wval = kmemdup(val, val_count * val_bytes, GFP_KERNEL); |
| 1046 | if (!wval) { |
| 1047 | ret = -ENOMEM; |
| 1048 | dev_err(map->dev, "Error in memory allocation\n"); |
| 1049 | goto out; |
| 1050 | } |
| 1051 | for (i = 0; i < val_count * val_bytes; i += val_bytes) |
| 1052 | map->format.parse_val(wval + i); |
| 1053 | } |
Ashish Jangam | 2e33caf | 2012-04-30 23:23:40 +0100 | [diff] [blame] | 1054 | /* |
| 1055 | * Some devices does not support bulk write, for |
| 1056 | * them we have a series of single write operations. |
| 1057 | */ |
| 1058 | if (map->use_single_rw) { |
| 1059 | for (i = 0; i < val_count; i++) { |
| 1060 | ret = regmap_raw_write(map, |
| 1061 | reg + (i * map->reg_stride), |
| 1062 | val + (i * val_bytes), |
| 1063 | val_bytes); |
| 1064 | if (ret != 0) |
| 1065 | return ret; |
| 1066 | } |
| 1067 | } else { |
| 1068 | ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count); |
| 1069 | } |
Laxman Dewangan | 8eaeb21 | 2012-02-12 19:49:43 +0530 | [diff] [blame] | 1070 | |
| 1071 | if (val_bytes != 1) |
| 1072 | kfree(wval); |
| 1073 | |
| 1074 | out: |
Stephen Warren | bacdbe0 | 2012-04-04 15:48:28 -0600 | [diff] [blame] | 1075 | map->unlock(map); |
Laxman Dewangan | 8eaeb21 | 2012-02-12 19:49:43 +0530 | [diff] [blame] | 1076 | return ret; |
| 1077 | } |
| 1078 | EXPORT_SYMBOL_GPL(regmap_bulk_write); |
| 1079 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1080 | static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val, |
| 1081 | unsigned int val_len) |
| 1082 | { |
| 1083 | u8 *u8 = map->work_buf; |
| 1084 | int ret; |
| 1085 | |
Krystian Garbaciak | 6863ca6 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 1086 | ret = _regmap_select_page(map, ®, val_len / map->format.val_bytes); |
| 1087 | if (ret < 0) |
| 1088 | return ret; |
| 1089 | |
Marc Reilly | d939fb9 | 2012-03-16 12:11:43 +1100 | [diff] [blame] | 1090 | map->format.format_reg(map->work_buf, reg, map->reg_shift); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1091 | |
| 1092 | /* |
Lars-Peter Clausen | 6f30644 | 2011-09-05 20:46:32 +0200 | [diff] [blame] | 1093 | * 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] | 1094 | * register addresss; since it's always the high bits for all |
| 1095 | * current formats we can do this here rather than in |
| 1096 | * formatting. This may break if we get interesting formats. |
| 1097 | */ |
Lars-Peter Clausen | 6f30644 | 2011-09-05 20:46:32 +0200 | [diff] [blame] | 1098 | u8[0] |= map->read_flag_mask; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1099 | |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 1100 | trace_regmap_hw_read_start(map->dev, reg, |
| 1101 | val_len / map->format.val_bytes); |
| 1102 | |
Stephen Warren | 0135bbc | 2012-04-04 15:48:30 -0600 | [diff] [blame] | 1103 | ret = map->bus->read(map->bus_context, map->work_buf, |
Mark Brown | 82159ba | 2012-01-18 10:52:25 +0000 | [diff] [blame] | 1104 | map->format.reg_bytes + map->format.pad_bytes, |
Mark Brown | 40c5cc2 | 2011-07-24 22:39:12 +0100 | [diff] [blame] | 1105 | val, val_len); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1106 | |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 1107 | trace_regmap_hw_read_done(map->dev, reg, |
| 1108 | val_len / map->format.val_bytes); |
| 1109 | |
| 1110 | return ret; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1111 | } |
| 1112 | |
| 1113 | static int _regmap_read(struct regmap *map, unsigned int reg, |
| 1114 | unsigned int *val) |
| 1115 | { |
| 1116 | int ret; |
| 1117 | |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 1118 | if (!map->cache_bypass) { |
| 1119 | ret = regcache_read(map, reg, val); |
| 1120 | if (ret == 0) |
| 1121 | return 0; |
| 1122 | } |
| 1123 | |
Lars-Peter Clausen | 1925441 | 2011-11-16 16:28:19 +0100 | [diff] [blame] | 1124 | if (!map->format.parse_val) |
| 1125 | return -EINVAL; |
| 1126 | |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 1127 | if (map->cache_only) |
| 1128 | return -EBUSY; |
| 1129 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1130 | 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] | 1131 | if (ret == 0) { |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1132 | *val = map->format.parse_val(map->work_buf); |
Mark Brown | 1044c18 | 2012-07-06 14:10:23 +0100 | [diff] [blame] | 1133 | |
| 1134 | #ifdef LOG_DEVICE |
| 1135 | if (strcmp(dev_name(map->dev), LOG_DEVICE) == 0) |
| 1136 | dev_info(map->dev, "%x => %x\n", reg, *val); |
| 1137 | #endif |
| 1138 | |
Mark Brown | fb2736b | 2011-07-24 21:30:55 +0100 | [diff] [blame] | 1139 | trace_regmap_reg_read(map->dev, reg, *val); |
| 1140 | } |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1141 | |
Mark Brown | f298536 | 2012-04-30 21:25:05 +0100 | [diff] [blame] | 1142 | if (ret == 0 && !map->cache_bypass) |
| 1143 | regcache_write(map, reg, *val); |
| 1144 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1145 | return ret; |
| 1146 | } |
| 1147 | |
| 1148 | /** |
| 1149 | * regmap_read(): Read a value from a single register |
| 1150 | * |
| 1151 | * @map: Register map to write to |
| 1152 | * @reg: Register to be read from |
| 1153 | * @val: Pointer to store read value |
| 1154 | * |
| 1155 | * A value of zero will be returned on success, a negative errno will |
| 1156 | * be returned in error cases. |
| 1157 | */ |
| 1158 | int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val) |
| 1159 | { |
| 1160 | int ret; |
| 1161 | |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 1162 | if (reg % map->reg_stride) |
| 1163 | return -EINVAL; |
| 1164 | |
Stephen Warren | bacdbe0 | 2012-04-04 15:48:28 -0600 | [diff] [blame] | 1165 | map->lock(map); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1166 | |
| 1167 | ret = _regmap_read(map, reg, val); |
| 1168 | |
Stephen Warren | bacdbe0 | 2012-04-04 15:48:28 -0600 | [diff] [blame] | 1169 | map->unlock(map); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1170 | |
| 1171 | return ret; |
| 1172 | } |
| 1173 | EXPORT_SYMBOL_GPL(regmap_read); |
| 1174 | |
| 1175 | /** |
| 1176 | * regmap_raw_read(): Read raw data from the device |
| 1177 | * |
| 1178 | * @map: Register map to write to |
| 1179 | * @reg: First register to be read from |
| 1180 | * @val: Pointer to store read value |
| 1181 | * @val_len: Size of data to read |
| 1182 | * |
| 1183 | * A value of zero will be returned on success, a negative errno will |
| 1184 | * be returned in error cases. |
| 1185 | */ |
| 1186 | int regmap_raw_read(struct regmap *map, unsigned int reg, void *val, |
| 1187 | size_t val_len) |
| 1188 | { |
Mark Brown | b8fb5ab | 2012-02-21 19:12:47 +0000 | [diff] [blame] | 1189 | size_t val_bytes = map->format.val_bytes; |
| 1190 | size_t val_count = val_len / val_bytes; |
| 1191 | unsigned int v; |
| 1192 | int ret, i; |
Mark Brown | 04e016a | 2011-10-09 13:35:43 +0100 | [diff] [blame] | 1193 | |
Stephen Warren | 851960b | 2012-04-06 15:16:03 -0600 | [diff] [blame] | 1194 | if (val_len % map->format.val_bytes) |
| 1195 | return -EINVAL; |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 1196 | if (reg % map->reg_stride) |
| 1197 | return -EINVAL; |
Stephen Warren | 851960b | 2012-04-06 15:16:03 -0600 | [diff] [blame] | 1198 | |
Stephen Warren | bacdbe0 | 2012-04-04 15:48:28 -0600 | [diff] [blame] | 1199 | map->lock(map); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1200 | |
Mark Brown | b8fb5ab | 2012-02-21 19:12:47 +0000 | [diff] [blame] | 1201 | if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass || |
| 1202 | map->cache_type == REGCACHE_NONE) { |
| 1203 | /* Physical block read if there's no cache involved */ |
| 1204 | ret = _regmap_raw_read(map, reg, val, val_len); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1205 | |
Mark Brown | b8fb5ab | 2012-02-21 19:12:47 +0000 | [diff] [blame] | 1206 | } else { |
| 1207 | /* Otherwise go word by word for the cache; should be low |
| 1208 | * cost as we expect to hit the cache. |
| 1209 | */ |
| 1210 | for (i = 0; i < val_count; i++) { |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 1211 | ret = _regmap_read(map, reg + (i * map->reg_stride), |
| 1212 | &v); |
Mark Brown | b8fb5ab | 2012-02-21 19:12:47 +0000 | [diff] [blame] | 1213 | if (ret != 0) |
| 1214 | goto out; |
| 1215 | |
Marc Reilly | d939fb9 | 2012-03-16 12:11:43 +1100 | [diff] [blame] | 1216 | map->format.format_val(val + (i * val_bytes), v, 0); |
Mark Brown | b8fb5ab | 2012-02-21 19:12:47 +0000 | [diff] [blame] | 1217 | } |
| 1218 | } |
| 1219 | |
| 1220 | out: |
Stephen Warren | bacdbe0 | 2012-04-04 15:48:28 -0600 | [diff] [blame] | 1221 | map->unlock(map); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1222 | |
| 1223 | return ret; |
| 1224 | } |
| 1225 | EXPORT_SYMBOL_GPL(regmap_raw_read); |
| 1226 | |
| 1227 | /** |
| 1228 | * regmap_bulk_read(): Read multiple registers from the device |
| 1229 | * |
| 1230 | * @map: Register map to write to |
| 1231 | * @reg: First register to be read from |
| 1232 | * @val: Pointer to store read value, in native register size for device |
| 1233 | * @val_count: Number of registers to read |
| 1234 | * |
| 1235 | * A value of zero will be returned on success, a negative errno will |
| 1236 | * be returned in error cases. |
| 1237 | */ |
| 1238 | int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val, |
| 1239 | size_t val_count) |
| 1240 | { |
| 1241 | int ret, i; |
| 1242 | size_t val_bytes = map->format.val_bytes; |
Lars-Peter Clausen | 82cd996 | 2011-11-08 18:37:25 +0100 | [diff] [blame] | 1243 | bool vol = regmap_volatile_range(map, reg, val_count); |
Dimitris Papastamos | 5d1729e | 2011-09-19 14:34:05 +0100 | [diff] [blame] | 1244 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1245 | if (!map->format.parse_val) |
| 1246 | return -EINVAL; |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 1247 | if (reg % map->reg_stride) |
| 1248 | return -EINVAL; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1249 | |
Mark Brown | de2d808 | 2011-10-10 13:24:52 +0100 | [diff] [blame] | 1250 | if (vol || map->cache_type == REGCACHE_NONE) { |
Ashish Jangam | 2e33caf | 2012-04-30 23:23:40 +0100 | [diff] [blame] | 1251 | /* |
| 1252 | * Some devices does not support bulk read, for |
| 1253 | * them we have a series of single read operations. |
| 1254 | */ |
| 1255 | if (map->use_single_rw) { |
| 1256 | for (i = 0; i < val_count; i++) { |
| 1257 | ret = regmap_raw_read(map, |
| 1258 | reg + (i * map->reg_stride), |
| 1259 | val + (i * val_bytes), |
| 1260 | val_bytes); |
| 1261 | if (ret != 0) |
| 1262 | return ret; |
| 1263 | } |
| 1264 | } else { |
| 1265 | ret = regmap_raw_read(map, reg, val, |
| 1266 | val_bytes * val_count); |
| 1267 | if (ret != 0) |
| 1268 | return ret; |
| 1269 | } |
Mark Brown | de2d808 | 2011-10-10 13:24:52 +0100 | [diff] [blame] | 1270 | |
| 1271 | for (i = 0; i < val_count * val_bytes; i += val_bytes) |
| 1272 | map->format.parse_val(val + i); |
| 1273 | } else { |
| 1274 | for (i = 0; i < val_count; i++) { |
Laxman Dewangan | 6560ffd | 2012-05-09 17:43:12 +0530 | [diff] [blame] | 1275 | unsigned int ival; |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 1276 | ret = regmap_read(map, reg + (i * map->reg_stride), |
Mark Brown | 25061d2 | 2012-05-12 13:06:08 +0100 | [diff] [blame] | 1277 | &ival); |
Mark Brown | de2d808 | 2011-10-10 13:24:52 +0100 | [diff] [blame] | 1278 | if (ret != 0) |
| 1279 | return ret; |
Laxman Dewangan | 6560ffd | 2012-05-09 17:43:12 +0530 | [diff] [blame] | 1280 | memcpy(val + (i * val_bytes), &ival, val_bytes); |
Mark Brown | de2d808 | 2011-10-10 13:24:52 +0100 | [diff] [blame] | 1281 | } |
| 1282 | } |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1283 | |
| 1284 | return 0; |
| 1285 | } |
| 1286 | EXPORT_SYMBOL_GPL(regmap_bulk_read); |
| 1287 | |
Mark Brown | 018690d | 2011-11-29 20:10:36 +0000 | [diff] [blame] | 1288 | static int _regmap_update_bits(struct regmap *map, unsigned int reg, |
| 1289 | unsigned int mask, unsigned int val, |
| 1290 | bool *change) |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1291 | { |
| 1292 | int ret; |
Mark Brown | d91e8db | 2011-11-18 16:03:50 +0000 | [diff] [blame] | 1293 | unsigned int tmp, orig; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1294 | |
Mark Brown | d91e8db | 2011-11-18 16:03:50 +0000 | [diff] [blame] | 1295 | ret = _regmap_read(map, reg, &orig); |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1296 | if (ret != 0) |
Krystian Garbaciak | fc3ebd7 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 1297 | return ret; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1298 | |
Mark Brown | d91e8db | 2011-11-18 16:03:50 +0000 | [diff] [blame] | 1299 | tmp = orig & ~mask; |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1300 | tmp |= val & mask; |
| 1301 | |
Mark Brown | 018690d | 2011-11-29 20:10:36 +0000 | [diff] [blame] | 1302 | if (tmp != orig) { |
Mark Brown | d91e8db | 2011-11-18 16:03:50 +0000 | [diff] [blame] | 1303 | ret = _regmap_write(map, reg, tmp); |
Mark Brown | 018690d | 2011-11-29 20:10:36 +0000 | [diff] [blame] | 1304 | *change = true; |
| 1305 | } else { |
| 1306 | *change = false; |
| 1307 | } |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1308 | |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1309 | return ret; |
| 1310 | } |
Mark Brown | 018690d | 2011-11-29 20:10:36 +0000 | [diff] [blame] | 1311 | |
| 1312 | /** |
| 1313 | * regmap_update_bits: Perform a read/modify/write cycle on the register map |
| 1314 | * |
| 1315 | * @map: Register map to update |
| 1316 | * @reg: Register to update |
| 1317 | * @mask: Bitmask to change |
| 1318 | * @val: New value for bitmask |
| 1319 | * |
| 1320 | * Returns zero for success, a negative number on error. |
| 1321 | */ |
| 1322 | int regmap_update_bits(struct regmap *map, unsigned int reg, |
| 1323 | unsigned int mask, unsigned int val) |
| 1324 | { |
| 1325 | bool change; |
Krystian Garbaciak | fc3ebd7 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 1326 | int ret; |
| 1327 | |
| 1328 | map->lock(map); |
| 1329 | ret = _regmap_update_bits(map, reg, mask, val, &change); |
| 1330 | map->unlock(map); |
| 1331 | |
| 1332 | return ret; |
Mark Brown | 018690d | 2011-11-29 20:10:36 +0000 | [diff] [blame] | 1333 | } |
Mark Brown | b83a313 | 2011-05-11 19:59:58 +0200 | [diff] [blame] | 1334 | EXPORT_SYMBOL_GPL(regmap_update_bits); |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 1335 | |
Mark Brown | 018690d | 2011-11-29 20:10:36 +0000 | [diff] [blame] | 1336 | /** |
| 1337 | * regmap_update_bits_check: Perform a read/modify/write cycle on the |
| 1338 | * register map and report if updated |
| 1339 | * |
| 1340 | * @map: Register map to update |
| 1341 | * @reg: Register to update |
| 1342 | * @mask: Bitmask to change |
| 1343 | * @val: New value for bitmask |
| 1344 | * @change: Boolean indicating if a write was done |
| 1345 | * |
| 1346 | * Returns zero for success, a negative number on error. |
| 1347 | */ |
| 1348 | int regmap_update_bits_check(struct regmap *map, unsigned int reg, |
| 1349 | unsigned int mask, unsigned int val, |
| 1350 | bool *change) |
| 1351 | { |
Krystian Garbaciak | fc3ebd7 | 2012-06-15 11:23:56 +0100 | [diff] [blame] | 1352 | int ret; |
| 1353 | |
| 1354 | map->lock(map); |
| 1355 | ret = _regmap_update_bits(map, reg, mask, val, change); |
| 1356 | map->unlock(map); |
| 1357 | return ret; |
Mark Brown | 018690d | 2011-11-29 20:10:36 +0000 | [diff] [blame] | 1358 | } |
| 1359 | EXPORT_SYMBOL_GPL(regmap_update_bits_check); |
| 1360 | |
Mark Brown | 22f0d90 | 2012-01-21 12:01:14 +0000 | [diff] [blame] | 1361 | /** |
| 1362 | * regmap_register_patch: Register and apply register updates to be applied |
| 1363 | * on device initialistion |
| 1364 | * |
| 1365 | * @map: Register map to apply updates to. |
| 1366 | * @regs: Values to update. |
| 1367 | * @num_regs: Number of entries in regs. |
| 1368 | * |
| 1369 | * Register a set of register updates to be applied to the device |
| 1370 | * whenever the device registers are synchronised with the cache and |
| 1371 | * apply them immediately. Typically this is used to apply |
| 1372 | * corrections to be applied to the device defaults on startup, such |
| 1373 | * as the updates some vendors provide to undocumented registers. |
| 1374 | */ |
| 1375 | int regmap_register_patch(struct regmap *map, const struct reg_default *regs, |
| 1376 | int num_regs) |
| 1377 | { |
| 1378 | int i, ret; |
| 1379 | bool bypass; |
| 1380 | |
| 1381 | /* If needed the implementation can be extended to support this */ |
| 1382 | if (map->patch) |
| 1383 | return -EBUSY; |
| 1384 | |
Stephen Warren | bacdbe0 | 2012-04-04 15:48:28 -0600 | [diff] [blame] | 1385 | map->lock(map); |
Mark Brown | 22f0d90 | 2012-01-21 12:01:14 +0000 | [diff] [blame] | 1386 | |
| 1387 | bypass = map->cache_bypass; |
| 1388 | |
| 1389 | map->cache_bypass = true; |
| 1390 | |
| 1391 | /* Write out first; it's useful to apply even if we fail later. */ |
| 1392 | for (i = 0; i < num_regs; i++) { |
| 1393 | ret = _regmap_write(map, regs[i].reg, regs[i].def); |
| 1394 | if (ret != 0) { |
| 1395 | dev_err(map->dev, "Failed to write %x = %x: %d\n", |
| 1396 | regs[i].reg, regs[i].def, ret); |
| 1397 | goto out; |
| 1398 | } |
| 1399 | } |
| 1400 | |
Axel Lin | 2a14d7d9 | 2012-02-10 19:29:55 +0800 | [diff] [blame] | 1401 | map->patch = kcalloc(num_regs, sizeof(struct reg_default), GFP_KERNEL); |
Mark Brown | 22f0d90 | 2012-01-21 12:01:14 +0000 | [diff] [blame] | 1402 | if (map->patch != NULL) { |
| 1403 | memcpy(map->patch, regs, |
| 1404 | num_regs * sizeof(struct reg_default)); |
| 1405 | map->patch_regs = num_regs; |
| 1406 | } else { |
| 1407 | ret = -ENOMEM; |
| 1408 | } |
| 1409 | |
| 1410 | out: |
| 1411 | map->cache_bypass = bypass; |
| 1412 | |
Stephen Warren | bacdbe0 | 2012-04-04 15:48:28 -0600 | [diff] [blame] | 1413 | map->unlock(map); |
Mark Brown | 22f0d90 | 2012-01-21 12:01:14 +0000 | [diff] [blame] | 1414 | |
| 1415 | return ret; |
| 1416 | } |
| 1417 | EXPORT_SYMBOL_GPL(regmap_register_patch); |
| 1418 | |
Mark Brown | eae4b51 | 2012-03-14 13:15:03 +0000 | [diff] [blame] | 1419 | /* |
Mark Brown | a6539c3 | 2012-02-17 14:20:14 -0800 | [diff] [blame] | 1420 | * regmap_get_val_bytes(): Report the size of a register value |
| 1421 | * |
| 1422 | * Report the size of a register value, mainly intended to for use by |
| 1423 | * generic infrastructure built on top of regmap. |
| 1424 | */ |
| 1425 | int regmap_get_val_bytes(struct regmap *map) |
| 1426 | { |
| 1427 | if (map->format.format_write) |
| 1428 | return -EINVAL; |
| 1429 | |
| 1430 | return map->format.val_bytes; |
| 1431 | } |
| 1432 | EXPORT_SYMBOL_GPL(regmap_get_val_bytes); |
| 1433 | |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 1434 | static int __init regmap_initcall(void) |
| 1435 | { |
| 1436 | regmap_debugfs_initcall(); |
| 1437 | |
| 1438 | return 0; |
| 1439 | } |
| 1440 | postcore_initcall(regmap_initcall); |