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 | |
| 31 | #include "intel_drv.h" |
| 32 | |
Takashi Iwai | ba3820a | 2011-03-10 14:02:12 +0100 | [diff] [blame] | 33 | #define PCI_LBPC 0xf4 /* legacy/combination backlight modes */ |
| 34 | |
Chris Wilson | 1d8e1c7 | 2010-08-07 11:01:28 +0100 | [diff] [blame] | 35 | void |
| 36 | intel_fixed_panel_mode(struct drm_display_mode *fixed_mode, |
| 37 | struct drm_display_mode *adjusted_mode) |
| 38 | { |
| 39 | adjusted_mode->hdisplay = fixed_mode->hdisplay; |
| 40 | adjusted_mode->hsync_start = fixed_mode->hsync_start; |
| 41 | adjusted_mode->hsync_end = fixed_mode->hsync_end; |
| 42 | adjusted_mode->htotal = fixed_mode->htotal; |
| 43 | |
| 44 | adjusted_mode->vdisplay = fixed_mode->vdisplay; |
| 45 | adjusted_mode->vsync_start = fixed_mode->vsync_start; |
| 46 | adjusted_mode->vsync_end = fixed_mode->vsync_end; |
| 47 | adjusted_mode->vtotal = fixed_mode->vtotal; |
| 48 | |
| 49 | adjusted_mode->clock = fixed_mode->clock; |
| 50 | |
| 51 | drm_mode_set_crtcinfo(adjusted_mode, CRTC_INTERLACE_HALVE_V); |
| 52 | } |
| 53 | |
| 54 | /* adjusted_mode has been preset to be the panel's fixed mode */ |
| 55 | void |
| 56 | intel_pch_panel_fitting(struct drm_device *dev, |
| 57 | int fitting_mode, |
| 58 | struct drm_display_mode *mode, |
| 59 | struct drm_display_mode *adjusted_mode) |
| 60 | { |
| 61 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 62 | int x, y, width, height; |
| 63 | |
| 64 | x = y = width = height = 0; |
| 65 | |
| 66 | /* Native modes don't need fitting */ |
| 67 | if (adjusted_mode->hdisplay == mode->hdisplay && |
| 68 | adjusted_mode->vdisplay == mode->vdisplay) |
| 69 | goto done; |
| 70 | |
| 71 | switch (fitting_mode) { |
| 72 | case DRM_MODE_SCALE_CENTER: |
| 73 | width = mode->hdisplay; |
| 74 | height = mode->vdisplay; |
| 75 | x = (adjusted_mode->hdisplay - width + 1)/2; |
| 76 | y = (adjusted_mode->vdisplay - height + 1)/2; |
| 77 | break; |
| 78 | |
| 79 | case DRM_MODE_SCALE_ASPECT: |
| 80 | /* Scale but preserve the aspect ratio */ |
| 81 | { |
| 82 | u32 scaled_width = adjusted_mode->hdisplay * mode->vdisplay; |
| 83 | u32 scaled_height = mode->hdisplay * adjusted_mode->vdisplay; |
| 84 | if (scaled_width > scaled_height) { /* pillar */ |
| 85 | width = scaled_height / mode->vdisplay; |
Adam Jackson | 302983e | 2011-07-13 16:32:32 -0400 | [diff] [blame] | 86 | if (width & 1) |
Akshay Joshi | 0206e35 | 2011-08-16 15:34:10 -0400 | [diff] [blame] | 87 | width++; |
Chris Wilson | 1d8e1c7 | 2010-08-07 11:01:28 +0100 | [diff] [blame] | 88 | x = (adjusted_mode->hdisplay - width + 1) / 2; |
| 89 | y = 0; |
| 90 | height = adjusted_mode->vdisplay; |
| 91 | } else if (scaled_width < scaled_height) { /* letter */ |
| 92 | height = scaled_width / mode->hdisplay; |
Adam Jackson | 302983e | 2011-07-13 16:32:32 -0400 | [diff] [blame] | 93 | if (height & 1) |
| 94 | height++; |
Chris Wilson | 1d8e1c7 | 2010-08-07 11:01:28 +0100 | [diff] [blame] | 95 | y = (adjusted_mode->vdisplay - height + 1) / 2; |
| 96 | x = 0; |
| 97 | width = adjusted_mode->hdisplay; |
| 98 | } else { |
| 99 | x = y = 0; |
| 100 | width = adjusted_mode->hdisplay; |
| 101 | height = adjusted_mode->vdisplay; |
| 102 | } |
| 103 | } |
| 104 | break; |
| 105 | |
| 106 | default: |
| 107 | case DRM_MODE_SCALE_FULLSCREEN: |
| 108 | x = y = 0; |
| 109 | width = adjusted_mode->hdisplay; |
| 110 | height = adjusted_mode->vdisplay; |
| 111 | break; |
| 112 | } |
| 113 | |
| 114 | done: |
| 115 | dev_priv->pch_pf_pos = (x << 16) | y; |
| 116 | dev_priv->pch_pf_size = (width << 16) | height; |
| 117 | } |
Chris Wilson | a957355 | 2010-08-22 13:18:16 +0100 | [diff] [blame] | 118 | |
Takashi Iwai | ba3820a | 2011-03-10 14:02:12 +0100 | [diff] [blame] | 119 | static int is_backlight_combination_mode(struct drm_device *dev) |
| 120 | { |
| 121 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 122 | |
| 123 | if (INTEL_INFO(dev)->gen >= 4) |
| 124 | return I915_READ(BLC_PWM_CTL2) & BLM_COMBINATION_MODE; |
| 125 | |
| 126 | if (IS_GEN2(dev)) |
| 127 | return I915_READ(BLC_PWM_CTL) & BLM_LEGACY_MODE; |
| 128 | |
| 129 | return 0; |
| 130 | } |
| 131 | |
Chris Wilson | 0b0b053 | 2010-11-23 09:45:50 +0000 | [diff] [blame] | 132 | static u32 i915_read_blc_pwm_ctl(struct drm_i915_private *dev_priv) |
| 133 | { |
| 134 | u32 val; |
| 135 | |
| 136 | /* Restore the CTL value if it lost, e.g. GPU reset */ |
| 137 | |
| 138 | if (HAS_PCH_SPLIT(dev_priv->dev)) { |
| 139 | val = I915_READ(BLC_PWM_PCH_CTL2); |
| 140 | if (dev_priv->saveBLC_PWM_CTL2 == 0) { |
| 141 | dev_priv->saveBLC_PWM_CTL2 = val; |
| 142 | } else if (val == 0) { |
| 143 | I915_WRITE(BLC_PWM_PCH_CTL2, |
| 144 | dev_priv->saveBLC_PWM_CTL); |
| 145 | val = dev_priv->saveBLC_PWM_CTL; |
| 146 | } |
| 147 | } else { |
| 148 | val = I915_READ(BLC_PWM_CTL); |
| 149 | if (dev_priv->saveBLC_PWM_CTL == 0) { |
| 150 | dev_priv->saveBLC_PWM_CTL = val; |
| 151 | dev_priv->saveBLC_PWM_CTL2 = I915_READ(BLC_PWM_CTL2); |
| 152 | } else if (val == 0) { |
| 153 | I915_WRITE(BLC_PWM_CTL, |
| 154 | dev_priv->saveBLC_PWM_CTL); |
| 155 | I915_WRITE(BLC_PWM_CTL2, |
| 156 | dev_priv->saveBLC_PWM_CTL2); |
| 157 | val = dev_priv->saveBLC_PWM_CTL; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | return val; |
| 162 | } |
| 163 | |
Chris Wilson | a957355 | 2010-08-22 13:18:16 +0100 | [diff] [blame] | 164 | u32 intel_panel_get_max_backlight(struct drm_device *dev) |
| 165 | { |
| 166 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 167 | u32 max; |
| 168 | |
Chris Wilson | 0b0b053 | 2010-11-23 09:45:50 +0000 | [diff] [blame] | 169 | max = i915_read_blc_pwm_ctl(dev_priv); |
| 170 | if (max == 0) { |
| 171 | /* XXX add code here to query mode clock or hardware clock |
| 172 | * and program max PWM appropriately. |
| 173 | */ |
| 174 | printk_once(KERN_WARNING "fixme: max PWM is zero.\n"); |
| 175 | return 1; |
| 176 | } |
| 177 | |
Chris Wilson | a957355 | 2010-08-22 13:18:16 +0100 | [diff] [blame] | 178 | if (HAS_PCH_SPLIT(dev)) { |
Chris Wilson | 0b0b053 | 2010-11-23 09:45:50 +0000 | [diff] [blame] | 179 | max >>= 16; |
Chris Wilson | a957355 | 2010-08-22 13:18:16 +0100 | [diff] [blame] | 180 | } else { |
Chris Wilson | a957355 | 2010-08-22 13:18:16 +0100 | [diff] [blame] | 181 | if (IS_PINEVIEW(dev)) { |
| 182 | max >>= 17; |
| 183 | } else { |
| 184 | max >>= 16; |
Chris Wilson | a6c45cf | 2010-09-17 00:32:17 +0100 | [diff] [blame] | 185 | if (INTEL_INFO(dev)->gen < 4) |
Chris Wilson | a957355 | 2010-08-22 13:18:16 +0100 | [diff] [blame] | 186 | max &= ~1; |
| 187 | } |
Takashi Iwai | ba3820a | 2011-03-10 14:02:12 +0100 | [diff] [blame] | 188 | |
| 189 | if (is_backlight_combination_mode(dev)) |
| 190 | max *= 0xff; |
Chris Wilson | a957355 | 2010-08-22 13:18:16 +0100 | [diff] [blame] | 191 | } |
| 192 | |
Chris Wilson | a957355 | 2010-08-22 13:18:16 +0100 | [diff] [blame] | 193 | DRM_DEBUG_DRIVER("max backlight PWM = %d\n", max); |
| 194 | return max; |
| 195 | } |
| 196 | |
| 197 | u32 intel_panel_get_backlight(struct drm_device *dev) |
| 198 | { |
| 199 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 200 | u32 val; |
| 201 | |
| 202 | if (HAS_PCH_SPLIT(dev)) { |
| 203 | val = I915_READ(BLC_PWM_CPU_CTL) & BACKLIGHT_DUTY_CYCLE_MASK; |
| 204 | } else { |
| 205 | val = I915_READ(BLC_PWM_CTL) & BACKLIGHT_DUTY_CYCLE_MASK; |
| 206 | if (IS_PINEVIEW(dev)) |
| 207 | val >>= 1; |
Takashi Iwai | ba3820a | 2011-03-10 14:02:12 +0100 | [diff] [blame] | 208 | |
Akshay Joshi | 0206e35 | 2011-08-16 15:34:10 -0400 | [diff] [blame] | 209 | if (is_backlight_combination_mode(dev)) { |
Takashi Iwai | ba3820a | 2011-03-10 14:02:12 +0100 | [diff] [blame] | 210 | u8 lbpc; |
| 211 | |
| 212 | val &= ~1; |
| 213 | pci_read_config_byte(dev->pdev, PCI_LBPC, &lbpc); |
| 214 | val *= lbpc; |
| 215 | } |
Chris Wilson | a957355 | 2010-08-22 13:18:16 +0100 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | DRM_DEBUG_DRIVER("get backlight PWM = %d\n", val); |
| 219 | return val; |
| 220 | } |
| 221 | |
| 222 | static void intel_pch_panel_set_backlight(struct drm_device *dev, u32 level) |
| 223 | { |
| 224 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 225 | u32 val = I915_READ(BLC_PWM_CPU_CTL) & ~BACKLIGHT_DUTY_CYCLE_MASK; |
| 226 | I915_WRITE(BLC_PWM_CPU_CTL, val | level); |
| 227 | } |
| 228 | |
Takashi Iwai | f52c619 | 2011-10-14 11:45:40 +0200 | [diff] [blame] | 229 | 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] | 230 | { |
| 231 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 232 | u32 tmp; |
| 233 | |
| 234 | DRM_DEBUG_DRIVER("set backlight PWM = %d\n", level); |
| 235 | |
| 236 | if (HAS_PCH_SPLIT(dev)) |
| 237 | return intel_pch_panel_set_backlight(dev, level); |
Takashi Iwai | ba3820a | 2011-03-10 14:02:12 +0100 | [diff] [blame] | 238 | |
Akshay Joshi | 0206e35 | 2011-08-16 15:34:10 -0400 | [diff] [blame] | 239 | if (is_backlight_combination_mode(dev)) { |
Takashi Iwai | ba3820a | 2011-03-10 14:02:12 +0100 | [diff] [blame] | 240 | u32 max = intel_panel_get_max_backlight(dev); |
| 241 | u8 lbpc; |
| 242 | |
| 243 | lbpc = level * 0xfe / max + 1; |
| 244 | level /= lbpc; |
| 245 | pci_write_config_byte(dev->pdev, PCI_LBPC, lbpc); |
| 246 | } |
| 247 | |
Chris Wilson | a957355 | 2010-08-22 13:18:16 +0100 | [diff] [blame] | 248 | tmp = I915_READ(BLC_PWM_CTL); |
| 249 | if (IS_PINEVIEW(dev)) { |
| 250 | tmp &= ~(BACKLIGHT_DUTY_CYCLE_MASK - 1); |
| 251 | level <<= 1; |
| 252 | } else |
| 253 | tmp &= ~BACKLIGHT_DUTY_CYCLE_MASK; |
| 254 | I915_WRITE(BLC_PWM_CTL, tmp | level); |
| 255 | } |
Chris Wilson | 47356eb | 2011-01-11 17:06:04 +0000 | [diff] [blame] | 256 | |
Takashi Iwai | f52c619 | 2011-10-14 11:45:40 +0200 | [diff] [blame] | 257 | void intel_panel_set_backlight(struct drm_device *dev, u32 level) |
| 258 | { |
| 259 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 260 | |
| 261 | dev_priv->backlight_level = level; |
| 262 | if (dev_priv->backlight_enabled) |
| 263 | intel_panel_actually_set_backlight(dev, level); |
| 264 | } |
| 265 | |
Chris Wilson | 47356eb | 2011-01-11 17:06:04 +0000 | [diff] [blame] | 266 | void intel_panel_disable_backlight(struct drm_device *dev) |
| 267 | { |
| 268 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 269 | |
Takashi Iwai | f52c619 | 2011-10-14 11:45:40 +0200 | [diff] [blame] | 270 | dev_priv->backlight_enabled = false; |
| 271 | intel_panel_actually_set_backlight(dev, 0); |
Chris Wilson | 47356eb | 2011-01-11 17:06:04 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | void intel_panel_enable_backlight(struct drm_device *dev) |
| 275 | { |
| 276 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 277 | |
| 278 | if (dev_priv->backlight_level == 0) |
| 279 | dev_priv->backlight_level = intel_panel_get_max_backlight(dev); |
| 280 | |
Chris Wilson | 47356eb | 2011-01-11 17:06:04 +0000 | [diff] [blame] | 281 | dev_priv->backlight_enabled = true; |
Takashi Iwai | f52c619 | 2011-10-14 11:45:40 +0200 | [diff] [blame] | 282 | intel_panel_actually_set_backlight(dev, dev_priv->backlight_level); |
Chris Wilson | 47356eb | 2011-01-11 17:06:04 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Matthew Garrett | aaa6fd2 | 2011-08-12 12:11:33 +0200 | [diff] [blame] | 285 | static void intel_panel_init_backlight(struct drm_device *dev) |
Chris Wilson | 47356eb | 2011-01-11 17:06:04 +0000 | [diff] [blame] | 286 | { |
| 287 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 288 | |
Indan Zupancic | c8303e7 | 2011-01-12 11:59:19 +0000 | [diff] [blame] | 289 | dev_priv->backlight_level = intel_panel_get_backlight(dev); |
Chris Wilson | 47356eb | 2011-01-11 17:06:04 +0000 | [diff] [blame] | 290 | dev_priv->backlight_enabled = dev_priv->backlight_level != 0; |
| 291 | } |
Chris Wilson | fe16d94 | 2011-02-12 10:29:38 +0000 | [diff] [blame] | 292 | |
| 293 | enum drm_connector_status |
| 294 | intel_panel_detect(struct drm_device *dev) |
| 295 | { |
Dave Airlie | bcd5023 | 2011-03-14 14:17:55 +1000 | [diff] [blame] | 296 | #if 0 |
Chris Wilson | fe16d94 | 2011-02-12 10:29:38 +0000 | [diff] [blame] | 297 | struct drm_i915_private *dev_priv = dev->dev_private; |
Dave Airlie | bcd5023 | 2011-03-14 14:17:55 +1000 | [diff] [blame] | 298 | #endif |
Chris Wilson | fe16d94 | 2011-02-12 10:29:38 +0000 | [diff] [blame] | 299 | |
Chris Wilson | fca8740 | 2011-02-17 13:44:48 +0000 | [diff] [blame] | 300 | if (i915_panel_ignore_lid) |
| 301 | return i915_panel_ignore_lid > 0 ? |
| 302 | connector_status_connected : |
| 303 | connector_status_disconnected; |
| 304 | |
Dave Airlie | bcd5023 | 2011-03-14 14:17:55 +1000 | [diff] [blame] | 305 | /* opregion lid state on HP 2540p is wrong at boot up, |
| 306 | * appears to be either the BIOS or Linux ACPI fault */ |
| 307 | #if 0 |
Chris Wilson | fe16d94 | 2011-02-12 10:29:38 +0000 | [diff] [blame] | 308 | /* Assume that the BIOS does not lie through the OpRegion... */ |
| 309 | if (dev_priv->opregion.lid_state) |
| 310 | return ioread32(dev_priv->opregion.lid_state) & 0x1 ? |
| 311 | connector_status_connected : |
| 312 | connector_status_disconnected; |
Dave Airlie | bcd5023 | 2011-03-14 14:17:55 +1000 | [diff] [blame] | 313 | #endif |
Chris Wilson | fe16d94 | 2011-02-12 10:29:38 +0000 | [diff] [blame] | 314 | |
| 315 | return connector_status_unknown; |
| 316 | } |
Matthew Garrett | aaa6fd2 | 2011-08-12 12:11:33 +0200 | [diff] [blame] | 317 | |
| 318 | #ifdef CONFIG_BACKLIGHT_CLASS_DEVICE |
| 319 | static int intel_panel_update_status(struct backlight_device *bd) |
| 320 | { |
| 321 | struct drm_device *dev = bl_get_data(bd); |
| 322 | intel_panel_set_backlight(dev, bd->props.brightness); |
| 323 | return 0; |
| 324 | } |
| 325 | |
| 326 | static int intel_panel_get_brightness(struct backlight_device *bd) |
| 327 | { |
| 328 | struct drm_device *dev = bl_get_data(bd); |
Takashi Iwai | 04b3867 | 2011-11-16 10:58:03 +0100 | [diff] [blame^] | 329 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 330 | return dev_priv->backlight_level; |
Matthew Garrett | aaa6fd2 | 2011-08-12 12:11:33 +0200 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | static const struct backlight_ops intel_panel_bl_ops = { |
| 334 | .update_status = intel_panel_update_status, |
| 335 | .get_brightness = intel_panel_get_brightness, |
| 336 | }; |
| 337 | |
| 338 | int intel_panel_setup_backlight(struct drm_device *dev) |
| 339 | { |
| 340 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 341 | struct backlight_properties props; |
| 342 | struct drm_connector *connector; |
| 343 | |
| 344 | intel_panel_init_backlight(dev); |
| 345 | |
| 346 | if (dev_priv->int_lvds_connector) |
| 347 | connector = dev_priv->int_lvds_connector; |
| 348 | else if (dev_priv->int_edp_connector) |
| 349 | connector = dev_priv->int_edp_connector; |
| 350 | else |
| 351 | return -ENODEV; |
| 352 | |
| 353 | props.type = BACKLIGHT_RAW; |
| 354 | props.max_brightness = intel_panel_get_max_backlight(dev); |
| 355 | dev_priv->backlight = |
| 356 | backlight_device_register("intel_backlight", |
| 357 | &connector->kdev, dev, |
| 358 | &intel_panel_bl_ops, &props); |
| 359 | |
| 360 | if (IS_ERR(dev_priv->backlight)) { |
| 361 | DRM_ERROR("Failed to register backlight: %ld\n", |
| 362 | PTR_ERR(dev_priv->backlight)); |
| 363 | dev_priv->backlight = NULL; |
| 364 | return -ENODEV; |
| 365 | } |
| 366 | dev_priv->backlight->props.brightness = intel_panel_get_backlight(dev); |
| 367 | return 0; |
| 368 | } |
| 369 | |
| 370 | void intel_panel_destroy_backlight(struct drm_device *dev) |
| 371 | { |
| 372 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 373 | if (dev_priv->backlight) |
| 374 | backlight_device_unregister(dev_priv->backlight); |
| 375 | } |
| 376 | #else |
| 377 | int intel_panel_setup_backlight(struct drm_device *dev) |
| 378 | { |
| 379 | intel_panel_init_backlight(dev); |
| 380 | return 0; |
| 381 | } |
| 382 | |
| 383 | void intel_panel_destroy_backlight(struct drm_device *dev) |
| 384 | { |
| 385 | return; |
| 386 | } |
| 387 | #endif |