xref: /openbmc/linux/drivers/gpu/drm/drm_probe_helper.c (revision ea0dd85a75f15174cc2bf75f805e378391995931)
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
468d754544SDaniel Vetter  * implementation of the core connector->fill_modes interface with
478d754544SDaniel 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
58*ea0dd85aSDaniel 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 /**
1184ad640e9SEgbert Eich  * drm_kms_helper_poll_enable_locked - re-enable output polling.
1194ad640e9SEgbert Eich  * @dev: drm_device
1204ad640e9SEgbert Eich  *
1214ad640e9SEgbert Eich  * This function re-enables the output polling work without
1224ad640e9SEgbert Eich  * locking the mode_config mutex.
1234ad640e9SEgbert Eich  *
1244ad640e9SEgbert Eich  * This is like drm_kms_helper_poll_enable() however it is to be
1254ad640e9SEgbert Eich  * called from a context where the mode_config mutex is locked
1264ad640e9SEgbert Eich  * already.
1274ad640e9SEgbert Eich  */
1284ad640e9SEgbert Eich void drm_kms_helper_poll_enable_locked(struct drm_device *dev)
1298c4ccc4aSDaniel Vetter {
1308c4ccc4aSDaniel Vetter 	bool poll = false;
1318c4ccc4aSDaniel Vetter 	struct drm_connector *connector;
132c36a3254SDaniel Vetter 	struct drm_connector_list_iter conn_iter;
133339fd362SPeter Ujfalusi 	unsigned long delay = DRM_OUTPUT_POLL_PERIOD;
1348c4ccc4aSDaniel Vetter 
1358c4ccc4aSDaniel Vetter 	WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
1368c4ccc4aSDaniel Vetter 
1378c4ccc4aSDaniel Vetter 	if (!dev->mode_config.poll_enabled || !drm_kms_helper_poll)
1388c4ccc4aSDaniel Vetter 		return;
1398c4ccc4aSDaniel Vetter 
140c36a3254SDaniel Vetter 	drm_connector_list_iter_get(dev, &conn_iter);
141c36a3254SDaniel Vetter 	drm_for_each_connector_iter(connector, &conn_iter) {
1428c4ccc4aSDaniel Vetter 		if (connector->polled & (DRM_CONNECTOR_POLL_CONNECT |
1438c4ccc4aSDaniel Vetter 					 DRM_CONNECTOR_POLL_DISCONNECT))
1448c4ccc4aSDaniel Vetter 			poll = true;
1458c4ccc4aSDaniel Vetter 	}
146c36a3254SDaniel Vetter 	drm_connector_list_iter_put(&conn_iter);
1478c4ccc4aSDaniel Vetter 
148339fd362SPeter Ujfalusi 	if (dev->mode_config.delayed_event) {
149339fd362SPeter Ujfalusi 		poll = true;
150339fd362SPeter Ujfalusi 		delay = 0;
151339fd362SPeter Ujfalusi 	}
152339fd362SPeter Ujfalusi 
1538c4ccc4aSDaniel Vetter 	if (poll)
154339fd362SPeter Ujfalusi 		schedule_delayed_work(&dev->mode_config.output_poll_work, delay);
1558c4ccc4aSDaniel Vetter }
1564ad640e9SEgbert Eich EXPORT_SYMBOL(drm_kms_helper_poll_enable_locked);
1574ad640e9SEgbert Eich 
158949f0886SLaurent Pinchart static enum drm_connector_status
159949f0886SLaurent Pinchart drm_connector_detect(struct drm_connector *connector, bool force)
160949f0886SLaurent Pinchart {
161949f0886SLaurent Pinchart 	return connector->funcs->detect ?
162949f0886SLaurent Pinchart 		connector->funcs->detect(connector, force) :
163949f0886SLaurent Pinchart 		connector_status_connected;
164949f0886SLaurent Pinchart }
165949f0886SLaurent Pinchart 
1666af3e656SVille Syrjälä /**
1676af3e656SVille Syrjälä  * drm_helper_probe_single_connector_modes - get complete set of display modes
1686af3e656SVille Syrjälä  * @connector: connector to probe
1696af3e656SVille Syrjälä  * @maxX: max width for modes
1706af3e656SVille Syrjälä  * @maxY: max height for modes
1716af3e656SVille Syrjälä  *
1726af3e656SVille Syrjälä  * Based on the helper callbacks implemented by @connector in struct
1736af3e656SVille Syrjälä  * &drm_connector_helper_funcs try to detect all valid modes.  Modes will first
1746af3e656SVille Syrjälä  * be added to the connector's probed_modes list, then culled (based on validity
1756af3e656SVille Syrjälä  * and the @maxX, @maxY parameters) and put into the normal modes list.
1766af3e656SVille Syrjälä  *
1776af3e656SVille Syrjälä  * Intended to be used as a generic implementation of the ->fill_modes()
1786af3e656SVille Syrjälä  * @connector vfunc for drivers that use the CRTC helpers for output mode
1796af3e656SVille Syrjälä  * filtering and detection.
1806af3e656SVille Syrjälä  *
1815dec293bSVille Syrjälä  * The basic procedure is as follows
1826af3e656SVille Syrjälä  *
1835dec293bSVille Syrjälä  * 1. All modes currently on the connector's modes list are marked as stale
1846af3e656SVille Syrjälä  *
1855dec293bSVille Syrjälä  * 2. New modes are added to the connector's probed_modes list with
1865dec293bSVille Syrjälä  *    drm_mode_probed_add(). New modes start their life with status as OK.
1875dec293bSVille Syrjälä  *    Modes are added from a single source using the following priority order.
1885dec293bSVille Syrjälä  *
1895dec293bSVille Syrjälä  *    - debugfs 'override_edid' (used for testing only)
1905dec293bSVille Syrjälä  *    - firmware EDID (drm_load_edid_firmware())
1915dec293bSVille Syrjälä  *    - connector helper ->get_modes() vfunc
1925dec293bSVille Syrjälä  *    - if the connector status is connector_status_connected, standard
1935dec293bSVille Syrjälä  *      VESA DMT modes up to 1024x768 are automatically added
1945dec293bSVille Syrjälä  *      (drm_add_modes_noedid())
1955dec293bSVille Syrjälä  *
1965dec293bSVille Syrjälä  *    Finally modes specified via the kernel command line (video=...) are
1975dec293bSVille Syrjälä  *    added in addition to what the earlier probes produced
1985dec293bSVille Syrjälä  *    (drm_helper_probe_add_cmdline_mode()). These modes are generated
1995dec293bSVille Syrjälä  *    using the VESA GTF/CVT formulas.
2005dec293bSVille Syrjälä  *
2015dec293bSVille Syrjälä  * 3. Modes are moved from the probed_modes list to the modes list. Potential
2025dec293bSVille Syrjälä  *    duplicates are merged together (see drm_mode_connector_list_update()).
2035dec293bSVille Syrjälä  *    After this step the probed_modes list will be empty again.
2045dec293bSVille Syrjälä  *
2055dec293bSVille Syrjälä  * 4. Any non-stale mode on the modes list then undergoes validation
2065dec293bSVille Syrjälä  *
2075dec293bSVille Syrjälä  *    - drm_mode_validate_basic() performs basic sanity checks
2085dec293bSVille Syrjälä  *    - drm_mode_validate_size() filters out modes larger than @maxX and @maxY
2095dec293bSVille Syrjälä  *      (if specified)
2105dec293bSVille Syrjälä  *    - drm_mode_validate_flag() checks the modes againt basic connector
2115dec293bSVille Syrjälä  *      capabilites (interlace_allowed,doublescan_allowed,stereo_allowed)
2125dec293bSVille Syrjälä  *    - the optional connector ->mode_valid() helper can perform driver and/or
2135dec293bSVille Syrjälä  *      hardware specific checks
2145dec293bSVille Syrjälä  *
2155dec293bSVille Syrjälä  * 5. Any mode whose status is not OK is pruned from the connector's modes list,
2165dec293bSVille Syrjälä  *    accompanied by a debug message indicating the reason for the mode's
2175dec293bSVille Syrjälä  *    rejection (see drm_mode_prune_invalid()).
2186af3e656SVille Syrjälä  *
2196af3e656SVille Syrjälä  * Returns:
2206af3e656SVille Syrjälä  * The number of modes found on @connector.
2216af3e656SVille Syrjälä  */
2226af3e656SVille Syrjälä int drm_helper_probe_single_connector_modes(struct drm_connector *connector,
2236af3e656SVille Syrjälä 					    uint32_t maxX, uint32_t maxY)
2248d754544SDaniel Vetter {
2258d754544SDaniel Vetter 	struct drm_device *dev = connector->dev;
2268d754544SDaniel Vetter 	struct drm_display_mode *mode;
227be26a66dSJani Nikula 	const struct drm_connector_helper_funcs *connector_funcs =
2288d754544SDaniel Vetter 		connector->helper_private;
2298d754544SDaniel Vetter 	int count = 0;
2308d754544SDaniel Vetter 	int mode_flags = 0;
2318d754544SDaniel Vetter 	bool verbose_prune = true;
232162b6a57SDaniel Vetter 	enum drm_connector_status old_status;
2338d754544SDaniel Vetter 
2348d754544SDaniel Vetter 	WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
2358d754544SDaniel Vetter 
2368d754544SDaniel Vetter 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", connector->base.id,
23725933820SJani Nikula 			connector->name);
2385ba89406SVille Syrjälä 	/* set all old modes to the stale state */
2398d754544SDaniel Vetter 	list_for_each_entry(mode, &connector->modes, head)
2405ba89406SVille Syrjälä 		mode->status = MODE_STALE;
2418d754544SDaniel Vetter 
242ed293f77SDaniel Vetter 	old_status = connector->status;
243ed293f77SDaniel Vetter 
2448d754544SDaniel Vetter 	if (connector->force) {
2452c4cc91bSPeter Hurley 		if (connector->force == DRM_FORCE_ON ||
2462c4cc91bSPeter Hurley 		    connector->force == DRM_FORCE_ON_DIGITAL)
2478d754544SDaniel Vetter 			connector->status = connector_status_connected;
2488d754544SDaniel Vetter 		else
2498d754544SDaniel Vetter 			connector->status = connector_status_disconnected;
2508d754544SDaniel Vetter 		if (connector->funcs->force)
2518d754544SDaniel Vetter 			connector->funcs->force(connector);
2528d754544SDaniel Vetter 	} else {
253949f0886SLaurent Pinchart 		connector->status = drm_connector_detect(connector, true);
254ed293f77SDaniel Vetter 	}
255162b6a57SDaniel Vetter 
256162b6a57SDaniel Vetter 	/*
257162b6a57SDaniel Vetter 	 * Normally either the driver's hpd code or the poll loop should
258162b6a57SDaniel Vetter 	 * pick up any changes and fire the hotplug event. But if
259162b6a57SDaniel Vetter 	 * userspace sneaks in a probe, we might miss a change. Hence
260162b6a57SDaniel Vetter 	 * check here, and if anything changed start the hotplug code.
261162b6a57SDaniel Vetter 	 */
262162b6a57SDaniel Vetter 	if (old_status != connector->status) {
2634e15f2a1SJani Nikula 		DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
264162b6a57SDaniel Vetter 			      connector->base.id,
265162b6a57SDaniel Vetter 			      connector->name,
2664e15f2a1SJani Nikula 			      drm_get_connector_status_name(old_status),
2674e15f2a1SJani Nikula 			      drm_get_connector_status_name(connector->status));
268162b6a57SDaniel Vetter 
269162b6a57SDaniel Vetter 		/*
270162b6a57SDaniel Vetter 		 * The hotplug event code might call into the fb
271162b6a57SDaniel Vetter 		 * helpers, and so expects that we do not hold any
272162b6a57SDaniel Vetter 		 * locks. Fire up the poll struct instead, it will
273162b6a57SDaniel Vetter 		 * disable itself again.
274162b6a57SDaniel Vetter 		 */
275162b6a57SDaniel Vetter 		dev->mode_config.delayed_event = true;
276162b6a57SDaniel Vetter 		if (dev->mode_config.poll_enabled)
277162b6a57SDaniel Vetter 			schedule_delayed_work(&dev->mode_config.output_poll_work,
278162b6a57SDaniel Vetter 					      0);
279162b6a57SDaniel Vetter 	}
2808d754544SDaniel Vetter 
2818d754544SDaniel Vetter 	/* Re-enable polling in case the global poll config changed. */
2828d754544SDaniel Vetter 	if (drm_kms_helper_poll != dev->mode_config.poll_running)
2834ad640e9SEgbert Eich 		drm_kms_helper_poll_enable_locked(dev);
2848d754544SDaniel Vetter 
2858d754544SDaniel Vetter 	dev->mode_config.poll_running = drm_kms_helper_poll;
2868d754544SDaniel Vetter 
2878d754544SDaniel Vetter 	if (connector->status == connector_status_disconnected) {
2888d754544SDaniel Vetter 		DRM_DEBUG_KMS("[CONNECTOR:%d:%s] disconnected\n",
28925933820SJani Nikula 			connector->base.id, connector->name);
2908d754544SDaniel Vetter 		drm_mode_connector_update_edid_property(connector, NULL);
2918d754544SDaniel Vetter 		verbose_prune = false;
2928d754544SDaniel Vetter 		goto prune;
2938d754544SDaniel Vetter 	}
2948d754544SDaniel Vetter 
2954cf2b281SThomas Wood 	if (connector->override_edid) {
2964cf2b281SThomas Wood 		struct edid *edid = (struct edid *) connector->edid_blob_ptr->data;
2974cf2b281SThomas Wood 
2984cf2b281SThomas Wood 		count = drm_add_edid_modes(connector, edid);
299ad692b46SJani Nikula 		drm_edid_to_eld(connector, edid);
3000e8578c9SVille Syrjälä 	} else {
3010e8578c9SVille Syrjälä 		count = drm_load_edid_firmware(connector);
3020e8578c9SVille Syrjälä 		if (count == 0)
3038d754544SDaniel Vetter 			count = (*connector_funcs->get_modes)(connector);
3044cf2b281SThomas Wood 	}
3058d754544SDaniel Vetter 
3068d754544SDaniel Vetter 	if (count == 0 && connector->status == connector_status_connected)
3078d754544SDaniel Vetter 		count = drm_add_modes_noedid(connector, 1024, 768);
308eaf99c74SChris Wilson 	count += drm_helper_probe_add_cmdline_mode(connector);
3098d754544SDaniel Vetter 	if (count == 0)
3108d754544SDaniel Vetter 		goto prune;
3118d754544SDaniel Vetter 
3126af3e656SVille Syrjälä 	drm_mode_connector_list_update(connector);
3138d754544SDaniel Vetter 
3148d754544SDaniel Vetter 	if (connector->interlace_allowed)
3158d754544SDaniel Vetter 		mode_flags |= DRM_MODE_FLAG_INTERLACE;
3168d754544SDaniel Vetter 	if (connector->doublescan_allowed)
3178d754544SDaniel Vetter 		mode_flags |= DRM_MODE_FLAG_DBLSCAN;
3188d754544SDaniel Vetter 	if (connector->stereo_allowed)
3198d754544SDaniel Vetter 		mode_flags |= DRM_MODE_FLAG_3D_MASK;
3208d754544SDaniel Vetter 
3218d754544SDaniel Vetter 	list_for_each_entry(mode, &connector->modes, head) {
322be8719a6SVille Syrjälä 		if (mode->status == MODE_OK)
323abc0b144SVille Syrjälä 			mode->status = drm_mode_validate_basic(mode);
324abc0b144SVille Syrjälä 
325abc0b144SVille Syrjälä 		if (mode->status == MODE_OK)
32605acaec3SVille Syrjälä 			mode->status = drm_mode_validate_size(mode, maxX, maxY);
32705acaec3SVille Syrjälä 
32805acaec3SVille Syrjälä 		if (mode->status == MODE_OK)
32905acaec3SVille Syrjälä 			mode->status = drm_mode_validate_flag(mode, mode_flags);
33005acaec3SVille Syrjälä 
331f9b0e251SAndrzej Hajda 		if (mode->status == MODE_OK && connector_funcs->mode_valid)
3328d754544SDaniel Vetter 			mode->status = connector_funcs->mode_valid(connector,
3338d754544SDaniel Vetter 								   mode);
3348d754544SDaniel Vetter 	}
3358d754544SDaniel Vetter 
3368d754544SDaniel Vetter prune:
3378d754544SDaniel Vetter 	drm_mode_prune_invalid(dev, &connector->modes, verbose_prune);
3388d754544SDaniel Vetter 
3398d754544SDaniel Vetter 	if (list_empty(&connector->modes))
3408d754544SDaniel Vetter 		return 0;
3418d754544SDaniel Vetter 
3428d754544SDaniel Vetter 	list_for_each_entry(mode, &connector->modes, head)
3438d754544SDaniel Vetter 		mode->vrefresh = drm_mode_vrefresh(mode);
3448d754544SDaniel Vetter 
3458d754544SDaniel Vetter 	drm_mode_sort(&connector->modes);
3468d754544SDaniel Vetter 
3478d754544SDaniel Vetter 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s] probed modes :\n", connector->base.id,
34825933820SJani Nikula 			connector->name);
3498d754544SDaniel Vetter 	list_for_each_entry(mode, &connector->modes, head) {
3508d754544SDaniel Vetter 		drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
3518d754544SDaniel Vetter 		drm_mode_debug_printmodeline(mode);
3528d754544SDaniel Vetter 	}
3538d754544SDaniel Vetter 
3548d754544SDaniel Vetter 	return count;
3558d754544SDaniel Vetter }
3568d754544SDaniel Vetter EXPORT_SYMBOL(drm_helper_probe_single_connector_modes);
3578d754544SDaniel Vetter 
3588d754544SDaniel Vetter /**
3598d754544SDaniel Vetter  * drm_kms_helper_hotplug_event - fire off KMS hotplug events
3608d754544SDaniel Vetter  * @dev: drm_device whose connector state changed
3618d754544SDaniel Vetter  *
3628d754544SDaniel Vetter  * This function fires off the uevent for userspace and also calls the
3638d754544SDaniel Vetter  * output_poll_changed function, which is most commonly used to inform the fbdev
3648d754544SDaniel Vetter  * emulation code and allow it to update the fbcon output configuration.
3658d754544SDaniel Vetter  *
3668d754544SDaniel Vetter  * Drivers should call this from their hotplug handling code when a change is
3678d754544SDaniel Vetter  * detected. Note that this function does not do any output detection of its
3688d754544SDaniel Vetter  * own, like drm_helper_hpd_irq_event() does - this is assumed to be done by the
3698d754544SDaniel Vetter  * driver already.
3708d754544SDaniel Vetter  *
3718d754544SDaniel Vetter  * This function must be called from process context with no mode
3728d754544SDaniel Vetter  * setting locks held.
3738d754544SDaniel Vetter  */
3748d754544SDaniel Vetter void drm_kms_helper_hotplug_event(struct drm_device *dev)
3758d754544SDaniel Vetter {
3768d754544SDaniel Vetter 	/* send a uevent + call fbdev */
3778d754544SDaniel Vetter 	drm_sysfs_hotplug_event(dev);
3788d754544SDaniel Vetter 	if (dev->mode_config.funcs->output_poll_changed)
3798d754544SDaniel Vetter 		dev->mode_config.funcs->output_poll_changed(dev);
3808d754544SDaniel Vetter }
3818d754544SDaniel Vetter EXPORT_SYMBOL(drm_kms_helper_hotplug_event);
3828d754544SDaniel Vetter 
3838d754544SDaniel Vetter static void output_poll_execute(struct work_struct *work)
3848d754544SDaniel Vetter {
3858d754544SDaniel Vetter 	struct delayed_work *delayed_work = to_delayed_work(work);
3868d754544SDaniel Vetter 	struct drm_device *dev = container_of(delayed_work, struct drm_device, mode_config.output_poll_work);
3878d754544SDaniel Vetter 	struct drm_connector *connector;
388c36a3254SDaniel Vetter 	struct drm_connector_list_iter conn_iter;
3898d754544SDaniel Vetter 	enum drm_connector_status old_status;
390162b6a57SDaniel Vetter 	bool repoll = false, changed;
391162b6a57SDaniel Vetter 
392162b6a57SDaniel Vetter 	/* Pick up any changes detected by the probe functions. */
393162b6a57SDaniel Vetter 	changed = dev->mode_config.delayed_event;
394162b6a57SDaniel Vetter 	dev->mode_config.delayed_event = false;
3958d754544SDaniel Vetter 
3968d754544SDaniel Vetter 	if (!drm_kms_helper_poll)
397162b6a57SDaniel Vetter 		goto out;
3988d754544SDaniel Vetter 
3991fe7841dSChris Wilson 	if (!mutex_trylock(&dev->mode_config.mutex)) {
4001fe7841dSChris Wilson 		repoll = true;
4011fe7841dSChris Wilson 		goto out;
4021fe7841dSChris Wilson 	}
4031fe7841dSChris Wilson 
404c36a3254SDaniel Vetter 	drm_connector_list_iter_get(dev, &conn_iter);
405c36a3254SDaniel Vetter 	drm_for_each_connector_iter(connector, &conn_iter) {
4068d754544SDaniel Vetter 		/* Ignore forced connectors. */
4078d754544SDaniel Vetter 		if (connector->force)
4088d754544SDaniel Vetter 			continue;
4098d754544SDaniel Vetter 
4108d754544SDaniel Vetter 		/* Ignore HPD capable connectors and connectors where we don't
4118d754544SDaniel Vetter 		 * want any hotplug detection at all for polling. */
4128d754544SDaniel Vetter 		if (!connector->polled || connector->polled == DRM_CONNECTOR_POLL_HPD)
4138d754544SDaniel Vetter 			continue;
4148d754544SDaniel Vetter 
4158d754544SDaniel Vetter 		old_status = connector->status;
4168d754544SDaniel Vetter 		/* if we are connected and don't want to poll for disconnect
4178d754544SDaniel Vetter 		   skip it */
4188d754544SDaniel Vetter 		if (old_status == connector_status_connected &&
4198d754544SDaniel Vetter 		    !(connector->polled & DRM_CONNECTOR_POLL_DISCONNECT))
4208d754544SDaniel Vetter 			continue;
4218d754544SDaniel Vetter 
422a3c6d686SJosef Holzmayr 		repoll = true;
423a3c6d686SJosef Holzmayr 
424949f0886SLaurent Pinchart 		connector->status = drm_connector_detect(connector, false);
4258d754544SDaniel Vetter 		if (old_status != connector->status) {
4268d754544SDaniel Vetter 			const char *old, *new;
4278d754544SDaniel Vetter 
428b7703726SDaniel Vetter 			/*
429b7703726SDaniel Vetter 			 * The poll work sets force=false when calling detect so
430b7703726SDaniel Vetter 			 * that drivers can avoid to do disruptive tests (e.g.
431b7703726SDaniel Vetter 			 * when load detect cycles could cause flickering on
432b7703726SDaniel Vetter 			 * other, running displays). This bears the risk that we
433b7703726SDaniel Vetter 			 * flip-flop between unknown here in the poll work and
434b7703726SDaniel Vetter 			 * the real state when userspace forces a full detect
435b7703726SDaniel Vetter 			 * call after receiving a hotplug event due to this
436b7703726SDaniel Vetter 			 * change.
437b7703726SDaniel Vetter 			 *
438b7703726SDaniel Vetter 			 * Hence clamp an unknown detect status to the old
439b7703726SDaniel Vetter 			 * value.
440b7703726SDaniel Vetter 			 */
441b7703726SDaniel Vetter 			if (connector->status == connector_status_unknown) {
442b7703726SDaniel Vetter 				connector->status = old_status;
443b7703726SDaniel Vetter 				continue;
444b7703726SDaniel Vetter 			}
445b7703726SDaniel Vetter 
4468d754544SDaniel Vetter 			old = drm_get_connector_status_name(old_status);
4478d754544SDaniel Vetter 			new = drm_get_connector_status_name(connector->status);
4488d754544SDaniel Vetter 
4498d754544SDaniel Vetter 			DRM_DEBUG_KMS("[CONNECTOR:%d:%s] "
4508d754544SDaniel Vetter 				      "status updated from %s to %s\n",
4518d754544SDaniel Vetter 				      connector->base.id,
45225933820SJani Nikula 				      connector->name,
4538d754544SDaniel Vetter 				      old, new);
4548d754544SDaniel Vetter 
4558d754544SDaniel Vetter 			changed = true;
4568d754544SDaniel Vetter 		}
4578d754544SDaniel Vetter 	}
458c36a3254SDaniel Vetter 	drm_connector_list_iter_put(&conn_iter);
4598d754544SDaniel Vetter 
4608d754544SDaniel Vetter 	mutex_unlock(&dev->mode_config.mutex);
4618d754544SDaniel Vetter 
462162b6a57SDaniel Vetter out:
4638d754544SDaniel Vetter 	if (changed)
4648d754544SDaniel Vetter 		drm_kms_helper_hotplug_event(dev);
4658d754544SDaniel Vetter 
4668d754544SDaniel Vetter 	if (repoll)
4678d754544SDaniel Vetter 		schedule_delayed_work(delayed_work, DRM_OUTPUT_POLL_PERIOD);
4688d754544SDaniel Vetter }
4698d754544SDaniel Vetter 
4708d754544SDaniel Vetter /**
4718d754544SDaniel Vetter  * drm_kms_helper_poll_disable - disable output polling
4728d754544SDaniel Vetter  * @dev: drm_device
4738d754544SDaniel Vetter  *
4748d754544SDaniel Vetter  * This function disables the output polling work.
4758d754544SDaniel Vetter  *
4768d754544SDaniel Vetter  * Drivers can call this helper from their device suspend implementation. It is
4778d754544SDaniel Vetter  * not an error to call this even when output polling isn't enabled or arlready
4788d754544SDaniel Vetter  * disabled.
4798d754544SDaniel Vetter  */
4808d754544SDaniel Vetter void drm_kms_helper_poll_disable(struct drm_device *dev)
4818d754544SDaniel Vetter {
4828d754544SDaniel Vetter 	if (!dev->mode_config.poll_enabled)
4838d754544SDaniel Vetter 		return;
4848d754544SDaniel Vetter 	cancel_delayed_work_sync(&dev->mode_config.output_poll_work);
4858d754544SDaniel Vetter }
4868d754544SDaniel Vetter EXPORT_SYMBOL(drm_kms_helper_poll_disable);
4878d754544SDaniel Vetter 
4888d754544SDaniel Vetter /**
4898d754544SDaniel Vetter  * drm_kms_helper_poll_enable - re-enable output polling.
4908d754544SDaniel Vetter  * @dev: drm_device
4918d754544SDaniel Vetter  *
4928d754544SDaniel Vetter  * This function re-enables the output polling work.
4938d754544SDaniel Vetter  *
4948d754544SDaniel Vetter  * Drivers can call this helper from their device resume implementation. It is
4958d754544SDaniel Vetter  * an error to call this when the output polling support has not yet been set
4968d754544SDaniel Vetter  * up.
4978d754544SDaniel Vetter  */
4988d754544SDaniel Vetter void drm_kms_helper_poll_enable(struct drm_device *dev)
4998d754544SDaniel Vetter {
5008c4ccc4aSDaniel Vetter 	mutex_lock(&dev->mode_config.mutex);
5014ad640e9SEgbert Eich 	drm_kms_helper_poll_enable_locked(dev);
5028c4ccc4aSDaniel Vetter 	mutex_unlock(&dev->mode_config.mutex);
5038d754544SDaniel Vetter }
5048d754544SDaniel Vetter EXPORT_SYMBOL(drm_kms_helper_poll_enable);
5058d754544SDaniel Vetter 
5068d754544SDaniel Vetter /**
5078d754544SDaniel Vetter  * drm_kms_helper_poll_init - initialize and enable output polling
5088d754544SDaniel Vetter  * @dev: drm_device
5098d754544SDaniel Vetter  *
5108d754544SDaniel Vetter  * This function intializes and then also enables output polling support for
5118d754544SDaniel Vetter  * @dev. Drivers which do not have reliable hotplug support in hardware can use
5128d754544SDaniel Vetter  * this helper infrastructure to regularly poll such connectors for changes in
5138d754544SDaniel Vetter  * their connection state.
5148d754544SDaniel Vetter  *
5158d754544SDaniel Vetter  * Drivers can control which connectors are polled by setting the
5168d754544SDaniel Vetter  * DRM_CONNECTOR_POLL_CONNECT and DRM_CONNECTOR_POLL_DISCONNECT flags. On
5178d754544SDaniel Vetter  * connectors where probing live outputs can result in visual distortion drivers
5188d754544SDaniel Vetter  * should not set the DRM_CONNECTOR_POLL_DISCONNECT flag to avoid this.
5198d754544SDaniel Vetter  * Connectors which have no flag or only DRM_CONNECTOR_POLL_HPD set are
5208d754544SDaniel Vetter  * completely ignored by the polling logic.
5218d754544SDaniel Vetter  *
5228d754544SDaniel Vetter  * Note that a connector can be both polled and probed from the hotplug handler,
5238d754544SDaniel Vetter  * in case the hotplug interrupt is known to be unreliable.
5248d754544SDaniel Vetter  */
5258d754544SDaniel Vetter void drm_kms_helper_poll_init(struct drm_device *dev)
5268d754544SDaniel Vetter {
5278d754544SDaniel Vetter 	INIT_DELAYED_WORK(&dev->mode_config.output_poll_work, output_poll_execute);
5288d754544SDaniel Vetter 	dev->mode_config.poll_enabled = true;
5298d754544SDaniel Vetter 
5308d754544SDaniel Vetter 	drm_kms_helper_poll_enable(dev);
5318d754544SDaniel Vetter }
5328d754544SDaniel Vetter EXPORT_SYMBOL(drm_kms_helper_poll_init);
5338d754544SDaniel Vetter 
5348d754544SDaniel Vetter /**
5358d754544SDaniel Vetter  * drm_kms_helper_poll_fini - disable output polling and clean it up
5368d754544SDaniel Vetter  * @dev: drm_device
5378d754544SDaniel Vetter  */
5388d754544SDaniel Vetter void drm_kms_helper_poll_fini(struct drm_device *dev)
5398d754544SDaniel Vetter {
5408d754544SDaniel Vetter 	drm_kms_helper_poll_disable(dev);
5418d754544SDaniel Vetter }
5428d754544SDaniel Vetter EXPORT_SYMBOL(drm_kms_helper_poll_fini);
5438d754544SDaniel Vetter 
5448d754544SDaniel Vetter /**
5458d754544SDaniel Vetter  * drm_helper_hpd_irq_event - hotplug processing
5468d754544SDaniel Vetter  * @dev: drm_device
5478d754544SDaniel Vetter  *
5488d754544SDaniel Vetter  * Drivers can use this helper function to run a detect cycle on all connectors
5498d754544SDaniel Vetter  * which have the DRM_CONNECTOR_POLL_HPD flag set in their &polled member. All
5508d754544SDaniel Vetter  * other connectors are ignored, which is useful to avoid reprobing fixed
5518d754544SDaniel Vetter  * panels.
5528d754544SDaniel Vetter  *
5538d754544SDaniel Vetter  * This helper function is useful for drivers which can't or don't track hotplug
5548d754544SDaniel Vetter  * interrupts for each connector.
5558d754544SDaniel Vetter  *
5568d754544SDaniel Vetter  * Drivers which support hotplug interrupts for each connector individually and
5578d754544SDaniel Vetter  * which have a more fine-grained detect logic should bypass this code and
5588d754544SDaniel Vetter  * directly call drm_kms_helper_hotplug_event() in case the connector state
5598d754544SDaniel Vetter  * changed.
5608d754544SDaniel Vetter  *
5618d754544SDaniel Vetter  * This function must be called from process context with no mode
5628d754544SDaniel Vetter  * setting locks held.
5638d754544SDaniel Vetter  *
5648d754544SDaniel Vetter  * Note that a connector can be both polled and probed from the hotplug handler,
5658d754544SDaniel Vetter  * in case the hotplug interrupt is known to be unreliable.
5668d754544SDaniel Vetter  */
5678d754544SDaniel Vetter bool drm_helper_hpd_irq_event(struct drm_device *dev)
5688d754544SDaniel Vetter {
5698d754544SDaniel Vetter 	struct drm_connector *connector;
570c36a3254SDaniel Vetter 	struct drm_connector_list_iter conn_iter;
5718d754544SDaniel Vetter 	enum drm_connector_status old_status;
5728d754544SDaniel Vetter 	bool changed = false;
5738d754544SDaniel Vetter 
5748d754544SDaniel Vetter 	if (!dev->mode_config.poll_enabled)
5758d754544SDaniel Vetter 		return false;
5768d754544SDaniel Vetter 
5778d754544SDaniel Vetter 	mutex_lock(&dev->mode_config.mutex);
578c36a3254SDaniel Vetter 	drm_connector_list_iter_get(dev, &conn_iter);
579c36a3254SDaniel Vetter 	drm_for_each_connector_iter(connector, &conn_iter) {
5808d754544SDaniel Vetter 		/* Only handle HPD capable connectors. */
5818d754544SDaniel Vetter 		if (!(connector->polled & DRM_CONNECTOR_POLL_HPD))
5828d754544SDaniel Vetter 			continue;
5838d754544SDaniel Vetter 
5848d754544SDaniel Vetter 		old_status = connector->status;
5858d754544SDaniel Vetter 
586949f0886SLaurent Pinchart 		connector->status = drm_connector_detect(connector, false);
5878d754544SDaniel Vetter 		DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
5888d754544SDaniel Vetter 			      connector->base.id,
58925933820SJani Nikula 			      connector->name,
5908d754544SDaniel Vetter 			      drm_get_connector_status_name(old_status),
5918d754544SDaniel Vetter 			      drm_get_connector_status_name(connector->status));
5928d754544SDaniel Vetter 		if (old_status != connector->status)
5938d754544SDaniel Vetter 			changed = true;
5948d754544SDaniel Vetter 	}
595c36a3254SDaniel Vetter 	drm_connector_list_iter_put(&conn_iter);
5968d754544SDaniel Vetter 	mutex_unlock(&dev->mode_config.mutex);
5978d754544SDaniel Vetter 
5988d754544SDaniel Vetter 	if (changed)
5998d754544SDaniel Vetter 		drm_kms_helper_hotplug_event(dev);
6008d754544SDaniel Vetter 
6018d754544SDaniel Vetter 	return changed;
6028d754544SDaniel Vetter }
6038d754544SDaniel Vetter EXPORT_SYMBOL(drm_helper_hpd_irq_event);
604