xref: /openbmc/linux/drivers/gpu/drm/drm_probe_helper.c (revision 48eaeb7664c76139438724d520a1ea4a84a3ed92)
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 
358d754544SDaniel Vetter #include <drm/drmP.h>
36c76f0f7cSNoralf Trønnes #include <drm/drm_client.h>
378d754544SDaniel Vetter #include <drm/drm_crtc.h>
388d754544SDaniel Vetter #include <drm/drm_fourcc.h>
398d754544SDaniel Vetter #include <drm/drm_fb_helper.h>
408d754544SDaniel Vetter #include <drm/drm_edid.h>
41da251bf8SJose Abreu #include <drm/drm_modeset_helper_vtables.h>
42fcd70cd3SDaniel Vetter #include <drm/drm_probe_helper.h>
43da251bf8SJose Abreu 
44da251bf8SJose Abreu #include "drm_crtc_helper_internal.h"
458d754544SDaniel Vetter 
468d754544SDaniel Vetter /**
478d754544SDaniel Vetter  * DOC: output probing helper overview
488d754544SDaniel Vetter  *
498d754544SDaniel Vetter  * This library provides some helper code for output probing. It provides an
506806cdf9SDaniel Vetter  * implementation of the core &drm_connector_funcs.fill_modes interface with
511bbfe9d1SDaniel Vetter  * drm_helper_probe_single_connector_modes().
528d754544SDaniel Vetter  *
538d754544SDaniel Vetter  * It also provides support for polling connectors with a work item and for
548d754544SDaniel Vetter  * generic hotplug interrupt handling where the driver doesn't or cannot keep
558d754544SDaniel Vetter  * track of a per-connector hpd interrupt.
568d754544SDaniel Vetter  *
578d754544SDaniel Vetter  * This helper library can be used independently of the modeset helper library.
588d754544SDaniel Vetter  * Drivers can also overwrite different parts e.g. use their own hotplug
598d754544SDaniel Vetter  * handling code to avoid probing unrelated outputs.
60092d01daSDaniel Vetter  *
61092d01daSDaniel Vetter  * The probe helpers share the function table structures with other display
62ea0dd85aSDaniel Vetter  * helper libraries. See &struct drm_connector_helper_funcs for the details.
638d754544SDaniel Vetter  */
648d754544SDaniel Vetter 
658d754544SDaniel Vetter static bool drm_kms_helper_poll = true;
668d754544SDaniel Vetter module_param_named(poll, drm_kms_helper_poll, bool, 0600);
678d754544SDaniel Vetter 
6805acaec3SVille Syrjälä static enum drm_mode_status
6905acaec3SVille Syrjälä drm_mode_validate_flag(const struct drm_display_mode *mode,
708d754544SDaniel Vetter 		       int flags)
718d754544SDaniel Vetter {
728d754544SDaniel Vetter 	if ((mode->flags & DRM_MODE_FLAG_INTERLACE) &&
738d754544SDaniel Vetter 	    !(flags & DRM_MODE_FLAG_INTERLACE))
7405acaec3SVille Syrjälä 		return MODE_NO_INTERLACE;
7505acaec3SVille Syrjälä 
768d754544SDaniel Vetter 	if ((mode->flags & DRM_MODE_FLAG_DBLSCAN) &&
778d754544SDaniel Vetter 	    !(flags & DRM_MODE_FLAG_DBLSCAN))
7805acaec3SVille Syrjälä 		return MODE_NO_DBLESCAN;
7905acaec3SVille Syrjälä 
808d754544SDaniel Vetter 	if ((mode->flags & DRM_MODE_FLAG_3D_MASK) &&
818d754544SDaniel Vetter 	    !(flags & DRM_MODE_FLAG_3D_MASK))
8205acaec3SVille Syrjälä 		return MODE_NO_STEREO;
838d754544SDaniel Vetter 
8405acaec3SVille Syrjälä 	return MODE_OK;
858d754544SDaniel Vetter }
868d754544SDaniel Vetter 
8797f973d6SJose Abreu static enum drm_mode_status
8897f973d6SJose Abreu drm_mode_validate_pipeline(struct drm_display_mode *mode,
8997f973d6SJose Abreu 			    struct drm_connector *connector)
9097f973d6SJose Abreu {
9197f973d6SJose Abreu 	struct drm_device *dev = connector->dev;
9297f973d6SJose Abreu 	enum drm_mode_status ret = MODE_OK;
9383aefbb8SVille Syrjälä 	struct drm_encoder *encoder;
9483aefbb8SVille Syrjälä 	int i;
9597f973d6SJose Abreu 
9697f973d6SJose Abreu 	/* Step 1: Validate against connector */
9797f973d6SJose Abreu 	ret = drm_connector_mode_valid(connector, mode);
9897f973d6SJose Abreu 	if (ret != MODE_OK)
9997f973d6SJose Abreu 		return ret;
10097f973d6SJose Abreu 
10197f973d6SJose Abreu 	/* Step 2: Validate against encoders and crtcs */
10283aefbb8SVille Syrjälä 	drm_connector_for_each_possible_encoder(connector, encoder, i) {
10397f973d6SJose Abreu 		struct drm_crtc *crtc;
10497f973d6SJose Abreu 
10597f973d6SJose Abreu 		ret = drm_encoder_mode_valid(encoder, mode);
10697f973d6SJose Abreu 		if (ret != MODE_OK) {
10797f973d6SJose Abreu 			/* No point in continuing for crtc check as this encoder
10897f973d6SJose Abreu 			 * will not accept the mode anyway. If all encoders
10997f973d6SJose Abreu 			 * reject the mode then, at exit, ret will not be
11097f973d6SJose Abreu 			 * MODE_OK. */
11197f973d6SJose Abreu 			continue;
11297f973d6SJose Abreu 		}
11397f973d6SJose Abreu 
11497f973d6SJose Abreu 		ret = drm_bridge_mode_valid(encoder->bridge, mode);
11597f973d6SJose Abreu 		if (ret != MODE_OK) {
11697f973d6SJose Abreu 			/* There is also no point in continuing for crtc check
11797f973d6SJose Abreu 			 * here. */
11897f973d6SJose Abreu 			continue;
11997f973d6SJose Abreu 		}
12097f973d6SJose Abreu 
12197f973d6SJose Abreu 		drm_for_each_crtc(crtc, dev) {
12297f973d6SJose Abreu 			if (!drm_encoder_crtc_ok(encoder, crtc))
12397f973d6SJose Abreu 				continue;
12497f973d6SJose Abreu 
12597f973d6SJose Abreu 			ret = drm_crtc_mode_valid(crtc, mode);
12697f973d6SJose Abreu 			if (ret == MODE_OK) {
12797f973d6SJose Abreu 				/* If we get to this point there is at least
12897f973d6SJose Abreu 				 * one combination of encoder+crtc that works
12997f973d6SJose Abreu 				 * for this mode. Lets return now. */
13097f973d6SJose Abreu 				return ret;
13197f973d6SJose Abreu 			}
13297f973d6SJose Abreu 		}
13397f973d6SJose Abreu 	}
13497f973d6SJose Abreu 
13597f973d6SJose Abreu 	return ret;
13697f973d6SJose Abreu }
13797f973d6SJose Abreu 
138eaf99c74SChris Wilson static int drm_helper_probe_add_cmdline_mode(struct drm_connector *connector)
139eaf99c74SChris Wilson {
1405f0c3f99SChris Wilson 	struct drm_cmdline_mode *cmdline_mode;
141eaf99c74SChris Wilson 	struct drm_display_mode *mode;
142eaf99c74SChris Wilson 
1435f0c3f99SChris Wilson 	cmdline_mode = &connector->cmdline_mode;
1445f0c3f99SChris Wilson 	if (!cmdline_mode->specified)
145eaf99c74SChris Wilson 		return 0;
146eaf99c74SChris Wilson 
1475f0c3f99SChris Wilson 	/* Only add a GTF mode if we find no matching probed modes */
1485f0c3f99SChris Wilson 	list_for_each_entry(mode, &connector->probed_modes, head) {
1495f0c3f99SChris Wilson 		if (mode->hdisplay != cmdline_mode->xres ||
1505f0c3f99SChris Wilson 		    mode->vdisplay != cmdline_mode->yres)
1515f0c3f99SChris Wilson 			continue;
1525f0c3f99SChris Wilson 
1535f0c3f99SChris Wilson 		if (cmdline_mode->refresh_specified) {
1545f0c3f99SChris Wilson 			/* The probed mode's vrefresh is set until later */
1555f0c3f99SChris Wilson 			if (drm_mode_vrefresh(mode) != cmdline_mode->refresh)
1565f0c3f99SChris Wilson 				continue;
1575f0c3f99SChris Wilson 		}
1585f0c3f99SChris Wilson 
1595f0c3f99SChris Wilson 		return 0;
1605f0c3f99SChris Wilson 	}
1615f0c3f99SChris Wilson 
162eaf99c74SChris Wilson 	mode = drm_mode_create_from_cmdline_mode(connector->dev,
1635f0c3f99SChris Wilson 						 cmdline_mode);
164eaf99c74SChris Wilson 	if (mode == NULL)
165eaf99c74SChris Wilson 		return 0;
166eaf99c74SChris Wilson 
167eaf99c74SChris Wilson 	drm_mode_probed_add(connector, mode);
168eaf99c74SChris Wilson 	return 1;
169eaf99c74SChris Wilson }
170eaf99c74SChris Wilson 
171da251bf8SJose Abreu enum drm_mode_status drm_crtc_mode_valid(struct drm_crtc *crtc,
172da251bf8SJose Abreu 					 const struct drm_display_mode *mode)
173da251bf8SJose Abreu {
174da251bf8SJose Abreu 	const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
175da251bf8SJose Abreu 
176da251bf8SJose Abreu 	if (!crtc_funcs || !crtc_funcs->mode_valid)
177da251bf8SJose Abreu 		return MODE_OK;
178da251bf8SJose Abreu 
179da251bf8SJose Abreu 	return crtc_funcs->mode_valid(crtc, mode);
180da251bf8SJose Abreu }
181da251bf8SJose Abreu 
182da251bf8SJose Abreu enum drm_mode_status drm_encoder_mode_valid(struct drm_encoder *encoder,
183da251bf8SJose Abreu 					    const struct drm_display_mode *mode)
184da251bf8SJose Abreu {
185da251bf8SJose Abreu 	const struct drm_encoder_helper_funcs *encoder_funcs =
186da251bf8SJose Abreu 		encoder->helper_private;
187da251bf8SJose Abreu 
188da251bf8SJose Abreu 	if (!encoder_funcs || !encoder_funcs->mode_valid)
189da251bf8SJose Abreu 		return MODE_OK;
190da251bf8SJose Abreu 
191da251bf8SJose Abreu 	return encoder_funcs->mode_valid(encoder, mode);
192da251bf8SJose Abreu }
193da251bf8SJose Abreu 
194da251bf8SJose Abreu enum drm_mode_status drm_connector_mode_valid(struct drm_connector *connector,
195da251bf8SJose Abreu 					      struct drm_display_mode *mode)
196da251bf8SJose Abreu {
197da251bf8SJose Abreu 	const struct drm_connector_helper_funcs *connector_funcs =
198da251bf8SJose Abreu 		connector->helper_private;
199da251bf8SJose Abreu 
200da251bf8SJose Abreu 	if (!connector_funcs || !connector_funcs->mode_valid)
201da251bf8SJose Abreu 		return MODE_OK;
202da251bf8SJose Abreu 
203da251bf8SJose Abreu 	return connector_funcs->mode_valid(connector, mode);
204da251bf8SJose Abreu }
205da251bf8SJose Abreu 
2068c4ccc4aSDaniel Vetter #define DRM_OUTPUT_POLL_PERIOD (10*HZ)
2074ad640e9SEgbert Eich /**
208c4d79c22SDave Airlie  * drm_kms_helper_poll_enable - re-enable output polling.
2094ad640e9SEgbert Eich  * @dev: drm_device
2104ad640e9SEgbert Eich  *
211c4d79c22SDave Airlie  * This function re-enables the output polling work, after it has been
212c4d79c22SDave Airlie  * temporarily disabled using drm_kms_helper_poll_disable(), for example over
213c4d79c22SDave Airlie  * suspend/resume.
2144ad640e9SEgbert Eich  *
215c4d79c22SDave Airlie  * Drivers can call this helper from their device resume implementation. It is
216b66d0f34SNoralf Trønnes  * not an error to call this even when output polling isn't enabled.
217c4d79c22SDave Airlie  *
218c4d79c22SDave Airlie  * Note that calls to enable and disable polling must be strictly ordered, which
219c4d79c22SDave Airlie  * is automatically the case when they're only call from suspend/resume
220c4d79c22SDave Airlie  * callbacks.
2214ad640e9SEgbert Eich  */
222c4d79c22SDave Airlie void drm_kms_helper_poll_enable(struct drm_device *dev)
2238c4ccc4aSDaniel Vetter {
2248c4ccc4aSDaniel Vetter 	bool poll = false;
2258c4ccc4aSDaniel Vetter 	struct drm_connector *connector;
226c36a3254SDaniel Vetter 	struct drm_connector_list_iter conn_iter;
227339fd362SPeter Ujfalusi 	unsigned long delay = DRM_OUTPUT_POLL_PERIOD;
2288c4ccc4aSDaniel Vetter 
2298c4ccc4aSDaniel Vetter 	if (!dev->mode_config.poll_enabled || !drm_kms_helper_poll)
2308c4ccc4aSDaniel Vetter 		return;
2318c4ccc4aSDaniel Vetter 
232b982dab1SThierry Reding 	drm_connector_list_iter_begin(dev, &conn_iter);
233c36a3254SDaniel Vetter 	drm_for_each_connector_iter(connector, &conn_iter) {
2348c4ccc4aSDaniel Vetter 		if (connector->polled & (DRM_CONNECTOR_POLL_CONNECT |
2358c4ccc4aSDaniel Vetter 					 DRM_CONNECTOR_POLL_DISCONNECT))
2368c4ccc4aSDaniel Vetter 			poll = true;
2378c4ccc4aSDaniel Vetter 	}
238b982dab1SThierry Reding 	drm_connector_list_iter_end(&conn_iter);
2398c4ccc4aSDaniel Vetter 
240339fd362SPeter Ujfalusi 	if (dev->mode_config.delayed_event) {
24168f458eeSPeter Ujfalusi 		/*
24268f458eeSPeter Ujfalusi 		 * FIXME:
24368f458eeSPeter Ujfalusi 		 *
24468f458eeSPeter Ujfalusi 		 * Use short (1s) delay to handle the initial delayed event.
24568f458eeSPeter Ujfalusi 		 * This delay should not be needed, but Optimus/nouveau will
24668f458eeSPeter Ujfalusi 		 * fail in a mysterious way if the delayed event is handled as
24768f458eeSPeter Ujfalusi 		 * soon as possible like it is done in
24868f458eeSPeter Ujfalusi 		 * drm_helper_probe_single_connector_modes() in case the poll
24968f458eeSPeter Ujfalusi 		 * was enabled before.
25068f458eeSPeter Ujfalusi 		 */
251339fd362SPeter Ujfalusi 		poll = true;
25268f458eeSPeter Ujfalusi 		delay = HZ;
253339fd362SPeter Ujfalusi 	}
254339fd362SPeter Ujfalusi 
2558c4ccc4aSDaniel Vetter 	if (poll)
256339fd362SPeter Ujfalusi 		schedule_delayed_work(&dev->mode_config.output_poll_work, delay);
2578c4ccc4aSDaniel Vetter }
258c4d79c22SDave Airlie EXPORT_SYMBOL(drm_kms_helper_poll_enable);
2594ad640e9SEgbert Eich 
260949f0886SLaurent Pinchart static enum drm_connector_status
2616c5ed5aeSMaarten Lankhorst drm_helper_probe_detect_ctx(struct drm_connector *connector, bool force)
262949f0886SLaurent Pinchart {
2636c5ed5aeSMaarten Lankhorst 	const struct drm_connector_helper_funcs *funcs = connector->helper_private;
2646c5ed5aeSMaarten Lankhorst 	struct drm_modeset_acquire_ctx ctx;
2656c5ed5aeSMaarten Lankhorst 	int ret;
2666c5ed5aeSMaarten Lankhorst 
2676c5ed5aeSMaarten Lankhorst 	drm_modeset_acquire_init(&ctx, 0);
2686c5ed5aeSMaarten Lankhorst 
2696c5ed5aeSMaarten Lankhorst retry:
2706c5ed5aeSMaarten Lankhorst 	ret = drm_modeset_lock(&connector->dev->mode_config.connection_mutex, &ctx);
2716c5ed5aeSMaarten Lankhorst 	if (!ret) {
2726c5ed5aeSMaarten Lankhorst 		if (funcs->detect_ctx)
2736c5ed5aeSMaarten Lankhorst 			ret = funcs->detect_ctx(connector, &ctx, force);
2746c5ed5aeSMaarten Lankhorst 		else if (connector->funcs->detect)
2756c5ed5aeSMaarten Lankhorst 			ret = connector->funcs->detect(connector, force);
2766c5ed5aeSMaarten Lankhorst 		else
2776c5ed5aeSMaarten Lankhorst 			ret = connector_status_connected;
278949f0886SLaurent Pinchart 	}
279949f0886SLaurent Pinchart 
2806c5ed5aeSMaarten Lankhorst 	if (ret == -EDEADLK) {
2816c5ed5aeSMaarten Lankhorst 		drm_modeset_backoff(&ctx);
2826c5ed5aeSMaarten Lankhorst 		goto retry;
2836c5ed5aeSMaarten Lankhorst 	}
2846c5ed5aeSMaarten Lankhorst 
2856c5ed5aeSMaarten Lankhorst 	if (WARN_ON(ret < 0))
2866c5ed5aeSMaarten Lankhorst 		ret = connector_status_unknown;
2876c5ed5aeSMaarten Lankhorst 
2886c5ed5aeSMaarten Lankhorst 	drm_modeset_drop_locks(&ctx);
2896c5ed5aeSMaarten Lankhorst 	drm_modeset_acquire_fini(&ctx);
2906c5ed5aeSMaarten Lankhorst 
2916c5ed5aeSMaarten Lankhorst 	return ret;
2926c5ed5aeSMaarten Lankhorst }
2936c5ed5aeSMaarten Lankhorst 
2946c5ed5aeSMaarten Lankhorst /**
2956c5ed5aeSMaarten Lankhorst  * drm_helper_probe_detect - probe connector status
2966c5ed5aeSMaarten Lankhorst  * @connector: connector to probe
2976c5ed5aeSMaarten Lankhorst  * @ctx: acquire_ctx, or NULL to let this function handle locking.
2986c5ed5aeSMaarten Lankhorst  * @force: Whether destructive probe operations should be performed.
2996c5ed5aeSMaarten Lankhorst  *
3006c5ed5aeSMaarten Lankhorst  * This function calls the detect callbacks of the connector.
3016c5ed5aeSMaarten Lankhorst  * This function returns &drm_connector_status, or
3026c5ed5aeSMaarten Lankhorst  * if @ctx is set, it might also return -EDEADLK.
3036c5ed5aeSMaarten Lankhorst  */
3046c5ed5aeSMaarten Lankhorst int
3056c5ed5aeSMaarten Lankhorst drm_helper_probe_detect(struct drm_connector *connector,
3066c5ed5aeSMaarten Lankhorst 			struct drm_modeset_acquire_ctx *ctx,
3076c5ed5aeSMaarten Lankhorst 			bool force)
3086c5ed5aeSMaarten Lankhorst {
3096c5ed5aeSMaarten Lankhorst 	const struct drm_connector_helper_funcs *funcs = connector->helper_private;
3106c5ed5aeSMaarten Lankhorst 	struct drm_device *dev = connector->dev;
3116c5ed5aeSMaarten Lankhorst 	int ret;
3126c5ed5aeSMaarten Lankhorst 
3136c5ed5aeSMaarten Lankhorst 	if (!ctx)
3146c5ed5aeSMaarten Lankhorst 		return drm_helper_probe_detect_ctx(connector, force);
3156c5ed5aeSMaarten Lankhorst 
3166c5ed5aeSMaarten Lankhorst 	ret = drm_modeset_lock(&dev->mode_config.connection_mutex, ctx);
3176c5ed5aeSMaarten Lankhorst 	if (ret)
3186c5ed5aeSMaarten Lankhorst 		return ret;
3196c5ed5aeSMaarten Lankhorst 
3206c5ed5aeSMaarten Lankhorst 	if (funcs->detect_ctx)
3216c5ed5aeSMaarten Lankhorst 		return funcs->detect_ctx(connector, ctx, force);
3226c5ed5aeSMaarten Lankhorst 	else if (connector->funcs->detect)
3236c5ed5aeSMaarten Lankhorst 		return connector->funcs->detect(connector, force);
3246c5ed5aeSMaarten Lankhorst 	else
3256c5ed5aeSMaarten Lankhorst 		return connector_status_connected;
3266c5ed5aeSMaarten Lankhorst }
3276c5ed5aeSMaarten Lankhorst EXPORT_SYMBOL(drm_helper_probe_detect);
3286c5ed5aeSMaarten Lankhorst 
3296af3e656SVille Syrjälä /**
3306af3e656SVille Syrjälä  * drm_helper_probe_single_connector_modes - get complete set of display modes
3316af3e656SVille Syrjälä  * @connector: connector to probe
3326af3e656SVille Syrjälä  * @maxX: max width for modes
3336af3e656SVille Syrjälä  * @maxY: max height for modes
3346af3e656SVille Syrjälä  *
3356af3e656SVille Syrjälä  * Based on the helper callbacks implemented by @connector in struct
3366af3e656SVille Syrjälä  * &drm_connector_helper_funcs try to detect all valid modes.  Modes will first
3376af3e656SVille Syrjälä  * be added to the connector's probed_modes list, then culled (based on validity
3386af3e656SVille Syrjälä  * and the @maxX, @maxY parameters) and put into the normal modes list.
3396af3e656SVille Syrjälä  *
3406806cdf9SDaniel Vetter  * Intended to be used as a generic implementation of the
3416806cdf9SDaniel Vetter  * &drm_connector_funcs.fill_modes() vfunc for drivers that use the CRTC helpers
3426806cdf9SDaniel Vetter  * for output mode filtering and detection.
3436af3e656SVille Syrjälä  *
3445dec293bSVille Syrjälä  * The basic procedure is as follows
3456af3e656SVille Syrjälä  *
3465dec293bSVille Syrjälä  * 1. All modes currently on the connector's modes list are marked as stale
3476af3e656SVille Syrjälä  *
3485dec293bSVille Syrjälä  * 2. New modes are added to the connector's probed_modes list with
3495dec293bSVille Syrjälä  *    drm_mode_probed_add(). New modes start their life with status as OK.
3505dec293bSVille Syrjälä  *    Modes are added from a single source using the following priority order.
3515dec293bSVille Syrjälä  *
3526806cdf9SDaniel Vetter  *    - &drm_connector_helper_funcs.get_modes vfunc
3535dec293bSVille Syrjälä  *    - if the connector status is connector_status_connected, standard
3545dec293bSVille Syrjälä  *      VESA DMT modes up to 1024x768 are automatically added
3555dec293bSVille Syrjälä  *      (drm_add_modes_noedid())
3565dec293bSVille Syrjälä  *
3575dec293bSVille Syrjälä  *    Finally modes specified via the kernel command line (video=...) are
3585dec293bSVille Syrjälä  *    added in addition to what the earlier probes produced
3595dec293bSVille Syrjälä  *    (drm_helper_probe_add_cmdline_mode()). These modes are generated
3605dec293bSVille Syrjälä  *    using the VESA GTF/CVT formulas.
3615dec293bSVille Syrjälä  *
3625dec293bSVille Syrjälä  * 3. Modes are moved from the probed_modes list to the modes list. Potential
36397e14fbeSDaniel Vetter  *    duplicates are merged together (see drm_connector_list_update()).
3645dec293bSVille Syrjälä  *    After this step the probed_modes list will be empty again.
3655dec293bSVille Syrjälä  *
3665dec293bSVille Syrjälä  * 4. Any non-stale mode on the modes list then undergoes validation
3675dec293bSVille Syrjälä  *
3685dec293bSVille Syrjälä  *    - drm_mode_validate_basic() performs basic sanity checks
3695dec293bSVille Syrjälä  *    - drm_mode_validate_size() filters out modes larger than @maxX and @maxY
3705dec293bSVille Syrjälä  *      (if specified)
37157366a8dSMasahiro Yamada  *    - drm_mode_validate_flag() checks the modes against basic connector
37257366a8dSMasahiro Yamada  *      capabilities (interlace_allowed,doublescan_allowed,stereo_allowed)
3736806cdf9SDaniel Vetter  *    - the optional &drm_connector_helper_funcs.mode_valid helper can perform
37497f973d6SJose Abreu  *      driver and/or sink specific checks
37597f973d6SJose Abreu  *    - the optional &drm_crtc_helper_funcs.mode_valid,
37697f973d6SJose Abreu  *      &drm_bridge_funcs.mode_valid and &drm_encoder_helper_funcs.mode_valid
37797f973d6SJose Abreu  *      helpers can perform driver and/or source specific checks which are also
37897f973d6SJose Abreu  *      enforced by the modeset/atomic helpers
3795dec293bSVille Syrjälä  *
3805dec293bSVille Syrjälä  * 5. Any mode whose status is not OK is pruned from the connector's modes list,
3815dec293bSVille Syrjälä  *    accompanied by a debug message indicating the reason for the mode's
3825dec293bSVille Syrjälä  *    rejection (see drm_mode_prune_invalid()).
3836af3e656SVille Syrjälä  *
3846af3e656SVille Syrjälä  * Returns:
3856af3e656SVille Syrjälä  * The number of modes found on @connector.
3866af3e656SVille Syrjälä  */
3876af3e656SVille Syrjälä int drm_helper_probe_single_connector_modes(struct drm_connector *connector,
3886af3e656SVille Syrjälä 					    uint32_t maxX, uint32_t maxY)
3898d754544SDaniel Vetter {
3908d754544SDaniel Vetter 	struct drm_device *dev = connector->dev;
3918d754544SDaniel Vetter 	struct drm_display_mode *mode;
392be26a66dSJani Nikula 	const struct drm_connector_helper_funcs *connector_funcs =
3938d754544SDaniel Vetter 		connector->helper_private;
3946c5ed5aeSMaarten Lankhorst 	int count = 0, ret;
3958d754544SDaniel Vetter 	int mode_flags = 0;
3968d754544SDaniel Vetter 	bool verbose_prune = true;
397162b6a57SDaniel Vetter 	enum drm_connector_status old_status;
3986c5ed5aeSMaarten Lankhorst 	struct drm_modeset_acquire_ctx ctx;
3998d754544SDaniel Vetter 
4008d754544SDaniel Vetter 	WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
4018d754544SDaniel Vetter 
4026c5ed5aeSMaarten Lankhorst 	drm_modeset_acquire_init(&ctx, 0);
4036c5ed5aeSMaarten Lankhorst 
4048d754544SDaniel Vetter 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", connector->base.id,
40525933820SJani Nikula 			connector->name);
4066c5ed5aeSMaarten Lankhorst 
4076c5ed5aeSMaarten Lankhorst retry:
4086c5ed5aeSMaarten Lankhorst 	ret = drm_modeset_lock(&dev->mode_config.connection_mutex, &ctx);
4096c5ed5aeSMaarten Lankhorst 	if (ret == -EDEADLK) {
4106c5ed5aeSMaarten Lankhorst 		drm_modeset_backoff(&ctx);
4116c5ed5aeSMaarten Lankhorst 		goto retry;
4126c5ed5aeSMaarten Lankhorst 	} else
4136c5ed5aeSMaarten Lankhorst 		WARN_ON(ret < 0);
4146c5ed5aeSMaarten Lankhorst 
4155ba89406SVille Syrjälä 	/* set all old modes to the stale state */
4168d754544SDaniel Vetter 	list_for_each_entry(mode, &connector->modes, head)
4175ba89406SVille Syrjälä 		mode->status = MODE_STALE;
4188d754544SDaniel Vetter 
419ed293f77SDaniel Vetter 	old_status = connector->status;
420ed293f77SDaniel Vetter 
4218d754544SDaniel Vetter 	if (connector->force) {
4222c4cc91bSPeter Hurley 		if (connector->force == DRM_FORCE_ON ||
4232c4cc91bSPeter Hurley 		    connector->force == DRM_FORCE_ON_DIGITAL)
4248d754544SDaniel Vetter 			connector->status = connector_status_connected;
4258d754544SDaniel Vetter 		else
4268d754544SDaniel Vetter 			connector->status = connector_status_disconnected;
4278d754544SDaniel Vetter 		if (connector->funcs->force)
4288d754544SDaniel Vetter 			connector->funcs->force(connector);
4298d754544SDaniel Vetter 	} else {
4306c5ed5aeSMaarten Lankhorst 		ret = drm_helper_probe_detect(connector, &ctx, true);
4316c5ed5aeSMaarten Lankhorst 
4326c5ed5aeSMaarten Lankhorst 		if (ret == -EDEADLK) {
4336c5ed5aeSMaarten Lankhorst 			drm_modeset_backoff(&ctx);
4346c5ed5aeSMaarten Lankhorst 			goto retry;
4356c5ed5aeSMaarten Lankhorst 		} else if (WARN(ret < 0, "Invalid return value %i for connector detection\n", ret))
4366c5ed5aeSMaarten Lankhorst 			ret = connector_status_unknown;
4376c5ed5aeSMaarten Lankhorst 
4386c5ed5aeSMaarten Lankhorst 		connector->status = ret;
439ed293f77SDaniel Vetter 	}
440162b6a57SDaniel Vetter 
441162b6a57SDaniel Vetter 	/*
442162b6a57SDaniel Vetter 	 * Normally either the driver's hpd code or the poll loop should
443162b6a57SDaniel Vetter 	 * pick up any changes and fire the hotplug event. But if
444162b6a57SDaniel Vetter 	 * userspace sneaks in a probe, we might miss a change. Hence
445162b6a57SDaniel Vetter 	 * check here, and if anything changed start the hotplug code.
446162b6a57SDaniel Vetter 	 */
447162b6a57SDaniel Vetter 	if (old_status != connector->status) {
4484e15f2a1SJani Nikula 		DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
449162b6a57SDaniel Vetter 			      connector->base.id,
450162b6a57SDaniel Vetter 			      connector->name,
4514e15f2a1SJani Nikula 			      drm_get_connector_status_name(old_status),
4524e15f2a1SJani Nikula 			      drm_get_connector_status_name(connector->status));
453162b6a57SDaniel Vetter 
454162b6a57SDaniel Vetter 		/*
455162b6a57SDaniel Vetter 		 * The hotplug event code might call into the fb
456162b6a57SDaniel Vetter 		 * helpers, and so expects that we do not hold any
457162b6a57SDaniel Vetter 		 * locks. Fire up the poll struct instead, it will
458162b6a57SDaniel Vetter 		 * disable itself again.
459162b6a57SDaniel Vetter 		 */
460162b6a57SDaniel Vetter 		dev->mode_config.delayed_event = true;
461162b6a57SDaniel Vetter 		if (dev->mode_config.poll_enabled)
462162b6a57SDaniel Vetter 			schedule_delayed_work(&dev->mode_config.output_poll_work,
463162b6a57SDaniel Vetter 					      0);
464162b6a57SDaniel Vetter 	}
4658d754544SDaniel Vetter 
4668d754544SDaniel Vetter 	/* Re-enable polling in case the global poll config changed. */
4678d754544SDaniel Vetter 	if (drm_kms_helper_poll != dev->mode_config.poll_running)
468c4d79c22SDave Airlie 		drm_kms_helper_poll_enable(dev);
4698d754544SDaniel Vetter 
4708d754544SDaniel Vetter 	dev->mode_config.poll_running = drm_kms_helper_poll;
4718d754544SDaniel Vetter 
4728d754544SDaniel Vetter 	if (connector->status == connector_status_disconnected) {
4738d754544SDaniel Vetter 		DRM_DEBUG_KMS("[CONNECTOR:%d:%s] disconnected\n",
47425933820SJani Nikula 			connector->base.id, connector->name);
475c555f023SDaniel Vetter 		drm_connector_update_edid_property(connector, NULL);
4768d754544SDaniel Vetter 		verbose_prune = false;
4778d754544SDaniel Vetter 		goto prune;
4788d754544SDaniel Vetter 	}
4798d754544SDaniel Vetter 
4808d754544SDaniel Vetter 	count = (*connector_funcs->get_modes)(connector);
4818d754544SDaniel Vetter 
482*48eaeb76SJani Nikula 	/*
483*48eaeb76SJani Nikula 	 * Fallback for when DDC probe failed in drm_get_edid() and thus skipped
484*48eaeb76SJani Nikula 	 * override/firmware EDID.
485*48eaeb76SJani Nikula 	 */
486*48eaeb76SJani Nikula 	if (count == 0 && connector->status == connector_status_connected)
487*48eaeb76SJani Nikula 		count = drm_add_override_edid_modes(connector);
488*48eaeb76SJani Nikula 
4898d754544SDaniel Vetter 	if (count == 0 && connector->status == connector_status_connected)
4908d754544SDaniel Vetter 		count = drm_add_modes_noedid(connector, 1024, 768);
491eaf99c74SChris Wilson 	count += drm_helper_probe_add_cmdline_mode(connector);
4928d754544SDaniel Vetter 	if (count == 0)
4938d754544SDaniel Vetter 		goto prune;
4948d754544SDaniel Vetter 
49597e14fbeSDaniel Vetter 	drm_connector_list_update(connector);
4968d754544SDaniel Vetter 
4978d754544SDaniel Vetter 	if (connector->interlace_allowed)
4988d754544SDaniel Vetter 		mode_flags |= DRM_MODE_FLAG_INTERLACE;
4998d754544SDaniel Vetter 	if (connector->doublescan_allowed)
5008d754544SDaniel Vetter 		mode_flags |= DRM_MODE_FLAG_DBLSCAN;
5018d754544SDaniel Vetter 	if (connector->stereo_allowed)
5028d754544SDaniel Vetter 		mode_flags |= DRM_MODE_FLAG_3D_MASK;
5038d754544SDaniel Vetter 
5048d754544SDaniel Vetter 	list_for_each_entry(mode, &connector->modes, head) {
505be8719a6SVille Syrjälä 		if (mode->status == MODE_OK)
50675a655e0SVille Syrjälä 			mode->status = drm_mode_validate_driver(dev, mode);
507abc0b144SVille Syrjälä 
508abc0b144SVille Syrjälä 		if (mode->status == MODE_OK)
50905acaec3SVille Syrjälä 			mode->status = drm_mode_validate_size(mode, maxX, maxY);
51005acaec3SVille Syrjälä 
51105acaec3SVille Syrjälä 		if (mode->status == MODE_OK)
51205acaec3SVille Syrjälä 			mode->status = drm_mode_validate_flag(mode, mode_flags);
51305acaec3SVille Syrjälä 
51497f973d6SJose Abreu 		if (mode->status == MODE_OK)
51597f973d6SJose Abreu 			mode->status = drm_mode_validate_pipeline(mode,
51697f973d6SJose Abreu 								  connector);
517d8523153SShashank Sharma 
518d8523153SShashank Sharma 		if (mode->status == MODE_OK)
519d8523153SShashank Sharma 			mode->status = drm_mode_validate_ycbcr420(mode,
520d8523153SShashank Sharma 								  connector);
5218d754544SDaniel Vetter 	}
5228d754544SDaniel Vetter 
5238d754544SDaniel Vetter prune:
5248d754544SDaniel Vetter 	drm_mode_prune_invalid(dev, &connector->modes, verbose_prune);
5258d754544SDaniel Vetter 
5266c5ed5aeSMaarten Lankhorst 	drm_modeset_drop_locks(&ctx);
5276c5ed5aeSMaarten Lankhorst 	drm_modeset_acquire_fini(&ctx);
5286c5ed5aeSMaarten Lankhorst 
5298d754544SDaniel Vetter 	if (list_empty(&connector->modes))
5308d754544SDaniel Vetter 		return 0;
5318d754544SDaniel Vetter 
5328d754544SDaniel Vetter 	list_for_each_entry(mode, &connector->modes, head)
5338d754544SDaniel Vetter 		mode->vrefresh = drm_mode_vrefresh(mode);
5348d754544SDaniel Vetter 
5358d754544SDaniel Vetter 	drm_mode_sort(&connector->modes);
5368d754544SDaniel Vetter 
5378d754544SDaniel Vetter 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s] probed modes :\n", connector->base.id,
53825933820SJani Nikula 			connector->name);
5398d754544SDaniel Vetter 	list_for_each_entry(mode, &connector->modes, head) {
5408d754544SDaniel Vetter 		drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
5418d754544SDaniel Vetter 		drm_mode_debug_printmodeline(mode);
5428d754544SDaniel Vetter 	}
5438d754544SDaniel Vetter 
5448d754544SDaniel Vetter 	return count;
5458d754544SDaniel Vetter }
5468d754544SDaniel Vetter EXPORT_SYMBOL(drm_helper_probe_single_connector_modes);
5478d754544SDaniel Vetter 
5488d754544SDaniel Vetter /**
5498d754544SDaniel Vetter  * drm_kms_helper_hotplug_event - fire off KMS hotplug events
5508d754544SDaniel Vetter  * @dev: drm_device whose connector state changed
5518d754544SDaniel Vetter  *
5528d754544SDaniel Vetter  * This function fires off the uevent for userspace and also calls the
5538d754544SDaniel Vetter  * output_poll_changed function, which is most commonly used to inform the fbdev
5548d754544SDaniel Vetter  * emulation code and allow it to update the fbcon output configuration.
5558d754544SDaniel Vetter  *
5568d754544SDaniel Vetter  * Drivers should call this from their hotplug handling code when a change is
5578d754544SDaniel Vetter  * detected. Note that this function does not do any output detection of its
5588d754544SDaniel Vetter  * own, like drm_helper_hpd_irq_event() does - this is assumed to be done by the
5598d754544SDaniel Vetter  * driver already.
5608d754544SDaniel Vetter  *
5618d754544SDaniel Vetter  * This function must be called from process context with no mode
5628d754544SDaniel Vetter  * setting locks held.
5638d754544SDaniel Vetter  */
5648d754544SDaniel Vetter void drm_kms_helper_hotplug_event(struct drm_device *dev)
5658d754544SDaniel Vetter {
5668d754544SDaniel Vetter 	/* send a uevent + call fbdev */
5678d754544SDaniel Vetter 	drm_sysfs_hotplug_event(dev);
5688d754544SDaniel Vetter 	if (dev->mode_config.funcs->output_poll_changed)
5698d754544SDaniel Vetter 		dev->mode_config.funcs->output_poll_changed(dev);
570c76f0f7cSNoralf Trønnes 
571c76f0f7cSNoralf Trønnes 	drm_client_dev_hotplug(dev);
5728d754544SDaniel Vetter }
5738d754544SDaniel Vetter EXPORT_SYMBOL(drm_kms_helper_hotplug_event);
5748d754544SDaniel Vetter 
5758d754544SDaniel Vetter static void output_poll_execute(struct work_struct *work)
5768d754544SDaniel Vetter {
5778d754544SDaniel Vetter 	struct delayed_work *delayed_work = to_delayed_work(work);
5788d754544SDaniel Vetter 	struct drm_device *dev = container_of(delayed_work, struct drm_device, mode_config.output_poll_work);
5798d754544SDaniel Vetter 	struct drm_connector *connector;
580c36a3254SDaniel Vetter 	struct drm_connector_list_iter conn_iter;
5818d754544SDaniel Vetter 	enum drm_connector_status old_status;
582162b6a57SDaniel Vetter 	bool repoll = false, changed;
583162b6a57SDaniel Vetter 
584162b6a57SDaniel Vetter 	/* Pick up any changes detected by the probe functions. */
585162b6a57SDaniel Vetter 	changed = dev->mode_config.delayed_event;
586162b6a57SDaniel Vetter 	dev->mode_config.delayed_event = false;
5878d754544SDaniel Vetter 
5888d754544SDaniel Vetter 	if (!drm_kms_helper_poll)
589162b6a57SDaniel Vetter 		goto out;
5908d754544SDaniel Vetter 
5911fe7841dSChris Wilson 	if (!mutex_trylock(&dev->mode_config.mutex)) {
5921fe7841dSChris Wilson 		repoll = true;
5931fe7841dSChris Wilson 		goto out;
5941fe7841dSChris Wilson 	}
5951fe7841dSChris Wilson 
596b982dab1SThierry Reding 	drm_connector_list_iter_begin(dev, &conn_iter);
597c36a3254SDaniel Vetter 	drm_for_each_connector_iter(connector, &conn_iter) {
5988d754544SDaniel Vetter 		/* Ignore forced connectors. */
5998d754544SDaniel Vetter 		if (connector->force)
6008d754544SDaniel Vetter 			continue;
6018d754544SDaniel Vetter 
6028d754544SDaniel Vetter 		/* Ignore HPD capable connectors and connectors where we don't
6038d754544SDaniel Vetter 		 * want any hotplug detection at all for polling. */
6048d754544SDaniel Vetter 		if (!connector->polled || connector->polled == DRM_CONNECTOR_POLL_HPD)
6058d754544SDaniel Vetter 			continue;
6068d754544SDaniel Vetter 
6078d754544SDaniel Vetter 		old_status = connector->status;
6088d754544SDaniel Vetter 		/* if we are connected and don't want to poll for disconnect
6098d754544SDaniel Vetter 		   skip it */
6108d754544SDaniel Vetter 		if (old_status == connector_status_connected &&
6118d754544SDaniel Vetter 		    !(connector->polled & DRM_CONNECTOR_POLL_DISCONNECT))
6128d754544SDaniel Vetter 			continue;
6138d754544SDaniel Vetter 
614a3c6d686SJosef Holzmayr 		repoll = true;
615a3c6d686SJosef Holzmayr 
6166c5ed5aeSMaarten Lankhorst 		connector->status = drm_helper_probe_detect(connector, NULL, false);
6178d754544SDaniel Vetter 		if (old_status != connector->status) {
6188d754544SDaniel Vetter 			const char *old, *new;
6198d754544SDaniel Vetter 
620b7703726SDaniel Vetter 			/*
621b7703726SDaniel Vetter 			 * The poll work sets force=false when calling detect so
622b7703726SDaniel Vetter 			 * that drivers can avoid to do disruptive tests (e.g.
623b7703726SDaniel Vetter 			 * when load detect cycles could cause flickering on
624b7703726SDaniel Vetter 			 * other, running displays). This bears the risk that we
625b7703726SDaniel Vetter 			 * flip-flop between unknown here in the poll work and
626b7703726SDaniel Vetter 			 * the real state when userspace forces a full detect
627b7703726SDaniel Vetter 			 * call after receiving a hotplug event due to this
628b7703726SDaniel Vetter 			 * change.
629b7703726SDaniel Vetter 			 *
630b7703726SDaniel Vetter 			 * Hence clamp an unknown detect status to the old
631b7703726SDaniel Vetter 			 * value.
632b7703726SDaniel Vetter 			 */
633b7703726SDaniel Vetter 			if (connector->status == connector_status_unknown) {
634b7703726SDaniel Vetter 				connector->status = old_status;
635b7703726SDaniel Vetter 				continue;
636b7703726SDaniel Vetter 			}
637b7703726SDaniel Vetter 
6388d754544SDaniel Vetter 			old = drm_get_connector_status_name(old_status);
6398d754544SDaniel Vetter 			new = drm_get_connector_status_name(connector->status);
6408d754544SDaniel Vetter 
6418d754544SDaniel Vetter 			DRM_DEBUG_KMS("[CONNECTOR:%d:%s] "
6428d754544SDaniel Vetter 				      "status updated from %s to %s\n",
6438d754544SDaniel Vetter 				      connector->base.id,
64425933820SJani Nikula 				      connector->name,
6458d754544SDaniel Vetter 				      old, new);
6468d754544SDaniel Vetter 
6478d754544SDaniel Vetter 			changed = true;
6488d754544SDaniel Vetter 		}
6498d754544SDaniel Vetter 	}
650b982dab1SThierry Reding 	drm_connector_list_iter_end(&conn_iter);
6518d754544SDaniel Vetter 
6528d754544SDaniel Vetter 	mutex_unlock(&dev->mode_config.mutex);
6538d754544SDaniel Vetter 
654162b6a57SDaniel Vetter out:
6558d754544SDaniel Vetter 	if (changed)
6568d754544SDaniel Vetter 		drm_kms_helper_hotplug_event(dev);
6578d754544SDaniel Vetter 
6588d754544SDaniel Vetter 	if (repoll)
6598d754544SDaniel Vetter 		schedule_delayed_work(delayed_work, DRM_OUTPUT_POLL_PERIOD);
6608d754544SDaniel Vetter }
6618d754544SDaniel Vetter 
6628d754544SDaniel Vetter /**
66325c058ccSLukas Wunner  * drm_kms_helper_is_poll_worker - is %current task an output poll worker?
66425c058ccSLukas Wunner  *
66525c058ccSLukas Wunner  * Determine if %current task is an output poll worker.  This can be used
66625c058ccSLukas Wunner  * to select distinct code paths for output polling versus other contexts.
66725c058ccSLukas Wunner  *
66825c058ccSLukas Wunner  * One use case is to avoid a deadlock between the output poll worker and
66925c058ccSLukas Wunner  * the autosuspend worker wherein the latter waits for polling to finish
67025c058ccSLukas Wunner  * upon calling drm_kms_helper_poll_disable(), while the former waits for
67125c058ccSLukas Wunner  * runtime suspend to finish upon calling pm_runtime_get_sync() in a
67225c058ccSLukas Wunner  * connector ->detect hook.
67325c058ccSLukas Wunner  */
67425c058ccSLukas Wunner bool drm_kms_helper_is_poll_worker(void)
67525c058ccSLukas Wunner {
67625c058ccSLukas Wunner 	struct work_struct *work = current_work();
67725c058ccSLukas Wunner 
67825c058ccSLukas Wunner 	return work && work->func == output_poll_execute;
67925c058ccSLukas Wunner }
68025c058ccSLukas Wunner EXPORT_SYMBOL(drm_kms_helper_is_poll_worker);
68125c058ccSLukas Wunner 
68225c058ccSLukas Wunner /**
6838d754544SDaniel Vetter  * drm_kms_helper_poll_disable - disable output polling
6848d754544SDaniel Vetter  * @dev: drm_device
6858d754544SDaniel Vetter  *
6868d754544SDaniel Vetter  * This function disables the output polling work.
6878d754544SDaniel Vetter  *
6888d754544SDaniel Vetter  * Drivers can call this helper from their device suspend implementation. It is
689c4d79c22SDave Airlie  * not an error to call this even when output polling isn't enabled or already
690c4d79c22SDave Airlie  * disabled. Polling is re-enabled by calling drm_kms_helper_poll_enable().
691c4d79c22SDave Airlie  *
692c4d79c22SDave Airlie  * Note that calls to enable and disable polling must be strictly ordered, which
693c4d79c22SDave Airlie  * is automatically the case when they're only call from suspend/resume
694c4d79c22SDave Airlie  * callbacks.
6958d754544SDaniel Vetter  */
6968d754544SDaniel Vetter void drm_kms_helper_poll_disable(struct drm_device *dev)
6978d754544SDaniel Vetter {
6988d754544SDaniel Vetter 	if (!dev->mode_config.poll_enabled)
6998d754544SDaniel Vetter 		return;
7008d754544SDaniel Vetter 	cancel_delayed_work_sync(&dev->mode_config.output_poll_work);
7018d754544SDaniel Vetter }
7028d754544SDaniel Vetter EXPORT_SYMBOL(drm_kms_helper_poll_disable);
7038d754544SDaniel Vetter 
7048d754544SDaniel Vetter /**
7058d754544SDaniel Vetter  * drm_kms_helper_poll_init - initialize and enable output polling
7068d754544SDaniel Vetter  * @dev: drm_device
7078d754544SDaniel Vetter  *
7088d754544SDaniel Vetter  * This function intializes and then also enables output polling support for
7098d754544SDaniel Vetter  * @dev. Drivers which do not have reliable hotplug support in hardware can use
7108d754544SDaniel Vetter  * this helper infrastructure to regularly poll such connectors for changes in
7118d754544SDaniel Vetter  * their connection state.
7128d754544SDaniel Vetter  *
7138d754544SDaniel Vetter  * Drivers can control which connectors are polled by setting the
7148d754544SDaniel Vetter  * DRM_CONNECTOR_POLL_CONNECT and DRM_CONNECTOR_POLL_DISCONNECT flags. On
7158d754544SDaniel Vetter  * connectors where probing live outputs can result in visual distortion drivers
7168d754544SDaniel Vetter  * should not set the DRM_CONNECTOR_POLL_DISCONNECT flag to avoid this.
7178d754544SDaniel Vetter  * Connectors which have no flag or only DRM_CONNECTOR_POLL_HPD set are
7188d754544SDaniel Vetter  * completely ignored by the polling logic.
7198d754544SDaniel Vetter  *
7208d754544SDaniel Vetter  * Note that a connector can be both polled and probed from the hotplug handler,
7218d754544SDaniel Vetter  * in case the hotplug interrupt is known to be unreliable.
7228d754544SDaniel Vetter  */
7238d754544SDaniel Vetter void drm_kms_helper_poll_init(struct drm_device *dev)
7248d754544SDaniel Vetter {
7258d754544SDaniel Vetter 	INIT_DELAYED_WORK(&dev->mode_config.output_poll_work, output_poll_execute);
7268d754544SDaniel Vetter 	dev->mode_config.poll_enabled = true;
7278d754544SDaniel Vetter 
7288d754544SDaniel Vetter 	drm_kms_helper_poll_enable(dev);
7298d754544SDaniel Vetter }
7308d754544SDaniel Vetter EXPORT_SYMBOL(drm_kms_helper_poll_init);
7318d754544SDaniel Vetter 
7328d754544SDaniel Vetter /**
7338d754544SDaniel Vetter  * drm_kms_helper_poll_fini - disable output polling and clean it up
7348d754544SDaniel Vetter  * @dev: drm_device
7358d754544SDaniel Vetter  */
7368d754544SDaniel Vetter void drm_kms_helper_poll_fini(struct drm_device *dev)
7378d754544SDaniel Vetter {
7388d754544SDaniel Vetter 	drm_kms_helper_poll_disable(dev);
7398d754544SDaniel Vetter }
7408d754544SDaniel Vetter EXPORT_SYMBOL(drm_kms_helper_poll_fini);
7418d754544SDaniel Vetter 
7428d754544SDaniel Vetter /**
7438d754544SDaniel Vetter  * drm_helper_hpd_irq_event - hotplug processing
7448d754544SDaniel Vetter  * @dev: drm_device
7458d754544SDaniel Vetter  *
7468d754544SDaniel Vetter  * Drivers can use this helper function to run a detect cycle on all connectors
7478d754544SDaniel Vetter  * which have the DRM_CONNECTOR_POLL_HPD flag set in their &polled member. All
7488d754544SDaniel Vetter  * other connectors are ignored, which is useful to avoid reprobing fixed
7498d754544SDaniel Vetter  * panels.
7508d754544SDaniel Vetter  *
7518d754544SDaniel Vetter  * This helper function is useful for drivers which can't or don't track hotplug
7528d754544SDaniel Vetter  * interrupts for each connector.
7538d754544SDaniel Vetter  *
7548d754544SDaniel Vetter  * Drivers which support hotplug interrupts for each connector individually and
7558d754544SDaniel Vetter  * which have a more fine-grained detect logic should bypass this code and
7568d754544SDaniel Vetter  * directly call drm_kms_helper_hotplug_event() in case the connector state
7578d754544SDaniel Vetter  * changed.
7588d754544SDaniel Vetter  *
7598d754544SDaniel Vetter  * This function must be called from process context with no mode
7608d754544SDaniel Vetter  * setting locks held.
7618d754544SDaniel Vetter  *
7628d754544SDaniel Vetter  * Note that a connector can be both polled and probed from the hotplug handler,
7638d754544SDaniel Vetter  * in case the hotplug interrupt is known to be unreliable.
7648d754544SDaniel Vetter  */
7658d754544SDaniel Vetter bool drm_helper_hpd_irq_event(struct drm_device *dev)
7668d754544SDaniel Vetter {
7678d754544SDaniel Vetter 	struct drm_connector *connector;
768c36a3254SDaniel Vetter 	struct drm_connector_list_iter conn_iter;
7698d754544SDaniel Vetter 	enum drm_connector_status old_status;
7708d754544SDaniel Vetter 	bool changed = false;
7718d754544SDaniel Vetter 
7728d754544SDaniel Vetter 	if (!dev->mode_config.poll_enabled)
7738d754544SDaniel Vetter 		return false;
7748d754544SDaniel Vetter 
7758d754544SDaniel Vetter 	mutex_lock(&dev->mode_config.mutex);
776b982dab1SThierry Reding 	drm_connector_list_iter_begin(dev, &conn_iter);
777c36a3254SDaniel Vetter 	drm_for_each_connector_iter(connector, &conn_iter) {
7788d754544SDaniel Vetter 		/* Only handle HPD capable connectors. */
7798d754544SDaniel Vetter 		if (!(connector->polled & DRM_CONNECTOR_POLL_HPD))
7808d754544SDaniel Vetter 			continue;
7818d754544SDaniel Vetter 
7828d754544SDaniel Vetter 		old_status = connector->status;
7838d754544SDaniel Vetter 
7846c5ed5aeSMaarten Lankhorst 		connector->status = drm_helper_probe_detect(connector, NULL, false);
7858d754544SDaniel Vetter 		DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
7868d754544SDaniel Vetter 			      connector->base.id,
78725933820SJani Nikula 			      connector->name,
7888d754544SDaniel Vetter 			      drm_get_connector_status_name(old_status),
7898d754544SDaniel Vetter 			      drm_get_connector_status_name(connector->status));
7908d754544SDaniel Vetter 		if (old_status != connector->status)
7918d754544SDaniel Vetter 			changed = true;
7928d754544SDaniel Vetter 	}
793b982dab1SThierry Reding 	drm_connector_list_iter_end(&conn_iter);
7948d754544SDaniel Vetter 	mutex_unlock(&dev->mode_config.mutex);
7958d754544SDaniel Vetter 
7968d754544SDaniel Vetter 	if (changed)
7978d754544SDaniel Vetter 		drm_kms_helper_hotplug_event(dev);
7988d754544SDaniel Vetter 
7998d754544SDaniel Vetter 	return changed;
8008d754544SDaniel Vetter }
8018d754544SDaniel Vetter EXPORT_SYMBOL(drm_helper_hpd_irq_event);
802