summaryrefslogtreecommitdiff
path: root/drivers/gpu/drm/tests/drm_connector_test.c
blob: 15e36a8db6858a48a612c13682e24c56a24d3bc1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
// SPDX-License-Identifier: GPL-2.0
/*
 * Kunit test for drm_modes functions
 */

#include <linux/i2c.h>

#include <drm/drm_atomic_state_helper.h>
#include <drm/drm_connector.h>
#include <drm/drm_drv.h>
#include <drm/drm_edid.h>
#include <drm/drm_kunit_helpers.h>
#include <drm/drm_modes.h>

#include <drm/display/drm_hdmi_helper.h>

#include <kunit/test.h>

#include "../drm_crtc_internal.h"

struct drm_connector_init_priv {
	struct drm_device drm;
	struct drm_connector connector;
	struct i2c_adapter ddc;
};

static const struct drm_connector_hdmi_funcs dummy_hdmi_funcs = {
};

static const struct drm_connector_funcs dummy_funcs = {
	.atomic_destroy_state	= drm_atomic_helper_connector_destroy_state,
	.atomic_duplicate_state	= drm_atomic_helper_connector_duplicate_state,
	.reset			= drm_atomic_helper_connector_reset,
};

static int dummy_ddc_xfer(struct i2c_adapter *adapter,
			  struct i2c_msg *msgs, int num)
{
	return num;
}

static u32 dummy_ddc_func(struct i2c_adapter *adapter)
{
	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
}

static const struct i2c_algorithm dummy_ddc_algorithm = {
	.master_xfer = dummy_ddc_xfer,
	.functionality = dummy_ddc_func,
};

static void i2c_del_adapter_wrapper(void *ptr)
{
	struct i2c_adapter *adap = ptr;

	i2c_del_adapter(adap);
}

static int drm_test_connector_init(struct kunit *test)
{
	struct drm_connector_init_priv *priv;
	struct device *dev;
	int ret;

	dev = drm_kunit_helper_alloc_device(test);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dev);

	priv = drm_kunit_helper_alloc_drm_device(test, dev,
						 struct drm_connector_init_priv, drm,
						 DRIVER_MODESET | DRIVER_ATOMIC);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, priv);

	strscpy(priv->ddc.name, "dummy-connector-ddc", sizeof(priv->ddc.name));
	priv->ddc.owner = THIS_MODULE;
	priv->ddc.algo = &dummy_ddc_algorithm;
	priv->ddc.dev.parent = dev;

	ret = i2c_add_adapter(&priv->ddc);
	KUNIT_ASSERT_EQ(test, ret, 0);

	ret = kunit_add_action_or_reset(test, i2c_del_adapter_wrapper, &priv->ddc);
	KUNIT_ASSERT_EQ(test, ret, 0);

	test->priv = priv;
	return 0;
}

/*
 * Test that the registration of a bog standard connector works as
 * expected and doesn't report any error.
 */
static void drm_test_drmm_connector_init(struct kunit *test)
{
	struct drm_connector_init_priv *priv = test->priv;
	int ret;

	ret = drmm_connector_init(&priv->drm, &priv->connector,
				  &dummy_funcs,
				  DRM_MODE_CONNECTOR_HDMIA,
				  &priv->ddc);
	KUNIT_EXPECT_EQ(test, ret, 0);
}

/*
 * Test that the registration of a connector without a DDC adapter
 * doesn't report any error.
 */
static void drm_test_drmm_connector_init_null_ddc(struct kunit *test)
{
	struct drm_connector_init_priv *priv = test->priv;
	int ret;

	ret = drmm_connector_init(&priv->drm, &priv->connector,
				  &dummy_funcs,
				  DRM_MODE_CONNECTOR_HDMIA,
				  NULL);
	KUNIT_EXPECT_EQ(test, ret, 0);
}

/*
 * Test that the registration of a connector succeeds for all possible
 * connector types.
 */
static void drm_test_drmm_connector_init_type_valid(struct kunit *test)
{
	struct drm_connector_init_priv *priv = test->priv;
	unsigned int connector_type = *(unsigned int *)test->param_value;
	int ret;

	ret = drmm_connector_init(&priv->drm, &priv->connector,
				  &dummy_funcs,
				  connector_type,
				  &priv->ddc);
	KUNIT_EXPECT_EQ(test, ret, 0);
}

static const unsigned int drm_connector_init_type_valid_tests[] = {
	DRM_MODE_CONNECTOR_Unknown,
	DRM_MODE_CONNECTOR_VGA,
	DRM_MODE_CONNECTOR_DVII,
	DRM_MODE_CONNECTOR_DVID,
	DRM_MODE_CONNECTOR_DVIA,
	DRM_MODE_CONNECTOR_Composite,
	DRM_MODE_CONNECTOR_SVIDEO,
	DRM_MODE_CONNECTOR_LVDS,
	DRM_MODE_CONNECTOR_Component,
	DRM_MODE_CONNECTOR_9PinDIN,
	DRM_MODE_CONNECTOR_DisplayPort,
	DRM_MODE_CONNECTOR_HDMIA,
	DRM_MODE_CONNECTOR_HDMIB,
	DRM_MODE_CONNECTOR_TV,
	DRM_MODE_CONNECTOR_eDP,
	DRM_MODE_CONNECTOR_VIRTUAL,
	DRM_MODE_CONNECTOR_DSI,
	DRM_MODE_CONNECTOR_DPI,
	DRM_MODE_CONNECTOR_WRITEBACK,
	DRM_MODE_CONNECTOR_SPI,
	DRM_MODE_CONNECTOR_USB,
};

static void drm_connector_init_type_desc(const unsigned int *type, char *desc)
{
	sprintf(desc, "%s", drm_get_connector_type_name(*type));
}

KUNIT_ARRAY_PARAM(drm_connector_init_type_valid,
		  drm_connector_init_type_valid_tests,
		  drm_connector_init_type_desc);

static struct kunit_case drmm_connector_init_tests[] = {
	KUNIT_CASE(drm_test_drmm_connector_init),
	KUNIT_CASE(drm_test_drmm_connector_init_null_ddc),
	KUNIT_CASE_PARAM(drm_test_drmm_connector_init_type_valid,
			 drm_connector_init_type_valid_gen_params),
	{ }
};

static struct kunit_suite drmm_connector_init_test_suite = {
	.name = "drmm_connector_init",
	.init = drm_test_connector_init,
	.test_cases = drmm_connector_init_tests,
};

/*
 * Test that the registration of a bog standard connector works as
 * expected and doesn't report any error.
 */
