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 void drm_mode_validate_flag(struct drm_connector
*connector
,
64 struct drm_display_mode
*mode
;
66 if (flags
== (DRM_MODE_FLAG_DBLSCAN
| DRM_MODE_FLAG_INTERLACE
|
67 DRM_MODE_FLAG_3D_MASK
))
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
;
85 static int drm_helper_probe_add_cmdline_mode(struct drm_connector
*connector
)
87 struct drm_display_mode
*mode
;
89 if (!connector
->cmdline_mode
.specified
)
92 mode
= drm_mode_create_from_cmdline_mode(connector
->dev
,
93 &connector
->cmdline_mode
);
97 drm_mode_probed_add(connector
, mode
);
101 static int drm_helper_probe_single_connector_modes_merge_bits(struct drm_connector
*connector
,
102 uint32_t maxX
, uint32_t maxY
, bool merge_type_bits
)
104 struct drm_device
*dev
= connector
->dev
;
105 struct drm_display_mode
*mode
;
106 struct drm_connector_helper_funcs
*connector_funcs
=
107 connector
->helper_private
;
110 bool verbose_prune
= true;
112 WARN_ON(!mutex_is_locked(&dev
->mode_config
.mutex
));
114 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", connector
->base
.id
,
116 /* set all modes to the unverified state */
117 list_for_each_entry(mode
, &connector
->modes
, head
)
118 mode
->status
= MODE_UNVERIFIED
;
120 if (connector
->force
) {
121 if (connector
->force
== DRM_FORCE_ON
||
122 connector
->force
== DRM_FORCE_ON_DIGITAL
)
123 connector
->status
= connector_status_connected
;
125 connector
->status
= connector_status_disconnected
;
126 if (connector
->funcs
->force
)
127 connector
->funcs
->force(connector
);
129 connector
->status
= connector
->funcs
->detect(connector
, true);
132 /* Re-enable polling in case the global poll config changed. */
133 if (drm_kms_helper_poll
!= dev
->mode_config
.poll_running
)
134 drm_kms_helper_poll_enable(dev
);
136 dev
->mode_config
.poll_running
= drm_kms_helper_poll
;
138 if (connector
->status
== connector_status_disconnected
) {
139 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] disconnected\n",
140 connector
->base
.id
, connector
->name
);
141 drm_mode_connector_update_edid_property(connector
, NULL
);
142 verbose_prune
= false;
146 #ifdef CONFIG_DRM_LOAD_EDID_FIRMWARE
147 count
= drm_load_edid_firmware(connector
);
151 if (connector
->override_edid
) {
152 struct edid
*edid
= (struct edid
*) connector
->edid_blob_ptr
->data
;
154 count
= drm_add_edid_modes(connector
, edid
);
156 count
= (*connector_funcs
->get_modes
)(connector
);
159 if (count
== 0 && connector
->status
== connector_status_connected
)
160 count
= drm_add_modes_noedid(connector
, 1024, 768);
161 count
+= drm_helper_probe_add_cmdline_mode(connector
);
165 drm_mode_connector_list_update(connector
, merge_type_bits
);
168 drm_mode_validate_size(dev
, &connector
->modes
, maxX
, maxY
);
170 if (connector
->interlace_allowed
)
171 mode_flags
|= DRM_MODE_FLAG_INTERLACE
;
172 if (connector
->doublescan_allowed
)
173 mode_flags
|= DRM_MODE_FLAG_DBLSCAN
;
174 if (connector
->stereo_allowed
)
175 mode_flags
|= DRM_MODE_FLAG_3D_MASK
;
176 drm_mode_validate_flag(connector
, mode_flags
);
178 list_for_each_entry(mode
, &connector
->modes
, head
) {
179 if (mode
->status
== MODE_OK
&& connector_funcs
->mode_valid
)
180 mode
->status
= connector_funcs
->mode_valid(connector
,
185 drm_mode_prune_invalid(dev
, &connector
->modes
, verbose_prune
);
187 if (list_empty(&connector
->modes
))
190 list_for_each_entry(mode
, &connector
->modes
, head
)
191 mode
->vrefresh
= drm_mode_vrefresh(mode
);
193 drm_mode_sort(&connector
->modes
);
195 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] probed modes :\n", connector
->base
.id
,
197 list_for_each_entry(mode
, &connector
->modes
, head
) {
198 drm_mode_set_crtcinfo(mode
, CRTC_INTERLACE_HALVE_V
);
199 drm_mode_debug_printmodeline(mode
);
206 * drm_helper_probe_single_connector_modes - get complete set of display modes
207 * @connector: connector to probe
208 * @maxX: max width for modes
209 * @maxY: max height for modes
211 * Based on the helper callbacks implemented by @connector try to detect all
212 * valid modes. Modes will first be added to the connector's probed_modes list,
213 * then culled (based on validity and the @maxX, @maxY parameters) and put into
214 * the normal modes list.
216 * Intended to be use as a generic implementation of the ->fill_modes()
217 * @connector vfunc for drivers that use the crtc helpers for output mode
218 * filtering and detection.
221 * The number of modes found on @connector.
223 int drm_helper_probe_single_connector_modes(struct drm_connector
*connector
,
224 uint32_t maxX
, uint32_t maxY
)
226 return drm_helper_probe_single_connector_modes_merge_bits(connector
, maxX
, maxY
, true);
228 EXPORT_SYMBOL(drm_helper_probe_single_connector_modes
);
231 * drm_helper_probe_single_connector_modes_nomerge - get complete set of display modes
232 * @connector: connector to probe
233 * @maxX: max width for modes
234 * @maxY: max height for modes
236 * This operates like drm_hehlper_probe_single_connector_modes except it
237 * replaces the mode bits instead of merging them for preferred modes.
239 int drm_helper_probe_single_connector_modes_nomerge(struct drm_connector
*connector
,
240 uint32_t maxX
, uint32_t maxY
)
242 return drm_helper_probe_single_connector_modes_merge_bits(connector
, maxX
, maxY
, false);
244 EXPORT_SYMBOL(drm_helper_probe_single_connector_modes_nomerge
);
247 * drm_kms_helper_hotplug_event - fire off KMS hotplug events
248 * @dev: drm_device whose connector state changed
250 * This function fires off the uevent for userspace and also calls the
251 * output_poll_changed function, which is most commonly used to inform the fbdev
252 * emulation code and allow it to update the fbcon output configuration.
254 * Drivers should call this from their hotplug handling code when a change is
255 * detected. Note that this function does not do any output detection of its
256 * own, like drm_helper_hpd_irq_event() does - this is assumed to be done by the
259 * This function must be called from process context with no mode
260 * setting locks held.
262 void drm_kms_helper_hotplug_event(struct drm_device
*dev
)
264 /* send a uevent + call fbdev */
265 drm_sysfs_hotplug_event(dev
);
266 if (dev
->mode_config
.funcs
->output_poll_changed
)
267 dev
->mode_config
.funcs
->output_poll_changed(dev
);
269 EXPORT_SYMBOL(drm_kms_helper_hotplug_event
);
271 #define DRM_OUTPUT_POLL_PERIOD (10*HZ)
272 static void output_poll_execute(struct work_struct
*work
)
274 struct delayed_work
*delayed_work
= to_delayed_work(work
);
275 struct drm_device
*dev
= container_of(delayed_work
, struct drm_device
, mode_config
.output_poll_work
);
276 struct drm_connector
*connector
;
277 enum drm_connector_status old_status
;
278 bool repoll
= false, changed
= false;
280 if (!drm_kms_helper_poll
)
283 mutex_lock(&dev
->mode_config
.mutex
);
284 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
286 /* Ignore forced connectors. */
287 if (connector
->force
)
290 /* Ignore HPD capable connectors and connectors where we don't
291 * want any hotplug detection at all for polling. */
292 if (!connector
->polled
|| connector
->polled
== DRM_CONNECTOR_POLL_HPD
)
297 old_status
= connector
->status
;
298 /* if we are connected and don't want to poll for disconnect
300 if (old_status
== connector_status_connected
&&
301 !(connector
->polled
& DRM_CONNECTOR_POLL_DISCONNECT
))
304 connector
->status
= connector
->funcs
->detect(connector
, false);
305 if (old_status
!= connector
->status
) {
306 const char *old
, *new;
308 old
= drm_get_connector_status_name(old_status
);
309 new = drm_get_connector_status_name(connector
->status
);
311 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] "
312 "status updated from %s to %s\n",
321 mutex_unlock(&dev
->mode_config
.mutex
);
324 drm_kms_helper_hotplug_event(dev
);
327 schedule_delayed_work(delayed_work
, DRM_OUTPUT_POLL_PERIOD
);
331 * drm_kms_helper_poll_disable - disable output polling
334 * This function disables the output polling work.
336 * Drivers can call this helper from their device suspend implementation. It is
337 * not an error to call this even when output polling isn't enabled or arlready
340 void drm_kms_helper_poll_disable(struct drm_device
*dev
)
342 if (!dev
->mode_config
.poll_enabled
)
344 cancel_delayed_work_sync(&dev
->mode_config
.output_poll_work
);
346 EXPORT_SYMBOL(drm_kms_helper_poll_disable
);
349 * drm_kms_helper_poll_enable - re-enable output polling.
352 * This function re-enables the output polling work.
354 * Drivers can call this helper from their device resume implementation. It is
355 * an error to call this when the output polling support has not yet been set
358 void drm_kms_helper_poll_enable(struct drm_device
*dev
)
361 struct drm_connector
*connector
;
363 if (!dev
->mode_config
.poll_enabled
|| !drm_kms_helper_poll
)
366 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
367 if (connector
->polled
& (DRM_CONNECTOR_POLL_CONNECT
|
368 DRM_CONNECTOR_POLL_DISCONNECT
))
373 schedule_delayed_work(&dev
->mode_config
.output_poll_work
, DRM_OUTPUT_POLL_PERIOD
);
375 EXPORT_SYMBOL(drm_kms_helper_poll_enable
);
378 * drm_kms_helper_poll_init - initialize and enable output polling
381 * This function intializes and then also enables output polling support for
382 * @dev. Drivers which do not have reliable hotplug support in hardware can use
383 * this helper infrastructure to regularly poll such connectors for changes in
384 * their connection state.
386 * Drivers can control which connectors are polled by setting the
387 * DRM_CONNECTOR_POLL_CONNECT and DRM_CONNECTOR_POLL_DISCONNECT flags. On
388 * connectors where probing live outputs can result in visual distortion drivers
389 * should not set the DRM_CONNECTOR_POLL_DISCONNECT flag to avoid this.
390 * Connectors which have no flag or only DRM_CONNECTOR_POLL_HPD set are
391 * completely ignored by the polling logic.
393 * Note that a connector can be both polled and probed from the hotplug handler,
394 * in case the hotplug interrupt is known to be unreliable.
396 void drm_kms_helper_poll_init(struct drm_device
*dev
)
398 INIT_DELAYED_WORK(&dev
->mode_config
.output_poll_work
, output_poll_execute
);
399 dev
->mode_config
.poll_enabled
= true;
401 drm_kms_helper_poll_enable(dev
);
403 EXPORT_SYMBOL(drm_kms_helper_poll_init
);
406 * drm_kms_helper_poll_fini - disable output polling and clean it up
409 void drm_kms_helper_poll_fini(struct drm_device
*dev
)
411 drm_kms_helper_poll_disable(dev
);
413 EXPORT_SYMBOL(drm_kms_helper_poll_fini
);
416 * drm_helper_hpd_irq_event - hotplug processing
419 * Drivers can use this helper function to run a detect cycle on all connectors
420 * which have the DRM_CONNECTOR_POLL_HPD flag set in their &polled member. All
421 * other connectors are ignored, which is useful to avoid reprobing fixed
424 * This helper function is useful for drivers which can't or don't track hotplug
425 * interrupts for each connector.
427 * Drivers which support hotplug interrupts for each connector individually and
428 * which have a more fine-grained detect logic should bypass this code and
429 * directly call drm_kms_helper_hotplug_event() in case the connector state
432 * This function must be called from process context with no mode
433 * setting locks held.
435 * Note that a connector can be both polled and probed from the hotplug handler,
436 * in case the hotplug interrupt is known to be unreliable.
438 bool drm_helper_hpd_irq_event(struct drm_device
*dev
)
440 struct drm_connector
*connector
;
441 enum drm_connector_status old_status
;
442 bool changed
= false;
444 if (!dev
->mode_config
.poll_enabled
)
447 mutex_lock(&dev
->mode_config
.mutex
);
448 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
450 /* Only handle HPD capable connectors. */
451 if (!(connector
->polled
& DRM_CONNECTOR_POLL_HPD
))
454 old_status
= connector
->status
;
456 connector
->status
= connector
->funcs
->detect(connector
, false);
457 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
460 drm_get_connector_status_name(old_status
),
461 drm_get_connector_status_name(connector
->status
));
462 if (old_status
!= connector
->status
)
466 mutex_unlock(&dev
->mode_config
.mutex
);
469 drm_kms_helper_hotplug_event(dev
);
473 EXPORT_SYMBOL(drm_helper_hpd_irq_event
);