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