drm/i915: Update to Linux 3.17
[dragonfly.git] / sys / dev / drm / drm_probe_helper.c
blob15263a50d4388b1e76177c6087ccd877fd073a6b
1 /*
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
23 * OF THIS SOFTWARE.
25 * Authors:
26 * Keith Packard
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>
35 #include <drm/drmP.h>
36 #include <drm/drm_crtc.h>
37 #include <uapi_drm/drm_fourcc.h>
38 #include <drm/drm_crtc_helper.h>
39 #include <drm/drm_fb_helper.h>
40 #include <drm/drm_edid.h>
42 /**
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,
62 int flags)
64 struct drm_display_mode *mode;
66 if (flags == (DRM_MODE_FLAG_DBLSCAN | DRM_MODE_FLAG_INTERLACE |
67 DRM_MODE_FLAG_3D_MASK))
68 return;
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;
82 return;
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;
92 int count = 0;
93 int mode_flags = 0;
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,
99 connector->name);
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;
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);
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;
126 goto prune;
129 #ifdef CONFIG_DRM_LOAD_EDID_FIRMWARE
130 count = drm_load_edid_firmware(connector);
131 if (count == 0)
132 #endif
134 if (connector->override_edid) {
135 struct edid *edid = (struct edid *) connector->edid_blob_ptr->data;
137 count = drm_add_edid_modes(connector, edid);
138 } else
139 count = (*connector_funcs->get_modes)(connector);
142 if (count == 0 && connector->status == connector_status_connected)
143 count = drm_add_modes_noedid(connector, 1024, 768);
144 if (count == 0)
145 goto prune;
147 drm_mode_connector_list_update(connector, merge_type_bits);
149 if (maxX && maxY)
150 drm_mode_validate_size(dev, &connector->modes, maxX, maxY);
152 if (connector->interlace_allowed)
153 mode_flags |= DRM_MODE_FLAG_INTERLACE;
154 if (connector->doublescan_allowed)
155 mode_flags |= DRM_MODE_FLAG_DBLSCAN;
156 if (connector->stereo_allowed)
157 mode_flags |= DRM_MODE_FLAG_3D_MASK;
158 drm_mode_validate_flag(connector, mode_flags);
160 list_for_each_entry(mode, &connector->modes, head) {
161 if (mode->status == MODE_OK && connector_funcs->mode_valid)
162 mode->status = connector_funcs->mode_valid(connector,
163 mode);
166 prune:
167 drm_mode_prune_invalid(dev, &connector->modes, verbose_prune);
169 if (list_empty(&connector->modes))
170 return 0;
172 list_for_each_entry(mode, &connector->modes, head)
173 mode->vrefresh = drm_mode_vrefresh(mode);
175 drm_mode_sort(&connector->modes);
177 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] probed modes :\n", connector->base.id,
178 connector->name);
179 list_for_each_entry(mode, &connector->modes, head) {
180 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
181 drm_mode_debug_printmodeline(mode);
184 return count;
188 * drm_helper_probe_single_connector_modes - get complete set of display modes
189 * @connector: connector to probe
190 * @maxX: max width for modes
191 * @maxY: max height for modes
193 * Based on the helper callbacks implemented by @connector try to detect all
194 * valid modes. Modes will first be added to the connector's probed_modes list,
195 * then culled (based on validity and the @maxX, @maxY parameters) and put into
196 * the normal modes list.
198 * Intended to be use as a generic implementation of the ->fill_modes()
199 * @connector vfunc for drivers that use the crtc helpers for output mode
200 * filtering and detection.
202 * Returns:
203 * The number of modes found on @connector.
205 int drm_helper_probe_single_connector_modes(struct drm_connector *connector,
206 uint32_t maxX, uint32_t maxY)
208 return drm_helper_probe_single_connector_modes_merge_bits(connector, maxX, maxY, true);
210 EXPORT_SYMBOL(drm_helper_probe_single_connector_modes);
213 * drm_helper_probe_single_connector_modes_nomerge - get complete set of display modes
214 * @connector: connector to probe
215 * @maxX: max width for modes
216 * @maxY: max height for modes
218 * This operates like drm_hehlper_probe_single_connector_modes except it
219 * replaces the mode bits instead of merging them for preferred modes.
221 int drm_helper_probe_single_connector_modes_nomerge(struct drm_connector *connector,
222 uint32_t maxX, uint32_t maxY)
224 return drm_helper_probe_single_connector_modes_merge_bits(connector, maxX, maxY, false);
226 EXPORT_SYMBOL(drm_helper_probe_single_connector_modes_nomerge);
229 * drm_kms_helper_hotplug_event - fire off KMS hotplug events
230 * @dev: drm_device whose connector state changed
232 * This function fires off the uevent for userspace and also calls the
233 * output_poll_changed function, which is most commonly used to inform the fbdev
234 * emulation code and allow it to update the fbcon output configuration.
236 * Drivers should call this from their hotplug handling code when a change is
237 * detected. Note that this function does not do any output detection of its
238 * own, like drm_helper_hpd_irq_event() does - this is assumed to be done by the
239 * driver already.
241 * This function must be called from process context with no mode
242 * setting locks held.
244 void drm_kms_helper_hotplug_event(struct drm_device *dev)
246 /* send a uevent + call fbdev */
247 drm_sysfs_hotplug_event(dev);
248 if (dev->mode_config.funcs->output_poll_changed)
249 dev->mode_config.funcs->output_poll_changed(dev);
251 EXPORT_SYMBOL(drm_kms_helper_hotplug_event);
253 #define DRM_OUTPUT_POLL_PERIOD (10*HZ)
254 static void output_poll_execute(struct work_struct *work)
256 struct delayed_work *delayed_work = to_delayed_work(work);
257 struct drm_device *dev = container_of(delayed_work, struct drm_device, mode_config.output_poll_work);
258 struct drm_connector *connector;
259 enum drm_connector_status old_status;
260 bool repoll = false, changed = false;
262 if (!drm_kms_helper_poll)
263 return;
265 mutex_lock(&dev->mode_config.mutex);
266 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
268 /* Ignore forced connectors. */
269 if (connector->force)
270 continue;
272 /* Ignore HPD capable connectors and connectors where we don't
273 * want any hotplug detection at all for polling. */
274 if (!connector->polled || connector->polled == DRM_CONNECTOR_POLL_HPD)
275 continue;
277 repoll = true;
279 old_status = connector->status;
280 /* if we are connected and don't want to poll for disconnect
281 skip it */
282 if (old_status == connector_status_connected &&
283 !(connector->polled & DRM_CONNECTOR_POLL_DISCONNECT))
284 continue;
286 connector->status = connector->funcs->detect(connector, false);
287 if (old_status != connector->status) {
288 const char *old, *new;
290 old = drm_get_connector_status_name(old_status);
291 new = drm_get_connector_status_name(connector->status);
293 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] "
294 "status updated from %s to %s\n",
295 connector->base.id,
296 connector->name,
297 old, new);
299 changed = true;
303 mutex_unlock(&dev->mode_config.mutex);
305 if (changed)
306 drm_kms_helper_hotplug_event(dev);
308 if (repoll)
309 schedule_delayed_work(delayed_work, DRM_OUTPUT_POLL_PERIOD);
313 * drm_kms_helper_poll_disable - disable output polling
314 * @dev: drm_device
316 * This function disables the output polling work.
318 * Drivers can call this helper from their device suspend implementation. It is
319 * not an error to call this even when output polling isn't enabled or arlready
320 * disabled.
322 void drm_kms_helper_poll_disable(struct drm_device *dev)
324 if (!dev->mode_config.poll_enabled)
325 return;
326 cancel_delayed_work_sync(&dev->mode_config.output_poll_work);
328 EXPORT_SYMBOL(drm_kms_helper_poll_disable);
331 * drm_kms_helper_poll_enable - re-enable output polling.
332 * @dev: drm_device
334 * This function re-enables the output polling work.
336 * Drivers can call this helper from their device resume implementation. It is
337 * an error to call this when the output polling support has not yet been set
338 * up.
340 void drm_kms_helper_poll_enable(struct drm_device *dev)
342 bool poll = false;
343 struct drm_connector *connector;
345 if (!dev->mode_config.poll_enabled || !drm_kms_helper_poll)
346 return;
348 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
349 if (connector->polled & (DRM_CONNECTOR_POLL_CONNECT |
350 DRM_CONNECTOR_POLL_DISCONNECT))
351 poll = true;
354 if (poll)
355 schedule_delayed_work(&dev->mode_config.output_poll_work, DRM_OUTPUT_POLL_PERIOD);
357 EXPORT_SYMBOL(drm_kms_helper_poll_enable);
360 * drm_kms_helper_poll_init - initialize and enable output polling
361 * @dev: drm_device
363 * This function intializes and then also enables output polling support for
364 * @dev. Drivers which do not have reliable hotplug support in hardware can use
365 * this helper infrastructure to regularly poll such connectors for changes in
366 * their connection state.
368 * Drivers can control which connectors are polled by setting the
369 * DRM_CONNECTOR_POLL_CONNECT and DRM_CONNECTOR_POLL_DISCONNECT flags. On
370 * connectors where probing live outputs can result in visual distortion drivers
371 * should not set the DRM_CONNECTOR_POLL_DISCONNECT flag to avoid this.
372 * Connectors which have no flag or only DRM_CONNECTOR_POLL_HPD set are
373 * completely ignored by the polling logic.
375 * Note that a connector can be both polled and probed from the hotplug handler,
376 * in case the hotplug interrupt is known to be unreliable.
378 void drm_kms_helper_poll_init(struct drm_device *dev)
380 INIT_DELAYED_WORK(&dev->mode_config.output_poll_work, output_poll_execute);
381 dev->mode_config.poll_enabled = true;
383 drm_kms_helper_poll_enable(dev);
385 EXPORT_SYMBOL(drm_kms_helper_poll_init);
388 * drm_kms_helper_poll_fini - disable output polling and clean it up
389 * @dev: drm_device
391 void drm_kms_helper_poll_fini(struct drm_device *dev)
393 drm_kms_helper_poll_disable(dev);
395 EXPORT_SYMBOL(drm_kms_helper_poll_fini);
398 * drm_helper_hpd_irq_event - hotplug processing
399 * @dev: drm_device
401 * Drivers can use this helper function to run a detect cycle on all connectors
402 * which have the DRM_CONNECTOR_POLL_HPD flag set in their &polled member. All
403 * other connectors are ignored, which is useful to avoid reprobing fixed
404 * panels.
406 * This helper function is useful for drivers which can't or don't track hotplug
407 * interrupts for each connector.
409 * Drivers which support hotplug interrupts for each connector individually and
410 * which have a more fine-grained detect logic should bypass this code and
411 * directly call drm_kms_helper_hotplug_event() in case the connector state
412 * changed.
414 * This function must be called from process context with no mode
415 * setting locks held.
417 * Note that a connector can be both polled and probed from the hotplug handler,
418 * in case the hotplug interrupt is known to be unreliable.
420 bool drm_helper_hpd_irq_event(struct drm_device *dev)
422 struct drm_connector *connector;
423 enum drm_connector_status old_status;
424 bool changed = false;
426 if (!dev->mode_config.poll_enabled)
427 return false;
429 mutex_lock(&dev->mode_config.mutex);
430 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
432 /* Only handle HPD capable connectors. */
433 if (!(connector->polled & DRM_CONNECTOR_POLL_HPD))
434 continue;
436 old_status = connector->status;
438 connector->status = connector->funcs->detect(connector, false);
439 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
440 connector->base.id,
441 connector->name,
442 drm_get_connector_status_name(old_status),
443 drm_get_connector_status_name(connector->status));
444 if (old_status != connector->status)
445 changed = true;
448 mutex_unlock(&dev->mode_config.mutex);
450 if (changed)
451 drm_kms_helper_hotplug_event(dev);
453 return changed;
455 EXPORT_SYMBOL(drm_helper_hpd_irq_event);