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