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/kernel.h>
33 #include <linux/export.h>
34 #include <linux/moduleparam.h>
37 #include <drm/drm_crtc.h>
38 #include <drm/drm_fourcc.h>
39 #include <drm/drm_crtc_helper.h>
40 #include <drm/drm_fb_helper.h>
41 #include <drm/drm_plane_helper.h>
42 #include <drm/drm_edid.h>
47 * The CRTC modeset helper library provides a default set_config implementation
48 * in drm_crtc_helper_set_config(). Plus a few other convenience functions using
49 * the same callbacks which drivers can use to e.g. restore the modeset
50 * configuration on resume with drm_helper_resume_force_mode().
52 * The driver callbacks are mostly compatible with the atomic modeset helpers,
53 * except for the handling of the primary plane: Atomic helpers require that the
54 * primary plane is implemented as a real standalone plane and not directly tied
55 * to the CRTC state. For easier transition this library provides functions to
56 * implement the old semantics required by the CRTC helpers using the new plane
57 * and atomic helper callbacks.
59 * Drivers are strongly urged to convert to the atomic helpers (by way of first
60 * converting to the plane helpers). New drivers must not use these functions
61 * but need to implement the atomic interface instead, potentially using the
62 * atomic helpers for that.
64 MODULE_AUTHOR("David Airlie, Jesse Barnes");
65 MODULE_DESCRIPTION("DRM KMS helper");
66 MODULE_LICENSE("GPL and additional rights");
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);
98 * drm_helper_encoder_in_use - check if a given encoder is in use
99 * @encoder: encoder to check
101 * Checks whether @encoder is with the current mode setting output configuration
102 * in use by any connector. This doesn't mean that it is actually enabled since
103 * the DPMS state is tracked separately.
106 * True if @encoder is used, false otherwise.
108 bool drm_helper_encoder_in_use(struct drm_encoder *encoder)
110 struct drm_connector *connector;
111 struct drm_device *dev = encoder->dev;
114 * We can expect this mutex to be locked if we are not panicking.
115 * Locking is currently fubar in the panic handler.
117 if (!oops_in_progress) {
118 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
119 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
122 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
123 if (connector->encoder == encoder)
127 EXPORT_SYMBOL(drm_helper_encoder_in_use);
130 * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config
131 * @crtc: CRTC to check
133 * Checks whether @crtc is with the current mode setting output configuration
134 * in use by any connector. This doesn't mean that it is actually enabled since
135 * the DPMS state is tracked separately.
138 * True if @crtc is used, false otherwise.
140 bool drm_helper_crtc_in_use(struct drm_crtc *crtc)
142 struct drm_encoder *encoder;
143 struct drm_device *dev = crtc->dev;
146 * We can expect this mutex to be locked if we are not panicking.
147 * Locking is currently fubar in the panic handler.
149 if (!oops_in_progress)
150 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
152 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
153 if (encoder->crtc == crtc && drm_helper_encoder_in_use(encoder))
157 EXPORT_SYMBOL(drm_helper_crtc_in_use);
160 drm_encoder_disable(struct drm_encoder *encoder)
162 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
165 encoder->bridge->funcs->disable(encoder->bridge);
167 if (encoder_funcs->disable)
168 (*encoder_funcs->disable)(encoder);
170 (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
173 encoder->bridge->funcs->post_disable(encoder->bridge);
176 static void __drm_helper_disable_unused_functions(struct drm_device *dev)
178 struct drm_encoder *encoder;
179 struct drm_crtc *crtc;
181 drm_warn_on_modeset_not_all_locked(dev);
183 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
184 if (!drm_helper_encoder_in_use(encoder)) {
185 drm_encoder_disable(encoder);
186 /* disconnect encoder from any connector */
187 encoder->crtc = NULL;
191 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
192 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
193 crtc->enabled = drm_helper_crtc_in_use(crtc);
194 if (!crtc->enabled) {
195 if (crtc_funcs->disable)
196 (*crtc_funcs->disable)(crtc);
198 (*crtc_funcs->dpms)(crtc, DRM_MODE_DPMS_OFF);
199 crtc->primary->fb = NULL;
205 * drm_helper_disable_unused_functions - disable unused objects
208 * This function walks through the entire mode setting configuration of @dev. It
209 * will remove any crtc links of unused encoders and encoder links of
210 * disconnected connectors. Then it will disable all unused encoders and crtcs
211 * either by calling their disable callback if available or by calling their
212 * dpms callback with DRM_MODE_DPMS_OFF.
214 void drm_helper_disable_unused_functions(struct drm_device *dev)
216 drm_modeset_lock_all(dev);
217 __drm_helper_disable_unused_functions(dev);
218 drm_modeset_unlock_all(dev);
220 EXPORT_SYMBOL(drm_helper_disable_unused_functions);
223 * Check the CRTC we're going to map each output to vs. its current
224 * CRTC. If they don't match, we have to disable the output and the CRTC
225 * since the driver will have to re-route things.
228 drm_crtc_prepare_encoders(struct drm_device *dev)
230 struct drm_encoder_helper_funcs *encoder_funcs;
231 struct drm_encoder *encoder;
233 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
234 encoder_funcs = encoder->helper_private;
235 /* Disable unused encoders */
236 if (encoder->crtc == NULL)
237 drm_encoder_disable(encoder);
238 /* Disable encoders whose CRTC is about to change */
239 if (encoder_funcs->get_crtc &&
240 encoder->crtc != (*encoder_funcs->get_crtc)(encoder))
241 drm_encoder_disable(encoder);
246 * drm_crtc_helper_set_mode - internal helper to set a mode
247 * @crtc: CRTC to program
249 * @x: horizontal offset into the surface
250 * @y: vertical offset into the surface
251 * @old_fb: old framebuffer, for cleanup
253 * Try to set @mode on @crtc. Give @crtc and its associated connectors a chance
254 * to fixup or reject the mode prior to trying to set it. This is an internal
255 * helper that drivers could e.g. use to update properties that require the
256 * entire output pipe to be disabled and re-enabled in a new configuration. For
257 * example for changing whether audio is enabled on a hdmi link or for changing
258 * panel fitter or dither attributes. It is also called by the
259 * drm_crtc_helper_set_config() helper function to drive the mode setting
263 * True if the mode was set successfully, false otherwise.
265 bool drm_crtc_helper_set_mode(struct drm_crtc *crtc,
266 struct drm_display_mode *mode,
268 struct drm_framebuffer *old_fb)
270 struct drm_device *dev = crtc->dev;
271 struct drm_display_mode *adjusted_mode, saved_mode;
272 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
273 struct drm_encoder_helper_funcs *encoder_funcs;
274 int saved_x, saved_y;
276 struct drm_encoder *encoder;
279 drm_warn_on_modeset_not_all_locked(dev);
281 saved_enabled = crtc->enabled;
282 crtc->enabled = drm_helper_crtc_in_use(crtc);
286 adjusted_mode = drm_mode_duplicate(dev, mode);
287 if (!adjusted_mode) {
288 crtc->enabled = saved_enabled;
292 saved_mode = crtc->mode;
296 /* Update crtc values up front so the driver can rely on them for mode
303 /* Pass our mode to the connectors and the CRTC to give them a chance to
304 * adjust it according to limitations or connector properties, and also
305 * a chance to reject the mode entirely.
307 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
309 if (encoder->crtc != crtc)
312 if (encoder->bridge && encoder->bridge->funcs->mode_fixup) {
313 ret = encoder->bridge->funcs->mode_fixup(
314 encoder->bridge, mode, adjusted_mode);
316 DRM_DEBUG_KMS("Bridge fixup failed\n");
321 encoder_funcs = encoder->helper_private;
322 if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
324 DRM_DEBUG_KMS("Encoder fixup failed\n");
329 if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) {
330 DRM_DEBUG_KMS("CRTC fixup failed\n");
333 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
335 /* Prepare the encoders and CRTCs before setting the mode. */
336 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
338 if (encoder->crtc != crtc)
342 encoder->bridge->funcs->disable(encoder->bridge);
344 encoder_funcs = encoder->helper_private;
345 /* Disable the encoders as the first thing we do. */
346 encoder_funcs->prepare(encoder);
349 encoder->bridge->funcs->post_disable(encoder->bridge);
352 drm_crtc_prepare_encoders(dev);
354 crtc_funcs->prepare(crtc);
356 /* Set up the DPLL and any encoders state that needs to adjust or depend
359 ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
363 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
365 if (encoder->crtc != crtc)
368 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
369 encoder->base.id, encoder->name,
370 mode->base.id, mode->name);
371 encoder_funcs = encoder->helper_private;
372 encoder_funcs->mode_set(encoder, mode, adjusted_mode);
374 if (encoder->bridge && encoder->bridge->funcs->mode_set)
375 encoder->bridge->funcs->mode_set(encoder->bridge, mode,
379 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
380 crtc_funcs->commit(crtc);
382 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
384 if (encoder->crtc != crtc)
388 encoder->bridge->funcs->pre_enable(encoder->bridge);
390 encoder_funcs = encoder->helper_private;
391 encoder_funcs->commit(encoder);
394 encoder->bridge->funcs->enable(encoder->bridge);
397 /* Store real post-adjustment hardware mode. */
398 crtc->hwmode = *adjusted_mode;
400 /* Calculate and store various constants which
401 * are later needed by vblank and swap-completion
402 * timestamping. They are derived from true hwmode.
404 drm_calc_timestamping_constants(crtc, &crtc->hwmode);
406 /* FIXME: add subpixel order */
408 drm_mode_destroy(dev, adjusted_mode);
410 crtc->enabled = saved_enabled;
411 crtc->mode = saved_mode;
418 EXPORT_SYMBOL(drm_crtc_helper_set_mode);
421 drm_crtc_helper_disable(struct drm_crtc *crtc)
423 struct drm_device *dev = crtc->dev;
424 struct drm_connector *connector;
425 struct drm_encoder *encoder;
427 /* Decouple all encoders and their attached connectors from this crtc */
428 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
429 if (encoder->crtc != crtc)
432 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
433 if (connector->encoder != encoder)
436 connector->encoder = NULL;
439 * drm_helper_disable_unused_functions() ought to be
440 * doing this, but since we've decoupled the encoder
441 * from the connector above, the required connection
442 * between them is henceforth no longer available.
444 connector->dpms = DRM_MODE_DPMS_OFF;
448 __drm_helper_disable_unused_functions(dev);
452 * drm_crtc_helper_set_config - set a new config from userspace
453 * @set: mode set configuration
455 * Setup a new configuration, provided by the upper layers (either an ioctl call
456 * from userspace or internally e.g. from the fbdev support code) in @set, and
457 * enable it. This is the main helper functions for drivers that implement
458 * kernel mode setting with the crtc helper functions and the assorted
459 * ->prepare(), ->modeset() and ->commit() helper callbacks.
462 * Returns 0 on success, negative errno numbers on failure.
464 int drm_crtc_helper_set_config(struct drm_mode_set *set)
466 struct drm_device *dev;
467 struct drm_crtc *new_crtc;
468 struct drm_encoder *save_encoders, *new_encoder, *encoder;
469 bool mode_changed = false; /* if true do a full mode set */
470 bool fb_changed = false; /* if true and !mode_changed just do a flip */
471 struct drm_connector *save_connectors, *connector;
472 int count = 0, ro, fail = 0;
473 struct drm_crtc_helper_funcs *crtc_funcs;
474 struct drm_mode_set save_set;
482 BUG_ON(!set->crtc->helper_private);
484 /* Enforce sane interface api - has been abused by the fb helper. */
485 BUG_ON(!set->mode && set->fb);
486 BUG_ON(set->fb && set->num_connectors == 0);
488 crtc_funcs = set->crtc->helper_private;
494 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
495 set->crtc->base.id, set->fb->base.id,
496 (int)set->num_connectors, set->x, set->y);
498 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
499 drm_crtc_helper_disable(set->crtc);
503 dev = set->crtc->dev;
505 drm_warn_on_modeset_not_all_locked(dev);
508 * Allocate space for the backup of all (non-pointer) encoder and
511 save_encoders = kzalloc(dev->mode_config.num_encoder *
512 sizeof(struct drm_encoder), GFP_KERNEL);
516 save_connectors = kzalloc(dev->mode_config.num_connector *
517 sizeof(struct drm_connector), GFP_KERNEL);
518 if (!save_connectors) {
519 kfree(save_encoders);
524 * Copy data. Note that driver private data is not affected.
525 * Should anything bad happen only the expected state is
526 * restored, not the drivers personal bookkeeping.
529 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
530 save_encoders[count++] = *encoder;
534 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
535 save_connectors[count++] = *connector;
538 save_set.crtc = set->crtc;
539 save_set.mode = &set->crtc->mode;
540 save_set.x = set->crtc->x;
541 save_set.y = set->crtc->y;
542 save_set.fb = set->crtc->primary->fb;
544 /* We should be able to check here if the fb has the same properties
545 * and then just flip_or_move it */
546 if (set->crtc->primary->fb != set->fb) {
547 /* If we have no fb then treat it as a full mode set */
548 if (set->crtc->primary->fb == NULL) {
549 DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
551 } else if (set->fb == NULL) {
553 } else if (set->fb->pixel_format !=
554 set->crtc->primary->fb->pixel_format) {
560 if (set->x != set->crtc->x || set->y != set->crtc->y)
563 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
564 DRM_DEBUG_KMS("modes are different, full mode set\n");
565 drm_mode_debug_printmodeline(&set->crtc->mode);
566 drm_mode_debug_printmodeline(set->mode);
570 /* a) traverse passed in connector list and get encoders for them */
572 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
573 struct drm_connector_helper_funcs *connector_funcs =
574 connector->helper_private;
575 new_encoder = connector->encoder;
576 for (ro = 0; ro < set->num_connectors; ro++) {
577 if (set->connectors[ro] == connector) {
578 new_encoder = connector_funcs->best_encoder(connector);
579 /* if we can't get an encoder for a connector
580 we are setting now - then fail */
581 if (new_encoder == NULL)
582 /* don't break so fail path works correct */
585 if (connector->dpms != DRM_MODE_DPMS_ON) {
586 DRM_DEBUG_KMS("connector dpms not on, full mode switch\n");
594 if (new_encoder != connector->encoder) {
595 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
597 /* If the encoder is reused for another connector, then
598 * the appropriate crtc will be set later.
600 if (connector->encoder)
601 connector->encoder->crtc = NULL;
602 connector->encoder = new_encoder;
612 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
613 if (!connector->encoder)
616 if (connector->encoder->crtc == set->crtc)
619 new_crtc = connector->encoder->crtc;
621 for (ro = 0; ro < set->num_connectors; ro++) {
622 if (set->connectors[ro] == connector)
623 new_crtc = set->crtc;
626 /* Make sure the new CRTC will work with the encoder */
628 !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
632 if (new_crtc != connector->encoder->crtc) {
633 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
635 connector->encoder->crtc = new_crtc;
638 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
639 connector->base.id, connector->name,
642 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
643 connector->base.id, connector->name);
647 /* mode_set_base is not a required function */
648 if (fb_changed && !crtc_funcs->mode_set_base)
652 if (drm_helper_crtc_in_use(set->crtc)) {
653 DRM_DEBUG_KMS("attempting to set mode from"
655 drm_mode_debug_printmodeline(set->mode);
656 set->crtc->primary->fb = set->fb;
657 if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
660 DRM_ERROR("failed to set mode on [CRTC:%d]\n",
662 set->crtc->primary->fb = save_set.fb;
666 DRM_DEBUG_KMS("Setting connector DPMS state to on\n");
667 for (i = 0; i < set->num_connectors; i++) {
668 DRM_DEBUG_KMS("\t[CONNECTOR:%d:%s] set DPMS on\n", set->connectors[i]->base.id,
669 set->connectors[i]->name);
670 set->connectors[i]->funcs->dpms(set->connectors[i], DRM_MODE_DPMS_ON);
673 __drm_helper_disable_unused_functions(dev);
674 } else if (fb_changed) {
675 set->crtc->x = set->x;
676 set->crtc->y = set->y;
677 set->crtc->primary->fb = set->fb;
678 ret = crtc_funcs->mode_set_base(set->crtc,
679 set->x, set->y, save_set.fb);
681 set->crtc->x = save_set.x;
682 set->crtc->y = save_set.y;
683 set->crtc->primary->fb = save_set.fb;
688 kfree(save_connectors);
689 kfree(save_encoders);
693 /* Restore all previous data. */
695 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
696 *encoder = save_encoders[count++];
700 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
701 *connector = save_connectors[count++];
704 /* Try to restore the config */
706 !drm_crtc_helper_set_mode(save_set.crtc, save_set.mode, save_set.x,
707 save_set.y, save_set.fb))
708 DRM_ERROR("failed to restore config after modeset failure\n");
710 kfree(save_connectors);
711 kfree(save_encoders);
714 EXPORT_SYMBOL(drm_crtc_helper_set_config);
716 static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder)
718 int dpms = DRM_MODE_DPMS_OFF;
719 struct drm_connector *connector;
720 struct drm_device *dev = encoder->dev;
722 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
723 if (connector->encoder == encoder)
724 if (connector->dpms < dpms)
725 dpms = connector->dpms;
729 /* Helper which handles bridge ordering around encoder dpms */
730 static void drm_helper_encoder_dpms(struct drm_encoder *encoder, int mode)
732 struct drm_bridge *bridge = encoder->bridge;
733 struct drm_encoder_helper_funcs *encoder_funcs;
736 if (mode == DRM_MODE_DPMS_ON)
737 bridge->funcs->pre_enable(bridge);
739 bridge->funcs->disable(bridge);
742 encoder_funcs = encoder->helper_private;
743 if (encoder_funcs->dpms)
744 encoder_funcs->dpms(encoder, mode);
747 if (mode == DRM_MODE_DPMS_ON)
748 bridge->funcs->enable(bridge);
750 bridge->funcs->post_disable(bridge);
754 static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc)
756 int dpms = DRM_MODE_DPMS_OFF;
757 struct drm_connector *connector;
758 struct drm_device *dev = crtc->dev;
760 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
761 if (connector->encoder && connector->encoder->crtc == crtc)
762 if (connector->dpms < dpms)
763 dpms = connector->dpms;
768 * drm_helper_connector_dpms() - connector dpms helper implementation
769 * @connector: affected connector
772 * This is the main helper function provided by the crtc helper framework for
773 * implementing the DPMS connector attribute. It computes the new desired DPMS
774 * state for all encoders and crtcs in the output mesh and calls the ->dpms()
775 * callback provided by the driver appropriately.
777 void drm_helper_connector_dpms(struct drm_connector *connector, int mode)
779 struct drm_encoder *encoder = connector->encoder;
780 struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
781 int old_dpms, encoder_dpms = DRM_MODE_DPMS_OFF;
783 if (mode == connector->dpms)
786 old_dpms = connector->dpms;
787 connector->dpms = mode;
790 encoder_dpms = drm_helper_choose_encoder_dpms(encoder);
792 /* from off to on, do crtc then encoder */
793 if (mode < old_dpms) {
795 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
796 if (crtc_funcs->dpms)
797 (*crtc_funcs->dpms) (crtc,
798 drm_helper_choose_crtc_dpms(crtc));
801 drm_helper_encoder_dpms(encoder, encoder_dpms);
804 /* from on to off, do encoder then crtc */
805 if (mode > old_dpms) {
807 drm_helper_encoder_dpms(encoder, encoder_dpms);
809 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
810 if (crtc_funcs->dpms)
811 (*crtc_funcs->dpms) (crtc,
812 drm_helper_choose_crtc_dpms(crtc));
818 EXPORT_SYMBOL(drm_helper_connector_dpms);
821 * drm_helper_mode_fill_fb_struct - fill out framebuffer metadata
822 * @fb: drm_framebuffer object to fill out
823 * @mode_cmd: metadata from the userspace fb creation request
825 * This helper can be used in a drivers fb_create callback to pre-fill the fb's
828 void drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb,
829 struct drm_mode_fb_cmd2 *mode_cmd)
833 fb->width = mode_cmd->width;
834 fb->height = mode_cmd->height;
835 for (i = 0; i < 4; i++) {
836 fb->pitches[i] = mode_cmd->pitches[i];
837 fb->offsets[i] = mode_cmd->offsets[i];
839 drm_fb_get_bpp_depth(mode_cmd->pixel_format, &fb->depth,
840 &fb->bits_per_pixel);
841 fb->pixel_format = mode_cmd->pixel_format;
842 fb->flags = mode_cmd->flags;
844 EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
847 * drm_helper_resume_force_mode - force-restore mode setting configuration
848 * @dev: drm_device which should be restored
850 * Drivers which use the mode setting helpers can use this function to
851 * force-restore the mode setting configuration e.g. on resume or when something
852 * else might have trampled over the hw state (like some overzealous old BIOSen
855 * This helper doesn't provide a error return value since restoring the old
856 * config should never fail due to resource allocation issues since the driver
857 * has successfully set the restored configuration already. Hence this should
858 * boil down to the equivalent of a few dpms on calls, which also don't provide
861 * Drivers where simply restoring an old configuration again might fail (e.g.
862 * due to slight differences in allocating shared resources when the
863 * configuration is restored in a different order than when userspace set it up)
864 * need to use their own restore logic.
866 void drm_helper_resume_force_mode(struct drm_device *dev)
868 struct drm_crtc *crtc;
869 struct drm_encoder *encoder;
870 struct drm_crtc_helper_funcs *crtc_funcs;
874 drm_modeset_lock_all(dev);
875 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
880 ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
881 crtc->x, crtc->y, crtc->primary->fb);
883 /* Restoring the old config should never fail! */
885 DRM_ERROR("failed to set mode on crtc %p\n", crtc);
887 /* Turn off outputs that were already powered off */
888 if (drm_helper_choose_crtc_dpms(crtc)) {
889 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
891 if(encoder->crtc != crtc)
894 encoder_dpms = drm_helper_choose_encoder_dpms(
897 drm_helper_encoder_dpms(encoder, encoder_dpms);
900 crtc_funcs = crtc->helper_private;
901 if (crtc_funcs->dpms)
902 (*crtc_funcs->dpms) (crtc,
903 drm_helper_choose_crtc_dpms(crtc));
907 /* disable the unused connectors while restoring the modesetting */
908 __drm_helper_disable_unused_functions(dev);
909 drm_modeset_unlock_all(dev);
911 EXPORT_SYMBOL(drm_helper_resume_force_mode);
914 * drm_helper_crtc_mode_set - mode_set implementation for atomic plane helpers
916 * @mode: DRM display mode which userspace requested
917 * @adjusted_mode: DRM display mode adjusted by ->mode_fixup callbacks
918 * @x: x offset of the CRTC scanout area on the underlying framebuffer
919 * @y: y offset of the CRTC scanout area on the underlying framebuffer
920 * @old_fb: previous framebuffer
922 * This function implements a callback useable as the ->mode_set callback
923 * required by the crtc helpers. Besides the atomic plane helper functions for
924 * the primary plane the driver must also provide the ->mode_set_nofb callback
925 * to set up the crtc.
927 * This is a transitional helper useful for converting drivers to the atomic
930 int drm_helper_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode,
931 struct drm_display_mode *adjusted_mode, int x, int y,
932 struct drm_framebuffer *old_fb)
934 struct drm_crtc_state *crtc_state;
935 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
938 if (crtc->funcs->atomic_duplicate_state)
939 crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
940 else if (crtc->state)
941 crtc_state = kmemdup(crtc->state, sizeof(*crtc_state),
944 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
948 crtc_state->enable = true;
949 crtc_state->planes_changed = true;
950 crtc_state->mode_changed = true;
951 drm_mode_copy(&crtc_state->mode, mode);
952 drm_mode_copy(&crtc_state->adjusted_mode, adjusted_mode);
954 if (crtc_funcs->atomic_check) {
955 ret = crtc_funcs->atomic_check(crtc, crtc_state);
963 swap(crtc->state, crtc_state);
965 crtc_funcs->mode_set_nofb(crtc);
968 if (crtc->funcs->atomic_destroy_state)
969 crtc->funcs->atomic_destroy_state(crtc, crtc_state);
974 return drm_helper_crtc_mode_set_base(crtc, x, y, old_fb);
976 EXPORT_SYMBOL(drm_helper_crtc_mode_set);
979 * drm_helper_crtc_mode_set_base - mode_set_base implementation for atomic plane helpers
981 * @x: x offset of the CRTC scanout area on the underlying framebuffer
982 * @y: y offset of the CRTC scanout area on the underlying framebuffer
983 * @old_fb: previous framebuffer
985 * This function implements a callback useable as the ->mode_set_base used
986 * required by the crtc helpers. The driver must provide the atomic plane helper
987 * functions for the primary plane.
989 * This is a transitional helper useful for converting drivers to the atomic
992 int drm_helper_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
993 struct drm_framebuffer *old_fb)
995 struct drm_plane_state *plane_state;
996 struct drm_plane *plane = crtc->primary;
998 if (plane->funcs->atomic_duplicate_state)
999 plane_state = plane->funcs->atomic_duplicate_state(plane);
1000 else if (plane->state)
1001 plane_state = kmemdup(plane->state, sizeof(*plane_state),
1004 plane_state = kzalloc(sizeof(*plane_state), GFP_KERNEL);
1008 plane_state->crtc = crtc;
1009 plane_state->fb = crtc->primary->fb;
1010 plane_state->crtc_x = 0;
1011 plane_state->crtc_y = 0;
1012 plane_state->crtc_h = crtc->mode.vdisplay;
1013 plane_state->crtc_w = crtc->mode.hdisplay;
1014 plane_state->src_x = x << 16;
1015 plane_state->src_y = y << 16;
1016 plane_state->src_h = crtc->mode.vdisplay << 16;
1017 plane_state->src_w = crtc->mode.hdisplay << 16;
1019 return drm_plane_helper_commit(plane, plane_state, old_fb);
1021 EXPORT_SYMBOL(drm_helper_crtc_mode_set_base);