Loading...
Note: File does not exist in v4.6.
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * KUnit tests for device tree overlays
4 */
5#include <linux/device/bus.h>
6#include <linux/kconfig.h>
7#include <linux/of.h>
8#include <linux/of_platform.h>
9#include <linux/platform_device.h>
10
11#include <kunit/of.h>
12#include <kunit/test.h>
13
14#include "of_private.h"
15
16static const char * const kunit_node_name = "kunit-test";
17static const char * const kunit_compatible = "test,empty";
18
19/* Test that of_overlay_apply_kunit() adds a node to the live tree */
20static void of_overlay_apply_kunit_apply(struct kunit *test)
21{
22 struct device_node *np;
23
24 KUNIT_ASSERT_EQ(test, 0,
25 of_overlay_apply_kunit(test, kunit_overlay_test));
26
27 np = of_find_node_by_name(NULL, kunit_node_name);
28 KUNIT_EXPECT_NOT_ERR_OR_NULL(test, np);
29 of_node_put(np);
30}
31
32/*
33 * Test that of_overlay_apply_kunit() creates platform devices with the
34 * expected device_node
35 */
36static void of_overlay_apply_kunit_platform_device(struct kunit *test)
37{
38 struct platform_device *pdev;
39 struct device_node *np;
40
41 KUNIT_ASSERT_EQ(test, 0,
42 of_overlay_apply_kunit(test, kunit_overlay_test));
43
44 np = of_find_node_by_name(NULL, kunit_node_name);
45 of_node_put_kunit(test, np);
46 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, np);
47
48 pdev = of_find_device_by_node(np);
49 KUNIT_EXPECT_NOT_ERR_OR_NULL(test, pdev);
50 if (pdev)
51 put_device(&pdev->dev);
52}
53
54static int of_overlay_bus_match_compatible(struct device *dev, const void *data)
55{
56 return of_device_is_compatible(dev->of_node, data);
57}
58
59/* Test that of_overlay_apply_kunit() cleans up after the test is finished */
60static void of_overlay_apply_kunit_cleanup(struct kunit *test)
61{
62 struct kunit fake;
63 struct platform_device *pdev;
64 struct device *dev;
65 struct device_node *np;
66
67 of_root_kunit_skip(test);
68 if (!IS_ENABLED(CONFIG_OF_OVERLAY))
69 kunit_skip(test, "requires CONFIG_OF_OVERLAY to apply overlay");
70 if (!IS_ENABLED(CONFIG_OF_EARLY_FLATTREE))
71 kunit_skip(test, "requires CONFIG_OF_EARLY_FLATTREE for root node");
72
73 kunit_init_test(&fake, "fake test", NULL);
74 KUNIT_ASSERT_EQ(test, fake.status, KUNIT_SUCCESS);
75
76 KUNIT_ASSERT_EQ(test, 0,
77 of_overlay_apply_kunit(&fake, kunit_overlay_test));
78
79 np = of_find_node_by_name(NULL, kunit_node_name);
80 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, np);
81 of_node_put_kunit(&fake, np);
82
83 pdev = of_find_device_by_node(np);
84 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
85 put_device(&pdev->dev); /* Not derefing 'pdev' after this */
86
87 /* Remove overlay */
88 kunit_cleanup(&fake);
89
90 /* The node and device should be removed */
91 np = of_find_node_by_name(NULL, kunit_node_name);
92 KUNIT_EXPECT_PTR_EQ(test, NULL, np);
93 of_node_put(np);
94
95 dev = bus_find_device(&platform_bus_type, NULL, kunit_compatible,
96 of_overlay_bus_match_compatible);
97 KUNIT_EXPECT_PTR_EQ(test, NULL, dev);
98 put_device(dev);
99}
100
101static struct kunit_case of_overlay_apply_kunit_test_cases[] = {
102 KUNIT_CASE(of_overlay_apply_kunit_apply),
103 KUNIT_CASE(of_overlay_apply_kunit_platform_device),
104 KUNIT_CASE(of_overlay_apply_kunit_cleanup),
105 {}
106};
107
108/*
109 * Test suite for test managed device tree overlays.
110 */
111static struct kunit_suite of_overlay_apply_kunit_suite = {
112 .name = "of_overlay_apply_kunit",
113 .test_cases = of_overlay_apply_kunit_test_cases,
114};
115
116kunit_test_suites(
117 &of_overlay_apply_kunit_suite,
118);
119MODULE_LICENSE("GPL");
120MODULE_DESCRIPTION("KUnit tests for device tree overlays");