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