blob: 2ce61d72d416f8539b49227ed3da1a1c606232fa [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"
Jesse Barnes308e5bc2011-11-14 14:51:28 -080037#include "drm_fourcc.h"
Dave Airlief453ba02008-11-07 14:05:41 -080038#include "drm_crtc_helper.h"
Dave Airlied50ba252009-09-23 14:44:08 +100039#include "drm_fb_helper.h"
Dave Airlief453ba02008-11-07 14:05:41 -080040
Chris Wilsone58f6372010-08-20 09:13:36 +010041static bool drm_kms_helper_poll = true;
42module_param_named(poll, drm_kms_helper_poll, bool, 0600);
43
yakui_zhao67149772009-04-02 11:52:12 +080044static void drm_mode_validate_flag(struct drm_connector *connector,
45 int flags)
46{
47 struct drm_display_mode *mode, *t;
48
49 if (flags == (DRM_MODE_FLAG_DBLSCAN | DRM_MODE_FLAG_INTERLACE))
50 return;
51
52 list_for_each_entry_safe(mode, t, &connector->modes, head) {
53 if ((mode->flags & DRM_MODE_FLAG_INTERLACE) &&
54 !(flags & DRM_MODE_FLAG_INTERLACE))
55 mode->status = MODE_NO_INTERLACE;
56 if ((mode->flags & DRM_MODE_FLAG_DBLSCAN) &&
57 !(flags & DRM_MODE_FLAG_DBLSCAN))
58 mode->status = MODE_NO_DBLESCAN;
59 }
60
61 return;
62}
63
Dave Airlief453ba02008-11-07 14:05:41 -080064/**
Dave Airlie38651672010-03-30 05:34:13 +000065 * drm_helper_probe_single_connector_modes - get complete set of display modes
Dave Airlief453ba02008-11-07 14:05:41 -080066 * @dev: DRM device
67 * @maxX: max width for modes
68 * @maxY: max height for modes
69 *
70 * LOCKING:
71 * Caller must hold mode config lock.
72 *
73 * Based on @dev's mode_config layout, scan all the connectors and try to detect
74 * modes on them. Modes will first be added to the connector's probed_modes
75 * list, then culled (based on validity and the @maxX, @maxY parameters) and
76 * put into the normal modes list.
77 *
78 * Intended to be used either at bootup time or when major configuration
79 * changes have occurred.
80 *
81 * FIXME: take into account monitor limits
Jesse Barnes40a518d2009-01-12 12:05:32 -080082 *
83 * RETURNS:
84 * Number of modes found on @connector.
Dave Airlief453ba02008-11-07 14:05:41 -080085 */
Jesse Barnes40a518d2009-01-12 12:05:32 -080086int drm_helper_probe_single_connector_modes(struct drm_connector *connector,
87 uint32_t maxX, uint32_t maxY)
Dave Airlief453ba02008-11-07 14:05:41 -080088{
89 struct drm_device *dev = connector->dev;
90 struct drm_display_mode *mode, *t;
91 struct drm_connector_helper_funcs *connector_funcs =
92 connector->helper_private;
Jesse Barnes40a518d2009-01-12 12:05:32 -080093 int count = 0;
yakui_zhao67149772009-04-02 11:52:12 +080094 int mode_flags = 0;
Dave Airlief453ba02008-11-07 14:05:41 -080095
Jerome Glisse94401062010-07-15 15:43:25 -040096 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", connector->base.id,
97 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -080098 /* set all modes to the unverified state */
99 list_for_each_entry_safe(mode, t, &connector->modes, head)
100 mode->status = MODE_UNVERIFIED;
101
Dave Airlied50ba252009-09-23 14:44:08 +1000102 if (connector->force) {
103 if (connector->force == DRM_FORCE_ON)
104 connector->status = connector_status_connected;
105 else
106 connector->status = connector_status_disconnected;
107 if (connector->funcs->force)
108 connector->funcs->force(connector);
Chris Wilsone58f6372010-08-20 09:13:36 +0100109 } else {
Chris Wilson930a9e22010-09-14 11:07:23 +0100110 connector->status = connector->funcs->detect(connector, true);
Chris Wilson551402a2010-09-06 23:53:47 +0100111 drm_kms_helper_poll_enable(dev);
Chris Wilsone58f6372010-08-20 09:13:36 +0100112 }
Dave Airlief453ba02008-11-07 14:05:41 -0800113
114 if (connector->status == connector_status_disconnected) {
Jerome Glisse94401062010-07-15 15:43:25 -0400115 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] disconnected\n",
116 connector->base.id, drm_get_connector_name(connector));
Zhao Yakui72539832010-03-04 08:25:55 +0000117 drm_mode_connector_update_edid_property(connector, NULL);
Adam Jackson620f3782009-09-08 11:51:46 +1000118 goto prune;
Dave Airlief453ba02008-11-07 14:05:41 -0800119 }
120
Jesse Barnes40a518d2009-01-12 12:05:32 -0800121 count = (*connector_funcs->get_modes)(connector);
Chris Wilsonc7ef35a2010-09-05 16:55:25 +0100122 if (count == 0 && connector->status == connector_status_connected)
Adam Jackson9632b412009-11-23 14:23:07 -0500123 count = drm_add_modes_noedid(connector, 1024, 768);
Chris Wilsonc7ef35a2010-09-05 16:55:25 +0100124 if (count == 0)
125 goto prune;
Dave Airlief453ba02008-11-07 14:05:41 -0800126
Jesse Barnes40a518d2009-01-12 12:05:32 -0800127 drm_mode_connector_list_update(connector);
Dave Airlief453ba02008-11-07 14:05:41 -0800128
129 if (maxX && maxY)
130 drm_mode_validate_size(dev, &connector->modes, maxX,
131 maxY, 0);
yakui_zhao67149772009-04-02 11:52:12 +0800132
133 if (connector->interlace_allowed)
134 mode_flags |= DRM_MODE_FLAG_INTERLACE;
135 if (connector->doublescan_allowed)
136 mode_flags |= DRM_MODE_FLAG_DBLSCAN;
137 drm_mode_validate_flag(connector, mode_flags);
138
Dave Airlief453ba02008-11-07 14:05:41 -0800139 list_for_each_entry_safe(mode, t, &connector->modes, head) {
140 if (mode->status == MODE_OK)
141 mode->status = connector_funcs->mode_valid(connector,
142 mode);
143 }
144
Adam Jackson620f3782009-09-08 11:51:46 +1000145prune:
Dave Airlief453ba02008-11-07 14:05:41 -0800146 drm_mode_prune_invalid(dev, &connector->modes, true);
147
Jesse Barnes40a518d2009-01-12 12:05:32 -0800148 if (list_empty(&connector->modes))
149 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800150
151 drm_mode_sort(&connector->modes);
152
Jerome Glisse94401062010-07-15 15:43:25 -0400153 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] probed modes :\n", connector->base.id,
154 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -0800155 list_for_each_entry_safe(mode, t, &connector->modes, head) {
156 mode->vrefresh = drm_mode_vrefresh(mode);
157
158 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
159 drm_mode_debug_printmodeline(mode);
160 }
Jesse Barnes40a518d2009-01-12 12:05:32 -0800161
162 return count;
Dave Airlief453ba02008-11-07 14:05:41 -0800163}
164EXPORT_SYMBOL(drm_helper_probe_single_connector_modes);
165
Dave Airlief453ba02008-11-07 14:05:41 -0800166/**
Keith Packardc9fb15f2009-05-30 20:42:28 -0700167 * drm_helper_encoder_in_use - check if a given encoder is in use
168 * @encoder: encoder to check
169 *
170 * LOCKING:
171 * Caller must hold mode config lock.
172 *
173 * Walk @encoders's DRM device's mode_config and see if it's in use.
174 *
175 * RETURNS:
176 * True if @encoder is part of the mode_config, false otherwise.
177 */
178bool drm_helper_encoder_in_use(struct drm_encoder *encoder)
179{
180 struct drm_connector *connector;
181 struct drm_device *dev = encoder->dev;
182 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
183 if (connector->encoder == encoder)
184 return true;
185 return false;
186}
187EXPORT_SYMBOL(drm_helper_encoder_in_use);
188
189/**
Dave Airlief453ba02008-11-07 14:05:41 -0800190 * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config
191 * @crtc: CRTC to check
192 *
193 * LOCKING:
194 * Caller must hold mode config lock.
195 *
196 * Walk @crtc's DRM device's mode_config and see if it's in use.
197 *
198 * RETURNS:
199 * True if @crtc is part of the mode_config, false otherwise.
200 */
201bool drm_helper_crtc_in_use(struct drm_crtc *crtc)
202{
203 struct drm_encoder *encoder;
204 struct drm_device *dev = crtc->dev;
205 /* FIXME: Locking around list access? */
206 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
Keith Packardc9fb15f2009-05-30 20:42:28 -0700207 if (encoder->crtc == crtc && drm_helper_encoder_in_use(encoder))
Dave Airlief453ba02008-11-07 14:05:41 -0800208 return true;
209 return false;
210}
211EXPORT_SYMBOL(drm_helper_crtc_in_use);
212
Ben Skeggs86a1b9d2010-07-01 16:49:57 +1000213static void
214drm_encoder_disable(struct drm_encoder *encoder)
215{
216 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
217
218 if (encoder_funcs->disable)
219 (*encoder_funcs->disable)(encoder);
220 else
221 (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
222}
223
Dave Airlief453ba02008-11-07 14:05:41 -0800224/**
David John89347bb2009-12-31 12:00:46 +0530225 * drm_helper_disable_unused_functions - disable unused objects
Dave Airlief453ba02008-11-07 14:05:41 -0800226 * @dev: DRM device
227 *
228 * LOCKING:
229 * Caller must hold mode config lock.
230 *
231 * If an connector or CRTC isn't part of @dev's mode_config, it can be disabled
232 * by calling its dpms function, which should power it off.
233 */
234void drm_helper_disable_unused_functions(struct drm_device *dev)
235{
236 struct drm_encoder *encoder;
Dave Airliea3a05442009-08-31 15:16:30 +1000237 struct drm_connector *connector;
Dave Airlief453ba02008-11-07 14:05:41 -0800238 struct drm_crtc *crtc;
239
Dave Airliea3a05442009-08-31 15:16:30 +1000240 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
241 if (!connector->encoder)
242 continue;
243 if (connector->status == connector_status_disconnected)
244 connector->encoder = NULL;
245 }
246
Dave Airlief453ba02008-11-07 14:05:41 -0800247 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Dave Airlie929710212010-12-21 12:47:56 +1000248 if (!drm_helper_encoder_in_use(encoder)) {
Ben Skeggs86a1b9d2010-07-01 16:49:57 +1000249 drm_encoder_disable(encoder);
Dave Airlie9c552dd2009-09-02 14:00:11 +1000250 /* disconnector encoder from any connector */
251 encoder->crtc = NULL;
Dave Airliea3a05442009-08-31 15:16:30 +1000252 }
Dave Airlief453ba02008-11-07 14:05:41 -0800253 }
254
255 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
256 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
257 crtc->enabled = drm_helper_crtc_in_use(crtc);
258 if (!crtc->enabled) {
Alex Deucher5c8d7172010-06-11 17:04:35 -0400259 if (crtc_funcs->disable)
260 (*crtc_funcs->disable)(crtc);
261 else
262 (*crtc_funcs->dpms)(crtc, DRM_MODE_DPMS_OFF);
Dave Airlief453ba02008-11-07 14:05:41 -0800263 crtc->fb = NULL;
264 }
265 }
266}
267EXPORT_SYMBOL(drm_helper_disable_unused_functions);
268
Jesse Barnes7bec7562009-02-23 16:09:34 -0800269/**
270 * drm_encoder_crtc_ok - can a given crtc drive a given encoder?
271 * @encoder: encoder to test
272 * @crtc: crtc to test
273 *
274 * Return false if @encoder can't be driven by @crtc, true otherwise.
275 */
276static bool drm_encoder_crtc_ok(struct drm_encoder *encoder,
277 struct drm_crtc *crtc)
278{
279 struct drm_device *dev;
280 struct drm_crtc *tmp;
281 int crtc_mask = 1;
282
Joe Perchesfce7d612010-10-30 21:08:30 +0000283 WARN(!crtc, "checking null crtc?\n");
Jesse Barnes7bec7562009-02-23 16:09:34 -0800284
285 dev = crtc->dev;
286
287 list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
288 if (tmp == crtc)
289 break;
290 crtc_mask <<= 1;
291 }
292
293 if (encoder->possible_crtcs & crtc_mask)
294 return true;
295 return false;
296}
297
298/*
299 * Check the CRTC we're going to map each output to vs. its current
300 * CRTC. If they don't match, we have to disable the output and the CRTC
301 * since the driver will have to re-route things.
302 */
303static void
304drm_crtc_prepare_encoders(struct drm_device *dev)
305{
306 struct drm_encoder_helper_funcs *encoder_funcs;
307 struct drm_encoder *encoder;
308
309 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
310 encoder_funcs = encoder->helper_private;
311 /* Disable unused encoders */
312 if (encoder->crtc == NULL)
Ben Skeggs86a1b9d2010-07-01 16:49:57 +1000313 drm_encoder_disable(encoder);
Jesse Barnes7bec7562009-02-23 16:09:34 -0800314 /* Disable encoders whose CRTC is about to change */
315 if (encoder_funcs->get_crtc &&
316 encoder->crtc != (*encoder_funcs->get_crtc)(encoder))
Ben Skeggs86a1b9d2010-07-01 16:49:57 +1000317 drm_encoder_disable(encoder);
Jesse Barnes7bec7562009-02-23 16:09:34 -0800318 }
319}
320
Dave Airlief453ba02008-11-07 14:05:41 -0800321/**
322 * drm_crtc_set_mode - set a mode
323 * @crtc: CRTC to program
324 * @mode: mode to use
325 * @x: width of mode
326 * @y: height of mode
327 *
328 * LOCKING:
329 * Caller must hold mode config lock.
330 *
331 * Try to set @mode on @crtc. Give @crtc and its associated connectors a chance
332 * to fixup or reject the mode prior to trying to set it.
333 *
334 * RETURNS:
335 * True if the mode was set successfully, or false otherwise.
336 */
337bool drm_crtc_helper_set_mode(struct drm_crtc *crtc,
338 struct drm_display_mode *mode,
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -0500339 int x, int y,
340 struct drm_framebuffer *old_fb)
Dave Airlief453ba02008-11-07 14:05:41 -0800341{
342 struct drm_device *dev = crtc->dev;
Mario Kleiner27641c32010-10-23 04:20:23 +0200343 struct drm_display_mode *adjusted_mode, saved_mode, saved_hwmode;
Dave Airlief453ba02008-11-07 14:05:41 -0800344 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
345 struct drm_encoder_helper_funcs *encoder_funcs;
346 int saved_x, saved_y;
347 struct drm_encoder *encoder;
348 bool ret = true;
349
Dave Airlief453ba02008-11-07 14:05:41 -0800350 crtc->enabled = drm_helper_crtc_in_use(crtc);
Dave Airlief453ba02008-11-07 14:05:41 -0800351 if (!crtc->enabled)
352 return true;
353
Chris Wilson021a8452011-01-28 11:31:56 +0000354 adjusted_mode = drm_mode_duplicate(dev, mode);
355
Mario Kleiner27641c32010-10-23 04:20:23 +0200356 saved_hwmode = crtc->hwmode;
Dave Airlief453ba02008-11-07 14:05:41 -0800357 saved_mode = crtc->mode;
358 saved_x = crtc->x;
359 saved_y = crtc->y;
360
361 /* Update crtc values up front so the driver can rely on them for mode
362 * setting.
363 */
364 crtc->mode = *mode;
365 crtc->x = x;
366 crtc->y = y;
367
Dave Airlief453ba02008-11-07 14:05:41 -0800368 /* Pass our mode to the connectors and the CRTC to give them a chance to
369 * adjust it according to limitations or connector properties, and also
370 * a chance to reject the mode entirely.
371 */
372 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
373
374 if (encoder->crtc != crtc)
375 continue;
376 encoder_funcs = encoder->helper_private;
377 if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
378 adjusted_mode))) {
Adam Jackson836e53d2011-10-10 16:21:27 -0400379 DRM_DEBUG_KMS("Encoder fixup failed\n");
Dave Airlief453ba02008-11-07 14:05:41 -0800380 goto done;
381 }
382 }
383
384 if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) {
Adam Jackson836e53d2011-10-10 16:21:27 -0400385 DRM_DEBUG_KMS("CRTC fixup failed\n");
Dave Airlief453ba02008-11-07 14:05:41 -0800386 goto done;
387 }
Jerome Glisse94401062010-07-15 15:43:25 -0400388 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -0800389
390 /* Prepare the encoders and CRTCs before setting the mode. */
391 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
392
393 if (encoder->crtc != crtc)
394 continue;
395 encoder_funcs = encoder->helper_private;
396 /* Disable the encoders as the first thing we do. */
397 encoder_funcs->prepare(encoder);
398 }
399
Jesse Barnes7bec7562009-02-23 16:09:34 -0800400 drm_crtc_prepare_encoders(dev);
401
Dave Airlief453ba02008-11-07 14:05:41 -0800402 crtc_funcs->prepare(crtc);
403
404 /* Set up the DPLL and any encoders state that needs to adjust or depend
405 * on the DPLL.
406 */
Chris Wilson5c3b82e2009-02-11 13:25:09 +0000407 ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
408 if (!ret)
409 goto done;
Dave Airlief453ba02008-11-07 14:05:41 -0800410
411 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
412
413 if (encoder->crtc != crtc)
414 continue;
415
Jerome Glisse94401062010-07-15 15:43:25 -0400416 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
417 encoder->base.id, drm_get_encoder_name(encoder),
418 mode->base.id, mode->name);
Dave Airlief453ba02008-11-07 14:05:41 -0800419 encoder_funcs = encoder->helper_private;
420 encoder_funcs->mode_set(encoder, mode, adjusted_mode);
421 }
422
423 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
424 crtc_funcs->commit(crtc);
425
426 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
427
428 if (encoder->crtc != crtc)
429 continue;
430
431 encoder_funcs = encoder->helper_private;
432 encoder_funcs->commit(encoder);
433
434 }
435
Mario Kleiner27641c32010-10-23 04:20:23 +0200436 /* Store real post-adjustment hardware mode. */
437 crtc->hwmode = *adjusted_mode;
438
439 /* Calculate and store various constants which
440 * are later needed by vblank and swap-completion
441 * timestamping. They are derived from true hwmode.
442 */
443 drm_calc_timestamping_constants(crtc);
444
Dave Airlief453ba02008-11-07 14:05:41 -0800445 /* FIXME: add subpixel order */
446done:
Chris Wilson021a8452011-01-28 11:31:56 +0000447 drm_mode_destroy(dev, adjusted_mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800448 if (!ret) {
Mario Kleiner27641c32010-10-23 04:20:23 +0200449 crtc->hwmode = saved_hwmode;
Dave Airlief453ba02008-11-07 14:05:41 -0800450 crtc->mode = saved_mode;
451 crtc->x = saved_x;
452 crtc->y = saved_y;
453 }
454
455 return ret;
456}
457EXPORT_SYMBOL(drm_crtc_helper_set_mode);
458
459
460/**
461 * drm_crtc_helper_set_config - set a new config from userspace
462 * @crtc: CRTC to setup
463 * @crtc_info: user provided configuration
464 * @new_mode: new mode to set
465 * @connector_set: set of connectors for the new config
466 * @fb: new framebuffer
467 *
468 * LOCKING:
469 * Caller must hold mode config lock.
470 *
471 * Setup a new configuration, provided by the user in @crtc_info, and enable
472 * it.
473 *
474 * RETURNS:
475 * Zero. (FIXME)
476 */
477int drm_crtc_helper_set_config(struct drm_mode_set *set)
478{
479 struct drm_device *dev;
Maarten Maathuise67aae72009-08-27 10:18:29 +0200480 struct drm_crtc *save_crtcs, *new_crtc, *crtc;
481 struct drm_encoder *save_encoders, *new_encoder, *encoder;
Jesse Barnes7bec7562009-02-23 16:09:34 -0800482 struct drm_framebuffer *old_fb = NULL;
Jakob Bornecrantz4cb72b12009-08-03 13:43:59 +0100483 bool mode_changed = false; /* if true do a full mode set */
484 bool fb_changed = false; /* if true and !mode_changed just do a flip */
Maarten Maathuise67aae72009-08-27 10:18:29 +0200485 struct drm_connector *save_connectors, *connector;
Dave Airlief453ba02008-11-07 14:05:41 -0800486 int count = 0, ro, fail = 0;
487 struct drm_crtc_helper_funcs *crtc_funcs;
488 int ret = 0;
Keith Packardbf9dc102010-11-26 10:45:58 -0800489 int i;
Dave Airlief453ba02008-11-07 14:05:41 -0800490
Zhao Yakui58367ed2009-07-20 13:48:07 +0800491 DRM_DEBUG_KMS("\n");
Dave Airlief453ba02008-11-07 14:05:41 -0800492
493 if (!set)
494 return -EINVAL;
495
496 if (!set->crtc)
497 return -EINVAL;
498
499 if (!set->crtc->helper_private)
500 return -EINVAL;
501
502 crtc_funcs = set->crtc->helper_private;
503
Chris Wilsonede3ff52011-01-31 11:16:33 +0000504 if (!set->mode)
505 set->fb = NULL;
506
Jerome Glisse94401062010-07-15 15:43:25 -0400507 if (set->fb) {
508 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
509 set->crtc->base.id, set->fb->base.id,
510 (int)set->num_connectors, set->x, set->y);
511 } else {
Chris Wilsonede3ff52011-01-31 11:16:33 +0000512 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
513 set->mode = NULL;
514 set->num_connectors = 0;
Jerome Glisse94401062010-07-15 15:43:25 -0400515 }
Dave Airlief453ba02008-11-07 14:05:41 -0800516
517 dev = set->crtc->dev;
518
Maarten Maathuise67aae72009-08-27 10:18:29 +0200519 /* Allocate space for the backup of all (non-pointer) crtc, encoder and
520 * connector data. */
521 save_crtcs = kzalloc(dev->mode_config.num_crtc *
522 sizeof(struct drm_crtc), GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -0800523 if (!save_crtcs)
524 return -ENOMEM;
525
Maarten Maathuise67aae72009-08-27 10:18:29 +0200526 save_encoders = kzalloc(dev->mode_config.num_encoder *
527 sizeof(struct drm_encoder), GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -0800528 if (!save_encoders) {
529 kfree(save_crtcs);
530 return -ENOMEM;
531 }
532
Maarten Maathuise67aae72009-08-27 10:18:29 +0200533 save_connectors = kzalloc(dev->mode_config.num_connector *
534 sizeof(struct drm_connector), GFP_KERNEL);
535 if (!save_connectors) {
536 kfree(save_crtcs);
537 kfree(save_encoders);
538 return -ENOMEM;
539 }
540
541 /* Copy data. Note that driver private data is not affected.
542 * Should anything bad happen only the expected state is
543 * restored, not the drivers personal bookkeeping.
544 */
545 count = 0;
546 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
547 save_crtcs[count++] = *crtc;
548 }
549
550 count = 0;
551 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
552 save_encoders[count++] = *encoder;
553 }
554
555 count = 0;
556 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
557 save_connectors[count++] = *connector;
558 }
559
Dave Airlief453ba02008-11-07 14:05:41 -0800560 /* We should be able to check here if the fb has the same properties
561 * and then just flip_or_move it */
562 if (set->crtc->fb != set->fb) {
Jesse Barnes712531b2009-01-09 13:56:14 -0800563 /* If we have no fb then treat it as a full mode set */
Jesse Barnes7bec7562009-02-23 16:09:34 -0800564 if (set->crtc->fb == NULL) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800565 DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
Jesse Barnes712531b2009-01-09 13:56:14 -0800566 mode_changed = true;
Jakob Bornecrantz4cb72b12009-08-03 13:43:59 +0100567 } else if (set->fb == NULL) {
568 mode_changed = true;
Jesse Barnes46e48452011-06-24 12:19:26 -0700569 } else if (set->fb->depth != set->crtc->fb->depth) {
570 mode_changed = true;
571 } else if (set->fb->bits_per_pixel !=
572 set->crtc->fb->bits_per_pixel) {
573 mode_changed = true;
Dave Airlie8dff4742010-02-11 14:28:58 +1000574 } else
Jesse Barnes712531b2009-01-09 13:56:14 -0800575 fb_changed = true;
Dave Airlief453ba02008-11-07 14:05:41 -0800576 }
577
578 if (set->x != set->crtc->x || set->y != set->crtc->y)
Jesse Barnes712531b2009-01-09 13:56:14 -0800579 fb_changed = true;
Dave Airlief453ba02008-11-07 14:05:41 -0800580
581 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800582 DRM_DEBUG_KMS("modes are different, full mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -0800583 drm_mode_debug_printmodeline(&set->crtc->mode);
584 drm_mode_debug_printmodeline(set->mode);
Jesse Barnes712531b2009-01-09 13:56:14 -0800585 mode_changed = true;
Dave Airlief453ba02008-11-07 14:05:41 -0800586 }
587
588 /* a) traverse passed in connector list and get encoders for them */
589 count = 0;
590 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
591 struct drm_connector_helper_funcs *connector_funcs =
592 connector->helper_private;
Dave Airlief453ba02008-11-07 14:05:41 -0800593 new_encoder = connector->encoder;
594 for (ro = 0; ro < set->num_connectors; ro++) {
595 if (set->connectors[ro] == connector) {
596 new_encoder = connector_funcs->best_encoder(connector);
597 /* if we can't get an encoder for a connector
598 we are setting now - then fail */
599 if (new_encoder == NULL)
600 /* don't break so fail path works correct */
601 fail = 1;
602 break;
603 }
604 }
605
606 if (new_encoder != connector->encoder) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800607 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
Jesse Barnes712531b2009-01-09 13:56:14 -0800608 mode_changed = true;
Maarten Maathuisff846ab2009-08-19 00:56:45 +0200609 /* If the encoder is reused for another connector, then
610 * the appropriate crtc will be set later.
611 */
Maarten Maathuisff6fdbe2009-09-01 03:39:04 +0200612 if (connector->encoder)
613 connector->encoder->crtc = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800614 connector->encoder = new_encoder;
615 }
616 }
617
618 if (fail) {
619 ret = -EINVAL;
Maarten Maathuise67aae72009-08-27 10:18:29 +0200620 goto fail;
Dave Airlief453ba02008-11-07 14:05:41 -0800621 }
622
623 count = 0;
624 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
625 if (!connector->encoder)
626 continue;
627
Dave Airlief453ba02008-11-07 14:05:41 -0800628 if (connector->encoder->crtc == set->crtc)
629 new_crtc = NULL;
630 else
631 new_crtc = connector->encoder->crtc;
632
633 for (ro = 0; ro < set->num_connectors; ro++) {
634 if (set->connectors[ro] == connector)
635 new_crtc = set->crtc;
636 }
Jesse Barnes7bec7562009-02-23 16:09:34 -0800637
638 /* Make sure the new CRTC will work with the encoder */
639 if (new_crtc &&
640 !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
641 ret = -EINVAL;
Maarten Maathuise67aae72009-08-27 10:18:29 +0200642 goto fail;
Jesse Barnes7bec7562009-02-23 16:09:34 -0800643 }
Dave Airlief453ba02008-11-07 14:05:41 -0800644 if (new_crtc != connector->encoder->crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800645 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
Jesse Barnes712531b2009-01-09 13:56:14 -0800646 mode_changed = true;
Dave Airlief453ba02008-11-07 14:05:41 -0800647 connector->encoder->crtc = new_crtc;
648 }
Jerome Glisse94401062010-07-15 15:43:25 -0400649 if (new_crtc) {
650 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
651 connector->base.id, drm_get_connector_name(connector),
652 new_crtc->base.id);
653 } else {
654 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
655 connector->base.id, drm_get_connector_name(connector));
656 }
Dave Airlief453ba02008-11-07 14:05:41 -0800657 }
658
659 /* mode_set_base is not a required function */
Jesse Barnes712531b2009-01-09 13:56:14 -0800660 if (fb_changed && !crtc_funcs->mode_set_base)
661 mode_changed = true;
Dave Airlief453ba02008-11-07 14:05:41 -0800662
Jesse Barnes712531b2009-01-09 13:56:14 -0800663 if (mode_changed) {
Chris Wilson9334ef72011-01-28 11:53:03 +0000664 set->crtc->enabled = drm_helper_crtc_in_use(set->crtc);
665 if (set->crtc->enabled) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800666 DRM_DEBUG_KMS("attempting to set mode from"
667 " userspace\n");
Dave Airlief453ba02008-11-07 14:05:41 -0800668 drm_mode_debug_printmodeline(set->mode);
Chris Wilson356ad3c2010-09-09 09:41:32 +0100669 old_fb = set->crtc->fb;
670 set->crtc->fb = set->fb;
Dave Airlief453ba02008-11-07 14:05:41 -0800671 if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -0500672 set->x, set->y,
673 old_fb)) {
Jerome Glisse94401062010-07-15 15:43:25 -0400674 DRM_ERROR("failed to set mode on [CRTC:%d]\n",
675 set->crtc->base.id);
Chris Wilson0ba41e42011-01-08 15:10:41 +0000676 set->crtc->fb = old_fb;
Dave Airlief453ba02008-11-07 14:05:41 -0800677 ret = -EINVAL;
Maarten Maathuise67aae72009-08-27 10:18:29 +0200678 goto fail;
Dave Airlief453ba02008-11-07 14:05:41 -0800679 }
Keith Packard811aaa52011-02-03 16:57:28 -0800680 DRM_DEBUG_KMS("Setting connector DPMS state to on\n");
681 for (i = 0; i < set->num_connectors; i++) {
682 DRM_DEBUG_KMS("\t[CONNECTOR:%d:%s] set DPMS on\n", set->connectors[i]->base.id,
683 drm_get_connector_name(set->connectors[i]));
684 set->connectors[i]->dpms = DRM_MODE_DPMS_ON;
685 }
Dave Airlief453ba02008-11-07 14:05:41 -0800686 }
687 drm_helper_disable_unused_functions(dev);
Jesse Barnes712531b2009-01-09 13:56:14 -0800688 } else if (fb_changed) {
Ben Skeggs9b1596a2009-09-18 10:43:52 +1000689 set->crtc->x = set->x;
690 set->crtc->y = set->y;
691
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -0500692 old_fb = set->crtc->fb;
Dave Airlief453ba02008-11-07 14:05:41 -0800693 if (set->crtc->fb != set->fb)
694 set->crtc->fb = set->fb;
Chris Wilson5c3b82e2009-02-11 13:25:09 +0000695 ret = crtc_funcs->mode_set_base(set->crtc,
696 set->x, set->y, old_fb);
Chris Wilson0ba41e42011-01-08 15:10:41 +0000697 if (ret != 0) {
698 set->crtc->fb = old_fb;
Maarten Maathuise67aae72009-08-27 10:18:29 +0200699 goto fail;
Chris Wilson0ba41e42011-01-08 15:10:41 +0000700 }
Dave Airlief453ba02008-11-07 14:05:41 -0800701 }
702
Maarten Maathuise67aae72009-08-27 10:18:29 +0200703 kfree(save_connectors);
Dave Airlief453ba02008-11-07 14:05:41 -0800704 kfree(save_encoders);
705 kfree(save_crtcs);
706 return 0;
707
Maarten Maathuise67aae72009-08-27 10:18:29 +0200708fail:
709 /* Restore all previous data. */
Dave Airlief453ba02008-11-07 14:05:41 -0800710 count = 0;
Maarten Maathuise67aae72009-08-27 10:18:29 +0200711 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
712 *crtc = save_crtcs[count++];
713 }
Chris Wilsone62fb642009-02-11 16:39:21 +0000714
Maarten Maathuise67aae72009-08-27 10:18:29 +0200715 count = 0;
716 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
717 *encoder = save_encoders[count++];
Chris Wilsone62fb642009-02-11 16:39:21 +0000718 }
Maarten Maathuise67aae72009-08-27 10:18:29 +0200719
Dave Airlief453ba02008-11-07 14:05:41 -0800720 count = 0;
721 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Maarten Maathuise67aae72009-08-27 10:18:29 +0200722 *connector = save_connectors[count++];
Dave Airlief453ba02008-11-07 14:05:41 -0800723 }
Maarten Maathuise67aae72009-08-27 10:18:29 +0200724
725 kfree(save_connectors);
Dave Airlief453ba02008-11-07 14:05:41 -0800726 kfree(save_encoders);
Maarten Maathuise67aae72009-08-27 10:18:29 +0200727 kfree(save_crtcs);
Dave Airlief453ba02008-11-07 14:05:41 -0800728 return ret;
729}
730EXPORT_SYMBOL(drm_crtc_helper_set_config);
731
Keith Packardc9fb15f2009-05-30 20:42:28 -0700732static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder)
733{
734 int dpms = DRM_MODE_DPMS_OFF;
735 struct drm_connector *connector;
736 struct drm_device *dev = encoder->dev;
737
738 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
739 if (connector->encoder == encoder)
740 if (connector->dpms < dpms)
741 dpms = connector->dpms;
742 return dpms;
743}
744
745static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc)
746{
747 int dpms = DRM_MODE_DPMS_OFF;
748 struct drm_connector *connector;
749 struct drm_device *dev = crtc->dev;
750
751 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
752 if (connector->encoder && connector->encoder->crtc == crtc)
753 if (connector->dpms < dpms)
754 dpms = connector->dpms;
755 return dpms;
756}
757
758/**
759 * drm_helper_connector_dpms
760 * @connector affected connector
761 * @mode DPMS mode
762 *
763 * Calls the low-level connector DPMS function, then
764 * calls appropriate encoder and crtc DPMS functions as well
765 */
766void drm_helper_connector_dpms(struct drm_connector *connector, int mode)
767{
768 struct drm_encoder *encoder = connector->encoder;
769 struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
770 int old_dpms;
771
772 if (mode == connector->dpms)
773 return;
774
775 old_dpms = connector->dpms;
776 connector->dpms = mode;
777
778 /* from off to on, do crtc then encoder */
779 if (mode < old_dpms) {
780 if (crtc) {
781 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
782 if (crtc_funcs->dpms)
783 (*crtc_funcs->dpms) (crtc,
784 drm_helper_choose_crtc_dpms(crtc));
785 }
786 if (encoder) {
787 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
788 if (encoder_funcs->dpms)
789 (*encoder_funcs->dpms) (encoder,
790 drm_helper_choose_encoder_dpms(encoder));
791 }
792 }
793
794 /* from on to off, do encoder then crtc */
795 if (mode > old_dpms) {
796 if (encoder) {
797 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
798 if (encoder_funcs->dpms)
799 (*encoder_funcs->dpms) (encoder,
800 drm_helper_choose_encoder_dpms(encoder));
801 }
802 if (crtc) {
803 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
804 if (crtc_funcs->dpms)
805 (*crtc_funcs->dpms) (crtc,
806 drm_helper_choose_crtc_dpms(crtc));
807 }
808 }
809
810 return;
811}
812EXPORT_SYMBOL(drm_helper_connector_dpms);
813
Dave Airlief453ba02008-11-07 14:05:41 -0800814int drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb,
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800815 struct drm_mode_fb_cmd2 *mode_cmd)
Dave Airlief453ba02008-11-07 14:05:41 -0800816{
817 fb->width = mode_cmd->width;
818 fb->height = mode_cmd->height;
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800819 fb->pitch = mode_cmd->pitches[0];
Dave Airlie248dbc22011-11-29 20:02:54 +0000820 drm_fb_get_bpp_depth(mode_cmd->pixel_format, &fb->depth,
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800821 &fb->bits_per_pixel);
822 fb->pixel_format = mode_cmd->pixel_format;
Dave Airlief453ba02008-11-07 14:05:41 -0800823
824 return 0;
825}
826EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
827
828int drm_helper_resume_force_mode(struct drm_device *dev)
829{
830 struct drm_crtc *crtc;
David John89347bb2009-12-31 12:00:46 +0530831 struct drm_encoder *encoder;
832 struct drm_encoder_helper_funcs *encoder_funcs;
833 struct drm_crtc_helper_funcs *crtc_funcs;
Dave Airlief453ba02008-11-07 14:05:41 -0800834 int ret;
835
836 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
837
838 if (!crtc->enabled)
839 continue;
840
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -0500841 ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
842 crtc->x, crtc->y, crtc->fb);
Dave Airlief453ba02008-11-07 14:05:41 -0800843
844 if (ret == false)
845 DRM_ERROR("failed to set mode on crtc %p\n", crtc);
David John89347bb2009-12-31 12:00:46 +0530846
847 /* Turn off outputs that were already powered off */
848 if (drm_helper_choose_crtc_dpms(crtc)) {
849 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
850
851 if(encoder->crtc != crtc)
852 continue;
853
854 encoder_funcs = encoder->helper_private;
855 if (encoder_funcs->dpms)
856 (*encoder_funcs->dpms) (encoder,
857 drm_helper_choose_encoder_dpms(encoder));
David John89347bb2009-12-31 12:00:46 +0530858 }
Chris Wilson817e6312010-08-06 15:03:31 +0100859
860 crtc_funcs = crtc->helper_private;
861 if (crtc_funcs->dpms)
862 (*crtc_funcs->dpms) (crtc,
863 drm_helper_choose_crtc_dpms(crtc));
David John89347bb2009-12-31 12:00:46 +0530864 }
Dave Airlief453ba02008-11-07 14:05:41 -0800865 }
Zhao Yakuiaf4fcb52009-07-08 14:13:13 +0800866 /* disable the unused connectors while restoring the modesetting */
867 drm_helper_disable_unused_functions(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800868 return 0;
869}
870EXPORT_SYMBOL(drm_helper_resume_force_mode);
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000871
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000872#define DRM_OUTPUT_POLL_PERIOD (10*HZ)
Tejun Heo991ea752010-07-20 22:09:02 +0200873static void output_poll_execute(struct work_struct *work)
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000874{
Tejun Heo991ea752010-07-20 22:09:02 +0200875 struct delayed_work *delayed_work = to_delayed_work(work);
876 struct drm_device *dev = container_of(delayed_work, struct drm_device, mode_config.output_poll_work);
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000877 struct drm_connector *connector;
Keith Packardc5027de2010-11-26 10:45:59 -0800878 enum drm_connector_status old_status;
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000879 bool repoll = false, changed = false;
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000880
Chris Wilsone58f6372010-08-20 09:13:36 +0100881 if (!drm_kms_helper_poll)
882 return;
883
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000884 mutex_lock(&dev->mode_config.mutex);
885 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
886
887 /* if this is HPD or polled don't check it -
888 TV out for instance */
889 if (!connector->polled)
890 continue;
891
892 else if (connector->polled & (DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT))
893 repoll = true;
894
895 old_status = connector->status;
896 /* if we are connected and don't want to poll for disconnect
897 skip it */
898 if (old_status == connector_status_connected &&
899 !(connector->polled & DRM_CONNECTOR_POLL_DISCONNECT) &&
900 !(connector->polled & DRM_CONNECTOR_POLL_HPD))
901 continue;
902
Keith Packardc5027de2010-11-26 10:45:59 -0800903 connector->status = connector->funcs->detect(connector, false);
Chris Wilson0f168302010-12-21 22:49:28 +0000904 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %d to %d\n",
905 connector->base.id,
906 drm_get_connector_name(connector),
907 old_status, connector->status);
Keith Packardc5027de2010-11-26 10:45:59 -0800908 if (old_status != connector->status)
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000909 changed = true;
910 }
911
912 mutex_unlock(&dev->mode_config.mutex);
913
914 if (changed) {
915 /* send a uevent + call fbdev */
916 drm_sysfs_hotplug_event(dev);
917 if (dev->mode_config.funcs->output_poll_changed)
918 dev->mode_config.funcs->output_poll_changed(dev);
919 }
920
Tejun Heo9a919c42010-08-09 12:01:27 +0200921 if (repoll)
922 queue_delayed_work(system_nrt_wq, delayed_work, DRM_OUTPUT_POLL_PERIOD);
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000923}
924
Dave Airliefbf81762010-06-01 09:09:06 +1000925void drm_kms_helper_poll_disable(struct drm_device *dev)
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000926{
Dave Airliefbf81762010-06-01 09:09:06 +1000927 if (!dev->mode_config.poll_enabled)
928 return;
Tejun Heo991ea752010-07-20 22:09:02 +0200929 cancel_delayed_work_sync(&dev->mode_config.output_poll_work);
Dave Airliefbf81762010-06-01 09:09:06 +1000930}
931EXPORT_SYMBOL(drm_kms_helper_poll_disable);
932
933void drm_kms_helper_poll_enable(struct drm_device *dev)
934{
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000935 bool poll = false;
Dave Airliefbf81762010-06-01 09:09:06 +1000936 struct drm_connector *connector;
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000937
Chris Wilsone58f6372010-08-20 09:13:36 +0100938 if (!dev->mode_config.poll_enabled || !drm_kms_helper_poll)
939 return;
940
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000941 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
942 if (connector->polled)
943 poll = true;
944 }
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000945
Tejun Heo9a919c42010-08-09 12:01:27 +0200946 if (poll)
947 queue_delayed_work(system_nrt_wq, &dev->mode_config.output_poll_work, DRM_OUTPUT_POLL_PERIOD);
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000948}
Dave Airliefbf81762010-06-01 09:09:06 +1000949EXPORT_SYMBOL(drm_kms_helper_poll_enable);
950
951void drm_kms_helper_poll_init(struct drm_device *dev)
952{
Tejun Heo991ea752010-07-20 22:09:02 +0200953 INIT_DELAYED_WORK(&dev->mode_config.output_poll_work, output_poll_execute);
Dave Airliefbf81762010-06-01 09:09:06 +1000954 dev->mode_config.poll_enabled = true;
955
956 drm_kms_helper_poll_enable(dev);
957}
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000958EXPORT_SYMBOL(drm_kms_helper_poll_init);
959
960void drm_kms_helper_poll_fini(struct drm_device *dev)
961{
Dave Airliefbf81762010-06-01 09:09:06 +1000962 drm_kms_helper_poll_disable(dev);
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000963}
964EXPORT_SYMBOL(drm_kms_helper_poll_fini);
965
966void drm_helper_hpd_irq_event(struct drm_device *dev)
967{
968 if (!dev->mode_config.poll_enabled)
969 return;
Chris Wilsone58f6372010-08-20 09:13:36 +0100970
Tejun Heo991ea752010-07-20 22:09:02 +0200971 /* kill timer and schedule immediate execution, this doesn't block */
972 cancel_delayed_work(&dev->mode_config.output_poll_work);
Chris Wilsone58f6372010-08-20 09:13:36 +0100973 if (drm_kms_helper_poll)
974 queue_delayed_work(system_nrt_wq, &dev->mode_config.output_poll_work, 0);
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000975}
976EXPORT_SYMBOL(drm_helper_hpd_irq_event);