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