1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Kunit test for drm_modes functions
4  */
5 
6 #include <drm/drm_drv.h>
7 #include <drm/drm_kunit_helpers.h>
8 #include <drm/drm_modes.h>
9 
10 #include <kunit/test.h>
11 
12 #include <linux/units.h>
13 
14 struct drm_test_modes_priv {
15 	struct drm_device *drm;
16 	struct device *dev;
17 };
18 
19 static int drm_test_modes_init(struct kunit *test)
20 {
21 	struct drm_test_modes_priv *priv;
22 
23 	priv = kunit_kzalloc(test, sizeof(*priv), GFP_KERNEL);
24 	KUNIT_ASSERT_NOT_NULL(test, priv);
25 
26 	priv->dev = drm_kunit_helper_alloc_device(test);
27 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, priv->dev);
28 
29 	priv->drm = __drm_kunit_helper_alloc_drm_device(test, priv->dev,
30 							sizeof(*priv->drm), 0,
31 							DRIVER_MODESET);
32 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, priv->drm);
33 
34 	test->priv = priv;
35 
36 	return 0;
37 }
38 
39 static void drm_test_modes_exit(struct kunit *test)
40 {
41 	struct drm_test_modes_priv *priv = test->priv;
42 
43 	drm_kunit_helper_free_device(test, priv->dev);
44 }
45 
46 static void drm_test_modes_analog_tv_ntsc_480i(struct kunit *test)
47 {
48 	struct drm_test_modes_priv *priv = test->priv;
49 	struct drm_display_mode *mode;
50 
51 	mode = drm_analog_tv_mode(priv->drm,
52 				  DRM_MODE_TV_MODE_NTSC,
53 				  13500 * HZ_PER_KHZ, 720, 480,
54 				  true);
55 	KUNIT_ASSERT_NOT_NULL(test, mode);
56 
57 	KUNIT_EXPECT_EQ(test, drm_mode_vrefresh(mode), 60);
58 	KUNIT_EXPECT_EQ(test, mode->hdisplay, 720);
59 
60 	/* BT.601 defines hsync_start at 736 for 480i */
61 	KUNIT_EXPECT_EQ(test, mode->hsync_start, 736);
62 
63 	/*
64 	 * The NTSC standard expects a line to take 63.556us. With a
65 	 * pixel clock of 13.5 MHz, a pixel takes around 74ns, so we
66 	 * need to have 63556ns / 74ns = 858.
67 	 *
68 	 * This is also mandated by BT.601.
69 	 */
70 	KUNIT_EXPECT_EQ(test, mode->htotal, 858);
71 
72 	KUNIT_EXPECT_EQ(test, mode->vdisplay, 480);
73 	KUNIT_EXPECT_EQ(test, mode->vtotal, 525);
74 }
75 
76 static void drm_test_modes_analog_tv_ntsc_480i_inlined(struct kunit *test)
77 {
78 	struct drm_test_modes_priv *priv = test->priv;
79 	struct drm_display_mode *expected, *mode;
80 
81 	expected = drm_analog_tv_mode(priv->drm,
82 				      DRM_MODE_TV_MODE_NTSC,
83 				      13500 * HZ_PER_KHZ, 720, 480,
84 				      true);
85 	KUNIT_ASSERT_NOT_NULL(test, expected);
86 
87 	mode = drm_mode_analog_ntsc_480i(priv->drm);
88 	KUNIT_ASSERT_NOT_NULL(test, mode);
89 
90 	KUNIT_EXPECT_TRUE(test, drm_mode_equal(expected, mode));
91 }
92 
93 static void drm_test_modes_analog_tv_pal_576i(struct kunit *test)
94 {
95 	struct drm_test_modes_priv *priv = test->priv;
96 	struct drm_display_mode *mode;
97 
98 	mode = drm_analog_tv_mode(priv->drm,
99 				  DRM_MODE_TV_MODE_PAL,
100 				  13500 * HZ_PER_KHZ, 720, 576,
101 				  true);
102 	KUNIT_ASSERT_NOT_NULL(test, mode);
103 
104 	KUNIT_EXPECT_EQ(test, drm_mode_vrefresh(mode), 50);
105 	KUNIT_EXPECT_EQ(test, mode->hdisplay, 720);
106 
107 	/* BT.601 defines hsync_start at 732 for 576i */
108 	KUNIT_EXPECT_EQ(test, mode->hsync_start, 732);
109 
110 	/*
111 	 * The PAL standard expects a line to take 64us. With a pixel
112 	 * clock of 13.5 MHz, a pixel takes around 74ns, so we need to
113 	 * have 64000ns / 74ns = 864.
114 	 *
115 	 * This is also mandated by BT.601.
116 	 */
117 	KUNIT_EXPECT_EQ(test, mode->htotal, 864);
118 
119 	KUNIT_EXPECT_EQ(test, mode->vdisplay, 576);
120 	KUNIT_EXPECT_EQ(test, mode->vtotal, 625);
121 }
122 
123 static void drm_test_modes_analog_tv_pal_576i_inlined(struct kunit *test)
124 {
125 	struct drm_test_modes_priv *priv = test->priv;
126 	struct drm_display_mode *expected, *mode;
127 
128 	expected = drm_analog_tv_mode(priv->drm,
129 				      DRM_MODE_TV_MODE_PAL,
130 				      13500 * HZ_PER_KHZ, 720, 576,
131 				      true);
132 	KUNIT_ASSERT_NOT_NULL(test, expected);
133 
134 	mode = drm_mode_analog_pal_576i(priv->drm);
135 	KUNIT_ASSERT_NOT_NULL(test, mode);
136 
137 	KUNIT_EXPECT_TRUE(test, drm_mode_equal(expected, mode));
138 }
139 
140 static struct kunit_case drm_modes_analog_tv_tests[] = {
141 	KUNIT_CASE(drm_test_modes_analog_tv_ntsc_480i),
142 	KUNIT_CASE(drm_test_modes_analog_tv_ntsc_480i_inlined),
143 	KUNIT_CASE(drm_test_modes_analog_tv_pal_576i),
144 	KUNIT_CASE(drm_test_modes_analog_tv_pal_576i_inlined),
145 	{ }
146 };
147 
148 static struct kunit_suite drm_modes_analog_tv_test_suite = {
149 	.name = "drm_modes_analog_tv",
150 	.init = drm_test_modes_init,
151 	.exit = drm_test_modes_exit,
152 	.test_cases = drm_modes_analog_tv_tests,
153 };
154 
155 kunit_test_suite(drm_modes_analog_tv_test_suite);
156 
157 MODULE_AUTHOR("Maxime Ripard <maxime@cerno.tech>");
158 MODULE_LICENSE("GPL");
159