]>
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 | ||
a9f10ca7 GL |
28 | static struct selftest_results { |
29 | int passed; | |
30 | int failed; | |
31 | } selftest_results; | |
32 | ||
2eb46da2 | 33 | #define NO_OF_NODES 3 |
ae9304c9 GM |
34 | static struct device_node *nodes[NO_OF_NODES]; |
35 | static int last_node_index; | |
b951f9dc | 36 | static bool selftest_live_tree; |
ae9304c9 | 37 | |
851da976 GL |
38 | #define selftest(result, fmt, ...) ({ \ |
39 | bool failed = !(result); \ | |
40 | if (failed) { \ | |
a9f10ca7 GL |
41 | selftest_results.failed++; \ |
42 | pr_err("FAIL %s():%i " fmt, __func__, __LINE__, ##__VA_ARGS__); \ | |
cabb7d5b | 43 | } else { \ |
a9f10ca7 GL |
44 | selftest_results.passed++; \ |
45 | pr_debug("pass %s():%i\n", __func__, __LINE__); \ | |
cabb7d5b | 46 | } \ |
851da976 GL |
47 | failed; \ |
48 | }) | |
53a42093 | 49 | |
ae91ff72 GL |
50 | static void __init of_selftest_find_node_by_name(void) |
51 | { | |
52 | struct device_node *np; | |
75c28c09 | 53 | const char *options; |
ae91ff72 GL |
54 | |
55 | np = of_find_node_by_path("/testcase-data"); | |
56 | selftest(np && !strcmp("/testcase-data", np->full_name), | |
57 | "find /testcase-data failed\n"); | |
58 | of_node_put(np); | |
59 | ||
60 | /* Test if trailing '/' works */ | |
61 | np = of_find_node_by_path("/testcase-data/"); | |
62 | selftest(!np, "trailing '/' on /testcase-data/ should fail\n"); | |
63 | ||
64 | np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a"); | |
65 | selftest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", np->full_name), | |
66 | "find /testcase-data/phandle-tests/consumer-a failed\n"); | |
67 | of_node_put(np); | |
68 | ||
69 | np = of_find_node_by_path("testcase-alias"); | |
70 | selftest(np && !strcmp("/testcase-data", np->full_name), | |
71 | "find testcase-alias failed\n"); | |
72 | of_node_put(np); | |
73 | ||
74 | /* Test if trailing '/' works on aliases */ | |
75 | np = of_find_node_by_path("testcase-alias/"); | |
76 | selftest(!np, "trailing '/' on testcase-alias/ should fail\n"); | |
77 | ||
78 | np = of_find_node_by_path("testcase-alias/phandle-tests/consumer-a"); | |
79 | selftest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", np->full_name), | |
80 | "find testcase-alias/phandle-tests/consumer-a failed\n"); | |
81 | of_node_put(np); | |
82 | ||
83 | np = of_find_node_by_path("/testcase-data/missing-path"); | |
84 | selftest(!np, "non-existent path returned node %s\n", np->full_name); | |
85 | of_node_put(np); | |
86 | ||
87 | np = of_find_node_by_path("missing-alias"); | |
88 | selftest(!np, "non-existent alias returned node %s\n", np->full_name); | |
89 | of_node_put(np); | |
90 | ||
91 | np = of_find_node_by_path("testcase-alias/missing-path"); | |
92 | selftest(!np, "non-existent alias with relative path returned node %s\n", np->full_name); | |
93 | of_node_put(np); | |
75c28c09 LL |
94 | |
95 | np = of_find_node_opts_by_path("/testcase-data:testoption", &options); | |
96 | selftest(np && !strcmp("testoption", options), | |
97 | "option path test failed\n"); | |
98 | of_node_put(np); | |
99 | ||
100 | np = of_find_node_opts_by_path("/testcase-data:testoption", NULL); | |
101 | selftest(np, "NULL option path test failed\n"); | |
102 | of_node_put(np); | |
103 | ||
104 | np = of_find_node_opts_by_path("testcase-alias:testaliasoption", | |
105 | &options); | |
106 | selftest(np && !strcmp("testaliasoption", options), | |
107 | "option alias path test failed\n"); | |
108 | of_node_put(np); | |
109 | ||
110 | np = of_find_node_opts_by_path("testcase-alias:testaliasoption", NULL); | |
111 | selftest(np, "NULL option alias path test failed\n"); | |
112 | of_node_put(np); | |
113 | ||
114 | options = "testoption"; | |
115 | np = of_find_node_opts_by_path("testcase-alias", &options); | |
116 | selftest(np && !options, "option clearing test failed\n"); | |
117 | of_node_put(np); | |
118 | ||
119 | options = "testoption"; | |
120 | np = of_find_node_opts_by_path("/", &options); | |
121 | selftest(np && !options, "option clearing root node test failed\n"); | |
122 | of_node_put(np); | |
ae91ff72 GL |
123 | } |
124 | ||
7e66c5c7 GL |
125 | static void __init of_selftest_dynamic(void) |
126 | { | |
127 | struct device_node *np; | |
128 | struct property *prop; | |
129 | ||
130 | np = of_find_node_by_path("/testcase-data"); | |
131 | if (!np) { | |
132 | pr_err("missing testcase data\n"); | |
133 | return; | |
134 | } | |
135 | ||
136 | /* Array of 4 properties for the purpose of testing */ | |
137 | prop = kzalloc(sizeof(*prop) * 4, GFP_KERNEL); | |
138 | if (!prop) { | |
139 | selftest(0, "kzalloc() failed\n"); | |
140 | return; | |
141 | } | |
142 | ||
143 | /* Add a new property - should pass*/ | |
144 | prop->name = "new-property"; | |
145 | prop->value = "new-property-data"; | |
146 | prop->length = strlen(prop->value); | |
147 | selftest(of_add_property(np, prop) == 0, "Adding a new property failed\n"); | |
148 | ||
149 | /* Try to add an existing property - should fail */ | |
150 | prop++; | |
151 | prop->name = "new-property"; | |
152 | prop->value = "new-property-data-should-fail"; | |
153 | prop->length = strlen(prop->value); | |
154 | selftest(of_add_property(np, prop) != 0, | |
155 | "Adding an existing property should have failed\n"); | |
156 | ||
157 | /* Try to modify an existing property - should pass */ | |
158 | prop->value = "modify-property-data-should-pass"; | |
159 | prop->length = strlen(prop->value); | |
160 | selftest(of_update_property(np, prop) == 0, | |
161 | "Updating an existing property should have passed\n"); | |
162 | ||
163 | /* Try to modify non-existent property - should pass*/ | |
164 | prop++; | |
165 | prop->name = "modify-property"; | |
166 | prop->value = "modify-missing-property-data-should-pass"; | |
167 | prop->length = strlen(prop->value); | |
168 | selftest(of_update_property(np, prop) == 0, | |
169 | "Updating a missing property should have passed\n"); | |
170 | ||
171 | /* Remove property - should pass */ | |
172 | selftest(of_remove_property(np, prop) == 0, | |
173 | "Removing a property should have passed\n"); | |
174 | ||
175 | /* Adding very large property - should pass */ | |
176 | prop++; | |
177 | prop->name = "large-property-PAGE_SIZEx8"; | |
178 | prop->length = PAGE_SIZE * 8; | |
179 | prop->value = kzalloc(prop->length, GFP_KERNEL); | |
180 | selftest(prop->value != NULL, "Unable to allocate large buffer\n"); | |
181 | if (prop->value) | |
182 | selftest(of_add_property(np, prop) == 0, | |
183 | "Adding a large property should have passed\n"); | |
184 | } | |
185 | ||
f2051d6a GL |
186 | static int __init of_selftest_check_node_linkage(struct device_node *np) |
187 | { | |
5063e25a | 188 | struct device_node *child; |
f2051d6a GL |
189 | int count = 0, rc; |
190 | ||
191 | for_each_child_of_node(np, child) { | |
192 | if (child->parent != np) { | |
193 | pr_err("Child node %s links to wrong parent %s\n", | |
194 | child->name, np->name); | |
195 | return -EINVAL; | |
196 | } | |
197 | ||
f2051d6a GL |
198 | rc = of_selftest_check_node_linkage(child); |
199 | if (rc < 0) | |
200 | return rc; | |
201 | count += rc; | |
202 | } | |
203 | ||
204 | return count + 1; | |
205 | } | |
206 | ||
207 | static void __init of_selftest_check_tree_linkage(void) | |
208 | { | |
209 | struct device_node *np; | |
210 | int allnode_count = 0, child_count; | |
211 | ||
5063e25a | 212 | if (!of_root) |
f2051d6a GL |
213 | return; |
214 | ||
215 | for_each_of_allnodes(np) | |
216 | allnode_count++; | |
5063e25a | 217 | child_count = of_selftest_check_node_linkage(of_root); |
f2051d6a GL |
218 | |
219 | selftest(child_count > 0, "Device node data structure is corrupted\n"); | |
220 | selftest(child_count == allnode_count, "allnodes list size (%i) doesn't match" | |
221 | "sibling lists size (%i)\n", allnode_count, child_count); | |
222 | pr_debug("allnodes list size (%i); sibling lists size (%i)\n", allnode_count, child_count); | |
223 | } | |
224 | ||
841ec213 GL |
225 | struct node_hash { |
226 | struct hlist_node node; | |
227 | struct device_node *np; | |
228 | }; | |
229 | ||
2118f4b8 | 230 | static DEFINE_HASHTABLE(phandle_ht, 8); |
841ec213 GL |
231 | static void __init of_selftest_check_phandles(void) |
232 | { | |
233 | struct device_node *np; | |
234 | struct node_hash *nh; | |
235 | struct hlist_node *tmp; | |
236 | int i, dup_count = 0, phandle_count = 0; | |
841ec213 | 237 | |
841ec213 GL |
238 | for_each_of_allnodes(np) { |
239 | if (!np->phandle) | |
240 | continue; | |
241 | ||
2118f4b8 | 242 | hash_for_each_possible(phandle_ht, nh, node, np->phandle) { |
841ec213 GL |
243 | if (nh->np->phandle == np->phandle) { |
244 | pr_info("Duplicate phandle! %i used by %s and %s\n", | |
245 | np->phandle, nh->np->full_name, np->full_name); | |
246 | dup_count++; | |
247 | break; | |
248 | } | |
249 | } | |
250 | ||
251 | nh = kzalloc(sizeof(*nh), GFP_KERNEL); | |
252 | if (WARN_ON(!nh)) | |
253 | return; | |
254 | ||
255 | nh->np = np; | |
2118f4b8 | 256 | hash_add(phandle_ht, &nh->node, np->phandle); |
841ec213 GL |
257 | phandle_count++; |
258 | } | |
259 | selftest(dup_count == 0, "Found %i duplicates in %i phandles\n", | |
260 | dup_count, phandle_count); | |
261 | ||
262 | /* Clean up */ | |
2118f4b8 | 263 | hash_for_each_safe(phandle_ht, i, tmp, nh, node) { |
841ec213 GL |
264 | hash_del(&nh->node); |
265 | kfree(nh); | |
266 | } | |
267 | } | |
268 | ||
53a42093 GL |
269 | static void __init of_selftest_parse_phandle_with_args(void) |
270 | { | |
271 | struct device_node *np; | |
272 | struct of_phandle_args args; | |
cabb7d5b | 273 | int i, rc; |
53a42093 | 274 | |
53a42093 GL |
275 | np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a"); |
276 | if (!np) { | |
277 | pr_err("missing testcase data\n"); | |
278 | return; | |
279 | } | |
280 | ||
bd69f73f GL |
281 | rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells"); |
282 | selftest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc); | |
283 | ||
f7f951c2 | 284 | for (i = 0; i < 8; i++) { |
53a42093 GL |
285 | bool passed = true; |
286 | rc = of_parse_phandle_with_args(np, "phandle-list", | |
287 | "#phandle-cells", i, &args); | |
288 | ||
289 | /* Test the values from tests-phandle.dtsi */ | |
290 | switch (i) { | |
291 | case 0: | |
292 | passed &= !rc; | |
293 | passed &= (args.args_count == 1); | |
294 | passed &= (args.args[0] == (i + 1)); | |
295 | break; | |
296 | case 1: | |
297 | passed &= !rc; | |
298 | passed &= (args.args_count == 2); | |
299 | passed &= (args.args[0] == (i + 1)); | |
300 | passed &= (args.args[1] == 0); | |
301 | break; | |
302 | case 2: | |
303 | passed &= (rc == -ENOENT); | |
304 | break; | |
305 | case 3: | |
306 | passed &= !rc; | |
307 | passed &= (args.args_count == 3); | |
308 | passed &= (args.args[0] == (i + 1)); | |
309 | passed &= (args.args[1] == 4); | |
310 | passed &= (args.args[2] == 3); | |
311 | break; | |
312 | case 4: | |
313 | passed &= !rc; | |
314 | passed &= (args.args_count == 2); | |
315 | passed &= (args.args[0] == (i + 1)); | |
316 | passed &= (args.args[1] == 100); | |
317 | break; | |
318 | case 5: | |
319 | passed &= !rc; | |
320 | passed &= (args.args_count == 0); | |
321 | break; | |
322 | case 6: | |
323 | passed &= !rc; | |
324 | passed &= (args.args_count == 1); | |
325 | passed &= (args.args[0] == (i + 1)); | |
326 | break; | |
327 | case 7: | |
cabb7d5b | 328 | passed &= (rc == -ENOENT); |
53a42093 GL |
329 | break; |
330 | default: | |
331 | passed = false; | |
332 | } | |
333 | ||
cabb7d5b GL |
334 | selftest(passed, "index %i - data error on node %s rc=%i\n", |
335 | i, args.np->full_name, rc); | |
53a42093 GL |
336 | } |
337 | ||
338 | /* Check for missing list property */ | |
339 | rc = of_parse_phandle_with_args(np, "phandle-list-missing", | |
340 | "#phandle-cells", 0, &args); | |
cabb7d5b | 341 | selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc); |
bd69f73f GL |
342 | rc = of_count_phandle_with_args(np, "phandle-list-missing", |
343 | "#phandle-cells"); | |
344 | selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc); | |
53a42093 GL |
345 | |
346 | /* Check for missing cells property */ | |
347 | rc = of_parse_phandle_with_args(np, "phandle-list", | |
348 | "#phandle-cells-missing", 0, &args); | |
cabb7d5b | 349 | selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc); |
bd69f73f GL |
350 | rc = of_count_phandle_with_args(np, "phandle-list", |
351 | "#phandle-cells-missing"); | |
352 | selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc); | |
53a42093 GL |
353 | |
354 | /* Check for bad phandle in list */ | |
355 | rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle", | |
356 | "#phandle-cells", 0, &args); | |
cabb7d5b | 357 | selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc); |
bd69f73f GL |
358 | rc = of_count_phandle_with_args(np, "phandle-list-bad-phandle", |
359 | "#phandle-cells"); | |
360 | selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc); | |
53a42093 GL |
361 | |
362 | /* Check for incorrectly formed argument list */ | |
363 | rc = of_parse_phandle_with_args(np, "phandle-list-bad-args", | |
364 | "#phandle-cells", 1, &args); | |
cabb7d5b | 365 | selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc); |
bd69f73f GL |
366 | rc = of_count_phandle_with_args(np, "phandle-list-bad-args", |
367 | "#phandle-cells"); | |
368 | selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc); | |
53a42093 GL |
369 | } |
370 | ||
a87fa1d8 | 371 | static void __init of_selftest_property_string(void) |
7aff0fe3 | 372 | { |
a87fa1d8 | 373 | const char *strings[4]; |
7aff0fe3 GL |
374 | struct device_node *np; |
375 | int rc; | |
376 | ||
7aff0fe3 GL |
377 | np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a"); |
378 | if (!np) { | |
379 | pr_err("No testcase data in device tree\n"); | |
380 | return; | |
381 | } | |
382 | ||
383 | rc = of_property_match_string(np, "phandle-list-names", "first"); | |
384 | selftest(rc == 0, "first expected:0 got:%i\n", rc); | |
385 | rc = of_property_match_string(np, "phandle-list-names", "second"); | |
386 | selftest(rc == 1, "second expected:0 got:%i\n", rc); | |
387 | rc = of_property_match_string(np, "phandle-list-names", "third"); | |
388 | selftest(rc == 2, "third expected:0 got:%i\n", rc); | |
389 | rc = of_property_match_string(np, "phandle-list-names", "fourth"); | |
a87fa1d8 | 390 | selftest(rc == -ENODATA, "unmatched string; rc=%i\n", rc); |
7aff0fe3 | 391 | rc = of_property_match_string(np, "missing-property", "blah"); |
a87fa1d8 | 392 | selftest(rc == -EINVAL, "missing property; rc=%i\n", rc); |
7aff0fe3 | 393 | rc = of_property_match_string(np, "empty-property", "blah"); |
a87fa1d8 | 394 | selftest(rc == -ENODATA, "empty property; rc=%i\n", rc); |
7aff0fe3 | 395 | rc = of_property_match_string(np, "unterminated-string", "blah"); |
a87fa1d8 GL |
396 | selftest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc); |
397 | ||
398 | /* of_property_count_strings() tests */ | |
399 | rc = of_property_count_strings(np, "string-property"); | |
400 | selftest(rc == 1, "Incorrect string count; rc=%i\n", rc); | |
401 | rc = of_property_count_strings(np, "phandle-list-names"); | |
402 | selftest(rc == 3, "Incorrect string count; rc=%i\n", rc); | |
403 | rc = of_property_count_strings(np, "unterminated-string"); | |
404 | selftest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc); | |
405 | rc = of_property_count_strings(np, "unterminated-string-list"); | |
406 | selftest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc); | |
407 | ||
408 | /* of_property_read_string_index() tests */ | |
409 | rc = of_property_read_string_index(np, "string-property", 0, strings); | |
410 | selftest(rc == 0 && !strcmp(strings[0], "foobar"), "of_property_read_string_index() failure; rc=%i\n", rc); | |
411 | strings[0] = NULL; | |
412 | rc = of_property_read_string_index(np, "string-property", 1, strings); | |
413 | selftest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc); | |
414 | rc = of_property_read_string_index(np, "phandle-list-names", 0, strings); | |
415 | selftest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc); | |
416 | rc = of_property_read_string_index(np, "phandle-list-names", 1, strings); | |
417 | selftest(rc == 0 && !strcmp(strings[0], "second"), "of_property_read_string_index() failure; rc=%i\n", rc); | |
418 | rc = of_property_read_string_index(np, "phandle-list-names", 2, strings); | |
419 | selftest(rc == 0 && !strcmp(strings[0], "third"), "of_property_read_string_index() failure; rc=%i\n", rc); | |
420 | strings[0] = NULL; | |
421 | rc = of_property_read_string_index(np, "phandle-list-names", 3, strings); | |
422 | selftest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc); | |
423 | strings[0] = NULL; | |
424 | rc = of_property_read_string_index(np, "unterminated-string", 0, strings); | |
425 | selftest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc); | |
426 | rc = of_property_read_string_index(np, "unterminated-string-list", 0, strings); | |
427 | selftest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc); | |
428 | strings[0] = NULL; | |
429 | rc = of_property_read_string_index(np, "unterminated-string-list", 2, strings); /* should fail */ | |
430 | selftest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc); | |
431 | strings[1] = NULL; | |
432 | ||
433 | /* of_property_read_string_array() tests */ | |
434 | rc = of_property_read_string_array(np, "string-property", strings, 4); | |
435 | selftest(rc == 1, "Incorrect string count; rc=%i\n", rc); | |
436 | rc = of_property_read_string_array(np, "phandle-list-names", strings, 4); | |
437 | selftest(rc == 3, "Incorrect string count; rc=%i\n", rc); | |
438 | rc = of_property_read_string_array(np, "unterminated-string", strings, 4); | |
439 | selftest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc); | |
440 | /* -- An incorrectly formed string should cause a failure */ | |
441 | rc = of_property_read_string_array(np, "unterminated-string-list", strings, 4); | |
442 | selftest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc); | |
443 | /* -- parsing the correctly formed strings should still work: */ | |
444 | strings[2] = NULL; | |
445 | rc = of_property_read_string_array(np, "unterminated-string-list", strings, 2); | |
446 | selftest(rc == 2 && strings[2] == NULL, "of_property_read_string_array() failure; rc=%i\n", rc); | |
447 | strings[1] = NULL; | |
448 | rc = of_property_read_string_array(np, "phandle-list-names", strings, 1); | |
449 | selftest(rc == 1 && strings[1] == NULL, "Overwrote end of string array; rc=%i, str='%s'\n", rc, strings[1]); | |
7aff0fe3 GL |
450 | } |
451 | ||
69843396 PA |
452 | #define propcmp(p1, p2) (((p1)->length == (p2)->length) && \ |
453 | (p1)->value && (p2)->value && \ | |
454 | !memcmp((p1)->value, (p2)->value, (p1)->length) && \ | |
455 | !strcmp((p1)->name, (p2)->name)) | |
456 | static void __init of_selftest_property_copy(void) | |
457 | { | |
458 | #ifdef CONFIG_OF_DYNAMIC | |
459 | struct property p1 = { .name = "p1", .length = 0, .value = "" }; | |
460 | struct property p2 = { .name = "p2", .length = 5, .value = "abcd" }; | |
461 | struct property *new; | |
462 | ||
463 | new = __of_prop_dup(&p1, GFP_KERNEL); | |
464 | selftest(new && propcmp(&p1, new), "empty property didn't copy correctly\n"); | |
465 | kfree(new->value); | |
466 | kfree(new->name); | |
467 | kfree(new); | |
468 | ||
469 | new = __of_prop_dup(&p2, GFP_KERNEL); | |
470 | selftest(new && propcmp(&p2, new), "non-empty property didn't copy correctly\n"); | |
471 | kfree(new->value); | |
472 | kfree(new->name); | |
473 | kfree(new); | |
474 | #endif | |
475 | } | |
476 | ||
201c910b PA |
477 | static void __init of_selftest_changeset(void) |
478 | { | |
479 | #ifdef CONFIG_OF_DYNAMIC | |
480 | struct property *ppadd, padd = { .name = "prop-add", .length = 0, .value = "" }; | |
481 | struct property *ppupdate, pupdate = { .name = "prop-update", .length = 5, .value = "abcd" }; | |
482 | struct property *ppremove; | |
e5179581 | 483 | struct device_node *n1, *n2, *n21, *nremove, *parent, *np; |
201c910b PA |
484 | struct of_changeset chgset; |
485 | ||
486 | of_changeset_init(&chgset); | |
e5179581 | 487 | n1 = __of_node_dup(NULL, "/testcase-data/changeset/n1"); |
201c910b | 488 | selftest(n1, "testcase setup failure\n"); |
e5179581 | 489 | n2 = __of_node_dup(NULL, "/testcase-data/changeset/n2"); |
201c910b | 490 | selftest(n2, "testcase setup failure\n"); |
e5179581 | 491 | n21 = __of_node_dup(NULL, "%s/%s", "/testcase-data/changeset/n2", "n21"); |
201c910b PA |
492 | selftest(n21, "testcase setup failure %p\n", n21); |
493 | nremove = of_find_node_by_path("/testcase-data/changeset/node-remove"); | |
494 | selftest(nremove, "testcase setup failure\n"); | |
495 | ppadd = __of_prop_dup(&padd, GFP_KERNEL); | |
496 | selftest(ppadd, "testcase setup failure\n"); | |
497 | ppupdate = __of_prop_dup(&pupdate, GFP_KERNEL); | |
498 | selftest(ppupdate, "testcase setup failure\n"); | |
499 | parent = nremove->parent; | |
500 | n1->parent = parent; | |
501 | n2->parent = parent; | |
502 | n21->parent = n2; | |
503 | n2->child = n21; | |
504 | ppremove = of_find_property(parent, "prop-remove", NULL); | |
505 | selftest(ppremove, "failed to find removal prop"); | |
506 | ||
507 | of_changeset_init(&chgset); | |
508 | selftest(!of_changeset_attach_node(&chgset, n1), "fail attach n1\n"); | |
509 | selftest(!of_changeset_attach_node(&chgset, n2), "fail attach n2\n"); | |
510 | selftest(!of_changeset_detach_node(&chgset, nremove), "fail remove node\n"); | |
511 | selftest(!of_changeset_attach_node(&chgset, n21), "fail attach n21\n"); | |
512 | selftest(!of_changeset_add_property(&chgset, parent, ppadd), "fail add prop\n"); | |
513 | selftest(!of_changeset_update_property(&chgset, parent, ppupdate), "fail update prop\n"); | |
514 | selftest(!of_changeset_remove_property(&chgset, parent, ppremove), "fail remove prop\n"); | |
515 | mutex_lock(&of_mutex); | |
516 | selftest(!of_changeset_apply(&chgset), "apply failed\n"); | |
517 | mutex_unlock(&of_mutex); | |
518 | ||
e5179581 GL |
519 | /* Make sure node names are constructed correctly */ |
520 | selftest((np = of_find_node_by_path("/testcase-data/changeset/n2/n21")), | |
521 | "'%s' not added\n", n21->full_name); | |
c46ca3c8 | 522 | of_node_put(np); |
e5179581 | 523 | |
201c910b PA |
524 | mutex_lock(&of_mutex); |
525 | selftest(!of_changeset_revert(&chgset), "revert failed\n"); | |
526 | mutex_unlock(&of_mutex); | |
527 | ||
528 | of_changeset_destroy(&chgset); | |
529 | #endif | |
530 | } | |
531 | ||
a9f10ca7 GL |
532 | static void __init of_selftest_parse_interrupts(void) |
533 | { | |
534 | struct device_node *np; | |
535 | struct of_phandle_args args; | |
536 | int i, rc; | |
537 | ||
538 | np = of_find_node_by_path("/testcase-data/interrupts/interrupts0"); | |
539 | if (!np) { | |
540 | pr_err("missing testcase data\n"); | |
541 | return; | |
542 | } | |
543 | ||
544 | for (i = 0; i < 4; i++) { | |
545 | bool passed = true; | |
546 | args.args_count = 0; | |
547 | rc = of_irq_parse_one(np, i, &args); | |
548 | ||
549 | passed &= !rc; | |
550 | passed &= (args.args_count == 1); | |
551 | passed &= (args.args[0] == (i + 1)); | |
552 | ||
553 | selftest(passed, "index %i - data error on node %s rc=%i\n", | |
554 | i, args.np->full_name, rc); | |
555 | } | |
556 | of_node_put(np); | |
557 | ||
558 | np = of_find_node_by_path("/testcase-data/interrupts/interrupts1"); | |
559 | if (!np) { | |
560 | pr_err("missing testcase data\n"); | |
561 | return; | |
562 | } | |
563 | ||
564 | for (i = 0; i < 4; i++) { | |
565 | bool passed = true; | |
566 | args.args_count = 0; | |
567 | rc = of_irq_parse_one(np, i, &args); | |
568 | ||
569 | /* Test the values from tests-phandle.dtsi */ | |
570 | switch (i) { | |
571 | case 0: | |
572 | passed &= !rc; | |
573 | passed &= (args.args_count == 1); | |
574 | passed &= (args.args[0] == 9); | |
575 | break; | |
576 | case 1: | |
577 | passed &= !rc; | |
578 | passed &= (args.args_count == 3); | |
579 | passed &= (args.args[0] == 10); | |
580 | passed &= (args.args[1] == 11); | |
581 | passed &= (args.args[2] == 12); | |
582 | break; | |
583 | case 2: | |
584 | passed &= !rc; | |
585 | passed &= (args.args_count == 2); | |
586 | passed &= (args.args[0] == 13); | |
587 | passed &= (args.args[1] == 14); | |
588 | break; | |
589 | case 3: | |
590 | passed &= !rc; | |
591 | passed &= (args.args_count == 2); | |
592 | passed &= (args.args[0] == 15); | |
593 | passed &= (args.args[1] == 16); | |
594 | break; | |
595 | default: | |
596 | passed = false; | |
597 | } | |
598 | selftest(passed, "index %i - data error on node %s rc=%i\n", | |
599 | i, args.np->full_name, rc); | |
600 | } | |
601 | of_node_put(np); | |
602 | } | |
603 | ||
79d97015 GL |
604 | static void __init of_selftest_parse_interrupts_extended(void) |
605 | { | |
606 | struct device_node *np; | |
607 | struct of_phandle_args args; | |
608 | int i, rc; | |
609 | ||
610 | np = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0"); | |
611 | if (!np) { | |
612 | pr_err("missing testcase data\n"); | |
613 | return; | |
614 | } | |
615 | ||
616 | for (i = 0; i < 7; i++) { | |
617 | bool passed = true; | |
618 | rc = of_irq_parse_one(np, i, &args); | |
619 | ||
620 | /* Test the values from tests-phandle.dtsi */ | |
621 | switch (i) { | |
622 | case 0: | |
623 | passed &= !rc; | |
624 | passed &= (args.args_count == 1); | |
625 | passed &= (args.args[0] == 1); | |
626 | break; | |
627 | case 1: | |
628 | passed &= !rc; | |
629 | passed &= (args.args_count == 3); | |
630 | passed &= (args.args[0] == 2); | |
631 | passed &= (args.args[1] == 3); | |
632 | passed &= (args.args[2] == 4); | |
633 | break; | |
634 | case 2: | |
635 | passed &= !rc; | |
636 | passed &= (args.args_count == 2); | |
637 | passed &= (args.args[0] == 5); | |
638 | passed &= (args.args[1] == 6); | |
639 | break; | |
640 | case 3: | |
641 | passed &= !rc; | |
642 | passed &= (args.args_count == 1); | |
643 | passed &= (args.args[0] == 9); | |
644 | break; | |
645 | case 4: | |
646 | passed &= !rc; | |
647 | passed &= (args.args_count == 3); | |
648 | passed &= (args.args[0] == 10); | |
649 | passed &= (args.args[1] == 11); | |
650 | passed &= (args.args[2] == 12); | |
651 | break; | |
652 | case 5: | |
653 | passed &= !rc; | |
654 | passed &= (args.args_count == 2); | |
655 | passed &= (args.args[0] == 13); | |
656 | passed &= (args.args[1] == 14); | |
657 | break; | |
658 | case 6: | |
659 | passed &= !rc; | |
660 | passed &= (args.args_count == 1); | |
661 | passed &= (args.args[0] == 15); | |
662 | break; | |
663 | default: | |
664 | passed = false; | |
665 | } | |
666 | ||
667 | selftest(passed, "index %i - data error on node %s rc=%i\n", | |
668 | i, args.np->full_name, rc); | |
669 | } | |
670 | of_node_put(np); | |
671 | } | |
672 | ||
1f42e5dd GL |
673 | static struct of_device_id match_node_table[] = { |
674 | { .data = "A", .name = "name0", }, /* Name alone is lowest priority */ | |
675 | { .data = "B", .type = "type1", }, /* followed by type alone */ | |
676 | ||
677 | { .data = "Ca", .name = "name2", .type = "type1", }, /* followed by both together */ | |
678 | { .data = "Cb", .name = "name2", }, /* Only match when type doesn't match */ | |
679 | { .data = "Cc", .name = "name2", .type = "type2", }, | |
680 | ||
681 | { .data = "E", .compatible = "compat3" }, | |
682 | { .data = "G", .compatible = "compat2", }, | |
683 | { .data = "H", .compatible = "compat2", .name = "name5", }, | |
684 | { .data = "I", .compatible = "compat2", .type = "type1", }, | |
685 | { .data = "J", .compatible = "compat2", .type = "type1", .name = "name8", }, | |
686 | { .data = "K", .compatible = "compat2", .name = "name9", }, | |
687 | {} | |
688 | }; | |
689 | ||
690 | static struct { | |
691 | const char *path; | |
692 | const char *data; | |
693 | } match_node_tests[] = { | |
694 | { .path = "/testcase-data/match-node/name0", .data = "A", }, | |
695 | { .path = "/testcase-data/match-node/name1", .data = "B", }, | |
696 | { .path = "/testcase-data/match-node/a/name2", .data = "Ca", }, | |
697 | { .path = "/testcase-data/match-node/b/name2", .data = "Cb", }, | |
698 | { .path = "/testcase-data/match-node/c/name2", .data = "Cc", }, | |
699 | { .path = "/testcase-data/match-node/name3", .data = "E", }, | |
700 | { .path = "/testcase-data/match-node/name4", .data = "G", }, | |
701 | { .path = "/testcase-data/match-node/name5", .data = "H", }, | |
702 | { .path = "/testcase-data/match-node/name6", .data = "G", }, | |
703 | { .path = "/testcase-data/match-node/name7", .data = "I", }, | |
704 | { .path = "/testcase-data/match-node/name8", .data = "J", }, | |
705 | { .path = "/testcase-data/match-node/name9", .data = "K", }, | |
706 | }; | |
707 | ||
708 | static void __init of_selftest_match_node(void) | |
709 | { | |
710 | struct device_node *np; | |
711 | const struct of_device_id *match; | |
712 | int i; | |
713 | ||
714 | for (i = 0; i < ARRAY_SIZE(match_node_tests); i++) { | |
715 | np = of_find_node_by_path(match_node_tests[i].path); | |
716 | if (!np) { | |
717 | selftest(0, "missing testcase node %s\n", | |
718 | match_node_tests[i].path); | |
719 | continue; | |
720 | } | |
721 | ||
722 | match = of_match_node(match_node_table, np); | |
723 | if (!match) { | |
724 | selftest(0, "%s didn't match anything\n", | |
725 | match_node_tests[i].path); | |
726 | continue; | |
727 | } | |
728 | ||
729 | if (strcmp(match->data, match_node_tests[i].data) != 0) { | |
730 | selftest(0, "%s got wrong match. expected %s, got %s\n", | |
731 | match_node_tests[i].path, match_node_tests[i].data, | |
732 | (const char *)match->data); | |
733 | continue; | |
734 | } | |
735 | selftest(1, "passed"); | |
736 | } | |
737 | } | |
738 | ||
851da976 GL |
739 | struct device test_bus = { |
740 | .init_name = "unittest-bus", | |
741 | }; | |
82c0f589 RH |
742 | static void __init of_selftest_platform_populate(void) |
743 | { | |
851da976 GL |
744 | int irq, rc; |
745 | struct device_node *np, *child, *grandchild; | |
82c0f589 | 746 | struct platform_device *pdev; |
fb2caa50 RH |
747 | struct of_device_id match[] = { |
748 | { .compatible = "test-device", }, | |
749 | {} | |
750 | }; | |
82c0f589 RH |
751 | |
752 | np = of_find_node_by_path("/testcase-data"); | |
753 | of_platform_populate(np, of_default_bus_match_table, NULL, NULL); | |
754 | ||
755 | /* Test that a missing irq domain returns -EPROBE_DEFER */ | |
756 | np = of_find_node_by_path("/testcase-data/testcase-device1"); | |
757 | pdev = of_find_device_by_node(np); | |
7d1cdc89 RH |
758 | selftest(pdev, "device 1 creation failed\n"); |
759 | ||
82c0f589 | 760 | irq = platform_get_irq(pdev, 0); |
7d1cdc89 | 761 | selftest(irq == -EPROBE_DEFER, "device deferred probe failed - %d\n", irq); |
82c0f589 RH |
762 | |
763 | /* Test that a parsing failure does not return -EPROBE_DEFER */ | |
764 | np = of_find_node_by_path("/testcase-data/testcase-device2"); | |
765 | pdev = of_find_device_by_node(np); | |
7d1cdc89 | 766 | selftest(pdev, "device 2 creation failed\n"); |
82c0f589 | 767 | irq = platform_get_irq(pdev, 0); |
7d1cdc89 | 768 | selftest(irq < 0 && irq != -EPROBE_DEFER, "device parsing error failed - %d\n", irq); |
82c0f589 | 769 | |
851da976 GL |
770 | if (selftest(np = of_find_node_by_path("/testcase-data/platform-tests"), |
771 | "No testcase data in device tree\n")); | |
772 | return; | |
773 | ||
774 | if (selftest(!(rc = device_register(&test_bus)), | |
775 | "testbus registration failed; rc=%i\n", rc)); | |
fb2caa50 | 776 | return; |
fb2caa50 RH |
777 | |
778 | for_each_child_of_node(np, child) { | |
851da976 | 779 | of_platform_populate(child, match, NULL, &test_bus); |
fb2caa50 RH |
780 | for_each_child_of_node(child, grandchild) |
781 | selftest(of_find_device_by_node(grandchild), | |
782 | "Could not create device for node '%s'\n", | |
783 | grandchild->name); | |
784 | } | |
851da976 GL |
785 | |
786 | of_platform_depopulate(&test_bus); | |
787 | for_each_child_of_node(np, child) { | |
788 | for_each_child_of_node(child, grandchild) | |
789 | selftest(!of_find_device_by_node(grandchild), | |
790 | "device didn't get destroyed '%s'\n", | |
791 | grandchild->name); | |
792 | } | |
793 | ||
794 | device_unregister(&test_bus); | |
795 | of_node_put(np); | |
82c0f589 RH |
796 | } |
797 | ||
ae9304c9 GM |
798 | /** |
799 | * update_node_properties - adds the properties | |
800 | * of np into dup node (present in live tree) and | |
801 | * updates parent of children of np to dup. | |
802 | * | |
803 | * @np: node already present in live tree | |
804 | * @dup: node present in live tree to be updated | |
805 | */ | |
806 | static void update_node_properties(struct device_node *np, | |
807 | struct device_node *dup) | |
808 | { | |
809 | struct property *prop; | |
810 | struct device_node *child; | |
811 | ||
812 | for_each_property_of_node(np, prop) | |
813 | of_add_property(dup, prop); | |
814 | ||
815 | for_each_child_of_node(np, child) | |
816 | child->parent = dup; | |
817 | } | |
818 | ||
819 | /** | |
820 | * attach_node_and_children - attaches nodes | |
821 | * and its children to live tree | |
822 | * | |
823 | * @np: Node to attach to live tree | |
824 | */ | |
825 | static int attach_node_and_children(struct device_node *np) | |
826 | { | |
5063e25a | 827 | struct device_node *next, *dup, *child; |
3ce04b4a | 828 | unsigned long flags; |
ae9304c9 | 829 | |
5063e25a GL |
830 | dup = of_find_node_by_path(np->full_name); |
831 | if (dup) { | |
832 | update_node_properties(np, dup); | |
833 | return 0; | |
834 | } | |
ae9304c9 | 835 | |
5063e25a GL |
836 | /* Children of the root need to be remembered for removal */ |
837 | if (np->parent == of_root) { | |
e66c98c7 GL |
838 | if (WARN_ON(last_node_index >= NO_OF_NODES)) |
839 | return -EINVAL; | |
5063e25a | 840 | nodes[last_node_index++] = np; |
ae9304c9 | 841 | } |
ae9304c9 | 842 | |
5063e25a GL |
843 | child = np->child; |
844 | np->child = NULL; | |
3ce04b4a GM |
845 | |
846 | mutex_lock(&of_mutex); | |
847 | raw_spin_lock_irqsave(&devtree_lock, flags); | |
848 | np->sibling = np->parent->child; | |
849 | np->parent->child = np; | |
850 | of_node_clear_flag(np, OF_DETACHED); | |
851 | raw_spin_unlock_irqrestore(&devtree_lock, flags); | |
852 | ||
853 | __of_attach_node_sysfs(np); | |
854 | mutex_unlock(&of_mutex); | |
855 | ||
5063e25a GL |
856 | while (child) { |
857 | next = child->sibling; | |
858 | attach_node_and_children(child); | |
859 | child = next; | |
ae9304c9 GM |
860 | } |
861 | ||
862 | return 0; | |
863 | } | |
864 | ||
865 | /** | |
866 | * selftest_data_add - Reads, copies data from | |
867 | * linked tree and attaches it to the live tree | |
868 | */ | |
869 | static int __init selftest_data_add(void) | |
870 | { | |
871 | void *selftest_data; | |
b951f9dc | 872 | struct device_node *selftest_data_node, *np; |
ae9304c9 GM |
873 | extern uint8_t __dtb_testcases_begin[]; |
874 | extern uint8_t __dtb_testcases_end[]; | |
875 | const int size = __dtb_testcases_end - __dtb_testcases_begin; | |
2eb46da2 | 876 | int rc; |
ae9304c9 | 877 | |
b951f9dc | 878 | if (!size) { |
ae9304c9 GM |
879 | pr_warn("%s: No testcase data to attach; not running tests\n", |
880 | __func__); | |
881 | return -ENODATA; | |
882 | } | |
883 | ||
884 | /* creating copy */ | |
885 | selftest_data = kmemdup(__dtb_testcases_begin, size, GFP_KERNEL); | |
886 | ||
887 | if (!selftest_data) { | |
888 | pr_warn("%s: Failed to allocate memory for selftest_data; " | |
889 | "not running tests\n", __func__); | |
890 | return -ENOMEM; | |
891 | } | |
892 | of_fdt_unflatten_tree(selftest_data, &selftest_data_node); | |
b951f9dc GM |
893 | if (!selftest_data_node) { |
894 | pr_warn("%s: No tree to attach; not running tests\n", __func__); | |
895 | return -ENODATA; | |
896 | } | |
2eb46da2 GL |
897 | of_node_set_flag(selftest_data_node, OF_DETACHED); |
898 | rc = of_resolve_phandles(selftest_data_node); | |
899 | if (rc) { | |
900 | pr_err("%s: Failed to resolve phandles (rc=%i)\n", __func__, rc); | |
901 | return -EINVAL; | |
902 | } | |
b951f9dc | 903 | |
5063e25a | 904 | if (!of_root) { |
b951f9dc GM |
905 | /* enabling flag for removing nodes */ |
906 | selftest_live_tree = true; | |
5063e25a | 907 | of_root = selftest_data_node; |
b951f9dc GM |
908 | |
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 */ | |
5063e25a GL |
917 | np = selftest_data_node->child; |
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 | ||
929 | static int selftest_probe(struct platform_device *pdev) | |
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 | ||
947 | static int selftest_remove(struct platform_device *pdev) | |
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 | ||
956 | static struct of_device_id selftest_match[] = { | |
957 | { .compatible = "selftest", }, | |
958 | {}, | |
959 | }; | |
177d271c PA |
960 | |
961 | static struct platform_driver selftest_driver = { | |
962 | .probe = selftest_probe, | |
963 | .remove = selftest_remove, | |
964 | .driver = { | |
965 | .name = "selftest", | |
966 | .owner = THIS_MODULE, | |
967 | .of_match_table = of_match_ptr(selftest_match), | |
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 | ||
d5e75500 PA |
997 | #if IS_ENABLED(CONFIG_I2C) |
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 | ||
1049 | static const char *selftest_path(int nr, enum overlay_type ovtype) | |
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 | } | |
1065 | snprintf(buf, sizeof(buf) - 1, "%s/test-selftest%d", base, nr); | |
177d271c | 1066 | buf[sizeof(buf) - 1] = '\0'; |
177d271c PA |
1067 | return buf; |
1068 | } | |
1069 | ||
d5e75500 PA |
1070 | static int of_selftest_device_exists(int selftest_nr, enum overlay_type ovtype) |
1071 | { | |
1072 | const char *path; | |
1073 | ||
1074 | path = selftest_path(selftest_nr, ovtype); | |
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 | ||
1098 | static int of_selftest_apply_overlay(int selftest_nr, int overlay_nr, | |
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) { | |
1106 | selftest(0, "could not find overlay node @\"%s\"\n", | |
1107 | overlay_path(overlay_nr)); | |
1108 | ret = -EINVAL; | |
1109 | goto out; | |
1110 | } | |
1111 | ||
1112 | ret = of_overlay_create(np); | |
1113 | if (ret < 0) { | |
1114 | selftest(0, "could not create overlay from \"%s\"\n", | |
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 */ | |
1132 | static int of_selftest_apply_overlay_check(int overlay_nr, int selftest_nr, | |
d5e75500 | 1133 | int before, int after, enum overlay_type ovtype) |
177d271c PA |
1134 | { |
1135 | int ret; | |
1136 | ||
1137 | /* selftest device must not be in before state */ | |
d5e75500 | 1138 | if (of_selftest_device_exists(selftest_nr, ovtype) != before) { |
177d271c PA |
1139 | selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n", |
1140 | overlay_path(overlay_nr), | |
d5e75500 | 1141 | selftest_path(selftest_nr, ovtype), |
177d271c PA |
1142 | !before ? "enabled" : "disabled"); |
1143 | return -EINVAL; | |
1144 | } | |
1145 | ||
1146 | ret = of_selftest_apply_overlay(overlay_nr, selftest_nr, NULL); | |
1147 | if (ret != 0) { | |
1148 | /* of_selftest_apply_overlay already called selftest() */ | |
1149 | return ret; | |
1150 | } | |
1151 | ||
1152 | /* selftest device must be to set to after state */ | |
d5e75500 | 1153 | if (of_selftest_device_exists(selftest_nr, ovtype) != after) { |
177d271c PA |
1154 | selftest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n", |
1155 | overlay_path(overlay_nr), | |
d5e75500 | 1156 | selftest_path(selftest_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 */ | |
1165 | static int of_selftest_apply_revert_overlay_check(int overlay_nr, | |
d5e75500 PA |
1166 | int selftest_nr, int before, int after, |
1167 | enum overlay_type ovtype) | |
177d271c PA |
1168 | { |
1169 | int ret, ov_id; | |
1170 | ||
1171 | /* selftest device must be in before state */ | |
d5e75500 | 1172 | if (of_selftest_device_exists(selftest_nr, ovtype) != before) { |
177d271c PA |
1173 | selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n", |
1174 | overlay_path(overlay_nr), | |
d5e75500 | 1175 | selftest_path(selftest_nr, ovtype), |
177d271c PA |
1176 | !before ? "enabled" : "disabled"); |
1177 | return -EINVAL; | |
1178 | } | |
1179 | ||
1180 | /* apply the overlay */ | |
1181 | ret = of_selftest_apply_overlay(overlay_nr, selftest_nr, &ov_id); | |
1182 | if (ret != 0) { | |
1183 | /* of_selftest_apply_overlay already called selftest() */ | |
1184 | return ret; | |
1185 | } | |
1186 | ||
1187 | /* selftest device must be in after state */ | |
d5e75500 | 1188 | if (of_selftest_device_exists(selftest_nr, ovtype) != after) { |
177d271c PA |
1189 | selftest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n", |
1190 | overlay_path(overlay_nr), | |
d5e75500 | 1191 | selftest_path(selftest_nr, ovtype), |
177d271c PA |
1192 | !after ? "enabled" : "disabled"); |
1193 | return -EINVAL; | |
1194 | } | |
1195 | ||
1196 | ret = of_overlay_destroy(ov_id); | |
1197 | if (ret != 0) { | |
1198 | selftest(0, "overlay @\"%s\" failed to be destroyed @\"%s\"\n", | |
1199 | overlay_path(overlay_nr), | |
d5e75500 | 1200 | selftest_path(selftest_nr, ovtype)); |
177d271c PA |
1201 | return ret; |
1202 | } | |
1203 | ||
1204 | /* selftest device must be again in before state */ | |
d5e75500 | 1205 | if (of_selftest_device_exists(selftest_nr, PDEV_OVERLAY) != before) { |
177d271c PA |
1206 | selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n", |
1207 | overlay_path(overlay_nr), | |
d5e75500 | 1208 | selftest_path(selftest_nr, ovtype), |
177d271c PA |
1209 | !before ? "enabled" : "disabled"); |
1210 | return -EINVAL; | |
1211 | } | |
1212 | ||
1213 | return 0; | |
1214 | } | |
1215 | ||
1216 | /* test activation of device */ | |
1217 | static void of_selftest_overlay_0(void) | |
1218 | { | |
1219 | int ret; | |
1220 | ||
1221 | /* device should enable */ | |
d5e75500 | 1222 | ret = of_selftest_apply_overlay_check(0, 0, 0, 1, PDEV_OVERLAY); |
177d271c PA |
1223 | if (ret != 0) |
1224 | return; | |
1225 | ||
1226 | selftest(1, "overlay test %d passed\n", 0); | |
1227 | } | |
1228 | ||
1229 | /* test deactivation of device */ | |
1230 | static void of_selftest_overlay_1(void) | |
1231 | { | |
1232 | int ret; | |
1233 | ||
1234 | /* device should disable */ | |
d5e75500 | 1235 | ret = of_selftest_apply_overlay_check(1, 1, 1, 0, PDEV_OVERLAY); |
177d271c PA |
1236 | if (ret != 0) |
1237 | return; | |
1238 | ||
1239 | selftest(1, "overlay test %d passed\n", 1); | |
1240 | } | |
1241 | ||
1242 | /* test activation of device */ | |
1243 | static void of_selftest_overlay_2(void) | |
1244 | { | |
1245 | int ret; | |
1246 | ||
1247 | /* device should enable */ | |
d5e75500 | 1248 | ret = of_selftest_apply_overlay_check(2, 2, 0, 1, PDEV_OVERLAY); |
177d271c PA |
1249 | if (ret != 0) |
1250 | return; | |
1251 | ||
1252 | selftest(1, "overlay test %d passed\n", 2); | |
1253 | } | |
1254 | ||
1255 | /* test deactivation of device */ | |
1256 | static void of_selftest_overlay_3(void) | |
1257 | { | |
1258 | int ret; | |
1259 | ||
1260 | /* device should disable */ | |
d5e75500 | 1261 | ret = of_selftest_apply_overlay_check(3, 3, 1, 0, PDEV_OVERLAY); |
177d271c PA |
1262 | if (ret != 0) |
1263 | return; | |
1264 | ||
1265 | selftest(1, "overlay test %d passed\n", 3); | |
1266 | } | |
1267 | ||
1268 | /* test activation of a full device node */ | |
1269 | static void of_selftest_overlay_4(void) | |
1270 | { | |
1271 | int ret; | |
1272 | ||
1273 | /* device should disable */ | |
d5e75500 | 1274 | ret = of_selftest_apply_overlay_check(4, 4, 0, 1, PDEV_OVERLAY); |
177d271c PA |
1275 | if (ret != 0) |
1276 | return; | |
1277 | ||
1278 | selftest(1, "overlay test %d passed\n", 4); | |
1279 | } | |
1280 | ||
1281 | /* test overlay apply/revert sequence */ | |
1282 | static void of_selftest_overlay_5(void) | |
1283 | { | |
1284 | int ret; | |
1285 | ||
1286 | /* device should disable */ | |
d5e75500 | 1287 | ret = of_selftest_apply_revert_overlay_check(5, 5, 0, 1, PDEV_OVERLAY); |
177d271c PA |
1288 | if (ret != 0) |
1289 | return; | |
1290 | ||
1291 | selftest(1, "overlay test %d passed\n", 5); | |
1292 | } | |
1293 | ||
1294 | /* test overlay application in sequence */ | |
1295 | static void of_selftest_overlay_6(void) | |
1296 | { | |
1297 | struct device_node *np; | |
1298 | int ret, i, ov_id[2]; | |
1299 | int overlay_nr = 6, selftest_nr = 6; | |
1300 | int before = 0, after = 1; | |
1301 | ||
1302 | /* selftest device must be in before state */ | |
1303 | for (i = 0; i < 2; i++) { | |
d5e75500 | 1304 | if (of_selftest_device_exists(selftest_nr + i, PDEV_OVERLAY) |
177d271c PA |
1305 | != before) { |
1306 | selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n", | |
1307 | overlay_path(overlay_nr + i), | |
d5e75500 PA |
1308 | selftest_path(selftest_nr + i, |
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) { | |
1320 | selftest(0, "could not find overlay node @\"%s\"\n", | |
1321 | overlay_path(overlay_nr + i)); | |
1322 | return; | |
1323 | } | |
1324 | ||
1325 | ret = of_overlay_create(np); | |
1326 | if (ret < 0) { | |
1327 | selftest(0, "could not create overlay from \"%s\"\n", | |
1328 | overlay_path(overlay_nr + i)); | |
1329 | return; | |
1330 | } | |
1331 | ov_id[i] = ret; | |
1332 | } | |
1333 | ||
1334 | for (i = 0; i < 2; i++) { | |
1335 | /* selftest device must be in after state */ | |
d5e75500 | 1336 | if (of_selftest_device_exists(selftest_nr + i, PDEV_OVERLAY) |
177d271c PA |
1337 | != after) { |
1338 | selftest(0, "overlay @\"%s\" failed @\"%s\" %s\n", | |
1339 | overlay_path(overlay_nr + i), | |
d5e75500 PA |
1340 | selftest_path(selftest_nr + i, |
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) { | |
1350 | selftest(0, "overlay @\"%s\" failed destroy @\"%s\"\n", | |
1351 | overlay_path(overlay_nr + i), | |
d5e75500 PA |
1352 | selftest_path(selftest_nr + i, |
1353 | PDEV_OVERLAY)); | |
177d271c PA |
1354 | return; |
1355 | } | |
1356 | } | |
1357 | ||
1358 | for (i = 0; i < 2; i++) { | |
1359 | /* selftest device must be again in before state */ | |
d5e75500 | 1360 | if (of_selftest_device_exists(selftest_nr + i, PDEV_OVERLAY) |
177d271c PA |
1361 | != before) { |
1362 | selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n", | |
1363 | overlay_path(overlay_nr + i), | |
d5e75500 PA |
1364 | selftest_path(selftest_nr + i, |
1365 | PDEV_OVERLAY), | |
177d271c PA |
1366 | !before ? "enabled" : "disabled"); |
1367 | return; | |
1368 | } | |
1369 | } | |
1370 | ||
1371 | selftest(1, "overlay test %d passed\n", 6); | |
1372 | } | |
1373 | ||
1374 | /* test overlay application in sequence */ | |
1375 | static void of_selftest_overlay_8(void) | |
1376 | { | |
1377 | struct device_node *np; | |
1378 | int ret, i, ov_id[2]; | |
1379 | int overlay_nr = 8, selftest_nr = 8; | |
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) { | |
1388 | selftest(0, "could not find overlay node @\"%s\"\n", | |
1389 | overlay_path(overlay_nr + i)); | |
1390 | return; | |
1391 | } | |
1392 | ||
1393 | ret = of_overlay_create(np); | |
1394 | if (ret < 0) { | |
1395 | selftest(0, "could not create overlay from \"%s\"\n", | |
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) { | |
1405 | selftest(0, "overlay @\"%s\" was destroyed @\"%s\"\n", | |
1406 | overlay_path(overlay_nr + 0), | |
d5e75500 PA |
1407 | selftest_path(selftest_nr, |
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) { | |
1416 | selftest(0, "overlay @\"%s\" not destroyed @\"%s\"\n", | |
1417 | overlay_path(overlay_nr + i), | |
d5e75500 PA |
1418 | selftest_path(selftest_nr, |
1419 | PDEV_OVERLAY)); | |
177d271c PA |
1420 | return; |
1421 | } | |
1422 | } | |
1423 | ||
1424 | selftest(1, "overlay test %d passed\n", 8); | |
1425 | } | |
1426 | ||
6b1271de PA |
1427 | /* test insertion of a bus with parent devices */ |
1428 | static void of_selftest_overlay_10(void) | |
1429 | { | |
1430 | int ret; | |
1431 | char *child_path; | |
1432 | ||
1433 | /* device should disable */ | |
d5e75500 PA |
1434 | ret = of_selftest_apply_overlay_check(10, 10, 0, 1, PDEV_OVERLAY); |
1435 | if (selftest(ret == 0, | |
1436 | "overlay test %d failed; overlay application\n", 10)) | |
6b1271de PA |
1437 | return; |
1438 | ||
1439 | child_path = kasprintf(GFP_KERNEL, "%s/test-selftest101", | |
d5e75500 | 1440 | selftest_path(10, PDEV_OVERLAY)); |
6b1271de PA |
1441 | if (selftest(child_path, "overlay test %d failed; kasprintf\n", 10)) |
1442 | return; | |
1443 | ||
d5e75500 | 1444 | ret = of_path_device_type_exists(child_path, PDEV_OVERLAY); |
6b1271de PA |
1445 | kfree(child_path); |
1446 | if (selftest(ret, "overlay test %d failed; no child device\n", 10)) | |
1447 | return; | |
1448 | } | |
1449 | ||
1450 | /* test insertion of a bus with parent devices (and revert) */ | |
1451 | static void of_selftest_overlay_11(void) | |
1452 | { | |
1453 | int ret; | |
1454 | ||
1455 | /* device should disable */ | |
d5e75500 PA |
1456 | ret = of_selftest_apply_revert_overlay_check(11, 11, 0, 1, |
1457 | PDEV_OVERLAY); | |
1458 | if (selftest(ret == 0, | |
1459 | "overlay test %d failed; overlay application\n", 11)) | |
1460 | return; | |
1461 | } | |
1462 | ||
1463 | #if IS_ENABLED(CONFIG_I2C) && IS_ENABLED(CONFIG_OF_OVERLAY) | |
1464 | ||
1465 | struct selftest_i2c_bus_data { | |
1466 | struct platform_device *pdev; | |
1467 | struct i2c_adapter adap; | |
1468 | }; | |
1469 | ||
1470 | static int selftest_i2c_master_xfer(struct i2c_adapter *adap, | |
1471 | struct i2c_msg *msgs, int num) | |
1472 | { | |
1473 | struct selftest_i2c_bus_data *std = i2c_get_adapdata(adap); | |
1474 | ||
1475 | (void)std; | |
1476 | ||
1477 | return num; | |
1478 | } | |
1479 | ||
1480 | static u32 selftest_i2c_functionality(struct i2c_adapter *adap) | |
1481 | { | |
1482 | return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL; | |
1483 | } | |
1484 | ||
1485 | static const struct i2c_algorithm selftest_i2c_algo = { | |
1486 | .master_xfer = selftest_i2c_master_xfer, | |
1487 | .functionality = selftest_i2c_functionality, | |
1488 | }; | |
1489 | ||
1490 | static int selftest_i2c_bus_probe(struct platform_device *pdev) | |
1491 | { | |
1492 | struct device *dev = &pdev->dev; | |
1493 | struct device_node *np = dev->of_node; | |
1494 | struct selftest_i2c_bus_data *std; | |
1495 | struct i2c_adapter *adap; | |
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) { | |
1508 | dev_err(dev, "Failed to allocate selftest i2c data\n"); | |
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; | |
1521 | adap->algo = &selftest_i2c_algo; | |
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 | ||
1536 | static int selftest_i2c_bus_remove(struct platform_device *pdev) | |
1537 | { | |
1538 | struct device *dev = &pdev->dev; | |
1539 | struct device_node *np = dev->of_node; | |
1540 | struct selftest_i2c_bus_data *std = platform_get_drvdata(pdev); | |
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 | ||
1548 | static struct of_device_id selftest_i2c_bus_match[] = { | |
1549 | { .compatible = "selftest-i2c-bus", }, | |
1550 | {}, | |
1551 | }; | |
1552 | ||
1553 | static struct platform_driver selftest_i2c_bus_driver = { | |
1554 | .probe = selftest_i2c_bus_probe, | |
1555 | .remove = selftest_i2c_bus_remove, | |
1556 | .driver = { | |
1557 | .name = "selftest-i2c-bus", | |
1558 | .of_match_table = of_match_ptr(selftest_i2c_bus_match), | |
1559 | }, | |
1560 | }; | |
1561 | ||
1562 | static int selftest_i2c_dev_probe(struct i2c_client *client, | |
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 | ||
1578 | static int selftest_i2c_dev_remove(struct i2c_client *client) | |
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 | ||
1587 | static const struct i2c_device_id selftest_i2c_dev_id[] = { | |
1588 | { .name = "selftest-i2c-dev" }, | |
1589 | { } | |
1590 | }; | |
1591 | ||
1592 | static struct i2c_driver selftest_i2c_dev_driver = { | |
1593 | .driver = { | |
1594 | .name = "selftest-i2c-dev", | |
1595 | .owner = THIS_MODULE, | |
1596 | }, | |
1597 | .probe = selftest_i2c_dev_probe, | |
1598 | .remove = selftest_i2c_dev_remove, | |
1599 | .id_table = selftest_i2c_dev_id, | |
1600 | }; | |
1601 | ||
1602 | #if IS_ENABLED(CONFIG_I2C_MUX) | |
1603 | ||
1604 | struct selftest_i2c_mux_data { | |
1605 | int nchans; | |
1606 | struct i2c_adapter *adap[]; | |
1607 | }; | |
1608 | ||
1609 | static int selftest_i2c_mux_select_chan(struct i2c_adapter *adap, | |
1610 | void *client, u32 chan) | |
1611 | { | |
1612 | return 0; | |
1613 | } | |
1614 | ||
1615 | static int selftest_i2c_mux_probe(struct i2c_client *client, | |
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; | |
1622 | struct selftest_i2c_mux_data *stm; | |
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 | ||
1646 | size = offsetof(struct selftest_i2c_mux_data, adap[nchans]); | |
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, | |
1655 | 0, i, 0, selftest_i2c_mux_select_chan, NULL); | |
1656 | if (!stm->adap[i]) { | |
1657 | dev_err(dev, "Failed to register mux #%d\n", i); | |
1658 | for (i--; i >= 0; i--) | |
1659 | i2c_del_mux_adapter(stm->adap[i]); | |
1660 | return -ENODEV; | |
1661 | } | |
1662 | } | |
1663 | ||
1664 | i2c_set_clientdata(client, stm); | |
1665 | ||
1666 | return 0; | |
1667 | }; | |
1668 | ||
1669 | static int selftest_i2c_mux_remove(struct i2c_client *client) | |
1670 | { | |
1671 | struct device *dev = &client->dev; | |
1672 | struct device_node *np = client->dev.of_node; | |
1673 | struct selftest_i2c_mux_data *stm = i2c_get_clientdata(client); | |
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 | ||
1682 | static const struct i2c_device_id selftest_i2c_mux_id[] = { | |
1683 | { .name = "selftest-i2c-mux" }, | |
1684 | { } | |
1685 | }; | |
1686 | ||
1687 | static struct i2c_driver selftest_i2c_mux_driver = { | |
1688 | .driver = { | |
1689 | .name = "selftest-i2c-mux", | |
1690 | .owner = THIS_MODULE, | |
1691 | }, | |
1692 | .probe = selftest_i2c_mux_probe, | |
1693 | .remove = selftest_i2c_mux_remove, | |
1694 | .id_table = selftest_i2c_mux_id, | |
1695 | }; | |
1696 | ||
1697 | #endif | |
1698 | ||
1699 | static int of_selftest_overlay_i2c_init(void) | |
1700 | { | |
1701 | int ret; | |
1702 | ||
1703 | ret = i2c_add_driver(&selftest_i2c_dev_driver); | |
1704 | if (selftest(ret == 0, | |
1705 | "could not register selftest i2c device driver\n")) | |
1706 | return ret; | |
1707 | ||
1708 | ret = platform_driver_register(&selftest_i2c_bus_driver); | |
1709 | if (selftest(ret == 0, | |
1710 | "could not register selftest i2c bus driver\n")) | |
1711 | return ret; | |
1712 | ||
1713 | #if IS_ENABLED(CONFIG_I2C_MUX) | |
1714 | ret = i2c_add_driver(&selftest_i2c_mux_driver); | |
1715 | if (selftest(ret == 0, | |
1716 | "could not register selftest i2c mux driver\n")) | |
1717 | return ret; | |
1718 | #endif | |
1719 | ||
1720 | return 0; | |
1721 | } | |
1722 | ||
1723 | static void of_selftest_overlay_i2c_cleanup(void) | |
1724 | { | |
1725 | #if IS_ENABLED(CONFIG_I2C_MUX) | |
1726 | i2c_del_driver(&selftest_i2c_mux_driver); | |
1727 | #endif | |
1728 | platform_driver_unregister(&selftest_i2c_bus_driver); | |
1729 | i2c_del_driver(&selftest_i2c_dev_driver); | |
1730 | } | |
1731 | ||
1732 | static void of_selftest_overlay_i2c_12(void) | |
1733 | { | |
1734 | int ret; | |
1735 | ||
1736 | /* device should enable */ | |
1737 | ret = of_selftest_apply_overlay_check(12, 12, 0, 1, I2C_OVERLAY); | |
1738 | if (ret != 0) | |
1739 | return; | |
1740 | ||
1741 | selftest(1, "overlay test %d passed\n", 12); | |
1742 | } | |
1743 | ||
1744 | /* test deactivation of device */ | |
1745 | static void of_selftest_overlay_i2c_13(void) | |
1746 | { | |
1747 | int ret; | |
1748 | ||
1749 | /* device should disable */ | |
1750 | ret = of_selftest_apply_overlay_check(13, 13, 1, 0, I2C_OVERLAY); | |
1751 | if (ret != 0) | |
6b1271de | 1752 | return; |
d5e75500 PA |
1753 | |
1754 | selftest(1, "overlay test %d passed\n", 13); | |
1755 | } | |
1756 | ||
1757 | /* just check for i2c mux existence */ | |
1758 | static void of_selftest_overlay_i2c_14(void) | |
1759 | { | |
6b1271de PA |
1760 | } |
1761 | ||
d5e75500 PA |
1762 | static void of_selftest_overlay_i2c_15(void) |
1763 | { | |
1764 | int ret; | |
1765 | ||
1766 | /* device should enable */ | |
1767 | ret = of_selftest_apply_overlay_check(16, 15, 0, 1, I2C_OVERLAY); | |
1768 | if (ret != 0) | |
1769 | return; | |
1770 | ||
1771 | selftest(1, "overlay test %d passed\n", 15); | |
1772 | } | |
1773 | ||
1774 | #else | |
1775 | ||
1776 | static inline void of_selftest_overlay_i2c_14(void) { } | |
1777 | static inline void of_selftest_overlay_i2c_15(void) { } | |
1778 | ||
1779 | #endif | |
1780 | ||
177d271c PA |
1781 | static void __init of_selftest_overlay(void) |
1782 | { | |
1783 | struct device_node *bus_np = NULL; | |
1784 | int ret; | |
1785 | ||
1786 | ret = platform_driver_register(&selftest_driver); | |
1787 | if (ret != 0) { | |
1788 | selftest(0, "could not register selftest driver\n"); | |
1789 | goto out; | |
1790 | } | |
1791 | ||
1792 | bus_np = of_find_node_by_path(bus_path); | |
1793 | if (bus_np == NULL) { | |
1794 | selftest(0, "could not find bus_path \"%s\"\n", bus_path); | |
1795 | goto out; | |
1796 | } | |
1797 | ||
1798 | ret = of_platform_populate(bus_np, of_default_bus_match_table, | |
1799 | NULL, NULL); | |
1800 | if (ret != 0) { | |
1801 | selftest(0, "could not populate bus @ \"%s\"\n", bus_path); | |
1802 | goto out; | |
1803 | } | |
1804 | ||
d5e75500 | 1805 | if (!of_selftest_device_exists(100, PDEV_OVERLAY)) { |
177d271c | 1806 | selftest(0, "could not find selftest0 @ \"%s\"\n", |
d5e75500 | 1807 | selftest_path(100, PDEV_OVERLAY)); |
177d271c PA |
1808 | goto out; |
1809 | } | |
1810 | ||
d5e75500 | 1811 | if (of_selftest_device_exists(101, PDEV_OVERLAY)) { |
177d271c | 1812 | selftest(0, "selftest1 @ \"%s\" should not exist\n", |
d5e75500 | 1813 | selftest_path(101, PDEV_OVERLAY)); |
177d271c PA |
1814 | goto out; |
1815 | } | |
1816 | ||
1817 | selftest(1, "basic infrastructure of overlays passed"); | |
1818 | ||
1819 | /* tests in sequence */ | |
1820 | of_selftest_overlay_0(); | |
1821 | of_selftest_overlay_1(); | |
1822 | of_selftest_overlay_2(); | |
1823 | of_selftest_overlay_3(); | |
1824 | of_selftest_overlay_4(); | |
1825 | of_selftest_overlay_5(); | |
1826 | of_selftest_overlay_6(); | |
1827 | of_selftest_overlay_8(); | |
1828 | ||
6b1271de PA |
1829 | of_selftest_overlay_10(); |
1830 | of_selftest_overlay_11(); | |
1831 | ||
d5e75500 PA |
1832 | #if IS_ENABLED(CONFIG_I2C) |
1833 | if (selftest(of_selftest_overlay_i2c_init() == 0, "i2c init failed\n")) | |
1834 | goto out; | |
1835 | ||
1836 | of_selftest_overlay_i2c_12(); | |
1837 | of_selftest_overlay_i2c_13(); | |
1838 | of_selftest_overlay_i2c_14(); | |
1839 | of_selftest_overlay_i2c_15(); | |
1840 | ||
1841 | of_selftest_overlay_i2c_cleanup(); | |
1842 | #endif | |
1843 | ||
177d271c PA |
1844 | out: |
1845 | of_node_put(bus_np); | |
1846 | } | |
1847 | ||
1848 | #else | |
1849 | static inline void __init of_selftest_overlay(void) { } | |
1850 | #endif | |
1851 | ||
53a42093 GL |
1852 | static int __init of_selftest(void) |
1853 | { | |
1854 | struct device_node *np; | |
ae9304c9 GM |
1855 | int res; |
1856 | ||
1857 | /* adding data for selftest */ | |
1858 | res = selftest_data_add(); | |
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 | ||
1871 | pr_info("start of selftest - you will see error messages\n"); | |
f2051d6a | 1872 | of_selftest_check_tree_linkage(); |
841ec213 | 1873 | of_selftest_check_phandles(); |
ae91ff72 | 1874 | of_selftest_find_node_by_name(); |
7e66c5c7 | 1875 | of_selftest_dynamic(); |
53a42093 | 1876 | of_selftest_parse_phandle_with_args(); |
a87fa1d8 | 1877 | of_selftest_property_string(); |
69843396 | 1878 | of_selftest_property_copy(); |
201c910b | 1879 | of_selftest_changeset(); |
a9f10ca7 | 1880 | of_selftest_parse_interrupts(); |
79d97015 | 1881 | of_selftest_parse_interrupts_extended(); |
1f42e5dd | 1882 | of_selftest_match_node(); |
82c0f589 | 1883 | of_selftest_platform_populate(); |
177d271c | 1884 | of_selftest_overlay(); |
ae9304c9 | 1885 | |
f2051d6a GL |
1886 | /* Double check linkage after removing testcase data */ |
1887 | of_selftest_check_tree_linkage(); | |
1888 | ||
1889 | pr_info("end of selftest - %i passed, %i failed\n", | |
1890 | selftest_results.passed, selftest_results.failed); | |
1891 | ||
53a42093 GL |
1892 | return 0; |
1893 | } | |
1894 | late_initcall(of_selftest); |