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 if (encoder_funcs->dpms)
285 (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
289 * drm_helper_disable_unused_functions - disable unused objects
293 * Caller must hold mode config lock.
295 * If an connector or CRTC isn't part of @dev's mode_config, it can be disabled
296 * by calling its dpms function, which should power it off.
298 void drm_helper_disable_unused_functions(struct drm_device *dev)
300 struct drm_encoder *encoder;
301 struct drm_connector *connector;
302 struct drm_crtc *crtc;
304 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
305 if (!connector->encoder)
307 if (connector->status == connector_status_disconnected)
308 connector->encoder = NULL;
311 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
312 if (!drm_helper_encoder_in_use(encoder)) {
313 drm_encoder_disable(encoder);
314 /* disconnector encoder from any connector */
315 encoder->crtc = NULL;
319 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
320 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
321 crtc->enabled = drm_helper_crtc_in_use(crtc);
322 if (!crtc->enabled) {
323 if (crtc_funcs->disable)
324 (*crtc_funcs->disable)(crtc);
326 if (crtc_funcs->dpms)
327 (*crtc_funcs->dpms)(crtc, DRM_MODE_DPMS_OFF);
332 EXPORT_SYMBOL(drm_helper_disable_unused_functions);
335 * drm_encoder_crtc_ok - can a given crtc drive a given encoder?
336 * @encoder: encoder to test
337 * @crtc: crtc to test
339 * Return false if @encoder can't be driven by @crtc, true otherwise.
341 static bool drm_encoder_crtc_ok(struct drm_encoder *encoder,
342 struct drm_crtc *crtc)
344 struct drm_device *dev;
345 struct drm_crtc *tmp;
348 WARN(!crtc, "checking null crtc?\n");
352 list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
358 if (encoder->possible_crtcs & crtc_mask)
364 * Check the CRTC we're going to map each output to vs. its current
365 * CRTC. If they don't match, we have to disable the output and the CRTC
366 * since the driver will have to re-route things.
369 drm_crtc_prepare_encoders(struct drm_device *dev)
371 struct drm_encoder_helper_funcs *encoder_funcs;
372 struct drm_encoder *encoder;
374 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
375 encoder_funcs = encoder->helper_private;
376 /* Disable unused encoders */
377 if (encoder->crtc == NULL)
378 drm_encoder_disable(encoder);
379 /* Disable encoders whose CRTC is about to change */
380 if (encoder_funcs->get_crtc &&
381 encoder->crtc != (*encoder_funcs->get_crtc)(encoder))
382 drm_encoder_disable(encoder);
387 * drm_crtc_helper_set_mode - internal helper to set a mode
388 * @crtc: CRTC to program
390 * @x: horizontal offset into the surface
391 * @y: vertical offset into the surface
392 * @old_fb: old framebuffer, for cleanup
395 * Caller must hold mode config lock.
397 * Try to set @mode on @crtc. Give @crtc and its associated connectors a chance
398 * to fixup or reject the mode prior to trying to set it. This is an internal
399 * helper that drivers could e.g. use to update properties that require the
400 * entire output pipe to be disabled and re-enabled in a new configuration. For
401 * example for changing whether audio is enabled on a hdmi link or for changing
402 * panel fitter or dither attributes. It is also called by the
403 * drm_crtc_helper_set_config() helper function to drive the mode setting
407 * True if the mode was set successfully, or false otherwise.
409 bool drm_crtc_helper_set_mode(struct drm_crtc *crtc,
410 struct drm_display_mode *mode,
412 struct drm_framebuffer *old_fb)
414 struct drm_device *dev = crtc->dev;
415 struct drm_display_mode *adjusted_mode, saved_mode, saved_hwmode;
416 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
417 struct drm_encoder_helper_funcs *encoder_funcs;
418 int saved_x, saved_y;
419 struct drm_encoder *encoder;
422 crtc->enabled = drm_helper_crtc_in_use(crtc);
426 adjusted_mode = drm_mode_duplicate(dev, mode);
430 saved_hwmode = crtc->hwmode;
431 saved_mode = crtc->mode;
435 /* Update crtc values up front so the driver can rely on them for mode
442 /* Pass our mode to the connectors and the CRTC to give them a chance to
443 * adjust it according to limitations or connector properties, and also
444 * a chance to reject the mode entirely.
446 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
448 if (encoder->crtc != crtc)
450 encoder_funcs = encoder->helper_private;
451 if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
453 DRM_DEBUG_KMS("Encoder fixup failed\n");
458 if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) {
459 DRM_DEBUG_KMS("CRTC fixup failed\n");
462 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
464 /* Prepare the encoders and CRTCs before setting the mode. */
465 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
467 if (encoder->crtc != crtc)
469 encoder_funcs = encoder->helper_private;
470 /* Disable the encoders as the first thing we do. */
471 encoder_funcs->prepare(encoder);
474 drm_crtc_prepare_encoders(dev);
476 crtc_funcs->prepare(crtc);
478 /* Set up the DPLL and any encoders state that needs to adjust or depend
481 ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
485 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
487 if (encoder->crtc != crtc)
490 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
491 encoder->base.id, drm_get_encoder_name(encoder),
492 mode->base.id, mode->name);
493 encoder_funcs = encoder->helper_private;
494 encoder_funcs->mode_set(encoder, mode, adjusted_mode);
497 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
498 crtc_funcs->commit(crtc);
500 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
502 if (encoder->crtc != crtc)
505 encoder_funcs = encoder->helper_private;
506 encoder_funcs->commit(encoder);
510 /* Store real post-adjustment hardware mode. */
511 crtc->hwmode = *adjusted_mode;
513 /* Calculate and store various constants which
514 * are later needed by vblank and swap-completion
515 * timestamping. They are derived from true hwmode.
517 drm_calc_timestamping_constants(crtc);
519 /* FIXME: add subpixel order */
521 drm_mode_destroy(dev, adjusted_mode);
523 crtc->hwmode = saved_hwmode;
524 crtc->mode = saved_mode;
531 EXPORT_SYMBOL(drm_crtc_helper_set_mode);
535 drm_crtc_helper_disable(struct drm_crtc *crtc)
537 struct drm_device *dev = crtc->dev;
538 struct drm_connector *connector;
539 struct drm_encoder *encoder;
541 /* Decouple all encoders and their attached connectors from this crtc */
542 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
543 if (encoder->crtc != crtc)
546 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
547 if (connector->encoder != encoder)
550 connector->encoder = NULL;
554 drm_helper_disable_unused_functions(dev);
559 * drm_crtc_helper_set_config - set a new config from userspace
560 * @set: mode set configuration
563 * Caller must hold mode config lock.
565 * Setup a new configuration, provided by the upper layers (either an ioctl call
566 * from userspace or internally e.g. from the fbdev suppport code) in @set, and
567 * enable it. This is the main helper functions for drivers that implement
568 * kernel mode setting with the crtc helper functions and the assorted
569 * ->prepare(), ->modeset() and ->commit() helper callbacks.
572 * Returns 0 on success, -ERRNO on failure.
574 int drm_crtc_helper_set_config(struct drm_mode_set *set)
576 struct drm_device *dev;
577 struct drm_crtc *save_crtcs, *new_crtc, *crtc;
578 struct drm_encoder *save_encoders, *new_encoder, *encoder;
579 struct drm_framebuffer *old_fb = NULL;
580 bool mode_changed = false; /* if true do a full mode set */
581 bool fb_changed = false; /* if true and !mode_changed just do a flip */
582 struct drm_connector *save_connectors, *connector;
583 int count = 0, ro, fail = 0;
584 struct drm_crtc_helper_funcs *crtc_funcs;
585 struct drm_mode_set save_set;
597 if (!set->crtc->helper_private)
600 crtc_funcs = set->crtc->helper_private;
606 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
607 set->crtc->base.id, set->fb->base.id,
608 (int)set->num_connectors, set->x, set->y);
610 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
611 return drm_crtc_helper_disable(set->crtc);
614 dev = set->crtc->dev;
616 /* Allocate space for the backup of all (non-pointer) crtc, encoder and
618 save_crtcs = kzalloc(dev->mode_config.num_crtc *
619 sizeof(struct drm_crtc), GFP_KERNEL);
623 save_encoders = kzalloc(dev->mode_config.num_encoder *
624 sizeof(struct drm_encoder), GFP_KERNEL);
625 if (!save_encoders) {
630 save_connectors = kzalloc(dev->mode_config.num_connector *
631 sizeof(struct drm_connector), GFP_KERNEL);
632 if (!save_connectors) {
634 kfree(save_encoders);
638 /* Copy data. Note that driver private data is not affected.
639 * Should anything bad happen only the expected state is
640 * restored, not the drivers personal bookkeeping.
643 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
644 save_crtcs[count++] = *crtc;
648 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
649 save_encoders[count++] = *encoder;
653 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
654 save_connectors[count++] = *connector;
657 save_set.crtc = set->crtc;
658 save_set.mode = &set->crtc->mode;
659 save_set.x = set->crtc->x;
660 save_set.y = set->crtc->y;
661 save_set.fb = set->crtc->fb;
663 /* We should be able to check here if the fb has the same properties
664 * and then just flip_or_move it */
665 if (set->crtc->fb != set->fb) {
666 /* If we have no fb then treat it as a full mode set */
667 if (set->crtc->fb == NULL) {
668 DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
670 } else if (set->fb == NULL) {
672 } else if (set->fb->depth != set->crtc->fb->depth) {
674 } else if (set->fb->bits_per_pixel !=
675 set->crtc->fb->bits_per_pixel) {
677 } else if (set->fb->pixel_format !=
678 set->crtc->fb->pixel_format) {
684 if (set->x != set->crtc->x || set->y != set->crtc->y)
687 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
688 DRM_DEBUG_KMS("modes are different, full mode set\n");
689 drm_mode_debug_printmodeline(&set->crtc->mode);
690 drm_mode_debug_printmodeline(set->mode);
694 /* a) traverse passed in connector list and get encoders for them */
696 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
697 struct drm_connector_helper_funcs *connector_funcs =
698 connector->helper_private;
699 new_encoder = connector->encoder;
700 for (ro = 0; ro < set->num_connectors; ro++) {
701 if (set->connectors[ro] == connector) {
702 new_encoder = connector_funcs->best_encoder(connector);
703 /* if we can't get an encoder for a connector
704 we are setting now - then fail */
705 if (new_encoder == NULL)
706 /* don't break so fail path works correct */
712 if (new_encoder != connector->encoder) {
713 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
715 /* If the encoder is reused for another connector, then
716 * the appropriate crtc will be set later.
718 if (connector->encoder)
719 connector->encoder->crtc = NULL;
720 connector->encoder = new_encoder;
730 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
731 if (!connector->encoder)
734 if (connector->encoder->crtc == set->crtc)
737 new_crtc = connector->encoder->crtc;
739 for (ro = 0; ro < set->num_connectors; ro++) {
740 if (set->connectors[ro] == connector)
741 new_crtc = set->crtc;
744 /* Make sure the new CRTC will work with the encoder */
746 !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
750 if (new_crtc != connector->encoder->crtc) {
751 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
753 connector->encoder->crtc = new_crtc;
756 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
757 connector->base.id, drm_get_connector_name(connector),
760 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
761 connector->base.id, drm_get_connector_name(connector));
765 /* mode_set_base is not a required function */
766 if (fb_changed && !crtc_funcs->mode_set_base)
770 set->crtc->enabled = drm_helper_crtc_in_use(set->crtc);
771 if (set->crtc->enabled) {
772 DRM_DEBUG_KMS("attempting to set mode from"
774 drm_mode_debug_printmodeline(set->mode);
775 old_fb = set->crtc->fb;
776 set->crtc->fb = set->fb;
777 if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
780 DRM_ERROR("failed to set mode on [CRTC:%d]\n",
782 set->crtc->fb = old_fb;
786 DRM_DEBUG_KMS("Setting connector DPMS state to on\n");
787 for (i = 0; i < set->num_connectors; i++) {
788 DRM_DEBUG_KMS("\t[CONNECTOR:%d:%s] set DPMS on\n", set->connectors[i]->base.id,
789 drm_get_connector_name(set->connectors[i]));
790 set->connectors[i]->funcs->dpms(set->connectors[i], DRM_MODE_DPMS_ON);
793 drm_helper_disable_unused_functions(dev);
794 } else if (fb_changed) {
795 set->crtc->x = set->x;
796 set->crtc->y = set->y;
798 old_fb = set->crtc->fb;
799 if (set->crtc->fb != set->fb)
800 set->crtc->fb = set->fb;
801 ret = crtc_funcs->mode_set_base(set->crtc,
802 set->x, set->y, old_fb);
804 set->crtc->fb = old_fb;
809 kfree(save_connectors);
810 kfree(save_encoders);
815 /* Restore all previous data. */
817 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
818 *crtc = save_crtcs[count++];
822 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
823 *encoder = save_encoders[count++];
827 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
828 *connector = save_connectors[count++];
831 /* Try to restore the config */
833 !drm_crtc_helper_set_mode(save_set.crtc, save_set.mode, save_set.x,
834 save_set.y, save_set.fb))
835 DRM_ERROR("failed to restore config after modeset failure\n");
837 kfree(save_connectors);
838 kfree(save_encoders);
842 EXPORT_SYMBOL(drm_crtc_helper_set_config);
844 static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder)
846 int dpms = DRM_MODE_DPMS_OFF;
847 struct drm_connector *connector;
848 struct drm_device *dev = encoder->dev;
850 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
851 if (connector->encoder == encoder)
852 if (connector->dpms < dpms)
853 dpms = connector->dpms;
857 static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc)
859 int dpms = DRM_MODE_DPMS_OFF;
860 struct drm_connector *connector;
861 struct drm_device *dev = crtc->dev;
863 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
864 if (connector->encoder && connector->encoder->crtc == crtc)
865 if (connector->dpms < dpms)
866 dpms = connector->dpms;
871 * drm_helper_connector_dpms() - connector dpms helper implementation
872 * @connector: affected connector
875 * This is the main helper function provided by the crtc helper framework for
876 * implementing the DPMS connector attribute. It computes the new desired DPMS
877 * state for all encoders and crtcs in the output mesh and calls the ->dpms()
878 * callback provided by the driver appropriately.
880 void drm_helper_connector_dpms(struct drm_connector *connector, int mode)
882 struct drm_encoder *encoder = connector->encoder;
883 struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
886 if (mode == connector->dpms)
889 old_dpms = connector->dpms;
890 connector->dpms = mode;
892 /* from off to on, do crtc then encoder */
893 if (mode < old_dpms) {
895 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
896 if (crtc_funcs->dpms)
897 (*crtc_funcs->dpms) (crtc,
898 drm_helper_choose_crtc_dpms(crtc));
901 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
902 if (encoder_funcs->dpms)
903 (*encoder_funcs->dpms) (encoder,
904 drm_helper_choose_encoder_dpms(encoder));
908 /* from on to off, do encoder then crtc */
909 if (mode > old_dpms) {
911 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
912 if (encoder_funcs->dpms)
913 (*encoder_funcs->dpms) (encoder,
914 drm_helper_choose_encoder_dpms(encoder));
917 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
918 if (crtc_funcs->dpms)
919 (*crtc_funcs->dpms) (crtc,
920 drm_helper_choose_crtc_dpms(crtc));
926 EXPORT_SYMBOL(drm_helper_connector_dpms);
928 int drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb,
929 struct drm_mode_fb_cmd2 *mode_cmd)
933 fb->width = mode_cmd->width;
934 fb->height = mode_cmd->height;
935 for (i = 0; i < 4; i++) {
936 fb->pitches[i] = mode_cmd->pitches[i];
937 fb->offsets[i] = mode_cmd->offsets[i];
939 drm_fb_get_bpp_depth(mode_cmd->pixel_format, &fb->depth,
940 &fb->bits_per_pixel);
941 fb->pixel_format = mode_cmd->pixel_format;
945 EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
947 int drm_helper_resume_force_mode(struct drm_device *dev)
949 struct drm_crtc *crtc;
950 struct drm_encoder *encoder;
951 struct drm_encoder_helper_funcs *encoder_funcs;
952 struct drm_crtc_helper_funcs *crtc_funcs;
955 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
960 ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
961 crtc->x, crtc->y, crtc->fb);
964 DRM_ERROR("failed to set mode on crtc %p\n", crtc);
966 /* Turn off outputs that were already powered off */
967 if (drm_helper_choose_crtc_dpms(crtc)) {
968 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
970 if(encoder->crtc != crtc)
973 encoder_funcs = encoder->helper_private;
974 if (encoder_funcs->dpms)
975 (*encoder_funcs->dpms) (encoder,
976 drm_helper_choose_encoder_dpms(encoder));
979 crtc_funcs = crtc->helper_private;
980 if (crtc_funcs->dpms)
981 (*crtc_funcs->dpms) (crtc,
982 drm_helper_choose_crtc_dpms(crtc));
985 /* disable the unused connectors while restoring the modesetting */
986 drm_helper_disable_unused_functions(dev);
989 EXPORT_SYMBOL(drm_helper_resume_force_mode);
991 void drm_kms_helper_hotplug_event(struct drm_device *dev)
993 /* send a uevent + call fbdev */
995 drm_sysfs_hotplug_event(dev);
997 if (dev->mode_config.funcs->output_poll_changed)
998 dev->mode_config.funcs->output_poll_changed(dev);
1000 EXPORT_SYMBOL(drm_kms_helper_hotplug_event);
1002 #define DRM_OUTPUT_POLL_PERIOD (10*HZ)
1003 static void output_poll_execute(struct work_struct *work)
1005 struct delayed_work *delayed_work = to_delayed_work(work);
1006 struct drm_device *dev = container_of(delayed_work, struct drm_device, mode_config.output_poll_work);
1007 struct drm_connector *connector;
1008 enum drm_connector_status old_status;
1009 bool repoll = false, changed = false;
1011 if (!drm_kms_helper_poll)
1014 mutex_lock(&dev->mode_config.mutex);
1015 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1017 /* Ignore forced connectors. */
1018 if (connector->force)
1021 /* Ignore HPD capable connectors and connectors where we don't
1022 * want any hotplug detection at all for polling. */
1023 if (!connector->polled || connector->polled == DRM_CONNECTOR_POLL_HPD)
1028 old_status = connector->status;
1029 /* if we are connected and don't want to poll for disconnect
1031 if (old_status == connector_status_connected &&
1032 !(connector->polled & DRM_CONNECTOR_POLL_DISCONNECT))
1035 connector->status = connector->funcs->detect(connector, false);
1036 if (old_status != connector->status) {
1037 const char *old, *new;
1039 old = drm_get_connector_status_name(old_status);
1040 new = drm_get_connector_status_name(connector->status);
1042 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] "
1043 "status updated from %s to %s\n",
1045 drm_get_connector_name(connector),
1052 mutex_unlock(&dev->mode_config.mutex);
1055 drm_kms_helper_hotplug_event(dev);
1058 schedule_delayed_work(delayed_work, DRM_OUTPUT_POLL_PERIOD);
1061 void drm_kms_helper_poll_disable(struct drm_device *dev)
1063 if (!dev->mode_config.poll_enabled)
1065 cancel_delayed_work_sync(&dev->mode_config.output_poll_work);
1067 EXPORT_SYMBOL(drm_kms_helper_poll_disable);
1069 void drm_kms_helper_poll_enable(struct drm_device *dev)
1072 struct drm_connector *connector;
1074 if (!dev->mode_config.poll_enabled || !drm_kms_helper_poll)
1077 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1078 if (connector->polled & (DRM_CONNECTOR_POLL_CONNECT |
1079 DRM_CONNECTOR_POLL_DISCONNECT))
1084 schedule_delayed_work(&dev->mode_config.output_poll_work, DRM_OUTPUT_POLL_PERIOD);
1086 EXPORT_SYMBOL(drm_kms_helper_poll_enable);
1088 void drm_kms_helper_poll_init(struct drm_device *dev)
1090 INIT_DELAYED_WORK(&dev->mode_config.output_poll_work, output_poll_execute);
1091 dev->mode_config.poll_enabled = true;
1093 drm_kms_helper_poll_enable(dev);
1095 EXPORT_SYMBOL(drm_kms_helper_poll_init);
1097 void drm_kms_helper_poll_fini(struct drm_device *dev)
1099 drm_kms_helper_poll_disable(dev);
1101 EXPORT_SYMBOL(drm_kms_helper_poll_fini);
1103 void drm_helper_hpd_irq_event(struct drm_device *dev)
1105 struct drm_connector *connector;
1106 enum drm_connector_status old_status;
1107 bool changed = false;
1109 if (!dev->mode_config.poll_enabled)
1112 mutex_lock(&dev->mode_config.mutex);
1113 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1115 /* Only handle HPD capable connectors. */
1116 if (!(connector->polled & DRM_CONNECTOR_POLL_HPD))
1119 old_status = connector->status;
1121 connector->status = connector->funcs->detect(connector, false);
1122 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
1124 drm_get_connector_name(connector),
1125 drm_get_connector_status_name(old_status),
1126 drm_get_connector_status_name(connector->status));
1127 if (old_status != connector->status)
1131 mutex_unlock(&dev->mode_config.mutex);
1134 drm_kms_helper_hotplug_event(dev);
1136 EXPORT_SYMBOL(drm_helper_hpd_irq_event);