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:test/option", &options
);
96 selftest(np
&& !strcmp("test/option", options
),
97 "option path test, subcase #1 failed\n");
100 np
= of_find_node_opts_by_path("/testcase-data/testcase-device1:test/option", &options
);
101 selftest(np
&& !strcmp("test/option", options
),
102 "option path test, subcase #2 failed\n");
105 np
= of_find_node_opts_by_path("/testcase-data:testoption", NULL
);
106 selftest(np
, "NULL option path test failed\n");
109 np
= of_find_node_opts_by_path("testcase-alias:testaliasoption",
111 selftest(np
&& !strcmp("testaliasoption", options
),
112 "option alias path test failed\n");
115 np
= of_find_node_opts_by_path("testcase-alias:test/alias/option",
117 selftest(np
&& !strcmp("test/alias/option", options
),
118 "option alias path test, subcase #1 failed\n");
121 np
= of_find_node_opts_by_path("testcase-alias:testaliasoption", NULL
);
122 selftest(np
, "NULL option alias path test failed\n");
125 options
= "testoption";
126 np
= of_find_node_opts_by_path("testcase-alias", &options
);
127 selftest(np
&& !options
, "option clearing test failed\n");
130 options
= "testoption";
131 np
= of_find_node_opts_by_path("/", &options
);
132 selftest(np
&& !options
, "option clearing root node test failed\n");
136 static void __init
of_selftest_dynamic(void)
138 struct device_node
*np
;
139 struct property
*prop
;
141 np
= of_find_node_by_path("/testcase-data");
143 pr_err("missing testcase data\n");
147 /* Array of 4 properties for the purpose of testing */
148 prop
= kzalloc(sizeof(*prop
) * 4, GFP_KERNEL
);
150 selftest(0, "kzalloc() failed\n");
154 /* Add a new property - should pass*/
155 prop
->name
= "new-property";
156 prop
->value
= "new-property-data";
157 prop
->length
= strlen(prop
->value
);
158 selftest(of_add_property(np
, prop
) == 0, "Adding a new property failed\n");
160 /* Try to add an existing property - should fail */
162 prop
->name
= "new-property";
163 prop
->value
= "new-property-data-should-fail";
164 prop
->length
= strlen(prop
->value
);
165 selftest(of_add_property(np
, prop
) != 0,
166 "Adding an existing property should have failed\n");
168 /* Try to modify an existing property - should pass */
169 prop
->value
= "modify-property-data-should-pass";
170 prop
->length
= strlen(prop
->value
);
171 selftest(of_update_property(np
, prop
) == 0,
172 "Updating an existing property should have passed\n");
174 /* Try to modify non-existent property - should pass*/
176 prop
->name
= "modify-property";
177 prop
->value
= "modify-missing-property-data-should-pass";
178 prop
->length
= strlen(prop
->value
);
179 selftest(of_update_property(np
, prop
) == 0,
180 "Updating a missing property should have passed\n");
182 /* Remove property - should pass */
183 selftest(of_remove_property(np
, prop
) == 0,
184 "Removing a property should have passed\n");
186 /* Adding very large property - should pass */
188 prop
->name
= "large-property-PAGE_SIZEx8";
189 prop
->length
= PAGE_SIZE
* 8;
190 prop
->value
= kzalloc(prop
->length
, GFP_KERNEL
);
191 selftest(prop
->value
!= NULL
, "Unable to allocate large buffer\n");
193 selftest(of_add_property(np
, prop
) == 0,
194 "Adding a large property should have passed\n");
197 static int __init
of_selftest_check_node_linkage(struct device_node
*np
)
199 struct device_node
*child
;
202 for_each_child_of_node(np
, child
) {
203 if (child
->parent
!= np
) {
204 pr_err("Child node %s links to wrong parent %s\n",
205 child
->name
, np
->name
);
209 rc
= of_selftest_check_node_linkage(child
);
218 static void __init
of_selftest_check_tree_linkage(void)
220 struct device_node
*np
;
221 int allnode_count
= 0, child_count
;
226 for_each_of_allnodes(np
)
228 child_count
= of_selftest_check_node_linkage(of_root
);
230 selftest(child_count
> 0, "Device node data structure is corrupted\n");
231 selftest(child_count
== allnode_count
, "allnodes list size (%i) doesn't match"
232 "sibling lists size (%i)\n", allnode_count
, child_count
);
233 pr_debug("allnodes list size (%i); sibling lists size (%i)\n", allnode_count
, child_count
);
237 struct hlist_node node
;
238 struct device_node
*np
;
241 static DEFINE_HASHTABLE(phandle_ht
, 8);
242 static void __init
of_selftest_check_phandles(void)
244 struct device_node
*np
;
245 struct node_hash
*nh
;
246 struct hlist_node
*tmp
;
247 int i
, dup_count
= 0, phandle_count
= 0;
249 for_each_of_allnodes(np
) {
253 hash_for_each_possible(phandle_ht
, nh
, node
, np
->phandle
) {
254 if (nh
->np
->phandle
== np
->phandle
) {
255 pr_info("Duplicate phandle! %i used by %s and %s\n",
256 np
->phandle
, nh
->np
->full_name
, np
->full_name
);
262 nh
= kzalloc(sizeof(*nh
), GFP_KERNEL
);
267 hash_add(phandle_ht
, &nh
->node
, np
->phandle
);
270 selftest(dup_count
== 0, "Found %i duplicates in %i phandles\n",
271 dup_count
, phandle_count
);
274 hash_for_each_safe(phandle_ht
, i
, tmp
, nh
, node
) {
280 static void __init
of_selftest_parse_phandle_with_args(void)
282 struct device_node
*np
;
283 struct of_phandle_args args
;
286 np
= of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
288 pr_err("missing testcase data\n");
292 rc
= of_count_phandle_with_args(np
, "phandle-list", "#phandle-cells");
293 selftest(rc
== 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc
);
295 for (i
= 0; i
< 8; i
++) {
297 rc
= of_parse_phandle_with_args(np
, "phandle-list",
298 "#phandle-cells", i
, &args
);
300 /* Test the values from tests-phandle.dtsi */
304 passed
&= (args
.args_count
== 1);
305 passed
&= (args
.args
[0] == (i
+ 1));
309 passed
&= (args
.args_count
== 2);
310 passed
&= (args
.args
[0] == (i
+ 1));
311 passed
&= (args
.args
[1] == 0);
314 passed
&= (rc
== -ENOENT
);
318 passed
&= (args
.args_count
== 3);
319 passed
&= (args
.args
[0] == (i
+ 1));
320 passed
&= (args
.args
[1] == 4);
321 passed
&= (args
.args
[2] == 3);
325 passed
&= (args
.args_count
== 2);
326 passed
&= (args
.args
[0] == (i
+ 1));
327 passed
&= (args
.args
[1] == 100);
331 passed
&= (args
.args_count
== 0);
335 passed
&= (args
.args_count
== 1);
336 passed
&= (args
.args
[0] == (i
+ 1));
339 passed
&= (rc
== -ENOENT
);
345 selftest(passed
, "index %i - data error on node %s rc=%i\n",
346 i
, args
.np
->full_name
, rc
);
349 /* Check for missing list property */
350 rc
= of_parse_phandle_with_args(np
, "phandle-list-missing",
351 "#phandle-cells", 0, &args
);
352 selftest(rc
== -ENOENT
, "expected:%i got:%i\n", -ENOENT
, rc
);
353 rc
= of_count_phandle_with_args(np
, "phandle-list-missing",
355 selftest(rc
== -ENOENT
, "expected:%i got:%i\n", -ENOENT
, rc
);
357 /* Check for missing cells property */
358 rc
= of_parse_phandle_with_args(np
, "phandle-list",
359 "#phandle-cells-missing", 0, &args
);
360 selftest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
361 rc
= of_count_phandle_with_args(np
, "phandle-list",
362 "#phandle-cells-missing");
363 selftest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
365 /* Check for bad phandle in list */
366 rc
= of_parse_phandle_with_args(np
, "phandle-list-bad-phandle",
367 "#phandle-cells", 0, &args
);
368 selftest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
369 rc
= of_count_phandle_with_args(np
, "phandle-list-bad-phandle",
371 selftest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
373 /* Check for incorrectly formed argument list */
374 rc
= of_parse_phandle_with_args(np
, "phandle-list-bad-args",
375 "#phandle-cells", 1, &args
);
376 selftest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
377 rc
= of_count_phandle_with_args(np
, "phandle-list-bad-args",
379 selftest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
382 static void __init
of_selftest_property_string(void)
384 const char *strings
[4];
385 struct device_node
*np
;
388 np
= of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
390 pr_err("No testcase data in device tree\n");
394 rc
= of_property_match_string(np
, "phandle-list-names", "first");
395 selftest(rc
== 0, "first expected:0 got:%i\n", rc
);
396 rc
= of_property_match_string(np
, "phandle-list-names", "second");
397 selftest(rc
== 1, "second expected:1 got:%i\n", rc
);
398 rc
= of_property_match_string(np
, "phandle-list-names", "third");
399 selftest(rc
== 2, "third expected:2 got:%i\n", rc
);
400 rc
= of_property_match_string(np
, "phandle-list-names", "fourth");
401 selftest(rc
== -ENODATA
, "unmatched string; rc=%i\n", rc
);
402 rc
= of_property_match_string(np
, "missing-property", "blah");
403 selftest(rc
== -EINVAL
, "missing property; rc=%i\n", rc
);
404 rc
= of_property_match_string(np
, "empty-property", "blah");
405 selftest(rc
== -ENODATA
, "empty property; rc=%i\n", rc
);
406 rc
= of_property_match_string(np
, "unterminated-string", "blah");
407 selftest(rc
== -EILSEQ
, "unterminated string; rc=%i\n", rc
);
409 /* of_property_count_strings() tests */
410 rc
= of_property_count_strings(np
, "string-property");
411 selftest(rc
== 1, "Incorrect string count; rc=%i\n", rc
);
412 rc
= of_property_count_strings(np
, "phandle-list-names");
413 selftest(rc
== 3, "Incorrect string count; rc=%i\n", rc
);
414 rc
= of_property_count_strings(np
, "unterminated-string");
415 selftest(rc
== -EILSEQ
, "unterminated string; rc=%i\n", rc
);
416 rc
= of_property_count_strings(np
, "unterminated-string-list");
417 selftest(rc
== -EILSEQ
, "unterminated string array; rc=%i\n", rc
);
419 /* of_property_read_string_index() tests */
420 rc
= of_property_read_string_index(np
, "string-property", 0, strings
);
421 selftest(rc
== 0 && !strcmp(strings
[0], "foobar"), "of_property_read_string_index() failure; rc=%i\n", rc
);
423 rc
= of_property_read_string_index(np
, "string-property", 1, strings
);
424 selftest(rc
== -ENODATA
&& strings
[0] == NULL
, "of_property_read_string_index() failure; rc=%i\n", rc
);
425 rc
= of_property_read_string_index(np
, "phandle-list-names", 0, strings
);
426 selftest(rc
== 0 && !strcmp(strings
[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc
);
427 rc
= of_property_read_string_index(np
, "phandle-list-names", 1, strings
);
428 selftest(rc
== 0 && !strcmp(strings
[0], "second"), "of_property_read_string_index() failure; rc=%i\n", rc
);
429 rc
= of_property_read_string_index(np
, "phandle-list-names", 2, strings
);
430 selftest(rc
== 0 && !strcmp(strings
[0], "third"), "of_property_read_string_index() failure; rc=%i\n", rc
);
432 rc
= of_property_read_string_index(np
, "phandle-list-names", 3, strings
);
433 selftest(rc
== -ENODATA
&& strings
[0] == NULL
, "of_property_read_string_index() failure; rc=%i\n", rc
);
435 rc
= of_property_read_string_index(np
, "unterminated-string", 0, strings
);
436 selftest(rc
== -EILSEQ
&& strings
[0] == NULL
, "of_property_read_string_index() failure; rc=%i\n", rc
);
437 rc
= of_property_read_string_index(np
, "unterminated-string-list", 0, strings
);
438 selftest(rc
== 0 && !strcmp(strings
[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc
);
440 rc
= of_property_read_string_index(np
, "unterminated-string-list", 2, strings
); /* should fail */
441 selftest(rc
== -EILSEQ
&& strings
[0] == NULL
, "of_property_read_string_index() failure; rc=%i\n", rc
);
444 /* of_property_read_string_array() tests */
445 rc
= of_property_read_string_array(np
, "string-property", strings
, 4);
446 selftest(rc
== 1, "Incorrect string count; rc=%i\n", rc
);
447 rc
= of_property_read_string_array(np
, "phandle-list-names", strings
, 4);
448 selftest(rc
== 3, "Incorrect string count; rc=%i\n", rc
);
449 rc
= of_property_read_string_array(np
, "unterminated-string", strings
, 4);
450 selftest(rc
== -EILSEQ
, "unterminated string; rc=%i\n", rc
);
451 /* -- An incorrectly formed string should cause a failure */
452 rc
= of_property_read_string_array(np
, "unterminated-string-list", strings
, 4);
453 selftest(rc
== -EILSEQ
, "unterminated string array; rc=%i\n", rc
);
454 /* -- parsing the correctly formed strings should still work: */
456 rc
= of_property_read_string_array(np
, "unterminated-string-list", strings
, 2);
457 selftest(rc
== 2 && strings
[2] == NULL
, "of_property_read_string_array() failure; rc=%i\n", rc
);
459 rc
= of_property_read_string_array(np
, "phandle-list-names", strings
, 1);
460 selftest(rc
== 1 && strings
[1] == NULL
, "Overwrote end of string array; rc=%i, str='%s'\n", rc
, strings
[1]);
463 #define propcmp(p1, p2) (((p1)->length == (p2)->length) && \
464 (p1)->value && (p2)->value && \
465 !memcmp((p1)->value, (p2)->value, (p1)->length) && \
466 !strcmp((p1)->name, (p2)->name))
467 static void __init
of_selftest_property_copy(void)
469 #ifdef CONFIG_OF_DYNAMIC
470 struct property p1
= { .name
= "p1", .length
= 0, .value
= "" };
471 struct property p2
= { .name
= "p2", .length
= 5, .value
= "abcd" };
472 struct property
*new;
474 new = __of_prop_dup(&p1
, GFP_KERNEL
);
475 selftest(new && propcmp(&p1
, new), "empty property didn't copy correctly\n");
480 new = __of_prop_dup(&p2
, GFP_KERNEL
);
481 selftest(new && propcmp(&p2
, new), "non-empty property didn't copy correctly\n");
488 static void __init
of_selftest_changeset(void)
490 #ifdef CONFIG_OF_DYNAMIC
491 struct property
*ppadd
, padd
= { .name
= "prop-add", .length
= 0, .value
= "" };
492 struct property
*ppupdate
, pupdate
= { .name
= "prop-update", .length
= 5, .value
= "abcd" };
493 struct property
*ppremove
;
494 struct device_node
*n1
, *n2
, *n21
, *nremove
, *parent
, *np
;
495 struct of_changeset chgset
;
497 n1
= __of_node_dup(NULL
, "/testcase-data/changeset/n1");
498 selftest(n1
, "testcase setup failure\n");
499 n2
= __of_node_dup(NULL
, "/testcase-data/changeset/n2");
500 selftest(n2
, "testcase setup failure\n");
501 n21
= __of_node_dup(NULL
, "%s/%s", "/testcase-data/changeset/n2", "n21");
502 selftest(n21
, "testcase setup failure %p\n", n21
);
503 nremove
= of_find_node_by_path("/testcase-data/changeset/node-remove");
504 selftest(nremove
, "testcase setup failure\n");
505 ppadd
= __of_prop_dup(&padd
, GFP_KERNEL
);
506 selftest(ppadd
, "testcase setup failure\n");
507 ppupdate
= __of_prop_dup(&pupdate
, GFP_KERNEL
);
508 selftest(ppupdate
, "testcase setup failure\n");
509 parent
= nremove
->parent
;
514 ppremove
= of_find_property(parent
, "prop-remove", NULL
);
515 selftest(ppremove
, "failed to find removal prop");
517 of_changeset_init(&chgset
);
518 selftest(!of_changeset_attach_node(&chgset
, n1
), "fail attach n1\n");
519 selftest(!of_changeset_attach_node(&chgset
, n2
), "fail attach n2\n");
520 selftest(!of_changeset_detach_node(&chgset
, nremove
), "fail remove node\n");
521 selftest(!of_changeset_attach_node(&chgset
, n21
), "fail attach n21\n");
522 selftest(!of_changeset_add_property(&chgset
, parent
, ppadd
), "fail add prop\n");
523 selftest(!of_changeset_update_property(&chgset
, parent
, ppupdate
), "fail update prop\n");
524 selftest(!of_changeset_remove_property(&chgset
, parent
, ppremove
), "fail remove prop\n");
525 mutex_lock(&of_mutex
);
526 selftest(!of_changeset_apply(&chgset
), "apply failed\n");
527 mutex_unlock(&of_mutex
);
529 /* Make sure node names are constructed correctly */
530 selftest((np
= of_find_node_by_path("/testcase-data/changeset/n2/n21")),
531 "'%s' not added\n", n21
->full_name
);
534 mutex_lock(&of_mutex
);
535 selftest(!of_changeset_revert(&chgset
), "revert failed\n");
536 mutex_unlock(&of_mutex
);
538 of_changeset_destroy(&chgset
);
542 static void __init
of_selftest_parse_interrupts(void)
544 struct device_node
*np
;
545 struct of_phandle_args args
;
548 np
= of_find_node_by_path("/testcase-data/interrupts/interrupts0");
550 pr_err("missing testcase data\n");
554 for (i
= 0; i
< 4; i
++) {
557 rc
= of_irq_parse_one(np
, i
, &args
);
560 passed
&= (args
.args_count
== 1);
561 passed
&= (args
.args
[0] == (i
+ 1));
563 selftest(passed
, "index %i - data error on node %s rc=%i\n",
564 i
, args
.np
->full_name
, rc
);
568 np
= of_find_node_by_path("/testcase-data/interrupts/interrupts1");
570 pr_err("missing testcase data\n");
574 for (i
= 0; i
< 4; i
++) {
577 rc
= of_irq_parse_one(np
, i
, &args
);
579 /* Test the values from tests-phandle.dtsi */
583 passed
&= (args
.args_count
== 1);
584 passed
&= (args
.args
[0] == 9);
588 passed
&= (args
.args_count
== 3);
589 passed
&= (args
.args
[0] == 10);
590 passed
&= (args
.args
[1] == 11);
591 passed
&= (args
.args
[2] == 12);
595 passed
&= (args
.args_count
== 2);
596 passed
&= (args
.args
[0] == 13);
597 passed
&= (args
.args
[1] == 14);
601 passed
&= (args
.args_count
== 2);
602 passed
&= (args
.args
[0] == 15);
603 passed
&= (args
.args
[1] == 16);
608 selftest(passed
, "index %i - data error on node %s rc=%i\n",
609 i
, args
.np
->full_name
, rc
);
614 static void __init
of_selftest_parse_interrupts_extended(void)
616 struct device_node
*np
;
617 struct of_phandle_args args
;
620 np
= of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
622 pr_err("missing testcase data\n");
626 for (i
= 0; i
< 7; i
++) {
628 rc
= of_irq_parse_one(np
, i
, &args
);
630 /* Test the values from tests-phandle.dtsi */
634 passed
&= (args
.args_count
== 1);
635 passed
&= (args
.args
[0] == 1);
639 passed
&= (args
.args_count
== 3);
640 passed
&= (args
.args
[0] == 2);
641 passed
&= (args
.args
[1] == 3);
642 passed
&= (args
.args
[2] == 4);
646 passed
&= (args
.args_count
== 2);
647 passed
&= (args
.args
[0] == 5);
648 passed
&= (args
.args
[1] == 6);
652 passed
&= (args
.args_count
== 1);
653 passed
&= (args
.args
[0] == 9);
657 passed
&= (args
.args_count
== 3);
658 passed
&= (args
.args
[0] == 10);
659 passed
&= (args
.args
[1] == 11);
660 passed
&= (args
.args
[2] == 12);
664 passed
&= (args
.args_count
== 2);
665 passed
&= (args
.args
[0] == 13);
666 passed
&= (args
.args
[1] == 14);
670 passed
&= (args
.args_count
== 1);
671 passed
&= (args
.args
[0] == 15);
677 selftest(passed
, "index %i - data error on node %s rc=%i\n",
678 i
, args
.np
->full_name
, rc
);
683 static struct of_device_id match_node_table
[] = {
684 { .data
= "A", .name
= "name0", }, /* Name alone is lowest priority */
685 { .data
= "B", .type
= "type1", }, /* followed by type alone */
687 { .data
= "Ca", .name
= "name2", .type
= "type1", }, /* followed by both together */
688 { .data
= "Cb", .name
= "name2", }, /* Only match when type doesn't match */
689 { .data
= "Cc", .name
= "name2", .type
= "type2", },
691 { .data
= "E", .compatible
= "compat3" },
692 { .data
= "G", .compatible
= "compat2", },
693 { .data
= "H", .compatible
= "compat2", .name
= "name5", },
694 { .data
= "I", .compatible
= "compat2", .type
= "type1", },
695 { .data
= "J", .compatible
= "compat2", .type
= "type1", .name
= "name8", },
696 { .data
= "K", .compatible
= "compat2", .name
= "name9", },
703 } match_node_tests
[] = {
704 { .path
= "/testcase-data/match-node/name0", .data
= "A", },
705 { .path
= "/testcase-data/match-node/name1", .data
= "B", },
706 { .path
= "/testcase-data/match-node/a/name2", .data
= "Ca", },
707 { .path
= "/testcase-data/match-node/b/name2", .data
= "Cb", },
708 { .path
= "/testcase-data/match-node/c/name2", .data
= "Cc", },
709 { .path
= "/testcase-data/match-node/name3", .data
= "E", },
710 { .path
= "/testcase-data/match-node/name4", .data
= "G", },
711 { .path
= "/testcase-data/match-node/name5", .data
= "H", },
712 { .path
= "/testcase-data/match-node/name6", .data
= "G", },
713 { .path
= "/testcase-data/match-node/name7", .data
= "I", },
714 { .path
= "/testcase-data/match-node/name8", .data
= "J", },
715 { .path
= "/testcase-data/match-node/name9", .data
= "K", },
718 static void __init
of_selftest_match_node(void)
720 struct device_node
*np
;
721 const struct of_device_id
*match
;
724 for (i
= 0; i
< ARRAY_SIZE(match_node_tests
); i
++) {
725 np
= of_find_node_by_path(match_node_tests
[i
].path
);
727 selftest(0, "missing testcase node %s\n",
728 match_node_tests
[i
].path
);
732 match
= of_match_node(match_node_table
, np
);
734 selftest(0, "%s didn't match anything\n",
735 match_node_tests
[i
].path
);
739 if (strcmp(match
->data
, match_node_tests
[i
].data
) != 0) {
740 selftest(0, "%s got wrong match. expected %s, got %s\n",
741 match_node_tests
[i
].path
, match_node_tests
[i
].data
,
742 (const char *)match
->data
);
745 selftest(1, "passed");
749 struct device test_bus
= {
750 .init_name
= "unittest-bus",
752 static void __init
of_selftest_platform_populate(void)
755 struct device_node
*np
, *child
, *grandchild
;
756 struct platform_device
*pdev
;
757 struct of_device_id match
[] = {
758 { .compatible
= "test-device", },
762 np
= of_find_node_by_path("/testcase-data");
763 of_platform_populate(np
, of_default_bus_match_table
, NULL
, NULL
);
765 /* Test that a missing irq domain returns -EPROBE_DEFER */
766 np
= of_find_node_by_path("/testcase-data/testcase-device1");
767 pdev
= of_find_device_by_node(np
);
768 selftest(pdev
, "device 1 creation failed\n");
770 irq
= platform_get_irq(pdev
, 0);
771 selftest(irq
== -EPROBE_DEFER
, "device deferred probe failed - %d\n", irq
);
773 /* Test that a parsing failure does not return -EPROBE_DEFER */
774 np
= of_find_node_by_path("/testcase-data/testcase-device2");
775 pdev
= of_find_device_by_node(np
);
776 selftest(pdev
, "device 2 creation failed\n");
777 irq
= platform_get_irq(pdev
, 0);
778 selftest(irq
< 0 && irq
!= -EPROBE_DEFER
, "device parsing error failed - %d\n", irq
);
780 if (selftest(np
= of_find_node_by_path("/testcase-data/platform-tests"),
781 "No testcase data in device tree\n"));
784 if (selftest(!(rc
= device_register(&test_bus
)),
785 "testbus registration failed; rc=%i\n", rc
));
788 for_each_child_of_node(np
, child
) {
789 of_platform_populate(child
, match
, NULL
, &test_bus
);
790 for_each_child_of_node(child
, grandchild
)
791 selftest(of_find_device_by_node(grandchild
),
792 "Could not create device for node '%s'\n",
796 of_platform_depopulate(&test_bus
);
797 for_each_child_of_node(np
, child
) {
798 for_each_child_of_node(child
, grandchild
)
799 selftest(!of_find_device_by_node(grandchild
),
800 "device didn't get destroyed '%s'\n",
804 device_unregister(&test_bus
);
809 * update_node_properties - adds the properties
810 * of np into dup node (present in live tree) and
811 * updates parent of children of np to dup.
813 * @np: node already present in live tree
814 * @dup: node present in live tree to be updated
816 static void update_node_properties(struct device_node
*np
,
817 struct device_node
*dup
)
819 struct property
*prop
;
820 struct device_node
*child
;
822 for_each_property_of_node(np
, prop
)
823 of_add_property(dup
, prop
);
825 for_each_child_of_node(np
, child
)
830 * attach_node_and_children - attaches nodes
831 * and its children to live tree
833 * @np: Node to attach to live tree
835 static int attach_node_and_children(struct device_node
*np
)
837 struct device_node
*next
, *dup
, *child
;
840 dup
= of_find_node_by_path(np
->full_name
);
842 update_node_properties(np
, dup
);
849 mutex_lock(&of_mutex
);
850 raw_spin_lock_irqsave(&devtree_lock
, flags
);
851 np
->sibling
= np
->parent
->child
;
852 np
->parent
->child
= np
;
853 of_node_clear_flag(np
, OF_DETACHED
);
854 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
856 __of_attach_node_sysfs(np
);
857 mutex_unlock(&of_mutex
);
860 next
= child
->sibling
;
861 attach_node_and_children(child
);
869 * selftest_data_add - Reads, copies data from
870 * linked tree and attaches it to the live tree
872 static int __init
selftest_data_add(void)
875 struct device_node
*selftest_data_node
, *np
;
876 extern uint8_t __dtb_testcases_begin
[];
877 extern uint8_t __dtb_testcases_end
[];
878 const int size
= __dtb_testcases_end
- __dtb_testcases_begin
;
882 pr_warn("%s: No testcase data to attach; not running tests\n",
888 selftest_data
= kmemdup(__dtb_testcases_begin
, size
, GFP_KERNEL
);
890 if (!selftest_data
) {
891 pr_warn("%s: Failed to allocate memory for selftest_data; "
892 "not running tests\n", __func__
);
895 of_fdt_unflatten_tree(selftest_data
, &selftest_data_node
);
896 if (!selftest_data_node
) {
897 pr_warn("%s: No tree to attach; not running tests\n", __func__
);
900 of_node_set_flag(selftest_data_node
, OF_DETACHED
);
901 rc
= of_resolve_phandles(selftest_data_node
);
903 pr_err("%s: Failed to resolve phandles (rc=%i)\n", __func__
, rc
);
908 of_root
= selftest_data_node
;
909 for_each_of_allnodes(np
)
910 __of_attach_node_sysfs(np
);
911 of_aliases
= of_find_node_by_path("/aliases");
912 of_chosen
= of_find_node_by_path("/chosen");
916 /* attach the sub-tree to live tree */
917 np
= selftest_data_node
->child
;
919 struct device_node
*next
= np
->sibling
;
920 np
->parent
= of_root
;
921 attach_node_and_children(np
);
927 #ifdef CONFIG_OF_OVERLAY
929 static int selftest_probe(struct platform_device
*pdev
)
931 struct device
*dev
= &pdev
->dev
;
932 struct device_node
*np
= dev
->of_node
;
935 dev_err(dev
, "No OF data for device\n");
940 dev_dbg(dev
, "%s for node @%s\n", __func__
, np
->full_name
);
942 of_platform_populate(np
, NULL
, NULL
, &pdev
->dev
);
947 static int selftest_remove(struct platform_device
*pdev
)
949 struct device
*dev
= &pdev
->dev
;
950 struct device_node
*np
= dev
->of_node
;
952 dev_dbg(dev
, "%s for node @%s\n", __func__
, np
->full_name
);
956 static struct of_device_id selftest_match
[] = {
957 { .compatible
= "selftest", },
961 static struct platform_driver selftest_driver
= {
962 .probe
= selftest_probe
,
963 .remove
= selftest_remove
,
966 .owner
= THIS_MODULE
,
967 .of_match_table
= of_match_ptr(selftest_match
),
971 /* get the platform device instantiated at the path */
972 static struct platform_device
*of_path_to_platform_device(const char *path
)
974 struct device_node
*np
;
975 struct platform_device
*pdev
;
977 np
= of_find_node_by_path(path
);
981 pdev
= of_find_device_by_node(np
);
987 /* find out if a platform device exists at that path */
988 static int of_path_platform_device_exists(const char *path
)
990 struct platform_device
*pdev
;
992 pdev
= of_path_to_platform_device(path
);
993 platform_device_put(pdev
);
997 #if IS_BUILTIN(CONFIG_I2C)
999 /* get the i2c client device instantiated at the path */
1000 static struct i2c_client
*of_path_to_i2c_client(const char *path
)
1002 struct device_node
*np
;
1003 struct i2c_client
*client
;
1005 np
= of_find_node_by_path(path
);
1009 client
= of_find_i2c_device_by_node(np
);
1015 /* find out if a i2c client device exists at that path */
1016 static int of_path_i2c_client_exists(const char *path
)
1018 struct i2c_client
*client
;
1020 client
= of_path_to_i2c_client(path
);
1022 put_device(&client
->dev
);
1023 return client
!= NULL
;
1026 static int of_path_i2c_client_exists(const char *path
)
1037 static int of_path_device_type_exists(const char *path
,
1038 enum overlay_type ovtype
)
1042 return of_path_platform_device_exists(path
);
1044 return of_path_i2c_client_exists(path
);
1049 static const char *selftest_path(int nr
, enum overlay_type ovtype
)
1052 static char buf
[256];
1056 base
= "/testcase-data/overlay-node/test-bus";
1059 base
= "/testcase-data/overlay-node/test-bus/i2c-test-bus";
1065 snprintf(buf
, sizeof(buf
) - 1, "%s/test-selftest%d", base
, nr
);
1066 buf
[sizeof(buf
) - 1] = '\0';
1070 static int of_selftest_device_exists(int selftest_nr
, enum overlay_type ovtype
)
1074 path
= selftest_path(selftest_nr
, ovtype
);
1078 return of_path_platform_device_exists(path
);
1080 return of_path_i2c_client_exists(path
);
1085 static const char *overlay_path(int nr
)
1087 static char buf
[256];
1089 snprintf(buf
, sizeof(buf
) - 1,
1090 "/testcase-data/overlay%d", nr
);
1091 buf
[sizeof(buf
) - 1] = '\0';
1096 static const char *bus_path
= "/testcase-data/overlay-node/test-bus";
1098 static int of_selftest_apply_overlay(int selftest_nr
, int overlay_nr
,
1101 struct device_node
*np
= NULL
;
1104 np
= of_find_node_by_path(overlay_path(overlay_nr
));
1106 selftest(0, "could not find overlay node @\"%s\"\n",
1107 overlay_path(overlay_nr
));
1112 ret
= of_overlay_create(np
);
1114 selftest(0, "could not create overlay from \"%s\"\n",
1115 overlay_path(overlay_nr
));
1131 /* apply an overlay while checking before and after states */
1132 static int of_selftest_apply_overlay_check(int overlay_nr
, int selftest_nr
,
1133 int before
, int after
, enum overlay_type ovtype
)
1137 /* selftest device must not be in before state */
1138 if (of_selftest_device_exists(selftest_nr
, ovtype
) != before
) {
1139 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1140 overlay_path(overlay_nr
),
1141 selftest_path(selftest_nr
, ovtype
),
1142 !before
? "enabled" : "disabled");
1146 ret
= of_selftest_apply_overlay(overlay_nr
, selftest_nr
, NULL
);
1148 /* of_selftest_apply_overlay already called selftest() */
1152 /* selftest device must be to set to after state */
1153 if (of_selftest_device_exists(selftest_nr
, ovtype
) != after
) {
1154 selftest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n",
1155 overlay_path(overlay_nr
),
1156 selftest_path(selftest_nr
, ovtype
),
1157 !after
? "enabled" : "disabled");
1164 /* apply an overlay and then revert it while checking before, after states */
1165 static int of_selftest_apply_revert_overlay_check(int overlay_nr
,
1166 int selftest_nr
, int before
, int after
,
1167 enum overlay_type ovtype
)
1171 /* selftest device must be in before state */
1172 if (of_selftest_device_exists(selftest_nr
, ovtype
) != before
) {
1173 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1174 overlay_path(overlay_nr
),
1175 selftest_path(selftest_nr
, ovtype
),
1176 !before
? "enabled" : "disabled");
1180 /* apply the overlay */
1181 ret
= of_selftest_apply_overlay(overlay_nr
, selftest_nr
, &ov_id
);
1183 /* of_selftest_apply_overlay already called selftest() */
1187 /* selftest device must be in after state */
1188 if (of_selftest_device_exists(selftest_nr
, ovtype
) != after
) {
1189 selftest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n",
1190 overlay_path(overlay_nr
),
1191 selftest_path(selftest_nr
, ovtype
),
1192 !after
? "enabled" : "disabled");
1196 ret
= of_overlay_destroy(ov_id
);
1198 selftest(0, "overlay @\"%s\" failed to be destroyed @\"%s\"\n",
1199 overlay_path(overlay_nr
),
1200 selftest_path(selftest_nr
, ovtype
));
1204 /* selftest device must be again in before state */
1205 if (of_selftest_device_exists(selftest_nr
, PDEV_OVERLAY
) != before
) {
1206 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1207 overlay_path(overlay_nr
),
1208 selftest_path(selftest_nr
, ovtype
),
1209 !before
? "enabled" : "disabled");
1216 /* test activation of device */
1217 static void of_selftest_overlay_0(void)
1221 /* device should enable */
1222 ret
= of_selftest_apply_overlay_check(0, 0, 0, 1, PDEV_OVERLAY
);
1226 selftest(1, "overlay test %d passed\n", 0);
1229 /* test deactivation of device */
1230 static void of_selftest_overlay_1(void)
1234 /* device should disable */
1235 ret
= of_selftest_apply_overlay_check(1, 1, 1, 0, PDEV_OVERLAY
);
1239 selftest(1, "overlay test %d passed\n", 1);
1242 /* test activation of device */
1243 static void of_selftest_overlay_2(void)
1247 /* device should enable */
1248 ret
= of_selftest_apply_overlay_check(2, 2, 0, 1, PDEV_OVERLAY
);
1252 selftest(1, "overlay test %d passed\n", 2);
1255 /* test deactivation of device */
1256 static void of_selftest_overlay_3(void)
1260 /* device should disable */
1261 ret
= of_selftest_apply_overlay_check(3, 3, 1, 0, PDEV_OVERLAY
);
1265 selftest(1, "overlay test %d passed\n", 3);
1268 /* test activation of a full device node */
1269 static void of_selftest_overlay_4(void)
1273 /* device should disable */
1274 ret
= of_selftest_apply_overlay_check(4, 4, 0, 1, PDEV_OVERLAY
);
1278 selftest(1, "overlay test %d passed\n", 4);
1281 /* test overlay apply/revert sequence */
1282 static void of_selftest_overlay_5(void)
1286 /* device should disable */
1287 ret
= of_selftest_apply_revert_overlay_check(5, 5, 0, 1, PDEV_OVERLAY
);
1291 selftest(1, "overlay test %d passed\n", 5);
1294 /* test overlay application in sequence */
1295 static void of_selftest_overlay_6(void)
1297 struct device_node
*np
;
1298 int ret
, i
, ov_id
[2];
1299 int overlay_nr
= 6, selftest_nr
= 6;
1300 int before
= 0, after
= 1;
1302 /* selftest device must be in before state */
1303 for (i
= 0; i
< 2; i
++) {
1304 if (of_selftest_device_exists(selftest_nr
+ i
, PDEV_OVERLAY
)
1306 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1307 overlay_path(overlay_nr
+ i
),
1308 selftest_path(selftest_nr
+ i
,
1310 !before
? "enabled" : "disabled");
1315 /* apply the overlays */
1316 for (i
= 0; i
< 2; i
++) {
1318 np
= of_find_node_by_path(overlay_path(overlay_nr
+ i
));
1320 selftest(0, "could not find overlay node @\"%s\"\n",
1321 overlay_path(overlay_nr
+ i
));
1325 ret
= of_overlay_create(np
);
1327 selftest(0, "could not create overlay from \"%s\"\n",
1328 overlay_path(overlay_nr
+ i
));
1334 for (i
= 0; i
< 2; i
++) {
1335 /* selftest device must be in after state */
1336 if (of_selftest_device_exists(selftest_nr
+ i
, PDEV_OVERLAY
)
1338 selftest(0, "overlay @\"%s\" failed @\"%s\" %s\n",
1339 overlay_path(overlay_nr
+ i
),
1340 selftest_path(selftest_nr
+ i
,
1342 !after
? "enabled" : "disabled");
1347 for (i
= 1; i
>= 0; i
--) {
1348 ret
= of_overlay_destroy(ov_id
[i
]);
1350 selftest(0, "overlay @\"%s\" failed destroy @\"%s\"\n",
1351 overlay_path(overlay_nr
+ i
),
1352 selftest_path(selftest_nr
+ i
,
1358 for (i
= 0; i
< 2; i
++) {
1359 /* selftest device must be again in before state */
1360 if (of_selftest_device_exists(selftest_nr
+ i
, PDEV_OVERLAY
)
1362 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1363 overlay_path(overlay_nr
+ i
),
1364 selftest_path(selftest_nr
+ i
,
1366 !before
? "enabled" : "disabled");
1371 selftest(1, "overlay test %d passed\n", 6);
1374 /* test overlay application in sequence */
1375 static void of_selftest_overlay_8(void)
1377 struct device_node
*np
;
1378 int ret
, i
, ov_id
[2];
1379 int overlay_nr
= 8, selftest_nr
= 8;
1381 /* we don't care about device state in this test */
1383 /* apply the overlays */
1384 for (i
= 0; i
< 2; i
++) {
1386 np
= of_find_node_by_path(overlay_path(overlay_nr
+ i
));
1388 selftest(0, "could not find overlay node @\"%s\"\n",
1389 overlay_path(overlay_nr
+ i
));
1393 ret
= of_overlay_create(np
);
1395 selftest(0, "could not create overlay from \"%s\"\n",
1396 overlay_path(overlay_nr
+ i
));
1402 /* now try to remove first overlay (it should fail) */
1403 ret
= of_overlay_destroy(ov_id
[0]);
1405 selftest(0, "overlay @\"%s\" was destroyed @\"%s\"\n",
1406 overlay_path(overlay_nr
+ 0),
1407 selftest_path(selftest_nr
,
1412 /* removing them in order should work */
1413 for (i
= 1; i
>= 0; i
--) {
1414 ret
= of_overlay_destroy(ov_id
[i
]);
1416 selftest(0, "overlay @\"%s\" not destroyed @\"%s\"\n",
1417 overlay_path(overlay_nr
+ i
),
1418 selftest_path(selftest_nr
,
1424 selftest(1, "overlay test %d passed\n", 8);
1427 /* test insertion of a bus with parent devices */
1428 static void of_selftest_overlay_10(void)
1433 /* device should disable */
1434 ret
= of_selftest_apply_overlay_check(10, 10, 0, 1, PDEV_OVERLAY
);
1435 if (selftest(ret
== 0,
1436 "overlay test %d failed; overlay application\n", 10))
1439 child_path
= kasprintf(GFP_KERNEL
, "%s/test-selftest101",
1440 selftest_path(10, PDEV_OVERLAY
));
1441 if (selftest(child_path
, "overlay test %d failed; kasprintf\n", 10))
1444 ret
= of_path_device_type_exists(child_path
, PDEV_OVERLAY
);
1446 if (selftest(ret
, "overlay test %d failed; no child device\n", 10))
1450 /* test insertion of a bus with parent devices (and revert) */
1451 static void of_selftest_overlay_11(void)
1455 /* device should disable */
1456 ret
= of_selftest_apply_revert_overlay_check(11, 11, 0, 1,
1458 if (selftest(ret
== 0,
1459 "overlay test %d failed; overlay application\n", 11))
1463 #if IS_BUILTIN(CONFIG_I2C) && IS_ENABLED(CONFIG_OF_OVERLAY)
1465 struct selftest_i2c_bus_data
{
1466 struct platform_device
*pdev
;
1467 struct i2c_adapter adap
;
1470 static int selftest_i2c_master_xfer(struct i2c_adapter
*adap
,
1471 struct i2c_msg
*msgs
, int num
)
1473 struct selftest_i2c_bus_data
*std
= i2c_get_adapdata(adap
);
1480 static u32
selftest_i2c_functionality(struct i2c_adapter
*adap
)
1482 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
;
1485 static const struct i2c_algorithm selftest_i2c_algo
= {
1486 .master_xfer
= selftest_i2c_master_xfer
,
1487 .functionality
= selftest_i2c_functionality
,
1490 static int selftest_i2c_bus_probe(struct platform_device
*pdev
)
1492 struct device
*dev
= &pdev
->dev
;
1493 struct device_node
*np
= dev
->of_node
;
1494 struct selftest_i2c_bus_data
*std
;
1495 struct i2c_adapter
*adap
;
1499 dev_err(dev
, "No OF data for device\n");
1504 dev_dbg(dev
, "%s for node @%s\n", __func__
, np
->full_name
);
1506 std
= devm_kzalloc(dev
, sizeof(*std
), GFP_KERNEL
);
1508 dev_err(dev
, "Failed to allocate selftest i2c data\n");
1512 /* link them together */
1514 platform_set_drvdata(pdev
, std
);
1517 i2c_set_adapdata(adap
, std
);
1519 strlcpy(adap
->name
, pdev
->name
, sizeof(adap
->name
));
1520 adap
->class = I2C_CLASS_DEPRECATED
;
1521 adap
->algo
= &selftest_i2c_algo
;
1522 adap
->dev
.parent
= dev
;
1523 adap
->dev
.of_node
= dev
->of_node
;
1524 adap
->timeout
= 5 * HZ
;
1527 ret
= i2c_add_numbered_adapter(adap
);
1529 dev_err(dev
, "Failed to add I2C adapter\n");
1536 static int selftest_i2c_bus_remove(struct platform_device
*pdev
)
1538 struct device
*dev
= &pdev
->dev
;
1539 struct device_node
*np
= dev
->of_node
;
1540 struct selftest_i2c_bus_data
*std
= platform_get_drvdata(pdev
);
1542 dev_dbg(dev
, "%s for node @%s\n", __func__
, np
->full_name
);
1543 i2c_del_adapter(&std
->adap
);
1548 static struct of_device_id selftest_i2c_bus_match
[] = {
1549 { .compatible
= "selftest-i2c-bus", },
1553 static struct platform_driver selftest_i2c_bus_driver
= {
1554 .probe
= selftest_i2c_bus_probe
,
1555 .remove
= selftest_i2c_bus_remove
,
1557 .name
= "selftest-i2c-bus",
1558 .of_match_table
= of_match_ptr(selftest_i2c_bus_match
),
1562 static int selftest_i2c_dev_probe(struct i2c_client
*client
,
1563 const struct i2c_device_id
*id
)
1565 struct device
*dev
= &client
->dev
;
1566 struct device_node
*np
= client
->dev
.of_node
;
1569 dev_err(dev
, "No OF node\n");
1573 dev_dbg(dev
, "%s for node @%s\n", __func__
, np
->full_name
);
1578 static int selftest_i2c_dev_remove(struct i2c_client
*client
)
1580 struct device
*dev
= &client
->dev
;
1581 struct device_node
*np
= client
->dev
.of_node
;
1583 dev_dbg(dev
, "%s for node @%s\n", __func__
, np
->full_name
);
1587 static const struct i2c_device_id selftest_i2c_dev_id
[] = {
1588 { .name
= "selftest-i2c-dev" },
1592 static struct i2c_driver selftest_i2c_dev_driver
= {
1594 .name
= "selftest-i2c-dev",
1595 .owner
= THIS_MODULE
,
1597 .probe
= selftest_i2c_dev_probe
,
1598 .remove
= selftest_i2c_dev_remove
,
1599 .id_table
= selftest_i2c_dev_id
,
1602 #if IS_BUILTIN(CONFIG_I2C_MUX)
1604 struct selftest_i2c_mux_data
{
1606 struct i2c_adapter
*adap
[];
1609 static int selftest_i2c_mux_select_chan(struct i2c_adapter
*adap
,
1610 void *client
, u32 chan
)
1615 static int selftest_i2c_mux_probe(struct i2c_client
*client
,
1616 const struct i2c_device_id
*id
)
1618 int ret
, i
, nchans
, size
;
1619 struct device
*dev
= &client
->dev
;
1620 struct i2c_adapter
*adap
= to_i2c_adapter(dev
->parent
);
1621 struct device_node
*np
= client
->dev
.of_node
, *child
;
1622 struct selftest_i2c_mux_data
*stm
;
1625 dev_dbg(dev
, "%s for node @%s\n", __func__
, np
->full_name
);
1628 dev_err(dev
, "No OF node\n");
1633 for_each_child_of_node(np
, child
) {
1634 ret
= of_property_read_u32(child
, "reg", ®
);
1637 if (max_reg
== (u32
)-1 || reg
> max_reg
)
1640 nchans
= max_reg
== (u32
)-1 ? 0 : max_reg
+ 1;
1642 dev_err(dev
, "No channels\n");
1646 size
= offsetof(struct selftest_i2c_mux_data
, adap
[nchans
]);
1647 stm
= devm_kzalloc(dev
, size
, GFP_KERNEL
);
1649 dev_err(dev
, "Out of memory\n");
1652 stm
->nchans
= nchans
;
1653 for (i
= 0; i
< nchans
; i
++) {
1654 stm
->adap
[i
] = i2c_add_mux_adapter(adap
, dev
, client
,
1655 0, i
, 0, selftest_i2c_mux_select_chan
, NULL
);
1656 if (!stm
->adap
[i
]) {
1657 dev_err(dev
, "Failed to register mux #%d\n", i
);
1658 for (i
--; i
>= 0; i
--)
1659 i2c_del_mux_adapter(stm
->adap
[i
]);
1664 i2c_set_clientdata(client
, stm
);
1669 static int selftest_i2c_mux_remove(struct i2c_client
*client
)
1671 struct device
*dev
= &client
->dev
;
1672 struct device_node
*np
= client
->dev
.of_node
;
1673 struct selftest_i2c_mux_data
*stm
= i2c_get_clientdata(client
);
1676 dev_dbg(dev
, "%s for node @%s\n", __func__
, np
->full_name
);
1677 for (i
= stm
->nchans
- 1; i
>= 0; i
--)
1678 i2c_del_mux_adapter(stm
->adap
[i
]);
1682 static const struct i2c_device_id selftest_i2c_mux_id
[] = {
1683 { .name
= "selftest-i2c-mux" },
1687 static struct i2c_driver selftest_i2c_mux_driver
= {
1689 .name
= "selftest-i2c-mux",
1690 .owner
= THIS_MODULE
,
1692 .probe
= selftest_i2c_mux_probe
,
1693 .remove
= selftest_i2c_mux_remove
,
1694 .id_table
= selftest_i2c_mux_id
,
1699 static int of_selftest_overlay_i2c_init(void)
1703 ret
= i2c_add_driver(&selftest_i2c_dev_driver
);
1704 if (selftest(ret
== 0,
1705 "could not register selftest i2c device driver\n"))
1708 ret
= platform_driver_register(&selftest_i2c_bus_driver
);
1709 if (selftest(ret
== 0,
1710 "could not register selftest i2c bus driver\n"))
1713 #if IS_BUILTIN(CONFIG_I2C_MUX)
1714 ret
= i2c_add_driver(&selftest_i2c_mux_driver
);
1715 if (selftest(ret
== 0,
1716 "could not register selftest i2c mux driver\n"))
1723 static void of_selftest_overlay_i2c_cleanup(void)
1725 #if IS_BUILTIN(CONFIG_I2C_MUX)
1726 i2c_del_driver(&selftest_i2c_mux_driver
);
1728 platform_driver_unregister(&selftest_i2c_bus_driver
);
1729 i2c_del_driver(&selftest_i2c_dev_driver
);
1732 static void of_selftest_overlay_i2c_12(void)
1736 /* device should enable */
1737 ret
= of_selftest_apply_overlay_check(12, 12, 0, 1, I2C_OVERLAY
);
1741 selftest(1, "overlay test %d passed\n", 12);
1744 /* test deactivation of device */
1745 static void of_selftest_overlay_i2c_13(void)
1749 /* device should disable */
1750 ret
= of_selftest_apply_overlay_check(13, 13, 1, 0, I2C_OVERLAY
);
1754 selftest(1, "overlay test %d passed\n", 13);
1757 /* just check for i2c mux existence */
1758 static void of_selftest_overlay_i2c_14(void)
1762 static void of_selftest_overlay_i2c_15(void)
1766 /* device should enable */
1767 ret
= of_selftest_apply_overlay_check(16, 15, 0, 1, I2C_OVERLAY
);
1771 selftest(1, "overlay test %d passed\n", 15);
1776 static inline void of_selftest_overlay_i2c_14(void) { }
1777 static inline void of_selftest_overlay_i2c_15(void) { }
1781 static void __init
of_selftest_overlay(void)
1783 struct device_node
*bus_np
= NULL
;
1786 ret
= platform_driver_register(&selftest_driver
);
1788 selftest(0, "could not register selftest driver\n");
1792 bus_np
= of_find_node_by_path(bus_path
);
1793 if (bus_np
== NULL
) {
1794 selftest(0, "could not find bus_path \"%s\"\n", bus_path
);
1798 ret
= of_platform_populate(bus_np
, of_default_bus_match_table
,
1801 selftest(0, "could not populate bus @ \"%s\"\n", bus_path
);
1805 if (!of_selftest_device_exists(100, PDEV_OVERLAY
)) {
1806 selftest(0, "could not find selftest0 @ \"%s\"\n",
1807 selftest_path(100, PDEV_OVERLAY
));
1811 if (of_selftest_device_exists(101, PDEV_OVERLAY
)) {
1812 selftest(0, "selftest1 @ \"%s\" should not exist\n",
1813 selftest_path(101, PDEV_OVERLAY
));
1817 selftest(1, "basic infrastructure of overlays passed");
1819 /* tests in sequence */
1820 of_selftest_overlay_0();
1821 of_selftest_overlay_1();
1822 of_selftest_overlay_2();
1823 of_selftest_overlay_3();
1824 of_selftest_overlay_4();
1825 of_selftest_overlay_5();
1826 of_selftest_overlay_6();
1827 of_selftest_overlay_8();
1829 of_selftest_overlay_10();
1830 of_selftest_overlay_11();
1832 #if IS_BUILTIN(CONFIG_I2C)
1833 if (selftest(of_selftest_overlay_i2c_init() == 0, "i2c init failed\n"))
1836 of_selftest_overlay_i2c_12();
1837 of_selftest_overlay_i2c_13();
1838 of_selftest_overlay_i2c_14();
1839 of_selftest_overlay_i2c_15();
1841 of_selftest_overlay_i2c_cleanup();
1845 of_node_put(bus_np
);
1849 static inline void __init
of_selftest_overlay(void) { }
1852 static int __init
of_selftest(void)
1854 struct device_node
*np
;
1857 /* adding data for selftest */
1858 res
= selftest_data_add();
1862 of_aliases
= of_find_node_by_path("/aliases");
1864 np
= of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
1866 pr_info("No testcase data in device tree; not running tests\n");
1871 pr_info("start of selftest - you will see error messages\n");
1872 of_selftest_check_tree_linkage();
1873 of_selftest_check_phandles();
1874 of_selftest_find_node_by_name();
1875 of_selftest_dynamic();
1876 of_selftest_parse_phandle_with_args();
1877 of_selftest_property_string();
1878 of_selftest_property_copy();
1879 of_selftest_changeset();
1880 of_selftest_parse_interrupts();
1881 of_selftest_parse_interrupts_extended();
1882 of_selftest_match_node();
1883 of_selftest_platform_populate();
1884 of_selftest_overlay();
1886 /* Double check linkage after removing testcase data */
1887 of_selftest_check_tree_linkage();
1889 pr_info("end of selftest - %i passed, %i failed\n",
1890 selftest_results
.passed
, selftest_results
.failed
);
1894 late_initcall(of_selftest
);