1 // SPDX-License-Identifier: GPL-2.0
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>
10 #include <kunit/test.h>
14 static const struct drm_connector_helper_funcs vc4_dummy_connector_helper_funcs = {
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,
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)
30 struct vc4_dummy_output *dummy_output;
31 struct drm_connector *conn;
32 struct drm_encoder *enc;
35 dummy_output = drmm_kzalloc(drm, sizeof(*dummy_output), GFP_KERNEL);
36 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dummy_output);
37 dummy_output->encoder.type = vc4_encoder_type;
39 enc = &dummy_output->encoder.base;
40 ret = drmm_encoder_init(drm, enc,
44 KUNIT_ASSERT_EQ(test, ret, 0);
45 enc->possible_crtcs = drm_crtc_mask(crtc);
47 conn = &dummy_output->connector;
48 ret = drmm_connector_init(drm, conn,
49 &vc4_dummy_connector_funcs,
52 KUNIT_ASSERT_EQ(test, ret, 0);
54 drm_connector_helper_add(conn, &vc4_dummy_connector_helper_funcs);
55 drm_connector_attach_encoder(conn, enc);
60 static const struct drm_display_mode default_mode = {
61 DRM_SIMPLE_MODE(640, 480, 64, 48)
64 struct vc4_dummy_output *
65 vc4_mock_atomic_add_output(struct kunit *test,
66 struct drm_atomic_state *state,
67 enum vc4_encoder_type type)
69 struct drm_device *drm = state->dev;
70 struct drm_connector_state *conn_state;
71 struct drm_crtc_state *crtc_state;
72 struct vc4_dummy_output *output;
73 struct drm_connector *conn;
74 struct drm_encoder *encoder;
75 struct drm_crtc *crtc;
78 encoder = vc4_find_encoder_by_type(drm, type);
79 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, encoder);
81 crtc = vc4_find_crtc_for_encoder(test, encoder);
82 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, crtc);
84 output = encoder_to_vc4_dummy_output(encoder);
85 conn = &output->connector;
86 conn_state = drm_atomic_get_connector_state(state, conn);
87 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, conn_state);
89 ret = drm_atomic_set_crtc_for_connector(conn_state, crtc);
90 KUNIT_EXPECT_EQ(test, ret, 0);
92 crtc_state = drm_atomic_get_crtc_state(state, crtc);
93 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, crtc_state);
95 ret = drm_atomic_set_mode_for_crtc(crtc_state, &default_mode);
96 KUNIT_EXPECT_EQ(test, ret, 0);
98 crtc_state->active = true;
103 int vc4_mock_atomic_del_output(struct kunit *test,
104 struct drm_atomic_state *state,
105 enum vc4_encoder_type type)
107 struct drm_device *drm = state->dev;
108 struct drm_connector_state *conn_state;
109 struct drm_crtc_state *crtc_state;
110 struct vc4_dummy_output *output;
111 struct drm_connector *conn;
112 struct drm_encoder *encoder;
113 struct drm_crtc *crtc;
116 encoder = vc4_find_encoder_by_type(drm, type);
117 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, encoder);
119 crtc = vc4_find_crtc_for_encoder(test, encoder);
120 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, crtc);
122 crtc_state = drm_atomic_get_crtc_state(state, crtc);
123 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, crtc_state);
125 crtc_state->active = false;
127 ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
128 KUNIT_ASSERT_EQ(test, ret, 0);
130 output = encoder_to_vc4_dummy_output(encoder);
131 conn = &output->connector;
132 conn_state = drm_atomic_get_connector_state(state, conn);
133 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, conn_state);
135 ret = drm_atomic_set_crtc_for_connector(conn_state, NULL);
136 KUNIT_ASSERT_EQ(test, ret, 0);