blob: 04e39a183e5399c3124ea94ecf2719c12aeb1a8f [file] [log] [blame]
Grant Likely53a42092011-12-12 09:25:57 -07001/*
2 * Self tests for device tree subsystem
3 */
4
Grant Likelycabb7d52013-02-12 21:19:37 +00005#define pr_fmt(fmt) "### dt-test ### " fmt
Grant Likely53a42092011-12-12 09:25:57 -07006
7#include <linux/clk.h>
8#include <linux/err.h>
9#include <linux/errno.h>
10#include <linux/module.h>
11#include <linux/of.h>
Grant Likelya9f10ca2013-10-11 22:04:23 +010012#include <linux/of_irq.h>
Rob Herring82c0f582014-04-23 17:57:40 -050013#include <linux/of_platform.h>
Grant Likely53a42092011-12-12 09:25:57 -070014#include <linux/list.h>
15#include <linux/mutex.h>
16#include <linux/slab.h>
17#include <linux/device.h>
18
Pantelis Antoniou69843392014-07-04 19:58:47 +030019#include "of_private.h"
20
Grant Likelya9f10ca2013-10-11 22:04:23 +010021static struct selftest_results {
22 int passed;
23 int failed;
24} selftest_results;
25
Grant Likely53a42092011-12-12 09:25:57 -070026#define selftest(result, fmt, ...) { \
Grant Likelycabb7d52013-02-12 21:19:37 +000027 if (!(result)) { \
Grant Likelya9f10ca2013-10-11 22:04:23 +010028 selftest_results.failed++; \
29 pr_err("FAIL %s():%i " fmt, __func__, __LINE__, ##__VA_ARGS__); \
Grant Likelycabb7d52013-02-12 21:19:37 +000030 } else { \
Grant Likelya9f10ca2013-10-11 22:04:23 +010031 selftest_results.passed++; \
32 pr_debug("pass %s():%i\n", __func__, __LINE__); \
Grant Likelycabb7d52013-02-12 21:19:37 +000033 } \
Grant Likely53a42092011-12-12 09:25:57 -070034}
35
Grant Likelyae91ff72014-03-14 13:53:10 +000036static void __init of_selftest_find_node_by_name(void)
37{
38 struct device_node *np;
39
40 np = of_find_node_by_path("/testcase-data");
41 selftest(np && !strcmp("/testcase-data", np->full_name),
42 "find /testcase-data failed\n");
43 of_node_put(np);
44
45 /* Test if trailing '/' works */
46 np = of_find_node_by_path("/testcase-data/");
47 selftest(!np, "trailing '/' on /testcase-data/ should fail\n");
48
49 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
50 selftest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", np->full_name),
51 "find /testcase-data/phandle-tests/consumer-a failed\n");
52 of_node_put(np);
53
54 np = of_find_node_by_path("testcase-alias");
55 selftest(np && !strcmp("/testcase-data", np->full_name),
56 "find testcase-alias failed\n");
57 of_node_put(np);
58
59 /* Test if trailing '/' works on aliases */
60 np = of_find_node_by_path("testcase-alias/");
61 selftest(!np, "trailing '/' on testcase-alias/ should fail\n");
62
63 np = of_find_node_by_path("testcase-alias/phandle-tests/consumer-a");
64 selftest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", np->full_name),
65 "find testcase-alias/phandle-tests/consumer-a failed\n");
66 of_node_put(np);
67
68 np = of_find_node_by_path("/testcase-data/missing-path");
69 selftest(!np, "non-existent path returned node %s\n", np->full_name);
70 of_node_put(np);
71
72 np = of_find_node_by_path("missing-alias");
73 selftest(!np, "non-existent alias returned node %s\n", np->full_name);
74 of_node_put(np);
75
76 np = of_find_node_by_path("testcase-alias/missing-path");
77 selftest(!np, "non-existent alias with relative path returned node %s\n", np->full_name);
78 of_node_put(np);
79}
80
Grant Likely7e66c5c2013-11-15 17:19:09 +000081static void __init of_selftest_dynamic(void)
82{
83 struct device_node *np;
84 struct property *prop;
85
86 np = of_find_node_by_path("/testcase-data");
87 if (!np) {
88 pr_err("missing testcase data\n");
89 return;
90 }
91
92 /* Array of 4 properties for the purpose of testing */
93 prop = kzalloc(sizeof(*prop) * 4, GFP_KERNEL);
94 if (!prop) {
95 selftest(0, "kzalloc() failed\n");
96 return;
97 }
98
99 /* Add a new property - should pass*/
100 prop->name = "new-property";
101 prop->value = "new-property-data";
102 prop->length = strlen(prop->value);
103 selftest(of_add_property(np, prop) == 0, "Adding a new property failed\n");
104
105 /* Try to add an existing property - should fail */
106 prop++;
107 prop->name = "new-property";
108 prop->value = "new-property-data-should-fail";
109 prop->length = strlen(prop->value);
110 selftest(of_add_property(np, prop) != 0,
111 "Adding an existing property should have failed\n");
112
113 /* Try to modify an existing property - should pass */
114 prop->value = "modify-property-data-should-pass";
115 prop->length = strlen(prop->value);
116 selftest(of_update_property(np, prop) == 0,
117 "Updating an existing property should have passed\n");
118
119 /* Try to modify non-existent property - should pass*/
120 prop++;
121 prop->name = "modify-property";
122 prop->value = "modify-missing-property-data-should-pass";
123 prop->length = strlen(prop->value);
124 selftest(of_update_property(np, prop) == 0,
125 "Updating a missing property should have passed\n");
126
127 /* Remove property - should pass */
128 selftest(of_remove_property(np, prop) == 0,
129 "Removing a property should have passed\n");
130
131 /* Adding very large property - should pass */
132 prop++;
133 prop->name = "large-property-PAGE_SIZEx8";
134 prop->length = PAGE_SIZE * 8;
135 prop->value = kzalloc(prop->length, GFP_KERNEL);
136 selftest(prop->value != NULL, "Unable to allocate large buffer\n");
137 if (prop->value)
138 selftest(of_add_property(np, prop) == 0,
139 "Adding a large property should have passed\n");
140}
141
Grant Likely53a42092011-12-12 09:25:57 -0700142static void __init of_selftest_parse_phandle_with_args(void)
143{
144 struct device_node *np;
145 struct of_phandle_args args;
Grant Likelycabb7d52013-02-12 21:19:37 +0000146 int i, rc;
Grant Likely53a42092011-12-12 09:25:57 -0700147
Grant Likely53a42092011-12-12 09:25:57 -0700148 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
149 if (!np) {
150 pr_err("missing testcase data\n");
151 return;
152 }
153
Grant Likelybd69f732013-02-10 22:57:21 +0000154 rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
155 selftest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc);
156
Grant Likelyf7f951c2013-02-12 17:41:22 +0000157 for (i = 0; i < 8; i++) {
Grant Likely53a42092011-12-12 09:25:57 -0700158 bool passed = true;
159 rc = of_parse_phandle_with_args(np, "phandle-list",
160 "#phandle-cells", i, &args);
161
162 /* Test the values from tests-phandle.dtsi */
163 switch (i) {
164 case 0:
165 passed &= !rc;
166 passed &= (args.args_count == 1);
167 passed &= (args.args[0] == (i + 1));
168 break;
169 case 1:
170 passed &= !rc;
171 passed &= (args.args_count == 2);
172 passed &= (args.args[0] == (i + 1));
173 passed &= (args.args[1] == 0);
174 break;
175 case 2:
176 passed &= (rc == -ENOENT);
177 break;
178 case 3:
179 passed &= !rc;
180 passed &= (args.args_count == 3);
181 passed &= (args.args[0] == (i + 1));
182 passed &= (args.args[1] == 4);
183 passed &= (args.args[2] == 3);
184 break;
185 case 4:
186 passed &= !rc;
187 passed &= (args.args_count == 2);
188 passed &= (args.args[0] == (i + 1));
189 passed &= (args.args[1] == 100);
190 break;
191 case 5:
192 passed &= !rc;
193 passed &= (args.args_count == 0);
194 break;
195 case 6:
196 passed &= !rc;
197 passed &= (args.args_count == 1);
198 passed &= (args.args[0] == (i + 1));
199 break;
200 case 7:
Grant Likelycabb7d52013-02-12 21:19:37 +0000201 passed &= (rc == -ENOENT);
Grant Likely53a42092011-12-12 09:25:57 -0700202 break;
203 default:
204 passed = false;
205 }
206
Grant Likelycabb7d52013-02-12 21:19:37 +0000207 selftest(passed, "index %i - data error on node %s rc=%i\n",
208 i, args.np->full_name, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700209 }
210
211 /* Check for missing list property */
212 rc = of_parse_phandle_with_args(np, "phandle-list-missing",
213 "#phandle-cells", 0, &args);
Grant Likelycabb7d52013-02-12 21:19:37 +0000214 selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000215 rc = of_count_phandle_with_args(np, "phandle-list-missing",
216 "#phandle-cells");
217 selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700218
219 /* Check for missing cells property */
220 rc = of_parse_phandle_with_args(np, "phandle-list",
221 "#phandle-cells-missing", 0, &args);
Grant Likelycabb7d52013-02-12 21:19:37 +0000222 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000223 rc = of_count_phandle_with_args(np, "phandle-list",
224 "#phandle-cells-missing");
225 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700226
227 /* Check for bad phandle in list */
228 rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle",
229 "#phandle-cells", 0, &args);
Grant Likelycabb7d52013-02-12 21:19:37 +0000230 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000231 rc = of_count_phandle_with_args(np, "phandle-list-bad-phandle",
232 "#phandle-cells");
233 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700234
235 /* Check for incorrectly formed argument list */
236 rc = of_parse_phandle_with_args(np, "phandle-list-bad-args",
237 "#phandle-cells", 1, &args);
Grant Likelycabb7d52013-02-12 21:19:37 +0000238 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000239 rc = of_count_phandle_with_args(np, "phandle-list-bad-args",
240 "#phandle-cells");
241 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700242}
243
Grant Likely7aff0fe2011-12-12 09:25:58 -0700244static void __init of_selftest_property_match_string(void)
245{
246 struct device_node *np;
247 int rc;
248
Grant Likely7aff0fe2011-12-12 09:25:58 -0700249 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
250 if (!np) {
251 pr_err("No testcase data in device tree\n");
252 return;
253 }
254
255 rc = of_property_match_string(np, "phandle-list-names", "first");
256 selftest(rc == 0, "first expected:0 got:%i\n", rc);
257 rc = of_property_match_string(np, "phandle-list-names", "second");
258 selftest(rc == 1, "second expected:0 got:%i\n", rc);
259 rc = of_property_match_string(np, "phandle-list-names", "third");
260 selftest(rc == 2, "third expected:0 got:%i\n", rc);
261 rc = of_property_match_string(np, "phandle-list-names", "fourth");
262 selftest(rc == -ENODATA, "unmatched string; rc=%i", rc);
263 rc = of_property_match_string(np, "missing-property", "blah");
264 selftest(rc == -EINVAL, "missing property; rc=%i", rc);
265 rc = of_property_match_string(np, "empty-property", "blah");
266 selftest(rc == -ENODATA, "empty property; rc=%i", rc);
267 rc = of_property_match_string(np, "unterminated-string", "blah");
268 selftest(rc == -EILSEQ, "unterminated string; rc=%i", rc);
269}
270
Pantelis Antoniou69843392014-07-04 19:58:47 +0300271#define propcmp(p1, p2) (((p1)->length == (p2)->length) && \
272 (p1)->value && (p2)->value && \
273 !memcmp((p1)->value, (p2)->value, (p1)->length) && \
274 !strcmp((p1)->name, (p2)->name))
275static void __init of_selftest_property_copy(void)
276{
277#ifdef CONFIG_OF_DYNAMIC
278 struct property p1 = { .name = "p1", .length = 0, .value = "" };
279 struct property p2 = { .name = "p2", .length = 5, .value = "abcd" };
280 struct property *new;
281
282 new = __of_prop_dup(&p1, GFP_KERNEL);
283 selftest(new && propcmp(&p1, new), "empty property didn't copy correctly\n");
284 kfree(new->value);
285 kfree(new->name);
286 kfree(new);
287
288 new = __of_prop_dup(&p2, GFP_KERNEL);
289 selftest(new && propcmp(&p2, new), "non-empty property didn't copy correctly\n");
290 kfree(new->value);
291 kfree(new->name);
292 kfree(new);
293#endif
294}
295
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300296static void __init of_selftest_changeset(void)
297{
298#ifdef CONFIG_OF_DYNAMIC
299 struct property *ppadd, padd = { .name = "prop-add", .length = 0, .value = "" };
300 struct property *ppupdate, pupdate = { .name = "prop-update", .length = 5, .value = "abcd" };
301 struct property *ppremove;
302 struct device_node *n1, *n2, *n21, *nremove, *parent;
303 struct of_changeset chgset;
304
305 of_changeset_init(&chgset);
306 n1 = __of_node_alloc("/testcase-data/changeset/n1", GFP_KERNEL);
307 selftest(n1, "testcase setup failure\n");
308 n2 = __of_node_alloc("/testcase-data/changeset/n2", GFP_KERNEL);
309 selftest(n2, "testcase setup failure\n");
310 n21 = __of_node_alloc("/testcase-data/changeset/n2/n21", GFP_KERNEL);
311 selftest(n21, "testcase setup failure %p\n", n21);
312 nremove = of_find_node_by_path("/testcase-data/changeset/node-remove");
313 selftest(nremove, "testcase setup failure\n");
314 ppadd = __of_prop_dup(&padd, GFP_KERNEL);
315 selftest(ppadd, "testcase setup failure\n");
316 ppupdate = __of_prop_dup(&pupdate, GFP_KERNEL);
317 selftest(ppupdate, "testcase setup failure\n");
318 parent = nremove->parent;
319 n1->parent = parent;
320 n2->parent = parent;
321 n21->parent = n2;
322 n2->child = n21;
323 ppremove = of_find_property(parent, "prop-remove", NULL);
324 selftest(ppremove, "failed to find removal prop");
325
326 of_changeset_init(&chgset);
327 selftest(!of_changeset_attach_node(&chgset, n1), "fail attach n1\n");
328 selftest(!of_changeset_attach_node(&chgset, n2), "fail attach n2\n");
329 selftest(!of_changeset_detach_node(&chgset, nremove), "fail remove node\n");
330 selftest(!of_changeset_attach_node(&chgset, n21), "fail attach n21\n");
331 selftest(!of_changeset_add_property(&chgset, parent, ppadd), "fail add prop\n");
332 selftest(!of_changeset_update_property(&chgset, parent, ppupdate), "fail update prop\n");
333 selftest(!of_changeset_remove_property(&chgset, parent, ppremove), "fail remove prop\n");
334 mutex_lock(&of_mutex);
335 selftest(!of_changeset_apply(&chgset), "apply failed\n");
336 mutex_unlock(&of_mutex);
337
338 mutex_lock(&of_mutex);
339 selftest(!of_changeset_revert(&chgset), "revert failed\n");
340 mutex_unlock(&of_mutex);
341
342 of_changeset_destroy(&chgset);
343#endif
344}
345
Grant Likelya9f10ca2013-10-11 22:04:23 +0100346static void __init of_selftest_parse_interrupts(void)
347{
348 struct device_node *np;
349 struct of_phandle_args args;
350 int i, rc;
351
352 np = of_find_node_by_path("/testcase-data/interrupts/interrupts0");
353 if (!np) {
354 pr_err("missing testcase data\n");
355 return;
356 }
357
358 for (i = 0; i < 4; i++) {
359 bool passed = true;
360 args.args_count = 0;
361 rc = of_irq_parse_one(np, i, &args);
362
363 passed &= !rc;
364 passed &= (args.args_count == 1);
365 passed &= (args.args[0] == (i + 1));
366
367 selftest(passed, "index %i - data error on node %s rc=%i\n",
368 i, args.np->full_name, rc);
369 }
370 of_node_put(np);
371
372 np = of_find_node_by_path("/testcase-data/interrupts/interrupts1");
373 if (!np) {
374 pr_err("missing testcase data\n");
375 return;
376 }
377
378 for (i = 0; i < 4; i++) {
379 bool passed = true;
380 args.args_count = 0;
381 rc = of_irq_parse_one(np, i, &args);
382
383 /* Test the values from tests-phandle.dtsi */
384 switch (i) {
385 case 0:
386 passed &= !rc;
387 passed &= (args.args_count == 1);
388 passed &= (args.args[0] == 9);
389 break;
390 case 1:
391 passed &= !rc;
392 passed &= (args.args_count == 3);
393 passed &= (args.args[0] == 10);
394 passed &= (args.args[1] == 11);
395 passed &= (args.args[2] == 12);
396 break;
397 case 2:
398 passed &= !rc;
399 passed &= (args.args_count == 2);
400 passed &= (args.args[0] == 13);
401 passed &= (args.args[1] == 14);
402 break;
403 case 3:
404 passed &= !rc;
405 passed &= (args.args_count == 2);
406 passed &= (args.args[0] == 15);
407 passed &= (args.args[1] == 16);
408 break;
409 default:
410 passed = false;
411 }
412 selftest(passed, "index %i - data error on node %s rc=%i\n",
413 i, args.np->full_name, rc);
414 }
415 of_node_put(np);
416}
417
Grant Likely79d97012013-09-19 16:47:37 -0500418static void __init of_selftest_parse_interrupts_extended(void)
419{
420 struct device_node *np;
421 struct of_phandle_args args;
422 int i, rc;
423
424 np = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
425 if (!np) {
426 pr_err("missing testcase data\n");
427 return;
428 }
429
430 for (i = 0; i < 7; i++) {
431 bool passed = true;
432 rc = of_irq_parse_one(np, i, &args);
433
434 /* Test the values from tests-phandle.dtsi */
435 switch (i) {
436 case 0:
437 passed &= !rc;
438 passed &= (args.args_count == 1);
439 passed &= (args.args[0] == 1);
440 break;
441 case 1:
442 passed &= !rc;
443 passed &= (args.args_count == 3);
444 passed &= (args.args[0] == 2);
445 passed &= (args.args[1] == 3);
446 passed &= (args.args[2] == 4);
447 break;
448 case 2:
449 passed &= !rc;
450 passed &= (args.args_count == 2);
451 passed &= (args.args[0] == 5);
452 passed &= (args.args[1] == 6);
453 break;
454 case 3:
455 passed &= !rc;
456 passed &= (args.args_count == 1);
457 passed &= (args.args[0] == 9);
458 break;
459 case 4:
460 passed &= !rc;
461 passed &= (args.args_count == 3);
462 passed &= (args.args[0] == 10);
463 passed &= (args.args[1] == 11);
464 passed &= (args.args[2] == 12);
465 break;
466 case 5:
467 passed &= !rc;
468 passed &= (args.args_count == 2);
469 passed &= (args.args[0] == 13);
470 passed &= (args.args[1] == 14);
471 break;
472 case 6:
473 passed &= !rc;
474 passed &= (args.args_count == 1);
475 passed &= (args.args[0] == 15);
476 break;
477 default:
478 passed = false;
479 }
480
481 selftest(passed, "index %i - data error on node %s rc=%i\n",
482 i, args.np->full_name, rc);
483 }
484 of_node_put(np);
485}
486
Grant Likely1f42e5d2014-02-18 21:38:55 +0000487static struct of_device_id match_node_table[] = {
488 { .data = "A", .name = "name0", }, /* Name alone is lowest priority */
489 { .data = "B", .type = "type1", }, /* followed by type alone */
490
491 { .data = "Ca", .name = "name2", .type = "type1", }, /* followed by both together */
492 { .data = "Cb", .name = "name2", }, /* Only match when type doesn't match */
493 { .data = "Cc", .name = "name2", .type = "type2", },
494
495 { .data = "E", .compatible = "compat3" },
496 { .data = "G", .compatible = "compat2", },
497 { .data = "H", .compatible = "compat2", .name = "name5", },
498 { .data = "I", .compatible = "compat2", .type = "type1", },
499 { .data = "J", .compatible = "compat2", .type = "type1", .name = "name8", },
500 { .data = "K", .compatible = "compat2", .name = "name9", },
501 {}
502};
503
504static struct {
505 const char *path;
506 const char *data;
507} match_node_tests[] = {
508 { .path = "/testcase-data/match-node/name0", .data = "A", },
509 { .path = "/testcase-data/match-node/name1", .data = "B", },
510 { .path = "/testcase-data/match-node/a/name2", .data = "Ca", },
511 { .path = "/testcase-data/match-node/b/name2", .data = "Cb", },
512 { .path = "/testcase-data/match-node/c/name2", .data = "Cc", },
513 { .path = "/testcase-data/match-node/name3", .data = "E", },
514 { .path = "/testcase-data/match-node/name4", .data = "G", },
515 { .path = "/testcase-data/match-node/name5", .data = "H", },
516 { .path = "/testcase-data/match-node/name6", .data = "G", },
517 { .path = "/testcase-data/match-node/name7", .data = "I", },
518 { .path = "/testcase-data/match-node/name8", .data = "J", },
519 { .path = "/testcase-data/match-node/name9", .data = "K", },
520};
521
522static void __init of_selftest_match_node(void)
523{
524 struct device_node *np;
525 const struct of_device_id *match;
526 int i;
527
528 for (i = 0; i < ARRAY_SIZE(match_node_tests); i++) {
529 np = of_find_node_by_path(match_node_tests[i].path);
530 if (!np) {
531 selftest(0, "missing testcase node %s\n",
532 match_node_tests[i].path);
533 continue;
534 }
535
536 match = of_match_node(match_node_table, np);
537 if (!match) {
538 selftest(0, "%s didn't match anything\n",
539 match_node_tests[i].path);
540 continue;
541 }
542
543 if (strcmp(match->data, match_node_tests[i].data) != 0) {
544 selftest(0, "%s got wrong match. expected %s, got %s\n",
545 match_node_tests[i].path, match_node_tests[i].data,
546 (const char *)match->data);
547 continue;
548 }
549 selftest(1, "passed");
550 }
551}
552
Rob Herring82c0f582014-04-23 17:57:40 -0500553static void __init of_selftest_platform_populate(void)
554{
555 int irq;
Rob Herringfb2caa52014-05-13 10:07:54 -0500556 struct device_node *np, *child;
Rob Herring82c0f582014-04-23 17:57:40 -0500557 struct platform_device *pdev;
Rob Herringfb2caa52014-05-13 10:07:54 -0500558 struct of_device_id match[] = {
559 { .compatible = "test-device", },
560 {}
561 };
Rob Herring82c0f582014-04-23 17:57:40 -0500562
563 np = of_find_node_by_path("/testcase-data");
564 of_platform_populate(np, of_default_bus_match_table, NULL, NULL);
565
566 /* Test that a missing irq domain returns -EPROBE_DEFER */
567 np = of_find_node_by_path("/testcase-data/testcase-device1");
568 pdev = of_find_device_by_node(np);
Rob Herring7d1cdc82014-05-13 10:07:29 -0500569 selftest(pdev, "device 1 creation failed\n");
570
Rob Herring82c0f582014-04-23 17:57:40 -0500571 irq = platform_get_irq(pdev, 0);
Rob Herring7d1cdc82014-05-13 10:07:29 -0500572 selftest(irq == -EPROBE_DEFER, "device deferred probe failed - %d\n", irq);
Rob Herring82c0f582014-04-23 17:57:40 -0500573
574 /* Test that a parsing failure does not return -EPROBE_DEFER */
575 np = of_find_node_by_path("/testcase-data/testcase-device2");
576 pdev = of_find_device_by_node(np);
Rob Herring7d1cdc82014-05-13 10:07:29 -0500577 selftest(pdev, "device 2 creation failed\n");
Rob Herring82c0f582014-04-23 17:57:40 -0500578 irq = platform_get_irq(pdev, 0);
Rob Herring7d1cdc82014-05-13 10:07:29 -0500579 selftest(irq < 0 && irq != -EPROBE_DEFER, "device parsing error failed - %d\n", irq);
Rob Herring82c0f582014-04-23 17:57:40 -0500580
Rob Herringfb2caa52014-05-13 10:07:54 -0500581 np = of_find_node_by_path("/testcase-data/platform-tests");
582 if (!np) {
583 pr_err("No testcase data in device tree\n");
584 return;
585 }
586
587 for_each_child_of_node(np, child) {
588 struct device_node *grandchild;
589 of_platform_populate(child, match, NULL, NULL);
590 for_each_child_of_node(child, grandchild)
591 selftest(of_find_device_by_node(grandchild),
592 "Could not create device for node '%s'\n",
593 grandchild->name);
594 }
Rob Herring82c0f582014-04-23 17:57:40 -0500595}
596
Grant Likely53a42092011-12-12 09:25:57 -0700597static int __init of_selftest(void)
598{
599 struct device_node *np;
600
601 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
602 if (!np) {
603 pr_info("No testcase data in device tree; not running tests\n");
604 return 0;
605 }
606 of_node_put(np);
607
608 pr_info("start of selftest - you will see error messages\n");
Grant Likelyae91ff72014-03-14 13:53:10 +0000609 of_selftest_find_node_by_name();
Grant Likely7e66c5c2013-11-15 17:19:09 +0000610 of_selftest_dynamic();
Grant Likely53a42092011-12-12 09:25:57 -0700611 of_selftest_parse_phandle_with_args();
Grant Likely7aff0fe2011-12-12 09:25:58 -0700612 of_selftest_property_match_string();
Pantelis Antoniou69843392014-07-04 19:58:47 +0300613 of_selftest_property_copy();
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300614 of_selftest_changeset();
Grant Likelya9f10ca2013-10-11 22:04:23 +0100615 of_selftest_parse_interrupts();
Grant Likely79d97012013-09-19 16:47:37 -0500616 of_selftest_parse_interrupts_extended();
Grant Likely1f42e5d2014-02-18 21:38:55 +0000617 of_selftest_match_node();
Rob Herring82c0f582014-04-23 17:57:40 -0500618 of_selftest_platform_populate();
Grant Likelya9f10ca2013-10-11 22:04:23 +0100619 pr_info("end of selftest - %i passed, %i failed\n",
620 selftest_results.passed, selftest_results.failed);
Grant Likely53a42092011-12-12 09:25:57 -0700621 return 0;
622}
623late_initcall(of_selftest);