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