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