]> Git Repo - linux.git/blob - drivers/base/regmap/regcache.c
Merge remote-tracking branch 'regmap/topic/core' into regmap-next
[linux.git] / drivers / base / regmap / regcache.c
1 /*
2  * Register cache access API
3  *
4  * Copyright 2011 Wolfson Microelectronics plc
5  *
6  * Author: Dimitris Papastamos <[email protected]>
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/bsearch.h>
14 #include <linux/device.h>
15 #include <linux/export.h>
16 #include <linux/slab.h>
17 #include <linux/sort.h>
18
19 #include "trace.h"
20 #include "internal.h"
21
22 static const struct regcache_ops *cache_types[] = {
23         &regcache_rbtree_ops,
24         &regcache_lzo_ops,
25         &regcache_flat_ops,
26 };
27
28 static int regcache_hw_init(struct regmap *map)
29 {
30         int i, j;
31         int ret;
32         int count;
33         unsigned int val;
34         void *tmp_buf;
35
36         if (!map->num_reg_defaults_raw)
37                 return -EINVAL;
38
39         /* calculate the size of reg_defaults */
40         for (count = 0, i = 0; i < map->num_reg_defaults_raw; i++)
41                 if (!regmap_volatile(map, i * map->reg_stride))
42                         count++;
43
44         /* all registers are volatile, so just bypass */
45         if (!count) {
46                 map->cache_bypass = true;
47                 return 0;
48         }
49
50         map->num_reg_defaults = count;
51         map->reg_defaults = kmalloc_array(count, sizeof(struct reg_default),
52                                           GFP_KERNEL);
53         if (!map->reg_defaults)
54                 return -ENOMEM;
55
56         if (!map->reg_defaults_raw) {
57                 bool cache_bypass = map->cache_bypass;
58                 dev_warn(map->dev, "No cache defaults, reading back from HW\n");
59
60                 /* Bypass the cache access till data read from HW*/
61                 map->cache_bypass = true;
62                 tmp_buf = kmalloc(map->cache_size_raw, GFP_KERNEL);
63                 if (!tmp_buf) {
64                         ret = -ENOMEM;
65                         goto err_free;
66                 }
67                 ret = regmap_raw_read(map, 0, tmp_buf,
68                                       map->num_reg_defaults_raw);
69                 map->cache_bypass = cache_bypass;
70                 if (ret < 0)
71                         goto err_cache_free;
72
73                 map->reg_defaults_raw = tmp_buf;
74                 map->cache_free = 1;
75         }
76
77         /* fill the reg_defaults */
78         for (i = 0, j = 0; i < map->num_reg_defaults_raw; i++) {
79                 if (regmap_volatile(map, i * map->reg_stride))
80                         continue;
81                 val = regcache_get_val(map, map->reg_defaults_raw, i);
82                 map->reg_defaults[j].reg = i * map->reg_stride;
83                 map->reg_defaults[j].def = val;
84                 j++;
85         }
86
87         return 0;
88
89 err_cache_free:
90         kfree(tmp_buf);
91 err_free:
92         kfree(map->reg_defaults);
93
94         return ret;
95 }
96
97 int regcache_init(struct regmap *map, const struct regmap_config *config)
98 {
99         int ret;
100         int i;
101         void *tmp_buf;
102
103         if (map->cache_type == REGCACHE_NONE) {
104                 if (config->reg_defaults || config->num_reg_defaults_raw)
105                         dev_warn(map->dev,
106                                  "No cache used with register defaults set!\n");
107
108                 map->cache_bypass = true;
109                 return 0;
110         }
111
112         if (config->reg_defaults && !config->num_reg_defaults) {
113                 dev_err(map->dev,
114                          "Register defaults are set without the number!\n");
115                 return -EINVAL;
116         }
117
118         for (i = 0; i < config->num_reg_defaults; i++)
119                 if (config->reg_defaults[i].reg % map->reg_stride)
120                         return -EINVAL;
121
122         for (i = 0; i < ARRAY_SIZE(cache_types); i++)
123                 if (cache_types[i]->type == map->cache_type)
124                         break;
125
126         if (i == ARRAY_SIZE(cache_types)) {
127                 dev_err(map->dev, "Could not match compress type: %d\n",
128                         map->cache_type);
129                 return -EINVAL;
130         }
131
132         map->num_reg_defaults = config->num_reg_defaults;
133         map->num_reg_defaults_raw = config->num_reg_defaults_raw;
134         map->reg_defaults_raw = config->reg_defaults_raw;
135         map->cache_word_size = DIV_ROUND_UP(config->val_bits, 8);
136         map->cache_size_raw = map->cache_word_size * config->num_reg_defaults_raw;
137
138         map->cache = NULL;
139         map->cache_ops = cache_types[i];
140
141         if (!map->cache_ops->read ||
142             !map->cache_ops->write ||
143             !map->cache_ops->name)
144                 return -EINVAL;
145
146         /* We still need to ensure that the reg_defaults
147          * won't vanish from under us.  We'll need to make
148          * a copy of it.
149          */
150         if (config->reg_defaults) {
151                 tmp_buf = kmemdup(config->reg_defaults, map->num_reg_defaults *
152                                   sizeof(struct reg_default), GFP_KERNEL);
153                 if (!tmp_buf)
154                         return -ENOMEM;
155                 map->reg_defaults = tmp_buf;
156         } else if (map->num_reg_defaults_raw) {
157                 /* Some devices such as PMICs don't have cache defaults,
158                  * we cope with this by reading back the HW registers and
159                  * crafting the cache defaults by hand.
160                  */
161                 ret = regcache_hw_init(map);
162                 if (ret < 0)
163                         return ret;
164                 if (map->cache_bypass)
165                         return 0;
166         }
167
168         if (!map->max_register)
169                 map->max_register = map->num_reg_defaults_raw;
170
171         if (map->cache_ops->init) {
172                 dev_dbg(map->dev, "Initializing %s cache\n",
173                         map->cache_ops->name);
174                 ret = map->cache_ops->init(map);
175                 if (ret)
176                         goto err_free;
177         }
178         return 0;
179
180 err_free:
181         kfree(map->reg_defaults);
182         if (map->cache_free)
183                 kfree(map->reg_defaults_raw);
184
185         return ret;
186 }
187
188 void regcache_exit(struct regmap *map)
189 {
190         if (map->cache_type == REGCACHE_NONE)
191                 return;
192
193         BUG_ON(!map->cache_ops);
194
195         kfree(map->reg_defaults);
196         if (map->cache_free)
197                 kfree(map->reg_defaults_raw);
198
199         if (map->cache_ops->exit) {
200                 dev_dbg(map->dev, "Destroying %s cache\n",
201                         map->cache_ops->name);
202                 map->cache_ops->exit(map);
203         }
204 }
205
206 /**
207  * regcache_read: Fetch the value of a given register from the cache.
208  *
209  * @map: map to configure.
210  * @reg: The register index.
211  * @value: The value to be returned.
212  *
213  * Return a negative value on failure, 0 on success.
214  */
215 int regcache_read(struct regmap *map,
216                   unsigned int reg, unsigned int *value)
217 {
218         int ret;
219
220         if (map->cache_type == REGCACHE_NONE)
221                 return -ENOSYS;
222
223         BUG_ON(!map->cache_ops);
224
225         if (!regmap_volatile(map, reg)) {
226                 ret = map->cache_ops->read(map, reg, value);
227
228                 if (ret == 0)
229                         trace_regmap_reg_read_cache(map, reg, *value);
230
231                 return ret;
232         }
233
234         return -EINVAL;
235 }
236
237 /**
238  * regcache_write: Set the value of a given register in the cache.
239  *
240  * @map: map to configure.
241  * @reg: The register index.
242  * @value: The new register value.
243  *
244  * Return a negative value on failure, 0 on success.
245  */
246 int regcache_write(struct regmap *map,
247                    unsigned int reg, unsigned int value)
248 {
249         if (map->cache_type == REGCACHE_NONE)
250                 return 0;
251
252         BUG_ON(!map->cache_ops);
253
254         if (!regmap_volatile(map, reg))
255                 return map->cache_ops->write(map, reg, value);
256
257         return 0;
258 }
259
260 static bool regcache_reg_needs_sync(struct regmap *map, unsigned int reg,
261                                     unsigned int val)
262 {
263         int ret;
264
265         /* If we don't know the chip just got reset, then sync everything. */
266         if (!map->no_sync_defaults)
267                 return true;
268
269         /* Is this the hardware default?  If so skip. */
270         ret = regcache_lookup_reg(map, reg);
271         if (ret >= 0 && val == map->reg_defaults[ret].def)
272                 return false;
273         return true;
274 }
275
276 static int regcache_default_sync(struct regmap *map, unsigned int min,
277                                  unsigned int max)
278 {
279         unsigned int reg;
280
281         for (reg = min; reg <= max; reg += map->reg_stride) {
282                 unsigned int val;
283                 int ret;
284
285                 if (regmap_volatile(map, reg) ||
286                     !regmap_writeable(map, reg))
287                         continue;
288
289                 ret = regcache_read(map, reg, &val);
290                 if (ret)
291                         return ret;
292
293                 if (!regcache_reg_needs_sync(map, reg, val))
294                         continue;
295
296                 map->cache_bypass = true;
297                 ret = _regmap_write(map, reg, val);
298                 map->cache_bypass = false;
299                 if (ret) {
300                         dev_err(map->dev, "Unable to sync register %#x. %d\n",
301                                 reg, ret);
302                         return ret;
303                 }
304                 dev_dbg(map->dev, "Synced register %#x, value %#x\n", reg, val);
305         }
306
307         return 0;
308 }
309
310 /**
311  * regcache_sync: Sync the register cache with the hardware.
312  *
313  * @map: map to configure.
314  *
315  * Any registers that should not be synced should be marked as
316  * volatile.  In general drivers can choose not to use the provided
317  * syncing functionality if they so require.
318  *
319  * Return a negative value on failure, 0 on success.
320  */
321 int regcache_sync(struct regmap *map)
322 {
323         int ret = 0;
324         unsigned int i;
325         const char *name;
326         bool bypass;
327
328         BUG_ON(!map->cache_ops);
329
330         map->lock(map->lock_arg);
331         /* Remember the initial bypass state */
332         bypass = map->cache_bypass;
333         dev_dbg(map->dev, "Syncing %s cache\n",
334                 map->cache_ops->name);
335         name = map->cache_ops->name;
336         trace_regcache_sync(map, name, "start");
337
338         if (!map->cache_dirty)
339                 goto out;
340
341         map->async = true;
342
343         /* Apply any patch first */
344         map->cache_bypass = true;
345         for (i = 0; i < map->patch_regs; i++) {
346                 ret = _regmap_write(map, map->patch[i].reg, map->patch[i].def);
347                 if (ret != 0) {
348                         dev_err(map->dev, "Failed to write %x = %x: %d\n",
349                                 map->patch[i].reg, map->patch[i].def, ret);
350                         goto out;
351                 }
352         }
353         map->cache_bypass = false;
354
355         if (map->cache_ops->sync)
356                 ret = map->cache_ops->sync(map, 0, map->max_register);
357         else
358                 ret = regcache_default_sync(map, 0, map->max_register);
359
360         if (ret == 0)
361                 map->cache_dirty = false;
362
363 out:
364         /* Restore the bypass state */
365         map->async = false;
366         map->cache_bypass = bypass;
367         map->no_sync_defaults = false;
368         map->unlock(map->lock_arg);
369
370         regmap_async_complete(map);
371
372         trace_regcache_sync(map, name, "stop");
373
374         return ret;
375 }
376 EXPORT_SYMBOL_GPL(regcache_sync);
377
378 /**
379  * regcache_sync_region: Sync part  of the register cache with the hardware.
380  *
381  * @map: map to sync.
382  * @min: first register to sync
383  * @max: last register to sync
384  *
385  * Write all non-default register values in the specified region to
386  * the hardware.
387  *
388  * Return a negative value on failure, 0 on success.
389  */
390 int regcache_sync_region(struct regmap *map, unsigned int min,
391                          unsigned int max)
392 {
393         int ret = 0;
394         const char *name;
395         bool bypass;
396
397         BUG_ON(!map->cache_ops);
398
399         map->lock(map->lock_arg);
400
401         /* Remember the initial bypass state */
402         bypass = map->cache_bypass;
403
404         name = map->cache_ops->name;
405         dev_dbg(map->dev, "Syncing %s cache from %d-%d\n", name, min, max);
406
407         trace_regcache_sync(map, name, "start region");
408
409         if (!map->cache_dirty)
410                 goto out;
411
412         map->async = true;
413
414         if (map->cache_ops->sync)
415                 ret = map->cache_ops->sync(map, min, max);
416         else
417                 ret = regcache_default_sync(map, min, max);
418
419 out:
420         /* Restore the bypass state */
421         map->cache_bypass = bypass;
422         map->async = false;
423         map->no_sync_defaults = false;
424         map->unlock(map->lock_arg);
425
426         regmap_async_complete(map);
427
428         trace_regcache_sync(map, name, "stop region");
429
430         return ret;
431 }
432 EXPORT_SYMBOL_GPL(regcache_sync_region);
433
434 /**
435  * regcache_drop_region: Discard part of the register cache
436  *
437  * @map: map to operate on
438  * @min: first register to discard
439  * @max: last register to discard
440  *
441  * Discard part of the register cache.
442  *
443  * Return a negative value on failure, 0 on success.
444  */
445 int regcache_drop_region(struct regmap *map, unsigned int min,
446                          unsigned int max)
447 {
448         int ret = 0;
449
450         if (!map->cache_ops || !map->cache_ops->drop)
451                 return -EINVAL;
452
453         map->lock(map->lock_arg);
454
455         trace_regcache_drop_region(map, min, max);
456
457         ret = map->cache_ops->drop(map, min, max);
458
459         map->unlock(map->lock_arg);
460
461         return ret;
462 }
463 EXPORT_SYMBOL_GPL(regcache_drop_region);
464
465 /**
466  * regcache_cache_only: Put a register map into cache only mode
467  *
468  * @map: map to configure
469  * @cache_only: flag if changes should be written to the hardware
470  *
471  * When a register map is marked as cache only writes to the register
472  * map API will only update the register cache, they will not cause
473  * any hardware changes.  This is useful for allowing portions of
474  * drivers to act as though the device were functioning as normal when
475  * it is disabled for power saving reasons.
476  */
477 void regcache_cache_only(struct regmap *map, bool enable)
478 {
479         map->lock(map->lock_arg);
480         WARN_ON(map->cache_bypass && enable);
481         map->cache_only = enable;
482         trace_regmap_cache_only(map, enable);
483         map->unlock(map->lock_arg);
484 }
485 EXPORT_SYMBOL_GPL(regcache_cache_only);
486
487 /**
488  * regcache_mark_dirty: Indicate that HW registers were reset to default values
489  *
490  * @map: map to mark
491  *
492  * Inform regcache that the device has been powered down or reset, so that
493  * on resume, regcache_sync() knows to write out all non-default values
494  * stored in the cache.
495  *
496  * If this function is not called, regcache_sync() will assume that
497  * the hardware state still matches the cache state, modulo any writes that
498  * happened when cache_only was true.
499  */
500 void regcache_mark_dirty(struct regmap *map)
501 {
502         map->lock(map->lock_arg);
503         map->cache_dirty = true;
504         map->no_sync_defaults = true;
505         map->unlock(map->lock_arg);
506 }
507 EXPORT_SYMBOL_GPL(regcache_mark_dirty);
508
509 /**
510  * regcache_cache_bypass: Put a register map into cache bypass mode
511  *
512  * @map: map to configure
513  * @cache_bypass: flag if changes should not be written to the hardware
514  *
515  * When a register map is marked with the cache bypass option, writes
516  * to the register map API will only update the hardware and not the
517  * the cache directly.  This is useful when syncing the cache back to
518  * the hardware.
519  */
520 void regcache_cache_bypass(struct regmap *map, bool enable)
521 {
522         map->lock(map->lock_arg);
523         WARN_ON(map->cache_only && enable);
524         map->cache_bypass = enable;
525         trace_regmap_cache_bypass(map, enable);
526         map->unlock(map->lock_arg);
527 }
528 EXPORT_SYMBOL_GPL(regcache_cache_bypass);
529
530 bool regcache_set_val(struct regmap *map, void *base, unsigned int idx,
531                       unsigned int val)
532 {
533         if (regcache_get_val(map, base, idx) == val)
534                 return true;
535
536         /* Use device native format if possible */
537         if (map->format.format_val) {
538                 map->format.format_val(base + (map->cache_word_size * idx),
539                                        val, 0);
540                 return false;
541         }
542
543         switch (map->cache_word_size) {
544         case 1: {
545                 u8 *cache = base;
546                 cache[idx] = val;
547                 break;
548         }
549         case 2: {
550                 u16 *cache = base;
551                 cache[idx] = val;
552                 break;
553         }
554         case 4: {
555                 u32 *cache = base;
556                 cache[idx] = val;
557                 break;
558         }
559         default:
560                 BUG();
561         }
562         return false;
563 }
564
565 unsigned int regcache_get_val(struct regmap *map, const void *base,
566                               unsigned int idx)
567 {
568         if (!base)
569                 return -EINVAL;
570
571         /* Use device native format if possible */
572         if (map->format.parse_val)
573                 return map->format.parse_val(regcache_get_val_addr(map, base,
574                                                                    idx));
575
576         switch (map->cache_word_size) {
577         case 1: {
578                 const u8 *cache = base;
579                 return cache[idx];
580         }
581         case 2: {
582                 const u16 *cache = base;
583                 return cache[idx];
584         }
585         case 4: {
586                 const u32 *cache = base;
587                 return cache[idx];
588         }
589         default:
590                 BUG();
591         }
592         /* unreachable */
593         return -1;
594 }
595
596 static int regcache_default_cmp(const void *a, const void *b)
597 {
598         const struct reg_default *_a = a;
599         const struct reg_default *_b = b;
600
601         return _a->reg - _b->reg;
602 }
603
604 int regcache_lookup_reg(struct regmap *map, unsigned int reg)
605 {
606         struct reg_default key;
607         struct reg_default *r;
608
609         key.reg = reg;
610         key.def = 0;
611
612         r = bsearch(&key, map->reg_defaults, map->num_reg_defaults,
613                     sizeof(struct reg_default), regcache_default_cmp);
614
615         if (r)
616                 return r - map->reg_defaults;
617         else
618                 return -ENOENT;
619 }
620
621 static bool regcache_reg_present(unsigned long *cache_present, unsigned int idx)
622 {
623         if (!cache_present)
624                 return true;
625
626         return test_bit(idx, cache_present);
627 }
628
629 static int regcache_sync_block_single(struct regmap *map, void *block,
630                                       unsigned long *cache_present,
631                                       unsigned int block_base,
632                                       unsigned int start, unsigned int end)
633 {
634         unsigned int i, regtmp, val;
635         int ret;
636
637         for (i = start; i < end; i++) {
638                 regtmp = block_base + (i * map->reg_stride);
639
640                 if (!regcache_reg_present(cache_present, i) ||
641                     !regmap_writeable(map, regtmp))
642                         continue;
643
644                 val = regcache_get_val(map, block, i);
645                 if (!regcache_reg_needs_sync(map, regtmp, val))
646                         continue;
647
648                 map->cache_bypass = true;
649
650                 ret = _regmap_write(map, regtmp, val);
651
652                 map->cache_bypass = false;
653                 if (ret != 0) {
654                         dev_err(map->dev, "Unable to sync register %#x. %d\n",
655                                 regtmp, ret);
656                         return ret;
657                 }
658                 dev_dbg(map->dev, "Synced register %#x, value %#x\n",
659                         regtmp, val);
660         }
661
662         return 0;
663 }
664
665 static int regcache_sync_block_raw_flush(struct regmap *map, const void **data,
666                                          unsigned int base, unsigned int cur)
667 {
668         size_t val_bytes = map->format.val_bytes;
669         int ret, count;
670
671         if (*data == NULL)
672                 return 0;
673
674         count = (cur - base) / map->reg_stride;
675
676         dev_dbg(map->dev, "Writing %zu bytes for %d registers from 0x%x-0x%x\n",
677                 count * val_bytes, count, base, cur - map->reg_stride);
678
679         map->cache_bypass = true;
680
681         ret = _regmap_raw_write(map, base, *data, count * val_bytes);
682         if (ret)
683                 dev_err(map->dev, "Unable to sync registers %#x-%#x. %d\n",
684                         base, cur - map->reg_stride, ret);
685
686         map->cache_bypass = false;
687
688         *data = NULL;
689
690         return ret;
691 }
692
693 static int regcache_sync_block_raw(struct regmap *map, void *block,
694                             unsigned long *cache_present,
695                             unsigned int block_base, unsigned int start,
696                             unsigned int end)
697 {
698         unsigned int i, val;
699         unsigned int regtmp = 0;
700         unsigned int base = 0;
701         const void *data = NULL;
702         int ret;
703
704         for (i = start; i < end; i++) {
705                 regtmp = block_base + (i * map->reg_stride);
706
707                 if (!regcache_reg_present(cache_present, i) ||
708                     !regmap_writeable(map, regtmp)) {
709                         ret = regcache_sync_block_raw_flush(map, &data,
710                                                             base, regtmp);
711                         if (ret != 0)
712                                 return ret;
713                         continue;
714                 }
715
716                 val = regcache_get_val(map, block, i);
717                 if (!regcache_reg_needs_sync(map, regtmp, val)) {
718                         ret = regcache_sync_block_raw_flush(map, &data,
719                                                             base, regtmp);
720                         if (ret != 0)
721                                 return ret;
722                         continue;
723                 }
724
725                 if (!data) {
726                         data = regcache_get_val_addr(map, block, i);
727                         base = regtmp;
728                 }
729         }
730
731         return regcache_sync_block_raw_flush(map, &data, base, regtmp +
732                         map->reg_stride);
733 }
734
735 int regcache_sync_block(struct regmap *map, void *block,
736                         unsigned long *cache_present,
737                         unsigned int block_base, unsigned int start,
738                         unsigned int end)
739 {
740         if (regmap_can_raw_write(map) && !map->use_single_write)
741                 return regcache_sync_block_raw(map, block, cache_present,
742                                                block_base, start, end);
743         else
744                 return regcache_sync_block_single(map, block, cache_present,
745                                                   block_base, start, end);
746 }
This page took 0.086019 seconds and 4 git commands to generate.