blob: b04cf5332599a2d019f5da08e17050ffadde2903 [file] [log] [blame]
Dave Airlief453ba02008-11-07 14:05:41 -08001/*
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
Paul Gortmaker2d1a8a42011-08-30 18:16:33 -040032#include <linux/export.h>
Paul Gortmaker0603ba12011-08-31 11:29:09 -040033#include <linux/moduleparam.h>
Paul Gortmaker2d1a8a42011-08-30 18:16:33 -040034
David Howells760285e2012-10-02 18:01:07 +010035#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>
Dave Airlief453ba02008-11-07 14:05:41 -080041
Daniel Vettercfc1a062012-10-27 15:52:04 +020042void drm_helper_move_panel_connectors_to_head(struct drm_device *dev)
43{
44 struct drm_connector *connector, *tmp;
45 struct list_head panel_list;
46
47 INIT_LIST_HEAD(&panel_list);
48
49 list_for_each_entry_safe(connector, tmp,
50 &dev->mode_config.connector_list, head) {
51 if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS ||
52 connector->connector_type == DRM_MODE_CONNECTOR_eDP)
53 list_move_tail(&connector->head, &panel_list);
54 }
55
56 list_splice(&panel_list, &dev->mode_config.connector_list);
57}
58EXPORT_SYMBOL(drm_helper_move_panel_connectors_to_head);
59
Chris Wilsone58f6372010-08-20 09:13:36 +010060static bool drm_kms_helper_poll = true;
61module_param_named(poll, drm_kms_helper_poll, bool, 0600);
62
yakui_zhao67149772009-04-02 11:52:12 +080063static void drm_mode_validate_flag(struct drm_connector *connector,
64 int flags)
65{
Sascha Hauera1178ca2012-02-01 11:38:30 +010066 struct drm_display_mode *mode;
yakui_zhao67149772009-04-02 11:52:12 +080067
68 if (flags == (DRM_MODE_FLAG_DBLSCAN | DRM_MODE_FLAG_INTERLACE))
69 return;
70
Sascha Hauera1178ca2012-02-01 11:38:30 +010071 list_for_each_entry(mode, &connector->modes, head) {
yakui_zhao67149772009-04-02 11:52:12 +080072 if ((mode->flags & DRM_MODE_FLAG_INTERLACE) &&
73 !(flags & DRM_MODE_FLAG_INTERLACE))
74 mode->status = MODE_NO_INTERLACE;
75 if ((mode->flags & DRM_MODE_FLAG_DBLSCAN) &&
76 !(flags & DRM_MODE_FLAG_DBLSCAN))
77 mode->status = MODE_NO_DBLESCAN;
78 }
79
80 return;
81}
82
Dave Airlief453ba02008-11-07 14:05:41 -080083/**
Dave Airlie38651672010-03-30 05:34:13 +000084 * drm_helper_probe_single_connector_modes - get complete set of display modes
Dave Airlief453ba02008-11-07 14:05:41 -080085 * @dev: DRM device
86 * @maxX: max width for modes
87 * @maxY: max height for modes
88 *
89 * LOCKING:
90 * Caller must hold mode config lock.
91 *
92 * Based on @dev's mode_config layout, scan all the connectors and try to detect
93 * modes on them. Modes will first be added to the connector's probed_modes
94 * list, then culled (based on validity and the @maxX, @maxY parameters) and
95 * put into the normal modes list.
96 *
97 * Intended to be used either at bootup time or when major configuration
98 * changes have occurred.
99 *
100 * FIXME: take into account monitor limits
Jesse Barnes40a518d2009-01-12 12:05:32 -0800101 *
102 * RETURNS:
103 * Number of modes found on @connector.
Dave Airlief453ba02008-11-07 14:05:41 -0800104 */
Jesse Barnes40a518d2009-01-12 12:05:32 -0800105int drm_helper_probe_single_connector_modes(struct drm_connector *connector,
106 uint32_t maxX, uint32_t maxY)
Dave Airlief453ba02008-11-07 14:05:41 -0800107{
108 struct drm_device *dev = connector->dev;
Sascha Hauera1178ca2012-02-01 11:38:30 +0100109 struct drm_display_mode *mode;
Dave Airlief453ba02008-11-07 14:05:41 -0800110 struct drm_connector_helper_funcs *connector_funcs =
111 connector->helper_private;
Jesse Barnes40a518d2009-01-12 12:05:32 -0800112 int count = 0;
yakui_zhao67149772009-04-02 11:52:12 +0800113 int mode_flags = 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800114
Jerome Glisse94401062010-07-15 15:43:25 -0400115 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", connector->base.id,
116 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -0800117 /* set all modes to the unverified state */
Sascha Hauera1178ca2012-02-01 11:38:30 +0100118 list_for_each_entry(mode, &connector->modes, head)
Dave Airlief453ba02008-11-07 14:05:41 -0800119 mode->status = MODE_UNVERIFIED;
120
Dave Airlied50ba252009-09-23 14:44:08 +1000121 if (connector->force) {
122 if (connector->force == DRM_FORCE_ON)
123 connector->status = connector_status_connected;
124 else
125 connector->status = connector_status_disconnected;
126 if (connector->funcs->force)
127 connector->funcs->force(connector);
Chris Wilsone58f6372010-08-20 09:13:36 +0100128 } else {
Chris Wilson930a9e22010-09-14 11:07:23 +0100129 connector->status = connector->funcs->detect(connector, true);
Chris Wilson551402a2010-09-06 23:53:47 +0100130 drm_kms_helper_poll_enable(dev);
Chris Wilsone58f6372010-08-20 09:13:36 +0100131 }
Dave Airlief453ba02008-11-07 14:05:41 -0800132
133 if (connector->status == connector_status_disconnected) {
Jerome Glisse94401062010-07-15 15:43:25 -0400134 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] disconnected\n",
135 connector->base.id, drm_get_connector_name(connector));
Zhao Yakui72539832010-03-04 08:25:55 +0000136 drm_mode_connector_update_edid_property(connector, NULL);
Adam Jackson620f3782009-09-08 11:51:46 +1000137 goto prune;
Dave Airlief453ba02008-11-07 14:05:41 -0800138 }
139
Carsten Emdeda0df922012-03-18 22:37:33 +0100140#ifdef CONFIG_DRM_LOAD_EDID_FIRMWARE
141 count = drm_load_edid_firmware(connector);
142 if (count == 0)
143#endif
144 count = (*connector_funcs->get_modes)(connector);
145
Chris Wilsonc7ef35a2010-09-05 16:55:25 +0100146 if (count == 0 && connector->status == connector_status_connected)
Adam Jackson9632b412009-11-23 14:23:07 -0500147 count = drm_add_modes_noedid(connector, 1024, 768);
Chris Wilsonc7ef35a2010-09-05 16:55:25 +0100148 if (count == 0)
149 goto prune;
Dave Airlief453ba02008-11-07 14:05:41 -0800150
Jesse Barnes40a518d2009-01-12 12:05:32 -0800151 drm_mode_connector_list_update(connector);
Dave Airlief453ba02008-11-07 14:05:41 -0800152
153 if (maxX && maxY)
154 drm_mode_validate_size(dev, &connector->modes, maxX,
155 maxY, 0);
yakui_zhao67149772009-04-02 11:52:12 +0800156
157 if (connector->interlace_allowed)
158 mode_flags |= DRM_MODE_FLAG_INTERLACE;
159 if (connector->doublescan_allowed)
160 mode_flags |= DRM_MODE_FLAG_DBLSCAN;
161 drm_mode_validate_flag(connector, mode_flags);
162
Sascha Hauera1178ca2012-02-01 11:38:30 +0100163 list_for_each_entry(mode, &connector->modes, head) {
Dave Airlief453ba02008-11-07 14:05:41 -0800164 if (mode->status == MODE_OK)
165 mode->status = connector_funcs->mode_valid(connector,
166 mode);
167 }
168
Adam Jackson620f3782009-09-08 11:51:46 +1000169prune:
Dave Airlief453ba02008-11-07 14:05:41 -0800170 drm_mode_prune_invalid(dev, &connector->modes, true);
171
Jesse Barnes40a518d2009-01-12 12:05:32 -0800172 if (list_empty(&connector->modes))
173 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800174
175 drm_mode_sort(&connector->modes);
176
Jerome Glisse94401062010-07-15 15:43:25 -0400177 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] probed modes :\n", connector->base.id,
178 drm_get_connector_name(connector));
Sascha Hauera1178ca2012-02-01 11:38:30 +0100179 list_for_each_entry(mode, &connector->modes, head) {
Dave Airlief453ba02008-11-07 14:05:41 -0800180 mode->vrefresh = drm_mode_vrefresh(mode);
181
182 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
183 drm_mode_debug_printmodeline(mode);
184 }
Jesse Barnes40a518d2009-01-12 12:05:32 -0800185
186 return count;
Dave Airlief453ba02008-11-07 14:05:41 -0800187}
188EXPORT_SYMBOL(drm_helper_probe_single_connector_modes);
189
Dave Airlief453ba02008-11-07 14:05:41 -0800190/**
Keith Packardc9fb15f2009-05-30 20:42:28 -0700191 * drm_helper_encoder_in_use - check if a given encoder is in use
192 * @encoder: encoder to check
193 *
194 * LOCKING:
195 * Caller must hold mode config lock.
196 *
197 * Walk @encoders's DRM device's mode_config and see if it's in use.
198 *
199 * RETURNS:
200 * True if @encoder is part of the mode_config, false otherwise.
201 */
202bool drm_helper_encoder_in_use(struct drm_encoder *encoder)
203{
204 struct drm_connector *connector;
205 struct drm_device *dev = encoder->dev;
206 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
207 if (connector->encoder == encoder)
208 return true;
209 return false;
210}
211EXPORT_SYMBOL(drm_helper_encoder_in_use);
212
213/**
Dave Airlief453ba02008-11-07 14:05:41 -0800214 * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config
215 * @crtc: CRTC to check
216 *
217 * LOCKING:
218 * Caller must hold mode config lock.
219 *
220 * Walk @crtc's DRM device's mode_config and see if it's in use.
221 *
222 * RETURNS:
223 * True if @crtc is part of the mode_config, false otherwise.
224 */
225bool drm_helper_crtc_in_use(struct drm_crtc *crtc)
226{
227 struct drm_encoder *encoder;
228 struct drm_device *dev = crtc->dev;
229 /* FIXME: Locking around list access? */
230 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
Keith Packardc9fb15f2009-05-30 20:42:28 -0700231 if (encoder->crtc == crtc && drm_helper_encoder_in_use(encoder))
Dave Airlief453ba02008-11-07 14:05:41 -0800232 return true;
233 return false;
234}
235EXPORT_SYMBOL(drm_helper_crtc_in_use);
236
Ben Skeggs86a1b9d2010-07-01 16:49:57 +1000237static void
238drm_encoder_disable(struct drm_encoder *encoder)
239{
240 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
241
242 if (encoder_funcs->disable)
243 (*encoder_funcs->disable)(encoder);
244 else
245 (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
246}
247
Dave Airlief453ba02008-11-07 14:05:41 -0800248/**
David John89347bb2009-12-31 12:00:46 +0530249 * drm_helper_disable_unused_functions - disable unused objects
Dave Airlief453ba02008-11-07 14:05:41 -0800250 * @dev: DRM device
251 *
252 * LOCKING:
253 * Caller must hold mode config lock.
254 *
255 * If an connector or CRTC isn't part of @dev's mode_config, it can be disabled
256 * by calling its dpms function, which should power it off.
257 */
258void drm_helper_disable_unused_functions(struct drm_device *dev)
259{
260 struct drm_encoder *encoder;
Dave Airliea3a05442009-08-31 15:16:30 +1000261 struct drm_connector *connector;
Dave Airlief453ba02008-11-07 14:05:41 -0800262 struct drm_crtc *crtc;
263
Dave Airliea3a05442009-08-31 15:16:30 +1000264 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
265 if (!connector->encoder)
266 continue;
267 if (connector->status == connector_status_disconnected)
268 connector->encoder = NULL;
269 }
270
Dave Airlief453ba02008-11-07 14:05:41 -0800271 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Dave Airlie929710212010-12-21 12:47:56 +1000272 if (!drm_helper_encoder_in_use(encoder)) {
Ben Skeggs86a1b9d2010-07-01 16:49:57 +1000273 drm_encoder_disable(encoder);
Dave Airlie9c552dd2009-09-02 14:00:11 +1000274 /* disconnector encoder from any connector */
275 encoder->crtc = NULL;
Dave Airliea3a05442009-08-31 15:16:30 +1000276 }
Dave Airlief453ba02008-11-07 14:05:41 -0800277 }
278
279 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
280 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
281 crtc->enabled = drm_helper_crtc_in_use(crtc);
282 if (!crtc->enabled) {
Alex Deucher5c8d7172010-06-11 17:04:35 -0400283 if (crtc_funcs->disable)
284 (*crtc_funcs->disable)(crtc);
285 else
286 (*crtc_funcs->dpms)(crtc, DRM_MODE_DPMS_OFF);
Dave Airlief453ba02008-11-07 14:05:41 -0800287 crtc->fb = NULL;
288 }
289 }
290}
291EXPORT_SYMBOL(drm_helper_disable_unused_functions);
292
Jesse Barnes7bec7562009-02-23 16:09:34 -0800293/**
294 * drm_encoder_crtc_ok - can a given crtc drive a given encoder?
295 * @encoder: encoder to test
296 * @crtc: crtc to test
297 *
298 * Return false if @encoder can't be driven by @crtc, true otherwise.
299 */
300static bool drm_encoder_crtc_ok(struct drm_encoder *encoder,
301 struct drm_crtc *crtc)
302{
303 struct drm_device *dev;
304 struct drm_crtc *tmp;
305 int crtc_mask = 1;
306
Joe Perchesfce7d612010-10-30 21:08:30 +0000307 WARN(!crtc, "checking null crtc?\n");
Jesse Barnes7bec7562009-02-23 16:09:34 -0800308
309 dev = crtc->dev;
310
311 list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
312 if (tmp == crtc)
313 break;
314 crtc_mask <<= 1;
315 }
316
317 if (encoder->possible_crtcs & crtc_mask)
318 return true;
319 return false;
320}
321
322/*
323 * Check the CRTC we're going to map each output to vs. its current
324 * CRTC. If they don't match, we have to disable the output and the CRTC
325 * since the driver will have to re-route things.
326 */
327static void
328drm_crtc_prepare_encoders(struct drm_device *dev)
329{
330 struct drm_encoder_helper_funcs *encoder_funcs;
331 struct drm_encoder *encoder;
332
333 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
334 encoder_funcs = encoder->helper_private;
335 /* Disable unused encoders */
336 if (encoder->crtc == NULL)
Ben Skeggs86a1b9d2010-07-01 16:49:57 +1000337 drm_encoder_disable(encoder);
Jesse Barnes7bec7562009-02-23 16:09:34 -0800338 /* Disable encoders whose CRTC is about to change */
339 if (encoder_funcs->get_crtc &&
340 encoder->crtc != (*encoder_funcs->get_crtc)(encoder))
Ben Skeggs86a1b9d2010-07-01 16:49:57 +1000341 drm_encoder_disable(encoder);
Jesse Barnes7bec7562009-02-23 16:09:34 -0800342 }
343}
344
Dave Airlief453ba02008-11-07 14:05:41 -0800345/**
346 * drm_crtc_set_mode - set a mode
347 * @crtc: CRTC to program
348 * @mode: mode to use
Alex Deucher4c9287c2012-11-09 17:26:32 +0000349 * @x: horizontal offset into the surface
350 * @y: vertical offset into the surface
Dave Airlief453ba02008-11-07 14:05:41 -0800351 *
352 * LOCKING:
353 * Caller must hold mode config lock.
354 *
355 * Try to set @mode on @crtc. Give @crtc and its associated connectors a chance
356 * to fixup or reject the mode prior to trying to set it.
357 *
358 * RETURNS:
359 * True if the mode was set successfully, or false otherwise.
360 */
361bool drm_crtc_helper_set_mode(struct drm_crtc *crtc,
362 struct drm_display_mode *mode,
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -0500363 int x, int y,
364 struct drm_framebuffer *old_fb)
Dave Airlief453ba02008-11-07 14:05:41 -0800365{
366 struct drm_device *dev = crtc->dev;
Mario Kleiner27641c32010-10-23 04:20:23 +0200367 struct drm_display_mode *adjusted_mode, saved_mode, saved_hwmode;
Dave Airlief453ba02008-11-07 14:05:41 -0800368 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
369 struct drm_encoder_helper_funcs *encoder_funcs;
370 int saved_x, saved_y;
371 struct drm_encoder *encoder;
372 bool ret = true;
373
Dave Airlief453ba02008-11-07 14:05:41 -0800374 crtc->enabled = drm_helper_crtc_in_use(crtc);
Dave Airlief453ba02008-11-07 14:05:41 -0800375 if (!crtc->enabled)
376 return true;
377
Chris Wilson021a8452011-01-28 11:31:56 +0000378 adjusted_mode = drm_mode_duplicate(dev, mode);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200379 if (!adjusted_mode)
380 return false;
Chris Wilson021a8452011-01-28 11:31:56 +0000381
Mario Kleiner27641c32010-10-23 04:20:23 +0200382 saved_hwmode = crtc->hwmode;
Dave Airlief453ba02008-11-07 14:05:41 -0800383 saved_mode = crtc->mode;
384 saved_x = crtc->x;
385 saved_y = crtc->y;
386
387 /* Update crtc values up front so the driver can rely on them for mode
388 * setting.
389 */
390 crtc->mode = *mode;
391 crtc->x = x;
392 crtc->y = y;
393
Dave Airlief453ba02008-11-07 14:05:41 -0800394 /* Pass our mode to the connectors and the CRTC to give them a chance to
395 * adjust it according to limitations or connector properties, and also
396 * a chance to reject the mode entirely.
397 */
398 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
399
400 if (encoder->crtc != crtc)
401 continue;
402 encoder_funcs = encoder->helper_private;
403 if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
404 adjusted_mode))) {
Adam Jackson836e53d2011-10-10 16:21:27 -0400405 DRM_DEBUG_KMS("Encoder fixup failed\n");
Dave Airlief453ba02008-11-07 14:05:41 -0800406 goto done;
407 }
408 }
409
410 if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) {
Adam Jackson836e53d2011-10-10 16:21:27 -0400411 DRM_DEBUG_KMS("CRTC fixup failed\n");
Dave Airlief453ba02008-11-07 14:05:41 -0800412 goto done;
413 }
Jerome Glisse94401062010-07-15 15:43:25 -0400414 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -0800415
416 /* Prepare the encoders and CRTCs before setting the mode. */
417 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
418
419 if (encoder->crtc != crtc)
420 continue;
421 encoder_funcs = encoder->helper_private;
422 /* Disable the encoders as the first thing we do. */
423 encoder_funcs->prepare(encoder);
424 }
425
Jesse Barnes7bec7562009-02-23 16:09:34 -0800426 drm_crtc_prepare_encoders(dev);
427
Dave Airlief453ba02008-11-07 14:05:41 -0800428 crtc_funcs->prepare(crtc);
429
430 /* Set up the DPLL and any encoders state that needs to adjust or depend
431 * on the DPLL.
432 */
Chris Wilson5c3b82e2009-02-11 13:25:09 +0000433 ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
434 if (!ret)
435 goto done;
Dave Airlief453ba02008-11-07 14:05:41 -0800436
437 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
438
439 if (encoder->crtc != crtc)
440 continue;
441
Jerome Glisse94401062010-07-15 15:43:25 -0400442 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
443 encoder->base.id, drm_get_encoder_name(encoder),
444 mode->base.id, mode->name);
Dave Airlief453ba02008-11-07 14:05:41 -0800445 encoder_funcs = encoder->helper_private;
446 encoder_funcs->mode_set(encoder, mode, adjusted_mode);
447 }
448
449 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
450 crtc_funcs->commit(crtc);
451
452 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
453
454 if (encoder->crtc != crtc)
455 continue;
456
457 encoder_funcs = encoder->helper_private;
458 encoder_funcs->commit(encoder);
459
460 }
461
Mario Kleiner27641c32010-10-23 04:20:23 +0200462 /* Store real post-adjustment hardware mode. */
463 crtc->hwmode = *adjusted_mode;
464
465 /* Calculate and store various constants which
466 * are later needed by vblank and swap-completion
467 * timestamping. They are derived from true hwmode.
468 */
469 drm_calc_timestamping_constants(crtc);
470
Dave Airlief453ba02008-11-07 14:05:41 -0800471 /* FIXME: add subpixel order */
472done:
Chris Wilson021a8452011-01-28 11:31:56 +0000473 drm_mode_destroy(dev, adjusted_mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800474 if (!ret) {
Mario Kleiner27641c32010-10-23 04:20:23 +0200475 crtc->hwmode = saved_hwmode;
Dave Airlief453ba02008-11-07 14:05:41 -0800476 crtc->mode = saved_mode;
477 crtc->x = saved_x;
478 crtc->y = saved_y;
479 }
480
481 return ret;
482}
483EXPORT_SYMBOL(drm_crtc_helper_set_mode);
484
485
Chris Wilson6eebd6b2011-11-28 21:10:05 +0000486static int
487drm_crtc_helper_disable(struct drm_crtc *crtc)
488{
489 struct drm_device *dev = crtc->dev;
490 struct drm_connector *connector;
491 struct drm_encoder *encoder;
492
493 /* Decouple all encoders and their attached connectors from this crtc */
494 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
495 if (encoder->crtc != crtc)
496 continue;
497
498 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
499 if (connector->encoder != encoder)
500 continue;
501
502 connector->encoder = NULL;
503 }
504 }
505
506 drm_helper_disable_unused_functions(dev);
507 return 0;
508}
509
Dave Airlief453ba02008-11-07 14:05:41 -0800510/**
511 * drm_crtc_helper_set_config - set a new config from userspace
512 * @crtc: CRTC to setup
513 * @crtc_info: user provided configuration
514 * @new_mode: new mode to set
515 * @connector_set: set of connectors for the new config
516 * @fb: new framebuffer
517 *
518 * LOCKING:
519 * Caller must hold mode config lock.
520 *
521 * Setup a new configuration, provided by the user in @crtc_info, and enable
522 * it.
523 *
524 * RETURNS:
525 * Zero. (FIXME)
526 */
527int drm_crtc_helper_set_config(struct drm_mode_set *set)
528{
529 struct drm_device *dev;
Maarten Maathuise67aae72009-08-27 10:18:29 +0200530 struct drm_crtc *save_crtcs, *new_crtc, *crtc;
531 struct drm_encoder *save_encoders, *new_encoder, *encoder;
Jesse Barnes7bec7562009-02-23 16:09:34 -0800532 struct drm_framebuffer *old_fb = NULL;
Jakob Bornecrantz4cb72b12009-08-03 13:43:59 +0100533 bool mode_changed = false; /* if true do a full mode set */
534 bool fb_changed = false; /* if true and !mode_changed just do a flip */
Maarten Maathuise67aae72009-08-27 10:18:29 +0200535 struct drm_connector *save_connectors, *connector;
Dave Airlief453ba02008-11-07 14:05:41 -0800536 int count = 0, ro, fail = 0;
537 struct drm_crtc_helper_funcs *crtc_funcs;
Jesse Barnesc5006cfe2011-11-07 10:39:57 -0800538 struct drm_mode_set save_set;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +0200539 int ret;
Keith Packardbf9dc102010-11-26 10:45:58 -0800540 int i;
Dave Airlief453ba02008-11-07 14:05:41 -0800541
Zhao Yakui58367ed2009-07-20 13:48:07 +0800542 DRM_DEBUG_KMS("\n");
Dave Airlief453ba02008-11-07 14:05:41 -0800543
544 if (!set)
545 return -EINVAL;
546
547 if (!set->crtc)
548 return -EINVAL;
549
550 if (!set->crtc->helper_private)
551 return -EINVAL;
552
553 crtc_funcs = set->crtc->helper_private;
554
Chris Wilsonede3ff52011-01-31 11:16:33 +0000555 if (!set->mode)
556 set->fb = NULL;
557
Jerome Glisse94401062010-07-15 15:43:25 -0400558 if (set->fb) {
559 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
560 set->crtc->base.id, set->fb->base.id,
561 (int)set->num_connectors, set->x, set->y);
562 } else {
Chris Wilsonede3ff52011-01-31 11:16:33 +0000563 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Chris Wilson6eebd6b2011-11-28 21:10:05 +0000564 return drm_crtc_helper_disable(set->crtc);
Jerome Glisse94401062010-07-15 15:43:25 -0400565 }
Dave Airlief453ba02008-11-07 14:05:41 -0800566
567 dev = set->crtc->dev;
568
Maarten Maathuise67aae72009-08-27 10:18:29 +0200569 /* Allocate space for the backup of all (non-pointer) crtc, encoder and
570 * connector data. */
571 save_crtcs = kzalloc(dev->mode_config.num_crtc *
572 sizeof(struct drm_crtc), GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -0800573 if (!save_crtcs)
574 return -ENOMEM;
575
Maarten Maathuise67aae72009-08-27 10:18:29 +0200576 save_encoders = kzalloc(dev->mode_config.num_encoder *
577 sizeof(struct drm_encoder), GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -0800578 if (!save_encoders) {
579 kfree(save_crtcs);
580 return -ENOMEM;
581 }
582
Maarten Maathuise67aae72009-08-27 10:18:29 +0200583 save_connectors = kzalloc(dev->mode_config.num_connector *
584 sizeof(struct drm_connector), GFP_KERNEL);
585 if (!save_connectors) {
586 kfree(save_crtcs);
587 kfree(save_encoders);
588 return -ENOMEM;
589 }
590
591 /* Copy data. Note that driver private data is not affected.
592 * Should anything bad happen only the expected state is
593 * restored, not the drivers personal bookkeeping.
594 */
595 count = 0;
596 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
597 save_crtcs[count++] = *crtc;
598 }
599
600 count = 0;
601 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
602 save_encoders[count++] = *encoder;
603 }
604
605 count = 0;
606 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
607 save_connectors[count++] = *connector;
608 }
609
Jesse Barnesc5006cfe2011-11-07 10:39:57 -0800610 save_set.crtc = set->crtc;
611 save_set.mode = &set->crtc->mode;
612 save_set.x = set->crtc->x;
613 save_set.y = set->crtc->y;
614 save_set.fb = set->crtc->fb;
615
Dave Airlief453ba02008-11-07 14:05:41 -0800616 /* We should be able to check here if the fb has the same properties
617 * and then just flip_or_move it */
618 if (set->crtc->fb != set->fb) {
Jesse Barnes712531b2009-01-09 13:56:14 -0800619 /* If we have no fb then treat it as a full mode set */
Jesse Barnes7bec7562009-02-23 16:09:34 -0800620 if (set->crtc->fb == NULL) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800621 DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
Jesse Barnes712531b2009-01-09 13:56:14 -0800622 mode_changed = true;
Jakob Bornecrantz4cb72b12009-08-03 13:43:59 +0100623 } else if (set->fb == NULL) {
624 mode_changed = true;
Jesse Barnes46e48452011-06-24 12:19:26 -0700625 } else if (set->fb->depth != set->crtc->fb->depth) {
626 mode_changed = true;
627 } else if (set->fb->bits_per_pixel !=
628 set->crtc->fb->bits_per_pixel) {
629 mode_changed = true;
Dave Airlie8dff4742010-02-11 14:28:58 +1000630 } else
Jesse Barnes712531b2009-01-09 13:56:14 -0800631 fb_changed = true;
Dave Airlief453ba02008-11-07 14:05:41 -0800632 }
633
634 if (set->x != set->crtc->x || set->y != set->crtc->y)
Jesse Barnes712531b2009-01-09 13:56:14 -0800635 fb_changed = true;
Dave Airlief453ba02008-11-07 14:05:41 -0800636
637 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800638 DRM_DEBUG_KMS("modes are different, full mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -0800639 drm_mode_debug_printmodeline(&set->crtc->mode);
640 drm_mode_debug_printmodeline(set->mode);
Jesse Barnes712531b2009-01-09 13:56:14 -0800641 mode_changed = true;
Dave Airlief453ba02008-11-07 14:05:41 -0800642 }
643
644 /* a) traverse passed in connector list and get encoders for them */
645 count = 0;
646 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
647 struct drm_connector_helper_funcs *connector_funcs =
648 connector->helper_private;
Dave Airlief453ba02008-11-07 14:05:41 -0800649 new_encoder = connector->encoder;
650 for (ro = 0; ro < set->num_connectors; ro++) {
651 if (set->connectors[ro] == connector) {
652 new_encoder = connector_funcs->best_encoder(connector);
653 /* if we can't get an encoder for a connector
654 we are setting now - then fail */
655 if (new_encoder == NULL)
656 /* don't break so fail path works correct */
657 fail = 1;
658 break;
659 }
660 }
661
662 if (new_encoder != connector->encoder) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800663 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
Jesse Barnes712531b2009-01-09 13:56:14 -0800664 mode_changed = true;
Maarten Maathuisff846ab2009-08-19 00:56:45 +0200665 /* If the encoder is reused for another connector, then
666 * the appropriate crtc will be set later.
667 */
Maarten Maathuisff6fdbe2009-09-01 03:39:04 +0200668 if (connector->encoder)
669 connector->encoder->crtc = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800670 connector->encoder = new_encoder;
671 }
672 }
673
674 if (fail) {
675 ret = -EINVAL;
Maarten Maathuise67aae72009-08-27 10:18:29 +0200676 goto fail;
Dave Airlief453ba02008-11-07 14:05:41 -0800677 }
678
679 count = 0;
680 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
681 if (!connector->encoder)
682 continue;
683
Dave Airlief453ba02008-11-07 14:05:41 -0800684 if (connector->encoder->crtc == set->crtc)
685 new_crtc = NULL;
686 else
687 new_crtc = connector->encoder->crtc;
688
689 for (ro = 0; ro < set->num_connectors; ro++) {
690 if (set->connectors[ro] == connector)
691 new_crtc = set->crtc;
692 }
Jesse Barnes7bec7562009-02-23 16:09:34 -0800693
694 /* Make sure the new CRTC will work with the encoder */
695 if (new_crtc &&
696 !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
697 ret = -EINVAL;
Maarten Maathuise67aae72009-08-27 10:18:29 +0200698 goto fail;
Jesse Barnes7bec7562009-02-23 16:09:34 -0800699 }
Dave Airlief453ba02008-11-07 14:05:41 -0800700 if (new_crtc != connector->encoder->crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800701 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
Jesse Barnes712531b2009-01-09 13:56:14 -0800702 mode_changed = true;
Dave Airlief453ba02008-11-07 14:05:41 -0800703 connector->encoder->crtc = new_crtc;
704 }
Jerome Glisse94401062010-07-15 15:43:25 -0400705 if (new_crtc) {
706 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
707 connector->base.id, drm_get_connector_name(connector),
708 new_crtc->base.id);
709 } else {
710 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
711 connector->base.id, drm_get_connector_name(connector));
712 }
Dave Airlief453ba02008-11-07 14:05:41 -0800713 }
714
715 /* mode_set_base is not a required function */
Jesse Barnes712531b2009-01-09 13:56:14 -0800716 if (fb_changed && !crtc_funcs->mode_set_base)
717 mode_changed = true;
Dave Airlief453ba02008-11-07 14:05:41 -0800718
Jesse Barnes712531b2009-01-09 13:56:14 -0800719 if (mode_changed) {
Chris Wilson9334ef72011-01-28 11:53:03 +0000720 set->crtc->enabled = drm_helper_crtc_in_use(set->crtc);
721 if (set->crtc->enabled) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800722 DRM_DEBUG_KMS("attempting to set mode from"
723 " userspace\n");
Dave Airlief453ba02008-11-07 14:05:41 -0800724 drm_mode_debug_printmodeline(set->mode);
Chris Wilson356ad3c2010-09-09 09:41:32 +0100725 old_fb = set->crtc->fb;
726 set->crtc->fb = set->fb;
Dave Airlief453ba02008-11-07 14:05:41 -0800727 if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -0500728 set->x, set->y,
729 old_fb)) {
Jerome Glisse94401062010-07-15 15:43:25 -0400730 DRM_ERROR("failed to set mode on [CRTC:%d]\n",
731 set->crtc->base.id);
Chris Wilson0ba41e42011-01-08 15:10:41 +0000732 set->crtc->fb = old_fb;
Dave Airlief453ba02008-11-07 14:05:41 -0800733 ret = -EINVAL;
Maarten Maathuise67aae72009-08-27 10:18:29 +0200734 goto fail;
Dave Airlief453ba02008-11-07 14:05:41 -0800735 }
Keith Packard811aaa52011-02-03 16:57:28 -0800736 DRM_DEBUG_KMS("Setting connector DPMS state to on\n");
737 for (i = 0; i < set->num_connectors; i++) {
738 DRM_DEBUG_KMS("\t[CONNECTOR:%d:%s] set DPMS on\n", set->connectors[i]->base.id,
739 drm_get_connector_name(set->connectors[i]));
Rob Clarkc7548832011-12-15 14:53:24 -0600740 set->connectors[i]->funcs->dpms(set->connectors[i], DRM_MODE_DPMS_ON);
Keith Packard811aaa52011-02-03 16:57:28 -0800741 }
Dave Airlief453ba02008-11-07 14:05:41 -0800742 }
743 drm_helper_disable_unused_functions(dev);
Jesse Barnes712531b2009-01-09 13:56:14 -0800744 } else if (fb_changed) {
Ben Skeggs9b1596a2009-09-18 10:43:52 +1000745 set->crtc->x = set->x;
746 set->crtc->y = set->y;
747
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -0500748 old_fb = set->crtc->fb;
Dave Airlief453ba02008-11-07 14:05:41 -0800749 if (set->crtc->fb != set->fb)
750 set->crtc->fb = set->fb;
Chris Wilson5c3b82e2009-02-11 13:25:09 +0000751 ret = crtc_funcs->mode_set_base(set->crtc,
752 set->x, set->y, old_fb);
Chris Wilson0ba41e42011-01-08 15:10:41 +0000753 if (ret != 0) {
754 set->crtc->fb = old_fb;
Maarten Maathuise67aae72009-08-27 10:18:29 +0200755 goto fail;
Chris Wilson0ba41e42011-01-08 15:10:41 +0000756 }
Dave Airlief453ba02008-11-07 14:05:41 -0800757 }
758
Maarten Maathuise67aae72009-08-27 10:18:29 +0200759 kfree(save_connectors);
Dave Airlief453ba02008-11-07 14:05:41 -0800760 kfree(save_encoders);
761 kfree(save_crtcs);
762 return 0;
763
Maarten Maathuise67aae72009-08-27 10:18:29 +0200764fail:
765 /* Restore all previous data. */
Dave Airlief453ba02008-11-07 14:05:41 -0800766 count = 0;
Maarten Maathuise67aae72009-08-27 10:18:29 +0200767 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
768 *crtc = save_crtcs[count++];
769 }
Chris Wilsone62fb642009-02-11 16:39:21 +0000770
Maarten Maathuise67aae72009-08-27 10:18:29 +0200771 count = 0;
772 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
773 *encoder = save_encoders[count++];
Chris Wilsone62fb642009-02-11 16:39:21 +0000774 }
Maarten Maathuise67aae72009-08-27 10:18:29 +0200775
Dave Airlief453ba02008-11-07 14:05:41 -0800776 count = 0;
777 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Maarten Maathuise67aae72009-08-27 10:18:29 +0200778 *connector = save_connectors[count++];
Dave Airlief453ba02008-11-07 14:05:41 -0800779 }
Maarten Maathuise67aae72009-08-27 10:18:29 +0200780
Jesse Barnesc5006cfe2011-11-07 10:39:57 -0800781 /* Try to restore the config */
782 if (mode_changed &&
783 !drm_crtc_helper_set_mode(save_set.crtc, save_set.mode, save_set.x,
784 save_set.y, save_set.fb))
785 DRM_ERROR("failed to restore config after modeset failure\n");
786
Maarten Maathuise67aae72009-08-27 10:18:29 +0200787 kfree(save_connectors);
Dave Airlief453ba02008-11-07 14:05:41 -0800788 kfree(save_encoders);
Maarten Maathuise67aae72009-08-27 10:18:29 +0200789 kfree(save_crtcs);
Dave Airlief453ba02008-11-07 14:05:41 -0800790 return ret;
791}
792EXPORT_SYMBOL(drm_crtc_helper_set_config);
793
Keith Packardc9fb15f2009-05-30 20:42:28 -0700794static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder)
795{
796 int dpms = DRM_MODE_DPMS_OFF;
797 struct drm_connector *connector;
798 struct drm_device *dev = encoder->dev;
799
800 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
801 if (connector->encoder == encoder)
802 if (connector->dpms < dpms)
803 dpms = connector->dpms;
804 return dpms;
805}
806
807static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc)
808{
809 int dpms = DRM_MODE_DPMS_OFF;
810 struct drm_connector *connector;
811 struct drm_device *dev = crtc->dev;
812
813 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
814 if (connector->encoder && connector->encoder->crtc == crtc)
815 if (connector->dpms < dpms)
816 dpms = connector->dpms;
817 return dpms;
818}
819
820/**
821 * drm_helper_connector_dpms
822 * @connector affected connector
823 * @mode DPMS mode
824 *
825 * Calls the low-level connector DPMS function, then
826 * calls appropriate encoder and crtc DPMS functions as well
827 */
828void drm_helper_connector_dpms(struct drm_connector *connector, int mode)
829{
830 struct drm_encoder *encoder = connector->encoder;
831 struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
832 int old_dpms;
833
834 if (mode == connector->dpms)
835 return;
836
837 old_dpms = connector->dpms;
838 connector->dpms = mode;
839
840 /* from off to on, do crtc then encoder */
841 if (mode < old_dpms) {
842 if (crtc) {
843 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
844 if (crtc_funcs->dpms)
845 (*crtc_funcs->dpms) (crtc,
846 drm_helper_choose_crtc_dpms(crtc));
847 }
848 if (encoder) {
849 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
850 if (encoder_funcs->dpms)
851 (*encoder_funcs->dpms) (encoder,
852 drm_helper_choose_encoder_dpms(encoder));
853 }
854 }
855
856 /* from on to off, do encoder then crtc */
857 if (mode > old_dpms) {
858 if (encoder) {
859 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
860 if (encoder_funcs->dpms)
861 (*encoder_funcs->dpms) (encoder,
862 drm_helper_choose_encoder_dpms(encoder));
863 }
864 if (crtc) {
865 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
866 if (crtc_funcs->dpms)
867 (*crtc_funcs->dpms) (crtc,
868 drm_helper_choose_crtc_dpms(crtc));
869 }
870 }
871
872 return;
873}
874EXPORT_SYMBOL(drm_helper_connector_dpms);
875
Dave Airlief453ba02008-11-07 14:05:41 -0800876int drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb,
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800877 struct drm_mode_fb_cmd2 *mode_cmd)
Dave Airlief453ba02008-11-07 14:05:41 -0800878{
Ville Syrjälä01f2c772011-12-20 00:06:49 +0200879 int i;
880
Dave Airlief453ba02008-11-07 14:05:41 -0800881 fb->width = mode_cmd->width;
882 fb->height = mode_cmd->height;
Ville Syrjälä01f2c772011-12-20 00:06:49 +0200883 for (i = 0; i < 4; i++) {
884 fb->pitches[i] = mode_cmd->pitches[i];
885 fb->offsets[i] = mode_cmd->offsets[i];
886 }
Dave Airlie248dbc22011-11-29 20:02:54 +0000887 drm_fb_get_bpp_depth(mode_cmd->pixel_format, &fb->depth,
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800888 &fb->bits_per_pixel);
889 fb->pixel_format = mode_cmd->pixel_format;
Dave Airlief453ba02008-11-07 14:05:41 -0800890
891 return 0;
892}
893EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
894
895int drm_helper_resume_force_mode(struct drm_device *dev)
896{
897 struct drm_crtc *crtc;
David John89347bb2009-12-31 12:00:46 +0530898 struct drm_encoder *encoder;
899 struct drm_encoder_helper_funcs *encoder_funcs;
900 struct drm_crtc_helper_funcs *crtc_funcs;
Dave Airlief453ba02008-11-07 14:05:41 -0800901 int ret;
902
903 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
904
905 if (!crtc->enabled)
906 continue;
907
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -0500908 ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
909 crtc->x, crtc->y, crtc->fb);
Dave Airlief453ba02008-11-07 14:05:41 -0800910
911 if (ret == false)
912 DRM_ERROR("failed to set mode on crtc %p\n", crtc);
David John89347bb2009-12-31 12:00:46 +0530913
914 /* Turn off outputs that were already powered off */
915 if (drm_helper_choose_crtc_dpms(crtc)) {
916 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
917
918 if(encoder->crtc != crtc)
919 continue;
920
921 encoder_funcs = encoder->helper_private;
922 if (encoder_funcs->dpms)
923 (*encoder_funcs->dpms) (encoder,
924 drm_helper_choose_encoder_dpms(encoder));
David John89347bb2009-12-31 12:00:46 +0530925 }
Chris Wilson817e6312010-08-06 15:03:31 +0100926
927 crtc_funcs = crtc->helper_private;
928 if (crtc_funcs->dpms)
929 (*crtc_funcs->dpms) (crtc,
930 drm_helper_choose_crtc_dpms(crtc));
David John89347bb2009-12-31 12:00:46 +0530931 }
Dave Airlief453ba02008-11-07 14:05:41 -0800932 }
Zhao Yakuiaf4fcb52009-07-08 14:13:13 +0800933 /* disable the unused connectors while restoring the modesetting */
934 drm_helper_disable_unused_functions(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800935 return 0;
936}
937EXPORT_SYMBOL(drm_helper_resume_force_mode);
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000938
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000939#define DRM_OUTPUT_POLL_PERIOD (10*HZ)
Tejun Heo991ea752010-07-20 22:09:02 +0200940static void output_poll_execute(struct work_struct *work)
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000941{
Tejun Heo991ea752010-07-20 22:09:02 +0200942 struct delayed_work *delayed_work = to_delayed_work(work);
943 struct drm_device *dev = container_of(delayed_work, struct drm_device, mode_config.output_poll_work);
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000944 struct drm_connector *connector;
Keith Packardc5027de2010-11-26 10:45:59 -0800945 enum drm_connector_status old_status;
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000946 bool repoll = false, changed = false;
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000947
Chris Wilsone58f6372010-08-20 09:13:36 +0100948 if (!drm_kms_helper_poll)
949 return;
950
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000951 mutex_lock(&dev->mode_config.mutex);
952 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
953
954 /* if this is HPD or polled don't check it -
955 TV out for instance */
956 if (!connector->polled)
957 continue;
958
959 else if (connector->polled & (DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT))
960 repoll = true;
961
962 old_status = connector->status;
963 /* if we are connected and don't want to poll for disconnect
964 skip it */
965 if (old_status == connector_status_connected &&
966 !(connector->polled & DRM_CONNECTOR_POLL_DISCONNECT) &&
967 !(connector->polled & DRM_CONNECTOR_POLL_HPD))
968 continue;
969
Keith Packardc5027de2010-11-26 10:45:59 -0800970 connector->status = connector->funcs->detect(connector, false);
Chris Wilson0f168302010-12-21 22:49:28 +0000971 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %d to %d\n",
972 connector->base.id,
973 drm_get_connector_name(connector),
974 old_status, connector->status);
Keith Packardc5027de2010-11-26 10:45:59 -0800975 if (old_status != connector->status)
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000976 changed = true;
977 }
978
979 mutex_unlock(&dev->mode_config.mutex);
980
981 if (changed) {
982 /* send a uevent + call fbdev */
983 drm_sysfs_hotplug_event(dev);
984 if (dev->mode_config.funcs->output_poll_changed)
985 dev->mode_config.funcs->output_poll_changed(dev);
986 }
987
Tejun Heo9a919c42010-08-09 12:01:27 +0200988 if (repoll)
Tejun Heo3b07e9c2012-08-20 14:51:24 -0700989 schedule_delayed_work(delayed_work, DRM_OUTPUT_POLL_PERIOD);
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000990}
991
Dave Airliefbf81762010-06-01 09:09:06 +1000992void drm_kms_helper_poll_disable(struct drm_device *dev)
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000993{
Dave Airliefbf81762010-06-01 09:09:06 +1000994 if (!dev->mode_config.poll_enabled)
995 return;
Tejun Heo991ea752010-07-20 22:09:02 +0200996 cancel_delayed_work_sync(&dev->mode_config.output_poll_work);
Dave Airliefbf81762010-06-01 09:09:06 +1000997}
998EXPORT_SYMBOL(drm_kms_helper_poll_disable);
999
1000void drm_kms_helper_poll_enable(struct drm_device *dev)
1001{
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001002 bool poll = false;
Dave Airliefbf81762010-06-01 09:09:06 +10001003 struct drm_connector *connector;
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001004
Chris Wilsone58f6372010-08-20 09:13:36 +01001005 if (!dev->mode_config.poll_enabled || !drm_kms_helper_poll)
1006 return;
1007
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001008 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1009 if (connector->polled)
1010 poll = true;
1011 }
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001012
Tejun Heo9a919c42010-08-09 12:01:27 +02001013 if (poll)
Tejun Heo3b07e9c2012-08-20 14:51:24 -07001014 schedule_delayed_work(&dev->mode_config.output_poll_work, DRM_OUTPUT_POLL_PERIOD);
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001015}
Dave Airliefbf81762010-06-01 09:09:06 +10001016EXPORT_SYMBOL(drm_kms_helper_poll_enable);
1017
1018void drm_kms_helper_poll_init(struct drm_device *dev)
1019{
Tejun Heo991ea752010-07-20 22:09:02 +02001020 INIT_DELAYED_WORK(&dev->mode_config.output_poll_work, output_poll_execute);
Dave Airliefbf81762010-06-01 09:09:06 +10001021 dev->mode_config.poll_enabled = true;
1022
1023 drm_kms_helper_poll_enable(dev);
1024}
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001025EXPORT_SYMBOL(drm_kms_helper_poll_init);
1026
1027void drm_kms_helper_poll_fini(struct drm_device *dev)
1028{
Dave Airliefbf81762010-06-01 09:09:06 +10001029 drm_kms_helper_poll_disable(dev);
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001030}
1031EXPORT_SYMBOL(drm_kms_helper_poll_fini);
1032
1033void drm_helper_hpd_irq_event(struct drm_device *dev)
1034{
1035 if (!dev->mode_config.poll_enabled)
1036 return;
Chris Wilsone58f6372010-08-20 09:13:36 +01001037
Tejun Heo991ea752010-07-20 22:09:02 +02001038 /* kill timer and schedule immediate execution, this doesn't block */
1039 cancel_delayed_work(&dev->mode_config.output_poll_work);
Chris Wilsone58f6372010-08-20 09:13:36 +01001040 if (drm_kms_helper_poll)
Tejun Heo3b07e9c2012-08-20 14:51:24 -07001041 schedule_delayed_work(&dev->mode_config.output_poll_work, 0);
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001042}
1043EXPORT_SYMBOL(drm_helper_hpd_irq_event);