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