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