Loading...
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/slab.h>
14#include <linux/device.h>
15#include <linux/seq_file.h>
16
17#include "internal.h"
18
19static int regcache_flat_init(struct regmap *map)
20{
21 int i;
22 unsigned int *cache;
23
24 map->cache = kzalloc(sizeof(unsigned int) * (map->max_register + 1),
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
37static int regcache_flat_exit(struct regmap *map)
38{
39 kfree(map->cache);
40 map->cache = NULL;
41
42 return 0;
43}
44
45static 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
55static 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
65struct 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};
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
19static inline unsigned int regcache_flat_get_index(const struct regmap *map,
20 unsigned int reg)
21{
22 return regcache_get_index_by_order(map, reg);
23}
24
25static int regcache_flat_init(struct regmap *map)
26{
27 int i;
28 unsigned int *cache;
29
30 if (!map || map->reg_stride_order < 0 || !map->max_register)
31 return -EINVAL;
32
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;
37
38 cache = map->cache;
39
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);
43
44 cache[index] = map->reg_defaults[i].def;
45 }
46
47 return 0;
48}
49
50static int regcache_flat_exit(struct regmap *map)
51{
52 kfree(map->cache);
53 map->cache = NULL;
54
55 return 0;
56}
57
58static int regcache_flat_read(struct regmap *map,
59 unsigned int reg, unsigned int *value)
60{
61 unsigned int *cache = map->cache;
62 unsigned int index = regcache_flat_get_index(map, reg);
63
64 *value = cache[index];
65
66 return 0;
67}
68
69static int regcache_flat_write(struct regmap *map, unsigned int reg,
70 unsigned int value)
71{
72 unsigned int *cache = map->cache;
73 unsigned int index = regcache_flat_get_index(map, reg);
74
75 cache[index] = value;
76
77 return 0;
78}
79
80struct 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,
87};