2 * Copyright © 2006-2010 Intel Corporation
3 * Copyright (c) 2006 Dave Airlie <airlied@linux.ie>
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
25 * Eric Anholt <eric@anholt.net>
26 * Dave Airlie <airlied@linux.ie>
27 * Jesse Barnes <jesse.barnes@intel.com>
28 * Chris Wilson <chris@chris-wilson.co.uk>
31 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
33 #include <linux/moduleparam.h>
34 #include "intel_drv.h"
37 intel_fixed_panel_mode(const struct drm_display_mode *fixed_mode,
38 struct drm_display_mode *adjusted_mode)
40 drm_mode_copy(adjusted_mode, fixed_mode);
42 drm_mode_set_crtcinfo(adjusted_mode, 0);
46 * intel_find_panel_downclock - find the reduced downclock for LVDS in EDID
48 * @fixed_mode : panel native mode
49 * @connector: LVDS/eDP connector
51 * Return downclock_avail
52 * Find the reduced downclock for LVDS/eDP in EDID.
54 struct drm_display_mode *
55 intel_find_panel_downclock(struct drm_device *dev,
56 struct drm_display_mode *fixed_mode,
57 struct drm_connector *connector)
59 struct drm_display_mode *scan, *tmp_mode;
62 temp_downclock = fixed_mode->clock;
65 list_for_each_entry(scan, &connector->probed_modes, head) {
67 * If one mode has the same resolution with the fixed_panel
68 * mode while they have the different refresh rate, it means
69 * that the reduced downclock is found. In such
70 * case we can set the different FPx0/1 to dynamically select
71 * between low and high frequency.
73 if (scan->hdisplay == fixed_mode->hdisplay &&
74 scan->hsync_start == fixed_mode->hsync_start &&
75 scan->hsync_end == fixed_mode->hsync_end &&
76 scan->htotal == fixed_mode->htotal &&
77 scan->vdisplay == fixed_mode->vdisplay &&
78 scan->vsync_start == fixed_mode->vsync_start &&
79 scan->vsync_end == fixed_mode->vsync_end &&
80 scan->vtotal == fixed_mode->vtotal) {
81 if (scan->clock < temp_downclock) {
83 * The downclock is already found. But we
84 * expect to find the lower downclock.
86 temp_downclock = scan->clock;
92 if (temp_downclock < fixed_mode->clock)
93 return drm_mode_duplicate(dev, tmp_mode);
98 /* adjusted_mode has been preset to be the panel's fixed mode */
100 intel_pch_panel_fitting(struct intel_crtc *intel_crtc,
101 struct intel_crtc_config *pipe_config,
104 struct drm_display_mode *adjusted_mode;
105 int x, y, width, height;
107 adjusted_mode = &pipe_config->adjusted_mode;
109 x = y = width = height = 0;
111 /* Native modes don't need fitting */
112 if (adjusted_mode->hdisplay == pipe_config->pipe_src_w &&
113 adjusted_mode->vdisplay == pipe_config->pipe_src_h)
116 switch (fitting_mode) {
117 case DRM_MODE_SCALE_CENTER:
118 width = pipe_config->pipe_src_w;
119 height = pipe_config->pipe_src_h;
120 x = (adjusted_mode->hdisplay - width + 1)/2;
121 y = (adjusted_mode->vdisplay - height + 1)/2;
124 case DRM_MODE_SCALE_ASPECT:
125 /* Scale but preserve the aspect ratio */
127 u32 scaled_width = adjusted_mode->hdisplay
128 * pipe_config->pipe_src_h;
129 u32 scaled_height = pipe_config->pipe_src_w
130 * adjusted_mode->vdisplay;
131 if (scaled_width > scaled_height) { /* pillar */
132 width = scaled_height / pipe_config->pipe_src_h;
135 x = (adjusted_mode->hdisplay - width + 1) / 2;
137 height = adjusted_mode->vdisplay;
138 } else if (scaled_width < scaled_height) { /* letter */
139 height = scaled_width / pipe_config->pipe_src_w;
142 y = (adjusted_mode->vdisplay - height + 1) / 2;
144 width = adjusted_mode->hdisplay;
147 width = adjusted_mode->hdisplay;
148 height = adjusted_mode->vdisplay;
153 case DRM_MODE_SCALE_FULLSCREEN:
155 width = adjusted_mode->hdisplay;
156 height = adjusted_mode->vdisplay;
160 WARN(1, "bad panel fit mode: %d\n", fitting_mode);
165 pipe_config->pch_pfit.pos = (x << 16) | y;
166 pipe_config->pch_pfit.size = (width << 16) | height;
167 pipe_config->pch_pfit.enabled = pipe_config->pch_pfit.size != 0;
171 centre_horizontally(struct drm_display_mode *mode,
174 u32 border, sync_pos, blank_width, sync_width;
176 /* keep the hsync and hblank widths constant */
177 sync_width = mode->crtc_hsync_end - mode->crtc_hsync_start;
178 blank_width = mode->crtc_hblank_end - mode->crtc_hblank_start;
179 sync_pos = (blank_width - sync_width + 1) / 2;
181 border = (mode->hdisplay - width + 1) / 2;
182 border += border & 1; /* make the border even */
184 mode->crtc_hdisplay = width;
185 mode->crtc_hblank_start = width + border;
186 mode->crtc_hblank_end = mode->crtc_hblank_start + blank_width;
188 mode->crtc_hsync_start = mode->crtc_hblank_start + sync_pos;
189 mode->crtc_hsync_end = mode->crtc_hsync_start + sync_width;
193 centre_vertically(struct drm_display_mode *mode,
196 u32 border, sync_pos, blank_width, sync_width;
198 /* keep the vsync and vblank widths constant */
199 sync_width = mode->crtc_vsync_end - mode->crtc_vsync_start;
200 blank_width = mode->crtc_vblank_end - mode->crtc_vblank_start;
201 sync_pos = (blank_width - sync_width + 1) / 2;
203 border = (mode->vdisplay - height + 1) / 2;
205 mode->crtc_vdisplay = height;
206 mode->crtc_vblank_start = height + border;
207 mode->crtc_vblank_end = mode->crtc_vblank_start + blank_width;
209 mode->crtc_vsync_start = mode->crtc_vblank_start + sync_pos;
210 mode->crtc_vsync_end = mode->crtc_vsync_start + sync_width;
213 static inline u32 panel_fitter_scaling(u32 source, u32 target)
216 * Floating point operation is not supported. So the FACTOR
217 * is defined, which can avoid the floating point computation
218 * when calculating the panel ratio.
221 #define FACTOR (1 << ACCURACY)
222 u32 ratio = source * FACTOR / target;
223 return (FACTOR * ratio + FACTOR/2) / FACTOR;
226 static void i965_scale_aspect(struct intel_crtc_config *pipe_config,
229 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
230 u32 scaled_width = adjusted_mode->hdisplay *
231 pipe_config->pipe_src_h;
232 u32 scaled_height = pipe_config->pipe_src_w *
233 adjusted_mode->vdisplay;
235 /* 965+ is easy, it does everything in hw */
236 if (scaled_width > scaled_height)
237 *pfit_control |= PFIT_ENABLE |
239 else if (scaled_width < scaled_height)
240 *pfit_control |= PFIT_ENABLE |
242 else if (adjusted_mode->hdisplay != pipe_config->pipe_src_w)
243 *pfit_control |= PFIT_ENABLE | PFIT_SCALING_AUTO;
246 static void i9xx_scale_aspect(struct intel_crtc_config *pipe_config,
247 u32 *pfit_control, u32 *pfit_pgm_ratios,
250 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
251 u32 scaled_width = adjusted_mode->hdisplay *
252 pipe_config->pipe_src_h;
253 u32 scaled_height = pipe_config->pipe_src_w *
254 adjusted_mode->vdisplay;
258 * For earlier chips we have to calculate the scaling
259 * ratio by hand and program it into the
260 * PFIT_PGM_RATIO register
262 if (scaled_width > scaled_height) { /* pillar */
263 centre_horizontally(adjusted_mode,
265 pipe_config->pipe_src_h);
267 *border = LVDS_BORDER_ENABLE;
268 if (pipe_config->pipe_src_h != adjusted_mode->vdisplay) {
269 bits = panel_fitter_scaling(pipe_config->pipe_src_h,
270 adjusted_mode->vdisplay);
272 *pfit_pgm_ratios |= (bits << PFIT_HORIZ_SCALE_SHIFT |
273 bits << PFIT_VERT_SCALE_SHIFT);
274 *pfit_control |= (PFIT_ENABLE |
275 VERT_INTERP_BILINEAR |
276 HORIZ_INTERP_BILINEAR);
278 } else if (scaled_width < scaled_height) { /* letter */
279 centre_vertically(adjusted_mode,
281 pipe_config->pipe_src_w);
283 *border = LVDS_BORDER_ENABLE;
284 if (pipe_config->pipe_src_w != adjusted_mode->hdisplay) {
285 bits = panel_fitter_scaling(pipe_config->pipe_src_w,
286 adjusted_mode->hdisplay);
288 *pfit_pgm_ratios |= (bits << PFIT_HORIZ_SCALE_SHIFT |
289 bits << PFIT_VERT_SCALE_SHIFT);
290 *pfit_control |= (PFIT_ENABLE |
291 VERT_INTERP_BILINEAR |
292 HORIZ_INTERP_BILINEAR);
295 /* Aspects match, Let hw scale both directions */
296 *pfit_control |= (PFIT_ENABLE |
297 VERT_AUTO_SCALE | HORIZ_AUTO_SCALE |
298 VERT_INTERP_BILINEAR |
299 HORIZ_INTERP_BILINEAR);
303 void intel_gmch_panel_fitting(struct intel_crtc *intel_crtc,
304 struct intel_crtc_config *pipe_config,
307 struct drm_device *dev = intel_crtc->base.dev;
308 u32 pfit_control = 0, pfit_pgm_ratios = 0, border = 0;
309 struct drm_display_mode *adjusted_mode;
311 adjusted_mode = &pipe_config->adjusted_mode;
313 /* Native modes don't need fitting */
314 if (adjusted_mode->hdisplay == pipe_config->pipe_src_w &&
315 adjusted_mode->vdisplay == pipe_config->pipe_src_h)
318 switch (fitting_mode) {
319 case DRM_MODE_SCALE_CENTER:
321 * For centered modes, we have to calculate border widths &
322 * heights and modify the values programmed into the CRTC.
324 centre_horizontally(adjusted_mode, pipe_config->pipe_src_w);
325 centre_vertically(adjusted_mode, pipe_config->pipe_src_h);
326 border = LVDS_BORDER_ENABLE;
328 case DRM_MODE_SCALE_ASPECT:
329 /* Scale but preserve the aspect ratio */
330 if (INTEL_INFO(dev)->gen >= 4)
331 i965_scale_aspect(pipe_config, &pfit_control);
333 i9xx_scale_aspect(pipe_config, &pfit_control,
334 &pfit_pgm_ratios, &border);
336 case DRM_MODE_SCALE_FULLSCREEN:
338 * Full scaling, even if it changes the aspect ratio.
339 * Fortunately this is all done for us in hw.
341 if (pipe_config->pipe_src_h != adjusted_mode->vdisplay ||
342 pipe_config->pipe_src_w != adjusted_mode->hdisplay) {
343 pfit_control |= PFIT_ENABLE;
344 if (INTEL_INFO(dev)->gen >= 4)
345 pfit_control |= PFIT_SCALING_AUTO;
347 pfit_control |= (VERT_AUTO_SCALE |
348 VERT_INTERP_BILINEAR |
350 HORIZ_INTERP_BILINEAR);
354 WARN(1, "bad panel fit mode: %d\n", fitting_mode);
358 /* 965+ wants fuzzy fitting */
359 /* FIXME: handle multiple panels by failing gracefully */
360 if (INTEL_INFO(dev)->gen >= 4)
361 pfit_control |= ((intel_crtc->pipe << PFIT_PIPE_SHIFT) |
364 /* Make sure pre-965 set dither correctly for 18bpp panels. */
365 if (INTEL_INFO(dev)->gen < 4 && pipe_config->pipe_bpp == 18)
366 pfit_control |= PANEL_8TO6_DITHER_ENABLE;
369 if ((pfit_control & PFIT_ENABLE) == 0) {
374 pipe_config->gmch_pfit.control = pfit_control;
375 pipe_config->gmch_pfit.pgm_ratios = pfit_pgm_ratios;
376 pipe_config->gmch_pfit.lvds_border_bits = border;
379 enum drm_connector_status
380 intel_panel_detect(struct drm_device *dev)
382 struct drm_i915_private *dev_priv = dev->dev_private;
384 /* Assume that the BIOS does not lie through the OpRegion... */
385 if (!i915.panel_ignore_lid && dev_priv->opregion.lid_state) {
386 return ioread32(dev_priv->opregion.lid_state) & 0x1 ?
387 connector_status_connected :
388 connector_status_disconnected;
391 switch (i915.panel_ignore_lid) {
393 return connector_status_connected;
395 return connector_status_disconnected;
397 return connector_status_unknown;
401 static u32 intel_panel_compute_brightness(struct intel_connector *connector,
404 struct drm_device *dev = connector->base.dev;
405 struct drm_i915_private *dev_priv = dev->dev_private;
406 struct intel_panel *panel = &connector->panel;
408 WARN_ON(panel->backlight.max == 0);
410 if (i915.invert_brightness < 0)
413 if (i915.invert_brightness > 0 ||
414 dev_priv->quirks & QUIRK_INVERT_BRIGHTNESS) {
415 return panel->backlight.max - val;
421 static u32 bdw_get_backlight(struct intel_connector *connector)
423 struct drm_device *dev = connector->base.dev;
424 struct drm_i915_private *dev_priv = dev->dev_private;
426 return I915_READ(BLC_PWM_PCH_CTL2) & BACKLIGHT_DUTY_CYCLE_MASK;
429 static u32 pch_get_backlight(struct intel_connector *connector)
431 struct drm_device *dev = connector->base.dev;
432 struct drm_i915_private *dev_priv = dev->dev_private;
434 return I915_READ(BLC_PWM_CPU_CTL) & BACKLIGHT_DUTY_CYCLE_MASK;
437 static u32 i9xx_get_backlight(struct intel_connector *connector)
439 struct drm_device *dev = connector->base.dev;
440 struct drm_i915_private *dev_priv = dev->dev_private;
441 struct intel_panel *panel = &connector->panel;
444 val = I915_READ(BLC_PWM_CTL) & BACKLIGHT_DUTY_CYCLE_MASK;
445 if (INTEL_INFO(dev)->gen < 4)
448 if (panel->backlight.combination_mode) {
451 pci_read_config_byte(dev->pdev, PCI_LBPC, &lbpc);
458 static u32 _vlv_get_backlight(struct drm_device *dev, enum pipe pipe)
460 struct drm_i915_private *dev_priv = dev->dev_private;
462 return I915_READ(VLV_BLC_PWM_CTL(pipe)) & BACKLIGHT_DUTY_CYCLE_MASK;
465 static u32 vlv_get_backlight(struct intel_connector *connector)
467 struct drm_device *dev = connector->base.dev;
468 enum pipe pipe = intel_get_pipe_from_connector(connector);
470 return _vlv_get_backlight(dev, pipe);
473 static u32 intel_panel_get_backlight(struct intel_connector *connector)
475 struct drm_device *dev = connector->base.dev;
476 struct drm_i915_private *dev_priv = dev->dev_private;
480 spin_lock_irqsave(&dev_priv->backlight_lock, flags);
482 val = dev_priv->display.get_backlight(connector);
483 val = intel_panel_compute_brightness(connector, val);
485 spin_unlock_irqrestore(&dev_priv->backlight_lock, flags);
487 DRM_DEBUG_DRIVER("get backlight PWM = %d\n", val);
491 static void bdw_set_backlight(struct intel_connector *connector, u32 level)
493 struct drm_device *dev = connector->base.dev;
494 struct drm_i915_private *dev_priv = dev->dev_private;
495 u32 val = I915_READ(BLC_PWM_PCH_CTL2) & ~BACKLIGHT_DUTY_CYCLE_MASK;
496 I915_WRITE(BLC_PWM_PCH_CTL2, val | level);
499 static void pch_set_backlight(struct intel_connector *connector, u32 level)
501 struct drm_device *dev = connector->base.dev;
502 struct drm_i915_private *dev_priv = dev->dev_private;
505 tmp = I915_READ(BLC_PWM_CPU_CTL) & ~BACKLIGHT_DUTY_CYCLE_MASK;
506 I915_WRITE(BLC_PWM_CPU_CTL, tmp | level);
509 static void i9xx_set_backlight(struct intel_connector *connector, u32 level)
511 struct drm_device *dev = connector->base.dev;
512 struct drm_i915_private *dev_priv = dev->dev_private;
513 struct intel_panel *panel = &connector->panel;
516 WARN_ON(panel->backlight.max == 0);
518 if (panel->backlight.combination_mode) {
521 lbpc = level * 0xfe / panel->backlight.max + 1;
523 pci_write_config_byte(dev->pdev, PCI_LBPC, lbpc);
527 mask = BACKLIGHT_DUTY_CYCLE_MASK;
530 mask = BACKLIGHT_DUTY_CYCLE_MASK_PNV;
533 tmp = I915_READ(BLC_PWM_CTL) & ~mask;
534 I915_WRITE(BLC_PWM_CTL, tmp | level);
537 static void vlv_set_backlight(struct intel_connector *connector, u32 level)
539 struct drm_device *dev = connector->base.dev;
540 struct drm_i915_private *dev_priv = dev->dev_private;
541 enum pipe pipe = intel_get_pipe_from_connector(connector);
544 tmp = I915_READ(VLV_BLC_PWM_CTL(pipe)) & ~BACKLIGHT_DUTY_CYCLE_MASK;
545 I915_WRITE(VLV_BLC_PWM_CTL(pipe), tmp | level);
549 intel_panel_actually_set_backlight(struct intel_connector *connector, u32 level)
551 struct drm_device *dev = connector->base.dev;
552 struct drm_i915_private *dev_priv = dev->dev_private;
554 DRM_DEBUG_DRIVER("set backlight PWM = %d\n", level);
556 level = intel_panel_compute_brightness(connector, level);
557 dev_priv->display.set_backlight(connector, level);
560 /* set backlight brightness to level in range [0..max] */
561 void intel_panel_set_backlight(struct intel_connector *connector, u32 level,
564 struct drm_device *dev = connector->base.dev;
565 struct drm_i915_private *dev_priv = dev->dev_private;
566 struct intel_panel *panel = &connector->panel;
567 enum pipe pipe = intel_get_pipe_from_connector(connector);
571 if (!panel->backlight.present || pipe == INVALID_PIPE)
574 spin_lock_irqsave(&dev_priv->backlight_lock, flags);
576 WARN_ON(panel->backlight.max == 0);
578 /* scale to hardware max, but be careful to not overflow */
579 freq = panel->backlight.max;
581 level = level * freq / max;
583 level = freq / max * level;
585 panel->backlight.level = level;
586 if (panel->backlight.device)
587 panel->backlight.device->props.brightness = level;
589 if (panel->backlight.enabled)
590 intel_panel_actually_set_backlight(connector, level);
592 spin_unlock_irqrestore(&dev_priv->backlight_lock, flags);
595 static void pch_disable_backlight(struct intel_connector *connector)
597 struct drm_device *dev = connector->base.dev;
598 struct drm_i915_private *dev_priv = dev->dev_private;
601 intel_panel_actually_set_backlight(connector, 0);
603 tmp = I915_READ(BLC_PWM_CPU_CTL2);
604 I915_WRITE(BLC_PWM_CPU_CTL2, tmp & ~BLM_PWM_ENABLE);
606 tmp = I915_READ(BLC_PWM_PCH_CTL1);
607 I915_WRITE(BLC_PWM_PCH_CTL1, tmp & ~BLM_PCH_PWM_ENABLE);
610 static void i9xx_disable_backlight(struct intel_connector *connector)
612 intel_panel_actually_set_backlight(connector, 0);
615 static void i965_disable_backlight(struct intel_connector *connector)
617 struct drm_device *dev = connector->base.dev;
618 struct drm_i915_private *dev_priv = dev->dev_private;
621 intel_panel_actually_set_backlight(connector, 0);
623 tmp = I915_READ(BLC_PWM_CTL2);
624 I915_WRITE(BLC_PWM_CTL2, tmp & ~BLM_PWM_ENABLE);
627 static void vlv_disable_backlight(struct intel_connector *connector)
629 struct drm_device *dev = connector->base.dev;
630 struct drm_i915_private *dev_priv = dev->dev_private;
631 enum pipe pipe = intel_get_pipe_from_connector(connector);
634 intel_panel_actually_set_backlight(connector, 0);
636 tmp = I915_READ(VLV_BLC_PWM_CTL2(pipe));
637 I915_WRITE(VLV_BLC_PWM_CTL2(pipe), tmp & ~BLM_PWM_ENABLE);
640 void intel_panel_disable_backlight(struct intel_connector *connector)
642 struct drm_device *dev = connector->base.dev;
643 struct drm_i915_private *dev_priv = dev->dev_private;
644 struct intel_panel *panel = &connector->panel;
645 enum pipe pipe = intel_get_pipe_from_connector(connector);
648 if (!panel->backlight.present || pipe == INVALID_PIPE)
652 * Do not disable backlight on the vgaswitcheroo path. When switching
653 * away from i915, the other client may depend on i915 to handle the
654 * backlight. This will leave the backlight on unnecessarily when
655 * another client is not activated.
657 if (dev->switch_power_state == DRM_SWITCH_POWER_CHANGING) {
658 DRM_DEBUG_DRIVER("Skipping backlight disable on vga switch\n");
662 spin_lock_irqsave(&dev_priv->backlight_lock, flags);
664 panel->backlight.enabled = false;
665 dev_priv->display.disable_backlight(connector);
667 spin_unlock_irqrestore(&dev_priv->backlight_lock, flags);
670 static void bdw_enable_backlight(struct intel_connector *connector)
672 struct drm_device *dev = connector->base.dev;
673 struct drm_i915_private *dev_priv = dev->dev_private;
674 struct intel_panel *panel = &connector->panel;
675 u32 pch_ctl1, pch_ctl2;
677 pch_ctl1 = I915_READ(BLC_PWM_PCH_CTL1);
678 if (pch_ctl1 & BLM_PCH_PWM_ENABLE) {
679 DRM_DEBUG_KMS("pch backlight already enabled\n");
680 pch_ctl1 &= ~BLM_PCH_PWM_ENABLE;
681 I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1);
684 pch_ctl2 = panel->backlight.max << 16;
685 I915_WRITE(BLC_PWM_PCH_CTL2, pch_ctl2);
688 if (panel->backlight.active_low_pwm)
689 pch_ctl1 |= BLM_PCH_POLARITY;
691 /* BDW always uses the pch pwm controls. */
692 pch_ctl1 |= BLM_PCH_OVERRIDE_ENABLE;
694 I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1);
695 POSTING_READ(BLC_PWM_PCH_CTL1);
696 I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1 | BLM_PCH_PWM_ENABLE);
698 /* This won't stick until the above enable. */
699 intel_panel_actually_set_backlight(connector, panel->backlight.level);
702 static void pch_enable_backlight(struct intel_connector *connector)
704 struct drm_device *dev = connector->base.dev;
705 struct drm_i915_private *dev_priv = dev->dev_private;
706 struct intel_panel *panel = &connector->panel;
707 enum pipe pipe = intel_get_pipe_from_connector(connector);
708 enum transcoder cpu_transcoder =
709 intel_pipe_to_cpu_transcoder(dev_priv, pipe);
710 u32 cpu_ctl2, pch_ctl1, pch_ctl2;
712 cpu_ctl2 = I915_READ(BLC_PWM_CPU_CTL2);
713 if (cpu_ctl2 & BLM_PWM_ENABLE) {
714 WARN(1, "cpu backlight already enabled\n");
715 cpu_ctl2 &= ~BLM_PWM_ENABLE;
716 I915_WRITE(BLC_PWM_CPU_CTL2, cpu_ctl2);
719 pch_ctl1 = I915_READ(BLC_PWM_PCH_CTL1);
720 if (pch_ctl1 & BLM_PCH_PWM_ENABLE) {
721 DRM_DEBUG_KMS("pch backlight already enabled\n");
722 pch_ctl1 &= ~BLM_PCH_PWM_ENABLE;
723 I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1);
726 if (cpu_transcoder == TRANSCODER_EDP)
727 cpu_ctl2 = BLM_TRANSCODER_EDP;
729 cpu_ctl2 = BLM_PIPE(cpu_transcoder);
730 I915_WRITE(BLC_PWM_CPU_CTL2, cpu_ctl2);
731 POSTING_READ(BLC_PWM_CPU_CTL2);
732 I915_WRITE(BLC_PWM_CPU_CTL2, cpu_ctl2 | BLM_PWM_ENABLE);
734 /* This won't stick until the above enable. */
735 intel_panel_actually_set_backlight(connector, panel->backlight.level);
737 pch_ctl2 = panel->backlight.max << 16;
738 I915_WRITE(BLC_PWM_PCH_CTL2, pch_ctl2);
741 if (panel->backlight.active_low_pwm)
742 pch_ctl1 |= BLM_PCH_POLARITY;
744 I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1);
745 POSTING_READ(BLC_PWM_PCH_CTL1);
746 I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1 | BLM_PCH_PWM_ENABLE);
749 static void i9xx_enable_backlight(struct intel_connector *connector)
751 struct drm_device *dev = connector->base.dev;
752 struct drm_i915_private *dev_priv = dev->dev_private;
753 struct intel_panel *panel = &connector->panel;
756 ctl = I915_READ(BLC_PWM_CTL);
757 if (ctl & BACKLIGHT_DUTY_CYCLE_MASK_PNV) {
758 WARN(1, "backlight already enabled\n");
759 I915_WRITE(BLC_PWM_CTL, 0);
762 freq = panel->backlight.max;
763 if (panel->backlight.combination_mode)
767 if (panel->backlight.combination_mode)
768 ctl |= BLM_LEGACY_MODE;
769 if (IS_PINEVIEW(dev) && panel->backlight.active_low_pwm)
770 ctl |= BLM_POLARITY_PNV;
772 I915_WRITE(BLC_PWM_CTL, ctl);
773 POSTING_READ(BLC_PWM_CTL);
775 /* XXX: combine this into above write? */
776 intel_panel_actually_set_backlight(connector, panel->backlight.level);
779 static void i965_enable_backlight(struct intel_connector *connector)
781 struct drm_device *dev = connector->base.dev;
782 struct drm_i915_private *dev_priv = dev->dev_private;
783 struct intel_panel *panel = &connector->panel;
784 enum pipe pipe = intel_get_pipe_from_connector(connector);
787 ctl2 = I915_READ(BLC_PWM_CTL2);
788 if (ctl2 & BLM_PWM_ENABLE) {
789 WARN(1, "backlight already enabled\n");
790 ctl2 &= ~BLM_PWM_ENABLE;
791 I915_WRITE(BLC_PWM_CTL2, ctl2);
794 freq = panel->backlight.max;
795 if (panel->backlight.combination_mode)
799 I915_WRITE(BLC_PWM_CTL, ctl);
801 /* XXX: combine this into above write? */
802 intel_panel_actually_set_backlight(connector, panel->backlight.level);
804 ctl2 = BLM_PIPE(pipe);
805 if (panel->backlight.combination_mode)
806 ctl2 |= BLM_COMBINATION_MODE;
807 if (panel->backlight.active_low_pwm)
808 ctl2 |= BLM_POLARITY_I965;
809 I915_WRITE(BLC_PWM_CTL2, ctl2);
810 POSTING_READ(BLC_PWM_CTL2);
811 I915_WRITE(BLC_PWM_CTL2, ctl2 | BLM_PWM_ENABLE);
814 static void vlv_enable_backlight(struct intel_connector *connector)
816 struct drm_device *dev = connector->base.dev;
817 struct drm_i915_private *dev_priv = dev->dev_private;
818 struct intel_panel *panel = &connector->panel;
819 enum pipe pipe = intel_get_pipe_from_connector(connector);
822 ctl2 = I915_READ(VLV_BLC_PWM_CTL2(pipe));
823 if (ctl2 & BLM_PWM_ENABLE) {
824 WARN(1, "backlight already enabled\n");
825 ctl2 &= ~BLM_PWM_ENABLE;
826 I915_WRITE(VLV_BLC_PWM_CTL2(pipe), ctl2);
829 ctl = panel->backlight.max << 16;
830 I915_WRITE(VLV_BLC_PWM_CTL(pipe), ctl);
832 /* XXX: combine this into above write? */
833 intel_panel_actually_set_backlight(connector, panel->backlight.level);
836 if (panel->backlight.active_low_pwm)
837 ctl2 |= BLM_POLARITY_I965;
838 I915_WRITE(VLV_BLC_PWM_CTL2(pipe), ctl2);
839 POSTING_READ(VLV_BLC_PWM_CTL2(pipe));
840 I915_WRITE(VLV_BLC_PWM_CTL2(pipe), ctl2 | BLM_PWM_ENABLE);
843 void intel_panel_enable_backlight(struct intel_connector *connector)
845 struct drm_device *dev = connector->base.dev;
846 struct drm_i915_private *dev_priv = dev->dev_private;
847 struct intel_panel *panel = &connector->panel;
848 enum pipe pipe = intel_get_pipe_from_connector(connector);
851 if (!panel->backlight.present || pipe == INVALID_PIPE)
854 DRM_DEBUG_KMS("pipe %c\n", pipe_name(pipe));
856 spin_lock_irqsave(&dev_priv->backlight_lock, flags);
858 WARN_ON(panel->backlight.max == 0);
860 if (panel->backlight.level == 0) {
861 panel->backlight.level = panel->backlight.max;
862 if (panel->backlight.device)
863 panel->backlight.device->props.brightness =
864 panel->backlight.level;
867 dev_priv->display.enable_backlight(connector);
868 panel->backlight.enabled = true;
870 spin_unlock_irqrestore(&dev_priv->backlight_lock, flags);
873 #if IS_ENABLED(CONFIG_BACKLIGHT_CLASS_DEVICE)
874 static int intel_backlight_device_update_status(struct backlight_device *bd)
876 struct intel_connector *connector = bl_get_data(bd);
877 struct drm_device *dev = connector->base.dev;
879 mutex_lock(&dev->mode_config.mutex);
880 DRM_DEBUG_KMS("updating intel_backlight, brightness=%d/%d\n",
881 bd->props.brightness, bd->props.max_brightness);
882 intel_panel_set_backlight(connector, bd->props.brightness,
883 bd->props.max_brightness);
884 mutex_unlock(&dev->mode_config.mutex);
888 static int intel_backlight_device_get_brightness(struct backlight_device *bd)
890 struct intel_connector *connector = bl_get_data(bd);
891 struct drm_device *dev = connector->base.dev;
892 struct drm_i915_private *dev_priv = dev->dev_private;
895 intel_runtime_pm_get(dev_priv);
896 mutex_lock(&dev->mode_config.mutex);
897 ret = intel_panel_get_backlight(connector);
898 mutex_unlock(&dev->mode_config.mutex);
899 intel_runtime_pm_put(dev_priv);
904 static const struct backlight_ops intel_backlight_device_ops = {
905 .update_status = intel_backlight_device_update_status,
906 .get_brightness = intel_backlight_device_get_brightness,
909 static int intel_backlight_device_register(struct intel_connector *connector)
911 struct intel_panel *panel = &connector->panel;
912 struct backlight_properties props;
914 if (WARN_ON(panel->backlight.device))
917 BUG_ON(panel->backlight.max == 0);
919 memset(&props, 0, sizeof(props));
920 props.type = BACKLIGHT_RAW;
921 props.brightness = panel->backlight.level;
922 props.max_brightness = panel->backlight.max;
925 * Note: using the same name independent of the connector prevents
926 * registration of multiple backlight devices in the driver.
928 panel->backlight.device =
929 backlight_device_register("intel_backlight",
930 connector->base.kdev,
932 &intel_backlight_device_ops, &props);
934 if (IS_ERR(panel->backlight.device)) {
935 DRM_ERROR("Failed to register backlight: %ld\n",
936 PTR_ERR(panel->backlight.device));
937 panel->backlight.device = NULL;
943 static void intel_backlight_device_unregister(struct intel_connector *connector)
945 struct intel_panel *panel = &connector->panel;
947 if (panel->backlight.device) {
948 backlight_device_unregister(panel->backlight.device);
949 panel->backlight.device = NULL;
952 #else /* CONFIG_BACKLIGHT_CLASS_DEVICE */
953 static int intel_backlight_device_register(struct intel_connector *connector)
957 static void intel_backlight_device_unregister(struct intel_connector *connector)
960 #endif /* CONFIG_BACKLIGHT_CLASS_DEVICE */
963 * Note: The setup hooks can't assume pipe is set!
965 * XXX: Query mode clock or hardware clock and program PWM modulation frequency
966 * appropriately when it's 0. Use VBT and/or sane defaults.
968 static int bdw_setup_backlight(struct intel_connector *connector)
970 struct drm_device *dev = connector->base.dev;
971 struct drm_i915_private *dev_priv = dev->dev_private;
972 struct intel_panel *panel = &connector->panel;
973 u32 pch_ctl1, pch_ctl2, val;
975 pch_ctl1 = I915_READ(BLC_PWM_PCH_CTL1);
976 panel->backlight.active_low_pwm = pch_ctl1 & BLM_PCH_POLARITY;
978 pch_ctl2 = I915_READ(BLC_PWM_PCH_CTL2);
979 panel->backlight.max = pch_ctl2 >> 16;
980 if (!panel->backlight.max)
983 val = bdw_get_backlight(connector);
984 panel->backlight.level = intel_panel_compute_brightness(connector, val);
986 panel->backlight.enabled = (pch_ctl1 & BLM_PCH_PWM_ENABLE) &&
987 panel->backlight.level != 0;
992 static int pch_setup_backlight(struct intel_connector *connector)
994 struct drm_device *dev = connector->base.dev;
995 struct drm_i915_private *dev_priv = dev->dev_private;
996 struct intel_panel *panel = &connector->panel;
997 u32 cpu_ctl2, pch_ctl1, pch_ctl2, val;
999 pch_ctl1 = I915_READ(BLC_PWM_PCH_CTL1);
1000 panel->backlight.active_low_pwm = pch_ctl1 & BLM_PCH_POLARITY;
1002 pch_ctl2 = I915_READ(BLC_PWM_PCH_CTL2);
1003 panel->backlight.max = pch_ctl2 >> 16;
1004 if (!panel->backlight.max)
1007 val = pch_get_backlight(connector);
1008 panel->backlight.level = intel_panel_compute_brightness(connector, val);
1010 cpu_ctl2 = I915_READ(BLC_PWM_CPU_CTL2);
1011 panel->backlight.enabled = (cpu_ctl2 & BLM_PWM_ENABLE) &&
1012 (pch_ctl1 & BLM_PCH_PWM_ENABLE) && panel->backlight.level != 0;
1017 static int i9xx_setup_backlight(struct intel_connector *connector)
1019 struct drm_device *dev = connector->base.dev;
1020 struct drm_i915_private *dev_priv = dev->dev_private;
1021 struct intel_panel *panel = &connector->panel;
1024 ctl = I915_READ(BLC_PWM_CTL);
1026 if (IS_GEN2(dev) || IS_I915GM(dev) || IS_I945GM(dev))
1027 panel->backlight.combination_mode = ctl & BLM_LEGACY_MODE;
1029 if (IS_PINEVIEW(dev))
1030 panel->backlight.active_low_pwm = ctl & BLM_POLARITY_PNV;
1032 panel->backlight.max = ctl >> 17;
1033 if (panel->backlight.combination_mode)
1034 panel->backlight.max *= 0xff;
1036 if (!panel->backlight.max)
1039 val = i9xx_get_backlight(connector);
1040 panel->backlight.level = intel_panel_compute_brightness(connector, val);
1042 panel->backlight.enabled = panel->backlight.level != 0;
1047 static int i965_setup_backlight(struct intel_connector *connector)
1049 struct drm_device *dev = connector->base.dev;
1050 struct drm_i915_private *dev_priv = dev->dev_private;
1051 struct intel_panel *panel = &connector->panel;
1054 ctl2 = I915_READ(BLC_PWM_CTL2);
1055 panel->backlight.combination_mode = ctl2 & BLM_COMBINATION_MODE;
1056 panel->backlight.active_low_pwm = ctl2 & BLM_POLARITY_I965;
1058 ctl = I915_READ(BLC_PWM_CTL);
1059 panel->backlight.max = ctl >> 16;
1060 if (panel->backlight.combination_mode)
1061 panel->backlight.max *= 0xff;
1063 if (!panel->backlight.max)
1066 val = i9xx_get_backlight(connector);
1067 panel->backlight.level = intel_panel_compute_brightness(connector, val);
1069 panel->backlight.enabled = (ctl2 & BLM_PWM_ENABLE) &&
1070 panel->backlight.level != 0;
1075 static int vlv_setup_backlight(struct intel_connector *connector)
1077 struct drm_device *dev = connector->base.dev;
1078 struct drm_i915_private *dev_priv = dev->dev_private;
1079 struct intel_panel *panel = &connector->panel;
1083 for_each_pipe(pipe) {
1084 u32 cur_val = I915_READ(VLV_BLC_PWM_CTL(pipe));
1086 /* Skip if the modulation freq is already set */
1087 if (cur_val & ~BACKLIGHT_DUTY_CYCLE_MASK)
1090 cur_val &= BACKLIGHT_DUTY_CYCLE_MASK;
1091 I915_WRITE(VLV_BLC_PWM_CTL(pipe), (0xf42 << 16) |
1095 ctl2 = I915_READ(VLV_BLC_PWM_CTL2(PIPE_A));
1096 panel->backlight.active_low_pwm = ctl2 & BLM_POLARITY_I965;
1098 ctl = I915_READ(VLV_BLC_PWM_CTL(PIPE_A));
1099 panel->backlight.max = ctl >> 16;
1100 if (!panel->backlight.max)
1103 val = _vlv_get_backlight(dev, PIPE_A);
1104 panel->backlight.level = intel_panel_compute_brightness(connector, val);
1106 panel->backlight.enabled = (ctl2 & BLM_PWM_ENABLE) &&
1107 panel->backlight.level != 0;
1112 int intel_panel_setup_backlight(struct drm_connector *connector)
1114 struct drm_device *dev = connector->dev;
1115 struct drm_i915_private *dev_priv = dev->dev_private;
1116 struct intel_connector *intel_connector = to_intel_connector(connector);
1117 struct intel_panel *panel = &intel_connector->panel;
1118 unsigned long flags;
1121 if (!dev_priv->vbt.backlight.present) {
1122 DRM_DEBUG_KMS("native backlight control not available per VBT\n");
1126 /* set level and max in panel struct */
1127 spin_lock_irqsave(&dev_priv->backlight_lock, flags);
1128 ret = dev_priv->display.setup_backlight(intel_connector);
1129 spin_unlock_irqrestore(&dev_priv->backlight_lock, flags);
1132 DRM_DEBUG_KMS("failed to setup backlight for connector %s\n",
1133 drm_get_connector_name(connector));
1137 intel_backlight_device_register(intel_connector);
1139 panel->backlight.present = true;
1141 DRM_DEBUG_KMS("backlight initialized, %s, brightness %u/%u, "
1142 "sysfs interface %sregistered\n",
1143 panel->backlight.enabled ? "enabled" : "disabled",
1144 panel->backlight.level, panel->backlight.max,
1145 panel->backlight.device ? "" : "not ");
1150 void intel_panel_destroy_backlight(struct drm_connector *connector)
1152 struct intel_connector *intel_connector = to_intel_connector(connector);
1153 struct intel_panel *panel = &intel_connector->panel;
1155 panel->backlight.present = false;
1156 intel_backlight_device_unregister(intel_connector);
1159 /* Set up chip specific backlight functions */
1160 void intel_panel_init_backlight_funcs(struct drm_device *dev)
1162 struct drm_i915_private *dev_priv = dev->dev_private;
1164 if (IS_BROADWELL(dev)) {
1165 dev_priv->display.setup_backlight = bdw_setup_backlight;
1166 dev_priv->display.enable_backlight = bdw_enable_backlight;
1167 dev_priv->display.disable_backlight = pch_disable_backlight;
1168 dev_priv->display.set_backlight = bdw_set_backlight;
1169 dev_priv->display.get_backlight = bdw_get_backlight;
1170 } else if (HAS_PCH_SPLIT(dev)) {
1171 dev_priv->display.setup_backlight = pch_setup_backlight;
1172 dev_priv->display.enable_backlight = pch_enable_backlight;
1173 dev_priv->display.disable_backlight = pch_disable_backlight;
1174 dev_priv->display.set_backlight = pch_set_backlight;
1175 dev_priv->display.get_backlight = pch_get_backlight;
1176 } else if (IS_VALLEYVIEW(dev)) {
1177 dev_priv->display.setup_backlight = vlv_setup_backlight;
1178 dev_priv->display.enable_backlight = vlv_enable_backlight;
1179 dev_priv->display.disable_backlight = vlv_disable_backlight;
1180 dev_priv->display.set_backlight = vlv_set_backlight;
1181 dev_priv->display.get_backlight = vlv_get_backlight;
1182 } else if (IS_GEN4(dev)) {
1183 dev_priv->display.setup_backlight = i965_setup_backlight;
1184 dev_priv->display.enable_backlight = i965_enable_backlight;
1185 dev_priv->display.disable_backlight = i965_disable_backlight;
1186 dev_priv->display.set_backlight = i9xx_set_backlight;
1187 dev_priv->display.get_backlight = i9xx_get_backlight;
1189 dev_priv->display.setup_backlight = i9xx_setup_backlight;
1190 dev_priv->display.enable_backlight = i9xx_enable_backlight;
1191 dev_priv->display.disable_backlight = i9xx_disable_backlight;
1192 dev_priv->display.set_backlight = i9xx_set_backlight;
1193 dev_priv->display.get_backlight = i9xx_get_backlight;
1197 int intel_panel_init(struct intel_panel *panel,
1198 struct drm_display_mode *fixed_mode,
1199 struct drm_display_mode *downclock_mode)
1201 panel->fixed_mode = fixed_mode;
1202 panel->downclock_mode = downclock_mode;
1207 void intel_panel_fini(struct intel_panel *panel)
1209 struct intel_connector *intel_connector =
1210 container_of(panel, struct intel_connector, panel);
1212 if (panel->fixed_mode)
1213 drm_mode_destroy(intel_connector->base.dev, panel->fixed_mode);
1215 if (panel->downclock_mode)
1216 drm_mode_destroy(intel_connector->base.dev,
1217 panel->downclock_mode);