blob: e490e69db21e053e5b9f13a117d3591361873ca8 [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
32#include "drmP.h"
33#include "drm_crtc.h"
34#include "drm_crtc_helper.h"
35
36/*
Dave Airlieaa91c662008-12-08 14:55:27 +100037 * Detailed mode info for 800x600@60Hz
Dave Airlief453ba02008-11-07 14:05:41 -080038 */
Jesse Barnes40a518d2009-01-12 12:05:32 -080039static struct drm_display_mode std_modes[] = {
Dave Airlieaa91c662008-12-08 14:55:27 +100040 { 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) },
Dave Airlief453ba02008-11-07 14:05:41 -080043};
44
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
50 *
51 * LOCKING:
52 * Caller must hold mode config lock.
53 *
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.
58 *
59 * Intended to be used either at bootup time or when major configuration
60 * changes have occurred.
61 *
62 * FIXME: take into account monitor limits
Jesse Barnes40a518d2009-01-12 12:05:32 -080063 *
64 * RETURNS:
65 * Number of modes found on @connector.
Dave Airlief453ba02008-11-07 14:05:41 -080066 */
Jesse Barnes40a518d2009-01-12 12:05:32 -080067int drm_helper_probe_single_connector_modes(struct drm_connector *connector,
68 uint32_t maxX, uint32_t maxY)
Dave Airlief453ba02008-11-07 14:05:41 -080069{
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;
Jesse Barnes40a518d2009-01-12 12:05:32 -080074 int count = 0;
Dave Airlief453ba02008-11-07 14:05:41 -080075
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;
80
81 connector->status = connector->funcs->detect(connector);
82
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 */
Jesse Barnes40a518d2009-01-12 12:05:32 -080087 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -080088 }
89
Jesse Barnes40a518d2009-01-12 12:05:32 -080090 count = (*connector_funcs->get_modes)(connector);
91 if (!count)
92 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -080093
Jesse Barnes40a518d2009-01-12 12:05:32 -080094 drm_mode_connector_list_update(connector);
Dave Airlief453ba02008-11-07 14:05:41 -080095
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);
103 }
104
105
106 drm_mode_prune_invalid(dev, &connector->modes, true);
107
Jesse Barnes40a518d2009-01-12 12:05:32 -0800108 if (list_empty(&connector->modes))
109 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800110
111 drm_mode_sort(&connector->modes);
112
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);
116
117 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
118 drm_mode_debug_printmodeline(mode);
119 }
Jesse Barnes40a518d2009-01-12 12:05:32 -0800120
121 return count;
Dave Airlief453ba02008-11-07 14:05:41 -0800122}
123EXPORT_SYMBOL(drm_helper_probe_single_connector_modes);
124
Jesse Barnes40a518d2009-01-12 12:05:32 -0800125int drm_helper_probe_connector_modes(struct drm_device *dev, uint32_t maxX,
Dave Airlief453ba02008-11-07 14:05:41 -0800126 uint32_t maxY)
127{
128 struct drm_connector *connector;
Jesse Barnes40a518d2009-01-12 12:05:32 -0800129 int count = 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800130
131 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Jesse Barnes40a518d2009-01-12 12:05:32 -0800132 count += drm_helper_probe_single_connector_modes(connector,
133 maxX, maxY);
Dave Airlief453ba02008-11-07 14:05:41 -0800134 }
Jesse Barnes40a518d2009-01-12 12:05:32 -0800135
136 return count;
Dave Airlief453ba02008-11-07 14:05:41 -0800137}
138EXPORT_SYMBOL(drm_helper_probe_connector_modes);
139
Jesse Barnes40a518d2009-01-12 12:05:32 -0800140static void drm_helper_add_std_modes(struct drm_device *dev,
141 struct drm_connector *connector)
142{
143 struct drm_display_mode *mode, *t;
144 int i;
145
146 for (i = 0; i < ARRAY_SIZE(std_modes); i++) {
147 struct drm_display_mode *stdmode;
148
149 /*
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.
153 */
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);
158
159 DRM_DEBUG("Adding mode %s to %s\n", stdmode->name,
160 drm_get_connector_name(connector));
161 }
162 drm_mode_sort(&connector->modes);
163
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);
167
168 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
169 drm_mode_debug_printmodeline(mode);
170 }
171}
Dave Airlief453ba02008-11-07 14:05:41 -0800172
173/**
174 * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config
175 * @crtc: CRTC to check
176 *
177 * LOCKING:
178 * Caller must hold mode config lock.
179 *
180 * Walk @crtc's DRM device's mode_config and see if it's in use.
181 *
182 * RETURNS:
183 * True if @crtc is part of the mode_config, false otherwise.
184 */
185bool drm_helper_crtc_in_use(struct drm_crtc *crtc)
186{
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;
194}
195EXPORT_SYMBOL(drm_helper_crtc_in_use);
196
197/**
198 * drm_disable_unused_functions - disable unused objects
199 * @dev: DRM device
200 *
201 * LOCKING:
202 * Caller must hold mode config lock.
203 *
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.
206 */
207void drm_helper_disable_unused_functions(struct drm_device *dev)
208{
209 struct drm_encoder *encoder;
210 struct drm_encoder_helper_funcs *encoder_funcs;
211 struct drm_crtc *crtc;
212
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);
217 }
218
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;
225 }
226 }
227}
228EXPORT_SYMBOL(drm_helper_disable_unused_functions);
229
230static struct drm_display_mode *drm_has_preferred_mode(struct drm_connector *connector, int width, int height)
231{
232 struct drm_display_mode *mode;
233
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;
240 }
241 return NULL;
242}
243
244static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
245{
246 bool enable;
247
248 if (strict) {
249 enable = connector->status == connector_status_connected;
250 } else {
251 enable = connector->status != connector_status_disconnected;
252 }
253 return enable;
254}
255
256static void drm_enable_connectors(struct drm_device *dev, bool *enabled)
257{
258 bool any_enabled = false;
259 struct drm_connector *connector;
260 int i = 0;
261
262 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
263 enabled[i] = drm_connector_enabled(connector, true);
Jesse Barnes40a518d2009-01-12 12:05:32 -0800264 DRM_DEBUG("connector %d enabled? %s\n", connector->base.id,
265 enabled[i] ? "yes" : "no");
Dave Airlief453ba02008-11-07 14:05:41 -0800266 any_enabled |= enabled[i];
267 i++;
268 }
269
270 if (any_enabled)
271 return;
272
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++;
277 }
278}
279
280static bool drm_target_preferred(struct drm_device *dev,
281 struct drm_display_mode **modes,
282 bool *enabled, int width, int height)
283{
284 struct drm_connector *connector;
285 int i = 0;
286
287 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
288
289 if (enabled[i] == false) {
290 i++;
291 continue;
292 }
293
Jesse Barnes40a518d2009-01-12 12:05:32 -0800294 DRM_DEBUG("looking for preferred mode on connector %d\n",
295 connector->base.id);
296
Dave Airlief453ba02008-11-07 14:05:41 -0800297 modes[i] = drm_has_preferred_mode(connector, width, height);
Jesse Barnes40a518d2009-01-12 12:05:32 -0800298 /* No preferred modes, pick one off the list */
299 if (!modes[i] && !list_empty(&connector->modes)) {
Dave Airlief453ba02008-11-07 14:05:41 -0800300 list_for_each_entry(modes[i], &connector->modes, head)
301 break;
302 }
Jesse Barnes40a518d2009-01-12 12:05:32 -0800303 DRM_DEBUG("found mode %s\n", modes[i] ? modes[i]->name :
304 "none");
Dave Airlief453ba02008-11-07 14:05:41 -0800305 i++;
306 }
307 return true;
308}
309
310static 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)
314{
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;
322
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++;
330 }
331
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;
337
338 crtcs = kmalloc(dev->mode_config.num_connector *
339 sizeof(struct drm_crtc *), GFP_KERNEL);
340 if (!crtcs)
341 return best_score;
342
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++;
348
349 connector_funcs = connector->helper_private;
350 encoder = connector_funcs->best_encoder(connector);
351 if (!encoder)
352 goto out;
353
354 connector->encoder = encoder;
355
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) {
360
361 if ((connector->encoder->possible_crtcs & (1 << c)) == 0) {
362 c++;
363 continue;
364 }
365
366 for (o = 0; o < n; o++)
367 if (best_crtcs[o] == crtc)
368 break;
369
370 if (o < n) {
371 /* ignore cloning for now */
372 c++;
373 continue;
374 }
375
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 *));
386 }
387 c++;
388 }
389out:
390 kfree(crtcs);
391 return best_score;
392}
393
394static void drm_setup_crtcs(struct drm_device *dev)
395{
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;
403
Jesse Barnes40a518d2009-01-12 12:05:32 -0800404 DRM_DEBUG("\n");
405
Dave Airlief453ba02008-11-07 14:05:41 -0800406 width = dev->mode_config.max_width;
407 height = dev->mode_config.max_height;
408
409 /* clean out all the encoder/crtc combos */
410 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
411 encoder->crtc = NULL;
412 }
413
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);
420
421 drm_enable_connectors(dev, enabled);
422
423 ret = drm_target_preferred(dev, modes, enabled, width, height);
424 if (!ret)
425 DRM_ERROR("Unable to find initial modes\n");
426
Jesse Barnes40a518d2009-01-12 12:05:32 -0800427 DRM_DEBUG("picking CRTCs for %dx%d config\n", width, height);
428
Dave Airlief453ba02008-11-07 14:05:41 -0800429 drm_pick_crtcs(dev, crtcs, modes, 0, width, height);
430
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];
435
436 if (connector->encoder == NULL) {
437 i++;
438 continue;
439 }
440
441 if (mode && crtc) {
Jesse Barnes40a518d2009-01-12 12:05:32 -0800442 DRM_DEBUG("desired mode %s set on crtc %d\n",
443 mode->name, crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -0800444 crtc->desired_mode = mode;
445 connector->encoder->crtc = crtc;
446 } else
447 connector->encoder->crtc = NULL;
448 i++;
449 }
450
451 kfree(crtcs);
452 kfree(modes);
453 kfree(enabled);
454}
455/**
456 * drm_crtc_set_mode - set a mode
457 * @crtc: CRTC to program
458 * @mode: mode to use
459 * @x: width of mode
460 * @y: height of mode
461 *
462 * LOCKING:
463 * Caller must hold mode config lock.
464 *
465 * Try to set @mode on @crtc. Give @crtc and its associated connectors a chance
466 * to fixup or reject the mode prior to trying to set it.
467 *
468 * RETURNS:
469 * True if the mode was set successfully, or false otherwise.
470 */
471bool drm_crtc_helper_set_mode(struct drm_crtc *crtc,
472 struct drm_display_mode *mode,
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -0500473 int x, int y,
474 struct drm_framebuffer *old_fb)
Dave Airlief453ba02008-11-07 14:05:41 -0800475{
476 struct drm_device *dev = crtc->dev;
477 struct drm_display_mode *adjusted_mode, saved_mode;
478 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
479 struct drm_encoder_helper_funcs *encoder_funcs;
480 int saved_x, saved_y;
481 struct drm_encoder *encoder;
482 bool ret = true;
483
484 adjusted_mode = drm_mode_duplicate(dev, mode);
485
486 crtc->enabled = drm_helper_crtc_in_use(crtc);
487
488 if (!crtc->enabled)
489 return true;
490
491 saved_mode = crtc->mode;
492 saved_x = crtc->x;
493 saved_y = crtc->y;
494
495 /* Update crtc values up front so the driver can rely on them for mode
496 * setting.
497 */
498 crtc->mode = *mode;
499 crtc->x = x;
500 crtc->y = y;
501
502 if (drm_mode_equal(&saved_mode, &crtc->mode)) {
503 if (saved_x != crtc->x || saved_y != crtc->y) {
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -0500504 crtc_funcs->mode_set_base(crtc, crtc->x, crtc->y,
505 old_fb);
Dave Airlief453ba02008-11-07 14:05:41 -0800506 goto done;
507 }
508 }
509
510 /* Pass our mode to the connectors and the CRTC to give them a chance to
511 * adjust it according to limitations or connector properties, and also
512 * a chance to reject the mode entirely.
513 */
514 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
515
516 if (encoder->crtc != crtc)
517 continue;
518 encoder_funcs = encoder->helper_private;
519 if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
520 adjusted_mode))) {
521 goto done;
522 }
523 }
524
525 if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) {
526 goto done;
527 }
528
529 /* Prepare the encoders and CRTCs before setting the mode. */
530 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
531
532 if (encoder->crtc != crtc)
533 continue;
534 encoder_funcs = encoder->helper_private;
535 /* Disable the encoders as the first thing we do. */
536 encoder_funcs->prepare(encoder);
537 }
538
539 crtc_funcs->prepare(crtc);
540
541 /* Set up the DPLL and any encoders state that needs to adjust or depend
542 * on the DPLL.
543 */
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -0500544 crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
Dave Airlief453ba02008-11-07 14:05:41 -0800545
546 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
547
548 if (encoder->crtc != crtc)
549 continue;
550
551 DRM_INFO("%s: set mode %s %x\n", drm_get_encoder_name(encoder),
552 mode->name, mode->base.id);
553 encoder_funcs = encoder->helper_private;
554 encoder_funcs->mode_set(encoder, mode, adjusted_mode);
555 }
556
557 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
558 crtc_funcs->commit(crtc);
559
560 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
561
562 if (encoder->crtc != crtc)
563 continue;
564
565 encoder_funcs = encoder->helper_private;
566 encoder_funcs->commit(encoder);
567
568 }
569
570 /* XXX free adjustedmode */
571 drm_mode_destroy(dev, adjusted_mode);
572 /* FIXME: add subpixel order */
573done:
574 if (!ret) {
575 crtc->mode = saved_mode;
576 crtc->x = saved_x;
577 crtc->y = saved_y;
578 }
579
580 return ret;
581}
582EXPORT_SYMBOL(drm_crtc_helper_set_mode);
583
584
585/**
586 * drm_crtc_helper_set_config - set a new config from userspace
587 * @crtc: CRTC to setup
588 * @crtc_info: user provided configuration
589 * @new_mode: new mode to set
590 * @connector_set: set of connectors for the new config
591 * @fb: new framebuffer
592 *
593 * LOCKING:
594 * Caller must hold mode config lock.
595 *
596 * Setup a new configuration, provided by the user in @crtc_info, and enable
597 * it.
598 *
599 * RETURNS:
600 * Zero. (FIXME)
601 */
602int drm_crtc_helper_set_config(struct drm_mode_set *set)
603{
604 struct drm_device *dev;
605 struct drm_crtc **save_crtcs, *new_crtc;
606 struct drm_encoder **save_encoders, *new_encoder;
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -0500607 struct drm_framebuffer *old_fb;
Dave Airlief453ba02008-11-07 14:05:41 -0800608 bool save_enabled;
609 bool changed = false;
610 bool flip_or_move = false;
611 struct drm_connector *connector;
612 int count = 0, ro, fail = 0;
613 struct drm_crtc_helper_funcs *crtc_funcs;
614 int ret = 0;
615
616 DRM_DEBUG("\n");
617
618 if (!set)
619 return -EINVAL;
620
621 if (!set->crtc)
622 return -EINVAL;
623
624 if (!set->crtc->helper_private)
625 return -EINVAL;
626
627 crtc_funcs = set->crtc->helper_private;
628
629 DRM_DEBUG("crtc: %p %d fb: %p connectors: %p num_connectors: %d (x, y) (%i, %i)\n",
630 set->crtc, set->crtc->base.id, set->fb, set->connectors,
631 (int)set->num_connectors, set->x, set->y);
632
633 dev = set->crtc->dev;
634
635 /* save previous config */
636 save_enabled = set->crtc->enabled;
637
638 /* this is meant to be num_connector not num_crtc */
639 save_crtcs = kzalloc(dev->mode_config.num_connector *
640 sizeof(struct drm_crtc *), GFP_KERNEL);
641 if (!save_crtcs)
642 return -ENOMEM;
643
644 save_encoders = kzalloc(dev->mode_config.num_connector *
645 sizeof(struct drm_encoders *), GFP_KERNEL);
646 if (!save_encoders) {
647 kfree(save_crtcs);
648 return -ENOMEM;
649 }
650
651 /* We should be able to check here if the fb has the same properties
652 * and then just flip_or_move it */
653 if (set->crtc->fb != set->fb) {
654 /* if we have no fb then its a change not a flip */
655 if (set->crtc->fb == NULL)
656 changed = true;
657 else
658 flip_or_move = true;
659 }
660
661 if (set->x != set->crtc->x || set->y != set->crtc->y)
662 flip_or_move = true;
663
664 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
665 DRM_DEBUG("modes are different\n");
666 drm_mode_debug_printmodeline(&set->crtc->mode);
667 drm_mode_debug_printmodeline(set->mode);
668 changed = true;
669 }
670
671 /* a) traverse passed in connector list and get encoders for them */
672 count = 0;
673 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
674 struct drm_connector_helper_funcs *connector_funcs =
675 connector->helper_private;
676 save_encoders[count++] = connector->encoder;
677 new_encoder = connector->encoder;
678 for (ro = 0; ro < set->num_connectors; ro++) {
679 if (set->connectors[ro] == connector) {
680 new_encoder = connector_funcs->best_encoder(connector);
681 /* if we can't get an encoder for a connector
682 we are setting now - then fail */
683 if (new_encoder == NULL)
684 /* don't break so fail path works correct */
685 fail = 1;
686 break;
687 }
688 }
689
690 if (new_encoder != connector->encoder) {
691 changed = true;
692 connector->encoder = new_encoder;
693 }
694 }
695
696 if (fail) {
697 ret = -EINVAL;
698 goto fail_no_encoder;
699 }
700
701 count = 0;
702 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
703 if (!connector->encoder)
704 continue;
705
706 save_crtcs[count++] = connector->encoder->crtc;
707
708 if (connector->encoder->crtc == set->crtc)
709 new_crtc = NULL;
710 else
711 new_crtc = connector->encoder->crtc;
712
713 for (ro = 0; ro < set->num_connectors; ro++) {
714 if (set->connectors[ro] == connector)
715 new_crtc = set->crtc;
716 }
717 if (new_crtc != connector->encoder->crtc) {
718 changed = true;
719 connector->encoder->crtc = new_crtc;
720 }
721 }
722
723 /* mode_set_base is not a required function */
724 if (flip_or_move && !crtc_funcs->mode_set_base)
725 changed = true;
726
727 if (changed) {
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -0500728 old_fb = set->crtc->fb;
Dave Airlief453ba02008-11-07 14:05:41 -0800729 set->crtc->fb = set->fb;
730 set->crtc->enabled = (set->mode != NULL);
731 if (set->mode != NULL) {
732 DRM_DEBUG("attempting to set mode from userspace\n");
733 drm_mode_debug_printmodeline(set->mode);
734 if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -0500735 set->x, set->y,
736 old_fb)) {
Dave Airlief453ba02008-11-07 14:05:41 -0800737 ret = -EINVAL;
738 goto fail_set_mode;
739 }
740 /* TODO are these needed? */
741 set->crtc->desired_x = set->x;
742 set->crtc->desired_y = set->y;
743 set->crtc->desired_mode = set->mode;
744 }
745 drm_helper_disable_unused_functions(dev);
746 } else if (flip_or_move) {
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -0500747 old_fb = set->crtc->fb;
Dave Airlief453ba02008-11-07 14:05:41 -0800748 if (set->crtc->fb != set->fb)
749 set->crtc->fb = set->fb;
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -0500750 crtc_funcs->mode_set_base(set->crtc, set->x, set->y, old_fb);
Dave Airlief453ba02008-11-07 14:05:41 -0800751 }
752
753 kfree(save_encoders);
754 kfree(save_crtcs);
755 return 0;
756
757fail_set_mode:
758 set->crtc->enabled = save_enabled;
759 count = 0;
760 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
761 connector->encoder->crtc = save_crtcs[count++];
762fail_no_encoder:
763 kfree(save_crtcs);
764 count = 0;
765 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
766 connector->encoder = save_encoders[count++];
767 }
768 kfree(save_encoders);
769 return ret;
770}
771EXPORT_SYMBOL(drm_crtc_helper_set_config);
772
773bool drm_helper_plugged_event(struct drm_device *dev)
774{
775 DRM_DEBUG("\n");
776
777 drm_helper_probe_connector_modes(dev, dev->mode_config.max_width,
778 dev->mode_config.max_height);
779
780 drm_setup_crtcs(dev);
781
782 /* alert the driver fb layer */
783 dev->mode_config.funcs->fb_changed(dev);
784
785 /* FIXME: send hotplug event */
786 return true;
787}
788/**
789 * drm_initial_config - setup a sane initial connector configuration
790 * @dev: DRM device
791 * @can_grow: this configuration is growable
792 *
793 * LOCKING:
794 * Called at init time, must take mode config lock.
795 *
796 * Scan the CRTCs and connectors and try to put together an initial setup.
797 * At the moment, this is a cloned configuration across all heads with
798 * a new framebuffer object as the backing store.
799 *
800 * RETURNS:
801 * Zero if everything went ok, nonzero otherwise.
802 */
803bool drm_helper_initial_config(struct drm_device *dev, bool can_grow)
804{
Jesse Barnes40a518d2009-01-12 12:05:32 -0800805 struct drm_connector *connector;
806 int count = 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800807
Jesse Barnes40a518d2009-01-12 12:05:32 -0800808 count = drm_helper_probe_connector_modes(dev,
809 dev->mode_config.max_width,
810 dev->mode_config.max_height);
811
812 /*
813 * None of the available connectors had any modes, so add some
814 * and try to light them up anyway
815 */
816 if (!count) {
817 DRM_ERROR("connectors have no modes, using standard modes\n");
818 list_for_each_entry(connector,
819 &dev->mode_config.connector_list,
820 head)
821 drm_helper_add_std_modes(dev, connector);
822 }
823
824 drm_setup_crtcs(dev);
825
826 /* alert the driver fb layer */
827 dev->mode_config.funcs->fb_changed(dev);
828
829 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800830}
831EXPORT_SYMBOL(drm_helper_initial_config);
832
833/**
834 * drm_hotplug_stage_two
835 * @dev DRM device
836 * @connector hotpluged connector
837 *
838 * LOCKING.
839 * Caller must hold mode config lock, function might grab struct lock.
840 *
841 * Stage two of a hotplug.
842 *
843 * RETURNS:
844 * Zero on success, errno on failure.
845 */
846int drm_helper_hotplug_stage_two(struct drm_device *dev)
847{
Dave Airlief453ba02008-11-07 14:05:41 -0800848 drm_helper_plugged_event(dev);
849
850 return 0;
851}
852EXPORT_SYMBOL(drm_helper_hotplug_stage_two);
853
854int drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb,
855 struct drm_mode_fb_cmd *mode_cmd)
856{
857 fb->width = mode_cmd->width;
858 fb->height = mode_cmd->height;
859 fb->pitch = mode_cmd->pitch;
860 fb->bits_per_pixel = mode_cmd->bpp;
861 fb->depth = mode_cmd->depth;
862
863 return 0;
864}
865EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
866
867int drm_helper_resume_force_mode(struct drm_device *dev)
868{
869 struct drm_crtc *crtc;
870 int ret;
871
872 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
873
874 if (!crtc->enabled)
875 continue;
876
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -0500877 ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
878 crtc->x, crtc->y, crtc->fb);
Dave Airlief453ba02008-11-07 14:05:41 -0800879
880 if (ret == false)
881 DRM_ERROR("failed to set mode on crtc %p\n", crtc);
882 }
883 return 0;
884}
885EXPORT_SYMBOL(drm_helper_resume_force_mode);