unittest.c (962a70d05edac2e2eb53cd077715930083964b9e) unittest.c (d5e75500ca401d3128c82c5b0dee2f9b259d5b5c)
1/*
2 * Self tests for device tree subsystem
3 */
4
5#define pr_fmt(fmt) "### dt-test ### " fmt
6
7#include <linux/clk.h>
8#include <linux/err.h>

--- 6 unchanged lines hidden (view full) ---

15#include <linux/of_platform.h>
16#include <linux/list.h>
17#include <linux/mutex.h>
18#include <linux/slab.h>
19#include <linux/device.h>
20#include <linux/platform_device.h>
21#include <linux/of_platform.h>
22
1/*
2 * Self tests for device tree subsystem
3 */
4
5#define pr_fmt(fmt) "### dt-test ### " fmt
6
7#include <linux/clk.h>
8#include <linux/err.h>

--- 6 unchanged lines hidden (view full) ---

15#include <linux/of_platform.h>
16#include <linux/list.h>
17#include <linux/mutex.h>
18#include <linux/slab.h>
19#include <linux/device.h>
20#include <linux/platform_device.h>
21#include <linux/of_platform.h>
22
23#include <linux/i2c.h>
24#include <linux/i2c-mux.h>
25
23#include "of_private.h"
24
25static struct selftest_results {
26 int passed;
27 int failed;
28} selftest_results;
29
30#define NO_OF_NODES 3

--- 955 unchanged lines hidden (view full) ---

986{
987 struct platform_device *pdev;
988
989 pdev = of_path_to_platform_device(path);
990 platform_device_put(pdev);
991 return pdev != NULL;
992}
993
26#include "of_private.h"
27
28static struct selftest_results {
29 int passed;
30 int failed;
31} selftest_results;
32
33#define NO_OF_NODES 3

--- 955 unchanged lines hidden (view full) ---

