| 1 | // SPDX-License-Identifier: GPL-2.0
|
|---|
| 2 | //
|
|---|
| 3 | // Register cache access API - flat caching support
|
|---|
| 4 | //
|
|---|
| 5 | // Copyright 2012 Wolfson Microelectronics plc
|
|---|
| 6 | //
|
|---|
| 7 | // Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
|
|---|
| 8 |
|
|---|
| 9 | /* from 5.10.10 */
|
|---|
| 10 |
|
|---|
| 11 | #include <linux/device.h>
|
|---|
| 12 | #include <linux/seq_file.h>
|
|---|
| 13 | #include <linux/slab.h>
|
|---|
| 14 |
|
|---|
| 15 | #include "internal.h"
|
|---|
| 16 |
|
|---|
| 17 | static inline unsigned int regcache_flat_get_index(const struct regmap *map,
|
|---|
| 18 | unsigned int reg)
|
|---|
| 19 | {
|
|---|
| 20 | return regcache_get_index_by_order(map, reg);
|
|---|
| 21 | }
|
|---|
| 22 |
|
|---|
| 23 | static int regcache_flat_init(struct regmap *map)
|
|---|
| 24 | {
|
|---|
| 25 | int i;
|
|---|
| 26 | unsigned int *cache;
|
|---|
| 27 |
|
|---|
| 28 | if (!map || map->reg_stride_order < 0 || !map->max_register)
|
|---|
| 29 | return -EINVAL;
|
|---|
| 30 |
|
|---|
| 31 | map->cache = kcalloc(regcache_flat_get_index(map, map->max_register)
|
|---|
| 32 | + 1, sizeof(unsigned int), GFP_KERNEL);
|
|---|
| 33 | if (!map->cache)
|
|---|
| 34 | return -ENOMEM;
|
|---|
| 35 |
|
|---|
| 36 | cache = map->cache;
|
|---|
| 37 |
|
|---|
| 38 | for (i = 0; i < map->num_reg_defaults; i++) {
|
|---|
| 39 | unsigned int reg = map->reg_defaults[i].reg;
|
|---|
| 40 | unsigned int index = regcache_flat_get_index(map, reg);
|
|---|
| 41 |
|
|---|
| 42 | cache[index] = map->reg_defaults[i].def;
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | return 0;
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | static int regcache_flat_exit(struct regmap *map)
|
|---|
| 49 | {
|
|---|
| 50 | kfree(map->cache);
|
|---|
| 51 | map->cache = NULL;
|
|---|
| 52 |
|
|---|
| 53 | return 0;
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | static int regcache_flat_read(struct regmap *map,
|
|---|
| 57 | unsigned int reg, unsigned int *value)
|
|---|
| 58 | {
|
|---|
| 59 | unsigned int *cache = map->cache;
|
|---|
| 60 | unsigned int index = regcache_flat_get_index(map, reg);
|
|---|
| 61 |
|
|---|
| 62 | *value = cache[index];
|
|---|
| 63 |
|
|---|
| 64 | return 0;
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | static int regcache_flat_write(struct regmap *map, unsigned int reg,
|
|---|
| 68 | unsigned int value)
|
|---|
| 69 | {
|
|---|
| 70 | unsigned int *cache = map->cache;
|
|---|
| 71 | unsigned int index = regcache_flat_get_index(map, reg);
|
|---|
| 72 |
|
|---|
| 73 | cache[index] = value;
|
|---|
| 74 |
|
|---|
| 75 | return 0;
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | struct regcache_ops regcache_flat_ops = {
|
|---|
| 79 | .type = REGCACHE_FLAT,
|
|---|
| 80 | .name = "flat",
|
|---|
| 81 | .init = regcache_flat_init,
|
|---|
| 82 | .exit = regcache_flat_exit,
|
|---|
| 83 | .read = regcache_flat_read,
|
|---|
| 84 | .write = regcache_flat_write,
|
|---|
| 85 | };
|
|---|