1 // SPDX-License-Identifier: GPL-2.0
3 #include <drm/drm_drv.h>
4 #include <drm/drm_kunit_helpers.h>
5 #include <drm/drm_managed.h>
7 #include <kunit/resource.h>
9 #include <linux/device.h>
10 #include <linux/platform_device.h>
12 #define KUNIT_DEVICE_NAME "drm-kunit-mock-device"
14 static const struct drm_mode_config_funcs drm_mode_config_funcs = {
17 static int fake_probe(struct platform_device *pdev)
22 static struct platform_driver fake_platform_driver = {
25 .name = KUNIT_DEVICE_NAME,
30 * drm_kunit_helper_alloc_device - Allocate a mock device for a KUnit test
31 * @test: The test context object
33 * This allocates a fake struct &device to create a mock for a KUnit
34 * test. The device will also be bound to a fake driver. It will thus be
35 * able to leverage the usual infrastructure and most notably the
36 * device-managed resources just like a "real" device.
38 * Callers need to make sure drm_kunit_helper_free_device() on the
42 * A pointer to the new device, or an ERR_PTR() otherwise.
44 struct device *drm_kunit_helper_alloc_device(struct kunit *test)
46 struct platform_device *pdev;
49 ret = platform_driver_register(&fake_platform_driver);
50 KUNIT_ASSERT_EQ(test, ret, 0);
52 pdev = platform_device_alloc(KUNIT_DEVICE_NAME, PLATFORM_DEVID_NONE);
53 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
55 ret = platform_device_add(pdev);
56 KUNIT_ASSERT_EQ(test, ret, 0);
60 EXPORT_SYMBOL_GPL(drm_kunit_helper_alloc_device);
63 * drm_kunit_helper_free_device - Frees a mock device
64 * @test: The test context object
65 * @dev: The device to free
67 * Frees a device allocated with drm_kunit_helper_alloc_device().
69 void drm_kunit_helper_free_device(struct kunit *test, struct device *dev)
71 struct platform_device *pdev = to_platform_device(dev);
73 platform_device_unregister(pdev);
74 platform_driver_unregister(&fake_platform_driver);
76 EXPORT_SYMBOL_GPL(drm_kunit_helper_free_device);
79 __drm_kunit_helper_alloc_drm_device_with_driver(struct kunit *test,
81 size_t size, size_t offset,
82 const struct drm_driver *driver)
84 struct drm_device *drm;
88 container = __devm_drm_dev_alloc(dev, driver, size, offset);
89 if (IS_ERR(container))
90 return ERR_CAST(container);
92 drm = container + offset;
93 drm->mode_config.funcs = &drm_mode_config_funcs;
95 ret = drmm_mode_config_init(drm);
101 EXPORT_SYMBOL_GPL(__drm_kunit_helper_alloc_drm_device_with_driver);
104 MODULE_LICENSE("GPL");