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/export.h>
33 #include <linux/moduleparam.h>
36 #include <drm/drm_atomic.h>
37 #include <drm/drm_crtc.h>
38 #include <drm/drm_crtc_helper.h>
39 #include <drm/drm_fb_helper.h>
40 #include <drm/drm_plane_helper.h>
41 #include <drm/drm_atomic_helper.h>
42 #include <drm/drm_edid.h>
47 * The CRTC modeset helper library provides a default set_config implementation
48 * in drm_crtc_helper_set_config(). Plus a few other convenience functions using
49 * the same callbacks which drivers can use to e.g. restore the modeset
50 * configuration on resume with drm_helper_resume_force_mode().
52 * The driver callbacks are mostly compatible with the atomic modeset helpers,
53 * except for the handling of the primary plane: Atomic helpers require that the
54 * primary plane is implemented as a real standalone plane and not directly tied
55 * to the CRTC state. For easier transition this library provides functions to
56 * implement the old semantics required by the CRTC helpers using the new plane
57 * and atomic helper callbacks.
59 * Drivers are strongly urged to convert to the atomic helpers (by way of first
60 * converting to the plane helpers). New drivers must not use these functions
61 * but need to implement the atomic interface instead, potentially using the
62 * atomic helpers for that.
64 MODULE_AUTHOR("David Airlie, Jesse Barnes");
65 MODULE_DESCRIPTION("DRM KMS helper");
68 * drm_helper_move_panel_connectors_to_head() - move panels to the front in the
70 * @dev: drm device to operate on
72 * Some userspace presumes that the first connected connector is the main
73 * display, where it's supposed to display e.g. the login screen. For
74 * laptops, this should be the main panel. Use this function to sort all
75 * (eDP/LVDS) panels to the front of the connector list, instead of
76 * painstakingly trying to initialize them in the right order.
78 void drm_helper_move_panel_connectors_to_head(struct drm_device *dev)
80 struct drm_connector *connector, *tmp;
81 struct list_head panel_list;
83 INIT_LIST_HEAD(&panel_list);
85 list_for_each_entry_safe(connector, tmp,
86 &dev->mode_config.connector_list, head) {
87 if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS ||
88 connector->connector_type == DRM_MODE_CONNECTOR_eDP)
89 list_move_tail(&connector->head, &panel_list);
92 list_splice(&panel_list, &dev->mode_config.connector_list);
94 EXPORT_SYMBOL(drm_helper_move_panel_connectors_to_head);
97 * drm_helper_encoder_in_use - check if a given encoder is in use
98 * @encoder: encoder to check
100 * Checks whether @encoder is with the current mode setting output configuration
101 * in use by any connector. This doesn't mean that it is actually enabled since
102 * the DPMS state is tracked separately.
105 * True if @encoder is used, false otherwise.
107 bool drm_helper_encoder_in_use(struct drm_encoder *encoder)
109 struct drm_connector *connector;
110 struct drm_device *dev = encoder->dev;
112 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
113 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
114 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
115 if (connector->encoder == encoder)
119 EXPORT_SYMBOL(drm_helper_encoder_in_use);
122 * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config
123 * @crtc: CRTC to check
125 * Checks whether @crtc is with the current mode setting output configuration
126 * in use by any connector. This doesn't mean that it is actually enabled since
127 * the DPMS state is tracked separately.
130 * True if @crtc is used, false otherwise.
132 bool drm_helper_crtc_in_use(struct drm_crtc *crtc)
134 struct drm_encoder *encoder;
135 struct drm_device *dev = crtc->dev;
137 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
138 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
139 if (encoder->crtc == crtc && drm_helper_encoder_in_use(encoder))
143 EXPORT_SYMBOL(drm_helper_crtc_in_use);
146 drm_encoder_disable(struct drm_encoder *encoder)
148 const struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
150 drm_bridge_disable(encoder->bridge);
152 if (encoder_funcs->disable)
153 (*encoder_funcs->disable)(encoder);
155 (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
157 drm_bridge_post_disable(encoder->bridge);
160 static void __drm_helper_disable_unused_functions(struct drm_device *dev)
162 struct drm_encoder *encoder;
163 struct drm_crtc *crtc;
165 drm_warn_on_modeset_not_all_locked(dev);
167 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
168 if (!drm_helper_encoder_in_use(encoder)) {
169 drm_encoder_disable(encoder);
170 /* disconnect encoder from any connector */
171 encoder->crtc = NULL;
175 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
176 const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
177 crtc->enabled = drm_helper_crtc_in_use(crtc);
178 if (!crtc->enabled) {
179 if (crtc_funcs->disable)
180 (*crtc_funcs->disable)(crtc);
182 (*crtc_funcs->dpms)(crtc, DRM_MODE_DPMS_OFF);
183 crtc->primary->fb = NULL;
189 * drm_helper_disable_unused_functions - disable unused objects
192 * This function walks through the entire mode setting configuration of @dev. It
193 * will remove any crtc links of unused encoders and encoder links of
194 * disconnected connectors. Then it will disable all unused encoders and crtcs
195 * either by calling their disable callback if available or by calling their
196 * dpms callback with DRM_MODE_DPMS_OFF.
198 void drm_helper_disable_unused_functions(struct drm_device *dev)
200 drm_modeset_lock_all(dev);
201 __drm_helper_disable_unused_functions(dev);
202 drm_modeset_unlock_all(dev);
204 EXPORT_SYMBOL(drm_helper_disable_unused_functions);
207 * Check the CRTC we're going to map each output to vs. its current
208 * CRTC. If they don't match, we have to disable the output and the CRTC
209 * since the driver will have to re-route things.
212 drm_crtc_prepare_encoders(struct drm_device *dev)
214 const struct drm_encoder_helper_funcs *encoder_funcs;
215 struct drm_encoder *encoder;
217 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
218 encoder_funcs = encoder->helper_private;
219 /* Disable unused encoders */
220 if (encoder->crtc == NULL)
221 drm_encoder_disable(encoder);
222 /* Disable encoders whose CRTC is about to change */
223 if (encoder_funcs->get_crtc &&
224 encoder->crtc != (*encoder_funcs->get_crtc)(encoder))
225 drm_encoder_disable(encoder);
230 * drm_crtc_helper_set_mode - internal helper to set a mode
231 * @crtc: CRTC to program
233 * @x: horizontal offset into the surface
234 * @y: vertical offset into the surface
235 * @old_fb: old framebuffer, for cleanup
237 * Try to set @mode on @crtc. Give @crtc and its associated connectors a chance
238 * to fixup or reject the mode prior to trying to set it. This is an internal
239 * helper that drivers could e.g. use to update properties that require the
240 * entire output pipe to be disabled and re-enabled in a new configuration. For
241 * example for changing whether audio is enabled on a hdmi link or for changing
242 * panel fitter or dither attributes. It is also called by the
243 * drm_crtc_helper_set_config() helper function to drive the mode setting
247 * True if the mode was set successfully, false otherwise.
249 bool drm_crtc_helper_set_mode(struct drm_crtc *crtc,
250 struct drm_display_mode *mode,
252 struct drm_framebuffer *old_fb)
254 struct drm_device *dev = crtc->dev;
255 struct drm_display_mode *adjusted_mode, saved_mode, saved_hwmode;
256 const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
257 const struct drm_encoder_helper_funcs *encoder_funcs;
258 int saved_x, saved_y;
260 struct drm_encoder *encoder;
263 drm_warn_on_modeset_not_all_locked(dev);
265 saved_enabled = crtc->enabled;
266 crtc->enabled = drm_helper_crtc_in_use(crtc);
270 adjusted_mode = drm_mode_duplicate(dev, mode);
271 if (!adjusted_mode) {
272 crtc->enabled = saved_enabled;
276 saved_mode = crtc->mode;
277 saved_hwmode = crtc->hwmode;
281 /* Update crtc values up front so the driver can rely on them for mode
288 /* Pass our mode to the connectors and the CRTC to give them a chance to
289 * adjust it according to limitations or connector properties, and also
290 * a chance to reject the mode entirely.
292 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
294 if (encoder->crtc != crtc)
297 ret = drm_bridge_mode_fixup(encoder->bridge,
298 mode, adjusted_mode);
300 DRM_DEBUG_KMS("Bridge fixup failed\n");
304 encoder_funcs = encoder->helper_private;
305 if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
307 DRM_DEBUG_KMS("Encoder fixup failed\n");
312 if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) {
313 DRM_DEBUG_KMS("CRTC fixup failed\n");
316 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
318 crtc->hwmode = *adjusted_mode;
320 /* Prepare the encoders and CRTCs before setting the mode. */
321 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
323 if (encoder->crtc != crtc)
326 drm_bridge_disable(encoder->bridge);
328 encoder_funcs = encoder->helper_private;
329 /* Disable the encoders as the first thing we do. */
330 encoder_funcs->prepare(encoder);
332 drm_bridge_post_disable(encoder->bridge);
335 drm_crtc_prepare_encoders(dev);
337 crtc_funcs->prepare(crtc);
339 /* Set up the DPLL and any encoders state that needs to adjust or depend
342 ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
346 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
348 if (encoder->crtc != crtc)
351 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
352 encoder->base.id, encoder->name,
353 mode->base.id, mode->name);
354 encoder_funcs = encoder->helper_private;
355 encoder_funcs->mode_set(encoder, mode, adjusted_mode);
357 drm_bridge_mode_set(encoder->bridge, mode, adjusted_mode);
360 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
361 crtc_funcs->commit(crtc);
363 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
365 if (encoder->crtc != crtc)
368 drm_bridge_pre_enable(encoder->bridge);
370 encoder_funcs = encoder->helper_private;
371 encoder_funcs->commit(encoder);
373 drm_bridge_enable(encoder->bridge);
376 /* Calculate and store various constants which
377 * are later needed by vblank and swap-completion
378 * timestamping. They are derived from true hwmode.
380 drm_calc_timestamping_constants(crtc, &crtc->hwmode);
382 /* FIXME: add subpixel order */
384 drm_mode_destroy(dev, adjusted_mode);
386 crtc->enabled = saved_enabled;
387 crtc->mode = saved_mode;
388 crtc->hwmode = saved_hwmode;
395 EXPORT_SYMBOL(drm_crtc_helper_set_mode);
398 drm_crtc_helper_disable(struct drm_crtc *crtc)
400 struct drm_device *dev = crtc->dev;
401 struct drm_connector *connector;
402 struct drm_encoder *encoder;
404 /* Decouple all encoders and their attached connectors from this crtc */
405 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
406 if (encoder->crtc != crtc)
409 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
410 if (connector->encoder != encoder)
413 connector->encoder = NULL;
416 * drm_helper_disable_unused_functions() ought to be
417 * doing this, but since we've decoupled the encoder
418 * from the connector above, the required connection
419 * between them is henceforth no longer available.
421 connector->dpms = DRM_MODE_DPMS_OFF;
425 __drm_helper_disable_unused_functions(dev);
429 * drm_crtc_helper_set_config - set a new config from userspace
430 * @set: mode set configuration
432 * Setup a new configuration, provided by the upper layers (either an ioctl call
433 * from userspace or internally e.g. from the fbdev support code) in @set, and
434 * enable it. This is the main helper functions for drivers that implement
435 * kernel mode setting with the crtc helper functions and the assorted
436 * ->prepare(), ->modeset() and ->commit() helper callbacks.
439 * Returns 0 on success, negative errno numbers on failure.
441 int drm_crtc_helper_set_config(struct drm_mode_set *set)
443 struct drm_device *dev;
444 struct drm_crtc *new_crtc;
445 struct drm_encoder *save_encoders, *new_encoder, *encoder;
446 bool mode_changed = false; /* if true do a full mode set */
447 bool fb_changed = false; /* if true and !mode_changed just do a flip */
448 struct drm_connector *save_connectors, *connector;
449 int count = 0, ro, fail = 0;
450 const struct drm_crtc_helper_funcs *crtc_funcs;
451 struct drm_mode_set save_set;
459 BUG_ON(!set->crtc->helper_private);
461 /* Enforce sane interface api - has been abused by the fb helper. */
462 BUG_ON(!set->mode && set->fb);
463 BUG_ON(set->fb && set->num_connectors == 0);
465 crtc_funcs = set->crtc->helper_private;
471 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
472 set->crtc->base.id, set->fb->base.id,
473 (int)set->num_connectors, set->x, set->y);
475 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
476 drm_crtc_helper_disable(set->crtc);
480 dev = set->crtc->dev;
482 drm_warn_on_modeset_not_all_locked(dev);
485 * Allocate space for the backup of all (non-pointer) encoder and
488 save_encoders = kzalloc(dev->mode_config.num_encoder *
489 sizeof(struct drm_encoder), GFP_KERNEL);
493 save_connectors = kzalloc(dev->mode_config.num_connector *
494 sizeof(struct drm_connector), GFP_KERNEL);
495 if (!save_connectors) {
496 kfree(save_encoders);
501 * Copy data. Note that driver private data is not affected.
502 * Should anything bad happen only the expected state is
503 * restored, not the drivers personal bookkeeping.
506 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
507 save_encoders[count++] = *encoder;
511 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
512 save_connectors[count++] = *connector;
515 save_set.crtc = set->crtc;
516 save_set.mode = &set->crtc->mode;
517 save_set.x = set->crtc->x;
518 save_set.y = set->crtc->y;
519 save_set.fb = set->crtc->primary->fb;
521 /* We should be able to check here if the fb has the same properties
522 * and then just flip_or_move it */
523 if (set->crtc->primary->fb != set->fb) {
524 /* If we have no fb then treat it as a full mode set */
525 if (set->crtc->primary->fb == NULL) {
526 DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
528 } else if (set->fb == NULL) {
530 } else if (set->fb->pixel_format !=
531 set->crtc->primary->fb->pixel_format) {
537 if (set->x != set->crtc->x || set->y != set->crtc->y)
540 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
541 DRM_DEBUG_KMS("modes are different, full mode set\n");
542 drm_mode_debug_printmodeline(&set->crtc->mode);
543 drm_mode_debug_printmodeline(set->mode);
547 /* a) traverse passed in connector list and get encoders for them */
549 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
550 const struct drm_connector_helper_funcs *connector_funcs =
551 connector->helper_private;
552 new_encoder = connector->encoder;
553 for (ro = 0; ro < set->num_connectors; ro++) {
554 if (set->connectors[ro] == connector) {
555 new_encoder = connector_funcs->best_encoder(connector);
556 /* if we can't get an encoder for a connector
557 we are setting now - then fail */
558 if (new_encoder == NULL)
559 /* don't break so fail path works correct */
562 if (connector->dpms != DRM_MODE_DPMS_ON) {
563 DRM_DEBUG_KMS("connector dpms not on, full mode switch\n");
571 if (new_encoder != connector->encoder) {
572 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
574 /* If the encoder is reused for another connector, then
575 * the appropriate crtc will be set later.
577 if (connector->encoder)
578 connector->encoder->crtc = NULL;
579 connector->encoder = new_encoder;
589 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
590 if (!connector->encoder)
593 if (connector->encoder->crtc == set->crtc)
596 new_crtc = connector->encoder->crtc;
598 for (ro = 0; ro < set->num_connectors; ro++) {
599 if (set->connectors[ro] == connector)
600 new_crtc = set->crtc;
603 /* Make sure the new CRTC will work with the encoder */
605 !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
609 if (new_crtc != connector->encoder->crtc) {
610 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
612 connector->encoder->crtc = new_crtc;
615 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
616 connector->base.id, connector->name,
619 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
620 connector->base.id, connector->name);
624 /* mode_set_base is not a required function */
625 if (fb_changed && !crtc_funcs->mode_set_base)
629 if (drm_helper_crtc_in_use(set->crtc)) {
630 DRM_DEBUG_KMS("attempting to set mode from"
632 drm_mode_debug_printmodeline(set->mode);
633 set->crtc->primary->fb = set->fb;
634 if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
637 DRM_ERROR("failed to set mode on [CRTC:%d]\n",
639 set->crtc->primary->fb = save_set.fb;
643 DRM_DEBUG_KMS("Setting connector DPMS state to on\n");
644 for (i = 0; i < set->num_connectors; i++) {
645 DRM_DEBUG_KMS("\t[CONNECTOR:%d:%s] set DPMS on\n", set->connectors[i]->base.id,
646 set->connectors[i]->name);
647 set->connectors[i]->funcs->dpms(set->connectors[i], DRM_MODE_DPMS_ON);
650 __drm_helper_disable_unused_functions(dev);
651 } else if (fb_changed) {
652 set->crtc->x = set->x;
653 set->crtc->y = set->y;
654 set->crtc->primary->fb = set->fb;
655 ret = crtc_funcs->mode_set_base(set->crtc,
656 set->x, set->y, save_set.fb);
658 set->crtc->x = save_set.x;
659 set->crtc->y = save_set.y;
660 set->crtc->primary->fb = save_set.fb;
665 kfree(save_connectors);
666 kfree(save_encoders);
670 /* Restore all previous data. */
672 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
673 *encoder = save_encoders[count++];
677 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
678 *connector = save_connectors[count++];
681 /* Try to restore the config */
683 !drm_crtc_helper_set_mode(save_set.crtc, save_set.mode, save_set.x,
684 save_set.y, save_set.fb))
685 DRM_ERROR("failed to restore config after modeset failure\n");
687 kfree(save_connectors);
688 kfree(save_encoders);
691 EXPORT_SYMBOL(drm_crtc_helper_set_config);
693 static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder)
695 int dpms = DRM_MODE_DPMS_OFF;
696 struct drm_connector *connector;
697 struct drm_device *dev = encoder->dev;
699 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
700 if (connector->encoder == encoder)
701 if (connector->dpms < dpms)
702 dpms = connector->dpms;
706 /* Helper which handles bridge ordering around encoder dpms */
707 static void drm_helper_encoder_dpms(struct drm_encoder *encoder, int mode)
709 struct drm_bridge *bridge = encoder->bridge;
710 const struct drm_encoder_helper_funcs *encoder_funcs;
712 if (mode == DRM_MODE_DPMS_ON)
713 drm_bridge_pre_enable(bridge);
715 drm_bridge_disable(bridge);
717 encoder_funcs = encoder->helper_private;
718 if (encoder_funcs->dpms)
719 encoder_funcs->dpms(encoder, mode);
721 if (mode == DRM_MODE_DPMS_ON)
722 drm_bridge_enable(bridge);
724 drm_bridge_post_disable(bridge);
727 static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc)
729 int dpms = DRM_MODE_DPMS_OFF;
730 struct drm_connector *connector;
731 struct drm_device *dev = crtc->dev;
733 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
734 if (connector->encoder && connector->encoder->crtc == crtc)
735 if (connector->dpms < dpms)
736 dpms = connector->dpms;
741 * drm_helper_connector_dpms() - connector dpms helper implementation
742 * @connector: affected connector
745 * This is the main helper function provided by the crtc helper framework for
746 * implementing the DPMS connector attribute. It computes the new desired DPMS
747 * state for all encoders and crtcs in the output mesh and calls the ->dpms()
748 * callback provided by the driver appropriately.
750 void drm_helper_connector_dpms(struct drm_connector *connector, int mode)
752 struct drm_encoder *encoder = connector->encoder;
753 struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
754 int old_dpms, encoder_dpms = DRM_MODE_DPMS_OFF;
756 if (mode == connector->dpms)
759 old_dpms = connector->dpms;
760 connector->dpms = mode;
763 encoder_dpms = drm_helper_choose_encoder_dpms(encoder);
765 /* from off to on, do crtc then encoder */
766 if (mode < old_dpms) {
768 const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
769 if (crtc_funcs->dpms)
770 (*crtc_funcs->dpms) (crtc,
771 drm_helper_choose_crtc_dpms(crtc));
774 drm_helper_encoder_dpms(encoder, encoder_dpms);
777 /* from on to off, do encoder then crtc */
778 if (mode > old_dpms) {
780 drm_helper_encoder_dpms(encoder, encoder_dpms);
782 const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
783 if (crtc_funcs->dpms)
784 (*crtc_funcs->dpms) (crtc,
785 drm_helper_choose_crtc_dpms(crtc));
791 EXPORT_SYMBOL(drm_helper_connector_dpms);
794 * drm_helper_mode_fill_fb_struct - fill out framebuffer metadata
795 * @fb: drm_framebuffer object to fill out
796 * @mode_cmd: metadata from the userspace fb creation request
798 * This helper can be used in a drivers fb_create callback to pre-fill the fb's
801 void drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb,
802 struct drm_mode_fb_cmd2 *mode_cmd)
806 fb->width = mode_cmd->width;
807 fb->height = mode_cmd->height;
808 for (i = 0; i < 4; i++) {
809 fb->pitches[i] = mode_cmd->pitches[i];
810 fb->offsets[i] = mode_cmd->offsets[i];
811 fb->modifier[i] = mode_cmd->modifier[i];
813 drm_fb_get_bpp_depth(mode_cmd->pixel_format, &fb->depth,
814 &fb->bits_per_pixel);
815 fb->pixel_format = mode_cmd->pixel_format;
816 fb->flags = mode_cmd->flags;
818 EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
821 * drm_helper_resume_force_mode - force-restore mode setting configuration
822 * @dev: drm_device which should be restored
824 * Drivers which use the mode setting helpers can use this function to
825 * force-restore the mode setting configuration e.g. on resume or when something
826 * else might have trampled over the hw state (like some overzealous old BIOSen
829 * This helper doesn't provide a error return value since restoring the old
830 * config should never fail due to resource allocation issues since the driver
831 * has successfully set the restored configuration already. Hence this should
832 * boil down to the equivalent of a few dpms on calls, which also don't provide
835 * Drivers where simply restoring an old configuration again might fail (e.g.
836 * due to slight differences in allocating shared resources when the
837 * configuration is restored in a different order than when userspace set it up)
838 * need to use their own restore logic.
840 void drm_helper_resume_force_mode(struct drm_device *dev)
842 struct drm_crtc *crtc;
843 struct drm_encoder *encoder;
844 const struct drm_crtc_helper_funcs *crtc_funcs;
848 drm_modeset_lock_all(dev);
849 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
854 ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
855 crtc->x, crtc->y, crtc->primary->fb);
857 /* Restoring the old config should never fail! */
859 DRM_ERROR("failed to set mode on crtc %p\n", crtc);
861 /* Turn off outputs that were already powered off */
862 if (drm_helper_choose_crtc_dpms(crtc)) {
863 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
865 if(encoder->crtc != crtc)
868 encoder_dpms = drm_helper_choose_encoder_dpms(
871 drm_helper_encoder_dpms(encoder, encoder_dpms);
874 crtc_funcs = crtc->helper_private;
875 if (crtc_funcs->dpms)
876 (*crtc_funcs->dpms) (crtc,
877 drm_helper_choose_crtc_dpms(crtc));
881 /* disable the unused connectors while restoring the modesetting */
882 __drm_helper_disable_unused_functions(dev);
883 drm_modeset_unlock_all(dev);
885 EXPORT_SYMBOL(drm_helper_resume_force_mode);
888 * drm_helper_crtc_mode_set - mode_set implementation for atomic plane helpers
890 * @mode: DRM display mode which userspace requested
891 * @adjusted_mode: DRM display mode adjusted by ->mode_fixup callbacks
892 * @x: x offset of the CRTC scanout area on the underlying framebuffer
893 * @y: y offset of the CRTC scanout area on the underlying framebuffer
894 * @old_fb: previous framebuffer
896 * This function implements a callback useable as the ->mode_set callback
897 * required by the crtc helpers. Besides the atomic plane helper functions for
898 * the primary plane the driver must also provide the ->mode_set_nofb callback
899 * to set up the crtc.
901 * This is a transitional helper useful for converting drivers to the atomic
904 int drm_helper_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode,
905 struct drm_display_mode *adjusted_mode, int x, int y,
906 struct drm_framebuffer *old_fb)
908 struct drm_crtc_state *crtc_state;
909 const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
912 if (crtc->funcs->atomic_duplicate_state)
913 crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
915 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
919 __drm_atomic_helper_crtc_duplicate_state(crtc, crtc_state);
921 crtc_state->crtc = crtc;
924 crtc_state->planes_changed = true;
925 crtc_state->mode_changed = true;
926 ret = drm_atomic_set_mode_for_crtc(crtc_state, mode);
929 drm_mode_copy(&crtc_state->adjusted_mode, adjusted_mode);
931 if (crtc_funcs->atomic_check) {
932 ret = crtc_funcs->atomic_check(crtc, crtc_state);
937 swap(crtc->state, crtc_state);
939 crtc_funcs->mode_set_nofb(crtc);
941 ret = drm_helper_crtc_mode_set_base(crtc, x, y, old_fb);
944 if (crtc->funcs->atomic_destroy_state)
945 crtc->funcs->atomic_destroy_state(crtc, crtc_state);
947 __drm_atomic_helper_crtc_destroy_state(crtc, crtc_state);
953 EXPORT_SYMBOL(drm_helper_crtc_mode_set);
956 * drm_helper_crtc_mode_set_base - mode_set_base implementation for atomic plane helpers
958 * @x: x offset of the CRTC scanout area on the underlying framebuffer
959 * @y: y offset of the CRTC scanout area on the underlying framebuffer
960 * @old_fb: previous framebuffer
962 * This function implements a callback useable as the ->mode_set_base used
963 * required by the crtc helpers. The driver must provide the atomic plane helper
964 * functions for the primary plane.
966 * This is a transitional helper useful for converting drivers to the atomic
969 int drm_helper_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
970 struct drm_framebuffer *old_fb)
972 struct drm_plane_state *plane_state;
973 struct drm_plane *plane = crtc->primary;
975 if (plane->funcs->atomic_duplicate_state)
976 plane_state = plane->funcs->atomic_duplicate_state(plane);
977 else if (plane->state)
978 plane_state = drm_atomic_helper_plane_duplicate_state(plane);
980 plane_state = kzalloc(sizeof(*plane_state), GFP_KERNEL);
983 plane_state->plane = plane;
985 plane_state->crtc = crtc;
986 drm_atomic_set_fb_for_plane(plane_state, crtc->primary->fb);
987 plane_state->crtc_x = 0;
988 plane_state->crtc_y = 0;
989 plane_state->crtc_h = crtc->mode.vdisplay;
990 plane_state->crtc_w = crtc->mode.hdisplay;
991 plane_state->src_x = x << 16;
992 plane_state->src_y = y << 16;
993 plane_state->src_h = crtc->mode.vdisplay << 16;
994 plane_state->src_w = crtc->mode.hdisplay << 16;
996 return drm_plane_helper_commit(plane, plane_state, old_fb);
998 EXPORT_SYMBOL(drm_helper_crtc_mode_set_base);