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. 568d754544SDaniel Vetter */ 578d754544SDaniel Vetter 588d754544SDaniel Vetter static bool drm_kms_helper_poll = true; 598d754544SDaniel Vetter module_param_named(poll, drm_kms_helper_poll, bool, 0600); 608d754544SDaniel Vetter 6105acaec3SVille Syrjälä static enum drm_mode_status 6205acaec3SVille Syrjälä drm_mode_validate_flag(const struct drm_display_mode *mode, 638d754544SDaniel Vetter int flags) 648d754544SDaniel Vetter { 658d754544SDaniel Vetter if ((mode->flags & DRM_MODE_FLAG_INTERLACE) && 668d754544SDaniel Vetter !(flags & DRM_MODE_FLAG_INTERLACE)) 6705acaec3SVille Syrjälä return MODE_NO_INTERLACE; 6805acaec3SVille Syrjälä 698d754544SDaniel Vetter if ((mode->flags & DRM_MODE_FLAG_DBLSCAN) && 708d754544SDaniel Vetter !(flags & DRM_MODE_FLAG_DBLSCAN)) 7105acaec3SVille Syrjälä return MODE_NO_DBLESCAN; 7205acaec3SVille Syrjälä 738d754544SDaniel Vetter if ((mode->flags & DRM_MODE_FLAG_3D_MASK) && 748d754544SDaniel Vetter !(flags & DRM_MODE_FLAG_3D_MASK)) 7505acaec3SVille Syrjälä return MODE_NO_STEREO; 768d754544SDaniel Vetter 7705acaec3SVille Syrjälä return MODE_OK; 788d754544SDaniel Vetter } 798d754544SDaniel Vetter 80eaf99c74SChris Wilson static int drm_helper_probe_add_cmdline_mode(struct drm_connector *connector) 81eaf99c74SChris Wilson { 82eaf99c74SChris Wilson struct drm_display_mode *mode; 83eaf99c74SChris Wilson 84eaf99c74SChris Wilson if (!connector->cmdline_mode.specified) 85eaf99c74SChris Wilson return 0; 86eaf99c74SChris Wilson 87eaf99c74SChris Wilson mode = drm_mode_create_from_cmdline_mode(connector->dev, 88eaf99c74SChris Wilson &connector->cmdline_mode); 89eaf99c74SChris Wilson if (mode == NULL) 90eaf99c74SChris Wilson return 0; 91eaf99c74SChris Wilson 92eaf99c74SChris Wilson drm_mode_probed_add(connector, mode); 93eaf99c74SChris Wilson return 1; 94eaf99c74SChris Wilson } 95eaf99c74SChris Wilson 96*8c4ccc4aSDaniel Vetter #define DRM_OUTPUT_POLL_PERIOD (10*HZ) 97*8c4ccc4aSDaniel Vetter static void __drm_kms_helper_poll_enable(struct drm_device *dev) 98*8c4ccc4aSDaniel Vetter { 99*8c4ccc4aSDaniel Vetter bool poll = false; 100*8c4ccc4aSDaniel Vetter struct drm_connector *connector; 101*8c4ccc4aSDaniel Vetter 102*8c4ccc4aSDaniel Vetter WARN_ON(!mutex_is_locked(&dev->mode_config.mutex)); 103*8c4ccc4aSDaniel Vetter 104*8c4ccc4aSDaniel Vetter if (!dev->mode_config.poll_enabled || !drm_kms_helper_poll) 105*8c4ccc4aSDaniel Vetter return; 106*8c4ccc4aSDaniel Vetter 107*8c4ccc4aSDaniel Vetter drm_for_each_connector(connector, dev) { 108*8c4ccc4aSDaniel Vetter if (connector->polled & (DRM_CONNECTOR_POLL_CONNECT | 109*8c4ccc4aSDaniel Vetter DRM_CONNECTOR_POLL_DISCONNECT)) 110*8c4ccc4aSDaniel Vetter poll = true; 111*8c4ccc4aSDaniel Vetter } 112*8c4ccc4aSDaniel Vetter 113*8c4ccc4aSDaniel Vetter if (poll) 114*8c4ccc4aSDaniel Vetter schedule_delayed_work(&dev->mode_config.output_poll_work, DRM_OUTPUT_POLL_PERIOD); 115*8c4ccc4aSDaniel Vetter } 116*8c4ccc4aSDaniel Vetter 117b87577b7SDave Airlie static int drm_helper_probe_single_connector_modes_merge_bits(struct drm_connector *connector, 118b87577b7SDave Airlie uint32_t maxX, uint32_t maxY, bool merge_type_bits) 1198d754544SDaniel Vetter { 1208d754544SDaniel Vetter struct drm_device *dev = connector->dev; 1218d754544SDaniel Vetter struct drm_display_mode *mode; 122be26a66dSJani Nikula const struct drm_connector_helper_funcs *connector_funcs = 1238d754544SDaniel Vetter connector->helper_private; 1248d754544SDaniel Vetter int count = 0; 1258d754544SDaniel Vetter int mode_flags = 0; 1268d754544SDaniel Vetter bool verbose_prune = true; 127162b6a57SDaniel Vetter enum drm_connector_status old_status; 1288d754544SDaniel Vetter 1298d754544SDaniel Vetter WARN_ON(!mutex_is_locked(&dev->mode_config.mutex)); 1308d754544SDaniel Vetter 1318d754544SDaniel Vetter DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", connector->base.id, 13225933820SJani Nikula connector->name); 1338d754544SDaniel Vetter /* set all modes to the unverified state */ 1348d754544SDaniel Vetter list_for_each_entry(mode, &connector->modes, head) 1358d754544SDaniel Vetter mode->status = MODE_UNVERIFIED; 1368d754544SDaniel Vetter 1378d754544SDaniel Vetter if (connector->force) { 1382c4cc91bSPeter Hurley if (connector->force == DRM_FORCE_ON || 1392c4cc91bSPeter Hurley connector->force == DRM_FORCE_ON_DIGITAL) 1408d754544SDaniel Vetter connector->status = connector_status_connected; 1418d754544SDaniel Vetter else 1428d754544SDaniel Vetter connector->status = connector_status_disconnected; 1438d754544SDaniel Vetter if (connector->funcs->force) 1448d754544SDaniel Vetter connector->funcs->force(connector); 1458d754544SDaniel Vetter } else { 146162b6a57SDaniel Vetter old_status = connector->status; 147162b6a57SDaniel Vetter 1488d754544SDaniel Vetter connector->status = connector->funcs->detect(connector, true); 149162b6a57SDaniel Vetter 150162b6a57SDaniel Vetter /* 151162b6a57SDaniel Vetter * Normally either the driver's hpd code or the poll loop should 152162b6a57SDaniel Vetter * pick up any changes and fire the hotplug event. But if 153162b6a57SDaniel Vetter * userspace sneaks in a probe, we might miss a change. Hence 154162b6a57SDaniel Vetter * check here, and if anything changed start the hotplug code. 155162b6a57SDaniel Vetter */ 156162b6a57SDaniel Vetter if (old_status != connector->status) { 157162b6a57SDaniel Vetter DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %d to %d\n", 158162b6a57SDaniel Vetter connector->base.id, 159162b6a57SDaniel Vetter connector->name, 160162b6a57SDaniel Vetter old_status, connector->status); 161162b6a57SDaniel Vetter 162162b6a57SDaniel Vetter /* 163162b6a57SDaniel Vetter * The hotplug event code might call into the fb 164162b6a57SDaniel Vetter * helpers, and so expects that we do not hold any 165162b6a57SDaniel Vetter * locks. Fire up the poll struct instead, it will 166162b6a57SDaniel Vetter * disable itself again. 167162b6a57SDaniel Vetter */ 168162b6a57SDaniel Vetter dev->mode_config.delayed_event = true; 169162b6a57SDaniel Vetter if (dev->mode_config.poll_enabled) 170162b6a57SDaniel Vetter schedule_delayed_work(&dev->mode_config.output_poll_work, 171162b6a57SDaniel Vetter 0); 172162b6a57SDaniel Vetter } 1738d754544SDaniel Vetter } 1748d754544SDaniel Vetter 1758d754544SDaniel Vetter /* Re-enable polling in case the global poll config changed. */ 1768d754544SDaniel Vetter if (drm_kms_helper_poll != dev->mode_config.poll_running) 177*8c4ccc4aSDaniel Vetter __drm_kms_helper_poll_enable(dev); 1788d754544SDaniel Vetter 1798d754544SDaniel Vetter dev->mode_config.poll_running = drm_kms_helper_poll; 1808d754544SDaniel Vetter 1818d754544SDaniel Vetter if (connector->status == connector_status_disconnected) { 1828d754544SDaniel Vetter DRM_DEBUG_KMS("[CONNECTOR:%d:%s] disconnected\n", 18325933820SJani Nikula connector->base.id, connector->name); 1848d754544SDaniel Vetter drm_mode_connector_update_edid_property(connector, NULL); 1858d754544SDaniel Vetter verbose_prune = false; 1868d754544SDaniel Vetter goto prune; 1878d754544SDaniel Vetter } 1888d754544SDaniel Vetter 1898d754544SDaniel Vetter #ifdef CONFIG_DRM_LOAD_EDID_FIRMWARE 1908d754544SDaniel Vetter count = drm_load_edid_firmware(connector); 1918d754544SDaniel Vetter if (count == 0) 1928d754544SDaniel Vetter #endif 1934cf2b281SThomas Wood { 1944cf2b281SThomas Wood if (connector->override_edid) { 1954cf2b281SThomas Wood struct edid *edid = (struct edid *) connector->edid_blob_ptr->data; 1964cf2b281SThomas Wood 1974cf2b281SThomas Wood count = drm_add_edid_modes(connector, edid); 198ad692b46SJani Nikula drm_edid_to_eld(connector, edid); 1994cf2b281SThomas Wood } else 2008d754544SDaniel Vetter count = (*connector_funcs->get_modes)(connector); 2014cf2b281SThomas Wood } 2028d754544SDaniel Vetter 2038d754544SDaniel Vetter if (count == 0 && connector->status == connector_status_connected) 2048d754544SDaniel Vetter count = drm_add_modes_noedid(connector, 1024, 768); 205eaf99c74SChris Wilson count += drm_helper_probe_add_cmdline_mode(connector); 2068d754544SDaniel Vetter if (count == 0) 2078d754544SDaniel Vetter goto prune; 2088d754544SDaniel Vetter 209b87577b7SDave Airlie drm_mode_connector_list_update(connector, merge_type_bits); 2108d754544SDaniel Vetter 2118d754544SDaniel Vetter if (connector->interlace_allowed) 2128d754544SDaniel Vetter mode_flags |= DRM_MODE_FLAG_INTERLACE; 2138d754544SDaniel Vetter if (connector->doublescan_allowed) 2148d754544SDaniel Vetter mode_flags |= DRM_MODE_FLAG_DBLSCAN; 2158d754544SDaniel Vetter if (connector->stereo_allowed) 2168d754544SDaniel Vetter mode_flags |= DRM_MODE_FLAG_3D_MASK; 2178d754544SDaniel Vetter 2188d754544SDaniel Vetter list_for_each_entry(mode, &connector->modes, head) { 219abc0b144SVille Syrjälä mode->status = drm_mode_validate_basic(mode); 220abc0b144SVille Syrjälä 221abc0b144SVille Syrjälä if (mode->status == MODE_OK) 22205acaec3SVille Syrjälä mode->status = drm_mode_validate_size(mode, maxX, maxY); 22305acaec3SVille Syrjälä 22405acaec3SVille Syrjälä if (mode->status == MODE_OK) 22505acaec3SVille Syrjälä mode->status = drm_mode_validate_flag(mode, mode_flags); 22605acaec3SVille Syrjälä 227f9b0e251SAndrzej Hajda if (mode->status == MODE_OK && connector_funcs->mode_valid) 2288d754544SDaniel Vetter mode->status = connector_funcs->mode_valid(connector, 2298d754544SDaniel Vetter mode); 2308d754544SDaniel Vetter } 2318d754544SDaniel Vetter 2328d754544SDaniel Vetter prune: 2338d754544SDaniel Vetter drm_mode_prune_invalid(dev, &connector->modes, verbose_prune); 2348d754544SDaniel Vetter 2358d754544SDaniel Vetter if (list_empty(&connector->modes)) 2368d754544SDaniel Vetter return 0; 2378d754544SDaniel Vetter 2388d754544SDaniel Vetter list_for_each_entry(mode, &connector->modes, head) 2398d754544SDaniel Vetter mode->vrefresh = drm_mode_vrefresh(mode); 2408d754544SDaniel Vetter 2418d754544SDaniel Vetter drm_mode_sort(&connector->modes); 2428d754544SDaniel Vetter 2438d754544SDaniel Vetter DRM_DEBUG_KMS("[CONNECTOR:%d:%s] probed modes :\n", connector->base.id, 24425933820SJani Nikula connector->name); 2458d754544SDaniel Vetter list_for_each_entry(mode, &connector->modes, head) { 2468d754544SDaniel Vetter drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V); 2478d754544SDaniel Vetter drm_mode_debug_printmodeline(mode); 2488d754544SDaniel Vetter } 2498d754544SDaniel Vetter 2508d754544SDaniel Vetter return count; 2518d754544SDaniel Vetter } 252b87577b7SDave Airlie 253b87577b7SDave Airlie /** 254b87577b7SDave Airlie * drm_helper_probe_single_connector_modes - get complete set of display modes 255b87577b7SDave Airlie * @connector: connector to probe 256b87577b7SDave Airlie * @maxX: max width for modes 257b87577b7SDave Airlie * @maxY: max height for modes 258b87577b7SDave Airlie * 259b87577b7SDave Airlie * Based on the helper callbacks implemented by @connector try to detect all 260b87577b7SDave Airlie * valid modes. Modes will first be added to the connector's probed_modes list, 261b87577b7SDave Airlie * then culled (based on validity and the @maxX, @maxY parameters) and put into 262b87577b7SDave Airlie * the normal modes list. 263b87577b7SDave Airlie * 264b87577b7SDave Airlie * Intended to be use as a generic implementation of the ->fill_modes() 265b87577b7SDave Airlie * @connector vfunc for drivers that use the crtc helpers for output mode 266b87577b7SDave Airlie * filtering and detection. 267b87577b7SDave Airlie * 268b87577b7SDave Airlie * Returns: 269b87577b7SDave Airlie * The number of modes found on @connector. 270b87577b7SDave Airlie */ 271b87577b7SDave Airlie int drm_helper_probe_single_connector_modes(struct drm_connector *connector, 272b87577b7SDave Airlie uint32_t maxX, uint32_t maxY) 273b87577b7SDave Airlie { 274b87577b7SDave Airlie return drm_helper_probe_single_connector_modes_merge_bits(connector, maxX, maxY, true); 275b87577b7SDave Airlie } 2768d754544SDaniel Vetter EXPORT_SYMBOL(drm_helper_probe_single_connector_modes); 2778d754544SDaniel Vetter 2788d754544SDaniel Vetter /** 279b87577b7SDave Airlie * drm_helper_probe_single_connector_modes_nomerge - get complete set of display modes 280b87577b7SDave Airlie * @connector: connector to probe 281b87577b7SDave Airlie * @maxX: max width for modes 282b87577b7SDave Airlie * @maxY: max height for modes 283b87577b7SDave Airlie * 284b87577b7SDave Airlie * This operates like drm_hehlper_probe_single_connector_modes except it 285b87577b7SDave Airlie * replaces the mode bits instead of merging them for preferred modes. 286b87577b7SDave Airlie */ 287b87577b7SDave Airlie int drm_helper_probe_single_connector_modes_nomerge(struct drm_connector *connector, 288b87577b7SDave Airlie uint32_t maxX, uint32_t maxY) 289b87577b7SDave Airlie { 290b87577b7SDave Airlie return drm_helper_probe_single_connector_modes_merge_bits(connector, maxX, maxY, false); 291b87577b7SDave Airlie } 292b87577b7SDave Airlie EXPORT_SYMBOL(drm_helper_probe_single_connector_modes_nomerge); 293b87577b7SDave Airlie 294b87577b7SDave Airlie /** 2958d754544SDaniel Vetter * drm_kms_helper_hotplug_event - fire off KMS hotplug events 2968d754544SDaniel Vetter * @dev: drm_device whose connector state changed 2978d754544SDaniel Vetter * 2988d754544SDaniel Vetter * This function fires off the uevent for userspace and also calls the 2998d754544SDaniel Vetter * output_poll_changed function, which is most commonly used to inform the fbdev 3008d754544SDaniel Vetter * emulation code and allow it to update the fbcon output configuration. 3018d754544SDaniel Vetter * 3028d754544SDaniel Vetter * Drivers should call this from their hotplug handling code when a change is 3038d754544SDaniel Vetter * detected. Note that this function does not do any output detection of its 3048d754544SDaniel Vetter * own, like drm_helper_hpd_irq_event() does - this is assumed to be done by the 3058d754544SDaniel Vetter * driver already. 3068d754544SDaniel Vetter * 3078d754544SDaniel Vetter * This function must be called from process context with no mode 3088d754544SDaniel Vetter * setting locks held. 3098d754544SDaniel Vetter */ 3108d754544SDaniel Vetter void drm_kms_helper_hotplug_event(struct drm_device *dev) 3118d754544SDaniel Vetter { 3128d754544SDaniel Vetter /* send a uevent + call fbdev */ 3138d754544SDaniel Vetter drm_sysfs_hotplug_event(dev); 3148d754544SDaniel Vetter if (dev->mode_config.funcs->output_poll_changed) 3158d754544SDaniel Vetter dev->mode_config.funcs->output_poll_changed(dev); 3168d754544SDaniel Vetter } 3178d754544SDaniel Vetter EXPORT_SYMBOL(drm_kms_helper_hotplug_event); 3188d754544SDaniel Vetter 3198d754544SDaniel Vetter static void output_poll_execute(struct work_struct *work) 3208d754544SDaniel Vetter { 3218d754544SDaniel Vetter struct delayed_work *delayed_work = to_delayed_work(work); 3228d754544SDaniel Vetter struct drm_device *dev = container_of(delayed_work, struct drm_device, mode_config.output_poll_work); 3238d754544SDaniel Vetter struct drm_connector *connector; 3248d754544SDaniel Vetter enum drm_connector_status old_status; 325162b6a57SDaniel Vetter bool repoll = false, changed; 326162b6a57SDaniel Vetter 327162b6a57SDaniel Vetter /* Pick up any changes detected by the probe functions. */ 328162b6a57SDaniel Vetter changed = dev->mode_config.delayed_event; 329162b6a57SDaniel Vetter dev->mode_config.delayed_event = false; 3308d754544SDaniel Vetter 3318d754544SDaniel Vetter if (!drm_kms_helper_poll) 332162b6a57SDaniel Vetter goto out; 3338d754544SDaniel Vetter 3348d754544SDaniel Vetter mutex_lock(&dev->mode_config.mutex); 3356295d607SDaniel Vetter drm_for_each_connector(connector, dev) { 3368d754544SDaniel Vetter 3378d754544SDaniel Vetter /* Ignore forced connectors. */ 3388d754544SDaniel Vetter if (connector->force) 3398d754544SDaniel Vetter continue; 3408d754544SDaniel Vetter 3418d754544SDaniel Vetter /* Ignore HPD capable connectors and connectors where we don't 3428d754544SDaniel Vetter * want any hotplug detection at all for polling. */ 3438d754544SDaniel Vetter if (!connector->polled || connector->polled == DRM_CONNECTOR_POLL_HPD) 3448d754544SDaniel Vetter continue; 3458d754544SDaniel Vetter 3468d754544SDaniel Vetter old_status = connector->status; 3478d754544SDaniel Vetter /* if we are connected and don't want to poll for disconnect 3488d754544SDaniel Vetter skip it */ 3498d754544SDaniel Vetter if (old_status == connector_status_connected && 3508d754544SDaniel Vetter !(connector->polled & DRM_CONNECTOR_POLL_DISCONNECT)) 3518d754544SDaniel Vetter continue; 3528d754544SDaniel Vetter 353a3c6d686SJosef Holzmayr repoll = true; 354a3c6d686SJosef Holzmayr 3558d754544SDaniel Vetter connector->status = connector->funcs->detect(connector, false); 3568d754544SDaniel Vetter if (old_status != connector->status) { 3578d754544SDaniel Vetter const char *old, *new; 3588d754544SDaniel Vetter 359b7703726SDaniel Vetter /* 360b7703726SDaniel Vetter * The poll work sets force=false when calling detect so 361b7703726SDaniel Vetter * that drivers can avoid to do disruptive tests (e.g. 362b7703726SDaniel Vetter * when load detect cycles could cause flickering on 363b7703726SDaniel Vetter * other, running displays). This bears the risk that we 364b7703726SDaniel Vetter * flip-flop between unknown here in the poll work and 365b7703726SDaniel Vetter * the real state when userspace forces a full detect 366b7703726SDaniel Vetter * call after receiving a hotplug event due to this 367b7703726SDaniel Vetter * change. 368b7703726SDaniel Vetter * 369b7703726SDaniel Vetter * Hence clamp an unknown detect status to the old 370b7703726SDaniel Vetter * value. 371b7703726SDaniel Vetter */ 372b7703726SDaniel Vetter if (connector->status == connector_status_unknown) { 373b7703726SDaniel Vetter connector->status = old_status; 374b7703726SDaniel Vetter continue; 375b7703726SDaniel Vetter } 376b7703726SDaniel Vetter 3778d754544SDaniel Vetter old = drm_get_connector_status_name(old_status); 3788d754544SDaniel Vetter new = drm_get_connector_status_name(connector->status); 3798d754544SDaniel Vetter 3808d754544SDaniel Vetter DRM_DEBUG_KMS("[CONNECTOR:%d:%s] " 3818d754544SDaniel Vetter "status updated from %s to %s\n", 3828d754544SDaniel Vetter connector->base.id, 38325933820SJani Nikula connector->name, 3848d754544SDaniel Vetter old, new); 3858d754544SDaniel Vetter 3868d754544SDaniel Vetter changed = true; 3878d754544SDaniel Vetter } 3888d754544SDaniel Vetter } 3898d754544SDaniel Vetter 3908d754544SDaniel Vetter mutex_unlock(&dev->mode_config.mutex); 3918d754544SDaniel Vetter 392162b6a57SDaniel Vetter out: 3938d754544SDaniel Vetter if (changed) 3948d754544SDaniel Vetter drm_kms_helper_hotplug_event(dev); 3958d754544SDaniel Vetter 3968d754544SDaniel Vetter if (repoll) 3978d754544SDaniel Vetter schedule_delayed_work(delayed_work, DRM_OUTPUT_POLL_PERIOD); 3988d754544SDaniel Vetter } 3998d754544SDaniel Vetter 4008d754544SDaniel Vetter /** 4018d754544SDaniel Vetter * drm_kms_helper_poll_disable - disable output polling 4028d754544SDaniel Vetter * @dev: drm_device 4038d754544SDaniel Vetter * 4048d754544SDaniel Vetter * This function disables the output polling work. 4058d754544SDaniel Vetter * 4068d754544SDaniel Vetter * Drivers can call this helper from their device suspend implementation. It is 4078d754544SDaniel Vetter * not an error to call this even when output polling isn't enabled or arlready 4088d754544SDaniel Vetter * disabled. 4098d754544SDaniel Vetter */ 4108d754544SDaniel Vetter void drm_kms_helper_poll_disable(struct drm_device *dev) 4118d754544SDaniel Vetter { 4128d754544SDaniel Vetter if (!dev->mode_config.poll_enabled) 4138d754544SDaniel Vetter return; 4148d754544SDaniel Vetter cancel_delayed_work_sync(&dev->mode_config.output_poll_work); 4158d754544SDaniel Vetter } 4168d754544SDaniel Vetter EXPORT_SYMBOL(drm_kms_helper_poll_disable); 4178d754544SDaniel Vetter 4188d754544SDaniel Vetter /** 4198d754544SDaniel Vetter * drm_kms_helper_poll_enable - re-enable output polling. 4208d754544SDaniel Vetter * @dev: drm_device 4218d754544SDaniel Vetter * 4228d754544SDaniel Vetter * This function re-enables the output polling work. 4238d754544SDaniel Vetter * 4248d754544SDaniel Vetter * Drivers can call this helper from their device resume implementation. It is 4258d754544SDaniel Vetter * an error to call this when the output polling support has not yet been set 4268d754544SDaniel Vetter * up. 4278d754544SDaniel Vetter */ 4288d754544SDaniel Vetter void drm_kms_helper_poll_enable(struct drm_device *dev) 4298d754544SDaniel Vetter { 430*8c4ccc4aSDaniel Vetter mutex_lock(&dev->mode_config.mutex); 431*8c4ccc4aSDaniel Vetter __drm_kms_helper_poll_enable(dev); 432*8c4ccc4aSDaniel Vetter mutex_unlock(&dev->mode_config.mutex); 4338d754544SDaniel Vetter } 4348d754544SDaniel Vetter EXPORT_SYMBOL(drm_kms_helper_poll_enable); 4358d754544SDaniel Vetter 4368d754544SDaniel Vetter /** 4378d754544SDaniel Vetter * drm_kms_helper_poll_init - initialize and enable output polling 4388d754544SDaniel Vetter * @dev: drm_device 4398d754544SDaniel Vetter * 4408d754544SDaniel Vetter * This function intializes and then also enables output polling support for 4418d754544SDaniel Vetter * @dev. Drivers which do not have reliable hotplug support in hardware can use 4428d754544SDaniel Vetter * this helper infrastructure to regularly poll such connectors for changes in 4438d754544SDaniel Vetter * their connection state. 4448d754544SDaniel Vetter * 4458d754544SDaniel Vetter * Drivers can control which connectors are polled by setting the 4468d754544SDaniel Vetter * DRM_CONNECTOR_POLL_CONNECT and DRM_CONNECTOR_POLL_DISCONNECT flags. On 4478d754544SDaniel Vetter * connectors where probing live outputs can result in visual distortion drivers 4488d754544SDaniel Vetter * should not set the DRM_CONNECTOR_POLL_DISCONNECT flag to avoid this. 4498d754544SDaniel Vetter * Connectors which have no flag or only DRM_CONNECTOR_POLL_HPD set are 4508d754544SDaniel Vetter * completely ignored by the polling logic. 4518d754544SDaniel Vetter * 4528d754544SDaniel Vetter * Note that a connector can be both polled and probed from the hotplug handler, 4538d754544SDaniel Vetter * in case the hotplug interrupt is known to be unreliable. 4548d754544SDaniel Vetter */ 4558d754544SDaniel Vetter void drm_kms_helper_poll_init(struct drm_device *dev) 4568d754544SDaniel Vetter { 4578d754544SDaniel Vetter INIT_DELAYED_WORK(&dev->mode_config.output_poll_work, output_poll_execute); 4588d754544SDaniel Vetter dev->mode_config.poll_enabled = true; 4598d754544SDaniel Vetter 4608d754544SDaniel Vetter drm_kms_helper_poll_enable(dev); 4618d754544SDaniel Vetter } 4628d754544SDaniel Vetter EXPORT_SYMBOL(drm_kms_helper_poll_init); 4638d754544SDaniel Vetter 4648d754544SDaniel Vetter /** 4658d754544SDaniel Vetter * drm_kms_helper_poll_fini - disable output polling and clean it up 4668d754544SDaniel Vetter * @dev: drm_device 4678d754544SDaniel Vetter */ 4688d754544SDaniel Vetter void drm_kms_helper_poll_fini(struct drm_device *dev) 4698d754544SDaniel Vetter { 4708d754544SDaniel Vetter drm_kms_helper_poll_disable(dev); 4718d754544SDaniel Vetter } 4728d754544SDaniel Vetter EXPORT_SYMBOL(drm_kms_helper_poll_fini); 4738d754544SDaniel Vetter 4748d754544SDaniel Vetter /** 4758d754544SDaniel Vetter * drm_helper_hpd_irq_event - hotplug processing 4768d754544SDaniel Vetter * @dev: drm_device 4778d754544SDaniel Vetter * 4788d754544SDaniel Vetter * Drivers can use this helper function to run a detect cycle on all connectors 4798d754544SDaniel Vetter * which have the DRM_CONNECTOR_POLL_HPD flag set in their &polled member. All 4808d754544SDaniel Vetter * other connectors are ignored, which is useful to avoid reprobing fixed 4818d754544SDaniel Vetter * panels. 4828d754544SDaniel Vetter * 4838d754544SDaniel Vetter * This helper function is useful for drivers which can't or don't track hotplug 4848d754544SDaniel Vetter * interrupts for each connector. 4858d754544SDaniel Vetter * 4868d754544SDaniel Vetter * Drivers which support hotplug interrupts for each connector individually and 4878d754544SDaniel Vetter * which have a more fine-grained detect logic should bypass this code and 4888d754544SDaniel Vetter * directly call drm_kms_helper_hotplug_event() in case the connector state 4898d754544SDaniel Vetter * changed. 4908d754544SDaniel Vetter * 4918d754544SDaniel Vetter * This function must be called from process context with no mode 4928d754544SDaniel Vetter * setting locks held. 4938d754544SDaniel Vetter * 4948d754544SDaniel Vetter * Note that a connector can be both polled and probed from the hotplug handler, 4958d754544SDaniel Vetter * in case the hotplug interrupt is known to be unreliable. 4968d754544SDaniel Vetter */ 4978d754544SDaniel Vetter bool drm_helper_hpd_irq_event(struct drm_device *dev) 4988d754544SDaniel Vetter { 4998d754544SDaniel Vetter struct drm_connector *connector; 5008d754544SDaniel Vetter enum drm_connector_status old_status; 5018d754544SDaniel Vetter bool changed = false; 5028d754544SDaniel Vetter 5038d754544SDaniel Vetter if (!dev->mode_config.poll_enabled) 5048d754544SDaniel Vetter return false; 5058d754544SDaniel Vetter 5068d754544SDaniel Vetter mutex_lock(&dev->mode_config.mutex); 5076295d607SDaniel Vetter drm_for_each_connector(connector, dev) { 5088d754544SDaniel Vetter 5098d754544SDaniel Vetter /* Only handle HPD capable connectors. */ 5108d754544SDaniel Vetter if (!(connector->polled & DRM_CONNECTOR_POLL_HPD)) 5118d754544SDaniel Vetter continue; 5128d754544SDaniel Vetter 5138d754544SDaniel Vetter old_status = connector->status; 5148d754544SDaniel Vetter 5158d754544SDaniel Vetter connector->status = connector->funcs->detect(connector, false); 5168d754544SDaniel Vetter DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n", 5178d754544SDaniel Vetter connector->base.id, 51825933820SJani Nikula connector->name, 5198d754544SDaniel Vetter drm_get_connector_status_name(old_status), 5208d754544SDaniel Vetter drm_get_connector_status_name(connector->status)); 5218d754544SDaniel Vetter if (old_status != connector->status) 5228d754544SDaniel Vetter changed = true; 5238d754544SDaniel Vetter } 5248d754544SDaniel Vetter 5258d754544SDaniel Vetter mutex_unlock(&dev->mode_config.mutex); 5268d754544SDaniel Vetter 5278d754544SDaniel Vetter if (changed) 5288d754544SDaniel Vetter drm_kms_helper_hotplug_event(dev); 5298d754544SDaniel Vetter 5308d754544SDaniel Vetter return changed; 5318d754544SDaniel Vetter } 5328d754544SDaniel Vetter EXPORT_SYMBOL(drm_helper_hpd_irq_event); 533