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_single_connector_modes_merge_bits(struct drm_connector
*connector
,
86 uint32_t maxX
, uint32_t maxY
, bool merge_type_bits
)
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
;
94 bool verbose_prune
= true;
96 WARN_ON(!mutex_is_locked(&dev
->mode_config
.mutex
));
98 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", connector
->base
.id
,
100 /* set all modes to the unverified state */
101 list_for_each_entry(mode
, &connector
->modes
, head
)
102 mode
->status
= MODE_UNVERIFIED
;
104 if (connector
->force
) {
105 if (connector
->force
== DRM_FORCE_ON
)
106 connector
->status
= connector_status_connected
;
108 connector
->status
= connector_status_disconnected
;
109 if (connector
->funcs
->force
)
110 connector
->funcs
->force(connector
);
112 connector
->status
= connector
->funcs
->detect(connector
, true);
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
);
119 dev
->mode_config
.poll_running
= drm_kms_helper_poll
;
121 if (connector
->status
== connector_status_disconnected
) {
122 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] disconnected\n",
123 connector
->base
.id
, connector
->name
);
124 drm_mode_connector_update_edid_property(connector
, NULL
);
125 verbose_prune
= false;
129 #ifdef CONFIG_DRM_LOAD_EDID_FIRMWARE
130 count
= drm_load_edid_firmware(connector
);
133 count
= (*connector_funcs
->get_modes
)(connector
);
135 if (count
== 0 && connector
->status
== connector_status_connected
)
136 count
= drm_add_modes_noedid(connector
, 1024, 768);
140 drm_mode_connector_list_update(connector
, merge_type_bits
);
143 drm_mode_validate_size(dev
, &connector
->modes
, maxX
, maxY
);
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
);
153 list_for_each_entry(mode
, &connector
->modes
, head
) {
154 if (mode
->status
== MODE_OK
&& connector_funcs
->mode_valid
)
155 mode
->status
= connector_funcs
->mode_valid(connector
,
160 drm_mode_prune_invalid(dev
, &connector
->modes
, verbose_prune
);
162 if (list_empty(&connector
->modes
))
165 list_for_each_entry(mode
, &connector
->modes
, head
)
166 mode
->vrefresh
= drm_mode_vrefresh(mode
);
168 drm_mode_sort(&connector
->modes
);
170 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] probed modes :\n", connector
->base
.id
,
172 list_for_each_entry(mode
, &connector
->modes
, head
) {
173 drm_mode_set_crtcinfo(mode
, CRTC_INTERLACE_HALVE_V
);
174 drm_mode_debug_printmodeline(mode
);
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
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.
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.
196 * The number of modes found on @connector.
198 int drm_helper_probe_single_connector_modes(struct drm_connector
*connector
,
199 uint32_t maxX
, uint32_t maxY
)
201 return drm_helper_probe_single_connector_modes_merge_bits(connector
, maxX
, maxY
, true);
203 EXPORT_SYMBOL(drm_helper_probe_single_connector_modes
);
206 * 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
211 * This operates like drm_hehlper_probe_single_connector_modes except it
212 * replaces the mode bits instead of merging them for preferred modes.
214 int drm_helper_probe_single_connector_modes_nomerge(struct drm_connector
*connector
,
215 uint32_t maxX
, uint32_t maxY
)
217 return drm_helper_probe_single_connector_modes_merge_bits(connector
, maxX
, maxY
, false);
219 EXPORT_SYMBOL(drm_helper_probe_single_connector_modes_nomerge
);
222 * drm_kms_helper_hotplug_event - fire off KMS hotplug events
223 * @dev: drm_device whose connector state changed
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.
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
234 * This function must be called from process context with no mode
235 * setting locks held.
237 void drm_kms_helper_hotplug_event(struct drm_device
*dev
)
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
);
244 EXPORT_SYMBOL(drm_kms_helper_hotplug_event
);
246 #define DRM_OUTPUT_POLL_PERIOD (10*HZ)
247 static void output_poll_execute(struct work_struct
*work
)
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;
255 if (!drm_kms_helper_poll
)
258 mutex_lock(&dev
->mode_config
.mutex
);
259 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
261 /* Ignore forced connectors. */
262 if (connector
->force
)
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
)
272 old_status
= connector
->status
;
273 /* if we are connected and don't want to poll for disconnect
275 if (old_status
== connector_status_connected
&&
276 !(connector
->polled
& DRM_CONNECTOR_POLL_DISCONNECT
))
279 connector
->status
= connector
->funcs
->detect(connector
, false);
280 if (old_status
!= connector
->status
) {
281 const char *old
, *new;
283 old
= drm_get_connector_status_name(old_status
);
284 new = drm_get_connector_status_name(connector
->status
);
286 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] "
287 "status updated from %s to %s\n",
296 mutex_unlock(&dev
->mode_config
.mutex
);
299 drm_kms_helper_hotplug_event(dev
);
302 schedule_delayed_work(delayed_work
, DRM_OUTPUT_POLL_PERIOD
);
306 * drm_kms_helper_poll_disable - disable output polling
309 * This function disables the output polling work.
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
315 void drm_kms_helper_poll_disable(struct drm_device
*dev
)
317 if (!dev
->mode_config
.poll_enabled
)
319 cancel_delayed_work_sync(&dev
->mode_config
.output_poll_work
);
321 EXPORT_SYMBOL(drm_kms_helper_poll_disable
);
324 * drm_kms_helper_poll_enable - re-enable output polling.
327 * This function re-enables the output polling work.
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
333 void drm_kms_helper_poll_enable(struct drm_device
*dev
)
336 struct drm_connector
*connector
;
338 if (!dev
->mode_config
.poll_enabled
|| !drm_kms_helper_poll
)
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
))
348 schedule_delayed_work(&dev
->mode_config
.output_poll_work
, DRM_OUTPUT_POLL_PERIOD
);
350 EXPORT_SYMBOL(drm_kms_helper_poll_enable
);
353 * drm_kms_helper_poll_init - initialize and enable output polling
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.
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.
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.
371 void drm_kms_helper_poll_init(struct drm_device
*dev
)
373 INIT_DELAYED_WORK(&dev
->mode_config
.output_poll_work
, output_poll_execute
);
374 dev
->mode_config
.poll_enabled
= true;
376 drm_kms_helper_poll_enable(dev
);
378 EXPORT_SYMBOL(drm_kms_helper_poll_init
);
381 * drm_kms_helper_poll_fini - disable output polling and clean it up
384 void drm_kms_helper_poll_fini(struct drm_device
*dev
)
386 drm_kms_helper_poll_disable(dev
);
388 EXPORT_SYMBOL(drm_kms_helper_poll_fini
);
391 * drm_helper_hpd_irq_event - hotplug processing
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
399 * This helper function is useful for drivers which can't or don't track hotplug
400 * interrupts for each connector.
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
407 * This function must be called from process context with no mode
408 * setting locks held.
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.
413 bool drm_helper_hpd_irq_event(struct drm_device
*dev
)
415 struct drm_connector
*connector
;
416 enum drm_connector_status old_status
;
417 bool changed
= false;
419 if (!dev
->mode_config
.poll_enabled
)
422 mutex_lock(&dev
->mode_config
.mutex
);
423 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
425 /* Only handle HPD capable connectors. */
426 if (!(connector
->polled
& DRM_CONNECTOR_POLL_HPD
))
429 old_status
= connector
->status
;
431 connector
->status
= connector
->funcs
->detect(connector
, false);
432 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
435 drm_get_connector_status_name(old_status
),
436 drm_get_connector_status_name(connector
->status
));
437 if (old_status
!= connector
->status
)
441 mutex_unlock(&dev
->mode_config
.mutex
);
444 drm_kms_helper_hotplug_event(dev
);
448 EXPORT_SYMBOL(drm_helper_hpd_irq_event
);