1 /*
2  * Register cache access API - flat caching support
3  *
4  * Copyright 2012 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/seq_file.h>
15 #include <linux/slab.h>
16 
17 #include "internal.h"
18 
19 static int regcache_flat_init(struct regmap *map)
20 {
21 	int i;
22 	unsigned int *cache;
23 
24 	map->cache = kcalloc(map->max_register + 1, sizeof(unsigned int),
25 			     GFP_KERNEL);
26 	if (!map->cache)
27 		return -ENOMEM;
28 
29 	cache = map->cache;
30 
31 	for (i = 0; i < map->num_reg_defaults; i++)
32 		cache[map->reg_defaults[i].reg] = map->reg_defaults[i].def;
33 
34 	return 0;
35 }
36 
37 static int regcache_flat_exit(struct regmap *map)
38 {
39 	kfree(map->cache);
40 	map->cache = NULL;
41 
42 	return 0;
43 }
44 
45 static int regcache_flat_read(struct regmap *map,
46 			      unsigned int reg, unsigned int *value)
47 {
48 	unsigned int *cache = map->cache;
49 
50 	*value = cache[reg];
51 
52 	return 0;
53 }
54 
55 static int regcache_flat_write(struct regmap *map, unsigned int reg,
56 			       unsigned int value)
57 {
58 	unsigned int *cache = map->cache;
59 
60 	cache[reg] = value;
61 
62 	return 0;
63 }
64 
65 struct regcache_ops regcache_flat_ops = {
66 	.type = REGCACHE_FLAT,
67 	.name = "flat",
68 	.init = regcache_flat_init,
69 	.exit = regcache_flat_exit,
70 	.read = regcache_flat_read,
71 	.write = regcache_flat_write,
72 };
73