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>
34 #include "drm_crtc_helper.h"
35 #include "drm_fb_helper.h"
37 static void drm_mode_validate_flag(struct drm_connector
*connector
,
40 struct drm_display_mode
*mode
, *t
;
42 if (flags
== (DRM_MODE_FLAG_DBLSCAN
| DRM_MODE_FLAG_INTERLACE
))
45 list_for_each_entry_safe(mode
, t
, &connector
->modes
, head
) {
46 if ((mode
->flags
& DRM_MODE_FLAG_INTERLACE
) &&
47 !(flags
& DRM_MODE_FLAG_INTERLACE
))
48 mode
->status
= MODE_NO_INTERLACE
;
49 if ((mode
->flags
& DRM_MODE_FLAG_DBLSCAN
) &&
50 !(flags
& DRM_MODE_FLAG_DBLSCAN
))
51 mode
->status
= MODE_NO_DBLESCAN
;
58 * drm_helper_probe_connector_modes - get complete set of display modes
60 * @maxX: max width for modes
61 * @maxY: max height for modes
64 * Caller must hold mode config lock.
66 * Based on @dev's mode_config layout, scan all the connectors and try to detect
67 * modes on them. Modes will first be added to the connector's probed_modes
68 * list, then culled (based on validity and the @maxX, @maxY parameters) and
69 * put into the normal modes list.
71 * Intended to be used either at bootup time or when major configuration
72 * changes have occurred.
74 * FIXME: take into account monitor limits
77 * Number of modes found on @connector.
79 int drm_helper_probe_single_connector_modes(struct drm_connector
*connector
,
80 uint32_t maxX
, uint32_t maxY
)
82 struct drm_device
*dev
= connector
->dev
;
83 struct drm_display_mode
*mode
, *t
;
84 struct drm_connector_helper_funcs
*connector_funcs
=
85 connector
->helper_private
;
89 DRM_DEBUG_KMS("%s\n", drm_get_connector_name(connector
));
90 /* set all modes to the unverified state */
91 list_for_each_entry_safe(mode
, t
, &connector
->modes
, head
)
92 mode
->status
= MODE_UNVERIFIED
;
94 if (connector
->force
) {
95 if (connector
->force
== DRM_FORCE_ON
)
96 connector
->status
= connector_status_connected
;
98 connector
->status
= connector_status_disconnected
;
99 if (connector
->funcs
->force
)
100 connector
->funcs
->force(connector
);
102 connector
->status
= connector
->funcs
->detect(connector
);
104 if (connector
->status
== connector_status_disconnected
) {
105 DRM_DEBUG_KMS("%s is disconnected\n",
106 drm_get_connector_name(connector
));
107 drm_mode_connector_update_edid_property(connector
, NULL
);
111 count
= (*connector_funcs
->get_modes
)(connector
);
113 count
= drm_add_modes_noedid(connector
, 1024, 768);
118 drm_mode_connector_list_update(connector
);
121 drm_mode_validate_size(dev
, &connector
->modes
, maxX
,
124 if (connector
->interlace_allowed
)
125 mode_flags
|= DRM_MODE_FLAG_INTERLACE
;
126 if (connector
->doublescan_allowed
)
127 mode_flags
|= DRM_MODE_FLAG_DBLSCAN
;
128 drm_mode_validate_flag(connector
, mode_flags
);
130 list_for_each_entry_safe(mode
, t
, &connector
->modes
, head
) {
131 if (mode
->status
== MODE_OK
)
132 mode
->status
= connector_funcs
->mode_valid(connector
,
137 drm_mode_prune_invalid(dev
, &connector
->modes
, true);
139 if (list_empty(&connector
->modes
))
142 drm_mode_sort(&connector
->modes
);
144 DRM_DEBUG_KMS("Probed modes for %s\n",
145 drm_get_connector_name(connector
));
146 list_for_each_entry_safe(mode
, t
, &connector
->modes
, head
) {
147 mode
->vrefresh
= drm_mode_vrefresh(mode
);
149 drm_mode_set_crtcinfo(mode
, CRTC_INTERLACE_HALVE_V
);
150 drm_mode_debug_printmodeline(mode
);
155 EXPORT_SYMBOL(drm_helper_probe_single_connector_modes
);
157 int drm_helper_probe_connector_modes(struct drm_device
*dev
, uint32_t maxX
,
160 struct drm_connector
*connector
;
163 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
164 count
+= drm_helper_probe_single_connector_modes(connector
,
170 EXPORT_SYMBOL(drm_helper_probe_connector_modes
);
173 * drm_helper_encoder_in_use - check if a given encoder is in use
174 * @encoder: encoder to check
177 * Caller must hold mode config lock.
179 * Walk @encoders's DRM device's mode_config and see if it's in use.
182 * True if @encoder is part of the mode_config, false otherwise.
184 bool drm_helper_encoder_in_use(struct drm_encoder
*encoder
)
186 struct drm_connector
*connector
;
187 struct drm_device
*dev
= encoder
->dev
;
188 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
)
189 if (connector
->encoder
== encoder
)
193 EXPORT_SYMBOL(drm_helper_encoder_in_use
);
196 * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config
197 * @crtc: CRTC to check
200 * Caller must hold mode config lock.
202 * Walk @crtc's DRM device's mode_config and see if it's in use.
205 * True if @crtc is part of the mode_config, false otherwise.
207 bool drm_helper_crtc_in_use(struct drm_crtc
*crtc
)
209 struct drm_encoder
*encoder
;
210 struct drm_device
*dev
= crtc
->dev
;
211 /* FIXME: Locking around list access? */
212 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
)
213 if (encoder
->crtc
== crtc
&& drm_helper_encoder_in_use(encoder
))
217 EXPORT_SYMBOL(drm_helper_crtc_in_use
);
220 * drm_helper_disable_unused_functions - disable unused objects
224 * Caller must hold mode config lock.
226 * If an connector or CRTC isn't part of @dev's mode_config, it can be disabled
227 * by calling its dpms function, which should power it off.
229 void drm_helper_disable_unused_functions(struct drm_device
*dev
)
231 struct drm_encoder
*encoder
;
232 struct drm_connector
*connector
;
233 struct drm_encoder_helper_funcs
*encoder_funcs
;
234 struct drm_crtc
*crtc
;
236 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
237 if (!connector
->encoder
)
239 if (connector
->status
== connector_status_disconnected
)
240 connector
->encoder
= NULL
;
243 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
) {
244 encoder_funcs
= encoder
->helper_private
;
245 if (!drm_helper_encoder_in_use(encoder
)) {
246 if (encoder_funcs
->disable
)
247 (*encoder_funcs
->disable
)(encoder
);
249 (*encoder_funcs
->dpms
)(encoder
, DRM_MODE_DPMS_OFF
);
250 /* disconnector encoder from any connector */
251 encoder
->crtc
= NULL
;
255 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
) {
256 struct drm_crtc_helper_funcs
*crtc_funcs
= crtc
->helper_private
;
257 crtc
->enabled
= drm_helper_crtc_in_use(crtc
);
258 if (!crtc
->enabled
) {
259 crtc_funcs
->dpms(crtc
, DRM_MODE_DPMS_OFF
);
264 EXPORT_SYMBOL(drm_helper_disable_unused_functions
);
266 static struct drm_display_mode
*drm_has_preferred_mode(struct drm_connector
*connector
, int width
, int height
)
268 struct drm_display_mode
*mode
;
270 list_for_each_entry(mode
, &connector
->modes
, head
) {
271 if (drm_mode_width(mode
) > width
||
272 drm_mode_height(mode
) > height
)
274 if (mode
->type
& DRM_MODE_TYPE_PREFERRED
)
280 static bool drm_has_cmdline_mode(struct drm_connector
*connector
)
282 struct drm_fb_helper_connector
*fb_help_conn
= connector
->fb_helper_private
;
283 struct drm_fb_helper_cmdline_mode
*cmdline_mode
;
288 cmdline_mode
= &fb_help_conn
->cmdline_mode
;
289 return cmdline_mode
->specified
;
292 static struct drm_display_mode
*drm_pick_cmdline_mode(struct drm_connector
*connector
, int width
, int height
)
294 struct drm_fb_helper_connector
*fb_help_conn
= connector
->fb_helper_private
;
295 struct drm_fb_helper_cmdline_mode
*cmdline_mode
;
296 struct drm_display_mode
*mode
= NULL
;
301 cmdline_mode
= &fb_help_conn
->cmdline_mode
;
302 if (cmdline_mode
->specified
== false)
305 /* attempt to find a matching mode in the list of modes
306 * we have gotten so far, if not add a CVT mode that conforms
308 if (cmdline_mode
->rb
|| cmdline_mode
->margins
)
311 list_for_each_entry(mode
, &connector
->modes
, head
) {
312 /* check width/height */
313 if (mode
->hdisplay
!= cmdline_mode
->xres
||
314 mode
->vdisplay
!= cmdline_mode
->yres
)
317 if (cmdline_mode
->refresh_specified
) {
318 if (mode
->vrefresh
!= cmdline_mode
->refresh
)
322 if (cmdline_mode
->interlace
) {
323 if (!(mode
->flags
& DRM_MODE_FLAG_INTERLACE
))
330 mode
= drm_cvt_mode(connector
->dev
, cmdline_mode
->xres
,
332 cmdline_mode
->refresh_specified
? cmdline_mode
->refresh
: 60,
333 cmdline_mode
->rb
, cmdline_mode
->interlace
,
334 cmdline_mode
->margins
);
335 drm_mode_set_crtcinfo(mode
, CRTC_INTERLACE_HALVE_V
);
336 list_add(&mode
->head
, &connector
->modes
);
340 static bool drm_connector_enabled(struct drm_connector
*connector
, bool strict
)
345 enable
= connector
->status
== connector_status_connected
;
347 enable
= connector
->status
!= connector_status_disconnected
;
352 static void drm_enable_connectors(struct drm_device
*dev
, bool *enabled
)
354 bool any_enabled
= false;
355 struct drm_connector
*connector
;
358 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
359 enabled
[i
] = drm_connector_enabled(connector
, true);
360 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector
->base
.id
,
361 enabled
[i
] ? "yes" : "no");
362 any_enabled
|= enabled
[i
];
370 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
371 enabled
[i
] = drm_connector_enabled(connector
, false);
376 static bool drm_target_preferred(struct drm_device
*dev
,
377 struct drm_display_mode
**modes
,
378 bool *enabled
, int width
, int height
)
380 struct drm_connector
*connector
;
383 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
385 if (enabled
[i
] == false) {
390 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
393 /* got for command line mode first */
394 modes
[i
] = drm_pick_cmdline_mode(connector
, width
, height
);
396 DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
398 modes
[i
] = drm_has_preferred_mode(connector
, width
, height
);
400 /* No preferred modes, pick one off the list */
401 if (!modes
[i
] && !list_empty(&connector
->modes
)) {
402 list_for_each_entry(modes
[i
], &connector
->modes
, head
)
405 DRM_DEBUG_KMS("found mode %s\n", modes
[i
] ? modes
[i
]->name
:
412 static int drm_pick_crtcs(struct drm_device
*dev
,
413 struct drm_crtc
**best_crtcs
,
414 struct drm_display_mode
**modes
,
415 int n
, int width
, int height
)
418 struct drm_connector
*connector
;
419 struct drm_connector_helper_funcs
*connector_funcs
;
420 struct drm_encoder
*encoder
;
421 struct drm_crtc
*best_crtc
;
422 int my_score
, best_score
, score
;
423 struct drm_crtc
**crtcs
, *crtc
;
425 if (n
== dev
->mode_config
.num_connector
)
428 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
434 best_crtcs
[n
] = NULL
;
436 best_score
= drm_pick_crtcs(dev
, best_crtcs
, modes
, n
+1, width
, height
);
437 if (modes
[n
] == NULL
)
440 crtcs
= kmalloc(dev
->mode_config
.num_connector
*
441 sizeof(struct drm_crtc
*), GFP_KERNEL
);
446 if (connector
->status
== connector_status_connected
)
448 if (drm_has_cmdline_mode(connector
))
450 if (drm_has_preferred_mode(connector
, width
, height
))
453 connector_funcs
= connector
->helper_private
;
454 encoder
= connector_funcs
->best_encoder(connector
);
458 connector
->encoder
= encoder
;
460 /* select a crtc for this connector and then attempt to configure
461 remaining connectors */
463 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
) {
465 if ((encoder
->possible_crtcs
& (1 << c
)) == 0) {
470 for (o
= 0; o
< n
; o
++)
471 if (best_crtcs
[o
] == crtc
)
475 /* ignore cloning for now */
481 memcpy(crtcs
, best_crtcs
, n
* sizeof(struct drm_crtc
*));
482 score
= my_score
+ drm_pick_crtcs(dev
, crtcs
, modes
, n
+ 1,
484 if (score
> best_score
) {
487 memcpy(best_crtcs
, crtcs
,
488 dev
->mode_config
.num_connector
*
489 sizeof(struct drm_crtc
*));
498 static void drm_setup_crtcs(struct drm_device
*dev
)
500 struct drm_crtc
**crtcs
;
501 struct drm_display_mode
**modes
;
502 struct drm_encoder
*encoder
;
503 struct drm_connector
*connector
;
510 width
= dev
->mode_config
.max_width
;
511 height
= dev
->mode_config
.max_height
;
513 /* clean out all the encoder/crtc combos */
514 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
) {
515 encoder
->crtc
= NULL
;
518 crtcs
= kcalloc(dev
->mode_config
.num_connector
,
519 sizeof(struct drm_crtc
*), GFP_KERNEL
);
520 modes
= kcalloc(dev
->mode_config
.num_connector
,
521 sizeof(struct drm_display_mode
*), GFP_KERNEL
);
522 enabled
= kcalloc(dev
->mode_config
.num_connector
,
523 sizeof(bool), GFP_KERNEL
);
525 drm_enable_connectors(dev
, enabled
);
527 ret
= drm_target_preferred(dev
, modes
, enabled
, width
, height
);
529 DRM_ERROR("Unable to find initial modes\n");
531 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width
, height
);
533 drm_pick_crtcs(dev
, crtcs
, modes
, 0, width
, height
);
536 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
537 struct drm_display_mode
*mode
= modes
[i
];
538 struct drm_crtc
*crtc
= crtcs
[i
];
540 if (connector
->encoder
== NULL
) {
546 DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
547 mode
->name
, crtc
->base
.id
);
548 crtc
->desired_mode
= mode
;
549 connector
->encoder
->crtc
= crtc
;
551 connector
->encoder
->crtc
= NULL
;
552 connector
->encoder
= NULL
;
563 * drm_encoder_crtc_ok - can a given crtc drive a given encoder?
564 * @encoder: encoder to test
565 * @crtc: crtc to test
567 * Return false if @encoder can't be driven by @crtc, true otherwise.
569 static bool drm_encoder_crtc_ok(struct drm_encoder
*encoder
,
570 struct drm_crtc
*crtc
)
572 struct drm_device
*dev
;
573 struct drm_crtc
*tmp
;
576 WARN(!crtc
, "checking null crtc?");
580 list_for_each_entry(tmp
, &dev
->mode_config
.crtc_list
, head
) {
586 if (encoder
->possible_crtcs
& crtc_mask
)
592 * Check the CRTC we're going to map each output to vs. its current
593 * CRTC. If they don't match, we have to disable the output and the CRTC
594 * since the driver will have to re-route things.
597 drm_crtc_prepare_encoders(struct drm_device
*dev
)
599 struct drm_encoder_helper_funcs
*encoder_funcs
;
600 struct drm_encoder
*encoder
;
602 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
) {
603 encoder_funcs
= encoder
->helper_private
;
604 /* Disable unused encoders */
605 if (encoder
->crtc
== NULL
)
606 (*encoder_funcs
->dpms
)(encoder
, DRM_MODE_DPMS_OFF
);
607 /* Disable encoders whose CRTC is about to change */
608 if (encoder_funcs
->get_crtc
&&
609 encoder
->crtc
!= (*encoder_funcs
->get_crtc
)(encoder
))
610 (*encoder_funcs
->dpms
)(encoder
, DRM_MODE_DPMS_OFF
);
615 * drm_crtc_set_mode - set a mode
616 * @crtc: CRTC to program
622 * Caller must hold mode config lock.
624 * Try to set @mode on @crtc. Give @crtc and its associated connectors a chance
625 * to fixup or reject the mode prior to trying to set it.
628 * True if the mode was set successfully, or false otherwise.
630 bool drm_crtc_helper_set_mode(struct drm_crtc
*crtc
,
631 struct drm_display_mode
*mode
,
633 struct drm_framebuffer
*old_fb
)
635 struct drm_device
*dev
= crtc
->dev
;
636 struct drm_display_mode
*adjusted_mode
, saved_mode
;
637 struct drm_crtc_helper_funcs
*crtc_funcs
= crtc
->helper_private
;
638 struct drm_encoder_helper_funcs
*encoder_funcs
;
639 int saved_x
, saved_y
;
640 struct drm_encoder
*encoder
;
643 adjusted_mode
= drm_mode_duplicate(dev
, mode
);
645 crtc
->enabled
= drm_helper_crtc_in_use(crtc
);
650 saved_mode
= crtc
->mode
;
654 /* Update crtc values up front so the driver can rely on them for mode
661 /* Pass our mode to the connectors and the CRTC to give them a chance to
662 * adjust it according to limitations or connector properties, and also
663 * a chance to reject the mode entirely.
665 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
) {
667 if (encoder
->crtc
!= crtc
)
669 encoder_funcs
= encoder
->helper_private
;
670 if (!(ret
= encoder_funcs
->mode_fixup(encoder
, mode
,
676 if (!(ret
= crtc_funcs
->mode_fixup(crtc
, mode
, adjusted_mode
))) {
680 /* Prepare the encoders and CRTCs before setting the mode. */
681 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
) {
683 if (encoder
->crtc
!= crtc
)
685 encoder_funcs
= encoder
->helper_private
;
686 /* Disable the encoders as the first thing we do. */
687 encoder_funcs
->prepare(encoder
);
690 drm_crtc_prepare_encoders(dev
);
692 crtc_funcs
->prepare(crtc
);
694 /* Set up the DPLL and any encoders state that needs to adjust or depend
697 ret
= !crtc_funcs
->mode_set(crtc
, mode
, adjusted_mode
, x
, y
, old_fb
);
701 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
) {
703 if (encoder
->crtc
!= crtc
)
706 DRM_DEBUG("%s: set mode %s %x\n", drm_get_encoder_name(encoder
),
707 mode
->name
, mode
->base
.id
);
708 encoder_funcs
= encoder
->helper_private
;
709 encoder_funcs
->mode_set(encoder
, mode
, adjusted_mode
);
712 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
713 crtc_funcs
->commit(crtc
);
715 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
) {
717 if (encoder
->crtc
!= crtc
)
720 encoder_funcs
= encoder
->helper_private
;
721 encoder_funcs
->commit(encoder
);
725 /* XXX free adjustedmode */
726 drm_mode_destroy(dev
, adjusted_mode
);
727 /* FIXME: add subpixel order */
730 crtc
->mode
= saved_mode
;
737 EXPORT_SYMBOL(drm_crtc_helper_set_mode
);
741 * drm_crtc_helper_set_config - set a new config from userspace
742 * @crtc: CRTC to setup
743 * @crtc_info: user provided configuration
744 * @new_mode: new mode to set
745 * @connector_set: set of connectors for the new config
746 * @fb: new framebuffer
749 * Caller must hold mode config lock.
751 * Setup a new configuration, provided by the user in @crtc_info, and enable
757 int drm_crtc_helper_set_config(struct drm_mode_set
*set
)
759 struct drm_device
*dev
;
760 struct drm_crtc
*save_crtcs
, *new_crtc
, *crtc
;
761 struct drm_encoder
*save_encoders
, *new_encoder
, *encoder
;
762 struct drm_framebuffer
*old_fb
= NULL
;
763 bool mode_changed
= false; /* if true do a full mode set */
764 bool fb_changed
= false; /* if true and !mode_changed just do a flip */
765 struct drm_connector
*save_connectors
, *connector
;
766 int count
= 0, ro
, fail
= 0;
767 struct drm_crtc_helper_funcs
*crtc_funcs
;
778 if (!set
->crtc
->helper_private
)
781 crtc_funcs
= set
->crtc
->helper_private
;
783 DRM_DEBUG_KMS("crtc: %p %d fb: %p connectors: %p num_connectors:"
784 " %d (x, y) (%i, %i)\n",
785 set
->crtc
, set
->crtc
->base
.id
, set
->fb
, set
->connectors
,
786 (int)set
->num_connectors
, set
->x
, set
->y
);
788 dev
= set
->crtc
->dev
;
790 /* Allocate space for the backup of all (non-pointer) crtc, encoder and
792 save_crtcs
= kzalloc(dev
->mode_config
.num_crtc
*
793 sizeof(struct drm_crtc
), GFP_KERNEL
);
797 save_encoders
= kzalloc(dev
->mode_config
.num_encoder
*
798 sizeof(struct drm_encoder
), GFP_KERNEL
);
799 if (!save_encoders
) {
804 save_connectors
= kzalloc(dev
->mode_config
.num_connector
*
805 sizeof(struct drm_connector
), GFP_KERNEL
);
806 if (!save_connectors
) {
808 kfree(save_encoders
);
812 /* Copy data. Note that driver private data is not affected.
813 * Should anything bad happen only the expected state is
814 * restored, not the drivers personal bookkeeping.
817 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
) {
818 save_crtcs
[count
++] = *crtc
;
822 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
) {
823 save_encoders
[count
++] = *encoder
;
827 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
828 save_connectors
[count
++] = *connector
;
831 /* We should be able to check here if the fb has the same properties
832 * and then just flip_or_move it */
833 if (set
->crtc
->fb
!= set
->fb
) {
834 /* If we have no fb then treat it as a full mode set */
835 if (set
->crtc
->fb
== NULL
) {
836 DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
838 } else if (set
->fb
== NULL
) {
844 if (set
->x
!= set
->crtc
->x
|| set
->y
!= set
->crtc
->y
)
847 if (set
->mode
&& !drm_mode_equal(set
->mode
, &set
->crtc
->mode
)) {
848 DRM_DEBUG_KMS("modes are different, full mode set\n");
849 drm_mode_debug_printmodeline(&set
->crtc
->mode
);
850 drm_mode_debug_printmodeline(set
->mode
);
854 /* a) traverse passed in connector list and get encoders for them */
856 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
857 struct drm_connector_helper_funcs
*connector_funcs
=
858 connector
->helper_private
;
859 new_encoder
= connector
->encoder
;
860 for (ro
= 0; ro
< set
->num_connectors
; ro
++) {
861 if (set
->connectors
[ro
] == connector
) {
862 new_encoder
= connector_funcs
->best_encoder(connector
);
863 /* if we can't get an encoder for a connector
864 we are setting now - then fail */
865 if (new_encoder
== NULL
)
866 /* don't break so fail path works correct */
872 if (new_encoder
!= connector
->encoder
) {
873 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
875 /* If the encoder is reused for another connector, then
876 * the appropriate crtc will be set later.
878 if (connector
->encoder
)
879 connector
->encoder
->crtc
= NULL
;
880 connector
->encoder
= new_encoder
;
890 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
891 if (!connector
->encoder
)
894 if (connector
->encoder
->crtc
== set
->crtc
)
897 new_crtc
= connector
->encoder
->crtc
;
899 for (ro
= 0; ro
< set
->num_connectors
; ro
++) {
900 if (set
->connectors
[ro
] == connector
)
901 new_crtc
= set
->crtc
;
904 /* Make sure the new CRTC will work with the encoder */
906 !drm_encoder_crtc_ok(connector
->encoder
, new_crtc
)) {
910 if (new_crtc
!= connector
->encoder
->crtc
) {
911 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
913 connector
->encoder
->crtc
= new_crtc
;
915 DRM_DEBUG_KMS("setting connector %d crtc to %p\n",
916 connector
->base
.id
, new_crtc
);
919 /* mode_set_base is not a required function */
920 if (fb_changed
&& !crtc_funcs
->mode_set_base
)
924 old_fb
= set
->crtc
->fb
;
925 set
->crtc
->fb
= set
->fb
;
926 set
->crtc
->enabled
= (set
->mode
!= NULL
);
927 if (set
->mode
!= NULL
) {
928 DRM_DEBUG_KMS("attempting to set mode from"
930 drm_mode_debug_printmodeline(set
->mode
);
931 if (!drm_crtc_helper_set_mode(set
->crtc
, set
->mode
,
934 DRM_ERROR("failed to set mode on crtc %p\n",
939 /* TODO are these needed? */
940 set
->crtc
->desired_x
= set
->x
;
941 set
->crtc
->desired_y
= set
->y
;
942 set
->crtc
->desired_mode
= set
->mode
;
944 drm_helper_disable_unused_functions(dev
);
945 } else if (fb_changed
) {
946 set
->crtc
->x
= set
->x
;
947 set
->crtc
->y
= set
->y
;
949 old_fb
= set
->crtc
->fb
;
950 if (set
->crtc
->fb
!= set
->fb
)
951 set
->crtc
->fb
= set
->fb
;
952 ret
= crtc_funcs
->mode_set_base(set
->crtc
,
953 set
->x
, set
->y
, old_fb
);
958 kfree(save_connectors
);
959 kfree(save_encoders
);
964 /* Restore all previous data. */
966 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
) {
967 *crtc
= save_crtcs
[count
++];
971 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
) {
972 *encoder
= save_encoders
[count
++];
976 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
977 *connector
= save_connectors
[count
++];
980 kfree(save_connectors
);
981 kfree(save_encoders
);
985 EXPORT_SYMBOL(drm_crtc_helper_set_config
);
987 bool drm_helper_plugged_event(struct drm_device
*dev
)
991 drm_helper_probe_connector_modes(dev
, dev
->mode_config
.max_width
,
992 dev
->mode_config
.max_height
);
994 drm_setup_crtcs(dev
);
996 /* alert the driver fb layer */
997 dev
->mode_config
.funcs
->fb_changed(dev
);
999 /* FIXME: send hotplug event */
1003 * drm_initial_config - setup a sane initial connector configuration
1007 * Called at init time, must take mode config lock.
1009 * Scan the CRTCs and connectors and try to put together an initial setup.
1010 * At the moment, this is a cloned configuration across all heads with
1011 * a new framebuffer object as the backing store.
1014 * Zero if everything went ok, nonzero otherwise.
1016 bool drm_helper_initial_config(struct drm_device
*dev
)
1020 /* disable all the possible outputs/crtcs before entering KMS mode */
1021 drm_helper_disable_unused_functions(dev
);
1023 drm_fb_helper_parse_command_line(dev
);
1025 count
= drm_helper_probe_connector_modes(dev
,
1026 dev
->mode_config
.max_width
,
1027 dev
->mode_config
.max_height
);
1030 * we shouldn't end up with no modes here.
1033 printk(KERN_INFO
"No connectors reported connected with modes\n");
1035 drm_setup_crtcs(dev
);
1037 /* alert the driver fb layer */
1038 dev
->mode_config
.funcs
->fb_changed(dev
);
1042 EXPORT_SYMBOL(drm_helper_initial_config
);
1044 static int drm_helper_choose_encoder_dpms(struct drm_encoder
*encoder
)
1046 int dpms
= DRM_MODE_DPMS_OFF
;
1047 struct drm_connector
*connector
;
1048 struct drm_device
*dev
= encoder
->dev
;
1050 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
)
1051 if (connector
->encoder
== encoder
)
1052 if (connector
->dpms
< dpms
)
1053 dpms
= connector
->dpms
;
1057 static int drm_helper_choose_crtc_dpms(struct drm_crtc
*crtc
)
1059 int dpms
= DRM_MODE_DPMS_OFF
;
1060 struct drm_connector
*connector
;
1061 struct drm_device
*dev
= crtc
->dev
;
1063 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
)
1064 if (connector
->encoder
&& connector
->encoder
->crtc
== crtc
)
1065 if (connector
->dpms
< dpms
)
1066 dpms
= connector
->dpms
;
1071 * drm_helper_connector_dpms
1072 * @connector affected connector
1075 * Calls the low-level connector DPMS function, then
1076 * calls appropriate encoder and crtc DPMS functions as well
1078 void drm_helper_connector_dpms(struct drm_connector
*connector
, int mode
)
1080 struct drm_encoder
*encoder
= connector
->encoder
;
1081 struct drm_crtc
*crtc
= encoder
? encoder
->crtc
: NULL
;
1084 if (mode
== connector
->dpms
)
1087 old_dpms
= connector
->dpms
;
1088 connector
->dpms
= mode
;
1090 /* from off to on, do crtc then encoder */
1091 if (mode
< old_dpms
) {
1093 struct drm_crtc_helper_funcs
*crtc_funcs
= crtc
->helper_private
;
1094 if (crtc_funcs
->dpms
)
1095 (*crtc_funcs
->dpms
) (crtc
,
1096 drm_helper_choose_crtc_dpms(crtc
));
1099 struct drm_encoder_helper_funcs
*encoder_funcs
= encoder
->helper_private
;
1100 if (encoder_funcs
->dpms
)
1101 (*encoder_funcs
->dpms
) (encoder
,
1102 drm_helper_choose_encoder_dpms(encoder
));
1106 /* from on to off, do encoder then crtc */
1107 if (mode
> old_dpms
) {
1109 struct drm_encoder_helper_funcs
*encoder_funcs
= encoder
->helper_private
;
1110 if (encoder_funcs
->dpms
)
1111 (*encoder_funcs
->dpms
) (encoder
,
1112 drm_helper_choose_encoder_dpms(encoder
));
1115 struct drm_crtc_helper_funcs
*crtc_funcs
= crtc
->helper_private
;
1116 if (crtc_funcs
->dpms
)
1117 (*crtc_funcs
->dpms
) (crtc
,
1118 drm_helper_choose_crtc_dpms(crtc
));
1124 EXPORT_SYMBOL(drm_helper_connector_dpms
);
1127 * drm_hotplug_stage_two
1129 * @connector hotpluged connector
1132 * Caller must hold mode config lock, function might grab struct lock.
1134 * Stage two of a hotplug.
1137 * Zero on success, errno on failure.
1139 int drm_helper_hotplug_stage_two(struct drm_device
*dev
)
1141 drm_helper_plugged_event(dev
);
1145 EXPORT_SYMBOL(drm_helper_hotplug_stage_two
);
1147 int drm_helper_mode_fill_fb_struct(struct drm_framebuffer
*fb
,
1148 struct drm_mode_fb_cmd
*mode_cmd
)
1150 fb
->width
= mode_cmd
->width
;
1151 fb
->height
= mode_cmd
->height
;
1152 fb
->pitch
= mode_cmd
->pitch
;
1153 fb
->bits_per_pixel
= mode_cmd
->bpp
;
1154 fb
->depth
= mode_cmd
->depth
;
1158 EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct
);
1160 int drm_helper_resume_force_mode(struct drm_device
*dev
)
1162 struct drm_crtc
*crtc
;
1163 struct drm_encoder
*encoder
;
1164 struct drm_encoder_helper_funcs
*encoder_funcs
;
1165 struct drm_crtc_helper_funcs
*crtc_funcs
;
1168 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
) {
1173 ret
= drm_crtc_helper_set_mode(crtc
, &crtc
->mode
,
1174 crtc
->x
, crtc
->y
, crtc
->fb
);
1177 DRM_ERROR("failed to set mode on crtc %p\n", crtc
);
1179 /* Turn off outputs that were already powered off */
1180 if (drm_helper_choose_crtc_dpms(crtc
)) {
1181 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
) {
1183 if(encoder
->crtc
!= crtc
)
1186 encoder_funcs
= encoder
->helper_private
;
1187 if (encoder_funcs
->dpms
)
1188 (*encoder_funcs
->dpms
) (encoder
,
1189 drm_helper_choose_encoder_dpms(encoder
));
1191 crtc_funcs
= crtc
->helper_private
;
1192 if (crtc_funcs
->dpms
)
1193 (*crtc_funcs
->dpms
) (crtc
,
1194 drm_helper_choose_crtc_dpms(crtc
));
1198 /* disable the unused connectors while restoring the modesetting */
1199 drm_helper_disable_unused_functions(dev
);
1202 EXPORT_SYMBOL(drm_helper_resume_force_mode
);