2 * Self tests for device tree subsystem
5 #define pr_fmt(fmt) "### dt-test ### " fmt
9 #include <linux/errno.h>
10 #include <linux/hashtable.h>
11 #include <linux/module.h>
13 #include <linux/of_fdt.h>
14 #include <linux/of_irq.h>
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>
23 #include <linux/i2c.h>
24 #include <linux/i2c-mux.h>
26 #include "of_private.h"
28 static struct selftest_results
{
33 #define selftest(result, fmt, ...) ({ \
34 bool failed = !(result); \
36 selftest_results.failed++; \
37 pr_err("FAIL %s():%i " fmt, __func__, __LINE__, ##__VA_ARGS__); \
39 selftest_results.passed++; \
40 pr_debug("pass %s():%i\n", __func__, __LINE__); \
45 static void __init
of_selftest_find_node_by_name(void)
47 struct device_node
*np
;
50 np
= of_find_node_by_path("/testcase-data");
51 selftest(np
&& !strcmp("/testcase-data", np
->full_name
),
52 "find /testcase-data failed\n");
55 /* Test if trailing '/' works */
56 np
= of_find_node_by_path("/testcase-data/");
57 selftest(!np
, "trailing '/' on /testcase-data/ should fail\n");
59 np
= of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
60 selftest(np
&& !strcmp("/testcase-data/phandle-tests/consumer-a", np
->full_name
),
61 "find /testcase-data/phandle-tests/consumer-a failed\n");
64 np
= of_find_node_by_path("testcase-alias");
65 selftest(np
&& !strcmp("/testcase-data", np
->full_name
),
66 "find testcase-alias failed\n");
69 /* Test if trailing '/' works on aliases */
70 np
= of_find_node_by_path("testcase-alias/");
71 selftest(!np
, "trailing '/' on testcase-alias/ should fail\n");
73 np
= of_find_node_by_path("testcase-alias/phandle-tests/consumer-a");
74 selftest(np
&& !strcmp("/testcase-data/phandle-tests/consumer-a", np
->full_name
),
75 "find testcase-alias/phandle-tests/consumer-a failed\n");
78 np
= of_find_node_by_path("/testcase-data/missing-path");
79 selftest(!np
, "non-existent path returned node %s\n", np
->full_name
);
82 np
= of_find_node_by_path("missing-alias");
83 selftest(!np
, "non-existent alias returned node %s\n", np
->full_name
);
86 np
= of_find_node_by_path("testcase-alias/missing-path");
87 selftest(!np
, "non-existent alias with relative path returned node %s\n", np
->full_name
);
90 np
= of_find_node_opts_by_path("/testcase-data:testoption", &options
);
91 selftest(np
&& !strcmp("testoption", options
),
92 "option path test failed\n");
95 np
= of_find_node_opts_by_path("/testcase-data:testoption", NULL
);
96 selftest(np
, "NULL option path test failed\n");
99 np
= of_find_node_opts_by_path("testcase-alias:testaliasoption",
101 selftest(np
&& !strcmp("testaliasoption", options
),
102 "option alias path test failed\n");
105 np
= of_find_node_opts_by_path("testcase-alias:testaliasoption", NULL
);
106 selftest(np
, "NULL option alias path test failed\n");
109 options
= "testoption";
110 np
= of_find_node_opts_by_path("testcase-alias", &options
);
111 selftest(np
&& !options
, "option clearing test failed\n");
114 options
= "testoption";
115 np
= of_find_node_opts_by_path("/", &options
);
116 selftest(np
&& !options
, "option clearing root node test failed\n");
120 static void __init
of_selftest_dynamic(void)
122 struct device_node
*np
;
123 struct property
*prop
;
125 np
= of_find_node_by_path("/testcase-data");
127 pr_err("missing testcase data\n");
131 /* Array of 4 properties for the purpose of testing */
132 prop
= kzalloc(sizeof(*prop
) * 4, GFP_KERNEL
);
134 selftest(0, "kzalloc() failed\n");
138 /* Add a new property - should pass*/
139 prop
->name
= "new-property";
140 prop
->value
= "new-property-data";
141 prop
->length
= strlen(prop
->value
);
142 selftest(of_add_property(np
, prop
) == 0, "Adding a new property failed\n");
144 /* Try to add an existing property - should fail */
146 prop
->name
= "new-property";
147 prop
->value
= "new-property-data-should-fail";
148 prop
->length
= strlen(prop
->value
);
149 selftest(of_add_property(np
, prop
) != 0,
150 "Adding an existing property should have failed\n");
152 /* Try to modify an existing property - should pass */
153 prop
->value
= "modify-property-data-should-pass";
154 prop
->length
= strlen(prop
->value
);
155 selftest(of_update_property(np
, prop
) == 0,
156 "Updating an existing property should have passed\n");
158 /* Try to modify non-existent property - should pass*/
160 prop
->name
= "modify-property";
161 prop
->value
= "modify-missing-property-data-should-pass";
162 prop
->length
= strlen(prop
->value
);
163 selftest(of_update_property(np
, prop
) == 0,
164 "Updating a missing property should have passed\n");
166 /* Remove property - should pass */
167 selftest(of_remove_property(np
, prop
) == 0,
168 "Removing a property should have passed\n");
170 /* Adding very large property - should pass */
172 prop
->name
= "large-property-PAGE_SIZEx8";
173 prop
->length
= PAGE_SIZE
* 8;
174 prop
->value
= kzalloc(prop
->length
, GFP_KERNEL
);
175 selftest(prop
->value
!= NULL
, "Unable to allocate large buffer\n");
177 selftest(of_add_property(np
, prop
) == 0,
178 "Adding a large property should have passed\n");
181 static int __init
of_selftest_check_node_linkage(struct device_node
*np
)
183 struct device_node
*child
;
186 for_each_child_of_node(np
, child
) {
187 if (child
->parent
!= np
) {
188 pr_err("Child node %s links to wrong parent %s\n",
189 child
->name
, np
->name
);
193 rc
= of_selftest_check_node_linkage(child
);
202 static void __init
of_selftest_check_tree_linkage(void)
204 struct device_node
*np
;
205 int allnode_count
= 0, child_count
;
210 for_each_of_allnodes(np
)
212 child_count
= of_selftest_check_node_linkage(of_root
);
214 selftest(child_count
> 0, "Device node data structure is corrupted\n");
215 selftest(child_count
== allnode_count
, "allnodes list size (%i) doesn't match"
216 "sibling lists size (%i)\n", allnode_count
, child_count
);
217 pr_debug("allnodes list size (%i); sibling lists size (%i)\n", allnode_count
, child_count
);
221 struct hlist_node node
;
222 struct device_node
*np
;
225 static DEFINE_HASHTABLE(phandle_ht
, 8);
226 static void __init
of_selftest_check_phandles(void)
228 struct device_node
*np
;
229 struct node_hash
*nh
;
230 struct hlist_node
*tmp
;
231 int i
, dup_count
= 0, phandle_count
= 0;
233 for_each_of_allnodes(np
) {
237 hash_for_each_possible(phandle_ht
, nh
, node
, np
->phandle
) {
238 if (nh
->np
->phandle
== np
->phandle
) {
239 pr_info("Duplicate phandle! %i used by %s and %s\n",
240 np
->phandle
, nh
->np
->full_name
, np
->full_name
);
246 nh
= kzalloc(sizeof(*nh
), GFP_KERNEL
);
251 hash_add(phandle_ht
, &nh
->node
, np
->phandle
);
254 selftest(dup_count
== 0, "Found %i duplicates in %i phandles\n",
255 dup_count
, phandle_count
);
258 hash_for_each_safe(phandle_ht
, i
, tmp
, nh
, node
) {
264 static void __init
of_selftest_parse_phandle_with_args(void)
266 struct device_node
*np
;
267 struct of_phandle_args args
;
270 np
= of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
272 pr_err("missing testcase data\n");
276 rc
= of_count_phandle_with_args(np
, "phandle-list", "#phandle-cells");
277 selftest(rc
== 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc
);
279 for (i
= 0; i
< 8; i
++) {
281 rc
= of_parse_phandle_with_args(np
, "phandle-list",
282 "#phandle-cells", i
, &args
);
284 /* Test the values from tests-phandle.dtsi */
288 passed
&= (args
.args_count
== 1);
289 passed
&= (args
.args
[0] == (i
+ 1));
293 passed
&= (args
.args_count
== 2);
294 passed
&= (args
.args
[0] == (i
+ 1));
295 passed
&= (args
.args
[1] == 0);
298 passed
&= (rc
== -ENOENT
);
302 passed
&= (args
.args_count
== 3);
303 passed
&= (args
.args
[0] == (i
+ 1));
304 passed
&= (args
.args
[1] == 4);
305 passed
&= (args
.args
[2] == 3);
309 passed
&= (args
.args_count
== 2);
310 passed
&= (args
.args
[0] == (i
+ 1));
311 passed
&= (args
.args
[1] == 100);
315 passed
&= (args
.args_count
== 0);
319 passed
&= (args
.args_count
== 1);
320 passed
&= (args
.args
[0] == (i
+ 1));
323 passed
&= (rc
== -ENOENT
);
329 selftest(passed
, "index %i - data error on node %s rc=%i\n",
330 i
, args
.np
->full_name
, rc
);
333 /* Check for missing list property */
334 rc
= of_parse_phandle_with_args(np
, "phandle-list-missing",
335 "#phandle-cells", 0, &args
);
336 selftest(rc
== -ENOENT
, "expected:%i got:%i\n", -ENOENT
, rc
);
337 rc
= of_count_phandle_with_args(np
, "phandle-list-missing",
339 selftest(rc
== -ENOENT
, "expected:%i got:%i\n", -ENOENT
, rc
);
341 /* Check for missing cells property */
342 rc
= of_parse_phandle_with_args(np
, "phandle-list",
343 "#phandle-cells-missing", 0, &args
);
344 selftest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
345 rc
= of_count_phandle_with_args(np
, "phandle-list",
346 "#phandle-cells-missing");
347 selftest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
349 /* Check for bad phandle in list */
350 rc
= of_parse_phandle_with_args(np
, "phandle-list-bad-phandle",
351 "#phandle-cells", 0, &args
);
352 selftest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
353 rc
= of_count_phandle_with_args(np
, "phandle-list-bad-phandle",
355 selftest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
357 /* Check for incorrectly formed argument list */
358 rc
= of_parse_phandle_with_args(np
, "phandle-list-bad-args",
359 "#phandle-cells", 1, &args
);
360 selftest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
361 rc
= of_count_phandle_with_args(np
, "phandle-list-bad-args",
363 selftest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
366 static void __init
of_selftest_property_string(void)
368 const char *strings
[4];
369 struct device_node
*np
;
372 np
= of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
374 pr_err("No testcase data in device tree\n");
378 rc
= of_property_match_string(np
, "phandle-list-names", "first");
379 selftest(rc
== 0, "first expected:0 got:%i\n", rc
);
380 rc
= of_property_match_string(np
, "phandle-list-names", "second");
381 selftest(rc
== 1, "second expected:0 got:%i\n", rc
);
382 rc
= of_property_match_string(np
, "phandle-list-names", "third");
383 selftest(rc
== 2, "third expected:0 got:%i\n", rc
);
384 rc
= of_property_match_string(np
, "phandle-list-names", "fourth");
385 selftest(rc
== -ENODATA
, "unmatched string; rc=%i\n", rc
);
386 rc
= of_property_match_string(np
, "missing-property", "blah");
387 selftest(rc
== -EINVAL
, "missing property; rc=%i\n", rc
);
388 rc
= of_property_match_string(np
, "empty-property", "blah");
389 selftest(rc
== -ENODATA
, "empty property; rc=%i\n", rc
);
390 rc
= of_property_match_string(np
, "unterminated-string", "blah");
391 selftest(rc
== -EILSEQ
, "unterminated string; rc=%i\n", rc
);
393 /* of_property_count_strings() tests */
394 rc
= of_property_count_strings(np
, "string-property");
395 selftest(rc
== 1, "Incorrect string count; rc=%i\n", rc
);
396 rc
= of_property_count_strings(np
, "phandle-list-names");
397 selftest(rc
== 3, "Incorrect string count; rc=%i\n", rc
);
398 rc
= of_property_count_strings(np
, "unterminated-string");
399 selftest(rc
== -EILSEQ
, "unterminated string; rc=%i\n", rc
);
400 rc
= of_property_count_strings(np
, "unterminated-string-list");
401 selftest(rc
== -EILSEQ
, "unterminated string array; rc=%i\n", rc
);
403 /* of_property_read_string_index() tests */
404 rc
= of_property_read_string_index(np
, "string-property", 0, strings
);
405 selftest(rc
== 0 && !strcmp(strings
[0], "foobar"), "of_property_read_string_index() failure; rc=%i\n", rc
);
407 rc
= of_property_read_string_index(np
, "string-property", 1, strings
);
408 selftest(rc
== -ENODATA
&& strings
[0] == NULL
, "of_property_read_string_index() failure; rc=%i\n", rc
);
409 rc
= of_property_read_string_index(np
, "phandle-list-names", 0, strings
);
410 selftest(rc
== 0 && !strcmp(strings
[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc
);
411 rc
= of_property_read_string_index(np
, "phandle-list-names", 1, strings
);
412 selftest(rc
== 0 && !strcmp(strings
[0], "second"), "of_property_read_string_index() failure; rc=%i\n", rc
);
413 rc
= of_property_read_string_index(np
, "phandle-list-names", 2, strings
);
414 selftest(rc
== 0 && !strcmp(strings
[0], "third"), "of_property_read_string_index() failure; rc=%i\n", rc
);
416 rc
= of_property_read_string_index(np
, "phandle-list-names", 3, strings
);
417 selftest(rc
== -ENODATA
&& strings
[0] == NULL
, "of_property_read_string_index() failure; rc=%i\n", rc
);
419 rc
= of_property_read_string_index(np
, "unterminated-string", 0, strings
);
420 selftest(rc
== -EILSEQ
&& strings
[0] == NULL
, "of_property_read_string_index() failure; rc=%i\n", rc
);
421 rc
= of_property_read_string_index(np
, "unterminated-string-list", 0, strings
);
422 selftest(rc
== 0 && !strcmp(strings
[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc
);
424 rc
= of_property_read_string_index(np
, "unterminated-string-list", 2, strings
); /* should fail */
425 selftest(rc
== -EILSEQ
&& strings
[0] == NULL
, "of_property_read_string_index() failure; rc=%i\n", rc
);
428 /* of_property_read_string_array() tests */
429 rc
= of_property_read_string_array(np
, "string-property", strings
, 4);
430 selftest(rc
== 1, "Incorrect string count; rc=%i\n", rc
);
431 rc
= of_property_read_string_array(np
, "phandle-list-names", strings
, 4);
432 selftest(rc
== 3, "Incorrect string count; rc=%i\n", rc
);
433 rc
= of_property_read_string_array(np
, "unterminated-string", strings
, 4);
434 selftest(rc
== -EILSEQ
, "unterminated string; rc=%i\n", rc
);
435 /* -- An incorrectly formed string should cause a failure */
436 rc
= of_property_read_string_array(np
, "unterminated-string-list", strings
, 4);
437 selftest(rc
== -EILSEQ
, "unterminated string array; rc=%i\n", rc
);
438 /* -- parsing the correctly formed strings should still work: */
440 rc
= of_property_read_string_array(np
, "unterminated-string-list", strings
, 2);
441 selftest(rc
== 2 && strings
[2] == NULL
, "of_property_read_string_array() failure; rc=%i\n", rc
);
443 rc
= of_property_read_string_array(np
, "phandle-list-names", strings
, 1);
444 selftest(rc
== 1 && strings
[1] == NULL
, "Overwrote end of string array; rc=%i, str='%s'\n", rc
, strings
[1]);
447 #define propcmp(p1, p2) (((p1)->length == (p2)->length) && \
448 (p1)->value && (p2)->value && \
449 !memcmp((p1)->value, (p2)->value, (p1)->length) && \
450 !strcmp((p1)->name, (p2)->name))
451 static void __init
of_selftest_property_copy(void)
453 #ifdef CONFIG_OF_DYNAMIC
454 struct property p1
= { .name
= "p1", .length
= 0, .value
= "" };
455 struct property p2
= { .name
= "p2", .length
= 5, .value
= "abcd" };
456 struct property
*new;
458 new = __of_prop_dup(&p1
, GFP_KERNEL
);
459 selftest(new && propcmp(&p1
, new), "empty property didn't copy correctly\n");
464 new = __of_prop_dup(&p2
, GFP_KERNEL
);
465 selftest(new && propcmp(&p2
, new), "non-empty property didn't copy correctly\n");
472 static void __init
of_selftest_changeset(void)
474 #ifdef CONFIG_OF_DYNAMIC
475 struct property
*ppadd
, padd
= { .name
= "prop-add", .length
= 0, .value
= "" };
476 struct property
*ppupdate
, pupdate
= { .name
= "prop-update", .length
= 5, .value
= "abcd" };
477 struct property
*ppremove
;
478 struct device_node
*n1
, *n2
, *n21
, *nremove
, *parent
, *np
;
479 struct of_changeset chgset
;
481 of_changeset_init(&chgset
);
482 n1
= __of_node_dup(NULL
, "/testcase-data/changeset/n1");
483 selftest(n1
, "testcase setup failure\n");
484 n2
= __of_node_dup(NULL
, "/testcase-data/changeset/n2");
485 selftest(n2
, "testcase setup failure\n");
486 n21
= __of_node_dup(NULL
, "%s/%s", "/testcase-data/changeset/n2", "n21");
487 selftest(n21
, "testcase setup failure %p\n", n21
);
488 nremove
= of_find_node_by_path("/testcase-data/changeset/node-remove");
489 selftest(nremove
, "testcase setup failure\n");
490 ppadd
= __of_prop_dup(&padd
, GFP_KERNEL
);
491 selftest(ppadd
, "testcase setup failure\n");
492 ppupdate
= __of_prop_dup(&pupdate
, GFP_KERNEL
);
493 selftest(ppupdate
, "testcase setup failure\n");
494 parent
= nremove
->parent
;
499 ppremove
= of_find_property(parent
, "prop-remove", NULL
);
500 selftest(ppremove
, "failed to find removal prop");
502 of_changeset_init(&chgset
);
503 selftest(!of_changeset_attach_node(&chgset
, n1
), "fail attach n1\n");
504 selftest(!of_changeset_attach_node(&chgset
, n2
), "fail attach n2\n");
505 selftest(!of_changeset_detach_node(&chgset
, nremove
), "fail remove node\n");
506 selftest(!of_changeset_attach_node(&chgset
, n21
), "fail attach n21\n");
507 selftest(!of_changeset_add_property(&chgset
, parent
, ppadd
), "fail add prop\n");
508 selftest(!of_changeset_update_property(&chgset
, parent
, ppupdate
), "fail update prop\n");
509 selftest(!of_changeset_remove_property(&chgset
, parent
, ppremove
), "fail remove prop\n");
510 mutex_lock(&of_mutex
);
511 selftest(!of_changeset_apply(&chgset
), "apply failed\n");
512 mutex_unlock(&of_mutex
);
514 /* Make sure node names are constructed correctly */
515 selftest((np
= of_find_node_by_path("/testcase-data/changeset/n2/n21")),
516 "'%s' not added\n", n21
->full_name
);
519 mutex_lock(&of_mutex
);
520 selftest(!of_changeset_revert(&chgset
), "revert failed\n");
521 mutex_unlock(&of_mutex
);
523 of_changeset_destroy(&chgset
);
527 static void __init
of_selftest_parse_interrupts(void)
529 struct device_node
*np
;
530 struct of_phandle_args args
;
533 np
= of_find_node_by_path("/testcase-data/interrupts/interrupts0");
535 pr_err("missing testcase data\n");
539 for (i
= 0; i
< 4; i
++) {
542 rc
= of_irq_parse_one(np
, i
, &args
);
545 passed
&= (args
.args_count
== 1);
546 passed
&= (args
.args
[0] == (i
+ 1));
548 selftest(passed
, "index %i - data error on node %s rc=%i\n",
549 i
, args
.np
->full_name
, rc
);
553 np
= of_find_node_by_path("/testcase-data/interrupts/interrupts1");
555 pr_err("missing testcase data\n");
559 for (i
= 0; i
< 4; i
++) {
562 rc
= of_irq_parse_one(np
, i
, &args
);
564 /* Test the values from tests-phandle.dtsi */
568 passed
&= (args
.args_count
== 1);
569 passed
&= (args
.args
[0] == 9);
573 passed
&= (args
.args_count
== 3);
574 passed
&= (args
.args
[0] == 10);
575 passed
&= (args
.args
[1] == 11);
576 passed
&= (args
.args
[2] == 12);
580 passed
&= (args
.args_count
== 2);
581 passed
&= (args
.args
[0] == 13);
582 passed
&= (args
.args
[1] == 14);
586 passed
&= (args
.args_count
== 2);
587 passed
&= (args
.args
[0] == 15);
588 passed
&= (args
.args
[1] == 16);
593 selftest(passed
, "index %i - data error on node %s rc=%i\n",
594 i
, args
.np
->full_name
, rc
);
599 static void __init
of_selftest_parse_interrupts_extended(void)
601 struct device_node
*np
;
602 struct of_phandle_args args
;
605 np
= of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
607 pr_err("missing testcase data\n");
611 for (i
= 0; i
< 7; i
++) {
613 rc
= of_irq_parse_one(np
, i
, &args
);
615 /* Test the values from tests-phandle.dtsi */
619 passed
&= (args
.args_count
== 1);
620 passed
&= (args
.args
[0] == 1);
624 passed
&= (args
.args_count
== 3);
625 passed
&= (args
.args
[0] == 2);
626 passed
&= (args
.args
[1] == 3);
627 passed
&= (args
.args
[2] == 4);
631 passed
&= (args
.args_count
== 2);
632 passed
&= (args
.args
[0] == 5);
633 passed
&= (args
.args
[1] == 6);
637 passed
&= (args
.args_count
== 1);
638 passed
&= (args
.args
[0] == 9);
642 passed
&= (args
.args_count
== 3);
643 passed
&= (args
.args
[0] == 10);
644 passed
&= (args
.args
[1] == 11);
645 passed
&= (args
.args
[2] == 12);
649 passed
&= (args
.args_count
== 2);
650 passed
&= (args
.args
[0] == 13);
651 passed
&= (args
.args
[1] == 14);
655 passed
&= (args
.args_count
== 1);
656 passed
&= (args
.args
[0] == 15);
662 selftest(passed
, "index %i - data error on node %s rc=%i\n",
663 i
, args
.np
->full_name
, rc
);
668 static struct of_device_id match_node_table
[] = {
669 { .data
= "A", .name
= "name0", }, /* Name alone is lowest priority */
670 { .data
= "B", .type
= "type1", }, /* followed by type alone */
672 { .data
= "Ca", .name
= "name2", .type
= "type1", }, /* followed by both together */
673 { .data
= "Cb", .name
= "name2", }, /* Only match when type doesn't match */
674 { .data
= "Cc", .name
= "name2", .type
= "type2", },
676 { .data
= "E", .compatible
= "compat3" },
677 { .data
= "G", .compatible
= "compat2", },
678 { .data
= "H", .compatible
= "compat2", .name
= "name5", },
679 { .data
= "I", .compatible
= "compat2", .type
= "type1", },
680 { .data
= "J", .compatible
= "compat2", .type
= "type1", .name
= "name8", },
681 { .data
= "K", .compatible
= "compat2", .name
= "name9", },
688 } match_node_tests
[] = {
689 { .path
= "/testcase-data/match-node/name0", .data
= "A", },
690 { .path
= "/testcase-data/match-node/name1", .data
= "B", },
691 { .path
= "/testcase-data/match-node/a/name2", .data
= "Ca", },
692 { .path
= "/testcase-data/match-node/b/name2", .data
= "Cb", },
693 { .path
= "/testcase-data/match-node/c/name2", .data
= "Cc", },
694 { .path
= "/testcase-data/match-node/name3", .data
= "E", },
695 { .path
= "/testcase-data/match-node/name4", .data
= "G", },
696 { .path
= "/testcase-data/match-node/name5", .data
= "H", },
697 { .path
= "/testcase-data/match-node/name6", .data
= "G", },
698 { .path
= "/testcase-data/match-node/name7", .data
= "I", },
699 { .path
= "/testcase-data/match-node/name8", .data
= "J", },
700 { .path
= "/testcase-data/match-node/name9", .data
= "K", },
703 static void __init
of_selftest_match_node(void)
705 struct device_node
*np
;
706 const struct of_device_id
*match
;
709 for (i
= 0; i
< ARRAY_SIZE(match_node_tests
); i
++) {
710 np
= of_find_node_by_path(match_node_tests
[i
].path
);
712 selftest(0, "missing testcase node %s\n",
713 match_node_tests
[i
].path
);
717 match
= of_match_node(match_node_table
, np
);
719 selftest(0, "%s didn't match anything\n",
720 match_node_tests
[i
].path
);
724 if (strcmp(match
->data
, match_node_tests
[i
].data
) != 0) {
725 selftest(0, "%s got wrong match. expected %s, got %s\n",
726 match_node_tests
[i
].path
, match_node_tests
[i
].data
,
727 (const char *)match
->data
);
730 selftest(1, "passed");
734 struct device test_bus
= {
735 .init_name
= "unittest-bus",
737 static void __init
of_selftest_platform_populate(void)
740 struct device_node
*np
, *child
, *grandchild
;
741 struct platform_device
*pdev
;
742 struct of_device_id match
[] = {
743 { .compatible
= "test-device", },
747 np
= of_find_node_by_path("/testcase-data");
748 of_platform_populate(np
, of_default_bus_match_table
, NULL
, NULL
);
750 /* Test that a missing irq domain returns -EPROBE_DEFER */
751 np
= of_find_node_by_path("/testcase-data/testcase-device1");
752 pdev
= of_find_device_by_node(np
);
753 selftest(pdev
, "device 1 creation failed\n");
755 irq
= platform_get_irq(pdev
, 0);
756 selftest(irq
== -EPROBE_DEFER
, "device deferred probe failed - %d\n", irq
);
758 /* Test that a parsing failure does not return -EPROBE_DEFER */
759 np
= of_find_node_by_path("/testcase-data/testcase-device2");
760 pdev
= of_find_device_by_node(np
);
761 selftest(pdev
, "device 2 creation failed\n");
762 irq
= platform_get_irq(pdev
, 0);
763 selftest(irq
< 0 && irq
!= -EPROBE_DEFER
, "device parsing error failed - %d\n", irq
);
765 if (selftest(np
= of_find_node_by_path("/testcase-data/platform-tests"),
766 "No testcase data in device tree\n"));
769 if (selftest(!(rc
= device_register(&test_bus
)),
770 "testbus registration failed; rc=%i\n", rc
));
773 for_each_child_of_node(np
, child
) {
774 of_platform_populate(child
, match
, NULL
, &test_bus
);
775 for_each_child_of_node(child
, grandchild
)
776 selftest(of_find_device_by_node(grandchild
),
777 "Could not create device for node '%s'\n",
781 of_platform_depopulate(&test_bus
);
782 for_each_child_of_node(np
, child
) {
783 for_each_child_of_node(child
, grandchild
)
784 selftest(!of_find_device_by_node(grandchild
),
785 "device didn't get destroyed '%s'\n",
789 device_unregister(&test_bus
);
794 * update_node_properties - adds the properties
795 * of np into dup node (present in live tree) and
796 * updates parent of children of np to dup.
798 * @np: node already present in live tree
799 * @dup: node present in live tree to be updated
801 static void update_node_properties(struct device_node
*np
,
802 struct device_node
*dup
)
804 struct property
*prop
;
805 struct device_node
*child
;
807 for_each_property_of_node(np
, prop
)
808 of_add_property(dup
, prop
);
810 for_each_child_of_node(np
, child
)
815 * attach_node_and_children - attaches nodes
816 * and its children to live tree
818 * @np: Node to attach to live tree
820 static int attach_node_and_children(struct device_node
*np
)
822 struct device_node
*next
, *dup
, *child
;
825 dup
= of_find_node_by_path(np
->full_name
);
827 update_node_properties(np
, dup
);
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
);
841 __of_attach_node_sysfs(np
);
842 mutex_unlock(&of_mutex
);
845 next
= child
->sibling
;
846 attach_node_and_children(child
);
854 * selftest_data_add - Reads, copies data from
855 * linked tree and attaches it to the live tree
857 static int __init
selftest_data_add(void)
860 struct device_node
*selftest_data_node
, *np
;
861 extern uint8_t __dtb_testcases_begin
[];
862 extern uint8_t __dtb_testcases_end
[];
863 const int size
= __dtb_testcases_end
- __dtb_testcases_begin
;
867 pr_warn("%s: No testcase data to attach; not running tests\n",
873 selftest_data
= kmemdup(__dtb_testcases_begin
, size
, GFP_KERNEL
);
875 if (!selftest_data
) {
876 pr_warn("%s: Failed to allocate memory for selftest_data; "
877 "not running tests\n", __func__
);
880 of_fdt_unflatten_tree(selftest_data
, &selftest_data_node
);
881 if (!selftest_data_node
) {
882 pr_warn("%s: No tree to attach; not running tests\n", __func__
);
885 of_node_set_flag(selftest_data_node
, OF_DETACHED
);
886 rc
= of_resolve_phandles(selftest_data_node
);
888 pr_err("%s: Failed to resolve phandles (rc=%i)\n", __func__
, rc
);
893 of_root
= selftest_data_node
;
894 for_each_of_allnodes(np
)
895 __of_attach_node_sysfs(np
);
896 of_aliases
= of_find_node_by_path("/aliases");
897 of_chosen
= of_find_node_by_path("/chosen");
901 /* attach the sub-tree to live tree */
902 np
= selftest_data_node
->child
;
904 struct device_node
*next
= np
->sibling
;
905 np
->parent
= of_root
;
906 attach_node_and_children(np
);
912 #ifdef CONFIG_OF_OVERLAY
914 static int selftest_probe(struct platform_device
*pdev
)
916 struct device
*dev
= &pdev
->dev
;
917 struct device_node
*np
= dev
->of_node
;
920 dev_err(dev
, "No OF data for device\n");
925 dev_dbg(dev
, "%s for node @%s\n", __func__
, np
->full_name
);
927 of_platform_populate(np
, NULL
, NULL
, &pdev
->dev
);
932 static int selftest_remove(struct platform_device
*pdev
)
934 struct device
*dev
= &pdev
->dev
;
935 struct device_node
*np
= dev
->of_node
;
937 dev_dbg(dev
, "%s for node @%s\n", __func__
, np
->full_name
);
941 static struct of_device_id selftest_match
[] = {
942 { .compatible
= "selftest", },
946 static struct platform_driver selftest_driver
= {
947 .probe
= selftest_probe
,
948 .remove
= selftest_remove
,
951 .owner
= THIS_MODULE
,
952 .of_match_table
= of_match_ptr(selftest_match
),
956 /* get the platform device instantiated at the path */
957 static struct platform_device
*of_path_to_platform_device(const char *path
)
959 struct device_node
*np
;
960 struct platform_device
*pdev
;
962 np
= of_find_node_by_path(path
);
966 pdev
= of_find_device_by_node(np
);
972 /* find out if a platform device exists at that path */
973 static int of_path_platform_device_exists(const char *path
)
975 struct platform_device
*pdev
;
977 pdev
= of_path_to_platform_device(path
);
978 platform_device_put(pdev
);
982 #if IS_ENABLED(CONFIG_I2C)
984 /* get the i2c client device instantiated at the path */
985 static struct i2c_client
*of_path_to_i2c_client(const char *path
)
987 struct device_node
*np
;
988 struct i2c_client
*client
;
990 np
= of_find_node_by_path(path
);
994 client
= of_find_i2c_device_by_node(np
);
1000 /* find out if a i2c client device exists at that path */
1001 static int of_path_i2c_client_exists(const char *path
)
1003 struct i2c_client
*client
;
1005 client
= of_path_to_i2c_client(path
);
1007 put_device(&client
->dev
);
1008 return client
!= NULL
;
1011 static int of_path_i2c_client_exists(const char *path
)
1022 static int of_path_device_type_exists(const char *path
,
1023 enum overlay_type ovtype
)
1027 return of_path_platform_device_exists(path
);
1029 return of_path_i2c_client_exists(path
);
1034 static const char *selftest_path(int nr
, enum overlay_type ovtype
)
1037 static char buf
[256];
1041 base
= "/testcase-data/overlay-node/test-bus";
1044 base
= "/testcase-data/overlay-node/test-bus/i2c-test-bus";
1050 snprintf(buf
, sizeof(buf
) - 1, "%s/test-selftest%d", base
, nr
);
1051 buf
[sizeof(buf
) - 1] = '\0';
1055 static int of_selftest_device_exists(int selftest_nr
, enum overlay_type ovtype
)
1059 path
= selftest_path(selftest_nr
, ovtype
);
1063 return of_path_platform_device_exists(path
);
1065 return of_path_i2c_client_exists(path
);
1070 static const char *overlay_path(int nr
)
1072 static char buf
[256];
1074 snprintf(buf
, sizeof(buf
) - 1,
1075 "/testcase-data/overlay%d", nr
);
1076 buf
[sizeof(buf
) - 1] = '\0';
1081 static const char *bus_path
= "/testcase-data/overlay-node/test-bus";
1083 static int of_selftest_apply_overlay(int selftest_nr
, int overlay_nr
,
1086 struct device_node
*np
= NULL
;
1089 np
= of_find_node_by_path(overlay_path(overlay_nr
));
1091 selftest(0, "could not find overlay node @\"%s\"\n",
1092 overlay_path(overlay_nr
));
1097 ret
= of_overlay_create(np
);
1099 selftest(0, "could not create overlay from \"%s\"\n",
1100 overlay_path(overlay_nr
));
1116 /* apply an overlay while checking before and after states */
1117 static int of_selftest_apply_overlay_check(int overlay_nr
, int selftest_nr
,
1118 int before
, int after
, enum overlay_type ovtype
)
1122 /* selftest device must not be in before state */
1123 if (of_selftest_device_exists(selftest_nr
, ovtype
) != before
) {
1124 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1125 overlay_path(overlay_nr
),
1126 selftest_path(selftest_nr
, ovtype
),
1127 !before
? "enabled" : "disabled");
1131 ret
= of_selftest_apply_overlay(overlay_nr
, selftest_nr
, NULL
);
1133 /* of_selftest_apply_overlay already called selftest() */
1137 /* selftest device must be to set to after state */
1138 if (of_selftest_device_exists(selftest_nr
, ovtype
) != after
) {
1139 selftest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n",
1140 overlay_path(overlay_nr
),
1141 selftest_path(selftest_nr
, ovtype
),
1142 !after
? "enabled" : "disabled");
1149 /* apply an overlay and then revert it while checking before, after states */
1150 static int of_selftest_apply_revert_overlay_check(int overlay_nr
,
1151 int selftest_nr
, int before
, int after
,
1152 enum overlay_type ovtype
)
1156 /* selftest device must be in before state */
1157 if (of_selftest_device_exists(selftest_nr
, ovtype
) != before
) {
1158 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1159 overlay_path(overlay_nr
),
1160 selftest_path(selftest_nr
, ovtype
),
1161 !before
? "enabled" : "disabled");
1165 /* apply the overlay */
1166 ret
= of_selftest_apply_overlay(overlay_nr
, selftest_nr
, &ov_id
);
1168 /* of_selftest_apply_overlay already called selftest() */
1172 /* selftest device must be in after state */
1173 if (of_selftest_device_exists(selftest_nr
, ovtype
) != after
) {
1174 selftest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n",
1175 overlay_path(overlay_nr
),
1176 selftest_path(selftest_nr
, ovtype
),
1177 !after
? "enabled" : "disabled");
1181 ret
= of_overlay_destroy(ov_id
);
1183 selftest(0, "overlay @\"%s\" failed to be destroyed @\"%s\"\n",
1184 overlay_path(overlay_nr
),
1185 selftest_path(selftest_nr
, ovtype
));
1189 /* selftest device must be again in before state */
1190 if (of_selftest_device_exists(selftest_nr
, PDEV_OVERLAY
) != before
) {
1191 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1192 overlay_path(overlay_nr
),
1193 selftest_path(selftest_nr
, ovtype
),
1194 !before
? "enabled" : "disabled");
1201 /* test activation of device */
1202 static void of_selftest_overlay_0(void)
1206 /* device should enable */
1207 ret
= of_selftest_apply_overlay_check(0, 0, 0, 1, PDEV_OVERLAY
);
1211 selftest(1, "overlay test %d passed\n", 0);
1214 /* test deactivation of device */
1215 static void of_selftest_overlay_1(void)
1219 /* device should disable */
1220 ret
= of_selftest_apply_overlay_check(1, 1, 1, 0, PDEV_OVERLAY
);
1224 selftest(1, "overlay test %d passed\n", 1);
1227 /* test activation of device */
1228 static void of_selftest_overlay_2(void)
1232 /* device should enable */
1233 ret
= of_selftest_apply_overlay_check(2, 2, 0, 1, PDEV_OVERLAY
);
1237 selftest(1, "overlay test %d passed\n", 2);
1240 /* test deactivation of device */
1241 static void of_selftest_overlay_3(void)
1245 /* device should disable */
1246 ret
= of_selftest_apply_overlay_check(3, 3, 1, 0, PDEV_OVERLAY
);
1250 selftest(1, "overlay test %d passed\n", 3);
1253 /* test activation of a full device node */
1254 static void of_selftest_overlay_4(void)
1258 /* device should disable */
1259 ret
= of_selftest_apply_overlay_check(4, 4, 0, 1, PDEV_OVERLAY
);
1263 selftest(1, "overlay test %d passed\n", 4);
1266 /* test overlay apply/revert sequence */
1267 static void of_selftest_overlay_5(void)
1271 /* device should disable */
1272 ret
= of_selftest_apply_revert_overlay_check(5, 5, 0, 1, PDEV_OVERLAY
);
1276 selftest(1, "overlay test %d passed\n", 5);
1279 /* test overlay application in sequence */
1280 static void of_selftest_overlay_6(void)
1282 struct device_node
*np
;
1283 int ret
, i
, ov_id
[2];
1284 int overlay_nr
= 6, selftest_nr
= 6;
1285 int before
= 0, after
= 1;
1287 /* selftest device must be in before state */
1288 for (i
= 0; i
< 2; i
++) {
1289 if (of_selftest_device_exists(selftest_nr
+ i
, PDEV_OVERLAY
)
1291 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1292 overlay_path(overlay_nr
+ i
),
1293 selftest_path(selftest_nr
+ i
,
1295 !before
? "enabled" : "disabled");
1300 /* apply the overlays */
1301 for (i
= 0; i
< 2; i
++) {
1303 np
= of_find_node_by_path(overlay_path(overlay_nr
+ i
));
1305 selftest(0, "could not find overlay node @\"%s\"\n",
1306 overlay_path(overlay_nr
+ i
));
1310 ret
= of_overlay_create(np
);
1312 selftest(0, "could not create overlay from \"%s\"\n",
1313 overlay_path(overlay_nr
+ i
));
1319 for (i
= 0; i
< 2; i
++) {
1320 /* selftest device must be in after state */
1321 if (of_selftest_device_exists(selftest_nr
+ i
, PDEV_OVERLAY
)
1323 selftest(0, "overlay @\"%s\" failed @\"%s\" %s\n",
1324 overlay_path(overlay_nr
+ i
),
1325 selftest_path(selftest_nr
+ i
,
1327 !after
? "enabled" : "disabled");
1332 for (i
= 1; i
>= 0; i
--) {
1333 ret
= of_overlay_destroy(ov_id
[i
]);
1335 selftest(0, "overlay @\"%s\" failed destroy @\"%s\"\n",
1336 overlay_path(overlay_nr
+ i
),
1337 selftest_path(selftest_nr
+ i
,
1343 for (i
= 0; i
< 2; i
++) {
1344 /* selftest device must be again in before state */
1345 if (of_selftest_device_exists(selftest_nr
+ i
, PDEV_OVERLAY
)
1347 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1348 overlay_path(overlay_nr
+ i
),
1349 selftest_path(selftest_nr
+ i
,
1351 !before
? "enabled" : "disabled");
1356 selftest(1, "overlay test %d passed\n", 6);
1359 /* test overlay application in sequence */
1360 static void of_selftest_overlay_8(void)
1362 struct device_node
*np
;
1363 int ret
, i
, ov_id
[2];
1364 int overlay_nr
= 8, selftest_nr
= 8;
1366 /* we don't care about device state in this test */
1368 /* apply the overlays */
1369 for (i
= 0; i
< 2; i
++) {
1371 np
= of_find_node_by_path(overlay_path(overlay_nr
+ i
));
1373 selftest(0, "could not find overlay node @\"%s\"\n",
1374 overlay_path(overlay_nr
+ i
));
1378 ret
= of_overlay_create(np
);
1380 selftest(0, "could not create overlay from \"%s\"\n",
1381 overlay_path(overlay_nr
+ i
));
1387 /* now try to remove first overlay (it should fail) */
1388 ret
= of_overlay_destroy(ov_id
[0]);
1390 selftest(0, "overlay @\"%s\" was destroyed @\"%s\"\n",
1391 overlay_path(overlay_nr
+ 0),
1392 selftest_path(selftest_nr
,
1397 /* removing them in order should work */
1398 for (i
= 1; i
>= 0; i
--) {
1399 ret
= of_overlay_destroy(ov_id
[i
]);
1401 selftest(0, "overlay @\"%s\" not destroyed @\"%s\"\n",
1402 overlay_path(overlay_nr
+ i
),
1403 selftest_path(selftest_nr
,
1409 selftest(1, "overlay test %d passed\n", 8);
1412 /* test insertion of a bus with parent devices */
1413 static void of_selftest_overlay_10(void)
1418 /* device should disable */
1419 ret
= of_selftest_apply_overlay_check(10, 10, 0, 1, PDEV_OVERLAY
);
1420 if (selftest(ret
== 0,
1421 "overlay test %d failed; overlay application\n", 10))
1424 child_path
= kasprintf(GFP_KERNEL
, "%s/test-selftest101",
1425 selftest_path(10, PDEV_OVERLAY
));
1426 if (selftest(child_path
, "overlay test %d failed; kasprintf\n", 10))
1429 ret
= of_path_device_type_exists(child_path
, PDEV_OVERLAY
);
1431 if (selftest(ret
, "overlay test %d failed; no child device\n", 10))
1435 /* test insertion of a bus with parent devices (and revert) */
1436 static void of_selftest_overlay_11(void)
1440 /* device should disable */
1441 ret
= of_selftest_apply_revert_overlay_check(11, 11, 0, 1,
1443 if (selftest(ret
== 0,
1444 "overlay test %d failed; overlay application\n", 11))
1448 #if IS_ENABLED(CONFIG_I2C) && IS_ENABLED(CONFIG_OF_OVERLAY)
1450 struct selftest_i2c_bus_data
{
1451 struct platform_device
*pdev
;
1452 struct i2c_adapter adap
;
1455 static int selftest_i2c_master_xfer(struct i2c_adapter
*adap
,
1456 struct i2c_msg
*msgs
, int num
)
1458 struct selftest_i2c_bus_data
*std
= i2c_get_adapdata(adap
);
1465 static u32
selftest_i2c_functionality(struct i2c_adapter
*adap
)
1467 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
;
1470 static const struct i2c_algorithm selftest_i2c_algo
= {
1471 .master_xfer
= selftest_i2c_master_xfer
,
1472 .functionality
= selftest_i2c_functionality
,
1475 static int selftest_i2c_bus_probe(struct platform_device
*pdev
)
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
;
1484 dev_err(dev
, "No OF data for device\n");
1489 dev_dbg(dev
, "%s for node @%s\n", __func__
, np
->full_name
);
1491 std
= devm_kzalloc(dev
, sizeof(*std
), GFP_KERNEL
);
1493 dev_err(dev
, "Failed to allocate selftest i2c data\n");
1497 /* link them together */
1499 platform_set_drvdata(pdev
, std
);
1502 i2c_set_adapdata(adap
, std
);
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
;
1512 ret
= i2c_add_numbered_adapter(adap
);
1514 dev_err(dev
, "Failed to add I2C adapter\n");
1521 static int selftest_i2c_bus_remove(struct platform_device
*pdev
)
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
);
1527 dev_dbg(dev
, "%s for node @%s\n", __func__
, np
->full_name
);
1528 i2c_del_adapter(&std
->adap
);
1533 static struct of_device_id selftest_i2c_bus_match
[] = {
1534 { .compatible
= "selftest-i2c-bus", },
1538 static struct platform_driver selftest_i2c_bus_driver
= {
1539 .probe
= selftest_i2c_bus_probe
,
1540 .remove
= selftest_i2c_bus_remove
,
1542 .name
= "selftest-i2c-bus",
1543 .of_match_table
= of_match_ptr(selftest_i2c_bus_match
),
1547 static int selftest_i2c_dev_probe(struct i2c_client
*client
,
1548 const struct i2c_device_id
*id
)
1550 struct device
*dev
= &client
->dev
;
1551 struct device_node
*np
= client
->dev
.of_node
;
1554 dev_err(dev
, "No OF node\n");
1558 dev_dbg(dev
, "%s for node @%s\n", __func__
, np
->full_name
);
1563 static int selftest_i2c_dev_remove(struct i2c_client
*client
)
1565 struct device
*dev
= &client
->dev
;
1566 struct device_node
*np
= client
->dev
.of_node
;
1568 dev_dbg(dev
, "%s for node @%s\n", __func__
, np
->full_name
);
1572 static const struct i2c_device_id selftest_i2c_dev_id
[] = {
1573 { .name
= "selftest-i2c-dev" },
1577 static struct i2c_driver selftest_i2c_dev_driver
= {
1579 .name
= "selftest-i2c-dev",
1580 .owner
= THIS_MODULE
,
1582 .probe
= selftest_i2c_dev_probe
,
1583 .remove
= selftest_i2c_dev_remove
,
1584 .id_table
= selftest_i2c_dev_id
,
1587 #if IS_ENABLED(CONFIG_I2C_MUX)
1589 struct selftest_i2c_mux_data
{
1591 struct i2c_adapter
*adap
[];
1594 static int selftest_i2c_mux_select_chan(struct i2c_adapter
*adap
,
1595 void *client
, u32 chan
)
1600 static int selftest_i2c_mux_probe(struct i2c_client
*client
,
1601 const struct i2c_device_id
*id
)
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
;
1610 dev_dbg(dev
, "%s for node @%s\n", __func__
, np
->full_name
);
1613 dev_err(dev
, "No OF node\n");
1618 for_each_child_of_node(np
, child
) {
1619 ret
= of_property_read_u32(child
, "reg", ®
);
1622 if (max_reg
== (u32
)-1 || reg
> max_reg
)
1625 nchans
= max_reg
== (u32
)-1 ? 0 : max_reg
+ 1;
1627 dev_err(dev
, "No channels\n");
1631 size
= offsetof(struct selftest_i2c_mux_data
, adap
[nchans
]);
1632 stm
= devm_kzalloc(dev
, size
, GFP_KERNEL
);
1634 dev_err(dev
, "Out of memory\n");
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
]);
1649 i2c_set_clientdata(client
, stm
);
1654 static int selftest_i2c_mux_remove(struct i2c_client
*client
)
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
);
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
]);
1667 static const struct i2c_device_id selftest_i2c_mux_id
[] = {
1668 { .name
= "selftest-i2c-mux" },
1672 static struct i2c_driver selftest_i2c_mux_driver
= {
1674 .name
= "selftest-i2c-mux",
1675 .owner
= THIS_MODULE
,
1677 .probe
= selftest_i2c_mux_probe
,
1678 .remove
= selftest_i2c_mux_remove
,
1679 .id_table
= selftest_i2c_mux_id
,
1684 static int of_selftest_overlay_i2c_init(void)
1688 ret
= i2c_add_driver(&selftest_i2c_dev_driver
);
1689 if (selftest(ret
== 0,
1690 "could not register selftest i2c device driver\n"))
1693 ret
= platform_driver_register(&selftest_i2c_bus_driver
);
1694 if (selftest(ret
== 0,
1695 "could not register selftest i2c bus driver\n"))
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"))
1708 static void of_selftest_overlay_i2c_cleanup(void)
1710 #if IS_ENABLED(CONFIG_I2C_MUX)
1711 i2c_del_driver(&selftest_i2c_mux_driver
);
1713 platform_driver_unregister(&selftest_i2c_bus_driver
);
1714 i2c_del_driver(&selftest_i2c_dev_driver
);
1717 static void of_selftest_overlay_i2c_12(void)
1721 /* device should enable */
1722 ret
= of_selftest_apply_overlay_check(12, 12, 0, 1, I2C_OVERLAY
);
1726 selftest(1, "overlay test %d passed\n", 12);
1729 /* test deactivation of device */
1730 static void of_selftest_overlay_i2c_13(void)
1734 /* device should disable */
1735 ret
= of_selftest_apply_overlay_check(13, 13, 1, 0, I2C_OVERLAY
);
1739 selftest(1, "overlay test %d passed\n", 13);
1742 /* just check for i2c mux existence */
1743 static void of_selftest_overlay_i2c_14(void)
1747 static void of_selftest_overlay_i2c_15(void)
1751 /* device should enable */
1752 ret
= of_selftest_apply_overlay_check(16, 15, 0, 1, I2C_OVERLAY
);
1756 selftest(1, "overlay test %d passed\n", 15);
1761 static inline void of_selftest_overlay_i2c_14(void) { }
1762 static inline void of_selftest_overlay_i2c_15(void) { }
1766 static void __init
of_selftest_overlay(void)
1768 struct device_node
*bus_np
= NULL
;
1771 ret
= platform_driver_register(&selftest_driver
);
1773 selftest(0, "could not register selftest driver\n");
1777 bus_np
= of_find_node_by_path(bus_path
);
1778 if (bus_np
== NULL
) {
1779 selftest(0, "could not find bus_path \"%s\"\n", bus_path
);
1783 ret
= of_platform_populate(bus_np
, of_default_bus_match_table
,
1786 selftest(0, "could not populate bus @ \"%s\"\n", bus_path
);
1790 if (!of_selftest_device_exists(100, PDEV_OVERLAY
)) {
1791 selftest(0, "could not find selftest0 @ \"%s\"\n",
1792 selftest_path(100, PDEV_OVERLAY
));
1796 if (of_selftest_device_exists(101, PDEV_OVERLAY
)) {
1797 selftest(0, "selftest1 @ \"%s\" should not exist\n",
1798 selftest_path(101, PDEV_OVERLAY
));
1802 selftest(1, "basic infrastructure of overlays passed");
1804 /* tests in sequence */
1805 of_selftest_overlay_0();
1806 of_selftest_overlay_1();
1807 of_selftest_overlay_2();
1808 of_selftest_overlay_3();
1809 of_selftest_overlay_4();
1810 of_selftest_overlay_5();
1811 of_selftest_overlay_6();
1812 of_selftest_overlay_8();
1814 of_selftest_overlay_10();
1815 of_selftest_overlay_11();
1817 #if IS_ENABLED(CONFIG_I2C)
1818 if (selftest(of_selftest_overlay_i2c_init() == 0, "i2c init failed\n"))
1821 of_selftest_overlay_i2c_12();
1822 of_selftest_overlay_i2c_13();
1823 of_selftest_overlay_i2c_14();
1824 of_selftest_overlay_i2c_15();
1826 of_selftest_overlay_i2c_cleanup();
1830 of_node_put(bus_np
);
1834 static inline void __init
of_selftest_overlay(void) { }
1837 static int __init
of_selftest(void)
1839 struct device_node
*np
;
1842 /* adding data for selftest */
1843 res
= selftest_data_add();
1847 of_aliases
= of_find_node_by_path("/aliases");
1849 np
= of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
1851 pr_info("No testcase data in device tree; not running tests\n");
1856 pr_info("start of selftest - you will see error messages\n");
1857 of_selftest_check_tree_linkage();
1858 of_selftest_check_phandles();
1859 of_selftest_find_node_by_name();
1860 of_selftest_dynamic();
1861 of_selftest_parse_phandle_with_args();
1862 of_selftest_property_string();
1863 of_selftest_property_copy();
1864 of_selftest_changeset();
1865 of_selftest_parse_interrupts();
1866 of_selftest_parse_interrupts_extended();
1867 of_selftest_match_node();
1868 of_selftest_platform_populate();
1869 of_selftest_overlay();
1871 /* Double check linkage after removing testcase data */
1872 of_selftest_check_tree_linkage();
1874 pr_info("end of selftest - %i passed, %i failed\n",
1875 selftest_results
.passed
, selftest_results
.failed
);
1879 late_initcall(of_selftest
);