blob: 142d9cdfef3a6646ffde0b16b5f32f9494c2e07a [file] [log] [blame]
Dimitris Papastamos9fabe242011-09-19 14:34:00 +01001/*
2 * Register cache access API
3 *
4 * Copyright 2011 Wolfson Microelectronics plc
5 *
6 * Author: Dimitris Papastamos <dp@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 <trace/events/regmap.h>
15
16#include "internal.h"
17
18static const struct regcache_ops *cache_types[] = {
Dimitris Papastamos195af652011-09-19 14:34:01 +010019 &regcache_indexed_ops,
Dimitris Papastamos28644c82011-09-19 14:34:02 +010020 &regcache_rbtree_ops,
Dimitris Papastamos2cbbb572011-09-19 14:34:03 +010021 &regcache_lzo_ops,
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010022};
23
24static int regcache_hw_init(struct regmap *map)
25{
26 int i, j;
27 int ret;
28 int count;
29 unsigned int val;
30 void *tmp_buf;
31
32 if (!map->num_reg_defaults_raw)
33 return -EINVAL;
34
35 if (!map->reg_defaults_raw) {
36 dev_warn(map->dev, "No cache defaults, reading back from HW\n");
37 tmp_buf = kmalloc(map->cache_size_raw, GFP_KERNEL);
38 if (!tmp_buf)
39 return -EINVAL;
40 ret = regmap_bulk_read(map, 0, tmp_buf,
41 map->num_reg_defaults_raw);
42 if (ret < 0) {
43 kfree(tmp_buf);
44 return ret;
45 }
46 map->reg_defaults_raw = tmp_buf;
47 map->cache_free = 1;
48 }
49
50 /* calculate the size of reg_defaults */
51 for (count = 0, i = 0; i < map->num_reg_defaults_raw; i++) {
52 val = regcache_get_val(map->reg_defaults_raw,
53 i, map->cache_word_size);
54 if (!val)
55 continue;
56 count++;
57 }
58
59 map->reg_defaults = kmalloc(count * sizeof(struct reg_default),
60 GFP_KERNEL);
61 if (!map->reg_defaults)
62 return -ENOMEM;
63
64 /* fill the reg_defaults */
65 map->num_reg_defaults = count;
66 for (i = 0, j = 0; i < map->num_reg_defaults_raw; i++) {
67 val = regcache_get_val(map->reg_defaults_raw,
68 i, map->cache_word_size);
69 if (!val)
70 continue;
71 map->reg_defaults[j].reg = i;
72 map->reg_defaults[j].def = val;
73 j++;
74 }
75
76 return 0;
77}
78
79int regcache_init(struct regmap *map)
80{
81 int ret;
82 int i;
83 void *tmp_buf;
84
85 if (map->cache_type == REGCACHE_NONE)
86 return 0;
87
88 for (i = 0; i < ARRAY_SIZE(cache_types); i++)
89 if (cache_types[i]->type == map->cache_type)
90 break;
91
92 if (i == ARRAY_SIZE(cache_types)) {
93 dev_err(map->dev, "Could not match compress type: %d\n",
94 map->cache_type);
95 return -EINVAL;
96 }
97
98 map->cache = NULL;
99 map->cache_ops = cache_types[i];
100
101 if (!map->cache_ops->read ||
102 !map->cache_ops->write ||
103 !map->cache_ops->name)
104 return -EINVAL;
105
106 /* We still need to ensure that the reg_defaults
107 * won't vanish from under us. We'll need to make
108 * a copy of it.
109 */
110 if (map->reg_defaults) {
111 if (!map->num_reg_defaults)
112 return -EINVAL;
113 tmp_buf = kmemdup(map->reg_defaults, map->num_reg_defaults *
114 sizeof(struct reg_default), GFP_KERNEL);
115 if (!tmp_buf)
116 return -ENOMEM;
117 map->reg_defaults = tmp_buf;
118 } else {
119 /* Some devices such as PMIC's don't have cache defaults,
120 * we cope with this by reading back the HW registers and
121 * crafting the cache defaults by hand.
122 */
123 ret = regcache_hw_init(map);
124 if (ret < 0)
125 return ret;
126 }
127
128 if (!map->max_register)
129 map->max_register = map->num_reg_defaults_raw;
130
131 if (map->cache_ops->init) {
132 dev_dbg(map->dev, "Initializing %s cache\n",
133 map->cache_ops->name);
134 return map->cache_ops->init(map);
135 }
136 return 0;
137}
138
139void regcache_exit(struct regmap *map)
140{
141 if (map->cache_type == REGCACHE_NONE)
142 return;
143
144 BUG_ON(!map->cache_ops);
145
146 kfree(map->reg_defaults);
147 if (map->cache_free)
148 kfree(map->reg_defaults_raw);
149
150 if (map->cache_ops->exit) {
151 dev_dbg(map->dev, "Destroying %s cache\n",
152 map->cache_ops->name);
153 map->cache_ops->exit(map);
154 }
155}
156
157/**
158 * regcache_read: Fetch the value of a given register from the cache.
159 *
160 * @map: map to configure.
161 * @reg: The register index.
162 * @value: The value to be returned.
163 *
164 * Return a negative value on failure, 0 on success.
165 */
166int regcache_read(struct regmap *map,
167 unsigned int reg, unsigned int *value)
168{
169 if (map->cache_type == REGCACHE_NONE)
170 return -ENOSYS;
171
172 BUG_ON(!map->cache_ops);
173
174 if (!regmap_readable(map, reg))
175 return -EIO;
176
177 if (!regmap_volatile(map, reg))
178 return map->cache_ops->read(map, reg, value);
179
180 return -EINVAL;
181}
182EXPORT_SYMBOL_GPL(regcache_read);
183
184/**
185 * regcache_write: Set the value of a given register in the cache.
186 *
187 * @map: map to configure.
188 * @reg: The register index.
189 * @value: The new register value.
190 *
191 * Return a negative value on failure, 0 on success.
192 */
193int regcache_write(struct regmap *map,
194 unsigned int reg, unsigned int value)
195{
196 if (map->cache_type == REGCACHE_NONE)
197 return 0;
198
199 BUG_ON(!map->cache_ops);
200
201 if (!regmap_writeable(map, reg))
202 return -EIO;
203
204 if (!regmap_volatile(map, reg))
205 return map->cache_ops->write(map, reg, value);
206
207 return 0;
208}
209EXPORT_SYMBOL_GPL(regcache_write);
210
211/**
212 * regcache_sync: Sync the register cache with the hardware.
213 *
214 * @map: map to configure.
215 *
216 * Any registers that should not be synced should be marked as
217 * volatile. In general drivers can choose not to use the provided
218 * syncing functionality if they so require.
219 *
220 * Return a negative value on failure, 0 on success.
221 */
222int regcache_sync(struct regmap *map)
223{
224 BUG_ON(!map->cache_ops);
225
226 if (map->cache_ops->sync) {
227 dev_dbg(map->dev, "Syncing %s cache\n",
228 map->cache_ops->name);
229 return map->cache_ops->sync(map);
230 }
231 return 0;
232}
233EXPORT_SYMBOL_GPL(regcache_sync);
234
235bool regcache_set_val(void *base, unsigned int idx,
236 unsigned int val, unsigned int word_size)
237{
238 switch (word_size) {
239 case 1: {
240 u8 *cache = base;
241 if (cache[idx] == val)
242 return true;
243 cache[idx] = val;
244 break;
245 }
246 case 2: {
247 u16 *cache = base;
248 if (cache[idx] == val)
249 return true;
250 cache[idx] = val;
251 break;
252 }
253 default:
254 BUG();
255 }
256 /* unreachable */
257 return false;
258}
259
260unsigned int regcache_get_val(const void *base, unsigned int idx,
261 unsigned int word_size)
262{
263 if (!base)
264 return -EINVAL;
265
266 switch (word_size) {
267 case 1: {
268 const u8 *cache = base;
269 return cache[idx];
270 }
271 case 2: {
272 const u16 *cache = base;
273 return cache[idx];
274 }
275 default:
276 BUG();
277 }
278 /* unreachable */
279 return -1;
280}
281
282int regcache_lookup_reg(struct regmap *map, unsigned int reg)
283{
284 unsigned int i;
285
286 for (i = 0; i < map->num_reg_defaults; i++)
287 if (map->reg_defaults[i].reg == reg)
288 return i;
289 return -1;
290}
291
292int regcache_insert_reg(struct regmap *map, unsigned int reg,
293 unsigned int val)
294{
295 void *tmp;
296
297 tmp = krealloc(map->reg_defaults,
298 (map->num_reg_defaults + 1) * sizeof(struct reg_default),
299 GFP_KERNEL);
300 if (!tmp)
301 return -ENOMEM;
302 map->reg_defaults = tmp;
303 map->num_reg_defaults++;
304 map->reg_defaults[map->num_reg_defaults - 1].reg = reg;
305 map->reg_defaults[map->num_reg_defaults - 1].def = val;
306 return 0;
307}