blob: 82fd6e82450f665d4bfcca644b13a95d2161bd00 [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"
Dave Airlied50ba252009-09-23 14:44:08 +100035#include "drm_fb_helper.h"
Dave Airlief453ba02008-11-07 14:05:41 -080036
yakui_zhao67149772009-04-02 11:52:12 +080037static void drm_mode_validate_flag(struct drm_connector *connector,
38 int flags)
39{
40 struct drm_display_mode *mode, *t;
41
42 if (flags == (DRM_MODE_FLAG_DBLSCAN | DRM_MODE_FLAG_INTERLACE))
43 return;
44
45 list_for_each_entry_safe(mode, t, &connector->modes, head) {
46 if ((mode->flags & DRM_MODE_FLAG_INTERLACE) &&
47 !(flags & DRM_MODE_FLAG_INTERLACE))
48 mode->status = MODE_NO_INTERLACE;
49 if ((mode->flags & DRM_MODE_FLAG_DBLSCAN) &&
50 !(flags & DRM_MODE_FLAG_DBLSCAN))
51 mode->status = MODE_NO_DBLESCAN;
52 }
53
54 return;
55}
56
Dave Airlief453ba02008-11-07 14:05:41 -080057/**
58 * drm_helper_probe_connector_modes - get complete set of display modes
59 * @dev: DRM device
60 * @maxX: max width for modes
61 * @maxY: max height for modes
62 *
63 * LOCKING:
64 * Caller must hold mode config lock.
65 *
66 * Based on @dev's mode_config layout, scan all the connectors and try to detect
67 * modes on them. Modes will first be added to the connector's probed_modes
68 * list, then culled (based on validity and the @maxX, @maxY parameters) and
69 * put into the normal modes list.
70 *
71 * Intended to be used either at bootup time or when major configuration
72 * changes have occurred.
73 *
74 * FIXME: take into account monitor limits
Jesse Barnes40a518d2009-01-12 12:05:32 -080075 *
76 * RETURNS:
77 * Number of modes found on @connector.
Dave Airlief453ba02008-11-07 14:05:41 -080078 */
Jesse Barnes40a518d2009-01-12 12:05:32 -080079int drm_helper_probe_single_connector_modes(struct drm_connector *connector,
80 uint32_t maxX, uint32_t maxY)
Dave Airlief453ba02008-11-07 14:05:41 -080081{
82 struct drm_device *dev = connector->dev;
83 struct drm_display_mode *mode, *t;
84 struct drm_connector_helper_funcs *connector_funcs =
85 connector->helper_private;
Jesse Barnes40a518d2009-01-12 12:05:32 -080086 int count = 0;
yakui_zhao67149772009-04-02 11:52:12 +080087 int mode_flags = 0;
Dave Airlief453ba02008-11-07 14:05:41 -080088
Zhao Yakui58367ed2009-07-20 13:48:07 +080089 DRM_DEBUG_KMS("%s\n", drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -080090 /* set all modes to the unverified state */
91 list_for_each_entry_safe(mode, t, &connector->modes, head)
92 mode->status = MODE_UNVERIFIED;
93
Dave Airlied50ba252009-09-23 14:44:08 +100094 if (connector->force) {
95 if (connector->force == DRM_FORCE_ON)
96 connector->status = connector_status_connected;
97 else
98 connector->status = connector_status_disconnected;
99 if (connector->funcs->force)
100 connector->funcs->force(connector);
101 } else
102 connector->status = connector->funcs->detect(connector);
Dave Airlief453ba02008-11-07 14:05:41 -0800103
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));
Adam Jackson620f3782009-09-08 11:51:46 +1000107 goto prune;
Dave Airlief453ba02008-11-07 14:05:41 -0800108 }
109
Jesse Barnes40a518d2009-01-12 12:05:32 -0800110 count = (*connector_funcs->get_modes)(connector);
ykzhao50fe4cf2009-09-03 14:30:04 +0800111 if (!count) {
112 count = drm_add_modes_noedid(connector, 800, 600);
113 if (!count)
114 return 0;
115 }
Dave Airlief453ba02008-11-07 14:05:41 -0800116
Jesse Barnes40a518d2009-01-12 12:05:32 -0800117 drm_mode_connector_list_update(connector);
Dave Airlief453ba02008-11-07 14:05:41 -0800118
119 if (maxX && maxY)
120 drm_mode_validate_size(dev, &connector->modes, maxX,
121 maxY, 0);
yakui_zhao67149772009-04-02 11:52:12 +0800122
123 if (connector->interlace_allowed)
124 mode_flags |= DRM_MODE_FLAG_INTERLACE;
125 if (connector->doublescan_allowed)
126 mode_flags |= DRM_MODE_FLAG_DBLSCAN;
127 drm_mode_validate_flag(connector, mode_flags);
128
Dave Airlief453ba02008-11-07 14:05:41 -0800129 list_for_each_entry_safe(mode, t, &connector->modes, head) {
130 if (mode->status == MODE_OK)
131 mode->status = connector_funcs->mode_valid(connector,
132 mode);
133 }
134
Adam Jackson620f3782009-09-08 11:51:46 +1000135prune:
Dave Airlief453ba02008-11-07 14:05:41 -0800136 drm_mode_prune_invalid(dev, &connector->modes, true);
137
Jesse Barnes40a518d2009-01-12 12:05:32 -0800138 if (list_empty(&connector->modes))
139 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800140
141 drm_mode_sort(&connector->modes);
142
Zhao Yakui58367ed2009-07-20 13:48:07 +0800143 DRM_DEBUG_KMS("Probed modes for %s\n",
144 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -0800145 list_for_each_entry_safe(mode, t, &connector->modes, head) {
146 mode->vrefresh = drm_mode_vrefresh(mode);
147
148 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
149 drm_mode_debug_printmodeline(mode);
150 }
Jesse Barnes40a518d2009-01-12 12:05:32 -0800151
152 return count;
Dave Airlief453ba02008-11-07 14:05:41 -0800153}
154EXPORT_SYMBOL(drm_helper_probe_single_connector_modes);
155
Jesse Barnes40a518d2009-01-12 12:05:32 -0800156int drm_helper_probe_connector_modes(struct drm_device *dev, uint32_t maxX,
Dave Airlief453ba02008-11-07 14:05:41 -0800157 uint32_t maxY)
158{
159 struct drm_connector *connector;
Jesse Barnes40a518d2009-01-12 12:05:32 -0800160 int count = 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800161
162 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Jesse Barnes40a518d2009-01-12 12:05:32 -0800163 count += drm_helper_probe_single_connector_modes(connector,
164 maxX, maxY);
Dave Airlief453ba02008-11-07 14:05:41 -0800165 }
Jesse Barnes40a518d2009-01-12 12:05:32 -0800166
167 return count;
Dave Airlief453ba02008-11-07 14:05:41 -0800168}
169EXPORT_SYMBOL(drm_helper_probe_connector_modes);
170
Dave Airlief453ba02008-11-07 14:05:41 -0800171/**
Keith Packardc9fb15f2009-05-30 20:42:28 -0700172 * drm_helper_encoder_in_use - check if a given encoder is in use
173 * @encoder: encoder to check
174 *
175 * LOCKING:
176 * Caller must hold mode config lock.
177 *
178 * Walk @encoders's DRM device's mode_config and see if it's in use.
179 *
180 * RETURNS:
181 * True if @encoder is part of the mode_config, false otherwise.
182 */
183bool drm_helper_encoder_in_use(struct drm_encoder *encoder)
184{
185 struct drm_connector *connector;
186 struct drm_device *dev = encoder->dev;
187 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
188 if (connector->encoder == encoder)
189 return true;
190 return false;
191}
192EXPORT_SYMBOL(drm_helper_encoder_in_use);
193
194/**
Dave Airlief453ba02008-11-07 14:05:41 -0800195 * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config
196 * @crtc: CRTC to check
197 *
198 * LOCKING:
199 * Caller must hold mode config lock.
200 *
201 * Walk @crtc's DRM device's mode_config and see if it's in use.
202 *
203 * RETURNS:
204 * True if @crtc is part of the mode_config, false otherwise.
205 */
206bool drm_helper_crtc_in_use(struct drm_crtc *crtc)
207{
208 struct drm_encoder *encoder;
209 struct drm_device *dev = crtc->dev;
210 /* FIXME: Locking around list access? */
211 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
Keith Packardc9fb15f2009-05-30 20:42:28 -0700212 if (encoder->crtc == crtc && drm_helper_encoder_in_use(encoder))
Dave Airlief453ba02008-11-07 14:05:41 -0800213 return true;
214 return false;
215}
216EXPORT_SYMBOL(drm_helper_crtc_in_use);
217
218/**
219 * drm_disable_unused_functions - disable unused objects
220 * @dev: DRM device
221 *
222 * LOCKING:
223 * Caller must hold mode config lock.
224 *
225 * If an connector or CRTC isn't part of @dev's mode_config, it can be disabled
226 * by calling its dpms function, which should power it off.
227 */
228void drm_helper_disable_unused_functions(struct drm_device *dev)
229{
230 struct drm_encoder *encoder;
Dave Airliea3a05442009-08-31 15:16:30 +1000231 struct drm_connector *connector;
Dave Airlief453ba02008-11-07 14:05:41 -0800232 struct drm_encoder_helper_funcs *encoder_funcs;
233 struct drm_crtc *crtc;
234
Dave Airliea3a05442009-08-31 15:16:30 +1000235 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
236 if (!connector->encoder)
237 continue;
238 if (connector->status == connector_status_disconnected)
239 connector->encoder = NULL;
240 }
241
Dave Airlief453ba02008-11-07 14:05:41 -0800242 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
243 encoder_funcs = encoder->helper_private;
Dave Airliea3a05442009-08-31 15:16:30 +1000244 if (!drm_helper_encoder_in_use(encoder)) {
245 if (encoder_funcs->disable)
246 (*encoder_funcs->disable)(encoder);
247 else
248 (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
Dave Airlie9c552dd2009-09-02 14:00:11 +1000249 /* disconnector encoder from any connector */
250 encoder->crtc = NULL;
Dave Airliea3a05442009-08-31 15:16:30 +1000251 }
Dave Airlief453ba02008-11-07 14:05:41 -0800252 }
253
254 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
255 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
256 crtc->enabled = drm_helper_crtc_in_use(crtc);
257 if (!crtc->enabled) {
258 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
259 crtc->fb = NULL;
260 }
261 }
262}
263EXPORT_SYMBOL(drm_helper_disable_unused_functions);
264
265static struct drm_display_mode *drm_has_preferred_mode(struct drm_connector *connector, int width, int height)
266{
267 struct drm_display_mode *mode;
268
269 list_for_each_entry(mode, &connector->modes, head) {
270 if (drm_mode_width(mode) > width ||
271 drm_mode_height(mode) > height)
272 continue;
273 if (mode->type & DRM_MODE_TYPE_PREFERRED)
274 return mode;
275 }
276 return NULL;
277}
278
Dave Airlied50ba252009-09-23 14:44:08 +1000279static bool drm_has_cmdline_mode(struct drm_connector *connector)
280{
281 struct drm_fb_helper_connector *fb_help_conn = connector->fb_helper_private;
282 struct drm_fb_helper_cmdline_mode *cmdline_mode = &fb_help_conn->cmdline_mode;
283 return cmdline_mode->specified;
284}
285
286static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_connector *connector, int width, int height)
287{
288 struct drm_fb_helper_connector *fb_help_conn = connector->fb_helper_private;
289 struct drm_fb_helper_cmdline_mode *cmdline_mode = &fb_help_conn->cmdline_mode;
290 struct drm_display_mode *mode = NULL;
291
292 if (cmdline_mode->specified == false)
293 return mode;
294
295 /* attempt to find a matching mode in the list of modes
296 * we have gotten so far, if not add a CVT mode that conforms
297 */
298 if (cmdline_mode->rb || cmdline_mode->margins)
299 goto create_mode;
300
301 list_for_each_entry(mode, &connector->modes, head) {
302 /* check width/height */
303 if (mode->hdisplay != cmdline_mode->xres ||
304 mode->vdisplay != cmdline_mode->yres)
305 continue;
306
307 if (cmdline_mode->refresh_specified) {
308 if (mode->vrefresh != cmdline_mode->refresh)
309 continue;
310 }
311
312 if (cmdline_mode->interlace) {
313 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
314 continue;
315 }
316 return mode;
317 }
318
319create_mode:
320 mode = drm_cvt_mode(connector->dev, cmdline_mode->xres,
321 cmdline_mode->yres,
322 cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
323 cmdline_mode->rb, cmdline_mode->interlace,
324 cmdline_mode->margins);
325 list_add(&mode->head, &connector->modes);
326 return mode;
327}
328
Dave Airlief453ba02008-11-07 14:05:41 -0800329static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
330{
331 bool enable;
332
333 if (strict) {
334 enable = connector->status == connector_status_connected;
335 } else {
336 enable = connector->status != connector_status_disconnected;
337 }
338 return enable;
339}
340
341static void drm_enable_connectors(struct drm_device *dev, bool *enabled)
342{
343 bool any_enabled = false;
344 struct drm_connector *connector;
345 int i = 0;
346
347 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
348 enabled[i] = drm_connector_enabled(connector, true);
Zhao Yakui58367ed2009-07-20 13:48:07 +0800349 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
Jesse Barnes40a518d2009-01-12 12:05:32 -0800350 enabled[i] ? "yes" : "no");
Dave Airlief453ba02008-11-07 14:05:41 -0800351 any_enabled |= enabled[i];
352 i++;
353 }
354
355 if (any_enabled)
356 return;
357
358 i = 0;
359 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
360 enabled[i] = drm_connector_enabled(connector, false);
361 i++;
362 }
363}
364
365static bool drm_target_preferred(struct drm_device *dev,
366 struct drm_display_mode **modes,
367 bool *enabled, int width, int height)
368{
369 struct drm_connector *connector;
370 int i = 0;
371
372 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
373
374 if (enabled[i] == false) {
375 i++;
376 continue;
377 }
378
Dave Airlied50ba252009-09-23 14:44:08 +1000379 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
380 connector->base.id);
Jesse Barnes40a518d2009-01-12 12:05:32 -0800381
Dave Airlied50ba252009-09-23 14:44:08 +1000382 /* got for command line mode first */
383 modes[i] = drm_pick_cmdline_mode(connector, width, height);
384 if (!modes[i]) {
385 DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
386 connector->base.id);
387 modes[i] = drm_has_preferred_mode(connector, width, height);
388 }
Jesse Barnes40a518d2009-01-12 12:05:32 -0800389 /* No preferred modes, pick one off the list */
390 if (!modes[i] && !list_empty(&connector->modes)) {
Dave Airlief453ba02008-11-07 14:05:41 -0800391 list_for_each_entry(modes[i], &connector->modes, head)
392 break;
393 }
Zhao Yakui58367ed2009-07-20 13:48:07 +0800394 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
Jesse Barnes40a518d2009-01-12 12:05:32 -0800395 "none");
Dave Airlief453ba02008-11-07 14:05:41 -0800396 i++;
397 }
398 return true;
399}
400
401static int drm_pick_crtcs(struct drm_device *dev,
402 struct drm_crtc **best_crtcs,
403 struct drm_display_mode **modes,
404 int n, int width, int height)
405{
406 int c, o;
407 struct drm_connector *connector;
408 struct drm_connector_helper_funcs *connector_funcs;
409 struct drm_encoder *encoder;
410 struct drm_crtc *best_crtc;
411 int my_score, best_score, score;
412 struct drm_crtc **crtcs, *crtc;
413
414 if (n == dev->mode_config.num_connector)
415 return 0;
416 c = 0;
417 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
418 if (c == n)
419 break;
420 c++;
421 }
422
423 best_crtcs[n] = NULL;
424 best_crtc = NULL;
425 best_score = drm_pick_crtcs(dev, best_crtcs, modes, n+1, width, height);
426 if (modes[n] == NULL)
427 return best_score;
428
429 crtcs = kmalloc(dev->mode_config.num_connector *
430 sizeof(struct drm_crtc *), GFP_KERNEL);
431 if (!crtcs)
432 return best_score;
433
434 my_score = 1;
435 if (connector->status == connector_status_connected)
436 my_score++;
Dave Airlied50ba252009-09-23 14:44:08 +1000437 if (drm_has_cmdline_mode(connector))
438 my_score++;
Dave Airlief453ba02008-11-07 14:05:41 -0800439 if (drm_has_preferred_mode(connector, width, height))
440 my_score++;
441
442 connector_funcs = connector->helper_private;
443 encoder = connector_funcs->best_encoder(connector);
444 if (!encoder)
445 goto out;
446
447 connector->encoder = encoder;
448
449 /* select a crtc for this connector and then attempt to configure
450 remaining connectors */
451 c = 0;
452 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
453
Dave Airliea3a05442009-08-31 15:16:30 +1000454 if ((encoder->possible_crtcs & (1 << c)) == 0) {
Dave Airlief453ba02008-11-07 14:05:41 -0800455 c++;
456 continue;
457 }
458
459 for (o = 0; o < n; o++)
460 if (best_crtcs[o] == crtc)
461 break;
462
463 if (o < n) {
464 /* ignore cloning for now */
465 c++;
466 continue;
467 }
468
469 crtcs[n] = crtc;
470 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_crtc *));
471 score = my_score + drm_pick_crtcs(dev, crtcs, modes, n + 1,
472 width, height);
473 if (score > best_score) {
474 best_crtc = crtc;
475 best_score = score;
476 memcpy(best_crtcs, crtcs,
477 dev->mode_config.num_connector *
478 sizeof(struct drm_crtc *));
479 }
480 c++;
481 }
482out:
483 kfree(crtcs);
484 return best_score;
485}
486
487static void drm_setup_crtcs(struct drm_device *dev)
488{
489 struct drm_crtc **crtcs;
490 struct drm_display_mode **modes;
491 struct drm_encoder *encoder;
492 struct drm_connector *connector;
493 bool *enabled;
494 int width, height;
495 int i, ret;
496
Zhao Yakui58367ed2009-07-20 13:48:07 +0800497 DRM_DEBUG_KMS("\n");
Jesse Barnes40a518d2009-01-12 12:05:32 -0800498
Dave Airlief453ba02008-11-07 14:05:41 -0800499 width = dev->mode_config.max_width;
500 height = dev->mode_config.max_height;
501
502 /* clean out all the encoder/crtc combos */
503 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
504 encoder->crtc = NULL;
505 }
506
507 crtcs = kcalloc(dev->mode_config.num_connector,
508 sizeof(struct drm_crtc *), GFP_KERNEL);
509 modes = kcalloc(dev->mode_config.num_connector,
510 sizeof(struct drm_display_mode *), GFP_KERNEL);
511 enabled = kcalloc(dev->mode_config.num_connector,
512 sizeof(bool), GFP_KERNEL);
513
514 drm_enable_connectors(dev, enabled);
515
516 ret = drm_target_preferred(dev, modes, enabled, width, height);
517 if (!ret)
518 DRM_ERROR("Unable to find initial modes\n");
519
Zhao Yakui58367ed2009-07-20 13:48:07 +0800520 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
Jesse Barnes40a518d2009-01-12 12:05:32 -0800521
Dave Airlief453ba02008-11-07 14:05:41 -0800522 drm_pick_crtcs(dev, crtcs, modes, 0, width, height);
523
524 i = 0;
525 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
526 struct drm_display_mode *mode = modes[i];
527 struct drm_crtc *crtc = crtcs[i];
528
529 if (connector->encoder == NULL) {
530 i++;
531 continue;
532 }
533
534 if (mode && crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800535 DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
Jesse Barnes40a518d2009-01-12 12:05:32 -0800536 mode->name, crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -0800537 crtc->desired_mode = mode;
538 connector->encoder->crtc = crtc;
Dave Airliea3a05442009-08-31 15:16:30 +1000539 } else {
Dave Airlief453ba02008-11-07 14:05:41 -0800540 connector->encoder->crtc = NULL;
Dave Airliea3a05442009-08-31 15:16:30 +1000541 connector->encoder = NULL;
542 }
Dave Airlief453ba02008-11-07 14:05:41 -0800543 i++;
544 }
545
546 kfree(crtcs);
547 kfree(modes);
548 kfree(enabled);
549}
Jesse Barnes7bec7562009-02-23 16:09:34 -0800550
551/**
552 * drm_encoder_crtc_ok - can a given crtc drive a given encoder?
553 * @encoder: encoder to test
554 * @crtc: crtc to test
555 *
556 * Return false if @encoder can't be driven by @crtc, true otherwise.
557 */
558static bool drm_encoder_crtc_ok(struct drm_encoder *encoder,
559 struct drm_crtc *crtc)
560{
561 struct drm_device *dev;
562 struct drm_crtc *tmp;
563 int crtc_mask = 1;
564
565 WARN(!crtc, "checking null crtc?");
566
567 dev = crtc->dev;
568
569 list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
570 if (tmp == crtc)
571 break;
572 crtc_mask <<= 1;
573 }
574
575 if (encoder->possible_crtcs & crtc_mask)
576 return true;
577 return false;
578}
579
580/*
581 * Check the CRTC we're going to map each output to vs. its current
582 * CRTC. If they don't match, we have to disable the output and the CRTC
583 * since the driver will have to re-route things.
584 */
585static void
586drm_crtc_prepare_encoders(struct drm_device *dev)
587{
588 struct drm_encoder_helper_funcs *encoder_funcs;
589 struct drm_encoder *encoder;
590
591 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
592 encoder_funcs = encoder->helper_private;
593 /* Disable unused encoders */
594 if (encoder->crtc == NULL)
595 (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
596 /* Disable encoders whose CRTC is about to change */
597 if (encoder_funcs->get_crtc &&
598 encoder->crtc != (*encoder_funcs->get_crtc)(encoder))
599 (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
600 }
601}
602
Dave Airlief453ba02008-11-07 14:05:41 -0800603/**
604 * drm_crtc_set_mode - set a mode
605 * @crtc: CRTC to program
606 * @mode: mode to use
607 * @x: width of mode
608 * @y: height of mode
609 *
610 * LOCKING:
611 * Caller must hold mode config lock.
612 *
613 * Try to set @mode on @crtc. Give @crtc and its associated connectors a chance
614 * to fixup or reject the mode prior to trying to set it.
615 *
616 * RETURNS:
617 * True if the mode was set successfully, or false otherwise.
618 */
619bool drm_crtc_helper_set_mode(struct drm_crtc *crtc,
620 struct drm_display_mode *mode,
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -0500621 int x, int y,
622 struct drm_framebuffer *old_fb)
Dave Airlief453ba02008-11-07 14:05:41 -0800623{
624 struct drm_device *dev = crtc->dev;
625 struct drm_display_mode *adjusted_mode, saved_mode;
626 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
627 struct drm_encoder_helper_funcs *encoder_funcs;
628 int saved_x, saved_y;
629 struct drm_encoder *encoder;
630 bool ret = true;
631
632 adjusted_mode = drm_mode_duplicate(dev, mode);
633
634 crtc->enabled = drm_helper_crtc_in_use(crtc);
635
636 if (!crtc->enabled)
637 return true;
638
639 saved_mode = crtc->mode;
640 saved_x = crtc->x;
641 saved_y = crtc->y;
642
643 /* Update crtc values up front so the driver can rely on them for mode
644 * setting.
645 */
646 crtc->mode = *mode;
647 crtc->x = x;
648 crtc->y = y;
649
Dave Airlief453ba02008-11-07 14:05:41 -0800650 /* Pass our mode to the connectors and the CRTC to give them a chance to
651 * adjust it according to limitations or connector properties, and also
652 * a chance to reject the mode entirely.
653 */
654 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
655
656 if (encoder->crtc != crtc)
657 continue;
658 encoder_funcs = encoder->helper_private;
659 if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
660 adjusted_mode))) {
661 goto done;
662 }
663 }
664
665 if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) {
666 goto done;
667 }
668
669 /* Prepare the encoders and CRTCs before setting the mode. */
670 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
671
672 if (encoder->crtc != crtc)
673 continue;
674 encoder_funcs = encoder->helper_private;
675 /* Disable the encoders as the first thing we do. */
676 encoder_funcs->prepare(encoder);
677 }
678
Jesse Barnes7bec7562009-02-23 16:09:34 -0800679 drm_crtc_prepare_encoders(dev);
680
Dave Airlief453ba02008-11-07 14:05:41 -0800681 crtc_funcs->prepare(crtc);
682
683 /* Set up the DPLL and any encoders state that needs to adjust or depend
684 * on the DPLL.
685 */
Chris Wilson5c3b82e2009-02-11 13:25:09 +0000686 ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
687 if (!ret)
688 goto done;
Dave Airlief453ba02008-11-07 14:05:41 -0800689
690 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
691
692 if (encoder->crtc != crtc)
693 continue;
694
695 DRM_INFO("%s: set mode %s %x\n", drm_get_encoder_name(encoder),
696 mode->name, mode->base.id);
697 encoder_funcs = encoder->helper_private;
698 encoder_funcs->mode_set(encoder, mode, adjusted_mode);
699 }
700
701 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
702 crtc_funcs->commit(crtc);
703
704 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
705
706 if (encoder->crtc != crtc)
707 continue;
708
709 encoder_funcs = encoder->helper_private;
710 encoder_funcs->commit(encoder);
711
712 }
713
714 /* XXX free adjustedmode */
715 drm_mode_destroy(dev, adjusted_mode);
716 /* FIXME: add subpixel order */
717done:
718 if (!ret) {
719 crtc->mode = saved_mode;
720 crtc->x = saved_x;
721 crtc->y = saved_y;
722 }
723
724 return ret;
725}
726EXPORT_SYMBOL(drm_crtc_helper_set_mode);
727
728
729/**
730 * drm_crtc_helper_set_config - set a new config from userspace
731 * @crtc: CRTC to setup
732 * @crtc_info: user provided configuration
733 * @new_mode: new mode to set
734 * @connector_set: set of connectors for the new config
735 * @fb: new framebuffer
736 *
737 * LOCKING:
738 * Caller must hold mode config lock.
739 *
740 * Setup a new configuration, provided by the user in @crtc_info, and enable
741 * it.
742 *
743 * RETURNS:
744 * Zero. (FIXME)
745 */
746int drm_crtc_helper_set_config(struct drm_mode_set *set)
747{
748 struct drm_device *dev;
Maarten Maathuise67aae72009-08-27 10:18:29 +0200749 struct drm_crtc *save_crtcs, *new_crtc, *crtc;
750 struct drm_encoder *save_encoders, *new_encoder, *encoder;
Jesse Barnes7bec7562009-02-23 16:09:34 -0800751 struct drm_framebuffer *old_fb = NULL;
Jakob Bornecrantz4cb72b12009-08-03 13:43:59 +0100752 bool mode_changed = false; /* if true do a full mode set */
753 bool fb_changed = false; /* if true and !mode_changed just do a flip */
Maarten Maathuise67aae72009-08-27 10:18:29 +0200754 struct drm_connector *save_connectors, *connector;
Dave Airlief453ba02008-11-07 14:05:41 -0800755 int count = 0, ro, fail = 0;
756 struct drm_crtc_helper_funcs *crtc_funcs;
757 int ret = 0;
758
Zhao Yakui58367ed2009-07-20 13:48:07 +0800759 DRM_DEBUG_KMS("\n");
Dave Airlief453ba02008-11-07 14:05:41 -0800760
761 if (!set)
762 return -EINVAL;
763
764 if (!set->crtc)
765 return -EINVAL;
766
767 if (!set->crtc->helper_private)
768 return -EINVAL;
769
770 crtc_funcs = set->crtc->helper_private;
771
Zhao Yakui58367ed2009-07-20 13:48:07 +0800772 DRM_DEBUG_KMS("crtc: %p %d fb: %p connectors: %p num_connectors:"
773 " %d (x, y) (%i, %i)\n",
Dave Airlief453ba02008-11-07 14:05:41 -0800774 set->crtc, set->crtc->base.id, set->fb, set->connectors,
775 (int)set->num_connectors, set->x, set->y);
776
777 dev = set->crtc->dev;
778
Maarten Maathuise67aae72009-08-27 10:18:29 +0200779 /* Allocate space for the backup of all (non-pointer) crtc, encoder and
780 * connector data. */
781 save_crtcs = kzalloc(dev->mode_config.num_crtc *
782 sizeof(struct drm_crtc), GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -0800783 if (!save_crtcs)
784 return -ENOMEM;
785
Maarten Maathuise67aae72009-08-27 10:18:29 +0200786 save_encoders = kzalloc(dev->mode_config.num_encoder *
787 sizeof(struct drm_encoder), GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -0800788 if (!save_encoders) {
789 kfree(save_crtcs);
790 return -ENOMEM;
791 }
792
Maarten Maathuise67aae72009-08-27 10:18:29 +0200793 save_connectors = kzalloc(dev->mode_config.num_connector *
794 sizeof(struct drm_connector), GFP_KERNEL);
795 if (!save_connectors) {
796 kfree(save_crtcs);
797 kfree(save_encoders);
798 return -ENOMEM;
799 }
800
801 /* Copy data. Note that driver private data is not affected.
802 * Should anything bad happen only the expected state is
803 * restored, not the drivers personal bookkeeping.
804 */
805 count = 0;
806 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
807 save_crtcs[count++] = *crtc;
808 }
809
810 count = 0;
811 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
812 save_encoders[count++] = *encoder;
813 }
814
815 count = 0;
816 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
817 save_connectors[count++] = *connector;
818 }
819
Dave Airlief453ba02008-11-07 14:05:41 -0800820 /* We should be able to check here if the fb has the same properties
821 * and then just flip_or_move it */
822 if (set->crtc->fb != set->fb) {
Jesse Barnes712531b2009-01-09 13:56:14 -0800823 /* If we have no fb then treat it as a full mode set */
Jesse Barnes7bec7562009-02-23 16:09:34 -0800824 if (set->crtc->fb == NULL) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800825 DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
Jesse Barnes712531b2009-01-09 13:56:14 -0800826 mode_changed = true;
Jakob Bornecrantz4cb72b12009-08-03 13:43:59 +0100827 } else if (set->fb == NULL) {
828 mode_changed = true;
Jesse Barnes7bec7562009-02-23 16:09:34 -0800829 } else if ((set->fb->bits_per_pixel !=
Jesse Barnes712531b2009-01-09 13:56:14 -0800830 set->crtc->fb->bits_per_pixel) ||
831 set->fb->depth != set->crtc->fb->depth)
832 fb_changed = true;
Dave Airlief453ba02008-11-07 14:05:41 -0800833 else
Jesse Barnes712531b2009-01-09 13:56:14 -0800834 fb_changed = true;
Dave Airlief453ba02008-11-07 14:05:41 -0800835 }
836
837 if (set->x != set->crtc->x || set->y != set->crtc->y)
Jesse Barnes712531b2009-01-09 13:56:14 -0800838 fb_changed = true;
Dave Airlief453ba02008-11-07 14:05:41 -0800839
840 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800841 DRM_DEBUG_KMS("modes are different, full mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -0800842 drm_mode_debug_printmodeline(&set->crtc->mode);
843 drm_mode_debug_printmodeline(set->mode);
Jesse Barnes712531b2009-01-09 13:56:14 -0800844 mode_changed = true;
Dave Airlief453ba02008-11-07 14:05:41 -0800845 }
846
847 /* a) traverse passed in connector list and get encoders for them */
848 count = 0;
849 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
850 struct drm_connector_helper_funcs *connector_funcs =
851 connector->helper_private;
Dave Airlief453ba02008-11-07 14:05:41 -0800852 new_encoder = connector->encoder;
853 for (ro = 0; ro < set->num_connectors; ro++) {
854 if (set->connectors[ro] == connector) {
855 new_encoder = connector_funcs->best_encoder(connector);
856 /* if we can't get an encoder for a connector
857 we are setting now - then fail */
858 if (new_encoder == NULL)
859 /* don't break so fail path works correct */
860 fail = 1;
861 break;
862 }
863 }
864
865 if (new_encoder != connector->encoder) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800866 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
Jesse Barnes712531b2009-01-09 13:56:14 -0800867 mode_changed = true;
Maarten Maathuisff846ab2009-08-19 00:56:45 +0200868 /* If the encoder is reused for another connector, then
869 * the appropriate crtc will be set later.
870 */
Maarten Maathuisff6fdbe2009-09-01 03:39:04 +0200871 if (connector->encoder)
872 connector->encoder->crtc = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800873 connector->encoder = new_encoder;
874 }
875 }
876
877 if (fail) {
878 ret = -EINVAL;
Maarten Maathuise67aae72009-08-27 10:18:29 +0200879 goto fail;
Dave Airlief453ba02008-11-07 14:05:41 -0800880 }
881
882 count = 0;
883 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
884 if (!connector->encoder)
885 continue;
886
Dave Airlief453ba02008-11-07 14:05:41 -0800887 if (connector->encoder->crtc == set->crtc)
888 new_crtc = NULL;
889 else
890 new_crtc = connector->encoder->crtc;
891
892 for (ro = 0; ro < set->num_connectors; ro++) {
893 if (set->connectors[ro] == connector)
894 new_crtc = set->crtc;
895 }
Jesse Barnes7bec7562009-02-23 16:09:34 -0800896
897 /* Make sure the new CRTC will work with the encoder */
898 if (new_crtc &&
899 !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
900 ret = -EINVAL;
Maarten Maathuise67aae72009-08-27 10:18:29 +0200901 goto fail;
Jesse Barnes7bec7562009-02-23 16:09:34 -0800902 }
Dave Airlief453ba02008-11-07 14:05:41 -0800903 if (new_crtc != connector->encoder->crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800904 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
Jesse Barnes712531b2009-01-09 13:56:14 -0800905 mode_changed = true;
Dave Airlief453ba02008-11-07 14:05:41 -0800906 connector->encoder->crtc = new_crtc;
907 }
Zhao Yakui58367ed2009-07-20 13:48:07 +0800908 DRM_DEBUG_KMS("setting connector %d crtc to %p\n",
Jesse Barnes7bec7562009-02-23 16:09:34 -0800909 connector->base.id, new_crtc);
Dave Airlief453ba02008-11-07 14:05:41 -0800910 }
911
912 /* mode_set_base is not a required function */
Jesse Barnes712531b2009-01-09 13:56:14 -0800913 if (fb_changed && !crtc_funcs->mode_set_base)
914 mode_changed = true;
Dave Airlief453ba02008-11-07 14:05:41 -0800915
Jesse Barnes712531b2009-01-09 13:56:14 -0800916 if (mode_changed) {
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -0500917 old_fb = set->crtc->fb;
Dave Airlief453ba02008-11-07 14:05:41 -0800918 set->crtc->fb = set->fb;
919 set->crtc->enabled = (set->mode != NULL);
920 if (set->mode != NULL) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800921 DRM_DEBUG_KMS("attempting to set mode from"
922 " userspace\n");
Dave Airlief453ba02008-11-07 14:05:41 -0800923 drm_mode_debug_printmodeline(set->mode);
924 if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -0500925 set->x, set->y,
926 old_fb)) {
Chris Wilson5c3b82e2009-02-11 13:25:09 +0000927 DRM_ERROR("failed to set mode on crtc %p\n",
928 set->crtc);
Dave Airlief453ba02008-11-07 14:05:41 -0800929 ret = -EINVAL;
Maarten Maathuise67aae72009-08-27 10:18:29 +0200930 goto fail;
Dave Airlief453ba02008-11-07 14:05:41 -0800931 }
932 /* TODO are these needed? */
933 set->crtc->desired_x = set->x;
934 set->crtc->desired_y = set->y;
935 set->crtc->desired_mode = set->mode;
936 }
937 drm_helper_disable_unused_functions(dev);
Jesse Barnes712531b2009-01-09 13:56:14 -0800938 } else if (fb_changed) {
Ben Skeggs9b1596a2009-09-18 10:43:52 +1000939 set->crtc->x = set->x;
940 set->crtc->y = set->y;
941
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -0500942 old_fb = set->crtc->fb;
Dave Airlief453ba02008-11-07 14:05:41 -0800943 if (set->crtc->fb != set->fb)
944 set->crtc->fb = set->fb;
Chris Wilson5c3b82e2009-02-11 13:25:09 +0000945 ret = crtc_funcs->mode_set_base(set->crtc,
946 set->x, set->y, old_fb);
947 if (ret != 0)
Maarten Maathuise67aae72009-08-27 10:18:29 +0200948 goto fail;
Dave Airlief453ba02008-11-07 14:05:41 -0800949 }
950
Maarten Maathuise67aae72009-08-27 10:18:29 +0200951 kfree(save_connectors);
Dave Airlief453ba02008-11-07 14:05:41 -0800952 kfree(save_encoders);
953 kfree(save_crtcs);
954 return 0;
955
Maarten Maathuise67aae72009-08-27 10:18:29 +0200956fail:
957 /* Restore all previous data. */
Dave Airlief453ba02008-11-07 14:05:41 -0800958 count = 0;
Maarten Maathuise67aae72009-08-27 10:18:29 +0200959 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
960 *crtc = save_crtcs[count++];
961 }
Chris Wilsone62fb642009-02-11 16:39:21 +0000962
Maarten Maathuise67aae72009-08-27 10:18:29 +0200963 count = 0;
964 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
965 *encoder = save_encoders[count++];
Chris Wilsone62fb642009-02-11 16:39:21 +0000966 }
Maarten Maathuise67aae72009-08-27 10:18:29 +0200967
Dave Airlief453ba02008-11-07 14:05:41 -0800968 count = 0;
969 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Maarten Maathuise67aae72009-08-27 10:18:29 +0200970 *connector = save_connectors[count++];
Dave Airlief453ba02008-11-07 14:05:41 -0800971 }
Maarten Maathuise67aae72009-08-27 10:18:29 +0200972
973 kfree(save_connectors);
Dave Airlief453ba02008-11-07 14:05:41 -0800974 kfree(save_encoders);
Maarten Maathuise67aae72009-08-27 10:18:29 +0200975 kfree(save_crtcs);
Dave Airlief453ba02008-11-07 14:05:41 -0800976 return ret;
977}
978EXPORT_SYMBOL(drm_crtc_helper_set_config);
979
980bool drm_helper_plugged_event(struct drm_device *dev)
981{
Zhao Yakui58367ed2009-07-20 13:48:07 +0800982 DRM_DEBUG_KMS("\n");
Dave Airlief453ba02008-11-07 14:05:41 -0800983
984 drm_helper_probe_connector_modes(dev, dev->mode_config.max_width,
985 dev->mode_config.max_height);
986
987 drm_setup_crtcs(dev);
988
989 /* alert the driver fb layer */
990 dev->mode_config.funcs->fb_changed(dev);
991
992 /* FIXME: send hotplug event */
993 return true;
994}
995/**
996 * drm_initial_config - setup a sane initial connector configuration
997 * @dev: DRM device
Dave Airlief453ba02008-11-07 14:05:41 -0800998 *
999 * LOCKING:
1000 * Called at init time, must take mode config lock.
1001 *
1002 * Scan the CRTCs and connectors and try to put together an initial setup.
1003 * At the moment, this is a cloned configuration across all heads with
1004 * a new framebuffer object as the backing store.
1005 *
1006 * RETURNS:
1007 * Zero if everything went ok, nonzero otherwise.
1008 */
Jesse Barnes7a1fb5d2009-03-27 13:05:19 -07001009bool drm_helper_initial_config(struct drm_device *dev)
Dave Airlief453ba02008-11-07 14:05:41 -08001010{
Jesse Barnes40a518d2009-01-12 12:05:32 -08001011 int count = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001012
Dave Airlied50ba252009-09-23 14:44:08 +10001013 drm_fb_helper_parse_command_line(dev);
1014
Jesse Barnes40a518d2009-01-12 12:05:32 -08001015 count = drm_helper_probe_connector_modes(dev,
1016 dev->mode_config.max_width,
1017 dev->mode_config.max_height);
1018
1019 /*
Dave Airlie575dc342009-09-07 18:43:26 +10001020 * we shouldn't end up with no modes here.
Jesse Barnes40a518d2009-01-12 12:05:32 -08001021 */
Dave Airlied50ba252009-09-23 14:44:08 +10001022 WARN(!count, "No connectors reported connected with modes\n");
Jesse Barnes40a518d2009-01-12 12:05:32 -08001023
1024 drm_setup_crtcs(dev);
1025
1026 /* alert the driver fb layer */
1027 dev->mode_config.funcs->fb_changed(dev);
1028
1029 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001030}
1031EXPORT_SYMBOL(drm_helper_initial_config);
1032
Keith Packardc9fb15f2009-05-30 20:42:28 -07001033static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder)
1034{
1035 int dpms = DRM_MODE_DPMS_OFF;
1036 struct drm_connector *connector;
1037 struct drm_device *dev = encoder->dev;
1038
1039 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1040 if (connector->encoder == encoder)
1041 if (connector->dpms < dpms)
1042 dpms = connector->dpms;
1043 return dpms;
1044}
1045
1046static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc)
1047{
1048 int dpms = DRM_MODE_DPMS_OFF;
1049 struct drm_connector *connector;
1050 struct drm_device *dev = crtc->dev;
1051
1052 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1053 if (connector->encoder && connector->encoder->crtc == crtc)
1054 if (connector->dpms < dpms)
1055 dpms = connector->dpms;
1056 return dpms;
1057}
1058
1059/**
1060 * drm_helper_connector_dpms
1061 * @connector affected connector
1062 * @mode DPMS mode
1063 *
1064 * Calls the low-level connector DPMS function, then
1065 * calls appropriate encoder and crtc DPMS functions as well
1066 */
1067void drm_helper_connector_dpms(struct drm_connector *connector, int mode)
1068{
1069 struct drm_encoder *encoder = connector->encoder;
1070 struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
1071 int old_dpms;
1072
1073 if (mode == connector->dpms)
1074 return;
1075
1076 old_dpms = connector->dpms;
1077 connector->dpms = mode;
1078
1079 /* from off to on, do crtc then encoder */
1080 if (mode < old_dpms) {
1081 if (crtc) {
1082 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
1083 if (crtc_funcs->dpms)
1084 (*crtc_funcs->dpms) (crtc,
1085 drm_helper_choose_crtc_dpms(crtc));
1086 }
1087 if (encoder) {
1088 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
1089 if (encoder_funcs->dpms)
1090 (*encoder_funcs->dpms) (encoder,
1091 drm_helper_choose_encoder_dpms(encoder));
1092 }
1093 }
1094
1095 /* from on to off, do encoder then crtc */
1096 if (mode > old_dpms) {
1097 if (encoder) {
1098 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
1099 if (encoder_funcs->dpms)
1100 (*encoder_funcs->dpms) (encoder,
1101 drm_helper_choose_encoder_dpms(encoder));
1102 }
1103 if (crtc) {
1104 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
1105 if (crtc_funcs->dpms)
1106 (*crtc_funcs->dpms) (crtc,
1107 drm_helper_choose_crtc_dpms(crtc));
1108 }
1109 }
1110
1111 return;
1112}
1113EXPORT_SYMBOL(drm_helper_connector_dpms);
1114
Dave Airlief453ba02008-11-07 14:05:41 -08001115/**
1116 * drm_hotplug_stage_two
1117 * @dev DRM device
1118 * @connector hotpluged connector
1119 *
1120 * LOCKING.
1121 * Caller must hold mode config lock, function might grab struct lock.
1122 *
1123 * Stage two of a hotplug.
1124 *
1125 * RETURNS:
1126 * Zero on success, errno on failure.
1127 */
1128int drm_helper_hotplug_stage_two(struct drm_device *dev)
1129{
Dave Airlief453ba02008-11-07 14:05:41 -08001130 drm_helper_plugged_event(dev);
1131
1132 return 0;
1133}
1134EXPORT_SYMBOL(drm_helper_hotplug_stage_two);
1135
1136int drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb,
1137 struct drm_mode_fb_cmd *mode_cmd)
1138{
1139 fb->width = mode_cmd->width;
1140 fb->height = mode_cmd->height;
1141 fb->pitch = mode_cmd->pitch;
1142 fb->bits_per_pixel = mode_cmd->bpp;
1143 fb->depth = mode_cmd->depth;
1144
1145 return 0;
1146}
1147EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
1148
1149int drm_helper_resume_force_mode(struct drm_device *dev)
1150{
1151 struct drm_crtc *crtc;
1152 int ret;
1153
1154 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1155
1156 if (!crtc->enabled)
1157 continue;
1158
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05001159 ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
1160 crtc->x, crtc->y, crtc->fb);
Dave Airlief453ba02008-11-07 14:05:41 -08001161
1162 if (ret == false)
1163 DRM_ERROR("failed to set mode on crtc %p\n", crtc);
1164 }
Zhao Yakuiaf4fcb52009-07-08 14:13:13 +08001165 /* disable the unused connectors while restoring the modesetting */
1166 drm_helper_disable_unused_functions(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001167 return 0;
1168}
1169EXPORT_SYMBOL(drm_helper_resume_force_mode);