2 * Copyright (c) 2006-2008 Intel Corporation
3 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
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
27 * Eric Anholt <eric@anholt.net>
28 * Dave Airlie <airlied@linux.ie>
29 * Jesse Barnes <jesse.barnes@intel.com>
32 #include <linux/kernel.h>
33 #include <linux/export.h>
34 #include <linux/moduleparam.h>
37 #include <drm/drm_atomic.h>
38 #include <drm/drm_crtc.h>
39 #include <drm/drm_fourcc.h>
40 #include <drm/drm_crtc_helper.h>
41 #include <drm/drm_fb_helper.h>
42 #include <drm/drm_plane_helper.h>
43 #include <drm/drm_atomic_helper.h>
44 #include <drm/drm_edid.h>
49 * The CRTC modeset helper library provides a default set_config implementation
50 * in drm_crtc_helper_set_config(). Plus a few other convenience functions using
51 * the same callbacks which drivers can use to e.g. restore the modeset
52 * configuration on resume with drm_helper_resume_force_mode().
54 * Note that this helper library doesn't track the current power state of CRTCs
55 * and encoders. It can call callbacks like ->dpms() even though the hardware is
56 * already in the desired state. This deficiency has been fixed in the atomic
59 * The driver callbacks are mostly compatible with the atomic modeset helpers,
60 * except for the handling of the primary plane: Atomic helpers require that the
61 * primary plane is implemented as a real standalone plane and not directly tied
62 * to the CRTC state. For easier transition this library provides functions to
63 * implement the old semantics required by the CRTC helpers using the new plane
64 * and atomic helper callbacks.
66 * Drivers are strongly urged to convert to the atomic helpers (by way of first
67 * converting to the plane helpers). New drivers must not use these functions
68 * but need to implement the atomic interface instead, potentially using the
69 * atomic helpers for that.
71 * These legacy modeset helpers use the same function table structures as
72 * all other modesetting helpers. See the documentation for struct
73 * &drm_crtc_helper_funcs, struct &drm_encoder_helper_funcs and struct
74 * &drm_connector_helper_funcs.
78 * drm_helper_encoder_in_use - check if a given encoder is in use
79 * @encoder: encoder to check
81 * Checks whether @encoder is with the current mode setting output configuration
82 * in use by any connector. This doesn't mean that it is actually enabled since
83 * the DPMS state is tracked separately.
86 * True if @encoder is used, false otherwise.
88 bool drm_helper_encoder_in_use(struct drm_encoder *encoder)
90 struct drm_connector *connector;
91 struct drm_device *dev = encoder->dev;
94 * We can expect this mutex to be locked if we are not panicking.
95 * Locking is currently fubar in the panic handler.
98 if (!oops_in_progress) {
99 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
100 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
104 drm_for_each_connector(connector, dev)
105 if (connector->encoder == encoder)
109 EXPORT_SYMBOL(drm_helper_encoder_in_use);
112 * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config
113 * @crtc: CRTC to check
115 * Checks whether @crtc is with the current mode setting output configuration
116 * in use by any connector. This doesn't mean that it is actually enabled since
117 * the DPMS state is tracked separately.
120 * True if @crtc is used, false otherwise.
122 bool drm_helper_crtc_in_use(struct drm_crtc *crtc)
124 struct drm_encoder *encoder;
125 struct drm_device *dev = crtc->dev;
128 * We can expect this mutex to be locked if we are not panicking.
129 * Locking is currently fubar in the panic handler.
132 if (!oops_in_progress)
134 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
136 drm_for_each_encoder(encoder, dev)
137 if (encoder->crtc == crtc && drm_helper_encoder_in_use(encoder))
141 EXPORT_SYMBOL(drm_helper_crtc_in_use);
144 drm_encoder_disable(struct drm_encoder *encoder)
146 const struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
151 drm_bridge_disable(encoder->bridge);
153 if (encoder_funcs->disable)
154 (*encoder_funcs->disable)(encoder);
155 else if (encoder_funcs->dpms)
156 (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
158 drm_bridge_post_disable(encoder->bridge);
161 static void __drm_helper_disable_unused_functions(struct drm_device *dev)
163 struct drm_encoder *encoder;
164 struct drm_crtc *crtc;
166 drm_warn_on_modeset_not_all_locked(dev);
168 drm_for_each_encoder(encoder, dev) {
169 if (!drm_helper_encoder_in_use(encoder)) {
170 drm_encoder_disable(encoder);
171 /* disconnect encoder from any connector */
172 encoder->crtc = NULL;
176 drm_for_each_crtc(crtc, dev) {
177 const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
178 crtc->enabled = drm_helper_crtc_in_use(crtc);
179 if (!crtc->enabled) {
180 if (crtc_funcs->disable)
181 (*crtc_funcs->disable)(crtc);
183 (*crtc_funcs->dpms)(crtc, DRM_MODE_DPMS_OFF);
184 crtc->primary->fb = NULL;
190 * drm_helper_disable_unused_functions - disable unused objects
193 * This function walks through the entire mode setting configuration of @dev. It
194 * will remove any CRTC links of unused encoders and encoder links of
195 * disconnected connectors. Then it will disable all unused encoders and CRTCs
196 * either by calling their disable callback if available or by calling their
197 * dpms callback with DRM_MODE_DPMS_OFF.
201 * This function is part of the legacy modeset helper library and will cause
202 * major confusion with atomic drivers. This is because atomic helpers guarantee
203 * to never call ->disable() hooks on a disabled function, or ->enable() hooks
204 * on an enabled functions. drm_helper_disable_unused_functions() on the other
205 * hand throws such guarantees into the wind and calls disable hooks
206 * unconditionally on unused functions.
208 void drm_helper_disable_unused_functions(struct drm_device *dev)
210 if (drm_core_check_feature(dev, DRIVER_ATOMIC))
211 DRM_ERROR("Called for atomic driver, this is not what you want.\n");
213 drm_modeset_lock_all(dev);
214 __drm_helper_disable_unused_functions(dev);
215 drm_modeset_unlock_all(dev);
217 EXPORT_SYMBOL(drm_helper_disable_unused_functions);
220 * Check the CRTC we're going to map each output to vs. its current
221 * CRTC. If they don't match, we have to disable the output and the CRTC
222 * since the driver will have to re-route things.
225 drm_crtc_prepare_encoders(struct drm_device *dev)
227 const struct drm_encoder_helper_funcs *encoder_funcs;
228 struct drm_encoder *encoder;
230 drm_for_each_encoder(encoder, dev) {
231 encoder_funcs = encoder->helper_private;
235 /* Disable unused encoders */
236 if (encoder->crtc == NULL)
237 drm_encoder_disable(encoder);
238 /* Disable encoders whose CRTC is about to change */
239 if (encoder_funcs->get_crtc &&
240 encoder->crtc != (*encoder_funcs->get_crtc)(encoder))
241 drm_encoder_disable(encoder);
246 * drm_crtc_helper_set_mode - internal helper to set a mode
247 * @crtc: CRTC to program
249 * @x: horizontal offset into the surface
250 * @y: vertical offset into the surface
251 * @old_fb: old framebuffer, for cleanup
253 * Try to set @mode on @crtc. Give @crtc and its associated connectors a chance
254 * to fixup or reject the mode prior to trying to set it. This is an internal
255 * helper that drivers could e.g. use to update properties that require the
256 * entire output pipe to be disabled and re-enabled in a new configuration. For
257 * example for changing whether audio is enabled on a hdmi link or for changing
258 * panel fitter or dither attributes. It is also called by the
259 * drm_crtc_helper_set_config() helper function to drive the mode setting
263 * True if the mode was set successfully, false otherwise.
265 bool drm_crtc_helper_set_mode(struct drm_crtc *crtc,
266 struct drm_display_mode *mode,
268 struct drm_framebuffer *old_fb)
270 struct drm_device *dev = crtc->dev;
271 struct drm_display_mode *adjusted_mode, saved_mode, saved_hwmode;
272 const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
273 const struct drm_encoder_helper_funcs *encoder_funcs;
274 int saved_x, saved_y;
276 struct drm_encoder *encoder;
279 drm_warn_on_modeset_not_all_locked(dev);
281 saved_enabled = crtc->enabled;
282 crtc->enabled = drm_helper_crtc_in_use(crtc);
286 adjusted_mode = drm_mode_duplicate(dev, mode);
287 if (!adjusted_mode) {
288 crtc->enabled = saved_enabled;
292 saved_mode = crtc->mode;
293 saved_hwmode = crtc->hwmode;
297 /* Update crtc values up front so the driver can rely on them for mode
304 /* Pass our mode to the connectors and the CRTC to give them a chance to
305 * adjust it according to limitations or connector properties, and also
306 * a chance to reject the mode entirely.
308 drm_for_each_encoder(encoder, dev) {
310 if (encoder->crtc != crtc)
313 encoder_funcs = encoder->helper_private;
317 ret = drm_bridge_mode_fixup(encoder->bridge,
318 mode, adjusted_mode);
320 DRM_DEBUG_KMS("Bridge fixup failed\n");
324 encoder_funcs = encoder->helper_private;
325 if (encoder_funcs->mode_fixup) {
326 if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
328 DRM_DEBUG_KMS("Encoder fixup failed\n");
334 if (crtc_funcs->mode_fixup) {
335 if (!(ret = crtc_funcs->mode_fixup(crtc, mode,
337 DRM_DEBUG_KMS("CRTC fixup failed\n");
341 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
343 crtc->hwmode = *adjusted_mode;
345 /* Prepare the encoders and CRTCs before setting the mode. */
346 drm_for_each_encoder(encoder, dev) {
348 if (encoder->crtc != crtc)
351 encoder_funcs = encoder->helper_private;
355 drm_bridge_disable(encoder->bridge);
357 /* Disable the encoders as the first thing we do. */
358 if (encoder_funcs->prepare)
359 encoder_funcs->prepare(encoder);
361 drm_bridge_post_disable(encoder->bridge);
364 drm_crtc_prepare_encoders(dev);
366 crtc_funcs->prepare(crtc);
368 /* Set up the DPLL and any encoders state that needs to adjust or depend
371 ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
375 drm_for_each_encoder(encoder, dev) {
377 if (encoder->crtc != crtc)
380 encoder_funcs = encoder->helper_private;
384 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
385 encoder->base.id, encoder->name,
386 mode->base.id, mode->name);
387 if (encoder_funcs->mode_set)
388 encoder_funcs->mode_set(encoder, mode, adjusted_mode);
390 drm_bridge_mode_set(encoder->bridge, mode, adjusted_mode);
393 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
394 crtc_funcs->commit(crtc);
396 drm_for_each_encoder(encoder, dev) {
398 if (encoder->crtc != crtc)
401 encoder_funcs = encoder->helper_private;
405 drm_bridge_pre_enable(encoder->bridge);
407 if (encoder_funcs->commit)
408 encoder_funcs->commit(encoder);
410 drm_bridge_enable(encoder->bridge);
413 /* Calculate and store various constants which
414 * are later needed by vblank and swap-completion
415 * timestamping. They are derived from true hwmode.
417 drm_calc_timestamping_constants(crtc, &crtc->hwmode);
419 /* FIXME: add subpixel order */
421 drm_mode_destroy(dev, adjusted_mode);
423 crtc->enabled = saved_enabled;
424 crtc->mode = saved_mode;
425 crtc->hwmode = saved_hwmode;
432 EXPORT_SYMBOL(drm_crtc_helper_set_mode);
435 drm_crtc_helper_disable(struct drm_crtc *crtc)
437 struct drm_device *dev = crtc->dev;
438 struct drm_connector *connector;
439 struct drm_encoder *encoder;
441 /* Decouple all encoders and their attached connectors from this crtc */
442 drm_for_each_encoder(encoder, dev) {
443 if (encoder->crtc != crtc)
446 drm_for_each_connector(connector, dev) {
447 if (connector->encoder != encoder)
450 connector->encoder = NULL;
453 * drm_helper_disable_unused_functions() ought to be
454 * doing this, but since we've decoupled the encoder
455 * from the connector above, the required connection
456 * between them is henceforth no longer available.
458 connector->dpms = DRM_MODE_DPMS_OFF;
460 /* we keep a reference while the encoder is bound */
461 drm_connector_unreference(connector);
465 __drm_helper_disable_unused_functions(dev);
469 * drm_crtc_helper_set_config - set a new config from userspace
470 * @set: mode set configuration
472 * The drm_crtc_helper_set_config() helper function implements the set_config
473 * callback of struct &drm_crtc_funcs for drivers using the legacy CRTC helpers.
475 * It first tries to locate the best encoder for each connector by calling the
476 * connector ->best_encoder() (struct &drm_connector_helper_funcs) helper
479 * After locating the appropriate encoders, the helper function will call the
480 * mode_fixup encoder and CRTC helper operations to adjust the requested mode,
481 * or reject it completely in which case an error will be returned to the
482 * application. If the new configuration after mode adjustment is identical to
483 * the current configuration the helper function will return without performing
484 * any other operation.
486 * If the adjusted mode is identical to the current mode but changes to the
487 * frame buffer need to be applied, the drm_crtc_helper_set_config() function
488 * will call the CRTC ->mode_set_base() (struct &drm_crtc_helper_funcs) helper
491 * If the adjusted mode differs from the current mode, or if the
492 * ->mode_set_base() helper operation is not provided, the helper function
493 * performs a full mode set sequence by calling the ->prepare(), ->mode_set()
494 * and ->commit() CRTC and encoder helper operations, in that order.
495 * Alternatively it can also use the dpms and disable helper operations. For
496 * details see struct &drm_crtc_helper_funcs and struct
497 * &drm_encoder_helper_funcs.
499 * This function is deprecated. New drivers must implement atomic modeset
500 * support, for which this function is unsuitable. Instead drivers should use
501 * drm_atomic_helper_set_config().
504 * Returns 0 on success, negative errno numbers on failure.
506 int drm_crtc_helper_set_config(struct drm_mode_set *set)
508 struct drm_device *dev;
509 struct drm_crtc **save_encoder_crtcs, *new_crtc;
510 struct drm_encoder **save_connector_encoders, *new_encoder, *encoder;
511 bool mode_changed = false; /* if true do a full mode set */
512 bool fb_changed = false; /* if true and !mode_changed just do a flip */
513 struct drm_connector *connector;
514 int count = 0, ro, fail = 0;
515 const struct drm_crtc_helper_funcs *crtc_funcs;
516 struct drm_mode_set save_set;
524 BUG_ON(!set->crtc->helper_private);
526 /* Enforce sane interface api - has been abused by the fb helper. */
527 BUG_ON(!set->mode && set->fb);
528 BUG_ON(set->fb && set->num_connectors == 0);
530 crtc_funcs = set->crtc->helper_private;
536 DRM_DEBUG_KMS("[CRTC:%d:%s] [FB:%d] #connectors=%d (x y) (%i %i)\n",
537 set->crtc->base.id, set->crtc->name,
539 (int)set->num_connectors, set->x, set->y);
541 DRM_DEBUG_KMS("[CRTC:%d:%s] [NOFB]\n",
542 set->crtc->base.id, set->crtc->name);
543 drm_crtc_helper_disable(set->crtc);
547 dev = set->crtc->dev;
549 drm_warn_on_modeset_not_all_locked(dev);
552 * Allocate space for the backup of all (non-pointer) encoder and
555 save_encoder_crtcs = kzalloc(dev->mode_config.num_encoder *
556 sizeof(struct drm_crtc *), GFP_KERNEL);
557 if (!save_encoder_crtcs)
560 save_connector_encoders = kzalloc(dev->mode_config.num_connector *
561 sizeof(struct drm_encoder *), GFP_KERNEL);
562 if (!save_connector_encoders) {
563 kfree(save_encoder_crtcs);
568 * Copy data. Note that driver private data is not affected.
569 * Should anything bad happen only the expected state is
570 * restored, not the drivers personal bookkeeping.
573 drm_for_each_encoder(encoder, dev) {
574 save_encoder_crtcs[count++] = encoder->crtc;
578 drm_for_each_connector(connector, dev) {
579 save_connector_encoders[count++] = connector->encoder;
582 save_set.crtc = set->crtc;
583 save_set.mode = &set->crtc->mode;
584 save_set.x = set->crtc->x;
585 save_set.y = set->crtc->y;
586 save_set.fb = set->crtc->primary->fb;
588 /* We should be able to check here if the fb has the same properties
589 * and then just flip_or_move it */
590 if (set->crtc->primary->fb != set->fb) {
591 /* If we have no fb then treat it as a full mode set */
592 if (set->crtc->primary->fb == NULL) {
593 DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
595 } else if (set->fb->pixel_format !=
596 set->crtc->primary->fb->pixel_format) {
602 if (set->x != set->crtc->x || set->y != set->crtc->y)
605 if (!drm_mode_equal(set->mode, &set->crtc->mode)) {
606 DRM_DEBUG_KMS("modes are different, full mode set\n");
607 drm_mode_debug_printmodeline(&set->crtc->mode);
608 drm_mode_debug_printmodeline(set->mode);
612 /* take a reference on all unbound connectors in set, reuse the
613 * already taken reference for bound connectors
615 for (ro = 0; ro < set->num_connectors; ro++) {
616 if (set->connectors[ro]->encoder)
618 drm_connector_reference(set->connectors[ro]);
621 /* a) traverse passed in connector list and get encoders for them */
623 drm_for_each_connector(connector, dev) {
624 const struct drm_connector_helper_funcs *connector_funcs =
625 connector->helper_private;
626 new_encoder = connector->encoder;
627 for (ro = 0; ro < set->num_connectors; ro++) {
628 if (set->connectors[ro] == connector) {
629 new_encoder = connector_funcs->best_encoder(connector);
630 /* if we can't get an encoder for a connector
631 we are setting now - then fail */
632 if (new_encoder == NULL)
633 /* don't break so fail path works correct */
636 if (connector->dpms != DRM_MODE_DPMS_ON) {
637 DRM_DEBUG_KMS("connector dpms not on, full mode switch\n");
645 if (new_encoder != connector->encoder) {
646 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
648 /* If the encoder is reused for another connector, then
649 * the appropriate crtc will be set later.
651 if (connector->encoder)
652 connector->encoder->crtc = NULL;
653 connector->encoder = new_encoder;
663 drm_for_each_connector(connector, dev) {
664 if (!connector->encoder)
667 if (connector->encoder->crtc == set->crtc)
670 new_crtc = connector->encoder->crtc;
672 for (ro = 0; ro < set->num_connectors; ro++) {
673 if (set->connectors[ro] == connector)
674 new_crtc = set->crtc;
677 /* Make sure the new CRTC will work with the encoder */
679 !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
683 if (new_crtc != connector->encoder->crtc) {
684 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
686 connector->encoder->crtc = new_crtc;
689 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d:%s]\n",
690 connector->base.id, connector->name,
691 new_crtc->base.id, new_crtc->name);
693 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
694 connector->base.id, connector->name);
698 /* mode_set_base is not a required function */
699 if (fb_changed && !crtc_funcs->mode_set_base)
703 if (drm_helper_crtc_in_use(set->crtc)) {
704 DRM_DEBUG_KMS("attempting to set mode from"
706 drm_mode_debug_printmodeline(set->mode);
707 set->crtc->primary->fb = set->fb;
708 if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
711 DRM_ERROR("failed to set mode on [CRTC:%d:%s]\n",
712 set->crtc->base.id, set->crtc->name);
713 set->crtc->primary->fb = save_set.fb;
717 DRM_DEBUG_KMS("Setting connector DPMS state to on\n");
718 for (i = 0; i < set->num_connectors; i++) {
719 DRM_DEBUG_KMS("\t[CONNECTOR:%d:%s] set DPMS on\n", set->connectors[i]->base.id,
720 set->connectors[i]->name);
721 set->connectors[i]->funcs->dpms(set->connectors[i], DRM_MODE_DPMS_ON);
724 __drm_helper_disable_unused_functions(dev);
725 } else if (fb_changed) {
726 set->crtc->x = set->x;
727 set->crtc->y = set->y;
728 set->crtc->primary->fb = set->fb;
729 ret = crtc_funcs->mode_set_base(set->crtc,
730 set->x, set->y, save_set.fb);
732 set->crtc->x = save_set.x;
733 set->crtc->y = save_set.y;
734 set->crtc->primary->fb = save_set.fb;
739 kfree(save_connector_encoders);
740 kfree(save_encoder_crtcs);
744 /* Restore all previous data. */
746 drm_for_each_encoder(encoder, dev) {
747 encoder->crtc = save_encoder_crtcs[count++];
751 drm_for_each_connector(connector, dev) {
752 connector->encoder = save_connector_encoders[count++];
755 /* after fail drop reference on all unbound connectors in set, let
756 * bound connectors keep their reference
758 for (ro = 0; ro < set->num_connectors; ro++) {
759 if (set->connectors[ro]->encoder)
761 drm_connector_unreference(set->connectors[ro]);
764 /* Try to restore the config */
766 !drm_crtc_helper_set_mode(save_set.crtc, save_set.mode, save_set.x,
767 save_set.y, save_set.fb))
768 DRM_ERROR("failed to restore config after modeset failure\n");
770 kfree(save_connector_encoders);
771 kfree(save_encoder_crtcs);
774 EXPORT_SYMBOL(drm_crtc_helper_set_config);
776 static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder)
778 int dpms = DRM_MODE_DPMS_OFF;
779 struct drm_connector *connector;
780 struct drm_device *dev = encoder->dev;
782 drm_for_each_connector(connector, dev)
783 if (connector->encoder == encoder)
784 if (connector->dpms < dpms)
785 dpms = connector->dpms;
789 /* Helper which handles bridge ordering around encoder dpms */
790 static void drm_helper_encoder_dpms(struct drm_encoder *encoder, int mode)
792 struct drm_bridge *bridge = encoder->bridge;
793 const struct drm_encoder_helper_funcs *encoder_funcs;
795 encoder_funcs = encoder->helper_private;
799 if (mode == DRM_MODE_DPMS_ON)
800 drm_bridge_pre_enable(bridge);
802 drm_bridge_disable(bridge);
804 if (encoder_funcs->dpms)
805 encoder_funcs->dpms(encoder, mode);
807 if (mode == DRM_MODE_DPMS_ON)
808 drm_bridge_enable(bridge);
810 drm_bridge_post_disable(bridge);
813 static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc)
815 int dpms = DRM_MODE_DPMS_OFF;
816 struct drm_connector *connector;
817 struct drm_device *dev = crtc->dev;
819 drm_for_each_connector(connector, dev)
820 if (connector->encoder && connector->encoder->crtc == crtc)
821 if (connector->dpms < dpms)
822 dpms = connector->dpms;
827 * drm_helper_connector_dpms() - connector dpms helper implementation
828 * @connector: affected connector
831 * The drm_helper_connector_dpms() helper function implements the ->dpms()
832 * callback of struct &drm_connector_funcs for drivers using the legacy CRTC helpers.
834 * This is the main helper function provided by the CRTC helper framework for
835 * implementing the DPMS connector attribute. It computes the new desired DPMS
836 * state for all encoders and CRTCs in the output mesh and calls the ->dpms()
837 * callbacks provided by the driver in struct &drm_crtc_helper_funcs and struct
838 * &drm_encoder_helper_funcs appropriately.
840 * This function is deprecated. New drivers must implement atomic modeset
841 * support, for which this function is unsuitable. Instead drivers should use
842 * drm_atomic_helper_connector_dpms().
847 int drm_helper_connector_dpms(struct drm_connector *connector, int mode)
849 struct drm_encoder *encoder = connector->encoder;
850 struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
851 int old_dpms, encoder_dpms = DRM_MODE_DPMS_OFF;
853 if (mode == connector->dpms)
856 old_dpms = connector->dpms;
857 connector->dpms = mode;
860 encoder_dpms = drm_helper_choose_encoder_dpms(encoder);
862 /* from off to on, do crtc then encoder */
863 if (mode < old_dpms) {
865 const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
866 if (crtc_funcs->dpms)
867 (*crtc_funcs->dpms) (crtc,
868 drm_helper_choose_crtc_dpms(crtc));
871 drm_helper_encoder_dpms(encoder, encoder_dpms);
874 /* from on to off, do encoder then crtc */
875 if (mode > old_dpms) {
877 drm_helper_encoder_dpms(encoder, encoder_dpms);
879 const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
880 if (crtc_funcs->dpms)
881 (*crtc_funcs->dpms) (crtc,
882 drm_helper_choose_crtc_dpms(crtc));
888 EXPORT_SYMBOL(drm_helper_connector_dpms);
891 * drm_helper_resume_force_mode - force-restore mode setting configuration
892 * @dev: drm_device which should be restored
894 * Drivers which use the mode setting helpers can use this function to
895 * force-restore the mode setting configuration e.g. on resume or when something
896 * else might have trampled over the hw state (like some overzealous old BIOSen
899 * This helper doesn't provide a error return value since restoring the old
900 * config should never fail due to resource allocation issues since the driver
901 * has successfully set the restored configuration already. Hence this should
902 * boil down to the equivalent of a few dpms on calls, which also don't provide
905 * Drivers where simply restoring an old configuration again might fail (e.g.
906 * due to slight differences in allocating shared resources when the
907 * configuration is restored in a different order than when userspace set it up)
908 * need to use their own restore logic.
910 * This function is deprecated. New drivers should implement atomic mode-
911 * setting and use the atomic suspend/resume helpers.
914 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
916 void drm_helper_resume_force_mode(struct drm_device *dev)
918 struct drm_crtc *crtc;
919 struct drm_encoder *encoder;
920 const struct drm_crtc_helper_funcs *crtc_funcs;
924 drm_modeset_lock_all(dev);
925 drm_for_each_crtc(crtc, dev) {
930 ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
931 crtc->x, crtc->y, crtc->primary->fb);
933 /* Restoring the old config should never fail! */
935 DRM_ERROR("failed to set mode on crtc %p\n", crtc);
937 /* Turn off outputs that were already powered off */
938 if (drm_helper_choose_crtc_dpms(crtc)) {
939 drm_for_each_encoder(encoder, dev) {
941 if(encoder->crtc != crtc)
944 encoder_dpms = drm_helper_choose_encoder_dpms(
947 drm_helper_encoder_dpms(encoder, encoder_dpms);
950 crtc_funcs = crtc->helper_private;
951 if (crtc_funcs->dpms)
952 (*crtc_funcs->dpms) (crtc,
953 drm_helper_choose_crtc_dpms(crtc));
957 /* disable the unused connectors while restoring the modesetting */
958 __drm_helper_disable_unused_functions(dev);
959 drm_modeset_unlock_all(dev);
961 EXPORT_SYMBOL(drm_helper_resume_force_mode);
964 * drm_helper_crtc_mode_set - mode_set implementation for atomic plane helpers
966 * @mode: DRM display mode which userspace requested
967 * @adjusted_mode: DRM display mode adjusted by ->mode_fixup callbacks
968 * @x: x offset of the CRTC scanout area on the underlying framebuffer
969 * @y: y offset of the CRTC scanout area on the underlying framebuffer
970 * @old_fb: previous framebuffer
972 * This function implements a callback useable as the ->mode_set callback
973 * required by the CRTC helpers. Besides the atomic plane helper functions for
974 * the primary plane the driver must also provide the ->mode_set_nofb callback
975 * to set up the CRTC.
977 * This is a transitional helper useful for converting drivers to the atomic
980 int drm_helper_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode,
981 struct drm_display_mode *adjusted_mode, int x, int y,
982 struct drm_framebuffer *old_fb)
984 struct drm_crtc_state *crtc_state;
985 const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
988 if (crtc->funcs->atomic_duplicate_state)
989 crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
992 drm_atomic_helper_crtc_reset(crtc);
994 crtc_state = drm_atomic_helper_crtc_duplicate_state(crtc);
1000 crtc_state->planes_changed = true;
1001 crtc_state->mode_changed = true;
1002 ret = drm_atomic_set_mode_for_crtc(crtc_state, mode);
1005 drm_mode_copy(&crtc_state->adjusted_mode, adjusted_mode);
1007 if (crtc_funcs->atomic_check) {
1008 ret = crtc_funcs->atomic_check(crtc, crtc_state);
1013 swap(crtc->state, crtc_state);
1015 crtc_funcs->mode_set_nofb(crtc);
1017 ret = drm_helper_crtc_mode_set_base(crtc, x, y, old_fb);
1021 if (crtc->funcs->atomic_destroy_state)
1022 crtc->funcs->atomic_destroy_state(crtc, crtc_state);
1024 drm_atomic_helper_crtc_destroy_state(crtc, crtc_state);
1029 EXPORT_SYMBOL(drm_helper_crtc_mode_set);
1032 * drm_helper_crtc_mode_set_base - mode_set_base implementation for atomic plane helpers
1034 * @x: x offset of the CRTC scanout area on the underlying framebuffer
1035 * @y: y offset of the CRTC scanout area on the underlying framebuffer
1036 * @old_fb: previous framebuffer
1038 * This function implements a callback useable as the ->mode_set_base used
1039 * required by the CRTC helpers. The driver must provide the atomic plane helper
1040 * functions for the primary plane.
1042 * This is a transitional helper useful for converting drivers to the atomic
1045 int drm_helper_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
1046 struct drm_framebuffer *old_fb)
1048 struct drm_plane_state *plane_state;
1049 struct drm_plane *plane = crtc->primary;
1051 if (plane->funcs->atomic_duplicate_state)
1052 plane_state = plane->funcs->atomic_duplicate_state(plane);
1055 drm_atomic_helper_plane_reset(plane);
1057 plane_state = drm_atomic_helper_plane_duplicate_state(plane);
1061 plane_state->plane = plane;
1063 plane_state->crtc = crtc;
1064 drm_atomic_set_fb_for_plane(plane_state, crtc->primary->fb);
1065 plane_state->crtc_x = 0;
1066 plane_state->crtc_y = 0;
1067 plane_state->crtc_h = crtc->mode.vdisplay;
1068 plane_state->crtc_w = crtc->mode.hdisplay;
1069 plane_state->src_x = x << 16;
1070 plane_state->src_y = y << 16;
1071 plane_state->src_h = crtc->mode.vdisplay << 16;
1072 plane_state->src_w = crtc->mode.hdisplay << 16;
1074 return drm_plane_helper_commit(plane, plane_state, old_fb);
1076 EXPORT_SYMBOL(drm_helper_crtc_mode_set_base);