1e3adcf8fSFrançois Tigeot /*
2e3adcf8fSFrançois Tigeot * Copyright © 2006-2010 Intel Corporation
3e3adcf8fSFrançois Tigeot * Copyright (c) 2006 Dave Airlie <airlied@linux.ie>
4e3adcf8fSFrançois Tigeot *
5e3adcf8fSFrançois Tigeot * Permission is hereby granted, free of charge, to any person obtaining a
6e3adcf8fSFrançois Tigeot * copy of this software and associated documentation files (the "Software"),
7e3adcf8fSFrançois Tigeot * to deal in the Software without restriction, including without limitation
8e3adcf8fSFrançois Tigeot * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9e3adcf8fSFrançois Tigeot * and/or sell copies of the Software, and to permit persons to whom the
10e3adcf8fSFrançois Tigeot * Software is furnished to do so, subject to the following conditions:
11e3adcf8fSFrançois Tigeot *
12e3adcf8fSFrançois Tigeot * The above copyright notice and this permission notice (including the next
13e3adcf8fSFrançois Tigeot * paragraph) shall be included in all copies or substantial portions of the
14e3adcf8fSFrançois Tigeot * Software.
15e3adcf8fSFrançois Tigeot *
16e3adcf8fSFrançois Tigeot * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17e3adcf8fSFrançois Tigeot * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18e3adcf8fSFrançois Tigeot * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19e3adcf8fSFrançois Tigeot * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20e3adcf8fSFrançois Tigeot * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21e3adcf8fSFrançois Tigeot * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22e3adcf8fSFrançois Tigeot * DEALINGS IN THE SOFTWARE.
23e3adcf8fSFrançois Tigeot *
24e3adcf8fSFrançois Tigeot * Authors:
25e3adcf8fSFrançois Tigeot * Eric Anholt <eric@anholt.net>
26e3adcf8fSFrançois Tigeot * Dave Airlie <airlied@linux.ie>
27e3adcf8fSFrançois Tigeot * Jesse Barnes <jesse.barnes@intel.com>
28e3adcf8fSFrançois Tigeot * Chris Wilson <chris@chris-wilson.co.uk>
29e3adcf8fSFrançois Tigeot */
30e3adcf8fSFrançois Tigeot
31bf017597SFrançois Tigeot #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
32aee94f86SFrançois Tigeot
33477eb7f9SFrançois Tigeot #include <linux/kernel.h>
34a2fdbec6SFrançois Tigeot #include <linux/moduleparam.h>
35a05eeebfSFrançois Tigeot #include <linux/pwm.h>
36e3adcf8fSFrançois Tigeot #include "intel_drv.h"
37e3adcf8fSFrançois Tigeot
38a05eeebfSFrançois Tigeot #define CRC_PMIC_PWM_PERIOD_NS 21333
39a05eeebfSFrançois Tigeot
40e3adcf8fSFrançois Tigeot void
intel_fixed_panel_mode(const struct drm_display_mode * fixed_mode,struct drm_display_mode * adjusted_mode)419edbd4a0SFrançois Tigeot intel_fixed_panel_mode(const struct drm_display_mode *fixed_mode,
42e3adcf8fSFrançois Tigeot struct drm_display_mode *adjusted_mode)
43e3adcf8fSFrançois Tigeot {
449edbd4a0SFrançois Tigeot drm_mode_copy(adjusted_mode, fixed_mode);
45e3adcf8fSFrançois Tigeot
469edbd4a0SFrançois Tigeot drm_mode_set_crtcinfo(adjusted_mode, 0);
47e3adcf8fSFrançois Tigeot }
48e3adcf8fSFrançois Tigeot
49ba55f2f5SFrançois Tigeot /**
50ba55f2f5SFrançois Tigeot * intel_find_panel_downclock - find the reduced downclock for LVDS in EDID
51a85cb24fSFrançois Tigeot * @dev_priv: i915 device instance
52ba55f2f5SFrançois Tigeot * @fixed_mode : panel native mode
53ba55f2f5SFrançois Tigeot * @connector: LVDS/eDP connector
54ba55f2f5SFrançois Tigeot *
55ba55f2f5SFrançois Tigeot * Return downclock_avail
56ba55f2f5SFrançois Tigeot * Find the reduced downclock for LVDS/eDP in EDID.
57ba55f2f5SFrançois Tigeot */
58ba55f2f5SFrançois Tigeot struct drm_display_mode *
intel_find_panel_downclock(struct drm_i915_private * dev_priv,struct drm_display_mode * fixed_mode,struct drm_connector * connector)59a85cb24fSFrançois Tigeot intel_find_panel_downclock(struct drm_i915_private *dev_priv,
60ba55f2f5SFrançois Tigeot struct drm_display_mode *fixed_mode,
61ba55f2f5SFrançois Tigeot struct drm_connector *connector)
62ba55f2f5SFrançois Tigeot {
63ba55f2f5SFrançois Tigeot struct drm_display_mode *scan, *tmp_mode;
64ba55f2f5SFrançois Tigeot int temp_downclock;
65ba55f2f5SFrançois Tigeot
66ba55f2f5SFrançois Tigeot temp_downclock = fixed_mode->clock;
67ba55f2f5SFrançois Tigeot tmp_mode = NULL;
68ba55f2f5SFrançois Tigeot
69ba55f2f5SFrançois Tigeot list_for_each_entry(scan, &connector->probed_modes, head) {
70ba55f2f5SFrançois Tigeot /*
71ba55f2f5SFrançois Tigeot * If one mode has the same resolution with the fixed_panel
72ba55f2f5SFrançois Tigeot * mode while they have the different refresh rate, it means
73ba55f2f5SFrançois Tigeot * that the reduced downclock is found. In such
74ba55f2f5SFrançois Tigeot * case we can set the different FPx0/1 to dynamically select
75ba55f2f5SFrançois Tigeot * between low and high frequency.
76ba55f2f5SFrançois Tigeot */
77ba55f2f5SFrançois Tigeot if (scan->hdisplay == fixed_mode->hdisplay &&
78ba55f2f5SFrançois Tigeot scan->hsync_start == fixed_mode->hsync_start &&
79ba55f2f5SFrançois Tigeot scan->hsync_end == fixed_mode->hsync_end &&
80ba55f2f5SFrançois Tigeot scan->htotal == fixed_mode->htotal &&
81ba55f2f5SFrançois Tigeot scan->vdisplay == fixed_mode->vdisplay &&
82ba55f2f5SFrançois Tigeot scan->vsync_start == fixed_mode->vsync_start &&
83ba55f2f5SFrançois Tigeot scan->vsync_end == fixed_mode->vsync_end &&
84ba55f2f5SFrançois Tigeot scan->vtotal == fixed_mode->vtotal) {
85ba55f2f5SFrançois Tigeot if (scan->clock < temp_downclock) {
86ba55f2f5SFrançois Tigeot /*
87ba55f2f5SFrançois Tigeot * The downclock is already found. But we
88ba55f2f5SFrançois Tigeot * expect to find the lower downclock.
89ba55f2f5SFrançois Tigeot */
90ba55f2f5SFrançois Tigeot temp_downclock = scan->clock;
91ba55f2f5SFrançois Tigeot tmp_mode = scan;
92ba55f2f5SFrançois Tigeot }
93ba55f2f5SFrançois Tigeot }
94ba55f2f5SFrançois Tigeot }
95ba55f2f5SFrançois Tigeot
96ba55f2f5SFrançois Tigeot if (temp_downclock < fixed_mode->clock)
97a85cb24fSFrançois Tigeot return drm_mode_duplicate(&dev_priv->drm, tmp_mode);
98ba55f2f5SFrançois Tigeot else
99ba55f2f5SFrançois Tigeot return NULL;
100ba55f2f5SFrançois Tigeot }
101ba55f2f5SFrançois Tigeot
102e3adcf8fSFrançois Tigeot /* adjusted_mode has been preset to be the panel's fixed mode */
103e3adcf8fSFrançois Tigeot void
intel_pch_panel_fitting(struct intel_crtc * intel_crtc,struct intel_crtc_state * pipe_config,int fitting_mode)1045d0b1887SFrançois Tigeot intel_pch_panel_fitting(struct intel_crtc *intel_crtc,
1052c9916cdSFrançois Tigeot struct intel_crtc_state *pipe_config,
1065d0b1887SFrançois Tigeot int fitting_mode)
107e3adcf8fSFrançois Tigeot {
108352ff8bdSFrançois Tigeot const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
109352ff8bdSFrançois Tigeot int x = 0, y = 0, width = 0, height = 0;
110e3adcf8fSFrançois Tigeot
111e3adcf8fSFrançois Tigeot /* Native modes don't need fitting */
112352ff8bdSFrançois Tigeot if (adjusted_mode->crtc_hdisplay == pipe_config->pipe_src_w &&
113*3f2dd94aSFrançois Tigeot adjusted_mode->crtc_vdisplay == pipe_config->pipe_src_h &&
114*3f2dd94aSFrançois Tigeot !pipe_config->ycbcr420)
115e3adcf8fSFrançois Tigeot goto done;
116e3adcf8fSFrançois Tigeot
117e3adcf8fSFrançois Tigeot switch (fitting_mode) {
118e3adcf8fSFrançois Tigeot case DRM_MODE_SCALE_CENTER:
1199edbd4a0SFrançois Tigeot width = pipe_config->pipe_src_w;
1209edbd4a0SFrançois Tigeot height = pipe_config->pipe_src_h;
121352ff8bdSFrançois Tigeot x = (adjusted_mode->crtc_hdisplay - width + 1)/2;
122352ff8bdSFrançois Tigeot y = (adjusted_mode->crtc_vdisplay - height + 1)/2;
123e3adcf8fSFrançois Tigeot break;
124e3adcf8fSFrançois Tigeot
125e3adcf8fSFrançois Tigeot case DRM_MODE_SCALE_ASPECT:
126e3adcf8fSFrançois Tigeot /* Scale but preserve the aspect ratio */
127e3adcf8fSFrançois Tigeot {
128352ff8bdSFrançois Tigeot u32 scaled_width = adjusted_mode->crtc_hdisplay
1299edbd4a0SFrançois Tigeot * pipe_config->pipe_src_h;
1309edbd4a0SFrançois Tigeot u32 scaled_height = pipe_config->pipe_src_w
131352ff8bdSFrançois Tigeot * adjusted_mode->crtc_vdisplay;
132e3adcf8fSFrançois Tigeot if (scaled_width > scaled_height) { /* pillar */
1339edbd4a0SFrançois Tigeot width = scaled_height / pipe_config->pipe_src_h;
134e3adcf8fSFrançois Tigeot if (width & 1)
135e3adcf8fSFrançois Tigeot width++;
136352ff8bdSFrançois Tigeot x = (adjusted_mode->crtc_hdisplay - width + 1) / 2;
137e3adcf8fSFrançois Tigeot y = 0;
138352ff8bdSFrançois Tigeot height = adjusted_mode->crtc_vdisplay;
139e3adcf8fSFrançois Tigeot } else if (scaled_width < scaled_height) { /* letter */
1409edbd4a0SFrançois Tigeot height = scaled_width / pipe_config->pipe_src_w;
141e3adcf8fSFrançois Tigeot if (height & 1)
142e3adcf8fSFrançois Tigeot height++;
143352ff8bdSFrançois Tigeot y = (adjusted_mode->crtc_vdisplay - height + 1) / 2;
144e3adcf8fSFrançois Tigeot x = 0;
145352ff8bdSFrançois Tigeot width = adjusted_mode->crtc_hdisplay;
146e3adcf8fSFrançois Tigeot } else {
147e3adcf8fSFrançois Tigeot x = y = 0;
148352ff8bdSFrançois Tigeot width = adjusted_mode->crtc_hdisplay;
149352ff8bdSFrançois Tigeot height = adjusted_mode->crtc_vdisplay;
150e3adcf8fSFrançois Tigeot }
151e3adcf8fSFrançois Tigeot }
152e3adcf8fSFrançois Tigeot break;
153e3adcf8fSFrançois Tigeot
154e3adcf8fSFrançois Tigeot case DRM_MODE_SCALE_FULLSCREEN:
155e3adcf8fSFrançois Tigeot x = y = 0;
156352ff8bdSFrançois Tigeot width = adjusted_mode->crtc_hdisplay;
157352ff8bdSFrançois Tigeot height = adjusted_mode->crtc_vdisplay;
158e3adcf8fSFrançois Tigeot break;
1595d0b1887SFrançois Tigeot
1605d0b1887SFrançois Tigeot default:
1615d0b1887SFrançois Tigeot WARN(1, "bad panel fit mode: %d\n", fitting_mode);
1625d0b1887SFrançois Tigeot return;
163e3adcf8fSFrançois Tigeot }
164e3adcf8fSFrançois Tigeot
165e3adcf8fSFrançois Tigeot done:
1665d0b1887SFrançois Tigeot pipe_config->pch_pfit.pos = (x << 16) | y;
1675d0b1887SFrançois Tigeot pipe_config->pch_pfit.size = (width << 16) | height;
1689edbd4a0SFrançois Tigeot pipe_config->pch_pfit.enabled = pipe_config->pch_pfit.size != 0;
1695d0b1887SFrançois Tigeot }
1705d0b1887SFrançois Tigeot
1715d0b1887SFrançois Tigeot static void
centre_horizontally(struct drm_display_mode * adjusted_mode,int width)172352ff8bdSFrançois Tigeot centre_horizontally(struct drm_display_mode *adjusted_mode,
1735d0b1887SFrançois Tigeot int width)
1745d0b1887SFrançois Tigeot {
1755d0b1887SFrançois Tigeot u32 border, sync_pos, blank_width, sync_width;
1765d0b1887SFrançois Tigeot
1775d0b1887SFrançois Tigeot /* keep the hsync and hblank widths constant */
178352ff8bdSFrançois Tigeot sync_width = adjusted_mode->crtc_hsync_end - adjusted_mode->crtc_hsync_start;
179352ff8bdSFrançois Tigeot blank_width = adjusted_mode->crtc_hblank_end - adjusted_mode->crtc_hblank_start;
1805d0b1887SFrançois Tigeot sync_pos = (blank_width - sync_width + 1) / 2;
1815d0b1887SFrançois Tigeot
182352ff8bdSFrançois Tigeot border = (adjusted_mode->crtc_hdisplay - width + 1) / 2;
1835d0b1887SFrançois Tigeot border += border & 1; /* make the border even */
1845d0b1887SFrançois Tigeot
185352ff8bdSFrançois Tigeot adjusted_mode->crtc_hdisplay = width;
186352ff8bdSFrançois Tigeot adjusted_mode->crtc_hblank_start = width + border;
187352ff8bdSFrançois Tigeot adjusted_mode->crtc_hblank_end = adjusted_mode->crtc_hblank_start + blank_width;
1885d0b1887SFrançois Tigeot
189352ff8bdSFrançois Tigeot adjusted_mode->crtc_hsync_start = adjusted_mode->crtc_hblank_start + sync_pos;
190352ff8bdSFrançois Tigeot adjusted_mode->crtc_hsync_end = adjusted_mode->crtc_hsync_start + sync_width;
1915d0b1887SFrançois Tigeot }
1925d0b1887SFrançois Tigeot
1935d0b1887SFrançois Tigeot static void
centre_vertically(struct drm_display_mode * adjusted_mode,int height)194352ff8bdSFrançois Tigeot centre_vertically(struct drm_display_mode *adjusted_mode,
1955d0b1887SFrançois Tigeot int height)
1965d0b1887SFrançois Tigeot {
1975d0b1887SFrançois Tigeot u32 border, sync_pos, blank_width, sync_width;
1985d0b1887SFrançois Tigeot
1995d0b1887SFrançois Tigeot /* keep the vsync and vblank widths constant */
200352ff8bdSFrançois Tigeot sync_width = adjusted_mode->crtc_vsync_end - adjusted_mode->crtc_vsync_start;
201352ff8bdSFrançois Tigeot blank_width = adjusted_mode->crtc_vblank_end - adjusted_mode->crtc_vblank_start;
2025d0b1887SFrançois Tigeot sync_pos = (blank_width - sync_width + 1) / 2;
2035d0b1887SFrançois Tigeot
204352ff8bdSFrançois Tigeot border = (adjusted_mode->crtc_vdisplay - height + 1) / 2;
2055d0b1887SFrançois Tigeot
206352ff8bdSFrançois Tigeot adjusted_mode->crtc_vdisplay = height;
207352ff8bdSFrançois Tigeot adjusted_mode->crtc_vblank_start = height + border;
208352ff8bdSFrançois Tigeot adjusted_mode->crtc_vblank_end = adjusted_mode->crtc_vblank_start + blank_width;
2095d0b1887SFrançois Tigeot
210352ff8bdSFrançois Tigeot adjusted_mode->crtc_vsync_start = adjusted_mode->crtc_vblank_start + sync_pos;
211352ff8bdSFrançois Tigeot adjusted_mode->crtc_vsync_end = adjusted_mode->crtc_vsync_start + sync_width;
2125d0b1887SFrançois Tigeot }
2135d0b1887SFrançois Tigeot
panel_fitter_scaling(u32 source,u32 target)2145d0b1887SFrançois Tigeot static inline u32 panel_fitter_scaling(u32 source, u32 target)
2155d0b1887SFrançois Tigeot {
2165d0b1887SFrançois Tigeot /*
2175d0b1887SFrançois Tigeot * Floating point operation is not supported. So the FACTOR
2185d0b1887SFrançois Tigeot * is defined, which can avoid the floating point computation
2195d0b1887SFrançois Tigeot * when calculating the panel ratio.
2205d0b1887SFrançois Tigeot */
2215d0b1887SFrançois Tigeot #define ACCURACY 12
2225d0b1887SFrançois Tigeot #define FACTOR (1 << ACCURACY)
2235d0b1887SFrançois Tigeot u32 ratio = source * FACTOR / target;
2245d0b1887SFrançois Tigeot return (FACTOR * ratio + FACTOR/2) / FACTOR;
2255d0b1887SFrançois Tigeot }
2265d0b1887SFrançois Tigeot
i965_scale_aspect(struct intel_crtc_state * pipe_config,u32 * pfit_control)2272c9916cdSFrançois Tigeot static void i965_scale_aspect(struct intel_crtc_state *pipe_config,
2289edbd4a0SFrançois Tigeot u32 *pfit_control)
2295d0b1887SFrançois Tigeot {
230352ff8bdSFrançois Tigeot const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
231352ff8bdSFrançois Tigeot u32 scaled_width = adjusted_mode->crtc_hdisplay *
2329edbd4a0SFrançois Tigeot pipe_config->pipe_src_h;
2339edbd4a0SFrançois Tigeot u32 scaled_height = pipe_config->pipe_src_w *
234352ff8bdSFrançois Tigeot adjusted_mode->crtc_vdisplay;
2355d0b1887SFrançois Tigeot
2365d0b1887SFrançois Tigeot /* 965+ is easy, it does everything in hw */
2375d0b1887SFrançois Tigeot if (scaled_width > scaled_height)
2389edbd4a0SFrançois Tigeot *pfit_control |= PFIT_ENABLE |
2395d0b1887SFrançois Tigeot PFIT_SCALING_PILLAR;
2405d0b1887SFrançois Tigeot else if (scaled_width < scaled_height)
2419edbd4a0SFrançois Tigeot *pfit_control |= PFIT_ENABLE |
2425d0b1887SFrançois Tigeot PFIT_SCALING_LETTER;
243352ff8bdSFrançois Tigeot else if (adjusted_mode->crtc_hdisplay != pipe_config->pipe_src_w)
2449edbd4a0SFrançois Tigeot *pfit_control |= PFIT_ENABLE | PFIT_SCALING_AUTO;
2459edbd4a0SFrançois Tigeot }
2469edbd4a0SFrançois Tigeot
i9xx_scale_aspect(struct intel_crtc_state * pipe_config,u32 * pfit_control,u32 * pfit_pgm_ratios,u32 * border)2472c9916cdSFrançois Tigeot static void i9xx_scale_aspect(struct intel_crtc_state *pipe_config,
2489edbd4a0SFrançois Tigeot u32 *pfit_control, u32 *pfit_pgm_ratios,
2499edbd4a0SFrançois Tigeot u32 *border)
2509edbd4a0SFrançois Tigeot {
2512c9916cdSFrançois Tigeot struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
252352ff8bdSFrançois Tigeot u32 scaled_width = adjusted_mode->crtc_hdisplay *
2539edbd4a0SFrançois Tigeot pipe_config->pipe_src_h;
2549edbd4a0SFrançois Tigeot u32 scaled_height = pipe_config->pipe_src_w *
255352ff8bdSFrançois Tigeot adjusted_mode->crtc_vdisplay;
2569edbd4a0SFrançois Tigeot u32 bits;
2579edbd4a0SFrançois Tigeot
2585d0b1887SFrançois Tigeot /*
2595d0b1887SFrançois Tigeot * For earlier chips we have to calculate the scaling
2605d0b1887SFrançois Tigeot * ratio by hand and program it into the
2615d0b1887SFrançois Tigeot * PFIT_PGM_RATIO register
2625d0b1887SFrançois Tigeot */
2635d0b1887SFrançois Tigeot if (scaled_width > scaled_height) { /* pillar */
2645d0b1887SFrançois Tigeot centre_horizontally(adjusted_mode,
2655d0b1887SFrançois Tigeot scaled_height /
2669edbd4a0SFrançois Tigeot pipe_config->pipe_src_h);
2675d0b1887SFrançois Tigeot
2689edbd4a0SFrançois Tigeot *border = LVDS_BORDER_ENABLE;
269352ff8bdSFrançois Tigeot if (pipe_config->pipe_src_h != adjusted_mode->crtc_vdisplay) {
2709edbd4a0SFrançois Tigeot bits = panel_fitter_scaling(pipe_config->pipe_src_h,
271352ff8bdSFrançois Tigeot adjusted_mode->crtc_vdisplay);
2729edbd4a0SFrançois Tigeot
2739edbd4a0SFrançois Tigeot *pfit_pgm_ratios |= (bits << PFIT_HORIZ_SCALE_SHIFT |
2745d0b1887SFrançois Tigeot bits << PFIT_VERT_SCALE_SHIFT);
2759edbd4a0SFrançois Tigeot *pfit_control |= (PFIT_ENABLE |
2765d0b1887SFrançois Tigeot VERT_INTERP_BILINEAR |
2775d0b1887SFrançois Tigeot HORIZ_INTERP_BILINEAR);
2785d0b1887SFrançois Tigeot }
2795d0b1887SFrançois Tigeot } else if (scaled_width < scaled_height) { /* letter */
2805d0b1887SFrançois Tigeot centre_vertically(adjusted_mode,
2815d0b1887SFrançois Tigeot scaled_width /
2829edbd4a0SFrançois Tigeot pipe_config->pipe_src_w);
2835d0b1887SFrançois Tigeot
2849edbd4a0SFrançois Tigeot *border = LVDS_BORDER_ENABLE;
285352ff8bdSFrançois Tigeot if (pipe_config->pipe_src_w != adjusted_mode->crtc_hdisplay) {
2869edbd4a0SFrançois Tigeot bits = panel_fitter_scaling(pipe_config->pipe_src_w,
287352ff8bdSFrançois Tigeot adjusted_mode->crtc_hdisplay);
2889edbd4a0SFrançois Tigeot
2899edbd4a0SFrançois Tigeot *pfit_pgm_ratios |= (bits << PFIT_HORIZ_SCALE_SHIFT |
2905d0b1887SFrançois Tigeot bits << PFIT_VERT_SCALE_SHIFT);
2919edbd4a0SFrançois Tigeot *pfit_control |= (PFIT_ENABLE |
2925d0b1887SFrançois Tigeot VERT_INTERP_BILINEAR |
2935d0b1887SFrançois Tigeot HORIZ_INTERP_BILINEAR);
2945d0b1887SFrançois Tigeot }
2955d0b1887SFrançois Tigeot } else {
2965d0b1887SFrançois Tigeot /* Aspects match, Let hw scale both directions */
2979edbd4a0SFrançois Tigeot *pfit_control |= (PFIT_ENABLE |
2985d0b1887SFrançois Tigeot VERT_AUTO_SCALE | HORIZ_AUTO_SCALE |
2995d0b1887SFrançois Tigeot VERT_INTERP_BILINEAR |
3005d0b1887SFrançois Tigeot HORIZ_INTERP_BILINEAR);
3015d0b1887SFrançois Tigeot }
3025d0b1887SFrançois Tigeot }
3039edbd4a0SFrançois Tigeot
intel_gmch_panel_fitting(struct intel_crtc * intel_crtc,struct intel_crtc_state * pipe_config,int fitting_mode)3049edbd4a0SFrançois Tigeot void intel_gmch_panel_fitting(struct intel_crtc *intel_crtc,
3052c9916cdSFrançois Tigeot struct intel_crtc_state *pipe_config,
3069edbd4a0SFrançois Tigeot int fitting_mode)
3079edbd4a0SFrançois Tigeot {
3084be47400SFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3099edbd4a0SFrançois Tigeot u32 pfit_control = 0, pfit_pgm_ratios = 0, border = 0;
310352ff8bdSFrançois Tigeot struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
3119edbd4a0SFrançois Tigeot
3129edbd4a0SFrançois Tigeot /* Native modes don't need fitting */
313352ff8bdSFrançois Tigeot if (adjusted_mode->crtc_hdisplay == pipe_config->pipe_src_w &&
314352ff8bdSFrançois Tigeot adjusted_mode->crtc_vdisplay == pipe_config->pipe_src_h)
3159edbd4a0SFrançois Tigeot goto out;
3169edbd4a0SFrançois Tigeot
3179edbd4a0SFrançois Tigeot switch (fitting_mode) {
3189edbd4a0SFrançois Tigeot case DRM_MODE_SCALE_CENTER:
3199edbd4a0SFrançois Tigeot /*
3209edbd4a0SFrançois Tigeot * For centered modes, we have to calculate border widths &
3219edbd4a0SFrançois Tigeot * heights and modify the values programmed into the CRTC.
3229edbd4a0SFrançois Tigeot */
3239edbd4a0SFrançois Tigeot centre_horizontally(adjusted_mode, pipe_config->pipe_src_w);
3249edbd4a0SFrançois Tigeot centre_vertically(adjusted_mode, pipe_config->pipe_src_h);
3259edbd4a0SFrançois Tigeot border = LVDS_BORDER_ENABLE;
3269edbd4a0SFrançois Tigeot break;
3279edbd4a0SFrançois Tigeot case DRM_MODE_SCALE_ASPECT:
3289edbd4a0SFrançois Tigeot /* Scale but preserve the aspect ratio */
3294be47400SFrançois Tigeot if (INTEL_GEN(dev_priv) >= 4)
3309edbd4a0SFrançois Tigeot i965_scale_aspect(pipe_config, &pfit_control);
3319edbd4a0SFrançois Tigeot else
3329edbd4a0SFrançois Tigeot i9xx_scale_aspect(pipe_config, &pfit_control,
3339edbd4a0SFrançois Tigeot &pfit_pgm_ratios, &border);
3345d0b1887SFrançois Tigeot break;
3355d0b1887SFrançois Tigeot case DRM_MODE_SCALE_FULLSCREEN:
3365d0b1887SFrançois Tigeot /*
3375d0b1887SFrançois Tigeot * Full scaling, even if it changes the aspect ratio.
3385d0b1887SFrançois Tigeot * Fortunately this is all done for us in hw.
3395d0b1887SFrançois Tigeot */
340352ff8bdSFrançois Tigeot if (pipe_config->pipe_src_h != adjusted_mode->crtc_vdisplay ||
341352ff8bdSFrançois Tigeot pipe_config->pipe_src_w != adjusted_mode->crtc_hdisplay) {
3425d0b1887SFrançois Tigeot pfit_control |= PFIT_ENABLE;
3434be47400SFrançois Tigeot if (INTEL_GEN(dev_priv) >= 4)
3445d0b1887SFrançois Tigeot pfit_control |= PFIT_SCALING_AUTO;
3455d0b1887SFrançois Tigeot else
3465d0b1887SFrançois Tigeot pfit_control |= (VERT_AUTO_SCALE |
3475d0b1887SFrançois Tigeot VERT_INTERP_BILINEAR |
3485d0b1887SFrançois Tigeot HORIZ_AUTO_SCALE |
3495d0b1887SFrançois Tigeot HORIZ_INTERP_BILINEAR);
3505d0b1887SFrançois Tigeot }
3515d0b1887SFrançois Tigeot break;
3525d0b1887SFrançois Tigeot default:
3535d0b1887SFrançois Tigeot WARN(1, "bad panel fit mode: %d\n", fitting_mode);
3545d0b1887SFrançois Tigeot return;
3555d0b1887SFrançois Tigeot }
3565d0b1887SFrançois Tigeot
3575d0b1887SFrançois Tigeot /* 965+ wants fuzzy fitting */
3585d0b1887SFrançois Tigeot /* FIXME: handle multiple panels by failing gracefully */
3594be47400SFrançois Tigeot if (INTEL_GEN(dev_priv) >= 4)
3605d0b1887SFrançois Tigeot pfit_control |= ((intel_crtc->pipe << PFIT_PIPE_SHIFT) |
3615d0b1887SFrançois Tigeot PFIT_FILTER_FUZZY);
3625d0b1887SFrançois Tigeot
3635d0b1887SFrançois Tigeot out:
3645d0b1887SFrançois Tigeot if ((pfit_control & PFIT_ENABLE) == 0) {
3655d0b1887SFrançois Tigeot pfit_control = 0;
3665d0b1887SFrançois Tigeot pfit_pgm_ratios = 0;
3675d0b1887SFrançois Tigeot }
3685d0b1887SFrançois Tigeot
3695d0b1887SFrançois Tigeot /* Make sure pre-965 set dither correctly for 18bpp panels. */
3704be47400SFrançois Tigeot if (INTEL_GEN(dev_priv) < 4 && pipe_config->pipe_bpp == 18)
3715d0b1887SFrançois Tigeot pfit_control |= PANEL_8TO6_DITHER_ENABLE;
3725d0b1887SFrançois Tigeot
3735d0b1887SFrançois Tigeot pipe_config->gmch_pfit.control = pfit_control;
3745d0b1887SFrançois Tigeot pipe_config->gmch_pfit.pgm_ratios = pfit_pgm_ratios;
3755d0b1887SFrançois Tigeot pipe_config->gmch_pfit.lvds_border_bits = border;
376e3adcf8fSFrançois Tigeot }
377e3adcf8fSFrançois Tigeot
378ba55f2f5SFrançois Tigeot enum drm_connector_status
intel_panel_detect(struct drm_i915_private * dev_priv)379a85cb24fSFrançois Tigeot intel_panel_detect(struct drm_i915_private *dev_priv)
380ba55f2f5SFrançois Tigeot {
381ba55f2f5SFrançois Tigeot /* Assume that the BIOS does not lie through the OpRegion... */
382*3f2dd94aSFrançois Tigeot if (!i915_modparams.panel_ignore_lid && dev_priv->opregion.lid_state) {
383352ff8bdSFrançois Tigeot return *dev_priv->opregion.lid_state & 0x1 ?
384ba55f2f5SFrançois Tigeot connector_status_connected :
385ba55f2f5SFrançois Tigeot connector_status_disconnected;
386ba55f2f5SFrançois Tigeot }
387ba55f2f5SFrançois Tigeot
388*3f2dd94aSFrançois Tigeot switch (i915_modparams.panel_ignore_lid) {
389ba55f2f5SFrançois Tigeot case -2:
390ba55f2f5SFrançois Tigeot return connector_status_connected;
391ba55f2f5SFrançois Tigeot case -1:
392ba55f2f5SFrançois Tigeot return connector_status_disconnected;
393ba55f2f5SFrançois Tigeot default:
394ba55f2f5SFrançois Tigeot return connector_status_unknown;
395ba55f2f5SFrançois Tigeot }
396ba55f2f5SFrançois Tigeot }
397ba55f2f5SFrançois Tigeot
39824edb884SFrançois Tigeot /**
39924edb884SFrançois Tigeot * scale - scale values from one range to another
40024edb884SFrançois Tigeot *
40124edb884SFrançois Tigeot * @source_val: value in range [@source_min..@source_max]
40224edb884SFrançois Tigeot *
40324edb884SFrançois Tigeot * Return @source_val in range [@source_min..@source_max] scaled to range
40424edb884SFrançois Tigeot * [@target_min..@target_max].
40524edb884SFrançois Tigeot */
scale(uint32_t source_val,uint32_t source_min,uint32_t source_max,uint32_t target_min,uint32_t target_max)40624edb884SFrançois Tigeot static uint32_t scale(uint32_t source_val,
40724edb884SFrançois Tigeot uint32_t source_min, uint32_t source_max,
40824edb884SFrançois Tigeot uint32_t target_min, uint32_t target_max)
40924edb884SFrançois Tigeot {
41024edb884SFrançois Tigeot uint64_t target_val;
41124edb884SFrançois Tigeot
41224edb884SFrançois Tigeot WARN_ON(source_min > source_max);
41324edb884SFrançois Tigeot WARN_ON(target_min > target_max);
41424edb884SFrançois Tigeot
41524edb884SFrançois Tigeot /* defensive */
41624edb884SFrançois Tigeot source_val = clamp(source_val, source_min, source_max);
41724edb884SFrançois Tigeot
41824edb884SFrançois Tigeot /* avoid overflows */
4191b13d190SFrançois Tigeot target_val = DIV_ROUND_CLOSEST_ULL((uint64_t)(source_val - source_min) *
4201b13d190SFrançois Tigeot (target_max - target_min), source_max - source_min);
42124edb884SFrançois Tigeot target_val += target_min;
42224edb884SFrançois Tigeot
42324edb884SFrançois Tigeot return target_val;
42424edb884SFrançois Tigeot }
42524edb884SFrançois Tigeot
42624edb884SFrançois Tigeot /* Scale user_level in range [0..user_max] to [hw_min..hw_max]. */
scale_user_to_hw(struct intel_connector * connector,u32 user_level,u32 user_max)42724edb884SFrançois Tigeot static inline u32 scale_user_to_hw(struct intel_connector *connector,
42824edb884SFrançois Tigeot u32 user_level, u32 user_max)
42924edb884SFrançois Tigeot {
43024edb884SFrançois Tigeot struct intel_panel *panel = &connector->panel;
43124edb884SFrançois Tigeot
43224edb884SFrançois Tigeot return scale(user_level, 0, user_max,
43324edb884SFrançois Tigeot panel->backlight.min, panel->backlight.max);
43424edb884SFrançois Tigeot }
43524edb884SFrançois Tigeot
43624edb884SFrançois Tigeot /* Scale user_level in range [0..user_max] to [0..hw_max], clamping the result
43724edb884SFrançois Tigeot * to [hw_min..hw_max]. */
clamp_user_to_hw(struct intel_connector * connector,u32 user_level,u32 user_max)43824edb884SFrançois Tigeot static inline u32 clamp_user_to_hw(struct intel_connector *connector,
43924edb884SFrançois Tigeot u32 user_level, u32 user_max)
44024edb884SFrançois Tigeot {
44124edb884SFrançois Tigeot struct intel_panel *panel = &connector->panel;
44224edb884SFrançois Tigeot u32 hw_level;
44324edb884SFrançois Tigeot
44424edb884SFrançois Tigeot hw_level = scale(user_level, 0, user_max, 0, panel->backlight.max);
44524edb884SFrançois Tigeot hw_level = clamp(hw_level, panel->backlight.min, panel->backlight.max);
44624edb884SFrançois Tigeot
44724edb884SFrançois Tigeot return hw_level;
44824edb884SFrançois Tigeot }
44924edb884SFrançois Tigeot
45024edb884SFrançois Tigeot /* Scale hw_level in range [hw_min..hw_max] to [0..user_max]. */
scale_hw_to_user(struct intel_connector * connector,u32 hw_level,u32 user_max)45124edb884SFrançois Tigeot static inline u32 scale_hw_to_user(struct intel_connector *connector,
45224edb884SFrançois Tigeot u32 hw_level, u32 user_max)
45324edb884SFrançois Tigeot {
45424edb884SFrançois Tigeot struct intel_panel *panel = &connector->panel;
45524edb884SFrançois Tigeot
45624edb884SFrançois Tigeot return scale(hw_level, panel->backlight.min, panel->backlight.max,
45724edb884SFrançois Tigeot 0, user_max);
45824edb884SFrançois Tigeot }
45924edb884SFrançois Tigeot
intel_panel_compute_brightness(struct intel_connector * connector,u32 val)4609edbd4a0SFrançois Tigeot static u32 intel_panel_compute_brightness(struct intel_connector *connector,
4619edbd4a0SFrançois Tigeot u32 val)
462e3440f96SFrançois Tigeot {
463aee94f86SFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
4649edbd4a0SFrançois Tigeot struct intel_panel *panel = &connector->panel;
4659edbd4a0SFrançois Tigeot
4669edbd4a0SFrançois Tigeot WARN_ON(panel->backlight.max == 0);
467e3440f96SFrançois Tigeot
468*3f2dd94aSFrançois Tigeot if (i915_modparams.invert_brightness < 0)
469e3440f96SFrançois Tigeot return val;
470e3440f96SFrançois Tigeot
471*3f2dd94aSFrançois Tigeot if (i915_modparams.invert_brightness > 0 ||
4725d0b1887SFrançois Tigeot dev_priv->quirks & QUIRK_INVERT_BRIGHTNESS) {
473*3f2dd94aSFrançois Tigeot return panel->backlight.max - val + panel->backlight.min;
4745d0b1887SFrançois Tigeot }
475e3440f96SFrançois Tigeot
476e3440f96SFrançois Tigeot return val;
477e3440f96SFrançois Tigeot }
478e3440f96SFrançois Tigeot
lpt_get_backlight(struct intel_connector * connector)479352ff8bdSFrançois Tigeot static u32 lpt_get_backlight(struct intel_connector *connector)
480e3adcf8fSFrançois Tigeot {
481aee94f86SFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
4829edbd4a0SFrançois Tigeot
4839edbd4a0SFrançois Tigeot return I915_READ(BLC_PWM_PCH_CTL2) & BACKLIGHT_DUTY_CYCLE_MASK;
4849edbd4a0SFrançois Tigeot }
4859edbd4a0SFrançois Tigeot
pch_get_backlight(struct intel_connector * connector)4869edbd4a0SFrançois Tigeot static u32 pch_get_backlight(struct intel_connector *connector)
4879edbd4a0SFrançois Tigeot {
488aee94f86SFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
4899edbd4a0SFrançois Tigeot
4909edbd4a0SFrançois Tigeot return I915_READ(BLC_PWM_CPU_CTL) & BACKLIGHT_DUTY_CYCLE_MASK;
4919edbd4a0SFrançois Tigeot }
4929edbd4a0SFrançois Tigeot
i9xx_get_backlight(struct intel_connector * connector)4939edbd4a0SFrançois Tigeot static u32 i9xx_get_backlight(struct intel_connector *connector)
4949edbd4a0SFrançois Tigeot {
495aee94f86SFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
4969edbd4a0SFrançois Tigeot struct intel_panel *panel = &connector->panel;
497e3adcf8fSFrançois Tigeot u32 val;
498e3adcf8fSFrançois Tigeot
499e3adcf8fSFrançois Tigeot val = I915_READ(BLC_PWM_CTL) & BACKLIGHT_DUTY_CYCLE_MASK;
500aee94f86SFrançois Tigeot if (INTEL_INFO(dev_priv)->gen < 4)
501e3adcf8fSFrançois Tigeot val >>= 1;
502e3adcf8fSFrançois Tigeot
5039edbd4a0SFrançois Tigeot if (panel->backlight.combination_mode) {
504e3adcf8fSFrançois Tigeot u8 lbpc;
505e3adcf8fSFrançois Tigeot
506303bf270SFrançois Tigeot pci_read_config_byte(dev_priv->drm.pdev, LBPC, &lbpc);
507e3adcf8fSFrançois Tigeot val *= lbpc;
508e3adcf8fSFrançois Tigeot }
5099edbd4a0SFrançois Tigeot
5109edbd4a0SFrançois Tigeot return val;
511e3adcf8fSFrançois Tigeot }
512e3adcf8fSFrançois Tigeot
_vlv_get_backlight(struct drm_i915_private * dev_priv,enum i915_pipe pipe)513aee94f86SFrançois Tigeot static u32 _vlv_get_backlight(struct drm_i915_private *dev_priv, enum i915_pipe pipe)
5149edbd4a0SFrançois Tigeot {
5152c9916cdSFrançois Tigeot if (WARN_ON(pipe != PIPE_A && pipe != PIPE_B))
5162c9916cdSFrançois Tigeot return 0;
5172c9916cdSFrançois Tigeot
5189edbd4a0SFrançois Tigeot return I915_READ(VLV_BLC_PWM_CTL(pipe)) & BACKLIGHT_DUTY_CYCLE_MASK;
5199edbd4a0SFrançois Tigeot }
5209edbd4a0SFrançois Tigeot
vlv_get_backlight(struct intel_connector * connector)5219edbd4a0SFrançois Tigeot static u32 vlv_get_backlight(struct intel_connector *connector)
5229edbd4a0SFrançois Tigeot {
523aee94f86SFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
5249edbd4a0SFrançois Tigeot enum i915_pipe pipe = intel_get_pipe_from_connector(connector);
5259edbd4a0SFrançois Tigeot
526aee94f86SFrançois Tigeot return _vlv_get_backlight(dev_priv, pipe);
5279edbd4a0SFrançois Tigeot }
5289edbd4a0SFrançois Tigeot
bxt_get_backlight(struct intel_connector * connector)52919c468b4SFrançois Tigeot static u32 bxt_get_backlight(struct intel_connector *connector)
53019c468b4SFrançois Tigeot {
531aee94f86SFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
532352ff8bdSFrançois Tigeot struct intel_panel *panel = &connector->panel;
53319c468b4SFrançois Tigeot
534352ff8bdSFrançois Tigeot return I915_READ(BXT_BLC_PWM_DUTY(panel->backlight.controller));
53519c468b4SFrançois Tigeot }
53619c468b4SFrançois Tigeot
pwm_get_backlight(struct intel_connector * connector)537a05eeebfSFrançois Tigeot static u32 pwm_get_backlight(struct intel_connector *connector)
538a05eeebfSFrançois Tigeot {
539a05eeebfSFrançois Tigeot struct intel_panel *panel = &connector->panel;
540a05eeebfSFrançois Tigeot int duty_ns;
541a05eeebfSFrançois Tigeot
542a05eeebfSFrançois Tigeot duty_ns = pwm_get_duty_cycle(panel->backlight.pwm);
543a05eeebfSFrançois Tigeot return DIV_ROUND_UP(duty_ns * 100, CRC_PMIC_PWM_PERIOD_NS);
544a05eeebfSFrançois Tigeot }
545a05eeebfSFrançois Tigeot
5469edbd4a0SFrançois Tigeot #if 0
5479edbd4a0SFrançois Tigeot static u32 intel_panel_get_backlight(struct intel_connector *connector)
5489edbd4a0SFrançois Tigeot {
549aee94f86SFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
5502c9916cdSFrançois Tigeot struct intel_panel *panel = &connector->panel;
5512c9916cdSFrançois Tigeot u32 val = 0;
5529edbd4a0SFrançois Tigeot
5532c9916cdSFrançois Tigeot mutex_lock(&dev_priv->backlight_lock);
5549edbd4a0SFrançois Tigeot
5552c9916cdSFrançois Tigeot if (panel->backlight.enabled) {
556352ff8bdSFrançois Tigeot val = panel->backlight.get(connector);
5579edbd4a0SFrançois Tigeot val = intel_panel_compute_brightness(connector, val);
5582c9916cdSFrançois Tigeot }
5599edbd4a0SFrançois Tigeot
5602c9916cdSFrançois Tigeot mutex_unlock(&dev_priv->backlight_lock);
5615d0b1887SFrançois Tigeot
562e3440f96SFrançois Tigeot DRM_DEBUG_DRIVER("get backlight PWM = %d\n", val);
563e3adcf8fSFrançois Tigeot return val;
564e3adcf8fSFrançois Tigeot }
5659edbd4a0SFrançois Tigeot #endif
566e3adcf8fSFrançois Tigeot
lpt_set_backlight(const struct drm_connector_state * conn_state,u32 level)567*3f2dd94aSFrançois Tigeot static void lpt_set_backlight(const struct drm_connector_state *conn_state, u32 level)
568e3adcf8fSFrançois Tigeot {
569*3f2dd94aSFrançois Tigeot struct intel_connector *connector = to_intel_connector(conn_state->connector);
570aee94f86SFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
571*3f2dd94aSFrançois Tigeot
5729edbd4a0SFrançois Tigeot u32 val = I915_READ(BLC_PWM_PCH_CTL2) & ~BACKLIGHT_DUTY_CYCLE_MASK;
5739edbd4a0SFrançois Tigeot I915_WRITE(BLC_PWM_PCH_CTL2, val | level);
574e3adcf8fSFrançois Tigeot }
575e3adcf8fSFrançois Tigeot
pch_set_backlight(const struct drm_connector_state * conn_state,u32 level)576*3f2dd94aSFrançois Tigeot static void pch_set_backlight(const struct drm_connector_state *conn_state, u32 level)
577e3adcf8fSFrançois Tigeot {
578*3f2dd94aSFrançois Tigeot struct intel_connector *connector = to_intel_connector(conn_state->connector);
579aee94f86SFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
580e3adcf8fSFrançois Tigeot u32 tmp;
581e3adcf8fSFrançois Tigeot
5829edbd4a0SFrançois Tigeot tmp = I915_READ(BLC_PWM_CPU_CTL) & ~BACKLIGHT_DUTY_CYCLE_MASK;
5839edbd4a0SFrançois Tigeot I915_WRITE(BLC_PWM_CPU_CTL, tmp | level);
5849edbd4a0SFrançois Tigeot }
585e3adcf8fSFrançois Tigeot
i9xx_set_backlight(const struct drm_connector_state * conn_state,u32 level)586*3f2dd94aSFrançois Tigeot static void i9xx_set_backlight(const struct drm_connector_state *conn_state, u32 level)
5879edbd4a0SFrançois Tigeot {
588*3f2dd94aSFrançois Tigeot struct intel_connector *connector = to_intel_connector(conn_state->connector);
589aee94f86SFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
5909edbd4a0SFrançois Tigeot struct intel_panel *panel = &connector->panel;
5919edbd4a0SFrançois Tigeot u32 tmp, mask;
592e3adcf8fSFrançois Tigeot
5939edbd4a0SFrançois Tigeot WARN_ON(panel->backlight.max == 0);
5949edbd4a0SFrançois Tigeot
5959edbd4a0SFrançois Tigeot if (panel->backlight.combination_mode) {
596e3adcf8fSFrançois Tigeot u8 lbpc;
597e3adcf8fSFrançois Tigeot
5989edbd4a0SFrançois Tigeot lbpc = level * 0xfe / panel->backlight.max + 1;
599e3adcf8fSFrançois Tigeot level /= lbpc;
600303bf270SFrançois Tigeot pci_write_config_byte(dev_priv->drm.pdev, LBPC, lbpc);
601e3adcf8fSFrançois Tigeot }
602e3adcf8fSFrançois Tigeot
603aee94f86SFrançois Tigeot if (IS_GEN4(dev_priv)) {
6049edbd4a0SFrançois Tigeot mask = BACKLIGHT_DUTY_CYCLE_MASK;
6059edbd4a0SFrançois Tigeot } else {
606e3adcf8fSFrançois Tigeot level <<= 1;
6079edbd4a0SFrançois Tigeot mask = BACKLIGHT_DUTY_CYCLE_MASK_PNV;
6089edbd4a0SFrançois Tigeot }
6099edbd4a0SFrançois Tigeot
6109edbd4a0SFrançois Tigeot tmp = I915_READ(BLC_PWM_CTL) & ~mask;
611e3adcf8fSFrançois Tigeot I915_WRITE(BLC_PWM_CTL, tmp | level);
612e3adcf8fSFrançois Tigeot }
613e3adcf8fSFrançois Tigeot
vlv_set_backlight(const struct drm_connector_state * conn_state,u32 level)614*3f2dd94aSFrançois Tigeot static void vlv_set_backlight(const struct drm_connector_state *conn_state, u32 level)
615e3adcf8fSFrançois Tigeot {
616*3f2dd94aSFrançois Tigeot struct intel_connector *connector = to_intel_connector(conn_state->connector);
617aee94f86SFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
618*3f2dd94aSFrançois Tigeot enum i915_pipe pipe = to_intel_crtc(conn_state->crtc)->pipe;
6199edbd4a0SFrançois Tigeot u32 tmp;
6205d0b1887SFrançois Tigeot
6219edbd4a0SFrançois Tigeot tmp = I915_READ(VLV_BLC_PWM_CTL(pipe)) & ~BACKLIGHT_DUTY_CYCLE_MASK;
6229edbd4a0SFrançois Tigeot I915_WRITE(VLV_BLC_PWM_CTL(pipe), tmp | level);
6235d0b1887SFrançois Tigeot }
6245d0b1887SFrançois Tigeot
bxt_set_backlight(const struct drm_connector_state * conn_state,u32 level)625*3f2dd94aSFrançois Tigeot static void bxt_set_backlight(const struct drm_connector_state *conn_state, u32 level)
62619c468b4SFrançois Tigeot {
627*3f2dd94aSFrançois Tigeot struct intel_connector *connector = to_intel_connector(conn_state->connector);
628aee94f86SFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
629352ff8bdSFrançois Tigeot struct intel_panel *panel = &connector->panel;
63019c468b4SFrançois Tigeot
631352ff8bdSFrançois Tigeot I915_WRITE(BXT_BLC_PWM_DUTY(panel->backlight.controller), level);
63219c468b4SFrançois Tigeot }
63319c468b4SFrançois Tigeot
pwm_set_backlight(const struct drm_connector_state * conn_state,u32 level)634*3f2dd94aSFrançois Tigeot static void pwm_set_backlight(const struct drm_connector_state *conn_state, u32 level)
635a05eeebfSFrançois Tigeot {
636*3f2dd94aSFrançois Tigeot struct intel_panel *panel = &to_intel_connector(conn_state->connector)->panel;
637a05eeebfSFrançois Tigeot int duty_ns = DIV_ROUND_UP(level * CRC_PMIC_PWM_PERIOD_NS, 100);
638a05eeebfSFrançois Tigeot
639a05eeebfSFrançois Tigeot pwm_config(panel->backlight.pwm, duty_ns, CRC_PMIC_PWM_PERIOD_NS);
640a05eeebfSFrançois Tigeot }
641a05eeebfSFrançois Tigeot
6429edbd4a0SFrançois Tigeot static void
intel_panel_actually_set_backlight(const struct drm_connector_state * conn_state,u32 level)643*3f2dd94aSFrançois Tigeot intel_panel_actually_set_backlight(const struct drm_connector_state *conn_state, u32 level)
6449edbd4a0SFrançois Tigeot {
645*3f2dd94aSFrançois Tigeot struct intel_connector *connector = to_intel_connector(conn_state->connector);
646352ff8bdSFrançois Tigeot struct intel_panel *panel = &connector->panel;
6479edbd4a0SFrançois Tigeot
6489edbd4a0SFrançois Tigeot DRM_DEBUG_DRIVER("set backlight PWM = %d\n", level);
6499edbd4a0SFrançois Tigeot
6509edbd4a0SFrançois Tigeot level = intel_panel_compute_brightness(connector, level);
651*3f2dd94aSFrançois Tigeot panel->backlight.set(conn_state, level);
6529edbd4a0SFrançois Tigeot }
6539edbd4a0SFrançois Tigeot
65424edb884SFrançois Tigeot /* set backlight brightness to level in range [0..max], scaling wrt hw min */
intel_panel_set_backlight(const struct drm_connector_state * conn_state,u32 user_level,u32 user_max)655*3f2dd94aSFrançois Tigeot static void intel_panel_set_backlight(const struct drm_connector_state *conn_state,
65624edb884SFrançois Tigeot u32 user_level, u32 user_max)
6579edbd4a0SFrançois Tigeot {
658*3f2dd94aSFrançois Tigeot struct intel_connector *connector = to_intel_connector(conn_state->connector);
659aee94f86SFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
6609edbd4a0SFrançois Tigeot struct intel_panel *panel = &connector->panel;
66124edb884SFrançois Tigeot u32 hw_level;
6629edbd4a0SFrançois Tigeot
6632c9916cdSFrançois Tigeot if (!panel->backlight.present)
6649edbd4a0SFrançois Tigeot return;
6659edbd4a0SFrançois Tigeot
6662c9916cdSFrançois Tigeot mutex_lock(&dev_priv->backlight_lock);
6679edbd4a0SFrançois Tigeot
6689edbd4a0SFrançois Tigeot WARN_ON(panel->backlight.max == 0);
6699edbd4a0SFrançois Tigeot
67024edb884SFrançois Tigeot hw_level = scale_user_to_hw(connector, user_level, user_max);
67124edb884SFrançois Tigeot panel->backlight.level = hw_level;
6728e26cdf6SFrançois Tigeot
6739edbd4a0SFrançois Tigeot if (panel->backlight.enabled)
674*3f2dd94aSFrançois Tigeot intel_panel_actually_set_backlight(conn_state, hw_level);
67524edb884SFrançois Tigeot
6762c9916cdSFrançois Tigeot mutex_unlock(&dev_priv->backlight_lock);
67724edb884SFrançois Tigeot }
67824edb884SFrançois Tigeot
67924edb884SFrançois Tigeot /* set backlight brightness to level in range [0..max], assuming hw min is
68024edb884SFrançois Tigeot * respected.
68124edb884SFrançois Tigeot */
intel_panel_set_backlight_acpi(const struct drm_connector_state * conn_state,u32 user_level,u32 user_max)682*3f2dd94aSFrançois Tigeot void intel_panel_set_backlight_acpi(const struct drm_connector_state *conn_state,
68324edb884SFrançois Tigeot u32 user_level, u32 user_max)
68424edb884SFrançois Tigeot {
685*3f2dd94aSFrançois Tigeot struct intel_connector *connector = to_intel_connector(conn_state->connector);
686aee94f86SFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
68724edb884SFrançois Tigeot struct intel_panel *panel = &connector->panel;
68824edb884SFrançois Tigeot u32 hw_level;
68924edb884SFrançois Tigeot
6902c9916cdSFrançois Tigeot /*
691*3f2dd94aSFrançois Tigeot * Lack of crtc may occur during driver init because
6922c9916cdSFrançois Tigeot * connection_mutex isn't held across the entire backlight
6932c9916cdSFrançois Tigeot * setup + modeset readout, and the BIOS can issue the
6942c9916cdSFrançois Tigeot * requests at any time.
6952c9916cdSFrançois Tigeot */
696*3f2dd94aSFrançois Tigeot if (!panel->backlight.present || !conn_state->crtc)
69724edb884SFrançois Tigeot return;
69824edb884SFrançois Tigeot
6992c9916cdSFrançois Tigeot mutex_lock(&dev_priv->backlight_lock);
70024edb884SFrançois Tigeot
70124edb884SFrançois Tigeot WARN_ON(panel->backlight.max == 0);
70224edb884SFrançois Tigeot
70324edb884SFrançois Tigeot hw_level = clamp_user_to_hw(connector, user_level, user_max);
70424edb884SFrançois Tigeot panel->backlight.level = hw_level;
70524edb884SFrançois Tigeot
70624edb884SFrançois Tigeot if (panel->backlight.device)
70724edb884SFrançois Tigeot panel->backlight.device->props.brightness =
70824edb884SFrançois Tigeot scale_hw_to_user(connector,
70924edb884SFrançois Tigeot panel->backlight.level,
71024edb884SFrançois Tigeot panel->backlight.device->props.max_brightness);
71124edb884SFrançois Tigeot
71224edb884SFrançois Tigeot if (panel->backlight.enabled)
713*3f2dd94aSFrançois Tigeot intel_panel_actually_set_backlight(conn_state, hw_level);
7149edbd4a0SFrançois Tigeot
7152c9916cdSFrançois Tigeot mutex_unlock(&dev_priv->backlight_lock);
716e3adcf8fSFrançois Tigeot }
717e3adcf8fSFrançois Tigeot
lpt_disable_backlight(const struct drm_connector_state * old_conn_state)718*3f2dd94aSFrançois Tigeot static void lpt_disable_backlight(const struct drm_connector_state *old_conn_state)
719352ff8bdSFrançois Tigeot {
720*3f2dd94aSFrançois Tigeot struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
721aee94f86SFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
722352ff8bdSFrançois Tigeot u32 tmp;
723352ff8bdSFrançois Tigeot
724*3f2dd94aSFrançois Tigeot intel_panel_actually_set_backlight(old_conn_state, 0);
725352ff8bdSFrançois Tigeot
726352ff8bdSFrançois Tigeot /*
727352ff8bdSFrançois Tigeot * Although we don't support or enable CPU PWM with LPT/SPT based
728352ff8bdSFrançois Tigeot * systems, it may have been enabled prior to loading the
729352ff8bdSFrançois Tigeot * driver. Disable to avoid warnings on LCPLL disable.
730352ff8bdSFrançois Tigeot *
731352ff8bdSFrançois Tigeot * This needs rework if we need to add support for CPU PWM on PCH split
732352ff8bdSFrançois Tigeot * platforms.
733352ff8bdSFrançois Tigeot */
734352ff8bdSFrançois Tigeot tmp = I915_READ(BLC_PWM_CPU_CTL2);
735352ff8bdSFrançois Tigeot if (tmp & BLM_PWM_ENABLE) {
736352ff8bdSFrançois Tigeot DRM_DEBUG_KMS("cpu backlight was enabled, disabling\n");
737352ff8bdSFrançois Tigeot I915_WRITE(BLC_PWM_CPU_CTL2, tmp & ~BLM_PWM_ENABLE);
738352ff8bdSFrançois Tigeot }
739352ff8bdSFrançois Tigeot
740352ff8bdSFrançois Tigeot tmp = I915_READ(BLC_PWM_PCH_CTL1);
741352ff8bdSFrançois Tigeot I915_WRITE(BLC_PWM_PCH_CTL1, tmp & ~BLM_PCH_PWM_ENABLE);
742352ff8bdSFrançois Tigeot }
743352ff8bdSFrançois Tigeot
pch_disable_backlight(const struct drm_connector_state * old_conn_state)744*3f2dd94aSFrançois Tigeot static void pch_disable_backlight(const struct drm_connector_state *old_conn_state)
745e3adcf8fSFrançois Tigeot {
746*3f2dd94aSFrançois Tigeot struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
747aee94f86SFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
7489edbd4a0SFrançois Tigeot u32 tmp;
749e3adcf8fSFrançois Tigeot
750*3f2dd94aSFrançois Tigeot intel_panel_actually_set_backlight(old_conn_state, 0);
7515d0b1887SFrançois Tigeot
7529edbd4a0SFrançois Tigeot tmp = I915_READ(BLC_PWM_CPU_CTL2);
7539edbd4a0SFrançois Tigeot I915_WRITE(BLC_PWM_CPU_CTL2, tmp & ~BLM_PWM_ENABLE);
75419df918dSFrançois Tigeot
755e3440f96SFrançois Tigeot tmp = I915_READ(BLC_PWM_PCH_CTL1);
7569edbd4a0SFrançois Tigeot I915_WRITE(BLC_PWM_PCH_CTL1, tmp & ~BLM_PCH_PWM_ENABLE);
75719df918dSFrançois Tigeot }
7585d0b1887SFrançois Tigeot
i9xx_disable_backlight(const struct drm_connector_state * old_conn_state)759*3f2dd94aSFrançois Tigeot static void i9xx_disable_backlight(const struct drm_connector_state *old_conn_state)
760e3adcf8fSFrançois Tigeot {
761*3f2dd94aSFrançois Tigeot intel_panel_actually_set_backlight(old_conn_state, 0);
7629edbd4a0SFrançois Tigeot }
7639edbd4a0SFrançois Tigeot
i965_disable_backlight(const struct drm_connector_state * old_conn_state)764*3f2dd94aSFrançois Tigeot static void i965_disable_backlight(const struct drm_connector_state *old_conn_state)
7659edbd4a0SFrançois Tigeot {
766*3f2dd94aSFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(old_conn_state->connector->dev);
7679edbd4a0SFrançois Tigeot u32 tmp;
7689edbd4a0SFrançois Tigeot
769*3f2dd94aSFrançois Tigeot intel_panel_actually_set_backlight(old_conn_state, 0);
7709edbd4a0SFrançois Tigeot
7719edbd4a0SFrançois Tigeot tmp = I915_READ(BLC_PWM_CTL2);
7729edbd4a0SFrançois Tigeot I915_WRITE(BLC_PWM_CTL2, tmp & ~BLM_PWM_ENABLE);
7739edbd4a0SFrançois Tigeot }
7749edbd4a0SFrançois Tigeot
vlv_disable_backlight(const struct drm_connector_state * old_conn_state)775*3f2dd94aSFrançois Tigeot static void vlv_disable_backlight(const struct drm_connector_state *old_conn_state)
7769edbd4a0SFrançois Tigeot {
777*3f2dd94aSFrançois Tigeot struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
778aee94f86SFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
779*3f2dd94aSFrançois Tigeot enum i915_pipe pipe = to_intel_crtc(old_conn_state->crtc)->pipe;
7809edbd4a0SFrançois Tigeot u32 tmp;
7819edbd4a0SFrançois Tigeot
782*3f2dd94aSFrançois Tigeot intel_panel_actually_set_backlight(old_conn_state, 0);
7839edbd4a0SFrançois Tigeot
7849edbd4a0SFrançois Tigeot tmp = I915_READ(VLV_BLC_PWM_CTL2(pipe));
7859edbd4a0SFrançois Tigeot I915_WRITE(VLV_BLC_PWM_CTL2(pipe), tmp & ~BLM_PWM_ENABLE);
7869edbd4a0SFrançois Tigeot }
7879edbd4a0SFrançois Tigeot
bxt_disable_backlight(const struct drm_connector_state * old_conn_state)788*3f2dd94aSFrançois Tigeot static void bxt_disable_backlight(const struct drm_connector_state *old_conn_state)
78919c468b4SFrançois Tigeot {
790*3f2dd94aSFrançois Tigeot struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
791aee94f86SFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
792352ff8bdSFrançois Tigeot struct intel_panel *panel = &connector->panel;
793352ff8bdSFrançois Tigeot u32 tmp, val;
79419c468b4SFrançois Tigeot
795*3f2dd94aSFrançois Tigeot intel_panel_actually_set_backlight(old_conn_state, 0);
79619c468b4SFrançois Tigeot
797352ff8bdSFrançois Tigeot tmp = I915_READ(BXT_BLC_PWM_CTL(panel->backlight.controller));
798352ff8bdSFrançois Tigeot I915_WRITE(BXT_BLC_PWM_CTL(panel->backlight.controller),
799352ff8bdSFrançois Tigeot tmp & ~BXT_BLC_PWM_ENABLE);
800352ff8bdSFrançois Tigeot
801352ff8bdSFrançois Tigeot if (panel->backlight.controller == 1) {
802352ff8bdSFrançois Tigeot val = I915_READ(UTIL_PIN_CTL);
803352ff8bdSFrançois Tigeot val &= ~UTIL_PIN_ENABLE;
804352ff8bdSFrançois Tigeot I915_WRITE(UTIL_PIN_CTL, val);
805352ff8bdSFrançois Tigeot }
80619c468b4SFrançois Tigeot }
80719c468b4SFrançois Tigeot
cnp_disable_backlight(const struct drm_connector_state * old_conn_state)808*3f2dd94aSFrançois Tigeot static void cnp_disable_backlight(const struct drm_connector_state *old_conn_state)
809a05eeebfSFrançois Tigeot {
810*3f2dd94aSFrançois Tigeot struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
811*3f2dd94aSFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
812*3f2dd94aSFrançois Tigeot struct intel_panel *panel = &connector->panel;
813*3f2dd94aSFrançois Tigeot u32 tmp;
814*3f2dd94aSFrançois Tigeot
815*3f2dd94aSFrançois Tigeot intel_panel_actually_set_backlight(old_conn_state, 0);
816*3f2dd94aSFrançois Tigeot
817*3f2dd94aSFrançois Tigeot tmp = I915_READ(BXT_BLC_PWM_CTL(panel->backlight.controller));
818*3f2dd94aSFrançois Tigeot I915_WRITE(BXT_BLC_PWM_CTL(panel->backlight.controller),
819*3f2dd94aSFrançois Tigeot tmp & ~BXT_BLC_PWM_ENABLE);
820*3f2dd94aSFrançois Tigeot }
821*3f2dd94aSFrançois Tigeot
pwm_disable_backlight(const struct drm_connector_state * old_conn_state)822*3f2dd94aSFrançois Tigeot static void pwm_disable_backlight(const struct drm_connector_state *old_conn_state)
823*3f2dd94aSFrançois Tigeot {
824*3f2dd94aSFrançois Tigeot struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
825a05eeebfSFrançois Tigeot struct intel_panel *panel = &connector->panel;
826a05eeebfSFrançois Tigeot
827a05eeebfSFrançois Tigeot /* Disable the backlight */
828a05eeebfSFrançois Tigeot pwm_config(panel->backlight.pwm, 0, CRC_PMIC_PWM_PERIOD_NS);
829a05eeebfSFrançois Tigeot usleep_range(2000, 3000);
830a05eeebfSFrançois Tigeot pwm_disable(panel->backlight.pwm);
831a05eeebfSFrançois Tigeot }
832a05eeebfSFrançois Tigeot
intel_panel_disable_backlight(const struct drm_connector_state * old_conn_state)833*3f2dd94aSFrançois Tigeot void intel_panel_disable_backlight(const struct drm_connector_state *old_conn_state)
8349edbd4a0SFrançois Tigeot {
835*3f2dd94aSFrançois Tigeot struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
836aee94f86SFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
8379edbd4a0SFrançois Tigeot struct intel_panel *panel = &connector->panel;
8389edbd4a0SFrançois Tigeot
8392c9916cdSFrançois Tigeot if (!panel->backlight.present)
8409edbd4a0SFrançois Tigeot return;
8419edbd4a0SFrançois Tigeot
8429edbd4a0SFrançois Tigeot /*
843352ff8bdSFrançois Tigeot * Do not disable backlight on the vga_switcheroo path. When switching
8449edbd4a0SFrançois Tigeot * away from i915, the other client may depend on i915 to handle the
8459edbd4a0SFrançois Tigeot * backlight. This will leave the backlight on unnecessarily when
8469edbd4a0SFrançois Tigeot * another client is not activated.
8479edbd4a0SFrançois Tigeot */
848303bf270SFrançois Tigeot if (dev_priv->drm.switch_power_state == DRM_SWITCH_POWER_CHANGING) {
8499edbd4a0SFrançois Tigeot DRM_DEBUG_DRIVER("Skipping backlight disable on vga switch\n");
8509edbd4a0SFrançois Tigeot return;
8519edbd4a0SFrançois Tigeot }
8529edbd4a0SFrançois Tigeot
8532c9916cdSFrançois Tigeot mutex_lock(&dev_priv->backlight_lock);
8549edbd4a0SFrançois Tigeot
8551b13d190SFrançois Tigeot if (panel->backlight.device)
8561b13d190SFrançois Tigeot panel->backlight.device->props.power = FB_BLANK_POWERDOWN;
8579edbd4a0SFrançois Tigeot panel->backlight.enabled = false;
858*3f2dd94aSFrançois Tigeot panel->backlight.disable(old_conn_state);
8599edbd4a0SFrançois Tigeot
8602c9916cdSFrançois Tigeot mutex_unlock(&dev_priv->backlight_lock);
8619edbd4a0SFrançois Tigeot }
8629edbd4a0SFrançois Tigeot
lpt_enable_backlight(const struct intel_crtc_state * crtc_state,const struct drm_connector_state * conn_state)863*3f2dd94aSFrançois Tigeot static void lpt_enable_backlight(const struct intel_crtc_state *crtc_state,
864*3f2dd94aSFrançois Tigeot const struct drm_connector_state *conn_state)
8659edbd4a0SFrançois Tigeot {
866*3f2dd94aSFrançois Tigeot struct intel_connector *connector = to_intel_connector(conn_state->connector);
867aee94f86SFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
8689edbd4a0SFrançois Tigeot struct intel_panel *panel = &connector->panel;
869bf017597SFrançois Tigeot u32 pch_ctl1, pch_ctl2, schicken;
8709edbd4a0SFrançois Tigeot
8719edbd4a0SFrançois Tigeot pch_ctl1 = I915_READ(BLC_PWM_PCH_CTL1);
8729edbd4a0SFrançois Tigeot if (pch_ctl1 & BLM_PCH_PWM_ENABLE) {
8739edbd4a0SFrançois Tigeot DRM_DEBUG_KMS("pch backlight already enabled\n");
8749edbd4a0SFrançois Tigeot pch_ctl1 &= ~BLM_PCH_PWM_ENABLE;
8759edbd4a0SFrançois Tigeot I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1);
8769edbd4a0SFrançois Tigeot }
8779edbd4a0SFrançois Tigeot
878bf017597SFrançois Tigeot if (HAS_PCH_LPT(dev_priv)) {
879bf017597SFrançois Tigeot schicken = I915_READ(SOUTH_CHICKEN2);
880bf017597SFrançois Tigeot if (panel->backlight.alternate_pwm_increment)
881bf017597SFrançois Tigeot schicken |= LPT_PWM_GRANULARITY;
882bf017597SFrançois Tigeot else
883bf017597SFrançois Tigeot schicken &= ~LPT_PWM_GRANULARITY;
884bf017597SFrançois Tigeot I915_WRITE(SOUTH_CHICKEN2, schicken);
885bf017597SFrançois Tigeot } else {
886bf017597SFrançois Tigeot schicken = I915_READ(SOUTH_CHICKEN1);
887bf017597SFrançois Tigeot if (panel->backlight.alternate_pwm_increment)
888bf017597SFrançois Tigeot schicken |= SPT_PWM_GRANULARITY;
889bf017597SFrançois Tigeot else
890bf017597SFrançois Tigeot schicken &= ~SPT_PWM_GRANULARITY;
891bf017597SFrançois Tigeot I915_WRITE(SOUTH_CHICKEN1, schicken);
892bf017597SFrançois Tigeot }
893bf017597SFrançois Tigeot
8949edbd4a0SFrançois Tigeot pch_ctl2 = panel->backlight.max << 16;
8959edbd4a0SFrançois Tigeot I915_WRITE(BLC_PWM_PCH_CTL2, pch_ctl2);
8969edbd4a0SFrançois Tigeot
8979edbd4a0SFrançois Tigeot pch_ctl1 = 0;
8989edbd4a0SFrançois Tigeot if (panel->backlight.active_low_pwm)
8999edbd4a0SFrançois Tigeot pch_ctl1 |= BLM_PCH_POLARITY;
9009edbd4a0SFrançois Tigeot
9012c9916cdSFrançois Tigeot /* After LPT, override is the default. */
9022c9916cdSFrançois Tigeot if (HAS_PCH_LPT(dev_priv))
9039edbd4a0SFrançois Tigeot pch_ctl1 |= BLM_PCH_OVERRIDE_ENABLE;
9049edbd4a0SFrançois Tigeot
9059edbd4a0SFrançois Tigeot I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1);
9069edbd4a0SFrançois Tigeot POSTING_READ(BLC_PWM_PCH_CTL1);
9079edbd4a0SFrançois Tigeot I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1 | BLM_PCH_PWM_ENABLE);
9089edbd4a0SFrançois Tigeot
9099edbd4a0SFrançois Tigeot /* This won't stick until the above enable. */
910*3f2dd94aSFrançois Tigeot intel_panel_actually_set_backlight(conn_state, panel->backlight.level);
9119edbd4a0SFrançois Tigeot }
9129edbd4a0SFrançois Tigeot
pch_enable_backlight(const struct intel_crtc_state * crtc_state,const struct drm_connector_state * conn_state)913*3f2dd94aSFrançois Tigeot static void pch_enable_backlight(const struct intel_crtc_state *crtc_state,
914*3f2dd94aSFrançois Tigeot const struct drm_connector_state *conn_state)
9159edbd4a0SFrançois Tigeot {
916*3f2dd94aSFrançois Tigeot struct intel_connector *connector = to_intel_connector(conn_state->connector);
917aee94f86SFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
9189edbd4a0SFrançois Tigeot struct intel_panel *panel = &connector->panel;
919*3f2dd94aSFrançois Tigeot enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
9209edbd4a0SFrançois Tigeot u32 cpu_ctl2, pch_ctl1, pch_ctl2;
9215d0b1887SFrançois Tigeot
9229edbd4a0SFrançois Tigeot cpu_ctl2 = I915_READ(BLC_PWM_CPU_CTL2);
9239edbd4a0SFrançois Tigeot if (cpu_ctl2 & BLM_PWM_ENABLE) {
92424edb884SFrançois Tigeot DRM_DEBUG_KMS("cpu backlight already enabled\n");
9259edbd4a0SFrançois Tigeot cpu_ctl2 &= ~BLM_PWM_ENABLE;
9269edbd4a0SFrançois Tigeot I915_WRITE(BLC_PWM_CPU_CTL2, cpu_ctl2);
9278e26cdf6SFrançois Tigeot }
928e3adcf8fSFrançois Tigeot
9299edbd4a0SFrançois Tigeot pch_ctl1 = I915_READ(BLC_PWM_PCH_CTL1);
9309edbd4a0SFrançois Tigeot if (pch_ctl1 & BLM_PCH_PWM_ENABLE) {
9319edbd4a0SFrançois Tigeot DRM_DEBUG_KMS("pch backlight already enabled\n");
9329edbd4a0SFrançois Tigeot pch_ctl1 &= ~BLM_PCH_PWM_ENABLE;
9339edbd4a0SFrançois Tigeot I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1);
9349edbd4a0SFrançois Tigeot }
93519df918dSFrançois Tigeot
9365d0b1887SFrançois Tigeot if (cpu_transcoder == TRANSCODER_EDP)
9379edbd4a0SFrançois Tigeot cpu_ctl2 = BLM_TRANSCODER_EDP;
9385d0b1887SFrançois Tigeot else
9399edbd4a0SFrançois Tigeot cpu_ctl2 = BLM_PIPE(cpu_transcoder);
9409edbd4a0SFrançois Tigeot I915_WRITE(BLC_PWM_CPU_CTL2, cpu_ctl2);
9419edbd4a0SFrançois Tigeot POSTING_READ(BLC_PWM_CPU_CTL2);
9429edbd4a0SFrançois Tigeot I915_WRITE(BLC_PWM_CPU_CTL2, cpu_ctl2 | BLM_PWM_ENABLE);
94319df918dSFrançois Tigeot
9449edbd4a0SFrançois Tigeot /* This won't stick until the above enable. */
945*3f2dd94aSFrançois Tigeot intel_panel_actually_set_backlight(conn_state, panel->backlight.level);
94619df918dSFrançois Tigeot
9479edbd4a0SFrançois Tigeot pch_ctl2 = panel->backlight.max << 16;
9489edbd4a0SFrançois Tigeot I915_WRITE(BLC_PWM_PCH_CTL2, pch_ctl2);
9499edbd4a0SFrançois Tigeot
9509edbd4a0SFrançois Tigeot pch_ctl1 = 0;
9519edbd4a0SFrançois Tigeot if (panel->backlight.active_low_pwm)
9529edbd4a0SFrançois Tigeot pch_ctl1 |= BLM_PCH_POLARITY;
953ba55f2f5SFrançois Tigeot
9549edbd4a0SFrançois Tigeot I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1);
9559edbd4a0SFrançois Tigeot POSTING_READ(BLC_PWM_PCH_CTL1);
9569edbd4a0SFrançois Tigeot I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1 | BLM_PCH_PWM_ENABLE);
95719df918dSFrançois Tigeot }
95819df918dSFrançois Tigeot
i9xx_enable_backlight(const struct intel_crtc_state * crtc_state,const struct drm_connector_state * conn_state)959*3f2dd94aSFrançois Tigeot static void i9xx_enable_backlight(const struct intel_crtc_state *crtc_state,
960*3f2dd94aSFrançois Tigeot const struct drm_connector_state *conn_state)
961e3adcf8fSFrançois Tigeot {
962*3f2dd94aSFrançois Tigeot struct intel_connector *connector = to_intel_connector(conn_state->connector);
963aee94f86SFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
9649edbd4a0SFrançois Tigeot struct intel_panel *panel = &connector->panel;
9659edbd4a0SFrançois Tigeot u32 ctl, freq;
966e3adcf8fSFrançois Tigeot
9679edbd4a0SFrançois Tigeot ctl = I915_READ(BLC_PWM_CTL);
9689edbd4a0SFrançois Tigeot if (ctl & BACKLIGHT_DUTY_CYCLE_MASK_PNV) {
96924edb884SFrançois Tigeot DRM_DEBUG_KMS("backlight already enabled\n");
9709edbd4a0SFrançois Tigeot I915_WRITE(BLC_PWM_CTL, 0);
9719edbd4a0SFrançois Tigeot }
9729edbd4a0SFrançois Tigeot
9739edbd4a0SFrançois Tigeot freq = panel->backlight.max;
9749edbd4a0SFrançois Tigeot if (panel->backlight.combination_mode)
9759edbd4a0SFrançois Tigeot freq /= 0xff;
9769edbd4a0SFrançois Tigeot
9779edbd4a0SFrançois Tigeot ctl = freq << 17;
9789edbd4a0SFrançois Tigeot if (panel->backlight.combination_mode)
9799edbd4a0SFrançois Tigeot ctl |= BLM_LEGACY_MODE;
980aee94f86SFrançois Tigeot if (IS_PINEVIEW(dev_priv) && panel->backlight.active_low_pwm)
9819edbd4a0SFrançois Tigeot ctl |= BLM_POLARITY_PNV;
9829edbd4a0SFrançois Tigeot
9839edbd4a0SFrançois Tigeot I915_WRITE(BLC_PWM_CTL, ctl);
9849edbd4a0SFrançois Tigeot POSTING_READ(BLC_PWM_CTL);
9859edbd4a0SFrançois Tigeot
9869edbd4a0SFrançois Tigeot /* XXX: combine this into above write? */
987*3f2dd94aSFrançois Tigeot intel_panel_actually_set_backlight(conn_state, panel->backlight.level);
98819c468b4SFrançois Tigeot
98919c468b4SFrançois Tigeot /*
99019c468b4SFrançois Tigeot * Needed to enable backlight on some 855gm models. BLC_HIST_CTL is
99119c468b4SFrançois Tigeot * 855gm only, but checking for gen2 is safe, as 855gm is the only gen2
99219c468b4SFrançois Tigeot * that has backlight.
99319c468b4SFrançois Tigeot */
994aee94f86SFrançois Tigeot if (IS_GEN2(dev_priv))
99519c468b4SFrançois Tigeot I915_WRITE(BLC_HIST_CTL, BLM_HISTOGRAM_ENABLE);
9969edbd4a0SFrançois Tigeot }
9979edbd4a0SFrançois Tigeot
i965_enable_backlight(const struct intel_crtc_state * crtc_state,const struct drm_connector_state * conn_state)998*3f2dd94aSFrançois Tigeot static void i965_enable_backlight(const struct intel_crtc_state *crtc_state,
999*3f2dd94aSFrançois Tigeot const struct drm_connector_state *conn_state)
10009edbd4a0SFrançois Tigeot {
1001*3f2dd94aSFrançois Tigeot struct intel_connector *connector = to_intel_connector(conn_state->connector);
1002aee94f86SFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
10039edbd4a0SFrançois Tigeot struct intel_panel *panel = &connector->panel;
1004*3f2dd94aSFrançois Tigeot enum i915_pipe pipe = to_intel_crtc(conn_state->crtc)->pipe;
10059edbd4a0SFrançois Tigeot u32 ctl, ctl2, freq;
10069edbd4a0SFrançois Tigeot
10079edbd4a0SFrançois Tigeot ctl2 = I915_READ(BLC_PWM_CTL2);
10089edbd4a0SFrançois Tigeot if (ctl2 & BLM_PWM_ENABLE) {
100924edb884SFrançois Tigeot DRM_DEBUG_KMS("backlight already enabled\n");
10109edbd4a0SFrançois Tigeot ctl2 &= ~BLM_PWM_ENABLE;
10119edbd4a0SFrançois Tigeot I915_WRITE(BLC_PWM_CTL2, ctl2);
10129edbd4a0SFrançois Tigeot }
10139edbd4a0SFrançois Tigeot
10149edbd4a0SFrançois Tigeot freq = panel->backlight.max;
10159edbd4a0SFrançois Tigeot if (panel->backlight.combination_mode)
10169edbd4a0SFrançois Tigeot freq /= 0xff;
10179edbd4a0SFrançois Tigeot
10189edbd4a0SFrançois Tigeot ctl = freq << 16;
10199edbd4a0SFrançois Tigeot I915_WRITE(BLC_PWM_CTL, ctl);
10209edbd4a0SFrançois Tigeot
10219edbd4a0SFrançois Tigeot ctl2 = BLM_PIPE(pipe);
10229edbd4a0SFrançois Tigeot if (panel->backlight.combination_mode)
10239edbd4a0SFrançois Tigeot ctl2 |= BLM_COMBINATION_MODE;
10249edbd4a0SFrançois Tigeot if (panel->backlight.active_low_pwm)
10259edbd4a0SFrançois Tigeot ctl2 |= BLM_POLARITY_I965;
10269edbd4a0SFrançois Tigeot I915_WRITE(BLC_PWM_CTL2, ctl2);
10279edbd4a0SFrançois Tigeot POSTING_READ(BLC_PWM_CTL2);
10289edbd4a0SFrançois Tigeot I915_WRITE(BLC_PWM_CTL2, ctl2 | BLM_PWM_ENABLE);
1029ba55f2f5SFrançois Tigeot
1030*3f2dd94aSFrançois Tigeot intel_panel_actually_set_backlight(conn_state, panel->backlight.level);
10319edbd4a0SFrançois Tigeot }
10329edbd4a0SFrançois Tigeot
vlv_enable_backlight(const struct intel_crtc_state * crtc_state,const struct drm_connector_state * conn_state)1033*3f2dd94aSFrançois Tigeot static void vlv_enable_backlight(const struct intel_crtc_state *crtc_state,
1034*3f2dd94aSFrançois Tigeot const struct drm_connector_state *conn_state)
10359edbd4a0SFrançois Tigeot {
1036*3f2dd94aSFrançois Tigeot struct intel_connector *connector = to_intel_connector(conn_state->connector);
1037aee94f86SFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
10389edbd4a0SFrançois Tigeot struct intel_panel *panel = &connector->panel;
1039*3f2dd94aSFrançois Tigeot enum i915_pipe pipe = to_intel_crtc(crtc_state->base.crtc)->pipe;
10409edbd4a0SFrançois Tigeot u32 ctl, ctl2;
10419edbd4a0SFrançois Tigeot
10429edbd4a0SFrançois Tigeot ctl2 = I915_READ(VLV_BLC_PWM_CTL2(pipe));
10439edbd4a0SFrançois Tigeot if (ctl2 & BLM_PWM_ENABLE) {
104424edb884SFrançois Tigeot DRM_DEBUG_KMS("backlight already enabled\n");
10459edbd4a0SFrançois Tigeot ctl2 &= ~BLM_PWM_ENABLE;
10469edbd4a0SFrançois Tigeot I915_WRITE(VLV_BLC_PWM_CTL2(pipe), ctl2);
10479edbd4a0SFrançois Tigeot }
10489edbd4a0SFrançois Tigeot
10499edbd4a0SFrançois Tigeot ctl = panel->backlight.max << 16;
10509edbd4a0SFrançois Tigeot I915_WRITE(VLV_BLC_PWM_CTL(pipe), ctl);
10519edbd4a0SFrançois Tigeot
10529edbd4a0SFrançois Tigeot /* XXX: combine this into above write? */
1053*3f2dd94aSFrançois Tigeot intel_panel_actually_set_backlight(conn_state, panel->backlight.level);
10549edbd4a0SFrançois Tigeot
10559edbd4a0SFrançois Tigeot ctl2 = 0;
10569edbd4a0SFrançois Tigeot if (panel->backlight.active_low_pwm)
10579edbd4a0SFrançois Tigeot ctl2 |= BLM_POLARITY_I965;
10589edbd4a0SFrançois Tigeot I915_WRITE(VLV_BLC_PWM_CTL2(pipe), ctl2);
10599edbd4a0SFrançois Tigeot POSTING_READ(VLV_BLC_PWM_CTL2(pipe));
10609edbd4a0SFrançois Tigeot I915_WRITE(VLV_BLC_PWM_CTL2(pipe), ctl2 | BLM_PWM_ENABLE);
10619edbd4a0SFrançois Tigeot }
10629edbd4a0SFrançois Tigeot
bxt_enable_backlight(const struct intel_crtc_state * crtc_state,const struct drm_connector_state * conn_state)1063*3f2dd94aSFrançois Tigeot static void bxt_enable_backlight(const struct intel_crtc_state *crtc_state,
1064*3f2dd94aSFrançois Tigeot const struct drm_connector_state *conn_state)
106519c468b4SFrançois Tigeot {
1066*3f2dd94aSFrançois Tigeot struct intel_connector *connector = to_intel_connector(conn_state->connector);
1067aee94f86SFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
106819c468b4SFrançois Tigeot struct intel_panel *panel = &connector->panel;
1069*3f2dd94aSFrançois Tigeot enum i915_pipe pipe = to_intel_crtc(crtc_state->base.crtc)->pipe;
1070352ff8bdSFrançois Tigeot u32 pwm_ctl, val;
107119c468b4SFrançois Tigeot
1072a85cb24fSFrançois Tigeot /* Controller 1 uses the utility pin. */
1073352ff8bdSFrançois Tigeot if (panel->backlight.controller == 1) {
1074352ff8bdSFrançois Tigeot val = I915_READ(UTIL_PIN_CTL);
1075352ff8bdSFrançois Tigeot if (val & UTIL_PIN_ENABLE) {
1076352ff8bdSFrançois Tigeot DRM_DEBUG_KMS("util pin already enabled\n");
1077352ff8bdSFrançois Tigeot val &= ~UTIL_PIN_ENABLE;
1078352ff8bdSFrançois Tigeot I915_WRITE(UTIL_PIN_CTL, val);
1079352ff8bdSFrançois Tigeot }
1080352ff8bdSFrançois Tigeot
1081352ff8bdSFrançois Tigeot val = 0;
1082352ff8bdSFrançois Tigeot if (panel->backlight.util_pin_active_low)
1083352ff8bdSFrançois Tigeot val |= UTIL_PIN_POLARITY;
1084352ff8bdSFrançois Tigeot I915_WRITE(UTIL_PIN_CTL, val | UTIL_PIN_PIPE(pipe) |
1085352ff8bdSFrançois Tigeot UTIL_PIN_MODE_PWM | UTIL_PIN_ENABLE);
1086352ff8bdSFrançois Tigeot }
1087352ff8bdSFrançois Tigeot
1088352ff8bdSFrançois Tigeot pwm_ctl = I915_READ(BXT_BLC_PWM_CTL(panel->backlight.controller));
108919c468b4SFrançois Tigeot if (pwm_ctl & BXT_BLC_PWM_ENABLE) {
109019c468b4SFrançois Tigeot DRM_DEBUG_KMS("backlight already enabled\n");
109119c468b4SFrançois Tigeot pwm_ctl &= ~BXT_BLC_PWM_ENABLE;
1092352ff8bdSFrançois Tigeot I915_WRITE(BXT_BLC_PWM_CTL(panel->backlight.controller),
1093352ff8bdSFrançois Tigeot pwm_ctl);
109419c468b4SFrançois Tigeot }
109519c468b4SFrançois Tigeot
1096352ff8bdSFrançois Tigeot I915_WRITE(BXT_BLC_PWM_FREQ(panel->backlight.controller),
1097352ff8bdSFrançois Tigeot panel->backlight.max);
109819c468b4SFrançois Tigeot
1099*3f2dd94aSFrançois Tigeot intel_panel_actually_set_backlight(conn_state, panel->backlight.level);
110019c468b4SFrançois Tigeot
110119c468b4SFrançois Tigeot pwm_ctl = 0;
110219c468b4SFrançois Tigeot if (panel->backlight.active_low_pwm)
110319c468b4SFrançois Tigeot pwm_ctl |= BXT_BLC_PWM_POLARITY;
110419c468b4SFrançois Tigeot
1105352ff8bdSFrançois Tigeot I915_WRITE(BXT_BLC_PWM_CTL(panel->backlight.controller), pwm_ctl);
1106352ff8bdSFrançois Tigeot POSTING_READ(BXT_BLC_PWM_CTL(panel->backlight.controller));
1107352ff8bdSFrançois Tigeot I915_WRITE(BXT_BLC_PWM_CTL(panel->backlight.controller),
1108352ff8bdSFrançois Tigeot pwm_ctl | BXT_BLC_PWM_ENABLE);
110919c468b4SFrançois Tigeot }
111019c468b4SFrançois Tigeot
cnp_enable_backlight(const struct intel_crtc_state * crtc_state,const struct drm_connector_state * conn_state)1111*3f2dd94aSFrançois Tigeot static void cnp_enable_backlight(const struct intel_crtc_state *crtc_state,
1112*3f2dd94aSFrançois Tigeot const struct drm_connector_state *conn_state)
1113a05eeebfSFrançois Tigeot {
1114*3f2dd94aSFrançois Tigeot struct intel_connector *connector = to_intel_connector(conn_state->connector);
1115*3f2dd94aSFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1116*3f2dd94aSFrançois Tigeot struct intel_panel *panel = &connector->panel;
1117*3f2dd94aSFrançois Tigeot u32 pwm_ctl;
1118*3f2dd94aSFrançois Tigeot
1119*3f2dd94aSFrançois Tigeot pwm_ctl = I915_READ(BXT_BLC_PWM_CTL(panel->backlight.controller));
1120*3f2dd94aSFrançois Tigeot if (pwm_ctl & BXT_BLC_PWM_ENABLE) {
1121*3f2dd94aSFrançois Tigeot DRM_DEBUG_KMS("backlight already enabled\n");
1122*3f2dd94aSFrançois Tigeot pwm_ctl &= ~BXT_BLC_PWM_ENABLE;
1123*3f2dd94aSFrançois Tigeot I915_WRITE(BXT_BLC_PWM_CTL(panel->backlight.controller),
1124*3f2dd94aSFrançois Tigeot pwm_ctl);
1125*3f2dd94aSFrançois Tigeot }
1126*3f2dd94aSFrançois Tigeot
1127*3f2dd94aSFrançois Tigeot I915_WRITE(BXT_BLC_PWM_FREQ(panel->backlight.controller),
1128*3f2dd94aSFrançois Tigeot panel->backlight.max);
1129*3f2dd94aSFrançois Tigeot
1130*3f2dd94aSFrançois Tigeot intel_panel_actually_set_backlight(conn_state, panel->backlight.level);
1131*3f2dd94aSFrançois Tigeot
1132*3f2dd94aSFrançois Tigeot pwm_ctl = 0;
1133*3f2dd94aSFrançois Tigeot if (panel->backlight.active_low_pwm)
1134*3f2dd94aSFrançois Tigeot pwm_ctl |= BXT_BLC_PWM_POLARITY;
1135*3f2dd94aSFrançois Tigeot
1136*3f2dd94aSFrançois Tigeot I915_WRITE(BXT_BLC_PWM_CTL(panel->backlight.controller), pwm_ctl);
1137*3f2dd94aSFrançois Tigeot POSTING_READ(BXT_BLC_PWM_CTL(panel->backlight.controller));
1138*3f2dd94aSFrançois Tigeot I915_WRITE(BXT_BLC_PWM_CTL(panel->backlight.controller),
1139*3f2dd94aSFrançois Tigeot pwm_ctl | BXT_BLC_PWM_ENABLE);
1140*3f2dd94aSFrançois Tigeot }
1141*3f2dd94aSFrançois Tigeot
pwm_enable_backlight(const struct intel_crtc_state * crtc_state,const struct drm_connector_state * conn_state)1142*3f2dd94aSFrançois Tigeot static void pwm_enable_backlight(const struct intel_crtc_state *crtc_state,
1143*3f2dd94aSFrançois Tigeot const struct drm_connector_state *conn_state)
1144*3f2dd94aSFrançois Tigeot {
1145*3f2dd94aSFrançois Tigeot struct intel_connector *connector = to_intel_connector(conn_state->connector);
1146a05eeebfSFrançois Tigeot struct intel_panel *panel = &connector->panel;
1147a05eeebfSFrançois Tigeot
1148a05eeebfSFrançois Tigeot pwm_enable(panel->backlight.pwm);
1149*3f2dd94aSFrançois Tigeot intel_panel_actually_set_backlight(conn_state, panel->backlight.level);
1150a05eeebfSFrançois Tigeot }
1151a05eeebfSFrançois Tigeot
intel_panel_enable_backlight(const struct intel_crtc_state * crtc_state,const struct drm_connector_state * conn_state)1152*3f2dd94aSFrançois Tigeot void intel_panel_enable_backlight(const struct intel_crtc_state *crtc_state,
1153*3f2dd94aSFrançois Tigeot const struct drm_connector_state *conn_state)
11549edbd4a0SFrançois Tigeot {
1155*3f2dd94aSFrançois Tigeot struct intel_connector *connector = to_intel_connector(conn_state->connector);
1156aee94f86SFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
11579edbd4a0SFrançois Tigeot struct intel_panel *panel = &connector->panel;
1158*3f2dd94aSFrançois Tigeot enum i915_pipe pipe = to_intel_crtc(crtc_state->base.crtc)->pipe;
11599edbd4a0SFrançois Tigeot
11602c9916cdSFrançois Tigeot if (!panel->backlight.present)
11619edbd4a0SFrançois Tigeot return;
11629edbd4a0SFrançois Tigeot
11639edbd4a0SFrançois Tigeot DRM_DEBUG_KMS("pipe %c\n", pipe_name(pipe));
11649edbd4a0SFrançois Tigeot
11652c9916cdSFrançois Tigeot mutex_lock(&dev_priv->backlight_lock);
11669edbd4a0SFrançois Tigeot
11679edbd4a0SFrançois Tigeot WARN_ON(panel->backlight.max == 0);
11689edbd4a0SFrançois Tigeot
11692c9916cdSFrançois Tigeot if (panel->backlight.level <= panel->backlight.min) {
11709edbd4a0SFrançois Tigeot panel->backlight.level = panel->backlight.max;
11719edbd4a0SFrançois Tigeot if (panel->backlight.device)
11729edbd4a0SFrançois Tigeot panel->backlight.device->props.brightness =
117324edb884SFrançois Tigeot scale_hw_to_user(connector,
117424edb884SFrançois Tigeot panel->backlight.level,
117524edb884SFrançois Tigeot panel->backlight.device->props.max_brightness);
11769edbd4a0SFrançois Tigeot }
11779edbd4a0SFrançois Tigeot
1178*3f2dd94aSFrançois Tigeot panel->backlight.enable(crtc_state, conn_state);
11799edbd4a0SFrançois Tigeot panel->backlight.enabled = true;
11801b13d190SFrançois Tigeot if (panel->backlight.device)
11811b13d190SFrançois Tigeot panel->backlight.device->props.power = FB_BLANK_UNBLANK;
11829edbd4a0SFrançois Tigeot
11832c9916cdSFrançois Tigeot mutex_unlock(&dev_priv->backlight_lock);
1184e3adcf8fSFrançois Tigeot }
1185e3adcf8fSFrançois Tigeot
1186ba55f2f5SFrançois Tigeot #if IS_ENABLED(CONFIG_BACKLIGHT_CLASS_DEVICE)
118776594197Szrj #if 0 /* unused */
11889edbd4a0SFrançois Tigeot static int intel_backlight_device_update_status(struct backlight_device *bd)
1189e3adcf8fSFrançois Tigeot {
11909edbd4a0SFrançois Tigeot struct intel_connector *connector = bl_get_data(bd);
11911b13d190SFrançois Tigeot struct intel_panel *panel = &connector->panel;
11929edbd4a0SFrançois Tigeot struct drm_device *dev = connector->base.dev;
11939edbd4a0SFrançois Tigeot
1194ba55f2f5SFrançois Tigeot drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
11959edbd4a0SFrançois Tigeot DRM_DEBUG_KMS("updating intel_backlight, brightness=%d/%d\n",
11969edbd4a0SFrançois Tigeot bd->props.brightness, bd->props.max_brightness);
1197*3f2dd94aSFrançois Tigeot intel_panel_set_backlight(connector->base.state, bd->props.brightness,
11985d0b1887SFrançois Tigeot bd->props.max_brightness);
11991b13d190SFrançois Tigeot
12001b13d190SFrançois Tigeot /*
12011b13d190SFrançois Tigeot * Allow flipping bl_power as a sub-state of enabled. Sadly the
12021b13d190SFrançois Tigeot * backlight class device does not make it easy to to differentiate
12031b13d190SFrançois Tigeot * between callbacks for brightness and bl_power, so our backlight_power
12041b13d190SFrançois Tigeot * callback needs to take this into account.
12051b13d190SFrançois Tigeot */
12061b13d190SFrançois Tigeot if (panel->backlight.enabled) {
1207352ff8bdSFrançois Tigeot if (panel->backlight.power) {
12081b13d190SFrançois Tigeot bool enable = bd->props.power == FB_BLANK_UNBLANK &&
12091b13d190SFrançois Tigeot bd->props.brightness != 0;
1210352ff8bdSFrançois Tigeot panel->backlight.power(connector, enable);
12111b13d190SFrançois Tigeot }
12121b13d190SFrançois Tigeot } else {
12131b13d190SFrançois Tigeot bd->props.power = FB_BLANK_POWERDOWN;
12141b13d190SFrançois Tigeot }
12151b13d190SFrançois Tigeot
1216ba55f2f5SFrançois Tigeot drm_modeset_unlock(&dev->mode_config.connection_mutex);
121719df918dSFrançois Tigeot return 0;
121819df918dSFrançois Tigeot }
121919df918dSFrançois Tigeot
12209edbd4a0SFrançois Tigeot static int intel_backlight_device_get_brightness(struct backlight_device *bd)
122119df918dSFrançois Tigeot {
12229edbd4a0SFrançois Tigeot struct intel_connector *connector = bl_get_data(bd);
12239edbd4a0SFrançois Tigeot struct drm_device *dev = connector->base.dev;
1224bf017597SFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(dev);
122524edb884SFrançois Tigeot u32 hw_level;
12269edbd4a0SFrançois Tigeot int ret;
12279edbd4a0SFrançois Tigeot
12289edbd4a0SFrançois Tigeot intel_runtime_pm_get(dev_priv);
1229ba55f2f5SFrançois Tigeot drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
123024edb884SFrançois Tigeot
123124edb884SFrançois Tigeot hw_level = intel_panel_get_backlight(connector);
123224edb884SFrançois Tigeot ret = scale_hw_to_user(connector, hw_level, bd->props.max_brightness);
123324edb884SFrançois Tigeot
1234ba55f2f5SFrançois Tigeot drm_modeset_unlock(&dev->mode_config.connection_mutex);
12359edbd4a0SFrançois Tigeot intel_runtime_pm_put(dev_priv);
12369edbd4a0SFrançois Tigeot
12379edbd4a0SFrançois Tigeot return ret;
123819df918dSFrançois Tigeot }
123919df918dSFrançois Tigeot
12409edbd4a0SFrançois Tigeot static const struct backlight_ops intel_backlight_device_ops = {
12419edbd4a0SFrançois Tigeot .update_status = intel_backlight_device_update_status,
12429edbd4a0SFrançois Tigeot .get_brightness = intel_backlight_device_get_brightness,
124319df918dSFrançois Tigeot };
124476594197Szrj #endif
124519df918dSFrançois Tigeot
intel_backlight_device_register(struct intel_connector * connector)12461487f786SFrançois Tigeot int intel_backlight_device_register(struct intel_connector *connector)
124719df918dSFrançois Tigeot {
12489edbd4a0SFrançois Tigeot struct intel_panel *panel = &connector->panel;
124919df918dSFrançois Tigeot struct backlight_properties props;
125019df918dSFrançois Tigeot
12519edbd4a0SFrançois Tigeot if (WARN_ON(panel->backlight.device))
125219df918dSFrançois Tigeot return -ENODEV;
125319df918dSFrançois Tigeot
12542c9916cdSFrançois Tigeot if (!panel->backlight.present)
12552c9916cdSFrançois Tigeot return 0;
12562c9916cdSFrançois Tigeot
125724edb884SFrançois Tigeot WARN_ON(panel->backlight.max == 0);
12589edbd4a0SFrançois Tigeot
125919df918dSFrançois Tigeot memset(&props, 0, sizeof(props));
126019df918dSFrançois Tigeot props.type = BACKLIGHT_RAW;
126124edb884SFrançois Tigeot
126224edb884SFrançois Tigeot /*
126324edb884SFrançois Tigeot * Note: Everything should work even if the backlight device max
126424edb884SFrançois Tigeot * presented to the userspace is arbitrarily chosen.
126524edb884SFrançois Tigeot */
12669edbd4a0SFrançois Tigeot props.max_brightness = panel->backlight.max;
126724edb884SFrançois Tigeot props.brightness = scale_hw_to_user(connector,
126824edb884SFrançois Tigeot panel->backlight.level,
126924edb884SFrançois Tigeot props.max_brightness);
12705d0b1887SFrançois Tigeot
12711b13d190SFrançois Tigeot if (panel->backlight.enabled)
12721b13d190SFrançois Tigeot props.power = FB_BLANK_UNBLANK;
12731b13d190SFrançois Tigeot else
12741b13d190SFrançois Tigeot props.power = FB_BLANK_POWERDOWN;
12751b13d190SFrançois Tigeot
12765d0b1887SFrançois Tigeot /*
12779edbd4a0SFrançois Tigeot * Note: using the same name independent of the connector prevents
12789edbd4a0SFrançois Tigeot * registration of multiple backlight devices in the driver.
12795d0b1887SFrançois Tigeot */
12802c9916cdSFrançois Tigeot #if 0
12819edbd4a0SFrançois Tigeot panel->backlight.device =
128219df918dSFrançois Tigeot backlight_device_register("intel_backlight",
12839edbd4a0SFrançois Tigeot connector->base.kdev,
12849edbd4a0SFrançois Tigeot connector,
12859edbd4a0SFrançois Tigeot &intel_backlight_device_ops, &props);
128619df918dSFrançois Tigeot
12879edbd4a0SFrançois Tigeot if (IS_ERR(panel->backlight.device)) {
128819df918dSFrançois Tigeot DRM_ERROR("Failed to register backlight: %ld\n",
12899edbd4a0SFrançois Tigeot PTR_ERR(panel->backlight.device));
12909edbd4a0SFrançois Tigeot panel->backlight.device = NULL;
129119df918dSFrançois Tigeot return -ENODEV;
129219df918dSFrançois Tigeot }
12932c9916cdSFrançois Tigeot
12942c9916cdSFrançois Tigeot DRM_DEBUG_KMS("Connector %s backlight sysfs interface registered\n",
12952c9916cdSFrançois Tigeot connector->base.name);
129680001c84SFrançois Tigeot #endif
12972c9916cdSFrançois Tigeot
129819df918dSFrançois Tigeot return 0;
129919df918dSFrançois Tigeot }
130019df918dSFrançois Tigeot
intel_backlight_device_unregister(struct intel_connector * connector)13011487f786SFrançois Tigeot void intel_backlight_device_unregister(struct intel_connector *connector)
130219df918dSFrançois Tigeot {
13032c9916cdSFrançois Tigeot #if 0
13049edbd4a0SFrançois Tigeot struct intel_panel *panel = &connector->panel;
13059edbd4a0SFrançois Tigeot
13069edbd4a0SFrançois Tigeot if (panel->backlight.device) {
13079edbd4a0SFrançois Tigeot backlight_device_unregister(panel->backlight.device);
13089edbd4a0SFrançois Tigeot panel->backlight.device = NULL;
13099edbd4a0SFrançois Tigeot }
13102c9916cdSFrançois Tigeot #endif
13119edbd4a0SFrançois Tigeot }
13129edbd4a0SFrançois Tigeot #endif /* CONFIG_BACKLIGHT_CLASS_DEVICE */
13139edbd4a0SFrançois Tigeot
13149edbd4a0SFrançois Tigeot /*
1315*3f2dd94aSFrançois Tigeot * CNP: PWM clock frequency is 19.2 MHz or 24 MHz.
1316*3f2dd94aSFrançois Tigeot * PWM increment = 1
1317*3f2dd94aSFrançois Tigeot */
cnp_hz_to_pwm(struct intel_connector * connector,u32 pwm_freq_hz)1318*3f2dd94aSFrançois Tigeot static u32 cnp_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
1319*3f2dd94aSFrançois Tigeot {
1320*3f2dd94aSFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1321*3f2dd94aSFrançois Tigeot
1322*3f2dd94aSFrançois Tigeot return DIV_ROUND_CLOSEST(KHz(dev_priv->rawclk_freq), pwm_freq_hz);
1323*3f2dd94aSFrançois Tigeot }
1324*3f2dd94aSFrançois Tigeot
1325*3f2dd94aSFrançois Tigeot /*
1326aee94f86SFrançois Tigeot * BXT: PWM clock frequency = 19.2 MHz.
1327aee94f86SFrançois Tigeot */
bxt_hz_to_pwm(struct intel_connector * connector,u32 pwm_freq_hz)1328aee94f86SFrançois Tigeot static u32 bxt_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
1329aee94f86SFrançois Tigeot {
13308621f407SFrançois Tigeot return DIV_ROUND_CLOSEST(KHz(19200), pwm_freq_hz);
1331aee94f86SFrançois Tigeot }
1332aee94f86SFrançois Tigeot
1333aee94f86SFrançois Tigeot /*
1334352ff8bdSFrançois Tigeot * SPT: This value represents the period of the PWM stream in clock periods
1335352ff8bdSFrançois Tigeot * multiplied by 16 (default increment) or 128 (alternate increment selected in
1336352ff8bdSFrançois Tigeot * SCHICKEN_1 bit 0). PWM clock is 24 MHz.
1337352ff8bdSFrançois Tigeot */
spt_hz_to_pwm(struct intel_connector * connector,u32 pwm_freq_hz)1338352ff8bdSFrançois Tigeot static u32 spt_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
1339352ff8bdSFrançois Tigeot {
1340bf017597SFrançois Tigeot struct intel_panel *panel = &connector->panel;
13418621f407SFrançois Tigeot u32 mul;
1342352ff8bdSFrançois Tigeot
1343bf017597SFrançois Tigeot if (panel->backlight.alternate_pwm_increment)
1344352ff8bdSFrançois Tigeot mul = 128;
1345352ff8bdSFrançois Tigeot else
1346352ff8bdSFrançois Tigeot mul = 16;
1347352ff8bdSFrançois Tigeot
13488621f407SFrançois Tigeot return DIV_ROUND_CLOSEST(MHz(24), pwm_freq_hz * mul);
1349352ff8bdSFrançois Tigeot }
1350352ff8bdSFrançois Tigeot
1351352ff8bdSFrançois Tigeot /*
1352352ff8bdSFrançois Tigeot * LPT: This value represents the period of the PWM stream in clock periods
1353352ff8bdSFrançois Tigeot * multiplied by 128 (default increment) or 16 (alternate increment, selected in
1354352ff8bdSFrançois Tigeot * LPT SOUTH_CHICKEN2 register bit 5).
1355352ff8bdSFrançois Tigeot */
lpt_hz_to_pwm(struct intel_connector * connector,u32 pwm_freq_hz)1356352ff8bdSFrançois Tigeot static u32 lpt_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
1357352ff8bdSFrançois Tigeot {
1358aee94f86SFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1359bf017597SFrançois Tigeot struct intel_panel *panel = &connector->panel;
1360352ff8bdSFrançois Tigeot u32 mul, clock;
1361352ff8bdSFrançois Tigeot
1362bf017597SFrançois Tigeot if (panel->backlight.alternate_pwm_increment)
1363352ff8bdSFrançois Tigeot mul = 16;
1364352ff8bdSFrançois Tigeot else
1365352ff8bdSFrançois Tigeot mul = 128;
1366352ff8bdSFrançois Tigeot
1367aee94f86SFrançois Tigeot if (HAS_PCH_LPT_H(dev_priv))
1368352ff8bdSFrançois Tigeot clock = MHz(135); /* LPT:H */
1369352ff8bdSFrançois Tigeot else
1370352ff8bdSFrançois Tigeot clock = MHz(24); /* LPT:LP */
1371352ff8bdSFrançois Tigeot
13728621f407SFrançois Tigeot return DIV_ROUND_CLOSEST(clock, pwm_freq_hz * mul);
1373352ff8bdSFrançois Tigeot }
1374352ff8bdSFrançois Tigeot
1375352ff8bdSFrançois Tigeot /*
1376352ff8bdSFrançois Tigeot * ILK/SNB/IVB: This value represents the period of the PWM stream in PCH
1377352ff8bdSFrançois Tigeot * display raw clocks multiplied by 128.
1378352ff8bdSFrançois Tigeot */
pch_hz_to_pwm(struct intel_connector * connector,u32 pwm_freq_hz)1379352ff8bdSFrançois Tigeot static u32 pch_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
1380352ff8bdSFrançois Tigeot {
13818621f407SFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1382352ff8bdSFrançois Tigeot
13838621f407SFrançois Tigeot return DIV_ROUND_CLOSEST(KHz(dev_priv->rawclk_freq), pwm_freq_hz * 128);
1384352ff8bdSFrançois Tigeot }
1385352ff8bdSFrançois Tigeot
1386352ff8bdSFrançois Tigeot /*
1387352ff8bdSFrançois Tigeot * Gen2: This field determines the number of time base events (display core
1388352ff8bdSFrançois Tigeot * clock frequency/32) in total for a complete cycle of modulated backlight
1389352ff8bdSFrançois Tigeot * control.
13909edbd4a0SFrançois Tigeot *
1391352ff8bdSFrançois Tigeot * Gen3: A time base event equals the display core clock ([DevPNV] HRAW clock)
1392352ff8bdSFrançois Tigeot * divided by 32.
1393352ff8bdSFrançois Tigeot */
i9xx_hz_to_pwm(struct intel_connector * connector,u32 pwm_freq_hz)1394352ff8bdSFrançois Tigeot static u32 i9xx_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
1395352ff8bdSFrançois Tigeot {
13968621f407SFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1397352ff8bdSFrançois Tigeot int clock;
1398352ff8bdSFrançois Tigeot
13998621f407SFrançois Tigeot if (IS_PINEVIEW(dev_priv))
14008621f407SFrançois Tigeot clock = KHz(dev_priv->rawclk_freq);
1401352ff8bdSFrançois Tigeot else
1402a85cb24fSFrançois Tigeot clock = KHz(dev_priv->cdclk.hw.cdclk);
1403352ff8bdSFrançois Tigeot
14048621f407SFrançois Tigeot return DIV_ROUND_CLOSEST(clock, pwm_freq_hz * 32);
1405352ff8bdSFrançois Tigeot }
1406352ff8bdSFrançois Tigeot
1407352ff8bdSFrançois Tigeot /*
1408352ff8bdSFrançois Tigeot * Gen4: This value represents the period of the PWM stream in display core
1409aee94f86SFrançois Tigeot * clocks ([DevCTG] HRAW clocks) multiplied by 128.
1410aee94f86SFrançois Tigeot *
1411352ff8bdSFrançois Tigeot */
i965_hz_to_pwm(struct intel_connector * connector,u32 pwm_freq_hz)1412352ff8bdSFrançois Tigeot static u32 i965_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
1413352ff8bdSFrançois Tigeot {
1414a85cb24fSFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1415aee94f86SFrançois Tigeot int clock;
1416aee94f86SFrançois Tigeot
1417aee94f86SFrançois Tigeot if (IS_G4X(dev_priv))
14188621f407SFrançois Tigeot clock = KHz(dev_priv->rawclk_freq);
1419aee94f86SFrançois Tigeot else
1420a85cb24fSFrançois Tigeot clock = KHz(dev_priv->cdclk.hw.cdclk);
1421352ff8bdSFrançois Tigeot
14228621f407SFrançois Tigeot return DIV_ROUND_CLOSEST(clock, pwm_freq_hz * 128);
1423352ff8bdSFrançois Tigeot }
1424352ff8bdSFrançois Tigeot
1425352ff8bdSFrançois Tigeot /*
1426352ff8bdSFrançois Tigeot * VLV: This value represents the period of the PWM stream in display core
1427352ff8bdSFrançois Tigeot * clocks ([DevCTG] 200MHz HRAW clocks) multiplied by 128 or 25MHz S0IX clocks
1428352ff8bdSFrançois Tigeot * multiplied by 16. CHV uses a 19.2MHz S0IX clock.
1429352ff8bdSFrançois Tigeot */
vlv_hz_to_pwm(struct intel_connector * connector,u32 pwm_freq_hz)1430352ff8bdSFrançois Tigeot static u32 vlv_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
1431352ff8bdSFrançois Tigeot {
14328621f407SFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
14338621f407SFrançois Tigeot int mul, clock;
1434352ff8bdSFrançois Tigeot
1435352ff8bdSFrançois Tigeot if ((I915_READ(CBR1_VLV) & CBR_PWM_CLOCK_MUX_SELECT) == 0) {
14368621f407SFrançois Tigeot if (IS_CHERRYVIEW(dev_priv))
14378621f407SFrançois Tigeot clock = KHz(19200);
1438352ff8bdSFrançois Tigeot else
14398621f407SFrançois Tigeot clock = MHz(25);
14408621f407SFrançois Tigeot mul = 16;
1441352ff8bdSFrançois Tigeot } else {
14428621f407SFrançois Tigeot clock = KHz(dev_priv->rawclk_freq);
14438621f407SFrançois Tigeot mul = 128;
1444352ff8bdSFrançois Tigeot }
14458621f407SFrançois Tigeot
14468621f407SFrançois Tigeot return DIV_ROUND_CLOSEST(clock, pwm_freq_hz * mul);
1447352ff8bdSFrançois Tigeot }
1448352ff8bdSFrançois Tigeot
get_backlight_max_vbt(struct intel_connector * connector)1449352ff8bdSFrançois Tigeot static u32 get_backlight_max_vbt(struct intel_connector *connector)
1450352ff8bdSFrançois Tigeot {
1451aee94f86SFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1452352ff8bdSFrançois Tigeot struct intel_panel *panel = &connector->panel;
1453352ff8bdSFrançois Tigeot u16 pwm_freq_hz = dev_priv->vbt.backlight.pwm_freq_hz;
1454352ff8bdSFrançois Tigeot u32 pwm;
1455352ff8bdSFrançois Tigeot
1456aee94f86SFrançois Tigeot if (!panel->backlight.hz_to_pwm) {
1457aee94f86SFrançois Tigeot DRM_DEBUG_KMS("backlight frequency conversion not supported\n");
1458352ff8bdSFrançois Tigeot return 0;
1459352ff8bdSFrançois Tigeot }
1460352ff8bdSFrançois Tigeot
1461aee94f86SFrançois Tigeot if (pwm_freq_hz) {
1462aee94f86SFrançois Tigeot DRM_DEBUG_KMS("VBT defined backlight frequency %u Hz\n",
1463aee94f86SFrançois Tigeot pwm_freq_hz);
1464aee94f86SFrançois Tigeot } else {
1465aee94f86SFrançois Tigeot pwm_freq_hz = 200;
1466aee94f86SFrançois Tigeot DRM_DEBUG_KMS("default backlight frequency %u Hz\n",
1467aee94f86SFrançois Tigeot pwm_freq_hz);
1468352ff8bdSFrançois Tigeot }
1469352ff8bdSFrançois Tigeot
1470352ff8bdSFrançois Tigeot pwm = panel->backlight.hz_to_pwm(connector, pwm_freq_hz);
1471352ff8bdSFrançois Tigeot if (!pwm) {
1472352ff8bdSFrançois Tigeot DRM_DEBUG_KMS("backlight frequency conversion failed\n");
1473352ff8bdSFrançois Tigeot return 0;
1474352ff8bdSFrançois Tigeot }
1475352ff8bdSFrançois Tigeot
1476352ff8bdSFrançois Tigeot return pwm;
1477352ff8bdSFrançois Tigeot }
1478352ff8bdSFrançois Tigeot
1479352ff8bdSFrançois Tigeot /*
1480352ff8bdSFrançois Tigeot * Note: The setup hooks can't assume pipe is set!
14819edbd4a0SFrançois Tigeot */
get_backlight_min_vbt(struct intel_connector * connector)148224edb884SFrançois Tigeot static u32 get_backlight_min_vbt(struct intel_connector *connector)
148324edb884SFrançois Tigeot {
1484aee94f86SFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
148524edb884SFrançois Tigeot struct intel_panel *panel = &connector->panel;
14861b13d190SFrançois Tigeot int min;
148724edb884SFrançois Tigeot
148824edb884SFrançois Tigeot WARN_ON(panel->backlight.max == 0);
148924edb884SFrançois Tigeot
14901b13d190SFrançois Tigeot /*
14911b13d190SFrançois Tigeot * XXX: If the vbt value is 255, it makes min equal to max, which leads
14921b13d190SFrançois Tigeot * to problems. There are such machines out there. Either our
14931b13d190SFrançois Tigeot * interpretation is wrong or the vbt has bogus data. Or both. Safeguard
14941b13d190SFrançois Tigeot * against this by letting the minimum be at most (arbitrarily chosen)
14951b13d190SFrançois Tigeot * 25% of the max.
14961b13d190SFrançois Tigeot */
14971b13d190SFrançois Tigeot min = clamp_t(int, dev_priv->vbt.backlight.min_brightness, 0, 64);
14981b13d190SFrançois Tigeot if (min != dev_priv->vbt.backlight.min_brightness) {
14991b13d190SFrançois Tigeot DRM_DEBUG_KMS("clamping VBT min backlight %d/255 to %d/255\n",
15001b13d190SFrançois Tigeot dev_priv->vbt.backlight.min_brightness, min);
15011b13d190SFrançois Tigeot }
15021b13d190SFrançois Tigeot
150324edb884SFrançois Tigeot /* vbt value is a coefficient in range [0..255] */
15041b13d190SFrançois Tigeot return scale(min, 0, 255, 0, panel->backlight.max);
150524edb884SFrançois Tigeot }
150624edb884SFrançois Tigeot
lpt_setup_backlight(struct intel_connector * connector,enum i915_pipe unused)1507352ff8bdSFrançois Tigeot static int lpt_setup_backlight(struct intel_connector *connector, enum i915_pipe unused)
15089edbd4a0SFrançois Tigeot {
1509aee94f86SFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
15109edbd4a0SFrançois Tigeot struct intel_panel *panel = &connector->panel;
15119edbd4a0SFrançois Tigeot u32 pch_ctl1, pch_ctl2, val;
1512bf017597SFrançois Tigeot bool alt;
1513bf017597SFrançois Tigeot
1514bf017597SFrançois Tigeot if (HAS_PCH_LPT(dev_priv))
1515bf017597SFrançois Tigeot alt = I915_READ(SOUTH_CHICKEN2) & LPT_PWM_GRANULARITY;
1516bf017597SFrançois Tigeot else
1517bf017597SFrançois Tigeot alt = I915_READ(SOUTH_CHICKEN1) & SPT_PWM_GRANULARITY;
1518bf017597SFrançois Tigeot panel->backlight.alternate_pwm_increment = alt;
15199edbd4a0SFrançois Tigeot
15209edbd4a0SFrançois Tigeot pch_ctl1 = I915_READ(BLC_PWM_PCH_CTL1);
15219edbd4a0SFrançois Tigeot panel->backlight.active_low_pwm = pch_ctl1 & BLM_PCH_POLARITY;
15229edbd4a0SFrançois Tigeot
15239edbd4a0SFrançois Tigeot pch_ctl2 = I915_READ(BLC_PWM_PCH_CTL2);
15249edbd4a0SFrançois Tigeot panel->backlight.max = pch_ctl2 >> 16;
1525352ff8bdSFrançois Tigeot
1526352ff8bdSFrançois Tigeot if (!panel->backlight.max)
1527352ff8bdSFrançois Tigeot panel->backlight.max = get_backlight_max_vbt(connector);
1528352ff8bdSFrançois Tigeot
15299edbd4a0SFrançois Tigeot if (!panel->backlight.max)
15309edbd4a0SFrançois Tigeot return -ENODEV;
15319edbd4a0SFrançois Tigeot
153224edb884SFrançois Tigeot panel->backlight.min = get_backlight_min_vbt(connector);
153324edb884SFrançois Tigeot
1534352ff8bdSFrançois Tigeot val = lpt_get_backlight(connector);
15351e12ee3bSFrançois Tigeot val = intel_panel_compute_brightness(connector, val);
15361e12ee3bSFrançois Tigeot panel->backlight.level = clamp(val, panel->backlight.min,
15371e12ee3bSFrançois Tigeot panel->backlight.max);
15389edbd4a0SFrançois Tigeot
15391e12ee3bSFrançois Tigeot panel->backlight.enabled = pch_ctl1 & BLM_PCH_PWM_ENABLE;
15409edbd4a0SFrançois Tigeot
15419edbd4a0SFrançois Tigeot return 0;
154219df918dSFrançois Tigeot }
15439edbd4a0SFrançois Tigeot
pch_setup_backlight(struct intel_connector * connector,enum i915_pipe unused)15442c9916cdSFrançois Tigeot static int pch_setup_backlight(struct intel_connector *connector, enum i915_pipe unused)
15459edbd4a0SFrançois Tigeot {
1546aee94f86SFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
15479edbd4a0SFrançois Tigeot struct intel_panel *panel = &connector->panel;
15489edbd4a0SFrançois Tigeot u32 cpu_ctl2, pch_ctl1, pch_ctl2, val;
15499edbd4a0SFrançois Tigeot
15509edbd4a0SFrançois Tigeot pch_ctl1 = I915_READ(BLC_PWM_PCH_CTL1);
15519edbd4a0SFrançois Tigeot panel->backlight.active_low_pwm = pch_ctl1 & BLM_PCH_POLARITY;
15529edbd4a0SFrançois Tigeot
15539edbd4a0SFrançois Tigeot pch_ctl2 = I915_READ(BLC_PWM_PCH_CTL2);
15549edbd4a0SFrançois Tigeot panel->backlight.max = pch_ctl2 >> 16;
1555352ff8bdSFrançois Tigeot
1556352ff8bdSFrançois Tigeot if (!panel->backlight.max)
1557352ff8bdSFrançois Tigeot panel->backlight.max = get_backlight_max_vbt(connector);
1558352ff8bdSFrançois Tigeot
15599edbd4a0SFrançois Tigeot if (!panel->backlight.max)
15609edbd4a0SFrançois Tigeot return -ENODEV;
15619edbd4a0SFrançois Tigeot
156224edb884SFrançois Tigeot panel->backlight.min = get_backlight_min_vbt(connector);
156324edb884SFrançois Tigeot
15649edbd4a0SFrançois Tigeot val = pch_get_backlight(connector);
15651e12ee3bSFrançois Tigeot val = intel_panel_compute_brightness(connector, val);
15661e12ee3bSFrançois Tigeot panel->backlight.level = clamp(val, panel->backlight.min,
15671e12ee3bSFrançois Tigeot panel->backlight.max);
15689edbd4a0SFrançois Tigeot
15699edbd4a0SFrançois Tigeot cpu_ctl2 = I915_READ(BLC_PWM_CPU_CTL2);
15709edbd4a0SFrançois Tigeot panel->backlight.enabled = (cpu_ctl2 & BLM_PWM_ENABLE) &&
15711e12ee3bSFrançois Tigeot (pch_ctl1 & BLM_PCH_PWM_ENABLE);
15729edbd4a0SFrançois Tigeot
15739edbd4a0SFrançois Tigeot return 0;
157419df918dSFrançois Tigeot }
15759edbd4a0SFrançois Tigeot
i9xx_setup_backlight(struct intel_connector * connector,enum i915_pipe unused)15762c9916cdSFrançois Tigeot static int i9xx_setup_backlight(struct intel_connector *connector, enum i915_pipe unused)
15779edbd4a0SFrançois Tigeot {
1578aee94f86SFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
15799edbd4a0SFrançois Tigeot struct intel_panel *panel = &connector->panel;
15809edbd4a0SFrançois Tigeot u32 ctl, val;
15819edbd4a0SFrançois Tigeot
15829edbd4a0SFrançois Tigeot ctl = I915_READ(BLC_PWM_CTL);
15839edbd4a0SFrançois Tigeot
1584aee94f86SFrançois Tigeot if (IS_GEN2(dev_priv) || IS_I915GM(dev_priv) || IS_I945GM(dev_priv))
15859edbd4a0SFrançois Tigeot panel->backlight.combination_mode = ctl & BLM_LEGACY_MODE;
15869edbd4a0SFrançois Tigeot
1587aee94f86SFrançois Tigeot if (IS_PINEVIEW(dev_priv))
15889edbd4a0SFrançois Tigeot panel->backlight.active_low_pwm = ctl & BLM_POLARITY_PNV;
15899edbd4a0SFrançois Tigeot
15909edbd4a0SFrançois Tigeot panel->backlight.max = ctl >> 17;
1591352ff8bdSFrançois Tigeot
1592352ff8bdSFrançois Tigeot if (!panel->backlight.max) {
1593352ff8bdSFrançois Tigeot panel->backlight.max = get_backlight_max_vbt(connector);
1594352ff8bdSFrançois Tigeot panel->backlight.max >>= 1;
1595352ff8bdSFrançois Tigeot }
15969edbd4a0SFrançois Tigeot
15979edbd4a0SFrançois Tigeot if (!panel->backlight.max)
15989edbd4a0SFrançois Tigeot return -ENODEV;
15999edbd4a0SFrançois Tigeot
1600352ff8bdSFrançois Tigeot if (panel->backlight.combination_mode)
1601352ff8bdSFrançois Tigeot panel->backlight.max *= 0xff;
1602352ff8bdSFrançois Tigeot
160324edb884SFrançois Tigeot panel->backlight.min = get_backlight_min_vbt(connector);
160424edb884SFrançois Tigeot
16059edbd4a0SFrançois Tigeot val = i9xx_get_backlight(connector);
16061e12ee3bSFrançois Tigeot val = intel_panel_compute_brightness(connector, val);
16071e12ee3bSFrançois Tigeot panel->backlight.level = clamp(val, panel->backlight.min,
16081e12ee3bSFrançois Tigeot panel->backlight.max);
16099edbd4a0SFrançois Tigeot
16101e12ee3bSFrançois Tigeot panel->backlight.enabled = val != 0;
16119edbd4a0SFrançois Tigeot
16129edbd4a0SFrançois Tigeot return 0;
16139edbd4a0SFrançois Tigeot }
16149edbd4a0SFrançois Tigeot
i965_setup_backlight(struct intel_connector * connector,enum i915_pipe unused)16152c9916cdSFrançois Tigeot static int i965_setup_backlight(struct intel_connector *connector, enum i915_pipe unused)
16169edbd4a0SFrançois Tigeot {
1617aee94f86SFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
16189edbd4a0SFrançois Tigeot struct intel_panel *panel = &connector->panel;
16199edbd4a0SFrançois Tigeot u32 ctl, ctl2, val;
16209edbd4a0SFrançois Tigeot
16219edbd4a0SFrançois Tigeot ctl2 = I915_READ(BLC_PWM_CTL2);
16229edbd4a0SFrançois Tigeot panel->backlight.combination_mode = ctl2 & BLM_COMBINATION_MODE;
16239edbd4a0SFrançois Tigeot panel->backlight.active_low_pwm = ctl2 & BLM_POLARITY_I965;
16249edbd4a0SFrançois Tigeot
16259edbd4a0SFrançois Tigeot ctl = I915_READ(BLC_PWM_CTL);
16269edbd4a0SFrançois Tigeot panel->backlight.max = ctl >> 16;
1627352ff8bdSFrançois Tigeot
1628352ff8bdSFrançois Tigeot if (!panel->backlight.max)
1629352ff8bdSFrançois Tigeot panel->backlight.max = get_backlight_max_vbt(connector);
16309edbd4a0SFrançois Tigeot
16319edbd4a0SFrançois Tigeot if (!panel->backlight.max)
16329edbd4a0SFrançois Tigeot return -ENODEV;
16339edbd4a0SFrançois Tigeot
1634352ff8bdSFrançois Tigeot if (panel->backlight.combination_mode)
1635352ff8bdSFrançois Tigeot panel->backlight.max *= 0xff;
1636352ff8bdSFrançois Tigeot
163724edb884SFrançois Tigeot panel->backlight.min = get_backlight_min_vbt(connector);
163824edb884SFrançois Tigeot
16399edbd4a0SFrançois Tigeot val = i9xx_get_backlight(connector);
16401e12ee3bSFrançois Tigeot val = intel_panel_compute_brightness(connector, val);
16411e12ee3bSFrançois Tigeot panel->backlight.level = clamp(val, panel->backlight.min,
16421e12ee3bSFrançois Tigeot panel->backlight.max);
16439edbd4a0SFrançois Tigeot
16441e12ee3bSFrançois Tigeot panel->backlight.enabled = ctl2 & BLM_PWM_ENABLE;
16459edbd4a0SFrançois Tigeot
16469edbd4a0SFrançois Tigeot return 0;
16479edbd4a0SFrançois Tigeot }
16489edbd4a0SFrançois Tigeot
vlv_setup_backlight(struct intel_connector * connector,enum i915_pipe pipe)16492c9916cdSFrançois Tigeot static int vlv_setup_backlight(struct intel_connector *connector, enum i915_pipe pipe)
16509edbd4a0SFrançois Tigeot {
1651aee94f86SFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
16529edbd4a0SFrançois Tigeot struct intel_panel *panel = &connector->panel;
16539edbd4a0SFrançois Tigeot u32 ctl, ctl2, val;
16549edbd4a0SFrançois Tigeot
16552c9916cdSFrançois Tigeot if (WARN_ON(pipe != PIPE_A && pipe != PIPE_B))
16562c9916cdSFrançois Tigeot return -ENODEV;
16572c9916cdSFrançois Tigeot
16582c9916cdSFrançois Tigeot ctl2 = I915_READ(VLV_BLC_PWM_CTL2(pipe));
16599edbd4a0SFrançois Tigeot panel->backlight.active_low_pwm = ctl2 & BLM_POLARITY_I965;
16609edbd4a0SFrançois Tigeot
16612c9916cdSFrançois Tigeot ctl = I915_READ(VLV_BLC_PWM_CTL(pipe));
16629edbd4a0SFrançois Tigeot panel->backlight.max = ctl >> 16;
1663352ff8bdSFrançois Tigeot
1664352ff8bdSFrançois Tigeot if (!panel->backlight.max)
1665352ff8bdSFrançois Tigeot panel->backlight.max = get_backlight_max_vbt(connector);
1666352ff8bdSFrançois Tigeot
16679edbd4a0SFrançois Tigeot if (!panel->backlight.max)
16689edbd4a0SFrançois Tigeot return -ENODEV;
16699edbd4a0SFrançois Tigeot
167024edb884SFrançois Tigeot panel->backlight.min = get_backlight_min_vbt(connector);
167124edb884SFrançois Tigeot
1672aee94f86SFrançois Tigeot val = _vlv_get_backlight(dev_priv, pipe);
16731e12ee3bSFrançois Tigeot val = intel_panel_compute_brightness(connector, val);
16741e12ee3bSFrançois Tigeot panel->backlight.level = clamp(val, panel->backlight.min,
16751e12ee3bSFrançois Tigeot panel->backlight.max);
16769edbd4a0SFrançois Tigeot
16771e12ee3bSFrançois Tigeot panel->backlight.enabled = ctl2 & BLM_PWM_ENABLE;
16789edbd4a0SFrançois Tigeot
16799edbd4a0SFrançois Tigeot return 0;
16809edbd4a0SFrançois Tigeot }
1681f557acc5SMatthew Dillon
168219c468b4SFrançois Tigeot static int
bxt_setup_backlight(struct intel_connector * connector,enum i915_pipe unused)168319c468b4SFrançois Tigeot bxt_setup_backlight(struct intel_connector *connector, enum i915_pipe unused)
168419c468b4SFrançois Tigeot {
1685aee94f86SFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
168619c468b4SFrançois Tigeot struct intel_panel *panel = &connector->panel;
168719c468b4SFrançois Tigeot u32 pwm_ctl, val;
168819c468b4SFrançois Tigeot
1689a85cb24fSFrançois Tigeot panel->backlight.controller = dev_priv->vbt.backlight.controller;
169019c468b4SFrançois Tigeot
1691352ff8bdSFrançois Tigeot pwm_ctl = I915_READ(BXT_BLC_PWM_CTL(panel->backlight.controller));
1692352ff8bdSFrançois Tigeot
1693a85cb24fSFrançois Tigeot /* Controller 1 uses the utility pin. */
1694352ff8bdSFrançois Tigeot if (panel->backlight.controller == 1) {
1695352ff8bdSFrançois Tigeot val = I915_READ(UTIL_PIN_CTL);
1696352ff8bdSFrançois Tigeot panel->backlight.util_pin_active_low =
1697352ff8bdSFrançois Tigeot val & UTIL_PIN_POLARITY;
1698352ff8bdSFrançois Tigeot }
1699352ff8bdSFrançois Tigeot
1700352ff8bdSFrançois Tigeot panel->backlight.active_low_pwm = pwm_ctl & BXT_BLC_PWM_POLARITY;
1701352ff8bdSFrançois Tigeot panel->backlight.max =
1702352ff8bdSFrançois Tigeot I915_READ(BXT_BLC_PWM_FREQ(panel->backlight.controller));
1703352ff8bdSFrançois Tigeot
1704352ff8bdSFrançois Tigeot if (!panel->backlight.max)
1705352ff8bdSFrançois Tigeot panel->backlight.max = get_backlight_max_vbt(connector);
1706352ff8bdSFrançois Tigeot
170719c468b4SFrançois Tigeot if (!panel->backlight.max)
170819c468b4SFrançois Tigeot return -ENODEV;
170919c468b4SFrançois Tigeot
1710*3f2dd94aSFrançois Tigeot panel->backlight.min = get_backlight_min_vbt(connector);
1711*3f2dd94aSFrançois Tigeot
1712*3f2dd94aSFrançois Tigeot panel->backlight.min = get_backlight_min_vbt(connector);
1713*3f2dd94aSFrançois Tigeot
1714*3f2dd94aSFrançois Tigeot val = bxt_get_backlight(connector);
1715*3f2dd94aSFrançois Tigeot val = intel_panel_compute_brightness(connector, val);
1716*3f2dd94aSFrançois Tigeot panel->backlight.level = clamp(val, panel->backlight.min,
1717*3f2dd94aSFrançois Tigeot panel->backlight.max);
1718*3f2dd94aSFrançois Tigeot
1719*3f2dd94aSFrançois Tigeot panel->backlight.enabled = pwm_ctl & BXT_BLC_PWM_ENABLE;
1720*3f2dd94aSFrançois Tigeot
1721*3f2dd94aSFrançois Tigeot return 0;
1722*3f2dd94aSFrançois Tigeot }
1723*3f2dd94aSFrançois Tigeot
1724*3f2dd94aSFrançois Tigeot static int
cnp_setup_backlight(struct intel_connector * connector,enum i915_pipe unused)1725*3f2dd94aSFrançois Tigeot cnp_setup_backlight(struct intel_connector *connector, enum i915_pipe unused)
1726*3f2dd94aSFrançois Tigeot {
1727*3f2dd94aSFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1728*3f2dd94aSFrançois Tigeot struct intel_panel *panel = &connector->panel;
1729*3f2dd94aSFrançois Tigeot u32 pwm_ctl, val;
1730*3f2dd94aSFrançois Tigeot
1731*3f2dd94aSFrançois Tigeot /*
1732*3f2dd94aSFrançois Tigeot * CNP has the BXT implementation of backlight, but with only
1733*3f2dd94aSFrançois Tigeot * one controller. Future platforms could have multiple controllers
1734*3f2dd94aSFrançois Tigeot * so let's make this extensible and prepared for the future.
1735*3f2dd94aSFrançois Tigeot */
1736*3f2dd94aSFrançois Tigeot panel->backlight.controller = 0;
1737*3f2dd94aSFrançois Tigeot
1738*3f2dd94aSFrançois Tigeot pwm_ctl = I915_READ(BXT_BLC_PWM_CTL(panel->backlight.controller));
1739*3f2dd94aSFrançois Tigeot
1740*3f2dd94aSFrançois Tigeot panel->backlight.active_low_pwm = pwm_ctl & BXT_BLC_PWM_POLARITY;
1741*3f2dd94aSFrançois Tigeot panel->backlight.max =
1742*3f2dd94aSFrançois Tigeot I915_READ(BXT_BLC_PWM_FREQ(panel->backlight.controller));
1743*3f2dd94aSFrançois Tigeot
1744*3f2dd94aSFrançois Tigeot if (!panel->backlight.max)
1745*3f2dd94aSFrançois Tigeot panel->backlight.max = get_backlight_max_vbt(connector);
1746*3f2dd94aSFrançois Tigeot
1747*3f2dd94aSFrançois Tigeot if (!panel->backlight.max)
1748*3f2dd94aSFrançois Tigeot return -ENODEV;
1749*3f2dd94aSFrançois Tigeot
1750*3f2dd94aSFrançois Tigeot panel->backlight.min = get_backlight_min_vbt(connector);
1751*3f2dd94aSFrançois Tigeot
1752*3f2dd94aSFrançois Tigeot panel->backlight.min = get_backlight_min_vbt(connector);
1753*3f2dd94aSFrançois Tigeot
175419c468b4SFrançois Tigeot val = bxt_get_backlight(connector);
17551e12ee3bSFrançois Tigeot val = intel_panel_compute_brightness(connector, val);
17561e12ee3bSFrançois Tigeot panel->backlight.level = clamp(val, panel->backlight.min,
17571e12ee3bSFrançois Tigeot panel->backlight.max);
175819c468b4SFrançois Tigeot
17591e12ee3bSFrançois Tigeot panel->backlight.enabled = pwm_ctl & BXT_BLC_PWM_ENABLE;
176019c468b4SFrançois Tigeot
176119c468b4SFrançois Tigeot return 0;
176219c468b4SFrançois Tigeot }
176319c468b4SFrançois Tigeot
pwm_setup_backlight(struct intel_connector * connector,enum i915_pipe pipe)1764a05eeebfSFrançois Tigeot static int pwm_setup_backlight(struct intel_connector *connector,
1765a05eeebfSFrançois Tigeot enum i915_pipe pipe)
1766a05eeebfSFrançois Tigeot {
1767a05eeebfSFrançois Tigeot struct drm_device *dev = connector->base.dev;
1768a05eeebfSFrançois Tigeot struct intel_panel *panel = &connector->panel;
1769a05eeebfSFrançois Tigeot int retval;
1770a05eeebfSFrançois Tigeot
1771a05eeebfSFrançois Tigeot /* Get the PWM chip for backlight control */
1772a05eeebfSFrançois Tigeot panel->backlight.pwm = pwm_get(dev->dev, "pwm_backlight");
1773a05eeebfSFrançois Tigeot if (IS_ERR(panel->backlight.pwm)) {
1774a05eeebfSFrançois Tigeot DRM_ERROR("Failed to own the pwm chip\n");
1775a05eeebfSFrançois Tigeot panel->backlight.pwm = NULL;
1776a05eeebfSFrançois Tigeot return -ENODEV;
1777a05eeebfSFrançois Tigeot }
1778a05eeebfSFrançois Tigeot
17798621f407SFrançois Tigeot /*
17808621f407SFrançois Tigeot * FIXME: pwm_apply_args() should be removed when switching to
17818621f407SFrançois Tigeot * the atomic PWM API.
17828621f407SFrançois Tigeot */
17838621f407SFrançois Tigeot #if 0
17848621f407SFrançois Tigeot pwm_apply_args(panel->backlight.pwm);
17858621f407SFrançois Tigeot #endif
17868621f407SFrançois Tigeot
1787a05eeebfSFrançois Tigeot retval = pwm_config(panel->backlight.pwm, CRC_PMIC_PWM_PERIOD_NS,
1788a05eeebfSFrançois Tigeot CRC_PMIC_PWM_PERIOD_NS);
1789a05eeebfSFrançois Tigeot if (retval < 0) {
1790a05eeebfSFrançois Tigeot DRM_ERROR("Failed to configure the pwm chip\n");
1791a05eeebfSFrançois Tigeot pwm_put(panel->backlight.pwm);
1792a05eeebfSFrançois Tigeot panel->backlight.pwm = NULL;
1793a05eeebfSFrançois Tigeot return retval;
1794a05eeebfSFrançois Tigeot }
1795a05eeebfSFrançois Tigeot
1796a05eeebfSFrançois Tigeot panel->backlight.min = 0; /* 0% */
1797a05eeebfSFrançois Tigeot panel->backlight.max = 100; /* 100% */
1798a05eeebfSFrançois Tigeot panel->backlight.level = DIV_ROUND_UP(
1799a05eeebfSFrançois Tigeot pwm_get_duty_cycle(panel->backlight.pwm) * 100,
1800a05eeebfSFrançois Tigeot CRC_PMIC_PWM_PERIOD_NS);
1801a05eeebfSFrançois Tigeot panel->backlight.enabled = panel->backlight.level != 0;
1802a05eeebfSFrançois Tigeot
1803a05eeebfSFrançois Tigeot return 0;
1804a05eeebfSFrançois Tigeot }
1805a05eeebfSFrançois Tigeot
1806bf017597SFrançois Tigeot #ifdef __DragonFly__
1807bf017597SFrançois Tigeot /*
1808bf017597SFrançois Tigeot * Read max backlight level
1809bf017597SFrançois Tigeot */
1810bf017597SFrançois Tigeot static int
sysctl_backlight_max(SYSCTL_HANDLER_ARGS)1811bf017597SFrançois Tigeot sysctl_backlight_max(SYSCTL_HANDLER_ARGS)
1812bf017597SFrançois Tigeot {
1813bf017597SFrançois Tigeot int err, val;
1814bf017597SFrançois Tigeot struct intel_connector *connector = arg1;
1815bf017597SFrançois Tigeot struct drm_device *dev = connector->base.dev;
1816bf017597SFrançois Tigeot struct drm_i915_private *dev_priv = dev->dev_private;
1817bf017597SFrançois Tigeot struct intel_panel *panel = &connector->panel;
1818bf017597SFrançois Tigeot
1819bf017597SFrançois Tigeot mutex_lock(&dev_priv->backlight_lock);
1820bf017597SFrançois Tigeot val = panel->backlight.max;
1821bf017597SFrançois Tigeot mutex_unlock(&dev_priv->backlight_lock);
1822bf017597SFrançois Tigeot
1823bf017597SFrançois Tigeot err = sysctl_handle_int(oidp, &val, 0, req);
1824bf017597SFrançois Tigeot return(err);
1825bf017597SFrançois Tigeot }
1826bf017597SFrançois Tigeot
1827bf017597SFrançois Tigeot /*
1828bf017597SFrançois Tigeot * Read/write backlight level
1829bf017597SFrançois Tigeot */
1830bf017597SFrançois Tigeot static int
sysctl_backlight_handler(SYSCTL_HANDLER_ARGS)1831bf017597SFrançois Tigeot sysctl_backlight_handler(SYSCTL_HANDLER_ARGS)
1832bf017597SFrançois Tigeot {
1833bf017597SFrançois Tigeot struct intel_connector *connector = arg1;
1834*3f2dd94aSFrançois Tigeot const struct drm_connector_state *conn_state = connector->base.state;
1835bf017597SFrançois Tigeot struct drm_device *dev = connector->base.dev;
1836bf017597SFrançois Tigeot struct drm_i915_private *dev_priv = dev->dev_private;
1837bf017597SFrançois Tigeot struct intel_panel *panel = &connector->panel;
1838bf017597SFrançois Tigeot int err, val;
1839bf017597SFrançois Tigeot u32 user_level, max_brightness;
1840bf017597SFrançois Tigeot
1841bf017597SFrançois Tigeot mutex_lock(&dev_priv->backlight_lock);
1842bf017597SFrançois Tigeot max_brightness = panel->backlight.max;
1843bf017597SFrançois Tigeot user_level = scale_hw_to_user(connector, panel->backlight.level,
1844bf017597SFrançois Tigeot max_brightness);
1845bf017597SFrançois Tigeot mutex_unlock(&dev_priv->backlight_lock);
1846bf017597SFrançois Tigeot
1847bf017597SFrançois Tigeot val = user_level;
1848bf017597SFrançois Tigeot err = sysctl_handle_int(oidp, &val, 0, req);
1849bf017597SFrançois Tigeot if (err != 0 || req->newptr == NULL) {
1850bf017597SFrançois Tigeot return(err);
1851bf017597SFrançois Tigeot }
1852bf017597SFrançois Tigeot
1853bf017597SFrançois Tigeot if (val != user_level && val >= 0 && val <= max_brightness) {
1854bf017597SFrançois Tigeot drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1855*3f2dd94aSFrançois Tigeot intel_panel_set_backlight(conn_state, val, max_brightness);
1856bf017597SFrançois Tigeot drm_modeset_unlock(&dev->mode_config.connection_mutex);
1857bf017597SFrançois Tigeot }
1858bf017597SFrançois Tigeot
1859bf017597SFrançois Tigeot return(err);
1860bf017597SFrançois Tigeot }
1861bf017597SFrançois Tigeot #endif /* __DragonFly__ */
1862bf017597SFrançois Tigeot
intel_panel_setup_backlight(struct drm_connector * connector,enum i915_pipe pipe)18632c9916cdSFrançois Tigeot int intel_panel_setup_backlight(struct drm_connector *connector, enum i915_pipe pipe)
186419df918dSFrançois Tigeot {
1865aee94f86SFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(connector->dev);
18669edbd4a0SFrançois Tigeot struct intel_connector *intel_connector = to_intel_connector(connector);
18679edbd4a0SFrançois Tigeot struct intel_panel *panel = &intel_connector->panel;
18689edbd4a0SFrançois Tigeot int ret;
18699edbd4a0SFrançois Tigeot
1870ba55f2f5SFrançois Tigeot if (!dev_priv->vbt.backlight.present) {
1871ba55f2f5SFrançois Tigeot if (dev_priv->quirks & QUIRK_BACKLIGHT_PRESENT) {
1872ba55f2f5SFrançois Tigeot DRM_DEBUG_KMS("no backlight present per VBT, but present per quirk\n");
1873ba55f2f5SFrançois Tigeot } else {
1874ba55f2f5SFrançois Tigeot DRM_DEBUG_KMS("no backlight present per VBT\n");
1875ba55f2f5SFrançois Tigeot return 0;
1876ba55f2f5SFrançois Tigeot }
1877ba55f2f5SFrançois Tigeot }
1878ba55f2f5SFrançois Tigeot
1879352ff8bdSFrançois Tigeot /* ensure intel_panel has been initialized first */
1880352ff8bdSFrançois Tigeot if (WARN_ON(!panel->backlight.setup))
1881352ff8bdSFrançois Tigeot return -ENODEV;
1882352ff8bdSFrançois Tigeot
18839edbd4a0SFrançois Tigeot /* set level and max in panel struct */
18842c9916cdSFrançois Tigeot mutex_lock(&dev_priv->backlight_lock);
1885352ff8bdSFrançois Tigeot ret = panel->backlight.setup(intel_connector, pipe);
18862c9916cdSFrançois Tigeot mutex_unlock(&dev_priv->backlight_lock);
18879edbd4a0SFrançois Tigeot
18889edbd4a0SFrançois Tigeot if (ret) {
18899edbd4a0SFrançois Tigeot DRM_DEBUG_KMS("failed to setup backlight for connector %s\n",
1890b4efbf42Szrj connector->name);
18919edbd4a0SFrançois Tigeot return ret;
18929edbd4a0SFrançois Tigeot }
18939edbd4a0SFrançois Tigeot
18949edbd4a0SFrançois Tigeot panel->backlight.present = true;
1895f557acc5SMatthew Dillon
1896310880c6SSascha Wildner #ifdef __DragonFly__
1897f557acc5SMatthew Dillon SYSCTL_ADD_PROC(&connector->dev->sysctl->ctx, &sysctl__hw_children,
1898f557acc5SMatthew Dillon OID_AUTO, "backlight_max",
1899f557acc5SMatthew Dillon CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_ANYBODY,
19009edbd4a0SFrançois Tigeot connector, sizeof(int),
1901f557acc5SMatthew Dillon sysctl_backlight_max,
1902f557acc5SMatthew Dillon "I", "Max backlight level");
1903f557acc5SMatthew Dillon SYSCTL_ADD_PROC(&connector->dev->sysctl->ctx, &sysctl__hw_children,
1904f557acc5SMatthew Dillon OID_AUTO, "backlight_level",
1905f557acc5SMatthew Dillon CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY,
19069edbd4a0SFrançois Tigeot connector, sizeof(int),
1907f557acc5SMatthew Dillon sysctl_backlight_handler,
1908f557acc5SMatthew Dillon "I", "Backlight level");
1909310880c6SSascha Wildner #endif
1910ba55f2f5SFrançois Tigeot
19112c9916cdSFrançois Tigeot DRM_DEBUG_KMS("Connector %s backlight initialized, %s, brightness %u/%u\n",
19122c9916cdSFrançois Tigeot connector->name,
19134be47400SFrançois Tigeot enableddisabled(panel->backlight.enabled),
19142c9916cdSFrançois Tigeot panel->backlight.level, panel->backlight.max);
19159edbd4a0SFrançois Tigeot
1916e3adcf8fSFrançois Tigeot return 0;
1917e3adcf8fSFrançois Tigeot }
1918e3adcf8fSFrançois Tigeot
intel_panel_destroy_backlight(struct drm_connector * connector)19199edbd4a0SFrançois Tigeot void intel_panel_destroy_backlight(struct drm_connector *connector)
1920e3adcf8fSFrançois Tigeot {
19219edbd4a0SFrançois Tigeot struct intel_connector *intel_connector = to_intel_connector(connector);
19229edbd4a0SFrançois Tigeot struct intel_panel *panel = &intel_connector->panel;
19239edbd4a0SFrançois Tigeot
1924a05eeebfSFrançois Tigeot /* dispose of the pwm */
1925a05eeebfSFrançois Tigeot if (panel->backlight.pwm)
1926a05eeebfSFrançois Tigeot pwm_put(panel->backlight.pwm);
1927a05eeebfSFrançois Tigeot
19289edbd4a0SFrançois Tigeot panel->backlight.present = false;
1929e3adcf8fSFrançois Tigeot }
19309edbd4a0SFrançois Tigeot
19319edbd4a0SFrançois Tigeot /* Set up chip specific backlight functions */
1932352ff8bdSFrançois Tigeot static void
intel_panel_init_backlight_funcs(struct intel_panel * panel)1933352ff8bdSFrançois Tigeot intel_panel_init_backlight_funcs(struct intel_panel *panel)
19349edbd4a0SFrançois Tigeot {
1935aee94f86SFrançois Tigeot struct intel_connector *connector =
1936352ff8bdSFrançois Tigeot container_of(panel, struct intel_connector, panel);
1937aee94f86SFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
19389edbd4a0SFrançois Tigeot
19391487f786SFrançois Tigeot if (connector->base.connector_type == DRM_MODE_CONNECTOR_eDP &&
19401487f786SFrançois Tigeot intel_dp_aux_init_backlight_funcs(connector) == 0)
19411487f786SFrançois Tigeot return;
19421487f786SFrançois Tigeot
19431487f786SFrançois Tigeot if (connector->base.connector_type == DRM_MODE_CONNECTOR_DSI &&
19441487f786SFrançois Tigeot intel_dsi_dcs_init_backlight_funcs(connector) == 0)
19451487f786SFrançois Tigeot return;
19461487f786SFrançois Tigeot
1947a85cb24fSFrançois Tigeot if (IS_GEN9_LP(dev_priv)) {
1948352ff8bdSFrançois Tigeot panel->backlight.setup = bxt_setup_backlight;
1949352ff8bdSFrançois Tigeot panel->backlight.enable = bxt_enable_backlight;
1950352ff8bdSFrançois Tigeot panel->backlight.disable = bxt_disable_backlight;
1951352ff8bdSFrançois Tigeot panel->backlight.set = bxt_set_backlight;
1952352ff8bdSFrançois Tigeot panel->backlight.get = bxt_get_backlight;
1953aee94f86SFrançois Tigeot panel->backlight.hz_to_pwm = bxt_hz_to_pwm;
1954*3f2dd94aSFrançois Tigeot } else if (HAS_PCH_CNP(dev_priv)) {
1955*3f2dd94aSFrançois Tigeot panel->backlight.setup = cnp_setup_backlight;
1956*3f2dd94aSFrançois Tigeot panel->backlight.enable = cnp_enable_backlight;
1957*3f2dd94aSFrançois Tigeot panel->backlight.disable = cnp_disable_backlight;
1958*3f2dd94aSFrançois Tigeot panel->backlight.set = bxt_set_backlight;
1959*3f2dd94aSFrançois Tigeot panel->backlight.get = bxt_get_backlight;
1960*3f2dd94aSFrançois Tigeot panel->backlight.hz_to_pwm = cnp_hz_to_pwm;
19618621f407SFrançois Tigeot } else if (HAS_PCH_LPT(dev_priv) || HAS_PCH_SPT(dev_priv) ||
19628621f407SFrançois Tigeot HAS_PCH_KBP(dev_priv)) {
1963352ff8bdSFrançois Tigeot panel->backlight.setup = lpt_setup_backlight;
1964352ff8bdSFrançois Tigeot panel->backlight.enable = lpt_enable_backlight;
1965352ff8bdSFrançois Tigeot panel->backlight.disable = lpt_disable_backlight;
1966352ff8bdSFrançois Tigeot panel->backlight.set = lpt_set_backlight;
1967352ff8bdSFrançois Tigeot panel->backlight.get = lpt_get_backlight;
1968aee94f86SFrançois Tigeot if (HAS_PCH_LPT(dev_priv))
1969352ff8bdSFrançois Tigeot panel->backlight.hz_to_pwm = lpt_hz_to_pwm;
1970352ff8bdSFrançois Tigeot else
1971352ff8bdSFrançois Tigeot panel->backlight.hz_to_pwm = spt_hz_to_pwm;
1972aee94f86SFrançois Tigeot } else if (HAS_PCH_SPLIT(dev_priv)) {
1973352ff8bdSFrançois Tigeot panel->backlight.setup = pch_setup_backlight;
1974352ff8bdSFrançois Tigeot panel->backlight.enable = pch_enable_backlight;
1975352ff8bdSFrançois Tigeot panel->backlight.disable = pch_disable_backlight;
1976352ff8bdSFrançois Tigeot panel->backlight.set = pch_set_backlight;
1977352ff8bdSFrançois Tigeot panel->backlight.get = pch_get_backlight;
1978352ff8bdSFrançois Tigeot panel->backlight.hz_to_pwm = pch_hz_to_pwm;
1979aee94f86SFrançois Tigeot } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
19808621f407SFrançois Tigeot if (connector->base.connector_type == DRM_MODE_CONNECTOR_DSI) {
1981352ff8bdSFrançois Tigeot panel->backlight.setup = pwm_setup_backlight;
1982352ff8bdSFrançois Tigeot panel->backlight.enable = pwm_enable_backlight;
1983352ff8bdSFrançois Tigeot panel->backlight.disable = pwm_disable_backlight;
1984352ff8bdSFrançois Tigeot panel->backlight.set = pwm_set_backlight;
1985352ff8bdSFrançois Tigeot panel->backlight.get = pwm_get_backlight;
1986a05eeebfSFrançois Tigeot } else {
1987352ff8bdSFrançois Tigeot panel->backlight.setup = vlv_setup_backlight;
1988352ff8bdSFrançois Tigeot panel->backlight.enable = vlv_enable_backlight;
1989352ff8bdSFrançois Tigeot panel->backlight.disable = vlv_disable_backlight;
1990352ff8bdSFrançois Tigeot panel->backlight.set = vlv_set_backlight;
1991352ff8bdSFrançois Tigeot panel->backlight.get = vlv_get_backlight;
1992352ff8bdSFrançois Tigeot panel->backlight.hz_to_pwm = vlv_hz_to_pwm;
1993a05eeebfSFrançois Tigeot }
1994aee94f86SFrançois Tigeot } else if (IS_GEN4(dev_priv)) {
1995352ff8bdSFrançois Tigeot panel->backlight.setup = i965_setup_backlight;
1996352ff8bdSFrançois Tigeot panel->backlight.enable = i965_enable_backlight;
1997352ff8bdSFrançois Tigeot panel->backlight.disable = i965_disable_backlight;
1998352ff8bdSFrançois Tigeot panel->backlight.set = i9xx_set_backlight;
1999352ff8bdSFrançois Tigeot panel->backlight.get = i9xx_get_backlight;
2000352ff8bdSFrançois Tigeot panel->backlight.hz_to_pwm = i965_hz_to_pwm;
20019edbd4a0SFrançois Tigeot } else {
2002352ff8bdSFrançois Tigeot panel->backlight.setup = i9xx_setup_backlight;
2003352ff8bdSFrançois Tigeot panel->backlight.enable = i9xx_enable_backlight;
2004352ff8bdSFrançois Tigeot panel->backlight.disable = i9xx_disable_backlight;
2005352ff8bdSFrançois Tigeot panel->backlight.set = i9xx_set_backlight;
2006352ff8bdSFrançois Tigeot panel->backlight.get = i9xx_get_backlight;
2007352ff8bdSFrançois Tigeot panel->backlight.hz_to_pwm = i9xx_hz_to_pwm;
20089edbd4a0SFrançois Tigeot }
20099edbd4a0SFrançois Tigeot }
201019df918dSFrançois Tigeot
intel_panel_init(struct intel_panel * panel,struct drm_display_mode * fixed_mode,struct drm_display_mode * alt_fixed_mode,struct drm_display_mode * downclock_mode)201119df918dSFrançois Tigeot int intel_panel_init(struct intel_panel *panel,
2012ba55f2f5SFrançois Tigeot struct drm_display_mode *fixed_mode,
2013*3f2dd94aSFrançois Tigeot struct drm_display_mode *alt_fixed_mode,
2014ba55f2f5SFrançois Tigeot struct drm_display_mode *downclock_mode)
201519df918dSFrançois Tigeot {
2016352ff8bdSFrançois Tigeot intel_panel_init_backlight_funcs(panel);
2017352ff8bdSFrançois Tigeot
201819df918dSFrançois Tigeot panel->fixed_mode = fixed_mode;
2019*3f2dd94aSFrançois Tigeot panel->alt_fixed_mode = alt_fixed_mode;
2020ba55f2f5SFrançois Tigeot panel->downclock_mode = downclock_mode;
202119df918dSFrançois Tigeot
202219df918dSFrançois Tigeot return 0;
202319df918dSFrançois Tigeot }
202419df918dSFrançois Tigeot
intel_panel_fini(struct intel_panel * panel)202519df918dSFrançois Tigeot void intel_panel_fini(struct intel_panel *panel)
202619df918dSFrançois Tigeot {
202719df918dSFrançois Tigeot struct intel_connector *intel_connector =
202819df918dSFrançois Tigeot container_of(panel, struct intel_connector, panel);
202919df918dSFrançois Tigeot
203019df918dSFrançois Tigeot if (panel->fixed_mode)
203119df918dSFrançois Tigeot drm_mode_destroy(intel_connector->base.dev, panel->fixed_mode);
20329edbd4a0SFrançois Tigeot
2033*3f2dd94aSFrançois Tigeot if (panel->alt_fixed_mode)
2034*3f2dd94aSFrançois Tigeot drm_mode_destroy(intel_connector->base.dev,
2035*3f2dd94aSFrançois Tigeot panel->alt_fixed_mode);
2036*3f2dd94aSFrançois Tigeot
20379edbd4a0SFrançois Tigeot if (panel->downclock_mode)
20389edbd4a0SFrançois Tigeot drm_mode_destroy(intel_connector->base.dev,
20399edbd4a0SFrançois Tigeot panel->downclock_mode);
204019df918dSFrançois Tigeot }
2041