Skip to content

Commit 65dd2f6

Browse files
committed
regmap: Provide a ram backed regmap with raw support
Provide a simple, 16 bit only, RAM backed regmap which supports raw I/O for use in testing. Signed-off-by: Mark Brown <broonie@kernel.org> Link: https://lore.kernel.org/r/20230610-regcache-raw-kunit-v1-1-583112cd28ac@kernel.org Signed-off-by: Mark Brown <broonie@kernel.org>
1 parent 9561de3 commit 65dd2f6

3 files changed

Lines changed: 142 additions & 1 deletion

File tree

drivers/base/regmap/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ obj-$(CONFIG_DEBUG_FS) += regmap-debugfs.o
88
obj-$(CONFIG_REGMAP_KUNIT) += regmap-kunit.o
99
obj-$(CONFIG_REGMAP_AC97) += regmap-ac97.o
1010
obj-$(CONFIG_REGMAP_I2C) += regmap-i2c.o
11-
obj-$(CONFIG_REGMAP_RAM) += regmap-ram.o
11+
obj-$(CONFIG_REGMAP_RAM) += regmap-ram.o regmap-raw-ram.o
1212
obj-$(CONFIG_REGMAP_SLIMBUS) += regmap-slimbus.o
1313
obj-$(CONFIG_REGMAP_SPI) += regmap-spi.o
1414
obj-$(CONFIG_REGMAP_SPMI) += regmap-spmi.o

drivers/base/regmap/internal.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ struct regmap_ram_data {
312312
unsigned int *vals; /* Allocatd by caller */
313313
bool *read;
314314
bool *written;
315+
enum regmap_endian reg_endian;
315316
};
316317

317318
/*
@@ -326,5 +327,12 @@ struct regmap *__regmap_init_ram(const struct regmap_config *config,
326327
#define regmap_init_ram(config, data) \
327328
__regmap_lockdep_wrapper(__regmap_init_ram, #config, config, data)
328329

330+
struct regmap *__regmap_init_raw_ram(const struct regmap_config *config,
331+
struct regmap_ram_data *data,
332+
struct lock_class_key *lock_key,
333+
const char *lock_name);
334+
335+
#define regmap_init_raw_ram(config, data) \
336+
__regmap_lockdep_wrapper(__regmap_init_raw_ram, #config, config, data)
329337

330338
#endif
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
//
3+
// Register map access API - Memory region with raw access
4+
//
5+
// This is intended for testing only
6+
//
7+
// Copyright (c) 2023, Arm Ltd
8+
9+
#include <linux/clk.h>
10+
#include <linux/err.h>
11+
#include <linux/io.h>
12+
#include <linux/module.h>
13+
#include <linux/regmap.h>
14+
#include <linux/slab.h>
15+
#include <linux/swab.h>
16+
17+
#include "internal.h"
18+
19+
static unsigned int decode_reg(enum regmap_endian endian, const void *reg)
20+
{
21+
const u16 *r = reg;
22+
23+
if (endian == REGMAP_ENDIAN_BIG)
24+
return be16_to_cpu(*r);
25+
else
26+
return le16_to_cpu(*r);
27+
}
28+
29+
static int regmap_raw_ram_gather_write(void *context,
30+
const void *reg, size_t reg_len,
31+
const void *val, size_t val_len)
32+
{
33+
struct regmap_ram_data *data = context;
34+
unsigned int r;
35+
u16 *our_buf = (u16 *)data->vals;
36+
int i;
37+
38+
if (reg_len != 2)
39+
return -EINVAL;
40+
if (val_len % 2)
41+
return -EINVAL;
42+
43+
r = decode_reg(data->reg_endian, reg);
44+
memcpy(&our_buf[r], val, val_len);
45+
46+
for (i = 0; i < val_len / 2; i++)
47+
data->written[r + i] = true;
48+
49+
return 0;
50+
}
51+
52+
static int regmap_raw_ram_write(void *context, const void *data, size_t count)
53+
{
54+
return regmap_raw_ram_gather_write(context, data, 2,
55+
data + 2, count - 2);
56+
}
57+
58+
static int regmap_raw_ram_read(void *context,
59+
const void *reg, size_t reg_len,
60+
void *val, size_t val_len)
61+
{
62+
struct regmap_ram_data *data = context;
63+
unsigned int r;
64+
u16 *our_buf = (u16 *)data->vals;
65+
int i;
66+
67+
if (reg_len != 2)
68+
return -EINVAL;
69+
if (val_len % 2)
70+
return -EINVAL;
71+
72+
r = decode_reg(data->reg_endian, reg);
73+
memcpy(val, &our_buf[r], val_len);
74+
75+
for (i = 0; i < val_len / 2; i++)
76+
data->read[r + i] = true;
77+
78+
return 0;
79+
}
80+
81+
static void regmap_raw_ram_free_context(void *context)
82+
{
83+
struct regmap_ram_data *data = context;
84+
85+
kfree(data->vals);
86+
kfree(data->read);
87+
kfree(data->written);
88+
kfree(data);
89+
}
90+
91+
static const struct regmap_bus regmap_raw_ram = {
92+
.fast_io = true,
93+
.write = regmap_raw_ram_write,
94+
.gather_write = regmap_raw_ram_gather_write,
95+
.read = regmap_raw_ram_read,
96+
.free_context = regmap_raw_ram_free_context,
97+
};
98+
99+
struct regmap *__regmap_init_raw_ram(const struct regmap_config *config,
100+
struct regmap_ram_data *data,
101+
struct lock_class_key *lock_key,
102+
const char *lock_name)
103+
{
104+
struct regmap *map;
105+
106+
if (config->reg_bits != 16)
107+
return ERR_PTR(-EINVAL);
108+
109+
if (!config->max_register) {
110+
pr_crit("No max_register specified for RAM regmap\n");
111+
return ERR_PTR(-EINVAL);
112+
}
113+
114+
data->read = kcalloc(sizeof(bool), config->max_register + 1,
115+
GFP_KERNEL);
116+
if (!data->read)
117+
return ERR_PTR(-ENOMEM);
118+
119+
data->written = kcalloc(sizeof(bool), config->max_register + 1,
120+
GFP_KERNEL);
121+
if (!data->written)
122+
return ERR_PTR(-ENOMEM);
123+
124+
data->reg_endian = config->reg_format_endian;
125+
126+
map = __regmap_init(NULL, &regmap_raw_ram, data, config,
127+
lock_key, lock_name);
128+
129+
return map;
130+
}
131+
EXPORT_SYMBOL_GPL(__regmap_init_raw_ram);
132+
133+
MODULE_LICENSE("GPL v2");

0 commit comments

Comments
 (0)