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