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