blob: d734780b31c0fdcd67d2d622333b0a78c8098e10 [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
Daniel Vetter8c4ccc42015-07-09 23:44:26 +020096#define DRM_OUTPUT_POLL_PERIOD (10*HZ)
97static void __drm_kms_helper_poll_enable(struct drm_device *dev)
98{
99 bool poll = false;
100 struct drm_connector *connector;
101
102 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
103
104 if (!dev->mode_config.poll_enabled || !drm_kms_helper_poll)
105 return;
106
107 drm_for_each_connector(connector, dev) {
108 if (connector->polled & (DRM_CONNECTOR_POLL_CONNECT |
109 DRM_CONNECTOR_POLL_DISCONNECT))
110 poll = true;
111 }
112
113 if (poll)
114 schedule_delayed_work(&dev->mode_config.output_poll_work, DRM_OUTPUT_POLL_PERIOD);
115}
116
Dave Airlieb87577b2014-05-01 09:26:53 +1000117static int drm_helper_probe_single_connector_modes_merge_bits(struct drm_connector *connector,
118 uint32_t maxX, uint32_t maxY, bool merge_type_bits)
Daniel Vetter8d754542014-04-10 10:51:11 +0200119{
120 struct drm_device *dev = connector->dev;
121 struct drm_display_mode *mode;
Jani Nikulabe26a662015-03-11 11:51:06 +0200122 const struct drm_connector_helper_funcs *connector_funcs =
Daniel Vetter8d754542014-04-10 10:51:11 +0200123 connector->helper_private;
124 int count = 0;
125 int mode_flags = 0;
126 bool verbose_prune = true;
Daniel Vetter162b6a52015-01-21 08:45:21 +0100127 enum drm_connector_status old_status;
Daniel Vetter8d754542014-04-10 10:51:11 +0200128
129 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
130
131 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", connector->base.id,
Jani Nikula25933822014-06-03 14:56:20 +0300132 connector->name);
Daniel Vetter8d754542014-04-10 10:51:11 +0200133 /* set all modes to the unverified state */
134 list_for_each_entry(mode, &connector->modes, head)
135 mode->status = MODE_UNVERIFIED;
136
137 if (connector->force) {
Peter Hurley2c4cc912014-11-03 20:51:45 -0500138 if (connector->force == DRM_FORCE_ON ||
139 connector->force == DRM_FORCE_ON_DIGITAL)
Daniel Vetter8d754542014-04-10 10:51:11 +0200140 connector->status = connector_status_connected;
141 else
142 connector->status = connector_status_disconnected;
143 if (connector->funcs->force)
144 connector->funcs->force(connector);
145 } else {
Daniel Vetter162b6a52015-01-21 08:45:21 +0100146 old_status = connector->status;
147
Daniel Vetter8d754542014-04-10 10:51:11 +0200148 connector->status = connector->funcs->detect(connector, true);
Daniel Vetter162b6a52015-01-21 08:45:21 +0100149
150 /*
151 * Normally either the driver's hpd code or the poll loop should
152 * pick up any changes and fire the hotplug event. But if
153 * userspace sneaks in a probe, we might miss a change. Hence
154 * check here, and if anything changed start the hotplug code.
155 */
156 if (old_status != connector->status) {
157 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %d to %d\n",
158 connector->base.id,
159 connector->name,
160 old_status, connector->status);
161
162 /*
163 * The hotplug event code might call into the fb
164 * helpers, and so expects that we do not hold any
165 * locks. Fire up the poll struct instead, it will
166 * disable itself again.
167 */
168 dev->mode_config.delayed_event = true;
169 if (dev->mode_config.poll_enabled)
170 schedule_delayed_work(&dev->mode_config.output_poll_work,
171 0);
172 }
Daniel Vetter8d754542014-04-10 10:51:11 +0200173 }
174
175 /* Re-enable polling in case the global poll config changed. */
176 if (drm_kms_helper_poll != dev->mode_config.poll_running)
Daniel Vetter8c4ccc42015-07-09 23:44:26 +0200177 __drm_kms_helper_poll_enable(dev);
Daniel Vetter8d754542014-04-10 10:51:11 +0200178
179 dev->mode_config.poll_running = drm_kms_helper_poll;
180
181 if (connector->status == connector_status_disconnected) {
182 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] disconnected\n",
Jani Nikula25933822014-06-03 14:56:20 +0300183 connector->base.id, connector->name);
Daniel Vetter8d754542014-04-10 10:51:11 +0200184 drm_mode_connector_update_edid_property(connector, NULL);
185 verbose_prune = false;
186 goto prune;
187 }
188
189#ifdef CONFIG_DRM_LOAD_EDID_FIRMWARE
190 count = drm_load_edid_firmware(connector);
191 if (count == 0)
192#endif
Thomas Wood4cf2b282014-06-18 17:52:33 +0100193 {
194 if (connector->override_edid) {
195 struct edid *edid = (struct edid *) connector->edid_blob_ptr->data;
196
197 count = drm_add_edid_modes(connector, edid);
Jani Nikulaad692b42015-03-26 10:42:00 +0200198 drm_edid_to_eld(connector, edid);
Thomas Wood4cf2b282014-06-18 17:52:33 +0100199 } else
200 count = (*connector_funcs->get_modes)(connector);
201 }
Daniel Vetter8d754542014-04-10 10:51:11 +0200202
203 if (count == 0 && connector->status == connector_status_connected)
204 count = drm_add_modes_noedid(connector, 1024, 768);
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200205 count += drm_helper_probe_add_cmdline_mode(connector);
Daniel Vetter8d754542014-04-10 10:51:11 +0200206 if (count == 0)
207 goto prune;
208
Dave Airlieb87577b2014-05-01 09:26:53 +1000209 drm_mode_connector_list_update(connector, merge_type_bits);
Daniel Vetter8d754542014-04-10 10:51:11 +0200210
Daniel Vetter8d754542014-04-10 10:51:11 +0200211 if (connector->interlace_allowed)
212 mode_flags |= DRM_MODE_FLAG_INTERLACE;
213 if (connector->doublescan_allowed)
214 mode_flags |= DRM_MODE_FLAG_DBLSCAN;
215 if (connector->stereo_allowed)
216 mode_flags |= DRM_MODE_FLAG_3D_MASK;
Daniel Vetter8d754542014-04-10 10:51:11 +0200217
218 list_for_each_entry(mode, &connector->modes, head) {
Ville Syrjäläabc0b142014-12-17 13:56:23 +0200219 mode->status = drm_mode_validate_basic(mode);
220
221 if (mode->status == MODE_OK)
222 mode->status = drm_mode_validate_size(mode, maxX, maxY);
Ville Syrjälä05acaec2014-12-17 13:56:22 +0200223
224 if (mode->status == MODE_OK)
225 mode->status = drm_mode_validate_flag(mode, mode_flags);
226
Andrzej Hajdaf9b0e252014-04-02 12:29:46 +0200227 if (mode->status == MODE_OK && connector_funcs->mode_valid)
Daniel Vetter8d754542014-04-10 10:51:11 +0200228 mode->status = connector_funcs->mode_valid(connector,
229 mode);
230 }
231
232prune:
233 drm_mode_prune_invalid(dev, &connector->modes, verbose_prune);
234
235 if (list_empty(&connector->modes))
236 return 0;
237
238 list_for_each_entry(mode, &connector->modes, head)
239 mode->vrefresh = drm_mode_vrefresh(mode);
240
241 drm_mode_sort(&connector->modes);
242
243 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] probed modes :\n", connector->base.id,
Jani Nikula25933822014-06-03 14:56:20 +0300244 connector->name);
Daniel Vetter8d754542014-04-10 10:51:11 +0200245 list_for_each_entry(mode, &connector->modes, head) {
246 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
247 drm_mode_debug_printmodeline(mode);
248 }
249
250 return count;
251}
Dave Airlieb87577b2014-05-01 09:26:53 +1000252
253/**
254 * drm_helper_probe_single_connector_modes - get complete set of display modes
255 * @connector: connector to probe
256 * @maxX: max width for modes
257 * @maxY: max height for modes
258 *
259 * Based on the helper callbacks implemented by @connector try to detect all
260 * valid modes. Modes will first be added to the connector's probed_modes list,
261 * then culled (based on validity and the @maxX, @maxY parameters) and put into
262 * the normal modes list.
263 *
264 * Intended to be use as a generic implementation of the ->fill_modes()
265 * @connector vfunc for drivers that use the crtc helpers for output mode
266 * filtering and detection.
267 *
268 * Returns:
269 * The number of modes found on @connector.
270 */
271int drm_helper_probe_single_connector_modes(struct drm_connector *connector,
272 uint32_t maxX, uint32_t maxY)
273{
274 return drm_helper_probe_single_connector_modes_merge_bits(connector, maxX, maxY, true);
275}
Daniel Vetter8d754542014-04-10 10:51:11 +0200276EXPORT_SYMBOL(drm_helper_probe_single_connector_modes);
277
278/**
Dave Airlieb87577b2014-05-01 09:26:53 +1000279 * drm_helper_probe_single_connector_modes_nomerge - get complete set of display modes
280 * @connector: connector to probe
281 * @maxX: max width for modes
282 * @maxY: max height for modes
283 *
284 * This operates like drm_hehlper_probe_single_connector_modes except it
285 * replaces the mode bits instead of merging them for preferred modes.
286 */
287int drm_helper_probe_single_connector_modes_nomerge(struct drm_connector *connector,
288 uint32_t maxX, uint32_t maxY)
289{
290 return drm_helper_probe_single_connector_modes_merge_bits(connector, maxX, maxY, false);
291}
292EXPORT_SYMBOL(drm_helper_probe_single_connector_modes_nomerge);
293
294/**
Daniel Vetter8d754542014-04-10 10:51:11 +0200295 * drm_kms_helper_hotplug_event - fire off KMS hotplug events
296 * @dev: drm_device whose connector state changed
297 *
298 * This function fires off the uevent for userspace and also calls the
299 * output_poll_changed function, which is most commonly used to inform the fbdev
300 * emulation code and allow it to update the fbcon output configuration.
301 *
302 * Drivers should call this from their hotplug handling code when a change is
303 * detected. Note that this function does not do any output detection of its
304 * own, like drm_helper_hpd_irq_event() does - this is assumed to be done by the
305 * driver already.
306 *
307 * This function must be called from process context with no mode
308 * setting locks held.
309 */
310void drm_kms_helper_hotplug_event(struct drm_device *dev)
311{
312 /* send a uevent + call fbdev */
313 drm_sysfs_hotplug_event(dev);
314 if (dev->mode_config.funcs->output_poll_changed)
315 dev->mode_config.funcs->output_poll_changed(dev);
316}
317EXPORT_SYMBOL(drm_kms_helper_hotplug_event);
318
Daniel Vetter8d754542014-04-10 10:51:11 +0200319static void output_poll_execute(struct work_struct *work)
320{
321 struct delayed_work *delayed_work = to_delayed_work(work);
322 struct drm_device *dev = container_of(delayed_work, struct drm_device, mode_config.output_poll_work);
323 struct drm_connector *connector;
324 enum drm_connector_status old_status;
Daniel Vetter162b6a52015-01-21 08:45:21 +0100325 bool repoll = false, changed;
326
327 /* Pick up any changes detected by the probe functions. */
328 changed = dev->mode_config.delayed_event;
329 dev->mode_config.delayed_event = false;
Daniel Vetter8d754542014-04-10 10:51:11 +0200330
331 if (!drm_kms_helper_poll)
Daniel Vetter162b6a52015-01-21 08:45:21 +0100332 goto out;
Daniel Vetter8d754542014-04-10 10:51:11 +0200333
334 mutex_lock(&dev->mode_config.mutex);
Daniel Vetter6295d602015-07-09 23:44:25 +0200335 drm_for_each_connector(connector, dev) {
Daniel Vetter8d754542014-04-10 10:51:11 +0200336
337 /* Ignore forced connectors. */
338 if (connector->force)
339 continue;
340
341 /* Ignore HPD capable connectors and connectors where we don't
342 * want any hotplug detection at all for polling. */
343 if (!connector->polled || connector->polled == DRM_CONNECTOR_POLL_HPD)
344 continue;
345
Daniel Vetter8d754542014-04-10 10:51:11 +0200346 old_status = connector->status;
347 /* if we are connected and don't want to poll for disconnect
348 skip it */
349 if (old_status == connector_status_connected &&
350 !(connector->polled & DRM_CONNECTOR_POLL_DISCONNECT))
351 continue;
352
Josef Holzmayra3c6d682015-04-16 14:16:29 +0200353 repoll = true;
354
Daniel Vetter8d754542014-04-10 10:51:11 +0200355 connector->status = connector->funcs->detect(connector, false);
356 if (old_status != connector->status) {
357 const char *old, *new;
358
Daniel Vetterb7703722015-01-21 08:45:22 +0100359 /*
360 * The poll work sets force=false when calling detect so
361 * that drivers can avoid to do disruptive tests (e.g.
362 * when load detect cycles could cause flickering on
363 * other, running displays). This bears the risk that we
364 * flip-flop between unknown here in the poll work and
365 * the real state when userspace forces a full detect
366 * call after receiving a hotplug event due to this
367 * change.
368 *
369 * Hence clamp an unknown detect status to the old
370 * value.
371 */
372 if (connector->status == connector_status_unknown) {
373 connector->status = old_status;
374 continue;
375 }
376
Daniel Vetter8d754542014-04-10 10:51:11 +0200377 old = drm_get_connector_status_name(old_status);
378 new = drm_get_connector_status_name(connector->status);
379
380 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] "
381 "status updated from %s to %s\n",
382 connector->base.id,
Jani Nikula25933822014-06-03 14:56:20 +0300383 connector->name,
Daniel Vetter8d754542014-04-10 10:51:11 +0200384 old, new);
385
386 changed = true;
387 }
388 }
389
390 mutex_unlock(&dev->mode_config.mutex);
391
Daniel Vetter162b6a52015-01-21 08:45:21 +0100392out:
Daniel Vetter8d754542014-04-10 10:51:11 +0200393 if (changed)
394 drm_kms_helper_hotplug_event(dev);
395
396 if (repoll)
397 schedule_delayed_work(delayed_work, DRM_OUTPUT_POLL_PERIOD);
398}
399
400/**
401 * drm_kms_helper_poll_disable - disable output polling
402 * @dev: drm_device
403 *
404 * This function disables the output polling work.
405 *
406 * Drivers can call this helper from their device suspend implementation. It is
407 * not an error to call this even when output polling isn't enabled or arlready
408 * disabled.
409 */
410void drm_kms_helper_poll_disable(struct drm_device *dev)
411{
412 if (!dev->mode_config.poll_enabled)
413 return;
414 cancel_delayed_work_sync(&dev->mode_config.output_poll_work);
415}
416EXPORT_SYMBOL(drm_kms_helper_poll_disable);
417
418/**
419 * drm_kms_helper_poll_enable - re-enable output polling.
420 * @dev: drm_device
421 *
422 * This function re-enables the output polling work.
423 *
424 * Drivers can call this helper from their device resume implementation. It is
425 * an error to call this when the output polling support has not yet been set
426 * up.
427 */
428void drm_kms_helper_poll_enable(struct drm_device *dev)
429{
Daniel Vetter8c4ccc42015-07-09 23:44:26 +0200430 mutex_lock(&dev->mode_config.mutex);
431 __drm_kms_helper_poll_enable(dev);
432 mutex_unlock(&dev->mode_config.mutex);
Daniel Vetter8d754542014-04-10 10:51:11 +0200433}
434EXPORT_SYMBOL(drm_kms_helper_poll_enable);
435
436/**
437 * drm_kms_helper_poll_init - initialize and enable output polling
438 * @dev: drm_device
439 *
440 * This function intializes and then also enables output polling support for
441 * @dev. Drivers which do not have reliable hotplug support in hardware can use
442 * this helper infrastructure to regularly poll such connectors for changes in
443 * their connection state.
444 *
445 * Drivers can control which connectors are polled by setting the
446 * DRM_CONNECTOR_POLL_CONNECT and DRM_CONNECTOR_POLL_DISCONNECT flags. On
447 * connectors where probing live outputs can result in visual distortion drivers
448 * should not set the DRM_CONNECTOR_POLL_DISCONNECT flag to avoid this.
449 * Connectors which have no flag or only DRM_CONNECTOR_POLL_HPD set are
450 * completely ignored by the polling logic.
451 *
452 * Note that a connector can be both polled and probed from the hotplug handler,
453 * in case the hotplug interrupt is known to be unreliable.
454 */
455void drm_kms_helper_poll_init(struct drm_device *dev)
456{
457 INIT_DELAYED_WORK(&dev->mode_config.output_poll_work, output_poll_execute);
458 dev->mode_config.poll_enabled = true;
459
460 drm_kms_helper_poll_enable(dev);
461}
462EXPORT_SYMBOL(drm_kms_helper_poll_init);
463
464/**
465 * drm_kms_helper_poll_fini - disable output polling and clean it up
466 * @dev: drm_device
467 */
468void drm_kms_helper_poll_fini(struct drm_device *dev)
469{
470 drm_kms_helper_poll_disable(dev);
471}
472EXPORT_SYMBOL(drm_kms_helper_poll_fini);
473
474/**
475 * drm_helper_hpd_irq_event - hotplug processing
476 * @dev: drm_device
477 *
478 * Drivers can use this helper function to run a detect cycle on all connectors
479 * which have the DRM_CONNECTOR_POLL_HPD flag set in their &polled member. All
480 * other connectors are ignored, which is useful to avoid reprobing fixed
481 * panels.
482 *
483 * This helper function is useful for drivers which can't or don't track hotplug
484 * interrupts for each connector.
485 *
486 * Drivers which support hotplug interrupts for each connector individually and
487 * which have a more fine-grained detect logic should bypass this code and
488 * directly call drm_kms_helper_hotplug_event() in case the connector state
489 * changed.
490 *
491 * This function must be called from process context with no mode
492 * setting locks held.
493 *
494 * Note that a connector can be both polled and probed from the hotplug handler,
495 * in case the hotplug interrupt is known to be unreliable.
496 */
497bool drm_helper_hpd_irq_event(struct drm_device *dev)
498{
499 struct drm_connector *connector;
500 enum drm_connector_status old_status;
501 bool changed = false;
502
503 if (!dev->mode_config.poll_enabled)
504 return false;
505
506 mutex_lock(&dev->mode_config.mutex);
Daniel Vetter6295d602015-07-09 23:44:25 +0200507 drm_for_each_connector(connector, dev) {
Daniel Vetter8d754542014-04-10 10:51:11 +0200508
509 /* Only handle HPD capable connectors. */
510 if (!(connector->polled & DRM_CONNECTOR_POLL_HPD))
511 continue;
512
513 old_status = connector->status;
514
515 connector->status = connector->funcs->detect(connector, false);
516 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
517 connector->base.id,
Jani Nikula25933822014-06-03 14:56:20 +0300518 connector->name,
Daniel Vetter8d754542014-04-10 10:51:11 +0200519 drm_get_connector_status_name(old_status),
520 drm_get_connector_status_name(connector->status));
521 if (old_status != connector->status)
522 changed = true;
523 }
524
525 mutex_unlock(&dev->mode_config.mutex);
526
527 if (changed)
528 drm_kms_helper_hotplug_event(dev);
529
530 return changed;
531}
532EXPORT_SYMBOL(drm_helper_hpd_irq_event);