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