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