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 #include <linux/kernel.h>
32 #include <linux/moduleparam.h>
33 #include <linux/pwm.h>
34 #include "intel_drv.h"
36 #define CRC_PMIC_PWM_PERIOD_NS 21333
39 intel_fixed_panel_mode(const struct drm_display_mode
*fixed_mode
,
40 struct drm_display_mode
*adjusted_mode
)
42 drm_mode_copy(adjusted_mode
, fixed_mode
);
44 drm_mode_set_crtcinfo(adjusted_mode
, 0);
48 * intel_find_panel_downclock - find the reduced downclock for LVDS in EDID
50 * @fixed_mode : panel native mode
51 * @connector: LVDS/eDP connector
53 * Return downclock_avail
54 * Find the reduced downclock for LVDS/eDP in EDID.
56 struct drm_display_mode
*
57 intel_find_panel_downclock(struct drm_device
*dev
,
58 struct drm_display_mode
*fixed_mode
,
59 struct drm_connector
*connector
)
61 struct drm_display_mode
*scan
, *tmp_mode
;
64 temp_downclock
= fixed_mode
->clock
;
67 list_for_each_entry(scan
, &connector
->probed_modes
, head
) {
69 * If one mode has the same resolution with the fixed_panel
70 * mode while they have the different refresh rate, it means
71 * that the reduced downclock is found. In such
72 * case we can set the different FPx0/1 to dynamically select
73 * between low and high frequency.
75 if (scan
->hdisplay
== fixed_mode
->hdisplay
&&
76 scan
->hsync_start
== fixed_mode
->hsync_start
&&
77 scan
->hsync_end
== fixed_mode
->hsync_end
&&
78 scan
->htotal
== fixed_mode
->htotal
&&
79 scan
->vdisplay
== fixed_mode
->vdisplay
&&
80 scan
->vsync_start
== fixed_mode
->vsync_start
&&
81 scan
->vsync_end
== fixed_mode
->vsync_end
&&
82 scan
->vtotal
== fixed_mode
->vtotal
) {
83 if (scan
->clock
< temp_downclock
) {
85 * The downclock is already found. But we
86 * expect to find the lower downclock.
88 temp_downclock
= scan
->clock
;
94 if (temp_downclock
< fixed_mode
->clock
)
95 return drm_mode_duplicate(dev
, tmp_mode
);
100 /* adjusted_mode has been preset to be the panel's fixed mode */
102 intel_pch_panel_fitting(struct intel_crtc
*intel_crtc
,
103 struct intel_crtc_state
*pipe_config
,
106 struct drm_display_mode
*adjusted_mode
;
107 int x
, y
, width
, height
;
109 adjusted_mode
= &pipe_config
->base
.adjusted_mode
;
111 x
= y
= width
= height
= 0;
113 /* Native modes don't need fitting */
114 if (adjusted_mode
->hdisplay
== pipe_config
->pipe_src_w
&&
115 adjusted_mode
->vdisplay
== pipe_config
->pipe_src_h
)
118 switch (fitting_mode
) {
119 case DRM_MODE_SCALE_CENTER
:
120 width
= pipe_config
->pipe_src_w
;
121 height
= pipe_config
->pipe_src_h
;
122 x
= (adjusted_mode
->hdisplay
- width
+ 1)/2;
123 y
= (adjusted_mode
->vdisplay
- height
+ 1)/2;
126 case DRM_MODE_SCALE_ASPECT
:
127 /* Scale but preserve the aspect ratio */
129 u32 scaled_width
= adjusted_mode
->hdisplay
130 * pipe_config
->pipe_src_h
;
131 u32 scaled_height
= pipe_config
->pipe_src_w
132 * adjusted_mode
->vdisplay
;
133 if (scaled_width
> scaled_height
) { /* pillar */
134 width
= scaled_height
/ pipe_config
->pipe_src_h
;
137 x
= (adjusted_mode
->hdisplay
- width
+ 1) / 2;
139 height
= adjusted_mode
->vdisplay
;
140 } else if (scaled_width
< scaled_height
) { /* letter */
141 height
= scaled_width
/ pipe_config
->pipe_src_w
;
144 y
= (adjusted_mode
->vdisplay
- height
+ 1) / 2;
146 width
= adjusted_mode
->hdisplay
;
149 width
= adjusted_mode
->hdisplay
;
150 height
= adjusted_mode
->vdisplay
;
155 case DRM_MODE_SCALE_FULLSCREEN
:
157 width
= adjusted_mode
->hdisplay
;
158 height
= adjusted_mode
->vdisplay
;
162 WARN(1, "bad panel fit mode: %d\n", fitting_mode
);
167 pipe_config
->pch_pfit
.pos
= (x
<< 16) | y
;
168 pipe_config
->pch_pfit
.size
= (width
<< 16) | height
;
169 pipe_config
->pch_pfit
.enabled
= pipe_config
->pch_pfit
.size
!= 0;
173 centre_horizontally(struct drm_display_mode
*mode
,
176 u32 border
, sync_pos
, blank_width
, sync_width
;
178 /* keep the hsync and hblank widths constant */
179 sync_width
= mode
->crtc_hsync_end
- mode
->crtc_hsync_start
;
180 blank_width
= mode
->crtc_hblank_end
- mode
->crtc_hblank_start
;
181 sync_pos
= (blank_width
- sync_width
+ 1) / 2;
183 border
= (mode
->hdisplay
- width
+ 1) / 2;
184 border
+= border
& 1; /* make the border even */
186 mode
->crtc_hdisplay
= width
;
187 mode
->crtc_hblank_start
= width
+ border
;
188 mode
->crtc_hblank_end
= mode
->crtc_hblank_start
+ blank_width
;
190 mode
->crtc_hsync_start
= mode
->crtc_hblank_start
+ sync_pos
;
191 mode
->crtc_hsync_end
= mode
->crtc_hsync_start
+ sync_width
;
195 centre_vertically(struct drm_display_mode
*mode
,
198 u32 border
, sync_pos
, blank_width
, sync_width
;
200 /* keep the vsync and vblank widths constant */
201 sync_width
= mode
->crtc_vsync_end
- mode
->crtc_vsync_start
;
202 blank_width
= mode
->crtc_vblank_end
- mode
->crtc_vblank_start
;
203 sync_pos
= (blank_width
- sync_width
+ 1) / 2;
205 border
= (mode
->vdisplay
- height
+ 1) / 2;
207 mode
->crtc_vdisplay
= height
;
208 mode
->crtc_vblank_start
= height
+ border
;
209 mode
->crtc_vblank_end
= mode
->crtc_vblank_start
+ blank_width
;
211 mode
->crtc_vsync_start
= mode
->crtc_vblank_start
+ sync_pos
;
212 mode
->crtc_vsync_end
= mode
->crtc_vsync_start
+ sync_width
;
215 static inline u32
panel_fitter_scaling(u32 source
, u32 target
)
218 * Floating point operation is not supported. So the FACTOR
219 * is defined, which can avoid the floating point computation
220 * when calculating the panel ratio.
223 #define FACTOR (1 << ACCURACY)
224 u32 ratio
= source
* FACTOR
/ target
;
225 return (FACTOR
* ratio
+ FACTOR
/2) / FACTOR
;
228 static void i965_scale_aspect(struct intel_crtc_state
*pipe_config
,
231 struct drm_display_mode
*adjusted_mode
= &pipe_config
->base
.adjusted_mode
;
232 u32 scaled_width
= adjusted_mode
->hdisplay
*
233 pipe_config
->pipe_src_h
;
234 u32 scaled_height
= pipe_config
->pipe_src_w
*
235 adjusted_mode
->vdisplay
;
237 /* 965+ is easy, it does everything in hw */
238 if (scaled_width
> scaled_height
)
239 *pfit_control
|= PFIT_ENABLE
|
241 else if (scaled_width
< scaled_height
)
242 *pfit_control
|= PFIT_ENABLE
|
244 else if (adjusted_mode
->hdisplay
!= pipe_config
->pipe_src_w
)
245 *pfit_control
|= PFIT_ENABLE
| PFIT_SCALING_AUTO
;
248 static void i9xx_scale_aspect(struct intel_crtc_state
*pipe_config
,
249 u32
*pfit_control
, u32
*pfit_pgm_ratios
,
252 struct drm_display_mode
*adjusted_mode
= &pipe_config
->base
.adjusted_mode
;
253 u32 scaled_width
= adjusted_mode
->hdisplay
*
254 pipe_config
->pipe_src_h
;
255 u32 scaled_height
= pipe_config
->pipe_src_w
*
256 adjusted_mode
->vdisplay
;
260 * For earlier chips we have to calculate the scaling
261 * ratio by hand and program it into the
262 * PFIT_PGM_RATIO register
264 if (scaled_width
> scaled_height
) { /* pillar */
265 centre_horizontally(adjusted_mode
,
267 pipe_config
->pipe_src_h
);
269 *border
= LVDS_BORDER_ENABLE
;
270 if (pipe_config
->pipe_src_h
!= adjusted_mode
->vdisplay
) {
271 bits
= panel_fitter_scaling(pipe_config
->pipe_src_h
,
272 adjusted_mode
->vdisplay
);
274 *pfit_pgm_ratios
|= (bits
<< PFIT_HORIZ_SCALE_SHIFT
|
275 bits
<< PFIT_VERT_SCALE_SHIFT
);
276 *pfit_control
|= (PFIT_ENABLE
|
277 VERT_INTERP_BILINEAR
|
278 HORIZ_INTERP_BILINEAR
);
280 } else if (scaled_width
< scaled_height
) { /* letter */
281 centre_vertically(adjusted_mode
,
283 pipe_config
->pipe_src_w
);
285 *border
= LVDS_BORDER_ENABLE
;
286 if (pipe_config
->pipe_src_w
!= adjusted_mode
->hdisplay
) {
287 bits
= panel_fitter_scaling(pipe_config
->pipe_src_w
,
288 adjusted_mode
->hdisplay
);
290 *pfit_pgm_ratios
|= (bits
<< PFIT_HORIZ_SCALE_SHIFT
|
291 bits
<< PFIT_VERT_SCALE_SHIFT
);
292 *pfit_control
|= (PFIT_ENABLE
|
293 VERT_INTERP_BILINEAR
|
294 HORIZ_INTERP_BILINEAR
);
297 /* Aspects match, Let hw scale both directions */
298 *pfit_control
|= (PFIT_ENABLE
|
299 VERT_AUTO_SCALE
| HORIZ_AUTO_SCALE
|
300 VERT_INTERP_BILINEAR
|
301 HORIZ_INTERP_BILINEAR
);
305 void intel_gmch_panel_fitting(struct intel_crtc
*intel_crtc
,
306 struct intel_crtc_state
*pipe_config
,
309 struct drm_device
*dev
= intel_crtc
->base
.dev
;
310 u32 pfit_control
= 0, pfit_pgm_ratios
= 0, border
= 0;
311 struct drm_display_mode
*adjusted_mode
;
313 adjusted_mode
= &pipe_config
->base
.adjusted_mode
;
315 /* Native modes don't need fitting */
316 if (adjusted_mode
->hdisplay
== pipe_config
->pipe_src_w
&&
317 adjusted_mode
->vdisplay
== pipe_config
->pipe_src_h
)
320 switch (fitting_mode
) {
321 case DRM_MODE_SCALE_CENTER
:
323 * For centered modes, we have to calculate border widths &
324 * heights and modify the values programmed into the CRTC.
326 centre_horizontally(adjusted_mode
, pipe_config
->pipe_src_w
);
327 centre_vertically(adjusted_mode
, pipe_config
->pipe_src_h
);
328 border
= LVDS_BORDER_ENABLE
;
330 case DRM_MODE_SCALE_ASPECT
:
331 /* Scale but preserve the aspect ratio */
332 if (INTEL_INFO(dev
)->gen
>= 4)
333 i965_scale_aspect(pipe_config
, &pfit_control
);
335 i9xx_scale_aspect(pipe_config
, &pfit_control
,
336 &pfit_pgm_ratios
, &border
);
338 case DRM_MODE_SCALE_FULLSCREEN
:
340 * Full scaling, even if it changes the aspect ratio.
341 * Fortunately this is all done for us in hw.
343 if (pipe_config
->pipe_src_h
!= adjusted_mode
->vdisplay
||
344 pipe_config
->pipe_src_w
!= adjusted_mode
->hdisplay
) {
345 pfit_control
|= PFIT_ENABLE
;
346 if (INTEL_INFO(dev
)->gen
>= 4)
347 pfit_control
|= PFIT_SCALING_AUTO
;
349 pfit_control
|= (VERT_AUTO_SCALE
|
350 VERT_INTERP_BILINEAR
|
352 HORIZ_INTERP_BILINEAR
);
356 WARN(1, "bad panel fit mode: %d\n", fitting_mode
);
360 /* 965+ wants fuzzy fitting */
361 /* FIXME: handle multiple panels by failing gracefully */
362 if (INTEL_INFO(dev
)->gen
>= 4)
363 pfit_control
|= ((intel_crtc
->pipe
<< PFIT_PIPE_SHIFT
) |
367 if ((pfit_control
& PFIT_ENABLE
) == 0) {
372 /* Make sure pre-965 set dither correctly for 18bpp panels. */
373 if (INTEL_INFO(dev
)->gen
< 4 && pipe_config
->pipe_bpp
== 18)
374 pfit_control
|= PANEL_8TO6_DITHER_ENABLE
;
376 pipe_config
->gmch_pfit
.control
= pfit_control
;
377 pipe_config
->gmch_pfit
.pgm_ratios
= pfit_pgm_ratios
;
378 pipe_config
->gmch_pfit
.lvds_border_bits
= border
;
381 enum drm_connector_status
382 intel_panel_detect(struct drm_device
*dev
)
384 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
386 /* Assume that the BIOS does not lie through the OpRegion... */
387 if (!i915
.panel_ignore_lid
&& dev_priv
->opregion
.lid_state
) {
388 return ioread32(dev_priv
->opregion
.lid_state
) & 0x1 ?
389 connector_status_connected
:
390 connector_status_disconnected
;
393 switch (i915
.panel_ignore_lid
) {
395 return connector_status_connected
;
397 return connector_status_disconnected
;
399 return connector_status_unknown
;
404 * scale - scale values from one range to another
406 * @source_val: value in range [@source_min..@source_max]
408 * Return @source_val in range [@source_min..@source_max] scaled to range
409 * [@target_min..@target_max].
411 static uint32_t scale(uint32_t source_val
,
412 uint32_t source_min
, uint32_t source_max
,
413 uint32_t target_min
, uint32_t target_max
)
417 WARN_ON(source_min
> source_max
);
418 WARN_ON(target_min
> target_max
);
421 source_val
= clamp(source_val
, source_min
, source_max
);
423 /* avoid overflows */
424 target_val
= DIV_ROUND_CLOSEST_ULL((uint64_t)(source_val
- source_min
) *
425 (target_max
- target_min
), source_max
- source_min
);
426 target_val
+= target_min
;
431 /* Scale user_level in range [0..user_max] to [hw_min..hw_max]. */
432 static inline u32
scale_user_to_hw(struct intel_connector
*connector
,
433 u32 user_level
, u32 user_max
)
435 struct intel_panel
*panel
= &connector
->panel
;
437 return scale(user_level
, 0, user_max
,
438 panel
->backlight
.min
, panel
->backlight
.max
);
441 /* Scale user_level in range [0..user_max] to [0..hw_max], clamping the result
442 * to [hw_min..hw_max]. */
443 static inline u32
clamp_user_to_hw(struct intel_connector
*connector
,
444 u32 user_level
, u32 user_max
)
446 struct intel_panel
*panel
= &connector
->panel
;
449 hw_level
= scale(user_level
, 0, user_max
, 0, panel
->backlight
.max
);
450 hw_level
= clamp(hw_level
, panel
->backlight
.min
, panel
->backlight
.max
);
455 /* Scale hw_level in range [hw_min..hw_max] to [0..user_max]. */
456 static inline u32
scale_hw_to_user(struct intel_connector
*connector
,
457 u32 hw_level
, u32 user_max
)
459 struct intel_panel
*panel
= &connector
->panel
;
461 return scale(hw_level
, panel
->backlight
.min
, panel
->backlight
.max
,
465 static u32
intel_panel_compute_brightness(struct intel_connector
*connector
,
468 struct drm_device
*dev
= connector
->base
.dev
;
469 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
470 struct intel_panel
*panel
= &connector
->panel
;
472 WARN_ON(panel
->backlight
.max
== 0);
474 if (i915
.invert_brightness
< 0)
477 if (i915
.invert_brightness
> 0 ||
478 dev_priv
->quirks
& QUIRK_INVERT_BRIGHTNESS
) {
479 return panel
->backlight
.max
- val
;
485 static u32
bdw_get_backlight(struct intel_connector
*connector
)
487 struct drm_device
*dev
= connector
->base
.dev
;
488 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
490 return I915_READ(BLC_PWM_PCH_CTL2
) & BACKLIGHT_DUTY_CYCLE_MASK
;
493 static u32
pch_get_backlight(struct intel_connector
*connector
)
495 struct drm_device
*dev
= connector
->base
.dev
;
496 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
498 return I915_READ(BLC_PWM_CPU_CTL
) & BACKLIGHT_DUTY_CYCLE_MASK
;
501 static u32
i9xx_get_backlight(struct intel_connector
*connector
)
503 struct drm_device
*dev
= connector
->base
.dev
;
504 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
505 struct intel_panel
*panel
= &connector
->panel
;
508 val
= I915_READ(BLC_PWM_CTL
) & BACKLIGHT_DUTY_CYCLE_MASK
;
509 if (INTEL_INFO(dev
)->gen
< 4)
512 if (panel
->backlight
.combination_mode
) {
515 pci_read_config_byte(dev
->pdev
, PCI_LBPC
, &lbpc
);
522 static u32
_vlv_get_backlight(struct drm_device
*dev
, enum i915_pipe pipe
)
524 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
526 if (WARN_ON(pipe
!= PIPE_A
&& pipe
!= PIPE_B
))
529 return I915_READ(VLV_BLC_PWM_CTL(pipe
)) & BACKLIGHT_DUTY_CYCLE_MASK
;
532 static u32
vlv_get_backlight(struct intel_connector
*connector
)
534 struct drm_device
*dev
= connector
->base
.dev
;
535 enum i915_pipe pipe
= intel_get_pipe_from_connector(connector
);
537 return _vlv_get_backlight(dev
, pipe
);
540 static u32
bxt_get_backlight(struct intel_connector
*connector
)
542 struct drm_device
*dev
= connector
->base
.dev
;
543 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
545 return I915_READ(BXT_BLC_PWM_DUTY1
);
548 static u32
pwm_get_backlight(struct intel_connector
*connector
)
550 struct intel_panel
*panel
= &connector
->panel
;
553 duty_ns
= pwm_get_duty_cycle(panel
->backlight
.pwm
);
554 return DIV_ROUND_UP(duty_ns
* 100, CRC_PMIC_PWM_PERIOD_NS
);
558 static u32
intel_panel_get_backlight(struct intel_connector
*connector
)
560 struct drm_device
*dev
= connector
->base
.dev
;
561 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
562 struct intel_panel
*panel
= &connector
->panel
;
565 mutex_lock(&dev_priv
->backlight_lock
);
567 if (panel
->backlight
.enabled
) {
568 val
= dev_priv
->display
.get_backlight(connector
);
569 val
= intel_panel_compute_brightness(connector
, val
);
572 mutex_unlock(&dev_priv
->backlight_lock
);
574 DRM_DEBUG_DRIVER("get backlight PWM = %d\n", val
);
579 static void bdw_set_backlight(struct intel_connector
*connector
, u32 level
)
581 struct drm_device
*dev
= connector
->base
.dev
;
582 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
583 u32 val
= I915_READ(BLC_PWM_PCH_CTL2
) & ~BACKLIGHT_DUTY_CYCLE_MASK
;
584 I915_WRITE(BLC_PWM_PCH_CTL2
, val
| level
);
587 static void pch_set_backlight(struct intel_connector
*connector
, u32 level
)
589 struct drm_device
*dev
= connector
->base
.dev
;
590 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
593 tmp
= I915_READ(BLC_PWM_CPU_CTL
) & ~BACKLIGHT_DUTY_CYCLE_MASK
;
594 I915_WRITE(BLC_PWM_CPU_CTL
, tmp
| level
);
597 static void i9xx_set_backlight(struct intel_connector
*connector
, u32 level
)
599 struct drm_device
*dev
= connector
->base
.dev
;
600 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
601 struct intel_panel
*panel
= &connector
->panel
;
604 WARN_ON(panel
->backlight
.max
== 0);
606 if (panel
->backlight
.combination_mode
) {
609 lbpc
= level
* 0xfe / panel
->backlight
.max
+ 1;
611 pci_write_config_byte(dev
->pdev
, PCI_LBPC
, lbpc
);
615 mask
= BACKLIGHT_DUTY_CYCLE_MASK
;
618 mask
= BACKLIGHT_DUTY_CYCLE_MASK_PNV
;
621 tmp
= I915_READ(BLC_PWM_CTL
) & ~mask
;
622 I915_WRITE(BLC_PWM_CTL
, tmp
| level
);
625 static void vlv_set_backlight(struct intel_connector
*connector
, u32 level
)
627 struct drm_device
*dev
= connector
->base
.dev
;
628 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
629 enum i915_pipe pipe
= intel_get_pipe_from_connector(connector
);
632 if (WARN_ON(pipe
!= PIPE_A
&& pipe
!= PIPE_B
))
635 tmp
= I915_READ(VLV_BLC_PWM_CTL(pipe
)) & ~BACKLIGHT_DUTY_CYCLE_MASK
;
636 I915_WRITE(VLV_BLC_PWM_CTL(pipe
), tmp
| level
);
639 static void bxt_set_backlight(struct intel_connector
*connector
, u32 level
)
641 struct drm_device
*dev
= connector
->base
.dev
;
642 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
644 I915_WRITE(BXT_BLC_PWM_DUTY1
, level
);
647 static void pwm_set_backlight(struct intel_connector
*connector
, u32 level
)
649 struct intel_panel
*panel
= &connector
->panel
;
650 int duty_ns
= DIV_ROUND_UP(level
* CRC_PMIC_PWM_PERIOD_NS
, 100);
652 pwm_config(panel
->backlight
.pwm
, duty_ns
, CRC_PMIC_PWM_PERIOD_NS
);
656 intel_panel_actually_set_backlight(struct intel_connector
*connector
, u32 level
)
658 struct drm_device
*dev
= connector
->base
.dev
;
659 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
661 DRM_DEBUG_DRIVER("set backlight PWM = %d\n", level
);
663 level
= intel_panel_compute_brightness(connector
, level
);
664 dev_priv
->display
.set_backlight(connector
, level
);
667 /* set backlight brightness to level in range [0..max], scaling wrt hw min */
668 static void intel_panel_set_backlight(struct intel_connector
*connector
,
669 u32 user_level
, u32 user_max
)
671 struct drm_device
*dev
= connector
->base
.dev
;
672 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
673 struct intel_panel
*panel
= &connector
->panel
;
676 if (!panel
->backlight
.present
)
679 mutex_lock(&dev_priv
->backlight_lock
);
681 WARN_ON(panel
->backlight
.max
== 0);
683 hw_level
= scale_user_to_hw(connector
, user_level
, user_max
);
684 panel
->backlight
.level
= hw_level
;
686 if (panel
->backlight
.enabled
)
687 intel_panel_actually_set_backlight(connector
, hw_level
);
689 mutex_unlock(&dev_priv
->backlight_lock
);
692 /* set backlight brightness to level in range [0..max], assuming hw min is
695 void intel_panel_set_backlight_acpi(struct intel_connector
*connector
,
696 u32 user_level
, u32 user_max
)
698 struct drm_device
*dev
= connector
->base
.dev
;
699 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
700 struct intel_panel
*panel
= &connector
->panel
;
701 enum i915_pipe pipe
= intel_get_pipe_from_connector(connector
);
705 * INVALID_PIPE may occur during driver init because
706 * connection_mutex isn't held across the entire backlight
707 * setup + modeset readout, and the BIOS can issue the
708 * requests at any time.
710 if (!panel
->backlight
.present
|| pipe
== INVALID_PIPE
)
713 mutex_lock(&dev_priv
->backlight_lock
);
715 WARN_ON(panel
->backlight
.max
== 0);
717 hw_level
= clamp_user_to_hw(connector
, user_level
, user_max
);
718 panel
->backlight
.level
= hw_level
;
720 if (panel
->backlight
.device
)
721 panel
->backlight
.device
->props
.brightness
=
722 scale_hw_to_user(connector
,
723 panel
->backlight
.level
,
724 panel
->backlight
.device
->props
.max_brightness
);
726 if (panel
->backlight
.enabled
)
727 intel_panel_actually_set_backlight(connector
, hw_level
);
729 mutex_unlock(&dev_priv
->backlight_lock
);
732 static void pch_disable_backlight(struct intel_connector
*connector
)
734 struct drm_device
*dev
= connector
->base
.dev
;
735 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
738 intel_panel_actually_set_backlight(connector
, 0);
740 tmp
= I915_READ(BLC_PWM_CPU_CTL2
);
741 I915_WRITE(BLC_PWM_CPU_CTL2
, tmp
& ~BLM_PWM_ENABLE
);
743 tmp
= I915_READ(BLC_PWM_PCH_CTL1
);
744 I915_WRITE(BLC_PWM_PCH_CTL1
, tmp
& ~BLM_PCH_PWM_ENABLE
);
747 static void i9xx_disable_backlight(struct intel_connector
*connector
)
749 intel_panel_actually_set_backlight(connector
, 0);
752 static void i965_disable_backlight(struct intel_connector
*connector
)
754 struct drm_device
*dev
= connector
->base
.dev
;
755 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
758 intel_panel_actually_set_backlight(connector
, 0);
760 tmp
= I915_READ(BLC_PWM_CTL2
);
761 I915_WRITE(BLC_PWM_CTL2
, tmp
& ~BLM_PWM_ENABLE
);
764 static void vlv_disable_backlight(struct intel_connector
*connector
)
766 struct drm_device
*dev
= connector
->base
.dev
;
767 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
768 enum i915_pipe pipe
= intel_get_pipe_from_connector(connector
);
771 if (WARN_ON(pipe
!= PIPE_A
&& pipe
!= PIPE_B
))
774 intel_panel_actually_set_backlight(connector
, 0);
776 tmp
= I915_READ(VLV_BLC_PWM_CTL2(pipe
));
777 I915_WRITE(VLV_BLC_PWM_CTL2(pipe
), tmp
& ~BLM_PWM_ENABLE
);
780 static void bxt_disable_backlight(struct intel_connector
*connector
)
782 struct drm_device
*dev
= connector
->base
.dev
;
783 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
786 intel_panel_actually_set_backlight(connector
, 0);
788 tmp
= I915_READ(BXT_BLC_PWM_CTL1
);
789 I915_WRITE(BXT_BLC_PWM_CTL1
, tmp
& ~BXT_BLC_PWM_ENABLE
);
792 static void pwm_disable_backlight(struct intel_connector
*connector
)
794 struct intel_panel
*panel
= &connector
->panel
;
796 /* Disable the backlight */
797 pwm_config(panel
->backlight
.pwm
, 0, CRC_PMIC_PWM_PERIOD_NS
);
798 usleep_range(2000, 3000);
799 pwm_disable(panel
->backlight
.pwm
);
802 void intel_panel_disable_backlight(struct intel_connector
*connector
)
804 struct drm_device
*dev
= connector
->base
.dev
;
805 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
806 struct intel_panel
*panel
= &connector
->panel
;
808 if (!panel
->backlight
.present
)
812 * Do not disable backlight on the vgaswitcheroo path. When switching
813 * away from i915, the other client may depend on i915 to handle the
814 * backlight. This will leave the backlight on unnecessarily when
815 * another client is not activated.
817 if (dev
->switch_power_state
== DRM_SWITCH_POWER_CHANGING
) {
818 DRM_DEBUG_DRIVER("Skipping backlight disable on vga switch\n");
822 mutex_lock(&dev_priv
->backlight_lock
);
824 if (panel
->backlight
.device
)
825 panel
->backlight
.device
->props
.power
= FB_BLANK_POWERDOWN
;
826 panel
->backlight
.enabled
= false;
827 dev_priv
->display
.disable_backlight(connector
);
829 mutex_unlock(&dev_priv
->backlight_lock
);
832 static void bdw_enable_backlight(struct intel_connector
*connector
)
834 struct drm_device
*dev
= connector
->base
.dev
;
835 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
836 struct intel_panel
*panel
= &connector
->panel
;
837 u32 pch_ctl1
, pch_ctl2
;
839 pch_ctl1
= I915_READ(BLC_PWM_PCH_CTL1
);
840 if (pch_ctl1
& BLM_PCH_PWM_ENABLE
) {
841 DRM_DEBUG_KMS("pch backlight already enabled\n");
842 pch_ctl1
&= ~BLM_PCH_PWM_ENABLE
;
843 I915_WRITE(BLC_PWM_PCH_CTL1
, pch_ctl1
);
846 pch_ctl2
= panel
->backlight
.max
<< 16;
847 I915_WRITE(BLC_PWM_PCH_CTL2
, pch_ctl2
);
850 if (panel
->backlight
.active_low_pwm
)
851 pch_ctl1
|= BLM_PCH_POLARITY
;
853 /* After LPT, override is the default. */
854 if (HAS_PCH_LPT(dev_priv
))
855 pch_ctl1
|= BLM_PCH_OVERRIDE_ENABLE
;
857 I915_WRITE(BLC_PWM_PCH_CTL1
, pch_ctl1
);
858 POSTING_READ(BLC_PWM_PCH_CTL1
);
859 I915_WRITE(BLC_PWM_PCH_CTL1
, pch_ctl1
| BLM_PCH_PWM_ENABLE
);
861 /* This won't stick until the above enable. */
862 intel_panel_actually_set_backlight(connector
, panel
->backlight
.level
);
865 static void pch_enable_backlight(struct intel_connector
*connector
)
867 struct drm_device
*dev
= connector
->base
.dev
;
868 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
869 struct intel_panel
*panel
= &connector
->panel
;
870 enum i915_pipe pipe
= intel_get_pipe_from_connector(connector
);
871 enum transcoder cpu_transcoder
=
872 intel_pipe_to_cpu_transcoder(dev_priv
, pipe
);
873 u32 cpu_ctl2
, pch_ctl1
, pch_ctl2
;
875 cpu_ctl2
= I915_READ(BLC_PWM_CPU_CTL2
);
876 if (cpu_ctl2
& BLM_PWM_ENABLE
) {
877 DRM_DEBUG_KMS("cpu backlight already enabled\n");
878 cpu_ctl2
&= ~BLM_PWM_ENABLE
;
879 I915_WRITE(BLC_PWM_CPU_CTL2
, cpu_ctl2
);
882 pch_ctl1
= I915_READ(BLC_PWM_PCH_CTL1
);
883 if (pch_ctl1
& BLM_PCH_PWM_ENABLE
) {
884 DRM_DEBUG_KMS("pch backlight already enabled\n");
885 pch_ctl1
&= ~BLM_PCH_PWM_ENABLE
;
886 I915_WRITE(BLC_PWM_PCH_CTL1
, pch_ctl1
);
889 if (cpu_transcoder
== TRANSCODER_EDP
)
890 cpu_ctl2
= BLM_TRANSCODER_EDP
;
892 cpu_ctl2
= BLM_PIPE(cpu_transcoder
);
893 I915_WRITE(BLC_PWM_CPU_CTL2
, cpu_ctl2
);
894 POSTING_READ(BLC_PWM_CPU_CTL2
);
895 I915_WRITE(BLC_PWM_CPU_CTL2
, cpu_ctl2
| BLM_PWM_ENABLE
);
897 /* This won't stick until the above enable. */
898 intel_panel_actually_set_backlight(connector
, panel
->backlight
.level
);
900 pch_ctl2
= panel
->backlight
.max
<< 16;
901 I915_WRITE(BLC_PWM_PCH_CTL2
, pch_ctl2
);
904 if (panel
->backlight
.active_low_pwm
)
905 pch_ctl1
|= BLM_PCH_POLARITY
;
907 I915_WRITE(BLC_PWM_PCH_CTL1
, pch_ctl1
);
908 POSTING_READ(BLC_PWM_PCH_CTL1
);
909 I915_WRITE(BLC_PWM_PCH_CTL1
, pch_ctl1
| BLM_PCH_PWM_ENABLE
);
912 static void i9xx_enable_backlight(struct intel_connector
*connector
)
914 struct drm_device
*dev
= connector
->base
.dev
;
915 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
916 struct intel_panel
*panel
= &connector
->panel
;
919 ctl
= I915_READ(BLC_PWM_CTL
);
920 if (ctl
& BACKLIGHT_DUTY_CYCLE_MASK_PNV
) {
921 DRM_DEBUG_KMS("backlight already enabled\n");
922 I915_WRITE(BLC_PWM_CTL
, 0);
925 freq
= panel
->backlight
.max
;
926 if (panel
->backlight
.combination_mode
)
930 if (panel
->backlight
.combination_mode
)
931 ctl
|= BLM_LEGACY_MODE
;
932 if (IS_PINEVIEW(dev
) && panel
->backlight
.active_low_pwm
)
933 ctl
|= BLM_POLARITY_PNV
;
935 I915_WRITE(BLC_PWM_CTL
, ctl
);
936 POSTING_READ(BLC_PWM_CTL
);
938 /* XXX: combine this into above write? */
939 intel_panel_actually_set_backlight(connector
, panel
->backlight
.level
);
942 * Needed to enable backlight on some 855gm models. BLC_HIST_CTL is
943 * 855gm only, but checking for gen2 is safe, as 855gm is the only gen2
944 * that has backlight.
947 I915_WRITE(BLC_HIST_CTL
, BLM_HISTOGRAM_ENABLE
);
950 static void i965_enable_backlight(struct intel_connector
*connector
)
952 struct drm_device
*dev
= connector
->base
.dev
;
953 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
954 struct intel_panel
*panel
= &connector
->panel
;
955 enum i915_pipe pipe
= intel_get_pipe_from_connector(connector
);
958 ctl2
= I915_READ(BLC_PWM_CTL2
);
959 if (ctl2
& BLM_PWM_ENABLE
) {
960 DRM_DEBUG_KMS("backlight already enabled\n");
961 ctl2
&= ~BLM_PWM_ENABLE
;
962 I915_WRITE(BLC_PWM_CTL2
, ctl2
);
965 freq
= panel
->backlight
.max
;
966 if (panel
->backlight
.combination_mode
)
970 I915_WRITE(BLC_PWM_CTL
, ctl
);
972 ctl2
= BLM_PIPE(pipe
);
973 if (panel
->backlight
.combination_mode
)
974 ctl2
|= BLM_COMBINATION_MODE
;
975 if (panel
->backlight
.active_low_pwm
)
976 ctl2
|= BLM_POLARITY_I965
;
977 I915_WRITE(BLC_PWM_CTL2
, ctl2
);
978 POSTING_READ(BLC_PWM_CTL2
);
979 I915_WRITE(BLC_PWM_CTL2
, ctl2
| BLM_PWM_ENABLE
);
981 intel_panel_actually_set_backlight(connector
, panel
->backlight
.level
);
984 static void vlv_enable_backlight(struct intel_connector
*connector
)
986 struct drm_device
*dev
= connector
->base
.dev
;
987 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
988 struct intel_panel
*panel
= &connector
->panel
;
989 enum i915_pipe pipe
= intel_get_pipe_from_connector(connector
);
992 if (WARN_ON(pipe
!= PIPE_A
&& pipe
!= PIPE_B
))
995 ctl2
= I915_READ(VLV_BLC_PWM_CTL2(pipe
));
996 if (ctl2
& BLM_PWM_ENABLE
) {
997 DRM_DEBUG_KMS("backlight already enabled\n");
998 ctl2
&= ~BLM_PWM_ENABLE
;
999 I915_WRITE(VLV_BLC_PWM_CTL2(pipe
), ctl2
);
1002 ctl
= panel
->backlight
.max
<< 16;
1003 I915_WRITE(VLV_BLC_PWM_CTL(pipe
), ctl
);
1005 /* XXX: combine this into above write? */
1006 intel_panel_actually_set_backlight(connector
, panel
->backlight
.level
);
1009 if (panel
->backlight
.active_low_pwm
)
1010 ctl2
|= BLM_POLARITY_I965
;
1011 I915_WRITE(VLV_BLC_PWM_CTL2(pipe
), ctl2
);
1012 POSTING_READ(VLV_BLC_PWM_CTL2(pipe
));
1013 I915_WRITE(VLV_BLC_PWM_CTL2(pipe
), ctl2
| BLM_PWM_ENABLE
);
1016 static void bxt_enable_backlight(struct intel_connector
*connector
)
1018 struct drm_device
*dev
= connector
->base
.dev
;
1019 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
1020 struct intel_panel
*panel
= &connector
->panel
;
1023 pwm_ctl
= I915_READ(BXT_BLC_PWM_CTL1
);
1024 if (pwm_ctl
& BXT_BLC_PWM_ENABLE
) {
1025 DRM_DEBUG_KMS("backlight already enabled\n");
1026 pwm_ctl
&= ~BXT_BLC_PWM_ENABLE
;
1027 I915_WRITE(BXT_BLC_PWM_CTL1
, pwm_ctl
);
1030 I915_WRITE(BXT_BLC_PWM_FREQ1
, panel
->backlight
.max
);
1032 intel_panel_actually_set_backlight(connector
, panel
->backlight
.level
);
1035 if (panel
->backlight
.active_low_pwm
)
1036 pwm_ctl
|= BXT_BLC_PWM_POLARITY
;
1038 I915_WRITE(BXT_BLC_PWM_CTL1
, pwm_ctl
);
1039 POSTING_READ(BXT_BLC_PWM_CTL1
);
1040 I915_WRITE(BXT_BLC_PWM_CTL1
, pwm_ctl
| BXT_BLC_PWM_ENABLE
);
1043 static void pwm_enable_backlight(struct intel_connector
*connector
)
1045 struct intel_panel
*panel
= &connector
->panel
;
1047 pwm_enable(panel
->backlight
.pwm
);
1048 intel_panel_actually_set_backlight(connector
, panel
->backlight
.level
);
1051 void intel_panel_enable_backlight(struct intel_connector
*connector
)
1053 struct drm_device
*dev
= connector
->base
.dev
;
1054 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
1055 struct intel_panel
*panel
= &connector
->panel
;
1056 enum i915_pipe pipe
= intel_get_pipe_from_connector(connector
);
1058 if (!panel
->backlight
.present
)
1061 DRM_DEBUG_KMS("pipe %c\n", pipe_name(pipe
));
1063 mutex_lock(&dev_priv
->backlight_lock
);
1065 WARN_ON(panel
->backlight
.max
== 0);
1067 if (panel
->backlight
.level
<= panel
->backlight
.min
) {
1068 panel
->backlight
.level
= panel
->backlight
.max
;
1069 if (panel
->backlight
.device
)
1070 panel
->backlight
.device
->props
.brightness
=
1071 scale_hw_to_user(connector
,
1072 panel
->backlight
.level
,
1073 panel
->backlight
.device
->props
.max_brightness
);
1076 dev_priv
->display
.enable_backlight(connector
);
1077 panel
->backlight
.enabled
= true;
1078 if (panel
->backlight
.device
)
1079 panel
->backlight
.device
->props
.power
= FB_BLANK_UNBLANK
;
1081 mutex_unlock(&dev_priv
->backlight_lock
);
1084 #if IS_ENABLED(CONFIG_BACKLIGHT_CLASS_DEVICE)
1085 static int intel_backlight_device_update_status(struct backlight_device
*bd
)
1088 struct intel_connector
*connector
= bl_get_data(bd
);
1089 struct intel_panel
*panel
= &connector
->panel
;
1090 struct drm_device
*dev
= connector
->base
.dev
;
1092 drm_modeset_lock(&dev
->mode_config
.connection_mutex
, NULL
);
1093 DRM_DEBUG_KMS("updating intel_backlight, brightness=%d/%d\n",
1094 bd
->props
.brightness
, bd
->props
.max_brightness
);
1095 intel_panel_set_backlight(connector
, bd
->props
.brightness
,
1096 bd
->props
.max_brightness
);
1099 * Allow flipping bl_power as a sub-state of enabled. Sadly the
1100 * backlight class device does not make it easy to to differentiate
1101 * between callbacks for brightness and bl_power, so our backlight_power
1102 * callback needs to take this into account.
1104 if (panel
->backlight
.enabled
) {
1105 if (panel
->backlight_power
) {
1106 bool enable
= bd
->props
.power
== FB_BLANK_UNBLANK
&&
1107 bd
->props
.brightness
!= 0;
1108 panel
->backlight_power(connector
, enable
);
1111 bd
->props
.power
= FB_BLANK_POWERDOWN
;
1114 drm_modeset_unlock(&dev
->mode_config
.connection_mutex
);
1119 static int intel_backlight_device_get_brightness(struct backlight_device
*bd
)
1122 struct intel_connector
*connector
= bl_get_data(bd
);
1123 struct drm_device
*dev
= connector
->base
.dev
;
1124 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
1128 intel_runtime_pm_get(dev_priv
);
1129 drm_modeset_lock(&dev
->mode_config
.connection_mutex
, NULL
);
1131 hw_level
= intel_panel_get_backlight(connector
);
1132 ret
= scale_hw_to_user(connector
, hw_level
, bd
->props
.max_brightness
);
1134 drm_modeset_unlock(&dev
->mode_config
.connection_mutex
);
1135 intel_runtime_pm_put(dev_priv
);
1142 static const struct backlight_ops intel_backlight_device_ops
= {
1143 .update_status
= intel_backlight_device_update_status
,
1144 .get_brightness
= intel_backlight_device_get_brightness
,
1147 static int intel_backlight_device_register(struct intel_connector
*connector
)
1149 struct intel_panel
*panel
= &connector
->panel
;
1150 struct backlight_properties props
;
1152 if (WARN_ON(panel
->backlight
.device
))
1155 if (!panel
->backlight
.present
)
1158 WARN_ON(panel
->backlight
.max
== 0);
1160 memset(&props
, 0, sizeof(props
));
1161 props
.type
= BACKLIGHT_RAW
;
1164 * Note: Everything should work even if the backlight device max
1165 * presented to the userspace is arbitrarily chosen.
1167 props
.max_brightness
= panel
->backlight
.max
;
1168 props
.brightness
= scale_hw_to_user(connector
,
1169 panel
->backlight
.level
,
1170 props
.max_brightness
);
1172 if (panel
->backlight
.enabled
)
1173 props
.power
= FB_BLANK_UNBLANK
;
1175 props
.power
= FB_BLANK_POWERDOWN
;
1178 * Note: using the same name independent of the connector prevents
1179 * registration of multiple backlight devices in the driver.
1182 panel
->backlight
.device
=
1183 backlight_device_register("intel_backlight",
1184 connector
->base
.kdev
,
1186 &intel_backlight_device_ops
, &props
);
1188 if (IS_ERR(panel
->backlight
.device
)) {
1189 DRM_ERROR("Failed to register backlight: %ld\n",
1190 PTR_ERR(panel
->backlight
.device
));
1191 panel
->backlight
.device
= NULL
;
1195 DRM_DEBUG_KMS("Connector %s backlight sysfs interface registered\n",
1196 connector
->base
.name
);
1202 static void intel_backlight_device_unregister(struct intel_connector
*connector
)
1205 struct intel_panel
*panel
= &connector
->panel
;
1207 if (panel
->backlight
.device
) {
1208 backlight_device_unregister(panel
->backlight
.device
);
1209 panel
->backlight
.device
= NULL
;
1213 #else /* CONFIG_BACKLIGHT_CLASS_DEVICE */
1214 static int intel_backlight_device_register(struct intel_connector
*connector
)
1218 static void intel_backlight_device_unregister(struct intel_connector
*connector
)
1221 #endif /* CONFIG_BACKLIGHT_CLASS_DEVICE */
1224 * Note: The setup hooks can't assume pipe is set!
1226 * XXX: Query mode clock or hardware clock and program PWM modulation frequency
1227 * appropriately when it's 0. Use VBT and/or sane defaults.
1229 static u32
get_backlight_min_vbt(struct intel_connector
*connector
)
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 WARN_ON(panel
->backlight
.max
== 0);
1239 * XXX: If the vbt value is 255, it makes min equal to max, which leads
1240 * to problems. There are such machines out there. Either our
1241 * interpretation is wrong or the vbt has bogus data. Or both. Safeguard
1242 * against this by letting the minimum be at most (arbitrarily chosen)
1245 min
= clamp_t(int, dev_priv
->vbt
.backlight
.min_brightness
, 0, 64);
1246 if (min
!= dev_priv
->vbt
.backlight
.min_brightness
) {
1247 DRM_DEBUG_KMS("clamping VBT min backlight %d/255 to %d/255\n",
1248 dev_priv
->vbt
.backlight
.min_brightness
, min
);
1251 /* vbt value is a coefficient in range [0..255] */
1252 return scale(min
, 0, 255, 0, panel
->backlight
.max
);
1255 static int bdw_setup_backlight(struct intel_connector
*connector
, enum i915_pipe unused
)
1257 struct drm_device
*dev
= connector
->base
.dev
;
1258 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
1259 struct intel_panel
*panel
= &connector
->panel
;
1260 u32 pch_ctl1
, pch_ctl2
, val
;
1262 pch_ctl1
= I915_READ(BLC_PWM_PCH_CTL1
);
1263 panel
->backlight
.active_low_pwm
= pch_ctl1
& BLM_PCH_POLARITY
;
1265 pch_ctl2
= I915_READ(BLC_PWM_PCH_CTL2
);
1266 panel
->backlight
.max
= pch_ctl2
>> 16;
1267 if (!panel
->backlight
.max
)
1270 panel
->backlight
.min
= get_backlight_min_vbt(connector
);
1272 val
= bdw_get_backlight(connector
);
1273 panel
->backlight
.level
= intel_panel_compute_brightness(connector
, val
);
1275 panel
->backlight
.enabled
= (pch_ctl1
& BLM_PCH_PWM_ENABLE
) &&
1276 panel
->backlight
.level
!= 0;
1281 static int pch_setup_backlight(struct intel_connector
*connector
, enum i915_pipe unused
)
1283 struct drm_device
*dev
= connector
->base
.dev
;
1284 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
1285 struct intel_panel
*panel
= &connector
->panel
;
1286 u32 cpu_ctl2
, pch_ctl1
, pch_ctl2
, val
;
1288 pch_ctl1
= I915_READ(BLC_PWM_PCH_CTL1
);
1289 panel
->backlight
.active_low_pwm
= pch_ctl1
& BLM_PCH_POLARITY
;
1291 pch_ctl2
= I915_READ(BLC_PWM_PCH_CTL2
);
1292 panel
->backlight
.max
= pch_ctl2
>> 16;
1293 if (!panel
->backlight
.max
)
1296 panel
->backlight
.min
= get_backlight_min_vbt(connector
);
1298 val
= pch_get_backlight(connector
);
1299 panel
->backlight
.level
= intel_panel_compute_brightness(connector
, val
);
1301 cpu_ctl2
= I915_READ(BLC_PWM_CPU_CTL2
);
1302 panel
->backlight
.enabled
= (cpu_ctl2
& BLM_PWM_ENABLE
) &&
1303 (pch_ctl1
& BLM_PCH_PWM_ENABLE
) && panel
->backlight
.level
!= 0;
1308 static int i9xx_setup_backlight(struct intel_connector
*connector
, enum i915_pipe unused
)
1310 struct drm_device
*dev
= connector
->base
.dev
;
1311 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
1312 struct intel_panel
*panel
= &connector
->panel
;
1315 ctl
= I915_READ(BLC_PWM_CTL
);
1317 if (IS_GEN2(dev
) || IS_I915GM(dev
) || IS_I945GM(dev
))
1318 panel
->backlight
.combination_mode
= ctl
& BLM_LEGACY_MODE
;
1320 if (IS_PINEVIEW(dev
))
1321 panel
->backlight
.active_low_pwm
= ctl
& BLM_POLARITY_PNV
;
1323 panel
->backlight
.max
= ctl
>> 17;
1324 if (panel
->backlight
.combination_mode
)
1325 panel
->backlight
.max
*= 0xff;
1327 if (!panel
->backlight
.max
)
1330 panel
->backlight
.min
= get_backlight_min_vbt(connector
);
1332 val
= i9xx_get_backlight(connector
);
1333 panel
->backlight
.level
= intel_panel_compute_brightness(connector
, val
);
1335 panel
->backlight
.enabled
= panel
->backlight
.level
!= 0;
1340 static int i965_setup_backlight(struct intel_connector
*connector
, enum i915_pipe unused
)
1342 struct drm_device
*dev
= connector
->base
.dev
;
1343 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
1344 struct intel_panel
*panel
= &connector
->panel
;
1347 ctl2
= I915_READ(BLC_PWM_CTL2
);
1348 panel
->backlight
.combination_mode
= ctl2
& BLM_COMBINATION_MODE
;
1349 panel
->backlight
.active_low_pwm
= ctl2
& BLM_POLARITY_I965
;
1351 ctl
= I915_READ(BLC_PWM_CTL
);
1352 panel
->backlight
.max
= ctl
>> 16;
1353 if (panel
->backlight
.combination_mode
)
1354 panel
->backlight
.max
*= 0xff;
1356 if (!panel
->backlight
.max
)
1359 panel
->backlight
.min
= get_backlight_min_vbt(connector
);
1361 val
= i9xx_get_backlight(connector
);
1362 panel
->backlight
.level
= intel_panel_compute_brightness(connector
, val
);
1364 panel
->backlight
.enabled
= (ctl2
& BLM_PWM_ENABLE
) &&
1365 panel
->backlight
.level
!= 0;
1370 static int vlv_setup_backlight(struct intel_connector
*connector
, enum i915_pipe pipe
)
1372 struct drm_device
*dev
= connector
->base
.dev
;
1373 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
1374 struct intel_panel
*panel
= &connector
->panel
;
1378 for_each_pipe(dev_priv
, p
) {
1379 u32 cur_val
= I915_READ(VLV_BLC_PWM_CTL(p
));
1381 /* Skip if the modulation freq is already set */
1382 if (cur_val
& ~BACKLIGHT_DUTY_CYCLE_MASK
)
1385 cur_val
&= BACKLIGHT_DUTY_CYCLE_MASK
;
1386 I915_WRITE(VLV_BLC_PWM_CTL(p
), (0xf42 << 16) |
1390 if (WARN_ON(pipe
!= PIPE_A
&& pipe
!= PIPE_B
))
1393 ctl2
= I915_READ(VLV_BLC_PWM_CTL2(pipe
));
1394 panel
->backlight
.active_low_pwm
= ctl2
& BLM_POLARITY_I965
;
1396 ctl
= I915_READ(VLV_BLC_PWM_CTL(pipe
));
1397 panel
->backlight
.max
= ctl
>> 16;
1398 if (!panel
->backlight
.max
)
1401 panel
->backlight
.min
= get_backlight_min_vbt(connector
);
1403 val
= _vlv_get_backlight(dev
, pipe
);
1404 panel
->backlight
.level
= intel_panel_compute_brightness(connector
, val
);
1406 panel
->backlight
.enabled
= (ctl2
& BLM_PWM_ENABLE
) &&
1407 panel
->backlight
.level
!= 0;
1412 #ifdef __DragonFly__
1414 * Read max backlight level
1417 sysctl_backlight_max(SYSCTL_HANDLER_ARGS
)
1420 struct intel_connector
*connector
= arg1
;
1421 struct drm_device
*dev
= connector
->base
.dev
;
1422 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
1423 struct intel_panel
*panel
= &connector
->panel
;
1425 mutex_lock(&dev_priv
->backlight_lock
);
1426 val
= panel
->backlight
.max
;
1427 mutex_unlock(&dev_priv
->backlight_lock
);
1429 err
= sysctl_handle_int(oidp
, &val
, 0, req
);
1434 * Read/write backlight level
1437 sysctl_backlight_handler(SYSCTL_HANDLER_ARGS
)
1439 struct intel_connector
*connector
= arg1
;
1440 struct drm_device
*dev
= connector
->base
.dev
;
1441 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
1442 struct intel_panel
*panel
= &connector
->panel
;
1444 u32 user_level
, max_brightness
;
1446 mutex_lock(&dev_priv
->backlight_lock
);
1447 max_brightness
= panel
->backlight
.max
;
1448 user_level
= scale_hw_to_user(connector
, panel
->backlight
.level
,
1450 mutex_unlock(&dev_priv
->backlight_lock
);
1453 err
= sysctl_handle_int(oidp
, &val
, 0, req
);
1454 if (err
!= 0 || req
->newptr
== NULL
) {
1458 if (val
!= user_level
&& val
>= 0 && val
<= max_brightness
) {
1459 drm_modeset_lock(&dev
->mode_config
.connection_mutex
, NULL
);
1460 intel_panel_set_backlight(arg1
, val
, max_brightness
);
1461 drm_modeset_unlock(&dev
->mode_config
.connection_mutex
);
1466 #endif /* __DragonFly__ */
1469 bxt_setup_backlight(struct intel_connector
*connector
, enum i915_pipe unused
)
1471 struct drm_device
*dev
= connector
->base
.dev
;
1472 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
1473 struct intel_panel
*panel
= &connector
->panel
;
1476 pwm_ctl
= I915_READ(BXT_BLC_PWM_CTL1
);
1477 panel
->backlight
.active_low_pwm
= pwm_ctl
& BXT_BLC_PWM_POLARITY
;
1479 panel
->backlight
.max
= I915_READ(BXT_BLC_PWM_FREQ1
);
1480 if (!panel
->backlight
.max
)
1483 val
= bxt_get_backlight(connector
);
1484 panel
->backlight
.level
= intel_panel_compute_brightness(connector
, val
);
1486 panel
->backlight
.enabled
= (pwm_ctl
& BXT_BLC_PWM_ENABLE
) &&
1487 panel
->backlight
.level
!= 0;
1492 static int pwm_setup_backlight(struct intel_connector
*connector
,
1493 enum i915_pipe pipe
)
1495 struct drm_device
*dev
= connector
->base
.dev
;
1496 struct intel_panel
*panel
= &connector
->panel
;
1499 /* Get the PWM chip for backlight control */
1500 panel
->backlight
.pwm
= pwm_get(dev
->dev
, "pwm_backlight");
1501 if (IS_ERR(panel
->backlight
.pwm
)) {
1502 DRM_ERROR("Failed to own the pwm chip\n");
1503 panel
->backlight
.pwm
= NULL
;
1507 retval
= pwm_config(panel
->backlight
.pwm
, CRC_PMIC_PWM_PERIOD_NS
,
1508 CRC_PMIC_PWM_PERIOD_NS
);
1510 DRM_ERROR("Failed to configure the pwm chip\n");
1511 pwm_put(panel
->backlight
.pwm
);
1512 panel
->backlight
.pwm
= NULL
;
1516 panel
->backlight
.min
= 0; /* 0% */
1517 panel
->backlight
.max
= 100; /* 100% */
1518 panel
->backlight
.level
= DIV_ROUND_UP(
1519 pwm_get_duty_cycle(panel
->backlight
.pwm
) * 100,
1520 CRC_PMIC_PWM_PERIOD_NS
);
1521 panel
->backlight
.enabled
= panel
->backlight
.level
!= 0;
1526 int intel_panel_setup_backlight(struct drm_connector
*connector
, enum i915_pipe pipe
)
1528 struct drm_device
*dev
= connector
->dev
;
1529 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
1530 struct intel_connector
*intel_connector
= to_intel_connector(connector
);
1531 struct intel_panel
*panel
= &intel_connector
->panel
;
1534 if (!dev_priv
->vbt
.backlight
.present
) {
1535 if (dev_priv
->quirks
& QUIRK_BACKLIGHT_PRESENT
) {
1536 DRM_DEBUG_KMS("no backlight present per VBT, but present per quirk\n");
1538 DRM_DEBUG_KMS("no backlight present per VBT\n");
1543 /* set level and max in panel struct */
1544 mutex_lock(&dev_priv
->backlight_lock
);
1545 ret
= dev_priv
->display
.setup_backlight(intel_connector
, pipe
);
1546 mutex_unlock(&dev_priv
->backlight_lock
);
1549 DRM_DEBUG_KMS("failed to setup backlight for connector %s\n",
1554 panel
->backlight
.present
= true;
1556 #ifdef __DragonFly__
1557 SYSCTL_ADD_PROC(&connector
->dev
->sysctl
->ctx
, &sysctl__hw_children
,
1558 OID_AUTO
, "backlight_max",
1559 CTLTYPE_INT
| CTLFLAG_RD
| CTLFLAG_ANYBODY
,
1560 connector
, sizeof(int),
1561 sysctl_backlight_max
,
1562 "I", "Max backlight level");
1563 SYSCTL_ADD_PROC(&connector
->dev
->sysctl
->ctx
, &sysctl__hw_children
,
1564 OID_AUTO
, "backlight_level",
1565 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_ANYBODY
,
1566 connector
, sizeof(int),
1567 sysctl_backlight_handler
,
1568 "I", "Backlight level");
1571 DRM_DEBUG_KMS("Connector %s backlight initialized, %s, brightness %u/%u\n",
1573 panel
->backlight
.enabled
? "enabled" : "disabled",
1574 panel
->backlight
.level
, panel
->backlight
.max
);
1579 void intel_panel_destroy_backlight(struct drm_connector
*connector
)
1581 struct intel_connector
*intel_connector
= to_intel_connector(connector
);
1582 struct intel_panel
*panel
= &intel_connector
->panel
;
1584 /* dispose of the pwm */
1585 if (panel
->backlight
.pwm
)
1586 pwm_put(panel
->backlight
.pwm
);
1588 panel
->backlight
.present
= false;
1591 /* Set up chip specific backlight functions */
1592 void intel_panel_init_backlight_funcs(struct drm_device
*dev
)
1594 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
1596 if (IS_BROXTON(dev
)) {
1597 dev_priv
->display
.setup_backlight
= bxt_setup_backlight
;
1598 dev_priv
->display
.enable_backlight
= bxt_enable_backlight
;
1599 dev_priv
->display
.disable_backlight
= bxt_disable_backlight
;
1600 dev_priv
->display
.set_backlight
= bxt_set_backlight
;
1601 dev_priv
->display
.get_backlight
= bxt_get_backlight
;
1602 } else if (IS_BROADWELL(dev
) || IS_SKYLAKE(dev
)) {
1603 dev_priv
->display
.setup_backlight
= bdw_setup_backlight
;
1604 dev_priv
->display
.enable_backlight
= bdw_enable_backlight
;
1605 dev_priv
->display
.disable_backlight
= pch_disable_backlight
;
1606 dev_priv
->display
.set_backlight
= bdw_set_backlight
;
1607 dev_priv
->display
.get_backlight
= bdw_get_backlight
;
1608 } else if (HAS_PCH_SPLIT(dev
)) {
1609 dev_priv
->display
.setup_backlight
= pch_setup_backlight
;
1610 dev_priv
->display
.enable_backlight
= pch_enable_backlight
;
1611 dev_priv
->display
.disable_backlight
= pch_disable_backlight
;
1612 dev_priv
->display
.set_backlight
= pch_set_backlight
;
1613 dev_priv
->display
.get_backlight
= pch_get_backlight
;
1614 } else if (IS_VALLEYVIEW(dev
)) {
1615 if (dev_priv
->vbt
.has_mipi
) {
1616 dev_priv
->display
.setup_backlight
= pwm_setup_backlight
;
1617 dev_priv
->display
.enable_backlight
= pwm_enable_backlight
;
1618 dev_priv
->display
.disable_backlight
= pwm_disable_backlight
;
1619 dev_priv
->display
.set_backlight
= pwm_set_backlight
;
1620 dev_priv
->display
.get_backlight
= pwm_get_backlight
;
1622 dev_priv
->display
.setup_backlight
= vlv_setup_backlight
;
1623 dev_priv
->display
.enable_backlight
= vlv_enable_backlight
;
1624 dev_priv
->display
.disable_backlight
= vlv_disable_backlight
;
1625 dev_priv
->display
.set_backlight
= vlv_set_backlight
;
1626 dev_priv
->display
.get_backlight
= vlv_get_backlight
;
1628 } else if (IS_GEN4(dev
)) {
1629 dev_priv
->display
.setup_backlight
= i965_setup_backlight
;
1630 dev_priv
->display
.enable_backlight
= i965_enable_backlight
;
1631 dev_priv
->display
.disable_backlight
= i965_disable_backlight
;
1632 dev_priv
->display
.set_backlight
= i9xx_set_backlight
;
1633 dev_priv
->display
.get_backlight
= i9xx_get_backlight
;
1635 dev_priv
->display
.setup_backlight
= i9xx_setup_backlight
;
1636 dev_priv
->display
.enable_backlight
= i9xx_enable_backlight
;
1637 dev_priv
->display
.disable_backlight
= i9xx_disable_backlight
;
1638 dev_priv
->display
.set_backlight
= i9xx_set_backlight
;
1639 dev_priv
->display
.get_backlight
= i9xx_get_backlight
;
1643 int intel_panel_init(struct intel_panel
*panel
,
1644 struct drm_display_mode
*fixed_mode
,
1645 struct drm_display_mode
*downclock_mode
)
1647 panel
->fixed_mode
= fixed_mode
;
1648 panel
->downclock_mode
= downclock_mode
;
1653 void intel_panel_fini(struct intel_panel
*panel
)
1655 struct intel_connector
*intel_connector
=
1656 container_of(panel
, struct intel_connector
, panel
);
1658 if (panel
->fixed_mode
)
1659 drm_mode_destroy(intel_connector
->base
.dev
, panel
->fixed_mode
);
1661 if (panel
->downclock_mode
)
1662 drm_mode_destroy(intel_connector
->base
.dev
,
1663 panel
->downclock_mode
);
1666 void intel_backlight_register(struct drm_device
*dev
)
1668 struct intel_connector
*connector
;
1670 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, base
.head
)
1671 intel_backlight_device_register(connector
);
1674 void intel_backlight_unregister(struct drm_device
*dev
)
1676 struct intel_connector
*connector
;
1678 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, base
.head
)
1679 intel_backlight_device_unregister(connector
);