Merge branch 'for-airlied' of git://people.freedesktop.org/~danvet/drm-intel into...
[linux-2.6/libata-dev.git] / drivers / gpu / drm / i915 / intel_panel.c
blob41d463573baa32b149742ea3dd5637e62e6181c3
1 /*
2 * Copyright © 2006-2010 Intel Corporation
3 * Copyright (c) 2006 Dave Airlie <airlied@linux.ie>
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
14 * Software.
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.
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>
31 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
33 #include <linux/moduleparam.h>
34 #include "intel_drv.h"
36 #define PCI_LBPC 0xf4 /* legacy/combination backlight modes */
38 void
39 intel_fixed_panel_mode(struct drm_display_mode *fixed_mode,
40 struct drm_display_mode *adjusted_mode)
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;
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;
52 adjusted_mode->clock = fixed_mode->clock;
55 /* adjusted_mode has been preset to be the panel's fixed mode */
56 void
57 intel_pch_panel_fitting(struct drm_device *dev,
58 int fitting_mode,
59 const struct drm_display_mode *mode,
60 struct drm_display_mode *adjusted_mode)
62 struct drm_i915_private *dev_priv = dev->dev_private;
63 int x, y, width, height;
65 x = y = width = height = 0;
67 /* Native modes don't need fitting */
68 if (adjusted_mode->hdisplay == mode->hdisplay &&
69 adjusted_mode->vdisplay == mode->vdisplay)
70 goto done;
72 switch (fitting_mode) {
73 case DRM_MODE_SCALE_CENTER:
74 width = mode->hdisplay;
75 height = mode->vdisplay;
76 x = (adjusted_mode->hdisplay - width + 1)/2;
77 y = (adjusted_mode->vdisplay - height + 1)/2;
78 break;
80 case DRM_MODE_SCALE_ASPECT:
81 /* Scale but preserve the aspect ratio */
83 u32 scaled_width = adjusted_mode->hdisplay * mode->vdisplay;
84 u32 scaled_height = mode->hdisplay * adjusted_mode->vdisplay;
85 if (scaled_width > scaled_height) { /* pillar */
86 width = scaled_height / mode->vdisplay;
87 if (width & 1)
88 width++;
89 x = (adjusted_mode->hdisplay - width + 1) / 2;
90 y = 0;
91 height = adjusted_mode->vdisplay;
92 } else if (scaled_width < scaled_height) { /* letter */
93 height = scaled_width / mode->hdisplay;
94 if (height & 1)
95 height++;
96 y = (adjusted_mode->vdisplay - height + 1) / 2;
97 x = 0;
98 width = adjusted_mode->hdisplay;
99 } else {
100 x = y = 0;
101 width = adjusted_mode->hdisplay;
102 height = adjusted_mode->vdisplay;
105 break;
107 default:
108 case DRM_MODE_SCALE_FULLSCREEN:
109 x = y = 0;
110 width = adjusted_mode->hdisplay;
111 height = adjusted_mode->vdisplay;
112 break;
115 done:
116 dev_priv->pch_pf_pos = (x << 16) | y;
117 dev_priv->pch_pf_size = (width << 16) | height;
120 static int is_backlight_combination_mode(struct drm_device *dev)
122 struct drm_i915_private *dev_priv = dev->dev_private;
124 if (INTEL_INFO(dev)->gen >= 4)
125 return I915_READ(BLC_PWM_CTL2) & BLM_COMBINATION_MODE;
127 if (IS_GEN2(dev))
128 return I915_READ(BLC_PWM_CTL) & BLM_LEGACY_MODE;
130 return 0;
133 static u32 i915_read_blc_pwm_ctl(struct drm_i915_private *dev_priv)
135 u32 val;
137 /* Restore the CTL value if it lost, e.g. GPU reset */
139 if (HAS_PCH_SPLIT(dev_priv->dev)) {
140 val = I915_READ(BLC_PWM_PCH_CTL2);
141 if (dev_priv->regfile.saveBLC_PWM_CTL2 == 0) {
142 dev_priv->regfile.saveBLC_PWM_CTL2 = val;
143 } else if (val == 0) {
144 I915_WRITE(BLC_PWM_PCH_CTL2,
145 dev_priv->regfile.saveBLC_PWM_CTL2);
146 val = dev_priv->regfile.saveBLC_PWM_CTL2;
148 } else {
149 val = I915_READ(BLC_PWM_CTL);
150 if (dev_priv->regfile.saveBLC_PWM_CTL == 0) {
151 dev_priv->regfile.saveBLC_PWM_CTL = val;
152 dev_priv->regfile.saveBLC_PWM_CTL2 = I915_READ(BLC_PWM_CTL2);
153 } else if (val == 0) {
154 I915_WRITE(BLC_PWM_CTL,
155 dev_priv->regfile.saveBLC_PWM_CTL);
156 I915_WRITE(BLC_PWM_CTL2,
157 dev_priv->regfile.saveBLC_PWM_CTL2);
158 val = dev_priv->regfile.saveBLC_PWM_CTL;
162 return val;
165 static u32 _intel_panel_get_max_backlight(struct drm_device *dev)
167 struct drm_i915_private *dev_priv = dev->dev_private;
168 u32 max;
170 max = i915_read_blc_pwm_ctl(dev_priv);
172 if (HAS_PCH_SPLIT(dev)) {
173 max >>= 16;
174 } else {
175 if (INTEL_INFO(dev)->gen < 4)
176 max >>= 17;
177 else
178 max >>= 16;
180 if (is_backlight_combination_mode(dev))
181 max *= 0xff;
184 return max;
187 u32 intel_panel_get_max_backlight(struct drm_device *dev)
189 u32 max;
191 max = _intel_panel_get_max_backlight(dev);
192 if (max == 0) {
193 /* XXX add code here to query mode clock or hardware clock
194 * and program max PWM appropriately.
196 pr_warn_once("fixme: max PWM is zero\n");
197 return 1;
200 DRM_DEBUG_DRIVER("max backlight PWM = %d\n", max);
201 return max;
204 static int i915_panel_invert_brightness;
205 MODULE_PARM_DESC(invert_brightness, "Invert backlight brightness "
206 "(-1 force normal, 0 machine defaults, 1 force inversion), please "
207 "report PCI device ID, subsystem vendor and subsystem device ID "
208 "to dri-devel@lists.freedesktop.org, if your machine needs it. "
209 "It will then be included in an upcoming module version.");
210 module_param_named(invert_brightness, i915_panel_invert_brightness, int, 0600);
211 static u32 intel_panel_compute_brightness(struct drm_device *dev, u32 val)
213 struct drm_i915_private *dev_priv = dev->dev_private;
215 if (i915_panel_invert_brightness < 0)
216 return val;
218 if (i915_panel_invert_brightness > 0 ||
219 dev_priv->quirks & QUIRK_INVERT_BRIGHTNESS)
220 return intel_panel_get_max_backlight(dev) - val;
222 return val;
225 static u32 intel_panel_get_backlight(struct drm_device *dev)
227 struct drm_i915_private *dev_priv = dev->dev_private;
228 u32 val;
230 if (HAS_PCH_SPLIT(dev)) {
231 val = I915_READ(BLC_PWM_CPU_CTL) & BACKLIGHT_DUTY_CYCLE_MASK;
232 } else {
233 val = I915_READ(BLC_PWM_CTL) & BACKLIGHT_DUTY_CYCLE_MASK;
234 if (INTEL_INFO(dev)->gen < 4)
235 val >>= 1;
237 if (is_backlight_combination_mode(dev)) {
238 u8 lbpc;
240 pci_read_config_byte(dev->pdev, PCI_LBPC, &lbpc);
241 val *= lbpc;
245 val = intel_panel_compute_brightness(dev, val);
246 DRM_DEBUG_DRIVER("get backlight PWM = %d\n", val);
247 return val;
250 static void intel_pch_panel_set_backlight(struct drm_device *dev, u32 level)
252 struct drm_i915_private *dev_priv = dev->dev_private;
253 u32 val = I915_READ(BLC_PWM_CPU_CTL) & ~BACKLIGHT_DUTY_CYCLE_MASK;
254 I915_WRITE(BLC_PWM_CPU_CTL, val | level);
257 static void intel_panel_actually_set_backlight(struct drm_device *dev, u32 level)
259 struct drm_i915_private *dev_priv = dev->dev_private;
260 u32 tmp;
262 DRM_DEBUG_DRIVER("set backlight PWM = %d\n", level);
263 level = intel_panel_compute_brightness(dev, level);
265 if (HAS_PCH_SPLIT(dev))
266 return intel_pch_panel_set_backlight(dev, level);
268 if (is_backlight_combination_mode(dev)) {
269 u32 max = intel_panel_get_max_backlight(dev);
270 u8 lbpc;
272 lbpc = level * 0xfe / max + 1;
273 level /= lbpc;
274 pci_write_config_byte(dev->pdev, PCI_LBPC, lbpc);
277 tmp = I915_READ(BLC_PWM_CTL);
278 if (INTEL_INFO(dev)->gen < 4)
279 level <<= 1;
280 tmp &= ~BACKLIGHT_DUTY_CYCLE_MASK;
281 I915_WRITE(BLC_PWM_CTL, tmp | level);
284 void intel_panel_set_backlight(struct drm_device *dev, u32 level)
286 struct drm_i915_private *dev_priv = dev->dev_private;
288 dev_priv->backlight_level = level;
289 if (dev_priv->backlight_enabled)
290 intel_panel_actually_set_backlight(dev, level);
293 void intel_panel_disable_backlight(struct drm_device *dev)
295 struct drm_i915_private *dev_priv = dev->dev_private;
297 dev_priv->backlight_enabled = false;
298 intel_panel_actually_set_backlight(dev, 0);
300 if (INTEL_INFO(dev)->gen >= 4) {
301 uint32_t reg, tmp;
303 reg = HAS_PCH_SPLIT(dev) ? BLC_PWM_CPU_CTL2 : BLC_PWM_CTL2;
305 I915_WRITE(reg, I915_READ(reg) & ~BLM_PWM_ENABLE);
307 if (HAS_PCH_SPLIT(dev)) {
308 tmp = I915_READ(BLC_PWM_PCH_CTL1);
309 tmp &= ~BLM_PCH_PWM_ENABLE;
310 I915_WRITE(BLC_PWM_PCH_CTL1, tmp);
315 void intel_panel_enable_backlight(struct drm_device *dev,
316 enum pipe pipe)
318 struct drm_i915_private *dev_priv = dev->dev_private;
320 if (dev_priv->backlight_level == 0)
321 dev_priv->backlight_level = intel_panel_get_max_backlight(dev);
323 if (INTEL_INFO(dev)->gen >= 4) {
324 uint32_t reg, tmp;
326 reg = HAS_PCH_SPLIT(dev) ? BLC_PWM_CPU_CTL2 : BLC_PWM_CTL2;
329 tmp = I915_READ(reg);
331 /* Note that this can also get called through dpms changes. And
332 * we don't track the backlight dpms state, hence check whether
333 * we have to do anything first. */
334 if (tmp & BLM_PWM_ENABLE)
335 goto set_level;
337 if (dev_priv->num_pipe == 3)
338 tmp &= ~BLM_PIPE_SELECT_IVB;
339 else
340 tmp &= ~BLM_PIPE_SELECT;
342 tmp |= BLM_PIPE(pipe);
343 tmp &= ~BLM_PWM_ENABLE;
345 I915_WRITE(reg, tmp);
346 POSTING_READ(reg);
347 I915_WRITE(reg, tmp | BLM_PWM_ENABLE);
349 if (HAS_PCH_SPLIT(dev)) {
350 tmp = I915_READ(BLC_PWM_PCH_CTL1);
351 tmp |= BLM_PCH_PWM_ENABLE;
352 tmp &= ~BLM_PCH_OVERRIDE_ENABLE;
353 I915_WRITE(BLC_PWM_PCH_CTL1, tmp);
357 set_level:
358 /* Call below after setting BLC_PWM_CPU_CTL2 and BLC_PWM_PCH_CTL1.
359 * BLC_PWM_CPU_CTL may be cleared to zero automatically when these
360 * registers are set.
362 dev_priv->backlight_enabled = true;
363 intel_panel_actually_set_backlight(dev, dev_priv->backlight_level);
366 static void intel_panel_init_backlight(struct drm_device *dev)
368 struct drm_i915_private *dev_priv = dev->dev_private;
370 dev_priv->backlight_level = intel_panel_get_backlight(dev);
371 dev_priv->backlight_enabled = dev_priv->backlight_level != 0;
374 enum drm_connector_status
375 intel_panel_detect(struct drm_device *dev)
377 #if 0
378 struct drm_i915_private *dev_priv = dev->dev_private;
379 #endif
381 if (i915_panel_ignore_lid)
382 return i915_panel_ignore_lid > 0 ?
383 connector_status_connected :
384 connector_status_disconnected;
386 /* opregion lid state on HP 2540p is wrong at boot up,
387 * appears to be either the BIOS or Linux ACPI fault */
388 #if 0
389 /* Assume that the BIOS does not lie through the OpRegion... */
390 if (dev_priv->opregion.lid_state)
391 return ioread32(dev_priv->opregion.lid_state) & 0x1 ?
392 connector_status_connected :
393 connector_status_disconnected;
394 #endif
396 return connector_status_unknown;
399 #ifdef CONFIG_BACKLIGHT_CLASS_DEVICE
400 static int intel_panel_update_status(struct backlight_device *bd)
402 struct drm_device *dev = bl_get_data(bd);
403 intel_panel_set_backlight(dev, bd->props.brightness);
404 return 0;
407 static int intel_panel_get_brightness(struct backlight_device *bd)
409 struct drm_device *dev = bl_get_data(bd);
410 struct drm_i915_private *dev_priv = dev->dev_private;
411 return dev_priv->backlight_level;
414 static const struct backlight_ops intel_panel_bl_ops = {
415 .update_status = intel_panel_update_status,
416 .get_brightness = intel_panel_get_brightness,
419 int intel_panel_setup_backlight(struct drm_connector *connector)
421 struct drm_device *dev = connector->dev;
422 struct drm_i915_private *dev_priv = dev->dev_private;
423 struct backlight_properties props;
425 intel_panel_init_backlight(dev);
427 memset(&props, 0, sizeof(props));
428 props.type = BACKLIGHT_RAW;
429 props.max_brightness = _intel_panel_get_max_backlight(dev);
430 if (props.max_brightness == 0) {
431 DRM_DEBUG_DRIVER("Failed to get maximum backlight value\n");
432 return -ENODEV;
434 dev_priv->backlight =
435 backlight_device_register("intel_backlight",
436 &connector->kdev, dev,
437 &intel_panel_bl_ops, &props);
439 if (IS_ERR(dev_priv->backlight)) {
440 DRM_ERROR("Failed to register backlight: %ld\n",
441 PTR_ERR(dev_priv->backlight));
442 dev_priv->backlight = NULL;
443 return -ENODEV;
445 dev_priv->backlight->props.brightness = intel_panel_get_backlight(dev);
446 return 0;
449 void intel_panel_destroy_backlight(struct drm_device *dev)
451 struct drm_i915_private *dev_priv = dev->dev_private;
452 if (dev_priv->backlight)
453 backlight_device_unregister(dev_priv->backlight);
455 #else
456 int intel_panel_setup_backlight(struct drm_connector *connector)
458 intel_panel_init_backlight(connector->dev);
459 return 0;
462 void intel_panel_destroy_backlight(struct drm_device *dev)
464 return;
466 #endif
468 int intel_panel_init(struct intel_panel *panel,
469 struct drm_display_mode *fixed_mode)
471 panel->fixed_mode = fixed_mode;
473 return 0;
476 void intel_panel_fini(struct intel_panel *panel)
478 struct intel_connector *intel_connector =
479 container_of(panel, struct intel_connector, panel);
481 if (panel->fixed_mode)
482 drm_mode_destroy(intel_connector->base.dev, panel->fixed_mode);