2 * Copyright © 2006-2010 Intel Corporation
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.
31 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
33 #include <linux/kernel.h>
34 #include <linux/moduleparam.h>
35 #include <linux/pwm.h>
36 #include "intel_drv.h"
38 #define CRC_PMIC_PWM_PERIOD_NS 21333
41 intel_fixed_panel_mode(const struct drm_display_mode *fixed_mode,
42 struct drm_display_mode *adjusted_mode)
44 drm_mode_copy(adjusted_mode, fixed_mode);
46 drm_mode_set_crtcinfo(adjusted_mode, 0);
50 * intel_find_panel_downclock - find the reduced downclock for LVDS in EDID
52 * @fixed_mode : panel native mode
53 * @connector: LVDS/eDP connector
55 * Return downclock_avail
56 * Find the reduced downclock for LVDS/eDP in EDID.
58 struct drm_display_mode *
59 intel_find_panel_downclock(struct drm_device *dev,
60 struct drm_display_mode *fixed_mode,
61 struct drm_connector *connector)
63 struct drm_display_mode *scan, *tmp_mode;
66 temp_downclock = fixed_mode->clock;
69 list_for_each_entry(scan, &connector->probed_modes, head) {
71 * If one mode has the same resolution with the fixed_panel
72 * mode while they have the different refresh rate, it means
73 * that the reduced downclock is found. In such
74 * case we can set the different FPx0/1 to dynamically select
75 * between low and high frequency.
77 if (scan->hdisplay == fixed_mode->hdisplay &&
78 scan->hsync_start == fixed_mode->hsync_start &&
79 scan->hsync_end == fixed_mode->hsync_end &&
80 scan->htotal == fixed_mode->htotal &&
81 scan->vdisplay == fixed_mode->vdisplay &&
82 scan->vsync_start == fixed_mode->vsync_start &&
83 scan->vsync_end == fixed_mode->vsync_end &&
84 scan->vtotal == fixed_mode->vtotal) {
85 if (scan->clock < temp_downclock) {
87 * The downclock is already found. But we
88 * expect to find the lower downclock.
90 temp_downclock = scan->clock;
96 if (temp_downclock < fixed_mode->clock)
97 return drm_mode_duplicate(dev, tmp_mode);
102 /* adjusted_mode has been preset to be the panel's fixed mode */
104 intel_pch_panel_fitting(struct intel_crtc *intel_crtc,
105 struct intel_crtc_state *pipe_config,
108 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
109 int x = 0, y = 0, width = 0, height = 0;
111 /* Native modes don't need fitting */
112 if (adjusted_mode->crtc_hdisplay == pipe_config->pipe_src_w &&
113 adjusted_mode->crtc_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->crtc_hdisplay - width + 1)/2;
121 y = (adjusted_mode->crtc_vdisplay - height + 1)/2;
124 case DRM_MODE_SCALE_ASPECT:
125 /* Scale but preserve the aspect ratio */
127 u32 scaled_width = adjusted_mode->crtc_hdisplay
128 * pipe_config->pipe_src_h;
129 u32 scaled_height = pipe_config->pipe_src_w
130 * adjusted_mode->crtc_vdisplay;
131 if (scaled_width > scaled_height) { /* pillar */
132 width = scaled_height / pipe_config->pipe_src_h;
135 x = (adjusted_mode->crtc_hdisplay - width + 1) / 2;
137 height = adjusted_mode->crtc_vdisplay;
138 } else if (scaled_width < scaled_height) { /* letter */
139 height = scaled_width / pipe_config->pipe_src_w;
142 y = (adjusted_mode->crtc_vdisplay - height + 1) / 2;
144 width = adjusted_mode->crtc_hdisplay;
147 width = adjusted_mode->crtc_hdisplay;
148 height = adjusted_mode->crtc_vdisplay;
153 case DRM_MODE_SCALE_FULLSCREEN:
155 width = adjusted_mode->crtc_hdisplay;
156 height = adjusted_mode->crtc_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 *adjusted_mode,
174 u32 border, sync_pos, blank_width, sync_width;
176 /* keep the hsync and hblank widths constant */
177 sync_width = adjusted_mode->crtc_hsync_end - adjusted_mode->crtc_hsync_start;
178 blank_width = adjusted_mode->crtc_hblank_end - adjusted_mode->crtc_hblank_start;
179 sync_pos = (blank_width - sync_width + 1) / 2;
181 border = (adjusted_mode->crtc_hdisplay - width + 1) / 2;
182 border += border & 1; /* make the border even */
184 adjusted_mode->crtc_hdisplay = width;
185 adjusted_mode->crtc_hblank_start = width + border;
186 adjusted_mode->crtc_hblank_end = adjusted_mode->crtc_hblank_start + blank_width;
188 adjusted_mode->crtc_hsync_start = adjusted_mode->crtc_hblank_start + sync_pos;
189 adjusted_mode->crtc_hsync_end = adjusted_mode->crtc_hsync_start + sync_width;
193 centre_vertically(struct drm_display_mode *adjusted_mode,
196 u32 border, sync_pos, blank_width, sync_width;
198 /* keep the vsync and vblank widths constant */
199 sync_width = adjusted_mode->crtc_vsync_end - adjusted_mode->crtc_vsync_start;
200 blank_width = adjusted_mode->crtc_vblank_end - adjusted_mode->crtc_vblank_start;
201 sync_pos = (blank_width - sync_width + 1) / 2;
203 border = (adjusted_mode->crtc_vdisplay - height + 1) / 2;
205 adjusted_mode->crtc_vdisplay = height;
206 adjusted_mode->crtc_vblank_start = height + border;
207 adjusted_mode->crtc_vblank_end = adjusted_mode->crtc_vblank_start + blank_width;
209 adjusted_mode->crtc_vsync_start = adjusted_mode->crtc_vblank_start + sync_pos;
210 adjusted_mode->crtc_vsync_end = adjusted_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 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
230 u32 scaled_width = adjusted_mode->crtc_hdisplay *
231 pipe_config->pipe_src_h;
232 u32 scaled_height = pipe_config->pipe_src_w *
233 adjusted_mode->crtc_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->crtc_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->crtc_hdisplay *
252 pipe_config->pipe_src_h;
253 u32 scaled_height = pipe_config->pipe_src_w *
254 adjusted_mode->crtc_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->crtc_vdisplay) {
269 bits = panel_fitter_scaling(pipe_config->pipe_src_h,
270 adjusted_mode->crtc_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->crtc_hdisplay) {
285 bits = panel_fitter_scaling(pipe_config->pipe_src_w,
286 adjusted_mode->crtc_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_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
308 u32 pfit_control = 0, pfit_pgm_ratios = 0, border = 0;
309 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
311 /* Native modes don't need fitting */
312 if (adjusted_mode->crtc_hdisplay == pipe_config->pipe_src_w &&
313 adjusted_mode->crtc_vdisplay == pipe_config->pipe_src_h)
316 switch (fitting_mode) {
317 case DRM_MODE_SCALE_CENTER:
319 * For centered modes, we have to calculate border widths &
320 * heights and modify the values programmed into the CRTC.
322 centre_horizontally(adjusted_mode, pipe_config->pipe_src_w);
323 centre_vertically(adjusted_mode, pipe_config->pipe_src_h);
324 border = LVDS_BORDER_ENABLE;
326 case DRM_MODE_SCALE_ASPECT:
327 /* Scale but preserve the aspect ratio */
328 if (INTEL_GEN(dev_priv) >= 4)
329 i965_scale_aspect(pipe_config, &pfit_control);
331 i9xx_scale_aspect(pipe_config, &pfit_control,
332 &pfit_pgm_ratios, &border);
334 case DRM_MODE_SCALE_FULLSCREEN:
336 * Full scaling, even if it changes the aspect ratio.
337 * Fortunately this is all done for us in hw.
339 if (pipe_config->pipe_src_h != adjusted_mode->crtc_vdisplay ||
340 pipe_config->pipe_src_w != adjusted_mode->crtc_hdisplay) {
341 pfit_control |= PFIT_ENABLE;
342 if (INTEL_GEN(dev_priv) >= 4)
343 pfit_control |= PFIT_SCALING_AUTO;
345 pfit_control |= (VERT_AUTO_SCALE |
346 VERT_INTERP_BILINEAR |
348 HORIZ_INTERP_BILINEAR);
352 WARN(1, "bad panel fit mode: %d\n", fitting_mode);
356 /* 965+ wants fuzzy fitting */
357 /* FIXME: handle multiple panels by failing gracefully */
358 if (INTEL_GEN(dev_priv) >= 4)
359 pfit_control |= ((intel_crtc->pipe << PFIT_PIPE_SHIFT) |
363 if ((pfit_control & PFIT_ENABLE) == 0) {
368 /* Make sure pre-965 set dither correctly for 18bpp panels. */
369 if (INTEL_GEN(dev_priv) < 4 && pipe_config->pipe_bpp == 18)
370 pfit_control |= PANEL_8TO6_DITHER_ENABLE;
372 pipe_config->gmch_pfit.control = pfit_control;
373 pipe_config->gmch_pfit.pgm_ratios = pfit_pgm_ratios;
374 pipe_config->gmch_pfit.lvds_border_bits = border;
377 enum drm_connector_status
378 intel_panel_detect(struct drm_device *dev)
380 struct drm_i915_private *dev_priv = to_i915(dev);
382 /* Assume that the BIOS does not lie through the OpRegion... */
383 if (!i915.panel_ignore_lid && dev_priv->opregion.lid_state) {
384 return *dev_priv->opregion.lid_state & 0x1 ?
385 connector_status_connected :
386 connector_status_disconnected;
389 switch (i915.panel_ignore_lid) {
391 return connector_status_connected;
393 return connector_status_disconnected;
395 return connector_status_unknown;
400 * scale - scale values from one range to another
402 * @source_val: value in range [@source_min..@source_max]
404 * Return @source_val in range [@source_min..@source_max] scaled to range
405 * [@target_min..@target_max].
407 static uint32_t scale(uint32_t source_val,
408 uint32_t source_min, uint32_t source_max,
409 uint32_t target_min, uint32_t target_max)
413 WARN_ON(source_min > source_max);
414 WARN_ON(target_min > target_max);
417 source_val = clamp(source_val, source_min, source_max);
419 /* avoid overflows */
420 target_val = DIV_ROUND_CLOSEST_ULL((uint64_t)(source_val - source_min) *
421 (target_max - target_min), source_max - source_min);
422 target_val += target_min;
427 /* Scale user_level in range [0..user_max] to [hw_min..hw_max]. */
428 static inline u32 scale_user_to_hw(struct intel_connector *connector,
429 u32 user_level, u32 user_max)
431 struct intel_panel *panel = &connector->panel;
433 return scale(user_level, 0, user_max,
434 panel->backlight.min, panel->backlight.max);
437 /* Scale user_level in range [0..user_max] to [0..hw_max], clamping the result
438 * to [hw_min..hw_max]. */
439 static inline u32 clamp_user_to_hw(struct intel_connector *connector,
440 u32 user_level, u32 user_max)
442 struct intel_panel *panel = &connector->panel;
445 hw_level = scale(user_level, 0, user_max, 0, panel->backlight.max);
446 hw_level = clamp(hw_level, panel->backlight.min, panel->backlight.max);
451 /* Scale hw_level in range [hw_min..hw_max] to [0..user_max]. */
452 static inline u32 scale_hw_to_user(struct intel_connector *connector,
453 u32 hw_level, u32 user_max)
455 struct intel_panel *panel = &connector->panel;
457 return scale(hw_level, panel->backlight.min, panel->backlight.max,
461 static u32 intel_panel_compute_brightness(struct intel_connector *connector,
464 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
465 struct intel_panel *panel = &connector->panel;
467 WARN_ON(panel->backlight.max == 0);
469 if (i915.invert_brightness < 0)
472 if (i915.invert_brightness > 0 ||
473 dev_priv->quirks & QUIRK_INVERT_BRIGHTNESS) {
474 return panel->backlight.max - val;
480 static u32 lpt_get_backlight(struct intel_connector *connector)
482 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
484 return I915_READ(BLC_PWM_PCH_CTL2) & BACKLIGHT_DUTY_CYCLE_MASK;
487 static u32 pch_get_backlight(struct intel_connector *connector)
489 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
491 return I915_READ(BLC_PWM_CPU_CTL) & BACKLIGHT_DUTY_CYCLE_MASK;
494 static u32 i9xx_get_backlight(struct intel_connector *connector)
496 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
497 struct intel_panel *panel = &connector->panel;
500 val = I915_READ(BLC_PWM_CTL) & BACKLIGHT_DUTY_CYCLE_MASK;
501 if (INTEL_INFO(dev_priv)->gen < 4)
504 if (panel->backlight.combination_mode) {
507 pci_read_config_byte(dev_priv->drm.pdev, LBPC, &lbpc);
514 static u32 _vlv_get_backlight(struct drm_i915_private *dev_priv, enum pipe pipe)
516 if (WARN_ON(pipe != PIPE_A && pipe != PIPE_B))
519 return I915_READ(VLV_BLC_PWM_CTL(pipe)) & BACKLIGHT_DUTY_CYCLE_MASK;
522 static u32 vlv_get_backlight(struct intel_connector *connector)
524 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
525 enum pipe pipe = intel_get_pipe_from_connector(connector);
527 return _vlv_get_backlight(dev_priv, pipe);
530 static u32 bxt_get_backlight(struct intel_connector *connector)
532 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
533 struct intel_panel *panel = &connector->panel;
535 return I915_READ(BXT_BLC_PWM_DUTY(panel->backlight.controller));
538 static u32 pwm_get_backlight(struct intel_connector *connector)
540 struct intel_panel *panel = &connector->panel;
543 duty_ns = pwm_get_duty_cycle(panel->backlight.pwm);
544 return DIV_ROUND_UP(duty_ns * 100, CRC_PMIC_PWM_PERIOD_NS);
547 static u32 intel_panel_get_backlight(struct intel_connector *connector)
549 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
550 struct intel_panel *panel = &connector->panel;
553 mutex_lock(&dev_priv->backlight_lock);
555 if (panel->backlight.enabled) {
556 val = panel->backlight.get(connector);
557 val = intel_panel_compute_brightness(connector, val);
560 mutex_unlock(&dev_priv->backlight_lock);
562 DRM_DEBUG_DRIVER("get backlight PWM = %d\n", val);
566 static void lpt_set_backlight(struct intel_connector *connector, u32 level)
568 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
569 u32 val = I915_READ(BLC_PWM_PCH_CTL2) & ~BACKLIGHT_DUTY_CYCLE_MASK;
570 I915_WRITE(BLC_PWM_PCH_CTL2, val | level);
573 static void pch_set_backlight(struct intel_connector *connector, u32 level)
575 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
578 tmp = I915_READ(BLC_PWM_CPU_CTL) & ~BACKLIGHT_DUTY_CYCLE_MASK;
579 I915_WRITE(BLC_PWM_CPU_CTL, tmp | level);
582 static void i9xx_set_backlight(struct intel_connector *connector, u32 level)
584 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
585 struct intel_panel *panel = &connector->panel;
588 WARN_ON(panel->backlight.max == 0);
590 if (panel->backlight.combination_mode) {
593 lbpc = level * 0xfe / panel->backlight.max + 1;
595 pci_write_config_byte(dev_priv->drm.pdev, LBPC, lbpc);
598 if (IS_GEN4(dev_priv)) {
599 mask = BACKLIGHT_DUTY_CYCLE_MASK;
602 mask = BACKLIGHT_DUTY_CYCLE_MASK_PNV;
605 tmp = I915_READ(BLC_PWM_CTL) & ~mask;
606 I915_WRITE(BLC_PWM_CTL, tmp | level);
609 static void vlv_set_backlight(struct intel_connector *connector, u32 level)
611 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
612 enum pipe pipe = intel_get_pipe_from_connector(connector);
615 if (WARN_ON(pipe != PIPE_A && pipe != PIPE_B))
618 tmp = I915_READ(VLV_BLC_PWM_CTL(pipe)) & ~BACKLIGHT_DUTY_CYCLE_MASK;
619 I915_WRITE(VLV_BLC_PWM_CTL(pipe), tmp | level);
622 static void bxt_set_backlight(struct intel_connector *connector, u32 level)
624 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
625 struct intel_panel *panel = &connector->panel;
627 I915_WRITE(BXT_BLC_PWM_DUTY(panel->backlight.controller), level);
630 static void pwm_set_backlight(struct intel_connector *connector, u32 level)
632 struct intel_panel *panel = &connector->panel;
633 int duty_ns = DIV_ROUND_UP(level * CRC_PMIC_PWM_PERIOD_NS, 100);
635 pwm_config(panel->backlight.pwm, duty_ns, CRC_PMIC_PWM_PERIOD_NS);
639 intel_panel_actually_set_backlight(struct intel_connector *connector, u32 level)
641 struct intel_panel *panel = &connector->panel;
643 DRM_DEBUG_DRIVER("set backlight PWM = %d\n", level);
645 level = intel_panel_compute_brightness(connector, level);
646 panel->backlight.set(connector, level);
649 /* set backlight brightness to level in range [0..max], scaling wrt hw min */
650 static void intel_panel_set_backlight(struct intel_connector *connector,
651 u32 user_level, u32 user_max)
653 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
654 struct intel_panel *panel = &connector->panel;
657 if (!panel->backlight.present)
660 mutex_lock(&dev_priv->backlight_lock);
662 WARN_ON(panel->backlight.max == 0);
664 hw_level = scale_user_to_hw(connector, user_level, user_max);
665 panel->backlight.level = hw_level;
667 if (panel->backlight.enabled)
668 intel_panel_actually_set_backlight(connector, hw_level);
670 mutex_unlock(&dev_priv->backlight_lock);
673 /* set backlight brightness to level in range [0..max], assuming hw min is
676 void intel_panel_set_backlight_acpi(struct intel_connector *connector,
677 u32 user_level, u32 user_max)
679 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
680 struct intel_panel *panel = &connector->panel;
681 enum pipe pipe = intel_get_pipe_from_connector(connector);
685 * INVALID_PIPE may occur during driver init because
686 * connection_mutex isn't held across the entire backlight
687 * setup + modeset readout, and the BIOS can issue the
688 * requests at any time.
690 if (!panel->backlight.present || pipe == INVALID_PIPE)
693 mutex_lock(&dev_priv->backlight_lock);
695 WARN_ON(panel->backlight.max == 0);
697 hw_level = clamp_user_to_hw(connector, user_level, user_max);
698 panel->backlight.level = hw_level;
700 if (panel->backlight.device)
701 panel->backlight.device->props.brightness =
702 scale_hw_to_user(connector,
703 panel->backlight.level,
704 panel->backlight.device->props.max_brightness);
706 if (panel->backlight.enabled)
707 intel_panel_actually_set_backlight(connector, hw_level);
709 mutex_unlock(&dev_priv->backlight_lock);
712 static void lpt_disable_backlight(struct intel_connector *connector)
714 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
717 intel_panel_actually_set_backlight(connector, 0);
720 * Although we don't support or enable CPU PWM with LPT/SPT based
721 * systems, it may have been enabled prior to loading the
722 * driver. Disable to avoid warnings on LCPLL disable.
724 * This needs rework if we need to add support for CPU PWM on PCH split
727 tmp = I915_READ(BLC_PWM_CPU_CTL2);
728 if (tmp & BLM_PWM_ENABLE) {
729 DRM_DEBUG_KMS("cpu backlight was enabled, disabling\n");
730 I915_WRITE(BLC_PWM_CPU_CTL2, tmp & ~BLM_PWM_ENABLE);
733 tmp = I915_READ(BLC_PWM_PCH_CTL1);
734 I915_WRITE(BLC_PWM_PCH_CTL1, tmp & ~BLM_PCH_PWM_ENABLE);
737 static void pch_disable_backlight(struct intel_connector *connector)
739 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
742 intel_panel_actually_set_backlight(connector, 0);
744 tmp = I915_READ(BLC_PWM_CPU_CTL2);
745 I915_WRITE(BLC_PWM_CPU_CTL2, tmp & ~BLM_PWM_ENABLE);
747 tmp = I915_READ(BLC_PWM_PCH_CTL1);
748 I915_WRITE(BLC_PWM_PCH_CTL1, tmp & ~BLM_PCH_PWM_ENABLE);
751 static void i9xx_disable_backlight(struct intel_connector *connector)
753 intel_panel_actually_set_backlight(connector, 0);
756 static void i965_disable_backlight(struct intel_connector *connector)
758 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
761 intel_panel_actually_set_backlight(connector, 0);
763 tmp = I915_READ(BLC_PWM_CTL2);
764 I915_WRITE(BLC_PWM_CTL2, tmp & ~BLM_PWM_ENABLE);
767 static void vlv_disable_backlight(struct intel_connector *connector)
769 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
770 enum pipe pipe = intel_get_pipe_from_connector(connector);
773 if (WARN_ON(pipe != PIPE_A && pipe != PIPE_B))
776 intel_panel_actually_set_backlight(connector, 0);
778 tmp = I915_READ(VLV_BLC_PWM_CTL2(pipe));
779 I915_WRITE(VLV_BLC_PWM_CTL2(pipe), tmp & ~BLM_PWM_ENABLE);
782 static void bxt_disable_backlight(struct intel_connector *connector)
784 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
785 struct intel_panel *panel = &connector->panel;
788 intel_panel_actually_set_backlight(connector, 0);
790 tmp = I915_READ(BXT_BLC_PWM_CTL(panel->backlight.controller));
791 I915_WRITE(BXT_BLC_PWM_CTL(panel->backlight.controller),
792 tmp & ~BXT_BLC_PWM_ENABLE);
794 if (panel->backlight.controller == 1) {
795 val = I915_READ(UTIL_PIN_CTL);
796 val &= ~UTIL_PIN_ENABLE;
797 I915_WRITE(UTIL_PIN_CTL, val);
801 static void pwm_disable_backlight(struct intel_connector *connector)
803 struct intel_panel *panel = &connector->panel;
805 /* Disable the backlight */
806 pwm_config(panel->backlight.pwm, 0, CRC_PMIC_PWM_PERIOD_NS);
807 usleep_range(2000, 3000);
808 pwm_disable(panel->backlight.pwm);
811 void intel_panel_disable_backlight(struct intel_connector *connector)
813 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
814 struct intel_panel *panel = &connector->panel;
816 if (!panel->backlight.present)
820 * Do not disable backlight on the vga_switcheroo path. When switching
821 * away from i915, the other client may depend on i915 to handle the
822 * backlight. This will leave the backlight on unnecessarily when
823 * another client is not activated.
825 if (dev_priv->drm.switch_power_state == DRM_SWITCH_POWER_CHANGING) {
826 DRM_DEBUG_DRIVER("Skipping backlight disable on vga switch\n");
830 mutex_lock(&dev_priv->backlight_lock);
832 if (panel->backlight.device)
833 panel->backlight.device->props.power = FB_BLANK_POWERDOWN;
834 panel->backlight.enabled = false;
835 panel->backlight.disable(connector);
837 mutex_unlock(&dev_priv->backlight_lock);
840 static void lpt_enable_backlight(struct intel_connector *connector)
842 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
843 struct intel_panel *panel = &connector->panel;
844 u32 pch_ctl1, pch_ctl2, schicken;
846 pch_ctl1 = I915_READ(BLC_PWM_PCH_CTL1);
847 if (pch_ctl1 & BLM_PCH_PWM_ENABLE) {
848 DRM_DEBUG_KMS("pch backlight already enabled\n");
849 pch_ctl1 &= ~BLM_PCH_PWM_ENABLE;
850 I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1);
853 if (HAS_PCH_LPT(dev_priv)) {
854 schicken = I915_READ(SOUTH_CHICKEN2);
855 if (panel->backlight.alternate_pwm_increment)
856 schicken |= LPT_PWM_GRANULARITY;
858 schicken &= ~LPT_PWM_GRANULARITY;
859 I915_WRITE(SOUTH_CHICKEN2, schicken);
861 schicken = I915_READ(SOUTH_CHICKEN1);
862 if (panel->backlight.alternate_pwm_increment)
863 schicken |= SPT_PWM_GRANULARITY;
865 schicken &= ~SPT_PWM_GRANULARITY;
866 I915_WRITE(SOUTH_CHICKEN1, schicken);
869 pch_ctl2 = panel->backlight.max << 16;
870 I915_WRITE(BLC_PWM_PCH_CTL2, pch_ctl2);
873 if (panel->backlight.active_low_pwm)
874 pch_ctl1 |= BLM_PCH_POLARITY;
876 /* After LPT, override is the default. */
877 if (HAS_PCH_LPT(dev_priv))
878 pch_ctl1 |= BLM_PCH_OVERRIDE_ENABLE;
880 I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1);
881 POSTING_READ(BLC_PWM_PCH_CTL1);
882 I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1 | BLM_PCH_PWM_ENABLE);
884 /* This won't stick until the above enable. */
885 intel_panel_actually_set_backlight(connector, panel->backlight.level);
888 static void pch_enable_backlight(struct intel_connector *connector)
890 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
891 struct intel_panel *panel = &connector->panel;
892 enum pipe pipe = intel_get_pipe_from_connector(connector);
893 enum transcoder cpu_transcoder =
894 intel_pipe_to_cpu_transcoder(dev_priv, pipe);
895 u32 cpu_ctl2, pch_ctl1, pch_ctl2;
897 cpu_ctl2 = I915_READ(BLC_PWM_CPU_CTL2);
898 if (cpu_ctl2 & BLM_PWM_ENABLE) {
899 DRM_DEBUG_KMS("cpu backlight already enabled\n");
900 cpu_ctl2 &= ~BLM_PWM_ENABLE;
901 I915_WRITE(BLC_PWM_CPU_CTL2, cpu_ctl2);
904 pch_ctl1 = I915_READ(BLC_PWM_PCH_CTL1);
905 if (pch_ctl1 & BLM_PCH_PWM_ENABLE) {
906 DRM_DEBUG_KMS("pch backlight already enabled\n");
907 pch_ctl1 &= ~BLM_PCH_PWM_ENABLE;
908 I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1);
911 if (cpu_transcoder == TRANSCODER_EDP)
912 cpu_ctl2 = BLM_TRANSCODER_EDP;
914 cpu_ctl2 = BLM_PIPE(cpu_transcoder);
915 I915_WRITE(BLC_PWM_CPU_CTL2, cpu_ctl2);
916 POSTING_READ(BLC_PWM_CPU_CTL2);
917 I915_WRITE(BLC_PWM_CPU_CTL2, cpu_ctl2 | BLM_PWM_ENABLE);
919 /* This won't stick until the above enable. */
920 intel_panel_actually_set_backlight(connector, panel->backlight.level);
922 pch_ctl2 = panel->backlight.max << 16;
923 I915_WRITE(BLC_PWM_PCH_CTL2, pch_ctl2);
926 if (panel->backlight.active_low_pwm)
927 pch_ctl1 |= BLM_PCH_POLARITY;
929 I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1);
930 POSTING_READ(BLC_PWM_PCH_CTL1);
931 I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1 | BLM_PCH_PWM_ENABLE);
934 static void i9xx_enable_backlight(struct intel_connector *connector)
936 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
937 struct intel_panel *panel = &connector->panel;
940 ctl = I915_READ(BLC_PWM_CTL);
941 if (ctl & BACKLIGHT_DUTY_CYCLE_MASK_PNV) {
942 DRM_DEBUG_KMS("backlight already enabled\n");
943 I915_WRITE(BLC_PWM_CTL, 0);
946 freq = panel->backlight.max;
947 if (panel->backlight.combination_mode)
951 if (panel->backlight.combination_mode)
952 ctl |= BLM_LEGACY_MODE;
953 if (IS_PINEVIEW(dev_priv) && panel->backlight.active_low_pwm)
954 ctl |= BLM_POLARITY_PNV;
956 I915_WRITE(BLC_PWM_CTL, ctl);
957 POSTING_READ(BLC_PWM_CTL);
959 /* XXX: combine this into above write? */
960 intel_panel_actually_set_backlight(connector, panel->backlight.level);
963 * Needed to enable backlight on some 855gm models. BLC_HIST_CTL is
964 * 855gm only, but checking for gen2 is safe, as 855gm is the only gen2
965 * that has backlight.
967 if (IS_GEN2(dev_priv))
968 I915_WRITE(BLC_HIST_CTL, BLM_HISTOGRAM_ENABLE);
971 static void i965_enable_backlight(struct intel_connector *connector)
973 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
974 struct intel_panel *panel = &connector->panel;
975 enum pipe pipe = intel_get_pipe_from_connector(connector);
978 ctl2 = I915_READ(BLC_PWM_CTL2);
979 if (ctl2 & BLM_PWM_ENABLE) {
980 DRM_DEBUG_KMS("backlight already enabled\n");
981 ctl2 &= ~BLM_PWM_ENABLE;
982 I915_WRITE(BLC_PWM_CTL2, ctl2);
985 freq = panel->backlight.max;
986 if (panel->backlight.combination_mode)
990 I915_WRITE(BLC_PWM_CTL, ctl);
992 ctl2 = BLM_PIPE(pipe);
993 if (panel->backlight.combination_mode)
994 ctl2 |= BLM_COMBINATION_MODE;
995 if (panel->backlight.active_low_pwm)
996 ctl2 |= BLM_POLARITY_I965;
997 I915_WRITE(BLC_PWM_CTL2, ctl2);
998 POSTING_READ(BLC_PWM_CTL2);
999 I915_WRITE(BLC_PWM_CTL2, ctl2 | BLM_PWM_ENABLE);
1001 intel_panel_actually_set_backlight(connector, panel->backlight.level);
1004 static void vlv_enable_backlight(struct intel_connector *connector)
1006 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1007 struct intel_panel *panel = &connector->panel;
1008 enum pipe pipe = intel_get_pipe_from_connector(connector);
1011 if (WARN_ON(pipe != PIPE_A && pipe != PIPE_B))
1014 ctl2 = I915_READ(VLV_BLC_PWM_CTL2(pipe));
1015 if (ctl2 & BLM_PWM_ENABLE) {
1016 DRM_DEBUG_KMS("backlight already enabled\n");
1017 ctl2 &= ~BLM_PWM_ENABLE;
1018 I915_WRITE(VLV_BLC_PWM_CTL2(pipe), ctl2);
1021 ctl = panel->backlight.max << 16;
1022 I915_WRITE(VLV_BLC_PWM_CTL(pipe), ctl);
1024 /* XXX: combine this into above write? */
1025 intel_panel_actually_set_backlight(connector, panel->backlight.level);
1028 if (panel->backlight.active_low_pwm)
1029 ctl2 |= BLM_POLARITY_I965;
1030 I915_WRITE(VLV_BLC_PWM_CTL2(pipe), ctl2);
1031 POSTING_READ(VLV_BLC_PWM_CTL2(pipe));
1032 I915_WRITE(VLV_BLC_PWM_CTL2(pipe), ctl2 | BLM_PWM_ENABLE);
1035 static void bxt_enable_backlight(struct intel_connector *connector)
1037 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1038 struct intel_panel *panel = &connector->panel;
1039 enum pipe pipe = intel_get_pipe_from_connector(connector);
1042 /* To use 2nd set of backlight registers, utility pin has to be
1043 * enabled with PWM mode.
1044 * The field should only be changed when the utility pin is disabled
1046 if (panel->backlight.controller == 1) {
1047 val = I915_READ(UTIL_PIN_CTL);
1048 if (val & UTIL_PIN_ENABLE) {
1049 DRM_DEBUG_KMS("util pin already enabled\n");
1050 val &= ~UTIL_PIN_ENABLE;
1051 I915_WRITE(UTIL_PIN_CTL, val);
1055 if (panel->backlight.util_pin_active_low)
1056 val |= UTIL_PIN_POLARITY;
1057 I915_WRITE(UTIL_PIN_CTL, val | UTIL_PIN_PIPE(pipe) |
1058 UTIL_PIN_MODE_PWM | UTIL_PIN_ENABLE);
1061 pwm_ctl = I915_READ(BXT_BLC_PWM_CTL(panel->backlight.controller));
1062 if (pwm_ctl & BXT_BLC_PWM_ENABLE) {
1063 DRM_DEBUG_KMS("backlight already enabled\n");
1064 pwm_ctl &= ~BXT_BLC_PWM_ENABLE;
1065 I915_WRITE(BXT_BLC_PWM_CTL(panel->backlight.controller),
1069 I915_WRITE(BXT_BLC_PWM_FREQ(panel->backlight.controller),
1070 panel->backlight.max);
1072 intel_panel_actually_set_backlight(connector, panel->backlight.level);
1075 if (panel->backlight.active_low_pwm)
1076 pwm_ctl |= BXT_BLC_PWM_POLARITY;
1078 I915_WRITE(BXT_BLC_PWM_CTL(panel->backlight.controller), pwm_ctl);
1079 POSTING_READ(BXT_BLC_PWM_CTL(panel->backlight.controller));
1080 I915_WRITE(BXT_BLC_PWM_CTL(panel->backlight.controller),
1081 pwm_ctl | BXT_BLC_PWM_ENABLE);
1084 static void pwm_enable_backlight(struct intel_connector *connector)
1086 struct intel_panel *panel = &connector->panel;
1088 pwm_enable(panel->backlight.pwm);
1089 intel_panel_actually_set_backlight(connector, panel->backlight.level);
1092 void intel_panel_enable_backlight(struct intel_connector *connector)
1094 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1095 struct intel_panel *panel = &connector->panel;
1096 enum pipe pipe = intel_get_pipe_from_connector(connector);
1098 if (!panel->backlight.present)
1101 DRM_DEBUG_KMS("pipe %c\n", pipe_name(pipe));
1103 mutex_lock(&dev_priv->backlight_lock);
1105 WARN_ON(panel->backlight.max == 0);
1107 if (panel->backlight.level <= panel->backlight.min) {
1108 panel->backlight.level = panel->backlight.max;
1109 if (panel->backlight.device)
1110 panel->backlight.device->props.brightness =
1111 scale_hw_to_user(connector,
1112 panel->backlight.level,
1113 panel->backlight.device->props.max_brightness);
1116 panel->backlight.enable(connector);
1117 panel->backlight.enabled = true;
1118 if (panel->backlight.device)
1119 panel->backlight.device->props.power = FB_BLANK_UNBLANK;
1121 mutex_unlock(&dev_priv->backlight_lock);
1124 #if IS_ENABLED(CONFIG_BACKLIGHT_CLASS_DEVICE)
1125 static int intel_backlight_device_update_status(struct backlight_device *bd)
1127 struct intel_connector *connector = bl_get_data(bd);
1128 struct intel_panel *panel = &connector->panel;
1129 struct drm_device *dev = connector->base.dev;
1131 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1132 DRM_DEBUG_KMS("updating intel_backlight, brightness=%d/%d\n",
1133 bd->props.brightness, bd->props.max_brightness);
1134 intel_panel_set_backlight(connector, bd->props.brightness,
1135 bd->props.max_brightness);
1138 * Allow flipping bl_power as a sub-state of enabled. Sadly the
1139 * backlight class device does not make it easy to to differentiate
1140 * between callbacks for brightness and bl_power, so our backlight_power
1141 * callback needs to take this into account.
1143 if (panel->backlight.enabled) {
1144 if (panel->backlight.power) {
1145 bool enable = bd->props.power == FB_BLANK_UNBLANK &&
1146 bd->props.brightness != 0;
1147 panel->backlight.power(connector, enable);
1150 bd->props.power = FB_BLANK_POWERDOWN;
1153 drm_modeset_unlock(&dev->mode_config.connection_mutex);
1157 static int intel_backlight_device_get_brightness(struct backlight_device *bd)
1159 struct intel_connector *connector = bl_get_data(bd);
1160 struct drm_device *dev = connector->base.dev;
1161 struct drm_i915_private *dev_priv = to_i915(dev);
1165 intel_runtime_pm_get(dev_priv);
1166 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1168 hw_level = intel_panel_get_backlight(connector);
1169 ret = scale_hw_to_user(connector, hw_level, bd->props.max_brightness);
1171 drm_modeset_unlock(&dev->mode_config.connection_mutex);
1172 intel_runtime_pm_put(dev_priv);
1177 static const struct backlight_ops intel_backlight_device_ops = {
1178 .update_status = intel_backlight_device_update_status,
1179 .get_brightness = intel_backlight_device_get_brightness,
1182 int intel_backlight_device_register(struct intel_connector *connector)
1184 struct intel_panel *panel = &connector->panel;
1185 struct backlight_properties props;
1187 if (WARN_ON(panel->backlight.device))
1190 if (!panel->backlight.present)
1193 WARN_ON(panel->backlight.max == 0);
1195 memset(&props, 0, sizeof(props));
1196 props.type = BACKLIGHT_RAW;
1199 * Note: Everything should work even if the backlight device max
1200 * presented to the userspace is arbitrarily chosen.
1202 props.max_brightness = panel->backlight.max;
1203 props.brightness = scale_hw_to_user(connector,
1204 panel->backlight.level,
1205 props.max_brightness);
1207 if (panel->backlight.enabled)
1208 props.power = FB_BLANK_UNBLANK;
1210 props.power = FB_BLANK_POWERDOWN;
1213 * Note: using the same name independent of the connector prevents
1214 * registration of multiple backlight devices in the driver.
1216 panel->backlight.device =
1217 backlight_device_register("intel_backlight",
1218 connector->base.kdev,
1220 &intel_backlight_device_ops, &props);
1222 if (IS_ERR(panel->backlight.device)) {
1223 DRM_ERROR("Failed to register backlight: %ld\n",
1224 PTR_ERR(panel->backlight.device));
1225 panel->backlight.device = NULL;
1229 DRM_DEBUG_KMS("Connector %s backlight sysfs interface registered\n",
1230 connector->base.name);
1235 void intel_backlight_device_unregister(struct intel_connector *connector)
1237 struct intel_panel *panel = &connector->panel;
1239 if (panel->backlight.device) {
1240 backlight_device_unregister(panel->backlight.device);
1241 panel->backlight.device = NULL;
1244 #endif /* CONFIG_BACKLIGHT_CLASS_DEVICE */
1247 * BXT: PWM clock frequency = 19.2 MHz.
1249 static u32 bxt_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
1251 return DIV_ROUND_CLOSEST(KHz(19200), pwm_freq_hz);
1255 * SPT: This value represents the period of the PWM stream in clock periods
1256 * multiplied by 16 (default increment) or 128 (alternate increment selected in
1257 * SCHICKEN_1 bit 0). PWM clock is 24 MHz.
1259 static u32 spt_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
1261 struct intel_panel *panel = &connector->panel;
1264 if (panel->backlight.alternate_pwm_increment)
1269 return DIV_ROUND_CLOSEST(MHz(24), pwm_freq_hz * mul);
1273 * LPT: This value represents the period of the PWM stream in clock periods
1274 * multiplied by 128 (default increment) or 16 (alternate increment, selected in
1275 * LPT SOUTH_CHICKEN2 register bit 5).
1277 static u32 lpt_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
1279 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1280 struct intel_panel *panel = &connector->panel;
1283 if (panel->backlight.alternate_pwm_increment)
1288 if (HAS_PCH_LPT_H(dev_priv))
1289 clock = MHz(135); /* LPT:H */
1291 clock = MHz(24); /* LPT:LP */
1293 return DIV_ROUND_CLOSEST(clock, pwm_freq_hz * mul);
1297 * ILK/SNB/IVB: This value represents the period of the PWM stream in PCH
1298 * display raw clocks multiplied by 128.
1300 static u32 pch_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
1302 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1304 return DIV_ROUND_CLOSEST(KHz(dev_priv->rawclk_freq), pwm_freq_hz * 128);
1308 * Gen2: This field determines the number of time base events (display core
1309 * clock frequency/32) in total for a complete cycle of modulated backlight
1312 * Gen3: A time base event equals the display core clock ([DevPNV] HRAW clock)
1315 static u32 i9xx_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
1317 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1320 if (IS_PINEVIEW(dev_priv))
1321 clock = KHz(dev_priv->rawclk_freq);
1323 clock = KHz(dev_priv->cdclk_freq);
1325 return DIV_ROUND_CLOSEST(clock, pwm_freq_hz * 32);
1329 * Gen4: This value represents the period of the PWM stream in display core
1330 * clocks ([DevCTG] HRAW clocks) multiplied by 128.
1333 static u32 i965_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
1335 struct drm_device *dev = connector->base.dev;
1336 struct drm_i915_private *dev_priv = to_i915(dev);
1339 if (IS_G4X(dev_priv))
1340 clock = KHz(dev_priv->rawclk_freq);
1342 clock = KHz(dev_priv->cdclk_freq);
1344 return DIV_ROUND_CLOSEST(clock, pwm_freq_hz * 128);
1348 * VLV: This value represents the period of the PWM stream in display core
1349 * clocks ([DevCTG] 200MHz HRAW clocks) multiplied by 128 or 25MHz S0IX clocks
1350 * multiplied by 16. CHV uses a 19.2MHz S0IX clock.
1352 static u32 vlv_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
1354 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1357 if ((I915_READ(CBR1_VLV) & CBR_PWM_CLOCK_MUX_SELECT) == 0) {
1358 if (IS_CHERRYVIEW(dev_priv))
1364 clock = KHz(dev_priv->rawclk_freq);
1368 return DIV_ROUND_CLOSEST(clock, pwm_freq_hz * mul);
1371 static u32 get_backlight_max_vbt(struct intel_connector *connector)
1373 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1374 struct intel_panel *panel = &connector->panel;
1375 u16 pwm_freq_hz = dev_priv->vbt.backlight.pwm_freq_hz;
1378 if (!panel->backlight.hz_to_pwm) {
1379 DRM_DEBUG_KMS("backlight frequency conversion not supported\n");
1384 DRM_DEBUG_KMS("VBT defined backlight frequency %u Hz\n",
1388 DRM_DEBUG_KMS("default backlight frequency %u Hz\n",
1392 pwm = panel->backlight.hz_to_pwm(connector, pwm_freq_hz);
1394 DRM_DEBUG_KMS("backlight frequency conversion failed\n");
1402 * Note: The setup hooks can't assume pipe is set!
1404 static u32 get_backlight_min_vbt(struct intel_connector *connector)
1406 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1407 struct intel_panel *panel = &connector->panel;
1410 WARN_ON(panel->backlight.max == 0);
1413 * XXX: If the vbt value is 255, it makes min equal to max, which leads
1414 * to problems. There are such machines out there. Either our
1415 * interpretation is wrong or the vbt has bogus data. Or both. Safeguard
1416 * against this by letting the minimum be at most (arbitrarily chosen)
1419 min = clamp_t(int, dev_priv->vbt.backlight.min_brightness, 0, 64);
1420 if (min != dev_priv->vbt.backlight.min_brightness) {
1421 DRM_DEBUG_KMS("clamping VBT min backlight %d/255 to %d/255\n",
1422 dev_priv->vbt.backlight.min_brightness, min);
1425 /* vbt value is a coefficient in range [0..255] */
1426 return scale(min, 0, 255, 0, panel->backlight.max);
1429 static int lpt_setup_backlight(struct intel_connector *connector, enum pipe unused)
1431 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1432 struct intel_panel *panel = &connector->panel;
1433 u32 pch_ctl1, pch_ctl2, val;
1436 if (HAS_PCH_LPT(dev_priv))
1437 alt = I915_READ(SOUTH_CHICKEN2) & LPT_PWM_GRANULARITY;
1439 alt = I915_READ(SOUTH_CHICKEN1) & SPT_PWM_GRANULARITY;
1440 panel->backlight.alternate_pwm_increment = alt;
1442 pch_ctl1 = I915_READ(BLC_PWM_PCH_CTL1);
1443 panel->backlight.active_low_pwm = pch_ctl1 & BLM_PCH_POLARITY;
1445 pch_ctl2 = I915_READ(BLC_PWM_PCH_CTL2);
1446 panel->backlight.max = pch_ctl2 >> 16;
1448 if (!panel->backlight.max)
1449 panel->backlight.max = get_backlight_max_vbt(connector);
1451 if (!panel->backlight.max)
1454 panel->backlight.min = get_backlight_min_vbt(connector);
1456 val = lpt_get_backlight(connector);
1457 val = intel_panel_compute_brightness(connector, val);
1458 panel->backlight.level = clamp(val, panel->backlight.min,
1459 panel->backlight.max);
1461 panel->backlight.enabled = pch_ctl1 & BLM_PCH_PWM_ENABLE;
1466 static int pch_setup_backlight(struct intel_connector *connector, enum pipe unused)
1468 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1469 struct intel_panel *panel = &connector->panel;
1470 u32 cpu_ctl2, pch_ctl1, pch_ctl2, val;
1472 pch_ctl1 = I915_READ(BLC_PWM_PCH_CTL1);
1473 panel->backlight.active_low_pwm = pch_ctl1 & BLM_PCH_POLARITY;
1475 pch_ctl2 = I915_READ(BLC_PWM_PCH_CTL2);
1476 panel->backlight.max = pch_ctl2 >> 16;
1478 if (!panel->backlight.max)
1479 panel->backlight.max = get_backlight_max_vbt(connector);
1481 if (!panel->backlight.max)
1484 panel->backlight.min = get_backlight_min_vbt(connector);
1486 val = pch_get_backlight(connector);
1487 val = intel_panel_compute_brightness(connector, val);
1488 panel->backlight.level = clamp(val, panel->backlight.min,
1489 panel->backlight.max);
1491 cpu_ctl2 = I915_READ(BLC_PWM_CPU_CTL2);
1492 panel->backlight.enabled = (cpu_ctl2 & BLM_PWM_ENABLE) &&
1493 (pch_ctl1 & BLM_PCH_PWM_ENABLE);
1498 static int i9xx_setup_backlight(struct intel_connector *connector, enum pipe unused)
1500 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1501 struct intel_panel *panel = &connector->panel;
1504 ctl = I915_READ(BLC_PWM_CTL);
1506 if (IS_GEN2(dev_priv) || IS_I915GM(dev_priv) || IS_I945GM(dev_priv))
1507 panel->backlight.combination_mode = ctl & BLM_LEGACY_MODE;
1509 if (IS_PINEVIEW(dev_priv))
1510 panel->backlight.active_low_pwm = ctl & BLM_POLARITY_PNV;
1512 panel->backlight.max = ctl >> 17;
1514 if (!panel->backlight.max) {
1515 panel->backlight.max = get_backlight_max_vbt(connector);
1516 panel->backlight.max >>= 1;
1519 if (!panel->backlight.max)
1522 if (panel->backlight.combination_mode)
1523 panel->backlight.max *= 0xff;
1525 panel->backlight.min = get_backlight_min_vbt(connector);
1527 val = i9xx_get_backlight(connector);
1528 val = intel_panel_compute_brightness(connector, val);
1529 panel->backlight.level = clamp(val, panel->backlight.min,
1530 panel->backlight.max);
1532 panel->backlight.enabled = val != 0;
1537 static int i965_setup_backlight(struct intel_connector *connector, enum pipe unused)
1539 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1540 struct intel_panel *panel = &connector->panel;
1543 ctl2 = I915_READ(BLC_PWM_CTL2);
1544 panel->backlight.combination_mode = ctl2 & BLM_COMBINATION_MODE;
1545 panel->backlight.active_low_pwm = ctl2 & BLM_POLARITY_I965;
1547 ctl = I915_READ(BLC_PWM_CTL);
1548 panel->backlight.max = ctl >> 16;
1550 if (!panel->backlight.max)
1551 panel->backlight.max = get_backlight_max_vbt(connector);
1553 if (!panel->backlight.max)
1556 if (panel->backlight.combination_mode)
1557 panel->backlight.max *= 0xff;
1559 panel->backlight.min = get_backlight_min_vbt(connector);
1561 val = i9xx_get_backlight(connector);
1562 val = intel_panel_compute_brightness(connector, val);
1563 panel->backlight.level = clamp(val, panel->backlight.min,
1564 panel->backlight.max);
1566 panel->backlight.enabled = ctl2 & BLM_PWM_ENABLE;
1571 static int vlv_setup_backlight(struct intel_connector *connector, enum pipe pipe)
1573 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1574 struct intel_panel *panel = &connector->panel;
1577 if (WARN_ON(pipe != PIPE_A && pipe != PIPE_B))
1580 ctl2 = I915_READ(VLV_BLC_PWM_CTL2(pipe));
1581 panel->backlight.active_low_pwm = ctl2 & BLM_POLARITY_I965;
1583 ctl = I915_READ(VLV_BLC_PWM_CTL(pipe));
1584 panel->backlight.max = ctl >> 16;
1586 if (!panel->backlight.max)
1587 panel->backlight.max = get_backlight_max_vbt(connector);
1589 if (!panel->backlight.max)
1592 panel->backlight.min = get_backlight_min_vbt(connector);
1594 val = _vlv_get_backlight(dev_priv, pipe);
1595 val = intel_panel_compute_brightness(connector, val);
1596 panel->backlight.level = clamp(val, panel->backlight.min,
1597 panel->backlight.max);
1599 panel->backlight.enabled = ctl2 & BLM_PWM_ENABLE;
1605 bxt_setup_backlight(struct intel_connector *connector, enum pipe unused)
1607 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1608 struct intel_panel *panel = &connector->panel;
1612 * For BXT hard coding the Backlight controller to 0.
1613 * TODO : Read the controller value from VBT and generalize
1615 panel->backlight.controller = 0;
1617 pwm_ctl = I915_READ(BXT_BLC_PWM_CTL(panel->backlight.controller));
1619 /* Keeping the check if controller 1 is to be programmed.
1620 * This will come into affect once the VBT parsing
1621 * is fixed for controller selection, and controller 1 is used
1622 * for a prticular display configuration.
1624 if (panel->backlight.controller == 1) {
1625 val = I915_READ(UTIL_PIN_CTL);
1626 panel->backlight.util_pin_active_low =
1627 val & UTIL_PIN_POLARITY;
1630 panel->backlight.active_low_pwm = pwm_ctl & BXT_BLC_PWM_POLARITY;
1631 panel->backlight.max =
1632 I915_READ(BXT_BLC_PWM_FREQ(panel->backlight.controller));
1634 if (!panel->backlight.max)
1635 panel->backlight.max = get_backlight_max_vbt(connector);
1637 if (!panel->backlight.max)
1640 val = bxt_get_backlight(connector);
1641 val = intel_panel_compute_brightness(connector, val);
1642 panel->backlight.level = clamp(val, panel->backlight.min,
1643 panel->backlight.max);
1645 panel->backlight.enabled = pwm_ctl & BXT_BLC_PWM_ENABLE;
1650 static int pwm_setup_backlight(struct intel_connector *connector,
1653 struct drm_device *dev = connector->base.dev;
1654 struct intel_panel *panel = &connector->panel;
1657 /* Get the PWM chip for backlight control */
1658 panel->backlight.pwm = pwm_get(dev->dev, "pwm_backlight");
1659 if (IS_ERR(panel->backlight.pwm)) {
1660 DRM_ERROR("Failed to own the pwm chip\n");
1661 panel->backlight.pwm = NULL;
1666 * FIXME: pwm_apply_args() should be removed when switching to
1667 * the atomic PWM API.
1669 pwm_apply_args(panel->backlight.pwm);
1671 retval = pwm_config(panel->backlight.pwm, CRC_PMIC_PWM_PERIOD_NS,
1672 CRC_PMIC_PWM_PERIOD_NS);
1674 DRM_ERROR("Failed to configure the pwm chip\n");
1675 pwm_put(panel->backlight.pwm);
1676 panel->backlight.pwm = NULL;
1680 panel->backlight.min = 0; /* 0% */
1681 panel->backlight.max = 100; /* 100% */
1682 panel->backlight.level = DIV_ROUND_UP(
1683 pwm_get_duty_cycle(panel->backlight.pwm) * 100,
1684 CRC_PMIC_PWM_PERIOD_NS);
1685 panel->backlight.enabled = panel->backlight.level != 0;
1690 int intel_panel_setup_backlight(struct drm_connector *connector, enum pipe pipe)
1692 struct drm_i915_private *dev_priv = to_i915(connector->dev);
1693 struct intel_connector *intel_connector = to_intel_connector(connector);
1694 struct intel_panel *panel = &intel_connector->panel;
1697 if (!dev_priv->vbt.backlight.present) {
1698 if (dev_priv->quirks & QUIRK_BACKLIGHT_PRESENT) {
1699 DRM_DEBUG_KMS("no backlight present per VBT, but present per quirk\n");
1701 DRM_DEBUG_KMS("no backlight present per VBT\n");
1706 /* ensure intel_panel has been initialized first */
1707 if (WARN_ON(!panel->backlight.setup))
1710 /* set level and max in panel struct */
1711 mutex_lock(&dev_priv->backlight_lock);
1712 ret = panel->backlight.setup(intel_connector, pipe);
1713 mutex_unlock(&dev_priv->backlight_lock);
1716 DRM_DEBUG_KMS("failed to setup backlight for connector %s\n",
1721 panel->backlight.present = true;
1723 DRM_DEBUG_KMS("Connector %s backlight initialized, %s, brightness %u/%u\n",
1725 enableddisabled(panel->backlight.enabled),
1726 panel->backlight.level, panel->backlight.max);
1731 void intel_panel_destroy_backlight(struct drm_connector *connector)
1733 struct intel_connector *intel_connector = to_intel_connector(connector);
1734 struct intel_panel *panel = &intel_connector->panel;
1736 /* dispose of the pwm */
1737 if (panel->backlight.pwm)
1738 pwm_put(panel->backlight.pwm);
1740 panel->backlight.present = false;
1743 /* Set up chip specific backlight functions */
1745 intel_panel_init_backlight_funcs(struct intel_panel *panel)
1747 struct intel_connector *connector =
1748 container_of(panel, struct intel_connector, panel);
1749 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1751 if (connector->base.connector_type == DRM_MODE_CONNECTOR_eDP &&
1752 intel_dp_aux_init_backlight_funcs(connector) == 0)
1755 if (connector->base.connector_type == DRM_MODE_CONNECTOR_DSI &&
1756 intel_dsi_dcs_init_backlight_funcs(connector) == 0)
1759 if (IS_BROXTON(dev_priv)) {
1760 panel->backlight.setup = bxt_setup_backlight;
1761 panel->backlight.enable = bxt_enable_backlight;
1762 panel->backlight.disable = bxt_disable_backlight;
1763 panel->backlight.set = bxt_set_backlight;
1764 panel->backlight.get = bxt_get_backlight;
1765 panel->backlight.hz_to_pwm = bxt_hz_to_pwm;
1766 } else if (HAS_PCH_LPT(dev_priv) || HAS_PCH_SPT(dev_priv) ||
1767 HAS_PCH_KBP(dev_priv)) {
1768 panel->backlight.setup = lpt_setup_backlight;
1769 panel->backlight.enable = lpt_enable_backlight;
1770 panel->backlight.disable = lpt_disable_backlight;
1771 panel->backlight.set = lpt_set_backlight;
1772 panel->backlight.get = lpt_get_backlight;
1773 if (HAS_PCH_LPT(dev_priv))
1774 panel->backlight.hz_to_pwm = lpt_hz_to_pwm;
1776 panel->backlight.hz_to_pwm = spt_hz_to_pwm;
1777 } else if (HAS_PCH_SPLIT(dev_priv)) {
1778 panel->backlight.setup = pch_setup_backlight;
1779 panel->backlight.enable = pch_enable_backlight;
1780 panel->backlight.disable = pch_disable_backlight;
1781 panel->backlight.set = pch_set_backlight;
1782 panel->backlight.get = pch_get_backlight;
1783 panel->backlight.hz_to_pwm = pch_hz_to_pwm;
1784 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
1785 if (connector->base.connector_type == DRM_MODE_CONNECTOR_DSI) {
1786 panel->backlight.setup = pwm_setup_backlight;
1787 panel->backlight.enable = pwm_enable_backlight;
1788 panel->backlight.disable = pwm_disable_backlight;
1789 panel->backlight.set = pwm_set_backlight;
1790 panel->backlight.get = pwm_get_backlight;
1792 panel->backlight.setup = vlv_setup_backlight;
1793 panel->backlight.enable = vlv_enable_backlight;
1794 panel->backlight.disable = vlv_disable_backlight;
1795 panel->backlight.set = vlv_set_backlight;
1796 panel->backlight.get = vlv_get_backlight;
1797 panel->backlight.hz_to_pwm = vlv_hz_to_pwm;
1799 } else if (IS_GEN4(dev_priv)) {
1800 panel->backlight.setup = i965_setup_backlight;
1801 panel->backlight.enable = i965_enable_backlight;
1802 panel->backlight.disable = i965_disable_backlight;
1803 panel->backlight.set = i9xx_set_backlight;
1804 panel->backlight.get = i9xx_get_backlight;
1805 panel->backlight.hz_to_pwm = i965_hz_to_pwm;
1807 panel->backlight.setup = i9xx_setup_backlight;
1808 panel->backlight.enable = i9xx_enable_backlight;
1809 panel->backlight.disable = i9xx_disable_backlight;
1810 panel->backlight.set = i9xx_set_backlight;
1811 panel->backlight.get = i9xx_get_backlight;
1812 panel->backlight.hz_to_pwm = i9xx_hz_to_pwm;
1816 int intel_panel_init(struct intel_panel *panel,
1817 struct drm_display_mode *fixed_mode,
1818 struct drm_display_mode *downclock_mode)
1820 intel_panel_init_backlight_funcs(panel);
1822 panel->fixed_mode = fixed_mode;
1823 panel->downclock_mode = downclock_mode;
1828 void intel_panel_fini(struct intel_panel *panel)
1830 struct intel_connector *intel_connector =
1831 container_of(panel, struct intel_connector, panel);
1833 if (panel->fixed_mode)
1834 drm_mode_destroy(intel_connector->base.dev, panel->fixed_mode);
1836 if (panel->downclock_mode)
1837 drm_mode_destroy(intel_connector->base.dev,
1838 panel->downclock_mode);