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 61 static void drm_mode_validate_flag(struct drm_connector *connector, 62 int flags) 63 { 64 struct drm_display_mode *mode; 65 66 if (flags == (DRM_MODE_FLAG_DBLSCAN | DRM_MODE_FLAG_INTERLACE | 67 DRM_MODE_FLAG_3D_MASK)) 68 return; 69 70 list_for_each_entry(mode, &connector->modes, head) { 71 if ((mode->flags & DRM_MODE_FLAG_INTERLACE) && 72 !(flags & DRM_MODE_FLAG_INTERLACE)) 73 mode->status = MODE_NO_INTERLACE; 74 if ((mode->flags & DRM_MODE_FLAG_DBLSCAN) && 75 !(flags & DRM_MODE_FLAG_DBLSCAN)) 76 mode->status = MODE_NO_DBLESCAN; 77 if ((mode->flags & DRM_MODE_FLAG_3D_MASK) && 78 !(flags & DRM_MODE_FLAG_3D_MASK)) 79 mode->status = MODE_NO_STEREO; 80 } 81 82 return; 83 } 84 85 static int drm_helper_probe_add_cmdline_mode(struct drm_connector *connector) 86 { 87 struct drm_display_mode *mode; 88 89 if (!connector->cmdline_mode.specified) 90 return 0; 91 92 mode = drm_mode_create_from_cmdline_mode(connector->dev, 93 &connector->cmdline_mode); 94 if (mode == NULL) 95 return 0; 96 97 drm_mode_probed_add(connector, mode); 98 return 1; 99 } 100 101 static int drm_helper_probe_single_connector_modes_merge_bits(struct drm_connector *connector, 102 uint32_t maxX, uint32_t maxY, bool merge_type_bits) 103 { 104 struct drm_device *dev = connector->dev; 105 struct drm_display_mode *mode; 106 struct drm_connector_helper_funcs *connector_funcs = 107 connector->helper_private; 108 int count = 0; 109 int mode_flags = 0; 110 bool verbose_prune = true; 111 112 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex)); 113 114 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", connector->base.id, 115 connector->name); 116 /* set all modes to the unverified state */ 117 list_for_each_entry(mode, &connector->modes, head) 118 mode->status = MODE_UNVERIFIED; 119 120 if (connector->force) { 121 if (connector->force == DRM_FORCE_ON) 122 connector->status = connector_status_connected; 123 else 124 connector->status = connector_status_disconnected; 125 if (connector->funcs->force) 126 connector->funcs->force(connector); 127 } else { 128 connector->status = connector->funcs->detect(connector, true); 129 } 130 131 /* Re-enable polling in case the global poll config changed. */ 132 if (drm_kms_helper_poll != dev->mode_config.poll_running) 133 drm_kms_helper_poll_enable(dev); 134 135 dev->mode_config.poll_running = drm_kms_helper_poll; 136 137 if (connector->status == connector_status_disconnected) { 138 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] disconnected\n", 139 connector->base.id, connector->name); 140 drm_mode_connector_update_edid_property(connector, NULL); 141 verbose_prune = false; 142 goto prune; 143 } 144 145 #ifdef CONFIG_DRM_LOAD_EDID_FIRMWARE 146 count = drm_load_edid_firmware(connector); 147 if (count == 0) 148 #endif 149 { 150 if (connector->override_edid) { 151 struct edid *edid = (struct edid *) connector->edid_blob_ptr->data; 152 153 count = drm_add_edid_modes(connector, edid); 154 } else 155 count = (*connector_funcs->get_modes)(connector); 156 } 157 158 if (count == 0 && connector->status == connector_status_connected) 159 count = drm_add_modes_noedid(connector, 1024, 768); 160 count += drm_helper_probe_add_cmdline_mode(connector); 161 if (count == 0) 162 goto prune; 163 164 drm_mode_connector_list_update(connector, merge_type_bits); 165 166 if (maxX && maxY) 167 drm_mode_validate_size(dev, &connector->modes, maxX, maxY); 168 169 if (connector->interlace_allowed) 170 mode_flags |= DRM_MODE_FLAG_INTERLACE; 171 if (connector->doublescan_allowed) 172 mode_flags |= DRM_MODE_FLAG_DBLSCAN; 173 if (connector->stereo_allowed) 174 mode_flags |= DRM_MODE_FLAG_3D_MASK; 175 drm_mode_validate_flag(connector, mode_flags); 176 177 list_for_each_entry(mode, &connector->modes, head) { 178 if (mode->status == MODE_OK && connector_funcs->mode_valid) 179 mode->status = connector_funcs->mode_valid(connector, 180 mode); 181 } 182 183 prune: 184 drm_mode_prune_invalid(dev, &connector->modes, verbose_prune); 185 186 if (list_empty(&connector->modes)) 187 return 0; 188 189 list_for_each_entry(mode, &connector->modes, head) 190 mode->vrefresh = drm_mode_vrefresh(mode); 191 192 drm_mode_sort(&connector->modes); 193 194 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] probed modes :\n", connector->base.id, 195 connector->name); 196 list_for_each_entry(mode, &connector->modes, head) { 197 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V); 198 drm_mode_debug_printmodeline(mode); 199 } 200 201 return count; 202 } 203 204 /** 205 * drm_helper_probe_single_connector_modes - get complete set of display modes 206 * @connector: connector to probe 207 * @maxX: max width for modes 208 * @maxY: max height for modes 209 * 210 * Based on the helper callbacks implemented by @connector try to detect all 211 * valid modes. Modes will first be added to the connector's probed_modes list, 212 * then culled (based on validity and the @maxX, @maxY parameters) and put into 213 * the normal modes list. 214 * 215 * Intended to be use as a generic implementation of the ->fill_modes() 216 * @connector vfunc for drivers that use the crtc helpers for output mode 217 * filtering and detection. 218 * 219 * Returns: 220 * The number of modes found on @connector. 221 */ 222 int drm_helper_probe_single_connector_modes(struct drm_connector *connector, 223 uint32_t maxX, uint32_t maxY) 224 { 225 return drm_helper_probe_single_connector_modes_merge_bits(connector, maxX, maxY, true); 226 } 227 EXPORT_SYMBOL(drm_helper_probe_single_connector_modes); 228 229 /** 230 * drm_helper_probe_single_connector_modes_nomerge - get complete set of display modes 231 * @connector: connector to probe 232 * @maxX: max width for modes 233 * @maxY: max height for modes 234 * 235 * This operates like drm_hehlper_probe_single_connector_modes except it 236 * replaces the mode bits instead of merging them for preferred modes. 237 */ 238 int drm_helper_probe_single_connector_modes_nomerge(struct drm_connector *connector, 239 uint32_t maxX, uint32_t maxY) 240 { 241 return drm_helper_probe_single_connector_modes_merge_bits(connector, maxX, maxY, false); 242 } 243 EXPORT_SYMBOL(drm_helper_probe_single_connector_modes_nomerge); 244 245 /** 246 * drm_kms_helper_hotplug_event - fire off KMS hotplug events 247 * @dev: drm_device whose connector state changed 248 * 249 * This function fires off the uevent for userspace and also calls the 250 * output_poll_changed function, which is most commonly used to inform the fbdev 251 * emulation code and allow it to update the fbcon output configuration. 252 * 253 * Drivers should call this from their hotplug handling code when a change is 254 * detected. Note that this function does not do any output detection of its 255 * own, like drm_helper_hpd_irq_event() does - this is assumed to be done by the 256 * driver already. 257 * 258 * This function must be called from process context with no mode 259 * setting locks held. 260 */ 261 void drm_kms_helper_hotplug_event(struct drm_device *dev) 262 { 263 /* send a uevent + call fbdev */ 264 drm_sysfs_hotplug_event(dev); 265 if (dev->mode_config.funcs->output_poll_changed) 266 dev->mode_config.funcs->output_poll_changed(dev); 267 } 268 EXPORT_SYMBOL(drm_kms_helper_hotplug_event); 269 270 #define DRM_OUTPUT_POLL_PERIOD (10*HZ) 271 static void output_poll_execute(struct work_struct *work) 272 { 273 struct delayed_work *delayed_work = to_delayed_work(work); 274 struct drm_device *dev = container_of(delayed_work, struct drm_device, mode_config.output_poll_work); 275 struct drm_connector *connector; 276 enum drm_connector_status old_status; 277 bool repoll = false, changed = false; 278 279 if (!drm_kms_helper_poll) 280 return; 281 282 mutex_lock(&dev->mode_config.mutex); 283 list_for_each_entry(connector, &dev->mode_config.connector_list, head) { 284 285 /* Ignore forced connectors. */ 286 if (connector->force) 287 continue; 288 289 /* Ignore HPD capable connectors and connectors where we don't 290 * want any hotplug detection at all for polling. */ 291 if (!connector->polled || connector->polled == DRM_CONNECTOR_POLL_HPD) 292 continue; 293 294 repoll = true; 295 296 old_status = connector->status; 297 /* if we are connected and don't want to poll for disconnect 298 skip it */ 299 if (old_status == connector_status_connected && 300 !(connector->polled & DRM_CONNECTOR_POLL_DISCONNECT)) 301 continue; 302 303 connector->status = connector->funcs->detect(connector, false); 304 if (old_status != connector->status) { 305 const char *old, *new; 306 307 old = drm_get_connector_status_name(old_status); 308 new = drm_get_connector_status_name(connector->status); 309 310 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] " 311 "status updated from %s to %s\n", 312 connector->base.id, 313 connector->name, 314 old, new); 315 316 changed = true; 317 } 318 } 319 320 mutex_unlock(&dev->mode_config.mutex); 321 322 if (changed) 323 drm_kms_helper_hotplug_event(dev); 324 325 if (repoll) 326 schedule_delayed_work(delayed_work, DRM_OUTPUT_POLL_PERIOD); 327 } 328 329 /** 330 * drm_kms_helper_poll_disable - disable output polling 331 * @dev: drm_device 332 * 333 * This function disables the output polling work. 334 * 335 * Drivers can call this helper from their device suspend implementation. It is 336 * not an error to call this even when output polling isn't enabled or arlready 337 * disabled. 338 */ 339 void drm_kms_helper_poll_disable(struct drm_device *dev) 340 { 341 if (!dev->mode_config.poll_enabled) 342 return; 343 cancel_delayed_work_sync(&dev->mode_config.output_poll_work); 344 } 345 EXPORT_SYMBOL(drm_kms_helper_poll_disable); 346 347 /** 348 * drm_kms_helper_poll_enable - re-enable output polling. 349 * @dev: drm_device 350 * 351 * This function re-enables the output polling work. 352 * 353 * Drivers can call this helper from their device resume implementation. It is 354 * an error to call this when the output polling support has not yet been set 355 * up. 356 */ 357 void drm_kms_helper_poll_enable(struct drm_device *dev) 358 { 359 bool poll = false; 360 struct drm_connector *connector; 361 362 if (!dev->mode_config.poll_enabled || !drm_kms_helper_poll) 363 return; 364 365 list_for_each_entry(connector, &dev->mode_config.connector_list, head) { 366 if (connector->polled & (DRM_CONNECTOR_POLL_CONNECT | 367 DRM_CONNECTOR_POLL_DISCONNECT)) 368 poll = true; 369 } 370 371 if (poll) 372 schedule_delayed_work(&dev->mode_config.output_poll_work, DRM_OUTPUT_POLL_PERIOD); 373 } 374 EXPORT_SYMBOL(drm_kms_helper_poll_enable); 375 376 /** 377 * drm_kms_helper_poll_init - initialize and enable output polling 378 * @dev: drm_device 379 * 380 * This function intializes and then also enables output polling support for 381 * @dev. Drivers which do not have reliable hotplug support in hardware can use 382 * this helper infrastructure to regularly poll such connectors for changes in 383 * their connection state. 384 * 385 * Drivers can control which connectors are polled by setting the 386 * DRM_CONNECTOR_POLL_CONNECT and DRM_CONNECTOR_POLL_DISCONNECT flags. On 387 * connectors where probing live outputs can result in visual distortion drivers 388 * should not set the DRM_CONNECTOR_POLL_DISCONNECT flag to avoid this. 389 * Connectors which have no flag or only DRM_CONNECTOR_POLL_HPD set are 390 * completely ignored by the polling logic. 391 * 392 * Note that a connector can be both polled and probed from the hotplug handler, 393 * in case the hotplug interrupt is known to be unreliable. 394 */ 395 void drm_kms_helper_poll_init(struct drm_device *dev) 396 { 397 INIT_DELAYED_WORK(&dev->mode_config.output_poll_work, output_poll_execute); 398 dev->mode_config.poll_enabled = true; 399 400 drm_kms_helper_poll_enable(dev); 401 } 402 EXPORT_SYMBOL(drm_kms_helper_poll_init); 403 404 /** 405 * drm_kms_helper_poll_fini - disable output polling and clean it up 406 * @dev: drm_device 407 */ 408 void drm_kms_helper_poll_fini(struct drm_device *dev) 409 { 410 drm_kms_helper_poll_disable(dev); 411 } 412 EXPORT_SYMBOL(drm_kms_helper_poll_fini); 413 414 /** 415 * drm_helper_hpd_irq_event - hotplug processing 416 * @dev: drm_device 417 * 418 * Drivers can use this helper function to run a detect cycle on all connectors 419 * which have the DRM_CONNECTOR_POLL_HPD flag set in their &polled member. All 420 * other connectors are ignored, which is useful to avoid reprobing fixed 421 * panels. 422 * 423 * This helper function is useful for drivers which can't or don't track hotplug 424 * interrupts for each connector. 425 * 426 * Drivers which support hotplug interrupts for each connector individually and 427 * which have a more fine-grained detect logic should bypass this code and 428 * directly call drm_kms_helper_hotplug_event() in case the connector state 429 * changed. 430 * 431 * This function must be called from process context with no mode 432 * setting locks held. 433 * 434 * Note that a connector can be both polled and probed from the hotplug handler, 435 * in case the hotplug interrupt is known to be unreliable. 436 */ 437 bool drm_helper_hpd_irq_event(struct drm_device *dev) 438 { 439 struct drm_connector *connector; 440 enum drm_connector_status old_status; 441 bool changed = false; 442 443 if (!dev->mode_config.poll_enabled) 444 return false; 445 446 mutex_lock(&dev->mode_config.mutex); 447 list_for_each_entry(connector, &dev->mode_config.connector_list, head) { 448 449 /* Only handle HPD capable connectors. */ 450 if (!(connector->polled & DRM_CONNECTOR_POLL_HPD)) 451 continue; 452 453 old_status = connector->status; 454 455 connector->status = connector->funcs->detect(connector, false); 456 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n", 457 connector->base.id, 458 connector->name, 459 drm_get_connector_status_name(old_status), 460 drm_get_connector_status_name(connector->status)); 461 if (old_status != connector->status) 462 changed = true; 463 } 464 465 mutex_unlock(&dev->mode_config.mutex); 466 467 if (changed) 468 drm_kms_helper_hotplug_event(dev); 469 470 return changed; 471 } 472 EXPORT_SYMBOL(drm_helper_hpd_irq_event); 473