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