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