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