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