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);
65 MODULE_AUTHOR("David Airlie, Jesse Barnes");
66 MODULE_DESCRIPTION("DRM KMS helper");
69 * drm_helper_move_panel_connectors_to_head() - move panels to the front in the
71 * @dev: drm device to operate on
73 * Some userspace presumes that the first connected connector is the main
74 * display, where it's supposed to display e.g. the login screen. For
75 * laptops, this should be the main panel. Use this function to sort all
76 * (eDP/LVDS) panels to the front of the connector list, instead of
77 * painstakingly trying to initialize them in the right order.
79 void drm_helper_move_panel_connectors_to_head(struct drm_device *dev)
81 struct drm_connector *connector, *tmp;
82 struct list_head panel_list;
84 INIT_LIST_HEAD(&panel_list);
86 list_for_each_entry_safe(connector, tmp,
87 &dev->mode_config.connector_list, head) {
88 if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS ||
89 connector->connector_type == DRM_MODE_CONNECTOR_eDP)
90 list_move_tail(&connector->head, &panel_list);
93 list_splice(&panel_list, &dev->mode_config.connector_list);
95 EXPORT_SYMBOL(drm_helper_move_panel_connectors_to_head);
97 static bool drm_kms_helper_poll = true;
98 module_param_named(poll, drm_kms_helper_poll, bool, 0600);
100 static void drm_mode_validate_flag(struct drm_connector *connector,
103 struct drm_display_mode *mode;
105 if (flags == (DRM_MODE_FLAG_DBLSCAN | DRM_MODE_FLAG_INTERLACE |
106 DRM_MODE_FLAG_3D_MASK))
109 list_for_each_entry(mode, &connector->modes, head) {
110 if ((mode->flags & DRM_MODE_FLAG_INTERLACE) &&
111 !(flags & DRM_MODE_FLAG_INTERLACE))
112 mode->status = MODE_NO_INTERLACE;
113 if ((mode->flags & DRM_MODE_FLAG_DBLSCAN) &&
114 !(flags & DRM_MODE_FLAG_DBLSCAN))
115 mode->status = MODE_NO_DBLESCAN;
116 if ((mode->flags & DRM_MODE_FLAG_3D_MASK) &&
117 !(flags & DRM_MODE_FLAG_3D_MASK))
118 mode->status = MODE_NO_STEREO;
125 * drm_helper_probe_single_connector_modes - get complete set of display modes
126 * @connector: connector to probe
127 * @maxX: max width for modes
128 * @maxY: max height for modes
131 * Caller must hold mode config lock.
133 * Based on the helper callbacks implemented by @connector try to detect all
134 * valid modes. Modes will first be added to the connector's probed_modes list,
135 * then culled (based on validity and the @maxX, @maxY parameters) and put into
136 * the normal modes list.
138 * Intended to be use as a generic implementation of the ->probe() @connector
139 * callback for drivers that use the crtc helpers for output mode filtering and
143 * Number of modes found on @connector.
145 int drm_helper_probe_single_connector_modes(struct drm_connector *connector,
146 uint32_t maxX, uint32_t maxY)
148 struct drm_device *dev = connector->dev;
149 struct drm_display_mode *mode;
150 struct drm_connector_helper_funcs *connector_funcs =
151 connector->helper_private;
154 bool verbose_prune = true;
156 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", connector->base.id,
157 drm_get_connector_name(connector));
158 /* set all modes to the unverified state */
159 list_for_each_entry(mode, &connector->modes, head)
160 mode->status = MODE_UNVERIFIED;
162 if (connector->force) {
163 if (connector->force == DRM_FORCE_ON)
164 connector->status = connector_status_connected;
166 connector->status = connector_status_disconnected;
167 if (connector->funcs->force)
168 connector->funcs->force(connector);
170 connector->status = connector->funcs->detect(connector, true);
173 /* Re-enable polling in case the global poll config changed. */
174 if (drm_kms_helper_poll != dev->mode_config.poll_running)
175 drm_kms_helper_poll_enable(dev);
177 dev->mode_config.poll_running = drm_kms_helper_poll;
179 if (connector->status == connector_status_disconnected) {
180 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] disconnected\n",
181 connector->base.id, drm_get_connector_name(connector));
182 drm_mode_connector_update_edid_property(connector, NULL);
183 verbose_prune = false;
187 #ifdef CONFIG_DRM_LOAD_EDID_FIRMWARE
188 count = drm_load_edid_firmware(connector);
191 count = (*connector_funcs->get_modes)(connector);
193 if (count == 0 && connector->status == connector_status_connected)
194 count = drm_add_modes_noedid(connector, 1024, 768);
198 drm_mode_connector_list_update(connector);
201 drm_mode_validate_size(dev, &connector->modes, maxX,
204 if (connector->interlace_allowed)
205 mode_flags |= DRM_MODE_FLAG_INTERLACE;
206 if (connector->doublescan_allowed)
207 mode_flags |= DRM_MODE_FLAG_DBLSCAN;
208 if (connector->stereo_allowed)
209 mode_flags |= DRM_MODE_FLAG_3D_MASK;
210 drm_mode_validate_flag(connector, mode_flags);
212 list_for_each_entry(mode, &connector->modes, head) {
213 if (mode->status == MODE_OK)
214 mode->status = connector_funcs->mode_valid(connector,
219 drm_mode_prune_invalid(dev, &connector->modes, verbose_prune);
221 if (list_empty(&connector->modes))
224 list_for_each_entry(mode, &connector->modes, head)
225 mode->vrefresh = drm_mode_vrefresh(mode);
227 drm_mode_sort(&connector->modes);
229 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] probed modes :\n", connector->base.id,
230 drm_get_connector_name(connector));
231 list_for_each_entry(mode, &connector->modes, head) {
232 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
233 drm_mode_debug_printmodeline(mode);
238 EXPORT_SYMBOL(drm_helper_probe_single_connector_modes);
241 * drm_helper_encoder_in_use - check if a given encoder is in use
242 * @encoder: encoder to check
245 * Caller must hold mode config lock.
247 * Walk @encoders's DRM device's mode_config and see if it's in use.
250 * True if @encoder is part of the mode_config, false otherwise.
252 bool drm_helper_encoder_in_use(struct drm_encoder *encoder)
254 struct drm_connector *connector;
255 struct drm_device *dev = encoder->dev;
256 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
257 if (connector->encoder == encoder)
261 EXPORT_SYMBOL(drm_helper_encoder_in_use);
264 * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config
265 * @crtc: CRTC to check
268 * Caller must hold mode config lock.
270 * Walk @crtc's DRM device's mode_config and see if it's in use.
273 * True if @crtc is part of the mode_config, false otherwise.
275 bool drm_helper_crtc_in_use(struct drm_crtc *crtc)
277 struct drm_encoder *encoder;
278 struct drm_device *dev = crtc->dev;
279 /* FIXME: Locking around list access? */
280 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
281 if (encoder->crtc == crtc && drm_helper_encoder_in_use(encoder))
285 EXPORT_SYMBOL(drm_helper_crtc_in_use);
288 drm_encoder_disable(struct drm_encoder *encoder)
290 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
292 if (encoder_funcs->disable)
293 (*encoder_funcs->disable)(encoder);
295 if (encoder_funcs->dpms)
296 (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
300 * drm_helper_disable_unused_functions - disable unused objects
304 * Caller must hold mode config lock.
306 * If an connector or CRTC isn't part of @dev's mode_config, it can be disabled
307 * by calling its dpms function, which should power it off.
309 void drm_helper_disable_unused_functions(struct drm_device *dev)
311 struct drm_encoder *encoder;
312 struct drm_connector *connector;
313 struct drm_crtc *crtc;
315 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
316 if (!connector->encoder)
318 if (connector->status == connector_status_disconnected)
319 connector->encoder = NULL;
322 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
323 if (!drm_helper_encoder_in_use(encoder)) {
324 drm_encoder_disable(encoder);
325 /* disconnector encoder from any connector */
326 encoder->crtc = NULL;
330 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
331 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
332 crtc->enabled = drm_helper_crtc_in_use(crtc);
333 if (!crtc->enabled) {
334 if (crtc_funcs->disable)
335 (*crtc_funcs->disable)(crtc);
337 if (crtc_funcs->dpms)
338 (*crtc_funcs->dpms)(crtc, DRM_MODE_DPMS_OFF);
343 EXPORT_SYMBOL(drm_helper_disable_unused_functions);
346 * Check the CRTC we're going to map each output to vs. its current
347 * CRTC. If they don't match, we have to disable the output and the CRTC
348 * since the driver will have to re-route things.
351 drm_crtc_prepare_encoders(struct drm_device *dev)
353 struct drm_encoder_helper_funcs *encoder_funcs;
354 struct drm_encoder *encoder;
356 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
357 encoder_funcs = encoder->helper_private;
358 /* Disable unused encoders */
359 if (encoder->crtc == NULL)
360 drm_encoder_disable(encoder);
361 /* Disable encoders whose CRTC is about to change */
362 if (encoder_funcs->get_crtc &&
363 encoder->crtc != (*encoder_funcs->get_crtc)(encoder))
364 drm_encoder_disable(encoder);
369 * drm_crtc_helper_set_mode - internal helper to set a mode
370 * @crtc: CRTC to program
372 * @x: horizontal offset into the surface
373 * @y: vertical offset into the surface
374 * @old_fb: old framebuffer, for cleanup
377 * Caller must hold mode config lock.
379 * Try to set @mode on @crtc. Give @crtc and its associated connectors a chance
380 * to fixup or reject the mode prior to trying to set it. This is an internal
381 * helper that drivers could e.g. use to update properties that require the
382 * entire output pipe to be disabled and re-enabled in a new configuration. For
383 * example for changing whether audio is enabled on a hdmi link or for changing
384 * panel fitter or dither attributes. It is also called by the
385 * drm_crtc_helper_set_config() helper function to drive the mode setting
389 * True if the mode was set successfully, or false otherwise.
391 bool drm_crtc_helper_set_mode(struct drm_crtc *crtc,
392 struct drm_display_mode *mode,
394 struct drm_framebuffer *old_fb)
396 struct drm_device *dev = crtc->dev;
397 struct drm_display_mode *adjusted_mode, saved_mode;
398 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
399 struct drm_encoder_helper_funcs *encoder_funcs;
400 int saved_x, saved_y;
402 struct drm_encoder *encoder;
405 saved_enabled = crtc->enabled;
406 crtc->enabled = drm_helper_crtc_in_use(crtc);
410 adjusted_mode = drm_mode_duplicate(dev, mode);
411 if (!adjusted_mode) {
412 crtc->enabled = saved_enabled;
416 saved_mode = crtc->mode;
420 /* Update crtc values up front so the driver can rely on them for mode
427 /* Pass our mode to the connectors and the CRTC to give them a chance to
428 * adjust it according to limitations or connector properties, and also
429 * a chance to reject the mode entirely.
431 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
433 if (encoder->crtc != crtc)
435 encoder_funcs = encoder->helper_private;
436 if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
438 DRM_DEBUG_KMS("Encoder fixup failed\n");
443 if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) {
444 DRM_DEBUG_KMS("CRTC fixup failed\n");
447 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
449 /* Prepare the encoders and CRTCs before setting the mode. */
450 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
452 if (encoder->crtc != crtc)
454 encoder_funcs = encoder->helper_private;
455 /* Disable the encoders as the first thing we do. */
456 encoder_funcs->prepare(encoder);
459 drm_crtc_prepare_encoders(dev);
461 crtc_funcs->prepare(crtc);
463 /* Set up the DPLL and any encoders state that needs to adjust or depend
466 ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
470 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
472 if (encoder->crtc != crtc)
475 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
476 encoder->base.id, drm_get_encoder_name(encoder),
477 mode->base.id, mode->name);
478 encoder_funcs = encoder->helper_private;
479 encoder_funcs->mode_set(encoder, mode, adjusted_mode);
482 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
483 crtc_funcs->commit(crtc);
485 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
487 if (encoder->crtc != crtc)
490 encoder_funcs = encoder->helper_private;
491 encoder_funcs->commit(encoder);
495 /* Store real post-adjustment hardware mode. */
496 crtc->hwmode = *adjusted_mode;
498 /* Calculate and store various constants which
499 * are later needed by vblank and swap-completion
500 * timestamping. They are derived from true hwmode.
502 drm_calc_timestamping_constants(crtc, &crtc->hwmode);
504 /* FIXME: add subpixel order */
506 drm_mode_destroy(dev, adjusted_mode);
508 crtc->enabled = saved_enabled;
509 crtc->mode = saved_mode;
516 EXPORT_SYMBOL(drm_crtc_helper_set_mode);
520 drm_crtc_helper_disable(struct drm_crtc *crtc)
522 struct drm_device *dev = crtc->dev;
523 struct drm_connector *connector;
524 struct drm_encoder *encoder;
526 /* Decouple all encoders and their attached connectors from this crtc */
527 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
528 if (encoder->crtc != crtc)
531 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
532 if (connector->encoder != encoder)
535 connector->encoder = NULL;
538 * drm_helper_disable_unused_functions() ought to be
539 * doing this, but since we've decoupled the encoder
540 * from the connector above, the required connection
541 * between them is henceforth no longer available.
543 connector->dpms = DRM_MODE_DPMS_OFF;
547 drm_helper_disable_unused_functions(dev);
552 * drm_crtc_helper_set_config - set a new config from userspace
553 * @set: mode set configuration
556 * Caller must hold mode config lock.
558 * Setup a new configuration, provided by the upper layers (either an ioctl call
559 * from userspace or internally e.g. from the fbdev suppport code) in @set, and
560 * enable it. This is the main helper functions for drivers that implement
561 * kernel mode setting with the crtc helper functions and the assorted
562 * ->prepare(), ->modeset() and ->commit() helper callbacks.
565 * Returns 0 on success, -ERRNO on failure.
567 int drm_crtc_helper_set_config(struct drm_mode_set *set)
569 struct drm_device *dev;
570 struct drm_crtc *new_crtc;
571 struct drm_encoder *save_encoders, *new_encoder, *encoder;
572 bool mode_changed = false; /* if true do a full mode set */
573 bool fb_changed = false; /* if true and !mode_changed just do a flip */
574 struct drm_connector *save_connectors, *connector;
575 int count = 0, ro, fail = 0;
576 struct drm_crtc_helper_funcs *crtc_funcs;
577 struct drm_mode_set save_set;
585 BUG_ON(!set->crtc->helper_private);
587 /* Enforce sane interface api - has been abused by the fb helper. */
588 BUG_ON(!set->mode && set->fb);
589 BUG_ON(set->fb && set->num_connectors == 0);
591 crtc_funcs = set->crtc->helper_private;
597 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
598 set->crtc->base.id, set->fb->base.id,
599 (int)set->num_connectors, set->x, set->y);
601 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
602 return drm_crtc_helper_disable(set->crtc);
605 dev = set->crtc->dev;
608 * Allocate space for the backup of all (non-pointer) encoder and
611 save_encoders = kzalloc(dev->mode_config.num_encoder *
612 sizeof(struct drm_encoder), GFP_KERNEL);
616 save_connectors = kzalloc(dev->mode_config.num_connector *
617 sizeof(struct drm_connector), GFP_KERNEL);
618 if (!save_connectors) {
619 kfree(save_encoders);
624 * Copy data. Note that driver private data is not affected.
625 * Should anything bad happen only the expected state is
626 * restored, not the drivers personal bookkeeping.
629 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
630 save_encoders[count++] = *encoder;
634 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
635 save_connectors[count++] = *connector;
638 save_set.crtc = set->crtc;
639 save_set.mode = &set->crtc->mode;
640 save_set.x = set->crtc->x;
641 save_set.y = set->crtc->y;
642 save_set.fb = set->crtc->fb;
644 /* We should be able to check here if the fb has the same properties
645 * and then just flip_or_move it */
646 if (set->crtc->fb != set->fb) {
647 /* If we have no fb then treat it as a full mode set */
648 if (set->crtc->fb == NULL) {
649 DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
651 } else if (set->fb == NULL) {
653 } else if (set->fb->pixel_format !=
654 set->crtc->fb->pixel_format) {
660 if (set->x != set->crtc->x || set->y != set->crtc->y)
663 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
664 DRM_DEBUG_KMS("modes are different, full mode set\n");
665 drm_mode_debug_printmodeline(&set->crtc->mode);
666 drm_mode_debug_printmodeline(set->mode);
670 /* a) traverse passed in connector list and get encoders for them */
672 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
673 struct drm_connector_helper_funcs *connector_funcs =
674 connector->helper_private;
675 new_encoder = connector->encoder;
676 for (ro = 0; ro < set->num_connectors; ro++) {
677 if (set->connectors[ro] == connector) {
678 new_encoder = connector_funcs->best_encoder(connector);
679 /* if we can't get an encoder for a connector
680 we are setting now - then fail */
681 if (new_encoder == NULL)
682 /* don't break so fail path works correct */
686 if (connector->dpms != DRM_MODE_DPMS_ON) {
687 DRM_DEBUG_KMS("connector dpms not on, full mode switch\n");
693 if (new_encoder != connector->encoder) {
694 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
696 /* If the encoder is reused for another connector, then
697 * the appropriate crtc will be set later.
699 if (connector->encoder)
700 connector->encoder->crtc = NULL;
701 connector->encoder = new_encoder;
711 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
712 if (!connector->encoder)
715 if (connector->encoder->crtc == set->crtc)
718 new_crtc = connector->encoder->crtc;
720 for (ro = 0; ro < set->num_connectors; ro++) {
721 if (set->connectors[ro] == connector)
722 new_crtc = set->crtc;
725 /* Make sure the new CRTC will work with the encoder */
727 !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
731 if (new_crtc != connector->encoder->crtc) {
732 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
734 connector->encoder->crtc = new_crtc;
737 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
738 connector->base.id, drm_get_connector_name(connector),
741 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
742 connector->base.id, drm_get_connector_name(connector));
746 /* mode_set_base is not a required function */
747 if (fb_changed && !crtc_funcs->mode_set_base)
751 if (drm_helper_crtc_in_use(set->crtc)) {
752 DRM_DEBUG_KMS("attempting to set mode from"
754 drm_mode_debug_printmodeline(set->mode);
755 set->crtc->fb = set->fb;
756 if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
759 DRM_ERROR("failed to set mode on [CRTC:%d]\n",
761 set->crtc->fb = save_set.fb;
765 DRM_DEBUG_KMS("Setting connector DPMS state to on\n");
766 for (i = 0; i < set->num_connectors; i++) {
767 DRM_DEBUG_KMS("\t[CONNECTOR:%d:%s] set DPMS on\n", set->connectors[i]->base.id,
768 drm_get_connector_name(set->connectors[i]));
769 set->connectors[i]->funcs->dpms(set->connectors[i], DRM_MODE_DPMS_ON);
772 drm_helper_disable_unused_functions(dev);
773 } else if (fb_changed) {
774 set->crtc->x = set->x;
775 set->crtc->y = set->y;
776 set->crtc->fb = set->fb;
777 ret = crtc_funcs->mode_set_base(set->crtc,
778 set->x, set->y, save_set.fb);
780 set->crtc->x = save_set.x;
781 set->crtc->y = save_set.y;
782 set->crtc->fb = save_set.fb;
787 kfree(save_connectors);
788 kfree(save_encoders);
792 /* Restore all previous data. */
794 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
795 *encoder = save_encoders[count++];
799 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
800 *connector = save_connectors[count++];
803 /* Try to restore the config */
805 !drm_crtc_helper_set_mode(save_set.crtc, save_set.mode, save_set.x,
806 save_set.y, save_set.fb))
807 DRM_ERROR("failed to restore config after modeset failure\n");
809 kfree(save_connectors);
810 kfree(save_encoders);
813 EXPORT_SYMBOL(drm_crtc_helper_set_config);
815 static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder)
817 int dpms = DRM_MODE_DPMS_OFF;
818 struct drm_connector *connector;
819 struct drm_device *dev = encoder->dev;
821 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
822 if (connector->encoder == encoder)
823 if (connector->dpms < dpms)
824 dpms = connector->dpms;
828 static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc)
830 int dpms = DRM_MODE_DPMS_OFF;
831 struct drm_connector *connector;
832 struct drm_device *dev = crtc->dev;
834 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
835 if (connector->encoder && connector->encoder->crtc == crtc)
836 if (connector->dpms < dpms)
837 dpms = connector->dpms;
842 * drm_helper_connector_dpms() - connector dpms helper implementation
843 * @connector: affected connector
846 * This is the main helper function provided by the crtc helper framework for
847 * implementing the DPMS connector attribute. It computes the new desired DPMS
848 * state for all encoders and crtcs in the output mesh and calls the ->dpms()
849 * callback provided by the driver appropriately.
851 void drm_helper_connector_dpms(struct drm_connector *connector, int mode)
853 struct drm_encoder *encoder = connector->encoder;
854 struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
857 if (mode == connector->dpms)
860 old_dpms = connector->dpms;
861 connector->dpms = mode;
863 /* from off to on, do crtc then encoder */
864 if (mode < old_dpms) {
866 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
867 if (crtc_funcs->dpms)
868 (*crtc_funcs->dpms) (crtc,
869 drm_helper_choose_crtc_dpms(crtc));
872 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
873 if (encoder_funcs->dpms)
874 (*encoder_funcs->dpms) (encoder,
875 drm_helper_choose_encoder_dpms(encoder));
879 /* from on to off, do encoder then crtc */
880 if (mode > old_dpms) {
882 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
883 if (encoder_funcs->dpms)
884 (*encoder_funcs->dpms) (encoder,
885 drm_helper_choose_encoder_dpms(encoder));
888 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
889 if (crtc_funcs->dpms)
890 (*crtc_funcs->dpms) (crtc,
891 drm_helper_choose_crtc_dpms(crtc));
897 EXPORT_SYMBOL(drm_helper_connector_dpms);
899 int drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb,
900 struct drm_mode_fb_cmd2 *mode_cmd)
904 fb->width = mode_cmd->width;
905 fb->height = mode_cmd->height;
906 for (i = 0; i < 4; i++) {
907 fb->pitches[i] = mode_cmd->pitches[i];
908 fb->offsets[i] = mode_cmd->offsets[i];
910 drm_fb_get_bpp_depth(mode_cmd->pixel_format, &fb->depth,
911 &fb->bits_per_pixel);
912 fb->pixel_format = mode_cmd->pixel_format;
916 EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
918 int drm_helper_resume_force_mode(struct drm_device *dev)
920 struct drm_crtc *crtc;
921 struct drm_encoder *encoder;
922 struct drm_encoder_helper_funcs *encoder_funcs;
923 struct drm_crtc_helper_funcs *crtc_funcs;
926 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
931 ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
932 crtc->x, crtc->y, crtc->fb);
935 DRM_ERROR("failed to set mode on crtc %p\n", crtc);
937 /* Turn off outputs that were already powered off */
938 if (drm_helper_choose_crtc_dpms(crtc)) {
939 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
941 if(encoder->crtc != crtc)
944 encoder_funcs = encoder->helper_private;
945 if (encoder_funcs->dpms)
946 (*encoder_funcs->dpms) (encoder,
947 drm_helper_choose_encoder_dpms(encoder));
950 crtc_funcs = crtc->helper_private;
951 if (crtc_funcs->dpms)
952 (*crtc_funcs->dpms) (crtc,
953 drm_helper_choose_crtc_dpms(crtc));
956 /* disable the unused connectors while restoring the modesetting */
957 drm_helper_disable_unused_functions(dev);
960 EXPORT_SYMBOL(drm_helper_resume_force_mode);
962 void drm_kms_helper_hotplug_event(struct drm_device *dev)
964 /* send a uevent + call fbdev */
966 drm_sysfs_hotplug_event(dev);
968 if (dev->mode_config.funcs->output_poll_changed)
969 dev->mode_config.funcs->output_poll_changed(dev);
971 EXPORT_SYMBOL(drm_kms_helper_hotplug_event);
973 #define DRM_OUTPUT_POLL_PERIOD (10*HZ)
974 static void output_poll_execute(struct work_struct *work)
976 struct delayed_work *delayed_work = to_delayed_work(work);
977 struct drm_device *dev = container_of(delayed_work, struct drm_device, mode_config.output_poll_work);
978 struct drm_connector *connector;
979 enum drm_connector_status old_status;
980 bool repoll = false, changed = false;
982 if (!drm_kms_helper_poll)
985 mutex_lock(&dev->mode_config.mutex);
986 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
988 /* Ignore forced connectors. */
989 if (connector->force)
992 /* Ignore HPD capable connectors and connectors where we don't
993 * want any hotplug detection at all for polling. */
994 if (!connector->polled || connector->polled == DRM_CONNECTOR_POLL_HPD)
999 old_status = connector->status;
1000 /* if we are connected and don't want to poll for disconnect
1002 if (old_status == connector_status_connected &&
1003 !(connector->polled & DRM_CONNECTOR_POLL_DISCONNECT))
1006 connector->status = connector->funcs->detect(connector, false);
1007 if (old_status != connector->status) {
1008 const char *old, *new;
1010 old = drm_get_connector_status_name(old_status);
1011 new = drm_get_connector_status_name(connector->status);
1013 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] "
1014 "status updated from %s to %s\n",
1016 drm_get_connector_name(connector),
1023 mutex_unlock(&dev->mode_config.mutex);
1026 drm_kms_helper_hotplug_event(dev);
1029 schedule_delayed_work(delayed_work, DRM_OUTPUT_POLL_PERIOD);
1032 void drm_kms_helper_poll_disable(struct drm_device *dev)
1034 if (!dev->mode_config.poll_enabled)
1036 cancel_delayed_work_sync(&dev->mode_config.output_poll_work);
1038 EXPORT_SYMBOL(drm_kms_helper_poll_disable);
1040 void drm_kms_helper_poll_enable(struct drm_device *dev)
1043 struct drm_connector *connector;
1045 if (!dev->mode_config.poll_enabled || !drm_kms_helper_poll)
1048 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1049 if (connector->polled & (DRM_CONNECTOR_POLL_CONNECT |
1050 DRM_CONNECTOR_POLL_DISCONNECT))
1055 schedule_delayed_work(&dev->mode_config.output_poll_work, DRM_OUTPUT_POLL_PERIOD);
1057 EXPORT_SYMBOL(drm_kms_helper_poll_enable);
1059 void drm_kms_helper_poll_init(struct drm_device *dev)
1061 INIT_DELAYED_WORK(&dev->mode_config.output_poll_work, output_poll_execute);
1062 dev->mode_config.poll_enabled = true;
1064 drm_kms_helper_poll_enable(dev);
1066 EXPORT_SYMBOL(drm_kms_helper_poll_init);
1068 void drm_kms_helper_poll_fini(struct drm_device *dev)
1070 drm_kms_helper_poll_disable(dev);
1072 EXPORT_SYMBOL(drm_kms_helper_poll_fini);
1074 bool drm_helper_hpd_irq_event(struct drm_device *dev)
1076 struct drm_connector *connector;
1077 enum drm_connector_status old_status;
1078 bool changed = false;
1080 if (!dev->mode_config.poll_enabled)
1083 mutex_lock(&dev->mode_config.mutex);
1084 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1086 /* Only handle HPD capable connectors. */
1087 if (!(connector->polled & DRM_CONNECTOR_POLL_HPD))
1090 old_status = connector->status;
1092 connector->status = connector->funcs->detect(connector, false);
1093 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
1095 drm_get_connector_name(connector),
1096 drm_get_connector_status_name(old_status),
1097 drm_get_connector_status_name(connector->status));
1098 if (old_status != connector->status)
1102 mutex_unlock(&dev->mode_config.mutex);
1105 drm_kms_helper_hotplug_event(dev);
1109 EXPORT_SYMBOL(drm_helper_hpd_irq_event);