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_crtc.h>
37 #include <drm/drm_fourcc.h>
38 #include <drm/drm_crtc_helper.h>
39 #include <drm/drm_fb_helper.h>
40 #include <drm/drm_edid.h>
42 MODULE_AUTHOR("David Airlie, Jesse Barnes");
43 MODULE_DESCRIPTION("DRM KMS helper");
44 MODULE_LICENSE("GPL and additional rights");
47 * drm_helper_move_panel_connectors_to_head() - move panels to the front in the
49 * @dev: drm device to operate on
51 * Some userspace presumes that the first connected connector is the main
52 * display, where it's supposed to display e.g. the login screen. For
53 * laptops, this should be the main panel. Use this function to sort all
54 * (eDP/LVDS) panels to the front of the connector list, instead of
55 * painstakingly trying to initialize them in the right order.
57 void drm_helper_move_panel_connectors_to_head(struct drm_device
*dev
)
59 struct drm_connector
*connector
, *tmp
;
60 struct list_head panel_list
;
62 INIT_LIST_HEAD(&panel_list
);
64 list_for_each_entry_safe(connector
, tmp
,
65 &dev
->mode_config
.connector_list
, head
) {
66 if (connector
->connector_type
== DRM_MODE_CONNECTOR_LVDS
||
67 connector
->connector_type
== DRM_MODE_CONNECTOR_eDP
)
68 list_move_tail(&connector
->head
, &panel_list
);
71 list_splice(&panel_list
, &dev
->mode_config
.connector_list
);
73 EXPORT_SYMBOL(drm_helper_move_panel_connectors_to_head
);
75 static bool drm_kms_helper_poll
= true;
76 module_param_named(poll
, drm_kms_helper_poll
, bool, 0600);
78 static void drm_mode_validate_flag(struct drm_connector
*connector
,
81 struct drm_display_mode
*mode
;
83 if (flags
== (DRM_MODE_FLAG_DBLSCAN
| DRM_MODE_FLAG_INTERLACE
|
84 DRM_MODE_FLAG_3D_MASK
))
87 list_for_each_entry(mode
, &connector
->modes
, head
) {
88 if ((mode
->flags
& DRM_MODE_FLAG_INTERLACE
) &&
89 !(flags
& DRM_MODE_FLAG_INTERLACE
))
90 mode
->status
= MODE_NO_INTERLACE
;
91 if ((mode
->flags
& DRM_MODE_FLAG_DBLSCAN
) &&
92 !(flags
& DRM_MODE_FLAG_DBLSCAN
))
93 mode
->status
= MODE_NO_DBLESCAN
;
94 if ((mode
->flags
& DRM_MODE_FLAG_3D_MASK
) &&
95 !(flags
& DRM_MODE_FLAG_3D_MASK
))
96 mode
->status
= MODE_NO_STEREO
;
103 * drm_helper_probe_single_connector_modes - get complete set of display modes
104 * @connector: connector to probe
105 * @maxX: max width for modes
106 * @maxY: max height for modes
108 * Based on the helper callbacks implemented by @connector try to detect all
109 * valid modes. Modes will first be added to the connector's probed_modes list,
110 * then culled (based on validity and the @maxX, @maxY parameters) and put into
111 * the normal modes list.
113 * Intended to be use as a generic implementation of the ->fill_modes()
114 * @connector vfunc for drivers that use the crtc helpers for output mode
115 * filtering and detection.
118 * Number of modes found on @connector.
120 int drm_helper_probe_single_connector_modes(struct drm_connector
*connector
,
121 uint32_t maxX
, uint32_t maxY
)
123 struct drm_device
*dev
= connector
->dev
;
124 struct drm_display_mode
*mode
;
125 struct drm_connector_helper_funcs
*connector_funcs
=
126 connector
->helper_private
;
129 bool verbose_prune
= true;
131 WARN_ON(!mutex_is_locked(&dev
->mode_config
.mutex
));
133 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", connector
->base
.id
,
134 drm_get_connector_name(connector
));
135 /* set all modes to the unverified state */
136 list_for_each_entry(mode
, &connector
->modes
, head
)
137 mode
->status
= MODE_UNVERIFIED
;
139 if (connector
->force
) {
140 if (connector
->force
== DRM_FORCE_ON
)
141 connector
->status
= connector_status_connected
;
143 connector
->status
= connector_status_disconnected
;
144 if (connector
->funcs
->force
)
145 connector
->funcs
->force(connector
);
147 connector
->status
= connector
->funcs
->detect(connector
, true);
150 /* Re-enable polling in case the global poll config changed. */
151 if (drm_kms_helper_poll
!= dev
->mode_config
.poll_running
)
152 drm_kms_helper_poll_enable(dev
);
154 dev
->mode_config
.poll_running
= drm_kms_helper_poll
;
156 if (connector
->status
== connector_status_disconnected
) {
157 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] disconnected\n",
158 connector
->base
.id
, drm_get_connector_name(connector
));
159 drm_mode_connector_update_edid_property(connector
, NULL
);
160 verbose_prune
= false;
164 #ifdef CONFIG_DRM_LOAD_EDID_FIRMWARE
165 count
= drm_load_edid_firmware(connector
);
168 count
= (*connector_funcs
->get_modes
)(connector
);
170 if (count
== 0 && connector
->status
== connector_status_connected
)
171 count
= drm_add_modes_noedid(connector
, 1024, 768);
175 drm_mode_connector_list_update(connector
);
178 drm_mode_validate_size(dev
, &connector
->modes
, maxX
, maxY
);
180 if (connector
->interlace_allowed
)
181 mode_flags
|= DRM_MODE_FLAG_INTERLACE
;
182 if (connector
->doublescan_allowed
)
183 mode_flags
|= DRM_MODE_FLAG_DBLSCAN
;
184 if (connector
->stereo_allowed
)
185 mode_flags
|= DRM_MODE_FLAG_3D_MASK
;
186 drm_mode_validate_flag(connector
, mode_flags
);
188 list_for_each_entry(mode
, &connector
->modes
, head
) {
189 if (mode
->status
== MODE_OK
)
190 mode
->status
= connector_funcs
->mode_valid(connector
,
195 drm_mode_prune_invalid(dev
, &connector
->modes
, verbose_prune
);
197 if (list_empty(&connector
->modes
))
200 list_for_each_entry(mode
, &connector
->modes
, head
)
201 mode
->vrefresh
= drm_mode_vrefresh(mode
);
203 drm_mode_sort(&connector
->modes
);
205 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] probed modes :\n", connector
->base
.id
,
206 drm_get_connector_name(connector
));
207 list_for_each_entry(mode
, &connector
->modes
, head
) {
208 drm_mode_set_crtcinfo(mode
, CRTC_INTERLACE_HALVE_V
);
209 drm_mode_debug_printmodeline(mode
);
214 EXPORT_SYMBOL(drm_helper_probe_single_connector_modes
);
217 * drm_helper_encoder_in_use - check if a given encoder is in use
218 * @encoder: encoder to check
220 * Walk @encoders's DRM device's mode_config and see if it's in use.
223 * True if @encoder is part of the mode_config, false otherwise.
225 bool drm_helper_encoder_in_use(struct drm_encoder
*encoder
)
227 struct drm_connector
*connector
;
228 struct drm_device
*dev
= encoder
->dev
;
230 WARN_ON(!mutex_is_locked(&dev
->mode_config
.mutex
));
231 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
)
232 if (connector
->encoder
== encoder
)
236 EXPORT_SYMBOL(drm_helper_encoder_in_use
);
239 * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config
240 * @crtc: CRTC to check
242 * Walk @crtc's DRM device's mode_config and see if it's in use.
245 * True if @crtc is part of the mode_config, false otherwise.
247 bool drm_helper_crtc_in_use(struct drm_crtc
*crtc
)
249 struct drm_encoder
*encoder
;
250 struct drm_device
*dev
= crtc
->dev
;
252 WARN_ON(!mutex_is_locked(&dev
->mode_config
.mutex
));
253 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
)
254 if (encoder
->crtc
== crtc
&& drm_helper_encoder_in_use(encoder
))
258 EXPORT_SYMBOL(drm_helper_crtc_in_use
);
261 drm_encoder_disable(struct drm_encoder
*encoder
)
263 struct drm_encoder_helper_funcs
*encoder_funcs
= encoder
->helper_private
;
266 encoder
->bridge
->funcs
->disable(encoder
->bridge
);
268 if (encoder_funcs
->disable
)
269 (*encoder_funcs
->disable
)(encoder
);
271 (*encoder_funcs
->dpms
)(encoder
, DRM_MODE_DPMS_OFF
);
274 encoder
->bridge
->funcs
->post_disable(encoder
->bridge
);
278 * drm_helper_disable_unused_functions - disable unused objects
281 * If an connector or CRTC isn't part of @dev's mode_config, it can be disabled
282 * by calling its dpms function, which should power it off.
284 void drm_helper_disable_unused_functions(struct drm_device
*dev
)
286 struct drm_encoder
*encoder
;
287 struct drm_connector
*connector
;
288 struct drm_crtc
*crtc
;
290 drm_warn_on_modeset_not_all_locked(dev
);
292 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
293 if (!connector
->encoder
)
295 if (connector
->status
== connector_status_disconnected
)
296 connector
->encoder
= NULL
;
299 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
) {
300 if (!drm_helper_encoder_in_use(encoder
)) {
301 drm_encoder_disable(encoder
);
302 /* disconnector encoder from any connector */
303 encoder
->crtc
= NULL
;
307 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
) {
308 struct drm_crtc_helper_funcs
*crtc_funcs
= crtc
->helper_private
;
309 crtc
->enabled
= drm_helper_crtc_in_use(crtc
);
310 if (!crtc
->enabled
) {
311 if (crtc_funcs
->disable
)
312 (*crtc_funcs
->disable
)(crtc
);
314 (*crtc_funcs
->dpms
)(crtc
, DRM_MODE_DPMS_OFF
);
319 EXPORT_SYMBOL(drm_helper_disable_unused_functions
);
322 * Check the CRTC we're going to map each output to vs. its current
323 * CRTC. If they don't match, we have to disable the output and the CRTC
324 * since the driver will have to re-route things.
327 drm_crtc_prepare_encoders(struct drm_device
*dev
)
329 struct drm_encoder_helper_funcs
*encoder_funcs
;
330 struct drm_encoder
*encoder
;
332 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
) {
333 encoder_funcs
= encoder
->helper_private
;
334 /* Disable unused encoders */
335 if (encoder
->crtc
== NULL
)
336 drm_encoder_disable(encoder
);
337 /* Disable encoders whose CRTC is about to change */
338 if (encoder_funcs
->get_crtc
&&
339 encoder
->crtc
!= (*encoder_funcs
->get_crtc
)(encoder
))
340 drm_encoder_disable(encoder
);
345 * drm_crtc_helper_set_mode - internal helper to set a mode
346 * @crtc: CRTC to program
348 * @x: horizontal offset into the surface
349 * @y: vertical offset into the surface
350 * @old_fb: old framebuffer, for cleanup
352 * Try to set @mode on @crtc. Give @crtc and its associated connectors a chance
353 * to fixup or reject the mode prior to trying to set it. This is an internal
354 * helper that drivers could e.g. use to update properties that require the
355 * entire output pipe to be disabled and re-enabled in a new configuration. For
356 * example for changing whether audio is enabled on a hdmi link or for changing
357 * panel fitter or dither attributes. It is also called by the
358 * drm_crtc_helper_set_config() helper function to drive the mode setting
362 * True if the mode was set successfully, or false otherwise.
364 bool drm_crtc_helper_set_mode(struct drm_crtc
*crtc
,
365 struct drm_display_mode
*mode
,
367 struct drm_framebuffer
*old_fb
)
369 struct drm_device
*dev
= crtc
->dev
;
370 struct drm_display_mode
*adjusted_mode
, saved_mode
;
371 struct drm_crtc_helper_funcs
*crtc_funcs
= crtc
->helper_private
;
372 struct drm_encoder_helper_funcs
*encoder_funcs
;
373 int saved_x
, saved_y
;
375 struct drm_encoder
*encoder
;
378 drm_warn_on_modeset_not_all_locked(dev
);
380 saved_enabled
= crtc
->enabled
;
381 crtc
->enabled
= drm_helper_crtc_in_use(crtc
);
385 adjusted_mode
= drm_mode_duplicate(dev
, mode
);
386 if (!adjusted_mode
) {
387 crtc
->enabled
= saved_enabled
;
391 saved_mode
= crtc
->mode
;
395 /* Update crtc values up front so the driver can rely on them for mode
402 /* Pass our mode to the connectors and the CRTC to give them a chance to
403 * adjust it according to limitations or connector properties, and also
404 * a chance to reject the mode entirely.
406 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
) {
408 if (encoder
->crtc
!= crtc
)
411 if (encoder
->bridge
&& encoder
->bridge
->funcs
->mode_fixup
) {
412 ret
= encoder
->bridge
->funcs
->mode_fixup(
413 encoder
->bridge
, mode
, adjusted_mode
);
415 DRM_DEBUG_KMS("Bridge fixup failed\n");
420 encoder_funcs
= encoder
->helper_private
;
421 if (!(ret
= encoder_funcs
->mode_fixup(encoder
, mode
,
423 DRM_DEBUG_KMS("Encoder fixup failed\n");
428 if (!(ret
= crtc_funcs
->mode_fixup(crtc
, mode
, adjusted_mode
))) {
429 DRM_DEBUG_KMS("CRTC fixup failed\n");
432 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc
->base
.id
);
434 /* Prepare the encoders and CRTCs before setting the mode. */
435 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
) {
437 if (encoder
->crtc
!= crtc
)
441 encoder
->bridge
->funcs
->disable(encoder
->bridge
);
443 encoder_funcs
= encoder
->helper_private
;
444 /* Disable the encoders as the first thing we do. */
445 encoder_funcs
->prepare(encoder
);
448 encoder
->bridge
->funcs
->post_disable(encoder
->bridge
);
451 drm_crtc_prepare_encoders(dev
);
453 crtc_funcs
->prepare(crtc
);
455 /* Set up the DPLL and any encoders state that needs to adjust or depend
458 ret
= !crtc_funcs
->mode_set(crtc
, mode
, adjusted_mode
, x
, y
, old_fb
);
462 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
) {
464 if (encoder
->crtc
!= crtc
)
467 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
468 encoder
->base
.id
, drm_get_encoder_name(encoder
),
469 mode
->base
.id
, mode
->name
);
470 encoder_funcs
= encoder
->helper_private
;
471 encoder_funcs
->mode_set(encoder
, mode
, adjusted_mode
);
473 if (encoder
->bridge
&& encoder
->bridge
->funcs
->mode_set
)
474 encoder
->bridge
->funcs
->mode_set(encoder
->bridge
, mode
,
478 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
479 crtc_funcs
->commit(crtc
);
481 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
) {
483 if (encoder
->crtc
!= crtc
)
487 encoder
->bridge
->funcs
->pre_enable(encoder
->bridge
);
489 encoder_funcs
= encoder
->helper_private
;
490 encoder_funcs
->commit(encoder
);
493 encoder
->bridge
->funcs
->enable(encoder
->bridge
);
496 /* Store real post-adjustment hardware mode. */
497 crtc
->hwmode
= *adjusted_mode
;
499 /* Calculate and store various constants which
500 * are later needed by vblank and swap-completion
501 * timestamping. They are derived from true hwmode.
503 drm_calc_timestamping_constants(crtc
, &crtc
->hwmode
);
505 /* FIXME: add subpixel order */
507 drm_mode_destroy(dev
, adjusted_mode
);
509 crtc
->enabled
= saved_enabled
;
510 crtc
->mode
= saved_mode
;
517 EXPORT_SYMBOL(drm_crtc_helper_set_mode
);
521 drm_crtc_helper_disable(struct drm_crtc
*crtc
)
523 struct drm_device
*dev
= crtc
->dev
;
524 struct drm_connector
*connector
;
525 struct drm_encoder
*encoder
;
527 /* Decouple all encoders and their attached connectors from this crtc */
528 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
) {
529 if (encoder
->crtc
!= crtc
)
532 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
533 if (connector
->encoder
!= encoder
)
536 connector
->encoder
= NULL
;
539 * drm_helper_disable_unused_functions() ought to be
540 * doing this, but since we've decoupled the encoder
541 * from the connector above, the required connection
542 * between them is henceforth no longer available.
544 connector
->dpms
= DRM_MODE_DPMS_OFF
;
548 drm_helper_disable_unused_functions(dev
);
553 * drm_crtc_helper_set_config - set a new config from userspace
554 * @set: mode set configuration
556 * Setup a new configuration, provided by the upper layers (either an ioctl call
557 * from userspace or internally e.g. from the fbdev suppport code) in @set, and
558 * enable it. This is the main helper functions for drivers that implement
559 * kernel mode setting with the crtc helper functions and the assorted
560 * ->prepare(), ->modeset() and ->commit() helper callbacks.
563 * Returns 0 on success, -ERRNO on failure.
565 int drm_crtc_helper_set_config(struct drm_mode_set
*set
)
567 struct drm_device
*dev
;
568 struct drm_crtc
*new_crtc
;
569 struct drm_encoder
*save_encoders
, *new_encoder
, *encoder
;
570 bool mode_changed
= false; /* if true do a full mode set */
571 bool fb_changed
= false; /* if true and !mode_changed just do a flip */
572 struct drm_connector
*save_connectors
, *connector
;
573 int count
= 0, ro
, fail
= 0;
574 struct drm_crtc_helper_funcs
*crtc_funcs
;
575 struct drm_mode_set save_set
;
583 BUG_ON(!set
->crtc
->helper_private
);
585 /* Enforce sane interface api - has been abused by the fb helper. */
586 BUG_ON(!set
->mode
&& set
->fb
);
587 BUG_ON(set
->fb
&& set
->num_connectors
== 0);
589 crtc_funcs
= set
->crtc
->helper_private
;
595 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
596 set
->crtc
->base
.id
, set
->fb
->base
.id
,
597 (int)set
->num_connectors
, set
->x
, set
->y
);
599 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set
->crtc
->base
.id
);
600 return drm_crtc_helper_disable(set
->crtc
);
603 dev
= set
->crtc
->dev
;
605 drm_warn_on_modeset_not_all_locked(dev
);
608 * Allocate space for the backup of all (non-pointer) encoder and
611 save_encoders
= kzalloc(dev
->mode_config
.num_encoder
*
612 sizeof(struct drm_encoder
), GFP_KERNEL
);
616 save_connectors
= kzalloc(dev
->mode_config
.num_connector
*
617 sizeof(struct drm_connector
), GFP_KERNEL
);
618 if (!save_connectors
) {
619 kfree(save_encoders
);
624 * Copy data. Note that driver private data is not affected.
625 * Should anything bad happen only the expected state is
626 * restored, not the drivers personal bookkeeping.
629 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
) {
630 save_encoders
[count
++] = *encoder
;
634 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
635 save_connectors
[count
++] = *connector
;
638 save_set
.crtc
= set
->crtc
;
639 save_set
.mode
= &set
->crtc
->mode
;
640 save_set
.x
= set
->crtc
->x
;
641 save_set
.y
= set
->crtc
->y
;
642 save_set
.fb
= set
->crtc
->fb
;
644 /* We should be able to check here if the fb has the same properties
645 * and then just flip_or_move it */
646 if (set
->crtc
->fb
!= set
->fb
) {
647 /* If we have no fb then treat it as a full mode set */
648 if (set
->crtc
->fb
== NULL
) {
649 DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
651 } else if (set
->fb
== NULL
) {
653 } else if (set
->fb
->pixel_format
!=
654 set
->crtc
->fb
->pixel_format
) {
660 if (set
->x
!= set
->crtc
->x
|| set
->y
!= set
->crtc
->y
)
663 if (set
->mode
&& !drm_mode_equal(set
->mode
, &set
->crtc
->mode
)) {
664 DRM_DEBUG_KMS("modes are different, full mode set\n");
665 drm_mode_debug_printmodeline(&set
->crtc
->mode
);
666 drm_mode_debug_printmodeline(set
->mode
);
670 /* a) traverse passed in connector list and get encoders for them */
672 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
673 struct drm_connector_helper_funcs
*connector_funcs
=
674 connector
->helper_private
;
675 new_encoder
= connector
->encoder
;
676 for (ro
= 0; ro
< set
->num_connectors
; ro
++) {
677 if (set
->connectors
[ro
] == connector
) {
678 new_encoder
= connector_funcs
->best_encoder(connector
);
679 /* if we can't get an encoder for a connector
680 we are setting now - then fail */
681 if (new_encoder
== NULL
)
682 /* don't break so fail path works correct */
686 if (connector
->dpms
!= DRM_MODE_DPMS_ON
) {
687 DRM_DEBUG_KMS("connector dpms not on, full mode switch\n");
693 if (new_encoder
!= connector
->encoder
) {
694 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
696 /* If the encoder is reused for another connector, then
697 * the appropriate crtc will be set later.
699 if (connector
->encoder
)
700 connector
->encoder
->crtc
= NULL
;
701 connector
->encoder
= new_encoder
;
711 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
712 if (!connector
->encoder
)
715 if (connector
->encoder
->crtc
== set
->crtc
)
718 new_crtc
= connector
->encoder
->crtc
;
720 for (ro
= 0; ro
< set
->num_connectors
; ro
++) {
721 if (set
->connectors
[ro
] == connector
)
722 new_crtc
= set
->crtc
;
725 /* Make sure the new CRTC will work with the encoder */
727 !drm_encoder_crtc_ok(connector
->encoder
, new_crtc
)) {
731 if (new_crtc
!= connector
->encoder
->crtc
) {
732 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
734 connector
->encoder
->crtc
= new_crtc
;
737 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
738 connector
->base
.id
, drm_get_connector_name(connector
),
741 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
742 connector
->base
.id
, drm_get_connector_name(connector
));
746 /* mode_set_base is not a required function */
747 if (fb_changed
&& !crtc_funcs
->mode_set_base
)
751 if (drm_helper_crtc_in_use(set
->crtc
)) {
752 DRM_DEBUG_KMS("attempting to set mode from"
754 drm_mode_debug_printmodeline(set
->mode
);
755 set
->crtc
->fb
= set
->fb
;
756 if (!drm_crtc_helper_set_mode(set
->crtc
, set
->mode
,
759 DRM_ERROR("failed to set mode on [CRTC:%d]\n",
761 set
->crtc
->fb
= save_set
.fb
;
765 DRM_DEBUG_KMS("Setting connector DPMS state to on\n");
766 for (i
= 0; i
< set
->num_connectors
; i
++) {
767 DRM_DEBUG_KMS("\t[CONNECTOR:%d:%s] set DPMS on\n", set
->connectors
[i
]->base
.id
,
768 drm_get_connector_name(set
->connectors
[i
]));
769 set
->connectors
[i
]->funcs
->dpms(set
->connectors
[i
], DRM_MODE_DPMS_ON
);
772 drm_helper_disable_unused_functions(dev
);
773 } else if (fb_changed
) {
774 set
->crtc
->x
= set
->x
;
775 set
->crtc
->y
= set
->y
;
776 set
->crtc
->fb
= set
->fb
;
777 ret
= crtc_funcs
->mode_set_base(set
->crtc
,
778 set
->x
, set
->y
, save_set
.fb
);
780 set
->crtc
->x
= save_set
.x
;
781 set
->crtc
->y
= save_set
.y
;
782 set
->crtc
->fb
= save_set
.fb
;
787 kfree(save_connectors
);
788 kfree(save_encoders
);
792 /* Restore all previous data. */
794 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
) {
795 *encoder
= save_encoders
[count
++];
799 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
800 *connector
= save_connectors
[count
++];
803 /* Try to restore the config */
805 !drm_crtc_helper_set_mode(save_set
.crtc
, save_set
.mode
, save_set
.x
,
806 save_set
.y
, save_set
.fb
))
807 DRM_ERROR("failed to restore config after modeset failure\n");
809 kfree(save_connectors
);
810 kfree(save_encoders
);
813 EXPORT_SYMBOL(drm_crtc_helper_set_config
);
815 static int drm_helper_choose_encoder_dpms(struct drm_encoder
*encoder
)
817 int dpms
= DRM_MODE_DPMS_OFF
;
818 struct drm_connector
*connector
;
819 struct drm_device
*dev
= encoder
->dev
;
821 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
)
822 if (connector
->encoder
== encoder
)
823 if (connector
->dpms
< dpms
)
824 dpms
= connector
->dpms
;
828 /* Helper which handles bridge ordering around encoder dpms */
829 static void drm_helper_encoder_dpms(struct drm_encoder
*encoder
, int mode
)
831 struct drm_bridge
*bridge
= encoder
->bridge
;
832 struct drm_encoder_helper_funcs
*encoder_funcs
;
835 if (mode
== DRM_MODE_DPMS_ON
)
836 bridge
->funcs
->pre_enable(bridge
);
838 bridge
->funcs
->disable(bridge
);
841 encoder_funcs
= encoder
->helper_private
;
842 if (encoder_funcs
->dpms
)
843 encoder_funcs
->dpms(encoder
, mode
);
846 if (mode
== DRM_MODE_DPMS_ON
)
847 bridge
->funcs
->enable(bridge
);
849 bridge
->funcs
->post_disable(bridge
);
853 static int drm_helper_choose_crtc_dpms(struct drm_crtc
*crtc
)
855 int dpms
= DRM_MODE_DPMS_OFF
;
856 struct drm_connector
*connector
;
857 struct drm_device
*dev
= crtc
->dev
;
859 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
)
860 if (connector
->encoder
&& connector
->encoder
->crtc
== crtc
)
861 if (connector
->dpms
< dpms
)
862 dpms
= connector
->dpms
;
867 * drm_helper_connector_dpms() - connector dpms helper implementation
868 * @connector: affected connector
871 * This is the main helper function provided by the crtc helper framework for
872 * implementing the DPMS connector attribute. It computes the new desired DPMS
873 * state for all encoders and crtcs in the output mesh and calls the ->dpms()
874 * callback provided by the driver appropriately.
876 void drm_helper_connector_dpms(struct drm_connector
*connector
, int mode
)
878 struct drm_encoder
*encoder
= connector
->encoder
;
879 struct drm_crtc
*crtc
= encoder
? encoder
->crtc
: NULL
;
880 int old_dpms
, encoder_dpms
= DRM_MODE_DPMS_OFF
;
882 if (mode
== connector
->dpms
)
885 old_dpms
= connector
->dpms
;
886 connector
->dpms
= mode
;
889 encoder_dpms
= drm_helper_choose_encoder_dpms(encoder
);
891 /* from off to on, do crtc then encoder */
892 if (mode
< old_dpms
) {
894 struct drm_crtc_helper_funcs
*crtc_funcs
= crtc
->helper_private
;
895 if (crtc_funcs
->dpms
)
896 (*crtc_funcs
->dpms
) (crtc
,
897 drm_helper_choose_crtc_dpms(crtc
));
900 drm_helper_encoder_dpms(encoder
, encoder_dpms
);
903 /* from on to off, do encoder then crtc */
904 if (mode
> old_dpms
) {
906 drm_helper_encoder_dpms(encoder
, encoder_dpms
);
908 struct drm_crtc_helper_funcs
*crtc_funcs
= crtc
->helper_private
;
909 if (crtc_funcs
->dpms
)
910 (*crtc_funcs
->dpms
) (crtc
,
911 drm_helper_choose_crtc_dpms(crtc
));
917 EXPORT_SYMBOL(drm_helper_connector_dpms
);
919 void drm_helper_mode_fill_fb_struct(struct drm_framebuffer
*fb
,
920 struct drm_mode_fb_cmd2
*mode_cmd
)
924 fb
->width
= mode_cmd
->width
;
925 fb
->height
= mode_cmd
->height
;
926 for (i
= 0; i
< 4; i
++) {
927 fb
->pitches
[i
] = mode_cmd
->pitches
[i
];
928 fb
->offsets
[i
] = mode_cmd
->offsets
[i
];
930 drm_fb_get_bpp_depth(mode_cmd
->pixel_format
, &fb
->depth
,
931 &fb
->bits_per_pixel
);
932 fb
->pixel_format
= mode_cmd
->pixel_format
;
934 EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct
);
937 * drm_helper_resume_force_mode - force-restore mode setting configuration
938 * @dev: drm_device which should be restored
940 * Drivers which use the mode setting helpers can use this function to
941 * force-restore the mode setting configuration e.g. on resume or when something
942 * else might have trampled over the hw state (like some overzealous old BIOSen
945 int drm_helper_resume_force_mode(struct drm_device
*dev
)
947 struct drm_crtc
*crtc
;
948 struct drm_encoder
*encoder
;
949 struct drm_crtc_helper_funcs
*crtc_funcs
;
950 int ret
, encoder_dpms
;
952 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
) {
957 ret
= drm_crtc_helper_set_mode(crtc
, &crtc
->mode
,
958 crtc
->x
, crtc
->y
, crtc
->fb
);
961 DRM_ERROR("failed to set mode on crtc %p\n", crtc
);
963 /* Turn off outputs that were already powered off */
964 if (drm_helper_choose_crtc_dpms(crtc
)) {
965 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
) {
967 if(encoder
->crtc
!= crtc
)
970 encoder_dpms
= drm_helper_choose_encoder_dpms(
973 drm_helper_encoder_dpms(encoder
, encoder_dpms
);
976 crtc_funcs
= crtc
->helper_private
;
977 if (crtc_funcs
->dpms
)
978 (*crtc_funcs
->dpms
) (crtc
,
979 drm_helper_choose_crtc_dpms(crtc
));
982 /* disable the unused connectors while restoring the modesetting */
983 drm_helper_disable_unused_functions(dev
);
986 EXPORT_SYMBOL(drm_helper_resume_force_mode
);
988 void drm_kms_helper_hotplug_event(struct drm_device
*dev
)
990 /* send a uevent + call fbdev */
991 drm_sysfs_hotplug_event(dev
);
992 if (dev
->mode_config
.funcs
->output_poll_changed
)
993 dev
->mode_config
.funcs
->output_poll_changed(dev
);
995 EXPORT_SYMBOL(drm_kms_helper_hotplug_event
);
997 #define DRM_OUTPUT_POLL_PERIOD (10*HZ)
998 static void output_poll_execute(struct work_struct
*work
)
1000 struct delayed_work
*delayed_work
= to_delayed_work(work
);
1001 struct drm_device
*dev
= container_of(delayed_work
, struct drm_device
, mode_config
.output_poll_work
);
1002 struct drm_connector
*connector
;
1003 enum drm_connector_status old_status
;
1004 bool repoll
= false, changed
= false;
1006 if (!drm_kms_helper_poll
)
1009 mutex_lock(&dev
->mode_config
.mutex
);
1010 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
1012 /* Ignore forced connectors. */
1013 if (connector
->force
)
1016 /* Ignore HPD capable connectors and connectors where we don't
1017 * want any hotplug detection at all for polling. */
1018 if (!connector
->polled
|| connector
->polled
== DRM_CONNECTOR_POLL_HPD
)
1023 old_status
= connector
->status
;
1024 /* if we are connected and don't want to poll for disconnect
1026 if (old_status
== connector_status_connected
&&
1027 !(connector
->polled
& DRM_CONNECTOR_POLL_DISCONNECT
))
1030 connector
->status
= connector
->funcs
->detect(connector
, false);
1031 if (old_status
!= connector
->status
) {
1032 const char *old
, *new;
1034 old
= drm_get_connector_status_name(old_status
);
1035 new = drm_get_connector_status_name(connector
->status
);
1037 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] "
1038 "status updated from %s to %s\n",
1040 drm_get_connector_name(connector
),
1047 mutex_unlock(&dev
->mode_config
.mutex
);
1050 drm_kms_helper_hotplug_event(dev
);
1053 schedule_delayed_work(delayed_work
, DRM_OUTPUT_POLL_PERIOD
);
1056 void drm_kms_helper_poll_disable(struct drm_device
*dev
)
1058 if (!dev
->mode_config
.poll_enabled
)
1060 cancel_delayed_work_sync(&dev
->mode_config
.output_poll_work
);
1062 EXPORT_SYMBOL(drm_kms_helper_poll_disable
);
1064 void drm_kms_helper_poll_enable(struct drm_device
*dev
)
1067 struct drm_connector
*connector
;
1069 if (!dev
->mode_config
.poll_enabled
|| !drm_kms_helper_poll
)
1072 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
1073 if (connector
->polled
& (DRM_CONNECTOR_POLL_CONNECT
|
1074 DRM_CONNECTOR_POLL_DISCONNECT
))
1079 schedule_delayed_work(&dev
->mode_config
.output_poll_work
, DRM_OUTPUT_POLL_PERIOD
);
1081 EXPORT_SYMBOL(drm_kms_helper_poll_enable
);
1083 void drm_kms_helper_poll_init(struct drm_device
*dev
)
1085 INIT_DELAYED_WORK(&dev
->mode_config
.output_poll_work
, output_poll_execute
);
1086 dev
->mode_config
.poll_enabled
= true;
1088 drm_kms_helper_poll_enable(dev
);
1090 EXPORT_SYMBOL(drm_kms_helper_poll_init
);
1092 void drm_kms_helper_poll_fini(struct drm_device
*dev
)
1094 drm_kms_helper_poll_disable(dev
);
1096 EXPORT_SYMBOL(drm_kms_helper_poll_fini
);
1098 bool drm_helper_hpd_irq_event(struct drm_device
*dev
)
1100 struct drm_connector
*connector
;
1101 enum drm_connector_status old_status
;
1102 bool changed
= false;
1104 if (!dev
->mode_config
.poll_enabled
)
1107 mutex_lock(&dev
->mode_config
.mutex
);
1108 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
1110 /* Only handle HPD capable connectors. */
1111 if (!(connector
->polled
& DRM_CONNECTOR_POLL_HPD
))
1114 old_status
= connector
->status
;
1116 connector
->status
= connector
->funcs
->detect(connector
, false);
1117 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
1119 drm_get_connector_name(connector
),
1120 drm_get_connector_status_name(old_status
),
1121 drm_get_connector_status_name(connector
->status
));
1122 if (old_status
!= connector
->status
)
1126 mutex_unlock(&dev
->mode_config
.mutex
);
1129 drm_kms_helper_hotplug_event(dev
);
1133 EXPORT_SYMBOL(drm_helper_hpd_irq_event
);