1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2022 Maxime Ripard <mripard@kernel.org>
4  */
5 
6 #include <kunit/test.h>
7 
8 #include <drm/drm_connector.h>
9 #include <drm/drm_edid.h>
10 #include <drm/drm_drv.h>
11 #include <drm/drm_modes.h>
12 #include <drm/drm_modeset_helper_vtables.h>
13 #include <drm/drm_probe_helper.h>
14 
15 #include "drm_kunit_helpers.h"
16 
17 struct drm_client_modeset_test_priv {
18 	struct drm_device *drm;
19 	struct drm_connector connector;
20 };
21 
22 static int drm_client_modeset_connector_get_modes(struct drm_connector *connector)
23 {
24 	return drm_add_modes_noedid(connector, 1920, 1200);
25 }
26 
27 static const struct drm_connector_helper_funcs drm_client_modeset_connector_helper_funcs = {
28 	.get_modes = drm_client_modeset_connector_get_modes,
29 };
30 
31 static const struct drm_connector_funcs drm_client_modeset_connector_funcs = {
32 };
33 
34 static int drm_client_modeset_test_init(struct kunit *test)
35 {
36 	struct drm_client_modeset_test_priv *priv;
37 	int ret;
38 
39 	priv = kunit_kzalloc(test, sizeof(*priv), GFP_KERNEL);
40 	KUNIT_ASSERT_NOT_NULL(test, priv);
41 
42 	test->priv = priv;
43 
44 	priv->drm = drm_kunit_device_init(test, DRIVER_MODESET, "drm-client-modeset-test");
45 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, priv->drm);
46 
47 	ret = drmm_connector_init(priv->drm, &priv->connector,
48 				  &drm_client_modeset_connector_funcs,
49 				  DRM_MODE_CONNECTOR_Unknown,
50 				  NULL);
51 	KUNIT_ASSERT_EQ(test, ret, 0);
52 
53 	drm_connector_helper_add(&priv->connector, &drm_client_modeset_connector_helper_funcs);
54 
55 	return 0;
56 }
57 
58 static void drm_test_pick_cmdline_res_1920_1080_60(struct kunit *test)
59 {
60 	struct drm_client_modeset_test_priv *priv = test->priv;
61 	struct drm_device *drm = priv->drm;
62 	struct drm_connector *connector = &priv->connector;
63 	struct drm_cmdline_mode *cmdline_mode = &connector->cmdline_mode;
64 	struct drm_display_mode *expected_mode, *mode;
65 	const char *cmdline = "1920x1080@60";
66 	int ret;
67 
68 	expected_mode = drm_mode_find_dmt(priv->drm, 1920, 1080, 60, false);
69 	KUNIT_ASSERT_NOT_NULL(test, expected_mode);
70 
71 	KUNIT_ASSERT_TRUE(test,
72 			  drm_mode_parse_command_line_for_connector(cmdline,
73 								    connector,
74 								    cmdline_mode));
75 
76 	mutex_lock(&drm->mode_config.mutex);
77 	ret = drm_helper_probe_single_connector_modes(connector, 1920, 1080);
78 	mutex_unlock(&drm->mode_config.mutex);
79 	KUNIT_ASSERT_GT(test, ret, 0);
80 
81 	mode = drm_connector_pick_cmdline_mode(connector);
82 	KUNIT_ASSERT_NOT_NULL(test, mode);
83 
84 	KUNIT_EXPECT_TRUE(test, drm_mode_equal(expected_mode, mode));
85 }
86 
87 static struct kunit_case drm_test_pick_cmdline_tests[] = {
88 	KUNIT_CASE(drm_test_pick_cmdline_res_1920_1080_60),
89 	{}
90 };
91 
92 static struct kunit_suite drm_test_pick_cmdline_test_suite = {
93 	.name = "drm_test_pick_cmdline",
94 	.init = drm_client_modeset_test_init,
95 	.test_cases = drm_test_pick_cmdline_tests
96 };
97 
98 kunit_test_suite(drm_test_pick_cmdline_test_suite);
99