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 <drm/drm_fourcc.h>
38 #include <drm/drm_crtc_helper.h>
39 #include <drm/drm_fb_helper.h>
40 #include <drm/drm_edid.h>
42 void drm_helper_move_panel_connectors_to_head(struct drm_device *dev)
44 struct drm_connector *connector, *tmp;
45 struct list_head panel_list;
47 INIT_LIST_HEAD(&panel_list);
49 list_for_each_entry_safe(connector, tmp,
50 &dev->mode_config.connector_list, head) {
51 if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS ||
52 connector->connector_type == DRM_MODE_CONNECTOR_eDP)
53 list_move_tail(&connector->head, &panel_list);
56 list_splice(&panel_list, &dev->mode_config.connector_list);
58 EXPORT_SYMBOL(drm_helper_move_panel_connectors_to_head);
60 static bool drm_kms_helper_poll = true;
61 module_param_named(poll, drm_kms_helper_poll, bool, 0600);
63 static void drm_mode_validate_flag(struct drm_connector *connector,
66 struct drm_display_mode *mode;
68 if (flags == (DRM_MODE_FLAG_DBLSCAN | DRM_MODE_FLAG_INTERLACE))
71 list_for_each_entry(mode, &connector->modes, head) {
72 if ((mode->flags & DRM_MODE_FLAG_INTERLACE) &&
73 !(flags & DRM_MODE_FLAG_INTERLACE))
74 mode->status = MODE_NO_INTERLACE;
75 if ((mode->flags & DRM_MODE_FLAG_DBLSCAN) &&
76 !(flags & DRM_MODE_FLAG_DBLSCAN))
77 mode->status = MODE_NO_DBLESCAN;
84 * drm_helper_probe_single_connector_modes - get complete set of display modes
86 * @maxX: max width for modes
87 * @maxY: max height for modes
90 * Caller must hold mode config lock.
92 * Based on @dev's mode_config layout, scan all the connectors and try to detect
93 * modes on them. Modes will first be added to the connector's probed_modes
94 * list, then culled (based on validity and the @maxX, @maxY parameters) and
95 * put into the normal modes list.
97 * Intended to be used either at bootup time or when major configuration
98 * changes have occurred.
100 * FIXME: take into account monitor limits
103 * Number of modes found on @connector.
105 int drm_helper_probe_single_connector_modes(struct drm_connector *connector,
106 uint32_t maxX, uint32_t maxY)
108 struct drm_device *dev = connector->dev;
109 struct drm_display_mode *mode;
110 struct drm_connector_helper_funcs *connector_funcs =
111 connector->helper_private;
115 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", connector->base.id,
116 drm_get_connector_name(connector));
117 /* set all modes to the unverified state */
118 list_for_each_entry(mode, &connector->modes, head)
119 mode->status = MODE_UNVERIFIED;
121 if (connector->force) {
122 if (connector->force == DRM_FORCE_ON)
123 connector->status = connector_status_connected;
125 connector->status = connector_status_disconnected;
126 if (connector->funcs->force)
127 connector->funcs->force(connector);
129 connector->status = connector->funcs->detect(connector, true);
132 /* Re-enable polling in case the global poll config changed. */
133 if (drm_kms_helper_poll != dev->mode_config.poll_running)
134 drm_kms_helper_poll_enable(dev);
136 dev->mode_config.poll_running = drm_kms_helper_poll;
138 if (connector->status == connector_status_disconnected) {
139 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] disconnected\n",
140 connector->base.id, drm_get_connector_name(connector));
141 drm_mode_connector_update_edid_property(connector, NULL);
145 #ifdef CONFIG_DRM_LOAD_EDID_FIRMWARE
146 count = drm_load_edid_firmware(connector);
149 count = (*connector_funcs->get_modes)(connector);
151 if (count == 0 && connector->status == connector_status_connected)
152 count = drm_add_modes_noedid(connector, 1024, 768);
156 drm_mode_connector_list_update(connector);
159 drm_mode_validate_size(dev, &connector->modes, maxX,
162 if (connector->interlace_allowed)
163 mode_flags |= DRM_MODE_FLAG_INTERLACE;
164 if (connector->doublescan_allowed)
165 mode_flags |= DRM_MODE_FLAG_DBLSCAN;
166 drm_mode_validate_flag(connector, mode_flags);
168 list_for_each_entry(mode, &connector->modes, head) {
169 if (mode->status == MODE_OK)
170 mode->status = connector_funcs->mode_valid(connector,
175 drm_mode_prune_invalid(dev, &connector->modes, true);
177 if (list_empty(&connector->modes))
180 drm_mode_sort(&connector->modes);
182 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] probed modes :\n", connector->base.id,
183 drm_get_connector_name(connector));
184 list_for_each_entry(mode, &connector->modes, head) {
185 mode->vrefresh = drm_mode_vrefresh(mode);
187 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
188 drm_mode_debug_printmodeline(mode);
193 EXPORT_SYMBOL(drm_helper_probe_single_connector_modes);
196 * drm_helper_encoder_in_use - check if a given encoder is in use
197 * @encoder: encoder to check
200 * Caller must hold mode config lock.
202 * Walk @encoders's DRM device's mode_config and see if it's in use.
205 * True if @encoder is part of the mode_config, false otherwise.
207 bool drm_helper_encoder_in_use(struct drm_encoder *encoder)
209 struct drm_connector *connector;
210 struct drm_device *dev = encoder->dev;
211 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
212 if (connector->encoder == encoder)
216 EXPORT_SYMBOL(drm_helper_encoder_in_use);
219 * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config
220 * @crtc: CRTC to check
223 * Caller must hold mode config lock.
225 * Walk @crtc's DRM device's mode_config and see if it's in use.
228 * True if @crtc is part of the mode_config, false otherwise.
230 bool drm_helper_crtc_in_use(struct drm_crtc *crtc)
232 struct drm_encoder *encoder;
233 struct drm_device *dev = crtc->dev;
234 /* FIXME: Locking around list access? */
235 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
236 if (encoder->crtc == crtc && drm_helper_encoder_in_use(encoder))
240 EXPORT_SYMBOL(drm_helper_crtc_in_use);
243 drm_encoder_disable(struct drm_encoder *encoder)
245 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
247 if (encoder_funcs->disable)
248 (*encoder_funcs->disable)(encoder);
250 (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
254 * drm_helper_disable_unused_functions - disable unused objects
258 * Caller must hold mode config lock.
260 * If an connector or CRTC isn't part of @dev's mode_config, it can be disabled
261 * by calling its dpms function, which should power it off.
263 void drm_helper_disable_unused_functions(struct drm_device *dev)
265 struct drm_encoder *encoder;
266 struct drm_connector *connector;
267 struct drm_crtc *crtc;
269 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
270 if (!connector->encoder)
272 if (connector->status == connector_status_disconnected)
273 connector->encoder = NULL;
276 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
277 if (!drm_helper_encoder_in_use(encoder)) {
278 drm_encoder_disable(encoder);
279 /* disconnector encoder from any connector */
280 encoder->crtc = NULL;
284 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
285 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
286 crtc->enabled = drm_helper_crtc_in_use(crtc);
287 if (!crtc->enabled) {
288 if (crtc_funcs->disable)
289 (*crtc_funcs->disable)(crtc);
291 (*crtc_funcs->dpms)(crtc, DRM_MODE_DPMS_OFF);
296 EXPORT_SYMBOL(drm_helper_disable_unused_functions);
299 * drm_encoder_crtc_ok - can a given crtc drive a given encoder?
300 * @encoder: encoder to test
301 * @crtc: crtc to test
303 * Return false if @encoder can't be driven by @crtc, true otherwise.
305 static bool drm_encoder_crtc_ok(struct drm_encoder *encoder,
306 struct drm_crtc *crtc)
308 struct drm_device *dev;
309 struct drm_crtc *tmp;
312 WARN(!crtc, "checking null crtc?\n");
316 list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
322 if (encoder->possible_crtcs & crtc_mask)
328 * Check the CRTC we're going to map each output to vs. its current
329 * CRTC. If they don't match, we have to disable the output and the CRTC
330 * since the driver will have to re-route things.
333 drm_crtc_prepare_encoders(struct drm_device *dev)
335 struct drm_encoder_helper_funcs *encoder_funcs;
336 struct drm_encoder *encoder;
338 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
339 encoder_funcs = encoder->helper_private;
340 /* Disable unused encoders */
341 if (encoder->crtc == NULL)
342 drm_encoder_disable(encoder);
343 /* Disable encoders whose CRTC is about to change */
344 if (encoder_funcs->get_crtc &&
345 encoder->crtc != (*encoder_funcs->get_crtc)(encoder))
346 drm_encoder_disable(encoder);
351 * drm_crtc_set_mode - set a mode
352 * @crtc: CRTC to program
354 * @x: horizontal offset into the surface
355 * @y: vertical offset into the surface
358 * Caller must hold mode config lock.
360 * Try to set @mode on @crtc. Give @crtc and its associated connectors a chance
361 * to fixup or reject the mode prior to trying to set it.
364 * True if the mode was set successfully, or false otherwise.
366 bool drm_crtc_helper_set_mode(struct drm_crtc *crtc,
367 struct drm_display_mode *mode,
369 struct drm_framebuffer *old_fb)
371 struct drm_device *dev = crtc->dev;
372 struct drm_display_mode *adjusted_mode, saved_mode, saved_hwmode;
373 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
374 struct drm_encoder_helper_funcs *encoder_funcs;
375 int saved_x, saved_y;
376 struct drm_encoder *encoder;
379 crtc->enabled = drm_helper_crtc_in_use(crtc);
383 adjusted_mode = drm_mode_duplicate(dev, mode);
387 saved_hwmode = crtc->hwmode;
388 saved_mode = crtc->mode;
392 /* Update crtc values up front so the driver can rely on them for mode
399 /* Pass our mode to the connectors and the CRTC to give them a chance to
400 * adjust it according to limitations or connector properties, and also
401 * a chance to reject the mode entirely.
403 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
405 if (encoder->crtc != crtc)
407 encoder_funcs = encoder->helper_private;
408 if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
410 DRM_DEBUG_KMS("Encoder fixup failed\n");
415 if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) {
416 DRM_DEBUG_KMS("CRTC fixup failed\n");
419 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
421 /* Prepare the encoders and CRTCs before setting the mode. */
422 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
424 if (encoder->crtc != crtc)
426 encoder_funcs = encoder->helper_private;
427 /* Disable the encoders as the first thing we do. */
428 encoder_funcs->prepare(encoder);
431 drm_crtc_prepare_encoders(dev);
433 crtc_funcs->prepare(crtc);
435 /* Set up the DPLL and any encoders state that needs to adjust or depend
438 ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
442 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
444 if (encoder->crtc != crtc)
447 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
448 encoder->base.id, drm_get_encoder_name(encoder),
449 mode->base.id, mode->name);
450 encoder_funcs = encoder->helper_private;
451 encoder_funcs->mode_set(encoder, mode, adjusted_mode);
454 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
455 crtc_funcs->commit(crtc);
457 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
459 if (encoder->crtc != crtc)
462 encoder_funcs = encoder->helper_private;
463 encoder_funcs->commit(encoder);
467 /* Store real post-adjustment hardware mode. */
468 crtc->hwmode = *adjusted_mode;
470 /* Calculate and store various constants which
471 * are later needed by vblank and swap-completion
472 * timestamping. They are derived from true hwmode.
474 drm_calc_timestamping_constants(crtc);
476 /* FIXME: add subpixel order */
478 drm_mode_destroy(dev, adjusted_mode);
480 crtc->hwmode = saved_hwmode;
481 crtc->mode = saved_mode;
488 EXPORT_SYMBOL(drm_crtc_helper_set_mode);
492 drm_crtc_helper_disable(struct drm_crtc *crtc)
494 struct drm_device *dev = crtc->dev;
495 struct drm_connector *connector;
496 struct drm_encoder *encoder;
498 /* Decouple all encoders and their attached connectors from this crtc */
499 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
500 if (encoder->crtc != crtc)
503 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
504 if (connector->encoder != encoder)
507 connector->encoder = NULL;
511 drm_helper_disable_unused_functions(dev);
516 * drm_crtc_helper_set_config - set a new config from userspace
517 * @crtc: CRTC to setup
518 * @crtc_info: user provided configuration
519 * @new_mode: new mode to set
520 * @connector_set: set of connectors for the new config
521 * @fb: new framebuffer
524 * Caller must hold mode config lock.
526 * Setup a new configuration, provided by the user in @crtc_info, and enable
532 int drm_crtc_helper_set_config(struct drm_mode_set *set)
534 struct drm_device *dev;
535 struct drm_crtc *save_crtcs, *new_crtc, *crtc;
536 struct drm_encoder *save_encoders, *new_encoder, *encoder;
537 struct drm_framebuffer *old_fb = NULL;
538 bool mode_changed = false; /* if true do a full mode set */
539 bool fb_changed = false; /* if true and !mode_changed just do a flip */
540 struct drm_connector *save_connectors, *connector;
541 int count = 0, ro, fail = 0;
542 struct drm_crtc_helper_funcs *crtc_funcs;
543 struct drm_mode_set save_set;
555 if (!set->crtc->helper_private)
558 crtc_funcs = set->crtc->helper_private;
564 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
565 set->crtc->base.id, set->fb->base.id,
566 (int)set->num_connectors, set->x, set->y);
568 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
569 return drm_crtc_helper_disable(set->crtc);
572 dev = set->crtc->dev;
574 /* Allocate space for the backup of all (non-pointer) crtc, encoder and
576 save_crtcs = kzalloc(dev->mode_config.num_crtc *
577 sizeof(struct drm_crtc), GFP_KERNEL);
581 save_encoders = kzalloc(dev->mode_config.num_encoder *
582 sizeof(struct drm_encoder), GFP_KERNEL);
583 if (!save_encoders) {
588 save_connectors = kzalloc(dev->mode_config.num_connector *
589 sizeof(struct drm_connector), GFP_KERNEL);
590 if (!save_connectors) {
592 kfree(save_encoders);
596 /* Copy data. Note that driver private data is not affected.
597 * Should anything bad happen only the expected state is
598 * restored, not the drivers personal bookkeeping.
601 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
602 save_crtcs[count++] = *crtc;
606 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
607 save_encoders[count++] = *encoder;
611 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
612 save_connectors[count++] = *connector;
615 save_set.crtc = set->crtc;
616 save_set.mode = &set->crtc->mode;
617 save_set.x = set->crtc->x;
618 save_set.y = set->crtc->y;
619 save_set.fb = set->crtc->fb;
621 /* We should be able to check here if the fb has the same properties
622 * and then just flip_or_move it */
623 if (set->crtc->fb != set->fb) {
624 /* If we have no fb then treat it as a full mode set */
625 if (set->crtc->fb == NULL) {
626 DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
628 } else if (set->fb == NULL) {
630 } else if (set->fb->depth != set->crtc->fb->depth) {
632 } else if (set->fb->bits_per_pixel !=
633 set->crtc->fb->bits_per_pixel) {
639 if (set->x != set->crtc->x || set->y != set->crtc->y)
642 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
643 DRM_DEBUG_KMS("modes are different, full mode set\n");
644 drm_mode_debug_printmodeline(&set->crtc->mode);
645 drm_mode_debug_printmodeline(set->mode);
649 /* a) traverse passed in connector list and get encoders for them */
651 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
652 struct drm_connector_helper_funcs *connector_funcs =
653 connector->helper_private;
654 new_encoder = connector->encoder;
655 for (ro = 0; ro < set->num_connectors; ro++) {
656 if (set->connectors[ro] == connector) {
657 new_encoder = connector_funcs->best_encoder(connector);
658 /* if we can't get an encoder for a connector
659 we are setting now - then fail */
660 if (new_encoder == NULL)
661 /* don't break so fail path works correct */
667 if (new_encoder != connector->encoder) {
668 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
670 /* If the encoder is reused for another connector, then
671 * the appropriate crtc will be set later.
673 if (connector->encoder)
674 connector->encoder->crtc = NULL;
675 connector->encoder = new_encoder;
685 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
686 if (!connector->encoder)
689 if (connector->encoder->crtc == set->crtc)
692 new_crtc = connector->encoder->crtc;
694 for (ro = 0; ro < set->num_connectors; ro++) {
695 if (set->connectors[ro] == connector)
696 new_crtc = set->crtc;
699 /* Make sure the new CRTC will work with the encoder */
701 !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
705 if (new_crtc != connector->encoder->crtc) {
706 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
708 connector->encoder->crtc = new_crtc;
711 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
712 connector->base.id, drm_get_connector_name(connector),
715 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
716 connector->base.id, drm_get_connector_name(connector));
720 /* mode_set_base is not a required function */
721 if (fb_changed && !crtc_funcs->mode_set_base)
725 set->crtc->enabled = drm_helper_crtc_in_use(set->crtc);
726 if (set->crtc->enabled) {
727 DRM_DEBUG_KMS("attempting to set mode from"
729 drm_mode_debug_printmodeline(set->mode);
730 old_fb = set->crtc->fb;
731 set->crtc->fb = set->fb;
732 if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
735 DRM_ERROR("failed to set mode on [CRTC:%d]\n",
737 set->crtc->fb = old_fb;
741 DRM_DEBUG_KMS("Setting connector DPMS state to on\n");
742 for (i = 0; i < set->num_connectors; i++) {
743 DRM_DEBUG_KMS("\t[CONNECTOR:%d:%s] set DPMS on\n", set->connectors[i]->base.id,
744 drm_get_connector_name(set->connectors[i]));
745 set->connectors[i]->funcs->dpms(set->connectors[i], DRM_MODE_DPMS_ON);
748 drm_helper_disable_unused_functions(dev);
749 } else if (fb_changed) {
750 set->crtc->x = set->x;
751 set->crtc->y = set->y;
753 old_fb = set->crtc->fb;
754 if (set->crtc->fb != set->fb)
755 set->crtc->fb = set->fb;
756 ret = crtc_funcs->mode_set_base(set->crtc,
757 set->x, set->y, old_fb);
759 set->crtc->fb = old_fb;
764 kfree(save_connectors);
765 kfree(save_encoders);
770 /* Restore all previous data. */
772 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
773 *crtc = save_crtcs[count++];
777 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
778 *encoder = save_encoders[count++];
782 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
783 *connector = save_connectors[count++];
786 /* Try to restore the config */
788 !drm_crtc_helper_set_mode(save_set.crtc, save_set.mode, save_set.x,
789 save_set.y, save_set.fb))
790 DRM_ERROR("failed to restore config after modeset failure\n");
792 kfree(save_connectors);
793 kfree(save_encoders);
797 EXPORT_SYMBOL(drm_crtc_helper_set_config);
799 static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder)
801 int dpms = DRM_MODE_DPMS_OFF;
802 struct drm_connector *connector;
803 struct drm_device *dev = encoder->dev;
805 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
806 if (connector->encoder == encoder)
807 if (connector->dpms < dpms)
808 dpms = connector->dpms;
812 static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc)
814 int dpms = DRM_MODE_DPMS_OFF;
815 struct drm_connector *connector;
816 struct drm_device *dev = crtc->dev;
818 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
819 if (connector->encoder && connector->encoder->crtc == crtc)
820 if (connector->dpms < dpms)
821 dpms = connector->dpms;
826 * drm_helper_connector_dpms
827 * @connector affected connector
830 * Calls the low-level connector DPMS function, then
831 * calls appropriate encoder and crtc DPMS functions as well
833 void drm_helper_connector_dpms(struct drm_connector *connector, int mode)
835 struct drm_encoder *encoder = connector->encoder;
836 struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
839 if (mode == connector->dpms)
842 old_dpms = connector->dpms;
843 connector->dpms = mode;
845 /* from off to on, do crtc then encoder */
846 if (mode < old_dpms) {
848 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
849 if (crtc_funcs->dpms)
850 (*crtc_funcs->dpms) (crtc,
851 drm_helper_choose_crtc_dpms(crtc));
854 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
855 if (encoder_funcs->dpms)
856 (*encoder_funcs->dpms) (encoder,
857 drm_helper_choose_encoder_dpms(encoder));
861 /* from on to off, do encoder then crtc */
862 if (mode > old_dpms) {
864 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
865 if (encoder_funcs->dpms)
866 (*encoder_funcs->dpms) (encoder,
867 drm_helper_choose_encoder_dpms(encoder));
870 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
871 if (crtc_funcs->dpms)
872 (*crtc_funcs->dpms) (crtc,
873 drm_helper_choose_crtc_dpms(crtc));
879 EXPORT_SYMBOL(drm_helper_connector_dpms);
881 int drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb,
882 struct drm_mode_fb_cmd2 *mode_cmd)
886 fb->width = mode_cmd->width;
887 fb->height = mode_cmd->height;
888 for (i = 0; i < 4; i++) {
889 fb->pitches[i] = mode_cmd->pitches[i];
890 fb->offsets[i] = mode_cmd->offsets[i];
892 drm_fb_get_bpp_depth(mode_cmd->pixel_format, &fb->depth,
893 &fb->bits_per_pixel);
894 fb->pixel_format = mode_cmd->pixel_format;
898 EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
900 int drm_helper_resume_force_mode(struct drm_device *dev)
902 struct drm_crtc *crtc;
903 struct drm_encoder *encoder;
904 struct drm_encoder_helper_funcs *encoder_funcs;
905 struct drm_crtc_helper_funcs *crtc_funcs;
908 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
913 ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
914 crtc->x, crtc->y, crtc->fb);
917 DRM_ERROR("failed to set mode on crtc %p\n", crtc);
919 /* Turn off outputs that were already powered off */
920 if (drm_helper_choose_crtc_dpms(crtc)) {
921 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
923 if(encoder->crtc != crtc)
926 encoder_funcs = encoder->helper_private;
927 if (encoder_funcs->dpms)
928 (*encoder_funcs->dpms) (encoder,
929 drm_helper_choose_encoder_dpms(encoder));
932 crtc_funcs = crtc->helper_private;
933 if (crtc_funcs->dpms)
934 (*crtc_funcs->dpms) (crtc,
935 drm_helper_choose_crtc_dpms(crtc));
938 /* disable the unused connectors while restoring the modesetting */
939 drm_helper_disable_unused_functions(dev);
942 EXPORT_SYMBOL(drm_helper_resume_force_mode);
944 void drm_kms_helper_hotplug_event(struct drm_device *dev)
946 /* send a uevent + call fbdev */
947 drm_sysfs_hotplug_event(dev);
948 if (dev->mode_config.funcs->output_poll_changed)
949 dev->mode_config.funcs->output_poll_changed(dev);
951 EXPORT_SYMBOL(drm_kms_helper_hotplug_event);
953 #define DRM_OUTPUT_POLL_PERIOD (10*HZ)
954 static void output_poll_execute(struct work_struct *work)
956 struct delayed_work *delayed_work = to_delayed_work(work);
957 struct drm_device *dev = container_of(delayed_work, struct drm_device, mode_config.output_poll_work);
958 struct drm_connector *connector;
959 enum drm_connector_status old_status;
960 bool repoll = false, changed = false;
962 if (!drm_kms_helper_poll)
965 mutex_lock(&dev->mode_config.mutex);
966 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
968 /* Ignore forced connectors. */
969 if (connector->force)
972 /* Ignore HPD capable connectors and connectors where we don't
973 * want any hotplug detection at all for polling. */
974 if (!connector->polled || connector->polled == DRM_CONNECTOR_POLL_HPD)
979 old_status = connector->status;
980 /* if we are connected and don't want to poll for disconnect
982 if (old_status == connector_status_connected &&
983 !(connector->polled & DRM_CONNECTOR_POLL_DISCONNECT))
986 connector->status = connector->funcs->detect(connector, false);
987 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %d to %d\n",
989 drm_get_connector_name(connector),
990 old_status, connector->status);
991 if (old_status != connector->status)
995 mutex_unlock(&dev->mode_config.mutex);
998 drm_kms_helper_hotplug_event(dev);
1001 schedule_delayed_work(delayed_work, DRM_OUTPUT_POLL_PERIOD);
1004 void drm_kms_helper_poll_disable(struct drm_device *dev)
1006 if (!dev->mode_config.poll_enabled)
1008 cancel_delayed_work_sync(&dev->mode_config.output_poll_work);
1010 EXPORT_SYMBOL(drm_kms_helper_poll_disable);
1012 void drm_kms_helper_poll_enable(struct drm_device *dev)
1015 struct drm_connector *connector;
1017 if (!dev->mode_config.poll_enabled || !drm_kms_helper_poll)
1020 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1021 if (connector->polled & (DRM_CONNECTOR_POLL_CONNECT |
1022 DRM_CONNECTOR_POLL_DISCONNECT))
1027 schedule_delayed_work(&dev->mode_config.output_poll_work, DRM_OUTPUT_POLL_PERIOD);
1029 EXPORT_SYMBOL(drm_kms_helper_poll_enable);
1031 void drm_kms_helper_poll_init(struct drm_device *dev)
1033 INIT_DELAYED_WORK(&dev->mode_config.output_poll_work, output_poll_execute);
1034 dev->mode_config.poll_enabled = true;
1036 drm_kms_helper_poll_enable(dev);
1038 EXPORT_SYMBOL(drm_kms_helper_poll_init);
1040 void drm_kms_helper_poll_fini(struct drm_device *dev)
1042 drm_kms_helper_poll_disable(dev);
1044 EXPORT_SYMBOL(drm_kms_helper_poll_fini);
1046 void drm_helper_hpd_irq_event(struct drm_device *dev)
1048 struct drm_connector *connector;
1049 enum drm_connector_status old_status;
1050 bool changed = false;
1052 if (!dev->mode_config.poll_enabled)
1055 mutex_lock(&dev->mode_config.mutex);
1056 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1058 /* Only handle HPD capable connectors. */
1059 if (!(connector->polled & DRM_CONNECTOR_POLL_HPD))
1062 old_status = connector->status;
1064 connector->status = connector->funcs->detect(connector, false);
1065 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %d to %d\n",
1067 drm_get_connector_name(connector),
1068 old_status, connector->status);
1069 if (old_status != connector->status)
1073 mutex_unlock(&dev->mode_config.mutex);
1076 drm_kms_helper_hotplug_event(dev);
1078 EXPORT_SYMBOL(drm_helper_hpd_irq_event);