Linux 4.19.133
[linux/fpc-iii.git] / drivers / base / regmap / regcache-flat.c
blobbc6cd88b8cc6b2f7d894725a5f5c5f9d1ac12af5
1 /*
2 * Register cache access API - flat caching support
4 * Copyright 2012 Wolfson Microelectronics plc
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
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.
13 #include <linux/device.h>
14 #include <linux/seq_file.h>
15 #include <linux/slab.h>
17 #include "internal.h"
19 static inline unsigned int regcache_flat_get_index(const struct regmap *map,
20 unsigned int reg)
22 return regcache_get_index_by_order(map, reg);
25 static int regcache_flat_init(struct regmap *map)
27 int i;
28 unsigned int *cache;
30 if (!map || map->reg_stride_order < 0 || !map->max_register)
31 return -EINVAL;
33 map->cache = kcalloc(regcache_flat_get_index(map, map->max_register)
34 + 1, sizeof(unsigned int), GFP_KERNEL);
35 if (!map->cache)
36 return -ENOMEM;
38 cache = map->cache;
40 for (i = 0; i < map->num_reg_defaults; i++) {
41 unsigned int reg = map->reg_defaults[i].reg;
42 unsigned int index = regcache_flat_get_index(map, reg);
44 cache[index] = map->reg_defaults[i].def;
47 return 0;
50 static int regcache_flat_exit(struct regmap *map)
52 kfree(map->cache);
53 map->cache = NULL;
55 return 0;
58 static int regcache_flat_read(struct regmap *map,
59 unsigned int reg, unsigned int *value)
61 unsigned int *cache = map->cache;
62 unsigned int index = regcache_flat_get_index(map, reg);
64 *value = cache[index];
66 return 0;
69 static int regcache_flat_write(struct regmap *map, unsigned int reg,
70 unsigned int value)
72 unsigned int *cache = map->cache;
73 unsigned int index = regcache_flat_get_index(map, reg);
75 cache[index] = value;
77 return 0;
80 struct regcache_ops regcache_flat_ops = {
81 .type = REGCACHE_FLAT,
82 .name = "flat",
83 .init = regcache_flat_init,
84 .exit = regcache_flat_exit,
85 .read = regcache_flat_read,
86 .write = regcache_flat_write,