2 * Copyright (c) 2006-2008 Intel Corporation
5 * DRM core CRTC related functions
7 * Permission to use, copy, modify, distribute, and sell this software and its
8 * documentation for any purpose is hereby granted without fee, provided that
9 * the above copyright notice appear in all copies and that both that copyright
10 * notice and this permission notice appear in supporting documentation, and
11 * that the name of the copyright holders not be used in advertising or
12 * publicity pertaining to distribution of the software without specific,
13 * written prior permission. The copyright holders make no representations
14 * about the suitability of this software for any purpose. It is provided "as
15 * is" without express or implied warranty.
17 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
18 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
19 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
20 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
22 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
32 #include <linux/export.h>
33 #include <linux/kernel.h>
34 #include <linux/moduleparam.h>
35 #include <linux/dynamic_debug.h>
37 #include <drm/drm_atomic.h>
38 #include <drm/drm_atomic_helper.h>
39 #include <drm/drm_atomic_uapi.h>
40 #include <drm/drm_bridge.h>
41 #include <drm/drm_crtc.h>
42 #include <drm/drm_crtc_helper.h>
43 #include <drm/drm_drv.h>
44 #include <drm/drm_edid.h>
45 #include <drm/drm_encoder.h>
46 #include <drm/drm_fb_helper.h>
47 #include <drm/drm_fourcc.h>
48 #include <drm/drm_framebuffer.h>
49 #include <drm/drm_print.h>
50 #include <drm/drm_vblank.h>
52 #include "drm_crtc_helper_internal.h"
54 DECLARE_DYNDBG_CLASSMAP(drm_debug_classes, DD_CLASS_TYPE_DISJOINT_BITS, 0,
69 * The CRTC modeset helper library provides a default set_config implementation
70 * in drm_crtc_helper_set_config(). Plus a few other convenience functions using
71 * the same callbacks which drivers can use to e.g. restore the modeset
72 * configuration on resume with drm_helper_resume_force_mode().
74 * Note that this helper library doesn't track the current power state of CRTCs
75 * and encoders. It can call callbacks like &drm_encoder_helper_funcs.dpms even
76 * though the hardware is already in the desired state. This deficiency has been
77 * fixed in the atomic helpers.
79 * The driver callbacks are mostly compatible with the atomic modeset helpers,
80 * except for the handling of the primary plane: Atomic helpers require that the
81 * primary plane is implemented as a real standalone plane and not directly tied
82 * to the CRTC state. For easier transition this library provides functions to
83 * implement the old semantics required by the CRTC helpers using the new plane
84 * and atomic helper callbacks.
86 * Drivers are strongly urged to convert to the atomic helpers (by way of first
87 * converting to the plane helpers). New drivers must not use these functions
88 * but need to implement the atomic interface instead, potentially using the
89 * atomic helpers for that.
91 * These legacy modeset helpers use the same function table structures as
92 * all other modesetting helpers. See the documentation for struct
93 * &drm_crtc_helper_funcs, &struct drm_encoder_helper_funcs and struct
94 * &drm_connector_helper_funcs.
98 * drm_helper_encoder_in_use - check if a given encoder is in use
99 * @encoder: encoder to check
101 * Checks whether @encoder is with the current mode setting output configuration
102 * in use by any connector. This doesn't mean that it is actually enabled since
103 * the DPMS state is tracked separately.
106 * True if @encoder is used, false otherwise.
108 bool drm_helper_encoder_in_use(struct drm_encoder *encoder)
110 struct drm_connector *connector;
111 struct drm_connector_list_iter conn_iter;
112 struct drm_device *dev = encoder->dev;
114 WARN_ON(drm_drv_uses_atomic_modeset(dev));
117 * We can expect this mutex to be locked if we are not panicking.
118 * Locking is currently fubar in the panic handler.
120 if (!oops_in_progress) {
121 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
122 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
126 drm_connector_list_iter_begin(dev, &conn_iter);
127 drm_for_each_connector_iter(connector, &conn_iter) {
128 if (connector->encoder == encoder) {
129 drm_connector_list_iter_end(&conn_iter);
133 drm_connector_list_iter_end(&conn_iter);
136 EXPORT_SYMBOL(drm_helper_encoder_in_use);
139 * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config
140 * @crtc: CRTC to check
142 * Checks whether @crtc is with the current mode setting output configuration
143 * in use by any connector. This doesn't mean that it is actually enabled since
144 * the DPMS state is tracked separately.
147 * True if @crtc is used, false otherwise.
149 bool drm_helper_crtc_in_use(struct drm_crtc *crtc)
151 struct drm_encoder *encoder;
152 struct drm_device *dev = crtc->dev;
154 WARN_ON(drm_drv_uses_atomic_modeset(dev));
157 * We can expect this mutex to be locked if we are not panicking.
158 * Locking is currently fubar in the panic handler.
160 if (!oops_in_progress)
161 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
163 drm_for_each_encoder(encoder, dev)
164 if (encoder->crtc == crtc && drm_helper_encoder_in_use(encoder))
168 EXPORT_SYMBOL(drm_helper_crtc_in_use);
171 drm_encoder_disable(struct drm_encoder *encoder)
173 const struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
178 if (encoder_funcs->disable)
179 (*encoder_funcs->disable)(encoder);
180 else if (encoder_funcs->dpms)
181 (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
184 static void __drm_helper_disable_unused_functions(struct drm_device *dev)
186 struct drm_encoder *encoder;
187 struct drm_crtc *crtc;
189 drm_warn_on_modeset_not_all_locked(dev);
191 drm_for_each_encoder(encoder, dev) {
192 if (!drm_helper_encoder_in_use(encoder)) {
193 drm_encoder_disable(encoder);
194 /* disconnect encoder from any connector */
195 encoder->crtc = NULL;
199 drm_for_each_crtc(crtc, dev) {
200 const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
202 crtc->enabled = drm_helper_crtc_in_use(crtc);
203 if (!crtc->enabled) {
204 if (crtc_funcs->disable)
205 (*crtc_funcs->disable)(crtc);
207 (*crtc_funcs->dpms)(crtc, DRM_MODE_DPMS_OFF);
208 crtc->primary->fb = NULL;
214 * drm_helper_disable_unused_functions - disable unused objects
217 * This function walks through the entire mode setting configuration of @dev. It
218 * will remove any CRTC links of unused encoders and encoder links of
219 * disconnected connectors. Then it will disable all unused encoders and CRTCs
220 * either by calling their disable callback if available or by calling their
221 * dpms callback with DRM_MODE_DPMS_OFF.
225 * This function is part of the legacy modeset helper library and will cause
226 * major confusion with atomic drivers. This is because atomic helpers guarantee
227 * to never call ->disable() hooks on a disabled function, or ->enable() hooks
228 * on an enabled functions. drm_helper_disable_unused_functions() on the other
229 * hand throws such guarantees into the wind and calls disable hooks
230 * unconditionally on unused functions.
232 void drm_helper_disable_unused_functions(struct drm_device *dev)
234 WARN_ON(drm_drv_uses_atomic_modeset(dev));
236 drm_modeset_lock_all(dev);
237 __drm_helper_disable_unused_functions(dev);
238 drm_modeset_unlock_all(dev);
240 EXPORT_SYMBOL(drm_helper_disable_unused_functions);
243 * Check the CRTC we're going to map each output to vs. its current
244 * CRTC. If they don't match, we have to disable the output and the CRTC
245 * since the driver will have to re-route things.
248 drm_crtc_prepare_encoders(struct drm_device *dev)
250 const struct drm_encoder_helper_funcs *encoder_funcs;
251 struct drm_encoder *encoder;
253 drm_for_each_encoder(encoder, dev) {
254 encoder_funcs = encoder->helper_private;
258 /* Disable unused encoders */
259 if (encoder->crtc == NULL)
260 drm_encoder_disable(encoder);
265 * drm_crtc_helper_set_mode - internal helper to set a mode
266 * @crtc: CRTC to program
268 * @x: horizontal offset into the surface
269 * @y: vertical offset into the surface
270 * @old_fb: old framebuffer, for cleanup
272 * Try to set @mode on @crtc. Give @crtc and its associated connectors a chance
273 * to fixup or reject the mode prior to trying to set it. This is an internal
274 * helper that drivers could e.g. use to update properties that require the
275 * entire output pipe to be disabled and re-enabled in a new configuration. For
276 * example for changing whether audio is enabled on a hdmi link or for changing
277 * panel fitter or dither attributes. It is also called by the
278 * drm_crtc_helper_set_config() helper function to drive the mode setting
282 * True if the mode was set successfully, false otherwise.
284 bool drm_crtc_helper_set_mode(struct drm_crtc *crtc,
285 struct drm_display_mode *mode,
287 struct drm_framebuffer *old_fb)
289 struct drm_device *dev = crtc->dev;
290 struct drm_display_mode *adjusted_mode, saved_mode, saved_hwmode;
291 const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
292 const struct drm_encoder_helper_funcs *encoder_funcs;
293 int saved_x, saved_y;
295 struct drm_encoder *encoder;
298 WARN_ON(drm_drv_uses_atomic_modeset(dev));
300 drm_warn_on_modeset_not_all_locked(dev);
302 saved_enabled = crtc->enabled;
303 crtc->enabled = drm_helper_crtc_in_use(crtc);
307 adjusted_mode = drm_mode_duplicate(dev, mode);
308 if (!adjusted_mode) {
309 crtc->enabled = saved_enabled;
313 drm_mode_init(&saved_mode, &crtc->mode);
314 drm_mode_init(&saved_hwmode, &crtc->hwmode);
318 /* Update crtc values up front so the driver can rely on them for mode
321 drm_mode_copy(&crtc->mode, mode);
325 /* Pass our mode to the connectors and the CRTC to give them a chance to
326 * adjust it according to limitations or connector properties, and also
327 * a chance to reject the mode entirely.
329 drm_for_each_encoder(encoder, dev) {
331 if (encoder->crtc != crtc)
334 encoder_funcs = encoder->helper_private;
338 encoder_funcs = encoder->helper_private;
339 if (encoder_funcs->mode_fixup) {
340 if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
342 DRM_DEBUG_KMS("Encoder fixup failed\n");
348 if (crtc_funcs->mode_fixup) {
349 if (!(ret = crtc_funcs->mode_fixup(crtc, mode,
351 DRM_DEBUG_KMS("CRTC fixup failed\n");
355 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
357 drm_mode_copy(&crtc->hwmode, adjusted_mode);
359 /* Prepare the encoders and CRTCs before setting the mode. */
360 drm_for_each_encoder(encoder, dev) {
362 if (encoder->crtc != crtc)
365 encoder_funcs = encoder->helper_private;
369 /* Disable the encoders as the first thing we do. */
370 if (encoder_funcs->prepare)
371 encoder_funcs->prepare(encoder);
374 drm_crtc_prepare_encoders(dev);
376 crtc_funcs->prepare(crtc);
378 /* Set up the DPLL and any encoders state that needs to adjust or depend
381 ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
385 drm_for_each_encoder(encoder, dev) {
387 if (encoder->crtc != crtc)
390 encoder_funcs = encoder->helper_private;
394 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%s]\n",
395 encoder->base.id, encoder->name, mode->name);
396 if (encoder_funcs->mode_set)
397 encoder_funcs->mode_set(encoder, mode, adjusted_mode);
400 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
401 crtc_funcs->commit(crtc);
403 drm_for_each_encoder(encoder, dev) {
405 if (encoder->crtc != crtc)
408 encoder_funcs = encoder->helper_private;
412 if (encoder_funcs->commit)
413 encoder_funcs->commit(encoder);
416 /* Calculate and store various constants which
417 * are later needed by vblank and swap-completion
418 * timestamping. They are derived from true hwmode.
420 drm_calc_timestamping_constants(crtc, &crtc->hwmode);
422 /* FIXME: add subpixel order */
424 drm_mode_destroy(dev, adjusted_mode);
426 crtc->enabled = saved_enabled;
427 drm_mode_copy(&crtc->mode, &saved_mode);
428 drm_mode_copy(&crtc->hwmode, &saved_hwmode);
435 EXPORT_SYMBOL(drm_crtc_helper_set_mode);
438 drm_crtc_helper_disable(struct drm_crtc *crtc)
440 struct drm_device *dev = crtc->dev;
441 struct drm_connector *connector;
442 struct drm_encoder *encoder;
444 /* Decouple all encoders and their attached connectors from this crtc */
445 drm_for_each_encoder(encoder, dev) {
446 struct drm_connector_list_iter conn_iter;
448 if (encoder->crtc != crtc)
451 drm_connector_list_iter_begin(dev, &conn_iter);
452 drm_for_each_connector_iter(connector, &conn_iter) {
453 if (connector->encoder != encoder)
456 connector->encoder = NULL;
459 * drm_helper_disable_unused_functions() ought to be
460 * doing this, but since we've decoupled the encoder
461 * from the connector above, the required connection
462 * between them is henceforth no longer available.
464 connector->dpms = DRM_MODE_DPMS_OFF;
466 /* we keep a reference while the encoder is bound */
467 drm_connector_put(connector);
469 drm_connector_list_iter_end(&conn_iter);
472 __drm_helper_disable_unused_functions(dev);
476 * For connectors that support multiple encoders, either the
477 * .atomic_best_encoder() or .best_encoder() operation must be implemented.
480 drm_connector_get_single_encoder(struct drm_connector *connector)
482 struct drm_encoder *encoder;
484 WARN_ON(hweight32(connector->possible_encoders) > 1);
485 drm_connector_for_each_possible_encoder(connector, encoder)
492 * drm_crtc_helper_set_config - set a new config from userspace
493 * @set: mode set configuration
494 * @ctx: lock acquire context, not used here
496 * The drm_crtc_helper_set_config() helper function implements the of
497 * &drm_crtc_funcs.set_config callback for drivers using the legacy CRTC
500 * It first tries to locate the best encoder for each connector by calling the
501 * connector @drm_connector_helper_funcs.best_encoder helper operation.
503 * After locating the appropriate encoders, the helper function will call the
504 * mode_fixup encoder and CRTC helper operations to adjust the requested mode,
505 * or reject it completely in which case an error will be returned to the
506 * application. If the new configuration after mode adjustment is identical to
507 * the current configuration the helper function will return without performing
508 * any other operation.
510 * If the adjusted mode is identical to the current mode but changes to the
511 * frame buffer need to be applied, the drm_crtc_helper_set_config() function
512 * will call the CRTC &drm_crtc_helper_funcs.mode_set_base helper operation.
514 * If the adjusted mode differs from the current mode, or if the
515 * ->mode_set_base() helper operation is not provided, the helper function
516 * performs a full mode set sequence by calling the ->prepare(), ->mode_set()
517 * and ->commit() CRTC and encoder helper operations, in that order.
518 * Alternatively it can also use the dpms and disable helper operations. For
519 * details see &struct drm_crtc_helper_funcs and struct
520 * &drm_encoder_helper_funcs.
522 * This function is deprecated. New drivers must implement atomic modeset
523 * support, for which this function is unsuitable. Instead drivers should use
524 * drm_atomic_helper_set_config().
527 * Returns 0 on success, negative errno numbers on failure.
529 int drm_crtc_helper_set_config(struct drm_mode_set *set,
530 struct drm_modeset_acquire_ctx *ctx)
532 struct drm_device *dev;
533 struct drm_crtc **save_encoder_crtcs, *new_crtc;
534 struct drm_encoder **save_connector_encoders, *new_encoder, *encoder;
535 bool mode_changed = false; /* if true do a full mode set */
536 bool fb_changed = false; /* if true and !mode_changed just do a flip */
537 struct drm_connector *connector;
538 struct drm_connector_list_iter conn_iter;
539 int count = 0, ro, fail = 0;
540 const struct drm_crtc_helper_funcs *crtc_funcs;
541 struct drm_mode_set save_set;
549 BUG_ON(!set->crtc->helper_private);
551 /* Enforce sane interface api - has been abused by the fb helper. */
552 BUG_ON(!set->mode && set->fb);
553 BUG_ON(set->fb && set->num_connectors == 0);
555 crtc_funcs = set->crtc->helper_private;
557 dev = set->crtc->dev;
558 WARN_ON(drm_drv_uses_atomic_modeset(dev));
564 DRM_DEBUG_KMS("[CRTC:%d:%s] [FB:%d] #connectors=%d (x y) (%i %i)\n",
565 set->crtc->base.id, set->crtc->name,
567 (int)set->num_connectors, set->x, set->y);
569 DRM_DEBUG_KMS("[CRTC:%d:%s] [NOFB]\n",
570 set->crtc->base.id, set->crtc->name);
571 drm_crtc_helper_disable(set->crtc);
575 drm_warn_on_modeset_not_all_locked(dev);
578 * Allocate space for the backup of all (non-pointer) encoder and
581 save_encoder_crtcs = kcalloc(dev->mode_config.num_encoder,
582 sizeof(struct drm_crtc *), GFP_KERNEL);
583 if (!save_encoder_crtcs)
586 save_connector_encoders = kcalloc(dev->mode_config.num_connector,
587 sizeof(struct drm_encoder *), GFP_KERNEL);
588 if (!save_connector_encoders) {
589 kfree(save_encoder_crtcs);
594 * Copy data. Note that driver private data is not affected.
595 * Should anything bad happen only the expected state is
596 * restored, not the drivers personal bookkeeping.
599 drm_for_each_encoder(encoder, dev) {
600 save_encoder_crtcs[count++] = encoder->crtc;
604 drm_connector_list_iter_begin(dev, &conn_iter);
605 drm_for_each_connector_iter(connector, &conn_iter)
606 save_connector_encoders[count++] = connector->encoder;
607 drm_connector_list_iter_end(&conn_iter);
609 save_set.crtc = set->crtc;
610 save_set.mode = &set->crtc->mode;
611 save_set.x = set->crtc->x;
612 save_set.y = set->crtc->y;
613 save_set.fb = set->crtc->primary->fb;
615 /* We should be able to check here if the fb has the same properties
616 * and then just flip_or_move it */
617 if (set->crtc->primary->fb != set->fb) {
618 /* If we have no fb then treat it as a full mode set */
619 if (set->crtc->primary->fb == NULL) {
620 DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
622 } else if (set->fb->format != set->crtc->primary->fb->format) {
628 if (set->x != set->crtc->x || set->y != set->crtc->y)
631 if (!drm_mode_equal(set->mode, &set->crtc->mode)) {
632 DRM_DEBUG_KMS("modes are different, full mode set\n");
633 drm_mode_debug_printmodeline(&set->crtc->mode);
634 drm_mode_debug_printmodeline(set->mode);
638 /* take a reference on all unbound connectors in set, reuse the
639 * already taken reference for bound connectors
641 for (ro = 0; ro < set->num_connectors; ro++) {
642 if (set->connectors[ro]->encoder)
644 drm_connector_get(set->connectors[ro]);
647 /* a) traverse passed in connector list and get encoders for them */
649 drm_connector_list_iter_begin(dev, &conn_iter);
650 drm_for_each_connector_iter(connector, &conn_iter) {
651 const struct drm_connector_helper_funcs *connector_funcs =
652 connector->helper_private;
653 new_encoder = connector->encoder;
654 for (ro = 0; ro < set->num_connectors; ro++) {
655 if (set->connectors[ro] == connector) {
656 if (connector_funcs->best_encoder)
657 new_encoder = connector_funcs->best_encoder(connector);
659 new_encoder = drm_connector_get_single_encoder(connector);
661 /* if we can't get an encoder for a connector
662 we are setting now - then fail */
663 if (new_encoder == NULL)
664 /* don't break so fail path works correct */
667 if (connector->dpms != DRM_MODE_DPMS_ON) {
668 DRM_DEBUG_KMS("connector dpms not on, full mode switch\n");
676 if (new_encoder != connector->encoder) {
677 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
679 /* If the encoder is reused for another connector, then
680 * the appropriate crtc will be set later.
682 if (connector->encoder)
683 connector->encoder->crtc = NULL;
684 connector->encoder = new_encoder;
687 drm_connector_list_iter_end(&conn_iter);
695 drm_connector_list_iter_begin(dev, &conn_iter);
696 drm_for_each_connector_iter(connector, &conn_iter) {
697 if (!connector->encoder)
700 if (connector->encoder->crtc == set->crtc)
703 new_crtc = connector->encoder->crtc;
705 for (ro = 0; ro < set->num_connectors; ro++) {
706 if (set->connectors[ro] == connector)
707 new_crtc = set->crtc;
710 /* Make sure the new CRTC will work with the encoder */
712 !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
714 drm_connector_list_iter_end(&conn_iter);
717 if (new_crtc != connector->encoder->crtc) {
718 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
720 connector->encoder->crtc = new_crtc;
723 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d:%s]\n",
724 connector->base.id, connector->name,
725 new_crtc->base.id, new_crtc->name);
727 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
728 connector->base.id, connector->name);
731 drm_connector_list_iter_end(&conn_iter);
733 /* mode_set_base is not a required function */
734 if (fb_changed && !crtc_funcs->mode_set_base)
738 if (drm_helper_crtc_in_use(set->crtc)) {
739 DRM_DEBUG_KMS("attempting to set mode from"
741 drm_mode_debug_printmodeline(set->mode);
742 set->crtc->primary->fb = set->fb;
743 if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
746 DRM_ERROR("failed to set mode on [CRTC:%d:%s]\n",
747 set->crtc->base.id, set->crtc->name);
748 set->crtc->primary->fb = save_set.fb;
752 DRM_DEBUG_KMS("Setting connector DPMS state to on\n");
753 for (i = 0; i < set->num_connectors; i++) {
754 DRM_DEBUG_KMS("\t[CONNECTOR:%d:%s] set DPMS on\n", set->connectors[i]->base.id,
755 set->connectors[i]->name);
756 set->connectors[i]->funcs->dpms(set->connectors[i], DRM_MODE_DPMS_ON);
759 __drm_helper_disable_unused_functions(dev);
760 } else if (fb_changed) {
761 set->crtc->x = set->x;
762 set->crtc->y = set->y;
763 set->crtc->primary->fb = set->fb;
764 ret = crtc_funcs->mode_set_base(set->crtc,
765 set->x, set->y, save_set.fb);
767 set->crtc->x = save_set.x;
768 set->crtc->y = save_set.y;
769 set->crtc->primary->fb = save_set.fb;
774 kfree(save_connector_encoders);
775 kfree(save_encoder_crtcs);
779 /* Restore all previous data. */
781 drm_for_each_encoder(encoder, dev) {
782 encoder->crtc = save_encoder_crtcs[count++];
786 drm_connector_list_iter_begin(dev, &conn_iter);
787 drm_for_each_connector_iter(connector, &conn_iter)
788 connector->encoder = save_connector_encoders[count++];
789 drm_connector_list_iter_end(&conn_iter);
791 /* after fail drop reference on all unbound connectors in set, let
792 * bound connectors keep their reference
794 for (ro = 0; ro < set->num_connectors; ro++) {
795 if (set->connectors[ro]->encoder)
797 drm_connector_put(set->connectors[ro]);
800 /* Try to restore the config */
802 !drm_crtc_helper_set_mode(save_set.crtc, save_set.mode, save_set.x,
803 save_set.y, save_set.fb))
804 DRM_ERROR("failed to restore config after modeset failure\n");
806 kfree(save_connector_encoders);
807 kfree(save_encoder_crtcs);
810 EXPORT_SYMBOL(drm_crtc_helper_set_config);
812 static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder)
814 int dpms = DRM_MODE_DPMS_OFF;
815 struct drm_connector *connector;
816 struct drm_connector_list_iter conn_iter;
817 struct drm_device *dev = encoder->dev;
819 drm_connector_list_iter_begin(dev, &conn_iter);
820 drm_for_each_connector_iter(connector, &conn_iter)
821 if (connector->encoder == encoder)
822 if (connector->dpms < dpms)
823 dpms = connector->dpms;
824 drm_connector_list_iter_end(&conn_iter);
829 /* Helper which handles bridge ordering around encoder dpms */
830 static void drm_helper_encoder_dpms(struct drm_encoder *encoder, int mode)
832 const struct drm_encoder_helper_funcs *encoder_funcs;
834 encoder_funcs = encoder->helper_private;
838 if (encoder_funcs->dpms)
839 encoder_funcs->dpms(encoder, mode);
842 static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc)
844 int dpms = DRM_MODE_DPMS_OFF;
845 struct drm_connector *connector;
846 struct drm_connector_list_iter conn_iter;
847 struct drm_device *dev = crtc->dev;
849 drm_connector_list_iter_begin(dev, &conn_iter);
850 drm_for_each_connector_iter(connector, &conn_iter)
851 if (connector->encoder && connector->encoder->crtc == crtc)
852 if (connector->dpms < dpms)
853 dpms = connector->dpms;
854 drm_connector_list_iter_end(&conn_iter);
860 * drm_helper_connector_dpms() - connector dpms helper implementation
861 * @connector: affected connector
864 * The drm_helper_connector_dpms() helper function implements the
865 * &drm_connector_funcs.dpms callback for drivers using the legacy CRTC
868 * This is the main helper function provided by the CRTC helper framework for
869 * implementing the DPMS connector attribute. It computes the new desired DPMS
870 * state for all encoders and CRTCs in the output mesh and calls the
871 * &drm_crtc_helper_funcs.dpms and &drm_encoder_helper_funcs.dpms callbacks
872 * provided by the driver.
874 * This function is deprecated. New drivers must implement atomic modeset
875 * support, where DPMS is handled in the DRM core.
880 int drm_helper_connector_dpms(struct drm_connector *connector, int mode)
882 struct drm_encoder *encoder = connector->encoder;
883 struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
884 int old_dpms, encoder_dpms = DRM_MODE_DPMS_OFF;
886 WARN_ON(drm_drv_uses_atomic_modeset(connector->dev));
888 if (mode == connector->dpms)
891 old_dpms = connector->dpms;
892 connector->dpms = mode;
895 encoder_dpms = drm_helper_choose_encoder_dpms(encoder);
897 /* from off to on, do crtc then encoder */
898 if (mode < old_dpms) {
900 const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
902 if (crtc_funcs->dpms)
903 (*crtc_funcs->dpms) (crtc,
904 drm_helper_choose_crtc_dpms(crtc));
907 drm_helper_encoder_dpms(encoder, encoder_dpms);
910 /* from on to off, do encoder then crtc */
911 if (mode > old_dpms) {
913 drm_helper_encoder_dpms(encoder, encoder_dpms);
915 const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
917 if (crtc_funcs->dpms)
918 (*crtc_funcs->dpms) (crtc,
919 drm_helper_choose_crtc_dpms(crtc));
925 EXPORT_SYMBOL(drm_helper_connector_dpms);
928 * drm_helper_resume_force_mode - force-restore mode setting configuration
929 * @dev: drm_device which should be restored
931 * Drivers which use the mode setting helpers can use this function to
932 * force-restore the mode setting configuration e.g. on resume or when something
933 * else might have trampled over the hw state (like some overzealous old BIOSen
936 * This helper doesn't provide a error return value since restoring the old
937 * config should never fail due to resource allocation issues since the driver
938 * has successfully set the restored configuration already. Hence this should
939 * boil down to the equivalent of a few dpms on calls, which also don't provide
942 * Drivers where simply restoring an old configuration again might fail (e.g.
943 * due to slight differences in allocating shared resources when the
944 * configuration is restored in a different order than when userspace set it up)
945 * need to use their own restore logic.
947 * This function is deprecated. New drivers should implement atomic mode-
948 * setting and use the atomic suspend/resume helpers.
951 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
953 void drm_helper_resume_force_mode(struct drm_device *dev)
955 struct drm_crtc *crtc;
956 struct drm_encoder *encoder;
957 const struct drm_crtc_helper_funcs *crtc_funcs;
961 WARN_ON(drm_drv_uses_atomic_modeset(dev));
963 drm_modeset_lock_all(dev);
964 drm_for_each_crtc(crtc, dev) {
969 ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
970 crtc->x, crtc->y, crtc->primary->fb);
972 /* Restoring the old config should never fail! */
974 DRM_ERROR("failed to set mode on crtc %p\n", crtc);
976 /* Turn off outputs that were already powered off */
977 if (drm_helper_choose_crtc_dpms(crtc)) {
978 drm_for_each_encoder(encoder, dev) {
980 if(encoder->crtc != crtc)
983 encoder_dpms = drm_helper_choose_encoder_dpms(
986 drm_helper_encoder_dpms(encoder, encoder_dpms);
989 crtc_funcs = crtc->helper_private;
990 if (crtc_funcs->dpms)
991 (*crtc_funcs->dpms) (crtc,
992 drm_helper_choose_crtc_dpms(crtc));
996 /* disable the unused connectors while restoring the modesetting */
997 __drm_helper_disable_unused_functions(dev);
998 drm_modeset_unlock_all(dev);
1000 EXPORT_SYMBOL(drm_helper_resume_force_mode);
1003 * drm_helper_force_disable_all - Forcibly turn off all enabled CRTCs
1004 * @dev: DRM device whose CRTCs to turn off
1006 * Drivers may want to call this on unload to ensure that all displays are
1007 * unlit and the GPU is in a consistent, low power state. Takes modeset locks.
1009 * Note: This should only be used by non-atomic legacy drivers. For an atomic
1010 * version look at drm_atomic_helper_shutdown().
1013 * Zero on success, error code on failure.
1015 int drm_helper_force_disable_all(struct drm_device *dev)
1017 struct drm_crtc *crtc;
1020 drm_modeset_lock_all(dev);
1021 drm_for_each_crtc(crtc, dev)
1022 if (crtc->enabled) {
1023 struct drm_mode_set set = {
1027 ret = drm_mode_set_config_internal(&set);
1032 drm_modeset_unlock_all(dev);
1035 EXPORT_SYMBOL(drm_helper_force_disable_all);