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