Skip to content

Commit 1800330

Browse files
committed
regmap: Add test that writes to write only registers are prevented
We should have error checking that verifies that writes to write only registers are suppressed, verify that this happens as it should. Signed-off-by: Mark Brown <broonie@kernel.org> Link: https://lore.kernel.org/r/20230613-regmap-kunit-read-write-v1-1-2db337c52827@kernel.org Signed-off-by: Mark Brown <broonie@kernel.org>
1 parent b629c69 commit 1800330

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

drivers/base/regmap/regmap-kunit.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ static struct regmap *gen_regmap(struct regmap_config *config,
9292
return ret;
9393
}
9494

95+
static bool reg_5_false(struct device *context, unsigned int reg)
96+
{
97+
return reg != 5;
98+
}
99+
95100
static void basic_read_write(struct kunit *test)
96101
{
97102
struct regcache_types *t = (struct regcache_types *)test->param_value;
@@ -191,6 +196,41 @@ static void bulk_read(struct kunit *test)
191196
regmap_exit(map);
192197
}
193198

199+
static void write_readonly(struct kunit *test)
200+
{
201+
struct regcache_types *t = (struct regcache_types *)test->param_value;
202+
struct regmap *map;
203+
struct regmap_config config;
204+
struct regmap_ram_data *data;
205+
unsigned int val;
206+
int i;
207+
208+
config = test_regmap_config;
209+
config.cache_type = t->type;
210+
config.num_reg_defaults = BLOCK_TEST_SIZE;
211+
config.writeable_reg = reg_5_false;
212+
213+
map = gen_regmap(&config, &data);
214+
KUNIT_ASSERT_FALSE(test, IS_ERR(map));
215+
if (IS_ERR(map))
216+
return;
217+
218+
get_random_bytes(&val, sizeof(val));
219+
220+
for (i = 0; i < BLOCK_TEST_SIZE; i++)
221+
data->written[i] = false;
222+
223+
/* Change the value of all registers, readonly should fail */
224+
for (i = 0; i < BLOCK_TEST_SIZE; i++)
225+
KUNIT_EXPECT_EQ(test, i != 5, regmap_write(map, i, val) == 0);
226+
227+
/* Did that match what we see on the device? */
228+
for (i = 0; i < BLOCK_TEST_SIZE; i++)
229+
KUNIT_EXPECT_EQ(test, i != 5, data->written[i]);
230+
231+
regmap_exit(map);
232+
}
233+
194234
static void reg_defaults(struct kunit *test)
195235
{
196236
struct regcache_types *t = (struct regcache_types *)test->param_value;
@@ -1037,6 +1077,7 @@ static struct kunit_case regmap_test_cases[] = {
10371077
KUNIT_CASE_PARAM(basic_read_write, regcache_types_gen_params),
10381078
KUNIT_CASE_PARAM(bulk_write, regcache_types_gen_params),
10391079
KUNIT_CASE_PARAM(bulk_read, regcache_types_gen_params),
1080+
KUNIT_CASE_PARAM(write_readonly, regcache_types_gen_params),
10401081
KUNIT_CASE_PARAM(reg_defaults, regcache_types_gen_params),
10411082
KUNIT_CASE_PARAM(reg_defaults_read_dev, regcache_types_gen_params),
10421083
KUNIT_CASE_PARAM(register_patch, regcache_types_gen_params),

0 commit comments

Comments
 (0)