Hint added.
[AROS.git] / workbench / hidds / nouveau / drm / drm / drm_crtc_helper.c
blob6c391ea50adca2cd2aaf9df9aea832d8baf950d4
1 /*
2 * Copyright (c) 2006-2008 Intel Corporation
3 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
5 * DRM core CRTC related functions
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.
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.
25 * Authors:
26 * Keith Packard
27 * Eric Anholt <eric@anholt.net>
28 * Dave Airlie <airlied@linux.ie>
29 * Jesse Barnes <jesse.barnes@intel.com>
32 #include "drmP.h"
33 #include "drm_crtc.h"
34 #include "drm_crtc_helper.h"
35 #if !defined(__AROS__)
36 #include "drm_fb_helper.h"
38 static bool drm_kms_helper_poll = true;
39 module_param_named(poll, drm_kms_helper_poll, bool, 0600);
40 #endif
42 static void drm_mode_validate_flag(struct drm_connector *connector,
43 int flags)
45 struct drm_display_mode *mode, *t;
47 if (flags == (DRM_MODE_FLAG_DBLSCAN | DRM_MODE_FLAG_INTERLACE))
48 return;
50 list_for_each_entry_safe(mode, t, &connector->modes, head) {
51 if ((mode->flags & DRM_MODE_FLAG_INTERLACE) &&
52 !(flags & DRM_MODE_FLAG_INTERLACE))
53 mode->status = MODE_NO_INTERLACE;
54 if ((mode->flags & DRM_MODE_FLAG_DBLSCAN) &&
55 !(flags & DRM_MODE_FLAG_DBLSCAN))
56 mode->status = MODE_NO_DBLESCAN;
59 return;
62 /**
63 * drm_helper_probe_single_connector_modes - get complete set of display modes
64 * @dev: DRM device
65 * @maxX: max width for modes
66 * @maxY: max height for modes
68 * LOCKING:
69 * Caller must hold mode config lock.
71 * Based on @dev's mode_config layout, scan all the connectors and try to detect
72 * modes on them. Modes will first be added to the connector's probed_modes
73 * list, then culled (based on validity and the @maxX, @maxY parameters) and
74 * put into the normal modes list.
76 * Intended to be used either at bootup time or when major configuration
77 * changes have occurred.
79 * FIXME: take into account monitor limits
81 * RETURNS:
82 * Number of modes found on @connector.
84 int drm_helper_probe_single_connector_modes(struct drm_connector *connector,
85 uint32_t maxX, uint32_t maxY)
87 struct drm_device *dev = connector->dev;
88 struct drm_display_mode *mode, *t;
89 struct drm_connector_helper_funcs *connector_funcs =
90 connector->helper_private;
91 int count = 0;
92 int mode_flags = 0;
94 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", connector->base.id,
95 drm_get_connector_name(connector));
96 /* set all modes to the unverified state */
97 list_for_each_entry_safe(mode, t, &connector->modes, head)
98 mode->status = MODE_UNVERIFIED;
100 if (connector->force) {
101 if (connector->force == DRM_FORCE_ON)
102 connector->status = connector_status_connected;
103 else
104 connector->status = connector_status_disconnected;
105 if (connector->funcs->force)
106 connector->funcs->force(connector);
107 } else {
108 connector->status = connector->funcs->detect(connector, true);
109 #if !defined(__AROS__)
110 drm_kms_helper_poll_enable(dev);
111 #endif
114 if (connector->status == connector_status_disconnected) {
115 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] disconnected\n",
116 connector->base.id, drm_get_connector_name(connector));
117 drm_mode_connector_update_edid_property(connector, NULL);
118 goto prune;
121 count = (*connector_funcs->get_modes)(connector);
122 if (count == 0 && connector->status == connector_status_connected)
123 count = drm_add_modes_noedid(connector, 1024, 768);
124 if (count == 0)
125 goto prune;
127 drm_mode_connector_list_update(connector);
129 if (maxX && maxY)
130 drm_mode_validate_size(dev, &connector->modes, maxX,
131 maxY, 0);
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);
139 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);
145 prune:
146 drm_mode_prune_invalid(dev, &connector->modes, true);
148 if (list_empty(&connector->modes))
149 return 0;
151 drm_mode_sort(&connector->modes);
153 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] probed modes :\n", connector->base.id,
154 drm_get_connector_name(connector));
155 list_for_each_entry_safe(mode, t, &connector->modes, head) {
156 mode->vrefresh = drm_mode_vrefresh(mode);
158 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
159 drm_mode_debug_printmodeline(mode);
162 return count;
164 EXPORT_SYMBOL(drm_helper_probe_single_connector_modes);
167 * drm_helper_encoder_in_use - check if a given encoder is in use
168 * @encoder: encoder to check
170 * LOCKING:
171 * Caller must hold mode config lock.
173 * Walk @encoders's DRM device's mode_config and see if it's in use.
175 * RETURNS:
176 * True if @encoder is part of the mode_config, false otherwise.
178 bool drm_helper_encoder_in_use(struct drm_encoder *encoder)
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;
187 EXPORT_SYMBOL(drm_helper_encoder_in_use);
190 * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config
191 * @crtc: CRTC to check
193 * LOCKING:
194 * Caller must hold mode config lock.
196 * Walk @crtc's DRM device's mode_config and see if it's in use.
198 * RETURNS:
199 * True if @crtc is part of the mode_config, false otherwise.
201 bool drm_helper_crtc_in_use(struct drm_crtc *crtc)
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)
207 if (encoder->crtc == crtc && drm_helper_encoder_in_use(encoder))
208 return true;
209 return false;
211 EXPORT_SYMBOL(drm_helper_crtc_in_use);
213 static void
214 drm_encoder_disable(struct drm_encoder *encoder)
216 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
218 if (encoder_funcs->disable)
219 (*encoder_funcs->disable)(encoder);
220 else
221 (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
224 #if !defined(__AROS__)
226 * drm_helper_disable_unused_functions - disable unused objects
227 * @dev: DRM device
229 * LOCKING:
230 * Caller must hold mode config lock.
232 * If an connector or CRTC isn't part of @dev's mode_config, it can be disabled
233 * by calling its dpms function, which should power it off.
235 void drm_helper_disable_unused_functions(struct drm_device *dev)
237 struct drm_encoder *encoder;
238 struct drm_connector *connector;
239 struct drm_crtc *crtc;
241 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
242 if (!connector->encoder)
243 continue;
244 if (connector->status == connector_status_disconnected)
245 connector->encoder = NULL;
248 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
249 if (!drm_helper_encoder_in_use(encoder)) {
250 drm_encoder_disable(encoder);
251 /* disconnector encoder from any connector */
252 encoder->crtc = NULL;
256 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
257 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
258 crtc->enabled = drm_helper_crtc_in_use(crtc);
259 if (!crtc->enabled) {
260 if (crtc_funcs->disable)
261 (*crtc_funcs->disable)(crtc);
262 else
263 (*crtc_funcs->dpms)(crtc, DRM_MODE_DPMS_OFF);
264 crtc->fb = NULL;
268 EXPORT_SYMBOL(drm_helper_disable_unused_functions);
269 #endif
272 * drm_encoder_crtc_ok - can a given crtc drive a given encoder?
273 * @encoder: encoder to test
274 * @crtc: crtc to test
276 * Return false if @encoder can't be driven by @crtc, true otherwise.
278 static bool drm_encoder_crtc_ok(struct drm_encoder *encoder,
279 struct drm_crtc *crtc)
281 struct drm_device *dev;
282 struct drm_crtc *tmp;
283 int crtc_mask = 1;
285 WARN(!crtc, "checking null crtc?\n");
287 dev = crtc->dev;
289 list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
290 if (tmp == crtc)
291 break;
292 crtc_mask <<= 1;
295 if (encoder->possible_crtcs & crtc_mask)
296 return true;
297 return false;
301 * Check the CRTC we're going to map each output to vs. its current
302 * CRTC. If they don't match, we have to disable the output and the CRTC
303 * since the driver will have to re-route things.
305 static void
306 drm_crtc_prepare_encoders(struct drm_device *dev)
308 struct drm_encoder_helper_funcs *encoder_funcs;
309 struct drm_encoder *encoder;
311 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
312 encoder_funcs = encoder->helper_private;
313 /* Disable unused encoders */
314 if (encoder->crtc == NULL)
315 drm_encoder_disable(encoder);
316 /* Disable encoders whose CRTC is about to change */
317 if (encoder_funcs->get_crtc &&
318 encoder->crtc != (*encoder_funcs->get_crtc)(encoder))
319 drm_encoder_disable(encoder);
324 * drm_crtc_set_mode - set a mode
325 * @crtc: CRTC to program
326 * @mode: mode to use
327 * @x: width of mode
328 * @y: height of mode
330 * LOCKING:
331 * Caller must hold mode config lock.
333 * Try to set @mode on @crtc. Give @crtc and its associated connectors a chance
334 * to fixup or reject the mode prior to trying to set it.
336 * RETURNS:
337 * True if the mode was set successfully, or false otherwise.
339 bool drm_crtc_helper_set_mode(struct drm_crtc *crtc,
340 struct drm_display_mode *mode,
341 int x, int y,
342 struct drm_framebuffer *old_fb)
344 struct drm_device *dev = crtc->dev;
345 struct drm_display_mode *adjusted_mode, saved_mode, saved_hwmode;
346 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
347 struct drm_encoder_helper_funcs *encoder_funcs;
348 int saved_x, saved_y;
349 struct drm_encoder *encoder;
350 bool ret = true;
352 crtc->enabled = drm_helper_crtc_in_use(crtc);
353 if (!crtc->enabled)
354 return true;
356 adjusted_mode = drm_mode_duplicate(dev, mode);
358 saved_hwmode = crtc->hwmode;
359 saved_mode = crtc->mode;
360 saved_x = crtc->x;
361 saved_y = crtc->y;
363 /* Update crtc values up front so the driver can rely on them for mode
364 * setting.
366 crtc->mode = *mode;
367 crtc->x = x;
368 crtc->y = y;
370 /* Pass our mode to the connectors and the CRTC to give them a chance to
371 * adjust it according to limitations or connector properties, and also
372 * a chance to reject the mode entirely.
374 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
376 if (encoder->crtc != crtc)
377 continue;
378 encoder_funcs = encoder->helper_private;
379 if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
380 adjusted_mode))) {
381 goto done;
385 if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) {
386 goto done;
388 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
390 /* Prepare the encoders and CRTCs before setting the mode. */
391 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
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);
400 drm_crtc_prepare_encoders(dev);
402 crtc_funcs->prepare(crtc);
404 /* Set up the DPLL and any encoders state that needs to adjust or depend
405 * on the DPLL.
407 ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
408 if (!ret)
409 goto done;
411 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
413 if (encoder->crtc != crtc)
414 continue;
416 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);
419 encoder_funcs = encoder->helper_private;
420 encoder_funcs->mode_set(encoder, mode, adjusted_mode);
423 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
424 crtc_funcs->commit(crtc);
426 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
428 if (encoder->crtc != crtc)
429 continue;
431 encoder_funcs = encoder->helper_private;
432 encoder_funcs->commit(encoder);
436 /* Store real post-adjustment hardware mode. */
437 crtc->hwmode = *adjusted_mode;
439 #if !defined(__AROS__)
440 /* Calculate and store various constants which
441 * are later needed by vblank and swap-completion
442 * timestamping. They are derived from true hwmode.
444 drm_calc_timestamping_constants(crtc);
445 #endif
447 /* FIXME: add subpixel order */
448 done:
449 drm_mode_destroy(dev, adjusted_mode);
450 if (!ret) {
451 crtc->hwmode = saved_hwmode;
452 crtc->mode = saved_mode;
453 crtc->x = saved_x;
454 crtc->y = saved_y;
457 return ret;
459 EXPORT_SYMBOL(drm_crtc_helper_set_mode);
463 * drm_crtc_helper_set_config - set a new config from userspace
464 * @crtc: CRTC to setup
465 * @crtc_info: user provided configuration
466 * @new_mode: new mode to set
467 * @connector_set: set of connectors for the new config
468 * @fb: new framebuffer
470 * LOCKING:
471 * Caller must hold mode config lock.
473 * Setup a new configuration, provided by the user in @crtc_info, and enable
474 * it.
476 * RETURNS:
477 * Zero. (FIXME)
479 int drm_crtc_helper_set_config(struct drm_mode_set *set)
481 struct drm_device *dev;
482 struct drm_crtc *save_crtcs, *new_crtc, *crtc;
483 struct drm_encoder *save_encoders, *new_encoder, *encoder;
484 struct drm_framebuffer *old_fb = NULL;
485 bool mode_changed = false; /* if true do a full mode set */
486 bool fb_changed = false; /* if true and !mode_changed just do a flip */
487 struct drm_connector *save_connectors, *connector;
488 int count = 0, ro, fail = 0;
489 struct drm_crtc_helper_funcs *crtc_funcs;
490 int ret = 0;
491 int i;
493 DRM_DEBUG_KMS("\n");
495 if (!set)
496 return -EINVAL;
498 if (!set->crtc)
499 return -EINVAL;
501 if (!set->crtc->helper_private)
502 return -EINVAL;
504 crtc_funcs = set->crtc->helper_private;
506 if (!set->mode)
507 set->fb = NULL;
509 if (set->fb) {
510 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
511 set->crtc->base.id, set->fb->base.id,
512 (int)set->num_connectors, set->x, set->y);
513 } else {
514 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
515 set->mode = NULL;
516 set->num_connectors = 0;
519 dev = set->crtc->dev;
521 /* Allocate space for the backup of all (non-pointer) crtc, encoder and
522 * connector data. */
523 save_crtcs = kzalloc(dev->mode_config.num_crtc *
524 sizeof(struct drm_crtc), GFP_KERNEL);
525 if (!save_crtcs)
526 return -ENOMEM;
528 save_encoders = kzalloc(dev->mode_config.num_encoder *
529 sizeof(struct drm_encoder), GFP_KERNEL);
530 if (!save_encoders) {
531 kfree(save_crtcs);
532 return -ENOMEM;
535 save_connectors = kzalloc(dev->mode_config.num_connector *
536 sizeof(struct drm_connector), GFP_KERNEL);
537 if (!save_connectors) {
538 kfree(save_crtcs);
539 kfree(save_encoders);
540 return -ENOMEM;
543 /* Copy data. Note that driver private data is not affected.
544 * Should anything bad happen only the expected state is
545 * restored, not the drivers personal bookkeeping.
547 count = 0;
548 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
549 save_crtcs[count++] = *crtc;
552 count = 0;
553 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
554 save_encoders[count++] = *encoder;
557 count = 0;
558 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
559 save_connectors[count++] = *connector;
562 /* We should be able to check here if the fb has the same properties
563 * and then just flip_or_move it */
564 if (set->crtc->fb != set->fb) {
565 /* If we have no fb then treat it as a full mode set */
566 if (set->crtc->fb == NULL) {
567 DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
568 mode_changed = true;
569 } else if (set->fb == NULL) {
570 mode_changed = true;
571 } else if (set->fb->depth != set->crtc->fb->depth) {
572 mode_changed = true;
573 } else if (set->fb->bits_per_pixel !=
574 set->crtc->fb->bits_per_pixel) {
575 mode_changed = true;
576 } else
577 fb_changed = true;
580 if (set->x != set->crtc->x || set->y != set->crtc->y)
581 fb_changed = true;
583 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
584 DRM_DEBUG_KMS("modes are different, full mode set\n");
585 drm_mode_debug_printmodeline(&set->crtc->mode);
586 drm_mode_debug_printmodeline(set->mode);
587 mode_changed = true;
590 /* a) traverse passed in connector list and get encoders for them */
591 count = 0;
592 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
593 struct drm_connector_helper_funcs *connector_funcs =
594 connector->helper_private;
595 new_encoder = connector->encoder;
596 for (ro = 0; ro < set->num_connectors; ro++) {
597 if (set->connectors[ro] == connector) {
598 new_encoder = connector_funcs->best_encoder(connector);
599 /* if we can't get an encoder for a connector
600 we are setting now - then fail */
601 if (new_encoder == NULL)
602 /* don't break so fail path works correct */
603 fail = 1;
604 break;
608 if (new_encoder != connector->encoder) {
609 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
610 mode_changed = true;
611 /* If the encoder is reused for another connector, then
612 * the appropriate crtc will be set later.
614 if (connector->encoder)
615 connector->encoder->crtc = NULL;
616 connector->encoder = new_encoder;
620 if (fail) {
621 ret = -EINVAL;
622 goto fail;
625 count = 0;
626 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
627 if (!connector->encoder)
628 continue;
630 if (connector->encoder->crtc == set->crtc)
631 new_crtc = NULL;
632 else
633 new_crtc = connector->encoder->crtc;
635 for (ro = 0; ro < set->num_connectors; ro++) {
636 if (set->connectors[ro] == connector)
637 new_crtc = set->crtc;
640 /* Make sure the new CRTC will work with the encoder */
641 if (new_crtc &&
642 !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
643 ret = -EINVAL;
644 goto fail;
646 if (new_crtc != connector->encoder->crtc) {
647 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
648 mode_changed = true;
649 connector->encoder->crtc = new_crtc;
651 if (new_crtc) {
652 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
653 connector->base.id, drm_get_connector_name(connector),
654 new_crtc->base.id);
655 } else {
656 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
657 connector->base.id, drm_get_connector_name(connector));
661 /* mode_set_base is not a required function */
662 if (fb_changed && !crtc_funcs->mode_set_base)
663 mode_changed = true;
665 if (mode_changed) {
666 set->crtc->enabled = drm_helper_crtc_in_use(set->crtc);
667 if (set->crtc->enabled) {
668 DRM_DEBUG_KMS("attempting to set mode from"
669 " userspace\n");
670 drm_mode_debug_printmodeline(set->mode);
671 old_fb = set->crtc->fb;
672 set->crtc->fb = set->fb;
673 if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
674 set->x, set->y,
675 old_fb)) {
676 DRM_ERROR("failed to set mode on [CRTC:%d]\n",
677 set->crtc->base.id);
678 set->crtc->fb = old_fb;
679 ret = -EINVAL;
680 goto fail;
682 DRM_DEBUG_KMS("Setting connector DPMS state to on\n");
683 for (i = 0; i < set->num_connectors; i++) {
684 DRM_DEBUG_KMS("\t[CONNECTOR:%d:%s] set DPMS on\n", set->connectors[i]->base.id,
685 drm_get_connector_name(set->connectors[i]));
686 set->connectors[i]->dpms = DRM_MODE_DPMS_ON;
689 #if !defined(__AROS__)
690 drm_helper_disable_unused_functions(dev);
691 #endif
692 } else if (fb_changed) {
693 set->crtc->x = set->x;
694 set->crtc->y = set->y;
696 old_fb = set->crtc->fb;
697 if (set->crtc->fb != set->fb)
698 set->crtc->fb = set->fb;
699 ret = crtc_funcs->mode_set_base(set->crtc,
700 set->x, set->y, old_fb);
701 if (ret != 0) {
702 set->crtc->fb = old_fb;
703 goto fail;
707 kfree(save_connectors);
708 kfree(save_encoders);
709 kfree(save_crtcs);
710 return 0;
712 fail:
713 /* Restore all previous data. */
714 count = 0;
715 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
716 *crtc = save_crtcs[count++];
719 count = 0;
720 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
721 *encoder = save_encoders[count++];
724 count = 0;
725 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
726 *connector = save_connectors[count++];
729 kfree(save_connectors);
730 kfree(save_encoders);
731 kfree(save_crtcs);
732 return ret;
734 EXPORT_SYMBOL(drm_crtc_helper_set_config);
736 static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder)
738 int dpms = DRM_MODE_DPMS_OFF;
739 struct drm_connector *connector;
740 struct drm_device *dev = encoder->dev;
742 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
743 if (connector->encoder == encoder)
744 if (connector->dpms < dpms)
745 dpms = connector->dpms;
746 return dpms;
749 static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc)
751 int dpms = DRM_MODE_DPMS_OFF;
752 struct drm_connector *connector;
753 struct drm_device *dev = crtc->dev;
755 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
756 if (connector->encoder && connector->encoder->crtc == crtc)
757 if (connector->dpms < dpms)
758 dpms = connector->dpms;
759 return dpms;
763 * drm_helper_connector_dpms
764 * @connector affected connector
765 * @mode DPMS mode
767 * Calls the low-level connector DPMS function, then
768 * calls appropriate encoder and crtc DPMS functions as well
770 void drm_helper_connector_dpms(struct drm_connector *connector, int mode)
772 struct drm_encoder *encoder = connector->encoder;
773 struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
774 int old_dpms;
776 if (mode == connector->dpms)
777 return;
779 old_dpms = connector->dpms;
780 connector->dpms = mode;
782 /* from off to on, do crtc then encoder */
783 if (mode < old_dpms) {
784 if (crtc) {
785 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
786 if (crtc_funcs->dpms)
787 (*crtc_funcs->dpms) (crtc,
788 drm_helper_choose_crtc_dpms(crtc));
790 if (encoder) {
791 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
792 if (encoder_funcs->dpms)
793 (*encoder_funcs->dpms) (encoder,
794 drm_helper_choose_encoder_dpms(encoder));
798 /* from on to off, do encoder then crtc */
799 if (mode > old_dpms) {
800 if (encoder) {
801 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
802 if (encoder_funcs->dpms)
803 (*encoder_funcs->dpms) (encoder,
804 drm_helper_choose_encoder_dpms(encoder));
806 if (crtc) {
807 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
808 if (crtc_funcs->dpms)
809 (*crtc_funcs->dpms) (crtc,
810 drm_helper_choose_crtc_dpms(crtc));
814 return;
816 EXPORT_SYMBOL(drm_helper_connector_dpms);
818 int drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb,
819 struct drm_mode_fb_cmd *mode_cmd)
821 fb->width = mode_cmd->width;
822 fb->height = mode_cmd->height;
823 fb->pitch = mode_cmd->pitch;
824 fb->bits_per_pixel = mode_cmd->bpp;
825 fb->depth = mode_cmd->depth;
827 return 0;
829 EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
831 #if !defined(__AROS__)
832 int drm_helper_resume_force_mode(struct drm_device *dev)
834 struct drm_crtc *crtc;
835 struct drm_encoder *encoder;
836 struct drm_encoder_helper_funcs *encoder_funcs;
837 struct drm_crtc_helper_funcs *crtc_funcs;
838 int ret;
840 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
842 if (!crtc->enabled)
843 continue;
845 ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
846 crtc->x, crtc->y, crtc->fb);
848 if (ret == false)
849 DRM_ERROR("failed to set mode on crtc %p\n", crtc);
851 /* Turn off outputs that were already powered off */
852 if (drm_helper_choose_crtc_dpms(crtc)) {
853 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
855 if(encoder->crtc != crtc)
856 continue;
858 encoder_funcs = encoder->helper_private;
859 if (encoder_funcs->dpms)
860 (*encoder_funcs->dpms) (encoder,
861 drm_helper_choose_encoder_dpms(encoder));
864 crtc_funcs = crtc->helper_private;
865 if (crtc_funcs->dpms)
866 (*crtc_funcs->dpms) (crtc,
867 drm_helper_choose_crtc_dpms(crtc));
870 /* disable the unused connectors while restoring the modesetting */
871 drm_helper_disable_unused_functions(dev);
872 return 0;
874 EXPORT_SYMBOL(drm_helper_resume_force_mode);
876 #define DRM_OUTPUT_POLL_PERIOD (10*HZ)
877 static void output_poll_execute(struct work_struct *work)
879 struct delayed_work *delayed_work = to_delayed_work(work);
880 struct drm_device *dev = container_of(delayed_work, struct drm_device, mode_config.output_poll_work);
881 struct drm_connector *connector;
882 enum drm_connector_status old_status;
883 bool repoll = false, changed = false;
885 if (!drm_kms_helper_poll)
886 return;
888 mutex_lock(&dev->mode_config.mutex);
889 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
891 /* if this is HPD or polled don't check it -
892 TV out for instance */
893 if (!connector->polled)
894 continue;
896 else if (connector->polled & (DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT))
897 repoll = true;
899 old_status = connector->status;
900 /* if we are connected and don't want to poll for disconnect
901 skip it */
902 if (old_status == connector_status_connected &&
903 !(connector->polled & DRM_CONNECTOR_POLL_DISCONNECT) &&
904 !(connector->polled & DRM_CONNECTOR_POLL_HPD))
905 continue;
907 connector->status = connector->funcs->detect(connector, false);
908 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %d to %d\n",
909 connector->base.id,
910 drm_get_connector_name(connector),
911 old_status, connector->status);
912 if (old_status != connector->status)
913 changed = true;
916 mutex_unlock(&dev->mode_config.mutex);
918 if (changed) {
919 /* send a uevent + call fbdev */
920 drm_sysfs_hotplug_event(dev);
921 if (dev->mode_config.funcs->output_poll_changed)
922 dev->mode_config.funcs->output_poll_changed(dev);
925 if (repoll)
926 queue_delayed_work(system_nrt_wq, delayed_work, DRM_OUTPUT_POLL_PERIOD);
929 void drm_kms_helper_poll_disable(struct drm_device *dev)
931 if (!dev->mode_config.poll_enabled)
932 return;
933 cancel_delayed_work_sync(&dev->mode_config.output_poll_work);
935 EXPORT_SYMBOL(drm_kms_helper_poll_disable);
937 void drm_kms_helper_poll_enable(struct drm_device *dev)
939 bool poll = false;
940 struct drm_connector *connector;
942 if (!dev->mode_config.poll_enabled || !drm_kms_helper_poll)
943 return;
945 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
946 if (connector->polled)
947 poll = true;
950 if (poll)
951 queue_delayed_work(system_nrt_wq, &dev->mode_config.output_poll_work, DRM_OUTPUT_POLL_PERIOD);
953 EXPORT_SYMBOL(drm_kms_helper_poll_enable);
955 void drm_kms_helper_poll_init(struct drm_device *dev)
957 INIT_DELAYED_WORK(&dev->mode_config.output_poll_work, output_poll_execute);
958 dev->mode_config.poll_enabled = true;
960 drm_kms_helper_poll_enable(dev);
962 EXPORT_SYMBOL(drm_kms_helper_poll_init);
964 void drm_kms_helper_poll_fini(struct drm_device *dev)
966 drm_kms_helper_poll_disable(dev);
968 EXPORT_SYMBOL(drm_kms_helper_poll_fini);
970 void drm_helper_hpd_irq_event(struct drm_device *dev)
972 if (!dev->mode_config.poll_enabled)
973 return;
975 /* kill timer and schedule immediate execution, this doesn't block */
976 cancel_delayed_work(&dev->mode_config.output_poll_work);
977 if (drm_kms_helper_poll)
978 queue_delayed_work(system_nrt_wq, &dev->mode_config.output_poll_work, 0);
980 EXPORT_SYMBOL(drm_helper_hpd_irq_event);
981 #endif