static void drm_test_connector_hdmi_init_valid(struct kunit *test)
{
	struct drm_connector_init_priv *priv = test->priv;
	int ret;

	ret = drmm_connector_hdmi_init(&priv->drm, &priv->connector,
				       "Vendor", "Product",
				       &dummy_funcs,
				       &dummy_hdmi_funcs,
				       DRM_MODE_CONNECTOR_HDMIA,
				       &priv->ddc,
				       BIT(HDMI_COLORSPACE_RGB),
				       8);
	KUNIT_EXPECT_EQ(test, ret, 0);
}

/*
 * Test that the registration of a connector without a DDC adapter
 * doesn't report any error.
 */
static void drm_test_connector_hdmi_init_null_ddc(struct kunit *test)
{
	struct drm_connector_init_priv *priv = test->priv;
	int ret;

	ret = drmm_connector_hdmi_init(&priv->drm, &priv->connector,
				       "Vendor", "Product",
				       &dummy_funcs,
				       &dummy_hdmi_funcs,
				       DRM_MODE_CONNECTOR_HDMIA,
				       NULL,
				       BIT(HDMI_COLORSPACE_RGB),
				       8);
	KUNIT_EXPECT_EQ(test, ret, 0);
}

/*
 * Test that the registration of an HDMI connector with a NULL vendor
 * fails.
 */
static void drm_test_connector_hdmi_init_null_vendor(struct kunit *test)
{
	struct drm_connector_init_priv *priv = test->priv;
	int ret;

	ret = drmm_connector_hdmi_init(&priv->drm, &priv->connector,
				       NULL, "Product",
				       &dummy_funcs,
				       &dummy_hdmi_funcs,
				       DRM_MODE_CONNECTOR_HDMIA,
				       &priv->ddc,
				       BIT(HDMI_COLORSPACE_RGB),
				       8);
	KUNIT_EXPECT_LT(test, ret, 0);
}

/*
 * Test that the registration of an HDMI connector with a NULL product
 * fails.
 */
static void drm_test_connector_hdmi_init_null_product(struct kunit *test)
{
	struct drm_connector_init_priv *priv = test->priv;
	int ret;

	ret = drmm_connector_hdmi_init(&priv->drm, &priv->connector,
				       "Vendor", NULL,
				       &dummy_funcs,
				       &dummy_hdmi_funcs,
				       DRM_MODE_CONNECTOR_HDMIA,
				       &priv->ddc,
				       BIT(HDMI_COLORSPACE_RGB),
				       8);
	KUNIT_EXPECT_LT(test, ret, 0);
}

/*
 * Test that the registration of a connector with a valid, shorter than
 * the max length, product name succeeds, and is stored padded with 0.
 */
static void drm_test_connector_hdmi_init_product_valid(struct kunit *test)
{
	struct drm_connector_init_priv *priv = test->priv;
	const unsigned char expected_product[DRM_CONNECTOR_HDMI_PRODUCT_LEN] = {
		'P', 'r', 'o', 'd',
	};
	const char *product_name = "Prod";
	int ret;

	KUNIT_ASSERT_LT(test, strlen(product_name), DRM_CONNECTOR_HDMI_PRODUCT_LEN);

	ret = drmm_connector_hdmi_init(&priv->drm, &priv->connector,
				       "Vendor", product_name,
				       &dummy_funcs,
				       &dummy_hdmi_funcs,
				       DRM_MODE_CONNECTOR_HDMIA,
				       &priv->ddc,
				       BIT(HDMI_COLORSPACE_RGB),
				       8);
	KUNIT_EXPECT_EQ(test, ret, 0);
	KUNIT_EXPECT_MEMEQ(test,
			   priv->connector.hdmi.product,
			   expected_product,
			   sizeof(priv->connector.hdmi.product));
}

/*
 * Test that the registration of a connector with a valid, at max
 * length, product name succeeds, and is stored padded without any
 * trailing \0.
 */
static void drm_test_connector_hdmi_init_product_length_exact(struct kunit *test)
{
	struct drm_connector_init_priv *priv = test->priv;
	const unsigned char expected_product[DRM_CONNECTOR_HDMI_PRODUCT_LEN] = {
		'P', 'r', 'o', 'd', 'u', 'c', 't',
		'P', 'r', 'o', 'd', 'u', 'c', 't',
		'P', 'r',
	};
	const char *product_name = "ProductProductPr";
	int ret;

	KUNIT_ASSERT_EQ(test, strlen(product_name), DRM_CONNECTOR_HDMI_PRODUCT_LEN);

	ret = drmm_connector_hdmi_init(&priv->drm, &priv->connector,
				       "Vendor", product_name,
				       &dummy_funcs,
				       &dummy_hdmi_funcs,
				       DRM_MODE_CONNECTOR_HDMIA,
				       &priv->ddc,
				       BIT(HDMI_COLORSPACE_RGB),
				       8);
	KUNIT_EXPECT_EQ(test, ret, 0);
	KUNIT_EXPECT_MEMEQ(test,
			   priv->connector.hdmi.product,
			   expected_product,
			   sizeof(priv->connector.hdmi.product));
}

/*
 * Test that the registration of a connector with a product name larger
 * than the maximum length fails.
 */
static void drm_test_connector_hdmi_init_product_length_too_long(struct kunit *test)
{
	struct drm_connector_init_priv *priv = test->priv;
	const char *product_name = "ProductProductProduct";
	int ret;

	KUNIT_ASSERT_GT(test, strlen(product_name), DRM_CONNECTOR_HDMI_PRODUCT_LEN);

	ret = drmm_connector_hdmi_init(&priv->drm, &priv->connector,
				       "Vendor", product_name,
				       &dummy_funcs,
				       &dummy_hdmi_funcs,
				       DRM_MODE_CONNECTOR_HDMIA,
				       &priv->ddc,
				       BIT(HDMI_COLORSPACE_RGB),
				       8);
	KUNIT_EXPECT_LT(test, ret, 0);
}

/*
 * Test that the registration of a connector with a vendor name smaller
 * than the maximum length succeeds, and is stored padded with zeros.
 */
static void drm_test_connector_hdmi_init_vendor_valid(struct kunit *test)
{
	struct drm_connector_init_priv *priv = test->priv;
	const char expected_vendor[DRM_CONNECTOR_HDMI_VENDOR_LEN] = {
		'V', 'e', 'n', 'd',
	};
	const char *vendor_name = "Vend";
	int ret;

	KUNIT_ASSERT_LT(test, strlen(vendor_name), DRM_CONNECTOR_HDMI_VENDOR_LEN);

	ret = drmm_connector_hdmi_init(&priv->drm, &priv->connector,
				       vendor_name, "Product",
				       &dummy_funcs,
				       &dummy_hdmi_funcs,
				       DRM_MODE_CONNECTOR_HDMIA,
				       &priv->ddc,
				       BIT(HDMI_COLORSPACE_RGB),
				       8);
	KUNIT_EXPECT_EQ(test, ret, 0);
	KUNIT_EXPECT_MEMEQ(test,
			   priv->connector.hdmi.vendor,
			   expected_vendor,
			   sizeof(priv->connector.hdmi.vendor));
}