989{
990 struct platform_device *pdev;
991
992 pdev = of_path_to_platform_device(path);
993 platform_device_put(pdev);
994 return pdev != NULL;
995}
996
994static const char *selftest_path(int nr)
997#if IS_ENABLED(CONFIG_I2C)
998
999/* get the i2c client device instantiated at the path */
1000static struct i2c_client *of_path_to_i2c_client(const char *path)
995{
1001{
1002 struct device_node *np;
1003 struct i2c_client *client;
1004
1005 np = of_find_node_by_path(path);
1006 if (np == NULL)
1007 return NULL;
1008
1009 client = of_find_i2c_device_by_node(np);
1010 of_node_put(np);
1011
1012 return client;
1013}
1014
1015/* find out if a i2c client device exists at that path */
1016static int of_path_i2c_client_exists(const char *path)
1017{
1018 struct i2c_client *client;
1019
1020 client = of_path_to_i2c_client(path);
1021 if (client)
1022 put_device(&client->dev);
1023 return client != NULL;
1024}
1025#else
1026static int of_path_i2c_client_exists(const char *path)
1027{
1028 return 0;
1029}
1030#endif
1031
1032enum overlay_type {
1033 PDEV_OVERLAY,
1034 I2C_OVERLAY
1035};
1036
1037static int of_path_device_type_exists(const char *path,
1038 enum overlay_type ovtype)
1039{
1040 switch (ovtype) {
1041 case PDEV_OVERLAY:
1042 return of_path_platform_device_exists(path);
1043 case I2C_OVERLAY:
1044 return of_path_i2c_client_exists(path);
1045 }
1046 return 0;
1047}
1048
1049static const char *selftest_path(int nr, enum overlay_type ovtype)
1050{
1051 const char *base;
996 static char buf[256];
997
1052 static char buf[256];
1053
998 snprintf(buf, sizeof(buf) - 1,
999 "/testcase-data/overlay-node/test-bus/test-selftest%d", nr);
1054 switch (ovtype) {
1055 case PDEV_OVERLAY:
1056 base = "/testcase-data/overlay-node/test-bus";
1057 break;
1058 case I2C_OVERLAY:
1059 base = "/testcase-data/overlay-node/test-bus/i2c-test-bus";
1060 break;
1061 default:
1062 buf[0] = '\0';
1063 return buf;
1064 }
1065 snprintf(buf, sizeof(buf) - 1, "%s/test-selftest%d", base, nr);
1000 buf[sizeof(buf) - 1] = '\0';
1066 buf[sizeof(buf) - 1] = '\0';
1001
1002 return buf;
1003}
1004
1067 return buf;
1068}
1069
1070static int of_selftest_device_exists(int selftest_nr, enum overlay_type ovtype)
1071{
1072 const char *path;
1073
1074 path = selftest_path(selftest_nr, ovtype);
1075
1076 switch (ovtype) {
1077 case PDEV_OVERLAY:
1078 return of_path_platform_device_exists(path);
1079 case I2C_OVERLAY:
1080 return of_path_i2c_client_exists(path);
1081 }
1082 return 0;
1083}
1084
1005static const char *overlay_path(int nr)
1006{
1007 static char buf[256];
1008
1009 snprintf(buf, sizeof(buf) - 1,
1010 "/testcase-data/overlay%d", nr);
1011 buf[sizeof(buf) - 1] = '\0';
1012

--- 32 unchanged lines hidden (view full) ---

1045 if (overlay_id)
1046 *overlay_id = id;
1047
1048 return ret;
1049}
1050
1051/* apply an overlay while checking before and after states */
1052static int of_selftest_apply_overlay_check(int overlay_nr, int selftest_nr,
1085static const char *overlay_path(int nr)
1086{
1087 static char buf[256];
1088
1089 snprintf(buf, sizeof(buf) - 1,
1090 "/testcase-data/overlay%d", nr);
1091 buf[sizeof(buf) - 1] = '\0';
1092

--- 32 unchanged lines hidden (view full) ---

1125 if (overlay_id)
1126 *overlay_id = id;
1127
1128 return ret;
1129}
1130
1131/* apply an overlay while checking before and after states */
1132static int of_selftest_apply_overlay_check(int overlay_nr, int selftest_nr,
1053 int before, int after)
1133 int before, int after, enum overlay_type ovtype)
1054{
1055 int ret;
1056
1057 /* selftest device must not be in before state */
1134{
1135 int ret;
1136
1137 /* selftest device must not be in before state */
1058 if (of_path_platform_device_exists(selftest_path(selftest_nr))
1059 != before) {
1138 if (of_selftest_device_exists(selftest_nr, ovtype) != before) {
1060 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1061 overlay_path(overlay_nr),
1139 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1140 overlay_path(overlay_nr),
1062 selftest_path(selftest_nr),
1141 selftest_path(selftest_nr, ovtype),
1063 !before ? "enabled" : "disabled");
1064 return -EINVAL;
1065 }
1066
1067 ret = of_selftest_apply_overlay(overlay_nr, selftest_nr, NULL);
1068 if (ret != 0) {
1069 /* of_selftest_apply_overlay already called selftest() */
1070 return ret;
1071 }
1072
1073 /* selftest device must be to set to after state */
1142 !before ? "enabled" : "disabled");
1143 return -EINVAL;
1144 }
1145
1146 ret = of_selftest_apply_overlay(overlay_nr, selftest_nr, NULL);
1147 if (ret != 0) {
1148 /* of_selftest_apply_overlay already called selftest() */
1149 return ret;
1150 }
1151
1152 /* selftest device must be to set to after state */
1074 if (of_path_platform_device_exists(selftest_path(selftest_nr))
1075 != after) {
1153 if (of_selftest_device_exists(selftest_nr, ovtype) != after) {
1076 selftest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n",
1077 overlay_path(overlay_nr),
1154 selftest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n",
1155 overlay_path(overlay_nr),
1078 selftest_path(selftest_nr),
1156 selftest_path(selftest_nr, ovtype),
1079 !after ? "enabled" : "disabled");
1080 return -EINVAL;
1081 }
1082
1083 return 0;
1084}
1085
1086/* apply an overlay and then revert it while checking before, after states */
1087static int of_selftest_apply_revert_overlay_check(int overlay_nr,
1157 !after ? "enabled" : "disabled");
1158 return -EINVAL;
1159 }
1160
1161 return 0;
1162}
1163
1164/* apply an overlay and then revert it while checking before, after states */
1165static int of_selftest_apply_revert_overlay_check(int overlay_nr,
1088 int selftest_nr, int before, int after)
1166 int selftest_nr, int before, int after,
1167 enum overlay_type ovtype)
1089{
1090 int ret, ov_id;
1091
1092 /* selftest device must be in before state */
1168{
1169 int ret, ov_id;
1170
1171 /* selftest device must be in before state */
1093 if (of_path_platform_device_exists(selftest_path(selftest_nr))
1094 != before) {
1172 if (of_selftest_device_exists(selftest_nr, ovtype) != before) {
1095 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1096 overlay_path(overlay_nr),
1173 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1174 overlay_path(overlay_nr),
1097 selftest_path(selftest_nr),
1175 selftest_path(selftest_nr, ovtype),
1098 !before ? "enabled" : "disabled");
1099 return -EINVAL;
1100 }
1101
1102 /* apply the overlay */
1103 ret = of_selftest_apply_overlay(overlay_nr, selftest_nr, &ov_id);
1104 if (ret != 0) {
1105 /* of_selftest_apply_overlay already called selftest() */
1106 return ret;
1107 }
1108
1109 /* selftest device must be in after state */
1176 !before ? "enabled" : "disabled");
1177 return -EINVAL;
1178 }
1179
1180 /* apply the overlay */
1181 ret = of_selftest_apply_overlay(overlay_nr, selftest_nr, &ov_id);
1182 if (ret != 0) {
1183 /* of_selftest_apply_overlay already called selftest() */
1184 return ret;
1185 }
1186
1187 /* selftest device must be in after state */
1110 if (of_path_platform_device_exists(selftest_path(selftest_nr))
1111 != after) {
1188 if (of_selftest_device_exists(selftest_nr, ovtype) != after) {
1112 selftest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n",
1113 overlay_path(overlay_nr),
1189 selftest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n",
1190 overlay_path(overlay_nr),
1114 selftest_path(selftest_nr),
1191 selftest_path(selftest_nr, ovtype),
1115 !after ? "enabled" : "disabled");
1116 return -EINVAL;
1117 }
1118
1119 ret = of_overlay_destroy(ov_id);
1120 if (ret != 0) {
1121 selftest(0, "overlay @\"%s\" failed to be destroyed @\"%s\"\n",
1122 overlay_path(overlay_nr),
1192 !after ? "enabled" : "disabled");
1193 return -EINVAL;
1194 }
1195
1196 ret = of_overlay_destroy(ov_id);
1197 if (ret != 0) {
1198 selftest(0, "overlay @\"%s\" failed to be destroyed @\"%s\"\n",
1199 overlay_path(overlay_nr),
1123 selftest_path(selftest_nr));
1200 selftest_path(selftest_nr, ovtype));
1124 return ret;
1125 }
1126
1127 /* selftest device must be again in before state */
1201 return ret;
1202 }
1203
1204 /* selftest device must be again in before state */
1128 if (of_path_platform_device_exists(selftest_path(selftest_nr))
1129 != before) {
1205 if (of_selftest_device_exists(selftest_nr, PDEV_OVERLAY) != before) {
1130 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1131 overlay_path(overlay_nr),
1206 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1207 overlay_path(overlay_nr),
1132 selftest_path(selftest_nr),
1208 selftest_path(selftest_nr, ovtype),
1133 !before ? "enabled" : "disabled");
1134 return -EINVAL;
1135 }
1136
1137 return 0;
1138}
1139
1140/* test activation of device */
1141static void of_selftest_overlay_0(void)
1142{
1143 int ret;
1144
1145 /* device should enable */
1209 !before ? "enabled" : "disabled");
1210 return -EINVAL;
1211 }
1212
1213 return 0;
1214}
1215
1216/* test activation of device */
1217static void of_selftest_overlay_0(void)
1218{
1219 int ret;
1220
1221 /* device should enable */
1146 ret = of_selftest_apply_overlay_check(0, 0, 0, 1);
1222 ret = of_selftest_apply_overlay_check(0, 0, 0, 1, PDEV_OVERLAY);
1147 if (ret != 0)
1148 return;
1149
1150 selftest(1, "overlay test %d passed\n", 0);
1151}
1152
1153/* test deactivation of device */
1154static void of_selftest_overlay_1(void)
1155{
1156 int ret;
1157
1158 /* device should disable */
1223 if (ret != 0)
1224 return;
1225
1226 selftest(1, "overlay test %d passed\n", 0);
1227}
1228
1229/* test deactivation of device */
1230static void of_selftest_overlay_1(void)
1231{
1232 int ret;
1233
1234 /* device should disable */
1159 ret = of_selftest_apply_overlay_check(1, 1, 1, 0);
1235 ret = of_selftest_apply_overlay_check(1, 1, 1, 0, PDEV_OVERLAY);
1160 if (ret != 0)
1161 return;
1162
1163 selftest(1, "overlay test %d passed\n", 1);
1164}
1165
1166/* test activation of device */
1167static void of_selftest_overlay_2(void)
1168{
1169 int ret;
1170
1171 /* device should enable */
1236 if (ret != 0)
1237 return;
1238
1239 selftest(1, "overlay test %d passed\n", 1);
1240}
1241
1242/* test activation of device */
1243static void of_selftest_overlay_2(void)
1244{
1245 int ret;
1246
1247 /* device should enable */
1172 ret = of_selftest_apply_overlay_check(2, 2, 0, 1);
1248 ret = of_selftest_apply_overlay_check(2, 2, 0, 1, PDEV_OVERLAY);
1173 if (ret != 0)
1174 return;
1175
1176 selftest(1, "overlay test %d passed\n", 2);
1177}
1178
1179/* test deactivation of device */
1180static void of_selftest_overlay_3(void)
1181{
1182 int ret;
1183
1184 /* device should disable */
1249 if (ret != 0)
1250 return;
1251
1252 selftest(1, "overlay test %d passed\n", 2);
1253}
1254
1255/* test deactivation of device */
1256static void of_selftest_overlay_3(void)
1257{
1258 int ret;
1259
1260 /* device should disable */
1185 ret = of_selftest_apply_overlay_check(3, 3, 1, 0);
1261 ret = of_selftest_apply_overlay_check(3, 3, 1, 0, PDEV_OVERLAY);
1186 if (ret != 0)
1187 return;
1188
1189 selftest(1, "overlay test %d passed\n", 3);
1190}
1191
1192/* test activation of a full device node */
1193static void of_selftest_overlay_4(void)
1194{
1195 int ret;
1196
1197 /* device should disable */
1262 if (ret != 0)
1263 return;
1264
1265 selftest(1, "overlay test %d passed\n", 3);
1266}
1267
1268/* test activation of a full device node */
1269static void of_selftest_overlay_4(void)
1270{
1271 int ret;
1272
1273 /* device should disable */
1198 ret = of_selftest_apply_overlay_check(4, 4, 0, 1);
1274 ret = of_selftest_apply_overlay_check(4, 4, 0, 1, PDEV_OVERLAY);
1199 if (ret != 0)
1200 return;
1201
1202 selftest(1, "overlay test %d passed\n", 4);
1203}
1204
1205/* test overlay apply/revert sequence */
1206static void of_selftest_overlay_5(void)
1207{
1208 int ret;
1209
1210 /* device should disable */
1275 if (ret != 0)
1276 return;
1277
1278 selftest(1, "overlay test %d passed\n", 4);
1279}
1280
1281/* test overlay apply/revert sequence */
1282static void of_selftest_overlay_5(void)
1283{
1284 int ret;
1285
1286 /* device should disable */
1211 ret = of_selftest_apply_revert_overlay_check(5, 5, 0, 1);
1287 ret = of_selftest_apply_revert_overlay_check(5, 5, 0, 1, PDEV_OVERLAY);
1212 if (ret != 0)
1213 return;
1214
1215 selftest(1, "overlay test %d passed\n", 5);
1216}
1217
1218/* test overlay application in sequence */
1219static void of_selftest_overlay_6(void)
1220{
1221 struct device_node *np;
1222 int ret, i, ov_id[2];
1223 int overlay_nr = 6, selftest_nr = 6;
1224 int before = 0, after = 1;
1225
1226 /* selftest device must be in before state */
1227 for (i = 0; i < 2; i++) {
1288 if (ret != 0)
1289 return;
1290
1291 selftest(1, "overlay test %d passed\n", 5);
1292}
1293
1294/* test overlay application in sequence */
1295static void of_selftest_overlay_6(void)
1296{
1297 struct device_node *np;
1298 int ret, i, ov_id[2];
1299 int overlay_nr = 6, selftest_nr = 6;
1300 int before = 0, after = 1;
1301
1302 /* selftest device must be in before state */
1303 for (i = 0; i < 2; i++) {
1228 if (of_path_platform_device_exists(
1229 selftest_path(selftest_nr + i))
1304 if (of_selftest_device_exists(selftest_nr + i, PDEV_OVERLAY)
1230 != before) {
1231 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1232 overlay_path(overlay_nr + i),
1305 != before) {
1306 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1307 overlay_path(overlay_nr + i),
1233 selftest_path(selftest_nr + i),
1308 selftest_path(selftest_nr + i,
1309 PDEV_OVERLAY),
1234 !before ? "enabled" : "disabled");
1235 return;
1236 }
1237 }
1238
1239 /* apply the overlays */
1240 for (i = 0; i < 2; i++) {
1241

--- 10 unchanged lines hidden (view full) ---

1252 overlay_path(overlay_nr + i));
1253 return;
1254 }
1255 ov_id[i] = ret;
1256 }
1257
1258 for (i = 0; i < 2; i++) {
1259 /* selftest device must be in after state */
1310 !before ? "enabled" : "disabled");
1311 return;
1312 }
1313 }
1314
1315 /* apply the overlays */
1316 for (i = 0; i < 2; i++) {
1317

--- 10 unchanged lines hidden (view full) ---

1328 overlay_path(overlay_nr + i));
1329 return;
1330 }
1331 ov_id[i] = ret;
1332 }
1333
1334 for (i = 0; i < 2; i++) {
1335 /* selftest device must be in after state */
1260 if (of_path_platform_device_exists(
1261 selftest_path(selftest_nr + i))
1336 if (of_selftest_device_exists(selftest_nr + i, PDEV_OVERLAY)
1262 != after) {
1263 selftest(0, "overlay @\"%s\" failed @\"%s\" %s\n",
1264 overlay_path(overlay_nr + i),
1337 != after) {
1338 selftest(0, "overlay @\"%s\" failed @\"%s\" %s\n",
1339 overlay_path(overlay_nr + i),
1265 selftest_path(selftest_nr + i),
1340 selftest_path(selftest_nr + i,
1341 PDEV_OVERLAY),
1266 !after ? "enabled" : "disabled");
1267 return;
1268 }
1269 }
1270
1271 for (i = 1; i >= 0; i--) {
1272 ret = of_overlay_destroy(ov_id[i]);
1273 if (ret != 0) {
1274 selftest(0, "overlay @\"%s\" failed destroy @\"%s\"\n",
1275 overlay_path(overlay_nr + i),
1342 !after ? "enabled" : "disabled");
1343 return;
1344 }
1345 }
1346
1347 for (i = 1; i >= 0; i--) {
1348 ret = of_overlay_destroy(ov_id[i]);
1349 if (ret != 0) {
1350 selftest(0, "overlay @\"%s\" failed destroy @\"%s\"\n",
1351 overlay_path(overlay_nr + i),
1276 selftest_path(selftest_nr + i));
1352 selftest_path(selftest_nr + i,
1353 PDEV_OVERLAY));
1277 return;
1278 }
1279 }
1280
1281 for (i = 0; i < 2; i++) {
1282 /* selftest device must be again in before state */
1354 return;
1355 }
1356 }
1357
1358 for (i = 0; i < 2; i++) {
1359 /* selftest device must be again in before state */
1283 if (of_path_platform_device_exists(
1284 selftest_path(selftest_nr + i))
1360 if (of_selftest_device_exists(selftest_nr + i, PDEV_OVERLAY)
1285 != before) {
1286 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1287 overlay_path(overlay_nr + i),
1361 != before) {
1362 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1363 overlay_path(overlay_nr + i),
1288 selftest_path(selftest_nr + i),
1364 selftest_path(selftest_nr + i,
1365 PDEV_OVERLAY),
1289 !before ? "enabled" : "disabled");
1290 return;
1291 }
1292 }
1293
1294 selftest(1, "overlay test %d passed\n", 6);
1295}
1296

