1 // SPDX-License-Identifier: GPL-2.0
3 * KUnit tests for device tree overlays
5 #include <linux/device/bus.h>
6 #include <linux/kconfig.h>
8 #include <linux/of_platform.h>
9 #include <linux/platform_device.h>
12 #include <kunit/test.h>
14 #include "of_private.h"
16 static const char * const kunit_node_name
= "kunit-test";
17 static const char * const kunit_compatible
= "test,empty";
19 /* Test that of_overlay_apply_kunit() adds a node to the live tree */
20 static void of_overlay_apply_kunit_apply(struct kunit
*test
)
22 struct device_node
*np
;
24 KUNIT_ASSERT_EQ(test
, 0,
25 of_overlay_apply_kunit(test
, kunit_overlay_test
));
27 np
= of_find_node_by_name(NULL
, kunit_node_name
);
28 KUNIT_EXPECT_NOT_ERR_OR_NULL(test
, np
);
33 * Test that of_overlay_apply_kunit() creates platform devices with the
34 * expected device_node
36 static void of_overlay_apply_kunit_platform_device(struct kunit
*test
)
38 struct platform_device
*pdev
;
39 struct device_node
*np
;
41 KUNIT_ASSERT_EQ(test
, 0,
42 of_overlay_apply_kunit(test
, kunit_overlay_test
));
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
);
48 pdev
= of_find_device_by_node(np
);
49 KUNIT_EXPECT_NOT_ERR_OR_NULL(test
, pdev
);
51 put_device(&pdev
->dev
);
54 static int of_overlay_bus_match_compatible(struct device
*dev
, const void *data
)
56 return of_device_is_compatible(dev
->of_node
, data
);
59 /* Test that of_overlay_apply_kunit() cleans up after the test is finished */
60 static void of_overlay_apply_kunit_cleanup(struct kunit
*test
)
63 struct platform_device
*pdev
;
65 struct device_node
*np
;
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");
73 kunit_init_test(&fake
, "fake test", NULL
);
74 KUNIT_ASSERT_EQ(test
, fake
.status
, KUNIT_SUCCESS
);
76 KUNIT_ASSERT_EQ(test
, 0,
77 of_overlay_apply_kunit(&fake
, kunit_overlay_test
));
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
);
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 */
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
);
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
);
101 static 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
),
109 * Test suite for test managed device tree overlays.
111 static struct kunit_suite of_overlay_apply_kunit_suite
= {
112 .name
= "of_overlay_apply_kunit",
113 .test_cases
= of_overlay_apply_kunit_test_cases
,
117 &of_overlay_apply_kunit_suite
,
119 MODULE_LICENSE("GPL");
120 MODULE_DESCRIPTION("KUnit tests for device tree overlays");