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