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