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));
110 count = (*connector_funcs->get_modes)(connector);
112 count = drm_add_modes_noedid(connector, 800, 600);
117 drm_mode_connector_list_update(connector);
120 drm_mode_validate_size(dev, &connector->modes, maxX,
123 if (connector->interlace_allowed)
124 mode_flags |= DRM_MODE_FLAG_INTERLACE;
125 if (connector->doublescan_allowed)
126 mode_flags |= DRM_MODE_FLAG_DBLSCAN;
127 drm_mode_validate_flag(connector, mode_flags);
129 list_for_each_entry_safe(mode, t, &connector->modes, head) {
130 if (mode->status == MODE_OK)
131 mode->status = connector_funcs->mode_valid(connector,
136 drm_mode_prune_invalid(dev, &connector->modes, true);
138 if (list_empty(&connector->modes))
141 drm_mode_sort(&connector->modes);
143 DRM_DEBUG_KMS("Probed modes for %s\n",
144 drm_get_connector_name(connector));
145 list_for_each_entry_safe(mode, t, &connector->modes, head) {
146 mode->vrefresh = drm_mode_vrefresh(mode);
148 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
149 drm_mode_debug_printmodeline(mode);
154 EXPORT_SYMBOL(drm_helper_probe_single_connector_modes);
156 int drm_helper_probe_connector_modes(struct drm_device *dev, uint32_t maxX,
159 struct drm_connector *connector;
162 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
163 count += drm_helper_probe_single_connector_modes(connector,
169 EXPORT_SYMBOL(drm_helper_probe_connector_modes);
172 * drm_helper_encoder_in_use - check if a given encoder is in use
173 * @encoder: encoder to check
176 * Caller must hold mode config lock.
178 * Walk @encoders's DRM device's mode_config and see if it's in use.
181 * True if @encoder is part of the mode_config, false otherwise.
183 bool drm_helper_encoder_in_use(struct drm_encoder *encoder)
185 struct drm_connector *connector;
186 struct drm_device *dev = encoder->dev;
187 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
188 if (connector->encoder == encoder)
192 EXPORT_SYMBOL(drm_helper_encoder_in_use);
195 * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config
196 * @crtc: CRTC to check
199 * Caller must hold mode config lock.
201 * Walk @crtc's DRM device's mode_config and see if it's in use.
204 * True if @crtc is part of the mode_config, false otherwise.
206 bool drm_helper_crtc_in_use(struct drm_crtc *crtc)
208 struct drm_encoder *encoder;
209 struct drm_device *dev = crtc->dev;
210 /* FIXME: Locking around list access? */
211 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
212 if (encoder->crtc == crtc && drm_helper_encoder_in_use(encoder))
216 EXPORT_SYMBOL(drm_helper_crtc_in_use);
219 * drm_disable_unused_functions - disable unused objects
223 * Caller must hold mode config lock.
225 * If an connector or CRTC isn't part of @dev's mode_config, it can be disabled
226 * by calling its dpms function, which should power it off.
228 void drm_helper_disable_unused_functions(struct drm_device *dev)
230 struct drm_encoder *encoder;
231 struct drm_connector *connector;
232 struct drm_encoder_helper_funcs *encoder_funcs;
233 struct drm_crtc *crtc;
235 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
236 if (!connector->encoder)
238 if (connector->status == connector_status_disconnected)
239 connector->encoder = NULL;
242 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
243 encoder_funcs = encoder->helper_private;
244 if (!drm_helper_encoder_in_use(encoder)) {
245 if (encoder_funcs->disable)
246 (*encoder_funcs->disable)(encoder);
248 (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
249 /* disconnector encoder from any connector */
250 encoder->crtc = NULL;
254 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
255 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
256 crtc->enabled = drm_helper_crtc_in_use(crtc);
257 if (!crtc->enabled) {
258 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
263 EXPORT_SYMBOL(drm_helper_disable_unused_functions);
265 static struct drm_display_mode *drm_has_preferred_mode(struct drm_connector *connector, int width, int height)
267 struct drm_display_mode *mode;
269 list_for_each_entry(mode, &connector->modes, head) {
270 if (drm_mode_width(mode) > width ||
271 drm_mode_height(mode) > height)
273 if (mode->type & DRM_MODE_TYPE_PREFERRED)
279 static bool drm_has_cmdline_mode(struct drm_connector *connector)
281 struct drm_fb_helper_connector *fb_help_conn = connector->fb_helper_private;
282 struct drm_fb_helper_cmdline_mode *cmdline_mode;
287 cmdline_mode = &fb_help_conn->cmdline_mode;
288 return cmdline_mode->specified;
291 static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_connector *connector, int width, int height)
293 struct drm_fb_helper_connector *fb_help_conn = connector->fb_helper_private;
294 struct drm_fb_helper_cmdline_mode *cmdline_mode;
295 struct drm_display_mode *mode = NULL;
300 cmdline_mode = &fb_help_conn->cmdline_mode;
301 if (cmdline_mode->specified == false)
304 /* attempt to find a matching mode in the list of modes
305 * we have gotten so far, if not add a CVT mode that conforms
307 if (cmdline_mode->rb || cmdline_mode->margins)
310 list_for_each_entry(mode, &connector->modes, head) {
311 /* check width/height */
312 if (mode->hdisplay != cmdline_mode->xres ||
313 mode->vdisplay != cmdline_mode->yres)
316 if (cmdline_mode->refresh_specified) {
317 if (mode->vrefresh != cmdline_mode->refresh)
321 if (cmdline_mode->interlace) {
322 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
329 mode = drm_cvt_mode(connector->dev, cmdline_mode->xres,
331 cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
332 cmdline_mode->rb, cmdline_mode->interlace,
333 cmdline_mode->margins);
334 list_add(&mode->head, &connector->modes);
338 static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
343 enable = connector->status == connector_status_connected;
345 enable = connector->status != connector_status_disconnected;
350 static void drm_enable_connectors(struct drm_device *dev, bool *enabled)
352 bool any_enabled = false;
353 struct drm_connector *connector;
356 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
357 enabled[i] = drm_connector_enabled(connector, true);
358 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
359 enabled[i] ? "yes" : "no");
360 any_enabled |= enabled[i];
368 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
369 enabled[i] = drm_connector_enabled(connector, false);
374 static bool drm_target_preferred(struct drm_device *dev,
375 struct drm_display_mode **modes,
376 bool *enabled, int width, int height)
378 struct drm_connector *connector;
381 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
383 if (enabled[i] == false) {
388 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
391 /* got for command line mode first */
392 modes[i] = drm_pick_cmdline_mode(connector, width, height);
394 DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
396 modes[i] = drm_has_preferred_mode(connector, width, height);
398 /* No preferred modes, pick one off the list */
399 if (!modes[i] && !list_empty(&connector->modes)) {
400 list_for_each_entry(modes[i], &connector->modes, head)
403 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
410 static int drm_pick_crtcs(struct drm_device *dev,
411 struct drm_crtc **best_crtcs,
412 struct drm_display_mode **modes,
413 int n, int width, int height)
416 struct drm_connector *connector;
417 struct drm_connector_helper_funcs *connector_funcs;
418 struct drm_encoder *encoder;
419 struct drm_crtc *best_crtc;
420 int my_score, best_score, score;
421 struct drm_crtc **crtcs, *crtc;
423 if (n == dev->mode_config.num_connector)
426 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
432 best_crtcs[n] = NULL;
434 best_score = drm_pick_crtcs(dev, best_crtcs, modes, n+1, width, height);
435 if (modes[n] == NULL)
438 crtcs = kmalloc(dev->mode_config.num_connector *
439 sizeof(struct drm_crtc *), GFP_KERNEL);
444 if (connector->status == connector_status_connected)
446 if (drm_has_cmdline_mode(connector))
448 if (drm_has_preferred_mode(connector, width, height))
451 connector_funcs = connector->helper_private;
452 encoder = connector_funcs->best_encoder(connector);
456 connector->encoder = encoder;
458 /* select a crtc for this connector and then attempt to configure
459 remaining connectors */
461 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
463 if ((encoder->possible_crtcs & (1 << c)) == 0) {
468 for (o = 0; o < n; o++)
469 if (best_crtcs[o] == crtc)
473 /* ignore cloning for now */
479 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_crtc *));
480 score = my_score + drm_pick_crtcs(dev, crtcs, modes, n + 1,
482 if (score > best_score) {
485 memcpy(best_crtcs, crtcs,
486 dev->mode_config.num_connector *
487 sizeof(struct drm_crtc *));
496 static void drm_setup_crtcs(struct drm_device *dev)
498 struct drm_crtc **crtcs;
499 struct drm_display_mode **modes;
500 struct drm_encoder *encoder;
501 struct drm_connector *connector;
508 width = dev->mode_config.max_width;
509 height = dev->mode_config.max_height;
511 /* clean out all the encoder/crtc combos */
512 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
513 encoder->crtc = NULL;
516 crtcs = kcalloc(dev->mode_config.num_connector,
517 sizeof(struct drm_crtc *), GFP_KERNEL);
518 modes = kcalloc(dev->mode_config.num_connector,
519 sizeof(struct drm_display_mode *), GFP_KERNEL);
520 enabled = kcalloc(dev->mode_config.num_connector,
521 sizeof(bool), GFP_KERNEL);
523 drm_enable_connectors(dev, enabled);
525 ret = drm_target_preferred(dev, modes, enabled, width, height);
527 DRM_ERROR("Unable to find initial modes\n");
529 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
531 drm_pick_crtcs(dev, crtcs, modes, 0, width, height);
534 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
535 struct drm_display_mode *mode = modes[i];
536 struct drm_crtc *crtc = crtcs[i];
538 if (connector->encoder == NULL) {
544 DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
545 mode->name, crtc->base.id);
546 crtc->desired_mode = mode;
547 connector->encoder->crtc = crtc;
549 connector->encoder->crtc = NULL;
550 connector->encoder = NULL;
561 * drm_encoder_crtc_ok - can a given crtc drive a given encoder?
562 * @encoder: encoder to test
563 * @crtc: crtc to test
565 * Return false if @encoder can't be driven by @crtc, true otherwise.
567 static bool drm_encoder_crtc_ok(struct drm_encoder *encoder,
568 struct drm_crtc *crtc)
570 struct drm_device *dev;
571 struct drm_crtc *tmp;
574 WARN(!crtc, "checking null crtc?");
578 list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
584 if (encoder->possible_crtcs & crtc_mask)
590 * Check the CRTC we're going to map each output to vs. its current
591 * CRTC. If they don't match, we have to disable the output and the CRTC
592 * since the driver will have to re-route things.
595 drm_crtc_prepare_encoders(struct drm_device *dev)
597 struct drm_encoder_helper_funcs *encoder_funcs;
598 struct drm_encoder *encoder;
600 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
601 encoder_funcs = encoder->helper_private;
602 /* Disable unused encoders */
603 if (encoder->crtc == NULL)
604 (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
605 /* Disable encoders whose CRTC is about to change */
606 if (encoder_funcs->get_crtc &&
607 encoder->crtc != (*encoder_funcs->get_crtc)(encoder))
608 (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
613 * drm_crtc_set_mode - set a mode
614 * @crtc: CRTC to program
620 * Caller must hold mode config lock.
622 * Try to set @mode on @crtc. Give @crtc and its associated connectors a chance
623 * to fixup or reject the mode prior to trying to set it.
626 * True if the mode was set successfully, or false otherwise.
628 bool drm_crtc_helper_set_mode(struct drm_crtc *crtc,
629 struct drm_display_mode *mode,
631 struct drm_framebuffer *old_fb)
633 struct drm_device *dev = crtc->dev;
634 struct drm_display_mode *adjusted_mode, saved_mode;
635 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
636 struct drm_encoder_helper_funcs *encoder_funcs;
637 int saved_x, saved_y;
638 struct drm_encoder *encoder;
641 adjusted_mode = drm_mode_duplicate(dev, mode);
643 crtc->enabled = drm_helper_crtc_in_use(crtc);
648 saved_mode = crtc->mode;
652 /* Update crtc values up front so the driver can rely on them for mode
659 /* Pass our mode to the connectors and the CRTC to give them a chance to
660 * adjust it according to limitations or connector properties, and also
661 * a chance to reject the mode entirely.
663 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
665 if (encoder->crtc != crtc)
667 encoder_funcs = encoder->helper_private;
668 if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
674 if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) {
678 /* Prepare the encoders and CRTCs before setting the mode. */
679 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
681 if (encoder->crtc != crtc)
683 encoder_funcs = encoder->helper_private;
684 /* Disable the encoders as the first thing we do. */
685 encoder_funcs->prepare(encoder);
688 drm_crtc_prepare_encoders(dev);
690 crtc_funcs->prepare(crtc);
692 /* Set up the DPLL and any encoders state that needs to adjust or depend
695 ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
699 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
701 if (encoder->crtc != crtc)
704 DRM_INFO("%s: set mode %s %x\n", drm_get_encoder_name(encoder),
705 mode->name, mode->base.id);
706 encoder_funcs = encoder->helper_private;
707 encoder_funcs->mode_set(encoder, mode, adjusted_mode);
710 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
711 crtc_funcs->commit(crtc);
713 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
715 if (encoder->crtc != crtc)
718 encoder_funcs = encoder->helper_private;
719 encoder_funcs->commit(encoder);
723 /* XXX free adjustedmode */
724 drm_mode_destroy(dev, adjusted_mode);
725 /* FIXME: add subpixel order */
728 crtc->mode = saved_mode;
735 EXPORT_SYMBOL(drm_crtc_helper_set_mode);
739 * drm_crtc_helper_set_config - set a new config from userspace
740 * @crtc: CRTC to setup
741 * @crtc_info: user provided configuration
742 * @new_mode: new mode to set
743 * @connector_set: set of connectors for the new config
744 * @fb: new framebuffer
747 * Caller must hold mode config lock.
749 * Setup a new configuration, provided by the user in @crtc_info, and enable
755 int drm_crtc_helper_set_config(struct drm_mode_set *set)
757 struct drm_device *dev;
758 struct drm_crtc *save_crtcs, *new_crtc, *crtc;
759 struct drm_encoder *save_encoders, *new_encoder, *encoder;
760 struct drm_framebuffer *old_fb = NULL;
761 bool mode_changed = false; /* if true do a full mode set */
762 bool fb_changed = false; /* if true and !mode_changed just do a flip */
763 struct drm_connector *save_connectors, *connector;
764 int count = 0, ro, fail = 0;
765 struct drm_crtc_helper_funcs *crtc_funcs;
776 if (!set->crtc->helper_private)
779 crtc_funcs = set->crtc->helper_private;
781 DRM_DEBUG_KMS("crtc: %p %d fb: %p connectors: %p num_connectors:"
782 " %d (x, y) (%i, %i)\n",
783 set->crtc, set->crtc->base.id, set->fb, set->connectors,
784 (int)set->num_connectors, set->x, set->y);
786 dev = set->crtc->dev;
788 /* Allocate space for the backup of all (non-pointer) crtc, encoder and
790 save_crtcs = kzalloc(dev->mode_config.num_crtc *
791 sizeof(struct drm_crtc), GFP_KERNEL);
795 save_encoders = kzalloc(dev->mode_config.num_encoder *
796 sizeof(struct drm_encoder), GFP_KERNEL);
797 if (!save_encoders) {
802 save_connectors = kzalloc(dev->mode_config.num_connector *
803 sizeof(struct drm_connector), GFP_KERNEL);
804 if (!save_connectors) {
806 kfree(save_encoders);
810 /* Copy data. Note that driver private data is not affected.
811 * Should anything bad happen only the expected state is
812 * restored, not the drivers personal bookkeeping.
815 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
816 save_crtcs[count++] = *crtc;
820 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
821 save_encoders[count++] = *encoder;
825 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
826 save_connectors[count++] = *connector;
829 /* We should be able to check here if the fb has the same properties
830 * and then just flip_or_move it */
831 if (set->crtc->fb != set->fb) {
832 /* If we have no fb then treat it as a full mode set */
833 if (set->crtc->fb == NULL) {
834 DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
836 } else if (set->fb == NULL) {
838 } else if ((set->fb->bits_per_pixel !=
839 set->crtc->fb->bits_per_pixel) ||
840 set->fb->depth != set->crtc->fb->depth)
846 if (set->x != set->crtc->x || set->y != set->crtc->y)
849 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
850 DRM_DEBUG_KMS("modes are different, full mode set\n");
851 drm_mode_debug_printmodeline(&set->crtc->mode);
852 drm_mode_debug_printmodeline(set->mode);
856 /* a) traverse passed in connector list and get encoders for them */
858 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
859 struct drm_connector_helper_funcs *connector_funcs =
860 connector->helper_private;
861 new_encoder = connector->encoder;
862 for (ro = 0; ro < set->num_connectors; ro++) {
863 if (set->connectors[ro] == connector) {
864 new_encoder = connector_funcs->best_encoder(connector);
865 /* if we can't get an encoder for a connector
866 we are setting now - then fail */
867 if (new_encoder == NULL)
868 /* don't break so fail path works correct */
874 if (new_encoder != connector->encoder) {
875 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
877 /* If the encoder is reused for another connector, then
878 * the appropriate crtc will be set later.
880 if (connector->encoder)
881 connector->encoder->crtc = NULL;
882 connector->encoder = new_encoder;
892 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
893 if (!connector->encoder)
896 if (connector->encoder->crtc == set->crtc)
899 new_crtc = connector->encoder->crtc;
901 for (ro = 0; ro < set->num_connectors; ro++) {
902 if (set->connectors[ro] == connector)
903 new_crtc = set->crtc;
906 /* Make sure the new CRTC will work with the encoder */
908 !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
912 if (new_crtc != connector->encoder->crtc) {
913 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
915 connector->encoder->crtc = new_crtc;
917 DRM_DEBUG_KMS("setting connector %d crtc to %p\n",
918 connector->base.id, new_crtc);
921 /* mode_set_base is not a required function */
922 if (fb_changed && !crtc_funcs->mode_set_base)
926 old_fb = set->crtc->fb;
927 set->crtc->fb = set->fb;
928 set->crtc->enabled = (set->mode != NULL);
929 if (set->mode != NULL) {
930 DRM_DEBUG_KMS("attempting to set mode from"
932 drm_mode_debug_printmodeline(set->mode);
933 if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
936 DRM_ERROR("failed to set mode on crtc %p\n",
941 /* TODO are these needed? */
942 set->crtc->desired_x = set->x;
943 set->crtc->desired_y = set->y;
944 set->crtc->desired_mode = set->mode;
946 drm_helper_disable_unused_functions(dev);
947 } else if (fb_changed) {
948 set->crtc->x = set->x;
949 set->crtc->y = set->y;
951 old_fb = set->crtc->fb;
952 if (set->crtc->fb != set->fb)
953 set->crtc->fb = set->fb;
954 ret = crtc_funcs->mode_set_base(set->crtc,
955 set->x, set->y, old_fb);
960 kfree(save_connectors);
961 kfree(save_encoders);
966 /* Restore all previous data. */
968 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
969 *crtc = save_crtcs[count++];
973 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
974 *encoder = save_encoders[count++];
978 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
979 *connector = save_connectors[count++];
982 kfree(save_connectors);
983 kfree(save_encoders);
987 EXPORT_SYMBOL(drm_crtc_helper_set_config);
989 bool drm_helper_plugged_event(struct drm_device *dev)
993 drm_helper_probe_connector_modes(dev, dev->mode_config.max_width,
994 dev->mode_config.max_height);
996 drm_setup_crtcs(dev);
998 /* alert the driver fb layer */
999 dev->mode_config.funcs->fb_changed(dev);
1001 /* FIXME: send hotplug event */
1005 * drm_initial_config - setup a sane initial connector configuration
1009 * Called at init time, must take mode config lock.
1011 * Scan the CRTCs and connectors and try to put together an initial setup.
1012 * At the moment, this is a cloned configuration across all heads with
1013 * a new framebuffer object as the backing store.
1016 * Zero if everything went ok, nonzero otherwise.
1018 bool drm_helper_initial_config(struct drm_device *dev)
1022 drm_fb_helper_parse_command_line(dev);
1024 count = drm_helper_probe_connector_modes(dev,
1025 dev->mode_config.max_width,
1026 dev->mode_config.max_height);
1029 * we shouldn't end up with no modes here.
1031 WARN(!count, "No connectors reported connected with modes\n");
1033 drm_setup_crtcs(dev);
1035 /* alert the driver fb layer */
1036 dev->mode_config.funcs->fb_changed(dev);
1040 EXPORT_SYMBOL(drm_helper_initial_config);
1042 static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder)
1044 int dpms = DRM_MODE_DPMS_OFF;
1045 struct drm_connector *connector;
1046 struct drm_device *dev = encoder->dev;
1048 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1049 if (connector->encoder == encoder)
1050 if (connector->dpms < dpms)
1051 dpms = connector->dpms;
1055 static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc)
1057 int dpms = DRM_MODE_DPMS_OFF;
1058 struct drm_connector *connector;
1059 struct drm_device *dev = crtc->dev;
1061 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1062 if (connector->encoder && connector->encoder->crtc == crtc)
1063 if (connector->dpms < dpms)
1064 dpms = connector->dpms;
1069 * drm_helper_connector_dpms
1070 * @connector affected connector
1073 * Calls the low-level connector DPMS function, then
1074 * calls appropriate encoder and crtc DPMS functions as well
1076 void drm_helper_connector_dpms(struct drm_connector *connector, int mode)
1078 struct drm_encoder *encoder = connector->encoder;
1079 struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
1082 if (mode == connector->dpms)
1085 old_dpms = connector->dpms;
1086 connector->dpms = mode;
1088 /* from off to on, do crtc then encoder */
1089 if (mode < old_dpms) {
1091 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
1092 if (crtc_funcs->dpms)
1093 (*crtc_funcs->dpms) (crtc,
1094 drm_helper_choose_crtc_dpms(crtc));
1097 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
1098 if (encoder_funcs->dpms)
1099 (*encoder_funcs->dpms) (encoder,
1100 drm_helper_choose_encoder_dpms(encoder));
1104 /* from on to off, do encoder then crtc */
1105 if (mode > old_dpms) {
1107 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
1108 if (encoder_funcs->dpms)
1109 (*encoder_funcs->dpms) (encoder,
1110 drm_helper_choose_encoder_dpms(encoder));
1113 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
1114 if (crtc_funcs->dpms)
1115 (*crtc_funcs->dpms) (crtc,
1116 drm_helper_choose_crtc_dpms(crtc));
1122 EXPORT_SYMBOL(drm_helper_connector_dpms);
1125 * drm_hotplug_stage_two
1127 * @connector hotpluged connector
1130 * Caller must hold mode config lock, function might grab struct lock.
1132 * Stage two of a hotplug.
1135 * Zero on success, errno on failure.
1137 int drm_helper_hotplug_stage_two(struct drm_device *dev)
1139 drm_helper_plugged_event(dev);
1143 EXPORT_SYMBOL(drm_helper_hotplug_stage_two);
1145 int drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb,
1146 struct drm_mode_fb_cmd *mode_cmd)
1148 fb->width = mode_cmd->width;
1149 fb->height = mode_cmd->height;
1150 fb->pitch = mode_cmd->pitch;
1151 fb->bits_per_pixel = mode_cmd->bpp;
1152 fb->depth = mode_cmd->depth;
1156 EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
1158 int drm_helper_resume_force_mode(struct drm_device *dev)
1160 struct drm_crtc *crtc;
1163 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1168 ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
1169 crtc->x, crtc->y, crtc->fb);
1172 DRM_ERROR("failed to set mode on crtc %p\n", crtc);
1174 /* disable the unused connectors while restoring the modesetting */
1175 drm_helper_disable_unused_functions(dev);
1178 EXPORT_SYMBOL(drm_helper_resume_force_mode);