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_crtc.h>
38 #include <drm/drm_fourcc.h>
39 #include <drm/drm_crtc_helper.h>
40 #include <drm/drm_fb_helper.h>
41 #include <drm/drm_edid.h>
43 MODULE_AUTHOR("David Airlie, Jesse Barnes");
44 MODULE_DESCRIPTION("DRM KMS helper");
45 MODULE_LICENSE("GPL and additional rights");
48 * drm_helper_move_panel_connectors_to_head() - move panels to the front in the
50 * @dev: drm device to operate on
52 * Some userspace presumes that the first connected connector is the main
53 * display, where it's supposed to display e.g. the login screen. For
54 * laptops, this should be the main panel. Use this function to sort all
55 * (eDP/LVDS) panels to the front of the connector list, instead of
56 * painstakingly trying to initialize them in the right order.
58 void drm_helper_move_panel_connectors_to_head(struct drm_device
*dev
)
60 struct drm_connector
*connector
, *tmp
;
61 struct list_head panel_list
;
63 INIT_LIST_HEAD(&panel_list
);
65 list_for_each_entry_safe(connector
, tmp
,
66 &dev
->mode_config
.connector_list
, head
) {
67 if (connector
->connector_type
== DRM_MODE_CONNECTOR_LVDS
||
68 connector
->connector_type
== DRM_MODE_CONNECTOR_eDP
)
69 list_move_tail(&connector
->head
, &panel_list
);
72 list_splice(&panel_list
, &dev
->mode_config
.connector_list
);
74 EXPORT_SYMBOL(drm_helper_move_panel_connectors_to_head
);
77 * drm_helper_encoder_in_use - check if a given encoder is in use
78 * @encoder: encoder to check
80 * Checks whether @encoder is with the current mode setting output configuration
81 * in use by any connector. This doesn't mean that it is actually enabled since
82 * the DPMS state is tracked separately.
85 * True if @encoder is used, false otherwise.
87 bool drm_helper_encoder_in_use(struct drm_encoder
*encoder
)
89 struct drm_connector
*connector
;
90 struct drm_device
*dev
= encoder
->dev
;
93 * We can expect this mutex to be locked if we are not panicking.
94 * Locking is currently fubar in the panic handler.
96 if (!oops_in_progress
) {
97 WARN_ON(!mutex_is_locked(&dev
->mode_config
.mutex
));
98 WARN_ON(!drm_modeset_is_locked(&dev
->mode_config
.connection_mutex
));
101 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
)
102 if (connector
->encoder
== encoder
)
106 EXPORT_SYMBOL(drm_helper_encoder_in_use
);
109 * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config
110 * @crtc: CRTC to check
112 * Checks whether @crtc is with the current mode setting output configuration
113 * in use by any connector. This doesn't mean that it is actually enabled since
114 * the DPMS state is tracked separately.
117 * True if @crtc is used, false otherwise.
119 bool drm_helper_crtc_in_use(struct drm_crtc
*crtc
)
121 struct drm_encoder
*encoder
;
122 struct drm_device
*dev
= crtc
->dev
;
125 * We can expect this mutex to be locked if we are not panicking.
126 * Locking is currently fubar in the panic handler.
128 if (!oops_in_progress
)
129 WARN_ON(!mutex_is_locked(&dev
->mode_config
.mutex
));
131 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
)
132 if (encoder
->crtc
== crtc
&& drm_helper_encoder_in_use(encoder
))
136 EXPORT_SYMBOL(drm_helper_crtc_in_use
);
139 drm_encoder_disable(struct drm_encoder
*encoder
)
141 struct drm_encoder_helper_funcs
*encoder_funcs
= encoder
->helper_private
;
144 encoder
->bridge
->funcs
->disable(encoder
->bridge
);
146 if (encoder_funcs
->disable
)
147 (*encoder_funcs
->disable
)(encoder
);
149 (*encoder_funcs
->dpms
)(encoder
, DRM_MODE_DPMS_OFF
);
152 encoder
->bridge
->funcs
->post_disable(encoder
->bridge
);
155 static void __drm_helper_disable_unused_functions(struct drm_device
*dev
)
157 struct drm_encoder
*encoder
;
158 struct drm_crtc
*crtc
;
160 drm_warn_on_modeset_not_all_locked(dev
);
162 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
) {
163 if (!drm_helper_encoder_in_use(encoder
)) {
164 drm_encoder_disable(encoder
);
165 /* disconnect encoder from any connector */
166 encoder
->crtc
= NULL
;
170 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
) {
171 struct drm_crtc_helper_funcs
*crtc_funcs
= crtc
->helper_private
;
172 crtc
->enabled
= drm_helper_crtc_in_use(crtc
);
173 if (!crtc
->enabled
) {
174 if (crtc_funcs
->disable
)
175 (*crtc_funcs
->disable
)(crtc
);
177 (*crtc_funcs
->dpms
)(crtc
, DRM_MODE_DPMS_OFF
);
178 crtc
->primary
->fb
= NULL
;
184 * drm_helper_disable_unused_functions - disable unused objects
187 * This function walks through the entire mode setting configuration of @dev. It
188 * will remove any crtc links of unused encoders and encoder links of
189 * disconnected connectors. Then it will disable all unused encoders and crtcs
190 * either by calling their disable callback if available or by calling their
191 * dpms callback with DRM_MODE_DPMS_OFF.
193 void drm_helper_disable_unused_functions(struct drm_device
*dev
)
195 drm_modeset_lock_all(dev
);
196 __drm_helper_disable_unused_functions(dev
);
197 drm_modeset_unlock_all(dev
);
199 EXPORT_SYMBOL(drm_helper_disable_unused_functions
);
202 * Check the CRTC we're going to map each output to vs. its current
203 * CRTC. If they don't match, we have to disable the output and the CRTC
204 * since the driver will have to re-route things.
207 drm_crtc_prepare_encoders(struct drm_device
*dev
)
209 struct drm_encoder_helper_funcs
*encoder_funcs
;
210 struct drm_encoder
*encoder
;
212 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
) {
213 encoder_funcs
= encoder
->helper_private
;
214 /* Disable unused encoders */
215 if (encoder
->crtc
== NULL
)
216 drm_encoder_disable(encoder
);
217 /* Disable encoders whose CRTC is about to change */
218 if (encoder_funcs
->get_crtc
&&
219 encoder
->crtc
!= (*encoder_funcs
->get_crtc
)(encoder
))
220 drm_encoder_disable(encoder
);
225 * drm_crtc_helper_set_mode - internal helper to set a mode
226 * @crtc: CRTC to program
228 * @x: horizontal offset into the surface
229 * @y: vertical offset into the surface
230 * @old_fb: old framebuffer, for cleanup
232 * Try to set @mode on @crtc. Give @crtc and its associated connectors a chance
233 * to fixup or reject the mode prior to trying to set it. This is an internal
234 * helper that drivers could e.g. use to update properties that require the
235 * entire output pipe to be disabled and re-enabled in a new configuration. For
236 * example for changing whether audio is enabled on a hdmi link or for changing
237 * panel fitter or dither attributes. It is also called by the
238 * drm_crtc_helper_set_config() helper function to drive the mode setting
242 * True if the mode was set successfully, false otherwise.
244 bool drm_crtc_helper_set_mode(struct drm_crtc
*crtc
,
245 struct drm_display_mode
*mode
,
247 struct drm_framebuffer
*old_fb
)
249 struct drm_device
*dev
= crtc
->dev
;
250 struct drm_display_mode
*adjusted_mode
, saved_mode
;
251 struct drm_crtc_helper_funcs
*crtc_funcs
= crtc
->helper_private
;
252 struct drm_encoder_helper_funcs
*encoder_funcs
;
253 int saved_x
, saved_y
;
255 struct drm_encoder
*encoder
;
258 drm_warn_on_modeset_not_all_locked(dev
);
260 saved_enabled
= crtc
->enabled
;
261 crtc
->enabled
= drm_helper_crtc_in_use(crtc
);
265 adjusted_mode
= drm_mode_duplicate(dev
, mode
);
266 if (!adjusted_mode
) {
267 crtc
->enabled
= saved_enabled
;
271 saved_mode
= crtc
->mode
;
275 /* Update crtc values up front so the driver can rely on them for mode
282 /* Pass our mode to the connectors and the CRTC to give them a chance to
283 * adjust it according to limitations or connector properties, and also
284 * a chance to reject the mode entirely.
286 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
) {
288 if (encoder
->crtc
!= crtc
)
291 if (encoder
->bridge
&& encoder
->bridge
->funcs
->mode_fixup
) {
292 ret
= encoder
->bridge
->funcs
->mode_fixup(
293 encoder
->bridge
, mode
, adjusted_mode
);
295 DRM_DEBUG_KMS("Bridge fixup failed\n");
300 encoder_funcs
= encoder
->helper_private
;
301 if (!(ret
= encoder_funcs
->mode_fixup(encoder
, mode
,
303 DRM_DEBUG_KMS("Encoder fixup failed\n");
308 if (!(ret
= crtc_funcs
->mode_fixup(crtc
, mode
, adjusted_mode
))) {
309 DRM_DEBUG_KMS("CRTC fixup failed\n");
312 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc
->base
.id
);
314 /* Prepare the encoders and CRTCs before setting the mode. */
315 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
) {
317 if (encoder
->crtc
!= crtc
)
321 encoder
->bridge
->funcs
->disable(encoder
->bridge
);
323 encoder_funcs
= encoder
->helper_private
;
324 /* Disable the encoders as the first thing we do. */
325 encoder_funcs
->prepare(encoder
);
328 encoder
->bridge
->funcs
->post_disable(encoder
->bridge
);
331 drm_crtc_prepare_encoders(dev
);
333 crtc_funcs
->prepare(crtc
);
335 /* Set up the DPLL and any encoders state that needs to adjust or depend
338 ret
= !crtc_funcs
->mode_set(crtc
, mode
, adjusted_mode
, x
, y
, old_fb
);
342 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
) {
344 if (encoder
->crtc
!= crtc
)
347 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
348 encoder
->base
.id
, encoder
->name
,
349 mode
->base
.id
, mode
->name
);
350 encoder_funcs
= encoder
->helper_private
;
351 encoder_funcs
->mode_set(encoder
, mode
, adjusted_mode
);
353 if (encoder
->bridge
&& encoder
->bridge
->funcs
->mode_set
)
354 encoder
->bridge
->funcs
->mode_set(encoder
->bridge
, mode
,
358 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
359 crtc_funcs
->commit(crtc
);
361 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
) {
363 if (encoder
->crtc
!= crtc
)
367 encoder
->bridge
->funcs
->pre_enable(encoder
->bridge
);
369 encoder_funcs
= encoder
->helper_private
;
370 encoder_funcs
->commit(encoder
);
373 encoder
->bridge
->funcs
->enable(encoder
->bridge
);
376 /* Store real post-adjustment hardware mode. */
377 crtc
->hwmode
= *adjusted_mode
;
379 /* Calculate and store various constants which
380 * are later needed by vblank and swap-completion
381 * timestamping. They are derived from true hwmode.
383 drm_calc_timestamping_constants(crtc
, &crtc
->hwmode
);
385 /* FIXME: add subpixel order */
387 drm_mode_destroy(dev
, adjusted_mode
);
389 crtc
->enabled
= saved_enabled
;
390 crtc
->mode
= saved_mode
;
397 EXPORT_SYMBOL(drm_crtc_helper_set_mode
);
400 drm_crtc_helper_disable(struct drm_crtc
*crtc
)
402 struct drm_device
*dev
= crtc
->dev
;
403 struct drm_connector
*connector
;
404 struct drm_encoder
*encoder
;
406 /* Decouple all encoders and their attached connectors from this crtc */
407 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
) {
408 if (encoder
->crtc
!= crtc
)
411 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
412 if (connector
->encoder
!= encoder
)
415 connector
->encoder
= NULL
;
418 * drm_helper_disable_unused_functions() ought to be
419 * doing this, but since we've decoupled the encoder
420 * from the connector above, the required connection
421 * between them is henceforth no longer available.
423 connector
->dpms
= DRM_MODE_DPMS_OFF
;
427 __drm_helper_disable_unused_functions(dev
);
431 * drm_crtc_helper_set_config - set a new config from userspace
432 * @set: mode set configuration
434 * Setup a new configuration, provided by the upper layers (either an ioctl call
435 * from userspace or internally e.g. from the fbdev support code) in @set, and
436 * enable it. This is the main helper functions for drivers that implement
437 * kernel mode setting with the crtc helper functions and the assorted
438 * ->prepare(), ->modeset() and ->commit() helper callbacks.
441 * Returns 0 on success, negative errno numbers on failure.
443 int drm_crtc_helper_set_config(struct drm_mode_set
*set
)
445 struct drm_device
*dev
;
446 struct drm_crtc
*new_crtc
;
447 struct drm_encoder
*save_encoders
, *new_encoder
, *encoder
;
448 bool mode_changed
= false; /* if true do a full mode set */
449 bool fb_changed
= false; /* if true and !mode_changed just do a flip */
450 struct drm_connector
*save_connectors
, *connector
;
451 int count
= 0, ro
, fail
= 0;
452 struct drm_crtc_helper_funcs
*crtc_funcs
;
453 struct drm_mode_set save_set
;
461 BUG_ON(!set
->crtc
->helper_private
);
463 /* Enforce sane interface api - has been abused by the fb helper. */
464 BUG_ON(!set
->mode
&& set
->fb
);
465 BUG_ON(set
->fb
&& set
->num_connectors
== 0);
467 crtc_funcs
= set
->crtc
->helper_private
;
473 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
474 set
->crtc
->base
.id
, set
->fb
->base
.id
,
475 (int)set
->num_connectors
, set
->x
, set
->y
);
477 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set
->crtc
->base
.id
);
478 drm_crtc_helper_disable(set
->crtc
);
482 dev
= set
->crtc
->dev
;
484 drm_warn_on_modeset_not_all_locked(dev
);
487 * Allocate space for the backup of all (non-pointer) encoder and
490 save_encoders
= kzalloc(dev
->mode_config
.num_encoder
*
491 sizeof(struct drm_encoder
), GFP_KERNEL
);
495 save_connectors
= kzalloc(dev
->mode_config
.num_connector
*
496 sizeof(struct drm_connector
), GFP_KERNEL
);
497 if (!save_connectors
) {
498 kfree(save_encoders
);
503 * Copy data. Note that driver private data is not affected.
504 * Should anything bad happen only the expected state is
505 * restored, not the drivers personal bookkeeping.
508 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
) {
509 save_encoders
[count
++] = *encoder
;
513 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
514 save_connectors
[count
++] = *connector
;
517 save_set
.crtc
= set
->crtc
;
518 save_set
.mode
= &set
->crtc
->mode
;
519 save_set
.x
= set
->crtc
->x
;
520 save_set
.y
= set
->crtc
->y
;
521 save_set
.fb
= set
->crtc
->primary
->fb
;
523 /* We should be able to check here if the fb has the same properties
524 * and then just flip_or_move it */
525 if (set
->crtc
->primary
->fb
!= set
->fb
) {
526 /* If we have no fb then treat it as a full mode set */
527 if (set
->crtc
->primary
->fb
== NULL
) {
528 DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
530 } else if (set
->fb
== NULL
) {
532 } else if (set
->fb
->pixel_format
!=
533 set
->crtc
->primary
->fb
->pixel_format
) {
539 if (set
->x
!= set
->crtc
->x
|| set
->y
!= set
->crtc
->y
)
542 if (set
->mode
&& !drm_mode_equal(set
->mode
, &set
->crtc
->mode
)) {
543 DRM_DEBUG_KMS("modes are different, full mode set\n");
544 drm_mode_debug_printmodeline(&set
->crtc
->mode
);
545 drm_mode_debug_printmodeline(set
->mode
);
549 /* a) traverse passed in connector list and get encoders for them */
551 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
552 struct drm_connector_helper_funcs
*connector_funcs
=
553 connector
->helper_private
;
554 new_encoder
= connector
->encoder
;
555 for (ro
= 0; ro
< set
->num_connectors
; ro
++) {
556 if (set
->connectors
[ro
] == connector
) {
557 new_encoder
= connector_funcs
->best_encoder(connector
);
558 /* if we can't get an encoder for a connector
559 we are setting now - then fail */
560 if (new_encoder
== NULL
)
561 /* don't break so fail path works correct */
564 if (connector
->dpms
!= DRM_MODE_DPMS_ON
) {
565 DRM_DEBUG_KMS("connector dpms not on, full mode switch\n");
573 if (new_encoder
!= connector
->encoder
) {
574 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
576 /* If the encoder is reused for another connector, then
577 * the appropriate crtc will be set later.
579 if (connector
->encoder
)
580 connector
->encoder
->crtc
= NULL
;
581 connector
->encoder
= new_encoder
;
591 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
592 if (!connector
->encoder
)
595 if (connector
->encoder
->crtc
== set
->crtc
)
598 new_crtc
= connector
->encoder
->crtc
;
600 for (ro
= 0; ro
< set
->num_connectors
; ro
++) {
601 if (set
->connectors
[ro
] == connector
)
602 new_crtc
= set
->crtc
;
605 /* Make sure the new CRTC will work with the encoder */
607 !drm_encoder_crtc_ok(connector
->encoder
, new_crtc
)) {
611 if (new_crtc
!= connector
->encoder
->crtc
) {
612 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
614 connector
->encoder
->crtc
= new_crtc
;
617 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
618 connector
->base
.id
, connector
->name
,
621 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
622 connector
->base
.id
, connector
->name
);
626 /* mode_set_base is not a required function */
627 if (fb_changed
&& !crtc_funcs
->mode_set_base
)
631 if (drm_helper_crtc_in_use(set
->crtc
)) {
632 DRM_DEBUG_KMS("attempting to set mode from"
634 drm_mode_debug_printmodeline(set
->mode
);
635 set
->crtc
->primary
->fb
= set
->fb
;
636 if (!drm_crtc_helper_set_mode(set
->crtc
, set
->mode
,
639 DRM_ERROR("failed to set mode on [CRTC:%d]\n",
641 set
->crtc
->primary
->fb
= save_set
.fb
;
645 DRM_DEBUG_KMS("Setting connector DPMS state to on\n");
646 for (i
= 0; i
< set
->num_connectors
; i
++) {
647 DRM_DEBUG_KMS("\t[CONNECTOR:%d:%s] set DPMS on\n", set
->connectors
[i
]->base
.id
,
648 set
->connectors
[i
]->name
);
649 set
->connectors
[i
]->funcs
->dpms(set
->connectors
[i
], DRM_MODE_DPMS_ON
);
652 __drm_helper_disable_unused_functions(dev
);
653 } else if (fb_changed
) {
654 set
->crtc
->x
= set
->x
;
655 set
->crtc
->y
= set
->y
;
656 set
->crtc
->primary
->fb
= set
->fb
;
657 ret
= crtc_funcs
->mode_set_base(set
->crtc
,
658 set
->x
, set
->y
, save_set
.fb
);
660 set
->crtc
->x
= save_set
.x
;
661 set
->crtc
->y
= save_set
.y
;
662 set
->crtc
->primary
->fb
= save_set
.fb
;
667 kfree(save_connectors
);
668 kfree(save_encoders
);
672 /* Restore all previous data. */
674 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
) {
675 *encoder
= save_encoders
[count
++];
679 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
680 *connector
= save_connectors
[count
++];
683 /* Try to restore the config */
685 !drm_crtc_helper_set_mode(save_set
.crtc
, save_set
.mode
, save_set
.x
,
686 save_set
.y
, save_set
.fb
))
687 DRM_ERROR("failed to restore config after modeset failure\n");
689 kfree(save_connectors
);
690 kfree(save_encoders
);
693 EXPORT_SYMBOL(drm_crtc_helper_set_config
);
695 static int drm_helper_choose_encoder_dpms(struct drm_encoder
*encoder
)
697 int dpms
= DRM_MODE_DPMS_OFF
;
698 struct drm_connector
*connector
;
699 struct drm_device
*dev
= encoder
->dev
;
701 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
)
702 if (connector
->encoder
== encoder
)
703 if (connector
->dpms
< dpms
)
704 dpms
= connector
->dpms
;
708 /* Helper which handles bridge ordering around encoder dpms */
709 static void drm_helper_encoder_dpms(struct drm_encoder
*encoder
, int mode
)
711 struct drm_bridge
*bridge
= encoder
->bridge
;
712 struct drm_encoder_helper_funcs
*encoder_funcs
;
715 if (mode
== DRM_MODE_DPMS_ON
)
716 bridge
->funcs
->pre_enable(bridge
);
718 bridge
->funcs
->disable(bridge
);
721 encoder_funcs
= encoder
->helper_private
;
722 if (encoder_funcs
->dpms
)
723 encoder_funcs
->dpms(encoder
, mode
);
726 if (mode
== DRM_MODE_DPMS_ON
)
727 bridge
->funcs
->enable(bridge
);
729 bridge
->funcs
->post_disable(bridge
);
733 static int drm_helper_choose_crtc_dpms(struct drm_crtc
*crtc
)
735 int dpms
= DRM_MODE_DPMS_OFF
;
736 struct drm_connector
*connector
;
737 struct drm_device
*dev
= crtc
->dev
;
739 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
)
740 if (connector
->encoder
&& connector
->encoder
->crtc
== crtc
)
741 if (connector
->dpms
< dpms
)
742 dpms
= connector
->dpms
;
747 * drm_helper_connector_dpms() - connector dpms helper implementation
748 * @connector: affected connector
751 * This is the main helper function provided by the crtc helper framework for
752 * implementing the DPMS connector attribute. It computes the new desired DPMS
753 * state for all encoders and crtcs in the output mesh and calls the ->dpms()
754 * callback provided by the driver appropriately.
756 void drm_helper_connector_dpms(struct drm_connector
*connector
, int mode
)
758 struct drm_encoder
*encoder
= connector
->encoder
;
759 struct drm_crtc
*crtc
= encoder
? encoder
->crtc
: NULL
;
760 int old_dpms
, encoder_dpms
= DRM_MODE_DPMS_OFF
;
762 if (mode
== connector
->dpms
)
765 old_dpms
= connector
->dpms
;
766 connector
->dpms
= mode
;
769 encoder_dpms
= drm_helper_choose_encoder_dpms(encoder
);
771 /* from off to on, do crtc then encoder */
772 if (mode
< old_dpms
) {
774 struct drm_crtc_helper_funcs
*crtc_funcs
= crtc
->helper_private
;
775 if (crtc_funcs
->dpms
)
776 (*crtc_funcs
->dpms
) (crtc
,
777 drm_helper_choose_crtc_dpms(crtc
));
780 drm_helper_encoder_dpms(encoder
, encoder_dpms
);
783 /* from on to off, do encoder then crtc */
784 if (mode
> old_dpms
) {
786 drm_helper_encoder_dpms(encoder
, encoder_dpms
);
788 struct drm_crtc_helper_funcs
*crtc_funcs
= crtc
->helper_private
;
789 if (crtc_funcs
->dpms
)
790 (*crtc_funcs
->dpms
) (crtc
,
791 drm_helper_choose_crtc_dpms(crtc
));
797 EXPORT_SYMBOL(drm_helper_connector_dpms
);
800 * drm_helper_mode_fill_fb_struct - fill out framebuffer metadata
801 * @fb: drm_framebuffer object to fill out
802 * @mode_cmd: metadata from the userspace fb creation request
804 * This helper can be used in a drivers fb_create callback to pre-fill the fb's
807 void drm_helper_mode_fill_fb_struct(struct drm_framebuffer
*fb
,
808 struct drm_mode_fb_cmd2
*mode_cmd
)
812 fb
->width
= mode_cmd
->width
;
813 fb
->height
= mode_cmd
->height
;
814 for (i
= 0; i
< 4; i
++) {
815 fb
->pitches
[i
] = mode_cmd
->pitches
[i
];
816 fb
->offsets
[i
] = mode_cmd
->offsets
[i
];
818 drm_fb_get_bpp_depth(mode_cmd
->pixel_format
, &fb
->depth
,
819 &fb
->bits_per_pixel
);
820 fb
->pixel_format
= mode_cmd
->pixel_format
;
821 fb
->flags
= mode_cmd
->flags
;
823 EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct
);
826 * drm_helper_resume_force_mode - force-restore mode setting configuration
827 * @dev: drm_device which should be restored
829 * Drivers which use the mode setting helpers can use this function to
830 * force-restore the mode setting configuration e.g. on resume or when something
831 * else might have trampled over the hw state (like some overzealous old BIOSen
834 * This helper doesn't provide a error return value since restoring the old
835 * config should never fail due to resource allocation issues since the driver
836 * has successfully set the restored configuration already. Hence this should
837 * boil down to the equivalent of a few dpms on calls, which also don't provide
840 * Drivers where simply restoring an old configuration again might fail (e.g.
841 * due to slight differences in allocating shared resources when the
842 * configuration is restored in a different order than when userspace set it up)
843 * need to use their own restore logic.
845 void drm_helper_resume_force_mode(struct drm_device
*dev
)
847 struct drm_crtc
*crtc
;
848 struct drm_encoder
*encoder
;
849 struct drm_crtc_helper_funcs
*crtc_funcs
;
853 drm_modeset_lock_all(dev
);
854 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
) {
859 ret
= drm_crtc_helper_set_mode(crtc
, &crtc
->mode
,
860 crtc
->x
, crtc
->y
, crtc
->primary
->fb
);
862 /* Restoring the old config should never fail! */
864 DRM_ERROR("failed to set mode on crtc %p\n", crtc
);
866 /* Turn off outputs that were already powered off */
867 if (drm_helper_choose_crtc_dpms(crtc
)) {
868 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
) {
870 if(encoder
->crtc
!= crtc
)
873 encoder_dpms
= drm_helper_choose_encoder_dpms(
876 drm_helper_encoder_dpms(encoder
, encoder_dpms
);
879 crtc_funcs
= crtc
->helper_private
;
880 if (crtc_funcs
->dpms
)
881 (*crtc_funcs
->dpms
) (crtc
,
882 drm_helper_choose_crtc_dpms(crtc
));
886 /* disable the unused connectors while restoring the modesetting */
887 __drm_helper_disable_unused_functions(dev
);
888 drm_modeset_unlock_all(dev
);
890 EXPORT_SYMBOL(drm_helper_resume_force_mode
);