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