4
4
//
5
5
// Copyright 2023 Arm Ltd
6
6
7
+ #include <kunit/device.h>
7
8
#include <kunit/test.h>
8
9
#include "internal.h"
9
10
10
11
#define BLOCK_TEST_SIZE 12
11
12
13
+ struct regmap_test_priv {
14
+ struct device * dev ;
15
+ };
16
+
12
17
static void get_changed_bytes (void * orig , void * new , size_t size )
13
18
{
14
19
char * o = orig ;
@@ -66,9 +71,11 @@ static const struct regcache_types sparse_cache_types_list[] = {
66
71
67
72
KUNIT_ARRAY_PARAM (sparse_cache_types , sparse_cache_types_list , case_to_desc );
68
73
69
- static struct regmap * gen_regmap (struct regmap_config * config ,
74
+ static struct regmap * gen_regmap (struct kunit * test ,
75
+ struct regmap_config * config ,
70
76
struct regmap_ram_data * * data )
71
77
{
78
+ struct regmap_test_priv * priv = test -> priv ;
72
79
unsigned int * buf ;
73
80
struct regmap * ret ;
74
81
size_t size = (config -> max_register + 1 ) * sizeof (unsigned int );
@@ -103,7 +110,7 @@ static struct regmap *gen_regmap(struct regmap_config *config,
103
110
}
104
111
}
105
112
106
- ret = regmap_init_ram (config , * data );
113
+ ret = regmap_init_ram (priv -> dev , config , * data );
107
114
if (IS_ERR (ret )) {
108
115
kfree (buf );
109
116
kfree (* data );
@@ -128,7 +135,7 @@ static void basic_read_write(struct kunit *test)
128
135
config = test_regmap_config ;
129
136
config .cache_type = t -> type ;
130
137
131
- map = gen_regmap (& config , & data );
138
+ map = gen_regmap (test , & config , & data );
132
139
KUNIT_ASSERT_FALSE (test , IS_ERR (map ));
133
140
if (IS_ERR (map ))
134
141
return ;
@@ -158,7 +165,7 @@ static void bulk_write(struct kunit *test)
158
165
config = test_regmap_config ;
159
166
config .cache_type = t -> type ;
160
167
161
- map = gen_regmap (& config , & data );
168
+ map = gen_regmap (test , & config , & data );
162
169
KUNIT_ASSERT_FALSE (test , IS_ERR (map ));
163
170
if (IS_ERR (map ))
164
171
return ;
@@ -195,7 +202,7 @@ static void bulk_read(struct kunit *test)
195
202
config = test_regmap_config ;
196
203
config .cache_type = t -> type ;
197
204
198
- map = gen_regmap (& config , & data );
205
+ map = gen_regmap (test , & config , & data );
199
206
KUNIT_ASSERT_FALSE (test , IS_ERR (map ));
200
207
if (IS_ERR (map ))
201
208
return ;
@@ -230,7 +237,7 @@ static void write_readonly(struct kunit *test)
230
237
config .num_reg_defaults = BLOCK_TEST_SIZE ;
231
238
config .writeable_reg = reg_5_false ;
232
239
233
- map = gen_regmap (& config , & data );
240
+ map = gen_regmap (test , & config , & data );
234
241
KUNIT_ASSERT_FALSE (test , IS_ERR (map ));
235
242
if (IS_ERR (map ))
236
243
return ;
@@ -264,7 +271,7 @@ static void read_writeonly(struct kunit *test)
264
271
config .cache_type = t -> type ;
265
272
config .readable_reg = reg_5_false ;
266
273
267
- map = gen_regmap (& config , & data );
274
+ map = gen_regmap (test , & config , & data );
268
275
KUNIT_ASSERT_FALSE (test , IS_ERR (map ));
269
276
if (IS_ERR (map ))
270
277
return ;
@@ -304,7 +311,7 @@ static void reg_defaults(struct kunit *test)
304
311
config .cache_type = t -> type ;
305
312
config .num_reg_defaults = BLOCK_TEST_SIZE ;
306
313
307
- map = gen_regmap (& config , & data );
314
+ map = gen_regmap (test , & config , & data );
308
315
KUNIT_ASSERT_FALSE (test , IS_ERR (map ));
309
316
if (IS_ERR (map ))
310
317
return ;
@@ -332,7 +339,7 @@ static void reg_defaults_read_dev(struct kunit *test)
332
339
config .cache_type = t -> type ;
333
340
config .num_reg_defaults_raw = BLOCK_TEST_SIZE ;
334
341
335
- map = gen_regmap (& config , & data );
342
+ map = gen_regmap (test , & config , & data );
336
343
KUNIT_ASSERT_FALSE (test , IS_ERR (map ));
337
344
if (IS_ERR (map ))
338
345
return ;
@@ -368,7 +375,7 @@ static void register_patch(struct kunit *test)
368
375
config .cache_type = t -> type ;
369
376
config .num_reg_defaults = BLOCK_TEST_SIZE ;
370
377
371
- map = gen_regmap (& config , & data );
378
+ map = gen_regmap (test , & config , & data );
372
379
KUNIT_ASSERT_FALSE (test , IS_ERR (map ));
373
380
if (IS_ERR (map ))
374
381
return ;
@@ -419,7 +426,7 @@ static void stride(struct kunit *test)
419
426
config .reg_stride = 2 ;
420
427
config .num_reg_defaults = BLOCK_TEST_SIZE / 2 ;
421
428
422
- map = gen_regmap (& config , & data );
429
+ map = gen_regmap (test , & config , & data );
423
430
KUNIT_ASSERT_FALSE (test , IS_ERR (map ));
424
431
if (IS_ERR (map ))
425
432
return ;
@@ -495,7 +502,7 @@ static void basic_ranges(struct kunit *test)
495
502
config .num_ranges = 1 ;
496
503
config .max_register = test_range .range_max ;
497
504
498
- map = gen_regmap (& config , & data );
505
+ map = gen_regmap (test , & config , & data );
499
506
KUNIT_ASSERT_FALSE (test , IS_ERR (map ));
500
507
if (IS_ERR (map ))
501
508
return ;
@@ -565,7 +572,7 @@ static void stress_insert(struct kunit *test)
565
572
config .cache_type = t -> type ;
566
573
config .max_register = 300 ;
567
574
568
- map = gen_regmap (& config , & data );
575
+ map = gen_regmap (test , & config , & data );
569
576
KUNIT_ASSERT_FALSE (test , IS_ERR (map ));
570
577
if (IS_ERR (map ))
571
578
return ;
@@ -616,7 +623,7 @@ static void cache_bypass(struct kunit *test)
616
623
config = test_regmap_config ;
617
624
config .cache_type = t -> type ;
618
625
619
- map = gen_regmap (& config , & data );
626
+ map = gen_regmap (test , & config , & data );
620
627
KUNIT_ASSERT_FALSE (test , IS_ERR (map ));
621
628
if (IS_ERR (map ))
622
629
return ;
@@ -655,7 +662,7 @@ static void cache_sync(struct kunit *test)
655
662
config = test_regmap_config ;
656
663
config .cache_type = t -> type ;
657
664
658
- map = gen_regmap (& config , & data );
665
+ map = gen_regmap (test , & config , & data );
659
666
KUNIT_ASSERT_FALSE (test , IS_ERR (map ));
660
667
if (IS_ERR (map ))
661
668
return ;
@@ -694,7 +701,7 @@ static void cache_sync_defaults(struct kunit *test)
694
701
config .cache_type = t -> type ;
695
702
config .num_reg_defaults = BLOCK_TEST_SIZE ;
696
703
697
- map = gen_regmap (& config , & data );
704
+ map = gen_regmap (test , & config , & data );
698
705
KUNIT_ASSERT_FALSE (test , IS_ERR (map ));
699
706
if (IS_ERR (map ))
700
707
return ;
@@ -730,7 +737,7 @@ static void cache_sync_readonly(struct kunit *test)
730
737
config .cache_type = t -> type ;
731
738
config .writeable_reg = reg_5_false ;
732
739
733
- map = gen_regmap (& config , & data );
740
+ map = gen_regmap (test , & config , & data );
734
741
KUNIT_ASSERT_FALSE (test , IS_ERR (map ));
735
742
if (IS_ERR (map ))
736
743
return ;
@@ -773,7 +780,7 @@ static void cache_sync_patch(struct kunit *test)
773
780
config .cache_type = t -> type ;
774
781
config .num_reg_defaults = BLOCK_TEST_SIZE ;
775
782
776
- map = gen_regmap (& config , & data );
783
+ map = gen_regmap (test , & config , & data );
777
784
KUNIT_ASSERT_FALSE (test , IS_ERR (map ));
778
785
if (IS_ERR (map ))
779
786
return ;
@@ -832,7 +839,7 @@ static void cache_drop(struct kunit *test)
832
839
config .cache_type = t -> type ;
833
840
config .num_reg_defaults = BLOCK_TEST_SIZE ;
834
841
835
- map = gen_regmap (& config , & data );
842
+ map = gen_regmap (test , & config , & data );
836
843
KUNIT_ASSERT_FALSE (test , IS_ERR (map ));
837
844
if (IS_ERR (map ))
838
845
return ;
@@ -873,7 +880,7 @@ static void cache_present(struct kunit *test)
873
880
config = test_regmap_config ;
874
881
config .cache_type = t -> type ;
875
882
876
- map = gen_regmap (& config , & data );
883
+ map = gen_regmap (test , & config , & data );
877
884
KUNIT_ASSERT_FALSE (test , IS_ERR (map ));
878
885
if (IS_ERR (map ))
879
886
return ;
@@ -917,7 +924,7 @@ static void cache_range_window_reg(struct kunit *test)
917
924
config .num_ranges = 1 ;
918
925
config .max_register = test_range .range_max ;
919
926
920
- map = gen_regmap (& config , & data );
927
+ map = gen_regmap (test , & config , & data );
921
928
KUNIT_ASSERT_FALSE (test , IS_ERR (map ));
922
929
if (IS_ERR (map ))
923
930
return ;
@@ -997,10 +1004,12 @@ static const struct regmap_config raw_regmap_config = {
997
1004
.val_bits = 16 ,
998
1005
};
999
1006
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 ,
1001
1009
struct raw_test_types * test_type ,
1002
1010
struct regmap_ram_data * * data )
1003
1011
{
1012
+ struct regmap_test_priv * priv = test -> priv ;
1004
1013
u16 * buf ;
1005
1014
struct regmap * ret ;
1006
1015
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,
1052
1061
if (config -> cache_type == REGCACHE_NONE )
1053
1062
config -> num_reg_defaults = 0 ;
1054
1063
1055
- ret = regmap_init_raw_ram (config , * data );
1064
+ ret = regmap_init_raw_ram (priv -> dev , config , * data );
1056
1065
if (IS_ERR (ret )) {
1057
1066
kfree (buf );
1058
1067
kfree (* data );
@@ -1072,7 +1081,7 @@ static void raw_read_defaults_single(struct kunit *test)
1072
1081
1073
1082
config = raw_regmap_config ;
1074
1083
1075
- map = gen_raw_regmap (& config , t , & data );
1084
+ map = gen_raw_regmap (test , & config , t , & data );
1076
1085
KUNIT_ASSERT_FALSE (test , IS_ERR (map ));
1077
1086
if (IS_ERR (map ))
1078
1087
return ;
@@ -1099,7 +1108,7 @@ static void raw_read_defaults(struct kunit *test)
1099
1108
1100
1109
config = raw_regmap_config ;
1101
1110
1102
- map = gen_raw_regmap (& config , t , & data );
1111
+ map = gen_raw_regmap (test , & config , t , & data );
1103
1112
KUNIT_ASSERT_FALSE (test , IS_ERR (map ));
1104
1113
if (IS_ERR (map ))
1105
1114
return ;
@@ -1109,7 +1118,7 @@ static void raw_read_defaults(struct kunit *test)
1109
1118
KUNIT_ASSERT_TRUE (test , rval != NULL );
1110
1119
if (!rval )
1111
1120
return ;
1112
-
1121
+
1113
1122
/* Check that we can read the defaults via the API */
1114
1123
KUNIT_EXPECT_EQ (test , 0 , regmap_raw_read (map , 0 , rval , val_len ));
1115
1124
for (i = 0 ; i < config .max_register + 1 ; i ++ ) {
@@ -1136,7 +1145,7 @@ static void raw_write_read_single(struct kunit *test)
1136
1145
1137
1146
config = raw_regmap_config ;
1138
1147
1139
- map = gen_raw_regmap (& config , t , & data );
1148
+ map = gen_raw_regmap (test , & config , t , & data );
1140
1149
KUNIT_ASSERT_FALSE (test , IS_ERR (map ));
1141
1150
if (IS_ERR (map ))
1142
1151
return ;
@@ -1164,7 +1173,7 @@ static void raw_write(struct kunit *test)
1164
1173
1165
1174
config = raw_regmap_config ;
1166
1175
1167
- map = gen_raw_regmap (& config , t , & data );
1176
+ map = gen_raw_regmap (test , & config , t , & data );
1168
1177
KUNIT_ASSERT_FALSE (test , IS_ERR (map ));
1169
1178
if (IS_ERR (map ))
1170
1179
return ;
@@ -1228,7 +1237,7 @@ static void raw_noinc_write(struct kunit *test)
1228
1237
config .writeable_noinc_reg = reg_zero ;
1229
1238
config .readable_noinc_reg = reg_zero ;
1230
1239
1231
- map = gen_raw_regmap (& config , t , & data );
1240
+ map = gen_raw_regmap (test , & config , t , & data );
1232
1241
KUNIT_ASSERT_FALSE (test , IS_ERR (map ));
1233
1242
if (IS_ERR (map ))
1234
1243
return ;
@@ -1276,7 +1285,7 @@ static void raw_sync(struct kunit *test)
1276
1285
1277
1286
config = raw_regmap_config ;
1278
1287
1279
- map = gen_raw_regmap (& config , t , & data );
1288
+ map = gen_raw_regmap (test , & config , t , & data );
1280
1289
KUNIT_ASSERT_FALSE (test , IS_ERR (map ));
1281
1290
if (IS_ERR (map ))
1282
1291
return ;
@@ -1323,7 +1332,7 @@ static void raw_sync(struct kunit *test)
1323
1332
val [2 ] = cpu_to_be16 (val [2 ]);
1324
1333
else
1325
1334
val [2 ] = cpu_to_le16 (val [2 ]);
1326
-
1335
+
1327
1336
/* The values should not appear in the "hardware" */
1328
1337
KUNIT_EXPECT_MEMNEQ (test , & hw_buf [2 ], & val [0 ], sizeof (val ));
1329
1338
@@ -1356,7 +1365,7 @@ static void raw_ranges(struct kunit *test)
1356
1365
config .num_ranges = 1 ;
1357
1366
config .max_register = test_range .range_max ;
1358
1367
1359
- map = gen_raw_regmap (& config , t , & data );
1368
+ map = gen_raw_regmap (test , & config , t , & data );
1360
1369
KUNIT_ASSERT_FALSE (test , IS_ERR (map ));
1361
1370
if (IS_ERR (map ))
1362
1371
return ;
@@ -1437,8 +1446,40 @@ static struct kunit_case regmap_test_cases[] = {
1437
1446
{}
1438
1447
};
1439
1448
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
+
1440
1479
static struct kunit_suite regmap_test_suite = {
1441
1480
.name = "regmap" ,
1481
+ .init = regmap_test_init ,
1482
+ .exit = regmap_test_exit ,
1442
1483
.test_cases = regmap_test_cases ,
1443
1484
};
1444
1485
kunit_test_suite (regmap_test_suite );
0 commit comments