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