dts: rk3228: add i2s, spdif dts node
[firefly-linux-kernel-4.4.55.git] / drivers / of / selftest.c
1 /*
2  * Self tests for device tree subsystem
3  */
4
5 #define pr_fmt(fmt) "### dt-test ### " fmt
6
7 #include <linux/clk.h>
8 #include <linux/err.h>
9 #include <linux/errno.h>
10 #include <linux/module.h>
11 #include <linux/of.h>
12 #include <linux/list.h>
13 #include <linux/mutex.h>
14 #include <linux/slab.h>
15 #include <linux/device.h>
16
17 #include "of_private.h"
18
19 static struct selftest_results {
20         int passed;
21         int failed;
22 } selftest_results;
23
24 #define selftest(result, fmt, ...) { \
25         if (!(result)) { \
26                 pr_err("FAIL %s:%i " fmt, __FILE__, __LINE__, ##__VA_ARGS__); \
27                 selftest_results.passed = false; \
28         } else { \
29                 pr_info("pass %s:%i\n", __FILE__, __LINE__); \
30         } \
31 }
32
33 static void __init of_selftest_parse_phandle_with_args(void)
34 {
35         struct device_node *np;
36         struct of_phandle_args args;
37         int i, rc;
38
39         np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
40         if (!np) {
41                 pr_err("missing testcase data\n");
42                 return;
43         }
44
45         rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
46         selftest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc);
47
48         for (i = 0; i < 8; i++) {
49                 bool passed = true;
50                 rc = of_parse_phandle_with_args(np, "phandle-list",
51                                                 "#phandle-cells", i, &args);
52
53                 /* Test the values from tests-phandle.dtsi */
54                 switch (i) {
55                 case 0:
56                         passed &= !rc;
57                         passed &= (args.args_count == 1);
58                         passed &= (args.args[0] == (i + 1));
59                         break;
60                 case 1:
61                         passed &= !rc;
62                         passed &= (args.args_count == 2);
63                         passed &= (args.args[0] == (i + 1));
64                         passed &= (args.args[1] == 0);
65                         break;
66                 case 2:
67                         passed &= (rc == -ENOENT);
68                         break;
69                 case 3:
70                         passed &= !rc;
71                         passed &= (args.args_count == 3);
72                         passed &= (args.args[0] == (i + 1));
73                         passed &= (args.args[1] == 4);
74                         passed &= (args.args[2] == 3);
75                         break;
76                 case 4:
77                         passed &= !rc;
78                         passed &= (args.args_count == 2);
79                         passed &= (args.args[0] == (i + 1));
80                         passed &= (args.args[1] == 100);
81                         break;
82                 case 5:
83                         passed &= !rc;
84                         passed &= (args.args_count == 0);
85                         break;
86                 case 6:
87                         passed &= !rc;
88                         passed &= (args.args_count == 1);
89                         passed &= (args.args[0] == (i + 1));
90                         break;
91                 case 7:
92                         passed &= (rc == -ENOENT);
93                         break;
94                 default:
95                         passed = false;
96                 }
97
98                 selftest(passed, "index %i - data error on node %s rc=%i\n",
99                          i, args.np->full_name, rc);
100         }
101
102         /* Check for missing list property */
103         rc = of_parse_phandle_with_args(np, "phandle-list-missing",
104                                         "#phandle-cells", 0, &args);
105         selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
106         rc = of_count_phandle_with_args(np, "phandle-list-missing",
107                                         "#phandle-cells");
108         selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
109
110         /* Check for missing cells property */
111         rc = of_parse_phandle_with_args(np, "phandle-list",
112                                         "#phandle-cells-missing", 0, &args);
113         selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
114         rc = of_count_phandle_with_args(np, "phandle-list",
115                                         "#phandle-cells-missing");
116         selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
117
118         /* Check for bad phandle in list */
119         rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle",
120                                         "#phandle-cells", 0, &args);
121         selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
122         rc = of_count_phandle_with_args(np, "phandle-list-bad-phandle",
123                                         "#phandle-cells");
124         selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
125
126         /* Check for incorrectly formed argument list */
127         rc = of_parse_phandle_with_args(np, "phandle-list-bad-args",
128                                         "#phandle-cells", 1, &args);
129         selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
130         rc = of_count_phandle_with_args(np, "phandle-list-bad-args",
131                                         "#phandle-cells");
132         selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
133 }
134
135 static void __init of_selftest_property_string(void)
136 {
137         const char *strings[4];
138         struct device_node *np;
139         int rc;
140
141         pr_info("start\n");
142         np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
143         if (!np) {
144                 pr_err("No testcase data in device tree\n");
145                 return;
146         }
147
148         rc = of_property_match_string(np, "phandle-list-names", "first");
149         selftest(rc == 0, "first expected:0 got:%i\n", rc);
150         rc = of_property_match_string(np, "phandle-list-names", "second");
151         selftest(rc == 1, "second expected:0 got:%i\n", rc);
152         rc = of_property_match_string(np, "phandle-list-names", "third");
153         selftest(rc == 2, "third expected:0 got:%i\n", rc);
154         rc = of_property_match_string(np, "phandle-list-names", "fourth");
155         selftest(rc == -ENODATA, "unmatched string; rc=%i\n", rc);
156         rc = of_property_match_string(np, "missing-property", "blah");
157         selftest(rc == -EINVAL, "missing property; rc=%i\n", rc);
158         rc = of_property_match_string(np, "empty-property", "blah");
159         selftest(rc == -ENODATA, "empty property; rc=%i\n", rc);
160         rc = of_property_match_string(np, "unterminated-string", "blah");
161         selftest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
162
163         /* of_property_count_strings() tests */
164         rc = of_property_count_strings(np, "string-property");
165         selftest(rc == 1, "Incorrect string count; rc=%i\n", rc);
166         rc = of_property_count_strings(np, "phandle-list-names");
167         selftest(rc == 3, "Incorrect string count; rc=%i\n", rc);
168         rc = of_property_count_strings(np, "unterminated-string");
169         selftest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
170         rc = of_property_count_strings(np, "unterminated-string-list");
171         selftest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
172
173         /* of_property_read_string_index() tests */
174         rc = of_property_read_string_index(np, "string-property", 0, strings);
175         selftest(rc == 0 && !strcmp(strings[0], "foobar"), "of_property_read_string_index() failure; rc=%i\n", rc);
176         strings[0] = NULL;
177         rc = of_property_read_string_index(np, "string-property", 1, strings);
178         selftest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
179         rc = of_property_read_string_index(np, "phandle-list-names", 0, strings);
180         selftest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
181         rc = of_property_read_string_index(np, "phandle-list-names", 1, strings);
182         selftest(rc == 0 && !strcmp(strings[0], "second"), "of_property_read_string_index() failure; rc=%i\n", rc);
183         rc = of_property_read_string_index(np, "phandle-list-names", 2, strings);
184         selftest(rc == 0 && !strcmp(strings[0], "third"), "of_property_read_string_index() failure; rc=%i\n", rc);
185         strings[0] = NULL;
186         rc = of_property_read_string_index(np, "phandle-list-names", 3, strings);
187         selftest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
188         strings[0] = NULL;
189         rc = of_property_read_string_index(np, "unterminated-string", 0, strings);
190         selftest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
191         rc = of_property_read_string_index(np, "unterminated-string-list", 0, strings);
192         selftest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
193         strings[0] = NULL;
194         rc = of_property_read_string_index(np, "unterminated-string-list", 2, strings); /* should fail */
195         selftest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
196         strings[1] = NULL;
197
198         /* of_property_read_string_array() tests */
199         rc = of_property_read_string_array(np, "string-property", strings, 4);
200         selftest(rc == 1, "Incorrect string count; rc=%i\n", rc);
201         rc = of_property_read_string_array(np, "phandle-list-names", strings, 4);
202         selftest(rc == 3, "Incorrect string count; rc=%i\n", rc);
203         rc = of_property_read_string_array(np, "unterminated-string", strings, 4);
204         selftest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
205         /* -- An incorrectly formed string should cause a failure */
206         rc = of_property_read_string_array(np, "unterminated-string-list", strings, 4);
207         selftest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
208         /* -- parsing the correctly formed strings should still work: */
209         strings[2] = NULL;
210         rc = of_property_read_string_array(np, "unterminated-string-list", strings, 2);
211         selftest(rc == 2 && strings[2] == NULL, "of_property_read_string_array() failure; rc=%i\n", rc);
212         strings[1] = NULL;
213         rc = of_property_read_string_array(np, "phandle-list-names", strings, 1);
214         selftest(rc == 1 && strings[1] == NULL, "Overwrote end of string array; rc=%i, str='%s'\n", rc, strings[1]);
215 }
216
217 #define propcmp(p1, p2) (((p1)->length == (p2)->length) && \
218                         (p1)->value && (p2)->value && \
219                         !memcmp((p1)->value, (p2)->value, (p1)->length) && \
220                         !strcmp((p1)->name, (p2)->name))
221 static void __init of_selftest_property_copy(void)
222 {
223 #ifdef CONFIG_OF_DYNAMIC
224         struct property p1 = { .name = "p1", .length = 0, .value = "" };
225         struct property p2 = { .name = "p2", .length = 5, .value = "abcd" };
226         struct property *new;
227
228         new = __of_prop_dup(&p1, GFP_KERNEL);
229         selftest(new && propcmp(&p1, new), "empty property didn't copy correctly\n");
230         kfree(new->value);
231         kfree(new->name);
232         kfree(new);
233
234         new = __of_prop_dup(&p2, GFP_KERNEL);
235         selftest(new && propcmp(&p2, new), "non-empty property didn't copy correctly\n");
236         kfree(new->value);
237         kfree(new->name);
238         kfree(new);
239 #endif
240 }
241
242 static void __init of_selftest_changeset(void)
243 {
244 #ifdef CONFIG_OF_DYNAMIC
245         struct property *ppadd, padd = { .name = "prop-add", .length = 0, .value = "" };
246         struct property *ppupdate, pupdate = { .name = "prop-update", .length = 5, .value = "abcd" };
247         struct property *ppremove;
248         struct device_node *n1, *n2, *n21, *nremove, *parent;
249         struct of_changeset chgset;
250
251         of_changeset_init(&chgset);
252         n1 = __of_node_dup(NULL, "/testcase-data/changeset/n1");
253         selftest(n1, "testcase setup failure\n");
254         n2 = __of_node_dup(NULL, "/testcase-data/changeset/n2");
255         selftest(n2, "testcase setup failure\n");
256         n21 = __of_node_dup(NULL, "%s/%s", "/testcase-data/changeset/n2", "n21");
257         selftest(n21, "testcase setup failure %p\n", n21);
258         nremove = of_find_node_by_path("/testcase-data/changeset/node-remove");
259         selftest(nremove, "testcase setup failure\n");
260         ppadd = __of_prop_dup(&padd, GFP_KERNEL);
261         selftest(ppadd, "testcase setup failure\n");
262         ppupdate = __of_prop_dup(&pupdate, GFP_KERNEL);
263         selftest(ppupdate, "testcase setup failure\n");
264         parent = nremove->parent;
265         n1->parent = parent;
266         n2->parent = parent;
267         n21->parent = n2;
268         n2->child = n21;
269         ppremove = of_find_property(parent, "prop-remove", NULL);
270         selftest(ppremove, "failed to find removal prop");
271
272         of_changeset_init(&chgset);
273         selftest(!of_changeset_attach_node(&chgset, n1), "fail attach n1\n");
274         selftest(!of_changeset_attach_node(&chgset, n2), "fail attach n2\n");
275         selftest(!of_changeset_detach_node(&chgset, nremove), "fail remove node\n");
276         selftest(!of_changeset_attach_node(&chgset, n21), "fail attach n21\n");
277         selftest(!of_changeset_add_property(&chgset, parent, ppadd), "fail add prop\n");
278         selftest(!of_changeset_update_property(&chgset, parent, ppupdate), "fail update prop\n");
279         selftest(!of_changeset_remove_property(&chgset, parent, ppremove), "fail remove prop\n");
280         mutex_lock(&of_mutex);
281         selftest(!of_changeset_apply(&chgset), "apply failed\n");
282         mutex_unlock(&of_mutex);
283
284         mutex_lock(&of_mutex);
285         selftest(!of_changeset_revert(&chgset), "revert failed\n");
286         mutex_unlock(&of_mutex);
287
288         of_changeset_destroy(&chgset);
289 #endif
290 }
291
292 static int __init of_selftest(void)
293 {
294         struct device_node *np;
295
296         np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
297         if (!np) {
298                 pr_info("No testcase data in device tree; not running tests\n");
299                 return 0;
300         }
301         of_node_put(np);
302
303         pr_info("start of selftest - you will see error messages\n");
304         of_selftest_parse_phandle_with_args();
305         of_selftest_property_match_string();
306         of_selftest_property_copy();
307         of_selftest_changeset();
308         of_selftest_property_string();
309         pr_info("end of selftest - %i passed, %i failed\n",
310                 selftest_results.passed, selftest_results.failed);
311         return 0;
312 }
313 late_initcall(of_selftest);