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