added 2.6.29.6 aldebaran kernel
[nao-ulib.git] / kernel / 2.6.29.6-aldebaran-rt / drivers / gpu / drm / drm_crtc_helper.c
blob1c3a8c5571408a6f0a698a53b58f93f6f471a9a9
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"
37 * Detailed mode info for 800x600@60Hz
39 static struct drm_display_mode std_modes[] = {
40 { DRM_MODE("800x600", DRM_MODE_TYPE_DEFAULT, 40000, 800, 840,
41 968, 1056, 0, 600, 601, 605, 628, 0,
42 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
45 /**
46 * drm_helper_probe_connector_modes - get complete set of display modes
47 * @dev: DRM device
48 * @maxX: max width for modes
49 * @maxY: max height for modes
51 * LOCKING:
52 * Caller must hold mode config lock.
54 * Based on @dev's mode_config layout, scan all the connectors and try to detect
55 * modes on them. Modes will first be added to the connector's probed_modes
56 * list, then culled (based on validity and the @maxX, @maxY parameters) and
57 * put into the normal modes list.
59 * Intended to be used either at bootup time or when major configuration
60 * changes have occurred.
62 * FIXME: take into account monitor limits
64 * RETURNS:
65 * Number of modes found on @connector.
67 int drm_helper_probe_single_connector_modes(struct drm_connector *connector,
68 uint32_t maxX, uint32_t maxY)
70 struct drm_device *dev = connector->dev;
71 struct drm_display_mode *mode, *t;
72 struct drm_connector_helper_funcs *connector_funcs =
73 connector->helper_private;
74 int count = 0;
76 DRM_DEBUG("%s\n", drm_get_connector_name(connector));
77 /* set all modes to the unverified state */
78 list_for_each_entry_safe(mode, t, &connector->modes, head)
79 mode->status = MODE_UNVERIFIED;
81 connector->status = connector->funcs->detect(connector);
83 if (connector->status == connector_status_disconnected) {
84 DRM_DEBUG("%s is disconnected\n",
85 drm_get_connector_name(connector));
86 /* TODO set EDID to NULL */
87 return 0;
90 count = (*connector_funcs->get_modes)(connector);
91 if (!count)
92 return 0;
94 drm_mode_connector_list_update(connector);
96 if (maxX && maxY)
97 drm_mode_validate_size(dev, &connector->modes, maxX,
98 maxY, 0);
99 list_for_each_entry_safe(mode, t, &connector->modes, head) {
100 if (mode->status == MODE_OK)
101 mode->status = connector_funcs->mode_valid(connector,
102 mode);
106 drm_mode_prune_invalid(dev, &connector->modes, true);
108 if (list_empty(&connector->modes))
109 return 0;
111 drm_mode_sort(&connector->modes);
113 DRM_DEBUG("Probed modes for %s\n", drm_get_connector_name(connector));
114 list_for_each_entry_safe(mode, t, &connector->modes, head) {
115 mode->vrefresh = drm_mode_vrefresh(mode);
117 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
118 drm_mode_debug_printmodeline(mode);
121 return count;
123 EXPORT_SYMBOL(drm_helper_probe_single_connector_modes);
125 int drm_helper_probe_connector_modes(struct drm_device *dev, uint32_t maxX,
126 uint32_t maxY)
128 struct drm_connector *connector;
129 int count = 0;
131 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
132 count += drm_helper_probe_single_connector_modes(connector,
133 maxX, maxY);
136 return count;
138 EXPORT_SYMBOL(drm_helper_probe_connector_modes);
140 static void drm_helper_add_std_modes(struct drm_device *dev,
141 struct drm_connector *connector)
143 struct drm_display_mode *mode, *t;
144 int i;
146 for (i = 0; i < ARRAY_SIZE(std_modes); i++) {
147 struct drm_display_mode *stdmode;
150 * When no valid EDID modes are available we end up
151 * here and bailed in the past, now we add some standard
152 * modes and move on.
154 stdmode = drm_mode_duplicate(dev, &std_modes[i]);
155 drm_mode_probed_add(connector, stdmode);
156 drm_mode_list_concat(&connector->probed_modes,
157 &connector->modes);
159 DRM_DEBUG("Adding mode %s to %s\n", stdmode->name,
160 drm_get_connector_name(connector));
162 drm_mode_sort(&connector->modes);
164 DRM_DEBUG("Added std modes on %s\n", drm_get_connector_name(connector));
165 list_for_each_entry_safe(mode, t, &connector->modes, head) {
166 mode->vrefresh = drm_mode_vrefresh(mode);
168 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
169 drm_mode_debug_printmodeline(mode);
174 * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config
175 * @crtc: CRTC to check
177 * LOCKING:
178 * Caller must hold mode config lock.
180 * Walk @crtc's DRM device's mode_config and see if it's in use.
182 * RETURNS:
183 * True if @crtc is part of the mode_config, false otherwise.
185 bool drm_helper_crtc_in_use(struct drm_crtc *crtc)
187 struct drm_encoder *encoder;
188 struct drm_device *dev = crtc->dev;
189 /* FIXME: Locking around list access? */
190 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
191 if (encoder->crtc == crtc)
192 return true;
193 return false;
195 EXPORT_SYMBOL(drm_helper_crtc_in_use);
198 * drm_disable_unused_functions - disable unused objects
199 * @dev: DRM device
201 * LOCKING:
202 * Caller must hold mode config lock.
204 * If an connector or CRTC isn't part of @dev's mode_config, it can be disabled
205 * by calling its dpms function, which should power it off.
207 void drm_helper_disable_unused_functions(struct drm_device *dev)
209 struct drm_encoder *encoder;
210 struct drm_encoder_helper_funcs *encoder_funcs;
211 struct drm_crtc *crtc;
213 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
214 encoder_funcs = encoder->helper_private;
215 if (!encoder->crtc)
216 (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
219 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
220 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
221 crtc->enabled = drm_helper_crtc_in_use(crtc);
222 if (!crtc->enabled) {
223 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
224 crtc->fb = NULL;
228 EXPORT_SYMBOL(drm_helper_disable_unused_functions);
230 static struct drm_display_mode *drm_has_preferred_mode(struct drm_connector *connector, int width, int height)
232 struct drm_display_mode *mode;
234 list_for_each_entry(mode, &connector->modes, head) {
235 if (drm_mode_width(mode) > width ||
236 drm_mode_height(mode) > height)
237 continue;
238 if (mode->type & DRM_MODE_TYPE_PREFERRED)
239 return mode;
241 return NULL;
244 static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
246 bool enable;
248 if (strict) {
249 enable = connector->status == connector_status_connected;
250 } else {
251 enable = connector->status != connector_status_disconnected;
253 return enable;
256 static void drm_enable_connectors(struct drm_device *dev, bool *enabled)
258 bool any_enabled = false;
259 struct drm_connector *connector;
260 int i = 0;
262 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
263 enabled[i] = drm_connector_enabled(connector, true);
264 DRM_DEBUG("connector %d enabled? %s\n", connector->base.id,
265 enabled[i] ? "yes" : "no");
266 any_enabled |= enabled[i];
267 i++;
270 if (any_enabled)
271 return;
273 i = 0;
274 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
275 enabled[i] = drm_connector_enabled(connector, false);
276 i++;
280 static bool drm_target_preferred(struct drm_device *dev,
281 struct drm_display_mode **modes,
282 bool *enabled, int width, int height)
284 struct drm_connector *connector;
285 int i = 0;
287 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
289 if (enabled[i] == false) {
290 i++;
291 continue;
294 DRM_DEBUG("looking for preferred mode on connector %d\n",
295 connector->base.id);
297 modes[i] = drm_has_preferred_mode(connector, width, height);
298 /* No preferred modes, pick one off the list */
299 if (!modes[i] && !list_empty(&connector->modes)) {
300 list_for_each_entry(modes[i], &connector->modes, head)
301 break;
303 DRM_DEBUG("found mode %s\n", modes[i] ? modes[i]->name :
304 "none");
305 i++;
307 return true;
310 static int drm_pick_crtcs(struct drm_device *dev,
311 struct drm_crtc **best_crtcs,
312 struct drm_display_mode **modes,
313 int n, int width, int height)
315 int c, o;
316 struct drm_connector *connector;
317 struct drm_connector_helper_funcs *connector_funcs;
318 struct drm_encoder *encoder;
319 struct drm_crtc *best_crtc;
320 int my_score, best_score, score;
321 struct drm_crtc **crtcs, *crtc;
323 if (n == dev->mode_config.num_connector)
324 return 0;
325 c = 0;
326 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
327 if (c == n)
328 break;
329 c++;
332 best_crtcs[n] = NULL;
333 best_crtc = NULL;
334 best_score = drm_pick_crtcs(dev, best_crtcs, modes, n+1, width, height);
335 if (modes[n] == NULL)
336 return best_score;
338 crtcs = kmalloc(dev->mode_config.num_connector *
339 sizeof(struct drm_crtc *), GFP_KERNEL);
340 if (!crtcs)
341 return best_score;
343 my_score = 1;
344 if (connector->status == connector_status_connected)
345 my_score++;
346 if (drm_has_preferred_mode(connector, width, height))
347 my_score++;
349 connector_funcs = connector->helper_private;
350 encoder = connector_funcs->best_encoder(connector);
351 if (!encoder)
352 goto out;
354 connector->encoder = encoder;
356 /* select a crtc for this connector and then attempt to configure
357 remaining connectors */
358 c = 0;
359 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
361 if ((connector->encoder->possible_crtcs & (1 << c)) == 0) {
362 c++;
363 continue;
366 for (o = 0; o < n; o++)
367 if (best_crtcs[o] == crtc)
368 break;
370 if (o < n) {
371 /* ignore cloning for now */
372 c++;
373 continue;
376 crtcs[n] = crtc;
377 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_crtc *));
378 score = my_score + drm_pick_crtcs(dev, crtcs, modes, n + 1,
379 width, height);
380 if (score > best_score) {
381 best_crtc = crtc;
382 best_score = score;
383 memcpy(best_crtcs, crtcs,
384 dev->mode_config.num_connector *
385 sizeof(struct drm_crtc *));
387 c++;
389 out:
390 kfree(crtcs);
391 return best_score;
394 static void drm_setup_crtcs(struct drm_device *dev)
396 struct drm_crtc **crtcs;
397 struct drm_display_mode **modes;
398 struct drm_encoder *encoder;
399 struct drm_connector *connector;
400 bool *enabled;
401 int width, height;
402 int i, ret;
404 DRM_DEBUG("\n");
406 width = dev->mode_config.max_width;
407 height = dev->mode_config.max_height;
409 /* clean out all the encoder/crtc combos */
410 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
411 encoder->crtc = NULL;
414 crtcs = kcalloc(dev->mode_config.num_connector,
415 sizeof(struct drm_crtc *), GFP_KERNEL);
416 modes = kcalloc(dev->mode_config.num_connector,
417 sizeof(struct drm_display_mode *), GFP_KERNEL);
418 enabled = kcalloc(dev->mode_config.num_connector,
419 sizeof(bool), GFP_KERNEL);
421 drm_enable_connectors(dev, enabled);
423 ret = drm_target_preferred(dev, modes, enabled, width, height);
424 if (!ret)
425 DRM_ERROR("Unable to find initial modes\n");
427 DRM_DEBUG("picking CRTCs for %dx%d config\n", width, height);
429 drm_pick_crtcs(dev, crtcs, modes, 0, width, height);
431 i = 0;
432 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
433 struct drm_display_mode *mode = modes[i];
434 struct drm_crtc *crtc = crtcs[i];
436 if (connector->encoder == NULL) {
437 i++;
438 continue;
441 if (mode && crtc) {
442 DRM_DEBUG("desired mode %s set on crtc %d\n",
443 mode->name, crtc->base.id);
444 crtc->desired_mode = mode;
445 connector->encoder->crtc = crtc;
446 } else
447 connector->encoder->crtc = NULL;
448 i++;
451 kfree(crtcs);
452 kfree(modes);
453 kfree(enabled);
457 * drm_encoder_crtc_ok - can a given crtc drive a given encoder?
458 * @encoder: encoder to test
459 * @crtc: crtc to test
461 * Return false if @encoder can't be driven by @crtc, true otherwise.
463 static bool drm_encoder_crtc_ok(struct drm_encoder *encoder,
464 struct drm_crtc *crtc)
466 struct drm_device *dev;
467 struct drm_crtc *tmp;
468 int crtc_mask = 1;
470 WARN(!crtc, "checking null crtc?");
472 dev = crtc->dev;
474 list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
475 if (tmp == crtc)
476 break;
477 crtc_mask <<= 1;
480 if (encoder->possible_crtcs & crtc_mask)
481 return true;
482 return false;
486 * Check the CRTC we're going to map each output to vs. its current
487 * CRTC. If they don't match, we have to disable the output and the CRTC
488 * since the driver will have to re-route things.
490 static void
491 drm_crtc_prepare_encoders(struct drm_device *dev)
493 struct drm_encoder_helper_funcs *encoder_funcs;
494 struct drm_encoder *encoder;
496 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
497 encoder_funcs = encoder->helper_private;
498 /* Disable unused encoders */
499 if (encoder->crtc == NULL)
500 (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
501 /* Disable encoders whose CRTC is about to change */
502 if (encoder_funcs->get_crtc &&
503 encoder->crtc != (*encoder_funcs->get_crtc)(encoder))
504 (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
509 * drm_crtc_set_mode - set a mode
510 * @crtc: CRTC to program
511 * @mode: mode to use
512 * @x: width of mode
513 * @y: height of mode
515 * LOCKING:
516 * Caller must hold mode config lock.
518 * Try to set @mode on @crtc. Give @crtc and its associated connectors a chance
519 * to fixup or reject the mode prior to trying to set it.
521 * RETURNS:
522 * True if the mode was set successfully, or false otherwise.
524 bool drm_crtc_helper_set_mode(struct drm_crtc *crtc,
525 struct drm_display_mode *mode,
526 int x, int y,
527 struct drm_framebuffer *old_fb)
529 struct drm_device *dev = crtc->dev;
530 struct drm_display_mode *adjusted_mode, saved_mode;
531 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
532 struct drm_encoder_helper_funcs *encoder_funcs;
533 int saved_x, saved_y;
534 struct drm_encoder *encoder;
535 bool ret = true;
536 bool depth_changed, bpp_changed;
538 adjusted_mode = drm_mode_duplicate(dev, mode);
540 crtc->enabled = drm_helper_crtc_in_use(crtc);
542 if (!crtc->enabled)
543 return true;
545 if (old_fb && crtc->fb) {
546 depth_changed = (old_fb->depth != crtc->fb->depth);
547 bpp_changed = (old_fb->bits_per_pixel !=
548 crtc->fb->bits_per_pixel);
549 } else {
550 depth_changed = true;
551 bpp_changed = true;
554 saved_mode = crtc->mode;
555 saved_x = crtc->x;
556 saved_y = crtc->y;
558 /* Update crtc values up front so the driver can rely on them for mode
559 * setting.
561 crtc->mode = *mode;
562 crtc->x = x;
563 crtc->y = y;
565 if (drm_mode_equal(&saved_mode, &crtc->mode)) {
566 if (saved_x != crtc->x || saved_y != crtc->y ||
567 depth_changed || bpp_changed) {
568 ret = !crtc_funcs->mode_set_base(crtc, crtc->x, crtc->y,
569 old_fb);
570 goto done;
574 /* Pass our mode to the connectors and the CRTC to give them a chance to
575 * adjust it according to limitations or connector properties, and also
576 * a chance to reject the mode entirely.
578 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
580 if (encoder->crtc != crtc)
581 continue;
582 encoder_funcs = encoder->helper_private;
583 if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
584 adjusted_mode))) {
585 goto done;
589 if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) {
590 goto done;
593 /* Prepare the encoders and CRTCs before setting the mode. */
594 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
596 if (encoder->crtc != crtc)
597 continue;
598 encoder_funcs = encoder->helper_private;
599 /* Disable the encoders as the first thing we do. */
600 encoder_funcs->prepare(encoder);
603 drm_crtc_prepare_encoders(dev);
605 crtc_funcs->prepare(crtc);
607 /* Set up the DPLL and any encoders state that needs to adjust or depend
608 * on the DPLL.
610 ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
611 if (!ret)
612 goto done;
614 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
616 if (encoder->crtc != crtc)
617 continue;
619 DRM_INFO("%s: set mode %s %x\n", drm_get_encoder_name(encoder),
620 mode->name, mode->base.id);
621 encoder_funcs = encoder->helper_private;
622 encoder_funcs->mode_set(encoder, mode, adjusted_mode);
625 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
626 crtc_funcs->commit(crtc);
628 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
630 if (encoder->crtc != crtc)
631 continue;
633 encoder_funcs = encoder->helper_private;
634 encoder_funcs->commit(encoder);
638 /* XXX free adjustedmode */
639 drm_mode_destroy(dev, adjusted_mode);
640 /* FIXME: add subpixel order */
641 done:
642 if (!ret) {
643 crtc->mode = saved_mode;
644 crtc->x = saved_x;
645 crtc->y = saved_y;
648 return ret;
650 EXPORT_SYMBOL(drm_crtc_helper_set_mode);
654 * drm_crtc_helper_set_config - set a new config from userspace
655 * @crtc: CRTC to setup
656 * @crtc_info: user provided configuration
657 * @new_mode: new mode to set
658 * @connector_set: set of connectors for the new config
659 * @fb: new framebuffer
661 * LOCKING:
662 * Caller must hold mode config lock.
664 * Setup a new configuration, provided by the user in @crtc_info, and enable
665 * it.
667 * RETURNS:
668 * Zero. (FIXME)
670 int drm_crtc_helper_set_config(struct drm_mode_set *set)
672 struct drm_device *dev;
673 struct drm_crtc **save_crtcs, *new_crtc;
674 struct drm_encoder **save_encoders, *new_encoder;
675 struct drm_framebuffer *old_fb = NULL;
676 bool save_enabled;
677 bool mode_changed = false;
678 bool fb_changed = false;
679 struct drm_connector *connector;
680 int count = 0, ro, fail = 0;
681 struct drm_crtc_helper_funcs *crtc_funcs;
682 int ret = 0;
684 DRM_DEBUG("\n");
686 if (!set)
687 return -EINVAL;
689 if (!set->crtc)
690 return -EINVAL;
692 if (!set->crtc->helper_private)
693 return -EINVAL;
695 crtc_funcs = set->crtc->helper_private;
697 DRM_DEBUG("crtc: %p %d fb: %p connectors: %p num_connectors: %d (x, y) (%i, %i)\n",
698 set->crtc, set->crtc->base.id, set->fb, set->connectors,
699 (int)set->num_connectors, set->x, set->y);
701 dev = set->crtc->dev;
703 /* save previous config */
704 save_enabled = set->crtc->enabled;
707 * We do mode_config.num_connectors here since we'll look at the
708 * CRTC and encoder associated with each connector later.
710 save_crtcs = kzalloc(dev->mode_config.num_connector *
711 sizeof(struct drm_crtc *), GFP_KERNEL);
712 if (!save_crtcs)
713 return -ENOMEM;
715 save_encoders = kzalloc(dev->mode_config.num_connector *
716 sizeof(struct drm_encoders *), GFP_KERNEL);
717 if (!save_encoders) {
718 kfree(save_crtcs);
719 return -ENOMEM;
722 /* We should be able to check here if the fb has the same properties
723 * and then just flip_or_move it */
724 if (set->crtc->fb != set->fb) {
725 /* If we have no fb then treat it as a full mode set */
726 if (set->crtc->fb == NULL) {
727 DRM_DEBUG("crtc has no fb, full mode set\n");
728 mode_changed = true;
729 } else if ((set->fb->bits_per_pixel !=
730 set->crtc->fb->bits_per_pixel) ||
731 set->fb->depth != set->crtc->fb->depth)
732 fb_changed = true;
733 else
734 fb_changed = true;
737 if (set->x != set->crtc->x || set->y != set->crtc->y)
738 fb_changed = true;
740 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
741 DRM_DEBUG("modes are different, full mode set\n");
742 drm_mode_debug_printmodeline(&set->crtc->mode);
743 drm_mode_debug_printmodeline(set->mode);
744 mode_changed = true;
747 /* a) traverse passed in connector list and get encoders for them */
748 count = 0;
749 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
750 struct drm_connector_helper_funcs *connector_funcs =
751 connector->helper_private;
752 save_encoders[count++] = connector->encoder;
753 new_encoder = connector->encoder;
754 for (ro = 0; ro < set->num_connectors; ro++) {
755 if (set->connectors[ro] == connector) {
756 new_encoder = connector_funcs->best_encoder(connector);
757 /* if we can't get an encoder for a connector
758 we are setting now - then fail */
759 if (new_encoder == NULL)
760 /* don't break so fail path works correct */
761 fail = 1;
762 break;
766 if (new_encoder != connector->encoder) {
767 DRM_DEBUG("encoder changed, full mode switch\n");
768 mode_changed = true;
769 connector->encoder = new_encoder;
773 if (fail) {
774 ret = -EINVAL;
775 goto fail_no_encoder;
778 count = 0;
779 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
780 if (!connector->encoder)
781 continue;
783 save_crtcs[count++] = connector->encoder->crtc;
785 if (connector->encoder->crtc == set->crtc)
786 new_crtc = NULL;
787 else
788 new_crtc = connector->encoder->crtc;
790 for (ro = 0; ro < set->num_connectors; ro++) {
791 if (set->connectors[ro] == connector)
792 new_crtc = set->crtc;
795 /* Make sure the new CRTC will work with the encoder */
796 if (new_crtc &&
797 !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
798 ret = -EINVAL;
799 goto fail_set_mode;
801 if (new_crtc != connector->encoder->crtc) {
802 DRM_DEBUG("crtc changed, full mode switch\n");
803 mode_changed = true;
804 connector->encoder->crtc = new_crtc;
806 DRM_DEBUG("setting connector %d crtc to %p\n",
807 connector->base.id, new_crtc);
810 /* mode_set_base is not a required function */
811 if (fb_changed && !crtc_funcs->mode_set_base)
812 mode_changed = true;
814 if (mode_changed) {
815 old_fb = set->crtc->fb;
816 set->crtc->fb = set->fb;
817 set->crtc->enabled = (set->mode != NULL);
818 if (set->mode != NULL) {
819 DRM_DEBUG("attempting to set mode from userspace\n");
820 drm_mode_debug_printmodeline(set->mode);
821 if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
822 set->x, set->y,
823 old_fb)) {
824 DRM_ERROR("failed to set mode on crtc %p\n",
825 set->crtc);
826 ret = -EINVAL;
827 goto fail_set_mode;
829 /* TODO are these needed? */
830 set->crtc->desired_x = set->x;
831 set->crtc->desired_y = set->y;
832 set->crtc->desired_mode = set->mode;
834 drm_helper_disable_unused_functions(dev);
835 } else if (fb_changed) {
836 old_fb = set->crtc->fb;
837 if (set->crtc->fb != set->fb)
838 set->crtc->fb = set->fb;
839 ret = crtc_funcs->mode_set_base(set->crtc,
840 set->x, set->y, old_fb);
841 if (ret != 0)
842 goto fail_set_mode;
845 kfree(save_encoders);
846 kfree(save_crtcs);
847 return 0;
849 fail_set_mode:
850 set->crtc->enabled = save_enabled;
851 set->crtc->fb = old_fb;
852 count = 0;
853 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
854 if (!connector->encoder)
855 continue;
857 connector->encoder->crtc = save_crtcs[count++];
859 fail_no_encoder:
860 kfree(save_crtcs);
861 count = 0;
862 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
863 connector->encoder = save_encoders[count++];
865 kfree(save_encoders);
866 return ret;
868 EXPORT_SYMBOL(drm_crtc_helper_set_config);
870 bool drm_helper_plugged_event(struct drm_device *dev)
872 DRM_DEBUG("\n");
874 drm_helper_probe_connector_modes(dev, dev->mode_config.max_width,
875 dev->mode_config.max_height);
877 drm_setup_crtcs(dev);
879 /* alert the driver fb layer */
880 dev->mode_config.funcs->fb_changed(dev);
882 /* FIXME: send hotplug event */
883 return true;
886 * drm_initial_config - setup a sane initial connector configuration
887 * @dev: DRM device
888 * @can_grow: this configuration is growable
890 * LOCKING:
891 * Called at init time, must take mode config lock.
893 * Scan the CRTCs and connectors and try to put together an initial setup.
894 * At the moment, this is a cloned configuration across all heads with
895 * a new framebuffer object as the backing store.
897 * RETURNS:
898 * Zero if everything went ok, nonzero otherwise.
900 bool drm_helper_initial_config(struct drm_device *dev, bool can_grow)
902 struct drm_connector *connector;
903 int count = 0;
905 count = drm_helper_probe_connector_modes(dev,
906 dev->mode_config.max_width,
907 dev->mode_config.max_height);
910 * None of the available connectors had any modes, so add some
911 * and try to light them up anyway
913 if (!count) {
914 DRM_ERROR("connectors have no modes, using standard modes\n");
915 list_for_each_entry(connector,
916 &dev->mode_config.connector_list,
917 head)
918 drm_helper_add_std_modes(dev, connector);
921 drm_setup_crtcs(dev);
923 /* alert the driver fb layer */
924 dev->mode_config.funcs->fb_changed(dev);
926 return 0;
928 EXPORT_SYMBOL(drm_helper_initial_config);
931 * drm_hotplug_stage_two
932 * @dev DRM device
933 * @connector hotpluged connector
935 * LOCKING.
936 * Caller must hold mode config lock, function might grab struct lock.
938 * Stage two of a hotplug.
940 * RETURNS:
941 * Zero on success, errno on failure.
943 int drm_helper_hotplug_stage_two(struct drm_device *dev)
945 drm_helper_plugged_event(dev);
947 return 0;
949 EXPORT_SYMBOL(drm_helper_hotplug_stage_two);
951 int drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb,
952 struct drm_mode_fb_cmd *mode_cmd)
954 fb->width = mode_cmd->width;
955 fb->height = mode_cmd->height;
956 fb->pitch = mode_cmd->pitch;
957 fb->bits_per_pixel = mode_cmd->bpp;
958 fb->depth = mode_cmd->depth;
960 return 0;
962 EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
964 int drm_helper_resume_force_mode(struct drm_device *dev)
966 struct drm_crtc *crtc;
967 int ret;
969 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
971 if (!crtc->enabled)
972 continue;
974 ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
975 crtc->x, crtc->y, crtc->fb);
977 if (ret == false)
978 DRM_ERROR("failed to set mode on crtc %p\n", crtc);
980 return 0;
982 EXPORT_SYMBOL(drm_helper_resume_force_mode);