xref: /openbmc/linux/drivers/gpu/drm/drm_probe_helper.c (revision 1bbfe9d1a315bb573d4f10bbf443b68b5ddac473)
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>
368d754544SDaniel Vetter #include <drm/drm_crtc.h>
378d754544SDaniel Vetter #include <drm/drm_fourcc.h>
388d754544SDaniel Vetter #include <drm/drm_crtc_helper.h>
398d754544SDaniel Vetter #include <drm/drm_fb_helper.h>
408d754544SDaniel Vetter #include <drm/drm_edid.h>
418d754544SDaniel Vetter 
428d754544SDaniel Vetter /**
438d754544SDaniel Vetter  * DOC: output probing helper overview
448d754544SDaniel Vetter  *
458d754544SDaniel Vetter  * This library provides some helper code for output probing. It provides an
466806cdf9SDaniel Vetter  * implementation of the core &drm_connector_funcs.fill_modes interface with
47*1bbfe9d1SDaniel Vetter  * drm_helper_probe_single_connector_modes().
488d754544SDaniel Vetter  *
498d754544SDaniel Vetter  * It also provides support for polling connectors with a work item and for
508d754544SDaniel Vetter  * generic hotplug interrupt handling where the driver doesn't or cannot keep
518d754544SDaniel Vetter  * track of a per-connector hpd interrupt.
528d754544SDaniel Vetter  *
538d754544SDaniel Vetter  * This helper library can be used independently of the modeset helper library.
548d754544SDaniel Vetter  * Drivers can also overwrite different parts e.g. use their own hotplug
558d754544SDaniel Vetter  * handling code to avoid probing unrelated outputs.
56092d01daSDaniel Vetter  *
57092d01daSDaniel Vetter  * The probe helpers share the function table structures with other display
58ea0dd85aSDaniel Vetter  * helper libraries. See &struct drm_connector_helper_funcs for the details.
598d754544SDaniel Vetter  */
608d754544SDaniel Vetter 
618d754544SDaniel Vetter static bool drm_kms_helper_poll = true;
628d754544SDaniel Vetter module_param_named(poll, drm_kms_helper_poll, bool, 0600);
638d754544SDaniel Vetter 
6405acaec3SVille Syrjälä static enum drm_mode_status
6505acaec3SVille Syrjälä drm_mode_validate_flag(const struct drm_display_mode *mode,
668d754544SDaniel Vetter 		       int flags)
678d754544SDaniel Vetter {
688d754544SDaniel Vetter 	if ((mode->flags & DRM_MODE_FLAG_INTERLACE) &&
698d754544SDaniel Vetter 	    !(flags & DRM_MODE_FLAG_INTERLACE))
7005acaec3SVille Syrjälä 		return MODE_NO_INTERLACE;
7105acaec3SVille Syrjälä 
728d754544SDaniel Vetter 	if ((mode->flags & DRM_MODE_FLAG_DBLSCAN) &&
738d754544SDaniel Vetter 	    !(flags & DRM_MODE_FLAG_DBLSCAN))
7405acaec3SVille Syrjälä 		return MODE_NO_DBLESCAN;
7505acaec3SVille Syrjälä 
768d754544SDaniel Vetter 	if ((mode->flags & DRM_MODE_FLAG_3D_MASK) &&
778d754544SDaniel Vetter 	    !(flags & DRM_MODE_FLAG_3D_MASK))
7805acaec3SVille Syrjälä 		return MODE_NO_STEREO;
798d754544SDaniel Vetter 
8005acaec3SVille Syrjälä 	return MODE_OK;
818d754544SDaniel Vetter }
828d754544SDaniel Vetter 
83eaf99c74SChris Wilson static int drm_helper_probe_add_cmdline_mode(struct drm_connector *connector)
84eaf99c74SChris Wilson {
855f0c3f99SChris Wilson 	struct drm_cmdline_mode *cmdline_mode;
86eaf99c74SChris Wilson 	struct drm_display_mode *mode;
87eaf99c74SChris Wilson 
885f0c3f99SChris Wilson 	cmdline_mode = &connector->cmdline_mode;
895f0c3f99SChris Wilson 	if (!cmdline_mode->specified)
90eaf99c74SChris Wilson 		return 0;
91eaf99c74SChris Wilson 
925f0c3f99SChris Wilson 	/* Only add a GTF mode if we find no matching probed modes */
935f0c3f99SChris Wilson 	list_for_each_entry(mode, &connector->probed_modes, head) {
945f0c3f99SChris Wilson 		if (mode->hdisplay != cmdline_mode->xres ||
955f0c3f99SChris Wilson 		    mode->vdisplay != cmdline_mode->yres)
965f0c3f99SChris Wilson 			continue;
975f0c3f99SChris Wilson 
985f0c3f99SChris Wilson 		if (cmdline_mode->refresh_specified) {
995f0c3f99SChris Wilson 			/* The probed mode's vrefresh is set until later */
1005f0c3f99SChris Wilson 			if (drm_mode_vrefresh(mode) != cmdline_mode->refresh)
1015f0c3f99SChris Wilson 				continue;
1025f0c3f99SChris Wilson 		}
1035f0c3f99SChris Wilson 
1045f0c3f99SChris Wilson 		return 0;
1055f0c3f99SChris Wilson 	}
1065f0c3f99SChris Wilson 
107eaf99c74SChris Wilson 	mode = drm_mode_create_from_cmdline_mode(connector->dev,
1085f0c3f99SChris Wilson 						 cmdline_mode);
109eaf99c74SChris Wilson 	if (mode == NULL)
110eaf99c74SChris Wilson 		return 0;
111eaf99c74SChris Wilson 
112eaf99c74SChris Wilson 	drm_mode_probed_add(connector, mode);
113eaf99c74SChris Wilson 	return 1;
114eaf99c74SChris Wilson }
115eaf99c74SChris Wilson 
1168c4ccc4aSDaniel Vetter #define DRM_OUTPUT_POLL_PERIOD (10*HZ)
1174ad640e9SEgbert Eich /**
118c4d79c22SDave Airlie  * drm_kms_helper_poll_enable - re-enable output polling.
1194ad640e9SEgbert Eich  * @dev: drm_device
1204ad640e9SEgbert Eich  *
121c4d79c22SDave Airlie  * This function re-enables the output polling work, after it has been
122c4d79c22SDave Airlie  * temporarily disabled using drm_kms_helper_poll_disable(), for example over
123c4d79c22SDave Airlie  * suspend/resume.
1244ad640e9SEgbert Eich  *
125c4d79c22SDave Airlie  * Drivers can call this helper from their device resume implementation. It is
126c4d79c22SDave Airlie  * an error to call this when the output polling support has not yet been set
127c4d79c22SDave Airlie  * up.
128c4d79c22SDave Airlie  *
129c4d79c22SDave Airlie  * Note that calls to enable and disable polling must be strictly ordered, which
130c4d79c22SDave Airlie  * is automatically the case when they're only call from suspend/resume
131c4d79c22SDave Airlie  * callbacks.
1324ad640e9SEgbert Eich  */
133c4d79c22SDave Airlie void drm_kms_helper_poll_enable(struct drm_device *dev)
1348c4ccc4aSDaniel Vetter {
1358c4ccc4aSDaniel Vetter 	bool poll = false;
1368c4ccc4aSDaniel Vetter 	struct drm_connector *connector;
137c36a3254SDaniel Vetter 	struct drm_connector_list_iter conn_iter;
138339fd362SPeter Ujfalusi 	unsigned long delay = DRM_OUTPUT_POLL_PERIOD;
1398c4ccc4aSDaniel Vetter 
1408c4ccc4aSDaniel Vetter 	if (!dev->mode_config.poll_enabled || !drm_kms_helper_poll)
1418c4ccc4aSDaniel Vetter 		return;
1428c4ccc4aSDaniel Vetter 
143b982dab1SThierry Reding 	drm_connector_list_iter_begin(dev, &conn_iter);
144c36a3254SDaniel Vetter 	drm_for_each_connector_iter(connector, &conn_iter) {
1458c4ccc4aSDaniel Vetter 		if (connector->polled & (DRM_CONNECTOR_POLL_CONNECT |
1468c4ccc4aSDaniel Vetter 					 DRM_CONNECTOR_POLL_DISCONNECT))
1478c4ccc4aSDaniel Vetter 			poll = true;
1488c4ccc4aSDaniel Vetter 	}
149b982dab1SThierry Reding 	drm_connector_list_iter_end(&conn_iter);
1508c4ccc4aSDaniel Vetter 
151339fd362SPeter Ujfalusi 	if (dev->mode_config.delayed_event) {
15268f458eeSPeter Ujfalusi 		/*
15368f458eeSPeter Ujfalusi 		 * FIXME:
15468f458eeSPeter Ujfalusi 		 *
15568f458eeSPeter Ujfalusi 		 * Use short (1s) delay to handle the initial delayed event.
15668f458eeSPeter Ujfalusi 		 * This delay should not be needed, but Optimus/nouveau will
15768f458eeSPeter Ujfalusi 		 * fail in a mysterious way if the delayed event is handled as
15868f458eeSPeter Ujfalusi 		 * soon as possible like it is done in
15968f458eeSPeter Ujfalusi 		 * drm_helper_probe_single_connector_modes() in case the poll
16068f458eeSPeter Ujfalusi 		 * was enabled before.
16168f458eeSPeter Ujfalusi 		 */
162339fd362SPeter Ujfalusi 		poll = true;
16368f458eeSPeter Ujfalusi 		delay = HZ;
164339fd362SPeter Ujfalusi 	}
165339fd362SPeter Ujfalusi 
1668c4ccc4aSDaniel Vetter 	if (poll)
167339fd362SPeter Ujfalusi 		schedule_delayed_work(&dev->mode_config.output_poll_work, delay);
1688c4ccc4aSDaniel Vetter }
169c4d79c22SDave Airlie EXPORT_SYMBOL(drm_kms_helper_poll_enable);
1704ad640e9SEgbert Eich 
171949f0886SLaurent Pinchart static enum drm_connector_status
172949f0886SLaurent Pinchart drm_connector_detect(struct drm_connector *connector, bool force)
173949f0886SLaurent Pinchart {
174949f0886SLaurent Pinchart 	return connector->funcs->detect ?
175949f0886SLaurent Pinchart 		connector->funcs->detect(connector, force) :
176949f0886SLaurent Pinchart 		connector_status_connected;
177949f0886SLaurent Pinchart }
178949f0886SLaurent Pinchart 
1796af3e656SVille Syrjälä /**
1806af3e656SVille Syrjälä  * drm_helper_probe_single_connector_modes - get complete set of display modes
1816af3e656SVille Syrjälä  * @connector: connector to probe
1826af3e656SVille Syrjälä  * @maxX: max width for modes
1836af3e656SVille Syrjälä  * @maxY: max height for modes
1846af3e656SVille Syrjälä  *
1856af3e656SVille Syrjälä  * Based on the helper callbacks implemented by @connector in struct
1866af3e656SVille Syrjälä  * &drm_connector_helper_funcs try to detect all valid modes.  Modes will first
1876af3e656SVille Syrjälä  * be added to the connector's probed_modes list, then culled (based on validity
1886af3e656SVille Syrjälä  * and the @maxX, @maxY parameters) and put into the normal modes list.
1896af3e656SVille Syrjälä  *
1906806cdf9SDaniel Vetter  * Intended to be used as a generic implementation of the
1916806cdf9SDaniel Vetter  * &drm_connector_funcs.fill_modes() vfunc for drivers that use the CRTC helpers
1926806cdf9SDaniel Vetter  * for output mode filtering and detection.
1936af3e656SVille Syrjälä  *
1945dec293bSVille Syrjälä  * The basic procedure is as follows
1956af3e656SVille Syrjälä  *
1965dec293bSVille Syrjälä  * 1. All modes currently on the connector's modes list are marked as stale
1976af3e656SVille Syrjälä  *
1985dec293bSVille Syrjälä  * 2. New modes are added to the connector's probed_modes list with
1995dec293bSVille Syrjälä  *    drm_mode_probed_add(). New modes start their life with status as OK.
2005dec293bSVille Syrjälä  *    Modes are added from a single source using the following priority order.
2015dec293bSVille Syrjälä  *
2025dec293bSVille Syrjälä  *    - debugfs 'override_edid' (used for testing only)
2035dec293bSVille Syrjälä  *    - firmware EDID (drm_load_edid_firmware())
2046806cdf9SDaniel Vetter  *    - &drm_connector_helper_funcs.get_modes vfunc
2055dec293bSVille Syrjälä  *    - if the connector status is connector_status_connected, standard
2065dec293bSVille Syrjälä  *      VESA DMT modes up to 1024x768 are automatically added
2075dec293bSVille Syrjälä  *      (drm_add_modes_noedid())
2085dec293bSVille Syrjälä  *
2095dec293bSVille Syrjälä  *    Finally modes specified via the kernel command line (video=...) are
2105dec293bSVille Syrjälä  *    added in addition to what the earlier probes produced
2115dec293bSVille Syrjälä  *    (drm_helper_probe_add_cmdline_mode()). These modes are generated
2125dec293bSVille Syrjälä  *    using the VESA GTF/CVT formulas.
2135dec293bSVille Syrjälä  *
2145dec293bSVille Syrjälä  * 3. Modes are moved from the probed_modes list to the modes list. Potential
2155dec293bSVille Syrjälä  *    duplicates are merged together (see drm_mode_connector_list_update()).
2165dec293bSVille Syrjälä  *    After this step the probed_modes list will be empty again.
2175dec293bSVille Syrjälä  *
2185dec293bSVille Syrjälä  * 4. Any non-stale mode on the modes list then undergoes validation
2195dec293bSVille Syrjälä  *
2205dec293bSVille Syrjälä  *    - drm_mode_validate_basic() performs basic sanity checks
2215dec293bSVille Syrjälä  *    - drm_mode_validate_size() filters out modes larger than @maxX and @maxY
2225dec293bSVille Syrjälä  *      (if specified)
22357366a8dSMasahiro Yamada  *    - drm_mode_validate_flag() checks the modes against basic connector
22457366a8dSMasahiro Yamada  *      capabilities (interlace_allowed,doublescan_allowed,stereo_allowed)
2256806cdf9SDaniel Vetter  *    - the optional &drm_connector_helper_funcs.mode_valid helper can perform
2266806cdf9SDaniel Vetter  *      driver and/or hardware specific checks
2275dec293bSVille Syrjälä  *
2285dec293bSVille Syrjälä  * 5. Any mode whose status is not OK is pruned from the connector's modes list,
2295dec293bSVille Syrjälä  *    accompanied by a debug message indicating the reason for the mode's
2305dec293bSVille Syrjälä  *    rejection (see drm_mode_prune_invalid()).
2316af3e656SVille Syrjälä  *
2326af3e656SVille Syrjälä  * Returns:
2336af3e656SVille Syrjälä  * The number of modes found on @connector.
2346af3e656SVille Syrjälä  */
2356af3e656SVille Syrjälä int drm_helper_probe_single_connector_modes(struct drm_connector *connector,
2366af3e656SVille Syrjälä 					    uint32_t maxX, uint32_t maxY)
2378d754544SDaniel Vetter {
2388d754544SDaniel Vetter 	struct drm_device *dev = connector->dev;
2398d754544SDaniel Vetter 	struct drm_display_mode *mode;
240be26a66dSJani Nikula 	const struct drm_connector_helper_funcs *connector_funcs =
2418d754544SDaniel Vetter 		connector->helper_private;
2428d754544SDaniel Vetter 	int count = 0;
2438d754544SDaniel Vetter 	int mode_flags = 0;
2448d754544SDaniel Vetter 	bool verbose_prune = true;
245162b6a57SDaniel Vetter 	enum drm_connector_status old_status;
2468d754544SDaniel Vetter 
2478d754544SDaniel Vetter 	WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
2488d754544SDaniel Vetter 
2498d754544SDaniel Vetter 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", connector->base.id,
25025933820SJani Nikula 			connector->name);
2515ba89406SVille Syrjälä 	/* set all old modes to the stale state */
2528d754544SDaniel Vetter 	list_for_each_entry(mode, &connector->modes, head)
2535ba89406SVille Syrjälä 		mode->status = MODE_STALE;
2548d754544SDaniel Vetter 
255ed293f77SDaniel Vetter 	old_status = connector->status;
256ed293f77SDaniel Vetter 
2578d754544SDaniel Vetter 	if (connector->force) {
2582c4cc91bSPeter Hurley 		if (connector->force == DRM_FORCE_ON ||
2592c4cc91bSPeter Hurley 		    connector->force == DRM_FORCE_ON_DIGITAL)
2608d754544SDaniel Vetter 			connector->status = connector_status_connected;
2618d754544SDaniel Vetter 		else
2628d754544SDaniel Vetter 			connector->status = connector_status_disconnected;
2638d754544SDaniel Vetter 		if (connector->funcs->force)
2648d754544SDaniel Vetter 			connector->funcs->force(connector);
2658d754544SDaniel Vetter 	} else {
266949f0886SLaurent Pinchart 		connector->status = drm_connector_detect(connector, true);
267ed293f77SDaniel Vetter 	}
268162b6a57SDaniel Vetter 
269162b6a57SDaniel Vetter 	/*
270162b6a57SDaniel Vetter 	 * Normally either the driver's hpd code or the poll loop should
271162b6a57SDaniel Vetter 	 * pick up any changes and fire the hotplug event. But if
272162b6a57SDaniel Vetter 	 * userspace sneaks in a probe, we might miss a change. Hence
273162b6a57SDaniel Vetter 	 * check here, and if anything changed start the hotplug code.
274162b6a57SDaniel Vetter 	 */
275162b6a57SDaniel Vetter 	if (old_status != connector->status) {
2764e15f2a1SJani Nikula 		DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
277162b6a57SDaniel Vetter 			      connector->base.id,
278162b6a57SDaniel Vetter 			      connector->name,
2794e15f2a1SJani Nikula 			      drm_get_connector_status_name(old_status),
2804e15f2a1SJani Nikula 			      drm_get_connector_status_name(connector->status));
281162b6a57SDaniel Vetter 
282162b6a57SDaniel Vetter 		/*
283162b6a57SDaniel Vetter 		 * The hotplug event code might call into the fb
284162b6a57SDaniel Vetter 		 * helpers, and so expects that we do not hold any
285162b6a57SDaniel Vetter 		 * locks. Fire up the poll struct instead, it will
286162b6a57SDaniel Vetter 		 * disable itself again.
287162b6a57SDaniel Vetter 		 */
288162b6a57SDaniel Vetter 		dev->mode_config.delayed_event = true;
289162b6a57SDaniel Vetter 		if (dev->mode_config.poll_enabled)
290162b6a57SDaniel Vetter 			schedule_delayed_work(&dev->mode_config.output_poll_work,
291162b6a57SDaniel Vetter 					      0);
292162b6a57SDaniel Vetter 	}
2938d754544SDaniel Vetter 
2948d754544SDaniel Vetter 	/* Re-enable polling in case the global poll config changed. */
2958d754544SDaniel Vetter 	if (drm_kms_helper_poll != dev->mode_config.poll_running)
296c4d79c22SDave Airlie 		drm_kms_helper_poll_enable(dev);
2978d754544SDaniel Vetter 
2988d754544SDaniel Vetter 	dev->mode_config.poll_running = drm_kms_helper_poll;
2998d754544SDaniel Vetter 
3008d754544SDaniel Vetter 	if (connector->status == connector_status_disconnected) {
3018d754544SDaniel Vetter 		DRM_DEBUG_KMS("[CONNECTOR:%d:%s] disconnected\n",
30225933820SJani Nikula 			connector->base.id, connector->name);
3038d754544SDaniel Vetter 		drm_mode_connector_update_edid_property(connector, NULL);
3048d754544SDaniel Vetter 		verbose_prune = false;
3058d754544SDaniel Vetter 		goto prune;
3068d754544SDaniel Vetter 	}
3078d754544SDaniel Vetter 
3084cf2b281SThomas Wood 	if (connector->override_edid) {
3094cf2b281SThomas Wood 		struct edid *edid = (struct edid *) connector->edid_blob_ptr->data;
3104cf2b281SThomas Wood 
3114cf2b281SThomas Wood 		count = drm_add_edid_modes(connector, edid);
312ad692b46SJani Nikula 		drm_edid_to_eld(connector, edid);
3130e8578c9SVille Syrjälä 	} else {
31407c2b84bSJani Nikula 		struct edid *edid = drm_load_edid_firmware(connector);
31507c2b84bSJani Nikula 		if (!IS_ERR_OR_NULL(edid)) {
31607c2b84bSJani Nikula 			drm_mode_connector_update_edid_property(connector, edid);
31707c2b84bSJani Nikula 			count = drm_add_edid_modes(connector, edid);
31807c2b84bSJani Nikula 			drm_edid_to_eld(connector, edid);
31907c2b84bSJani Nikula 			kfree(edid);
32007c2b84bSJani Nikula 		}
3210e8578c9SVille Syrjälä 		if (count == 0)
3228d754544SDaniel Vetter 			count = (*connector_funcs->get_modes)(connector);
3234cf2b281SThomas Wood 	}
3248d754544SDaniel Vetter 
3258d754544SDaniel Vetter 	if (count == 0 && connector->status == connector_status_connected)
3268d754544SDaniel Vetter 		count = drm_add_modes_noedid(connector, 1024, 768);
327eaf99c74SChris Wilson 	count += drm_helper_probe_add_cmdline_mode(connector);
3288d754544SDaniel Vetter 	if (count == 0)
3298d754544SDaniel Vetter 		goto prune;
3308d754544SDaniel Vetter 
3316af3e656SVille Syrjälä 	drm_mode_connector_list_update(connector);
3328d754544SDaniel Vetter 
3338d754544SDaniel Vetter 	if (connector->interlace_allowed)
3348d754544SDaniel Vetter 		mode_flags |= DRM_MODE_FLAG_INTERLACE;
3358d754544SDaniel Vetter 	if (connector->doublescan_allowed)
3368d754544SDaniel Vetter 		mode_flags |= DRM_MODE_FLAG_DBLSCAN;
3378d754544SDaniel Vetter 	if (connector->stereo_allowed)
3388d754544SDaniel Vetter 		mode_flags |= DRM_MODE_FLAG_3D_MASK;
3398d754544SDaniel Vetter 
3408d754544SDaniel Vetter 	list_for_each_entry(mode, &connector->modes, head) {
341be8719a6SVille Syrjälä 		if (mode->status == MODE_OK)
342abc0b144SVille Syrjälä 			mode->status = drm_mode_validate_basic(mode);
343abc0b144SVille Syrjälä 
344abc0b144SVille Syrjälä 		if (mode->status == MODE_OK)
34505acaec3SVille Syrjälä 			mode->status = drm_mode_validate_size(mode, maxX, maxY);
34605acaec3SVille Syrjälä 
34705acaec3SVille Syrjälä 		if (mode->status == MODE_OK)
34805acaec3SVille Syrjälä 			mode->status = drm_mode_validate_flag(mode, mode_flags);
34905acaec3SVille Syrjälä 
350f9b0e251SAndrzej Hajda 		if (mode->status == MODE_OK && connector_funcs->mode_valid)
3518d754544SDaniel Vetter 			mode->status = connector_funcs->mode_valid(connector,
3528d754544SDaniel Vetter 								   mode);
3538d754544SDaniel Vetter 	}
3548d754544SDaniel Vetter 
3558d754544SDaniel Vetter prune:
3568d754544SDaniel Vetter 	drm_mode_prune_invalid(dev, &connector->modes, verbose_prune);
3578d754544SDaniel Vetter 
3588d754544SDaniel Vetter 	if (list_empty(&connector->modes))
3598d754544SDaniel Vetter 		return 0;
3608d754544SDaniel Vetter 
3618d754544SDaniel Vetter 	list_for_each_entry(mode, &connector->modes, head)
3628d754544SDaniel Vetter 		mode->vrefresh = drm_mode_vrefresh(mode);
3638d754544SDaniel Vetter 
3648d754544SDaniel Vetter 	drm_mode_sort(&connector->modes);
3658d754544SDaniel Vetter 
3668d754544SDaniel Vetter 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s] probed modes :\n", connector->base.id,
36725933820SJani Nikula 			connector->name);
3688d754544SDaniel Vetter 	list_for_each_entry(mode, &connector->modes, head) {
3698d754544SDaniel Vetter 		drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
3708d754544SDaniel Vetter 		drm_mode_debug_printmodeline(mode);
3718d754544SDaniel Vetter 	}
3728d754544SDaniel Vetter 
3738d754544SDaniel Vetter 	return count;
3748d754544SDaniel Vetter }
3758d754544SDaniel Vetter EXPORT_SYMBOL(drm_helper_probe_single_connector_modes);
3768d754544SDaniel Vetter 
3778d754544SDaniel Vetter /**
3788d754544SDaniel Vetter  * drm_kms_helper_hotplug_event - fire off KMS hotplug events
3798d754544SDaniel Vetter  * @dev: drm_device whose connector state changed
3808d754544SDaniel Vetter  *
3818d754544SDaniel Vetter  * This function fires off the uevent for userspace and also calls the
3828d754544SDaniel Vetter  * output_poll_changed function, which is most commonly used to inform the fbdev
3838d754544SDaniel Vetter  * emulation code and allow it to update the fbcon output configuration.
3848d754544SDaniel Vetter  *
3858d754544SDaniel Vetter  * Drivers should call this from their hotplug handling code when a change is
3868d754544SDaniel Vetter  * detected. Note that this function does not do any output detection of its
3878d754544SDaniel Vetter  * own, like drm_helper_hpd_irq_event() does - this is assumed to be done by the
3888d754544SDaniel Vetter  * driver already.
3898d754544SDaniel Vetter  *
3908d754544SDaniel Vetter  * This function must be called from process context with no mode
3918d754544SDaniel Vetter  * setting locks held.
3928d754544SDaniel Vetter  */
3938d754544SDaniel Vetter void drm_kms_helper_hotplug_event(struct drm_device *dev)
3948d754544SDaniel Vetter {
3958d754544SDaniel Vetter 	/* send a uevent + call fbdev */
3968d754544SDaniel Vetter 	drm_sysfs_hotplug_event(dev);
3978d754544SDaniel Vetter 	if (dev->mode_config.funcs->output_poll_changed)
3988d754544SDaniel Vetter 		dev->mode_config.funcs->output_poll_changed(dev);
3998d754544SDaniel Vetter }
4008d754544SDaniel Vetter EXPORT_SYMBOL(drm_kms_helper_hotplug_event);
4018d754544SDaniel Vetter 
4028d754544SDaniel Vetter static void output_poll_execute(struct work_struct *work)
4038d754544SDaniel Vetter {
4048d754544SDaniel Vetter 	struct delayed_work *delayed_work = to_delayed_work(work);
4058d754544SDaniel Vetter 	struct drm_device *dev = container_of(delayed_work, struct drm_device, mode_config.output_poll_work);
4068d754544SDaniel Vetter 	struct drm_connector *connector;
407c36a3254SDaniel Vetter 	struct drm_connector_list_iter conn_iter;
4088d754544SDaniel Vetter 	enum drm_connector_status old_status;
409162b6a57SDaniel Vetter 	bool repoll = false, changed;
410162b6a57SDaniel Vetter 
411162b6a57SDaniel Vetter 	/* Pick up any changes detected by the probe functions. */
412162b6a57SDaniel Vetter 	changed = dev->mode_config.delayed_event;
413162b6a57SDaniel Vetter 	dev->mode_config.delayed_event = false;
4148d754544SDaniel Vetter 
4158d754544SDaniel Vetter 	if (!drm_kms_helper_poll)
416162b6a57SDaniel Vetter 		goto out;
4178d754544SDaniel Vetter 
4181fe7841dSChris Wilson 	if (!mutex_trylock(&dev->mode_config.mutex)) {
4191fe7841dSChris Wilson 		repoll = true;
4201fe7841dSChris Wilson 		goto out;
4211fe7841dSChris Wilson 	}
4221fe7841dSChris Wilson 
423b982dab1SThierry Reding 	drm_connector_list_iter_begin(dev, &conn_iter);
424c36a3254SDaniel Vetter 	drm_for_each_connector_iter(connector, &conn_iter) {
4258d754544SDaniel Vetter 		/* Ignore forced connectors. */
4268d754544SDaniel Vetter 		if (connector->force)
4278d754544SDaniel Vetter 			continue;
4288d754544SDaniel Vetter 
4298d754544SDaniel Vetter 		/* Ignore HPD capable connectors and connectors where we don't
4308d754544SDaniel Vetter 		 * want any hotplug detection at all for polling. */
4318d754544SDaniel Vetter 		if (!connector->polled || connector->polled == DRM_CONNECTOR_POLL_HPD)
4328d754544SDaniel Vetter 			continue;
4338d754544SDaniel Vetter 
4348d754544SDaniel Vetter 		old_status = connector->status;
4358d754544SDaniel Vetter 		/* if we are connected and don't want to poll for disconnect
4368d754544SDaniel Vetter 		   skip it */
4378d754544SDaniel Vetter 		if (old_status == connector_status_connected &&
4388d754544SDaniel Vetter 		    !(connector->polled & DRM_CONNECTOR_POLL_DISCONNECT))
4398d754544SDaniel Vetter 			continue;
4408d754544SDaniel Vetter 
441a3c6d686SJosef Holzmayr 		repoll = true;
442a3c6d686SJosef Holzmayr 
443949f0886SLaurent Pinchart 		connector->status = drm_connector_detect(connector, false);
4448d754544SDaniel Vetter 		if (old_status != connector->status) {
4458d754544SDaniel Vetter 			const char *old, *new;
4468d754544SDaniel Vetter 
447b7703726SDaniel Vetter 			/*
448b7703726SDaniel Vetter 			 * The poll work sets force=false when calling detect so
449b7703726SDaniel Vetter 			 * that drivers can avoid to do disruptive tests (e.g.
450b7703726SDaniel Vetter 			 * when load detect cycles could cause flickering on
451b7703726SDaniel Vetter 			 * other, running displays). This bears the risk that we
452b7703726SDaniel Vetter 			 * flip-flop between unknown here in the poll work and
453b7703726SDaniel Vetter 			 * the real state when userspace forces a full detect
454b7703726SDaniel Vetter 			 * call after receiving a hotplug event due to this
455b7703726SDaniel Vetter 			 * change.
456b7703726SDaniel Vetter 			 *
457b7703726SDaniel Vetter 			 * Hence clamp an unknown detect status to the old
458b7703726SDaniel Vetter 			 * value.
459b7703726SDaniel Vetter 			 */
460b7703726SDaniel Vetter 			if (connector->status == connector_status_unknown) {
461b7703726SDaniel Vetter 				connector->status = old_status;
462b7703726SDaniel Vetter 				continue;
463b7703726SDaniel Vetter 			}
464b7703726SDaniel Vetter 
4658d754544SDaniel Vetter 			old = drm_get_connector_status_name(old_status);
4668d754544SDaniel Vetter 			new = drm_get_connector_status_name(connector->status);
4678d754544SDaniel Vetter 
4688d754544SDaniel Vetter 			DRM_DEBUG_KMS("[CONNECTOR:%d:%s] "
4698d754544SDaniel Vetter 				      "status updated from %s to %s\n",
4708d754544SDaniel Vetter 				      connector->base.id,
47125933820SJani Nikula 				      connector->name,
4728d754544SDaniel Vetter 				      old, new);
4738d754544SDaniel Vetter 
4748d754544SDaniel Vetter 			changed = true;
4758d754544SDaniel Vetter 		}
4768d754544SDaniel Vetter 	}
477b982dab1SThierry Reding 	drm_connector_list_iter_end(&conn_iter);
4788d754544SDaniel Vetter 
4798d754544SDaniel Vetter 	mutex_unlock(&dev->mode_config.mutex);
4808d754544SDaniel Vetter 
481162b6a57SDaniel Vetter out:
4828d754544SDaniel Vetter 	if (changed)
4838d754544SDaniel Vetter 		drm_kms_helper_hotplug_event(dev);
4848d754544SDaniel Vetter 
4858d754544SDaniel Vetter 	if (repoll)
4868d754544SDaniel Vetter 		schedule_delayed_work(delayed_work, DRM_OUTPUT_POLL_PERIOD);
4878d754544SDaniel Vetter }
4888d754544SDaniel Vetter 
4898d754544SDaniel Vetter /**
4908d754544SDaniel Vetter  * drm_kms_helper_poll_disable - disable output polling
4918d754544SDaniel Vetter  * @dev: drm_device
4928d754544SDaniel Vetter  *
4938d754544SDaniel Vetter  * This function disables the output polling work.
4948d754544SDaniel Vetter  *
4958d754544SDaniel Vetter  * Drivers can call this helper from their device suspend implementation. It is
496c4d79c22SDave Airlie  * not an error to call this even when output polling isn't enabled or already
497c4d79c22SDave Airlie  * disabled. Polling is re-enabled by calling drm_kms_helper_poll_enable().
498c4d79c22SDave Airlie  *
499c4d79c22SDave Airlie  * Note that calls to enable and disable polling must be strictly ordered, which
500c4d79c22SDave Airlie  * is automatically the case when they're only call from suspend/resume
501c4d79c22SDave Airlie  * callbacks.
5028d754544SDaniel Vetter  */
5038d754544SDaniel Vetter void drm_kms_helper_poll_disable(struct drm_device *dev)
5048d754544SDaniel Vetter {
5058d754544SDaniel Vetter 	if (!dev->mode_config.poll_enabled)
5068d754544SDaniel Vetter 		return;
5078d754544SDaniel Vetter 	cancel_delayed_work_sync(&dev->mode_config.output_poll_work);
5088d754544SDaniel Vetter }
5098d754544SDaniel Vetter EXPORT_SYMBOL(drm_kms_helper_poll_disable);
5108d754544SDaniel Vetter 
5118d754544SDaniel Vetter /**
5128d754544SDaniel Vetter  * drm_kms_helper_poll_init - initialize and enable output polling
5138d754544SDaniel Vetter  * @dev: drm_device
5148d754544SDaniel Vetter  *
5158d754544SDaniel Vetter  * This function intializes and then also enables output polling support for
5168d754544SDaniel Vetter  * @dev. Drivers which do not have reliable hotplug support in hardware can use
5178d754544SDaniel Vetter  * this helper infrastructure to regularly poll such connectors for changes in
5188d754544SDaniel Vetter  * their connection state.
5198d754544SDaniel Vetter  *
5208d754544SDaniel Vetter  * Drivers can control which connectors are polled by setting the
5218d754544SDaniel Vetter  * DRM_CONNECTOR_POLL_CONNECT and DRM_CONNECTOR_POLL_DISCONNECT flags. On
5228d754544SDaniel Vetter  * connectors where probing live outputs can result in visual distortion drivers
5238d754544SDaniel Vetter  * should not set the DRM_CONNECTOR_POLL_DISCONNECT flag to avoid this.
5248d754544SDaniel Vetter  * Connectors which have no flag or only DRM_CONNECTOR_POLL_HPD set are
5258d754544SDaniel Vetter  * completely ignored by the polling logic.
5268d754544SDaniel Vetter  *
5278d754544SDaniel Vetter  * Note that a connector can be both polled and probed from the hotplug handler,
5288d754544SDaniel Vetter  * in case the hotplug interrupt is known to be unreliable.
5298d754544SDaniel Vetter  */
5308d754544SDaniel Vetter void drm_kms_helper_poll_init(struct drm_device *dev)
5318d754544SDaniel Vetter {
5328d754544SDaniel Vetter 	INIT_DELAYED_WORK(&dev->mode_config.output_poll_work, output_poll_execute);
5338d754544SDaniel Vetter 	dev->mode_config.poll_enabled = true;
5348d754544SDaniel Vetter 
5358d754544SDaniel Vetter 	drm_kms_helper_poll_enable(dev);
5368d754544SDaniel Vetter }
5378d754544SDaniel Vetter EXPORT_SYMBOL(drm_kms_helper_poll_init);
5388d754544SDaniel Vetter 
5398d754544SDaniel Vetter /**
5408d754544SDaniel Vetter  * drm_kms_helper_poll_fini - disable output polling and clean it up
5418d754544SDaniel Vetter  * @dev: drm_device
5428d754544SDaniel Vetter  */
5438d754544SDaniel Vetter void drm_kms_helper_poll_fini(struct drm_device *dev)
5448d754544SDaniel Vetter {
5458d754544SDaniel Vetter 	drm_kms_helper_poll_disable(dev);
5468d754544SDaniel Vetter }
5478d754544SDaniel Vetter EXPORT_SYMBOL(drm_kms_helper_poll_fini);
5488d754544SDaniel Vetter 
5498d754544SDaniel Vetter /**
5508d754544SDaniel Vetter  * drm_helper_hpd_irq_event - hotplug processing
5518d754544SDaniel Vetter  * @dev: drm_device
5528d754544SDaniel Vetter  *
5538d754544SDaniel Vetter  * Drivers can use this helper function to run a detect cycle on all connectors
5548d754544SDaniel Vetter  * which have the DRM_CONNECTOR_POLL_HPD flag set in their &polled member. All
5558d754544SDaniel Vetter  * other connectors are ignored, which is useful to avoid reprobing fixed
5568d754544SDaniel Vetter  * panels.
5578d754544SDaniel Vetter  *
5588d754544SDaniel Vetter  * This helper function is useful for drivers which can't or don't track hotplug
5598d754544SDaniel Vetter  * interrupts for each connector.
5608d754544SDaniel Vetter  *
5618d754544SDaniel Vetter  * Drivers which support hotplug interrupts for each connector individually and
5628d754544SDaniel Vetter  * which have a more fine-grained detect logic should bypass this code and
5638d754544SDaniel Vetter  * directly call drm_kms_helper_hotplug_event() in case the connector state
5648d754544SDaniel Vetter  * changed.
5658d754544SDaniel Vetter  *
5668d754544SDaniel Vetter  * This function must be called from process context with no mode
5678d754544SDaniel Vetter  * setting locks held.
5688d754544SDaniel Vetter  *
5698d754544SDaniel Vetter  * Note that a connector can be both polled and probed from the hotplug handler,
5708d754544SDaniel Vetter  * in case the hotplug interrupt is known to be unreliable.
5718d754544SDaniel Vetter  */
5728d754544SDaniel Vetter bool drm_helper_hpd_irq_event(struct drm_device *dev)
5738d754544SDaniel Vetter {
5748d754544SDaniel Vetter 	struct drm_connector *connector;
575c36a3254SDaniel Vetter 	struct drm_connector_list_iter conn_iter;
5768d754544SDaniel Vetter 	enum drm_connector_status old_status;
5778d754544SDaniel Vetter 	bool changed = false;
5788d754544SDaniel Vetter 
5798d754544SDaniel Vetter 	if (!dev->mode_config.poll_enabled)
5808d754544SDaniel Vetter 		return false;
5818d754544SDaniel Vetter 
5828d754544SDaniel Vetter 	mutex_lock(&dev->mode_config.mutex);
583b982dab1SThierry Reding 	drm_connector_list_iter_begin(dev, &conn_iter);
584c36a3254SDaniel Vetter 	drm_for_each_connector_iter(connector, &conn_iter) {
5858d754544SDaniel Vetter 		/* Only handle HPD capable connectors. */
5868d754544SDaniel Vetter 		if (!(connector->polled & DRM_CONNECTOR_POLL_HPD))
5878d754544SDaniel Vetter 			continue;
5888d754544SDaniel Vetter 
5898d754544SDaniel Vetter 		old_status = connector->status;
5908d754544SDaniel Vetter 
591949f0886SLaurent Pinchart 		connector->status = drm_connector_detect(connector, false);
5928d754544SDaniel Vetter 		DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
5938d754544SDaniel Vetter 			      connector->base.id,
59425933820SJani Nikula 			      connector->name,
5958d754544SDaniel Vetter 			      drm_get_connector_status_name(old_status),
5968d754544SDaniel Vetter 			      drm_get_connector_status_name(connector->status));
5978d754544SDaniel Vetter 		if (old_status != connector->status)
5988d754544SDaniel Vetter 			changed = true;
5998d754544SDaniel Vetter 	}
600b982dab1SThierry Reding 	drm_connector_list_iter_end(&conn_iter);
6018d754544SDaniel Vetter 	mutex_unlock(&dev->mode_config.mutex);
6028d754544SDaniel Vetter 
6038d754544SDaniel Vetter 	if (changed)
6048d754544SDaniel Vetter 		drm_kms_helper_hotplug_event(dev);
6058d754544SDaniel Vetter 
6068d754544SDaniel Vetter 	return changed;
6078d754544SDaniel Vetter }
6088d754544SDaniel Vetter EXPORT_SYMBOL(drm_helper_hpd_irq_event);
609