regmap: cache: Add 64-bit mode support
[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>
d647c199 18#include <linux/of.h>
6863ca62 19#include <linux/rbtree.h>
30b2a553 20#include <linux/sched.h>
2de9d600 21#include <linux/delay.h>
b83a313b 22
fb2736bb 23#define CREATE_TRACE_POINTS
f58078da 24#include "trace.h"
fb2736bb 25
93de9124 26#include "internal.h"
b83a313b 27
1044c180
MB
28/*
29 * Sometimes for failures during very early init the trace
30 * infrastructure isn't available early enough to be used. For this
31 * sort of problem defining LOG_DEVICE will add printks for basic
32 * register I/O on a specific device.
33 */
34#undef LOG_DEVICE
35
36static int _regmap_update_bits(struct regmap *map, unsigned int reg,
37 unsigned int mask, unsigned int val,
7ff0589c 38 bool *change, bool force_write);
1044c180 39
3ac17037
BB
40static int _regmap_bus_reg_read(void *context, unsigned int reg,
41 unsigned int *val);
ad278406
AS
42static int _regmap_bus_read(void *context, unsigned int reg,
43 unsigned int *val);
07c320dc
AS
44static int _regmap_bus_formatted_write(void *context, unsigned int reg,
45 unsigned int val);
3ac17037
BB
46static int _regmap_bus_reg_write(void *context, unsigned int reg,
47 unsigned int val);
07c320dc
AS
48static int _regmap_bus_raw_write(void *context, unsigned int reg,
49 unsigned int val);
ad278406 50
76aad392
DC
51bool regmap_reg_in_ranges(unsigned int reg,
52 const struct regmap_range *ranges,
53 unsigned int nranges)
54{
55 const struct regmap_range *r;
56 int i;
57
58 for (i = 0, r = ranges; i < nranges; i++, r++)
59 if (regmap_reg_in_range(reg, r))
60 return true;
61 return false;
62}
63EXPORT_SYMBOL_GPL(regmap_reg_in_ranges);
64
154881e5
MB
65bool regmap_check_range_table(struct regmap *map, unsigned int reg,
66 const struct regmap_access_table *table)
76aad392
DC
67{
68 /* Check "no ranges" first */
69 if (regmap_reg_in_ranges(reg, table->no_ranges, table->n_no_ranges))
70 return false;
71
72 /* In case zero "yes ranges" are supplied, any reg is OK */
73 if (!table->n_yes_ranges)
74 return true;
75
76 return regmap_reg_in_ranges(reg, table->yes_ranges,
77 table->n_yes_ranges);
78}
154881e5 79EXPORT_SYMBOL_GPL(regmap_check_range_table);
76aad392 80
8de2f081
MB
81bool regmap_writeable(struct regmap *map, unsigned int reg)
82{
83 if (map->max_register && reg > map->max_register)
84 return false;
85
86 if (map->writeable_reg)
87 return map->writeable_reg(map->dev, reg);
88
76aad392 89 if (map->wr_table)
154881e5 90 return regmap_check_range_table(map, reg, map->wr_table);
76aad392 91
8de2f081
MB
92 return true;
93}
94
95bool regmap_readable(struct regmap *map, unsigned int reg)
96{
04dc91ce
LPC
97 if (!map->reg_read)
98 return false;
99
8de2f081
MB
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{
5844a8b9 117 if (!map->format.format_write && !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
4aa8c069
XL
204static void regmap_format_16_le(void *buf, unsigned int val, unsigned int shift)
205{
206 __le16 *b = buf;
207
208 b[0] = cpu_to_le16(val << shift);
209}
210
141eba2e
SW
211static void regmap_format_16_native(void *buf, unsigned int val,
212 unsigned int shift)
213{
214 *(u16 *)buf = val << shift;
215}
216
d939fb9a 217static void regmap_format_24(void *buf, unsigned int val, unsigned int shift)
ea279fc5
MR
218{
219 u8 *b = buf;
220
d939fb9a
MR
221 val <<= shift;
222
ea279fc5
MR
223 b[0] = val >> 16;
224 b[1] = val >> 8;
225 b[2] = val;
226}
227
141eba2e 228static void regmap_format_32_be(void *buf, unsigned int val, unsigned int shift)
7d5e525b
MB
229{
230 __be32 *b = buf;
231
d939fb9a 232 b[0] = cpu_to_be32(val << shift);
7d5e525b
MB
233}
234
4aa8c069
XL
235static void regmap_format_32_le(void *buf, unsigned int val, unsigned int shift)
236{
237 __le32 *b = buf;
238
239 b[0] = cpu_to_le32(val << shift);
240}
241
141eba2e
SW
242static void regmap_format_32_native(void *buf, unsigned int val,
243 unsigned int shift)
244{
245 *(u32 *)buf = val << shift;
246}
247
afcc00b9
XL
248#ifdef CONFIG_64BIT
249static void regmap_format_64_be(void *buf, unsigned int val, unsigned int shift)
250{
251 __be64 *b = buf;
252
253 b[0] = cpu_to_be64(val << shift);
254}
255
256static void regmap_format_64_le(void *buf, unsigned int val, unsigned int shift)
257{
258 __le64 *b = buf;
259
260 b[0] = cpu_to_le64(val << shift);
261}
262
263static void regmap_format_64_native(void *buf, unsigned int val,
264 unsigned int shift)
265{
266 *(u64 *)buf = val << shift;
267}
268#endif
269
8a819ff8 270static void regmap_parse_inplace_noop(void *buf)
b83a313b 271{
8a819ff8
MB
272}
273
274static unsigned int regmap_parse_8(const void *buf)
275{
276 const u8 *b = buf;
b83a313b
MB
277
278 return b[0];
279}
280
8a819ff8
MB
281static unsigned int regmap_parse_16_be(const void *buf)
282{
283 const __be16 *b = buf;
284
285 return be16_to_cpu(b[0]);
286}
287
4aa8c069
XL
288static unsigned int regmap_parse_16_le(const void *buf)
289{
290 const __le16 *b = buf;
291
292 return le16_to_cpu(b[0]);
293}
294
8a819ff8 295static void regmap_parse_16_be_inplace(void *buf)
b83a313b
MB
296{
297 __be16 *b = buf;
298
299 b[0] = be16_to_cpu(b[0]);
b83a313b
MB
300}
301
4aa8c069
XL
302static void regmap_parse_16_le_inplace(void *buf)
303{
304 __le16 *b = buf;
305
306 b[0] = le16_to_cpu(b[0]);
307}
308
8a819ff8 309static unsigned int regmap_parse_16_native(const void *buf)
141eba2e
SW
310{
311 return *(u16 *)buf;
312}
313
8a819ff8 314static unsigned int regmap_parse_24(const void *buf)
ea279fc5 315{
8a819ff8 316 const u8 *b = buf;
ea279fc5
MR
317 unsigned int ret = b[2];
318 ret |= ((unsigned int)b[1]) << 8;
319 ret |= ((unsigned int)b[0]) << 16;
320
321 return ret;
322}
323
8a819ff8
MB
324static unsigned int regmap_parse_32_be(const void *buf)
325{
326 const __be32 *b = buf;
327
328 return be32_to_cpu(b[0]);
329}
330
4aa8c069
XL
331static unsigned int regmap_parse_32_le(const void *buf)
332{
333 const __le32 *b = buf;
334
335 return le32_to_cpu(b[0]);
336}
337
8a819ff8 338static void regmap_parse_32_be_inplace(void *buf)
7d5e525b
MB
339{
340 __be32 *b = buf;
341
342 b[0] = be32_to_cpu(b[0]);
7d5e525b
MB
343}
344
4aa8c069
XL
345static void regmap_parse_32_le_inplace(void *buf)
346{
347 __le32 *b = buf;
348
349 b[0] = le32_to_cpu(b[0]);
350}
351
8a819ff8 352static unsigned int regmap_parse_32_native(const void *buf)
141eba2e
SW
353{
354 return *(u32 *)buf;
355}
356
afcc00b9
XL
357#ifdef CONFIG_64BIT
358static unsigned int regmap_parse_64_be(const void *buf)
359{
360 const __be64 *b = buf;
361
362 return be64_to_cpu(b[0]);
363}
364
365static unsigned int regmap_parse_64_le(const void *buf)
366{
367 const __le64 *b = buf;
368
369 return le64_to_cpu(b[0]);
370}
371
372static void regmap_parse_64_be_inplace(void *buf)
373{
374 __be64 *b = buf;
375
376 b[0] = be64_to_cpu(b[0]);
377}
378
379static void regmap_parse_64_le_inplace(void *buf)
380{
381 __le64 *b = buf;
382
383 b[0] = le64_to_cpu(b[0]);
384}
385
386static unsigned int regmap_parse_64_native(const void *buf)
387{
388 return *(u64 *)buf;
389}
390#endif
391
0d4529c5 392static void regmap_lock_mutex(void *__map)
bacdbe07 393{
0d4529c5 394 struct regmap *map = __map;
bacdbe07
SW
395 mutex_lock(&map->mutex);
396}
397
0d4529c5 398static void regmap_unlock_mutex(void *__map)
bacdbe07 399{
0d4529c5 400 struct regmap *map = __map;
bacdbe07
SW
401 mutex_unlock(&map->mutex);
402}
403
0d4529c5 404static void regmap_lock_spinlock(void *__map)
b4519c71 405__acquires(&map->spinlock)
bacdbe07 406{
0d4529c5 407 struct regmap *map = __map;
92ab1aab
LPC
408 unsigned long flags;
409
410 spin_lock_irqsave(&map->spinlock, flags);
411 map->spinlock_flags = flags;
bacdbe07
SW
412}
413
0d4529c5 414static void regmap_unlock_spinlock(void *__map)
b4519c71 415__releases(&map->spinlock)
bacdbe07 416{
0d4529c5 417 struct regmap *map = __map;
92ab1aab 418 spin_unlock_irqrestore(&map->spinlock, map->spinlock_flags);
bacdbe07
SW
419}
420
72b39f6f
MB
421static void dev_get_regmap_release(struct device *dev, void *res)
422{
423 /*
424 * We don't actually have anything to do here; the goal here
425 * is not to manage the regmap but to provide a simple way to
426 * get the regmap back given a struct device.
427 */
428}
429
6863ca62
KG
430static bool _regmap_range_add(struct regmap *map,
431 struct regmap_range_node *data)
432{
433 struct rb_root *root = &map->range_tree;
434 struct rb_node **new = &(root->rb_node), *parent = NULL;
435
436 while (*new) {
437 struct regmap_range_node *this =
438 container_of(*new, struct regmap_range_node, node);
439
440 parent = *new;
441 if (data->range_max < this->range_min)
442 new = &((*new)->rb_left);
443 else if (data->range_min > this->range_max)
444 new = &((*new)->rb_right);
445 else
446 return false;
447 }
448
449 rb_link_node(&data->node, parent, new);
450 rb_insert_color(&data->node, root);
451
452 return true;
453}
454
455static struct regmap_range_node *_regmap_range_lookup(struct regmap *map,
456 unsigned int reg)
457{
458 struct rb_node *node = map->range_tree.rb_node;
459
460 while (node) {
461 struct regmap_range_node *this =
462 container_of(node, struct regmap_range_node, node);
463
464 if (reg < this->range_min)
465 node = node->rb_left;
466 else if (reg > this->range_max)
467 node = node->rb_right;
468 else
469 return this;
470 }
471
472 return NULL;
473}
474
475static void regmap_range_exit(struct regmap *map)
476{
477 struct rb_node *next;
478 struct regmap_range_node *range_node;
479
480 next = rb_first(&map->range_tree);
481 while (next) {
482 range_node = rb_entry(next, struct regmap_range_node, node);
483 next = rb_next(&range_node->node);
484 rb_erase(&range_node->node, &map->range_tree);
485 kfree(range_node);
486 }
487
488 kfree(map->selector_work_buf);
489}
490
6cfec04b
MS
491int regmap_attach_dev(struct device *dev, struct regmap *map,
492 const struct regmap_config *config)
493{
494 struct regmap **m;
495
496 map->dev = dev;
497
498 regmap_debugfs_init(map, config->name);
499
500 /* Add a devres resource for dev_get_regmap() */
501 m = devres_alloc(dev_get_regmap_release, sizeof(*m), GFP_KERNEL);
502 if (!m) {
503 regmap_debugfs_exit(map);
504 return -ENOMEM;
505 }
506 *m = map;
507 devres_add(dev, m);
508
509 return 0;
510}
511EXPORT_SYMBOL_GPL(regmap_attach_dev);
512
cf673fbc
GU
513static enum regmap_endian regmap_get_reg_endian(const struct regmap_bus *bus,
514 const struct regmap_config *config)
515{
516 enum regmap_endian endian;
517
518 /* Retrieve the endianness specification from the regmap config */
519 endian = config->reg_format_endian;
520
521 /* If the regmap config specified a non-default value, use that */
522 if (endian != REGMAP_ENDIAN_DEFAULT)
523 return endian;
524
525 /* Retrieve the endianness specification from the bus config */
526 if (bus && bus->reg_format_endian_default)
527 endian = bus->reg_format_endian_default;
d647c199 528
cf673fbc
GU
529 /* If the bus specified a non-default value, use that */
530 if (endian != REGMAP_ENDIAN_DEFAULT)
531 return endian;
532
533 /* Use this if no other value was found */
534 return REGMAP_ENDIAN_BIG;
535}
536
3c174d29
GR
537enum regmap_endian regmap_get_val_endian(struct device *dev,
538 const struct regmap_bus *bus,
539 const struct regmap_config *config)
d647c199 540{
6e64b6cc 541 struct device_node *np;
cf673fbc 542 enum regmap_endian endian;
d647c199 543
45e1a279 544 /* Retrieve the endianness specification from the regmap config */
cf673fbc 545 endian = config->val_format_endian;
d647c199 546
45e1a279 547 /* If the regmap config specified a non-default value, use that */
cf673fbc
GU
548 if (endian != REGMAP_ENDIAN_DEFAULT)
549 return endian;
d647c199 550
6e64b6cc
PD
551 /* If the dev and dev->of_node exist try to get endianness from DT */
552 if (dev && dev->of_node) {
553 np = dev->of_node;
d647c199 554
6e64b6cc
PD
555 /* Parse the device's DT node for an endianness specification */
556 if (of_property_read_bool(np, "big-endian"))
557 endian = REGMAP_ENDIAN_BIG;
558 else if (of_property_read_bool(np, "little-endian"))
559 endian = REGMAP_ENDIAN_LITTLE;
560
561 /* If the endianness was specified in DT, use that */
562 if (endian != REGMAP_ENDIAN_DEFAULT)
563 return endian;
564 }
45e1a279
SW
565
566 /* Retrieve the endianness specification from the bus config */
cf673fbc
GU
567 if (bus && bus->val_format_endian_default)
568 endian = bus->val_format_endian_default;
d647c199 569
45e1a279 570 /* If the bus specified a non-default value, use that */
cf673fbc
GU
571 if (endian != REGMAP_ENDIAN_DEFAULT)
572 return endian;
45e1a279
SW
573
574 /* Use this if no other value was found */
cf673fbc 575 return REGMAP_ENDIAN_BIG;
d647c199 576}
3c174d29 577EXPORT_SYMBOL_GPL(regmap_get_val_endian);
d647c199 578
3cfe7a74
NB
579struct regmap *__regmap_init(struct device *dev,
580 const struct regmap_bus *bus,
581 void *bus_context,
582 const struct regmap_config *config,
583 struct lock_class_key *lock_key,
584 const char *lock_name)
b83a313b 585{
6cfec04b 586 struct regmap *map;
b83a313b 587 int ret = -EINVAL;
141eba2e 588 enum regmap_endian reg_endian, val_endian;
6863ca62 589 int i, j;
b83a313b 590
d2a5884a 591 if (!config)
abbb18fb 592 goto err;
b83a313b
MB
593
594 map = kzalloc(sizeof(*map), GFP_KERNEL);
595 if (map == NULL) {
596 ret = -ENOMEM;
597 goto err;
598 }
599
0d4529c5
DC
600 if (config->lock && config->unlock) {
601 map->lock = config->lock;
602 map->unlock = config->unlock;
603 map->lock_arg = config->lock_arg;
bacdbe07 604 } else {
d2a5884a
AS
605 if ((bus && bus->fast_io) ||
606 config->fast_io) {
0d4529c5
DC
607 spin_lock_init(&map->spinlock);
608 map->lock = regmap_lock_spinlock;
609 map->unlock = regmap_unlock_spinlock;
3cfe7a74
NB
610 lockdep_set_class_and_name(&map->spinlock,
611 lock_key, lock_name);
0d4529c5
DC
612 } else {
613 mutex_init(&map->mutex);
614 map->lock = regmap_lock_mutex;
615 map->unlock = regmap_unlock_mutex;
3cfe7a74
NB
616 lockdep_set_class_and_name(&map->mutex,
617 lock_key, lock_name);
0d4529c5
DC
618 }
619 map->lock_arg = map;
bacdbe07 620 }
b4a21fc2
SB
621
622 /*
623 * When we write in fast-paths with regmap_bulk_write() don't allocate
624 * scratch buffers with sleeping allocations.
625 */
626 if ((bus && bus->fast_io) || config->fast_io)
627 map->alloc_flags = GFP_ATOMIC;
628 else
629 map->alloc_flags = GFP_KERNEL;
630
c212accc 631 map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8);
82159ba8 632 map->format.pad_bytes = config->pad_bits / 8;
c212accc 633 map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8);
5494a98f
FE
634 map->format.buf_size = DIV_ROUND_UP(config->reg_bits +
635 config->val_bits + config->pad_bits, 8);
d939fb9a 636 map->reg_shift = config->pad_bits % 8;
f01ee60f
SW
637 if (config->reg_stride)
638 map->reg_stride = config->reg_stride;
639 else
640 map->reg_stride = 1;
67921a1a
MP
641 map->use_single_read = config->use_single_rw || !bus || !bus->read;
642 map->use_single_write = config->use_single_rw || !bus || !bus->write;
9c9f7f67 643 map->can_multi_write = config->can_multi_write && bus && bus->write;
17649c90
SS
644 if (bus) {
645 map->max_raw_read = bus->max_raw_read;
646 map->max_raw_write = bus->max_raw_write;
647 }
b83a313b
MB
648 map->dev = dev;
649 map->bus = bus;
0135bbcc 650 map->bus_context = bus_context;
2e2ae66d 651 map->max_register = config->max_register;
76aad392
DC
652 map->wr_table = config->wr_table;
653 map->rd_table = config->rd_table;
654 map->volatile_table = config->volatile_table;
655 map->precious_table = config->precious_table;
2e2ae66d
MB
656 map->writeable_reg = config->writeable_reg;
657 map->readable_reg = config->readable_reg;
658 map->volatile_reg = config->volatile_reg;
2efe1642 659 map->precious_reg = config->precious_reg;
5d1729e7 660 map->cache_type = config->cache_type;
72b39f6f 661 map->name = config->name;
b83a313b 662
0d509f2b
MB
663 spin_lock_init(&map->async_lock);
664 INIT_LIST_HEAD(&map->async_list);
7e09a979 665 INIT_LIST_HEAD(&map->async_free);
0d509f2b
MB
666 init_waitqueue_head(&map->async_waitq);
667
6f306441
LPC
668 if (config->read_flag_mask || config->write_flag_mask) {
669 map->read_flag_mask = config->read_flag_mask;
670 map->write_flag_mask = config->write_flag_mask;
d2a5884a 671 } else if (bus) {
6f306441
LPC
672 map->read_flag_mask = bus->read_flag_mask;
673 }
674
d2a5884a
AS
675 if (!bus) {
676 map->reg_read = config->reg_read;
677 map->reg_write = config->reg_write;
678
3ac17037
BB
679 map->defer_caching = false;
680 goto skip_format_initialization;
681 } else if (!bus->read || !bus->write) {
682 map->reg_read = _regmap_bus_reg_read;
683 map->reg_write = _regmap_bus_reg_write;
684
d2a5884a
AS
685 map->defer_caching = false;
686 goto skip_format_initialization;
687 } else {
688 map->reg_read = _regmap_bus_read;
77792b11 689 map->reg_update_bits = bus->reg_update_bits;
d2a5884a 690 }
ad278406 691
cf673fbc
GU
692 reg_endian = regmap_get_reg_endian(bus, config);
693 val_endian = regmap_get_val_endian(dev, bus, config);
141eba2e 694
d939fb9a 695 switch (config->reg_bits + map->reg_shift) {
9aa50750
WS
696 case 2:
697 switch (config->val_bits) {
698 case 6:
699 map->format.format_write = regmap_format_2_6_write;
700 break;
701 default:
702 goto err_map;
703 }
704 break;
705
b83a313b
MB
706 case 4:
707 switch (config->val_bits) {
708 case 12:
709 map->format.format_write = regmap_format_4_12_write;
710 break;
711 default:
712 goto err_map;
713 }
714 break;
715
716 case 7:
717 switch (config->val_bits) {
718 case 9:
719 map->format.format_write = regmap_format_7_9_write;
720 break;
721 default:
722 goto err_map;
723 }
724 break;
725
7e5ec63e
LPC
726 case 10:
727 switch (config->val_bits) {
728 case 14:
729 map->format.format_write = regmap_format_10_14_write;
730 break;
731 default:
732 goto err_map;
733 }
734 break;
735
b83a313b
MB
736 case 8:
737 map->format.format_reg = regmap_format_8;
738 break;
739
740 case 16:
141eba2e
SW
741 switch (reg_endian) {
742 case REGMAP_ENDIAN_BIG:
743 map->format.format_reg = regmap_format_16_be;
744 break;
745 case REGMAP_ENDIAN_NATIVE:
746 map->format.format_reg = regmap_format_16_native;
747 break;
748 default:
749 goto err_map;
750 }
b83a313b
MB
751 break;
752
237019e7
LPC
753 case 24:
754 if (reg_endian != REGMAP_ENDIAN_BIG)
755 goto err_map;
756 map->format.format_reg = regmap_format_24;
757 break;
758
7d5e525b 759 case 32:
141eba2e
SW
760 switch (reg_endian) {
761 case REGMAP_ENDIAN_BIG:
762 map->format.format_reg = regmap_format_32_be;
763 break;
764 case REGMAP_ENDIAN_NATIVE:
765 map->format.format_reg = regmap_format_32_native;
766 break;
767 default:
768 goto err_map;
769 }
7d5e525b
MB
770 break;
771
afcc00b9
XL
772#ifdef CONFIG_64BIT
773 case 64:
774 switch (reg_endian) {
775 case REGMAP_ENDIAN_BIG:
776 map->format.format_reg = regmap_format_64_be;
777 break;
778 case REGMAP_ENDIAN_NATIVE:
779 map->format.format_reg = regmap_format_64_native;
780 break;
781 default:
782 goto err_map;
783 }
784 break;
785#endif
786
b83a313b
MB
787 default:
788 goto err_map;
789 }
790
8a819ff8
MB
791 if (val_endian == REGMAP_ENDIAN_NATIVE)
792 map->format.parse_inplace = regmap_parse_inplace_noop;
793
b83a313b
MB
794 switch (config->val_bits) {
795 case 8:
796 map->format.format_val = regmap_format_8;
797 map->format.parse_val = regmap_parse_8;
8a819ff8 798 map->format.parse_inplace = regmap_parse_inplace_noop;
b83a313b
MB
799 break;
800 case 16:
141eba2e
SW
801 switch (val_endian) {
802 case REGMAP_ENDIAN_BIG:
803 map->format.format_val = regmap_format_16_be;
804 map->format.parse_val = regmap_parse_16_be;
8a819ff8 805 map->format.parse_inplace = regmap_parse_16_be_inplace;
141eba2e 806 break;
4aa8c069
XL
807 case REGMAP_ENDIAN_LITTLE:
808 map->format.format_val = regmap_format_16_le;
809 map->format.parse_val = regmap_parse_16_le;
810 map->format.parse_inplace = regmap_parse_16_le_inplace;
811 break;
141eba2e
SW
812 case REGMAP_ENDIAN_NATIVE:
813 map->format.format_val = regmap_format_16_native;
814 map->format.parse_val = regmap_parse_16_native;
815 break;
816 default:
817 goto err_map;
818 }
b83a313b 819 break;
ea279fc5 820 case 24:
141eba2e
SW
821 if (val_endian != REGMAP_ENDIAN_BIG)
822 goto err_map;
ea279fc5
MR
823 map->format.format_val = regmap_format_24;
824 map->format.parse_val = regmap_parse_24;
825 break;
7d5e525b 826 case 32:
141eba2e
SW
827 switch (val_endian) {
828 case REGMAP_ENDIAN_BIG:
829 map->format.format_val = regmap_format_32_be;
830 map->format.parse_val = regmap_parse_32_be;
8a819ff8 831 map->format.parse_inplace = regmap_parse_32_be_inplace;
141eba2e 832 break;
4aa8c069
XL
833 case REGMAP_ENDIAN_LITTLE:
834 map->format.format_val = regmap_format_32_le;
835 map->format.parse_val = regmap_parse_32_le;
836 map->format.parse_inplace = regmap_parse_32_le_inplace;
837 break;
141eba2e
SW
838 case REGMAP_ENDIAN_NATIVE:
839 map->format.format_val = regmap_format_32_native;
840 map->format.parse_val = regmap_parse_32_native;
841 break;
842 default:
843 goto err_map;
844 }
7d5e525b 845 break;
afcc00b9
XL
846#ifdef CONFIG_64BIT
847 switch (val_endian) {
848 case REGMAP_ENDIAN_BIG:
849 map->format.format_val = regmap_format_64_be;
850 map->format.parse_val = regmap_parse_64_be;
851 map->format.parse_inplace = regmap_parse_64_be_inplace;
852 break;
853 case REGMAP_ENDIAN_LITTLE:
854 map->format.format_val = regmap_format_64_le;
855 map->format.parse_val = regmap_parse_64_le;
856 map->format.parse_inplace = regmap_parse_64_le_inplace;
857 break;
858 case REGMAP_ENDIAN_NATIVE:
859 map->format.format_val = regmap_format_64_native;
860 map->format.parse_val = regmap_parse_64_native;
861 break;
862 default:
863 goto err_map;
864 }
865 break;
866#endif
b83a313b
MB
867 }
868
141eba2e
SW
869 if (map->format.format_write) {
870 if ((reg_endian != REGMAP_ENDIAN_BIG) ||
871 (val_endian != REGMAP_ENDIAN_BIG))
872 goto err_map;
67921a1a 873 map->use_single_write = true;
141eba2e 874 }
7a647614 875
b83a313b
MB
876 if (!map->format.format_write &&
877 !(map->format.format_reg && map->format.format_val))
878 goto err_map;
879
82159ba8 880 map->work_buf = kzalloc(map->format.buf_size, GFP_KERNEL);
b83a313b
MB
881 if (map->work_buf == NULL) {
882 ret = -ENOMEM;
5204f5e3 883 goto err_map;
b83a313b
MB
884 }
885
d2a5884a
AS
886 if (map->format.format_write) {
887 map->defer_caching = false;
07c320dc 888 map->reg_write = _regmap_bus_formatted_write;
d2a5884a
AS
889 } else if (map->format.format_val) {
890 map->defer_caching = true;
07c320dc 891 map->reg_write = _regmap_bus_raw_write;
d2a5884a
AS
892 }
893
894skip_format_initialization:
07c320dc 895
6863ca62 896 map->range_tree = RB_ROOT;
e3549cd0 897 for (i = 0; i < config->num_ranges; i++) {
6863ca62
KG
898 const struct regmap_range_cfg *range_cfg = &config->ranges[i];
899 struct regmap_range_node *new;
900
901 /* Sanity check */
061adc06
MB
902 if (range_cfg->range_max < range_cfg->range_min) {
903 dev_err(map->dev, "Invalid range %d: %d < %d\n", i,
904 range_cfg->range_max, range_cfg->range_min);
6863ca62 905 goto err_range;
061adc06
MB
906 }
907
908 if (range_cfg->range_max > map->max_register) {
909 dev_err(map->dev, "Invalid range %d: %d > %d\n", i,
910 range_cfg->range_max, map->max_register);
911 goto err_range;
912 }
913
914 if (range_cfg->selector_reg > map->max_register) {
915 dev_err(map->dev,
916 "Invalid range %d: selector out of map\n", i);
917 goto err_range;
918 }
919
920 if (range_cfg->window_len == 0) {
921 dev_err(map->dev, "Invalid range %d: window_len 0\n",
922 i);
923 goto err_range;
924 }
6863ca62
KG
925
926 /* Make sure, that this register range has no selector
927 or data window within its boundary */
e3549cd0 928 for (j = 0; j < config->num_ranges; j++) {
6863ca62
KG
929 unsigned sel_reg = config->ranges[j].selector_reg;
930 unsigned win_min = config->ranges[j].window_start;
931 unsigned win_max = win_min +
932 config->ranges[j].window_len - 1;
933
f161d220
PZ
934 /* Allow data window inside its own virtual range */
935 if (j == i)
936 continue;
937
6863ca62
KG
938 if (range_cfg->range_min <= sel_reg &&
939 sel_reg <= range_cfg->range_max) {
061adc06
MB
940 dev_err(map->dev,
941 "Range %d: selector for %d in window\n",
942 i, j);
6863ca62
KG
943 goto err_range;
944 }
945
946 if (!(win_max < range_cfg->range_min ||
947 win_min > range_cfg->range_max)) {
061adc06
MB
948 dev_err(map->dev,
949 "Range %d: window for %d in window\n",
950 i, j);
6863ca62
KG
951 goto err_range;
952 }
953 }
954
955 new = kzalloc(sizeof(*new), GFP_KERNEL);
956 if (new == NULL) {
957 ret = -ENOMEM;
958 goto err_range;
959 }
960
4b020b3f 961 new->map = map;
d058bb49 962 new->name = range_cfg->name;
6863ca62
KG
963 new->range_min = range_cfg->range_min;
964 new->range_max = range_cfg->range_max;
965 new->selector_reg = range_cfg->selector_reg;
966 new->selector_mask = range_cfg->selector_mask;
967 new->selector_shift = range_cfg->selector_shift;
968 new->window_start = range_cfg->window_start;
969 new->window_len = range_cfg->window_len;
970
53e87f88 971 if (!_regmap_range_add(map, new)) {
061adc06 972 dev_err(map->dev, "Failed to add range %d\n", i);
6863ca62
KG
973 kfree(new);
974 goto err_range;
975 }
976
977 if (map->selector_work_buf == NULL) {
978 map->selector_work_buf =
979 kzalloc(map->format.buf_size, GFP_KERNEL);
980 if (map->selector_work_buf == NULL) {
981 ret = -ENOMEM;
982 goto err_range;
983 }
984 }
985 }
052d2cd1 986
e5e3b8ab 987 ret = regcache_init(map, config);
0ff3e62f 988 if (ret != 0)
6863ca62
KG
989 goto err_range;
990
a7a037c8 991 if (dev) {
6cfec04b
MS
992 ret = regmap_attach_dev(dev, map, config);
993 if (ret != 0)
994 goto err_regcache;
a7a037c8 995 }
72b39f6f 996
b83a313b
MB
997 return map;
998
6cfec04b 999err_regcache:
72b39f6f 1000 regcache_exit(map);
6863ca62
KG
1001err_range:
1002 regmap_range_exit(map);
58072cbf 1003 kfree(map->work_buf);
b83a313b
MB
1004err_map:
1005 kfree(map);
1006err:
1007 return ERR_PTR(ret);
1008}
3cfe7a74 1009EXPORT_SYMBOL_GPL(__regmap_init);
b83a313b 1010
c0eb4676
MB
1011static void devm_regmap_release(struct device *dev, void *res)
1012{
1013 regmap_exit(*(struct regmap **)res);
1014}
1015
3cfe7a74
NB
1016struct regmap *__devm_regmap_init(struct device *dev,
1017 const struct regmap_bus *bus,
1018 void *bus_context,
1019 const struct regmap_config *config,
1020 struct lock_class_key *lock_key,
1021 const char *lock_name)
c0eb4676
MB
1022{
1023 struct regmap **ptr, *regmap;
1024
1025 ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL);
1026 if (!ptr)
1027 return ERR_PTR(-ENOMEM);
1028
3cfe7a74
NB
1029 regmap = __regmap_init(dev, bus, bus_context, config,
1030 lock_key, lock_name);
c0eb4676
MB
1031 if (!IS_ERR(regmap)) {
1032 *ptr = regmap;
1033 devres_add(dev, ptr);
1034 } else {
1035 devres_free(ptr);
1036 }
1037
1038 return regmap;
1039}
3cfe7a74 1040EXPORT_SYMBOL_GPL(__devm_regmap_init);
c0eb4676 1041
67252287
SK
1042static void regmap_field_init(struct regmap_field *rm_field,
1043 struct regmap *regmap, struct reg_field reg_field)
1044{
67252287
SK
1045 rm_field->regmap = regmap;
1046 rm_field->reg = reg_field.reg;
1047 rm_field->shift = reg_field.lsb;
921cc294 1048 rm_field->mask = GENMASK(reg_field.msb, reg_field.lsb);
a0102375
KM
1049 rm_field->id_size = reg_field.id_size;
1050 rm_field->id_offset = reg_field.id_offset;
67252287
SK
1051}
1052
1053/**
1054 * devm_regmap_field_alloc(): Allocate and initialise a register field
1055 * in a register map.
1056 *
1057 * @dev: Device that will be interacted with
1058 * @regmap: regmap bank in which this register field is located.
1059 * @reg_field: Register field with in the bank.
1060 *
1061 * The return value will be an ERR_PTR() on error or a valid pointer
1062 * to a struct regmap_field. The regmap_field will be automatically freed
1063 * by the device management code.
1064 */
1065struct regmap_field *devm_regmap_field_alloc(struct device *dev,
1066 struct regmap *regmap, struct reg_field reg_field)
1067{
1068 struct regmap_field *rm_field = devm_kzalloc(dev,
1069 sizeof(*rm_field), GFP_KERNEL);
1070 if (!rm_field)
1071 return ERR_PTR(-ENOMEM);
1072
1073 regmap_field_init(rm_field, regmap, reg_field);
1074
1075 return rm_field;
1076
1077}
1078EXPORT_SYMBOL_GPL(devm_regmap_field_alloc);
1079
1080/**
1081 * devm_regmap_field_free(): Free register field allocated using
1082 * devm_regmap_field_alloc. Usally drivers need not call this function,
1083 * as the memory allocated via devm will be freed as per device-driver
1084 * life-cyle.
1085 *
1086 * @dev: Device that will be interacted with
1087 * @field: regmap field which should be freed.
1088 */
1089void devm_regmap_field_free(struct device *dev,
1090 struct regmap_field *field)
1091{
1092 devm_kfree(dev, field);
1093}
1094EXPORT_SYMBOL_GPL(devm_regmap_field_free);
1095
1096/**
1097 * regmap_field_alloc(): Allocate and initialise a register field
1098 * in a register map.
1099 *
1100 * @regmap: regmap bank in which this register field is located.
1101 * @reg_field: Register field with in the bank.
1102 *
1103 * The return value will be an ERR_PTR() on error or a valid pointer
1104 * to a struct regmap_field. The regmap_field should be freed by the
1105 * user once its finished working with it using regmap_field_free().
1106 */
1107struct regmap_field *regmap_field_alloc(struct regmap *regmap,
1108 struct reg_field reg_field)
1109{
1110 struct regmap_field *rm_field = kzalloc(sizeof(*rm_field), GFP_KERNEL);
1111
1112 if (!rm_field)
1113 return ERR_PTR(-ENOMEM);
1114
1115 regmap_field_init(rm_field, regmap, reg_field);
1116
1117 return rm_field;
1118}
1119EXPORT_SYMBOL_GPL(regmap_field_alloc);
1120
1121/**
1122 * regmap_field_free(): Free register field allocated using regmap_field_alloc
1123 *
1124 * @field: regmap field which should be freed.
1125 */
1126void regmap_field_free(struct regmap_field *field)
1127{
1128 kfree(field);
1129}
1130EXPORT_SYMBOL_GPL(regmap_field_free);
1131
bf315173
MB
1132/**
1133 * regmap_reinit_cache(): Reinitialise the current register cache
1134 *
1135 * @map: Register map to operate on.
1136 * @config: New configuration. Only the cache data will be used.
1137 *
1138 * Discard any existing register cache for the map and initialize a
1139 * new cache. This can be used to restore the cache to defaults or to
1140 * update the cache configuration to reflect runtime discovery of the
1141 * hardware.
4d879514
DP
1142 *
1143 * No explicit locking is done here, the user needs to ensure that
1144 * this function will not race with other calls to regmap.
bf315173
MB
1145 */
1146int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
1147{
bf315173 1148 regcache_exit(map);
a24f64a6 1149 regmap_debugfs_exit(map);
bf315173
MB
1150
1151 map->max_register = config->max_register;
1152 map->writeable_reg = config->writeable_reg;
1153 map->readable_reg = config->readable_reg;
1154 map->volatile_reg = config->volatile_reg;
1155 map->precious_reg = config->precious_reg;
1156 map->cache_type = config->cache_type;
1157
d3c242e1 1158 regmap_debugfs_init(map, config->name);
a24f64a6 1159
421e8d2d
MB
1160 map->cache_bypass = false;
1161 map->cache_only = false;
1162
4d879514 1163 return regcache_init(map, config);
bf315173 1164}
752a6a5f 1165EXPORT_SYMBOL_GPL(regmap_reinit_cache);
bf315173 1166
b83a313b
MB
1167/**
1168 * regmap_exit(): Free a previously allocated register map
1169 */
1170void regmap_exit(struct regmap *map)
1171{
7e09a979
MB
1172 struct regmap_async *async;
1173
5d1729e7 1174 regcache_exit(map);
31244e39 1175 regmap_debugfs_exit(map);
6863ca62 1176 regmap_range_exit(map);
d2a5884a 1177 if (map->bus && map->bus->free_context)
0135bbcc 1178 map->bus->free_context(map->bus_context);
b83a313b 1179 kfree(map->work_buf);
7e09a979
MB
1180 while (!list_empty(&map->async_free)) {
1181 async = list_first_entry_or_null(&map->async_free,
1182 struct regmap_async,
1183 list);
1184 list_del(&async->list);
1185 kfree(async->work_buf);
1186 kfree(async);
1187 }
b83a313b
MB
1188 kfree(map);
1189}
1190EXPORT_SYMBOL_GPL(regmap_exit);
1191
72b39f6f
MB
1192static int dev_get_regmap_match(struct device *dev, void *res, void *data)
1193{
1194 struct regmap **r = res;
1195 if (!r || !*r) {
1196 WARN_ON(!r || !*r);
1197 return 0;
1198 }
1199
1200 /* If the user didn't specify a name match any */
1201 if (data)
1202 return (*r)->name == data;
1203 else
1204 return 1;
1205}
1206
1207/**
1208 * dev_get_regmap(): Obtain the regmap (if any) for a device
1209 *
1210 * @dev: Device to retrieve the map for
1211 * @name: Optional name for the register map, usually NULL.
1212 *
1213 * Returns the regmap for the device if one is present, or NULL. If
1214 * name is specified then it must match the name specified when
1215 * registering the device, if it is NULL then the first regmap found
1216 * will be used. Devices with multiple register maps are very rare,
1217 * generic code should normally not need to specify a name.
1218 */
1219struct regmap *dev_get_regmap(struct device *dev, const char *name)
1220{
1221 struct regmap **r = devres_find(dev, dev_get_regmap_release,
1222 dev_get_regmap_match, (void *)name);
1223
1224 if (!r)
1225 return NULL;
1226 return *r;
1227}
1228EXPORT_SYMBOL_GPL(dev_get_regmap);
1229
8d7d3972
TT
1230/**
1231 * regmap_get_device(): Obtain the device from a regmap
1232 *
1233 * @map: Register map to operate on.
1234 *
1235 * Returns the underlying device that the regmap has been created for.
1236 */
1237struct device *regmap_get_device(struct regmap *map)
1238{
1239 return map->dev;
1240}
fa2fbe4a 1241EXPORT_SYMBOL_GPL(regmap_get_device);
8d7d3972 1242
6863ca62 1243static int _regmap_select_page(struct regmap *map, unsigned int *reg,
98bc7dfd 1244 struct regmap_range_node *range,
6863ca62
KG
1245 unsigned int val_num)
1246{
6863ca62
KG
1247 void *orig_work_buf;
1248 unsigned int win_offset;
1249 unsigned int win_page;
1250 bool page_chg;
1251 int ret;
1252
98bc7dfd
MB
1253 win_offset = (*reg - range->range_min) % range->window_len;
1254 win_page = (*reg - range->range_min) / range->window_len;
6863ca62 1255
98bc7dfd
MB
1256 if (val_num > 1) {
1257 /* Bulk write shouldn't cross range boundary */
1258 if (*reg + val_num - 1 > range->range_max)
1259 return -EINVAL;
6863ca62 1260
98bc7dfd
MB
1261 /* ... or single page boundary */
1262 if (val_num > range->window_len - win_offset)
1263 return -EINVAL;
1264 }
6863ca62 1265
98bc7dfd
MB
1266 /* It is possible to have selector register inside data window.
1267 In that case, selector register is located on every page and
1268 it needs no page switching, when accessed alone. */
1269 if (val_num > 1 ||
1270 range->window_start + win_offset != range->selector_reg) {
1271 /* Use separate work_buf during page switching */
1272 orig_work_buf = map->work_buf;
1273 map->work_buf = map->selector_work_buf;
6863ca62 1274
98bc7dfd
MB
1275 ret = _regmap_update_bits(map, range->selector_reg,
1276 range->selector_mask,
1277 win_page << range->selector_shift,
7ff0589c 1278 &page_chg, false);
632a5b01 1279
98bc7dfd 1280 map->work_buf = orig_work_buf;
6863ca62 1281
0ff3e62f 1282 if (ret != 0)
98bc7dfd 1283 return ret;
6863ca62
KG
1284 }
1285
98bc7dfd
MB
1286 *reg = range->window_start + win_offset;
1287
6863ca62
KG
1288 return 0;
1289}
1290
584de329 1291int _regmap_raw_write(struct regmap *map, unsigned int reg,
0a819809 1292 const void *val, size_t val_len)
b83a313b 1293{
98bc7dfd 1294 struct regmap_range_node *range;
0d509f2b 1295 unsigned long flags;
6f306441 1296 u8 *u8 = map->work_buf;
0d509f2b
MB
1297 void *work_val = map->work_buf + map->format.reg_bytes +
1298 map->format.pad_bytes;
b83a313b
MB
1299 void *buf;
1300 int ret = -ENOTSUPP;
1301 size_t len;
73304781
MB
1302 int i;
1303
f1b5c5c3 1304 WARN_ON(!map->bus);
d2a5884a 1305
73304781
MB
1306 /* Check for unwritable registers before we start */
1307 if (map->writeable_reg)
1308 for (i = 0; i < val_len / map->format.val_bytes; i++)
f01ee60f
SW
1309 if (!map->writeable_reg(map->dev,
1310 reg + (i * map->reg_stride)))
73304781 1311 return -EINVAL;
b83a313b 1312
c9157198
LD
1313 if (!map->cache_bypass && map->format.parse_val) {
1314 unsigned int ival;
1315 int val_bytes = map->format.val_bytes;
1316 for (i = 0; i < val_len / val_bytes; i++) {
5a08d156 1317 ival = map->format.parse_val(val + (i * val_bytes));
f01ee60f
SW
1318 ret = regcache_write(map, reg + (i * map->reg_stride),
1319 ival);
c9157198
LD
1320 if (ret) {
1321 dev_err(map->dev,
6d04b8ac 1322 "Error in caching of register: %x ret: %d\n",
c9157198
LD
1323 reg + i, ret);
1324 return ret;
1325 }
1326 }
1327 if (map->cache_only) {
1328 map->cache_dirty = true;
1329 return 0;
1330 }
1331 }
1332
98bc7dfd
MB
1333 range = _regmap_range_lookup(map, reg);
1334 if (range) {
8a2ceac6
MB
1335 int val_num = val_len / map->format.val_bytes;
1336 int win_offset = (reg - range->range_min) % range->window_len;
1337 int win_residue = range->window_len - win_offset;
1338
1339 /* If the write goes beyond the end of the window split it */
1340 while (val_num > win_residue) {
1a61cfe3 1341 dev_dbg(map->dev, "Writing window %d/%zu\n",
8a2ceac6
MB
1342 win_residue, val_len / map->format.val_bytes);
1343 ret = _regmap_raw_write(map, reg, val, win_residue *
0a819809 1344 map->format.val_bytes);
8a2ceac6
MB
1345 if (ret != 0)
1346 return ret;
1347
1348 reg += win_residue;
1349 val_num -= win_residue;
1350 val += win_residue * map->format.val_bytes;
1351 val_len -= win_residue * map->format.val_bytes;
1352
1353 win_offset = (reg - range->range_min) %
1354 range->window_len;
1355 win_residue = range->window_len - win_offset;
1356 }
1357
1358 ret = _regmap_select_page(map, &reg, range, val_num);
0ff3e62f 1359 if (ret != 0)
98bc7dfd
MB
1360 return ret;
1361 }
6863ca62 1362
d939fb9a 1363 map->format.format_reg(map->work_buf, reg, map->reg_shift);
b83a313b 1364
6f306441
LPC
1365 u8[0] |= map->write_flag_mask;
1366
651e013e
MB
1367 /*
1368 * Essentially all I/O mechanisms will be faster with a single
1369 * buffer to write. Since register syncs often generate raw
1370 * writes of single registers optimise that case.
1371 */
1372 if (val != work_val && val_len == map->format.val_bytes) {
1373 memcpy(work_val, val, map->format.val_bytes);
1374 val = work_val;
1375 }
1376
0a819809 1377 if (map->async && map->bus->async_write) {
7e09a979 1378 struct regmap_async *async;
0d509f2b 1379
c6b570d9 1380 trace_regmap_async_write_start(map, reg, val_len);
fe7d4ccd 1381
7e09a979
MB
1382 spin_lock_irqsave(&map->async_lock, flags);
1383 async = list_first_entry_or_null(&map->async_free,
1384 struct regmap_async,
1385 list);
1386 if (async)
1387 list_del(&async->list);
1388 spin_unlock_irqrestore(&map->async_lock, flags);
1389
1390 if (!async) {
1391 async = map->bus->async_alloc();
1392 if (!async)
1393 return -ENOMEM;
1394
1395 async->work_buf = kzalloc(map->format.buf_size,
1396 GFP_KERNEL | GFP_DMA);
1397 if (!async->work_buf) {
1398 kfree(async);
1399 return -ENOMEM;
1400 }
0d509f2b
MB
1401 }
1402
0d509f2b
MB
1403 async->map = map;
1404
1405 /* If the caller supplied the value we can use it safely. */
1406 memcpy(async->work_buf, map->work_buf, map->format.pad_bytes +
1407 map->format.reg_bytes + map->format.val_bytes);
0d509f2b
MB
1408
1409 spin_lock_irqsave(&map->async_lock, flags);
1410 list_add_tail(&async->list, &map->async_list);
1411 spin_unlock_irqrestore(&map->async_lock, flags);
1412
04c50ccf
MB
1413 if (val != work_val)
1414 ret = map->bus->async_write(map->bus_context,
1415 async->work_buf,
1416 map->format.reg_bytes +
1417 map->format.pad_bytes,
1418 val, val_len, async);
1419 else
1420 ret = map->bus->async_write(map->bus_context,
1421 async->work_buf,
1422 map->format.reg_bytes +
1423 map->format.pad_bytes +
1424 val_len, NULL, 0, async);
0d509f2b
MB
1425
1426 if (ret != 0) {
1427 dev_err(map->dev, "Failed to schedule write: %d\n",
1428 ret);
1429
1430 spin_lock_irqsave(&map->async_lock, flags);
7e09a979 1431 list_move(&async->list, &map->async_free);
0d509f2b 1432 spin_unlock_irqrestore(&map->async_lock, flags);
0d509f2b 1433 }
f951b658
MB
1434
1435 return ret;
0d509f2b
MB
1436 }
1437
c6b570d9 1438 trace_regmap_hw_write_start(map, reg, val_len / map->format.val_bytes);
fb2736bb 1439
2547e201
MB
1440 /* If we're doing a single register write we can probably just
1441 * send the work_buf directly, otherwise try to do a gather
1442 * write.
1443 */
0d509f2b 1444 if (val == work_val)
0135bbcc 1445 ret = map->bus->write(map->bus_context, map->work_buf,
82159ba8
MB
1446 map->format.reg_bytes +
1447 map->format.pad_bytes +
1448 val_len);
2547e201 1449 else if (map->bus->gather_write)
0135bbcc 1450 ret = map->bus->gather_write(map->bus_context, map->work_buf,
82159ba8
MB
1451 map->format.reg_bytes +
1452 map->format.pad_bytes,
b83a313b
MB
1453 val, val_len);
1454
2547e201 1455 /* If that didn't work fall back on linearising by hand. */
b83a313b 1456 if (ret == -ENOTSUPP) {
82159ba8
MB
1457 len = map->format.reg_bytes + map->format.pad_bytes + val_len;
1458 buf = kzalloc(len, GFP_KERNEL);
b83a313b
MB
1459 if (!buf)
1460 return -ENOMEM;
1461
1462 memcpy(buf, map->work_buf, map->format.reg_bytes);
82159ba8
MB
1463 memcpy(buf + map->format.reg_bytes + map->format.pad_bytes,
1464 val, val_len);
0135bbcc 1465 ret = map->bus->write(map->bus_context, buf, len);
b83a313b
MB
1466
1467 kfree(buf);
1468 }
1469
c6b570d9 1470 trace_regmap_hw_write_done(map, reg, val_len / map->format.val_bytes);
fb2736bb 1471
b83a313b
MB
1472 return ret;
1473}
1474
221ad7f2
MB
1475/**
1476 * regmap_can_raw_write - Test if regmap_raw_write() is supported
1477 *
1478 * @map: Map to check.
1479 */
1480bool regmap_can_raw_write(struct regmap *map)
1481{
07ea400e
MP
1482 return map->bus && map->bus->write && map->format.format_val &&
1483 map->format.format_reg;
221ad7f2
MB
1484}
1485EXPORT_SYMBOL_GPL(regmap_can_raw_write);
1486
f50c9eb4
MP
1487/**
1488 * regmap_get_raw_read_max - Get the maximum size we can read
1489 *
1490 * @map: Map to check.
1491 */
1492size_t regmap_get_raw_read_max(struct regmap *map)
1493{
1494 return map->max_raw_read;
1495}
1496EXPORT_SYMBOL_GPL(regmap_get_raw_read_max);
1497
1498/**
1499 * regmap_get_raw_write_max - Get the maximum size we can read
1500 *
1501 * @map: Map to check.
1502 */
1503size_t regmap_get_raw_write_max(struct regmap *map)
1504{
1505 return map->max_raw_write;
1506}
1507EXPORT_SYMBOL_GPL(regmap_get_raw_write_max);
1508
07c320dc
AS
1509static int _regmap_bus_formatted_write(void *context, unsigned int reg,
1510 unsigned int val)
1511{
1512 int ret;
1513 struct regmap_range_node *range;
1514 struct regmap *map = context;
1515
f1b5c5c3 1516 WARN_ON(!map->bus || !map->format.format_write);
07c320dc
AS
1517
1518 range = _regmap_range_lookup(map, reg);
1519 if (range) {
1520 ret = _regmap_select_page(map, &reg, range, 1);
1521 if (ret != 0)
1522 return ret;
1523 }
1524
1525 map->format.format_write(map, reg, val);
1526
c6b570d9 1527 trace_regmap_hw_write_start(map, reg, 1);
07c320dc
AS
1528
1529 ret = map->bus->write(map->bus_context, map->work_buf,
1530 map->format.buf_size);
1531
c6b570d9 1532 trace_regmap_hw_write_done(map, reg, 1);
07c320dc
AS
1533
1534 return ret;
1535}
1536
3ac17037
BB
1537static int _regmap_bus_reg_write(void *context, unsigned int reg,
1538 unsigned int val)
1539{
1540 struct regmap *map = context;
1541
1542 return map->bus->reg_write(map->bus_context, reg, val);
1543}
1544
07c320dc
AS
1545static int _regmap_bus_raw_write(void *context, unsigned int reg,
1546 unsigned int val)
1547{
1548 struct regmap *map = context;
1549
f1b5c5c3 1550 WARN_ON(!map->bus || !map->format.format_val);
07c320dc
AS
1551
1552 map->format.format_val(map->work_buf + map->format.reg_bytes
1553 + map->format.pad_bytes, val, 0);
1554 return _regmap_raw_write(map, reg,
1555 map->work_buf +
1556 map->format.reg_bytes +
1557 map->format.pad_bytes,
0a819809 1558 map->format.val_bytes);
07c320dc
AS
1559}
1560
d2a5884a
AS
1561static inline void *_regmap_map_get_context(struct regmap *map)
1562{
1563 return (map->bus) ? map : map->bus_context;
1564}
1565
4d2dc095
DP
1566int _regmap_write(struct regmap *map, unsigned int reg,
1567 unsigned int val)
b83a313b 1568{
fb2736bb 1569 int ret;
d2a5884a 1570 void *context = _regmap_map_get_context(map);
b83a313b 1571
515f2261
IN
1572 if (!regmap_writeable(map, reg))
1573 return -EIO;
1574
d2a5884a 1575 if (!map->cache_bypass && !map->defer_caching) {
5d1729e7
DP
1576 ret = regcache_write(map, reg, val);
1577 if (ret != 0)
1578 return ret;
8ae0d7e8
MB
1579 if (map->cache_only) {
1580 map->cache_dirty = true;
5d1729e7 1581 return 0;
8ae0d7e8 1582 }
5d1729e7
DP
1583 }
1584
1044c180 1585#ifdef LOG_DEVICE
5336be84 1586 if (map->dev && strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
1044c180
MB
1587 dev_info(map->dev, "%x <= %x\n", reg, val);
1588#endif
1589
c6b570d9 1590 trace_regmap_reg_write(map, reg, val);
fb2736bb 1591
d2a5884a 1592 return map->reg_write(context, reg, val);
b83a313b
MB
1593}
1594
1595/**
1596 * regmap_write(): Write a value to a single register
1597 *
1598 * @map: Register map to write to
1599 * @reg: Register to write to
1600 * @val: Value to be written
1601 *
1602 * A value of zero will be returned on success, a negative errno will
1603 * be returned in error cases.
1604 */
1605int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
1606{
1607 int ret;
1608
f01ee60f
SW
1609 if (reg % map->reg_stride)
1610 return -EINVAL;
1611
0d4529c5 1612 map->lock(map->lock_arg);
b83a313b
MB
1613
1614 ret = _regmap_write(map, reg, val);
1615
0d4529c5 1616 map->unlock(map->lock_arg);
b83a313b
MB
1617
1618 return ret;
1619}
1620EXPORT_SYMBOL_GPL(regmap_write);
1621
915f441b
MB
1622/**
1623 * regmap_write_async(): Write a value to a single register asynchronously
1624 *
1625 * @map: Register map to write to
1626 * @reg: Register to write to
1627 * @val: Value to be written
1628 *
1629 * A value of zero will be returned on success, a negative errno will
1630 * be returned in error cases.
1631 */
1632int regmap_write_async(struct regmap *map, unsigned int reg, unsigned int val)
1633{
1634 int ret;
1635
1636 if (reg % map->reg_stride)
1637 return -EINVAL;
1638
1639 map->lock(map->lock_arg);
1640
1641 map->async = true;
1642
1643 ret = _regmap_write(map, reg, val);
1644
1645 map->async = false;
1646
1647 map->unlock(map->lock_arg);
1648
1649 return ret;
1650}
1651EXPORT_SYMBOL_GPL(regmap_write_async);
1652
b83a313b
MB
1653/**
1654 * regmap_raw_write(): Write raw values to one or more registers
1655 *
1656 * @map: Register map to write to
1657 * @reg: Initial register to write to
1658 * @val: Block of data to be written, laid out for direct transmission to the
1659 * device
1660 * @val_len: Length of data pointed to by val.
1661 *
1662 * This function is intended to be used for things like firmware
1663 * download where a large block of data needs to be transferred to the
1664 * device. No formatting will be done on the data provided.
1665 *
1666 * A value of zero will be returned on success, a negative errno will
1667 * be returned in error cases.
1668 */
1669int regmap_raw_write(struct regmap *map, unsigned int reg,
1670 const void *val, size_t val_len)
1671{
1672 int ret;
1673
221ad7f2 1674 if (!regmap_can_raw_write(map))
d2a5884a 1675 return -EINVAL;
851960ba
SW
1676 if (val_len % map->format.val_bytes)
1677 return -EINVAL;
c335931e
MP
1678 if (map->max_raw_write && map->max_raw_write > val_len)
1679 return -E2BIG;
851960ba 1680
0d4529c5 1681 map->lock(map->lock_arg);
b83a313b 1682
0a819809 1683 ret = _regmap_raw_write(map, reg, val, val_len);
b83a313b 1684
0d4529c5 1685 map->unlock(map->lock_arg);
b83a313b
MB
1686
1687 return ret;
1688}
1689EXPORT_SYMBOL_GPL(regmap_raw_write);
1690
67252287
SK
1691/**
1692 * regmap_field_write(): Write a value to a single register field
1693 *
1694 * @field: Register field to write to
1695 * @val: Value to be written
1696 *
1697 * A value of zero will be returned on success, a negative errno will
1698 * be returned in error cases.
1699 */
1700int regmap_field_write(struct regmap_field *field, unsigned int val)
1701{
1702 return regmap_update_bits(field->regmap, field->reg,
1703 field->mask, val << field->shift);
1704}
1705EXPORT_SYMBOL_GPL(regmap_field_write);
1706
fdf20029
KM
1707/**
1708 * regmap_field_update_bits(): Perform a read/modify/write cycle
1709 * on the register field
1710 *
1711 * @field: Register field to write to
1712 * @mask: Bitmask to change
1713 * @val: Value to be written
1714 *
1715 * A value of zero will be returned on success, a negative errno will
1716 * be returned in error cases.
1717 */
1718int regmap_field_update_bits(struct regmap_field *field, unsigned int mask, unsigned int val)
1719{
1720 mask = (mask << field->shift) & field->mask;
1721
1722 return regmap_update_bits(field->regmap, field->reg,
1723 mask, val << field->shift);
1724}
1725EXPORT_SYMBOL_GPL(regmap_field_update_bits);
1726
a0102375
KM
1727/**
1728 * regmap_fields_write(): Write a value to a single register field with port ID
1729 *
1730 * @field: Register field to write to
1731 * @id: port ID
1732 * @val: Value to be written
1733 *
1734 * A value of zero will be returned on success, a negative errno will
1735 * be returned in error cases.
1736 */
1737int regmap_fields_write(struct regmap_field *field, unsigned int id,
1738 unsigned int val)
1739{
1740 if (id >= field->id_size)
1741 return -EINVAL;
1742
1743 return regmap_update_bits(field->regmap,
1744 field->reg + (field->id_offset * id),
1745 field->mask, val << field->shift);
1746}
1747EXPORT_SYMBOL_GPL(regmap_fields_write);
1748
e874e6c7
KM
1749int regmap_fields_force_write(struct regmap_field *field, unsigned int id,
1750 unsigned int val)
1751{
1752 if (id >= field->id_size)
1753 return -EINVAL;
1754
1755 return regmap_write_bits(field->regmap,
1756 field->reg + (field->id_offset * id),
1757 field->mask, val << field->shift);
1758}
1759EXPORT_SYMBOL_GPL(regmap_fields_force_write);
1760
a0102375
KM
1761/**
1762 * regmap_fields_update_bits(): Perform a read/modify/write cycle
1763 * on the register field
1764 *
1765 * @field: Register field to write to
1766 * @id: port ID
1767 * @mask: Bitmask to change
1768 * @val: Value to be written
1769 *
1770 * A value of zero will be returned on success, a negative errno will
1771 * be returned in error cases.
1772 */
1773int regmap_fields_update_bits(struct regmap_field *field, unsigned int id,
1774 unsigned int mask, unsigned int val)
1775{
1776 if (id >= field->id_size)
1777 return -EINVAL;
1778
1779 mask = (mask << field->shift) & field->mask;
1780
1781 return regmap_update_bits(field->regmap,
1782 field->reg + (field->id_offset * id),
1783 mask, val << field->shift);
1784}
1785EXPORT_SYMBOL_GPL(regmap_fields_update_bits);
1786
8eaeb219
LD
1787/*
1788 * regmap_bulk_write(): Write multiple registers to the device
1789 *
1790 * @map: Register map to write to
1791 * @reg: First register to be write from
1792 * @val: Block of data to be written, in native register size for device
1793 * @val_count: Number of registers to write
1794 *
1795 * This function is intended to be used for writing a large block of
31b35e9e 1796 * data to the device either in single transfer or multiple transfer.
8eaeb219
LD
1797 *
1798 * A value of zero will be returned on success, a negative errno will
1799 * be returned in error cases.
1800 */
1801int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
1802 size_t val_count)
1803{
1804 int ret = 0, i;
1805 size_t val_bytes = map->format.val_bytes;
adaac459 1806 size_t total_size = val_bytes * val_count;
8eaeb219 1807
f4298360 1808 if (map->bus && !map->format.parse_inplace)
8eaeb219 1809 return -EINVAL;
f01ee60f
SW
1810 if (reg % map->reg_stride)
1811 return -EINVAL;
8eaeb219 1812
f4298360
SB
1813 /*
1814 * Some devices don't support bulk write, for
c594b7f2
MP
1815 * them we have a series of single write operations in the first two if
1816 * blocks.
1817 *
1818 * The first if block is used for memory mapped io. It does not allow
1819 * val_bytes of 3 for example.
1820 * The second one is used for busses which do not have this limitation
1821 * and can write arbitrary value lengths.
f4298360 1822 */
c594b7f2 1823 if (!map->bus) {
4999e962 1824 map->lock(map->lock_arg);
f4298360
SB
1825 for (i = 0; i < val_count; i++) {
1826 unsigned int ival;
1827
1828 switch (val_bytes) {
1829 case 1:
1830 ival = *(u8 *)(val + (i * val_bytes));
1831 break;
1832 case 2:
1833 ival = *(u16 *)(val + (i * val_bytes));
1834 break;
1835 case 4:
1836 ival = *(u32 *)(val + (i * val_bytes));
1837 break;
1838#ifdef CONFIG_64BIT
1839 case 8:
1840 ival = *(u64 *)(val + (i * val_bytes));
1841 break;
1842#endif
1843 default:
1844 ret = -EINVAL;
1845 goto out;
1846 }
8eaeb219 1847
f4298360
SB
1848 ret = _regmap_write(map, reg + (i * map->reg_stride),
1849 ival);
1850 if (ret != 0)
1851 goto out;
1852 }
4999e962
TI
1853out:
1854 map->unlock(map->lock_arg);
adaac459
MP
1855 } else if (map->use_single_write ||
1856 (map->max_raw_write && map->max_raw_write < total_size)) {
1857 int chunk_stride = map->reg_stride;
1858 size_t chunk_size = val_bytes;
1859 size_t chunk_count = val_count;
1860
1861 if (!map->use_single_write) {
1862 chunk_size = map->max_raw_write;
1863 if (chunk_size % val_bytes)
1864 chunk_size -= chunk_size % val_bytes;
1865 chunk_count = total_size / chunk_size;
1866 chunk_stride *= chunk_size / val_bytes;
1867 }
1868
c594b7f2 1869 map->lock(map->lock_arg);
adaac459
MP
1870 /* Write as many bytes as possible with chunk_size */
1871 for (i = 0; i < chunk_count; i++) {
c594b7f2 1872 ret = _regmap_raw_write(map,
adaac459
MP
1873 reg + (i * chunk_stride),
1874 val + (i * chunk_size),
1875 chunk_size);
c594b7f2
MP
1876 if (ret)
1877 break;
1878 }
adaac459
MP
1879
1880 /* Write remaining bytes */
1881 if (!ret && chunk_size * i < total_size) {
1882 ret = _regmap_raw_write(map, reg + (i * chunk_stride),
1883 val + (i * chunk_size),
1884 total_size - i * chunk_size);
1885 }
c594b7f2 1886 map->unlock(map->lock_arg);
8eaeb219 1887 } else {
f4298360
SB
1888 void *wval;
1889
d6b41cb0
XL
1890 if (!val_count)
1891 return -EINVAL;
1892
b4a21fc2 1893 wval = kmemdup(val, val_count * val_bytes, map->alloc_flags);
8eaeb219 1894 if (!wval) {
8eaeb219 1895 dev_err(map->dev, "Error in memory allocation\n");
4999e962 1896 return -ENOMEM;
8eaeb219
LD
1897 }
1898 for (i = 0; i < val_count * val_bytes; i += val_bytes)
8a819ff8 1899 map->format.parse_inplace(wval + i);
f4298360 1900
4999e962 1901 map->lock(map->lock_arg);
0a819809 1902 ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count);
4999e962 1903 map->unlock(map->lock_arg);
8eaeb219 1904
8eaeb219 1905 kfree(wval);
f4298360 1906 }
8eaeb219
LD
1907 return ret;
1908}
1909EXPORT_SYMBOL_GPL(regmap_bulk_write);
1910
e894c3f4
OAO
1911/*
1912 * _regmap_raw_multi_reg_write()
1913 *
1914 * the (register,newvalue) pairs in regs have not been formatted, but
1915 * they are all in the same page and have been changed to being page
b486afbd 1916 * relative. The page register has been written if that was necessary.
e894c3f4
OAO
1917 */
1918static int _regmap_raw_multi_reg_write(struct regmap *map,
8019ff6c 1919 const struct reg_sequence *regs,
e894c3f4
OAO
1920 size_t num_regs)
1921{
1922 int ret;
1923 void *buf;
1924 int i;
1925 u8 *u8;
1926 size_t val_bytes = map->format.val_bytes;
1927 size_t reg_bytes = map->format.reg_bytes;
1928 size_t pad_bytes = map->format.pad_bytes;
1929 size_t pair_size = reg_bytes + pad_bytes + val_bytes;
1930 size_t len = pair_size * num_regs;
1931
f5727cd3
XL
1932 if (!len)
1933 return -EINVAL;
1934
e894c3f4
OAO
1935 buf = kzalloc(len, GFP_KERNEL);
1936 if (!buf)
1937 return -ENOMEM;
1938
1939 /* We have to linearise by hand. */
1940
1941 u8 = buf;
1942
1943 for (i = 0; i < num_regs; i++) {
2f9b660b
MP
1944 unsigned int reg = regs[i].reg;
1945 unsigned int val = regs[i].def;
c6b570d9 1946 trace_regmap_hw_write_start(map, reg, 1);
e894c3f4
OAO
1947 map->format.format_reg(u8, reg, map->reg_shift);
1948 u8 += reg_bytes + pad_bytes;
1949 map->format.format_val(u8, val, 0);
1950 u8 += val_bytes;
1951 }
1952 u8 = buf;
1953 *u8 |= map->write_flag_mask;
1954
1955 ret = map->bus->write(map->bus_context, buf, len);
1956
1957 kfree(buf);
1958
1959 for (i = 0; i < num_regs; i++) {
1960 int reg = regs[i].reg;
c6b570d9 1961 trace_regmap_hw_write_done(map, reg, 1);
e894c3f4
OAO
1962 }
1963 return ret;
1964}
1965
1966static unsigned int _regmap_register_page(struct regmap *map,
1967 unsigned int reg,
1968 struct regmap_range_node *range)
1969{
1970 unsigned int win_page = (reg - range->range_min) / range->window_len;
1971
1972 return win_page;
1973}
1974
1975static int _regmap_range_multi_paged_reg_write(struct regmap *map,
8019ff6c 1976 struct reg_sequence *regs,
e894c3f4
OAO
1977 size_t num_regs)
1978{
1979 int ret;
1980 int i, n;
8019ff6c 1981 struct reg_sequence *base;
b48d1398 1982 unsigned int this_page = 0;
2de9d600 1983 unsigned int page_change = 0;
e894c3f4
OAO
1984 /*
1985 * the set of registers are not neccessarily in order, but
1986 * since the order of write must be preserved this algorithm
2de9d600
NP
1987 * chops the set each time the page changes. This also applies
1988 * if there is a delay required at any point in the sequence.
e894c3f4
OAO
1989 */
1990 base = regs;
1991 for (i = 0, n = 0; i < num_regs; i++, n++) {
1992 unsigned int reg = regs[i].reg;
1993 struct regmap_range_node *range;
1994
1995 range = _regmap_range_lookup(map, reg);
1996 if (range) {
1997 unsigned int win_page = _regmap_register_page(map, reg,
1998 range);
1999
2000 if (i == 0)
2001 this_page = win_page;
2002 if (win_page != this_page) {
2003 this_page = win_page;
2de9d600
NP
2004 page_change = 1;
2005 }
2006 }
2007
2008 /* If we have both a page change and a delay make sure to
2009 * write the regs and apply the delay before we change the
2010 * page.
2011 */
2012
2013 if (page_change || regs[i].delay_us) {
2014
2015 /* For situations where the first write requires
2016 * a delay we need to make sure we don't call
2017 * raw_multi_reg_write with n=0
2018 * This can't occur with page breaks as we
2019 * never write on the first iteration
2020 */
2021 if (regs[i].delay_us && i == 0)
2022 n = 1;
2023
e894c3f4
OAO
2024 ret = _regmap_raw_multi_reg_write(map, base, n);
2025 if (ret != 0)
2026 return ret;
2de9d600
NP
2027
2028 if (regs[i].delay_us)
2029 udelay(regs[i].delay_us);
2030
e894c3f4
OAO
2031 base += n;
2032 n = 0;
2de9d600
NP
2033
2034 if (page_change) {
2035 ret = _regmap_select_page(map,
2036 &base[n].reg,
2037 range, 1);
2038 if (ret != 0)
2039 return ret;
2040
2041 page_change = 0;
2042 }
2043
e894c3f4 2044 }
2de9d600 2045
e894c3f4
OAO
2046 }
2047 if (n > 0)
2048 return _regmap_raw_multi_reg_write(map, base, n);
2049 return 0;
2050}
2051
1d5b40bc 2052static int _regmap_multi_reg_write(struct regmap *map,
8019ff6c 2053 const struct reg_sequence *regs,
e894c3f4 2054 size_t num_regs)
1d5b40bc 2055{
e894c3f4
OAO
2056 int i;
2057 int ret;
2058
2059 if (!map->can_multi_write) {
2060 for (i = 0; i < num_regs; i++) {
2061 ret = _regmap_write(map, regs[i].reg, regs[i].def);
2062 if (ret != 0)
2063 return ret;
2de9d600
NP
2064
2065 if (regs[i].delay_us)
2066 udelay(regs[i].delay_us);
e894c3f4
OAO
2067 }
2068 return 0;
2069 }
2070
2071 if (!map->format.parse_inplace)
2072 return -EINVAL;
2073
2074 if (map->writeable_reg)
2075 for (i = 0; i < num_regs; i++) {
2076 int reg = regs[i].reg;
2077 if (!map->writeable_reg(map->dev, reg))
2078 return -EINVAL;
2079 if (reg % map->reg_stride)
2080 return -EINVAL;
2081 }
2082
2083 if (!map->cache_bypass) {
2084 for (i = 0; i < num_regs; i++) {
2085 unsigned int val = regs[i].def;
2086 unsigned int reg = regs[i].reg;
2087 ret = regcache_write(map, reg, val);
2088 if (ret) {
2089 dev_err(map->dev,
2090 "Error in caching of register: %x ret: %d\n",
2091 reg, ret);
2092 return ret;
2093 }
2094 }
2095 if (map->cache_only) {
2096 map->cache_dirty = true;
2097 return 0;
2098 }
2099 }
2100
2101 WARN_ON(!map->bus);
1d5b40bc
CK
2102
2103 for (i = 0; i < num_regs; i++) {
e894c3f4
OAO
2104 unsigned int reg = regs[i].reg;
2105 struct regmap_range_node *range;
2de9d600
NP
2106
2107 /* Coalesce all the writes between a page break or a delay
2108 * in a sequence
2109 */
e894c3f4 2110 range = _regmap_range_lookup(map, reg);
2de9d600 2111 if (range || regs[i].delay_us) {
8019ff6c
NP
2112 size_t len = sizeof(struct reg_sequence)*num_regs;
2113 struct reg_sequence *base = kmemdup(regs, len,
e894c3f4
OAO
2114 GFP_KERNEL);
2115 if (!base)
2116 return -ENOMEM;
2117 ret = _regmap_range_multi_paged_reg_write(map, base,
2118 num_regs);
2119 kfree(base);
2120
1d5b40bc
CK
2121 return ret;
2122 }
2123 }
e894c3f4 2124 return _regmap_raw_multi_reg_write(map, regs, num_regs);
1d5b40bc
CK
2125}
2126
e33fabd3
AO
2127/*
2128 * regmap_multi_reg_write(): Write multiple registers to the device
2129 *
e894c3f4
OAO
2130 * where the set of register,value pairs are supplied in any order,
2131 * possibly not all in a single range.
e33fabd3
AO
2132 *
2133 * @map: Register map to write to
2134 * @regs: Array of structures containing register,value to be written
2135 * @num_regs: Number of registers to write
2136 *
e894c3f4
OAO
2137 * The 'normal' block write mode will send ultimately send data on the
2138 * target bus as R,V1,V2,V3,..,Vn where successively higer registers are
2139 * addressed. However, this alternative block multi write mode will send
2140 * the data as R1,V1,R2,V2,..,Rn,Vn on the target bus. The target device
2141 * must of course support the mode.
e33fabd3 2142 *
e894c3f4
OAO
2143 * A value of zero will be returned on success, a negative errno will be
2144 * returned in error cases.
e33fabd3 2145 */
8019ff6c 2146int regmap_multi_reg_write(struct regmap *map, const struct reg_sequence *regs,
f7e2cec0 2147 int num_regs)
e33fabd3 2148{
1d5b40bc 2149 int ret;
e33fabd3
AO
2150
2151 map->lock(map->lock_arg);
2152
1d5b40bc
CK
2153 ret = _regmap_multi_reg_write(map, regs, num_regs);
2154
e33fabd3
AO
2155 map->unlock(map->lock_arg);
2156
2157 return ret;
2158}
2159EXPORT_SYMBOL_GPL(regmap_multi_reg_write);
2160
1d5b40bc
CK
2161/*
2162 * regmap_multi_reg_write_bypassed(): Write multiple registers to the
2163 * device but not the cache
2164 *
e33fabd3
AO
2165 * where the set of register are supplied in any order
2166 *
2167 * @map: Register map to write to
2168 * @regs: Array of structures containing register,value to be written
2169 * @num_regs: Number of registers to write
2170 *
2171 * This function is intended to be used for writing a large block of data
2172 * atomically to the device in single transfer for those I2C client devices
2173 * that implement this alternative block write mode.
2174 *
2175 * A value of zero will be returned on success, a negative errno will
2176 * be returned in error cases.
2177 */
1d5b40bc 2178int regmap_multi_reg_write_bypassed(struct regmap *map,
8019ff6c 2179 const struct reg_sequence *regs,
1d5b40bc 2180 int num_regs)
e33fabd3 2181{
1d5b40bc
CK
2182 int ret;
2183 bool bypass;
e33fabd3
AO
2184
2185 map->lock(map->lock_arg);
2186
1d5b40bc
CK
2187 bypass = map->cache_bypass;
2188 map->cache_bypass = true;
2189
2190 ret = _regmap_multi_reg_write(map, regs, num_regs);
2191
2192 map->cache_bypass = bypass;
2193
e33fabd3
AO
2194 map->unlock(map->lock_arg);
2195
2196 return ret;
2197}
1d5b40bc 2198EXPORT_SYMBOL_GPL(regmap_multi_reg_write_bypassed);
e33fabd3 2199
0d509f2b
MB
2200/**
2201 * regmap_raw_write_async(): Write raw values to one or more registers
2202 * asynchronously
2203 *
2204 * @map: Register map to write to
2205 * @reg: Initial register to write to
2206 * @val: Block of data to be written, laid out for direct transmission to the
2207 * device. Must be valid until regmap_async_complete() is called.
2208 * @val_len: Length of data pointed to by val.
2209 *
2210 * This function is intended to be used for things like firmware
2211 * download where a large block of data needs to be transferred to the
2212 * device. No formatting will be done on the data provided.
2213 *
2214 * If supported by the underlying bus the write will be scheduled
2215 * asynchronously, helping maximise I/O speed on higher speed buses
2216 * like SPI. regmap_async_complete() can be called to ensure that all
2217 * asynchrnous writes have been completed.
2218 *
2219 * A value of zero will be returned on success, a negative errno will
2220 * be returned in error cases.
2221 */
2222int regmap_raw_write_async(struct regmap *map, unsigned int reg,
2223 const void *val, size_t val_len)
2224{
2225 int ret;
2226
2227 if (val_len % map->format.val_bytes)
2228 return -EINVAL;
2229 if (reg % map->reg_stride)
2230 return -EINVAL;
2231
2232 map->lock(map->lock_arg);
2233
0a819809
MB
2234 map->async = true;
2235
2236 ret = _regmap_raw_write(map, reg, val, val_len);
2237
2238 map->async = false;
0d509f2b
MB
2239
2240 map->unlock(map->lock_arg);
2241
2242 return ret;
2243}
2244EXPORT_SYMBOL_GPL(regmap_raw_write_async);
2245
b83a313b
MB
2246static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
2247 unsigned int val_len)
2248{
98bc7dfd 2249 struct regmap_range_node *range;
b83a313b
MB
2250 u8 *u8 = map->work_buf;
2251 int ret;
2252
f1b5c5c3 2253 WARN_ON(!map->bus);
d2a5884a 2254
98bc7dfd
MB
2255 range = _regmap_range_lookup(map, reg);
2256 if (range) {
2257 ret = _regmap_select_page(map, &reg, range,
2258 val_len / map->format.val_bytes);
0ff3e62f 2259 if (ret != 0)
98bc7dfd
MB
2260 return ret;
2261 }
6863ca62 2262
d939fb9a 2263 map->format.format_reg(map->work_buf, reg, map->reg_shift);
b83a313b
MB
2264
2265 /*
6f306441 2266 * Some buses or devices flag reads by setting the high bits in the
b486afbd 2267 * register address; since it's always the high bits for all
b83a313b
MB
2268 * current formats we can do this here rather than in
2269 * formatting. This may break if we get interesting formats.
2270 */
6f306441 2271 u8[0] |= map->read_flag_mask;
b83a313b 2272
c6b570d9 2273 trace_regmap_hw_read_start(map, reg, val_len / map->format.val_bytes);
fb2736bb 2274
0135bbcc 2275 ret = map->bus->read(map->bus_context, map->work_buf,
82159ba8 2276 map->format.reg_bytes + map->format.pad_bytes,
40c5cc26 2277 val, val_len);
b83a313b 2278
c6b570d9 2279 trace_regmap_hw_read_done(map, reg, val_len / map->format.val_bytes);
fb2736bb
MB
2280
2281 return ret;
b83a313b
MB
2282}
2283
3ac17037
BB
2284static int _regmap_bus_reg_read(void *context, unsigned int reg,
2285 unsigned int *val)
2286{
2287 struct regmap *map = context;
2288
2289 return map->bus->reg_read(map->bus_context, reg, val);
2290}
2291
ad278406
AS
2292static int _regmap_bus_read(void *context, unsigned int reg,
2293 unsigned int *val)
2294{
2295 int ret;
2296 struct regmap *map = context;
2297
2298 if (!map->format.parse_val)
2299 return -EINVAL;
2300
2301 ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
2302 if (ret == 0)
2303 *val = map->format.parse_val(map->work_buf);
2304
2305 return ret;
2306}
2307
b83a313b
MB
2308static int _regmap_read(struct regmap *map, unsigned int reg,
2309 unsigned int *val)
2310{
2311 int ret;
d2a5884a
AS
2312 void *context = _regmap_map_get_context(map);
2313
5d1729e7
DP
2314 if (!map->cache_bypass) {
2315 ret = regcache_read(map, reg, val);
2316 if (ret == 0)
2317 return 0;
2318 }
2319
2320 if (map->cache_only)
2321 return -EBUSY;
2322
d4807ad2
MS
2323 if (!regmap_readable(map, reg))
2324 return -EIO;
2325
d2a5884a 2326 ret = map->reg_read(context, reg, val);
fb2736bb 2327 if (ret == 0) {
1044c180 2328#ifdef LOG_DEVICE
5336be84 2329 if (map->dev && strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
1044c180
MB
2330 dev_info(map->dev, "%x => %x\n", reg, *val);
2331#endif
2332
c6b570d9 2333 trace_regmap_reg_read(map, reg, *val);
b83a313b 2334
ad278406
AS
2335 if (!map->cache_bypass)
2336 regcache_write(map, reg, *val);
2337 }
f2985367 2338
b83a313b
MB
2339 return ret;
2340}
2341
2342/**
2343 * regmap_read(): Read a value from a single register
2344 *
0093380c 2345 * @map: Register map to read from
b83a313b
MB
2346 * @reg: Register to be read from
2347 * @val: Pointer to store read value
2348 *
2349 * A value of zero will be returned on success, a negative errno will
2350 * be returned in error cases.
2351 */
2352int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
2353{
2354 int ret;
2355
f01ee60f
SW
2356 if (reg % map->reg_stride)
2357 return -EINVAL;
2358
0d4529c5 2359 map->lock(map->lock_arg);
b83a313b
MB
2360
2361 ret = _regmap_read(map, reg, val);
2362
0d4529c5 2363 map->unlock(map->lock_arg);
b83a313b
MB
2364
2365 return ret;
2366}
2367EXPORT_SYMBOL_GPL(regmap_read);
2368
2369/**
2370 * regmap_raw_read(): Read raw data from the device
2371 *
0093380c 2372 * @map: Register map to read from
b83a313b
MB
2373 * @reg: First register to be read from
2374 * @val: Pointer to store read value
2375 * @val_len: Size of data to read
2376 *
2377 * A value of zero will be returned on success, a negative errno will
2378 * be returned in error cases.
2379 */
2380int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
2381 size_t val_len)
2382{
b8fb5ab1
MB
2383 size_t val_bytes = map->format.val_bytes;
2384 size_t val_count = val_len / val_bytes;
2385 unsigned int v;
2386 int ret, i;
04e016ad 2387
d2a5884a
AS
2388 if (!map->bus)
2389 return -EINVAL;
851960ba
SW
2390 if (val_len % map->format.val_bytes)
2391 return -EINVAL;
f01ee60f
SW
2392 if (reg % map->reg_stride)
2393 return -EINVAL;
fa3eec77
MB
2394 if (val_count == 0)
2395 return -EINVAL;
851960ba 2396
0d4529c5 2397 map->lock(map->lock_arg);
b83a313b 2398
b8fb5ab1
MB
2399 if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
2400 map->cache_type == REGCACHE_NONE) {
9a16ea90
MP
2401 if (!map->bus->read) {
2402 ret = -ENOTSUPP;
2403 goto out;
2404 }
c335931e
MP
2405 if (map->max_raw_read && map->max_raw_read < val_len) {
2406 ret = -E2BIG;
2407 goto out;
2408 }
9a16ea90 2409
b8fb5ab1
MB
2410 /* Physical block read if there's no cache involved */
2411 ret = _regmap_raw_read(map, reg, val, val_len);
2412
2413 } else {
2414 /* Otherwise go word by word for the cache; should be low
2415 * cost as we expect to hit the cache.
2416 */
2417 for (i = 0; i < val_count; i++) {
f01ee60f
SW
2418 ret = _regmap_read(map, reg + (i * map->reg_stride),
2419 &v);
b8fb5ab1
MB
2420 if (ret != 0)
2421 goto out;
2422
d939fb9a 2423 map->format.format_val(val + (i * val_bytes), v, 0);
b8fb5ab1
MB
2424 }
2425 }
b83a313b 2426
b8fb5ab1 2427 out:
0d4529c5 2428 map->unlock(map->lock_arg);
b83a313b
MB
2429
2430 return ret;
2431}
2432EXPORT_SYMBOL_GPL(regmap_raw_read);
2433
67252287
SK
2434/**
2435 * regmap_field_read(): Read a value to a single register field
2436 *
2437 * @field: Register field to read from
2438 * @val: Pointer to store read value
2439 *
2440 * A value of zero will be returned on success, a negative errno will
2441 * be returned in error cases.
2442 */
2443int regmap_field_read(struct regmap_field *field, unsigned int *val)
2444{
2445 int ret;
2446 unsigned int reg_val;
2447 ret = regmap_read(field->regmap, field->reg, &reg_val);
2448 if (ret != 0)
2449 return ret;
2450
2451 reg_val &= field->mask;
2452 reg_val >>= field->shift;
2453 *val = reg_val;
2454
2455 return ret;
2456}
2457EXPORT_SYMBOL_GPL(regmap_field_read);
2458
a0102375
KM
2459/**
2460 * regmap_fields_read(): Read a value to a single register field with port ID
2461 *
2462 * @field: Register field to read from
2463 * @id: port ID
2464 * @val: Pointer to store read value
2465 *
2466 * A value of zero will be returned on success, a negative errno will
2467 * be returned in error cases.
2468 */
2469int regmap_fields_read(struct regmap_field *field, unsigned int id,
2470 unsigned int *val)
2471{
2472 int ret;
2473 unsigned int reg_val;
2474
2475 if (id >= field->id_size)
2476 return -EINVAL;
2477
2478 ret = regmap_read(field->regmap,
2479 field->reg + (field->id_offset * id),
2480 &reg_val);
2481 if (ret != 0)
2482 return ret;
2483
2484 reg_val &= field->mask;
2485 reg_val >>= field->shift;
2486 *val = reg_val;
2487
2488 return ret;
2489}
2490EXPORT_SYMBOL_GPL(regmap_fields_read);
2491
b83a313b
MB
2492/**
2493 * regmap_bulk_read(): Read multiple registers from the device
2494 *
0093380c 2495 * @map: Register map to read from
b83a313b
MB
2496 * @reg: First register to be read from
2497 * @val: Pointer to store read value, in native register size for device
2498 * @val_count: Number of registers to read
2499 *
2500 * A value of zero will be returned on success, a negative errno will
2501 * be returned in error cases.
2502 */
2503int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
2504 size_t val_count)
2505{
2506 int ret, i;
2507 size_t val_bytes = map->format.val_bytes;
82cd9965 2508 bool vol = regmap_volatile_range(map, reg, val_count);
5d1729e7 2509
f01ee60f
SW
2510 if (reg % map->reg_stride)
2511 return -EINVAL;
b83a313b 2512
3b58ee13 2513 if (map->bus && map->format.parse_inplace && (vol || map->cache_type == REGCACHE_NONE)) {
2e33caf1
AJ
2514 /*
2515 * Some devices does not support bulk read, for
2516 * them we have a series of single read operations.
2517 */
adaac459
MP
2518 size_t total_size = val_bytes * val_count;
2519
2520 if (!map->use_single_read &&
2521 (!map->max_raw_read || map->max_raw_read > total_size)) {
2e33caf1
AJ
2522 ret = regmap_raw_read(map, reg, val,
2523 val_bytes * val_count);
2524 if (ret != 0)
2525 return ret;
adaac459
MP
2526 } else {
2527 /*
2528 * Some devices do not support bulk read or do not
2529 * support large bulk reads, for them we have a series
2530 * of read operations.
2531 */
2532 int chunk_stride = map->reg_stride;
2533 size_t chunk_size = val_bytes;
2534 size_t chunk_count = val_count;
2535
2536 if (!map->use_single_read) {
2537 chunk_size = map->max_raw_read;
2538 if (chunk_size % val_bytes)
2539 chunk_size -= chunk_size % val_bytes;
2540 chunk_count = total_size / chunk_size;
2541 chunk_stride *= chunk_size / val_bytes;
2542 }
2543
2544 /* Read bytes that fit into a multiple of chunk_size */
2545 for (i = 0; i < chunk_count; i++) {
2546 ret = regmap_raw_read(map,
2547 reg + (i * chunk_stride),
2548 val + (i * chunk_size),
2549 chunk_size);
2550 if (ret != 0)
2551 return ret;
2552 }
2553
2554 /* Read remaining bytes */
2555 if (chunk_size * i < total_size) {
2556 ret = regmap_raw_read(map,
2557 reg + (i * chunk_stride),
2558 val + (i * chunk_size),
2559 total_size - i * chunk_size);
2560 if (ret != 0)
2561 return ret;
2562 }
2e33caf1 2563 }
de2d808f
MB
2564
2565 for (i = 0; i < val_count * val_bytes; i += val_bytes)
8a819ff8 2566 map->format.parse_inplace(val + i);
de2d808f
MB
2567 } else {
2568 for (i = 0; i < val_count; i++) {
6560ffd1 2569 unsigned int ival;
f01ee60f 2570 ret = regmap_read(map, reg + (i * map->reg_stride),
25061d28 2571 &ival);
de2d808f
MB
2572 if (ret != 0)
2573 return ret;
d5b98eb1
MB
2574
2575 if (map->format.format_val) {
2576 map->format.format_val(val + (i * val_bytes), ival, 0);
2577 } else {
2578 /* Devices providing read and write
2579 * operations can use the bulk I/O
2580 * functions if they define a val_bytes,
2581 * we assume that the values are native
2582 * endian.
2583 */
19c04788 2584#ifdef CONFIG_64BIT
afcc00b9 2585 u64 *u64 = val;
19c04788 2586#endif
d5b98eb1
MB
2587 u32 *u32 = val;
2588 u16 *u16 = val;
2589 u8 *u8 = val;
2590
2591 switch (map->format.val_bytes) {
afcc00b9
XL
2592#ifdef CONFIG_64BIT
2593 case 8:
2594 u64[i] = ival;
2595 break;
2596#endif
d5b98eb1
MB
2597 case 4:
2598 u32[i] = ival;
2599 break;
2600 case 2:
2601 u16[i] = ival;
2602 break;
2603 case 1:
2604 u8[i] = ival;
2605 break;
2606 default:
2607 return -EINVAL;
2608 }
2609 }
de2d808f
MB
2610 }
2611 }
b83a313b
MB
2612
2613 return 0;
2614}
2615EXPORT_SYMBOL_GPL(regmap_bulk_read);
2616
018690d3
MB
2617static int _regmap_update_bits(struct regmap *map, unsigned int reg,
2618 unsigned int mask, unsigned int val,
7ff0589c 2619 bool *change, bool force_write)
b83a313b
MB
2620{
2621 int ret;
d91e8db2 2622 unsigned int tmp, orig;
b83a313b 2623
77792b11
JR
2624 if (change)
2625 *change = false;
b83a313b 2626
77792b11
JR
2627 if (regmap_volatile(map, reg) && map->reg_update_bits) {
2628 ret = map->reg_update_bits(map->bus_context, reg, mask, val);
2629 if (ret == 0 && change)
e2f74dc6 2630 *change = true;
018690d3 2631 } else {
77792b11
JR
2632 ret = _regmap_read(map, reg, &orig);
2633 if (ret != 0)
2634 return ret;
2635
2636 tmp = orig & ~mask;
2637 tmp |= val & mask;
2638
2639 if (force_write || (tmp != orig)) {
2640 ret = _regmap_write(map, reg, tmp);
2641 if (ret == 0 && change)
2642 *change = true;
2643 }
018690d3 2644 }
b83a313b 2645
b83a313b
MB
2646 return ret;
2647}
018690d3
MB
2648
2649/**
2650 * regmap_update_bits: Perform a read/modify/write cycle on the register map
2651 *
2652 * @map: Register map to update
2653 * @reg: Register to update
2654 * @mask: Bitmask to change
2655 * @val: New value for bitmask
2656 *
2657 * Returns zero for success, a negative number on error.
2658 */
2659int regmap_update_bits(struct regmap *map, unsigned int reg,
2660 unsigned int mask, unsigned int val)
2661{
fc3ebd78
KG
2662 int ret;
2663
0d4529c5 2664 map->lock(map->lock_arg);
7ff0589c 2665 ret = _regmap_update_bits(map, reg, mask, val, NULL, false);
0d4529c5 2666 map->unlock(map->lock_arg);
fc3ebd78
KG
2667
2668 return ret;
018690d3 2669}
b83a313b 2670EXPORT_SYMBOL_GPL(regmap_update_bits);
31244e39 2671
fd4b7286
KM
2672/**
2673 * regmap_write_bits: Perform a read/modify/write cycle on the register map
2674 *
2675 * @map: Register map to update
2676 * @reg: Register to update
2677 * @mask: Bitmask to change
2678 * @val: New value for bitmask
2679 *
2680 * Returns zero for success, a negative number on error.
2681 */
2682int regmap_write_bits(struct regmap *map, unsigned int reg,
2683 unsigned int mask, unsigned int val)
2684{
2685 int ret;
2686
2687 map->lock(map->lock_arg);
2688 ret = _regmap_update_bits(map, reg, mask, val, NULL, true);
2689 map->unlock(map->lock_arg);
2690
2691 return ret;
2692}
2693EXPORT_SYMBOL_GPL(regmap_write_bits);
2694
915f441b
MB
2695/**
2696 * regmap_update_bits_async: Perform a read/modify/write cycle on the register
2697 * map asynchronously
2698 *
2699 * @map: Register map to update
2700 * @reg: Register to update
2701 * @mask: Bitmask to change
2702 * @val: New value for bitmask
2703 *
2704 * With most buses the read must be done synchronously so this is most
2705 * useful for devices with a cache which do not need to interact with
2706 * the hardware to determine the current register value.
2707 *
2708 * Returns zero for success, a negative number on error.
2709 */
2710int regmap_update_bits_async(struct regmap *map, unsigned int reg,
2711 unsigned int mask, unsigned int val)
2712{
915f441b
MB
2713 int ret;
2714
2715 map->lock(map->lock_arg);
2716
2717 map->async = true;
2718
7ff0589c 2719 ret = _regmap_update_bits(map, reg, mask, val, NULL, false);
915f441b
MB
2720
2721 map->async = false;
2722
2723 map->unlock(map->lock_arg);
2724
2725 return ret;
2726}
2727EXPORT_SYMBOL_GPL(regmap_update_bits_async);
2728
018690d3
MB
2729/**
2730 * regmap_update_bits_check: Perform a read/modify/write cycle on the
2731 * register map and report if updated
2732 *
2733 * @map: Register map to update
2734 * @reg: Register to update
2735 * @mask: Bitmask to change
2736 * @val: New value for bitmask
2737 * @change: Boolean indicating if a write was done
2738 *
2739 * Returns zero for success, a negative number on error.
2740 */
2741int regmap_update_bits_check(struct regmap *map, unsigned int reg,
2742 unsigned int mask, unsigned int val,
2743 bool *change)
2744{
fc3ebd78
KG
2745 int ret;
2746
0d4529c5 2747 map->lock(map->lock_arg);
7ff0589c 2748 ret = _regmap_update_bits(map, reg, mask, val, change, false);
0d4529c5 2749 map->unlock(map->lock_arg);
fc3ebd78 2750 return ret;
018690d3
MB
2751}
2752EXPORT_SYMBOL_GPL(regmap_update_bits_check);
2753
915f441b
MB
2754/**
2755 * regmap_update_bits_check_async: Perform a read/modify/write cycle on the
2756 * register map asynchronously and report if
2757 * updated
2758 *
2759 * @map: Register map to update
2760 * @reg: Register to update
2761 * @mask: Bitmask to change
2762 * @val: New value for bitmask
2763 * @change: Boolean indicating if a write was done
2764 *
2765 * With most buses the read must be done synchronously so this is most
2766 * useful for devices with a cache which do not need to interact with
2767 * the hardware to determine the current register value.
2768 *
2769 * Returns zero for success, a negative number on error.
2770 */
2771int regmap_update_bits_check_async(struct regmap *map, unsigned int reg,
2772 unsigned int mask, unsigned int val,
2773 bool *change)
2774{
2775 int ret;
2776
2777 map->lock(map->lock_arg);
2778
2779 map->async = true;
2780
7ff0589c 2781 ret = _regmap_update_bits(map, reg, mask, val, change, false);
915f441b
MB
2782
2783 map->async = false;
2784
2785 map->unlock(map->lock_arg);
2786
2787 return ret;
2788}
2789EXPORT_SYMBOL_GPL(regmap_update_bits_check_async);
2790
0d509f2b
MB
2791void regmap_async_complete_cb(struct regmap_async *async, int ret)
2792{
2793 struct regmap *map = async->map;
2794 bool wake;
2795
c6b570d9 2796 trace_regmap_async_io_complete(map);
fe7d4ccd 2797
0d509f2b 2798 spin_lock(&map->async_lock);
7e09a979 2799 list_move(&async->list, &map->async_free);
0d509f2b
MB
2800 wake = list_empty(&map->async_list);
2801
2802 if (ret != 0)
2803 map->async_ret = ret;
2804
2805 spin_unlock(&map->async_lock);
2806
0d509f2b
MB
2807 if (wake)
2808 wake_up(&map->async_waitq);
2809}
f804fb56 2810EXPORT_SYMBOL_GPL(regmap_async_complete_cb);
0d509f2b
MB
2811
2812static int regmap_async_is_done(struct regmap *map)
2813{
2814 unsigned long flags;
2815 int ret;
2816
2817 spin_lock_irqsave(&map->async_lock, flags);
2818 ret = list_empty(&map->async_list);
2819 spin_unlock_irqrestore(&map->async_lock, flags);
2820
2821 return ret;
2822}
2823
2824/**
2825 * regmap_async_complete: Ensure all asynchronous I/O has completed.
2826 *
2827 * @map: Map to operate on.
2828 *
2829 * Blocks until any pending asynchronous I/O has completed. Returns
2830 * an error code for any failed I/O operations.
2831 */
2832int regmap_async_complete(struct regmap *map)
2833{
2834 unsigned long flags;
2835 int ret;
2836
2837 /* Nothing to do with no async support */
f2e055e7 2838 if (!map->bus || !map->bus->async_write)
0d509f2b
MB
2839 return 0;
2840
c6b570d9 2841 trace_regmap_async_complete_start(map);
fe7d4ccd 2842
0d509f2b
MB
2843 wait_event(map->async_waitq, regmap_async_is_done(map));
2844
2845 spin_lock_irqsave(&map->async_lock, flags);
2846 ret = map->async_ret;
2847 map->async_ret = 0;
2848 spin_unlock_irqrestore(&map->async_lock, flags);
2849
c6b570d9 2850 trace_regmap_async_complete_done(map);
fe7d4ccd 2851
0d509f2b
MB
2852 return ret;
2853}
f88948ef 2854EXPORT_SYMBOL_GPL(regmap_async_complete);
0d509f2b 2855
22f0d90a
MB
2856/**
2857 * regmap_register_patch: Register and apply register updates to be applied
2858 * on device initialistion
2859 *
2860 * @map: Register map to apply updates to.
2861 * @regs: Values to update.
2862 * @num_regs: Number of entries in regs.
2863 *
2864 * Register a set of register updates to be applied to the device
2865 * whenever the device registers are synchronised with the cache and
2866 * apply them immediately. Typically this is used to apply
2867 * corrections to be applied to the device defaults on startup, such
2868 * as the updates some vendors provide to undocumented registers.
56fb1c74
MB
2869 *
2870 * The caller must ensure that this function cannot be called
2871 * concurrently with either itself or regcache_sync().
22f0d90a 2872 */
8019ff6c 2873int regmap_register_patch(struct regmap *map, const struct reg_sequence *regs,
22f0d90a
MB
2874 int num_regs)
2875{
8019ff6c 2876 struct reg_sequence *p;
6bf13103 2877 int ret;
22f0d90a
MB
2878 bool bypass;
2879
bd60e381
CZ
2880 if (WARN_ONCE(num_regs <= 0, "invalid registers number (%d)\n",
2881 num_regs))
2882 return 0;
2883
aab13ebc 2884 p = krealloc(map->patch,
8019ff6c 2885 sizeof(struct reg_sequence) * (map->patch_regs + num_regs),
aab13ebc
MB
2886 GFP_KERNEL);
2887 if (p) {
2888 memcpy(p + map->patch_regs, regs, num_regs * sizeof(*regs));
2889 map->patch = p;
2890 map->patch_regs += num_regs;
22f0d90a 2891 } else {
56fb1c74 2892 return -ENOMEM;
22f0d90a
MB
2893 }
2894
0d4529c5 2895 map->lock(map->lock_arg);
22f0d90a
MB
2896
2897 bypass = map->cache_bypass;
2898
2899 map->cache_bypass = true;
1a25f261 2900 map->async = true;
22f0d90a 2901
6bf13103 2902 ret = _regmap_multi_reg_write(map, regs, num_regs);
22f0d90a 2903
1a25f261 2904 map->async = false;
22f0d90a
MB
2905 map->cache_bypass = bypass;
2906
0d4529c5 2907 map->unlock(map->lock_arg);
22f0d90a 2908
1a25f261
MB
2909 regmap_async_complete(map);
2910
22f0d90a
MB
2911 return ret;
2912}
2913EXPORT_SYMBOL_GPL(regmap_register_patch);
2914
eae4b51b 2915/*
a6539c32
MB
2916 * regmap_get_val_bytes(): Report the size of a register value
2917 *
2918 * Report the size of a register value, mainly intended to for use by
2919 * generic infrastructure built on top of regmap.
2920 */
2921int regmap_get_val_bytes(struct regmap *map)
2922{
2923 if (map->format.format_write)
2924 return -EINVAL;
2925
2926 return map->format.val_bytes;
2927}
2928EXPORT_SYMBOL_GPL(regmap_get_val_bytes);
2929
668abc72
SK
2930/**
2931 * regmap_get_max_register(): Report the max register value
2932 *
2933 * Report the max register value, mainly intended to for use by
2934 * generic infrastructure built on top of regmap.
2935 */
2936int regmap_get_max_register(struct regmap *map)
2937{
2938 return map->max_register ? map->max_register : -EINVAL;
2939}
2940EXPORT_SYMBOL_GPL(regmap_get_max_register);
2941
a2f776cb
SK
2942/**
2943 * regmap_get_reg_stride(): Report the register address stride
2944 *
2945 * Report the register address stride, mainly intended to for use by
2946 * generic infrastructure built on top of regmap.
2947 */
2948int regmap_get_reg_stride(struct regmap *map)
2949{
2950 return map->reg_stride;
2951}
2952EXPORT_SYMBOL_GPL(regmap_get_reg_stride);
2953
13ff50c8
NC
2954int regmap_parse_val(struct regmap *map, const void *buf,
2955 unsigned int *val)
2956{
2957 if (!map->format.parse_val)
2958 return -EINVAL;
2959
2960 *val = map->format.parse_val(buf);
2961
2962 return 0;
2963}
2964EXPORT_SYMBOL_GPL(regmap_parse_val);
2965
31244e39
MB
2966static int __init regmap_initcall(void)
2967{
2968 regmap_debugfs_initcall();
2969
2970 return 0;
2971}
2972postcore_initcall(regmap_initcall);
This page took 0.381307 seconds and 5 git commands to generate.