/*
 * Test that the registration of a connector with a vendor name at the
 * maximum length succeeds, and is stored padded without the trailing
 * zero.
 */
static void drm_test_connector_hdmi_init_vendor_length_exact(struct kunit *test)
{
	struct drm_connector_init_priv *priv = test->priv;
	const char expected_vendor[DRM_CONNECTOR_HDMI_VENDOR_LEN] = {
		'V', 'e', 'n', 'd', 'o', 'r',
		'V', 'e',
	};
	const char *vendor_name = "VendorVe";
	int ret;

	KUNIT_ASSERT_EQ(test, strlen(vendor_name), DRM_CONNECTOR_HDMI_VENDOR_LEN);

	ret = drmm_connector_hdmi_init(&priv->drm, &priv->connector,
				       vendor_name, "Product",
				       &dummy_funcs,
				       &dummy_hdmi_funcs,
				       DRM_MODE_CONNECTOR_HDMIA,
				       &priv->ddc,
				       BIT(HDMI_COLORSPACE_RGB),
				       8);
	KUNIT_EXPECT_EQ(test, ret, 0);
	KUNIT_EXPECT_MEMEQ(test,
			   priv->connector.hdmi.vendor,
			   expected_vendor,
			   sizeof(priv->connector.hdmi.vendor));
}

/*
 * Test that the registration of a connector with a vendor name larger
 * than the maximum length fails.
 */
static void drm_test_connector_hdmi_init_vendor_length_too_long(struct kunit *test)
{
	struct drm_connector_init_priv *priv = test->priv;
	const char *vendor_name = "VendorVendor";
	int ret;

	KUNIT_ASSERT_GT(test, strlen(vendor_name), DRM_CONNECTOR_HDMI_VENDOR_LEN);

	ret = drmm_connector_hdmi_init(&priv->drm, &priv->connector,
				       vendor_name, "Product",
				       &dummy_funcs,
				       &dummy_hdmi_funcs,
				       DRM_MODE_CONNECTOR_HDMIA,
				       &priv->ddc,
				       BIT(HDMI_COLORSPACE_RGB),
				       8);
	KUNIT_EXPECT_LT(test, ret, 0);
}

/*
 * Test that the registration of a connector with an invalid maximum bpc
 * count fails.
 */
static void drm_test_connector_hdmi_init_bpc_invalid(struct kunit *test)
{
	struct drm_connector_init_priv *priv = test->priv;
	int ret;

	ret = drmm_connector_hdmi_init(&priv->drm, &priv->connector,
				       "Vendor", "Product",
				       &dummy_funcs,
				       &dummy_hdmi_funcs,
				       DRM_MODE_CONNECTOR_HDMIA,
				       &priv->ddc,
				       BIT(HDMI_COLORSPACE_RGB),
				       9);
	KUNIT_EXPECT_LT(test, ret, 0);
}

/*
 * Test that the registration of a connector with a null maximum bpc
 * count fails.
 */
static void drm_test_connector_hdmi_init_bpc_null(struct kunit *test)
{
	struct drm_connector_init_priv *priv = test->priv;
	int ret;

	ret = drmm_connector_hdmi_init(&priv->drm, &priv->connector,
				       "Vendor", "Product",
				       &dummy_funcs,
				       &dummy_hdmi_funcs,
				       DRM_MODE_CONNECTOR_HDMIA,
				       &priv->ddc,
				       BIT(HDMI_COLORSPACE_RGB),
				       0);
	KUNIT_EXPECT_LT(test, ret, 0);
}

/*
 * Test that the registration of a connector with a maximum bpc count of
 * 8 succeeds, registers the max bpc property, but doesn't register the
 * HDR output metadata one.
 */
static void drm_test_connector_hdmi_init_bpc_8(struct kunit *test)
{
	struct drm_connector_init_priv *priv = test->priv;
	struct drm_connector_state *state;
	struct drm_connector *connector = &priv->connector;
	struct drm_property *prop;
	uint64_t val;
	int ret;

	ret = drmm_connector_hdmi_init(&priv->drm, connector,
				       "Vendor", "Product",
				       &dummy_funcs,
				       &dummy_hdmi_funcs,
				       DRM_MODE_CONNECTOR_HDMIA,
				       &priv->ddc,
				       BIT(HDMI_COLORSPACE_RGB),
				       8);
	KUNIT_EXPECT_EQ(test, ret, 0);

	prop = connector->max_bpc_property;
	KUNIT_ASSERT_NOT_NULL(test, prop);
	KUNIT_EXPECT_NOT_NULL(test, drm_mode_obj_find_prop_id(&connector->base, prop->base.id));

	ret = drm_object_property_get_default_value(&connector->base, prop, &val);
	KUNIT_EXPECT_EQ(test, ret, 0);
	KUNIT_EXPECT_EQ(test, val, 8);

	state = connector->state;
	KUNIT_EXPECT_EQ(test, state->max_bpc, 8);
	KUNIT_EXPECT_EQ(test, state->max_requested_bpc, 8);

	prop = priv->drm.mode_config.hdr_output_metadata_property;
	KUNIT_ASSERT_NOT_NULL(test, prop);
	KUNIT_EXPECT_NULL(test, drm_mode_obj_find_prop_id(&connector->base, prop->base.id));
}

/*
 * Test that the registration of a connector with a maximum bpc count of
 * 10 succeeds and registers the max bpc and HDR output metadata
 * properties.
 */
static void drm_test_connector_hdmi_init_bpc_10(struct kunit *test)
{
	struct drm_connector_init_priv *priv = test->priv;
	struct drm_connector_state *state;
	struct drm_connector *connector = &priv->connector;
	struct drm_property *prop;
	uint64_t val;
	int ret;

	ret = drmm_connector_hdmi_init(&priv->drm, connector,
				       "Vendor", "Product",
				       &dummy_funcs,
				       &dummy_hdmi_funcs,
				       DRM_MODE_CONNECTOR_HDMIA,
				       &priv->ddc,
				       BIT(HDMI_COLORSPACE_RGB),
				       10);
	KUNIT_EXPECT_EQ(test, ret, 0);

	prop = connector->max_bpc_property;
	KUNIT_ASSERT_NOT_NULL(test, prop);
	KUNIT_EXPECT_NOT_NULL(test, drm_mode_obj_find_prop_id(&connector->base, prop->base.id));

	ret = drm_object_property_get_default_value(&connector->base, prop, &val);
	KUNIT_EXPECT_EQ(test, ret, 0);
	KUNIT_EXPECT_EQ(test, val, 10);

	state = connector->state;
	KUNIT_EXPECT_EQ(test, state->max_bpc, 10);
	KUNIT_EXPECT_EQ(test, state->max_requested_bpc, 10);

	prop = priv->drm.mode_config.hdr_output_metadata_property;
	KUNIT_ASSERT_NOT_NULL(test, prop);
	KUNIT_EXPECT_NOT_NULL(test, drm_mode_obj_find_prop_id(&connector->base, prop->base.id));
}

