2 * Copyright (c) 2006-2008 Intel Corporation
3 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
5 * DRM core CRTC related functions
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.
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
27 * Eric Anholt <eric@anholt.net>
28 * Dave Airlie <airlied@linux.ie>
29 * Jesse Barnes <jesse.barnes@intel.com>
32 #include <linux/export.h>
33 #include <linux/moduleparam.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>
43 * DOC: output probing helper overview
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.
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.
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.
58 static bool drm_kms_helper_poll = true;
59 module_param_named(poll, drm_kms_helper_poll, bool, 0600);
61 static enum drm_mode_status
62 drm_mode_validate_flag(const struct drm_display_mode *mode,
65 if ((mode->flags & DRM_MODE_FLAG_INTERLACE) &&
66 !(flags & DRM_MODE_FLAG_INTERLACE))
67 return MODE_NO_INTERLACE;
69 if ((mode->flags & DRM_MODE_FLAG_DBLSCAN) &&
70 !(flags & DRM_MODE_FLAG_DBLSCAN))
71 return MODE_NO_DBLESCAN;
73 if ((mode->flags & DRM_MODE_FLAG_3D_MASK) &&
74 !(flags & DRM_MODE_FLAG_3D_MASK))
75 return MODE_NO_STEREO;
80 static int drm_helper_probe_add_cmdline_mode(struct drm_connector *connector)
82 struct drm_display_mode *mode;
84 if (!connector->cmdline_mode.specified)
87 mode = drm_mode_create_from_cmdline_mode(connector->dev,
88 &connector->cmdline_mode);
92 drm_mode_probed_add(connector, mode);
96 #define DRM_OUTPUT_POLL_PERIOD (10*HZ)
98 * drm_kms_helper_poll_enable_locked - re-enable output polling.
101 * This function re-enables the output polling work without
102 * locking the mode_config mutex.
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
108 void drm_kms_helper_poll_enable_locked(struct drm_device *dev)
111 struct drm_connector *connector;
113 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
115 if (!dev->mode_config.poll_enabled || !drm_kms_helper_poll)
118 drm_for_each_connector(connector, dev) {
119 if (connector->polled & (DRM_CONNECTOR_POLL_CONNECT |
120 DRM_CONNECTOR_POLL_DISCONNECT))
125 schedule_delayed_work(&dev->mode_config.output_poll_work, DRM_OUTPUT_POLL_PERIOD);
127 EXPORT_SYMBOL(drm_kms_helper_poll_enable_locked);
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)
133 struct drm_device *dev = connector->dev;
134 struct drm_display_mode *mode;
135 const struct drm_connector_helper_funcs *connector_funcs =
136 connector->helper_private;
139 bool verbose_prune = true;
140 enum drm_connector_status old_status;
142 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
144 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", connector->base.id,
146 /* set all modes to the unverified state */
147 list_for_each_entry(mode, &connector->modes, head)
148 mode->status = MODE_UNVERIFIED;
150 if (connector->force) {
151 if (connector->force == DRM_FORCE_ON ||
152 connector->force == DRM_FORCE_ON_DIGITAL)
153 connector->status = connector_status_connected;
155 connector->status = connector_status_disconnected;
156 if (connector->funcs->force)
157 connector->funcs->force(connector);
159 old_status = connector->status;
161 connector->status = connector->funcs->detect(connector, true);
164 * Normally either the driver's hpd code or the poll loop should
165 * pick up any changes and fire the hotplug event. But if
166 * userspace sneaks in a probe, we might miss a change. Hence
167 * check here, and if anything changed start the hotplug code.
169 if (old_status != connector->status) {
170 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %d to %d\n",
173 old_status, connector->status);
176 * The hotplug event code might call into the fb
177 * helpers, and so expects that we do not hold any
178 * locks. Fire up the poll struct instead, it will
179 * disable itself again.
181 dev->mode_config.delayed_event = true;
182 if (dev->mode_config.poll_enabled)
183 schedule_delayed_work(&dev->mode_config.output_poll_work,
188 /* Re-enable polling in case the global poll config changed. */
189 if (drm_kms_helper_poll != dev->mode_config.poll_running)
190 drm_kms_helper_poll_enable_locked(dev);
192 dev->mode_config.poll_running = drm_kms_helper_poll;
194 if (connector->status == connector_status_disconnected) {
195 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] disconnected\n",
196 connector->base.id, connector->name);
197 drm_mode_connector_update_edid_property(connector, NULL);
198 verbose_prune = false;
202 count = (*connector_funcs->get_modes)(connector);
204 if (count == 0 && connector->status == connector_status_connected)
205 count = drm_add_modes_noedid(connector, 1024, 768);
206 count += drm_helper_probe_add_cmdline_mode(connector);
210 drm_mode_connector_list_update(connector, merge_type_bits);
212 if (connector->interlace_allowed)
213 mode_flags |= DRM_MODE_FLAG_INTERLACE;
214 if (connector->doublescan_allowed)
215 mode_flags |= DRM_MODE_FLAG_DBLSCAN;
216 if (connector->stereo_allowed)
217 mode_flags |= DRM_MODE_FLAG_3D_MASK;
219 list_for_each_entry(mode, &connector->modes, head) {
220 if (mode->status == MODE_OK)
221 mode->status = drm_mode_validate_basic(mode);
223 if (mode->status == MODE_OK)
224 mode->status = drm_mode_validate_size(mode, maxX, maxY);
226 if (mode->status == MODE_OK)
227 mode->status = drm_mode_validate_flag(mode, mode_flags);
229 if (mode->status == MODE_OK && connector_funcs->mode_valid)
230 mode->status = connector_funcs->mode_valid(connector,
235 drm_mode_prune_invalid(dev, &connector->modes, verbose_prune);
237 if (list_empty(&connector->modes))
240 list_for_each_entry(mode, &connector->modes, head)
241 mode->vrefresh = drm_mode_vrefresh(mode);
243 drm_mode_sort(&connector->modes);
245 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] probed modes :\n", connector->base.id,
247 list_for_each_entry(mode, &connector->modes, head) {
248 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
249 drm_mode_debug_printmodeline(mode);
256 * drm_helper_probe_single_connector_modes - get complete set of display modes
257 * @connector: connector to probe
258 * @maxX: max width for modes
259 * @maxY: max height for modes
261 * Based on the helper callbacks implemented by @connector try to detect all
262 * valid modes. Modes will first be added to the connector's probed_modes list,
263 * then culled (based on validity and the @maxX, @maxY parameters) and put into
264 * the normal modes list.
266 * Intended to be use as a generic implementation of the ->fill_modes()
267 * @connector vfunc for drivers that use the crtc helpers for output mode
268 * filtering and detection.
271 * The number of modes found on @connector.
273 int drm_helper_probe_single_connector_modes(struct drm_connector *connector,
274 uint32_t maxX, uint32_t maxY)
276 return drm_helper_probe_single_connector_modes_merge_bits(connector, maxX, maxY, true);
278 EXPORT_SYMBOL(drm_helper_probe_single_connector_modes);
281 * drm_helper_probe_single_connector_modes_nomerge - get complete set of display modes
282 * @connector: connector to probe
283 * @maxX: max width for modes
284 * @maxY: max height for modes
286 * This operates like drm_hehlper_probe_single_connector_modes except it
287 * replaces the mode bits instead of merging them for preferred modes.
289 int drm_helper_probe_single_connector_modes_nomerge(struct drm_connector *connector,
290 uint32_t maxX, uint32_t maxY)
292 return drm_helper_probe_single_connector_modes_merge_bits(connector, maxX, maxY, false);
294 EXPORT_SYMBOL(drm_helper_probe_single_connector_modes_nomerge);
297 * drm_kms_helper_hotplug_event - fire off KMS hotplug events
298 * @dev: drm_device whose connector state changed
300 * This function fires off the uevent for userspace and also calls the
301 * output_poll_changed function, which is most commonly used to inform the fbdev
302 * emulation code and allow it to update the fbcon output configuration.
304 * Drivers should call this from their hotplug handling code when a change is
305 * detected. Note that this function does not do any output detection of its
306 * own, like drm_helper_hpd_irq_event() does - this is assumed to be done by the
309 * This function must be called from process context with no mode
310 * setting locks held.
312 void drm_kms_helper_hotplug_event(struct drm_device *dev)
314 /* send a uevent + call fbdev */
315 drm_sysfs_hotplug_event(dev);
316 if (dev->mode_config.funcs->output_poll_changed)
317 dev->mode_config.funcs->output_poll_changed(dev);
319 EXPORT_SYMBOL(drm_kms_helper_hotplug_event);
321 static void output_poll_execute(struct work_struct *work)
323 struct delayed_work *delayed_work = to_delayed_work(work);
324 struct drm_device *dev = container_of(delayed_work, struct drm_device, mode_config.output_poll_work);
325 struct drm_connector *connector;
326 enum drm_connector_status old_status;
327 bool repoll = false, changed;
329 if (!dev->mode_config.poll_enabled)
332 /* Pick up any changes detected by the probe functions. */
333 changed = dev->mode_config.delayed_event;
334 dev->mode_config.delayed_event = false;
336 if (!drm_kms_helper_poll)
339 mutex_lock(&dev->mode_config.mutex);
340 drm_for_each_connector(connector, dev) {
342 /* Ignore forced connectors. */
343 if (connector->force)
346 /* Ignore HPD capable connectors and connectors where we don't
347 * want any hotplug detection at all for polling. */
348 if (!connector->polled || connector->polled == DRM_CONNECTOR_POLL_HPD)
351 old_status = connector->status;
352 /* if we are connected and don't want to poll for disconnect
354 if (old_status == connector_status_connected &&
355 !(connector->polled & DRM_CONNECTOR_POLL_DISCONNECT))
360 connector->status = connector->funcs->detect(connector, false);
361 if (old_status != connector->status) {
362 const char *old, *new;
365 * The poll work sets force=false when calling detect so
366 * that drivers can avoid to do disruptive tests (e.g.
367 * when load detect cycles could cause flickering on
368 * other, running displays). This bears the risk that we
369 * flip-flop between unknown here in the poll work and
370 * the real state when userspace forces a full detect
371 * call after receiving a hotplug event due to this
374 * Hence clamp an unknown detect status to the old
377 if (connector->status == connector_status_unknown) {
378 connector->status = old_status;
382 old = drm_get_connector_status_name(old_status);
383 new = drm_get_connector_status_name(connector->status);
385 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] "
386 "status updated from %s to %s\n",
395 mutex_unlock(&dev->mode_config.mutex);
399 drm_kms_helper_hotplug_event(dev);
402 schedule_delayed_work(delayed_work, DRM_OUTPUT_POLL_PERIOD);
406 * drm_kms_helper_is_poll_worker - is %current task an output poll worker?
408 * Determine if %current task is an output poll worker. This can be used
409 * to select distinct code paths for output polling versus other contexts.
411 * One use case is to avoid a deadlock between the output poll worker and
412 * the autosuspend worker wherein the latter waits for polling to finish
413 * upon calling drm_kms_helper_poll_disable(), while the former waits for
414 * runtime suspend to finish upon calling pm_runtime_get_sync() in a
415 * connector ->detect hook.
417 bool drm_kms_helper_is_poll_worker(void)
419 struct work_struct *work = current_work();
421 return work && work->func == output_poll_execute;
423 EXPORT_SYMBOL(drm_kms_helper_is_poll_worker);
426 * drm_kms_helper_poll_disable - disable output polling
429 * This function disables the output polling work.
431 * Drivers can call this helper from their device suspend implementation. It is
432 * not an error to call this even when output polling isn't enabled or arlready
435 void drm_kms_helper_poll_disable(struct drm_device *dev)
437 if (!dev->mode_config.poll_enabled)
439 cancel_delayed_work_sync(&dev->mode_config.output_poll_work);
441 EXPORT_SYMBOL(drm_kms_helper_poll_disable);
444 * drm_kms_helper_poll_enable - re-enable output polling.
447 * This function re-enables the output polling work.
449 * Drivers can call this helper from their device resume implementation. It is
450 * an error to call this when the output polling support has not yet been set
453 void drm_kms_helper_poll_enable(struct drm_device *dev)
455 mutex_lock(&dev->mode_config.mutex);
456 drm_kms_helper_poll_enable_locked(dev);
457 mutex_unlock(&dev->mode_config.mutex);
459 EXPORT_SYMBOL(drm_kms_helper_poll_enable);
462 * drm_kms_helper_poll_init - initialize and enable output polling
465 * This function intializes and then also enables output polling support for
466 * @dev. Drivers which do not have reliable hotplug support in hardware can use
467 * this helper infrastructure to regularly poll such connectors for changes in
468 * their connection state.
470 * Drivers can control which connectors are polled by setting the
471 * DRM_CONNECTOR_POLL_CONNECT and DRM_CONNECTOR_POLL_DISCONNECT flags. On
472 * connectors where probing live outputs can result in visual distortion drivers
473 * should not set the DRM_CONNECTOR_POLL_DISCONNECT flag to avoid this.
474 * Connectors which have no flag or only DRM_CONNECTOR_POLL_HPD set are
475 * completely ignored by the polling logic.
477 * Note that a connector can be both polled and probed from the hotplug handler,
478 * in case the hotplug interrupt is known to be unreliable.
480 void drm_kms_helper_poll_init(struct drm_device *dev)
482 INIT_DELAYED_WORK(&dev->mode_config.output_poll_work, output_poll_execute);
483 dev->mode_config.poll_enabled = true;
485 drm_kms_helper_poll_enable(dev);
487 EXPORT_SYMBOL(drm_kms_helper_poll_init);
490 * drm_kms_helper_poll_fini - disable output polling and clean it up
493 void drm_kms_helper_poll_fini(struct drm_device *dev)
495 if (!dev->mode_config.poll_enabled)
498 dev->mode_config.poll_enabled = false;
499 cancel_delayed_work_sync(&dev->mode_config.output_poll_work);
501 EXPORT_SYMBOL(drm_kms_helper_poll_fini);
504 * drm_helper_hpd_irq_event - hotplug processing
507 * Drivers can use this helper function to run a detect cycle on all connectors
508 * which have the DRM_CONNECTOR_POLL_HPD flag set in their &polled member. All
509 * other connectors are ignored, which is useful to avoid reprobing fixed
512 * This helper function is useful for drivers which can't or don't track hotplug
513 * interrupts for each connector.
515 * Drivers which support hotplug interrupts for each connector individually and
516 * which have a more fine-grained detect logic should bypass this code and
517 * directly call drm_kms_helper_hotplug_event() in case the connector state
520 * This function must be called from process context with no mode
521 * setting locks held.
523 * Note that a connector can be both polled and probed from the hotplug handler,
524 * in case the hotplug interrupt is known to be unreliable.
526 bool drm_helper_hpd_irq_event(struct drm_device *dev)
528 struct drm_connector *connector;
529 enum drm_connector_status old_status;
530 bool changed = false;
532 if (!dev->mode_config.poll_enabled)
535 mutex_lock(&dev->mode_config.mutex);
536 drm_for_each_connector(connector, dev) {
538 /* Only handle HPD capable connectors. */
539 if (!(connector->polled & DRM_CONNECTOR_POLL_HPD))
542 old_status = connector->status;
544 connector->status = connector->funcs->detect(connector, false);
545 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
548 drm_get_connector_status_name(old_status),
549 drm_get_connector_status_name(connector->status));
550 if (old_status != connector->status)
554 mutex_unlock(&dev->mode_config.mutex);
557 drm_kms_helper_hotplug_event(dev);
561 EXPORT_SYMBOL(drm_helper_hpd_irq_event);