]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/base/regmap/regmap.c
regmap: Implement dev_get_regmap()
[karo-tx-linux.git] / drivers / base / regmap / regmap.c
1 /*
2  * Register map access API
3  *
4  * Copyright 2011 Wolfson Microelectronics plc
5  *
6  * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12
13 #include <linux/device.h>
14 #include <linux/slab.h>
15 #include <linux/export.h>
16 #include <linux/mutex.h>
17 #include <linux/err.h>
18
19 #define CREATE_TRACE_POINTS
20 #include <trace/events/regmap.h>
21
22 #include "internal.h"
23
24 bool regmap_writeable(struct regmap *map, unsigned int reg)
25 {
26         if (map->max_register && reg > map->max_register)
27                 return false;
28
29         if (map->writeable_reg)
30                 return map->writeable_reg(map->dev, reg);
31
32         return true;
33 }
34
35 bool regmap_readable(struct regmap *map, unsigned int reg)
36 {
37         if (map->max_register && reg > map->max_register)
38                 return false;
39
40         if (map->format.format_write)
41                 return false;
42
43         if (map->readable_reg)
44                 return map->readable_reg(map->dev, reg);
45
46         return true;
47 }
48
49 bool regmap_volatile(struct regmap *map, unsigned int reg)
50 {
51         if (!regmap_readable(map, reg))
52                 return false;
53
54         if (map->volatile_reg)
55                 return map->volatile_reg(map->dev, reg);
56
57         return true;
58 }
59
60 bool regmap_precious(struct regmap *map, unsigned int reg)
61 {
62         if (!regmap_readable(map, reg))
63                 return false;
64
65         if (map->precious_reg)
66                 return map->precious_reg(map->dev, reg);
67
68         return false;
69 }
70
71 static bool regmap_volatile_range(struct regmap *map, unsigned int reg,
72         unsigned int num)
73 {
74         unsigned int i;
75
76         for (i = 0; i < num; i++)
77                 if (!regmap_volatile(map, reg + i))
78                         return false;
79
80         return true;
81 }
82
83 static void regmap_format_2_6_write(struct regmap *map,
84                                      unsigned int reg, unsigned int val)
85 {
86         u8 *out = map->work_buf;
87
88         *out = (reg << 6) | val;
89 }
90
91 static void regmap_format_4_12_write(struct regmap *map,
92                                      unsigned int reg, unsigned int val)
93 {
94         __be16 *out = map->work_buf;
95         *out = cpu_to_be16((reg << 12) | val);
96 }
97
98 static void regmap_format_7_9_write(struct regmap *map,
99                                     unsigned int reg, unsigned int val)
100 {
101         __be16 *out = map->work_buf;
102         *out = cpu_to_be16((reg << 9) | val);
103 }
104
105 static void regmap_format_10_14_write(struct regmap *map,
106                                     unsigned int reg, unsigned int val)
107 {
108         u8 *out = map->work_buf;
109
110         out[2] = val;
111         out[1] = (val >> 8) | (reg << 6);
112         out[0] = reg >> 2;
113 }
114
115 static void regmap_format_8(void *buf, unsigned int val)
116 {
117         u8 *b = buf;
118
119         b[0] = val;
120 }
121
122 static void regmap_format_16(void *buf, unsigned int val)
123 {
124         __be16 *b = buf;
125
126         b[0] = cpu_to_be16(val);
127 }
128
129 static void regmap_format_32(void *buf, unsigned int val)
130 {
131         __be32 *b = buf;
132
133         b[0] = cpu_to_be32(val);
134 }
135
136 static unsigned int regmap_parse_8(void *buf)
137 {
138         u8 *b = buf;
139
140         return b[0];
141 }
142
143 static unsigned int regmap_parse_16(void *buf)
144 {
145         __be16 *b = buf;
146
147         b[0] = be16_to_cpu(b[0]);
148
149         return b[0];
150 }
151
152 static unsigned int regmap_parse_32(void *buf)
153 {
154         __be32 *b = buf;
155
156         b[0] = be32_to_cpu(b[0]);
157
158         return b[0];
159 }
160
161 static void regmap_lock_mutex(struct regmap *map)
162 {
163         mutex_lock(&map->mutex);
164 }
165
166 static void regmap_unlock_mutex(struct regmap *map)
167 {
168         mutex_unlock(&map->mutex);
169 }
170
171 static void regmap_lock_spinlock(struct regmap *map)
172 {
173         spin_lock(&map->spinlock);
174 }
175
176 static void regmap_unlock_spinlock(struct regmap *map)
177 {
178         spin_unlock(&map->spinlock);
179 }
180
181 static void dev_get_regmap_release(struct device *dev, void *res)
182 {
183         /*
184          * We don't actually have anything to do here; the goal here
185          * is not to manage the regmap but to provide a simple way to
186          * get the regmap back given a struct device.
187          */
188 }
189
190 /**
191  * regmap_init(): Initialise register map
192  *
193  * @dev: Device that will be interacted with
194  * @bus: Bus-specific callbacks to use with device
195  * @bus_context: Data passed to bus-specific callbacks
196  * @config: Configuration for register map
197  *
198  * The return value will be an ERR_PTR() on error or a valid pointer to
199  * a struct regmap.  This function should generally not be called
200  * directly, it should be called by bus-specific init functions.
201  */
202 struct regmap *regmap_init(struct device *dev,
203                            const struct regmap_bus *bus,
204                            void *bus_context,
205                            const struct regmap_config *config)
206 {
207         struct regmap *map, **m;
208         int ret = -EINVAL;
209
210         if (!bus || !config)
211                 goto err;
212
213         map = kzalloc(sizeof(*map), GFP_KERNEL);
214         if (map == NULL) {
215                 ret = -ENOMEM;
216                 goto err;
217         }
218
219         if (bus->fast_io) {
220                 spin_lock_init(&map->spinlock);
221                 map->lock = regmap_lock_spinlock;
222                 map->unlock = regmap_unlock_spinlock;
223         } else {
224                 mutex_init(&map->mutex);
225                 map->lock = regmap_lock_mutex;
226                 map->unlock = regmap_unlock_mutex;
227         }
228         map->format.buf_size = (config->reg_bits + config->val_bits) / 8;
229         map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8);
230         map->format.pad_bytes = config->pad_bits / 8;
231         map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8);
232         map->format.buf_size += map->format.pad_bytes;
233         map->dev = dev;
234         map->bus = bus;
235         map->bus_context = bus_context;
236         map->max_register = config->max_register;
237         map->writeable_reg = config->writeable_reg;
238         map->readable_reg = config->readable_reg;
239         map->volatile_reg = config->volatile_reg;
240         map->precious_reg = config->precious_reg;
241         map->cache_type = config->cache_type;
242         map->name = config->name;
243
244         if (config->read_flag_mask || config->write_flag_mask) {
245                 map->read_flag_mask = config->read_flag_mask;
246                 map->write_flag_mask = config->write_flag_mask;
247         } else {
248                 map->read_flag_mask = bus->read_flag_mask;
249         }
250
251         switch (config->reg_bits) {
252         case 2:
253                 switch (config->val_bits) {
254                 case 6:
255                         map->format.format_write = regmap_format_2_6_write;
256                         break;
257                 default:
258                         goto err_map;
259                 }
260                 break;
261
262         case 4:
263                 switch (config->val_bits) {
264                 case 12:
265                         map->format.format_write = regmap_format_4_12_write;
266                         break;
267                 default:
268                         goto err_map;
269                 }
270                 break;
271
272         case 7:
273                 switch (config->val_bits) {
274                 case 9:
275                         map->format.format_write = regmap_format_7_9_write;
276                         break;
277                 default:
278                         goto err_map;
279                 }
280                 break;
281
282         case 10:
283                 switch (config->val_bits) {
284                 case 14:
285                         map->format.format_write = regmap_format_10_14_write;
286                         break;
287                 default:
288                         goto err_map;
289                 }
290                 break;
291
292         case 8:
293                 map->format.format_reg = regmap_format_8;
294                 break;
295
296         case 16:
297                 map->format.format_reg = regmap_format_16;
298                 break;
299
300         case 32:
301                 map->format.format_reg = regmap_format_32;
302                 break;
303
304         default:
305                 goto err_map;
306         }
307
308         switch (config->val_bits) {
309         case 8:
310                 map->format.format_val = regmap_format_8;
311                 map->format.parse_val = regmap_parse_8;
312                 break;
313         case 16:
314                 map->format.format_val = regmap_format_16;
315                 map->format.parse_val = regmap_parse_16;
316                 break;
317         case 32:
318                 map->format.format_val = regmap_format_32;
319                 map->format.parse_val = regmap_parse_32;
320                 break;
321         }
322
323         if (!map->format.format_write &&
324             !(map->format.format_reg && map->format.format_val))
325                 goto err_map;
326
327         map->work_buf = kzalloc(map->format.buf_size, GFP_KERNEL);
328         if (map->work_buf == NULL) {
329                 ret = -ENOMEM;
330                 goto err_map;
331         }
332
333         regmap_debugfs_init(map);
334
335         ret = regcache_init(map, config);
336         if (ret < 0)
337                 goto err_free_workbuf;
338
339         /* Add a devres resource for dev_get_regmap() */
340         m = devres_alloc(dev_get_regmap_release, sizeof(*m), GFP_KERNEL);
341         if (!m) {
342                 ret = -ENOMEM;
343                 goto err_cache;
344         }
345         *m = map;
346         devres_add(dev, m);
347
348         return map;
349
350 err_cache:
351         regcache_exit(map);
352 err_free_workbuf:
353         kfree(map->work_buf);
354 err_map:
355         kfree(map);
356 err:
357         return ERR_PTR(ret);
358 }
359 EXPORT_SYMBOL_GPL(regmap_init);
360
361 static void devm_regmap_release(struct device *dev, void *res)
362 {
363         regmap_exit(*(struct regmap **)res);
364 }
365
366 /**
367  * devm_regmap_init(): Initialise managed register map
368  *
369  * @dev: Device that will be interacted with
370  * @bus: Bus-specific callbacks to use with device
371  * @bus_context: Data passed to bus-specific callbacks
372  * @config: Configuration for register map
373  *
374  * The return value will be an ERR_PTR() on error or a valid pointer
375  * to a struct regmap.  This function should generally not be called
376  * directly, it should be called by bus-specific init functions.  The
377  * map will be automatically freed by the device management code.
378  */
379 struct regmap *devm_regmap_init(struct device *dev,
380                                 const struct regmap_bus *bus,
381                                 void *bus_context,
382                                 const struct regmap_config *config)
383 {
384         struct regmap **ptr, *regmap;
385
386         ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL);
387         if (!ptr)
388                 return ERR_PTR(-ENOMEM);
389
390         regmap = regmap_init(dev, bus, bus_context, config);
391         if (!IS_ERR(regmap)) {
392                 *ptr = regmap;
393                 devres_add(dev, ptr);
394         } else {
395                 devres_free(ptr);
396         }
397
398         return regmap;
399 }
400 EXPORT_SYMBOL_GPL(devm_regmap_init);
401
402 /**
403  * regmap_reinit_cache(): Reinitialise the current register cache
404  *
405  * @map: Register map to operate on.
406  * @config: New configuration.  Only the cache data will be used.
407  *
408  * Discard any existing register cache for the map and initialize a
409  * new cache.  This can be used to restore the cache to defaults or to
410  * update the cache configuration to reflect runtime discovery of the
411  * hardware.
412  */
413 int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
414 {
415         int ret;
416
417         map->lock(map);
418
419         regcache_exit(map);
420         regmap_debugfs_exit(map);
421
422         map->max_register = config->max_register;
423         map->writeable_reg = config->writeable_reg;
424         map->readable_reg = config->readable_reg;
425         map->volatile_reg = config->volatile_reg;
426         map->precious_reg = config->precious_reg;
427         map->cache_type = config->cache_type;
428
429         regmap_debugfs_init(map);
430
431         map->cache_bypass = false;
432         map->cache_only = false;
433
434         ret = regcache_init(map, config);
435
436         map->unlock(map);
437
438         return ret;
439 }
440
441 /**
442  * regmap_exit(): Free a previously allocated register map
443  */
444 void regmap_exit(struct regmap *map)
445 {
446         regcache_exit(map);
447         regmap_debugfs_exit(map);
448         if (map->bus->free_context)
449                 map->bus->free_context(map->bus_context);
450         kfree(map->work_buf);
451         kfree(map);
452 }
453 EXPORT_SYMBOL_GPL(regmap_exit);
454
455 static int dev_get_regmap_match(struct device *dev, void *res, void *data)
456 {
457         struct regmap **r = res;
458         if (!r || !*r) {
459                 WARN_ON(!r || !*r);
460                 return 0;
461         }
462
463         /* If the user didn't specify a name match any */
464         if (data)
465                 return (*r)->name == data;
466         else
467                 return 1;
468 }
469
470 /**
471  * dev_get_regmap(): Obtain the regmap (if any) for a device
472  *
473  * @dev: Device to retrieve the map for
474  * @name: Optional name for the register map, usually NULL.
475  *
476  * Returns the regmap for the device if one is present, or NULL.  If
477  * name is specified then it must match the name specified when
478  * registering the device, if it is NULL then the first regmap found
479  * will be used.  Devices with multiple register maps are very rare,
480  * generic code should normally not need to specify a name.
481  */
482 struct regmap *dev_get_regmap(struct device *dev, const char *name)
483 {
484         struct regmap **r = devres_find(dev, dev_get_regmap_release,
485                                         dev_get_regmap_match, (void *)name);
486
487         if (!r)
488                 return NULL;
489         return *r;
490 }
491 EXPORT_SYMBOL_GPL(dev_get_regmap);
492
493 static int _regmap_raw_write(struct regmap *map, unsigned int reg,
494                              const void *val, size_t val_len)
495 {
496         u8 *u8 = map->work_buf;
497         void *buf;
498         int ret = -ENOTSUPP;
499         size_t len;
500         int i;
501
502         /* Check for unwritable registers before we start */
503         if (map->writeable_reg)
504                 for (i = 0; i < val_len / map->format.val_bytes; i++)
505                         if (!map->writeable_reg(map->dev, reg + i))
506                                 return -EINVAL;
507
508         if (!map->cache_bypass && map->format.parse_val) {
509                 unsigned int ival;
510                 int val_bytes = map->format.val_bytes;
511                 for (i = 0; i < val_len / val_bytes; i++) {
512                         memcpy(map->work_buf, val + (i * val_bytes), val_bytes);
513                         ival = map->format.parse_val(map->work_buf);
514                         ret = regcache_write(map, reg + i, ival);
515                         if (ret) {
516                                 dev_err(map->dev,
517                                    "Error in caching of register: %u ret: %d\n",
518                                         reg + i, ret);
519                                 return ret;
520                         }
521                 }
522                 if (map->cache_only) {
523                         map->cache_dirty = true;
524                         return 0;
525                 }
526         }
527
528         map->format.format_reg(map->work_buf, reg);
529
530         u8[0] |= map->write_flag_mask;
531
532         trace_regmap_hw_write_start(map->dev, reg,
533                                     val_len / map->format.val_bytes);
534
535         /* If we're doing a single register write we can probably just
536          * send the work_buf directly, otherwise try to do a gather
537          * write.
538          */
539         if (val == (map->work_buf + map->format.pad_bytes +
540                     map->format.reg_bytes))
541                 ret = map->bus->write(map->bus_context, map->work_buf,
542                                       map->format.reg_bytes +
543                                       map->format.pad_bytes +
544                                       val_len);
545         else if (map->bus->gather_write)
546                 ret = map->bus->gather_write(map->bus_context, map->work_buf,
547                                              map->format.reg_bytes +
548                                              map->format.pad_bytes,
549                                              val, val_len);
550
551         /* If that didn't work fall back on linearising by hand. */
552         if (ret == -ENOTSUPP) {
553                 len = map->format.reg_bytes + map->format.pad_bytes + val_len;
554                 buf = kzalloc(len, GFP_KERNEL);
555                 if (!buf)
556                         return -ENOMEM;
557
558                 memcpy(buf, map->work_buf, map->format.reg_bytes);
559                 memcpy(buf + map->format.reg_bytes + map->format.pad_bytes,
560                        val, val_len);
561                 ret = map->bus->write(map->bus_context, buf, len);
562
563                 kfree(buf);
564         }
565
566         trace_regmap_hw_write_done(map->dev, reg,
567                                    val_len / map->format.val_bytes);
568
569         return ret;
570 }
571
572 int _regmap_write(struct regmap *map, unsigned int reg,
573                   unsigned int val)
574 {
575         int ret;
576         BUG_ON(!map->format.format_write && !map->format.format_val);
577
578         if (!map->cache_bypass && map->format.format_write) {
579                 ret = regcache_write(map, reg, val);
580                 if (ret != 0)
581                         return ret;
582                 if (map->cache_only) {
583                         map->cache_dirty = true;
584                         return 0;
585                 }
586         }
587
588         trace_regmap_reg_write(map->dev, reg, val);
589
590         if (map->format.format_write) {
591                 map->format.format_write(map, reg, val);
592
593                 trace_regmap_hw_write_start(map->dev, reg, 1);
594
595                 ret = map->bus->write(map->bus_context, map->work_buf,
596                                       map->format.buf_size);
597
598                 trace_regmap_hw_write_done(map->dev, reg, 1);
599
600                 return ret;
601         } else {
602                 map->format.format_val(map->work_buf + map->format.reg_bytes
603                                        + map->format.pad_bytes, val);
604                 return _regmap_raw_write(map, reg,
605                                          map->work_buf +
606                                          map->format.reg_bytes +
607                                          map->format.pad_bytes,
608                                          map->format.val_bytes);
609         }
610 }
611
612 /**
613  * regmap_write(): Write a value to a single register
614  *
615  * @map: Register map to write to
616  * @reg: Register to write to
617  * @val: Value to be written
618  *
619  * A value of zero will be returned on success, a negative errno will
620  * be returned in error cases.
621  */
622 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
623 {
624         int ret;
625
626         map->lock(map);
627
628         ret = _regmap_write(map, reg, val);
629
630         map->unlock(map);
631
632         return ret;
633 }
634 EXPORT_SYMBOL_GPL(regmap_write);
635
636 /**
637  * regmap_raw_write(): Write raw values to one or more registers
638  *
639  * @map: Register map to write to
640  * @reg: Initial register to write to
641  * @val: Block of data to be written, laid out for direct transmission to the
642  *       device
643  * @val_len: Length of data pointed to by val.
644  *
645  * This function is intended to be used for things like firmware
646  * download where a large block of data needs to be transferred to the
647  * device.  No formatting will be done on the data provided.
648  *
649  * A value of zero will be returned on success, a negative errno will
650  * be returned in error cases.
651  */
652 int regmap_raw_write(struct regmap *map, unsigned int reg,
653                      const void *val, size_t val_len)
654 {
655         int ret;
656
657         if (val_len % map->format.val_bytes)
658                 return -EINVAL;
659
660         map->lock(map);
661
662         ret = _regmap_raw_write(map, reg, val, val_len);
663
664         map->unlock(map);
665
666         return ret;
667 }
668 EXPORT_SYMBOL_GPL(regmap_raw_write);
669
670 /*
671  * regmap_bulk_write(): Write multiple registers to the device
672  *
673  * @map: Register map to write to
674  * @reg: First register to be write from
675  * @val: Block of data to be written, in native register size for device
676  * @val_count: Number of registers to write
677  *
678  * This function is intended to be used for writing a large block of
679  * data to be device either in single transfer or multiple transfer.
680  *
681  * A value of zero will be returned on success, a negative errno will
682  * be returned in error cases.
683  */
684 int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
685                      size_t val_count)
686 {
687         int ret = 0, i;
688         size_t val_bytes = map->format.val_bytes;
689         void *wval;
690
691         if (!map->format.parse_val)
692                 return -EINVAL;
693
694         map->lock(map);
695
696         /* No formatting is require if val_byte is 1 */
697         if (val_bytes == 1) {
698                 wval = (void *)val;
699         } else {
700                 wval = kmemdup(val, val_count * val_bytes, GFP_KERNEL);
701                 if (!wval) {
702                         ret = -ENOMEM;
703                         dev_err(map->dev, "Error in memory allocation\n");
704                         goto out;
705                 }
706                 for (i = 0; i < val_count * val_bytes; i += val_bytes)
707                         map->format.parse_val(wval + i);
708         }
709         ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count);
710
711         if (val_bytes != 1)
712                 kfree(wval);
713
714 out:
715         map->unlock(map);
716         return ret;
717 }
718 EXPORT_SYMBOL_GPL(regmap_bulk_write);
719
720 static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
721                             unsigned int val_len)
722 {
723         u8 *u8 = map->work_buf;
724         int ret;
725
726         map->format.format_reg(map->work_buf, reg);
727
728         /*
729          * Some buses or devices flag reads by setting the high bits in the
730          * register addresss; since it's always the high bits for all
731          * current formats we can do this here rather than in
732          * formatting.  This may break if we get interesting formats.
733          */
734         u8[0] |= map->read_flag_mask;
735
736         trace_regmap_hw_read_start(map->dev, reg,
737                                    val_len / map->format.val_bytes);
738
739         ret = map->bus->read(map->bus_context, map->work_buf,
740                              map->format.reg_bytes + map->format.pad_bytes,
741                              val, val_len);
742
743         trace_regmap_hw_read_done(map->dev, reg,
744                                   val_len / map->format.val_bytes);
745
746         return ret;
747 }
748
749 static int _regmap_read(struct regmap *map, unsigned int reg,
750                         unsigned int *val)
751 {
752         int ret;
753
754         if (!map->cache_bypass) {
755                 ret = regcache_read(map, reg, val);
756                 if (ret == 0)
757                         return 0;
758         }
759
760         if (!map->format.parse_val)
761                 return -EINVAL;
762
763         if (map->cache_only)
764                 return -EBUSY;
765
766         ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
767         if (ret == 0) {
768                 *val = map->format.parse_val(map->work_buf);
769                 trace_regmap_reg_read(map->dev, reg, *val);
770         }
771
772         return ret;
773 }
774
775 /**
776  * regmap_read(): Read a value from a single register
777  *
778  * @map: Register map to write to
779  * @reg: Register to be read from
780  * @val: Pointer to store read value
781  *
782  * A value of zero will be returned on success, a negative errno will
783  * be returned in error cases.
784  */
785 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
786 {
787         int ret;
788
789         map->lock(map);
790
791         ret = _regmap_read(map, reg, val);
792
793         map->unlock(map);
794
795         return ret;
796 }
797 EXPORT_SYMBOL_GPL(regmap_read);
798
799 /**
800  * regmap_raw_read(): Read raw data from the device
801  *
802  * @map: Register map to write to
803  * @reg: First register to be read from
804  * @val: Pointer to store read value
805  * @val_len: Size of data to read
806  *
807  * A value of zero will be returned on success, a negative errno will
808  * be returned in error cases.
809  */
810 int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
811                     size_t val_len)
812 {
813         size_t val_bytes = map->format.val_bytes;
814         size_t val_count = val_len / val_bytes;
815         unsigned int v;
816         int ret, i;
817
818         if (val_len % map->format.val_bytes)
819                 return -EINVAL;
820
821         map->lock(map);
822
823         if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
824             map->cache_type == REGCACHE_NONE) {
825                 /* Physical block read if there's no cache involved */
826                 ret = _regmap_raw_read(map, reg, val, val_len);
827
828         } else {
829                 /* Otherwise go word by word for the cache; should be low
830                  * cost as we expect to hit the cache.
831                  */
832                 for (i = 0; i < val_count; i++) {
833                         ret = _regmap_read(map, reg + i, &v);
834                         if (ret != 0)
835                                 goto out;
836
837                         map->format.format_val(val + (i * val_bytes), v);
838                 }
839         }
840
841  out:
842         map->unlock(map);
843
844         return ret;
845 }
846 EXPORT_SYMBOL_GPL(regmap_raw_read);
847
848 /**
849  * regmap_bulk_read(): Read multiple registers from the device
850  *
851  * @map: Register map to write to
852  * @reg: First register to be read from
853  * @val: Pointer to store read value, in native register size for device
854  * @val_count: Number of registers to read
855  *
856  * A value of zero will be returned on success, a negative errno will
857  * be returned in error cases.
858  */
859 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
860                      size_t val_count)
861 {
862         int ret, i;
863         size_t val_bytes = map->format.val_bytes;
864         bool vol = regmap_volatile_range(map, reg, val_count);
865
866         if (!map->format.parse_val)
867                 return -EINVAL;
868
869         if (vol || map->cache_type == REGCACHE_NONE) {
870                 ret = regmap_raw_read(map, reg, val, val_bytes * val_count);
871                 if (ret != 0)
872                         return ret;
873
874                 for (i = 0; i < val_count * val_bytes; i += val_bytes)
875                         map->format.parse_val(val + i);
876         } else {
877                 for (i = 0; i < val_count; i++) {
878                         ret = regmap_read(map, reg + i, val + (i * val_bytes));
879                         if (ret != 0)
880                                 return ret;
881                 }
882         }
883
884         return 0;
885 }
886 EXPORT_SYMBOL_GPL(regmap_bulk_read);
887
888 static int _regmap_update_bits(struct regmap *map, unsigned int reg,
889                                unsigned int mask, unsigned int val,
890                                bool *change)
891 {
892         int ret;
893         unsigned int tmp, orig;
894
895         map->lock(map);
896
897         ret = _regmap_read(map, reg, &orig);
898         if (ret != 0)
899                 goto out;
900
901         tmp = orig & ~mask;
902         tmp |= val & mask;
903
904         if (tmp != orig) {
905                 ret = _regmap_write(map, reg, tmp);
906                 *change = true;
907         } else {
908                 *change = false;
909         }
910
911 out:
912         map->unlock(map);
913
914         return ret;
915 }
916
917 /**
918  * regmap_update_bits: Perform a read/modify/write cycle on the register map
919  *
920  * @map: Register map to update
921  * @reg: Register to update
922  * @mask: Bitmask to change
923  * @val: New value for bitmask
924  *
925  * Returns zero for success, a negative number on error.
926  */
927 int regmap_update_bits(struct regmap *map, unsigned int reg,
928                        unsigned int mask, unsigned int val)
929 {
930         bool change;
931         return _regmap_update_bits(map, reg, mask, val, &change);
932 }
933 EXPORT_SYMBOL_GPL(regmap_update_bits);
934
935 /**
936  * regmap_update_bits_check: Perform a read/modify/write cycle on the
937  *                           register map and report if updated
938  *
939  * @map: Register map to update
940  * @reg: Register to update
941  * @mask: Bitmask to change
942  * @val: New value for bitmask
943  * @change: Boolean indicating if a write was done
944  *
945  * Returns zero for success, a negative number on error.
946  */
947 int regmap_update_bits_check(struct regmap *map, unsigned int reg,
948                              unsigned int mask, unsigned int val,
949                              bool *change)
950 {
951         return _regmap_update_bits(map, reg, mask, val, change);
952 }
953 EXPORT_SYMBOL_GPL(regmap_update_bits_check);
954
955 /**
956  * regmap_register_patch: Register and apply register updates to be applied
957  *                        on device initialistion
958  *
959  * @map: Register map to apply updates to.
960  * @regs: Values to update.
961  * @num_regs: Number of entries in regs.
962  *
963  * Register a set of register updates to be applied to the device
964  * whenever the device registers are synchronised with the cache and
965  * apply them immediately.  Typically this is used to apply
966  * corrections to be applied to the device defaults on startup, such
967  * as the updates some vendors provide to undocumented registers.
968  */
969 int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
970                           int num_regs)
971 {
972         int i, ret;
973         bool bypass;
974
975         /* If needed the implementation can be extended to support this */
976         if (map->patch)
977                 return -EBUSY;
978
979         map->lock(map);
980
981         bypass = map->cache_bypass;
982
983         map->cache_bypass = true;
984
985         /* Write out first; it's useful to apply even if we fail later. */
986         for (i = 0; i < num_regs; i++) {
987                 ret = _regmap_write(map, regs[i].reg, regs[i].def);
988                 if (ret != 0) {
989                         dev_err(map->dev, "Failed to write %x = %x: %d\n",
990                                 regs[i].reg, regs[i].def, ret);
991                         goto out;
992                 }
993         }
994
995         map->patch = kcalloc(num_regs, sizeof(struct reg_default), GFP_KERNEL);
996         if (map->patch != NULL) {
997                 memcpy(map->patch, regs,
998                        num_regs * sizeof(struct reg_default));
999                 map->patch_regs = num_regs;
1000         } else {
1001                 ret = -ENOMEM;
1002         }
1003
1004 out:
1005         map->cache_bypass = bypass;
1006
1007         map->unlock(map);
1008
1009         return ret;
1010 }
1011 EXPORT_SYMBOL_GPL(regmap_register_patch);
1012
1013 /*
1014  * regmap_get_val_bytes(): Report the size of a register value
1015  *
1016  * Report the size of a register value, mainly intended to for use by
1017  * generic infrastructure built on top of regmap.
1018  */
1019 int regmap_get_val_bytes(struct regmap *map)
1020 {
1021         if (map->format.format_write)
1022                 return -EINVAL;
1023
1024         return map->format.val_bytes;
1025 }
1026 EXPORT_SYMBOL_GPL(regmap_get_val_bytes);
1027
1028 static int __init regmap_initcall(void)
1029 {
1030         regmap_debugfs_initcall();
1031
1032         return 0;
1033 }
1034 postcore_initcall(regmap_initcall);