regmap: Add a config option for hwspinlock
[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         hwspin_lock_free(map->hwlock);
1120 err_map:
1121         kfree(map);
1122 err:
1123         return ERR_PTR(ret);
1124 }
1125 EXPORT_SYMBOL_GPL(__regmap_init);
1126
1127 static void devm_regmap_release(struct device *dev, void *res)
1128 {
1129         regmap_exit(*(struct regmap **)res);
1130 }
1131
1132 struct regmap *__devm_regmap_init(struct device *dev,
1133                                   const struct regmap_bus *bus,
1134                                   void *bus_context,
1135                                   const struct regmap_config *config,
1136                                   struct lock_class_key *lock_key,
1137                                   const char *lock_name)
1138 {
1139         struct regmap **ptr, *regmap;
1140
1141         ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL);
1142         if (!ptr)
1143                 return ERR_PTR(-ENOMEM);
1144
1145         regmap = __regmap_init(dev, bus, bus_context, config,
1146                                lock_key, lock_name);
1147         if (!IS_ERR(regmap)) {
1148                 *ptr = regmap;
1149                 devres_add(dev, ptr);
1150         } else {
1151                 devres_free(ptr);
1152         }
1153
1154         return regmap;
1155 }
1156 EXPORT_SYMBOL_GPL(__devm_regmap_init);
1157
1158 static void regmap_field_init(struct regmap_field *rm_field,
1159         struct regmap *regmap, struct reg_field reg_field)
1160 {
1161         rm_field->regmap = regmap;
1162         rm_field->reg = reg_field.reg;
1163         rm_field->shift = reg_field.lsb;
1164         rm_field->mask = GENMASK(reg_field.msb, reg_field.lsb);
1165         rm_field->id_size = reg_field.id_size;
1166         rm_field->id_offset = reg_field.id_offset;
1167 }
1168
1169 /**
1170  * devm_regmap_field_alloc() - Allocate and initialise a register field.
1171  *
1172  * @dev: Device that will be interacted with
1173  * @regmap: regmap bank in which this register field is located.
1174  * @reg_field: Register field with in the bank.
1175  *
1176  * The return value will be an ERR_PTR() on error or a valid pointer
1177  * to a struct regmap_field. The regmap_field will be automatically freed
1178  * by the device management code.
1179  */
1180 struct regmap_field *devm_regmap_field_alloc(struct device *dev,
1181                 struct regmap *regmap, struct reg_field reg_field)
1182 {
1183         struct regmap_field *rm_field = devm_kzalloc(dev,
1184                                         sizeof(*rm_field), GFP_KERNEL);
1185         if (!rm_field)
1186                 return ERR_PTR(-ENOMEM);
1187
1188         regmap_field_init(rm_field, regmap, reg_field);
1189
1190         return rm_field;
1191
1192 }
1193 EXPORT_SYMBOL_GPL(devm_regmap_field_alloc);
1194
1195 /**
1196  * devm_regmap_field_free() - Free a register field allocated using
1197  *                            devm_regmap_field_alloc.
1198  *
1199  * @dev: Device that will be interacted with
1200  * @field: regmap field which should be freed.
1201  *
1202  * Free register field allocated using devm_regmap_field_alloc(). Usually
1203  * drivers need not call this function, as the memory allocated via devm
1204  * will be freed as per device-driver life-cyle.
1205  */
1206 void devm_regmap_field_free(struct device *dev,
1207         struct regmap_field *field)
1208 {
1209         devm_kfree(dev, field);
1210 }
1211 EXPORT_SYMBOL_GPL(devm_regmap_field_free);
1212
1213 /**
1214  * regmap_field_alloc() - Allocate and initialise a register field.
1215  *
1216  * @regmap: regmap bank in which this register field is located.
1217  * @reg_field: Register field with in the bank.
1218  *
1219  * The return value will be an ERR_PTR() on error or a valid pointer
1220  * to a struct regmap_field. The regmap_field should be freed by the
1221  * user once its finished working with it using regmap_field_free().
1222  */
1223 struct regmap_field *regmap_field_alloc(struct regmap *regmap,
1224                 struct reg_field reg_field)
1225 {
1226         struct regmap_field *rm_field = kzalloc(sizeof(*rm_field), GFP_KERNEL);
1227
1228         if (!rm_field)
1229                 return ERR_PTR(-ENOMEM);
1230
1231         regmap_field_init(rm_field, regmap, reg_field);
1232
1233         return rm_field;
1234 }
1235 EXPORT_SYMBOL_GPL(regmap_field_alloc);
1236
1237 /**
1238  * regmap_field_free() - Free register field allocated using
1239  *                       regmap_field_alloc.
1240  *
1241  * @field: regmap field which should be freed.
1242  */
1243 void regmap_field_free(struct regmap_field *field)
1244 {
1245         kfree(field);
1246 }
1247 EXPORT_SYMBOL_GPL(regmap_field_free);
1248
1249 /**
1250  * regmap_reinit_cache() - Reinitialise the current register cache
1251  *
1252  * @map: Register map to operate on.
1253  * @config: New configuration.  Only the cache data will be used.
1254  *
1255  * Discard any existing register cache for the map and initialize a
1256  * new cache.  This can be used to restore the cache to defaults or to
1257  * update the cache configuration to reflect runtime discovery of the
1258  * hardware.
1259  *
1260  * No explicit locking is done here, the user needs to ensure that
1261  * this function will not race with other calls to regmap.
1262  */
1263 int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
1264 {
1265         regcache_exit(map);
1266         regmap_debugfs_exit(map);
1267
1268         map->max_register = config->max_register;
1269         map->writeable_reg = config->writeable_reg;
1270         map->readable_reg = config->readable_reg;
1271         map->volatile_reg = config->volatile_reg;
1272         map->precious_reg = config->precious_reg;
1273         map->cache_type = config->cache_type;
1274
1275         regmap_debugfs_init(map, config->name);
1276
1277         map->cache_bypass = false;
1278         map->cache_only = false;
1279
1280         return regcache_init(map, config);
1281 }
1282 EXPORT_SYMBOL_GPL(regmap_reinit_cache);
1283
1284 /**
1285  * regmap_exit() - Free a previously allocated register map
1286  *
1287  * @map: Register map to operate on.
1288  */
1289 void regmap_exit(struct regmap *map)
1290 {
1291         struct regmap_async *async;
1292
1293         regcache_exit(map);
1294         regmap_debugfs_exit(map);
1295         regmap_range_exit(map);
1296         if (map->bus && map->bus->free_context)
1297                 map->bus->free_context(map->bus_context);
1298         kfree(map->work_buf);
1299         while (!list_empty(&map->async_free)) {
1300                 async = list_first_entry_or_null(&map->async_free,
1301                                                  struct regmap_async,
1302                                                  list);
1303                 list_del(&async->list);
1304                 kfree(async->work_buf);
1305                 kfree(async);
1306         }
1307         kfree(map);
1308 }
1309 EXPORT_SYMBOL_GPL(regmap_exit);
1310
1311 static int dev_get_regmap_match(struct device *dev, void *res, void *data)
1312 {
1313         struct regmap **r = res;
1314         if (!r || !*r) {
1315                 WARN_ON(!r || !*r);
1316                 return 0;
1317         }
1318
1319         /* If the user didn't specify a name match any */
1320         if (data)
1321                 return (*r)->name == data;
1322         else
1323                 return 1;
1324 }
1325
1326 /**
1327  * dev_get_regmap() - Obtain the regmap (if any) for a device
1328  *
1329  * @dev: Device to retrieve the map for
1330  * @name: Optional name for the register map, usually NULL.
1331  *
1332  * Returns the regmap for the device if one is present, or NULL.  If
1333  * name is specified then it must match the name specified when
1334  * registering the device, if it is NULL then the first regmap found
1335  * will be used.  Devices with multiple register maps are very rare,
1336  * generic code should normally not need to specify a name.
1337  */
1338 struct regmap *dev_get_regmap(struct device *dev, const char *name)
1339 {
1340         struct regmap **r = devres_find(dev, dev_get_regmap_release,
1341                                         dev_get_regmap_match, (void *)name);
1342
1343         if (!r)
1344                 return NULL;
1345         return *r;
1346 }
1347 EXPORT_SYMBOL_GPL(dev_get_regmap);
1348
1349 /**
1350  * regmap_get_device() - Obtain the device from a regmap
1351  *
1352  * @map: Register map to operate on.
1353  *
1354  * Returns the underlying device that the regmap has been created for.
1355  */
1356 struct device *regmap_get_device(struct regmap *map)
1357 {
1358         return map->dev;
1359 }
1360 EXPORT_SYMBOL_GPL(regmap_get_device);
1361
1362 static int _regmap_select_page(struct regmap *map, unsigned int *reg,
1363                                struct regmap_range_node *range,
1364                                unsigned int val_num)
1365 {
1366         void *orig_work_buf;
1367         unsigned int win_offset;
1368         unsigned int win_page;
1369         bool page_chg;
1370         int ret;
1371
1372         win_offset = (*reg - range->range_min) % range->window_len;
1373         win_page = (*reg - range->range_min) / range->window_len;
1374
1375         if (val_num > 1) {
1376                 /* Bulk write shouldn't cross range boundary */
1377                 if (*reg + val_num - 1 > range->range_max)
1378                         return -EINVAL;
1379
1380                 /* ... or single page boundary */
1381                 if (val_num > range->window_len - win_offset)
1382                         return -EINVAL;
1383         }
1384
1385         /* It is possible to have selector register inside data window.
1386            In that case, selector register is located on every page and
1387            it needs no page switching, when accessed alone. */
1388         if (val_num > 1 ||
1389             range->window_start + win_offset != range->selector_reg) {
1390                 /* Use separate work_buf during page switching */
1391                 orig_work_buf = map->work_buf;
1392                 map->work_buf = map->selector_work_buf;
1393
1394                 ret = _regmap_update_bits(map, range->selector_reg,
1395                                           range->selector_mask,
1396                                           win_page << range->selector_shift,
1397                                           &page_chg, false);
1398
1399                 map->work_buf = orig_work_buf;
1400
1401                 if (ret != 0)
1402                         return ret;
1403         }
1404
1405         *reg = range->window_start + win_offset;
1406
1407         return 0;
1408 }
1409
1410 static void regmap_set_work_buf_flag_mask(struct regmap *map, int max_bytes,
1411                                           unsigned long mask)
1412 {
1413         u8 *buf;
1414         int i;
1415
1416         if (!mask || !map->work_buf)
1417                 return;
1418
1419         buf = map->work_buf;
1420
1421         for (i = 0; i < max_bytes; i++)
1422                 buf[i] |= (mask >> (8 * i)) & 0xff;
1423 }
1424
1425 int _regmap_raw_write(struct regmap *map, unsigned int reg,
1426                       const void *val, size_t val_len)
1427 {
1428         struct regmap_range_node *range;
1429         unsigned long flags;
1430         void *work_val = map->work_buf + map->format.reg_bytes +
1431                 map->format.pad_bytes;
1432         void *buf;
1433         int ret = -ENOTSUPP;
1434         size_t len;
1435         int i;
1436
1437         WARN_ON(!map->bus);
1438
1439         /* Check for unwritable registers before we start */
1440         if (map->writeable_reg)
1441                 for (i = 0; i < val_len / map->format.val_bytes; i++)
1442                         if (!map->writeable_reg(map->dev,
1443                                                reg + regmap_get_offset(map, i)))
1444                                 return -EINVAL;
1445
1446         if (!map->cache_bypass && map->format.parse_val) {
1447                 unsigned int ival;
1448                 int val_bytes = map->format.val_bytes;
1449                 for (i = 0; i < val_len / val_bytes; i++) {
1450                         ival = map->format.parse_val(val + (i * val_bytes));
1451                         ret = regcache_write(map,
1452                                              reg + regmap_get_offset(map, i),
1453                                              ival);
1454                         if (ret) {
1455                                 dev_err(map->dev,
1456                                         "Error in caching of register: %x ret: %d\n",
1457                                         reg + i, ret);
1458                                 return ret;
1459                         }
1460                 }
1461                 if (map->cache_only) {
1462                         map->cache_dirty = true;
1463                         return 0;
1464                 }
1465         }
1466
1467         range = _regmap_range_lookup(map, reg);
1468         if (range) {
1469                 int val_num = val_len / map->format.val_bytes;
1470                 int win_offset = (reg - range->range_min) % range->window_len;
1471                 int win_residue = range->window_len - win_offset;
1472
1473                 /* If the write goes beyond the end of the window split it */
1474                 while (val_num > win_residue) {
1475                         dev_dbg(map->dev, "Writing window %d/%zu\n",
1476                                 win_residue, val_len / map->format.val_bytes);
1477                         ret = _regmap_raw_write(map, reg, val, win_residue *
1478                                                 map->format.val_bytes);
1479                         if (ret != 0)
1480                                 return ret;
1481
1482                         reg += win_residue;
1483                         val_num -= win_residue;
1484                         val += win_residue * map->format.val_bytes;
1485                         val_len -= win_residue * map->format.val_bytes;
1486
1487                         win_offset = (reg - range->range_min) %
1488                                 range->window_len;
1489                         win_residue = range->window_len - win_offset;
1490                 }
1491
1492                 ret = _regmap_select_page(map, &reg, range, val_num);
1493                 if (ret != 0)
1494                         return ret;
1495         }
1496
1497         map->format.format_reg(map->work_buf, reg, map->reg_shift);
1498         regmap_set_work_buf_flag_mask(map, map->format.reg_bytes,
1499                                       map->write_flag_mask);
1500
1501         /*
1502          * Essentially all I/O mechanisms will be faster with a single
1503          * buffer to write.  Since register syncs often generate raw
1504          * writes of single registers optimise that case.
1505          */
1506         if (val != work_val && val_len == map->format.val_bytes) {
1507                 memcpy(work_val, val, map->format.val_bytes);
1508                 val = work_val;
1509         }
1510
1511         if (map->async && map->bus->async_write) {
1512                 struct regmap_async *async;
1513
1514                 trace_regmap_async_write_start(map, reg, val_len);
1515
1516                 spin_lock_irqsave(&map->async_lock, flags);
1517                 async = list_first_entry_or_null(&map->async_free,
1518                                                  struct regmap_async,
1519                                                  list);
1520                 if (async)
1521                         list_del(&async->list);
1522                 spin_unlock_irqrestore(&map->async_lock, flags);
1523
1524                 if (!async) {
1525                         async = map->bus->async_alloc();
1526                         if (!async)
1527                                 return -ENOMEM;
1528
1529                         async->work_buf = kzalloc(map->format.buf_size,
1530                                                   GFP_KERNEL | GFP_DMA);
1531                         if (!async->work_buf) {
1532                                 kfree(async);
1533                                 return -ENOMEM;
1534                         }
1535                 }
1536
1537                 async->map = map;
1538
1539                 /* If the caller supplied the value we can use it safely. */
1540                 memcpy(async->work_buf, map->work_buf, map->format.pad_bytes +
1541                        map->format.reg_bytes + map->format.val_bytes);
1542
1543                 spin_lock_irqsave(&map->async_lock, flags);
1544                 list_add_tail(&async->list, &map->async_list);
1545                 spin_unlock_irqrestore(&map->async_lock, flags);
1546
1547                 if (val != work_val)
1548                         ret = map->bus->async_write(map->bus_context,
1549                                                     async->work_buf,
1550                                                     map->format.reg_bytes +
1551                                                     map->format.pad_bytes,
1552                                                     val, val_len, async);
1553                 else
1554                         ret = map->bus->async_write(map->bus_context,
1555                                                     async->work_buf,
1556                                                     map->format.reg_bytes +
1557                                                     map->format.pad_bytes +
1558                                                     val_len, NULL, 0, async);
1559
1560                 if (ret != 0) {
1561                         dev_err(map->dev, "Failed to schedule write: %d\n",
1562                                 ret);
1563
1564                         spin_lock_irqsave(&map->async_lock, flags);
1565                         list_move(&async->list, &map->async_free);
1566                         spin_unlock_irqrestore(&map->async_lock, flags);
1567                 }
1568
1569                 return ret;
1570         }
1571
1572         trace_regmap_hw_write_start(map, reg, val_len / map->format.val_bytes);
1573
1574         /* If we're doing a single register write we can probably just
1575          * send the work_buf directly, otherwise try to do a gather
1576          * write.
1577          */
1578         if (val == work_val)
1579                 ret = map->bus->write(map->bus_context, map->work_buf,
1580                                       map->format.reg_bytes +
1581                                       map->format.pad_bytes +
1582                                       val_len);
1583         else if (map->bus->gather_write)
1584                 ret = map->bus->gather_write(map->bus_context, map->work_buf,
1585                                              map->format.reg_bytes +
1586                                              map->format.pad_bytes,
1587                                              val, val_len);
1588
1589         /* If that didn't work fall back on linearising by hand. */
1590         if (ret == -ENOTSUPP) {
1591                 len = map->format.reg_bytes + map->format.pad_bytes + val_len;
1592                 buf = kzalloc(len, GFP_KERNEL);
1593                 if (!buf)
1594                         return -ENOMEM;
1595
1596                 memcpy(buf, map->work_buf, map->format.reg_bytes);
1597                 memcpy(buf + map->format.reg_bytes + map->format.pad_bytes,
1598                        val, val_len);
1599                 ret = map->bus->write(map->bus_context, buf, len);
1600
1601                 kfree(buf);
1602         } else if (ret != 0 && !map->cache_bypass && map->format.parse_val) {
1603                 /* regcache_drop_region() takes lock that we already have,
1604                  * thus call map->cache_ops->drop() directly
1605                  */
1606                 if (map->cache_ops && map->cache_ops->drop)
1607                         map->cache_ops->drop(map, reg, reg + 1);
1608         }
1609
1610         trace_regmap_hw_write_done(map, reg, val_len / map->format.val_bytes);
1611
1612         return ret;
1613 }
1614
1615 /**
1616  * regmap_can_raw_write - Test if regmap_raw_write() is supported
1617  *
1618  * @map: Map to check.
1619  */
1620 bool regmap_can_raw_write(struct regmap *map)
1621 {
1622         return map->bus && map->bus->write && map->format.format_val &&
1623                 map->format.format_reg;
1624 }
1625 EXPORT_SYMBOL_GPL(regmap_can_raw_write);
1626
1627 /**
1628  * regmap_get_raw_read_max - Get the maximum size we can read
1629  *
1630  * @map: Map to check.
1631  */
1632 size_t regmap_get_raw_read_max(struct regmap *map)
1633 {
1634         return map->max_raw_read;
1635 }
1636 EXPORT_SYMBOL_GPL(regmap_get_raw_read_max);
1637
1638 /**
1639  * regmap_get_raw_write_max - Get the maximum size we can read
1640  *
1641  * @map: Map to check.
1642  */
1643 size_t regmap_get_raw_write_max(struct regmap *map)
1644 {
1645         return map->max_raw_write;
1646 }
1647 EXPORT_SYMBOL_GPL(regmap_get_raw_write_max);
1648
1649 static int _regmap_bus_formatted_write(void *context, unsigned int reg,
1650                                        unsigned int val)
1651 {
1652         int ret;
1653         struct regmap_range_node *range;
1654         struct regmap *map = context;
1655
1656         WARN_ON(!map->bus || !map->format.format_write);
1657
1658         range = _regmap_range_lookup(map, reg);
1659         if (range) {
1660                 ret = _regmap_select_page(map, &reg, range, 1);
1661                 if (ret != 0)
1662                         return ret;
1663         }
1664
1665         map->format.format_write(map, reg, val);
1666
1667         trace_regmap_hw_write_start(map, reg, 1);
1668
1669         ret = map->bus->write(map->bus_context, map->work_buf,
1670                               map->format.buf_size);
1671
1672         trace_regmap_hw_write_done(map, reg, 1);
1673
1674         return ret;
1675 }
1676
1677 static int _regmap_bus_reg_write(void *context, unsigned int reg,
1678                                  unsigned int val)
1679 {
1680         struct regmap *map = context;
1681
1682         return map->bus->reg_write(map->bus_context, reg, val);
1683 }
1684
1685 static int _regmap_bus_raw_write(void *context, unsigned int reg,
1686                                  unsigned int val)
1687 {
1688         struct regmap *map = context;
1689
1690         WARN_ON(!map->bus || !map->format.format_val);
1691
1692         map->format.format_val(map->work_buf + map->format.reg_bytes
1693                                + map->format.pad_bytes, val, 0);
1694         return _regmap_raw_write(map, reg,
1695                                  map->work_buf +
1696                                  map->format.reg_bytes +
1697                                  map->format.pad_bytes,
1698                                  map->format.val_bytes);
1699 }
1700
1701 static inline void *_regmap_map_get_context(struct regmap *map)
1702 {
1703         return (map->bus) ? map : map->bus_context;
1704 }
1705
1706 int _regmap_write(struct regmap *map, unsigned int reg,
1707                   unsigned int val)
1708 {
1709         int ret;
1710         void *context = _regmap_map_get_context(map);
1711
1712         if (!regmap_writeable(map, reg))
1713                 return -EIO;
1714
1715         if (!map->cache_bypass && !map->defer_caching) {
1716                 ret = regcache_write(map, reg, val);
1717                 if (ret != 0)
1718                         return ret;
1719                 if (map->cache_only) {
1720                         map->cache_dirty = true;
1721                         return 0;
1722                 }
1723         }
1724
1725 #ifdef LOG_DEVICE
1726         if (map->dev && strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
1727                 dev_info(map->dev, "%x <= %x\n", reg, val);
1728 #endif
1729
1730         trace_regmap_reg_write(map, reg, val);
1731
1732         return map->reg_write(context, reg, val);
1733 }
1734
1735 /**
1736  * regmap_write() - Write a value to a single register
1737  *
1738  * @map: Register map to write to
1739  * @reg: Register to write to
1740  * @val: Value to be written
1741  *
1742  * A value of zero will be returned on success, a negative errno will
1743  * be returned in error cases.
1744  */
1745 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
1746 {
1747         int ret;
1748
1749         if (!IS_ALIGNED(reg, map->reg_stride))
1750                 return -EINVAL;
1751
1752         map->lock(map->lock_arg);
1753
1754         ret = _regmap_write(map, reg, val);
1755
1756         map->unlock(map->lock_arg);
1757
1758         return ret;
1759 }
1760 EXPORT_SYMBOL_GPL(regmap_write);
1761
1762 /**
1763  * regmap_write_async() - Write a value to a single register asynchronously
1764  *
1765  * @map: Register map to write to
1766  * @reg: Register to write to
1767  * @val: Value to be written
1768  *
1769  * A value of zero will be returned on success, a negative errno will
1770  * be returned in error cases.
1771  */
1772 int regmap_write_async(struct regmap *map, unsigned int reg, unsigned int val)
1773 {
1774         int ret;
1775
1776         if (!IS_ALIGNED(reg, map->reg_stride))
1777                 return -EINVAL;
1778
1779         map->lock(map->lock_arg);
1780
1781         map->async = true;
1782
1783         ret = _regmap_write(map, reg, val);
1784
1785         map->async = false;
1786
1787         map->unlock(map->lock_arg);
1788
1789         return ret;
1790 }
1791 EXPORT_SYMBOL_GPL(regmap_write_async);
1792
1793 /**
1794  * regmap_raw_write() - Write raw values to one or more registers
1795  *
1796  * @map: Register map to write to
1797  * @reg: Initial register to write to
1798  * @val: Block of data to be written, laid out for direct transmission to the
1799  *       device
1800  * @val_len: Length of data pointed to by val.
1801  *
1802  * This function is intended to be used for things like firmware
1803  * download where a large block of data needs to be transferred to the
1804  * device.  No formatting will be done on the data provided.
1805  *
1806  * A value of zero will be returned on success, a negative errno will
1807  * be returned in error cases.
1808  */
1809 int regmap_raw_write(struct regmap *map, unsigned int reg,
1810                      const void *val, size_t val_len)
1811 {
1812         int ret;
1813
1814         if (!regmap_can_raw_write(map))
1815                 return -EINVAL;
1816         if (val_len % map->format.val_bytes)
1817                 return -EINVAL;
1818         if (map->max_raw_write && map->max_raw_write > val_len)
1819                 return -E2BIG;
1820
1821         map->lock(map->lock_arg);
1822
1823         ret = _regmap_raw_write(map, reg, val, val_len);
1824
1825         map->unlock(map->lock_arg);
1826
1827         return ret;
1828 }
1829 EXPORT_SYMBOL_GPL(regmap_raw_write);
1830
1831 /**
1832  * regmap_field_update_bits_base() - Perform a read/modify/write cycle a
1833  *                                   register field.
1834  *
1835  * @field: Register field to write to
1836  * @mask: Bitmask to change
1837  * @val: Value to be written
1838  * @change: Boolean indicating if a write was done
1839  * @async: Boolean indicating asynchronously
1840  * @force: Boolean indicating use force update
1841  *
1842  * Perform a read/modify/write cycle on the register field with change,
1843  * async, force option.
1844  *
1845  * A value of zero will be returned on success, a negative errno will
1846  * be returned in error cases.
1847  */
1848 int regmap_field_update_bits_base(struct regmap_field *field,
1849                                   unsigned int mask, unsigned int val,
1850                                   bool *change, bool async, bool force)
1851 {
1852         mask = (mask << field->shift) & field->mask;
1853
1854         return regmap_update_bits_base(field->regmap, field->reg,
1855                                        mask, val << field->shift,
1856                                        change, async, force);
1857 }
1858 EXPORT_SYMBOL_GPL(regmap_field_update_bits_base);
1859
1860 /**
1861  * regmap_fields_update_bits_base() - Perform a read/modify/write cycle a
1862  *                                    register field with port ID
1863  *
1864  * @field: Register field to write to
1865  * @id: port ID
1866  * @mask: Bitmask to change
1867  * @val: Value to be written
1868  * @change: Boolean indicating if a write was done
1869  * @async: Boolean indicating asynchronously
1870  * @force: Boolean indicating use force update
1871  *
1872  * A value of zero will be returned on success, a negative errno will
1873  * be returned in error cases.
1874  */
1875 int regmap_fields_update_bits_base(struct regmap_field *field,  unsigned int id,
1876                                    unsigned int mask, unsigned int val,
1877                                    bool *change, bool async, bool force)
1878 {
1879         if (id >= field->id_size)
1880                 return -EINVAL;
1881
1882         mask = (mask << field->shift) & field->mask;
1883
1884         return regmap_update_bits_base(field->regmap,
1885                                        field->reg + (field->id_offset * id),
1886                                        mask, val << field->shift,
1887                                        change, async, force);
1888 }
1889 EXPORT_SYMBOL_GPL(regmap_fields_update_bits_base);
1890
1891 /**
1892  * regmap_bulk_write() - Write multiple registers to the device
1893  *
1894  * @map: Register map to write to
1895  * @reg: First register to be write from
1896  * @val: Block of data to be written, in native register size for device
1897  * @val_count: Number of registers to write
1898  *
1899  * This function is intended to be used for writing a large block of
1900  * data to the device either in single transfer or multiple transfer.
1901  *
1902  * A value of zero will be returned on success, a negative errno will
1903  * be returned in error cases.
1904  */
1905 int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
1906                      size_t val_count)
1907 {
1908         int ret = 0, i;
1909         size_t val_bytes = map->format.val_bytes;
1910         size_t total_size = val_bytes * val_count;
1911
1912         if (!IS_ALIGNED(reg, map->reg_stride))
1913                 return -EINVAL;
1914
1915         /*
1916          * Some devices don't support bulk write, for
1917          * them we have a series of single write operations in the first two if
1918          * blocks.
1919          *
1920          * The first if block is used for memory mapped io. It does not allow
1921          * val_bytes of 3 for example.
1922          * The second one is for busses that do not provide raw I/O.
1923          * The third one is used for busses which do not have these limitations
1924          * and can write arbitrary value lengths.
1925          */
1926         if (!map->bus) {
1927                 map->lock(map->lock_arg);
1928                 for (i = 0; i < val_count; i++) {
1929                         unsigned int ival;
1930
1931                         switch (val_bytes) {
1932                         case 1:
1933                                 ival = *(u8 *)(val + (i * val_bytes));
1934                                 break;
1935                         case 2:
1936                                 ival = *(u16 *)(val + (i * val_bytes));
1937                                 break;
1938                         case 4:
1939                                 ival = *(u32 *)(val + (i * val_bytes));
1940                                 break;
1941 #ifdef CONFIG_64BIT
1942                         case 8:
1943                                 ival = *(u64 *)(val + (i * val_bytes));
1944                                 break;
1945 #endif
1946                         default:
1947                                 ret = -EINVAL;
1948                                 goto out;
1949                         }
1950
1951                         ret = _regmap_write(map,
1952                                             reg + regmap_get_offset(map, i),
1953                                             ival);
1954                         if (ret != 0)
1955                                 goto out;
1956                 }
1957 out:
1958                 map->unlock(map->lock_arg);
1959         } else if (map->bus && !map->format.parse_inplace) {
1960                 const u8 *u8 = val;
1961                 const u16 *u16 = val;
1962                 const u32 *u32 = val;
1963                 unsigned int ival;
1964
1965                 for (i = 0; i < val_count; i++) {
1966                         switch (map->format.val_bytes) {
1967                         case 4:
1968                                 ival = u32[i];
1969                                 break;
1970                         case 2:
1971                                 ival = u16[i];
1972                                 break;
1973                         case 1:
1974                                 ival = u8[i];
1975                                 break;
1976                         default:
1977                                 return -EINVAL;
1978                         }
1979
1980                         ret = regmap_write(map, reg + (i * map->reg_stride),
1981                                            ival);
1982                         if (ret)
1983                                 return ret;
1984                 }
1985         } else if (map->use_single_write ||
1986                    (map->max_raw_write && map->max_raw_write < total_size)) {
1987                 int chunk_stride = map->reg_stride;
1988                 size_t chunk_size = val_bytes;
1989                 size_t chunk_count = val_count;
1990
1991                 if (!map->use_single_write) {
1992                         chunk_size = map->max_raw_write;
1993                         if (chunk_size % val_bytes)
1994                                 chunk_size -= chunk_size % val_bytes;
1995                         chunk_count = total_size / chunk_size;
1996                         chunk_stride *= chunk_size / val_bytes;
1997                 }
1998
1999                 map->lock(map->lock_arg);
2000                 /* Write as many bytes as possible with chunk_size */
2001                 for (i = 0; i < chunk_count; i++) {
2002                         ret = _regmap_raw_write(map,
2003                                                 reg + (i * chunk_stride),
2004                                                 val + (i * chunk_size),
2005                                                 chunk_size);
2006                         if (ret)
2007                                 break;
2008                 }
2009
2010                 /* Write remaining bytes */
2011                 if (!ret && chunk_size * i < total_size) {
2012                         ret = _regmap_raw_write(map, reg + (i * chunk_stride),
2013                                                 val + (i * chunk_size),
2014                                                 total_size - i * chunk_size);
2015                 }
2016                 map->unlock(map->lock_arg);
2017         } else {
2018                 void *wval;
2019
2020                 if (!val_count)
2021                         return -EINVAL;
2022
2023                 wval = kmemdup(val, val_count * val_bytes, map->alloc_flags);
2024                 if (!wval) {
2025                         dev_err(map->dev, "Error in memory allocation\n");
2026                         return -ENOMEM;
2027                 }
2028                 for (i = 0; i < val_count * val_bytes; i += val_bytes)
2029                         map->format.parse_inplace(wval + i);
2030
2031                 map->lock(map->lock_arg);
2032                 ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count);
2033                 map->unlock(map->lock_arg);
2034
2035                 kfree(wval);
2036         }
2037         return ret;
2038 }
2039 EXPORT_SYMBOL_GPL(regmap_bulk_write);
2040
2041 /*
2042  * _regmap_raw_multi_reg_write()
2043  *
2044  * the (register,newvalue) pairs in regs have not been formatted, but
2045  * they are all in the same page and have been changed to being page
2046  * relative. The page register has been written if that was necessary.
2047  */
2048 static int _regmap_raw_multi_reg_write(struct regmap *map,
2049                                        const struct reg_sequence *regs,
2050                                        size_t num_regs)
2051 {
2052         int ret;
2053         void *buf;
2054         int i;
2055         u8 *u8;
2056         size_t val_bytes = map->format.val_bytes;
2057         size_t reg_bytes = map->format.reg_bytes;
2058         size_t pad_bytes = map->format.pad_bytes;
2059         size_t pair_size = reg_bytes + pad_bytes + val_bytes;
2060         size_t len = pair_size * num_regs;
2061
2062         if (!len)
2063                 return -EINVAL;
2064
2065         buf = kzalloc(len, GFP_KERNEL);
2066         if (!buf)
2067                 return -ENOMEM;
2068
2069         /* We have to linearise by hand. */
2070
2071         u8 = buf;
2072
2073         for (i = 0; i < num_regs; i++) {
2074                 unsigned int reg = regs[i].reg;
2075                 unsigned int val = regs[i].def;
2076                 trace_regmap_hw_write_start(map, reg, 1);
2077                 map->format.format_reg(u8, reg, map->reg_shift);
2078                 u8 += reg_bytes + pad_bytes;
2079                 map->format.format_val(u8, val, 0);
2080                 u8 += val_bytes;
2081         }
2082         u8 = buf;
2083         *u8 |= map->write_flag_mask;
2084
2085         ret = map->bus->write(map->bus_context, buf, len);
2086
2087         kfree(buf);
2088
2089         for (i = 0; i < num_regs; i++) {
2090                 int reg = regs[i].reg;
2091                 trace_regmap_hw_write_done(map, reg, 1);
2092         }
2093         return ret;
2094 }
2095
2096 static unsigned int _regmap_register_page(struct regmap *map,
2097                                           unsigned int reg,
2098                                           struct regmap_range_node *range)
2099 {
2100         unsigned int win_page = (reg - range->range_min) / range->window_len;
2101
2102         return win_page;
2103 }
2104
2105 static int _regmap_range_multi_paged_reg_write(struct regmap *map,
2106                                                struct reg_sequence *regs,
2107                                                size_t num_regs)
2108 {
2109         int ret;
2110         int i, n;
2111         struct reg_sequence *base;
2112         unsigned int this_page = 0;
2113         unsigned int page_change = 0;
2114         /*
2115          * the set of registers are not neccessarily in order, but
2116          * since the order of write must be preserved this algorithm
2117          * chops the set each time the page changes. This also applies
2118          * if there is a delay required at any point in the sequence.
2119          */
2120         base = regs;
2121         for (i = 0, n = 0; i < num_regs; i++, n++) {
2122                 unsigned int reg = regs[i].reg;
2123                 struct regmap_range_node *range;
2124
2125                 range = _regmap_range_lookup(map, reg);
2126                 if (range) {
2127                         unsigned int win_page = _regmap_register_page(map, reg,
2128                                                                       range);
2129
2130                         if (i == 0)
2131                                 this_page = win_page;
2132                         if (win_page != this_page) {
2133                                 this_page = win_page;
2134                                 page_change = 1;
2135                         }
2136                 }
2137
2138                 /* If we have both a page change and a delay make sure to
2139                  * write the regs and apply the delay before we change the
2140                  * page.
2141                  */
2142
2143                 if (page_change || regs[i].delay_us) {
2144
2145                                 /* For situations where the first write requires
2146                                  * a delay we need to make sure we don't call
2147                                  * raw_multi_reg_write with n=0
2148                                  * This can't occur with page breaks as we
2149                                  * never write on the first iteration
2150                                  */
2151                                 if (regs[i].delay_us && i == 0)
2152                                         n = 1;
2153
2154                                 ret = _regmap_raw_multi_reg_write(map, base, n);
2155                                 if (ret != 0)
2156                                         return ret;
2157
2158                                 if (regs[i].delay_us)
2159                                         udelay(regs[i].delay_us);
2160
2161                                 base += n;
2162                                 n = 0;
2163
2164                                 if (page_change) {
2165                                         ret = _regmap_select_page(map,
2166                                                                   &base[n].reg,
2167                                                                   range, 1);
2168                                         if (ret != 0)
2169                                                 return ret;
2170
2171                                         page_change = 0;
2172                                 }
2173
2174                 }
2175
2176         }
2177         if (n > 0)
2178                 return _regmap_raw_multi_reg_write(map, base, n);
2179         return 0;
2180 }
2181
2182 static int _regmap_multi_reg_write(struct regmap *map,
2183                                    const struct reg_sequence *regs,
2184                                    size_t num_regs)
2185 {
2186         int i;
2187         int ret;
2188
2189         if (!map->can_multi_write) {
2190                 for (i = 0; i < num_regs; i++) {
2191                         ret = _regmap_write(map, regs[i].reg, regs[i].def);
2192                         if (ret != 0)
2193                                 return ret;
2194
2195                         if (regs[i].delay_us)
2196                                 udelay(regs[i].delay_us);
2197                 }
2198                 return 0;
2199         }
2200
2201         if (!map->format.parse_inplace)
2202                 return -EINVAL;
2203
2204         if (map->writeable_reg)
2205                 for (i = 0; i < num_regs; i++) {
2206                         int reg = regs[i].reg;
2207                         if (!map->writeable_reg(map->dev, reg))
2208                                 return -EINVAL;
2209                         if (!IS_ALIGNED(reg, map->reg_stride))
2210                                 return -EINVAL;
2211                 }
2212
2213         if (!map->cache_bypass) {
2214                 for (i = 0; i < num_regs; i++) {
2215                         unsigned int val = regs[i].def;
2216                         unsigned int reg = regs[i].reg;
2217                         ret = regcache_write(map, reg, val);
2218                         if (ret) {
2219                                 dev_err(map->dev,
2220                                 "Error in caching of register: %x ret: %d\n",
2221                                                                 reg, ret);
2222                                 return ret;
2223                         }
2224                 }
2225                 if (map->cache_only) {
2226                         map->cache_dirty = true;
2227                         return 0;
2228                 }
2229         }
2230
2231         WARN_ON(!map->bus);
2232
2233         for (i = 0; i < num_regs; i++) {
2234                 unsigned int reg = regs[i].reg;
2235                 struct regmap_range_node *range;
2236
2237                 /* Coalesce all the writes between a page break or a delay
2238                  * in a sequence
2239                  */
2240                 range = _regmap_range_lookup(map, reg);
2241                 if (range || regs[i].delay_us) {
2242                         size_t len = sizeof(struct reg_sequence)*num_regs;
2243                         struct reg_sequence *base = kmemdup(regs, len,
2244                                                            GFP_KERNEL);
2245                         if (!base)
2246                                 return -ENOMEM;
2247                         ret = _regmap_range_multi_paged_reg_write(map, base,
2248                                                                   num_regs);
2249                         kfree(base);
2250
2251                         return ret;
2252                 }
2253         }
2254         return _regmap_raw_multi_reg_write(map, regs, num_regs);
2255 }
2256
2257 /**
2258  * regmap_multi_reg_write() - Write multiple registers to the device
2259  *
2260  * @map: Register map to write to
2261  * @regs: Array of structures containing register,value to be written
2262  * @num_regs: Number of registers to write
2263  *
2264  * Write multiple registers to the device where the set of register, value
2265  * pairs are supplied in any order, possibly not all in a single range.
2266  *
2267  * The 'normal' block write mode will send ultimately send data on the
2268  * target bus as R,V1,V2,V3,..,Vn where successively higher registers are
2269  * addressed. However, this alternative block multi write mode will send
2270  * the data as R1,V1,R2,V2,..,Rn,Vn on the target bus. The target device
2271  * must of course support the mode.
2272  *
2273  * A value of zero will be returned on success, a negative errno will be
2274  * returned in error cases.
2275  */
2276 int regmap_multi_reg_write(struct regmap *map, const struct reg_sequence *regs,
2277                            int num_regs)
2278 {
2279         int ret;
2280
2281         map->lock(map->lock_arg);
2282
2283         ret = _regmap_multi_reg_write(map, regs, num_regs);
2284
2285         map->unlock(map->lock_arg);
2286
2287         return ret;
2288 }
2289 EXPORT_SYMBOL_GPL(regmap_multi_reg_write);
2290
2291 /**
2292  * regmap_multi_reg_write_bypassed() - Write multiple registers to the
2293  *                                     device but not the cache
2294  *
2295  * @map: Register map to write to
2296  * @regs: Array of structures containing register,value to be written
2297  * @num_regs: Number of registers to write
2298  *
2299  * Write multiple registers to the device but not the cache where the set
2300  * of register are supplied in any order.
2301  *
2302  * This function is intended to be used for writing a large block of data
2303  * atomically to the device in single transfer for those I2C client devices
2304  * that implement this alternative block write mode.
2305  *
2306  * A value of zero will be returned on success, a negative errno will
2307  * be returned in error cases.
2308  */
2309 int regmap_multi_reg_write_bypassed(struct regmap *map,
2310                                     const struct reg_sequence *regs,
2311                                     int num_regs)
2312 {
2313         int ret;
2314         bool bypass;
2315
2316         map->lock(map->lock_arg);
2317
2318         bypass = map->cache_bypass;
2319         map->cache_bypass = true;
2320
2321         ret = _regmap_multi_reg_write(map, regs, num_regs);
2322
2323         map->cache_bypass = bypass;
2324
2325         map->unlock(map->lock_arg);
2326
2327         return ret;
2328 }
2329 EXPORT_SYMBOL_GPL(regmap_multi_reg_write_bypassed);
2330
2331 /**
2332  * regmap_raw_write_async() - Write raw values to one or more registers
2333  *                            asynchronously
2334  *
2335  * @map: Register map to write to
2336  * @reg: Initial register to write to
2337  * @val: Block of data to be written, laid out for direct transmission to the
2338  *       device.  Must be valid until regmap_async_complete() is called.
2339  * @val_len: Length of data pointed to by val.
2340  *
2341  * This function is intended to be used for things like firmware
2342  * download where a large block of data needs to be transferred to the
2343  * device.  No formatting will be done on the data provided.
2344  *
2345  * If supported by the underlying bus the write will be scheduled
2346  * asynchronously, helping maximise I/O speed on higher speed buses
2347  * like SPI.  regmap_async_complete() can be called to ensure that all
2348  * asynchrnous writes have been completed.
2349  *
2350  * A value of zero will be returned on success, a negative errno will
2351  * be returned in error cases.
2352  */
2353 int regmap_raw_write_async(struct regmap *map, unsigned int reg,
2354                            const void *val, size_t val_len)
2355 {
2356         int ret;
2357
2358         if (val_len % map->format.val_bytes)
2359                 return -EINVAL;
2360         if (!IS_ALIGNED(reg, map->reg_stride))
2361                 return -EINVAL;
2362
2363         map->lock(map->lock_arg);
2364
2365         map->async = true;
2366
2367         ret = _regmap_raw_write(map, reg, val, val_len);
2368
2369         map->async = false;
2370
2371         map->unlock(map->lock_arg);
2372
2373         return ret;
2374 }
2375 EXPORT_SYMBOL_GPL(regmap_raw_write_async);
2376
2377 static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
2378                             unsigned int val_len)
2379 {
2380         struct regmap_range_node *range;
2381         int ret;
2382
2383         WARN_ON(!map->bus);
2384
2385         if (!map->bus || !map->bus->read)
2386                 return -EINVAL;
2387
2388         range = _regmap_range_lookup(map, reg);
2389         if (range) {
2390                 ret = _regmap_select_page(map, &reg, range,
2391                                           val_len / map->format.val_bytes);
2392                 if (ret != 0)
2393                         return ret;
2394         }
2395
2396         map->format.format_reg(map->work_buf, reg, map->reg_shift);
2397         regmap_set_work_buf_flag_mask(map, map->format.reg_bytes,
2398                                       map->read_flag_mask);
2399         trace_regmap_hw_read_start(map, reg, val_len / map->format.val_bytes);
2400
2401         ret = map->bus->read(map->bus_context, map->work_buf,
2402                              map->format.reg_bytes + map->format.pad_bytes,
2403                              val, val_len);
2404
2405         trace_regmap_hw_read_done(map, reg, val_len / map->format.val_bytes);
2406
2407         return ret;
2408 }
2409
2410 static int _regmap_bus_reg_read(void *context, unsigned int reg,
2411                                 unsigned int *val)
2412 {
2413         struct regmap *map = context;
2414
2415         return map->bus->reg_read(map->bus_context, reg, val);
2416 }
2417
2418 static int _regmap_bus_read(void *context, unsigned int reg,
2419                             unsigned int *val)
2420 {
2421         int ret;
2422         struct regmap *map = context;
2423
2424         if (!map->format.parse_val)
2425                 return -EINVAL;
2426
2427         ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
2428         if (ret == 0)
2429                 *val = map->format.parse_val(map->work_buf);
2430
2431         return ret;
2432 }
2433
2434 static int _regmap_read(struct regmap *map, unsigned int reg,
2435                         unsigned int *val)
2436 {
2437         int ret;
2438         void *context = _regmap_map_get_context(map);
2439
2440         if (!map->cache_bypass) {
2441                 ret = regcache_read(map, reg, val);
2442                 if (ret == 0)
2443                         return 0;
2444         }
2445
2446         if (map->cache_only)
2447                 return -EBUSY;
2448
2449         if (!regmap_readable(map, reg))
2450                 return -EIO;
2451
2452         ret = map->reg_read(context, reg, val);
2453         if (ret == 0) {
2454 #ifdef LOG_DEVICE
2455                 if (map->dev && strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
2456                         dev_info(map->dev, "%x => %x\n", reg, *val);
2457 #endif
2458
2459                 trace_regmap_reg_read(map, reg, *val);
2460
2461                 if (!map->cache_bypass)
2462                         regcache_write(map, reg, *val);
2463         }
2464
2465         return ret;
2466 }
2467
2468 /**
2469  * regmap_read() - Read a value from a single register
2470  *
2471  * @map: Register map to read from
2472  * @reg: Register to be read from
2473  * @val: Pointer to store read value
2474  *
2475  * A value of zero will be returned on success, a negative errno will
2476  * be returned in error cases.
2477  */
2478 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
2479 {
2480         int ret;
2481
2482         if (!IS_ALIGNED(reg, map->reg_stride))
2483                 return -EINVAL;
2484
2485         map->lock(map->lock_arg);
2486
2487         ret = _regmap_read(map, reg, val);
2488
2489         map->unlock(map->lock_arg);
2490
2491         return ret;
2492 }
2493 EXPORT_SYMBOL_GPL(regmap_read);
2494
2495 /**
2496  * regmap_raw_read() - Read raw data from the device
2497  *
2498  * @map: Register map to read from
2499  * @reg: First register to be read from
2500  * @val: Pointer to store read value
2501  * @val_len: Size of data to read
2502  *
2503  * A value of zero will be returned on success, a negative errno will
2504  * be returned in error cases.
2505  */
2506 int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
2507                     size_t val_len)
2508 {
2509         size_t val_bytes = map->format.val_bytes;
2510         size_t val_count = val_len / val_bytes;
2511         unsigned int v;
2512         int ret, i;
2513
2514         if (!map->bus)
2515                 return -EINVAL;
2516         if (val_len % map->format.val_bytes)
2517                 return -EINVAL;
2518         if (!IS_ALIGNED(reg, map->reg_stride))
2519                 return -EINVAL;
2520         if (val_count == 0)
2521                 return -EINVAL;
2522
2523         map->lock(map->lock_arg);
2524
2525         if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
2526             map->cache_type == REGCACHE_NONE) {
2527                 if (!map->bus->read) {
2528                         ret = -ENOTSUPP;
2529                         goto out;
2530                 }
2531                 if (map->max_raw_read && map->max_raw_read < val_len) {
2532                         ret = -E2BIG;
2533                         goto out;
2534                 }
2535
2536                 /* Physical block read if there's no cache involved */
2537                 ret = _regmap_raw_read(map, reg, val, val_len);
2538
2539         } else {
2540                 /* Otherwise go word by word for the cache; should be low
2541                  * cost as we expect to hit the cache.
2542                  */
2543                 for (i = 0; i < val_count; i++) {
2544                         ret = _regmap_read(map, reg + regmap_get_offset(map, i),
2545                                            &v);
2546                         if (ret != 0)
2547                                 goto out;
2548
2549                         map->format.format_val(val + (i * val_bytes), v, 0);
2550                 }
2551         }
2552
2553  out:
2554         map->unlock(map->lock_arg);
2555
2556         return ret;
2557 }
2558 EXPORT_SYMBOL_GPL(regmap_raw_read);
2559
2560 /**
2561  * regmap_field_read() - Read a value to a single register field
2562  *
2563  * @field: Register field to read from
2564  * @val: Pointer to store read value
2565  *
2566  * A value of zero will be returned on success, a negative errno will
2567  * be returned in error cases.
2568  */
2569 int regmap_field_read(struct regmap_field *field, unsigned int *val)
2570 {
2571         int ret;
2572         unsigned int reg_val;
2573         ret = regmap_read(field->regmap, field->reg, &reg_val);
2574         if (ret != 0)
2575                 return ret;
2576
2577         reg_val &= field->mask;
2578         reg_val >>= field->shift;
2579         *val = reg_val;
2580
2581         return ret;
2582 }
2583 EXPORT_SYMBOL_GPL(regmap_field_read);
2584
2585 /**
2586  * regmap_fields_read() - Read a value to a single register field with port ID
2587  *
2588  * @field: Register field to read from
2589  * @id: port ID
2590  * @val: Pointer to store read value
2591  *
2592  * A value of zero will be returned on success, a negative errno will
2593  * be returned in error cases.
2594  */
2595 int regmap_fields_read(struct regmap_field *field, unsigned int id,
2596                        unsigned int *val)
2597 {
2598         int ret;
2599         unsigned int reg_val;
2600
2601         if (id >= field->id_size)
2602                 return -EINVAL;
2603
2604         ret = regmap_read(field->regmap,
2605                           field->reg + (field->id_offset * id),
2606                           &reg_val);
2607         if (ret != 0)
2608                 return ret;
2609
2610         reg_val &= field->mask;
2611         reg_val >>= field->shift;
2612         *val = reg_val;
2613
2614         return ret;
2615 }
2616 EXPORT_SYMBOL_GPL(regmap_fields_read);
2617
2618 /**
2619  * regmap_bulk_read() - Read multiple registers from the device
2620  *
2621  * @map: Register map to read from
2622  * @reg: First register to be read from
2623  * @val: Pointer to store read value, in native register size for device
2624  * @val_count: Number of registers to read
2625  *
2626  * A value of zero will be returned on success, a negative errno will
2627  * be returned in error cases.
2628  */
2629 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
2630                      size_t val_count)
2631 {
2632         int ret, i;
2633         size_t val_bytes = map->format.val_bytes;
2634         bool vol = regmap_volatile_range(map, reg, val_count);
2635
2636         if (!IS_ALIGNED(reg, map->reg_stride))
2637                 return -EINVAL;
2638
2639         if (map->bus && map->format.parse_inplace && (vol || map->cache_type == REGCACHE_NONE)) {
2640                 /*
2641                  * Some devices does not support bulk read, for
2642                  * them we have a series of single read operations.
2643                  */
2644                 size_t total_size = val_bytes * val_count;
2645
2646                 if (!map->use_single_read &&
2647                     (!map->max_raw_read || map->max_raw_read > total_size)) {
2648                         ret = regmap_raw_read(map, reg, val,
2649                                               val_bytes * val_count);
2650                         if (ret != 0)
2651                                 return ret;
2652                 } else {
2653                         /*
2654                          * Some devices do not support bulk read or do not
2655                          * support large bulk reads, for them we have a series
2656                          * of read operations.
2657                          */
2658                         int chunk_stride = map->reg_stride;
2659                         size_t chunk_size = val_bytes;
2660                         size_t chunk_count = val_count;
2661
2662                         if (!map->use_single_read) {
2663                                 chunk_size = map->max_raw_read;
2664                                 if (chunk_size % val_bytes)
2665                                         chunk_size -= chunk_size % val_bytes;
2666                                 chunk_count = total_size / chunk_size;
2667                                 chunk_stride *= chunk_size / val_bytes;
2668                         }
2669
2670                         /* Read bytes that fit into a multiple of chunk_size */
2671                         for (i = 0; i < chunk_count; i++) {
2672                                 ret = regmap_raw_read(map,
2673                                                       reg + (i * chunk_stride),
2674                                                       val + (i * chunk_size),
2675                                                       chunk_size);
2676                                 if (ret != 0)
2677                                         return ret;
2678                         }
2679
2680                         /* Read remaining bytes */
2681                         if (chunk_size * i < total_size) {
2682                                 ret = regmap_raw_read(map,
2683                                                       reg + (i * chunk_stride),
2684                                                       val + (i * chunk_size),
2685                                                       total_size - i * chunk_size);
2686                                 if (ret != 0)
2687                                         return ret;
2688                         }
2689                 }
2690
2691                 for (i = 0; i < val_count * val_bytes; i += val_bytes)
2692                         map->format.parse_inplace(val + i);
2693         } else {
2694                 for (i = 0; i < val_count; i++) {
2695                         unsigned int ival;
2696                         ret = regmap_read(map, reg + regmap_get_offset(map, i),
2697                                           &ival);
2698                         if (ret != 0)
2699                                 return ret;
2700
2701                         if (map->format.format_val) {
2702                                 map->format.format_val(val + (i * val_bytes), ival, 0);
2703                         } else {
2704                                 /* Devices providing read and write
2705                                  * operations can use the bulk I/O
2706                                  * functions if they define a val_bytes,
2707                                  * we assume that the values are native
2708                                  * endian.
2709                                  */
2710 #ifdef CONFIG_64BIT
2711                                 u64 *u64 = val;
2712 #endif
2713                                 u32 *u32 = val;
2714                                 u16 *u16 = val;
2715                                 u8 *u8 = val;
2716
2717                                 switch (map->format.val_bytes) {
2718 #ifdef CONFIG_64BIT
2719                                 case 8:
2720                                         u64[i] = ival;
2721                                         break;
2722 #endif
2723                                 case 4:
2724                                         u32[i] = ival;
2725                                         break;
2726                                 case 2:
2727                                         u16[i] = ival;
2728                                         break;
2729                                 case 1:
2730                                         u8[i] = ival;
2731                                         break;
2732                                 default:
2733                                         return -EINVAL;
2734                                 }
2735                         }
2736                 }
2737         }
2738
2739         return 0;
2740 }
2741 EXPORT_SYMBOL_GPL(regmap_bulk_read);
2742
2743 static int _regmap_update_bits(struct regmap *map, unsigned int reg,
2744                                unsigned int mask, unsigned int val,
2745                                bool *change, bool force_write)
2746 {
2747         int ret;
2748         unsigned int tmp, orig;
2749
2750         if (change)
2751                 *change = false;
2752
2753         if (regmap_volatile(map, reg) && map->reg_update_bits) {
2754                 ret = map->reg_update_bits(map->bus_context, reg, mask, val);
2755                 if (ret == 0 && change)
2756                         *change = true;
2757         } else {
2758                 ret = _regmap_read(map, reg, &orig);
2759                 if (ret != 0)
2760                         return ret;
2761
2762                 tmp = orig & ~mask;
2763                 tmp |= val & mask;
2764
2765                 if (force_write || (tmp != orig)) {
2766                         ret = _regmap_write(map, reg, tmp);
2767                         if (ret == 0 && change)
2768                                 *change = true;
2769                 }
2770         }
2771
2772         return ret;
2773 }
2774
2775 /**
2776  * regmap_update_bits_base() - Perform a read/modify/write cycle on a register
2777  *
2778  * @map: Register map to update
2779  * @reg: Register to update
2780  * @mask: Bitmask to change
2781  * @val: New value for bitmask
2782  * @change: Boolean indicating if a write was done
2783  * @async: Boolean indicating asynchronously
2784  * @force: Boolean indicating use force update
2785  *
2786  * Perform a read/modify/write cycle on a register map with change, async, force
2787  * options.
2788  *
2789  * If async is true:
2790  *
2791  * With most buses the read must be done synchronously so this is most useful
2792  * for devices with a cache which do not need to interact with the hardware to
2793  * determine the current register value.
2794  *
2795  * Returns zero for success, a negative number on error.
2796  */
2797 int regmap_update_bits_base(struct regmap *map, unsigned int reg,
2798                             unsigned int mask, unsigned int val,
2799                             bool *change, bool async, bool force)
2800 {
2801         int ret;
2802
2803         map->lock(map->lock_arg);
2804
2805         map->async = async;
2806
2807         ret = _regmap_update_bits(map, reg, mask, val, change, force);
2808
2809         map->async = false;
2810
2811         map->unlock(map->lock_arg);
2812
2813         return ret;
2814 }
2815 EXPORT_SYMBOL_GPL(regmap_update_bits_base);
2816
2817 void regmap_async_complete_cb(struct regmap_async *async, int ret)
2818 {
2819         struct regmap *map = async->map;
2820         bool wake;
2821
2822         trace_regmap_async_io_complete(map);
2823
2824         spin_lock(&map->async_lock);
2825         list_move(&async->list, &map->async_free);
2826         wake = list_empty(&map->async_list);
2827
2828         if (ret != 0)
2829                 map->async_ret = ret;
2830
2831         spin_unlock(&map->async_lock);
2832
2833         if (wake)
2834                 wake_up(&map->async_waitq);
2835 }
2836 EXPORT_SYMBOL_GPL(regmap_async_complete_cb);
2837
2838 static int regmap_async_is_done(struct regmap *map)
2839 {
2840         unsigned long flags;
2841         int ret;
2842
2843         spin_lock_irqsave(&map->async_lock, flags);
2844         ret = list_empty(&map->async_list);
2845         spin_unlock_irqrestore(&map->async_lock, flags);
2846
2847         return ret;
2848 }
2849
2850 /**
2851  * regmap_async_complete - Ensure all asynchronous I/O has completed.
2852  *
2853  * @map: Map to operate on.
2854  *
2855  * Blocks until any pending asynchronous I/O has completed.  Returns
2856  * an error code for any failed I/O operations.
2857  */
2858 int regmap_async_complete(struct regmap *map)
2859 {
2860         unsigned long flags;
2861         int ret;
2862
2863         /* Nothing to do with no async support */
2864         if (!map->bus || !map->bus->async_write)
2865                 return 0;
2866
2867         trace_regmap_async_complete_start(map);
2868
2869         wait_event(map->async_waitq, regmap_async_is_done(map));
2870
2871         spin_lock_irqsave(&map->async_lock, flags);
2872         ret = map->async_ret;
2873         map->async_ret = 0;
2874         spin_unlock_irqrestore(&map->async_lock, flags);
2875
2876         trace_regmap_async_complete_done(map);
2877
2878         return ret;
2879 }
2880 EXPORT_SYMBOL_GPL(regmap_async_complete);
2881
2882 /**
2883  * regmap_register_patch - Register and apply register updates to be applied
2884  *                         on device initialistion
2885  *
2886  * @map: Register map to apply updates to.
2887  * @regs: Values to update.
2888  * @num_regs: Number of entries in regs.
2889  *
2890  * Register a set of register updates to be applied to the device
2891  * whenever the device registers are synchronised with the cache and
2892  * apply them immediately.  Typically this is used to apply
2893  * corrections to be applied to the device defaults on startup, such
2894  * as the updates some vendors provide to undocumented registers.
2895  *
2896  * The caller must ensure that this function cannot be called
2897  * concurrently with either itself or regcache_sync().
2898  */
2899 int regmap_register_patch(struct regmap *map, const struct reg_sequence *regs,
2900                           int num_regs)
2901 {
2902         struct reg_sequence *p;
2903         int ret;
2904         bool bypass;
2905
2906         if (WARN_ONCE(num_regs <= 0, "invalid registers number (%d)\n",
2907             num_regs))
2908                 return 0;
2909
2910         p = krealloc(map->patch,
2911                      sizeof(struct reg_sequence) * (map->patch_regs + num_regs),
2912                      GFP_KERNEL);
2913         if (p) {
2914                 memcpy(p + map->patch_regs, regs, num_regs * sizeof(*regs));
2915                 map->patch = p;
2916                 map->patch_regs += num_regs;
2917         } else {
2918                 return -ENOMEM;
2919         }
2920
2921         map->lock(map->lock_arg);
2922
2923         bypass = map->cache_bypass;
2924
2925         map->cache_bypass = true;
2926         map->async = true;
2927
2928         ret = _regmap_multi_reg_write(map, regs, num_regs);
2929
2930         map->async = false;
2931         map->cache_bypass = bypass;
2932
2933         map->unlock(map->lock_arg);
2934
2935         regmap_async_complete(map);
2936
2937         return ret;
2938 }
2939 EXPORT_SYMBOL_GPL(regmap_register_patch);
2940
2941 /**
2942  * regmap_get_val_bytes() - Report the size of a register value
2943  *
2944  * @map: Register map to operate on.
2945  *
2946  * Report the size of a register value, mainly intended to for use by
2947  * generic infrastructure built on top of regmap.
2948  */
2949 int regmap_get_val_bytes(struct regmap *map)
2950 {
2951         if (map->format.format_write)
2952                 return -EINVAL;
2953
2954         return map->format.val_bytes;
2955 }
2956 EXPORT_SYMBOL_GPL(regmap_get_val_bytes);
2957
2958 /**
2959  * regmap_get_max_register() - Report the max register value
2960  *
2961  * @map: Register map to operate on.
2962  *
2963  * Report the max register value, mainly intended to for use by
2964  * generic infrastructure built on top of regmap.
2965  */
2966 int regmap_get_max_register(struct regmap *map)
2967 {
2968         return map->max_register ? map->max_register : -EINVAL;
2969 }
2970 EXPORT_SYMBOL_GPL(regmap_get_max_register);
2971
2972 /**
2973  * regmap_get_reg_stride() - Report the register address stride
2974  *
2975  * @map: Register map to operate on.
2976  *
2977  * Report the register address stride, mainly intended to for use by
2978  * generic infrastructure built on top of regmap.
2979  */
2980 int regmap_get_reg_stride(struct regmap *map)
2981 {
2982         return map->reg_stride;
2983 }
2984 EXPORT_SYMBOL_GPL(regmap_get_reg_stride);
2985
2986 int regmap_parse_val(struct regmap *map, const void *buf,
2987                         unsigned int *val)
2988 {
2989         if (!map->format.parse_val)
2990                 return -EINVAL;
2991
2992         *val = map->format.parse_val(buf);
2993
2994         return 0;
2995 }
2996 EXPORT_SYMBOL_GPL(regmap_parse_val);
2997
2998 static int __init regmap_initcall(void)
2999 {
3000         regmap_debugfs_initcall();
3001
3002         return 0;
3003 }
3004 postcore_initcall(regmap_initcall);