/*
 * Test that the registration of a connector with a maximum bpc count of
 * 12 succeeds and registers the max bpc and HDR output metadata
 * properties.
 */
static void drm_test_connector_hdmi_init_bpc_12(struct kunit *test)
{
	struct drm_connector_init_priv *priv = test->priv;
	struct drm_connector_state *state;
	struct drm_connector *connector = &priv->connector;
	struct drm_property *prop;
	uint64_t val;
	int ret;

	ret = drmm_connector_hdmi_init(&priv->drm, connector,
				       "Vendor", "Product",
				       &dummy_funcs,
				       &dummy_hdmi_funcs,
				       DRM_MODE_CONNECTOR_HDMIA,
				       &priv->ddc,
				       BIT(HDMI_COLORSPACE_RGB),
				       12);
	KUNIT_EXPECT_EQ(test, ret, 0);

	prop = connector->max_bpc_property;
	KUNIT_ASSERT_NOT_NULL(test, prop);
	KUNIT_EXPECT_NOT_NULL(test, drm_mode_obj_find_prop_id(&connector->base, prop->base.id));

	ret = drm_object_property_get_default_value(&connector->base, prop, &val);
	KUNIT_EXPECT_EQ(test, ret, 0);
	KUNIT_EXPECT_EQ(test, val, 12);

	state = connector->state;
	KUNIT_EXPECT_EQ(test, state->max_bpc, 12);
	KUNIT_EXPECT_EQ(test, state->max_requested_bpc, 12);

	prop = priv->drm.mode_config.hdr_output_metadata_property;
	KUNIT_ASSERT_NOT_NULL(test, prop);
	KUNIT_EXPECT_NOT_NULL(test, drm_mode_obj_find_prop_id(&connector->base, prop->base.id));
}

/*
 * Test that the registration of an HDMI connector with no supported
 * format fails.
 */
static void drm_test_connector_hdmi_init_formats_empty(struct kunit *test)
{
	struct drm_connector_init_priv *priv = test->priv;
	int ret;

	ret = drmm_connector_hdmi_init(&priv->drm, &priv->connector,
				       "Vendor", "Product",
				       &dummy_funcs,
				       &dummy_hdmi_funcs,
				       DRM_MODE_CONNECTOR_HDMIA,
				       &priv->ddc,
				       0,
				       8);
	KUNIT_EXPECT_LT(test, ret, 0);
}

/*
 * Test that the registration of an HDMI connector not listing RGB as a
 * supported format fails.
 */
static void drm_test_connector_hdmi_init_formats_no_rgb(struct kunit *test)
{
	struct drm_connector_init_priv *priv = test->priv;
	int ret;

	ret = drmm_connector_hdmi_init(&priv->drm, &priv->connector,
				       "Vendor", "Product",
				       &dummy_funcs,
				       &dummy_hdmi_funcs,
				       DRM_MODE_CONNECTOR_HDMIA,
				       &priv->ddc,
				       BIT(HDMI_COLORSPACE_YUV422),
				       8);
	KUNIT_EXPECT_LT(test, ret, 0);
}

/*
 * Test that the registration of an HDMI connector with an HDMI
 * connector type succeeds.
 */
static void drm_test_connector_hdmi_init_type_valid(struct kunit *test)
{
	struct drm_connector_init_priv *priv = test->priv;
	unsigned int connector_type = *(unsigned int *)test->param_value;
	int ret;

	ret = drmm_connector_hdmi_init(&priv->drm, &priv->connector,
				       "Vendor", "Product",
				       &dummy_funcs,
				       &dummy_hdmi_funcs,
				       connector_type,
				       &priv->ddc,
				       BIT(HDMI_COLORSPACE_RGB),
				       8);
	KUNIT_EXPECT_EQ(test, ret, 0);
}

static const unsigned int drm_connector_hdmi_init_type_valid_tests[] = {
	DRM_MODE_CONNECTOR_HDMIA,
	DRM_MODE_CONNECTOR_HDMIB,
};

static void drm_connector_hdmi_init_type_desc(const unsigned int *type, char *desc)
{
	sprintf(desc, "%s", drm_get_connector_type_name(*type));
}

KUNIT_ARRAY_PARAM(drm_connector_hdmi_init_type_valid,
		  drm_connector_hdmi_init_type_valid_tests,
		  drm_connector_hdmi_init_type_desc);

/*
 * Test that the registration of an HDMI connector with an !HDMI
 * connector type fails.
 */
static void drm_test_connector_hdmi_init_type_invalid(struct kunit *test)
{
	struct drm_connector_init_priv *priv = test->priv;
	unsigned int connector_type = *(unsigned int *)test->param_value;
	int ret;

	ret = drmm_connector_hdmi_init(&priv->drm, &priv->connector,
				       "Vendor", "Product",
				       &dummy_funcs,
				       &dummy_hdmi_funcs,
				       connector_type,
				       &priv->ddc,
				       BIT(HDMI_COLORSPACE_RGB),
				       8);
	KUNIT_EXPECT_LT(test, ret, 0);
}

static const unsigned int drm_connector_hdmi_init_type_invalid_tests[] = {
	DRM_MODE_CONNECTOR_Unknown,
	DRM_MODE_CONNECTOR_VGA,
	DRM_MODE_CONNECTOR_DVII,
	DRM_MODE_CONNECTOR_DVID,
	DRM_MODE_CONNECTOR_DVIA,
	DRM_MODE_CONNECTOR_Composite,
	DRM_MODE_CONNECTOR_SVIDEO,
	DRM_MODE_CONNECTOR_LVDS,
	DRM_MODE_CONNECTOR_Component,
	DRM_MODE_CONNECTOR_9PinDIN,
	DRM_MODE_CONNECTOR_DisplayPort,
	DRM_MODE_CONNECTOR_TV,
	DRM_MODE_CONNECTOR_eDP,
	DRM_MODE_CONNECTOR_VIRTUAL,
	DRM_MODE_CONNECTOR_DSI,
	DRM_MODE_CONNECTOR_DPI,
	DRM_MODE_CONNECTOR_WRITEBACK,
	DRM_MODE_CONNECTOR_SPI,
	DRM_MODE_CONNECTOR_USB,
};

