Daniel Vetter | 8d75454 | 2014-04-10 10:51:11 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2006-2008 Intel Corporation |
| 3 | * Copyright (c) 2007 Dave Airlie <airlied@linux.ie> |
| 4 | * |
| 5 | * DRM core CRTC related functions |
| 6 | * |
| 7 | * Permission to use, copy, modify, distribute, and sell this software and its |
| 8 | * documentation for any purpose is hereby granted without fee, provided that |
| 9 | * the above copyright notice appear in all copies and that both that copyright |
| 10 | * notice and this permission notice appear in supporting documentation, and |
| 11 | * that the name of the copyright holders not be used in advertising or |
| 12 | * publicity pertaining to distribution of the software without specific, |
| 13 | * written prior permission. The copyright holders make no representations |
| 14 | * about the suitability of this software for any purpose. It is provided "as |
| 15 | * is" without express or implied warranty. |
| 16 | * |
| 17 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
| 18 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO |
| 19 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR |
| 20 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, |
| 21 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 22 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
| 23 | * OF THIS SOFTWARE. |
| 24 | * |
| 25 | * Authors: |
| 26 | * Keith Packard |
| 27 | * Eric Anholt <eric@anholt.net> |
| 28 | * Dave Airlie <airlied@linux.ie> |
| 29 | * Jesse Barnes <jesse.barnes@intel.com> |
| 30 | */ |
| 31 | |
| 32 | #include <linux/export.h> |
| 33 | #include <linux/moduleparam.h> |
| 34 | |
| 35 | #include <drm/drmP.h> |
| 36 | #include <drm/drm_crtc.h> |
| 37 | #include <drm/drm_fourcc.h> |
| 38 | #include <drm/drm_crtc_helper.h> |
| 39 | #include <drm/drm_fb_helper.h> |
| 40 | #include <drm/drm_edid.h> |
| 41 | |
| 42 | /** |
| 43 | * DOC: output probing helper overview |
| 44 | * |
| 45 | * This library provides some helper code for output probing. It provides an |
| 46 | * implementation of the core connector->fill_modes interface with |
| 47 | * drm_helper_probe_single_connector_modes. |
| 48 | * |
| 49 | * It also provides support for polling connectors with a work item and for |
| 50 | * generic hotplug interrupt handling where the driver doesn't or cannot keep |
| 51 | * track of a per-connector hpd interrupt. |
| 52 | * |
| 53 | * This helper library can be used independently of the modeset helper library. |
| 54 | * Drivers can also overwrite different parts e.g. use their own hotplug |
| 55 | * handling code to avoid probing unrelated outputs. |
| 56 | */ |
| 57 | |
| 58 | static bool drm_kms_helper_poll = true; |
| 59 | module_param_named(poll, drm_kms_helper_poll, bool, 0600); |
| 60 | |
Ville Syrjälä | 05acaec | 2014-12-17 13:56:22 +0200 | [diff] [blame] | 61 | static enum drm_mode_status |
| 62 | drm_mode_validate_flag(const struct drm_display_mode *mode, |
| 63 | int flags) |
Daniel Vetter | 8d75454 | 2014-04-10 10:51:11 +0200 | [diff] [blame] | 64 | { |
Ville Syrjälä | 05acaec | 2014-12-17 13:56:22 +0200 | [diff] [blame] | 65 | if ((mode->flags & DRM_MODE_FLAG_INTERLACE) && |
| 66 | !(flags & DRM_MODE_FLAG_INTERLACE)) |
| 67 | return MODE_NO_INTERLACE; |
Daniel Vetter | 8d75454 | 2014-04-10 10:51:11 +0200 | [diff] [blame] | 68 | |
Ville Syrjälä | 05acaec | 2014-12-17 13:56:22 +0200 | [diff] [blame] | 69 | if ((mode->flags & DRM_MODE_FLAG_DBLSCAN) && |
| 70 | !(flags & DRM_MODE_FLAG_DBLSCAN)) |
| 71 | return MODE_NO_DBLESCAN; |
Daniel Vetter | 8d75454 | 2014-04-10 10:51:11 +0200 | [diff] [blame] | 72 | |
Ville Syrjälä | 05acaec | 2014-12-17 13:56:22 +0200 | [diff] [blame] | 73 | if ((mode->flags & DRM_MODE_FLAG_3D_MASK) && |
| 74 | !(flags & DRM_MODE_FLAG_3D_MASK)) |
| 75 | return MODE_NO_STEREO; |
Daniel Vetter | 8d75454 | 2014-04-10 10:51:11 +0200 | [diff] [blame] | 76 | |
Ville Syrjälä | 05acaec | 2014-12-17 13:56:22 +0200 | [diff] [blame] | 77 | return MODE_OK; |
Daniel Vetter | 8d75454 | 2014-04-10 10:51:11 +0200 | [diff] [blame] | 78 | } |
| 79 | |
Chris Wilson | eaf99c7 | 2014-08-06 10:08:32 +0200 | [diff] [blame] | 80 | static int drm_helper_probe_add_cmdline_mode(struct drm_connector *connector) |
| 81 | { |
| 82 | struct drm_display_mode *mode; |
| 83 | |
| 84 | if (!connector->cmdline_mode.specified) |
| 85 | return 0; |
| 86 | |
| 87 | mode = drm_mode_create_from_cmdline_mode(connector->dev, |
| 88 | &connector->cmdline_mode); |
| 89 | if (mode == NULL) |
| 90 | return 0; |
| 91 | |
| 92 | drm_mode_probed_add(connector, mode); |
| 93 | return 1; |
| 94 | } |
| 95 | |
Daniel Vetter | 8c4ccc4 | 2015-07-09 23:44:26 +0200 | [diff] [blame] | 96 | #define DRM_OUTPUT_POLL_PERIOD (10*HZ) |
Egbert Eich | 4ad640e | 2015-09-23 16:13:00 +0200 | [diff] [blame] | 97 | /** |
| 98 | * drm_kms_helper_poll_enable_locked - re-enable output polling. |
| 99 | * @dev: drm_device |
| 100 | * |
| 101 | * This function re-enables the output polling work without |
| 102 | * locking the mode_config mutex. |
| 103 | * |
| 104 | * This is like drm_kms_helper_poll_enable() however it is to be |
| 105 | * called from a context where the mode_config mutex is locked |
| 106 | * already. |
| 107 | */ |
| 108 | void drm_kms_helper_poll_enable_locked(struct drm_device *dev) |
Daniel Vetter | 8c4ccc4 | 2015-07-09 23:44:26 +0200 | [diff] [blame] | 109 | { |
| 110 | bool poll = false; |
| 111 | struct drm_connector *connector; |
| 112 | |
| 113 | WARN_ON(!mutex_is_locked(&dev->mode_config.mutex)); |
| 114 | |
| 115 | if (!dev->mode_config.poll_enabled || !drm_kms_helper_poll) |
| 116 | return; |
| 117 | |
| 118 | drm_for_each_connector(connector, dev) { |
| 119 | if (connector->polled & (DRM_CONNECTOR_POLL_CONNECT | |
| 120 | DRM_CONNECTOR_POLL_DISCONNECT)) |
| 121 | poll = true; |
| 122 | } |
| 123 | |
| 124 | if (poll) |
| 125 | schedule_delayed_work(&dev->mode_config.output_poll_work, DRM_OUTPUT_POLL_PERIOD); |
| 126 | } |
Egbert Eich | 4ad640e | 2015-09-23 16:13:00 +0200 | [diff] [blame] | 127 | EXPORT_SYMBOL(drm_kms_helper_poll_enable_locked); |
| 128 | |
Daniel Vetter | 8c4ccc4 | 2015-07-09 23:44:26 +0200 | [diff] [blame] | 129 | |
Dave Airlie | b87577b | 2014-05-01 09:26:53 +1000 | [diff] [blame] | 130 | static int drm_helper_probe_single_connector_modes_merge_bits(struct drm_connector *connector, |
| 131 | uint32_t maxX, uint32_t maxY, bool merge_type_bits) |
Daniel Vetter | 8d75454 | 2014-04-10 10:51:11 +0200 | [diff] [blame] | 132 | { |
| 133 | struct drm_device *dev = connector->dev; |
| 134 | struct drm_display_mode *mode; |
Jani Nikula | be26a66 | 2015-03-11 11:51:06 +0200 | [diff] [blame] | 135 | const struct drm_connector_helper_funcs *connector_funcs = |
Daniel Vetter | 8d75454 | 2014-04-10 10:51:11 +0200 | [diff] [blame] | 136 | connector->helper_private; |
| 137 | int count = 0; |
| 138 | int mode_flags = 0; |
| 139 | bool verbose_prune = true; |
Daniel Vetter | 162b6a5 | 2015-01-21 08:45:21 +0100 | [diff] [blame] | 140 | enum drm_connector_status old_status; |
Daniel Vetter | 8d75454 | 2014-04-10 10:51:11 +0200 | [diff] [blame] | 141 | |
| 142 | WARN_ON(!mutex_is_locked(&dev->mode_config.mutex)); |
| 143 | |
| 144 | DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", connector->base.id, |
Jani Nikula | 2593382 | 2014-06-03 14:56:20 +0300 | [diff] [blame] | 145 | connector->name); |
Daniel Vetter | 8d75454 | 2014-04-10 10:51:11 +0200 | [diff] [blame] | 146 | /* set all modes to the unverified state */ |
| 147 | list_for_each_entry(mode, &connector->modes, head) |
| 148 | mode->status = MODE_UNVERIFIED; |
| 149 | |
Daniel Vetter | ed293f7 | 2015-11-19 17:46:50 +0100 | [diff] [blame] | 150 | old_status = connector->status; |
| 151 | |
Daniel Vetter | 8d75454 | 2014-04-10 10:51:11 +0200 | [diff] [blame] | 152 | if (connector->force) { |
Peter Hurley | 2c4cc91 | 2014-11-03 20:51:45 -0500 | [diff] [blame] | 153 | if (connector->force == DRM_FORCE_ON || |
| 154 | connector->force == DRM_FORCE_ON_DIGITAL) |
Daniel Vetter | 8d75454 | 2014-04-10 10:51:11 +0200 | [diff] [blame] | 155 | connector->status = connector_status_connected; |
| 156 | else |
| 157 | connector->status = connector_status_disconnected; |
| 158 | if (connector->funcs->force) |
| 159 | connector->funcs->force(connector); |
| 160 | } else { |
| 161 | connector->status = connector->funcs->detect(connector, true); |
Daniel Vetter | ed293f7 | 2015-11-19 17:46:50 +0100 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | /* |
| 165 | * Normally either the driver's hpd code or the poll loop should |
| 166 | * pick up any changes and fire the hotplug event. But if |
| 167 | * userspace sneaks in a probe, we might miss a change. Hence |
| 168 | * check here, and if anything changed start the hotplug code. |
| 169 | */ |
| 170 | if (old_status != connector->status) { |
Jani Nikula | 4e15f2a | 2015-12-03 14:00:03 +0200 | [diff] [blame] | 171 | DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n", |
Daniel Vetter | ed293f7 | 2015-11-19 17:46:50 +0100 | [diff] [blame] | 172 | connector->base.id, |
| 173 | connector->name, |
Jani Nikula | 4e15f2a | 2015-12-03 14:00:03 +0200 | [diff] [blame] | 174 | drm_get_connector_status_name(old_status), |
| 175 | drm_get_connector_status_name(connector->status)); |
Daniel Vetter | 162b6a5 | 2015-01-21 08:45:21 +0100 | [diff] [blame] | 176 | |
| 177 | /* |
Daniel Vetter | ed293f7 | 2015-11-19 17:46:50 +0100 | [diff] [blame] | 178 | * The hotplug event code might call into the fb |
| 179 | * helpers, and so expects that we do not hold any |
| 180 | * locks. Fire up the poll struct instead, it will |
| 181 | * disable itself again. |
Daniel Vetter | 162b6a5 | 2015-01-21 08:45:21 +0100 | [diff] [blame] | 182 | */ |
Daniel Vetter | ed293f7 | 2015-11-19 17:46:50 +0100 | [diff] [blame] | 183 | dev->mode_config.delayed_event = true; |
| 184 | if (dev->mode_config.poll_enabled) |
| 185 | schedule_delayed_work(&dev->mode_config.output_poll_work, |
| 186 | 0); |
Daniel Vetter | 8d75454 | 2014-04-10 10:51:11 +0200 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | /* Re-enable polling in case the global poll config changed. */ |
| 190 | if (drm_kms_helper_poll != dev->mode_config.poll_running) |
Egbert Eich | 4ad640e | 2015-09-23 16:13:00 +0200 | [diff] [blame] | 191 | drm_kms_helper_poll_enable_locked(dev); |
Daniel Vetter | 8d75454 | 2014-04-10 10:51:11 +0200 | [diff] [blame] | 192 | |
| 193 | dev->mode_config.poll_running = drm_kms_helper_poll; |
| 194 | |
| 195 | if (connector->status == connector_status_disconnected) { |
| 196 | DRM_DEBUG_KMS("[CONNECTOR:%d:%s] disconnected\n", |
Jani Nikula | 2593382 | 2014-06-03 14:56:20 +0300 | [diff] [blame] | 197 | connector->base.id, connector->name); |
Daniel Vetter | 8d75454 | 2014-04-10 10:51:11 +0200 | [diff] [blame] | 198 | drm_mode_connector_update_edid_property(connector, NULL); |
| 199 | verbose_prune = false; |
| 200 | goto prune; |
| 201 | } |
| 202 | |
| 203 | #ifdef CONFIG_DRM_LOAD_EDID_FIRMWARE |
| 204 | count = drm_load_edid_firmware(connector); |
| 205 | if (count == 0) |
| 206 | #endif |
Thomas Wood | 4cf2b28 | 2014-06-18 17:52:33 +0100 | [diff] [blame] | 207 | { |
| 208 | if (connector->override_edid) { |
| 209 | struct edid *edid = (struct edid *) connector->edid_blob_ptr->data; |
| 210 | |
| 211 | count = drm_add_edid_modes(connector, edid); |
Jani Nikula | ad692b4 | 2015-03-26 10:42:00 +0200 | [diff] [blame] | 212 | drm_edid_to_eld(connector, edid); |
Thomas Wood | 4cf2b28 | 2014-06-18 17:52:33 +0100 | [diff] [blame] | 213 | } else |
| 214 | count = (*connector_funcs->get_modes)(connector); |
| 215 | } |
Daniel Vetter | 8d75454 | 2014-04-10 10:51:11 +0200 | [diff] [blame] | 216 | |
| 217 | if (count == 0 && connector->status == connector_status_connected) |
| 218 | count = drm_add_modes_noedid(connector, 1024, 768); |
Chris Wilson | eaf99c7 | 2014-08-06 10:08:32 +0200 | [diff] [blame] | 219 | count += drm_helper_probe_add_cmdline_mode(connector); |
Daniel Vetter | 8d75454 | 2014-04-10 10:51:11 +0200 | [diff] [blame] | 220 | if (count == 0) |
| 221 | goto prune; |
| 222 | |
Dave Airlie | b87577b | 2014-05-01 09:26:53 +1000 | [diff] [blame] | 223 | drm_mode_connector_list_update(connector, merge_type_bits); |
Daniel Vetter | 8d75454 | 2014-04-10 10:51:11 +0200 | [diff] [blame] | 224 | |
Daniel Vetter | 8d75454 | 2014-04-10 10:51:11 +0200 | [diff] [blame] | 225 | if (connector->interlace_allowed) |
| 226 | mode_flags |= DRM_MODE_FLAG_INTERLACE; |
| 227 | if (connector->doublescan_allowed) |
| 228 | mode_flags |= DRM_MODE_FLAG_DBLSCAN; |
| 229 | if (connector->stereo_allowed) |
| 230 | mode_flags |= DRM_MODE_FLAG_3D_MASK; |
Daniel Vetter | 8d75454 | 2014-04-10 10:51:11 +0200 | [diff] [blame] | 231 | |
| 232 | list_for_each_entry(mode, &connector->modes, head) { |
Ville Syrjälä | abc0b14 | 2014-12-17 13:56:23 +0200 | [diff] [blame] | 233 | mode->status = drm_mode_validate_basic(mode); |
| 234 | |
| 235 | if (mode->status == MODE_OK) |
| 236 | mode->status = drm_mode_validate_size(mode, maxX, maxY); |
Ville Syrjälä | 05acaec | 2014-12-17 13:56:22 +0200 | [diff] [blame] | 237 | |
| 238 | if (mode->status == MODE_OK) |
| 239 | mode->status = drm_mode_validate_flag(mode, mode_flags); |
| 240 | |
Andrzej Hajda | f9b0e25 | 2014-04-02 12:29:46 +0200 | [diff] [blame] | 241 | if (mode->status == MODE_OK && connector_funcs->mode_valid) |
Daniel Vetter | 8d75454 | 2014-04-10 10:51:11 +0200 | [diff] [blame] | 242 | mode->status = connector_funcs->mode_valid(connector, |
| 243 | mode); |
| 244 | } |
| 245 | |
| 246 | prune: |
| 247 | drm_mode_prune_invalid(dev, &connector->modes, verbose_prune); |
| 248 | |
| 249 | if (list_empty(&connector->modes)) |
| 250 | return 0; |
| 251 | |
| 252 | list_for_each_entry(mode, &connector->modes, head) |
| 253 | mode->vrefresh = drm_mode_vrefresh(mode); |
| 254 | |
| 255 | drm_mode_sort(&connector->modes); |
| 256 | |
| 257 | DRM_DEBUG_KMS("[CONNECTOR:%d:%s] probed modes :\n", connector->base.id, |
Jani Nikula | 2593382 | 2014-06-03 14:56:20 +0300 | [diff] [blame] | 258 | connector->name); |
Daniel Vetter | 8d75454 | 2014-04-10 10:51:11 +0200 | [diff] [blame] | 259 | list_for_each_entry(mode, &connector->modes, head) { |
| 260 | drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V); |
| 261 | drm_mode_debug_printmodeline(mode); |
| 262 | } |
| 263 | |
| 264 | return count; |
| 265 | } |
Dave Airlie | b87577b | 2014-05-01 09:26:53 +1000 | [diff] [blame] | 266 | |
| 267 | /** |
| 268 | * drm_helper_probe_single_connector_modes - get complete set of display modes |
| 269 | * @connector: connector to probe |
| 270 | * @maxX: max width for modes |
| 271 | * @maxY: max height for modes |
| 272 | * |
| 273 | * Based on the helper callbacks implemented by @connector try to detect all |
| 274 | * valid modes. Modes will first be added to the connector's probed_modes list, |
| 275 | * then culled (based on validity and the @maxX, @maxY parameters) and put into |
| 276 | * the normal modes list. |
| 277 | * |
| 278 | * Intended to be use as a generic implementation of the ->fill_modes() |
| 279 | * @connector vfunc for drivers that use the crtc helpers for output mode |
| 280 | * filtering and detection. |
| 281 | * |
| 282 | * Returns: |
| 283 | * The number of modes found on @connector. |
| 284 | */ |
| 285 | int drm_helper_probe_single_connector_modes(struct drm_connector *connector, |
| 286 | uint32_t maxX, uint32_t maxY) |
| 287 | { |
| 288 | return drm_helper_probe_single_connector_modes_merge_bits(connector, maxX, maxY, true); |
| 289 | } |
Daniel Vetter | 8d75454 | 2014-04-10 10:51:11 +0200 | [diff] [blame] | 290 | EXPORT_SYMBOL(drm_helper_probe_single_connector_modes); |
| 291 | |
| 292 | /** |
Dave Airlie | b87577b | 2014-05-01 09:26:53 +1000 | [diff] [blame] | 293 | * drm_helper_probe_single_connector_modes_nomerge - get complete set of display modes |
| 294 | * @connector: connector to probe |
| 295 | * @maxX: max width for modes |
| 296 | * @maxY: max height for modes |
| 297 | * |
| 298 | * This operates like drm_hehlper_probe_single_connector_modes except it |
| 299 | * replaces the mode bits instead of merging them for preferred modes. |
| 300 | */ |
| 301 | int drm_helper_probe_single_connector_modes_nomerge(struct drm_connector *connector, |
| 302 | uint32_t maxX, uint32_t maxY) |
| 303 | { |
| 304 | return drm_helper_probe_single_connector_modes_merge_bits(connector, maxX, maxY, false); |
| 305 | } |
| 306 | EXPORT_SYMBOL(drm_helper_probe_single_connector_modes_nomerge); |
| 307 | |
| 308 | /** |
Daniel Vetter | 8d75454 | 2014-04-10 10:51:11 +0200 | [diff] [blame] | 309 | * drm_kms_helper_hotplug_event - fire off KMS hotplug events |
| 310 | * @dev: drm_device whose connector state changed |
| 311 | * |
| 312 | * This function fires off the uevent for userspace and also calls the |
| 313 | * output_poll_changed function, which is most commonly used to inform the fbdev |
| 314 | * emulation code and allow it to update the fbcon output configuration. |
| 315 | * |
| 316 | * Drivers should call this from their hotplug handling code when a change is |
| 317 | * detected. Note that this function does not do any output detection of its |
| 318 | * own, like drm_helper_hpd_irq_event() does - this is assumed to be done by the |
| 319 | * driver already. |
| 320 | * |
| 321 | * This function must be called from process context with no mode |
| 322 | * setting locks held. |
| 323 | */ |
| 324 | void drm_kms_helper_hotplug_event(struct drm_device *dev) |
| 325 | { |
| 326 | /* send a uevent + call fbdev */ |
| 327 | drm_sysfs_hotplug_event(dev); |
| 328 | if (dev->mode_config.funcs->output_poll_changed) |
| 329 | dev->mode_config.funcs->output_poll_changed(dev); |
| 330 | } |
| 331 | EXPORT_SYMBOL(drm_kms_helper_hotplug_event); |
| 332 | |
Daniel Vetter | 8d75454 | 2014-04-10 10:51:11 +0200 | [diff] [blame] | 333 | static void output_poll_execute(struct work_struct *work) |
| 334 | { |
| 335 | struct delayed_work *delayed_work = to_delayed_work(work); |
| 336 | struct drm_device *dev = container_of(delayed_work, struct drm_device, mode_config.output_poll_work); |
| 337 | struct drm_connector *connector; |
| 338 | enum drm_connector_status old_status; |
Daniel Vetter | 162b6a5 | 2015-01-21 08:45:21 +0100 | [diff] [blame] | 339 | bool repoll = false, changed; |
| 340 | |
| 341 | /* Pick up any changes detected by the probe functions. */ |
| 342 | changed = dev->mode_config.delayed_event; |
| 343 | dev->mode_config.delayed_event = false; |
Daniel Vetter | 8d75454 | 2014-04-10 10:51:11 +0200 | [diff] [blame] | 344 | |
| 345 | if (!drm_kms_helper_poll) |
Daniel Vetter | 162b6a5 | 2015-01-21 08:45:21 +0100 | [diff] [blame] | 346 | goto out; |
Daniel Vetter | 8d75454 | 2014-04-10 10:51:11 +0200 | [diff] [blame] | 347 | |
| 348 | mutex_lock(&dev->mode_config.mutex); |
Daniel Vetter | 6295d60 | 2015-07-09 23:44:25 +0200 | [diff] [blame] | 349 | drm_for_each_connector(connector, dev) { |
Daniel Vetter | 8d75454 | 2014-04-10 10:51:11 +0200 | [diff] [blame] | 350 | |
| 351 | /* Ignore forced connectors. */ |
| 352 | if (connector->force) |
| 353 | continue; |
| 354 | |
| 355 | /* Ignore HPD capable connectors and connectors where we don't |
| 356 | * want any hotplug detection at all for polling. */ |
| 357 | if (!connector->polled || connector->polled == DRM_CONNECTOR_POLL_HPD) |
| 358 | continue; |
| 359 | |
Daniel Vetter | 8d75454 | 2014-04-10 10:51:11 +0200 | [diff] [blame] | 360 | old_status = connector->status; |
| 361 | /* if we are connected and don't want to poll for disconnect |
| 362 | skip it */ |
| 363 | if (old_status == connector_status_connected && |
| 364 | !(connector->polled & DRM_CONNECTOR_POLL_DISCONNECT)) |
| 365 | continue; |
| 366 | |
Josef Holzmayr | a3c6d68 | 2015-04-16 14:16:29 +0200 | [diff] [blame] | 367 | repoll = true; |
| 368 | |
Daniel Vetter | 8d75454 | 2014-04-10 10:51:11 +0200 | [diff] [blame] | 369 | connector->status = connector->funcs->detect(connector, false); |
| 370 | if (old_status != connector->status) { |
| 371 | const char *old, *new; |
| 372 | |
Daniel Vetter | b770372 | 2015-01-21 08:45:22 +0100 | [diff] [blame] | 373 | /* |
| 374 | * The poll work sets force=false when calling detect so |
| 375 | * that drivers can avoid to do disruptive tests (e.g. |
| 376 | * when load detect cycles could cause flickering on |
| 377 | * other, running displays). This bears the risk that we |
| 378 | * flip-flop between unknown here in the poll work and |
| 379 | * the real state when userspace forces a full detect |
| 380 | * call after receiving a hotplug event due to this |
| 381 | * change. |
| 382 | * |
| 383 | * Hence clamp an unknown detect status to the old |
| 384 | * value. |
| 385 | */ |
| 386 | if (connector->status == connector_status_unknown) { |
| 387 | connector->status = old_status; |
| 388 | continue; |
| 389 | } |
| 390 | |
Daniel Vetter | 8d75454 | 2014-04-10 10:51:11 +0200 | [diff] [blame] | 391 | old = drm_get_connector_status_name(old_status); |
| 392 | new = drm_get_connector_status_name(connector->status); |
| 393 | |
| 394 | DRM_DEBUG_KMS("[CONNECTOR:%d:%s] " |
| 395 | "status updated from %s to %s\n", |
| 396 | connector->base.id, |
Jani Nikula | 2593382 | 2014-06-03 14:56:20 +0300 | [diff] [blame] | 397 | connector->name, |
Daniel Vetter | 8d75454 | 2014-04-10 10:51:11 +0200 | [diff] [blame] | 398 | old, new); |
| 399 | |
| 400 | changed = true; |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | mutex_unlock(&dev->mode_config.mutex); |
| 405 | |
Daniel Vetter | 162b6a5 | 2015-01-21 08:45:21 +0100 | [diff] [blame] | 406 | out: |
Daniel Vetter | 8d75454 | 2014-04-10 10:51:11 +0200 | [diff] [blame] | 407 | if (changed) |
| 408 | drm_kms_helper_hotplug_event(dev); |
| 409 | |
| 410 | if (repoll) |
| 411 | schedule_delayed_work(delayed_work, DRM_OUTPUT_POLL_PERIOD); |
| 412 | } |
| 413 | |
| 414 | /** |
| 415 | * drm_kms_helper_poll_disable - disable output polling |
| 416 | * @dev: drm_device |
| 417 | * |
| 418 | * This function disables the output polling work. |
| 419 | * |
| 420 | * Drivers can call this helper from their device suspend implementation. It is |
| 421 | * not an error to call this even when output polling isn't enabled or arlready |
| 422 | * disabled. |
| 423 | */ |
| 424 | void drm_kms_helper_poll_disable(struct drm_device *dev) |
| 425 | { |
| 426 | if (!dev->mode_config.poll_enabled) |
| 427 | return; |
| 428 | cancel_delayed_work_sync(&dev->mode_config.output_poll_work); |
| 429 | } |
| 430 | EXPORT_SYMBOL(drm_kms_helper_poll_disable); |
| 431 | |
| 432 | /** |
| 433 | * drm_kms_helper_poll_enable - re-enable output polling. |
| 434 | * @dev: drm_device |
| 435 | * |
| 436 | * This function re-enables the output polling work. |
| 437 | * |
| 438 | * Drivers can call this helper from their device resume implementation. It is |
| 439 | * an error to call this when the output polling support has not yet been set |
| 440 | * up. |
| 441 | */ |
| 442 | void drm_kms_helper_poll_enable(struct drm_device *dev) |
| 443 | { |
Daniel Vetter | 8c4ccc4 | 2015-07-09 23:44:26 +0200 | [diff] [blame] | 444 | mutex_lock(&dev->mode_config.mutex); |
Egbert Eich | 4ad640e | 2015-09-23 16:13:00 +0200 | [diff] [blame] | 445 | drm_kms_helper_poll_enable_locked(dev); |
Daniel Vetter | 8c4ccc4 | 2015-07-09 23:44:26 +0200 | [diff] [blame] | 446 | mutex_unlock(&dev->mode_config.mutex); |
Daniel Vetter | 8d75454 | 2014-04-10 10:51:11 +0200 | [diff] [blame] | 447 | } |
| 448 | EXPORT_SYMBOL(drm_kms_helper_poll_enable); |
| 449 | |
| 450 | /** |
| 451 | * drm_kms_helper_poll_init - initialize and enable output polling |
| 452 | * @dev: drm_device |
| 453 | * |
| 454 | * This function intializes and then also enables output polling support for |
| 455 | * @dev. Drivers which do not have reliable hotplug support in hardware can use |
| 456 | * this helper infrastructure to regularly poll such connectors for changes in |
| 457 | * their connection state. |
| 458 | * |
| 459 | * Drivers can control which connectors are polled by setting the |
| 460 | * DRM_CONNECTOR_POLL_CONNECT and DRM_CONNECTOR_POLL_DISCONNECT flags. On |
| 461 | * connectors where probing live outputs can result in visual distortion drivers |
| 462 | * should not set the DRM_CONNECTOR_POLL_DISCONNECT flag to avoid this. |
| 463 | * Connectors which have no flag or only DRM_CONNECTOR_POLL_HPD set are |
| 464 | * completely ignored by the polling logic. |
| 465 | * |
| 466 | * Note that a connector can be both polled and probed from the hotplug handler, |
| 467 | * in case the hotplug interrupt is known to be unreliable. |
| 468 | */ |
| 469 | void drm_kms_helper_poll_init(struct drm_device *dev) |
| 470 | { |
| 471 | INIT_DELAYED_WORK(&dev->mode_config.output_poll_work, output_poll_execute); |
| 472 | dev->mode_config.poll_enabled = true; |
| 473 | |
| 474 | drm_kms_helper_poll_enable(dev); |
| 475 | } |
| 476 | EXPORT_SYMBOL(drm_kms_helper_poll_init); |
| 477 | |
| 478 | /** |
| 479 | * drm_kms_helper_poll_fini - disable output polling and clean it up |
| 480 | * @dev: drm_device |
| 481 | */ |
| 482 | void drm_kms_helper_poll_fini(struct drm_device *dev) |
| 483 | { |
| 484 | drm_kms_helper_poll_disable(dev); |
| 485 | } |
| 486 | EXPORT_SYMBOL(drm_kms_helper_poll_fini); |
| 487 | |
| 488 | /** |
| 489 | * drm_helper_hpd_irq_event - hotplug processing |
| 490 | * @dev: drm_device |
| 491 | * |
| 492 | * Drivers can use this helper function to run a detect cycle on all connectors |
| 493 | * which have the DRM_CONNECTOR_POLL_HPD flag set in their &polled member. All |
| 494 | * other connectors are ignored, which is useful to avoid reprobing fixed |
| 495 | * panels. |
| 496 | * |
| 497 | * This helper function is useful for drivers which can't or don't track hotplug |
| 498 | * interrupts for each connector. |
| 499 | * |
| 500 | * Drivers which support hotplug interrupts for each connector individually and |
| 501 | * which have a more fine-grained detect logic should bypass this code and |
| 502 | * directly call drm_kms_helper_hotplug_event() in case the connector state |
| 503 | * changed. |
| 504 | * |
| 505 | * This function must be called from process context with no mode |
| 506 | * setting locks held. |
| 507 | * |
| 508 | * Note that a connector can be both polled and probed from the hotplug handler, |
| 509 | * in case the hotplug interrupt is known to be unreliable. |
| 510 | */ |
| 511 | bool drm_helper_hpd_irq_event(struct drm_device *dev) |
| 512 | { |
| 513 | struct drm_connector *connector; |
| 514 | enum drm_connector_status old_status; |
| 515 | bool changed = false; |
| 516 | |
| 517 | if (!dev->mode_config.poll_enabled) |
| 518 | return false; |
| 519 | |
| 520 | mutex_lock(&dev->mode_config.mutex); |
Daniel Vetter | 6295d60 | 2015-07-09 23:44:25 +0200 | [diff] [blame] | 521 | drm_for_each_connector(connector, dev) { |
Daniel Vetter | 8d75454 | 2014-04-10 10:51:11 +0200 | [diff] [blame] | 522 | |
| 523 | /* Only handle HPD capable connectors. */ |
| 524 | if (!(connector->polled & DRM_CONNECTOR_POLL_HPD)) |
| 525 | continue; |
| 526 | |
| 527 | old_status = connector->status; |
| 528 | |
| 529 | connector->status = connector->funcs->detect(connector, false); |
| 530 | DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n", |
| 531 | connector->base.id, |
Jani Nikula | 2593382 | 2014-06-03 14:56:20 +0300 | [diff] [blame] | 532 | connector->name, |
Daniel Vetter | 8d75454 | 2014-04-10 10:51:11 +0200 | [diff] [blame] | 533 | drm_get_connector_status_name(old_status), |
| 534 | drm_get_connector_status_name(connector->status)); |
| 535 | if (old_status != connector->status) |
| 536 | changed = true; |
| 537 | } |
| 538 | |
| 539 | mutex_unlock(&dev->mode_config.mutex); |
| 540 | |
| 541 | if (changed) |
| 542 | drm_kms_helper_hotplug_event(dev); |
| 543 | |
| 544 | return changed; |
| 545 | } |
| 546 | EXPORT_SYMBOL(drm_helper_hpd_irq_event); |