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