drm: add plane support v3
[deliverable/linux.git] / drivers / gpu / drm / drm_crtc_helper.c
CommitLineData
f453ba04
DA
1/*
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
2d1a8a48 32#include <linux/export.h>
0603ba14 33#include <linux/moduleparam.h>
2d1a8a48 34
f453ba04
DA
35#include "drmP.h"
36#include "drm_crtc.h"
37#include "drm_crtc_helper.h"
d50ba256 38#include "drm_fb_helper.h"
f453ba04 39
e58f637b
CW
40static bool drm_kms_helper_poll = true;
41module_param_named(poll, drm_kms_helper_poll, bool, 0600);
42
6714977b 43static void drm_mode_validate_flag(struct drm_connector *connector,
44 int flags)
45{
46 struct drm_display_mode *mode, *t;
47
48 if (flags == (DRM_MODE_FLAG_DBLSCAN | DRM_MODE_FLAG_INTERLACE))
49 return;
50
51 list_for_each_entry_safe(mode, t, &connector->modes, head) {
52 if ((mode->flags & DRM_MODE_FLAG_INTERLACE) &&
53 !(flags & DRM_MODE_FLAG_INTERLACE))
54 mode->status = MODE_NO_INTERLACE;
55 if ((mode->flags & DRM_MODE_FLAG_DBLSCAN) &&
56 !(flags & DRM_MODE_FLAG_DBLSCAN))
57 mode->status = MODE_NO_DBLESCAN;
58 }
59
60 return;
61}
62
f453ba04 63/**
38651674 64 * drm_helper_probe_single_connector_modes - get complete set of display modes
f453ba04
DA
65 * @dev: DRM device
66 * @maxX: max width for modes
67 * @maxY: max height for modes
68 *
69 * LOCKING:
70 * Caller must hold mode config lock.
71 *
72 * Based on @dev's mode_config layout, scan all the connectors and try to detect
73 * modes on them. Modes will first be added to the connector's probed_modes
74 * list, then culled (based on validity and the @maxX, @maxY parameters) and
75 * put into the normal modes list.
76 *
77 * Intended to be used either at bootup time or when major configuration
78 * changes have occurred.
79 *
80 * FIXME: take into account monitor limits
40a518d9
JB
81 *
82 * RETURNS:
83 * Number of modes found on @connector.
f453ba04 84 */
40a518d9
JB
85int drm_helper_probe_single_connector_modes(struct drm_connector *connector,
86 uint32_t maxX, uint32_t maxY)
f453ba04
DA
87{
88 struct drm_device *dev = connector->dev;
89 struct drm_display_mode *mode, *t;
90 struct drm_connector_helper_funcs *connector_funcs =
91 connector->helper_private;
40a518d9 92 int count = 0;
6714977b 93 int mode_flags = 0;
f453ba04 94
9440106b
JG
95 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", connector->base.id,
96 drm_get_connector_name(connector));
f453ba04
DA
97 /* set all modes to the unverified state */
98 list_for_each_entry_safe(mode, t, &connector->modes, head)
99 mode->status = MODE_UNVERIFIED;
100
d50ba256
DA
101 if (connector->force) {
102 if (connector->force == DRM_FORCE_ON)
103 connector->status = connector_status_connected;
104 else
105 connector->status = connector_status_disconnected;
106 if (connector->funcs->force)
107 connector->funcs->force(connector);
e58f637b 108 } else {
930a9e28 109 connector->status = connector->funcs->detect(connector, true);
551402a3 110 drm_kms_helper_poll_enable(dev);
e58f637b 111 }
f453ba04
DA
112
113 if (connector->status == connector_status_disconnected) {
9440106b
JG
114 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] disconnected\n",
115 connector->base.id, drm_get_connector_name(connector));
72539832 116 drm_mode_connector_update_edid_property(connector, NULL);
620f3781 117 goto prune;
f453ba04
DA
118 }
119
40a518d9 120 count = (*connector_funcs->get_modes)(connector);
c7ef35a9 121 if (count == 0 && connector->status == connector_status_connected)
9632b41f 122 count = drm_add_modes_noedid(connector, 1024, 768);
c7ef35a9
CW
123 if (count == 0)
124 goto prune;
f453ba04 125
40a518d9 126 drm_mode_connector_list_update(connector);
f453ba04
DA
127
128 if (maxX && maxY)
129 drm_mode_validate_size(dev, &connector->modes, maxX,
130 maxY, 0);
6714977b 131
132 if (connector->interlace_allowed)
133 mode_flags |= DRM_MODE_FLAG_INTERLACE;
134 if (connector->doublescan_allowed)
135 mode_flags |= DRM_MODE_FLAG_DBLSCAN;
136 drm_mode_validate_flag(connector, mode_flags);
137
f453ba04
DA
138 list_for_each_entry_safe(mode, t, &connector->modes, head) {
139 if (mode->status == MODE_OK)
140 mode->status = connector_funcs->mode_valid(connector,
141 mode);
142 }
143
620f3781 144prune:
f453ba04
DA
145 drm_mode_prune_invalid(dev, &connector->modes, true);
146
40a518d9
JB
147 if (list_empty(&connector->modes))
148 return 0;
f453ba04
DA
149
150 drm_mode_sort(&connector->modes);
151
9440106b
JG
152 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] probed modes :\n", connector->base.id,
153 drm_get_connector_name(connector));
f453ba04
DA
154 list_for_each_entry_safe(mode, t, &connector->modes, head) {
155 mode->vrefresh = drm_mode_vrefresh(mode);
156
157 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
158 drm_mode_debug_printmodeline(mode);
159 }
40a518d9
JB
160
161 return count;
f453ba04
DA
162}
163EXPORT_SYMBOL(drm_helper_probe_single_connector_modes);
164
c9fb15f6
KP
165/**
166 * drm_helper_encoder_in_use - check if a given encoder is in use
167 * @encoder: encoder to check
168 *
169 * LOCKING:
170 * Caller must hold mode config lock.
171 *
172 * Walk @encoders's DRM device's mode_config and see if it's in use.
173 *
174 * RETURNS:
175 * True if @encoder is part of the mode_config, false otherwise.
176 */
177bool drm_helper_encoder_in_use(struct drm_encoder *encoder)
178{
179 struct drm_connector *connector;
180 struct drm_device *dev = encoder->dev;
181 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
182 if (connector->encoder == encoder)
183 return true;
184 return false;
185}
186EXPORT_SYMBOL(drm_helper_encoder_in_use);
187
f453ba04
DA
188/**
189 * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config
190 * @crtc: CRTC to check
191 *
192 * LOCKING:
193 * Caller must hold mode config lock.
194 *
195 * Walk @crtc's DRM device's mode_config and see if it's in use.
196 *
197 * RETURNS:
198 * True if @crtc is part of the mode_config, false otherwise.
199 */
200bool drm_helper_crtc_in_use(struct drm_crtc *crtc)
201{
202 struct drm_encoder *encoder;
203 struct drm_device *dev = crtc->dev;
204 /* FIXME: Locking around list access? */
205 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
c9fb15f6 206 if (encoder->crtc == crtc && drm_helper_encoder_in_use(encoder))
f453ba04
DA
207 return true;
208 return false;
209}
210EXPORT_SYMBOL(drm_helper_crtc_in_use);
211
86a1b9d1
BS
212static void
213drm_encoder_disable(struct drm_encoder *encoder)
214{
215 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
216
217 if (encoder_funcs->disable)
218 (*encoder_funcs->disable)(encoder);
219 else
220 (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
221}
222
f453ba04 223/**
89347bb8 224 * drm_helper_disable_unused_functions - disable unused objects
f453ba04
DA
225 * @dev: DRM device
226 *
227 * LOCKING:
228 * Caller must hold mode config lock.
229 *
230 * If an connector or CRTC isn't part of @dev's mode_config, it can be disabled
231 * by calling its dpms function, which should power it off.
232 */
233void drm_helper_disable_unused_functions(struct drm_device *dev)
234{
235 struct drm_encoder *encoder;
a3a0544b 236 struct drm_connector *connector;
f453ba04
DA
237 struct drm_crtc *crtc;
238
a3a0544b
DA
239 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
240 if (!connector->encoder)
241 continue;
242 if (connector->status == connector_status_disconnected)
243 connector->encoder = NULL;
244 }
245
f453ba04 246 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
92971021 247 if (!drm_helper_encoder_in_use(encoder)) {
86a1b9d1 248 drm_encoder_disable(encoder);
9c552dd7
DA
249 /* disconnector encoder from any connector */
250 encoder->crtc = NULL;
a3a0544b 251 }
f453ba04
DA
252 }
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) {
5c8d7171
AD
258 if (crtc_funcs->disable)
259 (*crtc_funcs->disable)(crtc);
260 else
261 (*crtc_funcs->dpms)(crtc, DRM_MODE_DPMS_OFF);
f453ba04
DA
262 crtc->fb = NULL;
263 }
264 }
265}
266EXPORT_SYMBOL(drm_helper_disable_unused_functions);
267
7bec756c
JB
268/**
269 * drm_encoder_crtc_ok - can a given crtc drive a given encoder?
270 * @encoder: encoder to test
271 * @crtc: crtc to test
272 *
273 * Return false if @encoder can't be driven by @crtc, true otherwise.
274 */
275static bool drm_encoder_crtc_ok(struct drm_encoder *encoder,
276 struct drm_crtc *crtc)
277{
278 struct drm_device *dev;
279 struct drm_crtc *tmp;
280 int crtc_mask = 1;
281
fce7d61b 282 WARN(!crtc, "checking null crtc?\n");
7bec756c
JB
283
284 dev = crtc->dev;
285
286 list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
287 if (tmp == crtc)
288 break;
289 crtc_mask <<= 1;
290 }
291
292 if (encoder->possible_crtcs & crtc_mask)
293 return true;
294 return false;
295}
296
297/*
298 * Check the CRTC we're going to map each output to vs. its current
299 * CRTC. If they don't match, we have to disable the output and the CRTC
300 * since the driver will have to re-route things.
301 */
302static void
303drm_crtc_prepare_encoders(struct drm_device *dev)
304{
305 struct drm_encoder_helper_funcs *encoder_funcs;
306 struct drm_encoder *encoder;
307
308 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
309 encoder_funcs = encoder->helper_private;
310 /* Disable unused encoders */
311 if (encoder->crtc == NULL)
86a1b9d1 312 drm_encoder_disable(encoder);
7bec756c
JB
313 /* Disable encoders whose CRTC is about to change */
314 if (encoder_funcs->get_crtc &&
315 encoder->crtc != (*encoder_funcs->get_crtc)(encoder))
86a1b9d1 316 drm_encoder_disable(encoder);
7bec756c
JB
317 }
318}
319
f453ba04
DA
320/**
321 * drm_crtc_set_mode - set a mode
322 * @crtc: CRTC to program
323 * @mode: mode to use
324 * @x: width of mode
325 * @y: height of mode
326 *
327 * LOCKING:
328 * Caller must hold mode config lock.
329 *
330 * Try to set @mode on @crtc. Give @crtc and its associated connectors a chance
331 * to fixup or reject the mode prior to trying to set it.
332 *
333 * RETURNS:
334 * True if the mode was set successfully, or false otherwise.
335 */
336bool drm_crtc_helper_set_mode(struct drm_crtc *crtc,
337 struct drm_display_mode *mode,
3c4fdcfb
KH
338 int x, int y,
339 struct drm_framebuffer *old_fb)
f453ba04
DA
340{
341 struct drm_device *dev = crtc->dev;
27641c3f 342 struct drm_display_mode *adjusted_mode, saved_mode, saved_hwmode;
f453ba04
DA
343 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
344 struct drm_encoder_helper_funcs *encoder_funcs;
345 int saved_x, saved_y;
346 struct drm_encoder *encoder;
347 bool ret = true;
348
f453ba04 349 crtc->enabled = drm_helper_crtc_in_use(crtc);
f453ba04
DA
350 if (!crtc->enabled)
351 return true;
352
021a8455
CW
353 adjusted_mode = drm_mode_duplicate(dev, mode);
354
27641c3f 355 saved_hwmode = crtc->hwmode;
f453ba04
DA
356 saved_mode = crtc->mode;
357 saved_x = crtc->x;
358 saved_y = crtc->y;
359
360 /* Update crtc values up front so the driver can rely on them for mode
361 * setting.
362 */
363 crtc->mode = *mode;
364 crtc->x = x;
365 crtc->y = y;
366
f453ba04
DA
367 /* Pass our mode to the connectors and the CRTC to give them a chance to
368 * adjust it according to limitations or connector properties, and also
369 * a chance to reject the mode entirely.
370 */
371 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
372
373 if (encoder->crtc != crtc)
374 continue;
375 encoder_funcs = encoder->helper_private;
376 if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
377 adjusted_mode))) {
836e53d7 378 DRM_DEBUG_KMS("Encoder fixup failed\n");
f453ba04
DA
379 goto done;
380 }
381 }
382
383 if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) {
836e53d7 384 DRM_DEBUG_KMS("CRTC fixup failed\n");
f453ba04
DA
385 goto done;
386 }
9440106b 387 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
f453ba04
DA
388
389 /* Prepare the encoders and CRTCs before setting the mode. */
390 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
391
392 if (encoder->crtc != crtc)
393 continue;
394 encoder_funcs = encoder->helper_private;
395 /* Disable the encoders as the first thing we do. */
396 encoder_funcs->prepare(encoder);
397 }
398
7bec756c
JB
399 drm_crtc_prepare_encoders(dev);
400
f453ba04
DA
401 crtc_funcs->prepare(crtc);
402
403 /* Set up the DPLL and any encoders state that needs to adjust or depend
404 * on the DPLL.
405 */
5c3b82e2
CW
406 ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
407 if (!ret)
408 goto done;
f453ba04
DA
409
410 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
411
412 if (encoder->crtc != crtc)
413 continue;
414
9440106b
JG
415 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
416 encoder->base.id, drm_get_encoder_name(encoder),
417 mode->base.id, mode->name);
f453ba04
DA
418 encoder_funcs = encoder->helper_private;
419 encoder_funcs->mode_set(encoder, mode, adjusted_mode);
420 }
421
422 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
423 crtc_funcs->commit(crtc);
424
425 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
426
427 if (encoder->crtc != crtc)
428 continue;
429
430 encoder_funcs = encoder->helper_private;
431 encoder_funcs->commit(encoder);
432
433 }
434
27641c3f
MK
435 /* Store real post-adjustment hardware mode. */
436 crtc->hwmode = *adjusted_mode;
437
438 /* Calculate and store various constants which
439 * are later needed by vblank and swap-completion
440 * timestamping. They are derived from true hwmode.
441 */
442 drm_calc_timestamping_constants(crtc);
443
f453ba04
DA
444 /* FIXME: add subpixel order */
445done:
021a8455 446 drm_mode_destroy(dev, adjusted_mode);
f453ba04 447 if (!ret) {
27641c3f 448 crtc->hwmode = saved_hwmode;
f453ba04
DA
449 crtc->mode = saved_mode;
450 crtc->x = saved_x;
451 crtc->y = saved_y;
452 }
453
454 return ret;
455}
456EXPORT_SYMBOL(drm_crtc_helper_set_mode);
457
458
459/**
460 * drm_crtc_helper_set_config - set a new config from userspace
461 * @crtc: CRTC to setup
462 * @crtc_info: user provided configuration
463 * @new_mode: new mode to set
464 * @connector_set: set of connectors for the new config
465 * @fb: new framebuffer
466 *
467 * LOCKING:
468 * Caller must hold mode config lock.
469 *
470 * Setup a new configuration, provided by the user in @crtc_info, and enable
471 * it.
472 *
473 * RETURNS:
474 * Zero. (FIXME)
475 */
476int drm_crtc_helper_set_config(struct drm_mode_set *set)
477{
478 struct drm_device *dev;
e67aae79
MM
479 struct drm_crtc *save_crtcs, *new_crtc, *crtc;
480 struct drm_encoder *save_encoders, *new_encoder, *encoder;
7bec756c 481 struct drm_framebuffer *old_fb = NULL;
4cb72b17
JB
482 bool mode_changed = false; /* if true do a full mode set */
483 bool fb_changed = false; /* if true and !mode_changed just do a flip */
e67aae79 484 struct drm_connector *save_connectors, *connector;
f453ba04
DA
485 int count = 0, ro, fail = 0;
486 struct drm_crtc_helper_funcs *crtc_funcs;
487 int ret = 0;
bf9dc102 488 int i;
f453ba04 489
58367ed6 490 DRM_DEBUG_KMS("\n");
f453ba04
DA
491
492 if (!set)
493 return -EINVAL;
494
495 if (!set->crtc)
496 return -EINVAL;
497
498 if (!set->crtc->helper_private)
499 return -EINVAL;
500
501 crtc_funcs = set->crtc->helper_private;
502
ede3ff52
CW
503 if (!set->mode)
504 set->fb = NULL;
505
9440106b
JG
506 if (set->fb) {
507 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
508 set->crtc->base.id, set->fb->base.id,
509 (int)set->num_connectors, set->x, set->y);
510 } else {
ede3ff52
CW
511 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
512 set->mode = NULL;
513 set->num_connectors = 0;
9440106b 514 }
f453ba04
DA
515
516 dev = set->crtc->dev;
517
e67aae79
MM
518 /* Allocate space for the backup of all (non-pointer) crtc, encoder and
519 * connector data. */
520 save_crtcs = kzalloc(dev->mode_config.num_crtc *
521 sizeof(struct drm_crtc), GFP_KERNEL);
f453ba04
DA
522 if (!save_crtcs)
523 return -ENOMEM;
524
e67aae79
MM
525 save_encoders = kzalloc(dev->mode_config.num_encoder *
526 sizeof(struct drm_encoder), GFP_KERNEL);
f453ba04
DA
527 if (!save_encoders) {
528 kfree(save_crtcs);
529 return -ENOMEM;
530 }
531
e67aae79
MM
532 save_connectors = kzalloc(dev->mode_config.num_connector *
533 sizeof(struct drm_connector), GFP_KERNEL);
534 if (!save_connectors) {
535 kfree(save_crtcs);
536 kfree(save_encoders);
537 return -ENOMEM;
538 }
539
540 /* Copy data. Note that driver private data is not affected.
541 * Should anything bad happen only the expected state is
542 * restored, not the drivers personal bookkeeping.
543 */
544 count = 0;
545 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
546 save_crtcs[count++] = *crtc;
547 }
548
549 count = 0;
550 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
551 save_encoders[count++] = *encoder;
552 }
553
554 count = 0;
555 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
556 save_connectors[count++] = *connector;
557 }
558
f453ba04
DA
559 /* We should be able to check here if the fb has the same properties
560 * and then just flip_or_move it */
561 if (set->crtc->fb != set->fb) {
712531bf 562 /* If we have no fb then treat it as a full mode set */
7bec756c 563 if (set->crtc->fb == NULL) {
58367ed6 564 DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
712531bf 565 mode_changed = true;
4cb72b17
JB
566 } else if (set->fb == NULL) {
567 mode_changed = true;
46e48456
JB
568 } else if (set->fb->depth != set->crtc->fb->depth) {
569 mode_changed = true;
570 } else if (set->fb->bits_per_pixel !=
571 set->crtc->fb->bits_per_pixel) {
572 mode_changed = true;
8dff4742 573 } else
712531bf 574 fb_changed = true;
f453ba04
DA
575 }
576
577 if (set->x != set->crtc->x || set->y != set->crtc->y)
712531bf 578 fb_changed = true;
f453ba04
DA
579
580 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
58367ed6 581 DRM_DEBUG_KMS("modes are different, full mode set\n");
f453ba04
DA
582 drm_mode_debug_printmodeline(&set->crtc->mode);
583 drm_mode_debug_printmodeline(set->mode);
712531bf 584 mode_changed = true;
f453ba04
DA
585 }
586
587 /* a) traverse passed in connector list and get encoders for them */
588 count = 0;
589 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
590 struct drm_connector_helper_funcs *connector_funcs =
591 connector->helper_private;
f453ba04
DA
592 new_encoder = connector->encoder;
593 for (ro = 0; ro < set->num_connectors; ro++) {
594 if (set->connectors[ro] == connector) {
595 new_encoder = connector_funcs->best_encoder(connector);
596 /* if we can't get an encoder for a connector
597 we are setting now - then fail */
598 if (new_encoder == NULL)
599 /* don't break so fail path works correct */
600 fail = 1;
601 break;
602 }
603 }
604
605 if (new_encoder != connector->encoder) {
58367ed6 606 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
712531bf 607 mode_changed = true;
ff846ab7
MM
608 /* If the encoder is reused for another connector, then
609 * the appropriate crtc will be set later.
610 */
ff6fdbed
MM
611 if (connector->encoder)
612 connector->encoder->crtc = NULL;
f453ba04
DA
613 connector->encoder = new_encoder;
614 }
615 }
616
617 if (fail) {
618 ret = -EINVAL;
e67aae79 619 goto fail;
f453ba04
DA
620 }
621
622 count = 0;
623 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
624 if (!connector->encoder)
625 continue;
626
f453ba04
DA
627 if (connector->encoder->crtc == set->crtc)
628 new_crtc = NULL;
629 else
630 new_crtc = connector->encoder->crtc;
631
632 for (ro = 0; ro < set->num_connectors; ro++) {
633 if (set->connectors[ro] == connector)
634 new_crtc = set->crtc;
635 }
7bec756c
JB
636
637 /* Make sure the new CRTC will work with the encoder */
638 if (new_crtc &&
639 !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
640 ret = -EINVAL;
e67aae79 641 goto fail;
7bec756c 642 }
f453ba04 643 if (new_crtc != connector->encoder->crtc) {
58367ed6 644 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
712531bf 645 mode_changed = true;
f453ba04
DA
646 connector->encoder->crtc = new_crtc;
647 }
9440106b
JG
648 if (new_crtc) {
649 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
650 connector->base.id, drm_get_connector_name(connector),
651 new_crtc->base.id);
652 } else {
653 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
654 connector->base.id, drm_get_connector_name(connector));
655 }
f453ba04
DA
656 }
657
658 /* mode_set_base is not a required function */
712531bf
JB
659 if (fb_changed && !crtc_funcs->mode_set_base)
660 mode_changed = true;
f453ba04 661
712531bf 662 if (mode_changed) {
9334ef75
CW
663 set->crtc->enabled = drm_helper_crtc_in_use(set->crtc);
664 if (set->crtc->enabled) {
58367ed6
ZY
665 DRM_DEBUG_KMS("attempting to set mode from"
666 " userspace\n");
f453ba04 667 drm_mode_debug_printmodeline(set->mode);
356ad3cd
CW
668 old_fb = set->crtc->fb;
669 set->crtc->fb = set->fb;
f453ba04 670 if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
3c4fdcfb
KH
671 set->x, set->y,
672 old_fb)) {
9440106b
JG
673 DRM_ERROR("failed to set mode on [CRTC:%d]\n",
674 set->crtc->base.id);
0ba41e44 675 set->crtc->fb = old_fb;
f453ba04 676 ret = -EINVAL;
e67aae79 677 goto fail;
f453ba04 678 }
811aaa55
KP
679 DRM_DEBUG_KMS("Setting connector DPMS state to on\n");
680 for (i = 0; i < set->num_connectors; i++) {
681 DRM_DEBUG_KMS("\t[CONNECTOR:%d:%s] set DPMS on\n", set->connectors[i]->base.id,
682 drm_get_connector_name(set->connectors[i]));
683 set->connectors[i]->dpms = DRM_MODE_DPMS_ON;
684 }
f453ba04
DA
685 }
686 drm_helper_disable_unused_functions(dev);
712531bf 687 } else if (fb_changed) {
9b1596af
BS
688 set->crtc->x = set->x;
689 set->crtc->y = set->y;
690
3c4fdcfb 691 old_fb = set->crtc->fb;
f453ba04
DA
692 if (set->crtc->fb != set->fb)
693 set->crtc->fb = set->fb;
5c3b82e2
CW
694 ret = crtc_funcs->mode_set_base(set->crtc,
695 set->x, set->y, old_fb);
0ba41e44
CW
696 if (ret != 0) {
697 set->crtc->fb = old_fb;
e67aae79 698 goto fail;
0ba41e44 699 }
f453ba04
DA
700 }
701
e67aae79 702 kfree(save_connectors);
f453ba04
DA
703 kfree(save_encoders);
704 kfree(save_crtcs);
705 return 0;
706
e67aae79
MM
707fail:
708 /* Restore all previous data. */
f453ba04 709 count = 0;
e67aae79
MM
710 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
711 *crtc = save_crtcs[count++];
712 }
e62fb64e 713
e67aae79
MM
714 count = 0;
715 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
716 *encoder = save_encoders[count++];
e62fb64e 717 }
e67aae79 718
f453ba04
DA
719 count = 0;
720 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
e67aae79 721 *connector = save_connectors[count++];
f453ba04 722 }
e67aae79
MM
723
724 kfree(save_connectors);
f453ba04 725 kfree(save_encoders);
e67aae79 726 kfree(save_crtcs);
f453ba04
DA
727 return ret;
728}
729EXPORT_SYMBOL(drm_crtc_helper_set_config);
730
c9fb15f6
KP
731static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder)
732{
733 int dpms = DRM_MODE_DPMS_OFF;
734 struct drm_connector *connector;
735 struct drm_device *dev = encoder->dev;
736
737 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
738 if (connector->encoder == encoder)
739 if (connector->dpms < dpms)
740 dpms = connector->dpms;
741 return dpms;
742}
743
744static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc)
745{
746 int dpms = DRM_MODE_DPMS_OFF;
747 struct drm_connector *connector;
748 struct drm_device *dev = crtc->dev;
749
750 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
751 if (connector->encoder && connector->encoder->crtc == crtc)
752 if (connector->dpms < dpms)
753 dpms = connector->dpms;
754 return dpms;
755}
756
757/**
758 * drm_helper_connector_dpms
759 * @connector affected connector
760 * @mode DPMS mode
761 *
762 * Calls the low-level connector DPMS function, then
763 * calls appropriate encoder and crtc DPMS functions as well
764 */
765void drm_helper_connector_dpms(struct drm_connector *connector, int mode)
766{
767 struct drm_encoder *encoder = connector->encoder;
768 struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
769 int old_dpms;
770
771 if (mode == connector->dpms)
772 return;
773
774 old_dpms = connector->dpms;
775 connector->dpms = mode;
776
777 /* from off to on, do crtc then encoder */
778 if (mode < old_dpms) {
779 if (crtc) {
780 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
781 if (crtc_funcs->dpms)
782 (*crtc_funcs->dpms) (crtc,
783 drm_helper_choose_crtc_dpms(crtc));
784 }
785 if (encoder) {
786 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
787 if (encoder_funcs->dpms)
788 (*encoder_funcs->dpms) (encoder,
789 drm_helper_choose_encoder_dpms(encoder));
790 }
791 }
792
793 /* from on to off, do encoder then crtc */
794 if (mode > old_dpms) {
795 if (encoder) {
796 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
797 if (encoder_funcs->dpms)
798 (*encoder_funcs->dpms) (encoder,
799 drm_helper_choose_encoder_dpms(encoder));
800 }
801 if (crtc) {
802 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
803 if (crtc_funcs->dpms)
804 (*crtc_funcs->dpms) (crtc,
805 drm_helper_choose_crtc_dpms(crtc));
806 }
807 }
808
809 return;
810}
811EXPORT_SYMBOL(drm_helper_connector_dpms);
812
f453ba04
DA
813int drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb,
814 struct drm_mode_fb_cmd *mode_cmd)
815{
816 fb->width = mode_cmd->width;
817 fb->height = mode_cmd->height;
818 fb->pitch = mode_cmd->pitch;
819 fb->bits_per_pixel = mode_cmd->bpp;
820 fb->depth = mode_cmd->depth;
821
822 return 0;
823}
824EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
825
826int drm_helper_resume_force_mode(struct drm_device *dev)
827{
828 struct drm_crtc *crtc;
89347bb8
DJ
829 struct drm_encoder *encoder;
830 struct drm_encoder_helper_funcs *encoder_funcs;
831 struct drm_crtc_helper_funcs *crtc_funcs;
f453ba04
DA
832 int ret;
833
834 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
835
836 if (!crtc->enabled)
837 continue;
838
3c4fdcfb
KH
839 ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
840 crtc->x, crtc->y, crtc->fb);
f453ba04
DA
841
842 if (ret == false)
843 DRM_ERROR("failed to set mode on crtc %p\n", crtc);
89347bb8
DJ
844
845 /* Turn off outputs that were already powered off */
846 if (drm_helper_choose_crtc_dpms(crtc)) {
847 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
848
849 if(encoder->crtc != crtc)
850 continue;
851
852 encoder_funcs = encoder->helper_private;
853 if (encoder_funcs->dpms)
854 (*encoder_funcs->dpms) (encoder,
855 drm_helper_choose_encoder_dpms(encoder));
89347bb8 856 }
817e631e
CW
857
858 crtc_funcs = crtc->helper_private;
859 if (crtc_funcs->dpms)
860 (*crtc_funcs->dpms) (crtc,
861 drm_helper_choose_crtc_dpms(crtc));
89347bb8 862 }
f453ba04 863 }
af4fcb57
ZY
864 /* disable the unused connectors while restoring the modesetting */
865 drm_helper_disable_unused_functions(dev);
f453ba04
DA
866 return 0;
867}
868EXPORT_SYMBOL(drm_helper_resume_force_mode);
eb1f8e4f 869
eb1f8e4f 870#define DRM_OUTPUT_POLL_PERIOD (10*HZ)
991ea75c 871static void output_poll_execute(struct work_struct *work)
eb1f8e4f 872{
991ea75c
TH
873 struct delayed_work *delayed_work = to_delayed_work(work);
874 struct drm_device *dev = container_of(delayed_work, struct drm_device, mode_config.output_poll_work);
eb1f8e4f 875 struct drm_connector *connector;
c5027dec 876 enum drm_connector_status old_status;
eb1f8e4f 877 bool repoll = false, changed = false;
eb1f8e4f 878
e58f637b
CW
879 if (!drm_kms_helper_poll)
880 return;
881
eb1f8e4f
DA
882 mutex_lock(&dev->mode_config.mutex);
883 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
884
885 /* if this is HPD or polled don't check it -
886 TV out for instance */
887 if (!connector->polled)
888 continue;
889
890 else if (connector->polled & (DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT))
891 repoll = true;
892
893 old_status = connector->status;
894 /* if we are connected and don't want to poll for disconnect
895 skip it */
896 if (old_status == connector_status_connected &&
897 !(connector->polled & DRM_CONNECTOR_POLL_DISCONNECT) &&
898 !(connector->polled & DRM_CONNECTOR_POLL_HPD))
899 continue;
900
c5027dec 901 connector->status = connector->funcs->detect(connector, false);
0f16830e
CW
902 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %d to %d\n",
903 connector->base.id,
904 drm_get_connector_name(connector),
905 old_status, connector->status);
c5027dec 906 if (old_status != connector->status)
eb1f8e4f
DA
907 changed = true;
908 }
909
910 mutex_unlock(&dev->mode_config.mutex);
911
912 if (changed) {
913 /* send a uevent + call fbdev */
914 drm_sysfs_hotplug_event(dev);
915 if (dev->mode_config.funcs->output_poll_changed)
916 dev->mode_config.funcs->output_poll_changed(dev);
917 }
918
9a919c46
TH
919 if (repoll)
920 queue_delayed_work(system_nrt_wq, delayed_work, DRM_OUTPUT_POLL_PERIOD);
eb1f8e4f
DA
921}
922
fbf81762
DA
923void drm_kms_helper_poll_disable(struct drm_device *dev)
924{
925 if (!dev->mode_config.poll_enabled)
926 return;
991ea75c 927 cancel_delayed_work_sync(&dev->mode_config.output_poll_work);
fbf81762
DA
928}
929EXPORT_SYMBOL(drm_kms_helper_poll_disable);
930
931void drm_kms_helper_poll_enable(struct drm_device *dev)
eb1f8e4f 932{
eb1f8e4f 933 bool poll = false;
fbf81762 934 struct drm_connector *connector;
eb1f8e4f 935
e58f637b
CW
936 if (!dev->mode_config.poll_enabled || !drm_kms_helper_poll)
937 return;
938
eb1f8e4f
DA
939 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
940 if (connector->polled)
941 poll = true;
942 }
eb1f8e4f 943
9a919c46
TH
944 if (poll)
945 queue_delayed_work(system_nrt_wq, &dev->mode_config.output_poll_work, DRM_OUTPUT_POLL_PERIOD);
eb1f8e4f 946}
fbf81762
DA
947EXPORT_SYMBOL(drm_kms_helper_poll_enable);
948
949void drm_kms_helper_poll_init(struct drm_device *dev)
950{
991ea75c 951 INIT_DELAYED_WORK(&dev->mode_config.output_poll_work, output_poll_execute);
fbf81762
DA
952 dev->mode_config.poll_enabled = true;
953
954 drm_kms_helper_poll_enable(dev);
955}
eb1f8e4f
DA
956EXPORT_SYMBOL(drm_kms_helper_poll_init);
957
958void drm_kms_helper_poll_fini(struct drm_device *dev)
959{
fbf81762 960 drm_kms_helper_poll_disable(dev);
eb1f8e4f
DA
961}
962EXPORT_SYMBOL(drm_kms_helper_poll_fini);
963
964void drm_helper_hpd_irq_event(struct drm_device *dev)
965{
966 if (!dev->mode_config.poll_enabled)
967 return;
e58f637b 968
991ea75c
TH
969 /* kill timer and schedule immediate execution, this doesn't block */
970 cancel_delayed_work(&dev->mode_config.output_poll_work);
e58f637b
CW
971 if (drm_kms_helper_poll)
972 queue_delayed_work(system_nrt_wq, &dev->mode_config.output_poll_work, 0);
eb1f8e4f
DA
973}
974EXPORT_SYMBOL(drm_helper_hpd_irq_event);
This page took 0.210221 seconds and 5 git commands to generate.