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