xref: /openbmc/linux/drivers/gpu/drm/drm_probe_helper.c (revision 418eda8f3fe292782c150266d693d55d284c0c98)
18d754544SDaniel Vetter /*
28d754544SDaniel Vetter  * Copyright (c) 2006-2008 Intel Corporation
38d754544SDaniel Vetter  * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
48d754544SDaniel Vetter  *
58d754544SDaniel Vetter  * DRM core CRTC related functions
68d754544SDaniel Vetter  *
78d754544SDaniel Vetter  * Permission to use, copy, modify, distribute, and sell this software and its
88d754544SDaniel Vetter  * documentation for any purpose is hereby granted without fee, provided that
98d754544SDaniel Vetter  * the above copyright notice appear in all copies and that both that copyright
108d754544SDaniel Vetter  * notice and this permission notice appear in supporting documentation, and
118d754544SDaniel Vetter  * that the name of the copyright holders not be used in advertising or
128d754544SDaniel Vetter  * publicity pertaining to distribution of the software without specific,
138d754544SDaniel Vetter  * written prior permission.  The copyright holders make no representations
148d754544SDaniel Vetter  * about the suitability of this software for any purpose.  It is provided "as
158d754544SDaniel Vetter  * is" without express or implied warranty.
168d754544SDaniel Vetter  *
178d754544SDaniel Vetter  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
188d754544SDaniel Vetter  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
198d754544SDaniel Vetter  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
208d754544SDaniel Vetter  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
218d754544SDaniel Vetter  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
228d754544SDaniel Vetter  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
238d754544SDaniel Vetter  * OF THIS SOFTWARE.
248d754544SDaniel Vetter  *
258d754544SDaniel Vetter  * Authors:
268d754544SDaniel Vetter  *      Keith Packard
278d754544SDaniel Vetter  *	Eric Anholt <eric@anholt.net>
288d754544SDaniel Vetter  *      Dave Airlie <airlied@linux.ie>
298d754544SDaniel Vetter  *      Jesse Barnes <jesse.barnes@intel.com>
308d754544SDaniel Vetter  */
318d754544SDaniel Vetter 
328d754544SDaniel Vetter #include <linux/export.h>
338d754544SDaniel Vetter #include <linux/moduleparam.h>
348d754544SDaniel Vetter 
35ee68c743SBoris Brezillon #include <drm/drm_bridge.h>
36c76f0f7cSNoralf Trønnes #include <drm/drm_client.h>
378d754544SDaniel Vetter #include <drm/drm_crtc.h>
388d754544SDaniel Vetter #include <drm/drm_edid.h>
390500c04eSSam Ravnborg #include <drm/drm_fb_helper.h>
400500c04eSSam Ravnborg #include <drm/drm_fourcc.h>
41da251bf8SJose Abreu #include <drm/drm_modeset_helper_vtables.h>
420500c04eSSam Ravnborg #include <drm/drm_print.h>
43fcd70cd3SDaniel Vetter #include <drm/drm_probe_helper.h>
440500c04eSSam Ravnborg #include <drm/drm_sysfs.h>
45da251bf8SJose Abreu 
46da251bf8SJose Abreu #include "drm_crtc_helper_internal.h"
478d754544SDaniel Vetter 
488d754544SDaniel Vetter /**
498d754544SDaniel Vetter  * DOC: output probing helper overview
508d754544SDaniel Vetter  *
518d754544SDaniel Vetter  * This library provides some helper code for output probing. It provides an
526806cdf9SDaniel Vetter  * implementation of the core &drm_connector_funcs.fill_modes interface with
531bbfe9d1SDaniel Vetter  * drm_helper_probe_single_connector_modes().
548d754544SDaniel Vetter  *
558d754544SDaniel Vetter  * It also provides support for polling connectors with a work item and for
568d754544SDaniel Vetter  * generic hotplug interrupt handling where the driver doesn't or cannot keep
578d754544SDaniel Vetter  * track of a per-connector hpd interrupt.
588d754544SDaniel Vetter  *
598d754544SDaniel Vetter  * This helper library can be used independently of the modeset helper library.
608d754544SDaniel Vetter  * Drivers can also overwrite different parts e.g. use their own hotplug
618d754544SDaniel Vetter  * handling code to avoid probing unrelated outputs.
62092d01daSDaniel Vetter  *
63092d01daSDaniel Vetter  * The probe helpers share the function table structures with other display
64ea0dd85aSDaniel Vetter  * helper libraries. See &struct drm_connector_helper_funcs for the details.
658d754544SDaniel Vetter  */
668d754544SDaniel Vetter 
678d754544SDaniel Vetter static bool drm_kms_helper_poll = true;
688d754544SDaniel Vetter module_param_named(poll, drm_kms_helper_poll, bool, 0600);
698d754544SDaniel Vetter 
7005acaec3SVille Syrjälä static enum drm_mode_status
7105acaec3SVille Syrjälä drm_mode_validate_flag(const struct drm_display_mode *mode,
728d754544SDaniel Vetter 		       int flags)
738d754544SDaniel Vetter {
748d754544SDaniel Vetter 	if ((mode->flags & DRM_MODE_FLAG_INTERLACE) &&
758d754544SDaniel Vetter 	    !(flags & DRM_MODE_FLAG_INTERLACE))
7605acaec3SVille Syrjälä 		return MODE_NO_INTERLACE;
7705acaec3SVille Syrjälä 
788d754544SDaniel Vetter 	if ((mode->flags & DRM_MODE_FLAG_DBLSCAN) &&
798d754544SDaniel Vetter 	    !(flags & DRM_MODE_FLAG_DBLSCAN))
8005acaec3SVille Syrjälä 		return MODE_NO_DBLESCAN;
8105acaec3SVille Syrjälä 
828d754544SDaniel Vetter 	if ((mode->flags & DRM_MODE_FLAG_3D_MASK) &&
838d754544SDaniel Vetter 	    !(flags & DRM_MODE_FLAG_3D_MASK))
8405acaec3SVille Syrjälä 		return MODE_NO_STEREO;
858d754544SDaniel Vetter 
8605acaec3SVille Syrjälä 	return MODE_OK;
878d754544SDaniel Vetter }
888d754544SDaniel Vetter 
891c26b8e0SLyude Paul static int
9097f973d6SJose Abreu drm_mode_validate_pipeline(struct drm_display_mode *mode,
911c26b8e0SLyude Paul 			   struct drm_connector *connector,
921c26b8e0SLyude Paul 			   struct drm_modeset_acquire_ctx *ctx,
931c26b8e0SLyude Paul 			   enum drm_mode_status *status)
9497f973d6SJose Abreu {
9597f973d6SJose Abreu 	struct drm_device *dev = connector->dev;
9683aefbb8SVille Syrjälä 	struct drm_encoder *encoder;
971c26b8e0SLyude Paul 	int ret;
9897f973d6SJose Abreu 
9997f973d6SJose Abreu 	/* Step 1: Validate against connector */
1001c26b8e0SLyude Paul 	ret = drm_connector_mode_valid(connector, mode, ctx, status);
1011c26b8e0SLyude Paul 	if (ret || *status != MODE_OK)
10297f973d6SJose Abreu 		return ret;
10397f973d6SJose Abreu 
10497f973d6SJose Abreu 	/* Step 2: Validate against encoders and crtcs */
10562afb4adSJosé Roberto de Souza 	drm_connector_for_each_possible_encoder(connector, encoder) {
10635a61fe9SBoris Brezillon 		struct drm_bridge *bridge;
10797f973d6SJose Abreu 		struct drm_crtc *crtc;
10897f973d6SJose Abreu 
1091c26b8e0SLyude Paul 		*status = drm_encoder_mode_valid(encoder, mode);
1101c26b8e0SLyude Paul 		if (*status != MODE_OK) {
11197f973d6SJose Abreu 			/* No point in continuing for crtc check as this encoder
11297f973d6SJose Abreu 			 * will not accept the mode anyway. If all encoders
11397f973d6SJose Abreu 			 * reject the mode then, at exit, ret will not be
11497f973d6SJose Abreu 			 * MODE_OK. */
11597f973d6SJose Abreu 			continue;
11697f973d6SJose Abreu 		}
11797f973d6SJose Abreu 
11835a61fe9SBoris Brezillon 		bridge = drm_bridge_chain_get_first_bridge(encoder);
119*418eda8fSDave Airlie 		*status = drm_bridge_chain_mode_valid(bridge,
12012c683e1SLaurent Pinchart 						      &connector->display_info,
12112c683e1SLaurent Pinchart 						      mode);
1221c26b8e0SLyude Paul 		if (*status != MODE_OK) {
12397f973d6SJose Abreu 			/* There is also no point in continuing for crtc check
12497f973d6SJose Abreu 			 * here. */
12597f973d6SJose Abreu 			continue;
12697f973d6SJose Abreu 		}
12797f973d6SJose Abreu 
12897f973d6SJose Abreu 		drm_for_each_crtc(crtc, dev) {
12997f973d6SJose Abreu 			if (!drm_encoder_crtc_ok(encoder, crtc))
13097f973d6SJose Abreu 				continue;
13197f973d6SJose Abreu 
1321c26b8e0SLyude Paul 			*status = drm_crtc_mode_valid(crtc, mode);
1331c26b8e0SLyude Paul 			if (*status == MODE_OK) {
13497f973d6SJose Abreu 				/* If we get to this point there is at least
13597f973d6SJose Abreu 				 * one combination of encoder+crtc that works
13697f973d6SJose Abreu 				 * for this mode. Lets return now. */
1371c26b8e0SLyude Paul 				return 0;
13897f973d6SJose Abreu 			}
13997f973d6SJose Abreu 		}
14097f973d6SJose Abreu 	}
14197f973d6SJose Abreu 
1421c26b8e0SLyude Paul 	return 0;
14397f973d6SJose Abreu }
14497f973d6SJose Abreu 
145eaf99c74SChris Wilson static int drm_helper_probe_add_cmdline_mode(struct drm_connector *connector)
146eaf99c74SChris Wilson {
1475f0c3f99SChris Wilson 	struct drm_cmdline_mode *cmdline_mode;
148eaf99c74SChris Wilson 	struct drm_display_mode *mode;
149eaf99c74SChris Wilson 
1505f0c3f99SChris Wilson 	cmdline_mode = &connector->cmdline_mode;
1515f0c3f99SChris Wilson 	if (!cmdline_mode->specified)
152eaf99c74SChris Wilson 		return 0;
153eaf99c74SChris Wilson 
1545f0c3f99SChris Wilson 	/* Only add a GTF mode if we find no matching probed modes */
1555f0c3f99SChris Wilson 	list_for_each_entry(mode, &connector->probed_modes, head) {
1565f0c3f99SChris Wilson 		if (mode->hdisplay != cmdline_mode->xres ||
1575f0c3f99SChris Wilson 		    mode->vdisplay != cmdline_mode->yres)
1585f0c3f99SChris Wilson 			continue;
1595f0c3f99SChris Wilson 
1605f0c3f99SChris Wilson 		if (cmdline_mode->refresh_specified) {
1615f0c3f99SChris Wilson 			/* The probed mode's vrefresh is set until later */
1625f0c3f99SChris Wilson 			if (drm_mode_vrefresh(mode) != cmdline_mode->refresh)
1635f0c3f99SChris Wilson 				continue;
1645f0c3f99SChris Wilson 		}
1655f0c3f99SChris Wilson 
166ebdc02ddSHans de Goede 		/* Mark the matching mode as being preferred by the user */
167ebdc02ddSHans de Goede 		mode->type |= DRM_MODE_TYPE_USERDEF;
1685f0c3f99SChris Wilson 		return 0;
1695f0c3f99SChris Wilson 	}
1705f0c3f99SChris Wilson 
171eaf99c74SChris Wilson 	mode = drm_mode_create_from_cmdline_mode(connector->dev,
1725f0c3f99SChris Wilson 						 cmdline_mode);
173eaf99c74SChris Wilson 	if (mode == NULL)
174eaf99c74SChris Wilson 		return 0;
175eaf99c74SChris Wilson 
176eaf99c74SChris Wilson 	drm_mode_probed_add(connector, mode);
177eaf99c74SChris Wilson 	return 1;
178eaf99c74SChris Wilson }
179eaf99c74SChris Wilson 
180da251bf8SJose Abreu enum drm_mode_status drm_crtc_mode_valid(struct drm_crtc *crtc,
181da251bf8SJose Abreu 					 const struct drm_display_mode *mode)
182da251bf8SJose Abreu {
183da251bf8SJose Abreu 	const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
184da251bf8SJose Abreu 
185da251bf8SJose Abreu 	if (!crtc_funcs || !crtc_funcs->mode_valid)
186da251bf8SJose Abreu 		return MODE_OK;
187da251bf8SJose Abreu 
188da251bf8SJose Abreu 	return crtc_funcs->mode_valid(crtc, mode);
189da251bf8SJose Abreu }
190da251bf8SJose Abreu 
191da251bf8SJose Abreu enum drm_mode_status drm_encoder_mode_valid(struct drm_encoder *encoder,
192da251bf8SJose Abreu 					    const struct drm_display_mode *mode)
193da251bf8SJose Abreu {
194da251bf8SJose Abreu 	const struct drm_encoder_helper_funcs *encoder_funcs =
195da251bf8SJose Abreu 		encoder->helper_private;
196da251bf8SJose Abreu 
197da251bf8SJose Abreu 	if (!encoder_funcs || !encoder_funcs->mode_valid)
198da251bf8SJose Abreu 		return MODE_OK;
199da251bf8SJose Abreu 
200da251bf8SJose Abreu 	return encoder_funcs->mode_valid(encoder, mode);
201da251bf8SJose Abreu }
202da251bf8SJose Abreu 
2031c26b8e0SLyude Paul int
2041c26b8e0SLyude Paul drm_connector_mode_valid(struct drm_connector *connector,
2051c26b8e0SLyude Paul 			 struct drm_display_mode *mode,
2061c26b8e0SLyude Paul 			 struct drm_modeset_acquire_ctx *ctx,
2071c26b8e0SLyude Paul 			 enum drm_mode_status *status)
208da251bf8SJose Abreu {
209da251bf8SJose Abreu 	const struct drm_connector_helper_funcs *connector_funcs =
210da251bf8SJose Abreu 		connector->helper_private;
2111c26b8e0SLyude Paul 	int ret = 0;
212da251bf8SJose Abreu 
2131c26b8e0SLyude Paul 	if (!connector_funcs)
2141c26b8e0SLyude Paul 		*status = MODE_OK;
2151c26b8e0SLyude Paul 	else if (connector_funcs->mode_valid_ctx)
2161c26b8e0SLyude Paul 		ret = connector_funcs->mode_valid_ctx(connector, mode, ctx,
2171c26b8e0SLyude Paul 						      status);
2181c26b8e0SLyude Paul 	else if (connector_funcs->mode_valid)
2191c26b8e0SLyude Paul 		*status = connector_funcs->mode_valid(connector, mode);
2201c26b8e0SLyude Paul 	else
2211c26b8e0SLyude Paul 		*status = MODE_OK;
222da251bf8SJose Abreu 
2231c26b8e0SLyude Paul 	return ret;
224da251bf8SJose Abreu }
225da251bf8SJose Abreu 
2268c4ccc4aSDaniel Vetter #define DRM_OUTPUT_POLL_PERIOD (10*HZ)
2274ad640e9SEgbert Eich /**
228c4d79c22SDave Airlie  * drm_kms_helper_poll_enable - re-enable output polling.
2294ad640e9SEgbert Eich  * @dev: drm_device
2304ad640e9SEgbert Eich  *
231c4d79c22SDave Airlie  * This function re-enables the output polling work, after it has been
232c4d79c22SDave Airlie  * temporarily disabled using drm_kms_helper_poll_disable(), for example over
233c4d79c22SDave Airlie  * suspend/resume.
2344ad640e9SEgbert Eich  *
235c4d79c22SDave Airlie  * Drivers can call this helper from their device resume implementation. It is
236b66d0f34SNoralf Trønnes  * not an error to call this even when output polling isn't enabled.
237c4d79c22SDave Airlie  *
238c4d79c22SDave Airlie  * Note that calls to enable and disable polling must be strictly ordered, which
239c4d79c22SDave Airlie  * is automatically the case when they're only call from suspend/resume
240c4d79c22SDave Airlie  * callbacks.
2414ad640e9SEgbert Eich  */
242c4d79c22SDave Airlie void drm_kms_helper_poll_enable(struct drm_device *dev)
2438c4ccc4aSDaniel Vetter {
2448c4ccc4aSDaniel Vetter 	bool poll = false;
2458c4ccc4aSDaniel Vetter 	struct drm_connector *connector;
246c36a3254SDaniel Vetter 	struct drm_connector_list_iter conn_iter;
247339fd362SPeter Ujfalusi 	unsigned long delay = DRM_OUTPUT_POLL_PERIOD;
2488c4ccc4aSDaniel Vetter 
2498c4ccc4aSDaniel Vetter 	if (!dev->mode_config.poll_enabled || !drm_kms_helper_poll)
2508c4ccc4aSDaniel Vetter 		return;
2518c4ccc4aSDaniel Vetter 
252b982dab1SThierry Reding 	drm_connector_list_iter_begin(dev, &conn_iter);
253c36a3254SDaniel Vetter 	drm_for_each_connector_iter(connector, &conn_iter) {
2548c4ccc4aSDaniel Vetter 		if (connector->polled & (DRM_CONNECTOR_POLL_CONNECT |
2558c4ccc4aSDaniel Vetter 					 DRM_CONNECTOR_POLL_DISCONNECT))
2568c4ccc4aSDaniel Vetter 			poll = true;
2578c4ccc4aSDaniel Vetter 	}
258b982dab1SThierry Reding 	drm_connector_list_iter_end(&conn_iter);
2598c4ccc4aSDaniel Vetter 
260339fd362SPeter Ujfalusi 	if (dev->mode_config.delayed_event) {
26168f458eeSPeter Ujfalusi 		/*
26268f458eeSPeter Ujfalusi 		 * FIXME:
26368f458eeSPeter Ujfalusi 		 *
26468f458eeSPeter Ujfalusi 		 * Use short (1s) delay to handle the initial delayed event.
26568f458eeSPeter Ujfalusi 		 * This delay should not be needed, but Optimus/nouveau will
26668f458eeSPeter Ujfalusi 		 * fail in a mysterious way if the delayed event is handled as
26768f458eeSPeter Ujfalusi 		 * soon as possible like it is done in
26868f458eeSPeter Ujfalusi 		 * drm_helper_probe_single_connector_modes() in case the poll
26968f458eeSPeter Ujfalusi 		 * was enabled before.
27068f458eeSPeter Ujfalusi 		 */
271339fd362SPeter Ujfalusi 		poll = true;
27268f458eeSPeter Ujfalusi 		delay = HZ;
273339fd362SPeter Ujfalusi 	}
274339fd362SPeter Ujfalusi 
2758c4ccc4aSDaniel Vetter 	if (poll)
276339fd362SPeter Ujfalusi 		schedule_delayed_work(&dev->mode_config.output_poll_work, delay);
2778c4ccc4aSDaniel Vetter }
278c4d79c22SDave Airlie EXPORT_SYMBOL(drm_kms_helper_poll_enable);
2794ad640e9SEgbert Eich 
280949f0886SLaurent Pinchart static enum drm_connector_status
2816c5ed5aeSMaarten Lankhorst drm_helper_probe_detect_ctx(struct drm_connector *connector, bool force)
282949f0886SLaurent Pinchart {
2836c5ed5aeSMaarten Lankhorst 	const struct drm_connector_helper_funcs *funcs = connector->helper_private;
2846c5ed5aeSMaarten Lankhorst 	struct drm_modeset_acquire_ctx ctx;
2856c5ed5aeSMaarten Lankhorst 	int ret;
2866c5ed5aeSMaarten Lankhorst 
2876c5ed5aeSMaarten Lankhorst 	drm_modeset_acquire_init(&ctx, 0);
2886c5ed5aeSMaarten Lankhorst 
2896c5ed5aeSMaarten Lankhorst retry:
2906c5ed5aeSMaarten Lankhorst 	ret = drm_modeset_lock(&connector->dev->mode_config.connection_mutex, &ctx);
2916c5ed5aeSMaarten Lankhorst 	if (!ret) {
2926c5ed5aeSMaarten Lankhorst 		if (funcs->detect_ctx)
2936c5ed5aeSMaarten Lankhorst 			ret = funcs->detect_ctx(connector, &ctx, force);
2946c5ed5aeSMaarten Lankhorst 		else if (connector->funcs->detect)
2956c5ed5aeSMaarten Lankhorst 			ret = connector->funcs->detect(connector, force);
2966c5ed5aeSMaarten Lankhorst 		else
2976c5ed5aeSMaarten Lankhorst 			ret = connector_status_connected;
298949f0886SLaurent Pinchart 	}
299949f0886SLaurent Pinchart 
3006c5ed5aeSMaarten Lankhorst 	if (ret == -EDEADLK) {
3016c5ed5aeSMaarten Lankhorst 		drm_modeset_backoff(&ctx);
3026c5ed5aeSMaarten Lankhorst 		goto retry;
3036c5ed5aeSMaarten Lankhorst 	}
3046c5ed5aeSMaarten Lankhorst 
3056c5ed5aeSMaarten Lankhorst 	if (WARN_ON(ret < 0))
3066c5ed5aeSMaarten Lankhorst 		ret = connector_status_unknown;
3076c5ed5aeSMaarten Lankhorst 
3085186421cSStanislav Lisovskiy 	if (ret != connector->status)
3095186421cSStanislav Lisovskiy 		connector->epoch_counter += 1;
3105186421cSStanislav Lisovskiy 
3116c5ed5aeSMaarten Lankhorst 	drm_modeset_drop_locks(&ctx);
3126c5ed5aeSMaarten Lankhorst 	drm_modeset_acquire_fini(&ctx);
3136c5ed5aeSMaarten Lankhorst 
3146c5ed5aeSMaarten Lankhorst 	return ret;
3156c5ed5aeSMaarten Lankhorst }
3166c5ed5aeSMaarten Lankhorst 
3176c5ed5aeSMaarten Lankhorst /**
3186c5ed5aeSMaarten Lankhorst  * drm_helper_probe_detect - probe connector status
3196c5ed5aeSMaarten Lankhorst  * @connector: connector to probe
3206c5ed5aeSMaarten Lankhorst  * @ctx: acquire_ctx, or NULL to let this function handle locking.
3216c5ed5aeSMaarten Lankhorst  * @force: Whether destructive probe operations should be performed.
3226c5ed5aeSMaarten Lankhorst  *
3236c5ed5aeSMaarten Lankhorst  * This function calls the detect callbacks of the connector.
3246c5ed5aeSMaarten Lankhorst  * This function returns &drm_connector_status, or
3256c5ed5aeSMaarten Lankhorst  * if @ctx is set, it might also return -EDEADLK.
3266c5ed5aeSMaarten Lankhorst  */
3276c5ed5aeSMaarten Lankhorst int
3286c5ed5aeSMaarten Lankhorst drm_helper_probe_detect(struct drm_connector *connector,
3296c5ed5aeSMaarten Lankhorst 			struct drm_modeset_acquire_ctx *ctx,
3306c5ed5aeSMaarten Lankhorst 			bool force)
3316c5ed5aeSMaarten Lankhorst {
3326c5ed5aeSMaarten Lankhorst 	const struct drm_connector_helper_funcs *funcs = connector->helper_private;
3336c5ed5aeSMaarten Lankhorst 	struct drm_device *dev = connector->dev;
3346c5ed5aeSMaarten Lankhorst 	int ret;
3356c5ed5aeSMaarten Lankhorst 
3366c5ed5aeSMaarten Lankhorst 	if (!ctx)
3376c5ed5aeSMaarten Lankhorst 		return drm_helper_probe_detect_ctx(connector, force);
3386c5ed5aeSMaarten Lankhorst 
3396c5ed5aeSMaarten Lankhorst 	ret = drm_modeset_lock(&dev->mode_config.connection_mutex, ctx);
3406c5ed5aeSMaarten Lankhorst 	if (ret)
3416c5ed5aeSMaarten Lankhorst 		return ret;
3426c5ed5aeSMaarten Lankhorst 
3436c5ed5aeSMaarten Lankhorst 	if (funcs->detect_ctx)
3445186421cSStanislav Lisovskiy 		ret = funcs->detect_ctx(connector, ctx, force);
3456c5ed5aeSMaarten Lankhorst 	else if (connector->funcs->detect)
3465186421cSStanislav Lisovskiy 		ret = connector->funcs->detect(connector, force);
3476c5ed5aeSMaarten Lankhorst 	else
3485186421cSStanislav Lisovskiy 		ret = connector_status_connected;
3495186421cSStanislav Lisovskiy 
3505186421cSStanislav Lisovskiy 	if (ret != connector->status)
3515186421cSStanislav Lisovskiy 		connector->epoch_counter += 1;
3525186421cSStanislav Lisovskiy 
3535186421cSStanislav Lisovskiy 	return ret;
3546c5ed5aeSMaarten Lankhorst }
3556c5ed5aeSMaarten Lankhorst EXPORT_SYMBOL(drm_helper_probe_detect);
3566c5ed5aeSMaarten Lankhorst 
3576af3e656SVille Syrjälä /**
3586af3e656SVille Syrjälä  * drm_helper_probe_single_connector_modes - get complete set of display modes
3596af3e656SVille Syrjälä  * @connector: connector to probe
3606af3e656SVille Syrjälä  * @maxX: max width for modes
3616af3e656SVille Syrjälä  * @maxY: max height for modes
3626af3e656SVille Syrjälä  *
3636af3e656SVille Syrjälä  * Based on the helper callbacks implemented by @connector in struct
3646af3e656SVille Syrjälä  * &drm_connector_helper_funcs try to detect all valid modes.  Modes will first
3656af3e656SVille Syrjälä  * be added to the connector's probed_modes list, then culled (based on validity
3666af3e656SVille Syrjälä  * and the @maxX, @maxY parameters) and put into the normal modes list.
3676af3e656SVille Syrjälä  *
3686806cdf9SDaniel Vetter  * Intended to be used as a generic implementation of the
3696806cdf9SDaniel Vetter  * &drm_connector_funcs.fill_modes() vfunc for drivers that use the CRTC helpers
3706806cdf9SDaniel Vetter  * for output mode filtering and detection.
3716af3e656SVille Syrjälä  *
3725dec293bSVille Syrjälä  * The basic procedure is as follows
3736af3e656SVille Syrjälä  *
3745dec293bSVille Syrjälä  * 1. All modes currently on the connector's modes list are marked as stale
3756af3e656SVille Syrjälä  *
3765dec293bSVille Syrjälä  * 2. New modes are added to the connector's probed_modes list with
3775dec293bSVille Syrjälä  *    drm_mode_probed_add(). New modes start their life with status as OK.
3785dec293bSVille Syrjälä  *    Modes are added from a single source using the following priority order.
3795dec293bSVille Syrjälä  *
3806806cdf9SDaniel Vetter  *    - &drm_connector_helper_funcs.get_modes vfunc
3815dec293bSVille Syrjälä  *    - if the connector status is connector_status_connected, standard
3825dec293bSVille Syrjälä  *      VESA DMT modes up to 1024x768 are automatically added
3835dec293bSVille Syrjälä  *      (drm_add_modes_noedid())
3845dec293bSVille Syrjälä  *
3855dec293bSVille Syrjälä  *    Finally modes specified via the kernel command line (video=...) are
3865dec293bSVille Syrjälä  *    added in addition to what the earlier probes produced
3875dec293bSVille Syrjälä  *    (drm_helper_probe_add_cmdline_mode()). These modes are generated
3885dec293bSVille Syrjälä  *    using the VESA GTF/CVT formulas.
3895dec293bSVille Syrjälä  *
3905dec293bSVille Syrjälä  * 3. Modes are moved from the probed_modes list to the modes list. Potential
39197e14fbeSDaniel Vetter  *    duplicates are merged together (see drm_connector_list_update()).
3925dec293bSVille Syrjälä  *    After this step the probed_modes list will be empty again.
3935dec293bSVille Syrjälä  *
3945dec293bSVille Syrjälä  * 4. Any non-stale mode on the modes list then undergoes validation
3955dec293bSVille Syrjälä  *
3965dec293bSVille Syrjälä  *    - drm_mode_validate_basic() performs basic sanity checks
3975dec293bSVille Syrjälä  *    - drm_mode_validate_size() filters out modes larger than @maxX and @maxY
3985dec293bSVille Syrjälä  *      (if specified)
39957366a8dSMasahiro Yamada  *    - drm_mode_validate_flag() checks the modes against basic connector
40057366a8dSMasahiro Yamada  *      capabilities (interlace_allowed,doublescan_allowed,stereo_allowed)
4011c26b8e0SLyude Paul  *    - the optional &drm_connector_helper_funcs.mode_valid or
4021c26b8e0SLyude Paul  *      &drm_connector_helper_funcs.mode_valid_ctx helpers can perform driver
4031c26b8e0SLyude Paul  *      and/or sink specific checks
40497f973d6SJose Abreu  *    - the optional &drm_crtc_helper_funcs.mode_valid,
40597f973d6SJose Abreu  *      &drm_bridge_funcs.mode_valid and &drm_encoder_helper_funcs.mode_valid
40697f973d6SJose Abreu  *      helpers can perform driver and/or source specific checks which are also
40797f973d6SJose Abreu  *      enforced by the modeset/atomic helpers
4085dec293bSVille Syrjälä  *
4095dec293bSVille Syrjälä  * 5. Any mode whose status is not OK is pruned from the connector's modes list,
4105dec293bSVille Syrjälä  *    accompanied by a debug message indicating the reason for the mode's
4115dec293bSVille Syrjälä  *    rejection (see drm_mode_prune_invalid()).
4126af3e656SVille Syrjälä  *
4136af3e656SVille Syrjälä  * Returns:
4146af3e656SVille Syrjälä  * The number of modes found on @connector.
4156af3e656SVille Syrjälä  */
4166af3e656SVille Syrjälä int drm_helper_probe_single_connector_modes(struct drm_connector *connector,
4176af3e656SVille Syrjälä 					    uint32_t maxX, uint32_t maxY)
4188d754544SDaniel Vetter {
4198d754544SDaniel Vetter 	struct drm_device *dev = connector->dev;
4208d754544SDaniel Vetter 	struct drm_display_mode *mode;
421be26a66dSJani Nikula 	const struct drm_connector_helper_funcs *connector_funcs =
4228d754544SDaniel Vetter 		connector->helper_private;
4236c5ed5aeSMaarten Lankhorst 	int count = 0, ret;
4248d754544SDaniel Vetter 	int mode_flags = 0;
4258d754544SDaniel Vetter 	bool verbose_prune = true;
426162b6a57SDaniel Vetter 	enum drm_connector_status old_status;
4276c5ed5aeSMaarten Lankhorst 	struct drm_modeset_acquire_ctx ctx;
4288d754544SDaniel Vetter 
4298d754544SDaniel Vetter 	WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
4308d754544SDaniel Vetter 
4316c5ed5aeSMaarten Lankhorst 	drm_modeset_acquire_init(&ctx, 0);
4326c5ed5aeSMaarten Lankhorst 
4338d754544SDaniel Vetter 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", connector->base.id,
43425933820SJani Nikula 			connector->name);
4356c5ed5aeSMaarten Lankhorst 
4366c5ed5aeSMaarten Lankhorst retry:
4376c5ed5aeSMaarten Lankhorst 	ret = drm_modeset_lock(&dev->mode_config.connection_mutex, &ctx);
4386c5ed5aeSMaarten Lankhorst 	if (ret == -EDEADLK) {
4396c5ed5aeSMaarten Lankhorst 		drm_modeset_backoff(&ctx);
4406c5ed5aeSMaarten Lankhorst 		goto retry;
4416c5ed5aeSMaarten Lankhorst 	} else
4426c5ed5aeSMaarten Lankhorst 		WARN_ON(ret < 0);
4436c5ed5aeSMaarten Lankhorst 
4445ba89406SVille Syrjälä 	/* set all old modes to the stale state */
4458d754544SDaniel Vetter 	list_for_each_entry(mode, &connector->modes, head)
4465ba89406SVille Syrjälä 		mode->status = MODE_STALE;
4478d754544SDaniel Vetter 
448ed293f77SDaniel Vetter 	old_status = connector->status;
449ed293f77SDaniel Vetter 
4508d754544SDaniel Vetter 	if (connector->force) {
4512c4cc91bSPeter Hurley 		if (connector->force == DRM_FORCE_ON ||
4522c4cc91bSPeter Hurley 		    connector->force == DRM_FORCE_ON_DIGITAL)
4538d754544SDaniel Vetter 			connector->status = connector_status_connected;
4548d754544SDaniel Vetter 		else
4558d754544SDaniel Vetter 			connector->status = connector_status_disconnected;
4568d754544SDaniel Vetter 		if (connector->funcs->force)
4578d754544SDaniel Vetter 			connector->funcs->force(connector);
4588d754544SDaniel Vetter 	} else {
4596c5ed5aeSMaarten Lankhorst 		ret = drm_helper_probe_detect(connector, &ctx, true);
4606c5ed5aeSMaarten Lankhorst 
4616c5ed5aeSMaarten Lankhorst 		if (ret == -EDEADLK) {
4626c5ed5aeSMaarten Lankhorst 			drm_modeset_backoff(&ctx);
4636c5ed5aeSMaarten Lankhorst 			goto retry;
4646c5ed5aeSMaarten Lankhorst 		} else if (WARN(ret < 0, "Invalid return value %i for connector detection\n", ret))
4656c5ed5aeSMaarten Lankhorst 			ret = connector_status_unknown;
4666c5ed5aeSMaarten Lankhorst 
4676c5ed5aeSMaarten Lankhorst 		connector->status = ret;
468ed293f77SDaniel Vetter 	}
469162b6a57SDaniel Vetter 
470162b6a57SDaniel Vetter 	/*
471162b6a57SDaniel Vetter 	 * Normally either the driver's hpd code or the poll loop should
472162b6a57SDaniel Vetter 	 * pick up any changes and fire the hotplug event. But if
473162b6a57SDaniel Vetter 	 * userspace sneaks in a probe, we might miss a change. Hence
474162b6a57SDaniel Vetter 	 * check here, and if anything changed start the hotplug code.
475162b6a57SDaniel Vetter 	 */
476162b6a57SDaniel Vetter 	if (old_status != connector->status) {
4774e15f2a1SJani Nikula 		DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
478162b6a57SDaniel Vetter 			      connector->base.id,
479162b6a57SDaniel Vetter 			      connector->name,
4804e15f2a1SJani Nikula 			      drm_get_connector_status_name(old_status),
4814e15f2a1SJani Nikula 			      drm_get_connector_status_name(connector->status));
482162b6a57SDaniel Vetter 
483162b6a57SDaniel Vetter 		/*
484162b6a57SDaniel Vetter 		 * The hotplug event code might call into the fb
485162b6a57SDaniel Vetter 		 * helpers, and so expects that we do not hold any
486162b6a57SDaniel Vetter 		 * locks. Fire up the poll struct instead, it will
487162b6a57SDaniel Vetter 		 * disable itself again.
488162b6a57SDaniel Vetter 		 */
489162b6a57SDaniel Vetter 		dev->mode_config.delayed_event = true;
490162b6a57SDaniel Vetter 		if (dev->mode_config.poll_enabled)
491162b6a57SDaniel Vetter 			schedule_delayed_work(&dev->mode_config.output_poll_work,
492162b6a57SDaniel Vetter 					      0);
493162b6a57SDaniel Vetter 	}
4948d754544SDaniel Vetter 
4958d754544SDaniel Vetter 	/* Re-enable polling in case the global poll config changed. */
4968d754544SDaniel Vetter 	if (drm_kms_helper_poll != dev->mode_config.poll_running)
497c4d79c22SDave Airlie 		drm_kms_helper_poll_enable(dev);
4988d754544SDaniel Vetter 
4998d754544SDaniel Vetter 	dev->mode_config.poll_running = drm_kms_helper_poll;
5008d754544SDaniel Vetter 
5018d754544SDaniel Vetter 	if (connector->status == connector_status_disconnected) {
5028d754544SDaniel Vetter 		DRM_DEBUG_KMS("[CONNECTOR:%d:%s] disconnected\n",
50325933820SJani Nikula 			connector->base.id, connector->name);
504c555f023SDaniel Vetter 		drm_connector_update_edid_property(connector, NULL);
5058d754544SDaniel Vetter 		verbose_prune = false;
5068d754544SDaniel Vetter 		goto prune;
5078d754544SDaniel Vetter 	}
5088d754544SDaniel Vetter 
5098d754544SDaniel Vetter 	count = (*connector_funcs->get_modes)(connector);
5108d754544SDaniel Vetter 
51148eaeb76SJani Nikula 	/*
51248eaeb76SJani Nikula 	 * Fallback for when DDC probe failed in drm_get_edid() and thus skipped
51348eaeb76SJani Nikula 	 * override/firmware EDID.
51448eaeb76SJani Nikula 	 */
51548eaeb76SJani Nikula 	if (count == 0 && connector->status == connector_status_connected)
51648eaeb76SJani Nikula 		count = drm_add_override_edid_modes(connector);
51748eaeb76SJani Nikula 
5188d754544SDaniel Vetter 	if (count == 0 && connector->status == connector_status_connected)
5198d754544SDaniel Vetter 		count = drm_add_modes_noedid(connector, 1024, 768);
520eaf99c74SChris Wilson 	count += drm_helper_probe_add_cmdline_mode(connector);
5218d754544SDaniel Vetter 	if (count == 0)
5228d754544SDaniel Vetter 		goto prune;
5238d754544SDaniel Vetter 
52497e14fbeSDaniel Vetter 	drm_connector_list_update(connector);
5258d754544SDaniel Vetter 
5268d754544SDaniel Vetter 	if (connector->interlace_allowed)
5278d754544SDaniel Vetter 		mode_flags |= DRM_MODE_FLAG_INTERLACE;
5288d754544SDaniel Vetter 	if (connector->doublescan_allowed)
5298d754544SDaniel Vetter 		mode_flags |= DRM_MODE_FLAG_DBLSCAN;
5308d754544SDaniel Vetter 	if (connector->stereo_allowed)
5318d754544SDaniel Vetter 		mode_flags |= DRM_MODE_FLAG_3D_MASK;
5328d754544SDaniel Vetter 
5338d754544SDaniel Vetter 	list_for_each_entry(mode, &connector->modes, head) {
5341c26b8e0SLyude Paul 		if (mode->status != MODE_OK)
5351c26b8e0SLyude Paul 			continue;
5361c26b8e0SLyude Paul 
53775a655e0SVille Syrjälä 		mode->status = drm_mode_validate_driver(dev, mode);
5381c26b8e0SLyude Paul 		if (mode->status != MODE_OK)
5391c26b8e0SLyude Paul 			continue;
540abc0b144SVille Syrjälä 
54105acaec3SVille Syrjälä 		mode->status = drm_mode_validate_size(mode, maxX, maxY);
5421c26b8e0SLyude Paul 		if (mode->status != MODE_OK)
5431c26b8e0SLyude Paul 			continue;
54405acaec3SVille Syrjälä 
54505acaec3SVille Syrjälä 		mode->status = drm_mode_validate_flag(mode, mode_flags);
5461c26b8e0SLyude Paul 		if (mode->status != MODE_OK)
5471c26b8e0SLyude Paul 			continue;
54805acaec3SVille Syrjälä 
5491c26b8e0SLyude Paul 		ret = drm_mode_validate_pipeline(mode, connector, &ctx,
5501c26b8e0SLyude Paul 						 &mode->status);
5511c26b8e0SLyude Paul 		if (ret) {
5521c26b8e0SLyude Paul 			drm_dbg_kms(dev,
5531c26b8e0SLyude Paul 				    "drm_mode_validate_pipeline failed: %d\n",
5541c26b8e0SLyude Paul 				    ret);
555d8523153SShashank Sharma 
5561c26b8e0SLyude Paul 			if (drm_WARN_ON_ONCE(dev, ret != -EDEADLK)) {
5571c26b8e0SLyude Paul 				mode->status = MODE_ERROR;
5581c26b8e0SLyude Paul 			} else {
5591c26b8e0SLyude Paul 				drm_modeset_backoff(&ctx);
5601c26b8e0SLyude Paul 				goto retry;
5611c26b8e0SLyude Paul 			}
5621c26b8e0SLyude Paul 		}
5631c26b8e0SLyude Paul 
5641c26b8e0SLyude Paul 		if (mode->status != MODE_OK)
5651c26b8e0SLyude Paul 			continue;
5661c26b8e0SLyude Paul 		mode->status = drm_mode_validate_ycbcr420(mode, connector);
5678d754544SDaniel Vetter 	}
5688d754544SDaniel Vetter 
5698d754544SDaniel Vetter prune:
5708d754544SDaniel Vetter 	drm_mode_prune_invalid(dev, &connector->modes, verbose_prune);
5718d754544SDaniel Vetter 
5726c5ed5aeSMaarten Lankhorst 	drm_modeset_drop_locks(&ctx);
5736c5ed5aeSMaarten Lankhorst 	drm_modeset_acquire_fini(&ctx);
5746c5ed5aeSMaarten Lankhorst 
5758d754544SDaniel Vetter 	if (list_empty(&connector->modes))
5768d754544SDaniel Vetter 		return 0;
5778d754544SDaniel Vetter 
5788d754544SDaniel Vetter 	drm_mode_sort(&connector->modes);
5798d754544SDaniel Vetter 
5808d754544SDaniel Vetter 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s] probed modes :\n", connector->base.id,
58125933820SJani Nikula 			connector->name);
5828d754544SDaniel Vetter 	list_for_each_entry(mode, &connector->modes, head) {
5838d754544SDaniel Vetter 		drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
5848d754544SDaniel Vetter 		drm_mode_debug_printmodeline(mode);
5858d754544SDaniel Vetter 	}
5868d754544SDaniel Vetter 
5878d754544SDaniel Vetter 	return count;
5888d754544SDaniel Vetter }
5898d754544SDaniel Vetter EXPORT_SYMBOL(drm_helper_probe_single_connector_modes);
5908d754544SDaniel Vetter 
5918d754544SDaniel Vetter /**
5928d754544SDaniel Vetter  * drm_kms_helper_hotplug_event - fire off KMS hotplug events
5938d754544SDaniel Vetter  * @dev: drm_device whose connector state changed
5948d754544SDaniel Vetter  *
5958d754544SDaniel Vetter  * This function fires off the uevent for userspace and also calls the
5968d754544SDaniel Vetter  * output_poll_changed function, which is most commonly used to inform the fbdev
5978d754544SDaniel Vetter  * emulation code and allow it to update the fbcon output configuration.
5988d754544SDaniel Vetter  *
5998d754544SDaniel Vetter  * Drivers should call this from their hotplug handling code when a change is
6008d754544SDaniel Vetter  * detected. Note that this function does not do any output detection of its
6018d754544SDaniel Vetter  * own, like drm_helper_hpd_irq_event() does - this is assumed to be done by the
6028d754544SDaniel Vetter  * driver already.
6038d754544SDaniel Vetter  *
6048d754544SDaniel Vetter  * This function must be called from process context with no mode
6058d754544SDaniel Vetter  * setting locks held.
6068d754544SDaniel Vetter  */
6078d754544SDaniel Vetter void drm_kms_helper_hotplug_event(struct drm_device *dev)
6088d754544SDaniel Vetter {
6098d754544SDaniel Vetter 	/* send a uevent + call fbdev */
6108d754544SDaniel Vetter 	drm_sysfs_hotplug_event(dev);
6118d754544SDaniel Vetter 	if (dev->mode_config.funcs->output_poll_changed)
6128d754544SDaniel Vetter 		dev->mode_config.funcs->output_poll_changed(dev);
613c76f0f7cSNoralf Trønnes 
614c76f0f7cSNoralf Trønnes 	drm_client_dev_hotplug(dev);
6158d754544SDaniel Vetter }
6168d754544SDaniel Vetter EXPORT_SYMBOL(drm_kms_helper_hotplug_event);
6178d754544SDaniel Vetter 
6188d754544SDaniel Vetter static void output_poll_execute(struct work_struct *work)
6198d754544SDaniel Vetter {
6208d754544SDaniel Vetter 	struct delayed_work *delayed_work = to_delayed_work(work);
6218d754544SDaniel Vetter 	struct drm_device *dev = container_of(delayed_work, struct drm_device, mode_config.output_poll_work);
6228d754544SDaniel Vetter 	struct drm_connector *connector;
623c36a3254SDaniel Vetter 	struct drm_connector_list_iter conn_iter;
6248d754544SDaniel Vetter 	enum drm_connector_status old_status;
625162b6a57SDaniel Vetter 	bool repoll = false, changed;
626162b6a57SDaniel Vetter 
6273b295cb1SChris Wilson 	if (!dev->mode_config.poll_enabled)
6283b295cb1SChris Wilson 		return;
6293b295cb1SChris Wilson 
630162b6a57SDaniel Vetter 	/* Pick up any changes detected by the probe functions. */
631162b6a57SDaniel Vetter 	changed = dev->mode_config.delayed_event;
632162b6a57SDaniel Vetter 	dev->mode_config.delayed_event = false;
6338d754544SDaniel Vetter 
6348d754544SDaniel Vetter 	if (!drm_kms_helper_poll)
635162b6a57SDaniel Vetter 		goto out;
6368d754544SDaniel Vetter 
6371fe7841dSChris Wilson 	if (!mutex_trylock(&dev->mode_config.mutex)) {
6381fe7841dSChris Wilson 		repoll = true;
6391fe7841dSChris Wilson 		goto out;
6401fe7841dSChris Wilson 	}
6411fe7841dSChris Wilson 
642b982dab1SThierry Reding 	drm_connector_list_iter_begin(dev, &conn_iter);
643c36a3254SDaniel Vetter 	drm_for_each_connector_iter(connector, &conn_iter) {
6448d754544SDaniel Vetter 		/* Ignore forced connectors. */
6458d754544SDaniel Vetter 		if (connector->force)
6468d754544SDaniel Vetter 			continue;
6478d754544SDaniel Vetter 
6488d754544SDaniel Vetter 		/* Ignore HPD capable connectors and connectors where we don't
6498d754544SDaniel Vetter 		 * want any hotplug detection at all for polling. */
6508d754544SDaniel Vetter 		if (!connector->polled || connector->polled == DRM_CONNECTOR_POLL_HPD)
6518d754544SDaniel Vetter 			continue;
6528d754544SDaniel Vetter 
6538d754544SDaniel Vetter 		old_status = connector->status;
6548d754544SDaniel Vetter 		/* if we are connected and don't want to poll for disconnect
6558d754544SDaniel Vetter 		   skip it */
6568d754544SDaniel Vetter 		if (old_status == connector_status_connected &&
6578d754544SDaniel Vetter 		    !(connector->polled & DRM_CONNECTOR_POLL_DISCONNECT))
6588d754544SDaniel Vetter 			continue;
6598d754544SDaniel Vetter 
660a3c6d686SJosef Holzmayr 		repoll = true;
661a3c6d686SJosef Holzmayr 
6626c5ed5aeSMaarten Lankhorst 		connector->status = drm_helper_probe_detect(connector, NULL, false);
6638d754544SDaniel Vetter 		if (old_status != connector->status) {
6648d754544SDaniel Vetter 			const char *old, *new;
6658d754544SDaniel Vetter 
666b7703726SDaniel Vetter 			/*
667b7703726SDaniel Vetter 			 * The poll work sets force=false when calling detect so
668b7703726SDaniel Vetter 			 * that drivers can avoid to do disruptive tests (e.g.
669b7703726SDaniel Vetter 			 * when load detect cycles could cause flickering on
670b7703726SDaniel Vetter 			 * other, running displays). This bears the risk that we
671b7703726SDaniel Vetter 			 * flip-flop between unknown here in the poll work and
672b7703726SDaniel Vetter 			 * the real state when userspace forces a full detect
673b7703726SDaniel Vetter 			 * call after receiving a hotplug event due to this
674b7703726SDaniel Vetter 			 * change.
675b7703726SDaniel Vetter 			 *
676b7703726SDaniel Vetter 			 * Hence clamp an unknown detect status to the old
677b7703726SDaniel Vetter 			 * value.
678b7703726SDaniel Vetter 			 */
679b7703726SDaniel Vetter 			if (connector->status == connector_status_unknown) {
680b7703726SDaniel Vetter 				connector->status = old_status;
681b7703726SDaniel Vetter 				continue;
682b7703726SDaniel Vetter 			}
683b7703726SDaniel Vetter 
6848d754544SDaniel Vetter 			old = drm_get_connector_status_name(old_status);
6858d754544SDaniel Vetter 			new = drm_get_connector_status_name(connector->status);
6868d754544SDaniel Vetter 
6878d754544SDaniel Vetter 			DRM_DEBUG_KMS("[CONNECTOR:%d:%s] "
6888d754544SDaniel Vetter 				      "status updated from %s to %s\n",
6898d754544SDaniel Vetter 				      connector->base.id,
69025933820SJani Nikula 				      connector->name,
6918d754544SDaniel Vetter 				      old, new);
6928d754544SDaniel Vetter 
6938d754544SDaniel Vetter 			changed = true;
6948d754544SDaniel Vetter 		}
6958d754544SDaniel Vetter 	}
696b982dab1SThierry Reding 	drm_connector_list_iter_end(&conn_iter);
6978d754544SDaniel Vetter 
6988d754544SDaniel Vetter 	mutex_unlock(&dev->mode_config.mutex);
6998d754544SDaniel Vetter 
700162b6a57SDaniel Vetter out:
7018d754544SDaniel Vetter 	if (changed)
7028d754544SDaniel Vetter 		drm_kms_helper_hotplug_event(dev);
7038d754544SDaniel Vetter 
7048d754544SDaniel Vetter 	if (repoll)
7058d754544SDaniel Vetter 		schedule_delayed_work(delayed_work, DRM_OUTPUT_POLL_PERIOD);
7068d754544SDaniel Vetter }
7078d754544SDaniel Vetter 
7088d754544SDaniel Vetter /**
70925c058ccSLukas Wunner  * drm_kms_helper_is_poll_worker - is %current task an output poll worker?
71025c058ccSLukas Wunner  *
71125c058ccSLukas Wunner  * Determine if %current task is an output poll worker.  This can be used
71225c058ccSLukas Wunner  * to select distinct code paths for output polling versus other contexts.
71325c058ccSLukas Wunner  *
71425c058ccSLukas Wunner  * One use case is to avoid a deadlock between the output poll worker and
71525c058ccSLukas Wunner  * the autosuspend worker wherein the latter waits for polling to finish
71625c058ccSLukas Wunner  * upon calling drm_kms_helper_poll_disable(), while the former waits for
71725c058ccSLukas Wunner  * runtime suspend to finish upon calling pm_runtime_get_sync() in a
71825c058ccSLukas Wunner  * connector ->detect hook.
71925c058ccSLukas Wunner  */
72025c058ccSLukas Wunner bool drm_kms_helper_is_poll_worker(void)
72125c058ccSLukas Wunner {
72225c058ccSLukas Wunner 	struct work_struct *work = current_work();
72325c058ccSLukas Wunner 
72425c058ccSLukas Wunner 	return work && work->func == output_poll_execute;
72525c058ccSLukas Wunner }
72625c058ccSLukas Wunner EXPORT_SYMBOL(drm_kms_helper_is_poll_worker);
72725c058ccSLukas Wunner 
72825c058ccSLukas Wunner /**
7298d754544SDaniel Vetter  * drm_kms_helper_poll_disable - disable output polling
7308d754544SDaniel Vetter  * @dev: drm_device
7318d754544SDaniel Vetter  *
7328d754544SDaniel Vetter  * This function disables the output polling work.
7338d754544SDaniel Vetter  *
7348d754544SDaniel Vetter  * Drivers can call this helper from their device suspend implementation. It is
735c4d79c22SDave Airlie  * not an error to call this even when output polling isn't enabled or already
736c4d79c22SDave Airlie  * disabled. Polling is re-enabled by calling drm_kms_helper_poll_enable().
737c4d79c22SDave Airlie  *
738c4d79c22SDave Airlie  * Note that calls to enable and disable polling must be strictly ordered, which
739c4d79c22SDave Airlie  * is automatically the case when they're only call from suspend/resume
740c4d79c22SDave Airlie  * callbacks.
7418d754544SDaniel Vetter  */
7428d754544SDaniel Vetter void drm_kms_helper_poll_disable(struct drm_device *dev)
7438d754544SDaniel Vetter {
7448d754544SDaniel Vetter 	if (!dev->mode_config.poll_enabled)
7458d754544SDaniel Vetter 		return;
7468d754544SDaniel Vetter 	cancel_delayed_work_sync(&dev->mode_config.output_poll_work);
7478d754544SDaniel Vetter }
7488d754544SDaniel Vetter EXPORT_SYMBOL(drm_kms_helper_poll_disable);
7498d754544SDaniel Vetter 
7508d754544SDaniel Vetter /**
7518d754544SDaniel Vetter  * drm_kms_helper_poll_init - initialize and enable output polling
7528d754544SDaniel Vetter  * @dev: drm_device
7538d754544SDaniel Vetter  *
7548d754544SDaniel Vetter  * This function intializes and then also enables output polling support for
7558d754544SDaniel Vetter  * @dev. Drivers which do not have reliable hotplug support in hardware can use
7568d754544SDaniel Vetter  * this helper infrastructure to regularly poll such connectors for changes in
7578d754544SDaniel Vetter  * their connection state.
7588d754544SDaniel Vetter  *
7598d754544SDaniel Vetter  * Drivers can control which connectors are polled by setting the
7608d754544SDaniel Vetter  * DRM_CONNECTOR_POLL_CONNECT and DRM_CONNECTOR_POLL_DISCONNECT flags. On
7618d754544SDaniel Vetter  * connectors where probing live outputs can result in visual distortion drivers
7628d754544SDaniel Vetter  * should not set the DRM_CONNECTOR_POLL_DISCONNECT flag to avoid this.
7638d754544SDaniel Vetter  * Connectors which have no flag or only DRM_CONNECTOR_POLL_HPD set are
7648d754544SDaniel Vetter  * completely ignored by the polling logic.
7658d754544SDaniel Vetter  *
7668d754544SDaniel Vetter  * Note that a connector can be both polled and probed from the hotplug handler,
7678d754544SDaniel Vetter  * in case the hotplug interrupt is known to be unreliable.
7688d754544SDaniel Vetter  */
7698d754544SDaniel Vetter void drm_kms_helper_poll_init(struct drm_device *dev)
7708d754544SDaniel Vetter {
7718d754544SDaniel Vetter 	INIT_DELAYED_WORK(&dev->mode_config.output_poll_work, output_poll_execute);
7728d754544SDaniel Vetter 	dev->mode_config.poll_enabled = true;
7738d754544SDaniel Vetter 
7748d754544SDaniel Vetter 	drm_kms_helper_poll_enable(dev);
7758d754544SDaniel Vetter }
7768d754544SDaniel Vetter EXPORT_SYMBOL(drm_kms_helper_poll_init);
7778d754544SDaniel Vetter 
7788d754544SDaniel Vetter /**
7798d754544SDaniel Vetter  * drm_kms_helper_poll_fini - disable output polling and clean it up
7808d754544SDaniel Vetter  * @dev: drm_device
7818d754544SDaniel Vetter  */
7828d754544SDaniel Vetter void drm_kms_helper_poll_fini(struct drm_device *dev)
7838d754544SDaniel Vetter {
7843b295cb1SChris Wilson 	if (!dev->mode_config.poll_enabled)
7853b295cb1SChris Wilson 		return;
7863b295cb1SChris Wilson 
7873b295cb1SChris Wilson 	dev->mode_config.poll_enabled = false;
7883b295cb1SChris Wilson 	cancel_delayed_work_sync(&dev->mode_config.output_poll_work);
7898d754544SDaniel Vetter }
7908d754544SDaniel Vetter EXPORT_SYMBOL(drm_kms_helper_poll_fini);
7918d754544SDaniel Vetter 
7928d754544SDaniel Vetter /**
7938d754544SDaniel Vetter  * drm_helper_hpd_irq_event - hotplug processing
7948d754544SDaniel Vetter  * @dev: drm_device
7958d754544SDaniel Vetter  *
7968d754544SDaniel Vetter  * Drivers can use this helper function to run a detect cycle on all connectors
7978d754544SDaniel Vetter  * which have the DRM_CONNECTOR_POLL_HPD flag set in their &polled member. All
7988d754544SDaniel Vetter  * other connectors are ignored, which is useful to avoid reprobing fixed
7998d754544SDaniel Vetter  * panels.
8008d754544SDaniel Vetter  *
8018d754544SDaniel Vetter  * This helper function is useful for drivers which can't or don't track hotplug
8028d754544SDaniel Vetter  * interrupts for each connector.
8038d754544SDaniel Vetter  *
8048d754544SDaniel Vetter  * Drivers which support hotplug interrupts for each connector individually and
8058d754544SDaniel Vetter  * which have a more fine-grained detect logic should bypass this code and
8068d754544SDaniel Vetter  * directly call drm_kms_helper_hotplug_event() in case the connector state
8078d754544SDaniel Vetter  * changed.
8088d754544SDaniel Vetter  *
8098d754544SDaniel Vetter  * This function must be called from process context with no mode
8108d754544SDaniel Vetter  * setting locks held.
8118d754544SDaniel Vetter  *
8128d754544SDaniel Vetter  * Note that a connector can be both polled and probed from the hotplug handler,
8138d754544SDaniel Vetter  * in case the hotplug interrupt is known to be unreliable.
8148d754544SDaniel Vetter  */
8158d754544SDaniel Vetter bool drm_helper_hpd_irq_event(struct drm_device *dev)
8168d754544SDaniel Vetter {
8178d754544SDaniel Vetter 	struct drm_connector *connector;
818c36a3254SDaniel Vetter 	struct drm_connector_list_iter conn_iter;
8198d754544SDaniel Vetter 	enum drm_connector_status old_status;
8208d754544SDaniel Vetter 	bool changed = false;
8215186421cSStanislav Lisovskiy 	u64 old_epoch_counter;
8228d754544SDaniel Vetter 
8238d754544SDaniel Vetter 	if (!dev->mode_config.poll_enabled)
8248d754544SDaniel Vetter 		return false;
8258d754544SDaniel Vetter 
8268d754544SDaniel Vetter 	mutex_lock(&dev->mode_config.mutex);
827b982dab1SThierry Reding 	drm_connector_list_iter_begin(dev, &conn_iter);
828c36a3254SDaniel Vetter 	drm_for_each_connector_iter(connector, &conn_iter) {
8298d754544SDaniel Vetter 		/* Only handle HPD capable connectors. */
8308d754544SDaniel Vetter 		if (!(connector->polled & DRM_CONNECTOR_POLL_HPD))
8318d754544SDaniel Vetter 			continue;
8328d754544SDaniel Vetter 
8338d754544SDaniel Vetter 		old_status = connector->status;
8348d754544SDaniel Vetter 
8355186421cSStanislav Lisovskiy 		old_epoch_counter = connector->epoch_counter;
8365186421cSStanislav Lisovskiy 
8375186421cSStanislav Lisovskiy 		DRM_DEBUG_KMS("[CONNECTOR:%d:%s] Old epoch counter %llu\n", connector->base.id,
8385186421cSStanislav Lisovskiy 			      connector->name,
8395186421cSStanislav Lisovskiy 			      old_epoch_counter);
8405186421cSStanislav Lisovskiy 
8416c5ed5aeSMaarten Lankhorst 		connector->status = drm_helper_probe_detect(connector, NULL, false);
8428d754544SDaniel Vetter 		DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
8438d754544SDaniel Vetter 			      connector->base.id,
84425933820SJani Nikula 			      connector->name,
8458d754544SDaniel Vetter 			      drm_get_connector_status_name(old_status),
8468d754544SDaniel Vetter 			      drm_get_connector_status_name(connector->status));
8475186421cSStanislav Lisovskiy 
8485186421cSStanislav Lisovskiy 		DRM_DEBUG_KMS("[CONNECTOR:%d:%s] New epoch counter %llu\n",
8495186421cSStanislav Lisovskiy 			      connector->base.id,
8505186421cSStanislav Lisovskiy 			      connector->name,
8515186421cSStanislav Lisovskiy 			      connector->epoch_counter);
8525186421cSStanislav Lisovskiy 
8535186421cSStanislav Lisovskiy 		/*
8545186421cSStanislav Lisovskiy 		 * Check if epoch counter had changed, meaning that we need
8555186421cSStanislav Lisovskiy 		 * to send a uevent.
8565186421cSStanislav Lisovskiy 		 */
8575186421cSStanislav Lisovskiy 		if (old_epoch_counter != connector->epoch_counter)
8588d754544SDaniel Vetter 			changed = true;
8595186421cSStanislav Lisovskiy 
8608d754544SDaniel Vetter 	}
861b982dab1SThierry Reding 	drm_connector_list_iter_end(&conn_iter);
8628d754544SDaniel Vetter 	mutex_unlock(&dev->mode_config.mutex);
8638d754544SDaniel Vetter 
8645186421cSStanislav Lisovskiy 	if (changed) {
8658d754544SDaniel Vetter 		drm_kms_helper_hotplug_event(dev);
8665186421cSStanislav Lisovskiy 		DRM_DEBUG_KMS("Sent hotplug event\n");
8675186421cSStanislav Lisovskiy 	}
8688d754544SDaniel Vetter 
8698d754544SDaniel Vetter 	return changed;
8708d754544SDaniel Vetter }
8718d754544SDaniel Vetter EXPORT_SYMBOL(drm_helper_hpd_irq_event);
872