blob: 42114ecbae0e3c4c8dc51b7b02a1f658de857873 [file] [log] [blame]
Chris Wilson1d8e1c72010-08-07 11:01:28 +01001/*
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
Joe Perchesa70491c2012-03-18 13:00:11 -070031#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
32
Carsten Emde7bd90902012-03-15 15:56:25 +010033#include <linux/moduleparam.h>
Chris Wilson1d8e1c72010-08-07 11:01:28 +010034#include "intel_drv.h"
35
Takashi Iwaiba3820a2011-03-10 14:02:12 +010036#define PCI_LBPC 0xf4 /* legacy/combination backlight modes */
37
Chris Wilson1d8e1c72010-08-07 11:01:28 +010038void
Ville Syrjälä4c6df4b2013-09-02 21:13:39 +030039intel_fixed_panel_mode(const struct drm_display_mode *fixed_mode,
Chris Wilson1d8e1c72010-08-07 11:01:28 +010040 struct drm_display_mode *adjusted_mode)
41{
Ville Syrjälä4c6df4b2013-09-02 21:13:39 +030042 drm_mode_copy(adjusted_mode, fixed_mode);
Imre Deaka52690e2013-08-27 12:24:09 +030043
44 drm_mode_set_crtcinfo(adjusted_mode, 0);
Chris Wilson1d8e1c72010-08-07 11:01:28 +010045}
46
47/* adjusted_mode has been preset to be the panel's fixed mode */
48void
Jesse Barnesb074cec2013-04-25 12:55:02 -070049intel_pch_panel_fitting(struct intel_crtc *intel_crtc,
50 struct intel_crtc_config *pipe_config,
51 int fitting_mode)
Chris Wilson1d8e1c72010-08-07 11:01:28 +010052{
Jesse Barnesb074cec2013-04-25 12:55:02 -070053 struct drm_display_mode *mode, *adjusted_mode;
Chris Wilson1d8e1c72010-08-07 11:01:28 +010054 int x, y, width, height;
55
Jesse Barnesb074cec2013-04-25 12:55:02 -070056 mode = &pipe_config->requested_mode;
57 adjusted_mode = &pipe_config->adjusted_mode;
58
Chris Wilson1d8e1c72010-08-07 11:01:28 +010059 x = y = width = height = 0;
60
61 /* Native modes don't need fitting */
62 if (adjusted_mode->hdisplay == mode->hdisplay &&
63 adjusted_mode->vdisplay == mode->vdisplay)
64 goto done;
65
66 switch (fitting_mode) {
67 case DRM_MODE_SCALE_CENTER:
68 width = mode->hdisplay;
69 height = mode->vdisplay;
70 x = (adjusted_mode->hdisplay - width + 1)/2;
71 y = (adjusted_mode->vdisplay - height + 1)/2;
72 break;
73
74 case DRM_MODE_SCALE_ASPECT:
75 /* Scale but preserve the aspect ratio */
76 {
77 u32 scaled_width = adjusted_mode->hdisplay * mode->vdisplay;
78 u32 scaled_height = mode->hdisplay * adjusted_mode->vdisplay;
79 if (scaled_width > scaled_height) { /* pillar */
80 width = scaled_height / mode->vdisplay;
Adam Jackson302983e2011-07-13 16:32:32 -040081 if (width & 1)
Akshay Joshi0206e352011-08-16 15:34:10 -040082 width++;
Chris Wilson1d8e1c72010-08-07 11:01:28 +010083 x = (adjusted_mode->hdisplay - width + 1) / 2;
84 y = 0;
85 height = adjusted_mode->vdisplay;
86 } else if (scaled_width < scaled_height) { /* letter */
87 height = scaled_width / mode->hdisplay;
Adam Jackson302983e2011-07-13 16:32:32 -040088 if (height & 1)
89 height++;
Chris Wilson1d8e1c72010-08-07 11:01:28 +010090 y = (adjusted_mode->vdisplay - height + 1) / 2;
91 x = 0;
92 width = adjusted_mode->hdisplay;
93 } else {
94 x = y = 0;
95 width = adjusted_mode->hdisplay;
96 height = adjusted_mode->vdisplay;
97 }
98 }
99 break;
100
Chris Wilson1d8e1c72010-08-07 11:01:28 +0100101 case DRM_MODE_SCALE_FULLSCREEN:
102 x = y = 0;
103 width = adjusted_mode->hdisplay;
104 height = adjusted_mode->vdisplay;
105 break;
Jesse Barnesab3e67f2013-04-25 12:55:03 -0700106
107 default:
108 WARN(1, "bad panel fit mode: %d\n", fitting_mode);
109 return;
Chris Wilson1d8e1c72010-08-07 11:01:28 +0100110 }
111
112done:
Jesse Barnesb074cec2013-04-25 12:55:02 -0700113 pipe_config->pch_pfit.pos = (x << 16) | y;
114 pipe_config->pch_pfit.size = (width << 16) | height;
Chris Wilson1d8e1c72010-08-07 11:01:28 +0100115}
Chris Wilsona9573552010-08-22 13:18:16 +0100116
Jesse Barnes2dd24552013-04-25 12:55:01 -0700117static void
118centre_horizontally(struct drm_display_mode *mode,
119 int width)
120{
121 u32 border, sync_pos, blank_width, sync_width;
122
123 /* keep the hsync and hblank widths constant */
124 sync_width = mode->crtc_hsync_end - mode->crtc_hsync_start;
125 blank_width = mode->crtc_hblank_end - mode->crtc_hblank_start;
126 sync_pos = (blank_width - sync_width + 1) / 2;
127
128 border = (mode->hdisplay - width + 1) / 2;
129 border += border & 1; /* make the border even */
130
131 mode->crtc_hdisplay = width;
132 mode->crtc_hblank_start = width + border;
133 mode->crtc_hblank_end = mode->crtc_hblank_start + blank_width;
134
135 mode->crtc_hsync_start = mode->crtc_hblank_start + sync_pos;
136 mode->crtc_hsync_end = mode->crtc_hsync_start + sync_width;
137}
138
139static void
140centre_vertically(struct drm_display_mode *mode,
141 int height)
142{
143 u32 border, sync_pos, blank_width, sync_width;
144
145 /* keep the vsync and vblank widths constant */
146 sync_width = mode->crtc_vsync_end - mode->crtc_vsync_start;
147 blank_width = mode->crtc_vblank_end - mode->crtc_vblank_start;
148 sync_pos = (blank_width - sync_width + 1) / 2;
149
150 border = (mode->vdisplay - height + 1) / 2;
151
152 mode->crtc_vdisplay = height;
153 mode->crtc_vblank_start = height + border;
154 mode->crtc_vblank_end = mode->crtc_vblank_start + blank_width;
155
156 mode->crtc_vsync_start = mode->crtc_vblank_start + sync_pos;
157 mode->crtc_vsync_end = mode->crtc_vsync_start + sync_width;
158}
159
160static inline u32 panel_fitter_scaling(u32 source, u32 target)
161{
162 /*
163 * Floating point operation is not supported. So the FACTOR
164 * is defined, which can avoid the floating point computation
165 * when calculating the panel ratio.
166 */
167#define ACCURACY 12
168#define FACTOR (1 << ACCURACY)
169 u32 ratio = source * FACTOR / target;
170 return (FACTOR * ratio + FACTOR/2) / FACTOR;
171}
172
173void intel_gmch_panel_fitting(struct intel_crtc *intel_crtc,
174 struct intel_crtc_config *pipe_config,
175 int fitting_mode)
176{
177 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes2dd24552013-04-25 12:55:01 -0700178 u32 pfit_control = 0, pfit_pgm_ratios = 0, border = 0;
179 struct drm_display_mode *mode, *adjusted_mode;
180
181 mode = &pipe_config->requested_mode;
182 adjusted_mode = &pipe_config->adjusted_mode;
183
184 /* Native modes don't need fitting */
185 if (adjusted_mode->hdisplay == mode->hdisplay &&
186 adjusted_mode->vdisplay == mode->vdisplay)
187 goto out;
188
189 switch (fitting_mode) {
190 case DRM_MODE_SCALE_CENTER:
191 /*
192 * For centered modes, we have to calculate border widths &
193 * heights and modify the values programmed into the CRTC.
194 */
195 centre_horizontally(adjusted_mode, mode->hdisplay);
196 centre_vertically(adjusted_mode, mode->vdisplay);
197 border = LVDS_BORDER_ENABLE;
198 break;
199 case DRM_MODE_SCALE_ASPECT:
200 /* Scale but preserve the aspect ratio */
201 if (INTEL_INFO(dev)->gen >= 4) {
202 u32 scaled_width = adjusted_mode->hdisplay *
203 mode->vdisplay;
204 u32 scaled_height = mode->hdisplay *
205 adjusted_mode->vdisplay;
206
207 /* 965+ is easy, it does everything in hw */
208 if (scaled_width > scaled_height)
209 pfit_control |= PFIT_ENABLE |
210 PFIT_SCALING_PILLAR;
211 else if (scaled_width < scaled_height)
212 pfit_control |= PFIT_ENABLE |
213 PFIT_SCALING_LETTER;
214 else if (adjusted_mode->hdisplay != mode->hdisplay)
215 pfit_control |= PFIT_ENABLE | PFIT_SCALING_AUTO;
216 } else {
217 u32 scaled_width = adjusted_mode->hdisplay *
218 mode->vdisplay;
219 u32 scaled_height = mode->hdisplay *
220 adjusted_mode->vdisplay;
221 /*
222 * For earlier chips we have to calculate the scaling
223 * ratio by hand and program it into the
224 * PFIT_PGM_RATIO register
225 */
226 if (scaled_width > scaled_height) { /* pillar */
227 centre_horizontally(adjusted_mode,
228 scaled_height /
229 mode->vdisplay);
230
231 border = LVDS_BORDER_ENABLE;
232 if (mode->vdisplay != adjusted_mode->vdisplay) {
233 u32 bits = panel_fitter_scaling(mode->vdisplay, adjusted_mode->vdisplay);
234 pfit_pgm_ratios |= (bits << PFIT_HORIZ_SCALE_SHIFT |
235 bits << PFIT_VERT_SCALE_SHIFT);
236 pfit_control |= (PFIT_ENABLE |
237 VERT_INTERP_BILINEAR |
238 HORIZ_INTERP_BILINEAR);
239 }
240 } else if (scaled_width < scaled_height) { /* letter */
241 centre_vertically(adjusted_mode,
242 scaled_width /
243 mode->hdisplay);
244
245 border = LVDS_BORDER_ENABLE;
246 if (mode->hdisplay != adjusted_mode->hdisplay) {
247 u32 bits = panel_fitter_scaling(mode->hdisplay, adjusted_mode->hdisplay);
248 pfit_pgm_ratios |= (bits << PFIT_HORIZ_SCALE_SHIFT |
249 bits << PFIT_VERT_SCALE_SHIFT);
250 pfit_control |= (PFIT_ENABLE |
251 VERT_INTERP_BILINEAR |
252 HORIZ_INTERP_BILINEAR);
253 }
254 } else {
255 /* Aspects match, Let hw scale both directions */
256 pfit_control |= (PFIT_ENABLE |
257 VERT_AUTO_SCALE | HORIZ_AUTO_SCALE |
258 VERT_INTERP_BILINEAR |
259 HORIZ_INTERP_BILINEAR);
260 }
261 }
262 break;
Jesse Barnes2dd24552013-04-25 12:55:01 -0700263 case DRM_MODE_SCALE_FULLSCREEN:
264 /*
265 * Full scaling, even if it changes the aspect ratio.
266 * Fortunately this is all done for us in hw.
267 */
268 if (mode->vdisplay != adjusted_mode->vdisplay ||
269 mode->hdisplay != adjusted_mode->hdisplay) {
270 pfit_control |= PFIT_ENABLE;
271 if (INTEL_INFO(dev)->gen >= 4)
272 pfit_control |= PFIT_SCALING_AUTO;
273 else
274 pfit_control |= (VERT_AUTO_SCALE |
275 VERT_INTERP_BILINEAR |
276 HORIZ_AUTO_SCALE |
277 HORIZ_INTERP_BILINEAR);
278 }
279 break;
Jesse Barnesab3e67f2013-04-25 12:55:03 -0700280 default:
281 WARN(1, "bad panel fit mode: %d\n", fitting_mode);
282 return;
Jesse Barnes2dd24552013-04-25 12:55:01 -0700283 }
284
285 /* 965+ wants fuzzy fitting */
286 /* FIXME: handle multiple panels by failing gracefully */
287 if (INTEL_INFO(dev)->gen >= 4)
288 pfit_control |= ((intel_crtc->pipe << PFIT_PIPE_SHIFT) |
289 PFIT_FILTER_FUZZY);
290
291out:
292 if ((pfit_control & PFIT_ENABLE) == 0) {
293 pfit_control = 0;
294 pfit_pgm_ratios = 0;
295 }
296
297 /* Make sure pre-965 set dither correctly for 18bpp panels. */
298 if (INTEL_INFO(dev)->gen < 4 && pipe_config->pipe_bpp == 18)
299 pfit_control |= PANEL_8TO6_DITHER_ENABLE;
300
Daniel Vetter2deefda2013-04-25 22:52:17 +0200301 pipe_config->gmch_pfit.control = pfit_control;
302 pipe_config->gmch_pfit.pgm_ratios = pfit_pgm_ratios;
Daniel Vetter68fc8742013-04-25 22:52:16 +0200303 pipe_config->gmch_pfit.lvds_border_bits = border;
Jesse Barnes2dd24552013-04-25 12:55:01 -0700304}
305
Takashi Iwaiba3820a2011-03-10 14:02:12 +0100306static int is_backlight_combination_mode(struct drm_device *dev)
307{
308 struct drm_i915_private *dev_priv = dev->dev_private;
309
310 if (INTEL_INFO(dev)->gen >= 4)
311 return I915_READ(BLC_PWM_CTL2) & BLM_COMBINATION_MODE;
312
313 if (IS_GEN2(dev))
314 return I915_READ(BLC_PWM_CTL) & BLM_LEGACY_MODE;
315
316 return 0;
317}
318
Jani Nikulad6540632013-04-12 15:18:36 +0300319/* XXX: query mode clock or hardware clock and program max PWM appropriately
320 * when it's 0.
321 */
Jani Nikulabfd75902012-12-04 16:36:28 +0200322static u32 i915_read_blc_pwm_ctl(struct drm_device *dev)
Chris Wilson0b0b0532010-11-23 09:45:50 +0000323{
Jani Nikulabfd75902012-12-04 16:36:28 +0200324 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson0b0b0532010-11-23 09:45:50 +0000325 u32 val;
326
Ville Syrjälädf0a6792013-05-22 11:36:40 +0300327 WARN_ON_SMP(!spin_is_locked(&dev_priv->backlight.lock));
Jani Nikula8ba2d182013-04-12 15:18:37 +0300328
Chris Wilson0b0b0532010-11-23 09:45:50 +0000329 /* Restore the CTL value if it lost, e.g. GPU reset */
330
331 if (HAS_PCH_SPLIT(dev_priv->dev)) {
332 val = I915_READ(BLC_PWM_PCH_CTL2);
Daniel Vetterf4c956a2012-11-02 19:55:02 +0100333 if (dev_priv->regfile.saveBLC_PWM_CTL2 == 0) {
334 dev_priv->regfile.saveBLC_PWM_CTL2 = val;
Chris Wilson0b0b0532010-11-23 09:45:50 +0000335 } else if (val == 0) {
Daniel Vetterf4c956a2012-11-02 19:55:02 +0100336 val = dev_priv->regfile.saveBLC_PWM_CTL2;
Jani Nikulabfd75902012-12-04 16:36:28 +0200337 I915_WRITE(BLC_PWM_PCH_CTL2, val);
Chris Wilson0b0b0532010-11-23 09:45:50 +0000338 }
339 } else {
340 val = I915_READ(BLC_PWM_CTL);
Daniel Vetterf4c956a2012-11-02 19:55:02 +0100341 if (dev_priv->regfile.saveBLC_PWM_CTL == 0) {
342 dev_priv->regfile.saveBLC_PWM_CTL = val;
Jani Nikulabfd75902012-12-04 16:36:28 +0200343 if (INTEL_INFO(dev)->gen >= 4)
344 dev_priv->regfile.saveBLC_PWM_CTL2 =
345 I915_READ(BLC_PWM_CTL2);
Chris Wilson0b0b0532010-11-23 09:45:50 +0000346 } else if (val == 0) {
Daniel Vetterf4c956a2012-11-02 19:55:02 +0100347 val = dev_priv->regfile.saveBLC_PWM_CTL;
Jani Nikulabfd75902012-12-04 16:36:28 +0200348 I915_WRITE(BLC_PWM_CTL, val);
349 if (INTEL_INFO(dev)->gen >= 4)
350 I915_WRITE(BLC_PWM_CTL2,
351 dev_priv->regfile.saveBLC_PWM_CTL2);
Chris Wilson0b0b0532010-11-23 09:45:50 +0000352 }
353 }
354
355 return val;
356}
357
Jani Nikulad6540632013-04-12 15:18:36 +0300358static u32 intel_panel_get_max_backlight(struct drm_device *dev)
Chris Wilsona9573552010-08-22 13:18:16 +0100359{
Chris Wilsona9573552010-08-22 13:18:16 +0100360 u32 max;
361
Jani Nikulabfd75902012-12-04 16:36:28 +0200362 max = i915_read_blc_pwm_ctl(dev);
Chris Wilson0b0b0532010-11-23 09:45:50 +0000363
Chris Wilsona9573552010-08-22 13:18:16 +0100364 if (HAS_PCH_SPLIT(dev)) {
Chris Wilson0b0b0532010-11-23 09:45:50 +0000365 max >>= 16;
Chris Wilsona9573552010-08-22 13:18:16 +0100366 } else {
Keith Packardca884792011-11-18 11:09:24 -0800367 if (INTEL_INFO(dev)->gen < 4)
Chris Wilsona9573552010-08-22 13:18:16 +0100368 max >>= 17;
Keith Packardca884792011-11-18 11:09:24 -0800369 else
Chris Wilsona9573552010-08-22 13:18:16 +0100370 max >>= 16;
Takashi Iwaiba3820a2011-03-10 14:02:12 +0100371
372 if (is_backlight_combination_mode(dev))
373 max *= 0xff;
Chris Wilsona9573552010-08-22 13:18:16 +0100374 }
375
Chris Wilsona9573552010-08-22 13:18:16 +0100376 DRM_DEBUG_DRIVER("max backlight PWM = %d\n", max);
Jani Nikulad6540632013-04-12 15:18:36 +0300377
Chris Wilsona9573552010-08-22 13:18:16 +0100378 return max;
379}
380
Carsten Emde4dca20e2012-03-15 15:56:26 +0100381static int i915_panel_invert_brightness;
382MODULE_PARM_DESC(invert_brightness, "Invert backlight brightness "
383 "(-1 force normal, 0 machine defaults, 1 force inversion), please "
Carsten Emde7bd90902012-03-15 15:56:25 +0100384 "report PCI device ID, subsystem vendor and subsystem device ID "
385 "to dri-devel@lists.freedesktop.org, if your machine needs it. "
386 "It will then be included in an upcoming module version.");
Carsten Emde4dca20e2012-03-15 15:56:26 +0100387module_param_named(invert_brightness, i915_panel_invert_brightness, int, 0600);
Carsten Emde7bd90902012-03-15 15:56:25 +0100388static u32 intel_panel_compute_brightness(struct drm_device *dev, u32 val)
389{
Carsten Emde4dca20e2012-03-15 15:56:26 +0100390 struct drm_i915_private *dev_priv = dev->dev_private;
391
392 if (i915_panel_invert_brightness < 0)
393 return val;
394
395 if (i915_panel_invert_brightness > 0 ||
Jani Nikulad6540632013-04-12 15:18:36 +0300396 dev_priv->quirks & QUIRK_INVERT_BRIGHTNESS) {
397 u32 max = intel_panel_get_max_backlight(dev);
398 if (max)
399 return max - val;
400 }
Carsten Emde7bd90902012-03-15 15:56:25 +0100401
402 return val;
403}
404
Stéphane Marchesinfaea35d2012-07-30 13:51:38 -0700405static u32 intel_panel_get_backlight(struct drm_device *dev)
Chris Wilsona9573552010-08-22 13:18:16 +0100406{
407 struct drm_i915_private *dev_priv = dev->dev_private;
408 u32 val;
Jani Nikula8ba2d182013-04-12 15:18:37 +0300409 unsigned long flags;
410
411 spin_lock_irqsave(&dev_priv->backlight.lock, flags);
Chris Wilsona9573552010-08-22 13:18:16 +0100412
413 if (HAS_PCH_SPLIT(dev)) {
414 val = I915_READ(BLC_PWM_CPU_CTL) & BACKLIGHT_DUTY_CYCLE_MASK;
415 } else {
416 val = I915_READ(BLC_PWM_CTL) & BACKLIGHT_DUTY_CYCLE_MASK;
Keith Packardca884792011-11-18 11:09:24 -0800417 if (INTEL_INFO(dev)->gen < 4)
Chris Wilsona9573552010-08-22 13:18:16 +0100418 val >>= 1;
Takashi Iwaiba3820a2011-03-10 14:02:12 +0100419
Akshay Joshi0206e352011-08-16 15:34:10 -0400420 if (is_backlight_combination_mode(dev)) {
Takashi Iwaiba3820a2011-03-10 14:02:12 +0100421 u8 lbpc;
422
Takashi Iwaiba3820a2011-03-10 14:02:12 +0100423 pci_read_config_byte(dev->pdev, PCI_LBPC, &lbpc);
424 val *= lbpc;
425 }
Chris Wilsona9573552010-08-22 13:18:16 +0100426 }
427
Carsten Emde7bd90902012-03-15 15:56:25 +0100428 val = intel_panel_compute_brightness(dev, val);
Jani Nikula8ba2d182013-04-12 15:18:37 +0300429
430 spin_unlock_irqrestore(&dev_priv->backlight.lock, flags);
431
Chris Wilsona9573552010-08-22 13:18:16 +0100432 DRM_DEBUG_DRIVER("get backlight PWM = %d\n", val);
433 return val;
434}
435
436static void intel_pch_panel_set_backlight(struct drm_device *dev, u32 level)
437{
438 struct drm_i915_private *dev_priv = dev->dev_private;
439 u32 val = I915_READ(BLC_PWM_CPU_CTL) & ~BACKLIGHT_DUTY_CYCLE_MASK;
440 I915_WRITE(BLC_PWM_CPU_CTL, val | level);
441}
442
Takashi Iwaif52c6192011-10-14 11:45:40 +0200443static void intel_panel_actually_set_backlight(struct drm_device *dev, u32 level)
Chris Wilsona9573552010-08-22 13:18:16 +0100444{
445 struct drm_i915_private *dev_priv = dev->dev_private;
446 u32 tmp;
447
448 DRM_DEBUG_DRIVER("set backlight PWM = %d\n", level);
Carsten Emde7bd90902012-03-15 15:56:25 +0100449 level = intel_panel_compute_brightness(dev, level);
Chris Wilsona9573552010-08-22 13:18:16 +0100450
451 if (HAS_PCH_SPLIT(dev))
452 return intel_pch_panel_set_backlight(dev, level);
Takashi Iwaiba3820a2011-03-10 14:02:12 +0100453
Akshay Joshi0206e352011-08-16 15:34:10 -0400454 if (is_backlight_combination_mode(dev)) {
Takashi Iwaiba3820a2011-03-10 14:02:12 +0100455 u32 max = intel_panel_get_max_backlight(dev);
456 u8 lbpc;
457
Jani Nikulad6540632013-04-12 15:18:36 +0300458 /* we're screwed, but keep behaviour backwards compatible */
459 if (!max)
460 max = 1;
461
Takashi Iwaiba3820a2011-03-10 14:02:12 +0100462 lbpc = level * 0xfe / max + 1;
463 level /= lbpc;
464 pci_write_config_byte(dev->pdev, PCI_LBPC, lbpc);
465 }
466
Chris Wilsona9573552010-08-22 13:18:16 +0100467 tmp = I915_READ(BLC_PWM_CTL);
Daniel Vettera7269152012-11-20 14:50:08 +0100468 if (INTEL_INFO(dev)->gen < 4)
Chris Wilsona9573552010-08-22 13:18:16 +0100469 level <<= 1;
Keith Packardca884792011-11-18 11:09:24 -0800470 tmp &= ~BACKLIGHT_DUTY_CYCLE_MASK;
Chris Wilsona9573552010-08-22 13:18:16 +0100471 I915_WRITE(BLC_PWM_CTL, tmp | level);
472}
Chris Wilson47356eb2011-01-11 17:06:04 +0000473
Jani Nikulad6540632013-04-12 15:18:36 +0300474/* set backlight brightness to level in range [0..max] */
475void intel_panel_set_backlight(struct drm_device *dev, u32 level, u32 max)
Takashi Iwaif52c6192011-10-14 11:45:40 +0200476{
477 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikulad6540632013-04-12 15:18:36 +0300478 u32 freq;
Jani Nikula8ba2d182013-04-12 15:18:37 +0300479 unsigned long flags;
480
481 spin_lock_irqsave(&dev_priv->backlight.lock, flags);
Jani Nikulad6540632013-04-12 15:18:36 +0300482
483 freq = intel_panel_get_max_backlight(dev);
484 if (!freq) {
485 /* we are screwed, bail out */
Jani Nikula8ba2d182013-04-12 15:18:37 +0300486 goto out;
Jani Nikulad6540632013-04-12 15:18:36 +0300487 }
488
Aaron Lu22505b82013-08-02 09:16:03 +0800489 /* scale to hardware, but be careful to not overflow */
490 if (freq < max)
491 level = level * freq / max;
492 else
493 level = freq / max * level;
Takashi Iwaif52c6192011-10-14 11:45:40 +0200494
Jani Nikula31ad8ec2013-04-02 15:48:09 +0300495 dev_priv->backlight.level = level;
496 if (dev_priv->backlight.device)
497 dev_priv->backlight.device->props.brightness = level;
Jani Nikulab6b3ba52013-03-12 11:44:15 +0200498
Jani Nikula31ad8ec2013-04-02 15:48:09 +0300499 if (dev_priv->backlight.enabled)
Takashi Iwaif52c6192011-10-14 11:45:40 +0200500 intel_panel_actually_set_backlight(dev, level);
Jani Nikula8ba2d182013-04-12 15:18:37 +0300501out:
502 spin_unlock_irqrestore(&dev_priv->backlight.lock, flags);
Takashi Iwaif52c6192011-10-14 11:45:40 +0200503}
504
Chris Wilson47356eb2011-01-11 17:06:04 +0000505void intel_panel_disable_backlight(struct drm_device *dev)
506{
507 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula8ba2d182013-04-12 15:18:37 +0300508 unsigned long flags;
509
Jani Nikula3f577572013-07-25 14:31:30 +0300510 /*
511 * Do not disable backlight on the vgaswitcheroo path. When switching
512 * away from i915, the other client may depend on i915 to handle the
513 * backlight. This will leave the backlight on unnecessarily when
514 * another client is not activated.
515 */
516 if (dev->switch_power_state == DRM_SWITCH_POWER_CHANGING) {
517 DRM_DEBUG_DRIVER("Skipping backlight disable on vga switch\n");
518 return;
519 }
520
Jani Nikula8ba2d182013-04-12 15:18:37 +0300521 spin_lock_irqsave(&dev_priv->backlight.lock, flags);
Chris Wilson47356eb2011-01-11 17:06:04 +0000522
Jani Nikula31ad8ec2013-04-02 15:48:09 +0300523 dev_priv->backlight.enabled = false;
Takashi Iwaif52c6192011-10-14 11:45:40 +0200524 intel_panel_actually_set_backlight(dev, 0);
Daniel Vetter24ded202012-06-05 12:14:54 +0200525
526 if (INTEL_INFO(dev)->gen >= 4) {
Paulo Zanonia4f32fc2012-07-14 11:57:12 -0300527 uint32_t reg, tmp;
Daniel Vetter24ded202012-06-05 12:14:54 +0200528
529 reg = HAS_PCH_SPLIT(dev) ? BLC_PWM_CPU_CTL2 : BLC_PWM_CTL2;
530
531 I915_WRITE(reg, I915_READ(reg) & ~BLM_PWM_ENABLE);
Paulo Zanonia4f32fc2012-07-14 11:57:12 -0300532
533 if (HAS_PCH_SPLIT(dev)) {
534 tmp = I915_READ(BLC_PWM_PCH_CTL1);
535 tmp &= ~BLM_PCH_PWM_ENABLE;
536 I915_WRITE(BLC_PWM_PCH_CTL1, tmp);
537 }
Daniel Vetter24ded202012-06-05 12:14:54 +0200538 }
Jani Nikula8ba2d182013-04-12 15:18:37 +0300539
540 spin_unlock_irqrestore(&dev_priv->backlight.lock, flags);
Chris Wilson47356eb2011-01-11 17:06:04 +0000541}
542
Daniel Vetter24ded202012-06-05 12:14:54 +0200543void intel_panel_enable_backlight(struct drm_device *dev,
544 enum pipe pipe)
Chris Wilson47356eb2011-01-11 17:06:04 +0000545{
546 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula35ffda42013-04-25 16:49:25 +0300547 enum transcoder cpu_transcoder =
548 intel_pipe_to_cpu_transcoder(dev_priv, pipe);
Jani Nikula8ba2d182013-04-12 15:18:37 +0300549 unsigned long flags;
550
551 spin_lock_irqsave(&dev_priv->backlight.lock, flags);
Chris Wilson47356eb2011-01-11 17:06:04 +0000552
Jani Nikula31ad8ec2013-04-02 15:48:09 +0300553 if (dev_priv->backlight.level == 0) {
554 dev_priv->backlight.level = intel_panel_get_max_backlight(dev);
555 if (dev_priv->backlight.device)
556 dev_priv->backlight.device->props.brightness =
557 dev_priv->backlight.level;
Jani Nikulab6b3ba52013-03-12 11:44:15 +0200558 }
Chris Wilson47356eb2011-01-11 17:06:04 +0000559
Daniel Vetter24ded202012-06-05 12:14:54 +0200560 if (INTEL_INFO(dev)->gen >= 4) {
561 uint32_t reg, tmp;
562
563 reg = HAS_PCH_SPLIT(dev) ? BLC_PWM_CPU_CTL2 : BLC_PWM_CTL2;
564
565
566 tmp = I915_READ(reg);
567
568 /* Note that this can also get called through dpms changes. And
569 * we don't track the backlight dpms state, hence check whether
570 * we have to do anything first. */
571 if (tmp & BLM_PWM_ENABLE)
Takashi Iwai770c1232012-08-11 08:56:42 +0200572 goto set_level;
Daniel Vetter24ded202012-06-05 12:14:54 +0200573
Ben Widawsky7eb552a2013-03-13 14:05:41 -0700574 if (INTEL_INFO(dev)->num_pipes == 3)
Daniel Vetter24ded202012-06-05 12:14:54 +0200575 tmp &= ~BLM_PIPE_SELECT_IVB;
576 else
577 tmp &= ~BLM_PIPE_SELECT;
578
Jani Nikula35ffda42013-04-25 16:49:25 +0300579 if (cpu_transcoder == TRANSCODER_EDP)
580 tmp |= BLM_TRANSCODER_EDP;
581 else
582 tmp |= BLM_PIPE(cpu_transcoder);
Daniel Vetter24ded202012-06-05 12:14:54 +0200583 tmp &= ~BLM_PWM_ENABLE;
584
585 I915_WRITE(reg, tmp);
586 POSTING_READ(reg);
587 I915_WRITE(reg, tmp | BLM_PWM_ENABLE);
Paulo Zanonia4f32fc2012-07-14 11:57:12 -0300588
Kamal Mostafae85843b2013-07-19 15:02:01 -0700589 if (HAS_PCH_SPLIT(dev) &&
590 !(dev_priv->quirks & QUIRK_NO_PCH_PWM_ENABLE)) {
Paulo Zanonia4f32fc2012-07-14 11:57:12 -0300591 tmp = I915_READ(BLC_PWM_PCH_CTL1);
592 tmp |= BLM_PCH_PWM_ENABLE;
593 tmp &= ~BLM_PCH_OVERRIDE_ENABLE;
594 I915_WRITE(BLC_PWM_PCH_CTL1, tmp);
595 }
Daniel Vetter24ded202012-06-05 12:14:54 +0200596 }
Takashi Iwai770c1232012-08-11 08:56:42 +0200597
598set_level:
Daniel Vetterb1289372013-03-22 15:44:46 +0100599 /* Call below after setting BLC_PWM_CPU_CTL2 and BLC_PWM_PCH_CTL1.
600 * BLC_PWM_CPU_CTL may be cleared to zero automatically when these
601 * registers are set.
Takashi Iwai770c1232012-08-11 08:56:42 +0200602 */
Daniel Vetterecb135a2013-04-03 11:25:32 +0200603 dev_priv->backlight.enabled = true;
604 intel_panel_actually_set_backlight(dev, dev_priv->backlight.level);
Jani Nikula8ba2d182013-04-12 15:18:37 +0300605
606 spin_unlock_irqrestore(&dev_priv->backlight.lock, flags);
Chris Wilson47356eb2011-01-11 17:06:04 +0000607}
608
Matthew Garrettaaa6fd22011-08-12 12:11:33 +0200609static void intel_panel_init_backlight(struct drm_device *dev)
Chris Wilson47356eb2011-01-11 17:06:04 +0000610{
611 struct drm_i915_private *dev_priv = dev->dev_private;
612
Jani Nikula31ad8ec2013-04-02 15:48:09 +0300613 dev_priv->backlight.level = intel_panel_get_backlight(dev);
614 dev_priv->backlight.enabled = dev_priv->backlight.level != 0;
Chris Wilson47356eb2011-01-11 17:06:04 +0000615}
Chris Wilsonfe16d942011-02-12 10:29:38 +0000616
617enum drm_connector_status
618intel_panel_detect(struct drm_device *dev)
619{
620 struct drm_i915_private *dev_priv = dev->dev_private;
621
622 /* Assume that the BIOS does not lie through the OpRegion... */
Daniel Vettera7269152012-11-20 14:50:08 +0100623 if (!i915_panel_ignore_lid && dev_priv->opregion.lid_state) {
Chris Wilsonfe16d942011-02-12 10:29:38 +0000624 return ioread32(dev_priv->opregion.lid_state) & 0x1 ?
625 connector_status_connected :
626 connector_status_disconnected;
Daniel Vettera7269152012-11-20 14:50:08 +0100627 }
Chris Wilsonfe16d942011-02-12 10:29:38 +0000628
Daniel Vettera7269152012-11-20 14:50:08 +0100629 switch (i915_panel_ignore_lid) {
630 case -2:
631 return connector_status_connected;
632 case -1:
633 return connector_status_disconnected;
634 default:
635 return connector_status_unknown;
636 }
Chris Wilsonfe16d942011-02-12 10:29:38 +0000637}
Matthew Garrettaaa6fd22011-08-12 12:11:33 +0200638
639#ifdef CONFIG_BACKLIGHT_CLASS_DEVICE
640static int intel_panel_update_status(struct backlight_device *bd)
641{
642 struct drm_device *dev = bl_get_data(bd);
Jani Nikulad6540632013-04-12 15:18:36 +0300643 intel_panel_set_backlight(dev, bd->props.brightness,
644 bd->props.max_brightness);
Matthew Garrettaaa6fd22011-08-12 12:11:33 +0200645 return 0;
646}
647
648static int intel_panel_get_brightness(struct backlight_device *bd)
649{
650 struct drm_device *dev = bl_get_data(bd);
Jani Nikula7c233962013-03-12 11:44:16 +0200651 return intel_panel_get_backlight(dev);
Matthew Garrettaaa6fd22011-08-12 12:11:33 +0200652}
653
654static const struct backlight_ops intel_panel_bl_ops = {
655 .update_status = intel_panel_update_status,
656 .get_brightness = intel_panel_get_brightness,
657};
658
Jani Nikula0657b6b2012-10-19 14:51:46 +0300659int intel_panel_setup_backlight(struct drm_connector *connector)
Matthew Garrettaaa6fd22011-08-12 12:11:33 +0200660{
Jani Nikula0657b6b2012-10-19 14:51:46 +0300661 struct drm_device *dev = connector->dev;
Matthew Garrettaaa6fd22011-08-12 12:11:33 +0200662 struct drm_i915_private *dev_priv = dev->dev_private;
663 struct backlight_properties props;
Jani Nikula8ba2d182013-04-12 15:18:37 +0300664 unsigned long flags;
Matthew Garrettaaa6fd22011-08-12 12:11:33 +0200665
666 intel_panel_init_backlight(dev);
667
Jani Nikuladc652f92013-04-12 15:18:38 +0300668 if (WARN_ON(dev_priv->backlight.device))
669 return -ENODEV;
670
Corentin Charyaf437cf2012-05-22 10:29:46 +0100671 memset(&props, 0, sizeof(props));
Matthew Garrettaaa6fd22011-08-12 12:11:33 +0200672 props.type = BACKLIGHT_RAW;
Jani Nikula31ad8ec2013-04-02 15:48:09 +0300673 props.brightness = dev_priv->backlight.level;
Jani Nikula8ba2d182013-04-12 15:18:37 +0300674
675 spin_lock_irqsave(&dev_priv->backlight.lock, flags);
Jani Nikulad6540632013-04-12 15:18:36 +0300676 props.max_brightness = intel_panel_get_max_backlight(dev);
Jani Nikula8ba2d182013-04-12 15:18:37 +0300677 spin_unlock_irqrestore(&dev_priv->backlight.lock, flags);
678
Jani Nikula28dcc2d2012-09-03 16:25:12 +0300679 if (props.max_brightness == 0) {
Jani Nikulae86b6182012-10-25 10:57:38 +0300680 DRM_DEBUG_DRIVER("Failed to get maximum backlight value\n");
Jani Nikula28dcc2d2012-09-03 16:25:12 +0300681 return -ENODEV;
682 }
Jani Nikula31ad8ec2013-04-02 15:48:09 +0300683 dev_priv->backlight.device =
Matthew Garrettaaa6fd22011-08-12 12:11:33 +0200684 backlight_device_register("intel_backlight",
685 &connector->kdev, dev,
686 &intel_panel_bl_ops, &props);
687
Jani Nikula31ad8ec2013-04-02 15:48:09 +0300688 if (IS_ERR(dev_priv->backlight.device)) {
Matthew Garrettaaa6fd22011-08-12 12:11:33 +0200689 DRM_ERROR("Failed to register backlight: %ld\n",
Jani Nikula31ad8ec2013-04-02 15:48:09 +0300690 PTR_ERR(dev_priv->backlight.device));
691 dev_priv->backlight.device = NULL;
Matthew Garrettaaa6fd22011-08-12 12:11:33 +0200692 return -ENODEV;
693 }
Matthew Garrettaaa6fd22011-08-12 12:11:33 +0200694 return 0;
695}
696
697void intel_panel_destroy_backlight(struct drm_device *dev)
698{
699 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikuladc652f92013-04-12 15:18:38 +0300700 if (dev_priv->backlight.device) {
Jani Nikula31ad8ec2013-04-02 15:48:09 +0300701 backlight_device_unregister(dev_priv->backlight.device);
Jani Nikuladc652f92013-04-12 15:18:38 +0300702 dev_priv->backlight.device = NULL;
703 }
Matthew Garrettaaa6fd22011-08-12 12:11:33 +0200704}
705#else
Jani Nikula0657b6b2012-10-19 14:51:46 +0300706int intel_panel_setup_backlight(struct drm_connector *connector)
Matthew Garrettaaa6fd22011-08-12 12:11:33 +0200707{
Jani Nikula0657b6b2012-10-19 14:51:46 +0300708 intel_panel_init_backlight(connector->dev);
Matthew Garrettaaa6fd22011-08-12 12:11:33 +0200709 return 0;
710}
711
712void intel_panel_destroy_backlight(struct drm_device *dev)
713{
714 return;
715}
716#endif
Jani Nikula1d508702012-10-19 14:51:49 +0300717
Jani Nikuladd06f902012-10-19 14:51:50 +0300718int intel_panel_init(struct intel_panel *panel,
719 struct drm_display_mode *fixed_mode)
Jani Nikula1d508702012-10-19 14:51:49 +0300720{
Jani Nikuladd06f902012-10-19 14:51:50 +0300721 panel->fixed_mode = fixed_mode;
722
Jani Nikula1d508702012-10-19 14:51:49 +0300723 return 0;
724}
725
726void intel_panel_fini(struct intel_panel *panel)
727{
Jani Nikuladd06f902012-10-19 14:51:50 +0300728 struct intel_connector *intel_connector =
729 container_of(panel, struct intel_connector, panel);
730
731 if (panel->fixed_mode)
732 drm_mode_destroy(intel_connector->base.dev, panel->fixed_mode);
Jani Nikula1d508702012-10-19 14:51:49 +0300733}