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