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