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
51 * @dev_priv: i915 device instance
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_i915_private *dev_priv,
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_priv->drm, 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 &&
114 !pipe_config->ycbcr420)
117 switch (fitting_mode) {
118 case DRM_MODE_SCALE_CENTER:
119 width = pipe_config->pipe_src_w;
120 height = pipe_config->pipe_src_h;
121 x = (adjusted_mode->crtc_hdisplay - width + 1)/2;
122 y = (adjusted_mode->crtc_vdisplay - height + 1)/2;
125 case DRM_MODE_SCALE_ASPECT:
126 /* Scale but preserve the aspect ratio */
128 u32 scaled_width = adjusted_mode->crtc_hdisplay
129 * pipe_config->pipe_src_h;
130 u32 scaled_height = pipe_config->pipe_src_w
131 * adjusted_mode->crtc_vdisplay;
132 if (scaled_width > scaled_height) { /* pillar */
133 width = scaled_height / pipe_config->pipe_src_h;
136 x = (adjusted_mode->crtc_hdisplay - width + 1) / 2;
138 height = adjusted_mode->crtc_vdisplay;
139 } else if (scaled_width < scaled_height) { /* letter */
140 height = scaled_width / pipe_config->pipe_src_w;
143 y = (adjusted_mode->crtc_vdisplay - height + 1) / 2;
145 width = adjusted_mode->crtc_hdisplay;
148 width = adjusted_mode->crtc_hdisplay;
149 height = adjusted_mode->crtc_vdisplay;
154 case DRM_MODE_SCALE_FULLSCREEN:
156 width = adjusted_mode->crtc_hdisplay;
157 height = adjusted_mode->crtc_vdisplay;
161 WARN(1, "bad panel fit mode: %d\n", fitting_mode);
166 pipe_config->pch_pfit.pos = (x << 16) | y;
167 pipe_config->pch_pfit.size = (width << 16) | height;
168 pipe_config->pch_pfit.enabled = pipe_config->pch_pfit.size != 0;
172 centre_horizontally(struct drm_display_mode *adjusted_mode,
175 u32 border, sync_pos, blank_width, sync_width;
177 /* keep the hsync and hblank widths constant */
178 sync_width = adjusted_mode->crtc_hsync_end - adjusted_mode->crtc_hsync_start;
179 blank_width = adjusted_mode->crtc_hblank_end - adjusted_mode->crtc_hblank_start;
180 sync_pos = (blank_width - sync_width + 1) / 2;
182 border = (adjusted_mode->crtc_hdisplay - width + 1) / 2;
183 border += border & 1; /* make the border even */
185 adjusted_mode->crtc_hdisplay = width;
186 adjusted_mode->crtc_hblank_start = width + border;
187 adjusted_mode->crtc_hblank_end = adjusted_mode->crtc_hblank_start + blank_width;
189 adjusted_mode->crtc_hsync_start = adjusted_mode->crtc_hblank_start + sync_pos;
190 adjusted_mode->crtc_hsync_end = adjusted_mode->crtc_hsync_start + sync_width;
194 centre_vertically(struct drm_display_mode *adjusted_mode,
197 u32 border, sync_pos, blank_width, sync_width;
199 /* keep the vsync and vblank widths constant */
200 sync_width = adjusted_mode->crtc_vsync_end - adjusted_mode->crtc_vsync_start;
201 blank_width = adjusted_mode->crtc_vblank_end - adjusted_mode->crtc_vblank_start;
202 sync_pos = (blank_width - sync_width + 1) / 2;
204 border = (adjusted_mode->crtc_vdisplay - height + 1) / 2;
206 adjusted_mode->crtc_vdisplay = height;
207 adjusted_mode->crtc_vblank_start = height + border;
208 adjusted_mode->crtc_vblank_end = adjusted_mode->crtc_vblank_start + blank_width;
210 adjusted_mode->crtc_vsync_start = adjusted_mode->crtc_vblank_start + sync_pos;
211 adjusted_mode->crtc_vsync_end = adjusted_mode->crtc_vsync_start + sync_width;
214 static inline u32 panel_fitter_scaling(u32 source, u32 target)
217 * Floating point operation is not supported. So the FACTOR
218 * is defined, which can avoid the floating point computation
219 * when calculating the panel ratio.
222 #define FACTOR (1 << ACCURACY)
223 u32 ratio = source * FACTOR / target;
224 return (FACTOR * ratio + FACTOR/2) / FACTOR;
227 static void i965_scale_aspect(struct intel_crtc_state *pipe_config,
230 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
231 u32 scaled_width = adjusted_mode->crtc_hdisplay *
232 pipe_config->pipe_src_h;
233 u32 scaled_height = pipe_config->pipe_src_w *
234 adjusted_mode->crtc_vdisplay;
236 /* 965+ is easy, it does everything in hw */
237 if (scaled_width > scaled_height)
238 *pfit_control |= PFIT_ENABLE |
240 else if (scaled_width < scaled_height)
241 *pfit_control |= PFIT_ENABLE |
243 else if (adjusted_mode->crtc_hdisplay != pipe_config->pipe_src_w)
244 *pfit_control |= PFIT_ENABLE | PFIT_SCALING_AUTO;
247 static void i9xx_scale_aspect(struct intel_crtc_state *pipe_config,
248 u32 *pfit_control, u32 *pfit_pgm_ratios,
251 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
252 u32 scaled_width = adjusted_mode->crtc_hdisplay *
253 pipe_config->pipe_src_h;
254 u32 scaled_height = pipe_config->pipe_src_w *
255 adjusted_mode->crtc_vdisplay;
259 * For earlier chips we have to calculate the scaling
260 * ratio by hand and program it into the
261 * PFIT_PGM_RATIO register
263 if (scaled_width > scaled_height) { /* pillar */
264 centre_horizontally(adjusted_mode,
266 pipe_config->pipe_src_h);
268 *border = LVDS_BORDER_ENABLE;
269 if (pipe_config->pipe_src_h != adjusted_mode->crtc_vdisplay) {
270 bits = panel_fitter_scaling(pipe_config->pipe_src_h,
271 adjusted_mode->crtc_vdisplay);
273 *pfit_pgm_ratios |= (bits << PFIT_HORIZ_SCALE_SHIFT |
274 bits << PFIT_VERT_SCALE_SHIFT);
275 *pfit_control |= (PFIT_ENABLE |
276 VERT_INTERP_BILINEAR |
277 HORIZ_INTERP_BILINEAR);
279 } else if (scaled_width < scaled_height) { /* letter */
280 centre_vertically(adjusted_mode,
282 pipe_config->pipe_src_w);
284 *border = LVDS_BORDER_ENABLE;
285 if (pipe_config->pipe_src_w != adjusted_mode->crtc_hdisplay) {
286 bits = panel_fitter_scaling(pipe_config->pipe_src_w,
287 adjusted_mode->crtc_hdisplay);
289 *pfit_pgm_ratios |= (bits << PFIT_HORIZ_SCALE_SHIFT |
290 bits << PFIT_VERT_SCALE_SHIFT);
291 *pfit_control |= (PFIT_ENABLE |
292 VERT_INTERP_BILINEAR |
293 HORIZ_INTERP_BILINEAR);
296 /* Aspects match, Let hw scale both directions */
297 *pfit_control |= (PFIT_ENABLE |
298 VERT_AUTO_SCALE | HORIZ_AUTO_SCALE |
299 VERT_INTERP_BILINEAR |
300 HORIZ_INTERP_BILINEAR);
304 void intel_gmch_panel_fitting(struct intel_crtc *intel_crtc,
305 struct intel_crtc_state *pipe_config,
308 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
309 u32 pfit_control = 0, pfit_pgm_ratios = 0, border = 0;
310 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
312 /* Native modes don't need fitting */
313 if (adjusted_mode->crtc_hdisplay == pipe_config->pipe_src_w &&
314 adjusted_mode->crtc_vdisplay == pipe_config->pipe_src_h)
317 switch (fitting_mode) {
318 case DRM_MODE_SCALE_CENTER:
320 * For centered modes, we have to calculate border widths &
321 * heights and modify the values programmed into the CRTC.
323 centre_horizontally(adjusted_mode, pipe_config->pipe_src_w);
324 centre_vertically(adjusted_mode, pipe_config->pipe_src_h);
325 border = LVDS_BORDER_ENABLE;
327 case DRM_MODE_SCALE_ASPECT:
328 /* Scale but preserve the aspect ratio */
329 if (INTEL_GEN(dev_priv) >= 4)
330 i965_scale_aspect(pipe_config, &pfit_control);
332 i9xx_scale_aspect(pipe_config, &pfit_control,
333 &pfit_pgm_ratios, &border);
335 case DRM_MODE_SCALE_FULLSCREEN:
337 * Full scaling, even if it changes the aspect ratio.
338 * Fortunately this is all done for us in hw.
340 if (pipe_config->pipe_src_h != adjusted_mode->crtc_vdisplay ||
341 pipe_config->pipe_src_w != adjusted_mode->crtc_hdisplay) {
342 pfit_control |= PFIT_ENABLE;
343 if (INTEL_GEN(dev_priv) >= 4)
344 pfit_control |= PFIT_SCALING_AUTO;
346 pfit_control |= (VERT_AUTO_SCALE |
347 VERT_INTERP_BILINEAR |
349 HORIZ_INTERP_BILINEAR);
353 WARN(1, "bad panel fit mode: %d\n", fitting_mode);
357 /* 965+ wants fuzzy fitting */
358 /* FIXME: handle multiple panels by failing gracefully */
359 if (INTEL_GEN(dev_priv) >= 4)
360 pfit_control |= ((intel_crtc->pipe << PFIT_PIPE_SHIFT) |
364 if ((pfit_control & PFIT_ENABLE) == 0) {
369 /* Make sure pre-965 set dither correctly for 18bpp panels. */
370 if (INTEL_GEN(dev_priv) < 4 && pipe_config->pipe_bpp == 18)
371 pfit_control |= PANEL_8TO6_DITHER_ENABLE;
373 pipe_config->gmch_pfit.control = pfit_control;
374 pipe_config->gmch_pfit.pgm_ratios = pfit_pgm_ratios;
375 pipe_config->gmch_pfit.lvds_border_bits = border;
378 enum drm_connector_status
379 intel_panel_detect(struct drm_i915_private *dev_priv)
381 /* Assume that the BIOS does not lie through the OpRegion... */
382 if (!i915_modparams.panel_ignore_lid && dev_priv->opregion.lid_state) {
383 return *dev_priv->opregion.lid_state & 0x1 ?
384 connector_status_connected :
385 connector_status_disconnected;
388 switch (i915_modparams.panel_ignore_lid) {
390 return connector_status_connected;
392 return connector_status_disconnected;
394 return connector_status_unknown;
399 * scale - scale values from one range to another
400 * @source_val: value in range [@source_min..@source_max]
401 * @source_min: minimum legal value for @source_val
402 * @source_max: maximum legal value for @source_val
403 * @target_min: corresponding target value for @source_min
404 * @target_max: corresponding target value for @source_max
406 * Return @source_val in range [@source_min..@source_max] scaled to range
407 * [@target_min..@target_max].
409 static uint32_t scale(uint32_t source_val,
410 uint32_t source_min, uint32_t source_max,
411 uint32_t target_min, uint32_t target_max)
415 WARN_ON(source_min > source_max);
416 WARN_ON(target_min > target_max);
419 source_val = clamp(source_val, source_min, source_max);
421 /* avoid overflows */
422 target_val = mul_u32_u32(source_val - source_min,
423 target_max - target_min);
424 target_val = DIV_ROUND_CLOSEST_ULL(target_val, source_max - source_min);
425 target_val += target_min;
430 /* Scale user_level in range [0..user_max] to [hw_min..hw_max]. */
431 static inline u32 scale_user_to_hw(struct intel_connector *connector,
432 u32 user_level, u32 user_max)
434 struct intel_panel *panel = &connector->panel;
436 return scale(user_level, 0, user_max,
437 panel->backlight.min, panel->backlight.max);
440 /* Scale user_level in range [0..user_max] to [0..hw_max], clamping the result
441 * to [hw_min..hw_max]. */
442 static inline u32 clamp_user_to_hw(struct intel_connector *connector,
443 u32 user_level, u32 user_max)
445 struct intel_panel *panel = &connector->panel;
448 hw_level = scale(user_level, 0, user_max, 0, panel->backlight.max);
449 hw_level = clamp(hw_level, panel->backlight.min, panel->backlight.max);
454 /* Scale hw_level in range [hw_min..hw_max] to [0..user_max]. */
455 static inline u32 scale_hw_to_user(struct intel_connector *connector,
456 u32 hw_level, u32 user_max)
458 struct intel_panel *panel = &connector->panel;
460 return scale(hw_level, panel->backlight.min, panel->backlight.max,
464 static u32 intel_panel_compute_brightness(struct intel_connector *connector,
467 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
468 struct intel_panel *panel = &connector->panel;
470 WARN_ON(panel->backlight.max == 0);
472 if (i915_modparams.invert_brightness < 0)
475 if (i915_modparams.invert_brightness > 0 ||
476 dev_priv->quirks & QUIRK_INVERT_BRIGHTNESS) {
477 return panel->backlight.max - val + panel->backlight.min;
483 static u32 lpt_get_backlight(struct intel_connector *connector)
485 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
487 return I915_READ(BLC_PWM_PCH_CTL2) & BACKLIGHT_DUTY_CYCLE_MASK;
490 static u32 pch_get_backlight(struct intel_connector *connector)
492 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
494 return I915_READ(BLC_PWM_CPU_CTL) & BACKLIGHT_DUTY_CYCLE_MASK;
497 static u32 i9xx_get_backlight(struct intel_connector *connector)
499 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
500 struct intel_panel *panel = &connector->panel;
503 val = I915_READ(BLC_PWM_CTL) & BACKLIGHT_DUTY_CYCLE_MASK;
504 if (INTEL_GEN(dev_priv) < 4)
507 if (panel->backlight.combination_mode) {
510 pci_read_config_byte(dev_priv->drm.pdev, LBPC, &lbpc);
517 static u32 _vlv_get_backlight(struct drm_i915_private *dev_priv, enum pipe pipe)
519 if (WARN_ON(pipe != PIPE_A && pipe != PIPE_B))
522 return I915_READ(VLV_BLC_PWM_CTL(pipe)) & BACKLIGHT_DUTY_CYCLE_MASK;
525 static u32 vlv_get_backlight(struct intel_connector *connector)
527 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
528 enum pipe pipe = intel_get_pipe_from_connector(connector);
530 return _vlv_get_backlight(dev_priv, pipe);
533 static u32 bxt_get_backlight(struct intel_connector *connector)
535 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
536 struct intel_panel *panel = &connector->panel;
538 return I915_READ(BXT_BLC_PWM_DUTY(panel->backlight.controller));
541 static u32 pwm_get_backlight(struct intel_connector *connector)
543 struct intel_panel *panel = &connector->panel;
546 duty_ns = pwm_get_duty_cycle(panel->backlight.pwm);
547 return DIV_ROUND_UP(duty_ns * 100, CRC_PMIC_PWM_PERIOD_NS);
550 static void lpt_set_backlight(const struct drm_connector_state *conn_state, u32 level)
552 struct intel_connector *connector = to_intel_connector(conn_state->connector);
553 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
555 u32 val = I915_READ(BLC_PWM_PCH_CTL2) & ~BACKLIGHT_DUTY_CYCLE_MASK;
556 I915_WRITE(BLC_PWM_PCH_CTL2, val | level);
559 static void pch_set_backlight(const struct drm_connector_state *conn_state, u32 level)
561 struct intel_connector *connector = to_intel_connector(conn_state->connector);
562 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
565 tmp = I915_READ(BLC_PWM_CPU_CTL) & ~BACKLIGHT_DUTY_CYCLE_MASK;
566 I915_WRITE(BLC_PWM_CPU_CTL, tmp | level);
569 static void i9xx_set_backlight(const struct drm_connector_state *conn_state, u32 level)
571 struct intel_connector *connector = to_intel_connector(conn_state->connector);
572 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
573 struct intel_panel *panel = &connector->panel;
576 WARN_ON(panel->backlight.max == 0);
578 if (panel->backlight.combination_mode) {
581 lbpc = level * 0xfe / panel->backlight.max + 1;
583 pci_write_config_byte(dev_priv->drm.pdev, LBPC, lbpc);
586 if (IS_GEN4(dev_priv)) {
587 mask = BACKLIGHT_DUTY_CYCLE_MASK;
590 mask = BACKLIGHT_DUTY_CYCLE_MASK_PNV;
593 tmp = I915_READ(BLC_PWM_CTL) & ~mask;
594 I915_WRITE(BLC_PWM_CTL, tmp | level);
597 static void vlv_set_backlight(const struct drm_connector_state *conn_state, u32 level)
599 struct intel_connector *connector = to_intel_connector(conn_state->connector);
600 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
601 enum pipe pipe = to_intel_crtc(conn_state->crtc)->pipe;
604 tmp = I915_READ(VLV_BLC_PWM_CTL(pipe)) & ~BACKLIGHT_DUTY_CYCLE_MASK;
605 I915_WRITE(VLV_BLC_PWM_CTL(pipe), tmp | level);
608 static void bxt_set_backlight(const struct drm_connector_state *conn_state, u32 level)
610 struct intel_connector *connector = to_intel_connector(conn_state->connector);
611 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
612 struct intel_panel *panel = &connector->panel;
614 I915_WRITE(BXT_BLC_PWM_DUTY(panel->backlight.controller), level);
617 static void pwm_set_backlight(const struct drm_connector_state *conn_state, u32 level)
619 struct intel_panel *panel = &to_intel_connector(conn_state->connector)->panel;
620 int duty_ns = DIV_ROUND_UP(level * CRC_PMIC_PWM_PERIOD_NS, 100);
622 pwm_config(panel->backlight.pwm, duty_ns, CRC_PMIC_PWM_PERIOD_NS);
626 intel_panel_actually_set_backlight(const struct drm_connector_state *conn_state, u32 level)
628 struct intel_connector *connector = to_intel_connector(conn_state->connector);
629 struct intel_panel *panel = &connector->panel;
631 DRM_DEBUG_DRIVER("set backlight PWM = %d\n", level);
633 level = intel_panel_compute_brightness(connector, level);
634 panel->backlight.set(conn_state, level);
637 /* set backlight brightness to level in range [0..max], assuming hw min is
640 void intel_panel_set_backlight_acpi(const struct drm_connector_state *conn_state,
641 u32 user_level, u32 user_max)
643 struct intel_connector *connector = to_intel_connector(conn_state->connector);
644 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
645 struct intel_panel *panel = &connector->panel;
649 * Lack of crtc may occur during driver init because
650 * connection_mutex isn't held across the entire backlight
651 * setup + modeset readout, and the BIOS can issue the
652 * requests at any time.
654 if (!panel->backlight.present || !conn_state->crtc)
657 mutex_lock(&dev_priv->backlight_lock);
659 WARN_ON(panel->backlight.max == 0);
661 hw_level = clamp_user_to_hw(connector, user_level, user_max);
662 panel->backlight.level = hw_level;
664 if (panel->backlight.device)
665 panel->backlight.device->props.brightness =
666 scale_hw_to_user(connector,
667 panel->backlight.level,
668 panel->backlight.device->props.max_brightness);
670 if (panel->backlight.enabled)
671 intel_panel_actually_set_backlight(conn_state, hw_level);
673 mutex_unlock(&dev_priv->backlight_lock);
676 static void lpt_disable_backlight(const struct drm_connector_state *old_conn_state)
678 struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
679 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
682 intel_panel_actually_set_backlight(old_conn_state, 0);
685 * Although we don't support or enable CPU PWM with LPT/SPT based
686 * systems, it may have been enabled prior to loading the
687 * driver. Disable to avoid warnings on LCPLL disable.
689 * This needs rework if we need to add support for CPU PWM on PCH split
692 tmp = I915_READ(BLC_PWM_CPU_CTL2);
693 if (tmp & BLM_PWM_ENABLE) {
694 DRM_DEBUG_KMS("cpu backlight was enabled, disabling\n");
695 I915_WRITE(BLC_PWM_CPU_CTL2, tmp & ~BLM_PWM_ENABLE);
698 tmp = I915_READ(BLC_PWM_PCH_CTL1);
699 I915_WRITE(BLC_PWM_PCH_CTL1, tmp & ~BLM_PCH_PWM_ENABLE);
702 static void pch_disable_backlight(const struct drm_connector_state *old_conn_state)
704 struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
705 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
708 intel_panel_actually_set_backlight(old_conn_state, 0);
710 tmp = I915_READ(BLC_PWM_CPU_CTL2);
711 I915_WRITE(BLC_PWM_CPU_CTL2, tmp & ~BLM_PWM_ENABLE);
713 tmp = I915_READ(BLC_PWM_PCH_CTL1);
714 I915_WRITE(BLC_PWM_PCH_CTL1, tmp & ~BLM_PCH_PWM_ENABLE);
717 static void i9xx_disable_backlight(const struct drm_connector_state *old_conn_state)
719 intel_panel_actually_set_backlight(old_conn_state, 0);
722 static void i965_disable_backlight(const struct drm_connector_state *old_conn_state)
724 struct drm_i915_private *dev_priv = to_i915(old_conn_state->connector->dev);
727 intel_panel_actually_set_backlight(old_conn_state, 0);
729 tmp = I915_READ(BLC_PWM_CTL2);
730 I915_WRITE(BLC_PWM_CTL2, tmp & ~BLM_PWM_ENABLE);
733 static void vlv_disable_backlight(const struct drm_connector_state *old_conn_state)
735 struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
736 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
737 enum pipe pipe = to_intel_crtc(old_conn_state->crtc)->pipe;
740 intel_panel_actually_set_backlight(old_conn_state, 0);
742 tmp = I915_READ(VLV_BLC_PWM_CTL2(pipe));
743 I915_WRITE(VLV_BLC_PWM_CTL2(pipe), tmp & ~BLM_PWM_ENABLE);
746 static void bxt_disable_backlight(const struct drm_connector_state *old_conn_state)
748 struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
749 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
750 struct intel_panel *panel = &connector->panel;
753 intel_panel_actually_set_backlight(old_conn_state, 0);
755 tmp = I915_READ(BXT_BLC_PWM_CTL(panel->backlight.controller));
756 I915_WRITE(BXT_BLC_PWM_CTL(panel->backlight.controller),
757 tmp & ~BXT_BLC_PWM_ENABLE);
759 if (panel->backlight.controller == 1) {
760 val = I915_READ(UTIL_PIN_CTL);
761 val &= ~UTIL_PIN_ENABLE;
762 I915_WRITE(UTIL_PIN_CTL, val);
766 static void cnp_disable_backlight(const struct drm_connector_state *old_conn_state)
768 struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
769 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
770 struct intel_panel *panel = &connector->panel;
773 intel_panel_actually_set_backlight(old_conn_state, 0);
775 tmp = I915_READ(BXT_BLC_PWM_CTL(panel->backlight.controller));
776 I915_WRITE(BXT_BLC_PWM_CTL(panel->backlight.controller),
777 tmp & ~BXT_BLC_PWM_ENABLE);
780 static void pwm_disable_backlight(const struct drm_connector_state *old_conn_state)
782 struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
783 struct intel_panel *panel = &connector->panel;
785 /* Disable the backlight */
786 pwm_config(panel->backlight.pwm, 0, CRC_PMIC_PWM_PERIOD_NS);
787 usleep_range(2000, 3000);
788 pwm_disable(panel->backlight.pwm);
791 void intel_panel_disable_backlight(const struct drm_connector_state *old_conn_state)
793 struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
794 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
795 struct intel_panel *panel = &connector->panel;
797 if (!panel->backlight.present)
801 * Do not disable backlight on the vga_switcheroo path. When switching
802 * away from i915, the other client may depend on i915 to handle the
803 * backlight. This will leave the backlight on unnecessarily when
804 * another client is not activated.
806 if (dev_priv->drm.switch_power_state == DRM_SWITCH_POWER_CHANGING) {
807 DRM_DEBUG_DRIVER("Skipping backlight disable on vga switch\n");
811 mutex_lock(&dev_priv->backlight_lock);
813 if (panel->backlight.device)
814 panel->backlight.device->props.power = FB_BLANK_POWERDOWN;
815 panel->backlight.enabled = false;
816 panel->backlight.disable(old_conn_state);
818 mutex_unlock(&dev_priv->backlight_lock);
821 static void lpt_enable_backlight(const struct intel_crtc_state *crtc_state,
822 const struct drm_connector_state *conn_state)
824 struct intel_connector *connector = to_intel_connector(conn_state->connector);
825 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
826 struct intel_panel *panel = &connector->panel;
827 u32 pch_ctl1, pch_ctl2, schicken;
829 pch_ctl1 = I915_READ(BLC_PWM_PCH_CTL1);
830 if (pch_ctl1 & BLM_PCH_PWM_ENABLE) {
831 DRM_DEBUG_KMS("pch backlight already enabled\n");
832 pch_ctl1 &= ~BLM_PCH_PWM_ENABLE;
833 I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1);
836 if (HAS_PCH_LPT(dev_priv)) {
837 schicken = I915_READ(SOUTH_CHICKEN2);
838 if (panel->backlight.alternate_pwm_increment)
839 schicken |= LPT_PWM_GRANULARITY;
841 schicken &= ~LPT_PWM_GRANULARITY;
842 I915_WRITE(SOUTH_CHICKEN2, schicken);
844 schicken = I915_READ(SOUTH_CHICKEN1);
845 if (panel->backlight.alternate_pwm_increment)
846 schicken |= SPT_PWM_GRANULARITY;
848 schicken &= ~SPT_PWM_GRANULARITY;
849 I915_WRITE(SOUTH_CHICKEN1, schicken);
852 pch_ctl2 = panel->backlight.max << 16;
853 I915_WRITE(BLC_PWM_PCH_CTL2, pch_ctl2);
856 if (panel->backlight.active_low_pwm)
857 pch_ctl1 |= BLM_PCH_POLARITY;
859 /* After LPT, override is the default. */
860 if (HAS_PCH_LPT(dev_priv))
861 pch_ctl1 |= BLM_PCH_OVERRIDE_ENABLE;
863 I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1);
864 POSTING_READ(BLC_PWM_PCH_CTL1);
865 I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1 | BLM_PCH_PWM_ENABLE);
867 /* This won't stick until the above enable. */
868 intel_panel_actually_set_backlight(conn_state, panel->backlight.level);
871 static void pch_enable_backlight(const struct intel_crtc_state *crtc_state,
872 const struct drm_connector_state *conn_state)
874 struct intel_connector *connector = to_intel_connector(conn_state->connector);
875 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
876 struct intel_panel *panel = &connector->panel;
877 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
878 u32 cpu_ctl2, pch_ctl1, pch_ctl2;
880 cpu_ctl2 = I915_READ(BLC_PWM_CPU_CTL2);
881 if (cpu_ctl2 & BLM_PWM_ENABLE) {
882 DRM_DEBUG_KMS("cpu backlight already enabled\n");
883 cpu_ctl2 &= ~BLM_PWM_ENABLE;
884 I915_WRITE(BLC_PWM_CPU_CTL2, cpu_ctl2);
887 pch_ctl1 = I915_READ(BLC_PWM_PCH_CTL1);
888 if (pch_ctl1 & BLM_PCH_PWM_ENABLE) {
889 DRM_DEBUG_KMS("pch backlight already enabled\n");
890 pch_ctl1 &= ~BLM_PCH_PWM_ENABLE;
891 I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1);
894 if (cpu_transcoder == TRANSCODER_EDP)
895 cpu_ctl2 = BLM_TRANSCODER_EDP;
897 cpu_ctl2 = BLM_PIPE(cpu_transcoder);
898 I915_WRITE(BLC_PWM_CPU_CTL2, cpu_ctl2);
899 POSTING_READ(BLC_PWM_CPU_CTL2);
900 I915_WRITE(BLC_PWM_CPU_CTL2, cpu_ctl2 | BLM_PWM_ENABLE);
902 /* This won't stick until the above enable. */
903 intel_panel_actually_set_backlight(conn_state, panel->backlight.level);
905 pch_ctl2 = panel->backlight.max << 16;
906 I915_WRITE(BLC_PWM_PCH_CTL2, pch_ctl2);
909 if (panel->backlight.active_low_pwm)
910 pch_ctl1 |= BLM_PCH_POLARITY;
912 I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1);
913 POSTING_READ(BLC_PWM_PCH_CTL1);
914 I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1 | BLM_PCH_PWM_ENABLE);
917 static void i9xx_enable_backlight(const struct intel_crtc_state *crtc_state,
918 const struct drm_connector_state *conn_state)
920 struct intel_connector *connector = to_intel_connector(conn_state->connector);
921 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
922 struct intel_panel *panel = &connector->panel;
925 ctl = I915_READ(BLC_PWM_CTL);
926 if (ctl & BACKLIGHT_DUTY_CYCLE_MASK_PNV) {
927 DRM_DEBUG_KMS("backlight already enabled\n");
928 I915_WRITE(BLC_PWM_CTL, 0);
931 freq = panel->backlight.max;
932 if (panel->backlight.combination_mode)
936 if (panel->backlight.combination_mode)
937 ctl |= BLM_LEGACY_MODE;
938 if (IS_PINEVIEW(dev_priv) && panel->backlight.active_low_pwm)
939 ctl |= BLM_POLARITY_PNV;
941 I915_WRITE(BLC_PWM_CTL, ctl);
942 POSTING_READ(BLC_PWM_CTL);
944 /* XXX: combine this into above write? */
945 intel_panel_actually_set_backlight(conn_state, panel->backlight.level);
948 * Needed to enable backlight on some 855gm models. BLC_HIST_CTL is
949 * 855gm only, but checking for gen2 is safe, as 855gm is the only gen2
950 * that has backlight.
952 if (IS_GEN2(dev_priv))
953 I915_WRITE(BLC_HIST_CTL, BLM_HISTOGRAM_ENABLE);
956 static void i965_enable_backlight(const struct intel_crtc_state *crtc_state,
957 const struct drm_connector_state *conn_state)
959 struct intel_connector *connector = to_intel_connector(conn_state->connector);
960 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
961 struct intel_panel *panel = &connector->panel;
962 enum pipe pipe = to_intel_crtc(conn_state->crtc)->pipe;
965 ctl2 = I915_READ(BLC_PWM_CTL2);
966 if (ctl2 & BLM_PWM_ENABLE) {
967 DRM_DEBUG_KMS("backlight already enabled\n");
968 ctl2 &= ~BLM_PWM_ENABLE;
969 I915_WRITE(BLC_PWM_CTL2, ctl2);
972 freq = panel->backlight.max;
973 if (panel->backlight.combination_mode)
977 I915_WRITE(BLC_PWM_CTL, ctl);
979 ctl2 = BLM_PIPE(pipe);
980 if (panel->backlight.combination_mode)
981 ctl2 |= BLM_COMBINATION_MODE;
982 if (panel->backlight.active_low_pwm)
983 ctl2 |= BLM_POLARITY_I965;
984 I915_WRITE(BLC_PWM_CTL2, ctl2);
985 POSTING_READ(BLC_PWM_CTL2);
986 I915_WRITE(BLC_PWM_CTL2, ctl2 | BLM_PWM_ENABLE);
988 intel_panel_actually_set_backlight(conn_state, panel->backlight.level);
991 static void vlv_enable_backlight(const struct intel_crtc_state *crtc_state,
992 const struct drm_connector_state *conn_state)
994 struct intel_connector *connector = to_intel_connector(conn_state->connector);
995 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
996 struct intel_panel *panel = &connector->panel;
997 enum pipe pipe = to_intel_crtc(crtc_state->base.crtc)->pipe;
1000 ctl2 = I915_READ(VLV_BLC_PWM_CTL2(pipe));
1001 if (ctl2 & BLM_PWM_ENABLE) {
1002 DRM_DEBUG_KMS("backlight already enabled\n");
1003 ctl2 &= ~BLM_PWM_ENABLE;
1004 I915_WRITE(VLV_BLC_PWM_CTL2(pipe), ctl2);
1007 ctl = panel->backlight.max << 16;
1008 I915_WRITE(VLV_BLC_PWM_CTL(pipe), ctl);
1010 /* XXX: combine this into above write? */
1011 intel_panel_actually_set_backlight(conn_state, panel->backlight.level);
1014 if (panel->backlight.active_low_pwm)
1015 ctl2 |= BLM_POLARITY_I965;
1016 I915_WRITE(VLV_BLC_PWM_CTL2(pipe), ctl2);
1017 POSTING_READ(VLV_BLC_PWM_CTL2(pipe));
1018 I915_WRITE(VLV_BLC_PWM_CTL2(pipe), ctl2 | BLM_PWM_ENABLE);
1021 static void bxt_enable_backlight(const struct intel_crtc_state *crtc_state,
1022 const struct drm_connector_state *conn_state)
1024 struct intel_connector *connector = to_intel_connector(conn_state->connector);
1025 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1026 struct intel_panel *panel = &connector->panel;
1027 enum pipe pipe = to_intel_crtc(crtc_state->base.crtc)->pipe;
1030 /* Controller 1 uses the utility pin. */
1031 if (panel->backlight.controller == 1) {
1032 val = I915_READ(UTIL_PIN_CTL);
1033 if (val & UTIL_PIN_ENABLE) {
1034 DRM_DEBUG_KMS("util pin already enabled\n");
1035 val &= ~UTIL_PIN_ENABLE;
1036 I915_WRITE(UTIL_PIN_CTL, val);
1040 if (panel->backlight.util_pin_active_low)
1041 val |= UTIL_PIN_POLARITY;
1042 I915_WRITE(UTIL_PIN_CTL, val | UTIL_PIN_PIPE(pipe) |
1043 UTIL_PIN_MODE_PWM | UTIL_PIN_ENABLE);
1046 pwm_ctl = I915_READ(BXT_BLC_PWM_CTL(panel->backlight.controller));
1047 if (pwm_ctl & BXT_BLC_PWM_ENABLE) {
1048 DRM_DEBUG_KMS("backlight already enabled\n");
1049 pwm_ctl &= ~BXT_BLC_PWM_ENABLE;
1050 I915_WRITE(BXT_BLC_PWM_CTL(panel->backlight.controller),
1054 I915_WRITE(BXT_BLC_PWM_FREQ(panel->backlight.controller),
1055 panel->backlight.max);
1057 intel_panel_actually_set_backlight(conn_state, panel->backlight.level);
1060 if (panel->backlight.active_low_pwm)
1061 pwm_ctl |= BXT_BLC_PWM_POLARITY;
1063 I915_WRITE(BXT_BLC_PWM_CTL(panel->backlight.controller), pwm_ctl);
1064 POSTING_READ(BXT_BLC_PWM_CTL(panel->backlight.controller));
1065 I915_WRITE(BXT_BLC_PWM_CTL(panel->backlight.controller),
1066 pwm_ctl | BXT_BLC_PWM_ENABLE);
1069 static void cnp_enable_backlight(const struct intel_crtc_state *crtc_state,
1070 const struct drm_connector_state *conn_state)
1072 struct intel_connector *connector = to_intel_connector(conn_state->connector);
1073 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1074 struct intel_panel *panel = &connector->panel;
1077 pwm_ctl = I915_READ(BXT_BLC_PWM_CTL(panel->backlight.controller));
1078 if (pwm_ctl & BXT_BLC_PWM_ENABLE) {
1079 DRM_DEBUG_KMS("backlight already enabled\n");
1080 pwm_ctl &= ~BXT_BLC_PWM_ENABLE;
1081 I915_WRITE(BXT_BLC_PWM_CTL(panel->backlight.controller),
1085 I915_WRITE(BXT_BLC_PWM_FREQ(panel->backlight.controller),
1086 panel->backlight.max);
1088 intel_panel_actually_set_backlight(conn_state, panel->backlight.level);
1091 if (panel->backlight.active_low_pwm)
1092 pwm_ctl |= BXT_BLC_PWM_POLARITY;
1094 I915_WRITE(BXT_BLC_PWM_CTL(panel->backlight.controller), pwm_ctl);
1095 POSTING_READ(BXT_BLC_PWM_CTL(panel->backlight.controller));
1096 I915_WRITE(BXT_BLC_PWM_CTL(panel->backlight.controller),
1097 pwm_ctl | BXT_BLC_PWM_ENABLE);
1100 static void pwm_enable_backlight(const struct intel_crtc_state *crtc_state,
1101 const struct drm_connector_state *conn_state)
1103 struct intel_connector *connector = to_intel_connector(conn_state->connector);
1104 struct intel_panel *panel = &connector->panel;
1106 pwm_enable(panel->backlight.pwm);
1107 intel_panel_actually_set_backlight(conn_state, panel->backlight.level);
1110 void intel_panel_enable_backlight(const struct intel_crtc_state *crtc_state,
1111 const struct drm_connector_state *conn_state)
1113 struct intel_connector *connector = to_intel_connector(conn_state->connector);
1114 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1115 struct intel_panel *panel = &connector->panel;
1116 enum pipe pipe = to_intel_crtc(crtc_state->base.crtc)->pipe;
1118 if (!panel->backlight.present)
1121 DRM_DEBUG_KMS("pipe %c\n", pipe_name(pipe));
1123 mutex_lock(&dev_priv->backlight_lock);
1125 WARN_ON(panel->backlight.max == 0);
1127 if (panel->backlight.level <= panel->backlight.min) {
1128 panel->backlight.level = panel->backlight.max;
1129 if (panel->backlight.device)
1130 panel->backlight.device->props.brightness =
1131 scale_hw_to_user(connector,
1132 panel->backlight.level,
1133 panel->backlight.device->props.max_brightness);
1136 panel->backlight.enable(crtc_state, conn_state);
1137 panel->backlight.enabled = true;
1138 if (panel->backlight.device)
1139 panel->backlight.device->props.power = FB_BLANK_UNBLANK;
1141 mutex_unlock(&dev_priv->backlight_lock);
1144 #if IS_ENABLED(CONFIG_BACKLIGHT_CLASS_DEVICE)
1145 static u32 intel_panel_get_backlight(struct intel_connector *connector)
1147 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1148 struct intel_panel *panel = &connector->panel;
1151 mutex_lock(&dev_priv->backlight_lock);
1153 if (panel->backlight.enabled) {
1154 val = panel->backlight.get(connector);
1155 val = intel_panel_compute_brightness(connector, val);
1158 mutex_unlock(&dev_priv->backlight_lock);
1160 DRM_DEBUG_DRIVER("get backlight PWM = %d\n", val);
1164 /* set backlight brightness to level in range [0..max], scaling wrt hw min */
1165 static void intel_panel_set_backlight(const struct drm_connector_state *conn_state,
1166 u32 user_level, u32 user_max)
1168 struct intel_connector *connector = to_intel_connector(conn_state->connector);
1169 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1170 struct intel_panel *panel = &connector->panel;
1173 if (!panel->backlight.present)
1176 mutex_lock(&dev_priv->backlight_lock);
1178 WARN_ON(panel->backlight.max == 0);
1180 hw_level = scale_user_to_hw(connector, user_level, user_max);
1181 panel->backlight.level = hw_level;
1183 if (panel->backlight.enabled)
1184 intel_panel_actually_set_backlight(conn_state, hw_level);
1186 mutex_unlock(&dev_priv->backlight_lock);
1189 static int intel_backlight_device_update_status(struct backlight_device *bd)
1191 struct intel_connector *connector = bl_get_data(bd);
1192 struct intel_panel *panel = &connector->panel;
1193 struct drm_device *dev = connector->base.dev;
1195 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1196 DRM_DEBUG_KMS("updating intel_backlight, brightness=%d/%d\n",
1197 bd->props.brightness, bd->props.max_brightness);
1198 intel_panel_set_backlight(connector->base.state, bd->props.brightness,
1199 bd->props.max_brightness);
1202 * Allow flipping bl_power as a sub-state of enabled. Sadly the
1203 * backlight class device does not make it easy to to differentiate
1204 * between callbacks for brightness and bl_power, so our backlight_power
1205 * callback needs to take this into account.
1207 if (panel->backlight.enabled) {
1208 if (panel->backlight.power) {
1209 bool enable = bd->props.power == FB_BLANK_UNBLANK &&
1210 bd->props.brightness != 0;
1211 panel->backlight.power(connector, enable);
1214 bd->props.power = FB_BLANK_POWERDOWN;
1217 drm_modeset_unlock(&dev->mode_config.connection_mutex);
1221 static int intel_backlight_device_get_brightness(struct backlight_device *bd)
1223 struct intel_connector *connector = bl_get_data(bd);
1224 struct drm_device *dev = connector->base.dev;
1225 struct drm_i915_private *dev_priv = to_i915(dev);
1229 intel_runtime_pm_get(dev_priv);
1230 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1232 hw_level = intel_panel_get_backlight(connector);
1233 ret = scale_hw_to_user(connector, hw_level, bd->props.max_brightness);
1235 drm_modeset_unlock(&dev->mode_config.connection_mutex);
1236 intel_runtime_pm_put(dev_priv);
1241 static const struct backlight_ops intel_backlight_device_ops = {
1242 .update_status = intel_backlight_device_update_status,
1243 .get_brightness = intel_backlight_device_get_brightness,
1246 int intel_backlight_device_register(struct intel_connector *connector)
1248 struct intel_panel *panel = &connector->panel;
1249 struct backlight_properties props;
1251 if (WARN_ON(panel->backlight.device))
1254 if (!panel->backlight.present)
1257 WARN_ON(panel->backlight.max == 0);
1259 memset(&props, 0, sizeof(props));
1260 props.type = BACKLIGHT_RAW;
1263 * Note: Everything should work even if the backlight device max
1264 * presented to the userspace is arbitrarily chosen.
1266 props.max_brightness = panel->backlight.max;
1267 props.brightness = scale_hw_to_user(connector,
1268 panel->backlight.level,
1269 props.max_brightness);
1271 if (panel->backlight.enabled)
1272 props.power = FB_BLANK_UNBLANK;
1274 props.power = FB_BLANK_POWERDOWN;
1277 * Note: using the same name independent of the connector prevents
1278 * registration of multiple backlight devices in the driver.
1280 panel->backlight.device =
1281 backlight_device_register("intel_backlight",
1282 connector->base.kdev,
1284 &intel_backlight_device_ops, &props);
1286 if (IS_ERR(panel->backlight.device)) {
1287 DRM_ERROR("Failed to register backlight: %ld\n",
1288 PTR_ERR(panel->backlight.device));
1289 panel->backlight.device = NULL;
1293 DRM_DEBUG_KMS("Connector %s backlight sysfs interface registered\n",
1294 connector->base.name);
1299 void intel_backlight_device_unregister(struct intel_connector *connector)
1301 struct intel_panel *panel = &connector->panel;
1303 if (panel->backlight.device) {
1304 backlight_device_unregister(panel->backlight.device);
1305 panel->backlight.device = NULL;
1308 #endif /* CONFIG_BACKLIGHT_CLASS_DEVICE */
1311 * CNP: PWM clock frequency is 19.2 MHz or 24 MHz.
1314 static u32 cnp_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
1316 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1318 return DIV_ROUND_CLOSEST(KHz(dev_priv->rawclk_freq), pwm_freq_hz);
1322 * BXT: PWM clock frequency = 19.2 MHz.
1324 static u32 bxt_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
1326 return DIV_ROUND_CLOSEST(KHz(19200), pwm_freq_hz);
1330 * SPT: This value represents the period of the PWM stream in clock periods
1331 * multiplied by 16 (default increment) or 128 (alternate increment selected in
1332 * SCHICKEN_1 bit 0). PWM clock is 24 MHz.
1334 static u32 spt_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
1336 struct intel_panel *panel = &connector->panel;
1339 if (panel->backlight.alternate_pwm_increment)
1344 return DIV_ROUND_CLOSEST(MHz(24), pwm_freq_hz * mul);
1348 * LPT: This value represents the period of the PWM stream in clock periods
1349 * multiplied by 128 (default increment) or 16 (alternate increment, selected in
1350 * LPT SOUTH_CHICKEN2 register bit 5).
1352 static u32 lpt_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
1354 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1355 struct intel_panel *panel = &connector->panel;
1358 if (panel->backlight.alternate_pwm_increment)
1363 if (HAS_PCH_LPT_H(dev_priv))
1364 clock = MHz(135); /* LPT:H */
1366 clock = MHz(24); /* LPT:LP */
1368 return DIV_ROUND_CLOSEST(clock, pwm_freq_hz * mul);
1372 * ILK/SNB/IVB: This value represents the period of the PWM stream in PCH
1373 * display raw clocks multiplied by 128.
1375 static u32 pch_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
1377 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1379 return DIV_ROUND_CLOSEST(KHz(dev_priv->rawclk_freq), pwm_freq_hz * 128);
1383 * Gen2: This field determines the number of time base events (display core
1384 * clock frequency/32) in total for a complete cycle of modulated backlight
1387 * Gen3: A time base event equals the display core clock ([DevPNV] HRAW clock)
1390 static u32 i9xx_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
1392 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1395 if (IS_PINEVIEW(dev_priv))
1396 clock = KHz(dev_priv->rawclk_freq);
1398 clock = KHz(dev_priv->cdclk.hw.cdclk);
1400 return DIV_ROUND_CLOSEST(clock, pwm_freq_hz * 32);
1404 * Gen4: This value represents the period of the PWM stream in display core
1405 * clocks ([DevCTG] HRAW clocks) multiplied by 128.
1408 static u32 i965_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
1410 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1413 if (IS_G4X(dev_priv))
1414 clock = KHz(dev_priv->rawclk_freq);
1416 clock = KHz(dev_priv->cdclk.hw.cdclk);
1418 return DIV_ROUND_CLOSEST(clock, pwm_freq_hz * 128);
1422 * VLV: This value represents the period of the PWM stream in display core
1423 * clocks ([DevCTG] 200MHz HRAW clocks) multiplied by 128 or 25MHz S0IX clocks
1424 * multiplied by 16. CHV uses a 19.2MHz S0IX clock.
1426 static u32 vlv_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
1428 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1431 if ((I915_READ(CBR1_VLV) & CBR_PWM_CLOCK_MUX_SELECT) == 0) {
1432 if (IS_CHERRYVIEW(dev_priv))
1438 clock = KHz(dev_priv->rawclk_freq);
1442 return DIV_ROUND_CLOSEST(clock, pwm_freq_hz * mul);
1445 static u32 get_backlight_max_vbt(struct intel_connector *connector)
1447 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1448 struct intel_panel *panel = &connector->panel;
1449 u16 pwm_freq_hz = dev_priv->vbt.backlight.pwm_freq_hz;
1452 if (!panel->backlight.hz_to_pwm) {
1453 DRM_DEBUG_KMS("backlight frequency conversion not supported\n");
1458 DRM_DEBUG_KMS("VBT defined backlight frequency %u Hz\n",
1462 DRM_DEBUG_KMS("default backlight frequency %u Hz\n",
1466 pwm = panel->backlight.hz_to_pwm(connector, pwm_freq_hz);
1468 DRM_DEBUG_KMS("backlight frequency conversion failed\n");
1476 * Note: The setup hooks can't assume pipe is set!
1478 static u32 get_backlight_min_vbt(struct intel_connector *connector)
1480 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1481 struct intel_panel *panel = &connector->panel;
1484 WARN_ON(panel->backlight.max == 0);
1487 * XXX: If the vbt value is 255, it makes min equal to max, which leads
1488 * to problems. There are such machines out there. Either our
1489 * interpretation is wrong or the vbt has bogus data. Or both. Safeguard
1490 * against this by letting the minimum be at most (arbitrarily chosen)
1493 min = clamp_t(int, dev_priv->vbt.backlight.min_brightness, 0, 64);
1494 if (min != dev_priv->vbt.backlight.min_brightness) {
1495 DRM_DEBUG_KMS("clamping VBT min backlight %d/255 to %d/255\n",
1496 dev_priv->vbt.backlight.min_brightness, min);
1499 /* vbt value is a coefficient in range [0..255] */
1500 return scale(min, 0, 255, 0, panel->backlight.max);
1503 static int lpt_setup_backlight(struct intel_connector *connector, enum pipe unused)
1505 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1506 struct intel_panel *panel = &connector->panel;
1507 u32 pch_ctl1, pch_ctl2, val;
1510 if (HAS_PCH_LPT(dev_priv))
1511 alt = I915_READ(SOUTH_CHICKEN2) & LPT_PWM_GRANULARITY;
1513 alt = I915_READ(SOUTH_CHICKEN1) & SPT_PWM_GRANULARITY;
1514 panel->backlight.alternate_pwm_increment = alt;
1516 pch_ctl1 = I915_READ(BLC_PWM_PCH_CTL1);
1517 panel->backlight.active_low_pwm = pch_ctl1 & BLM_PCH_POLARITY;
1519 pch_ctl2 = I915_READ(BLC_PWM_PCH_CTL2);
1520 panel->backlight.max = pch_ctl2 >> 16;
1522 if (!panel->backlight.max)
1523 panel->backlight.max = get_backlight_max_vbt(connector);
1525 if (!panel->backlight.max)
1528 panel->backlight.min = get_backlight_min_vbt(connector);
1530 val = lpt_get_backlight(connector);
1531 val = intel_panel_compute_brightness(connector, val);
1532 panel->backlight.level = clamp(val, panel->backlight.min,
1533 panel->backlight.max);
1535 panel->backlight.enabled = pch_ctl1 & BLM_PCH_PWM_ENABLE;
1540 static int pch_setup_backlight(struct intel_connector *connector, enum pipe unused)
1542 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1543 struct intel_panel *panel = &connector->panel;
1544 u32 cpu_ctl2, pch_ctl1, pch_ctl2, val;
1546 pch_ctl1 = I915_READ(BLC_PWM_PCH_CTL1);
1547 panel->backlight.active_low_pwm = pch_ctl1 & BLM_PCH_POLARITY;
1549 pch_ctl2 = I915_READ(BLC_PWM_PCH_CTL2);
1550 panel->backlight.max = pch_ctl2 >> 16;
1552 if (!panel->backlight.max)
1553 panel->backlight.max = get_backlight_max_vbt(connector);
1555 if (!panel->backlight.max)
1558 panel->backlight.min = get_backlight_min_vbt(connector);
1560 val = pch_get_backlight(connector);
1561 val = intel_panel_compute_brightness(connector, val);
1562 panel->backlight.level = clamp(val, panel->backlight.min,
1563 panel->backlight.max);
1565 cpu_ctl2 = I915_READ(BLC_PWM_CPU_CTL2);
1566 panel->backlight.enabled = (cpu_ctl2 & BLM_PWM_ENABLE) &&
1567 (pch_ctl1 & BLM_PCH_PWM_ENABLE);
1572 static int i9xx_setup_backlight(struct intel_connector *connector, enum pipe unused)
1574 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1575 struct intel_panel *panel = &connector->panel;
1578 ctl = I915_READ(BLC_PWM_CTL);
1580 if (IS_GEN2(dev_priv) || IS_I915GM(dev_priv) || IS_I945GM(dev_priv))
1581 panel->backlight.combination_mode = ctl & BLM_LEGACY_MODE;
1583 if (IS_PINEVIEW(dev_priv))
1584 panel->backlight.active_low_pwm = ctl & BLM_POLARITY_PNV;
1586 panel->backlight.max = ctl >> 17;
1588 if (!panel->backlight.max) {
1589 panel->backlight.max = get_backlight_max_vbt(connector);
1590 panel->backlight.max >>= 1;
1593 if (!panel->backlight.max)
1596 if (panel->backlight.combination_mode)
1597 panel->backlight.max *= 0xff;
1599 panel->backlight.min = get_backlight_min_vbt(connector);
1601 val = i9xx_get_backlight(connector);
1602 val = intel_panel_compute_brightness(connector, val);
1603 panel->backlight.level = clamp(val, panel->backlight.min,
1604 panel->backlight.max);
1606 panel->backlight.enabled = val != 0;
1611 static int i965_setup_backlight(struct intel_connector *connector, enum pipe unused)
1613 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1614 struct intel_panel *panel = &connector->panel;
1617 ctl2 = I915_READ(BLC_PWM_CTL2);
1618 panel->backlight.combination_mode = ctl2 & BLM_COMBINATION_MODE;
1619 panel->backlight.active_low_pwm = ctl2 & BLM_POLARITY_I965;
1621 ctl = I915_READ(BLC_PWM_CTL);
1622 panel->backlight.max = ctl >> 16;
1624 if (!panel->backlight.max)
1625 panel->backlight.max = get_backlight_max_vbt(connector);
1627 if (!panel->backlight.max)
1630 if (panel->backlight.combination_mode)
1631 panel->backlight.max *= 0xff;
1633 panel->backlight.min = get_backlight_min_vbt(connector);
1635 val = i9xx_get_backlight(connector);
1636 val = intel_panel_compute_brightness(connector, val);
1637 panel->backlight.level = clamp(val, panel->backlight.min,
1638 panel->backlight.max);
1640 panel->backlight.enabled = ctl2 & BLM_PWM_ENABLE;
1645 static int vlv_setup_backlight(struct intel_connector *connector, enum pipe pipe)
1647 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1648 struct intel_panel *panel = &connector->panel;
1651 if (WARN_ON(pipe != PIPE_A && pipe != PIPE_B))
1654 ctl2 = I915_READ(VLV_BLC_PWM_CTL2(pipe));
1655 panel->backlight.active_low_pwm = ctl2 & BLM_POLARITY_I965;
1657 ctl = I915_READ(VLV_BLC_PWM_CTL(pipe));
1658 panel->backlight.max = ctl >> 16;
1660 if (!panel->backlight.max)
1661 panel->backlight.max = get_backlight_max_vbt(connector);
1663 if (!panel->backlight.max)
1666 panel->backlight.min = get_backlight_min_vbt(connector);
1668 val = _vlv_get_backlight(dev_priv, pipe);
1669 val = intel_panel_compute_brightness(connector, val);
1670 panel->backlight.level = clamp(val, panel->backlight.min,
1671 panel->backlight.max);
1673 panel->backlight.enabled = ctl2 & BLM_PWM_ENABLE;
1679 bxt_setup_backlight(struct intel_connector *connector, enum pipe unused)
1681 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1682 struct intel_panel *panel = &connector->panel;
1685 panel->backlight.controller = dev_priv->vbt.backlight.controller;
1687 pwm_ctl = I915_READ(BXT_BLC_PWM_CTL(panel->backlight.controller));
1689 /* Controller 1 uses the utility pin. */
1690 if (panel->backlight.controller == 1) {
1691 val = I915_READ(UTIL_PIN_CTL);
1692 panel->backlight.util_pin_active_low =
1693 val & UTIL_PIN_POLARITY;
1696 panel->backlight.active_low_pwm = pwm_ctl & BXT_BLC_PWM_POLARITY;
1697 panel->backlight.max =
1698 I915_READ(BXT_BLC_PWM_FREQ(panel->backlight.controller));
1700 if (!panel->backlight.max)
1701 panel->backlight.max = get_backlight_max_vbt(connector);
1703 if (!panel->backlight.max)
1706 panel->backlight.min = get_backlight_min_vbt(connector);
1708 val = bxt_get_backlight(connector);
1709 val = intel_panel_compute_brightness(connector, val);
1710 panel->backlight.level = clamp(val, panel->backlight.min,
1711 panel->backlight.max);
1713 panel->backlight.enabled = pwm_ctl & BXT_BLC_PWM_ENABLE;
1719 cnp_setup_backlight(struct intel_connector *connector, enum pipe unused)
1721 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1722 struct intel_panel *panel = &connector->panel;
1726 * CNP has the BXT implementation of backlight, but with only one
1727 * controller. TODO: ICP has multiple controllers but we only use
1728 * controller 0 for now.
1730 panel->backlight.controller = 0;
1732 pwm_ctl = I915_READ(BXT_BLC_PWM_CTL(panel->backlight.controller));
1734 panel->backlight.active_low_pwm = pwm_ctl & BXT_BLC_PWM_POLARITY;
1735 panel->backlight.max =
1736 I915_READ(BXT_BLC_PWM_FREQ(panel->backlight.controller));
1738 if (!panel->backlight.max)
1739 panel->backlight.max = get_backlight_max_vbt(connector);
1741 if (!panel->backlight.max)
1744 panel->backlight.min = get_backlight_min_vbt(connector);
1746 val = bxt_get_backlight(connector);
1747 val = intel_panel_compute_brightness(connector, val);
1748 panel->backlight.level = clamp(val, panel->backlight.min,
1749 panel->backlight.max);
1751 panel->backlight.enabled = pwm_ctl & BXT_BLC_PWM_ENABLE;
1756 static int pwm_setup_backlight(struct intel_connector *connector,
1759 struct drm_device *dev = connector->base.dev;
1760 struct intel_panel *panel = &connector->panel;
1763 /* Get the PWM chip for backlight control */
1764 panel->backlight.pwm = pwm_get(dev->dev, "pwm_backlight");
1765 if (IS_ERR(panel->backlight.pwm)) {
1766 DRM_ERROR("Failed to own the pwm chip\n");
1767 panel->backlight.pwm = NULL;
1772 * FIXME: pwm_apply_args() should be removed when switching to
1773 * the atomic PWM API.
1775 pwm_apply_args(panel->backlight.pwm);
1777 retval = pwm_config(panel->backlight.pwm, CRC_PMIC_PWM_PERIOD_NS,
1778 CRC_PMIC_PWM_PERIOD_NS);
1780 DRM_ERROR("Failed to configure the pwm chip\n");
1781 pwm_put(panel->backlight.pwm);
1782 panel->backlight.pwm = NULL;
1786 panel->backlight.min = 0; /* 0% */
1787 panel->backlight.max = 100; /* 100% */
1788 panel->backlight.level = DIV_ROUND_UP(
1789 pwm_get_duty_cycle(panel->backlight.pwm) * 100,
1790 CRC_PMIC_PWM_PERIOD_NS);
1791 panel->backlight.enabled = panel->backlight.level != 0;
1796 int intel_panel_setup_backlight(struct drm_connector *connector, enum pipe pipe)
1798 struct drm_i915_private *dev_priv = to_i915(connector->dev);
1799 struct intel_connector *intel_connector = to_intel_connector(connector);
1800 struct intel_panel *panel = &intel_connector->panel;
1803 if (!dev_priv->vbt.backlight.present) {
1804 if (dev_priv->quirks & QUIRK_BACKLIGHT_PRESENT) {
1805 DRM_DEBUG_KMS("no backlight present per VBT, but present per quirk\n");
1807 DRM_DEBUG_KMS("no backlight present per VBT\n");
1812 /* ensure intel_panel has been initialized first */
1813 if (WARN_ON(!panel->backlight.setup))
1816 /* set level and max in panel struct */
1817 mutex_lock(&dev_priv->backlight_lock);
1818 ret = panel->backlight.setup(intel_connector, pipe);
1819 mutex_unlock(&dev_priv->backlight_lock);
1822 DRM_DEBUG_KMS("failed to setup backlight for connector %s\n",
1827 panel->backlight.present = true;
1829 DRM_DEBUG_KMS("Connector %s backlight initialized, %s, brightness %u/%u\n",
1831 enableddisabled(panel->backlight.enabled),
1832 panel->backlight.level, panel->backlight.max);
1837 void intel_panel_destroy_backlight(struct drm_connector *connector)
1839 struct intel_connector *intel_connector = to_intel_connector(connector);
1840 struct intel_panel *panel = &intel_connector->panel;
1842 /* dispose of the pwm */
1843 if (panel->backlight.pwm)
1844 pwm_put(panel->backlight.pwm);
1846 panel->backlight.present = false;
1849 /* Set up chip specific backlight functions */
1851 intel_panel_init_backlight_funcs(struct intel_panel *panel)
1853 struct intel_connector *connector =
1854 container_of(panel, struct intel_connector, panel);
1855 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1857 if (connector->base.connector_type == DRM_MODE_CONNECTOR_eDP &&
1858 intel_dp_aux_init_backlight_funcs(connector) == 0)
1861 if (connector->base.connector_type == DRM_MODE_CONNECTOR_DSI &&
1862 intel_dsi_dcs_init_backlight_funcs(connector) == 0)
1865 if (IS_GEN9_LP(dev_priv)) {
1866 panel->backlight.setup = bxt_setup_backlight;
1867 panel->backlight.enable = bxt_enable_backlight;
1868 panel->backlight.disable = bxt_disable_backlight;
1869 panel->backlight.set = bxt_set_backlight;
1870 panel->backlight.get = bxt_get_backlight;
1871 panel->backlight.hz_to_pwm = bxt_hz_to_pwm;
1872 } else if (HAS_PCH_CNP(dev_priv) || HAS_PCH_ICP(dev_priv)) {
1873 panel->backlight.setup = cnp_setup_backlight;
1874 panel->backlight.enable = cnp_enable_backlight;
1875 panel->backlight.disable = cnp_disable_backlight;
1876 panel->backlight.set = bxt_set_backlight;
1877 panel->backlight.get = bxt_get_backlight;
1878 panel->backlight.hz_to_pwm = cnp_hz_to_pwm;
1879 } else if (HAS_PCH_LPT(dev_priv) || HAS_PCH_SPT(dev_priv) ||
1880 HAS_PCH_KBP(dev_priv)) {
1881 panel->backlight.setup = lpt_setup_backlight;
1882 panel->backlight.enable = lpt_enable_backlight;
1883 panel->backlight.disable = lpt_disable_backlight;
1884 panel->backlight.set = lpt_set_backlight;
1885 panel->backlight.get = lpt_get_backlight;
1886 if (HAS_PCH_LPT(dev_priv))
1887 panel->backlight.hz_to_pwm = lpt_hz_to_pwm;
1889 panel->backlight.hz_to_pwm = spt_hz_to_pwm;
1890 } else if (HAS_PCH_SPLIT(dev_priv)) {
1891 panel->backlight.setup = pch_setup_backlight;
1892 panel->backlight.enable = pch_enable_backlight;
1893 panel->backlight.disable = pch_disable_backlight;
1894 panel->backlight.set = pch_set_backlight;
1895 panel->backlight.get = pch_get_backlight;
1896 panel->backlight.hz_to_pwm = pch_hz_to_pwm;
1897 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
1898 if (connector->base.connector_type == DRM_MODE_CONNECTOR_DSI) {
1899 panel->backlight.setup = pwm_setup_backlight;
1900 panel->backlight.enable = pwm_enable_backlight;
1901 panel->backlight.disable = pwm_disable_backlight;
1902 panel->backlight.set = pwm_set_backlight;
1903 panel->backlight.get = pwm_get_backlight;
1905 panel->backlight.setup = vlv_setup_backlight;
1906 panel->backlight.enable = vlv_enable_backlight;
1907 panel->backlight.disable = vlv_disable_backlight;
1908 panel->backlight.set = vlv_set_backlight;
1909 panel->backlight.get = vlv_get_backlight;
1910 panel->backlight.hz_to_pwm = vlv_hz_to_pwm;
1912 } else if (IS_GEN4(dev_priv)) {
1913 panel->backlight.setup = i965_setup_backlight;
1914 panel->backlight.enable = i965_enable_backlight;
1915 panel->backlight.disable = i965_disable_backlight;
1916 panel->backlight.set = i9xx_set_backlight;
1917 panel->backlight.get = i9xx_get_backlight;
1918 panel->backlight.hz_to_pwm = i965_hz_to_pwm;
1920 panel->backlight.setup = i9xx_setup_backlight;
1921 panel->backlight.enable = i9xx_enable_backlight;
1922 panel->backlight.disable = i9xx_disable_backlight;
1923 panel->backlight.set = i9xx_set_backlight;
1924 panel->backlight.get = i9xx_get_backlight;
1925 panel->backlight.hz_to_pwm = i9xx_hz_to_pwm;
1929 int intel_panel_init(struct intel_panel *panel,
1930 struct drm_display_mode *fixed_mode,
1931 struct drm_display_mode *downclock_mode)
1933 intel_panel_init_backlight_funcs(panel);
1935 panel->fixed_mode = fixed_mode;
1936 panel->downclock_mode = downclock_mode;
1941 void intel_panel_fini(struct intel_panel *panel)
1943 struct intel_connector *intel_connector =
1944 container_of(panel, struct intel_connector, panel);
1946 if (panel->fixed_mode)
1947 drm_mode_destroy(intel_connector->base.dev, panel->fixed_mode);
1949 if (panel->downclock_mode)
1950 drm_mode_destroy(intel_connector->base.dev,
1951 panel->downclock_mode);