2 * Register cache access API - rbtree caching support
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 <linux/rbtree.h>
18 static int regcache_rbtree_write(struct regmap
*map
, unsigned int reg
,
21 struct regcache_rbtree_node
{
22 /* the actual rbtree node holding this block */
24 /* base register handled by this block */
25 unsigned int base_reg
;
26 /* block of adjacent registers */
28 /* number of registers available in the block */
30 } __attribute__ ((packed
));
32 struct regcache_rbtree_ctx
{
34 struct regcache_rbtree_node
*cached_rbnode
;
37 static inline void regcache_rbtree_get_base_top_reg(
38 struct regcache_rbtree_node
*rbnode
,
39 unsigned int *base
, unsigned int *top
)
41 *base
= rbnode
->base_reg
;
42 *top
= rbnode
->base_reg
+ rbnode
->blklen
- 1;
45 static unsigned int regcache_rbtree_get_register(
46 struct regcache_rbtree_node
*rbnode
, unsigned int idx
,
47 unsigned int word_size
)
49 return regcache_get_val(rbnode
->block
, idx
, word_size
);
52 static void regcache_rbtree_set_register(struct regcache_rbtree_node
*rbnode
,
53 unsigned int idx
, unsigned int val
,
54 unsigned int word_size
)
56 regcache_set_val(rbnode
->block
, idx
, val
, word_size
);
59 static struct regcache_rbtree_node
*regcache_rbtree_lookup(struct regmap
*map
,
62 struct regcache_rbtree_ctx
*rbtree_ctx
= map
->cache
;
64 struct regcache_rbtree_node
*rbnode
;
65 unsigned int base_reg
, top_reg
;
67 rbnode
= rbtree_ctx
->cached_rbnode
;
69 regcache_rbtree_get_base_top_reg(rbnode
, &base_reg
, &top_reg
);
70 if (reg
>= base_reg
&& reg
<= top_reg
)
74 node
= rbtree_ctx
->root
.rb_node
;
76 rbnode
= container_of(node
, struct regcache_rbtree_node
, node
);
77 regcache_rbtree_get_base_top_reg(rbnode
, &base_reg
, &top_reg
);
78 if (reg
>= base_reg
&& reg
<= top_reg
) {
79 rbtree_ctx
->cached_rbnode
= rbnode
;
81 } else if (reg
> top_reg
) {
82 node
= node
->rb_right
;
83 } else if (reg
< base_reg
) {
91 static int regcache_rbtree_insert(struct rb_root
*root
,
92 struct regcache_rbtree_node
*rbnode
)
94 struct rb_node
**new, *parent
;
95 struct regcache_rbtree_node
*rbnode_tmp
;
96 unsigned int base_reg_tmp
, top_reg_tmp
;
97 unsigned int base_reg
;
100 new = &root
->rb_node
;
102 rbnode_tmp
= container_of(*new, struct regcache_rbtree_node
,
104 /* base and top registers of the current rbnode */
105 regcache_rbtree_get_base_top_reg(rbnode_tmp
, &base_reg_tmp
,
107 /* base register of the rbnode to be added */
108 base_reg
= rbnode
->base_reg
;
110 /* if this register has already been inserted, just return */
111 if (base_reg
>= base_reg_tmp
&&
112 base_reg
<= top_reg_tmp
)
114 else if (base_reg
> top_reg_tmp
)
115 new = &((*new)->rb_right
);
116 else if (base_reg
< base_reg_tmp
)
117 new = &((*new)->rb_left
);
120 /* insert the node into the rbtree */
121 rb_link_node(&rbnode
->node
, parent
, new);
122 rb_insert_color(&rbnode
->node
, root
);
127 static int regcache_rbtree_init(struct regmap
*map
)
129 struct regcache_rbtree_ctx
*rbtree_ctx
;
133 map
->cache
= kmalloc(sizeof *rbtree_ctx
, GFP_KERNEL
);
137 rbtree_ctx
= map
->cache
;
138 rbtree_ctx
->root
= RB_ROOT
;
139 rbtree_ctx
->cached_rbnode
= NULL
;
141 for (i
= 0; i
< map
->num_reg_defaults
; i
++) {
142 ret
= regcache_rbtree_write(map
,
143 map
->reg_defaults
[i
].reg
,
144 map
->reg_defaults
[i
].def
);
156 static int regcache_rbtree_exit(struct regmap
*map
)
158 struct rb_node
*next
;
159 struct regcache_rbtree_ctx
*rbtree_ctx
;
160 struct regcache_rbtree_node
*rbtree_node
;
162 /* if we've already been called then just return */
163 rbtree_ctx
= map
->cache
;
167 /* free up the rbtree */
168 next
= rb_first(&rbtree_ctx
->root
);
170 rbtree_node
= rb_entry(next
, struct regcache_rbtree_node
, node
);
171 next
= rb_next(&rbtree_node
->node
);
172 rb_erase(&rbtree_node
->node
, &rbtree_ctx
->root
);
173 kfree(rbtree_node
->block
);
177 /* release the resources */
184 static int regcache_rbtree_read(struct regmap
*map
,
185 unsigned int reg
, unsigned int *value
)
187 struct regcache_rbtree_node
*rbnode
;
188 unsigned int reg_tmp
;
190 rbnode
= regcache_rbtree_lookup(map
, reg
);
192 reg_tmp
= reg
- rbnode
->base_reg
;
193 *value
= regcache_rbtree_get_register(rbnode
, reg_tmp
,
194 map
->cache_word_size
);
203 static int regcache_rbtree_insert_to_block(struct regcache_rbtree_node
*rbnode
,
204 unsigned int pos
, unsigned int reg
,
205 unsigned int value
, unsigned int word_size
)
209 blk
= krealloc(rbnode
->block
,
210 (rbnode
->blklen
+ 1) * word_size
, GFP_KERNEL
);
214 /* insert the register value in the correct place in the rbnode block */
215 memmove(blk
+ (pos
+ 1) * word_size
,
216 blk
+ pos
* word_size
,
217 (rbnode
->blklen
- pos
) * word_size
);
219 /* update the rbnode block, its size and the base register */
223 rbnode
->base_reg
= reg
;
225 regcache_rbtree_set_register(rbnode
, pos
, value
, word_size
);
229 static int regcache_rbtree_write(struct regmap
*map
, unsigned int reg
,
232 struct regcache_rbtree_ctx
*rbtree_ctx
;
233 struct regcache_rbtree_node
*rbnode
, *rbnode_tmp
;
234 struct rb_node
*node
;
236 unsigned int reg_tmp
;
241 rbtree_ctx
= map
->cache
;
242 /* if we can't locate it in the cached rbnode we'll have
243 * to traverse the rbtree looking for it.
245 rbnode
= regcache_rbtree_lookup(map
, reg
);
247 reg_tmp
= reg
- rbnode
->base_reg
;
248 val
= regcache_rbtree_get_register(rbnode
, reg_tmp
,
249 map
->cache_word_size
);
252 regcache_rbtree_set_register(rbnode
, reg_tmp
, value
,
253 map
->cache_word_size
);
255 /* look for an adjacent register to the one we are about to add */
256 for (node
= rb_first(&rbtree_ctx
->root
); node
;
257 node
= rb_next(node
)) {
258 rbnode_tmp
= rb_entry(node
, struct regcache_rbtree_node
, node
);
259 for (i
= 0; i
< rbnode_tmp
->blklen
; i
++) {
260 reg_tmp
= rbnode_tmp
->base_reg
+ i
;
261 if (abs(reg_tmp
- reg
) != 1)
263 /* decide where in the block to place our register */
264 if (reg_tmp
+ 1 == reg
)
268 ret
= regcache_rbtree_insert_to_block(rbnode_tmp
, pos
,
270 map
->cache_word_size
);
273 rbtree_ctx
->cached_rbnode
= rbnode_tmp
;
277 /* we did not manage to find a place to insert it in an existing
278 * block so create a new rbnode with a single register in its block.
279 * This block will get populated further if any other adjacent
280 * registers get modified in the future.
282 rbnode
= kzalloc(sizeof *rbnode
, GFP_KERNEL
);
286 rbnode
->base_reg
= reg
;
287 rbnode
->block
= kmalloc(rbnode
->blklen
* map
->cache_word_size
,
289 if (!rbnode
->block
) {
293 regcache_rbtree_set_register(rbnode
, 0, value
, map
->cache_word_size
);
294 regcache_rbtree_insert(&rbtree_ctx
->root
, rbnode
);
295 rbtree_ctx
->cached_rbnode
= rbnode
;
301 static int regcache_rbtree_sync(struct regmap
*map
)
303 struct regcache_rbtree_ctx
*rbtree_ctx
;
304 struct rb_node
*node
;
305 struct regcache_rbtree_node
*rbnode
;
311 rbtree_ctx
= map
->cache
;
312 for (node
= rb_first(&rbtree_ctx
->root
); node
; node
= rb_next(node
)) {
313 rbnode
= rb_entry(node
, struct regcache_rbtree_node
, node
);
314 for (i
= 0; i
< rbnode
->blklen
; i
++) {
315 regtmp
= rbnode
->base_reg
+ i
;
316 val
= regcache_rbtree_get_register(rbnode
, i
,
317 map
->cache_word_size
);
319 /* Is this the hardware default? If so skip. */
320 ret
= regcache_lookup_reg(map
, i
);
321 if (ret
> 0 && val
== map
->reg_defaults
[ret
].def
)
324 map
->cache_bypass
= 1;
325 ret
= _regmap_write(map
, regtmp
, val
);
326 map
->cache_bypass
= 0;
329 dev_dbg(map
->dev
, "Synced register %#x, value %#x\n",
337 struct regcache_ops regcache_rbtree_ops
= {
338 .type
= REGCACHE_RBTREE
,
340 .init
= regcache_rbtree_init
,
341 .exit
= regcache_rbtree_exit
,
342 .read
= regcache_rbtree_read
,
343 .write
= regcache_rbtree_write
,
344 .sync
= regcache_rbtree_sync