1 // SPDX-License-Identifier: MIT
3 * Copyright © 2023 Intel Corporation
6 #include <drm/drm_atomic.h>
7 #include <drm/drm_atomic_helper.h>
8 #include <drm/drm_atomic_uapi.h>
11 #include "intel_atomic.h"
12 #include "intel_crtc.h"
13 #include "intel_display_types.h"
14 #include "intel_load_detect.h"
16 /* VESA 640x480x72Hz mode to set on the pipe */
17 static const struct drm_display_mode load_detect_mode = {
18 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
19 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
22 static int intel_modeset_disable_planes(struct drm_atomic_state *state,
23 struct drm_crtc *crtc)
25 struct drm_plane *plane;
26 struct drm_plane_state *plane_state;
29 ret = drm_atomic_add_affected_planes(state, crtc);
33 for_each_new_plane_in_state(state, plane, plane_state, i) {
34 if (plane_state->crtc != crtc)
37 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
41 drm_atomic_set_fb_for_plane(plane_state, NULL);
47 struct drm_atomic_state *
48 intel_load_detect_get_pipe(struct drm_connector *connector,
49 struct drm_modeset_acquire_ctx *ctx)
51 struct intel_encoder *encoder =
52 intel_attached_encoder(to_intel_connector(connector));
53 struct intel_crtc *possible_crtc;
54 struct intel_crtc *crtc = NULL;
55 struct drm_device *dev = encoder->base.dev;
56 struct drm_i915_private *dev_priv = to_i915(dev);
57 struct drm_mode_config *config = &dev->mode_config;
58 struct drm_atomic_state *state = NULL, *restore_state = NULL;
59 struct drm_connector_state *connector_state;
60 struct intel_crtc_state *crtc_state;
63 drm_dbg_kms(&dev_priv->drm, "[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
64 connector->base.id, connector->name,
65 encoder->base.base.id, encoder->base.name);
67 drm_WARN_ON(dev, !drm_modeset_is_locked(&config->connection_mutex));
70 * Algorithm gets a little messy:
72 * - if the connector already has an assigned crtc, use it (but make
75 * - try to find the first unused crtc that can drive this connector,
76 * and use that if we find one
79 /* See if we already have a CRTC for this connector */
80 if (connector->state->crtc) {
81 crtc = to_intel_crtc(connector->state->crtc);
83 ret = drm_modeset_lock(&crtc->base.mutex, ctx);
87 /* Make sure the crtc and connector are running */
91 /* Find an unused one (if possible) */
92 for_each_intel_crtc(dev, possible_crtc) {
93 if (!(encoder->base.possible_crtcs &
94 drm_crtc_mask(&possible_crtc->base)))
97 ret = drm_modeset_lock(&possible_crtc->base.mutex, ctx);
101 if (possible_crtc->base.state->enable) {
102 drm_modeset_unlock(&possible_crtc->base.mutex);
106 crtc = possible_crtc;
111 * If we didn't find an unused CRTC, don't use any.
114 drm_dbg_kms(&dev_priv->drm,
115 "no pipe available for load-detect\n");
121 state = drm_atomic_state_alloc(dev);
122 restore_state = drm_atomic_state_alloc(dev);
123 if (!state || !restore_state) {
128 state->acquire_ctx = ctx;
129 to_intel_atomic_state(state)->internal = true;
131 restore_state->acquire_ctx = ctx;
132 to_intel_atomic_state(restore_state)->internal = true;
134 connector_state = drm_atomic_get_connector_state(state, connector);
135 if (IS_ERR(connector_state)) {
136 ret = PTR_ERR(connector_state);
140 ret = drm_atomic_set_crtc_for_connector(connector_state, &crtc->base);
144 crtc_state = intel_atomic_get_crtc_state(state, crtc);
145 if (IS_ERR(crtc_state)) {
146 ret = PTR_ERR(crtc_state);
150 crtc_state->uapi.active = true;
152 ret = drm_atomic_set_mode_for_crtc(&crtc_state->uapi,
157 ret = intel_modeset_disable_planes(state, &crtc->base);
161 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
163 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, &crtc->base));
165 ret = drm_atomic_add_affected_planes(restore_state, &crtc->base);
167 drm_dbg_kms(&dev_priv->drm,
168 "Failed to create a copy of old state to restore: %i\n",
173 ret = drm_atomic_commit(state);
175 drm_dbg_kms(&dev_priv->drm,
176 "failed to set mode on load-detect pipe\n");
180 drm_atomic_state_put(state);
182 /* let the connector get through one full cycle before testing */
183 intel_crtc_wait_for_next_vblank(crtc);
185 return restore_state;
189 drm_atomic_state_put(state);
193 drm_atomic_state_put(restore_state);
194 restore_state = NULL;
203 void intel_load_detect_release_pipe(struct drm_connector *connector,
204 struct drm_atomic_state *state,
205 struct drm_modeset_acquire_ctx *ctx)
207 struct intel_encoder *intel_encoder =
208 intel_attached_encoder(to_intel_connector(connector));
209 struct drm_i915_private *i915 = to_i915(intel_encoder->base.dev);
210 struct drm_encoder *encoder = &intel_encoder->base;
213 drm_dbg_kms(&i915->drm, "[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
214 connector->base.id, connector->name,
215 encoder->base.id, encoder->name);
217 if (IS_ERR_OR_NULL(state))
220 ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
222 drm_dbg_kms(&i915->drm,
223 "Couldn't release load detect pipe: %i\n", ret);
224 drm_atomic_state_put(state);