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