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:1 got:%i\n", rc
);
382 rc
= of_property_match_string(np
, "phandle-list-names", "third");
383 selftest(rc
== 2, "third expected:2 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 n1
= __of_node_dup(NULL
, "/testcase-data/changeset/n1");
482 selftest(n1
, "testcase setup failure\n");
483 n2
= __of_node_dup(NULL
, "/testcase-data/changeset/n2");
484 selftest(n2
, "testcase setup failure\n");
485 n21
= __of_node_dup(NULL
, "%s/%s", "/testcase-data/changeset/n2", "n21");
486 selftest(n21
, "testcase setup failure %p\n", n21
);
487 nremove
= of_find_node_by_path("/testcase-data/changeset/node-remove");
488 selftest(nremove
, "testcase setup failure\n");
489 ppadd
= __of_prop_dup(&padd
, GFP_KERNEL
);
490 selftest(ppadd
, "testcase setup failure\n");
491 ppupdate
= __of_prop_dup(&pupdate
, GFP_KERNEL
);
492 selftest(ppupdate
, "testcase setup failure\n");
493 parent
= nremove
->parent
;
498 ppremove
= of_find_property(parent
, "prop-remove", NULL
);
499 selftest(ppremove
, "failed to find removal prop");
501 of_changeset_init(&chgset
);
502 selftest(!of_changeset_attach_node(&chgset
, n1
), "fail attach n1\n");
503 selftest(!of_changeset_attach_node(&chgset
, n2
), "fail attach n2\n");
504 selftest(!of_changeset_detach_node(&chgset
, nremove
), "fail remove node\n");
505 selftest(!of_changeset_attach_node(&chgset
, n21
), "fail attach n21\n");
506 selftest(!of_changeset_add_property(&chgset
, parent
, ppadd
), "fail add prop\n");
507 selftest(!of_changeset_update_property(&chgset
, parent
, ppupdate
), "fail update prop\n");
508 selftest(!of_changeset_remove_property(&chgset
, parent
, ppremove
), "fail remove prop\n");
509 mutex_lock(&of_mutex
);
510 selftest(!of_changeset_apply(&chgset
), "apply failed\n");
511 mutex_unlock(&of_mutex
);
513 /* Make sure node names are constructed correctly */
514 selftest((np
= of_find_node_by_path("/testcase-data/changeset/n2/n21")),
515 "'%s' not added\n", n21
->full_name
);
518 mutex_lock(&of_mutex
);
519 selftest(!of_changeset_revert(&chgset
), "revert failed\n");
520 mutex_unlock(&of_mutex
);
522 of_changeset_destroy(&chgset
);
526 static void __init
of_selftest_parse_interrupts(void)
528 struct device_node
*np
;
529 struct of_phandle_args args
;
532 np
= of_find_node_by_path("/testcase-data/interrupts/interrupts0");
534 pr_err("missing testcase data\n");
538 for (i
= 0; i
< 4; i
++) {
541 rc
= of_irq_parse_one(np
, i
, &args
);
544 passed
&= (args
.args_count
== 1);
545 passed
&= (args
.args
[0] == (i
+ 1));
547 selftest(passed
, "index %i - data error on node %s rc=%i\n",
548 i
, args
.np
->full_name
, rc
);
552 np
= of_find_node_by_path("/testcase-data/interrupts/interrupts1");
554 pr_err("missing testcase data\n");
558 for (i
= 0; i
< 4; i
++) {
561 rc
= of_irq_parse_one(np
, i
, &args
);
563 /* Test the values from tests-phandle.dtsi */
567 passed
&= (args
.args_count
== 1);
568 passed
&= (args
.args
[0] == 9);
572 passed
&= (args
.args_count
== 3);
573 passed
&= (args
.args
[0] == 10);
574 passed
&= (args
.args
[1] == 11);
575 passed
&= (args
.args
[2] == 12);
579 passed
&= (args
.args_count
== 2);
580 passed
&= (args
.args
[0] == 13);
581 passed
&= (args
.args
[1] == 14);
585 passed
&= (args
.args_count
== 2);
586 passed
&= (args
.args
[0] == 15);
587 passed
&= (args
.args
[1] == 16);
592 selftest(passed
, "index %i - data error on node %s rc=%i\n",
593 i
, args
.np
->full_name
, rc
);
598 static void __init
of_selftest_parse_interrupts_extended(void)
600 struct device_node
*np
;
601 struct of_phandle_args args
;
604 np
= of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
606 pr_err("missing testcase data\n");
610 for (i
= 0; i
< 7; i
++) {
612 rc
= of_irq_parse_one(np
, i
, &args
);
614 /* Test the values from tests-phandle.dtsi */
618 passed
&= (args
.args_count
== 1);
619 passed
&= (args
.args
[0] == 1);
623 passed
&= (args
.args_count
== 3);
624 passed
&= (args
.args
[0] == 2);
625 passed
&= (args
.args
[1] == 3);
626 passed
&= (args
.args
[2] == 4);
630 passed
&= (args
.args_count
== 2);
631 passed
&= (args
.args
[0] == 5);
632 passed
&= (args
.args
[1] == 6);
636 passed
&= (args
.args_count
== 1);
637 passed
&= (args
.args
[0] == 9);
641 passed
&= (args
.args_count
== 3);
642 passed
&= (args
.args
[0] == 10);
643 passed
&= (args
.args
[1] == 11);
644 passed
&= (args
.args
[2] == 12);
648 passed
&= (args
.args_count
== 2);
649 passed
&= (args
.args
[0] == 13);
650 passed
&= (args
.args
[1] == 14);
654 passed
&= (args
.args_count
== 1);
655 passed
&= (args
.args
[0] == 15);
661 selftest(passed
, "index %i - data error on node %s rc=%i\n",
662 i
, args
.np
->full_name
, rc
);
667 static struct of_device_id match_node_table
[] = {
668 { .data
= "A", .name
= "name0", }, /* Name alone is lowest priority */
669 { .data
= "B", .type
= "type1", }, /* followed by type alone */
671 { .data
= "Ca", .name
= "name2", .type
= "type1", }, /* followed by both together */
672 { .data
= "Cb", .name
= "name2", }, /* Only match when type doesn't match */
673 { .data
= "Cc", .name
= "name2", .type
= "type2", },
675 { .data
= "E", .compatible
= "compat3" },
676 { .data
= "G", .compatible
= "compat2", },
677 { .data
= "H", .compatible
= "compat2", .name
= "name5", },
678 { .data
= "I", .compatible
= "compat2", .type
= "type1", },
679 { .data
= "J", .compatible
= "compat2", .type
= "type1", .name
= "name8", },
680 { .data
= "K", .compatible
= "compat2", .name
= "name9", },
687 } match_node_tests
[] = {
688 { .path
= "/testcase-data/match-node/name0", .data
= "A", },
689 { .path
= "/testcase-data/match-node/name1", .data
= "B", },
690 { .path
= "/testcase-data/match-node/a/name2", .data
= "Ca", },
691 { .path
= "/testcase-data/match-node/b/name2", .data
= "Cb", },
692 { .path
= "/testcase-data/match-node/c/name2", .data
= "Cc", },
693 { .path
= "/testcase-data/match-node/name3", .data
= "E", },
694 { .path
= "/testcase-data/match-node/name4", .data
= "G", },
695 { .path
= "/testcase-data/match-node/name5", .data
= "H", },
696 { .path
= "/testcase-data/match-node/name6", .data
= "G", },
697 { .path
= "/testcase-data/match-node/name7", .data
= "I", },
698 { .path
= "/testcase-data/match-node/name8", .data
= "J", },
699 { .path
= "/testcase-data/match-node/name9", .data
= "K", },
702 static void __init
of_selftest_match_node(void)
704 struct device_node
*np
;
705 const struct of_device_id
*match
;
708 for (i
= 0; i
< ARRAY_SIZE(match_node_tests
); i
++) {
709 np
= of_find_node_by_path(match_node_tests
[i
].path
);
711 selftest(0, "missing testcase node %s\n",
712 match_node_tests
[i
].path
);
716 match
= of_match_node(match_node_table
, np
);
718 selftest(0, "%s didn't match anything\n",
719 match_node_tests
[i
].path
);
723 if (strcmp(match
->data
, match_node_tests
[i
].data
) != 0) {
724 selftest(0, "%s got wrong match. expected %s, got %s\n",
725 match_node_tests
[i
].path
, match_node_tests
[i
].data
,
726 (const char *)match
->data
);
729 selftest(1, "passed");
733 struct device test_bus
= {
734 .init_name
= "unittest-bus",
736 static void __init
of_selftest_platform_populate(void)
739 struct device_node
*np
, *child
, *grandchild
;
740 struct platform_device
*pdev
;
741 struct of_device_id match
[] = {
742 { .compatible
= "test-device", },
746 np
= of_find_node_by_path("/testcase-data");
747 of_platform_populate(np
, of_default_bus_match_table
, NULL
, NULL
);
749 /* Test that a missing irq domain returns -EPROBE_DEFER */
750 np
= of_find_node_by_path("/testcase-data/testcase-device1");
751 pdev
= of_find_device_by_node(np
);
752 selftest(pdev
, "device 1 creation failed\n");
754 irq
= platform_get_irq(pdev
, 0);
755 selftest(irq
== -EPROBE_DEFER
, "device deferred probe failed - %d\n", irq
);
757 /* Test that a parsing failure does not return -EPROBE_DEFER */
758 np
= of_find_node_by_path("/testcase-data/testcase-device2");
759 pdev
= of_find_device_by_node(np
);
760 selftest(pdev
, "device 2 creation failed\n");
761 irq
= platform_get_irq(pdev
, 0);
762 selftest(irq
< 0 && irq
!= -EPROBE_DEFER
, "device parsing error failed - %d\n", irq
);
764 if (selftest(np
= of_find_node_by_path("/testcase-data/platform-tests"),
765 "No testcase data in device tree\n"));
768 if (selftest(!(rc
= device_register(&test_bus
)),
769 "testbus registration failed; rc=%i\n", rc
));
772 for_each_child_of_node(np
, child
) {
773 of_platform_populate(child
, match
, NULL
, &test_bus
);
774 for_each_child_of_node(child
, grandchild
)
775 selftest(of_find_device_by_node(grandchild
),
776 "Could not create device for node '%s'\n",
780 of_platform_depopulate(&test_bus
);
781 for_each_child_of_node(np
, child
) {
782 for_each_child_of_node(child
, grandchild
)
783 selftest(!of_find_device_by_node(grandchild
),
784 "device didn't get destroyed '%s'\n",
788 device_unregister(&test_bus
);
793 * update_node_properties - adds the properties
794 * of np into dup node (present in live tree) and
795 * updates parent of children of np to dup.
797 * @np: node already present in live tree
798 * @dup: node present in live tree to be updated
800 static void update_node_properties(struct device_node
*np
,
801 struct device_node
*dup
)
803 struct property
*prop
;
804 struct device_node
*child
;
806 for_each_property_of_node(np
, prop
)
807 of_add_property(dup
, prop
);
809 for_each_child_of_node(np
, child
)
814 * attach_node_and_children - attaches nodes
815 * and its children to live tree
817 * @np: Node to attach to live tree
819 static int attach_node_and_children(struct device_node
*np
)
821 struct device_node
*next
, *dup
, *child
;
824 dup
= of_find_node_by_path(np
->full_name
);
826 update_node_properties(np
, dup
);
833 mutex_lock(&of_mutex
);
834 raw_spin_lock_irqsave(&devtree_lock
, flags
);
835 np
->sibling
= np
->parent
->child
;
836 np
->parent
->child
= np
;
837 of_node_clear_flag(np
, OF_DETACHED
);
838 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
840 __of_attach_node_sysfs(np
);
841 mutex_unlock(&of_mutex
);
844 next
= child
->sibling
;
845 attach_node_and_children(child
);
853 * selftest_data_add - Reads, copies data from
854 * linked tree and attaches it to the live tree
856 static int __init
selftest_data_add(void)
859 struct device_node
*selftest_data_node
, *np
;
860 extern uint8_t __dtb_testcases_begin
[];
861 extern uint8_t __dtb_testcases_end
[];
862 const int size
= __dtb_testcases_end
- __dtb_testcases_begin
;
866 pr_warn("%s: No testcase data to attach; not running tests\n",
872 selftest_data
= kmemdup(__dtb_testcases_begin
, size
, GFP_KERNEL
);
874 if (!selftest_data
) {
875 pr_warn("%s: Failed to allocate memory for selftest_data; "
876 "not running tests\n", __func__
);
879 of_fdt_unflatten_tree(selftest_data
, &selftest_data_node
);
880 if (!selftest_data_node
) {
881 pr_warn("%s: No tree to attach; not running tests\n", __func__
);
884 of_node_set_flag(selftest_data_node
, OF_DETACHED
);
885 rc
= of_resolve_phandles(selftest_data_node
);
887 pr_err("%s: Failed to resolve phandles (rc=%i)\n", __func__
, rc
);
892 of_root
= selftest_data_node
;
893 for_each_of_allnodes(np
)
894 __of_attach_node_sysfs(np
);
895 of_aliases
= of_find_node_by_path("/aliases");
896 of_chosen
= of_find_node_by_path("/chosen");
900 /* attach the sub-tree to live tree */
901 np
= selftest_data_node
->child
;
903 struct device_node
*next
= np
->sibling
;
904 np
->parent
= of_root
;
905 attach_node_and_children(np
);
911 #ifdef CONFIG_OF_OVERLAY
913 static int selftest_probe(struct platform_device
*pdev
)
915 struct device
*dev
= &pdev
->dev
;
916 struct device_node
*np
= dev
->of_node
;
919 dev_err(dev
, "No OF data for device\n");
924 dev_dbg(dev
, "%s for node @%s\n", __func__
, np
->full_name
);
926 of_platform_populate(np
, NULL
, NULL
, &pdev
->dev
);
931 static int selftest_remove(struct platform_device
*pdev
)
933 struct device
*dev
= &pdev
->dev
;
934 struct device_node
*np
= dev
->of_node
;
936 dev_dbg(dev
, "%s for node @%s\n", __func__
, np
->full_name
);
940 static struct of_device_id selftest_match
[] = {
941 { .compatible
= "selftest", },
945 static struct platform_driver selftest_driver
= {
946 .probe
= selftest_probe
,
947 .remove
= selftest_remove
,
950 .owner
= THIS_MODULE
,
951 .of_match_table
= of_match_ptr(selftest_match
),
955 /* get the platform device instantiated at the path */
956 static struct platform_device
*of_path_to_platform_device(const char *path
)
958 struct device_node
*np
;
959 struct platform_device
*pdev
;
961 np
= of_find_node_by_path(path
);
965 pdev
= of_find_device_by_node(np
);
971 /* find out if a platform device exists at that path */
972 static int of_path_platform_device_exists(const char *path
)
974 struct platform_device
*pdev
;
976 pdev
= of_path_to_platform_device(path
);
977 platform_device_put(pdev
);
981 #if IS_BUILTIN(CONFIG_I2C)
983 /* get the i2c client device instantiated at the path */
984 static struct i2c_client
*of_path_to_i2c_client(const char *path
)
986 struct device_node
*np
;
987 struct i2c_client
*client
;
989 np
= of_find_node_by_path(path
);
993 client
= of_find_i2c_device_by_node(np
);
999 /* find out if a i2c client device exists at that path */
1000 static int of_path_i2c_client_exists(const char *path
)
1002 struct i2c_client
*client
;
1004 client
= of_path_to_i2c_client(path
);
1006 put_device(&client
->dev
);
1007 return client
!= NULL
;
1010 static int of_path_i2c_client_exists(const char *path
)
1021 static int of_path_device_type_exists(const char *path
,
1022 enum overlay_type ovtype
)
1026 return of_path_platform_device_exists(path
);
1028 return of_path_i2c_client_exists(path
);
1033 static const char *selftest_path(int nr
, enum overlay_type ovtype
)
1036 static char buf
[256];
1040 base
= "/testcase-data/overlay-node/test-bus";
1043 base
= "/testcase-data/overlay-node/test-bus/i2c-test-bus";
1049 snprintf(buf
, sizeof(buf
) - 1, "%s/test-selftest%d", base
, nr
);
1050 buf
[sizeof(buf
) - 1] = '\0';
1054 static int of_selftest_device_exists(int selftest_nr
, enum overlay_type ovtype
)
1058 path
= selftest_path(selftest_nr
, ovtype
);
1062 return of_path_platform_device_exists(path
);
1064 return of_path_i2c_client_exists(path
);
1069 static const char *overlay_path(int nr
)
1071 static char buf
[256];
1073 snprintf(buf
, sizeof(buf
) - 1,
1074 "/testcase-data/overlay%d", nr
);
1075 buf
[sizeof(buf
) - 1] = '\0';
1080 static const char *bus_path
= "/testcase-data/overlay-node/test-bus";
1082 static int of_selftest_apply_overlay(int selftest_nr
, int overlay_nr
,
1085 struct device_node
*np
= NULL
;
1088 np
= of_find_node_by_path(overlay_path(overlay_nr
));
1090 selftest(0, "could not find overlay node @\"%s\"\n",
1091 overlay_path(overlay_nr
));
1096 ret
= of_overlay_create(np
);
1098 selftest(0, "could not create overlay from \"%s\"\n",
1099 overlay_path(overlay_nr
));
1115 /* apply an overlay while checking before and after states */
1116 static int of_selftest_apply_overlay_check(int overlay_nr
, int selftest_nr
,
1117 int before
, int after
, enum overlay_type ovtype
)
1121 /* selftest device must not be in before state */
1122 if (of_selftest_device_exists(selftest_nr
, ovtype
) != before
) {
1123 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1124 overlay_path(overlay_nr
),
1125 selftest_path(selftest_nr
, ovtype
),
1126 !before
? "enabled" : "disabled");
1130 ret
= of_selftest_apply_overlay(overlay_nr
, selftest_nr
, NULL
);
1132 /* of_selftest_apply_overlay already called selftest() */
1136 /* selftest device must be to set to after state */
1137 if (of_selftest_device_exists(selftest_nr
, ovtype
) != after
) {
1138 selftest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n",
1139 overlay_path(overlay_nr
),
1140 selftest_path(selftest_nr
, ovtype
),
1141 !after
? "enabled" : "disabled");
1148 /* apply an overlay and then revert it while checking before, after states */
1149 static int of_selftest_apply_revert_overlay_check(int overlay_nr
,
1150 int selftest_nr
, int before
, int after
,
1151 enum overlay_type ovtype
)
1155 /* selftest device must be in before state */
1156 if (of_selftest_device_exists(selftest_nr
, ovtype
) != before
) {
1157 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1158 overlay_path(overlay_nr
),
1159 selftest_path(selftest_nr
, ovtype
),
1160 !before
? "enabled" : "disabled");
1164 /* apply the overlay */
1165 ret
= of_selftest_apply_overlay(overlay_nr
, selftest_nr
, &ov_id
);
1167 /* of_selftest_apply_overlay already called selftest() */
1171 /* selftest device must be in after state */
1172 if (of_selftest_device_exists(selftest_nr
, ovtype
) != after
) {
1173 selftest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n",
1174 overlay_path(overlay_nr
),
1175 selftest_path(selftest_nr
, ovtype
),
1176 !after
? "enabled" : "disabled");
1180 ret
= of_overlay_destroy(ov_id
);
1182 selftest(0, "overlay @\"%s\" failed to be destroyed @\"%s\"\n",
1183 overlay_path(overlay_nr
),
1184 selftest_path(selftest_nr
, ovtype
));
1188 /* selftest device must be again in before state */
1189 if (of_selftest_device_exists(selftest_nr
, PDEV_OVERLAY
) != before
) {
1190 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1191 overlay_path(overlay_nr
),
1192 selftest_path(selftest_nr
, ovtype
),
1193 !before
? "enabled" : "disabled");
1200 /* test activation of device */
1201 static void of_selftest_overlay_0(void)
1205 /* device should enable */
1206 ret
= of_selftest_apply_overlay_check(0, 0, 0, 1, PDEV_OVERLAY
);
1210 selftest(1, "overlay test %d passed\n", 0);
1213 /* test deactivation of device */
1214 static void of_selftest_overlay_1(void)
1218 /* device should disable */
1219 ret
= of_selftest_apply_overlay_check(1, 1, 1, 0, PDEV_OVERLAY
);
1223 selftest(1, "overlay test %d passed\n", 1);
1226 /* test activation of device */
1227 static void of_selftest_overlay_2(void)
1231 /* device should enable */
1232 ret
= of_selftest_apply_overlay_check(2, 2, 0, 1, PDEV_OVERLAY
);
1236 selftest(1, "overlay test %d passed\n", 2);
1239 /* test deactivation of device */
1240 static void of_selftest_overlay_3(void)
1244 /* device should disable */
1245 ret
= of_selftest_apply_overlay_check(3, 3, 1, 0, PDEV_OVERLAY
);
1249 selftest(1, "overlay test %d passed\n", 3);
1252 /* test activation of a full device node */
1253 static void of_selftest_overlay_4(void)
1257 /* device should disable */
1258 ret
= of_selftest_apply_overlay_check(4, 4, 0, 1, PDEV_OVERLAY
);
1262 selftest(1, "overlay test %d passed\n", 4);
1265 /* test overlay apply/revert sequence */
1266 static void of_selftest_overlay_5(void)
1270 /* device should disable */
1271 ret
= of_selftest_apply_revert_overlay_check(5, 5, 0, 1, PDEV_OVERLAY
);
1275 selftest(1, "overlay test %d passed\n", 5);
1278 /* test overlay application in sequence */
1279 static void of_selftest_overlay_6(void)
1281 struct device_node
*np
;
1282 int ret
, i
, ov_id
[2];
1283 int overlay_nr
= 6, selftest_nr
= 6;
1284 int before
= 0, after
= 1;
1286 /* selftest device must be in before state */
1287 for (i
= 0; i
< 2; i
++) {
1288 if (of_selftest_device_exists(selftest_nr
+ i
, PDEV_OVERLAY
)
1290 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1291 overlay_path(overlay_nr
+ i
),
1292 selftest_path(selftest_nr
+ i
,
1294 !before
? "enabled" : "disabled");
1299 /* apply the overlays */
1300 for (i
= 0; i
< 2; i
++) {
1302 np
= of_find_node_by_path(overlay_path(overlay_nr
+ i
));
1304 selftest(0, "could not find overlay node @\"%s\"\n",
1305 overlay_path(overlay_nr
+ i
));
1309 ret
= of_overlay_create(np
);
1311 selftest(0, "could not create overlay from \"%s\"\n",
1312 overlay_path(overlay_nr
+ i
));
1318 for (i
= 0; i
< 2; i
++) {
1319 /* selftest device must be in after state */
1320 if (of_selftest_device_exists(selftest_nr
+ i
, PDEV_OVERLAY
)
1322 selftest(0, "overlay @\"%s\" failed @\"%s\" %s\n",
1323 overlay_path(overlay_nr
+ i
),
1324 selftest_path(selftest_nr
+ i
,
1326 !after
? "enabled" : "disabled");
1331 for (i
= 1; i
>= 0; i
--) {
1332 ret
= of_overlay_destroy(ov_id
[i
]);
1334 selftest(0, "overlay @\"%s\" failed destroy @\"%s\"\n",
1335 overlay_path(overlay_nr
+ i
),
1336 selftest_path(selftest_nr
+ i
,
1342 for (i
= 0; i
< 2; i
++) {
1343 /* selftest device must be again in before state */
1344 if (of_selftest_device_exists(selftest_nr
+ i
, PDEV_OVERLAY
)
1346 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1347 overlay_path(overlay_nr
+ i
),
1348 selftest_path(selftest_nr
+ i
,
1350 !before
? "enabled" : "disabled");
1355 selftest(1, "overlay test %d passed\n", 6);
1358 /* test overlay application in sequence */
1359 static void of_selftest_overlay_8(void)
1361 struct device_node
*np
;
1362 int ret
, i
, ov_id
[2];
1363 int overlay_nr
= 8, selftest_nr
= 8;
1365 /* we don't care about device state in this test */
1367 /* apply the overlays */
1368 for (i
= 0; i
< 2; i
++) {
1370 np
= of_find_node_by_path(overlay_path(overlay_nr
+ i
));
1372 selftest(0, "could not find overlay node @\"%s\"\n",
1373 overlay_path(overlay_nr
+ i
));
1377 ret
= of_overlay_create(np
);
1379 selftest(0, "could not create overlay from \"%s\"\n",
1380 overlay_path(overlay_nr
+ i
));
1386 /* now try to remove first overlay (it should fail) */
1387 ret
= of_overlay_destroy(ov_id
[0]);
1389 selftest(0, "overlay @\"%s\" was destroyed @\"%s\"\n",
1390 overlay_path(overlay_nr
+ 0),
1391 selftest_path(selftest_nr
,
1396 /* removing them in order should work */
1397 for (i
= 1; i
>= 0; i
--) {
1398 ret
= of_overlay_destroy(ov_id
[i
]);
1400 selftest(0, "overlay @\"%s\" not destroyed @\"%s\"\n",
1401 overlay_path(overlay_nr
+ i
),
1402 selftest_path(selftest_nr
,
1408 selftest(1, "overlay test %d passed\n", 8);
1411 /* test insertion of a bus with parent devices */
1412 static void of_selftest_overlay_10(void)
1417 /* device should disable */
1418 ret
= of_selftest_apply_overlay_check(10, 10, 0, 1, PDEV_OVERLAY
);
1419 if (selftest(ret
== 0,
1420 "overlay test %d failed; overlay application\n", 10))
1423 child_path
= kasprintf(GFP_KERNEL
, "%s/test-selftest101",
1424 selftest_path(10, PDEV_OVERLAY
));
1425 if (selftest(child_path
, "overlay test %d failed; kasprintf\n", 10))
1428 ret
= of_path_device_type_exists(child_path
, PDEV_OVERLAY
);
1430 if (selftest(ret
, "overlay test %d failed; no child device\n", 10))
1434 /* test insertion of a bus with parent devices (and revert) */
1435 static void of_selftest_overlay_11(void)
1439 /* device should disable */
1440 ret
= of_selftest_apply_revert_overlay_check(11, 11, 0, 1,
1442 if (selftest(ret
== 0,
1443 "overlay test %d failed; overlay application\n", 11))
1447 #if IS_BUILTIN(CONFIG_I2C) && IS_ENABLED(CONFIG_OF_OVERLAY)
1449 struct selftest_i2c_bus_data
{
1450 struct platform_device
*pdev
;
1451 struct i2c_adapter adap
;
1454 static int selftest_i2c_master_xfer(struct i2c_adapter
*adap
,
1455 struct i2c_msg
*msgs
, int num
)
1457 struct selftest_i2c_bus_data
*std
= i2c_get_adapdata(adap
);
1464 static u32
selftest_i2c_functionality(struct i2c_adapter
*adap
)
1466 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
;
1469 static const struct i2c_algorithm selftest_i2c_algo
= {
1470 .master_xfer
= selftest_i2c_master_xfer
,
1471 .functionality
= selftest_i2c_functionality
,
1474 static int selftest_i2c_bus_probe(struct platform_device
*pdev
)
1476 struct device
*dev
= &pdev
->dev
;
1477 struct device_node
*np
= dev
->of_node
;
1478 struct selftest_i2c_bus_data
*std
;
1479 struct i2c_adapter
*adap
;
1483 dev_err(dev
, "No OF data for device\n");
1488 dev_dbg(dev
, "%s for node @%s\n", __func__
, np
->full_name
);
1490 std
= devm_kzalloc(dev
, sizeof(*std
), GFP_KERNEL
);
1492 dev_err(dev
, "Failed to allocate selftest i2c data\n");
1496 /* link them together */
1498 platform_set_drvdata(pdev
, std
);
1501 i2c_set_adapdata(adap
, std
);
1503 strlcpy(adap
->name
, pdev
->name
, sizeof(adap
->name
));
1504 adap
->class = I2C_CLASS_DEPRECATED
;
1505 adap
->algo
= &selftest_i2c_algo
;
1506 adap
->dev
.parent
= dev
;
1507 adap
->dev
.of_node
= dev
->of_node
;
1508 adap
->timeout
= 5 * HZ
;
1511 ret
= i2c_add_numbered_adapter(adap
);
1513 dev_err(dev
, "Failed to add I2C adapter\n");
1520 static int selftest_i2c_bus_remove(struct platform_device
*pdev
)
1522 struct device
*dev
= &pdev
->dev
;
1523 struct device_node
*np
= dev
->of_node
;
1524 struct selftest_i2c_bus_data
*std
= platform_get_drvdata(pdev
);
1526 dev_dbg(dev
, "%s for node @%s\n", __func__
, np
->full_name
);
1527 i2c_del_adapter(&std
->adap
);
1532 static struct of_device_id selftest_i2c_bus_match
[] = {
1533 { .compatible
= "selftest-i2c-bus", },
1537 static struct platform_driver selftest_i2c_bus_driver
= {
1538 .probe
= selftest_i2c_bus_probe
,
1539 .remove
= selftest_i2c_bus_remove
,
1541 .name
= "selftest-i2c-bus",
1542 .of_match_table
= of_match_ptr(selftest_i2c_bus_match
),
1546 static int selftest_i2c_dev_probe(struct i2c_client
*client
,
1547 const struct i2c_device_id
*id
)
1549 struct device
*dev
= &client
->dev
;
1550 struct device_node
*np
= client
->dev
.of_node
;
1553 dev_err(dev
, "No OF node\n");
1557 dev_dbg(dev
, "%s for node @%s\n", __func__
, np
->full_name
);
1562 static int selftest_i2c_dev_remove(struct i2c_client
*client
)
1564 struct device
*dev
= &client
->dev
;
1565 struct device_node
*np
= client
->dev
.of_node
;
1567 dev_dbg(dev
, "%s for node @%s\n", __func__
, np
->full_name
);
1571 static const struct i2c_device_id selftest_i2c_dev_id
[] = {
1572 { .name
= "selftest-i2c-dev" },
1576 static struct i2c_driver selftest_i2c_dev_driver
= {
1578 .name
= "selftest-i2c-dev",
1579 .owner
= THIS_MODULE
,
1581 .probe
= selftest_i2c_dev_probe
,
1582 .remove
= selftest_i2c_dev_remove
,
1583 .id_table
= selftest_i2c_dev_id
,
1586 #if IS_BUILTIN(CONFIG_I2C_MUX)
1588 struct selftest_i2c_mux_data
{
1590 struct i2c_adapter
*adap
[];
1593 static int selftest_i2c_mux_select_chan(struct i2c_adapter
*adap
,
1594 void *client
, u32 chan
)
1599 static int selftest_i2c_mux_probe(struct i2c_client
*client
,
1600 const struct i2c_device_id
*id
)
1602 int ret
, i
, nchans
, size
;
1603 struct device
*dev
= &client
->dev
;
1604 struct i2c_adapter
*adap
= to_i2c_adapter(dev
->parent
);
1605 struct device_node
*np
= client
->dev
.of_node
, *child
;
1606 struct selftest_i2c_mux_data
*stm
;
1609 dev_dbg(dev
, "%s for node @%s\n", __func__
, np
->full_name
);
1612 dev_err(dev
, "No OF node\n");
1617 for_each_child_of_node(np
, child
) {
1618 ret
= of_property_read_u32(child
, "reg", ®
);
1621 if (max_reg
== (u32
)-1 || reg
> max_reg
)
1624 nchans
= max_reg
== (u32
)-1 ? 0 : max_reg
+ 1;
1626 dev_err(dev
, "No channels\n");
1630 size
= offsetof(struct selftest_i2c_mux_data
, adap
[nchans
]);
1631 stm
= devm_kzalloc(dev
, size
, GFP_KERNEL
);
1633 dev_err(dev
, "Out of memory\n");
1636 stm
->nchans
= nchans
;
1637 for (i
= 0; i
< nchans
; i
++) {
1638 stm
->adap
[i
] = i2c_add_mux_adapter(adap
, dev
, client
,
1639 0, i
, 0, selftest_i2c_mux_select_chan
, NULL
);
1640 if (!stm
->adap
[i
]) {
1641 dev_err(dev
, "Failed to register mux #%d\n", i
);
1642 for (i
--; i
>= 0; i
--)
1643 i2c_del_mux_adapter(stm
->adap
[i
]);
1648 i2c_set_clientdata(client
, stm
);
1653 static int selftest_i2c_mux_remove(struct i2c_client
*client
)
1655 struct device
*dev
= &client
->dev
;
1656 struct device_node
*np
= client
->dev
.of_node
;
1657 struct selftest_i2c_mux_data
*stm
= i2c_get_clientdata(client
);
1660 dev_dbg(dev
, "%s for node @%s\n", __func__
, np
->full_name
);
1661 for (i
= stm
->nchans
- 1; i
>= 0; i
--)
1662 i2c_del_mux_adapter(stm
->adap
[i
]);
1666 static const struct i2c_device_id selftest_i2c_mux_id
[] = {
1667 { .name
= "selftest-i2c-mux" },
1671 static struct i2c_driver selftest_i2c_mux_driver
= {
1673 .name
= "selftest-i2c-mux",
1674 .owner
= THIS_MODULE
,
1676 .probe
= selftest_i2c_mux_probe
,
1677 .remove
= selftest_i2c_mux_remove
,
1678 .id_table
= selftest_i2c_mux_id
,
1683 static int of_selftest_overlay_i2c_init(void)
1687 ret
= i2c_add_driver(&selftest_i2c_dev_driver
);
1688 if (selftest(ret
== 0,
1689 "could not register selftest i2c device driver\n"))
1692 ret
= platform_driver_register(&selftest_i2c_bus_driver
);
1693 if (selftest(ret
== 0,
1694 "could not register selftest i2c bus driver\n"))
1697 #if IS_BUILTIN(CONFIG_I2C_MUX)
1698 ret
= i2c_add_driver(&selftest_i2c_mux_driver
);
1699 if (selftest(ret
== 0,
1700 "could not register selftest i2c mux driver\n"))
1707 static void of_selftest_overlay_i2c_cleanup(void)
1709 #if IS_BUILTIN(CONFIG_I2C_MUX)
1710 i2c_del_driver(&selftest_i2c_mux_driver
);
1712 platform_driver_unregister(&selftest_i2c_bus_driver
);
1713 i2c_del_driver(&selftest_i2c_dev_driver
);
1716 static void of_selftest_overlay_i2c_12(void)
1720 /* device should enable */
1721 ret
= of_selftest_apply_overlay_check(12, 12, 0, 1, I2C_OVERLAY
);
1725 selftest(1, "overlay test %d passed\n", 12);
1728 /* test deactivation of device */
1729 static void of_selftest_overlay_i2c_13(void)
1733 /* device should disable */
1734 ret
= of_selftest_apply_overlay_check(13, 13, 1, 0, I2C_OVERLAY
);
1738 selftest(1, "overlay test %d passed\n", 13);
1741 /* just check for i2c mux existence */
1742 static void of_selftest_overlay_i2c_14(void)
1746 static void of_selftest_overlay_i2c_15(void)
1750 /* device should enable */
1751 ret
= of_selftest_apply_overlay_check(16, 15, 0, 1, I2C_OVERLAY
);
1755 selftest(1, "overlay test %d passed\n", 15);
1760 static inline void of_selftest_overlay_i2c_14(void) { }
1761 static inline void of_selftest_overlay_i2c_15(void) { }
1765 static void __init
of_selftest_overlay(void)
1767 struct device_node
*bus_np
= NULL
;
1770 ret
= platform_driver_register(&selftest_driver
);
1772 selftest(0, "could not register selftest driver\n");
1776 bus_np
= of_find_node_by_path(bus_path
);
1777 if (bus_np
== NULL
) {
1778 selftest(0, "could not find bus_path \"%s\"\n", bus_path
);
1782 ret
= of_platform_populate(bus_np
, of_default_bus_match_table
,
1785 selftest(0, "could not populate bus @ \"%s\"\n", bus_path
);
1789 if (!of_selftest_device_exists(100, PDEV_OVERLAY
)) {
1790 selftest(0, "could not find selftest0 @ \"%s\"\n",
1791 selftest_path(100, PDEV_OVERLAY
));
1795 if (of_selftest_device_exists(101, PDEV_OVERLAY
)) {
1796 selftest(0, "selftest1 @ \"%s\" should not exist\n",
1797 selftest_path(101, PDEV_OVERLAY
));
1801 selftest(1, "basic infrastructure of overlays passed");
1803 /* tests in sequence */
1804 of_selftest_overlay_0();
1805 of_selftest_overlay_1();
1806 of_selftest_overlay_2();
1807 of_selftest_overlay_3();
1808 of_selftest_overlay_4();
1809 of_selftest_overlay_5();
1810 of_selftest_overlay_6();
1811 of_selftest_overlay_8();
1813 of_selftest_overlay_10();
1814 of_selftest_overlay_11();
1816 #if IS_BUILTIN(CONFIG_I2C)
1817 if (selftest(of_selftest_overlay_i2c_init() == 0, "i2c init failed\n"))
1820 of_selftest_overlay_i2c_12();
1821 of_selftest_overlay_i2c_13();
1822 of_selftest_overlay_i2c_14();
1823 of_selftest_overlay_i2c_15();
1825 of_selftest_overlay_i2c_cleanup();
1829 of_node_put(bus_np
);
1833 static inline void __init
of_selftest_overlay(void) { }
1836 static int __init
of_selftest(void)
1838 struct device_node
*np
;
1841 /* adding data for selftest */
1842 res
= selftest_data_add();
1846 of_aliases
= of_find_node_by_path("/aliases");
1848 np
= of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
1850 pr_info("No testcase data in device tree; not running tests\n");
1855 pr_info("start of selftest - you will see error messages\n");
1856 of_selftest_check_tree_linkage();
1857 of_selftest_check_phandles();
1858 of_selftest_find_node_by_name();
1859 of_selftest_dynamic();
1860 of_selftest_parse_phandle_with_args();
1861 of_selftest_property_string();
1862 of_selftest_property_copy();
1863 of_selftest_changeset();
1864 of_selftest_parse_interrupts();
1865 of_selftest_parse_interrupts_extended();
1866 of_selftest_match_node();
1867 of_selftest_platform_populate();
1868 of_selftest_overlay();
1870 /* Double check linkage after removing testcase data */
1871 of_selftest_check_tree_linkage();
1873 pr_info("end of selftest - %i passed, %i failed\n",
1874 selftest_results
.passed
, selftest_results
.failed
);
1878 late_initcall(of_selftest
);