blob: 276d2ada16e93241977689191dac0a47ed73b690 [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
Brian Norris5ca1b0d2015-03-17 12:30:32 -0700100 np = of_find_node_opts_by_path("/testcase-data/testcase-device1:test/option", &options);
101 selftest(np && !strcmp("test/option", options),
102 "option path test, subcase #2 failed\n");
103 of_node_put(np);
104
Leif Lindholm75c28c02014-11-28 11:34:28 +0000105 np = of_find_node_opts_by_path("/testcase-data:testoption", NULL);
106 selftest(np, "NULL option path test failed\n");
107 of_node_put(np);
108
109 np = of_find_node_opts_by_path("testcase-alias:testaliasoption",
110 &options);
111 selftest(np && !strcmp("testaliasoption", options),
112 "option alias path test failed\n");
113 of_node_put(np);
114
Peter Hurley8cbba1a2015-03-06 13:59:59 -0500115 np = of_find_node_opts_by_path("testcase-alias:test/alias/option",
116 &options);
117 selftest(np && !strcmp("test/alias/option", options),
118 "option alias path test, subcase #1 failed\n");
119 of_node_put(np);
120
Leif Lindholm75c28c02014-11-28 11:34:28 +0000121 np = of_find_node_opts_by_path("testcase-alias:testaliasoption", NULL);
122 selftest(np, "NULL option alias path test failed\n");
123 of_node_put(np);
124
125 options = "testoption";
126 np = of_find_node_opts_by_path("testcase-alias", &options);
127 selftest(np && !options, "option clearing test failed\n");
128 of_node_put(np);
129
130 options = "testoption";
131 np = of_find_node_opts_by_path("/", &options);
132 selftest(np && !options, "option clearing root node test failed\n");
133 of_node_put(np);
Grant Likelyae91ff72014-03-14 13:53:10 +0000134}
135
Grant Likely7e66c5c2013-11-15 17:19:09 +0000136static void __init of_selftest_dynamic(void)
137{
138 struct device_node *np;
139 struct property *prop;
140
141 np = of_find_node_by_path("/testcase-data");
142 if (!np) {
143 pr_err("missing testcase data\n");
144 return;
145 }
146
147 /* Array of 4 properties for the purpose of testing */
148 prop = kzalloc(sizeof(*prop) * 4, GFP_KERNEL);
149 if (!prop) {
150 selftest(0, "kzalloc() failed\n");
151 return;
152 }
153
154 /* Add a new property - should pass*/
155 prop->name = "new-property";
156 prop->value = "new-property-data";
157 prop->length = strlen(prop->value);
158 selftest(of_add_property(np, prop) == 0, "Adding a new property failed\n");
159
160 /* Try to add an existing property - should fail */
161 prop++;
162 prop->name = "new-property";
163 prop->value = "new-property-data-should-fail";
164 prop->length = strlen(prop->value);
165 selftest(of_add_property(np, prop) != 0,
166 "Adding an existing property should have failed\n");
167
168 /* Try to modify an existing property - should pass */
169 prop->value = "modify-property-data-should-pass";
170 prop->length = strlen(prop->value);
171 selftest(of_update_property(np, prop) == 0,
172 "Updating an existing property should have passed\n");
173
174 /* Try to modify non-existent property - should pass*/
175 prop++;
176 prop->name = "modify-property";
177 prop->value = "modify-missing-property-data-should-pass";
178 prop->length = strlen(prop->value);
179 selftest(of_update_property(np, prop) == 0,
180 "Updating a missing property should have passed\n");
181
182 /* Remove property - should pass */
183 selftest(of_remove_property(np, prop) == 0,
184 "Removing a property should have passed\n");
185
186 /* Adding very large property - should pass */
187 prop++;
188 prop->name = "large-property-PAGE_SIZEx8";
189 prop->length = PAGE_SIZE * 8;
190 prop->value = kzalloc(prop->length, GFP_KERNEL);
191 selftest(prop->value != NULL, "Unable to allocate large buffer\n");
192 if (prop->value)
193 selftest(of_add_property(np, prop) == 0,
194 "Adding a large property should have passed\n");
195}
196
Grant Likelyf2051d62014-10-01 17:40:22 +0100197static int __init of_selftest_check_node_linkage(struct device_node *np)
198{
Grant Likely5063e252014-10-03 16:28:27 +0100199 struct device_node *child;
Grant Likelyf2051d62014-10-01 17:40:22 +0100200 int count = 0, rc;
201
202 for_each_child_of_node(np, child) {
203 if (child->parent != np) {
204 pr_err("Child node %s links to wrong parent %s\n",
205 child->name, np->name);
206 return -EINVAL;
207 }
208
Grant Likelyf2051d62014-10-01 17:40:22 +0100209 rc = of_selftest_check_node_linkage(child);
210 if (rc < 0)
211 return rc;
212 count += rc;
213 }
214
215 return count + 1;
216}
217
218static void __init of_selftest_check_tree_linkage(void)
219{
220 struct device_node *np;
221 int allnode_count = 0, child_count;
222
Grant Likely5063e252014-10-03 16:28:27 +0100223 if (!of_root)
Grant Likelyf2051d62014-10-01 17:40:22 +0100224 return;
225
226 for_each_of_allnodes(np)
227 allnode_count++;
Grant Likely5063e252014-10-03 16:28:27 +0100228 child_count = of_selftest_check_node_linkage(of_root);
Grant Likelyf2051d62014-10-01 17:40:22 +0100229
230 selftest(child_count > 0, "Device node data structure is corrupted\n");
Frank Rowanda6bb1212015-03-13 23:59:01 -0700231 selftest(child_count == allnode_count,
232 "allnodes list size (%i) doesn't match sibling lists size (%i)\n",
233 allnode_count, child_count);
Grant Likelyf2051d62014-10-01 17:40:22 +0100234 pr_debug("allnodes list size (%i); sibling lists size (%i)\n", allnode_count, child_count);
235}
236
Grant Likely841ec212014-10-02 13:09:15 +0100237struct node_hash {
238 struct hlist_node node;
239 struct device_node *np;
240};
241
Grant Likely2118f4b2014-10-07 11:30:31 +0100242static DEFINE_HASHTABLE(phandle_ht, 8);
Grant Likely841ec212014-10-02 13:09:15 +0100243static void __init of_selftest_check_phandles(void)
244{
245 struct device_node *np;
246 struct node_hash *nh;
247 struct hlist_node *tmp;
248 int i, dup_count = 0, phandle_count = 0;
Grant Likely841ec212014-10-02 13:09:15 +0100249
Grant Likely841ec212014-10-02 13:09:15 +0100250 for_each_of_allnodes(np) {
251 if (!np->phandle)
252 continue;
253
Grant Likely2118f4b2014-10-07 11:30:31 +0100254 hash_for_each_possible(phandle_ht, nh, node, np->phandle) {
Grant Likely841ec212014-10-02 13:09:15 +0100255 if (nh->np->phandle == np->phandle) {
256 pr_info("Duplicate phandle! %i used by %s and %s\n",
257 np->phandle, nh->np->full_name, np->full_name);
258 dup_count++;
259 break;
260 }
261 }
262
263 nh = kzalloc(sizeof(*nh), GFP_KERNEL);
264 if (WARN_ON(!nh))
265 return;
266
267 nh->np = np;
Grant Likely2118f4b2014-10-07 11:30:31 +0100268 hash_add(phandle_ht, &nh->node, np->phandle);
Grant Likely841ec212014-10-02 13:09:15 +0100269 phandle_count++;
270 }
271 selftest(dup_count == 0, "Found %i duplicates in %i phandles\n",
272 dup_count, phandle_count);
273
274 /* Clean up */
Grant Likely2118f4b2014-10-07 11:30:31 +0100275 hash_for_each_safe(phandle_ht, i, tmp, nh, node) {
Grant Likely841ec212014-10-02 13:09:15 +0100276 hash_del(&nh->node);
277 kfree(nh);
278 }
279}
280
Grant Likely53a42092011-12-12 09:25:57 -0700281static void __init of_selftest_parse_phandle_with_args(void)
282{
283 struct device_node *np;
284 struct of_phandle_args args;
Grant Likelycabb7d52013-02-12 21:19:37 +0000285 int i, rc;
Grant Likely53a42092011-12-12 09:25:57 -0700286
Grant Likely53a42092011-12-12 09:25:57 -0700287 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
288 if (!np) {
289 pr_err("missing testcase data\n");
290 return;
291 }
292
Grant Likelybd69f732013-02-10 22:57:21 +0000293 rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
294 selftest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc);
295
Grant Likelyf7f951c2013-02-12 17:41:22 +0000296 for (i = 0; i < 8; i++) {
Grant Likely53a42092011-12-12 09:25:57 -0700297 bool passed = true;
298 rc = of_parse_phandle_with_args(np, "phandle-list",
299 "#phandle-cells", i, &args);
300
301 /* Test the values from tests-phandle.dtsi */
302 switch (i) {
303 case 0:
304 passed &= !rc;
305 passed &= (args.args_count == 1);
306 passed &= (args.args[0] == (i + 1));
307 break;
308 case 1:
309 passed &= !rc;
310 passed &= (args.args_count == 2);
311 passed &= (args.args[0] == (i + 1));
312 passed &= (args.args[1] == 0);
313 break;
314 case 2:
315 passed &= (rc == -ENOENT);
316 break;
317 case 3:
318 passed &= !rc;
319 passed &= (args.args_count == 3);
320 passed &= (args.args[0] == (i + 1));
321 passed &= (args.args[1] == 4);
322 passed &= (args.args[2] == 3);
323 break;
324 case 4:
325 passed &= !rc;
326 passed &= (args.args_count == 2);
327 passed &= (args.args[0] == (i + 1));
328 passed &= (args.args[1] == 100);
329 break;
330 case 5:
331 passed &= !rc;
332 passed &= (args.args_count == 0);
333 break;
334 case 6:
335 passed &= !rc;
336 passed &= (args.args_count == 1);
337 passed &= (args.args[0] == (i + 1));
338 break;
339 case 7:
Grant Likelycabb7d52013-02-12 21:19:37 +0000340 passed &= (rc == -ENOENT);
Grant Likely53a42092011-12-12 09:25:57 -0700341 break;
342 default:
343 passed = false;
344 }
345
Grant Likelycabb7d52013-02-12 21:19:37 +0000346 selftest(passed, "index %i - data error on node %s rc=%i\n",
347 i, args.np->full_name, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700348 }
349
350 /* Check for missing list property */
351 rc = of_parse_phandle_with_args(np, "phandle-list-missing",
352 "#phandle-cells", 0, &args);
Grant Likelycabb7d52013-02-12 21:19:37 +0000353 selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000354 rc = of_count_phandle_with_args(np, "phandle-list-missing",
355 "#phandle-cells");
356 selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700357
358 /* Check for missing cells property */
359 rc = of_parse_phandle_with_args(np, "phandle-list",
360 "#phandle-cells-missing", 0, &args);
Grant Likelycabb7d52013-02-12 21:19:37 +0000361 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000362 rc = of_count_phandle_with_args(np, "phandle-list",
363 "#phandle-cells-missing");
364 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700365
366 /* Check for bad phandle in list */
367 rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle",
368 "#phandle-cells", 0, &args);
Grant Likelycabb7d52013-02-12 21:19:37 +0000369 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000370 rc = of_count_phandle_with_args(np, "phandle-list-bad-phandle",
371 "#phandle-cells");
372 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700373
374 /* Check for incorrectly formed argument list */
375 rc = of_parse_phandle_with_args(np, "phandle-list-bad-args",
376 "#phandle-cells", 1, &args);
Grant Likelycabb7d52013-02-12 21:19:37 +0000377 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000378 rc = of_count_phandle_with_args(np, "phandle-list-bad-args",
379 "#phandle-cells");
380 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700381}
382
Grant Likelya87fa1d2014-11-03 15:15:35 +0000383static void __init of_selftest_property_string(void)
Grant Likely7aff0fe2011-12-12 09:25:58 -0700384{
Grant Likelya87fa1d2014-11-03 15:15:35 +0000385 const char *strings[4];
Grant Likely7aff0fe2011-12-12 09:25:58 -0700386 struct device_node *np;
387 int rc;
388
Grant Likely7aff0fe2011-12-12 09:25:58 -0700389 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
390 if (!np) {
391 pr_err("No testcase data in device tree\n");
392 return;
393 }
394
395 rc = of_property_match_string(np, "phandle-list-names", "first");
396 selftest(rc == 0, "first expected:0 got:%i\n", rc);
397 rc = of_property_match_string(np, "phandle-list-names", "second");
Wang Long649022e2015-03-03 03:50:38 +0000398 selftest(rc == 1, "second expected:1 got:%i\n", rc);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700399 rc = of_property_match_string(np, "phandle-list-names", "third");
Wang Long649022e2015-03-03 03:50:38 +0000400 selftest(rc == 2, "third expected:2 got:%i\n", rc);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700401 rc = of_property_match_string(np, "phandle-list-names", "fourth");
Grant Likelya87fa1d2014-11-03 15:15:35 +0000402 selftest(rc == -ENODATA, "unmatched string; rc=%i\n", rc);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700403 rc = of_property_match_string(np, "missing-property", "blah");
Grant Likelya87fa1d2014-11-03 15:15:35 +0000404 selftest(rc == -EINVAL, "missing property; rc=%i\n", rc);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700405 rc = of_property_match_string(np, "empty-property", "blah");
Grant Likelya87fa1d2014-11-03 15:15:35 +0000406 selftest(rc == -ENODATA, "empty property; rc=%i\n", rc);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700407 rc = of_property_match_string(np, "unterminated-string", "blah");
Grant Likelya87fa1d2014-11-03 15:15:35 +0000408 selftest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
409
410 /* of_property_count_strings() tests */
411 rc = of_property_count_strings(np, "string-property");
412 selftest(rc == 1, "Incorrect string count; rc=%i\n", rc);
413 rc = of_property_count_strings(np, "phandle-list-names");
414 selftest(rc == 3, "Incorrect string count; rc=%i\n", rc);
415 rc = of_property_count_strings(np, "unterminated-string");
416 selftest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
417 rc = of_property_count_strings(np, "unterminated-string-list");
418 selftest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
419
420 /* of_property_read_string_index() tests */
421 rc = of_property_read_string_index(np, "string-property", 0, strings);
422 selftest(rc == 0 && !strcmp(strings[0], "foobar"), "of_property_read_string_index() failure; rc=%i\n", rc);
423 strings[0] = NULL;
424 rc = of_property_read_string_index(np, "string-property", 1, strings);
425 selftest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
426 rc = of_property_read_string_index(np, "phandle-list-names", 0, strings);
427 selftest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
428 rc = of_property_read_string_index(np, "phandle-list-names", 1, strings);
429 selftest(rc == 0 && !strcmp(strings[0], "second"), "of_property_read_string_index() failure; rc=%i\n", rc);
430 rc = of_property_read_string_index(np, "phandle-list-names", 2, strings);
431 selftest(rc == 0 && !strcmp(strings[0], "third"), "of_property_read_string_index() failure; rc=%i\n", rc);
432 strings[0] = NULL;
433 rc = of_property_read_string_index(np, "phandle-list-names", 3, strings);
434 selftest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
435 strings[0] = NULL;
436 rc = of_property_read_string_index(np, "unterminated-string", 0, strings);
437 selftest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
438 rc = of_property_read_string_index(np, "unterminated-string-list", 0, strings);
439 selftest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
440 strings[0] = NULL;
441 rc = of_property_read_string_index(np, "unterminated-string-list", 2, strings); /* should fail */
442 selftest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
443 strings[1] = NULL;
444
445 /* of_property_read_string_array() tests */
446 rc = of_property_read_string_array(np, "string-property", strings, 4);
447 selftest(rc == 1, "Incorrect string count; rc=%i\n", rc);
448 rc = of_property_read_string_array(np, "phandle-list-names", strings, 4);
449 selftest(rc == 3, "Incorrect string count; rc=%i\n", rc);
450 rc = of_property_read_string_array(np, "unterminated-string", strings, 4);
451 selftest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
452 /* -- An incorrectly formed string should cause a failure */
453 rc = of_property_read_string_array(np, "unterminated-string-list", strings, 4);
454 selftest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
455 /* -- parsing the correctly formed strings should still work: */
456 strings[2] = NULL;
457 rc = of_property_read_string_array(np, "unterminated-string-list", strings, 2);
458 selftest(rc == 2 && strings[2] == NULL, "of_property_read_string_array() failure; rc=%i\n", rc);
459 strings[1] = NULL;
460 rc = of_property_read_string_array(np, "phandle-list-names", strings, 1);
461 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 -0700462}
463
Pantelis Antoniou69843392014-07-04 19:58:47 +0300464#define propcmp(p1, p2) (((p1)->length == (p2)->length) && \
465 (p1)->value && (p2)->value && \
466 !memcmp((p1)->value, (p2)->value, (p1)->length) && \
467 !strcmp((p1)->name, (p2)->name))
468static void __init of_selftest_property_copy(void)
469{
470#ifdef CONFIG_OF_DYNAMIC
471 struct property p1 = { .name = "p1", .length = 0, .value = "" };
472 struct property p2 = { .name = "p2", .length = 5, .value = "abcd" };
473 struct property *new;
474
475 new = __of_prop_dup(&p1, GFP_KERNEL);
476 selftest(new && propcmp(&p1, new), "empty property didn't copy correctly\n");
477 kfree(new->value);
478 kfree(new->name);
479 kfree(new);
480
481 new = __of_prop_dup(&p2, GFP_KERNEL);
482 selftest(new && propcmp(&p2, new), "non-empty property didn't copy correctly\n");
483 kfree(new->value);
484 kfree(new->name);
485 kfree(new);
486#endif
487}
488
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300489static void __init of_selftest_changeset(void)
490{
491#ifdef CONFIG_OF_DYNAMIC
492 struct property *ppadd, padd = { .name = "prop-add", .length = 0, .value = "" };
493 struct property *ppupdate, pupdate = { .name = "prop-update", .length = 5, .value = "abcd" };
494 struct property *ppremove;
Grant Likelye5179582014-11-17 22:31:32 +0000495 struct device_node *n1, *n2, *n21, *nremove, *parent, *np;
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300496 struct of_changeset chgset;
497
Grant Likelye5179582014-11-17 22:31:32 +0000498 n1 = __of_node_dup(NULL, "/testcase-data/changeset/n1");
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300499 selftest(n1, "testcase setup failure\n");
Grant Likelye5179582014-11-17 22:31:32 +0000500 n2 = __of_node_dup(NULL, "/testcase-data/changeset/n2");
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300501 selftest(n2, "testcase setup failure\n");
Grant Likelye5179582014-11-17 22:31:32 +0000502 n21 = __of_node_dup(NULL, "%s/%s", "/testcase-data/changeset/n2", "n21");
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300503 selftest(n21, "testcase setup failure %p\n", n21);
504 nremove = of_find_node_by_path("/testcase-data/changeset/node-remove");
505 selftest(nremove, "testcase setup failure\n");
506 ppadd = __of_prop_dup(&padd, GFP_KERNEL);
507 selftest(ppadd, "testcase setup failure\n");
508 ppupdate = __of_prop_dup(&pupdate, GFP_KERNEL);
509 selftest(ppupdate, "testcase setup failure\n");
510 parent = nremove->parent;
511 n1->parent = parent;
512 n2->parent = parent;
513 n21->parent = n2;
514 n2->child = n21;
515 ppremove = of_find_property(parent, "prop-remove", NULL);
516 selftest(ppremove, "failed to find removal prop");
517
518 of_changeset_init(&chgset);
519 selftest(!of_changeset_attach_node(&chgset, n1), "fail attach n1\n");
520 selftest(!of_changeset_attach_node(&chgset, n2), "fail attach n2\n");
521 selftest(!of_changeset_detach_node(&chgset, nremove), "fail remove node\n");
522 selftest(!of_changeset_attach_node(&chgset, n21), "fail attach n21\n");
523 selftest(!of_changeset_add_property(&chgset, parent, ppadd), "fail add prop\n");
524 selftest(!of_changeset_update_property(&chgset, parent, ppupdate), "fail update prop\n");
525 selftest(!of_changeset_remove_property(&chgset, parent, ppremove), "fail remove prop\n");
526 mutex_lock(&of_mutex);
527 selftest(!of_changeset_apply(&chgset), "apply failed\n");
528 mutex_unlock(&of_mutex);
529
Grant Likelye5179582014-11-17 22:31:32 +0000530 /* Make sure node names are constructed correctly */
531 selftest((np = of_find_node_by_path("/testcase-data/changeset/n2/n21")),
532 "'%s' not added\n", n21->full_name);
Markus Elfringc46ca3c2014-12-02 13:54:00 +0100533 of_node_put(np);
Grant Likelye5179582014-11-17 22:31:32 +0000534
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300535 mutex_lock(&of_mutex);
536 selftest(!of_changeset_revert(&chgset), "revert failed\n");
537 mutex_unlock(&of_mutex);
538
539 of_changeset_destroy(&chgset);
540#endif
541}
542
Grant Likelya9f10ca2013-10-11 22:04:23 +0100543static void __init of_selftest_parse_interrupts(void)
544{
545 struct device_node *np;
546 struct of_phandle_args args;
547 int i, rc;
548
549 np = of_find_node_by_path("/testcase-data/interrupts/interrupts0");
550 if (!np) {
551 pr_err("missing testcase data\n");
552 return;
553 }
554
555 for (i = 0; i < 4; i++) {
556 bool passed = true;
557 args.args_count = 0;
558 rc = of_irq_parse_one(np, i, &args);
559
560 passed &= !rc;
561 passed &= (args.args_count == 1);
562 passed &= (args.args[0] == (i + 1));
563
564 selftest(passed, "index %i - data error on node %s rc=%i\n",
565 i, args.np->full_name, rc);
566 }
567 of_node_put(np);
568
569 np = of_find_node_by_path("/testcase-data/interrupts/interrupts1");
570 if (!np) {
571 pr_err("missing testcase data\n");
572 return;
573 }
574
575 for (i = 0; i < 4; i++) {
576 bool passed = true;
577 args.args_count = 0;
578 rc = of_irq_parse_one(np, i, &args);
579
580 /* Test the values from tests-phandle.dtsi */
581 switch (i) {
582 case 0:
583 passed &= !rc;
584 passed &= (args.args_count == 1);
585 passed &= (args.args[0] == 9);
586 break;
587 case 1:
588 passed &= !rc;
589 passed &= (args.args_count == 3);
590 passed &= (args.args[0] == 10);
591 passed &= (args.args[1] == 11);
592 passed &= (args.args[2] == 12);
593 break;
594 case 2:
595 passed &= !rc;
596 passed &= (args.args_count == 2);
597 passed &= (args.args[0] == 13);
598 passed &= (args.args[1] == 14);
599 break;
600 case 3:
601 passed &= !rc;
602 passed &= (args.args_count == 2);
603 passed &= (args.args[0] == 15);
604 passed &= (args.args[1] == 16);
605 break;
606 default:
607 passed = false;
608 }
609 selftest(passed, "index %i - data error on node %s rc=%i\n",
610 i, args.np->full_name, rc);
611 }
612 of_node_put(np);
613}
614
Grant Likely79d97012013-09-19 16:47:37 -0500615static void __init of_selftest_parse_interrupts_extended(void)
616{
617 struct device_node *np;
618 struct of_phandle_args args;
619 int i, rc;
620
621 np = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
622 if (!np) {
623 pr_err("missing testcase data\n");
624 return;
625 }
626
627 for (i = 0; i < 7; i++) {
628 bool passed = true;
629 rc = of_irq_parse_one(np, i, &args);
630
631 /* Test the values from tests-phandle.dtsi */
632 switch (i) {
633 case 0:
634 passed &= !rc;
635 passed &= (args.args_count == 1);
636 passed &= (args.args[0] == 1);
637 break;
638 case 1:
639 passed &= !rc;
640 passed &= (args.args_count == 3);
641 passed &= (args.args[0] == 2);
642 passed &= (args.args[1] == 3);
643 passed &= (args.args[2] == 4);
644 break;
645 case 2:
646 passed &= !rc;
647 passed &= (args.args_count == 2);
648 passed &= (args.args[0] == 5);
649 passed &= (args.args[1] == 6);
650 break;
651 case 3:
652 passed &= !rc;
653 passed &= (args.args_count == 1);
654 passed &= (args.args[0] == 9);
655 break;
656 case 4:
657 passed &= !rc;
658 passed &= (args.args_count == 3);
659 passed &= (args.args[0] == 10);
660 passed &= (args.args[1] == 11);
661 passed &= (args.args[2] == 12);
662 break;
663 case 5:
664 passed &= !rc;
665 passed &= (args.args_count == 2);
666 passed &= (args.args[0] == 13);
667 passed &= (args.args[1] == 14);
668 break;
669 case 6:
670 passed &= !rc;
671 passed &= (args.args_count == 1);
672 passed &= (args.args[0] == 15);
673 break;
674 default:
675 passed = false;
676 }
677
678 selftest(passed, "index %i - data error on node %s rc=%i\n",
679 i, args.np->full_name, rc);
680 }
681 of_node_put(np);
682}
683
Frank Rowandafaed7a2015-03-14 00:00:36 -0700684static const struct of_device_id match_node_table[] = {
Grant Likely1f42e5d2014-02-18 21:38:55 +0000685 { .data = "A", .name = "name0", }, /* Name alone is lowest priority */
686 { .data = "B", .type = "type1", }, /* followed by type alone */
687
688 { .data = "Ca", .name = "name2", .type = "type1", }, /* followed by both together */
689 { .data = "Cb", .name = "name2", }, /* Only match when type doesn't match */
690 { .data = "Cc", .name = "name2", .type = "type2", },
691
692 { .data = "E", .compatible = "compat3" },
693 { .data = "G", .compatible = "compat2", },
694 { .data = "H", .compatible = "compat2", .name = "name5", },
695 { .data = "I", .compatible = "compat2", .type = "type1", },
696 { .data = "J", .compatible = "compat2", .type = "type1", .name = "name8", },
697 { .data = "K", .compatible = "compat2", .name = "name9", },
698 {}
699};
700
701static struct {
702 const char *path;
703 const char *data;
704} match_node_tests[] = {
705 { .path = "/testcase-data/match-node/name0", .data = "A", },
706 { .path = "/testcase-data/match-node/name1", .data = "B", },
707 { .path = "/testcase-data/match-node/a/name2", .data = "Ca", },
708 { .path = "/testcase-data/match-node/b/name2", .data = "Cb", },
709 { .path = "/testcase-data/match-node/c/name2", .data = "Cc", },
710 { .path = "/testcase-data/match-node/name3", .data = "E", },
711 { .path = "/testcase-data/match-node/name4", .data = "G", },
712 { .path = "/testcase-data/match-node/name5", .data = "H", },
713 { .path = "/testcase-data/match-node/name6", .data = "G", },
714 { .path = "/testcase-data/match-node/name7", .data = "I", },
715 { .path = "/testcase-data/match-node/name8", .data = "J", },
716 { .path = "/testcase-data/match-node/name9", .data = "K", },
717};
718
719static void __init of_selftest_match_node(void)
720{
721 struct device_node *np;
722 const struct of_device_id *match;
723 int i;
724
725 for (i = 0; i < ARRAY_SIZE(match_node_tests); i++) {
726 np = of_find_node_by_path(match_node_tests[i].path);
727 if (!np) {
728 selftest(0, "missing testcase node %s\n",
729 match_node_tests[i].path);
730 continue;
731 }
732
733 match = of_match_node(match_node_table, np);
734 if (!match) {
735 selftest(0, "%s didn't match anything\n",
736 match_node_tests[i].path);
737 continue;
738 }
739
740 if (strcmp(match->data, match_node_tests[i].data) != 0) {
741 selftest(0, "%s got wrong match. expected %s, got %s\n",
742 match_node_tests[i].path, match_node_tests[i].data,
743 (const char *)match->data);
744 continue;
745 }
746 selftest(1, "passed");
747 }
748}
749
Grant Likely851da972014-11-04 13:14:13 +0000750struct device test_bus = {
751 .init_name = "unittest-bus",
752};
Rob Herring82c0f582014-04-23 17:57:40 -0500753static void __init of_selftest_platform_populate(void)
754{
Grant Likely851da972014-11-04 13:14:13 +0000755 int irq, rc;
756 struct device_node *np, *child, *grandchild;
Rob Herring82c0f582014-04-23 17:57:40 -0500757 struct platform_device *pdev;
Frank Rowandafaed7a2015-03-14 00:00:36 -0700758 const struct of_device_id match[] = {
Rob Herringfb2caa52014-05-13 10:07:54 -0500759 { .compatible = "test-device", },
760 {}
761 };
Rob Herring82c0f582014-04-23 17:57:40 -0500762
763 np = of_find_node_by_path("/testcase-data");
764 of_platform_populate(np, of_default_bus_match_table, NULL, NULL);
765
766 /* Test that a missing irq domain returns -EPROBE_DEFER */
767 np = of_find_node_by_path("/testcase-data/testcase-device1");
768 pdev = of_find_device_by_node(np);
Rob Herring7d1cdc82014-05-13 10:07:29 -0500769 selftest(pdev, "device 1 creation failed\n");
770
Rob Herring82c0f582014-04-23 17:57:40 -0500771 irq = platform_get_irq(pdev, 0);
Rob Herring7d1cdc82014-05-13 10:07:29 -0500772 selftest(irq == -EPROBE_DEFER, "device deferred probe failed - %d\n", irq);
Rob Herring82c0f582014-04-23 17:57:40 -0500773
774 /* Test that a parsing failure does not return -EPROBE_DEFER */
775 np = of_find_node_by_path("/testcase-data/testcase-device2");
776 pdev = of_find_device_by_node(np);
Rob Herring7d1cdc82014-05-13 10:07:29 -0500777 selftest(pdev, "device 2 creation failed\n");
Rob Herring82c0f582014-04-23 17:57:40 -0500778 irq = platform_get_irq(pdev, 0);
Rob Herring7d1cdc82014-05-13 10:07:29 -0500779 selftest(irq < 0 && irq != -EPROBE_DEFER, "device parsing error failed - %d\n", irq);
Rob Herring82c0f582014-04-23 17:57:40 -0500780
Grant Likely851da972014-11-04 13:14:13 +0000781 if (selftest(np = of_find_node_by_path("/testcase-data/platform-tests"),
782 "No testcase data in device tree\n"));
Rob Herringfb2caa52014-05-13 10:07:54 -0500783 return;
Grant Likely851da972014-11-04 13:14:13 +0000784
785 if (selftest(!(rc = device_register(&test_bus)),
786 "testbus registration failed; rc=%i\n", rc));
787 return;
Rob Herringfb2caa52014-05-13 10:07:54 -0500788
789 for_each_child_of_node(np, child) {
Grant Likely851da972014-11-04 13:14:13 +0000790 of_platform_populate(child, match, NULL, &test_bus);
Rob Herringfb2caa52014-05-13 10:07:54 -0500791 for_each_child_of_node(child, grandchild)
792 selftest(of_find_device_by_node(grandchild),
793 "Could not create device for node '%s'\n",
794 grandchild->name);
795 }
Grant Likely851da972014-11-04 13:14:13 +0000796
797 of_platform_depopulate(&test_bus);
798 for_each_child_of_node(np, child) {
799 for_each_child_of_node(child, grandchild)
800 selftest(!of_find_device_by_node(grandchild),
801 "device didn't get destroyed '%s'\n",
802 grandchild->name);
803 }
804
805 device_unregister(&test_bus);
806 of_node_put(np);
Rob Herring82c0f582014-04-23 17:57:40 -0500807}
808
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700809/**
810 * update_node_properties - adds the properties
811 * of np into dup node (present in live tree) and
812 * updates parent of children of np to dup.
813 *
814 * @np: node already present in live tree
815 * @dup: node present in live tree to be updated
816 */
817static void update_node_properties(struct device_node *np,
818 struct device_node *dup)
819{
820 struct property *prop;
821 struct device_node *child;
822
823 for_each_property_of_node(np, prop)
824 of_add_property(dup, prop);
825
826 for_each_child_of_node(np, child)
827 child->parent = dup;
828}
829
830/**
831 * attach_node_and_children - attaches nodes
832 * and its children to live tree
833 *
834 * @np: Node to attach to live tree
835 */
836static int attach_node_and_children(struct device_node *np)
837{
Grant Likely5063e252014-10-03 16:28:27 +0100838 struct device_node *next, *dup, *child;
Gaurav Minocha3ce04b42015-01-10 23:19:51 -0800839 unsigned long flags;
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700840
Grant Likely5063e252014-10-03 16:28:27 +0100841 dup = of_find_node_by_path(np->full_name);
842 if (dup) {
843 update_node_properties(np, dup);
844 return 0;
845 }
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700846
Grant Likely5063e252014-10-03 16:28:27 +0100847 child = np->child;
848 np->child = NULL;
Gaurav Minocha3ce04b42015-01-10 23:19:51 -0800849
850 mutex_lock(&of_mutex);
851 raw_spin_lock_irqsave(&devtree_lock, flags);
852 np->sibling = np->parent->child;
853 np->parent->child = np;
854 of_node_clear_flag(np, OF_DETACHED);
855 raw_spin_unlock_irqrestore(&devtree_lock, flags);
856
857 __of_attach_node_sysfs(np);
858 mutex_unlock(&of_mutex);
859
Grant Likely5063e252014-10-03 16:28:27 +0100860 while (child) {
861 next = child->sibling;
862 attach_node_and_children(child);
863 child = next;
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700864 }
865
866 return 0;
867}
868
869/**
870 * selftest_data_add - Reads, copies data from
871 * linked tree and attaches it to the live tree
872 */
873static int __init selftest_data_add(void)
874{
875 void *selftest_data;
Gaurav Minochab951f9d2014-07-26 12:48:50 -0700876 struct device_node *selftest_data_node, *np;
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700877 extern uint8_t __dtb_testcases_begin[];
878 extern uint8_t __dtb_testcases_end[];
879 const int size = __dtb_testcases_end - __dtb_testcases_begin;
Grant Likely2eb46da2014-10-02 14:36:46 +0100880 int rc;
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700881
Gaurav Minochab951f9d2014-07-26 12:48:50 -0700882 if (!size) {
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700883 pr_warn("%s: No testcase data to attach; not running tests\n",
884 __func__);
885 return -ENODATA;
886 }
887
888 /* creating copy */
889 selftest_data = kmemdup(__dtb_testcases_begin, size, GFP_KERNEL);
890
891 if (!selftest_data) {
892 pr_warn("%s: Failed to allocate memory for selftest_data; "
893 "not running tests\n", __func__);
894 return -ENOMEM;
895 }
896 of_fdt_unflatten_tree(selftest_data, &selftest_data_node);
Gaurav Minochab951f9d2014-07-26 12:48:50 -0700897 if (!selftest_data_node) {
898 pr_warn("%s: No tree to attach; not running tests\n", __func__);
899 return -ENODATA;
900 }
Grant Likely2eb46da2014-10-02 14:36:46 +0100901 of_node_set_flag(selftest_data_node, OF_DETACHED);
902 rc = of_resolve_phandles(selftest_data_node);
903 if (rc) {
904 pr_err("%s: Failed to resolve phandles (rc=%i)\n", __func__, rc);
905 return -EINVAL;
906 }
Gaurav Minochab951f9d2014-07-26 12:48:50 -0700907
Grant Likely5063e252014-10-03 16:28:27 +0100908 if (!of_root) {
Grant Likely5063e252014-10-03 16:28:27 +0100909 of_root = selftest_data_node;
Gaurav Minochab951f9d2014-07-26 12:48:50 -0700910 for_each_of_allnodes(np)
911 __of_attach_node_sysfs(np);
912 of_aliases = of_find_node_by_path("/aliases");
913 of_chosen = of_find_node_by_path("/chosen");
914 return 0;
915 }
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700916
917 /* attach the sub-tree to live tree */
Grant Likely5063e252014-10-03 16:28:27 +0100918 np = selftest_data_node->child;
919 while (np) {
920 struct device_node *next = np->sibling;
921 np->parent = of_root;
922 attach_node_and_children(np);
923 np = next;
924 }
925 return 0;
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700926}
927
Pantelis Antoniou177d2712014-10-28 22:35:59 +0200928#ifdef CONFIG_OF_OVERLAY
929
930static int selftest_probe(struct platform_device *pdev)
931{
932 struct device *dev = &pdev->dev;
933 struct device_node *np = dev->of_node;
934
935 if (np == NULL) {
936 dev_err(dev, "No OF data for device\n");
937 return -EINVAL;
938
939 }
940
941 dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +0200942
943 of_platform_populate(np, NULL, NULL, &pdev->dev);
944
Pantelis Antoniou177d2712014-10-28 22:35:59 +0200945 return 0;
946}
947
948static int selftest_remove(struct platform_device *pdev)
949{
950 struct device *dev = &pdev->dev;
951 struct device_node *np = dev->of_node;
952
953 dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
954 return 0;
955}
956
Frank Rowandafaed7a2015-03-14 00:00:36 -0700957static const struct of_device_id selftest_match[] = {
Pantelis Antoniou177d2712014-10-28 22:35:59 +0200958 { .compatible = "selftest", },
959 {},
960};
Pantelis Antoniou177d2712014-10-28 22:35:59 +0200961
962static struct platform_driver selftest_driver = {
963 .probe = selftest_probe,
964 .remove = selftest_remove,
965 .driver = {
966 .name = "selftest",
967 .owner = THIS_MODULE,
968 .of_match_table = of_match_ptr(selftest_match),
969 },
970};
971
972/* get the platform device instantiated at the path */
973static struct platform_device *of_path_to_platform_device(const char *path)
974{
975 struct device_node *np;
976 struct platform_device *pdev;
977
978 np = of_find_node_by_path(path);
979 if (np == NULL)
980 return NULL;
981
982 pdev = of_find_device_by_node(np);
983 of_node_put(np);
984
985 return pdev;
986}
987
988/* find out if a platform device exists at that path */
989static int of_path_platform_device_exists(const char *path)
990{
991 struct platform_device *pdev;
992
993 pdev = of_path_to_platform_device(path);
994 platform_device_put(pdev);
995 return pdev != NULL;
996}
997
Arnd Bergmann4252de32015-03-04 20:49:47 +0100998#if IS_BUILTIN(CONFIG_I2C)
Pantelis Antonioud5e75502015-01-12 19:02:49 +0200999
1000/* get the i2c client device instantiated at the path */
1001static struct i2c_client *of_path_to_i2c_client(const char *path)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001002{
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001003 struct device_node *np;
1004 struct i2c_client *client;
1005
1006 np = of_find_node_by_path(path);
1007 if (np == NULL)
1008 return NULL;
1009
1010 client = of_find_i2c_device_by_node(np);
1011 of_node_put(np);
1012
1013 return client;
1014}
1015
1016/* find out if a i2c client device exists at that path */
1017static int of_path_i2c_client_exists(const char *path)
1018{
1019 struct i2c_client *client;
1020
1021 client = of_path_to_i2c_client(path);
1022 if (client)
1023 put_device(&client->dev);
1024 return client != NULL;
1025}
1026#else
1027static int of_path_i2c_client_exists(const char *path)
1028{
1029 return 0;
1030}
1031#endif
1032
1033enum overlay_type {
1034 PDEV_OVERLAY,
1035 I2C_OVERLAY
1036};
1037
1038static int of_path_device_type_exists(const char *path,
1039 enum overlay_type ovtype)
1040{
1041 switch (ovtype) {
1042 case PDEV_OVERLAY:
1043 return of_path_platform_device_exists(path);
1044 case I2C_OVERLAY:
1045 return of_path_i2c_client_exists(path);
1046 }
1047 return 0;
1048}
1049
1050static const char *selftest_path(int nr, enum overlay_type ovtype)
1051{
1052 const char *base;
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001053 static char buf[256];
1054
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001055 switch (ovtype) {
1056 case PDEV_OVERLAY:
1057 base = "/testcase-data/overlay-node/test-bus";
1058 break;
1059 case I2C_OVERLAY:
1060 base = "/testcase-data/overlay-node/test-bus/i2c-test-bus";
1061 break;
1062 default:
1063 buf[0] = '\0';
1064 return buf;
1065 }
1066 snprintf(buf, sizeof(buf) - 1, "%s/test-selftest%d", base, nr);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001067 buf[sizeof(buf) - 1] = '\0';
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001068 return buf;
1069}
1070
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001071static int of_selftest_device_exists(int selftest_nr, enum overlay_type ovtype)
1072{
1073 const char *path;
1074
1075 path = selftest_path(selftest_nr, ovtype);
1076
1077 switch (ovtype) {
1078 case PDEV_OVERLAY:
1079 return of_path_platform_device_exists(path);
1080 case I2C_OVERLAY:
1081 return of_path_i2c_client_exists(path);
1082 }
1083 return 0;
1084}
1085
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001086static const char *overlay_path(int nr)
1087{
1088 static char buf[256];
1089
1090 snprintf(buf, sizeof(buf) - 1,
1091 "/testcase-data/overlay%d", nr);
1092 buf[sizeof(buf) - 1] = '\0';
1093
1094 return buf;
1095}
1096
1097static const char *bus_path = "/testcase-data/overlay-node/test-bus";
1098
1099static int of_selftest_apply_overlay(int selftest_nr, int overlay_nr,
1100 int *overlay_id)
1101{
1102 struct device_node *np = NULL;
1103 int ret, id = -1;
1104
1105 np = of_find_node_by_path(overlay_path(overlay_nr));
1106 if (np == NULL) {
1107 selftest(0, "could not find overlay node @\"%s\"\n",
1108 overlay_path(overlay_nr));
1109 ret = -EINVAL;
1110 goto out;
1111 }
1112
1113 ret = of_overlay_create(np);
1114 if (ret < 0) {
1115 selftest(0, "could not create overlay from \"%s\"\n",
1116 overlay_path(overlay_nr));
1117 goto out;
1118 }
1119 id = ret;
1120
1121 ret = 0;
1122
1123out:
1124 of_node_put(np);
1125
1126 if (overlay_id)
1127 *overlay_id = id;
1128
1129 return ret;
1130}
1131
1132/* apply an overlay while checking before and after states */
1133static int of_selftest_apply_overlay_check(int overlay_nr, int selftest_nr,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001134 int before, int after, enum overlay_type ovtype)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001135{
1136 int ret;
1137
1138 /* selftest device must not be in before state */
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001139 if (of_selftest_device_exists(selftest_nr, ovtype) != before) {
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001140 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1141 overlay_path(overlay_nr),
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001142 selftest_path(selftest_nr, ovtype),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001143 !before ? "enabled" : "disabled");
1144 return -EINVAL;
1145 }
1146
1147 ret = of_selftest_apply_overlay(overlay_nr, selftest_nr, NULL);
1148 if (ret != 0) {
1149 /* of_selftest_apply_overlay already called selftest() */
1150 return ret;
1151 }
1152
1153 /* selftest device must be to set to after state */
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001154 if (of_selftest_device_exists(selftest_nr, ovtype) != after) {
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001155 selftest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n",
1156 overlay_path(overlay_nr),
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001157 selftest_path(selftest_nr, ovtype),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001158 !after ? "enabled" : "disabled");
1159 return -EINVAL;
1160 }
1161
1162 return 0;
1163}
1164
1165/* apply an overlay and then revert it while checking before, after states */
1166static int of_selftest_apply_revert_overlay_check(int overlay_nr,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001167 int selftest_nr, int before, int after,
1168 enum overlay_type ovtype)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001169{
1170 int ret, ov_id;
1171
1172 /* selftest device must be in before state */
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001173 if (of_selftest_device_exists(selftest_nr, ovtype) != before) {
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001174 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1175 overlay_path(overlay_nr),
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001176 selftest_path(selftest_nr, ovtype),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001177 !before ? "enabled" : "disabled");
1178 return -EINVAL;
1179 }
1180
1181 /* apply the overlay */
1182 ret = of_selftest_apply_overlay(overlay_nr, selftest_nr, &ov_id);
1183 if (ret != 0) {
1184 /* of_selftest_apply_overlay already called selftest() */
1185 return ret;
1186 }
1187
1188 /* selftest device must be in after state */
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001189 if (of_selftest_device_exists(selftest_nr, ovtype) != after) {
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001190 selftest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n",
1191 overlay_path(overlay_nr),
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001192 selftest_path(selftest_nr, ovtype),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001193 !after ? "enabled" : "disabled");
1194 return -EINVAL;
1195 }
1196
1197 ret = of_overlay_destroy(ov_id);
1198 if (ret != 0) {
1199 selftest(0, "overlay @\"%s\" failed to be destroyed @\"%s\"\n",
1200 overlay_path(overlay_nr),
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001201 selftest_path(selftest_nr, ovtype));
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001202 return ret;
1203 }
1204
1205 /* selftest device must be again in before state */
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001206 if (of_selftest_device_exists(selftest_nr, PDEV_OVERLAY) != before) {
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001207 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1208 overlay_path(overlay_nr),
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001209 selftest_path(selftest_nr, ovtype),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001210 !before ? "enabled" : "disabled");
1211 return -EINVAL;
1212 }
1213
1214 return 0;
1215}
1216
1217/* test activation of device */
1218static void of_selftest_overlay_0(void)
1219{
1220 int ret;
1221
1222 /* device should enable */
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001223 ret = of_selftest_apply_overlay_check(0, 0, 0, 1, PDEV_OVERLAY);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001224 if (ret != 0)
1225 return;
1226
1227 selftest(1, "overlay test %d passed\n", 0);
1228}
1229
1230/* test deactivation of device */
1231static void of_selftest_overlay_1(void)
1232{
1233 int ret;
1234
1235 /* device should disable */
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001236 ret = of_selftest_apply_overlay_check(1, 1, 1, 0, PDEV_OVERLAY);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001237 if (ret != 0)
1238 return;
1239
1240 selftest(1, "overlay test %d passed\n", 1);
1241}
1242
1243/* test activation of device */
1244static void of_selftest_overlay_2(void)
1245{
1246 int ret;
1247
1248 /* device should enable */
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001249 ret = of_selftest_apply_overlay_check(2, 2, 0, 1, PDEV_OVERLAY);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001250 if (ret != 0)
1251 return;
1252
1253 selftest(1, "overlay test %d passed\n", 2);
1254}
1255
1256/* test deactivation of device */
1257static void of_selftest_overlay_3(void)
1258{
1259 int ret;
1260
1261 /* device should disable */
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001262 ret = of_selftest_apply_overlay_check(3, 3, 1, 0, PDEV_OVERLAY);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001263 if (ret != 0)
1264 return;
1265
1266 selftest(1, "overlay test %d passed\n", 3);
1267}
1268
1269/* test activation of a full device node */
1270static void of_selftest_overlay_4(void)
1271{
1272 int ret;
1273
1274 /* device should disable */
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001275 ret = of_selftest_apply_overlay_check(4, 4, 0, 1, PDEV_OVERLAY);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001276 if (ret != 0)
1277 return;
1278
1279 selftest(1, "overlay test %d passed\n", 4);
1280}
1281
1282/* test overlay apply/revert sequence */
1283static void of_selftest_overlay_5(void)
1284{
1285 int ret;
1286
1287 /* device should disable */
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001288 ret = of_selftest_apply_revert_overlay_check(5, 5, 0, 1, PDEV_OVERLAY);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001289 if (ret != 0)
1290 return;
1291
1292 selftest(1, "overlay test %d passed\n", 5);
1293}
1294
1295/* test overlay application in sequence */
1296static void of_selftest_overlay_6(void)
1297{
1298 struct device_node *np;
1299 int ret, i, ov_id[2];
1300 int overlay_nr = 6, selftest_nr = 6;
1301 int before = 0, after = 1;
1302
1303 /* selftest device must be in before state */
1304 for (i = 0; i < 2; i++) {
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001305 if (of_selftest_device_exists(selftest_nr + i, PDEV_OVERLAY)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001306 != before) {
1307 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1308 overlay_path(overlay_nr + i),
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001309 selftest_path(selftest_nr + i,
1310 PDEV_OVERLAY),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001311 !before ? "enabled" : "disabled");
1312 return;
1313 }
1314 }
1315
1316 /* apply the overlays */
1317 for (i = 0; i < 2; i++) {
1318
1319 np = of_find_node_by_path(overlay_path(overlay_nr + i));
1320 if (np == NULL) {
1321 selftest(0, "could not find overlay node @\"%s\"\n",
1322 overlay_path(overlay_nr + i));
1323 return;
1324 }
1325
1326 ret = of_overlay_create(np);
1327 if (ret < 0) {
1328 selftest(0, "could not create overlay from \"%s\"\n",
1329 overlay_path(overlay_nr + i));
1330 return;
1331 }
1332 ov_id[i] = ret;
1333 }
1334
1335 for (i = 0; i < 2; i++) {
1336 /* selftest device must be in after state */
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001337 if (of_selftest_device_exists(selftest_nr + i, PDEV_OVERLAY)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001338 != after) {
1339 selftest(0, "overlay @\"%s\" failed @\"%s\" %s\n",
1340 overlay_path(overlay_nr + i),
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001341 selftest_path(selftest_nr + i,
1342 PDEV_OVERLAY),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001343 !after ? "enabled" : "disabled");
1344 return;
1345 }
1346 }
1347
1348 for (i = 1; i >= 0; i--) {
1349 ret = of_overlay_destroy(ov_id[i]);
1350 if (ret != 0) {
1351 selftest(0, "overlay @\"%s\" failed destroy @\"%s\"\n",
1352 overlay_path(overlay_nr + i),
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001353 selftest_path(selftest_nr + i,
1354 PDEV_OVERLAY));
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001355 return;
1356 }
1357 }
1358
1359 for (i = 0; i < 2; i++) {
1360 /* selftest device must be again in before state */
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001361 if (of_selftest_device_exists(selftest_nr + i, PDEV_OVERLAY)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001362 != before) {
1363 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1364 overlay_path(overlay_nr + i),
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001365 selftest_path(selftest_nr + i,
1366 PDEV_OVERLAY),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001367 !before ? "enabled" : "disabled");
1368 return;
1369 }
1370 }
1371
1372 selftest(1, "overlay test %d passed\n", 6);
1373}
1374
1375/* test overlay application in sequence */
1376static void of_selftest_overlay_8(void)
1377{
1378 struct device_node *np;
1379 int ret, i, ov_id[2];
1380 int overlay_nr = 8, selftest_nr = 8;
1381
1382 /* we don't care about device state in this test */
1383
1384 /* apply the overlays */
1385 for (i = 0; i < 2; i++) {
1386
1387 np = of_find_node_by_path(overlay_path(overlay_nr + i));
1388 if (np == NULL) {
1389 selftest(0, "could not find overlay node @\"%s\"\n",
1390 overlay_path(overlay_nr + i));
1391 return;
1392 }
1393
1394 ret = of_overlay_create(np);
1395 if (ret < 0) {
1396 selftest(0, "could not create overlay from \"%s\"\n",
1397 overlay_path(overlay_nr + i));
1398 return;
1399 }
1400 ov_id[i] = ret;
1401 }
1402
1403 /* now try to remove first overlay (it should fail) */
1404 ret = of_overlay_destroy(ov_id[0]);
1405 if (ret == 0) {
1406 selftest(0, "overlay @\"%s\" was destroyed @\"%s\"\n",
1407 overlay_path(overlay_nr + 0),
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001408 selftest_path(selftest_nr,
1409 PDEV_OVERLAY));
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001410 return;
1411 }
1412
1413 /* removing them in order should work */
1414 for (i = 1; i >= 0; i--) {
1415 ret = of_overlay_destroy(ov_id[i]);
1416 if (ret != 0) {
1417 selftest(0, "overlay @\"%s\" not destroyed @\"%s\"\n",
1418 overlay_path(overlay_nr + i),
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001419 selftest_path(selftest_nr,
1420 PDEV_OVERLAY));
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001421 return;
1422 }
1423 }
1424
1425 selftest(1, "overlay test %d passed\n", 8);
1426}
1427
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001428/* test insertion of a bus with parent devices */
1429static void of_selftest_overlay_10(void)
1430{
1431 int ret;
1432 char *child_path;
1433
1434 /* device should disable */
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001435 ret = of_selftest_apply_overlay_check(10, 10, 0, 1, PDEV_OVERLAY);
1436 if (selftest(ret == 0,
1437 "overlay test %d failed; overlay application\n", 10))
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001438 return;
1439
1440 child_path = kasprintf(GFP_KERNEL, "%s/test-selftest101",
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001441 selftest_path(10, PDEV_OVERLAY));
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001442 if (selftest(child_path, "overlay test %d failed; kasprintf\n", 10))
1443 return;
1444
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001445 ret = of_path_device_type_exists(child_path, PDEV_OVERLAY);
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001446 kfree(child_path);
1447 if (selftest(ret, "overlay test %d failed; no child device\n", 10))
1448 return;
1449}
1450
1451/* test insertion of a bus with parent devices (and revert) */
1452static void of_selftest_overlay_11(void)
1453{
1454 int ret;
1455
1456 /* device should disable */
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001457 ret = of_selftest_apply_revert_overlay_check(11, 11, 0, 1,
1458 PDEV_OVERLAY);
1459 if (selftest(ret == 0,
1460 "overlay test %d failed; overlay application\n", 11))
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001461 return;
1462}
1463
Arnd Bergmann4252de32015-03-04 20:49:47 +01001464#if IS_BUILTIN(CONFIG_I2C) && IS_ENABLED(CONFIG_OF_OVERLAY)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001465
1466struct selftest_i2c_bus_data {
1467 struct platform_device *pdev;
1468 struct i2c_adapter adap;
1469};
1470
1471static int selftest_i2c_master_xfer(struct i2c_adapter *adap,
1472 struct i2c_msg *msgs, int num)
1473{
1474 struct selftest_i2c_bus_data *std = i2c_get_adapdata(adap);
1475
1476 (void)std;
1477
1478 return num;
1479}
1480
1481static u32 selftest_i2c_functionality(struct i2c_adapter *adap)
1482{
1483 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
1484}
1485
1486static const struct i2c_algorithm selftest_i2c_algo = {
1487 .master_xfer = selftest_i2c_master_xfer,
1488 .functionality = selftest_i2c_functionality,
1489};
1490
1491static int selftest_i2c_bus_probe(struct platform_device *pdev)
1492{
1493 struct device *dev = &pdev->dev;
1494 struct device_node *np = dev->of_node;
1495 struct selftest_i2c_bus_data *std;
1496 struct i2c_adapter *adap;
1497 int ret;
1498
1499 if (np == NULL) {
1500 dev_err(dev, "No OF data for device\n");
1501 return -EINVAL;
1502
1503 }
1504
1505 dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
1506
1507 std = devm_kzalloc(dev, sizeof(*std), GFP_KERNEL);
1508 if (!std) {
1509 dev_err(dev, "Failed to allocate selftest i2c data\n");
1510 return -ENOMEM;
1511 }
1512
1513 /* link them together */
1514 std->pdev = pdev;
1515 platform_set_drvdata(pdev, std);
1516
1517 adap = &std->adap;
1518 i2c_set_adapdata(adap, std);
1519 adap->nr = -1;
1520 strlcpy(adap->name, pdev->name, sizeof(adap->name));
1521 adap->class = I2C_CLASS_DEPRECATED;
1522 adap->algo = &selftest_i2c_algo;
1523 adap->dev.parent = dev;
1524 adap->dev.of_node = dev->of_node;
1525 adap->timeout = 5 * HZ;
1526 adap->retries = 3;
1527
1528 ret = i2c_add_numbered_adapter(adap);
1529 if (ret != 0) {
1530 dev_err(dev, "Failed to add I2C adapter\n");
1531 return ret;
1532 }
1533
1534 return 0;
1535}
1536
1537static int selftest_i2c_bus_remove(struct platform_device *pdev)
1538{
1539 struct device *dev = &pdev->dev;
1540 struct device_node *np = dev->of_node;
1541 struct selftest_i2c_bus_data *std = platform_get_drvdata(pdev);
1542
1543 dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
1544 i2c_del_adapter(&std->adap);
1545
1546 return 0;
1547}
1548
Frank Rowandafaed7a2015-03-14 00:00:36 -07001549static const struct of_device_id selftest_i2c_bus_match[] = {
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001550 { .compatible = "selftest-i2c-bus", },
1551 {},
1552};
1553
1554static struct platform_driver selftest_i2c_bus_driver = {
1555 .probe = selftest_i2c_bus_probe,
1556 .remove = selftest_i2c_bus_remove,
1557 .driver = {
1558 .name = "selftest-i2c-bus",
1559 .of_match_table = of_match_ptr(selftest_i2c_bus_match),
1560 },
1561};
1562
1563static int selftest_i2c_dev_probe(struct i2c_client *client,
1564 const struct i2c_device_id *id)
1565{
1566 struct device *dev = &client->dev;
1567 struct device_node *np = client->dev.of_node;
1568
1569 if (!np) {
1570 dev_err(dev, "No OF node\n");
1571 return -EINVAL;
1572 }
1573
1574 dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
1575
1576 return 0;
1577};
1578
1579static int selftest_i2c_dev_remove(struct i2c_client *client)
1580{
1581 struct device *dev = &client->dev;
1582 struct device_node *np = client->dev.of_node;
1583
1584 dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
1585 return 0;
1586}
1587
1588static const struct i2c_device_id selftest_i2c_dev_id[] = {
1589 { .name = "selftest-i2c-dev" },
1590 { }
1591};
1592
1593static struct i2c_driver selftest_i2c_dev_driver = {
1594 .driver = {
1595 .name = "selftest-i2c-dev",
1596 .owner = THIS_MODULE,
1597 },
1598 .probe = selftest_i2c_dev_probe,
1599 .remove = selftest_i2c_dev_remove,
1600 .id_table = selftest_i2c_dev_id,
1601};
1602
Arnd Bergmann4252de32015-03-04 20:49:47 +01001603#if IS_BUILTIN(CONFIG_I2C_MUX)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001604
1605struct selftest_i2c_mux_data {
1606 int nchans;
1607 struct i2c_adapter *adap[];
1608};
1609
1610static int selftest_i2c_mux_select_chan(struct i2c_adapter *adap,
1611 void *client, u32 chan)
1612{
1613 return 0;
1614}
1615
1616static int selftest_i2c_mux_probe(struct i2c_client *client,
1617 const struct i2c_device_id *id)
1618{
1619 int ret, i, nchans, size;
1620 struct device *dev = &client->dev;
1621 struct i2c_adapter *adap = to_i2c_adapter(dev->parent);
1622 struct device_node *np = client->dev.of_node, *child;
1623 struct selftest_i2c_mux_data *stm;
1624 u32 reg, max_reg;
1625
1626 dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
1627
1628 if (!np) {
1629 dev_err(dev, "No OF node\n");
1630 return -EINVAL;
1631 }
1632
1633 max_reg = (u32)-1;
1634 for_each_child_of_node(np, child) {
1635 ret = of_property_read_u32(child, "reg", &reg);
1636 if (ret)
1637 continue;
1638 if (max_reg == (u32)-1 || reg > max_reg)
1639 max_reg = reg;
1640 }
1641 nchans = max_reg == (u32)-1 ? 0 : max_reg + 1;
1642 if (nchans == 0) {
1643 dev_err(dev, "No channels\n");
1644 return -EINVAL;
1645 }
1646
1647 size = offsetof(struct selftest_i2c_mux_data, adap[nchans]);
1648 stm = devm_kzalloc(dev, size, GFP_KERNEL);
1649 if (!stm) {
1650 dev_err(dev, "Out of memory\n");
1651 return -ENOMEM;
1652 }
1653 stm->nchans = nchans;
1654 for (i = 0; i < nchans; i++) {
1655 stm->adap[i] = i2c_add_mux_adapter(adap, dev, client,
1656 0, i, 0, selftest_i2c_mux_select_chan, NULL);
1657 if (!stm->adap[i]) {
1658 dev_err(dev, "Failed to register mux #%d\n", i);
1659 for (i--; i >= 0; i--)
1660 i2c_del_mux_adapter(stm->adap[i]);
1661 return -ENODEV;
1662 }
1663 }
1664
1665 i2c_set_clientdata(client, stm);
1666
1667 return 0;
1668};
1669
1670static int selftest_i2c_mux_remove(struct i2c_client *client)
1671{
1672 struct device *dev = &client->dev;
1673 struct device_node *np = client->dev.of_node;
1674 struct selftest_i2c_mux_data *stm = i2c_get_clientdata(client);
1675 int i;
1676
1677 dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
1678 for (i = stm->nchans - 1; i >= 0; i--)
1679 i2c_del_mux_adapter(stm->adap[i]);
1680 return 0;
1681}
1682
1683static const struct i2c_device_id selftest_i2c_mux_id[] = {
1684 { .name = "selftest-i2c-mux" },
1685 { }
1686};
1687
1688static struct i2c_driver selftest_i2c_mux_driver = {
1689 .driver = {
1690 .name = "selftest-i2c-mux",
1691 .owner = THIS_MODULE,
1692 },
1693 .probe = selftest_i2c_mux_probe,
1694 .remove = selftest_i2c_mux_remove,
1695 .id_table = selftest_i2c_mux_id,
1696};
1697
1698#endif
1699
1700static int of_selftest_overlay_i2c_init(void)
1701{
1702 int ret;
1703
1704 ret = i2c_add_driver(&selftest_i2c_dev_driver);
1705 if (selftest(ret == 0,
1706 "could not register selftest i2c device driver\n"))
1707 return ret;
1708
1709 ret = platform_driver_register(&selftest_i2c_bus_driver);
1710 if (selftest(ret == 0,
1711 "could not register selftest i2c bus driver\n"))
1712 return ret;
1713
Arnd Bergmann4252de32015-03-04 20:49:47 +01001714#if IS_BUILTIN(CONFIG_I2C_MUX)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001715 ret = i2c_add_driver(&selftest_i2c_mux_driver);
1716 if (selftest(ret == 0,
1717 "could not register selftest i2c mux driver\n"))
1718 return ret;
1719#endif
1720
1721 return 0;
1722}
1723
1724static void of_selftest_overlay_i2c_cleanup(void)
1725{
Arnd Bergmann4252de32015-03-04 20:49:47 +01001726#if IS_BUILTIN(CONFIG_I2C_MUX)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001727 i2c_del_driver(&selftest_i2c_mux_driver);
1728#endif
1729 platform_driver_unregister(&selftest_i2c_bus_driver);
1730 i2c_del_driver(&selftest_i2c_dev_driver);
1731}
1732
1733static void of_selftest_overlay_i2c_12(void)
1734{
1735 int ret;
1736
1737 /* device should enable */
1738 ret = of_selftest_apply_overlay_check(12, 12, 0, 1, I2C_OVERLAY);
1739 if (ret != 0)
1740 return;
1741
1742 selftest(1, "overlay test %d passed\n", 12);
1743}
1744
1745/* test deactivation of device */
1746static void of_selftest_overlay_i2c_13(void)
1747{
1748 int ret;
1749
1750 /* device should disable */
1751 ret = of_selftest_apply_overlay_check(13, 13, 1, 0, I2C_OVERLAY);
1752 if (ret != 0)
1753 return;
1754
1755 selftest(1, "overlay test %d passed\n", 13);
1756}
1757
1758/* just check for i2c mux existence */
1759static void of_selftest_overlay_i2c_14(void)
1760{
1761}
1762
1763static void of_selftest_overlay_i2c_15(void)
1764{
1765 int ret;
1766
1767 /* device should enable */
1768 ret = of_selftest_apply_overlay_check(16, 15, 0, 1, I2C_OVERLAY);
1769 if (ret != 0)
1770 return;
1771
1772 selftest(1, "overlay test %d passed\n", 15);
1773}
1774
1775#else
1776
1777static inline void of_selftest_overlay_i2c_14(void) { }
1778static inline void of_selftest_overlay_i2c_15(void) { }
1779
1780#endif
1781
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001782static void __init of_selftest_overlay(void)
1783{
1784 struct device_node *bus_np = NULL;
1785 int ret;
1786
1787 ret = platform_driver_register(&selftest_driver);
1788 if (ret != 0) {
1789 selftest(0, "could not register selftest driver\n");
1790 goto out;
1791 }
1792
1793 bus_np = of_find_node_by_path(bus_path);
1794 if (bus_np == NULL) {
1795 selftest(0, "could not find bus_path \"%s\"\n", bus_path);
1796 goto out;
1797 }
1798
1799 ret = of_platform_populate(bus_np, of_default_bus_match_table,
1800 NULL, NULL);
1801 if (ret != 0) {
1802 selftest(0, "could not populate bus @ \"%s\"\n", bus_path);
1803 goto out;
1804 }
1805
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001806 if (!of_selftest_device_exists(100, PDEV_OVERLAY)) {
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001807 selftest(0, "could not find selftest0 @ \"%s\"\n",
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001808 selftest_path(100, PDEV_OVERLAY));
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001809 goto out;
1810 }
1811
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001812 if (of_selftest_device_exists(101, PDEV_OVERLAY)) {
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001813 selftest(0, "selftest1 @ \"%s\" should not exist\n",
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001814 selftest_path(101, PDEV_OVERLAY));
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001815 goto out;
1816 }
1817
1818 selftest(1, "basic infrastructure of overlays passed");
1819
1820 /* tests in sequence */
1821 of_selftest_overlay_0();
1822 of_selftest_overlay_1();
1823 of_selftest_overlay_2();
1824 of_selftest_overlay_3();
1825 of_selftest_overlay_4();
1826 of_selftest_overlay_5();
1827 of_selftest_overlay_6();
1828 of_selftest_overlay_8();
1829
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001830 of_selftest_overlay_10();
1831 of_selftest_overlay_11();
1832
Arnd Bergmann4252de32015-03-04 20:49:47 +01001833#if IS_BUILTIN(CONFIG_I2C)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001834 if (selftest(of_selftest_overlay_i2c_init() == 0, "i2c init failed\n"))
1835 goto out;
1836
1837 of_selftest_overlay_i2c_12();
1838 of_selftest_overlay_i2c_13();
1839 of_selftest_overlay_i2c_14();
1840 of_selftest_overlay_i2c_15();
1841
1842 of_selftest_overlay_i2c_cleanup();
1843#endif
1844
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001845out:
1846 of_node_put(bus_np);
1847}
1848
1849#else
1850static inline void __init of_selftest_overlay(void) { }
1851#endif
1852
Grant Likely53a42092011-12-12 09:25:57 -07001853static int __init of_selftest(void)
1854{
1855 struct device_node *np;
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001856 int res;
1857
1858 /* adding data for selftest */
1859 res = selftest_data_add();
1860 if (res)
1861 return res;
Grant Likely788ec2f2014-11-19 17:13:44 +00001862 if (!of_aliases)
1863 of_aliases = of_find_node_by_path("/aliases");
Grant Likely53a42092011-12-12 09:25:57 -07001864
1865 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
1866 if (!np) {
1867 pr_info("No testcase data in device tree; not running tests\n");
1868 return 0;
1869 }
1870 of_node_put(np);
1871
1872 pr_info("start of selftest - you will see error messages\n");
Grant Likelyf2051d62014-10-01 17:40:22 +01001873 of_selftest_check_tree_linkage();
Grant Likely841ec212014-10-02 13:09:15 +01001874 of_selftest_check_phandles();
Grant Likelyae91ff72014-03-14 13:53:10 +00001875 of_selftest_find_node_by_name();
Grant Likely7e66c5c2013-11-15 17:19:09 +00001876 of_selftest_dynamic();
Grant Likely53a42092011-12-12 09:25:57 -07001877 of_selftest_parse_phandle_with_args();
Grant Likelya87fa1d2014-11-03 15:15:35 +00001878 of_selftest_property_string();
Pantelis Antoniou69843392014-07-04 19:58:47 +03001879 of_selftest_property_copy();
Pantelis Antoniou201c9102014-07-04 19:58:49 +03001880 of_selftest_changeset();
Grant Likelya9f10ca2013-10-11 22:04:23 +01001881 of_selftest_parse_interrupts();
Grant Likely79d97012013-09-19 16:47:37 -05001882 of_selftest_parse_interrupts_extended();
Grant Likely1f42e5d2014-02-18 21:38:55 +00001883 of_selftest_match_node();
Rob Herring82c0f582014-04-23 17:57:40 -05001884 of_selftest_platform_populate();
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001885 of_selftest_overlay();
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001886
Grant Likelyf2051d62014-10-01 17:40:22 +01001887 /* Double check linkage after removing testcase data */
1888 of_selftest_check_tree_linkage();
1889
1890 pr_info("end of selftest - %i passed, %i failed\n",
1891 selftest_results.passed, selftest_results.failed);
1892
Grant Likely53a42092011-12-12 09:25:57 -07001893 return 0;
1894}
1895late_initcall(of_selftest);