Skip to content

Commit f635242

Browse files
committed
regmap: Add RAM backed register map
Add a register map that is a simple array of memory, for use in KUnit testing of the framework. This is not exposed in regmap.h since I can't think of a non-test use case, it is purely for use internally. To facilitate testing we track if registers have been read or written to. Signed-off-by: Mark Brown <broonie@kernel.org> Link: https://lore.kernel.org/r/20230324-regmap-kunit-v2-1-b208801dc2c8@kernel.org
1 parent 1e2bae6 commit f635242

4 files changed

Lines changed: 108 additions & 0 deletions

File tree

drivers/base/regmap/Kconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ config REGMAP_MMIO
4141
config REGMAP_IRQ
4242
bool
4343

44+
config REGMAP_RAM
45+
tristate
46+
4447
config REGMAP_SOUNDWIRE
4548
tristate
4649
depends on SOUNDWIRE

drivers/base/regmap/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ obj-$(CONFIG_REGMAP) += regcache-rbtree.o regcache-flat.o
77
obj-$(CONFIG_DEBUG_FS) += regmap-debugfs.o
88
obj-$(CONFIG_REGMAP_AC97) += regmap-ac97.o
99
obj-$(CONFIG_REGMAP_I2C) += regmap-i2c.o
10+
obj-$(CONFIG_REGMAP_RAM) += regmap-ram.o
1011
obj-$(CONFIG_REGMAP_SLIMBUS) += regmap-slimbus.o
1112
obj-$(CONFIG_REGMAP_SPI) += regmap-spi.o
1213
obj-$(CONFIG_REGMAP_SPMI) += regmap-spmi.o

drivers/base/regmap/internal.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,4 +306,23 @@ static inline unsigned int regcache_get_index_by_order(const struct regmap *map,
306306
return reg >> map->reg_stride_order;
307307
}
308308

309+
struct regmap_ram_data {
310+
unsigned int *vals; /* Allocatd by caller */
311+
bool *read;
312+
bool *written;
313+
};
314+
315+
/*
316+
* Create a test register map with data stored in RAM, not intended
317+
* for practical use.
318+
*/
319+
struct regmap *__regmap_init_ram(const struct regmap_config *config,
320+
struct regmap_ram_data *data,
321+
struct lock_class_key *lock_key,
322+
const char *lock_name);
323+
324+
#define regmap_init_ram(config, data) \
325+
__regmap_lockdep_wrapper(__regmap_init_ram, #config, config, data)
326+
327+
309328
#endif

drivers/base/regmap/regmap-ram.c

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
//
3+
// Register map access API - Memory region
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 int regmap_ram_write(void *context, unsigned int reg, unsigned int val)
20+
{
21+
struct regmap_ram_data *data = context;
22+
23+
data->vals[reg] = val;
24+
data->written[reg] = true;
25+
26+
return 0;
27+
}
28+
29+
static int regmap_ram_read(void *context, unsigned int reg, unsigned int *val)
30+
{
31+
struct regmap_ram_data *data = context;
32+
33+
*val = data->vals[reg];
34+
data->read[reg] = true;
35+
36+
return 0;
37+
}
38+
39+
static void regmap_ram_free_context(void *context)
40+
{
41+
struct regmap_ram_data *data = context;
42+
43+
kfree(data->vals);
44+
kfree(data->read);
45+
kfree(data->written);
46+
kfree(data);
47+
}
48+
49+
static const struct regmap_bus regmap_ram = {
50+
.fast_io = true,
51+
.reg_write = regmap_ram_write,
52+
.reg_read = regmap_ram_read,
53+
.free_context = regmap_ram_free_context,
54+
};
55+
56+
struct regmap *__regmap_init_ram(const struct regmap_config *config,
57+
struct regmap_ram_data *data,
58+
struct lock_class_key *lock_key,
59+
const char *lock_name)
60+
{
61+
struct regmap *map;
62+
63+
if (!config->max_register) {
64+
pr_crit("No max_register specified for RAM regmap\n");
65+
return ERR_PTR(-EINVAL);
66+
}
67+
68+
data->read = kcalloc(sizeof(bool), config->max_register + 1,
69+
GFP_KERNEL);
70+
if (!data->read)
71+
return ERR_PTR(-ENOMEM);
72+
73+
data->written = kcalloc(sizeof(bool), config->max_register + 1,
74+
GFP_KERNEL);
75+
if (!data->written)
76+
return ERR_PTR(-ENOMEM);
77+
78+
map = __regmap_init(NULL, &regmap_ram, data, config,
79+
lock_key, lock_name);
80+
81+
return map;
82+
}
83+
EXPORT_SYMBOL_GPL(__regmap_init_ram);
84+
85+
MODULE_LICENSE("GPL v2");

0 commit comments

Comments
 (0)