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