1 /*
2 * Copyright © 2006-2010 Intel Corporation
3 * Copyright (c) 2006 Dave Airlie <airlied@linux.ie>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Eric Anholt <eric@anholt.net>
26 * Dave Airlie <airlied@linux.ie>
27 * Jesse Barnes <jesse.barnes@intel.com>
28 * Chris Wilson <chris@chris-wilson.co.uk>
29 */
30
31 #include <linux/kernel.h>
32 #include <linux/pwm.h>
33
34 #include "intel_backlight.h"
35 #include "intel_connector.h"
36 #include "intel_de.h"
37 #include "intel_display_types.h"
38 #include "intel_panel.h"
39
40 void
intel_fixed_panel_mode(const struct drm_display_mode * fixed_mode,struct drm_display_mode * adjusted_mode)41 intel_fixed_panel_mode(const struct drm_display_mode *fixed_mode,
42 struct drm_display_mode *adjusted_mode)
43 {
44 drm_mode_copy(adjusted_mode, fixed_mode);
45
46 drm_mode_set_crtcinfo(adjusted_mode, 0);
47 }
48
is_downclock_mode(const struct drm_display_mode * downclock_mode,const struct drm_display_mode * fixed_mode)49 static bool is_downclock_mode(const struct drm_display_mode *downclock_mode,
50 const struct drm_display_mode *fixed_mode)
51 {
52 return drm_mode_match(downclock_mode, fixed_mode,
53 DRM_MODE_MATCH_TIMINGS |
54 DRM_MODE_MATCH_FLAGS |
55 DRM_MODE_MATCH_3D_FLAGS) &&
56 downclock_mode->clock < fixed_mode->clock;
57 }
58
59 struct drm_display_mode *
intel_panel_edid_downclock_mode(struct intel_connector * connector,const struct drm_display_mode * fixed_mode)60 intel_panel_edid_downclock_mode(struct intel_connector *connector,
61 const struct drm_display_mode *fixed_mode)
62 {
63 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
64 const struct drm_display_mode *scan, *best_mode = NULL;
65 struct drm_display_mode *downclock_mode;
66 int best_clock = fixed_mode->clock;
67
68 list_for_each_entry(scan, &connector->base.probed_modes, head) {
69 /*
70 * If one mode has the same resolution with the fixed_panel
71 * mode while they have the different refresh rate, it means
72 * that the reduced downclock is found. In such
73 * case we can set the different FPx0/1 to dynamically select
74 * between low and high frequency.
75 */
76 if (is_downclock_mode(scan, fixed_mode) &&
77 scan->clock < best_clock) {
78 /*
79 * The downclock is already found. But we
80 * expect to find the lower downclock.
81 */
82 best_clock = scan->clock;
83 best_mode = scan;
84 }
85 }
86
87 if (!best_mode)
88 return NULL;
89
90 downclock_mode = drm_mode_duplicate(&dev_priv->drm, best_mode);
91 if (!downclock_mode)
92 return NULL;
93
94 drm_dbg_kms(&dev_priv->drm,
95 "[CONNECTOR:%d:%s] using downclock mode from EDID: ",
96 connector->base.base.id, connector->base.name);
97 drm_mode_debug_printmodeline(downclock_mode);
98
99 return downclock_mode;
100 }
101
102 struct drm_display_mode *
intel_panel_edid_fixed_mode(struct intel_connector * connector)103 intel_panel_edid_fixed_mode(struct intel_connector *connector)
104 {
105 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
106 const struct drm_display_mode *scan;
107 struct drm_display_mode *fixed_mode;
108
109 if (list_empty(&connector->base.probed_modes))
110 return NULL;
111
112 /* prefer fixed mode from EDID if available */
113 list_for_each_entry(scan, &connector->base.probed_modes, head) {
114 if ((scan->type & DRM_MODE_TYPE_PREFERRED) == 0)
115 continue;
116
117 fixed_mode = drm_mode_duplicate(&dev_priv->drm, scan);
118 if (!fixed_mode)
119 return NULL;
120
121 drm_dbg_kms(&dev_priv->drm,
122 "[CONNECTOR:%d:%s] using preferred mode from EDID: ",
123 connector->base.base.id, connector->base.name);
124 drm_mode_debug_printmodeline(fixed_mode);
125
126 return fixed_mode;
127 }
128
129 scan = list_first_entry(&connector->base.probed_modes,
130 typeof(*scan), head);
131
132 fixed_mode = drm_mode_duplicate(&dev_priv->drm, scan);
133 if (!fixed_mode)
134 return NULL;
135
136 fixed_mode->type |= DRM_MODE_TYPE_PREFERRED;
137
138 drm_dbg_kms(&dev_priv->drm,
139 "[CONNECTOR:%d:%s] using first mode from EDID: ",
140 connector->base.base.id, connector->base.name);
141 drm_mode_debug_printmodeline(fixed_mode);
142
143 return fixed_mode;
144 }
145
146 struct drm_display_mode *
intel_panel_vbt_fixed_mode(struct intel_connector * connector)147 intel_panel_vbt_fixed_mode(struct intel_connector *connector)
148 {
149 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
150 struct drm_display_info *info = &connector->base.display_info;
151 struct drm_display_mode *fixed_mode;
152
153 if (!dev_priv->vbt.lfp_lvds_vbt_mode)
154 return NULL;
155
156 fixed_mode = drm_mode_duplicate(&dev_priv->drm,
157 dev_priv->vbt.lfp_lvds_vbt_mode);
158 if (!fixed_mode)
159 return NULL;
160
161 fixed_mode->type |= DRM_MODE_TYPE_PREFERRED;
162
163 drm_dbg_kms(&dev_priv->drm, "[CONNECTOR:%d:%s] using mode from VBT: ",
164 connector->base.base.id, connector->base.name);
165 drm_mode_debug_printmodeline(fixed_mode);
166
167 info->width_mm = fixed_mode->width_mm;
168 info->height_mm = fixed_mode->height_mm;
169
170 return fixed_mode;
171 }
172
173 /* adjusted_mode has been preset to be the panel's fixed mode */
intel_pch_panel_fitting(struct intel_crtc_state * crtc_state,const struct drm_connector_state * conn_state)174 int intel_pch_panel_fitting(struct intel_crtc_state *crtc_state,
175 const struct drm_connector_state *conn_state)
176 {
177 const struct drm_display_mode *adjusted_mode =
178 &crtc_state->hw.adjusted_mode;
179 int x, y, width, height;
180
181 /* Native modes don't need fitting */
182 if (adjusted_mode->crtc_hdisplay == crtc_state->pipe_src_w &&
183 adjusted_mode->crtc_vdisplay == crtc_state->pipe_src_h &&
184 crtc_state->output_format != INTEL_OUTPUT_FORMAT_YCBCR420)
185 return 0;
186
187 switch (conn_state->scaling_mode) {
188 case DRM_MODE_SCALE_CENTER:
189 width = crtc_state->pipe_src_w;
190 height = crtc_state->pipe_src_h;
191 x = (adjusted_mode->crtc_hdisplay - width + 1)/2;
192 y = (adjusted_mode->crtc_vdisplay - height + 1)/2;
193 break;
194
195 case DRM_MODE_SCALE_ASPECT:
196 /* Scale but preserve the aspect ratio */
197 {
198 u32 scaled_width = adjusted_mode->crtc_hdisplay
199 * crtc_state->pipe_src_h;
200 u32 scaled_height = crtc_state->pipe_src_w
201 * adjusted_mode->crtc_vdisplay;
202 if (scaled_width > scaled_height) { /* pillar */
203 width = scaled_height / crtc_state->pipe_src_h;
204 if (width & 1)
205 width++;
206 x = (adjusted_mode->crtc_hdisplay - width + 1) / 2;
207 y = 0;
208 height = adjusted_mode->crtc_vdisplay;
209 } else if (scaled_width < scaled_height) { /* letter */
210 height = scaled_width / crtc_state->pipe_src_w;
211 if (height & 1)
212 height++;
213 y = (adjusted_mode->crtc_vdisplay - height + 1) / 2;
214 x = 0;
215 width = adjusted_mode->crtc_hdisplay;
216 } else {
217 x = y = 0;
218 width = adjusted_mode->crtc_hdisplay;
219 height = adjusted_mode->crtc_vdisplay;
220 }
221 }
222 break;
223
224 case DRM_MODE_SCALE_NONE:
225 WARN_ON(adjusted_mode->crtc_hdisplay != crtc_state->pipe_src_w);
226 WARN_ON(adjusted_mode->crtc_vdisplay != crtc_state->pipe_src_h);
227 fallthrough;
228 case DRM_MODE_SCALE_FULLSCREEN:
229 x = y = 0;
230 width = adjusted_mode->crtc_hdisplay;
231 height = adjusted_mode->crtc_vdisplay;
232 break;
233
234 default:
235 MISSING_CASE(conn_state->scaling_mode);
236 return -EINVAL;
237 }
238
239 drm_rect_init(&crtc_state->pch_pfit.dst,
240 x, y, width, height);
241 crtc_state->pch_pfit.enabled = true;
242
243 return 0;
244 }
245
246 static void
centre_horizontally(struct drm_display_mode * adjusted_mode,int width)247 centre_horizontally(struct drm_display_mode *adjusted_mode,
248 int width)
249 {
250 u32 border, sync_pos, blank_width, sync_width;
251
252 /* keep the hsync and hblank widths constant */
253 sync_width = adjusted_mode->crtc_hsync_end - adjusted_mode->crtc_hsync_start;
254 blank_width = adjusted_mode->crtc_hblank_end - adjusted_mode->crtc_hblank_start;
255 sync_pos = (blank_width - sync_width + 1) / 2;
256
257 border = (adjusted_mode->crtc_hdisplay - width + 1) / 2;
258 border += border & 1; /* make the border even */
259
260 adjusted_mode->crtc_hdisplay = width;
261 adjusted_mode->crtc_hblank_start = width + border;
262 adjusted_mode->crtc_hblank_end = adjusted_mode->crtc_hblank_start + blank_width;
263
264 adjusted_mode->crtc_hsync_start = adjusted_mode->crtc_hblank_start + sync_pos;
265 adjusted_mode->crtc_hsync_end = adjusted_mode->crtc_hsync_start + sync_width;
266 }
267
268 static void
centre_vertically(struct drm_display_mode * adjusted_mode,int height)269 centre_vertically(struct drm_display_mode *adjusted_mode,
270 int height)
271 {
272 u32 border, sync_pos, blank_width, sync_width;
273
274 /* keep the vsync and vblank widths constant */
275 sync_width = adjusted_mode->crtc_vsync_end - adjusted_mode->crtc_vsync_start;
276 blank_width = adjusted_mode->crtc_vblank_end - adjusted_mode->crtc_vblank_start;
277 sync_pos = (blank_width - sync_width + 1) / 2;
278
279 border = (adjusted_mode->crtc_vdisplay - height + 1) / 2;
280
281 adjusted_mode->crtc_vdisplay = height;
282 adjusted_mode->crtc_vblank_start = height + border;
283 adjusted_mode->crtc_vblank_end = adjusted_mode->crtc_vblank_start + blank_width;
284
285 adjusted_mode->crtc_vsync_start = adjusted_mode->crtc_vblank_start + sync_pos;
286 adjusted_mode->crtc_vsync_end = adjusted_mode->crtc_vsync_start + sync_width;
287 }
288
panel_fitter_scaling(u32 source,u32 target)289 static u32 panel_fitter_scaling(u32 source, u32 target)
290 {
291 /*
292 * Floating point operation is not supported. So the FACTOR
293 * is defined, which can avoid the floating point computation
294 * when calculating the panel ratio.
295 */
296 #define ACCURACY 12
297 #define FACTOR (1 << ACCURACY)
298 u32 ratio = source * FACTOR / target;
299 return (FACTOR * ratio + FACTOR/2) / FACTOR;
300 }
301
i965_scale_aspect(struct intel_crtc_state * crtc_state,u32 * pfit_control)302 static void i965_scale_aspect(struct intel_crtc_state *crtc_state,
303 u32 *pfit_control)
304 {
305 const struct drm_display_mode *adjusted_mode =
306 &crtc_state->hw.adjusted_mode;
307 u32 scaled_width = adjusted_mode->crtc_hdisplay *
308 crtc_state->pipe_src_h;
309 u32 scaled_height = crtc_state->pipe_src_w *
310 adjusted_mode->crtc_vdisplay;
311
312 /* 965+ is easy, it does everything in hw */
313 if (scaled_width > scaled_height)
314 *pfit_control |= PFIT_ENABLE |
315 PFIT_SCALING_PILLAR;
316 else if (scaled_width < scaled_height)
317 *pfit_control |= PFIT_ENABLE |
318 PFIT_SCALING_LETTER;
319 else if (adjusted_mode->crtc_hdisplay != crtc_state->pipe_src_w)
320 *pfit_control |= PFIT_ENABLE | PFIT_SCALING_AUTO;
321 }
322
i9xx_scale_aspect(struct intel_crtc_state * crtc_state,u32 * pfit_control,u32 * pfit_pgm_ratios,u32 * border)323 static void i9xx_scale_aspect(struct intel_crtc_state *crtc_state,
324 u32 *pfit_control, u32 *pfit_pgm_ratios,
325 u32 *border)
326 {
327 struct drm_display_mode *adjusted_mode = &crtc_state->hw.adjusted_mode;
328 u32 scaled_width = adjusted_mode->crtc_hdisplay *
329 crtc_state->pipe_src_h;
330 u32 scaled_height = crtc_state->pipe_src_w *
331 adjusted_mode->crtc_vdisplay;
332 u32 bits;
333
334 /*
335 * For earlier chips we have to calculate the scaling
336 * ratio by hand and program it into the
337 * PFIT_PGM_RATIO register
338 */
339 if (scaled_width > scaled_height) { /* pillar */
340 centre_horizontally(adjusted_mode,
341 scaled_height /
342 crtc_state->pipe_src_h);
343
344 *border = LVDS_BORDER_ENABLE;
345 if (crtc_state->pipe_src_h != adjusted_mode->crtc_vdisplay) {
346 bits = panel_fitter_scaling(crtc_state->pipe_src_h,
347 adjusted_mode->crtc_vdisplay);
348
349 *pfit_pgm_ratios |= (bits << PFIT_HORIZ_SCALE_SHIFT |
350 bits << PFIT_VERT_SCALE_SHIFT);
351 *pfit_control |= (PFIT_ENABLE |
352 VERT_INTERP_BILINEAR |
353 HORIZ_INTERP_BILINEAR);
354 }
355 } else if (scaled_width < scaled_height) { /* letter */
356 centre_vertically(adjusted_mode,
357 scaled_width /
358 crtc_state->pipe_src_w);
359
360 *border = LVDS_BORDER_ENABLE;
361 if (crtc_state->pipe_src_w != adjusted_mode->crtc_hdisplay) {
362 bits = panel_fitter_scaling(crtc_state->pipe_src_w,
363 adjusted_mode->crtc_hdisplay);
364
365 *pfit_pgm_ratios |= (bits << PFIT_HORIZ_SCALE_SHIFT |
366 bits << PFIT_VERT_SCALE_SHIFT);
367 *pfit_control |= (PFIT_ENABLE |
368 VERT_INTERP_BILINEAR |
369 HORIZ_INTERP_BILINEAR);
370 }
371 } else {
372 /* Aspects match, Let hw scale both directions */
373 *pfit_control |= (PFIT_ENABLE |
374 VERT_AUTO_SCALE | HORIZ_AUTO_SCALE |
375 VERT_INTERP_BILINEAR |
376 HORIZ_INTERP_BILINEAR);
377 }
378 }
379
intel_gmch_panel_fitting(struct intel_crtc_state * crtc_state,const struct drm_connector_state * conn_state)380 int intel_gmch_panel_fitting(struct intel_crtc_state *crtc_state,
381 const struct drm_connector_state *conn_state)
382 {
383 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
384 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
385 u32 pfit_control = 0, pfit_pgm_ratios = 0, border = 0;
386 struct drm_display_mode *adjusted_mode = &crtc_state->hw.adjusted_mode;
387
388 /* Native modes don't need fitting */
389 if (adjusted_mode->crtc_hdisplay == crtc_state->pipe_src_w &&
390 adjusted_mode->crtc_vdisplay == crtc_state->pipe_src_h)
391 goto out;
392
393 switch (conn_state->scaling_mode) {
394 case DRM_MODE_SCALE_CENTER:
395 /*
396 * For centered modes, we have to calculate border widths &
397 * heights and modify the values programmed into the CRTC.
398 */
399 centre_horizontally(adjusted_mode, crtc_state->pipe_src_w);
400 centre_vertically(adjusted_mode, crtc_state->pipe_src_h);
401 border = LVDS_BORDER_ENABLE;
402 break;
403 case DRM_MODE_SCALE_ASPECT:
404 /* Scale but preserve the aspect ratio */
405 if (DISPLAY_VER(dev_priv) >= 4)
406 i965_scale_aspect(crtc_state, &pfit_control);
407 else
408 i9xx_scale_aspect(crtc_state, &pfit_control,
409 &pfit_pgm_ratios, &border);
410 break;
411 case DRM_MODE_SCALE_FULLSCREEN:
412 /*
413 * Full scaling, even if it changes the aspect ratio.
414 * Fortunately this is all done for us in hw.
415 */
416 if (crtc_state->pipe_src_h != adjusted_mode->crtc_vdisplay ||
417 crtc_state->pipe_src_w != adjusted_mode->crtc_hdisplay) {
418 pfit_control |= PFIT_ENABLE;
419 if (DISPLAY_VER(dev_priv) >= 4)
420 pfit_control |= PFIT_SCALING_AUTO;
421 else
422 pfit_control |= (VERT_AUTO_SCALE |
423 VERT_INTERP_BILINEAR |
424 HORIZ_AUTO_SCALE |
425 HORIZ_INTERP_BILINEAR);
426 }
427 break;
428 default:
429 MISSING_CASE(conn_state->scaling_mode);
430 return -EINVAL;
431 }
432
433 /* 965+ wants fuzzy fitting */
434 /* FIXME: handle multiple panels by failing gracefully */
435 if (DISPLAY_VER(dev_priv) >= 4)
436 pfit_control |= PFIT_PIPE(crtc->pipe) | PFIT_FILTER_FUZZY;
437
438 out:
439 if ((pfit_control & PFIT_ENABLE) == 0) {
440 pfit_control = 0;
441 pfit_pgm_ratios = 0;
442 }
443
444 /* Make sure pre-965 set dither correctly for 18bpp panels. */
445 if (DISPLAY_VER(dev_priv) < 4 && crtc_state->pipe_bpp == 18)
446 pfit_control |= PANEL_8TO6_DITHER_ENABLE;
447
448 crtc_state->gmch_pfit.control = pfit_control;
449 crtc_state->gmch_pfit.pgm_ratios = pfit_pgm_ratios;
450 crtc_state->gmch_pfit.lvds_border_bits = border;
451
452 return 0;
453 }
454
455 enum drm_connector_status
intel_panel_detect(struct drm_connector * connector,bool force)456 intel_panel_detect(struct drm_connector *connector, bool force)
457 {
458 struct drm_i915_private *i915 = to_i915(connector->dev);
459
460 if (!INTEL_DISPLAY_ENABLED(i915))
461 return connector_status_disconnected;
462
463 return connector_status_connected;
464 }
465
intel_panel_init(struct intel_panel * panel,struct drm_display_mode * fixed_mode,struct drm_display_mode * downclock_mode)466 int intel_panel_init(struct intel_panel *panel,
467 struct drm_display_mode *fixed_mode,
468 struct drm_display_mode *downclock_mode)
469 {
470 intel_panel_init_backlight_funcs(panel);
471
472 panel->fixed_mode = fixed_mode;
473 panel->downclock_mode = downclock_mode;
474
475 return 0;
476 }
477
intel_panel_fini(struct intel_panel * panel)478 void intel_panel_fini(struct intel_panel *panel)
479 {
480 struct intel_connector *intel_connector =
481 container_of(panel, struct intel_connector, panel);
482
483 intel_panel_destroy_backlight(panel);
484
485 if (panel->fixed_mode)
486 drm_mode_destroy(intel_connector->base.dev, panel->fixed_mode);
487
488 if (panel->downclock_mode)
489 drm_mode_destroy(intel_connector->base.dev,
490 panel->downclock_mode);
491 }
492