treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / gpu / drm / drm_crtc_helper.c
blob93a4eec429e8cfda0c15ec4a9ccca29409f53389
1 /*
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
23 * OF THIS SOFTWARE.
25 * Authors:
26 * Keith Packard
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/kernel.h>
34 #include <linux/moduleparam.h>
36 #include <drm/drm_atomic.h>
37 #include <drm/drm_atomic_helper.h>
38 #include <drm/drm_atomic_uapi.h>
39 #include <drm/drm_bridge.h>
40 #include <drm/drm_crtc.h>
41 #include <drm/drm_crtc_helper.h>
42 #include <drm/drm_drv.h>
43 #include <drm/drm_edid.h>
44 #include <drm/drm_encoder.h>
45 #include <drm/drm_fb_helper.h>
46 #include <drm/drm_fourcc.h>
47 #include <drm/drm_plane_helper.h>
48 #include <drm/drm_print.h>
49 #include <drm/drm_vblank.h>
51 #include "drm_crtc_helper_internal.h"
53 /**
54 * DOC: overview
56 * The CRTC modeset helper library provides a default set_config implementation
57 * in drm_crtc_helper_set_config(). Plus a few other convenience functions using
58 * the same callbacks which drivers can use to e.g. restore the modeset
59 * configuration on resume with drm_helper_resume_force_mode().
61 * Note that this helper library doesn't track the current power state of CRTCs
62 * and encoders. It can call callbacks like &drm_encoder_helper_funcs.dpms even
63 * though the hardware is already in the desired state. This deficiency has been
64 * fixed in the atomic helpers.
66 * The driver callbacks are mostly compatible with the atomic modeset helpers,
67 * except for the handling of the primary plane: Atomic helpers require that the
68 * primary plane is implemented as a real standalone plane and not directly tied
69 * to the CRTC state. For easier transition this library provides functions to
70 * implement the old semantics required by the CRTC helpers using the new plane
71 * and atomic helper callbacks.
73 * Drivers are strongly urged to convert to the atomic helpers (by way of first
74 * converting to the plane helpers). New drivers must not use these functions
75 * but need to implement the atomic interface instead, potentially using the
76 * atomic helpers for that.
78 * These legacy modeset helpers use the same function table structures as
79 * all other modesetting helpers. See the documentation for struct
80 * &drm_crtc_helper_funcs, &struct drm_encoder_helper_funcs and struct
81 * &drm_connector_helper_funcs.
84 /**
85 * drm_helper_encoder_in_use - check if a given encoder is in use
86 * @encoder: encoder to check
88 * Checks whether @encoder is with the current mode setting output configuration
89 * in use by any connector. This doesn't mean that it is actually enabled since
90 * the DPMS state is tracked separately.
92 * Returns:
93 * True if @encoder is used, false otherwise.
95 bool drm_helper_encoder_in_use(struct drm_encoder *encoder)
97 struct drm_connector *connector;
98 struct drm_connector_list_iter conn_iter;
99 struct drm_device *dev = encoder->dev;
101 WARN_ON(drm_drv_uses_atomic_modeset(dev));
104 * We can expect this mutex to be locked if we are not panicking.
105 * Locking is currently fubar in the panic handler.
107 if (!oops_in_progress) {
108 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
109 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
113 drm_connector_list_iter_begin(dev, &conn_iter);
114 drm_for_each_connector_iter(connector, &conn_iter) {
115 if (connector->encoder == encoder) {
116 drm_connector_list_iter_end(&conn_iter);
117 return true;
120 drm_connector_list_iter_end(&conn_iter);
121 return false;
123 EXPORT_SYMBOL(drm_helper_encoder_in_use);
126 * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config
127 * @crtc: CRTC to check
129 * Checks whether @crtc is with the current mode setting output configuration
130 * in use by any connector. This doesn't mean that it is actually enabled since
131 * the DPMS state is tracked separately.
133 * Returns:
134 * True if @crtc is used, false otherwise.
136 bool drm_helper_crtc_in_use(struct drm_crtc *crtc)
138 struct drm_encoder *encoder;
139 struct drm_device *dev = crtc->dev;
141 WARN_ON(drm_drv_uses_atomic_modeset(dev));
144 * We can expect this mutex to be locked if we are not panicking.
145 * Locking is currently fubar in the panic handler.
147 if (!oops_in_progress)
148 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
150 drm_for_each_encoder(encoder, dev)
151 if (encoder->crtc == crtc && drm_helper_encoder_in_use(encoder))
152 return true;
153 return false;
155 EXPORT_SYMBOL(drm_helper_crtc_in_use);
157 static void
158 drm_encoder_disable(struct drm_encoder *encoder)
160 const struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
162 if (!encoder_funcs)
163 return;
165 if (encoder_funcs->disable)
166 (*encoder_funcs->disable)(encoder);
167 else if (encoder_funcs->dpms)
168 (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
171 static void __drm_helper_disable_unused_functions(struct drm_device *dev)
173 struct drm_encoder *encoder;
174 struct drm_crtc *crtc;
176 drm_warn_on_modeset_not_all_locked(dev);
178 drm_for_each_encoder(encoder, dev) {
179 if (!drm_helper_encoder_in_use(encoder)) {
180 drm_encoder_disable(encoder);
181 /* disconnect encoder from any connector */
182 encoder->crtc = NULL;
186 drm_for_each_crtc(crtc, dev) {
187 const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
188 crtc->enabled = drm_helper_crtc_in_use(crtc);
189 if (!crtc->enabled) {
190 if (crtc_funcs->disable)
191 (*crtc_funcs->disable)(crtc);
192 else
193 (*crtc_funcs->dpms)(crtc, DRM_MODE_DPMS_OFF);
194 crtc->primary->fb = NULL;
200 * drm_helper_disable_unused_functions - disable unused objects
201 * @dev: DRM device
203 * This function walks through the entire mode setting configuration of @dev. It
204 * will remove any CRTC links of unused encoders and encoder links of
205 * disconnected connectors. Then it will disable all unused encoders and CRTCs
206 * either by calling their disable callback if available or by calling their
207 * dpms callback with DRM_MODE_DPMS_OFF.
209 * NOTE:
211 * This function is part of the legacy modeset helper library and will cause
212 * major confusion with atomic drivers. This is because atomic helpers guarantee
213 * to never call ->disable() hooks on a disabled function, or ->enable() hooks
214 * on an enabled functions. drm_helper_disable_unused_functions() on the other
215 * hand throws such guarantees into the wind and calls disable hooks
216 * unconditionally on unused functions.
218 void drm_helper_disable_unused_functions(struct drm_device *dev)
220 WARN_ON(drm_drv_uses_atomic_modeset(dev));
222 drm_modeset_lock_all(dev);
223 __drm_helper_disable_unused_functions(dev);
224 drm_modeset_unlock_all(dev);
226 EXPORT_SYMBOL(drm_helper_disable_unused_functions);
229 * Check the CRTC we're going to map each output to vs. its current
230 * CRTC. If they don't match, we have to disable the output and the CRTC
231 * since the driver will have to re-route things.
233 static void
234 drm_crtc_prepare_encoders(struct drm_device *dev)
236 const struct drm_encoder_helper_funcs *encoder_funcs;
237 struct drm_encoder *encoder;
239 drm_for_each_encoder(encoder, dev) {
240 encoder_funcs = encoder->helper_private;
241 if (!encoder_funcs)
242 continue;
244 /* Disable unused encoders */
245 if (encoder->crtc == NULL)
246 drm_encoder_disable(encoder);
247 /* Disable encoders whose CRTC is about to change */
248 if (encoder_funcs->get_crtc &&
249 encoder->crtc != (*encoder_funcs->get_crtc)(encoder))
250 drm_encoder_disable(encoder);
255 * drm_crtc_helper_set_mode - internal helper to set a mode
256 * @crtc: CRTC to program
257 * @mode: mode to use
258 * @x: horizontal offset into the surface
259 * @y: vertical offset into the surface
260 * @old_fb: old framebuffer, for cleanup
262 * Try to set @mode on @crtc. Give @crtc and its associated connectors a chance
263 * to fixup or reject the mode prior to trying to set it. This is an internal
264 * helper that drivers could e.g. use to update properties that require the
265 * entire output pipe to be disabled and re-enabled in a new configuration. For
266 * example for changing whether audio is enabled on a hdmi link or for changing
267 * panel fitter or dither attributes. It is also called by the
268 * drm_crtc_helper_set_config() helper function to drive the mode setting
269 * sequence.
271 * Returns:
272 * True if the mode was set successfully, false otherwise.
274 bool drm_crtc_helper_set_mode(struct drm_crtc *crtc,
275 struct drm_display_mode *mode,
276 int x, int y,
277 struct drm_framebuffer *old_fb)
279 struct drm_device *dev = crtc->dev;
280 struct drm_display_mode *adjusted_mode, saved_mode, saved_hwmode;
281 const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
282 const struct drm_encoder_helper_funcs *encoder_funcs;
283 int saved_x, saved_y;
284 bool saved_enabled;
285 struct drm_encoder *encoder;
286 bool ret = true;
288 WARN_ON(drm_drv_uses_atomic_modeset(dev));
290 drm_warn_on_modeset_not_all_locked(dev);
292 saved_enabled = crtc->enabled;
293 crtc->enabled = drm_helper_crtc_in_use(crtc);
294 if (!crtc->enabled)
295 return true;
297 adjusted_mode = drm_mode_duplicate(dev, mode);
298 if (!adjusted_mode) {
299 crtc->enabled = saved_enabled;
300 return false;
303 saved_mode = crtc->mode;
304 saved_hwmode = crtc->hwmode;
305 saved_x = crtc->x;
306 saved_y = crtc->y;
308 /* Update crtc values up front so the driver can rely on them for mode
309 * setting.
311 crtc->mode = *mode;
312 crtc->x = x;
313 crtc->y = y;
315 /* Pass our mode to the connectors and the CRTC to give them a chance to
316 * adjust it according to limitations or connector properties, and also
317 * a chance to reject the mode entirely.
319 drm_for_each_encoder(encoder, dev) {
321 if (encoder->crtc != crtc)
322 continue;
324 encoder_funcs = encoder->helper_private;
325 if (!encoder_funcs)
326 continue;
328 encoder_funcs = encoder->helper_private;
329 if (encoder_funcs->mode_fixup) {
330 if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
331 adjusted_mode))) {
332 DRM_DEBUG_KMS("Encoder fixup failed\n");
333 goto done;
338 if (crtc_funcs->mode_fixup) {
339 if (!(ret = crtc_funcs->mode_fixup(crtc, mode,
340 adjusted_mode))) {
341 DRM_DEBUG_KMS("CRTC fixup failed\n");
342 goto done;
345 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
347 crtc->hwmode = *adjusted_mode;
349 /* Prepare the encoders and CRTCs before setting the mode. */
350 drm_for_each_encoder(encoder, dev) {
352 if (encoder->crtc != crtc)
353 continue;
355 encoder_funcs = encoder->helper_private;
356 if (!encoder_funcs)
357 continue;
359 /* Disable the encoders as the first thing we do. */
360 if (encoder_funcs->prepare)
361 encoder_funcs->prepare(encoder);
364 drm_crtc_prepare_encoders(dev);
366 crtc_funcs->prepare(crtc);
368 /* Set up the DPLL and any encoders state that needs to adjust or depend
369 * on the DPLL.
371 ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
372 if (!ret)
373 goto done;
375 drm_for_each_encoder(encoder, dev) {
377 if (encoder->crtc != crtc)
378 continue;
380 encoder_funcs = encoder->helper_private;
381 if (!encoder_funcs)
382 continue;
384 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%s]\n",
385 encoder->base.id, encoder->name, mode->name);
386 if (encoder_funcs->mode_set)
387 encoder_funcs->mode_set(encoder, mode, adjusted_mode);
390 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
391 crtc_funcs->commit(crtc);
393 drm_for_each_encoder(encoder, dev) {
395 if (encoder->crtc != crtc)
396 continue;
398 encoder_funcs = encoder->helper_private;
399 if (!encoder_funcs)
400 continue;
402 if (encoder_funcs->commit)
403 encoder_funcs->commit(encoder);
406 /* Calculate and store various constants which
407 * are later needed by vblank and swap-completion
408 * timestamping. They are derived from true hwmode.
410 drm_calc_timestamping_constants(crtc, &crtc->hwmode);
412 /* FIXME: add subpixel order */
413 done:
414 drm_mode_destroy(dev, adjusted_mode);
415 if (!ret) {
416 crtc->enabled = saved_enabled;
417 crtc->mode = saved_mode;
418 crtc->hwmode = saved_hwmode;
419 crtc->x = saved_x;
420 crtc->y = saved_y;
423 return ret;
425 EXPORT_SYMBOL(drm_crtc_helper_set_mode);
427 static void
428 drm_crtc_helper_disable(struct drm_crtc *crtc)
430 struct drm_device *dev = crtc->dev;
431 struct drm_connector *connector;
432 struct drm_encoder *encoder;
434 /* Decouple all encoders and their attached connectors from this crtc */
435 drm_for_each_encoder(encoder, dev) {
436 struct drm_connector_list_iter conn_iter;
438 if (encoder->crtc != crtc)
439 continue;
441 drm_connector_list_iter_begin(dev, &conn_iter);
442 drm_for_each_connector_iter(connector, &conn_iter) {
443 if (connector->encoder != encoder)
444 continue;
446 connector->encoder = NULL;
449 * drm_helper_disable_unused_functions() ought to be
450 * doing this, but since we've decoupled the encoder
451 * from the connector above, the required connection
452 * between them is henceforth no longer available.
454 connector->dpms = DRM_MODE_DPMS_OFF;
456 /* we keep a reference while the encoder is bound */
457 drm_connector_put(connector);
459 drm_connector_list_iter_end(&conn_iter);
462 __drm_helper_disable_unused_functions(dev);
466 * For connectors that support multiple encoders, either the
467 * .atomic_best_encoder() or .best_encoder() operation must be implemented.
469 struct drm_encoder *
470 drm_connector_get_single_encoder(struct drm_connector *connector)
472 struct drm_encoder *encoder;
474 WARN_ON(hweight32(connector->possible_encoders) > 1);
475 drm_connector_for_each_possible_encoder(connector, encoder)
476 return encoder;
478 return NULL;
482 * drm_crtc_helper_set_config - set a new config from userspace
483 * @set: mode set configuration
484 * @ctx: lock acquire context, not used here
486 * The drm_crtc_helper_set_config() helper function implements the of
487 * &drm_crtc_funcs.set_config callback for drivers using the legacy CRTC
488 * helpers.
490 * It first tries to locate the best encoder for each connector by calling the
491 * connector @drm_connector_helper_funcs.best_encoder helper operation.
493 * After locating the appropriate encoders, the helper function will call the
494 * mode_fixup encoder and CRTC helper operations to adjust the requested mode,
495 * or reject it completely in which case an error will be returned to the
496 * application. If the new configuration after mode adjustment is identical to
497 * the current configuration the helper function will return without performing
498 * any other operation.
500 * If the adjusted mode is identical to the current mode but changes to the
501 * frame buffer need to be applied, the drm_crtc_helper_set_config() function
502 * will call the CRTC &drm_crtc_helper_funcs.mode_set_base helper operation.
504 * If the adjusted mode differs from the current mode, or if the
505 * ->mode_set_base() helper operation is not provided, the helper function
506 * performs a full mode set sequence by calling the ->prepare(), ->mode_set()
507 * and ->commit() CRTC and encoder helper operations, in that order.
508 * Alternatively it can also use the dpms and disable helper operations. For
509 * details see &struct drm_crtc_helper_funcs and struct
510 * &drm_encoder_helper_funcs.
512 * This function is deprecated. New drivers must implement atomic modeset
513 * support, for which this function is unsuitable. Instead drivers should use
514 * drm_atomic_helper_set_config().
516 * Returns:
517 * Returns 0 on success, negative errno numbers on failure.
519 int drm_crtc_helper_set_config(struct drm_mode_set *set,
520 struct drm_modeset_acquire_ctx *ctx)
522 struct drm_device *dev;
523 struct drm_crtc **save_encoder_crtcs, *new_crtc;
524 struct drm_encoder **save_connector_encoders, *new_encoder, *encoder;
525 bool mode_changed = false; /* if true do a full mode set */
526 bool fb_changed = false; /* if true and !mode_changed just do a flip */
527 struct drm_connector *connector;
528 struct drm_connector_list_iter conn_iter;
529 int count = 0, ro, fail = 0;
530 const struct drm_crtc_helper_funcs *crtc_funcs;
531 struct drm_mode_set save_set;
532 int ret;
533 int i;
535 DRM_DEBUG_KMS("\n");
537 BUG_ON(!set);
538 BUG_ON(!set->crtc);
539 BUG_ON(!set->crtc->helper_private);
541 /* Enforce sane interface api - has been abused by the fb helper. */
542 BUG_ON(!set->mode && set->fb);
543 BUG_ON(set->fb && set->num_connectors == 0);
545 crtc_funcs = set->crtc->helper_private;
547 dev = set->crtc->dev;
548 WARN_ON(drm_drv_uses_atomic_modeset(dev));
550 if (!set->mode)
551 set->fb = NULL;
553 if (set->fb) {
554 DRM_DEBUG_KMS("[CRTC:%d:%s] [FB:%d] #connectors=%d (x y) (%i %i)\n",
555 set->crtc->base.id, set->crtc->name,
556 set->fb->base.id,
557 (int)set->num_connectors, set->x, set->y);
558 } else {
559 DRM_DEBUG_KMS("[CRTC:%d:%s] [NOFB]\n",
560 set->crtc->base.id, set->crtc->name);
561 drm_crtc_helper_disable(set->crtc);
562 return 0;
565 drm_warn_on_modeset_not_all_locked(dev);
568 * Allocate space for the backup of all (non-pointer) encoder and
569 * connector data.
571 save_encoder_crtcs = kcalloc(dev->mode_config.num_encoder,
572 sizeof(struct drm_crtc *), GFP_KERNEL);
573 if (!save_encoder_crtcs)
574 return -ENOMEM;
576 save_connector_encoders = kcalloc(dev->mode_config.num_connector,
577 sizeof(struct drm_encoder *), GFP_KERNEL);
578 if (!save_connector_encoders) {
579 kfree(save_encoder_crtcs);
580 return -ENOMEM;
584 * Copy data. Note that driver private data is not affected.
585 * Should anything bad happen only the expected state is
586 * restored, not the drivers personal bookkeeping.
588 count = 0;
589 drm_for_each_encoder(encoder, dev) {
590 save_encoder_crtcs[count++] = encoder->crtc;
593 count = 0;
594 drm_connector_list_iter_begin(dev, &conn_iter);
595 drm_for_each_connector_iter(connector, &conn_iter)
596 save_connector_encoders[count++] = connector->encoder;
597 drm_connector_list_iter_end(&conn_iter);
599 save_set.crtc = set->crtc;
600 save_set.mode = &set->crtc->mode;
601 save_set.x = set->crtc->x;
602 save_set.y = set->crtc->y;
603 save_set.fb = set->crtc->primary->fb;
605 /* We should be able to check here if the fb has the same properties
606 * and then just flip_or_move it */
607 if (set->crtc->primary->fb != set->fb) {
608 /* If we have no fb then treat it as a full mode set */
609 if (set->crtc->primary->fb == NULL) {
610 DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
611 mode_changed = true;
612 } else if (set->fb->format != set->crtc->primary->fb->format) {
613 mode_changed = true;
614 } else
615 fb_changed = true;
618 if (set->x != set->crtc->x || set->y != set->crtc->y)
619 fb_changed = true;
621 if (!drm_mode_equal(set->mode, &set->crtc->mode)) {
622 DRM_DEBUG_KMS("modes are different, full mode set\n");
623 drm_mode_debug_printmodeline(&set->crtc->mode);
624 drm_mode_debug_printmodeline(set->mode);
625 mode_changed = true;
628 /* take a reference on all unbound connectors in set, reuse the
629 * already taken reference for bound connectors
631 for (ro = 0; ro < set->num_connectors; ro++) {
632 if (set->connectors[ro]->encoder)
633 continue;
634 drm_connector_get(set->connectors[ro]);
637 /* a) traverse passed in connector list and get encoders for them */
638 count = 0;
639 drm_connector_list_iter_begin(dev, &conn_iter);
640 drm_for_each_connector_iter(connector, &conn_iter) {
641 const struct drm_connector_helper_funcs *connector_funcs =
642 connector->helper_private;
643 new_encoder = connector->encoder;
644 for (ro = 0; ro < set->num_connectors; ro++) {
645 if (set->connectors[ro] == connector) {
646 if (connector_funcs->best_encoder)
647 new_encoder = connector_funcs->best_encoder(connector);
648 else
649 new_encoder = drm_connector_get_single_encoder(connector);
651 /* if we can't get an encoder for a connector
652 we are setting now - then fail */
653 if (new_encoder == NULL)
654 /* don't break so fail path works correct */
655 fail = 1;
657 if (connector->dpms != DRM_MODE_DPMS_ON) {
658 DRM_DEBUG_KMS("connector dpms not on, full mode switch\n");
659 mode_changed = true;
662 break;
666 if (new_encoder != connector->encoder) {
667 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
668 mode_changed = true;
669 /* If the encoder is reused for another connector, then
670 * the appropriate crtc will be set later.
672 if (connector->encoder)
673 connector->encoder->crtc = NULL;
674 connector->encoder = new_encoder;
677 drm_connector_list_iter_end(&conn_iter);
679 if (fail) {
680 ret = -EINVAL;
681 goto fail;
684 count = 0;
685 drm_connector_list_iter_begin(dev, &conn_iter);
686 drm_for_each_connector_iter(connector, &conn_iter) {
687 if (!connector->encoder)
688 continue;
690 if (connector->encoder->crtc == set->crtc)
691 new_crtc = NULL;
692 else
693 new_crtc = connector->encoder->crtc;
695 for (ro = 0; ro < set->num_connectors; ro++) {
696 if (set->connectors[ro] == connector)
697 new_crtc = set->crtc;
700 /* Make sure the new CRTC will work with the encoder */
701 if (new_crtc &&
702 !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
703 ret = -EINVAL;
704 drm_connector_list_iter_end(&conn_iter);
705 goto fail;
707 if (new_crtc != connector->encoder->crtc) {
708 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
709 mode_changed = true;
710 connector->encoder->crtc = new_crtc;
712 if (new_crtc) {
713 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d:%s]\n",
714 connector->base.id, connector->name,
715 new_crtc->base.id, new_crtc->name);
716 } else {
717 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
718 connector->base.id, connector->name);
721 drm_connector_list_iter_end(&conn_iter);
723 /* mode_set_base is not a required function */
724 if (fb_changed && !crtc_funcs->mode_set_base)
725 mode_changed = true;
727 if (mode_changed) {
728 if (drm_helper_crtc_in_use(set->crtc)) {
729 DRM_DEBUG_KMS("attempting to set mode from"
730 " userspace\n");
731 drm_mode_debug_printmodeline(set->mode);
732 set->crtc->primary->fb = set->fb;
733 if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
734 set->x, set->y,
735 save_set.fb)) {
736 DRM_ERROR("failed to set mode on [CRTC:%d:%s]\n",
737 set->crtc->base.id, set->crtc->name);
738 set->crtc->primary->fb = save_set.fb;
739 ret = -EINVAL;
740 goto fail;
742 DRM_DEBUG_KMS("Setting connector DPMS state to on\n");
743 for (i = 0; i < set->num_connectors; i++) {
744 DRM_DEBUG_KMS("\t[CONNECTOR:%d:%s] set DPMS on\n", set->connectors[i]->base.id,
745 set->connectors[i]->name);
746 set->connectors[i]->funcs->dpms(set->connectors[i], DRM_MODE_DPMS_ON);
749 __drm_helper_disable_unused_functions(dev);
750 } else if (fb_changed) {
751 set->crtc->x = set->x;
752 set->crtc->y = set->y;
753 set->crtc->primary->fb = set->fb;
754 ret = crtc_funcs->mode_set_base(set->crtc,
755 set->x, set->y, save_set.fb);
756 if (ret != 0) {
757 set->crtc->x = save_set.x;
758 set->crtc->y = save_set.y;
759 set->crtc->primary->fb = save_set.fb;
760 goto fail;
764 kfree(save_connector_encoders);
765 kfree(save_encoder_crtcs);
766 return 0;
768 fail:
769 /* Restore all previous data. */
770 count = 0;
771 drm_for_each_encoder(encoder, dev) {
772 encoder->crtc = save_encoder_crtcs[count++];
775 count = 0;
776 drm_connector_list_iter_begin(dev, &conn_iter);
777 drm_for_each_connector_iter(connector, &conn_iter)
778 connector->encoder = save_connector_encoders[count++];
779 drm_connector_list_iter_end(&conn_iter);
781 /* after fail drop reference on all unbound connectors in set, let
782 * bound connectors keep their reference
784 for (ro = 0; ro < set->num_connectors; ro++) {
785 if (set->connectors[ro]->encoder)
786 continue;
787 drm_connector_put(set->connectors[ro]);
790 /* Try to restore the config */
791 if (mode_changed &&
792 !drm_crtc_helper_set_mode(save_set.crtc, save_set.mode, save_set.x,
793 save_set.y, save_set.fb))
794 DRM_ERROR("failed to restore config after modeset failure\n");
796 kfree(save_connector_encoders);
797 kfree(save_encoder_crtcs);
798 return ret;
800 EXPORT_SYMBOL(drm_crtc_helper_set_config);
802 static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder)
804 int dpms = DRM_MODE_DPMS_OFF;
805 struct drm_connector *connector;
806 struct drm_connector_list_iter conn_iter;
807 struct drm_device *dev = encoder->dev;
809 drm_connector_list_iter_begin(dev, &conn_iter);
810 drm_for_each_connector_iter(connector, &conn_iter)
811 if (connector->encoder == encoder)
812 if (connector->dpms < dpms)
813 dpms = connector->dpms;
814 drm_connector_list_iter_end(&conn_iter);
816 return dpms;
819 /* Helper which handles bridge ordering around encoder dpms */
820 static void drm_helper_encoder_dpms(struct drm_encoder *encoder, int mode)
822 const struct drm_encoder_helper_funcs *encoder_funcs;
824 encoder_funcs = encoder->helper_private;
825 if (!encoder_funcs)
826 return;
828 if (encoder_funcs->dpms)
829 encoder_funcs->dpms(encoder, mode);
832 static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc)
834 int dpms = DRM_MODE_DPMS_OFF;
835 struct drm_connector *connector;
836 struct drm_connector_list_iter conn_iter;
837 struct drm_device *dev = crtc->dev;
839 drm_connector_list_iter_begin(dev, &conn_iter);
840 drm_for_each_connector_iter(connector, &conn_iter)
841 if (connector->encoder && connector->encoder->crtc == crtc)
842 if (connector->dpms < dpms)
843 dpms = connector->dpms;
844 drm_connector_list_iter_end(&conn_iter);
846 return dpms;
850 * drm_helper_connector_dpms() - connector dpms helper implementation
851 * @connector: affected connector
852 * @mode: DPMS mode
854 * The drm_helper_connector_dpms() helper function implements the
855 * &drm_connector_funcs.dpms callback for drivers using the legacy CRTC
856 * helpers.
858 * This is the main helper function provided by the CRTC helper framework for
859 * implementing the DPMS connector attribute. It computes the new desired DPMS
860 * state for all encoders and CRTCs in the output mesh and calls the
861 * &drm_crtc_helper_funcs.dpms and &drm_encoder_helper_funcs.dpms callbacks
862 * provided by the driver.
864 * This function is deprecated. New drivers must implement atomic modeset
865 * support, where DPMS is handled in the DRM core.
867 * Returns:
868 * Always returns 0.
870 int drm_helper_connector_dpms(struct drm_connector *connector, int mode)
872 struct drm_encoder *encoder = connector->encoder;
873 struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
874 int old_dpms, encoder_dpms = DRM_MODE_DPMS_OFF;
876 WARN_ON(drm_drv_uses_atomic_modeset(connector->dev));
878 if (mode == connector->dpms)
879 return 0;
881 old_dpms = connector->dpms;
882 connector->dpms = mode;
884 if (encoder)
885 encoder_dpms = drm_helper_choose_encoder_dpms(encoder);
887 /* from off to on, do crtc then encoder */
888 if (mode < old_dpms) {
889 if (crtc) {
890 const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
891 if (crtc_funcs->dpms)
892 (*crtc_funcs->dpms) (crtc,
893 drm_helper_choose_crtc_dpms(crtc));
895 if (encoder)
896 drm_helper_encoder_dpms(encoder, encoder_dpms);
899 /* from on to off, do encoder then crtc */
900 if (mode > old_dpms) {
901 if (encoder)
902 drm_helper_encoder_dpms(encoder, encoder_dpms);
903 if (crtc) {
904 const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
905 if (crtc_funcs->dpms)
906 (*crtc_funcs->dpms) (crtc,
907 drm_helper_choose_crtc_dpms(crtc));
911 return 0;
913 EXPORT_SYMBOL(drm_helper_connector_dpms);
916 * drm_helper_resume_force_mode - force-restore mode setting configuration
917 * @dev: drm_device which should be restored
919 * Drivers which use the mode setting helpers can use this function to
920 * force-restore the mode setting configuration e.g. on resume or when something
921 * else might have trampled over the hw state (like some overzealous old BIOSen
922 * tended to do).
924 * This helper doesn't provide a error return value since restoring the old
925 * config should never fail due to resource allocation issues since the driver
926 * has successfully set the restored configuration already. Hence this should
927 * boil down to the equivalent of a few dpms on calls, which also don't provide
928 * an error code.
930 * Drivers where simply restoring an old configuration again might fail (e.g.
931 * due to slight differences in allocating shared resources when the
932 * configuration is restored in a different order than when userspace set it up)
933 * need to use their own restore logic.
935 * This function is deprecated. New drivers should implement atomic mode-
936 * setting and use the atomic suspend/resume helpers.
938 * See also:
939 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
941 void drm_helper_resume_force_mode(struct drm_device *dev)
943 struct drm_crtc *crtc;
944 struct drm_encoder *encoder;
945 const struct drm_crtc_helper_funcs *crtc_funcs;
946 int encoder_dpms;
947 bool ret;
949 WARN_ON(drm_drv_uses_atomic_modeset(dev));
951 drm_modeset_lock_all(dev);
952 drm_for_each_crtc(crtc, dev) {
954 if (!crtc->enabled)
955 continue;
957 ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
958 crtc->x, crtc->y, crtc->primary->fb);
960 /* Restoring the old config should never fail! */
961 if (ret == false)
962 DRM_ERROR("failed to set mode on crtc %p\n", crtc);
964 /* Turn off outputs that were already powered off */
965 if (drm_helper_choose_crtc_dpms(crtc)) {
966 drm_for_each_encoder(encoder, dev) {
968 if(encoder->crtc != crtc)
969 continue;
971 encoder_dpms = drm_helper_choose_encoder_dpms(
972 encoder);
974 drm_helper_encoder_dpms(encoder, encoder_dpms);
977 crtc_funcs = crtc->helper_private;
978 if (crtc_funcs->dpms)
979 (*crtc_funcs->dpms) (crtc,
980 drm_helper_choose_crtc_dpms(crtc));
984 /* disable the unused connectors while restoring the modesetting */
985 __drm_helper_disable_unused_functions(dev);
986 drm_modeset_unlock_all(dev);
988 EXPORT_SYMBOL(drm_helper_resume_force_mode);
991 * drm_helper_force_disable_all - Forcibly turn off all enabled CRTCs
992 * @dev: DRM device whose CRTCs to turn off
994 * Drivers may want to call this on unload to ensure that all displays are
995 * unlit and the GPU is in a consistent, low power state. Takes modeset locks.
997 * Note: This should only be used by non-atomic legacy drivers. For an atomic
998 * version look at drm_atomic_helper_shutdown().
1000 * Returns:
1001 * Zero on success, error code on failure.
1003 int drm_helper_force_disable_all(struct drm_device *dev)
1005 struct drm_crtc *crtc;
1006 int ret = 0;
1008 drm_modeset_lock_all(dev);
1009 drm_for_each_crtc(crtc, dev)
1010 if (crtc->enabled) {
1011 struct drm_mode_set set = {
1012 .crtc = crtc,
1015 ret = drm_mode_set_config_internal(&set);
1016 if (ret)
1017 goto out;
1019 out:
1020 drm_modeset_unlock_all(dev);
1021 return ret;
1023 EXPORT_SYMBOL(drm_helper_force_disable_all);