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:testoption", NULL
);
101 selftest(np
, "NULL option path test failed\n");
104 np
= of_find_node_opts_by_path("testcase-alias:testaliasoption",
106 selftest(np
&& !strcmp("testaliasoption", options
),
107 "option alias path test failed\n");
110 np
= of_find_node_opts_by_path("testcase-alias:test/alias/option",
112 selftest(np
&& !strcmp("test/alias/option", options
),
113 "option alias path test, subcase #1 failed\n");
116 np
= of_find_node_opts_by_path("testcase-alias:testaliasoption", NULL
);
117 selftest(np
, "NULL option alias path test failed\n");
120 options
= "testoption";
121 np
= of_find_node_opts_by_path("testcase-alias", &options
);
122 selftest(np
&& !options
, "option clearing test failed\n");
125 options
= "testoption";
126 np
= of_find_node_opts_by_path("/", &options
);
127 selftest(np
&& !options
, "option clearing root node test failed\n");
131 static void __init
of_selftest_dynamic(void)
133 struct device_node
*np
;
134 struct property
*prop
;
136 np
= of_find_node_by_path("/testcase-data");
138 pr_err("missing testcase data\n");
142 /* Array of 4 properties for the purpose of testing */
143 prop
= kzalloc(sizeof(*prop
) * 4, GFP_KERNEL
);
145 selftest(0, "kzalloc() failed\n");
149 /* Add a new property - should pass*/
150 prop
->name
= "new-property";
151 prop
->value
= "new-property-data";
152 prop
->length
= strlen(prop
->value
);
153 selftest(of_add_property(np
, prop
) == 0, "Adding a new property failed\n");
155 /* Try to add an existing property - should fail */
157 prop
->name
= "new-property";
158 prop
->value
= "new-property-data-should-fail";
159 prop
->length
= strlen(prop
->value
);
160 selftest(of_add_property(np
, prop
) != 0,
161 "Adding an existing property should have failed\n");
163 /* Try to modify an existing property - should pass */
164 prop
->value
= "modify-property-data-should-pass";
165 prop
->length
= strlen(prop
->value
);
166 selftest(of_update_property(np
, prop
) == 0,
167 "Updating an existing property should have passed\n");
169 /* Try to modify non-existent property - should pass*/
171 prop
->name
= "modify-property";
172 prop
->value
= "modify-missing-property-data-should-pass";
173 prop
->length
= strlen(prop
->value
);
174 selftest(of_update_property(np
, prop
) == 0,
175 "Updating a missing property should have passed\n");
177 /* Remove property - should pass */
178 selftest(of_remove_property(np
, prop
) == 0,
179 "Removing a property should have passed\n");
181 /* Adding very large property - should pass */
183 prop
->name
= "large-property-PAGE_SIZEx8";
184 prop
->length
= PAGE_SIZE
* 8;
185 prop
->value
= kzalloc(prop
->length
, GFP_KERNEL
);
186 selftest(prop
->value
!= NULL
, "Unable to allocate large buffer\n");
188 selftest(of_add_property(np
, prop
) == 0,
189 "Adding a large property should have passed\n");
192 static int __init
of_selftest_check_node_linkage(struct device_node
*np
)
194 struct device_node
*child
;
197 for_each_child_of_node(np
, child
) {
198 if (child
->parent
!= np
) {
199 pr_err("Child node %s links to wrong parent %s\n",
200 child
->name
, np
->name
);
204 rc
= of_selftest_check_node_linkage(child
);
213 static void __init
of_selftest_check_tree_linkage(void)
215 struct device_node
*np
;
216 int allnode_count
= 0, child_count
;
221 for_each_of_allnodes(np
)
223 child_count
= of_selftest_check_node_linkage(of_root
);
225 selftest(child_count
> 0, "Device node data structure is corrupted\n");
226 selftest(child_count
== allnode_count
, "allnodes list size (%i) doesn't match"
227 "sibling lists size (%i)\n", allnode_count
, child_count
);
228 pr_debug("allnodes list size (%i); sibling lists size (%i)\n", allnode_count
, child_count
);
232 struct hlist_node node
;
233 struct device_node
*np
;
236 static DEFINE_HASHTABLE(phandle_ht
, 8);
237 static void __init
of_selftest_check_phandles(void)
239 struct device_node
*np
;
240 struct node_hash
*nh
;
241 struct hlist_node
*tmp
;
242 int i
, dup_count
= 0, phandle_count
= 0;
244 for_each_of_allnodes(np
) {
248 hash_for_each_possible(phandle_ht
, nh
, node
, np
->phandle
) {
249 if (nh
->np
->phandle
== np
->phandle
) {
250 pr_info("Duplicate phandle! %i used by %s and %s\n",
251 np
->phandle
, nh
->np
->full_name
, np
->full_name
);
257 nh
= kzalloc(sizeof(*nh
), GFP_KERNEL
);
262 hash_add(phandle_ht
, &nh
->node
, np
->phandle
);
265 selftest(dup_count
== 0, "Found %i duplicates in %i phandles\n",
266 dup_count
, phandle_count
);
269 hash_for_each_safe(phandle_ht
, i
, tmp
, nh
, node
) {
275 static void __init
of_selftest_parse_phandle_with_args(void)
277 struct device_node
*np
;
278 struct of_phandle_args args
;
281 np
= of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
283 pr_err("missing testcase data\n");
287 rc
= of_count_phandle_with_args(np
, "phandle-list", "#phandle-cells");
288 selftest(rc
== 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc
);
290 for (i
= 0; i
< 8; i
++) {
292 rc
= of_parse_phandle_with_args(np
, "phandle-list",
293 "#phandle-cells", i
, &args
);
295 /* Test the values from tests-phandle.dtsi */
299 passed
&= (args
.args_count
== 1);
300 passed
&= (args
.args
[0] == (i
+ 1));
304 passed
&= (args
.args_count
== 2);
305 passed
&= (args
.args
[0] == (i
+ 1));
306 passed
&= (args
.args
[1] == 0);
309 passed
&= (rc
== -ENOENT
);
313 passed
&= (args
.args_count
== 3);
314 passed
&= (args
.args
[0] == (i
+ 1));
315 passed
&= (args
.args
[1] == 4);
316 passed
&= (args
.args
[2] == 3);
320 passed
&= (args
.args_count
== 2);
321 passed
&= (args
.args
[0] == (i
+ 1));
322 passed
&= (args
.args
[1] == 100);
326 passed
&= (args
.args_count
== 0);
330 passed
&= (args
.args_count
== 1);
331 passed
&= (args
.args
[0] == (i
+ 1));
334 passed
&= (rc
== -ENOENT
);
340 selftest(passed
, "index %i - data error on node %s rc=%i\n",
341 i
, args
.np
->full_name
, rc
);
344 /* Check for missing list property */
345 rc
= of_parse_phandle_with_args(np
, "phandle-list-missing",
346 "#phandle-cells", 0, &args
);
347 selftest(rc
== -ENOENT
, "expected:%i got:%i\n", -ENOENT
, rc
);
348 rc
= of_count_phandle_with_args(np
, "phandle-list-missing",
350 selftest(rc
== -ENOENT
, "expected:%i got:%i\n", -ENOENT
, rc
);
352 /* Check for missing cells property */
353 rc
= of_parse_phandle_with_args(np
, "phandle-list",
354 "#phandle-cells-missing", 0, &args
);
355 selftest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
356 rc
= of_count_phandle_with_args(np
, "phandle-list",
357 "#phandle-cells-missing");
358 selftest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
360 /* Check for bad phandle in list */
361 rc
= of_parse_phandle_with_args(np
, "phandle-list-bad-phandle",
362 "#phandle-cells", 0, &args
);
363 selftest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
364 rc
= of_count_phandle_with_args(np
, "phandle-list-bad-phandle",
366 selftest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
368 /* Check for incorrectly formed argument list */
369 rc
= of_parse_phandle_with_args(np
, "phandle-list-bad-args",
370 "#phandle-cells", 1, &args
);
371 selftest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
372 rc
= of_count_phandle_with_args(np
, "phandle-list-bad-args",
374 selftest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
377 static void __init
of_selftest_property_string(void)
379 const char *strings
[4];
380 struct device_node
*np
;
383 np
= of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
385 pr_err("No testcase data in device tree\n");
389 rc
= of_property_match_string(np
, "phandle-list-names", "first");
390 selftest(rc
== 0, "first expected:0 got:%i\n", rc
);
391 rc
= of_property_match_string(np
, "phandle-list-names", "second");
392 selftest(rc
== 1, "second expected:1 got:%i\n", rc
);
393 rc
= of_property_match_string(np
, "phandle-list-names", "third");
394 selftest(rc
== 2, "third expected:2 got:%i\n", rc
);
395 rc
= of_property_match_string(np
, "phandle-list-names", "fourth");
396 selftest(rc
== -ENODATA
, "unmatched string; rc=%i\n", rc
);
397 rc
= of_property_match_string(np
, "missing-property", "blah");
398 selftest(rc
== -EINVAL
, "missing property; rc=%i\n", rc
);
399 rc
= of_property_match_string(np
, "empty-property", "blah");
400 selftest(rc
== -ENODATA
, "empty property; rc=%i\n", rc
);
401 rc
= of_property_match_string(np
, "unterminated-string", "blah");
402 selftest(rc
== -EILSEQ
, "unterminated string; rc=%i\n", rc
);
404 /* of_property_count_strings() tests */
405 rc
= of_property_count_strings(np
, "string-property");
406 selftest(rc
== 1, "Incorrect string count; rc=%i\n", rc
);
407 rc
= of_property_count_strings(np
, "phandle-list-names");
408 selftest(rc
== 3, "Incorrect string count; rc=%i\n", rc
);
409 rc
= of_property_count_strings(np
, "unterminated-string");
410 selftest(rc
== -EILSEQ
, "unterminated string; rc=%i\n", rc
);
411 rc
= of_property_count_strings(np
, "unterminated-string-list");
412 selftest(rc
== -EILSEQ
, "unterminated string array; rc=%i\n", rc
);
414 /* of_property_read_string_index() tests */
415 rc
= of_property_read_string_index(np
, "string-property", 0, strings
);
416 selftest(rc
== 0 && !strcmp(strings
[0], "foobar"), "of_property_read_string_index() failure; rc=%i\n", rc
);
418 rc
= of_property_read_string_index(np
, "string-property", 1, strings
);
419 selftest(rc
== -ENODATA
&& strings
[0] == NULL
, "of_property_read_string_index() failure; rc=%i\n", rc
);
420 rc
= of_property_read_string_index(np
, "phandle-list-names", 0, strings
);
421 selftest(rc
== 0 && !strcmp(strings
[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc
);
422 rc
= of_property_read_string_index(np
, "phandle-list-names", 1, strings
);
423 selftest(rc
== 0 && !strcmp(strings
[0], "second"), "of_property_read_string_index() failure; rc=%i\n", rc
);
424 rc
= of_property_read_string_index(np
, "phandle-list-names", 2, strings
);
425 selftest(rc
== 0 && !strcmp(strings
[0], "third"), "of_property_read_string_index() failure; rc=%i\n", rc
);
427 rc
= of_property_read_string_index(np
, "phandle-list-names", 3, strings
);
428 selftest(rc
== -ENODATA
&& strings
[0] == NULL
, "of_property_read_string_index() failure; rc=%i\n", rc
);
430 rc
= of_property_read_string_index(np
, "unterminated-string", 0, strings
);
431 selftest(rc
== -EILSEQ
&& strings
[0] == NULL
, "of_property_read_string_index() failure; rc=%i\n", rc
);
432 rc
= of_property_read_string_index(np
, "unterminated-string-list", 0, strings
);
433 selftest(rc
== 0 && !strcmp(strings
[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc
);
435 rc
= of_property_read_string_index(np
, "unterminated-string-list", 2, strings
); /* should fail */
436 selftest(rc
== -EILSEQ
&& strings
[0] == NULL
, "of_property_read_string_index() failure; rc=%i\n", rc
);
439 /* of_property_read_string_array() tests */
440 rc
= of_property_read_string_array(np
, "string-property", strings
, 4);
441 selftest(rc
== 1, "Incorrect string count; rc=%i\n", rc
);
442 rc
= of_property_read_string_array(np
, "phandle-list-names", strings
, 4);
443 selftest(rc
== 3, "Incorrect string count; rc=%i\n", rc
);
444 rc
= of_property_read_string_array(np
, "unterminated-string", strings
, 4);
445 selftest(rc
== -EILSEQ
, "unterminated string; rc=%i\n", rc
);
446 /* -- An incorrectly formed string should cause a failure */
447 rc
= of_property_read_string_array(np
, "unterminated-string-list", strings
, 4);
448 selftest(rc
== -EILSEQ
, "unterminated string array; rc=%i\n", rc
);
449 /* -- parsing the correctly formed strings should still work: */
451 rc
= of_property_read_string_array(np
, "unterminated-string-list", strings
, 2);
452 selftest(rc
== 2 && strings
[2] == NULL
, "of_property_read_string_array() failure; rc=%i\n", rc
);
454 rc
= of_property_read_string_array(np
, "phandle-list-names", strings
, 1);
455 selftest(rc
== 1 && strings
[1] == NULL
, "Overwrote end of string array; rc=%i, str='%s'\n", rc
, strings
[1]);
458 #define propcmp(p1, p2) (((p1)->length == (p2)->length) && \
459 (p1)->value && (p2)->value && \
460 !memcmp((p1)->value, (p2)->value, (p1)->length) && \
461 !strcmp((p1)->name, (p2)->name))
462 static void __init
of_selftest_property_copy(void)
464 #ifdef CONFIG_OF_DYNAMIC
465 struct property p1
= { .name
= "p1", .length
= 0, .value
= "" };
466 struct property p2
= { .name
= "p2", .length
= 5, .value
= "abcd" };
467 struct property
*new;
469 new = __of_prop_dup(&p1
, GFP_KERNEL
);
470 selftest(new && propcmp(&p1
, new), "empty property didn't copy correctly\n");
475 new = __of_prop_dup(&p2
, GFP_KERNEL
);
476 selftest(new && propcmp(&p2
, new), "non-empty property didn't copy correctly\n");
483 static void __init
of_selftest_changeset(void)
485 #ifdef CONFIG_OF_DYNAMIC
486 struct property
*ppadd
, padd
= { .name
= "prop-add", .length
= 0, .value
= "" };
487 struct property
*ppupdate
, pupdate
= { .name
= "prop-update", .length
= 5, .value
= "abcd" };
488 struct property
*ppremove
;
489 struct device_node
*n1
, *n2
, *n21
, *nremove
, *parent
, *np
;
490 struct of_changeset chgset
;
492 n1
= __of_node_dup(NULL
, "/testcase-data/changeset/n1");
493 selftest(n1
, "testcase setup failure\n");
494 n2
= __of_node_dup(NULL
, "/testcase-data/changeset/n2");
495 selftest(n2
, "testcase setup failure\n");
496 n21
= __of_node_dup(NULL
, "%s/%s", "/testcase-data/changeset/n2", "n21");
497 selftest(n21
, "testcase setup failure %p\n", n21
);
498 nremove
= of_find_node_by_path("/testcase-data/changeset/node-remove");
499 selftest(nremove
, "testcase setup failure\n");
500 ppadd
= __of_prop_dup(&padd
, GFP_KERNEL
);
501 selftest(ppadd
, "testcase setup failure\n");
502 ppupdate
= __of_prop_dup(&pupdate
, GFP_KERNEL
);
503 selftest(ppupdate
, "testcase setup failure\n");
504 parent
= nremove
->parent
;
509 ppremove
= of_find_property(parent
, "prop-remove", NULL
);
510 selftest(ppremove
, "failed to find removal prop");
512 of_changeset_init(&chgset
);
513 selftest(!of_changeset_attach_node(&chgset
, n1
), "fail attach n1\n");
514 selftest(!of_changeset_attach_node(&chgset
, n2
), "fail attach n2\n");
515 selftest(!of_changeset_detach_node(&chgset
, nremove
), "fail remove node\n");
516 selftest(!of_changeset_attach_node(&chgset
, n21
), "fail attach n21\n");
517 selftest(!of_changeset_add_property(&chgset
, parent
, ppadd
), "fail add prop\n");
518 selftest(!of_changeset_update_property(&chgset
, parent
, ppupdate
), "fail update prop\n");
519 selftest(!of_changeset_remove_property(&chgset
, parent
, ppremove
), "fail remove prop\n");
520 mutex_lock(&of_mutex
);
521 selftest(!of_changeset_apply(&chgset
), "apply failed\n");
522 mutex_unlock(&of_mutex
);
524 /* Make sure node names are constructed correctly */
525 selftest((np
= of_find_node_by_path("/testcase-data/changeset/n2/n21")),
526 "'%s' not added\n", n21
->full_name
);
529 mutex_lock(&of_mutex
);
530 selftest(!of_changeset_revert(&chgset
), "revert failed\n");
531 mutex_unlock(&of_mutex
);
533 of_changeset_destroy(&chgset
);
537 static void __init
of_selftest_parse_interrupts(void)
539 struct device_node
*np
;
540 struct of_phandle_args args
;
543 np
= of_find_node_by_path("/testcase-data/interrupts/interrupts0");
545 pr_err("missing testcase data\n");
549 for (i
= 0; i
< 4; i
++) {
552 rc
= of_irq_parse_one(np
, i
, &args
);
555 passed
&= (args
.args_count
== 1);
556 passed
&= (args
.args
[0] == (i
+ 1));
558 selftest(passed
, "index %i - data error on node %s rc=%i\n",
559 i
, args
.np
->full_name
, rc
);
563 np
= of_find_node_by_path("/testcase-data/interrupts/interrupts1");
565 pr_err("missing testcase data\n");
569 for (i
= 0; i
< 4; i
++) {
572 rc
= of_irq_parse_one(np
, i
, &args
);
574 /* Test the values from tests-phandle.dtsi */
578 passed
&= (args
.args_count
== 1);
579 passed
&= (args
.args
[0] == 9);
583 passed
&= (args
.args_count
== 3);
584 passed
&= (args
.args
[0] == 10);
585 passed
&= (args
.args
[1] == 11);
586 passed
&= (args
.args
[2] == 12);
590 passed
&= (args
.args_count
== 2);
591 passed
&= (args
.args
[0] == 13);
592 passed
&= (args
.args
[1] == 14);
596 passed
&= (args
.args_count
== 2);
597 passed
&= (args
.args
[0] == 15);
598 passed
&= (args
.args
[1] == 16);
603 selftest(passed
, "index %i - data error on node %s rc=%i\n",
604 i
, args
.np
->full_name
, rc
);
609 static void __init
of_selftest_parse_interrupts_extended(void)
611 struct device_node
*np
;
612 struct of_phandle_args args
;
615 np
= of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
617 pr_err("missing testcase data\n");
621 for (i
= 0; i
< 7; i
++) {
623 rc
= of_irq_parse_one(np
, i
, &args
);
625 /* Test the values from tests-phandle.dtsi */
629 passed
&= (args
.args_count
== 1);
630 passed
&= (args
.args
[0] == 1);
634 passed
&= (args
.args_count
== 3);
635 passed
&= (args
.args
[0] == 2);
636 passed
&= (args
.args
[1] == 3);
637 passed
&= (args
.args
[2] == 4);
641 passed
&= (args
.args_count
== 2);
642 passed
&= (args
.args
[0] == 5);
643 passed
&= (args
.args
[1] == 6);
647 passed
&= (args
.args_count
== 1);
648 passed
&= (args
.args
[0] == 9);
652 passed
&= (args
.args_count
== 3);
653 passed
&= (args
.args
[0] == 10);
654 passed
&= (args
.args
[1] == 11);
655 passed
&= (args
.args
[2] == 12);
659 passed
&= (args
.args_count
== 2);
660 passed
&= (args
.args
[0] == 13);
661 passed
&= (args
.args
[1] == 14);
665 passed
&= (args
.args_count
== 1);
666 passed
&= (args
.args
[0] == 15);
672 selftest(passed
, "index %i - data error on node %s rc=%i\n",
673 i
, args
.np
->full_name
, rc
);
678 static struct of_device_id match_node_table
[] = {
679 { .data
= "A", .name
= "name0", }, /* Name alone is lowest priority */
680 { .data
= "B", .type
= "type1", }, /* followed by type alone */
682 { .data
= "Ca", .name
= "name2", .type
= "type1", }, /* followed by both together */
683 { .data
= "Cb", .name
= "name2", }, /* Only match when type doesn't match */
684 { .data
= "Cc", .name
= "name2", .type
= "type2", },
686 { .data
= "E", .compatible
= "compat3" },
687 { .data
= "G", .compatible
= "compat2", },
688 { .data
= "H", .compatible
= "compat2", .name
= "name5", },
689 { .data
= "I", .compatible
= "compat2", .type
= "type1", },
690 { .data
= "J", .compatible
= "compat2", .type
= "type1", .name
= "name8", },
691 { .data
= "K", .compatible
= "compat2", .name
= "name9", },
698 } match_node_tests
[] = {
699 { .path
= "/testcase-data/match-node/name0", .data
= "A", },
700 { .path
= "/testcase-data/match-node/name1", .data
= "B", },
701 { .path
= "/testcase-data/match-node/a/name2", .data
= "Ca", },
702 { .path
= "/testcase-data/match-node/b/name2", .data
= "Cb", },
703 { .path
= "/testcase-data/match-node/c/name2", .data
= "Cc", },
704 { .path
= "/testcase-data/match-node/name3", .data
= "E", },
705 { .path
= "/testcase-data/match-node/name4", .data
= "G", },
706 { .path
= "/testcase-data/match-node/name5", .data
= "H", },
707 { .path
= "/testcase-data/match-node/name6", .data
= "G", },
708 { .path
= "/testcase-data/match-node/name7", .data
= "I", },
709 { .path
= "/testcase-data/match-node/name8", .data
= "J", },
710 { .path
= "/testcase-data/match-node/name9", .data
= "K", },
713 static void __init
of_selftest_match_node(void)
715 struct device_node
*np
;
716 const struct of_device_id
*match
;
719 for (i
= 0; i
< ARRAY_SIZE(match_node_tests
); i
++) {
720 np
= of_find_node_by_path(match_node_tests
[i
].path
);
722 selftest(0, "missing testcase node %s\n",
723 match_node_tests
[i
].path
);
727 match
= of_match_node(match_node_table
, np
);
729 selftest(0, "%s didn't match anything\n",
730 match_node_tests
[i
].path
);
734 if (strcmp(match
->data
, match_node_tests
[i
].data
) != 0) {
735 selftest(0, "%s got wrong match. expected %s, got %s\n",
736 match_node_tests
[i
].path
, match_node_tests
[i
].data
,
737 (const char *)match
->data
);
740 selftest(1, "passed");
744 struct device test_bus
= {
745 .init_name
= "unittest-bus",
747 static void __init
of_selftest_platform_populate(void)
750 struct device_node
*np
, *child
, *grandchild
;
751 struct platform_device
*pdev
;
752 struct of_device_id match
[] = {
753 { .compatible
= "test-device", },
757 np
= of_find_node_by_path("/testcase-data");
758 of_platform_populate(np
, of_default_bus_match_table
, NULL
, NULL
);
760 /* Test that a missing irq domain returns -EPROBE_DEFER */
761 np
= of_find_node_by_path("/testcase-data/testcase-device1");
762 pdev
= of_find_device_by_node(np
);
763 selftest(pdev
, "device 1 creation failed\n");
765 irq
= platform_get_irq(pdev
, 0);
766 selftest(irq
== -EPROBE_DEFER
, "device deferred probe failed - %d\n", irq
);
768 /* Test that a parsing failure does not return -EPROBE_DEFER */
769 np
= of_find_node_by_path("/testcase-data/testcase-device2");
770 pdev
= of_find_device_by_node(np
);
771 selftest(pdev
, "device 2 creation failed\n");
772 irq
= platform_get_irq(pdev
, 0);
773 selftest(irq
< 0 && irq
!= -EPROBE_DEFER
, "device parsing error failed - %d\n", irq
);
775 if (selftest(np
= of_find_node_by_path("/testcase-data/platform-tests"),
776 "No testcase data in device tree\n"));
779 if (selftest(!(rc
= device_register(&test_bus
)),
780 "testbus registration failed; rc=%i\n", rc
));
783 for_each_child_of_node(np
, child
) {
784 of_platform_populate(child
, match
, NULL
, &test_bus
);
785 for_each_child_of_node(child
, grandchild
)
786 selftest(of_find_device_by_node(grandchild
),
787 "Could not create device for node '%s'\n",
791 of_platform_depopulate(&test_bus
);
792 for_each_child_of_node(np
, child
) {
793 for_each_child_of_node(child
, grandchild
)
794 selftest(!of_find_device_by_node(grandchild
),
795 "device didn't get destroyed '%s'\n",
799 device_unregister(&test_bus
);
804 * update_node_properties - adds the properties
805 * of np into dup node (present in live tree) and
806 * updates parent of children of np to dup.
808 * @np: node already present in live tree
809 * @dup: node present in live tree to be updated
811 static void update_node_properties(struct device_node
*np
,
812 struct device_node
*dup
)
814 struct property
*prop
;
815 struct device_node
*child
;
817 for_each_property_of_node(np
, prop
)
818 of_add_property(dup
, prop
);
820 for_each_child_of_node(np
, child
)
825 * attach_node_and_children - attaches nodes
826 * and its children to live tree
828 * @np: Node to attach to live tree
830 static int attach_node_and_children(struct device_node
*np
)
832 struct device_node
*next
, *dup
, *child
;
835 dup
= of_find_node_by_path(np
->full_name
);
837 update_node_properties(np
, dup
);
844 mutex_lock(&of_mutex
);
845 raw_spin_lock_irqsave(&devtree_lock
, flags
);
846 np
->sibling
= np
->parent
->child
;
847 np
->parent
->child
= np
;
848 of_node_clear_flag(np
, OF_DETACHED
);
849 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
851 __of_attach_node_sysfs(np
);
852 mutex_unlock(&of_mutex
);
855 next
= child
->sibling
;
856 attach_node_and_children(child
);
864 * selftest_data_add - Reads, copies data from
865 * linked tree and attaches it to the live tree
867 static int __init
selftest_data_add(void)
870 struct device_node
*selftest_data_node
, *np
;
871 extern uint8_t __dtb_testcases_begin
[];
872 extern uint8_t __dtb_testcases_end
[];
873 const int size
= __dtb_testcases_end
- __dtb_testcases_begin
;
877 pr_warn("%s: No testcase data to attach; not running tests\n",
883 selftest_data
= kmemdup(__dtb_testcases_begin
, size
, GFP_KERNEL
);
885 if (!selftest_data
) {
886 pr_warn("%s: Failed to allocate memory for selftest_data; "
887 "not running tests\n", __func__
);
890 of_fdt_unflatten_tree(selftest_data
, &selftest_data_node
);
891 if (!selftest_data_node
) {
892 pr_warn("%s: No tree to attach; not running tests\n", __func__
);
895 of_node_set_flag(selftest_data_node
, OF_DETACHED
);
896 rc
= of_resolve_phandles(selftest_data_node
);
898 pr_err("%s: Failed to resolve phandles (rc=%i)\n", __func__
, rc
);
903 of_root
= selftest_data_node
;
904 for_each_of_allnodes(np
)
905 __of_attach_node_sysfs(np
);
906 of_aliases
= of_find_node_by_path("/aliases");
907 of_chosen
= of_find_node_by_path("/chosen");
911 /* attach the sub-tree to live tree */
912 np
= selftest_data_node
->child
;
914 struct device_node
*next
= np
->sibling
;
915 np
->parent
= of_root
;
916 attach_node_and_children(np
);
922 #ifdef CONFIG_OF_OVERLAY
924 static int selftest_probe(struct platform_device
*pdev
)
926 struct device
*dev
= &pdev
->dev
;
927 struct device_node
*np
= dev
->of_node
;
930 dev_err(dev
, "No OF data for device\n");
935 dev_dbg(dev
, "%s for node @%s\n", __func__
, np
->full_name
);
937 of_platform_populate(np
, NULL
, NULL
, &pdev
->dev
);
942 static int selftest_remove(struct platform_device
*pdev
)
944 struct device
*dev
= &pdev
->dev
;
945 struct device_node
*np
= dev
->of_node
;
947 dev_dbg(dev
, "%s for node @%s\n", __func__
, np
->full_name
);
951 static struct of_device_id selftest_match
[] = {
952 { .compatible
= "selftest", },
956 static struct platform_driver selftest_driver
= {
957 .probe
= selftest_probe
,
958 .remove
= selftest_remove
,
961 .owner
= THIS_MODULE
,
962 .of_match_table
= of_match_ptr(selftest_match
),
966 /* get the platform device instantiated at the path */
967 static struct platform_device
*of_path_to_platform_device(const char *path
)
969 struct device_node
*np
;
970 struct platform_device
*pdev
;
972 np
= of_find_node_by_path(path
);
976 pdev
= of_find_device_by_node(np
);
982 /* find out if a platform device exists at that path */
983 static int of_path_platform_device_exists(const char *path
)
985 struct platform_device
*pdev
;
987 pdev
= of_path_to_platform_device(path
);
988 platform_device_put(pdev
);
992 #if IS_BUILTIN(CONFIG_I2C)
994 /* get the i2c client device instantiated at the path */
995 static struct i2c_client
*of_path_to_i2c_client(const char *path
)
997 struct device_node
*np
;
998 struct i2c_client
*client
;
1000 np
= of_find_node_by_path(path
);
1004 client
= of_find_i2c_device_by_node(np
);
1010 /* find out if a i2c client device exists at that path */
1011 static int of_path_i2c_client_exists(const char *path
)
1013 struct i2c_client
*client
;
1015 client
= of_path_to_i2c_client(path
);
1017 put_device(&client
->dev
);
1018 return client
!= NULL
;
1021 static int of_path_i2c_client_exists(const char *path
)
1032 static int of_path_device_type_exists(const char *path
,
1033 enum overlay_type ovtype
)
1037 return of_path_platform_device_exists(path
);
1039 return of_path_i2c_client_exists(path
);
1044 static const char *selftest_path(int nr
, enum overlay_type ovtype
)
1047 static char buf
[256];
1051 base
= "/testcase-data/overlay-node/test-bus";
1054 base
= "/testcase-data/overlay-node/test-bus/i2c-test-bus";
1060 snprintf(buf
, sizeof(buf
) - 1, "%s/test-selftest%d", base
, nr
);
1061 buf
[sizeof(buf
) - 1] = '\0';
1065 static int of_selftest_device_exists(int selftest_nr
, enum overlay_type ovtype
)
1069 path
= selftest_path(selftest_nr
, ovtype
);
1073 return of_path_platform_device_exists(path
);
1075 return of_path_i2c_client_exists(path
);
1080 static const char *overlay_path(int nr
)
1082 static char buf
[256];
1084 snprintf(buf
, sizeof(buf
) - 1,
1085 "/testcase-data/overlay%d", nr
);
1086 buf
[sizeof(buf
) - 1] = '\0';
1091 static const char *bus_path
= "/testcase-data/overlay-node/test-bus";
1093 static int of_selftest_apply_overlay(int selftest_nr
, int overlay_nr
,
1096 struct device_node
*np
= NULL
;
1099 np
= of_find_node_by_path(overlay_path(overlay_nr
));
1101 selftest(0, "could not find overlay node @\"%s\"\n",
1102 overlay_path(overlay_nr
));
1107 ret
= of_overlay_create(np
);
1109 selftest(0, "could not create overlay from \"%s\"\n",
1110 overlay_path(overlay_nr
));
1126 /* apply an overlay while checking before and after states */
1127 static int of_selftest_apply_overlay_check(int overlay_nr
, int selftest_nr
,
1128 int before
, int after
, enum overlay_type ovtype
)
1132 /* selftest device must not be in before state */
1133 if (of_selftest_device_exists(selftest_nr
, ovtype
) != before
) {
1134 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1135 overlay_path(overlay_nr
),
1136 selftest_path(selftest_nr
, ovtype
),
1137 !before
? "enabled" : "disabled");
1141 ret
= of_selftest_apply_overlay(overlay_nr
, selftest_nr
, NULL
);
1143 /* of_selftest_apply_overlay already called selftest() */
1147 /* selftest device must be to set to after state */
1148 if (of_selftest_device_exists(selftest_nr
, ovtype
) != after
) {
1149 selftest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n",
1150 overlay_path(overlay_nr
),
1151 selftest_path(selftest_nr
, ovtype
),
1152 !after
? "enabled" : "disabled");
1159 /* apply an overlay and then revert it while checking before, after states */
1160 static int of_selftest_apply_revert_overlay_check(int overlay_nr
,
1161 int selftest_nr
, int before
, int after
,
1162 enum overlay_type ovtype
)
1166 /* selftest device must be in before state */
1167 if (of_selftest_device_exists(selftest_nr
, ovtype
) != before
) {
1168 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1169 overlay_path(overlay_nr
),
1170 selftest_path(selftest_nr
, ovtype
),
1171 !before
? "enabled" : "disabled");
1175 /* apply the overlay */
1176 ret
= of_selftest_apply_overlay(overlay_nr
, selftest_nr
, &ov_id
);
1178 /* of_selftest_apply_overlay already called selftest() */
1182 /* selftest device must be in after state */
1183 if (of_selftest_device_exists(selftest_nr
, ovtype
) != after
) {
1184 selftest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n",
1185 overlay_path(overlay_nr
),
1186 selftest_path(selftest_nr
, ovtype
),
1187 !after
? "enabled" : "disabled");
1191 ret
= of_overlay_destroy(ov_id
);
1193 selftest(0, "overlay @\"%s\" failed to be destroyed @\"%s\"\n",
1194 overlay_path(overlay_nr
),
1195 selftest_path(selftest_nr
, ovtype
));
1199 /* selftest device must be again in before state */
1200 if (of_selftest_device_exists(selftest_nr
, PDEV_OVERLAY
) != before
) {
1201 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1202 overlay_path(overlay_nr
),
1203 selftest_path(selftest_nr
, ovtype
),
1204 !before
? "enabled" : "disabled");
1211 /* test activation of device */
1212 static void of_selftest_overlay_0(void)
1216 /* device should enable */
1217 ret
= of_selftest_apply_overlay_check(0, 0, 0, 1, PDEV_OVERLAY
);
1221 selftest(1, "overlay test %d passed\n", 0);
1224 /* test deactivation of device */
1225 static void of_selftest_overlay_1(void)
1229 /* device should disable */
1230 ret
= of_selftest_apply_overlay_check(1, 1, 1, 0, PDEV_OVERLAY
);
1234 selftest(1, "overlay test %d passed\n", 1);
1237 /* test activation of device */
1238 static void of_selftest_overlay_2(void)
1242 /* device should enable */
1243 ret
= of_selftest_apply_overlay_check(2, 2, 0, 1, PDEV_OVERLAY
);
1247 selftest(1, "overlay test %d passed\n", 2);
1250 /* test deactivation of device */
1251 static void of_selftest_overlay_3(void)
1255 /* device should disable */
1256 ret
= of_selftest_apply_overlay_check(3, 3, 1, 0, PDEV_OVERLAY
);
1260 selftest(1, "overlay test %d passed\n", 3);
1263 /* test activation of a full device node */
1264 static void of_selftest_overlay_4(void)
1268 /* device should disable */
1269 ret
= of_selftest_apply_overlay_check(4, 4, 0, 1, PDEV_OVERLAY
);
1273 selftest(1, "overlay test %d passed\n", 4);
1276 /* test overlay apply/revert sequence */
1277 static void of_selftest_overlay_5(void)
1281 /* device should disable */
1282 ret
= of_selftest_apply_revert_overlay_check(5, 5, 0, 1, PDEV_OVERLAY
);
1286 selftest(1, "overlay test %d passed\n", 5);
1289 /* test overlay application in sequence */
1290 static void of_selftest_overlay_6(void)
1292 struct device_node
*np
;
1293 int ret
, i
, ov_id
[2];
1294 int overlay_nr
= 6, selftest_nr
= 6;
1295 int before
= 0, after
= 1;
1297 /* selftest device must be in before state */
1298 for (i
= 0; i
< 2; i
++) {
1299 if (of_selftest_device_exists(selftest_nr
+ i
, PDEV_OVERLAY
)
1301 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1302 overlay_path(overlay_nr
+ i
),
1303 selftest_path(selftest_nr
+ i
,
1305 !before
? "enabled" : "disabled");
1310 /* apply the overlays */
1311 for (i
= 0; i
< 2; i
++) {
1313 np
= of_find_node_by_path(overlay_path(overlay_nr
+ i
));
1315 selftest(0, "could not find overlay node @\"%s\"\n",
1316 overlay_path(overlay_nr
+ i
));
1320 ret
= of_overlay_create(np
);
1322 selftest(0, "could not create overlay from \"%s\"\n",
1323 overlay_path(overlay_nr
+ i
));
1329 for (i
= 0; i
< 2; i
++) {
1330 /* selftest device must be in after state */
1331 if (of_selftest_device_exists(selftest_nr
+ i
, PDEV_OVERLAY
)
1333 selftest(0, "overlay @\"%s\" failed @\"%s\" %s\n",
1334 overlay_path(overlay_nr
+ i
),
1335 selftest_path(selftest_nr
+ i
,
1337 !after
? "enabled" : "disabled");
1342 for (i
= 1; i
>= 0; i
--) {
1343 ret
= of_overlay_destroy(ov_id
[i
]);
1345 selftest(0, "overlay @\"%s\" failed destroy @\"%s\"\n",
1346 overlay_path(overlay_nr
+ i
),
1347 selftest_path(selftest_nr
+ i
,
1353 for (i
= 0; i
< 2; i
++) {
1354 /* selftest device must be again in before state */
1355 if (of_selftest_device_exists(selftest_nr
+ i
, PDEV_OVERLAY
)
1357 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1358 overlay_path(overlay_nr
+ i
),
1359 selftest_path(selftest_nr
+ i
,
1361 !before
? "enabled" : "disabled");
1366 selftest(1, "overlay test %d passed\n", 6);
1369 /* test overlay application in sequence */
1370 static void of_selftest_overlay_8(void)
1372 struct device_node
*np
;
1373 int ret
, i
, ov_id
[2];
1374 int overlay_nr
= 8, selftest_nr
= 8;
1376 /* we don't care about device state in this test */
1378 /* apply the overlays */
1379 for (i
= 0; i
< 2; i
++) {
1381 np
= of_find_node_by_path(overlay_path(overlay_nr
+ i
));
1383 selftest(0, "could not find overlay node @\"%s\"\n",
1384 overlay_path(overlay_nr
+ i
));
1388 ret
= of_overlay_create(np
);
1390 selftest(0, "could not create overlay from \"%s\"\n",
1391 overlay_path(overlay_nr
+ i
));
1397 /* now try to remove first overlay (it should fail) */
1398 ret
= of_overlay_destroy(ov_id
[0]);
1400 selftest(0, "overlay @\"%s\" was destroyed @\"%s\"\n",
1401 overlay_path(overlay_nr
+ 0),
1402 selftest_path(selftest_nr
,
1407 /* removing them in order should work */
1408 for (i
= 1; i
>= 0; i
--) {
1409 ret
= of_overlay_destroy(ov_id
[i
]);
1411 selftest(0, "overlay @\"%s\" not destroyed @\"%s\"\n",
1412 overlay_path(overlay_nr
+ i
),
1413 selftest_path(selftest_nr
,
1419 selftest(1, "overlay test %d passed\n", 8);
1422 /* test insertion of a bus with parent devices */
1423 static void of_selftest_overlay_10(void)
1428 /* device should disable */
1429 ret
= of_selftest_apply_overlay_check(10, 10, 0, 1, PDEV_OVERLAY
);
1430 if (selftest(ret
== 0,
1431 "overlay test %d failed; overlay application\n", 10))
1434 child_path
= kasprintf(GFP_KERNEL
, "%s/test-selftest101",
1435 selftest_path(10, PDEV_OVERLAY
));
1436 if (selftest(child_path
, "overlay test %d failed; kasprintf\n", 10))
1439 ret
= of_path_device_type_exists(child_path
, PDEV_OVERLAY
);
1441 if (selftest(ret
, "overlay test %d failed; no child device\n", 10))
1445 /* test insertion of a bus with parent devices (and revert) */
1446 static void of_selftest_overlay_11(void)
1450 /* device should disable */
1451 ret
= of_selftest_apply_revert_overlay_check(11, 11, 0, 1,
1453 if (selftest(ret
== 0,
1454 "overlay test %d failed; overlay application\n", 11))
1458 #if IS_BUILTIN(CONFIG_I2C) && IS_ENABLED(CONFIG_OF_OVERLAY)
1460 struct selftest_i2c_bus_data
{
1461 struct platform_device
*pdev
;
1462 struct i2c_adapter adap
;
1465 static int selftest_i2c_master_xfer(struct i2c_adapter
*adap
,
1466 struct i2c_msg
*msgs
, int num
)
1468 struct selftest_i2c_bus_data
*std
= i2c_get_adapdata(adap
);
1475 static u32
selftest_i2c_functionality(struct i2c_adapter
*adap
)
1477 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
;
1480 static const struct i2c_algorithm selftest_i2c_algo
= {
1481 .master_xfer
= selftest_i2c_master_xfer
,
1482 .functionality
= selftest_i2c_functionality
,
1485 static int selftest_i2c_bus_probe(struct platform_device
*pdev
)
1487 struct device
*dev
= &pdev
->dev
;
1488 struct device_node
*np
= dev
->of_node
;
1489 struct selftest_i2c_bus_data
*std
;
1490 struct i2c_adapter
*adap
;
1494 dev_err(dev
, "No OF data for device\n");
1499 dev_dbg(dev
, "%s for node @%s\n", __func__
, np
->full_name
);
1501 std
= devm_kzalloc(dev
, sizeof(*std
), GFP_KERNEL
);
1503 dev_err(dev
, "Failed to allocate selftest i2c data\n");
1507 /* link them together */
1509 platform_set_drvdata(pdev
, std
);
1512 i2c_set_adapdata(adap
, std
);
1514 strlcpy(adap
->name
, pdev
->name
, sizeof(adap
->name
));
1515 adap
->class = I2C_CLASS_DEPRECATED
;
1516 adap
->algo
= &selftest_i2c_algo
;
1517 adap
->dev
.parent
= dev
;
1518 adap
->dev
.of_node
= dev
->of_node
;
1519 adap
->timeout
= 5 * HZ
;
1522 ret
= i2c_add_numbered_adapter(adap
);
1524 dev_err(dev
, "Failed to add I2C adapter\n");
1531 static int selftest_i2c_bus_remove(struct platform_device
*pdev
)
1533 struct device
*dev
= &pdev
->dev
;
1534 struct device_node
*np
= dev
->of_node
;
1535 struct selftest_i2c_bus_data
*std
= platform_get_drvdata(pdev
);
1537 dev_dbg(dev
, "%s for node @%s\n", __func__
, np
->full_name
);
1538 i2c_del_adapter(&std
->adap
);
1543 static struct of_device_id selftest_i2c_bus_match
[] = {
1544 { .compatible
= "selftest-i2c-bus", },
1548 static struct platform_driver selftest_i2c_bus_driver
= {
1549 .probe
= selftest_i2c_bus_probe
,
1550 .remove
= selftest_i2c_bus_remove
,
1552 .name
= "selftest-i2c-bus",
1553 .of_match_table
= of_match_ptr(selftest_i2c_bus_match
),
1557 static int selftest_i2c_dev_probe(struct i2c_client
*client
,
1558 const struct i2c_device_id
*id
)
1560 struct device
*dev
= &client
->dev
;
1561 struct device_node
*np
= client
->dev
.of_node
;
1564 dev_err(dev
, "No OF node\n");
1568 dev_dbg(dev
, "%s for node @%s\n", __func__
, np
->full_name
);
1573 static int selftest_i2c_dev_remove(struct i2c_client
*client
)
1575 struct device
*dev
= &client
->dev
;
1576 struct device_node
*np
= client
->dev
.of_node
;
1578 dev_dbg(dev
, "%s for node @%s\n", __func__
, np
->full_name
);
1582 static const struct i2c_device_id selftest_i2c_dev_id
[] = {
1583 { .name
= "selftest-i2c-dev" },
1587 static struct i2c_driver selftest_i2c_dev_driver
= {
1589 .name
= "selftest-i2c-dev",
1590 .owner
= THIS_MODULE
,
1592 .probe
= selftest_i2c_dev_probe
,
1593 .remove
= selftest_i2c_dev_remove
,
1594 .id_table
= selftest_i2c_dev_id
,
1597 #if IS_BUILTIN(CONFIG_I2C_MUX)
1599 struct selftest_i2c_mux_data
{
1601 struct i2c_adapter
*adap
[];
1604 static int selftest_i2c_mux_select_chan(struct i2c_adapter
*adap
,
1605 void *client
, u32 chan
)
1610 static int selftest_i2c_mux_probe(struct i2c_client
*client
,
1611 const struct i2c_device_id
*id
)
1613 int ret
, i
, nchans
, size
;
1614 struct device
*dev
= &client
->dev
;
1615 struct i2c_adapter
*adap
= to_i2c_adapter(dev
->parent
);
1616 struct device_node
*np
= client
->dev
.of_node
, *child
;
1617 struct selftest_i2c_mux_data
*stm
;
1620 dev_dbg(dev
, "%s for node @%s\n", __func__
, np
->full_name
);
1623 dev_err(dev
, "No OF node\n");
1628 for_each_child_of_node(np
, child
) {
1629 ret
= of_property_read_u32(child
, "reg", ®
);
1632 if (max_reg
== (u32
)-1 || reg
> max_reg
)
1635 nchans
= max_reg
== (u32
)-1 ? 0 : max_reg
+ 1;
1637 dev_err(dev
, "No channels\n");
1641 size
= offsetof(struct selftest_i2c_mux_data
, adap
[nchans
]);
1642 stm
= devm_kzalloc(dev
, size
, GFP_KERNEL
);
1644 dev_err(dev
, "Out of memory\n");
1647 stm
->nchans
= nchans
;
1648 for (i
= 0; i
< nchans
; i
++) {
1649 stm
->adap
[i
] = i2c_add_mux_adapter(adap
, dev
, client
,
1650 0, i
, 0, selftest_i2c_mux_select_chan
, NULL
);
1651 if (!stm
->adap
[i
]) {
1652 dev_err(dev
, "Failed to register mux #%d\n", i
);
1653 for (i
--; i
>= 0; i
--)
1654 i2c_del_mux_adapter(stm
->adap
[i
]);
1659 i2c_set_clientdata(client
, stm
);
1664 static int selftest_i2c_mux_remove(struct i2c_client
*client
)
1666 struct device
*dev
= &client
->dev
;
1667 struct device_node
*np
= client
->dev
.of_node
;
1668 struct selftest_i2c_mux_data
*stm
= i2c_get_clientdata(client
);
1671 dev_dbg(dev
, "%s for node @%s\n", __func__
, np
->full_name
);
1672 for (i
= stm
->nchans
- 1; i
>= 0; i
--)
1673 i2c_del_mux_adapter(stm
->adap
[i
]);
1677 static const struct i2c_device_id selftest_i2c_mux_id
[] = {
1678 { .name
= "selftest-i2c-mux" },
1682 static struct i2c_driver selftest_i2c_mux_driver
= {
1684 .name
= "selftest-i2c-mux",
1685 .owner
= THIS_MODULE
,
1687 .probe
= selftest_i2c_mux_probe
,
1688 .remove
= selftest_i2c_mux_remove
,
1689 .id_table
= selftest_i2c_mux_id
,
1694 static int of_selftest_overlay_i2c_init(void)
1698 ret
= i2c_add_driver(&selftest_i2c_dev_driver
);
1699 if (selftest(ret
== 0,
1700 "could not register selftest i2c device driver\n"))
1703 ret
= platform_driver_register(&selftest_i2c_bus_driver
);
1704 if (selftest(ret
== 0,
1705 "could not register selftest i2c bus driver\n"))
1708 #if IS_BUILTIN(CONFIG_I2C_MUX)
1709 ret
= i2c_add_driver(&selftest_i2c_mux_driver
);
1710 if (selftest(ret
== 0,
1711 "could not register selftest i2c mux driver\n"))
1718 static void of_selftest_overlay_i2c_cleanup(void)
1720 #if IS_BUILTIN(CONFIG_I2C_MUX)
1721 i2c_del_driver(&selftest_i2c_mux_driver
);
1723 platform_driver_unregister(&selftest_i2c_bus_driver
);
1724 i2c_del_driver(&selftest_i2c_dev_driver
);
1727 static void of_selftest_overlay_i2c_12(void)
1731 /* device should enable */
1732 ret
= of_selftest_apply_overlay_check(12, 12, 0, 1, I2C_OVERLAY
);
1736 selftest(1, "overlay test %d passed\n", 12);
1739 /* test deactivation of device */
1740 static void of_selftest_overlay_i2c_13(void)
1744 /* device should disable */
1745 ret
= of_selftest_apply_overlay_check(13, 13, 1, 0, I2C_OVERLAY
);
1749 selftest(1, "overlay test %d passed\n", 13);
1752 /* just check for i2c mux existence */
1753 static void of_selftest_overlay_i2c_14(void)
1757 static void of_selftest_overlay_i2c_15(void)
1761 /* device should enable */
1762 ret
= of_selftest_apply_overlay_check(16, 15, 0, 1, I2C_OVERLAY
);
1766 selftest(1, "overlay test %d passed\n", 15);
1771 static inline void of_selftest_overlay_i2c_14(void) { }
1772 static inline void of_selftest_overlay_i2c_15(void) { }
1776 static void __init
of_selftest_overlay(void)
1778 struct device_node
*bus_np
= NULL
;
1781 ret
= platform_driver_register(&selftest_driver
);
1783 selftest(0, "could not register selftest driver\n");
1787 bus_np
= of_find_node_by_path(bus_path
);
1788 if (bus_np
== NULL
) {
1789 selftest(0, "could not find bus_path \"%s\"\n", bus_path
);
1793 ret
= of_platform_populate(bus_np
, of_default_bus_match_table
,
1796 selftest(0, "could not populate bus @ \"%s\"\n", bus_path
);
1800 if (!of_selftest_device_exists(100, PDEV_OVERLAY
)) {
1801 selftest(0, "could not find selftest0 @ \"%s\"\n",
1802 selftest_path(100, PDEV_OVERLAY
));
1806 if (of_selftest_device_exists(101, PDEV_OVERLAY
)) {
1807 selftest(0, "selftest1 @ \"%s\" should not exist\n",
1808 selftest_path(101, PDEV_OVERLAY
));
1812 selftest(1, "basic infrastructure of overlays passed");
1814 /* tests in sequence */
1815 of_selftest_overlay_0();
1816 of_selftest_overlay_1();
1817 of_selftest_overlay_2();
1818 of_selftest_overlay_3();
1819 of_selftest_overlay_4();
1820 of_selftest_overlay_5();
1821 of_selftest_overlay_6();
1822 of_selftest_overlay_8();
1824 of_selftest_overlay_10();
1825 of_selftest_overlay_11();
1827 #if IS_BUILTIN(CONFIG_I2C)
1828 if (selftest(of_selftest_overlay_i2c_init() == 0, "i2c init failed\n"))
1831 of_selftest_overlay_i2c_12();
1832 of_selftest_overlay_i2c_13();
1833 of_selftest_overlay_i2c_14();
1834 of_selftest_overlay_i2c_15();
1836 of_selftest_overlay_i2c_cleanup();
1840 of_node_put(bus_np
);
1844 static inline void __init
of_selftest_overlay(void) { }
1847 static int __init
of_selftest(void)
1849 struct device_node
*np
;
1852 /* adding data for selftest */
1853 res
= selftest_data_add();
1857 of_aliases
= of_find_node_by_path("/aliases");
1859 np
= of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
1861 pr_info("No testcase data in device tree; not running tests\n");
1866 pr_info("start of selftest - you will see error messages\n");
1867 of_selftest_check_tree_linkage();
1868 of_selftest_check_phandles();
1869 of_selftest_find_node_by_name();
1870 of_selftest_dynamic();
1871 of_selftest_parse_phandle_with_args();
1872 of_selftest_property_string();
1873 of_selftest_property_copy();
1874 of_selftest_changeset();
1875 of_selftest_parse_interrupts();
1876 of_selftest_parse_interrupts_extended();
1877 of_selftest_match_node();
1878 of_selftest_platform_populate();
1879 of_selftest_overlay();
1881 /* Double check linkage after removing testcase data */
1882 of_selftest_check_tree_linkage();
1884 pr_info("end of selftest - %i passed, %i failed\n",
1885 selftest_results
.passed
, selftest_results
.failed
);
1889 late_initcall(of_selftest
);