--- 25 unchanged lines hidden (view full) ---

1322 ov_id[i] = ret;
1323 }
1324
1325 /* now try to remove first overlay (it should fail) */
1326 ret = of_overlay_destroy(ov_id[0]);
1327 if (ret == 0) {
1328 selftest(0, "overlay @\"%s\" was destroyed @\"%s\"\n",
1329 overlay_path(overlay_nr + 0),
1366 !before ? "enabled" : "disabled");
1367 return;
1368 }
1369 }
1370
1371 selftest(1, "overlay test %d passed\n", 6);
1372}
1373

--- 25 unchanged lines hidden (view full) ---

1399 ov_id[i] = ret;
1400 }
1401
1402 /* now try to remove first overlay (it should fail) */
1403 ret = of_overlay_destroy(ov_id[0]);
1404 if (ret == 0) {
1405 selftest(0, "overlay @\"%s\" was destroyed @\"%s\"\n",
1406 overlay_path(overlay_nr + 0),
1330 selftest_path(selftest_nr));
1407 selftest_path(selftest_nr,
1408 PDEV_OVERLAY));
1331 return;
1332 }
1333
1334 /* removing them in order should work */
1335 for (i = 1; i >= 0; i--) {
1336 ret = of_overlay_destroy(ov_id[i]);
1337 if (ret != 0) {
1338 selftest(0, "overlay @\"%s\" not destroyed @\"%s\"\n",
1339 overlay_path(overlay_nr + i),
1409 return;
1410 }
1411
1412 /* removing them in order should work */
1413 for (i = 1; i >= 0; i--) {
1414 ret = of_overlay_destroy(ov_id[i]);
1415 if (ret != 0) {
1416 selftest(0, "overlay @\"%s\" not destroyed @\"%s\"\n",
1417 overlay_path(overlay_nr + i),
1340 selftest_path(selftest_nr));
1418 selftest_path(selftest_nr,
1419 PDEV_OVERLAY));
1341 return;
1342 }
1343 }
1344
1345 selftest(1, "overlay test %d passed\n", 8);
1346}
1347
1348/* test insertion of a bus with parent devices */
1349static void of_selftest_overlay_10(void)
1350{
1351 int ret;
1352 char *child_path;
1353
1354 /* device should disable */
1420 return;
1421 }
1422 }
1423
1424 selftest(1, "overlay test %d passed\n", 8);
1425}
1426
1427/* test insertion of a bus with parent devices */
1428static void of_selftest_overlay_10(void)
1429{
1430 int ret;
1431 char *child_path;
1432
1433 /* device should disable */
1355 ret = of_selftest_apply_overlay_check(10, 10, 0, 1);
1356 if (selftest(ret == 0, "overlay test %d failed; overlay application\n", 10))
1434 ret = of_selftest_apply_overlay_check(10, 10, 0, 1, PDEV_OVERLAY);
1435 if (selftest(ret == 0,
1436 "overlay test %d failed; overlay application\n", 10))
1357 return;
1358
1359 child_path = kasprintf(GFP_KERNEL, "%s/test-selftest101",
1437 return;
1438
1439 child_path = kasprintf(GFP_KERNEL, "%s/test-selftest101",
1360 selftest_path(10));
1440 selftest_path(10, PDEV_OVERLAY));
1361 if (selftest(child_path, "overlay test %d failed; kasprintf\n", 10))
1362 return;
1363
1441 if (selftest(child_path, "overlay test %d failed; kasprintf\n", 10))
1442 return;
1443
1364 ret = of_path_platform_device_exists(child_path);
1444 ret = of_path_device_type_exists(child_path, PDEV_OVERLAY);
1365 kfree(child_path);
1366 if (selftest(ret, "overlay test %d failed; no child device\n", 10))
1367 return;
1368}
1369
1370/* test insertion of a bus with parent devices (and revert) */
1371static void of_selftest_overlay_11(void)
1372{
1373 int ret;
1374
1375 /* device should disable */
1445 kfree(child_path);
1446 if (selftest(ret, "overlay test %d failed; no child device\n", 10))
1447 return;
1448}
1449
1450/* test insertion of a bus with parent devices (and revert) */
1451static void of_selftest_overlay_11(void)
1452{
1453 int ret;
1454
1455 /* device should disable */
1376 ret = of_selftest_apply_revert_overlay_check(11, 11, 0, 1);
1377 if (selftest(ret == 0, "overlay test %d failed; overlay application\n", 11))
1456 ret = of_selftest_apply_revert_overlay_check(11, 11, 0, 1,
1457 PDEV_OVERLAY);
1458 if (selftest(ret == 0,
1459 "overlay test %d failed; overlay application\n", 11))
1378 return;
1379}
1380
1460 return;
1461}
1462
1463#if IS_ENABLED(CONFIG_I2C) && IS_ENABLED(CONFIG_OF_OVERLAY)
1464
1465struct selftest_i2c_bus_data {
1466 struct platform_device *pdev;
1467 struct i2c_adapter adap;
1468};
1469
1470static int selftest_i2c_master_xfer(struct i2c_adapter *adap,
1471 struct i2c_msg *msgs, int num)
1472{
1473 struct selftest_i2c_bus_data *std = i2c_get_adapdata(adap);
1474
1475 (void)std;
1476
1477 return num;
1478}
1479
1480static u32 selftest_i2c_functionality(struct i2c_adapter *adap)
1481{
1482 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
1483}
1484
1485static const struct i2c_algorithm selftest_i2c_algo = {
1486 .master_xfer = selftest_i2c_master_xfer,
1487 .functionality = selftest_i2c_functionality,
1488};
1489
1490static int selftest_i2c_bus_probe(struct platform_device *pdev)
1491{
1492 struct device *dev = &pdev->dev;
1493 struct device_node *np = dev->of_node;
1494 struct selftest_i2c_bus_data *std;
1495 struct i2c_adapter *adap;
1496 int ret;
1497
1498 if (np == NULL) {
1499 dev_err(dev, "No OF data for device\n");
1500 return -EINVAL;
1501
1502 }
1503
1504 dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
1505
1506 std = devm_kzalloc(dev, sizeof(*std), GFP_KERNEL);
1507 if (!std) {
1508 dev_err(dev, "Failed to allocate selftest i2c data\n");
1509 return -ENOMEM;
1510 }
1511
1512 /* link them together */
1513 std->pdev = pdev;
1514 platform_set_drvdata(pdev, std);
1515
1516 adap = &std->adap;
1517 i2c_set_adapdata(adap, std);
1518 adap->nr = -1;
1519 strlcpy(adap->name, pdev->name, sizeof(adap->name));
1520 adap->class = I2C_CLASS_DEPRECATED;
1521 adap->algo = &selftest_i2c_algo;
1522 adap->dev.parent = dev;
1523 adap->dev.of_node = dev->of_node;
1524 adap->timeout = 5 * HZ;
1525 adap->retries = 3;
1526
1527 ret = i2c_add_numbered_adapter(adap);
1528 if (ret != 0) {
1529 dev_err(dev, "Failed to add I2C adapter\n");
1530 return ret;
1531 }
1532
1533 return 0;
1534}
1535
1536static int selftest_i2c_bus_remove(struct platform_device *pdev)
1537{
1538 struct device *dev = &pdev->dev;
1539 struct device_node *np = dev->of_node;
1540 struct selftest_i2c_bus_data *std = platform_get_drvdata(pdev);
1541
1542 dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
1543 i2c_del_adapter(&std->adap);
1544
1545 return 0;
1546}
1547
1548static struct of_device_id selftest_i2c_bus_match[] = {
1549 { .compatible = "selftest-i2c-bus", },
1550 {},
1551};
1552
1553static struct platform_driver selftest_i2c_bus_driver = {
1554 .probe = selftest_i2c_bus_probe,
1555 .remove = selftest_i2c_bus_remove,
1556 .driver = {
1557 .name = "selftest-i2c-bus",
1558 .of_match_table = of_match_ptr(selftest_i2c_bus_match),
1559 },
1560};
1561
1562static int selftest_i2c_dev_probe(struct i2c_client *client,
1563 const struct i2c_device_id *id)
1564{
1565 struct device *dev = &client->dev;
1566 struct device_node *np = client->dev.of_node;
1567
1568 if (!np) {
1569 dev_err(dev, "No OF node\n");
1570 return -EINVAL;
1571 }
1572
1573 dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
1574
1575 return 0;
1576};
1577
1578static int selftest_i2c_dev_remove(struct i2c_client *client)
1579{
1580 struct device *dev = &client->dev;
1581 struct device_node *np = client->dev.of_node;
1582
1583 dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
1584 return 0;
1585}
1586
1587static const struct i2c_device_id selftest_i2c_dev_id[] = {
1588 { .name = "selftest-i2c-dev" },
1589 { }
1590};
1591
1592static struct i2c_driver selftest_i2c_dev_driver = {
1593 .driver = {
1594 .name = "selftest-i2c-dev",
1595 .owner = THIS_MODULE,
1596 },
1597 .probe = selftest_i2c_dev_probe,
1598 .remove = selftest_i2c_dev_remove,
1599 .id_table = selftest_i2c_dev_id,
1600};
1601
1602#if IS_ENABLED(CONFIG_I2C_MUX)
1603
1604struct selftest_i2c_mux_data {
1605 int nchans;
1606 struct i2c_adapter *adap[];
1607};
1608
1609static int selftest_i2c_mux_select_chan(struct i2c_adapter *adap,
1610 void *client, u32 chan)
1611{
1612 return 0;
1613}
1614
1615static int selftest_i2c_mux_probe(struct i2c_client *client,
1616 const struct i2c_device_id *id)
1617{
1618 int ret, i, nchans, size;
1619 struct device *dev = &client->dev;
1620 struct i2c_adapter *adap = to_i2c_adapter(dev->parent);
1621 struct device_node *np = client->dev.of_node, *child;
1622 struct selftest_i2c_mux_data *stm;
1623 u32 reg, max_reg;
1624
1625 dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
1626
1627 if (!np) {
1628 dev_err(dev, "No OF node\n");
1629 return -EINVAL;
1630 }
1631
1632 max_reg = (u32)-1;
1633 for_each_child_of_node(np, child) {
1634 ret = of_property_read_u32(child, "reg", &reg);
1635 if (ret)
1636 continue;
1637 if (max_reg == (u32)-1 || reg > max_reg)
1638 max_reg = reg;
1639 }
1640 nchans = max_reg == (u32)-1 ? 0 : max_reg + 1;
1641 if (nchans == 0) {
1642 dev_err(dev, "No channels\n");
1643 return -EINVAL;
1644 }
1645
1646 size = offsetof(struct selftest_i2c_mux_data, adap[nchans]);
1647 stm = devm_kzalloc(dev, size, GFP_KERNEL);
1648 if (!stm) {
1649 dev_err(dev, "Out of memory\n");
1650 return -ENOMEM;
1651 }
1652 stm->nchans = nchans;
1653 for (i = 0; i < nchans; i++) {
1654 stm->adap[i] = i2c_add_mux_adapter(adap, dev, client,
1655 0, i, 0, selftest_i2c_mux_select_chan, NULL);
1656 if (!stm->adap[i]) {
1657 dev_err(dev, "Failed to register mux #%d\n", i);
1658 for (i--; i >= 0; i--)
1659 i2c_del_mux_adapter(stm->adap[i]);
1660 return -ENODEV;
1661 }
1662 }
1663
1664 i2c_set_clientdata(client, stm);
1665
1666 return 0;
1667};
1668
1669static int selftest_i2c_mux_remove(struct i2c_client *client)
1670{
1671 struct device *dev = &client->dev;
1672 struct device_node *np = client->dev.of_node;
1673 struct selftest_i2c_mux_data *stm = i2c_get_clientdata(client);
1674 int i;
1675
1676 dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
1677 for (i = stm->nchans - 1; i >= 0; i--)
1678 i2c_del_mux_adapter(stm->adap[i]);
1679 return 0;
1680}
1681
1682static const struct i2c_device_id selftest_i2c_mux_id[] = {
1683 { .name = "selftest-i2c-mux" },
1684 { }
1685};
1686
1687static struct i2c_driver selftest_i2c_mux_driver = {
1688 .driver = {
1689 .name = "selftest-i2c-mux",
1690 .owner = THIS_MODULE,
1691 },
1692 .probe = selftest_i2c_mux_probe,
1693 .remove = selftest_i2c_mux_remove,
1694 .id_table = selftest_i2c_mux_id,
1695};
1696
1697#endif
1698
1699static int of_selftest_overlay_i2c_init(void)
1700{
1701 int ret;
1702
1703 ret = i2c_add_driver(&selftest_i2c_dev_driver);
1704 if (selftest(ret == 0,
1705 "could not register selftest i2c device driver\n"))
1706 return ret;
1707
1708 ret = platform_driver_register(&selftest_i2c_bus_driver);
1709 if (selftest(ret == 0,
1710 "could not register selftest i2c bus driver\n"))
1711 return ret;
1712
1713#if IS_ENABLED(CONFIG_I2C_MUX)
1714 ret = i2c_add_driver(&selftest_i2c_mux_driver);
1715 if (selftest(ret == 0,
1716 "could not register selftest i2c mux driver\n"))
1717 return ret;
1718#endif
1719
1720 return 0;
1721}
1722
1723static void of_selftest_overlay_i2c_cleanup(void)
1724{
1725#if IS_ENABLED(CONFIG_I2C_MUX)
1726 i2c_del_driver(&selftest_i2c_mux_driver);
1727#endif
1728 platform_driver_unregister(&selftest_i2c_bus_driver);
1729 i2c_del_driver(&selftest_i2c_dev_driver);
1730}
1731
1732static void of_selftest_overlay_i2c_12(void)
1733{
1734 int ret;
1735
1736 /* device should enable */
1737 ret = of_selftest_apply_overlay_check(12, 12, 0, 1, I2C_OVERLAY);
1738 if (ret != 0)
1739 return;
1740
1741 selftest(1, "overlay test %d passed\n", 12);
1742}
1743
1744/* test deactivation of device */
1745static void of_selftest_overlay_i2c_13(void)
1746{
1747 int ret;
1748
1749 /* device should disable */
1750 ret = of_selftest_apply_overlay_check(13, 13, 1, 0, I2C_OVERLAY);
1751 if (ret != 0)
1752 return;
1753
1754 selftest(1, "overlay test %d passed\n", 13);
1755}
1756
1757/* just check for i2c mux existence */
1758static void of_selftest_overlay_i2c_14(void)
1759{
1760}
1761
1762static void of_selftest_overlay_i2c_15(void)
1763{
1764 int ret;
1765
1766 /* device should enable */
1767 ret = of_selftest_apply_overlay_check(16, 15, 0, 1, I2C_OVERLAY);
1768 if (ret != 0)
1769 return;
1770
1771 selftest(1, "overlay test %d passed\n", 15);
1772}
1773
1774#else
1775
1776static inline void of_selftest_overlay_i2c_14(void) { }
1777static inline void of_selftest_overlay_i2c_15(void) { }
1778
1779#endif
1780
1381static void __init of_selftest_overlay(void)
1382{
1383 struct device_node *bus_np = NULL;
1384 int ret;
1385
1386 ret = platform_driver_register(&selftest_driver);
1387 if (ret != 0) {
1388 selftest(0, "could not register selftest driver\n");

--- 8 unchanged lines hidden (view full) ---

1397
1398 ret = of_platform_populate(bus_np, of_default_bus_match_table,
1399 NULL, NULL);
1400 if (ret != 0) {
1401 selftest(0, "could not populate bus @ \"%s\"\n", bus_path);
1402 goto out;
1403 }
1404
1781static void __init of_selftest_overlay(void)
1782{
1783 struct device_node *bus_np = NULL;
1784 int ret;
1785
1786 ret = platform_driver_register(&selftest_driver);
1787 if (ret != 0) {
1788 selftest(0, "could not register selftest driver\n");

--- 8 unchanged lines hidden (view full) ---

1797
1798 ret = of_platform_populate(bus_np, of_default_bus_match_table,
1799 NULL, NULL);
1800 if (ret != 0) {
1801 selftest(0, "could not populate bus @ \"%s\"\n", bus_path);
1802 goto out;
1803 }
1804
1405 if (!of_path_platform_device_exists(selftest_path(100))) {
1805 if (!of_selftest_device_exists(100, PDEV_OVERLAY)) {
1406 selftest(0, "could not find selftest0 @ \"%s\"\n",
1806 selftest(0, "could not find selftest0 @ \"%s\"\n",
1407 selftest_path(100));
1807 selftest_path(100, PDEV_OVERLAY));
1408 goto out;
1409 }
1410
1808 goto out;
1809 }
1810
1411 if (of_path_platform_device_exists(selftest_path(101))) {
1811 if (of_selftest_device_exists(101, PDEV_OVERLAY)) {
1412 selftest(0, "selftest1 @ \"%s\" should not exist\n",
1812 selftest(0, "selftest1 @ \"%s\" should not exist\n",
1413 selftest_path(101));
1813 selftest_path(101, PDEV_OVERLAY));
1414 goto out;
1415 }
1416
1417 selftest(1, "basic infrastructure of overlays passed");
1418
1419 /* tests in sequence */
1420 of_selftest_overlay_0();
1421 of_selftest_overlay_1();
1422 of_selftest_overlay_2();
1423 of_selftest_overlay_3();
1424 of_selftest_overlay_4();
1425 of_selftest_overlay_5();
1426 of_selftest_overlay_6();
1427 of_selftest_overlay_8();
1428
1429 of_selftest_overlay_10();
1430 of_selftest_overlay_11();
1431
1814 goto out;
1815 }
1816
1817 selftest(1, "basic infrastructure of overlays passed");
1818
1819 /* tests in sequence */
1820 of_selftest_overlay_0();
1821 of_selftest_overlay_1();
1822 of_selftest_overlay_2();
1823 of_selftest_overlay_3();
1824 of_selftest_overlay_4();
1825 of_selftest_overlay_5();
1826 of_selftest_overlay_6();
1827 of_selftest_overlay_8();
1828
1829 of_selftest_overlay_10();
1830 of_selftest_overlay_11();
1831
1832#if IS_ENABLED(CONFIG_I2C)
1833 if (selftest(of_selftest_overlay_i2c_init() == 0, "i2c init failed\n"))
1834 goto out;
1835
1836 of_selftest_overlay_i2c_12();
1837 of_selftest_overlay_i2c_13();
1838 of_selftest_overlay_i2c_14();
1839 of_selftest_overlay_i2c_15();
1840
1841 of_selftest_overlay_i2c_cleanup();
1842#endif
1843
1432out:
1433 of_node_put(bus_np);
1434}
1435
1436#else
1437static inline void __init of_selftest_overlay(void) { }
1438#endif
1439

--- 43 unchanged lines hidden ---
1844out:
1845 of_node_put(bus_np);
1846}
1847
1848#else
1849static inline void __init of_selftest_overlay(void) { }
1850#endif
1851

--- 43 unchanged lines hidden ---