blob: 41d00b1603e304ff5694cc2bd8ec8daee80bc80c [file] [log] [blame]
Chris Wilson1d8e1c72010-08-07 11:01:28 +01001/*
2 * Copyright © 2006-2010 Intel Corporation
3 * Copyright (c) 2006 Dave Airlie <airlied@linux.ie>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Eric Anholt <eric@anholt.net>
26 * Dave Airlie <airlied@linux.ie>
27 * Jesse Barnes <jesse.barnes@intel.com>
28 * Chris Wilson <chris@chris-wilson.co.uk>
29 */
30
Joe Perchesa70491c2012-03-18 13:00:11 -070031#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
32
Javi Merinof7660932015-04-16 12:43:45 -070033#include <linux/kernel.h>
Carsten Emde7bd90902012-03-15 15:56:25 +010034#include <linux/moduleparam.h>
Shobhit Kumarb029e662015-06-26 14:32:10 +053035#include <linux/pwm.h>
Chris Wilson1d8e1c72010-08-07 11:01:28 +010036#include "intel_drv.h"
37
Shobhit Kumarb029e662015-06-26 14:32:10 +053038#define CRC_PMIC_PWM_PERIOD_NS 21333
39
Chris Wilson1d8e1c72010-08-07 11:01:28 +010040void
Ville Syrjälä4c6df4b2013-09-02 21:13:39 +030041intel_fixed_panel_mode(const struct drm_display_mode *fixed_mode,
Chris Wilson1d8e1c72010-08-07 11:01:28 +010042 struct drm_display_mode *adjusted_mode)
43{
Ville Syrjälä4c6df4b2013-09-02 21:13:39 +030044 drm_mode_copy(adjusted_mode, fixed_mode);
Imre Deaka52690e2013-08-27 12:24:09 +030045
46 drm_mode_set_crtcinfo(adjusted_mode, 0);
Chris Wilson1d8e1c72010-08-07 11:01:28 +010047}
48
Jani Nikula525997e2014-04-29 23:30:48 +030049/**
50 * intel_find_panel_downclock - find the reduced downclock for LVDS in EDID
Mika Kaholaa318b4c2016-12-13 10:02:48 +020051 * @dev_priv: i915 device instance
Jani Nikula525997e2014-04-29 23:30:48 +030052 * @fixed_mode : panel native mode
53 * @connector: LVDS/eDP connector
54 *
55 * Return downclock_avail
56 * Find the reduced downclock for LVDS/eDP in EDID.
57 */
58struct drm_display_mode *
Mika Kaholaa318b4c2016-12-13 10:02:48 +020059intel_find_panel_downclock(struct drm_i915_private *dev_priv,
Jani Nikula525997e2014-04-29 23:30:48 +030060 struct drm_display_mode *fixed_mode,
61 struct drm_connector *connector)
62{
63 struct drm_display_mode *scan, *tmp_mode;
64 int temp_downclock;
65
66 temp_downclock = fixed_mode->clock;
67 tmp_mode = NULL;
68
69 list_for_each_entry(scan, &connector->probed_modes, head) {
70 /*
71 * If one mode has the same resolution with the fixed_panel
72 * mode while they have the different refresh rate, it means
73 * that the reduced downclock is found. In such
74 * case we can set the different FPx0/1 to dynamically select
75 * between low and high frequency.
76 */
77 if (scan->hdisplay == fixed_mode->hdisplay &&
78 scan->hsync_start == fixed_mode->hsync_start &&
79 scan->hsync_end == fixed_mode->hsync_end &&
80 scan->htotal == fixed_mode->htotal &&
81 scan->vdisplay == fixed_mode->vdisplay &&
82 scan->vsync_start == fixed_mode->vsync_start &&
83 scan->vsync_end == fixed_mode->vsync_end &&
84 scan->vtotal == fixed_mode->vtotal) {
85 if (scan->clock < temp_downclock) {
86 /*
87 * The downclock is already found. But we
88 * expect to find the lower downclock.
89 */
90 temp_downclock = scan->clock;
91 tmp_mode = scan;
92 }
93 }
94 }
95
96 if (temp_downclock < fixed_mode->clock)
Mika Kaholaa318b4c2016-12-13 10:02:48 +020097 return drm_mode_duplicate(&dev_priv->drm, tmp_mode);
Jani Nikula525997e2014-04-29 23:30:48 +030098 else
99 return NULL;
100}
101
Chris Wilson1d8e1c72010-08-07 11:01:28 +0100102/* adjusted_mode has been preset to be the panel's fixed mode */
103void
Jesse Barnesb074cec2013-04-25 12:55:02 -0700104intel_pch_panel_fitting(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200105 struct intel_crtc_state *pipe_config,
Jesse Barnesb074cec2013-04-25 12:55:02 -0700106 int fitting_mode)
Chris Wilson1d8e1c72010-08-07 11:01:28 +0100107{
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +0300108 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
109 int x = 0, y = 0, width = 0, height = 0;
Chris Wilson1d8e1c72010-08-07 11:01:28 +0100110
111 /* Native modes don't need fitting */
Ville Syrjäläaad941d2015-09-25 16:38:56 +0300112 if (adjusted_mode->crtc_hdisplay == pipe_config->pipe_src_w &&
Shashank Sharmae5c05932017-07-21 20:55:05 +0530113 adjusted_mode->crtc_vdisplay == pipe_config->pipe_src_h &&
114 !pipe_config->ycbcr420)
Chris Wilson1d8e1c72010-08-07 11:01:28 +0100115 goto done;
116
117 switch (fitting_mode) {
118 case DRM_MODE_SCALE_CENTER:
Ville Syrjälä37327ab2013-09-04 18:25:28 +0300119 width = pipe_config->pipe_src_w;
120 height = pipe_config->pipe_src_h;
Ville Syrjäläaad941d2015-09-25 16:38:56 +0300121 x = (adjusted_mode->crtc_hdisplay - width + 1)/2;
122 y = (adjusted_mode->crtc_vdisplay - height + 1)/2;
Chris Wilson1d8e1c72010-08-07 11:01:28 +0100123 break;
124
125 case DRM_MODE_SCALE_ASPECT:
126 /* Scale but preserve the aspect ratio */
127 {
Ville Syrjäläaad941d2015-09-25 16:38:56 +0300128 u32 scaled_width = adjusted_mode->crtc_hdisplay
Daniel Vetter9084e7d2013-09-16 23:43:45 +0200129 * pipe_config->pipe_src_h;
130 u32 scaled_height = pipe_config->pipe_src_w
Ville Syrjäläaad941d2015-09-25 16:38:56 +0300131 * adjusted_mode->crtc_vdisplay;
Chris Wilson1d8e1c72010-08-07 11:01:28 +0100132 if (scaled_width > scaled_height) { /* pillar */
Ville Syrjälä37327ab2013-09-04 18:25:28 +0300133 width = scaled_height / pipe_config->pipe_src_h;
Adam Jackson302983e2011-07-13 16:32:32 -0400134 if (width & 1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400135 width++;
Ville Syrjäläaad941d2015-09-25 16:38:56 +0300136 x = (adjusted_mode->crtc_hdisplay - width + 1) / 2;
Chris Wilson1d8e1c72010-08-07 11:01:28 +0100137 y = 0;
Ville Syrjäläaad941d2015-09-25 16:38:56 +0300138 height = adjusted_mode->crtc_vdisplay;
Chris Wilson1d8e1c72010-08-07 11:01:28 +0100139 } else if (scaled_width < scaled_height) { /* letter */
Ville Syrjälä37327ab2013-09-04 18:25:28 +0300140 height = scaled_width / pipe_config->pipe_src_w;
Adam Jackson302983e2011-07-13 16:32:32 -0400141 if (height & 1)
142 height++;
Ville Syrjäläaad941d2015-09-25 16:38:56 +0300143 y = (adjusted_mode->crtc_vdisplay - height + 1) / 2;
Chris Wilson1d8e1c72010-08-07 11:01:28 +0100144 x = 0;
Ville Syrjäläaad941d2015-09-25 16:38:56 +0300145 width = adjusted_mode->crtc_hdisplay;
Chris Wilson1d8e1c72010-08-07 11:01:28 +0100146 } else {
147 x = y = 0;
Ville Syrjäläaad941d2015-09-25 16:38:56 +0300148 width = adjusted_mode->crtc_hdisplay;
149 height = adjusted_mode->crtc_vdisplay;
Chris Wilson1d8e1c72010-08-07 11:01:28 +0100150 }
151 }
152 break;
153
Chris Wilson1d8e1c72010-08-07 11:01:28 +0100154 case DRM_MODE_SCALE_FULLSCREEN:
155 x = y = 0;
Ville Syrjäläaad941d2015-09-25 16:38:56 +0300156 width = adjusted_mode->crtc_hdisplay;
157 height = adjusted_mode->crtc_vdisplay;
Chris Wilson1d8e1c72010-08-07 11:01:28 +0100158 break;
Jesse Barnesab3e67f2013-04-25 12:55:03 -0700159
160 default:
161 WARN(1, "bad panel fit mode: %d\n", fitting_mode);
162 return;
Chris Wilson1d8e1c72010-08-07 11:01:28 +0100163 }
164
165done:
Jesse Barnesb074cec2013-04-25 12:55:02 -0700166 pipe_config->pch_pfit.pos = (x << 16) | y;
167 pipe_config->pch_pfit.size = (width << 16) | height;
Chris Wilsonfd4daa92013-08-27 17:04:17 +0100168 pipe_config->pch_pfit.enabled = pipe_config->pch_pfit.size != 0;
Chris Wilson1d8e1c72010-08-07 11:01:28 +0100169}
Chris Wilsona9573552010-08-22 13:18:16 +0100170
Jesse Barnes2dd24552013-04-25 12:55:01 -0700171static void
Ville Syrjälä5e7234c2015-09-25 16:37:43 +0300172centre_horizontally(struct drm_display_mode *adjusted_mode,
Jesse Barnes2dd24552013-04-25 12:55:01 -0700173 int width)
174{
175 u32 border, sync_pos, blank_width, sync_width;
176
177 /* keep the hsync and hblank widths constant */
Ville Syrjälä5e7234c2015-09-25 16:37:43 +0300178 sync_width = adjusted_mode->crtc_hsync_end - adjusted_mode->crtc_hsync_start;
179 blank_width = adjusted_mode->crtc_hblank_end - adjusted_mode->crtc_hblank_start;
Jesse Barnes2dd24552013-04-25 12:55:01 -0700180 sync_pos = (blank_width - sync_width + 1) / 2;
181
Ville Syrjäläaad941d2015-09-25 16:38:56 +0300182 border = (adjusted_mode->crtc_hdisplay - width + 1) / 2;
Jesse Barnes2dd24552013-04-25 12:55:01 -0700183 border += border & 1; /* make the border even */
184
Ville Syrjälä5e7234c2015-09-25 16:37:43 +0300185 adjusted_mode->crtc_hdisplay = width;
186 adjusted_mode->crtc_hblank_start = width + border;
187 adjusted_mode->crtc_hblank_end = adjusted_mode->crtc_hblank_start + blank_width;
Jesse Barnes2dd24552013-04-25 12:55:01 -0700188
Ville Syrjälä5e7234c2015-09-25 16:37:43 +0300189 adjusted_mode->crtc_hsync_start = adjusted_mode->crtc_hblank_start + sync_pos;
190 adjusted_mode->crtc_hsync_end = adjusted_mode->crtc_hsync_start + sync_width;
Jesse Barnes2dd24552013-04-25 12:55:01 -0700191}
192
193static void
Ville Syrjälä5e7234c2015-09-25 16:37:43 +0300194centre_vertically(struct drm_display_mode *adjusted_mode,
Jesse Barnes2dd24552013-04-25 12:55:01 -0700195 int height)
196{
197 u32 border, sync_pos, blank_width, sync_width;
198
199 /* keep the vsync and vblank widths constant */
Ville Syrjälä5e7234c2015-09-25 16:37:43 +0300200 sync_width = adjusted_mode->crtc_vsync_end - adjusted_mode->crtc_vsync_start;
201 blank_width = adjusted_mode->crtc_vblank_end - adjusted_mode->crtc_vblank_start;
Jesse Barnes2dd24552013-04-25 12:55:01 -0700202 sync_pos = (blank_width - sync_width + 1) / 2;
203
Ville Syrjäläaad941d2015-09-25 16:38:56 +0300204 border = (adjusted_mode->crtc_vdisplay - height + 1) / 2;
Jesse Barnes2dd24552013-04-25 12:55:01 -0700205
Ville Syrjälä5e7234c2015-09-25 16:37:43 +0300206 adjusted_mode->crtc_vdisplay = height;
207 adjusted_mode->crtc_vblank_start = height + border;
208 adjusted_mode->crtc_vblank_end = adjusted_mode->crtc_vblank_start + blank_width;
Jesse Barnes2dd24552013-04-25 12:55:01 -0700209
Ville Syrjälä5e7234c2015-09-25 16:37:43 +0300210 adjusted_mode->crtc_vsync_start = adjusted_mode->crtc_vblank_start + sync_pos;
211 adjusted_mode->crtc_vsync_end = adjusted_mode->crtc_vsync_start + sync_width;
Jesse Barnes2dd24552013-04-25 12:55:01 -0700212}
213
214static inline u32 panel_fitter_scaling(u32 source, u32 target)
215{
216 /*
217 * Floating point operation is not supported. So the FACTOR
218 * is defined, which can avoid the floating point computation
219 * when calculating the panel ratio.
220 */
221#define ACCURACY 12
222#define FACTOR (1 << ACCURACY)
223 u32 ratio = source * FACTOR / target;
224 return (FACTOR * ratio + FACTOR/2) / FACTOR;
225}
226
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200227static void i965_scale_aspect(struct intel_crtc_state *pipe_config,
Daniel Vetter9084e7d2013-09-16 23:43:45 +0200228 u32 *pfit_control)
229{
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +0300230 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ville Syrjäläaad941d2015-09-25 16:38:56 +0300231 u32 scaled_width = adjusted_mode->crtc_hdisplay *
Daniel Vetter9084e7d2013-09-16 23:43:45 +0200232 pipe_config->pipe_src_h;
233 u32 scaled_height = pipe_config->pipe_src_w *
Ville Syrjäläaad941d2015-09-25 16:38:56 +0300234 adjusted_mode->crtc_vdisplay;
Daniel Vetter9084e7d2013-09-16 23:43:45 +0200235
236 /* 965+ is easy, it does everything in hw */
237 if (scaled_width > scaled_height)
238 *pfit_control |= PFIT_ENABLE |
239 PFIT_SCALING_PILLAR;
240 else if (scaled_width < scaled_height)
241 *pfit_control |= PFIT_ENABLE |
242 PFIT_SCALING_LETTER;
Ville Syrjäläaad941d2015-09-25 16:38:56 +0300243 else if (adjusted_mode->crtc_hdisplay != pipe_config->pipe_src_w)
Daniel Vetter9084e7d2013-09-16 23:43:45 +0200244 *pfit_control |= PFIT_ENABLE | PFIT_SCALING_AUTO;
245}
246
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200247static void i9xx_scale_aspect(struct intel_crtc_state *pipe_config,
Daniel Vetter9084e7d2013-09-16 23:43:45 +0200248 u32 *pfit_control, u32 *pfit_pgm_ratios,
249 u32 *border)
250{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +0200251 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ville Syrjäläaad941d2015-09-25 16:38:56 +0300252 u32 scaled_width = adjusted_mode->crtc_hdisplay *
Daniel Vetter9084e7d2013-09-16 23:43:45 +0200253 pipe_config->pipe_src_h;
254 u32 scaled_height = pipe_config->pipe_src_w *
Ville Syrjäläaad941d2015-09-25 16:38:56 +0300255 adjusted_mode->crtc_vdisplay;
Daniel Vetter9084e7d2013-09-16 23:43:45 +0200256 u32 bits;
257
258 /*
259 * For earlier chips we have to calculate the scaling
260 * ratio by hand and program it into the
261 * PFIT_PGM_RATIO register
262 */
263 if (scaled_width > scaled_height) { /* pillar */
264 centre_horizontally(adjusted_mode,
265 scaled_height /
266 pipe_config->pipe_src_h);
267
268 *border = LVDS_BORDER_ENABLE;
Ville Syrjäläaad941d2015-09-25 16:38:56 +0300269 if (pipe_config->pipe_src_h != adjusted_mode->crtc_vdisplay) {
Daniel Vetter9084e7d2013-09-16 23:43:45 +0200270 bits = panel_fitter_scaling(pipe_config->pipe_src_h,
Ville Syrjäläaad941d2015-09-25 16:38:56 +0300271 adjusted_mode->crtc_vdisplay);
Daniel Vetter9084e7d2013-09-16 23:43:45 +0200272
273 *pfit_pgm_ratios |= (bits << PFIT_HORIZ_SCALE_SHIFT |
274 bits << PFIT_VERT_SCALE_SHIFT);
275 *pfit_control |= (PFIT_ENABLE |
276 VERT_INTERP_BILINEAR |
277 HORIZ_INTERP_BILINEAR);
278 }
279 } else if (scaled_width < scaled_height) { /* letter */
280 centre_vertically(adjusted_mode,
281 scaled_width /
282 pipe_config->pipe_src_w);
283
284 *border = LVDS_BORDER_ENABLE;
Ville Syrjäläaad941d2015-09-25 16:38:56 +0300285 if (pipe_config->pipe_src_w != adjusted_mode->crtc_hdisplay) {
Daniel Vetter9084e7d2013-09-16 23:43:45 +0200286 bits = panel_fitter_scaling(pipe_config->pipe_src_w,
Ville Syrjäläaad941d2015-09-25 16:38:56 +0300287 adjusted_mode->crtc_hdisplay);
Daniel Vetter9084e7d2013-09-16 23:43:45 +0200288
289 *pfit_pgm_ratios |= (bits << PFIT_HORIZ_SCALE_SHIFT |
290 bits << PFIT_VERT_SCALE_SHIFT);
291 *pfit_control |= (PFIT_ENABLE |
292 VERT_INTERP_BILINEAR |
293 HORIZ_INTERP_BILINEAR);
294 }
295 } else {
296 /* Aspects match, Let hw scale both directions */
297 *pfit_control |= (PFIT_ENABLE |
298 VERT_AUTO_SCALE | HORIZ_AUTO_SCALE |
299 VERT_INTERP_BILINEAR |
300 HORIZ_INTERP_BILINEAR);
301 }
302}
303
Jesse Barnes2dd24552013-04-25 12:55:01 -0700304void intel_gmch_panel_fitting(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200305 struct intel_crtc_state *pipe_config,
Jesse Barnes2dd24552013-04-25 12:55:01 -0700306 int fitting_mode)
307{
Tvrtko Ursulin66478472016-11-16 08:55:40 +0000308 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
Jesse Barnes2dd24552013-04-25 12:55:01 -0700309 u32 pfit_control = 0, pfit_pgm_ratios = 0, border = 0;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +0300310 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Jesse Barnes2dd24552013-04-25 12:55:01 -0700311
312 /* Native modes don't need fitting */
Ville Syrjäläaad941d2015-09-25 16:38:56 +0300313 if (adjusted_mode->crtc_hdisplay == pipe_config->pipe_src_w &&
314 adjusted_mode->crtc_vdisplay == pipe_config->pipe_src_h)
Jesse Barnes2dd24552013-04-25 12:55:01 -0700315 goto out;
316
317 switch (fitting_mode) {
318 case DRM_MODE_SCALE_CENTER:
319 /*
320 * For centered modes, we have to calculate border widths &
321 * heights and modify the values programmed into the CRTC.
322 */
Ville Syrjälä37327ab2013-09-04 18:25:28 +0300323 centre_horizontally(adjusted_mode, pipe_config->pipe_src_w);
324 centre_vertically(adjusted_mode, pipe_config->pipe_src_h);
Jesse Barnes2dd24552013-04-25 12:55:01 -0700325 border = LVDS_BORDER_ENABLE;
326 break;
327 case DRM_MODE_SCALE_ASPECT:
328 /* Scale but preserve the aspect ratio */
Tvrtko Ursulin66478472016-11-16 08:55:40 +0000329 if (INTEL_GEN(dev_priv) >= 4)
Daniel Vetter9084e7d2013-09-16 23:43:45 +0200330 i965_scale_aspect(pipe_config, &pfit_control);
331 else
332 i9xx_scale_aspect(pipe_config, &pfit_control,
333 &pfit_pgm_ratios, &border);
Jesse Barnes2dd24552013-04-25 12:55:01 -0700334 break;
Jesse Barnes2dd24552013-04-25 12:55:01 -0700335 case DRM_MODE_SCALE_FULLSCREEN:
336 /*
337 * Full scaling, even if it changes the aspect ratio.
338 * Fortunately this is all done for us in hw.
339 */
Ville Syrjäläaad941d2015-09-25 16:38:56 +0300340 if (pipe_config->pipe_src_h != adjusted_mode->crtc_vdisplay ||
341 pipe_config->pipe_src_w != adjusted_mode->crtc_hdisplay) {
Jesse Barnes2dd24552013-04-25 12:55:01 -0700342 pfit_control |= PFIT_ENABLE;
Tvrtko Ursulin66478472016-11-16 08:55:40 +0000343 if (INTEL_GEN(dev_priv) >= 4)
Jesse Barnes2dd24552013-04-25 12:55:01 -0700344 pfit_control |= PFIT_SCALING_AUTO;
345 else
346 pfit_control |= (VERT_AUTO_SCALE |
347 VERT_INTERP_BILINEAR |
348 HORIZ_AUTO_SCALE |
349 HORIZ_INTERP_BILINEAR);
350 }
351 break;
Jesse Barnesab3e67f2013-04-25 12:55:03 -0700352 default:
353 WARN(1, "bad panel fit mode: %d\n", fitting_mode);
354 return;
Jesse Barnes2dd24552013-04-25 12:55:01 -0700355 }
356
357 /* 965+ wants fuzzy fitting */
358 /* FIXME: handle multiple panels by failing gracefully */
Tvrtko Ursulin66478472016-11-16 08:55:40 +0000359 if (INTEL_GEN(dev_priv) >= 4)
Jesse Barnes2dd24552013-04-25 12:55:01 -0700360 pfit_control |= ((intel_crtc->pipe << PFIT_PIPE_SHIFT) |
361 PFIT_FILTER_FUZZY);
362
363out:
364 if ((pfit_control & PFIT_ENABLE) == 0) {
365 pfit_control = 0;
366 pfit_pgm_ratios = 0;
367 }
368
Daniel Vetter6b89cdd2014-07-09 22:35:53 +0200369 /* Make sure pre-965 set dither correctly for 18bpp panels. */
Tvrtko Ursulin66478472016-11-16 08:55:40 +0000370 if (INTEL_GEN(dev_priv) < 4 && pipe_config->pipe_bpp == 18)
Daniel Vetter6b89cdd2014-07-09 22:35:53 +0200371 pfit_control |= PANEL_8TO6_DITHER_ENABLE;
372
Daniel Vetter2deefda2013-04-25 22:52:17 +0200373 pipe_config->gmch_pfit.control = pfit_control;
374 pipe_config->gmch_pfit.pgm_ratios = pfit_pgm_ratios;
Daniel Vetter68fc8742013-04-25 22:52:16 +0200375 pipe_config->gmch_pfit.lvds_border_bits = border;
Jesse Barnes2dd24552013-04-25 12:55:01 -0700376}
377
Jani Nikula525997e2014-04-29 23:30:48 +0300378enum drm_connector_status
Mika Kahola1650be72016-12-13 10:02:47 +0200379intel_panel_detect(struct drm_i915_private *dev_priv)
Jani Nikula525997e2014-04-29 23:30:48 +0300380{
Jani Nikula525997e2014-04-29 23:30:48 +0300381 /* Assume that the BIOS does not lie through the OpRegion... */
Michal Wajdeczko4f044a82017-09-19 19:38:44 +0000382 if (!i915_modparams.panel_ignore_lid && dev_priv->opregion.lid_state) {
Williams, Dan J115719f2015-10-12 21:12:57 +0000383 return *dev_priv->opregion.lid_state & 0x1 ?
Jani Nikula525997e2014-04-29 23:30:48 +0300384 connector_status_connected :
385 connector_status_disconnected;
386 }
387
Michal Wajdeczko4f044a82017-09-19 19:38:44 +0000388 switch (i915_modparams.panel_ignore_lid) {
Jani Nikula525997e2014-04-29 23:30:48 +0300389 case -2:
390 return connector_status_connected;
391 case -1:
392 return connector_status_disconnected;
393 default:
394 return connector_status_unknown;
395 }
396}
397
Jani Nikula6dda7302014-06-24 18:27:40 +0300398/**
399 * scale - scale values from one range to another
Jani Nikula6dda7302014-06-24 18:27:40 +0300400 * @source_val: value in range [@source_min..@source_max]
Chris Wilsone9a744f2018-02-14 09:17:46 +0000401 * @source_min: minimum legal value for @source_val
402 * @source_max: maximum legal value for @source_val
403 * @target_min: corresponding target value for @source_min
404 * @target_max: corresponding target value for @source_max
Jani Nikula6dda7302014-06-24 18:27:40 +0300405 *
406 * Return @source_val in range [@source_min..@source_max] scaled to range
407 * [@target_min..@target_max].
408 */
409static uint32_t scale(uint32_t source_val,
410 uint32_t source_min, uint32_t source_max,
411 uint32_t target_min, uint32_t target_max)
412{
413 uint64_t target_val;
414
415 WARN_ON(source_min > source_max);
416 WARN_ON(target_min > target_max);
417
418 /* defensive */
419 source_val = clamp(source_val, source_min, source_max);
420
421 /* avoid overflows */
Chris Wilson5af4ce72018-02-14 09:17:47 +0000422 target_val = mul_u32_u32(source_val - source_min,
423 target_max - target_min);
424 target_val = DIV_ROUND_CLOSEST_ULL(target_val, source_max - source_min);
Jani Nikula6dda7302014-06-24 18:27:40 +0300425 target_val += target_min;
426
427 return target_val;
428}
429
430/* Scale user_level in range [0..user_max] to [hw_min..hw_max]. */
431static inline u32 scale_user_to_hw(struct intel_connector *connector,
432 u32 user_level, u32 user_max)
433{
434 struct intel_panel *panel = &connector->panel;
435
436 return scale(user_level, 0, user_max,
437 panel->backlight.min, panel->backlight.max);
438}
439
440/* Scale user_level in range [0..user_max] to [0..hw_max], clamping the result
441 * to [hw_min..hw_max]. */
442static inline u32 clamp_user_to_hw(struct intel_connector *connector,
443 u32 user_level, u32 user_max)
444{
445 struct intel_panel *panel = &connector->panel;
446 u32 hw_level;
447
448 hw_level = scale(user_level, 0, user_max, 0, panel->backlight.max);
449 hw_level = clamp(hw_level, panel->backlight.min, panel->backlight.max);
450
451 return hw_level;
452}
453
454/* Scale hw_level in range [hw_min..hw_max] to [0..user_max]. */
455static inline u32 scale_hw_to_user(struct intel_connector *connector,
456 u32 hw_level, u32 user_max)
457{
458 struct intel_panel *panel = &connector->panel;
459
460 return scale(hw_level, panel->backlight.min, panel->backlight.max,
461 0, user_max);
462}
463
Jani Nikula7bd688c2013-11-08 16:48:56 +0200464static u32 intel_panel_compute_brightness(struct intel_connector *connector,
465 u32 val)
Carsten Emde7bd90902012-03-15 15:56:25 +0100466{
Jani Nikulae6cb3722015-12-18 10:51:35 +0200467 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Jani Nikulaf91c15e2013-11-08 16:49:00 +0200468 struct intel_panel *panel = &connector->panel;
469
470 WARN_ON(panel->backlight.max == 0);
Carsten Emde4dca20e2012-03-15 15:56:26 +0100471
Michal Wajdeczko4f044a82017-09-19 19:38:44 +0000472 if (i915_modparams.invert_brightness < 0)
Carsten Emde4dca20e2012-03-15 15:56:26 +0100473 return val;
474
Michal Wajdeczko4f044a82017-09-19 19:38:44 +0000475 if (i915_modparams.invert_brightness > 0 ||
Jani Nikulad6540632013-04-12 15:18:36 +0300476 dev_priv->quirks & QUIRK_INVERT_BRIGHTNESS) {
Jani Nikulae9d74862017-05-31 11:33:55 +0300477 return panel->backlight.max - val + panel->backlight.min;
Jani Nikulad6540632013-04-12 15:18:36 +0300478 }
Carsten Emde7bd90902012-03-15 15:56:25 +0100479
480 return val;
481}
482
Jani Nikula437b15b2015-09-04 16:55:13 +0300483static u32 lpt_get_backlight(struct intel_connector *connector)
Chris Wilsona9573552010-08-22 13:18:16 +0100484{
Jani Nikulae6cb3722015-12-18 10:51:35 +0200485 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Daniel Vetter96ab4c72013-11-14 15:17:41 +0100486
487 return I915_READ(BLC_PWM_PCH_CTL2) & BACKLIGHT_DUTY_CYCLE_MASK;
488}
489
Jani Nikula7bd688c2013-11-08 16:48:56 +0200490static u32 pch_get_backlight(struct intel_connector *connector)
Chris Wilsona9573552010-08-22 13:18:16 +0100491{
Jani Nikulae6cb3722015-12-18 10:51:35 +0200492 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Jani Nikula7bd688c2013-11-08 16:48:56 +0200493
494 return I915_READ(BLC_PWM_CPU_CTL) & BACKLIGHT_DUTY_CYCLE_MASK;
495}
496
497static u32 i9xx_get_backlight(struct intel_connector *connector)
498{
Jani Nikulae6cb3722015-12-18 10:51:35 +0200499 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Jani Nikula636baeb2013-11-08 16:49:02 +0200500 struct intel_panel *panel = &connector->panel;
Jani Nikula7bd688c2013-11-08 16:48:56 +0200501 u32 val;
502
503 val = I915_READ(BLC_PWM_CTL) & BACKLIGHT_DUTY_CYCLE_MASK;
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +0000504 if (INTEL_GEN(dev_priv) < 4)
Jani Nikula7bd688c2013-11-08 16:48:56 +0200505 val >>= 1;
506
Jani Nikula636baeb2013-11-08 16:49:02 +0200507 if (panel->backlight.combination_mode) {
Jani Nikula7bd688c2013-11-08 16:48:56 +0200508 u8 lbpc;
509
Chris Wilson91c8a322016-07-05 10:40:23 +0100510 pci_read_config_byte(dev_priv->drm.pdev, LBPC, &lbpc);
Jani Nikula7bd688c2013-11-08 16:48:56 +0200511 val *= lbpc;
512 }
513
514 return val;
515}
516
Jani Nikulae6cb3722015-12-18 10:51:35 +0200517static u32 _vlv_get_backlight(struct drm_i915_private *dev_priv, enum pipe pipe)
Jani Nikula7bd688c2013-11-08 16:48:56 +0200518{
Ville Syrjälä23ec0a82014-11-07 11:15:59 +0200519 if (WARN_ON(pipe != PIPE_A && pipe != PIPE_B))
520 return 0;
521
Jani Nikula7bd688c2013-11-08 16:48:56 +0200522 return I915_READ(VLV_BLC_PWM_CTL(pipe)) & BACKLIGHT_DUTY_CYCLE_MASK;
523}
524
525static u32 vlv_get_backlight(struct intel_connector *connector)
526{
Jani Nikulae6cb3722015-12-18 10:51:35 +0200527 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Jani Nikula7bd688c2013-11-08 16:48:56 +0200528 enum pipe pipe = intel_get_pipe_from_connector(connector);
529
Jani Nikulae6cb3722015-12-18 10:51:35 +0200530 return _vlv_get_backlight(dev_priv, pipe);
Jani Nikula7bd688c2013-11-08 16:48:56 +0200531}
532
Vandana Kannan0fb890c2015-05-05 14:51:56 +0530533static u32 bxt_get_backlight(struct intel_connector *connector)
534{
Jani Nikulae6cb3722015-12-18 10:51:35 +0200535 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Sunil Kamath022e4e52015-09-30 22:34:57 +0530536 struct intel_panel *panel = &connector->panel;
Vandana Kannan0fb890c2015-05-05 14:51:56 +0530537
Sunil Kamath022e4e52015-09-30 22:34:57 +0530538 return I915_READ(BXT_BLC_PWM_DUTY(panel->backlight.controller));
Vandana Kannan0fb890c2015-05-05 14:51:56 +0530539}
540
Shobhit Kumarb029e662015-06-26 14:32:10 +0530541static u32 pwm_get_backlight(struct intel_connector *connector)
542{
543 struct intel_panel *panel = &connector->panel;
544 int duty_ns;
545
546 duty_ns = pwm_get_duty_cycle(panel->backlight.pwm);
547 return DIV_ROUND_UP(duty_ns * 100, CRC_PMIC_PWM_PERIOD_NS);
548}
549
Maarten Lankhorst7d025e02017-06-12 12:21:15 +0200550static void lpt_set_backlight(const struct drm_connector_state *conn_state, u32 level)
Ben Widawskyf8e10062013-11-11 11:12:57 +0200551{
Maarten Lankhorst7d025e02017-06-12 12:21:15 +0200552 struct intel_connector *connector = to_intel_connector(conn_state->connector);
Jani Nikulae6cb3722015-12-18 10:51:35 +0200553 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Maarten Lankhorst7d025e02017-06-12 12:21:15 +0200554
Ben Widawskyf8e10062013-11-11 11:12:57 +0200555 u32 val = I915_READ(BLC_PWM_PCH_CTL2) & ~BACKLIGHT_DUTY_CYCLE_MASK;
556 I915_WRITE(BLC_PWM_PCH_CTL2, val | level);
557}
558
Maarten Lankhorst7d025e02017-06-12 12:21:15 +0200559static void pch_set_backlight(const struct drm_connector_state *conn_state, u32 level)
Chris Wilsona9573552010-08-22 13:18:16 +0100560{
Maarten Lankhorst7d025e02017-06-12 12:21:15 +0200561 struct intel_connector *connector = to_intel_connector(conn_state->connector);
Jani Nikulae6cb3722015-12-18 10:51:35 +0200562 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Chris Wilsona9573552010-08-22 13:18:16 +0100563 u32 tmp;
564
Jani Nikula7bd688c2013-11-08 16:48:56 +0200565 tmp = I915_READ(BLC_PWM_CPU_CTL) & ~BACKLIGHT_DUTY_CYCLE_MASK;
566 I915_WRITE(BLC_PWM_CPU_CTL, tmp | level);
567}
Chris Wilsona9573552010-08-22 13:18:16 +0100568
Maarten Lankhorst7d025e02017-06-12 12:21:15 +0200569static void i9xx_set_backlight(const struct drm_connector_state *conn_state, u32 level)
Jani Nikula7bd688c2013-11-08 16:48:56 +0200570{
Maarten Lankhorst7d025e02017-06-12 12:21:15 +0200571 struct intel_connector *connector = to_intel_connector(conn_state->connector);
Jani Nikulae6cb3722015-12-18 10:51:35 +0200572 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Jani Nikulaf91c15e2013-11-08 16:49:00 +0200573 struct intel_panel *panel = &connector->panel;
Jani Nikulab329b322013-11-08 16:48:57 +0200574 u32 tmp, mask;
Takashi Iwaiba3820a2011-03-10 14:02:12 +0100575
Jani Nikulaf91c15e2013-11-08 16:49:00 +0200576 WARN_ON(panel->backlight.max == 0);
577
Jani Nikula636baeb2013-11-08 16:49:02 +0200578 if (panel->backlight.combination_mode) {
Takashi Iwaiba3820a2011-03-10 14:02:12 +0100579 u8 lbpc;
580
Jani Nikulaf91c15e2013-11-08 16:49:00 +0200581 lbpc = level * 0xfe / panel->backlight.max + 1;
Takashi Iwaiba3820a2011-03-10 14:02:12 +0100582 level /= lbpc;
Chris Wilson91c8a322016-07-05 10:40:23 +0100583 pci_write_config_byte(dev_priv->drm.pdev, LBPC, lbpc);
Takashi Iwaiba3820a2011-03-10 14:02:12 +0100584 }
585
Jani Nikulae6cb3722015-12-18 10:51:35 +0200586 if (IS_GEN4(dev_priv)) {
Jani Nikulab329b322013-11-08 16:48:57 +0200587 mask = BACKLIGHT_DUTY_CYCLE_MASK;
588 } else {
Chris Wilsona9573552010-08-22 13:18:16 +0100589 level <<= 1;
Jani Nikulab329b322013-11-08 16:48:57 +0200590 mask = BACKLIGHT_DUTY_CYCLE_MASK_PNV;
591 }
Jani Nikula7bd688c2013-11-08 16:48:56 +0200592
Jani Nikulab329b322013-11-08 16:48:57 +0200593 tmp = I915_READ(BLC_PWM_CTL) & ~mask;
Jani Nikula7bd688c2013-11-08 16:48:56 +0200594 I915_WRITE(BLC_PWM_CTL, tmp | level);
595}
596
Maarten Lankhorst7d025e02017-06-12 12:21:15 +0200597static void vlv_set_backlight(const struct drm_connector_state *conn_state, u32 level)
Jani Nikula7bd688c2013-11-08 16:48:56 +0200598{
Maarten Lankhorst7d025e02017-06-12 12:21:15 +0200599 struct intel_connector *connector = to_intel_connector(conn_state->connector);
Jani Nikulae6cb3722015-12-18 10:51:35 +0200600 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Maarten Lankhorst7d025e02017-06-12 12:21:15 +0200601 enum pipe pipe = to_intel_crtc(conn_state->crtc)->pipe;
Jani Nikula7bd688c2013-11-08 16:48:56 +0200602 u32 tmp;
603
604 tmp = I915_READ(VLV_BLC_PWM_CTL(pipe)) & ~BACKLIGHT_DUTY_CYCLE_MASK;
605 I915_WRITE(VLV_BLC_PWM_CTL(pipe), tmp | level);
606}
607
Maarten Lankhorst7d025e02017-06-12 12:21:15 +0200608static void bxt_set_backlight(const struct drm_connector_state *conn_state, u32 level)
Vandana Kannan0fb890c2015-05-05 14:51:56 +0530609{
Maarten Lankhorst7d025e02017-06-12 12:21:15 +0200610 struct intel_connector *connector = to_intel_connector(conn_state->connector);
Jani Nikulae6cb3722015-12-18 10:51:35 +0200611 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Sunil Kamath022e4e52015-09-30 22:34:57 +0530612 struct intel_panel *panel = &connector->panel;
Vandana Kannan0fb890c2015-05-05 14:51:56 +0530613
Sunil Kamath022e4e52015-09-30 22:34:57 +0530614 I915_WRITE(BXT_BLC_PWM_DUTY(panel->backlight.controller), level);
Vandana Kannan0fb890c2015-05-05 14:51:56 +0530615}
616
Maarten Lankhorst7d025e02017-06-12 12:21:15 +0200617static void pwm_set_backlight(const struct drm_connector_state *conn_state, u32 level)
Shobhit Kumarb029e662015-06-26 14:32:10 +0530618{
Maarten Lankhorst7d025e02017-06-12 12:21:15 +0200619 struct intel_panel *panel = &to_intel_connector(conn_state->connector)->panel;
Shobhit Kumarb029e662015-06-26 14:32:10 +0530620 int duty_ns = DIV_ROUND_UP(level * CRC_PMIC_PWM_PERIOD_NS, 100);
621
622 pwm_config(panel->backlight.pwm, duty_ns, CRC_PMIC_PWM_PERIOD_NS);
623}
624
Jani Nikula7bd688c2013-11-08 16:48:56 +0200625static void
Maarten Lankhorst7d025e02017-06-12 12:21:15 +0200626intel_panel_actually_set_backlight(const struct drm_connector_state *conn_state, u32 level)
Jani Nikula7bd688c2013-11-08 16:48:56 +0200627{
Maarten Lankhorst7d025e02017-06-12 12:21:15 +0200628 struct intel_connector *connector = to_intel_connector(conn_state->connector);
Jani Nikula5507fae2015-09-14 14:03:48 +0300629 struct intel_panel *panel = &connector->panel;
Jani Nikula7bd688c2013-11-08 16:48:56 +0200630
631 DRM_DEBUG_DRIVER("set backlight PWM = %d\n", level);
632
633 level = intel_panel_compute_brightness(connector, level);
Maarten Lankhorst7d025e02017-06-12 12:21:15 +0200634 panel->backlight.set(conn_state, level);
Chris Wilsona9573552010-08-22 13:18:16 +0100635}
Chris Wilson47356eb2011-01-11 17:06:04 +0000636
Jani Nikula6dda7302014-06-24 18:27:40 +0300637/* set backlight brightness to level in range [0..max], assuming hw min is
638 * respected.
639 */
Maarten Lankhorst90d7cd22017-06-12 12:21:14 +0200640void intel_panel_set_backlight_acpi(const struct drm_connector_state *conn_state,
Jani Nikula6dda7302014-06-24 18:27:40 +0300641 u32 user_level, u32 user_max)
642{
Maarten Lankhorst90d7cd22017-06-12 12:21:14 +0200643 struct intel_connector *connector = to_intel_connector(conn_state->connector);
Jani Nikulae6cb3722015-12-18 10:51:35 +0200644 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Jani Nikula6dda7302014-06-24 18:27:40 +0300645 struct intel_panel *panel = &connector->panel;
Jani Nikula6dda7302014-06-24 18:27:40 +0300646 u32 hw_level;
Jani Nikula6dda7302014-06-24 18:27:40 +0300647
Ville Syrjälä260d8f92014-11-07 15:20:23 +0200648 /*
Maarten Lankhorst90d7cd22017-06-12 12:21:14 +0200649 * Lack of crtc may occur during driver init because
Ville Syrjälä260d8f92014-11-07 15:20:23 +0200650 * connection_mutex isn't held across the entire backlight
651 * setup + modeset readout, and the BIOS can issue the
652 * requests at any time.
653 */
Maarten Lankhorst90d7cd22017-06-12 12:21:14 +0200654 if (!panel->backlight.present || !conn_state->crtc)
Jani Nikula6dda7302014-06-24 18:27:40 +0300655 return;
656
Daniel Vetter07f11d42014-09-15 14:35:09 +0200657 mutex_lock(&dev_priv->backlight_lock);
Jani Nikula6dda7302014-06-24 18:27:40 +0300658
659 WARN_ON(panel->backlight.max == 0);
660
661 hw_level = clamp_user_to_hw(connector, user_level, user_max);
662 panel->backlight.level = hw_level;
663
664 if (panel->backlight.device)
665 panel->backlight.device->props.brightness =
666 scale_hw_to_user(connector,
667 panel->backlight.level,
668 panel->backlight.device->props.max_brightness);
669
670 if (panel->backlight.enabled)
Maarten Lankhorst7d025e02017-06-12 12:21:15 +0200671 intel_panel_actually_set_backlight(conn_state, hw_level);
Jani Nikulaf91c15e2013-11-08 16:49:00 +0200672
Daniel Vetter07f11d42014-09-15 14:35:09 +0200673 mutex_unlock(&dev_priv->backlight_lock);
Takashi Iwaif52c6192011-10-14 11:45:40 +0200674}
675
Maarten Lankhorst7d025e02017-06-12 12:21:15 +0200676static void lpt_disable_backlight(const struct drm_connector_state *old_conn_state)
Jani Nikula437b15b2015-09-04 16:55:13 +0300677{
Maarten Lankhorst7d025e02017-06-12 12:21:15 +0200678 struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
Jani Nikulae6cb3722015-12-18 10:51:35 +0200679 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Jani Nikula437b15b2015-09-04 16:55:13 +0300680 u32 tmp;
681
Maarten Lankhorst7d025e02017-06-12 12:21:15 +0200682 intel_panel_actually_set_backlight(old_conn_state, 0);
Jani Nikula437b15b2015-09-04 16:55:13 +0300683
Jani Nikula6675bce2015-10-28 13:57:09 +0200684 /*
685 * Although we don't support or enable CPU PWM with LPT/SPT based
686 * systems, it may have been enabled prior to loading the
687 * driver. Disable to avoid warnings on LCPLL disable.
688 *
689 * This needs rework if we need to add support for CPU PWM on PCH split
690 * platforms.
691 */
692 tmp = I915_READ(BLC_PWM_CPU_CTL2);
693 if (tmp & BLM_PWM_ENABLE) {
694 DRM_DEBUG_KMS("cpu backlight was enabled, disabling\n");
695 I915_WRITE(BLC_PWM_CPU_CTL2, tmp & ~BLM_PWM_ENABLE);
696 }
697
Jani Nikula437b15b2015-09-04 16:55:13 +0300698 tmp = I915_READ(BLC_PWM_PCH_CTL1);
699 I915_WRITE(BLC_PWM_PCH_CTL1, tmp & ~BLM_PCH_PWM_ENABLE);
700}
701
Maarten Lankhorst7d025e02017-06-12 12:21:15 +0200702static void pch_disable_backlight(const struct drm_connector_state *old_conn_state)
Jani Nikula7bd688c2013-11-08 16:48:56 +0200703{
Maarten Lankhorst7d025e02017-06-12 12:21:15 +0200704 struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
Jani Nikulae6cb3722015-12-18 10:51:35 +0200705 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Jani Nikula7bd688c2013-11-08 16:48:56 +0200706 u32 tmp;
707
Maarten Lankhorst7d025e02017-06-12 12:21:15 +0200708 intel_panel_actually_set_backlight(old_conn_state, 0);
Jani Nikula3bd712e2013-11-08 16:48:59 +0200709
Jani Nikula7bd688c2013-11-08 16:48:56 +0200710 tmp = I915_READ(BLC_PWM_CPU_CTL2);
711 I915_WRITE(BLC_PWM_CPU_CTL2, tmp & ~BLM_PWM_ENABLE);
712
713 tmp = I915_READ(BLC_PWM_PCH_CTL1);
714 I915_WRITE(BLC_PWM_PCH_CTL1, tmp & ~BLM_PCH_PWM_ENABLE);
715}
716
Maarten Lankhorst7d025e02017-06-12 12:21:15 +0200717static void i9xx_disable_backlight(const struct drm_connector_state *old_conn_state)
Jani Nikula3bd712e2013-11-08 16:48:59 +0200718{
Maarten Lankhorst7d025e02017-06-12 12:21:15 +0200719 intel_panel_actually_set_backlight(old_conn_state, 0);
Jani Nikula3bd712e2013-11-08 16:48:59 +0200720}
721
Maarten Lankhorst7d025e02017-06-12 12:21:15 +0200722static void i965_disable_backlight(const struct drm_connector_state *old_conn_state)
Jani Nikula7bd688c2013-11-08 16:48:56 +0200723{
Maarten Lankhorst7d025e02017-06-12 12:21:15 +0200724 struct drm_i915_private *dev_priv = to_i915(old_conn_state->connector->dev);
Jani Nikula7bd688c2013-11-08 16:48:56 +0200725 u32 tmp;
726
Maarten Lankhorst7d025e02017-06-12 12:21:15 +0200727 intel_panel_actually_set_backlight(old_conn_state, 0);
Jani Nikula3bd712e2013-11-08 16:48:59 +0200728
Jani Nikula7bd688c2013-11-08 16:48:56 +0200729 tmp = I915_READ(BLC_PWM_CTL2);
730 I915_WRITE(BLC_PWM_CTL2, tmp & ~BLM_PWM_ENABLE);
731}
732
Maarten Lankhorst7d025e02017-06-12 12:21:15 +0200733static void vlv_disable_backlight(const struct drm_connector_state *old_conn_state)
Jani Nikula7bd688c2013-11-08 16:48:56 +0200734{
Maarten Lankhorst7d025e02017-06-12 12:21:15 +0200735 struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
Jani Nikulae6cb3722015-12-18 10:51:35 +0200736 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Maarten Lankhorst7d025e02017-06-12 12:21:15 +0200737 enum pipe pipe = to_intel_crtc(old_conn_state->crtc)->pipe;
Jani Nikula7bd688c2013-11-08 16:48:56 +0200738 u32 tmp;
739
Maarten Lankhorst7d025e02017-06-12 12:21:15 +0200740 intel_panel_actually_set_backlight(old_conn_state, 0);
Jani Nikula3bd712e2013-11-08 16:48:59 +0200741
Jani Nikula7bd688c2013-11-08 16:48:56 +0200742 tmp = I915_READ(VLV_BLC_PWM_CTL2(pipe));
743 I915_WRITE(VLV_BLC_PWM_CTL2(pipe), tmp & ~BLM_PWM_ENABLE);
744}
745
Maarten Lankhorst7d025e02017-06-12 12:21:15 +0200746static void bxt_disable_backlight(const struct drm_connector_state *old_conn_state)
Vandana Kannan0fb890c2015-05-05 14:51:56 +0530747{
Maarten Lankhorst7d025e02017-06-12 12:21:15 +0200748 struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
Jani Nikulae6cb3722015-12-18 10:51:35 +0200749 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Sunil Kamath022e4e52015-09-30 22:34:57 +0530750 struct intel_panel *panel = &connector->panel;
751 u32 tmp, val;
Vandana Kannan0fb890c2015-05-05 14:51:56 +0530752
Maarten Lankhorst7d025e02017-06-12 12:21:15 +0200753 intel_panel_actually_set_backlight(old_conn_state, 0);
Vandana Kannan0fb890c2015-05-05 14:51:56 +0530754
Sunil Kamath022e4e52015-09-30 22:34:57 +0530755 tmp = I915_READ(BXT_BLC_PWM_CTL(panel->backlight.controller));
756 I915_WRITE(BXT_BLC_PWM_CTL(panel->backlight.controller),
757 tmp & ~BXT_BLC_PWM_ENABLE);
758
759 if (panel->backlight.controller == 1) {
760 val = I915_READ(UTIL_PIN_CTL);
761 val &= ~UTIL_PIN_ENABLE;
762 I915_WRITE(UTIL_PIN_CTL, val);
763 }
Vandana Kannan0fb890c2015-05-05 14:51:56 +0530764}
765
Maarten Lankhorst7d025e02017-06-12 12:21:15 +0200766static void cnp_disable_backlight(const struct drm_connector_state *old_conn_state)
Rodrigo Vivi4c9f7082017-06-02 13:06:42 -0700767{
Maarten Lankhorst7d025e02017-06-12 12:21:15 +0200768 struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
Rodrigo Vivi4c9f7082017-06-02 13:06:42 -0700769 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
770 struct intel_panel *panel = &connector->panel;
771 u32 tmp;
772
Maarten Lankhorst7d025e02017-06-12 12:21:15 +0200773 intel_panel_actually_set_backlight(old_conn_state, 0);
Rodrigo Vivi4c9f7082017-06-02 13:06:42 -0700774
775 tmp = I915_READ(BXT_BLC_PWM_CTL(panel->backlight.controller));
776 I915_WRITE(BXT_BLC_PWM_CTL(panel->backlight.controller),
777 tmp & ~BXT_BLC_PWM_ENABLE);
778}
779
Maarten Lankhorst7d025e02017-06-12 12:21:15 +0200780static void pwm_disable_backlight(const struct drm_connector_state *old_conn_state)
Shobhit Kumarb029e662015-06-26 14:32:10 +0530781{
Maarten Lankhorst7d025e02017-06-12 12:21:15 +0200782 struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
Shobhit Kumarb029e662015-06-26 14:32:10 +0530783 struct intel_panel *panel = &connector->panel;
784
785 /* Disable the backlight */
786 pwm_config(panel->backlight.pwm, 0, CRC_PMIC_PWM_PERIOD_NS);
787 usleep_range(2000, 3000);
788 pwm_disable(panel->backlight.pwm);
789}
790
Maarten Lankhorstb037d582017-06-12 12:21:13 +0200791void intel_panel_disable_backlight(const struct drm_connector_state *old_conn_state)
Chris Wilson47356eb2011-01-11 17:06:04 +0000792{
Maarten Lankhorstb037d582017-06-12 12:21:13 +0200793 struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
Jani Nikulae6cb3722015-12-18 10:51:35 +0200794 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Jani Nikula58c68772013-11-08 16:48:54 +0200795 struct intel_panel *panel = &connector->panel;
Jani Nikula8ba2d182013-04-12 15:18:37 +0300796
Ville Syrjälä260d8f92014-11-07 15:20:23 +0200797 if (!panel->backlight.present)
Jesse Barnes752aa882013-10-31 18:55:49 +0200798 return;
799
Jani Nikula3f577572013-07-25 14:31:30 +0300800 /*
Lukas Wunner5389e912015-09-04 21:06:15 +0200801 * Do not disable backlight on the vga_switcheroo path. When switching
Jani Nikula3f577572013-07-25 14:31:30 +0300802 * away from i915, the other client may depend on i915 to handle the
803 * backlight. This will leave the backlight on unnecessarily when
804 * another client is not activated.
805 */
Chris Wilson91c8a322016-07-05 10:40:23 +0100806 if (dev_priv->drm.switch_power_state == DRM_SWITCH_POWER_CHANGING) {
Jani Nikula3f577572013-07-25 14:31:30 +0300807 DRM_DEBUG_DRIVER("Skipping backlight disable on vga switch\n");
808 return;
809 }
810
Daniel Vetter07f11d42014-09-15 14:35:09 +0200811 mutex_lock(&dev_priv->backlight_lock);
Chris Wilson47356eb2011-01-11 17:06:04 +0000812
Jani Nikulaab656bb2014-08-13 12:10:12 +0300813 if (panel->backlight.device)
814 panel->backlight.device->props.power = FB_BLANK_POWERDOWN;
Jani Nikula58c68772013-11-08 16:48:54 +0200815 panel->backlight.enabled = false;
Maarten Lankhorst7d025e02017-06-12 12:21:15 +0200816 panel->backlight.disable(old_conn_state);
Jani Nikula8ba2d182013-04-12 15:18:37 +0300817
Daniel Vetter07f11d42014-09-15 14:35:09 +0200818 mutex_unlock(&dev_priv->backlight_lock);
Chris Wilson47356eb2011-01-11 17:06:04 +0000819}
820
Maarten Lankhorst7d025e02017-06-12 12:21:15 +0200821static void lpt_enable_backlight(const struct intel_crtc_state *crtc_state,
822 const struct drm_connector_state *conn_state)
Daniel Vetter96ab4c72013-11-14 15:17:41 +0100823{
Maarten Lankhorst7d025e02017-06-12 12:21:15 +0200824 struct intel_connector *connector = to_intel_connector(conn_state->connector);
Jani Nikulae6cb3722015-12-18 10:51:35 +0200825 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Daniel Vetter96ab4c72013-11-14 15:17:41 +0100826 struct intel_panel *panel = &connector->panel;
Shawn Leee29aff02016-09-19 13:35:26 +0300827 u32 pch_ctl1, pch_ctl2, schicken;
Chris Wilson47356eb2011-01-11 17:06:04 +0000828
Daniel Vetter96ab4c72013-11-14 15:17:41 +0100829 pch_ctl1 = I915_READ(BLC_PWM_PCH_CTL1);
830 if (pch_ctl1 & BLM_PCH_PWM_ENABLE) {
831 DRM_DEBUG_KMS("pch backlight already enabled\n");
832 pch_ctl1 &= ~BLM_PCH_PWM_ENABLE;
833 I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1);
Chris Wilson47356eb2011-01-11 17:06:04 +0000834 }
835
Shawn Leee29aff02016-09-19 13:35:26 +0300836 if (HAS_PCH_LPT(dev_priv)) {
837 schicken = I915_READ(SOUTH_CHICKEN2);
838 if (panel->backlight.alternate_pwm_increment)
839 schicken |= LPT_PWM_GRANULARITY;
840 else
841 schicken &= ~LPT_PWM_GRANULARITY;
842 I915_WRITE(SOUTH_CHICKEN2, schicken);
843 } else {
844 schicken = I915_READ(SOUTH_CHICKEN1);
845 if (panel->backlight.alternate_pwm_increment)
846 schicken |= SPT_PWM_GRANULARITY;
847 else
848 schicken &= ~SPT_PWM_GRANULARITY;
849 I915_WRITE(SOUTH_CHICKEN1, schicken);
850 }
851
Daniel Vetter96ab4c72013-11-14 15:17:41 +0100852 pch_ctl2 = panel->backlight.max << 16;
853 I915_WRITE(BLC_PWM_PCH_CTL2, pch_ctl2);
854
855 pch_ctl1 = 0;
856 if (panel->backlight.active_low_pwm)
857 pch_ctl1 |= BLM_PCH_POLARITY;
858
Jani Nikulae6b26272014-10-10 17:53:33 +0300859 /* After LPT, override is the default. */
860 if (HAS_PCH_LPT(dev_priv))
861 pch_ctl1 |= BLM_PCH_OVERRIDE_ENABLE;
Daniel Vetter96ab4c72013-11-14 15:17:41 +0100862
863 I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1);
864 POSTING_READ(BLC_PWM_PCH_CTL1);
865 I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1 | BLM_PCH_PWM_ENABLE);
866
867 /* This won't stick until the above enable. */
Maarten Lankhorst7d025e02017-06-12 12:21:15 +0200868 intel_panel_actually_set_backlight(conn_state, panel->backlight.level);
Daniel Vetter96ab4c72013-11-14 15:17:41 +0100869}
870
Maarten Lankhorst7d025e02017-06-12 12:21:15 +0200871static void pch_enable_backlight(const struct intel_crtc_state *crtc_state,
872 const struct drm_connector_state *conn_state)
Jani Nikula7bd688c2013-11-08 16:48:56 +0200873{
Maarten Lankhorst7d025e02017-06-12 12:21:15 +0200874 struct intel_connector *connector = to_intel_connector(conn_state->connector);
Jani Nikulae6cb3722015-12-18 10:51:35 +0200875 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Jani Nikula3bd712e2013-11-08 16:48:59 +0200876 struct intel_panel *panel = &connector->panel;
Maarten Lankhorst7d025e02017-06-12 12:21:15 +0200877 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
Jani Nikulab35684b2013-11-14 12:13:41 +0200878 u32 cpu_ctl2, pch_ctl1, pch_ctl2;
Jani Nikula7bd688c2013-11-08 16:48:56 +0200879
Jani Nikulab35684b2013-11-14 12:13:41 +0200880 cpu_ctl2 = I915_READ(BLC_PWM_CPU_CTL2);
881 if (cpu_ctl2 & BLM_PWM_ENABLE) {
Scot Doyle813008c2014-08-19 02:07:13 +0000882 DRM_DEBUG_KMS("cpu backlight already enabled\n");
Jani Nikulab35684b2013-11-14 12:13:41 +0200883 cpu_ctl2 &= ~BLM_PWM_ENABLE;
884 I915_WRITE(BLC_PWM_CPU_CTL2, cpu_ctl2);
Jani Nikula7bd688c2013-11-08 16:48:56 +0200885 }
Jani Nikula3bd712e2013-11-08 16:48:59 +0200886
Jani Nikulab35684b2013-11-14 12:13:41 +0200887 pch_ctl1 = I915_READ(BLC_PWM_PCH_CTL1);
888 if (pch_ctl1 & BLM_PCH_PWM_ENABLE) {
889 DRM_DEBUG_KMS("pch backlight already enabled\n");
890 pch_ctl1 &= ~BLM_PCH_PWM_ENABLE;
891 I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1);
892 }
893
894 if (cpu_transcoder == TRANSCODER_EDP)
895 cpu_ctl2 = BLM_TRANSCODER_EDP;
896 else
897 cpu_ctl2 = BLM_PIPE(cpu_transcoder);
898 I915_WRITE(BLC_PWM_CPU_CTL2, cpu_ctl2);
899 POSTING_READ(BLC_PWM_CPU_CTL2);
900 I915_WRITE(BLC_PWM_CPU_CTL2, cpu_ctl2 | BLM_PWM_ENABLE);
901
902 /* This won't stick until the above enable. */
Maarten Lankhorst7d025e02017-06-12 12:21:15 +0200903 intel_panel_actually_set_backlight(conn_state, panel->backlight.level);
Jani Nikulab35684b2013-11-14 12:13:41 +0200904
905 pch_ctl2 = panel->backlight.max << 16;
906 I915_WRITE(BLC_PWM_PCH_CTL2, pch_ctl2);
907
Jani Nikulab35684b2013-11-14 12:13:41 +0200908 pch_ctl1 = 0;
909 if (panel->backlight.active_low_pwm)
910 pch_ctl1 |= BLM_PCH_POLARITY;
Daniel Vetter96ab4c72013-11-14 15:17:41 +0100911
Jani Nikulab35684b2013-11-14 12:13:41 +0200912 I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1);
913 POSTING_READ(BLC_PWM_PCH_CTL1);
914 I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1 | BLM_PCH_PWM_ENABLE);
Jani Nikula3bd712e2013-11-08 16:48:59 +0200915}
916
Maarten Lankhorst7d025e02017-06-12 12:21:15 +0200917static void i9xx_enable_backlight(const struct intel_crtc_state *crtc_state,
918 const struct drm_connector_state *conn_state)
Jani Nikula3bd712e2013-11-08 16:48:59 +0200919{
Maarten Lankhorst7d025e02017-06-12 12:21:15 +0200920 struct intel_connector *connector = to_intel_connector(conn_state->connector);
Jani Nikulae6cb3722015-12-18 10:51:35 +0200921 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Jani Nikula3bd712e2013-11-08 16:48:59 +0200922 struct intel_panel *panel = &connector->panel;
Jani Nikulab35684b2013-11-14 12:13:41 +0200923 u32 ctl, freq;
Jani Nikula3bd712e2013-11-08 16:48:59 +0200924
Jani Nikulab35684b2013-11-14 12:13:41 +0200925 ctl = I915_READ(BLC_PWM_CTL);
926 if (ctl & BACKLIGHT_DUTY_CYCLE_MASK_PNV) {
Scot Doyle813008c2014-08-19 02:07:13 +0000927 DRM_DEBUG_KMS("backlight already enabled\n");
Jani Nikulab35684b2013-11-14 12:13:41 +0200928 I915_WRITE(BLC_PWM_CTL, 0);
929 }
930
931 freq = panel->backlight.max;
932 if (panel->backlight.combination_mode)
933 freq /= 0xff;
934
935 ctl = freq << 17;
Jani Nikulab6ab66a2014-02-25 13:11:47 +0200936 if (panel->backlight.combination_mode)
Jani Nikulab35684b2013-11-14 12:13:41 +0200937 ctl |= BLM_LEGACY_MODE;
Jani Nikulae6cb3722015-12-18 10:51:35 +0200938 if (IS_PINEVIEW(dev_priv) && panel->backlight.active_low_pwm)
Jani Nikulab35684b2013-11-14 12:13:41 +0200939 ctl |= BLM_POLARITY_PNV;
940
941 I915_WRITE(BLC_PWM_CTL, ctl);
942 POSTING_READ(BLC_PWM_CTL);
943
944 /* XXX: combine this into above write? */
Maarten Lankhorst7d025e02017-06-12 12:21:15 +0200945 intel_panel_actually_set_backlight(conn_state, panel->backlight.level);
Jani Nikula2059ac32015-06-26 14:18:56 +0300946
947 /*
948 * Needed to enable backlight on some 855gm models. BLC_HIST_CTL is
949 * 855gm only, but checking for gen2 is safe, as 855gm is the only gen2
950 * that has backlight.
951 */
Jani Nikulae6cb3722015-12-18 10:51:35 +0200952 if (IS_GEN2(dev_priv))
Jani Nikula2059ac32015-06-26 14:18:56 +0300953 I915_WRITE(BLC_HIST_CTL, BLM_HISTOGRAM_ENABLE);
Jani Nikula7bd688c2013-11-08 16:48:56 +0200954}
955
Maarten Lankhorst7d025e02017-06-12 12:21:15 +0200956static void i965_enable_backlight(const struct intel_crtc_state *crtc_state,
957 const struct drm_connector_state *conn_state)
Jani Nikula7bd688c2013-11-08 16:48:56 +0200958{
Maarten Lankhorst7d025e02017-06-12 12:21:15 +0200959 struct intel_connector *connector = to_intel_connector(conn_state->connector);
Jani Nikulae6cb3722015-12-18 10:51:35 +0200960 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Jani Nikula3bd712e2013-11-08 16:48:59 +0200961 struct intel_panel *panel = &connector->panel;
Maarten Lankhorst7d025e02017-06-12 12:21:15 +0200962 enum pipe pipe = to_intel_crtc(conn_state->crtc)->pipe;
Jani Nikulab35684b2013-11-14 12:13:41 +0200963 u32 ctl, ctl2, freq;
Jani Nikula7bd688c2013-11-08 16:48:56 +0200964
Jani Nikulab35684b2013-11-14 12:13:41 +0200965 ctl2 = I915_READ(BLC_PWM_CTL2);
966 if (ctl2 & BLM_PWM_ENABLE) {
Scot Doyle813008c2014-08-19 02:07:13 +0000967 DRM_DEBUG_KMS("backlight already enabled\n");
Jani Nikulab35684b2013-11-14 12:13:41 +0200968 ctl2 &= ~BLM_PWM_ENABLE;
969 I915_WRITE(BLC_PWM_CTL2, ctl2);
970 }
Jani Nikula7bd688c2013-11-08 16:48:56 +0200971
Jani Nikulab35684b2013-11-14 12:13:41 +0200972 freq = panel->backlight.max;
973 if (panel->backlight.combination_mode)
974 freq /= 0xff;
Jani Nikula7bd688c2013-11-08 16:48:56 +0200975
Jani Nikulab35684b2013-11-14 12:13:41 +0200976 ctl = freq << 16;
977 I915_WRITE(BLC_PWM_CTL, ctl);
Jani Nikula7bd688c2013-11-08 16:48:56 +0200978
Jani Nikulab35684b2013-11-14 12:13:41 +0200979 ctl2 = BLM_PIPE(pipe);
980 if (panel->backlight.combination_mode)
981 ctl2 |= BLM_COMBINATION_MODE;
982 if (panel->backlight.active_low_pwm)
983 ctl2 |= BLM_POLARITY_I965;
984 I915_WRITE(BLC_PWM_CTL2, ctl2);
985 POSTING_READ(BLC_PWM_CTL2);
986 I915_WRITE(BLC_PWM_CTL2, ctl2 | BLM_PWM_ENABLE);
Jani Nikula2e7eeeb2014-06-09 18:24:34 +0300987
Maarten Lankhorst7d025e02017-06-12 12:21:15 +0200988 intel_panel_actually_set_backlight(conn_state, panel->backlight.level);
Jani Nikula7bd688c2013-11-08 16:48:56 +0200989}
990
Maarten Lankhorst7d025e02017-06-12 12:21:15 +0200991static void vlv_enable_backlight(const struct intel_crtc_state *crtc_state,
992 const struct drm_connector_state *conn_state)
Jani Nikula7bd688c2013-11-08 16:48:56 +0200993{
Maarten Lankhorst7d025e02017-06-12 12:21:15 +0200994 struct intel_connector *connector = to_intel_connector(conn_state->connector);
Jani Nikulae6cb3722015-12-18 10:51:35 +0200995 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Jani Nikula3bd712e2013-11-08 16:48:59 +0200996 struct intel_panel *panel = &connector->panel;
Maarten Lankhorst7d025e02017-06-12 12:21:15 +0200997 enum pipe pipe = to_intel_crtc(crtc_state->base.crtc)->pipe;
Jani Nikulab35684b2013-11-14 12:13:41 +0200998 u32 ctl, ctl2;
Jani Nikula7bd688c2013-11-08 16:48:56 +0200999
Jani Nikulab35684b2013-11-14 12:13:41 +02001000 ctl2 = I915_READ(VLV_BLC_PWM_CTL2(pipe));
1001 if (ctl2 & BLM_PWM_ENABLE) {
Scot Doyle813008c2014-08-19 02:07:13 +00001002 DRM_DEBUG_KMS("backlight already enabled\n");
Jani Nikulab35684b2013-11-14 12:13:41 +02001003 ctl2 &= ~BLM_PWM_ENABLE;
1004 I915_WRITE(VLV_BLC_PWM_CTL2(pipe), ctl2);
1005 }
Jani Nikula7bd688c2013-11-08 16:48:56 +02001006
Jani Nikulab35684b2013-11-14 12:13:41 +02001007 ctl = panel->backlight.max << 16;
1008 I915_WRITE(VLV_BLC_PWM_CTL(pipe), ctl);
Jani Nikula7bd688c2013-11-08 16:48:56 +02001009
Jani Nikulab35684b2013-11-14 12:13:41 +02001010 /* XXX: combine this into above write? */
Maarten Lankhorst7d025e02017-06-12 12:21:15 +02001011 intel_panel_actually_set_backlight(conn_state, panel->backlight.level);
Jani Nikulab35684b2013-11-14 12:13:41 +02001012
1013 ctl2 = 0;
1014 if (panel->backlight.active_low_pwm)
1015 ctl2 |= BLM_POLARITY_I965;
1016 I915_WRITE(VLV_BLC_PWM_CTL2(pipe), ctl2);
1017 POSTING_READ(VLV_BLC_PWM_CTL2(pipe));
1018 I915_WRITE(VLV_BLC_PWM_CTL2(pipe), ctl2 | BLM_PWM_ENABLE);
Jani Nikula7bd688c2013-11-08 16:48:56 +02001019}
1020
Maarten Lankhorst7d025e02017-06-12 12:21:15 +02001021static void bxt_enable_backlight(const struct intel_crtc_state *crtc_state,
1022 const struct drm_connector_state *conn_state)
Vandana Kannan0fb890c2015-05-05 14:51:56 +05301023{
Maarten Lankhorst7d025e02017-06-12 12:21:15 +02001024 struct intel_connector *connector = to_intel_connector(conn_state->connector);
Jani Nikulae6cb3722015-12-18 10:51:35 +02001025 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Vandana Kannan0fb890c2015-05-05 14:51:56 +05301026 struct intel_panel *panel = &connector->panel;
Maarten Lankhorst7d025e02017-06-12 12:21:15 +02001027 enum pipe pipe = to_intel_crtc(crtc_state->base.crtc)->pipe;
Sunil Kamath022e4e52015-09-30 22:34:57 +05301028 u32 pwm_ctl, val;
Vandana Kannan0fb890c2015-05-05 14:51:56 +05301029
Vidya Srinivasadd03372016-12-08 11:26:18 +02001030 /* Controller 1 uses the utility pin. */
Sunil Kamath022e4e52015-09-30 22:34:57 +05301031 if (panel->backlight.controller == 1) {
1032 val = I915_READ(UTIL_PIN_CTL);
1033 if (val & UTIL_PIN_ENABLE) {
1034 DRM_DEBUG_KMS("util pin already enabled\n");
1035 val &= ~UTIL_PIN_ENABLE;
1036 I915_WRITE(UTIL_PIN_CTL, val);
1037 }
1038
1039 val = 0;
1040 if (panel->backlight.util_pin_active_low)
1041 val |= UTIL_PIN_POLARITY;
1042 I915_WRITE(UTIL_PIN_CTL, val | UTIL_PIN_PIPE(pipe) |
1043 UTIL_PIN_MODE_PWM | UTIL_PIN_ENABLE);
1044 }
1045
1046 pwm_ctl = I915_READ(BXT_BLC_PWM_CTL(panel->backlight.controller));
Vandana Kannan0fb890c2015-05-05 14:51:56 +05301047 if (pwm_ctl & BXT_BLC_PWM_ENABLE) {
1048 DRM_DEBUG_KMS("backlight already enabled\n");
1049 pwm_ctl &= ~BXT_BLC_PWM_ENABLE;
Sunil Kamath022e4e52015-09-30 22:34:57 +05301050 I915_WRITE(BXT_BLC_PWM_CTL(panel->backlight.controller),
1051 pwm_ctl);
Vandana Kannan0fb890c2015-05-05 14:51:56 +05301052 }
1053
Sunil Kamath022e4e52015-09-30 22:34:57 +05301054 I915_WRITE(BXT_BLC_PWM_FREQ(panel->backlight.controller),
1055 panel->backlight.max);
Vandana Kannan0fb890c2015-05-05 14:51:56 +05301056
Maarten Lankhorst7d025e02017-06-12 12:21:15 +02001057 intel_panel_actually_set_backlight(conn_state, panel->backlight.level);
Vandana Kannan0fb890c2015-05-05 14:51:56 +05301058
1059 pwm_ctl = 0;
1060 if (panel->backlight.active_low_pwm)
1061 pwm_ctl |= BXT_BLC_PWM_POLARITY;
1062
Sunil Kamath022e4e52015-09-30 22:34:57 +05301063 I915_WRITE(BXT_BLC_PWM_CTL(panel->backlight.controller), pwm_ctl);
1064 POSTING_READ(BXT_BLC_PWM_CTL(panel->backlight.controller));
1065 I915_WRITE(BXT_BLC_PWM_CTL(panel->backlight.controller),
1066 pwm_ctl | BXT_BLC_PWM_ENABLE);
Vandana Kannan0fb890c2015-05-05 14:51:56 +05301067}
1068
Maarten Lankhorst7d025e02017-06-12 12:21:15 +02001069static void cnp_enable_backlight(const struct intel_crtc_state *crtc_state,
1070 const struct drm_connector_state *conn_state)
Rodrigo Vivi4c9f7082017-06-02 13:06:42 -07001071{
Maarten Lankhorst7d025e02017-06-12 12:21:15 +02001072 struct intel_connector *connector = to_intel_connector(conn_state->connector);
Rodrigo Vivi4c9f7082017-06-02 13:06:42 -07001073 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1074 struct intel_panel *panel = &connector->panel;
1075 u32 pwm_ctl;
1076
1077 pwm_ctl = I915_READ(BXT_BLC_PWM_CTL(panel->backlight.controller));
1078 if (pwm_ctl & BXT_BLC_PWM_ENABLE) {
1079 DRM_DEBUG_KMS("backlight already enabled\n");
1080 pwm_ctl &= ~BXT_BLC_PWM_ENABLE;
1081 I915_WRITE(BXT_BLC_PWM_CTL(panel->backlight.controller),
1082 pwm_ctl);
1083 }
1084
1085 I915_WRITE(BXT_BLC_PWM_FREQ(panel->backlight.controller),
1086 panel->backlight.max);
1087
Maarten Lankhorst7d025e02017-06-12 12:21:15 +02001088 intel_panel_actually_set_backlight(conn_state, panel->backlight.level);
Rodrigo Vivi4c9f7082017-06-02 13:06:42 -07001089
1090 pwm_ctl = 0;
1091 if (panel->backlight.active_low_pwm)
1092 pwm_ctl |= BXT_BLC_PWM_POLARITY;
1093
1094 I915_WRITE(BXT_BLC_PWM_CTL(panel->backlight.controller), pwm_ctl);
1095 POSTING_READ(BXT_BLC_PWM_CTL(panel->backlight.controller));
1096 I915_WRITE(BXT_BLC_PWM_CTL(panel->backlight.controller),
1097 pwm_ctl | BXT_BLC_PWM_ENABLE);
1098}
1099
Maarten Lankhorst7d025e02017-06-12 12:21:15 +02001100static void pwm_enable_backlight(const struct intel_crtc_state *crtc_state,
1101 const struct drm_connector_state *conn_state)
Shobhit Kumarb029e662015-06-26 14:32:10 +05301102{
Maarten Lankhorst7d025e02017-06-12 12:21:15 +02001103 struct intel_connector *connector = to_intel_connector(conn_state->connector);
Shobhit Kumarb029e662015-06-26 14:32:10 +05301104 struct intel_panel *panel = &connector->panel;
1105
1106 pwm_enable(panel->backlight.pwm);
Maarten Lankhorst7d025e02017-06-12 12:21:15 +02001107 intel_panel_actually_set_backlight(conn_state, panel->backlight.level);
Shobhit Kumarb029e662015-06-26 14:32:10 +05301108}
1109
Maarten Lankhorstb037d582017-06-12 12:21:13 +02001110void intel_panel_enable_backlight(const struct intel_crtc_state *crtc_state,
1111 const struct drm_connector_state *conn_state)
Chris Wilson47356eb2011-01-11 17:06:04 +00001112{
Maarten Lankhorstb037d582017-06-12 12:21:13 +02001113 struct intel_connector *connector = to_intel_connector(conn_state->connector);
Jani Nikulae6cb3722015-12-18 10:51:35 +02001114 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Jani Nikula58c68772013-11-08 16:48:54 +02001115 struct intel_panel *panel = &connector->panel;
Maarten Lankhorstb037d582017-06-12 12:21:13 +02001116 enum pipe pipe = to_intel_crtc(crtc_state->base.crtc)->pipe;
Jani Nikula8ba2d182013-04-12 15:18:37 +03001117
Ville Syrjälä260d8f92014-11-07 15:20:23 +02001118 if (!panel->backlight.present)
Jesse Barnes752aa882013-10-31 18:55:49 +02001119 return;
1120
Maarten Lankhorstb037d582017-06-12 12:21:13 +02001121 DRM_DEBUG_KMS("pipe %c\n", pipe_name(pipe));
Chris Wilson540b5d02013-10-13 12:56:31 +01001122
Daniel Vetter07f11d42014-09-15 14:35:09 +02001123 mutex_lock(&dev_priv->backlight_lock);
Chris Wilson47356eb2011-01-11 17:06:04 +00001124
Jani Nikulaf91c15e2013-11-08 16:49:00 +02001125 WARN_ON(panel->backlight.max == 0);
1126
Jeremiah Mahler13f3fbe2015-01-12 11:01:03 -08001127 if (panel->backlight.level <= panel->backlight.min) {
Jani Nikulaf91c15e2013-11-08 16:49:00 +02001128 panel->backlight.level = panel->backlight.max;
Jani Nikula58c68772013-11-08 16:48:54 +02001129 if (panel->backlight.device)
1130 panel->backlight.device->props.brightness =
Jani Nikula6dda7302014-06-24 18:27:40 +03001131 scale_hw_to_user(connector,
1132 panel->backlight.level,
1133 panel->backlight.device->props.max_brightness);
Jani Nikulab6b3ba52013-03-12 11:44:15 +02001134 }
Chris Wilson47356eb2011-01-11 17:06:04 +00001135
Maarten Lankhorst7d025e02017-06-12 12:21:15 +02001136 panel->backlight.enable(crtc_state, conn_state);
Jani Nikula58c68772013-11-08 16:48:54 +02001137 panel->backlight.enabled = true;
Jani Nikulaab656bb2014-08-13 12:10:12 +03001138 if (panel->backlight.device)
1139 panel->backlight.device->props.power = FB_BLANK_UNBLANK;
Jani Nikula8ba2d182013-04-12 15:18:37 +03001140
Daniel Vetter07f11d42014-09-15 14:35:09 +02001141 mutex_unlock(&dev_priv->backlight_lock);
Chris Wilson47356eb2011-01-11 17:06:04 +00001142}
1143
Jani Nikula912e8b12013-09-18 17:19:45 +03001144#if IS_ENABLED(CONFIG_BACKLIGHT_CLASS_DEVICE)
Arnd Bergmannfd94d532017-11-27 16:10:28 +01001145static u32 intel_panel_get_backlight(struct intel_connector *connector)
1146{
1147 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1148 struct intel_panel *panel = &connector->panel;
1149 u32 val = 0;
1150
1151 mutex_lock(&dev_priv->backlight_lock);
1152
1153 if (panel->backlight.enabled) {
1154 val = panel->backlight.get(connector);
1155 val = intel_panel_compute_brightness(connector, val);
1156 }
1157
1158 mutex_unlock(&dev_priv->backlight_lock);
1159
1160 DRM_DEBUG_DRIVER("get backlight PWM = %d\n", val);
1161 return val;
1162}
1163
1164/* set backlight brightness to level in range [0..max], scaling wrt hw min */
1165static void intel_panel_set_backlight(const struct drm_connector_state *conn_state,
1166 u32 user_level, u32 user_max)
1167{
1168 struct intel_connector *connector = to_intel_connector(conn_state->connector);
1169 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1170 struct intel_panel *panel = &connector->panel;
1171 u32 hw_level;
1172
1173 if (!panel->backlight.present)
1174 return;
1175
1176 mutex_lock(&dev_priv->backlight_lock);
1177
1178 WARN_ON(panel->backlight.max == 0);
1179
1180 hw_level = scale_user_to_hw(connector, user_level, user_max);
1181 panel->backlight.level = hw_level;
1182
1183 if (panel->backlight.enabled)
1184 intel_panel_actually_set_backlight(conn_state, hw_level);
1185
1186 mutex_unlock(&dev_priv->backlight_lock);
1187}
1188
Jani Nikuladb31af1d2013-11-08 16:48:53 +02001189static int intel_backlight_device_update_status(struct backlight_device *bd)
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001190{
Jesse Barnes752aa882013-10-31 18:55:49 +02001191 struct intel_connector *connector = bl_get_data(bd);
Jani Nikulaab656bb2014-08-13 12:10:12 +03001192 struct intel_panel *panel = &connector->panel;
Jesse Barnes752aa882013-10-31 18:55:49 +02001193 struct drm_device *dev = connector->base.dev;
1194
Rob Clark51fd3712013-11-19 12:10:12 -05001195 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
Chris Wilson540b5d02013-10-13 12:56:31 +01001196 DRM_DEBUG_KMS("updating intel_backlight, brightness=%d/%d\n",
1197 bd->props.brightness, bd->props.max_brightness);
Maarten Lankhorst7d025e02017-06-12 12:21:15 +02001198 intel_panel_set_backlight(connector->base.state, bd->props.brightness,
Jani Nikulad6540632013-04-12 15:18:36 +03001199 bd->props.max_brightness);
Jani Nikulaab656bb2014-08-13 12:10:12 +03001200
1201 /*
1202 * Allow flipping bl_power as a sub-state of enabled. Sadly the
1203 * backlight class device does not make it easy to to differentiate
1204 * between callbacks for brightness and bl_power, so our backlight_power
1205 * callback needs to take this into account.
1206 */
1207 if (panel->backlight.enabled) {
Jani Nikula5507fae2015-09-14 14:03:48 +03001208 if (panel->backlight.power) {
Jani Nikulae6755fb2014-08-12 17:11:42 +03001209 bool enable = bd->props.power == FB_BLANK_UNBLANK &&
1210 bd->props.brightness != 0;
Jani Nikula5507fae2015-09-14 14:03:48 +03001211 panel->backlight.power(connector, enable);
Jani Nikulaab656bb2014-08-13 12:10:12 +03001212 }
1213 } else {
1214 bd->props.power = FB_BLANK_POWERDOWN;
1215 }
1216
Rob Clark51fd3712013-11-19 12:10:12 -05001217 drm_modeset_unlock(&dev->mode_config.connection_mutex);
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001218 return 0;
1219}
1220
Jani Nikuladb31af1d2013-11-08 16:48:53 +02001221static int intel_backlight_device_get_brightness(struct backlight_device *bd)
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001222{
Jesse Barnes752aa882013-10-31 18:55:49 +02001223 struct intel_connector *connector = bl_get_data(bd);
1224 struct drm_device *dev = connector->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01001225 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikula6dda7302014-06-24 18:27:40 +03001226 u32 hw_level;
Jani Nikula7bd688c2013-11-08 16:48:56 +02001227 int ret;
Jesse Barnes752aa882013-10-31 18:55:49 +02001228
Paulo Zanonic8c8fb32013-11-27 18:21:54 -02001229 intel_runtime_pm_get(dev_priv);
Rob Clark51fd3712013-11-19 12:10:12 -05001230 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
Jani Nikula6dda7302014-06-24 18:27:40 +03001231
1232 hw_level = intel_panel_get_backlight(connector);
1233 ret = scale_hw_to_user(connector, hw_level, bd->props.max_brightness);
1234
Rob Clark51fd3712013-11-19 12:10:12 -05001235 drm_modeset_unlock(&dev->mode_config.connection_mutex);
Paulo Zanonic8c8fb32013-11-27 18:21:54 -02001236 intel_runtime_pm_put(dev_priv);
Jesse Barnes752aa882013-10-31 18:55:49 +02001237
Jani Nikula7bd688c2013-11-08 16:48:56 +02001238 return ret;
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001239}
1240
Jani Nikuladb31af1d2013-11-08 16:48:53 +02001241static const struct backlight_ops intel_backlight_device_ops = {
1242 .update_status = intel_backlight_device_update_status,
1243 .get_brightness = intel_backlight_device_get_brightness,
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001244};
1245
Chris Wilson1ebaa0b2016-06-24 14:00:15 +01001246int intel_backlight_device_register(struct intel_connector *connector)
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001247{
Jani Nikula58c68772013-11-08 16:48:54 +02001248 struct intel_panel *panel = &connector->panel;
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001249 struct backlight_properties props;
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001250
Jani Nikula58c68772013-11-08 16:48:54 +02001251 if (WARN_ON(panel->backlight.device))
Jani Nikuladc652f92013-04-12 15:18:38 +03001252 return -ENODEV;
1253
Ville Syrjälä0962c3c2014-11-07 15:19:46 +02001254 if (!panel->backlight.present)
1255 return 0;
1256
Jani Nikula6dda7302014-06-24 18:27:40 +03001257 WARN_ON(panel->backlight.max == 0);
Jani Nikula7bd688c2013-11-08 16:48:56 +02001258
Corentin Charyaf437cf2012-05-22 10:29:46 +01001259 memset(&props, 0, sizeof(props));
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001260 props.type = BACKLIGHT_RAW;
Jani Nikula6dda7302014-06-24 18:27:40 +03001261
1262 /*
1263 * Note: Everything should work even if the backlight device max
1264 * presented to the userspace is arbitrarily chosen.
1265 */
Jani Nikula7bd688c2013-11-08 16:48:56 +02001266 props.max_brightness = panel->backlight.max;
Jani Nikula6dda7302014-06-24 18:27:40 +03001267 props.brightness = scale_hw_to_user(connector,
1268 panel->backlight.level,
1269 props.max_brightness);
Jani Nikula58c68772013-11-08 16:48:54 +02001270
Jani Nikulaab656bb2014-08-13 12:10:12 +03001271 if (panel->backlight.enabled)
1272 props.power = FB_BLANK_UNBLANK;
1273 else
1274 props.power = FB_BLANK_POWERDOWN;
1275
Jani Nikula58c68772013-11-08 16:48:54 +02001276 /*
1277 * Note: using the same name independent of the connector prevents
1278 * registration of multiple backlight devices in the driver.
1279 */
1280 panel->backlight.device =
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001281 backlight_device_register("intel_backlight",
Jani Nikuladb31af1d2013-11-08 16:48:53 +02001282 connector->base.kdev,
1283 connector,
1284 &intel_backlight_device_ops, &props);
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001285
Jani Nikula58c68772013-11-08 16:48:54 +02001286 if (IS_ERR(panel->backlight.device)) {
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001287 DRM_ERROR("Failed to register backlight: %ld\n",
Jani Nikula58c68772013-11-08 16:48:54 +02001288 PTR_ERR(panel->backlight.device));
1289 panel->backlight.device = NULL;
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001290 return -ENODEV;
1291 }
Ville Syrjälä0962c3c2014-11-07 15:19:46 +02001292
1293 DRM_DEBUG_KMS("Connector %s backlight sysfs interface registered\n",
1294 connector->base.name);
1295
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001296 return 0;
1297}
1298
Chris Wilsone63d87c2016-06-17 11:40:34 +01001299void intel_backlight_device_unregister(struct intel_connector *connector)
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001300{
Jani Nikula58c68772013-11-08 16:48:54 +02001301 struct intel_panel *panel = &connector->panel;
1302
1303 if (panel->backlight.device) {
1304 backlight_device_unregister(panel->backlight.device);
1305 panel->backlight.device = NULL;
Jani Nikuladc652f92013-04-12 15:18:38 +03001306 }
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001307}
Jani Nikuladb31af1d2013-11-08 16:48:53 +02001308#endif /* CONFIG_BACKLIGHT_CLASS_DEVICE */
1309
Jani Nikulaf91c15e2013-11-08 16:49:00 +02001310/*
Rodrigo Vivi4c9f7082017-06-02 13:06:42 -07001311 * CNP: PWM clock frequency is 19.2 MHz or 24 MHz.
1312 * PWM increment = 1
1313 */
1314static u32 cnp_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
1315{
1316 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1317
1318 return DIV_ROUND_CLOSEST(KHz(dev_priv->rawclk_freq), pwm_freq_hz);
1319}
1320
1321/*
Jani Nikula2dd69822015-12-01 10:23:50 +02001322 * BXT: PWM clock frequency = 19.2 MHz.
1323 */
1324static u32 bxt_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
1325{
Ville Syrjälä37f22482016-03-02 17:22:18 +02001326 return DIV_ROUND_CLOSEST(KHz(19200), pwm_freq_hz);
Jani Nikula2dd69822015-12-01 10:23:50 +02001327}
1328
1329/*
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001330 * SPT: This value represents the period of the PWM stream in clock periods
1331 * multiplied by 16 (default increment) or 128 (alternate increment selected in
1332 * SCHICKEN_1 bit 0). PWM clock is 24 MHz.
1333 */
1334static u32 spt_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
1335{
Jani Nikula32b421e2016-09-19 13:35:25 +03001336 struct intel_panel *panel = &connector->panel;
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +02001337 u32 mul;
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001338
Jani Nikula32b421e2016-09-19 13:35:25 +03001339 if (panel->backlight.alternate_pwm_increment)
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001340 mul = 128;
1341 else
1342 mul = 16;
1343
Ville Syrjälä37f22482016-03-02 17:22:18 +02001344 return DIV_ROUND_CLOSEST(MHz(24), pwm_freq_hz * mul);
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001345}
1346
1347/*
1348 * LPT: This value represents the period of the PWM stream in clock periods
1349 * multiplied by 128 (default increment) or 16 (alternate increment, selected in
1350 * LPT SOUTH_CHICKEN2 register bit 5).
1351 */
1352static u32 lpt_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
1353{
Jani Nikulae6cb3722015-12-18 10:51:35 +02001354 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Jani Nikula32b421e2016-09-19 13:35:25 +03001355 struct intel_panel *panel = &connector->panel;
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001356 u32 mul, clock;
1357
Jani Nikula32b421e2016-09-19 13:35:25 +03001358 if (panel->backlight.alternate_pwm_increment)
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001359 mul = 16;
1360 else
1361 mul = 128;
1362
Ville Syrjälä56f5f702015-11-30 16:23:44 +02001363 if (HAS_PCH_LPT_H(dev_priv))
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001364 clock = MHz(135); /* LPT:H */
1365 else
1366 clock = MHz(24); /* LPT:LP */
1367
Ville Syrjälä37f22482016-03-02 17:22:18 +02001368 return DIV_ROUND_CLOSEST(clock, pwm_freq_hz * mul);
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001369}
1370
1371/*
1372 * ILK/SNB/IVB: This value represents the period of the PWM stream in PCH
1373 * display raw clocks multiplied by 128.
1374 */
1375static u32 pch_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
1376{
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +02001377 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001378
Ville Syrjälä37f22482016-03-02 17:22:18 +02001379 return DIV_ROUND_CLOSEST(KHz(dev_priv->rawclk_freq), pwm_freq_hz * 128);
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001380}
1381
1382/*
1383 * Gen2: This field determines the number of time base events (display core
1384 * clock frequency/32) in total for a complete cycle of modulated backlight
1385 * control.
Jani Nikulaf91c15e2013-11-08 16:49:00 +02001386 *
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001387 * Gen3: A time base event equals the display core clock ([DevPNV] HRAW clock)
1388 * divided by 32.
1389 */
1390static u32 i9xx_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
1391{
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +02001392 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001393 int clock;
1394
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +02001395 if (IS_PINEVIEW(dev_priv))
1396 clock = KHz(dev_priv->rawclk_freq);
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001397 else
Ville Syrjälä49cd97a2017-02-07 20:33:45 +02001398 clock = KHz(dev_priv->cdclk.hw.cdclk);
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001399
Ville Syrjälä37f22482016-03-02 17:22:18 +02001400 return DIV_ROUND_CLOSEST(clock, pwm_freq_hz * 32);
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001401}
1402
1403/*
1404 * Gen4: This value represents the period of the PWM stream in display core
Ville Syrjälä83d83392015-11-30 16:23:43 +02001405 * clocks ([DevCTG] HRAW clocks) multiplied by 128.
1406 *
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001407 */
1408static u32 i965_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
1409{
Mika Kahola3bed7f42016-12-13 10:02:49 +02001410 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Ville Syrjälä83d83392015-11-30 16:23:43 +02001411 int clock;
1412
1413 if (IS_G4X(dev_priv))
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +02001414 clock = KHz(dev_priv->rawclk_freq);
Ville Syrjälä83d83392015-11-30 16:23:43 +02001415 else
Ville Syrjälä49cd97a2017-02-07 20:33:45 +02001416 clock = KHz(dev_priv->cdclk.hw.cdclk);
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001417
Ville Syrjälä37f22482016-03-02 17:22:18 +02001418 return DIV_ROUND_CLOSEST(clock, pwm_freq_hz * 128);
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001419}
1420
1421/*
1422 * VLV: This value represents the period of the PWM stream in display core
1423 * clocks ([DevCTG] 200MHz HRAW clocks) multiplied by 128 or 25MHz S0IX clocks
1424 * multiplied by 16. CHV uses a 19.2MHz S0IX clock.
1425 */
1426static u32 vlv_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
1427{
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +02001428 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1429 int mul, clock;
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001430
1431 if ((I915_READ(CBR1_VLV) & CBR_PWM_CLOCK_MUX_SELECT) == 0) {
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +02001432 if (IS_CHERRYVIEW(dev_priv))
1433 clock = KHz(19200);
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001434 else
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +02001435 clock = MHz(25);
1436 mul = 16;
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001437 } else {
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +02001438 clock = KHz(dev_priv->rawclk_freq);
1439 mul = 128;
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001440 }
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +02001441
Ville Syrjälä37f22482016-03-02 17:22:18 +02001442 return DIV_ROUND_CLOSEST(clock, pwm_freq_hz * mul);
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001443}
1444
1445static u32 get_backlight_max_vbt(struct intel_connector *connector)
1446{
Jani Nikulae6cb3722015-12-18 10:51:35 +02001447 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Jani Nikula5507fae2015-09-14 14:03:48 +03001448 struct intel_panel *panel = &connector->panel;
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001449 u16 pwm_freq_hz = dev_priv->vbt.backlight.pwm_freq_hz;
1450 u32 pwm;
1451
Jani Nikula50752222015-12-01 10:23:51 +02001452 if (!panel->backlight.hz_to_pwm) {
1453 DRM_DEBUG_KMS("backlight frequency conversion not supported\n");
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001454 return 0;
1455 }
1456
Jani Nikula50752222015-12-01 10:23:51 +02001457 if (pwm_freq_hz) {
1458 DRM_DEBUG_KMS("VBT defined backlight frequency %u Hz\n",
1459 pwm_freq_hz);
1460 } else {
1461 pwm_freq_hz = 200;
1462 DRM_DEBUG_KMS("default backlight frequency %u Hz\n",
1463 pwm_freq_hz);
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001464 }
1465
Jani Nikula5507fae2015-09-14 14:03:48 +03001466 pwm = panel->backlight.hz_to_pwm(connector, pwm_freq_hz);
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001467 if (!pwm) {
1468 DRM_DEBUG_KMS("backlight frequency conversion failed\n");
1469 return 0;
1470 }
1471
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001472 return pwm;
1473}
1474
1475/*
1476 * Note: The setup hooks can't assume pipe is set!
Jani Nikulaf91c15e2013-11-08 16:49:00 +02001477 */
Jani Nikula6dda7302014-06-24 18:27:40 +03001478static u32 get_backlight_min_vbt(struct intel_connector *connector)
1479{
Jani Nikulae6cb3722015-12-18 10:51:35 +02001480 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Jani Nikula6dda7302014-06-24 18:27:40 +03001481 struct intel_panel *panel = &connector->panel;
Jani Nikulae1c412e2014-11-05 14:46:31 +02001482 int min;
Jani Nikula6dda7302014-06-24 18:27:40 +03001483
1484 WARN_ON(panel->backlight.max == 0);
1485
Jani Nikulae1c412e2014-11-05 14:46:31 +02001486 /*
1487 * XXX: If the vbt value is 255, it makes min equal to max, which leads
1488 * to problems. There are such machines out there. Either our
1489 * interpretation is wrong or the vbt has bogus data. Or both. Safeguard
1490 * against this by letting the minimum be at most (arbitrarily chosen)
1491 * 25% of the max.
1492 */
1493 min = clamp_t(int, dev_priv->vbt.backlight.min_brightness, 0, 64);
1494 if (min != dev_priv->vbt.backlight.min_brightness) {
1495 DRM_DEBUG_KMS("clamping VBT min backlight %d/255 to %d/255\n",
1496 dev_priv->vbt.backlight.min_brightness, min);
1497 }
1498
Jani Nikula6dda7302014-06-24 18:27:40 +03001499 /* vbt value is a coefficient in range [0..255] */
Jani Nikulae1c412e2014-11-05 14:46:31 +02001500 return scale(min, 0, 255, 0, panel->backlight.max);
Jani Nikula6dda7302014-06-24 18:27:40 +03001501}
1502
Jani Nikula437b15b2015-09-04 16:55:13 +03001503static int lpt_setup_backlight(struct intel_connector *connector, enum pipe unused)
Daniel Vetter96ab4c72013-11-14 15:17:41 +01001504{
Jani Nikulae6cb3722015-12-18 10:51:35 +02001505 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Daniel Vetter96ab4c72013-11-14 15:17:41 +01001506 struct intel_panel *panel = &connector->panel;
1507 u32 pch_ctl1, pch_ctl2, val;
Jani Nikula32b421e2016-09-19 13:35:25 +03001508 bool alt;
1509
1510 if (HAS_PCH_LPT(dev_priv))
1511 alt = I915_READ(SOUTH_CHICKEN2) & LPT_PWM_GRANULARITY;
1512 else
1513 alt = I915_READ(SOUTH_CHICKEN1) & SPT_PWM_GRANULARITY;
1514 panel->backlight.alternate_pwm_increment = alt;
Daniel Vetter96ab4c72013-11-14 15:17:41 +01001515
1516 pch_ctl1 = I915_READ(BLC_PWM_PCH_CTL1);
1517 panel->backlight.active_low_pwm = pch_ctl1 & BLM_PCH_POLARITY;
1518
1519 pch_ctl2 = I915_READ(BLC_PWM_PCH_CTL2);
1520 panel->backlight.max = pch_ctl2 >> 16;
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001521
1522 if (!panel->backlight.max)
1523 panel->backlight.max = get_backlight_max_vbt(connector);
1524
Daniel Vetter96ab4c72013-11-14 15:17:41 +01001525 if (!panel->backlight.max)
1526 return -ENODEV;
1527
Jani Nikula6dda7302014-06-24 18:27:40 +03001528 panel->backlight.min = get_backlight_min_vbt(connector);
1529
Jani Nikula437b15b2015-09-04 16:55:13 +03001530 val = lpt_get_backlight(connector);
Jani Nikula46e69f32016-08-23 10:10:05 +02001531 val = intel_panel_compute_brightness(connector, val);
1532 panel->backlight.level = clamp(val, panel->backlight.min,
1533 panel->backlight.max);
Daniel Vetter96ab4c72013-11-14 15:17:41 +01001534
Jani Nikula46e69f32016-08-23 10:10:05 +02001535 panel->backlight.enabled = pch_ctl1 & BLM_PCH_PWM_ENABLE;
Daniel Vetter96ab4c72013-11-14 15:17:41 +01001536
1537 return 0;
1538}
1539
Ville Syrjälä6517d272014-11-07 11:16:02 +02001540static int pch_setup_backlight(struct intel_connector *connector, enum pipe unused)
Jani Nikula7bd688c2013-11-08 16:48:56 +02001541{
Jani Nikulae6cb3722015-12-18 10:51:35 +02001542 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Jani Nikula7bd688c2013-11-08 16:48:56 +02001543 struct intel_panel *panel = &connector->panel;
Jani Nikula636baeb2013-11-08 16:49:02 +02001544 u32 cpu_ctl2, pch_ctl1, pch_ctl2, val;
Jani Nikula7bd688c2013-11-08 16:48:56 +02001545
Jani Nikula636baeb2013-11-08 16:49:02 +02001546 pch_ctl1 = I915_READ(BLC_PWM_PCH_CTL1);
1547 panel->backlight.active_low_pwm = pch_ctl1 & BLM_PCH_POLARITY;
1548
1549 pch_ctl2 = I915_READ(BLC_PWM_PCH_CTL2);
1550 panel->backlight.max = pch_ctl2 >> 16;
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001551
1552 if (!panel->backlight.max)
1553 panel->backlight.max = get_backlight_max_vbt(connector);
1554
Jani Nikula7bd688c2013-11-08 16:48:56 +02001555 if (!panel->backlight.max)
1556 return -ENODEV;
1557
Jani Nikula6dda7302014-06-24 18:27:40 +03001558 panel->backlight.min = get_backlight_min_vbt(connector);
1559
Jani Nikula7bd688c2013-11-08 16:48:56 +02001560 val = pch_get_backlight(connector);
Jani Nikula46e69f32016-08-23 10:10:05 +02001561 val = intel_panel_compute_brightness(connector, val);
1562 panel->backlight.level = clamp(val, panel->backlight.min,
1563 panel->backlight.max);
Jani Nikula7bd688c2013-11-08 16:48:56 +02001564
Jani Nikula636baeb2013-11-08 16:49:02 +02001565 cpu_ctl2 = I915_READ(BLC_PWM_CPU_CTL2);
1566 panel->backlight.enabled = (cpu_ctl2 & BLM_PWM_ENABLE) &&
Jani Nikula46e69f32016-08-23 10:10:05 +02001567 (pch_ctl1 & BLM_PCH_PWM_ENABLE);
Jani Nikula636baeb2013-11-08 16:49:02 +02001568
Jani Nikula7bd688c2013-11-08 16:48:56 +02001569 return 0;
1570}
1571
Ville Syrjälä6517d272014-11-07 11:16:02 +02001572static int i9xx_setup_backlight(struct intel_connector *connector, enum pipe unused)
Jani Nikula7bd688c2013-11-08 16:48:56 +02001573{
Jani Nikulae6cb3722015-12-18 10:51:35 +02001574 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Jani Nikula7bd688c2013-11-08 16:48:56 +02001575 struct intel_panel *panel = &connector->panel;
Jani Nikula636baeb2013-11-08 16:49:02 +02001576 u32 ctl, val;
Jani Nikula7bd688c2013-11-08 16:48:56 +02001577
Jani Nikula636baeb2013-11-08 16:49:02 +02001578 ctl = I915_READ(BLC_PWM_CTL);
1579
Jani Nikulae6cb3722015-12-18 10:51:35 +02001580 if (IS_GEN2(dev_priv) || IS_I915GM(dev_priv) || IS_I945GM(dev_priv))
Jani Nikula636baeb2013-11-08 16:49:02 +02001581 panel->backlight.combination_mode = ctl & BLM_LEGACY_MODE;
1582
Jani Nikulae6cb3722015-12-18 10:51:35 +02001583 if (IS_PINEVIEW(dev_priv))
Jani Nikula636baeb2013-11-08 16:49:02 +02001584 panel->backlight.active_low_pwm = ctl & BLM_POLARITY_PNV;
1585
1586 panel->backlight.max = ctl >> 17;
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001587
1588 if (!panel->backlight.max) {
1589 panel->backlight.max = get_backlight_max_vbt(connector);
1590 panel->backlight.max >>= 1;
1591 }
Jani Nikula636baeb2013-11-08 16:49:02 +02001592
Jani Nikula7bd688c2013-11-08 16:48:56 +02001593 if (!panel->backlight.max)
1594 return -ENODEV;
1595
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001596 if (panel->backlight.combination_mode)
1597 panel->backlight.max *= 0xff;
1598
Jani Nikula6dda7302014-06-24 18:27:40 +03001599 panel->backlight.min = get_backlight_min_vbt(connector);
1600
Jani Nikula7bd688c2013-11-08 16:48:56 +02001601 val = i9xx_get_backlight(connector);
Jani Nikula46e69f32016-08-23 10:10:05 +02001602 val = intel_panel_compute_brightness(connector, val);
1603 panel->backlight.level = clamp(val, panel->backlight.min,
1604 panel->backlight.max);
Jani Nikula7bd688c2013-11-08 16:48:56 +02001605
Jani Nikula46e69f32016-08-23 10:10:05 +02001606 panel->backlight.enabled = val != 0;
Jani Nikula636baeb2013-11-08 16:49:02 +02001607
Jani Nikula7bd688c2013-11-08 16:48:56 +02001608 return 0;
1609}
1610
Ville Syrjälä6517d272014-11-07 11:16:02 +02001611static int i965_setup_backlight(struct intel_connector *connector, enum pipe unused)
Jani Nikula7bd688c2013-11-08 16:48:56 +02001612{
Jani Nikulae6cb3722015-12-18 10:51:35 +02001613 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Jani Nikula7bd688c2013-11-08 16:48:56 +02001614 struct intel_panel *panel = &connector->panel;
Jani Nikula636baeb2013-11-08 16:49:02 +02001615 u32 ctl, ctl2, val;
Jani Nikula7bd688c2013-11-08 16:48:56 +02001616
Jani Nikula636baeb2013-11-08 16:49:02 +02001617 ctl2 = I915_READ(BLC_PWM_CTL2);
1618 panel->backlight.combination_mode = ctl2 & BLM_COMBINATION_MODE;
1619 panel->backlight.active_low_pwm = ctl2 & BLM_POLARITY_I965;
1620
1621 ctl = I915_READ(BLC_PWM_CTL);
1622 panel->backlight.max = ctl >> 16;
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001623
1624 if (!panel->backlight.max)
1625 panel->backlight.max = get_backlight_max_vbt(connector);
Jani Nikula636baeb2013-11-08 16:49:02 +02001626
Jani Nikula7bd688c2013-11-08 16:48:56 +02001627 if (!panel->backlight.max)
1628 return -ENODEV;
1629
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001630 if (panel->backlight.combination_mode)
1631 panel->backlight.max *= 0xff;
1632
Jani Nikula6dda7302014-06-24 18:27:40 +03001633 panel->backlight.min = get_backlight_min_vbt(connector);
1634
Jani Nikula7bd688c2013-11-08 16:48:56 +02001635 val = i9xx_get_backlight(connector);
Jani Nikula46e69f32016-08-23 10:10:05 +02001636 val = intel_panel_compute_brightness(connector, val);
1637 panel->backlight.level = clamp(val, panel->backlight.min,
1638 panel->backlight.max);
Jani Nikula7bd688c2013-11-08 16:48:56 +02001639
Jani Nikula46e69f32016-08-23 10:10:05 +02001640 panel->backlight.enabled = ctl2 & BLM_PWM_ENABLE;
Jani Nikula636baeb2013-11-08 16:49:02 +02001641
Jani Nikula7bd688c2013-11-08 16:48:56 +02001642 return 0;
1643}
1644
Ville Syrjälä6517d272014-11-07 11:16:02 +02001645static int vlv_setup_backlight(struct intel_connector *connector, enum pipe pipe)
Jani Nikula7bd688c2013-11-08 16:48:56 +02001646{
Jani Nikulae6cb3722015-12-18 10:51:35 +02001647 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Jani Nikula7bd688c2013-11-08 16:48:56 +02001648 struct intel_panel *panel = &connector->panel;
Jani Nikula636baeb2013-11-08 16:49:02 +02001649 u32 ctl, ctl2, val;
Jani Nikula7bd688c2013-11-08 16:48:56 +02001650
Ville Syrjälä6517d272014-11-07 11:16:02 +02001651 if (WARN_ON(pipe != PIPE_A && pipe != PIPE_B))
1652 return -ENODEV;
1653
1654 ctl2 = I915_READ(VLV_BLC_PWM_CTL2(pipe));
Jani Nikula636baeb2013-11-08 16:49:02 +02001655 panel->backlight.active_low_pwm = ctl2 & BLM_POLARITY_I965;
1656
Ville Syrjälä6517d272014-11-07 11:16:02 +02001657 ctl = I915_READ(VLV_BLC_PWM_CTL(pipe));
Jani Nikula636baeb2013-11-08 16:49:02 +02001658 panel->backlight.max = ctl >> 16;
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001659
1660 if (!panel->backlight.max)
1661 panel->backlight.max = get_backlight_max_vbt(connector);
1662
Jani Nikula7bd688c2013-11-08 16:48:56 +02001663 if (!panel->backlight.max)
1664 return -ENODEV;
1665
Jani Nikula6dda7302014-06-24 18:27:40 +03001666 panel->backlight.min = get_backlight_min_vbt(connector);
1667
Jani Nikulae6cb3722015-12-18 10:51:35 +02001668 val = _vlv_get_backlight(dev_priv, pipe);
Jani Nikula46e69f32016-08-23 10:10:05 +02001669 val = intel_panel_compute_brightness(connector, val);
1670 panel->backlight.level = clamp(val, panel->backlight.min,
1671 panel->backlight.max);
Jani Nikula7bd688c2013-11-08 16:48:56 +02001672
Jani Nikula46e69f32016-08-23 10:10:05 +02001673 panel->backlight.enabled = ctl2 & BLM_PWM_ENABLE;
Jani Nikula636baeb2013-11-08 16:49:02 +02001674
Jani Nikula7bd688c2013-11-08 16:48:56 +02001675 return 0;
1676}
1677
Vandana Kannan0fb890c2015-05-05 14:51:56 +05301678static int
1679bxt_setup_backlight(struct intel_connector *connector, enum pipe unused)
1680{
Jani Nikulae6cb3722015-12-18 10:51:35 +02001681 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Vandana Kannan0fb890c2015-05-05 14:51:56 +05301682 struct intel_panel *panel = &connector->panel;
1683 u32 pwm_ctl, val;
1684
Vidya Srinivasadd03372016-12-08 11:26:18 +02001685 panel->backlight.controller = dev_priv->vbt.backlight.controller;
Vandana Kannan0fb890c2015-05-05 14:51:56 +05301686
Sunil Kamath022e4e52015-09-30 22:34:57 +05301687 pwm_ctl = I915_READ(BXT_BLC_PWM_CTL(panel->backlight.controller));
1688
Vidya Srinivasadd03372016-12-08 11:26:18 +02001689 /* Controller 1 uses the utility pin. */
Sunil Kamath022e4e52015-09-30 22:34:57 +05301690 if (panel->backlight.controller == 1) {
1691 val = I915_READ(UTIL_PIN_CTL);
1692 panel->backlight.util_pin_active_low =
1693 val & UTIL_PIN_POLARITY;
1694 }
1695
1696 panel->backlight.active_low_pwm = pwm_ctl & BXT_BLC_PWM_POLARITY;
1697 panel->backlight.max =
1698 I915_READ(BXT_BLC_PWM_FREQ(panel->backlight.controller));
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001699
1700 if (!panel->backlight.max)
1701 panel->backlight.max = get_backlight_max_vbt(connector);
1702
Vandana Kannan0fb890c2015-05-05 14:51:56 +05301703 if (!panel->backlight.max)
1704 return -ENODEV;
1705
Lee, Shawn Cc3881122017-09-12 11:36:30 +08001706 panel->backlight.min = get_backlight_min_vbt(connector);
1707
Vandana Kannan0fb890c2015-05-05 14:51:56 +05301708 val = bxt_get_backlight(connector);
Jani Nikula46e69f32016-08-23 10:10:05 +02001709 val = intel_panel_compute_brightness(connector, val);
1710 panel->backlight.level = clamp(val, panel->backlight.min,
1711 panel->backlight.max);
Vandana Kannan0fb890c2015-05-05 14:51:56 +05301712
Jani Nikula46e69f32016-08-23 10:10:05 +02001713 panel->backlight.enabled = pwm_ctl & BXT_BLC_PWM_ENABLE;
Vandana Kannan0fb890c2015-05-05 14:51:56 +05301714
1715 return 0;
1716}
1717
Rodrigo Vivi4c9f7082017-06-02 13:06:42 -07001718static int
1719cnp_setup_backlight(struct intel_connector *connector, enum pipe unused)
1720{
1721 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1722 struct intel_panel *panel = &connector->panel;
1723 u32 pwm_ctl, val;
1724
1725 /*
Anusha Srivatsaccf6e0d2018-01-19 16:48:12 -02001726 * CNP has the BXT implementation of backlight, but with only one
1727 * controller. TODO: ICP has multiple controllers but we only use
1728 * controller 0 for now.
Rodrigo Vivi4c9f7082017-06-02 13:06:42 -07001729 */
1730 panel->backlight.controller = 0;
1731
1732 pwm_ctl = I915_READ(BXT_BLC_PWM_CTL(panel->backlight.controller));
1733
1734 panel->backlight.active_low_pwm = pwm_ctl & BXT_BLC_PWM_POLARITY;
1735 panel->backlight.max =
1736 I915_READ(BXT_BLC_PWM_FREQ(panel->backlight.controller));
1737
1738 if (!panel->backlight.max)
1739 panel->backlight.max = get_backlight_max_vbt(connector);
1740
1741 if (!panel->backlight.max)
1742 return -ENODEV;
1743
Lee, Shawn Cf44e3542017-09-13 13:19:20 +08001744 panel->backlight.min = get_backlight_min_vbt(connector);
1745
Rodrigo Vivi4c9f7082017-06-02 13:06:42 -07001746 val = bxt_get_backlight(connector);
1747 val = intel_panel_compute_brightness(connector, val);
1748 panel->backlight.level = clamp(val, panel->backlight.min,
1749 panel->backlight.max);
1750
1751 panel->backlight.enabled = pwm_ctl & BXT_BLC_PWM_ENABLE;
1752
1753 return 0;
1754}
1755
Shobhit Kumarb029e662015-06-26 14:32:10 +05301756static int pwm_setup_backlight(struct intel_connector *connector,
1757 enum pipe pipe)
1758{
1759 struct drm_device *dev = connector->base.dev;
1760 struct intel_panel *panel = &connector->panel;
1761 int retval;
1762
1763 /* Get the PWM chip for backlight control */
1764 panel->backlight.pwm = pwm_get(dev->dev, "pwm_backlight");
1765 if (IS_ERR(panel->backlight.pwm)) {
1766 DRM_ERROR("Failed to own the pwm chip\n");
1767 panel->backlight.pwm = NULL;
1768 return -ENODEV;
1769 }
1770
Boris Brezillon2347aa72016-04-14 21:17:35 +02001771 /*
1772 * FIXME: pwm_apply_args() should be removed when switching to
1773 * the atomic PWM API.
1774 */
1775 pwm_apply_args(panel->backlight.pwm);
1776
Shobhit Kumarb029e662015-06-26 14:32:10 +05301777 retval = pwm_config(panel->backlight.pwm, CRC_PMIC_PWM_PERIOD_NS,
1778 CRC_PMIC_PWM_PERIOD_NS);
1779 if (retval < 0) {
1780 DRM_ERROR("Failed to configure the pwm chip\n");
1781 pwm_put(panel->backlight.pwm);
1782 panel->backlight.pwm = NULL;
1783 return retval;
1784 }
1785
1786 panel->backlight.min = 0; /* 0% */
1787 panel->backlight.max = 100; /* 100% */
1788 panel->backlight.level = DIV_ROUND_UP(
1789 pwm_get_duty_cycle(panel->backlight.pwm) * 100,
1790 CRC_PMIC_PWM_PERIOD_NS);
1791 panel->backlight.enabled = panel->backlight.level != 0;
1792
1793 return 0;
1794}
1795
Ville Syrjälä6517d272014-11-07 11:16:02 +02001796int intel_panel_setup_backlight(struct drm_connector *connector, enum pipe pipe)
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001797{
Jani Nikulae6cb3722015-12-18 10:51:35 +02001798 struct drm_i915_private *dev_priv = to_i915(connector->dev);
Jani Nikuladb31af1d2013-11-08 16:48:53 +02001799 struct intel_connector *intel_connector = to_intel_connector(connector);
Jani Nikula58c68772013-11-08 16:48:54 +02001800 struct intel_panel *panel = &intel_connector->panel;
Jani Nikula7bd688c2013-11-08 16:48:56 +02001801 int ret;
Jani Nikuladb31af1d2013-11-08 16:48:53 +02001802
Jani Nikulac6759492014-04-09 11:31:37 +03001803 if (!dev_priv->vbt.backlight.present) {
Scot Doyle9c72cc62014-07-03 23:27:50 +00001804 if (dev_priv->quirks & QUIRK_BACKLIGHT_PRESENT) {
1805 DRM_DEBUG_KMS("no backlight present per VBT, but present per quirk\n");
1806 } else {
1807 DRM_DEBUG_KMS("no backlight present per VBT\n");
1808 return 0;
1809 }
Jani Nikulac6759492014-04-09 11:31:37 +03001810 }
1811
Jani Nikula5507fae2015-09-14 14:03:48 +03001812 /* ensure intel_panel has been initialized first */
1813 if (WARN_ON(!panel->backlight.setup))
1814 return -ENODEV;
1815
Jani Nikula7bd688c2013-11-08 16:48:56 +02001816 /* set level and max in panel struct */
Daniel Vetter07f11d42014-09-15 14:35:09 +02001817 mutex_lock(&dev_priv->backlight_lock);
Jani Nikula5507fae2015-09-14 14:03:48 +03001818 ret = panel->backlight.setup(intel_connector, pipe);
Daniel Vetter07f11d42014-09-15 14:35:09 +02001819 mutex_unlock(&dev_priv->backlight_lock);
Jani Nikuladb31af1d2013-11-08 16:48:53 +02001820
Jani Nikula7bd688c2013-11-08 16:48:56 +02001821 if (ret) {
1822 DRM_DEBUG_KMS("failed to setup backlight for connector %s\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03001823 connector->name);
Jani Nikula7bd688c2013-11-08 16:48:56 +02001824 return ret;
1825 }
1826
Jani Nikulac91c9f32013-11-08 16:48:55 +02001827 panel->backlight.present = true;
1828
Ville Syrjälä0962c3c2014-11-07 15:19:46 +02001829 DRM_DEBUG_KMS("Connector %s backlight initialized, %s, brightness %u/%u\n",
1830 connector->name,
Tvrtko Ursulin08c4d7f2016-11-17 12:30:14 +00001831 enableddisabled(panel->backlight.enabled),
Ville Syrjälä0962c3c2014-11-07 15:19:46 +02001832 panel->backlight.level, panel->backlight.max);
Jani Nikulac445b3b2013-11-08 16:49:01 +02001833
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001834 return 0;
1835}
1836
Jani Nikuladb31af1d2013-11-08 16:48:53 +02001837void intel_panel_destroy_backlight(struct drm_connector *connector)
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001838{
Jani Nikuladb31af1d2013-11-08 16:48:53 +02001839 struct intel_connector *intel_connector = to_intel_connector(connector);
Jani Nikulac91c9f32013-11-08 16:48:55 +02001840 struct intel_panel *panel = &intel_connector->panel;
Jani Nikuladb31af1d2013-11-08 16:48:53 +02001841
Shobhit Kumarb029e662015-06-26 14:32:10 +05301842 /* dispose of the pwm */
1843 if (panel->backlight.pwm)
1844 pwm_put(panel->backlight.pwm);
1845
Jani Nikulac91c9f32013-11-08 16:48:55 +02001846 panel->backlight.present = false;
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001847}
Jani Nikula1d508702012-10-19 14:51:49 +03001848
Jani Nikula7bd688c2013-11-08 16:48:56 +02001849/* Set up chip specific backlight functions */
Jani Nikula5507fae2015-09-14 14:03:48 +03001850static void
1851intel_panel_init_backlight_funcs(struct intel_panel *panel)
Jani Nikula7bd688c2013-11-08 16:48:56 +02001852{
Jani Nikulae6cb3722015-12-18 10:51:35 +02001853 struct intel_connector *connector =
Jani Nikula5507fae2015-09-14 14:03:48 +03001854 container_of(panel, struct intel_connector, panel);
Jani Nikulae6cb3722015-12-18 10:51:35 +02001855 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Jani Nikula7bd688c2013-11-08 16:48:56 +02001856
Yetunde Adebisie7156c82016-04-05 15:10:52 +01001857 if (connector->base.connector_type == DRM_MODE_CONNECTOR_eDP &&
1858 intel_dp_aux_init_backlight_funcs(connector) == 0)
1859 return;
1860
Jani Nikula90198352016-04-26 16:14:25 +03001861 if (connector->base.connector_type == DRM_MODE_CONNECTOR_DSI &&
1862 intel_dsi_dcs_init_backlight_funcs(connector) == 0)
1863 return;
1864
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02001865 if (IS_GEN9_LP(dev_priv)) {
Jani Nikula5507fae2015-09-14 14:03:48 +03001866 panel->backlight.setup = bxt_setup_backlight;
1867 panel->backlight.enable = bxt_enable_backlight;
1868 panel->backlight.disable = bxt_disable_backlight;
1869 panel->backlight.set = bxt_set_backlight;
1870 panel->backlight.get = bxt_get_backlight;
Jani Nikula2dd69822015-12-01 10:23:50 +02001871 panel->backlight.hz_to_pwm = bxt_hz_to_pwm;
Anusha Srivatsaccf6e0d2018-01-19 16:48:12 -02001872 } else if (HAS_PCH_CNP(dev_priv) || HAS_PCH_ICP(dev_priv)) {
Rodrigo Vivi4c9f7082017-06-02 13:06:42 -07001873 panel->backlight.setup = cnp_setup_backlight;
1874 panel->backlight.enable = cnp_enable_backlight;
1875 panel->backlight.disable = cnp_disable_backlight;
1876 panel->backlight.set = bxt_set_backlight;
1877 panel->backlight.get = bxt_get_backlight;
1878 panel->backlight.hz_to_pwm = cnp_hz_to_pwm;
Rodrigo Vivi22dea0b2016-07-01 17:07:12 -07001879 } else if (HAS_PCH_LPT(dev_priv) || HAS_PCH_SPT(dev_priv) ||
1880 HAS_PCH_KBP(dev_priv)) {
Jani Nikula5507fae2015-09-14 14:03:48 +03001881 panel->backlight.setup = lpt_setup_backlight;
1882 panel->backlight.enable = lpt_enable_backlight;
1883 panel->backlight.disable = lpt_disable_backlight;
1884 panel->backlight.set = lpt_set_backlight;
1885 panel->backlight.get = lpt_get_backlight;
Jani Nikulae6cb3722015-12-18 10:51:35 +02001886 if (HAS_PCH_LPT(dev_priv))
Jani Nikula5507fae2015-09-14 14:03:48 +03001887 panel->backlight.hz_to_pwm = lpt_hz_to_pwm;
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001888 else
Jani Nikula5507fae2015-09-14 14:03:48 +03001889 panel->backlight.hz_to_pwm = spt_hz_to_pwm;
Jani Nikulae6cb3722015-12-18 10:51:35 +02001890 } else if (HAS_PCH_SPLIT(dev_priv)) {
Jani Nikula5507fae2015-09-14 14:03:48 +03001891 panel->backlight.setup = pch_setup_backlight;
1892 panel->backlight.enable = pch_enable_backlight;
1893 panel->backlight.disable = pch_disable_backlight;
1894 panel->backlight.set = pch_set_backlight;
1895 panel->backlight.get = pch_get_backlight;
1896 panel->backlight.hz_to_pwm = pch_hz_to_pwm;
Jani Nikulae6cb3722015-12-18 10:51:35 +02001897 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Jani Nikula92c45652016-03-16 12:43:33 +02001898 if (connector->base.connector_type == DRM_MODE_CONNECTOR_DSI) {
Jani Nikula5507fae2015-09-14 14:03:48 +03001899 panel->backlight.setup = pwm_setup_backlight;
1900 panel->backlight.enable = pwm_enable_backlight;
1901 panel->backlight.disable = pwm_disable_backlight;
1902 panel->backlight.set = pwm_set_backlight;
1903 panel->backlight.get = pwm_get_backlight;
Shobhit Kumarb029e662015-06-26 14:32:10 +05301904 } else {
Jani Nikula5507fae2015-09-14 14:03:48 +03001905 panel->backlight.setup = vlv_setup_backlight;
1906 panel->backlight.enable = vlv_enable_backlight;
1907 panel->backlight.disable = vlv_disable_backlight;
1908 panel->backlight.set = vlv_set_backlight;
1909 panel->backlight.get = vlv_get_backlight;
1910 panel->backlight.hz_to_pwm = vlv_hz_to_pwm;
Shobhit Kumarb029e662015-06-26 14:32:10 +05301911 }
Jani Nikulae6cb3722015-12-18 10:51:35 +02001912 } else if (IS_GEN4(dev_priv)) {
Jani Nikula5507fae2015-09-14 14:03:48 +03001913 panel->backlight.setup = i965_setup_backlight;
1914 panel->backlight.enable = i965_enable_backlight;
1915 panel->backlight.disable = i965_disable_backlight;
1916 panel->backlight.set = i9xx_set_backlight;
1917 panel->backlight.get = i9xx_get_backlight;
1918 panel->backlight.hz_to_pwm = i965_hz_to_pwm;
Jani Nikula7bd688c2013-11-08 16:48:56 +02001919 } else {
Jani Nikula5507fae2015-09-14 14:03:48 +03001920 panel->backlight.setup = i9xx_setup_backlight;
1921 panel->backlight.enable = i9xx_enable_backlight;
1922 panel->backlight.disable = i9xx_disable_backlight;
1923 panel->backlight.set = i9xx_set_backlight;
1924 panel->backlight.get = i9xx_get_backlight;
1925 panel->backlight.hz_to_pwm = i9xx_hz_to_pwm;
Jani Nikula7bd688c2013-11-08 16:48:56 +02001926 }
1927}
1928
Jani Nikuladd06f902012-10-19 14:51:50 +03001929int intel_panel_init(struct intel_panel *panel,
Vandana Kannan4b6ed682014-02-11 14:26:36 +05301930 struct drm_display_mode *fixed_mode,
Jim Bridedc911f52017-08-09 12:48:53 -07001931 struct drm_display_mode *alt_fixed_mode,
Vandana Kannan4b6ed682014-02-11 14:26:36 +05301932 struct drm_display_mode *downclock_mode)
Jani Nikula1d508702012-10-19 14:51:49 +03001933{
Jani Nikula5507fae2015-09-14 14:03:48 +03001934 intel_panel_init_backlight_funcs(panel);
1935
Jani Nikuladd06f902012-10-19 14:51:50 +03001936 panel->fixed_mode = fixed_mode;
Jim Bridedc911f52017-08-09 12:48:53 -07001937 panel->alt_fixed_mode = alt_fixed_mode;
Vandana Kannan4b6ed682014-02-11 14:26:36 +05301938 panel->downclock_mode = downclock_mode;
Jani Nikuladd06f902012-10-19 14:51:50 +03001939
Jani Nikula1d508702012-10-19 14:51:49 +03001940 return 0;
1941}
1942
1943void intel_panel_fini(struct intel_panel *panel)
1944{
Jani Nikuladd06f902012-10-19 14:51:50 +03001945 struct intel_connector *intel_connector =
1946 container_of(panel, struct intel_connector, panel);
1947
1948 if (panel->fixed_mode)
1949 drm_mode_destroy(intel_connector->base.dev, panel->fixed_mode);
Vandana Kannanec9ed192013-12-10 13:37:36 +05301950
Jim Bridedc911f52017-08-09 12:48:53 -07001951 if (panel->alt_fixed_mode)
1952 drm_mode_destroy(intel_connector->base.dev,
1953 panel->alt_fixed_mode);
1954
Vandana Kannanec9ed192013-12-10 13:37:36 +05301955 if (panel->downclock_mode)
1956 drm_mode_destroy(intel_connector->base.dev,
1957 panel->downclock_mode);
Jani Nikula1d508702012-10-19 14:51:49 +03001958}