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 <uapi_drm/drm_fourcc.h>
38 #include <drm/drm_crtc_helper.h>
39 #include <drm/drm_fb_helper.h>
40 #include <drm/drm_edid.h>
43 drm_fetch_cmdline_mode_from_kenv(struct drm_connector *connector,
44 struct drm_cmdline_mode *cmdline_mode)
46 char *tun_var_name, *tun_mode;
47 static const char tun_prefix[] = "drm_mode.";
51 tun_var_name = kmalloc(sizeof(tun_prefix) +
52 strlen(drm_get_connector_name(connector)), M_TEMP, M_WAITOK);
53 strcpy(tun_var_name, tun_prefix);
54 strcat(tun_var_name, drm_get_connector_name(connector));
55 tun_mode = kgetenv(tun_var_name);
56 if (tun_mode != NULL) {
57 res = drm_mode_parse_command_line_for_connector(tun_mode,
58 connector, cmdline_mode);
61 drm_free(tun_var_name, M_TEMP);
66 * drm_helper_move_panel_connectors_to_head() - move panels to the front in the
68 * @dev: drm device to operate on
70 * Some userspace presumes that the first connected connector is the main
71 * display, where it's supposed to display e.g. the login screen. For
72 * laptops, this should be the main panel. Use this function to sort all
73 * (eDP/LVDS) panels to the front of the connector list, instead of
74 * painstakingly trying to initialize them in the right order.
76 void drm_helper_move_panel_connectors_to_head(struct drm_device *dev)
78 struct drm_connector *connector, *tmp;
79 struct list_head panel_list;
81 INIT_LIST_HEAD(&panel_list);
83 list_for_each_entry_safe(connector, tmp,
84 &dev->mode_config.connector_list, head) {
85 if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS ||
86 connector->connector_type == DRM_MODE_CONNECTOR_eDP)
87 list_move_tail(&connector->head, &panel_list);
90 list_splice(&panel_list, &dev->mode_config.connector_list);
92 EXPORT_SYMBOL(drm_helper_move_panel_connectors_to_head);
94 static bool drm_kms_helper_poll = true;
96 static void drm_mode_validate_flag(struct drm_connector *connector,
99 struct drm_display_mode *mode;
101 if (flags == (DRM_MODE_FLAG_DBLSCAN | DRM_MODE_FLAG_INTERLACE))
104 list_for_each_entry(mode, &connector->modes, head) {
105 if ((mode->flags & DRM_MODE_FLAG_INTERLACE) &&
106 !(flags & DRM_MODE_FLAG_INTERLACE))
107 mode->status = MODE_NO_INTERLACE;
108 if ((mode->flags & DRM_MODE_FLAG_DBLSCAN) &&
109 !(flags & DRM_MODE_FLAG_DBLSCAN))
110 mode->status = MODE_NO_DBLESCAN;
117 * drm_helper_probe_single_connector_modes - get complete set of display modes
118 * @connector: connector to probe
119 * @maxX: max width for modes
120 * @maxY: max height for modes
123 * Caller must hold mode config lock.
125 * Based on the helper callbacks implemented by @connector try to detect all
126 * valid modes. Modes will first be added to the connector's probed_modes list,
127 * then culled (based on validity and the @maxX, @maxY parameters) and put into
128 * the normal modes list.
130 * Intended to be use as a generic implementation of the ->probe() @connector
131 * callback for drivers that use the crtc helpers for output mode filtering and
135 * Number of modes found on @connector.
137 int drm_helper_probe_single_connector_modes(struct drm_connector *connector,
138 uint32_t maxX, uint32_t maxY)
140 struct drm_device *dev = connector->dev;
141 struct drm_display_mode *mode;
142 struct drm_connector_helper_funcs *connector_funcs =
143 connector->helper_private;
146 bool verbose_prune = true;
148 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", connector->base.id,
149 drm_get_connector_name(connector));
150 /* set all modes to the unverified state */
151 list_for_each_entry(mode, &connector->modes, head)
152 mode->status = MODE_UNVERIFIED;
154 if (connector->force) {
155 if (connector->force == DRM_FORCE_ON)
156 connector->status = connector_status_connected;
158 connector->status = connector_status_disconnected;
159 if (connector->funcs->force)
160 connector->funcs->force(connector);
162 connector->status = connector->funcs->detect(connector, true);
165 /* Re-enable polling in case the global poll config changed. */
166 if (drm_kms_helper_poll != dev->mode_config.poll_running)
167 drm_kms_helper_poll_enable(dev);
169 dev->mode_config.poll_running = drm_kms_helper_poll;
171 if (connector->status == connector_status_disconnected) {
172 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] disconnected\n",
173 connector->base.id, drm_get_connector_name(connector));
174 drm_mode_connector_update_edid_property(connector, NULL);
175 verbose_prune = false;
179 #ifdef CONFIG_DRM_LOAD_EDID_FIRMWARE
180 count = drm_load_edid_firmware(connector);
183 count = (*connector_funcs->get_modes)(connector);
185 if (count == 0 && connector->status == connector_status_connected)
186 count = drm_add_modes_noedid(connector, 1024, 768);
190 drm_mode_connector_list_update(connector);
193 drm_mode_validate_size(dev, &connector->modes, maxX,
196 if (connector->interlace_allowed)
197 mode_flags |= DRM_MODE_FLAG_INTERLACE;
198 if (connector->doublescan_allowed)
199 mode_flags |= DRM_MODE_FLAG_DBLSCAN;
200 drm_mode_validate_flag(connector, mode_flags);
202 list_for_each_entry(mode, &connector->modes, head) {
203 if (mode->status == MODE_OK)
204 mode->status = connector_funcs->mode_valid(connector,
209 drm_mode_prune_invalid(dev, &connector->modes, verbose_prune);
211 if (list_empty(&connector->modes))
214 drm_mode_sort(&connector->modes);
216 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] probed modes :\n", connector->base.id,
217 drm_get_connector_name(connector));
218 list_for_each_entry(mode, &connector->modes, head) {
219 mode->vrefresh = drm_mode_vrefresh(mode);
221 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
222 drm_mode_debug_printmodeline(mode);
227 EXPORT_SYMBOL(drm_helper_probe_single_connector_modes);
230 * drm_helper_encoder_in_use - check if a given encoder is in use
231 * @encoder: encoder to check
234 * Caller must hold mode config lock.
236 * Walk @encoders's DRM device's mode_config and see if it's in use.
239 * True if @encoder is part of the mode_config, false otherwise.
241 bool drm_helper_encoder_in_use(struct drm_encoder *encoder)
243 struct drm_connector *connector;
244 struct drm_device *dev = encoder->dev;
245 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
246 if (connector->encoder == encoder)
250 EXPORT_SYMBOL(drm_helper_encoder_in_use);
253 * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config
254 * @crtc: CRTC to check
257 * Caller must hold mode config lock.
259 * Walk @crtc's DRM device's mode_config and see if it's in use.
262 * True if @crtc is part of the mode_config, false otherwise.
264 bool drm_helper_crtc_in_use(struct drm_crtc *crtc)
266 struct drm_encoder *encoder;
267 struct drm_device *dev = crtc->dev;
268 /* FIXME: Locking around list access? */
269 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
270 if (encoder->crtc == crtc && drm_helper_encoder_in_use(encoder))
274 EXPORT_SYMBOL(drm_helper_crtc_in_use);
277 drm_encoder_disable(struct drm_encoder *encoder)
279 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
281 if (encoder_funcs->disable)
282 (*encoder_funcs->disable)(encoder);
284 (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
288 * drm_helper_disable_unused_functions - disable unused objects
292 * Caller must hold mode config lock.
294 * If an connector or CRTC isn't part of @dev's mode_config, it can be disabled
295 * by calling its dpms function, which should power it off.
297 void drm_helper_disable_unused_functions(struct drm_device *dev)
299 struct drm_encoder *encoder;
300 struct drm_connector *connector;
301 struct drm_crtc *crtc;
303 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
304 if (!connector->encoder)
306 if (connector->status == connector_status_disconnected)
307 connector->encoder = NULL;
310 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
311 if (!drm_helper_encoder_in_use(encoder)) {
312 drm_encoder_disable(encoder);
313 /* disconnector encoder from any connector */
314 encoder->crtc = NULL;
318 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
319 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
320 crtc->enabled = drm_helper_crtc_in_use(crtc);
321 if (!crtc->enabled) {
322 if (crtc_funcs->disable)
323 (*crtc_funcs->disable)(crtc);
325 (*crtc_funcs->dpms)(crtc, DRM_MODE_DPMS_OFF);
330 EXPORT_SYMBOL(drm_helper_disable_unused_functions);
333 * drm_encoder_crtc_ok - can a given crtc drive a given encoder?
334 * @encoder: encoder to test
335 * @crtc: crtc to test
337 * Return false if @encoder can't be driven by @crtc, true otherwise.
339 static bool drm_encoder_crtc_ok(struct drm_encoder *encoder,
340 struct drm_crtc *crtc)
342 struct drm_device *dev;
343 struct drm_crtc *tmp;
346 WARN(!crtc, "checking null crtc?\n");
350 list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
356 if (encoder->possible_crtcs & crtc_mask)
362 * Check the CRTC we're going to map each output to vs. its current
363 * CRTC. If they don't match, we have to disable the output and the CRTC
364 * since the driver will have to re-route things.
367 drm_crtc_prepare_encoders(struct drm_device *dev)
369 struct drm_encoder_helper_funcs *encoder_funcs;
370 struct drm_encoder *encoder;
372 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
373 encoder_funcs = encoder->helper_private;
374 /* Disable unused encoders */
375 if (encoder->crtc == NULL)
376 drm_encoder_disable(encoder);
377 /* Disable encoders whose CRTC is about to change */
378 if (encoder_funcs->get_crtc &&
379 encoder->crtc != (*encoder_funcs->get_crtc)(encoder))
380 drm_encoder_disable(encoder);
385 * drm_crtc_helper_set_mode - internal helper to set a mode
386 * @crtc: CRTC to program
388 * @x: horizontal offset into the surface
389 * @y: vertical offset into the surface
390 * @old_fb: old framebuffer, for cleanup
393 * Caller must hold mode config lock.
395 * Try to set @mode on @crtc. Give @crtc and its associated connectors a chance
396 * to fixup or reject the mode prior to trying to set it. This is an internal
397 * helper that drivers could e.g. use to update properties that require the
398 * entire output pipe to be disabled and re-enabled in a new configuration. For
399 * example for changing whether audio is enabled on a hdmi link or for changing
400 * panel fitter or dither attributes. It is also called by the
401 * drm_crtc_helper_set_config() helper function to drive the mode setting
405 * True if the mode was set successfully, or false otherwise.
407 bool drm_crtc_helper_set_mode(struct drm_crtc *crtc,
408 struct drm_display_mode *mode,
410 struct drm_framebuffer *old_fb)
412 struct drm_device *dev = crtc->dev;
413 struct drm_display_mode *adjusted_mode, saved_mode, saved_hwmode;
414 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
415 struct drm_encoder_helper_funcs *encoder_funcs;
416 int saved_x, saved_y;
417 struct drm_encoder *encoder;
420 crtc->enabled = drm_helper_crtc_in_use(crtc);
424 adjusted_mode = drm_mode_duplicate(dev, mode);
428 saved_hwmode = crtc->hwmode;
429 saved_mode = crtc->mode;
433 /* Update crtc values up front so the driver can rely on them for mode
440 /* Pass our mode to the connectors and the CRTC to give them a chance to
441 * adjust it according to limitations or connector properties, and also
442 * a chance to reject the mode entirely.
444 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
446 if (encoder->crtc != crtc)
448 encoder_funcs = encoder->helper_private;
449 if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
451 DRM_DEBUG_KMS("Encoder fixup failed\n");
456 if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) {
457 DRM_DEBUG_KMS("CRTC fixup failed\n");
460 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
462 /* Prepare the encoders and CRTCs before setting the mode. */
463 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
465 if (encoder->crtc != crtc)
467 encoder_funcs = encoder->helper_private;
468 /* Disable the encoders as the first thing we do. */
469 encoder_funcs->prepare(encoder);
472 drm_crtc_prepare_encoders(dev);
474 crtc_funcs->prepare(crtc);
476 /* Set up the DPLL and any encoders state that needs to adjust or depend
479 ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
483 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
485 if (encoder->crtc != crtc)
488 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
489 encoder->base.id, drm_get_encoder_name(encoder),
490 mode->base.id, mode->name);
491 encoder_funcs = encoder->helper_private;
492 encoder_funcs->mode_set(encoder, mode, adjusted_mode);
495 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
496 crtc_funcs->commit(crtc);
498 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
500 if (encoder->crtc != crtc)
503 encoder_funcs = encoder->helper_private;
504 encoder_funcs->commit(encoder);
508 /* Store real post-adjustment hardware mode. */
509 crtc->hwmode = *adjusted_mode;
511 /* Calculate and store various constants which
512 * are later needed by vblank and swap-completion
513 * timestamping. They are derived from true hwmode.
515 drm_calc_timestamping_constants(crtc);
517 /* FIXME: add subpixel order */
519 drm_mode_destroy(dev, adjusted_mode);
521 crtc->hwmode = saved_hwmode;
522 crtc->mode = saved_mode;
529 EXPORT_SYMBOL(drm_crtc_helper_set_mode);
533 drm_crtc_helper_disable(struct drm_crtc *crtc)
535 struct drm_device *dev = crtc->dev;
536 struct drm_connector *connector;
537 struct drm_encoder *encoder;
539 /* Decouple all encoders and their attached connectors from this crtc */
540 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
541 if (encoder->crtc != crtc)
544 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
545 if (connector->encoder != encoder)
548 connector->encoder = NULL;
552 drm_helper_disable_unused_functions(dev);
557 * drm_crtc_helper_set_config - set a new config from userspace
558 * @set: mode set configuration
561 * Caller must hold mode config lock.
563 * Setup a new configuration, provided by the upper layers (either an ioctl call
564 * from userspace or internally e.g. from the fbdev suppport code) in @set, and
565 * enable it. This is the main helper functions for drivers that implement
566 * kernel mode setting with the crtc helper functions and the assorted
567 * ->prepare(), ->modeset() and ->commit() helper callbacks.
570 * Returns 0 on success, -ERRNO on failure.
572 int drm_crtc_helper_set_config(struct drm_mode_set *set)
574 struct drm_device *dev;
575 struct drm_crtc *save_crtcs, *new_crtc, *crtc;
576 struct drm_encoder *save_encoders, *new_encoder, *encoder;
577 struct drm_framebuffer *old_fb = NULL;
578 bool mode_changed = false; /* if true do a full mode set */
579 bool fb_changed = false; /* if true and !mode_changed just do a flip */
580 struct drm_connector *save_connectors, *connector;
581 int count = 0, ro, fail = 0;
582 struct drm_crtc_helper_funcs *crtc_funcs;
583 struct drm_mode_set save_set;
595 if (!set->crtc->helper_private)
598 crtc_funcs = set->crtc->helper_private;
604 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
605 set->crtc->base.id, set->fb->base.id,
606 (int)set->num_connectors, set->x, set->y);
608 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
609 return drm_crtc_helper_disable(set->crtc);
612 dev = set->crtc->dev;
614 /* Allocate space for the backup of all (non-pointer) crtc, encoder and
616 save_crtcs = kzalloc(dev->mode_config.num_crtc *
617 sizeof(struct drm_crtc), GFP_KERNEL);
621 save_encoders = kzalloc(dev->mode_config.num_encoder *
622 sizeof(struct drm_encoder), GFP_KERNEL);
623 if (!save_encoders) {
628 save_connectors = kzalloc(dev->mode_config.num_connector *
629 sizeof(struct drm_connector), GFP_KERNEL);
630 if (!save_connectors) {
632 kfree(save_encoders);
636 /* Copy data. Note that driver private data is not affected.
637 * Should anything bad happen only the expected state is
638 * restored, not the drivers personal bookkeeping.
641 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
642 save_crtcs[count++] = *crtc;
646 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
647 save_encoders[count++] = *encoder;
651 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
652 save_connectors[count++] = *connector;
655 save_set.crtc = set->crtc;
656 save_set.mode = &set->crtc->mode;
657 save_set.x = set->crtc->x;
658 save_set.y = set->crtc->y;
659 save_set.fb = set->crtc->fb;
661 /* We should be able to check here if the fb has the same properties
662 * and then just flip_or_move it */
663 if (set->crtc->fb != set->fb) {
664 /* If we have no fb then treat it as a full mode set */
665 if (set->crtc->fb == NULL) {
666 DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
668 } else if (set->fb == NULL) {
670 } else if (set->fb->depth != set->crtc->fb->depth) {
672 } else if (set->fb->bits_per_pixel !=
673 set->crtc->fb->bits_per_pixel) {
675 } else if (set->fb->pixel_format !=
676 set->crtc->fb->pixel_format) {
682 if (set->x != set->crtc->x || set->y != set->crtc->y)
685 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
686 DRM_DEBUG_KMS("modes are different, full mode set\n");
687 drm_mode_debug_printmodeline(&set->crtc->mode);
688 drm_mode_debug_printmodeline(set->mode);
692 /* a) traverse passed in connector list and get encoders for them */
694 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
695 struct drm_connector_helper_funcs *connector_funcs =
696 connector->helper_private;
697 new_encoder = connector->encoder;
698 for (ro = 0; ro < set->num_connectors; ro++) {
699 if (set->connectors[ro] == connector) {
700 new_encoder = connector_funcs->best_encoder(connector);
701 /* if we can't get an encoder for a connector
702 we are setting now - then fail */
703 if (new_encoder == NULL)
704 /* don't break so fail path works correct */
710 if (new_encoder != connector->encoder) {
711 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
713 /* If the encoder is reused for another connector, then
714 * the appropriate crtc will be set later.
716 if (connector->encoder)
717 connector->encoder->crtc = NULL;
718 connector->encoder = new_encoder;
728 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
729 if (!connector->encoder)
732 if (connector->encoder->crtc == set->crtc)
735 new_crtc = connector->encoder->crtc;
737 for (ro = 0; ro < set->num_connectors; ro++) {
738 if (set->connectors[ro] == connector)
739 new_crtc = set->crtc;
742 /* Make sure the new CRTC will work with the encoder */
744 !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
748 if (new_crtc != connector->encoder->crtc) {
749 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
751 connector->encoder->crtc = new_crtc;
754 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
755 connector->base.id, drm_get_connector_name(connector),
758 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
759 connector->base.id, drm_get_connector_name(connector));
763 /* mode_set_base is not a required function */
764 if (fb_changed && !crtc_funcs->mode_set_base)
768 set->crtc->enabled = drm_helper_crtc_in_use(set->crtc);
769 if (set->crtc->enabled) {
770 DRM_DEBUG_KMS("attempting to set mode from"
772 drm_mode_debug_printmodeline(set->mode);
773 old_fb = set->crtc->fb;
774 set->crtc->fb = set->fb;
775 if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
778 DRM_ERROR("failed to set mode on [CRTC:%d]\n",
780 set->crtc->fb = old_fb;
784 DRM_DEBUG_KMS("Setting connector DPMS state to on\n");
785 for (i = 0; i < set->num_connectors; i++) {
786 DRM_DEBUG_KMS("\t[CONNECTOR:%d:%s] set DPMS on\n", set->connectors[i]->base.id,
787 drm_get_connector_name(set->connectors[i]));
788 set->connectors[i]->funcs->dpms(set->connectors[i], DRM_MODE_DPMS_ON);
791 drm_helper_disable_unused_functions(dev);
792 } else if (fb_changed) {
793 set->crtc->x = set->x;
794 set->crtc->y = set->y;
796 old_fb = set->crtc->fb;
797 if (set->crtc->fb != set->fb)
798 set->crtc->fb = set->fb;
799 ret = crtc_funcs->mode_set_base(set->crtc,
800 set->x, set->y, old_fb);
802 set->crtc->fb = old_fb;
807 kfree(save_connectors);
808 kfree(save_encoders);
813 /* Restore all previous data. */
815 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
816 *crtc = save_crtcs[count++];
820 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
821 *encoder = save_encoders[count++];
825 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
826 *connector = save_connectors[count++];
829 /* Try to restore the config */
831 !drm_crtc_helper_set_mode(save_set.crtc, save_set.mode, save_set.x,
832 save_set.y, save_set.fb))
833 DRM_ERROR("failed to restore config after modeset failure\n");
835 kfree(save_connectors);
836 kfree(save_encoders);
840 EXPORT_SYMBOL(drm_crtc_helper_set_config);
842 static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder)
844 int dpms = DRM_MODE_DPMS_OFF;
845 struct drm_connector *connector;
846 struct drm_device *dev = encoder->dev;
848 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
849 if (connector->encoder == encoder)
850 if (connector->dpms < dpms)
851 dpms = connector->dpms;
855 static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc)
857 int dpms = DRM_MODE_DPMS_OFF;
858 struct drm_connector *connector;
859 struct drm_device *dev = crtc->dev;
861 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
862 if (connector->encoder && connector->encoder->crtc == crtc)
863 if (connector->dpms < dpms)
864 dpms = connector->dpms;
869 * drm_helper_connector_dpms() - connector dpms helper implementation
870 * @connector: affected connector
873 * This is the main helper function provided by the crtc helper framework for
874 * implementing the DPMS connector attribute. It computes the new desired DPMS
875 * state for all encoders and crtcs in the output mesh and calls the ->dpms()
876 * callback provided by the driver appropriately.
878 void drm_helper_connector_dpms(struct drm_connector *connector, int mode)
880 struct drm_encoder *encoder = connector->encoder;
881 struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
884 if (mode == connector->dpms)
887 old_dpms = connector->dpms;
888 connector->dpms = mode;
890 /* from off to on, do crtc then encoder */
891 if (mode < old_dpms) {
893 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
894 if (crtc_funcs->dpms)
895 (*crtc_funcs->dpms) (crtc,
896 drm_helper_choose_crtc_dpms(crtc));
899 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
900 if (encoder_funcs->dpms)
901 (*encoder_funcs->dpms) (encoder,
902 drm_helper_choose_encoder_dpms(encoder));
906 /* from on to off, do encoder then crtc */
907 if (mode > old_dpms) {
909 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
910 if (encoder_funcs->dpms)
911 (*encoder_funcs->dpms) (encoder,
912 drm_helper_choose_encoder_dpms(encoder));
915 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
916 if (crtc_funcs->dpms)
917 (*crtc_funcs->dpms) (crtc,
918 drm_helper_choose_crtc_dpms(crtc));
924 EXPORT_SYMBOL(drm_helper_connector_dpms);
926 int drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb,
927 struct drm_mode_fb_cmd2 *mode_cmd)
931 fb->width = mode_cmd->width;
932 fb->height = mode_cmd->height;
933 for (i = 0; i < 4; i++) {
934 fb->pitches[i] = mode_cmd->pitches[i];
935 fb->offsets[i] = mode_cmd->offsets[i];
937 drm_fb_get_bpp_depth(mode_cmd->pixel_format, &fb->depth,
938 &fb->bits_per_pixel);
939 fb->pixel_format = mode_cmd->pixel_format;
943 EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
945 int drm_helper_resume_force_mode(struct drm_device *dev)
947 struct drm_crtc *crtc;
948 struct drm_encoder *encoder;
949 struct drm_encoder_helper_funcs *encoder_funcs;
950 struct drm_crtc_helper_funcs *crtc_funcs;
953 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
958 ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
959 crtc->x, crtc->y, crtc->fb);
962 DRM_ERROR("failed to set mode on crtc %p\n", crtc);
964 /* Turn off outputs that were already powered off */
965 if (drm_helper_choose_crtc_dpms(crtc)) {
966 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
968 if(encoder->crtc != crtc)
971 encoder_funcs = encoder->helper_private;
972 if (encoder_funcs->dpms)
973 (*encoder_funcs->dpms) (encoder,
974 drm_helper_choose_encoder_dpms(encoder));
977 crtc_funcs = crtc->helper_private;
978 if (crtc_funcs->dpms)
979 (*crtc_funcs->dpms) (crtc,
980 drm_helper_choose_crtc_dpms(crtc));
983 /* disable the unused connectors while restoring the modesetting */
984 drm_helper_disable_unused_functions(dev);
987 EXPORT_SYMBOL(drm_helper_resume_force_mode);
989 void drm_kms_helper_hotplug_event(struct drm_device *dev)
991 /* send a uevent + call fbdev */
993 drm_sysfs_hotplug_event(dev);
995 if (dev->mode_config.funcs->output_poll_changed)
996 dev->mode_config.funcs->output_poll_changed(dev);
998 EXPORT_SYMBOL(drm_kms_helper_hotplug_event);
1000 #define DRM_OUTPUT_POLL_PERIOD (10*HZ)
1001 static void output_poll_execute(struct work_struct *work)
1003 struct delayed_work *delayed_work = to_delayed_work(work);
1004 struct drm_device *dev = container_of(delayed_work, struct drm_device, mode_config.output_poll_work);
1005 struct drm_connector *connector;
1006 enum drm_connector_status old_status;
1007 bool repoll = false, changed = false;
1009 if (!drm_kms_helper_poll)
1012 mutex_lock(&dev->mode_config.mutex);
1013 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1015 /* Ignore forced connectors. */
1016 if (connector->force)
1019 /* Ignore HPD capable connectors and connectors where we don't
1020 * want any hotplug detection at all for polling. */
1021 if (!connector->polled || connector->polled == DRM_CONNECTOR_POLL_HPD)
1026 old_status = connector->status;
1027 /* if we are connected and don't want to poll for disconnect
1029 if (old_status == connector_status_connected &&
1030 !(connector->polled & DRM_CONNECTOR_POLL_DISCONNECT))
1033 connector->status = connector->funcs->detect(connector, false);
1034 if (old_status != connector->status) {
1035 const char *old, *new;
1037 old = drm_get_connector_status_name(old_status);
1038 new = drm_get_connector_status_name(connector->status);
1040 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] "
1041 "status updated from %s to %s\n",
1043 drm_get_connector_name(connector),
1050 mutex_unlock(&dev->mode_config.mutex);
1053 drm_kms_helper_hotplug_event(dev);
1056 schedule_delayed_work(delayed_work, DRM_OUTPUT_POLL_PERIOD);
1059 void drm_kms_helper_poll_disable(struct drm_device *dev)
1061 if (!dev->mode_config.poll_enabled)
1063 cancel_delayed_work_sync(&dev->mode_config.output_poll_work);
1065 EXPORT_SYMBOL(drm_kms_helper_poll_disable);
1067 void drm_kms_helper_poll_enable(struct drm_device *dev)
1070 struct drm_connector *connector;
1072 if (!dev->mode_config.poll_enabled || !drm_kms_helper_poll)
1075 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1076 if (connector->polled & (DRM_CONNECTOR_POLL_CONNECT |
1077 DRM_CONNECTOR_POLL_DISCONNECT))
1082 schedule_delayed_work(&dev->mode_config.output_poll_work, DRM_OUTPUT_POLL_PERIOD);
1084 EXPORT_SYMBOL(drm_kms_helper_poll_enable);
1086 void drm_kms_helper_poll_init(struct drm_device *dev)
1088 INIT_DELAYED_WORK(&dev->mode_config.output_poll_work, output_poll_execute);
1089 dev->mode_config.poll_enabled = true;
1091 drm_kms_helper_poll_enable(dev);
1093 EXPORT_SYMBOL(drm_kms_helper_poll_init);
1095 void drm_kms_helper_poll_fini(struct drm_device *dev)
1097 drm_kms_helper_poll_disable(dev);
1099 EXPORT_SYMBOL(drm_kms_helper_poll_fini);
1101 void drm_helper_hpd_irq_event(struct drm_device *dev)
1103 struct drm_connector *connector;
1104 enum drm_connector_status old_status;
1105 bool changed = false;
1107 if (!dev->mode_config.poll_enabled)
1110 mutex_lock(&dev->mode_config.mutex);
1111 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1113 /* Only handle HPD capable connectors. */
1114 if (!(connector->polled & DRM_CONNECTOR_POLL_HPD))
1117 old_status = connector->status;
1119 connector->status = connector->funcs->detect(connector, false);
1120 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
1122 drm_get_connector_name(connector),
1123 drm_get_connector_status_name(old_status),
1124 drm_get_connector_status_name(connector->status));
1125 if (old_status != connector->status)
1129 mutex_unlock(&dev->mode_config.mutex);
1132 drm_kms_helper_hotplug_event(dev);
1134 EXPORT_SYMBOL(drm_helper_hpd_irq_event);