2 * Copyright © 2006-2010 Intel Corporation
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:
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
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.
31 #include <linux/kernel.h>
32 #include <linux/pwm.h>
34 #include "intel_backlight.h"
35 #include "intel_connector.h"
37 #include "intel_display_types.h"
38 #include "intel_drrs.h"
39 #include "intel_panel.h"
40 #include "intel_quirks.h"
42 bool intel_panel_use_ssc(struct drm_i915_private *i915)
44 if (i915->params.panel_use_ssc >= 0)
45 return i915->params.panel_use_ssc != 0;
46 return i915->display.vbt.lvds_use_ssc &&
47 !intel_has_quirk(i915, QUIRK_LVDS_SSC_DISABLE);
50 const struct drm_display_mode *
51 intel_panel_preferred_fixed_mode(struct intel_connector *connector)
53 return list_first_entry_or_null(&connector->panel.fixed_modes,
54 struct drm_display_mode, head);
57 const struct drm_display_mode *
58 intel_panel_fixed_mode(struct intel_connector *connector,
59 const struct drm_display_mode *mode)
61 const struct drm_display_mode *fixed_mode, *best_mode = NULL;
62 int vrefresh = drm_mode_vrefresh(mode);
64 /* pick the fixed_mode that is closest in terms of vrefresh */
65 list_for_each_entry(fixed_mode, &connector->panel.fixed_modes, head) {
67 abs(drm_mode_vrefresh(fixed_mode) - vrefresh) <
68 abs(drm_mode_vrefresh(best_mode) - vrefresh))
69 best_mode = fixed_mode;
75 static bool is_alt_drrs_mode(const struct drm_display_mode *mode,
76 const struct drm_display_mode *preferred_mode)
78 return drm_mode_match(mode, preferred_mode,
79 DRM_MODE_MATCH_TIMINGS |
80 DRM_MODE_MATCH_FLAGS |
81 DRM_MODE_MATCH_3D_FLAGS) &&
82 mode->clock != preferred_mode->clock;
85 static bool is_alt_fixed_mode(const struct drm_display_mode *mode,
86 const struct drm_display_mode *preferred_mode)
88 return drm_mode_match(mode, preferred_mode,
89 DRM_MODE_MATCH_FLAGS |
90 DRM_MODE_MATCH_3D_FLAGS) &&
91 mode->hdisplay == preferred_mode->hdisplay &&
92 mode->vdisplay == preferred_mode->vdisplay;
95 const struct drm_display_mode *
96 intel_panel_downclock_mode(struct intel_connector *connector,
97 const struct drm_display_mode *adjusted_mode)
99 const struct drm_display_mode *fixed_mode, *best_mode = NULL;
100 int min_vrefresh = connector->panel.vbt.seamless_drrs_min_refresh_rate;
101 int max_vrefresh = drm_mode_vrefresh(adjusted_mode);
103 /* pick the fixed_mode with the lowest refresh rate */
104 list_for_each_entry(fixed_mode, &connector->panel.fixed_modes, head) {
105 int vrefresh = drm_mode_vrefresh(fixed_mode);
107 if (is_alt_drrs_mode(fixed_mode, adjusted_mode) &&
108 vrefresh >= min_vrefresh && vrefresh < max_vrefresh) {
109 max_vrefresh = vrefresh;
110 best_mode = fixed_mode;
117 const struct drm_display_mode *
118 intel_panel_highest_mode(struct intel_connector *connector,
119 const struct drm_display_mode *adjusted_mode)
121 const struct drm_display_mode *fixed_mode, *best_mode = adjusted_mode;
123 /* pick the fixed_mode that has the highest clock */
124 list_for_each_entry(fixed_mode, &connector->panel.fixed_modes, head) {
125 if (fixed_mode->clock > best_mode->clock)
126 best_mode = fixed_mode;
132 int intel_panel_get_modes(struct intel_connector *connector)
134 const struct drm_display_mode *fixed_mode;
137 list_for_each_entry(fixed_mode, &connector->panel.fixed_modes, head) {
138 struct drm_display_mode *mode;
140 mode = drm_mode_duplicate(connector->base.dev, fixed_mode);
142 drm_mode_probed_add(&connector->base, mode);
150 enum drrs_type intel_panel_drrs_type(struct intel_connector *connector)
152 if (list_empty(&connector->panel.fixed_modes) ||
153 list_is_singular(&connector->panel.fixed_modes))
154 return DRRS_TYPE_NONE;
156 return connector->panel.vbt.drrs_type;
159 int intel_panel_compute_config(struct intel_connector *connector,
160 struct drm_display_mode *adjusted_mode)
162 const struct drm_display_mode *fixed_mode =
163 intel_panel_fixed_mode(connector, adjusted_mode);
169 * We don't want to lie too much to the user about the refresh
170 * rate they're going to get. But we have to allow a bit of latitude
171 * for Xorg since it likes to automagically cook up modes with slightly
174 if (abs(drm_mode_vrefresh(adjusted_mode) - drm_mode_vrefresh(fixed_mode)) > 1) {
175 drm_dbg_kms(connector->base.dev,
176 "[CONNECTOR:%d:%s] Requested mode vrefresh (%d Hz) does not match fixed mode vrefresh (%d Hz)\n",
177 connector->base.base.id, connector->base.name,
178 drm_mode_vrefresh(adjusted_mode), drm_mode_vrefresh(fixed_mode));
183 drm_mode_copy(adjusted_mode, fixed_mode);
185 drm_mode_set_crtcinfo(adjusted_mode, 0);
190 static void intel_panel_add_edid_alt_fixed_modes(struct intel_connector *connector)
192 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
193 const struct drm_display_mode *preferred_mode =
194 intel_panel_preferred_fixed_mode(connector);
195 struct drm_display_mode *mode, *next;
197 list_for_each_entry_safe(mode, next, &connector->base.probed_modes, head) {
198 if (!is_alt_fixed_mode(mode, preferred_mode))
201 drm_dbg_kms(&dev_priv->drm,
202 "[CONNECTOR:%d:%s] using alternate EDID fixed mode: " DRM_MODE_FMT "\n",
203 connector->base.base.id, connector->base.name,
206 list_move_tail(&mode->head, &connector->panel.fixed_modes);
210 static void intel_panel_add_edid_preferred_mode(struct intel_connector *connector)
212 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
213 struct drm_display_mode *scan, *fixed_mode = NULL;
215 if (list_empty(&connector->base.probed_modes))
218 /* make sure the preferred mode is first */
219 list_for_each_entry(scan, &connector->base.probed_modes, head) {
220 if (scan->type & DRM_MODE_TYPE_PREFERRED) {
227 fixed_mode = list_first_entry(&connector->base.probed_modes,
228 typeof(*fixed_mode), head);
230 drm_dbg_kms(&dev_priv->drm,
231 "[CONNECTOR:%d:%s] using %s EDID fixed mode: " DRM_MODE_FMT "\n",
232 connector->base.base.id, connector->base.name,
233 fixed_mode->type & DRM_MODE_TYPE_PREFERRED ? "preferred" : "first",
234 DRM_MODE_ARG(fixed_mode));
236 fixed_mode->type |= DRM_MODE_TYPE_PREFERRED;
238 list_move_tail(&fixed_mode->head, &connector->panel.fixed_modes);
241 static void intel_panel_destroy_probed_modes(struct intel_connector *connector)
243 struct drm_i915_private *i915 = to_i915(connector->base.dev);
244 struct drm_display_mode *mode, *next;
246 list_for_each_entry_safe(mode, next, &connector->base.probed_modes, head) {
247 drm_dbg_kms(&i915->drm,
248 "[CONNECTOR:%d:%s] not using EDID mode: " DRM_MODE_FMT "\n",
249 connector->base.base.id, connector->base.name,
251 list_del(&mode->head);
252 drm_mode_destroy(&i915->drm, mode);
256 void intel_panel_add_edid_fixed_modes(struct intel_connector *connector,
257 bool use_alt_fixed_modes)
259 intel_panel_add_edid_preferred_mode(connector);
260 if (intel_panel_preferred_fixed_mode(connector) && use_alt_fixed_modes)
261 intel_panel_add_edid_alt_fixed_modes(connector);
262 intel_panel_destroy_probed_modes(connector);
265 static void intel_panel_add_fixed_mode(struct intel_connector *connector,
266 struct drm_display_mode *fixed_mode,
269 struct drm_i915_private *i915 = to_i915(connector->base.dev);
270 struct drm_display_info *info = &connector->base.display_info;
275 fixed_mode->type |= DRM_MODE_TYPE_PREFERRED | DRM_MODE_TYPE_DRIVER;
277 info->width_mm = fixed_mode->width_mm;
278 info->height_mm = fixed_mode->height_mm;
280 drm_dbg_kms(&i915->drm, "[CONNECTOR:%d:%s] using %s fixed mode: " DRM_MODE_FMT "\n",
281 connector->base.base.id, connector->base.name, type,
282 DRM_MODE_ARG(fixed_mode));
284 list_add_tail(&fixed_mode->head, &connector->panel.fixed_modes);
287 void intel_panel_add_vbt_lfp_fixed_mode(struct intel_connector *connector)
289 struct drm_i915_private *i915 = to_i915(connector->base.dev);
290 const struct drm_display_mode *mode;
292 mode = connector->panel.vbt.lfp_lvds_vbt_mode;
296 intel_panel_add_fixed_mode(connector,
297 drm_mode_duplicate(&i915->drm, mode),
301 void intel_panel_add_vbt_sdvo_fixed_mode(struct intel_connector *connector)
303 struct drm_i915_private *i915 = to_i915(connector->base.dev);
304 const struct drm_display_mode *mode;
306 mode = connector->panel.vbt.sdvo_lvds_vbt_mode;
310 intel_panel_add_fixed_mode(connector,
311 drm_mode_duplicate(&i915->drm, mode),
315 void intel_panel_add_encoder_fixed_mode(struct intel_connector *connector,
316 struct intel_encoder *encoder)
318 intel_panel_add_fixed_mode(connector,
319 intel_encoder_current_mode(encoder),
323 /* adjusted_mode has been preset to be the panel's fixed mode */
324 static int pch_panel_fitting(struct intel_crtc_state *crtc_state,
325 const struct drm_connector_state *conn_state)
327 const struct drm_display_mode *adjusted_mode =
328 &crtc_state->hw.adjusted_mode;
329 int pipe_src_w = drm_rect_width(&crtc_state->pipe_src);
330 int pipe_src_h = drm_rect_height(&crtc_state->pipe_src);
331 int x, y, width, height;
333 /* Native modes don't need fitting */
334 if (adjusted_mode->crtc_hdisplay == pipe_src_w &&
335 adjusted_mode->crtc_vdisplay == pipe_src_h &&
336 crtc_state->output_format != INTEL_OUTPUT_FORMAT_YCBCR420)
339 switch (conn_state->scaling_mode) {
340 case DRM_MODE_SCALE_CENTER:
343 x = (adjusted_mode->crtc_hdisplay - width + 1)/2;
344 y = (adjusted_mode->crtc_vdisplay - height + 1)/2;
347 case DRM_MODE_SCALE_ASPECT:
348 /* Scale but preserve the aspect ratio */
350 u32 scaled_width = adjusted_mode->crtc_hdisplay * pipe_src_h;
351 u32 scaled_height = pipe_src_w * adjusted_mode->crtc_vdisplay;
352 if (scaled_width > scaled_height) { /* pillar */
353 width = scaled_height / pipe_src_h;
356 x = (adjusted_mode->crtc_hdisplay - width + 1) / 2;
358 height = adjusted_mode->crtc_vdisplay;
359 } else if (scaled_width < scaled_height) { /* letter */
360 height = scaled_width / pipe_src_w;
363 y = (adjusted_mode->crtc_vdisplay - height + 1) / 2;
365 width = adjusted_mode->crtc_hdisplay;
368 width = adjusted_mode->crtc_hdisplay;
369 height = adjusted_mode->crtc_vdisplay;
374 case DRM_MODE_SCALE_NONE:
375 WARN_ON(adjusted_mode->crtc_hdisplay != pipe_src_w);
376 WARN_ON(adjusted_mode->crtc_vdisplay != pipe_src_h);
378 case DRM_MODE_SCALE_FULLSCREEN:
380 width = adjusted_mode->crtc_hdisplay;
381 height = adjusted_mode->crtc_vdisplay;
385 MISSING_CASE(conn_state->scaling_mode);
389 drm_rect_init(&crtc_state->pch_pfit.dst,
390 x, y, width, height);
391 crtc_state->pch_pfit.enabled = true;
397 centre_horizontally(struct drm_display_mode *adjusted_mode,
400 u32 border, sync_pos, blank_width, sync_width;
402 /* keep the hsync and hblank widths constant */
403 sync_width = adjusted_mode->crtc_hsync_end - adjusted_mode->crtc_hsync_start;
404 blank_width = adjusted_mode->crtc_hblank_end - adjusted_mode->crtc_hblank_start;
405 sync_pos = (blank_width - sync_width + 1) / 2;
407 border = (adjusted_mode->crtc_hdisplay - width + 1) / 2;
408 border += border & 1; /* make the border even */
410 adjusted_mode->crtc_hdisplay = width;
411 adjusted_mode->crtc_hblank_start = width + border;
412 adjusted_mode->crtc_hblank_end = adjusted_mode->crtc_hblank_start + blank_width;
414 adjusted_mode->crtc_hsync_start = adjusted_mode->crtc_hblank_start + sync_pos;
415 adjusted_mode->crtc_hsync_end = adjusted_mode->crtc_hsync_start + sync_width;
419 centre_vertically(struct drm_display_mode *adjusted_mode,
422 u32 border, sync_pos, blank_width, sync_width;
424 /* keep the vsync and vblank widths constant */
425 sync_width = adjusted_mode->crtc_vsync_end - adjusted_mode->crtc_vsync_start;
426 blank_width = adjusted_mode->crtc_vblank_end - adjusted_mode->crtc_vblank_start;
427 sync_pos = (blank_width - sync_width + 1) / 2;
429 border = (adjusted_mode->crtc_vdisplay - height + 1) / 2;
431 adjusted_mode->crtc_vdisplay = height;
432 adjusted_mode->crtc_vblank_start = height + border;
433 adjusted_mode->crtc_vblank_end = adjusted_mode->crtc_vblank_start + blank_width;
435 adjusted_mode->crtc_vsync_start = adjusted_mode->crtc_vblank_start + sync_pos;
436 adjusted_mode->crtc_vsync_end = adjusted_mode->crtc_vsync_start + sync_width;
439 static u32 panel_fitter_scaling(u32 source, u32 target)
442 * Floating point operation is not supported. So the FACTOR
443 * is defined, which can avoid the floating point computation
444 * when calculating the panel ratio.
447 #define FACTOR (1 << ACCURACY)
448 u32 ratio = source * FACTOR / target;
449 return (FACTOR * ratio + FACTOR/2) / FACTOR;
452 static void i965_scale_aspect(struct intel_crtc_state *crtc_state,
455 const struct drm_display_mode *adjusted_mode =
456 &crtc_state->hw.adjusted_mode;
457 int pipe_src_w = drm_rect_width(&crtc_state->pipe_src);
458 int pipe_src_h = drm_rect_height(&crtc_state->pipe_src);
459 u32 scaled_width = adjusted_mode->crtc_hdisplay * pipe_src_h;
460 u32 scaled_height = pipe_src_w * adjusted_mode->crtc_vdisplay;
462 /* 965+ is easy, it does everything in hw */
463 if (scaled_width > scaled_height)
464 *pfit_control |= PFIT_ENABLE |
466 else if (scaled_width < scaled_height)
467 *pfit_control |= PFIT_ENABLE |
469 else if (adjusted_mode->crtc_hdisplay != pipe_src_w)
470 *pfit_control |= PFIT_ENABLE | PFIT_SCALING_AUTO;
473 static void i9xx_scale_aspect(struct intel_crtc_state *crtc_state,
474 u32 *pfit_control, u32 *pfit_pgm_ratios,
477 struct drm_display_mode *adjusted_mode = &crtc_state->hw.adjusted_mode;
478 int pipe_src_w = drm_rect_width(&crtc_state->pipe_src);
479 int pipe_src_h = drm_rect_height(&crtc_state->pipe_src);
480 u32 scaled_width = adjusted_mode->crtc_hdisplay * pipe_src_h;
481 u32 scaled_height = pipe_src_w * adjusted_mode->crtc_vdisplay;
485 * For earlier chips we have to calculate the scaling
486 * ratio by hand and program it into the
487 * PFIT_PGM_RATIO register
489 if (scaled_width > scaled_height) { /* pillar */
490 centre_horizontally(adjusted_mode,
491 scaled_height / pipe_src_h);
493 *border = LVDS_BORDER_ENABLE;
494 if (pipe_src_h != adjusted_mode->crtc_vdisplay) {
495 bits = panel_fitter_scaling(pipe_src_h,
496 adjusted_mode->crtc_vdisplay);
498 *pfit_pgm_ratios |= (bits << PFIT_HORIZ_SCALE_SHIFT |
499 bits << PFIT_VERT_SCALE_SHIFT);
500 *pfit_control |= (PFIT_ENABLE |
501 VERT_INTERP_BILINEAR |
502 HORIZ_INTERP_BILINEAR);
504 } else if (scaled_width < scaled_height) { /* letter */
505 centre_vertically(adjusted_mode,
506 scaled_width / pipe_src_w);
508 *border = LVDS_BORDER_ENABLE;
509 if (pipe_src_w != adjusted_mode->crtc_hdisplay) {
510 bits = panel_fitter_scaling(pipe_src_w,
511 adjusted_mode->crtc_hdisplay);
513 *pfit_pgm_ratios |= (bits << PFIT_HORIZ_SCALE_SHIFT |
514 bits << PFIT_VERT_SCALE_SHIFT);
515 *pfit_control |= (PFIT_ENABLE |
516 VERT_INTERP_BILINEAR |
517 HORIZ_INTERP_BILINEAR);
520 /* Aspects match, Let hw scale both directions */
521 *pfit_control |= (PFIT_ENABLE |
522 VERT_AUTO_SCALE | HORIZ_AUTO_SCALE |
523 VERT_INTERP_BILINEAR |
524 HORIZ_INTERP_BILINEAR);
528 static int gmch_panel_fitting(struct intel_crtc_state *crtc_state,
529 const struct drm_connector_state *conn_state)
531 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
532 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
533 u32 pfit_control = 0, pfit_pgm_ratios = 0, border = 0;
534 struct drm_display_mode *adjusted_mode = &crtc_state->hw.adjusted_mode;
535 int pipe_src_w = drm_rect_width(&crtc_state->pipe_src);
536 int pipe_src_h = drm_rect_height(&crtc_state->pipe_src);
538 /* Native modes don't need fitting */
539 if (adjusted_mode->crtc_hdisplay == pipe_src_w &&
540 adjusted_mode->crtc_vdisplay == pipe_src_h)
543 switch (conn_state->scaling_mode) {
544 case DRM_MODE_SCALE_CENTER:
546 * For centered modes, we have to calculate border widths &
547 * heights and modify the values programmed into the CRTC.
549 centre_horizontally(adjusted_mode, pipe_src_w);
550 centre_vertically(adjusted_mode, pipe_src_h);
551 border = LVDS_BORDER_ENABLE;
553 case DRM_MODE_SCALE_ASPECT:
554 /* Scale but preserve the aspect ratio */
555 if (DISPLAY_VER(dev_priv) >= 4)
556 i965_scale_aspect(crtc_state, &pfit_control);
558 i9xx_scale_aspect(crtc_state, &pfit_control,
559 &pfit_pgm_ratios, &border);
561 case DRM_MODE_SCALE_FULLSCREEN:
563 * Full scaling, even if it changes the aspect ratio.
564 * Fortunately this is all done for us in hw.
566 if (pipe_src_h != adjusted_mode->crtc_vdisplay ||
567 pipe_src_w != adjusted_mode->crtc_hdisplay) {
568 pfit_control |= PFIT_ENABLE;
569 if (DISPLAY_VER(dev_priv) >= 4)
570 pfit_control |= PFIT_SCALING_AUTO;
572 pfit_control |= (VERT_AUTO_SCALE |
573 VERT_INTERP_BILINEAR |
575 HORIZ_INTERP_BILINEAR);
579 MISSING_CASE(conn_state->scaling_mode);
583 /* 965+ wants fuzzy fitting */
584 /* FIXME: handle multiple panels by failing gracefully */
585 if (DISPLAY_VER(dev_priv) >= 4)
586 pfit_control |= PFIT_PIPE(crtc->pipe) | PFIT_FILTER_FUZZY;
589 if ((pfit_control & PFIT_ENABLE) == 0) {
594 /* Make sure pre-965 set dither correctly for 18bpp panels. */
595 if (DISPLAY_VER(dev_priv) < 4 && crtc_state->pipe_bpp == 18)
596 pfit_control |= PANEL_8TO6_DITHER_ENABLE;
598 crtc_state->gmch_pfit.control = pfit_control;
599 crtc_state->gmch_pfit.pgm_ratios = pfit_pgm_ratios;
600 crtc_state->gmch_pfit.lvds_border_bits = border;
605 int intel_panel_fitting(struct intel_crtc_state *crtc_state,
606 const struct drm_connector_state *conn_state)
608 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
609 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
612 return gmch_panel_fitting(crtc_state, conn_state);
614 return pch_panel_fitting(crtc_state, conn_state);
617 enum drm_connector_status
618 intel_panel_detect(struct drm_connector *connector, bool force)
620 struct drm_i915_private *i915 = to_i915(connector->dev);
622 if (!INTEL_DISPLAY_ENABLED(i915))
623 return connector_status_disconnected;
625 return connector_status_connected;
629 intel_panel_mode_valid(struct intel_connector *connector,
630 const struct drm_display_mode *mode)
632 const struct drm_display_mode *fixed_mode =
633 intel_panel_fixed_mode(connector, mode);
638 if (mode->hdisplay != fixed_mode->hdisplay)
641 if (mode->vdisplay != fixed_mode->vdisplay)
644 if (drm_mode_vrefresh(mode) != drm_mode_vrefresh(fixed_mode))
650 int intel_panel_init(struct intel_connector *connector)
652 struct intel_panel *panel = &connector->panel;
654 intel_backlight_init_funcs(panel);
656 drm_dbg_kms(connector->base.dev,
657 "[CONNECTOR:%d:%s] DRRS type: %s\n",
658 connector->base.base.id, connector->base.name,
659 intel_drrs_type_str(intel_panel_drrs_type(connector)));
664 void intel_panel_fini(struct intel_connector *connector)
666 struct intel_panel *panel = &connector->panel;
667 struct drm_display_mode *fixed_mode, *next;
669 intel_backlight_destroy(panel);
671 intel_bios_fini_panel(panel);
673 list_for_each_entry_safe(fixed_mode, next, &panel->fixed_modes, head) {
674 list_del(&fixed_mode->head);
675 drm_mode_destroy(connector->base.dev, fixed_mode);