Merge remote-tracking branches 'regmap/topic/debugfs' and 'regmap/topic/force-update...
[pandora-kernel.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, bool force_write);
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, false);
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 int regmap_fields_force_write(struct regmap_field *field, unsigned int id,
1661                         unsigned int val)
1662 {
1663         if (id >= field->id_size)
1664                 return -EINVAL;
1665
1666         return regmap_write_bits(field->regmap,
1667                                   field->reg + (field->id_offset * id),
1668                                   field->mask, val << field->shift);
1669 }
1670 EXPORT_SYMBOL_GPL(regmap_fields_force_write);
1671
1672 /**
1673  * regmap_fields_update_bits(): Perform a read/modify/write cycle
1674  *                              on the register field
1675  *
1676  * @field: Register field to write to
1677  * @id: port ID
1678  * @mask: Bitmask to change
1679  * @val: Value to be written
1680  *
1681  * A value of zero will be returned on success, a negative errno will
1682  * be returned in error cases.
1683  */
1684 int regmap_fields_update_bits(struct regmap_field *field,  unsigned int id,
1685                               unsigned int mask, unsigned int val)
1686 {
1687         if (id >= field->id_size)
1688                 return -EINVAL;
1689
1690         mask = (mask << field->shift) & field->mask;
1691
1692         return regmap_update_bits(field->regmap,
1693                                   field->reg + (field->id_offset * id),
1694                                   mask, val << field->shift);
1695 }
1696 EXPORT_SYMBOL_GPL(regmap_fields_update_bits);
1697
1698 /*
1699  * regmap_bulk_write(): Write multiple registers to the device
1700  *
1701  * @map: Register map to write to
1702  * @reg: First register to be write from
1703  * @val: Block of data to be written, in native register size for device
1704  * @val_count: Number of registers to write
1705  *
1706  * This function is intended to be used for writing a large block of
1707  * data to the device either in single transfer or multiple transfer.
1708  *
1709  * A value of zero will be returned on success, a negative errno will
1710  * be returned in error cases.
1711  */
1712 int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
1713                      size_t val_count)
1714 {
1715         int ret = 0, i;
1716         size_t val_bytes = map->format.val_bytes;
1717         size_t total_size = val_bytes * val_count;
1718
1719         if (map->bus && !map->format.parse_inplace)
1720                 return -EINVAL;
1721         if (reg % map->reg_stride)
1722                 return -EINVAL;
1723
1724         /*
1725          * Some devices don't support bulk write, for
1726          * them we have a series of single write operations in the first two if
1727          * blocks.
1728          *
1729          * The first if block is used for memory mapped io. It does not allow
1730          * val_bytes of 3 for example.
1731          * The second one is used for busses which do not have this limitation
1732          * and can write arbitrary value lengths.
1733          */
1734         if (!map->bus) {
1735                 map->lock(map->lock_arg);
1736                 for (i = 0; i < val_count; i++) {
1737                         unsigned int ival;
1738
1739                         switch (val_bytes) {
1740                         case 1:
1741                                 ival = *(u8 *)(val + (i * val_bytes));
1742                                 break;
1743                         case 2:
1744                                 ival = *(u16 *)(val + (i * val_bytes));
1745                                 break;
1746                         case 4:
1747                                 ival = *(u32 *)(val + (i * val_bytes));
1748                                 break;
1749 #ifdef CONFIG_64BIT
1750                         case 8:
1751                                 ival = *(u64 *)(val + (i * val_bytes));
1752                                 break;
1753 #endif
1754                         default:
1755                                 ret = -EINVAL;
1756                                 goto out;
1757                         }
1758
1759                         ret = _regmap_write(map, reg + (i * map->reg_stride),
1760                                         ival);
1761                         if (ret != 0)
1762                                 goto out;
1763                 }
1764 out:
1765                 map->unlock(map->lock_arg);
1766         } else if (map->use_single_write ||
1767                    (map->max_raw_write && map->max_raw_write < total_size)) {
1768                 int chunk_stride = map->reg_stride;
1769                 size_t chunk_size = val_bytes;
1770                 size_t chunk_count = val_count;
1771
1772                 if (!map->use_single_write) {
1773                         chunk_size = map->max_raw_write;
1774                         if (chunk_size % val_bytes)
1775                                 chunk_size -= chunk_size % val_bytes;
1776                         chunk_count = total_size / chunk_size;
1777                         chunk_stride *= chunk_size / val_bytes;
1778                 }
1779
1780                 map->lock(map->lock_arg);
1781                 /* Write as many bytes as possible with chunk_size */
1782                 for (i = 0; i < chunk_count; i++) {
1783                         ret = _regmap_raw_write(map,
1784                                                 reg + (i * chunk_stride),
1785                                                 val + (i * chunk_size),
1786                                                 chunk_size);
1787                         if (ret)
1788                                 break;
1789                 }
1790
1791                 /* Write remaining bytes */
1792                 if (!ret && chunk_size * i < total_size) {
1793                         ret = _regmap_raw_write(map, reg + (i * chunk_stride),
1794                                                 val + (i * chunk_size),
1795                                                 total_size - i * chunk_size);
1796                 }
1797                 map->unlock(map->lock_arg);
1798         } else {
1799                 void *wval;
1800
1801                 if (!val_count)
1802                         return -EINVAL;
1803
1804                 wval = kmemdup(val, val_count * val_bytes, GFP_KERNEL);
1805                 if (!wval) {
1806                         dev_err(map->dev, "Error in memory allocation\n");
1807                         return -ENOMEM;
1808                 }
1809                 for (i = 0; i < val_count * val_bytes; i += val_bytes)
1810                         map->format.parse_inplace(wval + i);
1811
1812                 map->lock(map->lock_arg);
1813                 ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count);
1814                 map->unlock(map->lock_arg);
1815
1816                 kfree(wval);
1817         }
1818         return ret;
1819 }
1820 EXPORT_SYMBOL_GPL(regmap_bulk_write);
1821
1822 /*
1823  * _regmap_raw_multi_reg_write()
1824  *
1825  * the (register,newvalue) pairs in regs have not been formatted, but
1826  * they are all in the same page and have been changed to being page
1827  * relative. The page register has been written if that was necessary.
1828  */
1829 static int _regmap_raw_multi_reg_write(struct regmap *map,
1830                                        const struct reg_default *regs,
1831                                        size_t num_regs)
1832 {
1833         int ret;
1834         void *buf;
1835         int i;
1836         u8 *u8;
1837         size_t val_bytes = map->format.val_bytes;
1838         size_t reg_bytes = map->format.reg_bytes;
1839         size_t pad_bytes = map->format.pad_bytes;
1840         size_t pair_size = reg_bytes + pad_bytes + val_bytes;
1841         size_t len = pair_size * num_regs;
1842
1843         if (!len)
1844                 return -EINVAL;
1845
1846         buf = kzalloc(len, GFP_KERNEL);
1847         if (!buf)
1848                 return -ENOMEM;
1849
1850         /* We have to linearise by hand. */
1851
1852         u8 = buf;
1853
1854         for (i = 0; i < num_regs; i++) {
1855                 unsigned int reg = regs[i].reg;
1856                 unsigned int val = regs[i].def;
1857                 trace_regmap_hw_write_start(map, reg, 1);
1858                 map->format.format_reg(u8, reg, map->reg_shift);
1859                 u8 += reg_bytes + pad_bytes;
1860                 map->format.format_val(u8, val, 0);
1861                 u8 += val_bytes;
1862         }
1863         u8 = buf;
1864         *u8 |= map->write_flag_mask;
1865
1866         ret = map->bus->write(map->bus_context, buf, len);
1867
1868         kfree(buf);
1869
1870         for (i = 0; i < num_regs; i++) {
1871                 int reg = regs[i].reg;
1872                 trace_regmap_hw_write_done(map, reg, 1);
1873         }
1874         return ret;
1875 }
1876
1877 static unsigned int _regmap_register_page(struct regmap *map,
1878                                           unsigned int reg,
1879                                           struct regmap_range_node *range)
1880 {
1881         unsigned int win_page = (reg - range->range_min) / range->window_len;
1882
1883         return win_page;
1884 }
1885
1886 static int _regmap_range_multi_paged_reg_write(struct regmap *map,
1887                                                struct reg_default *regs,
1888                                                size_t num_regs)
1889 {
1890         int ret;
1891         int i, n;
1892         struct reg_default *base;
1893         unsigned int this_page = 0;
1894         /*
1895          * the set of registers are not neccessarily in order, but
1896          * since the order of write must be preserved this algorithm
1897          * chops the set each time the page changes
1898          */
1899         base = regs;
1900         for (i = 0, n = 0; i < num_regs; i++, n++) {
1901                 unsigned int reg = regs[i].reg;
1902                 struct regmap_range_node *range;
1903
1904                 range = _regmap_range_lookup(map, reg);
1905                 if (range) {
1906                         unsigned int win_page = _regmap_register_page(map, reg,
1907                                                                       range);
1908
1909                         if (i == 0)
1910                                 this_page = win_page;
1911                         if (win_page != this_page) {
1912                                 this_page = win_page;
1913                                 ret = _regmap_raw_multi_reg_write(map, base, n);
1914                                 if (ret != 0)
1915                                         return ret;
1916                                 base += n;
1917                                 n = 0;
1918                         }
1919                         ret = _regmap_select_page(map, &base[n].reg, range, 1);
1920                         if (ret != 0)
1921                                 return ret;
1922                 }
1923         }
1924         if (n > 0)
1925                 return _regmap_raw_multi_reg_write(map, base, n);
1926         return 0;
1927 }
1928
1929 static int _regmap_multi_reg_write(struct regmap *map,
1930                                    const struct reg_default *regs,
1931                                    size_t num_regs)
1932 {
1933         int i;
1934         int ret;
1935
1936         if (!map->can_multi_write) {
1937                 for (i = 0; i < num_regs; i++) {
1938                         ret = _regmap_write(map, regs[i].reg, regs[i].def);
1939                         if (ret != 0)
1940                                 return ret;
1941                 }
1942                 return 0;
1943         }
1944
1945         if (!map->format.parse_inplace)
1946                 return -EINVAL;
1947
1948         if (map->writeable_reg)
1949                 for (i = 0; i < num_regs; i++) {
1950                         int reg = regs[i].reg;
1951                         if (!map->writeable_reg(map->dev, reg))
1952                                 return -EINVAL;
1953                         if (reg % map->reg_stride)
1954                                 return -EINVAL;
1955                 }
1956
1957         if (!map->cache_bypass) {
1958                 for (i = 0; i < num_regs; i++) {
1959                         unsigned int val = regs[i].def;
1960                         unsigned int reg = regs[i].reg;
1961                         ret = regcache_write(map, reg, val);
1962                         if (ret) {
1963                                 dev_err(map->dev,
1964                                 "Error in caching of register: %x ret: %d\n",
1965                                                                 reg, ret);
1966                                 return ret;
1967                         }
1968                 }
1969                 if (map->cache_only) {
1970                         map->cache_dirty = true;
1971                         return 0;
1972                 }
1973         }
1974
1975         WARN_ON(!map->bus);
1976
1977         for (i = 0; i < num_regs; i++) {
1978                 unsigned int reg = regs[i].reg;
1979                 struct regmap_range_node *range;
1980                 range = _regmap_range_lookup(map, reg);
1981                 if (range) {
1982                         size_t len = sizeof(struct reg_default)*num_regs;
1983                         struct reg_default *base = kmemdup(regs, len,
1984                                                            GFP_KERNEL);
1985                         if (!base)
1986                                 return -ENOMEM;
1987                         ret = _regmap_range_multi_paged_reg_write(map, base,
1988                                                                   num_regs);
1989                         kfree(base);
1990
1991                         return ret;
1992                 }
1993         }
1994         return _regmap_raw_multi_reg_write(map, regs, num_regs);
1995 }
1996
1997 /*
1998  * regmap_multi_reg_write(): Write multiple registers to the device
1999  *
2000  * where the set of register,value pairs are supplied in any order,
2001  * possibly not all in a single range.
2002  *
2003  * @map: Register map to write to
2004  * @regs: Array of structures containing register,value to be written
2005  * @num_regs: Number of registers to write
2006  *
2007  * The 'normal' block write mode will send ultimately send data on the
2008  * target bus as R,V1,V2,V3,..,Vn where successively higer registers are
2009  * addressed. However, this alternative block multi write mode will send
2010  * the data as R1,V1,R2,V2,..,Rn,Vn on the target bus. The target device
2011  * must of course support the mode.
2012  *
2013  * A value of zero will be returned on success, a negative errno will be
2014  * returned in error cases.
2015  */
2016 int regmap_multi_reg_write(struct regmap *map, const struct reg_default *regs,
2017                            int num_regs)
2018 {
2019         int ret;
2020
2021         map->lock(map->lock_arg);
2022
2023         ret = _regmap_multi_reg_write(map, regs, num_regs);
2024
2025         map->unlock(map->lock_arg);
2026
2027         return ret;
2028 }
2029 EXPORT_SYMBOL_GPL(regmap_multi_reg_write);
2030
2031 /*
2032  * regmap_multi_reg_write_bypassed(): Write multiple registers to the
2033  *                                    device but not the cache
2034  *
2035  * where the set of register are supplied in any order
2036  *
2037  * @map: Register map to write to
2038  * @regs: Array of structures containing register,value to be written
2039  * @num_regs: Number of registers to write
2040  *
2041  * This function is intended to be used for writing a large block of data
2042  * atomically to the device in single transfer for those I2C client devices
2043  * that implement this alternative block write mode.
2044  *
2045  * A value of zero will be returned on success, a negative errno will
2046  * be returned in error cases.
2047  */
2048 int regmap_multi_reg_write_bypassed(struct regmap *map,
2049                                     const struct reg_default *regs,
2050                                     int num_regs)
2051 {
2052         int ret;
2053         bool bypass;
2054
2055         map->lock(map->lock_arg);
2056
2057         bypass = map->cache_bypass;
2058         map->cache_bypass = true;
2059
2060         ret = _regmap_multi_reg_write(map, regs, num_regs);
2061
2062         map->cache_bypass = bypass;
2063
2064         map->unlock(map->lock_arg);
2065
2066         return ret;
2067 }
2068 EXPORT_SYMBOL_GPL(regmap_multi_reg_write_bypassed);
2069
2070 /**
2071  * regmap_raw_write_async(): Write raw values to one or more registers
2072  *                           asynchronously
2073  *
2074  * @map: Register map to write to
2075  * @reg: Initial register to write to
2076  * @val: Block of data to be written, laid out for direct transmission to the
2077  *       device.  Must be valid until regmap_async_complete() is called.
2078  * @val_len: Length of data pointed to by val.
2079  *
2080  * This function is intended to be used for things like firmware
2081  * download where a large block of data needs to be transferred to the
2082  * device.  No formatting will be done on the data provided.
2083  *
2084  * If supported by the underlying bus the write will be scheduled
2085  * asynchronously, helping maximise I/O speed on higher speed buses
2086  * like SPI.  regmap_async_complete() can be called to ensure that all
2087  * asynchrnous writes have been completed.
2088  *
2089  * A value of zero will be returned on success, a negative errno will
2090  * be returned in error cases.
2091  */
2092 int regmap_raw_write_async(struct regmap *map, unsigned int reg,
2093                            const void *val, size_t val_len)
2094 {
2095         int ret;
2096
2097         if (val_len % map->format.val_bytes)
2098                 return -EINVAL;
2099         if (reg % map->reg_stride)
2100                 return -EINVAL;
2101
2102         map->lock(map->lock_arg);
2103
2104         map->async = true;
2105
2106         ret = _regmap_raw_write(map, reg, val, val_len);
2107
2108         map->async = false;
2109
2110         map->unlock(map->lock_arg);
2111
2112         return ret;
2113 }
2114 EXPORT_SYMBOL_GPL(regmap_raw_write_async);
2115
2116 static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
2117                             unsigned int val_len)
2118 {
2119         struct regmap_range_node *range;
2120         u8 *u8 = map->work_buf;
2121         int ret;
2122
2123         WARN_ON(!map->bus);
2124
2125         range = _regmap_range_lookup(map, reg);
2126         if (range) {
2127                 ret = _regmap_select_page(map, &reg, range,
2128                                           val_len / map->format.val_bytes);
2129                 if (ret != 0)
2130                         return ret;
2131         }
2132
2133         map->format.format_reg(map->work_buf, reg, map->reg_shift);
2134
2135         /*
2136          * Some buses or devices flag reads by setting the high bits in the
2137          * register address; since it's always the high bits for all
2138          * current formats we can do this here rather than in
2139          * formatting.  This may break if we get interesting formats.
2140          */
2141         u8[0] |= map->read_flag_mask;
2142
2143         trace_regmap_hw_read_start(map, reg, val_len / map->format.val_bytes);
2144
2145         ret = map->bus->read(map->bus_context, map->work_buf,
2146                              map->format.reg_bytes + map->format.pad_bytes,
2147                              val, val_len);
2148
2149         trace_regmap_hw_read_done(map, reg, val_len / map->format.val_bytes);
2150
2151         return ret;
2152 }
2153
2154 static int _regmap_bus_reg_read(void *context, unsigned int reg,
2155                                 unsigned int *val)
2156 {
2157         struct regmap *map = context;
2158
2159         return map->bus->reg_read(map->bus_context, reg, val);
2160 }
2161
2162 static int _regmap_bus_read(void *context, unsigned int reg,
2163                             unsigned int *val)
2164 {
2165         int ret;
2166         struct regmap *map = context;
2167
2168         if (!map->format.parse_val)
2169                 return -EINVAL;
2170
2171         ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
2172         if (ret == 0)
2173                 *val = map->format.parse_val(map->work_buf);
2174
2175         return ret;
2176 }
2177
2178 static int _regmap_read(struct regmap *map, unsigned int reg,
2179                         unsigned int *val)
2180 {
2181         int ret;
2182         void *context = _regmap_map_get_context(map);
2183
2184         if (!map->cache_bypass) {
2185                 ret = regcache_read(map, reg, val);
2186                 if (ret == 0)
2187                         return 0;
2188         }
2189
2190         if (map->cache_only)
2191                 return -EBUSY;
2192
2193         if (!regmap_readable(map, reg))
2194                 return -EIO;
2195
2196         ret = map->reg_read(context, reg, val);
2197         if (ret == 0) {
2198 #ifdef LOG_DEVICE
2199                 if (map->dev && strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
2200                         dev_info(map->dev, "%x => %x\n", reg, *val);
2201 #endif
2202
2203                 trace_regmap_reg_read(map, reg, *val);
2204
2205                 if (!map->cache_bypass)
2206                         regcache_write(map, reg, *val);
2207         }
2208
2209         return ret;
2210 }
2211
2212 /**
2213  * regmap_read(): Read a value from a single register
2214  *
2215  * @map: Register map to read from
2216  * @reg: Register to be read from
2217  * @val: Pointer to store read value
2218  *
2219  * A value of zero will be returned on success, a negative errno will
2220  * be returned in error cases.
2221  */
2222 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
2223 {
2224         int ret;
2225
2226         if (reg % map->reg_stride)
2227                 return -EINVAL;
2228
2229         map->lock(map->lock_arg);
2230
2231         ret = _regmap_read(map, reg, val);
2232
2233         map->unlock(map->lock_arg);
2234
2235         return ret;
2236 }
2237 EXPORT_SYMBOL_GPL(regmap_read);
2238
2239 /**
2240  * regmap_raw_read(): Read raw data from the device
2241  *
2242  * @map: Register map to read from
2243  * @reg: First register to be read from
2244  * @val: Pointer to store read value
2245  * @val_len: Size of data to read
2246  *
2247  * A value of zero will be returned on success, a negative errno will
2248  * be returned in error cases.
2249  */
2250 int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
2251                     size_t val_len)
2252 {
2253         size_t val_bytes = map->format.val_bytes;
2254         size_t val_count = val_len / val_bytes;
2255         unsigned int v;
2256         int ret, i;
2257
2258         if (!map->bus)
2259                 return -EINVAL;
2260         if (val_len % map->format.val_bytes)
2261                 return -EINVAL;
2262         if (reg % map->reg_stride)
2263                 return -EINVAL;
2264         if (val_count == 0)
2265                 return -EINVAL;
2266
2267         map->lock(map->lock_arg);
2268
2269         if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
2270             map->cache_type == REGCACHE_NONE) {
2271                 if (!map->bus->read) {
2272                         ret = -ENOTSUPP;
2273                         goto out;
2274                 }
2275                 if (map->max_raw_read && map->max_raw_read < val_len) {
2276                         ret = -E2BIG;
2277                         goto out;
2278                 }
2279
2280                 /* Physical block read if there's no cache involved */
2281                 ret = _regmap_raw_read(map, reg, val, val_len);
2282
2283         } else {
2284                 /* Otherwise go word by word for the cache; should be low
2285                  * cost as we expect to hit the cache.
2286                  */
2287                 for (i = 0; i < val_count; i++) {
2288                         ret = _regmap_read(map, reg + (i * map->reg_stride),
2289                                            &v);
2290                         if (ret != 0)
2291                                 goto out;
2292
2293                         map->format.format_val(val + (i * val_bytes), v, 0);
2294                 }
2295         }
2296
2297  out:
2298         map->unlock(map->lock_arg);
2299
2300         return ret;
2301 }
2302 EXPORT_SYMBOL_GPL(regmap_raw_read);
2303
2304 /**
2305  * regmap_field_read(): Read a value to a single register field
2306  *
2307  * @field: Register field to read from
2308  * @val: Pointer to store read value
2309  *
2310  * A value of zero will be returned on success, a negative errno will
2311  * be returned in error cases.
2312  */
2313 int regmap_field_read(struct regmap_field *field, unsigned int *val)
2314 {
2315         int ret;
2316         unsigned int reg_val;
2317         ret = regmap_read(field->regmap, field->reg, &reg_val);
2318         if (ret != 0)
2319                 return ret;
2320
2321         reg_val &= field->mask;
2322         reg_val >>= field->shift;
2323         *val = reg_val;
2324
2325         return ret;
2326 }
2327 EXPORT_SYMBOL_GPL(regmap_field_read);
2328
2329 /**
2330  * regmap_fields_read(): Read a value to a single register field with port ID
2331  *
2332  * @field: Register field to read from
2333  * @id: port ID
2334  * @val: Pointer to store read value
2335  *
2336  * A value of zero will be returned on success, a negative errno will
2337  * be returned in error cases.
2338  */
2339 int regmap_fields_read(struct regmap_field *field, unsigned int id,
2340                        unsigned int *val)
2341 {
2342         int ret;
2343         unsigned int reg_val;
2344
2345         if (id >= field->id_size)
2346                 return -EINVAL;
2347
2348         ret = regmap_read(field->regmap,
2349                           field->reg + (field->id_offset * id),
2350                           &reg_val);
2351         if (ret != 0)
2352                 return ret;
2353
2354         reg_val &= field->mask;
2355         reg_val >>= field->shift;
2356         *val = reg_val;
2357
2358         return ret;
2359 }
2360 EXPORT_SYMBOL_GPL(regmap_fields_read);
2361
2362 /**
2363  * regmap_bulk_read(): Read multiple registers from the device
2364  *
2365  * @map: Register map to read from
2366  * @reg: First register to be read from
2367  * @val: Pointer to store read value, in native register size for device
2368  * @val_count: Number of registers to read
2369  *
2370  * A value of zero will be returned on success, a negative errno will
2371  * be returned in error cases.
2372  */
2373 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
2374                      size_t val_count)
2375 {
2376         int ret, i;
2377         size_t val_bytes = map->format.val_bytes;
2378         bool vol = regmap_volatile_range(map, reg, val_count);
2379
2380         if (reg % map->reg_stride)
2381                 return -EINVAL;
2382
2383         if (map->bus && map->format.parse_inplace && (vol || map->cache_type == REGCACHE_NONE)) {
2384                 /*
2385                  * Some devices does not support bulk read, for
2386                  * them we have a series of single read operations.
2387                  */
2388                 size_t total_size = val_bytes * val_count;
2389
2390                 if (!map->use_single_read &&
2391                     (!map->max_raw_read || map->max_raw_read > total_size)) {
2392                         ret = regmap_raw_read(map, reg, val,
2393                                               val_bytes * val_count);
2394                         if (ret != 0)
2395                                 return ret;
2396                 } else {
2397                         /*
2398                          * Some devices do not support bulk read or do not
2399                          * support large bulk reads, for them we have a series
2400                          * of read operations.
2401                          */
2402                         int chunk_stride = map->reg_stride;
2403                         size_t chunk_size = val_bytes;
2404                         size_t chunk_count = val_count;
2405
2406                         if (!map->use_single_read) {
2407                                 chunk_size = map->max_raw_read;
2408                                 if (chunk_size % val_bytes)
2409                                         chunk_size -= chunk_size % val_bytes;
2410                                 chunk_count = total_size / chunk_size;
2411                                 chunk_stride *= chunk_size / val_bytes;
2412                         }
2413
2414                         /* Read bytes that fit into a multiple of chunk_size */
2415                         for (i = 0; i < chunk_count; i++) {
2416                                 ret = regmap_raw_read(map,
2417                                                       reg + (i * chunk_stride),
2418                                                       val + (i * chunk_size),
2419                                                       chunk_size);
2420                                 if (ret != 0)
2421                                         return ret;
2422                         }
2423
2424                         /* Read remaining bytes */
2425                         if (chunk_size * i < total_size) {
2426                                 ret = regmap_raw_read(map,
2427                                                       reg + (i * chunk_stride),
2428                                                       val + (i * chunk_size),
2429                                                       total_size - i * chunk_size);
2430                                 if (ret != 0)
2431                                         return ret;
2432                         }
2433                 }
2434
2435                 for (i = 0; i < val_count * val_bytes; i += val_bytes)
2436                         map->format.parse_inplace(val + i);
2437         } else {
2438                 for (i = 0; i < val_count; i++) {
2439                         unsigned int ival;
2440                         ret = regmap_read(map, reg + (i * map->reg_stride),
2441                                           &ival);
2442                         if (ret != 0)
2443                                 return ret;
2444
2445                         if (map->format.format_val) {
2446                                 map->format.format_val(val + (i * val_bytes), ival, 0);
2447                         } else {
2448                                 /* Devices providing read and write
2449                                  * operations can use the bulk I/O
2450                                  * functions if they define a val_bytes,
2451                                  * we assume that the values are native
2452                                  * endian.
2453                                  */
2454                                 u32 *u32 = val;
2455                                 u16 *u16 = val;
2456                                 u8 *u8 = val;
2457
2458                                 switch (map->format.val_bytes) {
2459                                 case 4:
2460                                         u32[i] = ival;
2461                                         break;
2462                                 case 2:
2463                                         u16[i] = ival;
2464                                         break;
2465                                 case 1:
2466                                         u8[i] = ival;
2467                                         break;
2468                                 default:
2469                                         return -EINVAL;
2470                                 }
2471                         }
2472                 }
2473         }
2474
2475         return 0;
2476 }
2477 EXPORT_SYMBOL_GPL(regmap_bulk_read);
2478
2479 static int _regmap_update_bits(struct regmap *map, unsigned int reg,
2480                                unsigned int mask, unsigned int val,
2481                                bool *change, bool force_write)
2482 {
2483         int ret;
2484         unsigned int tmp, orig;
2485
2486         ret = _regmap_read(map, reg, &orig);
2487         if (ret != 0)
2488                 return ret;
2489
2490         tmp = orig & ~mask;
2491         tmp |= val & mask;
2492
2493         if (force_write || (tmp != orig)) {
2494                 ret = _regmap_write(map, reg, tmp);
2495                 if (change)
2496                         *change = true;
2497         } else {
2498                 if (change)
2499                         *change = false;
2500         }
2501
2502         return ret;
2503 }
2504
2505 /**
2506  * regmap_update_bits: Perform a read/modify/write cycle on the register map
2507  *
2508  * @map: Register map to update
2509  * @reg: Register to update
2510  * @mask: Bitmask to change
2511  * @val: New value for bitmask
2512  *
2513  * Returns zero for success, a negative number on error.
2514  */
2515 int regmap_update_bits(struct regmap *map, unsigned int reg,
2516                        unsigned int mask, unsigned int val)
2517 {
2518         int ret;
2519
2520         map->lock(map->lock_arg);
2521         ret = _regmap_update_bits(map, reg, mask, val, NULL, false);
2522         map->unlock(map->lock_arg);
2523
2524         return ret;
2525 }
2526 EXPORT_SYMBOL_GPL(regmap_update_bits);
2527
2528 /**
2529  * regmap_write_bits: Perform a read/modify/write cycle on the register map
2530  *
2531  * @map: Register map to update
2532  * @reg: Register to update
2533  * @mask: Bitmask to change
2534  * @val: New value for bitmask
2535  *
2536  * Returns zero for success, a negative number on error.
2537  */
2538 int regmap_write_bits(struct regmap *map, unsigned int reg,
2539                       unsigned int mask, unsigned int val)
2540 {
2541         int ret;
2542
2543         map->lock(map->lock_arg);
2544         ret = _regmap_update_bits(map, reg, mask, val, NULL, true);
2545         map->unlock(map->lock_arg);
2546
2547         return ret;
2548 }
2549 EXPORT_SYMBOL_GPL(regmap_write_bits);
2550
2551 /**
2552  * regmap_update_bits_async: Perform a read/modify/write cycle on the register
2553  *                           map asynchronously
2554  *
2555  * @map: Register map to update
2556  * @reg: Register to update
2557  * @mask: Bitmask to change
2558  * @val: New value for bitmask
2559  *
2560  * With most buses the read must be done synchronously so this is most
2561  * useful for devices with a cache which do not need to interact with
2562  * the hardware to determine the current register value.
2563  *
2564  * Returns zero for success, a negative number on error.
2565  */
2566 int regmap_update_bits_async(struct regmap *map, unsigned int reg,
2567                              unsigned int mask, unsigned int val)
2568 {
2569         int ret;
2570
2571         map->lock(map->lock_arg);
2572
2573         map->async = true;
2574
2575         ret = _regmap_update_bits(map, reg, mask, val, NULL, false);
2576
2577         map->async = false;
2578
2579         map->unlock(map->lock_arg);
2580
2581         return ret;
2582 }
2583 EXPORT_SYMBOL_GPL(regmap_update_bits_async);
2584
2585 /**
2586  * regmap_update_bits_check: Perform a read/modify/write cycle on the
2587  *                           register map and report if updated
2588  *
2589  * @map: Register map to update
2590  * @reg: Register to update
2591  * @mask: Bitmask to change
2592  * @val: New value for bitmask
2593  * @change: Boolean indicating if a write was done
2594  *
2595  * Returns zero for success, a negative number on error.
2596  */
2597 int regmap_update_bits_check(struct regmap *map, unsigned int reg,
2598                              unsigned int mask, unsigned int val,
2599                              bool *change)
2600 {
2601         int ret;
2602
2603         map->lock(map->lock_arg);
2604         ret = _regmap_update_bits(map, reg, mask, val, change, false);
2605         map->unlock(map->lock_arg);
2606         return ret;
2607 }
2608 EXPORT_SYMBOL_GPL(regmap_update_bits_check);
2609
2610 /**
2611  * regmap_update_bits_check_async: Perform a read/modify/write cycle on the
2612  *                                 register map asynchronously and report if
2613  *                                 updated
2614  *
2615  * @map: Register map to update
2616  * @reg: Register to update
2617  * @mask: Bitmask to change
2618  * @val: New value for bitmask
2619  * @change: Boolean indicating if a write was done
2620  *
2621  * With most buses the read must be done synchronously so this is most
2622  * useful for devices with a cache which do not need to interact with
2623  * the hardware to determine the current register value.
2624  *
2625  * Returns zero for success, a negative number on error.
2626  */
2627 int regmap_update_bits_check_async(struct regmap *map, unsigned int reg,
2628                                    unsigned int mask, unsigned int val,
2629                                    bool *change)
2630 {
2631         int ret;
2632
2633         map->lock(map->lock_arg);
2634
2635         map->async = true;
2636
2637         ret = _regmap_update_bits(map, reg, mask, val, change, false);
2638
2639         map->async = false;
2640
2641         map->unlock(map->lock_arg);
2642
2643         return ret;
2644 }
2645 EXPORT_SYMBOL_GPL(regmap_update_bits_check_async);
2646
2647 void regmap_async_complete_cb(struct regmap_async *async, int ret)
2648 {
2649         struct regmap *map = async->map;
2650         bool wake;
2651
2652         trace_regmap_async_io_complete(map);
2653
2654         spin_lock(&map->async_lock);
2655         list_move(&async->list, &map->async_free);
2656         wake = list_empty(&map->async_list);
2657
2658         if (ret != 0)
2659                 map->async_ret = ret;
2660
2661         spin_unlock(&map->async_lock);
2662
2663         if (wake)
2664                 wake_up(&map->async_waitq);
2665 }
2666 EXPORT_SYMBOL_GPL(regmap_async_complete_cb);
2667
2668 static int regmap_async_is_done(struct regmap *map)
2669 {
2670         unsigned long flags;
2671         int ret;
2672
2673         spin_lock_irqsave(&map->async_lock, flags);
2674         ret = list_empty(&map->async_list);
2675         spin_unlock_irqrestore(&map->async_lock, flags);
2676
2677         return ret;
2678 }
2679
2680 /**
2681  * regmap_async_complete: Ensure all asynchronous I/O has completed.
2682  *
2683  * @map: Map to operate on.
2684  *
2685  * Blocks until any pending asynchronous I/O has completed.  Returns
2686  * an error code for any failed I/O operations.
2687  */
2688 int regmap_async_complete(struct regmap *map)
2689 {
2690         unsigned long flags;
2691         int ret;
2692
2693         /* Nothing to do with no async support */
2694         if (!map->bus || !map->bus->async_write)
2695                 return 0;
2696
2697         trace_regmap_async_complete_start(map);
2698
2699         wait_event(map->async_waitq, regmap_async_is_done(map));
2700
2701         spin_lock_irqsave(&map->async_lock, flags);
2702         ret = map->async_ret;
2703         map->async_ret = 0;
2704         spin_unlock_irqrestore(&map->async_lock, flags);
2705
2706         trace_regmap_async_complete_done(map);
2707
2708         return ret;
2709 }
2710 EXPORT_SYMBOL_GPL(regmap_async_complete);
2711
2712 /**
2713  * regmap_register_patch: Register and apply register updates to be applied
2714  *                        on device initialistion
2715  *
2716  * @map: Register map to apply updates to.
2717  * @regs: Values to update.
2718  * @num_regs: Number of entries in regs.
2719  *
2720  * Register a set of register updates to be applied to the device
2721  * whenever the device registers are synchronised with the cache and
2722  * apply them immediately.  Typically this is used to apply
2723  * corrections to be applied to the device defaults on startup, such
2724  * as the updates some vendors provide to undocumented registers.
2725  *
2726  * The caller must ensure that this function cannot be called
2727  * concurrently with either itself or regcache_sync().
2728  */
2729 int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
2730                           int num_regs)
2731 {
2732         struct reg_default *p;
2733         int ret;
2734         bool bypass;
2735
2736         if (WARN_ONCE(num_regs <= 0, "invalid registers number (%d)\n",
2737             num_regs))
2738                 return 0;
2739
2740         p = krealloc(map->patch,
2741                      sizeof(struct reg_default) * (map->patch_regs + num_regs),
2742                      GFP_KERNEL);
2743         if (p) {
2744                 memcpy(p + map->patch_regs, regs, num_regs * sizeof(*regs));
2745                 map->patch = p;
2746                 map->patch_regs += num_regs;
2747         } else {
2748                 return -ENOMEM;
2749         }
2750
2751         map->lock(map->lock_arg);
2752
2753         bypass = map->cache_bypass;
2754
2755         map->cache_bypass = true;
2756         map->async = true;
2757
2758         ret = _regmap_multi_reg_write(map, regs, num_regs);
2759
2760         map->async = false;
2761         map->cache_bypass = bypass;
2762
2763         map->unlock(map->lock_arg);
2764
2765         regmap_async_complete(map);
2766
2767         return ret;
2768 }
2769 EXPORT_SYMBOL_GPL(regmap_register_patch);
2770
2771 /*
2772  * regmap_get_val_bytes(): Report the size of a register value
2773  *
2774  * Report the size of a register value, mainly intended to for use by
2775  * generic infrastructure built on top of regmap.
2776  */
2777 int regmap_get_val_bytes(struct regmap *map)
2778 {
2779         if (map->format.format_write)
2780                 return -EINVAL;
2781
2782         return map->format.val_bytes;
2783 }
2784 EXPORT_SYMBOL_GPL(regmap_get_val_bytes);
2785
2786 /**
2787  * regmap_get_max_register(): Report the max register value
2788  *
2789  * Report the max register value, mainly intended to for use by
2790  * generic infrastructure built on top of regmap.
2791  */
2792 int regmap_get_max_register(struct regmap *map)
2793 {
2794         return map->max_register ? map->max_register : -EINVAL;
2795 }
2796 EXPORT_SYMBOL_GPL(regmap_get_max_register);
2797
2798 /**
2799  * regmap_get_reg_stride(): Report the register address stride
2800  *
2801  * Report the register address stride, mainly intended to for use by
2802  * generic infrastructure built on top of regmap.
2803  */
2804 int regmap_get_reg_stride(struct regmap *map)
2805 {
2806         return map->reg_stride;
2807 }
2808 EXPORT_SYMBOL_GPL(regmap_get_reg_stride);
2809
2810 int regmap_parse_val(struct regmap *map, const void *buf,
2811                         unsigned int *val)
2812 {
2813         if (!map->format.parse_val)
2814                 return -EINVAL;
2815
2816         *val = map->format.parse_val(buf);
2817
2818         return 0;
2819 }
2820 EXPORT_SYMBOL_GPL(regmap_parse_val);
2821
2822 static int __init regmap_initcall(void)
2823 {
2824         regmap_debugfs_initcall();
2825
2826         return 0;
2827 }
2828 postcore_initcall(regmap_initcall);