xref: /linux/drivers/gpu/drm/drm_crtc_helper.c (revision b43ab901d671e3e3cad425ea5e9a3c74e266dcdd)
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, *t;
48 
49 	if (flags == (DRM_MODE_FLAG_DBLSCAN | DRM_MODE_FLAG_INTERLACE))
50 		return;
51 
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;
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, *t;
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_safe(mode, t, &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_safe(mode, t, &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_safe(mode, t, &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 
356 	saved_hwmode = crtc->hwmode;
357 	saved_mode = crtc->mode;
358 	saved_x = crtc->x;
359 	saved_y = crtc->y;
360 
361 	/* Update crtc values up front so the driver can rely on them for mode
362 	 * setting.
363 	 */
364 	crtc->mode = *mode;
365 	crtc->x = x;
366 	crtc->y = y;
367 
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.
371 	 */
372 	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
373 
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;
381 		}
382 	}
383 
384 	if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) {
385 		DRM_DEBUG_KMS("CRTC fixup failed\n");
386 		goto done;
387 	}
388 	DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
389 
390 	/* Prepare the encoders and CRTCs before setting the mode. */
391 	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
392 
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);
398 	}
399 
400 	drm_crtc_prepare_encoders(dev);
401 
402 	crtc_funcs->prepare(crtc);
403 
404 	/* Set up the DPLL and any encoders state that needs to adjust or depend
405 	 * on the DPLL.
406 	 */
407 	ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
408 	if (!ret)
409 	    goto done;
410 
411 	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
412 
413 		if (encoder->crtc != crtc)
414 			continue;
415 
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);
421 	}
422 
423 	/* Now enable the clocks, plane, pipe, and connectors that we set up. */
424 	crtc_funcs->commit(crtc);
425 
426 	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
427 
428 		if (encoder->crtc != crtc)
429 			continue;
430 
431 		encoder_funcs = encoder->helper_private;
432 		encoder_funcs->commit(encoder);
433 
434 	}
435 
436 	/* Store real post-adjustment hardware mode. */
437 	crtc->hwmode = *adjusted_mode;
438 
439 	/* Calculate and store various constants which
440 	 * are later needed by vblank and swap-completion
441 	 * timestamping. They are derived from true hwmode.
442 	 */
443 	drm_calc_timestamping_constants(crtc);
444 
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;
453 	}
454 
455 	return ret;
456 }
457 EXPORT_SYMBOL(drm_crtc_helper_set_mode);
458 
459 
460 static int
461 drm_crtc_helper_disable(struct drm_crtc *crtc)
462 {
463 	struct drm_device *dev = crtc->dev;
464 	struct drm_connector *connector;
465 	struct drm_encoder *encoder;
466 
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;
471 
472 		list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
473 			if (connector->encoder != encoder)
474 				continue;
475 
476 			connector->encoder = NULL;
477 		}
478 	}
479 
480 	drm_helper_disable_unused_functions(dev);
481 	return 0;
482 }
483 
484 /**
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
491  *
492  * LOCKING:
493  * Caller must hold mode config lock.
494  *
495  * Setup a new configuration, provided by the user in @crtc_info, and enable
496  * it.
497  *
498  * RETURNS:
499  * Zero. (FIXME)
500  */
501 int drm_crtc_helper_set_config(struct drm_mode_set *set)
502 {
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;
515 
516 	DRM_DEBUG_KMS("\n");
517 
518 	if (!set)
519 		return -EINVAL;
520 
521 	if (!set->crtc)
522 		return -EINVAL;
523 
524 	if (!set->crtc->helper_private)
525 		return -EINVAL;
526 
527 	crtc_funcs = set->crtc->helper_private;
528 
529 	if (!set->mode)
530 		set->fb = NULL;
531 
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);
539 	}
540 
541 	dev = set->crtc->dev;
542 
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;
549 
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;
555 	}
556 
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;
563 	}
564 
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.
568 	 */
569 	count = 0;
570 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
571 		save_crtcs[count++] = *crtc;
572 	}
573 
574 	count = 0;
575 	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
576 		save_encoders[count++] = *encoder;
577 	}
578 
579 	count = 0;
580 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
581 		save_connectors[count++] = *connector;
582 	}
583 
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;
589 
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;
606 	}
607 
608 	if (set->x != set->crtc->x || set->y != set->crtc->y)
609 		fb_changed = true;
610 
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;
616 	}
617 
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;
633 			}
634 		}
635 
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.
641 			 */
642 			if (connector->encoder)
643 				connector->encoder->crtc = NULL;
644 			connector->encoder = new_encoder;
645 		}
646 	}
647 
648 	if (fail) {
649 		ret = -EINVAL;
650 		goto fail;
651 	}
652 
653 	count = 0;
654 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
655 		if (!connector->encoder)
656 			continue;
657 
658 		if (connector->encoder->crtc == set->crtc)
659 			new_crtc = NULL;
660 		else
661 			new_crtc = connector->encoder->crtc;
662 
663 		for (ro = 0; ro < set->num_connectors; ro++) {
664 			if (set->connectors[ro] == connector)
665 				new_crtc = set->crtc;
666 		}
667 
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;
673 		}
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;
678 		}
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));
686 		}
687 	}
688 
689 	/* mode_set_base is not a required function */
690 	if (fb_changed && !crtc_funcs->mode_set_base)
691 		mode_changed = true;
692 
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;
709 			}
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);
715 			}
716 		}
717 		drm_helper_disable_unused_functions(dev);
718 	} else if (fb_changed) {
719 		set->crtc->x = set->x;
720 		set->crtc->y = set->y;
721 
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;
730 		}
731 	}
732 
733 	kfree(save_connectors);
734 	kfree(save_encoders);
735 	kfree(save_crtcs);
736 	return 0;
737 
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++];
743 	}
744 
745 	count = 0;
746 	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
747 		*encoder = save_encoders[count++];
748 	}
749 
750 	count = 0;
751 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
752 		*connector = save_connectors[count++];
753 	}
754 
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");
760 
761 	kfree(save_connectors);
762 	kfree(save_encoders);
763 	kfree(save_crtcs);
764 	return ret;
765 }
766 EXPORT_SYMBOL(drm_crtc_helper_set_config);
767 
768 static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder)
769 {
770 	int dpms = DRM_MODE_DPMS_OFF;
771 	struct drm_connector *connector;
772 	struct drm_device *dev = encoder->dev;
773 
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;
779 }
780 
781 static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc)
782 {
783 	int dpms = DRM_MODE_DPMS_OFF;
784 	struct drm_connector *connector;
785 	struct drm_device *dev = crtc->dev;
786 
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;
792 }
793 
794 /**
795  * drm_helper_connector_dpms
796  * @connector affected connector
797  * @mode DPMS mode
798  *
799  * Calls the low-level connector DPMS function, then
800  * calls appropriate encoder and crtc DPMS functions as well
801  */
802 void drm_helper_connector_dpms(struct drm_connector *connector, int mode)
803 {
804 	struct drm_encoder *encoder = connector->encoder;
805 	struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
806 	int old_dpms;
807 
808 	if (mode == connector->dpms)
809 		return;
810 
811 	old_dpms = connector->dpms;
812 	connector->dpms = mode;
813 
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));
821 		}
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));
827 		}
828 	}
829 
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));
837 		}
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));
843 		}
844 	}
845 
846 	return;
847 }
848 EXPORT_SYMBOL(drm_helper_connector_dpms);
849 
850 int drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb,
851 				   struct drm_mode_fb_cmd2 *mode_cmd)
852 {
853 	int i;
854 
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];
860 	}
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;
864 
865 	return 0;
866 }
867 EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
868 
869 int drm_helper_resume_force_mode(struct drm_device *dev)
870 {
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;
876 
877 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
878 
879 		if (!crtc->enabled)
880 			continue;
881 
882 		ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
883 					       crtc->x, crtc->y, crtc->fb);
884 
885 		if (ret == false)
886 			DRM_ERROR("failed to set mode on crtc %p\n", crtc);
887 
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) {
891 
892 				if(encoder->crtc != crtc)
893 					continue;
894 
895 				encoder_funcs = encoder->helper_private;
896 				if (encoder_funcs->dpms)
897 					(*encoder_funcs->dpms) (encoder,
898 								drm_helper_choose_encoder_dpms(encoder));
899 			}
900 
901 			crtc_funcs = crtc->helper_private;
902 			if (crtc_funcs->dpms)
903 				(*crtc_funcs->dpms) (crtc,
904 						     drm_helper_choose_crtc_dpms(crtc));
905 		}
906 	}
907 	/* disable the unused connectors while restoring the modesetting */
908 	drm_helper_disable_unused_functions(dev);
909 	return 0;
910 }
911 EXPORT_SYMBOL(drm_helper_resume_force_mode);
912 
913 #define DRM_OUTPUT_POLL_PERIOD (10*HZ)
914 static void output_poll_execute(struct work_struct *work)
915 {
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;
921 
922 	if (!drm_kms_helper_poll)
923 		return;
924 
925 	mutex_lock(&dev->mode_config.mutex);
926 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
927 
928 		/* if this is HPD or polled don't check it -
929 		   TV out for instance */
930 		if (!connector->polled)
931 			continue;
932 
933 		else if (connector->polled & (DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT))
934 			repoll = true;
935 
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;
943 
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;
951 	}
952 
953 	mutex_unlock(&dev->mode_config.mutex);
954 
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);
960 	}
961 
962 	if (repoll)
963 		queue_delayed_work(system_nrt_wq, delayed_work, DRM_OUTPUT_POLL_PERIOD);
964 }
965 
966 void drm_kms_helper_poll_disable(struct drm_device *dev)
967 {
968 	if (!dev->mode_config.poll_enabled)
969 		return;
970 	cancel_delayed_work_sync(&dev->mode_config.output_poll_work);
971 }
972 EXPORT_SYMBOL(drm_kms_helper_poll_disable);
973 
974 void drm_kms_helper_poll_enable(struct drm_device *dev)
975 {
976 	bool poll = false;
977 	struct drm_connector *connector;
978 
979 	if (!dev->mode_config.poll_enabled || !drm_kms_helper_poll)
980 		return;
981 
982 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
983 		if (connector->polled)
984 			poll = true;
985 	}
986 
987 	if (poll)
988 		queue_delayed_work(system_nrt_wq, &dev->mode_config.output_poll_work, DRM_OUTPUT_POLL_PERIOD);
989 }
990 EXPORT_SYMBOL(drm_kms_helper_poll_enable);
991 
992 void drm_kms_helper_poll_init(struct drm_device *dev)
993 {
994 	INIT_DELAYED_WORK(&dev->mode_config.output_poll_work, output_poll_execute);
995 	dev->mode_config.poll_enabled = true;
996 
997 	drm_kms_helper_poll_enable(dev);
998 }
999 EXPORT_SYMBOL(drm_kms_helper_poll_init);
1000 
1001 void drm_kms_helper_poll_fini(struct drm_device *dev)
1002 {
1003 	drm_kms_helper_poll_disable(dev);
1004 }
1005 EXPORT_SYMBOL(drm_kms_helper_poll_fini);
1006 
1007 void drm_helper_hpd_irq_event(struct drm_device *dev)
1008 {
1009 	if (!dev->mode_config.poll_enabled)
1010 		return;
1011 
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);
1016 }
1017 EXPORT_SYMBOL(drm_helper_hpd_irq_event);
1018 
1019 
1020 /**
1021  * drm_format_num_planes - get the number of planes for format
1022  * @format: pixel format (DRM_FORMAT_*)
1023  *
1024  * RETURNS:
1025  * The number of planes used by the specified pixel format.
1026  */
1027 int drm_format_num_planes(uint32_t format)
1028 {
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;
1048 	}
1049 }
1050 EXPORT_SYMBOL(drm_format_num_planes);
1051