Linux 4.2.1
[linux/fpc-iii.git] / drivers / gpu / drm / i915 / intel_panel.c
blob55aad2322e10ec8e7ea168aa84e0b7d98dcf9b91
1 /*
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
14 * Software.
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.
24 * Authors:
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/kernel.h>
34 #include <linux/moduleparam.h>
35 #include "intel_drv.h"
37 void
38 intel_fixed_panel_mode(const struct drm_display_mode *fixed_mode,
39 struct drm_display_mode *adjusted_mode)
41 drm_mode_copy(adjusted_mode, fixed_mode);
43 drm_mode_set_crtcinfo(adjusted_mode, 0);
46 /**
47 * intel_find_panel_downclock - find the reduced downclock for LVDS in EDID
48 * @dev: drm device
49 * @fixed_mode : panel native mode
50 * @connector: LVDS/eDP connector
52 * Return downclock_avail
53 * Find the reduced downclock for LVDS/eDP in EDID.
55 struct drm_display_mode *
56 intel_find_panel_downclock(struct drm_device *dev,
57 struct drm_display_mode *fixed_mode,
58 struct drm_connector *connector)
60 struct drm_display_mode *scan, *tmp_mode;
61 int temp_downclock;
63 temp_downclock = fixed_mode->clock;
64 tmp_mode = NULL;
66 list_for_each_entry(scan, &connector->probed_modes, head) {
68 * If one mode has the same resolution with the fixed_panel
69 * mode while they have the different refresh rate, it means
70 * that the reduced downclock is found. In such
71 * case we can set the different FPx0/1 to dynamically select
72 * between low and high frequency.
74 if (scan->hdisplay == fixed_mode->hdisplay &&
75 scan->hsync_start == fixed_mode->hsync_start &&
76 scan->hsync_end == fixed_mode->hsync_end &&
77 scan->htotal == fixed_mode->htotal &&
78 scan->vdisplay == fixed_mode->vdisplay &&
79 scan->vsync_start == fixed_mode->vsync_start &&
80 scan->vsync_end == fixed_mode->vsync_end &&
81 scan->vtotal == fixed_mode->vtotal) {
82 if (scan->clock < temp_downclock) {
84 * The downclock is already found. But we
85 * expect to find the lower downclock.
87 temp_downclock = scan->clock;
88 tmp_mode = scan;
93 if (temp_downclock < fixed_mode->clock)
94 return drm_mode_duplicate(dev, tmp_mode);
95 else
96 return NULL;
99 /* adjusted_mode has been preset to be the panel's fixed mode */
100 void
101 intel_pch_panel_fitting(struct intel_crtc *intel_crtc,
102 struct intel_crtc_state *pipe_config,
103 int fitting_mode)
105 struct drm_display_mode *adjusted_mode;
106 int x, y, width, height;
108 adjusted_mode = &pipe_config->base.adjusted_mode;
110 x = y = width = height = 0;
112 /* Native modes don't need fitting */
113 if (adjusted_mode->hdisplay == pipe_config->pipe_src_w &&
114 adjusted_mode->vdisplay == pipe_config->pipe_src_h)
115 goto done;
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->hdisplay - width + 1)/2;
122 y = (adjusted_mode->vdisplay - height + 1)/2;
123 break;
125 case DRM_MODE_SCALE_ASPECT:
126 /* Scale but preserve the aspect ratio */
128 u32 scaled_width = adjusted_mode->hdisplay
129 * pipe_config->pipe_src_h;
130 u32 scaled_height = pipe_config->pipe_src_w
131 * adjusted_mode->vdisplay;
132 if (scaled_width > scaled_height) { /* pillar */
133 width = scaled_height / pipe_config->pipe_src_h;
134 if (width & 1)
135 width++;
136 x = (adjusted_mode->hdisplay - width + 1) / 2;
137 y = 0;
138 height = adjusted_mode->vdisplay;
139 } else if (scaled_width < scaled_height) { /* letter */
140 height = scaled_width / pipe_config->pipe_src_w;
141 if (height & 1)
142 height++;
143 y = (adjusted_mode->vdisplay - height + 1) / 2;
144 x = 0;
145 width = adjusted_mode->hdisplay;
146 } else {
147 x = y = 0;
148 width = adjusted_mode->hdisplay;
149 height = adjusted_mode->vdisplay;
152 break;
154 case DRM_MODE_SCALE_FULLSCREEN:
155 x = y = 0;
156 width = adjusted_mode->hdisplay;
157 height = adjusted_mode->vdisplay;
158 break;
160 default:
161 WARN(1, "bad panel fit mode: %d\n", fitting_mode);
162 return;
165 done:
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;
171 static void
172 centre_horizontally(struct drm_display_mode *mode,
173 int width)
175 u32 border, sync_pos, blank_width, sync_width;
177 /* keep the hsync and hblank widths constant */
178 sync_width = mode->crtc_hsync_end - mode->crtc_hsync_start;
179 blank_width = mode->crtc_hblank_end - mode->crtc_hblank_start;
180 sync_pos = (blank_width - sync_width + 1) / 2;
182 border = (mode->hdisplay - width + 1) / 2;
183 border += border & 1; /* make the border even */
185 mode->crtc_hdisplay = width;
186 mode->crtc_hblank_start = width + border;
187 mode->crtc_hblank_end = mode->crtc_hblank_start + blank_width;
189 mode->crtc_hsync_start = mode->crtc_hblank_start + sync_pos;
190 mode->crtc_hsync_end = mode->crtc_hsync_start + sync_width;
193 static void
194 centre_vertically(struct drm_display_mode *mode,
195 int height)
197 u32 border, sync_pos, blank_width, sync_width;
199 /* keep the vsync and vblank widths constant */
200 sync_width = mode->crtc_vsync_end - mode->crtc_vsync_start;
201 blank_width = mode->crtc_vblank_end - mode->crtc_vblank_start;
202 sync_pos = (blank_width - sync_width + 1) / 2;
204 border = (mode->vdisplay - height + 1) / 2;
206 mode->crtc_vdisplay = height;
207 mode->crtc_vblank_start = height + border;
208 mode->crtc_vblank_end = mode->crtc_vblank_start + blank_width;
210 mode->crtc_vsync_start = mode->crtc_vblank_start + sync_pos;
211 mode->crtc_vsync_end = 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.
221 #define ACCURACY 12
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,
228 u32 *pfit_control)
230 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
231 u32 scaled_width = adjusted_mode->hdisplay *
232 pipe_config->pipe_src_h;
233 u32 scaled_height = pipe_config->pipe_src_w *
234 adjusted_mode->vdisplay;
236 /* 965+ is easy, it does everything in hw */
237 if (scaled_width > scaled_height)
238 *pfit_control |= PFIT_ENABLE |
239 PFIT_SCALING_PILLAR;
240 else if (scaled_width < scaled_height)
241 *pfit_control |= PFIT_ENABLE |
242 PFIT_SCALING_LETTER;
243 else if (adjusted_mode->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,
249 u32 *border)
251 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
252 u32 scaled_width = adjusted_mode->hdisplay *
253 pipe_config->pipe_src_h;
254 u32 scaled_height = pipe_config->pipe_src_w *
255 adjusted_mode->vdisplay;
256 u32 bits;
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,
265 scaled_height /
266 pipe_config->pipe_src_h);
268 *border = LVDS_BORDER_ENABLE;
269 if (pipe_config->pipe_src_h != adjusted_mode->vdisplay) {
270 bits = panel_fitter_scaling(pipe_config->pipe_src_h,
271 adjusted_mode->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,
281 scaled_width /
282 pipe_config->pipe_src_w);
284 *border = LVDS_BORDER_ENABLE;
285 if (pipe_config->pipe_src_w != adjusted_mode->hdisplay) {
286 bits = panel_fitter_scaling(pipe_config->pipe_src_w,
287 adjusted_mode->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);
295 } else {
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,
306 int fitting_mode)
308 struct drm_device *dev = intel_crtc->base.dev;
309 u32 pfit_control = 0, pfit_pgm_ratios = 0, border = 0;
310 struct drm_display_mode *adjusted_mode;
312 adjusted_mode = &pipe_config->base.adjusted_mode;
314 /* Native modes don't need fitting */
315 if (adjusted_mode->hdisplay == pipe_config->pipe_src_w &&
316 adjusted_mode->vdisplay == pipe_config->pipe_src_h)
317 goto out;
319 switch (fitting_mode) {
320 case DRM_MODE_SCALE_CENTER:
322 * For centered modes, we have to calculate border widths &
323 * heights and modify the values programmed into the CRTC.
325 centre_horizontally(adjusted_mode, pipe_config->pipe_src_w);
326 centre_vertically(adjusted_mode, pipe_config->pipe_src_h);
327 border = LVDS_BORDER_ENABLE;
328 break;
329 case DRM_MODE_SCALE_ASPECT:
330 /* Scale but preserve the aspect ratio */
331 if (INTEL_INFO(dev)->gen >= 4)
332 i965_scale_aspect(pipe_config, &pfit_control);
333 else
334 i9xx_scale_aspect(pipe_config, &pfit_control,
335 &pfit_pgm_ratios, &border);
336 break;
337 case DRM_MODE_SCALE_FULLSCREEN:
339 * Full scaling, even if it changes the aspect ratio.
340 * Fortunately this is all done for us in hw.
342 if (pipe_config->pipe_src_h != adjusted_mode->vdisplay ||
343 pipe_config->pipe_src_w != adjusted_mode->hdisplay) {
344 pfit_control |= PFIT_ENABLE;
345 if (INTEL_INFO(dev)->gen >= 4)
346 pfit_control |= PFIT_SCALING_AUTO;
347 else
348 pfit_control |= (VERT_AUTO_SCALE |
349 VERT_INTERP_BILINEAR |
350 HORIZ_AUTO_SCALE |
351 HORIZ_INTERP_BILINEAR);
353 break;
354 default:
355 WARN(1, "bad panel fit mode: %d\n", fitting_mode);
356 return;
359 /* 965+ wants fuzzy fitting */
360 /* FIXME: handle multiple panels by failing gracefully */
361 if (INTEL_INFO(dev)->gen >= 4)
362 pfit_control |= ((intel_crtc->pipe << PFIT_PIPE_SHIFT) |
363 PFIT_FILTER_FUZZY);
365 out:
366 if ((pfit_control & PFIT_ENABLE) == 0) {
367 pfit_control = 0;
368 pfit_pgm_ratios = 0;
371 /* Make sure pre-965 set dither correctly for 18bpp panels. */
372 if (INTEL_INFO(dev)->gen < 4 && pipe_config->pipe_bpp == 18)
373 pfit_control |= PANEL_8TO6_DITHER_ENABLE;
375 pipe_config->gmch_pfit.control = pfit_control;
376 pipe_config->gmch_pfit.pgm_ratios = pfit_pgm_ratios;
377 pipe_config->gmch_pfit.lvds_border_bits = border;
380 enum drm_connector_status
381 intel_panel_detect(struct drm_device *dev)
383 struct drm_i915_private *dev_priv = dev->dev_private;
385 /* Assume that the BIOS does not lie through the OpRegion... */
386 if (!i915.panel_ignore_lid && dev_priv->opregion.lid_state) {
387 return ioread32(dev_priv->opregion.lid_state) & 0x1 ?
388 connector_status_connected :
389 connector_status_disconnected;
392 switch (i915.panel_ignore_lid) {
393 case -2:
394 return connector_status_connected;
395 case -1:
396 return connector_status_disconnected;
397 default:
398 return connector_status_unknown;
403 * scale - scale values from one range to another
405 * @source_val: value in range [@source_min..@source_max]
407 * Return @source_val in range [@source_min..@source_max] scaled to range
408 * [@target_min..@target_max].
410 static uint32_t scale(uint32_t source_val,
411 uint32_t source_min, uint32_t source_max,
412 uint32_t target_min, uint32_t target_max)
414 uint64_t target_val;
416 WARN_ON(source_min > source_max);
417 WARN_ON(target_min > target_max);
419 /* defensive */
420 source_val = clamp(source_val, source_min, source_max);
422 /* avoid overflows */
423 target_val = DIV_ROUND_CLOSEST_ULL((uint64_t)(source_val - source_min) *
424 (target_max - target_min), source_max - source_min);
425 target_val += target_min;
427 return target_val;
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;
446 u32 hw_level;
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);
451 return hw_level;
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,
461 0, user_max);
464 static u32 intel_panel_compute_brightness(struct intel_connector *connector,
465 u32 val)
467 struct drm_device *dev = connector->base.dev;
468 struct drm_i915_private *dev_priv = dev->dev_private;
469 struct intel_panel *panel = &connector->panel;
471 WARN_ON(panel->backlight.max == 0);
473 if (i915.invert_brightness < 0)
474 return val;
476 if (i915.invert_brightness > 0 ||
477 dev_priv->quirks & QUIRK_INVERT_BRIGHTNESS) {
478 return panel->backlight.max - val;
481 return val;
484 static u32 bdw_get_backlight(struct intel_connector *connector)
486 struct drm_device *dev = connector->base.dev;
487 struct drm_i915_private *dev_priv = dev->dev_private;
489 return I915_READ(BLC_PWM_PCH_CTL2) & BACKLIGHT_DUTY_CYCLE_MASK;
492 static u32 pch_get_backlight(struct intel_connector *connector)
494 struct drm_device *dev = connector->base.dev;
495 struct drm_i915_private *dev_priv = dev->dev_private;
497 return I915_READ(BLC_PWM_CPU_CTL) & BACKLIGHT_DUTY_CYCLE_MASK;
500 static u32 i9xx_get_backlight(struct intel_connector *connector)
502 struct drm_device *dev = connector->base.dev;
503 struct drm_i915_private *dev_priv = dev->dev_private;
504 struct intel_panel *panel = &connector->panel;
505 u32 val;
507 val = I915_READ(BLC_PWM_CTL) & BACKLIGHT_DUTY_CYCLE_MASK;
508 if (INTEL_INFO(dev)->gen < 4)
509 val >>= 1;
511 if (panel->backlight.combination_mode) {
512 u8 lbpc;
514 pci_read_config_byte(dev->pdev, PCI_LBPC, &lbpc);
515 val *= lbpc;
518 return val;
521 static u32 _vlv_get_backlight(struct drm_device *dev, enum pipe pipe)
523 struct drm_i915_private *dev_priv = dev->dev_private;
525 if (WARN_ON(pipe != PIPE_A && pipe != PIPE_B))
526 return 0;
528 return I915_READ(VLV_BLC_PWM_CTL(pipe)) & BACKLIGHT_DUTY_CYCLE_MASK;
531 static u32 vlv_get_backlight(struct intel_connector *connector)
533 struct drm_device *dev = connector->base.dev;
534 enum pipe pipe = intel_get_pipe_from_connector(connector);
536 return _vlv_get_backlight(dev, pipe);
539 static u32 bxt_get_backlight(struct intel_connector *connector)
541 struct drm_device *dev = connector->base.dev;
542 struct drm_i915_private *dev_priv = dev->dev_private;
544 return I915_READ(BXT_BLC_PWM_DUTY1);
547 static u32 intel_panel_get_backlight(struct intel_connector *connector)
549 struct drm_device *dev = connector->base.dev;
550 struct drm_i915_private *dev_priv = dev->dev_private;
551 struct intel_panel *panel = &connector->panel;
552 u32 val = 0;
554 mutex_lock(&dev_priv->backlight_lock);
556 if (panel->backlight.enabled) {
557 val = dev_priv->display.get_backlight(connector);
558 val = intel_panel_compute_brightness(connector, val);
561 mutex_unlock(&dev_priv->backlight_lock);
563 DRM_DEBUG_DRIVER("get backlight PWM = %d\n", val);
564 return val;
567 static void bdw_set_backlight(struct intel_connector *connector, u32 level)
569 struct drm_device *dev = connector->base.dev;
570 struct drm_i915_private *dev_priv = dev->dev_private;
571 u32 val = I915_READ(BLC_PWM_PCH_CTL2) & ~BACKLIGHT_DUTY_CYCLE_MASK;
572 I915_WRITE(BLC_PWM_PCH_CTL2, val | level);
575 static void pch_set_backlight(struct intel_connector *connector, u32 level)
577 struct drm_device *dev = connector->base.dev;
578 struct drm_i915_private *dev_priv = dev->dev_private;
579 u32 tmp;
581 tmp = I915_READ(BLC_PWM_CPU_CTL) & ~BACKLIGHT_DUTY_CYCLE_MASK;
582 I915_WRITE(BLC_PWM_CPU_CTL, tmp | level);
585 static void i9xx_set_backlight(struct intel_connector *connector, u32 level)
587 struct drm_device *dev = connector->base.dev;
588 struct drm_i915_private *dev_priv = dev->dev_private;
589 struct intel_panel *panel = &connector->panel;
590 u32 tmp, mask;
592 WARN_ON(panel->backlight.max == 0);
594 if (panel->backlight.combination_mode) {
595 u8 lbpc;
597 lbpc = level * 0xfe / panel->backlight.max + 1;
598 level /= lbpc;
599 pci_write_config_byte(dev->pdev, PCI_LBPC, lbpc);
602 if (IS_GEN4(dev)) {
603 mask = BACKLIGHT_DUTY_CYCLE_MASK;
604 } else {
605 level <<= 1;
606 mask = BACKLIGHT_DUTY_CYCLE_MASK_PNV;
609 tmp = I915_READ(BLC_PWM_CTL) & ~mask;
610 I915_WRITE(BLC_PWM_CTL, tmp | level);
613 static void vlv_set_backlight(struct intel_connector *connector, u32 level)
615 struct drm_device *dev = connector->base.dev;
616 struct drm_i915_private *dev_priv = dev->dev_private;
617 enum pipe pipe = intel_get_pipe_from_connector(connector);
618 u32 tmp;
620 if (WARN_ON(pipe != PIPE_A && pipe != PIPE_B))
621 return;
623 tmp = I915_READ(VLV_BLC_PWM_CTL(pipe)) & ~BACKLIGHT_DUTY_CYCLE_MASK;
624 I915_WRITE(VLV_BLC_PWM_CTL(pipe), tmp | level);
627 static void bxt_set_backlight(struct intel_connector *connector, u32 level)
629 struct drm_device *dev = connector->base.dev;
630 struct drm_i915_private *dev_priv = dev->dev_private;
632 I915_WRITE(BXT_BLC_PWM_DUTY1, level);
635 static void
636 intel_panel_actually_set_backlight(struct intel_connector *connector, u32 level)
638 struct drm_device *dev = connector->base.dev;
639 struct drm_i915_private *dev_priv = dev->dev_private;
641 DRM_DEBUG_DRIVER("set backlight PWM = %d\n", level);
643 level = intel_panel_compute_brightness(connector, level);
644 dev_priv->display.set_backlight(connector, level);
647 /* set backlight brightness to level in range [0..max], scaling wrt hw min */
648 static void intel_panel_set_backlight(struct intel_connector *connector,
649 u32 user_level, u32 user_max)
651 struct drm_device *dev = connector->base.dev;
652 struct drm_i915_private *dev_priv = dev->dev_private;
653 struct intel_panel *panel = &connector->panel;
654 u32 hw_level;
656 if (!panel->backlight.present)
657 return;
659 mutex_lock(&dev_priv->backlight_lock);
661 WARN_ON(panel->backlight.max == 0);
663 hw_level = scale_user_to_hw(connector, user_level, user_max);
664 panel->backlight.level = hw_level;
666 if (panel->backlight.enabled)
667 intel_panel_actually_set_backlight(connector, hw_level);
669 mutex_unlock(&dev_priv->backlight_lock);
672 /* set backlight brightness to level in range [0..max], assuming hw min is
673 * respected.
675 void intel_panel_set_backlight_acpi(struct intel_connector *connector,
676 u32 user_level, u32 user_max)
678 struct drm_device *dev = connector->base.dev;
679 struct drm_i915_private *dev_priv = dev->dev_private;
680 struct intel_panel *panel = &connector->panel;
681 enum pipe pipe = intel_get_pipe_from_connector(connector);
682 u32 hw_level;
685 * INVALID_PIPE may occur during driver init because
686 * connection_mutex isn't held across the entire backlight
687 * setup + modeset readout, and the BIOS can issue the
688 * requests at any time.
690 if (!panel->backlight.present || pipe == INVALID_PIPE)
691 return;
693 mutex_lock(&dev_priv->backlight_lock);
695 WARN_ON(panel->backlight.max == 0);
697 hw_level = clamp_user_to_hw(connector, user_level, user_max);
698 panel->backlight.level = hw_level;
700 if (panel->backlight.device)
701 panel->backlight.device->props.brightness =
702 scale_hw_to_user(connector,
703 panel->backlight.level,
704 panel->backlight.device->props.max_brightness);
706 if (panel->backlight.enabled)
707 intel_panel_actually_set_backlight(connector, hw_level);
709 mutex_unlock(&dev_priv->backlight_lock);
712 static void pch_disable_backlight(struct intel_connector *connector)
714 struct drm_device *dev = connector->base.dev;
715 struct drm_i915_private *dev_priv = dev->dev_private;
716 u32 tmp;
718 intel_panel_actually_set_backlight(connector, 0);
720 tmp = I915_READ(BLC_PWM_CPU_CTL2);
721 I915_WRITE(BLC_PWM_CPU_CTL2, tmp & ~BLM_PWM_ENABLE);
723 tmp = I915_READ(BLC_PWM_PCH_CTL1);
724 I915_WRITE(BLC_PWM_PCH_CTL1, tmp & ~BLM_PCH_PWM_ENABLE);
727 static void i9xx_disable_backlight(struct intel_connector *connector)
729 intel_panel_actually_set_backlight(connector, 0);
732 static void i965_disable_backlight(struct intel_connector *connector)
734 struct drm_device *dev = connector->base.dev;
735 struct drm_i915_private *dev_priv = dev->dev_private;
736 u32 tmp;
738 intel_panel_actually_set_backlight(connector, 0);
740 tmp = I915_READ(BLC_PWM_CTL2);
741 I915_WRITE(BLC_PWM_CTL2, tmp & ~BLM_PWM_ENABLE);
744 static void vlv_disable_backlight(struct intel_connector *connector)
746 struct drm_device *dev = connector->base.dev;
747 struct drm_i915_private *dev_priv = dev->dev_private;
748 enum pipe pipe = intel_get_pipe_from_connector(connector);
749 u32 tmp;
751 if (WARN_ON(pipe != PIPE_A && pipe != PIPE_B))
752 return;
754 intel_panel_actually_set_backlight(connector, 0);
756 tmp = I915_READ(VLV_BLC_PWM_CTL2(pipe));
757 I915_WRITE(VLV_BLC_PWM_CTL2(pipe), tmp & ~BLM_PWM_ENABLE);
760 static void bxt_disable_backlight(struct intel_connector *connector)
762 struct drm_device *dev = connector->base.dev;
763 struct drm_i915_private *dev_priv = dev->dev_private;
764 u32 tmp;
766 intel_panel_actually_set_backlight(connector, 0);
768 tmp = I915_READ(BXT_BLC_PWM_CTL1);
769 I915_WRITE(BXT_BLC_PWM_CTL1, tmp & ~BXT_BLC_PWM_ENABLE);
772 void intel_panel_disable_backlight(struct intel_connector *connector)
774 struct drm_device *dev = connector->base.dev;
775 struct drm_i915_private *dev_priv = dev->dev_private;
776 struct intel_panel *panel = &connector->panel;
778 if (!panel->backlight.present)
779 return;
782 * Do not disable backlight on the vgaswitcheroo path. When switching
783 * away from i915, the other client may depend on i915 to handle the
784 * backlight. This will leave the backlight on unnecessarily when
785 * another client is not activated.
787 if (dev->switch_power_state == DRM_SWITCH_POWER_CHANGING) {
788 DRM_DEBUG_DRIVER("Skipping backlight disable on vga switch\n");
789 return;
792 mutex_lock(&dev_priv->backlight_lock);
794 if (panel->backlight.device)
795 panel->backlight.device->props.power = FB_BLANK_POWERDOWN;
796 panel->backlight.enabled = false;
797 dev_priv->display.disable_backlight(connector);
799 mutex_unlock(&dev_priv->backlight_lock);
802 static void bdw_enable_backlight(struct intel_connector *connector)
804 struct drm_device *dev = connector->base.dev;
805 struct drm_i915_private *dev_priv = dev->dev_private;
806 struct intel_panel *panel = &connector->panel;
807 u32 pch_ctl1, pch_ctl2;
809 pch_ctl1 = I915_READ(BLC_PWM_PCH_CTL1);
810 if (pch_ctl1 & BLM_PCH_PWM_ENABLE) {
811 DRM_DEBUG_KMS("pch backlight already enabled\n");
812 pch_ctl1 &= ~BLM_PCH_PWM_ENABLE;
813 I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1);
816 pch_ctl2 = panel->backlight.max << 16;
817 I915_WRITE(BLC_PWM_PCH_CTL2, pch_ctl2);
819 pch_ctl1 = 0;
820 if (panel->backlight.active_low_pwm)
821 pch_ctl1 |= BLM_PCH_POLARITY;
823 /* After LPT, override is the default. */
824 if (HAS_PCH_LPT(dev_priv))
825 pch_ctl1 |= BLM_PCH_OVERRIDE_ENABLE;
827 I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1);
828 POSTING_READ(BLC_PWM_PCH_CTL1);
829 I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1 | BLM_PCH_PWM_ENABLE);
831 /* This won't stick until the above enable. */
832 intel_panel_actually_set_backlight(connector, panel->backlight.level);
835 static void pch_enable_backlight(struct intel_connector *connector)
837 struct drm_device *dev = connector->base.dev;
838 struct drm_i915_private *dev_priv = dev->dev_private;
839 struct intel_panel *panel = &connector->panel;
840 enum pipe pipe = intel_get_pipe_from_connector(connector);
841 enum transcoder cpu_transcoder =
842 intel_pipe_to_cpu_transcoder(dev_priv, pipe);
843 u32 cpu_ctl2, pch_ctl1, pch_ctl2;
845 cpu_ctl2 = I915_READ(BLC_PWM_CPU_CTL2);
846 if (cpu_ctl2 & BLM_PWM_ENABLE) {
847 DRM_DEBUG_KMS("cpu backlight already enabled\n");
848 cpu_ctl2 &= ~BLM_PWM_ENABLE;
849 I915_WRITE(BLC_PWM_CPU_CTL2, cpu_ctl2);
852 pch_ctl1 = I915_READ(BLC_PWM_PCH_CTL1);
853 if (pch_ctl1 & BLM_PCH_PWM_ENABLE) {
854 DRM_DEBUG_KMS("pch backlight already enabled\n");
855 pch_ctl1 &= ~BLM_PCH_PWM_ENABLE;
856 I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1);
859 if (cpu_transcoder == TRANSCODER_EDP)
860 cpu_ctl2 = BLM_TRANSCODER_EDP;
861 else
862 cpu_ctl2 = BLM_PIPE(cpu_transcoder);
863 I915_WRITE(BLC_PWM_CPU_CTL2, cpu_ctl2);
864 POSTING_READ(BLC_PWM_CPU_CTL2);
865 I915_WRITE(BLC_PWM_CPU_CTL2, cpu_ctl2 | BLM_PWM_ENABLE);
867 /* This won't stick until the above enable. */
868 intel_panel_actually_set_backlight(connector, panel->backlight.level);
870 pch_ctl2 = panel->backlight.max << 16;
871 I915_WRITE(BLC_PWM_PCH_CTL2, pch_ctl2);
873 pch_ctl1 = 0;
874 if (panel->backlight.active_low_pwm)
875 pch_ctl1 |= BLM_PCH_POLARITY;
877 I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1);
878 POSTING_READ(BLC_PWM_PCH_CTL1);
879 I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1 | BLM_PCH_PWM_ENABLE);
882 static void i9xx_enable_backlight(struct intel_connector *connector)
884 struct drm_device *dev = connector->base.dev;
885 struct drm_i915_private *dev_priv = dev->dev_private;
886 struct intel_panel *panel = &connector->panel;
887 u32 ctl, freq;
889 ctl = I915_READ(BLC_PWM_CTL);
890 if (ctl & BACKLIGHT_DUTY_CYCLE_MASK_PNV) {
891 DRM_DEBUG_KMS("backlight already enabled\n");
892 I915_WRITE(BLC_PWM_CTL, 0);
895 freq = panel->backlight.max;
896 if (panel->backlight.combination_mode)
897 freq /= 0xff;
899 ctl = freq << 17;
900 if (panel->backlight.combination_mode)
901 ctl |= BLM_LEGACY_MODE;
902 if (IS_PINEVIEW(dev) && panel->backlight.active_low_pwm)
903 ctl |= BLM_POLARITY_PNV;
905 I915_WRITE(BLC_PWM_CTL, ctl);
906 POSTING_READ(BLC_PWM_CTL);
908 /* XXX: combine this into above write? */
909 intel_panel_actually_set_backlight(connector, panel->backlight.level);
912 * Needed to enable backlight on some 855gm models. BLC_HIST_CTL is
913 * 855gm only, but checking for gen2 is safe, as 855gm is the only gen2
914 * that has backlight.
916 if (IS_GEN2(dev))
917 I915_WRITE(BLC_HIST_CTL, BLM_HISTOGRAM_ENABLE);
920 static void i965_enable_backlight(struct intel_connector *connector)
922 struct drm_device *dev = connector->base.dev;
923 struct drm_i915_private *dev_priv = dev->dev_private;
924 struct intel_panel *panel = &connector->panel;
925 enum pipe pipe = intel_get_pipe_from_connector(connector);
926 u32 ctl, ctl2, freq;
928 ctl2 = I915_READ(BLC_PWM_CTL2);
929 if (ctl2 & BLM_PWM_ENABLE) {
930 DRM_DEBUG_KMS("backlight already enabled\n");
931 ctl2 &= ~BLM_PWM_ENABLE;
932 I915_WRITE(BLC_PWM_CTL2, ctl2);
935 freq = panel->backlight.max;
936 if (panel->backlight.combination_mode)
937 freq /= 0xff;
939 ctl = freq << 16;
940 I915_WRITE(BLC_PWM_CTL, ctl);
942 ctl2 = BLM_PIPE(pipe);
943 if (panel->backlight.combination_mode)
944 ctl2 |= BLM_COMBINATION_MODE;
945 if (panel->backlight.active_low_pwm)
946 ctl2 |= BLM_POLARITY_I965;
947 I915_WRITE(BLC_PWM_CTL2, ctl2);
948 POSTING_READ(BLC_PWM_CTL2);
949 I915_WRITE(BLC_PWM_CTL2, ctl2 | BLM_PWM_ENABLE);
951 intel_panel_actually_set_backlight(connector, panel->backlight.level);
954 static void vlv_enable_backlight(struct intel_connector *connector)
956 struct drm_device *dev = connector->base.dev;
957 struct drm_i915_private *dev_priv = dev->dev_private;
958 struct intel_panel *panel = &connector->panel;
959 enum pipe pipe = intel_get_pipe_from_connector(connector);
960 u32 ctl, ctl2;
962 if (WARN_ON(pipe != PIPE_A && pipe != PIPE_B))
963 return;
965 ctl2 = I915_READ(VLV_BLC_PWM_CTL2(pipe));
966 if (ctl2 & BLM_PWM_ENABLE) {
967 DRM_DEBUG_KMS("backlight already enabled\n");
968 ctl2 &= ~BLM_PWM_ENABLE;
969 I915_WRITE(VLV_BLC_PWM_CTL2(pipe), ctl2);
972 ctl = panel->backlight.max << 16;
973 I915_WRITE(VLV_BLC_PWM_CTL(pipe), ctl);
975 /* XXX: combine this into above write? */
976 intel_panel_actually_set_backlight(connector, panel->backlight.level);
978 ctl2 = 0;
979 if (panel->backlight.active_low_pwm)
980 ctl2 |= BLM_POLARITY_I965;
981 I915_WRITE(VLV_BLC_PWM_CTL2(pipe), ctl2);
982 POSTING_READ(VLV_BLC_PWM_CTL2(pipe));
983 I915_WRITE(VLV_BLC_PWM_CTL2(pipe), ctl2 | BLM_PWM_ENABLE);
986 static void bxt_enable_backlight(struct intel_connector *connector)
988 struct drm_device *dev = connector->base.dev;
989 struct drm_i915_private *dev_priv = dev->dev_private;
990 struct intel_panel *panel = &connector->panel;
991 u32 pwm_ctl;
993 pwm_ctl = I915_READ(BXT_BLC_PWM_CTL1);
994 if (pwm_ctl & BXT_BLC_PWM_ENABLE) {
995 DRM_DEBUG_KMS("backlight already enabled\n");
996 pwm_ctl &= ~BXT_BLC_PWM_ENABLE;
997 I915_WRITE(BXT_BLC_PWM_CTL1, pwm_ctl);
1000 I915_WRITE(BXT_BLC_PWM_FREQ1, panel->backlight.max);
1002 intel_panel_actually_set_backlight(connector, panel->backlight.level);
1004 pwm_ctl = 0;
1005 if (panel->backlight.active_low_pwm)
1006 pwm_ctl |= BXT_BLC_PWM_POLARITY;
1008 I915_WRITE(BXT_BLC_PWM_CTL1, pwm_ctl);
1009 POSTING_READ(BXT_BLC_PWM_CTL1);
1010 I915_WRITE(BXT_BLC_PWM_CTL1, pwm_ctl | BXT_BLC_PWM_ENABLE);
1013 void intel_panel_enable_backlight(struct intel_connector *connector)
1015 struct drm_device *dev = connector->base.dev;
1016 struct drm_i915_private *dev_priv = dev->dev_private;
1017 struct intel_panel *panel = &connector->panel;
1018 enum pipe pipe = intel_get_pipe_from_connector(connector);
1020 if (!panel->backlight.present)
1021 return;
1023 DRM_DEBUG_KMS("pipe %c\n", pipe_name(pipe));
1025 mutex_lock(&dev_priv->backlight_lock);
1027 WARN_ON(panel->backlight.max == 0);
1029 if (panel->backlight.level <= panel->backlight.min) {
1030 panel->backlight.level = panel->backlight.max;
1031 if (panel->backlight.device)
1032 panel->backlight.device->props.brightness =
1033 scale_hw_to_user(connector,
1034 panel->backlight.level,
1035 panel->backlight.device->props.max_brightness);
1038 dev_priv->display.enable_backlight(connector);
1039 panel->backlight.enabled = true;
1040 if (panel->backlight.device)
1041 panel->backlight.device->props.power = FB_BLANK_UNBLANK;
1043 mutex_unlock(&dev_priv->backlight_lock);
1046 #if IS_ENABLED(CONFIG_BACKLIGHT_CLASS_DEVICE)
1047 static int intel_backlight_device_update_status(struct backlight_device *bd)
1049 struct intel_connector *connector = bl_get_data(bd);
1050 struct intel_panel *panel = &connector->panel;
1051 struct drm_device *dev = connector->base.dev;
1053 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1054 DRM_DEBUG_KMS("updating intel_backlight, brightness=%d/%d\n",
1055 bd->props.brightness, bd->props.max_brightness);
1056 intel_panel_set_backlight(connector, bd->props.brightness,
1057 bd->props.max_brightness);
1060 * Allow flipping bl_power as a sub-state of enabled. Sadly the
1061 * backlight class device does not make it easy to to differentiate
1062 * between callbacks for brightness and bl_power, so our backlight_power
1063 * callback needs to take this into account.
1065 if (panel->backlight.enabled) {
1066 if (panel->backlight_power) {
1067 bool enable = bd->props.power == FB_BLANK_UNBLANK &&
1068 bd->props.brightness != 0;
1069 panel->backlight_power(connector, enable);
1071 } else {
1072 bd->props.power = FB_BLANK_POWERDOWN;
1075 drm_modeset_unlock(&dev->mode_config.connection_mutex);
1076 return 0;
1079 static int intel_backlight_device_get_brightness(struct backlight_device *bd)
1081 struct intel_connector *connector = bl_get_data(bd);
1082 struct drm_device *dev = connector->base.dev;
1083 struct drm_i915_private *dev_priv = dev->dev_private;
1084 u32 hw_level;
1085 int ret;
1087 intel_runtime_pm_get(dev_priv);
1088 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1090 hw_level = intel_panel_get_backlight(connector);
1091 ret = scale_hw_to_user(connector, hw_level, bd->props.max_brightness);
1093 drm_modeset_unlock(&dev->mode_config.connection_mutex);
1094 intel_runtime_pm_put(dev_priv);
1096 return ret;
1099 static const struct backlight_ops intel_backlight_device_ops = {
1100 .update_status = intel_backlight_device_update_status,
1101 .get_brightness = intel_backlight_device_get_brightness,
1104 static int intel_backlight_device_register(struct intel_connector *connector)
1106 struct intel_panel *panel = &connector->panel;
1107 struct backlight_properties props;
1109 if (WARN_ON(panel->backlight.device))
1110 return -ENODEV;
1112 if (!panel->backlight.present)
1113 return 0;
1115 WARN_ON(panel->backlight.max == 0);
1117 memset(&props, 0, sizeof(props));
1118 props.type = BACKLIGHT_RAW;
1121 * Note: Everything should work even if the backlight device max
1122 * presented to the userspace is arbitrarily chosen.
1124 props.max_brightness = panel->backlight.max;
1125 props.brightness = scale_hw_to_user(connector,
1126 panel->backlight.level,
1127 props.max_brightness);
1129 if (panel->backlight.enabled)
1130 props.power = FB_BLANK_UNBLANK;
1131 else
1132 props.power = FB_BLANK_POWERDOWN;
1135 * Note: using the same name independent of the connector prevents
1136 * registration of multiple backlight devices in the driver.
1138 panel->backlight.device =
1139 backlight_device_register("intel_backlight",
1140 connector->base.kdev,
1141 connector,
1142 &intel_backlight_device_ops, &props);
1144 if (IS_ERR(panel->backlight.device)) {
1145 DRM_ERROR("Failed to register backlight: %ld\n",
1146 PTR_ERR(panel->backlight.device));
1147 panel->backlight.device = NULL;
1148 return -ENODEV;
1151 DRM_DEBUG_KMS("Connector %s backlight sysfs interface registered\n",
1152 connector->base.name);
1154 return 0;
1157 static void intel_backlight_device_unregister(struct intel_connector *connector)
1159 struct intel_panel *panel = &connector->panel;
1161 if (panel->backlight.device) {
1162 backlight_device_unregister(panel->backlight.device);
1163 panel->backlight.device = NULL;
1166 #else /* CONFIG_BACKLIGHT_CLASS_DEVICE */
1167 static int intel_backlight_device_register(struct intel_connector *connector)
1169 return 0;
1171 static void intel_backlight_device_unregister(struct intel_connector *connector)
1174 #endif /* CONFIG_BACKLIGHT_CLASS_DEVICE */
1177 * Note: The setup hooks can't assume pipe is set!
1179 * XXX: Query mode clock or hardware clock and program PWM modulation frequency
1180 * appropriately when it's 0. Use VBT and/or sane defaults.
1182 static u32 get_backlight_min_vbt(struct intel_connector *connector)
1184 struct drm_device *dev = connector->base.dev;
1185 struct drm_i915_private *dev_priv = dev->dev_private;
1186 struct intel_panel *panel = &connector->panel;
1187 int min;
1189 WARN_ON(panel->backlight.max == 0);
1192 * XXX: If the vbt value is 255, it makes min equal to max, which leads
1193 * to problems. There are such machines out there. Either our
1194 * interpretation is wrong or the vbt has bogus data. Or both. Safeguard
1195 * against this by letting the minimum be at most (arbitrarily chosen)
1196 * 25% of the max.
1198 min = clamp_t(int, dev_priv->vbt.backlight.min_brightness, 0, 64);
1199 if (min != dev_priv->vbt.backlight.min_brightness) {
1200 DRM_DEBUG_KMS("clamping VBT min backlight %d/255 to %d/255\n",
1201 dev_priv->vbt.backlight.min_brightness, min);
1204 /* vbt value is a coefficient in range [0..255] */
1205 return scale(min, 0, 255, 0, panel->backlight.max);
1208 static int bdw_setup_backlight(struct intel_connector *connector, enum pipe unused)
1210 struct drm_device *dev = connector->base.dev;
1211 struct drm_i915_private *dev_priv = dev->dev_private;
1212 struct intel_panel *panel = &connector->panel;
1213 u32 pch_ctl1, pch_ctl2, val;
1215 pch_ctl1 = I915_READ(BLC_PWM_PCH_CTL1);
1216 panel->backlight.active_low_pwm = pch_ctl1 & BLM_PCH_POLARITY;
1218 pch_ctl2 = I915_READ(BLC_PWM_PCH_CTL2);
1219 panel->backlight.max = pch_ctl2 >> 16;
1220 if (!panel->backlight.max)
1221 return -ENODEV;
1223 panel->backlight.min = get_backlight_min_vbt(connector);
1225 val = bdw_get_backlight(connector);
1226 panel->backlight.level = intel_panel_compute_brightness(connector, val);
1228 panel->backlight.enabled = (pch_ctl1 & BLM_PCH_PWM_ENABLE) &&
1229 panel->backlight.level != 0;
1231 return 0;
1234 static int pch_setup_backlight(struct intel_connector *connector, enum pipe unused)
1236 struct drm_device *dev = connector->base.dev;
1237 struct drm_i915_private *dev_priv = dev->dev_private;
1238 struct intel_panel *panel = &connector->panel;
1239 u32 cpu_ctl2, pch_ctl1, pch_ctl2, val;
1241 pch_ctl1 = I915_READ(BLC_PWM_PCH_CTL1);
1242 panel->backlight.active_low_pwm = pch_ctl1 & BLM_PCH_POLARITY;
1244 pch_ctl2 = I915_READ(BLC_PWM_PCH_CTL2);
1245 panel->backlight.max = pch_ctl2 >> 16;
1246 if (!panel->backlight.max)
1247 return -ENODEV;
1249 panel->backlight.min = get_backlight_min_vbt(connector);
1251 val = pch_get_backlight(connector);
1252 panel->backlight.level = intel_panel_compute_brightness(connector, val);
1254 cpu_ctl2 = I915_READ(BLC_PWM_CPU_CTL2);
1255 panel->backlight.enabled = (cpu_ctl2 & BLM_PWM_ENABLE) &&
1256 (pch_ctl1 & BLM_PCH_PWM_ENABLE) && panel->backlight.level != 0;
1258 return 0;
1261 static int i9xx_setup_backlight(struct intel_connector *connector, enum pipe unused)
1263 struct drm_device *dev = connector->base.dev;
1264 struct drm_i915_private *dev_priv = dev->dev_private;
1265 struct intel_panel *panel = &connector->panel;
1266 u32 ctl, val;
1268 ctl = I915_READ(BLC_PWM_CTL);
1270 if (IS_GEN2(dev) || IS_I915GM(dev) || IS_I945GM(dev))
1271 panel->backlight.combination_mode = ctl & BLM_LEGACY_MODE;
1273 if (IS_PINEVIEW(dev))
1274 panel->backlight.active_low_pwm = ctl & BLM_POLARITY_PNV;
1276 panel->backlight.max = ctl >> 17;
1277 if (panel->backlight.combination_mode)
1278 panel->backlight.max *= 0xff;
1280 if (!panel->backlight.max)
1281 return -ENODEV;
1283 panel->backlight.min = get_backlight_min_vbt(connector);
1285 val = i9xx_get_backlight(connector);
1286 panel->backlight.level = intel_panel_compute_brightness(connector, val);
1288 panel->backlight.enabled = panel->backlight.level != 0;
1290 return 0;
1293 static int i965_setup_backlight(struct intel_connector *connector, enum pipe unused)
1295 struct drm_device *dev = connector->base.dev;
1296 struct drm_i915_private *dev_priv = dev->dev_private;
1297 struct intel_panel *panel = &connector->panel;
1298 u32 ctl, ctl2, val;
1300 ctl2 = I915_READ(BLC_PWM_CTL2);
1301 panel->backlight.combination_mode = ctl2 & BLM_COMBINATION_MODE;
1302 panel->backlight.active_low_pwm = ctl2 & BLM_POLARITY_I965;
1304 ctl = I915_READ(BLC_PWM_CTL);
1305 panel->backlight.max = ctl >> 16;
1306 if (panel->backlight.combination_mode)
1307 panel->backlight.max *= 0xff;
1309 if (!panel->backlight.max)
1310 return -ENODEV;
1312 panel->backlight.min = get_backlight_min_vbt(connector);
1314 val = i9xx_get_backlight(connector);
1315 panel->backlight.level = intel_panel_compute_brightness(connector, val);
1317 panel->backlight.enabled = (ctl2 & BLM_PWM_ENABLE) &&
1318 panel->backlight.level != 0;
1320 return 0;
1323 static int vlv_setup_backlight(struct intel_connector *connector, enum pipe pipe)
1325 struct drm_device *dev = connector->base.dev;
1326 struct drm_i915_private *dev_priv = dev->dev_private;
1327 struct intel_panel *panel = &connector->panel;
1328 enum pipe p;
1329 u32 ctl, ctl2, val;
1331 for_each_pipe(dev_priv, p) {
1332 u32 cur_val = I915_READ(VLV_BLC_PWM_CTL(p));
1334 /* Skip if the modulation freq is already set */
1335 if (cur_val & ~BACKLIGHT_DUTY_CYCLE_MASK)
1336 continue;
1338 cur_val &= BACKLIGHT_DUTY_CYCLE_MASK;
1339 I915_WRITE(VLV_BLC_PWM_CTL(p), (0xf42 << 16) |
1340 cur_val);
1343 if (WARN_ON(pipe != PIPE_A && pipe != PIPE_B))
1344 return -ENODEV;
1346 ctl2 = I915_READ(VLV_BLC_PWM_CTL2(pipe));
1347 panel->backlight.active_low_pwm = ctl2 & BLM_POLARITY_I965;
1349 ctl = I915_READ(VLV_BLC_PWM_CTL(pipe));
1350 panel->backlight.max = ctl >> 16;
1351 if (!panel->backlight.max)
1352 return -ENODEV;
1354 panel->backlight.min = get_backlight_min_vbt(connector);
1356 val = _vlv_get_backlight(dev, pipe);
1357 panel->backlight.level = intel_panel_compute_brightness(connector, val);
1359 panel->backlight.enabled = (ctl2 & BLM_PWM_ENABLE) &&
1360 panel->backlight.level != 0;
1362 return 0;
1365 static int
1366 bxt_setup_backlight(struct intel_connector *connector, enum pipe unused)
1368 struct drm_device *dev = connector->base.dev;
1369 struct drm_i915_private *dev_priv = dev->dev_private;
1370 struct intel_panel *panel = &connector->panel;
1371 u32 pwm_ctl, val;
1373 pwm_ctl = I915_READ(BXT_BLC_PWM_CTL1);
1374 panel->backlight.active_low_pwm = pwm_ctl & BXT_BLC_PWM_POLARITY;
1376 panel->backlight.max = I915_READ(BXT_BLC_PWM_FREQ1);
1377 if (!panel->backlight.max)
1378 return -ENODEV;
1380 val = bxt_get_backlight(connector);
1381 panel->backlight.level = intel_panel_compute_brightness(connector, val);
1383 panel->backlight.enabled = (pwm_ctl & BXT_BLC_PWM_ENABLE) &&
1384 panel->backlight.level != 0;
1386 return 0;
1389 int intel_panel_setup_backlight(struct drm_connector *connector, enum pipe pipe)
1391 struct drm_device *dev = connector->dev;
1392 struct drm_i915_private *dev_priv = dev->dev_private;
1393 struct intel_connector *intel_connector = to_intel_connector(connector);
1394 struct intel_panel *panel = &intel_connector->panel;
1395 int ret;
1397 if (!dev_priv->vbt.backlight.present) {
1398 if (dev_priv->quirks & QUIRK_BACKLIGHT_PRESENT) {
1399 DRM_DEBUG_KMS("no backlight present per VBT, but present per quirk\n");
1400 } else {
1401 DRM_DEBUG_KMS("no backlight present per VBT\n");
1402 return 0;
1406 /* set level and max in panel struct */
1407 mutex_lock(&dev_priv->backlight_lock);
1408 ret = dev_priv->display.setup_backlight(intel_connector, pipe);
1409 mutex_unlock(&dev_priv->backlight_lock);
1411 if (ret) {
1412 DRM_DEBUG_KMS("failed to setup backlight for connector %s\n",
1413 connector->name);
1414 return ret;
1417 panel->backlight.present = true;
1419 DRM_DEBUG_KMS("Connector %s backlight initialized, %s, brightness %u/%u\n",
1420 connector->name,
1421 panel->backlight.enabled ? "enabled" : "disabled",
1422 panel->backlight.level, panel->backlight.max);
1424 return 0;
1427 void intel_panel_destroy_backlight(struct drm_connector *connector)
1429 struct intel_connector *intel_connector = to_intel_connector(connector);
1430 struct intel_panel *panel = &intel_connector->panel;
1432 panel->backlight.present = false;
1435 /* Set up chip specific backlight functions */
1436 void intel_panel_init_backlight_funcs(struct drm_device *dev)
1438 struct drm_i915_private *dev_priv = dev->dev_private;
1440 if (IS_BROXTON(dev)) {
1441 dev_priv->display.setup_backlight = bxt_setup_backlight;
1442 dev_priv->display.enable_backlight = bxt_enable_backlight;
1443 dev_priv->display.disable_backlight = bxt_disable_backlight;
1444 dev_priv->display.set_backlight = bxt_set_backlight;
1445 dev_priv->display.get_backlight = bxt_get_backlight;
1446 } else if (IS_BROADWELL(dev) || IS_SKYLAKE(dev)) {
1447 dev_priv->display.setup_backlight = bdw_setup_backlight;
1448 dev_priv->display.enable_backlight = bdw_enable_backlight;
1449 dev_priv->display.disable_backlight = pch_disable_backlight;
1450 dev_priv->display.set_backlight = bdw_set_backlight;
1451 dev_priv->display.get_backlight = bdw_get_backlight;
1452 } else if (HAS_PCH_SPLIT(dev)) {
1453 dev_priv->display.setup_backlight = pch_setup_backlight;
1454 dev_priv->display.enable_backlight = pch_enable_backlight;
1455 dev_priv->display.disable_backlight = pch_disable_backlight;
1456 dev_priv->display.set_backlight = pch_set_backlight;
1457 dev_priv->display.get_backlight = pch_get_backlight;
1458 } else if (IS_VALLEYVIEW(dev)) {
1459 dev_priv->display.setup_backlight = vlv_setup_backlight;
1460 dev_priv->display.enable_backlight = vlv_enable_backlight;
1461 dev_priv->display.disable_backlight = vlv_disable_backlight;
1462 dev_priv->display.set_backlight = vlv_set_backlight;
1463 dev_priv->display.get_backlight = vlv_get_backlight;
1464 } else if (IS_GEN4(dev)) {
1465 dev_priv->display.setup_backlight = i965_setup_backlight;
1466 dev_priv->display.enable_backlight = i965_enable_backlight;
1467 dev_priv->display.disable_backlight = i965_disable_backlight;
1468 dev_priv->display.set_backlight = i9xx_set_backlight;
1469 dev_priv->display.get_backlight = i9xx_get_backlight;
1470 } else {
1471 dev_priv->display.setup_backlight = i9xx_setup_backlight;
1472 dev_priv->display.enable_backlight = i9xx_enable_backlight;
1473 dev_priv->display.disable_backlight = i9xx_disable_backlight;
1474 dev_priv->display.set_backlight = i9xx_set_backlight;
1475 dev_priv->display.get_backlight = i9xx_get_backlight;
1479 int intel_panel_init(struct intel_panel *panel,
1480 struct drm_display_mode *fixed_mode,
1481 struct drm_display_mode *downclock_mode)
1483 panel->fixed_mode = fixed_mode;
1484 panel->downclock_mode = downclock_mode;
1486 return 0;
1489 void intel_panel_fini(struct intel_panel *panel)
1491 struct intel_connector *intel_connector =
1492 container_of(panel, struct intel_connector, panel);
1494 if (panel->fixed_mode)
1495 drm_mode_destroy(intel_connector->base.dev, panel->fixed_mode);
1497 if (panel->downclock_mode)
1498 drm_mode_destroy(intel_connector->base.dev,
1499 panel->downclock_mode);
1502 void intel_backlight_register(struct drm_device *dev)
1504 struct intel_connector *connector;
1506 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head)
1507 intel_backlight_device_register(connector);
1510 void intel_backlight_unregister(struct drm_device *dev)
1512 struct intel_connector *connector;
1514 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head)
1515 intel_backlight_device_unregister(connector);