Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Self tests for device tree subsystem |
| 3 | */ |
| 4 | |
Grant Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 5 | #define pr_fmt(fmt) "### dt-test ### " fmt |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 6 | |
| 7 | #include <linux/clk.h> |
| 8 | #include <linux/err.h> |
| 9 | #include <linux/errno.h> |
Grant Likely | 841ec21 | 2014-10-02 13:09:15 +0100 | [diff] [blame] | 10 | #include <linux/hashtable.h> |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 11 | #include <linux/module.h> |
| 12 | #include <linux/of.h> |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 13 | #include <linux/of_fdt.h> |
Grant Likely | a9f10ca | 2013-10-11 22:04:23 +0100 | [diff] [blame] | 14 | #include <linux/of_irq.h> |
Rob Herring | 82c0f58 | 2014-04-23 17:57:40 -0500 | [diff] [blame] | 15 | #include <linux/of_platform.h> |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 16 | #include <linux/list.h> |
| 17 | #include <linux/mutex.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/device.h> |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 20 | #include <linux/platform_device.h> |
| 21 | #include <linux/of_platform.h> |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 22 | |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 23 | #include <linux/i2c.h> |
| 24 | #include <linux/i2c-mux.h> |
| 25 | |
Pantelis Antoniou | 6984339 | 2014-07-04 19:58:47 +0300 | [diff] [blame] | 26 | #include "of_private.h" |
| 27 | |
Grant Likely | a9f10ca | 2013-10-11 22:04:23 +0100 | [diff] [blame] | 28 | static struct selftest_results { |
| 29 | int passed; |
| 30 | int failed; |
| 31 | } selftest_results; |
| 32 | |
Grant Likely | 851da97 | 2014-11-04 13:14:13 +0000 | [diff] [blame] | 33 | #define selftest(result, fmt, ...) ({ \ |
| 34 | bool failed = !(result); \ |
| 35 | if (failed) { \ |
Grant Likely | a9f10ca | 2013-10-11 22:04:23 +0100 | [diff] [blame] | 36 | selftest_results.failed++; \ |
| 37 | pr_err("FAIL %s():%i " fmt, __func__, __LINE__, ##__VA_ARGS__); \ |
Grant Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 38 | } else { \ |
Grant Likely | a9f10ca | 2013-10-11 22:04:23 +0100 | [diff] [blame] | 39 | selftest_results.passed++; \ |
| 40 | pr_debug("pass %s():%i\n", __func__, __LINE__); \ |
Grant Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 41 | } \ |
Grant Likely | 851da97 | 2014-11-04 13:14:13 +0000 | [diff] [blame] | 42 | failed; \ |
| 43 | }) |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 44 | |
Grant Likely | ae91ff7 | 2014-03-14 13:53:10 +0000 | [diff] [blame] | 45 | static void __init of_selftest_find_node_by_name(void) |
| 46 | { |
| 47 | struct device_node *np; |
Leif Lindholm | 75c28c0 | 2014-11-28 11:34:28 +0000 | [diff] [blame] | 48 | const char *options; |
Grant Likely | ae91ff7 | 2014-03-14 13:53:10 +0000 | [diff] [blame] | 49 | |
| 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 Lindholm | 75c28c0 | 2014-11-28 11:34:28 +0000 | [diff] [blame] | 89 | |
| 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 Hurley | 8cbba1a | 2015-03-06 13:59:59 -0500 | [diff] [blame] | 95 | 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 Lindholm | 75c28c0 | 2014-11-28 11:34:28 +0000 | [diff] [blame] | 100 | np = of_find_node_opts_by_path("/testcase-data:testoption", NULL); |
| 101 | selftest(np, "NULL option path test failed\n"); |
| 102 | of_node_put(np); |
| 103 | |
| 104 | np = of_find_node_opts_by_path("testcase-alias:testaliasoption", |
| 105 | &options); |
| 106 | selftest(np && !strcmp("testaliasoption", options), |
| 107 | "option alias path test failed\n"); |
| 108 | of_node_put(np); |
| 109 | |
Peter Hurley | 8cbba1a | 2015-03-06 13:59:59 -0500 | [diff] [blame] | 110 | 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 Lindholm | 75c28c0 | 2014-11-28 11:34:28 +0000 | [diff] [blame] | 116 | 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 Likely | ae91ff7 | 2014-03-14 13:53:10 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Grant Likely | 7e66c5c | 2013-11-15 17:19:09 +0000 | [diff] [blame] | 131 | static 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 Likely | f2051d6 | 2014-10-01 17:40:22 +0100 | [diff] [blame] | 192 | static int __init of_selftest_check_node_linkage(struct device_node *np) |
| 193 | { |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 194 | struct device_node *child; |
Grant Likely | f2051d6 | 2014-10-01 17:40:22 +0100 | [diff] [blame] | 195 | 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 Likely | f2051d6 | 2014-10-01 17:40:22 +0100 | [diff] [blame] | 204 | 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 | |
| 213 | static void __init of_selftest_check_tree_linkage(void) |
| 214 | { |
| 215 | struct device_node *np; |
| 216 | int allnode_count = 0, child_count; |
| 217 | |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 218 | if (!of_root) |
Grant Likely | f2051d6 | 2014-10-01 17:40:22 +0100 | [diff] [blame] | 219 | return; |
| 220 | |
| 221 | for_each_of_allnodes(np) |
| 222 | allnode_count++; |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 223 | child_count = of_selftest_check_node_linkage(of_root); |
Grant Likely | f2051d6 | 2014-10-01 17:40:22 +0100 | [diff] [blame] | 224 | |
| 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 Likely | 841ec21 | 2014-10-02 13:09:15 +0100 | [diff] [blame] | 231 | struct node_hash { |
| 232 | struct hlist_node node; |
| 233 | struct device_node *np; |
| 234 | }; |
| 235 | |
Grant Likely | 2118f4b | 2014-10-07 11:30:31 +0100 | [diff] [blame] | 236 | static DEFINE_HASHTABLE(phandle_ht, 8); |
Grant Likely | 841ec21 | 2014-10-02 13:09:15 +0100 | [diff] [blame] | 237 | static 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 Likely | 841ec21 | 2014-10-02 13:09:15 +0100 | [diff] [blame] | 243 | |
Grant Likely | 841ec21 | 2014-10-02 13:09:15 +0100 | [diff] [blame] | 244 | for_each_of_allnodes(np) { |
| 245 | if (!np->phandle) |
| 246 | continue; |
| 247 | |
Grant Likely | 2118f4b | 2014-10-07 11:30:31 +0100 | [diff] [blame] | 248 | hash_for_each_possible(phandle_ht, nh, node, np->phandle) { |
Grant Likely | 841ec21 | 2014-10-02 13:09:15 +0100 | [diff] [blame] | 249 | 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 Likely | 2118f4b | 2014-10-07 11:30:31 +0100 | [diff] [blame] | 262 | hash_add(phandle_ht, &nh->node, np->phandle); |
Grant Likely | 841ec21 | 2014-10-02 13:09:15 +0100 | [diff] [blame] | 263 | 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 Likely | 2118f4b | 2014-10-07 11:30:31 +0100 | [diff] [blame] | 269 | hash_for_each_safe(phandle_ht, i, tmp, nh, node) { |
Grant Likely | 841ec21 | 2014-10-02 13:09:15 +0100 | [diff] [blame] | 270 | hash_del(&nh->node); |
| 271 | kfree(nh); |
| 272 | } |
| 273 | } |
| 274 | |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 275 | static void __init of_selftest_parse_phandle_with_args(void) |
| 276 | { |
| 277 | struct device_node *np; |
| 278 | struct of_phandle_args args; |
Grant Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 279 | int i, rc; |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 280 | |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 281 | 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 Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 287 | 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 Likely | f7f951c | 2013-02-12 17:41:22 +0000 | [diff] [blame] | 290 | for (i = 0; i < 8; i++) { |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 291 | 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 Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 334 | passed &= (rc == -ENOENT); |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 335 | break; |
| 336 | default: |
| 337 | passed = false; |
| 338 | } |
| 339 | |
Grant Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 340 | selftest(passed, "index %i - data error on node %s rc=%i\n", |
| 341 | i, args.np->full_name, rc); |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | /* Check for missing list property */ |
| 345 | rc = of_parse_phandle_with_args(np, "phandle-list-missing", |
| 346 | "#phandle-cells", 0, &args); |
Grant Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 347 | selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc); |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 348 | 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 Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 351 | |
| 352 | /* Check for missing cells property */ |
| 353 | rc = of_parse_phandle_with_args(np, "phandle-list", |
| 354 | "#phandle-cells-missing", 0, &args); |
Grant Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 355 | selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc); |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 356 | 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 Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 359 | |
| 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 Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 363 | selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc); |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 364 | 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 Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 367 | |
| 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 Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 371 | selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc); |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 372 | 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 Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 375 | } |
| 376 | |
Grant Likely | a87fa1d | 2014-11-03 15:15:35 +0000 | [diff] [blame] | 377 | static void __init of_selftest_property_string(void) |
Grant Likely | 7aff0fe | 2011-12-12 09:25:58 -0700 | [diff] [blame] | 378 | { |
Grant Likely | a87fa1d | 2014-11-03 15:15:35 +0000 | [diff] [blame] | 379 | const char *strings[4]; |
Grant Likely | 7aff0fe | 2011-12-12 09:25:58 -0700 | [diff] [blame] | 380 | struct device_node *np; |
| 381 | int rc; |
| 382 | |
Grant Likely | 7aff0fe | 2011-12-12 09:25:58 -0700 | [diff] [blame] | 383 | 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 Long | 649022e | 2015-03-03 03:50:38 +0000 | [diff] [blame] | 392 | selftest(rc == 1, "second expected:1 got:%i\n", rc); |
Grant Likely | 7aff0fe | 2011-12-12 09:25:58 -0700 | [diff] [blame] | 393 | rc = of_property_match_string(np, "phandle-list-names", "third"); |
Wang Long | 649022e | 2015-03-03 03:50:38 +0000 | [diff] [blame] | 394 | selftest(rc == 2, "third expected:2 got:%i\n", rc); |
Grant Likely | 7aff0fe | 2011-12-12 09:25:58 -0700 | [diff] [blame] | 395 | rc = of_property_match_string(np, "phandle-list-names", "fourth"); |
Grant Likely | a87fa1d | 2014-11-03 15:15:35 +0000 | [diff] [blame] | 396 | selftest(rc == -ENODATA, "unmatched string; rc=%i\n", rc); |
Grant Likely | 7aff0fe | 2011-12-12 09:25:58 -0700 | [diff] [blame] | 397 | rc = of_property_match_string(np, "missing-property", "blah"); |
Grant Likely | a87fa1d | 2014-11-03 15:15:35 +0000 | [diff] [blame] | 398 | selftest(rc == -EINVAL, "missing property; rc=%i\n", rc); |
Grant Likely | 7aff0fe | 2011-12-12 09:25:58 -0700 | [diff] [blame] | 399 | rc = of_property_match_string(np, "empty-property", "blah"); |
Grant Likely | a87fa1d | 2014-11-03 15:15:35 +0000 | [diff] [blame] | 400 | selftest(rc == -ENODATA, "empty property; rc=%i\n", rc); |
Grant Likely | 7aff0fe | 2011-12-12 09:25:58 -0700 | [diff] [blame] | 401 | rc = of_property_match_string(np, "unterminated-string", "blah"); |
Grant Likely | a87fa1d | 2014-11-03 15:15:35 +0000 | [diff] [blame] | 402 | 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 Likely | 7aff0fe | 2011-12-12 09:25:58 -0700 | [diff] [blame] | 456 | } |
| 457 | |
Pantelis Antoniou | 6984339 | 2014-07-04 19:58:47 +0300 | [diff] [blame] | 458 | #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)) |
| 462 | static 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 Antoniou | 201c910 | 2014-07-04 19:58:49 +0300 | [diff] [blame] | 483 | static 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 Likely | e517958 | 2014-11-17 22:31:32 +0000 | [diff] [blame] | 489 | struct device_node *n1, *n2, *n21, *nremove, *parent, *np; |
Pantelis Antoniou | 201c910 | 2014-07-04 19:58:49 +0300 | [diff] [blame] | 490 | struct of_changeset chgset; |
| 491 | |
Grant Likely | e517958 | 2014-11-17 22:31:32 +0000 | [diff] [blame] | 492 | n1 = __of_node_dup(NULL, "/testcase-data/changeset/n1"); |
Pantelis Antoniou | 201c910 | 2014-07-04 19:58:49 +0300 | [diff] [blame] | 493 | selftest(n1, "testcase setup failure\n"); |
Grant Likely | e517958 | 2014-11-17 22:31:32 +0000 | [diff] [blame] | 494 | n2 = __of_node_dup(NULL, "/testcase-data/changeset/n2"); |
Pantelis Antoniou | 201c910 | 2014-07-04 19:58:49 +0300 | [diff] [blame] | 495 | selftest(n2, "testcase setup failure\n"); |
Grant Likely | e517958 | 2014-11-17 22:31:32 +0000 | [diff] [blame] | 496 | n21 = __of_node_dup(NULL, "%s/%s", "/testcase-data/changeset/n2", "n21"); |
Pantelis Antoniou | 201c910 | 2014-07-04 19:58:49 +0300 | [diff] [blame] | 497 | 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 Likely | e517958 | 2014-11-17 22:31:32 +0000 | [diff] [blame] | 524 | /* 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 Elfring | c46ca3c | 2014-12-02 13:54:00 +0100 | [diff] [blame] | 527 | of_node_put(np); |
Grant Likely | e517958 | 2014-11-17 22:31:32 +0000 | [diff] [blame] | 528 | |
Pantelis Antoniou | 201c910 | 2014-07-04 19:58:49 +0300 | [diff] [blame] | 529 | 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 Likely | a9f10ca | 2013-10-11 22:04:23 +0100 | [diff] [blame] | 537 | static 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 Likely | 79d9701 | 2013-09-19 16:47:37 -0500 | [diff] [blame] | 609 | static 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 Likely | 1f42e5d | 2014-02-18 21:38:55 +0000 | [diff] [blame] | 678 | static 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 | |
| 695 | static 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 | |
| 713 | static 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 Likely | 851da97 | 2014-11-04 13:14:13 +0000 | [diff] [blame] | 744 | struct device test_bus = { |
| 745 | .init_name = "unittest-bus", |
| 746 | }; |
Rob Herring | 82c0f58 | 2014-04-23 17:57:40 -0500 | [diff] [blame] | 747 | static void __init of_selftest_platform_populate(void) |
| 748 | { |
Grant Likely | 851da97 | 2014-11-04 13:14:13 +0000 | [diff] [blame] | 749 | int irq, rc; |
| 750 | struct device_node *np, *child, *grandchild; |
Rob Herring | 82c0f58 | 2014-04-23 17:57:40 -0500 | [diff] [blame] | 751 | struct platform_device *pdev; |
Rob Herring | fb2caa5 | 2014-05-13 10:07:54 -0500 | [diff] [blame] | 752 | struct of_device_id match[] = { |
| 753 | { .compatible = "test-device", }, |
| 754 | {} |
| 755 | }; |
Rob Herring | 82c0f58 | 2014-04-23 17:57:40 -0500 | [diff] [blame] | 756 | |
| 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 Herring | 7d1cdc8 | 2014-05-13 10:07:29 -0500 | [diff] [blame] | 763 | selftest(pdev, "device 1 creation failed\n"); |
| 764 | |
Rob Herring | 82c0f58 | 2014-04-23 17:57:40 -0500 | [diff] [blame] | 765 | irq = platform_get_irq(pdev, 0); |
Rob Herring | 7d1cdc8 | 2014-05-13 10:07:29 -0500 | [diff] [blame] | 766 | selftest(irq == -EPROBE_DEFER, "device deferred probe failed - %d\n", irq); |
Rob Herring | 82c0f58 | 2014-04-23 17:57:40 -0500 | [diff] [blame] | 767 | |
| 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 Herring | 7d1cdc8 | 2014-05-13 10:07:29 -0500 | [diff] [blame] | 771 | selftest(pdev, "device 2 creation failed\n"); |
Rob Herring | 82c0f58 | 2014-04-23 17:57:40 -0500 | [diff] [blame] | 772 | irq = platform_get_irq(pdev, 0); |
Rob Herring | 7d1cdc8 | 2014-05-13 10:07:29 -0500 | [diff] [blame] | 773 | selftest(irq < 0 && irq != -EPROBE_DEFER, "device parsing error failed - %d\n", irq); |
Rob Herring | 82c0f58 | 2014-04-23 17:57:40 -0500 | [diff] [blame] | 774 | |
Grant Likely | 851da97 | 2014-11-04 13:14:13 +0000 | [diff] [blame] | 775 | if (selftest(np = of_find_node_by_path("/testcase-data/platform-tests"), |
| 776 | "No testcase data in device tree\n")); |
Rob Herring | fb2caa5 | 2014-05-13 10:07:54 -0500 | [diff] [blame] | 777 | return; |
Grant Likely | 851da97 | 2014-11-04 13:14:13 +0000 | [diff] [blame] | 778 | |
| 779 | if (selftest(!(rc = device_register(&test_bus)), |
| 780 | "testbus registration failed; rc=%i\n", rc)); |
| 781 | return; |
Rob Herring | fb2caa5 | 2014-05-13 10:07:54 -0500 | [diff] [blame] | 782 | |
| 783 | for_each_child_of_node(np, child) { |
Grant Likely | 851da97 | 2014-11-04 13:14:13 +0000 | [diff] [blame] | 784 | of_platform_populate(child, match, NULL, &test_bus); |
Rob Herring | fb2caa5 | 2014-05-13 10:07:54 -0500 | [diff] [blame] | 785 | 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 Likely | 851da97 | 2014-11-04 13:14:13 +0000 | [diff] [blame] | 790 | |
| 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 Herring | 82c0f58 | 2014-04-23 17:57:40 -0500 | [diff] [blame] | 801 | } |
| 802 | |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 803 | /** |
| 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 | */ |
| 811 | static 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 | */ |
| 830 | static int attach_node_and_children(struct device_node *np) |
| 831 | { |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 832 | struct device_node *next, *dup, *child; |
Gaurav Minocha | 3ce04b4 | 2015-01-10 23:19:51 -0800 | [diff] [blame] | 833 | unsigned long flags; |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 834 | |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 835 | dup = of_find_node_by_path(np->full_name); |
| 836 | if (dup) { |
| 837 | update_node_properties(np, dup); |
| 838 | return 0; |
| 839 | } |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 840 | |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 841 | child = np->child; |
| 842 | np->child = NULL; |
Gaurav Minocha | 3ce04b4 | 2015-01-10 23:19:51 -0800 | [diff] [blame] | 843 | |
| 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 Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 854 | while (child) { |
| 855 | next = child->sibling; |
| 856 | attach_node_and_children(child); |
| 857 | child = next; |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 858 | } |
| 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 | */ |
| 867 | static int __init selftest_data_add(void) |
| 868 | { |
| 869 | void *selftest_data; |
Gaurav Minocha | b951f9d | 2014-07-26 12:48:50 -0700 | [diff] [blame] | 870 | struct device_node *selftest_data_node, *np; |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 871 | extern uint8_t __dtb_testcases_begin[]; |
| 872 | extern uint8_t __dtb_testcases_end[]; |
| 873 | const int size = __dtb_testcases_end - __dtb_testcases_begin; |
Grant Likely | 2eb46da | 2014-10-02 14:36:46 +0100 | [diff] [blame] | 874 | int rc; |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 875 | |
Gaurav Minocha | b951f9d | 2014-07-26 12:48:50 -0700 | [diff] [blame] | 876 | if (!size) { |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 877 | 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 Minocha | b951f9d | 2014-07-26 12:48:50 -0700 | [diff] [blame] | 891 | if (!selftest_data_node) { |
| 892 | pr_warn("%s: No tree to attach; not running tests\n", __func__); |
| 893 | return -ENODATA; |
| 894 | } |
Grant Likely | 2eb46da | 2014-10-02 14:36:46 +0100 | [diff] [blame] | 895 | 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 Minocha | b951f9d | 2014-07-26 12:48:50 -0700 | [diff] [blame] | 901 | |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 902 | if (!of_root) { |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 903 | of_root = selftest_data_node; |
Gaurav Minocha | b951f9d | 2014-07-26 12:48:50 -0700 | [diff] [blame] | 904 | 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 Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 910 | |
| 911 | /* attach the sub-tree to live tree */ |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 912 | 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 Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 920 | } |
| 921 | |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 922 | #ifdef CONFIG_OF_OVERLAY |
| 923 | |
| 924 | static 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 Antoniou | 6b1271d | 2014-12-19 14:34:34 +0200 | [diff] [blame] | 936 | |
| 937 | of_platform_populate(np, NULL, NULL, &pdev->dev); |
| 938 | |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 939 | return 0; |
| 940 | } |
| 941 | |
| 942 | static 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 | |
| 951 | static struct of_device_id selftest_match[] = { |
| 952 | { .compatible = "selftest", }, |
| 953 | {}, |
| 954 | }; |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 955 | |
| 956 | static 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 */ |
| 967 | static 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 */ |
| 983 | static 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 Bergmann | 4252de3 | 2015-03-04 20:49:47 +0100 | [diff] [blame] | 992 | #if IS_BUILTIN(CONFIG_I2C) |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 993 | |
| 994 | /* get the i2c client device instantiated at the path */ |
| 995 | static struct i2c_client *of_path_to_i2c_client(const char *path) |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 996 | { |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 997 | 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 */ |
| 1011 | static 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 |
| 1021 | static int of_path_i2c_client_exists(const char *path) |
| 1022 | { |
| 1023 | return 0; |
| 1024 | } |
| 1025 | #endif |
| 1026 | |
| 1027 | enum overlay_type { |
| 1028 | PDEV_OVERLAY, |
| 1029 | I2C_OVERLAY |
| 1030 | }; |
| 1031 | |
| 1032 | static 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 | |
| 1044 | static const char *selftest_path(int nr, enum overlay_type ovtype) |
| 1045 | { |
| 1046 | const char *base; |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1047 | static char buf[256]; |
| 1048 | |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1049 | 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 Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1061 | buf[sizeof(buf) - 1] = '\0'; |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1062 | return buf; |
| 1063 | } |
| 1064 | |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1065 | static 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 Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1080 | static 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 | |
| 1091 | static const char *bus_path = "/testcase-data/overlay-node/test-bus"; |
| 1092 | |
| 1093 | static 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 | |
| 1117 | out: |
| 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 */ |
| 1127 | static int of_selftest_apply_overlay_check(int overlay_nr, int selftest_nr, |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1128 | int before, int after, enum overlay_type ovtype) |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1129 | { |
| 1130 | int ret; |
| 1131 | |
| 1132 | /* selftest device must not be in before state */ |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1133 | if (of_selftest_device_exists(selftest_nr, ovtype) != before) { |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1134 | selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n", |
| 1135 | overlay_path(overlay_nr), |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1136 | selftest_path(selftest_nr, ovtype), |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1137 | !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 Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1148 | if (of_selftest_device_exists(selftest_nr, ovtype) != after) { |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1149 | selftest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n", |
| 1150 | overlay_path(overlay_nr), |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1151 | selftest_path(selftest_nr, ovtype), |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1152 | !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 */ |
| 1160 | static int of_selftest_apply_revert_overlay_check(int overlay_nr, |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1161 | int selftest_nr, int before, int after, |
| 1162 | enum overlay_type ovtype) |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1163 | { |
| 1164 | int ret, ov_id; |
| 1165 | |
| 1166 | /* selftest device must be in before state */ |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1167 | if (of_selftest_device_exists(selftest_nr, ovtype) != before) { |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1168 | selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n", |
| 1169 | overlay_path(overlay_nr), |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1170 | selftest_path(selftest_nr, ovtype), |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1171 | !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 Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1183 | if (of_selftest_device_exists(selftest_nr, ovtype) != after) { |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1184 | selftest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n", |
| 1185 | overlay_path(overlay_nr), |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1186 | selftest_path(selftest_nr, ovtype), |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1187 | !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 Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1195 | selftest_path(selftest_nr, ovtype)); |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1196 | return ret; |
| 1197 | } |
| 1198 | |
| 1199 | /* selftest device must be again in before state */ |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1200 | if (of_selftest_device_exists(selftest_nr, PDEV_OVERLAY) != before) { |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1201 | selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n", |
| 1202 | overlay_path(overlay_nr), |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1203 | selftest_path(selftest_nr, ovtype), |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1204 | !before ? "enabled" : "disabled"); |
| 1205 | return -EINVAL; |
| 1206 | } |
| 1207 | |
| 1208 | return 0; |
| 1209 | } |
| 1210 | |
| 1211 | /* test activation of device */ |
| 1212 | static void of_selftest_overlay_0(void) |
| 1213 | { |
| 1214 | int ret; |
| 1215 | |
| 1216 | /* device should enable */ |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1217 | ret = of_selftest_apply_overlay_check(0, 0, 0, 1, PDEV_OVERLAY); |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1218 | if (ret != 0) |
| 1219 | return; |
| 1220 | |
| 1221 | selftest(1, "overlay test %d passed\n", 0); |
| 1222 | } |
| 1223 | |
| 1224 | /* test deactivation of device */ |
| 1225 | static void of_selftest_overlay_1(void) |
| 1226 | { |
| 1227 | int ret; |
| 1228 | |
| 1229 | /* device should disable */ |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1230 | ret = of_selftest_apply_overlay_check(1, 1, 1, 0, PDEV_OVERLAY); |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1231 | if (ret != 0) |
| 1232 | return; |
| 1233 | |
| 1234 | selftest(1, "overlay test %d passed\n", 1); |
| 1235 | } |
| 1236 | |
| 1237 | /* test activation of device */ |
| 1238 | static void of_selftest_overlay_2(void) |
| 1239 | { |
| 1240 | int ret; |
| 1241 | |
| 1242 | /* device should enable */ |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1243 | ret = of_selftest_apply_overlay_check(2, 2, 0, 1, PDEV_OVERLAY); |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1244 | if (ret != 0) |
| 1245 | return; |
| 1246 | |
| 1247 | selftest(1, "overlay test %d passed\n", 2); |
| 1248 | } |
| 1249 | |
| 1250 | /* test deactivation of device */ |
| 1251 | static void of_selftest_overlay_3(void) |
| 1252 | { |
| 1253 | int ret; |
| 1254 | |
| 1255 | /* device should disable */ |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1256 | ret = of_selftest_apply_overlay_check(3, 3, 1, 0, PDEV_OVERLAY); |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1257 | 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 */ |
| 1264 | static void of_selftest_overlay_4(void) |
| 1265 | { |
| 1266 | int ret; |
| 1267 | |
| 1268 | /* device should disable */ |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1269 | ret = of_selftest_apply_overlay_check(4, 4, 0, 1, PDEV_OVERLAY); |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1270 | if (ret != 0) |
| 1271 | return; |
| 1272 | |
| 1273 | selftest(1, "overlay test %d passed\n", 4); |
| 1274 | } |
| 1275 | |
| 1276 | /* test overlay apply/revert sequence */ |
| 1277 | static void of_selftest_overlay_5(void) |
| 1278 | { |
| 1279 | int ret; |
| 1280 | |
| 1281 | /* device should disable */ |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1282 | ret = of_selftest_apply_revert_overlay_check(5, 5, 0, 1, PDEV_OVERLAY); |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1283 | if (ret != 0) |
| 1284 | return; |
| 1285 | |
| 1286 | selftest(1, "overlay test %d passed\n", 5); |
| 1287 | } |
| 1288 | |
| 1289 | /* test overlay application in sequence */ |
| 1290 | static 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 Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1299 | if (of_selftest_device_exists(selftest_nr + i, PDEV_OVERLAY) |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1300 | != before) { |
| 1301 | selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n", |
| 1302 | overlay_path(overlay_nr + i), |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1303 | selftest_path(selftest_nr + i, |
| 1304 | PDEV_OVERLAY), |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1305 | !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 Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1331 | if (of_selftest_device_exists(selftest_nr + i, PDEV_OVERLAY) |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1332 | != after) { |
| 1333 | selftest(0, "overlay @\"%s\" failed @\"%s\" %s\n", |
| 1334 | overlay_path(overlay_nr + i), |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1335 | selftest_path(selftest_nr + i, |
| 1336 | PDEV_OVERLAY), |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1337 | !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 Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1347 | selftest_path(selftest_nr + i, |
| 1348 | PDEV_OVERLAY)); |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1349 | return; |
| 1350 | } |
| 1351 | } |
| 1352 | |
| 1353 | for (i = 0; i < 2; i++) { |
| 1354 | /* selftest device must be again in before state */ |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1355 | if (of_selftest_device_exists(selftest_nr + i, PDEV_OVERLAY) |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1356 | != before) { |
| 1357 | selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n", |
| 1358 | overlay_path(overlay_nr + i), |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1359 | selftest_path(selftest_nr + i, |
| 1360 | PDEV_OVERLAY), |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1361 | !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 */ |
| 1370 | static 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 Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1402 | selftest_path(selftest_nr, |
| 1403 | PDEV_OVERLAY)); |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1404 | 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 Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1413 | selftest_path(selftest_nr, |
| 1414 | PDEV_OVERLAY)); |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1415 | return; |
| 1416 | } |
| 1417 | } |
| 1418 | |
| 1419 | selftest(1, "overlay test %d passed\n", 8); |
| 1420 | } |
| 1421 | |
Pantelis Antoniou | 6b1271d | 2014-12-19 14:34:34 +0200 | [diff] [blame] | 1422 | /* test insertion of a bus with parent devices */ |
| 1423 | static void of_selftest_overlay_10(void) |
| 1424 | { |
| 1425 | int ret; |
| 1426 | char *child_path; |
| 1427 | |
| 1428 | /* device should disable */ |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1429 | 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 Antoniou | 6b1271d | 2014-12-19 14:34:34 +0200 | [diff] [blame] | 1432 | return; |
| 1433 | |
| 1434 | child_path = kasprintf(GFP_KERNEL, "%s/test-selftest101", |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1435 | selftest_path(10, PDEV_OVERLAY)); |
Pantelis Antoniou | 6b1271d | 2014-12-19 14:34:34 +0200 | [diff] [blame] | 1436 | if (selftest(child_path, "overlay test %d failed; kasprintf\n", 10)) |
| 1437 | return; |
| 1438 | |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1439 | ret = of_path_device_type_exists(child_path, PDEV_OVERLAY); |
Pantelis Antoniou | 6b1271d | 2014-12-19 14:34:34 +0200 | [diff] [blame] | 1440 | 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) */ |
| 1446 | static void of_selftest_overlay_11(void) |
| 1447 | { |
| 1448 | int ret; |
| 1449 | |
| 1450 | /* device should disable */ |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1451 | 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 Antoniou | 6b1271d | 2014-12-19 14:34:34 +0200 | [diff] [blame] | 1455 | return; |
| 1456 | } |
| 1457 | |
Arnd Bergmann | 4252de3 | 2015-03-04 20:49:47 +0100 | [diff] [blame] | 1458 | #if IS_BUILTIN(CONFIG_I2C) && IS_ENABLED(CONFIG_OF_OVERLAY) |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1459 | |
| 1460 | struct selftest_i2c_bus_data { |
| 1461 | struct platform_device *pdev; |
| 1462 | struct i2c_adapter adap; |
| 1463 | }; |
| 1464 | |
| 1465 | static 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 | |
| 1475 | static u32 selftest_i2c_functionality(struct i2c_adapter *adap) |
| 1476 | { |
| 1477 | return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL; |
| 1478 | } |
| 1479 | |
| 1480 | static const struct i2c_algorithm selftest_i2c_algo = { |
| 1481 | .master_xfer = selftest_i2c_master_xfer, |
| 1482 | .functionality = selftest_i2c_functionality, |
| 1483 | }; |
| 1484 | |
| 1485 | static 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 | |
| 1531 | static 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 | |
| 1543 | static struct of_device_id selftest_i2c_bus_match[] = { |
| 1544 | { .compatible = "selftest-i2c-bus", }, |
| 1545 | {}, |
| 1546 | }; |
| 1547 | |
| 1548 | static 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 | |
| 1557 | static 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 | |
| 1573 | static 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 | |
| 1582 | static const struct i2c_device_id selftest_i2c_dev_id[] = { |
| 1583 | { .name = "selftest-i2c-dev" }, |
| 1584 | { } |
| 1585 | }; |
| 1586 | |
| 1587 | static 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 Bergmann | 4252de3 | 2015-03-04 20:49:47 +0100 | [diff] [blame] | 1597 | #if IS_BUILTIN(CONFIG_I2C_MUX) |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1598 | |
| 1599 | struct selftest_i2c_mux_data { |
| 1600 | int nchans; |
| 1601 | struct i2c_adapter *adap[]; |
| 1602 | }; |
| 1603 | |
| 1604 | static int selftest_i2c_mux_select_chan(struct i2c_adapter *adap, |
| 1605 | void *client, u32 chan) |
| 1606 | { |
| 1607 | return 0; |
| 1608 | } |
| 1609 | |
| 1610 | static 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", ®); |
| 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 | |
| 1664 | static 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 | |
| 1677 | static const struct i2c_device_id selftest_i2c_mux_id[] = { |
| 1678 | { .name = "selftest-i2c-mux" }, |
| 1679 | { } |
| 1680 | }; |
| 1681 | |
| 1682 | static 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 | |
| 1694 | static 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 Bergmann | 4252de3 | 2015-03-04 20:49:47 +0100 | [diff] [blame] | 1708 | #if IS_BUILTIN(CONFIG_I2C_MUX) |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1709 | 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 | |
| 1718 | static void of_selftest_overlay_i2c_cleanup(void) |
| 1719 | { |
Arnd Bergmann | 4252de3 | 2015-03-04 20:49:47 +0100 | [diff] [blame] | 1720 | #if IS_BUILTIN(CONFIG_I2C_MUX) |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1721 | 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 | |
| 1727 | static 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 */ |
| 1740 | static 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 */ |
| 1753 | static void of_selftest_overlay_i2c_14(void) |
| 1754 | { |
| 1755 | } |
| 1756 | |
| 1757 | static 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 | |
| 1771 | static inline void of_selftest_overlay_i2c_14(void) { } |
| 1772 | static inline void of_selftest_overlay_i2c_15(void) { } |
| 1773 | |
| 1774 | #endif |
| 1775 | |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1776 | static 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 Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1800 | if (!of_selftest_device_exists(100, PDEV_OVERLAY)) { |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1801 | selftest(0, "could not find selftest0 @ \"%s\"\n", |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1802 | selftest_path(100, PDEV_OVERLAY)); |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1803 | goto out; |
| 1804 | } |
| 1805 | |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1806 | if (of_selftest_device_exists(101, PDEV_OVERLAY)) { |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1807 | selftest(0, "selftest1 @ \"%s\" should not exist\n", |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1808 | selftest_path(101, PDEV_OVERLAY)); |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1809 | 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 Antoniou | 6b1271d | 2014-12-19 14:34:34 +0200 | [diff] [blame] | 1824 | of_selftest_overlay_10(); |
| 1825 | of_selftest_overlay_11(); |
| 1826 | |
Arnd Bergmann | 4252de3 | 2015-03-04 20:49:47 +0100 | [diff] [blame] | 1827 | #if IS_BUILTIN(CONFIG_I2C) |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1828 | 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 Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1839 | out: |
| 1840 | of_node_put(bus_np); |
| 1841 | } |
| 1842 | |
| 1843 | #else |
| 1844 | static inline void __init of_selftest_overlay(void) { } |
| 1845 | #endif |
| 1846 | |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1847 | static int __init of_selftest(void) |
| 1848 | { |
| 1849 | struct device_node *np; |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 1850 | int res; |
| 1851 | |
| 1852 | /* adding data for selftest */ |
| 1853 | res = selftest_data_add(); |
| 1854 | if (res) |
| 1855 | return res; |
Grant Likely | 788ec2f | 2014-11-19 17:13:44 +0000 | [diff] [blame] | 1856 | if (!of_aliases) |
| 1857 | of_aliases = of_find_node_by_path("/aliases"); |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1858 | |
| 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 Likely | f2051d6 | 2014-10-01 17:40:22 +0100 | [diff] [blame] | 1867 | of_selftest_check_tree_linkage(); |
Grant Likely | 841ec21 | 2014-10-02 13:09:15 +0100 | [diff] [blame] | 1868 | of_selftest_check_phandles(); |
Grant Likely | ae91ff7 | 2014-03-14 13:53:10 +0000 | [diff] [blame] | 1869 | of_selftest_find_node_by_name(); |
Grant Likely | 7e66c5c | 2013-11-15 17:19:09 +0000 | [diff] [blame] | 1870 | of_selftest_dynamic(); |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1871 | of_selftest_parse_phandle_with_args(); |
Grant Likely | a87fa1d | 2014-11-03 15:15:35 +0000 | [diff] [blame] | 1872 | of_selftest_property_string(); |
Pantelis Antoniou | 6984339 | 2014-07-04 19:58:47 +0300 | [diff] [blame] | 1873 | of_selftest_property_copy(); |
Pantelis Antoniou | 201c910 | 2014-07-04 19:58:49 +0300 | [diff] [blame] | 1874 | of_selftest_changeset(); |
Grant Likely | a9f10ca | 2013-10-11 22:04:23 +0100 | [diff] [blame] | 1875 | of_selftest_parse_interrupts(); |
Grant Likely | 79d9701 | 2013-09-19 16:47:37 -0500 | [diff] [blame] | 1876 | of_selftest_parse_interrupts_extended(); |
Grant Likely | 1f42e5d | 2014-02-18 21:38:55 +0000 | [diff] [blame] | 1877 | of_selftest_match_node(); |
Rob Herring | 82c0f58 | 2014-04-23 17:57:40 -0500 | [diff] [blame] | 1878 | of_selftest_platform_populate(); |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1879 | of_selftest_overlay(); |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 1880 | |
Grant Likely | f2051d6 | 2014-10-01 17:40:22 +0100 | [diff] [blame] | 1881 | /* 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 Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1887 | return 0; |
| 1888 | } |
| 1889 | late_initcall(of_selftest); |