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