of: base: improve of_get_next_child() kernel-doc
[deliverable/linux.git] / drivers / of / unittest.c
CommitLineData
53a42093
GL
1/*
2 * Self tests for device tree subsystem
3 */
4
cabb7d5b 5#define pr_fmt(fmt) "### dt-test ### " fmt
53a42093
GL
6
7#include <linux/clk.h>
8#include <linux/err.h>
9#include <linux/errno.h>
841ec213 10#include <linux/hashtable.h>
53a42093
GL
11#include <linux/module.h>
12#include <linux/of.h>
ae9304c9 13#include <linux/of_fdt.h>
a9f10ca7 14#include <linux/of_irq.h>
82c0f589 15#include <linux/of_platform.h>
53a42093
GL
16#include <linux/list.h>
17#include <linux/mutex.h>
18#include <linux/slab.h>
19#include <linux/device.h>
177d271c
PA
20#include <linux/platform_device.h>
21#include <linux/of_platform.h>
53a42093 22
d5e75500
PA
23#include <linux/i2c.h>
24#include <linux/i2c-mux.h>
25
492a22ac
PA
26#include <linux/bitops.h>
27
69843396
PA
28#include "of_private.h"
29
9697a559 30static struct unittest_results {
a9f10ca7
GL
31 int passed;
32 int failed;
9697a559 33} unittest_results;
a9f10ca7 34
9697a559 35#define unittest(result, fmt, ...) ({ \
851da976
GL
36 bool failed = !(result); \
37 if (failed) { \
9697a559 38 unittest_results.failed++; \
a9f10ca7 39 pr_err("FAIL %s():%i " fmt, __func__, __LINE__, ##__VA_ARGS__); \
cabb7d5b 40 } else { \
9697a559 41 unittest_results.passed++; \
a9f10ca7 42 pr_debug("pass %s():%i\n", __func__, __LINE__); \
cabb7d5b 43 } \
851da976
GL
44 failed; \
45})
53a42093 46
9697a559 47static void __init of_unittest_find_node_by_name(void)
ae91ff72
GL
48{
49 struct device_node *np;
75c28c09 50 const char *options;
ae91ff72
GL
51
52 np = of_find_node_by_path("/testcase-data");
9697a559 53 unittest(np && !strcmp("/testcase-data", np->full_name),
ae91ff72
GL
54 "find /testcase-data failed\n");
55 of_node_put(np);
56
57 /* Test if trailing '/' works */
58 np = of_find_node_by_path("/testcase-data/");
9697a559 59 unittest(!np, "trailing '/' on /testcase-data/ should fail\n");
ae91ff72
GL
60
61 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
9697a559 62 unittest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", np->full_name),
ae91ff72
GL
63 "find /testcase-data/phandle-tests/consumer-a failed\n");
64 of_node_put(np);
65
66 np = of_find_node_by_path("testcase-alias");
9697a559 67 unittest(np && !strcmp("/testcase-data", np->full_name),
ae91ff72
GL
68 "find testcase-alias failed\n");
69 of_node_put(np);
70
71 /* Test if trailing '/' works on aliases */
72 np = of_find_node_by_path("testcase-alias/");
9697a559 73 unittest(!np, "trailing '/' on testcase-alias/ should fail\n");
ae91ff72
GL
74
75 np = of_find_node_by_path("testcase-alias/phandle-tests/consumer-a");
9697a559 76 unittest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", np->full_name),
ae91ff72
GL
77 "find testcase-alias/phandle-tests/consumer-a failed\n");
78 of_node_put(np);
79
80 np = of_find_node_by_path("/testcase-data/missing-path");
9697a559 81 unittest(!np, "non-existent path returned node %s\n", np->full_name);
ae91ff72
GL
82 of_node_put(np);
83
84 np = of_find_node_by_path("missing-alias");
9697a559 85 unittest(!np, "non-existent alias returned node %s\n", np->full_name);
ae91ff72
GL
86 of_node_put(np);
87
88 np = of_find_node_by_path("testcase-alias/missing-path");
9697a559 89 unittest(!np, "non-existent alias with relative path returned node %s\n", np->full_name);
ae91ff72 90 of_node_put(np);
75c28c09
LL
91
92 np = of_find_node_opts_by_path("/testcase-data:testoption", &options);
9697a559 93 unittest(np && !strcmp("testoption", options),
75c28c09
LL
94 "option path test failed\n");
95 of_node_put(np);
96
8cbba1ab 97 np = of_find_node_opts_by_path("/testcase-data:test/option", &options);
9697a559 98 unittest(np && !strcmp("test/option", options),
8cbba1ab
PH
99 "option path test, subcase #1 failed\n");
100 of_node_put(np);
101
5ca1b0dd 102 np = of_find_node_opts_by_path("/testcase-data/testcase-device1:test/option", &options);
9697a559 103 unittest(np && !strcmp("test/option", options),
5ca1b0dd
BN
104 "option path test, subcase #2 failed\n");
105 of_node_put(np);
106
75c28c09 107 np = of_find_node_opts_by_path("/testcase-data:testoption", NULL);
9697a559 108 unittest(np, "NULL option path test failed\n");
75c28c09
LL
109 of_node_put(np);
110
111 np = of_find_node_opts_by_path("testcase-alias:testaliasoption",
112 &options);
9697a559 113 unittest(np && !strcmp("testaliasoption", options),
75c28c09
LL
114 "option alias path test failed\n");
115 of_node_put(np);
116
8cbba1ab
PH
117 np = of_find_node_opts_by_path("testcase-alias:test/alias/option",
118 &options);
9697a559 119 unittest(np && !strcmp("test/alias/option", options),
8cbba1ab
PH
120 "option alias path test, subcase #1 failed\n");
121 of_node_put(np);
122
75c28c09 123 np = of_find_node_opts_by_path("testcase-alias:testaliasoption", NULL);
9697a559 124 unittest(np, "NULL option alias path test failed\n");
75c28c09
LL
125 of_node_put(np);
126
127 options = "testoption";
128 np = of_find_node_opts_by_path("testcase-alias", &options);
9697a559 129 unittest(np && !options, "option clearing test failed\n");
75c28c09
LL
130 of_node_put(np);
131
132 options = "testoption";
133 np = of_find_node_opts_by_path("/", &options);
9697a559 134 unittest(np && !options, "option clearing root node test failed\n");
75c28c09 135 of_node_put(np);
ae91ff72
GL
136}
137
9697a559 138static void __init of_unittest_dynamic(void)
7e66c5c7
GL
139{
140 struct device_node *np;
141 struct property *prop;
142
143 np = of_find_node_by_path("/testcase-data");
144 if (!np) {
145 pr_err("missing testcase data\n");
146 return;
147 }
148
149 /* Array of 4 properties for the purpose of testing */
150 prop = kzalloc(sizeof(*prop) * 4, GFP_KERNEL);
151 if (!prop) {
9697a559 152 unittest(0, "kzalloc() failed\n");
7e66c5c7
GL
153 return;
154 }
155
156 /* Add a new property - should pass*/
157 prop->name = "new-property";
158 prop->value = "new-property-data";
159 prop->length = strlen(prop->value);
9697a559 160 unittest(of_add_property(np, prop) == 0, "Adding a new property failed\n");
7e66c5c7
GL
161
162 /* Try to add an existing property - should fail */
163 prop++;
164 prop->name = "new-property";
165 prop->value = "new-property-data-should-fail";
166 prop->length = strlen(prop->value);
9697a559 167 unittest(of_add_property(np, prop) != 0,
7e66c5c7
GL
168 "Adding an existing property should have failed\n");
169
170 /* Try to modify an existing property - should pass */
171 prop->value = "modify-property-data-should-pass";
172 prop->length = strlen(prop->value);
9697a559 173 unittest(of_update_property(np, prop) == 0,
7e66c5c7
GL
174 "Updating an existing property should have passed\n");
175
176 /* Try to modify non-existent property - should pass*/
177 prop++;
178 prop->name = "modify-property";
179 prop->value = "modify-missing-property-data-should-pass";
180 prop->length = strlen(prop->value);
9697a559 181 unittest(of_update_property(np, prop) == 0,
7e66c5c7
GL
182 "Updating a missing property should have passed\n");
183
184 /* Remove property - should pass */
9697a559 185 unittest(of_remove_property(np, prop) == 0,
7e66c5c7
GL
186 "Removing a property should have passed\n");
187
188 /* Adding very large property - should pass */
189 prop++;
190 prop->name = "large-property-PAGE_SIZEx8";
191 prop->length = PAGE_SIZE * 8;
192 prop->value = kzalloc(prop->length, GFP_KERNEL);
9697a559 193 unittest(prop->value != NULL, "Unable to allocate large buffer\n");
7e66c5c7 194 if (prop->value)
9697a559 195 unittest(of_add_property(np, prop) == 0,
7e66c5c7
GL
196 "Adding a large property should have passed\n");
197}
198
9697a559 199static int __init of_unittest_check_node_linkage(struct device_node *np)
f2051d6a 200{
5063e25a 201 struct device_node *child;
f2051d6a
GL
202 int count = 0, rc;
203
204 for_each_child_of_node(np, child) {
205 if (child->parent != np) {
206 pr_err("Child node %s links to wrong parent %s\n",
207 child->name, np->name);
208 return -EINVAL;
209 }
210
9697a559 211 rc = of_unittest_check_node_linkage(child);
f2051d6a
GL
212 if (rc < 0)
213 return rc;
214 count += rc;
215 }
216
217 return count + 1;
218}
219
9697a559 220static void __init of_unittest_check_tree_linkage(void)
f2051d6a
GL
221{
222 struct device_node *np;
223 int allnode_count = 0, child_count;
224
5063e25a 225 if (!of_root)
f2051d6a
GL
226 return;
227
228 for_each_of_allnodes(np)
229 allnode_count++;
9697a559 230 child_count = of_unittest_check_node_linkage(of_root);
f2051d6a 231
9697a559
WL
232 unittest(child_count > 0, "Device node data structure is corrupted\n");
233 unittest(child_count == allnode_count, "allnodes list size (%i) doesn't match"
f2051d6a
GL
234 "sibling lists size (%i)\n", allnode_count, child_count);
235 pr_debug("allnodes list size (%i); sibling lists size (%i)\n", allnode_count, child_count);
236}
237
841ec213
GL
238struct node_hash {
239 struct hlist_node node;
240 struct device_node *np;
241};
242
2118f4b8 243static DEFINE_HASHTABLE(phandle_ht, 8);
9697a559 244static void __init of_unittest_check_phandles(void)
841ec213
GL
245{
246 struct device_node *np;
247 struct node_hash *nh;
248 struct hlist_node *tmp;
249 int i, dup_count = 0, phandle_count = 0;
841ec213 250
841ec213
GL
251 for_each_of_allnodes(np) {
252 if (!np->phandle)
253 continue;
254
2118f4b8 255 hash_for_each_possible(phandle_ht, nh, node, np->phandle) {
841ec213
GL
256 if (nh->np->phandle == np->phandle) {
257 pr_info("Duplicate phandle! %i used by %s and %s\n",
258 np->phandle, nh->np->full_name, np->full_name);
259 dup_count++;
260 break;
261 }
262 }
263
264 nh = kzalloc(sizeof(*nh), GFP_KERNEL);
265 if (WARN_ON(!nh))
266 return;
267
268 nh->np = np;
2118f4b8 269 hash_add(phandle_ht, &nh->node, np->phandle);
841ec213
GL
270 phandle_count++;
271 }
9697a559 272 unittest(dup_count == 0, "Found %i duplicates in %i phandles\n",
841ec213
GL
273 dup_count, phandle_count);
274
275 /* Clean up */
2118f4b8 276 hash_for_each_safe(phandle_ht, i, tmp, nh, node) {
841ec213
GL
277 hash_del(&nh->node);
278 kfree(nh);
279 }
280}
281
9697a559 282static void __init of_unittest_parse_phandle_with_args(void)
53a42093
GL
283{
284 struct device_node *np;
285 struct of_phandle_args args;
cabb7d5b 286 int i, rc;
53a42093 287
53a42093
GL
288 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
289 if (!np) {
290 pr_err("missing testcase data\n");
291 return;
292 }
293
bd69f73f 294 rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
9697a559 295 unittest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc);
bd69f73f 296
f7f951c2 297 for (i = 0; i < 8; i++) {
53a42093
GL
298 bool passed = true;
299 rc = of_parse_phandle_with_args(np, "phandle-list",
300 "#phandle-cells", i, &args);
301
302 /* Test the values from tests-phandle.dtsi */
303 switch (i) {
304 case 0:
305 passed &= !rc;
306 passed &= (args.args_count == 1);
307 passed &= (args.args[0] == (i + 1));
308 break;
309 case 1:
310 passed &= !rc;
311 passed &= (args.args_count == 2);
312 passed &= (args.args[0] == (i + 1));
313 passed &= (args.args[1] == 0);
314 break;
315 case 2:
316 passed &= (rc == -ENOENT);
317 break;
318 case 3:
319 passed &= !rc;
320 passed &= (args.args_count == 3);
321 passed &= (args.args[0] == (i + 1));
322 passed &= (args.args[1] == 4);
323 passed &= (args.args[2] == 3);
324 break;
325 case 4:
326 passed &= !rc;
327 passed &= (args.args_count == 2);
328 passed &= (args.args[0] == (i + 1));
329 passed &= (args.args[1] == 100);
330 break;
331 case 5:
332 passed &= !rc;
333 passed &= (args.args_count == 0);
334 break;
335 case 6:
336 passed &= !rc;
337 passed &= (args.args_count == 1);
338 passed &= (args.args[0] == (i + 1));
339 break;
340 case 7:
cabb7d5b 341 passed &= (rc == -ENOENT);
53a42093
GL
342 break;
343 default:
344 passed = false;
345 }
346
9697a559 347 unittest(passed, "index %i - data error on node %s rc=%i\n",
cabb7d5b 348 i, args.np->full_name, rc);
53a42093
GL
349 }
350
351 /* Check for missing list property */
352 rc = of_parse_phandle_with_args(np, "phandle-list-missing",
353 "#phandle-cells", 0, &args);
9697a559 354 unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
bd69f73f
GL
355 rc = of_count_phandle_with_args(np, "phandle-list-missing",
356 "#phandle-cells");
9697a559 357 unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
53a42093
GL
358
359 /* Check for missing cells property */
360 rc = of_parse_phandle_with_args(np, "phandle-list",
361 "#phandle-cells-missing", 0, &args);
9697a559 362 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
bd69f73f
GL
363 rc = of_count_phandle_with_args(np, "phandle-list",
364 "#phandle-cells-missing");
9697a559 365 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
53a42093
GL
366
367 /* Check for bad phandle in list */
368 rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle",
369 "#phandle-cells", 0, &args);
9697a559 370 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
bd69f73f
GL
371 rc = of_count_phandle_with_args(np, "phandle-list-bad-phandle",
372 "#phandle-cells");
9697a559 373 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
53a42093
GL
374
375 /* Check for incorrectly formed argument list */
376 rc = of_parse_phandle_with_args(np, "phandle-list-bad-args",
377 "#phandle-cells", 1, &args);
9697a559 378 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
bd69f73f
GL
379 rc = of_count_phandle_with_args(np, "phandle-list-bad-args",
380 "#phandle-cells");
9697a559 381 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
53a42093
GL
382}
383
9697a559 384static void __init of_unittest_property_string(void)
7aff0fe3 385{
a87fa1d8 386 const char *strings[4];
7aff0fe3
GL
387 struct device_node *np;
388 int rc;
389
7aff0fe3
GL
390 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
391 if (!np) {
392 pr_err("No testcase data in device tree\n");
393 return;
394 }
395
396 rc = of_property_match_string(np, "phandle-list-names", "first");
9697a559 397 unittest(rc == 0, "first expected:0 got:%i\n", rc);
7aff0fe3 398 rc = of_property_match_string(np, "phandle-list-names", "second");
9697a559 399 unittest(rc == 1, "second expected:1 got:%i\n", rc);
7aff0fe3 400 rc = of_property_match_string(np, "phandle-list-names", "third");
9697a559 401 unittest(rc == 2, "third expected:2 got:%i\n", rc);
7aff0fe3 402 rc = of_property_match_string(np, "phandle-list-names", "fourth");
9697a559 403 unittest(rc == -ENODATA, "unmatched string; rc=%i\n", rc);
7aff0fe3 404 rc = of_property_match_string(np, "missing-property", "blah");
9697a559 405 unittest(rc == -EINVAL, "missing property; rc=%i\n", rc);
7aff0fe3 406 rc = of_property_match_string(np, "empty-property", "blah");
9697a559 407 unittest(rc == -ENODATA, "empty property; rc=%i\n", rc);
7aff0fe3 408 rc = of_property_match_string(np, "unterminated-string", "blah");
9697a559 409 unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
a87fa1d8
GL
410
411 /* of_property_count_strings() tests */
412 rc = of_property_count_strings(np, "string-property");
9697a559 413 unittest(rc == 1, "Incorrect string count; rc=%i\n", rc);
a87fa1d8 414 rc = of_property_count_strings(np, "phandle-list-names");
9697a559 415 unittest(rc == 3, "Incorrect string count; rc=%i\n", rc);
a87fa1d8 416 rc = of_property_count_strings(np, "unterminated-string");
9697a559 417 unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
a87fa1d8 418 rc = of_property_count_strings(np, "unterminated-string-list");
9697a559 419 unittest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
a87fa1d8
GL
420
421 /* of_property_read_string_index() tests */
422 rc = of_property_read_string_index(np, "string-property", 0, strings);
9697a559 423 unittest(rc == 0 && !strcmp(strings[0], "foobar"), "of_property_read_string_index() failure; rc=%i\n", rc);
a87fa1d8
GL
424 strings[0] = NULL;
425 rc = of_property_read_string_index(np, "string-property", 1, strings);
9697a559 426 unittest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
a87fa1d8 427 rc = of_property_read_string_index(np, "phandle-list-names", 0, strings);
9697a559 428 unittest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
a87fa1d8 429 rc = of_property_read_string_index(np, "phandle-list-names", 1, strings);
9697a559 430 unittest(rc == 0 && !strcmp(strings[0], "second"), "of_property_read_string_index() failure; rc=%i\n", rc);
a87fa1d8 431 rc = of_property_read_string_index(np, "phandle-list-names", 2, strings);
9697a559 432 unittest(rc == 0 && !strcmp(strings[0], "third"), "of_property_read_string_index() failure; rc=%i\n", rc);
a87fa1d8
GL
433 strings[0] = NULL;
434 rc = of_property_read_string_index(np, "phandle-list-names", 3, strings);
9697a559 435 unittest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
a87fa1d8
GL
436 strings[0] = NULL;
437 rc = of_property_read_string_index(np, "unterminated-string", 0, strings);
9697a559 438 unittest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
a87fa1d8 439 rc = of_property_read_string_index(np, "unterminated-string-list", 0, strings);
9697a559 440 unittest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
a87fa1d8
GL
441 strings[0] = NULL;
442 rc = of_property_read_string_index(np, "unterminated-string-list", 2, strings); /* should fail */
9697a559 443 unittest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
a87fa1d8
GL
444 strings[1] = NULL;
445
446 /* of_property_read_string_array() tests */
447 rc = of_property_read_string_array(np, "string-property", strings, 4);
9697a559 448 unittest(rc == 1, "Incorrect string count; rc=%i\n", rc);
a87fa1d8 449 rc = of_property_read_string_array(np, "phandle-list-names", strings, 4);
9697a559 450 unittest(rc == 3, "Incorrect string count; rc=%i\n", rc);
a87fa1d8 451 rc = of_property_read_string_array(np, "unterminated-string", strings, 4);
9697a559 452 unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
a87fa1d8
GL
453 /* -- An incorrectly formed string should cause a failure */
454 rc = of_property_read_string_array(np, "unterminated-string-list", strings, 4);
9697a559 455 unittest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
a87fa1d8
GL
456 /* -- parsing the correctly formed strings should still work: */
457 strings[2] = NULL;
458 rc = of_property_read_string_array(np, "unterminated-string-list", strings, 2);
9697a559 459 unittest(rc == 2 && strings[2] == NULL, "of_property_read_string_array() failure; rc=%i\n", rc);
a87fa1d8
GL
460 strings[1] = NULL;
461 rc = of_property_read_string_array(np, "phandle-list-names", strings, 1);
9697a559 462 unittest(rc == 1 && strings[1] == NULL, "Overwrote end of string array; rc=%i, str='%s'\n", rc, strings[1]);
7aff0fe3
GL
463}
464
69843396
PA
465#define propcmp(p1, p2) (((p1)->length == (p2)->length) && \
466 (p1)->value && (p2)->value && \
467 !memcmp((p1)->value, (p2)->value, (p1)->length) && \
468 !strcmp((p1)->name, (p2)->name))
9697a559 469static void __init of_unittest_property_copy(void)
69843396
PA
470{
471#ifdef CONFIG_OF_DYNAMIC
472 struct property p1 = { .name = "p1", .length = 0, .value = "" };
473 struct property p2 = { .name = "p2", .length = 5, .value = "abcd" };
474 struct property *new;
475
476 new = __of_prop_dup(&p1, GFP_KERNEL);
9697a559 477 unittest(new && propcmp(&p1, new), "empty property didn't copy correctly\n");
69843396
PA
478 kfree(new->value);
479 kfree(new->name);
480 kfree(new);
481
482 new = __of_prop_dup(&p2, GFP_KERNEL);
9697a559 483 unittest(new && propcmp(&p2, new), "non-empty property didn't copy correctly\n");
69843396
PA
484 kfree(new->value);
485 kfree(new->name);
486 kfree(new);
487#endif
488}
489
9697a559 490static void __init of_unittest_changeset(void)
201c910b
PA
491{
492#ifdef CONFIG_OF_DYNAMIC
493 struct property *ppadd, padd = { .name = "prop-add", .length = 0, .value = "" };
494 struct property *ppupdate, pupdate = { .name = "prop-update", .length = 5, .value = "abcd" };
495 struct property *ppremove;
e5179581 496 struct device_node *n1, *n2, *n21, *nremove, *parent, *np;
201c910b
PA
497 struct of_changeset chgset;
498
e5179581 499 n1 = __of_node_dup(NULL, "/testcase-data/changeset/n1");
9697a559 500 unittest(n1, "testcase setup failure\n");
e5179581 501 n2 = __of_node_dup(NULL, "/testcase-data/changeset/n2");
9697a559 502 unittest(n2, "testcase setup failure\n");
e5179581 503 n21 = __of_node_dup(NULL, "%s/%s", "/testcase-data/changeset/n2", "n21");
9697a559 504 unittest(n21, "testcase setup failure %p\n", n21);
201c910b 505 nremove = of_find_node_by_path("/testcase-data/changeset/node-remove");
9697a559 506 unittest(nremove, "testcase setup failure\n");
201c910b 507 ppadd = __of_prop_dup(&padd, GFP_KERNEL);
9697a559 508 unittest(ppadd, "testcase setup failure\n");
201c910b 509 ppupdate = __of_prop_dup(&pupdate, GFP_KERNEL);
9697a559 510 unittest(ppupdate, "testcase setup failure\n");
201c910b
PA
511 parent = nremove->parent;
512 n1->parent = parent;
513 n2->parent = parent;
514 n21->parent = n2;
515 n2->child = n21;
516 ppremove = of_find_property(parent, "prop-remove", NULL);
9697a559 517 unittest(ppremove, "failed to find removal prop");
201c910b
PA
518
519 of_changeset_init(&chgset);
9697a559
WL
520 unittest(!of_changeset_attach_node(&chgset, n1), "fail attach n1\n");
521 unittest(!of_changeset_attach_node(&chgset, n2), "fail attach n2\n");
522 unittest(!of_changeset_detach_node(&chgset, nremove), "fail remove node\n");
523 unittest(!of_changeset_attach_node(&chgset, n21), "fail attach n21\n");
524 unittest(!of_changeset_add_property(&chgset, parent, ppadd), "fail add prop\n");
525 unittest(!of_changeset_update_property(&chgset, parent, ppupdate), "fail update prop\n");
526 unittest(!of_changeset_remove_property(&chgset, parent, ppremove), "fail remove prop\n");
201c910b 527 mutex_lock(&of_mutex);
9697a559 528 unittest(!of_changeset_apply(&chgset), "apply failed\n");
201c910b
PA
529 mutex_unlock(&of_mutex);
530
e5179581 531 /* Make sure node names are constructed correctly */
9697a559 532 unittest((np = of_find_node_by_path("/testcase-data/changeset/n2/n21")),
e5179581 533 "'%s' not added\n", n21->full_name);
c46ca3c8 534 of_node_put(np);
e5179581 535
201c910b 536 mutex_lock(&of_mutex);
9697a559 537 unittest(!of_changeset_revert(&chgset), "revert failed\n");
201c910b
PA
538 mutex_unlock(&of_mutex);
539
540 of_changeset_destroy(&chgset);
541#endif
542}
543
9697a559 544static void __init of_unittest_parse_interrupts(void)
a9f10ca7
GL
545{
546 struct device_node *np;
547 struct of_phandle_args args;
548 int i, rc;
549
550 np = of_find_node_by_path("/testcase-data/interrupts/interrupts0");
551 if (!np) {
552 pr_err("missing testcase data\n");
553 return;
554 }
555
556 for (i = 0; i < 4; i++) {
557 bool passed = true;
558 args.args_count = 0;
559 rc = of_irq_parse_one(np, i, &args);
560
561 passed &= !rc;
562 passed &= (args.args_count == 1);
563 passed &= (args.args[0] == (i + 1));
564
9697a559 565 unittest(passed, "index %i - data error on node %s rc=%i\n",
a9f10ca7
GL
566 i, args.np->full_name, rc);
567 }
568 of_node_put(np);
569
570 np = of_find_node_by_path("/testcase-data/interrupts/interrupts1");
571 if (!np) {
572 pr_err("missing testcase data\n");
573 return;
574 }
575
576 for (i = 0; i < 4; i++) {
577 bool passed = true;
578 args.args_count = 0;
579 rc = of_irq_parse_one(np, i, &args);
580
581 /* Test the values from tests-phandle.dtsi */
582 switch (i) {
583 case 0:
584 passed &= !rc;
585 passed &= (args.args_count == 1);
586 passed &= (args.args[0] == 9);
587 break;
588 case 1:
589 passed &= !rc;
590 passed &= (args.args_count == 3);
591 passed &= (args.args[0] == 10);
592 passed &= (args.args[1] == 11);
593 passed &= (args.args[2] == 12);
594 break;
595 case 2:
596 passed &= !rc;
597 passed &= (args.args_count == 2);
598 passed &= (args.args[0] == 13);
599 passed &= (args.args[1] == 14);
600 break;
601 case 3:
602 passed &= !rc;
603 passed &= (args.args_count == 2);
604 passed &= (args.args[0] == 15);
605 passed &= (args.args[1] == 16);
606 break;
607 default:
608 passed = false;
609 }
9697a559 610 unittest(passed, "index %i - data error on node %s rc=%i\n",
a9f10ca7
GL
611 i, args.np->full_name, rc);
612 }
613 of_node_put(np);
614}
615
9697a559 616static void __init of_unittest_parse_interrupts_extended(void)
79d97015
GL
617{
618 struct device_node *np;
619 struct of_phandle_args args;
620 int i, rc;
621
622 np = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
623 if (!np) {
624 pr_err("missing testcase data\n");
625 return;
626 }
627
628 for (i = 0; i < 7; i++) {
629 bool passed = true;
630 rc = of_irq_parse_one(np, i, &args);
631
632 /* Test the values from tests-phandle.dtsi */
633 switch (i) {
634 case 0:
635 passed &= !rc;
636 passed &= (args.args_count == 1);
637 passed &= (args.args[0] == 1);
638 break;
639 case 1:
640 passed &= !rc;
641 passed &= (args.args_count == 3);
642 passed &= (args.args[0] == 2);
643 passed &= (args.args[1] == 3);
644 passed &= (args.args[2] == 4);
645 break;
646 case 2:
647 passed &= !rc;
648 passed &= (args.args_count == 2);
649 passed &= (args.args[0] == 5);
650 passed &= (args.args[1] == 6);
651 break;
652 case 3:
653 passed &= !rc;
654 passed &= (args.args_count == 1);
655 passed &= (args.args[0] == 9);
656 break;
657 case 4:
658 passed &= !rc;
659 passed &= (args.args_count == 3);
660 passed &= (args.args[0] == 10);
661 passed &= (args.args[1] == 11);
662 passed &= (args.args[2] == 12);
663 break;
664 case 5:
665 passed &= !rc;
666 passed &= (args.args_count == 2);
667 passed &= (args.args[0] == 13);
668 passed &= (args.args[1] == 14);
669 break;
670 case 6:
671 passed &= !rc;
672 passed &= (args.args_count == 1);
673 passed &= (args.args[0] == 15);
674 break;
675 default:
676 passed = false;
677 }
678
9697a559 679 unittest(passed, "index %i - data error on node %s rc=%i\n",
79d97015
GL
680 i, args.np->full_name, rc);
681 }
682 of_node_put(np);
683}
684
1f42e5dd
GL
685static struct of_device_id match_node_table[] = {
686 { .data = "A", .name = "name0", }, /* Name alone is lowest priority */
687 { .data = "B", .type = "type1", }, /* followed by type alone */
688
689 { .data = "Ca", .name = "name2", .type = "type1", }, /* followed by both together */
690 { .data = "Cb", .name = "name2", }, /* Only match when type doesn't match */
691 { .data = "Cc", .name = "name2", .type = "type2", },
692
693 { .data = "E", .compatible = "compat3" },
694 { .data = "G", .compatible = "compat2", },
695 { .data = "H", .compatible = "compat2", .name = "name5", },
696 { .data = "I", .compatible = "compat2", .type = "type1", },
697 { .data = "J", .compatible = "compat2", .type = "type1", .name = "name8", },
698 { .data = "K", .compatible = "compat2", .name = "name9", },
699 {}
700};
701
702static struct {
703 const char *path;
704 const char *data;
705} match_node_tests[] = {
706 { .path = "/testcase-data/match-node/name0", .data = "A", },
707 { .path = "/testcase-data/match-node/name1", .data = "B", },
708 { .path = "/testcase-data/match-node/a/name2", .data = "Ca", },
709 { .path = "/testcase-data/match-node/b/name2", .data = "Cb", },
710 { .path = "/testcase-data/match-node/c/name2", .data = "Cc", },
711 { .path = "/testcase-data/match-node/name3", .data = "E", },
712 { .path = "/testcase-data/match-node/name4", .data = "G", },
713 { .path = "/testcase-data/match-node/name5", .data = "H", },
714 { .path = "/testcase-data/match-node/name6", .data = "G", },
715 { .path = "/testcase-data/match-node/name7", .data = "I", },
716 { .path = "/testcase-data/match-node/name8", .data = "J", },
717 { .path = "/testcase-data/match-node/name9", .data = "K", },
718};
719
9697a559 720static void __init of_unittest_match_node(void)
1f42e5dd
GL
721{
722 struct device_node *np;
723 const struct of_device_id *match;
724 int i;
725
726 for (i = 0; i < ARRAY_SIZE(match_node_tests); i++) {
727 np = of_find_node_by_path(match_node_tests[i].path);
728 if (!np) {
9697a559 729 unittest(0, "missing testcase node %s\n",
1f42e5dd
GL
730 match_node_tests[i].path);
731 continue;
732 }
733
734 match = of_match_node(match_node_table, np);
735 if (!match) {
9697a559 736 unittest(0, "%s didn't match anything\n",
1f42e5dd
GL
737 match_node_tests[i].path);
738 continue;
739 }
740
741 if (strcmp(match->data, match_node_tests[i].data) != 0) {
9697a559 742 unittest(0, "%s got wrong match. expected %s, got %s\n",
1f42e5dd
GL
743 match_node_tests[i].path, match_node_tests[i].data,
744 (const char *)match->data);
745 continue;
746 }
9697a559 747 unittest(1, "passed");
1f42e5dd
GL
748 }
749}
750
851da976
GL
751struct device test_bus = {
752 .init_name = "unittest-bus",
753};
9697a559 754static void __init of_unittest_platform_populate(void)
82c0f589 755{
851da976
GL
756 int irq, rc;
757 struct device_node *np, *child, *grandchild;
82c0f589 758 struct platform_device *pdev;
fb2caa50
RH
759 struct of_device_id match[] = {
760 { .compatible = "test-device", },
761 {}
762 };
82c0f589
RH
763
764 np = of_find_node_by_path("/testcase-data");
765 of_platform_populate(np, of_default_bus_match_table, NULL, NULL);
766
767 /* Test that a missing irq domain returns -EPROBE_DEFER */
768 np = of_find_node_by_path("/testcase-data/testcase-device1");
769 pdev = of_find_device_by_node(np);
9697a559 770 unittest(pdev, "device 1 creation failed\n");
7d1cdc89 771
82c0f589 772 irq = platform_get_irq(pdev, 0);
9697a559 773 unittest(irq == -EPROBE_DEFER, "device deferred probe failed - %d\n", irq);
82c0f589
RH
774
775 /* Test that a parsing failure does not return -EPROBE_DEFER */
776 np = of_find_node_by_path("/testcase-data/testcase-device2");
777 pdev = of_find_device_by_node(np);
9697a559 778 unittest(pdev, "device 2 creation failed\n");
82c0f589 779 irq = platform_get_irq(pdev, 0);
9697a559 780 unittest(irq < 0 && irq != -EPROBE_DEFER, "device parsing error failed - %d\n", irq);
82c0f589 781
9697a559 782 if (unittest(np = of_find_node_by_path("/testcase-data/platform-tests"),
851da976
GL
783 "No testcase data in device tree\n"));
784 return;
785
9697a559 786 if (unittest(!(rc = device_register(&test_bus)),
851da976 787 "testbus registration failed; rc=%i\n", rc));
fb2caa50 788 return;
fb2caa50
RH
789
790 for_each_child_of_node(np, child) {
851da976 791 of_platform_populate(child, match, NULL, &test_bus);
fb2caa50 792 for_each_child_of_node(child, grandchild)
9697a559 793 unittest(of_find_device_by_node(grandchild),
fb2caa50
RH
794 "Could not create device for node '%s'\n",
795 grandchild->name);
796 }
851da976
GL
797
798 of_platform_depopulate(&test_bus);
799 for_each_child_of_node(np, child) {
800 for_each_child_of_node(child, grandchild)
9697a559 801 unittest(!of_find_device_by_node(grandchild),
851da976
GL
802 "device didn't get destroyed '%s'\n",
803 grandchild->name);
804 }
805
806 device_unregister(&test_bus);
807 of_node_put(np);
82c0f589
RH
808}
809
ae9304c9
GM
810/**
811 * update_node_properties - adds the properties
812 * of np into dup node (present in live tree) and
813 * updates parent of children of np to dup.
814 *
815 * @np: node already present in live tree
816 * @dup: node present in live tree to be updated
817 */
818static void update_node_properties(struct device_node *np,
819 struct device_node *dup)
820{
821 struct property *prop;
822 struct device_node *child;
823
824 for_each_property_of_node(np, prop)
825 of_add_property(dup, prop);
826
827 for_each_child_of_node(np, child)
828 child->parent = dup;
829}
830
831/**
832 * attach_node_and_children - attaches nodes
833 * and its children to live tree
834 *
835 * @np: Node to attach to live tree
836 */
837static int attach_node_and_children(struct device_node *np)
838{
5063e25a 839 struct device_node *next, *dup, *child;
3ce04b4a 840 unsigned long flags;
ae9304c9 841
5063e25a
GL
842 dup = of_find_node_by_path(np->full_name);
843 if (dup) {
844 update_node_properties(np, dup);
845 return 0;
846 }
ae9304c9 847
5063e25a
GL
848 child = np->child;
849 np->child = NULL;
3ce04b4a
GM
850
851 mutex_lock(&of_mutex);
852 raw_spin_lock_irqsave(&devtree_lock, flags);
853 np->sibling = np->parent->child;
854 np->parent->child = np;
855 of_node_clear_flag(np, OF_DETACHED);
856 raw_spin_unlock_irqrestore(&devtree_lock, flags);
857
858 __of_attach_node_sysfs(np);
859 mutex_unlock(&of_mutex);
860
5063e25a
GL
861 while (child) {
862 next = child->sibling;
863 attach_node_and_children(child);
864 child = next;
ae9304c9
GM
865 }
866
867 return 0;
868}
869
870/**
9697a559 871 * unittest_data_add - Reads, copies data from
ae9304c9
GM
872 * linked tree and attaches it to the live tree
873 */
9697a559 874static int __init unittest_data_add(void)
ae9304c9 875{
9697a559
WL
876 void *unittest_data;
877 struct device_node *unittest_data_node, *np;
ae9304c9
GM
878 extern uint8_t __dtb_testcases_begin[];
879 extern uint8_t __dtb_testcases_end[];
880 const int size = __dtb_testcases_end - __dtb_testcases_begin;
2eb46da2 881 int rc;
ae9304c9 882
b951f9dc 883 if (!size) {
ae9304c9
GM
884 pr_warn("%s: No testcase data to attach; not running tests\n",
885 __func__);
886 return -ENODATA;
887 }
888
889 /* creating copy */
9697a559 890 unittest_data = kmemdup(__dtb_testcases_begin, size, GFP_KERNEL);
ae9304c9 891
9697a559
WL
892 if (!unittest_data) {
893 pr_warn("%s: Failed to allocate memory for unittest_data; "
ae9304c9
GM
894 "not running tests\n", __func__);
895 return -ENOMEM;
896 }
9697a559
WL
897 of_fdt_unflatten_tree(unittest_data, &unittest_data_node);
898 if (!unittest_data_node) {
b951f9dc
GM
899 pr_warn("%s: No tree to attach; not running tests\n", __func__);
900 return -ENODATA;
901 }
9697a559
WL
902 of_node_set_flag(unittest_data_node, OF_DETACHED);
903 rc = of_resolve_phandles(unittest_data_node);
2eb46da2
GL
904 if (rc) {
905 pr_err("%s: Failed to resolve phandles (rc=%i)\n", __func__, rc);
906 return -EINVAL;
907 }
b951f9dc 908
5063e25a 909 if (!of_root) {
9697a559 910 of_root = unittest_data_node;
b951f9dc
GM
911 for_each_of_allnodes(np)
912 __of_attach_node_sysfs(np);
913 of_aliases = of_find_node_by_path("/aliases");
914 of_chosen = of_find_node_by_path("/chosen");
915 return 0;
916 }
ae9304c9
GM
917
918 /* attach the sub-tree to live tree */
9697a559 919 np = unittest_data_node->child;
5063e25a
GL
920 while (np) {
921 struct device_node *next = np->sibling;
922 np->parent = of_root;
923 attach_node_and_children(np);
924 np = next;
925 }
926 return 0;
ae9304c9
GM
927}
928
177d271c
PA
929#ifdef CONFIG_OF_OVERLAY
930
9697a559 931static int unittest_probe(struct platform_device *pdev)
177d271c
PA
932{
933 struct device *dev = &pdev->dev;
934 struct device_node *np = dev->of_node;
935
936 if (np == NULL) {
937 dev_err(dev, "No OF data for device\n");
938 return -EINVAL;
939
940 }
941
942 dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
6b1271de
PA
943
944 of_platform_populate(np, NULL, NULL, &pdev->dev);
945
177d271c
PA
946 return 0;
947}
948
9697a559 949static int unittest_remove(struct platform_device *pdev)
177d271c
PA
950{
951 struct device *dev = &pdev->dev;
952 struct device_node *np = dev->of_node;
953
954 dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
955 return 0;
956}
957
9697a559
WL
958static struct of_device_id unittest_match[] = {
959 { .compatible = "unittest", },
177d271c
PA
960 {},
961};
177d271c 962
9697a559
WL
963static struct platform_driver unittest_driver = {
964 .probe = unittest_probe,
965 .remove = unittest_remove,
177d271c 966 .driver = {
9697a559 967 .name = "unittest",
177d271c 968 .owner = THIS_MODULE,
9697a559 969 .of_match_table = of_match_ptr(unittest_match),
177d271c
PA
970 },
971};
972
973/* get the platform device instantiated at the path */
974static struct platform_device *of_path_to_platform_device(const char *path)
975{
976 struct device_node *np;
977 struct platform_device *pdev;
978
979 np = of_find_node_by_path(path);
980 if (np == NULL)
981 return NULL;
982
983 pdev = of_find_device_by_node(np);
984 of_node_put(np);
985
986 return pdev;
987}
988
989/* find out if a platform device exists at that path */
990static int of_path_platform_device_exists(const char *path)
991{
992 struct platform_device *pdev;
993
994 pdev = of_path_to_platform_device(path);
995 platform_device_put(pdev);
996 return pdev != NULL;
997}
998
4252de39 999#if IS_BUILTIN(CONFIG_I2C)
d5e75500
PA
1000
1001/* get the i2c client device instantiated at the path */
1002static struct i2c_client *of_path_to_i2c_client(const char *path)
1003{
1004 struct device_node *np;
1005 struct i2c_client *client;
1006
1007 np = of_find_node_by_path(path);
1008 if (np == NULL)
1009 return NULL;
1010
1011 client = of_find_i2c_device_by_node(np);
1012 of_node_put(np);
1013
1014 return client;
1015}
1016
1017/* find out if a i2c client device exists at that path */
1018static int of_path_i2c_client_exists(const char *path)
1019{
1020 struct i2c_client *client;
1021
1022 client = of_path_to_i2c_client(path);
1023 if (client)
1024 put_device(&client->dev);
1025 return client != NULL;
1026}
1027#else
1028static int of_path_i2c_client_exists(const char *path)
1029{
1030 return 0;
1031}
1032#endif
1033
1034enum overlay_type {
1035 PDEV_OVERLAY,
1036 I2C_OVERLAY
1037};
1038
1039static int of_path_device_type_exists(const char *path,
1040 enum overlay_type ovtype)
177d271c 1041{
d5e75500
PA
1042 switch (ovtype) {
1043 case PDEV_OVERLAY:
1044 return of_path_platform_device_exists(path);
1045 case I2C_OVERLAY:
1046 return of_path_i2c_client_exists(path);
1047 }
1048 return 0;
1049}
1050
9697a559 1051static const char *unittest_path(int nr, enum overlay_type ovtype)
d5e75500
PA
1052{
1053 const char *base;
177d271c
PA
1054 static char buf[256];
1055
d5e75500
PA
1056 switch (ovtype) {
1057 case PDEV_OVERLAY:
1058 base = "/testcase-data/overlay-node/test-bus";
1059 break;
1060 case I2C_OVERLAY:
1061 base = "/testcase-data/overlay-node/test-bus/i2c-test-bus";
1062 break;
1063 default:
1064 buf[0] = '\0';
1065 return buf;
1066 }
9697a559 1067 snprintf(buf, sizeof(buf) - 1, "%s/test-unittest%d", base, nr);
177d271c 1068 buf[sizeof(buf) - 1] = '\0';
177d271c
PA
1069 return buf;
1070}
1071
9697a559 1072static int of_unittest_device_exists(int unittest_nr, enum overlay_type ovtype)
d5e75500
PA
1073{
1074 const char *path;
1075
9697a559 1076 path = unittest_path(unittest_nr, ovtype);
d5e75500
PA
1077
1078 switch (ovtype) {
1079 case PDEV_OVERLAY:
1080 return of_path_platform_device_exists(path);
1081 case I2C_OVERLAY:
1082 return of_path_i2c_client_exists(path);
1083 }
1084 return 0;
1085}
1086
177d271c
PA
1087static const char *overlay_path(int nr)
1088{
1089 static char buf[256];
1090
1091 snprintf(buf, sizeof(buf) - 1,
1092 "/testcase-data/overlay%d", nr);
1093 buf[sizeof(buf) - 1] = '\0';
1094
1095 return buf;
1096}
1097
1098static const char *bus_path = "/testcase-data/overlay-node/test-bus";
1099
492a22ac
PA
1100/* it is guaranteed that overlay ids are assigned in sequence */
1101#define MAX_UNITTEST_OVERLAYS 256
1102static unsigned long overlay_id_bits[BITS_TO_LONGS(MAX_UNITTEST_OVERLAYS)];
1103static int overlay_first_id = -1;
1104
1105static void of_unittest_track_overlay(int id)
1106{
1107 if (overlay_first_id < 0)
1108 overlay_first_id = id;
1109 id -= overlay_first_id;
1110
1111 /* we shouldn't need that many */
1112 BUG_ON(id >= MAX_UNITTEST_OVERLAYS);
1113 overlay_id_bits[BIT_WORD(id)] |= BIT_MASK(id);
1114}
1115
1116static void of_unittest_untrack_overlay(int id)
1117{
1118 if (overlay_first_id < 0)
1119 return;
1120 id -= overlay_first_id;
1121 BUG_ON(id >= MAX_UNITTEST_OVERLAYS);
1122 overlay_id_bits[BIT_WORD(id)] &= ~BIT_MASK(id);
1123}
1124
1125static void of_unittest_destroy_tracked_overlays(void)
1126{
1127 int id, ret, defers;
1128
1129 if (overlay_first_id < 0)
1130 return;
1131
1132 /* try until no defers */
1133 do {
1134 defers = 0;
1135 /* remove in reverse order */
1136 for (id = MAX_UNITTEST_OVERLAYS - 1; id >= 0; id--) {
1137 if (!(overlay_id_bits[BIT_WORD(id)] & BIT_MASK(id)))
1138 continue;
1139
1140 ret = of_overlay_destroy(id + overlay_first_id);
1141 if (ret != 0) {
1142 defers++;
1143 pr_warn("%s: overlay destroy failed for #%d\n",
1144 __func__, id + overlay_first_id);
1145 continue;
1146 }
1147
1148 overlay_id_bits[BIT_WORD(id)] &= ~BIT_MASK(id);
1149 }
1150 } while (defers > 0);
1151}
1152
9697a559 1153static int of_unittest_apply_overlay(int unittest_nr, int overlay_nr,
177d271c
PA
1154 int *overlay_id)
1155{
1156 struct device_node *np = NULL;
1157 int ret, id = -1;
1158
1159 np = of_find_node_by_path(overlay_path(overlay_nr));
1160 if (np == NULL) {
9697a559 1161 unittest(0, "could not find overlay node @\"%s\"\n",
177d271c
PA
1162 overlay_path(overlay_nr));
1163 ret = -EINVAL;
1164 goto out;
1165 }
1166
1167 ret = of_overlay_create(np);
1168 if (ret < 0) {
9697a559 1169 unittest(0, "could not create overlay from \"%s\"\n",
177d271c
PA
1170 overlay_path(overlay_nr));
1171 goto out;
1172 }
1173 id = ret;
492a22ac 1174 of_unittest_track_overlay(id);
177d271c
PA
1175
1176 ret = 0;
1177
1178out:
1179 of_node_put(np);
1180
1181 if (overlay_id)
1182 *overlay_id = id;
1183
1184 return ret;
1185}
1186
1187/* apply an overlay while checking before and after states */
9697a559 1188static int of_unittest_apply_overlay_check(int overlay_nr, int unittest_nr,
d5e75500 1189 int before, int after, enum overlay_type ovtype)
177d271c
PA
1190{
1191 int ret;
1192
9697a559
WL
1193 /* unittest device must not be in before state */
1194 if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
1195 unittest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
177d271c 1196 overlay_path(overlay_nr),
9697a559 1197 unittest_path(unittest_nr, ovtype),
177d271c
PA
1198 !before ? "enabled" : "disabled");
1199 return -EINVAL;
1200 }
1201
9697a559 1202 ret = of_unittest_apply_overlay(overlay_nr, unittest_nr, NULL);
177d271c 1203 if (ret != 0) {
9697a559 1204 /* of_unittest_apply_overlay already called unittest() */
177d271c
PA
1205 return ret;
1206 }
1207
9697a559
WL
1208 /* unittest device must be to set to after state */
1209 if (of_unittest_device_exists(unittest_nr, ovtype) != after) {
1210 unittest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n",
177d271c 1211 overlay_path(overlay_nr),
9697a559 1212 unittest_path(unittest_nr, ovtype),
177d271c
PA
1213 !after ? "enabled" : "disabled");
1214 return -EINVAL;
1215 }
1216
1217 return 0;
1218}
1219
1220/* apply an overlay and then revert it while checking before, after states */
9697a559
WL
1221static int of_unittest_apply_revert_overlay_check(int overlay_nr,
1222 int unittest_nr, int before, int after,
d5e75500 1223 enum overlay_type ovtype)
177d271c
PA
1224{
1225 int ret, ov_id;
1226
9697a559
WL
1227 /* unittest device must be in before state */
1228 if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
1229 unittest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
177d271c 1230 overlay_path(overlay_nr),
9697a559 1231 unittest_path(unittest_nr, ovtype),
177d271c
PA
1232 !before ? "enabled" : "disabled");
1233 return -EINVAL;
1234 }
1235
1236 /* apply the overlay */
9697a559 1237 ret = of_unittest_apply_overlay(overlay_nr, unittest_nr, &ov_id);
177d271c 1238 if (ret != 0) {
9697a559 1239 /* of_unittest_apply_overlay already called unittest() */
177d271c
PA
1240 return ret;
1241 }
1242
9697a559
WL
1243 /* unittest device must be in after state */
1244 if (of_unittest_device_exists(unittest_nr, ovtype) != after) {
1245 unittest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n",
177d271c 1246 overlay_path(overlay_nr),
9697a559 1247 unittest_path(unittest_nr, ovtype),
177d271c
PA
1248 !after ? "enabled" : "disabled");
1249 return -EINVAL;
1250 }
1251
1252 ret = of_overlay_destroy(ov_id);
1253 if (ret != 0) {
9697a559 1254 unittest(0, "overlay @\"%s\" failed to be destroyed @\"%s\"\n",
177d271c 1255 overlay_path(overlay_nr),
9697a559 1256 unittest_path(unittest_nr, ovtype));
177d271c
PA
1257 return ret;
1258 }
1259
9697a559
WL
1260 /* unittest device must be again in before state */
1261 if (of_unittest_device_exists(unittest_nr, PDEV_OVERLAY) != before) {
1262 unittest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
177d271c 1263 overlay_path(overlay_nr),
9697a559 1264 unittest_path(unittest_nr, ovtype),
177d271c
PA
1265 !before ? "enabled" : "disabled");
1266 return -EINVAL;
1267 }
1268
1269 return 0;
1270}
1271
1272/* test activation of device */
9697a559 1273static void of_unittest_overlay_0(void)
177d271c
PA
1274{
1275 int ret;
1276
1277 /* device should enable */
9697a559 1278 ret = of_unittest_apply_overlay_check(0, 0, 0, 1, PDEV_OVERLAY);
177d271c
PA
1279 if (ret != 0)
1280 return;
1281
9697a559 1282 unittest(1, "overlay test %d passed\n", 0);
177d271c
PA
1283}
1284
1285/* test deactivation of device */
9697a559 1286static void of_unittest_overlay_1(void)
177d271c
PA
1287{
1288 int ret;
1289
1290 /* device should disable */
9697a559 1291 ret = of_unittest_apply_overlay_check(1, 1, 1, 0, PDEV_OVERLAY);
177d271c
PA
1292 if (ret != 0)
1293 return;
1294
9697a559 1295 unittest(1, "overlay test %d passed\n", 1);
177d271c
PA
1296}
1297
1298/* test activation of device */
9697a559 1299static void of_unittest_overlay_2(void)
177d271c
PA
1300{
1301 int ret;
1302
1303 /* device should enable */
9697a559 1304 ret = of_unittest_apply_overlay_check(2, 2, 0, 1, PDEV_OVERLAY);
177d271c
PA
1305 if (ret != 0)
1306 return;
1307
9697a559 1308 unittest(1, "overlay test %d passed\n", 2);
177d271c
PA
1309}
1310
1311/* test deactivation of device */
9697a559 1312static void of_unittest_overlay_3(void)
177d271c
PA
1313{
1314 int ret;
1315
1316 /* device should disable */
9697a559 1317 ret = of_unittest_apply_overlay_check(3, 3, 1, 0, PDEV_OVERLAY);
177d271c
PA
1318 if (ret != 0)
1319 return;
1320
9697a559 1321 unittest(1, "overlay test %d passed\n", 3);
177d271c
PA
1322}
1323
1324/* test activation of a full device node */
9697a559 1325static void of_unittest_overlay_4(void)
177d271c
PA
1326{
1327 int ret;
1328
1329 /* device should disable */
9697a559 1330 ret = of_unittest_apply_overlay_check(4, 4, 0, 1, PDEV_OVERLAY);
177d271c
PA
1331 if (ret != 0)
1332 return;
1333
9697a559 1334 unittest(1, "overlay test %d passed\n", 4);
177d271c
PA
1335}
1336
1337/* test overlay apply/revert sequence */
9697a559 1338static void of_unittest_overlay_5(void)
177d271c
PA
1339{
1340 int ret;
1341
1342 /* device should disable */
9697a559 1343 ret = of_unittest_apply_revert_overlay_check(5, 5, 0, 1, PDEV_OVERLAY);
177d271c
PA
1344 if (ret != 0)
1345 return;
1346
9697a559 1347 unittest(1, "overlay test %d passed\n", 5);
177d271c
PA
1348}
1349
1350/* test overlay application in sequence */
9697a559 1351static void of_unittest_overlay_6(void)
177d271c
PA
1352{
1353 struct device_node *np;
1354 int ret, i, ov_id[2];
9697a559 1355 int overlay_nr = 6, unittest_nr = 6;
177d271c
PA
1356 int before = 0, after = 1;
1357
9697a559 1358 /* unittest device must be in before state */
177d271c 1359 for (i = 0; i < 2; i++) {
9697a559 1360 if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
177d271c 1361 != before) {
9697a559 1362 unittest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
177d271c 1363 overlay_path(overlay_nr + i),
9697a559 1364 unittest_path(unittest_nr + i,
d5e75500 1365 PDEV_OVERLAY),
177d271c
PA
1366 !before ? "enabled" : "disabled");
1367 return;
1368 }
1369 }
1370
1371 /* apply the overlays */
1372 for (i = 0; i < 2; i++) {
1373
1374 np = of_find_node_by_path(overlay_path(overlay_nr + i));
1375 if (np == NULL) {
9697a559 1376 unittest(0, "could not find overlay node @\"%s\"\n",
177d271c
PA
1377 overlay_path(overlay_nr + i));
1378 return;
1379 }
1380
1381 ret = of_overlay_create(np);
1382 if (ret < 0) {
9697a559 1383 unittest(0, "could not create overlay from \"%s\"\n",
177d271c
PA
1384 overlay_path(overlay_nr + i));
1385 return;
1386 }
1387 ov_id[i] = ret;
492a22ac 1388 of_unittest_track_overlay(ov_id[i]);
177d271c
PA
1389 }
1390
1391 for (i = 0; i < 2; i++) {
9697a559
WL
1392 /* unittest device must be in after state */
1393 if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
177d271c 1394 != after) {
9697a559 1395 unittest(0, "overlay @\"%s\" failed @\"%s\" %s\n",
177d271c 1396 overlay_path(overlay_nr + i),
9697a559 1397 unittest_path(unittest_nr + i,
d5e75500 1398 PDEV_OVERLAY),
177d271c
PA
1399 !after ? "enabled" : "disabled");
1400 return;
1401 }
1402 }
1403
1404 for (i = 1; i >= 0; i--) {
1405 ret = of_overlay_destroy(ov_id[i]);
1406 if (ret != 0) {
9697a559 1407 unittest(0, "overlay @\"%s\" failed destroy @\"%s\"\n",
177d271c 1408 overlay_path(overlay_nr + i),
9697a559 1409 unittest_path(unittest_nr + i,
d5e75500 1410 PDEV_OVERLAY));
177d271c
PA
1411 return;
1412 }
492a22ac 1413 of_unittest_untrack_overlay(ov_id[i]);
177d271c
PA
1414 }
1415
1416 for (i = 0; i < 2; i++) {
9697a559
WL
1417 /* unittest device must be again in before state */
1418 if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
177d271c 1419 != before) {
9697a559 1420 unittest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
177d271c 1421 overlay_path(overlay_nr + i),
9697a559 1422 unittest_path(unittest_nr + i,
d5e75500 1423 PDEV_OVERLAY),
177d271c
PA
1424 !before ? "enabled" : "disabled");
1425 return;
1426 }
1427 }
1428
9697a559 1429 unittest(1, "overlay test %d passed\n", 6);
177d271c
PA
1430}
1431
1432/* test overlay application in sequence */
9697a559 1433static void of_unittest_overlay_8(void)
177d271c
PA
1434{
1435 struct device_node *np;
1436 int ret, i, ov_id[2];
9697a559 1437 int overlay_nr = 8, unittest_nr = 8;
177d271c
PA
1438
1439 /* we don't care about device state in this test */
1440
1441 /* apply the overlays */
1442 for (i = 0; i < 2; i++) {
1443
1444 np = of_find_node_by_path(overlay_path(overlay_nr + i));
1445 if (np == NULL) {
9697a559 1446 unittest(0, "could not find overlay node @\"%s\"\n",
177d271c
PA
1447 overlay_path(overlay_nr + i));
1448 return;
1449 }
1450
1451 ret = of_overlay_create(np);
1452 if (ret < 0) {
9697a559 1453 unittest(0, "could not create overlay from \"%s\"\n",
177d271c
PA
1454 overlay_path(overlay_nr + i));
1455 return;
1456 }
1457 ov_id[i] = ret;
492a22ac 1458 of_unittest_track_overlay(ov_id[i]);
177d271c
PA
1459 }
1460
1461 /* now try to remove first overlay (it should fail) */
1462 ret = of_overlay_destroy(ov_id[0]);
1463 if (ret == 0) {
9697a559 1464 unittest(0, "overlay @\"%s\" was destroyed @\"%s\"\n",
177d271c 1465 overlay_path(overlay_nr + 0),
9697a559 1466 unittest_path(unittest_nr,
d5e75500 1467 PDEV_OVERLAY));
177d271c
PA
1468 return;
1469 }
1470
1471 /* removing them in order should work */
1472 for (i = 1; i >= 0; i--) {
1473 ret = of_overlay_destroy(ov_id[i]);
1474 if (ret != 0) {
9697a559 1475 unittest(0, "overlay @\"%s\" not destroyed @\"%s\"\n",
177d271c 1476 overlay_path(overlay_nr + i),
9697a559 1477 unittest_path(unittest_nr,
d5e75500 1478 PDEV_OVERLAY));
177d271c
PA
1479 return;
1480 }
492a22ac 1481 of_unittest_untrack_overlay(ov_id[i]);
177d271c
PA
1482 }
1483
9697a559 1484 unittest(1, "overlay test %d passed\n", 8);
177d271c
PA
1485}
1486
6b1271de 1487/* test insertion of a bus with parent devices */
9697a559 1488static void of_unittest_overlay_10(void)
6b1271de
PA
1489{
1490 int ret;
1491 char *child_path;
1492
1493 /* device should disable */
9697a559
WL
1494 ret = of_unittest_apply_overlay_check(10, 10, 0, 1, PDEV_OVERLAY);
1495 if (unittest(ret == 0,
d5e75500 1496 "overlay test %d failed; overlay application\n", 10))
6b1271de
PA
1497 return;
1498
9697a559
WL
1499 child_path = kasprintf(GFP_KERNEL, "%s/test-unittest101",
1500 unittest_path(10, PDEV_OVERLAY));
1501 if (unittest(child_path, "overlay test %d failed; kasprintf\n", 10))
6b1271de
PA
1502 return;
1503
d5e75500 1504 ret = of_path_device_type_exists(child_path, PDEV_OVERLAY);
6b1271de 1505 kfree(child_path);
9697a559 1506 if (unittest(ret, "overlay test %d failed; no child device\n", 10))
6b1271de
PA
1507 return;
1508}
1509
1510/* test insertion of a bus with parent devices (and revert) */
9697a559 1511static void of_unittest_overlay_11(void)
6b1271de
PA
1512{
1513 int ret;
1514
1515 /* device should disable */
9697a559 1516 ret = of_unittest_apply_revert_overlay_check(11, 11, 0, 1,
d5e75500 1517 PDEV_OVERLAY);
9697a559 1518 if (unittest(ret == 0,
d5e75500
PA
1519 "overlay test %d failed; overlay application\n", 11))
1520 return;
1521}
1522
4252de39 1523#if IS_BUILTIN(CONFIG_I2C) && IS_ENABLED(CONFIG_OF_OVERLAY)
d5e75500 1524
9697a559 1525struct unittest_i2c_bus_data {
d5e75500
PA
1526 struct platform_device *pdev;
1527 struct i2c_adapter adap;
1528};
1529
9697a559 1530static int unittest_i2c_master_xfer(struct i2c_adapter *adap,
d5e75500
PA
1531 struct i2c_msg *msgs, int num)
1532{
9697a559 1533 struct unittest_i2c_bus_data *std = i2c_get_adapdata(adap);
d5e75500
PA
1534
1535 (void)std;
1536
1537 return num;
1538}
1539
9697a559 1540static u32 unittest_i2c_functionality(struct i2c_adapter *adap)
d5e75500
PA
1541{
1542 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
1543}
1544
9697a559
WL
1545static const struct i2c_algorithm unittest_i2c_algo = {
1546 .master_xfer = unittest_i2c_master_xfer,
1547 .functionality = unittest_i2c_functionality,
d5e75500
PA
1548};
1549
9697a559 1550static int unittest_i2c_bus_probe(struct platform_device *pdev)
d5e75500
PA
1551{
1552 struct device *dev = &pdev->dev;
1553 struct device_node *np = dev->of_node;
9697a559 1554 struct unittest_i2c_bus_data *std;
d5e75500
PA
1555 struct i2c_adapter *adap;
1556 int ret;
1557
1558 if (np == NULL) {
1559 dev_err(dev, "No OF data for device\n");
1560 return -EINVAL;
1561
1562 }
1563
1564 dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
1565
1566 std = devm_kzalloc(dev, sizeof(*std), GFP_KERNEL);
1567 if (!std) {
9697a559 1568 dev_err(dev, "Failed to allocate unittest i2c data\n");
d5e75500
PA
1569 return -ENOMEM;
1570 }
1571
1572 /* link them together */
1573 std->pdev = pdev;
1574 platform_set_drvdata(pdev, std);
1575
1576 adap = &std->adap;
1577 i2c_set_adapdata(adap, std);
1578 adap->nr = -1;
1579 strlcpy(adap->name, pdev->name, sizeof(adap->name));
1580 adap->class = I2C_CLASS_DEPRECATED;
9697a559 1581 adap->algo = &unittest_i2c_algo;
d5e75500
PA
1582 adap->dev.parent = dev;
1583 adap->dev.of_node = dev->of_node;
1584 adap->timeout = 5 * HZ;
1585 adap->retries = 3;
1586
1587 ret = i2c_add_numbered_adapter(adap);
1588 if (ret != 0) {
1589 dev_err(dev, "Failed to add I2C adapter\n");
1590 return ret;
1591 }
1592
1593 return 0;
1594}
1595
9697a559 1596static int unittest_i2c_bus_remove(struct platform_device *pdev)
d5e75500
PA
1597{
1598 struct device *dev = &pdev->dev;
1599 struct device_node *np = dev->of_node;
9697a559 1600 struct unittest_i2c_bus_data *std = platform_get_drvdata(pdev);
d5e75500
PA
1601
1602 dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
1603 i2c_del_adapter(&std->adap);
1604
1605 return 0;
1606}
1607
9697a559
WL
1608static struct of_device_id unittest_i2c_bus_match[] = {
1609 { .compatible = "unittest-i2c-bus", },
d5e75500
PA
1610 {},
1611};
1612
9697a559
WL
1613static struct platform_driver unittest_i2c_bus_driver = {
1614 .probe = unittest_i2c_bus_probe,
1615 .remove = unittest_i2c_bus_remove,
d5e75500 1616 .driver = {
9697a559
WL
1617 .name = "unittest-i2c-bus",
1618 .of_match_table = of_match_ptr(unittest_i2c_bus_match),
d5e75500
PA
1619 },
1620};
1621
9697a559 1622static int unittest_i2c_dev_probe(struct i2c_client *client,
d5e75500
PA
1623 const struct i2c_device_id *id)
1624{
1625 struct device *dev = &client->dev;
1626 struct device_node *np = client->dev.of_node;
1627
1628 if (!np) {
1629 dev_err(dev, "No OF node\n");
1630 return -EINVAL;
1631 }
1632
1633 dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
1634
1635 return 0;
1636};
1637
9697a559 1638static int unittest_i2c_dev_remove(struct i2c_client *client)
d5e75500
PA
1639{
1640 struct device *dev = &client->dev;
1641 struct device_node *np = client->dev.of_node;
1642
1643 dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
1644 return 0;
1645}
1646
9697a559
WL
1647static const struct i2c_device_id unittest_i2c_dev_id[] = {
1648 { .name = "unittest-i2c-dev" },
d5e75500
PA
1649 { }
1650};
1651
9697a559 1652static struct i2c_driver unittest_i2c_dev_driver = {
d5e75500 1653 .driver = {
9697a559 1654 .name = "unittest-i2c-dev",
d5e75500
PA
1655 .owner = THIS_MODULE,
1656 },
9697a559
WL
1657 .probe = unittest_i2c_dev_probe,
1658 .remove = unittest_i2c_dev_remove,
1659 .id_table = unittest_i2c_dev_id,
d5e75500
PA
1660};
1661
4252de39 1662#if IS_BUILTIN(CONFIG_I2C_MUX)
d5e75500 1663
9697a559 1664struct unittest_i2c_mux_data {
d5e75500
PA
1665 int nchans;
1666 struct i2c_adapter *adap[];
1667};
1668
9697a559 1669static int unittest_i2c_mux_select_chan(struct i2c_adapter *adap,
d5e75500
PA
1670 void *client, u32 chan)
1671{
1672 return 0;
1673}
1674
9697a559 1675static int unittest_i2c_mux_probe(struct i2c_client *client,
d5e75500
PA
1676 const struct i2c_device_id *id)
1677{
1678 int ret, i, nchans, size;
1679 struct device *dev = &client->dev;
1680 struct i2c_adapter *adap = to_i2c_adapter(dev->parent);
1681 struct device_node *np = client->dev.of_node, *child;
9697a559 1682 struct unittest_i2c_mux_data *stm;
d5e75500
PA
1683 u32 reg, max_reg;
1684
1685 dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
1686
1687 if (!np) {
1688 dev_err(dev, "No OF node\n");
1689 return -EINVAL;
1690 }
1691
1692 max_reg = (u32)-1;
1693 for_each_child_of_node(np, child) {
1694 ret = of_property_read_u32(child, "reg", &reg);
1695 if (ret)
1696 continue;
1697 if (max_reg == (u32)-1 || reg > max_reg)
1698 max_reg = reg;
1699 }
1700 nchans = max_reg == (u32)-1 ? 0 : max_reg + 1;
1701 if (nchans == 0) {
1702 dev_err(dev, "No channels\n");
1703 return -EINVAL;
1704 }
1705
9697a559 1706 size = offsetof(struct unittest_i2c_mux_data, adap[nchans]);
d5e75500
PA
1707 stm = devm_kzalloc(dev, size, GFP_KERNEL);
1708 if (!stm) {
1709 dev_err(dev, "Out of memory\n");
1710 return -ENOMEM;
1711 }
1712 stm->nchans = nchans;
1713 for (i = 0; i < nchans; i++) {
1714 stm->adap[i] = i2c_add_mux_adapter(adap, dev, client,
9697a559 1715 0, i, 0, unittest_i2c_mux_select_chan, NULL);
d5e75500
PA
1716 if (!stm->adap[i]) {
1717 dev_err(dev, "Failed to register mux #%d\n", i);
1718 for (i--; i >= 0; i--)
1719 i2c_del_mux_adapter(stm->adap[i]);
1720 return -ENODEV;
1721 }
1722 }
1723
1724 i2c_set_clientdata(client, stm);
1725
1726 return 0;
1727};
1728
9697a559 1729static int unittest_i2c_mux_remove(struct i2c_client *client)
d5e75500
PA
1730{
1731 struct device *dev = &client->dev;
1732 struct device_node *np = client->dev.of_node;
9697a559 1733 struct unittest_i2c_mux_data *stm = i2c_get_clientdata(client);
d5e75500
PA
1734 int i;
1735
1736 dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
1737 for (i = stm->nchans - 1; i >= 0; i--)
1738 i2c_del_mux_adapter(stm->adap[i]);
1739 return 0;
1740}
1741
9697a559
WL
1742static const struct i2c_device_id unittest_i2c_mux_id[] = {
1743 { .name = "unittest-i2c-mux" },
d5e75500
PA
1744 { }
1745};
1746
9697a559 1747static struct i2c_driver unittest_i2c_mux_driver = {
d5e75500 1748 .driver = {
9697a559 1749 .name = "unittest-i2c-mux",
d5e75500
PA
1750 .owner = THIS_MODULE,
1751 },
9697a559
WL
1752 .probe = unittest_i2c_mux_probe,
1753 .remove = unittest_i2c_mux_remove,
1754 .id_table = unittest_i2c_mux_id,
d5e75500
PA
1755};
1756
1757#endif
1758
9697a559 1759static int of_unittest_overlay_i2c_init(void)
d5e75500
PA
1760{
1761 int ret;
1762
9697a559
WL
1763 ret = i2c_add_driver(&unittest_i2c_dev_driver);
1764 if (unittest(ret == 0,
1765 "could not register unittest i2c device driver\n"))
d5e75500
PA
1766 return ret;
1767
9697a559
WL
1768 ret = platform_driver_register(&unittest_i2c_bus_driver);
1769 if (unittest(ret == 0,
1770 "could not register unittest i2c bus driver\n"))
d5e75500
PA
1771 return ret;
1772
4252de39 1773#if IS_BUILTIN(CONFIG_I2C_MUX)
9697a559
WL
1774 ret = i2c_add_driver(&unittest_i2c_mux_driver);
1775 if (unittest(ret == 0,
1776 "could not register unittest i2c mux driver\n"))
d5e75500
PA
1777 return ret;
1778#endif
1779
1780 return 0;
1781}
1782
9697a559 1783static void of_unittest_overlay_i2c_cleanup(void)
d5e75500 1784{
4252de39 1785#if IS_BUILTIN(CONFIG_I2C_MUX)
9697a559 1786 i2c_del_driver(&unittest_i2c_mux_driver);
d5e75500 1787#endif
9697a559
WL
1788 platform_driver_unregister(&unittest_i2c_bus_driver);
1789 i2c_del_driver(&unittest_i2c_dev_driver);
d5e75500
PA
1790}
1791
9697a559 1792static void of_unittest_overlay_i2c_12(void)
d5e75500
PA
1793{
1794 int ret;
1795
1796 /* device should enable */
9697a559 1797 ret = of_unittest_apply_overlay_check(12, 12, 0, 1, I2C_OVERLAY);
d5e75500
PA
1798 if (ret != 0)
1799 return;
1800
9697a559 1801 unittest(1, "overlay test %d passed\n", 12);
d5e75500
PA
1802}
1803
1804/* test deactivation of device */
9697a559 1805static void of_unittest_overlay_i2c_13(void)
d5e75500
PA
1806{
1807 int ret;
1808
1809 /* device should disable */
9697a559 1810 ret = of_unittest_apply_overlay_check(13, 13, 1, 0, I2C_OVERLAY);
d5e75500 1811 if (ret != 0)
6b1271de 1812 return;
d5e75500 1813
9697a559 1814 unittest(1, "overlay test %d passed\n", 13);
d5e75500
PA
1815}
1816
1817/* just check for i2c mux existence */
9697a559 1818static void of_unittest_overlay_i2c_14(void)
d5e75500 1819{
6b1271de
PA
1820}
1821
9697a559 1822static void of_unittest_overlay_i2c_15(void)
d5e75500
PA
1823{
1824 int ret;
1825
1826 /* device should enable */
9697a559 1827 ret = of_unittest_apply_overlay_check(16, 15, 0, 1, I2C_OVERLAY);
d5e75500
PA
1828 if (ret != 0)
1829 return;
1830
9697a559 1831 unittest(1, "overlay test %d passed\n", 15);
d5e75500
PA
1832}
1833
1834#else
1835
9697a559
WL
1836static inline void of_unittest_overlay_i2c_14(void) { }
1837static inline void of_unittest_overlay_i2c_15(void) { }
d5e75500
PA
1838
1839#endif
1840
9697a559 1841static void __init of_unittest_overlay(void)
177d271c
PA
1842{
1843 struct device_node *bus_np = NULL;
1844 int ret;
1845
9697a559 1846 ret = platform_driver_register(&unittest_driver);
177d271c 1847 if (ret != 0) {
9697a559 1848 unittest(0, "could not register unittest driver\n");
177d271c
PA
1849 goto out;
1850 }
1851
1852 bus_np = of_find_node_by_path(bus_path);
1853 if (bus_np == NULL) {
9697a559 1854 unittest(0, "could not find bus_path \"%s\"\n", bus_path);
177d271c
PA
1855 goto out;
1856 }
1857
1858 ret = of_platform_populate(bus_np, of_default_bus_match_table,
1859 NULL, NULL);
1860 if (ret != 0) {
9697a559 1861 unittest(0, "could not populate bus @ \"%s\"\n", bus_path);
177d271c
PA
1862 goto out;
1863 }
1864
9697a559
WL
1865 if (!of_unittest_device_exists(100, PDEV_OVERLAY)) {
1866 unittest(0, "could not find unittest0 @ \"%s\"\n",
1867 unittest_path(100, PDEV_OVERLAY));
177d271c
PA
1868 goto out;
1869 }
1870
9697a559
WL
1871 if (of_unittest_device_exists(101, PDEV_OVERLAY)) {
1872 unittest(0, "unittest1 @ \"%s\" should not exist\n",
1873 unittest_path(101, PDEV_OVERLAY));
177d271c
PA
1874 goto out;
1875 }
1876
9697a559 1877 unittest(1, "basic infrastructure of overlays passed");
177d271c
PA
1878
1879 /* tests in sequence */
9697a559
WL
1880 of_unittest_overlay_0();
1881 of_unittest_overlay_1();
1882 of_unittest_overlay_2();
1883 of_unittest_overlay_3();
1884 of_unittest_overlay_4();
1885 of_unittest_overlay_5();
1886 of_unittest_overlay_6();
1887 of_unittest_overlay_8();
1888
1889 of_unittest_overlay_10();
1890 of_unittest_overlay_11();
6b1271de 1891
4252de39 1892#if IS_BUILTIN(CONFIG_I2C)
9697a559 1893 if (unittest(of_unittest_overlay_i2c_init() == 0, "i2c init failed\n"))
d5e75500
PA
1894 goto out;
1895
9697a559
WL
1896 of_unittest_overlay_i2c_12();
1897 of_unittest_overlay_i2c_13();
1898 of_unittest_overlay_i2c_14();
1899 of_unittest_overlay_i2c_15();
d5e75500 1900
9697a559 1901 of_unittest_overlay_i2c_cleanup();
d5e75500
PA
1902#endif
1903
492a22ac
PA
1904 of_unittest_destroy_tracked_overlays();
1905
177d271c
PA
1906out:
1907 of_node_put(bus_np);
1908}
1909
1910#else
9697a559 1911static inline void __init of_unittest_overlay(void) { }
177d271c
PA
1912#endif
1913
9697a559 1914static int __init of_unittest(void)
53a42093
GL
1915{
1916 struct device_node *np;
ae9304c9
GM
1917 int res;
1918
9697a559
WL
1919 /* adding data for unittest */
1920 res = unittest_data_add();
ae9304c9
GM
1921 if (res)
1922 return res;
788ec2fc
GL
1923 if (!of_aliases)
1924 of_aliases = of_find_node_by_path("/aliases");
53a42093
GL
1925
1926 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
1927 if (!np) {
1928 pr_info("No testcase data in device tree; not running tests\n");
1929 return 0;
1930 }
1931 of_node_put(np);
1932
9697a559
WL
1933 pr_info("start of unittest - you will see error messages\n");
1934 of_unittest_check_tree_linkage();
1935 of_unittest_check_phandles();
1936 of_unittest_find_node_by_name();
1937 of_unittest_dynamic();
1938 of_unittest_parse_phandle_with_args();
1939 of_unittest_property_string();
1940 of_unittest_property_copy();
1941 of_unittest_changeset();
1942 of_unittest_parse_interrupts();
1943 of_unittest_parse_interrupts_extended();
1944 of_unittest_match_node();
1945 of_unittest_platform_populate();
1946 of_unittest_overlay();
ae9304c9 1947
f2051d6a 1948 /* Double check linkage after removing testcase data */
9697a559 1949 of_unittest_check_tree_linkage();
f2051d6a 1950
9697a559
WL
1951 pr_info("end of unittest - %i passed, %i failed\n",
1952 unittest_results.passed, unittest_results.failed);
f2051d6a 1953
53a42093
GL
1954 return 0;
1955}
9697a559 1956late_initcall(of_unittest);
This page took 0.557368 seconds and 5 git commands to generate.