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