mfd: Improve performance of later WM1811 revisions
[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/slab.h>
14 #include <linux/module.h>
15 #include <linux/mutex.h>
16 #include <linux/err.h>
17
18 #define CREATE_TRACE_POINTS
19 #include <trace/events/regmap.h>
20
21 #include "internal.h"
22
23 bool regmap_writeable(struct regmap *map, unsigned int reg)
24 {
25         if (map->max_register && reg > map->max_register)
26                 return false;
27
28         if (map->writeable_reg)
29                 return map->writeable_reg(map->dev, reg);
30
31         return true;
32 }
33
34 bool regmap_readable(struct regmap *map, unsigned int reg)
35 {
36         if (map->max_register && reg > map->max_register)
37                 return false;
38
39         if (map->readable_reg)
40                 return map->readable_reg(map->dev, reg);
41
42         return true;
43 }
44
45 bool regmap_volatile(struct regmap *map, unsigned int reg)
46 {
47         if (map->max_register && reg > map->max_register)
48                 return false;
49
50         if (map->volatile_reg)
51                 return map->volatile_reg(map->dev, reg);
52
53         return true;
54 }
55
56 bool regmap_precious(struct regmap *map, unsigned int reg)
57 {
58         if (map->max_register && reg > map->max_register)
59                 return false;
60
61         if (map->precious_reg)
62                 return map->precious_reg(map->dev, reg);
63
64         return false;
65 }
66
67 static bool regmap_volatile_range(struct regmap *map, unsigned int reg,
68         unsigned int num)
69 {
70         unsigned int i;
71
72         for (i = 0; i < num; i++)
73                 if (!regmap_volatile(map, reg + i))
74                         return false;
75
76         return true;
77 }
78
79 static void regmap_format_4_12_write(struct regmap *map,
80                                      unsigned int reg, unsigned int val)
81 {
82         __be16 *out = map->work_buf;
83         *out = cpu_to_be16((reg << 12) | val);
84 }
85
86 static void regmap_format_7_9_write(struct regmap *map,
87                                     unsigned int reg, unsigned int val)
88 {
89         __be16 *out = map->work_buf;
90         *out = cpu_to_be16((reg << 9) | val);
91 }
92
93 static void regmap_format_10_14_write(struct regmap *map,
94                                     unsigned int reg, unsigned int val)
95 {
96         u8 *out = map->work_buf;
97
98         out[2] = val;
99         out[1] = (val >> 8) | (reg << 6);
100         out[0] = reg >> 2;
101 }
102
103 static void regmap_format_8(void *buf, unsigned int val)
104 {
105         u8 *b = buf;
106
107         b[0] = val;
108 }
109
110 static void regmap_format_16(void *buf, unsigned int val)
111 {
112         __be16 *b = buf;
113
114         b[0] = cpu_to_be16(val);
115 }
116
117 static unsigned int regmap_parse_8(void *buf)
118 {
119         u8 *b = buf;
120
121         return b[0];
122 }
123
124 static unsigned int regmap_parse_16(void *buf)
125 {
126         __be16 *b = buf;
127
128         b[0] = be16_to_cpu(b[0]);
129
130         return b[0];
131 }
132
133 /**
134  * regmap_init(): Initialise register map
135  *
136  * @dev: Device that will be interacted with
137  * @bus: Bus-specific callbacks to use with device
138  * @config: Configuration for register map
139  *
140  * The return value will be an ERR_PTR() on error or a valid pointer to
141  * a struct regmap.  This function should generally not be called
142  * directly, it should be called by bus-specific init functions.
143  */
144 struct regmap *regmap_init(struct device *dev,
145                            const struct regmap_bus *bus,
146                            const struct regmap_config *config)
147 {
148         struct regmap *map;
149         int ret = -EINVAL;
150
151         if (!bus || !config)
152                 goto err;
153
154         map = kzalloc(sizeof(*map), GFP_KERNEL);
155         if (map == NULL) {
156                 ret = -ENOMEM;
157                 goto err;
158         }
159
160         mutex_init(&map->lock);
161         map->format.buf_size = (config->reg_bits + config->val_bits) / 8;
162         map->format.reg_bytes = config->reg_bits / 8;
163         map->format.val_bytes = config->val_bits / 8;
164         map->dev = dev;
165         map->bus = bus;
166         map->max_register = config->max_register;
167         map->writeable_reg = config->writeable_reg;
168         map->readable_reg = config->readable_reg;
169         map->volatile_reg = config->volatile_reg;
170         map->precious_reg = config->precious_reg;
171         map->cache_type = config->cache_type;
172
173         if (config->read_flag_mask || config->write_flag_mask) {
174                 map->read_flag_mask = config->read_flag_mask;
175                 map->write_flag_mask = config->write_flag_mask;
176         } else {
177                 map->read_flag_mask = bus->read_flag_mask;
178         }
179
180         switch (config->reg_bits) {
181         case 4:
182                 switch (config->val_bits) {
183                 case 12:
184                         map->format.format_write = regmap_format_4_12_write;
185                         break;
186                 default:
187                         goto err_map;
188                 }
189                 break;
190
191         case 7:
192                 switch (config->val_bits) {
193                 case 9:
194                         map->format.format_write = regmap_format_7_9_write;
195                         break;
196                 default:
197                         goto err_map;
198                 }
199                 break;
200
201         case 10:
202                 switch (config->val_bits) {
203                 case 14:
204                         map->format.format_write = regmap_format_10_14_write;
205                         break;
206                 default:
207                         goto err_map;
208                 }
209                 break;
210
211         case 8:
212                 map->format.format_reg = regmap_format_8;
213                 break;
214
215         case 16:
216                 map->format.format_reg = regmap_format_16;
217                 break;
218
219         default:
220                 goto err_map;
221         }
222
223         switch (config->val_bits) {
224         case 8:
225                 map->format.format_val = regmap_format_8;
226                 map->format.parse_val = regmap_parse_8;
227                 break;
228         case 16:
229                 map->format.format_val = regmap_format_16;
230                 map->format.parse_val = regmap_parse_16;
231                 break;
232         }
233
234         if (!map->format.format_write &&
235             !(map->format.format_reg && map->format.format_val))
236                 goto err_map;
237
238         map->work_buf = kmalloc(map->format.buf_size, GFP_KERNEL);
239         if (map->work_buf == NULL) {
240                 ret = -ENOMEM;
241                 goto err_map;
242         }
243
244         regmap_debugfs_init(map);
245
246         ret = regcache_init(map, config);
247         if (ret < 0)
248                 goto err_free_workbuf;
249
250         return map;
251
252 err_free_workbuf:
253         kfree(map->work_buf);
254 err_map:
255         kfree(map);
256 err:
257         return ERR_PTR(ret);
258 }
259 EXPORT_SYMBOL_GPL(regmap_init);
260
261 static void devm_regmap_release(struct device *dev, void *res)
262 {
263         regmap_exit(*(struct regmap **)res);
264 }
265
266 /**
267  * devm_regmap_init(): Initialise managed register map
268  *
269  * @dev: Device that will be interacted with
270  * @bus: Bus-specific callbacks to use with device
271  * @config: Configuration for register map
272  *
273  * The return value will be an ERR_PTR() on error or a valid pointer
274  * to a struct regmap.  This function should generally not be called
275  * directly, it should be called by bus-specific init functions.  The
276  * map will be automatically freed by the device management code.
277  */
278 struct regmap *devm_regmap_init(struct device *dev,
279                                 const struct regmap_bus *bus,
280                                 const struct regmap_config *config)
281 {
282         struct regmap **ptr, *regmap;
283
284         ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL);
285         if (!ptr)
286                 return ERR_PTR(-ENOMEM);
287
288         regmap = regmap_init(dev, bus, config);
289         if (!IS_ERR(regmap)) {
290                 *ptr = regmap;
291                 devres_add(dev, ptr);
292         } else {
293                 devres_free(ptr);
294         }
295
296         return regmap;
297 }
298 EXPORT_SYMBOL_GPL(devm_regmap_init);
299
300 /**
301  * regmap_reinit_cache(): Reinitialise the current register cache
302  *
303  * @map: Register map to operate on.
304  * @config: New configuration.  Only the cache data will be used.
305  *
306  * Discard any existing register cache for the map and initialize a
307  * new cache.  This can be used to restore the cache to defaults or to
308  * update the cache configuration to reflect runtime discovery of the
309  * hardware.
310  */
311 int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
312 {
313         int ret;
314
315         mutex_lock(&map->lock);
316
317         regcache_exit(map);
318
319         map->max_register = config->max_register;
320         map->writeable_reg = config->writeable_reg;
321         map->readable_reg = config->readable_reg;
322         map->volatile_reg = config->volatile_reg;
323         map->precious_reg = config->precious_reg;
324         map->cache_type = config->cache_type;
325
326         map->cache_bypass = false;
327         map->cache_only = false;
328
329         ret = regcache_init(map, config);
330
331         mutex_unlock(&map->lock);
332
333         return ret;
334 }
335
336 /**
337  * regmap_exit(): Free a previously allocated register map
338  */
339 void regmap_exit(struct regmap *map)
340 {
341         regcache_exit(map);
342         regmap_debugfs_exit(map);
343         kfree(map->work_buf);
344         kfree(map);
345 }
346 EXPORT_SYMBOL_GPL(regmap_exit);
347
348 static int _regmap_raw_write(struct regmap *map, unsigned int reg,
349                              const void *val, size_t val_len)
350 {
351         u8 *u8 = map->work_buf;
352         void *buf;
353         int ret = -ENOTSUPP;
354         size_t len;
355         int i;
356
357         /* Check for unwritable registers before we start */
358         if (map->writeable_reg)
359                 for (i = 0; i < val_len / map->format.val_bytes; i++)
360                         if (!map->writeable_reg(map->dev, reg + i))
361                                 return -EINVAL;
362
363         map->format.format_reg(map->work_buf, reg);
364
365         u8[0] |= map->write_flag_mask;
366
367         trace_regmap_hw_write_start(map->dev, reg,
368                                     val_len / map->format.val_bytes);
369
370         /* If we're doing a single register write we can probably just
371          * send the work_buf directly, otherwise try to do a gather
372          * write.
373          */
374         if (val == map->work_buf + map->format.reg_bytes)
375                 ret = map->bus->write(map->dev, map->work_buf,
376                                       map->format.reg_bytes + val_len);
377         else if (map->bus->gather_write)
378                 ret = map->bus->gather_write(map->dev, map->work_buf,
379                                              map->format.reg_bytes,
380                                              val, val_len);
381
382         /* If that didn't work fall back on linearising by hand. */
383         if (ret == -ENOTSUPP) {
384                 len = map->format.reg_bytes + val_len;
385                 buf = kmalloc(len, GFP_KERNEL);
386                 if (!buf)
387                         return -ENOMEM;
388
389                 memcpy(buf, map->work_buf, map->format.reg_bytes);
390                 memcpy(buf + map->format.reg_bytes, val, val_len);
391                 ret = map->bus->write(map->dev, buf, len);
392
393                 kfree(buf);
394         }
395
396         trace_regmap_hw_write_done(map->dev, reg,
397                                    val_len / map->format.val_bytes);
398
399         return ret;
400 }
401
402 int _regmap_write(struct regmap *map, unsigned int reg,
403                   unsigned int val)
404 {
405         int ret;
406         BUG_ON(!map->format.format_write && !map->format.format_val);
407
408         if (!map->cache_bypass) {
409                 ret = regcache_write(map, reg, val);
410                 if (ret != 0)
411                         return ret;
412                 if (map->cache_only) {
413                         map->cache_dirty = true;
414                         return 0;
415                 }
416         }
417
418         trace_regmap_reg_write(map->dev, reg, val);
419
420         if (map->format.format_write) {
421                 map->format.format_write(map, reg, val);
422
423                 trace_regmap_hw_write_start(map->dev, reg, 1);
424
425                 ret = map->bus->write(map->dev, map->work_buf,
426                                       map->format.buf_size);
427
428                 trace_regmap_hw_write_done(map->dev, reg, 1);
429
430                 return ret;
431         } else {
432                 map->format.format_val(map->work_buf + map->format.reg_bytes,
433                                        val);
434                 return _regmap_raw_write(map, reg,
435                                          map->work_buf + map->format.reg_bytes,
436                                          map->format.val_bytes);
437         }
438 }
439
440 /**
441  * regmap_write(): Write a value to a single register
442  *
443  * @map: Register map to write to
444  * @reg: Register to write to
445  * @val: Value to be written
446  *
447  * A value of zero will be returned on success, a negative errno will
448  * be returned in error cases.
449  */
450 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
451 {
452         int ret;
453
454         mutex_lock(&map->lock);
455
456         ret = _regmap_write(map, reg, val);
457
458         mutex_unlock(&map->lock);
459
460         return ret;
461 }
462 EXPORT_SYMBOL_GPL(regmap_write);
463
464 /**
465  * regmap_raw_write(): Write raw values to one or more registers
466  *
467  * @map: Register map to write to
468  * @reg: Initial register to write to
469  * @val: Block of data to be written, laid out for direct transmission to the
470  *       device
471  * @val_len: Length of data pointed to by val.
472  *
473  * This function is intended to be used for things like firmware
474  * download where a large block of data needs to be transferred to the
475  * device.  No formatting will be done on the data provided.
476  *
477  * A value of zero will be returned on success, a negative errno will
478  * be returned in error cases.
479  */
480 int regmap_raw_write(struct regmap *map, unsigned int reg,
481                      const void *val, size_t val_len)
482 {
483         size_t val_count = val_len / map->format.val_bytes;
484         int ret;
485
486         WARN_ON(!regmap_volatile_range(map, reg, val_count) &&
487                 map->cache_type != REGCACHE_NONE);
488
489         mutex_lock(&map->lock);
490
491         ret = _regmap_raw_write(map, reg, val, val_len);
492
493         mutex_unlock(&map->lock);
494
495         return ret;
496 }
497 EXPORT_SYMBOL_GPL(regmap_raw_write);
498
499 static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
500                             unsigned int val_len)
501 {
502         u8 *u8 = map->work_buf;
503         int ret;
504
505         map->format.format_reg(map->work_buf, reg);
506
507         /*
508          * Some buses or devices flag reads by setting the high bits in the
509          * register addresss; since it's always the high bits for all
510          * current formats we can do this here rather than in
511          * formatting.  This may break if we get interesting formats.
512          */
513         u8[0] |= map->read_flag_mask;
514
515         trace_regmap_hw_read_start(map->dev, reg,
516                                    val_len / map->format.val_bytes);
517
518         ret = map->bus->read(map->dev, map->work_buf, map->format.reg_bytes,
519                              val, val_len);
520
521         trace_regmap_hw_read_done(map->dev, reg,
522                                   val_len / map->format.val_bytes);
523
524         return ret;
525 }
526
527 static int _regmap_read(struct regmap *map, unsigned int reg,
528                         unsigned int *val)
529 {
530         int ret;
531
532         if (!map->cache_bypass) {
533                 ret = regcache_read(map, reg, val);
534                 if (ret == 0)
535                         return 0;
536         }
537
538         if (!map->format.parse_val)
539                 return -EINVAL;
540
541         if (map->cache_only)
542                 return -EBUSY;
543
544         ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
545         if (ret == 0) {
546                 *val = map->format.parse_val(map->work_buf);
547                 trace_regmap_reg_read(map->dev, reg, *val);
548         }
549
550         return ret;
551 }
552
553 /**
554  * regmap_read(): Read a value from a single register
555  *
556  * @map: Register map to write to
557  * @reg: Register to be read from
558  * @val: Pointer to store read value
559  *
560  * A value of zero will be returned on success, a negative errno will
561  * be returned in error cases.
562  */
563 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
564 {
565         int ret;
566
567         mutex_lock(&map->lock);
568
569         ret = _regmap_read(map, reg, val);
570
571         mutex_unlock(&map->lock);
572
573         return ret;
574 }
575 EXPORT_SYMBOL_GPL(regmap_read);
576
577 /**
578  * regmap_raw_read(): Read raw data from the device
579  *
580  * @map: Register map to write to
581  * @reg: First register to be read from
582  * @val: Pointer to store read value
583  * @val_len: Size of data to read
584  *
585  * A value of zero will be returned on success, a negative errno will
586  * be returned in error cases.
587  */
588 int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
589                     size_t val_len)
590 {
591         size_t val_count = val_len / map->format.val_bytes;
592         int ret;
593
594         WARN_ON(!regmap_volatile_range(map, reg, val_count) &&
595                 map->cache_type != REGCACHE_NONE);
596
597         mutex_lock(&map->lock);
598
599         ret = _regmap_raw_read(map, reg, val, val_len);
600
601         mutex_unlock(&map->lock);
602
603         return ret;
604 }
605 EXPORT_SYMBOL_GPL(regmap_raw_read);
606
607 /**
608  * regmap_bulk_read(): Read multiple registers from the device
609  *
610  * @map: Register map to write to
611  * @reg: First register to be read from
612  * @val: Pointer to store read value, in native register size for device
613  * @val_count: Number of registers to read
614  *
615  * A value of zero will be returned on success, a negative errno will
616  * be returned in error cases.
617  */
618 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
619                      size_t val_count)
620 {
621         int ret, i;
622         size_t val_bytes = map->format.val_bytes;
623         bool vol = regmap_volatile_range(map, reg, val_count);
624
625         if (!map->format.parse_val)
626                 return -EINVAL;
627
628         if (vol || map->cache_type == REGCACHE_NONE) {
629                 ret = regmap_raw_read(map, reg, val, val_bytes * val_count);
630                 if (ret != 0)
631                         return ret;
632
633                 for (i = 0; i < val_count * val_bytes; i += val_bytes)
634                         map->format.parse_val(val + i);
635         } else {
636                 for (i = 0; i < val_count; i++) {
637                         ret = regmap_read(map, reg + i, val + (i * val_bytes));
638                         if (ret != 0)
639                                 return ret;
640                 }
641         }
642
643         return 0;
644 }
645 EXPORT_SYMBOL_GPL(regmap_bulk_read);
646
647 static int _regmap_update_bits(struct regmap *map, unsigned int reg,
648                                unsigned int mask, unsigned int val,
649                                bool *change)
650 {
651         int ret;
652         unsigned int tmp, orig;
653
654         mutex_lock(&map->lock);
655
656         ret = _regmap_read(map, reg, &orig);
657         if (ret != 0)
658                 goto out;
659
660         tmp = orig & ~mask;
661         tmp |= val & mask;
662
663         if (tmp != orig) {
664                 ret = _regmap_write(map, reg, tmp);
665                 *change = true;
666         } else {
667                 *change = false;
668         }
669
670 out:
671         mutex_unlock(&map->lock);
672
673         return ret;
674 }
675
676 /**
677  * regmap_update_bits: Perform a read/modify/write cycle on the register map
678  *
679  * @map: Register map to update
680  * @reg: Register to update
681  * @mask: Bitmask to change
682  * @val: New value for bitmask
683  *
684  * Returns zero for success, a negative number on error.
685  */
686 int regmap_update_bits(struct regmap *map, unsigned int reg,
687                        unsigned int mask, unsigned int val)
688 {
689         bool change;
690         return _regmap_update_bits(map, reg, mask, val, &change);
691 }
692 EXPORT_SYMBOL_GPL(regmap_update_bits);
693
694 /**
695  * regmap_update_bits_check: Perform a read/modify/write cycle on the
696  *                           register map and report if updated
697  *
698  * @map: Register map to update
699  * @reg: Register to update
700  * @mask: Bitmask to change
701  * @val: New value for bitmask
702  * @change: Boolean indicating if a write was done
703  *
704  * Returns zero for success, a negative number on error.
705  */
706 int regmap_update_bits_check(struct regmap *map, unsigned int reg,
707                              unsigned int mask, unsigned int val,
708                              bool *change)
709 {
710         return _regmap_update_bits(map, reg, mask, val, change);
711 }
712 EXPORT_SYMBOL_GPL(regmap_update_bits_check);
713
714 /**
715  * regmap_register_patch: Register and apply register updates to be applied
716  *                        on device initialistion
717  *
718  * @map: Register map to apply updates to.
719  * @regs: Values to update.
720  * @num_regs: Number of entries in regs.
721  *
722  * Register a set of register updates to be applied to the device
723  * whenever the device registers are synchronised with the cache and
724  * apply them immediately.  Typically this is used to apply
725  * corrections to be applied to the device defaults on startup, such
726  * as the updates some vendors provide to undocumented registers.
727  */
728 int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
729                           int num_regs)
730 {
731         int i, ret;
732         bool bypass;
733
734         /* If needed the implementation can be extended to support this */
735         if (map->patch)
736                 return -EBUSY;
737
738         mutex_lock(&map->lock);
739
740         bypass = map->cache_bypass;
741
742         map->cache_bypass = true;
743
744         /* Write out first; it's useful to apply even if we fail later. */
745         for (i = 0; i < num_regs; i++) {
746                 ret = _regmap_write(map, regs[i].reg, regs[i].def);
747                 if (ret != 0) {
748                         dev_err(map->dev, "Failed to write %x = %x: %d\n",
749                                 regs[i].reg, regs[i].def, ret);
750                         goto out;
751                 }
752         }
753
754         map->patch = kcalloc(num_regs, sizeof(struct reg_default), GFP_KERNEL);
755         if (map->patch != NULL) {
756                 memcpy(map->patch, regs,
757                        num_regs * sizeof(struct reg_default));
758                 map->patch_regs = num_regs;
759         } else {
760                 ret = -ENOMEM;
761         }
762
763 out:
764         map->cache_bypass = bypass;
765
766         mutex_unlock(&map->lock);
767
768         return ret;
769 }
770 EXPORT_SYMBOL_GPL(regmap_register_patch);
771
772 static int __init regmap_initcall(void)
773 {
774         regmap_debugfs_initcall();
775
776         return 0;
777 }
778 postcore_initcall(regmap_initcall);