xref: /dflybsd-src/sys/dev/drm/drm_probe_helper.c (revision 3f2dd94a569761201b5b0a18b2f697f97fe1b9dc)
1ba55f2f5SFrançois Tigeot /*
2ba55f2f5SFrançois Tigeot  * Copyright (c) 2006-2008 Intel Corporation
3ba55f2f5SFrançois Tigeot  * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
4ba55f2f5SFrançois Tigeot  *
5ba55f2f5SFrançois Tigeot  * DRM core CRTC related functions
6ba55f2f5SFrançois Tigeot  *
7ba55f2f5SFrançois Tigeot  * Permission to use, copy, modify, distribute, and sell this software and its
8ba55f2f5SFrançois Tigeot  * documentation for any purpose is hereby granted without fee, provided that
9ba55f2f5SFrançois Tigeot  * the above copyright notice appear in all copies and that both that copyright
10ba55f2f5SFrançois Tigeot  * notice and this permission notice appear in supporting documentation, and
11ba55f2f5SFrançois Tigeot  * that the name of the copyright holders not be used in advertising or
12ba55f2f5SFrançois Tigeot  * publicity pertaining to distribution of the software without specific,
13ba55f2f5SFrançois Tigeot  * written prior permission.  The copyright holders make no representations
14ba55f2f5SFrançois Tigeot  * about the suitability of this software for any purpose.  It is provided "as
15ba55f2f5SFrançois Tigeot  * is" without express or implied warranty.
16ba55f2f5SFrançois Tigeot  *
17ba55f2f5SFrançois Tigeot  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
18ba55f2f5SFrançois Tigeot  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
19ba55f2f5SFrançois Tigeot  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
20ba55f2f5SFrançois Tigeot  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21ba55f2f5SFrançois Tigeot  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
22ba55f2f5SFrançois Tigeot  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
23ba55f2f5SFrançois Tigeot  * OF THIS SOFTWARE.
24ba55f2f5SFrançois Tigeot  *
25ba55f2f5SFrançois Tigeot  * Authors:
26ba55f2f5SFrançois Tigeot  *      Keith Packard
27ba55f2f5SFrançois Tigeot  *	Eric Anholt <eric@anholt.net>
28ba55f2f5SFrançois Tigeot  *      Dave Airlie <airlied@linux.ie>
29ba55f2f5SFrançois Tigeot  *      Jesse Barnes <jesse.barnes@intel.com>
30ba55f2f5SFrançois Tigeot  */
31ba55f2f5SFrançois Tigeot 
32ba55f2f5SFrançois Tigeot #include <linux/export.h>
33ba55f2f5SFrançois Tigeot #include <linux/moduleparam.h>
34ba55f2f5SFrançois Tigeot 
35ba55f2f5SFrançois Tigeot #include <drm/drmP.h>
36ba55f2f5SFrançois Tigeot #include <drm/drm_crtc.h>
3783b4b9b9SFrançois Tigeot #include <drm/drm_fourcc.h>
38ba55f2f5SFrançois Tigeot #include <drm/drm_crtc_helper.h>
39ba55f2f5SFrançois Tigeot #include <drm/drm_fb_helper.h>
40ba55f2f5SFrançois Tigeot #include <drm/drm_edid.h>
41*3f2dd94aSFrançois Tigeot #include <drm/drm_modeset_helper_vtables.h>
42*3f2dd94aSFrançois Tigeot 
43*3f2dd94aSFrançois Tigeot #include "drm_crtc_helper_internal.h"
44ba55f2f5SFrançois Tigeot 
45ba55f2f5SFrançois Tigeot /**
46ba55f2f5SFrançois Tigeot  * DOC: output probing helper overview
47ba55f2f5SFrançois Tigeot  *
48ba55f2f5SFrançois Tigeot  * This library provides some helper code for output probing. It provides an
49a85cb24fSFrançois Tigeot  * implementation of the core &drm_connector_funcs.fill_modes interface with
50a85cb24fSFrançois Tigeot  * drm_helper_probe_single_connector_modes().
51ba55f2f5SFrançois Tigeot  *
52ba55f2f5SFrançois Tigeot  * It also provides support for polling connectors with a work item and for
53ba55f2f5SFrançois Tigeot  * generic hotplug interrupt handling where the driver doesn't or cannot keep
54ba55f2f5SFrançois Tigeot  * track of a per-connector hpd interrupt.
55ba55f2f5SFrançois Tigeot  *
56ba55f2f5SFrançois Tigeot  * This helper library can be used independently of the modeset helper library.
57ba55f2f5SFrançois Tigeot  * Drivers can also overwrite different parts e.g. use their own hotplug
58ba55f2f5SFrançois Tigeot  * handling code to avoid probing unrelated outputs.
59aee94f86SFrançois Tigeot  *
60aee94f86SFrançois Tigeot  * The probe helpers share the function table structures with other display
61a85cb24fSFrançois Tigeot  * helper libraries. See &struct drm_connector_helper_funcs for the details.
62ba55f2f5SFrançois Tigeot  */
63ba55f2f5SFrançois Tigeot 
64ba55f2f5SFrançois Tigeot static bool drm_kms_helper_poll = true;
65ba55f2f5SFrançois Tigeot module_param_named(poll, drm_kms_helper_poll, bool, 0600);
66ba55f2f5SFrançois Tigeot 
672c9916cdSFrançois Tigeot static enum drm_mode_status
drm_mode_validate_flag(const struct drm_display_mode * mode,int flags)682c9916cdSFrançois Tigeot drm_mode_validate_flag(const struct drm_display_mode *mode,
69ba55f2f5SFrançois Tigeot 		       int flags)
70ba55f2f5SFrançois Tigeot {
71ba55f2f5SFrançois Tigeot 	if ((mode->flags & DRM_MODE_FLAG_INTERLACE) &&
72ba55f2f5SFrançois Tigeot 	    !(flags & DRM_MODE_FLAG_INTERLACE))
732c9916cdSFrançois Tigeot 		return MODE_NO_INTERLACE;
742c9916cdSFrançois Tigeot 
75ba55f2f5SFrançois Tigeot 	if ((mode->flags & DRM_MODE_FLAG_DBLSCAN) &&
76ba55f2f5SFrançois Tigeot 	    !(flags & DRM_MODE_FLAG_DBLSCAN))
772c9916cdSFrançois Tigeot 		return MODE_NO_DBLESCAN;
782c9916cdSFrançois Tigeot 
79ba55f2f5SFrançois Tigeot 	if ((mode->flags & DRM_MODE_FLAG_3D_MASK) &&
80ba55f2f5SFrançois Tigeot 	    !(flags & DRM_MODE_FLAG_3D_MASK))
812c9916cdSFrançois Tigeot 		return MODE_NO_STEREO;
82ba55f2f5SFrançois Tigeot 
832c9916cdSFrançois Tigeot 	return MODE_OK;
84ba55f2f5SFrançois Tigeot }
85ba55f2f5SFrançois Tigeot 
86*3f2dd94aSFrançois Tigeot static enum drm_mode_status
drm_mode_validate_pipeline(struct drm_display_mode * mode,struct drm_connector * connector)87*3f2dd94aSFrançois Tigeot drm_mode_validate_pipeline(struct drm_display_mode *mode,
88*3f2dd94aSFrançois Tigeot 			    struct drm_connector *connector)
89*3f2dd94aSFrançois Tigeot {
90*3f2dd94aSFrançois Tigeot 	struct drm_device *dev = connector->dev;
91*3f2dd94aSFrançois Tigeot 	uint32_t *ids = connector->encoder_ids;
92*3f2dd94aSFrançois Tigeot 	enum drm_mode_status ret = MODE_OK;
93*3f2dd94aSFrançois Tigeot 	unsigned int i;
94*3f2dd94aSFrançois Tigeot 
95*3f2dd94aSFrançois Tigeot 	/* Step 1: Validate against connector */
96*3f2dd94aSFrançois Tigeot 	ret = drm_connector_mode_valid(connector, mode);
97*3f2dd94aSFrançois Tigeot 	if (ret != MODE_OK)
98*3f2dd94aSFrançois Tigeot 		return ret;
99*3f2dd94aSFrançois Tigeot 
100*3f2dd94aSFrançois Tigeot 	/* Step 2: Validate against encoders and crtcs */
101*3f2dd94aSFrançois Tigeot 	for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
102*3f2dd94aSFrançois Tigeot 		struct drm_encoder *encoder = drm_encoder_find(dev, NULL, ids[i]);
103*3f2dd94aSFrançois Tigeot 		struct drm_crtc *crtc;
104*3f2dd94aSFrançois Tigeot 
105*3f2dd94aSFrançois Tigeot 		if (!encoder)
106*3f2dd94aSFrançois Tigeot 			continue;
107*3f2dd94aSFrançois Tigeot 
108*3f2dd94aSFrançois Tigeot 		ret = drm_encoder_mode_valid(encoder, mode);
109*3f2dd94aSFrançois Tigeot 		if (ret != MODE_OK) {
110*3f2dd94aSFrançois Tigeot 			/* No point in continuing for crtc check as this encoder
111*3f2dd94aSFrançois Tigeot 			 * will not accept the mode anyway. If all encoders
112*3f2dd94aSFrançois Tigeot 			 * reject the mode then, at exit, ret will not be
113*3f2dd94aSFrançois Tigeot 			 * MODE_OK. */
114*3f2dd94aSFrançois Tigeot 			continue;
115*3f2dd94aSFrançois Tigeot 		}
116*3f2dd94aSFrançois Tigeot 
117*3f2dd94aSFrançois Tigeot 		ret = drm_bridge_mode_valid(encoder->bridge, mode);
118*3f2dd94aSFrançois Tigeot 		if (ret != MODE_OK) {
119*3f2dd94aSFrançois Tigeot 			/* There is also no point in continuing for crtc check
120*3f2dd94aSFrançois Tigeot 			 * here. */
121*3f2dd94aSFrançois Tigeot 			continue;
122*3f2dd94aSFrançois Tigeot 		}
123*3f2dd94aSFrançois Tigeot 
124*3f2dd94aSFrançois Tigeot 		drm_for_each_crtc(crtc, dev) {
125*3f2dd94aSFrançois Tigeot 			if (!drm_encoder_crtc_ok(encoder, crtc))
126*3f2dd94aSFrançois Tigeot 				continue;
127*3f2dd94aSFrançois Tigeot 
128*3f2dd94aSFrançois Tigeot 			ret = drm_crtc_mode_valid(crtc, mode);
129*3f2dd94aSFrançois Tigeot 			if (ret == MODE_OK) {
130*3f2dd94aSFrançois Tigeot 				/* If we get to this point there is at least
131*3f2dd94aSFrançois Tigeot 				 * one combination of encoder+crtc that works
132*3f2dd94aSFrançois Tigeot 				 * for this mode. Lets return now. */
133*3f2dd94aSFrançois Tigeot 				return ret;
134*3f2dd94aSFrançois Tigeot 			}
135*3f2dd94aSFrançois Tigeot 		}
136*3f2dd94aSFrançois Tigeot 	}
137*3f2dd94aSFrançois Tigeot 
138*3f2dd94aSFrançois Tigeot 	return ret;
139*3f2dd94aSFrançois Tigeot }
140*3f2dd94aSFrançois Tigeot 
drm_helper_probe_add_cmdline_mode(struct drm_connector * connector)1411b13d190SFrançois Tigeot static int drm_helper_probe_add_cmdline_mode(struct drm_connector *connector)
1421b13d190SFrançois Tigeot {
1431dedbd3bSFrançois Tigeot 	struct drm_cmdline_mode *cmdline_mode;
1441b13d190SFrançois Tigeot 	struct drm_display_mode *mode;
1451b13d190SFrançois Tigeot 
1461dedbd3bSFrançois Tigeot 	cmdline_mode = &connector->cmdline_mode;
1471dedbd3bSFrançois Tigeot 	if (!cmdline_mode->specified)
1481b13d190SFrançois Tigeot 		return 0;
1491b13d190SFrançois Tigeot 
1501dedbd3bSFrançois Tigeot 	/* Only add a GTF mode if we find no matching probed modes */
1511dedbd3bSFrançois Tigeot 	list_for_each_entry(mode, &connector->probed_modes, head) {
1521dedbd3bSFrançois Tigeot 		if (mode->hdisplay != cmdline_mode->xres ||
1531dedbd3bSFrançois Tigeot 		    mode->vdisplay != cmdline_mode->yres)
1541dedbd3bSFrançois Tigeot 			continue;
1551dedbd3bSFrançois Tigeot 
1561dedbd3bSFrançois Tigeot 		if (cmdline_mode->refresh_specified) {
1571dedbd3bSFrançois Tigeot 			/* The probed mode's vrefresh is set until later */
1581dedbd3bSFrançois Tigeot 			if (drm_mode_vrefresh(mode) != cmdline_mode->refresh)
1591dedbd3bSFrançois Tigeot 				continue;
1601dedbd3bSFrançois Tigeot 		}
1611dedbd3bSFrançois Tigeot 
1621dedbd3bSFrançois Tigeot 		return 0;
1631dedbd3bSFrançois Tigeot 	}
1641dedbd3bSFrançois Tigeot 
1651b13d190SFrançois Tigeot 	mode = drm_mode_create_from_cmdline_mode(connector->dev,
1661dedbd3bSFrançois Tigeot 						 cmdline_mode);
1671b13d190SFrançois Tigeot 	if (mode == NULL)
1681b13d190SFrançois Tigeot 		return 0;
1691b13d190SFrançois Tigeot 
1701b13d190SFrançois Tigeot 	drm_mode_probed_add(connector, mode);
1711b13d190SFrançois Tigeot 	return 1;
1721b13d190SFrançois Tigeot }
1731b13d190SFrançois Tigeot 
drm_crtc_mode_valid(struct drm_crtc * crtc,const struct drm_display_mode * mode)174*3f2dd94aSFrançois Tigeot enum drm_mode_status drm_crtc_mode_valid(struct drm_crtc *crtc,
175*3f2dd94aSFrançois Tigeot 					 const struct drm_display_mode *mode)
176*3f2dd94aSFrançois Tigeot {
177*3f2dd94aSFrançois Tigeot 	const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
178*3f2dd94aSFrançois Tigeot 
179*3f2dd94aSFrançois Tigeot 	if (!crtc_funcs || !crtc_funcs->mode_valid)
180*3f2dd94aSFrançois Tigeot 		return MODE_OK;
181*3f2dd94aSFrançois Tigeot 
182*3f2dd94aSFrançois Tigeot 	return crtc_funcs->mode_valid(crtc, mode);
183*3f2dd94aSFrançois Tigeot }
184*3f2dd94aSFrançois Tigeot 
drm_encoder_mode_valid(struct drm_encoder * encoder,const struct drm_display_mode * mode)185*3f2dd94aSFrançois Tigeot enum drm_mode_status drm_encoder_mode_valid(struct drm_encoder *encoder,
186*3f2dd94aSFrançois Tigeot 					    const struct drm_display_mode *mode)
187*3f2dd94aSFrançois Tigeot {
188*3f2dd94aSFrançois Tigeot 	const struct drm_encoder_helper_funcs *encoder_funcs =
189*3f2dd94aSFrançois Tigeot 		encoder->helper_private;
190*3f2dd94aSFrançois Tigeot 
191*3f2dd94aSFrançois Tigeot 	if (!encoder_funcs || !encoder_funcs->mode_valid)
192*3f2dd94aSFrançois Tigeot 		return MODE_OK;
193*3f2dd94aSFrançois Tigeot 
194*3f2dd94aSFrançois Tigeot 	return encoder_funcs->mode_valid(encoder, mode);
195*3f2dd94aSFrançois Tigeot }
196*3f2dd94aSFrançois Tigeot 
drm_connector_mode_valid(struct drm_connector * connector,struct drm_display_mode * mode)197*3f2dd94aSFrançois Tigeot enum drm_mode_status drm_connector_mode_valid(struct drm_connector *connector,
198*3f2dd94aSFrançois Tigeot 					      struct drm_display_mode *mode)
199*3f2dd94aSFrançois Tigeot {
200*3f2dd94aSFrançois Tigeot 	const struct drm_connector_helper_funcs *connector_funcs =
201*3f2dd94aSFrançois Tigeot 		connector->helper_private;
202*3f2dd94aSFrançois Tigeot 
203*3f2dd94aSFrançois Tigeot 	if (!connector_funcs || !connector_funcs->mode_valid)
204*3f2dd94aSFrançois Tigeot 		return MODE_OK;
205*3f2dd94aSFrançois Tigeot 
206*3f2dd94aSFrançois Tigeot 	return connector_funcs->mode_valid(connector, mode);
207*3f2dd94aSFrançois Tigeot }
208*3f2dd94aSFrançois Tigeot 
209a05eeebfSFrançois Tigeot #define DRM_OUTPUT_POLL_PERIOD (10*HZ)
210a05eeebfSFrançois Tigeot /**
211a85cb24fSFrançois Tigeot  * drm_kms_helper_poll_enable - re-enable output polling.
212a05eeebfSFrançois Tigeot  * @dev: drm_device
213a05eeebfSFrançois Tigeot  *
214a85cb24fSFrançois Tigeot  * This function re-enables the output polling work, after it has been
215a85cb24fSFrançois Tigeot  * temporarily disabled using drm_kms_helper_poll_disable(), for example over
216a85cb24fSFrançois Tigeot  * suspend/resume.
217a05eeebfSFrançois Tigeot  *
218a85cb24fSFrançois Tigeot  * Drivers can call this helper from their device resume implementation. It is
219a85cb24fSFrançois Tigeot  * an error to call this when the output polling support has not yet been set
220a85cb24fSFrançois Tigeot  * up.
221a85cb24fSFrançois Tigeot  *
222a85cb24fSFrançois Tigeot  * Note that calls to enable and disable polling must be strictly ordered, which
223a85cb24fSFrançois Tigeot  * is automatically the case when they're only call from suspend/resume
224a85cb24fSFrançois Tigeot  * callbacks.
225a05eeebfSFrançois Tigeot  */
drm_kms_helper_poll_enable(struct drm_device * dev)226a85cb24fSFrançois Tigeot void drm_kms_helper_poll_enable(struct drm_device *dev)
227a05eeebfSFrançois Tigeot {
228a05eeebfSFrançois Tigeot 	bool poll = false;
229a05eeebfSFrançois Tigeot 	struct drm_connector *connector;
230a85cb24fSFrançois Tigeot 	struct drm_connector_list_iter conn_iter;
2311dedbd3bSFrançois Tigeot 	unsigned long delay = DRM_OUTPUT_POLL_PERIOD;
232a05eeebfSFrançois Tigeot 
233a05eeebfSFrançois Tigeot 	if (!dev->mode_config.poll_enabled || !drm_kms_helper_poll)
234a05eeebfSFrançois Tigeot 		return;
235a05eeebfSFrançois Tigeot 
236a85cb24fSFrançois Tigeot 	drm_connector_list_iter_begin(dev, &conn_iter);
237a85cb24fSFrançois Tigeot 	drm_for_each_connector_iter(connector, &conn_iter) {
238a05eeebfSFrançois Tigeot 		if (connector->polled & (DRM_CONNECTOR_POLL_CONNECT |
239a05eeebfSFrançois Tigeot 					 DRM_CONNECTOR_POLL_DISCONNECT))
240a05eeebfSFrançois Tigeot 			poll = true;
241a05eeebfSFrançois Tigeot 	}
242a85cb24fSFrançois Tigeot 	drm_connector_list_iter_end(&conn_iter);
243a05eeebfSFrançois Tigeot 
2441dedbd3bSFrançois Tigeot 	if (dev->mode_config.delayed_event) {
2454be47400SFrançois Tigeot 		/*
2464be47400SFrançois Tigeot 		 * FIXME:
2474be47400SFrançois Tigeot 		 *
2484be47400SFrançois Tigeot 		 * Use short (1s) delay to handle the initial delayed event.
2494be47400SFrançois Tigeot 		 * This delay should not be needed, but Optimus/nouveau will
2504be47400SFrançois Tigeot 		 * fail in a mysterious way if the delayed event is handled as
2514be47400SFrançois Tigeot 		 * soon as possible like it is done in
2524be47400SFrançois Tigeot 		 * drm_helper_probe_single_connector_modes() in case the poll
2534be47400SFrançois Tigeot 		 * was enabled before.
2544be47400SFrançois Tigeot 		 */
2551dedbd3bSFrançois Tigeot 		poll = true;
2564be47400SFrançois Tigeot 		delay = HZ;
2571dedbd3bSFrançois Tigeot 	}
2581dedbd3bSFrançois Tigeot 
259a05eeebfSFrançois Tigeot 	if (poll)
2601dedbd3bSFrançois Tigeot 		schedule_delayed_work(&dev->mode_config.output_poll_work, delay);
261a05eeebfSFrançois Tigeot }
262a85cb24fSFrançois Tigeot EXPORT_SYMBOL(drm_kms_helper_poll_enable);
263a05eeebfSFrançois Tigeot 
2644be47400SFrançois Tigeot static enum drm_connector_status
drm_helper_probe_detect_ctx(struct drm_connector * connector,bool force)265a85cb24fSFrançois Tigeot drm_helper_probe_detect_ctx(struct drm_connector *connector, bool force)
2664be47400SFrançois Tigeot {
267a85cb24fSFrançois Tigeot 	const struct drm_connector_helper_funcs *funcs = connector->helper_private;
268a85cb24fSFrançois Tigeot 	struct drm_modeset_acquire_ctx ctx;
269a85cb24fSFrançois Tigeot 	int ret;
270a85cb24fSFrançois Tigeot 
271a85cb24fSFrançois Tigeot 	drm_modeset_acquire_init(&ctx, 0);
272a85cb24fSFrançois Tigeot 
273a85cb24fSFrançois Tigeot retry:
274a85cb24fSFrançois Tigeot 	ret = drm_modeset_lock(&connector->dev->mode_config.connection_mutex, &ctx);
275a85cb24fSFrançois Tigeot 	if (!ret) {
276a85cb24fSFrançois Tigeot 		if (funcs->detect_ctx)
277a85cb24fSFrançois Tigeot 			ret = funcs->detect_ctx(connector, &ctx, force);
278a85cb24fSFrançois Tigeot 		else if (connector->funcs->detect)
279a85cb24fSFrançois Tigeot 			ret = connector->funcs->detect(connector, force);
280a85cb24fSFrançois Tigeot 		else
281a85cb24fSFrançois Tigeot 			ret = connector_status_connected;
2824be47400SFrançois Tigeot 	}
2834be47400SFrançois Tigeot 
284a85cb24fSFrançois Tigeot 	if (ret == -EDEADLK) {
285a85cb24fSFrançois Tigeot 		drm_modeset_backoff(&ctx);
286a85cb24fSFrançois Tigeot 		goto retry;
287a85cb24fSFrançois Tigeot 	}
288a85cb24fSFrançois Tigeot 
289a85cb24fSFrançois Tigeot 	if (WARN_ON(ret < 0))
290a85cb24fSFrançois Tigeot 		ret = connector_status_unknown;
291a85cb24fSFrançois Tigeot 
292a85cb24fSFrançois Tigeot 	drm_modeset_drop_locks(&ctx);
293a85cb24fSFrançois Tigeot 	drm_modeset_acquire_fini(&ctx);
294a85cb24fSFrançois Tigeot 
295a85cb24fSFrançois Tigeot 	return ret;
296a85cb24fSFrançois Tigeot }
297a85cb24fSFrançois Tigeot 
298a85cb24fSFrançois Tigeot /**
299a85cb24fSFrançois Tigeot  * drm_helper_probe_detect - probe connector status
300a85cb24fSFrançois Tigeot  * @connector: connector to probe
301a85cb24fSFrançois Tigeot  * @ctx: acquire_ctx, or NULL to let this function handle locking.
302a85cb24fSFrançois Tigeot  * @force: Whether destructive probe operations should be performed.
303a85cb24fSFrançois Tigeot  *
304a85cb24fSFrançois Tigeot  * This function calls the detect callbacks of the connector.
305a85cb24fSFrançois Tigeot  * This function returns &drm_connector_status, or
306a85cb24fSFrançois Tigeot  * if @ctx is set, it might also return -EDEADLK.
307a85cb24fSFrançois Tigeot  */
308a85cb24fSFrançois Tigeot int
drm_helper_probe_detect(struct drm_connector * connector,struct drm_modeset_acquire_ctx * ctx,bool force)309a85cb24fSFrançois Tigeot drm_helper_probe_detect(struct drm_connector *connector,
310a85cb24fSFrançois Tigeot 			struct drm_modeset_acquire_ctx *ctx,
311a85cb24fSFrançois Tigeot 			bool force)
312a85cb24fSFrançois Tigeot {
313a85cb24fSFrançois Tigeot 	const struct drm_connector_helper_funcs *funcs = connector->helper_private;
314a85cb24fSFrançois Tigeot 	struct drm_device *dev = connector->dev;
315a85cb24fSFrançois Tigeot 	int ret;
316a85cb24fSFrançois Tigeot 
317a85cb24fSFrançois Tigeot 	if (!ctx)
318a85cb24fSFrançois Tigeot 		return drm_helper_probe_detect_ctx(connector, force);
319a85cb24fSFrançois Tigeot 
320a85cb24fSFrançois Tigeot 	ret = drm_modeset_lock(&dev->mode_config.connection_mutex, ctx);
321a85cb24fSFrançois Tigeot 	if (ret)
322a85cb24fSFrançois Tigeot 		return ret;
323a85cb24fSFrançois Tigeot 
324a85cb24fSFrançois Tigeot 	if (funcs->detect_ctx)
325a85cb24fSFrançois Tigeot 		return funcs->detect_ctx(connector, ctx, force);
326a85cb24fSFrançois Tigeot 	else if (connector->funcs->detect)
327a85cb24fSFrançois Tigeot 		return connector->funcs->detect(connector, force);
328a85cb24fSFrançois Tigeot 	else
329a85cb24fSFrançois Tigeot 		return connector_status_connected;
330a85cb24fSFrançois Tigeot }
331a85cb24fSFrançois Tigeot EXPORT_SYMBOL(drm_helper_probe_detect);
332a85cb24fSFrançois Tigeot 
333aee94f86SFrançois Tigeot /**
334aee94f86SFrançois Tigeot  * drm_helper_probe_single_connector_modes - get complete set of display modes
335aee94f86SFrançois Tigeot  * @connector: connector to probe
336aee94f86SFrançois Tigeot  * @maxX: max width for modes
337aee94f86SFrançois Tigeot  * @maxY: max height for modes
338aee94f86SFrançois Tigeot  *
339aee94f86SFrançois Tigeot  * Based on the helper callbacks implemented by @connector in struct
340aee94f86SFrançois Tigeot  * &drm_connector_helper_funcs try to detect all valid modes.  Modes will first
341aee94f86SFrançois Tigeot  * be added to the connector's probed_modes list, then culled (based on validity
342aee94f86SFrançois Tigeot  * and the @maxX, @maxY parameters) and put into the normal modes list.
343aee94f86SFrançois Tigeot  *
344a85cb24fSFrançois Tigeot  * Intended to be used as a generic implementation of the
345a85cb24fSFrançois Tigeot  * &drm_connector_funcs.fill_modes() vfunc for drivers that use the CRTC helpers
346a85cb24fSFrançois Tigeot  * for output mode filtering and detection.
347aee94f86SFrançois Tigeot  *
348aee94f86SFrançois Tigeot  * The basic procedure is as follows
349aee94f86SFrançois Tigeot  *
350aee94f86SFrançois Tigeot  * 1. All modes currently on the connector's modes list are marked as stale
351aee94f86SFrançois Tigeot  *
352aee94f86SFrançois Tigeot  * 2. New modes are added to the connector's probed_modes list with
353aee94f86SFrançois Tigeot  *    drm_mode_probed_add(). New modes start their life with status as OK.
354aee94f86SFrançois Tigeot  *    Modes are added from a single source using the following priority order.
355aee94f86SFrançois Tigeot  *
356a85cb24fSFrançois Tigeot  *    - &drm_connector_helper_funcs.get_modes vfunc
357aee94f86SFrançois Tigeot  *    - if the connector status is connector_status_connected, standard
358aee94f86SFrançois Tigeot  *      VESA DMT modes up to 1024x768 are automatically added
359aee94f86SFrançois Tigeot  *      (drm_add_modes_noedid())
360aee94f86SFrançois Tigeot  *
361aee94f86SFrançois Tigeot  *    Finally modes specified via the kernel command line (video=...) are
362aee94f86SFrançois Tigeot  *    added in addition to what the earlier probes produced
363aee94f86SFrançois Tigeot  *    (drm_helper_probe_add_cmdline_mode()). These modes are generated
364aee94f86SFrançois Tigeot  *    using the VESA GTF/CVT formulas.
365aee94f86SFrançois Tigeot  *
366aee94f86SFrançois Tigeot  * 3. Modes are moved from the probed_modes list to the modes list. Potential
367aee94f86SFrançois Tigeot  *    duplicates are merged together (see drm_mode_connector_list_update()).
368aee94f86SFrançois Tigeot  *    After this step the probed_modes list will be empty again.
369aee94f86SFrançois Tigeot  *
370aee94f86SFrançois Tigeot  * 4. Any non-stale mode on the modes list then undergoes validation
371aee94f86SFrançois Tigeot  *
372aee94f86SFrançois Tigeot  *    - drm_mode_validate_basic() performs basic sanity checks
373aee94f86SFrançois Tigeot  *    - drm_mode_validate_size() filters out modes larger than @maxX and @maxY
374aee94f86SFrançois Tigeot  *      (if specified)
375a85cb24fSFrançois Tigeot  *    - drm_mode_validate_flag() checks the modes against basic connector
376a85cb24fSFrançois Tigeot  *      capabilities (interlace_allowed,doublescan_allowed,stereo_allowed)
377a85cb24fSFrançois Tigeot  *    - the optional &drm_connector_helper_funcs.mode_valid helper can perform
378*3f2dd94aSFrançois Tigeot  *      driver and/or sink specific checks
379*3f2dd94aSFrançois Tigeot  *    - the optional &drm_crtc_helper_funcs.mode_valid,
380*3f2dd94aSFrançois Tigeot  *      &drm_bridge_funcs.mode_valid and &drm_encoder_helper_funcs.mode_valid
381*3f2dd94aSFrançois Tigeot  *      helpers can perform driver and/or source specific checks which are also
382*3f2dd94aSFrançois Tigeot  *      enforced by the modeset/atomic helpers
383aee94f86SFrançois Tigeot  *
384aee94f86SFrançois Tigeot  * 5. Any mode whose status is not OK is pruned from the connector's modes list,
385aee94f86SFrançois Tigeot  *    accompanied by a debug message indicating the reason for the mode's
386aee94f86SFrançois Tigeot  *    rejection (see drm_mode_prune_invalid()).
387aee94f86SFrançois Tigeot  *
388aee94f86SFrançois Tigeot  * Returns:
389aee94f86SFrançois Tigeot  * The number of modes found on @connector.
390aee94f86SFrançois Tigeot  */
drm_helper_probe_single_connector_modes(struct drm_connector * connector,uint32_t maxX,uint32_t maxY)391aee94f86SFrançois Tigeot int drm_helper_probe_single_connector_modes(struct drm_connector *connector,
392aee94f86SFrançois Tigeot 					    uint32_t maxX, uint32_t maxY)
393ba55f2f5SFrançois Tigeot {
394ba55f2f5SFrançois Tigeot 	struct drm_device *dev = connector->dev;
395ba55f2f5SFrançois Tigeot 	struct drm_display_mode *mode;
396477eb7f9SFrançois Tigeot 	const struct drm_connector_helper_funcs *connector_funcs =
397ba55f2f5SFrançois Tigeot 		connector->helper_private;
398a85cb24fSFrançois Tigeot 	int count = 0, ret;
399ba55f2f5SFrançois Tigeot 	int mode_flags = 0;
400ba55f2f5SFrançois Tigeot 	bool verbose_prune = true;
401aee94f86SFrançois Tigeot 	enum drm_connector_status old_status;
402a85cb24fSFrançois Tigeot 	struct drm_modeset_acquire_ctx ctx;
403ba55f2f5SFrançois Tigeot 
404ba55f2f5SFrançois Tigeot 	WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
405ba55f2f5SFrançois Tigeot 
406a85cb24fSFrançois Tigeot 	drm_modeset_acquire_init(&ctx, 0);
407a85cb24fSFrançois Tigeot 
408ba55f2f5SFrançois Tigeot 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", connector->base.id,
409ba55f2f5SFrançois Tigeot 			connector->name);
410a85cb24fSFrançois Tigeot 
411a85cb24fSFrançois Tigeot retry:
412a85cb24fSFrançois Tigeot 	ret = drm_modeset_lock(&dev->mode_config.connection_mutex, &ctx);
413a85cb24fSFrançois Tigeot 	if (ret == -EDEADLK) {
414a85cb24fSFrançois Tigeot 		drm_modeset_backoff(&ctx);
415a85cb24fSFrançois Tigeot 		goto retry;
416a85cb24fSFrançois Tigeot 	} else
417a85cb24fSFrançois Tigeot 		WARN_ON(ret < 0);
418a85cb24fSFrançois Tigeot 
419aee94f86SFrançois Tigeot 	/* set all old modes to the stale state */
420ba55f2f5SFrançois Tigeot 	list_for_each_entry(mode, &connector->modes, head)
421aee94f86SFrançois Tigeot 		mode->status = MODE_STALE;
422aee94f86SFrançois Tigeot 
423aee94f86SFrançois Tigeot 	old_status = connector->status;
424ba55f2f5SFrançois Tigeot 
425ba55f2f5SFrançois Tigeot 	if (connector->force) {
4262c9916cdSFrançois Tigeot 		if (connector->force == DRM_FORCE_ON ||
4272c9916cdSFrançois Tigeot 		    connector->force == DRM_FORCE_ON_DIGITAL)
428ba55f2f5SFrançois Tigeot 			connector->status = connector_status_connected;
429ba55f2f5SFrançois Tigeot 		else
430ba55f2f5SFrançois Tigeot 			connector->status = connector_status_disconnected;
431ba55f2f5SFrançois Tigeot 		if (connector->funcs->force)
432ba55f2f5SFrançois Tigeot 			connector->funcs->force(connector);
433ba55f2f5SFrançois Tigeot 	} else {
434a85cb24fSFrançois Tigeot 		ret = drm_helper_probe_detect(connector, &ctx, true);
435a85cb24fSFrançois Tigeot 
436a85cb24fSFrançois Tigeot 		if (ret == -EDEADLK) {
437a85cb24fSFrançois Tigeot 			drm_modeset_backoff(&ctx);
438a85cb24fSFrançois Tigeot 			goto retry;
439a85cb24fSFrançois Tigeot 		} else if (WARN(ret < 0, "Invalid return value %i for connector detection\n", ret))
440a85cb24fSFrançois Tigeot 			ret = connector_status_unknown;
441a85cb24fSFrançois Tigeot 
442a85cb24fSFrançois Tigeot 		connector->status = ret;
443ba55f2f5SFrançois Tigeot 	}
444ba55f2f5SFrançois Tigeot 
445aee94f86SFrançois Tigeot 	/*
446aee94f86SFrançois Tigeot 	 * Normally either the driver's hpd code or the poll loop should
447aee94f86SFrançois Tigeot 	 * pick up any changes and fire the hotplug event. But if
448aee94f86SFrançois Tigeot 	 * userspace sneaks in a probe, we might miss a change. Hence
449aee94f86SFrançois Tigeot 	 * check here, and if anything changed start the hotplug code.
450aee94f86SFrançois Tigeot 	 */
451aee94f86SFrançois Tigeot 	if (old_status != connector->status) {
452aee94f86SFrançois Tigeot 		DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
453aee94f86SFrançois Tigeot 			      connector->base.id,
454aee94f86SFrançois Tigeot 			      connector->name,
455aee94f86SFrançois Tigeot 			      drm_get_connector_status_name(old_status),
456aee94f86SFrançois Tigeot 			      drm_get_connector_status_name(connector->status));
457aee94f86SFrançois Tigeot 
458aee94f86SFrançois Tigeot 		/*
459aee94f86SFrançois Tigeot 		 * The hotplug event code might call into the fb
460aee94f86SFrançois Tigeot 		 * helpers, and so expects that we do not hold any
461aee94f86SFrançois Tigeot 		 * locks. Fire up the poll struct instead, it will
462aee94f86SFrançois Tigeot 		 * disable itself again.
463aee94f86SFrançois Tigeot 		 */
464aee94f86SFrançois Tigeot 		dev->mode_config.delayed_event = true;
465aee94f86SFrançois Tigeot 		if (dev->mode_config.poll_enabled)
466aee94f86SFrançois Tigeot 			schedule_delayed_work(&dev->mode_config.output_poll_work,
467aee94f86SFrançois Tigeot 					      0);
468aee94f86SFrançois Tigeot 	}
469aee94f86SFrançois Tigeot 
470ba55f2f5SFrançois Tigeot 	/* Re-enable polling in case the global poll config changed. */
471ba55f2f5SFrançois Tigeot 	if (drm_kms_helper_poll != dev->mode_config.poll_running)
472a85cb24fSFrançois Tigeot 		drm_kms_helper_poll_enable(dev);
473ba55f2f5SFrançois Tigeot 
474ba55f2f5SFrançois Tigeot 	dev->mode_config.poll_running = drm_kms_helper_poll;
475ba55f2f5SFrançois Tigeot 
476ba55f2f5SFrançois Tigeot 	if (connector->status == connector_status_disconnected) {
477ba55f2f5SFrançois Tigeot 		DRM_DEBUG_KMS("[CONNECTOR:%d:%s] disconnected\n",
478ba55f2f5SFrançois Tigeot 			connector->base.id, connector->name);
479ba55f2f5SFrançois Tigeot 		drm_mode_connector_update_edid_property(connector, NULL);
480ba55f2f5SFrançois Tigeot 		verbose_prune = false;
481ba55f2f5SFrançois Tigeot 		goto prune;
482ba55f2f5SFrançois Tigeot 	}
483ba55f2f5SFrançois Tigeot 
484ba55f2f5SFrançois Tigeot 	count = (*connector_funcs->get_modes)(connector);
485ba55f2f5SFrançois Tigeot 
486ba55f2f5SFrançois Tigeot 	if (count == 0 && connector->status == connector_status_connected)
487ba55f2f5SFrançois Tigeot 		count = drm_add_modes_noedid(connector, 1024, 768);
4881b13d190SFrançois Tigeot 	count += drm_helper_probe_add_cmdline_mode(connector);
489ba55f2f5SFrançois Tigeot 	if (count == 0)
490ba55f2f5SFrançois Tigeot 		goto prune;
491ba55f2f5SFrançois Tigeot 
492aee94f86SFrançois Tigeot 	drm_mode_connector_list_update(connector);
493ba55f2f5SFrançois Tigeot 
494ba55f2f5SFrançois Tigeot 	if (connector->interlace_allowed)
495ba55f2f5SFrançois Tigeot 		mode_flags |= DRM_MODE_FLAG_INTERLACE;
496ba55f2f5SFrançois Tigeot 	if (connector->doublescan_allowed)
497ba55f2f5SFrançois Tigeot 		mode_flags |= DRM_MODE_FLAG_DBLSCAN;
498ba55f2f5SFrançois Tigeot 	if (connector->stereo_allowed)
499ba55f2f5SFrançois Tigeot 		mode_flags |= DRM_MODE_FLAG_3D_MASK;
500ba55f2f5SFrançois Tigeot 
501ba55f2f5SFrançois Tigeot 	list_for_each_entry(mode, &connector->modes, head) {
502352ff8bdSFrançois Tigeot 		if (mode->status == MODE_OK)
5032c9916cdSFrançois Tigeot 			mode->status = drm_mode_validate_basic(mode);
5042c9916cdSFrançois Tigeot 
5052c9916cdSFrançois Tigeot 		if (mode->status == MODE_OK)
5062c9916cdSFrançois Tigeot 			mode->status = drm_mode_validate_size(mode, maxX, maxY);
5072c9916cdSFrançois Tigeot 
5082c9916cdSFrançois Tigeot 		if (mode->status == MODE_OK)
5092c9916cdSFrançois Tigeot 			mode->status = drm_mode_validate_flag(mode, mode_flags);
5102c9916cdSFrançois Tigeot 
511*3f2dd94aSFrançois Tigeot 		if (mode->status == MODE_OK)
512*3f2dd94aSFrançois Tigeot 			mode->status = drm_mode_validate_pipeline(mode,
513*3f2dd94aSFrançois Tigeot 								  connector);
514*3f2dd94aSFrançois Tigeot 
515*3f2dd94aSFrançois Tigeot 		if (mode->status == MODE_OK)
516*3f2dd94aSFrançois Tigeot 			mode->status = drm_mode_validate_ycbcr420(mode,
517*3f2dd94aSFrançois Tigeot 								  connector);
518ba55f2f5SFrançois Tigeot 	}
519ba55f2f5SFrançois Tigeot 
520ba55f2f5SFrançois Tigeot prune:
521ba55f2f5SFrançois Tigeot 	drm_mode_prune_invalid(dev, &connector->modes, verbose_prune);
522ba55f2f5SFrançois Tigeot 
523a85cb24fSFrançois Tigeot 	drm_modeset_drop_locks(&ctx);
524a85cb24fSFrançois Tigeot 	drm_modeset_acquire_fini(&ctx);
525a85cb24fSFrançois Tigeot 
526ba55f2f5SFrançois Tigeot 	if (list_empty(&connector->modes))
527ba55f2f5SFrançois Tigeot 		return 0;
528ba55f2f5SFrançois Tigeot 
529ba55f2f5SFrançois Tigeot 	list_for_each_entry(mode, &connector->modes, head)
530ba55f2f5SFrançois Tigeot 		mode->vrefresh = drm_mode_vrefresh(mode);
531ba55f2f5SFrançois Tigeot 
532ba55f2f5SFrançois Tigeot 	drm_mode_sort(&connector->modes);
533ba55f2f5SFrançois Tigeot 
534ba55f2f5SFrançois Tigeot 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s] probed modes :\n", connector->base.id,
535ba55f2f5SFrançois Tigeot 			connector->name);
536ba55f2f5SFrançois Tigeot 	list_for_each_entry(mode, &connector->modes, head) {
537ba55f2f5SFrançois Tigeot 		drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
538ba55f2f5SFrançois Tigeot 		drm_mode_debug_printmodeline(mode);
539ba55f2f5SFrançois Tigeot 	}
540ba55f2f5SFrançois Tigeot 
541ba55f2f5SFrançois Tigeot 	return count;
542ba55f2f5SFrançois Tigeot }
543ba55f2f5SFrançois Tigeot EXPORT_SYMBOL(drm_helper_probe_single_connector_modes);
544ba55f2f5SFrançois Tigeot 
545ba55f2f5SFrançois Tigeot /**
546ba55f2f5SFrançois Tigeot  * drm_kms_helper_hotplug_event - fire off KMS hotplug events
547ba55f2f5SFrançois Tigeot  * @dev: drm_device whose connector state changed
548ba55f2f5SFrançois Tigeot  *
549ba55f2f5SFrançois Tigeot  * This function fires off the uevent for userspace and also calls the
550ba55f2f5SFrançois Tigeot  * output_poll_changed function, which is most commonly used to inform the fbdev
551ba55f2f5SFrançois Tigeot  * emulation code and allow it to update the fbcon output configuration.
552ba55f2f5SFrançois Tigeot  *
553ba55f2f5SFrançois Tigeot  * Drivers should call this from their hotplug handling code when a change is
554ba55f2f5SFrançois Tigeot  * detected. Note that this function does not do any output detection of its
555ba55f2f5SFrançois Tigeot  * own, like drm_helper_hpd_irq_event() does - this is assumed to be done by the
556ba55f2f5SFrançois Tigeot  * driver already.
557ba55f2f5SFrançois Tigeot  *
558ba55f2f5SFrançois Tigeot  * This function must be called from process context with no mode
559ba55f2f5SFrançois Tigeot  * setting locks held.
560ba55f2f5SFrançois Tigeot  */
drm_kms_helper_hotplug_event(struct drm_device * dev)561ba55f2f5SFrançois Tigeot void drm_kms_helper_hotplug_event(struct drm_device *dev)
562ba55f2f5SFrançois Tigeot {
563ba55f2f5SFrançois Tigeot 	/* send a uevent + call fbdev */
564ba55f2f5SFrançois Tigeot 	drm_sysfs_hotplug_event(dev);
565ba55f2f5SFrançois Tigeot 	if (dev->mode_config.funcs->output_poll_changed)
566ba55f2f5SFrançois Tigeot 		dev->mode_config.funcs->output_poll_changed(dev);
567ba55f2f5SFrançois Tigeot }
568ba55f2f5SFrançois Tigeot EXPORT_SYMBOL(drm_kms_helper_hotplug_event);
569ba55f2f5SFrançois Tigeot 
output_poll_execute(struct work_struct * work)570ba55f2f5SFrançois Tigeot static void output_poll_execute(struct work_struct *work)
571ba55f2f5SFrançois Tigeot {
572ba55f2f5SFrançois Tigeot 	struct delayed_work *delayed_work = to_delayed_work(work);
573ba55f2f5SFrançois Tigeot 	struct drm_device *dev = container_of(delayed_work, struct drm_device, mode_config.output_poll_work);
574ba55f2f5SFrançois Tigeot 	struct drm_connector *connector;
575a85cb24fSFrançois Tigeot 	struct drm_connector_list_iter conn_iter;
576ba55f2f5SFrançois Tigeot 	enum drm_connector_status old_status;
577c0e85e96SFrançois Tigeot 	bool repoll = false, changed;
578c0e85e96SFrançois Tigeot 
579c0e85e96SFrançois Tigeot 	/* Pick up any changes detected by the probe functions. */
580c0e85e96SFrançois Tigeot 	changed = dev->mode_config.delayed_event;
581c0e85e96SFrançois Tigeot 	dev->mode_config.delayed_event = false;
582ba55f2f5SFrançois Tigeot 
583ba55f2f5SFrançois Tigeot 	if (!drm_kms_helper_poll)
584c0e85e96SFrançois Tigeot 		goto out;
585ba55f2f5SFrançois Tigeot 
5864be47400SFrançois Tigeot 	if (!mutex_trylock(&dev->mode_config.mutex)) {
5874be47400SFrançois Tigeot 		repoll = true;
5884be47400SFrançois Tigeot 		goto out;
5894be47400SFrançois Tigeot 	}
5904be47400SFrançois Tigeot 
591a85cb24fSFrançois Tigeot 	drm_connector_list_iter_begin(dev, &conn_iter);
592a85cb24fSFrançois Tigeot 	drm_for_each_connector_iter(connector, &conn_iter) {
593ba55f2f5SFrançois Tigeot 		/* Ignore forced connectors. */
594ba55f2f5SFrançois Tigeot 		if (connector->force)
595ba55f2f5SFrançois Tigeot 			continue;
596ba55f2f5SFrançois Tigeot 
597ba55f2f5SFrançois Tigeot 		/* Ignore HPD capable connectors and connectors where we don't
598ba55f2f5SFrançois Tigeot 		 * want any hotplug detection at all for polling. */
599ba55f2f5SFrançois Tigeot 		if (!connector->polled || connector->polled == DRM_CONNECTOR_POLL_HPD)
600ba55f2f5SFrançois Tigeot 			continue;
601ba55f2f5SFrançois Tigeot 
602ba55f2f5SFrançois Tigeot 		old_status = connector->status;
603ba55f2f5SFrançois Tigeot 		/* if we are connected and don't want to poll for disconnect
604ba55f2f5SFrançois Tigeot 		   skip it */
605ba55f2f5SFrançois Tigeot 		if (old_status == connector_status_connected &&
606ba55f2f5SFrançois Tigeot 		    !(connector->polled & DRM_CONNECTOR_POLL_DISCONNECT))
607ba55f2f5SFrançois Tigeot 			continue;
608ba55f2f5SFrançois Tigeot 
60919c468b4SFrançois Tigeot 		repoll = true;
61019c468b4SFrançois Tigeot 
611a85cb24fSFrançois Tigeot 		connector->status = drm_helper_probe_detect(connector, NULL, false);
612ba55f2f5SFrançois Tigeot 		if (old_status != connector->status) {
613ba55f2f5SFrançois Tigeot 			const char *old, *new;
614ba55f2f5SFrançois Tigeot 
615c0e85e96SFrançois Tigeot 			/*
616c0e85e96SFrançois Tigeot 			 * The poll work sets force=false when calling detect so
617c0e85e96SFrançois Tigeot 			 * that drivers can avoid to do disruptive tests (e.g.
618c0e85e96SFrançois Tigeot 			 * when load detect cycles could cause flickering on
619c0e85e96SFrançois Tigeot 			 * other, running displays). This bears the risk that we
620c0e85e96SFrançois Tigeot 			 * flip-flop between unknown here in the poll work and
621c0e85e96SFrançois Tigeot 			 * the real state when userspace forces a full detect
622c0e85e96SFrançois Tigeot 			 * call after receiving a hotplug event due to this
623c0e85e96SFrançois Tigeot 			 * change.
624c0e85e96SFrançois Tigeot 			 *
625c0e85e96SFrançois Tigeot 			 * Hence clamp an unknown detect status to the old
626c0e85e96SFrançois Tigeot 			 * value.
627c0e85e96SFrançois Tigeot 			 */
628c0e85e96SFrançois Tigeot 			if (connector->status == connector_status_unknown) {
629c0e85e96SFrançois Tigeot 				connector->status = old_status;
630c0e85e96SFrançois Tigeot 				continue;
631c0e85e96SFrançois Tigeot 			}
632c0e85e96SFrançois Tigeot 
633ba55f2f5SFrançois Tigeot 			old = drm_get_connector_status_name(old_status);
634ba55f2f5SFrançois Tigeot 			new = drm_get_connector_status_name(connector->status);
635ba55f2f5SFrançois Tigeot 
636ba55f2f5SFrançois Tigeot 			DRM_DEBUG_KMS("[CONNECTOR:%d:%s] "
637ba55f2f5SFrançois Tigeot 				      "status updated from %s to %s\n",
638ba55f2f5SFrançois Tigeot 				      connector->base.id,
639ba55f2f5SFrançois Tigeot 				      connector->name,
640ba55f2f5SFrançois Tigeot 				      old, new);
641ba55f2f5SFrançois Tigeot 
642ba55f2f5SFrançois Tigeot 			changed = true;
643ba55f2f5SFrançois Tigeot 		}
644ba55f2f5SFrançois Tigeot 	}
645a85cb24fSFrançois Tigeot 	drm_connector_list_iter_end(&conn_iter);
646ba55f2f5SFrançois Tigeot 
647ba55f2f5SFrançois Tigeot 	mutex_unlock(&dev->mode_config.mutex);
648ba55f2f5SFrançois Tigeot 
649c0e85e96SFrançois Tigeot out:
650ba55f2f5SFrançois Tigeot 	if (changed)
651ba55f2f5SFrançois Tigeot 		drm_kms_helper_hotplug_event(dev);
652ba55f2f5SFrançois Tigeot 
653ba55f2f5SFrançois Tigeot 	if (repoll)
654ba55f2f5SFrançois Tigeot 		schedule_delayed_work(delayed_work, DRM_OUTPUT_POLL_PERIOD);
655ba55f2f5SFrançois Tigeot }
656ba55f2f5SFrançois Tigeot 
657ba55f2f5SFrançois Tigeot /**
658*3f2dd94aSFrançois Tigeot  * drm_kms_helper_is_poll_worker - is %current task an output poll worker?
659*3f2dd94aSFrançois Tigeot  *
660*3f2dd94aSFrançois Tigeot  * Determine if %current task is an output poll worker.  This can be used
661*3f2dd94aSFrançois Tigeot  * to select distinct code paths for output polling versus other contexts.
662*3f2dd94aSFrançois Tigeot  *
663*3f2dd94aSFrançois Tigeot  * One use case is to avoid a deadlock between the output poll worker and
664*3f2dd94aSFrançois Tigeot  * the autosuspend worker wherein the latter waits for polling to finish
665*3f2dd94aSFrançois Tigeot  * upon calling drm_kms_helper_poll_disable(), while the former waits for
666*3f2dd94aSFrançois Tigeot  * runtime suspend to finish upon calling pm_runtime_get_sync() in a
667*3f2dd94aSFrançois Tigeot  * connector ->detect hook.
668*3f2dd94aSFrançois Tigeot  */
drm_kms_helper_is_poll_worker(void)669*3f2dd94aSFrançois Tigeot bool drm_kms_helper_is_poll_worker(void)
670*3f2dd94aSFrançois Tigeot {
671*3f2dd94aSFrançois Tigeot #ifndef __DragonFly__
672*3f2dd94aSFrançois Tigeot 	struct work_struct *work = current_work();
673*3f2dd94aSFrançois Tigeot 
674*3f2dd94aSFrançois Tigeot 	return work && work->func == output_poll_execute;
675*3f2dd94aSFrançois Tigeot #else
676*3f2dd94aSFrançois Tigeot 	return false;
677*3f2dd94aSFrançois Tigeot #endif
678*3f2dd94aSFrançois Tigeot }
679*3f2dd94aSFrançois Tigeot EXPORT_SYMBOL(drm_kms_helper_is_poll_worker);
680*3f2dd94aSFrançois Tigeot 
681*3f2dd94aSFrançois Tigeot /**
682ba55f2f5SFrançois Tigeot  * drm_kms_helper_poll_disable - disable output polling
683ba55f2f5SFrançois Tigeot  * @dev: drm_device
684ba55f2f5SFrançois Tigeot  *
685ba55f2f5SFrançois Tigeot  * This function disables the output polling work.
686ba55f2f5SFrançois Tigeot  *
687ba55f2f5SFrançois Tigeot  * Drivers can call this helper from their device suspend implementation. It is
688a85cb24fSFrançois Tigeot  * not an error to call this even when output polling isn't enabled or already
689a85cb24fSFrançois Tigeot  * disabled. Polling is re-enabled by calling drm_kms_helper_poll_enable().
690a85cb24fSFrançois Tigeot  *
691a85cb24fSFrançois Tigeot  * Note that calls to enable and disable polling must be strictly ordered, which
692a85cb24fSFrançois Tigeot  * is automatically the case when they're only call from suspend/resume
693a85cb24fSFrançois Tigeot  * callbacks.
694ba55f2f5SFrançois Tigeot  */
drm_kms_helper_poll_disable(struct drm_device * dev)695ba55f2f5SFrançois Tigeot void drm_kms_helper_poll_disable(struct drm_device *dev)
696ba55f2f5SFrançois Tigeot {
697ba55f2f5SFrançois Tigeot 	if (!dev->mode_config.poll_enabled)
698ba55f2f5SFrançois Tigeot 		return;
699ba55f2f5SFrançois Tigeot 	cancel_delayed_work_sync(&dev->mode_config.output_poll_work);
700ba55f2f5SFrançois Tigeot }
701ba55f2f5SFrançois Tigeot EXPORT_SYMBOL(drm_kms_helper_poll_disable);
702ba55f2f5SFrançois Tigeot 
703ba55f2f5SFrançois Tigeot /**
704ba55f2f5SFrançois Tigeot  * drm_kms_helper_poll_init - initialize and enable output polling
705ba55f2f5SFrançois Tigeot  * @dev: drm_device
706ba55f2f5SFrançois Tigeot  *
707ba55f2f5SFrançois Tigeot  * This function intializes and then also enables output polling support for
708ba55f2f5SFrançois Tigeot  * @dev. Drivers which do not have reliable hotplug support in hardware can use
709ba55f2f5SFrançois Tigeot  * this helper infrastructure to regularly poll such connectors for changes in
710ba55f2f5SFrançois Tigeot  * their connection state.
711ba55f2f5SFrançois Tigeot  *
712ba55f2f5SFrançois Tigeot  * Drivers can control which connectors are polled by setting the
713ba55f2f5SFrançois Tigeot  * DRM_CONNECTOR_POLL_CONNECT and DRM_CONNECTOR_POLL_DISCONNECT flags. On
714ba55f2f5SFrançois Tigeot  * connectors where probing live outputs can result in visual distortion drivers
715ba55f2f5SFrançois Tigeot  * should not set the DRM_CONNECTOR_POLL_DISCONNECT flag to avoid this.
716ba55f2f5SFrançois Tigeot  * Connectors which have no flag or only DRM_CONNECTOR_POLL_HPD set are
717ba55f2f5SFrançois Tigeot  * completely ignored by the polling logic.
718ba55f2f5SFrançois Tigeot  *
719ba55f2f5SFrançois Tigeot  * Note that a connector can be both polled and probed from the hotplug handler,
720ba55f2f5SFrançois Tigeot  * in case the hotplug interrupt is known to be unreliable.
721ba55f2f5SFrançois Tigeot  */
drm_kms_helper_poll_init(struct drm_device * dev)722ba55f2f5SFrançois Tigeot void drm_kms_helper_poll_init(struct drm_device *dev)
723ba55f2f5SFrançois Tigeot {
724ba55f2f5SFrançois Tigeot 	INIT_DELAYED_WORK(&dev->mode_config.output_poll_work, output_poll_execute);
725ba55f2f5SFrançois Tigeot 	dev->mode_config.poll_enabled = true;
726ba55f2f5SFrançois Tigeot 
727ba55f2f5SFrançois Tigeot 	drm_kms_helper_poll_enable(dev);
728ba55f2f5SFrançois Tigeot }
729ba55f2f5SFrançois Tigeot EXPORT_SYMBOL(drm_kms_helper_poll_init);
730ba55f2f5SFrançois Tigeot 
731ba55f2f5SFrançois Tigeot /**
732ba55f2f5SFrançois Tigeot  * drm_kms_helper_poll_fini - disable output polling and clean it up
733ba55f2f5SFrançois Tigeot  * @dev: drm_device
734ba55f2f5SFrançois Tigeot  */
drm_kms_helper_poll_fini(struct drm_device * dev)735ba55f2f5SFrançois Tigeot void drm_kms_helper_poll_fini(struct drm_device *dev)
736ba55f2f5SFrançois Tigeot {
737ba55f2f5SFrançois Tigeot 	drm_kms_helper_poll_disable(dev);
738ba55f2f5SFrançois Tigeot }
739ba55f2f5SFrançois Tigeot EXPORT_SYMBOL(drm_kms_helper_poll_fini);
740ba55f2f5SFrançois Tigeot 
741ba55f2f5SFrançois Tigeot /**
742ba55f2f5SFrançois Tigeot  * drm_helper_hpd_irq_event - hotplug processing
743ba55f2f5SFrançois Tigeot  * @dev: drm_device
744ba55f2f5SFrançois Tigeot  *
745ba55f2f5SFrançois Tigeot  * Drivers can use this helper function to run a detect cycle on all connectors
746ba55f2f5SFrançois Tigeot  * which have the DRM_CONNECTOR_POLL_HPD flag set in their &polled member. All
747ba55f2f5SFrançois Tigeot  * other connectors are ignored, which is useful to avoid reprobing fixed
748ba55f2f5SFrançois Tigeot  * panels.
749ba55f2f5SFrançois Tigeot  *
750ba55f2f5SFrançois Tigeot  * This helper function is useful for drivers which can't or don't track hotplug
751ba55f2f5SFrançois Tigeot  * interrupts for each connector.
752ba55f2f5SFrançois Tigeot  *
753ba55f2f5SFrançois Tigeot  * Drivers which support hotplug interrupts for each connector individually and
754ba55f2f5SFrançois Tigeot  * which have a more fine-grained detect logic should bypass this code and
755ba55f2f5SFrançois Tigeot  * directly call drm_kms_helper_hotplug_event() in case the connector state
756ba55f2f5SFrançois Tigeot  * changed.
757ba55f2f5SFrançois Tigeot  *
758ba55f2f5SFrançois Tigeot  * This function must be called from process context with no mode
759ba55f2f5SFrançois Tigeot  * setting locks held.
760ba55f2f5SFrançois Tigeot  *
761ba55f2f5SFrançois Tigeot  * Note that a connector can be both polled and probed from the hotplug handler,
762ba55f2f5SFrançois Tigeot  * in case the hotplug interrupt is known to be unreliable.
763ba55f2f5SFrançois Tigeot  */
drm_helper_hpd_irq_event(struct drm_device * dev)764ba55f2f5SFrançois Tigeot bool drm_helper_hpd_irq_event(struct drm_device *dev)
765ba55f2f5SFrançois Tigeot {
766ba55f2f5SFrançois Tigeot 	struct drm_connector *connector;
767a85cb24fSFrançois Tigeot 	struct drm_connector_list_iter conn_iter;
768ba55f2f5SFrançois Tigeot 	enum drm_connector_status old_status;
769ba55f2f5SFrançois Tigeot 	bool changed = false;
770ba55f2f5SFrançois Tigeot 
771ba55f2f5SFrançois Tigeot 	if (!dev->mode_config.poll_enabled)
772ba55f2f5SFrançois Tigeot 		return false;
773ba55f2f5SFrançois Tigeot 
774ba55f2f5SFrançois Tigeot 	mutex_lock(&dev->mode_config.mutex);
775a85cb24fSFrançois Tigeot 	drm_connector_list_iter_begin(dev, &conn_iter);
776a85cb24fSFrançois Tigeot 	drm_for_each_connector_iter(connector, &conn_iter) {
777ba55f2f5SFrançois Tigeot 		/* Only handle HPD capable connectors. */
778ba55f2f5SFrançois Tigeot 		if (!(connector->polled & DRM_CONNECTOR_POLL_HPD))
779ba55f2f5SFrançois Tigeot 			continue;
780ba55f2f5SFrançois Tigeot 
781ba55f2f5SFrançois Tigeot 		old_status = connector->status;
782ba55f2f5SFrançois Tigeot 
783a85cb24fSFrançois Tigeot 		connector->status = drm_helper_probe_detect(connector, NULL, false);
784ba55f2f5SFrançois Tigeot 		DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
785ba55f2f5SFrançois Tigeot 			      connector->base.id,
786ba55f2f5SFrançois Tigeot 			      connector->name,
787ba55f2f5SFrançois Tigeot 			      drm_get_connector_status_name(old_status),
788ba55f2f5SFrançois Tigeot 			      drm_get_connector_status_name(connector->status));
789ba55f2f5SFrançois Tigeot 		if (old_status != connector->status)
790ba55f2f5SFrançois Tigeot 			changed = true;
791ba55f2f5SFrançois Tigeot 	}
792a85cb24fSFrançois Tigeot 	drm_connector_list_iter_end(&conn_iter);
793ba55f2f5SFrançois Tigeot 	mutex_unlock(&dev->mode_config.mutex);
794ba55f2f5SFrançois Tigeot 
795ba55f2f5SFrançois Tigeot 	if (changed)
796ba55f2f5SFrançois Tigeot 		drm_kms_helper_hotplug_event(dev);
797ba55f2f5SFrançois Tigeot 
798ba55f2f5SFrançois Tigeot 	return changed;
799ba55f2f5SFrançois Tigeot }
800ba55f2f5SFrançois Tigeot EXPORT_SYMBOL(drm_helper_hpd_irq_event);
801