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