blob: 51103aa469f8c1bf773859e6176a004076a70e9a [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));
Zhao Yakui72539832010-03-04 08:25:55 +0000107 drm_mode_connector_update_edid_property(connector, NULL);
Adam Jackson620f3782009-09-08 11:51:46 +1000108 goto prune;
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) {
Adam Jackson9632b412009-11-23 14:23:07 -0500113 count = drm_add_modes_noedid(connector, 1024, 768);
ykzhao50fe4cf2009-09-03 14:30:04 +0800114 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
Adam Jackson620f3782009-09-08 11:51:46 +1000136prune:
Dave Airlief453ba02008-11-07 14:05:41 -0800137 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
Dave Airlief453ba02008-11-07 14:05:41 -0800172/**
Keith Packardc9fb15f2009-05-30 20:42:28 -0700173 * drm_helper_encoder_in_use - check if a given encoder is in use
174 * @encoder: encoder to check
175 *
176 * LOCKING:
177 * Caller must hold mode config lock.
178 *
179 * Walk @encoders's DRM device's mode_config and see if it's in use.
180 *
181 * RETURNS:
182 * True if @encoder is part of the mode_config, false otherwise.
183 */
184bool drm_helper_encoder_in_use(struct drm_encoder *encoder)
185{
186 struct drm_connector *connector;
187 struct drm_device *dev = encoder->dev;
188 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
189 if (connector->encoder == encoder)
190 return true;
191 return false;
192}
193EXPORT_SYMBOL(drm_helper_encoder_in_use);
194
195/**
Dave Airlief453ba02008-11-07 14:05:41 -0800196 * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config
197 * @crtc: CRTC to check
198 *
199 * LOCKING:
200 * Caller must hold mode config lock.
201 *
202 * Walk @crtc's DRM device's mode_config and see if it's in use.
203 *
204 * RETURNS:
205 * True if @crtc is part of the mode_config, false otherwise.
206 */
207bool drm_helper_crtc_in_use(struct drm_crtc *crtc)
208{
209 struct drm_encoder *encoder;
210 struct drm_device *dev = crtc->dev;
211 /* FIXME: Locking around list access? */
212 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
Keith Packardc9fb15f2009-05-30 20:42:28 -0700213 if (encoder->crtc == crtc && drm_helper_encoder_in_use(encoder))
Dave Airlief453ba02008-11-07 14:05:41 -0800214 return true;
215 return false;
216}
217EXPORT_SYMBOL(drm_helper_crtc_in_use);
218
219/**
David John89347bb2009-12-31 12:00:46 +0530220 * drm_helper_disable_unused_functions - disable unused objects
Dave Airlief453ba02008-11-07 14:05:41 -0800221 * @dev: DRM device
222 *
223 * LOCKING:
224 * Caller must hold mode config lock.
225 *
226 * If an connector or CRTC isn't part of @dev's mode_config, it can be disabled
227 * by calling its dpms function, which should power it off.
228 */
229void drm_helper_disable_unused_functions(struct drm_device *dev)
230{
231 struct drm_encoder *encoder;
Dave Airliea3a05442009-08-31 15:16:30 +1000232 struct drm_connector *connector;
Dave Airlief453ba02008-11-07 14:05:41 -0800233 struct drm_encoder_helper_funcs *encoder_funcs;
234 struct drm_crtc *crtc;
235
Dave Airliea3a05442009-08-31 15:16:30 +1000236 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
237 if (!connector->encoder)
238 continue;
239 if (connector->status == connector_status_disconnected)
240 connector->encoder = NULL;
241 }
242
Dave Airlief453ba02008-11-07 14:05:41 -0800243 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
244 encoder_funcs = encoder->helper_private;
Dave Airliea3a05442009-08-31 15:16:30 +1000245 if (!drm_helper_encoder_in_use(encoder)) {
246 if (encoder_funcs->disable)
247 (*encoder_funcs->disable)(encoder);
248 else
249 (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
Dave Airlie9c552dd2009-09-02 14:00:11 +1000250 /* disconnector encoder from any connector */
251 encoder->crtc = NULL;
Dave Airliea3a05442009-08-31 15:16:30 +1000252 }
Dave Airlief453ba02008-11-07 14:05:41 -0800253 }
254
255 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
256 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
257 crtc->enabled = drm_helper_crtc_in_use(crtc);
258 if (!crtc->enabled) {
259 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
260 crtc->fb = NULL;
261 }
262 }
263}
264EXPORT_SYMBOL(drm_helper_disable_unused_functions);
265
266static struct drm_display_mode *drm_has_preferred_mode(struct drm_connector *connector, int width, int height)
267{
268 struct drm_display_mode *mode;
269
270 list_for_each_entry(mode, &connector->modes, head) {
271 if (drm_mode_width(mode) > width ||
272 drm_mode_height(mode) > height)
273 continue;
274 if (mode->type & DRM_MODE_TYPE_PREFERRED)
275 return mode;
276 }
277 return NULL;
278}
279
Dave Airlied50ba252009-09-23 14:44:08 +1000280static bool drm_has_cmdline_mode(struct drm_connector *connector)
281{
282 struct drm_fb_helper_connector *fb_help_conn = connector->fb_helper_private;
Dave Airlie8ef86782009-09-26 06:39:00 +1000283 struct drm_fb_helper_cmdline_mode *cmdline_mode;
284
285 if (!fb_help_conn)
286 return false;
287
288 cmdline_mode = &fb_help_conn->cmdline_mode;
Dave Airlied50ba252009-09-23 14:44:08 +1000289 return cmdline_mode->specified;
290}
291
292static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_connector *connector, int width, int height)
293{
294 struct drm_fb_helper_connector *fb_help_conn = connector->fb_helper_private;
Dave Airlie8ef86782009-09-26 06:39:00 +1000295 struct drm_fb_helper_cmdline_mode *cmdline_mode;
Dave Airlied50ba252009-09-23 14:44:08 +1000296 struct drm_display_mode *mode = NULL;
297
Dave Airlie8ef86782009-09-26 06:39:00 +1000298 if (!fb_help_conn)
299 return mode;
300
301 cmdline_mode = &fb_help_conn->cmdline_mode;
Dave Airlied50ba252009-09-23 14:44:08 +1000302 if (cmdline_mode->specified == false)
303 return mode;
304
305 /* attempt to find a matching mode in the list of modes
306 * we have gotten so far, if not add a CVT mode that conforms
307 */
308 if (cmdline_mode->rb || cmdline_mode->margins)
309 goto create_mode;
310
311 list_for_each_entry(mode, &connector->modes, head) {
312 /* check width/height */
313 if (mode->hdisplay != cmdline_mode->xres ||
314 mode->vdisplay != cmdline_mode->yres)
315 continue;
316
317 if (cmdline_mode->refresh_specified) {
318 if (mode->vrefresh != cmdline_mode->refresh)
319 continue;
320 }
321
322 if (cmdline_mode->interlace) {
323 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
324 continue;
325 }
326 return mode;
327 }
328
329create_mode:
330 mode = drm_cvt_mode(connector->dev, cmdline_mode->xres,
331 cmdline_mode->yres,
332 cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
333 cmdline_mode->rb, cmdline_mode->interlace,
334 cmdline_mode->margins);
Francisco Jerezeeba5752009-11-07 16:27:32 +0100335 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Dave Airlied50ba252009-09-23 14:44:08 +1000336 list_add(&mode->head, &connector->modes);
337 return mode;
338}
339
Dave Airlief453ba02008-11-07 14:05:41 -0800340static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
341{
342 bool enable;
343
344 if (strict) {
345 enable = connector->status == connector_status_connected;
346 } else {
347 enable = connector->status != connector_status_disconnected;
348 }
349 return enable;
350}
351
352static void drm_enable_connectors(struct drm_device *dev, bool *enabled)
353{
354 bool any_enabled = false;
355 struct drm_connector *connector;
356 int i = 0;
357
358 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
359 enabled[i] = drm_connector_enabled(connector, true);
Zhao Yakui58367ed2009-07-20 13:48:07 +0800360 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
Jesse Barnes40a518d2009-01-12 12:05:32 -0800361 enabled[i] ? "yes" : "no");
Dave Airlief453ba02008-11-07 14:05:41 -0800362 any_enabled |= enabled[i];
363 i++;
364 }
365
366 if (any_enabled)
367 return;
368
369 i = 0;
370 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
371 enabled[i] = drm_connector_enabled(connector, false);
372 i++;
373 }
374}
375
376static bool drm_target_preferred(struct drm_device *dev,
377 struct drm_display_mode **modes,
378 bool *enabled, int width, int height)
379{
380 struct drm_connector *connector;
381 int i = 0;
382
383 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
384
385 if (enabled[i] == false) {
386 i++;
387 continue;
388 }
389
Dave Airlied50ba252009-09-23 14:44:08 +1000390 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
391 connector->base.id);
Jesse Barnes40a518d2009-01-12 12:05:32 -0800392
Dave Airlied50ba252009-09-23 14:44:08 +1000393 /* got for command line mode first */
394 modes[i] = drm_pick_cmdline_mode(connector, width, height);
395 if (!modes[i]) {
396 DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
397 connector->base.id);
398 modes[i] = drm_has_preferred_mode(connector, width, height);
399 }
Jesse Barnes40a518d2009-01-12 12:05:32 -0800400 /* No preferred modes, pick one off the list */
401 if (!modes[i] && !list_empty(&connector->modes)) {
Dave Airlief453ba02008-11-07 14:05:41 -0800402 list_for_each_entry(modes[i], &connector->modes, head)
403 break;
404 }
Zhao Yakui58367ed2009-07-20 13:48:07 +0800405 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
Jesse Barnes40a518d2009-01-12 12:05:32 -0800406 "none");
Dave Airlief453ba02008-11-07 14:05:41 -0800407 i++;
408 }
409 return true;
410}
411
412static int drm_pick_crtcs(struct drm_device *dev,
413 struct drm_crtc **best_crtcs,
414 struct drm_display_mode **modes,
415 int n, int width, int height)
416{
417 int c, o;
418 struct drm_connector *connector;
419 struct drm_connector_helper_funcs *connector_funcs;
420 struct drm_encoder *encoder;
421 struct drm_crtc *best_crtc;
422 int my_score, best_score, score;
423 struct drm_crtc **crtcs, *crtc;
424
425 if (n == dev->mode_config.num_connector)
426 return 0;
427 c = 0;
428 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
429 if (c == n)
430 break;
431 c++;
432 }
433
434 best_crtcs[n] = NULL;
435 best_crtc = NULL;
436 best_score = drm_pick_crtcs(dev, best_crtcs, modes, n+1, width, height);
437 if (modes[n] == NULL)
438 return best_score;
439
440 crtcs = kmalloc(dev->mode_config.num_connector *
441 sizeof(struct drm_crtc *), GFP_KERNEL);
442 if (!crtcs)
443 return best_score;
444
445 my_score = 1;
446 if (connector->status == connector_status_connected)
447 my_score++;
Dave Airlied50ba252009-09-23 14:44:08 +1000448 if (drm_has_cmdline_mode(connector))
449 my_score++;
Dave Airlief453ba02008-11-07 14:05:41 -0800450 if (drm_has_preferred_mode(connector, width, height))
451 my_score++;
452
453 connector_funcs = connector->helper_private;
454 encoder = connector_funcs->best_encoder(connector);
455 if (!encoder)
456 goto out;
457
458 connector->encoder = encoder;
459
460 /* select a crtc for this connector and then attempt to configure
461 remaining connectors */
462 c = 0;
463 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
464
Dave Airliea3a05442009-08-31 15:16:30 +1000465 if ((encoder->possible_crtcs & (1 << c)) == 0) {
Dave Airlief453ba02008-11-07 14:05:41 -0800466 c++;
467 continue;
468 }
469
470 for (o = 0; o < n; o++)
471 if (best_crtcs[o] == crtc)
472 break;
473
474 if (o < n) {
475 /* ignore cloning for now */
476 c++;
477 continue;
478 }
479
480 crtcs[n] = crtc;
481 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_crtc *));
482 score = my_score + drm_pick_crtcs(dev, crtcs, modes, n + 1,
483 width, height);
484 if (score > best_score) {
485 best_crtc = crtc;
486 best_score = score;
487 memcpy(best_crtcs, crtcs,
488 dev->mode_config.num_connector *
489 sizeof(struct drm_crtc *));
490 }
491 c++;
492 }
493out:
494 kfree(crtcs);
495 return best_score;
496}
497
498static void drm_setup_crtcs(struct drm_device *dev)
499{
500 struct drm_crtc **crtcs;
501 struct drm_display_mode **modes;
502 struct drm_encoder *encoder;
503 struct drm_connector *connector;
504 bool *enabled;
505 int width, height;
506 int i, ret;
507
Zhao Yakui58367ed2009-07-20 13:48:07 +0800508 DRM_DEBUG_KMS("\n");
Jesse Barnes40a518d2009-01-12 12:05:32 -0800509
Dave Airlief453ba02008-11-07 14:05:41 -0800510 width = dev->mode_config.max_width;
511 height = dev->mode_config.max_height;
512
513 /* clean out all the encoder/crtc combos */
514 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
515 encoder->crtc = NULL;
516 }
517
518 crtcs = kcalloc(dev->mode_config.num_connector,
519 sizeof(struct drm_crtc *), GFP_KERNEL);
520 modes = kcalloc(dev->mode_config.num_connector,
521 sizeof(struct drm_display_mode *), GFP_KERNEL);
522 enabled = kcalloc(dev->mode_config.num_connector,
523 sizeof(bool), GFP_KERNEL);
524
525 drm_enable_connectors(dev, enabled);
526
527 ret = drm_target_preferred(dev, modes, enabled, width, height);
528 if (!ret)
529 DRM_ERROR("Unable to find initial modes\n");
530
Zhao Yakui58367ed2009-07-20 13:48:07 +0800531 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
Jesse Barnes40a518d2009-01-12 12:05:32 -0800532
Dave Airlief453ba02008-11-07 14:05:41 -0800533 drm_pick_crtcs(dev, crtcs, modes, 0, width, height);
534
535 i = 0;
536 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
537 struct drm_display_mode *mode = modes[i];
538 struct drm_crtc *crtc = crtcs[i];
539
540 if (connector->encoder == NULL) {
541 i++;
542 continue;
543 }
544
545 if (mode && crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800546 DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
Jesse Barnes40a518d2009-01-12 12:05:32 -0800547 mode->name, crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -0800548 crtc->desired_mode = mode;
549 connector->encoder->crtc = crtc;
Dave Airliea3a05442009-08-31 15:16:30 +1000550 } else {
Dave Airlief453ba02008-11-07 14:05:41 -0800551 connector->encoder->crtc = NULL;
Dave Airliea3a05442009-08-31 15:16:30 +1000552 connector->encoder = NULL;
553 }
Dave Airlief453ba02008-11-07 14:05:41 -0800554 i++;
555 }
556
557 kfree(crtcs);
558 kfree(modes);
559 kfree(enabled);
560}
Jesse Barnes7bec7562009-02-23 16:09:34 -0800561
562/**
563 * drm_encoder_crtc_ok - can a given crtc drive a given encoder?
564 * @encoder: encoder to test
565 * @crtc: crtc to test
566 *
567 * Return false if @encoder can't be driven by @crtc, true otherwise.
568 */
569static bool drm_encoder_crtc_ok(struct drm_encoder *encoder,
570 struct drm_crtc *crtc)
571{
572 struct drm_device *dev;
573 struct drm_crtc *tmp;
574 int crtc_mask = 1;
575
576 WARN(!crtc, "checking null crtc?");
577
578 dev = crtc->dev;
579
580 list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
581 if (tmp == crtc)
582 break;
583 crtc_mask <<= 1;
584 }
585
586 if (encoder->possible_crtcs & crtc_mask)
587 return true;
588 return false;
589}
590
591/*
592 * Check the CRTC we're going to map each output to vs. its current
593 * CRTC. If they don't match, we have to disable the output and the CRTC
594 * since the driver will have to re-route things.
595 */
596static void
597drm_crtc_prepare_encoders(struct drm_device *dev)
598{
599 struct drm_encoder_helper_funcs *encoder_funcs;
600 struct drm_encoder *encoder;
601
602 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
603 encoder_funcs = encoder->helper_private;
604 /* Disable unused encoders */
605 if (encoder->crtc == NULL)
606 (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
607 /* Disable encoders whose CRTC is about to change */
608 if (encoder_funcs->get_crtc &&
609 encoder->crtc != (*encoder_funcs->get_crtc)(encoder))
610 (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
611 }
612}
613
Dave Airlief453ba02008-11-07 14:05:41 -0800614/**
615 * drm_crtc_set_mode - set a mode
616 * @crtc: CRTC to program
617 * @mode: mode to use
618 * @x: width of mode
619 * @y: height of mode
620 *
621 * LOCKING:
622 * Caller must hold mode config lock.
623 *
624 * Try to set @mode on @crtc. Give @crtc and its associated connectors a chance
625 * to fixup or reject the mode prior to trying to set it.
626 *
627 * RETURNS:
628 * True if the mode was set successfully, or false otherwise.
629 */
630bool drm_crtc_helper_set_mode(struct drm_crtc *crtc,
631 struct drm_display_mode *mode,
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -0500632 int x, int y,
633 struct drm_framebuffer *old_fb)
Dave Airlief453ba02008-11-07 14:05:41 -0800634{
635 struct drm_device *dev = crtc->dev;
636 struct drm_display_mode *adjusted_mode, saved_mode;
637 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
638 struct drm_encoder_helper_funcs *encoder_funcs;
639 int saved_x, saved_y;
640 struct drm_encoder *encoder;
641 bool ret = true;
642
643 adjusted_mode = drm_mode_duplicate(dev, mode);
644
645 crtc->enabled = drm_helper_crtc_in_use(crtc);
646
647 if (!crtc->enabled)
648 return true;
649
650 saved_mode = crtc->mode;
651 saved_x = crtc->x;
652 saved_y = crtc->y;
653
654 /* Update crtc values up front so the driver can rely on them for mode
655 * setting.
656 */
657 crtc->mode = *mode;
658 crtc->x = x;
659 crtc->y = y;
660
Dave Airlief453ba02008-11-07 14:05:41 -0800661 /* Pass our mode to the connectors and the CRTC to give them a chance to
662 * adjust it according to limitations or connector properties, and also
663 * a chance to reject the mode entirely.
664 */
665 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
666
667 if (encoder->crtc != crtc)
668 continue;
669 encoder_funcs = encoder->helper_private;
670 if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
671 adjusted_mode))) {
672 goto done;
673 }
674 }
675
676 if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) {
677 goto done;
678 }
679
680 /* Prepare the encoders and CRTCs before setting the mode. */
681 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
682
683 if (encoder->crtc != crtc)
684 continue;
685 encoder_funcs = encoder->helper_private;
686 /* Disable the encoders as the first thing we do. */
687 encoder_funcs->prepare(encoder);
688 }
689
Jesse Barnes7bec7562009-02-23 16:09:34 -0800690 drm_crtc_prepare_encoders(dev);
691
Dave Airlief453ba02008-11-07 14:05:41 -0800692 crtc_funcs->prepare(crtc);
693
694 /* Set up the DPLL and any encoders state that needs to adjust or depend
695 * on the DPLL.
696 */
Chris Wilson5c3b82e2009-02-11 13:25:09 +0000697 ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
698 if (!ret)
699 goto done;
Dave Airlief453ba02008-11-07 14:05:41 -0800700
701 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
702
703 if (encoder->crtc != crtc)
704 continue;
705
Dave Youngef145872010-01-13 13:38:59 +0800706 DRM_DEBUG("%s: set mode %s %x\n", drm_get_encoder_name(encoder),
Dave Airlief453ba02008-11-07 14:05:41 -0800707 mode->name, mode->base.id);
708 encoder_funcs = encoder->helper_private;
709 encoder_funcs->mode_set(encoder, mode, adjusted_mode);
710 }
711
712 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
713 crtc_funcs->commit(crtc);
714
715 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
716
717 if (encoder->crtc != crtc)
718 continue;
719
720 encoder_funcs = encoder->helper_private;
721 encoder_funcs->commit(encoder);
722
723 }
724
725 /* XXX free adjustedmode */
726 drm_mode_destroy(dev, adjusted_mode);
727 /* FIXME: add subpixel order */
728done:
729 if (!ret) {
730 crtc->mode = saved_mode;
731 crtc->x = saved_x;
732 crtc->y = saved_y;
733 }
734
735 return ret;
736}
737EXPORT_SYMBOL(drm_crtc_helper_set_mode);
738
739
740/**
741 * drm_crtc_helper_set_config - set a new config from userspace
742 * @crtc: CRTC to setup
743 * @crtc_info: user provided configuration
744 * @new_mode: new mode to set
745 * @connector_set: set of connectors for the new config
746 * @fb: new framebuffer
747 *
748 * LOCKING:
749 * Caller must hold mode config lock.
750 *
751 * Setup a new configuration, provided by the user in @crtc_info, and enable
752 * it.
753 *
754 * RETURNS:
755 * Zero. (FIXME)
756 */
757int drm_crtc_helper_set_config(struct drm_mode_set *set)
758{
759 struct drm_device *dev;
Maarten Maathuise67aae72009-08-27 10:18:29 +0200760 struct drm_crtc *save_crtcs, *new_crtc, *crtc;
761 struct drm_encoder *save_encoders, *new_encoder, *encoder;
Jesse Barnes7bec7562009-02-23 16:09:34 -0800762 struct drm_framebuffer *old_fb = NULL;
Jakob Bornecrantz4cb72b12009-08-03 13:43:59 +0100763 bool mode_changed = false; /* if true do a full mode set */
764 bool fb_changed = false; /* if true and !mode_changed just do a flip */
Maarten Maathuise67aae72009-08-27 10:18:29 +0200765 struct drm_connector *save_connectors, *connector;
Dave Airlief453ba02008-11-07 14:05:41 -0800766 int count = 0, ro, fail = 0;
767 struct drm_crtc_helper_funcs *crtc_funcs;
768 int ret = 0;
769
Zhao Yakui58367ed2009-07-20 13:48:07 +0800770 DRM_DEBUG_KMS("\n");
Dave Airlief453ba02008-11-07 14:05:41 -0800771
772 if (!set)
773 return -EINVAL;
774
775 if (!set->crtc)
776 return -EINVAL;
777
778 if (!set->crtc->helper_private)
779 return -EINVAL;
780
781 crtc_funcs = set->crtc->helper_private;
782
Zhao Yakui58367ed2009-07-20 13:48:07 +0800783 DRM_DEBUG_KMS("crtc: %p %d fb: %p connectors: %p num_connectors:"
784 " %d (x, y) (%i, %i)\n",
Dave Airlief453ba02008-11-07 14:05:41 -0800785 set->crtc, set->crtc->base.id, set->fb, set->connectors,
786 (int)set->num_connectors, set->x, set->y);
787
788 dev = set->crtc->dev;
789
Maarten Maathuise67aae72009-08-27 10:18:29 +0200790 /* Allocate space for the backup of all (non-pointer) crtc, encoder and
791 * connector data. */
792 save_crtcs = kzalloc(dev->mode_config.num_crtc *
793 sizeof(struct drm_crtc), GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -0800794 if (!save_crtcs)
795 return -ENOMEM;
796
Maarten Maathuise67aae72009-08-27 10:18:29 +0200797 save_encoders = kzalloc(dev->mode_config.num_encoder *
798 sizeof(struct drm_encoder), GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -0800799 if (!save_encoders) {
800 kfree(save_crtcs);
801 return -ENOMEM;
802 }
803
Maarten Maathuise67aae72009-08-27 10:18:29 +0200804 save_connectors = kzalloc(dev->mode_config.num_connector *
805 sizeof(struct drm_connector), GFP_KERNEL);
806 if (!save_connectors) {
807 kfree(save_crtcs);
808 kfree(save_encoders);
809 return -ENOMEM;
810 }
811
812 /* Copy data. Note that driver private data is not affected.
813 * Should anything bad happen only the expected state is
814 * restored, not the drivers personal bookkeeping.
815 */
816 count = 0;
817 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
818 save_crtcs[count++] = *crtc;
819 }
820
821 count = 0;
822 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
823 save_encoders[count++] = *encoder;
824 }
825
826 count = 0;
827 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
828 save_connectors[count++] = *connector;
829 }
830
Dave Airlief453ba02008-11-07 14:05:41 -0800831 /* We should be able to check here if the fb has the same properties
832 * and then just flip_or_move it */
833 if (set->crtc->fb != set->fb) {
Jesse Barnes712531b2009-01-09 13:56:14 -0800834 /* If we have no fb then treat it as a full mode set */
Jesse Barnes7bec7562009-02-23 16:09:34 -0800835 if (set->crtc->fb == NULL) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800836 DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
Jesse Barnes712531b2009-01-09 13:56:14 -0800837 mode_changed = true;
Jakob Bornecrantz4cb72b12009-08-03 13:43:59 +0100838 } else if (set->fb == NULL) {
839 mode_changed = true;
Dave Airlie8dff4742010-02-11 14:28:58 +1000840 } else
Jesse Barnes712531b2009-01-09 13:56:14 -0800841 fb_changed = true;
Dave Airlief453ba02008-11-07 14:05:41 -0800842 }
843
844 if (set->x != set->crtc->x || set->y != set->crtc->y)
Jesse Barnes712531b2009-01-09 13:56:14 -0800845 fb_changed = true;
Dave Airlief453ba02008-11-07 14:05:41 -0800846
847 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800848 DRM_DEBUG_KMS("modes are different, full mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -0800849 drm_mode_debug_printmodeline(&set->crtc->mode);
850 drm_mode_debug_printmodeline(set->mode);
Jesse Barnes712531b2009-01-09 13:56:14 -0800851 mode_changed = true;
Dave Airlief453ba02008-11-07 14:05:41 -0800852 }
853
854 /* a) traverse passed in connector list and get encoders for them */
855 count = 0;
856 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
857 struct drm_connector_helper_funcs *connector_funcs =
858 connector->helper_private;
Dave Airlief453ba02008-11-07 14:05:41 -0800859 new_encoder = connector->encoder;
860 for (ro = 0; ro < set->num_connectors; ro++) {
861 if (set->connectors[ro] == connector) {
862 new_encoder = connector_funcs->best_encoder(connector);
863 /* if we can't get an encoder for a connector
864 we are setting now - then fail */
865 if (new_encoder == NULL)
866 /* don't break so fail path works correct */
867 fail = 1;
868 break;
869 }
870 }
871
872 if (new_encoder != connector->encoder) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800873 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
Jesse Barnes712531b2009-01-09 13:56:14 -0800874 mode_changed = true;
Maarten Maathuisff846ab2009-08-19 00:56:45 +0200875 /* If the encoder is reused for another connector, then
876 * the appropriate crtc will be set later.
877 */
Maarten Maathuisff6fdbe2009-09-01 03:39:04 +0200878 if (connector->encoder)
879 connector->encoder->crtc = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800880 connector->encoder = new_encoder;
881 }
882 }
883
884 if (fail) {
885 ret = -EINVAL;
Maarten Maathuise67aae72009-08-27 10:18:29 +0200886 goto fail;
Dave Airlief453ba02008-11-07 14:05:41 -0800887 }
888
889 count = 0;
890 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
891 if (!connector->encoder)
892 continue;
893
Dave Airlief453ba02008-11-07 14:05:41 -0800894 if (connector->encoder->crtc == set->crtc)
895 new_crtc = NULL;
896 else
897 new_crtc = connector->encoder->crtc;
898
899 for (ro = 0; ro < set->num_connectors; ro++) {
900 if (set->connectors[ro] == connector)
901 new_crtc = set->crtc;
902 }
Jesse Barnes7bec7562009-02-23 16:09:34 -0800903
904 /* Make sure the new CRTC will work with the encoder */
905 if (new_crtc &&
906 !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
907 ret = -EINVAL;
Maarten Maathuise67aae72009-08-27 10:18:29 +0200908 goto fail;
Jesse Barnes7bec7562009-02-23 16:09:34 -0800909 }
Dave Airlief453ba02008-11-07 14:05:41 -0800910 if (new_crtc != connector->encoder->crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800911 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
Jesse Barnes712531b2009-01-09 13:56:14 -0800912 mode_changed = true;
Dave Airlief453ba02008-11-07 14:05:41 -0800913 connector->encoder->crtc = new_crtc;
914 }
Zhao Yakui58367ed2009-07-20 13:48:07 +0800915 DRM_DEBUG_KMS("setting connector %d crtc to %p\n",
Jesse Barnes7bec7562009-02-23 16:09:34 -0800916 connector->base.id, new_crtc);
Dave Airlief453ba02008-11-07 14:05:41 -0800917 }
918
919 /* mode_set_base is not a required function */
Jesse Barnes712531b2009-01-09 13:56:14 -0800920 if (fb_changed && !crtc_funcs->mode_set_base)
921 mode_changed = true;
Dave Airlief453ba02008-11-07 14:05:41 -0800922
Jesse Barnes712531b2009-01-09 13:56:14 -0800923 if (mode_changed) {
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -0500924 old_fb = set->crtc->fb;
Dave Airlief453ba02008-11-07 14:05:41 -0800925 set->crtc->fb = set->fb;
926 set->crtc->enabled = (set->mode != NULL);
927 if (set->mode != NULL) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800928 DRM_DEBUG_KMS("attempting to set mode from"
929 " userspace\n");
Dave Airlief453ba02008-11-07 14:05:41 -0800930 drm_mode_debug_printmodeline(set->mode);
931 if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -0500932 set->x, set->y,
933 old_fb)) {
Chris Wilson5c3b82e2009-02-11 13:25:09 +0000934 DRM_ERROR("failed to set mode on crtc %p\n",
935 set->crtc);
Dave Airlief453ba02008-11-07 14:05:41 -0800936 ret = -EINVAL;
Maarten Maathuise67aae72009-08-27 10:18:29 +0200937 goto fail;
Dave Airlief453ba02008-11-07 14:05:41 -0800938 }
939 /* TODO are these needed? */
940 set->crtc->desired_x = set->x;
941 set->crtc->desired_y = set->y;
942 set->crtc->desired_mode = set->mode;
943 }
944 drm_helper_disable_unused_functions(dev);
Jesse Barnes712531b2009-01-09 13:56:14 -0800945 } else if (fb_changed) {
Ben Skeggs9b1596a2009-09-18 10:43:52 +1000946 set->crtc->x = set->x;
947 set->crtc->y = set->y;
948
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -0500949 old_fb = set->crtc->fb;
Dave Airlief453ba02008-11-07 14:05:41 -0800950 if (set->crtc->fb != set->fb)
951 set->crtc->fb = set->fb;
Chris Wilson5c3b82e2009-02-11 13:25:09 +0000952 ret = crtc_funcs->mode_set_base(set->crtc,
953 set->x, set->y, old_fb);
954 if (ret != 0)
Maarten Maathuise67aae72009-08-27 10:18:29 +0200955 goto fail;
Dave Airlief453ba02008-11-07 14:05:41 -0800956 }
957
Maarten Maathuise67aae72009-08-27 10:18:29 +0200958 kfree(save_connectors);
Dave Airlief453ba02008-11-07 14:05:41 -0800959 kfree(save_encoders);
960 kfree(save_crtcs);
961 return 0;
962
Maarten Maathuise67aae72009-08-27 10:18:29 +0200963fail:
964 /* Restore all previous data. */
Dave Airlief453ba02008-11-07 14:05:41 -0800965 count = 0;
Maarten Maathuise67aae72009-08-27 10:18:29 +0200966 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
967 *crtc = save_crtcs[count++];
968 }
Chris Wilsone62fb642009-02-11 16:39:21 +0000969
Maarten Maathuise67aae72009-08-27 10:18:29 +0200970 count = 0;
971 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
972 *encoder = save_encoders[count++];
Chris Wilsone62fb642009-02-11 16:39:21 +0000973 }
Maarten Maathuise67aae72009-08-27 10:18:29 +0200974
Dave Airlief453ba02008-11-07 14:05:41 -0800975 count = 0;
976 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Maarten Maathuise67aae72009-08-27 10:18:29 +0200977 *connector = save_connectors[count++];
Dave Airlief453ba02008-11-07 14:05:41 -0800978 }
Maarten Maathuise67aae72009-08-27 10:18:29 +0200979
980 kfree(save_connectors);
Dave Airlief453ba02008-11-07 14:05:41 -0800981 kfree(save_encoders);
Maarten Maathuise67aae72009-08-27 10:18:29 +0200982 kfree(save_crtcs);
Dave Airlief453ba02008-11-07 14:05:41 -0800983 return ret;
984}
985EXPORT_SYMBOL(drm_crtc_helper_set_config);
986
987bool drm_helper_plugged_event(struct drm_device *dev)
988{
Zhao Yakui58367ed2009-07-20 13:48:07 +0800989 DRM_DEBUG_KMS("\n");
Dave Airlief453ba02008-11-07 14:05:41 -0800990
991 drm_helper_probe_connector_modes(dev, dev->mode_config.max_width,
992 dev->mode_config.max_height);
993
994 drm_setup_crtcs(dev);
995
996 /* alert the driver fb layer */
997 dev->mode_config.funcs->fb_changed(dev);
998
999 /* FIXME: send hotplug event */
1000 return true;
1001}
1002/**
1003 * drm_initial_config - setup a sane initial connector configuration
1004 * @dev: DRM device
Dave Airlief453ba02008-11-07 14:05:41 -08001005 *
1006 * LOCKING:
1007 * Called at init time, must take mode config lock.
1008 *
1009 * Scan the CRTCs and connectors and try to put together an initial setup.
1010 * At the moment, this is a cloned configuration across all heads with
1011 * a new framebuffer object as the backing store.
1012 *
1013 * RETURNS:
1014 * Zero if everything went ok, nonzero otherwise.
1015 */
Jesse Barnes7a1fb5d2009-03-27 13:05:19 -07001016bool drm_helper_initial_config(struct drm_device *dev)
Dave Airlief453ba02008-11-07 14:05:41 -08001017{
Jesse Barnes40a518d2009-01-12 12:05:32 -08001018 int count = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001019
Zhao Yakuib16d9ac2009-12-09 11:23:42 +08001020 /* disable all the possible outputs/crtcs before entering KMS mode */
1021 drm_helper_disable_unused_functions(dev);
1022
Dave Airlied50ba252009-09-23 14:44:08 +10001023 drm_fb_helper_parse_command_line(dev);
1024
Jesse Barnes40a518d2009-01-12 12:05:32 -08001025 count = drm_helper_probe_connector_modes(dev,
1026 dev->mode_config.max_width,
1027 dev->mode_config.max_height);
1028
1029 /*
Dave Airlie575dc342009-09-07 18:43:26 +10001030 * we shouldn't end up with no modes here.
Jesse Barnes40a518d2009-01-12 12:05:32 -08001031 */
Dave Airlie70a94d62010-01-13 16:15:11 +10001032 if (count == 0)
1033 printk(KERN_INFO "No connectors reported connected with modes\n");
Jesse Barnes40a518d2009-01-12 12:05:32 -08001034
1035 drm_setup_crtcs(dev);
1036
1037 /* alert the driver fb layer */
1038 dev->mode_config.funcs->fb_changed(dev);
1039
1040 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001041}
1042EXPORT_SYMBOL(drm_helper_initial_config);
1043
Keith Packardc9fb15f2009-05-30 20:42:28 -07001044static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder)
1045{
1046 int dpms = DRM_MODE_DPMS_OFF;
1047 struct drm_connector *connector;
1048 struct drm_device *dev = encoder->dev;
1049
1050 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1051 if (connector->encoder == encoder)
1052 if (connector->dpms < dpms)
1053 dpms = connector->dpms;
1054 return dpms;
1055}
1056
1057static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc)
1058{
1059 int dpms = DRM_MODE_DPMS_OFF;
1060 struct drm_connector *connector;
1061 struct drm_device *dev = crtc->dev;
1062
1063 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1064 if (connector->encoder && connector->encoder->crtc == crtc)
1065 if (connector->dpms < dpms)
1066 dpms = connector->dpms;
1067 return dpms;
1068}
1069
1070/**
1071 * drm_helper_connector_dpms
1072 * @connector affected connector
1073 * @mode DPMS mode
1074 *
1075 * Calls the low-level connector DPMS function, then
1076 * calls appropriate encoder and crtc DPMS functions as well
1077 */
1078void drm_helper_connector_dpms(struct drm_connector *connector, int mode)
1079{
1080 struct drm_encoder *encoder = connector->encoder;
1081 struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
1082 int old_dpms;
1083
1084 if (mode == connector->dpms)
1085 return;
1086
1087 old_dpms = connector->dpms;
1088 connector->dpms = mode;
1089
1090 /* from off to on, do crtc then encoder */
1091 if (mode < old_dpms) {
1092 if (crtc) {
1093 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
1094 if (crtc_funcs->dpms)
1095 (*crtc_funcs->dpms) (crtc,
1096 drm_helper_choose_crtc_dpms(crtc));
1097 }
1098 if (encoder) {
1099 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
1100 if (encoder_funcs->dpms)
1101 (*encoder_funcs->dpms) (encoder,
1102 drm_helper_choose_encoder_dpms(encoder));
1103 }
1104 }
1105
1106 /* from on to off, do encoder then crtc */
1107 if (mode > old_dpms) {
1108 if (encoder) {
1109 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
1110 if (encoder_funcs->dpms)
1111 (*encoder_funcs->dpms) (encoder,
1112 drm_helper_choose_encoder_dpms(encoder));
1113 }
1114 if (crtc) {
1115 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
1116 if (crtc_funcs->dpms)
1117 (*crtc_funcs->dpms) (crtc,
1118 drm_helper_choose_crtc_dpms(crtc));
1119 }
1120 }
1121
1122 return;
1123}
1124EXPORT_SYMBOL(drm_helper_connector_dpms);
1125
Dave Airlief453ba02008-11-07 14:05:41 -08001126/**
1127 * drm_hotplug_stage_two
1128 * @dev DRM device
1129 * @connector hotpluged connector
1130 *
1131 * LOCKING.
1132 * Caller must hold mode config lock, function might grab struct lock.
1133 *
1134 * Stage two of a hotplug.
1135 *
1136 * RETURNS:
1137 * Zero on success, errno on failure.
1138 */
1139int drm_helper_hotplug_stage_two(struct drm_device *dev)
1140{
Dave Airlief453ba02008-11-07 14:05:41 -08001141 drm_helper_plugged_event(dev);
1142
1143 return 0;
1144}
1145EXPORT_SYMBOL(drm_helper_hotplug_stage_two);
1146
1147int drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb,
1148 struct drm_mode_fb_cmd *mode_cmd)
1149{
1150 fb->width = mode_cmd->width;
1151 fb->height = mode_cmd->height;
1152 fb->pitch = mode_cmd->pitch;
1153 fb->bits_per_pixel = mode_cmd->bpp;
1154 fb->depth = mode_cmd->depth;
1155
1156 return 0;
1157}
1158EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
1159
1160int drm_helper_resume_force_mode(struct drm_device *dev)
1161{
1162 struct drm_crtc *crtc;
David John89347bb2009-12-31 12:00:46 +05301163 struct drm_encoder *encoder;
1164 struct drm_encoder_helper_funcs *encoder_funcs;
1165 struct drm_crtc_helper_funcs *crtc_funcs;
Dave Airlief453ba02008-11-07 14:05:41 -08001166 int ret;
1167
1168 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1169
1170 if (!crtc->enabled)
1171 continue;
1172
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05001173 ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
1174 crtc->x, crtc->y, crtc->fb);
Dave Airlief453ba02008-11-07 14:05:41 -08001175
1176 if (ret == false)
1177 DRM_ERROR("failed to set mode on crtc %p\n", crtc);
David John89347bb2009-12-31 12:00:46 +05301178
1179 /* Turn off outputs that were already powered off */
1180 if (drm_helper_choose_crtc_dpms(crtc)) {
1181 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1182
1183 if(encoder->crtc != crtc)
1184 continue;
1185
1186 encoder_funcs = encoder->helper_private;
1187 if (encoder_funcs->dpms)
1188 (*encoder_funcs->dpms) (encoder,
1189 drm_helper_choose_encoder_dpms(encoder));
1190
1191 crtc_funcs = crtc->helper_private;
1192 if (crtc_funcs->dpms)
1193 (*crtc_funcs->dpms) (crtc,
1194 drm_helper_choose_crtc_dpms(crtc));
1195 }
1196 }
Dave Airlief453ba02008-11-07 14:05:41 -08001197 }
Zhao Yakuiaf4fcb52009-07-08 14:13:13 +08001198 /* disable the unused connectors while restoring the modesetting */
1199 drm_helper_disable_unused_functions(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001200 return 0;
1201}
1202EXPORT_SYMBOL(drm_helper_resume_force_mode);