xref: /openbmc/linux/drivers/gpu/drm/drm_probe_helper.c (revision 1e952e95843d437b8a904dbd5b48d72db8ac23ec)
18d754544SDaniel Vetter /*
28d754544SDaniel Vetter  * Copyright (c) 2006-2008 Intel Corporation
38d754544SDaniel Vetter  * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
48d754544SDaniel Vetter  *
58d754544SDaniel Vetter  * DRM core CRTC related functions
68d754544SDaniel Vetter  *
78d754544SDaniel Vetter  * Permission to use, copy, modify, distribute, and sell this software and its
88d754544SDaniel Vetter  * documentation for any purpose is hereby granted without fee, provided that
98d754544SDaniel Vetter  * the above copyright notice appear in all copies and that both that copyright
108d754544SDaniel Vetter  * notice and this permission notice appear in supporting documentation, and
118d754544SDaniel Vetter  * that the name of the copyright holders not be used in advertising or
128d754544SDaniel Vetter  * publicity pertaining to distribution of the software without specific,
138d754544SDaniel Vetter  * written prior permission.  The copyright holders make no representations
148d754544SDaniel Vetter  * about the suitability of this software for any purpose.  It is provided "as
158d754544SDaniel Vetter  * is" without express or implied warranty.
168d754544SDaniel Vetter  *
178d754544SDaniel Vetter  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
188d754544SDaniel Vetter  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
198d754544SDaniel Vetter  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
208d754544SDaniel Vetter  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
218d754544SDaniel Vetter  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
228d754544SDaniel Vetter  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
238d754544SDaniel Vetter  * OF THIS SOFTWARE.
248d754544SDaniel Vetter  *
258d754544SDaniel Vetter  * Authors:
268d754544SDaniel Vetter  *      Keith Packard
278d754544SDaniel Vetter  *	Eric Anholt <eric@anholt.net>
288d754544SDaniel Vetter  *      Dave Airlie <airlied@linux.ie>
298d754544SDaniel Vetter  *      Jesse Barnes <jesse.barnes@intel.com>
308d754544SDaniel Vetter  */
318d754544SDaniel Vetter 
328d754544SDaniel Vetter #include <linux/export.h>
338d754544SDaniel Vetter #include <linux/moduleparam.h>
348d754544SDaniel Vetter 
35ee68c743SBoris Brezillon #include <drm/drm_bridge.h>
36c76f0f7cSNoralf Trønnes #include <drm/drm_client.h>
378d754544SDaniel Vetter #include <drm/drm_crtc.h>
388d754544SDaniel Vetter #include <drm/drm_edid.h>
390500c04eSSam Ravnborg #include <drm/drm_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
drm_mode_validate_flag(const struct drm_display_mode * mode,int flags)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 
881c26b8e0SLyude Paul static int
drm_mode_validate_pipeline(struct drm_display_mode * mode,struct drm_connector * connector,struct drm_modeset_acquire_ctx * ctx,enum drm_mode_status * status)8997f973d6SJose Abreu drm_mode_validate_pipeline(struct drm_display_mode *mode,
901c26b8e0SLyude Paul 			   struct drm_connector *connector,
911c26b8e0SLyude Paul 			   struct drm_modeset_acquire_ctx *ctx,
921c26b8e0SLyude Paul 			   enum drm_mode_status *status)
9397f973d6SJose Abreu {
9497f973d6SJose Abreu 	struct drm_device *dev = connector->dev;
9583aefbb8SVille Syrjälä 	struct drm_encoder *encoder;
961c26b8e0SLyude Paul 	int ret;
9797f973d6SJose Abreu 
9897f973d6SJose Abreu 	/* Step 1: Validate against connector */
991c26b8e0SLyude Paul 	ret = drm_connector_mode_valid(connector, mode, ctx, status);
1001c26b8e0SLyude Paul 	if (ret || *status != MODE_OK)
10197f973d6SJose Abreu 		return ret;
10297f973d6SJose Abreu 
10397f973d6SJose Abreu 	/* Step 2: Validate against encoders and crtcs */
10462afb4adSJosé Roberto de Souza 	drm_connector_for_each_possible_encoder(connector, encoder) {
10535a61fe9SBoris Brezillon 		struct drm_bridge *bridge;
10697f973d6SJose Abreu 		struct drm_crtc *crtc;
10797f973d6SJose Abreu 
1081c26b8e0SLyude Paul 		*status = drm_encoder_mode_valid(encoder, mode);
1091c26b8e0SLyude Paul 		if (*status != MODE_OK) {
11097f973d6SJose Abreu 			/* No point in continuing for crtc check as this encoder
11197f973d6SJose Abreu 			 * will not accept the mode anyway. If all encoders
11297f973d6SJose Abreu 			 * reject the mode then, at exit, ret will not be
11397f973d6SJose Abreu 			 * MODE_OK. */
11497f973d6SJose Abreu 			continue;
11597f973d6SJose Abreu 		}
11697f973d6SJose Abreu 
11735a61fe9SBoris Brezillon 		bridge = drm_bridge_chain_get_first_bridge(encoder);
118418eda8fSDave Airlie 		*status = drm_bridge_chain_mode_valid(bridge,
11912c683e1SLaurent Pinchart 						      &connector->display_info,
12012c683e1SLaurent Pinchart 						      mode);
1211c26b8e0SLyude Paul 		if (*status != MODE_OK) {
12297f973d6SJose Abreu 			/* There is also no point in continuing for crtc check
12397f973d6SJose Abreu 			 * here. */
12497f973d6SJose Abreu 			continue;
12597f973d6SJose Abreu 		}
12697f973d6SJose Abreu 
12797f973d6SJose Abreu 		drm_for_each_crtc(crtc, dev) {
12897f973d6SJose Abreu 			if (!drm_encoder_crtc_ok(encoder, crtc))
12997f973d6SJose Abreu 				continue;
13097f973d6SJose Abreu 
1311c26b8e0SLyude Paul 			*status = drm_crtc_mode_valid(crtc, mode);
1321c26b8e0SLyude Paul 			if (*status == MODE_OK) {
13397f973d6SJose Abreu 				/* If we get to this point there is at least
13497f973d6SJose Abreu 				 * one combination of encoder+crtc that works
13597f973d6SJose Abreu 				 * for this mode. Lets return now. */
1361c26b8e0SLyude Paul 				return 0;
13797f973d6SJose Abreu 			}
13897f973d6SJose Abreu 		}
13997f973d6SJose Abreu 	}
14097f973d6SJose Abreu 
1411c26b8e0SLyude Paul 	return 0;
14297f973d6SJose Abreu }
14397f973d6SJose Abreu 
drm_helper_probe_add_cmdline_mode(struct drm_connector * connector)144eaf99c74SChris Wilson static int drm_helper_probe_add_cmdline_mode(struct drm_connector *connector)
145eaf99c74SChris Wilson {
1465f0c3f99SChris Wilson 	struct drm_cmdline_mode *cmdline_mode;
147eaf99c74SChris Wilson 	struct drm_display_mode *mode;
148eaf99c74SChris Wilson 
1495f0c3f99SChris Wilson 	cmdline_mode = &connector->cmdline_mode;
1505f0c3f99SChris Wilson 	if (!cmdline_mode->specified)
151eaf99c74SChris Wilson 		return 0;
152eaf99c74SChris Wilson 
1535f0c3f99SChris Wilson 	/* Only add a GTF mode if we find no matching probed modes */
1545f0c3f99SChris Wilson 	list_for_each_entry(mode, &connector->probed_modes, head) {
1555f0c3f99SChris Wilson 		if (mode->hdisplay != cmdline_mode->xres ||
1565f0c3f99SChris Wilson 		    mode->vdisplay != cmdline_mode->yres)
1575f0c3f99SChris Wilson 			continue;
1585f0c3f99SChris Wilson 
1595f0c3f99SChris Wilson 		if (cmdline_mode->refresh_specified) {
1605f0c3f99SChris Wilson 			/* The probed mode's vrefresh is set until later */
1615f0c3f99SChris Wilson 			if (drm_mode_vrefresh(mode) != cmdline_mode->refresh)
1625f0c3f99SChris Wilson 				continue;
1635f0c3f99SChris Wilson 		}
1645f0c3f99SChris Wilson 
165ebdc02ddSHans de Goede 		/* Mark the matching mode as being preferred by the user */
166ebdc02ddSHans de Goede 		mode->type |= DRM_MODE_TYPE_USERDEF;
1675f0c3f99SChris Wilson 		return 0;
1685f0c3f99SChris Wilson 	}
1695f0c3f99SChris Wilson 
170eaf99c74SChris Wilson 	mode = drm_mode_create_from_cmdline_mode(connector->dev,
1715f0c3f99SChris Wilson 						 cmdline_mode);
172eaf99c74SChris Wilson 	if (mode == NULL)
173eaf99c74SChris Wilson 		return 0;
174eaf99c74SChris Wilson 
175eaf99c74SChris Wilson 	drm_mode_probed_add(connector, mode);
176eaf99c74SChris Wilson 	return 1;
177eaf99c74SChris Wilson }
178eaf99c74SChris Wilson 
drm_crtc_mode_valid(struct drm_crtc * crtc,const struct drm_display_mode * mode)179da251bf8SJose Abreu enum drm_mode_status drm_crtc_mode_valid(struct drm_crtc *crtc,
180da251bf8SJose Abreu 					 const struct drm_display_mode *mode)
181da251bf8SJose Abreu {
182da251bf8SJose Abreu 	const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
183da251bf8SJose Abreu 
184da251bf8SJose Abreu 	if (!crtc_funcs || !crtc_funcs->mode_valid)
185da251bf8SJose Abreu 		return MODE_OK;
186da251bf8SJose Abreu 
187da251bf8SJose Abreu 	return crtc_funcs->mode_valid(crtc, mode);
188da251bf8SJose Abreu }
189da251bf8SJose Abreu 
drm_encoder_mode_valid(struct drm_encoder * encoder,const struct drm_display_mode * mode)190da251bf8SJose Abreu enum drm_mode_status drm_encoder_mode_valid(struct drm_encoder *encoder,
191da251bf8SJose Abreu 					    const struct drm_display_mode *mode)
192da251bf8SJose Abreu {
193da251bf8SJose Abreu 	const struct drm_encoder_helper_funcs *encoder_funcs =
194da251bf8SJose Abreu 		encoder->helper_private;
195da251bf8SJose Abreu 
196da251bf8SJose Abreu 	if (!encoder_funcs || !encoder_funcs->mode_valid)
197da251bf8SJose Abreu 		return MODE_OK;
198da251bf8SJose Abreu 
199da251bf8SJose Abreu 	return encoder_funcs->mode_valid(encoder, mode);
200da251bf8SJose Abreu }
201da251bf8SJose Abreu 
2021c26b8e0SLyude Paul int
drm_connector_mode_valid(struct drm_connector * connector,struct drm_display_mode * mode,struct drm_modeset_acquire_ctx * ctx,enum drm_mode_status * status)2031c26b8e0SLyude Paul drm_connector_mode_valid(struct drm_connector *connector,
2041c26b8e0SLyude Paul 			 struct drm_display_mode *mode,
2051c26b8e0SLyude Paul 			 struct drm_modeset_acquire_ctx *ctx,
2061c26b8e0SLyude Paul 			 enum drm_mode_status *status)
207da251bf8SJose Abreu {
208da251bf8SJose Abreu 	const struct drm_connector_helper_funcs *connector_funcs =
209da251bf8SJose Abreu 		connector->helper_private;
2101c26b8e0SLyude Paul 	int ret = 0;
211da251bf8SJose Abreu 
2121c26b8e0SLyude Paul 	if (!connector_funcs)
2131c26b8e0SLyude Paul 		*status = MODE_OK;
2141c26b8e0SLyude Paul 	else if (connector_funcs->mode_valid_ctx)
2151c26b8e0SLyude Paul 		ret = connector_funcs->mode_valid_ctx(connector, mode, ctx,
2161c26b8e0SLyude Paul 						      status);
2171c26b8e0SLyude Paul 	else if (connector_funcs->mode_valid)
2181c26b8e0SLyude Paul 		*status = connector_funcs->mode_valid(connector, mode);
2191c26b8e0SLyude Paul 	else
2201c26b8e0SLyude Paul 		*status = MODE_OK;
221da251bf8SJose Abreu 
2221c26b8e0SLyude Paul 	return ret;
223da251bf8SJose Abreu }
224da251bf8SJose Abreu 
drm_kms_helper_disable_hpd(struct drm_device * dev)225cbf143b2SDmitry Baryshkov static void drm_kms_helper_disable_hpd(struct drm_device *dev)
226cbf143b2SDmitry Baryshkov {
227cbf143b2SDmitry Baryshkov 	struct drm_connector *connector;
228cbf143b2SDmitry Baryshkov 	struct drm_connector_list_iter conn_iter;
229cbf143b2SDmitry Baryshkov 
230cbf143b2SDmitry Baryshkov 	drm_connector_list_iter_begin(dev, &conn_iter);
231cbf143b2SDmitry Baryshkov 	drm_for_each_connector_iter(connector, &conn_iter) {
232cbf143b2SDmitry Baryshkov 		const struct drm_connector_helper_funcs *funcs =
233cbf143b2SDmitry Baryshkov 			connector->helper_private;
234cbf143b2SDmitry Baryshkov 
235cbf143b2SDmitry Baryshkov 		if (funcs && funcs->disable_hpd)
236cbf143b2SDmitry Baryshkov 			funcs->disable_hpd(connector);
237cbf143b2SDmitry Baryshkov 	}
238cbf143b2SDmitry Baryshkov 	drm_connector_list_iter_end(&conn_iter);
239cbf143b2SDmitry Baryshkov }
240cbf143b2SDmitry Baryshkov 
drm_kms_helper_enable_hpd(struct drm_device * dev)241cbf143b2SDmitry Baryshkov static bool drm_kms_helper_enable_hpd(struct drm_device *dev)
242cbf143b2SDmitry Baryshkov {
243cbf143b2SDmitry Baryshkov 	bool poll = false;
244cbf143b2SDmitry Baryshkov 	struct drm_connector *connector;
245cbf143b2SDmitry Baryshkov 	struct drm_connector_list_iter conn_iter;
246cbf143b2SDmitry Baryshkov 
247cbf143b2SDmitry Baryshkov 	drm_connector_list_iter_begin(dev, &conn_iter);
248cbf143b2SDmitry Baryshkov 	drm_for_each_connector_iter(connector, &conn_iter) {
249cbf143b2SDmitry Baryshkov 		const struct drm_connector_helper_funcs *funcs =
250cbf143b2SDmitry Baryshkov 			connector->helper_private;
251cbf143b2SDmitry Baryshkov 
252cbf143b2SDmitry Baryshkov 		if (funcs && funcs->enable_hpd)
253cbf143b2SDmitry Baryshkov 			funcs->enable_hpd(connector);
254cbf143b2SDmitry Baryshkov 
255cbf143b2SDmitry Baryshkov 		if (connector->polled & (DRM_CONNECTOR_POLL_CONNECT |
256cbf143b2SDmitry Baryshkov 					 DRM_CONNECTOR_POLL_DISCONNECT))
257cbf143b2SDmitry Baryshkov 			poll = true;
258cbf143b2SDmitry Baryshkov 	}
259cbf143b2SDmitry Baryshkov 	drm_connector_list_iter_end(&conn_iter);
260cbf143b2SDmitry Baryshkov 
261cbf143b2SDmitry Baryshkov 	return poll;
262cbf143b2SDmitry Baryshkov }
263cbf143b2SDmitry Baryshkov 
2648c4ccc4aSDaniel Vetter #define DRM_OUTPUT_POLL_PERIOD (10*HZ)
reschedule_output_poll_work(struct drm_device * dev)265a94e7ccfSImre Deak static void reschedule_output_poll_work(struct drm_device *dev)
266a94e7ccfSImre Deak {
267a94e7ccfSImre Deak 	unsigned long delay = DRM_OUTPUT_POLL_PERIOD;
268a94e7ccfSImre Deak 
269a94e7ccfSImre Deak 	if (dev->mode_config.delayed_event)
270a94e7ccfSImre Deak 		/*
271a94e7ccfSImre Deak 		 * FIXME:
272a94e7ccfSImre Deak 		 *
273a94e7ccfSImre Deak 		 * Use short (1s) delay to handle the initial delayed event.
274a94e7ccfSImre Deak 		 * This delay should not be needed, but Optimus/nouveau will
275a94e7ccfSImre Deak 		 * fail in a mysterious way if the delayed event is handled as
276a94e7ccfSImre Deak 		 * soon as possible like it is done in
277a94e7ccfSImre Deak 		 * drm_helper_probe_single_connector_modes() in case the poll
278a94e7ccfSImre Deak 		 * was enabled before.
279a94e7ccfSImre Deak 		 */
280a94e7ccfSImre Deak 		delay = HZ;
281a94e7ccfSImre Deak 
282a94e7ccfSImre Deak 	schedule_delayed_work(&dev->mode_config.output_poll_work, delay);
283a94e7ccfSImre Deak }
284a94e7ccfSImre Deak 
2854ad640e9SEgbert Eich /**
286c4d79c22SDave Airlie  * drm_kms_helper_poll_enable - re-enable output polling.
2874ad640e9SEgbert Eich  * @dev: drm_device
2884ad640e9SEgbert Eich  *
289c4d79c22SDave Airlie  * This function re-enables the output polling work, after it has been
290c4d79c22SDave Airlie  * temporarily disabled using drm_kms_helper_poll_disable(), for example over
291c4d79c22SDave Airlie  * suspend/resume.
2924ad640e9SEgbert Eich  *
293c4d79c22SDave Airlie  * Drivers can call this helper from their device resume implementation. It is
294b66d0f34SNoralf Trønnes  * not an error to call this even when output polling isn't enabled.
295c4d79c22SDave Airlie  *
2963d1b47e3SShradha Gupta  * If device polling was never initialized before, this call will trigger a
2973d1b47e3SShradha Gupta  * warning and return.
2983d1b47e3SShradha Gupta  *
299c4d79c22SDave Airlie  * Note that calls to enable and disable polling must be strictly ordered, which
300c4d79c22SDave Airlie  * is automatically the case when they're only call from suspend/resume
301c4d79c22SDave Airlie  * callbacks.
3024ad640e9SEgbert Eich  */
drm_kms_helper_poll_enable(struct drm_device * dev)303c4d79c22SDave Airlie void drm_kms_helper_poll_enable(struct drm_device *dev)
3048c4ccc4aSDaniel Vetter {
3053d1b47e3SShradha Gupta 	if (drm_WARN_ON_ONCE(dev, !dev->mode_config.poll_enabled) ||
3063d1b47e3SShradha Gupta 	    !drm_kms_helper_poll || dev->mode_config.poll_running)
3078c4ccc4aSDaniel Vetter 		return;
3088c4ccc4aSDaniel Vetter 
309a94e7ccfSImre Deak 	if (drm_kms_helper_enable_hpd(dev) ||
310a94e7ccfSImre Deak 	    dev->mode_config.delayed_event)
311a94e7ccfSImre Deak 		reschedule_output_poll_work(dev);
312d33a54e3SDmitry Baryshkov 
313d33a54e3SDmitry Baryshkov 	dev->mode_config.poll_running = true;
3148c4ccc4aSDaniel Vetter }
315c4d79c22SDave Airlie EXPORT_SYMBOL(drm_kms_helper_poll_enable);
3164ad640e9SEgbert Eich 
317a94e7ccfSImre Deak /**
318a94e7ccfSImre Deak  * drm_kms_helper_poll_reschedule - reschedule the output polling work
319a94e7ccfSImre Deak  * @dev: drm_device
320a94e7ccfSImre Deak  *
321a94e7ccfSImre Deak  * This function reschedules the output polling work, after polling for a
322a94e7ccfSImre Deak  * connector has been enabled.
323a94e7ccfSImre Deak  *
324a94e7ccfSImre Deak  * Drivers must call this helper after enabling polling for a connector by
325a94e7ccfSImre Deak  * setting %DRM_CONNECTOR_POLL_CONNECT / %DRM_CONNECTOR_POLL_DISCONNECT flags
326a94e7ccfSImre Deak  * in drm_connector::polled. Note that after disabling polling by clearing these
327a94e7ccfSImre Deak  * flags for a connector will stop the output polling work automatically if
328a94e7ccfSImre Deak  * the polling is disabled for all other connectors as well.
329a94e7ccfSImre Deak  *
330a94e7ccfSImre Deak  * The function can be called only after polling has been enabled by calling
331a94e7ccfSImre Deak  * drm_kms_helper_poll_init() / drm_kms_helper_poll_enable().
332a94e7ccfSImre Deak  */
drm_kms_helper_poll_reschedule(struct drm_device * dev)333a94e7ccfSImre Deak void drm_kms_helper_poll_reschedule(struct drm_device *dev)
334a94e7ccfSImre Deak {
335a94e7ccfSImre Deak 	if (dev->mode_config.poll_running)
336a94e7ccfSImre Deak 		reschedule_output_poll_work(dev);
337a94e7ccfSImre Deak }
338a94e7ccfSImre Deak EXPORT_SYMBOL(drm_kms_helper_poll_reschedule);
339a94e7ccfSImre Deak 
340949f0886SLaurent Pinchart static enum drm_connector_status
drm_helper_probe_detect_ctx(struct drm_connector * connector,bool force)3416c5ed5aeSMaarten Lankhorst drm_helper_probe_detect_ctx(struct drm_connector *connector, bool force)
342949f0886SLaurent Pinchart {
3436c5ed5aeSMaarten Lankhorst 	const struct drm_connector_helper_funcs *funcs = connector->helper_private;
3446c5ed5aeSMaarten Lankhorst 	struct drm_modeset_acquire_ctx ctx;
3456c5ed5aeSMaarten Lankhorst 	int ret;
3466c5ed5aeSMaarten Lankhorst 
3476c5ed5aeSMaarten Lankhorst 	drm_modeset_acquire_init(&ctx, 0);
3486c5ed5aeSMaarten Lankhorst 
3496c5ed5aeSMaarten Lankhorst retry:
3506c5ed5aeSMaarten Lankhorst 	ret = drm_modeset_lock(&connector->dev->mode_config.connection_mutex, &ctx);
3516c5ed5aeSMaarten Lankhorst 	if (!ret) {
3526c5ed5aeSMaarten Lankhorst 		if (funcs->detect_ctx)
3536c5ed5aeSMaarten Lankhorst 			ret = funcs->detect_ctx(connector, &ctx, force);
3546c5ed5aeSMaarten Lankhorst 		else if (connector->funcs->detect)
3556c5ed5aeSMaarten Lankhorst 			ret = connector->funcs->detect(connector, force);
3566c5ed5aeSMaarten Lankhorst 		else
3576c5ed5aeSMaarten Lankhorst 			ret = connector_status_connected;
358949f0886SLaurent Pinchart 	}
359949f0886SLaurent Pinchart 
3606c5ed5aeSMaarten Lankhorst 	if (ret == -EDEADLK) {
3616c5ed5aeSMaarten Lankhorst 		drm_modeset_backoff(&ctx);
3626c5ed5aeSMaarten Lankhorst 		goto retry;
3636c5ed5aeSMaarten Lankhorst 	}
3646c5ed5aeSMaarten Lankhorst 
3656c5ed5aeSMaarten Lankhorst 	if (WARN_ON(ret < 0))
3666c5ed5aeSMaarten Lankhorst 		ret = connector_status_unknown;
3676c5ed5aeSMaarten Lankhorst 
3685186421cSStanislav Lisovskiy 	if (ret != connector->status)
3695186421cSStanislav Lisovskiy 		connector->epoch_counter += 1;
3705186421cSStanislav Lisovskiy 
3716c5ed5aeSMaarten Lankhorst 	drm_modeset_drop_locks(&ctx);
3726c5ed5aeSMaarten Lankhorst 	drm_modeset_acquire_fini(&ctx);
3736c5ed5aeSMaarten Lankhorst 
3746c5ed5aeSMaarten Lankhorst 	return ret;
3756c5ed5aeSMaarten Lankhorst }
3766c5ed5aeSMaarten Lankhorst 
3776c5ed5aeSMaarten Lankhorst /**
3786c5ed5aeSMaarten Lankhorst  * drm_helper_probe_detect - probe connector status
3796c5ed5aeSMaarten Lankhorst  * @connector: connector to probe
3806c5ed5aeSMaarten Lankhorst  * @ctx: acquire_ctx, or NULL to let this function handle locking.
3816c5ed5aeSMaarten Lankhorst  * @force: Whether destructive probe operations should be performed.
3826c5ed5aeSMaarten Lankhorst  *
3836c5ed5aeSMaarten Lankhorst  * This function calls the detect callbacks of the connector.
3846c5ed5aeSMaarten Lankhorst  * This function returns &drm_connector_status, or
3856c5ed5aeSMaarten Lankhorst  * if @ctx is set, it might also return -EDEADLK.
3866c5ed5aeSMaarten Lankhorst  */
3876c5ed5aeSMaarten Lankhorst int
drm_helper_probe_detect(struct drm_connector * connector,struct drm_modeset_acquire_ctx * ctx,bool force)3886c5ed5aeSMaarten Lankhorst drm_helper_probe_detect(struct drm_connector *connector,
3896c5ed5aeSMaarten Lankhorst 			struct drm_modeset_acquire_ctx *ctx,
3906c5ed5aeSMaarten Lankhorst 			bool force)
3916c5ed5aeSMaarten Lankhorst {
3926c5ed5aeSMaarten Lankhorst 	const struct drm_connector_helper_funcs *funcs = connector->helper_private;
3936c5ed5aeSMaarten Lankhorst 	struct drm_device *dev = connector->dev;
3946c5ed5aeSMaarten Lankhorst 	int ret;
3956c5ed5aeSMaarten Lankhorst 
3966c5ed5aeSMaarten Lankhorst 	if (!ctx)
3976c5ed5aeSMaarten Lankhorst 		return drm_helper_probe_detect_ctx(connector, force);
3986c5ed5aeSMaarten Lankhorst 
3996c5ed5aeSMaarten Lankhorst 	ret = drm_modeset_lock(&dev->mode_config.connection_mutex, ctx);
4006c5ed5aeSMaarten Lankhorst 	if (ret)
4016c5ed5aeSMaarten Lankhorst 		return ret;
4026c5ed5aeSMaarten Lankhorst 
4036c5ed5aeSMaarten Lankhorst 	if (funcs->detect_ctx)
4045186421cSStanislav Lisovskiy 		ret = funcs->detect_ctx(connector, ctx, force);
4056c5ed5aeSMaarten Lankhorst 	else if (connector->funcs->detect)
4065186421cSStanislav Lisovskiy 		ret = connector->funcs->detect(connector, force);
4076c5ed5aeSMaarten Lankhorst 	else
4085186421cSStanislav Lisovskiy 		ret = connector_status_connected;
4095186421cSStanislav Lisovskiy 
4105186421cSStanislav Lisovskiy 	if (ret != connector->status)
4115186421cSStanislav Lisovskiy 		connector->epoch_counter += 1;
4125186421cSStanislav Lisovskiy 
4135186421cSStanislav Lisovskiy 	return ret;
4146c5ed5aeSMaarten Lankhorst }
4156c5ed5aeSMaarten Lankhorst EXPORT_SYMBOL(drm_helper_probe_detect);
4166c5ed5aeSMaarten Lankhorst 
drm_helper_probe_get_modes(struct drm_connector * connector)417019fd800SJani Nikula static int drm_helper_probe_get_modes(struct drm_connector *connector)
418019fd800SJani Nikula {
419019fd800SJani Nikula 	const struct drm_connector_helper_funcs *connector_funcs =
420019fd800SJani Nikula 		connector->helper_private;
421019fd800SJani Nikula 	int count;
422019fd800SJani Nikula 
423019fd800SJani Nikula 	count = connector_funcs->get_modes(connector);
424019fd800SJani Nikula 
42512bbe2c2SJani Nikula 	/* The .get_modes() callback should not return negative values. */
42612bbe2c2SJani Nikula 	if (count < 0) {
42712bbe2c2SJani Nikula 		drm_err(connector->dev, ".get_modes() returned %pe\n",
42812bbe2c2SJani Nikula 			ERR_PTR(count));
42912bbe2c2SJani Nikula 		count = 0;
43012bbe2c2SJani Nikula 	}
43112bbe2c2SJani Nikula 
432019fd800SJani Nikula 	/*
433019fd800SJani Nikula 	 * Fallback for when DDC probe failed in drm_get_edid() and thus skipped
434019fd800SJani Nikula 	 * override/firmware EDID.
435019fd800SJani Nikula 	 */
436019fd800SJani Nikula 	if (count == 0 && connector->status == connector_status_connected)
437019b9387SJani Nikula 		count = drm_edid_override_connector_update(connector);
438019fd800SJani Nikula 
439019fd800SJani Nikula 	return count;
440019fd800SJani Nikula }
441019fd800SJani Nikula 
__drm_helper_update_and_validate(struct drm_connector * connector,uint32_t maxX,uint32_t maxY,struct drm_modeset_acquire_ctx * ctx)4424a2a13a5SDouglas Anderson static int __drm_helper_update_and_validate(struct drm_connector *connector,
4434a2a13a5SDouglas Anderson 					    uint32_t maxX, uint32_t maxY,
4444a2a13a5SDouglas Anderson 					    struct drm_modeset_acquire_ctx *ctx)
4454a2a13a5SDouglas Anderson {
4464a2a13a5SDouglas Anderson 	struct drm_device *dev = connector->dev;
4474a2a13a5SDouglas Anderson 	struct drm_display_mode *mode;
4484a2a13a5SDouglas Anderson 	int mode_flags = 0;
4494a2a13a5SDouglas Anderson 	int ret;
4504a2a13a5SDouglas Anderson 
4514a2a13a5SDouglas Anderson 	drm_connector_list_update(connector);
4524a2a13a5SDouglas Anderson 
4534a2a13a5SDouglas Anderson 	if (connector->interlace_allowed)
4544a2a13a5SDouglas Anderson 		mode_flags |= DRM_MODE_FLAG_INTERLACE;
4554a2a13a5SDouglas Anderson 	if (connector->doublescan_allowed)
4564a2a13a5SDouglas Anderson 		mode_flags |= DRM_MODE_FLAG_DBLSCAN;
4574a2a13a5SDouglas Anderson 	if (connector->stereo_allowed)
4584a2a13a5SDouglas Anderson 		mode_flags |= DRM_MODE_FLAG_3D_MASK;
4594a2a13a5SDouglas Anderson 
4604a2a13a5SDouglas Anderson 	list_for_each_entry(mode, &connector->modes, head) {
4614a2a13a5SDouglas Anderson 		if (mode->status != MODE_OK)
4624a2a13a5SDouglas Anderson 			continue;
4634a2a13a5SDouglas Anderson 
4644a2a13a5SDouglas Anderson 		mode->status = drm_mode_validate_driver(dev, mode);
4654a2a13a5SDouglas Anderson 		if (mode->status != MODE_OK)
4664a2a13a5SDouglas Anderson 			continue;
4674a2a13a5SDouglas Anderson 
4684a2a13a5SDouglas Anderson 		mode->status = drm_mode_validate_size(mode, maxX, maxY);
4694a2a13a5SDouglas Anderson 		if (mode->status != MODE_OK)
4704a2a13a5SDouglas Anderson 			continue;
4714a2a13a5SDouglas Anderson 
4724a2a13a5SDouglas Anderson 		mode->status = drm_mode_validate_flag(mode, mode_flags);
4734a2a13a5SDouglas Anderson 		if (mode->status != MODE_OK)
4744a2a13a5SDouglas Anderson 			continue;
4754a2a13a5SDouglas Anderson 
4764a2a13a5SDouglas Anderson 		ret = drm_mode_validate_pipeline(mode, connector, ctx,
4774a2a13a5SDouglas Anderson 						 &mode->status);
4784a2a13a5SDouglas Anderson 		if (ret) {
4794a2a13a5SDouglas Anderson 			drm_dbg_kms(dev,
4804a2a13a5SDouglas Anderson 				    "drm_mode_validate_pipeline failed: %d\n",
4814a2a13a5SDouglas Anderson 				    ret);
4824a2a13a5SDouglas Anderson 
4834a2a13a5SDouglas Anderson 			if (drm_WARN_ON_ONCE(dev, ret != -EDEADLK))
4844a2a13a5SDouglas Anderson 				mode->status = MODE_ERROR;
4854a2a13a5SDouglas Anderson 			else
4864a2a13a5SDouglas Anderson 				return -EDEADLK;
4874a2a13a5SDouglas Anderson 		}
4884a2a13a5SDouglas Anderson 
4894a2a13a5SDouglas Anderson 		if (mode->status != MODE_OK)
4904a2a13a5SDouglas Anderson 			continue;
4914a2a13a5SDouglas Anderson 		mode->status = drm_mode_validate_ycbcr420(mode, connector);
4924a2a13a5SDouglas Anderson 	}
4934a2a13a5SDouglas Anderson 
4944a2a13a5SDouglas Anderson 	return 0;
4954a2a13a5SDouglas Anderson }
4964a2a13a5SDouglas Anderson 
4976af3e656SVille Syrjälä /**
4986af3e656SVille Syrjälä  * drm_helper_probe_single_connector_modes - get complete set of display modes
4996af3e656SVille Syrjälä  * @connector: connector to probe
5006af3e656SVille Syrjälä  * @maxX: max width for modes
5016af3e656SVille Syrjälä  * @maxY: max height for modes
5026af3e656SVille Syrjälä  *
5036af3e656SVille Syrjälä  * Based on the helper callbacks implemented by @connector in struct
5046af3e656SVille Syrjälä  * &drm_connector_helper_funcs try to detect all valid modes.  Modes will first
5056af3e656SVille Syrjälä  * be added to the connector's probed_modes list, then culled (based on validity
5066af3e656SVille Syrjälä  * and the @maxX, @maxY parameters) and put into the normal modes list.
5076af3e656SVille Syrjälä  *
5086806cdf9SDaniel Vetter  * Intended to be used as a generic implementation of the
5096806cdf9SDaniel Vetter  * &drm_connector_funcs.fill_modes() vfunc for drivers that use the CRTC helpers
5106806cdf9SDaniel Vetter  * for output mode filtering and detection.
5116af3e656SVille Syrjälä  *
5125dec293bSVille Syrjälä  * The basic procedure is as follows
5136af3e656SVille Syrjälä  *
5145dec293bSVille Syrjälä  * 1. All modes currently on the connector's modes list are marked as stale
5156af3e656SVille Syrjälä  *
5165dec293bSVille Syrjälä  * 2. New modes are added to the connector's probed_modes list with
5175dec293bSVille Syrjälä  *    drm_mode_probed_add(). New modes start their life with status as OK.
5185dec293bSVille Syrjälä  *    Modes are added from a single source using the following priority order.
5195dec293bSVille Syrjälä  *
5206806cdf9SDaniel Vetter  *    - &drm_connector_helper_funcs.get_modes vfunc
5215dec293bSVille Syrjälä  *    - if the connector status is connector_status_connected, standard
5225dec293bSVille Syrjälä  *      VESA DMT modes up to 1024x768 are automatically added
5235dec293bSVille Syrjälä  *      (drm_add_modes_noedid())
5245dec293bSVille Syrjälä  *
5255dec293bSVille Syrjälä  *    Finally modes specified via the kernel command line (video=...) are
5265dec293bSVille Syrjälä  *    added in addition to what the earlier probes produced
5275dec293bSVille Syrjälä  *    (drm_helper_probe_add_cmdline_mode()). These modes are generated
5285dec293bSVille Syrjälä  *    using the VESA GTF/CVT formulas.
5295dec293bSVille Syrjälä  *
5305dec293bSVille Syrjälä  * 3. Modes are moved from the probed_modes list to the modes list. Potential
53197e14fbeSDaniel Vetter  *    duplicates are merged together (see drm_connector_list_update()).
5325dec293bSVille Syrjälä  *    After this step the probed_modes list will be empty again.
5335dec293bSVille Syrjälä  *
5345dec293bSVille Syrjälä  * 4. Any non-stale mode on the modes list then undergoes validation
5355dec293bSVille Syrjälä  *
5365dec293bSVille Syrjälä  *    - drm_mode_validate_basic() performs basic sanity checks
5375dec293bSVille Syrjälä  *    - drm_mode_validate_size() filters out modes larger than @maxX and @maxY
5385dec293bSVille Syrjälä  *      (if specified)
53957366a8dSMasahiro Yamada  *    - drm_mode_validate_flag() checks the modes against basic connector
54057366a8dSMasahiro Yamada  *      capabilities (interlace_allowed,doublescan_allowed,stereo_allowed)
5411c26b8e0SLyude Paul  *    - the optional &drm_connector_helper_funcs.mode_valid or
5421c26b8e0SLyude Paul  *      &drm_connector_helper_funcs.mode_valid_ctx helpers can perform driver
5431c26b8e0SLyude Paul  *      and/or sink specific checks
54497f973d6SJose Abreu  *    - the optional &drm_crtc_helper_funcs.mode_valid,
54597f973d6SJose Abreu  *      &drm_bridge_funcs.mode_valid and &drm_encoder_helper_funcs.mode_valid
54697f973d6SJose Abreu  *      helpers can perform driver and/or source specific checks which are also
54797f973d6SJose Abreu  *      enforced by the modeset/atomic helpers
5485dec293bSVille Syrjälä  *
5495dec293bSVille Syrjälä  * 5. Any mode whose status is not OK is pruned from the connector's modes list,
5505dec293bSVille Syrjälä  *    accompanied by a debug message indicating the reason for the mode's
5515dec293bSVille Syrjälä  *    rejection (see drm_mode_prune_invalid()).
5526af3e656SVille Syrjälä  *
5536af3e656SVille Syrjälä  * Returns:
5546af3e656SVille Syrjälä  * The number of modes found on @connector.
5556af3e656SVille Syrjälä  */
drm_helper_probe_single_connector_modes(struct drm_connector * connector,uint32_t maxX,uint32_t maxY)5566af3e656SVille Syrjälä int drm_helper_probe_single_connector_modes(struct drm_connector *connector,
5576af3e656SVille Syrjälä 					    uint32_t maxX, uint32_t maxY)
5588d754544SDaniel Vetter {
5598d754544SDaniel Vetter 	struct drm_device *dev = connector->dev;
5608d754544SDaniel Vetter 	struct drm_display_mode *mode;
5616c5ed5aeSMaarten Lankhorst 	int count = 0, ret;
562162b6a57SDaniel Vetter 	enum drm_connector_status old_status;
5636c5ed5aeSMaarten Lankhorst 	struct drm_modeset_acquire_ctx ctx;
5648d754544SDaniel Vetter 
5658d754544SDaniel Vetter 	WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
5668d754544SDaniel Vetter 
5676c5ed5aeSMaarten Lankhorst 	drm_modeset_acquire_init(&ctx, 0);
5686c5ed5aeSMaarten Lankhorst 
5698d754544SDaniel Vetter 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", connector->base.id,
57025933820SJani Nikula 			connector->name);
5716c5ed5aeSMaarten Lankhorst 
5726c5ed5aeSMaarten Lankhorst retry:
5736c5ed5aeSMaarten Lankhorst 	ret = drm_modeset_lock(&dev->mode_config.connection_mutex, &ctx);
5746c5ed5aeSMaarten Lankhorst 	if (ret == -EDEADLK) {
5756c5ed5aeSMaarten Lankhorst 		drm_modeset_backoff(&ctx);
5766c5ed5aeSMaarten Lankhorst 		goto retry;
5776c5ed5aeSMaarten Lankhorst 	} else
5786c5ed5aeSMaarten Lankhorst 		WARN_ON(ret < 0);
5796c5ed5aeSMaarten Lankhorst 
5805ba89406SVille Syrjälä 	/* set all old modes to the stale state */
5818d754544SDaniel Vetter 	list_for_each_entry(mode, &connector->modes, head)
5825ba89406SVille Syrjälä 		mode->status = MODE_STALE;
5838d754544SDaniel Vetter 
584ed293f77SDaniel Vetter 	old_status = connector->status;
585ed293f77SDaniel Vetter 
5868d754544SDaniel Vetter 	if (connector->force) {
5872c4cc91bSPeter Hurley 		if (connector->force == DRM_FORCE_ON ||
5882c4cc91bSPeter Hurley 		    connector->force == DRM_FORCE_ON_DIGITAL)
5898d754544SDaniel Vetter 			connector->status = connector_status_connected;
5908d754544SDaniel Vetter 		else
5918d754544SDaniel Vetter 			connector->status = connector_status_disconnected;
5928d754544SDaniel Vetter 		if (connector->funcs->force)
5938d754544SDaniel Vetter 			connector->funcs->force(connector);
5948d754544SDaniel Vetter 	} else {
5956c5ed5aeSMaarten Lankhorst 		ret = drm_helper_probe_detect(connector, &ctx, true);
5966c5ed5aeSMaarten Lankhorst 
5976c5ed5aeSMaarten Lankhorst 		if (ret == -EDEADLK) {
5986c5ed5aeSMaarten Lankhorst 			drm_modeset_backoff(&ctx);
5996c5ed5aeSMaarten Lankhorst 			goto retry;
6006c5ed5aeSMaarten Lankhorst 		} else if (WARN(ret < 0, "Invalid return value %i for connector detection\n", ret))
6016c5ed5aeSMaarten Lankhorst 			ret = connector_status_unknown;
6026c5ed5aeSMaarten Lankhorst 
6036c5ed5aeSMaarten Lankhorst 		connector->status = ret;
604ed293f77SDaniel Vetter 	}
605162b6a57SDaniel Vetter 
606162b6a57SDaniel Vetter 	/*
607162b6a57SDaniel Vetter 	 * Normally either the driver's hpd code or the poll loop should
608162b6a57SDaniel Vetter 	 * pick up any changes and fire the hotplug event. But if
609162b6a57SDaniel Vetter 	 * userspace sneaks in a probe, we might miss a change. Hence
610162b6a57SDaniel Vetter 	 * check here, and if anything changed start the hotplug code.
611162b6a57SDaniel Vetter 	 */
612162b6a57SDaniel Vetter 	if (old_status != connector->status) {
6134e15f2a1SJani Nikula 		DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
614162b6a57SDaniel Vetter 			      connector->base.id,
615162b6a57SDaniel Vetter 			      connector->name,
6164e15f2a1SJani Nikula 			      drm_get_connector_status_name(old_status),
6174e15f2a1SJani Nikula 			      drm_get_connector_status_name(connector->status));
618162b6a57SDaniel Vetter 
619162b6a57SDaniel Vetter 		/*
620162b6a57SDaniel Vetter 		 * The hotplug event code might call into the fb
621162b6a57SDaniel Vetter 		 * helpers, and so expects that we do not hold any
622162b6a57SDaniel Vetter 		 * locks. Fire up the poll struct instead, it will
623162b6a57SDaniel Vetter 		 * disable itself again.
624162b6a57SDaniel Vetter 		 */
625162b6a57SDaniel Vetter 		dev->mode_config.delayed_event = true;
626162b6a57SDaniel Vetter 		if (dev->mode_config.poll_enabled)
627a8e47884SDom Cobley 			mod_delayed_work(system_wq,
628a8e47884SDom Cobley 					 &dev->mode_config.output_poll_work,
629162b6a57SDaniel Vetter 					 0);
630162b6a57SDaniel Vetter 	}
6318d754544SDaniel Vetter 
632*5c1dc516SShradha Gupta 	/*
633*5c1dc516SShradha Gupta 	 * Re-enable polling in case the global poll config changed but polling
634*5c1dc516SShradha Gupta 	 * is still initialized.
635*5c1dc516SShradha Gupta 	 */
636*5c1dc516SShradha Gupta 	if (dev->mode_config.poll_enabled)
637c4d79c22SDave Airlie 		drm_kms_helper_poll_enable(dev);
6388d754544SDaniel Vetter 
6398d754544SDaniel Vetter 	if (connector->status == connector_status_disconnected) {
6408d754544SDaniel Vetter 		DRM_DEBUG_KMS("[CONNECTOR:%d:%s] disconnected\n",
64125933820SJani Nikula 			connector->base.id, connector->name);
642c555f023SDaniel Vetter 		drm_connector_update_edid_property(connector, NULL);
643e7c254d7SDouglas Anderson 		drm_mode_prune_invalid(dev, &connector->modes, false);
644e7c254d7SDouglas Anderson 		goto exit;
6458d754544SDaniel Vetter 	}
6468d754544SDaniel Vetter 
647019fd800SJani Nikula 	count = drm_helper_probe_get_modes(connector);
64848eaeb76SJani Nikula 
6492551952eSLaurent Pinchart 	if (count == 0 && (connector->status == connector_status_connected ||
650fae7d186SDouglas Anderson 			   connector->status == connector_status_unknown)) {
6518d754544SDaniel Vetter 		count = drm_add_modes_noedid(connector, 1024, 768);
652fae7d186SDouglas Anderson 
653fae7d186SDouglas Anderson 		/*
654fae7d186SDouglas Anderson 		 * Section 4.2.2.6 (EDID Corruption Detection) of the DP 1.4a
655fae7d186SDouglas Anderson 		 * Link CTS specifies that 640x480 (the official "failsafe"
656fae7d186SDouglas Anderson 		 * mode) needs to be the default if there's no EDID.
657fae7d186SDouglas Anderson 		 */
658fae7d186SDouglas Anderson 		if (connector->connector_type == DRM_MODE_CONNECTOR_DisplayPort)
659fae7d186SDouglas Anderson 			drm_set_preferred_mode(connector, 640, 480);
660fae7d186SDouglas Anderson 	}
661eaf99c74SChris Wilson 	count += drm_helper_probe_add_cmdline_mode(connector);
6624a2a13a5SDouglas Anderson 	if (count != 0) {
6634a2a13a5SDouglas Anderson 		ret = __drm_helper_update_and_validate(connector, maxX, maxY, &ctx);
6644a2a13a5SDouglas Anderson 		if (ret == -EDEADLK) {
6651c26b8e0SLyude Paul 			drm_modeset_backoff(&ctx);
6661c26b8e0SLyude Paul 			goto retry;
6671c26b8e0SLyude Paul 		}
6681c26b8e0SLyude Paul 	}
6691c26b8e0SLyude Paul 
670e7c254d7SDouglas Anderson 	drm_mode_prune_invalid(dev, &connector->modes, true);
6718d754544SDaniel Vetter 
672e7c254d7SDouglas Anderson 	/*
673e7c254d7SDouglas Anderson 	 * Displayport spec section 5.2.1.2 ("Video Timing Format") says that
674e7c254d7SDouglas Anderson 	 * all detachable sinks shall support 640x480 @60Hz as a fail safe
675e7c254d7SDouglas Anderson 	 * mode. If all modes were pruned, perhaps because they need more
676e7c254d7SDouglas Anderson 	 * lanes or a higher pixel clock than available, at least try to add
677e7c254d7SDouglas Anderson 	 * in 640x480.
678e7c254d7SDouglas Anderson 	 */
679e7c254d7SDouglas Anderson 	if (list_empty(&connector->modes) &&
680e7c254d7SDouglas Anderson 	    connector->connector_type == DRM_MODE_CONNECTOR_DisplayPort) {
681e7c254d7SDouglas Anderson 		count = drm_add_modes_noedid(connector, 640, 480);
682e7c254d7SDouglas Anderson 		ret = __drm_helper_update_and_validate(connector, maxX, maxY, &ctx);
683e7c254d7SDouglas Anderson 		if (ret == -EDEADLK) {
684e7c254d7SDouglas Anderson 			drm_modeset_backoff(&ctx);
685e7c254d7SDouglas Anderson 			goto retry;
686e7c254d7SDouglas Anderson 		}
687e7c254d7SDouglas Anderson 		drm_mode_prune_invalid(dev, &connector->modes, true);
688e7c254d7SDouglas Anderson 	}
689e7c254d7SDouglas Anderson 
690e7c254d7SDouglas Anderson exit:
6916c5ed5aeSMaarten Lankhorst 	drm_modeset_drop_locks(&ctx);
6926c5ed5aeSMaarten Lankhorst 	drm_modeset_acquire_fini(&ctx);
6936c5ed5aeSMaarten Lankhorst 
6948d754544SDaniel Vetter 	if (list_empty(&connector->modes))
6958d754544SDaniel Vetter 		return 0;
6968d754544SDaniel Vetter 
6978d754544SDaniel Vetter 	drm_mode_sort(&connector->modes);
6988d754544SDaniel Vetter 
6998d754544SDaniel Vetter 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s] probed modes :\n", connector->base.id,
70025933820SJani Nikula 			connector->name);
7018d754544SDaniel Vetter 	list_for_each_entry(mode, &connector->modes, head) {
7028d754544SDaniel Vetter 		drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
7038d754544SDaniel Vetter 		drm_mode_debug_printmodeline(mode);
7048d754544SDaniel Vetter 	}
7058d754544SDaniel Vetter 
7068d754544SDaniel Vetter 	return count;
7078d754544SDaniel Vetter }
7088d754544SDaniel Vetter EXPORT_SYMBOL(drm_helper_probe_single_connector_modes);
7098d754544SDaniel Vetter 
7108d754544SDaniel Vetter /**
7118d754544SDaniel Vetter  * drm_kms_helper_hotplug_event - fire off KMS hotplug events
7128d754544SDaniel Vetter  * @dev: drm_device whose connector state changed
7138d754544SDaniel Vetter  *
7148d754544SDaniel Vetter  * This function fires off the uevent for userspace and also calls the
7158d754544SDaniel Vetter  * output_poll_changed function, which is most commonly used to inform the fbdev
7168d754544SDaniel Vetter  * emulation code and allow it to update the fbcon output configuration.
7178d754544SDaniel Vetter  *
7188d754544SDaniel Vetter  * Drivers should call this from their hotplug handling code when a change is
7198d754544SDaniel Vetter  * detected. Note that this function does not do any output detection of its
7208d754544SDaniel Vetter  * own, like drm_helper_hpd_irq_event() does - this is assumed to be done by the
7218d754544SDaniel Vetter  * driver already.
7228d754544SDaniel Vetter  *
7238d754544SDaniel Vetter  * This function must be called from process context with no mode
7248d754544SDaniel Vetter  * setting locks held.
725710074bbSSimon Ser  *
726710074bbSSimon Ser  * If only a single connector has changed, consider calling
727710074bbSSimon Ser  * drm_kms_helper_connector_hotplug_event() instead.
7288d754544SDaniel Vetter  */
drm_kms_helper_hotplug_event(struct drm_device * dev)7298d754544SDaniel Vetter void drm_kms_helper_hotplug_event(struct drm_device *dev)
7308d754544SDaniel Vetter {
7318d754544SDaniel Vetter 	/* send a uevent + call fbdev */
7328d754544SDaniel Vetter 	drm_sysfs_hotplug_event(dev);
7338d754544SDaniel Vetter 	if (dev->mode_config.funcs->output_poll_changed)
7348d754544SDaniel Vetter 		dev->mode_config.funcs->output_poll_changed(dev);
735c76f0f7cSNoralf Trønnes 
736c76f0f7cSNoralf Trønnes 	drm_client_dev_hotplug(dev);
7378d754544SDaniel Vetter }
7388d754544SDaniel Vetter EXPORT_SYMBOL(drm_kms_helper_hotplug_event);
7398d754544SDaniel Vetter 
740710074bbSSimon Ser /**
741710074bbSSimon Ser  * drm_kms_helper_connector_hotplug_event - fire off a KMS connector hotplug event
742710074bbSSimon Ser  * @connector: drm_connector which has changed
743710074bbSSimon Ser  *
744710074bbSSimon Ser  * This is the same as drm_kms_helper_hotplug_event(), except it fires a more
745710074bbSSimon Ser  * fine-grained uevent for a single connector.
746710074bbSSimon Ser  */
drm_kms_helper_connector_hotplug_event(struct drm_connector * connector)747710074bbSSimon Ser void drm_kms_helper_connector_hotplug_event(struct drm_connector *connector)
748710074bbSSimon Ser {
749710074bbSSimon Ser 	struct drm_device *dev = connector->dev;
750710074bbSSimon Ser 
751710074bbSSimon Ser 	/* send a uevent + call fbdev */
752710074bbSSimon Ser 	drm_sysfs_connector_hotplug_event(connector);
753710074bbSSimon Ser 	if (dev->mode_config.funcs->output_poll_changed)
754710074bbSSimon Ser 		dev->mode_config.funcs->output_poll_changed(dev);
755710074bbSSimon Ser 
756710074bbSSimon Ser 	drm_client_dev_hotplug(dev);
757710074bbSSimon Ser }
758710074bbSSimon Ser EXPORT_SYMBOL(drm_kms_helper_connector_hotplug_event);
759710074bbSSimon Ser 
output_poll_execute(struct work_struct * work)7608d754544SDaniel Vetter static void output_poll_execute(struct work_struct *work)
7618d754544SDaniel Vetter {
7628d754544SDaniel Vetter 	struct delayed_work *delayed_work = to_delayed_work(work);
7638d754544SDaniel Vetter 	struct drm_device *dev = container_of(delayed_work, struct drm_device, mode_config.output_poll_work);
7648d754544SDaniel Vetter 	struct drm_connector *connector;
765c36a3254SDaniel Vetter 	struct drm_connector_list_iter conn_iter;
7668d754544SDaniel Vetter 	enum drm_connector_status old_status;
767162b6a57SDaniel Vetter 	bool repoll = false, changed;
768dc659a4eSNoralf Trønnes 	u64 old_epoch_counter;
769162b6a57SDaniel Vetter 
7703b295cb1SChris Wilson 	if (!dev->mode_config.poll_enabled)
7713b295cb1SChris Wilson 		return;
7723b295cb1SChris Wilson 
773162b6a57SDaniel Vetter 	/* Pick up any changes detected by the probe functions. */
774162b6a57SDaniel Vetter 	changed = dev->mode_config.delayed_event;
775162b6a57SDaniel Vetter 	dev->mode_config.delayed_event = false;
7768d754544SDaniel Vetter 
777d33a54e3SDmitry Baryshkov 	if (!drm_kms_helper_poll && dev->mode_config.poll_running) {
778d33a54e3SDmitry Baryshkov 		drm_kms_helper_disable_hpd(dev);
779d33a54e3SDmitry Baryshkov 		dev->mode_config.poll_running = false;
780162b6a57SDaniel Vetter 		goto out;
781d33a54e3SDmitry Baryshkov 	}
7828d754544SDaniel Vetter 
7831fe7841dSChris Wilson 	if (!mutex_trylock(&dev->mode_config.mutex)) {
7841fe7841dSChris Wilson 		repoll = true;
7851fe7841dSChris Wilson 		goto out;
7861fe7841dSChris Wilson 	}
7871fe7841dSChris Wilson 
788b982dab1SThierry Reding 	drm_connector_list_iter_begin(dev, &conn_iter);
789c36a3254SDaniel Vetter 	drm_for_each_connector_iter(connector, &conn_iter) {
7908d754544SDaniel Vetter 		/* Ignore forced connectors. */
7918d754544SDaniel Vetter 		if (connector->force)
7928d754544SDaniel Vetter 			continue;
7938d754544SDaniel Vetter 
7948d754544SDaniel Vetter 		/* Ignore HPD capable connectors and connectors where we don't
7958d754544SDaniel Vetter 		 * want any hotplug detection at all for polling. */
7968d754544SDaniel Vetter 		if (!connector->polled || connector->polled == DRM_CONNECTOR_POLL_HPD)
7978d754544SDaniel Vetter 			continue;
7988d754544SDaniel Vetter 
7998d754544SDaniel Vetter 		old_status = connector->status;
8008d754544SDaniel Vetter 		/* if we are connected and don't want to poll for disconnect
8018d754544SDaniel Vetter 		   skip it */
8028d754544SDaniel Vetter 		if (old_status == connector_status_connected &&
8038d754544SDaniel Vetter 		    !(connector->polled & DRM_CONNECTOR_POLL_DISCONNECT))
8048d754544SDaniel Vetter 			continue;
8058d754544SDaniel Vetter 
806a3c6d686SJosef Holzmayr 		repoll = true;
807a3c6d686SJosef Holzmayr 
808dc659a4eSNoralf Trønnes 		old_epoch_counter = connector->epoch_counter;
8096c5ed5aeSMaarten Lankhorst 		connector->status = drm_helper_probe_detect(connector, NULL, false);
810dc659a4eSNoralf Trønnes 		if (old_epoch_counter != connector->epoch_counter) {
8118d754544SDaniel Vetter 			const char *old, *new;
8128d754544SDaniel Vetter 
813b7703726SDaniel Vetter 			/*
814b7703726SDaniel Vetter 			 * The poll work sets force=false when calling detect so
815b7703726SDaniel Vetter 			 * that drivers can avoid to do disruptive tests (e.g.
816b7703726SDaniel Vetter 			 * when load detect cycles could cause flickering on
817b7703726SDaniel Vetter 			 * other, running displays). This bears the risk that we
818b7703726SDaniel Vetter 			 * flip-flop between unknown here in the poll work and
819b7703726SDaniel Vetter 			 * the real state when userspace forces a full detect
820b7703726SDaniel Vetter 			 * call after receiving a hotplug event due to this
821b7703726SDaniel Vetter 			 * change.
822b7703726SDaniel Vetter 			 *
823b7703726SDaniel Vetter 			 * Hence clamp an unknown detect status to the old
824b7703726SDaniel Vetter 			 * value.
825b7703726SDaniel Vetter 			 */
826b7703726SDaniel Vetter 			if (connector->status == connector_status_unknown) {
827b7703726SDaniel Vetter 				connector->status = old_status;
828b7703726SDaniel Vetter 				continue;
829b7703726SDaniel Vetter 			}
830b7703726SDaniel Vetter 
8318d754544SDaniel Vetter 			old = drm_get_connector_status_name(old_status);
8328d754544SDaniel Vetter 			new = drm_get_connector_status_name(connector->status);
8338d754544SDaniel Vetter 
8348d754544SDaniel Vetter 			DRM_DEBUG_KMS("[CONNECTOR:%d:%s] "
8358d754544SDaniel Vetter 				      "status updated from %s to %s\n",
8368d754544SDaniel Vetter 				      connector->base.id,
83725933820SJani Nikula 				      connector->name,
8388d754544SDaniel Vetter 				      old, new);
839dc659a4eSNoralf Trønnes 			DRM_DEBUG_KMS("[CONNECTOR:%d:%s] epoch counter %llu -> %llu\n",
840dc659a4eSNoralf Trønnes 				      connector->base.id, connector->name,
841dc659a4eSNoralf Trønnes 				      old_epoch_counter, connector->epoch_counter);
8428d754544SDaniel Vetter 
8438d754544SDaniel Vetter 			changed = true;
8448d754544SDaniel Vetter 		}
8458d754544SDaniel Vetter 	}
846b982dab1SThierry Reding 	drm_connector_list_iter_end(&conn_iter);
8478d754544SDaniel Vetter 
8488d754544SDaniel Vetter 	mutex_unlock(&dev->mode_config.mutex);
8498d754544SDaniel Vetter 
850162b6a57SDaniel Vetter out:
8518d754544SDaniel Vetter 	if (changed)
8528d754544SDaniel Vetter 		drm_kms_helper_hotplug_event(dev);
8538d754544SDaniel Vetter 
8548d754544SDaniel Vetter 	if (repoll)
8558d754544SDaniel Vetter 		schedule_delayed_work(delayed_work, DRM_OUTPUT_POLL_PERIOD);
8568d754544SDaniel Vetter }
8578d754544SDaniel Vetter 
8588d754544SDaniel Vetter /**
85925c058ccSLukas Wunner  * drm_kms_helper_is_poll_worker - is %current task an output poll worker?
86025c058ccSLukas Wunner  *
86125c058ccSLukas Wunner  * Determine if %current task is an output poll worker.  This can be used
86225c058ccSLukas Wunner  * to select distinct code paths for output polling versus other contexts.
86325c058ccSLukas Wunner  *
86425c058ccSLukas Wunner  * One use case is to avoid a deadlock between the output poll worker and
86525c058ccSLukas Wunner  * the autosuspend worker wherein the latter waits for polling to finish
86625c058ccSLukas Wunner  * upon calling drm_kms_helper_poll_disable(), while the former waits for
86725c058ccSLukas Wunner  * runtime suspend to finish upon calling pm_runtime_get_sync() in a
86825c058ccSLukas Wunner  * connector ->detect hook.
86925c058ccSLukas Wunner  */
drm_kms_helper_is_poll_worker(void)87025c058ccSLukas Wunner bool drm_kms_helper_is_poll_worker(void)
87125c058ccSLukas Wunner {
87225c058ccSLukas Wunner 	struct work_struct *work = current_work();
87325c058ccSLukas Wunner 
87425c058ccSLukas Wunner 	return work && work->func == output_poll_execute;
87525c058ccSLukas Wunner }
87625c058ccSLukas Wunner EXPORT_SYMBOL(drm_kms_helper_is_poll_worker);
87725c058ccSLukas Wunner 
87825c058ccSLukas Wunner /**
8798d754544SDaniel Vetter  * drm_kms_helper_poll_disable - disable output polling
8808d754544SDaniel Vetter  * @dev: drm_device
8818d754544SDaniel Vetter  *
8828d754544SDaniel Vetter  * This function disables the output polling work.
8838d754544SDaniel Vetter  *
8848d754544SDaniel Vetter  * Drivers can call this helper from their device suspend implementation. It is
885c4d79c22SDave Airlie  * not an error to call this even when output polling isn't enabled or already
886c4d79c22SDave Airlie  * disabled. Polling is re-enabled by calling drm_kms_helper_poll_enable().
887c4d79c22SDave Airlie  *
8883d1b47e3SShradha Gupta  * If however, the polling was never initialized, this call will trigger a
8893d1b47e3SShradha Gupta  * warning and return
8903d1b47e3SShradha Gupta  *
891c4d79c22SDave Airlie  * Note that calls to enable and disable polling must be strictly ordered, which
892c4d79c22SDave Airlie  * is automatically the case when they're only call from suspend/resume
893c4d79c22SDave Airlie  * callbacks.
8948d754544SDaniel Vetter  */
drm_kms_helper_poll_disable(struct drm_device * dev)8958d754544SDaniel Vetter void drm_kms_helper_poll_disable(struct drm_device *dev)
8968d754544SDaniel Vetter {
8973d1b47e3SShradha Gupta 	if (drm_WARN_ON(dev, !dev->mode_config.poll_enabled))
8983d1b47e3SShradha Gupta 		return;
8993d1b47e3SShradha Gupta 
900d33a54e3SDmitry Baryshkov 	if (dev->mode_config.poll_running)
901d33a54e3SDmitry Baryshkov 		drm_kms_helper_disable_hpd(dev);
902d33a54e3SDmitry Baryshkov 
903d33a54e3SDmitry Baryshkov 	cancel_delayed_work_sync(&dev->mode_config.output_poll_work);
904d33a54e3SDmitry Baryshkov 
905d33a54e3SDmitry Baryshkov 	dev->mode_config.poll_running = false;
9068d754544SDaniel Vetter }
9078d754544SDaniel Vetter EXPORT_SYMBOL(drm_kms_helper_poll_disable);
9088d754544SDaniel Vetter 
9098d754544SDaniel Vetter /**
9108d754544SDaniel Vetter  * drm_kms_helper_poll_init - initialize and enable output polling
9118d754544SDaniel Vetter  * @dev: drm_device
9128d754544SDaniel Vetter  *
9130ae865efSCai Huoqing  * This function initializes and then also enables output polling support for
9148d754544SDaniel Vetter  * @dev. Drivers which do not have reliable hotplug support in hardware can use
9158d754544SDaniel Vetter  * this helper infrastructure to regularly poll such connectors for changes in
9168d754544SDaniel Vetter  * their connection state.
9178d754544SDaniel Vetter  *
9188d754544SDaniel Vetter  * Drivers can control which connectors are polled by setting the
9198d754544SDaniel Vetter  * DRM_CONNECTOR_POLL_CONNECT and DRM_CONNECTOR_POLL_DISCONNECT flags. On
9208d754544SDaniel Vetter  * connectors where probing live outputs can result in visual distortion drivers
9218d754544SDaniel Vetter  * should not set the DRM_CONNECTOR_POLL_DISCONNECT flag to avoid this.
9228d754544SDaniel Vetter  * Connectors which have no flag or only DRM_CONNECTOR_POLL_HPD set are
9238d754544SDaniel Vetter  * completely ignored by the polling logic.
9248d754544SDaniel Vetter  *
9258d754544SDaniel Vetter  * Note that a connector can be both polled and probed from the hotplug handler,
9268d754544SDaniel Vetter  * in case the hotplug interrupt is known to be unreliable.
9278d754544SDaniel Vetter  */
drm_kms_helper_poll_init(struct drm_device * dev)9288d754544SDaniel Vetter void drm_kms_helper_poll_init(struct drm_device *dev)
9298d754544SDaniel Vetter {
9308d754544SDaniel Vetter 	INIT_DELAYED_WORK(&dev->mode_config.output_poll_work, output_poll_execute);
9318d754544SDaniel Vetter 	dev->mode_config.poll_enabled = true;
9328d754544SDaniel Vetter 
9338d754544SDaniel Vetter 	drm_kms_helper_poll_enable(dev);
9348d754544SDaniel Vetter }
9358d754544SDaniel Vetter EXPORT_SYMBOL(drm_kms_helper_poll_init);
9368d754544SDaniel Vetter 
9378d754544SDaniel Vetter /**
9388d754544SDaniel Vetter  * drm_kms_helper_poll_fini - disable output polling and clean it up
9398d754544SDaniel Vetter  * @dev: drm_device
9408d754544SDaniel Vetter  */
drm_kms_helper_poll_fini(struct drm_device * dev)9418d754544SDaniel Vetter void drm_kms_helper_poll_fini(struct drm_device *dev)
9428d754544SDaniel Vetter {
943d33a54e3SDmitry Baryshkov 	if (!dev->mode_config.poll_enabled)
944d33a54e3SDmitry Baryshkov 		return;
945d33a54e3SDmitry Baryshkov 
946d33a54e3SDmitry Baryshkov 	drm_kms_helper_poll_disable(dev);
947d33a54e3SDmitry Baryshkov 
948d33a54e3SDmitry Baryshkov 	dev->mode_config.poll_enabled = false;
9498d754544SDaniel Vetter }
9508d754544SDaniel Vetter EXPORT_SYMBOL(drm_kms_helper_poll_fini);
9518d754544SDaniel Vetter 
check_connector_changed(struct drm_connector * connector)9520464ed1aSMaxime Ripard static bool check_connector_changed(struct drm_connector *connector)
9530464ed1aSMaxime Ripard {
9540464ed1aSMaxime Ripard 	struct drm_device *dev = connector->dev;
9550464ed1aSMaxime Ripard 	enum drm_connector_status old_status;
9560464ed1aSMaxime Ripard 	u64 old_epoch_counter;
9570464ed1aSMaxime Ripard 
9580464ed1aSMaxime Ripard 	/* Only handle HPD capable connectors. */
9590464ed1aSMaxime Ripard 	drm_WARN_ON(dev, !(connector->polled & DRM_CONNECTOR_POLL_HPD));
9600464ed1aSMaxime Ripard 
9610464ed1aSMaxime Ripard 	drm_WARN_ON(dev, !mutex_is_locked(&dev->mode_config.mutex));
9620464ed1aSMaxime Ripard 
9630464ed1aSMaxime Ripard 	old_status = connector->status;
9640464ed1aSMaxime Ripard 	old_epoch_counter = connector->epoch_counter;
9650464ed1aSMaxime Ripard 	connector->status = drm_helper_probe_detect(connector, NULL, false);
9660464ed1aSMaxime Ripard 
9670464ed1aSMaxime Ripard 	if (old_epoch_counter == connector->epoch_counter) {
9680464ed1aSMaxime Ripard 		drm_dbg_kms(dev, "[CONNECTOR:%d:%s] Same epoch counter %llu\n",
9690464ed1aSMaxime Ripard 			    connector->base.id,
9700464ed1aSMaxime Ripard 			    connector->name,
9710464ed1aSMaxime Ripard 			    connector->epoch_counter);
9720464ed1aSMaxime Ripard 
9730464ed1aSMaxime Ripard 		return false;
9740464ed1aSMaxime Ripard 	}
9750464ed1aSMaxime Ripard 
9760464ed1aSMaxime Ripard 	drm_dbg_kms(dev, "[CONNECTOR:%d:%s] status updated from %s to %s\n",
9770464ed1aSMaxime Ripard 		    connector->base.id,
9780464ed1aSMaxime Ripard 		    connector->name,
9790464ed1aSMaxime Ripard 		    drm_get_connector_status_name(old_status),
9800464ed1aSMaxime Ripard 		    drm_get_connector_status_name(connector->status));
9810464ed1aSMaxime Ripard 
9820464ed1aSMaxime Ripard 	drm_dbg_kms(dev, "[CONNECTOR:%d:%s] Changed epoch counter %llu => %llu\n",
9830464ed1aSMaxime Ripard 		    connector->base.id,
9840464ed1aSMaxime Ripard 		    connector->name,
9850464ed1aSMaxime Ripard 		    old_epoch_counter,
9860464ed1aSMaxime Ripard 		    connector->epoch_counter);
9870464ed1aSMaxime Ripard 
9880464ed1aSMaxime Ripard 	return true;
9890464ed1aSMaxime Ripard }
9900464ed1aSMaxime Ripard 
9910464ed1aSMaxime Ripard /**
9920464ed1aSMaxime Ripard  * drm_connector_helper_hpd_irq_event - hotplug processing
9930464ed1aSMaxime Ripard  * @connector: drm_connector
9940464ed1aSMaxime Ripard  *
9950464ed1aSMaxime Ripard  * Drivers can use this helper function to run a detect cycle on a connector
9960464ed1aSMaxime Ripard  * which has the DRM_CONNECTOR_POLL_HPD flag set in its &polled member.
9970464ed1aSMaxime Ripard  *
9980464ed1aSMaxime Ripard  * This helper function is useful for drivers which can track hotplug
9990464ed1aSMaxime Ripard  * interrupts for a single connector. Drivers that want to send a
10000464ed1aSMaxime Ripard  * hotplug event for all connectors or can't track hotplug interrupts
10010464ed1aSMaxime Ripard  * per connector need to use drm_helper_hpd_irq_event().
10020464ed1aSMaxime Ripard  *
10030464ed1aSMaxime Ripard  * This function must be called from process context with no mode
10040464ed1aSMaxime Ripard  * setting locks held.
10050464ed1aSMaxime Ripard  *
10060464ed1aSMaxime Ripard  * Note that a connector can be both polled and probed from the hotplug
10070464ed1aSMaxime Ripard  * handler, in case the hotplug interrupt is known to be unreliable.
10080464ed1aSMaxime Ripard  *
10090464ed1aSMaxime Ripard  * Returns:
10100464ed1aSMaxime Ripard  * A boolean indicating whether the connector status changed or not
10110464ed1aSMaxime Ripard  */
drm_connector_helper_hpd_irq_event(struct drm_connector * connector)10120464ed1aSMaxime Ripard bool drm_connector_helper_hpd_irq_event(struct drm_connector *connector)
10130464ed1aSMaxime Ripard {
10140464ed1aSMaxime Ripard 	struct drm_device *dev = connector->dev;
10150464ed1aSMaxime Ripard 	bool changed;
10160464ed1aSMaxime Ripard 
10170464ed1aSMaxime Ripard 	mutex_lock(&dev->mode_config.mutex);
10180464ed1aSMaxime Ripard 	changed = check_connector_changed(connector);
10190464ed1aSMaxime Ripard 	mutex_unlock(&dev->mode_config.mutex);
10200464ed1aSMaxime Ripard 
10210464ed1aSMaxime Ripard 	if (changed) {
1022d35d4dbcSSimon Ser 		drm_kms_helper_connector_hotplug_event(connector);
10230464ed1aSMaxime Ripard 		drm_dbg_kms(dev, "[CONNECTOR:%d:%s] Sent hotplug event\n",
10240464ed1aSMaxime Ripard 			    connector->base.id,
10250464ed1aSMaxime Ripard 			    connector->name);
10260464ed1aSMaxime Ripard 	}
10270464ed1aSMaxime Ripard 
10280464ed1aSMaxime Ripard 	return changed;
10290464ed1aSMaxime Ripard }
10300464ed1aSMaxime Ripard EXPORT_SYMBOL(drm_connector_helper_hpd_irq_event);
10310464ed1aSMaxime Ripard 
10328d754544SDaniel Vetter /**
10338d754544SDaniel Vetter  * drm_helper_hpd_irq_event - hotplug processing
10348d754544SDaniel Vetter  * @dev: drm_device
10358d754544SDaniel Vetter  *
10368d754544SDaniel Vetter  * Drivers can use this helper function to run a detect cycle on all connectors
10378d754544SDaniel Vetter  * which have the DRM_CONNECTOR_POLL_HPD flag set in their &polled member. All
10388d754544SDaniel Vetter  * other connectors are ignored, which is useful to avoid reprobing fixed
10398d754544SDaniel Vetter  * panels.
10408d754544SDaniel Vetter  *
10418d754544SDaniel Vetter  * This helper function is useful for drivers which can't or don't track hotplug
10428d754544SDaniel Vetter  * interrupts for each connector.
10438d754544SDaniel Vetter  *
10448d754544SDaniel Vetter  * Drivers which support hotplug interrupts for each connector individually and
10450464ed1aSMaxime Ripard  * which have a more fine-grained detect logic can use
10460464ed1aSMaxime Ripard  * drm_connector_helper_hpd_irq_event(). Alternatively, they should bypass this
10470464ed1aSMaxime Ripard  * code and directly call drm_kms_helper_hotplug_event() in case the connector
10480464ed1aSMaxime Ripard  * state changed.
10498d754544SDaniel Vetter  *
10508d754544SDaniel Vetter  * This function must be called from process context with no mode
10518d754544SDaniel Vetter  * setting locks held.
10528d754544SDaniel Vetter  *
10538d754544SDaniel Vetter  * Note that a connector can be both polled and probed from the hotplug handler,
10548d754544SDaniel Vetter  * in case the hotplug interrupt is known to be unreliable.
10557dad41aaSMaxime Ripard  *
10567dad41aaSMaxime Ripard  * Returns:
10577dad41aaSMaxime Ripard  * A boolean indicating whether the connector status changed or not
10588d754544SDaniel Vetter  */
drm_helper_hpd_irq_event(struct drm_device * dev)10598d754544SDaniel Vetter bool drm_helper_hpd_irq_event(struct drm_device *dev)
10608d754544SDaniel Vetter {
1061d35d4dbcSSimon Ser 	struct drm_connector *connector, *first_changed_connector = NULL;
1062c36a3254SDaniel Vetter 	struct drm_connector_list_iter conn_iter;
1063d35d4dbcSSimon Ser 	int changed = 0;
10648d754544SDaniel Vetter 
10658d754544SDaniel Vetter 	if (!dev->mode_config.poll_enabled)
10668d754544SDaniel Vetter 		return false;
10678d754544SDaniel Vetter 
10688d754544SDaniel Vetter 	mutex_lock(&dev->mode_config.mutex);
1069b982dab1SThierry Reding 	drm_connector_list_iter_begin(dev, &conn_iter);
1070c36a3254SDaniel Vetter 	drm_for_each_connector_iter(connector, &conn_iter) {
10718d754544SDaniel Vetter 		/* Only handle HPD capable connectors. */
10728d754544SDaniel Vetter 		if (!(connector->polled & DRM_CONNECTOR_POLL_HPD))
10738d754544SDaniel Vetter 			continue;
10748d754544SDaniel Vetter 
1075d35d4dbcSSimon Ser 		if (check_connector_changed(connector)) {
1076d35d4dbcSSimon Ser 			if (!first_changed_connector) {
1077d35d4dbcSSimon Ser 				drm_connector_get(connector);
1078d35d4dbcSSimon Ser 				first_changed_connector = connector;
1079d35d4dbcSSimon Ser 			}
1080d35d4dbcSSimon Ser 
1081d35d4dbcSSimon Ser 			changed++;
1082d35d4dbcSSimon Ser 		}
10838d754544SDaniel Vetter 	}
1084b982dab1SThierry Reding 	drm_connector_list_iter_end(&conn_iter);
10858d754544SDaniel Vetter 	mutex_unlock(&dev->mode_config.mutex);
10868d754544SDaniel Vetter 
1087d35d4dbcSSimon Ser 	if (changed == 1)
1088d35d4dbcSSimon Ser 		drm_kms_helper_connector_hotplug_event(first_changed_connector);
1089d35d4dbcSSimon Ser 	else if (changed > 0)
10908d754544SDaniel Vetter 		drm_kms_helper_hotplug_event(dev);
1091d35d4dbcSSimon Ser 
1092d35d4dbcSSimon Ser 	if (first_changed_connector)
1093d35d4dbcSSimon Ser 		drm_connector_put(first_changed_connector);
10948d754544SDaniel Vetter 
10958d754544SDaniel Vetter 	return changed;
10968d754544SDaniel Vetter }
10978d754544SDaniel Vetter EXPORT_SYMBOL(drm_helper_hpd_irq_event);
109816f14564SThomas Zimmermann 
109916f14564SThomas Zimmermann /**
1100216b9bbaSThomas Zimmermann  * drm_crtc_helper_mode_valid_fixed - Validates a display mode
1101216b9bbaSThomas Zimmermann  * @crtc: the crtc
1102216b9bbaSThomas Zimmermann  * @mode: the mode to validate
1103216b9bbaSThomas Zimmermann  * @fixed_mode: the display hardware's mode
1104216b9bbaSThomas Zimmermann  *
1105216b9bbaSThomas Zimmermann  * Returns:
1106216b9bbaSThomas Zimmermann  * MODE_OK on success, or another mode-status code otherwise.
1107216b9bbaSThomas Zimmermann  */
drm_crtc_helper_mode_valid_fixed(struct drm_crtc * crtc,const struct drm_display_mode * mode,const struct drm_display_mode * fixed_mode)1108216b9bbaSThomas Zimmermann enum drm_mode_status drm_crtc_helper_mode_valid_fixed(struct drm_crtc *crtc,
1109216b9bbaSThomas Zimmermann 						      const struct drm_display_mode *mode,
1110216b9bbaSThomas Zimmermann 						      const struct drm_display_mode *fixed_mode)
1111216b9bbaSThomas Zimmermann {
1112216b9bbaSThomas Zimmermann 	if (mode->hdisplay != fixed_mode->hdisplay && mode->vdisplay != fixed_mode->vdisplay)
1113216b9bbaSThomas Zimmermann 		return MODE_ONE_SIZE;
1114216b9bbaSThomas Zimmermann 	else if (mode->hdisplay != fixed_mode->hdisplay)
1115216b9bbaSThomas Zimmermann 		return MODE_ONE_WIDTH;
1116216b9bbaSThomas Zimmermann 	else if (mode->vdisplay != fixed_mode->vdisplay)
1117216b9bbaSThomas Zimmermann 		return MODE_ONE_HEIGHT;
1118216b9bbaSThomas Zimmermann 
1119216b9bbaSThomas Zimmermann 	return MODE_OK;
1120216b9bbaSThomas Zimmermann }
1121216b9bbaSThomas Zimmermann EXPORT_SYMBOL(drm_crtc_helper_mode_valid_fixed);
1122216b9bbaSThomas Zimmermann 
1123216b9bbaSThomas Zimmermann /**
112416f14564SThomas Zimmermann  * drm_connector_helper_get_modes_from_ddc - Updates the connector's EDID
112516f14564SThomas Zimmermann  *                                           property from the connector's
112616f14564SThomas Zimmermann  *                                           DDC channel
112716f14564SThomas Zimmermann  * @connector: The connector
112816f14564SThomas Zimmermann  *
112916f14564SThomas Zimmermann  * Returns:
113016f14564SThomas Zimmermann  * The number of detected display modes.
113116f14564SThomas Zimmermann  *
113216f14564SThomas Zimmermann  * Uses a connector's DDC channel to retrieve EDID data and update the
113316f14564SThomas Zimmermann  * connector's EDID property and display modes. Drivers can use this
113416f14564SThomas Zimmermann  * function to implement struct &drm_connector_helper_funcs.get_modes
113516f14564SThomas Zimmermann  * for connectors with a DDC channel.
113616f14564SThomas Zimmermann  */
drm_connector_helper_get_modes_from_ddc(struct drm_connector * connector)113716f14564SThomas Zimmermann int drm_connector_helper_get_modes_from_ddc(struct drm_connector *connector)
113816f14564SThomas Zimmermann {
113916f14564SThomas Zimmermann 	struct edid *edid;
114016f14564SThomas Zimmermann 	int count = 0;
114116f14564SThomas Zimmermann 
114216f14564SThomas Zimmermann 	if (!connector->ddc)
114316f14564SThomas Zimmermann 		return 0;
114416f14564SThomas Zimmermann 
114516f14564SThomas Zimmermann 	edid = drm_get_edid(connector, connector->ddc);
114616f14564SThomas Zimmermann 
114716f14564SThomas Zimmermann 	// clears property if EDID is NULL
114816f14564SThomas Zimmermann 	drm_connector_update_edid_property(connector, edid);
114916f14564SThomas Zimmermann 
115016f14564SThomas Zimmermann 	if (edid) {
115116f14564SThomas Zimmermann 		count = drm_add_edid_modes(connector, edid);
115216f14564SThomas Zimmermann 		kfree(edid);
115316f14564SThomas Zimmermann 	}
115416f14564SThomas Zimmermann 
115516f14564SThomas Zimmermann 	return count;
115616f14564SThomas Zimmermann }
115716f14564SThomas Zimmermann EXPORT_SYMBOL(drm_connector_helper_get_modes_from_ddc);
1158964893d3SJani Nikula 
1159964893d3SJani Nikula /**
1160d25654b3SThomas Zimmermann  * drm_connector_helper_get_modes_fixed - Duplicates a display mode for a connector
1161d25654b3SThomas Zimmermann  * @connector: the connector
1162d25654b3SThomas Zimmermann  * @fixed_mode: the display hardware's mode
1163d25654b3SThomas Zimmermann  *
1164d25654b3SThomas Zimmermann  * This function duplicates a display modes for a connector. Drivers for hardware
1165d25654b3SThomas Zimmermann  * that only supports a single fixed mode can use this function in their connector's
1166d25654b3SThomas Zimmermann  * get_modes helper.
1167d25654b3SThomas Zimmermann  *
1168d25654b3SThomas Zimmermann  * Returns:
1169d25654b3SThomas Zimmermann  * The number of created modes.
1170d25654b3SThomas Zimmermann  */
drm_connector_helper_get_modes_fixed(struct drm_connector * connector,const struct drm_display_mode * fixed_mode)1171d25654b3SThomas Zimmermann int drm_connector_helper_get_modes_fixed(struct drm_connector *connector,
1172d25654b3SThomas Zimmermann 					 const struct drm_display_mode *fixed_mode)
1173d25654b3SThomas Zimmermann {
1174d25654b3SThomas Zimmermann 	struct drm_device *dev = connector->dev;
1175d25654b3SThomas Zimmermann 	struct drm_display_mode *mode;
1176d25654b3SThomas Zimmermann 
1177d25654b3SThomas Zimmermann 	mode = drm_mode_duplicate(dev, fixed_mode);
1178d25654b3SThomas Zimmermann 	if (!mode) {
1179d25654b3SThomas Zimmermann 		drm_err(dev, "Failed to duplicate mode " DRM_MODE_FMT "\n",
1180d25654b3SThomas Zimmermann 			DRM_MODE_ARG(fixed_mode));
1181d25654b3SThomas Zimmermann 		return 0;
1182d25654b3SThomas Zimmermann 	}
1183d25654b3SThomas Zimmermann 
1184d25654b3SThomas Zimmermann 	if (mode->name[0] == '\0')
1185d25654b3SThomas Zimmermann 		drm_mode_set_name(mode);
1186d25654b3SThomas Zimmermann 
1187d25654b3SThomas Zimmermann 	mode->type |= DRM_MODE_TYPE_PREFERRED;
1188d25654b3SThomas Zimmermann 	drm_mode_probed_add(connector, mode);
1189d25654b3SThomas Zimmermann 
1190d25654b3SThomas Zimmermann 	if (mode->width_mm)
1191d25654b3SThomas Zimmermann 		connector->display_info.width_mm = mode->width_mm;
1192d25654b3SThomas Zimmermann 	if (mode->height_mm)
1193d25654b3SThomas Zimmermann 		connector->display_info.height_mm = mode->height_mm;
1194d25654b3SThomas Zimmermann 
1195d25654b3SThomas Zimmermann 	return 1;
1196d25654b3SThomas Zimmermann }
1197d25654b3SThomas Zimmermann EXPORT_SYMBOL(drm_connector_helper_get_modes_fixed);
1198d25654b3SThomas Zimmermann 
1199d25654b3SThomas Zimmermann /**
1200964893d3SJani Nikula  * drm_connector_helper_get_modes - Read EDID and update connector.
1201964893d3SJani Nikula  * @connector: The connector
1202964893d3SJani Nikula  *
1203964893d3SJani Nikula  * Read the EDID using drm_edid_read() (which requires that connector->ddc is
1204964893d3SJani Nikula  * set), and update the connector using the EDID.
1205964893d3SJani Nikula  *
1206964893d3SJani Nikula  * This can be used as the "default" connector helper .get_modes() hook if the
1207964893d3SJani Nikula  * driver does not need any special processing. This is sets the example what
1208964893d3SJani Nikula  * custom .get_modes() hooks should do regarding EDID read and connector update.
1209964893d3SJani Nikula  *
1210964893d3SJani Nikula  * Returns: Number of modes.
1211964893d3SJani Nikula  */
drm_connector_helper_get_modes(struct drm_connector * connector)1212964893d3SJani Nikula int drm_connector_helper_get_modes(struct drm_connector *connector)
1213964893d3SJani Nikula {
1214964893d3SJani Nikula 	const struct drm_edid *drm_edid;
1215964893d3SJani Nikula 	int count;
1216964893d3SJani Nikula 
1217964893d3SJani Nikula 	drm_edid = drm_edid_read(connector);
1218964893d3SJani Nikula 
1219964893d3SJani Nikula 	/*
1220964893d3SJani Nikula 	 * Unconditionally update the connector. If the EDID was read
1221964893d3SJani Nikula 	 * successfully, fill in the connector information derived from the
1222964893d3SJani Nikula 	 * EDID. Otherwise, if the EDID is NULL, clear the connector
1223964893d3SJani Nikula 	 * information.
1224964893d3SJani Nikula 	 */
1225c533b516SJani Nikula 	drm_edid_connector_update(connector, drm_edid);
1226c533b516SJani Nikula 
1227c533b516SJani Nikula 	count = drm_edid_connector_add_modes(connector);
1228964893d3SJani Nikula 
1229964893d3SJani Nikula 	drm_edid_free(drm_edid);
1230964893d3SJani Nikula 
1231964893d3SJani Nikula 	return count;
1232964893d3SJani Nikula }
1233964893d3SJani Nikula EXPORT_SYMBOL(drm_connector_helper_get_modes);
12341e4a91dbSNoralf Trønnes 
12351e4a91dbSNoralf Trønnes /**
12361e4a91dbSNoralf Trønnes  * drm_connector_helper_tv_get_modes - Fills the modes availables to a TV connector
12371e4a91dbSNoralf Trønnes  * @connector: The connector
12381e4a91dbSNoralf Trønnes  *
12391e4a91dbSNoralf Trønnes  * Fills the available modes for a TV connector based on the supported
12401e4a91dbSNoralf Trønnes  * TV modes, and the default mode expressed by the kernel command line.
12411e4a91dbSNoralf Trønnes  *
12421e4a91dbSNoralf Trønnes  * This can be used as the default TV connector helper .get_modes() hook
12431e4a91dbSNoralf Trønnes  * if the driver does not need any special processing.
12441e4a91dbSNoralf Trønnes  *
12451e4a91dbSNoralf Trønnes  * Returns:
12461e4a91dbSNoralf Trønnes  * The number of modes added to the connector.
12471e4a91dbSNoralf Trønnes  */
drm_connector_helper_tv_get_modes(struct drm_connector * connector)12481e4a91dbSNoralf Trønnes int drm_connector_helper_tv_get_modes(struct drm_connector *connector)
12491e4a91dbSNoralf Trønnes {
12501e4a91dbSNoralf Trønnes 	struct drm_device *dev = connector->dev;
12511e4a91dbSNoralf Trønnes 	struct drm_property *tv_mode_property =
12521e4a91dbSNoralf Trønnes 		dev->mode_config.tv_mode_property;
12531e4a91dbSNoralf Trønnes 	struct drm_cmdline_mode *cmdline = &connector->cmdline_mode;
12541e4a91dbSNoralf Trønnes 	unsigned int ntsc_modes = BIT(DRM_MODE_TV_MODE_NTSC) |
12551e4a91dbSNoralf Trønnes 		BIT(DRM_MODE_TV_MODE_NTSC_443) |
12561e4a91dbSNoralf Trønnes 		BIT(DRM_MODE_TV_MODE_NTSC_J) |
12571e4a91dbSNoralf Trønnes 		BIT(DRM_MODE_TV_MODE_PAL_M);
12581e4a91dbSNoralf Trønnes 	unsigned int pal_modes = BIT(DRM_MODE_TV_MODE_PAL) |
12591e4a91dbSNoralf Trønnes 		BIT(DRM_MODE_TV_MODE_PAL_N) |
12601e4a91dbSNoralf Trønnes 		BIT(DRM_MODE_TV_MODE_SECAM);
12611e4a91dbSNoralf Trønnes 	unsigned int tv_modes[2] = { UINT_MAX, UINT_MAX };
12621e4a91dbSNoralf Trønnes 	unsigned int i, supported_tv_modes = 0;
12631e4a91dbSNoralf Trønnes 
12641e4a91dbSNoralf Trønnes 	if (!tv_mode_property)
12651e4a91dbSNoralf Trønnes 		return 0;
12661e4a91dbSNoralf Trønnes 
12671e4a91dbSNoralf Trønnes 	for (i = 0; i < tv_mode_property->num_values; i++)
12681e4a91dbSNoralf Trønnes 		supported_tv_modes |= BIT(tv_mode_property->values[i]);
12691e4a91dbSNoralf Trønnes 
12701e4a91dbSNoralf Trønnes 	if ((supported_tv_modes & ntsc_modes) &&
12711e4a91dbSNoralf Trønnes 	    (supported_tv_modes & pal_modes)) {
12721e4a91dbSNoralf Trønnes 		uint64_t default_mode;
12731e4a91dbSNoralf Trønnes 
12741e4a91dbSNoralf Trønnes 		if (drm_object_property_get_default_value(&connector->base,
12751e4a91dbSNoralf Trønnes 							  tv_mode_property,
12761e4a91dbSNoralf Trønnes 							  &default_mode))
12771e4a91dbSNoralf Trønnes 			return 0;
12781e4a91dbSNoralf Trønnes 
12791e4a91dbSNoralf Trønnes 		if (cmdline->tv_mode_specified)
12801e4a91dbSNoralf Trønnes 			default_mode = cmdline->tv_mode;
12811e4a91dbSNoralf Trønnes 
12821e4a91dbSNoralf Trønnes 		if (BIT(default_mode) & ntsc_modes) {
12831e4a91dbSNoralf Trønnes 			tv_modes[0] = DRM_MODE_TV_MODE_NTSC;
12841e4a91dbSNoralf Trønnes 			tv_modes[1] = DRM_MODE_TV_MODE_PAL;
12851e4a91dbSNoralf Trønnes 		} else {
12861e4a91dbSNoralf Trønnes 			tv_modes[0] = DRM_MODE_TV_MODE_PAL;
12871e4a91dbSNoralf Trønnes 			tv_modes[1] = DRM_MODE_TV_MODE_NTSC;
12881e4a91dbSNoralf Trønnes 		}
12891e4a91dbSNoralf Trønnes 	} else if (supported_tv_modes & ntsc_modes) {
12901e4a91dbSNoralf Trønnes 		tv_modes[0] = DRM_MODE_TV_MODE_NTSC;
12911e4a91dbSNoralf Trønnes 	} else if (supported_tv_modes & pal_modes) {
12921e4a91dbSNoralf Trønnes 		tv_modes[0] = DRM_MODE_TV_MODE_PAL;
12931e4a91dbSNoralf Trønnes 	} else {
12941e4a91dbSNoralf Trønnes 		return 0;
12951e4a91dbSNoralf Trønnes 	}
12961e4a91dbSNoralf Trønnes 
12971e4a91dbSNoralf Trønnes 	for (i = 0; i < ARRAY_SIZE(tv_modes); i++) {
12981e4a91dbSNoralf Trønnes 		struct drm_display_mode *mode;
12991e4a91dbSNoralf Trønnes 
13001e4a91dbSNoralf Trønnes 		if (tv_modes[i] == DRM_MODE_TV_MODE_NTSC)
13011e4a91dbSNoralf Trønnes 			mode = drm_mode_analog_ntsc_480i(dev);
13021e4a91dbSNoralf Trønnes 		else if (tv_modes[i] == DRM_MODE_TV_MODE_PAL)
13031e4a91dbSNoralf Trønnes 			mode = drm_mode_analog_pal_576i(dev);
13041e4a91dbSNoralf Trønnes 		else
13051e4a91dbSNoralf Trønnes 			break;
13061e4a91dbSNoralf Trønnes 		if (!mode)
13071e4a91dbSNoralf Trønnes 			return i;
13081e4a91dbSNoralf Trønnes 		if (!i)
13091e4a91dbSNoralf Trønnes 			mode->type |= DRM_MODE_TYPE_PREFERRED;
13101e4a91dbSNoralf Trønnes 		drm_mode_probed_add(connector, mode);
13111e4a91dbSNoralf Trønnes 	}
13121e4a91dbSNoralf Trønnes 
13131e4a91dbSNoralf Trønnes 	return i;
13141e4a91dbSNoralf Trønnes }
13151e4a91dbSNoralf Trønnes EXPORT_SYMBOL(drm_connector_helper_tv_get_modes);
1316