Skip to content

Commit 7b7982f

Browse files
rfvirgilbroonie
authored andcommitted
regmap: kunit: Create a struct device for the regmap
Use kunit_device_register() to create a real struct device for the regmap instead of leaving it at NULL. The main reason for this is that it allows context data to be passed into the readable_reg/writable_reg/volatile_reg functions by attaching it to the struct device with dev_set_drvdata(). The gen_regmap() and gen_raw_regmap() functions are updated to take a struct kunit * argument. A new struct regmap_test_priv has been created to hold the struct device created by kunit_device_register(). This allows the struct to be extended in the future to hold more private data for the test suite. Signed-off-by: Richard Fitzgerald <[email protected]> Link: https://msgid.link/r/[email protected] Signed-off-by: Mark Brown <[email protected]>
1 parent 866f702 commit 7b7982f

File tree

4 files changed

+87
-42
lines changed

4 files changed

+87
-42
lines changed

drivers/base/regmap/internal.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -326,20 +326,22 @@ struct regmap_ram_data {
326326
* Create a test register map with data stored in RAM, not intended
327327
* for practical use.
328328
*/
329-
struct regmap *__regmap_init_ram(const struct regmap_config *config,
329+
struct regmap *__regmap_init_ram(struct device *dev,
330+
const struct regmap_config *config,
330331
struct regmap_ram_data *data,
331332
struct lock_class_key *lock_key,
332333
const char *lock_name);
333334

334-
#define regmap_init_ram(config, data) \
335-
__regmap_lockdep_wrapper(__regmap_init_ram, #config, config, data)
335+
#define regmap_init_ram(dev, config, data) \
336+
__regmap_lockdep_wrapper(__regmap_init_ram, #dev, dev, config, data)
336337

337-
struct regmap *__regmap_init_raw_ram(const struct regmap_config *config,
338+
struct regmap *__regmap_init_raw_ram(struct device *dev,
339+
const struct regmap_config *config,
338340
struct regmap_ram_data *data,
339341
struct lock_class_key *lock_key,
340342
const char *lock_name);
341343

342-
#define regmap_init_raw_ram(config, data) \
343-
__regmap_lockdep_wrapper(__regmap_init_raw_ram, #config, config, data)
344+
#define regmap_init_raw_ram(dev, config, data) \
345+
__regmap_lockdep_wrapper(__regmap_init_raw_ram, #dev, dev, config, data)
344346

345347
#endif

drivers/base/regmap/regmap-kunit.c

Lines changed: 73 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@
44
//
55
// Copyright 2023 Arm Ltd
66

7+
#include <kunit/device.h>
78
#include <kunit/test.h>
89
#include "internal.h"
910

1011
#define BLOCK_TEST_SIZE 12
1112

13+
struct regmap_test_priv {
14+
struct device *dev;
15+
};
16+
1217
static void get_changed_bytes(void *orig, void *new, size_t size)
1318
{
1419
char *o = orig;
@@ -66,9 +71,11 @@ static const struct regcache_types sparse_cache_types_list[] = {
6671

6772
KUNIT_ARRAY_PARAM(sparse_cache_types, sparse_cache_types_list, case_to_desc);
6873

69-
static struct regmap *gen_regmap(struct regmap_config *config,
74+
static struct regmap *gen_regmap(struct kunit *test,
75+
struct regmap_config *config,
7076
struct regmap_ram_data **data)
7177
{
78+
struct regmap_test_priv *priv = test->priv;
7279
unsigned int *buf;
7380
struct regmap *ret;
7481
size_t size = (config->max_register + 1) * sizeof(unsigned int);
@@ -103,7 +110,7 @@ static struct regmap *gen_regmap(struct regmap_config *config,
103110
}
104111
}
105112

106-
ret = regmap_init_ram(config, *data);
113+
ret = regmap_init_ram(priv->dev, config, *data);
107114
if (IS_ERR(ret)) {
108115
kfree(buf);
109116
kfree(*data);
@@ -128,7 +135,7 @@ static void basic_read_write(struct kunit *test)
128135
config = test_regmap_config;
129136
config.cache_type = t->type;
130137

131-
map = gen_regmap(&config, &data);
138+
map = gen_regmap(test, &config, &data);
132139
KUNIT_ASSERT_FALSE(test, IS_ERR(map));
133140
if (IS_ERR(map))
134141
return;
@@ -158,7 +165,7 @@ static void bulk_write(struct kunit *test)
158165
config = test_regmap_config;
159166
config.cache_type = t->type;
160167

161-
map = gen_regmap(&config, &data);
168+
map = gen_regmap(test, &config, &data);
162169
KUNIT_ASSERT_FALSE(test, IS_ERR(map));
163170
if (IS_ERR(map))
164171
return;
@@ -195,7 +202,7 @@ static void bulk_read(struct kunit *test)
195202
config = test_regmap_config;
196203
config.cache_type = t->type;
197204

198-
map = gen_regmap(&config, &data);
205+
map = gen_regmap(test, &config, &data);
199206
KUNIT_ASSERT_FALSE(test, IS_ERR(map));
200207
if (IS_ERR(map))
201208
return;
@@ -230,7 +237,7 @@ static void write_readonly(struct kunit *test)
230237
config.num_reg_defaults = BLOCK_TEST_SIZE;
231238
config.writeable_reg = reg_5_false;
232239

233-
map = gen_regmap(&config, &data);
240+
map = gen_regmap(test, &config, &data);
234241
KUNIT_ASSERT_FALSE(test, IS_ERR(map));
235242
if (IS_ERR(map))
236243
return;
@@ -264,7 +271,7 @@ static void read_writeonly(struct kunit *test)
264271
config.cache_type = t->type;
265272
config.readable_reg = reg_5_false;
266273

267-
map = gen_regmap(&config, &data);
274+
map = gen_regmap(test, &config, &data);
268275
KUNIT_ASSERT_FALSE(test, IS_ERR(map));
269276
if (IS_ERR(map))
270277
return;
@@ -304,7 +311,7 @@ static void reg_defaults(struct kunit *test)
304311
config.cache_type = t->type;
305312
config.num_reg_defaults = BLOCK_TEST_SIZE;
306313

307-
map = gen_regmap(&config, &data);
314+
map = gen_regmap(test, &config, &data);
308315
KUNIT_ASSERT_FALSE(test, IS_ERR(map));
309316
if (IS_ERR(map))
310317
return;
@@ -332,7 +339,7 @@ static void reg_defaults_read_dev(struct kunit *test)
332339
config.cache_type = t->type;
333340
config.num_reg_defaults_raw = BLOCK_TEST_SIZE;
334341

335-
map = gen_regmap(&config, &data);
342+
map = gen_regmap(test, &config, &data);
336343
KUNIT_ASSERT_FALSE(test, IS_ERR(map));
337344
if (IS_ERR(map))
338345
return;
@@ -368,7 +375,7 @@ static void register_patch(struct kunit *test)
368375
config.cache_type = t->type;
369376
config.num_reg_defaults = BLOCK_TEST_SIZE;
370377

371-
map = gen_regmap(&config, &data);
378+
map = gen_regmap(test, &config, &data);
372379
KUNIT_ASSERT_FALSE(test, IS_ERR(map));
373380
if (IS_ERR(map))
374381
return;
@@ -419,7 +426,7 @@ static void stride(struct kunit *test)
419426
config.reg_stride = 2;
420427
config.num_reg_defaults = BLOCK_TEST_SIZE / 2;
421428

422-
map = gen_regmap(&config, &data);
429+
map = gen_regmap(test, &config, &data);
423430
KUNIT_ASSERT_FALSE(test, IS_ERR(map));
424431
if (IS_ERR(map))
425432
return;
@@ -495,7 +502,7 @@ static void basic_ranges(struct kunit *test)
495502
config.num_ranges = 1;
496503
config.max_register = test_range.range_max;
497504

498-
map = gen_regmap(&config, &data);
505+
map = gen_regmap(test, &config, &data);
499506
KUNIT_ASSERT_FALSE(test, IS_ERR(map));
500507
if (IS_ERR(map))
501508
return;
@@ -565,7 +572,7 @@ static void stress_insert(struct kunit *test)
565572
config.cache_type = t->type;
566573
config.max_register = 300;
567574

568-
map = gen_regmap(&config, &data);
575+
map = gen_regmap(test, &config, &data);
569576
KUNIT_ASSERT_FALSE(test, IS_ERR(map));
570577
if (IS_ERR(map))
571578
return;
@@ -616,7 +623,7 @@ static void cache_bypass(struct kunit *test)
616623
config = test_regmap_config;
617624
config.cache_type = t->type;
618625

619-
map = gen_regmap(&config, &data);
626+
map = gen_regmap(test, &config, &data);
620627
KUNIT_ASSERT_FALSE(test, IS_ERR(map));
621628
if (IS_ERR(map))
622629
return;
@@ -655,7 +662,7 @@ static void cache_sync(struct kunit *test)
655662
config = test_regmap_config;
656663
config.cache_type = t->type;
657664

658-
map = gen_regmap(&config, &data);
665+
map = gen_regmap(test, &config, &data);
659666
KUNIT_ASSERT_FALSE(test, IS_ERR(map));
660667
if (IS_ERR(map))
661668
return;
@@ -694,7 +701,7 @@ static void cache_sync_defaults(struct kunit *test)
694701
config.cache_type = t->type;
695702
config.num_reg_defaults = BLOCK_TEST_SIZE;
696703

697-
map = gen_regmap(&config, &data);
704+
map = gen_regmap(test, &config, &data);
698705
KUNIT_ASSERT_FALSE(test, IS_ERR(map));
699706
if (IS_ERR(map))
700707
return;
@@ -730,7 +737,7 @@ static void cache_sync_readonly(struct kunit *test)
730737
config.cache_type = t->type;
731738
config.writeable_reg = reg_5_false;
732739

733-
map = gen_regmap(&config, &data);
740+
map = gen_regmap(test, &config, &data);
734741
KUNIT_ASSERT_FALSE(test, IS_ERR(map));
735742
if (IS_ERR(map))
736743
return;
@@ -773,7 +780,7 @@ static void cache_sync_patch(struct kunit *test)
773780
config.cache_type = t->type;
774781
config.num_reg_defaults = BLOCK_TEST_SIZE;
775782

776-
map = gen_regmap(&config, &data);
783+
map = gen_regmap(test, &config, &data);
777784
KUNIT_ASSERT_FALSE(test, IS_ERR(map));
778785
if (IS_ERR(map))
779786
return;
@@ -832,7 +839,7 @@ static void cache_drop(struct kunit *test)
832839
config.cache_type = t->type;
833840
config.num_reg_defaults = BLOCK_TEST_SIZE;
834841

835-
map = gen_regmap(&config, &data);
842+
map = gen_regmap(test, &config, &data);
836843
KUNIT_ASSERT_FALSE(test, IS_ERR(map));
837844
if (IS_ERR(map))
838845
return;
@@ -873,7 +880,7 @@ static void cache_present(struct kunit *test)
873880
config = test_regmap_config;
874881
config.cache_type = t->type;
875882

876-
map = gen_regmap(&config, &data);
883+
map = gen_regmap(test, &config, &data);
877884
KUNIT_ASSERT_FALSE(test, IS_ERR(map));
878885
if (IS_ERR(map))
879886
return;
@@ -917,7 +924,7 @@ static void cache_range_window_reg(struct kunit *test)
917924
config.num_ranges = 1;
918925
config.max_register = test_range.range_max;
919926

920-
map = gen_regmap(&config, &data);
927+
map = gen_regmap(test, &config, &data);
921928
KUNIT_ASSERT_FALSE(test, IS_ERR(map));
922929
if (IS_ERR(map))
923930
return;
@@ -997,10 +1004,12 @@ static const struct regmap_config raw_regmap_config = {
9971004
.val_bits = 16,
9981005
};
9991006

1000-
static struct regmap *gen_raw_regmap(struct regmap_config *config,
1007+
static struct regmap *gen_raw_regmap(struct kunit *test,
1008+
struct regmap_config *config,
10011009
struct raw_test_types *test_type,
10021010
struct regmap_ram_data **data)
10031011
{
1012+
struct regmap_test_priv *priv = test->priv;
10041013
u16 *buf;
10051014
struct regmap *ret;
10061015
size_t size = (config->max_register + 1) * config->reg_bits / 8;
@@ -1052,7 +1061,7 @@ static struct regmap *gen_raw_regmap(struct regmap_config *config,
10521061
if (config->cache_type == REGCACHE_NONE)
10531062
config->num_reg_defaults = 0;
10541063

1055-
ret = regmap_init_raw_ram(config, *data);
1064+
ret = regmap_init_raw_ram(priv->dev, config, *data);
10561065
if (IS_ERR(ret)) {
10571066
kfree(buf);
10581067
kfree(*data);
@@ -1072,7 +1081,7 @@ static void raw_read_defaults_single(struct kunit *test)
10721081

10731082
config = raw_regmap_config;
10741083

1075-
map = gen_raw_regmap(&config, t, &data);
1084+
map = gen_raw_regmap(test, &config, t, &data);
10761085
KUNIT_ASSERT_FALSE(test, IS_ERR(map));
10771086
if (IS_ERR(map))
10781087
return;
@@ -1099,7 +1108,7 @@ static void raw_read_defaults(struct kunit *test)
10991108

11001109
config = raw_regmap_config;
11011110

1102-
map = gen_raw_regmap(&config, t, &data);
1111+
map = gen_raw_regmap(test, &config, t, &data);
11031112
KUNIT_ASSERT_FALSE(test, IS_ERR(map));
11041113
if (IS_ERR(map))
11051114
return;
@@ -1109,7 +1118,7 @@ static void raw_read_defaults(struct kunit *test)
11091118
KUNIT_ASSERT_TRUE(test, rval != NULL);
11101119
if (!rval)
11111120
return;
1112-
1121+
11131122
/* Check that we can read the defaults via the API */
11141123
KUNIT_EXPECT_EQ(test, 0, regmap_raw_read(map, 0, rval, val_len));
11151124
for (i = 0; i < config.max_register + 1; i++) {
@@ -1136,7 +1145,7 @@ static void raw_write_read_single(struct kunit *test)
11361145

11371146
config = raw_regmap_config;
11381147

1139-
map = gen_raw_regmap(&config, t, &data);
1148+
map = gen_raw_regmap(test, &config, t, &data);
11401149
KUNIT_ASSERT_FALSE(test, IS_ERR(map));
11411150
if (IS_ERR(map))
11421151
return;
@@ -1164,7 +1173,7 @@ static void raw_write(struct kunit *test)
11641173

11651174
config = raw_regmap_config;
11661175

1167-
map = gen_raw_regmap(&config, t, &data);
1176+
map = gen_raw_regmap(test, &config, t, &data);
11681177
KUNIT_ASSERT_FALSE(test, IS_ERR(map));
11691178
if (IS_ERR(map))
11701179
return;
@@ -1228,7 +1237,7 @@ static void raw_noinc_write(struct kunit *test)
12281237
config.writeable_noinc_reg = reg_zero;
12291238
config.readable_noinc_reg = reg_zero;
12301239

1231-
map = gen_raw_regmap(&config, t, &data);
1240+
map = gen_raw_regmap(test, &config, t, &data);
12321241
KUNIT_ASSERT_FALSE(test, IS_ERR(map));
12331242
if (IS_ERR(map))
12341243
return;
@@ -1276,7 +1285,7 @@ static void raw_sync(struct kunit *test)
12761285

12771286
config = raw_regmap_config;
12781287

1279-
map = gen_raw_regmap(&config, t, &data);
1288+
map = gen_raw_regmap(test, &config, t, &data);
12801289
KUNIT_ASSERT_FALSE(test, IS_ERR(map));
12811290
if (IS_ERR(map))
12821291
return;
@@ -1323,7 +1332,7 @@ static void raw_sync(struct kunit *test)
13231332
val[2] = cpu_to_be16(val[2]);
13241333
else
13251334
val[2] = cpu_to_le16(val[2]);
1326-
1335+
13271336
/* The values should not appear in the "hardware" */
13281337
KUNIT_EXPECT_MEMNEQ(test, &hw_buf[2], &val[0], sizeof(val));
13291338

@@ -1356,7 +1365,7 @@ static void raw_ranges(struct kunit *test)
13561365
config.num_ranges = 1;
13571366
config.max_register = test_range.range_max;
13581367

1359-
map = gen_raw_regmap(&config, t, &data);
1368+
map = gen_raw_regmap(test, &config, t, &data);
13601369
KUNIT_ASSERT_FALSE(test, IS_ERR(map));
13611370
if (IS_ERR(map))
13621371
return;
@@ -1437,8 +1446,40 @@ static struct kunit_case regmap_test_cases[] = {
14371446
{}
14381447
};
14391448

1449+
static int regmap_test_init(struct kunit *test)
1450+
{
1451+
struct regmap_test_priv *priv;
1452+
struct device *dev;
1453+
1454+
priv = kunit_kzalloc(test, sizeof(*priv), GFP_KERNEL);
1455+
if (!priv)
1456+
return -ENOMEM;
1457+
1458+
test->priv = priv;
1459+
1460+
dev = kunit_device_register(test, "regmap_test");
1461+
priv->dev = get_device(dev);
1462+
if (!priv->dev)
1463+
return -ENODEV;
1464+
1465+
dev_set_drvdata(dev, test);
1466+
1467+
return 0;
1468+
}
1469+
1470+
static void regmap_test_exit(struct kunit *test)
1471+
{
1472+
struct regmap_test_priv *priv = test->priv;
1473+
1474+
/* Destroy the dummy struct device */
1475+
if (priv && priv->dev)
1476+
put_device(priv->dev);
1477+
}
1478+
14401479
static struct kunit_suite regmap_test_suite = {
14411480
.name = "regmap",
1481+
.init = regmap_test_init,
1482+
.exit = regmap_test_exit,
14421483
.test_cases = regmap_test_cases,
14431484
};
14441485
kunit_test_suite(regmap_test_suite);

0 commit comments

Comments
 (0)