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