| 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 = drmm_kzalloc(dev: drm, size: 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(dev: drm, encoder: enc, |
| 41 | NULL, |
| 42 | encoder_type: 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(dev: drm, connector: conn, |
| 49 | funcs: &vc4_dummy_connector_funcs, |
| 50 | connector_type, |
| 51 | NULL); |
| 52 | KUNIT_ASSERT_EQ(test, ret, 0); |
| 53 | |
| 54 | drm_connector_helper_add(connector: conn, funcs: &vc4_dummy_connector_helper_funcs); |
| 55 | drm_connector_attach_encoder(connector: conn, encoder: 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 | /** |
| 65 | * vc4_mock_atomic_add_output() - Enables an output in a state |
| 66 | * @test: The test context object |
| 67 | * @state: Atomic state to enable the output in. |
| 68 | * @type: Type of the output encoder |
| 69 | * |
| 70 | * Adds an output CRTC and connector to a state, and enables them. |
| 71 | * |
| 72 | * Returns: |
| 73 | * 0 on success, a negative error code on failure. If the error is |
| 74 | * EDEADLK, the entire atomic sequence must be restarted. All other |
| 75 | * errors are fatal. |
| 76 | */ |
| 77 | int vc4_mock_atomic_add_output(struct kunit *test, |
| 78 | struct drm_atomic_state *state, |
| 79 | enum vc4_encoder_type type) |
| 80 | { |
| 81 | struct drm_device *drm = state->dev; |
| 82 | struct drm_connector_state *conn_state; |
| 83 | struct drm_crtc_state *crtc_state; |
| 84 | struct vc4_dummy_output *output; |
| 85 | struct drm_connector *conn; |
| 86 | struct drm_encoder *encoder; |
| 87 | struct drm_crtc *crtc; |
| 88 | int ret; |
| 89 | |
| 90 | encoder = vc4_find_encoder_by_type(drm, type); |
| 91 | if (!encoder) |
| 92 | return -ENODEV; |
| 93 | |
| 94 | crtc = vc4_find_crtc_for_encoder(test, drm, encoder); |
| 95 | if (!crtc) |
| 96 | return -ENODEV; |
| 97 | |
| 98 | output = encoder_to_vc4_dummy_output(encoder); |
| 99 | conn = &output->connector; |
| 100 | conn_state = drm_atomic_get_connector_state(state, connector: conn); |
| 101 | if (IS_ERR(ptr: conn_state)) |
| 102 | return PTR_ERR(ptr: conn_state); |
| 103 | |
| 104 | ret = drm_atomic_set_crtc_for_connector(conn_state, crtc); |
| 105 | if (ret) |
| 106 | return ret; |
| 107 | |
| 108 | crtc_state = drm_atomic_get_crtc_state(state, crtc); |
| 109 | if (IS_ERR(ptr: crtc_state)) |
| 110 | return PTR_ERR(ptr: crtc_state); |
| 111 | |
| 112 | ret = drm_atomic_set_mode_for_crtc(state: crtc_state, mode: &default_mode); |
| 113 | if (ret) |
| 114 | return ret; |
| 115 | |
| 116 | crtc_state->active = true; |
| 117 | |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * vc4_mock_atomic_del_output() - Disables an output in a state |
| 123 | * @test: The test context object |
| 124 | * @state: Atomic state to disable the output in. |
| 125 | * @type: Type of the output encoder |
| 126 | * |
| 127 | * Adds an output CRTC and connector to a state, and disables them. |
| 128 | * |
| 129 | * Returns: |
| 130 | * 0 on success, a negative error code on failure. If the error is |
| 131 | * EDEADLK, the entire atomic sequence must be restarted. All other |
| 132 | * errors are fatal. |
| 133 | */ |
| 134 | int vc4_mock_atomic_del_output(struct kunit *test, |
| 135 | struct drm_atomic_state *state, |
| 136 | enum vc4_encoder_type type) |
| 137 | { |
| 138 | struct drm_device *drm = state->dev; |
| 139 | struct drm_connector_state *conn_state; |
| 140 | struct drm_crtc_state *crtc_state; |
| 141 | struct vc4_dummy_output *output; |
| 142 | struct drm_connector *conn; |
| 143 | struct drm_encoder *encoder; |
| 144 | struct drm_crtc *crtc; |
| 145 | int ret; |
| 146 | |
| 147 | encoder = vc4_find_encoder_by_type(drm, type); |
| 148 | if (!encoder) |
| 149 | return -ENODEV; |
| 150 | |
| 151 | crtc = vc4_find_crtc_for_encoder(test, drm, encoder); |
| 152 | if (!crtc) |
| 153 | return -ENODEV; |
| 154 | |
| 155 | crtc_state = drm_atomic_get_crtc_state(state, crtc); |
| 156 | if (IS_ERR(ptr: crtc_state)) |
| 157 | return PTR_ERR(ptr: crtc_state); |
| 158 | |
| 159 | crtc_state->active = false; |
| 160 | |
| 161 | ret = drm_atomic_set_mode_for_crtc(state: crtc_state, NULL); |
| 162 | if (ret) |
| 163 | return ret; |
| 164 | |
| 165 | output = encoder_to_vc4_dummy_output(encoder); |
| 166 | conn = &output->connector; |
| 167 | conn_state = drm_atomic_get_connector_state(state, connector: conn); |
| 168 | if (IS_ERR(ptr: conn_state)) |
| 169 | return PTR_ERR(ptr: conn_state); |
| 170 | |
| 171 | ret = drm_atomic_set_crtc_for_connector(conn_state, NULL); |
| 172 | if (ret) |
| 173 | return ret; |
| 174 | |
| 175 | return 0; |
| 176 | } |
| 177 | |