blob: 6f4b4446fcdf3923e7fa4c736998cbd1b1cc25ec [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
yakui_zhao67149772009-04-02 11:52:12 +080045static void drm_mode_validate_flag(struct drm_connector *connector,
46 int flags)
47{
48 struct drm_display_mode *mode, *t;
49
50 if (flags == (DRM_MODE_FLAG_DBLSCAN | DRM_MODE_FLAG_INTERLACE))
51 return;
52
53 list_for_each_entry_safe(mode, t, &connector->modes, head) {
54 if ((mode->flags & DRM_MODE_FLAG_INTERLACE) &&
55 !(flags & DRM_MODE_FLAG_INTERLACE))
56 mode->status = MODE_NO_INTERLACE;
57 if ((mode->flags & DRM_MODE_FLAG_DBLSCAN) &&
58 !(flags & DRM_MODE_FLAG_DBLSCAN))
59 mode->status = MODE_NO_DBLESCAN;
60 }
61
62 return;
63}
64
Dave Airlief453ba02008-11-07 14:05:41 -080065/**
66 * drm_helper_probe_connector_modes - get complete set of display modes
67 * @dev: DRM device
68 * @maxX: max width for modes
69 * @maxY: max height for modes
70 *
71 * LOCKING:
72 * Caller must hold mode config lock.
73 *
74 * Based on @dev's mode_config layout, scan all the connectors and try to detect
75 * modes on them. Modes will first be added to the connector's probed_modes
76 * list, then culled (based on validity and the @maxX, @maxY parameters) and
77 * put into the normal modes list.
78 *
79 * Intended to be used either at bootup time or when major configuration
80 * changes have occurred.
81 *
82 * FIXME: take into account monitor limits
Jesse Barnes40a518d2009-01-12 12:05:32 -080083 *
84 * RETURNS:
85 * Number of modes found on @connector.
Dave Airlief453ba02008-11-07 14:05:41 -080086 */
Jesse Barnes40a518d2009-01-12 12:05:32 -080087int drm_helper_probe_single_connector_modes(struct drm_connector *connector,
88 uint32_t maxX, uint32_t maxY)
Dave Airlief453ba02008-11-07 14:05:41 -080089{
90 struct drm_device *dev = connector->dev;
91 struct drm_display_mode *mode, *t;
92 struct drm_connector_helper_funcs *connector_funcs =
93 connector->helper_private;
Jesse Barnes40a518d2009-01-12 12:05:32 -080094 int count = 0;
yakui_zhao67149772009-04-02 11:52:12 +080095 int mode_flags = 0;
Dave Airlief453ba02008-11-07 14:05:41 -080096
Zhao Yakui58367ed2009-07-20 13:48:07 +080097 DRM_DEBUG_KMS("%s\n", 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
102 connector->status = connector->funcs->detect(connector);
103
104 if (connector->status == connector_status_disconnected) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800105 DRM_DEBUG_KMS("%s is disconnected\n",
Dave Airlief453ba02008-11-07 14:05:41 -0800106 drm_get_connector_name(connector));
107 /* TODO set EDID to NULL */
Jesse Barnes40a518d2009-01-12 12:05:32 -0800108 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800109 }
110
Jesse Barnes40a518d2009-01-12 12:05:32 -0800111 count = (*connector_funcs->get_modes)(connector);
ykzhao50fe4cf2009-09-03 14:30:04 +0800112 if (!count) {
113 count = drm_add_modes_noedid(connector, 800, 600);
114 if (!count)
115 return 0;
116 }
Dave Airlief453ba02008-11-07 14:05:41 -0800117
Jesse Barnes40a518d2009-01-12 12:05:32 -0800118 drm_mode_connector_list_update(connector);
Dave Airlief453ba02008-11-07 14:05:41 -0800119
120 if (maxX && maxY)
121 drm_mode_validate_size(dev, &connector->modes, maxX,
122 maxY, 0);
yakui_zhao67149772009-04-02 11:52:12 +0800123
124 if (connector->interlace_allowed)
125 mode_flags |= DRM_MODE_FLAG_INTERLACE;
126 if (connector->doublescan_allowed)
127 mode_flags |= DRM_MODE_FLAG_DBLSCAN;
128 drm_mode_validate_flag(connector, mode_flags);
129
Dave Airlief453ba02008-11-07 14:05:41 -0800130 list_for_each_entry_safe(mode, t, &connector->modes, head) {
131 if (mode->status == MODE_OK)
132 mode->status = connector_funcs->mode_valid(connector,
133 mode);
134 }
135
136
137 drm_mode_prune_invalid(dev, &connector->modes, true);
138
Jesse Barnes40a518d2009-01-12 12:05:32 -0800139 if (list_empty(&connector->modes))
140 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800141
142 drm_mode_sort(&connector->modes);
143
Zhao Yakui58367ed2009-07-20 13:48:07 +0800144 DRM_DEBUG_KMS("Probed modes for %s\n",
145 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -0800146 list_for_each_entry_safe(mode, t, &connector->modes, head) {
147 mode->vrefresh = drm_mode_vrefresh(mode);
148
149 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
150 drm_mode_debug_printmodeline(mode);
151 }
Jesse Barnes40a518d2009-01-12 12:05:32 -0800152
153 return count;
Dave Airlief453ba02008-11-07 14:05:41 -0800154}
155EXPORT_SYMBOL(drm_helper_probe_single_connector_modes);
156
Jesse Barnes40a518d2009-01-12 12:05:32 -0800157int drm_helper_probe_connector_modes(struct drm_device *dev, uint32_t maxX,
Dave Airlief453ba02008-11-07 14:05:41 -0800158 uint32_t maxY)
159{
160 struct drm_connector *connector;
Jesse Barnes40a518d2009-01-12 12:05:32 -0800161 int count = 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800162
163 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Jesse Barnes40a518d2009-01-12 12:05:32 -0800164 count += drm_helper_probe_single_connector_modes(connector,
165 maxX, maxY);
Dave Airlief453ba02008-11-07 14:05:41 -0800166 }
Jesse Barnes40a518d2009-01-12 12:05:32 -0800167
168 return count;
Dave Airlief453ba02008-11-07 14:05:41 -0800169}
170EXPORT_SYMBOL(drm_helper_probe_connector_modes);
171
Jesse Barnes40a518d2009-01-12 12:05:32 -0800172static void drm_helper_add_std_modes(struct drm_device *dev,
173 struct drm_connector *connector)
174{
175 struct drm_display_mode *mode, *t;
176 int i;
177
178 for (i = 0; i < ARRAY_SIZE(std_modes); i++) {
179 struct drm_display_mode *stdmode;
180
181 /*
182 * When no valid EDID modes are available we end up
183 * here and bailed in the past, now we add some standard
184 * modes and move on.
185 */
186 stdmode = drm_mode_duplicate(dev, &std_modes[i]);
187 drm_mode_probed_add(connector, stdmode);
188 drm_mode_list_concat(&connector->probed_modes,
189 &connector->modes);
190
Zhao Yakui58367ed2009-07-20 13:48:07 +0800191 DRM_DEBUG_KMS("Adding mode %s to %s\n", stdmode->name,
Jesse Barnes40a518d2009-01-12 12:05:32 -0800192 drm_get_connector_name(connector));
193 }
194 drm_mode_sort(&connector->modes);
195
Zhao Yakui58367ed2009-07-20 13:48:07 +0800196 DRM_DEBUG_KMS("Added std modes on %s\n",
197 drm_get_connector_name(connector));
Jesse Barnes40a518d2009-01-12 12:05:32 -0800198 list_for_each_entry_safe(mode, t, &connector->modes, head) {
199 mode->vrefresh = drm_mode_vrefresh(mode);
200
201 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
202 drm_mode_debug_printmodeline(mode);
203 }
204}
Dave Airlief453ba02008-11-07 14:05:41 -0800205
206/**
Keith Packardc9fb15f2009-05-30 20:42:28 -0700207 * drm_helper_encoder_in_use - check if a given encoder is in use
208 * @encoder: encoder to check
209 *
210 * LOCKING:
211 * Caller must hold mode config lock.
212 *
213 * Walk @encoders's DRM device's mode_config and see if it's in use.
214 *
215 * RETURNS:
216 * True if @encoder is part of the mode_config, false otherwise.
217 */
218bool drm_helper_encoder_in_use(struct drm_encoder *encoder)
219{
220 struct drm_connector *connector;
221 struct drm_device *dev = encoder->dev;
222 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
223 if (connector->encoder == encoder)
224 return true;
225 return false;
226}
227EXPORT_SYMBOL(drm_helper_encoder_in_use);
228
229/**
Dave Airlief453ba02008-11-07 14:05:41 -0800230 * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config
231 * @crtc: CRTC to check
232 *
233 * LOCKING:
234 * Caller must hold mode config lock.
235 *
236 * Walk @crtc's DRM device's mode_config and see if it's in use.
237 *
238 * RETURNS:
239 * True if @crtc is part of the mode_config, false otherwise.
240 */
241bool drm_helper_crtc_in_use(struct drm_crtc *crtc)
242{
243 struct drm_encoder *encoder;
244 struct drm_device *dev = crtc->dev;
245 /* FIXME: Locking around list access? */
246 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
Keith Packardc9fb15f2009-05-30 20:42:28 -0700247 if (encoder->crtc == crtc && drm_helper_encoder_in_use(encoder))
Dave Airlief453ba02008-11-07 14:05:41 -0800248 return true;
249 return false;
250}
251EXPORT_SYMBOL(drm_helper_crtc_in_use);
252
253/**
254 * drm_disable_unused_functions - disable unused objects
255 * @dev: DRM device
256 *
257 * LOCKING:
258 * Caller must hold mode config lock.
259 *
260 * If an connector or CRTC isn't part of @dev's mode_config, it can be disabled
261 * by calling its dpms function, which should power it off.
262 */
263void drm_helper_disable_unused_functions(struct drm_device *dev)
264{
265 struct drm_encoder *encoder;
Dave Airliea3a05442009-08-31 15:16:30 +1000266 struct drm_connector *connector;
Dave Airlief453ba02008-11-07 14:05:41 -0800267 struct drm_encoder_helper_funcs *encoder_funcs;
268 struct drm_crtc *crtc;
269
Dave Airliea3a05442009-08-31 15:16:30 +1000270 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
271 if (!connector->encoder)
272 continue;
273 if (connector->status == connector_status_disconnected)
274 connector->encoder = NULL;
275 }
276
Dave Airlief453ba02008-11-07 14:05:41 -0800277 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
278 encoder_funcs = encoder->helper_private;
Dave Airliea3a05442009-08-31 15:16:30 +1000279 if (!drm_helper_encoder_in_use(encoder)) {
280 if (encoder_funcs->disable)
281 (*encoder_funcs->disable)(encoder);
282 else
283 (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
Dave Airlie9c552dd2009-09-02 14:00:11 +1000284 /* disconnector encoder from any connector */
285 encoder->crtc = NULL;
Dave Airliea3a05442009-08-31 15:16:30 +1000286 }
Dave Airlief453ba02008-11-07 14:05:41 -0800287 }
288
289 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
290 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
291 crtc->enabled = drm_helper_crtc_in_use(crtc);
292 if (!crtc->enabled) {
293 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
294 crtc->fb = NULL;
295 }
296 }
297}
298EXPORT_SYMBOL(drm_helper_disable_unused_functions);
299
300static struct drm_display_mode *drm_has_preferred_mode(struct drm_connector *connector, int width, int height)
301{
302 struct drm_display_mode *mode;
303
304 list_for_each_entry(mode, &connector->modes, head) {
305 if (drm_mode_width(mode) > width ||
306 drm_mode_height(mode) > height)
307 continue;
308 if (mode->type & DRM_MODE_TYPE_PREFERRED)
309 return mode;
310 }
311 return NULL;
312}
313
314static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
315{
316 bool enable;
317
318 if (strict) {
319 enable = connector->status == connector_status_connected;
320 } else {
321 enable = connector->status != connector_status_disconnected;
322 }
323 return enable;
324}
325
326static void drm_enable_connectors(struct drm_device *dev, bool *enabled)
327{
328 bool any_enabled = false;
329 struct drm_connector *connector;
330 int i = 0;
331
332 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
333 enabled[i] = drm_connector_enabled(connector, true);
Zhao Yakui58367ed2009-07-20 13:48:07 +0800334 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
Jesse Barnes40a518d2009-01-12 12:05:32 -0800335 enabled[i] ? "yes" : "no");
Dave Airlief453ba02008-11-07 14:05:41 -0800336 any_enabled |= enabled[i];
337 i++;
338 }
339
340 if (any_enabled)
341 return;
342
343 i = 0;
344 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
345 enabled[i] = drm_connector_enabled(connector, false);
346 i++;
347 }
348}
349
350static bool drm_target_preferred(struct drm_device *dev,
351 struct drm_display_mode **modes,
352 bool *enabled, int width, int height)
353{
354 struct drm_connector *connector;
355 int i = 0;
356
357 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
358
359 if (enabled[i] == false) {
360 i++;
361 continue;
362 }
363
Zhao Yakui58367ed2009-07-20 13:48:07 +0800364 DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
Jesse Barnes40a518d2009-01-12 12:05:32 -0800365 connector->base.id);
366
Dave Airlief453ba02008-11-07 14:05:41 -0800367 modes[i] = drm_has_preferred_mode(connector, width, height);
Jesse Barnes40a518d2009-01-12 12:05:32 -0800368 /* No preferred modes, pick one off the list */
369 if (!modes[i] && !list_empty(&connector->modes)) {
Dave Airlief453ba02008-11-07 14:05:41 -0800370 list_for_each_entry(modes[i], &connector->modes, head)
371 break;
372 }
Zhao Yakui58367ed2009-07-20 13:48:07 +0800373 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
Jesse Barnes40a518d2009-01-12 12:05:32 -0800374 "none");
Dave Airlief453ba02008-11-07 14:05:41 -0800375 i++;
376 }
377 return true;
378}
379
380static int drm_pick_crtcs(struct drm_device *dev,
381 struct drm_crtc **best_crtcs,
382 struct drm_display_mode **modes,
383 int n, int width, int height)
384{
385 int c, o;
386 struct drm_connector *connector;
387 struct drm_connector_helper_funcs *connector_funcs;
388 struct drm_encoder *encoder;
389 struct drm_crtc *best_crtc;
390 int my_score, best_score, score;
391 struct drm_crtc **crtcs, *crtc;
392
393 if (n == dev->mode_config.num_connector)
394 return 0;
395 c = 0;
396 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
397 if (c == n)
398 break;
399 c++;
400 }
401
402 best_crtcs[n] = NULL;
403 best_crtc = NULL;
404 best_score = drm_pick_crtcs(dev, best_crtcs, modes, n+1, width, height);
405 if (modes[n] == NULL)
406 return best_score;
407
408 crtcs = kmalloc(dev->mode_config.num_connector *
409 sizeof(struct drm_crtc *), GFP_KERNEL);
410 if (!crtcs)
411 return best_score;
412
413 my_score = 1;
414 if (connector->status == connector_status_connected)
415 my_score++;
416 if (drm_has_preferred_mode(connector, width, height))
417 my_score++;
418
419 connector_funcs = connector->helper_private;
420 encoder = connector_funcs->best_encoder(connector);
421 if (!encoder)
422 goto out;
423
424 connector->encoder = encoder;
425
426 /* select a crtc for this connector and then attempt to configure
427 remaining connectors */
428 c = 0;
429 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
430
Dave Airliea3a05442009-08-31 15:16:30 +1000431 if ((encoder->possible_crtcs & (1 << c)) == 0) {
Dave Airlief453ba02008-11-07 14:05:41 -0800432 c++;
433 continue;
434 }
435
436 for (o = 0; o < n; o++)
437 if (best_crtcs[o] == crtc)
438 break;
439
440 if (o < n) {
441 /* ignore cloning for now */
442 c++;
443 continue;
444 }
445
446 crtcs[n] = crtc;
447 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_crtc *));
448 score = my_score + drm_pick_crtcs(dev, crtcs, modes, n + 1,
449 width, height);
450 if (score > best_score) {
451 best_crtc = crtc;
452 best_score = score;
453 memcpy(best_crtcs, crtcs,
454 dev->mode_config.num_connector *
455 sizeof(struct drm_crtc *));
456 }
457 c++;
458 }
459out:
460 kfree(crtcs);
461 return best_score;
462}
463
464static void drm_setup_crtcs(struct drm_device *dev)
465{
466 struct drm_crtc **crtcs;
467 struct drm_display_mode **modes;
468 struct drm_encoder *encoder;
469 struct drm_connector *connector;
470 bool *enabled;
471 int width, height;
472 int i, ret;
473
Zhao Yakui58367ed2009-07-20 13:48:07 +0800474 DRM_DEBUG_KMS("\n");
Jesse Barnes40a518d2009-01-12 12:05:32 -0800475
Dave Airlief453ba02008-11-07 14:05:41 -0800476 width = dev->mode_config.max_width;
477 height = dev->mode_config.max_height;
478
479 /* clean out all the encoder/crtc combos */
480 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
481 encoder->crtc = NULL;
482 }
483
484 crtcs = kcalloc(dev->mode_config.num_connector,
485 sizeof(struct drm_crtc *), GFP_KERNEL);
486 modes = kcalloc(dev->mode_config.num_connector,
487 sizeof(struct drm_display_mode *), GFP_KERNEL);
488 enabled = kcalloc(dev->mode_config.num_connector,
489 sizeof(bool), GFP_KERNEL);
490
491 drm_enable_connectors(dev, enabled);
492
493 ret = drm_target_preferred(dev, modes, enabled, width, height);
494 if (!ret)
495 DRM_ERROR("Unable to find initial modes\n");
496
Zhao Yakui58367ed2009-07-20 13:48:07 +0800497 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
Jesse Barnes40a518d2009-01-12 12:05:32 -0800498
Dave Airlief453ba02008-11-07 14:05:41 -0800499 drm_pick_crtcs(dev, crtcs, modes, 0, width, height);
500
501 i = 0;
502 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
503 struct drm_display_mode *mode = modes[i];
504 struct drm_crtc *crtc = crtcs[i];
505
506 if (connector->encoder == NULL) {
507 i++;
508 continue;
509 }
510
511 if (mode && crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800512 DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
Jesse Barnes40a518d2009-01-12 12:05:32 -0800513 mode->name, crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -0800514 crtc->desired_mode = mode;
515 connector->encoder->crtc = crtc;
Dave Airliea3a05442009-08-31 15:16:30 +1000516 } else {
Dave Airlief453ba02008-11-07 14:05:41 -0800517 connector->encoder->crtc = NULL;
Dave Airliea3a05442009-08-31 15:16:30 +1000518 connector->encoder = NULL;
519 }
Dave Airlief453ba02008-11-07 14:05:41 -0800520 i++;
521 }
522
523 kfree(crtcs);
524 kfree(modes);
525 kfree(enabled);
526}
Jesse Barnes7bec7562009-02-23 16:09:34 -0800527
528/**
529 * drm_encoder_crtc_ok - can a given crtc drive a given encoder?
530 * @encoder: encoder to test
531 * @crtc: crtc to test
532 *
533 * Return false if @encoder can't be driven by @crtc, true otherwise.
534 */
535static bool drm_encoder_crtc_ok(struct drm_encoder *encoder,
536 struct drm_crtc *crtc)
537{
538 struct drm_device *dev;
539 struct drm_crtc *tmp;
540 int crtc_mask = 1;
541
542 WARN(!crtc, "checking null crtc?");
543
544 dev = crtc->dev;
545
546 list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
547 if (tmp == crtc)
548 break;
549 crtc_mask <<= 1;
550 }
551
552 if (encoder->possible_crtcs & crtc_mask)
553 return true;
554 return false;
555}
556
557/*
558 * Check the CRTC we're going to map each output to vs. its current
559 * CRTC. If they don't match, we have to disable the output and the CRTC
560 * since the driver will have to re-route things.
561 */
562static void
563drm_crtc_prepare_encoders(struct drm_device *dev)
564{
565 struct drm_encoder_helper_funcs *encoder_funcs;
566 struct drm_encoder *encoder;
567
568 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
569 encoder_funcs = encoder->helper_private;
570 /* Disable unused encoders */
571 if (encoder->crtc == NULL)
572 (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
573 /* Disable encoders whose CRTC is about to change */
574 if (encoder_funcs->get_crtc &&
575 encoder->crtc != (*encoder_funcs->get_crtc)(encoder))
576 (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
577 }
578}
579
Dave Airlief453ba02008-11-07 14:05:41 -0800580/**
581 * drm_crtc_set_mode - set a mode
582 * @crtc: CRTC to program
583 * @mode: mode to use
584 * @x: width of mode
585 * @y: height of mode
586 *
587 * LOCKING:
588 * Caller must hold mode config lock.
589 *
590 * Try to set @mode on @crtc. Give @crtc and its associated connectors a chance
591 * to fixup or reject the mode prior to trying to set it.
592 *
593 * RETURNS:
594 * True if the mode was set successfully, or false otherwise.
595 */
596bool drm_crtc_helper_set_mode(struct drm_crtc *crtc,
597 struct drm_display_mode *mode,
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -0500598 int x, int y,
599 struct drm_framebuffer *old_fb)
Dave Airlief453ba02008-11-07 14:05:41 -0800600{
601 struct drm_device *dev = crtc->dev;
602 struct drm_display_mode *adjusted_mode, saved_mode;
603 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
604 struct drm_encoder_helper_funcs *encoder_funcs;
605 int saved_x, saved_y;
606 struct drm_encoder *encoder;
607 bool ret = true;
608
609 adjusted_mode = drm_mode_duplicate(dev, mode);
610
611 crtc->enabled = drm_helper_crtc_in_use(crtc);
612
613 if (!crtc->enabled)
614 return true;
615
616 saved_mode = crtc->mode;
617 saved_x = crtc->x;
618 saved_y = crtc->y;
619
620 /* Update crtc values up front so the driver can rely on them for mode
621 * setting.
622 */
623 crtc->mode = *mode;
624 crtc->x = x;
625 crtc->y = y;
626
Dave Airlief453ba02008-11-07 14:05:41 -0800627 /* Pass our mode to the connectors and the CRTC to give them a chance to
628 * adjust it according to limitations or connector properties, and also
629 * a chance to reject the mode entirely.
630 */
631 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
632
633 if (encoder->crtc != crtc)
634 continue;
635 encoder_funcs = encoder->helper_private;
636 if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
637 adjusted_mode))) {
638 goto done;
639 }
640 }
641
642 if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) {
643 goto done;
644 }
645
646 /* Prepare the encoders and CRTCs before setting the mode. */
647 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
648
649 if (encoder->crtc != crtc)
650 continue;
651 encoder_funcs = encoder->helper_private;
652 /* Disable the encoders as the first thing we do. */
653 encoder_funcs->prepare(encoder);
654 }
655
Jesse Barnes7bec7562009-02-23 16:09:34 -0800656 drm_crtc_prepare_encoders(dev);
657
Dave Airlief453ba02008-11-07 14:05:41 -0800658 crtc_funcs->prepare(crtc);
659
660 /* Set up the DPLL and any encoders state that needs to adjust or depend
661 * on the DPLL.
662 */
Chris Wilson5c3b82e2009-02-11 13:25:09 +0000663 ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
664 if (!ret)
665 goto done;
Dave Airlief453ba02008-11-07 14:05:41 -0800666
667 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
668
669 if (encoder->crtc != crtc)
670 continue;
671
672 DRM_INFO("%s: set mode %s %x\n", drm_get_encoder_name(encoder),
673 mode->name, mode->base.id);
674 encoder_funcs = encoder->helper_private;
675 encoder_funcs->mode_set(encoder, mode, adjusted_mode);
676 }
677
678 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
679 crtc_funcs->commit(crtc);
680
681 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
682
683 if (encoder->crtc != crtc)
684 continue;
685
686 encoder_funcs = encoder->helper_private;
687 encoder_funcs->commit(encoder);
688
689 }
690
691 /* XXX free adjustedmode */
692 drm_mode_destroy(dev, adjusted_mode);
693 /* FIXME: add subpixel order */
694done:
695 if (!ret) {
696 crtc->mode = saved_mode;
697 crtc->x = saved_x;
698 crtc->y = saved_y;
699 }
700
701 return ret;
702}
703EXPORT_SYMBOL(drm_crtc_helper_set_mode);
704
705
706/**
707 * drm_crtc_helper_set_config - set a new config from userspace
708 * @crtc: CRTC to setup
709 * @crtc_info: user provided configuration
710 * @new_mode: new mode to set
711 * @connector_set: set of connectors for the new config
712 * @fb: new framebuffer
713 *
714 * LOCKING:
715 * Caller must hold mode config lock.
716 *
717 * Setup a new configuration, provided by the user in @crtc_info, and enable
718 * it.
719 *
720 * RETURNS:
721 * Zero. (FIXME)
722 */
723int drm_crtc_helper_set_config(struct drm_mode_set *set)
724{
725 struct drm_device *dev;
Maarten Maathuise67aae72009-08-27 10:18:29 +0200726 struct drm_crtc *save_crtcs, *new_crtc, *crtc;
727 struct drm_encoder *save_encoders, *new_encoder, *encoder;
Jesse Barnes7bec7562009-02-23 16:09:34 -0800728 struct drm_framebuffer *old_fb = NULL;
Jakob Bornecrantz4cb72b12009-08-03 13:43:59 +0100729 bool mode_changed = false; /* if true do a full mode set */
730 bool fb_changed = false; /* if true and !mode_changed just do a flip */
Maarten Maathuise67aae72009-08-27 10:18:29 +0200731 struct drm_connector *save_connectors, *connector;
Dave Airlief453ba02008-11-07 14:05:41 -0800732 int count = 0, ro, fail = 0;
733 struct drm_crtc_helper_funcs *crtc_funcs;
734 int ret = 0;
735
Zhao Yakui58367ed2009-07-20 13:48:07 +0800736 DRM_DEBUG_KMS("\n");
Dave Airlief453ba02008-11-07 14:05:41 -0800737
738 if (!set)
739 return -EINVAL;
740
741 if (!set->crtc)
742 return -EINVAL;
743
744 if (!set->crtc->helper_private)
745 return -EINVAL;
746
747 crtc_funcs = set->crtc->helper_private;
748
Zhao Yakui58367ed2009-07-20 13:48:07 +0800749 DRM_DEBUG_KMS("crtc: %p %d fb: %p connectors: %p num_connectors:"
750 " %d (x, y) (%i, %i)\n",
Dave Airlief453ba02008-11-07 14:05:41 -0800751 set->crtc, set->crtc->base.id, set->fb, set->connectors,
752 (int)set->num_connectors, set->x, set->y);
753
754 dev = set->crtc->dev;
755
Maarten Maathuise67aae72009-08-27 10:18:29 +0200756 /* Allocate space for the backup of all (non-pointer) crtc, encoder and
757 * connector data. */
758 save_crtcs = kzalloc(dev->mode_config.num_crtc *
759 sizeof(struct drm_crtc), GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -0800760 if (!save_crtcs)
761 return -ENOMEM;
762
Maarten Maathuise67aae72009-08-27 10:18:29 +0200763 save_encoders = kzalloc(dev->mode_config.num_encoder *
764 sizeof(struct drm_encoder), GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -0800765 if (!save_encoders) {
766 kfree(save_crtcs);
767 return -ENOMEM;
768 }
769
Maarten Maathuise67aae72009-08-27 10:18:29 +0200770 save_connectors = kzalloc(dev->mode_config.num_connector *
771 sizeof(struct drm_connector), GFP_KERNEL);
772 if (!save_connectors) {
773 kfree(save_crtcs);
774 kfree(save_encoders);
775 return -ENOMEM;
776 }
777
778 /* Copy data. Note that driver private data is not affected.
779 * Should anything bad happen only the expected state is
780 * restored, not the drivers personal bookkeeping.
781 */
782 count = 0;
783 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
784 save_crtcs[count++] = *crtc;
785 }
786
787 count = 0;
788 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
789 save_encoders[count++] = *encoder;
790 }
791
792 count = 0;
793 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
794 save_connectors[count++] = *connector;
795 }
796
Dave Airlief453ba02008-11-07 14:05:41 -0800797 /* We should be able to check here if the fb has the same properties
798 * and then just flip_or_move it */
799 if (set->crtc->fb != set->fb) {
Jesse Barnes712531b2009-01-09 13:56:14 -0800800 /* If we have no fb then treat it as a full mode set */
Jesse Barnes7bec7562009-02-23 16:09:34 -0800801 if (set->crtc->fb == NULL) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800802 DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
Jesse Barnes712531b2009-01-09 13:56:14 -0800803 mode_changed = true;
Jakob Bornecrantz4cb72b12009-08-03 13:43:59 +0100804 } else if (set->fb == NULL) {
805 mode_changed = true;
Jesse Barnes7bec7562009-02-23 16:09:34 -0800806 } else if ((set->fb->bits_per_pixel !=
Jesse Barnes712531b2009-01-09 13:56:14 -0800807 set->crtc->fb->bits_per_pixel) ||
808 set->fb->depth != set->crtc->fb->depth)
809 fb_changed = true;
Dave Airlief453ba02008-11-07 14:05:41 -0800810 else
Jesse Barnes712531b2009-01-09 13:56:14 -0800811 fb_changed = true;
Dave Airlief453ba02008-11-07 14:05:41 -0800812 }
813
814 if (set->x != set->crtc->x || set->y != set->crtc->y)
Jesse Barnes712531b2009-01-09 13:56:14 -0800815 fb_changed = true;
Dave Airlief453ba02008-11-07 14:05:41 -0800816
817 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800818 DRM_DEBUG_KMS("modes are different, full mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -0800819 drm_mode_debug_printmodeline(&set->crtc->mode);
820 drm_mode_debug_printmodeline(set->mode);
Jesse Barnes712531b2009-01-09 13:56:14 -0800821 mode_changed = true;
Dave Airlief453ba02008-11-07 14:05:41 -0800822 }
823
824 /* a) traverse passed in connector list and get encoders for them */
825 count = 0;
826 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
827 struct drm_connector_helper_funcs *connector_funcs =
828 connector->helper_private;
Dave Airlief453ba02008-11-07 14:05:41 -0800829 new_encoder = connector->encoder;
830 for (ro = 0; ro < set->num_connectors; ro++) {
831 if (set->connectors[ro] == connector) {
832 new_encoder = connector_funcs->best_encoder(connector);
833 /* if we can't get an encoder for a connector
834 we are setting now - then fail */
835 if (new_encoder == NULL)
836 /* don't break so fail path works correct */
837 fail = 1;
838 break;
839 }
840 }
841
842 if (new_encoder != connector->encoder) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800843 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
Jesse Barnes712531b2009-01-09 13:56:14 -0800844 mode_changed = true;
Maarten Maathuisff846ab2009-08-19 00:56:45 +0200845 /* If the encoder is reused for another connector, then
846 * the appropriate crtc will be set later.
847 */
Maarten Maathuisff6fdbe2009-09-01 03:39:04 +0200848 if (connector->encoder)
849 connector->encoder->crtc = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800850 connector->encoder = new_encoder;
851 }
852 }
853
854 if (fail) {
855 ret = -EINVAL;
Maarten Maathuise67aae72009-08-27 10:18:29 +0200856 goto fail;
Dave Airlief453ba02008-11-07 14:05:41 -0800857 }
858
859 count = 0;
860 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
861 if (!connector->encoder)
862 continue;
863
Dave Airlief453ba02008-11-07 14:05:41 -0800864 if (connector->encoder->crtc == set->crtc)
865 new_crtc = NULL;
866 else
867 new_crtc = connector->encoder->crtc;
868
869 for (ro = 0; ro < set->num_connectors; ro++) {
870 if (set->connectors[ro] == connector)
871 new_crtc = set->crtc;
872 }
Jesse Barnes7bec7562009-02-23 16:09:34 -0800873
874 /* Make sure the new CRTC will work with the encoder */
875 if (new_crtc &&
876 !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
877 ret = -EINVAL;
Maarten Maathuise67aae72009-08-27 10:18:29 +0200878 goto fail;
Jesse Barnes7bec7562009-02-23 16:09:34 -0800879 }
Dave Airlief453ba02008-11-07 14:05:41 -0800880 if (new_crtc != connector->encoder->crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800881 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
Jesse Barnes712531b2009-01-09 13:56:14 -0800882 mode_changed = true;
Dave Airlief453ba02008-11-07 14:05:41 -0800883 connector->encoder->crtc = new_crtc;
884 }
Zhao Yakui58367ed2009-07-20 13:48:07 +0800885 DRM_DEBUG_KMS("setting connector %d crtc to %p\n",
Jesse Barnes7bec7562009-02-23 16:09:34 -0800886 connector->base.id, new_crtc);
Dave Airlief453ba02008-11-07 14:05:41 -0800887 }
888
889 /* mode_set_base is not a required function */
Jesse Barnes712531b2009-01-09 13:56:14 -0800890 if (fb_changed && !crtc_funcs->mode_set_base)
891 mode_changed = true;
Dave Airlief453ba02008-11-07 14:05:41 -0800892
Jesse Barnes712531b2009-01-09 13:56:14 -0800893 if (mode_changed) {
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -0500894 old_fb = set->crtc->fb;
Dave Airlief453ba02008-11-07 14:05:41 -0800895 set->crtc->fb = set->fb;
896 set->crtc->enabled = (set->mode != NULL);
897 if (set->mode != NULL) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800898 DRM_DEBUG_KMS("attempting to set mode from"
899 " userspace\n");
Dave Airlief453ba02008-11-07 14:05:41 -0800900 drm_mode_debug_printmodeline(set->mode);
901 if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -0500902 set->x, set->y,
903 old_fb)) {
Chris Wilson5c3b82e2009-02-11 13:25:09 +0000904 DRM_ERROR("failed to set mode on crtc %p\n",
905 set->crtc);
Dave Airlief453ba02008-11-07 14:05:41 -0800906 ret = -EINVAL;
Maarten Maathuise67aae72009-08-27 10:18:29 +0200907 goto fail;
Dave Airlief453ba02008-11-07 14:05:41 -0800908 }
909 /* TODO are these needed? */
910 set->crtc->desired_x = set->x;
911 set->crtc->desired_y = set->y;
912 set->crtc->desired_mode = set->mode;
913 }
914 drm_helper_disable_unused_functions(dev);
Jesse Barnes712531b2009-01-09 13:56:14 -0800915 } else if (fb_changed) {
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -0500916 old_fb = set->crtc->fb;
Dave Airlief453ba02008-11-07 14:05:41 -0800917 if (set->crtc->fb != set->fb)
918 set->crtc->fb = set->fb;
Chris Wilson5c3b82e2009-02-11 13:25:09 +0000919 ret = crtc_funcs->mode_set_base(set->crtc,
920 set->x, set->y, old_fb);
921 if (ret != 0)
Maarten Maathuise67aae72009-08-27 10:18:29 +0200922 goto fail;
Dave Airlief453ba02008-11-07 14:05:41 -0800923 }
924
Maarten Maathuise67aae72009-08-27 10:18:29 +0200925 kfree(save_connectors);
Dave Airlief453ba02008-11-07 14:05:41 -0800926 kfree(save_encoders);
927 kfree(save_crtcs);
928 return 0;
929
Maarten Maathuise67aae72009-08-27 10:18:29 +0200930fail:
931 /* Restore all previous data. */
Dave Airlief453ba02008-11-07 14:05:41 -0800932 count = 0;
Maarten Maathuise67aae72009-08-27 10:18:29 +0200933 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
934 *crtc = save_crtcs[count++];
935 }
Chris Wilsone62fb642009-02-11 16:39:21 +0000936
Maarten Maathuise67aae72009-08-27 10:18:29 +0200937 count = 0;
938 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
939 *encoder = save_encoders[count++];
Chris Wilsone62fb642009-02-11 16:39:21 +0000940 }
Maarten Maathuise67aae72009-08-27 10:18:29 +0200941
Dave Airlief453ba02008-11-07 14:05:41 -0800942 count = 0;
943 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Maarten Maathuise67aae72009-08-27 10:18:29 +0200944 *connector = save_connectors[count++];
Dave Airlief453ba02008-11-07 14:05:41 -0800945 }
Maarten Maathuise67aae72009-08-27 10:18:29 +0200946
947 kfree(save_connectors);
Dave Airlief453ba02008-11-07 14:05:41 -0800948 kfree(save_encoders);
Maarten Maathuise67aae72009-08-27 10:18:29 +0200949 kfree(save_crtcs);
Dave Airlief453ba02008-11-07 14:05:41 -0800950 return ret;
951}
952EXPORT_SYMBOL(drm_crtc_helper_set_config);
953
954bool drm_helper_plugged_event(struct drm_device *dev)
955{
Zhao Yakui58367ed2009-07-20 13:48:07 +0800956 DRM_DEBUG_KMS("\n");
Dave Airlief453ba02008-11-07 14:05:41 -0800957
958 drm_helper_probe_connector_modes(dev, dev->mode_config.max_width,
959 dev->mode_config.max_height);
960
961 drm_setup_crtcs(dev);
962
963 /* alert the driver fb layer */
964 dev->mode_config.funcs->fb_changed(dev);
965
966 /* FIXME: send hotplug event */
967 return true;
968}
969/**
970 * drm_initial_config - setup a sane initial connector configuration
971 * @dev: DRM device
Dave Airlief453ba02008-11-07 14:05:41 -0800972 *
973 * LOCKING:
974 * Called at init time, must take mode config lock.
975 *
976 * Scan the CRTCs and connectors and try to put together an initial setup.
977 * At the moment, this is a cloned configuration across all heads with
978 * a new framebuffer object as the backing store.
979 *
980 * RETURNS:
981 * Zero if everything went ok, nonzero otherwise.
982 */
Jesse Barnes7a1fb5d2009-03-27 13:05:19 -0700983bool drm_helper_initial_config(struct drm_device *dev)
Dave Airlief453ba02008-11-07 14:05:41 -0800984{
Jesse Barnes40a518d2009-01-12 12:05:32 -0800985 struct drm_connector *connector;
986 int count = 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800987
Jesse Barnes40a518d2009-01-12 12:05:32 -0800988 count = drm_helper_probe_connector_modes(dev,
989 dev->mode_config.max_width,
990 dev->mode_config.max_height);
991
992 /*
993 * None of the available connectors had any modes, so add some
994 * and try to light them up anyway
995 */
996 if (!count) {
997 DRM_ERROR("connectors have no modes, using standard modes\n");
998 list_for_each_entry(connector,
999 &dev->mode_config.connector_list,
1000 head)
1001 drm_helper_add_std_modes(dev, connector);
1002 }
1003
1004 drm_setup_crtcs(dev);
1005
1006 /* alert the driver fb layer */
1007 dev->mode_config.funcs->fb_changed(dev);
1008
1009 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001010}
1011EXPORT_SYMBOL(drm_helper_initial_config);
1012
Keith Packardc9fb15f2009-05-30 20:42:28 -07001013static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder)
1014{
1015 int dpms = DRM_MODE_DPMS_OFF;
1016 struct drm_connector *connector;
1017 struct drm_device *dev = encoder->dev;
1018
1019 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1020 if (connector->encoder == encoder)
1021 if (connector->dpms < dpms)
1022 dpms = connector->dpms;
1023 return dpms;
1024}
1025
1026static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc)
1027{
1028 int dpms = DRM_MODE_DPMS_OFF;
1029 struct drm_connector *connector;
1030 struct drm_device *dev = crtc->dev;
1031
1032 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1033 if (connector->encoder && connector->encoder->crtc == crtc)
1034 if (connector->dpms < dpms)
1035 dpms = connector->dpms;
1036 return dpms;
1037}
1038
1039/**
1040 * drm_helper_connector_dpms
1041 * @connector affected connector
1042 * @mode DPMS mode
1043 *
1044 * Calls the low-level connector DPMS function, then
1045 * calls appropriate encoder and crtc DPMS functions as well
1046 */
1047void drm_helper_connector_dpms(struct drm_connector *connector, int mode)
1048{
1049 struct drm_encoder *encoder = connector->encoder;
1050 struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
1051 int old_dpms;
1052
1053 if (mode == connector->dpms)
1054 return;
1055
1056 old_dpms = connector->dpms;
1057 connector->dpms = mode;
1058
1059 /* from off to on, do crtc then encoder */
1060 if (mode < old_dpms) {
1061 if (crtc) {
1062 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
1063 if (crtc_funcs->dpms)
1064 (*crtc_funcs->dpms) (crtc,
1065 drm_helper_choose_crtc_dpms(crtc));
1066 }
1067 if (encoder) {
1068 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
1069 if (encoder_funcs->dpms)
1070 (*encoder_funcs->dpms) (encoder,
1071 drm_helper_choose_encoder_dpms(encoder));
1072 }
1073 }
1074
1075 /* from on to off, do encoder then crtc */
1076 if (mode > old_dpms) {
1077 if (encoder) {
1078 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
1079 if (encoder_funcs->dpms)
1080 (*encoder_funcs->dpms) (encoder,
1081 drm_helper_choose_encoder_dpms(encoder));
1082 }
1083 if (crtc) {
1084 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
1085 if (crtc_funcs->dpms)
1086 (*crtc_funcs->dpms) (crtc,
1087 drm_helper_choose_crtc_dpms(crtc));
1088 }
1089 }
1090
1091 return;
1092}
1093EXPORT_SYMBOL(drm_helper_connector_dpms);
1094
Dave Airlief453ba02008-11-07 14:05:41 -08001095/**
1096 * drm_hotplug_stage_two
1097 * @dev DRM device
1098 * @connector hotpluged connector
1099 *
1100 * LOCKING.
1101 * Caller must hold mode config lock, function might grab struct lock.
1102 *
1103 * Stage two of a hotplug.
1104 *
1105 * RETURNS:
1106 * Zero on success, errno on failure.
1107 */
1108int drm_helper_hotplug_stage_two(struct drm_device *dev)
1109{
Dave Airlief453ba02008-11-07 14:05:41 -08001110 drm_helper_plugged_event(dev);
1111
1112 return 0;
1113}
1114EXPORT_SYMBOL(drm_helper_hotplug_stage_two);
1115
1116int drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb,
1117 struct drm_mode_fb_cmd *mode_cmd)
1118{
1119 fb->width = mode_cmd->width;
1120 fb->height = mode_cmd->height;
1121 fb->pitch = mode_cmd->pitch;
1122 fb->bits_per_pixel = mode_cmd->bpp;
1123 fb->depth = mode_cmd->depth;
1124
1125 return 0;
1126}
1127EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
1128
1129int drm_helper_resume_force_mode(struct drm_device *dev)
1130{
1131 struct drm_crtc *crtc;
1132 int ret;
1133
1134 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1135
1136 if (!crtc->enabled)
1137 continue;
1138
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05001139 ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
1140 crtc->x, crtc->y, crtc->fb);
Dave Airlief453ba02008-11-07 14:05:41 -08001141
1142 if (ret == false)
1143 DRM_ERROR("failed to set mode on crtc %p\n", crtc);
1144 }
Zhao Yakuiaf4fcb52009-07-08 14:13:13 +08001145 /* disable the unused connectors while restoring the modesetting */
1146 drm_helper_disable_unused_functions(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001147 return 0;
1148}
1149EXPORT_SYMBOL(drm_helper_resume_force_mode);