xref: /dflybsd-src/sys/dev/drm/drm_crtc_helper.c (revision 3f2dd94a569761201b5b0a18b2f697f97fe1b9dc)
15718399fSFrançois Tigeot /*
25718399fSFrançois Tigeot  * Copyright (c) 2006-2008 Intel Corporation
35718399fSFrançois Tigeot  * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
45718399fSFrançois Tigeot  *
55718399fSFrançois Tigeot  * DRM core CRTC related functions
65718399fSFrançois Tigeot  *
75718399fSFrançois Tigeot  * Permission to use, copy, modify, distribute, and sell this software and its
85718399fSFrançois Tigeot  * documentation for any purpose is hereby granted without fee, provided that
95718399fSFrançois Tigeot  * the above copyright notice appear in all copies and that both that copyright
105718399fSFrançois Tigeot  * notice and this permission notice appear in supporting documentation, and
115718399fSFrançois Tigeot  * that the name of the copyright holders not be used in advertising or
125718399fSFrançois Tigeot  * publicity pertaining to distribution of the software without specific,
135718399fSFrançois Tigeot  * written prior permission.  The copyright holders make no representations
145718399fSFrançois Tigeot  * about the suitability of this software for any purpose.  It is provided "as
155718399fSFrançois Tigeot  * is" without express or implied warranty.
165718399fSFrançois Tigeot  *
175718399fSFrançois Tigeot  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
185718399fSFrançois Tigeot  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
195718399fSFrançois Tigeot  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
205718399fSFrançois Tigeot  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
215718399fSFrançois Tigeot  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
225718399fSFrançois Tigeot  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
235718399fSFrançois Tigeot  * OF THIS SOFTWARE.
245718399fSFrançois Tigeot  *
255718399fSFrançois Tigeot  * Authors:
265718399fSFrançois Tigeot  *      Keith Packard
275718399fSFrançois Tigeot  *	Eric Anholt <eric@anholt.net>
285718399fSFrançois Tigeot  *      Dave Airlie <airlied@linux.ie>
295718399fSFrançois Tigeot  *      Jesse Barnes <jesse.barnes@intel.com>
305718399fSFrançois Tigeot  */
315718399fSFrançois Tigeot 
32a05eeebfSFrançois Tigeot #include <linux/kernel.h>
336e29dde8SFrançois Tigeot #include <linux/export.h>
344dbb207bSFrançois Tigeot #include <linux/moduleparam.h>
356e29dde8SFrançois Tigeot 
3618e26a6dSFrançois Tigeot #include <drm/drmP.h>
372c9916cdSFrançois Tigeot #include <drm/drm_atomic.h>
3818e26a6dSFrançois Tigeot #include <drm/drm_crtc.h>
39a85cb24fSFrançois Tigeot #include <drm/drm_encoder.h>
4083b4b9b9SFrançois Tigeot #include <drm/drm_fourcc.h>
4118e26a6dSFrançois Tigeot #include <drm/drm_crtc_helper.h>
4218e26a6dSFrançois Tigeot #include <drm/drm_fb_helper.h>
432c9916cdSFrançois Tigeot #include <drm/drm_plane_helper.h>
442c9916cdSFrançois Tigeot #include <drm/drm_atomic_helper.h>
456e29dde8SFrançois Tigeot #include <drm/drm_edid.h>
465718399fSFrançois Tigeot 
472c9916cdSFrançois Tigeot /**
482c9916cdSFrançois Tigeot  * DOC: overview
492c9916cdSFrançois Tigeot  *
502c9916cdSFrançois Tigeot  * The CRTC modeset helper library provides a default set_config implementation
512c9916cdSFrançois Tigeot  * in drm_crtc_helper_set_config(). Plus a few other convenience functions using
522c9916cdSFrançois Tigeot  * the same callbacks which drivers can use to e.g. restore the modeset
532c9916cdSFrançois Tigeot  * configuration on resume with drm_helper_resume_force_mode().
542c9916cdSFrançois Tigeot  *
55aee94f86SFrançois Tigeot  * Note that this helper library doesn't track the current power state of CRTCs
56a85cb24fSFrançois Tigeot  * and encoders. It can call callbacks like &drm_encoder_helper_funcs.dpms even
57a85cb24fSFrançois Tigeot  * though the hardware is already in the desired state. This deficiency has been
58a85cb24fSFrançois Tigeot  * fixed in the atomic helpers.
59aee94f86SFrançois Tigeot  *
602c9916cdSFrançois Tigeot  * The driver callbacks are mostly compatible with the atomic modeset helpers,
612c9916cdSFrançois Tigeot  * except for the handling of the primary plane: Atomic helpers require that the
622c9916cdSFrançois Tigeot  * primary plane is implemented as a real standalone plane and not directly tied
632c9916cdSFrançois Tigeot  * to the CRTC state. For easier transition this library provides functions to
642c9916cdSFrançois Tigeot  * implement the old semantics required by the CRTC helpers using the new plane
652c9916cdSFrançois Tigeot  * and atomic helper callbacks.
662c9916cdSFrançois Tigeot  *
672c9916cdSFrançois Tigeot  * Drivers are strongly urged to convert to the atomic helpers (by way of first
682c9916cdSFrançois Tigeot  * converting to the plane helpers). New drivers must not use these functions
692c9916cdSFrançois Tigeot  * but need to implement the atomic interface instead, potentially using the
702c9916cdSFrançois Tigeot  * atomic helpers for that.
71aee94f86SFrançois Tigeot  *
72aee94f86SFrançois Tigeot  * These legacy modeset helpers use the same function table structures as
73aee94f86SFrançois Tigeot  * all other modesetting helpers. See the documentation for struct
74*3f2dd94aSFrançois Tigeot  * &drm_crtc_helper_funcs, &struct drm_encoder_helper_funcs and struct
75aee94f86SFrançois Tigeot  * &drm_connector_helper_funcs.
762c9916cdSFrançois Tigeot  */
779edbd4a0SFrançois Tigeot 
786e29dde8SFrançois Tigeot /**
795718399fSFrançois Tigeot  * drm_helper_encoder_in_use - check if a given encoder is in use
805718399fSFrançois Tigeot  * @encoder: encoder to check
815718399fSFrançois Tigeot  *
82ba55f2f5SFrançois Tigeot  * Checks whether @encoder is with the current mode setting output configuration
83ba55f2f5SFrançois Tigeot  * in use by any connector. This doesn't mean that it is actually enabled since
84ba55f2f5SFrançois Tigeot  * the DPMS state is tracked separately.
855718399fSFrançois Tigeot  *
86ba55f2f5SFrançois Tigeot  * Returns:
87ba55f2f5SFrançois Tigeot  * True if @encoder is used, false otherwise.
885718399fSFrançois Tigeot  */
drm_helper_encoder_in_use(struct drm_encoder * encoder)895718399fSFrançois Tigeot bool drm_helper_encoder_in_use(struct drm_encoder *encoder)
905718399fSFrançois Tigeot {
915718399fSFrançois Tigeot 	struct drm_connector *connector;
92a85cb24fSFrançois Tigeot 	struct drm_connector_list_iter conn_iter;
935718399fSFrançois Tigeot 	struct drm_device *dev = encoder->dev;
94ba55f2f5SFrançois Tigeot 
95a05eeebfSFrançois Tigeot 	/*
96a05eeebfSFrançois Tigeot 	 * We can expect this mutex to be locked if we are not panicking.
97a05eeebfSFrançois Tigeot 	 * Locking is currently fubar in the panic handler.
98a05eeebfSFrançois Tigeot 	 */
99a05eeebfSFrançois Tigeot 	if (!oops_in_progress) {
100ba55f2f5SFrançois Tigeot 		WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
101ba55f2f5SFrançois Tigeot 		WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
102a05eeebfSFrançois Tigeot 	}
103a05eeebfSFrançois Tigeot 
104a85cb24fSFrançois Tigeot 
105a85cb24fSFrançois Tigeot 	drm_connector_list_iter_begin(dev, &conn_iter);
106a85cb24fSFrançois Tigeot 	drm_for_each_connector_iter(connector, &conn_iter) {
107a85cb24fSFrançois Tigeot 		if (connector->encoder == encoder) {
108a85cb24fSFrançois Tigeot 			drm_connector_list_iter_end(&conn_iter);
1095718399fSFrançois Tigeot 			return true;
110a85cb24fSFrançois Tigeot 		}
111a85cb24fSFrançois Tigeot 	}
112a85cb24fSFrançois Tigeot 	drm_connector_list_iter_end(&conn_iter);
1135718399fSFrançois Tigeot 	return false;
1145718399fSFrançois Tigeot }
1156e29dde8SFrançois Tigeot EXPORT_SYMBOL(drm_helper_encoder_in_use);
1165718399fSFrançois Tigeot 
1175718399fSFrançois Tigeot /**
1185718399fSFrançois Tigeot  * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config
1195718399fSFrançois Tigeot  * @crtc: CRTC to check
1205718399fSFrançois Tigeot  *
121ba55f2f5SFrançois Tigeot  * Checks whether @crtc is with the current mode setting output configuration
122ba55f2f5SFrançois Tigeot  * in use by any connector. This doesn't mean that it is actually enabled since
123ba55f2f5SFrançois Tigeot  * the DPMS state is tracked separately.
1245718399fSFrançois Tigeot  *
125ba55f2f5SFrançois Tigeot  * Returns:
126ba55f2f5SFrançois Tigeot  * True if @crtc is used, false otherwise.
1275718399fSFrançois Tigeot  */
drm_helper_crtc_in_use(struct drm_crtc * crtc)1285718399fSFrançois Tigeot bool drm_helper_crtc_in_use(struct drm_crtc *crtc)
1295718399fSFrançois Tigeot {
1305718399fSFrançois Tigeot 	struct drm_encoder *encoder;
1315718399fSFrançois Tigeot 	struct drm_device *dev = crtc->dev;
132ba55f2f5SFrançois Tigeot 
133a05eeebfSFrançois Tigeot 	/*
134a05eeebfSFrançois Tigeot 	 * We can expect this mutex to be locked if we are not panicking.
135a05eeebfSFrançois Tigeot 	 * Locking is currently fubar in the panic handler.
136a05eeebfSFrançois Tigeot 	 */
137a05eeebfSFrançois Tigeot 	if (!oops_in_progress)
138ba55f2f5SFrançois Tigeot 		WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
139a05eeebfSFrançois Tigeot 
140a05eeebfSFrançois Tigeot 	drm_for_each_encoder(encoder, dev)
1415718399fSFrançois Tigeot 		if (encoder->crtc == crtc && drm_helper_encoder_in_use(encoder))
1425718399fSFrançois Tigeot 			return true;
1435718399fSFrançois Tigeot 	return false;
1445718399fSFrançois Tigeot }
1456e29dde8SFrançois Tigeot EXPORT_SYMBOL(drm_helper_crtc_in_use);
1465718399fSFrançois Tigeot 
1475718399fSFrançois Tigeot static void
drm_encoder_disable(struct drm_encoder * encoder)1485718399fSFrançois Tigeot drm_encoder_disable(struct drm_encoder *encoder)
1495718399fSFrançois Tigeot {
150477eb7f9SFrançois Tigeot 	const struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
1515718399fSFrançois Tigeot 
1528621f407SFrançois Tigeot 	if (!encoder_funcs)
1538621f407SFrançois Tigeot 		return;
1548621f407SFrançois Tigeot 
15519c468b4SFrançois Tigeot 	drm_bridge_disable(encoder->bridge);
156ba55f2f5SFrançois Tigeot 
1575718399fSFrançois Tigeot 	if (encoder_funcs->disable)
1585718399fSFrançois Tigeot 		(*encoder_funcs->disable)(encoder);
1598621f407SFrançois Tigeot 	else if (encoder_funcs->dpms)
1605718399fSFrançois Tigeot 		(*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
161ba55f2f5SFrançois Tigeot 
16219c468b4SFrançois Tigeot 	drm_bridge_post_disable(encoder->bridge);
1635718399fSFrançois Tigeot }
1645718399fSFrançois Tigeot 
__drm_helper_disable_unused_functions(struct drm_device * dev)165ba55f2f5SFrançois Tigeot static void __drm_helper_disable_unused_functions(struct drm_device *dev)
1665718399fSFrançois Tigeot {
1675718399fSFrançois Tigeot 	struct drm_encoder *encoder;
1685718399fSFrançois Tigeot 	struct drm_crtc *crtc;
1695718399fSFrançois Tigeot 
170ba55f2f5SFrançois Tigeot 	drm_warn_on_modeset_not_all_locked(dev);
1715718399fSFrançois Tigeot 
172a05eeebfSFrançois Tigeot 	drm_for_each_encoder(encoder, dev) {
1735718399fSFrançois Tigeot 		if (!drm_helper_encoder_in_use(encoder)) {
1745718399fSFrançois Tigeot 			drm_encoder_disable(encoder);
175ba55f2f5SFrançois Tigeot 			/* disconnect encoder from any connector */
1765718399fSFrançois Tigeot 			encoder->crtc = NULL;
1775718399fSFrançois Tigeot 		}
1785718399fSFrançois Tigeot 	}
1795718399fSFrançois Tigeot 
180a05eeebfSFrançois Tigeot 	drm_for_each_crtc(crtc, dev) {
181477eb7f9SFrançois Tigeot 		const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
1825718399fSFrançois Tigeot 		crtc->enabled = drm_helper_crtc_in_use(crtc);
1835718399fSFrançois Tigeot 		if (!crtc->enabled) {
1845718399fSFrançois Tigeot 			if (crtc_funcs->disable)
1855718399fSFrançois Tigeot 				(*crtc_funcs->disable)(crtc);
1865718399fSFrançois Tigeot 			else
1875718399fSFrançois Tigeot 				(*crtc_funcs->dpms)(crtc, DRM_MODE_DPMS_OFF);
188ba55f2f5SFrançois Tigeot 			crtc->primary->fb = NULL;
1895718399fSFrançois Tigeot 		}
1905718399fSFrançois Tigeot 	}
1915718399fSFrançois Tigeot }
192ba55f2f5SFrançois Tigeot 
193ba55f2f5SFrançois Tigeot /**
194ba55f2f5SFrançois Tigeot  * drm_helper_disable_unused_functions - disable unused objects
195ba55f2f5SFrançois Tigeot  * @dev: DRM device
196ba55f2f5SFrançois Tigeot  *
197ba55f2f5SFrançois Tigeot  * This function walks through the entire mode setting configuration of @dev. It
198aee94f86SFrançois Tigeot  * will remove any CRTC links of unused encoders and encoder links of
199aee94f86SFrançois Tigeot  * disconnected connectors. Then it will disable all unused encoders and CRTCs
200ba55f2f5SFrançois Tigeot  * either by calling their disable callback if available or by calling their
201ba55f2f5SFrançois Tigeot  * dpms callback with DRM_MODE_DPMS_OFF.
202c0e85e96SFrançois Tigeot  *
203c0e85e96SFrançois Tigeot  * NOTE:
204c0e85e96SFrançois Tigeot  *
205c0e85e96SFrançois Tigeot  * This function is part of the legacy modeset helper library and will cause
206c0e85e96SFrançois Tigeot  * major confusion with atomic drivers. This is because atomic helpers guarantee
207c0e85e96SFrançois Tigeot  * to never call ->disable() hooks on a disabled function, or ->enable() hooks
208c0e85e96SFrançois Tigeot  * on an enabled functions. drm_helper_disable_unused_functions() on the other
209c0e85e96SFrançois Tigeot  * hand throws such guarantees into the wind and calls disable hooks
210c0e85e96SFrançois Tigeot  * unconditionally on unused functions.
211ba55f2f5SFrançois Tigeot  */
drm_helper_disable_unused_functions(struct drm_device * dev)212ba55f2f5SFrançois Tigeot void drm_helper_disable_unused_functions(struct drm_device *dev)
213ba55f2f5SFrançois Tigeot {
2141dedbd3bSFrançois Tigeot 	if (drm_core_check_feature(dev, DRIVER_ATOMIC))
2151dedbd3bSFrançois Tigeot 		DRM_ERROR("Called for atomic driver, this is not what you want.\n");
2161dedbd3bSFrançois Tigeot 
217ba55f2f5SFrançois Tigeot 	drm_modeset_lock_all(dev);
218ba55f2f5SFrançois Tigeot 	__drm_helper_disable_unused_functions(dev);
219ba55f2f5SFrançois Tigeot 	drm_modeset_unlock_all(dev);
220ba55f2f5SFrançois Tigeot }
2216e29dde8SFrançois Tigeot EXPORT_SYMBOL(drm_helper_disable_unused_functions);
2225718399fSFrançois Tigeot 
2235718399fSFrançois Tigeot /*
2245718399fSFrançois Tigeot  * Check the CRTC we're going to map each output to vs. its current
2255718399fSFrançois Tigeot  * CRTC.  If they don't match, we have to disable the output and the CRTC
2265718399fSFrançois Tigeot  * since the driver will have to re-route things.
2275718399fSFrançois Tigeot  */
2285718399fSFrançois Tigeot static void
drm_crtc_prepare_encoders(struct drm_device * dev)2295718399fSFrançois Tigeot drm_crtc_prepare_encoders(struct drm_device *dev)
2305718399fSFrançois Tigeot {
231477eb7f9SFrançois Tigeot 	const struct drm_encoder_helper_funcs *encoder_funcs;
2325718399fSFrançois Tigeot 	struct drm_encoder *encoder;
2335718399fSFrançois Tigeot 
234a05eeebfSFrançois Tigeot 	drm_for_each_encoder(encoder, dev) {
2355718399fSFrançois Tigeot 		encoder_funcs = encoder->helper_private;
2368621f407SFrançois Tigeot 		if (!encoder_funcs)
2378621f407SFrançois Tigeot 			continue;
2388621f407SFrançois Tigeot 
2395718399fSFrançois Tigeot 		/* Disable unused encoders */
2405718399fSFrançois Tigeot 		if (encoder->crtc == NULL)
2415718399fSFrançois Tigeot 			drm_encoder_disable(encoder);
2425718399fSFrançois Tigeot 		/* Disable encoders whose CRTC is about to change */
2435718399fSFrançois Tigeot 		if (encoder_funcs->get_crtc &&
2445718399fSFrançois Tigeot 		    encoder->crtc != (*encoder_funcs->get_crtc)(encoder))
2455718399fSFrançois Tigeot 			drm_encoder_disable(encoder);
2465718399fSFrançois Tigeot 	}
2475718399fSFrançois Tigeot }
2485718399fSFrançois Tigeot 
2495718399fSFrançois Tigeot /**
2506e29dde8SFrançois Tigeot  * drm_crtc_helper_set_mode - internal helper to set a mode
2515718399fSFrançois Tigeot  * @crtc: CRTC to program
2525718399fSFrançois Tigeot  * @mode: mode to use
2536e29dde8SFrançois Tigeot  * @x: horizontal offset into the surface
2546e29dde8SFrançois Tigeot  * @y: vertical offset into the surface
2556e29dde8SFrançois Tigeot  * @old_fb: old framebuffer, for cleanup
2565718399fSFrançois Tigeot  *
2575718399fSFrançois Tigeot  * Try to set @mode on @crtc.  Give @crtc and its associated connectors a chance
2586e29dde8SFrançois Tigeot  * to fixup or reject the mode prior to trying to set it. This is an internal
2596e29dde8SFrançois Tigeot  * helper that drivers could e.g. use to update properties that require the
2606e29dde8SFrançois Tigeot  * entire output pipe to be disabled and re-enabled in a new configuration. For
2616e29dde8SFrançois Tigeot  * example for changing whether audio is enabled on a hdmi link or for changing
2626e29dde8SFrançois Tigeot  * panel fitter or dither attributes. It is also called by the
2636e29dde8SFrançois Tigeot  * drm_crtc_helper_set_config() helper function to drive the mode setting
2646e29dde8SFrançois Tigeot  * sequence.
2655718399fSFrançois Tigeot  *
266ba55f2f5SFrançois Tigeot  * Returns:
267ba55f2f5SFrançois Tigeot  * True if the mode was set successfully, false otherwise.
2685718399fSFrançois Tigeot  */
drm_crtc_helper_set_mode(struct drm_crtc * crtc,struct drm_display_mode * mode,int x,int y,struct drm_framebuffer * old_fb)2695718399fSFrançois Tigeot bool drm_crtc_helper_set_mode(struct drm_crtc *crtc,
2705718399fSFrançois Tigeot 			      struct drm_display_mode *mode,
2715718399fSFrançois Tigeot 			      int x, int y,
2725718399fSFrançois Tigeot 			      struct drm_framebuffer *old_fb)
2735718399fSFrançois Tigeot {
2745718399fSFrançois Tigeot 	struct drm_device *dev = crtc->dev;
275477eb7f9SFrançois Tigeot 	struct drm_display_mode *adjusted_mode, saved_mode, saved_hwmode;
276477eb7f9SFrançois Tigeot 	const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
277477eb7f9SFrançois Tigeot 	const struct drm_encoder_helper_funcs *encoder_funcs;
2785718399fSFrançois Tigeot 	int saved_x, saved_y;
2799edbd4a0SFrançois Tigeot 	bool saved_enabled;
2805718399fSFrançois Tigeot 	struct drm_encoder *encoder;
2815718399fSFrançois Tigeot 	bool ret = true;
2825718399fSFrançois Tigeot 
283ba55f2f5SFrançois Tigeot 	drm_warn_on_modeset_not_all_locked(dev);
284ba55f2f5SFrançois Tigeot 
2859edbd4a0SFrançois Tigeot 	saved_enabled = crtc->enabled;
2865718399fSFrançois Tigeot 	crtc->enabled = drm_helper_crtc_in_use(crtc);
2875718399fSFrançois Tigeot 	if (!crtc->enabled)
2885718399fSFrançois Tigeot 		return true;
2895718399fSFrançois Tigeot 
2905718399fSFrançois Tigeot 	adjusted_mode = drm_mode_duplicate(dev, mode);
2919edbd4a0SFrançois Tigeot 	if (!adjusted_mode) {
2929edbd4a0SFrançois Tigeot 		crtc->enabled = saved_enabled;
2935718399fSFrançois Tigeot 		return false;
2949edbd4a0SFrançois Tigeot 	}
2955718399fSFrançois Tigeot 
2965718399fSFrançois Tigeot 	saved_mode = crtc->mode;
297477eb7f9SFrançois Tigeot 	saved_hwmode = crtc->hwmode;
2985718399fSFrançois Tigeot 	saved_x = crtc->x;
2995718399fSFrançois Tigeot 	saved_y = crtc->y;
3005718399fSFrançois Tigeot 
3015718399fSFrançois Tigeot 	/* Update crtc values up front so the driver can rely on them for mode
3025718399fSFrançois Tigeot 	 * setting.
3035718399fSFrançois Tigeot 	 */
3045718399fSFrançois Tigeot 	crtc->mode = *mode;
3055718399fSFrançois Tigeot 	crtc->x = x;
3065718399fSFrançois Tigeot 	crtc->y = y;
3075718399fSFrançois Tigeot 
3085718399fSFrançois Tigeot 	/* Pass our mode to the connectors and the CRTC to give them a chance to
3095718399fSFrançois Tigeot 	 * adjust it according to limitations or connector properties, and also
3105718399fSFrançois Tigeot 	 * a chance to reject the mode entirely.
3115718399fSFrançois Tigeot 	 */
312a05eeebfSFrançois Tigeot 	drm_for_each_encoder(encoder, dev) {
3135718399fSFrançois Tigeot 
3145718399fSFrançois Tigeot 		if (encoder->crtc != crtc)
3155718399fSFrançois Tigeot 			continue;
316ba55f2f5SFrançois Tigeot 
3178621f407SFrançois Tigeot 		encoder_funcs = encoder->helper_private;
3188621f407SFrançois Tigeot 		if (!encoder_funcs)
3198621f407SFrançois Tigeot 			continue;
3208621f407SFrançois Tigeot 
32119c468b4SFrançois Tigeot 		ret = drm_bridge_mode_fixup(encoder->bridge,
32219c468b4SFrançois Tigeot 			mode, adjusted_mode);
323ba55f2f5SFrançois Tigeot 		if (!ret) {
324ba55f2f5SFrançois Tigeot 			DRM_DEBUG_KMS("Bridge fixup failed\n");
325ba55f2f5SFrançois Tigeot 			goto done;
326ba55f2f5SFrançois Tigeot 		}
327ba55f2f5SFrançois Tigeot 
3285718399fSFrançois Tigeot 		encoder_funcs = encoder->helper_private;
329c0e85e96SFrançois Tigeot 		if (encoder_funcs->mode_fixup) {
3305718399fSFrançois Tigeot 			if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
3315718399fSFrançois Tigeot 							      adjusted_mode))) {
3326e29dde8SFrançois Tigeot 				DRM_DEBUG_KMS("Encoder fixup failed\n");
3335718399fSFrançois Tigeot 				goto done;
3345718399fSFrançois Tigeot 			}
3355718399fSFrançois Tigeot 		}
336c0e85e96SFrançois Tigeot 	}
3375718399fSFrançois Tigeot 
338c0e85e96SFrançois Tigeot 	if (crtc_funcs->mode_fixup) {
339c0e85e96SFrançois Tigeot 		if (!(ret = crtc_funcs->mode_fixup(crtc, mode,
340c0e85e96SFrançois Tigeot 						adjusted_mode))) {
3416e29dde8SFrançois Tigeot 			DRM_DEBUG_KMS("CRTC fixup failed\n");
3425718399fSFrançois Tigeot 			goto done;
3435718399fSFrançois Tigeot 		}
344c0e85e96SFrançois Tigeot 	}
345aee94f86SFrançois Tigeot 	DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
3465718399fSFrançois Tigeot 
347477eb7f9SFrançois Tigeot 	crtc->hwmode = *adjusted_mode;
348477eb7f9SFrançois Tigeot 
3495718399fSFrançois Tigeot 	/* Prepare the encoders and CRTCs before setting the mode. */
350a05eeebfSFrançois Tigeot 	drm_for_each_encoder(encoder, dev) {
3515718399fSFrançois Tigeot 
3525718399fSFrançois Tigeot 		if (encoder->crtc != crtc)
3535718399fSFrançois Tigeot 			continue;
354ba55f2f5SFrançois Tigeot 
3558621f407SFrançois Tigeot 		encoder_funcs = encoder->helper_private;
3568621f407SFrançois Tigeot 		if (!encoder_funcs)
3578621f407SFrançois Tigeot 			continue;
3588621f407SFrançois Tigeot 
35919c468b4SFrançois Tigeot 		drm_bridge_disable(encoder->bridge);
360ba55f2f5SFrançois Tigeot 
3615718399fSFrançois Tigeot 		/* Disable the encoders as the first thing we do. */
3628621f407SFrançois Tigeot 		if (encoder_funcs->prepare)
3635718399fSFrançois Tigeot 			encoder_funcs->prepare(encoder);
364ba55f2f5SFrançois Tigeot 
36519c468b4SFrançois Tigeot 		drm_bridge_post_disable(encoder->bridge);
3665718399fSFrançois Tigeot 	}
3675718399fSFrançois Tigeot 
3685718399fSFrançois Tigeot 	drm_crtc_prepare_encoders(dev);
3695718399fSFrançois Tigeot 
3705718399fSFrançois Tigeot 	crtc_funcs->prepare(crtc);
3715718399fSFrançois Tigeot 
3725718399fSFrançois Tigeot 	/* Set up the DPLL and any encoders state that needs to adjust or depend
3735718399fSFrançois Tigeot 	 * on the DPLL.
3745718399fSFrançois Tigeot 	 */
3755718399fSFrançois Tigeot 	ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
3765718399fSFrançois Tigeot 	if (!ret)
3775718399fSFrançois Tigeot 	    goto done;
3785718399fSFrançois Tigeot 
379a05eeebfSFrançois Tigeot 	drm_for_each_encoder(encoder, dev) {
3805718399fSFrançois Tigeot 
3815718399fSFrançois Tigeot 		if (encoder->crtc != crtc)
3825718399fSFrançois Tigeot 			continue;
3835718399fSFrançois Tigeot 
3848621f407SFrançois Tigeot 		encoder_funcs = encoder->helper_private;
3858621f407SFrançois Tigeot 		if (!encoder_funcs)
3868621f407SFrançois Tigeot 			continue;
3878621f407SFrançois Tigeot 
3885718399fSFrançois Tigeot 		DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
389ba55f2f5SFrançois Tigeot 			encoder->base.id, encoder->name,
3905718399fSFrançois Tigeot 			mode->base.id, mode->name);
3918621f407SFrançois Tigeot 		if (encoder_funcs->mode_set)
3925718399fSFrançois Tigeot 			encoder_funcs->mode_set(encoder, mode, adjusted_mode);
393ba55f2f5SFrançois Tigeot 
39419c468b4SFrançois Tigeot 		drm_bridge_mode_set(encoder->bridge, mode, adjusted_mode);
3955718399fSFrançois Tigeot 	}
3965718399fSFrançois Tigeot 
3975718399fSFrançois Tigeot 	/* Now enable the clocks, plane, pipe, and connectors that we set up. */
3985718399fSFrançois Tigeot 	crtc_funcs->commit(crtc);
3995718399fSFrançois Tigeot 
400a05eeebfSFrançois Tigeot 	drm_for_each_encoder(encoder, dev) {
4015718399fSFrançois Tigeot 
4025718399fSFrançois Tigeot 		if (encoder->crtc != crtc)
4035718399fSFrançois Tigeot 			continue;
4045718399fSFrançois Tigeot 
4058621f407SFrançois Tigeot 		encoder_funcs = encoder->helper_private;
4068621f407SFrançois Tigeot 		if (!encoder_funcs)
4078621f407SFrançois Tigeot 			continue;
4088621f407SFrançois Tigeot 
40919c468b4SFrançois Tigeot 		drm_bridge_pre_enable(encoder->bridge);
410ba55f2f5SFrançois Tigeot 
4118621f407SFrançois Tigeot 		if (encoder_funcs->commit)
4125718399fSFrançois Tigeot 			encoder_funcs->commit(encoder);
4135718399fSFrançois Tigeot 
41419c468b4SFrançois Tigeot 		drm_bridge_enable(encoder->bridge);
4155718399fSFrançois Tigeot 	}
4165718399fSFrançois Tigeot 
4175718399fSFrançois Tigeot 	/* Calculate and store various constants which
4185718399fSFrançois Tigeot 	 * are later needed by vblank and swap-completion
4195718399fSFrançois Tigeot 	 * timestamping. They are derived from true hwmode.
4205718399fSFrançois Tigeot 	 */
4219edbd4a0SFrançois Tigeot 	drm_calc_timestamping_constants(crtc, &crtc->hwmode);
4225718399fSFrançois Tigeot 
4235718399fSFrançois Tigeot 	/* FIXME: add subpixel order */
4245718399fSFrançois Tigeot done:
4255718399fSFrançois Tigeot 	drm_mode_destroy(dev, adjusted_mode);
4265718399fSFrançois Tigeot 	if (!ret) {
4279edbd4a0SFrançois Tigeot 		crtc->enabled = saved_enabled;
4285718399fSFrançois Tigeot 		crtc->mode = saved_mode;
429477eb7f9SFrançois Tigeot 		crtc->hwmode = saved_hwmode;
4305718399fSFrançois Tigeot 		crtc->x = saved_x;
4315718399fSFrançois Tigeot 		crtc->y = saved_y;
4325718399fSFrançois Tigeot 	}
4335718399fSFrançois Tigeot 
4345718399fSFrançois Tigeot 	return ret;
4355718399fSFrançois Tigeot }
4366e29dde8SFrançois Tigeot EXPORT_SYMBOL(drm_crtc_helper_set_mode);
4376e29dde8SFrançois Tigeot 
438ba55f2f5SFrançois Tigeot static void
drm_crtc_helper_disable(struct drm_crtc * crtc)4395718399fSFrançois Tigeot drm_crtc_helper_disable(struct drm_crtc *crtc)
4405718399fSFrançois Tigeot {
4415718399fSFrançois Tigeot 	struct drm_device *dev = crtc->dev;
4425718399fSFrançois Tigeot 	struct drm_connector *connector;
4435718399fSFrançois Tigeot 	struct drm_encoder *encoder;
4445718399fSFrançois Tigeot 
4455718399fSFrançois Tigeot 	/* Decouple all encoders and their attached connectors from this crtc */
446a05eeebfSFrançois Tigeot 	drm_for_each_encoder(encoder, dev) {
447a85cb24fSFrançois Tigeot 		struct drm_connector_list_iter conn_iter;
448a85cb24fSFrançois Tigeot 
4495718399fSFrançois Tigeot 		if (encoder->crtc != crtc)
4505718399fSFrançois Tigeot 			continue;
4515718399fSFrançois Tigeot 
452a85cb24fSFrançois Tigeot 		drm_connector_list_iter_begin(dev, &conn_iter);
453a85cb24fSFrançois Tigeot 		drm_for_each_connector_iter(connector, &conn_iter) {
4545718399fSFrançois Tigeot 			if (connector->encoder != encoder)
4555718399fSFrançois Tigeot 				continue;
4565718399fSFrançois Tigeot 
4575718399fSFrançois Tigeot 			connector->encoder = NULL;
4589edbd4a0SFrançois Tigeot 
4599edbd4a0SFrançois Tigeot 			/*
4609edbd4a0SFrançois Tigeot 			 * drm_helper_disable_unused_functions() ought to be
4619edbd4a0SFrançois Tigeot 			 * doing this, but since we've decoupled the encoder
4629edbd4a0SFrançois Tigeot 			 * from the connector above, the required connection
4639edbd4a0SFrançois Tigeot 			 * between them is henceforth no longer available.
4649edbd4a0SFrançois Tigeot 			 */
4659edbd4a0SFrançois Tigeot 			connector->dpms = DRM_MODE_DPMS_OFF;
4668621f407SFrançois Tigeot 
4678621f407SFrançois Tigeot 			/* we keep a reference while the encoder is bound */
468a85cb24fSFrançois Tigeot 			drm_connector_put(connector);
4695718399fSFrançois Tigeot 		}
470a85cb24fSFrançois Tigeot 		drm_connector_list_iter_end(&conn_iter);
4715718399fSFrançois Tigeot 	}
4725718399fSFrançois Tigeot 
473ba55f2f5SFrançois Tigeot 	__drm_helper_disable_unused_functions(dev);
4745718399fSFrançois Tigeot }
4755718399fSFrançois Tigeot 
4765718399fSFrançois Tigeot /**
4775718399fSFrançois Tigeot  * drm_crtc_helper_set_config - set a new config from userspace
4786e29dde8SFrançois Tigeot  * @set: mode set configuration
479a85cb24fSFrançois Tigeot  * @ctx: lock acquire context, not used here
4805718399fSFrançois Tigeot  *
481*3f2dd94aSFrançois Tigeot  * The drm_crtc_helper_set_config() helper function implements the of
482*3f2dd94aSFrançois Tigeot  * &drm_crtc_funcs.set_config callback for drivers using the legacy CRTC
483*3f2dd94aSFrançois Tigeot  * helpers.
484aee94f86SFrançois Tigeot  *
485aee94f86SFrançois Tigeot  * It first tries to locate the best encoder for each connector by calling the
486*3f2dd94aSFrançois Tigeot  * connector @drm_connector_helper_funcs.best_encoder helper operation.
487aee94f86SFrançois Tigeot  *
488aee94f86SFrançois Tigeot  * After locating the appropriate encoders, the helper function will call the
489aee94f86SFrançois Tigeot  * mode_fixup encoder and CRTC helper operations to adjust the requested mode,
490aee94f86SFrançois Tigeot  * or reject it completely in which case an error will be returned to the
491aee94f86SFrançois Tigeot  * application. If the new configuration after mode adjustment is identical to
492aee94f86SFrançois Tigeot  * the current configuration the helper function will return without performing
493aee94f86SFrançois Tigeot  * any other operation.
494aee94f86SFrançois Tigeot  *
495aee94f86SFrançois Tigeot  * If the adjusted mode is identical to the current mode but changes to the
496aee94f86SFrançois Tigeot  * frame buffer need to be applied, the drm_crtc_helper_set_config() function
497*3f2dd94aSFrançois Tigeot  * will call the CRTC &drm_crtc_helper_funcs.mode_set_base helper operation.
498aee94f86SFrançois Tigeot  *
499aee94f86SFrançois Tigeot  * If the adjusted mode differs from the current mode, or if the
500aee94f86SFrançois Tigeot  * ->mode_set_base() helper operation is not provided, the helper function
501aee94f86SFrançois Tigeot  * performs a full mode set sequence by calling the ->prepare(), ->mode_set()
502aee94f86SFrançois Tigeot  * and ->commit() CRTC and encoder helper operations, in that order.
503aee94f86SFrançois Tigeot  * Alternatively it can also use the dpms and disable helper operations. For
504*3f2dd94aSFrançois Tigeot  * details see &struct drm_crtc_helper_funcs and struct
505aee94f86SFrançois Tigeot  * &drm_encoder_helper_funcs.
506aee94f86SFrançois Tigeot  *
507aee94f86SFrançois Tigeot  * This function is deprecated.  New drivers must implement atomic modeset
508aee94f86SFrançois Tigeot  * support, for which this function is unsuitable. Instead drivers should use
509aee94f86SFrançois Tigeot  * drm_atomic_helper_set_config().
5105718399fSFrançois Tigeot  *
511ba55f2f5SFrançois Tigeot  * Returns:
512ba55f2f5SFrançois Tigeot  * Returns 0 on success, negative errno numbers on failure.
5135718399fSFrançois Tigeot  */
drm_crtc_helper_set_config(struct drm_mode_set * set,struct drm_modeset_acquire_ctx * ctx)514a85cb24fSFrançois Tigeot int drm_crtc_helper_set_config(struct drm_mode_set *set,
515a85cb24fSFrançois Tigeot 			       struct drm_modeset_acquire_ctx *ctx)
5165718399fSFrançois Tigeot {
5175718399fSFrançois Tigeot 	struct drm_device *dev;
5188621f407SFrançois Tigeot 	struct drm_crtc **save_encoder_crtcs, *new_crtc;
5198621f407SFrançois Tigeot 	struct drm_encoder **save_connector_encoders, *new_encoder, *encoder;
5205718399fSFrançois Tigeot 	bool mode_changed = false; /* if true do a full mode set */
5215718399fSFrançois Tigeot 	bool fb_changed = false; /* if true and !mode_changed just do a flip */
5228621f407SFrançois Tigeot 	struct drm_connector *connector;
523a85cb24fSFrançois Tigeot 	struct drm_connector_list_iter conn_iter;
5245718399fSFrançois Tigeot 	int count = 0, ro, fail = 0;
525477eb7f9SFrançois Tigeot 	const struct drm_crtc_helper_funcs *crtc_funcs;
5265718399fSFrançois Tigeot 	struct drm_mode_set save_set;
5276e29dde8SFrançois Tigeot 	int ret;
5285718399fSFrançois Tigeot 	int i;
5295718399fSFrançois Tigeot 
5305718399fSFrançois Tigeot 	DRM_DEBUG_KMS("\n");
5315718399fSFrançois Tigeot 
5329edbd4a0SFrançois Tigeot 	BUG_ON(!set);
5339edbd4a0SFrançois Tigeot 	BUG_ON(!set->crtc);
5349edbd4a0SFrançois Tigeot 	BUG_ON(!set->crtc->helper_private);
5355718399fSFrançois Tigeot 
5369edbd4a0SFrançois Tigeot 	/* Enforce sane interface api - has been abused by the fb helper. */
5379edbd4a0SFrançois Tigeot 	BUG_ON(!set->mode && set->fb);
5389edbd4a0SFrançois Tigeot 	BUG_ON(set->fb && set->num_connectors == 0);
5395718399fSFrançois Tigeot 
5405718399fSFrançois Tigeot 	crtc_funcs = set->crtc->helper_private;
5415718399fSFrançois Tigeot 
5425718399fSFrançois Tigeot 	if (!set->mode)
5435718399fSFrançois Tigeot 		set->fb = NULL;
5445718399fSFrançois Tigeot 
5455718399fSFrançois Tigeot 	if (set->fb) {
546aee94f86SFrançois Tigeot 		DRM_DEBUG_KMS("[CRTC:%d:%s] [FB:%d] #connectors=%d (x y) (%i %i)\n",
547aee94f86SFrançois Tigeot 			      set->crtc->base.id, set->crtc->name,
548aee94f86SFrançois Tigeot 			      set->fb->base.id,
5495718399fSFrançois Tigeot 			      (int)set->num_connectors, set->x, set->y);
5505718399fSFrançois Tigeot 	} else {
551aee94f86SFrançois Tigeot 		DRM_DEBUG_KMS("[CRTC:%d:%s] [NOFB]\n",
552aee94f86SFrançois Tigeot 			      set->crtc->base.id, set->crtc->name);
553ba55f2f5SFrançois Tigeot 		drm_crtc_helper_disable(set->crtc);
554ba55f2f5SFrançois Tigeot 		return 0;
5555718399fSFrançois Tigeot 	}
5565718399fSFrançois Tigeot 
5575718399fSFrançois Tigeot 	dev = set->crtc->dev;
5585718399fSFrançois Tigeot 
559ba55f2f5SFrançois Tigeot 	drm_warn_on_modeset_not_all_locked(dev);
560ba55f2f5SFrançois Tigeot 
5619edbd4a0SFrançois Tigeot 	/*
5629edbd4a0SFrançois Tigeot 	 * Allocate space for the backup of all (non-pointer) encoder and
5639edbd4a0SFrançois Tigeot 	 * connector data.
5649edbd4a0SFrançois Tigeot 	 */
565*3f2dd94aSFrançois Tigeot 	save_encoder_crtcs = kcalloc(dev->mode_config.num_encoder,
5668621f407SFrançois Tigeot 				sizeof(struct drm_crtc *), GFP_KERNEL);
5678621f407SFrançois Tigeot 	if (!save_encoder_crtcs)
5684dbb207bSFrançois Tigeot 		return -ENOMEM;
5694dbb207bSFrançois Tigeot 
570*3f2dd94aSFrançois Tigeot 	save_connector_encoders = kcalloc(dev->mode_config.num_connector,
5718621f407SFrançois Tigeot 				sizeof(struct drm_encoder *), GFP_KERNEL);
5728621f407SFrançois Tigeot 	if (!save_connector_encoders) {
5738621f407SFrançois Tigeot 		kfree(save_encoder_crtcs);
5744dbb207bSFrançois Tigeot 		return -ENOMEM;
5754dbb207bSFrançois Tigeot 	}
5765718399fSFrançois Tigeot 
5779edbd4a0SFrançois Tigeot 	/*
5789edbd4a0SFrançois Tigeot 	 * Copy data. Note that driver private data is not affected.
5795718399fSFrançois Tigeot 	 * Should anything bad happen only the expected state is
5805718399fSFrançois Tigeot 	 * restored, not the drivers personal bookkeeping.
5815718399fSFrançois Tigeot 	 */
5825718399fSFrançois Tigeot 	count = 0;
583a05eeebfSFrançois Tigeot 	drm_for_each_encoder(encoder, dev) {
5848621f407SFrançois Tigeot 		save_encoder_crtcs[count++] = encoder->crtc;
5855718399fSFrançois Tigeot 	}
5865718399fSFrançois Tigeot 
5875718399fSFrançois Tigeot 	count = 0;
588a85cb24fSFrançois Tigeot 	drm_connector_list_iter_begin(dev, &conn_iter);
589a85cb24fSFrançois Tigeot 	drm_for_each_connector_iter(connector, &conn_iter)
5908621f407SFrançois Tigeot 		save_connector_encoders[count++] = connector->encoder;
591a85cb24fSFrançois Tigeot 	drm_connector_list_iter_end(&conn_iter);
5925718399fSFrançois Tigeot 
5935718399fSFrançois Tigeot 	save_set.crtc = set->crtc;
5945718399fSFrançois Tigeot 	save_set.mode = &set->crtc->mode;
5955718399fSFrançois Tigeot 	save_set.x = set->crtc->x;
5965718399fSFrançois Tigeot 	save_set.y = set->crtc->y;
597ba55f2f5SFrançois Tigeot 	save_set.fb = set->crtc->primary->fb;
5985718399fSFrançois Tigeot 
5995718399fSFrançois Tigeot 	/* We should be able to check here if the fb has the same properties
6005718399fSFrançois Tigeot 	 * and then just flip_or_move it */
601ba55f2f5SFrançois Tigeot 	if (set->crtc->primary->fb != set->fb) {
6025718399fSFrançois Tigeot 		/* If we have no fb then treat it as a full mode set */
603ba55f2f5SFrançois Tigeot 		if (set->crtc->primary->fb == NULL) {
6045718399fSFrançois Tigeot 			DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
6055718399fSFrançois Tigeot 			mode_changed = true;
606a85cb24fSFrançois Tigeot 		} else if (set->fb->format != set->crtc->primary->fb->format) {
6074dbb207bSFrançois Tigeot 			mode_changed = true;
6085718399fSFrançois Tigeot 		} else
6095718399fSFrançois Tigeot 			fb_changed = true;
6105718399fSFrançois Tigeot 	}
6115718399fSFrançois Tigeot 
6125718399fSFrançois Tigeot 	if (set->x != set->crtc->x || set->y != set->crtc->y)
6135718399fSFrançois Tigeot 		fb_changed = true;
6145718399fSFrançois Tigeot 
615c0e85e96SFrançois Tigeot 	if (!drm_mode_equal(set->mode, &set->crtc->mode)) {
6165718399fSFrançois Tigeot 		DRM_DEBUG_KMS("modes are different, full mode set\n");
6175718399fSFrançois Tigeot 		drm_mode_debug_printmodeline(&set->crtc->mode);
6185718399fSFrançois Tigeot 		drm_mode_debug_printmodeline(set->mode);
6195718399fSFrançois Tigeot 		mode_changed = true;
6205718399fSFrançois Tigeot 	}
6215718399fSFrançois Tigeot 
6228621f407SFrançois Tigeot 	/* take a reference on all unbound connectors in set, reuse the
6238621f407SFrançois Tigeot 	 * already taken reference for bound connectors
6248621f407SFrançois Tigeot 	 */
6258621f407SFrançois Tigeot 	for (ro = 0; ro < set->num_connectors; ro++) {
6268621f407SFrançois Tigeot 		if (set->connectors[ro]->encoder)
6278621f407SFrançois Tigeot 			continue;
628a85cb24fSFrançois Tigeot 		drm_connector_get(set->connectors[ro]);
6298621f407SFrançois Tigeot 	}
6308621f407SFrançois Tigeot 
6315718399fSFrançois Tigeot 	/* a) traverse passed in connector list and get encoders for them */
6325718399fSFrançois Tigeot 	count = 0;
633a85cb24fSFrançois Tigeot 	drm_connector_list_iter_begin(dev, &conn_iter);
634a85cb24fSFrançois Tigeot 	drm_for_each_connector_iter(connector, &conn_iter) {
635477eb7f9SFrançois Tigeot 		const struct drm_connector_helper_funcs *connector_funcs =
6365718399fSFrançois Tigeot 			connector->helper_private;
6375718399fSFrançois Tigeot 		new_encoder = connector->encoder;
6385718399fSFrançois Tigeot 		for (ro = 0; ro < set->num_connectors; ro++) {
6395718399fSFrançois Tigeot 			if (set->connectors[ro] == connector) {
6405718399fSFrançois Tigeot 				new_encoder = connector_funcs->best_encoder(connector);
6415718399fSFrançois Tigeot 				/* if we can't get an encoder for a connector
6425718399fSFrançois Tigeot 				   we are setting now - then fail */
6435718399fSFrançois Tigeot 				if (new_encoder == NULL)
6445718399fSFrançois Tigeot 					/* don't break so fail path works correct */
6455718399fSFrançois Tigeot 					fail = 1;
6469edbd4a0SFrançois Tigeot 
6479edbd4a0SFrançois Tigeot 				if (connector->dpms != DRM_MODE_DPMS_ON) {
6489edbd4a0SFrançois Tigeot 					DRM_DEBUG_KMS("connector dpms not on, full mode switch\n");
6499edbd4a0SFrançois Tigeot 					mode_changed = true;
6509edbd4a0SFrançois Tigeot 				}
651ba55f2f5SFrançois Tigeot 
652ba55f2f5SFrançois Tigeot 				break;
6535718399fSFrançois Tigeot 			}
6545718399fSFrançois Tigeot 		}
6555718399fSFrançois Tigeot 
6565718399fSFrançois Tigeot 		if (new_encoder != connector->encoder) {
6575718399fSFrançois Tigeot 			DRM_DEBUG_KMS("encoder changed, full mode switch\n");
6585718399fSFrançois Tigeot 			mode_changed = true;
6595718399fSFrançois Tigeot 			/* If the encoder is reused for another connector, then
6605718399fSFrançois Tigeot 			 * the appropriate crtc will be set later.
6615718399fSFrançois Tigeot 			 */
6625718399fSFrançois Tigeot 			if (connector->encoder)
6635718399fSFrançois Tigeot 				connector->encoder->crtc = NULL;
6645718399fSFrançois Tigeot 			connector->encoder = new_encoder;
6655718399fSFrançois Tigeot 		}
6665718399fSFrançois Tigeot 	}
667a85cb24fSFrançois Tigeot 	drm_connector_list_iter_end(&conn_iter);
6685718399fSFrançois Tigeot 
6695718399fSFrançois Tigeot 	if (fail) {
6705718399fSFrançois Tigeot 		ret = -EINVAL;
6715718399fSFrançois Tigeot 		goto fail;
6725718399fSFrançois Tigeot 	}
6735718399fSFrançois Tigeot 
6745718399fSFrançois Tigeot 	count = 0;
675a85cb24fSFrançois Tigeot 	drm_connector_list_iter_begin(dev, &conn_iter);
676a85cb24fSFrançois Tigeot 	drm_for_each_connector_iter(connector, &conn_iter) {
6775718399fSFrançois Tigeot 		if (!connector->encoder)
6785718399fSFrançois Tigeot 			continue;
6795718399fSFrançois Tigeot 
6805718399fSFrançois Tigeot 		if (connector->encoder->crtc == set->crtc)
6815718399fSFrançois Tigeot 			new_crtc = NULL;
6825718399fSFrançois Tigeot 		else
6835718399fSFrançois Tigeot 			new_crtc = connector->encoder->crtc;
6845718399fSFrançois Tigeot 
6855718399fSFrançois Tigeot 		for (ro = 0; ro < set->num_connectors; ro++) {
6865718399fSFrançois Tigeot 			if (set->connectors[ro] == connector)
6875718399fSFrançois Tigeot 				new_crtc = set->crtc;
6885718399fSFrançois Tigeot 		}
6895718399fSFrançois Tigeot 
6905718399fSFrançois Tigeot 		/* Make sure the new CRTC will work with the encoder */
6915718399fSFrançois Tigeot 		if (new_crtc &&
6925718399fSFrançois Tigeot 		    !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
6935718399fSFrançois Tigeot 			ret = -EINVAL;
694a85cb24fSFrançois Tigeot 			drm_connector_list_iter_end(&conn_iter);
6955718399fSFrançois Tigeot 			goto fail;
6965718399fSFrançois Tigeot 		}
6975718399fSFrançois Tigeot 		if (new_crtc != connector->encoder->crtc) {
6985718399fSFrançois Tigeot 			DRM_DEBUG_KMS("crtc changed, full mode switch\n");
6995718399fSFrançois Tigeot 			mode_changed = true;
7005718399fSFrançois Tigeot 			connector->encoder->crtc = new_crtc;
7015718399fSFrançois Tigeot 		}
7025718399fSFrançois Tigeot 		if (new_crtc) {
703aee94f86SFrançois Tigeot 			DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d:%s]\n",
704ba55f2f5SFrançois Tigeot 				      connector->base.id, connector->name,
705aee94f86SFrançois Tigeot 				      new_crtc->base.id, new_crtc->name);
7065718399fSFrançois Tigeot 		} else {
7075718399fSFrançois Tigeot 			DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
708ba55f2f5SFrançois Tigeot 				      connector->base.id, connector->name);
7095718399fSFrançois Tigeot 		}
7105718399fSFrançois Tigeot 	}
711a85cb24fSFrançois Tigeot 	drm_connector_list_iter_end(&conn_iter);
7125718399fSFrançois Tigeot 
7135718399fSFrançois Tigeot 	/* mode_set_base is not a required function */
7145718399fSFrançois Tigeot 	if (fb_changed && !crtc_funcs->mode_set_base)
7155718399fSFrançois Tigeot 		mode_changed = true;
7165718399fSFrançois Tigeot 
7175718399fSFrançois Tigeot 	if (mode_changed) {
7189edbd4a0SFrançois Tigeot 		if (drm_helper_crtc_in_use(set->crtc)) {
7195718399fSFrançois Tigeot 			DRM_DEBUG_KMS("attempting to set mode from"
7205718399fSFrançois Tigeot 					" userspace\n");
7215718399fSFrançois Tigeot 			drm_mode_debug_printmodeline(set->mode);
722ba55f2f5SFrançois Tigeot 			set->crtc->primary->fb = set->fb;
7235718399fSFrançois Tigeot 			if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
7245718399fSFrançois Tigeot 						      set->x, set->y,
7259edbd4a0SFrançois Tigeot 						      save_set.fb)) {
726aee94f86SFrançois Tigeot 				DRM_ERROR("failed to set mode on [CRTC:%d:%s]\n",
727aee94f86SFrançois Tigeot 					  set->crtc->base.id, set->crtc->name);
728ba55f2f5SFrançois Tigeot 				set->crtc->primary->fb = save_set.fb;
7295718399fSFrançois Tigeot 				ret = -EINVAL;
7305718399fSFrançois Tigeot 				goto fail;
7315718399fSFrançois Tigeot 			}
7325718399fSFrançois Tigeot 			DRM_DEBUG_KMS("Setting connector DPMS state to on\n");
7335718399fSFrançois Tigeot 			for (i = 0; i < set->num_connectors; i++) {
7345718399fSFrançois Tigeot 				DRM_DEBUG_KMS("\t[CONNECTOR:%d:%s] set DPMS on\n", set->connectors[i]->base.id,
735ba55f2f5SFrançois Tigeot 					      set->connectors[i]->name);
7366e29dde8SFrançois Tigeot 				set->connectors[i]->funcs->dpms(set->connectors[i], DRM_MODE_DPMS_ON);
7375718399fSFrançois Tigeot 			}
7385718399fSFrançois Tigeot 		}
739ba55f2f5SFrançois Tigeot 		__drm_helper_disable_unused_functions(dev);
7405718399fSFrançois Tigeot 	} else if (fb_changed) {
7415718399fSFrançois Tigeot 		set->crtc->x = set->x;
7425718399fSFrançois Tigeot 		set->crtc->y = set->y;
743ba55f2f5SFrançois Tigeot 		set->crtc->primary->fb = set->fb;
7445718399fSFrançois Tigeot 		ret = crtc_funcs->mode_set_base(set->crtc,
7459edbd4a0SFrançois Tigeot 						set->x, set->y, save_set.fb);
7465718399fSFrançois Tigeot 		if (ret != 0) {
7479edbd4a0SFrançois Tigeot 			set->crtc->x = save_set.x;
7489edbd4a0SFrançois Tigeot 			set->crtc->y = save_set.y;
749ba55f2f5SFrançois Tigeot 			set->crtc->primary->fb = save_set.fb;
7505718399fSFrançois Tigeot 			goto fail;
7515718399fSFrançois Tigeot 		}
7525718399fSFrançois Tigeot 	}
7535718399fSFrançois Tigeot 
7548621f407SFrançois Tigeot 	kfree(save_connector_encoders);
7558621f407SFrançois Tigeot 	kfree(save_encoder_crtcs);
7565718399fSFrançois Tigeot 	return 0;
7575718399fSFrançois Tigeot 
7585718399fSFrançois Tigeot fail:
7595718399fSFrançois Tigeot 	/* Restore all previous data. */
7605718399fSFrançois Tigeot 	count = 0;
761a05eeebfSFrançois Tigeot 	drm_for_each_encoder(encoder, dev) {
7628621f407SFrançois Tigeot 		encoder->crtc = save_encoder_crtcs[count++];
7635718399fSFrançois Tigeot 	}
7645718399fSFrançois Tigeot 
7655718399fSFrançois Tigeot 	count = 0;
766a85cb24fSFrançois Tigeot 	drm_connector_list_iter_begin(dev, &conn_iter);
767a85cb24fSFrançois Tigeot 	drm_for_each_connector_iter(connector, &conn_iter)
7688621f407SFrançois Tigeot 		connector->encoder = save_connector_encoders[count++];
769a85cb24fSFrançois Tigeot 	drm_connector_list_iter_end(&conn_iter);
7708621f407SFrançois Tigeot 
7718621f407SFrançois Tigeot 	/* after fail drop reference on all unbound connectors in set, let
7728621f407SFrançois Tigeot 	 * bound connectors keep their reference
7738621f407SFrançois Tigeot 	 */
7748621f407SFrançois Tigeot 	for (ro = 0; ro < set->num_connectors; ro++) {
7758621f407SFrançois Tigeot 		if (set->connectors[ro]->encoder)
7768621f407SFrançois Tigeot 			continue;
777a85cb24fSFrançois Tigeot 		drm_connector_put(set->connectors[ro]);
7785718399fSFrançois Tigeot 	}
7795718399fSFrançois Tigeot 
7805718399fSFrançois Tigeot 	/* Try to restore the config */
7815718399fSFrançois Tigeot 	if (mode_changed &&
7825718399fSFrançois Tigeot 	    !drm_crtc_helper_set_mode(save_set.crtc, save_set.mode, save_set.x,
7835718399fSFrançois Tigeot 				      save_set.y, save_set.fb))
7845718399fSFrançois Tigeot 		DRM_ERROR("failed to restore config after modeset failure\n");
7855718399fSFrançois Tigeot 
7868621f407SFrançois Tigeot 	kfree(save_connector_encoders);
7878621f407SFrançois Tigeot 	kfree(save_encoder_crtcs);
7885718399fSFrançois Tigeot 	return ret;
7895718399fSFrançois Tigeot }
7906e29dde8SFrançois Tigeot EXPORT_SYMBOL(drm_crtc_helper_set_config);
7915718399fSFrançois Tigeot 
drm_helper_choose_encoder_dpms(struct drm_encoder * encoder)7925718399fSFrançois Tigeot static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder)
7935718399fSFrançois Tigeot {
7945718399fSFrançois Tigeot 	int dpms = DRM_MODE_DPMS_OFF;
7955718399fSFrançois Tigeot 	struct drm_connector *connector;
796a85cb24fSFrançois Tigeot 	struct drm_connector_list_iter conn_iter;
7975718399fSFrançois Tigeot 	struct drm_device *dev = encoder->dev;
7985718399fSFrançois Tigeot 
799a85cb24fSFrançois Tigeot 	drm_connector_list_iter_begin(dev, &conn_iter);
800a85cb24fSFrançois Tigeot 	drm_for_each_connector_iter(connector, &conn_iter)
8015718399fSFrançois Tigeot 		if (connector->encoder == encoder)
8025718399fSFrançois Tigeot 			if (connector->dpms < dpms)
8035718399fSFrançois Tigeot 				dpms = connector->dpms;
804a85cb24fSFrançois Tigeot 	drm_connector_list_iter_end(&conn_iter);
805a85cb24fSFrançois Tigeot 
8065718399fSFrançois Tigeot 	return dpms;
8075718399fSFrançois Tigeot }
8085718399fSFrançois Tigeot 
809ba55f2f5SFrançois Tigeot /* Helper which handles bridge ordering around encoder dpms */
drm_helper_encoder_dpms(struct drm_encoder * encoder,int mode)810ba55f2f5SFrançois Tigeot static void drm_helper_encoder_dpms(struct drm_encoder *encoder, int mode)
811ba55f2f5SFrançois Tigeot {
812ba55f2f5SFrançois Tigeot 	struct drm_bridge *bridge = encoder->bridge;
813477eb7f9SFrançois Tigeot 	const struct drm_encoder_helper_funcs *encoder_funcs;
814ba55f2f5SFrançois Tigeot 
8158621f407SFrançois Tigeot 	encoder_funcs = encoder->helper_private;
8168621f407SFrançois Tigeot 	if (!encoder_funcs)
8178621f407SFrançois Tigeot 		return;
8188621f407SFrançois Tigeot 
819ba55f2f5SFrançois Tigeot 	if (mode == DRM_MODE_DPMS_ON)
82019c468b4SFrançois Tigeot 		drm_bridge_pre_enable(bridge);
821ba55f2f5SFrançois Tigeot 	else
82219c468b4SFrançois Tigeot 		drm_bridge_disable(bridge);
823ba55f2f5SFrançois Tigeot 
824ba55f2f5SFrançois Tigeot 	if (encoder_funcs->dpms)
825ba55f2f5SFrançois Tigeot 		encoder_funcs->dpms(encoder, mode);
826ba55f2f5SFrançois Tigeot 
827ba55f2f5SFrançois Tigeot 	if (mode == DRM_MODE_DPMS_ON)
82819c468b4SFrançois Tigeot 		drm_bridge_enable(bridge);
829ba55f2f5SFrançois Tigeot 	else
83019c468b4SFrançois Tigeot 		drm_bridge_post_disable(bridge);
831ba55f2f5SFrançois Tigeot }
832ba55f2f5SFrançois Tigeot 
drm_helper_choose_crtc_dpms(struct drm_crtc * crtc)8335718399fSFrançois Tigeot static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc)
8345718399fSFrançois Tigeot {
8355718399fSFrançois Tigeot 	int dpms = DRM_MODE_DPMS_OFF;
8365718399fSFrançois Tigeot 	struct drm_connector *connector;
837a85cb24fSFrançois Tigeot 	struct drm_connector_list_iter conn_iter;
8385718399fSFrançois Tigeot 	struct drm_device *dev = crtc->dev;
8395718399fSFrançois Tigeot 
840a85cb24fSFrançois Tigeot 	drm_connector_list_iter_begin(dev, &conn_iter);
841a85cb24fSFrançois Tigeot 	drm_for_each_connector_iter(connector, &conn_iter)
8425718399fSFrançois Tigeot 		if (connector->encoder && connector->encoder->crtc == crtc)
8435718399fSFrançois Tigeot 			if (connector->dpms < dpms)
8445718399fSFrançois Tigeot 				dpms = connector->dpms;
845a85cb24fSFrançois Tigeot 	drm_connector_list_iter_end(&conn_iter);
846a85cb24fSFrançois Tigeot 
8475718399fSFrançois Tigeot 	return dpms;
8485718399fSFrançois Tigeot }
8495718399fSFrançois Tigeot 
8505718399fSFrançois Tigeot /**
8516e29dde8SFrançois Tigeot  * drm_helper_connector_dpms() - connector dpms helper implementation
8526e29dde8SFrançois Tigeot  * @connector: affected connector
8536e29dde8SFrançois Tigeot  * @mode: DPMS mode
8545718399fSFrançois Tigeot  *
855*3f2dd94aSFrançois Tigeot  * The drm_helper_connector_dpms() helper function implements the
856*3f2dd94aSFrançois Tigeot  * &drm_connector_funcs.dpms callback for drivers using the legacy CRTC
857*3f2dd94aSFrançois Tigeot  * helpers.
858aee94f86SFrançois Tigeot  *
859aee94f86SFrançois Tigeot  * This is the main helper function provided by the CRTC helper framework for
8606e29dde8SFrançois Tigeot  * implementing the DPMS connector attribute. It computes the new desired DPMS
861*3f2dd94aSFrançois Tigeot  * state for all encoders and CRTCs in the output mesh and calls the
862*3f2dd94aSFrançois Tigeot  * &drm_crtc_helper_funcs.dpms and &drm_encoder_helper_funcs.dpms callbacks
863*3f2dd94aSFrançois Tigeot  * provided by the driver.
864aee94f86SFrançois Tigeot  *
865aee94f86SFrançois Tigeot  * This function is deprecated.  New drivers must implement atomic modeset
866*3f2dd94aSFrançois Tigeot  * support, where DPMS is handled in the DRM core.
867a05eeebfSFrançois Tigeot  *
868a05eeebfSFrançois Tigeot  * Returns:
869a05eeebfSFrançois Tigeot  * Always returns 0.
8705718399fSFrançois Tigeot  */
drm_helper_connector_dpms(struct drm_connector * connector,int mode)871a05eeebfSFrançois Tigeot int drm_helper_connector_dpms(struct drm_connector *connector, int mode)
8725718399fSFrançois Tigeot {
8735718399fSFrançois Tigeot 	struct drm_encoder *encoder = connector->encoder;
8745718399fSFrançois Tigeot 	struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
875ba55f2f5SFrançois Tigeot 	int old_dpms, encoder_dpms = DRM_MODE_DPMS_OFF;
8765718399fSFrançois Tigeot 
8775718399fSFrançois Tigeot 	if (mode == connector->dpms)
878a05eeebfSFrançois Tigeot 		return 0;
8795718399fSFrançois Tigeot 
8805718399fSFrançois Tigeot 	old_dpms = connector->dpms;
8815718399fSFrançois Tigeot 	connector->dpms = mode;
8825718399fSFrançois Tigeot 
883ba55f2f5SFrançois Tigeot 	if (encoder)
884ba55f2f5SFrançois Tigeot 		encoder_dpms = drm_helper_choose_encoder_dpms(encoder);
885ba55f2f5SFrançois Tigeot 
8865718399fSFrançois Tigeot 	/* from off to on, do crtc then encoder */
8875718399fSFrançois Tigeot 	if (mode < old_dpms) {
8885718399fSFrançois Tigeot 		if (crtc) {
889477eb7f9SFrançois Tigeot 			const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
8905718399fSFrançois Tigeot 			if (crtc_funcs->dpms)
8915718399fSFrançois Tigeot 				(*crtc_funcs->dpms) (crtc,
8925718399fSFrançois Tigeot 						     drm_helper_choose_crtc_dpms(crtc));
8935718399fSFrançois Tigeot 		}
894ba55f2f5SFrançois Tigeot 		if (encoder)
895ba55f2f5SFrançois Tigeot 			drm_helper_encoder_dpms(encoder, encoder_dpms);
8965718399fSFrançois Tigeot 	}
8975718399fSFrançois Tigeot 
8985718399fSFrançois Tigeot 	/* from on to off, do encoder then crtc */
8995718399fSFrançois Tigeot 	if (mode > old_dpms) {
900ba55f2f5SFrançois Tigeot 		if (encoder)
901ba55f2f5SFrançois Tigeot 			drm_helper_encoder_dpms(encoder, encoder_dpms);
9025718399fSFrançois Tigeot 		if (crtc) {
903477eb7f9SFrançois Tigeot 			const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
9045718399fSFrançois Tigeot 			if (crtc_funcs->dpms)
9055718399fSFrançois Tigeot 				(*crtc_funcs->dpms) (crtc,
9065718399fSFrançois Tigeot 						     drm_helper_choose_crtc_dpms(crtc));
9075718399fSFrançois Tigeot 		}
9085718399fSFrançois Tigeot 	}
9095718399fSFrançois Tigeot 
910a05eeebfSFrançois Tigeot 	return 0;
9115718399fSFrançois Tigeot }
9126e29dde8SFrançois Tigeot EXPORT_SYMBOL(drm_helper_connector_dpms);
9135718399fSFrançois Tigeot 
914ba55f2f5SFrançois Tigeot /**
915ba55f2f5SFrançois Tigeot  * drm_helper_resume_force_mode - force-restore mode setting configuration
916ba55f2f5SFrançois Tigeot  * @dev: drm_device which should be restored
917ba55f2f5SFrançois Tigeot  *
918ba55f2f5SFrançois Tigeot  * Drivers which use the mode setting helpers can use this function to
919ba55f2f5SFrançois Tigeot  * force-restore the mode setting configuration e.g. on resume or when something
920ba55f2f5SFrançois Tigeot  * else might have trampled over the hw state (like some overzealous old BIOSen
921ba55f2f5SFrançois Tigeot  * tended to do).
922ba55f2f5SFrançois Tigeot  *
923ba55f2f5SFrançois Tigeot  * This helper doesn't provide a error return value since restoring the old
924ba55f2f5SFrançois Tigeot  * config should never fail due to resource allocation issues since the driver
925ba55f2f5SFrançois Tigeot  * has successfully set the restored configuration already. Hence this should
926ba55f2f5SFrançois Tigeot  * boil down to the equivalent of a few dpms on calls, which also don't provide
927ba55f2f5SFrançois Tigeot  * an error code.
928ba55f2f5SFrançois Tigeot  *
929ba55f2f5SFrançois Tigeot  * Drivers where simply restoring an old configuration again might fail (e.g.
930ba55f2f5SFrançois Tigeot  * due to slight differences in allocating shared resources when the
931ba55f2f5SFrançois Tigeot  * configuration is restored in a different order than when userspace set it up)
932ba55f2f5SFrançois Tigeot  * need to use their own restore logic.
933aee94f86SFrançois Tigeot  *
934aee94f86SFrançois Tigeot  * This function is deprecated. New drivers should implement atomic mode-
935aee94f86SFrançois Tigeot  * setting and use the atomic suspend/resume helpers.
936aee94f86SFrançois Tigeot  *
937aee94f86SFrançois Tigeot  * See also:
938aee94f86SFrançois Tigeot  * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
939ba55f2f5SFrançois Tigeot  */
drm_helper_resume_force_mode(struct drm_device * dev)940ba55f2f5SFrançois Tigeot void drm_helper_resume_force_mode(struct drm_device *dev)
9415718399fSFrançois Tigeot {
9425718399fSFrançois Tigeot 	struct drm_crtc *crtc;
9435718399fSFrançois Tigeot 	struct drm_encoder *encoder;
944477eb7f9SFrançois Tigeot 	const struct drm_crtc_helper_funcs *crtc_funcs;
945ba55f2f5SFrançois Tigeot 	int encoder_dpms;
946ba55f2f5SFrançois Tigeot 	bool ret;
9475718399fSFrançois Tigeot 
948ba55f2f5SFrançois Tigeot 	drm_modeset_lock_all(dev);
949a05eeebfSFrançois Tigeot 	drm_for_each_crtc(crtc, dev) {
9505718399fSFrançois Tigeot 
9515718399fSFrançois Tigeot 		if (!crtc->enabled)
9525718399fSFrançois Tigeot 			continue;
9535718399fSFrançois Tigeot 
9545718399fSFrançois Tigeot 		ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
955ba55f2f5SFrançois Tigeot 					       crtc->x, crtc->y, crtc->primary->fb);
9565718399fSFrançois Tigeot 
957ba55f2f5SFrançois Tigeot 		/* Restoring the old config should never fail! */
9586e29dde8SFrançois Tigeot 		if (ret == false)
9595718399fSFrançois Tigeot 			DRM_ERROR("failed to set mode on crtc %p\n", crtc);
9605718399fSFrançois Tigeot 
9615718399fSFrançois Tigeot 		/* Turn off outputs that were already powered off */
9625718399fSFrançois Tigeot 		if (drm_helper_choose_crtc_dpms(crtc)) {
963a05eeebfSFrançois Tigeot 			drm_for_each_encoder(encoder, dev) {
9645718399fSFrançois Tigeot 
9655718399fSFrançois Tigeot 				if(encoder->crtc != crtc)
9665718399fSFrançois Tigeot 					continue;
9675718399fSFrançois Tigeot 
968ba55f2f5SFrançois Tigeot 				encoder_dpms = drm_helper_choose_encoder_dpms(
969ba55f2f5SFrançois Tigeot 							encoder);
970ba55f2f5SFrançois Tigeot 
971ba55f2f5SFrançois Tigeot 				drm_helper_encoder_dpms(encoder, encoder_dpms);
9725718399fSFrançois Tigeot 			}
9735718399fSFrançois Tigeot 
9745718399fSFrançois Tigeot 			crtc_funcs = crtc->helper_private;
9755718399fSFrançois Tigeot 			if (crtc_funcs->dpms)
9765718399fSFrançois Tigeot 				(*crtc_funcs->dpms) (crtc,
9775718399fSFrançois Tigeot 						     drm_helper_choose_crtc_dpms(crtc));
9785718399fSFrançois Tigeot 		}
9795718399fSFrançois Tigeot 	}
980ba55f2f5SFrançois Tigeot 
9815718399fSFrançois Tigeot 	/* disable the unused connectors while restoring the modesetting */
982ba55f2f5SFrançois Tigeot 	__drm_helper_disable_unused_functions(dev);
983ba55f2f5SFrançois Tigeot 	drm_modeset_unlock_all(dev);
9845718399fSFrançois Tigeot }
9856e29dde8SFrançois Tigeot EXPORT_SYMBOL(drm_helper_resume_force_mode);
9862c9916cdSFrançois Tigeot 
9872c9916cdSFrançois Tigeot /**
9882c9916cdSFrançois Tigeot  * drm_helper_crtc_mode_set - mode_set implementation for atomic plane helpers
9892c9916cdSFrançois Tigeot  * @crtc: DRM CRTC
9902c9916cdSFrançois Tigeot  * @mode: DRM display mode which userspace requested
9912c9916cdSFrançois Tigeot  * @adjusted_mode: DRM display mode adjusted by ->mode_fixup callbacks
9922c9916cdSFrançois Tigeot  * @x: x offset of the CRTC scanout area on the underlying framebuffer
9932c9916cdSFrançois Tigeot  * @y: y offset of the CRTC scanout area on the underlying framebuffer
9942c9916cdSFrançois Tigeot  * @old_fb: previous framebuffer
9952c9916cdSFrançois Tigeot  *
9962c9916cdSFrançois Tigeot  * This function implements a callback useable as the ->mode_set callback
997aee94f86SFrançois Tigeot  * required by the CRTC helpers. Besides the atomic plane helper functions for
9982c9916cdSFrançois Tigeot  * the primary plane the driver must also provide the ->mode_set_nofb callback
999aee94f86SFrançois Tigeot  * to set up the CRTC.
10002c9916cdSFrançois Tigeot  *
10012c9916cdSFrançois Tigeot  * This is a transitional helper useful for converting drivers to the atomic
10022c9916cdSFrançois Tigeot  * interfaces.
10032c9916cdSFrançois Tigeot  */
drm_helper_crtc_mode_set(struct drm_crtc * crtc,struct drm_display_mode * mode,struct drm_display_mode * adjusted_mode,int x,int y,struct drm_framebuffer * old_fb)10042c9916cdSFrançois Tigeot int drm_helper_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode,
10052c9916cdSFrançois Tigeot 			     struct drm_display_mode *adjusted_mode, int x, int y,
10062c9916cdSFrançois Tigeot 			     struct drm_framebuffer *old_fb)
10072c9916cdSFrançois Tigeot {
10082c9916cdSFrançois Tigeot 	struct drm_crtc_state *crtc_state;
1009477eb7f9SFrançois Tigeot 	const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
10102c9916cdSFrançois Tigeot 	int ret;
10112c9916cdSFrançois Tigeot 
10122c9916cdSFrançois Tigeot 	if (crtc->funcs->atomic_duplicate_state)
10132c9916cdSFrançois Tigeot 		crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
101419c468b4SFrançois Tigeot 	else {
1015c0e85e96SFrançois Tigeot 		if (!crtc->state)
1016c0e85e96SFrançois Tigeot 			drm_atomic_helper_crtc_reset(crtc);
1017c0e85e96SFrançois Tigeot 
1018c0e85e96SFrançois Tigeot 		crtc_state = drm_atomic_helper_crtc_duplicate_state(crtc);
1019c0e85e96SFrançois Tigeot 	}
1020c0e85e96SFrançois Tigeot 
10212c9916cdSFrançois Tigeot 	if (!crtc_state)
10222c9916cdSFrançois Tigeot 		return -ENOMEM;
10232c9916cdSFrançois Tigeot 
10242c9916cdSFrançois Tigeot 	crtc_state->planes_changed = true;
10252c9916cdSFrançois Tigeot 	crtc_state->mode_changed = true;
102619c468b4SFrançois Tigeot 	ret = drm_atomic_set_mode_for_crtc(crtc_state, mode);
102719c468b4SFrançois Tigeot 	if (ret)
102819c468b4SFrançois Tigeot 		goto out;
10292c9916cdSFrançois Tigeot 	drm_mode_copy(&crtc_state->adjusted_mode, adjusted_mode);
10302c9916cdSFrançois Tigeot 
10312c9916cdSFrançois Tigeot 	if (crtc_funcs->atomic_check) {
10322c9916cdSFrançois Tigeot 		ret = crtc_funcs->atomic_check(crtc, crtc_state);
103319c468b4SFrançois Tigeot 		if (ret)
103419c468b4SFrançois Tigeot 			goto out;
10352c9916cdSFrançois Tigeot 	}
10362c9916cdSFrançois Tigeot 
10372c9916cdSFrançois Tigeot 	swap(crtc->state, crtc_state);
10382c9916cdSFrançois Tigeot 
10392c9916cdSFrançois Tigeot 	crtc_funcs->mode_set_nofb(crtc);
10402c9916cdSFrançois Tigeot 
104119c468b4SFrançois Tigeot 	ret = drm_helper_crtc_mode_set_base(crtc, x, y, old_fb);
104219c468b4SFrançois Tigeot 
104319c468b4SFrançois Tigeot out:
1044c0e85e96SFrançois Tigeot 	if (crtc_state) {
10452c9916cdSFrançois Tigeot 		if (crtc->funcs->atomic_destroy_state)
10462c9916cdSFrançois Tigeot 			crtc->funcs->atomic_destroy_state(crtc, crtc_state);
1047c0e85e96SFrançois Tigeot 		else
1048c0e85e96SFrançois Tigeot 			drm_atomic_helper_crtc_destroy_state(crtc, crtc_state);
10492c9916cdSFrançois Tigeot 	}
10502c9916cdSFrançois Tigeot 
105119c468b4SFrançois Tigeot 	return ret;
10522c9916cdSFrançois Tigeot }
10532c9916cdSFrançois Tigeot EXPORT_SYMBOL(drm_helper_crtc_mode_set);
10542c9916cdSFrançois Tigeot 
10552c9916cdSFrançois Tigeot /**
10562c9916cdSFrançois Tigeot  * drm_helper_crtc_mode_set_base - mode_set_base implementation for atomic plane helpers
10572c9916cdSFrançois Tigeot  * @crtc: DRM CRTC
10582c9916cdSFrançois Tigeot  * @x: x offset of the CRTC scanout area on the underlying framebuffer
10592c9916cdSFrançois Tigeot  * @y: y offset of the CRTC scanout area on the underlying framebuffer
10602c9916cdSFrançois Tigeot  * @old_fb: previous framebuffer
10612c9916cdSFrançois Tigeot  *
10622c9916cdSFrançois Tigeot  * This function implements a callback useable as the ->mode_set_base used
1063aee94f86SFrançois Tigeot  * required by the CRTC helpers. The driver must provide the atomic plane helper
10642c9916cdSFrançois Tigeot  * functions for the primary plane.
10652c9916cdSFrançois Tigeot  *
10662c9916cdSFrançois Tigeot  * This is a transitional helper useful for converting drivers to the atomic
10672c9916cdSFrançois Tigeot  * interfaces.
10682c9916cdSFrançois Tigeot  */
drm_helper_crtc_mode_set_base(struct drm_crtc * crtc,int x,int y,struct drm_framebuffer * old_fb)10692c9916cdSFrançois Tigeot int drm_helper_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
10702c9916cdSFrançois Tigeot 				  struct drm_framebuffer *old_fb)
10712c9916cdSFrançois Tigeot {
10722c9916cdSFrançois Tigeot 	struct drm_plane_state *plane_state;
10732c9916cdSFrançois Tigeot 	struct drm_plane *plane = crtc->primary;
10742c9916cdSFrançois Tigeot 
10752c9916cdSFrançois Tigeot 	if (plane->funcs->atomic_duplicate_state)
10762c9916cdSFrançois Tigeot 		plane_state = plane->funcs->atomic_duplicate_state(plane);
10778621f407SFrançois Tigeot 	else {
10788621f407SFrançois Tigeot 		if (!plane->state)
10798621f407SFrançois Tigeot 			drm_atomic_helper_plane_reset(plane);
10808621f407SFrançois Tigeot 
10812c9916cdSFrançois Tigeot 		plane_state = drm_atomic_helper_plane_duplicate_state(plane);
10828621f407SFrançois Tigeot 	}
10832c9916cdSFrançois Tigeot 	if (!plane_state)
10842c9916cdSFrançois Tigeot 		return -ENOMEM;
10852c9916cdSFrançois Tigeot 	plane_state->plane = plane;
10862c9916cdSFrançois Tigeot 
10872c9916cdSFrançois Tigeot 	plane_state->crtc = crtc;
10882c9916cdSFrançois Tigeot 	drm_atomic_set_fb_for_plane(plane_state, crtc->primary->fb);
10892c9916cdSFrançois Tigeot 	plane_state->crtc_x = 0;
10902c9916cdSFrançois Tigeot 	plane_state->crtc_y = 0;
10912c9916cdSFrançois Tigeot 	plane_state->crtc_h = crtc->mode.vdisplay;
10922c9916cdSFrançois Tigeot 	plane_state->crtc_w = crtc->mode.hdisplay;
10932c9916cdSFrançois Tigeot 	plane_state->src_x = x << 16;
10942c9916cdSFrançois Tigeot 	plane_state->src_y = y << 16;
10952c9916cdSFrançois Tigeot 	plane_state->src_h = crtc->mode.vdisplay << 16;
10962c9916cdSFrançois Tigeot 	plane_state->src_w = crtc->mode.hdisplay << 16;
10972c9916cdSFrançois Tigeot 
10982c9916cdSFrançois Tigeot 	return drm_plane_helper_commit(plane, plane_state, old_fb);
10992c9916cdSFrançois Tigeot }
11002c9916cdSFrançois Tigeot EXPORT_SYMBOL(drm_helper_crtc_mode_set_base);
1101