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