blob: aba79c4945877ade5ea9f93701f0d123abfdb523 [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) {
Adam Jackson9632b412009-11-23 14:23:07 -0500112 count = drm_add_modes_noedid(connector, 1024, 768);
ykzhao50fe4cf2009-09-03 14:30:04 +0800113 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/**
David John89347bb2009-12-31 12:00:46 +0530219 * drm_helper_disable_unused_functions - disable unused objects
Dave Airlief453ba02008-11-07 14:05:41 -0800220 * @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;
Dave Airlie8ef86782009-09-26 06:39:00 +1000282 struct drm_fb_helper_cmdline_mode *cmdline_mode;
283
284 if (!fb_help_conn)
285 return false;
286
287 cmdline_mode = &fb_help_conn->cmdline_mode;
Dave Airlied50ba252009-09-23 14:44:08 +1000288 return cmdline_mode->specified;
289}
290
291static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_connector *connector, int width, int height)
292{
293 struct drm_fb_helper_connector *fb_help_conn = connector->fb_helper_private;
Dave Airlie8ef86782009-09-26 06:39:00 +1000294 struct drm_fb_helper_cmdline_mode *cmdline_mode;
Dave Airlied50ba252009-09-23 14:44:08 +1000295 struct drm_display_mode *mode = NULL;
296
Dave Airlie8ef86782009-09-26 06:39:00 +1000297 if (!fb_help_conn)
298 return mode;
299
300 cmdline_mode = &fb_help_conn->cmdline_mode;
Dave Airlied50ba252009-09-23 14:44:08 +1000301 if (cmdline_mode->specified == false)
302 return mode;
303
304 /* attempt to find a matching mode in the list of modes
305 * we have gotten so far, if not add a CVT mode that conforms
306 */
307 if (cmdline_mode->rb || cmdline_mode->margins)
308 goto create_mode;
309
310 list_for_each_entry(mode, &connector->modes, head) {
311 /* check width/height */
312 if (mode->hdisplay != cmdline_mode->xres ||
313 mode->vdisplay != cmdline_mode->yres)
314 continue;
315
316 if (cmdline_mode->refresh_specified) {
317 if (mode->vrefresh != cmdline_mode->refresh)
318 continue;
319 }
320
321 if (cmdline_mode->interlace) {
322 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
323 continue;
324 }
325 return mode;
326 }
327
328create_mode:
329 mode = drm_cvt_mode(connector->dev, cmdline_mode->xres,
330 cmdline_mode->yres,
331 cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
332 cmdline_mode->rb, cmdline_mode->interlace,
333 cmdline_mode->margins);
Francisco Jerezeeba5752009-11-07 16:27:32 +0100334 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Dave Airlied50ba252009-09-23 14:44:08 +1000335 list_add(&mode->head, &connector->modes);
336 return mode;
337}
338
Dave Airlief453ba02008-11-07 14:05:41 -0800339static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
340{
341 bool enable;
342
343 if (strict) {
344 enable = connector->status == connector_status_connected;
345 } else {
346 enable = connector->status != connector_status_disconnected;
347 }
348 return enable;
349}
350
351static void drm_enable_connectors(struct drm_device *dev, bool *enabled)
352{
353 bool any_enabled = false;
354 struct drm_connector *connector;
355 int i = 0;
356
357 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
358 enabled[i] = drm_connector_enabled(connector, true);
Zhao Yakui58367ed2009-07-20 13:48:07 +0800359 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
Jesse Barnes40a518d2009-01-12 12:05:32 -0800360 enabled[i] ? "yes" : "no");
Dave Airlief453ba02008-11-07 14:05:41 -0800361 any_enabled |= enabled[i];
362 i++;
363 }
364
365 if (any_enabled)
366 return;
367
368 i = 0;
369 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
370 enabled[i] = drm_connector_enabled(connector, false);
371 i++;
372 }
373}
374
375static bool drm_target_preferred(struct drm_device *dev,
376 struct drm_display_mode **modes,
377 bool *enabled, int width, int height)
378{
379 struct drm_connector *connector;
380 int i = 0;
381
382 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
383
384 if (enabled[i] == false) {
385 i++;
386 continue;
387 }
388
Dave Airlied50ba252009-09-23 14:44:08 +1000389 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
390 connector->base.id);
Jesse Barnes40a518d2009-01-12 12:05:32 -0800391
Dave Airlied50ba252009-09-23 14:44:08 +1000392 /* got for command line mode first */
393 modes[i] = drm_pick_cmdline_mode(connector, width, height);
394 if (!modes[i]) {
395 DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
396 connector->base.id);
397 modes[i] = drm_has_preferred_mode(connector, width, height);
398 }
Jesse Barnes40a518d2009-01-12 12:05:32 -0800399 /* No preferred modes, pick one off the list */
400 if (!modes[i] && !list_empty(&connector->modes)) {
Dave Airlief453ba02008-11-07 14:05:41 -0800401 list_for_each_entry(modes[i], &connector->modes, head)
402 break;
403 }
Zhao Yakui58367ed2009-07-20 13:48:07 +0800404 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
Jesse Barnes40a518d2009-01-12 12:05:32 -0800405 "none");
Dave Airlief453ba02008-11-07 14:05:41 -0800406 i++;
407 }
408 return true;
409}
410
411static int drm_pick_crtcs(struct drm_device *dev,
412 struct drm_crtc **best_crtcs,
413 struct drm_display_mode **modes,
414 int n, int width, int height)
415{
416 int c, o;
417 struct drm_connector *connector;
418 struct drm_connector_helper_funcs *connector_funcs;
419 struct drm_encoder *encoder;
420 struct drm_crtc *best_crtc;
421 int my_score, best_score, score;
422 struct drm_crtc **crtcs, *crtc;
423
424 if (n == dev->mode_config.num_connector)
425 return 0;
426 c = 0;
427 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
428 if (c == n)
429 break;
430 c++;
431 }
432
433 best_crtcs[n] = NULL;
434 best_crtc = NULL;
435 best_score = drm_pick_crtcs(dev, best_crtcs, modes, n+1, width, height);
436 if (modes[n] == NULL)
437 return best_score;
438
439 crtcs = kmalloc(dev->mode_config.num_connector *
440 sizeof(struct drm_crtc *), GFP_KERNEL);
441 if (!crtcs)
442 return best_score;
443
444 my_score = 1;
445 if (connector->status == connector_status_connected)
446 my_score++;
Dave Airlied50ba252009-09-23 14:44:08 +1000447 if (drm_has_cmdline_mode(connector))
448 my_score++;
Dave Airlief453ba02008-11-07 14:05:41 -0800449 if (drm_has_preferred_mode(connector, width, height))
450 my_score++;
451
452 connector_funcs = connector->helper_private;
453 encoder = connector_funcs->best_encoder(connector);
454 if (!encoder)
455 goto out;
456
457 connector->encoder = encoder;
458
459 /* select a crtc for this connector and then attempt to configure
460 remaining connectors */
461 c = 0;
462 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
463
Dave Airliea3a05442009-08-31 15:16:30 +1000464 if ((encoder->possible_crtcs & (1 << c)) == 0) {
Dave Airlief453ba02008-11-07 14:05:41 -0800465 c++;
466 continue;
467 }
468
469 for (o = 0; o < n; o++)
470 if (best_crtcs[o] == crtc)
471 break;
472
473 if (o < n) {
474 /* ignore cloning for now */
475 c++;
476 continue;
477 }
478
479 crtcs[n] = crtc;
480 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_crtc *));
481 score = my_score + drm_pick_crtcs(dev, crtcs, modes, n + 1,
482 width, height);
483 if (score > best_score) {
484 best_crtc = crtc;
485 best_score = score;
486 memcpy(best_crtcs, crtcs,
487 dev->mode_config.num_connector *
488 sizeof(struct drm_crtc *));
489 }
490 c++;
491 }
492out:
493 kfree(crtcs);
494 return best_score;
495}
496
497static void drm_setup_crtcs(struct drm_device *dev)
498{
499 struct drm_crtc **crtcs;
500 struct drm_display_mode **modes;
501 struct drm_encoder *encoder;
502 struct drm_connector *connector;
503 bool *enabled;
504 int width, height;
505 int i, ret;
506
Zhao Yakui58367ed2009-07-20 13:48:07 +0800507 DRM_DEBUG_KMS("\n");
Jesse Barnes40a518d2009-01-12 12:05:32 -0800508
Dave Airlief453ba02008-11-07 14:05:41 -0800509 width = dev->mode_config.max_width;
510 height = dev->mode_config.max_height;
511
512 /* clean out all the encoder/crtc combos */
513 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
514 encoder->crtc = NULL;
515 }
516
517 crtcs = kcalloc(dev->mode_config.num_connector,
518 sizeof(struct drm_crtc *), GFP_KERNEL);
519 modes = kcalloc(dev->mode_config.num_connector,
520 sizeof(struct drm_display_mode *), GFP_KERNEL);
521 enabled = kcalloc(dev->mode_config.num_connector,
522 sizeof(bool), GFP_KERNEL);
523
524 drm_enable_connectors(dev, enabled);
525
526 ret = drm_target_preferred(dev, modes, enabled, width, height);
527 if (!ret)
528 DRM_ERROR("Unable to find initial modes\n");
529
Zhao Yakui58367ed2009-07-20 13:48:07 +0800530 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
Jesse Barnes40a518d2009-01-12 12:05:32 -0800531
Dave Airlief453ba02008-11-07 14:05:41 -0800532 drm_pick_crtcs(dev, crtcs, modes, 0, width, height);
533
534 i = 0;
535 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
536 struct drm_display_mode *mode = modes[i];
537 struct drm_crtc *crtc = crtcs[i];
538
539 if (connector->encoder == NULL) {
540 i++;
541 continue;
542 }
543
544 if (mode && crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800545 DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
Jesse Barnes40a518d2009-01-12 12:05:32 -0800546 mode->name, crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -0800547 crtc->desired_mode = mode;
548 connector->encoder->crtc = crtc;
Dave Airliea3a05442009-08-31 15:16:30 +1000549 } else {
Dave Airlief453ba02008-11-07 14:05:41 -0800550 connector->encoder->crtc = NULL;
Dave Airliea3a05442009-08-31 15:16:30 +1000551 connector->encoder = NULL;
552 }
Dave Airlief453ba02008-11-07 14:05:41 -0800553 i++;
554 }
555
556 kfree(crtcs);
557 kfree(modes);
558 kfree(enabled);
559}
Jesse Barnes7bec7562009-02-23 16:09:34 -0800560
561/**
562 * drm_encoder_crtc_ok - can a given crtc drive a given encoder?
563 * @encoder: encoder to test
564 * @crtc: crtc to test
565 *
566 * Return false if @encoder can't be driven by @crtc, true otherwise.
567 */
568static bool drm_encoder_crtc_ok(struct drm_encoder *encoder,
569 struct drm_crtc *crtc)
570{
571 struct drm_device *dev;
572 struct drm_crtc *tmp;
573 int crtc_mask = 1;
574
575 WARN(!crtc, "checking null crtc?");
576
577 dev = crtc->dev;
578
579 list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
580 if (tmp == crtc)
581 break;
582 crtc_mask <<= 1;
583 }
584
585 if (encoder->possible_crtcs & crtc_mask)
586 return true;
587 return false;
588}
589
590/*
591 * Check the CRTC we're going to map each output to vs. its current
592 * CRTC. If they don't match, we have to disable the output and the CRTC
593 * since the driver will have to re-route things.
594 */
595static void
596drm_crtc_prepare_encoders(struct drm_device *dev)
597{
598 struct drm_encoder_helper_funcs *encoder_funcs;
599 struct drm_encoder *encoder;
600
601 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
602 encoder_funcs = encoder->helper_private;
603 /* Disable unused encoders */
604 if (encoder->crtc == NULL)
605 (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
606 /* Disable encoders whose CRTC is about to change */
607 if (encoder_funcs->get_crtc &&
608 encoder->crtc != (*encoder_funcs->get_crtc)(encoder))
609 (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
610 }
611}
612
Dave Airlief453ba02008-11-07 14:05:41 -0800613/**
614 * drm_crtc_set_mode - set a mode
615 * @crtc: CRTC to program
616 * @mode: mode to use
617 * @x: width of mode
618 * @y: height of mode
619 *
620 * LOCKING:
621 * Caller must hold mode config lock.
622 *
623 * Try to set @mode on @crtc. Give @crtc and its associated connectors a chance
624 * to fixup or reject the mode prior to trying to set it.
625 *
626 * RETURNS:
627 * True if the mode was set successfully, or false otherwise.
628 */
629bool drm_crtc_helper_set_mode(struct drm_crtc *crtc,
630 struct drm_display_mode *mode,
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -0500631 int x, int y,
632 struct drm_framebuffer *old_fb)
Dave Airlief453ba02008-11-07 14:05:41 -0800633{
634 struct drm_device *dev = crtc->dev;
635 struct drm_display_mode *adjusted_mode, saved_mode;
636 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
637 struct drm_encoder_helper_funcs *encoder_funcs;
638 int saved_x, saved_y;
639 struct drm_encoder *encoder;
640 bool ret = true;
641
642 adjusted_mode = drm_mode_duplicate(dev, mode);
643
644 crtc->enabled = drm_helper_crtc_in_use(crtc);
645
646 if (!crtc->enabled)
647 return true;
648
649 saved_mode = crtc->mode;
650 saved_x = crtc->x;
651 saved_y = crtc->y;
652
653 /* Update crtc values up front so the driver can rely on them for mode
654 * setting.
655 */
656 crtc->mode = *mode;
657 crtc->x = x;
658 crtc->y = y;
659
Dave Airlief453ba02008-11-07 14:05:41 -0800660 /* Pass our mode to the connectors and the CRTC to give them a chance to
661 * adjust it according to limitations or connector properties, and also
662 * a chance to reject the mode entirely.
663 */
664 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
665
666 if (encoder->crtc != crtc)
667 continue;
668 encoder_funcs = encoder->helper_private;
669 if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
670 adjusted_mode))) {
671 goto done;
672 }
673 }
674
675 if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) {
676 goto done;
677 }
678
679 /* Prepare the encoders and CRTCs before setting the mode. */
680 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
681
682 if (encoder->crtc != crtc)
683 continue;
684 encoder_funcs = encoder->helper_private;
685 /* Disable the encoders as the first thing we do. */
686 encoder_funcs->prepare(encoder);
687 }
688
Jesse Barnes7bec7562009-02-23 16:09:34 -0800689 drm_crtc_prepare_encoders(dev);
690
Dave Airlief453ba02008-11-07 14:05:41 -0800691 crtc_funcs->prepare(crtc);
692
693 /* Set up the DPLL and any encoders state that needs to adjust or depend
694 * on the DPLL.
695 */
Chris Wilson5c3b82e2009-02-11 13:25:09 +0000696 ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
697 if (!ret)
698 goto done;
Dave Airlief453ba02008-11-07 14:05:41 -0800699
700 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
701
702 if (encoder->crtc != crtc)
703 continue;
704
705 DRM_INFO("%s: set mode %s %x\n", drm_get_encoder_name(encoder),
706 mode->name, mode->base.id);
707 encoder_funcs = encoder->helper_private;
708 encoder_funcs->mode_set(encoder, mode, adjusted_mode);
709 }
710
711 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
712 crtc_funcs->commit(crtc);
713
714 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
715
716 if (encoder->crtc != crtc)
717 continue;
718
719 encoder_funcs = encoder->helper_private;
720 encoder_funcs->commit(encoder);
721
722 }
723
724 /* XXX free adjustedmode */
725 drm_mode_destroy(dev, adjusted_mode);
726 /* FIXME: add subpixel order */
727done:
728 if (!ret) {
729 crtc->mode = saved_mode;
730 crtc->x = saved_x;
731 crtc->y = saved_y;
732 }
733
734 return ret;
735}
736EXPORT_SYMBOL(drm_crtc_helper_set_mode);
737
738
739/**
740 * drm_crtc_helper_set_config - set a new config from userspace
741 * @crtc: CRTC to setup
742 * @crtc_info: user provided configuration
743 * @new_mode: new mode to set
744 * @connector_set: set of connectors for the new config
745 * @fb: new framebuffer
746 *
747 * LOCKING:
748 * Caller must hold mode config lock.
749 *
750 * Setup a new configuration, provided by the user in @crtc_info, and enable
751 * it.
752 *
753 * RETURNS:
754 * Zero. (FIXME)
755 */
756int drm_crtc_helper_set_config(struct drm_mode_set *set)
757{
758 struct drm_device *dev;
Maarten Maathuise67aae72009-08-27 10:18:29 +0200759 struct drm_crtc *save_crtcs, *new_crtc, *crtc;
760 struct drm_encoder *save_encoders, *new_encoder, *encoder;
Jesse Barnes7bec7562009-02-23 16:09:34 -0800761 struct drm_framebuffer *old_fb = NULL;
Jakob Bornecrantz4cb72b12009-08-03 13:43:59 +0100762 bool mode_changed = false; /* if true do a full mode set */
763 bool fb_changed = false; /* if true and !mode_changed just do a flip */
Maarten Maathuise67aae72009-08-27 10:18:29 +0200764 struct drm_connector *save_connectors, *connector;
Dave Airlief453ba02008-11-07 14:05:41 -0800765 int count = 0, ro, fail = 0;
766 struct drm_crtc_helper_funcs *crtc_funcs;
767 int ret = 0;
768
Zhao Yakui58367ed2009-07-20 13:48:07 +0800769 DRM_DEBUG_KMS("\n");
Dave Airlief453ba02008-11-07 14:05:41 -0800770
771 if (!set)
772 return -EINVAL;
773
774 if (!set->crtc)
775 return -EINVAL;
776
777 if (!set->crtc->helper_private)
778 return -EINVAL;
779
780 crtc_funcs = set->crtc->helper_private;
781
Zhao Yakui58367ed2009-07-20 13:48:07 +0800782 DRM_DEBUG_KMS("crtc: %p %d fb: %p connectors: %p num_connectors:"
783 " %d (x, y) (%i, %i)\n",
Dave Airlief453ba02008-11-07 14:05:41 -0800784 set->crtc, set->crtc->base.id, set->fb, set->connectors,
785 (int)set->num_connectors, set->x, set->y);
786
787 dev = set->crtc->dev;
788
Maarten Maathuise67aae72009-08-27 10:18:29 +0200789 /* Allocate space for the backup of all (non-pointer) crtc, encoder and
790 * connector data. */
791 save_crtcs = kzalloc(dev->mode_config.num_crtc *
792 sizeof(struct drm_crtc), GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -0800793 if (!save_crtcs)
794 return -ENOMEM;
795
Maarten Maathuise67aae72009-08-27 10:18:29 +0200796 save_encoders = kzalloc(dev->mode_config.num_encoder *
797 sizeof(struct drm_encoder), GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -0800798 if (!save_encoders) {
799 kfree(save_crtcs);
800 return -ENOMEM;
801 }
802
Maarten Maathuise67aae72009-08-27 10:18:29 +0200803 save_connectors = kzalloc(dev->mode_config.num_connector *
804 sizeof(struct drm_connector), GFP_KERNEL);
805 if (!save_connectors) {
806 kfree(save_crtcs);
807 kfree(save_encoders);
808 return -ENOMEM;
809 }
810
811 /* Copy data. Note that driver private data is not affected.
812 * Should anything bad happen only the expected state is
813 * restored, not the drivers personal bookkeeping.
814 */
815 count = 0;
816 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
817 save_crtcs[count++] = *crtc;
818 }
819
820 count = 0;
821 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
822 save_encoders[count++] = *encoder;
823 }
824
825 count = 0;
826 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
827 save_connectors[count++] = *connector;
828 }
829
Dave Airlief453ba02008-11-07 14:05:41 -0800830 /* We should be able to check here if the fb has the same properties
831 * and then just flip_or_move it */
832 if (set->crtc->fb != set->fb) {
Jesse Barnes712531b2009-01-09 13:56:14 -0800833 /* If we have no fb then treat it as a full mode set */
Jesse Barnes7bec7562009-02-23 16:09:34 -0800834 if (set->crtc->fb == NULL) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800835 DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
Jesse Barnes712531b2009-01-09 13:56:14 -0800836 mode_changed = true;
Jakob Bornecrantz4cb72b12009-08-03 13:43:59 +0100837 } else if (set->fb == NULL) {
838 mode_changed = true;
Jesse Barnes7bec7562009-02-23 16:09:34 -0800839 } else if ((set->fb->bits_per_pixel !=
Jesse Barnes712531b2009-01-09 13:56:14 -0800840 set->crtc->fb->bits_per_pixel) ||
841 set->fb->depth != set->crtc->fb->depth)
842 fb_changed = true;
Dave Airlief453ba02008-11-07 14:05:41 -0800843 else
Jesse Barnes712531b2009-01-09 13:56:14 -0800844 fb_changed = true;
Dave Airlief453ba02008-11-07 14:05:41 -0800845 }
846
847 if (set->x != set->crtc->x || set->y != set->crtc->y)
Jesse Barnes712531b2009-01-09 13:56:14 -0800848 fb_changed = true;
Dave Airlief453ba02008-11-07 14:05:41 -0800849
850 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800851 DRM_DEBUG_KMS("modes are different, full mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -0800852 drm_mode_debug_printmodeline(&set->crtc->mode);
853 drm_mode_debug_printmodeline(set->mode);
Jesse Barnes712531b2009-01-09 13:56:14 -0800854 mode_changed = true;
Dave Airlief453ba02008-11-07 14:05:41 -0800855 }
856
857 /* a) traverse passed in connector list and get encoders for them */
858 count = 0;
859 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
860 struct drm_connector_helper_funcs *connector_funcs =
861 connector->helper_private;
Dave Airlief453ba02008-11-07 14:05:41 -0800862 new_encoder = connector->encoder;
863 for (ro = 0; ro < set->num_connectors; ro++) {
864 if (set->connectors[ro] == connector) {
865 new_encoder = connector_funcs->best_encoder(connector);
866 /* if we can't get an encoder for a connector
867 we are setting now - then fail */
868 if (new_encoder == NULL)
869 /* don't break so fail path works correct */
870 fail = 1;
871 break;
872 }
873 }
874
875 if (new_encoder != connector->encoder) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800876 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
Jesse Barnes712531b2009-01-09 13:56:14 -0800877 mode_changed = true;
Maarten Maathuisff846ab2009-08-19 00:56:45 +0200878 /* If the encoder is reused for another connector, then
879 * the appropriate crtc will be set later.
880 */
Maarten Maathuisff6fdbe2009-09-01 03:39:04 +0200881 if (connector->encoder)
882 connector->encoder->crtc = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800883 connector->encoder = new_encoder;
884 }
885 }
886
887 if (fail) {
888 ret = -EINVAL;
Maarten Maathuise67aae72009-08-27 10:18:29 +0200889 goto fail;
Dave Airlief453ba02008-11-07 14:05:41 -0800890 }
891
892 count = 0;
893 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
894 if (!connector->encoder)
895 continue;
896
Dave Airlief453ba02008-11-07 14:05:41 -0800897 if (connector->encoder->crtc == set->crtc)
898 new_crtc = NULL;
899 else
900 new_crtc = connector->encoder->crtc;
901
902 for (ro = 0; ro < set->num_connectors; ro++) {
903 if (set->connectors[ro] == connector)
904 new_crtc = set->crtc;
905 }
Jesse Barnes7bec7562009-02-23 16:09:34 -0800906
907 /* Make sure the new CRTC will work with the encoder */
908 if (new_crtc &&
909 !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
910 ret = -EINVAL;
Maarten Maathuise67aae72009-08-27 10:18:29 +0200911 goto fail;
Jesse Barnes7bec7562009-02-23 16:09:34 -0800912 }
Dave Airlief453ba02008-11-07 14:05:41 -0800913 if (new_crtc != connector->encoder->crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800914 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
Jesse Barnes712531b2009-01-09 13:56:14 -0800915 mode_changed = true;
Dave Airlief453ba02008-11-07 14:05:41 -0800916 connector->encoder->crtc = new_crtc;
917 }
Zhao Yakui58367ed2009-07-20 13:48:07 +0800918 DRM_DEBUG_KMS("setting connector %d crtc to %p\n",
Jesse Barnes7bec7562009-02-23 16:09:34 -0800919 connector->base.id, new_crtc);
Dave Airlief453ba02008-11-07 14:05:41 -0800920 }
921
922 /* mode_set_base is not a required function */
Jesse Barnes712531b2009-01-09 13:56:14 -0800923 if (fb_changed && !crtc_funcs->mode_set_base)
924 mode_changed = true;
Dave Airlief453ba02008-11-07 14:05:41 -0800925
Jesse Barnes712531b2009-01-09 13:56:14 -0800926 if (mode_changed) {
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -0500927 old_fb = set->crtc->fb;
Dave Airlief453ba02008-11-07 14:05:41 -0800928 set->crtc->fb = set->fb;
929 set->crtc->enabled = (set->mode != NULL);
930 if (set->mode != NULL) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800931 DRM_DEBUG_KMS("attempting to set mode from"
932 " userspace\n");
Dave Airlief453ba02008-11-07 14:05:41 -0800933 drm_mode_debug_printmodeline(set->mode);
934 if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -0500935 set->x, set->y,
936 old_fb)) {
Chris Wilson5c3b82e2009-02-11 13:25:09 +0000937 DRM_ERROR("failed to set mode on crtc %p\n",
938 set->crtc);
Dave Airlief453ba02008-11-07 14:05:41 -0800939 ret = -EINVAL;
Maarten Maathuise67aae72009-08-27 10:18:29 +0200940 goto fail;
Dave Airlief453ba02008-11-07 14:05:41 -0800941 }
942 /* TODO are these needed? */
943 set->crtc->desired_x = set->x;
944 set->crtc->desired_y = set->y;
945 set->crtc->desired_mode = set->mode;
946 }
947 drm_helper_disable_unused_functions(dev);
Jesse Barnes712531b2009-01-09 13:56:14 -0800948 } else if (fb_changed) {
Ben Skeggs9b1596a2009-09-18 10:43:52 +1000949 set->crtc->x = set->x;
950 set->crtc->y = set->y;
951
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -0500952 old_fb = set->crtc->fb;
Dave Airlief453ba02008-11-07 14:05:41 -0800953 if (set->crtc->fb != set->fb)
954 set->crtc->fb = set->fb;
Chris Wilson5c3b82e2009-02-11 13:25:09 +0000955 ret = crtc_funcs->mode_set_base(set->crtc,
956 set->x, set->y, old_fb);
957 if (ret != 0)
Maarten Maathuise67aae72009-08-27 10:18:29 +0200958 goto fail;
Dave Airlief453ba02008-11-07 14:05:41 -0800959 }
960
Maarten Maathuise67aae72009-08-27 10:18:29 +0200961 kfree(save_connectors);
Dave Airlief453ba02008-11-07 14:05:41 -0800962 kfree(save_encoders);
963 kfree(save_crtcs);
964 return 0;
965
Maarten Maathuise67aae72009-08-27 10:18:29 +0200966fail:
967 /* Restore all previous data. */
Dave Airlief453ba02008-11-07 14:05:41 -0800968 count = 0;
Maarten Maathuise67aae72009-08-27 10:18:29 +0200969 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
970 *crtc = save_crtcs[count++];
971 }
Chris Wilsone62fb642009-02-11 16:39:21 +0000972
Maarten Maathuise67aae72009-08-27 10:18:29 +0200973 count = 0;
974 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
975 *encoder = save_encoders[count++];
Chris Wilsone62fb642009-02-11 16:39:21 +0000976 }
Maarten Maathuise67aae72009-08-27 10:18:29 +0200977
Dave Airlief453ba02008-11-07 14:05:41 -0800978 count = 0;
979 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Maarten Maathuise67aae72009-08-27 10:18:29 +0200980 *connector = save_connectors[count++];
Dave Airlief453ba02008-11-07 14:05:41 -0800981 }
Maarten Maathuise67aae72009-08-27 10:18:29 +0200982
983 kfree(save_connectors);
Dave Airlief453ba02008-11-07 14:05:41 -0800984 kfree(save_encoders);
Maarten Maathuise67aae72009-08-27 10:18:29 +0200985 kfree(save_crtcs);
Dave Airlief453ba02008-11-07 14:05:41 -0800986 return ret;
987}
988EXPORT_SYMBOL(drm_crtc_helper_set_config);
989
990bool drm_helper_plugged_event(struct drm_device *dev)
991{
Zhao Yakui58367ed2009-07-20 13:48:07 +0800992 DRM_DEBUG_KMS("\n");
Dave Airlief453ba02008-11-07 14:05:41 -0800993
994 drm_helper_probe_connector_modes(dev, dev->mode_config.max_width,
995 dev->mode_config.max_height);
996
997 drm_setup_crtcs(dev);
998
999 /* alert the driver fb layer */
1000 dev->mode_config.funcs->fb_changed(dev);
1001
1002 /* FIXME: send hotplug event */
1003 return true;
1004}
1005/**
1006 * drm_initial_config - setup a sane initial connector configuration
1007 * @dev: DRM device
Dave Airlief453ba02008-11-07 14:05:41 -08001008 *
1009 * LOCKING:
1010 * Called at init time, must take mode config lock.
1011 *
1012 * Scan the CRTCs and connectors and try to put together an initial setup.
1013 * At the moment, this is a cloned configuration across all heads with
1014 * a new framebuffer object as the backing store.
1015 *
1016 * RETURNS:
1017 * Zero if everything went ok, nonzero otherwise.
1018 */
Jesse Barnes7a1fb5d2009-03-27 13:05:19 -07001019bool drm_helper_initial_config(struct drm_device *dev)
Dave Airlief453ba02008-11-07 14:05:41 -08001020{
Jesse Barnes40a518d2009-01-12 12:05:32 -08001021 int count = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001022
Zhao Yakuib16d9ac2009-12-09 11:23:42 +08001023 /* disable all the possible outputs/crtcs before entering KMS mode */
1024 drm_helper_disable_unused_functions(dev);
1025
Dave Airlied50ba252009-09-23 14:44:08 +10001026 drm_fb_helper_parse_command_line(dev);
1027
Jesse Barnes40a518d2009-01-12 12:05:32 -08001028 count = drm_helper_probe_connector_modes(dev,
1029 dev->mode_config.max_width,
1030 dev->mode_config.max_height);
1031
1032 /*
Dave Airlie575dc342009-09-07 18:43:26 +10001033 * we shouldn't end up with no modes here.
Jesse Barnes40a518d2009-01-12 12:05:32 -08001034 */
Dave Airlied50ba252009-09-23 14:44:08 +10001035 WARN(!count, "No connectors reported connected with modes\n");
Jesse Barnes40a518d2009-01-12 12:05:32 -08001036
1037 drm_setup_crtcs(dev);
1038
1039 /* alert the driver fb layer */
1040 dev->mode_config.funcs->fb_changed(dev);
1041
1042 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001043}
1044EXPORT_SYMBOL(drm_helper_initial_config);
1045
Keith Packardc9fb15f2009-05-30 20:42:28 -07001046static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder)
1047{
1048 int dpms = DRM_MODE_DPMS_OFF;
1049 struct drm_connector *connector;
1050 struct drm_device *dev = encoder->dev;
1051
1052 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1053 if (connector->encoder == encoder)
1054 if (connector->dpms < dpms)
1055 dpms = connector->dpms;
1056 return dpms;
1057}
1058
1059static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc)
1060{
1061 int dpms = DRM_MODE_DPMS_OFF;
1062 struct drm_connector *connector;
1063 struct drm_device *dev = crtc->dev;
1064
1065 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1066 if (connector->encoder && connector->encoder->crtc == crtc)
1067 if (connector->dpms < dpms)
1068 dpms = connector->dpms;
1069 return dpms;
1070}
1071
1072/**
1073 * drm_helper_connector_dpms
1074 * @connector affected connector
1075 * @mode DPMS mode
1076 *
1077 * Calls the low-level connector DPMS function, then
1078 * calls appropriate encoder and crtc DPMS functions as well
1079 */
1080void drm_helper_connector_dpms(struct drm_connector *connector, int mode)
1081{
1082 struct drm_encoder *encoder = connector->encoder;
1083 struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
1084 int old_dpms;
1085
1086 if (mode == connector->dpms)
1087 return;
1088
1089 old_dpms = connector->dpms;
1090 connector->dpms = mode;
1091
1092 /* from off to on, do crtc then encoder */
1093 if (mode < old_dpms) {
1094 if (crtc) {
1095 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
1096 if (crtc_funcs->dpms)
1097 (*crtc_funcs->dpms) (crtc,
1098 drm_helper_choose_crtc_dpms(crtc));
1099 }
1100 if (encoder) {
1101 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
1102 if (encoder_funcs->dpms)
1103 (*encoder_funcs->dpms) (encoder,
1104 drm_helper_choose_encoder_dpms(encoder));
1105 }
1106 }
1107
1108 /* from on to off, do encoder then crtc */
1109 if (mode > old_dpms) {
1110 if (encoder) {
1111 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
1112 if (encoder_funcs->dpms)
1113 (*encoder_funcs->dpms) (encoder,
1114 drm_helper_choose_encoder_dpms(encoder));
1115 }
1116 if (crtc) {
1117 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
1118 if (crtc_funcs->dpms)
1119 (*crtc_funcs->dpms) (crtc,
1120 drm_helper_choose_crtc_dpms(crtc));
1121 }
1122 }
1123
1124 return;
1125}
1126EXPORT_SYMBOL(drm_helper_connector_dpms);
1127
Dave Airlief453ba02008-11-07 14:05:41 -08001128/**
1129 * drm_hotplug_stage_two
1130 * @dev DRM device
1131 * @connector hotpluged connector
1132 *
1133 * LOCKING.
1134 * Caller must hold mode config lock, function might grab struct lock.
1135 *
1136 * Stage two of a hotplug.
1137 *
1138 * RETURNS:
1139 * Zero on success, errno on failure.
1140 */
1141int drm_helper_hotplug_stage_two(struct drm_device *dev)
1142{
Dave Airlief453ba02008-11-07 14:05:41 -08001143 drm_helper_plugged_event(dev);
1144
1145 return 0;
1146}
1147EXPORT_SYMBOL(drm_helper_hotplug_stage_two);
1148
1149int drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb,
1150 struct drm_mode_fb_cmd *mode_cmd)
1151{
1152 fb->width = mode_cmd->width;
1153 fb->height = mode_cmd->height;
1154 fb->pitch = mode_cmd->pitch;
1155 fb->bits_per_pixel = mode_cmd->bpp;
1156 fb->depth = mode_cmd->depth;
1157
1158 return 0;
1159}
1160EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
1161
1162int drm_helper_resume_force_mode(struct drm_device *dev)
1163{
1164 struct drm_crtc *crtc;
David John89347bb2009-12-31 12:00:46 +05301165 struct drm_encoder *encoder;
1166 struct drm_encoder_helper_funcs *encoder_funcs;
1167 struct drm_crtc_helper_funcs *crtc_funcs;
Dave Airlief453ba02008-11-07 14:05:41 -08001168 int ret;
1169
1170 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1171
1172 if (!crtc->enabled)
1173 continue;
1174
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05001175 ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
1176 crtc->x, crtc->y, crtc->fb);
Dave Airlief453ba02008-11-07 14:05:41 -08001177
1178 if (ret == false)
1179 DRM_ERROR("failed to set mode on crtc %p\n", crtc);
David John89347bb2009-12-31 12:00:46 +05301180
1181 /* Turn off outputs that were already powered off */
1182 if (drm_helper_choose_crtc_dpms(crtc)) {
1183 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1184
1185 if(encoder->crtc != crtc)
1186 continue;
1187
1188 encoder_funcs = encoder->helper_private;
1189 if (encoder_funcs->dpms)
1190 (*encoder_funcs->dpms) (encoder,
1191 drm_helper_choose_encoder_dpms(encoder));
1192
1193 crtc_funcs = crtc->helper_private;
1194 if (crtc_funcs->dpms)
1195 (*crtc_funcs->dpms) (crtc,
1196 drm_helper_choose_crtc_dpms(crtc));
1197 }
1198 }
Dave Airlief453ba02008-11-07 14:05:41 -08001199 }
Zhao Yakuiaf4fcb52009-07-08 14:13:13 +08001200 /* disable the unused connectors while restoring the modesetting */
1201 drm_helper_disable_unused_functions(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001202 return 0;
1203}
1204EXPORT_SYMBOL(drm_helper_resume_force_mode);