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