Linux Audio

Check our new training course

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