xref: /openbmc/linux/drivers/gpu/drm/i915/display/intel_panel.c (revision f43e47c090dc7fe32d5410d8740c3a004eb2676f)
1 /*
2  * Copyright © 2006-2010 Intel Corporation
3  * Copyright (c) 2006 Dave Airlie <airlied@linux.ie>
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  *
24  * Authors:
25  *	Eric Anholt <eric@anholt.net>
26  *      Dave Airlie <airlied@linux.ie>
27  *      Jesse Barnes <jesse.barnes@intel.com>
28  *      Chris Wilson <chris@chris-wilson.co.uk>
29  */
30 
31 #include <linux/kernel.h>
32 #include <linux/pwm.h>
33 
34 #include "intel_backlight.h"
35 #include "intel_connector.h"
36 #include "intel_de.h"
37 #include "intel_display_types.h"
38 #include "intel_drrs.h"
39 #include "intel_panel.h"
40 #include "intel_quirks.h"
41 
42 bool intel_panel_use_ssc(struct drm_i915_private *i915)
43 {
44 	if (i915->params.panel_use_ssc >= 0)
45 		return i915->params.panel_use_ssc != 0;
46 	return i915->display.vbt.lvds_use_ssc &&
47 		!intel_has_quirk(i915, QUIRK_LVDS_SSC_DISABLE);
48 }
49 
50 const struct drm_display_mode *
51 intel_panel_preferred_fixed_mode(struct intel_connector *connector)
52 {
53 	return list_first_entry_or_null(&connector->panel.fixed_modes,
54 					struct drm_display_mode, head);
55 }
56 
57 const struct drm_display_mode *
58 intel_panel_fixed_mode(struct intel_connector *connector,
59 		       const struct drm_display_mode *mode)
60 {
61 	const struct drm_display_mode *fixed_mode, *best_mode = NULL;
62 	int vrefresh = drm_mode_vrefresh(mode);
63 
64 	/* pick the fixed_mode that is closest in terms of vrefresh */
65 	list_for_each_entry(fixed_mode, &connector->panel.fixed_modes, head) {
66 		if (!best_mode ||
67 		    abs(drm_mode_vrefresh(fixed_mode) - vrefresh) <
68 		    abs(drm_mode_vrefresh(best_mode) - vrefresh))
69 			best_mode = fixed_mode;
70 	}
71 
72 	return best_mode;
73 }
74 
75 static bool is_alt_drrs_mode(const struct drm_display_mode *mode,
76 			     const struct drm_display_mode *preferred_mode)
77 {
78 	return drm_mode_match(mode, preferred_mode,
79 			      DRM_MODE_MATCH_TIMINGS |
80 			      DRM_MODE_MATCH_FLAGS |
81 			      DRM_MODE_MATCH_3D_FLAGS) &&
82 		mode->clock != preferred_mode->clock;
83 }
84 
85 static bool is_alt_fixed_mode(const struct drm_display_mode *mode,
86 			      const struct drm_display_mode *preferred_mode)
87 {
88 	u32 sync_flags = DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC |
89 		DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC;
90 
91 	return (mode->flags & ~sync_flags) == (preferred_mode->flags & ~sync_flags) &&
92 		mode->hdisplay == preferred_mode->hdisplay &&
93 		mode->vdisplay == preferred_mode->vdisplay;
94 }
95 
96 const struct drm_display_mode *
97 intel_panel_downclock_mode(struct intel_connector *connector,
98 			   const struct drm_display_mode *adjusted_mode)
99 {
100 	const struct drm_display_mode *fixed_mode, *best_mode = NULL;
101 	int min_vrefresh = connector->panel.vbt.seamless_drrs_min_refresh_rate;
102 	int max_vrefresh = drm_mode_vrefresh(adjusted_mode);
103 
104 	/* pick the fixed_mode with the lowest refresh rate */
105 	list_for_each_entry(fixed_mode, &connector->panel.fixed_modes, head) {
106 		int vrefresh = drm_mode_vrefresh(fixed_mode);
107 
108 		if (is_alt_drrs_mode(fixed_mode, adjusted_mode) &&
109 		    vrefresh >= min_vrefresh && vrefresh < max_vrefresh) {
110 			max_vrefresh = vrefresh;
111 			best_mode = fixed_mode;
112 		}
113 	}
114 
115 	return best_mode;
116 }
117 
118 const struct drm_display_mode *
119 intel_panel_highest_mode(struct intel_connector *connector,
120 			 const struct drm_display_mode *adjusted_mode)
121 {
122 	const struct drm_display_mode *fixed_mode, *best_mode = adjusted_mode;
123 
124 	/* pick the fixed_mode that has the highest clock */
125 	list_for_each_entry(fixed_mode, &connector->panel.fixed_modes, head) {
126 		if (fixed_mode->clock > best_mode->clock)
127 			best_mode = fixed_mode;
128 	}
129 
130 	return best_mode;
131 }
132 
133 int intel_panel_get_modes(struct intel_connector *connector)
134 {
135 	const struct drm_display_mode *fixed_mode;
136 	int num_modes = 0;
137 
138 	list_for_each_entry(fixed_mode, &connector->panel.fixed_modes, head) {
139 		struct drm_display_mode *mode;
140 
141 		mode = drm_mode_duplicate(connector->base.dev, fixed_mode);
142 		if (mode) {
143 			drm_mode_probed_add(&connector->base, mode);
144 			num_modes++;
145 		}
146 	}
147 
148 	return num_modes;
149 }
150 
151 static bool has_drrs_modes(struct intel_connector *connector)
152 {
153 	const struct drm_display_mode *mode1;
154 
155 	list_for_each_entry(mode1, &connector->panel.fixed_modes, head) {
156 		const struct drm_display_mode *mode2 = mode1;
157 
158 		list_for_each_entry_continue(mode2, &connector->panel.fixed_modes, head) {
159 			if (is_alt_drrs_mode(mode1, mode2))
160 				return true;
161 		}
162 	}
163 
164 	return false;
165 }
166 
167 enum drrs_type intel_panel_drrs_type(struct intel_connector *connector)
168 {
169 	return connector->panel.vbt.drrs_type;
170 }
171 
172 int intel_panel_compute_config(struct intel_connector *connector,
173 			       struct drm_display_mode *adjusted_mode)
174 {
175 	const struct drm_display_mode *fixed_mode =
176 		intel_panel_fixed_mode(connector, adjusted_mode);
177 
178 	if (!fixed_mode)
179 		return 0;
180 
181 	/*
182 	 * We don't want to lie too much to the user about the refresh
183 	 * rate they're going to get. But we have to allow a bit of latitude
184 	 * for Xorg since it likes to automagically cook up modes with slightly
185 	 * off refresh rates.
186 	 */
187 	if (abs(drm_mode_vrefresh(adjusted_mode) - drm_mode_vrefresh(fixed_mode)) > 1) {
188 		drm_dbg_kms(connector->base.dev,
189 			    "[CONNECTOR:%d:%s] Requested mode vrefresh (%d Hz) does not match fixed mode vrefresh (%d Hz)\n",
190 			    connector->base.base.id, connector->base.name,
191 			    drm_mode_vrefresh(adjusted_mode), drm_mode_vrefresh(fixed_mode));
192 
193 		return -EINVAL;
194 	}
195 
196 	drm_mode_copy(adjusted_mode, fixed_mode);
197 
198 	drm_mode_set_crtcinfo(adjusted_mode, 0);
199 
200 	return 0;
201 }
202 
203 static void intel_panel_add_edid_alt_fixed_modes(struct intel_connector *connector)
204 {
205 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
206 	const struct drm_display_mode *preferred_mode =
207 		intel_panel_preferred_fixed_mode(connector);
208 	struct drm_display_mode *mode, *next;
209 
210 	list_for_each_entry_safe(mode, next, &connector->base.probed_modes, head) {
211 		if (!is_alt_fixed_mode(mode, preferred_mode))
212 			continue;
213 
214 		drm_dbg_kms(&dev_priv->drm,
215 			    "[CONNECTOR:%d:%s] using alternate EDID fixed mode: " DRM_MODE_FMT "\n",
216 			    connector->base.base.id, connector->base.name,
217 			    DRM_MODE_ARG(mode));
218 
219 		list_move_tail(&mode->head, &connector->panel.fixed_modes);
220 	}
221 }
222 
223 static void intel_panel_add_edid_preferred_mode(struct intel_connector *connector)
224 {
225 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
226 	struct drm_display_mode *scan, *fixed_mode = NULL;
227 
228 	if (list_empty(&connector->base.probed_modes))
229 		return;
230 
231 	/* make sure the preferred mode is first */
232 	list_for_each_entry(scan, &connector->base.probed_modes, head) {
233 		if (scan->type & DRM_MODE_TYPE_PREFERRED) {
234 			fixed_mode = scan;
235 			break;
236 		}
237 	}
238 
239 	if (!fixed_mode)
240 		fixed_mode = list_first_entry(&connector->base.probed_modes,
241 					      typeof(*fixed_mode), head);
242 
243 	drm_dbg_kms(&dev_priv->drm,
244 		    "[CONNECTOR:%d:%s] using %s EDID fixed mode: " DRM_MODE_FMT "\n",
245 		    connector->base.base.id, connector->base.name,
246 		    fixed_mode->type & DRM_MODE_TYPE_PREFERRED ? "preferred" : "first",
247 		    DRM_MODE_ARG(fixed_mode));
248 
249 	fixed_mode->type |= DRM_MODE_TYPE_PREFERRED;
250 
251 	list_move_tail(&fixed_mode->head, &connector->panel.fixed_modes);
252 }
253 
254 static void intel_panel_destroy_probed_modes(struct intel_connector *connector)
255 {
256 	struct drm_i915_private *i915 = to_i915(connector->base.dev);
257 	struct drm_display_mode *mode, *next;
258 
259 	list_for_each_entry_safe(mode, next, &connector->base.probed_modes, head) {
260 		drm_dbg_kms(&i915->drm,
261 			    "[CONNECTOR:%d:%s] not using EDID mode: " DRM_MODE_FMT "\n",
262 			    connector->base.base.id, connector->base.name,
263 			    DRM_MODE_ARG(mode));
264 		list_del(&mode->head);
265 		drm_mode_destroy(&i915->drm, mode);
266 	}
267 }
268 
269 void intel_panel_add_edid_fixed_modes(struct intel_connector *connector,
270 				      bool use_alt_fixed_modes)
271 {
272 	intel_panel_add_edid_preferred_mode(connector);
273 	if (intel_panel_preferred_fixed_mode(connector) && use_alt_fixed_modes)
274 		intel_panel_add_edid_alt_fixed_modes(connector);
275 	intel_panel_destroy_probed_modes(connector);
276 }
277 
278 static void intel_panel_add_fixed_mode(struct intel_connector *connector,
279 				       struct drm_display_mode *fixed_mode,
280 				       const char *type)
281 {
282 	struct drm_i915_private *i915 = to_i915(connector->base.dev);
283 	struct drm_display_info *info = &connector->base.display_info;
284 
285 	if (!fixed_mode)
286 		return;
287 
288 	fixed_mode->type |= DRM_MODE_TYPE_PREFERRED | DRM_MODE_TYPE_DRIVER;
289 
290 	info->width_mm = fixed_mode->width_mm;
291 	info->height_mm = fixed_mode->height_mm;
292 
293 	drm_dbg_kms(&i915->drm, "[CONNECTOR:%d:%s] using %s fixed mode: " DRM_MODE_FMT "\n",
294 		    connector->base.base.id, connector->base.name, type,
295 		    DRM_MODE_ARG(fixed_mode));
296 
297 	list_add_tail(&fixed_mode->head, &connector->panel.fixed_modes);
298 }
299 
300 void intel_panel_add_vbt_lfp_fixed_mode(struct intel_connector *connector)
301 {
302 	struct drm_i915_private *i915 = to_i915(connector->base.dev);
303 	const struct drm_display_mode *mode;
304 
305 	mode = connector->panel.vbt.lfp_lvds_vbt_mode;
306 	if (!mode)
307 		return;
308 
309 	intel_panel_add_fixed_mode(connector,
310 				   drm_mode_duplicate(&i915->drm, mode),
311 				   "VBT LFP");
312 }
313 
314 void intel_panel_add_vbt_sdvo_fixed_mode(struct intel_connector *connector)
315 {
316 	struct drm_i915_private *i915 = to_i915(connector->base.dev);
317 	const struct drm_display_mode *mode;
318 
319 	mode = connector->panel.vbt.sdvo_lvds_vbt_mode;
320 	if (!mode)
321 		return;
322 
323 	intel_panel_add_fixed_mode(connector,
324 				   drm_mode_duplicate(&i915->drm, mode),
325 				   "VBT SDVO");
326 }
327 
328 void intel_panel_add_encoder_fixed_mode(struct intel_connector *connector,
329 					struct intel_encoder *encoder)
330 {
331 	intel_panel_add_fixed_mode(connector,
332 				   intel_encoder_current_mode(encoder),
333 				   "current (BIOS)");
334 }
335 
336 /* adjusted_mode has been preset to be the panel's fixed mode */
337 static int pch_panel_fitting(struct intel_crtc_state *crtc_state,
338 			     const struct drm_connector_state *conn_state)
339 {
340 	const struct drm_display_mode *adjusted_mode =
341 		&crtc_state->hw.adjusted_mode;
342 	int pipe_src_w = drm_rect_width(&crtc_state->pipe_src);
343 	int pipe_src_h = drm_rect_height(&crtc_state->pipe_src);
344 	int x, y, width, height;
345 
346 	/* Native modes don't need fitting */
347 	if (adjusted_mode->crtc_hdisplay == pipe_src_w &&
348 	    adjusted_mode->crtc_vdisplay == pipe_src_h &&
349 	    crtc_state->output_format != INTEL_OUTPUT_FORMAT_YCBCR420)
350 		return 0;
351 
352 	switch (conn_state->scaling_mode) {
353 	case DRM_MODE_SCALE_CENTER:
354 		width = pipe_src_w;
355 		height = pipe_src_h;
356 		x = (adjusted_mode->crtc_hdisplay - width + 1)/2;
357 		y = (adjusted_mode->crtc_vdisplay - height + 1)/2;
358 		break;
359 
360 	case DRM_MODE_SCALE_ASPECT:
361 		/* Scale but preserve the aspect ratio */
362 		{
363 			u32 scaled_width = adjusted_mode->crtc_hdisplay * pipe_src_h;
364 			u32 scaled_height = pipe_src_w * adjusted_mode->crtc_vdisplay;
365 			if (scaled_width > scaled_height) { /* pillar */
366 				width = scaled_height / pipe_src_h;
367 				if (width & 1)
368 					width++;
369 				x = (adjusted_mode->crtc_hdisplay - width + 1) / 2;
370 				y = 0;
371 				height = adjusted_mode->crtc_vdisplay;
372 			} else if (scaled_width < scaled_height) { /* letter */
373 				height = scaled_width / pipe_src_w;
374 				if (height & 1)
375 				    height++;
376 				y = (adjusted_mode->crtc_vdisplay - height + 1) / 2;
377 				x = 0;
378 				width = adjusted_mode->crtc_hdisplay;
379 			} else {
380 				x = y = 0;
381 				width = adjusted_mode->crtc_hdisplay;
382 				height = adjusted_mode->crtc_vdisplay;
383 			}
384 		}
385 		break;
386 
387 	case DRM_MODE_SCALE_NONE:
388 		WARN_ON(adjusted_mode->crtc_hdisplay != pipe_src_w);
389 		WARN_ON(adjusted_mode->crtc_vdisplay != pipe_src_h);
390 		fallthrough;
391 	case DRM_MODE_SCALE_FULLSCREEN:
392 		x = y = 0;
393 		width = adjusted_mode->crtc_hdisplay;
394 		height = adjusted_mode->crtc_vdisplay;
395 		break;
396 
397 	default:
398 		MISSING_CASE(conn_state->scaling_mode);
399 		return -EINVAL;
400 	}
401 
402 	drm_rect_init(&crtc_state->pch_pfit.dst,
403 		      x, y, width, height);
404 	crtc_state->pch_pfit.enabled = true;
405 
406 	return 0;
407 }
408 
409 static void
410 centre_horizontally(struct drm_display_mode *adjusted_mode,
411 		    int width)
412 {
413 	u32 border, sync_pos, blank_width, sync_width;
414 
415 	/* keep the hsync and hblank widths constant */
416 	sync_width = adjusted_mode->crtc_hsync_end - adjusted_mode->crtc_hsync_start;
417 	blank_width = adjusted_mode->crtc_hblank_end - adjusted_mode->crtc_hblank_start;
418 	sync_pos = (blank_width - sync_width + 1) / 2;
419 
420 	border = (adjusted_mode->crtc_hdisplay - width + 1) / 2;
421 	border += border & 1; /* make the border even */
422 
423 	adjusted_mode->crtc_hdisplay = width;
424 	adjusted_mode->crtc_hblank_start = width + border;
425 	adjusted_mode->crtc_hblank_end = adjusted_mode->crtc_hblank_start + blank_width;
426 
427 	adjusted_mode->crtc_hsync_start = adjusted_mode->crtc_hblank_start + sync_pos;
428 	adjusted_mode->crtc_hsync_end = adjusted_mode->crtc_hsync_start + sync_width;
429 }
430 
431 static void
432 centre_vertically(struct drm_display_mode *adjusted_mode,
433 		  int height)
434 {
435 	u32 border, sync_pos, blank_width, sync_width;
436 
437 	/* keep the vsync and vblank widths constant */
438 	sync_width = adjusted_mode->crtc_vsync_end - adjusted_mode->crtc_vsync_start;
439 	blank_width = adjusted_mode->crtc_vblank_end - adjusted_mode->crtc_vblank_start;
440 	sync_pos = (blank_width - sync_width + 1) / 2;
441 
442 	border = (adjusted_mode->crtc_vdisplay - height + 1) / 2;
443 
444 	adjusted_mode->crtc_vdisplay = height;
445 	adjusted_mode->crtc_vblank_start = height + border;
446 	adjusted_mode->crtc_vblank_end = adjusted_mode->crtc_vblank_start + blank_width;
447 
448 	adjusted_mode->crtc_vsync_start = adjusted_mode->crtc_vblank_start + sync_pos;
449 	adjusted_mode->crtc_vsync_end = adjusted_mode->crtc_vsync_start + sync_width;
450 }
451 
452 static u32 panel_fitter_scaling(u32 source, u32 target)
453 {
454 	/*
455 	 * Floating point operation is not supported. So the FACTOR
456 	 * is defined, which can avoid the floating point computation
457 	 * when calculating the panel ratio.
458 	 */
459 #define ACCURACY 12
460 #define FACTOR (1 << ACCURACY)
461 	u32 ratio = source * FACTOR / target;
462 	return (FACTOR * ratio + FACTOR/2) / FACTOR;
463 }
464 
465 static void i965_scale_aspect(struct intel_crtc_state *crtc_state,
466 			      u32 *pfit_control)
467 {
468 	const struct drm_display_mode *adjusted_mode =
469 		&crtc_state->hw.adjusted_mode;
470 	int pipe_src_w = drm_rect_width(&crtc_state->pipe_src);
471 	int pipe_src_h = drm_rect_height(&crtc_state->pipe_src);
472 	u32 scaled_width = adjusted_mode->crtc_hdisplay * pipe_src_h;
473 	u32 scaled_height = pipe_src_w * adjusted_mode->crtc_vdisplay;
474 
475 	/* 965+ is easy, it does everything in hw */
476 	if (scaled_width > scaled_height)
477 		*pfit_control |= PFIT_ENABLE |
478 			PFIT_SCALING_PILLAR;
479 	else if (scaled_width < scaled_height)
480 		*pfit_control |= PFIT_ENABLE |
481 			PFIT_SCALING_LETTER;
482 	else if (adjusted_mode->crtc_hdisplay != pipe_src_w)
483 		*pfit_control |= PFIT_ENABLE | PFIT_SCALING_AUTO;
484 }
485 
486 static void i9xx_scale_aspect(struct intel_crtc_state *crtc_state,
487 			      u32 *pfit_control, u32 *pfit_pgm_ratios,
488 			      u32 *border)
489 {
490 	struct drm_display_mode *adjusted_mode = &crtc_state->hw.adjusted_mode;
491 	int pipe_src_w = drm_rect_width(&crtc_state->pipe_src);
492 	int pipe_src_h = drm_rect_height(&crtc_state->pipe_src);
493 	u32 scaled_width = adjusted_mode->crtc_hdisplay * pipe_src_h;
494 	u32 scaled_height = pipe_src_w * adjusted_mode->crtc_vdisplay;
495 	u32 bits;
496 
497 	/*
498 	 * For earlier chips we have to calculate the scaling
499 	 * ratio by hand and program it into the
500 	 * PFIT_PGM_RATIO register
501 	 */
502 	if (scaled_width > scaled_height) { /* pillar */
503 		centre_horizontally(adjusted_mode,
504 				    scaled_height / pipe_src_h);
505 
506 		*border = LVDS_BORDER_ENABLE;
507 		if (pipe_src_h != adjusted_mode->crtc_vdisplay) {
508 			bits = panel_fitter_scaling(pipe_src_h,
509 						    adjusted_mode->crtc_vdisplay);
510 
511 			*pfit_pgm_ratios |= (bits << PFIT_HORIZ_SCALE_SHIFT |
512 					     bits << PFIT_VERT_SCALE_SHIFT);
513 			*pfit_control |= (PFIT_ENABLE |
514 					  VERT_INTERP_BILINEAR |
515 					  HORIZ_INTERP_BILINEAR);
516 		}
517 	} else if (scaled_width < scaled_height) { /* letter */
518 		centre_vertically(adjusted_mode,
519 				  scaled_width / pipe_src_w);
520 
521 		*border = LVDS_BORDER_ENABLE;
522 		if (pipe_src_w != adjusted_mode->crtc_hdisplay) {
523 			bits = panel_fitter_scaling(pipe_src_w,
524 						    adjusted_mode->crtc_hdisplay);
525 
526 			*pfit_pgm_ratios |= (bits << PFIT_HORIZ_SCALE_SHIFT |
527 					     bits << PFIT_VERT_SCALE_SHIFT);
528 			*pfit_control |= (PFIT_ENABLE |
529 					  VERT_INTERP_BILINEAR |
530 					  HORIZ_INTERP_BILINEAR);
531 		}
532 	} else {
533 		/* Aspects match, Let hw scale both directions */
534 		*pfit_control |= (PFIT_ENABLE |
535 				  VERT_AUTO_SCALE | HORIZ_AUTO_SCALE |
536 				  VERT_INTERP_BILINEAR |
537 				  HORIZ_INTERP_BILINEAR);
538 	}
539 }
540 
541 static int gmch_panel_fitting(struct intel_crtc_state *crtc_state,
542 			      const struct drm_connector_state *conn_state)
543 {
544 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
545 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
546 	u32 pfit_control = 0, pfit_pgm_ratios = 0, border = 0;
547 	struct drm_display_mode *adjusted_mode = &crtc_state->hw.adjusted_mode;
548 	int pipe_src_w = drm_rect_width(&crtc_state->pipe_src);
549 	int pipe_src_h = drm_rect_height(&crtc_state->pipe_src);
550 
551 	/* Native modes don't need fitting */
552 	if (adjusted_mode->crtc_hdisplay == pipe_src_w &&
553 	    adjusted_mode->crtc_vdisplay == pipe_src_h)
554 		goto out;
555 
556 	switch (conn_state->scaling_mode) {
557 	case DRM_MODE_SCALE_CENTER:
558 		/*
559 		 * For centered modes, we have to calculate border widths &
560 		 * heights and modify the values programmed into the CRTC.
561 		 */
562 		centre_horizontally(adjusted_mode, pipe_src_w);
563 		centre_vertically(adjusted_mode, pipe_src_h);
564 		border = LVDS_BORDER_ENABLE;
565 		break;
566 	case DRM_MODE_SCALE_ASPECT:
567 		/* Scale but preserve the aspect ratio */
568 		if (DISPLAY_VER(dev_priv) >= 4)
569 			i965_scale_aspect(crtc_state, &pfit_control);
570 		else
571 			i9xx_scale_aspect(crtc_state, &pfit_control,
572 					  &pfit_pgm_ratios, &border);
573 		break;
574 	case DRM_MODE_SCALE_FULLSCREEN:
575 		/*
576 		 * Full scaling, even if it changes the aspect ratio.
577 		 * Fortunately this is all done for us in hw.
578 		 */
579 		if (pipe_src_h != adjusted_mode->crtc_vdisplay ||
580 		    pipe_src_w != adjusted_mode->crtc_hdisplay) {
581 			pfit_control |= PFIT_ENABLE;
582 			if (DISPLAY_VER(dev_priv) >= 4)
583 				pfit_control |= PFIT_SCALING_AUTO;
584 			else
585 				pfit_control |= (VERT_AUTO_SCALE |
586 						 VERT_INTERP_BILINEAR |
587 						 HORIZ_AUTO_SCALE |
588 						 HORIZ_INTERP_BILINEAR);
589 		}
590 		break;
591 	default:
592 		MISSING_CASE(conn_state->scaling_mode);
593 		return -EINVAL;
594 	}
595 
596 	/* 965+ wants fuzzy fitting */
597 	/* FIXME: handle multiple panels by failing gracefully */
598 	if (DISPLAY_VER(dev_priv) >= 4)
599 		pfit_control |= PFIT_PIPE(crtc->pipe) | PFIT_FILTER_FUZZY;
600 
601 out:
602 	if ((pfit_control & PFIT_ENABLE) == 0) {
603 		pfit_control = 0;
604 		pfit_pgm_ratios = 0;
605 	}
606 
607 	/* Make sure pre-965 set dither correctly for 18bpp panels. */
608 	if (DISPLAY_VER(dev_priv) < 4 && crtc_state->pipe_bpp == 18)
609 		pfit_control |= PANEL_8TO6_DITHER_ENABLE;
610 
611 	crtc_state->gmch_pfit.control = pfit_control;
612 	crtc_state->gmch_pfit.pgm_ratios = pfit_pgm_ratios;
613 	crtc_state->gmch_pfit.lvds_border_bits = border;
614 
615 	return 0;
616 }
617 
618 int intel_panel_fitting(struct intel_crtc_state *crtc_state,
619 			const struct drm_connector_state *conn_state)
620 {
621 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
622 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
623 
624 	if (HAS_GMCH(i915))
625 		return gmch_panel_fitting(crtc_state, conn_state);
626 	else
627 		return pch_panel_fitting(crtc_state, conn_state);
628 }
629 
630 enum drm_connector_status
631 intel_panel_detect(struct drm_connector *connector, bool force)
632 {
633 	struct drm_i915_private *i915 = to_i915(connector->dev);
634 
635 	if (!INTEL_DISPLAY_ENABLED(i915))
636 		return connector_status_disconnected;
637 
638 	return connector_status_connected;
639 }
640 
641 enum drm_mode_status
642 intel_panel_mode_valid(struct intel_connector *connector,
643 		       const struct drm_display_mode *mode)
644 {
645 	const struct drm_display_mode *fixed_mode =
646 		intel_panel_fixed_mode(connector, mode);
647 
648 	if (!fixed_mode)
649 		return MODE_OK;
650 
651 	if (mode->hdisplay != fixed_mode->hdisplay)
652 		return MODE_PANEL;
653 
654 	if (mode->vdisplay != fixed_mode->vdisplay)
655 		return MODE_PANEL;
656 
657 	if (drm_mode_vrefresh(mode) != drm_mode_vrefresh(fixed_mode))
658 		return MODE_PANEL;
659 
660 	return MODE_OK;
661 }
662 
663 int intel_panel_init(struct intel_connector *connector)
664 {
665 	struct intel_panel *panel = &connector->panel;
666 
667 	intel_backlight_init_funcs(panel);
668 
669 	if (!has_drrs_modes(connector))
670 		connector->panel.vbt.drrs_type = DRRS_TYPE_NONE;
671 
672 	drm_dbg_kms(connector->base.dev,
673 		    "[CONNECTOR:%d:%s] DRRS type: %s\n",
674 		    connector->base.base.id, connector->base.name,
675 		    intel_drrs_type_str(intel_panel_drrs_type(connector)));
676 
677 	return 0;
678 }
679 
680 void intel_panel_fini(struct intel_connector *connector)
681 {
682 	struct intel_panel *panel = &connector->panel;
683 	struct drm_display_mode *fixed_mode, *next;
684 
685 	intel_backlight_destroy(panel);
686 
687 	intel_bios_fini_panel(panel);
688 
689 	list_for_each_entry_safe(fixed_mode, next, &panel->fixed_modes, head) {
690 		list_del(&fixed_mode->head);
691 		drm_mode_destroy(connector->base.dev, fixed_mode);
692 	}
693 }
694