2 * Copyright © 2006-2010 Intel Corporation
3 * Copyright (c) 2006 Dave Airlie <airlied@linux.ie>
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:
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
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.
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>
31 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
33 #include <linux/moduleparam.h>
34 #include "intel_drv.h"
37 intel_fixed_panel_mode(const struct drm_display_mode
*fixed_mode
,
38 struct drm_display_mode
*adjusted_mode
)
40 drm_mode_copy(adjusted_mode
, fixed_mode
);
42 drm_mode_set_crtcinfo(adjusted_mode
, 0);
46 * intel_find_panel_downclock - find the reduced downclock for LVDS in EDID
48 * @fixed_mode : panel native mode
49 * @connector: LVDS/eDP connector
51 * Return downclock_avail
52 * Find the reduced downclock for LVDS/eDP in EDID.
54 struct drm_display_mode
*
55 intel_find_panel_downclock(struct drm_device
*dev
,
56 struct drm_display_mode
*fixed_mode
,
57 struct drm_connector
*connector
)
59 struct drm_display_mode
*scan
, *tmp_mode
;
62 temp_downclock
= fixed_mode
->clock
;
65 list_for_each_entry(scan
, &connector
->probed_modes
, head
) {
67 * If one mode has the same resolution with the fixed_panel
68 * mode while they have the different refresh rate, it means
69 * that the reduced downclock is found. In such
70 * case we can set the different FPx0/1 to dynamically select
71 * between low and high frequency.
73 if (scan
->hdisplay
== fixed_mode
->hdisplay
&&
74 scan
->hsync_start
== fixed_mode
->hsync_start
&&
75 scan
->hsync_end
== fixed_mode
->hsync_end
&&
76 scan
->htotal
== fixed_mode
->htotal
&&
77 scan
->vdisplay
== fixed_mode
->vdisplay
&&
78 scan
->vsync_start
== fixed_mode
->vsync_start
&&
79 scan
->vsync_end
== fixed_mode
->vsync_end
&&
80 scan
->vtotal
== fixed_mode
->vtotal
) {
81 if (scan
->clock
< temp_downclock
) {
83 * The downclock is already found. But we
84 * expect to find the lower downclock.
86 temp_downclock
= scan
->clock
;
92 if (temp_downclock
< fixed_mode
->clock
)
93 return drm_mode_duplicate(dev
, tmp_mode
);
98 /* adjusted_mode has been preset to be the panel's fixed mode */
100 intel_pch_panel_fitting(struct intel_crtc
*intel_crtc
,
101 struct intel_crtc_state
*pipe_config
,
104 struct drm_display_mode
*adjusted_mode
;
105 int x
, y
, width
, height
;
107 adjusted_mode
= &pipe_config
->base
.adjusted_mode
;
109 x
= y
= width
= height
= 0;
111 /* Native modes don't need fitting */
112 if (adjusted_mode
->hdisplay
== pipe_config
->pipe_src_w
&&
113 adjusted_mode
->vdisplay
== pipe_config
->pipe_src_h
)
116 switch (fitting_mode
) {
117 case DRM_MODE_SCALE_CENTER
:
118 width
= pipe_config
->pipe_src_w
;
119 height
= pipe_config
->pipe_src_h
;
120 x
= (adjusted_mode
->hdisplay
- width
+ 1)/2;
121 y
= (adjusted_mode
->vdisplay
- height
+ 1)/2;
124 case DRM_MODE_SCALE_ASPECT
:
125 /* Scale but preserve the aspect ratio */
127 u32 scaled_width
= adjusted_mode
->hdisplay
128 * pipe_config
->pipe_src_h
;
129 u32 scaled_height
= pipe_config
->pipe_src_w
130 * adjusted_mode
->vdisplay
;
131 if (scaled_width
> scaled_height
) { /* pillar */
132 width
= scaled_height
/ pipe_config
->pipe_src_h
;
135 x
= (adjusted_mode
->hdisplay
- width
+ 1) / 2;
137 height
= adjusted_mode
->vdisplay
;
138 } else if (scaled_width
< scaled_height
) { /* letter */
139 height
= scaled_width
/ pipe_config
->pipe_src_w
;
142 y
= (adjusted_mode
->vdisplay
- height
+ 1) / 2;
144 width
= adjusted_mode
->hdisplay
;
147 width
= adjusted_mode
->hdisplay
;
148 height
= adjusted_mode
->vdisplay
;
153 case DRM_MODE_SCALE_FULLSCREEN
:
155 width
= adjusted_mode
->hdisplay
;
156 height
= adjusted_mode
->vdisplay
;
160 WARN(1, "bad panel fit mode: %d\n", fitting_mode
);
165 pipe_config
->pch_pfit
.pos
= (x
<< 16) | y
;
166 pipe_config
->pch_pfit
.size
= (width
<< 16) | height
;
167 pipe_config
->pch_pfit
.enabled
= pipe_config
->pch_pfit
.size
!= 0;
171 centre_horizontally(struct drm_display_mode
*mode
,
174 u32 border
, sync_pos
, blank_width
, sync_width
;
176 /* keep the hsync and hblank widths constant */
177 sync_width
= mode
->crtc_hsync_end
- mode
->crtc_hsync_start
;
178 blank_width
= mode
->crtc_hblank_end
- mode
->crtc_hblank_start
;
179 sync_pos
= (blank_width
- sync_width
+ 1) / 2;
181 border
= (mode
->hdisplay
- width
+ 1) / 2;
182 border
+= border
& 1; /* make the border even */
184 mode
->crtc_hdisplay
= width
;
185 mode
->crtc_hblank_start
= width
+ border
;
186 mode
->crtc_hblank_end
= mode
->crtc_hblank_start
+ blank_width
;
188 mode
->crtc_hsync_start
= mode
->crtc_hblank_start
+ sync_pos
;
189 mode
->crtc_hsync_end
= mode
->crtc_hsync_start
+ sync_width
;
193 centre_vertically(struct drm_display_mode
*mode
,
196 u32 border
, sync_pos
, blank_width
, sync_width
;
198 /* keep the vsync and vblank widths constant */
199 sync_width
= mode
->crtc_vsync_end
- mode
->crtc_vsync_start
;
200 blank_width
= mode
->crtc_vblank_end
- mode
->crtc_vblank_start
;
201 sync_pos
= (blank_width
- sync_width
+ 1) / 2;
203 border
= (mode
->vdisplay
- height
+ 1) / 2;
205 mode
->crtc_vdisplay
= height
;
206 mode
->crtc_vblank_start
= height
+ border
;
207 mode
->crtc_vblank_end
= mode
->crtc_vblank_start
+ blank_width
;
209 mode
->crtc_vsync_start
= mode
->crtc_vblank_start
+ sync_pos
;
210 mode
->crtc_vsync_end
= mode
->crtc_vsync_start
+ sync_width
;
213 static inline u32
panel_fitter_scaling(u32 source
, u32 target
)
216 * Floating point operation is not supported. So the FACTOR
217 * is defined, which can avoid the floating point computation
218 * when calculating the panel ratio.
221 #define FACTOR (1 << ACCURACY)
222 u32 ratio
= source
* FACTOR
/ target
;
223 return (FACTOR
* ratio
+ FACTOR
/2) / FACTOR
;
226 static void i965_scale_aspect(struct intel_crtc_state
*pipe_config
,
229 struct drm_display_mode
*adjusted_mode
= &pipe_config
->base
.adjusted_mode
;
230 u32 scaled_width
= adjusted_mode
->hdisplay
*
231 pipe_config
->pipe_src_h
;
232 u32 scaled_height
= pipe_config
->pipe_src_w
*
233 adjusted_mode
->vdisplay
;
235 /* 965+ is easy, it does everything in hw */
236 if (scaled_width
> scaled_height
)
237 *pfit_control
|= PFIT_ENABLE
|
239 else if (scaled_width
< scaled_height
)
240 *pfit_control
|= PFIT_ENABLE
|
242 else if (adjusted_mode
->hdisplay
!= pipe_config
->pipe_src_w
)
243 *pfit_control
|= PFIT_ENABLE
| PFIT_SCALING_AUTO
;
246 static void i9xx_scale_aspect(struct intel_crtc_state
*pipe_config
,
247 u32
*pfit_control
, u32
*pfit_pgm_ratios
,
250 struct drm_display_mode
*adjusted_mode
= &pipe_config
->base
.adjusted_mode
;
251 u32 scaled_width
= adjusted_mode
->hdisplay
*
252 pipe_config
->pipe_src_h
;
253 u32 scaled_height
= pipe_config
->pipe_src_w
*
254 adjusted_mode
->vdisplay
;
258 * For earlier chips we have to calculate the scaling
259 * ratio by hand and program it into the
260 * PFIT_PGM_RATIO register
262 if (scaled_width
> scaled_height
) { /* pillar */
263 centre_horizontally(adjusted_mode
,
265 pipe_config
->pipe_src_h
);
267 *border
= LVDS_BORDER_ENABLE
;
268 if (pipe_config
->pipe_src_h
!= adjusted_mode
->vdisplay
) {
269 bits
= panel_fitter_scaling(pipe_config
->pipe_src_h
,
270 adjusted_mode
->vdisplay
);
272 *pfit_pgm_ratios
|= (bits
<< PFIT_HORIZ_SCALE_SHIFT
|
273 bits
<< PFIT_VERT_SCALE_SHIFT
);
274 *pfit_control
|= (PFIT_ENABLE
|
275 VERT_INTERP_BILINEAR
|
276 HORIZ_INTERP_BILINEAR
);
278 } else if (scaled_width
< scaled_height
) { /* letter */
279 centre_vertically(adjusted_mode
,
281 pipe_config
->pipe_src_w
);
283 *border
= LVDS_BORDER_ENABLE
;
284 if (pipe_config
->pipe_src_w
!= adjusted_mode
->hdisplay
) {
285 bits
= panel_fitter_scaling(pipe_config
->pipe_src_w
,
286 adjusted_mode
->hdisplay
);
288 *pfit_pgm_ratios
|= (bits
<< PFIT_HORIZ_SCALE_SHIFT
|
289 bits
<< PFIT_VERT_SCALE_SHIFT
);
290 *pfit_control
|= (PFIT_ENABLE
|
291 VERT_INTERP_BILINEAR
|
292 HORIZ_INTERP_BILINEAR
);
295 /* Aspects match, Let hw scale both directions */
296 *pfit_control
|= (PFIT_ENABLE
|
297 VERT_AUTO_SCALE
| HORIZ_AUTO_SCALE
|
298 VERT_INTERP_BILINEAR
|
299 HORIZ_INTERP_BILINEAR
);
303 void intel_gmch_panel_fitting(struct intel_crtc
*intel_crtc
,
304 struct intel_crtc_state
*pipe_config
,
307 struct drm_device
*dev
= intel_crtc
->base
.dev
;
308 u32 pfit_control
= 0, pfit_pgm_ratios
= 0, border
= 0;
309 struct drm_display_mode
*adjusted_mode
;
311 adjusted_mode
= &pipe_config
->base
.adjusted_mode
;
313 /* Native modes don't need fitting */
314 if (adjusted_mode
->hdisplay
== pipe_config
->pipe_src_w
&&
315 adjusted_mode
->vdisplay
== pipe_config
->pipe_src_h
)
318 switch (fitting_mode
) {
319 case DRM_MODE_SCALE_CENTER
:
321 * For centered modes, we have to calculate border widths &
322 * heights and modify the values programmed into the CRTC.
324 centre_horizontally(adjusted_mode
, pipe_config
->pipe_src_w
);
325 centre_vertically(adjusted_mode
, pipe_config
->pipe_src_h
);
326 border
= LVDS_BORDER_ENABLE
;
328 case DRM_MODE_SCALE_ASPECT
:
329 /* Scale but preserve the aspect ratio */
330 if (INTEL_INFO(dev
)->gen
>= 4)
331 i965_scale_aspect(pipe_config
, &pfit_control
);
333 i9xx_scale_aspect(pipe_config
, &pfit_control
,
334 &pfit_pgm_ratios
, &border
);
336 case DRM_MODE_SCALE_FULLSCREEN
:
338 * Full scaling, even if it changes the aspect ratio.
339 * Fortunately this is all done for us in hw.
341 if (pipe_config
->pipe_src_h
!= adjusted_mode
->vdisplay
||
342 pipe_config
->pipe_src_w
!= adjusted_mode
->hdisplay
) {
343 pfit_control
|= PFIT_ENABLE
;
344 if (INTEL_INFO(dev
)->gen
>= 4)
345 pfit_control
|= PFIT_SCALING_AUTO
;
347 pfit_control
|= (VERT_AUTO_SCALE
|
348 VERT_INTERP_BILINEAR
|
350 HORIZ_INTERP_BILINEAR
);
354 WARN(1, "bad panel fit mode: %d\n", fitting_mode
);
358 /* 965+ wants fuzzy fitting */
359 /* FIXME: handle multiple panels by failing gracefully */
360 if (INTEL_INFO(dev
)->gen
>= 4)
361 pfit_control
|= ((intel_crtc
->pipe
<< PFIT_PIPE_SHIFT
) |
365 if ((pfit_control
& PFIT_ENABLE
) == 0) {
370 /* Make sure pre-965 set dither correctly for 18bpp panels. */
371 if (INTEL_INFO(dev
)->gen
< 4 && pipe_config
->pipe_bpp
== 18)
372 pfit_control
|= PANEL_8TO6_DITHER_ENABLE
;
374 pipe_config
->gmch_pfit
.control
= pfit_control
;
375 pipe_config
->gmch_pfit
.pgm_ratios
= pfit_pgm_ratios
;
376 pipe_config
->gmch_pfit
.lvds_border_bits
= border
;
379 enum drm_connector_status
380 intel_panel_detect(struct drm_device
*dev
)
382 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
384 /* Assume that the BIOS does not lie through the OpRegion... */
385 if (!i915
.panel_ignore_lid
&& dev_priv
->opregion
.lid_state
) {
386 return ioread32(dev_priv
->opregion
.lid_state
) & 0x1 ?
387 connector_status_connected
:
388 connector_status_disconnected
;
391 switch (i915
.panel_ignore_lid
) {
393 return connector_status_connected
;
395 return connector_status_disconnected
;
397 return connector_status_unknown
;
402 * scale - scale values from one range to another
404 * @source_val: value in range [@source_min..@source_max]
406 * Return @source_val in range [@source_min..@source_max] scaled to range
407 * [@target_min..@target_max].
409 static uint32_t scale(uint32_t source_val
,
410 uint32_t source_min
, uint32_t source_max
,
411 uint32_t target_min
, uint32_t target_max
)
415 WARN_ON(source_min
> source_max
);
416 WARN_ON(target_min
> target_max
);
419 source_val
= clamp(source_val
, source_min
, source_max
);
421 /* avoid overflows */
422 target_val
= DIV_ROUND_CLOSEST_ULL((uint64_t)(source_val
- source_min
) *
423 (target_max
- target_min
), source_max
- source_min
);
424 target_val
+= target_min
;
429 /* Scale user_level in range [0..user_max] to [hw_min..hw_max]. */
430 static inline u32
scale_user_to_hw(struct intel_connector
*connector
,
431 u32 user_level
, u32 user_max
)
433 struct intel_panel
*panel
= &connector
->panel
;
435 return scale(user_level
, 0, user_max
,
436 panel
->backlight
.min
, panel
->backlight
.max
);
439 /* Scale user_level in range [0..user_max] to [0..hw_max], clamping the result
440 * to [hw_min..hw_max]. */
441 static inline u32
clamp_user_to_hw(struct intel_connector
*connector
,
442 u32 user_level
, u32 user_max
)
444 struct intel_panel
*panel
= &connector
->panel
;
447 hw_level
= scale(user_level
, 0, user_max
, 0, panel
->backlight
.max
);
448 hw_level
= clamp(hw_level
, panel
->backlight
.min
, panel
->backlight
.max
);
453 /* Scale hw_level in range [hw_min..hw_max] to [0..user_max]. */
454 static inline u32
scale_hw_to_user(struct intel_connector
*connector
,
455 u32 hw_level
, u32 user_max
)
457 struct intel_panel
*panel
= &connector
->panel
;
459 return scale(hw_level
, panel
->backlight
.min
, panel
->backlight
.max
,
463 static u32
intel_panel_compute_brightness(struct intel_connector
*connector
,
466 struct drm_device
*dev
= connector
->base
.dev
;
467 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
468 struct intel_panel
*panel
= &connector
->panel
;
470 WARN_ON(panel
->backlight
.max
== 0);
472 if (i915
.invert_brightness
< 0)
475 if (i915
.invert_brightness
> 0 ||
476 dev_priv
->quirks
& QUIRK_INVERT_BRIGHTNESS
) {
477 return panel
->backlight
.max
- val
;
483 static u32
bdw_get_backlight(struct intel_connector
*connector
)
485 struct drm_device
*dev
= connector
->base
.dev
;
486 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
488 return I915_READ(BLC_PWM_PCH_CTL2
) & BACKLIGHT_DUTY_CYCLE_MASK
;
491 static u32
pch_get_backlight(struct intel_connector
*connector
)
493 struct drm_device
*dev
= connector
->base
.dev
;
494 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
496 return I915_READ(BLC_PWM_CPU_CTL
) & BACKLIGHT_DUTY_CYCLE_MASK
;
499 static u32
i9xx_get_backlight(struct intel_connector
*connector
)
501 struct drm_device
*dev
= connector
->base
.dev
;
502 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
503 struct intel_panel
*panel
= &connector
->panel
;
506 val
= I915_READ(BLC_PWM_CTL
) & BACKLIGHT_DUTY_CYCLE_MASK
;
507 if (INTEL_INFO(dev
)->gen
< 4)
510 if (panel
->backlight
.combination_mode
) {
513 pci_read_config_byte(dev
->pdev
, PCI_LBPC
, &lbpc
);
520 static u32
_vlv_get_backlight(struct drm_device
*dev
, enum pipe pipe
)
522 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
524 if (WARN_ON(pipe
!= PIPE_A
&& pipe
!= PIPE_B
))
527 return I915_READ(VLV_BLC_PWM_CTL(pipe
)) & BACKLIGHT_DUTY_CYCLE_MASK
;
530 static u32
vlv_get_backlight(struct intel_connector
*connector
)
532 struct drm_device
*dev
= connector
->base
.dev
;
533 enum pipe pipe
= intel_get_pipe_from_connector(connector
);
535 return _vlv_get_backlight(dev
, pipe
);
538 static u32
intel_panel_get_backlight(struct intel_connector
*connector
)
540 struct drm_device
*dev
= connector
->base
.dev
;
541 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
542 struct intel_panel
*panel
= &connector
->panel
;
545 mutex_lock(&dev_priv
->backlight_lock
);
547 if (panel
->backlight
.enabled
) {
548 val
= dev_priv
->display
.get_backlight(connector
);
549 val
= intel_panel_compute_brightness(connector
, val
);
552 mutex_unlock(&dev_priv
->backlight_lock
);
554 DRM_DEBUG_DRIVER("get backlight PWM = %d\n", val
);
558 static void bdw_set_backlight(struct intel_connector
*connector
, u32 level
)
560 struct drm_device
*dev
= connector
->base
.dev
;
561 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
562 u32 val
= I915_READ(BLC_PWM_PCH_CTL2
) & ~BACKLIGHT_DUTY_CYCLE_MASK
;
563 I915_WRITE(BLC_PWM_PCH_CTL2
, val
| level
);
566 static void pch_set_backlight(struct intel_connector
*connector
, u32 level
)
568 struct drm_device
*dev
= connector
->base
.dev
;
569 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
572 tmp
= I915_READ(BLC_PWM_CPU_CTL
) & ~BACKLIGHT_DUTY_CYCLE_MASK
;
573 I915_WRITE(BLC_PWM_CPU_CTL
, tmp
| level
);
576 static void i9xx_set_backlight(struct intel_connector
*connector
, u32 level
)
578 struct drm_device
*dev
= connector
->base
.dev
;
579 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
580 struct intel_panel
*panel
= &connector
->panel
;
583 WARN_ON(panel
->backlight
.max
== 0);
585 if (panel
->backlight
.combination_mode
) {
588 lbpc
= level
* 0xfe / panel
->backlight
.max
+ 1;
590 pci_write_config_byte(dev
->pdev
, PCI_LBPC
, lbpc
);
594 mask
= BACKLIGHT_DUTY_CYCLE_MASK
;
597 mask
= BACKLIGHT_DUTY_CYCLE_MASK_PNV
;
600 tmp
= I915_READ(BLC_PWM_CTL
) & ~mask
;
601 I915_WRITE(BLC_PWM_CTL
, tmp
| level
);
604 static void vlv_set_backlight(struct intel_connector
*connector
, u32 level
)
606 struct drm_device
*dev
= connector
->base
.dev
;
607 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
608 enum pipe pipe
= intel_get_pipe_from_connector(connector
);
611 if (WARN_ON(pipe
!= PIPE_A
&& pipe
!= PIPE_B
))
614 tmp
= I915_READ(VLV_BLC_PWM_CTL(pipe
)) & ~BACKLIGHT_DUTY_CYCLE_MASK
;
615 I915_WRITE(VLV_BLC_PWM_CTL(pipe
), tmp
| level
);
619 intel_panel_actually_set_backlight(struct intel_connector
*connector
, u32 level
)
621 struct drm_device
*dev
= connector
->base
.dev
;
622 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
624 DRM_DEBUG_DRIVER("set backlight PWM = %d\n", level
);
626 level
= intel_panel_compute_brightness(connector
, level
);
627 dev_priv
->display
.set_backlight(connector
, level
);
630 /* set backlight brightness to level in range [0..max], scaling wrt hw min */
631 static void intel_panel_set_backlight(struct intel_connector
*connector
,
632 u32 user_level
, u32 user_max
)
634 struct drm_device
*dev
= connector
->base
.dev
;
635 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
636 struct intel_panel
*panel
= &connector
->panel
;
639 if (!panel
->backlight
.present
)
642 mutex_lock(&dev_priv
->backlight_lock
);
644 WARN_ON(panel
->backlight
.max
== 0);
646 hw_level
= scale_user_to_hw(connector
, user_level
, user_max
);
647 panel
->backlight
.level
= hw_level
;
649 if (panel
->backlight
.enabled
)
650 intel_panel_actually_set_backlight(connector
, hw_level
);
652 mutex_unlock(&dev_priv
->backlight_lock
);
655 /* set backlight brightness to level in range [0..max], assuming hw min is
658 void intel_panel_set_backlight_acpi(struct intel_connector
*connector
,
659 u32 user_level
, u32 user_max
)
661 struct drm_device
*dev
= connector
->base
.dev
;
662 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
663 struct intel_panel
*panel
= &connector
->panel
;
664 enum pipe pipe
= intel_get_pipe_from_connector(connector
);
668 * INVALID_PIPE may occur during driver init because
669 * connection_mutex isn't held across the entire backlight
670 * setup + modeset readout, and the BIOS can issue the
671 * requests at any time.
673 if (!panel
->backlight
.present
|| pipe
== INVALID_PIPE
)
676 mutex_lock(&dev_priv
->backlight_lock
);
678 WARN_ON(panel
->backlight
.max
== 0);
680 hw_level
= clamp_user_to_hw(connector
, user_level
, user_max
);
681 panel
->backlight
.level
= hw_level
;
683 if (panel
->backlight
.device
)
684 panel
->backlight
.device
->props
.brightness
=
685 scale_hw_to_user(connector
,
686 panel
->backlight
.level
,
687 panel
->backlight
.device
->props
.max_brightness
);
689 if (panel
->backlight
.enabled
)
690 intel_panel_actually_set_backlight(connector
, hw_level
);
692 mutex_unlock(&dev_priv
->backlight_lock
);
695 static void pch_disable_backlight(struct intel_connector
*connector
)
697 struct drm_device
*dev
= connector
->base
.dev
;
698 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
701 intel_panel_actually_set_backlight(connector
, 0);
703 tmp
= I915_READ(BLC_PWM_CPU_CTL2
);
704 I915_WRITE(BLC_PWM_CPU_CTL2
, tmp
& ~BLM_PWM_ENABLE
);
706 tmp
= I915_READ(BLC_PWM_PCH_CTL1
);
707 I915_WRITE(BLC_PWM_PCH_CTL1
, tmp
& ~BLM_PCH_PWM_ENABLE
);
710 static void i9xx_disable_backlight(struct intel_connector
*connector
)
712 intel_panel_actually_set_backlight(connector
, 0);
715 static void i965_disable_backlight(struct intel_connector
*connector
)
717 struct drm_device
*dev
= connector
->base
.dev
;
718 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
721 intel_panel_actually_set_backlight(connector
, 0);
723 tmp
= I915_READ(BLC_PWM_CTL2
);
724 I915_WRITE(BLC_PWM_CTL2
, tmp
& ~BLM_PWM_ENABLE
);
727 static void vlv_disable_backlight(struct intel_connector
*connector
)
729 struct drm_device
*dev
= connector
->base
.dev
;
730 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
731 enum pipe pipe
= intel_get_pipe_from_connector(connector
);
734 if (WARN_ON(pipe
!= PIPE_A
&& pipe
!= PIPE_B
))
737 intel_panel_actually_set_backlight(connector
, 0);
739 tmp
= I915_READ(VLV_BLC_PWM_CTL2(pipe
));
740 I915_WRITE(VLV_BLC_PWM_CTL2(pipe
), tmp
& ~BLM_PWM_ENABLE
);
743 void intel_panel_disable_backlight(struct intel_connector
*connector
)
745 struct drm_device
*dev
= connector
->base
.dev
;
746 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
747 struct intel_panel
*panel
= &connector
->panel
;
749 if (!panel
->backlight
.present
)
753 * Do not disable backlight on the vgaswitcheroo path. When switching
754 * away from i915, the other client may depend on i915 to handle the
755 * backlight. This will leave the backlight on unnecessarily when
756 * another client is not activated.
758 if (dev
->switch_power_state
== DRM_SWITCH_POWER_CHANGING
) {
759 DRM_DEBUG_DRIVER("Skipping backlight disable on vga switch\n");
763 mutex_lock(&dev_priv
->backlight_lock
);
765 if (panel
->backlight
.device
)
766 panel
->backlight
.device
->props
.power
= FB_BLANK_POWERDOWN
;
767 panel
->backlight
.enabled
= false;
768 dev_priv
->display
.disable_backlight(connector
);
770 mutex_unlock(&dev_priv
->backlight_lock
);
773 static void bdw_enable_backlight(struct intel_connector
*connector
)
775 struct drm_device
*dev
= connector
->base
.dev
;
776 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
777 struct intel_panel
*panel
= &connector
->panel
;
778 u32 pch_ctl1
, pch_ctl2
;
780 pch_ctl1
= I915_READ(BLC_PWM_PCH_CTL1
);
781 if (pch_ctl1
& BLM_PCH_PWM_ENABLE
) {
782 DRM_DEBUG_KMS("pch backlight already enabled\n");
783 pch_ctl1
&= ~BLM_PCH_PWM_ENABLE
;
784 I915_WRITE(BLC_PWM_PCH_CTL1
, pch_ctl1
);
787 pch_ctl2
= panel
->backlight
.max
<< 16;
788 I915_WRITE(BLC_PWM_PCH_CTL2
, pch_ctl2
);
791 if (panel
->backlight
.active_low_pwm
)
792 pch_ctl1
|= BLM_PCH_POLARITY
;
794 /* After LPT, override is the default. */
795 if (HAS_PCH_LPT(dev_priv
))
796 pch_ctl1
|= BLM_PCH_OVERRIDE_ENABLE
;
798 I915_WRITE(BLC_PWM_PCH_CTL1
, pch_ctl1
);
799 POSTING_READ(BLC_PWM_PCH_CTL1
);
800 I915_WRITE(BLC_PWM_PCH_CTL1
, pch_ctl1
| BLM_PCH_PWM_ENABLE
);
802 /* This won't stick until the above enable. */
803 intel_panel_actually_set_backlight(connector
, panel
->backlight
.level
);
806 static void pch_enable_backlight(struct intel_connector
*connector
)
808 struct drm_device
*dev
= connector
->base
.dev
;
809 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
810 struct intel_panel
*panel
= &connector
->panel
;
811 enum pipe pipe
= intel_get_pipe_from_connector(connector
);
812 enum transcoder cpu_transcoder
=
813 intel_pipe_to_cpu_transcoder(dev_priv
, pipe
);
814 u32 cpu_ctl2
, pch_ctl1
, pch_ctl2
;
816 cpu_ctl2
= I915_READ(BLC_PWM_CPU_CTL2
);
817 if (cpu_ctl2
& BLM_PWM_ENABLE
) {
818 DRM_DEBUG_KMS("cpu backlight already enabled\n");
819 cpu_ctl2
&= ~BLM_PWM_ENABLE
;
820 I915_WRITE(BLC_PWM_CPU_CTL2
, cpu_ctl2
);
823 pch_ctl1
= I915_READ(BLC_PWM_PCH_CTL1
);
824 if (pch_ctl1
& BLM_PCH_PWM_ENABLE
) {
825 DRM_DEBUG_KMS("pch backlight already enabled\n");
826 pch_ctl1
&= ~BLM_PCH_PWM_ENABLE
;
827 I915_WRITE(BLC_PWM_PCH_CTL1
, pch_ctl1
);
830 if (cpu_transcoder
== TRANSCODER_EDP
)
831 cpu_ctl2
= BLM_TRANSCODER_EDP
;
833 cpu_ctl2
= BLM_PIPE(cpu_transcoder
);
834 I915_WRITE(BLC_PWM_CPU_CTL2
, cpu_ctl2
);
835 POSTING_READ(BLC_PWM_CPU_CTL2
);
836 I915_WRITE(BLC_PWM_CPU_CTL2
, cpu_ctl2
| BLM_PWM_ENABLE
);
838 /* This won't stick until the above enable. */
839 intel_panel_actually_set_backlight(connector
, panel
->backlight
.level
);
841 pch_ctl2
= panel
->backlight
.max
<< 16;
842 I915_WRITE(BLC_PWM_PCH_CTL2
, pch_ctl2
);
845 if (panel
->backlight
.active_low_pwm
)
846 pch_ctl1
|= BLM_PCH_POLARITY
;
848 I915_WRITE(BLC_PWM_PCH_CTL1
, pch_ctl1
);
849 POSTING_READ(BLC_PWM_PCH_CTL1
);
850 I915_WRITE(BLC_PWM_PCH_CTL1
, pch_ctl1
| BLM_PCH_PWM_ENABLE
);
853 static void i9xx_enable_backlight(struct intel_connector
*connector
)
855 struct drm_device
*dev
= connector
->base
.dev
;
856 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
857 struct intel_panel
*panel
= &connector
->panel
;
860 ctl
= I915_READ(BLC_PWM_CTL
);
861 if (ctl
& BACKLIGHT_DUTY_CYCLE_MASK_PNV
) {
862 DRM_DEBUG_KMS("backlight already enabled\n");
863 I915_WRITE(BLC_PWM_CTL
, 0);
866 freq
= panel
->backlight
.max
;
867 if (panel
->backlight
.combination_mode
)
871 if (panel
->backlight
.combination_mode
)
872 ctl
|= BLM_LEGACY_MODE
;
873 if (IS_PINEVIEW(dev
) && panel
->backlight
.active_low_pwm
)
874 ctl
|= BLM_POLARITY_PNV
;
876 I915_WRITE(BLC_PWM_CTL
, ctl
);
877 POSTING_READ(BLC_PWM_CTL
);
879 /* XXX: combine this into above write? */
880 intel_panel_actually_set_backlight(connector
, panel
->backlight
.level
);
883 static void i965_enable_backlight(struct intel_connector
*connector
)
885 struct drm_device
*dev
= connector
->base
.dev
;
886 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
887 struct intel_panel
*panel
= &connector
->panel
;
888 enum pipe pipe
= intel_get_pipe_from_connector(connector
);
891 ctl2
= I915_READ(BLC_PWM_CTL2
);
892 if (ctl2
& BLM_PWM_ENABLE
) {
893 DRM_DEBUG_KMS("backlight already enabled\n");
894 ctl2
&= ~BLM_PWM_ENABLE
;
895 I915_WRITE(BLC_PWM_CTL2
, ctl2
);
898 freq
= panel
->backlight
.max
;
899 if (panel
->backlight
.combination_mode
)
903 I915_WRITE(BLC_PWM_CTL
, ctl
);
905 ctl2
= BLM_PIPE(pipe
);
906 if (panel
->backlight
.combination_mode
)
907 ctl2
|= BLM_COMBINATION_MODE
;
908 if (panel
->backlight
.active_low_pwm
)
909 ctl2
|= BLM_POLARITY_I965
;
910 I915_WRITE(BLC_PWM_CTL2
, ctl2
);
911 POSTING_READ(BLC_PWM_CTL2
);
912 I915_WRITE(BLC_PWM_CTL2
, ctl2
| BLM_PWM_ENABLE
);
914 intel_panel_actually_set_backlight(connector
, panel
->backlight
.level
);
917 static void vlv_enable_backlight(struct intel_connector
*connector
)
919 struct drm_device
*dev
= connector
->base
.dev
;
920 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
921 struct intel_panel
*panel
= &connector
->panel
;
922 enum pipe pipe
= intel_get_pipe_from_connector(connector
);
925 if (WARN_ON(pipe
!= PIPE_A
&& pipe
!= PIPE_B
))
928 ctl2
= I915_READ(VLV_BLC_PWM_CTL2(pipe
));
929 if (ctl2
& BLM_PWM_ENABLE
) {
930 DRM_DEBUG_KMS("backlight already enabled\n");
931 ctl2
&= ~BLM_PWM_ENABLE
;
932 I915_WRITE(VLV_BLC_PWM_CTL2(pipe
), ctl2
);
935 ctl
= panel
->backlight
.max
<< 16;
936 I915_WRITE(VLV_BLC_PWM_CTL(pipe
), ctl
);
938 /* XXX: combine this into above write? */
939 intel_panel_actually_set_backlight(connector
, panel
->backlight
.level
);
942 if (panel
->backlight
.active_low_pwm
)
943 ctl2
|= BLM_POLARITY_I965
;
944 I915_WRITE(VLV_BLC_PWM_CTL2(pipe
), ctl2
);
945 POSTING_READ(VLV_BLC_PWM_CTL2(pipe
));
946 I915_WRITE(VLV_BLC_PWM_CTL2(pipe
), ctl2
| BLM_PWM_ENABLE
);
949 void intel_panel_enable_backlight(struct intel_connector
*connector
)
951 struct drm_device
*dev
= connector
->base
.dev
;
952 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
953 struct intel_panel
*panel
= &connector
->panel
;
954 enum pipe pipe
= intel_get_pipe_from_connector(connector
);
956 if (!panel
->backlight
.present
)
959 DRM_DEBUG_KMS("pipe %c\n", pipe_name(pipe
));
961 mutex_lock(&dev_priv
->backlight_lock
);
963 WARN_ON(panel
->backlight
.max
== 0);
965 if (panel
->backlight
.level
<= panel
->backlight
.min
) {
966 panel
->backlight
.level
= panel
->backlight
.max
;
967 if (panel
->backlight
.device
)
968 panel
->backlight
.device
->props
.brightness
=
969 scale_hw_to_user(connector
,
970 panel
->backlight
.level
,
971 panel
->backlight
.device
->props
.max_brightness
);
974 dev_priv
->display
.enable_backlight(connector
);
975 panel
->backlight
.enabled
= true;
976 if (panel
->backlight
.device
)
977 panel
->backlight
.device
->props
.power
= FB_BLANK_UNBLANK
;
979 mutex_unlock(&dev_priv
->backlight_lock
);
982 #if IS_ENABLED(CONFIG_BACKLIGHT_CLASS_DEVICE)
983 static int intel_backlight_device_update_status(struct backlight_device
*bd
)
985 struct intel_connector
*connector
= bl_get_data(bd
);
986 struct intel_panel
*panel
= &connector
->panel
;
987 struct drm_device
*dev
= connector
->base
.dev
;
989 drm_modeset_lock(&dev
->mode_config
.connection_mutex
, NULL
);
990 DRM_DEBUG_KMS("updating intel_backlight, brightness=%d/%d\n",
991 bd
->props
.brightness
, bd
->props
.max_brightness
);
992 intel_panel_set_backlight(connector
, bd
->props
.brightness
,
993 bd
->props
.max_brightness
);
996 * Allow flipping bl_power as a sub-state of enabled. Sadly the
997 * backlight class device does not make it easy to to differentiate
998 * between callbacks for brightness and bl_power, so our backlight_power
999 * callback needs to take this into account.
1001 if (panel
->backlight
.enabled
) {
1002 if (panel
->backlight_power
) {
1003 bool enable
= bd
->props
.power
== FB_BLANK_UNBLANK
&&
1004 bd
->props
.brightness
!= 0;
1005 panel
->backlight_power(connector
, enable
);
1008 bd
->props
.power
= FB_BLANK_POWERDOWN
;
1011 drm_modeset_unlock(&dev
->mode_config
.connection_mutex
);
1015 static int intel_backlight_device_get_brightness(struct backlight_device
*bd
)
1017 struct intel_connector
*connector
= bl_get_data(bd
);
1018 struct drm_device
*dev
= connector
->base
.dev
;
1019 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
1023 intel_runtime_pm_get(dev_priv
);
1024 drm_modeset_lock(&dev
->mode_config
.connection_mutex
, NULL
);
1026 hw_level
= intel_panel_get_backlight(connector
);
1027 ret
= scale_hw_to_user(connector
, hw_level
, bd
->props
.max_brightness
);
1029 drm_modeset_unlock(&dev
->mode_config
.connection_mutex
);
1030 intel_runtime_pm_put(dev_priv
);
1035 static const struct backlight_ops intel_backlight_device_ops
= {
1036 .update_status
= intel_backlight_device_update_status
,
1037 .get_brightness
= intel_backlight_device_get_brightness
,
1040 static int intel_backlight_device_register(struct intel_connector
*connector
)
1042 struct intel_panel
*panel
= &connector
->panel
;
1043 struct backlight_properties props
;
1045 if (WARN_ON(panel
->backlight
.device
))
1048 if (!panel
->backlight
.present
)
1051 WARN_ON(panel
->backlight
.max
== 0);
1053 memset(&props
, 0, sizeof(props
));
1054 props
.type
= BACKLIGHT_RAW
;
1057 * Note: Everything should work even if the backlight device max
1058 * presented to the userspace is arbitrarily chosen.
1060 props
.max_brightness
= panel
->backlight
.max
;
1061 props
.brightness
= scale_hw_to_user(connector
,
1062 panel
->backlight
.level
,
1063 props
.max_brightness
);
1065 if (panel
->backlight
.enabled
)
1066 props
.power
= FB_BLANK_UNBLANK
;
1068 props
.power
= FB_BLANK_POWERDOWN
;
1071 * Note: using the same name independent of the connector prevents
1072 * registration of multiple backlight devices in the driver.
1074 panel
->backlight
.device
=
1075 backlight_device_register("intel_backlight",
1076 connector
->base
.kdev
,
1078 &intel_backlight_device_ops
, &props
);
1080 if (IS_ERR(panel
->backlight
.device
)) {
1081 DRM_ERROR("Failed to register backlight: %ld\n",
1082 PTR_ERR(panel
->backlight
.device
));
1083 panel
->backlight
.device
= NULL
;
1087 DRM_DEBUG_KMS("Connector %s backlight sysfs interface registered\n",
1088 connector
->base
.name
);
1093 static void intel_backlight_device_unregister(struct intel_connector
*connector
)
1095 struct intel_panel
*panel
= &connector
->panel
;
1097 if (panel
->backlight
.device
) {
1098 backlight_device_unregister(panel
->backlight
.device
);
1099 panel
->backlight
.device
= NULL
;
1102 #else /* CONFIG_BACKLIGHT_CLASS_DEVICE */
1103 static int intel_backlight_device_register(struct intel_connector
*connector
)
1107 static void intel_backlight_device_unregister(struct intel_connector
*connector
)
1110 #endif /* CONFIG_BACKLIGHT_CLASS_DEVICE */
1113 * Note: The setup hooks can't assume pipe is set!
1115 * XXX: Query mode clock or hardware clock and program PWM modulation frequency
1116 * appropriately when it's 0. Use VBT and/or sane defaults.
1118 static u32
get_backlight_min_vbt(struct intel_connector
*connector
)
1120 struct drm_device
*dev
= connector
->base
.dev
;
1121 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
1122 struct intel_panel
*panel
= &connector
->panel
;
1125 WARN_ON(panel
->backlight
.max
== 0);
1128 * XXX: If the vbt value is 255, it makes min equal to max, which leads
1129 * to problems. There are such machines out there. Either our
1130 * interpretation is wrong or the vbt has bogus data. Or both. Safeguard
1131 * against this by letting the minimum be at most (arbitrarily chosen)
1134 min
= clamp_t(int, dev_priv
->vbt
.backlight
.min_brightness
, 0, 64);
1135 if (min
!= dev_priv
->vbt
.backlight
.min_brightness
) {
1136 DRM_DEBUG_KMS("clamping VBT min backlight %d/255 to %d/255\n",
1137 dev_priv
->vbt
.backlight
.min_brightness
, min
);
1140 /* vbt value is a coefficient in range [0..255] */
1141 return scale(min
, 0, 255, 0, panel
->backlight
.max
);
1144 static int bdw_setup_backlight(struct intel_connector
*connector
, enum pipe unused
)
1146 struct drm_device
*dev
= connector
->base
.dev
;
1147 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
1148 struct intel_panel
*panel
= &connector
->panel
;
1149 u32 pch_ctl1
, pch_ctl2
, val
;
1151 pch_ctl1
= I915_READ(BLC_PWM_PCH_CTL1
);
1152 panel
->backlight
.active_low_pwm
= pch_ctl1
& BLM_PCH_POLARITY
;
1154 pch_ctl2
= I915_READ(BLC_PWM_PCH_CTL2
);
1155 panel
->backlight
.max
= pch_ctl2
>> 16;
1156 if (!panel
->backlight
.max
)
1159 panel
->backlight
.min
= get_backlight_min_vbt(connector
);
1161 val
= bdw_get_backlight(connector
);
1162 panel
->backlight
.level
= intel_panel_compute_brightness(connector
, val
);
1164 panel
->backlight
.enabled
= (pch_ctl1
& BLM_PCH_PWM_ENABLE
) &&
1165 panel
->backlight
.level
!= 0;
1170 static int pch_setup_backlight(struct intel_connector
*connector
, enum pipe unused
)
1172 struct drm_device
*dev
= connector
->base
.dev
;
1173 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
1174 struct intel_panel
*panel
= &connector
->panel
;
1175 u32 cpu_ctl2
, pch_ctl1
, pch_ctl2
, val
;
1177 pch_ctl1
= I915_READ(BLC_PWM_PCH_CTL1
);
1178 panel
->backlight
.active_low_pwm
= pch_ctl1
& BLM_PCH_POLARITY
;
1180 pch_ctl2
= I915_READ(BLC_PWM_PCH_CTL2
);
1181 panel
->backlight
.max
= pch_ctl2
>> 16;
1182 if (!panel
->backlight
.max
)
1185 panel
->backlight
.min
= get_backlight_min_vbt(connector
);
1187 val
= pch_get_backlight(connector
);
1188 panel
->backlight
.level
= intel_panel_compute_brightness(connector
, val
);
1190 cpu_ctl2
= I915_READ(BLC_PWM_CPU_CTL2
);
1191 panel
->backlight
.enabled
= (cpu_ctl2
& BLM_PWM_ENABLE
) &&
1192 (pch_ctl1
& BLM_PCH_PWM_ENABLE
) && panel
->backlight
.level
!= 0;
1197 static int i9xx_setup_backlight(struct intel_connector
*connector
, enum pipe unused
)
1199 struct drm_device
*dev
= connector
->base
.dev
;
1200 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
1201 struct intel_panel
*panel
= &connector
->panel
;
1204 ctl
= I915_READ(BLC_PWM_CTL
);
1206 if (IS_GEN2(dev
) || IS_I915GM(dev
) || IS_I945GM(dev
))
1207 panel
->backlight
.combination_mode
= ctl
& BLM_LEGACY_MODE
;
1209 if (IS_PINEVIEW(dev
))
1210 panel
->backlight
.active_low_pwm
= ctl
& BLM_POLARITY_PNV
;
1212 panel
->backlight
.max
= ctl
>> 17;
1213 if (panel
->backlight
.combination_mode
)
1214 panel
->backlight
.max
*= 0xff;
1216 if (!panel
->backlight
.max
)
1219 panel
->backlight
.min
= get_backlight_min_vbt(connector
);
1221 val
= i9xx_get_backlight(connector
);
1222 panel
->backlight
.level
= intel_panel_compute_brightness(connector
, val
);
1224 panel
->backlight
.enabled
= panel
->backlight
.level
!= 0;
1229 static int i965_setup_backlight(struct intel_connector
*connector
, enum pipe unused
)
1231 struct drm_device
*dev
= connector
->base
.dev
;
1232 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
1233 struct intel_panel
*panel
= &connector
->panel
;
1236 ctl2
= I915_READ(BLC_PWM_CTL2
);
1237 panel
->backlight
.combination_mode
= ctl2
& BLM_COMBINATION_MODE
;
1238 panel
->backlight
.active_low_pwm
= ctl2
& BLM_POLARITY_I965
;
1240 ctl
= I915_READ(BLC_PWM_CTL
);
1241 panel
->backlight
.max
= ctl
>> 16;
1242 if (panel
->backlight
.combination_mode
)
1243 panel
->backlight
.max
*= 0xff;
1245 if (!panel
->backlight
.max
)
1248 panel
->backlight
.min
= get_backlight_min_vbt(connector
);
1250 val
= i9xx_get_backlight(connector
);
1251 panel
->backlight
.level
= intel_panel_compute_brightness(connector
, val
);
1253 panel
->backlight
.enabled
= (ctl2
& BLM_PWM_ENABLE
) &&
1254 panel
->backlight
.level
!= 0;
1259 static int vlv_setup_backlight(struct intel_connector
*connector
, enum pipe pipe
)
1261 struct drm_device
*dev
= connector
->base
.dev
;
1262 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
1263 struct intel_panel
*panel
= &connector
->panel
;
1267 for_each_pipe(dev_priv
, p
) {
1268 u32 cur_val
= I915_READ(VLV_BLC_PWM_CTL(p
));
1270 /* Skip if the modulation freq is already set */
1271 if (cur_val
& ~BACKLIGHT_DUTY_CYCLE_MASK
)
1274 cur_val
&= BACKLIGHT_DUTY_CYCLE_MASK
;
1275 I915_WRITE(VLV_BLC_PWM_CTL(p
), (0xf42 << 16) |
1279 if (WARN_ON(pipe
!= PIPE_A
&& pipe
!= PIPE_B
))
1282 ctl2
= I915_READ(VLV_BLC_PWM_CTL2(pipe
));
1283 panel
->backlight
.active_low_pwm
= ctl2
& BLM_POLARITY_I965
;
1285 ctl
= I915_READ(VLV_BLC_PWM_CTL(pipe
));
1286 panel
->backlight
.max
= ctl
>> 16;
1287 if (!panel
->backlight
.max
)
1290 panel
->backlight
.min
= get_backlight_min_vbt(connector
);
1292 val
= _vlv_get_backlight(dev
, pipe
);
1293 panel
->backlight
.level
= intel_panel_compute_brightness(connector
, val
);
1295 panel
->backlight
.enabled
= (ctl2
& BLM_PWM_ENABLE
) &&
1296 panel
->backlight
.level
!= 0;
1301 int intel_panel_setup_backlight(struct drm_connector
*connector
, enum pipe pipe
)
1303 struct drm_device
*dev
= connector
->dev
;
1304 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
1305 struct intel_connector
*intel_connector
= to_intel_connector(connector
);
1306 struct intel_panel
*panel
= &intel_connector
->panel
;
1309 if (!dev_priv
->vbt
.backlight
.present
) {
1310 if (dev_priv
->quirks
& QUIRK_BACKLIGHT_PRESENT
) {
1311 DRM_DEBUG_KMS("no backlight present per VBT, but present per quirk\n");
1313 DRM_DEBUG_KMS("no backlight present per VBT\n");
1318 /* set level and max in panel struct */
1319 mutex_lock(&dev_priv
->backlight_lock
);
1320 ret
= dev_priv
->display
.setup_backlight(intel_connector
, pipe
);
1321 mutex_unlock(&dev_priv
->backlight_lock
);
1324 DRM_DEBUG_KMS("failed to setup backlight for connector %s\n",
1329 panel
->backlight
.present
= true;
1331 DRM_DEBUG_KMS("Connector %s backlight initialized, %s, brightness %u/%u\n",
1333 panel
->backlight
.enabled
? "enabled" : "disabled",
1334 panel
->backlight
.level
, panel
->backlight
.max
);
1339 void intel_panel_destroy_backlight(struct drm_connector
*connector
)
1341 struct intel_connector
*intel_connector
= to_intel_connector(connector
);
1342 struct intel_panel
*panel
= &intel_connector
->panel
;
1344 panel
->backlight
.present
= false;
1347 /* Set up chip specific backlight functions */
1348 void intel_panel_init_backlight_funcs(struct drm_device
*dev
)
1350 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
1352 if (IS_BROADWELL(dev
) || (INTEL_INFO(dev
)->gen
>= 9)) {
1353 dev_priv
->display
.setup_backlight
= bdw_setup_backlight
;
1354 dev_priv
->display
.enable_backlight
= bdw_enable_backlight
;
1355 dev_priv
->display
.disable_backlight
= pch_disable_backlight
;
1356 dev_priv
->display
.set_backlight
= bdw_set_backlight
;
1357 dev_priv
->display
.get_backlight
= bdw_get_backlight
;
1358 } else if (HAS_PCH_SPLIT(dev
)) {
1359 dev_priv
->display
.setup_backlight
= pch_setup_backlight
;
1360 dev_priv
->display
.enable_backlight
= pch_enable_backlight
;
1361 dev_priv
->display
.disable_backlight
= pch_disable_backlight
;
1362 dev_priv
->display
.set_backlight
= pch_set_backlight
;
1363 dev_priv
->display
.get_backlight
= pch_get_backlight
;
1364 } else if (IS_VALLEYVIEW(dev
)) {
1365 dev_priv
->display
.setup_backlight
= vlv_setup_backlight
;
1366 dev_priv
->display
.enable_backlight
= vlv_enable_backlight
;
1367 dev_priv
->display
.disable_backlight
= vlv_disable_backlight
;
1368 dev_priv
->display
.set_backlight
= vlv_set_backlight
;
1369 dev_priv
->display
.get_backlight
= vlv_get_backlight
;
1370 } else if (IS_GEN4(dev
)) {
1371 dev_priv
->display
.setup_backlight
= i965_setup_backlight
;
1372 dev_priv
->display
.enable_backlight
= i965_enable_backlight
;
1373 dev_priv
->display
.disable_backlight
= i965_disable_backlight
;
1374 dev_priv
->display
.set_backlight
= i9xx_set_backlight
;
1375 dev_priv
->display
.get_backlight
= i9xx_get_backlight
;
1377 dev_priv
->display
.setup_backlight
= i9xx_setup_backlight
;
1378 dev_priv
->display
.enable_backlight
= i9xx_enable_backlight
;
1379 dev_priv
->display
.disable_backlight
= i9xx_disable_backlight
;
1380 dev_priv
->display
.set_backlight
= i9xx_set_backlight
;
1381 dev_priv
->display
.get_backlight
= i9xx_get_backlight
;
1385 int intel_panel_init(struct intel_panel
*panel
,
1386 struct drm_display_mode
*fixed_mode
,
1387 struct drm_display_mode
*downclock_mode
)
1389 panel
->fixed_mode
= fixed_mode
;
1390 panel
->downclock_mode
= downclock_mode
;
1395 void intel_panel_fini(struct intel_panel
*panel
)
1397 struct intel_connector
*intel_connector
=
1398 container_of(panel
, struct intel_connector
, panel
);
1400 if (panel
->fixed_mode
)
1401 drm_mode_destroy(intel_connector
->base
.dev
, panel
->fixed_mode
);
1403 if (panel
->downclock_mode
)
1404 drm_mode_destroy(intel_connector
->base
.dev
,
1405 panel
->downclock_mode
);
1408 void intel_backlight_register(struct drm_device
*dev
)
1410 struct intel_connector
*connector
;
1412 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, base
.head
)
1413 intel_backlight_device_register(connector
);
1416 void intel_backlight_unregister(struct drm_device
*dev
)
1418 struct intel_connector
*connector
;
1420 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, base
.head
)
1421 intel_backlight_device_unregister(connector
);