• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
32 
33 #include <linux/moduleparam.h>
34 #include "intel_drv.h"
35 
36 #define PCI_LBPC 0xf4 /* legacy/combination backlight modes */
37 
38 void
intel_fixed_panel_mode(struct drm_display_mode * fixed_mode,struct drm_display_mode * adjusted_mode)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;
53 }
54 
55 /* adjusted_mode has been preset to be the panel's fixed mode */
56 void
intel_pch_panel_fitting(struct drm_device * dev,int fitting_mode,const struct drm_display_mode * mode,struct drm_display_mode * adjusted_mode)57 intel_pch_panel_fitting(struct drm_device *dev,
58 			int fitting_mode,
59 			const struct drm_display_mode *mode,
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;
87 				if (width & 1)
88 					width++;
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;
94 				if (height & 1)
95 				    height++;
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 }
119 
is_backlight_combination_mode(struct drm_device * dev)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 
i915_read_blc_pwm_ctl(struct drm_device * dev)133 static u32 i915_read_blc_pwm_ctl(struct drm_device *dev)
134 {
135 	struct drm_i915_private *dev_priv = dev->dev_private;
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);
142 		if (dev_priv->regfile.saveBLC_PWM_CTL2 == 0) {
143 			dev_priv->regfile.saveBLC_PWM_CTL2 = val;
144 		} else if (val == 0) {
145 			val = dev_priv->regfile.saveBLC_PWM_CTL2;
146 			I915_WRITE(BLC_PWM_PCH_CTL2, val);
147 		}
148 	} else {
149 		val = I915_READ(BLC_PWM_CTL);
150 		if (dev_priv->regfile.saveBLC_PWM_CTL == 0) {
151 			dev_priv->regfile.saveBLC_PWM_CTL = val;
152 			if (INTEL_INFO(dev)->gen >= 4)
153 				dev_priv->regfile.saveBLC_PWM_CTL2 =
154 					I915_READ(BLC_PWM_CTL2);
155 		} else if (val == 0) {
156 			val = dev_priv->regfile.saveBLC_PWM_CTL;
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);
161 		}
162 	}
163 
164 	return val;
165 }
166 
_intel_panel_get_max_backlight(struct drm_device * dev)167 static u32 _intel_panel_get_max_backlight(struct drm_device *dev)
168 {
169 	u32 max;
170 
171 	max = i915_read_blc_pwm_ctl(dev);
172 
173 	if (HAS_PCH_SPLIT(dev)) {
174 		max >>= 16;
175 	} else {
176 		if (INTEL_INFO(dev)->gen < 4)
177 			max >>= 17;
178 		else
179 			max >>= 16;
180 
181 		if (is_backlight_combination_mode(dev))
182 			max *= 0xff;
183 	}
184 
185 	return max;
186 }
187 
intel_panel_get_max_backlight(struct drm_device * dev)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 
201 	DRM_DEBUG_DRIVER("max backlight PWM = %d\n", max);
202 	return max;
203 }
204 
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 "
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.");
211 module_param_named(invert_brightness, i915_panel_invert_brightness, int, 0600);
intel_panel_compute_brightness(struct drm_device * dev,u32 val)212 static u32 intel_panel_compute_brightness(struct drm_device *dev, u32 val)
213 {
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)
221 		return intel_panel_get_max_backlight(dev) - val;
222 
223 	return val;
224 }
225 
intel_panel_get_backlight(struct drm_device * dev)226 static u32 intel_panel_get_backlight(struct drm_device *dev)
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;
235 		if (INTEL_INFO(dev)->gen < 4)
236 			val >>= 1;
237 
238 		if (is_backlight_combination_mode(dev)) {
239 			u8 lbpc;
240 
241 			pci_read_config_byte(dev->pdev, PCI_LBPC, &lbpc);
242 			val *= lbpc;
243 		}
244 	}
245 
246 	val = intel_panel_compute_brightness(dev, val);
247 	DRM_DEBUG_DRIVER("get backlight PWM = %d\n", val);
248 	return val;
249 }
250 
intel_pch_panel_set_backlight(struct drm_device * dev,u32 level)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 
intel_panel_actually_set_backlight(struct drm_device * dev,u32 level)258 static void intel_panel_actually_set_backlight(struct drm_device *dev, u32 level)
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);
264 	level = intel_panel_compute_brightness(dev, level);
265 
266 	if (HAS_PCH_SPLIT(dev))
267 		return intel_pch_panel_set_backlight(dev, level);
268 
269 	if (is_backlight_combination_mode(dev)) {
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 
278 	tmp = I915_READ(BLC_PWM_CTL);
279 	if (INTEL_INFO(dev)->gen < 4)
280 		level <<= 1;
281 	tmp &= ~BACKLIGHT_DUTY_CYCLE_MASK;
282 	I915_WRITE(BLC_PWM_CTL, tmp | level);
283 }
284 
intel_panel_set_backlight(struct drm_device * dev,u32 level)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.device)
291 		dev_priv->backlight.device->props.brightness = level;
292 
293 	if (dev_priv->backlight.enabled)
294 		intel_panel_actually_set_backlight(dev, level);
295 }
296 
intel_panel_disable_backlight(struct drm_device * dev)297 void intel_panel_disable_backlight(struct drm_device *dev)
298 {
299 	struct drm_i915_private *dev_priv = dev->dev_private;
300 
301 	dev_priv->backlight.enabled = false;
302 	intel_panel_actually_set_backlight(dev, 0);
303 
304 	if (INTEL_INFO(dev)->gen >= 4) {
305 		uint32_t reg, tmp;
306 
307 		reg = HAS_PCH_SPLIT(dev) ? BLC_PWM_CPU_CTL2 : BLC_PWM_CTL2;
308 
309 		I915_WRITE(reg, I915_READ(reg) & ~BLM_PWM_ENABLE);
310 
311 		if (HAS_PCH_SPLIT(dev)) {
312 			tmp = I915_READ(BLC_PWM_PCH_CTL1);
313 			tmp &= ~BLM_PCH_PWM_ENABLE;
314 			I915_WRITE(BLC_PWM_PCH_CTL1, tmp);
315 		}
316 	}
317 }
318 
intel_panel_enable_backlight(struct drm_device * dev,enum pipe pipe)319 void intel_panel_enable_backlight(struct drm_device *dev,
320 				  enum pipe pipe)
321 {
322 	struct drm_i915_private *dev_priv = dev->dev_private;
323 
324 	if (dev_priv->backlight.level == 0) {
325 		dev_priv->backlight.level = intel_panel_get_max_backlight(dev);
326 		if (dev_priv->backlight.device)
327 			dev_priv->backlight.device->props.brightness =
328 				dev_priv->backlight.level;
329 	}
330 
331 	if (INTEL_INFO(dev)->gen >= 4) {
332 		uint32_t reg, tmp;
333 
334 		reg = HAS_PCH_SPLIT(dev) ? BLC_PWM_CPU_CTL2 : BLC_PWM_CTL2;
335 
336 
337 		tmp = I915_READ(reg);
338 
339 		/* Note that this can also get called through dpms changes. And
340 		 * we don't track the backlight dpms state, hence check whether
341 		 * we have to do anything first. */
342 		if (tmp & BLM_PWM_ENABLE)
343 			goto set_level;
344 
345 		if (INTEL_INFO(dev)->num_pipes == 3)
346 			tmp &= ~BLM_PIPE_SELECT_IVB;
347 		else
348 			tmp &= ~BLM_PIPE_SELECT;
349 
350 		tmp |= BLM_PIPE(pipe);
351 		tmp &= ~BLM_PWM_ENABLE;
352 
353 		I915_WRITE(reg, tmp);
354 		POSTING_READ(reg);
355 		I915_WRITE(reg, tmp | BLM_PWM_ENABLE);
356 
357 		if (HAS_PCH_SPLIT(dev)) {
358 			tmp = I915_READ(BLC_PWM_PCH_CTL1);
359 			tmp |= BLM_PCH_PWM_ENABLE;
360 			tmp &= ~BLM_PCH_OVERRIDE_ENABLE;
361 			I915_WRITE(BLC_PWM_PCH_CTL1, tmp);
362 		}
363 	}
364 
365 set_level:
366 	/* Call below after setting BLC_PWM_CPU_CTL2 and BLC_PWM_PCH_CTL1.
367 	 * BLC_PWM_CPU_CTL may be cleared to zero automatically when these
368 	 * registers are set.
369 	 */
370 	dev_priv->backlight.enabled = true;
371 	intel_panel_actually_set_backlight(dev, dev_priv->backlight.level);
372 }
373 
intel_panel_init_backlight(struct drm_device * dev)374 static void intel_panel_init_backlight(struct drm_device *dev)
375 {
376 	struct drm_i915_private *dev_priv = dev->dev_private;
377 
378 	dev_priv->backlight.level = intel_panel_get_backlight(dev);
379 	dev_priv->backlight.enabled = dev_priv->backlight.level != 0;
380 }
381 
382 enum drm_connector_status
intel_panel_detect(struct drm_device * dev)383 intel_panel_detect(struct drm_device *dev)
384 {
385 	struct drm_i915_private *dev_priv = dev->dev_private;
386 
387 	/* Assume that the BIOS does not lie through the OpRegion... */
388 	if (!i915_panel_ignore_lid && dev_priv->opregion.lid_state) {
389 		return ioread32(dev_priv->opregion.lid_state) & 0x1 ?
390 			connector_status_connected :
391 			connector_status_disconnected;
392 	}
393 
394 	switch (i915_panel_ignore_lid) {
395 	case -2:
396 		return connector_status_connected;
397 	case -1:
398 		return connector_status_disconnected;
399 	default:
400 		return connector_status_unknown;
401 	}
402 }
403 
404 #ifdef CONFIG_BACKLIGHT_CLASS_DEVICE
intel_panel_update_status(struct backlight_device * bd)405 static int intel_panel_update_status(struct backlight_device *bd)
406 {
407 	struct drm_device *dev = bl_get_data(bd);
408 	intel_panel_set_backlight(dev, bd->props.brightness);
409 	return 0;
410 }
411 
intel_panel_get_brightness(struct backlight_device * bd)412 static int intel_panel_get_brightness(struct backlight_device *bd)
413 {
414 	struct drm_device *dev = bl_get_data(bd);
415 	return intel_panel_get_backlight(dev);
416 }
417 
418 static const struct backlight_ops intel_panel_bl_ops = {
419 	.update_status = intel_panel_update_status,
420 	.get_brightness = intel_panel_get_brightness,
421 };
422 
intel_panel_setup_backlight(struct drm_connector * connector)423 int intel_panel_setup_backlight(struct drm_connector *connector)
424 {
425 	struct drm_device *dev = connector->dev;
426 	struct drm_i915_private *dev_priv = dev->dev_private;
427 	struct backlight_properties props;
428 
429 	intel_panel_init_backlight(dev);
430 
431 	if (WARN_ON(dev_priv->backlight.device))
432 		return -ENODEV;
433 
434 	memset(&props, 0, sizeof(props));
435 	props.type = BACKLIGHT_RAW;
436 	props.brightness = dev_priv->backlight.level;
437 	props.max_brightness = _intel_panel_get_max_backlight(dev);
438 	if (props.max_brightness == 0) {
439 		DRM_DEBUG_DRIVER("Failed to get maximum backlight value\n");
440 		return -ENODEV;
441 	}
442 	dev_priv->backlight.device =
443 		backlight_device_register("intel_backlight",
444 					  &connector->kdev, dev,
445 					  &intel_panel_bl_ops, &props);
446 
447 	if (IS_ERR(dev_priv->backlight.device)) {
448 		DRM_ERROR("Failed to register backlight: %ld\n",
449 			  PTR_ERR(dev_priv->backlight.device));
450 		dev_priv->backlight.device = NULL;
451 		return -ENODEV;
452 	}
453 	return 0;
454 }
455 
intel_panel_destroy_backlight(struct drm_device * dev)456 void intel_panel_destroy_backlight(struct drm_device *dev)
457 {
458 	struct drm_i915_private *dev_priv = dev->dev_private;
459 	if (dev_priv->backlight.device) {
460 		backlight_device_unregister(dev_priv->backlight.device);
461 		dev_priv->backlight.device = NULL;
462 	}
463 }
464 #else
intel_panel_setup_backlight(struct drm_connector * connector)465 int intel_panel_setup_backlight(struct drm_connector *connector)
466 {
467 	intel_panel_init_backlight(connector->dev);
468 	return 0;
469 }
470 
intel_panel_destroy_backlight(struct drm_device * dev)471 void intel_panel_destroy_backlight(struct drm_device *dev)
472 {
473 	return;
474 }
475 #endif
476 
intel_panel_init(struct intel_panel * panel,struct drm_display_mode * fixed_mode)477 int intel_panel_init(struct intel_panel *panel,
478 		     struct drm_display_mode *fixed_mode)
479 {
480 	panel->fixed_mode = fixed_mode;
481 
482 	return 0;
483 }
484 
intel_panel_fini(struct intel_panel * panel)485 void intel_panel_fini(struct intel_panel *panel)
486 {
487 	struct intel_connector *intel_connector =
488 		container_of(panel, struct intel_connector, panel);
489 
490 	if (panel->fixed_mode)
491 		drm_mode_destroy(intel_connector->base.dev, panel->fixed_mode);
492 }
493