blob: d2619d72ceceb16d7b88a33b09708219483019b4 [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
Dave Airlief453ba02008-11-07 14:05:41 -080035#include "drmP.h"
36#include "drm_crtc.h"
37#include "drm_crtc_helper.h"
Dave Airlied50ba252009-09-23 14:44:08 +100038#include "drm_fb_helper.h"
Dave Airlief453ba02008-11-07 14:05:41 -080039
Chris Wilsone58f6372010-08-20 09:13:36 +010040static bool drm_kms_helper_poll = true;
41module_param_named(poll, drm_kms_helper_poll, bool, 0600);
42
yakui_zhao67149772009-04-02 11:52:12 +080043static void drm_mode_validate_flag(struct drm_connector *connector,
44 int flags)
45{
46 struct drm_display_mode *mode, *t;
47
48 if (flags == (DRM_MODE_FLAG_DBLSCAN | DRM_MODE_FLAG_INTERLACE))
49 return;
50
51 list_for_each_entry_safe(mode, t, &connector->modes, head) {
52 if ((mode->flags & DRM_MODE_FLAG_INTERLACE) &&
53 !(flags & DRM_MODE_FLAG_INTERLACE))
54 mode->status = MODE_NO_INTERLACE;
55 if ((mode->flags & DRM_MODE_FLAG_DBLSCAN) &&
56 !(flags & DRM_MODE_FLAG_DBLSCAN))
57 mode->status = MODE_NO_DBLESCAN;
58 }
59
60 return;
61}
62
Dave Airlief453ba02008-11-07 14:05:41 -080063/**
Dave Airlie38651672010-03-30 05:34:13 +000064 * drm_helper_probe_single_connector_modes - get complete set of display modes
Dave Airlief453ba02008-11-07 14:05:41 -080065 * @dev: DRM device
66 * @maxX: max width for modes
67 * @maxY: max height for modes
68 *
69 * LOCKING:
70 * Caller must hold mode config lock.
71 *
72 * Based on @dev's mode_config layout, scan all the connectors and try to detect
73 * modes on them. Modes will first be added to the connector's probed_modes
74 * list, then culled (based on validity and the @maxX, @maxY parameters) and
75 * put into the normal modes list.
76 *
77 * Intended to be used either at bootup time or when major configuration
78 * changes have occurred.
79 *
80 * FIXME: take into account monitor limits
Jesse Barnes40a518d2009-01-12 12:05:32 -080081 *
82 * RETURNS:
83 * Number of modes found on @connector.
Dave Airlief453ba02008-11-07 14:05:41 -080084 */
Jesse Barnes40a518d2009-01-12 12:05:32 -080085int drm_helper_probe_single_connector_modes(struct drm_connector *connector,
86 uint32_t maxX, uint32_t maxY)
Dave Airlief453ba02008-11-07 14:05:41 -080087{
88 struct drm_device *dev = connector->dev;
89 struct drm_display_mode *mode, *t;
90 struct drm_connector_helper_funcs *connector_funcs =
91 connector->helper_private;
Jesse Barnes40a518d2009-01-12 12:05:32 -080092 int count = 0;
yakui_zhao67149772009-04-02 11:52:12 +080093 int mode_flags = 0;
Dave Airlief453ba02008-11-07 14:05:41 -080094
Jerome Glisse94401062010-07-15 15:43:25 -040095 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", connector->base.id,
96 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -080097 /* set all modes to the unverified state */
98 list_for_each_entry_safe(mode, t, &connector->modes, head)
99 mode->status = MODE_UNVERIFIED;
100
Dave Airlied50ba252009-09-23 14:44:08 +1000101 if (connector->force) {
102 if (connector->force == DRM_FORCE_ON)
103 connector->status = connector_status_connected;
104 else
105 connector->status = connector_status_disconnected;
106 if (connector->funcs->force)
107 connector->funcs->force(connector);
Chris Wilsone58f6372010-08-20 09:13:36 +0100108 } else {
Chris Wilson930a9e22010-09-14 11:07:23 +0100109 connector->status = connector->funcs->detect(connector, true);
Chris Wilson551402a2010-09-06 23:53:47 +0100110 drm_kms_helper_poll_enable(dev);
Chris Wilsone58f6372010-08-20 09:13:36 +0100111 }
Dave Airlief453ba02008-11-07 14:05:41 -0800112
113 if (connector->status == connector_status_disconnected) {
Jerome Glisse94401062010-07-15 15:43:25 -0400114 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] disconnected\n",
115 connector->base.id, drm_get_connector_name(connector));
Zhao Yakui72539832010-03-04 08:25:55 +0000116 drm_mode_connector_update_edid_property(connector, NULL);
Adam Jackson620f3782009-09-08 11:51:46 +1000117 goto prune;
Dave Airlief453ba02008-11-07 14:05:41 -0800118 }
119
Jesse Barnes40a518d2009-01-12 12:05:32 -0800120 count = (*connector_funcs->get_modes)(connector);
Chris Wilsonc7ef35a2010-09-05 16:55:25 +0100121 if (count == 0 && connector->status == connector_status_connected)
Adam Jackson9632b412009-11-23 14:23:07 -0500122 count = drm_add_modes_noedid(connector, 1024, 768);
Chris Wilsonc7ef35a2010-09-05 16:55:25 +0100123 if (count == 0)
124 goto prune;
Dave Airlief453ba02008-11-07 14:05:41 -0800125
Jesse Barnes40a518d2009-01-12 12:05:32 -0800126 drm_mode_connector_list_update(connector);
Dave Airlief453ba02008-11-07 14:05:41 -0800127
128 if (maxX && maxY)
129 drm_mode_validate_size(dev, &connector->modes, maxX,
130 maxY, 0);
yakui_zhao67149772009-04-02 11:52:12 +0800131
132 if (connector->interlace_allowed)
133 mode_flags |= DRM_MODE_FLAG_INTERLACE;
134 if (connector->doublescan_allowed)
135 mode_flags |= DRM_MODE_FLAG_DBLSCAN;
136 drm_mode_validate_flag(connector, mode_flags);
137
Dave Airlief453ba02008-11-07 14:05:41 -0800138 list_for_each_entry_safe(mode, t, &connector->modes, head) {
139 if (mode->status == MODE_OK)
140 mode->status = connector_funcs->mode_valid(connector,
141 mode);
142 }
143
Adam Jackson620f3782009-09-08 11:51:46 +1000144prune:
Dave Airlief453ba02008-11-07 14:05:41 -0800145 drm_mode_prune_invalid(dev, &connector->modes, true);
146
Jesse Barnes40a518d2009-01-12 12:05:32 -0800147 if (list_empty(&connector->modes))
148 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800149
150 drm_mode_sort(&connector->modes);
151
Jerome Glisse94401062010-07-15 15:43:25 -0400152 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] probed modes :\n", connector->base.id,
153 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -0800154 list_for_each_entry_safe(mode, t, &connector->modes, head) {
155 mode->vrefresh = drm_mode_vrefresh(mode);
156
157 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
158 drm_mode_debug_printmodeline(mode);
159 }
Jesse Barnes40a518d2009-01-12 12:05:32 -0800160
161 return count;
Dave Airlief453ba02008-11-07 14:05:41 -0800162}
163EXPORT_SYMBOL(drm_helper_probe_single_connector_modes);
164
Dave Airlief453ba02008-11-07 14:05:41 -0800165/**
Keith Packardc9fb15f2009-05-30 20:42:28 -0700166 * drm_helper_encoder_in_use - check if a given encoder is in use
167 * @encoder: encoder to check
168 *
169 * LOCKING:
170 * Caller must hold mode config lock.
171 *
172 * Walk @encoders's DRM device's mode_config and see if it's in use.
173 *
174 * RETURNS:
175 * True if @encoder is part of the mode_config, false otherwise.
176 */
177bool drm_helper_encoder_in_use(struct drm_encoder *encoder)
178{
179 struct drm_connector *connector;
180 struct drm_device *dev = encoder->dev;
181 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
182 if (connector->encoder == encoder)
183 return true;
184 return false;
185}
186EXPORT_SYMBOL(drm_helper_encoder_in_use);
187
188/**
Dave Airlief453ba02008-11-07 14:05:41 -0800189 * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config
190 * @crtc: CRTC to check
191 *
192 * LOCKING:
193 * Caller must hold mode config lock.
194 *
195 * Walk @crtc's DRM device's mode_config and see if it's in use.
196 *
197 * RETURNS:
198 * True if @crtc is part of the mode_config, false otherwise.
199 */
200bool drm_helper_crtc_in_use(struct drm_crtc *crtc)
201{
202 struct drm_encoder *encoder;
203 struct drm_device *dev = crtc->dev;
204 /* FIXME: Locking around list access? */
205 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
Keith Packardc9fb15f2009-05-30 20:42:28 -0700206 if (encoder->crtc == crtc && drm_helper_encoder_in_use(encoder))
Dave Airlief453ba02008-11-07 14:05:41 -0800207 return true;
208 return false;
209}
210EXPORT_SYMBOL(drm_helper_crtc_in_use);
211
Ben Skeggs86a1b9d2010-07-01 16:49:57 +1000212static void
213drm_encoder_disable(struct drm_encoder *encoder)
214{
215 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
216
217 if (encoder_funcs->disable)
218 (*encoder_funcs->disable)(encoder);
219 else
220 (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
221}
222
Dave Airlief453ba02008-11-07 14:05:41 -0800223/**
David John89347bb2009-12-31 12:00:46 +0530224 * drm_helper_disable_unused_functions - disable unused objects
Dave Airlief453ba02008-11-07 14:05:41 -0800225 * @dev: DRM device
226 *
227 * LOCKING:
228 * Caller must hold mode config lock.
229 *
230 * If an connector or CRTC isn't part of @dev's mode_config, it can be disabled
231 * by calling its dpms function, which should power it off.
232 */
233void drm_helper_disable_unused_functions(struct drm_device *dev)
234{
235 struct drm_encoder *encoder;
Dave Airliea3a05442009-08-31 15:16:30 +1000236 struct drm_connector *connector;
Dave Airlief453ba02008-11-07 14:05:41 -0800237 struct drm_crtc *crtc;
238
Dave Airliea3a05442009-08-31 15:16:30 +1000239 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
240 if (!connector->encoder)
241 continue;
242 if (connector->status == connector_status_disconnected)
243 connector->encoder = NULL;
244 }
245
Dave Airlief453ba02008-11-07 14:05:41 -0800246 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Dave Airlie929710212010-12-21 12:47:56 +1000247 if (!drm_helper_encoder_in_use(encoder)) {
Ben Skeggs86a1b9d2010-07-01 16:49:57 +1000248 drm_encoder_disable(encoder);
Dave Airlie9c552dd2009-09-02 14:00:11 +1000249 /* disconnector encoder from any connector */
250 encoder->crtc = NULL;
Dave Airliea3a05442009-08-31 15:16:30 +1000251 }
Dave Airlief453ba02008-11-07 14:05:41 -0800252 }
253
254 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
255 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
256 crtc->enabled = drm_helper_crtc_in_use(crtc);
257 if (!crtc->enabled) {
Alex Deucher5c8d7172010-06-11 17:04:35 -0400258 if (crtc_funcs->disable)
259 (*crtc_funcs->disable)(crtc);
260 else
261 (*crtc_funcs->dpms)(crtc, DRM_MODE_DPMS_OFF);
Dave Airlief453ba02008-11-07 14:05:41 -0800262 crtc->fb = NULL;
263 }
264 }
265}
266EXPORT_SYMBOL(drm_helper_disable_unused_functions);
267
Jesse Barnes7bec7562009-02-23 16:09:34 -0800268/**
269 * drm_encoder_crtc_ok - can a given crtc drive a given encoder?
270 * @encoder: encoder to test
271 * @crtc: crtc to test
272 *
273 * Return false if @encoder can't be driven by @crtc, true otherwise.
274 */
275static bool drm_encoder_crtc_ok(struct drm_encoder *encoder,
276 struct drm_crtc *crtc)
277{
278 struct drm_device *dev;
279 struct drm_crtc *tmp;
280 int crtc_mask = 1;
281
Joe Perchesfce7d612010-10-30 21:08:30 +0000282 WARN(!crtc, "checking null crtc?\n");
Jesse Barnes7bec7562009-02-23 16:09:34 -0800283
284 dev = crtc->dev;
285
286 list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
287 if (tmp == crtc)
288 break;
289 crtc_mask <<= 1;
290 }
291
292 if (encoder->possible_crtcs & crtc_mask)
293 return true;
294 return false;
295}
296
297/*
298 * Check the CRTC we're going to map each output to vs. its current
299 * CRTC. If they don't match, we have to disable the output and the CRTC
300 * since the driver will have to re-route things.
301 */
302static void
303drm_crtc_prepare_encoders(struct drm_device *dev)
304{
305 struct drm_encoder_helper_funcs *encoder_funcs;
306 struct drm_encoder *encoder;
307
308 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
309 encoder_funcs = encoder->helper_private;
310 /* Disable unused encoders */
311 if (encoder->crtc == NULL)
Ben Skeggs86a1b9d2010-07-01 16:49:57 +1000312 drm_encoder_disable(encoder);
Jesse Barnes7bec7562009-02-23 16:09:34 -0800313 /* Disable encoders whose CRTC is about to change */
314 if (encoder_funcs->get_crtc &&
315 encoder->crtc != (*encoder_funcs->get_crtc)(encoder))
Ben Skeggs86a1b9d2010-07-01 16:49:57 +1000316 drm_encoder_disable(encoder);
Jesse Barnes7bec7562009-02-23 16:09:34 -0800317 }
318}
319
Dave Airlief453ba02008-11-07 14:05:41 -0800320/**
321 * drm_crtc_set_mode - set a mode
322 * @crtc: CRTC to program
323 * @mode: mode to use
324 * @x: width of mode
325 * @y: height of mode
326 *
327 * LOCKING:
328 * Caller must hold mode config lock.
329 *
330 * Try to set @mode on @crtc. Give @crtc and its associated connectors a chance
331 * to fixup or reject the mode prior to trying to set it.
332 *
333 * RETURNS:
334 * True if the mode was set successfully, or false otherwise.
335 */
336bool drm_crtc_helper_set_mode(struct drm_crtc *crtc,
337 struct drm_display_mode *mode,
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -0500338 int x, int y,
339 struct drm_framebuffer *old_fb)
Dave Airlief453ba02008-11-07 14:05:41 -0800340{
341 struct drm_device *dev = crtc->dev;
Mario Kleiner27641c32010-10-23 04:20:23 +0200342 struct drm_display_mode *adjusted_mode, saved_mode, saved_hwmode;
Dave Airlief453ba02008-11-07 14:05:41 -0800343 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
344 struct drm_encoder_helper_funcs *encoder_funcs;
345 int saved_x, saved_y;
346 struct drm_encoder *encoder;
347 bool ret = true;
348
Dave Airlief453ba02008-11-07 14:05:41 -0800349 crtc->enabled = drm_helper_crtc_in_use(crtc);
Dave Airlief453ba02008-11-07 14:05:41 -0800350 if (!crtc->enabled)
351 return true;
352
Chris Wilson021a8452011-01-28 11:31:56 +0000353 adjusted_mode = drm_mode_duplicate(dev, mode);
354
Mario Kleiner27641c32010-10-23 04:20:23 +0200355 saved_hwmode = crtc->hwmode;
Dave Airlief453ba02008-11-07 14:05:41 -0800356 saved_mode = crtc->mode;
357 saved_x = crtc->x;
358 saved_y = crtc->y;
359
360 /* Update crtc values up front so the driver can rely on them for mode
361 * setting.
362 */
363 crtc->mode = *mode;
364 crtc->x = x;
365 crtc->y = y;
366
Dave Airlief453ba02008-11-07 14:05:41 -0800367 /* Pass our mode to the connectors and the CRTC to give them a chance to
368 * adjust it according to limitations or connector properties, and also
369 * a chance to reject the mode entirely.
370 */
371 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
372
373 if (encoder->crtc != crtc)
374 continue;
375 encoder_funcs = encoder->helper_private;
376 if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
377 adjusted_mode))) {
Adam Jackson836e53d2011-10-10 16:21:27 -0400378 DRM_DEBUG_KMS("Encoder fixup failed\n");
Dave Airlief453ba02008-11-07 14:05:41 -0800379 goto done;
380 }
381 }
382
383 if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) {
Adam Jackson836e53d2011-10-10 16:21:27 -0400384 DRM_DEBUG_KMS("CRTC fixup failed\n");
Dave Airlief453ba02008-11-07 14:05:41 -0800385 goto done;
386 }
Jerome Glisse94401062010-07-15 15:43:25 -0400387 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -0800388
389 /* Prepare the encoders and CRTCs before setting the mode. */
390 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
391
392 if (encoder->crtc != crtc)
393 continue;
394 encoder_funcs = encoder->helper_private;
395 /* Disable the encoders as the first thing we do. */
396 encoder_funcs->prepare(encoder);
397 }
398
Jesse Barnes7bec7562009-02-23 16:09:34 -0800399 drm_crtc_prepare_encoders(dev);
400
Dave Airlief453ba02008-11-07 14:05:41 -0800401 crtc_funcs->prepare(crtc);
402
403 /* Set up the DPLL and any encoders state that needs to adjust or depend
404 * on the DPLL.
405 */
Chris Wilson5c3b82e2009-02-11 13:25:09 +0000406 ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
407 if (!ret)
408 goto done;
Dave Airlief453ba02008-11-07 14:05:41 -0800409
410 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
411
412 if (encoder->crtc != crtc)
413 continue;
414
Jerome Glisse94401062010-07-15 15:43:25 -0400415 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
416 encoder->base.id, drm_get_encoder_name(encoder),
417 mode->base.id, mode->name);
Dave Airlief453ba02008-11-07 14:05:41 -0800418 encoder_funcs = encoder->helper_private;
419 encoder_funcs->mode_set(encoder, mode, adjusted_mode);
420 }
421
422 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
423 crtc_funcs->commit(crtc);
424
425 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
426
427 if (encoder->crtc != crtc)
428 continue;
429
430 encoder_funcs = encoder->helper_private;
431 encoder_funcs->commit(encoder);
432
433 }
434
Mario Kleiner27641c32010-10-23 04:20:23 +0200435 /* Store real post-adjustment hardware mode. */
436 crtc->hwmode = *adjusted_mode;
437
438 /* Calculate and store various constants which
439 * are later needed by vblank and swap-completion
440 * timestamping. They are derived from true hwmode.
441 */
442 drm_calc_timestamping_constants(crtc);
443
Dave Airlief453ba02008-11-07 14:05:41 -0800444 /* FIXME: add subpixel order */
445done:
Chris Wilson021a8452011-01-28 11:31:56 +0000446 drm_mode_destroy(dev, adjusted_mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800447 if (!ret) {
Mario Kleiner27641c32010-10-23 04:20:23 +0200448 crtc->hwmode = saved_hwmode;
Dave Airlief453ba02008-11-07 14:05:41 -0800449 crtc->mode = saved_mode;
450 crtc->x = saved_x;
451 crtc->y = saved_y;
452 }
453
454 return ret;
455}
456EXPORT_SYMBOL(drm_crtc_helper_set_mode);
457
458
Chris Wilson6eebd6b2011-11-28 21:10:05 +0000459static int
460drm_crtc_helper_disable(struct drm_crtc *crtc)
461{
462 struct drm_device *dev = crtc->dev;
463 struct drm_connector *connector;
464 struct drm_encoder *encoder;
465
466 /* Decouple all encoders and their attached connectors from this crtc */
467 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
468 if (encoder->crtc != crtc)
469 continue;
470
471 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
472 if (connector->encoder != encoder)
473 continue;
474
475 connector->encoder = NULL;
476 }
477 }
478
479 drm_helper_disable_unused_functions(dev);
480 return 0;
481}
482
Dave Airlief453ba02008-11-07 14:05:41 -0800483/**
484 * drm_crtc_helper_set_config - set a new config from userspace
485 * @crtc: CRTC to setup
486 * @crtc_info: user provided configuration
487 * @new_mode: new mode to set
488 * @connector_set: set of connectors for the new config
489 * @fb: new framebuffer
490 *
491 * LOCKING:
492 * Caller must hold mode config lock.
493 *
494 * Setup a new configuration, provided by the user in @crtc_info, and enable
495 * it.
496 *
497 * RETURNS:
498 * Zero. (FIXME)
499 */
500int drm_crtc_helper_set_config(struct drm_mode_set *set)
501{
502 struct drm_device *dev;
Maarten Maathuise67aae72009-08-27 10:18:29 +0200503 struct drm_crtc *save_crtcs, *new_crtc, *crtc;
504 struct drm_encoder *save_encoders, *new_encoder, *encoder;
Jesse Barnes7bec7562009-02-23 16:09:34 -0800505 struct drm_framebuffer *old_fb = NULL;
Jakob Bornecrantz4cb72b12009-08-03 13:43:59 +0100506 bool mode_changed = false; /* if true do a full mode set */
507 bool fb_changed = false; /* if true and !mode_changed just do a flip */
Maarten Maathuise67aae72009-08-27 10:18:29 +0200508 struct drm_connector *save_connectors, *connector;
Dave Airlief453ba02008-11-07 14:05:41 -0800509 int count = 0, ro, fail = 0;
510 struct drm_crtc_helper_funcs *crtc_funcs;
Jesse Barnesc5006cfe2011-11-07 10:39:57 -0800511 struct drm_mode_set save_set;
Dave Airlief453ba02008-11-07 14:05:41 -0800512 int ret = 0;
Keith Packardbf9dc102010-11-26 10:45:58 -0800513 int i;
Dave Airlief453ba02008-11-07 14:05:41 -0800514
Zhao Yakui58367ed2009-07-20 13:48:07 +0800515 DRM_DEBUG_KMS("\n");
Dave Airlief453ba02008-11-07 14:05:41 -0800516
517 if (!set)
518 return -EINVAL;
519
520 if (!set->crtc)
521 return -EINVAL;
522
523 if (!set->crtc->helper_private)
524 return -EINVAL;
525
526 crtc_funcs = set->crtc->helper_private;
527
Chris Wilsonede3ff52011-01-31 11:16:33 +0000528 if (!set->mode)
529 set->fb = NULL;
530
Jerome Glisse94401062010-07-15 15:43:25 -0400531 if (set->fb) {
532 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
533 set->crtc->base.id, set->fb->base.id,
534 (int)set->num_connectors, set->x, set->y);
535 } else {
Chris Wilsonede3ff52011-01-31 11:16:33 +0000536 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Chris Wilson6eebd6b2011-11-28 21:10:05 +0000537 return drm_crtc_helper_disable(set->crtc);
Jerome Glisse94401062010-07-15 15:43:25 -0400538 }
Dave Airlief453ba02008-11-07 14:05:41 -0800539
540 dev = set->crtc->dev;
541
Maarten Maathuise67aae72009-08-27 10:18:29 +0200542 /* Allocate space for the backup of all (non-pointer) crtc, encoder and
543 * connector data. */
544 save_crtcs = kzalloc(dev->mode_config.num_crtc *
545 sizeof(struct drm_crtc), GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -0800546 if (!save_crtcs)
547 return -ENOMEM;
548
Maarten Maathuise67aae72009-08-27 10:18:29 +0200549 save_encoders = kzalloc(dev->mode_config.num_encoder *
550 sizeof(struct drm_encoder), GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -0800551 if (!save_encoders) {
552 kfree(save_crtcs);
553 return -ENOMEM;
554 }
555
Maarten Maathuise67aae72009-08-27 10:18:29 +0200556 save_connectors = kzalloc(dev->mode_config.num_connector *
557 sizeof(struct drm_connector), GFP_KERNEL);
558 if (!save_connectors) {
559 kfree(save_crtcs);
560 kfree(save_encoders);
561 return -ENOMEM;
562 }
563
564 /* Copy data. Note that driver private data is not affected.
565 * Should anything bad happen only the expected state is
566 * restored, not the drivers personal bookkeeping.
567 */
568 count = 0;
569 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
570 save_crtcs[count++] = *crtc;
571 }
572
573 count = 0;
574 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
575 save_encoders[count++] = *encoder;
576 }
577
578 count = 0;
579 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
580 save_connectors[count++] = *connector;
581 }
582
Jesse Barnesc5006cfe2011-11-07 10:39:57 -0800583 save_set.crtc = set->crtc;
584 save_set.mode = &set->crtc->mode;
585 save_set.x = set->crtc->x;
586 save_set.y = set->crtc->y;
587 save_set.fb = set->crtc->fb;
588
Dave Airlief453ba02008-11-07 14:05:41 -0800589 /* We should be able to check here if the fb has the same properties
590 * and then just flip_or_move it */
591 if (set->crtc->fb != set->fb) {
Jesse Barnes712531b2009-01-09 13:56:14 -0800592 /* If we have no fb then treat it as a full mode set */
Jesse Barnes7bec7562009-02-23 16:09:34 -0800593 if (set->crtc->fb == NULL) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800594 DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
Jesse Barnes712531b2009-01-09 13:56:14 -0800595 mode_changed = true;
Jakob Bornecrantz4cb72b12009-08-03 13:43:59 +0100596 } else if (set->fb == NULL) {
597 mode_changed = true;
Jesse Barnes46e48452011-06-24 12:19:26 -0700598 } else if (set->fb->depth != set->crtc->fb->depth) {
599 mode_changed = true;
600 } else if (set->fb->bits_per_pixel !=
601 set->crtc->fb->bits_per_pixel) {
602 mode_changed = true;
Dave Airlie8dff4742010-02-11 14:28:58 +1000603 } else
Jesse Barnes712531b2009-01-09 13:56:14 -0800604 fb_changed = true;
Dave Airlief453ba02008-11-07 14:05:41 -0800605 }
606
607 if (set->x != set->crtc->x || set->y != set->crtc->y)
Jesse Barnes712531b2009-01-09 13:56:14 -0800608 fb_changed = true;
Dave Airlief453ba02008-11-07 14:05:41 -0800609
610 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800611 DRM_DEBUG_KMS("modes are different, full mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -0800612 drm_mode_debug_printmodeline(&set->crtc->mode);
613 drm_mode_debug_printmodeline(set->mode);
Jesse Barnes712531b2009-01-09 13:56:14 -0800614 mode_changed = true;
Dave Airlief453ba02008-11-07 14:05:41 -0800615 }
616
617 /* a) traverse passed in connector list and get encoders for them */
618 count = 0;
619 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
620 struct drm_connector_helper_funcs *connector_funcs =
621 connector->helper_private;
Dave Airlief453ba02008-11-07 14:05:41 -0800622 new_encoder = connector->encoder;
623 for (ro = 0; ro < set->num_connectors; ro++) {
624 if (set->connectors[ro] == connector) {
625 new_encoder = connector_funcs->best_encoder(connector);
626 /* if we can't get an encoder for a connector
627 we are setting now - then fail */
628 if (new_encoder == NULL)
629 /* don't break so fail path works correct */
630 fail = 1;
631 break;
632 }
633 }
634
635 if (new_encoder != connector->encoder) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800636 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
Jesse Barnes712531b2009-01-09 13:56:14 -0800637 mode_changed = true;
Maarten Maathuisff846ab2009-08-19 00:56:45 +0200638 /* If the encoder is reused for another connector, then
639 * the appropriate crtc will be set later.
640 */
Maarten Maathuisff6fdbe2009-09-01 03:39:04 +0200641 if (connector->encoder)
642 connector->encoder->crtc = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800643 connector->encoder = new_encoder;
644 }
645 }
646
647 if (fail) {
648 ret = -EINVAL;
Maarten Maathuise67aae72009-08-27 10:18:29 +0200649 goto fail;
Dave Airlief453ba02008-11-07 14:05:41 -0800650 }
651
652 count = 0;
653 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
654 if (!connector->encoder)
655 continue;
656
Dave Airlief453ba02008-11-07 14:05:41 -0800657 if (connector->encoder->crtc == set->crtc)
658 new_crtc = NULL;
659 else
660 new_crtc = connector->encoder->crtc;
661
662 for (ro = 0; ro < set->num_connectors; ro++) {
663 if (set->connectors[ro] == connector)
664 new_crtc = set->crtc;
665 }
Jesse Barnes7bec7562009-02-23 16:09:34 -0800666
667 /* Make sure the new CRTC will work with the encoder */
668 if (new_crtc &&
669 !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
670 ret = -EINVAL;
Maarten Maathuise67aae72009-08-27 10:18:29 +0200671 goto fail;
Jesse Barnes7bec7562009-02-23 16:09:34 -0800672 }
Dave Airlief453ba02008-11-07 14:05:41 -0800673 if (new_crtc != connector->encoder->crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800674 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
Jesse Barnes712531b2009-01-09 13:56:14 -0800675 mode_changed = true;
Dave Airlief453ba02008-11-07 14:05:41 -0800676 connector->encoder->crtc = new_crtc;
677 }
Jerome Glisse94401062010-07-15 15:43:25 -0400678 if (new_crtc) {
679 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
680 connector->base.id, drm_get_connector_name(connector),
681 new_crtc->base.id);
682 } else {
683 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
684 connector->base.id, drm_get_connector_name(connector));
685 }
Dave Airlief453ba02008-11-07 14:05:41 -0800686 }
687
688 /* mode_set_base is not a required function */
Jesse Barnes712531b2009-01-09 13:56:14 -0800689 if (fb_changed && !crtc_funcs->mode_set_base)
690 mode_changed = true;
Dave Airlief453ba02008-11-07 14:05:41 -0800691
Jesse Barnes712531b2009-01-09 13:56:14 -0800692 if (mode_changed) {
Chris Wilson9334ef72011-01-28 11:53:03 +0000693 set->crtc->enabled = drm_helper_crtc_in_use(set->crtc);
694 if (set->crtc->enabled) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800695 DRM_DEBUG_KMS("attempting to set mode from"
696 " userspace\n");
Dave Airlief453ba02008-11-07 14:05:41 -0800697 drm_mode_debug_printmodeline(set->mode);
Chris Wilson356ad3c2010-09-09 09:41:32 +0100698 old_fb = set->crtc->fb;
699 set->crtc->fb = set->fb;
Dave Airlief453ba02008-11-07 14:05:41 -0800700 if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -0500701 set->x, set->y,
702 old_fb)) {
Jerome Glisse94401062010-07-15 15:43:25 -0400703 DRM_ERROR("failed to set mode on [CRTC:%d]\n",
704 set->crtc->base.id);
Chris Wilson0ba41e42011-01-08 15:10:41 +0000705 set->crtc->fb = old_fb;
Dave Airlief453ba02008-11-07 14:05:41 -0800706 ret = -EINVAL;
Maarten Maathuise67aae72009-08-27 10:18:29 +0200707 goto fail;
Dave Airlief453ba02008-11-07 14:05:41 -0800708 }
Keith Packard811aaa52011-02-03 16:57:28 -0800709 DRM_DEBUG_KMS("Setting connector DPMS state to on\n");
710 for (i = 0; i < set->num_connectors; i++) {
711 DRM_DEBUG_KMS("\t[CONNECTOR:%d:%s] set DPMS on\n", set->connectors[i]->base.id,
712 drm_get_connector_name(set->connectors[i]));
713 set->connectors[i]->dpms = DRM_MODE_DPMS_ON;
714 }
Dave Airlief453ba02008-11-07 14:05:41 -0800715 }
716 drm_helper_disable_unused_functions(dev);
Jesse Barnes712531b2009-01-09 13:56:14 -0800717 } else if (fb_changed) {
Ben Skeggs9b1596a2009-09-18 10:43:52 +1000718 set->crtc->x = set->x;
719 set->crtc->y = set->y;
720
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -0500721 old_fb = set->crtc->fb;
Dave Airlief453ba02008-11-07 14:05:41 -0800722 if (set->crtc->fb != set->fb)
723 set->crtc->fb = set->fb;
Chris Wilson5c3b82e2009-02-11 13:25:09 +0000724 ret = crtc_funcs->mode_set_base(set->crtc,
725 set->x, set->y, old_fb);
Chris Wilson0ba41e42011-01-08 15:10:41 +0000726 if (ret != 0) {
727 set->crtc->fb = old_fb;
Maarten Maathuise67aae72009-08-27 10:18:29 +0200728 goto fail;
Chris Wilson0ba41e42011-01-08 15:10:41 +0000729 }
Dave Airlief453ba02008-11-07 14:05:41 -0800730 }
731
Maarten Maathuise67aae72009-08-27 10:18:29 +0200732 kfree(save_connectors);
Dave Airlief453ba02008-11-07 14:05:41 -0800733 kfree(save_encoders);
734 kfree(save_crtcs);
735 return 0;
736
Maarten Maathuise67aae72009-08-27 10:18:29 +0200737fail:
738 /* Restore all previous data. */
Dave Airlief453ba02008-11-07 14:05:41 -0800739 count = 0;
Maarten Maathuise67aae72009-08-27 10:18:29 +0200740 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
741 *crtc = save_crtcs[count++];
742 }
Chris Wilsone62fb642009-02-11 16:39:21 +0000743
Maarten Maathuise67aae72009-08-27 10:18:29 +0200744 count = 0;
745 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
746 *encoder = save_encoders[count++];
Chris Wilsone62fb642009-02-11 16:39:21 +0000747 }
Maarten Maathuise67aae72009-08-27 10:18:29 +0200748
Dave Airlief453ba02008-11-07 14:05:41 -0800749 count = 0;
750 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Maarten Maathuise67aae72009-08-27 10:18:29 +0200751 *connector = save_connectors[count++];
Dave Airlief453ba02008-11-07 14:05:41 -0800752 }
Maarten Maathuise67aae72009-08-27 10:18:29 +0200753
Jesse Barnesc5006cfe2011-11-07 10:39:57 -0800754 /* Try to restore the config */
755 if (mode_changed &&
756 !drm_crtc_helper_set_mode(save_set.crtc, save_set.mode, save_set.x,
757 save_set.y, save_set.fb))
758 DRM_ERROR("failed to restore config after modeset failure\n");
759
Maarten Maathuise67aae72009-08-27 10:18:29 +0200760 kfree(save_connectors);
Dave Airlief453ba02008-11-07 14:05:41 -0800761 kfree(save_encoders);
Maarten Maathuise67aae72009-08-27 10:18:29 +0200762 kfree(save_crtcs);
Dave Airlief453ba02008-11-07 14:05:41 -0800763 return ret;
764}
765EXPORT_SYMBOL(drm_crtc_helper_set_config);
766
Keith Packardc9fb15f2009-05-30 20:42:28 -0700767static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder)
768{
769 int dpms = DRM_MODE_DPMS_OFF;
770 struct drm_connector *connector;
771 struct drm_device *dev = encoder->dev;
772
773 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
774 if (connector->encoder == encoder)
775 if (connector->dpms < dpms)
776 dpms = connector->dpms;
777 return dpms;
778}
779
780static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc)
781{
782 int dpms = DRM_MODE_DPMS_OFF;
783 struct drm_connector *connector;
784 struct drm_device *dev = crtc->dev;
785
786 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
787 if (connector->encoder && connector->encoder->crtc == crtc)
788 if (connector->dpms < dpms)
789 dpms = connector->dpms;
790 return dpms;
791}
792
793/**
794 * drm_helper_connector_dpms
795 * @connector affected connector
796 * @mode DPMS mode
797 *
798 * Calls the low-level connector DPMS function, then
799 * calls appropriate encoder and crtc DPMS functions as well
800 */
801void drm_helper_connector_dpms(struct drm_connector *connector, int mode)
802{
803 struct drm_encoder *encoder = connector->encoder;
804 struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
805 int old_dpms;
806
807 if (mode == connector->dpms)
808 return;
809
810 old_dpms = connector->dpms;
811 connector->dpms = mode;
812
813 /* from off to on, do crtc then encoder */
814 if (mode < old_dpms) {
815 if (crtc) {
816 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
817 if (crtc_funcs->dpms)
818 (*crtc_funcs->dpms) (crtc,
819 drm_helper_choose_crtc_dpms(crtc));
820 }
821 if (encoder) {
822 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
823 if (encoder_funcs->dpms)
824 (*encoder_funcs->dpms) (encoder,
825 drm_helper_choose_encoder_dpms(encoder));
826 }
827 }
828
829 /* from on to off, do encoder then crtc */
830 if (mode > old_dpms) {
831 if (encoder) {
832 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
833 if (encoder_funcs->dpms)
834 (*encoder_funcs->dpms) (encoder,
835 drm_helper_choose_encoder_dpms(encoder));
836 }
837 if (crtc) {
838 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
839 if (crtc_funcs->dpms)
840 (*crtc_funcs->dpms) (crtc,
841 drm_helper_choose_crtc_dpms(crtc));
842 }
843 }
844
845 return;
846}
847EXPORT_SYMBOL(drm_helper_connector_dpms);
848
Dave Airlief453ba02008-11-07 14:05:41 -0800849int drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb,
850 struct drm_mode_fb_cmd *mode_cmd)
851{
852 fb->width = mode_cmd->width;
853 fb->height = mode_cmd->height;
854 fb->pitch = mode_cmd->pitch;
855 fb->bits_per_pixel = mode_cmd->bpp;
856 fb->depth = mode_cmd->depth;
857
858 return 0;
859}
860EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
861
862int drm_helper_resume_force_mode(struct drm_device *dev)
863{
864 struct drm_crtc *crtc;
David John89347bb2009-12-31 12:00:46 +0530865 struct drm_encoder *encoder;
866 struct drm_encoder_helper_funcs *encoder_funcs;
867 struct drm_crtc_helper_funcs *crtc_funcs;
Dave Airlief453ba02008-11-07 14:05:41 -0800868 int ret;
869
870 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
871
872 if (!crtc->enabled)
873 continue;
874
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -0500875 ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
876 crtc->x, crtc->y, crtc->fb);
Dave Airlief453ba02008-11-07 14:05:41 -0800877
878 if (ret == false)
879 DRM_ERROR("failed to set mode on crtc %p\n", crtc);
David John89347bb2009-12-31 12:00:46 +0530880
881 /* Turn off outputs that were already powered off */
882 if (drm_helper_choose_crtc_dpms(crtc)) {
883 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
884
885 if(encoder->crtc != crtc)
886 continue;
887
888 encoder_funcs = encoder->helper_private;
889 if (encoder_funcs->dpms)
890 (*encoder_funcs->dpms) (encoder,
891 drm_helper_choose_encoder_dpms(encoder));
David John89347bb2009-12-31 12:00:46 +0530892 }
Chris Wilson817e6312010-08-06 15:03:31 +0100893
894 crtc_funcs = crtc->helper_private;
895 if (crtc_funcs->dpms)
896 (*crtc_funcs->dpms) (crtc,
897 drm_helper_choose_crtc_dpms(crtc));
David John89347bb2009-12-31 12:00:46 +0530898 }
Dave Airlief453ba02008-11-07 14:05:41 -0800899 }
Zhao Yakuiaf4fcb52009-07-08 14:13:13 +0800900 /* disable the unused connectors while restoring the modesetting */
901 drm_helper_disable_unused_functions(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800902 return 0;
903}
904EXPORT_SYMBOL(drm_helper_resume_force_mode);
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000905
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000906#define DRM_OUTPUT_POLL_PERIOD (10*HZ)
Tejun Heo991ea752010-07-20 22:09:02 +0200907static void output_poll_execute(struct work_struct *work)
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000908{
Tejun Heo991ea752010-07-20 22:09:02 +0200909 struct delayed_work *delayed_work = to_delayed_work(work);
910 struct drm_device *dev = container_of(delayed_work, struct drm_device, mode_config.output_poll_work);
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000911 struct drm_connector *connector;
Keith Packardc5027de2010-11-26 10:45:59 -0800912 enum drm_connector_status old_status;
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000913 bool repoll = false, changed = false;
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000914
Chris Wilsone58f6372010-08-20 09:13:36 +0100915 if (!drm_kms_helper_poll)
916 return;
917
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000918 mutex_lock(&dev->mode_config.mutex);
919 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
920
921 /* if this is HPD or polled don't check it -
922 TV out for instance */
923 if (!connector->polled)
924 continue;
925
926 else if (connector->polled & (DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT))
927 repoll = true;
928
929 old_status = connector->status;
930 /* if we are connected and don't want to poll for disconnect
931 skip it */
932 if (old_status == connector_status_connected &&
933 !(connector->polled & DRM_CONNECTOR_POLL_DISCONNECT) &&
934 !(connector->polled & DRM_CONNECTOR_POLL_HPD))
935 continue;
936
Keith Packardc5027de2010-11-26 10:45:59 -0800937 connector->status = connector->funcs->detect(connector, false);
Chris Wilson0f168302010-12-21 22:49:28 +0000938 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %d to %d\n",
939 connector->base.id,
940 drm_get_connector_name(connector),
941 old_status, connector->status);
Keith Packardc5027de2010-11-26 10:45:59 -0800942 if (old_status != connector->status)
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000943 changed = true;
944 }
945
946 mutex_unlock(&dev->mode_config.mutex);
947
948 if (changed) {
949 /* send a uevent + call fbdev */
950 drm_sysfs_hotplug_event(dev);
951 if (dev->mode_config.funcs->output_poll_changed)
952 dev->mode_config.funcs->output_poll_changed(dev);
953 }
954
Tejun Heo9a919c42010-08-09 12:01:27 +0200955 if (repoll)
956 queue_delayed_work(system_nrt_wq, delayed_work, DRM_OUTPUT_POLL_PERIOD);
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000957}
958
Dave Airliefbf81762010-06-01 09:09:06 +1000959void drm_kms_helper_poll_disable(struct drm_device *dev)
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000960{
Dave Airliefbf81762010-06-01 09:09:06 +1000961 if (!dev->mode_config.poll_enabled)
962 return;
Tejun Heo991ea752010-07-20 22:09:02 +0200963 cancel_delayed_work_sync(&dev->mode_config.output_poll_work);
Dave Airliefbf81762010-06-01 09:09:06 +1000964}
965EXPORT_SYMBOL(drm_kms_helper_poll_disable);
966
967void drm_kms_helper_poll_enable(struct drm_device *dev)
968{
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000969 bool poll = false;
Dave Airliefbf81762010-06-01 09:09:06 +1000970 struct drm_connector *connector;
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000971
Chris Wilsone58f6372010-08-20 09:13:36 +0100972 if (!dev->mode_config.poll_enabled || !drm_kms_helper_poll)
973 return;
974
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000975 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
976 if (connector->polled)
977 poll = true;
978 }
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000979
Tejun Heo9a919c42010-08-09 12:01:27 +0200980 if (poll)
981 queue_delayed_work(system_nrt_wq, &dev->mode_config.output_poll_work, DRM_OUTPUT_POLL_PERIOD);
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000982}
Dave Airliefbf81762010-06-01 09:09:06 +1000983EXPORT_SYMBOL(drm_kms_helper_poll_enable);
984
985void drm_kms_helper_poll_init(struct drm_device *dev)
986{
Tejun Heo991ea752010-07-20 22:09:02 +0200987 INIT_DELAYED_WORK(&dev->mode_config.output_poll_work, output_poll_execute);
Dave Airliefbf81762010-06-01 09:09:06 +1000988 dev->mode_config.poll_enabled = true;
989
990 drm_kms_helper_poll_enable(dev);
991}
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000992EXPORT_SYMBOL(drm_kms_helper_poll_init);
993
994void drm_kms_helper_poll_fini(struct drm_device *dev)
995{
Dave Airliefbf81762010-06-01 09:09:06 +1000996 drm_kms_helper_poll_disable(dev);
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000997}
998EXPORT_SYMBOL(drm_kms_helper_poll_fini);
999
1000void drm_helper_hpd_irq_event(struct drm_device *dev)
1001{
1002 if (!dev->mode_config.poll_enabled)
1003 return;
Chris Wilsone58f6372010-08-20 09:13:36 +01001004
Tejun Heo991ea752010-07-20 22:09:02 +02001005 /* kill timer and schedule immediate execution, this doesn't block */
1006 cancel_delayed_work(&dev->mode_config.output_poll_work);
Chris Wilsone58f6372010-08-20 09:13:36 +01001007 if (drm_kms_helper_poll)
1008 queue_delayed_work(system_nrt_wq, &dev->mode_config.output_poll_work, 0);
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001009}
1010EXPORT_SYMBOL(drm_helper_hpd_irq_event);