1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include <drm/drm_atomic_state_helper.h>
4 #include <drm/drm_atomic_uapi.h>
5 #include <drm/drm_connector.h>
6 #include <drm/drm_crtc.h>
7 #include <drm/drm_encoder.h>
8 #include <drm/drm_modeset_helper_vtables.h>
9 
10 #include <kunit/test.h>
11 
12 #include "vc4_mock.h"
13 
14 static const struct drm_connector_helper_funcs vc4_dummy_connector_helper_funcs = {
15 };
16 
17 static const struct drm_connector_funcs vc4_dummy_connector_funcs = {
18 	.atomic_destroy_state	= drm_atomic_helper_connector_destroy_state,
19 	.atomic_duplicate_state	= drm_atomic_helper_connector_duplicate_state,
20 	.reset			= drm_atomic_helper_connector_reset,
21 };
22 
23 struct vc4_dummy_output *vc4_dummy_output(struct kunit *test,
24 					  struct drm_device *drm,
25 					  struct drm_crtc *crtc,
26 					  enum vc4_encoder_type vc4_encoder_type,
27 					  unsigned int kms_encoder_type,
28 					  unsigned int connector_type)
29 {
30 	struct vc4_dummy_output *dummy_output;
31 	struct drm_connector *conn;
32 	struct drm_encoder *enc;
33 	int ret;
34 
35 	dummy_output = kunit_kzalloc(test, sizeof(*dummy_output), GFP_KERNEL);
36 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dummy_output);
37 	dummy_output->encoder.type = vc4_encoder_type;
38 
39 	enc = &dummy_output->encoder.base;
40 	ret = drmm_encoder_init(drm, enc,
41 				NULL,
42 				kms_encoder_type,
43 				NULL);
44 	KUNIT_ASSERT_EQ(test, ret, 0);
45 	enc->possible_crtcs = drm_crtc_mask(crtc);
46 
47 	conn = &dummy_output->connector;
48 	ret = drmm_connector_init(drm, conn,
49 				  &vc4_dummy_connector_funcs,
50 				  connector_type,
51 				  NULL);
52 	KUNIT_ASSERT_EQ(test, ret, 0);
53 
54 	drm_connector_helper_add(conn, &vc4_dummy_connector_helper_funcs);
55 	drm_connector_attach_encoder(conn, enc);
56 
57 	return dummy_output;
58 }
59 
60 static const struct drm_display_mode default_mode = {
61 	DRM_SIMPLE_MODE(640, 480, 64, 48)
62 };
63 
64 int vc4_mock_atomic_add_output(struct kunit *test,
65 			       struct drm_atomic_state *state,
66 			       enum vc4_encoder_type type)
67 {
68 	struct drm_device *drm = state->dev;
69 	struct drm_connector_state *conn_state;
70 	struct drm_crtc_state *crtc_state;
71 	struct vc4_dummy_output *output;
72 	struct drm_connector *conn;
73 	struct drm_encoder *encoder;
74 	struct drm_crtc *crtc;
75 	int ret;
76 
77 	encoder = vc4_find_encoder_by_type(drm, type);
78 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, encoder);
79 
80 	crtc = vc4_find_crtc_for_encoder(test, drm, encoder);
81 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, crtc);
82 
83 	output = container_of(encoder, struct vc4_dummy_output, encoder.base);
84 	conn = &output->connector;
85 	conn_state = drm_atomic_get_connector_state(state, conn);
86 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, conn_state);
87 
88 	ret = drm_atomic_set_crtc_for_connector(conn_state, crtc);
89 	KUNIT_EXPECT_EQ(test, ret, 0);
90 
91 	crtc_state = drm_atomic_get_crtc_state(state, crtc);
92 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, crtc_state);
93 
94 	ret = drm_atomic_set_mode_for_crtc(crtc_state, &default_mode);
95 	KUNIT_EXPECT_EQ(test, ret, 0);
96 
97 	crtc_state->active = true;
98 
99 	return 0;
100 }
101 
102 int vc4_mock_atomic_del_output(struct kunit *test,
103 			       struct drm_atomic_state *state,
104 			       enum vc4_encoder_type type)
105 {
106 	struct drm_device *drm = state->dev;
107 	struct drm_connector_state *conn_state;
108 	struct drm_crtc_state *crtc_state;
109 	struct vc4_dummy_output *output;
110 	struct drm_connector *conn;
111 	struct drm_encoder *encoder;
112 	struct drm_crtc *crtc;
113 	int ret;
114 
115 	encoder = vc4_find_encoder_by_type(drm, type);
116 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, encoder);
117 
118 	crtc = vc4_find_crtc_for_encoder(test, drm, encoder);
119 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, crtc);
120 
121 	crtc_state = drm_atomic_get_crtc_state(state, crtc);
122 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, crtc_state);
123 
124 	crtc_state->active = false;
125 
126 	ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
127 	KUNIT_ASSERT_EQ(test, ret, 0);
128 
129 	output = container_of(encoder, struct vc4_dummy_output, encoder.base);
130 	conn = &output->connector;
131 	conn_state = drm_atomic_get_connector_state(state, conn);
132 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, conn_state);
133 
134 	ret = drm_atomic_set_crtc_for_connector(conn_state, NULL);
135 	KUNIT_ASSERT_EQ(test, ret, 0);
136 
137 	return 0;
138 }
139