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