KUNIT_ARRAY_PARAM(drm_connector_hdmi_init_type_invalid,
		  drm_connector_hdmi_init_type_invalid_tests,
		  drm_connector_hdmi_init_type_desc);

static struct kunit_case drmm_connector_hdmi_init_tests[] = {
	KUNIT_CASE(drm_test_connector_hdmi_init_valid),
	KUNIT_CASE(drm_test_connector_hdmi_init_bpc_8),
	KUNIT_CASE(drm_test_connector_hdmi_init_bpc_10),
	KUNIT_CASE(drm_test_connector_hdmi_init_bpc_12),
	KUNIT_CASE(drm_test_connector_hdmi_init_bpc_invalid),
	KUNIT_CASE(drm_test_connector_hdmi_init_bpc_null),
	KUNIT_CASE(drm_test_connector_hdmi_init_formats_empty),
	KUNIT_CASE(drm_test_connector_hdmi_init_formats_no_rgb),
	KUNIT_CASE(drm_test_connector_hdmi_init_null_ddc),
	KUNIT_CASE(drm_test_connector_hdmi_init_null_product),
	KUNIT_CASE(drm_test_connector_hdmi_init_null_vendor),
	KUNIT_CASE(drm_test_connector_hdmi_init_product_length_exact),
	KUNIT_CASE(drm_test_connector_hdmi_init_product_length_too_long),
	KUNIT_CASE(drm_test_connector_hdmi_init_product_valid),
	KUNIT_CASE(drm_test_connector_hdmi_init_vendor_length_exact),
	KUNIT_CASE(drm_test_connector_hdmi_init_vendor_length_too_long),
	KUNIT_CASE(drm_test_connector_hdmi_init_vendor_valid),
	KUNIT_CASE_PARAM(drm_test_connector_hdmi_init_type_valid,
			 drm_connector_hdmi_init_type_valid_gen_params),
	KUNIT_CASE_PARAM(drm_test_connector_hdmi_init_type_invalid,
			 drm_connector_hdmi_init_type_invalid_gen_params),
	{ }
};

static struct kunit_suite drmm_connector_hdmi_init_test_suite = {
	.name = "drmm_connector_hdmi_init",
	.init = drm_test_connector_init,
	.test_cases = drmm_connector_hdmi_init_tests,
};

struct drm_get_tv_mode_from_name_test {
	const char *name;
	enum drm_connector_tv_mode expected_mode;
};

#define TV_MODE_NAME(_name, _mode)		\
	{					\
		.name = _name,			\
		.expected_mode = _mode,		\
	}

static void drm_test_get_tv_mode_from_name_valid(struct kunit *test)
{
	const struct drm_get_tv_mode_from_name_test *params = test->param_value;

	KUNIT_EXPECT_EQ(test,
			drm_get_tv_mode_from_name(params->name, strlen(params->name)),
			params->expected_mode);
}

static const
struct drm_get_tv_mode_from_name_test drm_get_tv_mode_from_name_valid_tests[] = {
	TV_MODE_NAME("NTSC", DRM_MODE_TV_MODE_NTSC),
	TV_MODE_NAME("NTSC-443", DRM_MODE_TV_MODE_NTSC_443),
	TV_MODE_NAME("NTSC-J", DRM_MODE_TV_MODE_NTSC_J),
	TV_MODE_NAME("PAL", DRM_MODE_TV_MODE_PAL),
	TV_MODE_NAME("PAL-M", DRM_MODE_TV_MODE_PAL_M),
	TV_MODE_NAME("PAL-N", DRM_MODE_TV_MODE_PAL_N),
	TV_MODE_NAME("SECAM", DRM_MODE_TV_MODE_SECAM),
	TV_MODE_NAME("Mono", DRM_MODE_TV_MODE_MONOCHROME),
};

static void
drm_get_tv_mode_from_name_valid_desc(const struct drm_get_tv_mode_from_name_test *t,
				     char *desc)
{
	sprintf(desc, "%s", t->name);
}

KUNIT_ARRAY_PARAM(drm_get_tv_mode_from_name_valid,
		  drm_get_tv_mode_from_name_valid_tests,
		  drm_get_tv_mode_from_name_valid_desc);

static void drm_test_get_tv_mode_from_name_truncated(struct kunit *test)
{
	const char *name = "NTS";
	int ret;

	ret = drm_get_tv_mode_from_name(name, strlen(name));
	KUNIT_EXPECT_LT(test, ret, 0);
};

static struct kunit_case drm_get_tv_mode_from_name_tests[] = {
	KUNIT_CASE_PARAM(drm_test_get_tv_mode_from_name_valid,
			 drm_get_tv_mode_from_name_valid_gen_params),
	KUNIT_CASE(drm_test_get_tv_mode_from_name_truncated),
	{ }
};

static struct kunit_suite drm_get_tv_mode_from_name_test_suite = {
	.name = "drm_get_tv_mode_from_name",
	.test_cases = drm_get_tv_mode_from_name_tests,
};

struct drm_hdmi_connector_get_broadcast_rgb_name_test {
	unsigned int kind;
	const char *expected_name;
};

#define BROADCAST_RGB_TEST(_kind, _name)	\
	{					\
		.kind = _kind,			\
		.expected_name = _name,		\
	}

static void drm_test_drm_hdmi_connector_get_broadcast_rgb_name(struct kunit *test)
{
	const struct drm_hdmi_connector_get_broadcast_rgb_name_test *params =
		test->param_value;

	KUNIT_EXPECT_STREQ(test,
			   drm_hdmi_connector_get_broadcast_rgb_name(params->kind),
			   params->expected_name);
}

static const
struct drm_hdmi_connector_get_broadcast_rgb_name_test
drm_hdmi_connector_get_broadcast_rgb_name_valid_tests[] = {
	BROADCAST_RGB_TEST(DRM_HDMI_BROADCAST_RGB_AUTO, "Automatic"),
	BROADCAST_RGB_TEST(DRM_HDMI_BROADCAST_RGB_FULL, "Full"),
	BROADCAST_RGB_TEST(DRM_HDMI_BROADCAST_RGB_LIMITED, "Limited 16:235"),
};

static void
drm_hdmi_connector_get_broadcast_rgb_name_valid_desc(const struct drm_hdmi_connector_get_broadcast_rgb_name_test *t,
						     char *desc)
{
	sprintf(desc, "%s", t->expected_name);
}

KUNIT_ARRAY_PARAM(drm_hdmi_connector_get_broadcast_rgb_name_valid,
		  drm_hdmi_connector_get_broadcast_rgb_name_valid_tests,
		  drm_hdmi_connector_get_broadcast_rgb_name_valid_desc);

static void drm_test_drm_hdmi_connector_get_broadcast_rgb_name_invalid(struct kunit *test)
{
	KUNIT_EXPECT_NULL(test, drm_hdmi_connector_get_broadcast_rgb_name(3));
};

