blob: aba8946cac46dddd171aed6ddf6e35f58be152b7 [file] [log] [blame]
Grant Likely53a42092011-12-12 09:25:57 -07001/*
2 * Self tests for device tree subsystem
3 */
4
Grant Likelycabb7d52013-02-12 21:19:37 +00005#define pr_fmt(fmt) "### dt-test ### " fmt
Grant Likely53a42092011-12-12 09:25:57 -07006
7#include <linux/clk.h>
8#include <linux/err.h>
9#include <linux/errno.h>
Grant Likely841ec212014-10-02 13:09:15 +010010#include <linux/hashtable.h>
Grant Likely53a42092011-12-12 09:25:57 -070011#include <linux/module.h>
12#include <linux/of.h>
Gaurav Minochaae9304c2014-07-16 23:09:39 -070013#include <linux/of_fdt.h>
Grant Likelya9f10ca2013-10-11 22:04:23 +010014#include <linux/of_irq.h>
Rob Herring82c0f582014-04-23 17:57:40 -050015#include <linux/of_platform.h>
Grant Likely53a42092011-12-12 09:25:57 -070016#include <linux/list.h>
17#include <linux/mutex.h>
18#include <linux/slab.h>
19#include <linux/device.h>
Pantelis Antoniou177d2712014-10-28 22:35:59 +020020#include <linux/platform_device.h>
21#include <linux/of_platform.h>
Grant Likely53a42092011-12-12 09:25:57 -070022
Pantelis Antonioud5e75502015-01-12 19:02:49 +020023#include <linux/i2c.h>
24#include <linux/i2c-mux.h>
25
Pantelis Antoniou69843392014-07-04 19:58:47 +030026#include "of_private.h"
27
Grant Likelya9f10ca2013-10-11 22:04:23 +010028static struct selftest_results {
29 int passed;
30 int failed;
31} selftest_results;
32
Grant Likely851da972014-11-04 13:14:13 +000033#define selftest(result, fmt, ...) ({ \
34 bool failed = !(result); \
35 if (failed) { \
Grant Likelya9f10ca2013-10-11 22:04:23 +010036 selftest_results.failed++; \
37 pr_err("FAIL %s():%i " fmt, __func__, __LINE__, ##__VA_ARGS__); \
Grant Likelycabb7d52013-02-12 21:19:37 +000038 } else { \
Grant Likelya9f10ca2013-10-11 22:04:23 +010039 selftest_results.passed++; \
40 pr_debug("pass %s():%i\n", __func__, __LINE__); \
Grant Likelycabb7d52013-02-12 21:19:37 +000041 } \
Grant Likely851da972014-11-04 13:14:13 +000042 failed; \
43})
Grant Likely53a42092011-12-12 09:25:57 -070044
Grant Likelyae91ff72014-03-14 13:53:10 +000045static void __init of_selftest_find_node_by_name(void)
46{
47 struct device_node *np;
Leif Lindholm75c28c02014-11-28 11:34:28 +000048 const char *options;
Grant Likelyae91ff72014-03-14 13:53:10 +000049
50 np = of_find_node_by_path("/testcase-data");
51 selftest(np && !strcmp("/testcase-data", np->full_name),
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/");
57 selftest(!np, "trailing '/' on /testcase-data/ should fail\n");
58
59 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
60 selftest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", np->full_name),
61 "find /testcase-data/phandle-tests/consumer-a failed\n");
62 of_node_put(np);
63
64 np = of_find_node_by_path("testcase-alias");
65 selftest(np && !strcmp("/testcase-data", np->full_name),
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/");
71 selftest(!np, "trailing '/' on testcase-alias/ should fail\n");
72
73 np = of_find_node_by_path("testcase-alias/phandle-tests/consumer-a");
74 selftest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", np->full_name),
75 "find testcase-alias/phandle-tests/consumer-a failed\n");
76 of_node_put(np);
77
78 np = of_find_node_by_path("/testcase-data/missing-path");
79 selftest(!np, "non-existent path returned node %s\n", np->full_name);
80 of_node_put(np);
81
82 np = of_find_node_by_path("missing-alias");
83 selftest(!np, "non-existent alias returned node %s\n", np->full_name);
84 of_node_put(np);
85
86 np = of_find_node_by_path("testcase-alias/missing-path");
87 selftest(!np, "non-existent alias with relative path returned node %s\n", np->full_name);
88 of_node_put(np);
Leif Lindholm75c28c02014-11-28 11:34:28 +000089
90 np = of_find_node_opts_by_path("/testcase-data:testoption", &options);
91 selftest(np && !strcmp("testoption", options),
92 "option path test failed\n");
93 of_node_put(np);
94
Peter Hurley8cbba1a2015-03-06 13:59:59 -050095 np = of_find_node_opts_by_path("/testcase-data:test/option", &options);
96 selftest(np && !strcmp("test/option", options),
97 "option path test, subcase #1 failed\n");
98 of_node_put(np);
99
Leif Lindholm75c28c02014-11-28 11:34:28 +0000100 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
Peter Hurley8cbba1a2015-03-06 13:59:59 -0500110 np = of_find_node_opts_by_path("testcase-alias:test/alias/option",
111 &options);
112 selftest(np && !strcmp("test/alias/option", options),
113 "option alias path test, subcase #1 failed\n");
114 of_node_put(np);
115
Leif Lindholm75c28c02014-11-28 11:34:28 +0000116 np = of_find_node_opts_by_path("testcase-alias:testaliasoption", NULL);
117 selftest(np, "NULL option alias path test failed\n");
118 of_node_put(np);
119
120 options = "testoption";
121 np = of_find_node_opts_by_path("testcase-alias", &options);
122 selftest(np && !options, "option clearing test failed\n");
123 of_node_put(np);
124
125 options = "testoption";
126 np = of_find_node_opts_by_path("/", &options);
127 selftest(np && !options, "option clearing root node test failed\n");
128 of_node_put(np);
Grant Likelyae91ff72014-03-14 13:53:10 +0000129}
130
Grant Likely7e66c5c2013-11-15 17:19:09 +0000131static void __init of_selftest_dynamic(void)
132{
133 struct device_node *np;
134 struct property *prop;
135
136 np = of_find_node_by_path("/testcase-data");
137 if (!np) {
138 pr_err("missing testcase data\n");
139 return;
140 }
141
142 /* Array of 4 properties for the purpose of testing */
143 prop = kzalloc(sizeof(*prop) * 4, GFP_KERNEL);
144 if (!prop) {
145 selftest(0, "kzalloc() failed\n");
146 return;
147 }
148
149 /* Add a new property - should pass*/
150 prop->name = "new-property";
151 prop->value = "new-property-data";
152 prop->length = strlen(prop->value);
153 selftest(of_add_property(np, prop) == 0, "Adding a new property failed\n");
154
155 /* Try to add an existing property - should fail */
156 prop++;
157 prop->name = "new-property";
158 prop->value = "new-property-data-should-fail";
159 prop->length = strlen(prop->value);
160 selftest(of_add_property(np, prop) != 0,
161 "Adding an existing property should have failed\n");
162
163 /* Try to modify an existing property - should pass */
164 prop->value = "modify-property-data-should-pass";
165 prop->length = strlen(prop->value);
166 selftest(of_update_property(np, prop) == 0,
167 "Updating an existing property should have passed\n");
168
169 /* Try to modify non-existent property - should pass*/
170 prop++;
171 prop->name = "modify-property";
172 prop->value = "modify-missing-property-data-should-pass";
173 prop->length = strlen(prop->value);
174 selftest(of_update_property(np, prop) == 0,
175 "Updating a missing property should have passed\n");
176
177 /* Remove property - should pass */
178 selftest(of_remove_property(np, prop) == 0,
179 "Removing a property should have passed\n");
180
181 /* Adding very large property - should pass */
182 prop++;
183 prop->name = "large-property-PAGE_SIZEx8";
184 prop->length = PAGE_SIZE * 8;
185 prop->value = kzalloc(prop->length, GFP_KERNEL);
186 selftest(prop->value != NULL, "Unable to allocate large buffer\n");
187 if (prop->value)
188 selftest(of_add_property(np, prop) == 0,
189 "Adding a large property should have passed\n");
190}
191
Grant Likelyf2051d62014-10-01 17:40:22 +0100192static int __init of_selftest_check_node_linkage(struct device_node *np)
193{
Grant Likely5063e252014-10-03 16:28:27 +0100194 struct device_node *child;
Grant Likelyf2051d62014-10-01 17:40:22 +0100195 int count = 0, rc;
196
197 for_each_child_of_node(np, child) {
198 if (child->parent != np) {
199 pr_err("Child node %s links to wrong parent %s\n",
200 child->name, np->name);
201 return -EINVAL;
202 }
203
Grant Likelyf2051d62014-10-01 17:40:22 +0100204 rc = of_selftest_check_node_linkage(child);
205 if (rc < 0)
206 return rc;
207 count += rc;
208 }
209
210 return count + 1;
211}
212
213static void __init of_selftest_check_tree_linkage(void)
214{
215 struct device_node *np;
216 int allnode_count = 0, child_count;
217
Grant Likely5063e252014-10-03 16:28:27 +0100218 if (!of_root)
Grant Likelyf2051d62014-10-01 17:40:22 +0100219 return;
220
221 for_each_of_allnodes(np)
222 allnode_count++;
Grant Likely5063e252014-10-03 16:28:27 +0100223 child_count = of_selftest_check_node_linkage(of_root);
Grant Likelyf2051d62014-10-01 17:40:22 +0100224
225 selftest(child_count > 0, "Device node data structure is corrupted\n");
226 selftest(child_count == allnode_count, "allnodes list size (%i) doesn't match"
227 "sibling lists size (%i)\n", allnode_count, child_count);
228 pr_debug("allnodes list size (%i); sibling lists size (%i)\n", allnode_count, child_count);
229}
230
Grant Likely841ec212014-10-02 13:09:15 +0100231struct node_hash {
232 struct hlist_node node;
233 struct device_node *np;
234};
235
Grant Likely2118f4b2014-10-07 11:30:31 +0100236static DEFINE_HASHTABLE(phandle_ht, 8);
Grant Likely841ec212014-10-02 13:09:15 +0100237static void __init of_selftest_check_phandles(void)
238{
239 struct device_node *np;
240 struct node_hash *nh;
241 struct hlist_node *tmp;
242 int i, dup_count = 0, phandle_count = 0;
Grant Likely841ec212014-10-02 13:09:15 +0100243
Grant Likely841ec212014-10-02 13:09:15 +0100244 for_each_of_allnodes(np) {
245 if (!np->phandle)
246 continue;
247
Grant Likely2118f4b2014-10-07 11:30:31 +0100248 hash_for_each_possible(phandle_ht, nh, node, np->phandle) {
Grant Likely841ec212014-10-02 13:09:15 +0100249 if (nh->np->phandle == np->phandle) {
250 pr_info("Duplicate phandle! %i used by %s and %s\n",
251 np->phandle, nh->np->full_name, np->full_name);
252 dup_count++;
253 break;
254 }
255 }
256
257 nh = kzalloc(sizeof(*nh), GFP_KERNEL);
258 if (WARN_ON(!nh))
259 return;
260
261 nh->np = np;
Grant Likely2118f4b2014-10-07 11:30:31 +0100262 hash_add(phandle_ht, &nh->node, np->phandle);
Grant Likely841ec212014-10-02 13:09:15 +0100263 phandle_count++;
264 }
265 selftest(dup_count == 0, "Found %i duplicates in %i phandles\n",
266 dup_count, phandle_count);
267
268 /* Clean up */
Grant Likely2118f4b2014-10-07 11:30:31 +0100269 hash_for_each_safe(phandle_ht, i, tmp, nh, node) {
Grant Likely841ec212014-10-02 13:09:15 +0100270 hash_del(&nh->node);
271 kfree(nh);
272 }
273}
274
Grant Likely53a42092011-12-12 09:25:57 -0700275static void __init of_selftest_parse_phandle_with_args(void)
276{
277 struct device_node *np;
278 struct of_phandle_args args;
Grant Likelycabb7d52013-02-12 21:19:37 +0000279 int i, rc;
Grant Likely53a42092011-12-12 09:25:57 -0700280
Grant Likely53a42092011-12-12 09:25:57 -0700281 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
282 if (!np) {
283 pr_err("missing testcase data\n");
284 return;
285 }
286
Grant Likelybd69f732013-02-10 22:57:21 +0000287 rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
288 selftest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc);
289
Grant Likelyf7f951c2013-02-12 17:41:22 +0000290 for (i = 0; i < 8; i++) {
Grant Likely53a42092011-12-12 09:25:57 -0700291 bool passed = true;
292 rc = of_parse_phandle_with_args(np, "phandle-list",
293 "#phandle-cells", i, &args);
294
295 /* Test the values from tests-phandle.dtsi */
296 switch (i) {
297 case 0:
298 passed &= !rc;
299 passed &= (args.args_count == 1);
300 passed &= (args.args[0] == (i + 1));
301 break;
302 case 1:
303 passed &= !rc;
304 passed &= (args.args_count == 2);
305 passed &= (args.args[0] == (i + 1));
306 passed &= (args.args[1] == 0);
307 break;
308 case 2:
309 passed &= (rc == -ENOENT);
310 break;
311 case 3:
312 passed &= !rc;
313 passed &= (args.args_count == 3);
314 passed &= (args.args[0] == (i + 1));
315 passed &= (args.args[1] == 4);
316 passed &= (args.args[2] == 3);
317 break;
318 case 4:
319 passed &= !rc;
320 passed &= (args.args_count == 2);
321 passed &= (args.args[0] == (i + 1));
322 passed &= (args.args[1] == 100);
323 break;
324 case 5:
325 passed &= !rc;
326 passed &= (args.args_count == 0);
327 break;
328 case 6:
329 passed &= !rc;
330 passed &= (args.args_count == 1);
331 passed &= (args.args[0] == (i + 1));
332 break;
333 case 7:
Grant Likelycabb7d52013-02-12 21:19:37 +0000334 passed &= (rc == -ENOENT);
Grant Likely53a42092011-12-12 09:25:57 -0700335 break;
336 default:
337 passed = false;
338 }
339
Grant Likelycabb7d52013-02-12 21:19:37 +0000340 selftest(passed, "index %i - data error on node %s rc=%i\n",
341 i, args.np->full_name, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700342 }
343
344 /* Check for missing list property */
345 rc = of_parse_phandle_with_args(np, "phandle-list-missing",
346 "#phandle-cells", 0, &args);
Grant Likelycabb7d52013-02-12 21:19:37 +0000347 selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000348 rc = of_count_phandle_with_args(np, "phandle-list-missing",
349 "#phandle-cells");
350 selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700351
352 /* Check for missing cells property */
353 rc = of_parse_phandle_with_args(np, "phandle-list",
354 "#phandle-cells-missing", 0, &args);
Grant Likelycabb7d52013-02-12 21:19:37 +0000355 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000356 rc = of_count_phandle_with_args(np, "phandle-list",
357 "#phandle-cells-missing");
358 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700359
360 /* Check for bad phandle in list */
361 rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle",
362 "#phandle-cells", 0, &args);
Grant Likelycabb7d52013-02-12 21:19:37 +0000363 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000364 rc = of_count_phandle_with_args(np, "phandle-list-bad-phandle",
365 "#phandle-cells");
366 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700367
368 /* Check for incorrectly formed argument list */
369 rc = of_parse_phandle_with_args(np, "phandle-list-bad-args",
370 "#phandle-cells", 1, &args);
Grant Likelycabb7d52013-02-12 21:19:37 +0000371 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000372 rc = of_count_phandle_with_args(np, "phandle-list-bad-args",
373 "#phandle-cells");
374 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700375}
376
Grant Likelya87fa1d2014-11-03 15:15:35 +0000377static void __init of_selftest_property_string(void)
Grant Likely7aff0fe2011-12-12 09:25:58 -0700378{
Grant Likelya87fa1d2014-11-03 15:15:35 +0000379 const char *strings[4];
Grant Likely7aff0fe2011-12-12 09:25:58 -0700380 struct device_node *np;
381 int rc;
382
Grant Likely7aff0fe2011-12-12 09:25:58 -0700383 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
384 if (!np) {
385 pr_err("No testcase data in device tree\n");
386 return;
387 }
388
389 rc = of_property_match_string(np, "phandle-list-names", "first");
390 selftest(rc == 0, "first expected:0 got:%i\n", rc);
391 rc = of_property_match_string(np, "phandle-list-names", "second");
Wang Long649022e2015-03-03 03:50:38 +0000392 selftest(rc == 1, "second expected:1 got:%i\n", rc);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700393 rc = of_property_match_string(np, "phandle-list-names", "third");
Wang Long649022e2015-03-03 03:50:38 +0000394 selftest(rc == 2, "third expected:2 got:%i\n", rc);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700395 rc = of_property_match_string(np, "phandle-list-names", "fourth");
Grant Likelya87fa1d2014-11-03 15:15:35 +0000396 selftest(rc == -ENODATA, "unmatched string; rc=%i\n", rc);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700397 rc = of_property_match_string(np, "missing-property", "blah");
Grant Likelya87fa1d2014-11-03 15:15:35 +0000398 selftest(rc == -EINVAL, "missing property; rc=%i\n", rc);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700399 rc = of_property_match_string(np, "empty-property", "blah");
Grant Likelya87fa1d2014-11-03 15:15:35 +0000400 selftest(rc == -ENODATA, "empty property; rc=%i\n", rc);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700401 rc = of_property_match_string(np, "unterminated-string", "blah");
Grant Likelya87fa1d2014-11-03 15:15:35 +0000402 selftest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
403
404 /* of_property_count_strings() tests */
405 rc = of_property_count_strings(np, "string-property");
406 selftest(rc == 1, "Incorrect string count; rc=%i\n", rc);
407 rc = of_property_count_strings(np, "phandle-list-names");
408 selftest(rc == 3, "Incorrect string count; rc=%i\n", rc);
409 rc = of_property_count_strings(np, "unterminated-string");
410 selftest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
411 rc = of_property_count_strings(np, "unterminated-string-list");
412 selftest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
413
414 /* of_property_read_string_index() tests */
415 rc = of_property_read_string_index(np, "string-property", 0, strings);
416 selftest(rc == 0 && !strcmp(strings[0], "foobar"), "of_property_read_string_index() failure; rc=%i\n", rc);
417 strings[0] = NULL;
418 rc = of_property_read_string_index(np, "string-property", 1, strings);
419 selftest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
420 rc = of_property_read_string_index(np, "phandle-list-names", 0, strings);
421 selftest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
422 rc = of_property_read_string_index(np, "phandle-list-names", 1, strings);
423 selftest(rc == 0 && !strcmp(strings[0], "second"), "of_property_read_string_index() failure; rc=%i\n", rc);
424 rc = of_property_read_string_index(np, "phandle-list-names", 2, strings);
425 selftest(rc == 0 && !strcmp(strings[0], "third"), "of_property_read_string_index() failure; rc=%i\n", rc);
426 strings[0] = NULL;
427 rc = of_property_read_string_index(np, "phandle-list-names", 3, strings);
428 selftest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
429 strings[0] = NULL;
430 rc = of_property_read_string_index(np, "unterminated-string", 0, strings);
431 selftest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
432 rc = of_property_read_string_index(np, "unterminated-string-list", 0, strings);
433 selftest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
434 strings[0] = NULL;
435 rc = of_property_read_string_index(np, "unterminated-string-list", 2, strings); /* should fail */
436 selftest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
437 strings[1] = NULL;
438
439 /* of_property_read_string_array() tests */
440 rc = of_property_read_string_array(np, "string-property", strings, 4);
441 selftest(rc == 1, "Incorrect string count; rc=%i\n", rc);
442 rc = of_property_read_string_array(np, "phandle-list-names", strings, 4);
443 selftest(rc == 3, "Incorrect string count; rc=%i\n", rc);
444 rc = of_property_read_string_array(np, "unterminated-string", strings, 4);
445 selftest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
446 /* -- An incorrectly formed string should cause a failure */
447 rc = of_property_read_string_array(np, "unterminated-string-list", strings, 4);
448 selftest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
449 /* -- parsing the correctly formed strings should still work: */
450 strings[2] = NULL;
451 rc = of_property_read_string_array(np, "unterminated-string-list", strings, 2);
452 selftest(rc == 2 && strings[2] == NULL, "of_property_read_string_array() failure; rc=%i\n", rc);
453 strings[1] = NULL;
454 rc = of_property_read_string_array(np, "phandle-list-names", strings, 1);
455 selftest(rc == 1 && strings[1] == NULL, "Overwrote end of string array; rc=%i, str='%s'\n", rc, strings[1]);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700456}
457
Pantelis Antoniou69843392014-07-04 19:58:47 +0300458#define propcmp(p1, p2) (((p1)->length == (p2)->length) && \
459 (p1)->value && (p2)->value && \
460 !memcmp((p1)->value, (p2)->value, (p1)->length) && \
461 !strcmp((p1)->name, (p2)->name))
462static void __init of_selftest_property_copy(void)
463{
464#ifdef CONFIG_OF_DYNAMIC
465 struct property p1 = { .name = "p1", .length = 0, .value = "" };
466 struct property p2 = { .name = "p2", .length = 5, .value = "abcd" };
467 struct property *new;
468
469 new = __of_prop_dup(&p1, GFP_KERNEL);
470 selftest(new && propcmp(&p1, new), "empty property didn't copy correctly\n");
471 kfree(new->value);
472 kfree(new->name);
473 kfree(new);
474
475 new = __of_prop_dup(&p2, GFP_KERNEL);
476 selftest(new && propcmp(&p2, new), "non-empty property didn't copy correctly\n");
477 kfree(new->value);
478 kfree(new->name);
479 kfree(new);
480#endif
481}
482
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300483static void __init of_selftest_changeset(void)
484{
485#ifdef CONFIG_OF_DYNAMIC
486 struct property *ppadd, padd = { .name = "prop-add", .length = 0, .value = "" };
487 struct property *ppupdate, pupdate = { .name = "prop-update", .length = 5, .value = "abcd" };
488 struct property *ppremove;
Grant Likelye5179582014-11-17 22:31:32 +0000489 struct device_node *n1, *n2, *n21, *nremove, *parent, *np;
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300490 struct of_changeset chgset;
491
Grant Likelye5179582014-11-17 22:31:32 +0000492 n1 = __of_node_dup(NULL, "/testcase-data/changeset/n1");
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300493 selftest(n1, "testcase setup failure\n");
Grant Likelye5179582014-11-17 22:31:32 +0000494 n2 = __of_node_dup(NULL, "/testcase-data/changeset/n2");
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300495 selftest(n2, "testcase setup failure\n");
Grant Likelye5179582014-11-17 22:31:32 +0000496 n21 = __of_node_dup(NULL, "%s/%s", "/testcase-data/changeset/n2", "n21");
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300497 selftest(n21, "testcase setup failure %p\n", n21);
498 nremove = of_find_node_by_path("/testcase-data/changeset/node-remove");
499 selftest(nremove, "testcase setup failure\n");
500 ppadd = __of_prop_dup(&padd, GFP_KERNEL);
501 selftest(ppadd, "testcase setup failure\n");
502 ppupdate = __of_prop_dup(&pupdate, GFP_KERNEL);
503 selftest(ppupdate, "testcase setup failure\n");
504 parent = nremove->parent;
505 n1->parent = parent;
506 n2->parent = parent;
507 n21->parent = n2;
508 n2->child = n21;
509 ppremove = of_find_property(parent, "prop-remove", NULL);
510 selftest(ppremove, "failed to find removal prop");
511
512 of_changeset_init(&chgset);
513 selftest(!of_changeset_attach_node(&chgset, n1), "fail attach n1\n");
514 selftest(!of_changeset_attach_node(&chgset, n2), "fail attach n2\n");
515 selftest(!of_changeset_detach_node(&chgset, nremove), "fail remove node\n");
516 selftest(!of_changeset_attach_node(&chgset, n21), "fail attach n21\n");
517 selftest(!of_changeset_add_property(&chgset, parent, ppadd), "fail add prop\n");
518 selftest(!of_changeset_update_property(&chgset, parent, ppupdate), "fail update prop\n");
519 selftest(!of_changeset_remove_property(&chgset, parent, ppremove), "fail remove prop\n");
520 mutex_lock(&of_mutex);
521 selftest(!of_changeset_apply(&chgset), "apply failed\n");
522 mutex_unlock(&of_mutex);
523
Grant Likelye5179582014-11-17 22:31:32 +0000524 /* Make sure node names are constructed correctly */
525 selftest((np = of_find_node_by_path("/testcase-data/changeset/n2/n21")),
526 "'%s' not added\n", n21->full_name);
Markus Elfringc46ca3c2014-12-02 13:54:00 +0100527 of_node_put(np);
Grant Likelye5179582014-11-17 22:31:32 +0000528
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300529 mutex_lock(&of_mutex);
530 selftest(!of_changeset_revert(&chgset), "revert failed\n");
531 mutex_unlock(&of_mutex);
532
533 of_changeset_destroy(&chgset);
534#endif
535}
536
Grant Likelya9f10ca2013-10-11 22:04:23 +0100537static void __init of_selftest_parse_interrupts(void)
538{
539 struct device_node *np;
540 struct of_phandle_args args;
541 int i, rc;
542
543 np = of_find_node_by_path("/testcase-data/interrupts/interrupts0");
544 if (!np) {
545 pr_err("missing testcase data\n");
546 return;
547 }
548
549 for (i = 0; i < 4; i++) {
550 bool passed = true;
551 args.args_count = 0;
552 rc = of_irq_parse_one(np, i, &args);
553
554 passed &= !rc;
555 passed &= (args.args_count == 1);
556 passed &= (args.args[0] == (i + 1));
557
558 selftest(passed, "index %i - data error on node %s rc=%i\n",
559 i, args.np->full_name, rc);
560 }
561 of_node_put(np);
562
563 np = of_find_node_by_path("/testcase-data/interrupts/interrupts1");
564 if (!np) {
565 pr_err("missing testcase data\n");
566 return;
567 }
568
569 for (i = 0; i < 4; i++) {
570 bool passed = true;
571 args.args_count = 0;
572 rc = of_irq_parse_one(np, i, &args);
573
574 /* Test the values from tests-phandle.dtsi */
575 switch (i) {
576 case 0:
577 passed &= !rc;
578 passed &= (args.args_count == 1);
579 passed &= (args.args[0] == 9);
580 break;
581 case 1:
582 passed &= !rc;
583 passed &= (args.args_count == 3);
584 passed &= (args.args[0] == 10);
585 passed &= (args.args[1] == 11);
586 passed &= (args.args[2] == 12);
587 break;
588 case 2:
589 passed &= !rc;
590 passed &= (args.args_count == 2);
591 passed &= (args.args[0] == 13);
592 passed &= (args.args[1] == 14);
593 break;
594 case 3:
595 passed &= !rc;
596 passed &= (args.args_count == 2);
597 passed &= (args.args[0] == 15);
598 passed &= (args.args[1] == 16);
599 break;
600 default:
601 passed = false;
602 }
603 selftest(passed, "index %i - data error on node %s rc=%i\n",
604 i, args.np->full_name, rc);
605 }
606 of_node_put(np);
607}
608
Grant Likely79d97012013-09-19 16:47:37 -0500609static void __init of_selftest_parse_interrupts_extended(void)
610{
611 struct device_node *np;
612 struct of_phandle_args args;
613 int i, rc;
614
615 np = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
616 if (!np) {
617 pr_err("missing testcase data\n");
618 return;
619 }
620
621 for (i = 0; i < 7; i++) {
622 bool passed = true;
623 rc = of_irq_parse_one(np, i, &args);
624
625 /* Test the values from tests-phandle.dtsi */
626 switch (i) {
627 case 0:
628 passed &= !rc;
629 passed &= (args.args_count == 1);
630 passed &= (args.args[0] == 1);
631 break;
632 case 1:
633 passed &= !rc;
634 passed &= (args.args_count == 3);
635 passed &= (args.args[0] == 2);
636 passed &= (args.args[1] == 3);
637 passed &= (args.args[2] == 4);
638 break;
639 case 2:
640 passed &= !rc;
641 passed &= (args.args_count == 2);
642 passed &= (args.args[0] == 5);
643 passed &= (args.args[1] == 6);
644 break;
645 case 3:
646 passed &= !rc;
647 passed &= (args.args_count == 1);
648 passed &= (args.args[0] == 9);
649 break;
650 case 4:
651 passed &= !rc;
652 passed &= (args.args_count == 3);
653 passed &= (args.args[0] == 10);
654 passed &= (args.args[1] == 11);
655 passed &= (args.args[2] == 12);
656 break;
657 case 5:
658 passed &= !rc;
659 passed &= (args.args_count == 2);
660 passed &= (args.args[0] == 13);
661 passed &= (args.args[1] == 14);
662 break;
663 case 6:
664 passed &= !rc;
665 passed &= (args.args_count == 1);
666 passed &= (args.args[0] == 15);
667 break;
668 default:
669 passed = false;
670 }
671
672 selftest(passed, "index %i - data error on node %s rc=%i\n",
673 i, args.np->full_name, rc);
674 }
675 of_node_put(np);
676}
677
Grant Likely1f42e5d2014-02-18 21:38:55 +0000678static struct of_device_id match_node_table[] = {
679 { .data = "A", .name = "name0", }, /* Name alone is lowest priority */
680 { .data = "B", .type = "type1", }, /* followed by type alone */
681
682 { .data = "Ca", .name = "name2", .type = "type1", }, /* followed by both together */
683 { .data = "Cb", .name = "name2", }, /* Only match when type doesn't match */
684 { .data = "Cc", .name = "name2", .type = "type2", },
685
686 { .data = "E", .compatible = "compat3" },
687 { .data = "G", .compatible = "compat2", },
688 { .data = "H", .compatible = "compat2", .name = "name5", },
689 { .data = "I", .compatible = "compat2", .type = "type1", },
690 { .data = "J", .compatible = "compat2", .type = "type1", .name = "name8", },
691 { .data = "K", .compatible = "compat2", .name = "name9", },
692 {}
693};
694
695static struct {
696 const char *path;
697 const char *data;
698} match_node_tests[] = {
699 { .path = "/testcase-data/match-node/name0", .data = "A", },
700 { .path = "/testcase-data/match-node/name1", .data = "B", },
701 { .path = "/testcase-data/match-node/a/name2", .data = "Ca", },
702 { .path = "/testcase-data/match-node/b/name2", .data = "Cb", },
703 { .path = "/testcase-data/match-node/c/name2", .data = "Cc", },
704 { .path = "/testcase-data/match-node/name3", .data = "E", },
705 { .path = "/testcase-data/match-node/name4", .data = "G", },
706 { .path = "/testcase-data/match-node/name5", .data = "H", },
707 { .path = "/testcase-data/match-node/name6", .data = "G", },
708 { .path = "/testcase-data/match-node/name7", .data = "I", },
709 { .path = "/testcase-data/match-node/name8", .data = "J", },
710 { .path = "/testcase-data/match-node/name9", .data = "K", },
711};
712
713static void __init of_selftest_match_node(void)
714{
715 struct device_node *np;
716 const struct of_device_id *match;
717 int i;
718
719 for (i = 0; i < ARRAY_SIZE(match_node_tests); i++) {
720 np = of_find_node_by_path(match_node_tests[i].path);
721 if (!np) {
722 selftest(0, "missing testcase node %s\n",
723 match_node_tests[i].path);
724 continue;
725 }
726
727 match = of_match_node(match_node_table, np);
728 if (!match) {
729 selftest(0, "%s didn't match anything\n",
730 match_node_tests[i].path);
731 continue;
732 }
733
734 if (strcmp(match->data, match_node_tests[i].data) != 0) {
735 selftest(0, "%s got wrong match. expected %s, got %s\n",
736 match_node_tests[i].path, match_node_tests[i].data,
737 (const char *)match->data);
738 continue;
739 }
740 selftest(1, "passed");
741 }
742}
743
Grant Likely851da972014-11-04 13:14:13 +0000744struct device test_bus = {
745 .init_name = "unittest-bus",
746};
Rob Herring82c0f582014-04-23 17:57:40 -0500747static void __init of_selftest_platform_populate(void)
748{
Grant Likely851da972014-11-04 13:14:13 +0000749 int irq, rc;
750 struct device_node *np, *child, *grandchild;
Rob Herring82c0f582014-04-23 17:57:40 -0500751 struct platform_device *pdev;
Rob Herringfb2caa52014-05-13 10:07:54 -0500752 struct of_device_id match[] = {
753 { .compatible = "test-device", },
754 {}
755 };
Rob Herring82c0f582014-04-23 17:57:40 -0500756
757 np = of_find_node_by_path("/testcase-data");
758 of_platform_populate(np, of_default_bus_match_table, NULL, NULL);
759
760 /* Test that a missing irq domain returns -EPROBE_DEFER */
761 np = of_find_node_by_path("/testcase-data/testcase-device1");
762 pdev = of_find_device_by_node(np);
Rob Herring7d1cdc82014-05-13 10:07:29 -0500763 selftest(pdev, "device 1 creation failed\n");
764
Rob Herring82c0f582014-04-23 17:57:40 -0500765 irq = platform_get_irq(pdev, 0);
Rob Herring7d1cdc82014-05-13 10:07:29 -0500766 selftest(irq == -EPROBE_DEFER, "device deferred probe failed - %d\n", irq);
Rob Herring82c0f582014-04-23 17:57:40 -0500767
768 /* Test that a parsing failure does not return -EPROBE_DEFER */
769 np = of_find_node_by_path("/testcase-data/testcase-device2");
770 pdev = of_find_device_by_node(np);
Rob Herring7d1cdc82014-05-13 10:07:29 -0500771 selftest(pdev, "device 2 creation failed\n");
Rob Herring82c0f582014-04-23 17:57:40 -0500772 irq = platform_get_irq(pdev, 0);
Rob Herring7d1cdc82014-05-13 10:07:29 -0500773 selftest(irq < 0 && irq != -EPROBE_DEFER, "device parsing error failed - %d\n", irq);
Rob Herring82c0f582014-04-23 17:57:40 -0500774
Grant Likely851da972014-11-04 13:14:13 +0000775 if (selftest(np = of_find_node_by_path("/testcase-data/platform-tests"),
776 "No testcase data in device tree\n"));
Rob Herringfb2caa52014-05-13 10:07:54 -0500777 return;
Grant Likely851da972014-11-04 13:14:13 +0000778
779 if (selftest(!(rc = device_register(&test_bus)),
780 "testbus registration failed; rc=%i\n", rc));
781 return;
Rob Herringfb2caa52014-05-13 10:07:54 -0500782
783 for_each_child_of_node(np, child) {
Grant Likely851da972014-11-04 13:14:13 +0000784 of_platform_populate(child, match, NULL, &test_bus);
Rob Herringfb2caa52014-05-13 10:07:54 -0500785 for_each_child_of_node(child, grandchild)
786 selftest(of_find_device_by_node(grandchild),
787 "Could not create device for node '%s'\n",
788 grandchild->name);
789 }
Grant Likely851da972014-11-04 13:14:13 +0000790
791 of_platform_depopulate(&test_bus);
792 for_each_child_of_node(np, child) {
793 for_each_child_of_node(child, grandchild)
794 selftest(!of_find_device_by_node(grandchild),
795 "device didn't get destroyed '%s'\n",
796 grandchild->name);
797 }
798
799 device_unregister(&test_bus);
800 of_node_put(np);
Rob Herring82c0f582014-04-23 17:57:40 -0500801}
802
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700803/**
804 * update_node_properties - adds the properties
805 * of np into dup node (present in live tree) and
806 * updates parent of children of np to dup.
807 *
808 * @np: node already present in live tree
809 * @dup: node present in live tree to be updated
810 */
811static void update_node_properties(struct device_node *np,
812 struct device_node *dup)
813{
814 struct property *prop;
815 struct device_node *child;
816
817 for_each_property_of_node(np, prop)
818 of_add_property(dup, prop);
819
820 for_each_child_of_node(np, child)
821 child->parent = dup;
822}
823
824/**
825 * attach_node_and_children - attaches nodes
826 * and its children to live tree
827 *
828 * @np: Node to attach to live tree
829 */
830static int attach_node_and_children(struct device_node *np)
831{
Grant Likely5063e252014-10-03 16:28:27 +0100832 struct device_node *next, *dup, *child;
Gaurav Minocha3ce04b42015-01-10 23:19:51 -0800833 unsigned long flags;
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700834
Grant Likely5063e252014-10-03 16:28:27 +0100835 dup = of_find_node_by_path(np->full_name);
836 if (dup) {
837 update_node_properties(np, dup);
838 return 0;
839 }
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700840
Grant Likely5063e252014-10-03 16:28:27 +0100841 child = np->child;
842 np->child = NULL;
Gaurav Minocha3ce04b42015-01-10 23:19:51 -0800843
844 mutex_lock(&of_mutex);
845 raw_spin_lock_irqsave(&devtree_lock, flags);
846 np->sibling = np->parent->child;
847 np->parent->child = np;
848 of_node_clear_flag(np, OF_DETACHED);
849 raw_spin_unlock_irqrestore(&devtree_lock, flags);
850
851 __of_attach_node_sysfs(np);
852 mutex_unlock(&of_mutex);
853
Grant Likely5063e252014-10-03 16:28:27 +0100854 while (child) {
855 next = child->sibling;
856 attach_node_and_children(child);
857 child = next;
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700858 }
859
860 return 0;
861}
862
863/**
864 * selftest_data_add - Reads, copies data from
865 * linked tree and attaches it to the live tree
866 */
867static int __init selftest_data_add(void)
868{
869 void *selftest_data;
Gaurav Minochab951f9d2014-07-26 12:48:50 -0700870 struct device_node *selftest_data_node, *np;
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700871 extern uint8_t __dtb_testcases_begin[];
872 extern uint8_t __dtb_testcases_end[];
873 const int size = __dtb_testcases_end - __dtb_testcases_begin;
Grant Likely2eb46da2014-10-02 14:36:46 +0100874 int rc;
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700875
Gaurav Minochab951f9d2014-07-26 12:48:50 -0700876 if (!size) {
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700877 pr_warn("%s: No testcase data to attach; not running tests\n",
878 __func__);
879 return -ENODATA;
880 }
881
882 /* creating copy */
883 selftest_data = kmemdup(__dtb_testcases_begin, size, GFP_KERNEL);
884
885 if (!selftest_data) {
886 pr_warn("%s: Failed to allocate memory for selftest_data; "
887 "not running tests\n", __func__);
888 return -ENOMEM;
889 }
890 of_fdt_unflatten_tree(selftest_data, &selftest_data_node);
Gaurav Minochab951f9d2014-07-26 12:48:50 -0700891 if (!selftest_data_node) {
892 pr_warn("%s: No tree to attach; not running tests\n", __func__);
893 return -ENODATA;
894 }
Grant Likely2eb46da2014-10-02 14:36:46 +0100895 of_node_set_flag(selftest_data_node, OF_DETACHED);
896 rc = of_resolve_phandles(selftest_data_node);
897 if (rc) {
898 pr_err("%s: Failed to resolve phandles (rc=%i)\n", __func__, rc);
899 return -EINVAL;
900 }
Gaurav Minochab951f9d2014-07-26 12:48:50 -0700901
Grant Likely5063e252014-10-03 16:28:27 +0100902 if (!of_root) {
Grant Likely5063e252014-10-03 16:28:27 +0100903 of_root = selftest_data_node;
Gaurav Minochab951f9d2014-07-26 12:48:50 -0700904 for_each_of_allnodes(np)
905 __of_attach_node_sysfs(np);
906 of_aliases = of_find_node_by_path("/aliases");
907 of_chosen = of_find_node_by_path("/chosen");
908 return 0;
909 }
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700910
911 /* attach the sub-tree to live tree */
Grant Likely5063e252014-10-03 16:28:27 +0100912 np = selftest_data_node->child;
913 while (np) {
914 struct device_node *next = np->sibling;
915 np->parent = of_root;
916 attach_node_and_children(np);
917 np = next;
918 }
919 return 0;
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700920}
921
Pantelis Antoniou177d2712014-10-28 22:35:59 +0200922#ifdef CONFIG_OF_OVERLAY
923
924static int selftest_probe(struct platform_device *pdev)
925{
926 struct device *dev = &pdev->dev;
927 struct device_node *np = dev->of_node;
928
929 if (np == NULL) {
930 dev_err(dev, "No OF data for device\n");
931 return -EINVAL;
932
933 }
934
935 dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +0200936
937 of_platform_populate(np, NULL, NULL, &pdev->dev);
938
Pantelis Antoniou177d2712014-10-28 22:35:59 +0200939 return 0;
940}
941
942static int selftest_remove(struct platform_device *pdev)
943{
944 struct device *dev = &pdev->dev;
945 struct device_node *np = dev->of_node;
946
947 dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
948 return 0;
949}
950
951static struct of_device_id selftest_match[] = {
952 { .compatible = "selftest", },
953 {},
954};
Pantelis Antoniou177d2712014-10-28 22:35:59 +0200955
956static struct platform_driver selftest_driver = {
957 .probe = selftest_probe,
958 .remove = selftest_remove,
959 .driver = {
960 .name = "selftest",
961 .owner = THIS_MODULE,
962 .of_match_table = of_match_ptr(selftest_match),
963 },
964};
965
966/* get the platform device instantiated at the path */
967static struct platform_device *of_path_to_platform_device(const char *path)
968{
969 struct device_node *np;
970 struct platform_device *pdev;
971
972 np = of_find_node_by_path(path);
973 if (np == NULL)
974 return NULL;
975
976 pdev = of_find_device_by_node(np);
977 of_node_put(np);
978
979 return pdev;
980}
981
982/* find out if a platform device exists at that path */
983static int of_path_platform_device_exists(const char *path)
984{
985 struct platform_device *pdev;
986
987 pdev = of_path_to_platform_device(path);
988 platform_device_put(pdev);
989 return pdev != NULL;
990}
991
Arnd Bergmann4252de32015-03-04 20:49:47 +0100992#if IS_BUILTIN(CONFIG_I2C)
Pantelis Antonioud5e75502015-01-12 19:02:49 +0200993
994/* get the i2c client device instantiated at the path */
995static struct i2c_client *of_path_to_i2c_client(const char *path)
Pantelis Antoniou177d2712014-10-28 22:35:59 +0200996{
Pantelis Antonioud5e75502015-01-12 19:02:49 +0200997 struct device_node *np;
998 struct i2c_client *client;
999
1000 np = of_find_node_by_path(path);
1001 if (np == NULL)
1002 return NULL;
1003
1004 client = of_find_i2c_device_by_node(np);
1005 of_node_put(np);
1006
1007 return client;
1008}
1009
1010/* find out if a i2c client device exists at that path */
1011static int of_path_i2c_client_exists(const char *path)
1012{
1013 struct i2c_client *client;
1014
1015 client = of_path_to_i2c_client(path);
1016 if (client)
1017 put_device(&client->dev);
1018 return client != NULL;
1019}
1020#else
1021static int of_path_i2c_client_exists(const char *path)
1022{
1023 return 0;
1024}
1025#endif
1026
1027enum overlay_type {
1028 PDEV_OVERLAY,
1029 I2C_OVERLAY
1030};
1031
1032static int of_path_device_type_exists(const char *path,
1033 enum overlay_type ovtype)
1034{
1035 switch (ovtype) {
1036 case PDEV_OVERLAY:
1037 return of_path_platform_device_exists(path);
1038 case I2C_OVERLAY:
1039 return of_path_i2c_client_exists(path);
1040 }
1041 return 0;
1042}
1043
1044static const char *selftest_path(int nr, enum overlay_type ovtype)
1045{
1046 const char *base;
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001047 static char buf[256];
1048
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001049 switch (ovtype) {
1050 case PDEV_OVERLAY:
1051 base = "/testcase-data/overlay-node/test-bus";
1052 break;
1053 case I2C_OVERLAY:
1054 base = "/testcase-data/overlay-node/test-bus/i2c-test-bus";
1055 break;
1056 default:
1057 buf[0] = '\0';
1058 return buf;
1059 }
1060 snprintf(buf, sizeof(buf) - 1, "%s/test-selftest%d", base, nr);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001061 buf[sizeof(buf) - 1] = '\0';
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001062 return buf;
1063}
1064
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001065static int of_selftest_device_exists(int selftest_nr, enum overlay_type ovtype)
1066{
1067 const char *path;
1068
1069 path = selftest_path(selftest_nr, ovtype);
1070
1071 switch (ovtype) {
1072 case PDEV_OVERLAY:
1073 return of_path_platform_device_exists(path);
1074 case I2C_OVERLAY:
1075 return of_path_i2c_client_exists(path);
1076 }
1077 return 0;
1078}
1079
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001080static const char *overlay_path(int nr)
1081{
1082 static char buf[256];
1083
1084 snprintf(buf, sizeof(buf) - 1,
1085 "/testcase-data/overlay%d", nr);
1086 buf[sizeof(buf) - 1] = '\0';
1087
1088 return buf;
1089}
1090
1091static const char *bus_path = "/testcase-data/overlay-node/test-bus";
1092
1093static int of_selftest_apply_overlay(int selftest_nr, int overlay_nr,
1094 int *overlay_id)
1095{
1096 struct device_node *np = NULL;
1097 int ret, id = -1;
1098
1099 np = of_find_node_by_path(overlay_path(overlay_nr));
1100 if (np == NULL) {
1101 selftest(0, "could not find overlay node @\"%s\"\n",
1102 overlay_path(overlay_nr));
1103 ret = -EINVAL;
1104 goto out;
1105 }
1106
1107 ret = of_overlay_create(np);
1108 if (ret < 0) {
1109 selftest(0, "could not create overlay from \"%s\"\n",
1110 overlay_path(overlay_nr));
1111 goto out;
1112 }
1113 id = ret;
1114
1115 ret = 0;
1116
1117out:
1118 of_node_put(np);
1119
1120 if (overlay_id)
1121 *overlay_id = id;
1122
1123 return ret;
1124}
1125
1126/* apply an overlay while checking before and after states */
1127static int of_selftest_apply_overlay_check(int overlay_nr, int selftest_nr,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001128 int before, int after, enum overlay_type ovtype)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001129{
1130 int ret;
1131
1132 /* selftest device must not be in before state */
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001133 if (of_selftest_device_exists(selftest_nr, ovtype) != before) {
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001134 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1135 overlay_path(overlay_nr),
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001136 selftest_path(selftest_nr, ovtype),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001137 !before ? "enabled" : "disabled");
1138 return -EINVAL;
1139 }
1140
1141 ret = of_selftest_apply_overlay(overlay_nr, selftest_nr, NULL);
1142 if (ret != 0) {
1143 /* of_selftest_apply_overlay already called selftest() */
1144 return ret;
1145 }
1146
1147 /* selftest device must be to set to after state */
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001148 if (of_selftest_device_exists(selftest_nr, ovtype) != after) {
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001149 selftest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n",
1150 overlay_path(overlay_nr),
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001151 selftest_path(selftest_nr, ovtype),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001152 !after ? "enabled" : "disabled");
1153 return -EINVAL;
1154 }
1155
1156 return 0;
1157}
1158
1159/* apply an overlay and then revert it while checking before, after states */
1160static int of_selftest_apply_revert_overlay_check(int overlay_nr,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001161 int selftest_nr, int before, int after,
1162 enum overlay_type ovtype)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001163{
1164 int ret, ov_id;
1165
1166 /* selftest device must be in before state */
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001167 if (of_selftest_device_exists(selftest_nr, ovtype) != before) {
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001168 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1169 overlay_path(overlay_nr),
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001170 selftest_path(selftest_nr, ovtype),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001171 !before ? "enabled" : "disabled");
1172 return -EINVAL;
1173 }
1174
1175 /* apply the overlay */
1176 ret = of_selftest_apply_overlay(overlay_nr, selftest_nr, &ov_id);
1177 if (ret != 0) {
1178 /* of_selftest_apply_overlay already called selftest() */
1179 return ret;
1180 }
1181
1182 /* selftest device must be in after state */
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001183 if (of_selftest_device_exists(selftest_nr, ovtype) != after) {
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001184 selftest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n",
1185 overlay_path(overlay_nr),
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001186 selftest_path(selftest_nr, ovtype),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001187 !after ? "enabled" : "disabled");
1188 return -EINVAL;
1189 }
1190
1191 ret = of_overlay_destroy(ov_id);
1192 if (ret != 0) {
1193 selftest(0, "overlay @\"%s\" failed to be destroyed @\"%s\"\n",
1194 overlay_path(overlay_nr),
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001195 selftest_path(selftest_nr, ovtype));
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001196 return ret;
1197 }
1198
1199 /* selftest device must be again in before state */
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001200 if (of_selftest_device_exists(selftest_nr, PDEV_OVERLAY) != before) {
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001201 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1202 overlay_path(overlay_nr),
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001203 selftest_path(selftest_nr, ovtype),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001204 !before ? "enabled" : "disabled");
1205 return -EINVAL;
1206 }
1207
1208 return 0;
1209}
1210
1211/* test activation of device */
1212static void of_selftest_overlay_0(void)
1213{
1214 int ret;
1215
1216 /* device should enable */
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001217 ret = of_selftest_apply_overlay_check(0, 0, 0, 1, PDEV_OVERLAY);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001218 if (ret != 0)
1219 return;
1220
1221 selftest(1, "overlay test %d passed\n", 0);
1222}
1223
1224/* test deactivation of device */
1225static void of_selftest_overlay_1(void)
1226{
1227 int ret;
1228
1229 /* device should disable */
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001230 ret = of_selftest_apply_overlay_check(1, 1, 1, 0, PDEV_OVERLAY);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001231 if (ret != 0)
1232 return;
1233
1234 selftest(1, "overlay test %d passed\n", 1);
1235}
1236
1237/* test activation of device */
1238static void of_selftest_overlay_2(void)
1239{
1240 int ret;
1241
1242 /* device should enable */
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001243 ret = of_selftest_apply_overlay_check(2, 2, 0, 1, PDEV_OVERLAY);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001244 if (ret != 0)
1245 return;
1246
1247 selftest(1, "overlay test %d passed\n", 2);
1248}
1249
1250/* test deactivation of device */
1251static void of_selftest_overlay_3(void)
1252{
1253 int ret;
1254
1255 /* device should disable */
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001256 ret = of_selftest_apply_overlay_check(3, 3, 1, 0, PDEV_OVERLAY);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001257 if (ret != 0)
1258 return;
1259
1260 selftest(1, "overlay test %d passed\n", 3);
1261}
1262
1263/* test activation of a full device node */
1264static void of_selftest_overlay_4(void)
1265{
1266 int ret;
1267
1268 /* device should disable */
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001269 ret = of_selftest_apply_overlay_check(4, 4, 0, 1, PDEV_OVERLAY);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001270 if (ret != 0)
1271 return;
1272
1273 selftest(1, "overlay test %d passed\n", 4);
1274}
1275
1276/* test overlay apply/revert sequence */
1277static void of_selftest_overlay_5(void)
1278{
1279 int ret;
1280
1281 /* device should disable */
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001282 ret = of_selftest_apply_revert_overlay_check(5, 5, 0, 1, PDEV_OVERLAY);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001283 if (ret != 0)
1284 return;
1285
1286 selftest(1, "overlay test %d passed\n", 5);
1287}
1288
1289/* test overlay application in sequence */
1290static void of_selftest_overlay_6(void)
1291{
1292 struct device_node *np;
1293 int ret, i, ov_id[2];
1294 int overlay_nr = 6, selftest_nr = 6;
1295 int before = 0, after = 1;
1296
1297 /* selftest device must be in before state */
1298 for (i = 0; i < 2; i++) {
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001299 if (of_selftest_device_exists(selftest_nr + i, PDEV_OVERLAY)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001300 != before) {
1301 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1302 overlay_path(overlay_nr + i),
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001303 selftest_path(selftest_nr + i,
1304 PDEV_OVERLAY),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001305 !before ? "enabled" : "disabled");
1306 return;
1307 }
1308 }
1309
1310 /* apply the overlays */
1311 for (i = 0; i < 2; i++) {
1312
1313 np = of_find_node_by_path(overlay_path(overlay_nr + i));
1314 if (np == NULL) {
1315 selftest(0, "could not find overlay node @\"%s\"\n",
1316 overlay_path(overlay_nr + i));
1317 return;
1318 }
1319
1320 ret = of_overlay_create(np);
1321 if (ret < 0) {
1322 selftest(0, "could not create overlay from \"%s\"\n",
1323 overlay_path(overlay_nr + i));
1324 return;
1325 }
1326 ov_id[i] = ret;
1327 }
1328
1329 for (i = 0; i < 2; i++) {
1330 /* selftest device must be in after state */
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001331 if (of_selftest_device_exists(selftest_nr + i, PDEV_OVERLAY)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001332 != after) {
1333 selftest(0, "overlay @\"%s\" failed @\"%s\" %s\n",
1334 overlay_path(overlay_nr + i),
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001335 selftest_path(selftest_nr + i,
1336 PDEV_OVERLAY),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001337 !after ? "enabled" : "disabled");
1338 return;
1339 }
1340 }
1341
1342 for (i = 1; i >= 0; i--) {
1343 ret = of_overlay_destroy(ov_id[i]);
1344 if (ret != 0) {
1345 selftest(0, "overlay @\"%s\" failed destroy @\"%s\"\n",
1346 overlay_path(overlay_nr + i),
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001347 selftest_path(selftest_nr + i,
1348 PDEV_OVERLAY));
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001349 return;
1350 }
1351 }
1352
1353 for (i = 0; i < 2; i++) {
1354 /* selftest device must be again in before state */
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001355 if (of_selftest_device_exists(selftest_nr + i, PDEV_OVERLAY)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001356 != before) {
1357 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1358 overlay_path(overlay_nr + i),
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001359 selftest_path(selftest_nr + i,
1360 PDEV_OVERLAY),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001361 !before ? "enabled" : "disabled");
1362 return;
1363 }
1364 }
1365
1366 selftest(1, "overlay test %d passed\n", 6);
1367}
1368
1369/* test overlay application in sequence */
1370static void of_selftest_overlay_8(void)
1371{
1372 struct device_node *np;
1373 int ret, i, ov_id[2];
1374 int overlay_nr = 8, selftest_nr = 8;
1375
1376 /* we don't care about device state in this test */
1377
1378 /* apply the overlays */
1379 for (i = 0; i < 2; i++) {
1380
1381 np = of_find_node_by_path(overlay_path(overlay_nr + i));
1382 if (np == NULL) {
1383 selftest(0, "could not find overlay node @\"%s\"\n",
1384 overlay_path(overlay_nr + i));
1385 return;
1386 }
1387
1388 ret = of_overlay_create(np);
1389 if (ret < 0) {
1390 selftest(0, "could not create overlay from \"%s\"\n",
1391 overlay_path(overlay_nr + i));
1392 return;
1393 }
1394 ov_id[i] = ret;
1395 }
1396
1397 /* now try to remove first overlay (it should fail) */
1398 ret = of_overlay_destroy(ov_id[0]);
1399 if (ret == 0) {
1400 selftest(0, "overlay @\"%s\" was destroyed @\"%s\"\n",
1401 overlay_path(overlay_nr + 0),
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001402 selftest_path(selftest_nr,
1403 PDEV_OVERLAY));
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001404 return;
1405 }
1406
1407 /* removing them in order should work */
1408 for (i = 1; i >= 0; i--) {
1409 ret = of_overlay_destroy(ov_id[i]);
1410 if (ret != 0) {
1411 selftest(0, "overlay @\"%s\" not destroyed @\"%s\"\n",
1412 overlay_path(overlay_nr + i),
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001413 selftest_path(selftest_nr,
1414 PDEV_OVERLAY));
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001415 return;
1416 }
1417 }
1418
1419 selftest(1, "overlay test %d passed\n", 8);
1420}
1421
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001422/* test insertion of a bus with parent devices */
1423static void of_selftest_overlay_10(void)
1424{
1425 int ret;
1426 char *child_path;
1427
1428 /* device should disable */
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001429 ret = of_selftest_apply_overlay_check(10, 10, 0, 1, PDEV_OVERLAY);
1430 if (selftest(ret == 0,
1431 "overlay test %d failed; overlay application\n", 10))
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001432 return;
1433
1434 child_path = kasprintf(GFP_KERNEL, "%s/test-selftest101",
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001435 selftest_path(10, PDEV_OVERLAY));
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001436 if (selftest(child_path, "overlay test %d failed; kasprintf\n", 10))
1437 return;
1438
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001439 ret = of_path_device_type_exists(child_path, PDEV_OVERLAY);
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001440 kfree(child_path);
1441 if (selftest(ret, "overlay test %d failed; no child device\n", 10))
1442 return;
1443}
1444
1445/* test insertion of a bus with parent devices (and revert) */
1446static void of_selftest_overlay_11(void)
1447{
1448 int ret;
1449
1450 /* device should disable */
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001451 ret = of_selftest_apply_revert_overlay_check(11, 11, 0, 1,
1452 PDEV_OVERLAY);
1453 if (selftest(ret == 0,
1454 "overlay test %d failed; overlay application\n", 11))
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001455 return;
1456}
1457
Arnd Bergmann4252de32015-03-04 20:49:47 +01001458#if IS_BUILTIN(CONFIG_I2C) && IS_ENABLED(CONFIG_OF_OVERLAY)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001459
1460struct selftest_i2c_bus_data {
1461 struct platform_device *pdev;
1462 struct i2c_adapter adap;
1463};
1464
1465static int selftest_i2c_master_xfer(struct i2c_adapter *adap,
1466 struct i2c_msg *msgs, int num)
1467{
1468 struct selftest_i2c_bus_data *std = i2c_get_adapdata(adap);
1469
1470 (void)std;
1471
1472 return num;
1473}
1474
1475static u32 selftest_i2c_functionality(struct i2c_adapter *adap)
1476{
1477 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
1478}
1479
1480static const struct i2c_algorithm selftest_i2c_algo = {
1481 .master_xfer = selftest_i2c_master_xfer,
1482 .functionality = selftest_i2c_functionality,
1483};
1484
1485static int selftest_i2c_bus_probe(struct platform_device *pdev)
1486{
1487 struct device *dev = &pdev->dev;
1488 struct device_node *np = dev->of_node;
1489 struct selftest_i2c_bus_data *std;
1490 struct i2c_adapter *adap;
1491 int ret;
1492
1493 if (np == NULL) {
1494 dev_err(dev, "No OF data for device\n");
1495 return -EINVAL;
1496
1497 }
1498
1499 dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
1500
1501 std = devm_kzalloc(dev, sizeof(*std), GFP_KERNEL);
1502 if (!std) {
1503 dev_err(dev, "Failed to allocate selftest i2c data\n");
1504 return -ENOMEM;
1505 }
1506
1507 /* link them together */
1508 std->pdev = pdev;
1509 platform_set_drvdata(pdev, std);
1510
1511 adap = &std->adap;
1512 i2c_set_adapdata(adap, std);
1513 adap->nr = -1;
1514 strlcpy(adap->name, pdev->name, sizeof(adap->name));
1515 adap->class = I2C_CLASS_DEPRECATED;
1516 adap->algo = &selftest_i2c_algo;
1517 adap->dev.parent = dev;
1518 adap->dev.of_node = dev->of_node;
1519 adap->timeout = 5 * HZ;
1520 adap->retries = 3;
1521
1522 ret = i2c_add_numbered_adapter(adap);
1523 if (ret != 0) {
1524 dev_err(dev, "Failed to add I2C adapter\n");
1525 return ret;
1526 }
1527
1528 return 0;
1529}
1530
1531static int selftest_i2c_bus_remove(struct platform_device *pdev)
1532{
1533 struct device *dev = &pdev->dev;
1534 struct device_node *np = dev->of_node;
1535 struct selftest_i2c_bus_data *std = platform_get_drvdata(pdev);
1536
1537 dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
1538 i2c_del_adapter(&std->adap);
1539
1540 return 0;
1541}
1542
1543static struct of_device_id selftest_i2c_bus_match[] = {
1544 { .compatible = "selftest-i2c-bus", },
1545 {},
1546};
1547
1548static struct platform_driver selftest_i2c_bus_driver = {
1549 .probe = selftest_i2c_bus_probe,
1550 .remove = selftest_i2c_bus_remove,
1551 .driver = {
1552 .name = "selftest-i2c-bus",
1553 .of_match_table = of_match_ptr(selftest_i2c_bus_match),
1554 },
1555};
1556
1557static int selftest_i2c_dev_probe(struct i2c_client *client,
1558 const struct i2c_device_id *id)
1559{
1560 struct device *dev = &client->dev;
1561 struct device_node *np = client->dev.of_node;
1562
1563 if (!np) {
1564 dev_err(dev, "No OF node\n");
1565 return -EINVAL;
1566 }
1567
1568 dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
1569
1570 return 0;
1571};
1572
1573static int selftest_i2c_dev_remove(struct i2c_client *client)
1574{
1575 struct device *dev = &client->dev;
1576 struct device_node *np = client->dev.of_node;
1577
1578 dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
1579 return 0;
1580}
1581
1582static const struct i2c_device_id selftest_i2c_dev_id[] = {
1583 { .name = "selftest-i2c-dev" },
1584 { }
1585};
1586
1587static struct i2c_driver selftest_i2c_dev_driver = {
1588 .driver = {
1589 .name = "selftest-i2c-dev",
1590 .owner = THIS_MODULE,
1591 },
1592 .probe = selftest_i2c_dev_probe,
1593 .remove = selftest_i2c_dev_remove,
1594 .id_table = selftest_i2c_dev_id,
1595};
1596
Arnd Bergmann4252de32015-03-04 20:49:47 +01001597#if IS_BUILTIN(CONFIG_I2C_MUX)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001598
1599struct selftest_i2c_mux_data {
1600 int nchans;
1601 struct i2c_adapter *adap[];
1602};
1603
1604static int selftest_i2c_mux_select_chan(struct i2c_adapter *adap,
1605 void *client, u32 chan)
1606{
1607 return 0;
1608}
1609
1610static int selftest_i2c_mux_probe(struct i2c_client *client,
1611 const struct i2c_device_id *id)
1612{
1613 int ret, i, nchans, size;
1614 struct device *dev = &client->dev;
1615 struct i2c_adapter *adap = to_i2c_adapter(dev->parent);
1616 struct device_node *np = client->dev.of_node, *child;
1617 struct selftest_i2c_mux_data *stm;
1618 u32 reg, max_reg;
1619
1620 dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
1621
1622 if (!np) {
1623 dev_err(dev, "No OF node\n");
1624 return -EINVAL;
1625 }
1626
1627 max_reg = (u32)-1;
1628 for_each_child_of_node(np, child) {
1629 ret = of_property_read_u32(child, "reg", &reg);
1630 if (ret)
1631 continue;
1632 if (max_reg == (u32)-1 || reg > max_reg)
1633 max_reg = reg;
1634 }
1635 nchans = max_reg == (u32)-1 ? 0 : max_reg + 1;
1636 if (nchans == 0) {
1637 dev_err(dev, "No channels\n");
1638 return -EINVAL;
1639 }
1640
1641 size = offsetof(struct selftest_i2c_mux_data, adap[nchans]);
1642 stm = devm_kzalloc(dev, size, GFP_KERNEL);
1643 if (!stm) {
1644 dev_err(dev, "Out of memory\n");
1645 return -ENOMEM;
1646 }
1647 stm->nchans = nchans;
1648 for (i = 0; i < nchans; i++) {
1649 stm->adap[i] = i2c_add_mux_adapter(adap, dev, client,
1650 0, i, 0, selftest_i2c_mux_select_chan, NULL);
1651 if (!stm->adap[i]) {
1652 dev_err(dev, "Failed to register mux #%d\n", i);
1653 for (i--; i >= 0; i--)
1654 i2c_del_mux_adapter(stm->adap[i]);
1655 return -ENODEV;
1656 }
1657 }
1658
1659 i2c_set_clientdata(client, stm);
1660
1661 return 0;
1662};
1663
1664static int selftest_i2c_mux_remove(struct i2c_client *client)
1665{
1666 struct device *dev = &client->dev;
1667 struct device_node *np = client->dev.of_node;
1668 struct selftest_i2c_mux_data *stm = i2c_get_clientdata(client);
1669 int i;
1670
1671 dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
1672 for (i = stm->nchans - 1; i >= 0; i--)
1673 i2c_del_mux_adapter(stm->adap[i]);
1674 return 0;
1675}
1676
1677static const struct i2c_device_id selftest_i2c_mux_id[] = {
1678 { .name = "selftest-i2c-mux" },
1679 { }
1680};
1681
1682static struct i2c_driver selftest_i2c_mux_driver = {
1683 .driver = {
1684 .name = "selftest-i2c-mux",
1685 .owner = THIS_MODULE,
1686 },
1687 .probe = selftest_i2c_mux_probe,
1688 .remove = selftest_i2c_mux_remove,
1689 .id_table = selftest_i2c_mux_id,
1690};
1691
1692#endif
1693
1694static int of_selftest_overlay_i2c_init(void)
1695{
1696 int ret;
1697
1698 ret = i2c_add_driver(&selftest_i2c_dev_driver);
1699 if (selftest(ret == 0,
1700 "could not register selftest i2c device driver\n"))
1701 return ret;
1702
1703 ret = platform_driver_register(&selftest_i2c_bus_driver);
1704 if (selftest(ret == 0,
1705 "could not register selftest i2c bus driver\n"))
1706 return ret;
1707
Arnd Bergmann4252de32015-03-04 20:49:47 +01001708#if IS_BUILTIN(CONFIG_I2C_MUX)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001709 ret = i2c_add_driver(&selftest_i2c_mux_driver);
1710 if (selftest(ret == 0,
1711 "could not register selftest i2c mux driver\n"))
1712 return ret;
1713#endif
1714
1715 return 0;
1716}
1717
1718static void of_selftest_overlay_i2c_cleanup(void)
1719{
Arnd Bergmann4252de32015-03-04 20:49:47 +01001720#if IS_BUILTIN(CONFIG_I2C_MUX)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001721 i2c_del_driver(&selftest_i2c_mux_driver);
1722#endif
1723 platform_driver_unregister(&selftest_i2c_bus_driver);
1724 i2c_del_driver(&selftest_i2c_dev_driver);
1725}
1726
1727static void of_selftest_overlay_i2c_12(void)
1728{
1729 int ret;
1730
1731 /* device should enable */
1732 ret = of_selftest_apply_overlay_check(12, 12, 0, 1, I2C_OVERLAY);
1733 if (ret != 0)
1734 return;
1735
1736 selftest(1, "overlay test %d passed\n", 12);
1737}
1738
1739/* test deactivation of device */
1740static void of_selftest_overlay_i2c_13(void)
1741{
1742 int ret;
1743
1744 /* device should disable */
1745 ret = of_selftest_apply_overlay_check(13, 13, 1, 0, I2C_OVERLAY);
1746 if (ret != 0)
1747 return;
1748
1749 selftest(1, "overlay test %d passed\n", 13);
1750}
1751
1752/* just check for i2c mux existence */
1753static void of_selftest_overlay_i2c_14(void)
1754{
1755}
1756
1757static void of_selftest_overlay_i2c_15(void)
1758{
1759 int ret;
1760
1761 /* device should enable */
1762 ret = of_selftest_apply_overlay_check(16, 15, 0, 1, I2C_OVERLAY);
1763 if (ret != 0)
1764 return;
1765
1766 selftest(1, "overlay test %d passed\n", 15);
1767}
1768
1769#else
1770
1771static inline void of_selftest_overlay_i2c_14(void) { }
1772static inline void of_selftest_overlay_i2c_15(void) { }
1773
1774#endif
1775
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001776static void __init of_selftest_overlay(void)
1777{
1778 struct device_node *bus_np = NULL;
1779 int ret;
1780
1781 ret = platform_driver_register(&selftest_driver);
1782 if (ret != 0) {
1783 selftest(0, "could not register selftest driver\n");
1784 goto out;
1785 }
1786
1787 bus_np = of_find_node_by_path(bus_path);
1788 if (bus_np == NULL) {
1789 selftest(0, "could not find bus_path \"%s\"\n", bus_path);
1790 goto out;
1791 }
1792
1793 ret = of_platform_populate(bus_np, of_default_bus_match_table,
1794 NULL, NULL);
1795 if (ret != 0) {
1796 selftest(0, "could not populate bus @ \"%s\"\n", bus_path);
1797 goto out;
1798 }
1799
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001800 if (!of_selftest_device_exists(100, PDEV_OVERLAY)) {
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001801 selftest(0, "could not find selftest0 @ \"%s\"\n",
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001802 selftest_path(100, PDEV_OVERLAY));
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001803 goto out;
1804 }
1805
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001806 if (of_selftest_device_exists(101, PDEV_OVERLAY)) {
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001807 selftest(0, "selftest1 @ \"%s\" should not exist\n",
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001808 selftest_path(101, PDEV_OVERLAY));
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001809 goto out;
1810 }
1811
1812 selftest(1, "basic infrastructure of overlays passed");
1813
1814 /* tests in sequence */
1815 of_selftest_overlay_0();
1816 of_selftest_overlay_1();
1817 of_selftest_overlay_2();
1818 of_selftest_overlay_3();
1819 of_selftest_overlay_4();
1820 of_selftest_overlay_5();
1821 of_selftest_overlay_6();
1822 of_selftest_overlay_8();
1823
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001824 of_selftest_overlay_10();
1825 of_selftest_overlay_11();
1826
Arnd Bergmann4252de32015-03-04 20:49:47 +01001827#if IS_BUILTIN(CONFIG_I2C)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001828 if (selftest(of_selftest_overlay_i2c_init() == 0, "i2c init failed\n"))
1829 goto out;
1830
1831 of_selftest_overlay_i2c_12();
1832 of_selftest_overlay_i2c_13();
1833 of_selftest_overlay_i2c_14();
1834 of_selftest_overlay_i2c_15();
1835
1836 of_selftest_overlay_i2c_cleanup();
1837#endif
1838
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001839out:
1840 of_node_put(bus_np);
1841}
1842
1843#else
1844static inline void __init of_selftest_overlay(void) { }
1845#endif
1846
Grant Likely53a42092011-12-12 09:25:57 -07001847static int __init of_selftest(void)
1848{
1849 struct device_node *np;
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001850 int res;
1851
1852 /* adding data for selftest */
1853 res = selftest_data_add();
1854 if (res)
1855 return res;
Grant Likely788ec2f2014-11-19 17:13:44 +00001856 if (!of_aliases)
1857 of_aliases = of_find_node_by_path("/aliases");
Grant Likely53a42092011-12-12 09:25:57 -07001858
1859 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
1860 if (!np) {
1861 pr_info("No testcase data in device tree; not running tests\n");
1862 return 0;
1863 }
1864 of_node_put(np);
1865
1866 pr_info("start of selftest - you will see error messages\n");
Grant Likelyf2051d62014-10-01 17:40:22 +01001867 of_selftest_check_tree_linkage();
Grant Likely841ec212014-10-02 13:09:15 +01001868 of_selftest_check_phandles();
Grant Likelyae91ff72014-03-14 13:53:10 +00001869 of_selftest_find_node_by_name();
Grant Likely7e66c5c2013-11-15 17:19:09 +00001870 of_selftest_dynamic();
Grant Likely53a42092011-12-12 09:25:57 -07001871 of_selftest_parse_phandle_with_args();
Grant Likelya87fa1d2014-11-03 15:15:35 +00001872 of_selftest_property_string();
Pantelis Antoniou69843392014-07-04 19:58:47 +03001873 of_selftest_property_copy();
Pantelis Antoniou201c9102014-07-04 19:58:49 +03001874 of_selftest_changeset();
Grant Likelya9f10ca2013-10-11 22:04:23 +01001875 of_selftest_parse_interrupts();
Grant Likely79d97012013-09-19 16:47:37 -05001876 of_selftest_parse_interrupts_extended();
Grant Likely1f42e5d2014-02-18 21:38:55 +00001877 of_selftest_match_node();
Rob Herring82c0f582014-04-23 17:57:40 -05001878 of_selftest_platform_populate();
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001879 of_selftest_overlay();
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001880
Grant Likelyf2051d62014-10-01 17:40:22 +01001881 /* Double check linkage after removing testcase data */
1882 of_selftest_check_tree_linkage();
1883
1884 pr_info("end of selftest - %i passed, %i failed\n",
1885 selftest_results.passed, selftest_results.failed);
1886
Grant Likely53a42092011-12-12 09:25:57 -07001887 return 0;
1888}
1889late_initcall(of_selftest);