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