static struct kunit_case drm_hdmi_connector_get_broadcast_rgb_name_tests[] = {
	KUNIT_CASE_PARAM(drm_test_drm_hdmi_connector_get_broadcast_rgb_name,
			 drm_hdmi_connector_get_broadcast_rgb_name_valid_gen_params),
	KUNIT_CASE(drm_test_drm_hdmi_connector_get_broadcast_rgb_name_invalid),
	{ }
};

static struct kunit_suite drm_hdmi_connector_get_broadcast_rgb_name_test_suite = {
	.name = "drm_hdmi_connector_get_broadcast_rgb_name",
	.test_cases = drm_hdmi_connector_get_broadcast_rgb_name_tests,
};

struct drm_hdmi_connector_get_output_format_name_test {
	unsigned int kind;
	const char *expected_name;
};

#define OUTPUT_FORMAT_TEST(_kind, _name)	\
	{					\
		.kind = _kind,			\
		.expected_name = _name,		\
	}

static void drm_test_drm_hdmi_connector_get_output_format_name(struct kunit *test)
{
	const struct drm_hdmi_connector_get_output_format_name_test *params =
		test->param_value;

	KUNIT_EXPECT_STREQ(test,
			   drm_hdmi_connector_get_output_format_name(params->kind),
			   params->expected_name);
}

static const
struct drm_hdmi_connector_get_output_format_name_test
drm_hdmi_connector_get_output_format_name_valid_tests[] = {
	OUTPUT_FORMAT_TEST(HDMI_COLORSPACE_RGB, "RGB"),
	OUTPUT_FORMAT_TEST(HDMI_COLORSPACE_YUV420, "YUV 4:2:0"),
	OUTPUT_FORMAT_TEST(HDMI_COLORSPACE_YUV422, "YUV 4:2:2"),
	OUTPUT_FORMAT_TEST(HDMI_COLORSPACE_YUV444, "YUV 4:4:4"),
};

static void
drm_hdmi_connector_get_output_format_name_valid_desc(const struct drm_hdmi_connector_get_output_format_name_test *t,
						     char *desc)
{
	sprintf(desc, "%s", t->expected_name);
}

KUNIT_ARRAY_PARAM(drm_hdmi_connector_get_output_format_name_valid,
		  drm_hdmi_connector_get_output_format_name_valid_tests,
		  drm_hdmi_connector_get_output_format_name_valid_desc);

static void drm_test_drm_hdmi_connector_get_output_format_name_invalid(struct kunit *test)
{
	KUNIT_EXPECT_NULL(test, drm_hdmi_connector_get_output_format_name(4));
};

static struct kunit_case drm_hdmi_connector_get_output_format_name_tests[] = {
	KUNIT_CASE_PARAM(drm_test_drm_hdmi_connector_get_output_format_name,
			 drm_hdmi_connector_get_output_format_name_valid_gen_params),
	KUNIT_CASE(drm_test_drm_hdmi_connector_get_output_format_name_invalid),
	{ }
};

static struct kunit_suite drm_hdmi_connector_get_output_format_name_test_suite = {
	.name = "drm_hdmi_connector_get_output_format_name",
	.test_cases = drm_hdmi_connector_get_output_format_name_tests,
};

static void drm_test_drm_connector_attach_broadcast_rgb_property(struct kunit *test)
{
	struct drm_connector_init_priv *priv = test->priv;
	struct drm_connector *connector = &priv->connector;
	struct drm_property *prop;
	int ret;

	ret = drmm_connector_init(&priv->drm, connector,
				  &dummy_funcs,
				  DRM_MODE_CONNECTOR_HDMIA,
				  &priv->ddc);
	KUNIT_ASSERT_EQ(test, ret, 0);

	ret = drm_connector_attach_broadcast_rgb_property(connector);
	KUNIT_ASSERT_EQ(test, ret, 0);

	prop = connector->broadcast_rgb_property;
	KUNIT_ASSERT_NOT_NULL(test, prop);
	KUNIT_EXPECT_NOT_NULL(test, drm_mode_obj_find_prop_id(&connector->base, prop->base.id));
}

static void drm_test_drm_connector_attach_broadcast_rgb_property_hdmi_connector(struct kunit *test)
{
	struct drm_connector_init_priv *priv = test->priv;
	struct drm_connector *connector = &priv->connector;
	struct drm_property *prop;
	int ret;

	ret = drmm_connector_hdmi_init(&priv->drm, connector,
				       "Vendor", "Product",
				       &dummy_funcs,
				       &dummy_hdmi_funcs,
				       DRM_MODE_CONNECTOR_HDMIA,
				       &priv->ddc,
				       BIT(HDMI_COLORSPACE_RGB),
				       8);
	KUNIT_EXPECT_EQ(test, ret, 0);

	ret = drm_connector_attach_broadcast_rgb_property(connector);
	KUNIT_ASSERT_EQ(test, ret, 0);

	prop = connector->broadcast_rgb_property;
	KUNIT_ASSERT_NOT_NULL(test, prop);
	KUNIT_EXPECT_NOT_NULL(test, drm_mode_obj_find_prop_id(&connector->base, prop->base.id));
}

static struct kunit_case drm_connector_attach_broadcast_rgb_property_tests[] = {
	KUNIT_CASE(drm_test_drm_connector_attach_broadcast_rgb_property),
	KUNIT_CASE(drm_test_drm_connector_attach_broadcast_rgb_property_hdmi_connector),
	{ }
};

static struct kunit_suite drm_connector_attach_broadcast_rgb_property_test_suite = {
	.name = "drm_connector_attach_broadcast_rgb_property",
	.init = drm_test_connector_init,
	.test_cases = drm_connector_attach_broadcast_rgb_property_tests,
};

/*
 * Test that for a given mode, with 8bpc and an RGB output the TMDS
 * character rate is equal to the mode pixel clock.
 */
static void drm_test_drm_hdmi_compute_mode_clock_rgb(struct kunit *test)
{
	struct drm_connector_init_priv *priv = test->priv;
	const struct drm_display_mode *mode;
	unsigned long long rate;
	struct drm_device *drm = &priv->drm;

	mode = drm_display_mode_from_cea_vic(drm, 16);
	KUNIT_ASSERT_NOT_NULL(test, mode);

	KUNIT_ASSERT_FALSE(test, mode->flags & DRM_MODE_FLAG_DBLCLK);

	rate = drm_hdmi_compute_mode_clock(mode, 8, HDMI_COLORSPACE_RGB);
	KUNIT_ASSERT_GT(test, rate, 0);
	KUNIT_EXPECT_EQ(test, mode->clock * 1000ULL, rate);
}

