Chris Wilson | 1d8e1c7 | 2010-08-07 11:01:28 +0100 | [diff] [blame] | 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 | |
Joe Perches | a70491c | 2012-03-18 13:00:11 -0700 | [diff] [blame] | 31 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 32 | |
Carsten Emde | 7bd9090 | 2012-03-15 15:56:25 +0100 | [diff] [blame] | 33 | #include <linux/moduleparam.h> |
Chris Wilson | 1d8e1c7 | 2010-08-07 11:01:28 +0100 | [diff] [blame] | 34 | #include "intel_drv.h" |
| 35 | |
Takashi Iwai | ba3820a | 2011-03-10 14:02:12 +0100 | [diff] [blame] | 36 | #define PCI_LBPC 0xf4 /* legacy/combination backlight modes */ |
| 37 | |
Chris Wilson | 1d8e1c7 | 2010-08-07 11:01:28 +0100 | [diff] [blame] | 38 | void |
| 39 | intel_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; |
Chris Wilson | 1d8e1c7 | 2010-08-07 11:01:28 +0100 | [diff] [blame] | 53 | } |
| 54 | |
| 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, |
Daniel Vetter | cb1793c | 2012-06-04 18:39:21 +0200 | [diff] [blame] | 59 | const struct drm_display_mode *mode, |
Chris Wilson | 1d8e1c7 | 2010-08-07 11:01:28 +0100 | [diff] [blame] | 60 | struct drm_display_mode *adjusted_mode) |
| 61 | { |
| 62 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 63 | int x, y, width, height; |
| 64 | |
| 65 | x = y = width = height = 0; |
| 66 | |
| 67 | /* Native modes don't need fitting */ |
| 68 | if (adjusted_mode->hdisplay == mode->hdisplay && |
| 69 | adjusted_mode->vdisplay == mode->vdisplay) |
| 70 | goto done; |
| 71 | |
| 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; |
| 79 | |
| 80 | case DRM_MODE_SCALE_ASPECT: |
| 81 | /* Scale but preserve the aspect ratio */ |
| 82 | { |
| 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; |
Adam Jackson | 302983e | 2011-07-13 16:32:32 -0400 | [diff] [blame] | 87 | if (width & 1) |
Akshay Joshi | 0206e35 | 2011-08-16 15:34:10 -0400 | [diff] [blame] | 88 | width++; |
Chris Wilson | 1d8e1c7 | 2010-08-07 11:01:28 +0100 | [diff] [blame] | 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; |
Adam Jackson | 302983e | 2011-07-13 16:32:32 -0400 | [diff] [blame] | 94 | if (height & 1) |
| 95 | height++; |
Chris Wilson | 1d8e1c7 | 2010-08-07 11:01:28 +0100 | [diff] [blame] | 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; |
| 103 | } |
| 104 | } |
| 105 | break; |
| 106 | |
| 107 | default: |
| 108 | case DRM_MODE_SCALE_FULLSCREEN: |
| 109 | x = y = 0; |
| 110 | width = adjusted_mode->hdisplay; |
| 111 | height = adjusted_mode->vdisplay; |
| 112 | break; |
| 113 | } |
| 114 | |
| 115 | done: |
| 116 | dev_priv->pch_pf_pos = (x << 16) | y; |
| 117 | dev_priv->pch_pf_size = (width << 16) | height; |
| 118 | } |
Chris Wilson | a957355 | 2010-08-22 13:18:16 +0100 | [diff] [blame] | 119 | |
Takashi Iwai | ba3820a | 2011-03-10 14:02:12 +0100 | [diff] [blame] | 120 | static int is_backlight_combination_mode(struct drm_device *dev) |
| 121 | { |
| 122 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 123 | |
| 124 | if (INTEL_INFO(dev)->gen >= 4) |
| 125 | return I915_READ(BLC_PWM_CTL2) & BLM_COMBINATION_MODE; |
| 126 | |
| 127 | if (IS_GEN2(dev)) |
| 128 | return I915_READ(BLC_PWM_CTL) & BLM_LEGACY_MODE; |
| 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | |
Jani Nikula | bfd7590 | 2012-12-04 16:36:28 +0200 | [diff] [blame] | 133 | static u32 i915_read_blc_pwm_ctl(struct drm_device *dev) |
Chris Wilson | 0b0b053 | 2010-11-23 09:45:50 +0000 | [diff] [blame] | 134 | { |
Jani Nikula | bfd7590 | 2012-12-04 16:36:28 +0200 | [diff] [blame] | 135 | struct drm_i915_private *dev_priv = dev->dev_private; |
Chris Wilson | 0b0b053 | 2010-11-23 09:45:50 +0000 | [diff] [blame] | 136 | u32 val; |
| 137 | |
| 138 | /* Restore the CTL value if it lost, e.g. GPU reset */ |
| 139 | |
| 140 | if (HAS_PCH_SPLIT(dev_priv->dev)) { |
| 141 | val = I915_READ(BLC_PWM_PCH_CTL2); |
Daniel Vetter | f4c956a | 2012-11-02 19:55:02 +0100 | [diff] [blame] | 142 | if (dev_priv->regfile.saveBLC_PWM_CTL2 == 0) { |
| 143 | dev_priv->regfile.saveBLC_PWM_CTL2 = val; |
Chris Wilson | 0b0b053 | 2010-11-23 09:45:50 +0000 | [diff] [blame] | 144 | } else if (val == 0) { |
Daniel Vetter | f4c956a | 2012-11-02 19:55:02 +0100 | [diff] [blame] | 145 | val = dev_priv->regfile.saveBLC_PWM_CTL2; |
Jani Nikula | bfd7590 | 2012-12-04 16:36:28 +0200 | [diff] [blame] | 146 | I915_WRITE(BLC_PWM_PCH_CTL2, val); |
Chris Wilson | 0b0b053 | 2010-11-23 09:45:50 +0000 | [diff] [blame] | 147 | } |
| 148 | } else { |
| 149 | val = I915_READ(BLC_PWM_CTL); |
Daniel Vetter | f4c956a | 2012-11-02 19:55:02 +0100 | [diff] [blame] | 150 | if (dev_priv->regfile.saveBLC_PWM_CTL == 0) { |
| 151 | dev_priv->regfile.saveBLC_PWM_CTL = val; |
Jani Nikula | bfd7590 | 2012-12-04 16:36:28 +0200 | [diff] [blame] | 152 | if (INTEL_INFO(dev)->gen >= 4) |
| 153 | dev_priv->regfile.saveBLC_PWM_CTL2 = |
| 154 | I915_READ(BLC_PWM_CTL2); |
Chris Wilson | 0b0b053 | 2010-11-23 09:45:50 +0000 | [diff] [blame] | 155 | } else if (val == 0) { |
Daniel Vetter | f4c956a | 2012-11-02 19:55:02 +0100 | [diff] [blame] | 156 | val = dev_priv->regfile.saveBLC_PWM_CTL; |
Jani Nikula | bfd7590 | 2012-12-04 16:36:28 +0200 | [diff] [blame] | 157 | I915_WRITE(BLC_PWM_CTL, val); |
| 158 | if (INTEL_INFO(dev)->gen >= 4) |
| 159 | I915_WRITE(BLC_PWM_CTL2, |
| 160 | dev_priv->regfile.saveBLC_PWM_CTL2); |
Chris Wilson | 0b0b053 | 2010-11-23 09:45:50 +0000 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | |
| 164 | return val; |
| 165 | } |
| 166 | |
Jani Nikula | 28dcc2d | 2012-09-03 16:25:12 +0300 | [diff] [blame] | 167 | static u32 _intel_panel_get_max_backlight(struct drm_device *dev) |
Chris Wilson | a957355 | 2010-08-22 13:18:16 +0100 | [diff] [blame] | 168 | { |
Chris Wilson | a957355 | 2010-08-22 13:18:16 +0100 | [diff] [blame] | 169 | u32 max; |
| 170 | |
Jani Nikula | bfd7590 | 2012-12-04 16:36:28 +0200 | [diff] [blame] | 171 | max = i915_read_blc_pwm_ctl(dev); |
Chris Wilson | 0b0b053 | 2010-11-23 09:45:50 +0000 | [diff] [blame] | 172 | |
Chris Wilson | a957355 | 2010-08-22 13:18:16 +0100 | [diff] [blame] | 173 | if (HAS_PCH_SPLIT(dev)) { |
Chris Wilson | 0b0b053 | 2010-11-23 09:45:50 +0000 | [diff] [blame] | 174 | max >>= 16; |
Chris Wilson | a957355 | 2010-08-22 13:18:16 +0100 | [diff] [blame] | 175 | } else { |
Keith Packard | ca88479 | 2011-11-18 11:09:24 -0800 | [diff] [blame] | 176 | if (INTEL_INFO(dev)->gen < 4) |
Chris Wilson | a957355 | 2010-08-22 13:18:16 +0100 | [diff] [blame] | 177 | max >>= 17; |
Keith Packard | ca88479 | 2011-11-18 11:09:24 -0800 | [diff] [blame] | 178 | else |
Chris Wilson | a957355 | 2010-08-22 13:18:16 +0100 | [diff] [blame] | 179 | max >>= 16; |
Takashi Iwai | ba3820a | 2011-03-10 14:02:12 +0100 | [diff] [blame] | 180 | |
| 181 | if (is_backlight_combination_mode(dev)) |
| 182 | max *= 0xff; |
Chris Wilson | a957355 | 2010-08-22 13:18:16 +0100 | [diff] [blame] | 183 | } |
| 184 | |
Jani Nikula | 28dcc2d | 2012-09-03 16:25:12 +0300 | [diff] [blame] | 185 | return max; |
| 186 | } |
| 187 | |
| 188 | u32 intel_panel_get_max_backlight(struct drm_device *dev) |
| 189 | { |
| 190 | u32 max; |
| 191 | |
| 192 | max = _intel_panel_get_max_backlight(dev); |
| 193 | if (max == 0) { |
| 194 | /* XXX add code here to query mode clock or hardware clock |
| 195 | * and program max PWM appropriately. |
| 196 | */ |
| 197 | pr_warn_once("fixme: max PWM is zero\n"); |
| 198 | return 1; |
| 199 | } |
| 200 | |
Chris Wilson | a957355 | 2010-08-22 13:18:16 +0100 | [diff] [blame] | 201 | DRM_DEBUG_DRIVER("max backlight PWM = %d\n", max); |
| 202 | return max; |
| 203 | } |
| 204 | |
Carsten Emde | 4dca20e | 2012-03-15 15:56:26 +0100 | [diff] [blame] | 205 | static int i915_panel_invert_brightness; |
| 206 | MODULE_PARM_DESC(invert_brightness, "Invert backlight brightness " |
| 207 | "(-1 force normal, 0 machine defaults, 1 force inversion), please " |
Carsten Emde | 7bd9090 | 2012-03-15 15:56:25 +0100 | [diff] [blame] | 208 | "report PCI device ID, subsystem vendor and subsystem device ID " |
| 209 | "to dri-devel@lists.freedesktop.org, if your machine needs it. " |
| 210 | "It will then be included in an upcoming module version."); |
Carsten Emde | 4dca20e | 2012-03-15 15:56:26 +0100 | [diff] [blame] | 211 | module_param_named(invert_brightness, i915_panel_invert_brightness, int, 0600); |
Carsten Emde | 7bd9090 | 2012-03-15 15:56:25 +0100 | [diff] [blame] | 212 | static u32 intel_panel_compute_brightness(struct drm_device *dev, u32 val) |
| 213 | { |
Carsten Emde | 4dca20e | 2012-03-15 15:56:26 +0100 | [diff] [blame] | 214 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 215 | |
| 216 | if (i915_panel_invert_brightness < 0) |
| 217 | return val; |
| 218 | |
| 219 | if (i915_panel_invert_brightness > 0 || |
| 220 | dev_priv->quirks & QUIRK_INVERT_BRIGHTNESS) |
Carsten Emde | 7bd9090 | 2012-03-15 15:56:25 +0100 | [diff] [blame] | 221 | return intel_panel_get_max_backlight(dev) - val; |
| 222 | |
| 223 | return val; |
| 224 | } |
| 225 | |
Stéphane Marchesin | faea35d | 2012-07-30 13:51:38 -0700 | [diff] [blame] | 226 | static u32 intel_panel_get_backlight(struct drm_device *dev) |
Chris Wilson | a957355 | 2010-08-22 13:18:16 +0100 | [diff] [blame] | 227 | { |
| 228 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 229 | u32 val; |
| 230 | |
| 231 | if (HAS_PCH_SPLIT(dev)) { |
| 232 | val = I915_READ(BLC_PWM_CPU_CTL) & BACKLIGHT_DUTY_CYCLE_MASK; |
| 233 | } else { |
| 234 | val = I915_READ(BLC_PWM_CTL) & BACKLIGHT_DUTY_CYCLE_MASK; |
Keith Packard | ca88479 | 2011-11-18 11:09:24 -0800 | [diff] [blame] | 235 | if (INTEL_INFO(dev)->gen < 4) |
Chris Wilson | a957355 | 2010-08-22 13:18:16 +0100 | [diff] [blame] | 236 | val >>= 1; |
Takashi Iwai | ba3820a | 2011-03-10 14:02:12 +0100 | [diff] [blame] | 237 | |
Akshay Joshi | 0206e35 | 2011-08-16 15:34:10 -0400 | [diff] [blame] | 238 | if (is_backlight_combination_mode(dev)) { |
Takashi Iwai | ba3820a | 2011-03-10 14:02:12 +0100 | [diff] [blame] | 239 | u8 lbpc; |
| 240 | |
Takashi Iwai | ba3820a | 2011-03-10 14:02:12 +0100 | [diff] [blame] | 241 | pci_read_config_byte(dev->pdev, PCI_LBPC, &lbpc); |
| 242 | val *= lbpc; |
| 243 | } |
Chris Wilson | a957355 | 2010-08-22 13:18:16 +0100 | [diff] [blame] | 244 | } |
| 245 | |
Carsten Emde | 7bd9090 | 2012-03-15 15:56:25 +0100 | [diff] [blame] | 246 | val = intel_panel_compute_brightness(dev, val); |
Chris Wilson | a957355 | 2010-08-22 13:18:16 +0100 | [diff] [blame] | 247 | DRM_DEBUG_DRIVER("get backlight PWM = %d\n", val); |
| 248 | return val; |
| 249 | } |
| 250 | |
| 251 | static void intel_pch_panel_set_backlight(struct drm_device *dev, u32 level) |
| 252 | { |
| 253 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 254 | u32 val = I915_READ(BLC_PWM_CPU_CTL) & ~BACKLIGHT_DUTY_CYCLE_MASK; |
| 255 | I915_WRITE(BLC_PWM_CPU_CTL, val | level); |
| 256 | } |
| 257 | |
Takashi Iwai | f52c619 | 2011-10-14 11:45:40 +0200 | [diff] [blame] | 258 | static void intel_panel_actually_set_backlight(struct drm_device *dev, u32 level) |
Chris Wilson | a957355 | 2010-08-22 13:18:16 +0100 | [diff] [blame] | 259 | { |
| 260 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 261 | u32 tmp; |
| 262 | |
| 263 | DRM_DEBUG_DRIVER("set backlight PWM = %d\n", level); |
Carsten Emde | 7bd9090 | 2012-03-15 15:56:25 +0100 | [diff] [blame] | 264 | level = intel_panel_compute_brightness(dev, level); |
Chris Wilson | a957355 | 2010-08-22 13:18:16 +0100 | [diff] [blame] | 265 | |
| 266 | if (HAS_PCH_SPLIT(dev)) |
| 267 | return intel_pch_panel_set_backlight(dev, level); |
Takashi Iwai | ba3820a | 2011-03-10 14:02:12 +0100 | [diff] [blame] | 268 | |
Akshay Joshi | 0206e35 | 2011-08-16 15:34:10 -0400 | [diff] [blame] | 269 | if (is_backlight_combination_mode(dev)) { |
Takashi Iwai | ba3820a | 2011-03-10 14:02:12 +0100 | [diff] [blame] | 270 | u32 max = intel_panel_get_max_backlight(dev); |
| 271 | u8 lbpc; |
| 272 | |
| 273 | lbpc = level * 0xfe / max + 1; |
| 274 | level /= lbpc; |
| 275 | pci_write_config_byte(dev->pdev, PCI_LBPC, lbpc); |
| 276 | } |
| 277 | |
Chris Wilson | a957355 | 2010-08-22 13:18:16 +0100 | [diff] [blame] | 278 | tmp = I915_READ(BLC_PWM_CTL); |
Daniel Vetter | a726915 | 2012-11-20 14:50:08 +0100 | [diff] [blame] | 279 | if (INTEL_INFO(dev)->gen < 4) |
Chris Wilson | a957355 | 2010-08-22 13:18:16 +0100 | [diff] [blame] | 280 | level <<= 1; |
Keith Packard | ca88479 | 2011-11-18 11:09:24 -0800 | [diff] [blame] | 281 | tmp &= ~BACKLIGHT_DUTY_CYCLE_MASK; |
Chris Wilson | a957355 | 2010-08-22 13:18:16 +0100 | [diff] [blame] | 282 | I915_WRITE(BLC_PWM_CTL, tmp | level); |
| 283 | } |
Chris Wilson | 47356eb | 2011-01-11 17:06:04 +0000 | [diff] [blame] | 284 | |
Takashi Iwai | f52c619 | 2011-10-14 11:45:40 +0200 | [diff] [blame] | 285 | void intel_panel_set_backlight(struct drm_device *dev, u32 level) |
| 286 | { |
| 287 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 288 | |
| 289 | dev_priv->backlight_level = level; |
| 290 | if (dev_priv->backlight_enabled) |
| 291 | intel_panel_actually_set_backlight(dev, level); |
| 292 | } |
| 293 | |
Chris Wilson | 47356eb | 2011-01-11 17:06:04 +0000 | [diff] [blame] | 294 | void intel_panel_disable_backlight(struct drm_device *dev) |
| 295 | { |
| 296 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 297 | |
Takashi Iwai | f52c619 | 2011-10-14 11:45:40 +0200 | [diff] [blame] | 298 | dev_priv->backlight_enabled = false; |
| 299 | intel_panel_actually_set_backlight(dev, 0); |
Daniel Vetter | 24ded20 | 2012-06-05 12:14:54 +0200 | [diff] [blame] | 300 | |
| 301 | if (INTEL_INFO(dev)->gen >= 4) { |
Paulo Zanoni | a4f32fc | 2012-07-14 11:57:12 -0300 | [diff] [blame] | 302 | uint32_t reg, tmp; |
Daniel Vetter | 24ded20 | 2012-06-05 12:14:54 +0200 | [diff] [blame] | 303 | |
| 304 | reg = HAS_PCH_SPLIT(dev) ? BLC_PWM_CPU_CTL2 : BLC_PWM_CTL2; |
| 305 | |
| 306 | I915_WRITE(reg, I915_READ(reg) & ~BLM_PWM_ENABLE); |
Paulo Zanoni | a4f32fc | 2012-07-14 11:57:12 -0300 | [diff] [blame] | 307 | |
| 308 | if (HAS_PCH_SPLIT(dev)) { |
| 309 | tmp = I915_READ(BLC_PWM_PCH_CTL1); |
| 310 | tmp &= ~BLM_PCH_PWM_ENABLE; |
| 311 | I915_WRITE(BLC_PWM_PCH_CTL1, tmp); |
| 312 | } |
Daniel Vetter | 24ded20 | 2012-06-05 12:14:54 +0200 | [diff] [blame] | 313 | } |
Chris Wilson | 47356eb | 2011-01-11 17:06:04 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Daniel Vetter | 24ded20 | 2012-06-05 12:14:54 +0200 | [diff] [blame] | 316 | void intel_panel_enable_backlight(struct drm_device *dev, |
| 317 | enum pipe pipe) |
Chris Wilson | 47356eb | 2011-01-11 17:06:04 +0000 | [diff] [blame] | 318 | { |
| 319 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 320 | |
| 321 | if (dev_priv->backlight_level == 0) |
| 322 | dev_priv->backlight_level = intel_panel_get_max_backlight(dev); |
| 323 | |
Daniel Vetter | 24ded20 | 2012-06-05 12:14:54 +0200 | [diff] [blame] | 324 | if (INTEL_INFO(dev)->gen >= 4) { |
| 325 | uint32_t reg, tmp; |
| 326 | |
| 327 | reg = HAS_PCH_SPLIT(dev) ? BLC_PWM_CPU_CTL2 : BLC_PWM_CTL2; |
| 328 | |
| 329 | |
| 330 | tmp = I915_READ(reg); |
| 331 | |
| 332 | /* Note that this can also get called through dpms changes. And |
| 333 | * we don't track the backlight dpms state, hence check whether |
| 334 | * we have to do anything first. */ |
| 335 | if (tmp & BLM_PWM_ENABLE) |
Takashi Iwai | 770c123 | 2012-08-11 08:56:42 +0200 | [diff] [blame] | 336 | goto set_level; |
Daniel Vetter | 24ded20 | 2012-06-05 12:14:54 +0200 | [diff] [blame] | 337 | |
| 338 | if (dev_priv->num_pipe == 3) |
| 339 | tmp &= ~BLM_PIPE_SELECT_IVB; |
| 340 | else |
| 341 | tmp &= ~BLM_PIPE_SELECT; |
| 342 | |
| 343 | tmp |= BLM_PIPE(pipe); |
| 344 | tmp &= ~BLM_PWM_ENABLE; |
| 345 | |
| 346 | I915_WRITE(reg, tmp); |
| 347 | POSTING_READ(reg); |
| 348 | I915_WRITE(reg, tmp | BLM_PWM_ENABLE); |
Paulo Zanoni | a4f32fc | 2012-07-14 11:57:12 -0300 | [diff] [blame] | 349 | |
| 350 | if (HAS_PCH_SPLIT(dev)) { |
| 351 | tmp = I915_READ(BLC_PWM_PCH_CTL1); |
| 352 | tmp |= BLM_PCH_PWM_ENABLE; |
| 353 | tmp &= ~BLM_PCH_OVERRIDE_ENABLE; |
| 354 | I915_WRITE(BLC_PWM_PCH_CTL1, tmp); |
| 355 | } |
Daniel Vetter | 24ded20 | 2012-06-05 12:14:54 +0200 | [diff] [blame] | 356 | } |
Takashi Iwai | 770c123 | 2012-08-11 08:56:42 +0200 | [diff] [blame] | 357 | |
| 358 | set_level: |
| 359 | /* Call below after setting BLC_PWM_CPU_CTL2 and BLC_PWM_PCH_CTL1. |
| 360 | * BLC_PWM_CPU_CTL may be cleared to zero automatically when these |
| 361 | * registers are set. |
| 362 | */ |
| 363 | dev_priv->backlight_enabled = true; |
| 364 | intel_panel_actually_set_backlight(dev, dev_priv->backlight_level); |
Chris Wilson | 47356eb | 2011-01-11 17:06:04 +0000 | [diff] [blame] | 365 | } |
| 366 | |
Matthew Garrett | aaa6fd2 | 2011-08-12 12:11:33 +0200 | [diff] [blame] | 367 | static void intel_panel_init_backlight(struct drm_device *dev) |
Chris Wilson | 47356eb | 2011-01-11 17:06:04 +0000 | [diff] [blame] | 368 | { |
| 369 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 370 | |
Indan Zupancic | c8303e7 | 2011-01-12 11:59:19 +0000 | [diff] [blame] | 371 | dev_priv->backlight_level = intel_panel_get_backlight(dev); |
Chris Wilson | 47356eb | 2011-01-11 17:06:04 +0000 | [diff] [blame] | 372 | dev_priv->backlight_enabled = dev_priv->backlight_level != 0; |
| 373 | } |
Chris Wilson | fe16d94 | 2011-02-12 10:29:38 +0000 | [diff] [blame] | 374 | |
| 375 | enum drm_connector_status |
| 376 | intel_panel_detect(struct drm_device *dev) |
| 377 | { |
| 378 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 379 | |
| 380 | /* Assume that the BIOS does not lie through the OpRegion... */ |
Daniel Vetter | a726915 | 2012-11-20 14:50:08 +0100 | [diff] [blame] | 381 | if (!i915_panel_ignore_lid && dev_priv->opregion.lid_state) { |
Chris Wilson | fe16d94 | 2011-02-12 10:29:38 +0000 | [diff] [blame] | 382 | return ioread32(dev_priv->opregion.lid_state) & 0x1 ? |
| 383 | connector_status_connected : |
| 384 | connector_status_disconnected; |
Daniel Vetter | a726915 | 2012-11-20 14:50:08 +0100 | [diff] [blame] | 385 | } |
Chris Wilson | fe16d94 | 2011-02-12 10:29:38 +0000 | [diff] [blame] | 386 | |
Daniel Vetter | a726915 | 2012-11-20 14:50:08 +0100 | [diff] [blame] | 387 | switch (i915_panel_ignore_lid) { |
| 388 | case -2: |
| 389 | return connector_status_connected; |
| 390 | case -1: |
| 391 | return connector_status_disconnected; |
| 392 | default: |
| 393 | return connector_status_unknown; |
| 394 | } |
Chris Wilson | fe16d94 | 2011-02-12 10:29:38 +0000 | [diff] [blame] | 395 | } |
Matthew Garrett | aaa6fd2 | 2011-08-12 12:11:33 +0200 | [diff] [blame] | 396 | |
| 397 | #ifdef CONFIG_BACKLIGHT_CLASS_DEVICE |
| 398 | static int intel_panel_update_status(struct backlight_device *bd) |
| 399 | { |
| 400 | struct drm_device *dev = bl_get_data(bd); |
| 401 | intel_panel_set_backlight(dev, bd->props.brightness); |
| 402 | return 0; |
| 403 | } |
| 404 | |
| 405 | static int intel_panel_get_brightness(struct backlight_device *bd) |
| 406 | { |
| 407 | struct drm_device *dev = bl_get_data(bd); |
Takashi Iwai | 04b3867 | 2011-11-16 10:58:03 +0100 | [diff] [blame] | 408 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 409 | return dev_priv->backlight_level; |
Matthew Garrett | aaa6fd2 | 2011-08-12 12:11:33 +0200 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | static const struct backlight_ops intel_panel_bl_ops = { |
| 413 | .update_status = intel_panel_update_status, |
| 414 | .get_brightness = intel_panel_get_brightness, |
| 415 | }; |
| 416 | |
Jani Nikula | 0657b6b | 2012-10-19 14:51:46 +0300 | [diff] [blame] | 417 | int intel_panel_setup_backlight(struct drm_connector *connector) |
Matthew Garrett | aaa6fd2 | 2011-08-12 12:11:33 +0200 | [diff] [blame] | 418 | { |
Jani Nikula | 0657b6b | 2012-10-19 14:51:46 +0300 | [diff] [blame] | 419 | struct drm_device *dev = connector->dev; |
Matthew Garrett | aaa6fd2 | 2011-08-12 12:11:33 +0200 | [diff] [blame] | 420 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 421 | struct backlight_properties props; |
Matthew Garrett | aaa6fd2 | 2011-08-12 12:11:33 +0200 | [diff] [blame] | 422 | |
| 423 | intel_panel_init_backlight(dev); |
| 424 | |
Corentin Chary | af437cf | 2012-05-22 10:29:46 +0100 | [diff] [blame] | 425 | memset(&props, 0, sizeof(props)); |
Matthew Garrett | aaa6fd2 | 2011-08-12 12:11:33 +0200 | [diff] [blame] | 426 | props.type = BACKLIGHT_RAW; |
Jani Nikula | 28dcc2d | 2012-09-03 16:25:12 +0300 | [diff] [blame] | 427 | props.max_brightness = _intel_panel_get_max_backlight(dev); |
| 428 | if (props.max_brightness == 0) { |
Jani Nikula | e86b618 | 2012-10-25 10:57:38 +0300 | [diff] [blame] | 429 | DRM_DEBUG_DRIVER("Failed to get maximum backlight value\n"); |
Jani Nikula | 28dcc2d | 2012-09-03 16:25:12 +0300 | [diff] [blame] | 430 | return -ENODEV; |
| 431 | } |
Matthew Garrett | aaa6fd2 | 2011-08-12 12:11:33 +0200 | [diff] [blame] | 432 | dev_priv->backlight = |
| 433 | backlight_device_register("intel_backlight", |
| 434 | &connector->kdev, dev, |
| 435 | &intel_panel_bl_ops, &props); |
| 436 | |
| 437 | if (IS_ERR(dev_priv->backlight)) { |
| 438 | DRM_ERROR("Failed to register backlight: %ld\n", |
| 439 | PTR_ERR(dev_priv->backlight)); |
| 440 | dev_priv->backlight = NULL; |
| 441 | return -ENODEV; |
| 442 | } |
| 443 | dev_priv->backlight->props.brightness = intel_panel_get_backlight(dev); |
| 444 | return 0; |
| 445 | } |
| 446 | |
| 447 | void intel_panel_destroy_backlight(struct drm_device *dev) |
| 448 | { |
| 449 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 450 | if (dev_priv->backlight) |
| 451 | backlight_device_unregister(dev_priv->backlight); |
| 452 | } |
| 453 | #else |
Jani Nikula | 0657b6b | 2012-10-19 14:51:46 +0300 | [diff] [blame] | 454 | int intel_panel_setup_backlight(struct drm_connector *connector) |
Matthew Garrett | aaa6fd2 | 2011-08-12 12:11:33 +0200 | [diff] [blame] | 455 | { |
Jani Nikula | 0657b6b | 2012-10-19 14:51:46 +0300 | [diff] [blame] | 456 | intel_panel_init_backlight(connector->dev); |
Matthew Garrett | aaa6fd2 | 2011-08-12 12:11:33 +0200 | [diff] [blame] | 457 | return 0; |
| 458 | } |
| 459 | |
| 460 | void intel_panel_destroy_backlight(struct drm_device *dev) |
| 461 | { |
| 462 | return; |
| 463 | } |
| 464 | #endif |
Jani Nikula | 1d50870 | 2012-10-19 14:51:49 +0300 | [diff] [blame] | 465 | |
Jani Nikula | dd06f90 | 2012-10-19 14:51:50 +0300 | [diff] [blame] | 466 | int intel_panel_init(struct intel_panel *panel, |
| 467 | struct drm_display_mode *fixed_mode) |
Jani Nikula | 1d50870 | 2012-10-19 14:51:49 +0300 | [diff] [blame] | 468 | { |
Jani Nikula | dd06f90 | 2012-10-19 14:51:50 +0300 | [diff] [blame] | 469 | panel->fixed_mode = fixed_mode; |
| 470 | |
Jani Nikula | 1d50870 | 2012-10-19 14:51:49 +0300 | [diff] [blame] | 471 | return 0; |
| 472 | } |
| 473 | |
| 474 | void intel_panel_fini(struct intel_panel *panel) |
| 475 | { |
Jani Nikula | dd06f90 | 2012-10-19 14:51:50 +0300 | [diff] [blame] | 476 | struct intel_connector *intel_connector = |
| 477 | container_of(panel, struct intel_connector, panel); |
| 478 | |
| 479 | if (panel->fixed_mode) |
| 480 | drm_mode_destroy(intel_connector->base.dev, panel->fixed_mode); |
Jani Nikula | 1d50870 | 2012-10-19 14:51:49 +0300 | [diff] [blame] | 481 | } |