blob: baf9586b4fd8d230772b50efb099123524717218 [file] [log] [blame]
Mark Brownb83a3132011-05-11 19:59:58 +02001/*
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 Warrenf5d6eba2012-03-09 13:17:28 -070013#include <linux/device.h>
Mark Brownb83a3132011-05-11 19:59:58 +020014#include <linux/slab.h>
Paul Gortmaker19694b52012-02-28 19:28:02 -050015#include <linux/export.h>
Mark Brownb83a3132011-05-11 19:59:58 +020016#include <linux/mutex.h>
17#include <linux/err.h>
Krystian Garbaciak6863ca62012-06-15 11:23:56 +010018#include <linux/rbtree.h>
Mark Brownb83a3132011-05-11 19:59:58 +020019
Mark Brownfb2736b2011-07-24 21:30:55 +010020#define CREATE_TRACE_POINTS
21#include <trace/events/regmap.h>
22
Mark Brown93de9122011-07-20 22:35:37 +010023#include "internal.h"
Mark Brownb83a3132011-05-11 19:59:58 +020024
Mark Brown1044c182012-07-06 14:10:23 +010025/*
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
33static int _regmap_update_bits(struct regmap *map, unsigned int reg,
34 unsigned int mask, unsigned int val,
35 bool *change);
36
Mark Brown8de2f082011-08-10 17:14:41 +090037bool regmap_writeable(struct regmap *map, unsigned int reg)
38{
39 if (map->max_register && reg > map->max_register)
40 return false;
41
42 if (map->writeable_reg)
43 return map->writeable_reg(map->dev, reg);
44
45 return true;
46}
47
48bool regmap_readable(struct regmap *map, unsigned int reg)
49{
50 if (map->max_register && reg > map->max_register)
51 return false;
52
Wolfram Sang4191f192012-01-30 15:08:16 +010053 if (map->format.format_write)
54 return false;
55
Mark Brown8de2f082011-08-10 17:14:41 +090056 if (map->readable_reg)
57 return map->readable_reg(map->dev, reg);
58
59 return true;
60}
61
62bool regmap_volatile(struct regmap *map, unsigned int reg)
63{
Wolfram Sang4191f192012-01-30 15:08:16 +010064 if (!regmap_readable(map, reg))
Mark Brown8de2f082011-08-10 17:14:41 +090065 return false;
66
67 if (map->volatile_reg)
68 return map->volatile_reg(map->dev, reg);
69
70 return true;
71}
72
73bool regmap_precious(struct regmap *map, unsigned int reg)
74{
Wolfram Sang4191f192012-01-30 15:08:16 +010075 if (!regmap_readable(map, reg))
Mark Brown8de2f082011-08-10 17:14:41 +090076 return false;
77
78 if (map->precious_reg)
79 return map->precious_reg(map->dev, reg);
80
81 return false;
82}
83
Lars-Peter Clausen82cd9962011-11-08 18:37:25 +010084static bool regmap_volatile_range(struct regmap *map, unsigned int reg,
85 unsigned int num)
86{
87 unsigned int i;
88
89 for (i = 0; i < num; i++)
90 if (!regmap_volatile(map, reg + i))
91 return false;
92
93 return true;
94}
95
Wolfram Sang9aa50752012-01-27 16:10:22 +010096static void regmap_format_2_6_write(struct regmap *map,
97 unsigned int reg, unsigned int val)
98{
99 u8 *out = map->work_buf;
100
101 *out = (reg << 6) | val;
102}
103
Mark Brownb83a3132011-05-11 19:59:58 +0200104static void regmap_format_4_12_write(struct regmap *map,
105 unsigned int reg, unsigned int val)
106{
107 __be16 *out = map->work_buf;
108 *out = cpu_to_be16((reg << 12) | val);
109}
110
111static void regmap_format_7_9_write(struct regmap *map,
112 unsigned int reg, unsigned int val)
113{
114 __be16 *out = map->work_buf;
115 *out = cpu_to_be16((reg << 9) | val);
116}
117
Lars-Peter Clausen7e5ec632011-11-16 16:28:21 +0100118static void regmap_format_10_14_write(struct regmap *map,
119 unsigned int reg, unsigned int val)
120{
121 u8 *out = map->work_buf;
122
123 out[2] = val;
124 out[1] = (val >> 8) | (reg << 6);
125 out[0] = reg >> 2;
126}
127
Marc Reillyd939fb92012-03-16 12:11:43 +1100128static void regmap_format_8(void *buf, unsigned int val, unsigned int shift)
Mark Brownb83a3132011-05-11 19:59:58 +0200129{
130 u8 *b = buf;
131
Marc Reillyd939fb92012-03-16 12:11:43 +1100132 b[0] = val << shift;
Mark Brownb83a3132011-05-11 19:59:58 +0200133}
134
Stephen Warren141eba22012-05-24 10:47:26 -0600135static void regmap_format_16_be(void *buf, unsigned int val, unsigned int shift)
Mark Brownb83a3132011-05-11 19:59:58 +0200136{
137 __be16 *b = buf;
138
Marc Reillyd939fb92012-03-16 12:11:43 +1100139 b[0] = cpu_to_be16(val << shift);
Mark Brownb83a3132011-05-11 19:59:58 +0200140}
141
Stephen Warren141eba22012-05-24 10:47:26 -0600142static void regmap_format_16_native(void *buf, unsigned int val,
143 unsigned int shift)
144{
145 *(u16 *)buf = val << shift;
146}
147
Marc Reillyd939fb92012-03-16 12:11:43 +1100148static void regmap_format_24(void *buf, unsigned int val, unsigned int shift)
Marc Reillyea279fc2012-03-16 12:11:42 +1100149{
150 u8 *b = buf;
151
Marc Reillyd939fb92012-03-16 12:11:43 +1100152 val <<= shift;
153
Marc Reillyea279fc2012-03-16 12:11:42 +1100154 b[0] = val >> 16;
155 b[1] = val >> 8;
156 b[2] = val;
157}
158
Stephen Warren141eba22012-05-24 10:47:26 -0600159static void regmap_format_32_be(void *buf, unsigned int val, unsigned int shift)
Mark Brown7d5e5252012-02-17 15:58:25 -0800160{
161 __be32 *b = buf;
162
Marc Reillyd939fb92012-03-16 12:11:43 +1100163 b[0] = cpu_to_be32(val << shift);
Mark Brown7d5e5252012-02-17 15:58:25 -0800164}
165
Stephen Warren141eba22012-05-24 10:47:26 -0600166static void regmap_format_32_native(void *buf, unsigned int val,
167 unsigned int shift)
168{
169 *(u32 *)buf = val << shift;
170}
171
Mark Brownb83a3132011-05-11 19:59:58 +0200172static unsigned int regmap_parse_8(void *buf)
173{
174 u8 *b = buf;
175
176 return b[0];
177}
178
Stephen Warren141eba22012-05-24 10:47:26 -0600179static unsigned int regmap_parse_16_be(void *buf)
Mark Brownb83a3132011-05-11 19:59:58 +0200180{
181 __be16 *b = buf;
182
183 b[0] = be16_to_cpu(b[0]);
184
185 return b[0];
186}
187
Stephen Warren141eba22012-05-24 10:47:26 -0600188static unsigned int regmap_parse_16_native(void *buf)
189{
190 return *(u16 *)buf;
191}
192
Marc Reillyea279fc2012-03-16 12:11:42 +1100193static unsigned int regmap_parse_24(void *buf)
194{
195 u8 *b = buf;
196 unsigned int ret = b[2];
197 ret |= ((unsigned int)b[1]) << 8;
198 ret |= ((unsigned int)b[0]) << 16;
199
200 return ret;
201}
202
Stephen Warren141eba22012-05-24 10:47:26 -0600203static unsigned int regmap_parse_32_be(void *buf)
Mark Brown7d5e5252012-02-17 15:58:25 -0800204{
205 __be32 *b = buf;
206
207 b[0] = be32_to_cpu(b[0]);
208
209 return b[0];
210}
211
Stephen Warren141eba22012-05-24 10:47:26 -0600212static unsigned int regmap_parse_32_native(void *buf)
213{
214 return *(u32 *)buf;
215}
216
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600217static void regmap_lock_mutex(struct regmap *map)
218{
219 mutex_lock(&map->mutex);
220}
221
222static void regmap_unlock_mutex(struct regmap *map)
223{
224 mutex_unlock(&map->mutex);
225}
226
227static void regmap_lock_spinlock(struct regmap *map)
228{
229 spin_lock(&map->spinlock);
230}
231
232static void regmap_unlock_spinlock(struct regmap *map)
233{
234 spin_unlock(&map->spinlock);
235}
236
Mark Brown72b39f62012-05-08 17:44:40 +0100237static void dev_get_regmap_release(struct device *dev, void *res)
238{
239 /*
240 * We don't actually have anything to do here; the goal here
241 * is not to manage the regmap but to provide a simple way to
242 * get the regmap back given a struct device.
243 */
244}
245
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100246static bool _regmap_range_add(struct regmap *map,
247 struct regmap_range_node *data)
248{
249 struct rb_root *root = &map->range_tree;
250 struct rb_node **new = &(root->rb_node), *parent = NULL;
251
252 while (*new) {
253 struct regmap_range_node *this =
254 container_of(*new, struct regmap_range_node, node);
255
256 parent = *new;
257 if (data->range_max < this->range_min)
258 new = &((*new)->rb_left);
259 else if (data->range_min > this->range_max)
260 new = &((*new)->rb_right);
261 else
262 return false;
263 }
264
265 rb_link_node(&data->node, parent, new);
266 rb_insert_color(&data->node, root);
267
268 return true;
269}
270
271static struct regmap_range_node *_regmap_range_lookup(struct regmap *map,
272 unsigned int reg)
273{
274 struct rb_node *node = map->range_tree.rb_node;
275
276 while (node) {
277 struct regmap_range_node *this =
278 container_of(node, struct regmap_range_node, node);
279
280 if (reg < this->range_min)
281 node = node->rb_left;
282 else if (reg > this->range_max)
283 node = node->rb_right;
284 else
285 return this;
286 }
287
288 return NULL;
289}
290
291static void regmap_range_exit(struct regmap *map)
292{
293 struct rb_node *next;
294 struct regmap_range_node *range_node;
295
296 next = rb_first(&map->range_tree);
297 while (next) {
298 range_node = rb_entry(next, struct regmap_range_node, node);
299 next = rb_next(&range_node->node);
300 rb_erase(&range_node->node, &map->range_tree);
301 kfree(range_node);
302 }
303
304 kfree(map->selector_work_buf);
305}
306
Mark Brownb83a3132011-05-11 19:59:58 +0200307/**
308 * regmap_init(): Initialise register map
309 *
310 * @dev: Device that will be interacted with
311 * @bus: Bus-specific callbacks to use with device
Stephen Warren0135bbc2012-04-04 15:48:30 -0600312 * @bus_context: Data passed to bus-specific callbacks
Mark Brownb83a3132011-05-11 19:59:58 +0200313 * @config: Configuration for register map
314 *
315 * The return value will be an ERR_PTR() on error or a valid pointer to
316 * a struct regmap. This function should generally not be called
317 * directly, it should be called by bus-specific init functions.
318 */
319struct regmap *regmap_init(struct device *dev,
320 const struct regmap_bus *bus,
Stephen Warren0135bbc2012-04-04 15:48:30 -0600321 void *bus_context,
Mark Brownb83a3132011-05-11 19:59:58 +0200322 const struct regmap_config *config)
323{
Mark Brown72b39f62012-05-08 17:44:40 +0100324 struct regmap *map, **m;
Mark Brownb83a3132011-05-11 19:59:58 +0200325 int ret = -EINVAL;
Stephen Warren141eba22012-05-24 10:47:26 -0600326 enum regmap_endian reg_endian, val_endian;
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100327 int i, j;
Mark Brownb83a3132011-05-11 19:59:58 +0200328
329 if (!bus || !config)
Lars-Peter Clausenabbb18f2011-11-14 10:40:15 +0100330 goto err;
Mark Brownb83a3132011-05-11 19:59:58 +0200331
332 map = kzalloc(sizeof(*map), GFP_KERNEL);
333 if (map == NULL) {
334 ret = -ENOMEM;
335 goto err;
336 }
337
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600338 if (bus->fast_io) {
339 spin_lock_init(&map->spinlock);
340 map->lock = regmap_lock_spinlock;
341 map->unlock = regmap_unlock_spinlock;
342 } else {
343 mutex_init(&map->mutex);
344 map->lock = regmap_lock_mutex;
345 map->unlock = regmap_unlock_mutex;
346 }
Wolfram Sangc212acc2012-01-28 02:16:41 +0100347 map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8);
Mark Brown82159ba2012-01-18 10:52:25 +0000348 map->format.pad_bytes = config->pad_bits / 8;
Wolfram Sangc212acc2012-01-28 02:16:41 +0100349 map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8);
Fabio Estevam5494a982012-05-31 21:10:30 -0300350 map->format.buf_size = DIV_ROUND_UP(config->reg_bits +
351 config->val_bits + config->pad_bits, 8);
Marc Reillyd939fb92012-03-16 12:11:43 +1100352 map->reg_shift = config->pad_bits % 8;
Stephen Warrenf01ee602012-04-09 13:40:24 -0600353 if (config->reg_stride)
354 map->reg_stride = config->reg_stride;
355 else
356 map->reg_stride = 1;
Ashish Jangam2e33caf2012-04-30 23:23:40 +0100357 map->use_single_rw = config->use_single_rw;
Mark Brownb83a3132011-05-11 19:59:58 +0200358 map->dev = dev;
359 map->bus = bus;
Stephen Warren0135bbc2012-04-04 15:48:30 -0600360 map->bus_context = bus_context;
Mark Brown2e2ae662011-07-20 22:33:39 +0100361 map->max_register = config->max_register;
362 map->writeable_reg = config->writeable_reg;
363 map->readable_reg = config->readable_reg;
364 map->volatile_reg = config->volatile_reg;
Mark Brown2efe1642011-08-08 15:41:46 +0900365 map->precious_reg = config->precious_reg;
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100366 map->cache_type = config->cache_type;
Mark Brown72b39f62012-05-08 17:44:40 +0100367 map->name = config->name;
Mark Brownb83a3132011-05-11 19:59:58 +0200368
Lars-Peter Clausen6f306442011-09-05 20:46:32 +0200369 if (config->read_flag_mask || config->write_flag_mask) {
370 map->read_flag_mask = config->read_flag_mask;
371 map->write_flag_mask = config->write_flag_mask;
372 } else {
373 map->read_flag_mask = bus->read_flag_mask;
374 }
375
Stephen Warren141eba22012-05-24 10:47:26 -0600376 reg_endian = config->reg_format_endian;
377 if (reg_endian == REGMAP_ENDIAN_DEFAULT)
378 reg_endian = bus->reg_format_endian_default;
379 if (reg_endian == REGMAP_ENDIAN_DEFAULT)
380 reg_endian = REGMAP_ENDIAN_BIG;
381
382 val_endian = config->val_format_endian;
383 if (val_endian == REGMAP_ENDIAN_DEFAULT)
384 val_endian = bus->val_format_endian_default;
385 if (val_endian == REGMAP_ENDIAN_DEFAULT)
386 val_endian = REGMAP_ENDIAN_BIG;
387
Marc Reillyd939fb92012-03-16 12:11:43 +1100388 switch (config->reg_bits + map->reg_shift) {
Wolfram Sang9aa50752012-01-27 16:10:22 +0100389 case 2:
390 switch (config->val_bits) {
391 case 6:
392 map->format.format_write = regmap_format_2_6_write;
393 break;
394 default:
395 goto err_map;
396 }
397 break;
398
Mark Brownb83a3132011-05-11 19:59:58 +0200399 case 4:
400 switch (config->val_bits) {
401 case 12:
402 map->format.format_write = regmap_format_4_12_write;
403 break;
404 default:
405 goto err_map;
406 }
407 break;
408
409 case 7:
410 switch (config->val_bits) {
411 case 9:
412 map->format.format_write = regmap_format_7_9_write;
413 break;
414 default:
415 goto err_map;
416 }
417 break;
418
Lars-Peter Clausen7e5ec632011-11-16 16:28:21 +0100419 case 10:
420 switch (config->val_bits) {
421 case 14:
422 map->format.format_write = regmap_format_10_14_write;
423 break;
424 default:
425 goto err_map;
426 }
427 break;
428
Mark Brownb83a3132011-05-11 19:59:58 +0200429 case 8:
430 map->format.format_reg = regmap_format_8;
431 break;
432
433 case 16:
Stephen Warren141eba22012-05-24 10:47:26 -0600434 switch (reg_endian) {
435 case REGMAP_ENDIAN_BIG:
436 map->format.format_reg = regmap_format_16_be;
437 break;
438 case REGMAP_ENDIAN_NATIVE:
439 map->format.format_reg = regmap_format_16_native;
440 break;
441 default:
442 goto err_map;
443 }
Mark Brownb83a3132011-05-11 19:59:58 +0200444 break;
445
Mark Brown7d5e5252012-02-17 15:58:25 -0800446 case 32:
Stephen Warren141eba22012-05-24 10:47:26 -0600447 switch (reg_endian) {
448 case REGMAP_ENDIAN_BIG:
449 map->format.format_reg = regmap_format_32_be;
450 break;
451 case REGMAP_ENDIAN_NATIVE:
452 map->format.format_reg = regmap_format_32_native;
453 break;
454 default:
455 goto err_map;
456 }
Mark Brown7d5e5252012-02-17 15:58:25 -0800457 break;
458
Mark Brownb83a3132011-05-11 19:59:58 +0200459 default:
460 goto err_map;
461 }
462
463 switch (config->val_bits) {
464 case 8:
465 map->format.format_val = regmap_format_8;
466 map->format.parse_val = regmap_parse_8;
467 break;
468 case 16:
Stephen Warren141eba22012-05-24 10:47:26 -0600469 switch (val_endian) {
470 case REGMAP_ENDIAN_BIG:
471 map->format.format_val = regmap_format_16_be;
472 map->format.parse_val = regmap_parse_16_be;
473 break;
474 case REGMAP_ENDIAN_NATIVE:
475 map->format.format_val = regmap_format_16_native;
476 map->format.parse_val = regmap_parse_16_native;
477 break;
478 default:
479 goto err_map;
480 }
Mark Brownb83a3132011-05-11 19:59:58 +0200481 break;
Marc Reillyea279fc2012-03-16 12:11:42 +1100482 case 24:
Stephen Warren141eba22012-05-24 10:47:26 -0600483 if (val_endian != REGMAP_ENDIAN_BIG)
484 goto err_map;
Marc Reillyea279fc2012-03-16 12:11:42 +1100485 map->format.format_val = regmap_format_24;
486 map->format.parse_val = regmap_parse_24;
487 break;
Mark Brown7d5e5252012-02-17 15:58:25 -0800488 case 32:
Stephen Warren141eba22012-05-24 10:47:26 -0600489 switch (val_endian) {
490 case REGMAP_ENDIAN_BIG:
491 map->format.format_val = regmap_format_32_be;
492 map->format.parse_val = regmap_parse_32_be;
493 break;
494 case REGMAP_ENDIAN_NATIVE:
495 map->format.format_val = regmap_format_32_native;
496 map->format.parse_val = regmap_parse_32_native;
497 break;
498 default:
499 goto err_map;
500 }
Mark Brown7d5e5252012-02-17 15:58:25 -0800501 break;
Mark Brownb83a3132011-05-11 19:59:58 +0200502 }
503
Stephen Warren141eba22012-05-24 10:47:26 -0600504 if (map->format.format_write) {
505 if ((reg_endian != REGMAP_ENDIAN_BIG) ||
506 (val_endian != REGMAP_ENDIAN_BIG))
507 goto err_map;
Mark Brown7a647612012-04-30 23:26:32 +0100508 map->use_single_rw = true;
Stephen Warren141eba22012-05-24 10:47:26 -0600509 }
Mark Brown7a647612012-04-30 23:26:32 +0100510
Mark Brownb83a3132011-05-11 19:59:58 +0200511 if (!map->format.format_write &&
512 !(map->format.format_reg && map->format.format_val))
513 goto err_map;
514
Mark Brown82159ba2012-01-18 10:52:25 +0000515 map->work_buf = kzalloc(map->format.buf_size, GFP_KERNEL);
Mark Brownb83a3132011-05-11 19:59:58 +0200516 if (map->work_buf == NULL) {
517 ret = -ENOMEM;
Mark Brown5204f5e2011-09-05 08:07:47 -0700518 goto err_map;
Mark Brownb83a3132011-05-11 19:59:58 +0200519 }
520
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100521 map->range_tree = RB_ROOT;
Mark Browne3549cd2012-10-02 20:17:15 +0100522 for (i = 0; i < config->num_ranges; i++) {
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100523 const struct regmap_range_cfg *range_cfg = &config->ranges[i];
524 struct regmap_range_node *new;
525
526 /* Sanity check */
Mark Brown061adc02012-10-03 12:17:51 +0100527 if (range_cfg->range_max < range_cfg->range_min) {
528 dev_err(map->dev, "Invalid range %d: %d < %d\n", i,
529 range_cfg->range_max, range_cfg->range_min);
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100530 goto err_range;
Mark Brown061adc02012-10-03 12:17:51 +0100531 }
532
533 if (range_cfg->range_max > map->max_register) {
534 dev_err(map->dev, "Invalid range %d: %d > %d\n", i,
535 range_cfg->range_max, map->max_register);
536 goto err_range;
537 }
538
539 if (range_cfg->selector_reg > map->max_register) {
540 dev_err(map->dev,
541 "Invalid range %d: selector out of map\n", i);
542 goto err_range;
543 }
544
545 if (range_cfg->window_len == 0) {
546 dev_err(map->dev, "Invalid range %d: window_len 0\n",
547 i);
548 goto err_range;
549 }
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100550
551 /* Make sure, that this register range has no selector
552 or data window within its boundary */
Mark Browne3549cd2012-10-02 20:17:15 +0100553 for (j = 0; j < config->num_ranges; j++) {
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100554 unsigned sel_reg = config->ranges[j].selector_reg;
555 unsigned win_min = config->ranges[j].window_start;
556 unsigned win_max = win_min +
557 config->ranges[j].window_len - 1;
558
559 if (range_cfg->range_min <= sel_reg &&
560 sel_reg <= range_cfg->range_max) {
Mark Brown061adc02012-10-03 12:17:51 +0100561 dev_err(map->dev,
562 "Range %d: selector for %d in window\n",
563 i, j);
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100564 goto err_range;
565 }
566
567 if (!(win_max < range_cfg->range_min ||
568 win_min > range_cfg->range_max)) {
Mark Brown061adc02012-10-03 12:17:51 +0100569 dev_err(map->dev,
570 "Range %d: window for %d in window\n",
571 i, j);
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100572 goto err_range;
573 }
574 }
575
576 new = kzalloc(sizeof(*new), GFP_KERNEL);
577 if (new == NULL) {
578 ret = -ENOMEM;
579 goto err_range;
580 }
581
Mark Brown4b020b32012-10-03 13:13:16 +0100582 new->map = map;
Mark Brownd058bb42012-10-03 12:40:47 +0100583 new->name = range_cfg->name;
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100584 new->range_min = range_cfg->range_min;
585 new->range_max = range_cfg->range_max;
586 new->selector_reg = range_cfg->selector_reg;
587 new->selector_mask = range_cfg->selector_mask;
588 new->selector_shift = range_cfg->selector_shift;
589 new->window_start = range_cfg->window_start;
590 new->window_len = range_cfg->window_len;
591
592 if (_regmap_range_add(map, new) == false) {
Mark Brown061adc02012-10-03 12:17:51 +0100593 dev_err(map->dev, "Failed to add range %d\n", i);
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100594 kfree(new);
595 goto err_range;
596 }
597
598 if (map->selector_work_buf == NULL) {
599 map->selector_work_buf =
600 kzalloc(map->format.buf_size, GFP_KERNEL);
601 if (map->selector_work_buf == NULL) {
602 ret = -ENOMEM;
603 goto err_range;
604 }
605 }
606 }
Mark Brown052d2cd2011-11-21 19:05:13 +0000607
Lars-Peter Clausene5e3b8a2011-11-16 16:28:16 +0100608 ret = regcache_init(map, config);
Mark Brown0ff3e622012-10-04 17:39:13 +0100609 if (ret != 0)
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100610 goto err_range;
611
612 regmap_debugfs_init(map, config->name);
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100613
Mark Brown72b39f62012-05-08 17:44:40 +0100614 /* Add a devres resource for dev_get_regmap() */
615 m = devres_alloc(dev_get_regmap_release, sizeof(*m), GFP_KERNEL);
616 if (!m) {
617 ret = -ENOMEM;
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100618 goto err_debugfs;
Mark Brown72b39f62012-05-08 17:44:40 +0100619 }
620 *m = map;
621 devres_add(dev, m);
622
Mark Brownb83a3132011-05-11 19:59:58 +0200623 return map;
624
Stephen Warrenbfaa25f2012-05-23 16:30:53 -0600625err_debugfs:
626 regmap_debugfs_exit(map);
Mark Brown72b39f62012-05-08 17:44:40 +0100627 regcache_exit(map);
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100628err_range:
629 regmap_range_exit(map);
Lars-Peter Clausen58072cb2011-11-10 18:15:15 +0100630 kfree(map->work_buf);
Mark Brownb83a3132011-05-11 19:59:58 +0200631err_map:
632 kfree(map);
633err:
634 return ERR_PTR(ret);
635}
636EXPORT_SYMBOL_GPL(regmap_init);
637
Mark Brownc0eb4672012-01-30 19:56:52 +0000638static void devm_regmap_release(struct device *dev, void *res)
639{
640 regmap_exit(*(struct regmap **)res);
641}
642
643/**
644 * devm_regmap_init(): Initialise managed register map
645 *
646 * @dev: Device that will be interacted with
647 * @bus: Bus-specific callbacks to use with device
Stephen Warren0135bbc2012-04-04 15:48:30 -0600648 * @bus_context: Data passed to bus-specific callbacks
Mark Brownc0eb4672012-01-30 19:56:52 +0000649 * @config: Configuration for register map
650 *
651 * The return value will be an ERR_PTR() on error or a valid pointer
652 * to a struct regmap. This function should generally not be called
653 * directly, it should be called by bus-specific init functions. The
654 * map will be automatically freed by the device management code.
655 */
656struct regmap *devm_regmap_init(struct device *dev,
657 const struct regmap_bus *bus,
Stephen Warren0135bbc2012-04-04 15:48:30 -0600658 void *bus_context,
Mark Brownc0eb4672012-01-30 19:56:52 +0000659 const struct regmap_config *config)
660{
661 struct regmap **ptr, *regmap;
662
663 ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL);
664 if (!ptr)
665 return ERR_PTR(-ENOMEM);
666
Stephen Warren0135bbc2012-04-04 15:48:30 -0600667 regmap = regmap_init(dev, bus, bus_context, config);
Mark Brownc0eb4672012-01-30 19:56:52 +0000668 if (!IS_ERR(regmap)) {
669 *ptr = regmap;
670 devres_add(dev, ptr);
671 } else {
672 devres_free(ptr);
673 }
674
675 return regmap;
676}
677EXPORT_SYMBOL_GPL(devm_regmap_init);
678
Mark Brownb83a3132011-05-11 19:59:58 +0200679/**
Mark Brownbf315172011-12-03 17:06:20 +0000680 * regmap_reinit_cache(): Reinitialise the current register cache
681 *
682 * @map: Register map to operate on.
683 * @config: New configuration. Only the cache data will be used.
684 *
685 * Discard any existing register cache for the map and initialize a
686 * new cache. This can be used to restore the cache to defaults or to
687 * update the cache configuration to reflect runtime discovery of the
688 * hardware.
Dimitris Papastamos4d879512012-07-27 14:54:15 +0100689 *
690 * No explicit locking is done here, the user needs to ensure that
691 * this function will not race with other calls to regmap.
Mark Brownbf315172011-12-03 17:06:20 +0000692 */
693int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
694{
Mark Brownbf315172011-12-03 17:06:20 +0000695 regcache_exit(map);
Mark Browna24f64a2012-01-26 18:30:16 +0000696 regmap_debugfs_exit(map);
Mark Brownbf315172011-12-03 17:06:20 +0000697
698 map->max_register = config->max_register;
699 map->writeable_reg = config->writeable_reg;
700 map->readable_reg = config->readable_reg;
701 map->volatile_reg = config->volatile_reg;
702 map->precious_reg = config->precious_reg;
703 map->cache_type = config->cache_type;
704
Stephen Warrend3c242e2012-04-04 15:48:29 -0600705 regmap_debugfs_init(map, config->name);
Mark Browna24f64a2012-01-26 18:30:16 +0000706
Mark Brown421e8d22012-01-20 13:39:37 +0000707 map->cache_bypass = false;
708 map->cache_only = false;
709
Dimitris Papastamos4d879512012-07-27 14:54:15 +0100710 return regcache_init(map, config);
Mark Brownbf315172011-12-03 17:06:20 +0000711}
Mark Brown752a6a52012-05-14 10:00:12 +0100712EXPORT_SYMBOL_GPL(regmap_reinit_cache);
Mark Brownbf315172011-12-03 17:06:20 +0000713
714/**
Mark Brownb83a3132011-05-11 19:59:58 +0200715 * regmap_exit(): Free a previously allocated register map
716 */
717void regmap_exit(struct regmap *map)
718{
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100719 regcache_exit(map);
Mark Brown31244e32011-07-20 22:56:53 +0100720 regmap_debugfs_exit(map);
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100721 regmap_range_exit(map);
Stephen Warren0135bbc2012-04-04 15:48:30 -0600722 if (map->bus->free_context)
723 map->bus->free_context(map->bus_context);
Mark Brownb83a3132011-05-11 19:59:58 +0200724 kfree(map->work_buf);
Mark Brownb83a3132011-05-11 19:59:58 +0200725 kfree(map);
726}
727EXPORT_SYMBOL_GPL(regmap_exit);
728
Mark Brown72b39f62012-05-08 17:44:40 +0100729static int dev_get_regmap_match(struct device *dev, void *res, void *data)
730{
731 struct regmap **r = res;
732 if (!r || !*r) {
733 WARN_ON(!r || !*r);
734 return 0;
735 }
736
737 /* If the user didn't specify a name match any */
738 if (data)
739 return (*r)->name == data;
740 else
741 return 1;
742}
743
744/**
745 * dev_get_regmap(): Obtain the regmap (if any) for a device
746 *
747 * @dev: Device to retrieve the map for
748 * @name: Optional name for the register map, usually NULL.
749 *
750 * Returns the regmap for the device if one is present, or NULL. If
751 * name is specified then it must match the name specified when
752 * registering the device, if it is NULL then the first regmap found
753 * will be used. Devices with multiple register maps are very rare,
754 * generic code should normally not need to specify a name.
755 */
756struct regmap *dev_get_regmap(struct device *dev, const char *name)
757{
758 struct regmap **r = devres_find(dev, dev_get_regmap_release,
759 dev_get_regmap_match, (void *)name);
760
761 if (!r)
762 return NULL;
763 return *r;
764}
765EXPORT_SYMBOL_GPL(dev_get_regmap);
766
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100767static int _regmap_select_page(struct regmap *map, unsigned int *reg,
Mark Brown98bc7df2012-10-04 17:31:11 +0100768 struct regmap_range_node *range,
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100769 unsigned int val_num)
770{
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100771 void *orig_work_buf;
772 unsigned int win_offset;
773 unsigned int win_page;
774 bool page_chg;
775 int ret;
776
Mark Brown98bc7df2012-10-04 17:31:11 +0100777 win_offset = (*reg - range->range_min) % range->window_len;
778 win_page = (*reg - range->range_min) / range->window_len;
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100779
Mark Brown98bc7df2012-10-04 17:31:11 +0100780 if (val_num > 1) {
781 /* Bulk write shouldn't cross range boundary */
782 if (*reg + val_num - 1 > range->range_max)
783 return -EINVAL;
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100784
Mark Brown98bc7df2012-10-04 17:31:11 +0100785 /* ... or single page boundary */
786 if (val_num > range->window_len - win_offset)
787 return -EINVAL;
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100788 }
789
Mark Brown98bc7df2012-10-04 17:31:11 +0100790 /* It is possible to have selector register inside data window.
791 In that case, selector register is located on every page and
792 it needs no page switching, when accessed alone. */
793 if (val_num > 1 ||
794 range->window_start + win_offset != range->selector_reg) {
795 /* Use separate work_buf during page switching */
796 orig_work_buf = map->work_buf;
797 map->work_buf = map->selector_work_buf;
798
799 ret = _regmap_update_bits(map, range->selector_reg,
800 range->selector_mask,
801 win_page << range->selector_shift,
802 &page_chg);
803
804 map->work_buf = orig_work_buf;
805
Mark Brown0ff3e622012-10-04 17:39:13 +0100806 if (ret != 0)
Mark Brown98bc7df2012-10-04 17:31:11 +0100807 return ret;
808 }
809
810 *reg = range->window_start + win_offset;
811
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100812 return 0;
813}
814
Mark Brownb83a3132011-05-11 19:59:58 +0200815static int _regmap_raw_write(struct regmap *map, unsigned int reg,
816 const void *val, size_t val_len)
817{
Mark Brown98bc7df2012-10-04 17:31:11 +0100818 struct regmap_range_node *range;
Lars-Peter Clausen6f306442011-09-05 20:46:32 +0200819 u8 *u8 = map->work_buf;
Mark Brownb83a3132011-05-11 19:59:58 +0200820 void *buf;
821 int ret = -ENOTSUPP;
822 size_t len;
Mark Brown73304782011-07-24 11:46:20 +0100823 int i;
824
825 /* Check for unwritable registers before we start */
826 if (map->writeable_reg)
827 for (i = 0; i < val_len / map->format.val_bytes; i++)
Stephen Warrenf01ee602012-04-09 13:40:24 -0600828 if (!map->writeable_reg(map->dev,
829 reg + (i * map->reg_stride)))
Mark Brown73304782011-07-24 11:46:20 +0100830 return -EINVAL;
Mark Brownb83a3132011-05-11 19:59:58 +0200831
Laxman Dewanganc9157192012-02-10 21:30:27 +0530832 if (!map->cache_bypass && map->format.parse_val) {
833 unsigned int ival;
834 int val_bytes = map->format.val_bytes;
835 for (i = 0; i < val_len / val_bytes; i++) {
836 memcpy(map->work_buf, val + (i * val_bytes), val_bytes);
837 ival = map->format.parse_val(map->work_buf);
Stephen Warrenf01ee602012-04-09 13:40:24 -0600838 ret = regcache_write(map, reg + (i * map->reg_stride),
839 ival);
Laxman Dewanganc9157192012-02-10 21:30:27 +0530840 if (ret) {
841 dev_err(map->dev,
842 "Error in caching of register: %u ret: %d\n",
843 reg + i, ret);
844 return ret;
845 }
846 }
847 if (map->cache_only) {
848 map->cache_dirty = true;
849 return 0;
850 }
851 }
852
Mark Brown98bc7df2012-10-04 17:31:11 +0100853 range = _regmap_range_lookup(map, reg);
854 if (range) {
855 ret = _regmap_select_page(map, &reg, range,
856 val_len / map->format.val_bytes);
Mark Brown0ff3e622012-10-04 17:39:13 +0100857 if (ret != 0)
Mark Brown98bc7df2012-10-04 17:31:11 +0100858 return ret;
859 }
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100860
Marc Reillyd939fb92012-03-16 12:11:43 +1100861 map->format.format_reg(map->work_buf, reg, map->reg_shift);
Mark Brownb83a3132011-05-11 19:59:58 +0200862
Lars-Peter Clausen6f306442011-09-05 20:46:32 +0200863 u8[0] |= map->write_flag_mask;
864
Mark Brownfb2736b2011-07-24 21:30:55 +0100865 trace_regmap_hw_write_start(map->dev, reg,
866 val_len / map->format.val_bytes);
867
Mark Brown2547e202011-07-20 21:47:22 +0100868 /* If we're doing a single register write we can probably just
869 * send the work_buf directly, otherwise try to do a gather
870 * write.
871 */
Mark Brown82159ba2012-01-18 10:52:25 +0000872 if (val == (map->work_buf + map->format.pad_bytes +
873 map->format.reg_bytes))
Stephen Warren0135bbc2012-04-04 15:48:30 -0600874 ret = map->bus->write(map->bus_context, map->work_buf,
Mark Brown82159ba2012-01-18 10:52:25 +0000875 map->format.reg_bytes +
876 map->format.pad_bytes +
877 val_len);
Mark Brown2547e202011-07-20 21:47:22 +0100878 else if (map->bus->gather_write)
Stephen Warren0135bbc2012-04-04 15:48:30 -0600879 ret = map->bus->gather_write(map->bus_context, map->work_buf,
Mark Brown82159ba2012-01-18 10:52:25 +0000880 map->format.reg_bytes +
881 map->format.pad_bytes,
Mark Brownb83a3132011-05-11 19:59:58 +0200882 val, val_len);
883
Mark Brown2547e202011-07-20 21:47:22 +0100884 /* If that didn't work fall back on linearising by hand. */
Mark Brownb83a3132011-05-11 19:59:58 +0200885 if (ret == -ENOTSUPP) {
Mark Brown82159ba2012-01-18 10:52:25 +0000886 len = map->format.reg_bytes + map->format.pad_bytes + val_len;
887 buf = kzalloc(len, GFP_KERNEL);
Mark Brownb83a3132011-05-11 19:59:58 +0200888 if (!buf)
889 return -ENOMEM;
890
891 memcpy(buf, map->work_buf, map->format.reg_bytes);
Mark Brown82159ba2012-01-18 10:52:25 +0000892 memcpy(buf + map->format.reg_bytes + map->format.pad_bytes,
893 val, val_len);
Stephen Warren0135bbc2012-04-04 15:48:30 -0600894 ret = map->bus->write(map->bus_context, buf, len);
Mark Brownb83a3132011-05-11 19:59:58 +0200895
896 kfree(buf);
897 }
898
Mark Brownfb2736b2011-07-24 21:30:55 +0100899 trace_regmap_hw_write_done(map->dev, reg,
900 val_len / map->format.val_bytes);
901
Mark Brownb83a3132011-05-11 19:59:58 +0200902 return ret;
903}
904
Dimitris Papastamos4d2dc092011-09-29 10:39:07 +0100905int _regmap_write(struct regmap *map, unsigned int reg,
906 unsigned int val)
Mark Brownb83a3132011-05-11 19:59:58 +0200907{
Mark Brown98bc7df2012-10-04 17:31:11 +0100908 struct regmap_range_node *range;
Mark Brownfb2736b2011-07-24 21:30:55 +0100909 int ret;
Mark Brownb83a3132011-05-11 19:59:58 +0200910 BUG_ON(!map->format.format_write && !map->format.format_val);
911
Laxman Dewanganc9157192012-02-10 21:30:27 +0530912 if (!map->cache_bypass && map->format.format_write) {
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100913 ret = regcache_write(map, reg, val);
914 if (ret != 0)
915 return ret;
Mark Brown8ae0d7e2011-10-26 10:34:22 +0200916 if (map->cache_only) {
917 map->cache_dirty = true;
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100918 return 0;
Mark Brown8ae0d7e2011-10-26 10:34:22 +0200919 }
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100920 }
921
Mark Brown1044c182012-07-06 14:10:23 +0100922#ifdef LOG_DEVICE
923 if (strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
924 dev_info(map->dev, "%x <= %x\n", reg, val);
925#endif
926
Mark Brownfb2736b2011-07-24 21:30:55 +0100927 trace_regmap_reg_write(map->dev, reg, val);
928
Mark Brownb83a3132011-05-11 19:59:58 +0200929 if (map->format.format_write) {
Mark Brown98bc7df2012-10-04 17:31:11 +0100930 range = _regmap_range_lookup(map, reg);
931 if (range) {
932 ret = _regmap_select_page(map, &reg, range, 1);
Mark Brown0ff3e622012-10-04 17:39:13 +0100933 if (ret != 0)
Mark Brown98bc7df2012-10-04 17:31:11 +0100934 return ret;
935 }
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100936
Mark Brownb83a3132011-05-11 19:59:58 +0200937 map->format.format_write(map, reg, val);
938
Mark Brownfb2736b2011-07-24 21:30:55 +0100939 trace_regmap_hw_write_start(map->dev, reg, 1);
940
Stephen Warren0135bbc2012-04-04 15:48:30 -0600941 ret = map->bus->write(map->bus_context, map->work_buf,
Mark Brownfb2736b2011-07-24 21:30:55 +0100942 map->format.buf_size);
943
944 trace_regmap_hw_write_done(map->dev, reg, 1);
945
946 return ret;
Mark Brownb83a3132011-05-11 19:59:58 +0200947 } else {
Mark Brown82159ba2012-01-18 10:52:25 +0000948 map->format.format_val(map->work_buf + map->format.reg_bytes
Marc Reillyd939fb92012-03-16 12:11:43 +1100949 + map->format.pad_bytes, val, 0);
Mark Brownb83a3132011-05-11 19:59:58 +0200950 return _regmap_raw_write(map, reg,
Mark Brown82159ba2012-01-18 10:52:25 +0000951 map->work_buf +
952 map->format.reg_bytes +
953 map->format.pad_bytes,
Mark Brownb83a3132011-05-11 19:59:58 +0200954 map->format.val_bytes);
955 }
956}
957
958/**
959 * regmap_write(): Write a value to a single register
960 *
961 * @map: Register map to write to
962 * @reg: Register to write to
963 * @val: Value to be written
964 *
965 * A value of zero will be returned on success, a negative errno will
966 * be returned in error cases.
967 */
968int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
969{
970 int ret;
971
Stephen Warrenf01ee602012-04-09 13:40:24 -0600972 if (reg % map->reg_stride)
973 return -EINVAL;
974
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600975 map->lock(map);
Mark Brownb83a3132011-05-11 19:59:58 +0200976
977 ret = _regmap_write(map, reg, val);
978
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600979 map->unlock(map);
Mark Brownb83a3132011-05-11 19:59:58 +0200980
981 return ret;
982}
983EXPORT_SYMBOL_GPL(regmap_write);
984
985/**
986 * regmap_raw_write(): Write raw values to one or more registers
987 *
988 * @map: Register map to write to
989 * @reg: Initial register to write to
990 * @val: Block of data to be written, laid out for direct transmission to the
991 * device
992 * @val_len: Length of data pointed to by val.
993 *
994 * This function is intended to be used for things like firmware
995 * download where a large block of data needs to be transferred to the
996 * device. No formatting will be done on the data provided.
997 *
998 * A value of zero will be returned on success, a negative errno will
999 * be returned in error cases.
1000 */
1001int regmap_raw_write(struct regmap *map, unsigned int reg,
1002 const void *val, size_t val_len)
1003{
1004 int ret;
1005
Stephen Warren851960b2012-04-06 15:16:03 -06001006 if (val_len % map->format.val_bytes)
1007 return -EINVAL;
Stephen Warrenf01ee602012-04-09 13:40:24 -06001008 if (reg % map->reg_stride)
1009 return -EINVAL;
Stephen Warren851960b2012-04-06 15:16:03 -06001010
Stephen Warrenbacdbe02012-04-04 15:48:28 -06001011 map->lock(map);
Mark Brownb83a3132011-05-11 19:59:58 +02001012
1013 ret = _regmap_raw_write(map, reg, val, val_len);
1014
Stephen Warrenbacdbe02012-04-04 15:48:28 -06001015 map->unlock(map);
Mark Brownb83a3132011-05-11 19:59:58 +02001016
1017 return ret;
1018}
1019EXPORT_SYMBOL_GPL(regmap_raw_write);
1020
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301021/*
1022 * regmap_bulk_write(): Write multiple registers to the device
1023 *
1024 * @map: Register map to write to
1025 * @reg: First register to be write from
1026 * @val: Block of data to be written, in native register size for device
1027 * @val_count: Number of registers to write
1028 *
1029 * This function is intended to be used for writing a large block of
1030 * data to be device either in single transfer or multiple transfer.
1031 *
1032 * A value of zero will be returned on success, a negative errno will
1033 * be returned in error cases.
1034 */
1035int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
1036 size_t val_count)
1037{
1038 int ret = 0, i;
1039 size_t val_bytes = map->format.val_bytes;
1040 void *wval;
1041
1042 if (!map->format.parse_val)
1043 return -EINVAL;
Stephen Warrenf01ee602012-04-09 13:40:24 -06001044 if (reg % map->reg_stride)
1045 return -EINVAL;
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301046
Stephen Warrenbacdbe02012-04-04 15:48:28 -06001047 map->lock(map);
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301048
1049 /* No formatting is require if val_byte is 1 */
1050 if (val_bytes == 1) {
1051 wval = (void *)val;
1052 } else {
1053 wval = kmemdup(val, val_count * val_bytes, GFP_KERNEL);
1054 if (!wval) {
1055 ret = -ENOMEM;
1056 dev_err(map->dev, "Error in memory allocation\n");
1057 goto out;
1058 }
1059 for (i = 0; i < val_count * val_bytes; i += val_bytes)
1060 map->format.parse_val(wval + i);
1061 }
Ashish Jangam2e33caf2012-04-30 23:23:40 +01001062 /*
1063 * Some devices does not support bulk write, for
1064 * them we have a series of single write operations.
1065 */
1066 if (map->use_single_rw) {
1067 for (i = 0; i < val_count; i++) {
1068 ret = regmap_raw_write(map,
1069 reg + (i * map->reg_stride),
1070 val + (i * val_bytes),
1071 val_bytes);
1072 if (ret != 0)
1073 return ret;
1074 }
1075 } else {
1076 ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count);
1077 }
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301078
1079 if (val_bytes != 1)
1080 kfree(wval);
1081
1082out:
Stephen Warrenbacdbe02012-04-04 15:48:28 -06001083 map->unlock(map);
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301084 return ret;
1085}
1086EXPORT_SYMBOL_GPL(regmap_bulk_write);
1087
Mark Brownb83a3132011-05-11 19:59:58 +02001088static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
1089 unsigned int val_len)
1090{
Mark Brown98bc7df2012-10-04 17:31:11 +01001091 struct regmap_range_node *range;
Mark Brownb83a3132011-05-11 19:59:58 +02001092 u8 *u8 = map->work_buf;
1093 int ret;
1094
Mark Brown98bc7df2012-10-04 17:31:11 +01001095 range = _regmap_range_lookup(map, reg);
1096 if (range) {
1097 ret = _regmap_select_page(map, &reg, range,
1098 val_len / map->format.val_bytes);
Mark Brown0ff3e622012-10-04 17:39:13 +01001099 if (ret != 0)
Mark Brown98bc7df2012-10-04 17:31:11 +01001100 return ret;
1101 }
Krystian Garbaciak6863ca62012-06-15 11:23:56 +01001102
Marc Reillyd939fb92012-03-16 12:11:43 +11001103 map->format.format_reg(map->work_buf, reg, map->reg_shift);
Mark Brownb83a3132011-05-11 19:59:58 +02001104
1105 /*
Lars-Peter Clausen6f306442011-09-05 20:46:32 +02001106 * Some buses or devices flag reads by setting the high bits in the
Mark Brownb83a3132011-05-11 19:59:58 +02001107 * register addresss; since it's always the high bits for all
1108 * current formats we can do this here rather than in
1109 * formatting. This may break if we get interesting formats.
1110 */
Lars-Peter Clausen6f306442011-09-05 20:46:32 +02001111 u8[0] |= map->read_flag_mask;
Mark Brownb83a3132011-05-11 19:59:58 +02001112
Mark Brownfb2736b2011-07-24 21:30:55 +01001113 trace_regmap_hw_read_start(map->dev, reg,
1114 val_len / map->format.val_bytes);
1115
Stephen Warren0135bbc2012-04-04 15:48:30 -06001116 ret = map->bus->read(map->bus_context, map->work_buf,
Mark Brown82159ba2012-01-18 10:52:25 +00001117 map->format.reg_bytes + map->format.pad_bytes,
Mark Brown40c5cc22011-07-24 22:39:12 +01001118 val, val_len);
Mark Brownb83a3132011-05-11 19:59:58 +02001119
Mark Brownfb2736b2011-07-24 21:30:55 +01001120 trace_regmap_hw_read_done(map->dev, reg,
1121 val_len / map->format.val_bytes);
1122
1123 return ret;
Mark Brownb83a3132011-05-11 19:59:58 +02001124}
1125
1126static int _regmap_read(struct regmap *map, unsigned int reg,
1127 unsigned int *val)
1128{
1129 int ret;
1130
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +01001131 if (!map->cache_bypass) {
1132 ret = regcache_read(map, reg, val);
1133 if (ret == 0)
1134 return 0;
1135 }
1136
Lars-Peter Clausen19254412011-11-16 16:28:19 +01001137 if (!map->format.parse_val)
1138 return -EINVAL;
1139
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +01001140 if (map->cache_only)
1141 return -EBUSY;
1142
Mark Brownb83a3132011-05-11 19:59:58 +02001143 ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
Mark Brownfb2736b2011-07-24 21:30:55 +01001144 if (ret == 0) {
Mark Brownb83a3132011-05-11 19:59:58 +02001145 *val = map->format.parse_val(map->work_buf);
Mark Brown1044c182012-07-06 14:10:23 +01001146
1147#ifdef LOG_DEVICE
1148 if (strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
1149 dev_info(map->dev, "%x => %x\n", reg, *val);
1150#endif
1151
Mark Brownfb2736b2011-07-24 21:30:55 +01001152 trace_regmap_reg_read(map->dev, reg, *val);
1153 }
Mark Brownb83a3132011-05-11 19:59:58 +02001154
Mark Brownf2985362012-04-30 21:25:05 +01001155 if (ret == 0 && !map->cache_bypass)
1156 regcache_write(map, reg, *val);
1157
Mark Brownb83a3132011-05-11 19:59:58 +02001158 return ret;
1159}
1160
1161/**
1162 * regmap_read(): Read a value from a single register
1163 *
1164 * @map: Register map to write to
1165 * @reg: Register to be read from
1166 * @val: Pointer to store read value
1167 *
1168 * A value of zero will be returned on success, a negative errno will
1169 * be returned in error cases.
1170 */
1171int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
1172{
1173 int ret;
1174
Stephen Warrenf01ee602012-04-09 13:40:24 -06001175 if (reg % map->reg_stride)
1176 return -EINVAL;
1177
Stephen Warrenbacdbe02012-04-04 15:48:28 -06001178 map->lock(map);
Mark Brownb83a3132011-05-11 19:59:58 +02001179
1180 ret = _regmap_read(map, reg, val);
1181
Stephen Warrenbacdbe02012-04-04 15:48:28 -06001182 map->unlock(map);
Mark Brownb83a3132011-05-11 19:59:58 +02001183
1184 return ret;
1185}
1186EXPORT_SYMBOL_GPL(regmap_read);
1187
1188/**
1189 * regmap_raw_read(): Read raw data from the device
1190 *
1191 * @map: Register map to write to
1192 * @reg: First register to be read from
1193 * @val: Pointer to store read value
1194 * @val_len: Size of data to read
1195 *
1196 * A value of zero will be returned on success, a negative errno will
1197 * be returned in error cases.
1198 */
1199int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
1200 size_t val_len)
1201{
Mark Brownb8fb5ab2012-02-21 19:12:47 +00001202 size_t val_bytes = map->format.val_bytes;
1203 size_t val_count = val_len / val_bytes;
1204 unsigned int v;
1205 int ret, i;
Mark Brown04e016a2011-10-09 13:35:43 +01001206
Stephen Warren851960b2012-04-06 15:16:03 -06001207 if (val_len % map->format.val_bytes)
1208 return -EINVAL;
Stephen Warrenf01ee602012-04-09 13:40:24 -06001209 if (reg % map->reg_stride)
1210 return -EINVAL;
Stephen Warren851960b2012-04-06 15:16:03 -06001211
Stephen Warrenbacdbe02012-04-04 15:48:28 -06001212 map->lock(map);
Mark Brownb83a3132011-05-11 19:59:58 +02001213
Mark Brownb8fb5ab2012-02-21 19:12:47 +00001214 if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
1215 map->cache_type == REGCACHE_NONE) {
1216 /* Physical block read if there's no cache involved */
1217 ret = _regmap_raw_read(map, reg, val, val_len);
Mark Brownb83a3132011-05-11 19:59:58 +02001218
Mark Brownb8fb5ab2012-02-21 19:12:47 +00001219 } else {
1220 /* Otherwise go word by word for the cache; should be low
1221 * cost as we expect to hit the cache.
1222 */
1223 for (i = 0; i < val_count; i++) {
Stephen Warrenf01ee602012-04-09 13:40:24 -06001224 ret = _regmap_read(map, reg + (i * map->reg_stride),
1225 &v);
Mark Brownb8fb5ab2012-02-21 19:12:47 +00001226 if (ret != 0)
1227 goto out;
1228
Marc Reillyd939fb92012-03-16 12:11:43 +11001229 map->format.format_val(val + (i * val_bytes), v, 0);
Mark Brownb8fb5ab2012-02-21 19:12:47 +00001230 }
1231 }
1232
1233 out:
Stephen Warrenbacdbe02012-04-04 15:48:28 -06001234 map->unlock(map);
Mark Brownb83a3132011-05-11 19:59:58 +02001235
1236 return ret;
1237}
1238EXPORT_SYMBOL_GPL(regmap_raw_read);
1239
1240/**
1241 * regmap_bulk_read(): Read multiple registers from the device
1242 *
1243 * @map: Register map to write to
1244 * @reg: First register to be read from
1245 * @val: Pointer to store read value, in native register size for device
1246 * @val_count: Number of registers to read
1247 *
1248 * A value of zero will be returned on success, a negative errno will
1249 * be returned in error cases.
1250 */
1251int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
1252 size_t val_count)
1253{
1254 int ret, i;
1255 size_t val_bytes = map->format.val_bytes;
Lars-Peter Clausen82cd9962011-11-08 18:37:25 +01001256 bool vol = regmap_volatile_range(map, reg, val_count);
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +01001257
Mark Brownb83a3132011-05-11 19:59:58 +02001258 if (!map->format.parse_val)
1259 return -EINVAL;
Stephen Warrenf01ee602012-04-09 13:40:24 -06001260 if (reg % map->reg_stride)
1261 return -EINVAL;
Mark Brownb83a3132011-05-11 19:59:58 +02001262
Mark Brownde2d8082011-10-10 13:24:52 +01001263 if (vol || map->cache_type == REGCACHE_NONE) {
Ashish Jangam2e33caf2012-04-30 23:23:40 +01001264 /*
1265 * Some devices does not support bulk read, for
1266 * them we have a series of single read operations.
1267 */
1268 if (map->use_single_rw) {
1269 for (i = 0; i < val_count; i++) {
1270 ret = regmap_raw_read(map,
1271 reg + (i * map->reg_stride),
1272 val + (i * val_bytes),
1273 val_bytes);
1274 if (ret != 0)
1275 return ret;
1276 }
1277 } else {
1278 ret = regmap_raw_read(map, reg, val,
1279 val_bytes * val_count);
1280 if (ret != 0)
1281 return ret;
1282 }
Mark Brownde2d8082011-10-10 13:24:52 +01001283
1284 for (i = 0; i < val_count * val_bytes; i += val_bytes)
1285 map->format.parse_val(val + i);
1286 } else {
1287 for (i = 0; i < val_count; i++) {
Laxman Dewangan6560ffd2012-05-09 17:43:12 +05301288 unsigned int ival;
Stephen Warrenf01ee602012-04-09 13:40:24 -06001289 ret = regmap_read(map, reg + (i * map->reg_stride),
Mark Brown25061d22012-05-12 13:06:08 +01001290 &ival);
Mark Brownde2d8082011-10-10 13:24:52 +01001291 if (ret != 0)
1292 return ret;
Laxman Dewangan6560ffd2012-05-09 17:43:12 +05301293 memcpy(val + (i * val_bytes), &ival, val_bytes);
Mark Brownde2d8082011-10-10 13:24:52 +01001294 }
1295 }
Mark Brownb83a3132011-05-11 19:59:58 +02001296
1297 return 0;
1298}
1299EXPORT_SYMBOL_GPL(regmap_bulk_read);
1300
Mark Brown018690d2011-11-29 20:10:36 +00001301static int _regmap_update_bits(struct regmap *map, unsigned int reg,
1302 unsigned int mask, unsigned int val,
1303 bool *change)
Mark Brownb83a3132011-05-11 19:59:58 +02001304{
1305 int ret;
Mark Brownd91e8db22011-11-18 16:03:50 +00001306 unsigned int tmp, orig;
Mark Brownb83a3132011-05-11 19:59:58 +02001307
Mark Brownd91e8db22011-11-18 16:03:50 +00001308 ret = _regmap_read(map, reg, &orig);
Mark Brownb83a3132011-05-11 19:59:58 +02001309 if (ret != 0)
Krystian Garbaciakfc3ebd72012-06-15 11:23:56 +01001310 return ret;
Mark Brownb83a3132011-05-11 19:59:58 +02001311
Mark Brownd91e8db22011-11-18 16:03:50 +00001312 tmp = orig & ~mask;
Mark Brownb83a3132011-05-11 19:59:58 +02001313 tmp |= val & mask;
1314
Mark Brown018690d2011-11-29 20:10:36 +00001315 if (tmp != orig) {
Mark Brownd91e8db22011-11-18 16:03:50 +00001316 ret = _regmap_write(map, reg, tmp);
Mark Brown018690d2011-11-29 20:10:36 +00001317 *change = true;
1318 } else {
1319 *change = false;
1320 }
Mark Brownb83a3132011-05-11 19:59:58 +02001321
Mark Brownb83a3132011-05-11 19:59:58 +02001322 return ret;
1323}
Mark Brown018690d2011-11-29 20:10:36 +00001324
1325/**
1326 * regmap_update_bits: Perform a read/modify/write cycle on the register map
1327 *
1328 * @map: Register map to update
1329 * @reg: Register to update
1330 * @mask: Bitmask to change
1331 * @val: New value for bitmask
1332 *
1333 * Returns zero for success, a negative number on error.
1334 */
1335int regmap_update_bits(struct regmap *map, unsigned int reg,
1336 unsigned int mask, unsigned int val)
1337{
1338 bool change;
Krystian Garbaciakfc3ebd72012-06-15 11:23:56 +01001339 int ret;
1340
1341 map->lock(map);
1342 ret = _regmap_update_bits(map, reg, mask, val, &change);
1343 map->unlock(map);
1344
1345 return ret;
Mark Brown018690d2011-11-29 20:10:36 +00001346}
Mark Brownb83a3132011-05-11 19:59:58 +02001347EXPORT_SYMBOL_GPL(regmap_update_bits);
Mark Brown31244e32011-07-20 22:56:53 +01001348
Mark Brown018690d2011-11-29 20:10:36 +00001349/**
1350 * regmap_update_bits_check: Perform a read/modify/write cycle on the
1351 * register map and report if updated
1352 *
1353 * @map: Register map to update
1354 * @reg: Register to update
1355 * @mask: Bitmask to change
1356 * @val: New value for bitmask
1357 * @change: Boolean indicating if a write was done
1358 *
1359 * Returns zero for success, a negative number on error.
1360 */
1361int regmap_update_bits_check(struct regmap *map, unsigned int reg,
1362 unsigned int mask, unsigned int val,
1363 bool *change)
1364{
Krystian Garbaciakfc3ebd72012-06-15 11:23:56 +01001365 int ret;
1366
1367 map->lock(map);
1368 ret = _regmap_update_bits(map, reg, mask, val, change);
1369 map->unlock(map);
1370 return ret;
Mark Brown018690d2011-11-29 20:10:36 +00001371}
1372EXPORT_SYMBOL_GPL(regmap_update_bits_check);
1373
Mark Brown22f0d902012-01-21 12:01:14 +00001374/**
1375 * regmap_register_patch: Register and apply register updates to be applied
1376 * on device initialistion
1377 *
1378 * @map: Register map to apply updates to.
1379 * @regs: Values to update.
1380 * @num_regs: Number of entries in regs.
1381 *
1382 * Register a set of register updates to be applied to the device
1383 * whenever the device registers are synchronised with the cache and
1384 * apply them immediately. Typically this is used to apply
1385 * corrections to be applied to the device defaults on startup, such
1386 * as the updates some vendors provide to undocumented registers.
1387 */
1388int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
1389 int num_regs)
1390{
1391 int i, ret;
1392 bool bypass;
1393
1394 /* If needed the implementation can be extended to support this */
1395 if (map->patch)
1396 return -EBUSY;
1397
Stephen Warrenbacdbe02012-04-04 15:48:28 -06001398 map->lock(map);
Mark Brown22f0d902012-01-21 12:01:14 +00001399
1400 bypass = map->cache_bypass;
1401
1402 map->cache_bypass = true;
1403
1404 /* Write out first; it's useful to apply even if we fail later. */
1405 for (i = 0; i < num_regs; i++) {
1406 ret = _regmap_write(map, regs[i].reg, regs[i].def);
1407 if (ret != 0) {
1408 dev_err(map->dev, "Failed to write %x = %x: %d\n",
1409 regs[i].reg, regs[i].def, ret);
1410 goto out;
1411 }
1412 }
1413
Axel Lin2a14d7d92012-02-10 19:29:55 +08001414 map->patch = kcalloc(num_regs, sizeof(struct reg_default), GFP_KERNEL);
Mark Brown22f0d902012-01-21 12:01:14 +00001415 if (map->patch != NULL) {
1416 memcpy(map->patch, regs,
1417 num_regs * sizeof(struct reg_default));
1418 map->patch_regs = num_regs;
1419 } else {
1420 ret = -ENOMEM;
1421 }
1422
1423out:
1424 map->cache_bypass = bypass;
1425
Stephen Warrenbacdbe02012-04-04 15:48:28 -06001426 map->unlock(map);
Mark Brown22f0d902012-01-21 12:01:14 +00001427
1428 return ret;
1429}
1430EXPORT_SYMBOL_GPL(regmap_register_patch);
1431
Mark Browneae4b512012-03-14 13:15:03 +00001432/*
Mark Browna6539c32012-02-17 14:20:14 -08001433 * regmap_get_val_bytes(): Report the size of a register value
1434 *
1435 * Report the size of a register value, mainly intended to for use by
1436 * generic infrastructure built on top of regmap.
1437 */
1438int regmap_get_val_bytes(struct regmap *map)
1439{
1440 if (map->format.format_write)
1441 return -EINVAL;
1442
1443 return map->format.val_bytes;
1444}
1445EXPORT_SYMBOL_GPL(regmap_get_val_bytes);
1446
Mark Brown31244e32011-07-20 22:56:53 +01001447static int __init regmap_initcall(void)
1448{
1449 regmap_debugfs_initcall();
1450
1451 return 0;
1452}
1453postcore_initcall(regmap_initcall);