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"
37 * Detailed mode info for 800x600@60Hz
39 static struct drm_display_mode std_modes[] = {
40 { DRM_MODE("800x600", DRM_MODE_TYPE_DEFAULT, 40000, 800, 840,
41 968, 1056, 0, 600, 601, 605, 628, 0,
42 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
45 static void drm_mode_validate_flag(struct drm_connector *connector,
48 struct drm_display_mode *mode, *t;
50 if (flags == (DRM_MODE_FLAG_DBLSCAN | DRM_MODE_FLAG_INTERLACE))
53 list_for_each_entry_safe(mode, t, &connector->modes, head) {
54 if ((mode->flags & DRM_MODE_FLAG_INTERLACE) &&
55 !(flags & DRM_MODE_FLAG_INTERLACE))
56 mode->status = MODE_NO_INTERLACE;
57 if ((mode->flags & DRM_MODE_FLAG_DBLSCAN) &&
58 !(flags & DRM_MODE_FLAG_DBLSCAN))
59 mode->status = MODE_NO_DBLESCAN;
66 * drm_helper_probe_connector_modes - get complete set of display modes
68 * @maxX: max width for modes
69 * @maxY: max height for modes
72 * Caller must hold mode config lock.
74 * Based on @dev's mode_config layout, scan all the connectors and try to detect
75 * modes on them. Modes will first be added to the connector's probed_modes
76 * list, then culled (based on validity and the @maxX, @maxY parameters) and
77 * put into the normal modes list.
79 * Intended to be used either at bootup time or when major configuration
80 * changes have occurred.
82 * FIXME: take into account monitor limits
85 * Number of modes found on @connector.
87 int drm_helper_probe_single_connector_modes(struct drm_connector *connector,
88 uint32_t maxX, uint32_t maxY)
90 struct drm_device *dev = connector->dev;
91 struct drm_display_mode *mode, *t;
92 struct drm_connector_helper_funcs *connector_funcs =
93 connector->helper_private;
97 DRM_DEBUG_KMS("%s\n", drm_get_connector_name(connector));
98 /* set all modes to the unverified state */
99 list_for_each_entry_safe(mode, t, &connector->modes, head)
100 mode->status = MODE_UNVERIFIED;
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 /* TODO set EDID to NULL */
111 count = (*connector_funcs->get_modes)(connector);
115 drm_mode_connector_list_update(connector);
118 drm_mode_validate_size(dev, &connector->modes, maxX,
121 if (connector->interlace_allowed)
122 mode_flags |= DRM_MODE_FLAG_INTERLACE;
123 if (connector->doublescan_allowed)
124 mode_flags |= DRM_MODE_FLAG_DBLSCAN;
125 drm_mode_validate_flag(connector, mode_flags);
127 list_for_each_entry_safe(mode, t, &connector->modes, head) {
128 if (mode->status == MODE_OK)
129 mode->status = connector_funcs->mode_valid(connector,
134 drm_mode_prune_invalid(dev, &connector->modes, true);
136 if (list_empty(&connector->modes))
139 drm_mode_sort(&connector->modes);
141 DRM_DEBUG_KMS("Probed modes for %s\n",
142 drm_get_connector_name(connector));
143 list_for_each_entry_safe(mode, t, &connector->modes, head) {
144 mode->vrefresh = drm_mode_vrefresh(mode);
146 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
147 drm_mode_debug_printmodeline(mode);
152 EXPORT_SYMBOL(drm_helper_probe_single_connector_modes);
154 int drm_helper_probe_connector_modes(struct drm_device *dev, uint32_t maxX,
157 struct drm_connector *connector;
160 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
161 count += drm_helper_probe_single_connector_modes(connector,
167 EXPORT_SYMBOL(drm_helper_probe_connector_modes);
169 static void drm_helper_add_std_modes(struct drm_device *dev,
170 struct drm_connector *connector)
172 struct drm_display_mode *mode, *t;
175 for (i = 0; i < ARRAY_SIZE(std_modes); i++) {
176 struct drm_display_mode *stdmode;
179 * When no valid EDID modes are available we end up
180 * here and bailed in the past, now we add some standard
183 stdmode = drm_mode_duplicate(dev, &std_modes[i]);
184 drm_mode_probed_add(connector, stdmode);
185 drm_mode_list_concat(&connector->probed_modes,
188 DRM_DEBUG_KMS("Adding mode %s to %s\n", stdmode->name,
189 drm_get_connector_name(connector));
191 drm_mode_sort(&connector->modes);
193 DRM_DEBUG_KMS("Added std modes on %s\n",
194 drm_get_connector_name(connector));
195 list_for_each_entry_safe(mode, t, &connector->modes, head) {
196 mode->vrefresh = drm_mode_vrefresh(mode);
198 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
199 drm_mode_debug_printmodeline(mode);
204 * drm_helper_encoder_in_use - check if a given encoder is in use
205 * @encoder: encoder to check
208 * Caller must hold mode config lock.
210 * Walk @encoders's DRM device's mode_config and see if it's in use.
213 * True if @encoder is part of the mode_config, false otherwise.
215 bool drm_helper_encoder_in_use(struct drm_encoder *encoder)
217 struct drm_connector *connector;
218 struct drm_device *dev = encoder->dev;
219 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
220 if (connector->encoder == encoder)
224 EXPORT_SYMBOL(drm_helper_encoder_in_use);
227 * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config
228 * @crtc: CRTC to check
231 * Caller must hold mode config lock.
233 * Walk @crtc's DRM device's mode_config and see if it's in use.
236 * True if @crtc is part of the mode_config, false otherwise.
238 bool drm_helper_crtc_in_use(struct drm_crtc *crtc)
240 struct drm_encoder *encoder;
241 struct drm_device *dev = crtc->dev;
242 /* FIXME: Locking around list access? */
243 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
244 if (encoder->crtc == crtc && drm_helper_encoder_in_use(encoder))
248 EXPORT_SYMBOL(drm_helper_crtc_in_use);
251 * drm_disable_unused_functions - disable unused objects
255 * Caller must hold mode config lock.
257 * If an connector or CRTC isn't part of @dev's mode_config, it can be disabled
258 * by calling its dpms function, which should power it off.
260 void drm_helper_disable_unused_functions(struct drm_device *dev)
262 struct drm_encoder *encoder;
263 struct drm_encoder_helper_funcs *encoder_funcs;
264 struct drm_crtc *crtc;
266 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
267 encoder_funcs = encoder->helper_private;
268 if (!drm_helper_encoder_in_use(encoder))
269 (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
272 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
273 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
274 crtc->enabled = drm_helper_crtc_in_use(crtc);
275 if (!crtc->enabled) {
276 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
281 EXPORT_SYMBOL(drm_helper_disable_unused_functions);
283 static struct drm_display_mode *drm_has_preferred_mode(struct drm_connector *connector, int width, int height)
285 struct drm_display_mode *mode;
287 list_for_each_entry(mode, &connector->modes, head) {
288 if (drm_mode_width(mode) > width ||
289 drm_mode_height(mode) > height)
291 if (mode->type & DRM_MODE_TYPE_PREFERRED)
297 static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
302 enable = connector->status == connector_status_connected;
304 enable = connector->status != connector_status_disconnected;
309 static void drm_enable_connectors(struct drm_device *dev, bool *enabled)
311 bool any_enabled = false;
312 struct drm_connector *connector;
315 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
316 enabled[i] = drm_connector_enabled(connector, true);
317 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
318 enabled[i] ? "yes" : "no");
319 any_enabled |= enabled[i];
327 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
328 enabled[i] = drm_connector_enabled(connector, false);
333 static bool drm_target_preferred(struct drm_device *dev,
334 struct drm_display_mode **modes,
335 bool *enabled, int width, int height)
337 struct drm_connector *connector;
340 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
342 if (enabled[i] == false) {
347 DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
350 modes[i] = drm_has_preferred_mode(connector, width, height);
351 /* No preferred modes, pick one off the list */
352 if (!modes[i] && !list_empty(&connector->modes)) {
353 list_for_each_entry(modes[i], &connector->modes, head)
356 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
363 static int drm_pick_crtcs(struct drm_device *dev,
364 struct drm_crtc **best_crtcs,
365 struct drm_display_mode **modes,
366 int n, int width, int height)
369 struct drm_connector *connector;
370 struct drm_connector_helper_funcs *connector_funcs;
371 struct drm_encoder *encoder;
372 struct drm_crtc *best_crtc;
373 int my_score, best_score, score;
374 struct drm_crtc **crtcs, *crtc;
376 if (n == dev->mode_config.num_connector)
379 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
385 best_crtcs[n] = NULL;
387 best_score = drm_pick_crtcs(dev, best_crtcs, modes, n+1, width, height);
388 if (modes[n] == NULL)
391 crtcs = kmalloc(dev->mode_config.num_connector *
392 sizeof(struct drm_crtc *), GFP_KERNEL);
397 if (connector->status == connector_status_connected)
399 if (drm_has_preferred_mode(connector, width, height))
402 connector_funcs = connector->helper_private;
403 encoder = connector_funcs->best_encoder(connector);
407 connector->encoder = encoder;
409 /* select a crtc for this connector and then attempt to configure
410 remaining connectors */
412 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
414 if ((connector->encoder->possible_crtcs & (1 << c)) == 0) {
419 for (o = 0; o < n; o++)
420 if (best_crtcs[o] == crtc)
424 /* ignore cloning for now */
430 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_crtc *));
431 score = my_score + drm_pick_crtcs(dev, crtcs, modes, n + 1,
433 if (score > best_score) {
436 memcpy(best_crtcs, crtcs,
437 dev->mode_config.num_connector *
438 sizeof(struct drm_crtc *));
447 static void drm_setup_crtcs(struct drm_device *dev)
449 struct drm_crtc **crtcs;
450 struct drm_display_mode **modes;
451 struct drm_encoder *encoder;
452 struct drm_connector *connector;
459 width = dev->mode_config.max_width;
460 height = dev->mode_config.max_height;
462 /* clean out all the encoder/crtc combos */
463 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
464 encoder->crtc = NULL;
467 crtcs = kcalloc(dev->mode_config.num_connector,
468 sizeof(struct drm_crtc *), GFP_KERNEL);
469 modes = kcalloc(dev->mode_config.num_connector,
470 sizeof(struct drm_display_mode *), GFP_KERNEL);
471 enabled = kcalloc(dev->mode_config.num_connector,
472 sizeof(bool), GFP_KERNEL);
474 drm_enable_connectors(dev, enabled);
476 ret = drm_target_preferred(dev, modes, enabled, width, height);
478 DRM_ERROR("Unable to find initial modes\n");
480 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
482 drm_pick_crtcs(dev, crtcs, modes, 0, width, height);
485 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
486 struct drm_display_mode *mode = modes[i];
487 struct drm_crtc *crtc = crtcs[i];
489 if (connector->encoder == NULL) {
495 DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
496 mode->name, crtc->base.id);
497 crtc->desired_mode = mode;
498 connector->encoder->crtc = crtc;
500 connector->encoder->crtc = NULL;
510 * drm_encoder_crtc_ok - can a given crtc drive a given encoder?
511 * @encoder: encoder to test
512 * @crtc: crtc to test
514 * Return false if @encoder can't be driven by @crtc, true otherwise.
516 static bool drm_encoder_crtc_ok(struct drm_encoder *encoder,
517 struct drm_crtc *crtc)
519 struct drm_device *dev;
520 struct drm_crtc *tmp;
523 WARN(!crtc, "checking null crtc?");
527 list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
533 if (encoder->possible_crtcs & crtc_mask)
539 * Check the CRTC we're going to map each output to vs. its current
540 * CRTC. If they don't match, we have to disable the output and the CRTC
541 * since the driver will have to re-route things.
544 drm_crtc_prepare_encoders(struct drm_device *dev)
546 struct drm_encoder_helper_funcs *encoder_funcs;
547 struct drm_encoder *encoder;
549 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
550 encoder_funcs = encoder->helper_private;
551 /* Disable unused encoders */
552 if (encoder->crtc == NULL)
553 (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
554 /* Disable encoders whose CRTC is about to change */
555 if (encoder_funcs->get_crtc &&
556 encoder->crtc != (*encoder_funcs->get_crtc)(encoder))
557 (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
562 * drm_crtc_set_mode - set a mode
563 * @crtc: CRTC to program
569 * Caller must hold mode config lock.
571 * Try to set @mode on @crtc. Give @crtc and its associated connectors a chance
572 * to fixup or reject the mode prior to trying to set it.
575 * True if the mode was set successfully, or false otherwise.
577 bool drm_crtc_helper_set_mode(struct drm_crtc *crtc,
578 struct drm_display_mode *mode,
580 struct drm_framebuffer *old_fb)
582 struct drm_device *dev = crtc->dev;
583 struct drm_display_mode *adjusted_mode, saved_mode;
584 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
585 struct drm_encoder_helper_funcs *encoder_funcs;
586 int saved_x, saved_y;
587 struct drm_encoder *encoder;
590 adjusted_mode = drm_mode_duplicate(dev, mode);
592 crtc->enabled = drm_helper_crtc_in_use(crtc);
597 saved_mode = crtc->mode;
601 /* Update crtc values up front so the driver can rely on them for mode
608 /* Pass our mode to the connectors and the CRTC to give them a chance to
609 * adjust it according to limitations or connector properties, and also
610 * a chance to reject the mode entirely.
612 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
614 if (encoder->crtc != crtc)
616 encoder_funcs = encoder->helper_private;
617 if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
623 if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) {
627 /* Prepare the encoders and CRTCs before setting the mode. */
628 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
630 if (encoder->crtc != crtc)
632 encoder_funcs = encoder->helper_private;
633 /* Disable the encoders as the first thing we do. */
634 encoder_funcs->prepare(encoder);
637 drm_crtc_prepare_encoders(dev);
639 crtc_funcs->prepare(crtc);
641 /* Set up the DPLL and any encoders state that needs to adjust or depend
644 ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
648 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
650 if (encoder->crtc != crtc)
653 DRM_INFO("%s: set mode %s %x\n", drm_get_encoder_name(encoder),
654 mode->name, mode->base.id);
655 encoder_funcs = encoder->helper_private;
656 encoder_funcs->mode_set(encoder, mode, adjusted_mode);
659 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
660 crtc_funcs->commit(crtc);
662 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
664 if (encoder->crtc != crtc)
667 encoder_funcs = encoder->helper_private;
668 encoder_funcs->commit(encoder);
672 /* XXX free adjustedmode */
673 drm_mode_destroy(dev, adjusted_mode);
674 /* FIXME: add subpixel order */
677 crtc->mode = saved_mode;
684 EXPORT_SYMBOL(drm_crtc_helper_set_mode);
688 * drm_crtc_helper_set_config - set a new config from userspace
689 * @crtc: CRTC to setup
690 * @crtc_info: user provided configuration
691 * @new_mode: new mode to set
692 * @connector_set: set of connectors for the new config
693 * @fb: new framebuffer
696 * Caller must hold mode config lock.
698 * Setup a new configuration, provided by the user in @crtc_info, and enable
704 int drm_crtc_helper_set_config(struct drm_mode_set *set)
706 struct drm_device *dev;
707 struct drm_crtc **save_crtcs, *new_crtc;
708 struct drm_encoder **save_encoders, *new_encoder;
709 struct drm_framebuffer *old_fb = NULL;
711 bool mode_changed = false; /* if true do a full mode set */
712 bool fb_changed = false; /* if true and !mode_changed just do a flip */
713 struct drm_connector *connector;
714 int count = 0, ro, fail = 0;
715 struct drm_crtc_helper_funcs *crtc_funcs;
726 if (!set->crtc->helper_private)
729 crtc_funcs = set->crtc->helper_private;
731 DRM_DEBUG_KMS("crtc: %p %d fb: %p connectors: %p num_connectors:"
732 " %d (x, y) (%i, %i)\n",
733 set->crtc, set->crtc->base.id, set->fb, set->connectors,
734 (int)set->num_connectors, set->x, set->y);
736 dev = set->crtc->dev;
738 /* save previous config */
739 save_enabled = set->crtc->enabled;
742 * We do mode_config.num_connectors here since we'll look at the
743 * CRTC and encoder associated with each connector later.
745 save_crtcs = kzalloc(dev->mode_config.num_connector *
746 sizeof(struct drm_crtc *), GFP_KERNEL);
750 save_encoders = kzalloc(dev->mode_config.num_connector *
751 sizeof(struct drm_encoders *), GFP_KERNEL);
752 if (!save_encoders) {
757 /* We should be able to check here if the fb has the same properties
758 * and then just flip_or_move it */
759 if (set->crtc->fb != set->fb) {
760 /* If we have no fb then treat it as a full mode set */
761 if (set->crtc->fb == NULL) {
762 DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
764 } else if (set->fb == NULL) {
766 } else if ((set->fb->bits_per_pixel !=
767 set->crtc->fb->bits_per_pixel) ||
768 set->fb->depth != set->crtc->fb->depth)
774 if (set->x != set->crtc->x || set->y != set->crtc->y)
777 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
778 DRM_DEBUG_KMS("modes are different, full mode set\n");
779 drm_mode_debug_printmodeline(&set->crtc->mode);
780 drm_mode_debug_printmodeline(set->mode);
784 /* a) traverse passed in connector list and get encoders for them */
786 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
787 struct drm_connector_helper_funcs *connector_funcs =
788 connector->helper_private;
789 save_encoders[count++] = connector->encoder;
790 new_encoder = connector->encoder;
791 for (ro = 0; ro < set->num_connectors; ro++) {
792 if (set->connectors[ro] == connector) {
793 new_encoder = connector_funcs->best_encoder(connector);
794 /* if we can't get an encoder for a connector
795 we are setting now - then fail */
796 if (new_encoder == NULL)
797 /* don't break so fail path works correct */
803 if (new_encoder != connector->encoder) {
804 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
806 connector->encoder = new_encoder;
812 goto fail_no_encoder;
816 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
817 if (!connector->encoder)
820 save_crtcs[count++] = connector->encoder->crtc;
822 if (connector->encoder->crtc == set->crtc)
825 new_crtc = connector->encoder->crtc;
827 for (ro = 0; ro < set->num_connectors; ro++) {
828 if (set->connectors[ro] == connector)
829 new_crtc = set->crtc;
832 /* Make sure the new CRTC will work with the encoder */
834 !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
838 if (new_crtc != connector->encoder->crtc) {
839 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
841 connector->encoder->crtc = new_crtc;
843 DRM_DEBUG_KMS("setting connector %d crtc to %p\n",
844 connector->base.id, new_crtc);
847 /* mode_set_base is not a required function */
848 if (fb_changed && !crtc_funcs->mode_set_base)
852 old_fb = set->crtc->fb;
853 set->crtc->fb = set->fb;
854 set->crtc->enabled = (set->mode != NULL);
855 if (set->mode != NULL) {
856 DRM_DEBUG_KMS("attempting to set mode from"
858 drm_mode_debug_printmodeline(set->mode);
859 if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
862 DRM_ERROR("failed to set mode on crtc %p\n",
867 /* TODO are these needed? */
868 set->crtc->desired_x = set->x;
869 set->crtc->desired_y = set->y;
870 set->crtc->desired_mode = set->mode;
872 drm_helper_disable_unused_functions(dev);
873 } else if (fb_changed) {
874 old_fb = set->crtc->fb;
875 if (set->crtc->fb != set->fb)
876 set->crtc->fb = set->fb;
877 ret = crtc_funcs->mode_set_base(set->crtc,
878 set->x, set->y, old_fb);
883 kfree(save_encoders);
888 set->crtc->enabled = save_enabled;
889 set->crtc->fb = old_fb;
891 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
892 if (!connector->encoder)
895 connector->encoder->crtc = save_crtcs[count++];
900 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
901 connector->encoder = save_encoders[count++];
903 kfree(save_encoders);
906 EXPORT_SYMBOL(drm_crtc_helper_set_config);
908 bool drm_helper_plugged_event(struct drm_device *dev)
912 drm_helper_probe_connector_modes(dev, dev->mode_config.max_width,
913 dev->mode_config.max_height);
915 drm_setup_crtcs(dev);
917 /* alert the driver fb layer */
918 dev->mode_config.funcs->fb_changed(dev);
920 /* FIXME: send hotplug event */
924 * drm_initial_config - setup a sane initial connector configuration
928 * Called at init time, must take mode config lock.
930 * Scan the CRTCs and connectors and try to put together an initial setup.
931 * At the moment, this is a cloned configuration across all heads with
932 * a new framebuffer object as the backing store.
935 * Zero if everything went ok, nonzero otherwise.
937 bool drm_helper_initial_config(struct drm_device *dev)
939 struct drm_connector *connector;
942 count = drm_helper_probe_connector_modes(dev,
943 dev->mode_config.max_width,
944 dev->mode_config.max_height);
947 * None of the available connectors had any modes, so add some
948 * and try to light them up anyway
951 DRM_ERROR("connectors have no modes, using standard modes\n");
952 list_for_each_entry(connector,
953 &dev->mode_config.connector_list,
955 drm_helper_add_std_modes(dev, connector);
958 drm_setup_crtcs(dev);
960 /* alert the driver fb layer */
961 dev->mode_config.funcs->fb_changed(dev);
965 EXPORT_SYMBOL(drm_helper_initial_config);
967 static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder)
969 int dpms = DRM_MODE_DPMS_OFF;
970 struct drm_connector *connector;
971 struct drm_device *dev = encoder->dev;
973 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
974 if (connector->encoder == encoder)
975 if (connector->dpms < dpms)
976 dpms = connector->dpms;
980 static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc)
982 int dpms = DRM_MODE_DPMS_OFF;
983 struct drm_connector *connector;
984 struct drm_device *dev = crtc->dev;
986 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
987 if (connector->encoder && connector->encoder->crtc == crtc)
988 if (connector->dpms < dpms)
989 dpms = connector->dpms;
994 * drm_helper_connector_dpms
995 * @connector affected connector
998 * Calls the low-level connector DPMS function, then
999 * calls appropriate encoder and crtc DPMS functions as well
1001 void drm_helper_connector_dpms(struct drm_connector *connector, int mode)
1003 struct drm_encoder *encoder = connector->encoder;
1004 struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
1007 if (mode == connector->dpms)
1010 old_dpms = connector->dpms;
1011 connector->dpms = mode;
1013 /* from off to on, do crtc then encoder */
1014 if (mode < old_dpms) {
1016 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
1017 if (crtc_funcs->dpms)
1018 (*crtc_funcs->dpms) (crtc,
1019 drm_helper_choose_crtc_dpms(crtc));
1022 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
1023 if (encoder_funcs->dpms)
1024 (*encoder_funcs->dpms) (encoder,
1025 drm_helper_choose_encoder_dpms(encoder));
1029 /* from on to off, do encoder then crtc */
1030 if (mode > old_dpms) {
1032 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
1033 if (encoder_funcs->dpms)
1034 (*encoder_funcs->dpms) (encoder,
1035 drm_helper_choose_encoder_dpms(encoder));
1038 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
1039 if (crtc_funcs->dpms)
1040 (*crtc_funcs->dpms) (crtc,
1041 drm_helper_choose_crtc_dpms(crtc));
1047 EXPORT_SYMBOL(drm_helper_connector_dpms);
1050 * drm_hotplug_stage_two
1052 * @connector hotpluged connector
1055 * Caller must hold mode config lock, function might grab struct lock.
1057 * Stage two of a hotplug.
1060 * Zero on success, errno on failure.
1062 int drm_helper_hotplug_stage_two(struct drm_device *dev)
1064 drm_helper_plugged_event(dev);
1068 EXPORT_SYMBOL(drm_helper_hotplug_stage_two);
1070 int drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb,
1071 struct drm_mode_fb_cmd *mode_cmd)
1073 fb->width = mode_cmd->width;
1074 fb->height = mode_cmd->height;
1075 fb->pitch = mode_cmd->pitch;
1076 fb->bits_per_pixel = mode_cmd->bpp;
1077 fb->depth = mode_cmd->depth;
1081 EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
1083 int drm_helper_resume_force_mode(struct drm_device *dev)
1085 struct drm_crtc *crtc;
1088 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1093 ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
1094 crtc->x, crtc->y, crtc->fb);
1097 DRM_ERROR("failed to set mode on crtc %p\n", crtc);
1099 /* disable the unused connectors while restoring the modesetting */
1100 drm_helper_disable_unused_functions(dev);
1103 EXPORT_SYMBOL(drm_helper_resume_force_mode);