aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/of/unittest.c
diff options
context:
space:
mode:
authorIngo Molnar <mingo@kernel.org>2015-03-05 14:52:18 -0500
committerIngo Molnar <mingo@kernel.org>2015-03-05 14:52:18 -0500
commit33ca8a53f262b4af40611bea331b8c87d133af72 (patch)
treed6468c820a556c4915bcb5b761204a0fb19e8225 /drivers/of/unittest.c
parentdb2dcb4f91d5fec5c346a82c309187ee821e2495 (diff)
parent13a7a6ac0a11197edcd0f756a035f472b42cdf8b (diff)
Merge tag 'v4.0-rc2' into irq/core, to refresh the tree before applying new changes
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Diffstat (limited to 'drivers/of/unittest.c')
-rw-r--r--drivers/of/unittest.c597
1 files changed, 474 insertions, 123 deletions
diff --git a/drivers/of/unittest.c b/drivers/of/unittest.c
index 41a4a138f53b..0cf9a236d438 100644
--- a/drivers/of/unittest.c
+++ b/drivers/of/unittest.c
@@ -20,6 +20,9 @@
20#include <linux/platform_device.h> 20#include <linux/platform_device.h>
21#include <linux/of_platform.h> 21#include <linux/of_platform.h>
22 22
23#include <linux/i2c.h>
24#include <linux/i2c-mux.h>
25
23#include "of_private.h" 26#include "of_private.h"
24 27
25static struct selftest_results { 28static struct selftest_results {
@@ -27,11 +30,6 @@ static struct selftest_results {
27 int failed; 30 int failed;
28} selftest_results; 31} selftest_results;
29 32
30#define NO_OF_NODES 3
31static struct device_node *nodes[NO_OF_NODES];
32static int last_node_index;
33static bool selftest_live_tree;
34
35#define selftest(result, fmt, ...) ({ \ 33#define selftest(result, fmt, ...) ({ \
36 bool failed = !(result); \ 34 bool failed = !(result); \
37 if (failed) { \ 35 if (failed) { \
@@ -822,6 +820,7 @@ static void update_node_properties(struct device_node *np,
822static int attach_node_and_children(struct device_node *np) 820static int attach_node_and_children(struct device_node *np)
823{ 821{
824 struct device_node *next, *dup, *child; 822 struct device_node *next, *dup, *child;
823 unsigned long flags;
825 824
826 dup = of_find_node_by_path(np->full_name); 825 dup = of_find_node_by_path(np->full_name);
827 if (dup) { 826 if (dup) {
@@ -829,17 +828,19 @@ static int attach_node_and_children(struct device_node *np)
829 return 0; 828 return 0;
830 } 829 }
831 830
832 /* Children of the root need to be remembered for removal */
833 if (np->parent == of_root) {
834 if (WARN_ON(last_node_index >= NO_OF_NODES))
835 return -EINVAL;
836 nodes[last_node_index++] = np;
837 }
838
839 child = np->child; 831 child = np->child;
840 np->child = NULL; 832 np->child = NULL;
841 np->sibling = NULL; 833
842 of_attach_node(np); 834 mutex_lock(&of_mutex);
835 raw_spin_lock_irqsave(&devtree_lock, flags);
836 np->sibling = np->parent->child;
837 np->parent->child = np;
838 of_node_clear_flag(np, OF_DETACHED);
839 raw_spin_unlock_irqrestore(&devtree_lock, flags);
840
841 __of_attach_node_sysfs(np);
842 mutex_unlock(&of_mutex);
843
843 while (child) { 844 while (child) {
844 next = child->sibling; 845 next = child->sibling;
845 attach_node_and_children(child); 846 attach_node_and_children(child);
@@ -889,10 +890,7 @@ static int __init selftest_data_add(void)
889 } 890 }
890 891
891 if (!of_root) { 892 if (!of_root) {
892 /* enabling flag for removing nodes */
893 selftest_live_tree = true;
894 of_root = selftest_data_node; 893 of_root = selftest_data_node;
895
896 for_each_of_allnodes(np) 894 for_each_of_allnodes(np)
897 __of_attach_node_sysfs(np); 895 __of_attach_node_sysfs(np);
898 of_aliases = of_find_node_by_path("/aliases"); 896 of_aliases = of_find_node_by_path("/aliases");
@@ -911,59 +909,6 @@ static int __init selftest_data_add(void)
911 return 0; 909 return 0;
912} 910}
913 911
914/**
915 * detach_node_and_children - detaches node
916 * and its children from live tree
917 *
918 * @np: Node to detach from live tree
919 */
920static void detach_node_and_children(struct device_node *np)
921{
922 while (np->child)
923 detach_node_and_children(np->child);
924 of_detach_node(np);
925}
926
927/**
928 * selftest_data_remove - removes the selftest data
929 * nodes from the live tree
930 */
931static void selftest_data_remove(void)
932{
933 struct device_node *np;
934 struct property *prop;
935
936 if (selftest_live_tree) {
937 of_node_put(of_aliases);
938 of_node_put(of_chosen);
939 of_aliases = NULL;
940 of_chosen = NULL;
941 for_each_child_of_node(of_root, np)
942 detach_node_and_children(np);
943 __of_detach_node_sysfs(of_root);
944 of_root = NULL;
945 return;
946 }
947
948 while (last_node_index-- > 0) {
949 if (nodes[last_node_index]) {
950 np = of_find_node_by_path(nodes[last_node_index]->full_name);
951 if (np == nodes[last_node_index]) {
952 if (of_aliases == np) {
953 of_node_put(of_aliases);
954 of_aliases = NULL;
955 }
956 detach_node_and_children(np);
957 } else {
958 for_each_property_of_node(np, prop) {
959 if (strcmp(prop->name, "testcase-alias") == 0)
960 of_remove_property(np, prop);
961 }
962 }
963 }
964 }
965}
966
967#ifdef CONFIG_OF_OVERLAY 912#ifdef CONFIG_OF_OVERLAY
968 913
969static int selftest_probe(struct platform_device *pdev) 914static int selftest_probe(struct platform_device *pdev)
@@ -1034,17 +979,94 @@ static int of_path_platform_device_exists(const char *path)
1034 return pdev != NULL; 979 return pdev != NULL;
1035} 980}
1036 981
1037static const char *selftest_path(int nr) 982#if IS_ENABLED(CONFIG_I2C)
983
984/* get the i2c client device instantiated at the path */
985static struct i2c_client *of_path_to_i2c_client(const char *path)
1038{ 986{
987 struct device_node *np;
988 struct i2c_client *client;
989
990 np = of_find_node_by_path(path);
991 if (np == NULL)
992 return NULL;
993
994 client = of_find_i2c_device_by_node(np);
995 of_node_put(np);
996
997 return client;
998}
999
1000/* find out if a i2c client device exists at that path */
1001static int of_path_i2c_client_exists(const char *path)
1002{
1003 struct i2c_client *client;
1004
1005 client = of_path_to_i2c_client(path);
1006 if (client)
1007 put_device(&client->dev);
1008 return client != NULL;
1009}
1010#else
1011static int of_path_i2c_client_exists(const char *path)
1012{
1013 return 0;
1014}
1015#endif
1016
1017enum overlay_type {
1018 PDEV_OVERLAY,
1019 I2C_OVERLAY
1020};
1021
1022static int of_path_device_type_exists(const char *path,
1023 enum overlay_type ovtype)
1024{
1025 switch (ovtype) {
1026 case PDEV_OVERLAY:
1027 return of_path_platform_device_exists(path);
1028 case I2C_OVERLAY:
1029 return of_path_i2c_client_exists(path);
1030 }
1031 return 0;
1032}
1033
1034static const char *selftest_path(int nr, enum overlay_type ovtype)
1035{
1036 const char *base;
1039 static char buf[256]; 1037 static char buf[256];
1040 1038
1041 snprintf(buf, sizeof(buf) - 1, 1039 switch (ovtype) {
1042 "/testcase-data/overlay-node/test-bus/test-selftest%d", nr); 1040 case PDEV_OVERLAY:
1041 base = "/testcase-data/overlay-node/test-bus";
1042 break;
1043 case I2C_OVERLAY:
1044 base = "/testcase-data/overlay-node/test-bus/i2c-test-bus";
1045 break;
1046 default:
1047 buf[0] = '\0';
1048 return buf;
1049 }
1050 snprintf(buf, sizeof(buf) - 1, "%s/test-selftest%d", base, nr);
1043 buf[sizeof(buf) - 1] = '\0'; 1051 buf[sizeof(buf) - 1] = '\0';
1044
1045 return buf; 1052 return buf;
1046} 1053}
1047 1054
1055static int of_selftest_device_exists(int selftest_nr, enum overlay_type ovtype)
1056{
1057 const char *path;
1058
1059 path = selftest_path(selftest_nr, ovtype);
1060
1061 switch (ovtype) {
1062 case PDEV_OVERLAY:
1063 return of_path_platform_device_exists(path);
1064 case I2C_OVERLAY:
1065 return of_path_i2c_client_exists(path);
1066 }
1067 return 0;
1068}
1069
1048static const char *overlay_path(int nr) 1070static const char *overlay_path(int nr)
1049{ 1071{
1050 static char buf[256]; 1072 static char buf[256];
@@ -1093,16 +1115,15 @@ out:
1093 1115
1094/* apply an overlay while checking before and after states */ 1116/* apply an overlay while checking before and after states */
1095static int of_selftest_apply_overlay_check(int overlay_nr, int selftest_nr, 1117static int of_selftest_apply_overlay_check(int overlay_nr, int selftest_nr,
1096 int before, int after) 1118 int before, int after, enum overlay_type ovtype)
1097{ 1119{
1098 int ret; 1120 int ret;
1099 1121
1100 /* selftest device must not be in before state */ 1122 /* selftest device must not be in before state */
1101 if (of_path_platform_device_exists(selftest_path(selftest_nr)) 1123 if (of_selftest_device_exists(selftest_nr, ovtype) != before) {
1102 != before) {
1103 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n", 1124 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1104 overlay_path(overlay_nr), 1125 overlay_path(overlay_nr),
1105 selftest_path(selftest_nr), 1126 selftest_path(selftest_nr, ovtype),
1106 !before ? "enabled" : "disabled"); 1127 !before ? "enabled" : "disabled");
1107 return -EINVAL; 1128 return -EINVAL;
1108 } 1129 }
@@ -1114,11 +1135,10 @@ static int of_selftest_apply_overlay_check(int overlay_nr, int selftest_nr,
1114 } 1135 }
1115 1136
1116 /* selftest device must be to set to after state */ 1137 /* selftest device must be to set to after state */
1117 if (of_path_platform_device_exists(selftest_path(selftest_nr)) 1138 if (of_selftest_device_exists(selftest_nr, ovtype) != after) {
1118 != after) {
1119 selftest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n", 1139 selftest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n",
1120 overlay_path(overlay_nr), 1140 overlay_path(overlay_nr),
1121 selftest_path(selftest_nr), 1141 selftest_path(selftest_nr, ovtype),
1122 !after ? "enabled" : "disabled"); 1142 !after ? "enabled" : "disabled");
1123 return -EINVAL; 1143 return -EINVAL;
1124 } 1144 }
@@ -1128,16 +1148,16 @@ static int of_selftest_apply_overlay_check(int overlay_nr, int selftest_nr,
1128 1148
1129/* apply an overlay and then revert it while checking before, after states */ 1149/* apply an overlay and then revert it while checking before, after states */
1130static int of_selftest_apply_revert_overlay_check(int overlay_nr, 1150static int of_selftest_apply_revert_overlay_check(int overlay_nr,
1131 int selftest_nr, int before, int after) 1151 int selftest_nr, int before, int after,
1152 enum overlay_type ovtype)
1132{ 1153{
1133 int ret, ov_id; 1154 int ret, ov_id;
1134 1155
1135 /* selftest device must be in before state */ 1156 /* selftest device must be in before state */
1136 if (of_path_platform_device_exists(selftest_path(selftest_nr)) 1157 if (of_selftest_device_exists(selftest_nr, ovtype) != before) {
1137 != before) {
1138 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n", 1158 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1139 overlay_path(overlay_nr), 1159 overlay_path(overlay_nr),
1140 selftest_path(selftest_nr), 1160 selftest_path(selftest_nr, ovtype),
1141 !before ? "enabled" : "disabled"); 1161 !before ? "enabled" : "disabled");
1142 return -EINVAL; 1162 return -EINVAL;
1143 } 1163 }
@@ -1150,11 +1170,10 @@ static int of_selftest_apply_revert_overlay_check(int overlay_nr,
1150 } 1170 }
1151 1171
1152 /* selftest device must be in after state */ 1172 /* selftest device must be in after state */
1153 if (of_path_platform_device_exists(selftest_path(selftest_nr)) 1173 if (of_selftest_device_exists(selftest_nr, ovtype) != after) {
1154 != after) {
1155 selftest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n", 1174 selftest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n",
1156 overlay_path(overlay_nr), 1175 overlay_path(overlay_nr),
1157 selftest_path(selftest_nr), 1176 selftest_path(selftest_nr, ovtype),
1158 !after ? "enabled" : "disabled"); 1177 !after ? "enabled" : "disabled");
1159 return -EINVAL; 1178 return -EINVAL;
1160 } 1179 }
@@ -1163,16 +1182,15 @@ static int of_selftest_apply_revert_overlay_check(int overlay_nr,
1163 if (ret != 0) { 1182 if (ret != 0) {
1164 selftest(0, "overlay @\"%s\" failed to be destroyed @\"%s\"\n", 1183 selftest(0, "overlay @\"%s\" failed to be destroyed @\"%s\"\n",
1165 overlay_path(overlay_nr), 1184 overlay_path(overlay_nr),
1166 selftest_path(selftest_nr)); 1185 selftest_path(selftest_nr, ovtype));
1167 return ret; 1186 return ret;
1168 } 1187 }
1169 1188
1170 /* selftest device must be again in before state */ 1189 /* selftest device must be again in before state */
1171 if (of_path_platform_device_exists(selftest_path(selftest_nr)) 1190 if (of_selftest_device_exists(selftest_nr, PDEV_OVERLAY) != before) {
1172 != before) {
1173 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n", 1191 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1174 overlay_path(overlay_nr), 1192 overlay_path(overlay_nr),
1175 selftest_path(selftest_nr), 1193 selftest_path(selftest_nr, ovtype),
1176 !before ? "enabled" : "disabled"); 1194 !before ? "enabled" : "disabled");
1177 return -EINVAL; 1195 return -EINVAL;
1178 } 1196 }
@@ -1186,7 +1204,7 @@ static void of_selftest_overlay_0(void)
1186 int ret; 1204 int ret;
1187 1205
1188 /* device should enable */ 1206 /* device should enable */
1189 ret = of_selftest_apply_overlay_check(0, 0, 0, 1); 1207 ret = of_selftest_apply_overlay_check(0, 0, 0, 1, PDEV_OVERLAY);
1190 if (ret != 0) 1208 if (ret != 0)
1191 return; 1209 return;
1192 1210
@@ -1199,7 +1217,7 @@ static void of_selftest_overlay_1(void)
1199 int ret; 1217 int ret;
1200 1218
1201 /* device should disable */ 1219 /* device should disable */
1202 ret = of_selftest_apply_overlay_check(1, 1, 1, 0); 1220 ret = of_selftest_apply_overlay_check(1, 1, 1, 0, PDEV_OVERLAY);
1203 if (ret != 0) 1221 if (ret != 0)
1204 return; 1222 return;
1205 1223
@@ -1212,7 +1230,7 @@ static void of_selftest_overlay_2(void)
1212 int ret; 1230 int ret;
1213 1231
1214 /* device should enable */ 1232 /* device should enable */
1215 ret = of_selftest_apply_overlay_check(2, 2, 0, 1); 1233 ret = of_selftest_apply_overlay_check(2, 2, 0, 1, PDEV_OVERLAY);
1216 if (ret != 0) 1234 if (ret != 0)
1217 return; 1235 return;
1218 1236
@@ -1225,7 +1243,7 @@ static void of_selftest_overlay_3(void)
1225 int ret; 1243 int ret;
1226 1244
1227 /* device should disable */ 1245 /* device should disable */
1228 ret = of_selftest_apply_overlay_check(3, 3, 1, 0); 1246 ret = of_selftest_apply_overlay_check(3, 3, 1, 0, PDEV_OVERLAY);
1229 if (ret != 0) 1247 if (ret != 0)
1230 return; 1248 return;
1231 1249
@@ -1238,7 +1256,7 @@ static void of_selftest_overlay_4(void)
1238 int ret; 1256 int ret;
1239 1257
1240 /* device should disable */ 1258 /* device should disable */
1241 ret = of_selftest_apply_overlay_check(4, 4, 0, 1); 1259 ret = of_selftest_apply_overlay_check(4, 4, 0, 1, PDEV_OVERLAY);
1242 if (ret != 0) 1260 if (ret != 0)
1243 return; 1261 return;
1244 1262
@@ -1251,7 +1269,7 @@ static void of_selftest_overlay_5(void)
1251 int ret; 1269 int ret;
1252 1270
1253 /* device should disable */ 1271 /* device should disable */
1254 ret = of_selftest_apply_revert_overlay_check(5, 5, 0, 1); 1272 ret = of_selftest_apply_revert_overlay_check(5, 5, 0, 1, PDEV_OVERLAY);
1255 if (ret != 0) 1273 if (ret != 0)
1256 return; 1274 return;
1257 1275
@@ -1268,12 +1286,12 @@ static void of_selftest_overlay_6(void)
1268 1286
1269 /* selftest device must be in before state */ 1287 /* selftest device must be in before state */
1270 for (i = 0; i < 2; i++) { 1288 for (i = 0; i < 2; i++) {
1271 if (of_path_platform_device_exists( 1289 if (of_selftest_device_exists(selftest_nr + i, PDEV_OVERLAY)
1272 selftest_path(selftest_nr + i))
1273 != before) { 1290 != before) {
1274 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n", 1291 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1275 overlay_path(overlay_nr + i), 1292 overlay_path(overlay_nr + i),
1276 selftest_path(selftest_nr + i), 1293 selftest_path(selftest_nr + i,
1294 PDEV_OVERLAY),
1277 !before ? "enabled" : "disabled"); 1295 !before ? "enabled" : "disabled");
1278 return; 1296 return;
1279 } 1297 }
@@ -1300,12 +1318,12 @@ static void of_selftest_overlay_6(void)
1300 1318
1301 for (i = 0; i < 2; i++) { 1319 for (i = 0; i < 2; i++) {
1302 /* selftest device must be in after state */ 1320 /* selftest device must be in after state */
1303 if (of_path_platform_device_exists( 1321 if (of_selftest_device_exists(selftest_nr + i, PDEV_OVERLAY)
1304 selftest_path(selftest_nr + i))
1305 != after) { 1322 != after) {
1306 selftest(0, "overlay @\"%s\" failed @\"%s\" %s\n", 1323 selftest(0, "overlay @\"%s\" failed @\"%s\" %s\n",
1307 overlay_path(overlay_nr + i), 1324 overlay_path(overlay_nr + i),
1308 selftest_path(selftest_nr + i), 1325 selftest_path(selftest_nr + i,
1326 PDEV_OVERLAY),
1309 !after ? "enabled" : "disabled"); 1327 !after ? "enabled" : "disabled");
1310 return; 1328 return;
1311 } 1329 }
@@ -1316,19 +1334,20 @@ static void of_selftest_overlay_6(void)
1316 if (ret != 0) { 1334 if (ret != 0) {
1317 selftest(0, "overlay @\"%s\" failed destroy @\"%s\"\n", 1335 selftest(0, "overlay @\"%s\" failed destroy @\"%s\"\n",
1318 overlay_path(overlay_nr + i), 1336 overlay_path(overlay_nr + i),
1319 selftest_path(selftest_nr + i)); 1337 selftest_path(selftest_nr + i,
1338 PDEV_OVERLAY));
1320 return; 1339 return;
1321 } 1340 }
1322 } 1341 }
1323 1342
1324 for (i = 0; i < 2; i++) { 1343 for (i = 0; i < 2; i++) {
1325 /* selftest device must be again in before state */ 1344 /* selftest device must be again in before state */
1326 if (of_path_platform_device_exists( 1345 if (of_selftest_device_exists(selftest_nr + i, PDEV_OVERLAY)
1327 selftest_path(selftest_nr + i))
1328 != before) { 1346 != before) {
1329 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n", 1347 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1330 overlay_path(overlay_nr + i), 1348 overlay_path(overlay_nr + i),
1331 selftest_path(selftest_nr + i), 1349 selftest_path(selftest_nr + i,
1350 PDEV_OVERLAY),
1332 !before ? "enabled" : "disabled"); 1351 !before ? "enabled" : "disabled");
1333 return; 1352 return;
1334 } 1353 }
@@ -1370,7 +1389,8 @@ static void of_selftest_overlay_8(void)
1370 if (ret == 0) { 1389 if (ret == 0) {
1371 selftest(0, "overlay @\"%s\" was destroyed @\"%s\"\n", 1390 selftest(0, "overlay @\"%s\" was destroyed @\"%s\"\n",
1372 overlay_path(overlay_nr + 0), 1391 overlay_path(overlay_nr + 0),
1373 selftest_path(selftest_nr)); 1392 selftest_path(selftest_nr,
1393 PDEV_OVERLAY));
1374 return; 1394 return;
1375 } 1395 }
1376 1396
@@ -1380,7 +1400,8 @@ static void of_selftest_overlay_8(void)
1380 if (ret != 0) { 1400 if (ret != 0) {
1381 selftest(0, "overlay @\"%s\" not destroyed @\"%s\"\n", 1401 selftest(0, "overlay @\"%s\" not destroyed @\"%s\"\n",
1382 overlay_path(overlay_nr + i), 1402 overlay_path(overlay_nr + i),
1383 selftest_path(selftest_nr)); 1403 selftest_path(selftest_nr,
1404 PDEV_OVERLAY));
1384 return; 1405 return;
1385 } 1406 }
1386 } 1407 }
@@ -1395,16 +1416,17 @@ static void of_selftest_overlay_10(void)
1395 char *child_path; 1416 char *child_path;
1396 1417
1397 /* device should disable */ 1418 /* device should disable */
1398 ret = of_selftest_apply_overlay_check(10, 10, 0, 1); 1419 ret = of_selftest_apply_overlay_check(10, 10, 0, 1, PDEV_OVERLAY);
1399 if (selftest(ret == 0, "overlay test %d failed; overlay application\n", 10)) 1420 if (selftest(ret == 0,
1421 "overlay test %d failed; overlay application\n", 10))
1400 return; 1422 return;
1401 1423
1402 child_path = kasprintf(GFP_KERNEL, "%s/test-selftest101", 1424 child_path = kasprintf(GFP_KERNEL, "%s/test-selftest101",
1403 selftest_path(10)); 1425 selftest_path(10, PDEV_OVERLAY));
1404 if (selftest(child_path, "overlay test %d failed; kasprintf\n", 10)) 1426 if (selftest(child_path, "overlay test %d failed; kasprintf\n", 10))
1405 return; 1427 return;
1406 1428
1407 ret = of_path_platform_device_exists(child_path); 1429 ret = of_path_device_type_exists(child_path, PDEV_OVERLAY);
1408 kfree(child_path); 1430 kfree(child_path);
1409 if (selftest(ret, "overlay test %d failed; no child device\n", 10)) 1431 if (selftest(ret, "overlay test %d failed; no child device\n", 10))
1410 return; 1432 return;
@@ -1416,11 +1438,331 @@ static void of_selftest_overlay_11(void)
1416 int ret; 1438 int ret;
1417 1439
1418 /* device should disable */ 1440 /* device should disable */
1419 ret = of_selftest_apply_revert_overlay_check(11, 11, 0, 1); 1441 ret = of_selftest_apply_revert_overlay_check(11, 11, 0, 1,
1420 if (selftest(ret == 0, "overlay test %d failed; overlay application\n", 11)) 1442 PDEV_OVERLAY);
1443 if (selftest(ret == 0,
1444 "overlay test %d failed; overlay application\n", 11))
1421 return; 1445 return;
1422} 1446}
1423 1447
1448#if IS_ENABLED(CONFIG_I2C) && IS_ENABLED(CONFIG_OF_OVERLAY)
1449
1450struct selftest_i2c_bus_data {
1451 struct platform_device *pdev;
1452 struct i2c_adapter adap;
1453};
1454
1455static int selftest_i2c_master_xfer(struct i2c_adapter *adap,
1456 struct i2c_msg *msgs, int num)
1457{
1458 struct selftest_i2c_bus_data *std = i2c_get_adapdata(adap);
1459
1460 (void)std;
1461
1462 return num;
1463}
1464
1465static u32 selftest_i2c_functionality(struct i2c_adapter *adap)
1466{
1467 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
1468}
1469
1470static const struct i2c_algorithm selftest_i2c_algo = {
1471 .master_xfer = selftest_i2c_master_xfer,
1472 .functionality = selftest_i2c_functionality,
1473};
1474
1475static int selftest_i2c_bus_probe(struct platform_device *pdev)
1476{
1477 struct device *dev = &pdev->dev;
1478 struct device_node *np = dev->of_node;
1479 struct selftest_i2c_bus_data *std;
1480 struct i2c_adapter *adap;
1481 int ret;
1482
1483 if (np == NULL) {
1484 dev_err(dev, "No OF data for device\n");
1485 return -EINVAL;
1486
1487 }
1488
1489 dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
1490
1491 std = devm_kzalloc(dev, sizeof(*std), GFP_KERNEL);
1492 if (!std) {
1493 dev_err(dev, "Failed to allocate selftest i2c data\n");
1494 return -ENOMEM;
1495 }
1496
1497 /* link them together */
1498 std->pdev = pdev;
1499 platform_set_drvdata(pdev, std);
1500
1501 adap = &std->adap;
1502 i2c_set_adapdata(adap, std);
1503 adap->nr = -1;
1504 strlcpy(adap->name, pdev->name, sizeof(adap->name));
1505 adap->class = I2C_CLASS_DEPRECATED;
1506 adap->algo = &selftest_i2c_algo;
1507 adap->dev.parent = dev;
1508 adap->dev.of_node = dev->of_node;
1509 adap->timeout = 5 * HZ;
1510 adap->retries = 3;
1511
1512 ret = i2c_add_numbered_adapter(adap);
1513 if (ret != 0) {
1514 dev_err(dev, "Failed to add I2C adapter\n");
1515 return ret;
1516 }
1517
1518 return 0;
1519}
1520
1521static int selftest_i2c_bus_remove(struct platform_device *pdev)
1522{
1523 struct device *dev = &pdev->dev;
1524 struct device_node *np = dev->of_node;
1525 struct selftest_i2c_bus_data *std = platform_get_drvdata(pdev);
1526
1527 dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
1528 i2c_del_adapter(&std->adap);
1529
1530 return 0;
1531}
1532
1533static struct of_device_id selftest_i2c_bus_match[] = {
1534 { .compatible = "selftest-i2c-bus", },
1535 {},
1536};
1537
1538static struct platform_driver selftest_i2c_bus_driver = {
1539 .probe = selftest_i2c_bus_probe,
1540 .remove = selftest_i2c_bus_remove,
1541 .driver = {
1542 .name = "selftest-i2c-bus",
1543 .of_match_table = of_match_ptr(selftest_i2c_bus_match),
1544 },
1545};
1546
1547static int selftest_i2c_dev_probe(struct i2c_client *client,
1548 const struct i2c_device_id *id)
1549{
1550 struct device *dev = &client->dev;
1551 struct device_node *np = client->dev.of_node;
1552
1553 if (!np) {
1554 dev_err(dev, "No OF node\n");
1555 return -EINVAL;
1556 }
1557
1558 dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
1559
1560 return 0;
1561};
1562
1563static int selftest_i2c_dev_remove(struct i2c_client *client)
1564{
1565 struct device *dev = &client->dev;
1566 struct device_node *np = client->dev.of_node;
1567
1568 dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
1569 return 0;
1570}
1571
1572static const struct i2c_device_id selftest_i2c_dev_id[] = {
1573 { .name = "selftest-i2c-dev" },
1574 { }
1575};
1576
1577static struct i2c_driver selftest_i2c_dev_driver = {
1578 .driver = {
1579 .name = "selftest-i2c-dev",
1580 .owner = THIS_MODULE,
1581 },
1582 .probe = selftest_i2c_dev_probe,
1583 .remove = selftest_i2c_dev_remove,
1584 .id_table = selftest_i2c_dev_id,
1585};
1586
1587#if IS_ENABLED(CONFIG_I2C_MUX)
1588
1589struct selftest_i2c_mux_data {
1590 int nchans;
1591 struct i2c_adapter *adap[];
1592};
1593
1594static int selftest_i2c_mux_select_chan(struct i2c_adapter *adap,
1595 void *client, u32 chan)
1596{
1597 return 0;
1598}
1599
1600static int selftest_i2c_mux_probe(struct i2c_client *client,
1601 const struct i2c_device_id *id)
1602{
1603 int ret, i, nchans, size;
1604 struct device *dev = &client->dev;
1605 struct i2c_adapter *adap = to_i2c_adapter(dev->parent);
1606 struct device_node *np = client->dev.of_node, *child;
1607 struct selftest_i2c_mux_data *stm;
1608 u32 reg, max_reg;
1609
1610 dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
1611
1612 if (!np) {
1613 dev_err(dev, "No OF node\n");
1614 return -EINVAL;
1615 }
1616
1617 max_reg = (u32)-1;
1618 for_each_child_of_node(np, child) {
1619 ret = of_property_read_u32(child, "reg", &reg);
1620 if (ret)
1621 continue;
1622 if (max_reg == (u32)-1 || reg > max_reg)
1623 max_reg = reg;
1624 }
1625 nchans = max_reg == (u32)-1 ? 0 : max_reg + 1;
1626 if (nchans == 0) {
1627 dev_err(dev, "No channels\n");
1628 return -EINVAL;
1629 }
1630
1631 size = offsetof(struct selftest_i2c_mux_data, adap[nchans]);
1632 stm = devm_kzalloc(dev, size, GFP_KERNEL);
1633 if (!stm) {
1634 dev_err(dev, "Out of memory\n");
1635 return -ENOMEM;
1636 }
1637 stm->nchans = nchans;
1638 for (i = 0; i < nchans; i++) {
1639 stm->adap[i] = i2c_add_mux_adapter(adap, dev, client,
1640 0, i, 0, selftest_i2c_mux_select_chan, NULL);
1641 if (!stm->adap[i]) {
1642 dev_err(dev, "Failed to register mux #%d\n", i);
1643 for (i--; i >= 0; i--)
1644 i2c_del_mux_adapter(stm->adap[i]);
1645 return -ENODEV;
1646 }
1647 }
1648
1649 i2c_set_clientdata(client, stm);
1650
1651 return 0;
1652};
1653
1654static int selftest_i2c_mux_remove(struct i2c_client *client)
1655{
1656 struct device *dev = &client->dev;
1657 struct device_node *np = client->dev.of_node;
1658 struct selftest_i2c_mux_data *stm = i2c_get_clientdata(client);
1659 int i;
1660
1661 dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
1662 for (i = stm->nchans - 1; i >= 0; i--)
1663 i2c_del_mux_adapter(stm->adap[i]);
1664 return 0;
1665}
1666
1667static const struct i2c_device_id selftest_i2c_mux_id[] = {
1668 { .name = "selftest-i2c-mux" },
1669 { }
1670};
1671
1672static struct i2c_driver selftest_i2c_mux_driver = {
1673 .driver = {
1674 .name = "selftest-i2c-mux",
1675 .owner = THIS_MODULE,
1676 },
1677 .probe = selftest_i2c_mux_probe,
1678 .remove = selftest_i2c_mux_remove,
1679 .id_table = selftest_i2c_mux_id,
1680};
1681
1682#endif
1683
1684static int of_selftest_overlay_i2c_init(void)
1685{
1686 int ret;
1687
1688 ret = i2c_add_driver(&selftest_i2c_dev_driver);
1689 if (selftest(ret == 0,
1690 "could not register selftest i2c device driver\n"))
1691 return ret;
1692
1693 ret = platform_driver_register(&selftest_i2c_bus_driver);
1694 if (selftest(ret == 0,
1695 "could not register selftest i2c bus driver\n"))
1696 return ret;
1697
1698#if IS_ENABLED(CONFIG_I2C_MUX)
1699 ret = i2c_add_driver(&selftest_i2c_mux_driver);
1700 if (selftest(ret == 0,
1701 "could not register selftest i2c mux driver\n"))
1702 return ret;
1703#endif
1704
1705 return 0;
1706}
1707
1708static void of_selftest_overlay_i2c_cleanup(void)
1709{
1710#if IS_ENABLED(CONFIG_I2C_MUX)
1711 i2c_del_driver(&selftest_i2c_mux_driver);
1712#endif
1713 platform_driver_unregister(&selftest_i2c_bus_driver);
1714 i2c_del_driver(&selftest_i2c_dev_driver);
1715}
1716
1717static void of_selftest_overlay_i2c_12(void)
1718{
1719 int ret;
1720
1721 /* device should enable */
1722 ret = of_selftest_apply_overlay_check(12, 12, 0, 1, I2C_OVERLAY);
1723 if (ret != 0)
1724 return;
1725
1726 selftest(1, "overlay test %d passed\n", 12);
1727}
1728
1729/* test deactivation of device */
1730static void of_selftest_overlay_i2c_13(void)
1731{
1732 int ret;
1733
1734 /* device should disable */
1735 ret = of_selftest_apply_overlay_check(13, 13, 1, 0, I2C_OVERLAY);
1736 if (ret != 0)
1737 return;
1738
1739 selftest(1, "overlay test %d passed\n", 13);
1740}
1741
1742/* just check for i2c mux existence */
1743static void of_selftest_overlay_i2c_14(void)
1744{
1745}
1746
1747static void of_selftest_overlay_i2c_15(void)
1748{
1749 int ret;
1750
1751 /* device should enable */
1752 ret = of_selftest_apply_overlay_check(16, 15, 0, 1, I2C_OVERLAY);
1753 if (ret != 0)
1754 return;
1755
1756 selftest(1, "overlay test %d passed\n", 15);
1757}
1758
1759#else
1760
1761static inline void of_selftest_overlay_i2c_14(void) { }
1762static inline void of_selftest_overlay_i2c_15(void) { }
1763
1764#endif
1765
1424static void __init of_selftest_overlay(void) 1766static void __init of_selftest_overlay(void)
1425{ 1767{
1426 struct device_node *bus_np = NULL; 1768 struct device_node *bus_np = NULL;
@@ -1445,15 +1787,15 @@ static void __init of_selftest_overlay(void)
1445 goto out; 1787 goto out;
1446 } 1788 }
1447 1789
1448 if (!of_path_platform_device_exists(selftest_path(100))) { 1790 if (!of_selftest_device_exists(100, PDEV_OVERLAY)) {
1449 selftest(0, "could not find selftest0 @ \"%s\"\n", 1791 selftest(0, "could not find selftest0 @ \"%s\"\n",
1450 selftest_path(100)); 1792 selftest_path(100, PDEV_OVERLAY));
1451 goto out; 1793 goto out;
1452 } 1794 }
1453 1795
1454 if (of_path_platform_device_exists(selftest_path(101))) { 1796 if (of_selftest_device_exists(101, PDEV_OVERLAY)) {
1455 selftest(0, "selftest1 @ \"%s\" should not exist\n", 1797 selftest(0, "selftest1 @ \"%s\" should not exist\n",
1456 selftest_path(101)); 1798 selftest_path(101, PDEV_OVERLAY));
1457 goto out; 1799 goto out;
1458 } 1800 }
1459 1801
@@ -1472,6 +1814,18 @@ static void __init of_selftest_overlay(void)
1472 of_selftest_overlay_10(); 1814 of_selftest_overlay_10();
1473 of_selftest_overlay_11(); 1815 of_selftest_overlay_11();
1474 1816
1817#if IS_ENABLED(CONFIG_I2C)
1818 if (selftest(of_selftest_overlay_i2c_init() == 0, "i2c init failed\n"))
1819 goto out;
1820
1821 of_selftest_overlay_i2c_12();
1822 of_selftest_overlay_i2c_13();
1823 of_selftest_overlay_i2c_14();
1824 of_selftest_overlay_i2c_15();
1825
1826 of_selftest_overlay_i2c_cleanup();
1827#endif
1828
1475out: 1829out:
1476 of_node_put(bus_np); 1830 of_node_put(bus_np);
1477} 1831}
@@ -1514,9 +1868,6 @@ static int __init of_selftest(void)
1514 of_selftest_platform_populate(); 1868 of_selftest_platform_populate();
1515 of_selftest_overlay(); 1869 of_selftest_overlay();
1516 1870
1517 /* removing selftest data from live tree */
1518 selftest_data_remove();
1519
1520 /* Double check linkage after removing testcase data */ 1871 /* Double check linkage after removing testcase data */
1521 of_selftest_check_tree_linkage(); 1872 of_selftest_check_tree_linkage();
1522 1873