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;
151 encoder->bridge->funcs->disable(encoder->bridge);
153 if (encoder_funcs->disable)
154 (*encoder_funcs->disable)(encoder);
156 (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
159 encoder->bridge->funcs->post_disable(encoder->bridge);
162 static void __drm_helper_disable_unused_functions(struct drm_device *dev)
164 struct drm_encoder *encoder;
165 struct drm_crtc *crtc;
167 drm_warn_on_modeset_not_all_locked(dev);
169 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
170 if (!drm_helper_encoder_in_use(encoder)) {
171 drm_encoder_disable(encoder);
172 /* disconnect encoder from any connector */
173 encoder->crtc = NULL;
177 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
178 const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
179 crtc->enabled = drm_helper_crtc_in_use(crtc);
180 if (!crtc->enabled) {
181 if (crtc_funcs->disable)
182 (*crtc_funcs->disable)(crtc);
184 (*crtc_funcs->dpms)(crtc, DRM_MODE_DPMS_OFF);
185 crtc->primary->fb = NULL;
191 * drm_helper_disable_unused_functions - disable unused objects
194 * This function walks through the entire mode setting configuration of @dev. It
195 * will remove any crtc links of unused encoders and encoder links of
196 * disconnected connectors. Then it will disable all unused encoders and crtcs
197 * either by calling their disable callback if available or by calling their
198 * dpms callback with DRM_MODE_DPMS_OFF.
200 void drm_helper_disable_unused_functions(struct drm_device *dev)
202 drm_modeset_lock_all(dev);
203 __drm_helper_disable_unused_functions(dev);
204 drm_modeset_unlock_all(dev);
206 EXPORT_SYMBOL(drm_helper_disable_unused_functions);
209 * Check the CRTC we're going to map each output to vs. its current
210 * CRTC. If they don't match, we have to disable the output and the CRTC
211 * since the driver will have to re-route things.
214 drm_crtc_prepare_encoders(struct drm_device *dev)
216 const struct drm_encoder_helper_funcs *encoder_funcs;
217 struct drm_encoder *encoder;
219 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
220 encoder_funcs = encoder->helper_private;
221 /* Disable unused encoders */
222 if (encoder->crtc == NULL)
223 drm_encoder_disable(encoder);
224 /* Disable encoders whose CRTC is about to change */
225 if (encoder_funcs->get_crtc &&
226 encoder->crtc != (*encoder_funcs->get_crtc)(encoder))
227 drm_encoder_disable(encoder);
232 * drm_crtc_helper_set_mode - internal helper to set a mode
233 * @crtc: CRTC to program
235 * @x: horizontal offset into the surface
236 * @y: vertical offset into the surface
237 * @old_fb: old framebuffer, for cleanup
239 * Try to set @mode on @crtc. Give @crtc and its associated connectors a chance
240 * to fixup or reject the mode prior to trying to set it. This is an internal
241 * helper that drivers could e.g. use to update properties that require the
242 * entire output pipe to be disabled and re-enabled in a new configuration. For
243 * example for changing whether audio is enabled on a hdmi link or for changing
244 * panel fitter or dither attributes. It is also called by the
245 * drm_crtc_helper_set_config() helper function to drive the mode setting
249 * True if the mode was set successfully, false otherwise.
251 bool drm_crtc_helper_set_mode(struct drm_crtc *crtc,
252 struct drm_display_mode *mode,
254 struct drm_framebuffer *old_fb)
256 struct drm_device *dev = crtc->dev;
257 struct drm_display_mode *adjusted_mode, saved_mode, saved_hwmode;
258 const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
259 const struct drm_encoder_helper_funcs *encoder_funcs;
260 int saved_x, saved_y;
262 struct drm_encoder *encoder;
265 drm_warn_on_modeset_not_all_locked(dev);
267 saved_enabled = crtc->enabled;
268 crtc->enabled = drm_helper_crtc_in_use(crtc);
272 adjusted_mode = drm_mode_duplicate(dev, mode);
273 if (!adjusted_mode) {
274 crtc->enabled = saved_enabled;
278 saved_mode = crtc->mode;
279 saved_hwmode = crtc->hwmode;
283 /* Update crtc values up front so the driver can rely on them for mode
290 /* Pass our mode to the connectors and the CRTC to give them a chance to
291 * adjust it according to limitations or connector properties, and also
292 * a chance to reject the mode entirely.
294 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
296 if (encoder->crtc != crtc)
299 if (encoder->bridge && encoder->bridge->funcs->mode_fixup) {
300 ret = encoder->bridge->funcs->mode_fixup(
301 encoder->bridge, mode, adjusted_mode);
303 DRM_DEBUG_KMS("Bridge fixup failed\n");
308 encoder_funcs = encoder->helper_private;
309 if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
311 DRM_DEBUG_KMS("Encoder fixup failed\n");
316 if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) {
317 DRM_DEBUG_KMS("CRTC fixup failed\n");
320 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
322 crtc->hwmode = *adjusted_mode;
324 /* Prepare the encoders and CRTCs before setting the mode. */
325 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
327 if (encoder->crtc != crtc)
331 encoder->bridge->funcs->disable(encoder->bridge);
333 encoder_funcs = encoder->helper_private;
334 /* Disable the encoders as the first thing we do. */
335 encoder_funcs->prepare(encoder);
338 encoder->bridge->funcs->post_disable(encoder->bridge);
341 drm_crtc_prepare_encoders(dev);
343 crtc_funcs->prepare(crtc);
345 /* Set up the DPLL and any encoders state that needs to adjust or depend
348 ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
352 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
354 if (encoder->crtc != crtc)
357 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
358 encoder->base.id, encoder->name,
359 mode->base.id, mode->name);
360 encoder_funcs = encoder->helper_private;
361 encoder_funcs->mode_set(encoder, mode, adjusted_mode);
363 if (encoder->bridge && encoder->bridge->funcs->mode_set)
364 encoder->bridge->funcs->mode_set(encoder->bridge, mode,
368 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
369 crtc_funcs->commit(crtc);
371 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
373 if (encoder->crtc != crtc)
377 encoder->bridge->funcs->pre_enable(encoder->bridge);
379 encoder_funcs = encoder->helper_private;
380 encoder_funcs->commit(encoder);
383 encoder->bridge->funcs->enable(encoder->bridge);
386 /* Calculate and store various constants which
387 * are later needed by vblank and swap-completion
388 * timestamping. They are derived from true hwmode.
390 drm_calc_timestamping_constants(crtc, &crtc->hwmode);
392 /* FIXME: add subpixel order */
394 drm_mode_destroy(dev, adjusted_mode);
396 crtc->enabled = saved_enabled;
397 crtc->mode = saved_mode;
398 crtc->hwmode = saved_hwmode;
405 EXPORT_SYMBOL(drm_crtc_helper_set_mode);
408 drm_crtc_helper_disable(struct drm_crtc *crtc)
410 struct drm_device *dev = crtc->dev;
411 struct drm_connector *connector;
412 struct drm_encoder *encoder;
414 /* Decouple all encoders and their attached connectors from this crtc */
415 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
416 if (encoder->crtc != crtc)
419 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
420 if (connector->encoder != encoder)
423 connector->encoder = NULL;
426 * drm_helper_disable_unused_functions() ought to be
427 * doing this, but since we've decoupled the encoder
428 * from the connector above, the required connection
429 * between them is henceforth no longer available.
431 connector->dpms = DRM_MODE_DPMS_OFF;
435 __drm_helper_disable_unused_functions(dev);
439 * drm_crtc_helper_set_config - set a new config from userspace
440 * @set: mode set configuration
442 * Setup a new configuration, provided by the upper layers (either an ioctl call
443 * from userspace or internally e.g. from the fbdev support code) in @set, and
444 * enable it. This is the main helper functions for drivers that implement
445 * kernel mode setting with the crtc helper functions and the assorted
446 * ->prepare(), ->modeset() and ->commit() helper callbacks.
449 * Returns 0 on success, negative errno numbers on failure.
451 int drm_crtc_helper_set_config(struct drm_mode_set *set)
453 struct drm_device *dev;
454 struct drm_crtc *new_crtc;
455 struct drm_encoder *save_encoders, *new_encoder, *encoder;
456 bool mode_changed = false; /* if true do a full mode set */
457 bool fb_changed = false; /* if true and !mode_changed just do a flip */
458 struct drm_connector *save_connectors, *connector;
459 int count = 0, ro, fail = 0;
460 const struct drm_crtc_helper_funcs *crtc_funcs;
461 struct drm_mode_set save_set;
469 BUG_ON(!set->crtc->helper_private);
471 /* Enforce sane interface api - has been abused by the fb helper. */
472 BUG_ON(!set->mode && set->fb);
473 BUG_ON(set->fb && set->num_connectors == 0);
475 crtc_funcs = set->crtc->helper_private;
481 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
482 set->crtc->base.id, set->fb->base.id,
483 (int)set->num_connectors, set->x, set->y);
485 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
486 drm_crtc_helper_disable(set->crtc);
490 dev = set->crtc->dev;
492 drm_warn_on_modeset_not_all_locked(dev);
495 * Allocate space for the backup of all (non-pointer) encoder and
498 save_encoders = kzalloc(dev->mode_config.num_encoder *
499 sizeof(struct drm_encoder), GFP_KERNEL);
503 save_connectors = kzalloc(dev->mode_config.num_connector *
504 sizeof(struct drm_connector), GFP_KERNEL);
505 if (!save_connectors) {
506 kfree(save_encoders);
511 * Copy data. Note that driver private data is not affected.
512 * Should anything bad happen only the expected state is
513 * restored, not the drivers personal bookkeeping.
516 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
517 save_encoders[count++] = *encoder;
521 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
522 save_connectors[count++] = *connector;
525 save_set.crtc = set->crtc;
526 save_set.mode = &set->crtc->mode;
527 save_set.x = set->crtc->x;
528 save_set.y = set->crtc->y;
529 save_set.fb = set->crtc->primary->fb;
531 /* We should be able to check here if the fb has the same properties
532 * and then just flip_or_move it */
533 if (set->crtc->primary->fb != set->fb) {
534 /* If we have no fb then treat it as a full mode set */
535 if (set->crtc->primary->fb == NULL) {
536 DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
538 } else if (set->fb == NULL) {
540 } else if (set->fb->pixel_format !=
541 set->crtc->primary->fb->pixel_format) {
547 if (set->x != set->crtc->x || set->y != set->crtc->y)
550 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
551 DRM_DEBUG_KMS("modes are different, full mode set\n");
552 drm_mode_debug_printmodeline(&set->crtc->mode);
553 drm_mode_debug_printmodeline(set->mode);
557 /* a) traverse passed in connector list and get encoders for them */
559 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
560 const struct drm_connector_helper_funcs *connector_funcs =
561 connector->helper_private;
562 new_encoder = connector->encoder;
563 for (ro = 0; ro < set->num_connectors; ro++) {
564 if (set->connectors[ro] == connector) {
565 new_encoder = connector_funcs->best_encoder(connector);
566 /* if we can't get an encoder for a connector
567 we are setting now - then fail */
568 if (new_encoder == NULL)
569 /* don't break so fail path works correct */
572 if (connector->dpms != DRM_MODE_DPMS_ON) {
573 DRM_DEBUG_KMS("connector dpms not on, full mode switch\n");
581 if (new_encoder != connector->encoder) {
582 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
584 /* If the encoder is reused for another connector, then
585 * the appropriate crtc will be set later.
587 if (connector->encoder)
588 connector->encoder->crtc = NULL;
589 connector->encoder = new_encoder;
599 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
600 if (!connector->encoder)
603 if (connector->encoder->crtc == set->crtc)
606 new_crtc = connector->encoder->crtc;
608 for (ro = 0; ro < set->num_connectors; ro++) {
609 if (set->connectors[ro] == connector)
610 new_crtc = set->crtc;
613 /* Make sure the new CRTC will work with the encoder */
615 !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
619 if (new_crtc != connector->encoder->crtc) {
620 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
622 connector->encoder->crtc = new_crtc;
625 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
626 connector->base.id, connector->name,
629 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
630 connector->base.id, connector->name);
634 /* mode_set_base is not a required function */
635 if (fb_changed && !crtc_funcs->mode_set_base)
639 if (drm_helper_crtc_in_use(set->crtc)) {
640 DRM_DEBUG_KMS("attempting to set mode from"
642 drm_mode_debug_printmodeline(set->mode);
643 set->crtc->primary->fb = set->fb;
644 if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
647 DRM_ERROR("failed to set mode on [CRTC:%d]\n",
649 set->crtc->primary->fb = save_set.fb;
653 DRM_DEBUG_KMS("Setting connector DPMS state to on\n");
654 for (i = 0; i < set->num_connectors; i++) {
655 DRM_DEBUG_KMS("\t[CONNECTOR:%d:%s] set DPMS on\n", set->connectors[i]->base.id,
656 set->connectors[i]->name);
657 set->connectors[i]->funcs->dpms(set->connectors[i], DRM_MODE_DPMS_ON);
660 __drm_helper_disable_unused_functions(dev);
661 } else if (fb_changed) {
662 set->crtc->x = set->x;
663 set->crtc->y = set->y;
664 set->crtc->primary->fb = set->fb;
665 ret = crtc_funcs->mode_set_base(set->crtc,
666 set->x, set->y, save_set.fb);
668 set->crtc->x = save_set.x;
669 set->crtc->y = save_set.y;
670 set->crtc->primary->fb = save_set.fb;
675 kfree(save_connectors);
676 kfree(save_encoders);
680 /* Restore all previous data. */
682 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
683 *encoder = save_encoders[count++];
687 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
688 *connector = save_connectors[count++];
691 /* Try to restore the config */
693 !drm_crtc_helper_set_mode(save_set.crtc, save_set.mode, save_set.x,
694 save_set.y, save_set.fb))
695 DRM_ERROR("failed to restore config after modeset failure\n");
697 kfree(save_connectors);
698 kfree(save_encoders);
701 EXPORT_SYMBOL(drm_crtc_helper_set_config);
703 static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder)
705 int dpms = DRM_MODE_DPMS_OFF;
706 struct drm_connector *connector;
707 struct drm_device *dev = encoder->dev;
709 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
710 if (connector->encoder == encoder)
711 if (connector->dpms < dpms)
712 dpms = connector->dpms;
716 /* Helper which handles bridge ordering around encoder dpms */
717 static void drm_helper_encoder_dpms(struct drm_encoder *encoder, int mode)
719 struct drm_bridge *bridge = encoder->bridge;
720 const struct drm_encoder_helper_funcs *encoder_funcs;
723 if (mode == DRM_MODE_DPMS_ON)
724 bridge->funcs->pre_enable(bridge);
726 bridge->funcs->disable(bridge);
729 encoder_funcs = encoder->helper_private;
730 if (encoder_funcs->dpms)
731 encoder_funcs->dpms(encoder, mode);
734 if (mode == DRM_MODE_DPMS_ON)
735 bridge->funcs->enable(bridge);
737 bridge->funcs->post_disable(bridge);
741 static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc)
743 int dpms = DRM_MODE_DPMS_OFF;
744 struct drm_connector *connector;
745 struct drm_device *dev = crtc->dev;
747 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
748 if (connector->encoder && connector->encoder->crtc == crtc)
749 if (connector->dpms < dpms)
750 dpms = connector->dpms;
755 * drm_helper_connector_dpms() - connector dpms helper implementation
756 * @connector: affected connector
759 * This is the main helper function provided by the crtc helper framework for
760 * implementing the DPMS connector attribute. It computes the new desired DPMS
761 * state for all encoders and crtcs in the output mesh and calls the ->dpms()
762 * callback provided by the driver appropriately.
764 void drm_helper_connector_dpms(struct drm_connector *connector, int mode)
766 struct drm_encoder *encoder = connector->encoder;
767 struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
768 int old_dpms, encoder_dpms = DRM_MODE_DPMS_OFF;
770 if (mode == connector->dpms)
773 old_dpms = connector->dpms;
774 connector->dpms = mode;
777 encoder_dpms = drm_helper_choose_encoder_dpms(encoder);
779 /* from off to on, do crtc then encoder */
780 if (mode < old_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));
788 drm_helper_encoder_dpms(encoder, encoder_dpms);
791 /* from on to off, do encoder then crtc */
792 if (mode > old_dpms) {
794 drm_helper_encoder_dpms(encoder, encoder_dpms);
796 const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
797 if (crtc_funcs->dpms)
798 (*crtc_funcs->dpms) (crtc,
799 drm_helper_choose_crtc_dpms(crtc));
805 EXPORT_SYMBOL(drm_helper_connector_dpms);
808 * drm_helper_mode_fill_fb_struct - fill out framebuffer metadata
809 * @fb: drm_framebuffer object to fill out
810 * @mode_cmd: metadata from the userspace fb creation request
812 * This helper can be used in a drivers fb_create callback to pre-fill the fb's
815 void drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb,
816 struct drm_mode_fb_cmd2 *mode_cmd)
820 fb->width = mode_cmd->width;
821 fb->height = mode_cmd->height;
822 for (i = 0; i < 4; i++) {
823 fb->pitches[i] = mode_cmd->pitches[i];
824 fb->offsets[i] = mode_cmd->offsets[i];
825 fb->modifier[i] = mode_cmd->modifier[i];
827 drm_fb_get_bpp_depth(mode_cmd->pixel_format, &fb->depth,
828 &fb->bits_per_pixel);
829 fb->pixel_format = mode_cmd->pixel_format;
830 fb->flags = mode_cmd->flags;
832 EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
835 * drm_helper_resume_force_mode - force-restore mode setting configuration
836 * @dev: drm_device which should be restored
838 * Drivers which use the mode setting helpers can use this function to
839 * force-restore the mode setting configuration e.g. on resume or when something
840 * else might have trampled over the hw state (like some overzealous old BIOSen
843 * This helper doesn't provide a error return value since restoring the old
844 * config should never fail due to resource allocation issues since the driver
845 * has successfully set the restored configuration already. Hence this should
846 * boil down to the equivalent of a few dpms on calls, which also don't provide
849 * Drivers where simply restoring an old configuration again might fail (e.g.
850 * due to slight differences in allocating shared resources when the
851 * configuration is restored in a different order than when userspace set it up)
852 * need to use their own restore logic.
854 void drm_helper_resume_force_mode(struct drm_device *dev)
856 struct drm_crtc *crtc;
857 struct drm_encoder *encoder;
858 const struct drm_crtc_helper_funcs *crtc_funcs;
862 drm_modeset_lock_all(dev);
863 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
868 ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
869 crtc->x, crtc->y, crtc->primary->fb);
871 /* Restoring the old config should never fail! */
873 DRM_ERROR("failed to set mode on crtc %p\n", crtc);
875 /* Turn off outputs that were already powered off */
876 if (drm_helper_choose_crtc_dpms(crtc)) {
877 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
879 if(encoder->crtc != crtc)
882 encoder_dpms = drm_helper_choose_encoder_dpms(
885 drm_helper_encoder_dpms(encoder, encoder_dpms);
888 crtc_funcs = crtc->helper_private;
889 if (crtc_funcs->dpms)
890 (*crtc_funcs->dpms) (crtc,
891 drm_helper_choose_crtc_dpms(crtc));
895 /* disable the unused connectors while restoring the modesetting */
896 __drm_helper_disable_unused_functions(dev);
897 drm_modeset_unlock_all(dev);
899 EXPORT_SYMBOL(drm_helper_resume_force_mode);
902 * drm_helper_crtc_mode_set - mode_set implementation for atomic plane helpers
904 * @mode: DRM display mode which userspace requested
905 * @adjusted_mode: DRM display mode adjusted by ->mode_fixup callbacks
906 * @x: x offset of the CRTC scanout area on the underlying framebuffer
907 * @y: y offset of the CRTC scanout area on the underlying framebuffer
908 * @old_fb: previous framebuffer
910 * This function implements a callback useable as the ->mode_set callback
911 * required by the crtc helpers. Besides the atomic plane helper functions for
912 * the primary plane the driver must also provide the ->mode_set_nofb callback
913 * to set up the crtc.
915 * This is a transitional helper useful for converting drivers to the atomic
918 int drm_helper_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode,
919 struct drm_display_mode *adjusted_mode, int x, int y,
920 struct drm_framebuffer *old_fb)
922 struct drm_crtc_state *crtc_state;
923 const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
926 if (crtc->funcs->atomic_duplicate_state)
927 crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
928 else if (crtc->state)
929 crtc_state = kmemdup(crtc->state, sizeof(*crtc_state),
932 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
935 crtc_state->crtc = crtc;
937 crtc_state->enable = true;
938 crtc_state->planes_changed = true;
939 crtc_state->mode_changed = true;
940 drm_mode_copy(&crtc_state->mode, mode);
941 drm_mode_copy(&crtc_state->adjusted_mode, adjusted_mode);
943 if (crtc_funcs->atomic_check) {
944 ret = crtc_funcs->atomic_check(crtc, crtc_state);
952 swap(crtc->state, crtc_state);
954 crtc_funcs->mode_set_nofb(crtc);
957 if (crtc->funcs->atomic_destroy_state)
958 crtc->funcs->atomic_destroy_state(crtc, crtc_state);
963 return drm_helper_crtc_mode_set_base(crtc, x, y, old_fb);
965 EXPORT_SYMBOL(drm_helper_crtc_mode_set);
968 * drm_helper_crtc_mode_set_base - mode_set_base implementation for atomic plane helpers
970 * @x: x offset of the CRTC scanout area on the underlying framebuffer
971 * @y: y offset of the CRTC scanout area on the underlying framebuffer
972 * @old_fb: previous framebuffer
974 * This function implements a callback useable as the ->mode_set_base used
975 * required by the crtc helpers. The driver must provide the atomic plane helper
976 * functions for the primary plane.
978 * This is a transitional helper useful for converting drivers to the atomic
981 int drm_helper_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
982 struct drm_framebuffer *old_fb)
984 struct drm_plane_state *plane_state;
985 struct drm_plane *plane = crtc->primary;
987 if (plane->funcs->atomic_duplicate_state)
988 plane_state = plane->funcs->atomic_duplicate_state(plane);
989 else if (plane->state)
990 plane_state = drm_atomic_helper_plane_duplicate_state(plane);
992 plane_state = kzalloc(sizeof(*plane_state), GFP_KERNEL);
995 plane_state->plane = plane;
997 plane_state->crtc = crtc;
998 drm_atomic_set_fb_for_plane(plane_state, crtc->primary->fb);
999 plane_state->crtc_x = 0;
1000 plane_state->crtc_y = 0;
1001 plane_state->crtc_h = crtc->mode.vdisplay;
1002 plane_state->crtc_w = crtc->mode.hdisplay;
1003 plane_state->src_x = x << 16;
1004 plane_state->src_y = y << 16;
1005 plane_state->src_h = crtc->mode.vdisplay << 16;
1006 plane_state->src_w = crtc->mode.hdisplay << 16;
1008 return drm_plane_helper_commit(plane, plane_state, old_fb);
1010 EXPORT_SYMBOL(drm_helper_crtc_mode_set_base);