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