ASoC: tlv312aic23: unbreak resume
[zen-stable.git] / drivers / gpu / drm / drm_crtc_helper.c
blob84a4a809793ff7bcf7fdf68e9d28555eb5e5b8ba
1 /*
2 * Copyright (c) 2006-2008 Intel Corporation
3 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
5 * DRM core CRTC related functions
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.
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.
25 * Authors:
26 * Keith Packard
27 * Eric Anholt <eric@anholt.net>
28 * Dave Airlie <airlied@linux.ie>
29 * Jesse Barnes <jesse.barnes@intel.com>
32 #include <linux/export.h>
33 #include <linux/moduleparam.h>
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"
41 static bool drm_kms_helper_poll = true;
42 module_param_named(poll, drm_kms_helper_poll, bool, 0600);
44 static void drm_mode_validate_flag(struct drm_connector *connector,
45 int flags)
47 struct drm_display_mode *mode, *t;
49 if (flags == (DRM_MODE_FLAG_DBLSCAN | DRM_MODE_FLAG_INTERLACE))
50 return;
52 list_for_each_entry_safe(mode, t, &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;
61 return;
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
70 * LOCKING:
71 * Caller must hold mode config lock.
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.
78 * Intended to be used either at bootup time or when major configuration
79 * changes have occurred.
81 * FIXME: take into account monitor limits
83 * RETURNS:
84 * Number of modes found on @connector.
86 int drm_helper_probe_single_connector_modes(struct drm_connector *connector,
87 uint32_t maxX, uint32_t maxY)
89 struct drm_device *dev = connector->dev;
90 struct drm_display_mode *mode, *t;
91 struct drm_connector_helper_funcs *connector_funcs =
92 connector->helper_private;
93 int count = 0;
94 int mode_flags = 0;
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_safe(mode, t, &connector->modes, head)
100 mode->status = MODE_UNVERIFIED;
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);
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;
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;
127 drm_mode_connector_list_update(connector);
129 if (maxX && maxY)
130 drm_mode_validate_size(dev, &connector->modes, maxX,
131 maxY, 0);
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);
139 list_for_each_entry_safe(mode, t, &connector->modes, head) {
140 if (mode->status == MODE_OK)
141 mode->status = connector_funcs->mode_valid(connector,
142 mode);
145 prune:
146 drm_mode_prune_invalid(dev, &connector->modes, true);
148 if (list_empty(&connector->modes))
149 return 0;
151 drm_mode_sort(&connector->modes);
153 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] probed modes :\n", connector->base.id,
154 drm_get_connector_name(connector));
155 list_for_each_entry_safe(mode, t, &connector->modes, head) {
156 mode->vrefresh = drm_mode_vrefresh(mode);
158 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
159 drm_mode_debug_printmodeline(mode);
162 return count;
164 EXPORT_SYMBOL(drm_helper_probe_single_connector_modes);
167 * drm_helper_encoder_in_use - check if a given encoder is in use
168 * @encoder: encoder to check
170 * LOCKING:
171 * Caller must hold mode config lock.
173 * Walk @encoders's DRM device's mode_config and see if it's in use.
175 * RETURNS:
176 * True if @encoder is part of the mode_config, false otherwise.
178 bool drm_helper_encoder_in_use(struct drm_encoder *encoder)
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;
187 EXPORT_SYMBOL(drm_helper_encoder_in_use);
190 * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config
191 * @crtc: CRTC to check
193 * LOCKING:
194 * Caller must hold mode config lock.
196 * Walk @crtc's DRM device's mode_config and see if it's in use.
198 * RETURNS:
199 * True if @crtc is part of the mode_config, false otherwise.
201 bool drm_helper_crtc_in_use(struct drm_crtc *crtc)
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;
211 EXPORT_SYMBOL(drm_helper_crtc_in_use);
213 static void
214 drm_encoder_disable(struct drm_encoder *encoder)
216 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
218 if (encoder_funcs->disable)
219 (*encoder_funcs->disable)(encoder);
220 else
221 (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
225 * drm_helper_disable_unused_functions - disable unused objects
226 * @dev: DRM device
228 * LOCKING:
229 * Caller must hold mode config lock.
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.
234 void drm_helper_disable_unused_functions(struct drm_device *dev)
236 struct drm_encoder *encoder;
237 struct drm_connector *connector;
238 struct drm_crtc *crtc;
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;
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;
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;
267 EXPORT_SYMBOL(drm_helper_disable_unused_functions);
270 * drm_encoder_crtc_ok - can a given crtc drive a given encoder?
271 * @encoder: encoder to test
272 * @crtc: crtc to test
274 * Return false if @encoder can't be driven by @crtc, true otherwise.
276 static bool drm_encoder_crtc_ok(struct drm_encoder *encoder,
277 struct drm_crtc *crtc)
279 struct drm_device *dev;
280 struct drm_crtc *tmp;
281 int crtc_mask = 1;
283 WARN(!crtc, "checking null crtc?\n");
285 dev = crtc->dev;
287 list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
288 if (tmp == crtc)
289 break;
290 crtc_mask <<= 1;
293 if (encoder->possible_crtcs & crtc_mask)
294 return true;
295 return false;
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.
303 static void
304 drm_crtc_prepare_encoders(struct drm_device *dev)
306 struct drm_encoder_helper_funcs *encoder_funcs;
307 struct drm_encoder *encoder;
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);
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
328 * LOCKING:
329 * Caller must hold mode config lock.
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.
334 * RETURNS:
335 * True if the mode was set successfully, or false otherwise.
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)
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;
350 crtc->enabled = drm_helper_crtc_in_use(crtc);
351 if (!crtc->enabled)
352 return true;
354 adjusted_mode = drm_mode_duplicate(dev, mode);
356 saved_hwmode = crtc->hwmode;
357 saved_mode = crtc->mode;
358 saved_x = crtc->x;
359 saved_y = crtc->y;
361 /* Update crtc values up front so the driver can rely on them for mode
362 * setting.
364 crtc->mode = *mode;
365 crtc->x = x;
366 crtc->y = y;
368 /* Pass our mode to the connectors and the CRTC to give them a chance to
369 * adjust it according to limitations or connector properties, and also
370 * a chance to reject the mode entirely.
372 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
374 if (encoder->crtc != crtc)
375 continue;
376 encoder_funcs = encoder->helper_private;
377 if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
378 adjusted_mode))) {
379 DRM_DEBUG_KMS("Encoder fixup failed\n");
380 goto done;
384 if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) {
385 DRM_DEBUG_KMS("CRTC fixup failed\n");
386 goto done;
388 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
390 /* Prepare the encoders and CRTCs before setting the mode. */
391 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
393 if (encoder->crtc != crtc)
394 continue;
395 encoder_funcs = encoder->helper_private;
396 /* Disable the encoders as the first thing we do. */
397 encoder_funcs->prepare(encoder);
400 drm_crtc_prepare_encoders(dev);
402 crtc_funcs->prepare(crtc);
404 /* Set up the DPLL and any encoders state that needs to adjust or depend
405 * on the DPLL.
407 ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
408 if (!ret)
409 goto done;
411 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
413 if (encoder->crtc != crtc)
414 continue;
416 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
417 encoder->base.id, drm_get_encoder_name(encoder),
418 mode->base.id, mode->name);
419 encoder_funcs = encoder->helper_private;
420 encoder_funcs->mode_set(encoder, mode, adjusted_mode);
423 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
424 crtc_funcs->commit(crtc);
426 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
428 if (encoder->crtc != crtc)
429 continue;
431 encoder_funcs = encoder->helper_private;
432 encoder_funcs->commit(encoder);
436 /* Store real post-adjustment hardware mode. */
437 crtc->hwmode = *adjusted_mode;
439 /* Calculate and store various constants which
440 * are later needed by vblank and swap-completion
441 * timestamping. They are derived from true hwmode.
443 drm_calc_timestamping_constants(crtc);
445 /* FIXME: add subpixel order */
446 done:
447 drm_mode_destroy(dev, adjusted_mode);
448 if (!ret) {
449 crtc->hwmode = saved_hwmode;
450 crtc->mode = saved_mode;
451 crtc->x = saved_x;
452 crtc->y = saved_y;
455 return ret;
457 EXPORT_SYMBOL(drm_crtc_helper_set_mode);
460 static int
461 drm_crtc_helper_disable(struct drm_crtc *crtc)
463 struct drm_device *dev = crtc->dev;
464 struct drm_connector *connector;
465 struct drm_encoder *encoder;
467 /* Decouple all encoders and their attached connectors from this crtc */
468 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
469 if (encoder->crtc != crtc)
470 continue;
472 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
473 if (connector->encoder != encoder)
474 continue;
476 connector->encoder = NULL;
480 drm_helper_disable_unused_functions(dev);
481 return 0;
485 * drm_crtc_helper_set_config - set a new config from userspace
486 * @crtc: CRTC to setup
487 * @crtc_info: user provided configuration
488 * @new_mode: new mode to set
489 * @connector_set: set of connectors for the new config
490 * @fb: new framebuffer
492 * LOCKING:
493 * Caller must hold mode config lock.
495 * Setup a new configuration, provided by the user in @crtc_info, and enable
496 * it.
498 * RETURNS:
499 * Zero. (FIXME)
501 int drm_crtc_helper_set_config(struct drm_mode_set *set)
503 struct drm_device *dev;
504 struct drm_crtc *save_crtcs, *new_crtc, *crtc;
505 struct drm_encoder *save_encoders, *new_encoder, *encoder;
506 struct drm_framebuffer *old_fb = NULL;
507 bool mode_changed = false; /* if true do a full mode set */
508 bool fb_changed = false; /* if true and !mode_changed just do a flip */
509 struct drm_connector *save_connectors, *connector;
510 int count = 0, ro, fail = 0;
511 struct drm_crtc_helper_funcs *crtc_funcs;
512 struct drm_mode_set save_set;
513 int ret = 0;
514 int i;
516 DRM_DEBUG_KMS("\n");
518 if (!set)
519 return -EINVAL;
521 if (!set->crtc)
522 return -EINVAL;
524 if (!set->crtc->helper_private)
525 return -EINVAL;
527 crtc_funcs = set->crtc->helper_private;
529 if (!set->mode)
530 set->fb = NULL;
532 if (set->fb) {
533 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
534 set->crtc->base.id, set->fb->base.id,
535 (int)set->num_connectors, set->x, set->y);
536 } else {
537 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
538 return drm_crtc_helper_disable(set->crtc);
541 dev = set->crtc->dev;
543 /* Allocate space for the backup of all (non-pointer) crtc, encoder and
544 * connector data. */
545 save_crtcs = kzalloc(dev->mode_config.num_crtc *
546 sizeof(struct drm_crtc), GFP_KERNEL);
547 if (!save_crtcs)
548 return -ENOMEM;
550 save_encoders = kzalloc(dev->mode_config.num_encoder *
551 sizeof(struct drm_encoder), GFP_KERNEL);
552 if (!save_encoders) {
553 kfree(save_crtcs);
554 return -ENOMEM;
557 save_connectors = kzalloc(dev->mode_config.num_connector *
558 sizeof(struct drm_connector), GFP_KERNEL);
559 if (!save_connectors) {
560 kfree(save_crtcs);
561 kfree(save_encoders);
562 return -ENOMEM;
565 /* Copy data. Note that driver private data is not affected.
566 * Should anything bad happen only the expected state is
567 * restored, not the drivers personal bookkeeping.
569 count = 0;
570 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
571 save_crtcs[count++] = *crtc;
574 count = 0;
575 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
576 save_encoders[count++] = *encoder;
579 count = 0;
580 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
581 save_connectors[count++] = *connector;
584 save_set.crtc = set->crtc;
585 save_set.mode = &set->crtc->mode;
586 save_set.x = set->crtc->x;
587 save_set.y = set->crtc->y;
588 save_set.fb = set->crtc->fb;
590 /* We should be able to check here if the fb has the same properties
591 * and then just flip_or_move it */
592 if (set->crtc->fb != set->fb) {
593 /* If we have no fb then treat it as a full mode set */
594 if (set->crtc->fb == NULL) {
595 DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
596 mode_changed = true;
597 } else if (set->fb == NULL) {
598 mode_changed = true;
599 } else if (set->fb->depth != set->crtc->fb->depth) {
600 mode_changed = true;
601 } else if (set->fb->bits_per_pixel !=
602 set->crtc->fb->bits_per_pixel) {
603 mode_changed = true;
604 } else
605 fb_changed = true;
608 if (set->x != set->crtc->x || set->y != set->crtc->y)
609 fb_changed = true;
611 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
612 DRM_DEBUG_KMS("modes are different, full mode set\n");
613 drm_mode_debug_printmodeline(&set->crtc->mode);
614 drm_mode_debug_printmodeline(set->mode);
615 mode_changed = true;
618 /* a) traverse passed in connector list and get encoders for them */
619 count = 0;
620 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
621 struct drm_connector_helper_funcs *connector_funcs =
622 connector->helper_private;
623 new_encoder = connector->encoder;
624 for (ro = 0; ro < set->num_connectors; ro++) {
625 if (set->connectors[ro] == connector) {
626 new_encoder = connector_funcs->best_encoder(connector);
627 /* if we can't get an encoder for a connector
628 we are setting now - then fail */
629 if (new_encoder == NULL)
630 /* don't break so fail path works correct */
631 fail = 1;
632 break;
636 if (new_encoder != connector->encoder) {
637 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
638 mode_changed = true;
639 /* If the encoder is reused for another connector, then
640 * the appropriate crtc will be set later.
642 if (connector->encoder)
643 connector->encoder->crtc = NULL;
644 connector->encoder = new_encoder;
648 if (fail) {
649 ret = -EINVAL;
650 goto fail;
653 count = 0;
654 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
655 if (!connector->encoder)
656 continue;
658 if (connector->encoder->crtc == set->crtc)
659 new_crtc = NULL;
660 else
661 new_crtc = connector->encoder->crtc;
663 for (ro = 0; ro < set->num_connectors; ro++) {
664 if (set->connectors[ro] == connector)
665 new_crtc = set->crtc;
668 /* Make sure the new CRTC will work with the encoder */
669 if (new_crtc &&
670 !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
671 ret = -EINVAL;
672 goto fail;
674 if (new_crtc != connector->encoder->crtc) {
675 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
676 mode_changed = true;
677 connector->encoder->crtc = new_crtc;
679 if (new_crtc) {
680 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
681 connector->base.id, drm_get_connector_name(connector),
682 new_crtc->base.id);
683 } else {
684 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
685 connector->base.id, drm_get_connector_name(connector));
689 /* mode_set_base is not a required function */
690 if (fb_changed && !crtc_funcs->mode_set_base)
691 mode_changed = true;
693 if (mode_changed) {
694 set->crtc->enabled = drm_helper_crtc_in_use(set->crtc);
695 if (set->crtc->enabled) {
696 DRM_DEBUG_KMS("attempting to set mode from"
697 " userspace\n");
698 drm_mode_debug_printmodeline(set->mode);
699 old_fb = set->crtc->fb;
700 set->crtc->fb = set->fb;
701 if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
702 set->x, set->y,
703 old_fb)) {
704 DRM_ERROR("failed to set mode on [CRTC:%d]\n",
705 set->crtc->base.id);
706 set->crtc->fb = old_fb;
707 ret = -EINVAL;
708 goto fail;
710 DRM_DEBUG_KMS("Setting connector DPMS state to on\n");
711 for (i = 0; i < set->num_connectors; i++) {
712 DRM_DEBUG_KMS("\t[CONNECTOR:%d:%s] set DPMS on\n", set->connectors[i]->base.id,
713 drm_get_connector_name(set->connectors[i]));
714 set->connectors[i]->funcs->dpms(set->connectors[i], DRM_MODE_DPMS_ON);
717 drm_helper_disable_unused_functions(dev);
718 } else if (fb_changed) {
719 set->crtc->x = set->x;
720 set->crtc->y = set->y;
722 old_fb = set->crtc->fb;
723 if (set->crtc->fb != set->fb)
724 set->crtc->fb = set->fb;
725 ret = crtc_funcs->mode_set_base(set->crtc,
726 set->x, set->y, old_fb);
727 if (ret != 0) {
728 set->crtc->fb = old_fb;
729 goto fail;
733 kfree(save_connectors);
734 kfree(save_encoders);
735 kfree(save_crtcs);
736 return 0;
738 fail:
739 /* Restore all previous data. */
740 count = 0;
741 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
742 *crtc = save_crtcs[count++];
745 count = 0;
746 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
747 *encoder = save_encoders[count++];
750 count = 0;
751 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
752 *connector = save_connectors[count++];
755 /* Try to restore the config */
756 if (mode_changed &&
757 !drm_crtc_helper_set_mode(save_set.crtc, save_set.mode, save_set.x,
758 save_set.y, save_set.fb))
759 DRM_ERROR("failed to restore config after modeset failure\n");
761 kfree(save_connectors);
762 kfree(save_encoders);
763 kfree(save_crtcs);
764 return ret;
766 EXPORT_SYMBOL(drm_crtc_helper_set_config);
768 static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder)
770 int dpms = DRM_MODE_DPMS_OFF;
771 struct drm_connector *connector;
772 struct drm_device *dev = encoder->dev;
774 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
775 if (connector->encoder == encoder)
776 if (connector->dpms < dpms)
777 dpms = connector->dpms;
778 return dpms;
781 static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc)
783 int dpms = DRM_MODE_DPMS_OFF;
784 struct drm_connector *connector;
785 struct drm_device *dev = crtc->dev;
787 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
788 if (connector->encoder && connector->encoder->crtc == crtc)
789 if (connector->dpms < dpms)
790 dpms = connector->dpms;
791 return dpms;
795 * drm_helper_connector_dpms
796 * @connector affected connector
797 * @mode DPMS mode
799 * Calls the low-level connector DPMS function, then
800 * calls appropriate encoder and crtc DPMS functions as well
802 void drm_helper_connector_dpms(struct drm_connector *connector, int mode)
804 struct drm_encoder *encoder = connector->encoder;
805 struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
806 int old_dpms;
808 if (mode == connector->dpms)
809 return;
811 old_dpms = connector->dpms;
812 connector->dpms = mode;
814 /* from off to on, do crtc then encoder */
815 if (mode < old_dpms) {
816 if (crtc) {
817 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
818 if (crtc_funcs->dpms)
819 (*crtc_funcs->dpms) (crtc,
820 drm_helper_choose_crtc_dpms(crtc));
822 if (encoder) {
823 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
824 if (encoder_funcs->dpms)
825 (*encoder_funcs->dpms) (encoder,
826 drm_helper_choose_encoder_dpms(encoder));
830 /* from on to off, do encoder then crtc */
831 if (mode > old_dpms) {
832 if (encoder) {
833 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
834 if (encoder_funcs->dpms)
835 (*encoder_funcs->dpms) (encoder,
836 drm_helper_choose_encoder_dpms(encoder));
838 if (crtc) {
839 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
840 if (crtc_funcs->dpms)
841 (*crtc_funcs->dpms) (crtc,
842 drm_helper_choose_crtc_dpms(crtc));
846 return;
848 EXPORT_SYMBOL(drm_helper_connector_dpms);
850 int drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb,
851 struct drm_mode_fb_cmd2 *mode_cmd)
853 int i;
855 fb->width = mode_cmd->width;
856 fb->height = mode_cmd->height;
857 for (i = 0; i < 4; i++) {
858 fb->pitches[i] = mode_cmd->pitches[i];
859 fb->offsets[i] = mode_cmd->offsets[i];
861 drm_fb_get_bpp_depth(mode_cmd->pixel_format, &fb->depth,
862 &fb->bits_per_pixel);
863 fb->pixel_format = mode_cmd->pixel_format;
865 return 0;
867 EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
869 int drm_helper_resume_force_mode(struct drm_device *dev)
871 struct drm_crtc *crtc;
872 struct drm_encoder *encoder;
873 struct drm_encoder_helper_funcs *encoder_funcs;
874 struct drm_crtc_helper_funcs *crtc_funcs;
875 int ret;
877 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
879 if (!crtc->enabled)
880 continue;
882 ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
883 crtc->x, crtc->y, crtc->fb);
885 if (ret == false)
886 DRM_ERROR("failed to set mode on crtc %p\n", crtc);
888 /* Turn off outputs that were already powered off */
889 if (drm_helper_choose_crtc_dpms(crtc)) {
890 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
892 if(encoder->crtc != crtc)
893 continue;
895 encoder_funcs = encoder->helper_private;
896 if (encoder_funcs->dpms)
897 (*encoder_funcs->dpms) (encoder,
898 drm_helper_choose_encoder_dpms(encoder));
901 crtc_funcs = crtc->helper_private;
902 if (crtc_funcs->dpms)
903 (*crtc_funcs->dpms) (crtc,
904 drm_helper_choose_crtc_dpms(crtc));
907 /* disable the unused connectors while restoring the modesetting */
908 drm_helper_disable_unused_functions(dev);
909 return 0;
911 EXPORT_SYMBOL(drm_helper_resume_force_mode);
913 #define DRM_OUTPUT_POLL_PERIOD (10*HZ)
914 static void output_poll_execute(struct work_struct *work)
916 struct delayed_work *delayed_work = to_delayed_work(work);
917 struct drm_device *dev = container_of(delayed_work, struct drm_device, mode_config.output_poll_work);
918 struct drm_connector *connector;
919 enum drm_connector_status old_status;
920 bool repoll = false, changed = false;
922 if (!drm_kms_helper_poll)
923 return;
925 mutex_lock(&dev->mode_config.mutex);
926 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
928 /* if this is HPD or polled don't check it -
929 TV out for instance */
930 if (!connector->polled)
931 continue;
933 else if (connector->polled & (DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT))
934 repoll = true;
936 old_status = connector->status;
937 /* if we are connected and don't want to poll for disconnect
938 skip it */
939 if (old_status == connector_status_connected &&
940 !(connector->polled & DRM_CONNECTOR_POLL_DISCONNECT) &&
941 !(connector->polled & DRM_CONNECTOR_POLL_HPD))
942 continue;
944 connector->status = connector->funcs->detect(connector, false);
945 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %d to %d\n",
946 connector->base.id,
947 drm_get_connector_name(connector),
948 old_status, connector->status);
949 if (old_status != connector->status)
950 changed = true;
953 mutex_unlock(&dev->mode_config.mutex);
955 if (changed) {
956 /* send a uevent + call fbdev */
957 drm_sysfs_hotplug_event(dev);
958 if (dev->mode_config.funcs->output_poll_changed)
959 dev->mode_config.funcs->output_poll_changed(dev);
962 if (repoll)
963 queue_delayed_work(system_nrt_wq, delayed_work, DRM_OUTPUT_POLL_PERIOD);
966 void drm_kms_helper_poll_disable(struct drm_device *dev)
968 if (!dev->mode_config.poll_enabled)
969 return;
970 cancel_delayed_work_sync(&dev->mode_config.output_poll_work);
972 EXPORT_SYMBOL(drm_kms_helper_poll_disable);
974 void drm_kms_helper_poll_enable(struct drm_device *dev)
976 bool poll = false;
977 struct drm_connector *connector;
979 if (!dev->mode_config.poll_enabled || !drm_kms_helper_poll)
980 return;
982 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
983 if (connector->polled)
984 poll = true;
987 if (poll)
988 queue_delayed_work(system_nrt_wq, &dev->mode_config.output_poll_work, DRM_OUTPUT_POLL_PERIOD);
990 EXPORT_SYMBOL(drm_kms_helper_poll_enable);
992 void drm_kms_helper_poll_init(struct drm_device *dev)
994 INIT_DELAYED_WORK(&dev->mode_config.output_poll_work, output_poll_execute);
995 dev->mode_config.poll_enabled = true;
997 drm_kms_helper_poll_enable(dev);
999 EXPORT_SYMBOL(drm_kms_helper_poll_init);
1001 void drm_kms_helper_poll_fini(struct drm_device *dev)
1003 drm_kms_helper_poll_disable(dev);
1005 EXPORT_SYMBOL(drm_kms_helper_poll_fini);
1007 void drm_helper_hpd_irq_event(struct drm_device *dev)
1009 if (!dev->mode_config.poll_enabled)
1010 return;
1012 /* kill timer and schedule immediate execution, this doesn't block */
1013 cancel_delayed_work(&dev->mode_config.output_poll_work);
1014 if (drm_kms_helper_poll)
1015 queue_delayed_work(system_nrt_wq, &dev->mode_config.output_poll_work, 0);
1017 EXPORT_SYMBOL(drm_helper_hpd_irq_event);
1021 * drm_format_num_planes - get the number of planes for format
1022 * @format: pixel format (DRM_FORMAT_*)
1024 * RETURNS:
1025 * The number of planes used by the specified pixel format.
1027 int drm_format_num_planes(uint32_t format)
1029 switch (format) {
1030 case DRM_FORMAT_YUV410:
1031 case DRM_FORMAT_YVU410:
1032 case DRM_FORMAT_YUV411:
1033 case DRM_FORMAT_YVU411:
1034 case DRM_FORMAT_YUV420:
1035 case DRM_FORMAT_YVU420:
1036 case DRM_FORMAT_YUV422:
1037 case DRM_FORMAT_YVU422:
1038 case DRM_FORMAT_YUV444:
1039 case DRM_FORMAT_YVU444:
1040 return 3;
1041 case DRM_FORMAT_NV12:
1042 case DRM_FORMAT_NV21:
1043 case DRM_FORMAT_NV16:
1044 case DRM_FORMAT_NV61:
1045 return 2;
1046 default:
1047 return 1;
1050 EXPORT_SYMBOL(drm_format_num_planes);