xref: /openbmc/linux/drivers/gpu/drm/drm_probe_helper.c (revision 3b295cb1a411d9c82bbfaa66bc17a8508716ed07)
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 
35c76f0f7cSNoralf Trønnes #include <drm/drm_client.h>
368d754544SDaniel Vetter #include <drm/drm_crtc.h>
378d754544SDaniel Vetter #include <drm/drm_edid.h>
380500c04eSSam Ravnborg #include <drm/drm_fb_helper.h>
390500c04eSSam Ravnborg #include <drm/drm_fourcc.h>
40da251bf8SJose Abreu #include <drm/drm_modeset_helper_vtables.h>
410500c04eSSam Ravnborg #include <drm/drm_print.h>
42fcd70cd3SDaniel Vetter #include <drm/drm_probe_helper.h>
430500c04eSSam Ravnborg #include <drm/drm_sysfs.h>
44da251bf8SJose Abreu 
45da251bf8SJose Abreu #include "drm_crtc_helper_internal.h"
468d754544SDaniel Vetter 
478d754544SDaniel Vetter /**
488d754544SDaniel Vetter  * DOC: output probing helper overview
498d754544SDaniel Vetter  *
508d754544SDaniel Vetter  * This library provides some helper code for output probing. It provides an
516806cdf9SDaniel Vetter  * implementation of the core &drm_connector_funcs.fill_modes interface with
521bbfe9d1SDaniel Vetter  * drm_helper_probe_single_connector_modes().
538d754544SDaniel Vetter  *
548d754544SDaniel Vetter  * It also provides support for polling connectors with a work item and for
558d754544SDaniel Vetter  * generic hotplug interrupt handling where the driver doesn't or cannot keep
568d754544SDaniel Vetter  * track of a per-connector hpd interrupt.
578d754544SDaniel Vetter  *
588d754544SDaniel Vetter  * This helper library can be used independently of the modeset helper library.
598d754544SDaniel Vetter  * Drivers can also overwrite different parts e.g. use their own hotplug
608d754544SDaniel Vetter  * handling code to avoid probing unrelated outputs.
61092d01daSDaniel Vetter  *
62092d01daSDaniel Vetter  * The probe helpers share the function table structures with other display
63ea0dd85aSDaniel Vetter  * helper libraries. See &struct drm_connector_helper_funcs for the details.
648d754544SDaniel Vetter  */
658d754544SDaniel Vetter 
668d754544SDaniel Vetter static bool drm_kms_helper_poll = true;
678d754544SDaniel Vetter module_param_named(poll, drm_kms_helper_poll, bool, 0600);
688d754544SDaniel Vetter 
6905acaec3SVille Syrjälä static enum drm_mode_status
7005acaec3SVille Syrjälä drm_mode_validate_flag(const struct drm_display_mode *mode,
718d754544SDaniel Vetter 		       int flags)
728d754544SDaniel Vetter {
738d754544SDaniel Vetter 	if ((mode->flags & DRM_MODE_FLAG_INTERLACE) &&
748d754544SDaniel Vetter 	    !(flags & DRM_MODE_FLAG_INTERLACE))
7505acaec3SVille Syrjälä 		return MODE_NO_INTERLACE;
7605acaec3SVille Syrjälä 
778d754544SDaniel Vetter 	if ((mode->flags & DRM_MODE_FLAG_DBLSCAN) &&
788d754544SDaniel Vetter 	    !(flags & DRM_MODE_FLAG_DBLSCAN))
7905acaec3SVille Syrjälä 		return MODE_NO_DBLESCAN;
8005acaec3SVille Syrjälä 
818d754544SDaniel Vetter 	if ((mode->flags & DRM_MODE_FLAG_3D_MASK) &&
828d754544SDaniel Vetter 	    !(flags & DRM_MODE_FLAG_3D_MASK))
8305acaec3SVille Syrjälä 		return MODE_NO_STEREO;
848d754544SDaniel Vetter 
8505acaec3SVille Syrjälä 	return MODE_OK;
868d754544SDaniel Vetter }
878d754544SDaniel Vetter 
8897f973d6SJose Abreu static enum drm_mode_status
8997f973d6SJose Abreu drm_mode_validate_pipeline(struct drm_display_mode *mode,
9097f973d6SJose Abreu 			    struct drm_connector *connector)
9197f973d6SJose Abreu {
9297f973d6SJose Abreu 	struct drm_device *dev = connector->dev;
9397f973d6SJose Abreu 	enum drm_mode_status ret = MODE_OK;
9483aefbb8SVille Syrjälä 	struct drm_encoder *encoder;
9583aefbb8SVille Syrjälä 	int i;
9697f973d6SJose Abreu 
9797f973d6SJose Abreu 	/* Step 1: Validate against connector */
9897f973d6SJose Abreu 	ret = drm_connector_mode_valid(connector, mode);
9997f973d6SJose Abreu 	if (ret != MODE_OK)
10097f973d6SJose Abreu 		return ret;
10197f973d6SJose Abreu 
10297f973d6SJose Abreu 	/* Step 2: Validate against encoders and crtcs */
10383aefbb8SVille Syrjälä 	drm_connector_for_each_possible_encoder(connector, encoder, i) {
10497f973d6SJose Abreu 		struct drm_crtc *crtc;
10597f973d6SJose Abreu 
10697f973d6SJose Abreu 		ret = drm_encoder_mode_valid(encoder, mode);
10797f973d6SJose Abreu 		if (ret != MODE_OK) {
10897f973d6SJose Abreu 			/* No point in continuing for crtc check as this encoder
10997f973d6SJose Abreu 			 * will not accept the mode anyway. If all encoders
11097f973d6SJose Abreu 			 * reject the mode then, at exit, ret will not be
11197f973d6SJose Abreu 			 * MODE_OK. */
11297f973d6SJose Abreu 			continue;
11397f973d6SJose Abreu 		}
11497f973d6SJose Abreu 
11597f973d6SJose Abreu 		ret = drm_bridge_mode_valid(encoder->bridge, mode);
11697f973d6SJose Abreu 		if (ret != MODE_OK) {
11797f973d6SJose Abreu 			/* There is also no point in continuing for crtc check
11897f973d6SJose Abreu 			 * here. */
11997f973d6SJose Abreu 			continue;
12097f973d6SJose Abreu 		}
12197f973d6SJose Abreu 
12297f973d6SJose Abreu 		drm_for_each_crtc(crtc, dev) {
12397f973d6SJose Abreu 			if (!drm_encoder_crtc_ok(encoder, crtc))
12497f973d6SJose Abreu 				continue;
12597f973d6SJose Abreu 
12697f973d6SJose Abreu 			ret = drm_crtc_mode_valid(crtc, mode);
12797f973d6SJose Abreu 			if (ret == MODE_OK) {
12897f973d6SJose Abreu 				/* If we get to this point there is at least
12997f973d6SJose Abreu 				 * one combination of encoder+crtc that works
13097f973d6SJose Abreu 				 * for this mode. Lets return now. */
13197f973d6SJose Abreu 				return ret;
13297f973d6SJose Abreu 			}
13397f973d6SJose Abreu 		}
13497f973d6SJose Abreu 	}
13597f973d6SJose Abreu 
13697f973d6SJose Abreu 	return ret;
13797f973d6SJose Abreu }
13897f973d6SJose Abreu 
139eaf99c74SChris Wilson static int drm_helper_probe_add_cmdline_mode(struct drm_connector *connector)
140eaf99c74SChris Wilson {
1415f0c3f99SChris Wilson 	struct drm_cmdline_mode *cmdline_mode;
142eaf99c74SChris Wilson 	struct drm_display_mode *mode;
143eaf99c74SChris Wilson 
1445f0c3f99SChris Wilson 	cmdline_mode = &connector->cmdline_mode;
1455f0c3f99SChris Wilson 	if (!cmdline_mode->specified)
146eaf99c74SChris Wilson 		return 0;
147eaf99c74SChris Wilson 
1485f0c3f99SChris Wilson 	/* Only add a GTF mode if we find no matching probed modes */
1495f0c3f99SChris Wilson 	list_for_each_entry(mode, &connector->probed_modes, head) {
1505f0c3f99SChris Wilson 		if (mode->hdisplay != cmdline_mode->xres ||
1515f0c3f99SChris Wilson 		    mode->vdisplay != cmdline_mode->yres)
1525f0c3f99SChris Wilson 			continue;
1535f0c3f99SChris Wilson 
1545f0c3f99SChris Wilson 		if (cmdline_mode->refresh_specified) {
1555f0c3f99SChris Wilson 			/* The probed mode's vrefresh is set until later */
1565f0c3f99SChris Wilson 			if (drm_mode_vrefresh(mode) != cmdline_mode->refresh)
1575f0c3f99SChris Wilson 				continue;
1585f0c3f99SChris Wilson 		}
1595f0c3f99SChris Wilson 
1605f0c3f99SChris Wilson 		return 0;
1615f0c3f99SChris Wilson 	}
1625f0c3f99SChris Wilson 
163eaf99c74SChris Wilson 	mode = drm_mode_create_from_cmdline_mode(connector->dev,
1645f0c3f99SChris Wilson 						 cmdline_mode);
165eaf99c74SChris Wilson 	if (mode == NULL)
166eaf99c74SChris Wilson 		return 0;
167eaf99c74SChris Wilson 
168eaf99c74SChris Wilson 	drm_mode_probed_add(connector, mode);
169eaf99c74SChris Wilson 	return 1;
170eaf99c74SChris Wilson }
171eaf99c74SChris Wilson 
172da251bf8SJose Abreu enum drm_mode_status drm_crtc_mode_valid(struct drm_crtc *crtc,
173da251bf8SJose Abreu 					 const struct drm_display_mode *mode)
174da251bf8SJose Abreu {
175da251bf8SJose Abreu 	const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
176da251bf8SJose Abreu 
177da251bf8SJose Abreu 	if (!crtc_funcs || !crtc_funcs->mode_valid)
178da251bf8SJose Abreu 		return MODE_OK;
179da251bf8SJose Abreu 
180da251bf8SJose Abreu 	return crtc_funcs->mode_valid(crtc, mode);
181da251bf8SJose Abreu }
182da251bf8SJose Abreu 
183da251bf8SJose Abreu enum drm_mode_status drm_encoder_mode_valid(struct drm_encoder *encoder,
184da251bf8SJose Abreu 					    const struct drm_display_mode *mode)
185da251bf8SJose Abreu {
186da251bf8SJose Abreu 	const struct drm_encoder_helper_funcs *encoder_funcs =
187da251bf8SJose Abreu 		encoder->helper_private;
188da251bf8SJose Abreu 
189da251bf8SJose Abreu 	if (!encoder_funcs || !encoder_funcs->mode_valid)
190da251bf8SJose Abreu 		return MODE_OK;
191da251bf8SJose Abreu 
192da251bf8SJose Abreu 	return encoder_funcs->mode_valid(encoder, mode);
193da251bf8SJose Abreu }
194da251bf8SJose Abreu 
195da251bf8SJose Abreu enum drm_mode_status drm_connector_mode_valid(struct drm_connector *connector,
196da251bf8SJose Abreu 					      struct drm_display_mode *mode)
197da251bf8SJose Abreu {
198da251bf8SJose Abreu 	const struct drm_connector_helper_funcs *connector_funcs =
199da251bf8SJose Abreu 		connector->helper_private;
200da251bf8SJose Abreu 
201da251bf8SJose Abreu 	if (!connector_funcs || !connector_funcs->mode_valid)
202da251bf8SJose Abreu 		return MODE_OK;
203da251bf8SJose Abreu 
204da251bf8SJose Abreu 	return connector_funcs->mode_valid(connector, mode);
205da251bf8SJose Abreu }
206da251bf8SJose Abreu 
2078c4ccc4aSDaniel Vetter #define DRM_OUTPUT_POLL_PERIOD (10*HZ)
2084ad640e9SEgbert Eich /**
209c4d79c22SDave Airlie  * drm_kms_helper_poll_enable - re-enable output polling.
2104ad640e9SEgbert Eich  * @dev: drm_device
2114ad640e9SEgbert Eich  *
212c4d79c22SDave Airlie  * This function re-enables the output polling work, after it has been
213c4d79c22SDave Airlie  * temporarily disabled using drm_kms_helper_poll_disable(), for example over
214c4d79c22SDave Airlie  * suspend/resume.
2154ad640e9SEgbert Eich  *
216c4d79c22SDave Airlie  * Drivers can call this helper from their device resume implementation. It is
217b66d0f34SNoralf Trønnes  * not an error to call this even when output polling isn't enabled.
218c4d79c22SDave Airlie  *
219c4d79c22SDave Airlie  * Note that calls to enable and disable polling must be strictly ordered, which
220c4d79c22SDave Airlie  * is automatically the case when they're only call from suspend/resume
221c4d79c22SDave Airlie  * callbacks.
2224ad640e9SEgbert Eich  */
223c4d79c22SDave Airlie void drm_kms_helper_poll_enable(struct drm_device *dev)
2248c4ccc4aSDaniel Vetter {
2258c4ccc4aSDaniel Vetter 	bool poll = false;
2268c4ccc4aSDaniel Vetter 	struct drm_connector *connector;
227c36a3254SDaniel Vetter 	struct drm_connector_list_iter conn_iter;
228339fd362SPeter Ujfalusi 	unsigned long delay = DRM_OUTPUT_POLL_PERIOD;
2298c4ccc4aSDaniel Vetter 
2308c4ccc4aSDaniel Vetter 	if (!dev->mode_config.poll_enabled || !drm_kms_helper_poll)
2318c4ccc4aSDaniel Vetter 		return;
2328c4ccc4aSDaniel Vetter 
233b982dab1SThierry Reding 	drm_connector_list_iter_begin(dev, &conn_iter);
234c36a3254SDaniel Vetter 	drm_for_each_connector_iter(connector, &conn_iter) {
2358c4ccc4aSDaniel Vetter 		if (connector->polled & (DRM_CONNECTOR_POLL_CONNECT |
2368c4ccc4aSDaniel Vetter 					 DRM_CONNECTOR_POLL_DISCONNECT))
2378c4ccc4aSDaniel Vetter 			poll = true;
2388c4ccc4aSDaniel Vetter 	}
239b982dab1SThierry Reding 	drm_connector_list_iter_end(&conn_iter);
2408c4ccc4aSDaniel Vetter 
241339fd362SPeter Ujfalusi 	if (dev->mode_config.delayed_event) {
24268f458eeSPeter Ujfalusi 		/*
24368f458eeSPeter Ujfalusi 		 * FIXME:
24468f458eeSPeter Ujfalusi 		 *
24568f458eeSPeter Ujfalusi 		 * Use short (1s) delay to handle the initial delayed event.
24668f458eeSPeter Ujfalusi 		 * This delay should not be needed, but Optimus/nouveau will
24768f458eeSPeter Ujfalusi 		 * fail in a mysterious way if the delayed event is handled as
24868f458eeSPeter Ujfalusi 		 * soon as possible like it is done in
24968f458eeSPeter Ujfalusi 		 * drm_helper_probe_single_connector_modes() in case the poll
25068f458eeSPeter Ujfalusi 		 * was enabled before.
25168f458eeSPeter Ujfalusi 		 */
252339fd362SPeter Ujfalusi 		poll = true;
25368f458eeSPeter Ujfalusi 		delay = HZ;
254339fd362SPeter Ujfalusi 	}
255339fd362SPeter Ujfalusi 
2568c4ccc4aSDaniel Vetter 	if (poll)
257339fd362SPeter Ujfalusi 		schedule_delayed_work(&dev->mode_config.output_poll_work, delay);
2588c4ccc4aSDaniel Vetter }
259c4d79c22SDave Airlie EXPORT_SYMBOL(drm_kms_helper_poll_enable);
2604ad640e9SEgbert Eich 
261949f0886SLaurent Pinchart static enum drm_connector_status
2626c5ed5aeSMaarten Lankhorst drm_helper_probe_detect_ctx(struct drm_connector *connector, bool force)
263949f0886SLaurent Pinchart {
2646c5ed5aeSMaarten Lankhorst 	const struct drm_connector_helper_funcs *funcs = connector->helper_private;
2656c5ed5aeSMaarten Lankhorst 	struct drm_modeset_acquire_ctx ctx;
2666c5ed5aeSMaarten Lankhorst 	int ret;
2676c5ed5aeSMaarten Lankhorst 
2686c5ed5aeSMaarten Lankhorst 	drm_modeset_acquire_init(&ctx, 0);
2696c5ed5aeSMaarten Lankhorst 
2706c5ed5aeSMaarten Lankhorst retry:
2716c5ed5aeSMaarten Lankhorst 	ret = drm_modeset_lock(&connector->dev->mode_config.connection_mutex, &ctx);
2726c5ed5aeSMaarten Lankhorst 	if (!ret) {
2736c5ed5aeSMaarten Lankhorst 		if (funcs->detect_ctx)
2746c5ed5aeSMaarten Lankhorst 			ret = funcs->detect_ctx(connector, &ctx, force);
2756c5ed5aeSMaarten Lankhorst 		else if (connector->funcs->detect)
2766c5ed5aeSMaarten Lankhorst 			ret = connector->funcs->detect(connector, force);
2776c5ed5aeSMaarten Lankhorst 		else
2786c5ed5aeSMaarten Lankhorst 			ret = connector_status_connected;
279949f0886SLaurent Pinchart 	}
280949f0886SLaurent Pinchart 
2816c5ed5aeSMaarten Lankhorst 	if (ret == -EDEADLK) {
2826c5ed5aeSMaarten Lankhorst 		drm_modeset_backoff(&ctx);
2836c5ed5aeSMaarten Lankhorst 		goto retry;
2846c5ed5aeSMaarten Lankhorst 	}
2856c5ed5aeSMaarten Lankhorst 
2866c5ed5aeSMaarten Lankhorst 	if (WARN_ON(ret < 0))
2876c5ed5aeSMaarten Lankhorst 		ret = connector_status_unknown;
2886c5ed5aeSMaarten Lankhorst 
2896c5ed5aeSMaarten Lankhorst 	drm_modeset_drop_locks(&ctx);
2906c5ed5aeSMaarten Lankhorst 	drm_modeset_acquire_fini(&ctx);
2916c5ed5aeSMaarten Lankhorst 
2926c5ed5aeSMaarten Lankhorst 	return ret;
2936c5ed5aeSMaarten Lankhorst }
2946c5ed5aeSMaarten Lankhorst 
2956c5ed5aeSMaarten Lankhorst /**
2966c5ed5aeSMaarten Lankhorst  * drm_helper_probe_detect - probe connector status
2976c5ed5aeSMaarten Lankhorst  * @connector: connector to probe
2986c5ed5aeSMaarten Lankhorst  * @ctx: acquire_ctx, or NULL to let this function handle locking.
2996c5ed5aeSMaarten Lankhorst  * @force: Whether destructive probe operations should be performed.
3006c5ed5aeSMaarten Lankhorst  *
3016c5ed5aeSMaarten Lankhorst  * This function calls the detect callbacks of the connector.
3026c5ed5aeSMaarten Lankhorst  * This function returns &drm_connector_status, or
3036c5ed5aeSMaarten Lankhorst  * if @ctx is set, it might also return -EDEADLK.
3046c5ed5aeSMaarten Lankhorst  */
3056c5ed5aeSMaarten Lankhorst int
3066c5ed5aeSMaarten Lankhorst drm_helper_probe_detect(struct drm_connector *connector,
3076c5ed5aeSMaarten Lankhorst 			struct drm_modeset_acquire_ctx *ctx,
3086c5ed5aeSMaarten Lankhorst 			bool force)
3096c5ed5aeSMaarten Lankhorst {
3106c5ed5aeSMaarten Lankhorst 	const struct drm_connector_helper_funcs *funcs = connector->helper_private;
3116c5ed5aeSMaarten Lankhorst 	struct drm_device *dev = connector->dev;
3126c5ed5aeSMaarten Lankhorst 	int ret;
3136c5ed5aeSMaarten Lankhorst 
3146c5ed5aeSMaarten Lankhorst 	if (!ctx)
3156c5ed5aeSMaarten Lankhorst 		return drm_helper_probe_detect_ctx(connector, force);
3166c5ed5aeSMaarten Lankhorst 
3176c5ed5aeSMaarten Lankhorst 	ret = drm_modeset_lock(&dev->mode_config.connection_mutex, ctx);
3186c5ed5aeSMaarten Lankhorst 	if (ret)
3196c5ed5aeSMaarten Lankhorst 		return ret;
3206c5ed5aeSMaarten Lankhorst 
3216c5ed5aeSMaarten Lankhorst 	if (funcs->detect_ctx)
3226c5ed5aeSMaarten Lankhorst 		return funcs->detect_ctx(connector, ctx, force);
3236c5ed5aeSMaarten Lankhorst 	else if (connector->funcs->detect)
3246c5ed5aeSMaarten Lankhorst 		return connector->funcs->detect(connector, force);
3256c5ed5aeSMaarten Lankhorst 	else
3266c5ed5aeSMaarten Lankhorst 		return connector_status_connected;
3276c5ed5aeSMaarten Lankhorst }
3286c5ed5aeSMaarten Lankhorst EXPORT_SYMBOL(drm_helper_probe_detect);
3296c5ed5aeSMaarten Lankhorst 
3306af3e656SVille Syrjälä /**
3316af3e656SVille Syrjälä  * drm_helper_probe_single_connector_modes - get complete set of display modes
3326af3e656SVille Syrjälä  * @connector: connector to probe
3336af3e656SVille Syrjälä  * @maxX: max width for modes
3346af3e656SVille Syrjälä  * @maxY: max height for modes
3356af3e656SVille Syrjälä  *
3366af3e656SVille Syrjälä  * Based on the helper callbacks implemented by @connector in struct
3376af3e656SVille Syrjälä  * &drm_connector_helper_funcs try to detect all valid modes.  Modes will first
3386af3e656SVille Syrjälä  * be added to the connector's probed_modes list, then culled (based on validity
3396af3e656SVille Syrjälä  * and the @maxX, @maxY parameters) and put into the normal modes list.
3406af3e656SVille Syrjälä  *
3416806cdf9SDaniel Vetter  * Intended to be used as a generic implementation of the
3426806cdf9SDaniel Vetter  * &drm_connector_funcs.fill_modes() vfunc for drivers that use the CRTC helpers
3436806cdf9SDaniel Vetter  * for output mode filtering and detection.
3446af3e656SVille Syrjälä  *
3455dec293bSVille Syrjälä  * The basic procedure is as follows
3466af3e656SVille Syrjälä  *
3475dec293bSVille Syrjälä  * 1. All modes currently on the connector's modes list are marked as stale
3486af3e656SVille Syrjälä  *
3495dec293bSVille Syrjälä  * 2. New modes are added to the connector's probed_modes list with
3505dec293bSVille Syrjälä  *    drm_mode_probed_add(). New modes start their life with status as OK.
3515dec293bSVille Syrjälä  *    Modes are added from a single source using the following priority order.
3525dec293bSVille Syrjälä  *
3536806cdf9SDaniel Vetter  *    - &drm_connector_helper_funcs.get_modes vfunc
3545dec293bSVille Syrjälä  *    - if the connector status is connector_status_connected, standard
3555dec293bSVille Syrjälä  *      VESA DMT modes up to 1024x768 are automatically added
3565dec293bSVille Syrjälä  *      (drm_add_modes_noedid())
3575dec293bSVille Syrjälä  *
3585dec293bSVille Syrjälä  *    Finally modes specified via the kernel command line (video=...) are
3595dec293bSVille Syrjälä  *    added in addition to what the earlier probes produced
3605dec293bSVille Syrjälä  *    (drm_helper_probe_add_cmdline_mode()). These modes are generated
3615dec293bSVille Syrjälä  *    using the VESA GTF/CVT formulas.
3625dec293bSVille Syrjälä  *
3635dec293bSVille Syrjälä  * 3. Modes are moved from the probed_modes list to the modes list. Potential
36497e14fbeSDaniel Vetter  *    duplicates are merged together (see drm_connector_list_update()).
3655dec293bSVille Syrjälä  *    After this step the probed_modes list will be empty again.
3665dec293bSVille Syrjälä  *
3675dec293bSVille Syrjälä  * 4. Any non-stale mode on the modes list then undergoes validation
3685dec293bSVille Syrjälä  *
3695dec293bSVille Syrjälä  *    - drm_mode_validate_basic() performs basic sanity checks
3705dec293bSVille Syrjälä  *    - drm_mode_validate_size() filters out modes larger than @maxX and @maxY
3715dec293bSVille Syrjälä  *      (if specified)
37257366a8dSMasahiro Yamada  *    - drm_mode_validate_flag() checks the modes against basic connector
37357366a8dSMasahiro Yamada  *      capabilities (interlace_allowed,doublescan_allowed,stereo_allowed)
3746806cdf9SDaniel Vetter  *    - the optional &drm_connector_helper_funcs.mode_valid helper can perform
37597f973d6SJose Abreu  *      driver and/or sink specific checks
37697f973d6SJose Abreu  *    - the optional &drm_crtc_helper_funcs.mode_valid,
37797f973d6SJose Abreu  *      &drm_bridge_funcs.mode_valid and &drm_encoder_helper_funcs.mode_valid
37897f973d6SJose Abreu  *      helpers can perform driver and/or source specific checks which are also
37997f973d6SJose Abreu  *      enforced by the modeset/atomic helpers
3805dec293bSVille Syrjälä  *
3815dec293bSVille Syrjälä  * 5. Any mode whose status is not OK is pruned from the connector's modes list,
3825dec293bSVille Syrjälä  *    accompanied by a debug message indicating the reason for the mode's
3835dec293bSVille Syrjälä  *    rejection (see drm_mode_prune_invalid()).
3846af3e656SVille Syrjälä  *
3856af3e656SVille Syrjälä  * Returns:
3866af3e656SVille Syrjälä  * The number of modes found on @connector.
3876af3e656SVille Syrjälä  */
3886af3e656SVille Syrjälä int drm_helper_probe_single_connector_modes(struct drm_connector *connector,
3896af3e656SVille Syrjälä 					    uint32_t maxX, uint32_t maxY)
3908d754544SDaniel Vetter {
3918d754544SDaniel Vetter 	struct drm_device *dev = connector->dev;
3928d754544SDaniel Vetter 	struct drm_display_mode *mode;
393be26a66dSJani Nikula 	const struct drm_connector_helper_funcs *connector_funcs =
3948d754544SDaniel Vetter 		connector->helper_private;
3956c5ed5aeSMaarten Lankhorst 	int count = 0, ret;
3968d754544SDaniel Vetter 	int mode_flags = 0;
3978d754544SDaniel Vetter 	bool verbose_prune = true;
398162b6a57SDaniel Vetter 	enum drm_connector_status old_status;
3996c5ed5aeSMaarten Lankhorst 	struct drm_modeset_acquire_ctx ctx;
4008d754544SDaniel Vetter 
4018d754544SDaniel Vetter 	WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
4028d754544SDaniel Vetter 
4036c5ed5aeSMaarten Lankhorst 	drm_modeset_acquire_init(&ctx, 0);
4046c5ed5aeSMaarten Lankhorst 
4058d754544SDaniel Vetter 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", connector->base.id,
40625933820SJani Nikula 			connector->name);
4076c5ed5aeSMaarten Lankhorst 
4086c5ed5aeSMaarten Lankhorst retry:
4096c5ed5aeSMaarten Lankhorst 	ret = drm_modeset_lock(&dev->mode_config.connection_mutex, &ctx);
4106c5ed5aeSMaarten Lankhorst 	if (ret == -EDEADLK) {
4116c5ed5aeSMaarten Lankhorst 		drm_modeset_backoff(&ctx);
4126c5ed5aeSMaarten Lankhorst 		goto retry;
4136c5ed5aeSMaarten Lankhorst 	} else
4146c5ed5aeSMaarten Lankhorst 		WARN_ON(ret < 0);
4156c5ed5aeSMaarten Lankhorst 
4165ba89406SVille Syrjälä 	/* set all old modes to the stale state */
4178d754544SDaniel Vetter 	list_for_each_entry(mode, &connector->modes, head)
4185ba89406SVille Syrjälä 		mode->status = MODE_STALE;
4198d754544SDaniel Vetter 
420ed293f77SDaniel Vetter 	old_status = connector->status;
421ed293f77SDaniel Vetter 
4228d754544SDaniel Vetter 	if (connector->force) {
4232c4cc91bSPeter Hurley 		if (connector->force == DRM_FORCE_ON ||
4242c4cc91bSPeter Hurley 		    connector->force == DRM_FORCE_ON_DIGITAL)
4258d754544SDaniel Vetter 			connector->status = connector_status_connected;
4268d754544SDaniel Vetter 		else
4278d754544SDaniel Vetter 			connector->status = connector_status_disconnected;
4288d754544SDaniel Vetter 		if (connector->funcs->force)
4298d754544SDaniel Vetter 			connector->funcs->force(connector);
4308d754544SDaniel Vetter 	} else {
4316c5ed5aeSMaarten Lankhorst 		ret = drm_helper_probe_detect(connector, &ctx, true);
4326c5ed5aeSMaarten Lankhorst 
4336c5ed5aeSMaarten Lankhorst 		if (ret == -EDEADLK) {
4346c5ed5aeSMaarten Lankhorst 			drm_modeset_backoff(&ctx);
4356c5ed5aeSMaarten Lankhorst 			goto retry;
4366c5ed5aeSMaarten Lankhorst 		} else if (WARN(ret < 0, "Invalid return value %i for connector detection\n", ret))
4376c5ed5aeSMaarten Lankhorst 			ret = connector_status_unknown;
4386c5ed5aeSMaarten Lankhorst 
4396c5ed5aeSMaarten Lankhorst 		connector->status = ret;
440ed293f77SDaniel Vetter 	}
441162b6a57SDaniel Vetter 
442162b6a57SDaniel Vetter 	/*
443162b6a57SDaniel Vetter 	 * Normally either the driver's hpd code or the poll loop should
444162b6a57SDaniel Vetter 	 * pick up any changes and fire the hotplug event. But if
445162b6a57SDaniel Vetter 	 * userspace sneaks in a probe, we might miss a change. Hence
446162b6a57SDaniel Vetter 	 * check here, and if anything changed start the hotplug code.
447162b6a57SDaniel Vetter 	 */
448162b6a57SDaniel Vetter 	if (old_status != connector->status) {
4494e15f2a1SJani Nikula 		DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
450162b6a57SDaniel Vetter 			      connector->base.id,
451162b6a57SDaniel Vetter 			      connector->name,
4524e15f2a1SJani Nikula 			      drm_get_connector_status_name(old_status),
4534e15f2a1SJani Nikula 			      drm_get_connector_status_name(connector->status));
454162b6a57SDaniel Vetter 
455162b6a57SDaniel Vetter 		/*
456162b6a57SDaniel Vetter 		 * The hotplug event code might call into the fb
457162b6a57SDaniel Vetter 		 * helpers, and so expects that we do not hold any
458162b6a57SDaniel Vetter 		 * locks. Fire up the poll struct instead, it will
459162b6a57SDaniel Vetter 		 * disable itself again.
460162b6a57SDaniel Vetter 		 */
461162b6a57SDaniel Vetter 		dev->mode_config.delayed_event = true;
462162b6a57SDaniel Vetter 		if (dev->mode_config.poll_enabled)
463162b6a57SDaniel Vetter 			schedule_delayed_work(&dev->mode_config.output_poll_work,
464162b6a57SDaniel Vetter 					      0);
465162b6a57SDaniel Vetter 	}
4668d754544SDaniel Vetter 
4678d754544SDaniel Vetter 	/* Re-enable polling in case the global poll config changed. */
4688d754544SDaniel Vetter 	if (drm_kms_helper_poll != dev->mode_config.poll_running)
469c4d79c22SDave Airlie 		drm_kms_helper_poll_enable(dev);
4708d754544SDaniel Vetter 
4718d754544SDaniel Vetter 	dev->mode_config.poll_running = drm_kms_helper_poll;
4728d754544SDaniel Vetter 
4738d754544SDaniel Vetter 	if (connector->status == connector_status_disconnected) {
4748d754544SDaniel Vetter 		DRM_DEBUG_KMS("[CONNECTOR:%d:%s] disconnected\n",
47525933820SJani Nikula 			connector->base.id, connector->name);
476c555f023SDaniel Vetter 		drm_connector_update_edid_property(connector, NULL);
4778d754544SDaniel Vetter 		verbose_prune = false;
4788d754544SDaniel Vetter 		goto prune;
4798d754544SDaniel Vetter 	}
4808d754544SDaniel Vetter 
4818d754544SDaniel Vetter 	count = (*connector_funcs->get_modes)(connector);
4828d754544SDaniel Vetter 
4838d754544SDaniel Vetter 	if (count == 0 && connector->status == connector_status_connected)
4848d754544SDaniel Vetter 		count = drm_add_modes_noedid(connector, 1024, 768);
485eaf99c74SChris Wilson 	count += drm_helper_probe_add_cmdline_mode(connector);
4868d754544SDaniel Vetter 	if (count == 0)
4878d754544SDaniel Vetter 		goto prune;
4888d754544SDaniel Vetter 
48997e14fbeSDaniel Vetter 	drm_connector_list_update(connector);
4908d754544SDaniel Vetter 
4918d754544SDaniel Vetter 	if (connector->interlace_allowed)
4928d754544SDaniel Vetter 		mode_flags |= DRM_MODE_FLAG_INTERLACE;
4938d754544SDaniel Vetter 	if (connector->doublescan_allowed)
4948d754544SDaniel Vetter 		mode_flags |= DRM_MODE_FLAG_DBLSCAN;
4958d754544SDaniel Vetter 	if (connector->stereo_allowed)
4968d754544SDaniel Vetter 		mode_flags |= DRM_MODE_FLAG_3D_MASK;
4978d754544SDaniel Vetter 
4988d754544SDaniel Vetter 	list_for_each_entry(mode, &connector->modes, head) {
499be8719a6SVille Syrjälä 		if (mode->status == MODE_OK)
50075a655e0SVille Syrjälä 			mode->status = drm_mode_validate_driver(dev, mode);
501abc0b144SVille Syrjälä 
502abc0b144SVille Syrjälä 		if (mode->status == MODE_OK)
50305acaec3SVille Syrjälä 			mode->status = drm_mode_validate_size(mode, maxX, maxY);
50405acaec3SVille Syrjälä 
50505acaec3SVille Syrjälä 		if (mode->status == MODE_OK)
50605acaec3SVille Syrjälä 			mode->status = drm_mode_validate_flag(mode, mode_flags);
50705acaec3SVille Syrjälä 
50897f973d6SJose Abreu 		if (mode->status == MODE_OK)
50997f973d6SJose Abreu 			mode->status = drm_mode_validate_pipeline(mode,
51097f973d6SJose Abreu 								  connector);
511d8523153SShashank Sharma 
512d8523153SShashank Sharma 		if (mode->status == MODE_OK)
513d8523153SShashank Sharma 			mode->status = drm_mode_validate_ycbcr420(mode,
514d8523153SShashank Sharma 								  connector);
5158d754544SDaniel Vetter 	}
5168d754544SDaniel Vetter 
5178d754544SDaniel Vetter prune:
5188d754544SDaniel Vetter 	drm_mode_prune_invalid(dev, &connector->modes, verbose_prune);
5198d754544SDaniel Vetter 
5206c5ed5aeSMaarten Lankhorst 	drm_modeset_drop_locks(&ctx);
5216c5ed5aeSMaarten Lankhorst 	drm_modeset_acquire_fini(&ctx);
5226c5ed5aeSMaarten Lankhorst 
5238d754544SDaniel Vetter 	if (list_empty(&connector->modes))
5248d754544SDaniel Vetter 		return 0;
5258d754544SDaniel Vetter 
5268d754544SDaniel Vetter 	list_for_each_entry(mode, &connector->modes, head)
5278d754544SDaniel Vetter 		mode->vrefresh = drm_mode_vrefresh(mode);
5288d754544SDaniel Vetter 
5298d754544SDaniel Vetter 	drm_mode_sort(&connector->modes);
5308d754544SDaniel Vetter 
5318d754544SDaniel Vetter 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s] probed modes :\n", connector->base.id,
53225933820SJani Nikula 			connector->name);
5338d754544SDaniel Vetter 	list_for_each_entry(mode, &connector->modes, head) {
5348d754544SDaniel Vetter 		drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
5358d754544SDaniel Vetter 		drm_mode_debug_printmodeline(mode);
5368d754544SDaniel Vetter 	}
5378d754544SDaniel Vetter 
5388d754544SDaniel Vetter 	return count;
5398d754544SDaniel Vetter }
5408d754544SDaniel Vetter EXPORT_SYMBOL(drm_helper_probe_single_connector_modes);
5418d754544SDaniel Vetter 
5428d754544SDaniel Vetter /**
5438d754544SDaniel Vetter  * drm_kms_helper_hotplug_event - fire off KMS hotplug events
5448d754544SDaniel Vetter  * @dev: drm_device whose connector state changed
5458d754544SDaniel Vetter  *
5468d754544SDaniel Vetter  * This function fires off the uevent for userspace and also calls the
5478d754544SDaniel Vetter  * output_poll_changed function, which is most commonly used to inform the fbdev
5488d754544SDaniel Vetter  * emulation code and allow it to update the fbcon output configuration.
5498d754544SDaniel Vetter  *
5508d754544SDaniel Vetter  * Drivers should call this from their hotplug handling code when a change is
5518d754544SDaniel Vetter  * detected. Note that this function does not do any output detection of its
5528d754544SDaniel Vetter  * own, like drm_helper_hpd_irq_event() does - this is assumed to be done by the
5538d754544SDaniel Vetter  * driver already.
5548d754544SDaniel Vetter  *
5558d754544SDaniel Vetter  * This function must be called from process context with no mode
5568d754544SDaniel Vetter  * setting locks held.
5578d754544SDaniel Vetter  */
5588d754544SDaniel Vetter void drm_kms_helper_hotplug_event(struct drm_device *dev)
5598d754544SDaniel Vetter {
5608d754544SDaniel Vetter 	/* send a uevent + call fbdev */
5618d754544SDaniel Vetter 	drm_sysfs_hotplug_event(dev);
5628d754544SDaniel Vetter 	if (dev->mode_config.funcs->output_poll_changed)
5638d754544SDaniel Vetter 		dev->mode_config.funcs->output_poll_changed(dev);
564c76f0f7cSNoralf Trønnes 
565c76f0f7cSNoralf Trønnes 	drm_client_dev_hotplug(dev);
5668d754544SDaniel Vetter }
5678d754544SDaniel Vetter EXPORT_SYMBOL(drm_kms_helper_hotplug_event);
5688d754544SDaniel Vetter 
5698d754544SDaniel Vetter static void output_poll_execute(struct work_struct *work)
5708d754544SDaniel Vetter {
5718d754544SDaniel Vetter 	struct delayed_work *delayed_work = to_delayed_work(work);
5728d754544SDaniel Vetter 	struct drm_device *dev = container_of(delayed_work, struct drm_device, mode_config.output_poll_work);
5738d754544SDaniel Vetter 	struct drm_connector *connector;
574c36a3254SDaniel Vetter 	struct drm_connector_list_iter conn_iter;
5758d754544SDaniel Vetter 	enum drm_connector_status old_status;
576162b6a57SDaniel Vetter 	bool repoll = false, changed;
577162b6a57SDaniel Vetter 
578*3b295cb1SChris Wilson 	if (!dev->mode_config.poll_enabled)
579*3b295cb1SChris Wilson 		return;
580*3b295cb1SChris Wilson 
581162b6a57SDaniel Vetter 	/* Pick up any changes detected by the probe functions. */
582162b6a57SDaniel Vetter 	changed = dev->mode_config.delayed_event;
583162b6a57SDaniel Vetter 	dev->mode_config.delayed_event = false;
5848d754544SDaniel Vetter 
5858d754544SDaniel Vetter 	if (!drm_kms_helper_poll)
586162b6a57SDaniel Vetter 		goto out;
5878d754544SDaniel Vetter 
5881fe7841dSChris Wilson 	if (!mutex_trylock(&dev->mode_config.mutex)) {
5891fe7841dSChris Wilson 		repoll = true;
5901fe7841dSChris Wilson 		goto out;
5911fe7841dSChris Wilson 	}
5921fe7841dSChris Wilson 
593b982dab1SThierry Reding 	drm_connector_list_iter_begin(dev, &conn_iter);
594c36a3254SDaniel Vetter 	drm_for_each_connector_iter(connector, &conn_iter) {
5958d754544SDaniel Vetter 		/* Ignore forced connectors. */
5968d754544SDaniel Vetter 		if (connector->force)
5978d754544SDaniel Vetter 			continue;
5988d754544SDaniel Vetter 
5998d754544SDaniel Vetter 		/* Ignore HPD capable connectors and connectors where we don't
6008d754544SDaniel Vetter 		 * want any hotplug detection at all for polling. */
6018d754544SDaniel Vetter 		if (!connector->polled || connector->polled == DRM_CONNECTOR_POLL_HPD)
6028d754544SDaniel Vetter 			continue;
6038d754544SDaniel Vetter 
6048d754544SDaniel Vetter 		old_status = connector->status;
6058d754544SDaniel Vetter 		/* if we are connected and don't want to poll for disconnect
6068d754544SDaniel Vetter 		   skip it */
6078d754544SDaniel Vetter 		if (old_status == connector_status_connected &&
6088d754544SDaniel Vetter 		    !(connector->polled & DRM_CONNECTOR_POLL_DISCONNECT))
6098d754544SDaniel Vetter 			continue;
6108d754544SDaniel Vetter 
611a3c6d686SJosef Holzmayr 		repoll = true;
612a3c6d686SJosef Holzmayr 
6136c5ed5aeSMaarten Lankhorst 		connector->status = drm_helper_probe_detect(connector, NULL, false);
6148d754544SDaniel Vetter 		if (old_status != connector->status) {
6158d754544SDaniel Vetter 			const char *old, *new;
6168d754544SDaniel Vetter 
617b7703726SDaniel Vetter 			/*
618b7703726SDaniel Vetter 			 * The poll work sets force=false when calling detect so
619b7703726SDaniel Vetter 			 * that drivers can avoid to do disruptive tests (e.g.
620b7703726SDaniel Vetter 			 * when load detect cycles could cause flickering on
621b7703726SDaniel Vetter 			 * other, running displays). This bears the risk that we
622b7703726SDaniel Vetter 			 * flip-flop between unknown here in the poll work and
623b7703726SDaniel Vetter 			 * the real state when userspace forces a full detect
624b7703726SDaniel Vetter 			 * call after receiving a hotplug event due to this
625b7703726SDaniel Vetter 			 * change.
626b7703726SDaniel Vetter 			 *
627b7703726SDaniel Vetter 			 * Hence clamp an unknown detect status to the old
628b7703726SDaniel Vetter 			 * value.
629b7703726SDaniel Vetter 			 */
630b7703726SDaniel Vetter 			if (connector->status == connector_status_unknown) {
631b7703726SDaniel Vetter 				connector->status = old_status;
632b7703726SDaniel Vetter 				continue;
633b7703726SDaniel Vetter 			}
634b7703726SDaniel Vetter 
6358d754544SDaniel Vetter 			old = drm_get_connector_status_name(old_status);
6368d754544SDaniel Vetter 			new = drm_get_connector_status_name(connector->status);
6378d754544SDaniel Vetter 
6388d754544SDaniel Vetter 			DRM_DEBUG_KMS("[CONNECTOR:%d:%s] "
6398d754544SDaniel Vetter 				      "status updated from %s to %s\n",
6408d754544SDaniel Vetter 				      connector->base.id,
64125933820SJani Nikula 				      connector->name,
6428d754544SDaniel Vetter 				      old, new);
6438d754544SDaniel Vetter 
6448d754544SDaniel Vetter 			changed = true;
6458d754544SDaniel Vetter 		}
6468d754544SDaniel Vetter 	}
647b982dab1SThierry Reding 	drm_connector_list_iter_end(&conn_iter);
6488d754544SDaniel Vetter 
6498d754544SDaniel Vetter 	mutex_unlock(&dev->mode_config.mutex);
6508d754544SDaniel Vetter 
651162b6a57SDaniel Vetter out:
6528d754544SDaniel Vetter 	if (changed)
6538d754544SDaniel Vetter 		drm_kms_helper_hotplug_event(dev);
6548d754544SDaniel Vetter 
6558d754544SDaniel Vetter 	if (repoll)
6568d754544SDaniel Vetter 		schedule_delayed_work(delayed_work, DRM_OUTPUT_POLL_PERIOD);
6578d754544SDaniel Vetter }
6588d754544SDaniel Vetter 
6598d754544SDaniel Vetter /**
66025c058ccSLukas Wunner  * drm_kms_helper_is_poll_worker - is %current task an output poll worker?
66125c058ccSLukas Wunner  *
66225c058ccSLukas Wunner  * Determine if %current task is an output poll worker.  This can be used
66325c058ccSLukas Wunner  * to select distinct code paths for output polling versus other contexts.
66425c058ccSLukas Wunner  *
66525c058ccSLukas Wunner  * One use case is to avoid a deadlock between the output poll worker and
66625c058ccSLukas Wunner  * the autosuspend worker wherein the latter waits for polling to finish
66725c058ccSLukas Wunner  * upon calling drm_kms_helper_poll_disable(), while the former waits for
66825c058ccSLukas Wunner  * runtime suspend to finish upon calling pm_runtime_get_sync() in a
66925c058ccSLukas Wunner  * connector ->detect hook.
67025c058ccSLukas Wunner  */
67125c058ccSLukas Wunner bool drm_kms_helper_is_poll_worker(void)
67225c058ccSLukas Wunner {
67325c058ccSLukas Wunner 	struct work_struct *work = current_work();
67425c058ccSLukas Wunner 
67525c058ccSLukas Wunner 	return work && work->func == output_poll_execute;
67625c058ccSLukas Wunner }
67725c058ccSLukas Wunner EXPORT_SYMBOL(drm_kms_helper_is_poll_worker);
67825c058ccSLukas Wunner 
67925c058ccSLukas Wunner /**
6808d754544SDaniel Vetter  * drm_kms_helper_poll_disable - disable output polling
6818d754544SDaniel Vetter  * @dev: drm_device
6828d754544SDaniel Vetter  *
6838d754544SDaniel Vetter  * This function disables the output polling work.
6848d754544SDaniel Vetter  *
6858d754544SDaniel Vetter  * Drivers can call this helper from their device suspend implementation. It is
686c4d79c22SDave Airlie  * not an error to call this even when output polling isn't enabled or already
687c4d79c22SDave Airlie  * disabled. Polling is re-enabled by calling drm_kms_helper_poll_enable().
688c4d79c22SDave Airlie  *
689c4d79c22SDave Airlie  * Note that calls to enable and disable polling must be strictly ordered, which
690c4d79c22SDave Airlie  * is automatically the case when they're only call from suspend/resume
691c4d79c22SDave Airlie  * callbacks.
6928d754544SDaniel Vetter  */
6938d754544SDaniel Vetter void drm_kms_helper_poll_disable(struct drm_device *dev)
6948d754544SDaniel Vetter {
6958d754544SDaniel Vetter 	if (!dev->mode_config.poll_enabled)
6968d754544SDaniel Vetter 		return;
6978d754544SDaniel Vetter 	cancel_delayed_work_sync(&dev->mode_config.output_poll_work);
6988d754544SDaniel Vetter }
6998d754544SDaniel Vetter EXPORT_SYMBOL(drm_kms_helper_poll_disable);
7008d754544SDaniel Vetter 
7018d754544SDaniel Vetter /**
7028d754544SDaniel Vetter  * drm_kms_helper_poll_init - initialize and enable output polling
7038d754544SDaniel Vetter  * @dev: drm_device
7048d754544SDaniel Vetter  *
7058d754544SDaniel Vetter  * This function intializes and then also enables output polling support for
7068d754544SDaniel Vetter  * @dev. Drivers which do not have reliable hotplug support in hardware can use
7078d754544SDaniel Vetter  * this helper infrastructure to regularly poll such connectors for changes in
7088d754544SDaniel Vetter  * their connection state.
7098d754544SDaniel Vetter  *
7108d754544SDaniel Vetter  * Drivers can control which connectors are polled by setting the
7118d754544SDaniel Vetter  * DRM_CONNECTOR_POLL_CONNECT and DRM_CONNECTOR_POLL_DISCONNECT flags. On
7128d754544SDaniel Vetter  * connectors where probing live outputs can result in visual distortion drivers
7138d754544SDaniel Vetter  * should not set the DRM_CONNECTOR_POLL_DISCONNECT flag to avoid this.
7148d754544SDaniel Vetter  * Connectors which have no flag or only DRM_CONNECTOR_POLL_HPD set are
7158d754544SDaniel Vetter  * completely ignored by the polling logic.
7168d754544SDaniel Vetter  *
7178d754544SDaniel Vetter  * Note that a connector can be both polled and probed from the hotplug handler,
7188d754544SDaniel Vetter  * in case the hotplug interrupt is known to be unreliable.
7198d754544SDaniel Vetter  */
7208d754544SDaniel Vetter void drm_kms_helper_poll_init(struct drm_device *dev)
7218d754544SDaniel Vetter {
7228d754544SDaniel Vetter 	INIT_DELAYED_WORK(&dev->mode_config.output_poll_work, output_poll_execute);
7238d754544SDaniel Vetter 	dev->mode_config.poll_enabled = true;
7248d754544SDaniel Vetter 
7258d754544SDaniel Vetter 	drm_kms_helper_poll_enable(dev);
7268d754544SDaniel Vetter }
7278d754544SDaniel Vetter EXPORT_SYMBOL(drm_kms_helper_poll_init);
7288d754544SDaniel Vetter 
7298d754544SDaniel Vetter /**
7308d754544SDaniel Vetter  * drm_kms_helper_poll_fini - disable output polling and clean it up
7318d754544SDaniel Vetter  * @dev: drm_device
7328d754544SDaniel Vetter  */
7338d754544SDaniel Vetter void drm_kms_helper_poll_fini(struct drm_device *dev)
7348d754544SDaniel Vetter {
735*3b295cb1SChris Wilson 	if (!dev->mode_config.poll_enabled)
736*3b295cb1SChris Wilson 		return;
737*3b295cb1SChris Wilson 
738*3b295cb1SChris Wilson 	dev->mode_config.poll_enabled = false;
739*3b295cb1SChris Wilson 	cancel_delayed_work_sync(&dev->mode_config.output_poll_work);
7408d754544SDaniel Vetter }
7418d754544SDaniel Vetter EXPORT_SYMBOL(drm_kms_helper_poll_fini);
7428d754544SDaniel Vetter 
7438d754544SDaniel Vetter /**
7448d754544SDaniel Vetter  * drm_helper_hpd_irq_event - hotplug processing
7458d754544SDaniel Vetter  * @dev: drm_device
7468d754544SDaniel Vetter  *
7478d754544SDaniel Vetter  * Drivers can use this helper function to run a detect cycle on all connectors
7488d754544SDaniel Vetter  * which have the DRM_CONNECTOR_POLL_HPD flag set in their &polled member. All
7498d754544SDaniel Vetter  * other connectors are ignored, which is useful to avoid reprobing fixed
7508d754544SDaniel Vetter  * panels.
7518d754544SDaniel Vetter  *
7528d754544SDaniel Vetter  * This helper function is useful for drivers which can't or don't track hotplug
7538d754544SDaniel Vetter  * interrupts for each connector.
7548d754544SDaniel Vetter  *
7558d754544SDaniel Vetter  * Drivers which support hotplug interrupts for each connector individually and
7568d754544SDaniel Vetter  * which have a more fine-grained detect logic should bypass this code and
7578d754544SDaniel Vetter  * directly call drm_kms_helper_hotplug_event() in case the connector state
7588d754544SDaniel Vetter  * changed.
7598d754544SDaniel Vetter  *
7608d754544SDaniel Vetter  * This function must be called from process context with no mode
7618d754544SDaniel Vetter  * setting locks held.
7628d754544SDaniel Vetter  *
7638d754544SDaniel Vetter  * Note that a connector can be both polled and probed from the hotplug handler,
7648d754544SDaniel Vetter  * in case the hotplug interrupt is known to be unreliable.
7658d754544SDaniel Vetter  */
7668d754544SDaniel Vetter bool drm_helper_hpd_irq_event(struct drm_device *dev)
7678d754544SDaniel Vetter {
7688d754544SDaniel Vetter 	struct drm_connector *connector;
769c36a3254SDaniel Vetter 	struct drm_connector_list_iter conn_iter;
7708d754544SDaniel Vetter 	enum drm_connector_status old_status;
7718d754544SDaniel Vetter 	bool changed = false;
7728d754544SDaniel Vetter 
7738d754544SDaniel Vetter 	if (!dev->mode_config.poll_enabled)
7748d754544SDaniel Vetter 		return false;
7758d754544SDaniel Vetter 
7768d754544SDaniel Vetter 	mutex_lock(&dev->mode_config.mutex);
777b982dab1SThierry Reding 	drm_connector_list_iter_begin(dev, &conn_iter);
778c36a3254SDaniel Vetter 	drm_for_each_connector_iter(connector, &conn_iter) {
7798d754544SDaniel Vetter 		/* Only handle HPD capable connectors. */
7808d754544SDaniel Vetter 		if (!(connector->polled & DRM_CONNECTOR_POLL_HPD))
7818d754544SDaniel Vetter 			continue;
7828d754544SDaniel Vetter 
7838d754544SDaniel Vetter 		old_status = connector->status;
7848d754544SDaniel Vetter 
7856c5ed5aeSMaarten Lankhorst 		connector->status = drm_helper_probe_detect(connector, NULL, false);
7868d754544SDaniel Vetter 		DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
7878d754544SDaniel Vetter 			      connector->base.id,
78825933820SJani Nikula 			      connector->name,
7898d754544SDaniel Vetter 			      drm_get_connector_status_name(old_status),
7908d754544SDaniel Vetter 			      drm_get_connector_status_name(connector->status));
7918d754544SDaniel Vetter 		if (old_status != connector->status)
7928d754544SDaniel Vetter 			changed = true;
7938d754544SDaniel Vetter 	}
794b982dab1SThierry Reding 	drm_connector_list_iter_end(&conn_iter);
7958d754544SDaniel Vetter 	mutex_unlock(&dev->mode_config.mutex);
7968d754544SDaniel Vetter 
7978d754544SDaniel Vetter 	if (changed)
7988d754544SDaniel Vetter 		drm_kms_helper_hotplug_event(dev);
7998d754544SDaniel Vetter 
8008d754544SDaniel Vetter 	return changed;
8018d754544SDaniel Vetter }
8028d754544SDaniel Vetter EXPORT_SYMBOL(drm_helper_hpd_irq_event);
803