xref: /linux/drivers/gpu/drm/tests/drm_connector_test.c (revision a23e1966932464e1c5226cb9ac4ce1d5fc10ba22)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Kunit test for drm_modes functions
4  */
5 
6 #include <linux/i2c.h>
7 
8 #include <drm/drm_atomic_state_helper.h>
9 #include <drm/drm_connector.h>
10 #include <drm/drm_drv.h>
11 #include <drm/drm_kunit_helpers.h>
12 
13 #include <kunit/test.h>
14 
15 struct drm_connector_init_priv {
16 	struct drm_device drm;
17 	struct drm_connector connector;
18 	struct i2c_adapter ddc;
19 };
20 
21 static const struct drm_connector_funcs dummy_funcs = {
22 	.atomic_destroy_state	= drm_atomic_helper_connector_destroy_state,
23 	.atomic_duplicate_state	= drm_atomic_helper_connector_duplicate_state,
24 	.reset			= drm_atomic_helper_connector_reset,
25 };
26 
27 static int dummy_ddc_xfer(struct i2c_adapter *adapter,
28 			  struct i2c_msg *msgs, int num)
29 {
30 	return num;
31 }
32 
33 static u32 dummy_ddc_func(struct i2c_adapter *adapter)
34 {
35 	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
36 }
37 
38 static const struct i2c_algorithm dummy_ddc_algorithm = {
39 	.master_xfer = dummy_ddc_xfer,
40 	.functionality = dummy_ddc_func,
41 };
42 
43 static void i2c_del_adapter_wrapper(void *ptr)
44 {
45 	struct i2c_adapter *adap = ptr;
46 
47 	i2c_del_adapter(adap);
48 }
49 
50 static int drm_test_connector_init(struct kunit *test)
51 {
52 	struct drm_connector_init_priv *priv;
53 	struct device *dev;
54 	int ret;
55 
56 	dev = drm_kunit_helper_alloc_device(test);
57 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dev);
58 
59 	priv = drm_kunit_helper_alloc_drm_device(test, dev,
60 						 struct drm_connector_init_priv, drm,
61 						 DRIVER_MODESET | DRIVER_ATOMIC);
62 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, priv);
63 
64 	strscpy(priv->ddc.name, "dummy-connector-ddc", sizeof(priv->ddc.name));
65 	priv->ddc.owner = THIS_MODULE;
66 	priv->ddc.algo = &dummy_ddc_algorithm;
67 	priv->ddc.dev.parent = dev;
68 
69 	ret = i2c_add_adapter(&priv->ddc);
70 	KUNIT_ASSERT_EQ(test, ret, 0);
71 
72 	ret = kunit_add_action_or_reset(test, i2c_del_adapter_wrapper, &priv->ddc);
73 	KUNIT_ASSERT_EQ(test, ret, 0);
74 
75 	test->priv = priv;
76 	return 0;
77 }
78 
79 /*
80  * Test that the registration of a bog standard connector works as
81  * expected and doesn't report any error.
82  */
83 static void drm_test_drmm_connector_init(struct kunit *test)
84 {
85 	struct drm_connector_init_priv *priv = test->priv;
86 	int ret;
87 
88 	ret = drmm_connector_init(&priv->drm, &priv->connector,
89 				  &dummy_funcs,
90 				  DRM_MODE_CONNECTOR_HDMIA,
91 				  &priv->ddc);
92 	KUNIT_EXPECT_EQ(test, ret, 0);
93 }
94 
95 /*
96  * Test that the registration of a connector without a DDC adapter
97  * doesn't report any error.
98  */
99 static void drm_test_drmm_connector_init_null_ddc(struct kunit *test)
100 {
101 	struct drm_connector_init_priv *priv = test->priv;
102 	int ret;
103 
104 	ret = drmm_connector_init(&priv->drm, &priv->connector,
105 				  &dummy_funcs,
106 				  DRM_MODE_CONNECTOR_HDMIA,
107 				  NULL);
108 	KUNIT_EXPECT_EQ(test, ret, 0);
109 }
110 
111 /*
112  * Test that the registration of a connector succeeds for all possible
113  * connector types.
114  */
115 static void drm_test_drmm_connector_init_type_valid(struct kunit *test)
116 {
117 	struct drm_connector_init_priv *priv = test->priv;
118 	unsigned int connector_type = *(unsigned int *)test->param_value;
119 	int ret;
120 
121 	ret = drmm_connector_init(&priv->drm, &priv->connector,
122 				  &dummy_funcs,
123 				  connector_type,
124 				  &priv->ddc);
125 	KUNIT_EXPECT_EQ(test, ret, 0);
126 }
127 
128 static const unsigned int drm_connector_init_type_valid_tests[] = {
129 	DRM_MODE_CONNECTOR_Unknown,
130 	DRM_MODE_CONNECTOR_VGA,
131 	DRM_MODE_CONNECTOR_DVII,
132 	DRM_MODE_CONNECTOR_DVID,
133 	DRM_MODE_CONNECTOR_DVIA,
134 	DRM_MODE_CONNECTOR_Composite,
135 	DRM_MODE_CONNECTOR_SVIDEO,
136 	DRM_MODE_CONNECTOR_LVDS,
137 	DRM_MODE_CONNECTOR_Component,
138 	DRM_MODE_CONNECTOR_9PinDIN,
139 	DRM_MODE_CONNECTOR_DisplayPort,
140 	DRM_MODE_CONNECTOR_HDMIA,
141 	DRM_MODE_CONNECTOR_HDMIB,
142 	DRM_MODE_CONNECTOR_TV,
143 	DRM_MODE_CONNECTOR_eDP,
144 	DRM_MODE_CONNECTOR_VIRTUAL,
145 	DRM_MODE_CONNECTOR_DSI,
146 	DRM_MODE_CONNECTOR_DPI,
147 	DRM_MODE_CONNECTOR_WRITEBACK,
148 	DRM_MODE_CONNECTOR_SPI,
149 	DRM_MODE_CONNECTOR_USB,
150 };
151 
152 static void drm_connector_init_type_desc(const unsigned int *type, char *desc)
153 {
154 	sprintf(desc, "%s", drm_get_connector_type_name(*type));
155 }
156 
157 KUNIT_ARRAY_PARAM(drm_connector_init_type_valid,
158 		  drm_connector_init_type_valid_tests,
159 		  drm_connector_init_type_desc);
160 
161 static struct kunit_case drmm_connector_init_tests[] = {
162 	KUNIT_CASE(drm_test_drmm_connector_init),
163 	KUNIT_CASE(drm_test_drmm_connector_init_null_ddc),
164 	KUNIT_CASE_PARAM(drm_test_drmm_connector_init_type_valid,
165 			 drm_connector_init_type_valid_gen_params),
166 	{ }
167 };
168 
169 static struct kunit_suite drmm_connector_init_test_suite = {
170 	.name = "drmm_connector_init",
171 	.init = drm_test_connector_init,
172 	.test_cases = drmm_connector_init_tests,
173 };
174 
175 struct drm_get_tv_mode_from_name_test {
176 	const char *name;
177 	enum drm_connector_tv_mode expected_mode;
178 };
179 
180 #define TV_MODE_NAME(_name, _mode)		\
181 	{					\
182 		.name = _name,			\
183 		.expected_mode = _mode,		\
184 	}
185 
186 static void drm_test_get_tv_mode_from_name_valid(struct kunit *test)
187 {
188 	const struct drm_get_tv_mode_from_name_test *params = test->param_value;
189 
190 	KUNIT_EXPECT_EQ(test,
191 			drm_get_tv_mode_from_name(params->name, strlen(params->name)),
192 			params->expected_mode);
193 }
194 
195 static const
196 struct drm_get_tv_mode_from_name_test drm_get_tv_mode_from_name_valid_tests[] = {
197 	TV_MODE_NAME("NTSC", DRM_MODE_TV_MODE_NTSC),
198 	TV_MODE_NAME("NTSC-443", DRM_MODE_TV_MODE_NTSC_443),
199 	TV_MODE_NAME("NTSC-J", DRM_MODE_TV_MODE_NTSC_J),
200 	TV_MODE_NAME("PAL", DRM_MODE_TV_MODE_PAL),
201 	TV_MODE_NAME("PAL-M", DRM_MODE_TV_MODE_PAL_M),
202 	TV_MODE_NAME("PAL-N", DRM_MODE_TV_MODE_PAL_N),
203 	TV_MODE_NAME("SECAM", DRM_MODE_TV_MODE_SECAM),
204 };
205 
206 static void
207 drm_get_tv_mode_from_name_valid_desc(const struct drm_get_tv_mode_from_name_test *t,
208 				     char *desc)
209 {
210 	sprintf(desc, "%s", t->name);
211 }
212 
213 KUNIT_ARRAY_PARAM(drm_get_tv_mode_from_name_valid,
214 		  drm_get_tv_mode_from_name_valid_tests,
215 		  drm_get_tv_mode_from_name_valid_desc);
216 
217 static void drm_test_get_tv_mode_from_name_truncated(struct kunit *test)
218 {
219 	const char *name = "NTS";
220 	int ret;
221 
222 	ret = drm_get_tv_mode_from_name(name, strlen(name));
223 	KUNIT_EXPECT_LT(test, ret, 0);
224 };
225 
226 static struct kunit_case drm_get_tv_mode_from_name_tests[] = {
227 	KUNIT_CASE_PARAM(drm_test_get_tv_mode_from_name_valid,
228 			 drm_get_tv_mode_from_name_valid_gen_params),
229 	KUNIT_CASE(drm_test_get_tv_mode_from_name_truncated),
230 	{ }
231 };
232 
233 static struct kunit_suite drm_get_tv_mode_from_name_test_suite = {
234 	.name = "drm_get_tv_mode_from_name",
235 	.test_cases = drm_get_tv_mode_from_name_tests,
236 };
237 
238 kunit_test_suites(
239 	&drmm_connector_init_test_suite,
240 	&drm_get_tv_mode_from_name_test_suite
241 );
242 
243 MODULE_AUTHOR("Maxime Ripard <maxime@cerno.tech>");
244 MODULE_LICENSE("GPL");
245