2 * Register cache access API
4 * Copyright 2011 Wolfson Microelectronics plc
6 * Author: Dimitris Papastamos <dp@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/slab.h>
14 #include <trace/events/regmap.h>
15 #include <linux/bsearch.h>
16 #include <linux/sort.h>
20 static const struct regcache_ops
*cache_types
[] = {
21 ®cache_indexed_ops
,
26 static int regcache_hw_init(struct regmap
*map
)
34 if (!map
->num_reg_defaults_raw
)
37 if (!map
->reg_defaults_raw
) {
38 dev_warn(map
->dev
, "No cache defaults, reading back from HW\n");
39 tmp_buf
= kmalloc(map
->cache_size_raw
, GFP_KERNEL
);
42 ret
= regmap_bulk_read(map
, 0, tmp_buf
,
43 map
->num_reg_defaults_raw
);
48 map
->reg_defaults_raw
= tmp_buf
;
52 /* calculate the size of reg_defaults */
53 for (count
= 0, i
= 0; i
< map
->num_reg_defaults_raw
; i
++) {
54 val
= regcache_get_val(map
->reg_defaults_raw
,
55 i
, map
->cache_word_size
);
61 map
->reg_defaults
= kmalloc(count
* sizeof(struct reg_default
),
63 if (!map
->reg_defaults
)
66 /* fill the reg_defaults */
67 map
->num_reg_defaults
= count
;
68 for (i
= 0, j
= 0; i
< map
->num_reg_defaults_raw
; i
++) {
69 val
= regcache_get_val(map
->reg_defaults_raw
,
70 i
, map
->cache_word_size
);
73 map
->reg_defaults
[j
].reg
= i
;
74 map
->reg_defaults
[j
].def
= val
;
81 int regcache_init(struct regmap
*map
)
87 if (map
->cache_type
== REGCACHE_NONE
) {
88 map
->cache_bypass
= true;
92 for (i
= 0; i
< ARRAY_SIZE(cache_types
); i
++)
93 if (cache_types
[i
]->type
== map
->cache_type
)
96 if (i
== ARRAY_SIZE(cache_types
)) {
97 dev_err(map
->dev
, "Could not match compress type: %d\n",
103 map
->cache_ops
= cache_types
[i
];
105 if (!map
->cache_ops
->read
||
106 !map
->cache_ops
->write
||
107 !map
->cache_ops
->name
)
110 /* We still need to ensure that the reg_defaults
111 * won't vanish from under us. We'll need to make
114 if (map
->reg_defaults
) {
115 if (!map
->num_reg_defaults
)
117 tmp_buf
= kmemdup(map
->reg_defaults
, map
->num_reg_defaults
*
118 sizeof(struct reg_default
), GFP_KERNEL
);
121 map
->reg_defaults
= tmp_buf
;
122 } else if (map
->num_reg_defaults_raw
) {
123 /* Some devices such as PMICs don't have cache defaults,
124 * we cope with this by reading back the HW registers and
125 * crafting the cache defaults by hand.
127 ret
= regcache_hw_init(map
);
132 if (!map
->max_register
)
133 map
->max_register
= map
->num_reg_defaults_raw
;
135 if (map
->cache_ops
->init
) {
136 dev_dbg(map
->dev
, "Initializing %s cache\n",
137 map
->cache_ops
->name
);
138 return map
->cache_ops
->init(map
);
143 void regcache_exit(struct regmap
*map
)
145 if (map
->cache_type
== REGCACHE_NONE
)
148 BUG_ON(!map
->cache_ops
);
150 kfree(map
->reg_defaults
);
152 kfree(map
->reg_defaults_raw
);
154 if (map
->cache_ops
->exit
) {
155 dev_dbg(map
->dev
, "Destroying %s cache\n",
156 map
->cache_ops
->name
);
157 map
->cache_ops
->exit(map
);
162 * regcache_read: Fetch the value of a given register from the cache.
164 * @map: map to configure.
165 * @reg: The register index.
166 * @value: The value to be returned.
168 * Return a negative value on failure, 0 on success.
170 int regcache_read(struct regmap
*map
,
171 unsigned int reg
, unsigned int *value
)
173 if (map
->cache_type
== REGCACHE_NONE
)
176 BUG_ON(!map
->cache_ops
);
178 if (!regmap_readable(map
, reg
))
181 if (!regmap_volatile(map
, reg
))
182 return map
->cache_ops
->read(map
, reg
, value
);
186 EXPORT_SYMBOL_GPL(regcache_read
);
189 * regcache_write: Set the value of a given register in the cache.
191 * @map: map to configure.
192 * @reg: The register index.
193 * @value: The new register value.
195 * Return a negative value on failure, 0 on success.
197 int regcache_write(struct regmap
*map
,
198 unsigned int reg
, unsigned int value
)
200 if (map
->cache_type
== REGCACHE_NONE
)
203 BUG_ON(!map
->cache_ops
);
205 if (!regmap_writeable(map
, reg
))
208 if (!regmap_volatile(map
, reg
))
209 return map
->cache_ops
->write(map
, reg
, value
);
213 EXPORT_SYMBOL_GPL(regcache_write
);
216 * regcache_sync: Sync the register cache with the hardware.
218 * @map: map to configure.
220 * Any registers that should not be synced should be marked as
221 * volatile. In general drivers can choose not to use the provided
222 * syncing functionality if they so require.
224 * Return a negative value on failure, 0 on success.
226 int regcache_sync(struct regmap
*map
)
234 BUG_ON(!map
->cache_ops
);
236 mutex_lock(&map
->lock
);
237 /* Remember the initial bypass state */
238 bypass
= map
->cache_bypass
;
239 dev_dbg(map
->dev
, "Syncing %s cache\n",
240 map
->cache_ops
->name
);
241 name
= map
->cache_ops
->name
;
242 trace_regcache_sync(map
->dev
, name
, "start");
243 if (map
->cache_ops
->sync
) {
244 ret
= map
->cache_ops
->sync(map
);
246 for (i
= 0; i
< map
->num_reg_defaults
; i
++) {
247 ret
= regcache_read(map
, i
, &val
);
250 map
->cache_bypass
= 1;
251 ret
= _regmap_write(map
, i
, val
);
252 map
->cache_bypass
= 0;
255 dev_dbg(map
->dev
, "Synced register %#x, value %#x\n",
256 map
->reg_defaults
[i
].reg
,
257 map
->reg_defaults
[i
].def
);
262 trace_regcache_sync(map
->dev
, name
, "stop");
263 /* Restore the bypass state */
264 map
->cache_bypass
= bypass
;
265 mutex_unlock(&map
->lock
);
269 EXPORT_SYMBOL_GPL(regcache_sync
);
272 * regcache_cache_only: Put a register map into cache only mode
274 * @map: map to configure
275 * @cache_only: flag if changes should be written to the hardware
277 * When a register map is marked as cache only writes to the register
278 * map API will only update the register cache, they will not cause
279 * any hardware changes. This is useful for allowing portions of
280 * drivers to act as though the device were functioning as normal when
281 * it is disabled for power saving reasons.
283 void regcache_cache_only(struct regmap
*map
, bool enable
)
285 mutex_lock(&map
->lock
);
286 WARN_ON(map
->cache_bypass
&& enable
);
287 map
->cache_only
= enable
;
288 mutex_unlock(&map
->lock
);
290 EXPORT_SYMBOL_GPL(regcache_cache_only
);
293 * regcache_cache_bypass: Put a register map into cache bypass mode
295 * @map: map to configure
296 * @cache_bypass: flag if changes should not be written to the hardware
298 * When a register map is marked with the cache bypass option, writes
299 * to the register map API will only update the hardware and not the
300 * the cache directly. This is useful when syncing the cache back to
303 void regcache_cache_bypass(struct regmap
*map
, bool enable
)
305 mutex_lock(&map
->lock
);
306 WARN_ON(map
->cache_only
&& enable
);
307 map
->cache_bypass
= enable
;
308 mutex_unlock(&map
->lock
);
310 EXPORT_SYMBOL_GPL(regcache_cache_bypass
);
312 bool regcache_set_val(void *base
, unsigned int idx
,
313 unsigned int val
, unsigned int word_size
)
318 if (cache
[idx
] == val
)
325 if (cache
[idx
] == val
)
337 unsigned int regcache_get_val(const void *base
, unsigned int idx
,
338 unsigned int word_size
)
345 const u8
*cache
= base
;
349 const u16
*cache
= base
;
359 static int regcache_default_cmp(const void *a
, const void *b
)
361 const struct reg_default
*_a
= a
;
362 const struct reg_default
*_b
= b
;
364 return _a
->reg
- _b
->reg
;
367 int regcache_lookup_reg(struct regmap
*map
, unsigned int reg
)
369 struct reg_default key
;
370 struct reg_default
*r
;
375 r
= bsearch(&key
, map
->reg_defaults
, map
->num_reg_defaults
,
376 sizeof(struct reg_default
), regcache_default_cmp
);
379 return r
- map
->reg_defaults
;
384 int regcache_insert_reg(struct regmap
*map
, unsigned int reg
,
389 tmp
= krealloc(map
->reg_defaults
,
390 (map
->num_reg_defaults
+ 1) * sizeof(struct reg_default
),
394 map
->reg_defaults
= tmp
;
395 map
->num_reg_defaults
++;
396 map
->reg_defaults
[map
->num_reg_defaults
- 1].reg
= reg
;
397 map
->reg_defaults
[map
->num_reg_defaults
- 1].def
= val
;
398 sort(map
->reg_defaults
, map
->num_reg_defaults
,
399 sizeof(struct reg_default
), regcache_default_cmp
, NULL
);