/*
 * Test that for a given mode, with 10bpc and an RGB output the TMDS
 * character rate is equal to 1.25 times the mode pixel clock.
 */
static void drm_test_drm_hdmi_compute_mode_clock_rgb_10bpc(struct kunit *test)
{
	struct drm_connector_init_priv *priv = test->priv;
	const struct drm_display_mode *mode;
	unsigned long long rate;
	struct drm_device *drm = &priv->drm;

	mode = drm_display_mode_from_cea_vic(drm, 16);
	KUNIT_ASSERT_NOT_NULL(test, mode);

	KUNIT_ASSERT_FALSE(test, mode->flags & DRM_MODE_FLAG_DBLCLK);

	rate = drm_hdmi_compute_mode_clock(mode, 10, HDMI_COLORSPACE_RGB);
	KUNIT_ASSERT_GT(test, rate, 0);
	KUNIT_EXPECT_EQ(test, mode->clock * 1250, rate);
}

/*
 * Test that for the VIC-1 mode, with 10bpc and an RGB output the TMDS
 * character rate computation fails.
 */
static void drm_test_drm_hdmi_compute_mode_clock_rgb_10bpc_vic_1(struct kunit *test)
{
	struct drm_connector_init_priv *priv = test->priv;
	const struct drm_display_mode *mode;
	unsigned long long rate;
	struct drm_device *drm = &priv->drm;

	mode = drm_display_mode_from_cea_vic(drm, 1);
	KUNIT_ASSERT_NOT_NULL(test, mode);

	rate = drm_hdmi_compute_mode_clock(mode, 10, HDMI_COLORSPACE_RGB);
	KUNIT_EXPECT_EQ(test, rate, 0);
}

/*
 * Test that for a given mode, with 12bpc and an RGB output the TMDS
 * character rate is equal to 1.5 times the mode pixel clock.
 */
static void drm_test_drm_hdmi_compute_mode_clock_rgb_12bpc(struct kunit *test)
{
	struct drm_connector_init_priv *priv = test->priv;
	const struct drm_display_mode *mode;
	unsigned long long rate;
	struct drm_device *drm = &priv->drm;

	mode = drm_display_mode_from_cea_vic(drm, 16);
	KUNIT_ASSERT_NOT_NULL(test, mode);

	KUNIT_ASSERT_FALSE(test, mode->flags & DRM_MODE_FLAG_DBLCLK);

	rate = drm_hdmi_compute_mode_clock(mode, 12, HDMI_COLORSPACE_RGB);
	KUNIT_ASSERT_GT(test, rate, 0);
	KUNIT_EXPECT_EQ(test, mode->clock * 1500, rate);
}

/*
 * Test that for the VIC-1 mode, with 12bpc and an RGB output the TMDS
 * character rate computation fails.
 */
static void drm_test_drm_hdmi_compute_mode_clock_rgb_12bpc_vic_1(struct kunit *test)
{
	struct drm_connector_init_priv *priv = test->priv;
	const struct drm_display_mode *mode;
	unsigned long long rate;
	struct drm_device *drm = &priv->drm;

	mode = drm_display_mode_from_cea_vic(drm, 1);
	KUNIT_ASSERT_NOT_NULL(test, mode);

	rate = drm_hdmi_compute_mode_clock(mode, 12, HDMI_COLORSPACE_RGB);
	KUNIT_EXPECT_EQ(test, rate, 0);
}

/*
 * Test that for a mode with the pixel repetition flag, the TMDS
 * character rate is indeed double the mode pixel clock.
 */
static void drm_test_drm_hdmi_compute_mode_clock_rgb_double(struct kunit *test)
{
	struct drm_connector_init_priv *priv = test->priv;
	const struct drm_display_mode *mode;
	unsigned long long rate;
	struct drm_device *drm = &priv->drm;

	mode = drm_display_mode_from_cea_vic(drm, 6);
	KUNIT_ASSERT_NOT_NULL(test, mode);

	KUNIT_ASSERT_TRUE(test, mode->flags & DRM_MODE_FLAG_DBLCLK);

	rate = drm_hdmi_compute_mode_clock(mode, 8, HDMI_COLORSPACE_RGB);
	KUNIT_ASSERT_GT(test, rate, 0);
	KUNIT_EXPECT_EQ(test, (mode->clock * 1000ULL) * 2, rate);
}

/*
 * Test that the TMDS character rate computation for the VIC modes
 * explicitly listed in the spec as supporting YUV420 succeed and return
 * half the mode pixel clock.
 */
static void drm_test_connector_hdmi_compute_mode_clock_yuv420_valid(struct kunit *test)
{
	struct drm_connector_init_priv *priv = test->priv;
	const struct drm_display_mode *mode;
	struct drm_device *drm = &priv->drm;
	unsigned long long rate;
	unsigned int vic = *(unsigned int *)test->param_value;

	mode = drm_display_mode_from_cea_vic(drm, vic);
	KUNIT_ASSERT_NOT_NULL(test, mode);

	KUNIT_ASSERT_FALSE(test, mode->flags & DRM_MODE_FLAG_DBLCLK);

	rate = drm_hdmi_compute_mode_clock(mode, 8, HDMI_COLORSPACE_YUV420);
	KUNIT_ASSERT_GT(test, rate, 0);
	KUNIT_EXPECT_EQ(test, (mode->clock * 1000ULL) / 2, rate);
}

static const unsigned int drm_hdmi_compute_mode_clock_yuv420_vic_valid_tests[] = {
	96, 97, 101, 102, 106, 107,
};

static void drm_hdmi_compute_mode_clock_yuv420_vic_desc(const unsigned int *vic, char *desc)
{
	sprintf(desc, "VIC %u", *vic);
}

KUNIT_ARRAY_PARAM(drm_hdmi_compute_mode_clock_yuv420_valid,
		  drm_hdmi_compute_mode_clock_yuv420_vic_valid_tests,
		  drm_hdmi_compute_mode_clock_yuv420_vic_desc);

/*
 * Test that for a given mode listed supporting it and an YUV420 output
 * with 10bpc, the TMDS character rate is equal to 0.625 times the mode
 * pixel clock.
 */
static void drm_test_connector_hdmi_compute_mode_clock_yuv420_10_bpc(struct kunit *test)
{
	struct drm_connector_init_priv *priv = test->priv;
	const struct drm_display_mode *mode;
	struct drm_device *drm = &priv->drm;
	unsigned int vic =
		drm_hdmi_compute_mode_clock_yuv420_vic_valid_tests[0];
	unsigned long long rate;

	mode = drm_display_mode_from_cea_vic(drm, vic);
	KUNIT_ASSERT_NOT_NULL(test, mode);

	KUNIT_ASSERT_FALSE(test, mode->flags & DRM_MODE_FLAG_DBLCLK);

	rate = drm_hdmi_compute_mode_clock(mode, 10, HDMI_COLORSPACE_YUV420);
	KUNIT_ASSERT_GT(test, rate, 0);

	KUNIT_EXPECT_EQ(test, mode->clock * 625, rate);
}

