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