blob: cad45ff8251b004a7484c2efa4664b3ec64b77c7 [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
39intel_fixed_panel_mode(struct drm_display_mode *fixed_mode,
40 struct drm_display_mode *adjusted_mode)
41{
42 adjusted_mode->hdisplay = fixed_mode->hdisplay;
43 adjusted_mode->hsync_start = fixed_mode->hsync_start;
44 adjusted_mode->hsync_end = fixed_mode->hsync_end;
45 adjusted_mode->htotal = fixed_mode->htotal;
46
47 adjusted_mode->vdisplay = fixed_mode->vdisplay;
48 adjusted_mode->vsync_start = fixed_mode->vsync_start;
49 adjusted_mode->vsync_end = fixed_mode->vsync_end;
50 adjusted_mode->vtotal = fixed_mode->vtotal;
51
52 adjusted_mode->clock = fixed_mode->clock;
53
Daniel Vetterca9bfa72012-01-28 14:49:20 +010054 drm_mode_set_crtcinfo(adjusted_mode, 0);
Chris Wilson1d8e1c72010-08-07 11:01:28 +010055}
56
57/* adjusted_mode has been preset to be the panel's fixed mode */
58void
59intel_pch_panel_fitting(struct drm_device *dev,
60 int fitting_mode,
61 struct drm_display_mode *mode,
62 struct drm_display_mode *adjusted_mode)
63{
64 struct drm_i915_private *dev_priv = dev->dev_private;
65 int x, y, width, height;
66
67 x = y = width = height = 0;
68
69 /* Native modes don't need fitting */
70 if (adjusted_mode->hdisplay == mode->hdisplay &&
71 adjusted_mode->vdisplay == mode->vdisplay)
72 goto done;
73
74 switch (fitting_mode) {
75 case DRM_MODE_SCALE_CENTER:
76 width = mode->hdisplay;
77 height = mode->vdisplay;
78 x = (adjusted_mode->hdisplay - width + 1)/2;
79 y = (adjusted_mode->vdisplay - height + 1)/2;
80 break;
81
82 case DRM_MODE_SCALE_ASPECT:
83 /* Scale but preserve the aspect ratio */
84 {
85 u32 scaled_width = adjusted_mode->hdisplay * mode->vdisplay;
86 u32 scaled_height = mode->hdisplay * adjusted_mode->vdisplay;
87 if (scaled_width > scaled_height) { /* pillar */
88 width = scaled_height / mode->vdisplay;
Adam Jackson302983e2011-07-13 16:32:32 -040089 if (width & 1)
Akshay Joshi0206e352011-08-16 15:34:10 -040090 width++;
Chris Wilson1d8e1c72010-08-07 11:01:28 +010091 x = (adjusted_mode->hdisplay - width + 1) / 2;
92 y = 0;
93 height = adjusted_mode->vdisplay;
94 } else if (scaled_width < scaled_height) { /* letter */
95 height = scaled_width / mode->hdisplay;
Adam Jackson302983e2011-07-13 16:32:32 -040096 if (height & 1)
97 height++;
Chris Wilson1d8e1c72010-08-07 11:01:28 +010098 y = (adjusted_mode->vdisplay - height + 1) / 2;
99 x = 0;
100 width = adjusted_mode->hdisplay;
101 } else {
102 x = y = 0;
103 width = adjusted_mode->hdisplay;
104 height = adjusted_mode->vdisplay;
105 }
106 }
107 break;
108
109 default:
110 case DRM_MODE_SCALE_FULLSCREEN:
111 x = y = 0;
112 width = adjusted_mode->hdisplay;
113 height = adjusted_mode->vdisplay;
114 break;
115 }
116
117done:
118 dev_priv->pch_pf_pos = (x << 16) | y;
119 dev_priv->pch_pf_size = (width << 16) | height;
120}
Chris Wilsona9573552010-08-22 13:18:16 +0100121
Takashi Iwaiba3820a2011-03-10 14:02:12 +0100122static int is_backlight_combination_mode(struct drm_device *dev)
123{
124 struct drm_i915_private *dev_priv = dev->dev_private;
125
126 if (INTEL_INFO(dev)->gen >= 4)
127 return I915_READ(BLC_PWM_CTL2) & BLM_COMBINATION_MODE;
128
129 if (IS_GEN2(dev))
130 return I915_READ(BLC_PWM_CTL) & BLM_LEGACY_MODE;
131
132 return 0;
133}
134
Chris Wilson0b0b0532010-11-23 09:45:50 +0000135static u32 i915_read_blc_pwm_ctl(struct drm_i915_private *dev_priv)
136{
137 u32 val;
138
139 /* Restore the CTL value if it lost, e.g. GPU reset */
140
141 if (HAS_PCH_SPLIT(dev_priv->dev)) {
142 val = I915_READ(BLC_PWM_PCH_CTL2);
143 if (dev_priv->saveBLC_PWM_CTL2 == 0) {
144 dev_priv->saveBLC_PWM_CTL2 = val;
145 } else if (val == 0) {
146 I915_WRITE(BLC_PWM_PCH_CTL2,
Simon Que2aded1b2011-11-10 17:50:26 -0800147 dev_priv->saveBLC_PWM_CTL2);
148 val = dev_priv->saveBLC_PWM_CTL2;
Chris Wilson0b0b0532010-11-23 09:45:50 +0000149 }
150 } else {
151 val = I915_READ(BLC_PWM_CTL);
152 if (dev_priv->saveBLC_PWM_CTL == 0) {
153 dev_priv->saveBLC_PWM_CTL = val;
154 dev_priv->saveBLC_PWM_CTL2 = I915_READ(BLC_PWM_CTL2);
155 } else if (val == 0) {
156 I915_WRITE(BLC_PWM_CTL,
157 dev_priv->saveBLC_PWM_CTL);
158 I915_WRITE(BLC_PWM_CTL2,
159 dev_priv->saveBLC_PWM_CTL2);
160 val = dev_priv->saveBLC_PWM_CTL;
161 }
162 }
163
164 return val;
165}
166
Chris Wilsona9573552010-08-22 13:18:16 +0100167u32 intel_panel_get_max_backlight(struct drm_device *dev)
168{
169 struct drm_i915_private *dev_priv = dev->dev_private;
170 u32 max;
171
Chris Wilson0b0b0532010-11-23 09:45:50 +0000172 max = i915_read_blc_pwm_ctl(dev_priv);
173 if (max == 0) {
174 /* XXX add code here to query mode clock or hardware clock
175 * and program max PWM appropriately.
176 */
Joe Perchesa70491c2012-03-18 13:00:11 -0700177 pr_warn_once("fixme: max PWM is zero\n");
Chris Wilson0b0b0532010-11-23 09:45:50 +0000178 return 1;
179 }
180
Chris Wilsona9573552010-08-22 13:18:16 +0100181 if (HAS_PCH_SPLIT(dev)) {
Chris Wilson0b0b0532010-11-23 09:45:50 +0000182 max >>= 16;
Chris Wilsona9573552010-08-22 13:18:16 +0100183 } else {
Keith Packardca884792011-11-18 11:09:24 -0800184 if (INTEL_INFO(dev)->gen < 4)
Chris Wilsona9573552010-08-22 13:18:16 +0100185 max >>= 17;
Keith Packardca884792011-11-18 11:09:24 -0800186 else
Chris Wilsona9573552010-08-22 13:18:16 +0100187 max >>= 16;
Takashi Iwaiba3820a2011-03-10 14:02:12 +0100188
189 if (is_backlight_combination_mode(dev))
190 max *= 0xff;
Chris Wilsona9573552010-08-22 13:18:16 +0100191 }
192
Chris Wilsona9573552010-08-22 13:18:16 +0100193 DRM_DEBUG_DRIVER("max backlight PWM = %d\n", max);
194 return max;
195}
196
Carsten Emde4dca20e2012-03-15 15:56:26 +0100197static int i915_panel_invert_brightness;
198MODULE_PARM_DESC(invert_brightness, "Invert backlight brightness "
199 "(-1 force normal, 0 machine defaults, 1 force inversion), please "
Carsten Emde7bd90902012-03-15 15:56:25 +0100200 "report PCI device ID, subsystem vendor and subsystem device ID "
201 "to dri-devel@lists.freedesktop.org, if your machine needs it. "
202 "It will then be included in an upcoming module version.");
Carsten Emde4dca20e2012-03-15 15:56:26 +0100203module_param_named(invert_brightness, i915_panel_invert_brightness, int, 0600);
Carsten Emde7bd90902012-03-15 15:56:25 +0100204static u32 intel_panel_compute_brightness(struct drm_device *dev, u32 val)
205{
Carsten Emde4dca20e2012-03-15 15:56:26 +0100206 struct drm_i915_private *dev_priv = dev->dev_private;
207
208 if (i915_panel_invert_brightness < 0)
209 return val;
210
211 if (i915_panel_invert_brightness > 0 ||
212 dev_priv->quirks & QUIRK_INVERT_BRIGHTNESS)
Carsten Emde7bd90902012-03-15 15:56:25 +0100213 return intel_panel_get_max_backlight(dev) - val;
214
215 return val;
216}
217
Chris Wilsona9573552010-08-22 13:18:16 +0100218u32 intel_panel_get_backlight(struct drm_device *dev)
219{
220 struct drm_i915_private *dev_priv = dev->dev_private;
221 u32 val;
222
223 if (HAS_PCH_SPLIT(dev)) {
224 val = I915_READ(BLC_PWM_CPU_CTL) & BACKLIGHT_DUTY_CYCLE_MASK;
225 } else {
226 val = I915_READ(BLC_PWM_CTL) & BACKLIGHT_DUTY_CYCLE_MASK;
Keith Packardca884792011-11-18 11:09:24 -0800227 if (INTEL_INFO(dev)->gen < 4)
Chris Wilsona9573552010-08-22 13:18:16 +0100228 val >>= 1;
Takashi Iwaiba3820a2011-03-10 14:02:12 +0100229
Akshay Joshi0206e352011-08-16 15:34:10 -0400230 if (is_backlight_combination_mode(dev)) {
Takashi Iwaiba3820a2011-03-10 14:02:12 +0100231 u8 lbpc;
232
Takashi Iwaiba3820a2011-03-10 14:02:12 +0100233 pci_read_config_byte(dev->pdev, PCI_LBPC, &lbpc);
234 val *= lbpc;
235 }
Chris Wilsona9573552010-08-22 13:18:16 +0100236 }
237
Carsten Emde7bd90902012-03-15 15:56:25 +0100238 val = intel_panel_compute_brightness(dev, val);
Chris Wilsona9573552010-08-22 13:18:16 +0100239 DRM_DEBUG_DRIVER("get backlight PWM = %d\n", val);
240 return val;
241}
242
243static void intel_pch_panel_set_backlight(struct drm_device *dev, u32 level)
244{
245 struct drm_i915_private *dev_priv = dev->dev_private;
246 u32 val = I915_READ(BLC_PWM_CPU_CTL) & ~BACKLIGHT_DUTY_CYCLE_MASK;
247 I915_WRITE(BLC_PWM_CPU_CTL, val | level);
248}
249
Takashi Iwaif52c6192011-10-14 11:45:40 +0200250static void intel_panel_actually_set_backlight(struct drm_device *dev, u32 level)
Chris Wilsona9573552010-08-22 13:18:16 +0100251{
252 struct drm_i915_private *dev_priv = dev->dev_private;
253 u32 tmp;
254
255 DRM_DEBUG_DRIVER("set backlight PWM = %d\n", level);
Carsten Emde7bd90902012-03-15 15:56:25 +0100256 level = intel_panel_compute_brightness(dev, level);
Chris Wilsona9573552010-08-22 13:18:16 +0100257
258 if (HAS_PCH_SPLIT(dev))
259 return intel_pch_panel_set_backlight(dev, level);
Takashi Iwaiba3820a2011-03-10 14:02:12 +0100260
Akshay Joshi0206e352011-08-16 15:34:10 -0400261 if (is_backlight_combination_mode(dev)) {
Takashi Iwaiba3820a2011-03-10 14:02:12 +0100262 u32 max = intel_panel_get_max_backlight(dev);
263 u8 lbpc;
264
265 lbpc = level * 0xfe / max + 1;
266 level /= lbpc;
267 pci_write_config_byte(dev->pdev, PCI_LBPC, lbpc);
268 }
269
Chris Wilsona9573552010-08-22 13:18:16 +0100270 tmp = I915_READ(BLC_PWM_CTL);
Keith Packardca884792011-11-18 11:09:24 -0800271 if (INTEL_INFO(dev)->gen < 4)
Chris Wilsona9573552010-08-22 13:18:16 +0100272 level <<= 1;
Keith Packardca884792011-11-18 11:09:24 -0800273 tmp &= ~BACKLIGHT_DUTY_CYCLE_MASK;
Chris Wilsona9573552010-08-22 13:18:16 +0100274 I915_WRITE(BLC_PWM_CTL, tmp | level);
275}
Chris Wilson47356eb2011-01-11 17:06:04 +0000276
Takashi Iwaif52c6192011-10-14 11:45:40 +0200277void intel_panel_set_backlight(struct drm_device *dev, u32 level)
278{
279 struct drm_i915_private *dev_priv = dev->dev_private;
280
281 dev_priv->backlight_level = level;
282 if (dev_priv->backlight_enabled)
283 intel_panel_actually_set_backlight(dev, level);
284}
285
Chris Wilson47356eb2011-01-11 17:06:04 +0000286void intel_panel_disable_backlight(struct drm_device *dev)
287{
288 struct drm_i915_private *dev_priv = dev->dev_private;
289
Takashi Iwaif52c6192011-10-14 11:45:40 +0200290 dev_priv->backlight_enabled = false;
291 intel_panel_actually_set_backlight(dev, 0);
Chris Wilson47356eb2011-01-11 17:06:04 +0000292}
293
294void intel_panel_enable_backlight(struct drm_device *dev)
295{
296 struct drm_i915_private *dev_priv = dev->dev_private;
297
298 if (dev_priv->backlight_level == 0)
299 dev_priv->backlight_level = intel_panel_get_max_backlight(dev);
300
Chris Wilson47356eb2011-01-11 17:06:04 +0000301 dev_priv->backlight_enabled = true;
Takashi Iwaif52c6192011-10-14 11:45:40 +0200302 intel_panel_actually_set_backlight(dev, dev_priv->backlight_level);
Chris Wilson47356eb2011-01-11 17:06:04 +0000303}
304
Matthew Garrettaaa6fd22011-08-12 12:11:33 +0200305static void intel_panel_init_backlight(struct drm_device *dev)
Chris Wilson47356eb2011-01-11 17:06:04 +0000306{
307 struct drm_i915_private *dev_priv = dev->dev_private;
308
Indan Zupancicc8303e72011-01-12 11:59:19 +0000309 dev_priv->backlight_level = intel_panel_get_backlight(dev);
Chris Wilson47356eb2011-01-11 17:06:04 +0000310 dev_priv->backlight_enabled = dev_priv->backlight_level != 0;
311}
Chris Wilsonfe16d942011-02-12 10:29:38 +0000312
313enum drm_connector_status
314intel_panel_detect(struct drm_device *dev)
315{
Dave Airliebcd50232011-03-14 14:17:55 +1000316#if 0
Chris Wilsonfe16d942011-02-12 10:29:38 +0000317 struct drm_i915_private *dev_priv = dev->dev_private;
Dave Airliebcd50232011-03-14 14:17:55 +1000318#endif
Chris Wilsonfe16d942011-02-12 10:29:38 +0000319
Chris Wilsonfca87402011-02-17 13:44:48 +0000320 if (i915_panel_ignore_lid)
321 return i915_panel_ignore_lid > 0 ?
322 connector_status_connected :
323 connector_status_disconnected;
324
Dave Airliebcd50232011-03-14 14:17:55 +1000325 /* opregion lid state on HP 2540p is wrong at boot up,
326 * appears to be either the BIOS or Linux ACPI fault */
327#if 0
Chris Wilsonfe16d942011-02-12 10:29:38 +0000328 /* Assume that the BIOS does not lie through the OpRegion... */
329 if (dev_priv->opregion.lid_state)
330 return ioread32(dev_priv->opregion.lid_state) & 0x1 ?
331 connector_status_connected :
332 connector_status_disconnected;
Dave Airliebcd50232011-03-14 14:17:55 +1000333#endif
Chris Wilsonfe16d942011-02-12 10:29:38 +0000334
335 return connector_status_unknown;
336}
Matthew Garrettaaa6fd22011-08-12 12:11:33 +0200337
338#ifdef CONFIG_BACKLIGHT_CLASS_DEVICE
339static int intel_panel_update_status(struct backlight_device *bd)
340{
341 struct drm_device *dev = bl_get_data(bd);
342 intel_panel_set_backlight(dev, bd->props.brightness);
343 return 0;
344}
345
346static int intel_panel_get_brightness(struct backlight_device *bd)
347{
348 struct drm_device *dev = bl_get_data(bd);
Takashi Iwai04b38672011-11-16 10:58:03 +0100349 struct drm_i915_private *dev_priv = dev->dev_private;
350 return dev_priv->backlight_level;
Matthew Garrettaaa6fd22011-08-12 12:11:33 +0200351}
352
353static const struct backlight_ops intel_panel_bl_ops = {
354 .update_status = intel_panel_update_status,
355 .get_brightness = intel_panel_get_brightness,
356};
357
358int intel_panel_setup_backlight(struct drm_device *dev)
359{
360 struct drm_i915_private *dev_priv = dev->dev_private;
361 struct backlight_properties props;
362 struct drm_connector *connector;
363
364 intel_panel_init_backlight(dev);
365
366 if (dev_priv->int_lvds_connector)
367 connector = dev_priv->int_lvds_connector;
368 else if (dev_priv->int_edp_connector)
369 connector = dev_priv->int_edp_connector;
370 else
371 return -ENODEV;
372
373 props.type = BACKLIGHT_RAW;
374 props.max_brightness = intel_panel_get_max_backlight(dev);
375 dev_priv->backlight =
376 backlight_device_register("intel_backlight",
377 &connector->kdev, dev,
378 &intel_panel_bl_ops, &props);
379
380 if (IS_ERR(dev_priv->backlight)) {
381 DRM_ERROR("Failed to register backlight: %ld\n",
382 PTR_ERR(dev_priv->backlight));
383 dev_priv->backlight = NULL;
384 return -ENODEV;
385 }
386 dev_priv->backlight->props.brightness = intel_panel_get_backlight(dev);
387 return 0;
388}
389
390void intel_panel_destroy_backlight(struct drm_device *dev)
391{
392 struct drm_i915_private *dev_priv = dev->dev_private;
393 if (dev_priv->backlight)
394 backlight_device_unregister(dev_priv->backlight);
395}
396#else
397int intel_panel_setup_backlight(struct drm_device *dev)
398{
399 intel_panel_init_backlight(dev);
400 return 0;
401}
402
403void intel_panel_destroy_backlight(struct drm_device *dev)
404{
405 return;
406}
407#endif