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