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