blob: 04203c0d2ecbe0c2a73bc0a5909f9ef85f533baa [file] [log] [blame]
Daniel Vetter8d754542014-04-10 10:51:11 +02001/*
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
Ville Syrjälä05acaec2014-12-17 13:56:22 +020061static enum drm_mode_status
62drm_mode_validate_flag(const struct drm_display_mode *mode,
63 int flags)
Daniel Vetter8d754542014-04-10 10:51:11 +020064{
Ville Syrjälä05acaec2014-12-17 13:56:22 +020065 if ((mode->flags & DRM_MODE_FLAG_INTERLACE) &&
66 !(flags & DRM_MODE_FLAG_INTERLACE))
67 return MODE_NO_INTERLACE;
Daniel Vetter8d754542014-04-10 10:51:11 +020068
Ville Syrjälä05acaec2014-12-17 13:56:22 +020069 if ((mode->flags & DRM_MODE_FLAG_DBLSCAN) &&
70 !(flags & DRM_MODE_FLAG_DBLSCAN))
71 return MODE_NO_DBLESCAN;
Daniel Vetter8d754542014-04-10 10:51:11 +020072
Ville Syrjälä05acaec2014-12-17 13:56:22 +020073 if ((mode->flags & DRM_MODE_FLAG_3D_MASK) &&
74 !(flags & DRM_MODE_FLAG_3D_MASK))
75 return MODE_NO_STEREO;
Daniel Vetter8d754542014-04-10 10:51:11 +020076
Ville Syrjälä05acaec2014-12-17 13:56:22 +020077 return MODE_OK;
Daniel Vetter8d754542014-04-10 10:51:11 +020078}
79
Chris Wilsoneaf99c72014-08-06 10:08:32 +020080static 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
Dave Airlieb87577b2014-05-01 09:26:53 +100096static int drm_helper_probe_single_connector_modes_merge_bits(struct drm_connector *connector,
97 uint32_t maxX, uint32_t maxY, bool merge_type_bits)
Daniel Vetter8d754542014-04-10 10:51:11 +020098{
99 struct drm_device *dev = connector->dev;
100 struct drm_display_mode *mode;
Jani Nikulabe26a662015-03-11 11:51:06 +0200101 const struct drm_connector_helper_funcs *connector_funcs =
Daniel Vetter8d754542014-04-10 10:51:11 +0200102 connector->helper_private;
103 int count = 0;
104 int mode_flags = 0;
105 bool verbose_prune = true;
Daniel Vetter162b6a52015-01-21 08:45:21 +0100106 enum drm_connector_status old_status;
Daniel Vetter8d754542014-04-10 10:51:11 +0200107
108 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
109
110 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", connector->base.id,
Jani Nikula25933822014-06-03 14:56:20 +0300111 connector->name);
Daniel Vetter8d754542014-04-10 10:51:11 +0200112 /* 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) {
Peter Hurley2c4cc912014-11-03 20:51:45 -0500117 if (connector->force == DRM_FORCE_ON ||
118 connector->force == DRM_FORCE_ON_DIGITAL)
Daniel Vetter8d754542014-04-10 10:51:11 +0200119 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 {
Daniel Vetter162b6a52015-01-21 08:45:21 +0100125 old_status = connector->status;
126
Daniel Vetter8d754542014-04-10 10:51:11 +0200127 connector->status = connector->funcs->detect(connector, true);
Daniel Vetter162b6a52015-01-21 08:45:21 +0100128
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 }
Daniel Vetter8d754542014-04-10 10:51:11 +0200152 }
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",
Jani Nikula25933822014-06-03 14:56:20 +0300162 connector->base.id, connector->name);
Daniel Vetter8d754542014-04-10 10:51:11 +0200163 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
Thomas Wood4cf2b282014-06-18 17:52:33 +0100172 {
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);
Jani Nikulaad692b42015-03-26 10:42:00 +0200177 drm_edid_to_eld(connector, edid);
Thomas Wood4cf2b282014-06-18 17:52:33 +0100178 } else
179 count = (*connector_funcs->get_modes)(connector);
180 }
Daniel Vetter8d754542014-04-10 10:51:11 +0200181
182 if (count == 0 && connector->status == connector_status_connected)
183 count = drm_add_modes_noedid(connector, 1024, 768);
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200184 count += drm_helper_probe_add_cmdline_mode(connector);
Daniel Vetter8d754542014-04-10 10:51:11 +0200185 if (count == 0)
186 goto prune;
187
Dave Airlieb87577b2014-05-01 09:26:53 +1000188 drm_mode_connector_list_update(connector, merge_type_bits);
Daniel Vetter8d754542014-04-10 10:51:11 +0200189
Daniel Vetter8d754542014-04-10 10:51:11 +0200190 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;
Daniel Vetter8d754542014-04-10 10:51:11 +0200196
197 list_for_each_entry(mode, &connector->modes, head) {
Ville Syrjäläabc0b142014-12-17 13:56:23 +0200198 mode->status = drm_mode_validate_basic(mode);
199
200 if (mode->status == MODE_OK)
201 mode->status = drm_mode_validate_size(mode, maxX, maxY);
Ville Syrjälä05acaec2014-12-17 13:56:22 +0200202
203 if (mode->status == MODE_OK)
204 mode->status = drm_mode_validate_flag(mode, mode_flags);
205
Andrzej Hajdaf9b0e252014-04-02 12:29:46 +0200206 if (mode->status == MODE_OK && connector_funcs->mode_valid)
Daniel Vetter8d754542014-04-10 10:51:11 +0200207 mode->status = connector_funcs->mode_valid(connector,
208 mode);
209 }
210
211prune:
212 drm_mode_prune_invalid(dev, &connector->modes, verbose_prune);
213
214 if (list_empty(&connector->modes))
215 return 0;
216
217 list_for_each_entry(mode, &connector->modes, head)
218 mode->vrefresh = drm_mode_vrefresh(mode);
219
220 drm_mode_sort(&connector->modes);
221
222 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] probed modes :\n", connector->base.id,
Jani Nikula25933822014-06-03 14:56:20 +0300223 connector->name);
Daniel Vetter8d754542014-04-10 10:51:11 +0200224 list_for_each_entry(mode, &connector->modes, head) {
225 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
226 drm_mode_debug_printmodeline(mode);
227 }
228
229 return count;
230}
Dave Airlieb87577b2014-05-01 09:26:53 +1000231
232/**
233 * drm_helper_probe_single_connector_modes - get complete set of display modes
234 * @connector: connector to probe
235 * @maxX: max width for modes
236 * @maxY: max height for modes
237 *
238 * Based on the helper callbacks implemented by @connector try to detect all
239 * valid modes. Modes will first be added to the connector's probed_modes list,
240 * then culled (based on validity and the @maxX, @maxY parameters) and put into
241 * the normal modes list.
242 *
243 * Intended to be use as a generic implementation of the ->fill_modes()
244 * @connector vfunc for drivers that use the crtc helpers for output mode
245 * filtering and detection.
246 *
247 * Returns:
248 * The number of modes found on @connector.
249 */
250int drm_helper_probe_single_connector_modes(struct drm_connector *connector,
251 uint32_t maxX, uint32_t maxY)
252{
253 return drm_helper_probe_single_connector_modes_merge_bits(connector, maxX, maxY, true);
254}
Daniel Vetter8d754542014-04-10 10:51:11 +0200255EXPORT_SYMBOL(drm_helper_probe_single_connector_modes);
256
257/**
Dave Airlieb87577b2014-05-01 09:26:53 +1000258 * drm_helper_probe_single_connector_modes_nomerge - get complete set of display modes
259 * @connector: connector to probe
260 * @maxX: max width for modes
261 * @maxY: max height for modes
262 *
263 * This operates like drm_hehlper_probe_single_connector_modes except it
264 * replaces the mode bits instead of merging them for preferred modes.
265 */
266int drm_helper_probe_single_connector_modes_nomerge(struct drm_connector *connector,
267 uint32_t maxX, uint32_t maxY)
268{
269 return drm_helper_probe_single_connector_modes_merge_bits(connector, maxX, maxY, false);
270}
271EXPORT_SYMBOL(drm_helper_probe_single_connector_modes_nomerge);
272
273/**
Daniel Vetter8d754542014-04-10 10:51:11 +0200274 * drm_kms_helper_hotplug_event - fire off KMS hotplug events
275 * @dev: drm_device whose connector state changed
276 *
277 * This function fires off the uevent for userspace and also calls the
278 * output_poll_changed function, which is most commonly used to inform the fbdev
279 * emulation code and allow it to update the fbcon output configuration.
280 *
281 * Drivers should call this from their hotplug handling code when a change is
282 * detected. Note that this function does not do any output detection of its
283 * own, like drm_helper_hpd_irq_event() does - this is assumed to be done by the
284 * driver already.
285 *
286 * This function must be called from process context with no mode
287 * setting locks held.
288 */
289void drm_kms_helper_hotplug_event(struct drm_device *dev)
290{
291 /* send a uevent + call fbdev */
292 drm_sysfs_hotplug_event(dev);
293 if (dev->mode_config.funcs->output_poll_changed)
294 dev->mode_config.funcs->output_poll_changed(dev);
295}
296EXPORT_SYMBOL(drm_kms_helper_hotplug_event);
297
298#define DRM_OUTPUT_POLL_PERIOD (10*HZ)
299static void output_poll_execute(struct work_struct *work)
300{
301 struct delayed_work *delayed_work = to_delayed_work(work);
302 struct drm_device *dev = container_of(delayed_work, struct drm_device, mode_config.output_poll_work);
303 struct drm_connector *connector;
304 enum drm_connector_status old_status;
Daniel Vetter162b6a52015-01-21 08:45:21 +0100305 bool repoll = false, changed;
306
307 /* Pick up any changes detected by the probe functions. */
308 changed = dev->mode_config.delayed_event;
309 dev->mode_config.delayed_event = false;
Daniel Vetter8d754542014-04-10 10:51:11 +0200310
311 if (!drm_kms_helper_poll)
Daniel Vetter162b6a52015-01-21 08:45:21 +0100312 goto out;
Daniel Vetter8d754542014-04-10 10:51:11 +0200313
314 mutex_lock(&dev->mode_config.mutex);
315 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
316
317 /* Ignore forced connectors. */
318 if (connector->force)
319 continue;
320
321 /* Ignore HPD capable connectors and connectors where we don't
322 * want any hotplug detection at all for polling. */
323 if (!connector->polled || connector->polled == DRM_CONNECTOR_POLL_HPD)
324 continue;
325
Daniel Vetter8d754542014-04-10 10:51:11 +0200326 old_status = connector->status;
327 /* if we are connected and don't want to poll for disconnect
328 skip it */
329 if (old_status == connector_status_connected &&
330 !(connector->polled & DRM_CONNECTOR_POLL_DISCONNECT))
331 continue;
332
Josef Holzmayra3c6d682015-04-16 14:16:29 +0200333 repoll = true;
334
Daniel Vetter8d754542014-04-10 10:51:11 +0200335 connector->status = connector->funcs->detect(connector, false);
336 if (old_status != connector->status) {
337 const char *old, *new;
338
Daniel Vetterb7703722015-01-21 08:45:22 +0100339 /*
340 * The poll work sets force=false when calling detect so
341 * that drivers can avoid to do disruptive tests (e.g.
342 * when load detect cycles could cause flickering on
343 * other, running displays). This bears the risk that we
344 * flip-flop between unknown here in the poll work and
345 * the real state when userspace forces a full detect
346 * call after receiving a hotplug event due to this
347 * change.
348 *
349 * Hence clamp an unknown detect status to the old
350 * value.
351 */
352 if (connector->status == connector_status_unknown) {
353 connector->status = old_status;
354 continue;
355 }
356
Daniel Vetter8d754542014-04-10 10:51:11 +0200357 old = drm_get_connector_status_name(old_status);
358 new = drm_get_connector_status_name(connector->status);
359
360 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] "
361 "status updated from %s to %s\n",
362 connector->base.id,
Jani Nikula25933822014-06-03 14:56:20 +0300363 connector->name,
Daniel Vetter8d754542014-04-10 10:51:11 +0200364 old, new);
365
366 changed = true;
367 }
368 }
369
370 mutex_unlock(&dev->mode_config.mutex);
371
Daniel Vetter162b6a52015-01-21 08:45:21 +0100372out:
Daniel Vetter8d754542014-04-10 10:51:11 +0200373 if (changed)
374 drm_kms_helper_hotplug_event(dev);
375
376 if (repoll)
377 schedule_delayed_work(delayed_work, DRM_OUTPUT_POLL_PERIOD);
378}
379
380/**
381 * drm_kms_helper_poll_disable - disable output polling
382 * @dev: drm_device
383 *
384 * This function disables the output polling work.
385 *
386 * Drivers can call this helper from their device suspend implementation. It is
387 * not an error to call this even when output polling isn't enabled or arlready
388 * disabled.
389 */
390void drm_kms_helper_poll_disable(struct drm_device *dev)
391{
392 if (!dev->mode_config.poll_enabled)
393 return;
394 cancel_delayed_work_sync(&dev->mode_config.output_poll_work);
395}
396EXPORT_SYMBOL(drm_kms_helper_poll_disable);
397
398/**
399 * drm_kms_helper_poll_enable - re-enable output polling.
400 * @dev: drm_device
401 *
402 * This function re-enables the output polling work.
403 *
404 * Drivers can call this helper from their device resume implementation. It is
405 * an error to call this when the output polling support has not yet been set
406 * up.
407 */
408void drm_kms_helper_poll_enable(struct drm_device *dev)
409{
410 bool poll = false;
411 struct drm_connector *connector;
412
413 if (!dev->mode_config.poll_enabled || !drm_kms_helper_poll)
414 return;
415
416 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
417 if (connector->polled & (DRM_CONNECTOR_POLL_CONNECT |
418 DRM_CONNECTOR_POLL_DISCONNECT))
419 poll = true;
420 }
421
422 if (poll)
423 schedule_delayed_work(&dev->mode_config.output_poll_work, DRM_OUTPUT_POLL_PERIOD);
424}
425EXPORT_SYMBOL(drm_kms_helper_poll_enable);
426
427/**
428 * drm_kms_helper_poll_init - initialize and enable output polling
429 * @dev: drm_device
430 *
431 * This function intializes and then also enables output polling support for
432 * @dev. Drivers which do not have reliable hotplug support in hardware can use
433 * this helper infrastructure to regularly poll such connectors for changes in
434 * their connection state.
435 *
436 * Drivers can control which connectors are polled by setting the
437 * DRM_CONNECTOR_POLL_CONNECT and DRM_CONNECTOR_POLL_DISCONNECT flags. On
438 * connectors where probing live outputs can result in visual distortion drivers
439 * should not set the DRM_CONNECTOR_POLL_DISCONNECT flag to avoid this.
440 * Connectors which have no flag or only DRM_CONNECTOR_POLL_HPD set are
441 * completely ignored by the polling logic.
442 *
443 * Note that a connector can be both polled and probed from the hotplug handler,
444 * in case the hotplug interrupt is known to be unreliable.
445 */
446void drm_kms_helper_poll_init(struct drm_device *dev)
447{
448 INIT_DELAYED_WORK(&dev->mode_config.output_poll_work, output_poll_execute);
449 dev->mode_config.poll_enabled = true;
450
451 drm_kms_helper_poll_enable(dev);
452}
453EXPORT_SYMBOL(drm_kms_helper_poll_init);
454
455/**
456 * drm_kms_helper_poll_fini - disable output polling and clean it up
457 * @dev: drm_device
458 */
459void drm_kms_helper_poll_fini(struct drm_device *dev)
460{
461 drm_kms_helper_poll_disable(dev);
462}
463EXPORT_SYMBOL(drm_kms_helper_poll_fini);
464
465/**
466 * drm_helper_hpd_irq_event - hotplug processing
467 * @dev: drm_device
468 *
469 * Drivers can use this helper function to run a detect cycle on all connectors
470 * which have the DRM_CONNECTOR_POLL_HPD flag set in their &polled member. All
471 * other connectors are ignored, which is useful to avoid reprobing fixed
472 * panels.
473 *
474 * This helper function is useful for drivers which can't or don't track hotplug
475 * interrupts for each connector.
476 *
477 * Drivers which support hotplug interrupts for each connector individually and
478 * which have a more fine-grained detect logic should bypass this code and
479 * directly call drm_kms_helper_hotplug_event() in case the connector state
480 * changed.
481 *
482 * This function must be called from process context with no mode
483 * setting locks held.
484 *
485 * Note that a connector can be both polled and probed from the hotplug handler,
486 * in case the hotplug interrupt is known to be unreliable.
487 */
488bool drm_helper_hpd_irq_event(struct drm_device *dev)
489{
490 struct drm_connector *connector;
491 enum drm_connector_status old_status;
492 bool changed = false;
493
494 if (!dev->mode_config.poll_enabled)
495 return false;
496
497 mutex_lock(&dev->mode_config.mutex);
498 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
499
500 /* Only handle HPD capable connectors. */
501 if (!(connector->polled & DRM_CONNECTOR_POLL_HPD))
502 continue;
503
504 old_status = connector->status;
505
506 connector->status = connector->funcs->detect(connector, false);
507 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
508 connector->base.id,
Jani Nikula25933822014-06-03 14:56:20 +0300509 connector->name,
Daniel Vetter8d754542014-04-10 10:51:11 +0200510 drm_get_connector_status_name(old_status),
511 drm_get_connector_status_name(connector->status));
512 if (old_status != connector->status)
513 changed = true;
514 }
515
516 mutex_unlock(&dev->mode_config.mutex);
517
518 if (changed)
519 drm_kms_helper_hotplug_event(dev);
520
521 return changed;
522}
523EXPORT_SYMBOL(drm_helper_hpd_irq_event);