/*
 * Test that for a given mode listed supporting it and an YUV420 output
 * with 12bpc, the TMDS character rate is equal to 0.75 times the mode
 * pixel clock.
 */
static void drm_test_connector_hdmi_compute_mode_clock_yuv420_12_bpc(struct kunit *test)
{
	struct drm_connector_init_priv *priv = test->priv;
	const struct drm_display_mode *mode;
	struct drm_device *drm = &priv->drm;
	unsigned int vic =
		drm_hdmi_compute_mode_clock_yuv420_vic_valid_tests[0];
	unsigned long long rate;

	mode = drm_display_mode_from_cea_vic(drm, vic);
	KUNIT_ASSERT_NOT_NULL(test, mode);

	KUNIT_ASSERT_FALSE(test, mode->flags & DRM_MODE_FLAG_DBLCLK);

	rate = drm_hdmi_compute_mode_clock(mode, 12, HDMI_COLORSPACE_YUV420);
	KUNIT_ASSERT_GT(test, rate, 0);

	KUNIT_EXPECT_EQ(test, mode->clock * 750, rate);
}

/*
 * Test that for a given mode, the computation of the TMDS character
 * rate with 8bpc and a YUV422 output succeeds and returns a rate equal
 * to the mode pixel clock.
 */
static void drm_test_connector_hdmi_compute_mode_clock_yuv422_8_bpc(struct kunit *test)
{
	struct drm_connector_init_priv *priv = test->priv;
	const struct drm_display_mode *mode;
	struct drm_device *drm = &priv->drm;
	unsigned long long rate;

	mode = drm_display_mode_from_cea_vic(drm, 16);
	KUNIT_ASSERT_NOT_NULL(test, mode);

	KUNIT_ASSERT_FALSE(test, mode->flags & DRM_MODE_FLAG_DBLCLK);

	rate = drm_hdmi_compute_mode_clock(mode, 8, HDMI_COLORSPACE_YUV422);
	KUNIT_ASSERT_GT(test, rate, 0);
	KUNIT_EXPECT_EQ(test, mode->clock * 1000, rate);
}

/*
 * Test that for a given mode, the computation of the TMDS character
 * rate with 10bpc and a YUV422 output succeeds and returns a rate equal
 * to the mode pixel clock.
 */
static void drm_test_connector_hdmi_compute_mode_clock_yuv422_10_bpc(struct kunit *test)
{
	struct drm_connector_init_priv *priv = test->priv;
	const struct drm_display_mode *mode;
	struct drm_device *drm = &priv->drm;
	unsigned long long rate;

	mode = drm_display_mode_from_cea_vic(drm, 16);
	KUNIT_ASSERT_NOT_NULL(test, mode);

	KUNIT_ASSERT_FALSE(test, mode->flags & DRM_MODE_FLAG_DBLCLK);

	rate = drm_hdmi_compute_mode_clock(mode, 10, HDMI_COLORSPACE_YUV422);
	KUNIT_ASSERT_GT(test, rate, 0);
	KUNIT_EXPECT_EQ(test, mode->clock * 1000, rate);
}

/*
 * Test that for a given mode, the computation of the TMDS character
 * rate with 12bpc and a YUV422 output succeeds and returns a rate equal
 * to the mode pixel clock.
 */
static void drm_test_connector_hdmi_compute_mode_clock_yuv422_12_bpc(struct kunit *test)
{
	struct drm_connector_init_priv *priv = test->priv;
	const struct drm_display_mode *mode;
	struct drm_device *drm = &priv->drm;
	unsigned long long rate;

	mode = drm_display_mode_from_cea_vic(drm, 16);
	KUNIT_ASSERT_NOT_NULL(test, mode);

	KUNIT_ASSERT_FALSE(test, mode->flags & DRM_MODE_FLAG_DBLCLK);

	rate = drm_hdmi_compute_mode_clock(mode, 12, HDMI_COLORSPACE_YUV422);
	KUNIT_ASSERT_GT(test, rate, 0);
	KUNIT_EXPECT_EQ(test, mode->clock * 1000, rate);
}

static struct kunit_case drm_hdmi_compute_mode_clock_tests[] = {
	KUNIT_CASE(drm_test_drm_hdmi_compute_mode_clock_rgb),
	KUNIT_CASE(drm_test_drm_hdmi_compute_mode_clock_rgb_10bpc),
	KUNIT_CASE(drm_test_drm_hdmi_compute_mode_clock_rgb_10bpc_vic_1),
	KUNIT_CASE(drm_test_drm_hdmi_compute_mode_clock_rgb_12bpc),
	KUNIT_CASE(drm_test_drm_hdmi_compute_mode_clock_rgb_12bpc_vic_1),
	KUNIT_CASE(drm_test_drm_hdmi_compute_mode_clock_rgb_double),
	KUNIT_CASE_PARAM(drm_test_connector_hdmi_compute_mode_clock_yuv420_valid,
			 drm_hdmi_compute_mode_clock_yuv420_valid_gen_params),
	KUNIT_CASE(drm_test_connector_hdmi_compute_mode_clock_yuv420_10_bpc),
	KUNIT_CASE(drm_test_connector_hdmi_compute_mode_clock_yuv420_12_bpc),
	KUNIT_CASE(drm_test_connector_hdmi_compute_mode_clock_yuv422_8_bpc),
	KUNIT_CASE(drm_test_connector_hdmi_compute_mode_clock_yuv422_10_bpc),
	KUNIT_CASE(drm_test_connector_hdmi_compute_mode_clock_yuv422_12_bpc),
	{ }
};

static struct kunit_suite drm_hdmi_compute_mode_clock_test_suite = {
	.name = "drm_test_connector_hdmi_compute_mode_clock",
	.init = drm_test_connector_init,
	.test_cases = drm_hdmi_compute_mode_clock_tests,
};

kunit_test_suites(
	&drmm_connector_hdmi_init_test_suite,
	&drmm_connector_init_test_suite,
	&drm_connector_attach_broadcast_rgb_property_test_suite,
	&drm_get_tv_mode_from_name_test_suite,
	&drm_hdmi_compute_mode_clock_test_suite,
	&drm_hdmi_connector_get_broadcast_rgb_name_test_suite,
	&drm_hdmi_connector_get_output_format_name_test_suite
);

MODULE_AUTHOR("Maxime Ripard <maxime@cerno.tech>");
MODULE_DESCRIPTION("Kunit test for drm_modes functions");
MODULE_LICENSE("GPL");